]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
Remove Cilk Plus support.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
cbe34bb5 2 Copyright (C) 1987-2017 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);
db3927fb 98static tree pointer_diff (location_t, 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)
744aa42f 1180 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1181 enum_and_int_p,
1182 different_types_p)))
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:
744aa42f
ILT
1240 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
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)
744aa42f 1665 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1666 enum_and_int_p, different_types_p))
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)
744aa42f 1675 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1676 enum_and_int_p, different_types_p))
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
2f6e4e97 1835decl_constant_value (tree decl)
400fbf9f 1836{
a7c1916a 1837 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1838 in a place where a variable is invalid. Note that DECL_INITIAL
1839 isn't valid for a PARM_DECL. */
3fa8871b 1840 current_function_decl != NULL_TREE
4f976745 1841 && TREE_CODE (decl) != PARM_DECL
3f75a254 1842 && !TREE_THIS_VOLATILE (decl)
83bab8db 1843 && TREE_READONLY (decl)
3fa8871b 1844 && DECL_INITIAL (decl) != NULL_TREE
400fbf9f
JW
1845 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1846 /* This is invalid if initial value is not constant.
1847 If it has either a function call, a memory reference,
1848 or a variable, then re-evaluating it could give different results. */
1849 && TREE_CONSTANT (DECL_INITIAL (decl))
1850 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1851 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1852 return DECL_INITIAL (decl);
1853 return decl;
1854}
1855
f2a71bbc
JM
1856/* Convert the array expression EXP to a pointer. */
1857static tree
c2255bc4 1858array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1859{
f2a71bbc 1860 tree orig_exp = exp;
207bf485 1861 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1862 tree adr;
1863 tree restype = TREE_TYPE (type);
1864 tree ptrtype;
207bf485 1865
f2a71bbc 1866 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1867
f2a71bbc 1868 STRIP_TYPE_NOPS (exp);
207bf485 1869
487a92fe
JM
1870 if (TREE_NO_WARNING (orig_exp))
1871 TREE_NO_WARNING (exp) = 1;
207bf485 1872
f2a71bbc
JM
1873 ptrtype = build_pointer_type (restype);
1874
22d03525 1875 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1876 return convert (ptrtype, TREE_OPERAND (exp, 0));
1877
1f37c583
JM
1878 /* In C++ array compound literals are temporary objects unless they are
1879 const or appear in namespace scope, so they are destroyed too soon
1880 to use them for much of anything (c++/53220). */
1881 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1882 {
1883 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1884 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1885 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1886 "converting an array compound literal to a pointer "
1887 "is ill-formed in C++");
1888 }
1889
e51fbec3 1890 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
f2a71bbc
JM
1891 return convert (ptrtype, adr);
1892}
207bf485 1893
f2a71bbc
JM
1894/* Convert the function expression EXP to a pointer. */
1895static tree
c2255bc4 1896function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1897{
1898 tree orig_exp = exp;
207bf485 1899
f2a71bbc 1900 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1901
f2a71bbc 1902 STRIP_TYPE_NOPS (exp);
207bf485 1903
f2a71bbc
JM
1904 if (TREE_NO_WARNING (orig_exp))
1905 TREE_NO_WARNING (exp) = 1;
207bf485 1906
e51fbec3 1907 return build_unary_op (loc, ADDR_EXPR, exp, false);
f2a71bbc 1908}
207bf485 1909
ebfbbdc5
JJ
1910/* Mark EXP as read, not just set, for set but not used -Wunused
1911 warning purposes. */
1912
1913void
1914mark_exp_read (tree exp)
1915{
1916 switch (TREE_CODE (exp))
1917 {
1918 case VAR_DECL:
1919 case PARM_DECL:
1920 DECL_READ_P (exp) = 1;
1921 break;
1922 case ARRAY_REF:
1923 case COMPONENT_REF:
1924 case MODIFY_EXPR:
1925 case REALPART_EXPR:
1926 case IMAGPART_EXPR:
1927 CASE_CONVERT:
1928 case ADDR_EXPR:
557e8c49 1929 case VIEW_CONVERT_EXPR:
ebfbbdc5
JJ
1930 mark_exp_read (TREE_OPERAND (exp, 0));
1931 break;
1932 case COMPOUND_EXPR:
82c3c067 1933 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1934 mark_exp_read (TREE_OPERAND (exp, 1));
1935 break;
1936 default:
1937 break;
1938 }
1939}
1940
f2a71bbc
JM
1941/* Perform the default conversion of arrays and functions to pointers.
1942 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1943 return EXP.
1944
1945 LOC is the location of the expression. */
f2a71bbc
JM
1946
1947struct c_expr
c2255bc4 1948default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1949{
1950 tree orig_exp = exp.value;
1951 tree type = TREE_TYPE (exp.value);
1952 enum tree_code code = TREE_CODE (type);
1953
1954 switch (code)
1955 {
1956 case ARRAY_TYPE:
1957 {
1958 bool not_lvalue = false;
1959 bool lvalue_array_p;
1960
1961 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1962 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1963 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1964 {
1965 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1966 not_lvalue = true;
1967 exp.value = TREE_OPERAND (exp.value, 0);
1968 }
1969
1970 if (TREE_NO_WARNING (orig_exp))
1971 TREE_NO_WARNING (exp.value) = 1;
1972
1973 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1974 if (!flag_isoc99 && !lvalue_array_p)
1975 {
1976 /* Before C99, non-lvalue arrays do not decay to pointers.
1977 Normally, using such an array would be invalid; but it can
1978 be used correctly inside sizeof or as a statement expression.
1979 Thus, do not give an error here; an error will result later. */
1980 return exp;
1981 }
1982
c2255bc4 1983 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1984 }
1985 break;
1986 case FUNCTION_TYPE:
c2255bc4 1987 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1988 break;
1989 default:
f2a71bbc 1990 break;
207bf485 1991 }
f2a71bbc 1992
207bf485
JM
1993 return exp;
1994}
1995
ebfbbdc5
JJ
1996struct c_expr
1997default_function_array_read_conversion (location_t loc, struct c_expr exp)
1998{
1999 mark_exp_read (exp.value);
2000 return default_function_array_conversion (loc, exp);
2001}
522ddfa2 2002
267bac10
JM
2003/* Return whether EXPR should be treated as an atomic lvalue for the
2004 purposes of load and store handling. */
2005
2006static bool
2007really_atomic_lvalue (tree expr)
2008{
7a0ca710 2009 if (error_operand_p (expr))
267bac10
JM
2010 return false;
2011 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2012 return false;
2013 if (!lvalue_p (expr))
2014 return false;
2015
2016 /* Ignore _Atomic on register variables, since their addresses can't
2017 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2018 sequences wouldn't work. Ignore _Atomic on structures containing
2019 bit-fields, since accessing elements of atomic structures or
2020 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2021 it's undefined at translation time or execution time, and the
2022 normal atomic sequences again wouldn't work. */
2023 while (handled_component_p (expr))
2024 {
2025 if (TREE_CODE (expr) == COMPONENT_REF
2026 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2027 return false;
2028 expr = TREE_OPERAND (expr, 0);
2029 }
2030 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2031 return false;
2032 return true;
2033}
2034
2035/* Convert expression EXP (location LOC) from lvalue to rvalue,
2036 including converting functions and arrays to pointers if CONVERT_P.
2037 If READ_P, also mark the expression as having been read. */
2038
2039struct c_expr
2040convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2041 bool convert_p, bool read_p)
2042{
2043 if (read_p)
2044 mark_exp_read (exp.value);
2045 if (convert_p)
2046 exp = default_function_array_conversion (loc, exp);
2047 if (really_atomic_lvalue (exp.value))
2048 {
2049 vec<tree, va_gc> *params;
2050 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2051 tree expr_type = TREE_TYPE (exp.value);
e51fbec3 2052 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
267bac10
JM
2053 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2054
2055 gcc_assert (TYPE_ATOMIC (expr_type));
2056
2057 /* Expansion of a generic atomic load may require an addition
2058 element, so allocate enough to prevent a resize. */
2059 vec_alloc (params, 4);
2060
2061 /* Remove the qualifiers for the rest of the expressions and
2062 create the VAL temp variable to hold the RHS. */
2063 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2064 tmp = create_tmp_var_raw (nonatomic_type);
e51fbec3 2065 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
267bac10 2066 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2067 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2068
2069 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2070 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2071 params->quick_push (expr_addr);
2072 params->quick_push (tmp_addr);
2073 params->quick_push (seq_cst);
8edbfaa6 2074 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2075
cc28fc7f
MP
2076 /* EXPR is always read. */
2077 mark_exp_read (exp.value);
2078
267bac10 2079 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2080 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2081 NULL_TREE, NULL_TREE);
267bac10
JM
2082 }
2083 return exp;
2084}
2085
522ddfa2
JM
2086/* EXP is an expression of integer type. Apply the integer promotions
2087 to it and return the promoted value. */
400fbf9f
JW
2088
2089tree
522ddfa2 2090perform_integral_promotions (tree exp)
400fbf9f 2091{
b3694847
SS
2092 tree type = TREE_TYPE (exp);
2093 enum tree_code code = TREE_CODE (type);
400fbf9f 2094
522ddfa2 2095 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2096
400fbf9f
JW
2097 /* Normally convert enums to int,
2098 but convert wide enums to something wider. */
2099 if (code == ENUMERAL_TYPE)
2100 {
b0c48229
NB
2101 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2102 TYPE_PRECISION (integer_type_node)),
2103 ((TYPE_PRECISION (type)
2104 >= TYPE_PRECISION (integer_type_node))
8df83eae 2105 && TYPE_UNSIGNED (type)));
05bccae2 2106
400fbf9f
JW
2107 return convert (type, exp);
2108 }
2109
522ddfa2
JM
2110 /* ??? This should no longer be needed now bit-fields have their
2111 proper types. */
9753f113 2112 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2113 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2114 /* If it's thinner than an int, promote it like a
d72040f5 2115 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2116 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2117 TYPE_PRECISION (integer_type_node)))
f458d1d5 2118 return convert (integer_type_node, exp);
9753f113 2119
d72040f5 2120 if (c_promoting_integer_type_p (type))
400fbf9f 2121 {
f458d1d5 2122 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2123 if (TYPE_UNSIGNED (type)
f458d1d5 2124 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2125 return convert (unsigned_type_node, exp);
05bccae2 2126
400fbf9f
JW
2127 return convert (integer_type_node, exp);
2128 }
05bccae2 2129
522ddfa2
JM
2130 return exp;
2131}
2132
2133
2134/* Perform default promotions for C data used in expressions.
46bdb9cf 2135 Enumeral types or short or char are converted to int.
522ddfa2
JM
2136 In addition, manifest constants symbols are replaced by their values. */
2137
2138tree
2139default_conversion (tree exp)
2140{
2141 tree orig_exp;
2142 tree type = TREE_TYPE (exp);
2143 enum tree_code code = TREE_CODE (type);
40449a90 2144 tree promoted_type;
522ddfa2 2145
ebfbbdc5
JJ
2146 mark_exp_read (exp);
2147
46bdb9cf
JM
2148 /* Functions and arrays have been converted during parsing. */
2149 gcc_assert (code != FUNCTION_TYPE);
2150 if (code == ARRAY_TYPE)
2151 return exp;
522ddfa2
JM
2152
2153 /* Constants can be used directly unless they're not loadable. */
2154 if (TREE_CODE (exp) == CONST_DECL)
2155 exp = DECL_INITIAL (exp);
2156
522ddfa2
JM
2157 /* Strip no-op conversions. */
2158 orig_exp = exp;
2159 STRIP_TYPE_NOPS (exp);
2160
2161 if (TREE_NO_WARNING (orig_exp))
2162 TREE_NO_WARNING (exp) = 1;
2163
400fbf9f
JW
2164 if (code == VOID_TYPE)
2165 {
5436fa2e
MP
2166 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2167 "void value not ignored as it ought to be");
400fbf9f
JW
2168 return error_mark_node;
2169 }
808d6eaa 2170
4f2e1536 2171 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
808d6eaa
JM
2172 if (exp == error_mark_node)
2173 return error_mark_node;
2174
40449a90
SL
2175 promoted_type = targetm.promoted_type (type);
2176 if (promoted_type)
2177 return convert (promoted_type, exp);
2178
808d6eaa
JM
2179 if (INTEGRAL_TYPE_P (type))
2180 return perform_integral_promotions (exp);
2181
400fbf9f
JW
2182 return exp;
2183}
2184\f
0fb96aa4 2185/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2186
2187 If the component name is not found, returns NULL_TREE. Otherwise,
2188 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2189 stepping down the chain to the component, which is in the last
2190 TREE_VALUE of the list. Normally the list is of length one, but if
2191 the component is embedded within (nested) anonymous structures or
2192 unions, the list steps down the chain to the component. */
2f6e4e97 2193
2f2d13da 2194static tree
0fb96aa4 2195lookup_field (tree type, tree component)
2f2d13da
DE
2196{
2197 tree field;
2198
2199 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2200 to the field elements. Use a binary search on this array to quickly
2201 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2202 will always be set for structures which have many elements. */
2203
22a0b85f 2204 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2205 {
2206 int bot, top, half;
d07605f5 2207 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2208
2209 field = TYPE_FIELDS (type);
2210 bot = 0;
d07605f5 2211 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2212 while (top - bot > 1)
2213 {
2f2d13da
DE
2214 half = (top - bot + 1) >> 1;
2215 field = field_array[bot+half];
2216
2217 if (DECL_NAME (field) == NULL_TREE)
2218 {
2219 /* Step through all anon unions in linear fashion. */
2220 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2221 {
2f2d13da 2222 field = field_array[bot++];
3e636daf 2223 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2224 {
0fb96aa4 2225 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2226
2227 if (anon)
2228 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2229
2230 /* The Plan 9 compiler permits referring
2231 directly to an anonymous struct/union field
2232 using a typedef name. */
2233 if (flag_plan9_extensions
2234 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2235 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2236 == TYPE_DECL)
2237 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2238 == component))
2239 break;
2f6e4e97 2240 }
2f2d13da
DE
2241 }
2242
2243 /* Entire record is only anon unions. */
2244 if (bot > top)
2245 return NULL_TREE;
2246
2247 /* Restart the binary search, with new lower bound. */
2248 continue;
2249 }
2250
e8b87aac 2251 if (DECL_NAME (field) == component)
2f2d13da 2252 break;
e8b87aac 2253 if (DECL_NAME (field) < component)
2f2d13da
DE
2254 bot += half;
2255 else
2256 top = bot + half;
2257 }
2258
2259 if (DECL_NAME (field_array[bot]) == component)
2260 field = field_array[bot];
2261 else if (DECL_NAME (field) != component)
e9b2c823 2262 return NULL_TREE;
2f2d13da
DE
2263 }
2264 else
2265 {
910ad8de 2266 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2267 {
e9b2c823 2268 if (DECL_NAME (field) == NULL_TREE
3e636daf 2269 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2270 {
0fb96aa4 2271 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2272
e9b2c823
NB
2273 if (anon)
2274 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2275
2276 /* The Plan 9 compiler permits referring directly to an
2277 anonymous struct/union field using a typedef
2278 name. */
2279 if (flag_plan9_extensions
2280 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2281 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2282 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2283 == component))
2284 break;
2f2d13da
DE
2285 }
2286
2287 if (DECL_NAME (field) == component)
2288 break;
2289 }
e9b2c823
NB
2290
2291 if (field == NULL_TREE)
2292 return NULL_TREE;
2f2d13da
DE
2293 }
2294
e9b2c823 2295 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2296}
2297
277fe616
DM
2298/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2299
2300static void
2301lookup_field_fuzzy_find_candidates (tree type, tree component,
2302 vec<tree> *candidates)
2303{
2304 tree field;
2305 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2306 {
2307 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2308 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2309 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2310 candidates);
277fe616
DM
2311
2312 if (DECL_NAME (field))
2313 candidates->safe_push (DECL_NAME (field));
2314 }
2315}
2316
2317/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2318 rather than returning a TREE_LIST for an exact match. */
2319
2320static tree
2321lookup_field_fuzzy (tree type, tree component)
2322{
2323 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2324
2325 /* First, gather a list of candidates. */
2326 auto_vec <tree> candidates;
2327
2328 lookup_field_fuzzy_find_candidates (type, component,
2329 &candidates);
2330
8ece8dfb 2331 return find_closest_identifier (component, &candidates);
277fe616
DM
2332}
2333
a87a86e1
DM
2334/* Support function for build_component_ref's error-handling.
2335
2336 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2337 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2338
2339static bool
2340should_suggest_deref_p (tree datum_type)
2341{
2342 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2343 allows "." for ptrs; we could be handling a failed attempt
2344 to access a property. */
2345 if (c_dialect_objc ())
2346 return false;
2347
2348 /* Only suggest it for pointers... */
2349 if (TREE_CODE (datum_type) != POINTER_TYPE)
2350 return false;
2351
2352 /* ...to structs/unions. */
2353 tree underlying_type = TREE_TYPE (datum_type);
2354 enum tree_code code = TREE_CODE (underlying_type);
2355 if (code == RECORD_TYPE || code == UNION_TYPE)
2356 return true;
2357 else
2358 return false;
2359}
2360
c2255bc4
AH
2361/* Make an expression to refer to the COMPONENT field of structure or
2362 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
6ffd47b7
DM
2363 location of the COMPONENT_REF. COMPONENT_LOC is the location
2364 of COMPONENT. */
400fbf9f
JW
2365
2366tree
6ffd47b7
DM
2367build_component_ref (location_t loc, tree datum, tree component,
2368 location_t component_loc)
400fbf9f 2369{
b3694847
SS
2370 tree type = TREE_TYPE (datum);
2371 enum tree_code code = TREE_CODE (type);
2372 tree field = NULL;
2373 tree ref;
1e57bf47 2374 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2375
7a3ea201
RH
2376 if (!objc_is_public (datum, component))
2377 return error_mark_node;
2378
46a88c12 2379 /* Detect Objective-C property syntax object.property. */
668ea4b1 2380 if (c_dialect_objc ()
46a88c12 2381 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2382 return ref;
2383
400fbf9f
JW
2384 /* See if there is a field or component with name COMPONENT. */
2385
2386 if (code == RECORD_TYPE || code == UNION_TYPE)
2387 {
d0f062fb 2388 if (!COMPLETE_TYPE_P (type))
400fbf9f 2389 {
4f2e1536 2390 c_incomplete_type_error (loc, NULL_TREE, type);
400fbf9f
JW
2391 return error_mark_node;
2392 }
2393
0fb96aa4 2394 field = lookup_field (type, component);
400fbf9f
JW
2395
2396 if (!field)
2397 {
277fe616
DM
2398 tree guessed_id = lookup_field_fuzzy (type, component);
2399 if (guessed_id)
6ffd47b7
DM
2400 {
2401 /* Attempt to provide a fixit replacement hint, if
2402 we have a valid range for the component. */
2403 location_t reported_loc
2404 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
264757fb 2405 gcc_rich_location rich_loc (reported_loc);
6ffd47b7 2406 if (component_loc != UNKNOWN_LOCATION)
264757fb 2407 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
64a5912c
DM
2408 error_at (&rich_loc,
2409 "%qT has no member named %qE; did you mean %qE?",
2410 type, component, guessed_id);
6ffd47b7 2411 }
277fe616
DM
2412 else
2413 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2414 return error_mark_node;
2415 }
400fbf9f 2416
fda5652f
MP
2417 /* Accessing elements of atomic structures or unions is undefined
2418 behavior (C11 6.5.2.3#5). */
2419 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2420 {
2421 if (code == RECORD_TYPE)
2422 warning_at (loc, 0, "accessing a member %qE of an atomic "
2423 "structure %qE", component, datum);
2424 else
2425 warning_at (loc, 0, "accessing a member %qE of an atomic "
2426 "union %qE", component, datum);
2427 }
2428
e9b2c823
NB
2429 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2430 This might be better solved in future the way the C++ front
2431 end does it - by giving the anonymous entities each a
2432 separate name and type, and then have build_component_ref
2433 recursively call itself. We can't do that here. */
46ea50cb 2434 do
19d76e60 2435 {
e9b2c823 2436 tree subdatum = TREE_VALUE (field);
efed193e
JM
2437 int quals;
2438 tree subtype;
1e57bf47 2439 bool use_datum_quals;
e9b2c823
NB
2440
2441 if (TREE_TYPE (subdatum) == error_mark_node)
2442 return error_mark_node;
2443
1e57bf47
JM
2444 /* If this is an rvalue, it does not have qualifiers in C
2445 standard terms and we must avoid propagating such
2446 qualifiers down to a non-lvalue array that is then
2447 converted to a pointer. */
2448 use_datum_quals = (datum_lvalue
2449 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2450
efed193e 2451 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2452 if (use_datum_quals)
2453 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2454 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2455
2456 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2457 NULL_TREE);
c2255bc4 2458 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2459 if (TREE_READONLY (subdatum)
2460 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2461 TREE_READONLY (ref) = 1;
1e57bf47
JM
2462 if (TREE_THIS_VOLATILE (subdatum)
2463 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2464 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2465
2466 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2467 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2468
19d76e60 2469 datum = ref;
46ea50cb
RS
2470
2471 field = TREE_CHAIN (field);
19d76e60 2472 }
46ea50cb 2473 while (field);
19d76e60 2474
400fbf9f
JW
2475 return ref;
2476 }
a87a86e1
DM
2477 else if (should_suggest_deref_p (type))
2478 {
2479 /* Special-case the error message for "ptr.field" for the case
2480 where the user has confused "." vs "->". */
2481 rich_location richloc (line_table, loc);
2482 /* "loc" should be the "." token. */
f9087798 2483 richloc.add_fixit_replace ("->");
64a5912c
DM
2484 error_at (&richloc,
2485 "%qE is a pointer; did you mean to use %<->%>?",
2486 datum);
a87a86e1
DM
2487 return error_mark_node;
2488 }
400fbf9f 2489 else if (code != ERROR_MARK)
c2255bc4
AH
2490 error_at (loc,
2491 "request for member %qE in something not a structure or union",
2492 component);
400fbf9f
JW
2493
2494 return error_mark_node;
2495}
2496\f
2497/* Given an expression PTR for a pointer, return an expression
2498 for the value pointed to.
6a3799eb
AH
2499 ERRORSTRING is the name of the operator to appear in error messages.
2500
2501 LOC is the location to use for the generated tree. */
400fbf9f
JW
2502
2503tree
dd865ef6 2504build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2505{
b3694847
SS
2506 tree pointer = default_conversion (ptr);
2507 tree type = TREE_TYPE (pointer);
6a3799eb 2508 tree ref;
400fbf9f
JW
2509
2510 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2511 {
1043771b 2512 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2513 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2514 {
2515 /* If a warning is issued, mark it to avoid duplicates from
2516 the backend. This only needs to be done at
2517 warn_strict_aliasing > 2. */
2518 if (warn_strict_aliasing > 2)
2519 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2520 type, TREE_OPERAND (pointer, 0)))
2521 TREE_NO_WARNING (pointer) = 1;
2522 }
2523
870cc33b 2524 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2525 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2526 == TREE_TYPE (type)))
6a3799eb
AH
2527 {
2528 ref = TREE_OPERAND (pointer, 0);
2529 protected_set_expr_location (ref, loc);
2530 return ref;
2531 }
870cc33b
RS
2532 else
2533 {
2534 tree t = TREE_TYPE (type);
46df2823 2535
984dfd8c 2536 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2537
baae9b65 2538 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2539 {
d9b7be2e
MP
2540 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2541 {
2542 error_at (loc, "dereferencing pointer to incomplete type "
2543 "%qT", t);
2544 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2545 }
870cc33b
RS
2546 return error_mark_node;
2547 }
7d882b83 2548 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2549 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2550
2551 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2552 so that we get the proper error message if the result is used
2553 to assign to. Also, &* is supposed to be a no-op.
2554 And ANSI C seems to specify that the type of the result
2555 should be the const type. */
2556 /* A de-reference of a pointer to const is not a const. It is valid
2557 to change it via some other pointer. */
2558 TREE_READONLY (ref) = TYPE_READONLY (t);
2559 TREE_SIDE_EFFECTS (ref)
271bd540 2560 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2561 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2562 protected_set_expr_location (ref, loc);
870cc33b
RS
2563 return ref;
2564 }
2565 }
400fbf9f 2566 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2567 invalid_indirection_error (loc, type, errstring);
2568
400fbf9f
JW
2569 return error_mark_node;
2570}
2571
2572/* This handles expressions of the form "a[i]", which denotes
2573 an array reference.
2574
2575 This is logically equivalent in C to *(a+i), but we may do it differently.
2576 If A is a variable or a member, we generate a primitive ARRAY_REF.
2577 This avoids forcing the array out of registers, and can work on
2578 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2579 by functions).
2580
30cd1c5d
AS
2581 For vector types, allow vector[i] but not i[vector], and create
2582 *(((type*)&vectortype) + i) for the expression.
2583
6a3799eb 2584 LOC is the location to use for the returned expression. */
400fbf9f
JW
2585
2586tree
c2255bc4 2587build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2588{
6a3799eb 2589 tree ret;
a4ab7973 2590 bool swapped = false;
400fbf9f
JW
2591 if (TREE_TYPE (array) == error_mark_node
2592 || TREE_TYPE (index) == error_mark_node)
2593 return error_mark_node;
2594
a4ab7973 2595 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2596 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2597 /* Allow vector[index] but not index[vector]. */
31521951 2598 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2599 {
a4ab7973
JM
2600 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2601 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2602 {
f90e8e2e 2603 error_at (loc,
30cd1c5d
AS
2604 "subscripted value is neither array nor pointer nor vector");
2605
fdeefd49
RS
2606 return error_mark_node;
2607 }
fab27f52 2608 std::swap (array, index);
a4ab7973
JM
2609 swapped = true;
2610 }
2611
2612 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2613 {
a63068b6 2614 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2615 return error_mark_node;
2616 }
2617
2618 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2619 {
a63068b6 2620 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2621 return error_mark_node;
2622 }
2623
ff6b6641
GDR
2624 /* ??? Existing practice has been to warn only when the char
2625 index is syntactically the index, not for char[array]. */
2626 if (!swapped)
5bd012f8 2627 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2628
2629 /* Apply default promotions *after* noticing character types. */
2630 index = default_conversion (index);
f406ae1f
MP
2631 if (index == error_mark_node)
2632 return error_mark_node;
a4ab7973
JM
2633
2634 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2635
f17a223d
RB
2636 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2637 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
a4ab7973
JM
2638
2639 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2640 {
e4d35515 2641 tree rval, type;
fdeefd49 2642
400fbf9f
JW
2643 /* An array that is indexed by a non-constant
2644 cannot be stored in a register; we must be able to do
2645 address arithmetic on its address.
2646 Likewise an array of elements of variable size. */
2647 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2648 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2649 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2650 {
a9e4a1a5 2651 if (!c_mark_addressable (array, true))
400fbf9f
JW
2652 return error_mark_node;
2653 }
e6d52559
JW
2654 /* An array that is indexed by a constant value which is not within
2655 the array bounds cannot be stored in a register either; because we
2656 would get a crash in store_bit_field/extract_bit_field when trying
2657 to access a non-existent part of the register. */
2658 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2659 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2660 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2661 {
dffd7eb6 2662 if (!c_mark_addressable (array))
e6d52559
JW
2663 return error_mark_node;
2664 }
400fbf9f 2665
f17a223d
RB
2666 if ((pedantic || warn_c90_c99_compat)
2667 && ! was_vector)
400fbf9f
JW
2668 {
2669 tree foo = array;
2670 while (TREE_CODE (foo) == COMPONENT_REF)
2671 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2672 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2673 pedwarn (loc, OPT_Wpedantic,
fcf73884 2674 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2675 else if (!lvalue_p (foo))
2676 pedwarn_c90 (loc, OPT_Wpedantic,
2677 "ISO C90 forbids subscripting non-lvalue "
2678 "array");
400fbf9f
JW
2679 }
2680
46df2823 2681 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2682 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2683 /* Array ref is const/volatile if the array elements are
c22cacf3 2684 or if the array is. */
400fbf9f
JW
2685 TREE_READONLY (rval)
2686 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2687 | TREE_READONLY (array));
2688 TREE_SIDE_EFFECTS (rval)
2689 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2690 | TREE_SIDE_EFFECTS (array));
2691 TREE_THIS_VOLATILE (rval)
2692 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2693 /* This was added by rms on 16 Nov 91.
2f6e4e97 2694 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2695 in an inline function.
2696 Hope it doesn't break something else. */
2697 | TREE_THIS_VOLATILE (array));
4f2e1536 2698 ret = require_complete_type (loc, rval);
6a3799eb 2699 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2700 if (non_lvalue)
2701 ret = non_lvalue_loc (loc, ret);
6a3799eb 2702 return ret;
400fbf9f 2703 }
a4ab7973
JM
2704 else
2705 {
2706 tree ar = default_conversion (array);
400fbf9f 2707
a4ab7973
JM
2708 if (ar == error_mark_node)
2709 return ar;
400fbf9f 2710
a4ab7973
JM
2711 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2712 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2713
aa7da51a 2714 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
30af3a2b 2715 index, false),
aa7da51a
JJ
2716 RO_ARRAY_INDEXING);
2717 if (non_lvalue)
2718 ret = non_lvalue_loc (loc, ret);
2719 return ret;
a4ab7973 2720 }
400fbf9f
JW
2721}
2722\f
7e585d16 2723/* Build an external reference to identifier ID. FUN indicates
766beb40 2724 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2725 location of the identifier. This sets *TYPE to the type of the
2726 identifier, which is not the same as the type of the returned value
2727 for CONST_DECLs defined as enum constants. If the type of the
2728 identifier is not available, *TYPE is set to NULL. */
7e585d16 2729tree
30af3a2b 2730build_external_ref (location_t loc, tree id, bool fun, tree *type)
7e585d16
ZW
2731{
2732 tree ref;
2733 tree decl = lookup_name (id);
16b34ad6
ZL
2734
2735 /* In Objective-C, an instance variable (ivar) may be preferred to
2736 whatever lookup_name() found. */
2737 decl = objc_lookup_ivar (decl, id);
7e585d16 2738
6866c6e8 2739 *type = NULL;
339a28b9 2740 if (decl && decl != error_mark_node)
6866c6e8
ILT
2741 {
2742 ref = decl;
2743 *type = TREE_TYPE (ref);
2744 }
339a28b9
ZW
2745 else if (fun)
2746 /* Implicit function declaration. */
c2255bc4 2747 ref = implicitly_declare (loc, id);
339a28b9
ZW
2748 else if (decl == error_mark_node)
2749 /* Don't complain about something that's already been
2750 complained about. */
2751 return error_mark_node;
2752 else
2753 {
c2255bc4 2754 undeclared_variable (loc, id);
339a28b9
ZW
2755 return error_mark_node;
2756 }
7e585d16
ZW
2757
2758 if (TREE_TYPE (ref) == error_mark_node)
2759 return error_mark_node;
2760
339a28b9 2761 if (TREE_DEPRECATED (ref))
9b86d6bb 2762 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2763
ad960f56 2764 /* Recursive call does not count as usage. */
b8698a0f 2765 if (ref != current_function_decl)
ad960f56 2766 {
ad960f56
MLI
2767 TREE_USED (ref) = 1;
2768 }
7e585d16 2769
bc4b653b
JM
2770 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2771 {
2772 if (!in_sizeof && !in_typeof)
2773 C_DECL_USED (ref) = 1;
3fa8871b 2774 else if (DECL_INITIAL (ref) == NULL_TREE
bc4b653b
JM
2775 && DECL_EXTERNAL (ref)
2776 && !TREE_PUBLIC (ref))
2777 record_maybe_used_decl (ref);
2778 }
2779
7e585d16
ZW
2780 if (TREE_CODE (ref) == CONST_DECL)
2781 {
6193b8b7 2782 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2783
2784 if (warn_cxx_compat
2785 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2786 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2787 {
2788 warning_at (loc, OPT_Wc___compat,
2789 ("enum constant defined in struct or union "
2790 "is not visible in C++"));
2791 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2792 }
2793
7e585d16
ZW
2794 ref = DECL_INITIAL (ref);
2795 TREE_CONSTANT (ref) = 1;
2796 }
3fa8871b 2797 else if (current_function_decl != NULL_TREE
4b1e44be 2798 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2799 && (VAR_OR_FUNCTION_DECL_P (ref)
2800 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2801 {
2802 tree context = decl_function_context (ref);
2f6e4e97 2803
3fa8871b 2804 if (context != NULL_TREE && context != current_function_decl)
6a29edea
EB
2805 DECL_NONLOCAL (ref) = 1;
2806 }
71113fcd
GK
2807 /* C99 6.7.4p3: An inline definition of a function with external
2808 linkage ... shall not contain a reference to an identifier with
2809 internal linkage. */
3fa8871b 2810 else if (current_function_decl != NULL_TREE
71113fcd
GK
2811 && DECL_DECLARED_INLINE_P (current_function_decl)
2812 && DECL_EXTERNAL (current_function_decl)
2813 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2814 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2815 && ! TREE_PUBLIC (ref)
2816 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2817 record_inline_static (loc, current_function_decl, ref,
2818 csi_internal);
7e585d16
ZW
2819
2820 return ref;
2821}
2822
bc4b653b
JM
2823/* Record details of decls possibly used inside sizeof or typeof. */
2824struct maybe_used_decl
2825{
2826 /* The decl. */
2827 tree decl;
2828 /* The level seen at (in_sizeof + in_typeof). */
2829 int level;
2830 /* The next one at this level or above, or NULL. */
2831 struct maybe_used_decl *next;
2832};
2833
2834static struct maybe_used_decl *maybe_used_decls;
2835
2836/* Record that DECL, an undefined static function reference seen
2837 inside sizeof or typeof, might be used if the operand of sizeof is
2838 a VLA type or the operand of typeof is a variably modified
2839 type. */
2840
4e2fb7de 2841static void
bc4b653b
JM
2842record_maybe_used_decl (tree decl)
2843{
2844 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2845 t->decl = decl;
2846 t->level = in_sizeof + in_typeof;
2847 t->next = maybe_used_decls;
2848 maybe_used_decls = t;
2849}
2850
2851/* Pop the stack of decls possibly used inside sizeof or typeof. If
2852 USED is false, just discard them. If it is true, mark them used
2853 (if no longer inside sizeof or typeof) or move them to the next
2854 level up (if still inside sizeof or typeof). */
2855
2856void
2857pop_maybe_used (bool used)
2858{
2859 struct maybe_used_decl *p = maybe_used_decls;
2860 int cur_level = in_sizeof + in_typeof;
2861 while (p && p->level > cur_level)
2862 {
2863 if (used)
2864 {
2865 if (cur_level == 0)
2866 C_DECL_USED (p->decl) = 1;
2867 else
2868 p->level = cur_level;
2869 }
2870 p = p->next;
2871 }
2872 if (!used || cur_level == 0)
2873 maybe_used_decls = p;
2874}
2875
2876/* Return the result of sizeof applied to EXPR. */
2877
2878struct c_expr
c2255bc4 2879c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2880{
2881 struct c_expr ret;
ad97f4be
JM
2882 if (expr.value == error_mark_node)
2883 {
2884 ret.value = error_mark_node;
2885 ret.original_code = ERROR_MARK;
6866c6e8 2886 ret.original_type = NULL;
ad97f4be
JM
2887 pop_maybe_used (false);
2888 }
2889 else
2890 {
928c19bb 2891 bool expr_const_operands = true;
773ec47f
MP
2892
2893 if (TREE_CODE (expr.value) == PARM_DECL
2894 && C_ARRAY_PARAMETER (expr.value))
2895 {
2896 if (warning_at (loc, OPT_Wsizeof_array_argument,
2897 "%<sizeof%> on array function parameter %qE will "
2898 "return size of %qT", expr.value,
267bbb6f 2899 TREE_TYPE (expr.value)))
773ec47f
MP
2900 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2901 }
928c19bb
JM
2902 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2903 &expr_const_operands);
c2255bc4 2904 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7 2905 c_last_sizeof_arg = expr.value;
40ffd95f 2906 c_last_sizeof_loc = loc;
1a4049e7 2907 ret.original_code = SIZEOF_EXPR;
6866c6e8 2908 ret.original_type = NULL;
928c19bb 2909 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2910 {
2911 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2912 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2913 folded_expr, ret.value);
2914 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2915 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2916 }
928c19bb 2917 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2918 }
bc4b653b
JM
2919 return ret;
2920}
2921
2922/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2923 name passed to sizeof (rather than the type itself). LOC is the
2924 location of the original expression. */
bc4b653b
JM
2925
2926struct c_expr
c2255bc4 2927c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2928{
2929 tree type;
2930 struct c_expr ret;
928c19bb
JM
2931 tree type_expr = NULL_TREE;
2932 bool type_expr_const = true;
2933 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2934 ret.value = c_sizeof (loc, type);
1a4049e7 2935 c_last_sizeof_arg = type;
40ffd95f 2936 c_last_sizeof_loc = loc;
1a4049e7 2937 ret.original_code = SIZEOF_EXPR;
6866c6e8 2938 ret.original_type = NULL;
24070fcb
JM
2939 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2940 && c_vla_type_p (type))
928c19bb 2941 {
24070fcb
JM
2942 /* If the type is a [*] array, it is a VLA but is represented as
2943 having a size of zero. In such a case we must ensure that
2944 the result of sizeof does not get folded to a constant by
2945 c_fully_fold, because if the size is evaluated the result is
2946 not constant and so constraints on zero or negative size
2947 arrays must not be applied when this sizeof call is inside
2948 another array declarator. */
2949 if (!type_expr)
2950 type_expr = integer_zero_node;
928c19bb
JM
2951 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2952 type_expr, ret.value);
2953 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2954 }
16464cc1
VR
2955 pop_maybe_used (type != error_mark_node
2956 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2957 return ret;
2958}
2959
400fbf9f 2960/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2961 The function call is at LOC.
400fbf9f
JW
2962 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2963 TREE_VALUE of each node is a parameter-expression.
2964 FUNCTION's data type may be a function type or a pointer-to-function. */
2965
2966tree
c2255bc4 2967build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2968{
9771b263 2969 vec<tree, va_gc> *v;
bbbbb16a
ILT
2970 tree ret;
2971
9771b263 2972 vec_alloc (v, list_length (params));
bbbbb16a 2973 for (; params; params = TREE_CHAIN (params))
9771b263 2974 v->quick_push (TREE_VALUE (params));
8edbfaa6 2975 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2976 vec_free (v);
bbbbb16a
ILT
2977 return ret;
2978}
2979
ae52741c
MLI
2980/* Give a note about the location of the declaration of DECL. */
2981
c7b70a3c
MP
2982static void
2983inform_declaration (tree decl)
ae52741c 2984{
c7b70a3c 2985 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2986 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2987}
2988
bbbbb16a
ILT
2989/* Build a function call to function FUNCTION with parameters PARAMS.
2990 ORIGTYPES, if not NULL, is a vector of types; each element is
2991 either NULL or the original type of the corresponding element in
2992 PARAMS. The original type may differ from TREE_TYPE of the
2993 parameter for enums. FUNCTION's data type may be a function type
2994 or pointer-to-function. This function changes the elements of
2995 PARAMS. */
2996
2997tree
81e5eca8
MP
2998build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2999 tree function, vec<tree, va_gc> *params,
9771b263 3000 vec<tree, va_gc> *origtypes)
400fbf9f 3001{
3fa8871b 3002 tree fntype, fundecl = NULL_TREE;
4977bab6 3003 tree name = NULL_TREE, result;
c96f4f73 3004 tree tem;
94a0dd7b
SL
3005 int nargs;
3006 tree *argarray;
b8698a0f 3007
400fbf9f 3008
fc76e425 3009 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 3010 STRIP_TYPE_NOPS (function);
400fbf9f
JW
3011
3012 /* Convert anything with function type to a pointer-to-function. */
3013 if (TREE_CODE (function) == FUNCTION_DECL)
3014 {
3015 name = DECL_NAME (function);
0a35513e
AH
3016
3017 if (flag_tm)
3018 tm_malloc_replacement (function);
a5eadacc 3019 fundecl = function;
86951993
AM
3020 /* Atomic functions have type checking/casting already done. They are
3021 often rewritten and don't match the original parameter list. */
3022 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3023 origtypes = NULL;
400fbf9f 3024 }
f2a71bbc 3025 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 3026 function = function_to_pointer_conversion (loc, function);
400fbf9f 3027
6e955430
ZL
3028 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3029 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
3030 if (params && !params->is_empty ())
3031 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 3032
928c19bb
JM
3033 function = c_fully_fold (function, false, NULL);
3034
400fbf9f
JW
3035 fntype = TREE_TYPE (function);
3036
3037 if (TREE_CODE (fntype) == ERROR_MARK)
3038 return error_mark_node;
3039
3040 if (!(TREE_CODE (fntype) == POINTER_TYPE
3041 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3042 {
ae52741c
MLI
3043 if (!flag_diagnostics_show_caret)
3044 error_at (loc,
3045 "called object %qE is not a function or function pointer",
3046 function);
3047 else if (DECL_P (function))
3048 {
3049 error_at (loc,
3050 "called object %qD is not a function or function pointer",
3051 function);
3052 inform_declaration (function);
3053 }
3054 else
3055 error_at (loc,
3056 "called object is not a function or function pointer");
400fbf9f
JW
3057 return error_mark_node;
3058 }
3059
5ce89b2e
JM
3060 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3061 current_function_returns_abnormally = 1;
3062
400fbf9f
JW
3063 /* fntype now gets the type of function pointed to. */
3064 fntype = TREE_TYPE (fntype);
3065
ab4194da
JM
3066 /* Convert the parameters to the types declared in the
3067 function prototype, or apply default promotions. */
3068
81e5eca8
MP
3069 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3070 origtypes, function, fundecl);
ab4194da
JM
3071 if (nargs < 0)
3072 return error_mark_node;
3073
c96f4f73 3074 /* Check that the function is called through a compatible prototype.
fa337f3a 3075 If it is not, warn. */
1043771b 3076 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3077 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3078 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3079 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3080 {
3081 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3082
3083 /* This situation leads to run-time undefined behavior. We can't,
3084 therefore, simply error unless we can prove that all possible
3085 executions of the program must execute the code. */
fa337f3a 3086 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3087
fa337f3a
RB
3088 if (VOID_TYPE_P (return_type)
3089 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3090 pedwarn (loc, 0,
3091 "function with qualified void return type called");
3092 }
c96f4f73 3093
9771b263 3094 argarray = vec_safe_address (params);
bbbbb16a 3095
83322951
RG
3096 /* Check that arguments to builtin functions match the expectations. */
3097 if (fundecl
3098 && DECL_BUILT_IN (fundecl)
3099 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
79ce98bc
MP
3100 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3101 argarray))
83322951 3102 return error_mark_node;
400fbf9f 3103
83322951 3104 /* Check that the arguments to the function are valid. */
4227c9ad 3105 bool warned_p = check_function_arguments (loc, fundecl, fntype,
2f687306 3106 nargs, argarray, &arg_loc);
400fbf9f 3107
928c19bb
JM
3108 if (name != NULL_TREE
3109 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3110 {
928c19bb 3111 if (require_constant_value)
0dba7960
JJ
3112 result
3113 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3114 function, nargs, argarray);
928c19bb 3115 else
db3927fb
AH
3116 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3117 function, nargs, argarray);
928c19bb
JM
3118 if (TREE_CODE (result) == NOP_EXPR
3119 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3120 STRIP_TYPE_NOPS (result);
bf730f15
RS
3121 }
3122 else
db3927fb
AH
3123 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3124 function, nargs, argarray);
0dba7960
JJ
3125 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3126 later. */
3127 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3128 TREE_NO_WARNING (result) = 1;
b0b3afb2 3129
ab4c578f
MP
3130 /* In this improbable scenario, a nested function returns a VM type.
3131 Create a TARGET_EXPR so that the call always has a LHS, much as
3132 what the C++ FE does for functions returning non-PODs. */
3133 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3134 {
3135 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3136 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3137 NULL_TREE, NULL_TREE);
3138 }
3139
71653180 3140 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3141 {
3142 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3143 pedwarn (loc, 0,
3ce62965
JM
3144 "function with qualified void return type called");
3145 return result;
3146 }
4f2e1536 3147 return require_complete_type (loc, result);
400fbf9f 3148}
8edbfaa6
JJ
3149
3150/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3151
3152tree
3153c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3154 tree function, vec<tree, va_gc> *params,
3155 vec<tree, va_gc> *origtypes)
3156{
3157 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3158 STRIP_TYPE_NOPS (function);
3159
3160 /* Convert anything with function type to a pointer-to-function. */
3161 if (TREE_CODE (function) == FUNCTION_DECL)
3162 {
3163 /* Implement type-directed function overloading for builtins.
3164 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3165 handle all the type checking. The result is a complete expression
3166 that implements this function call. */
3167 tree tem = resolve_overloaded_builtin (loc, function, params);
3168 if (tem)
3169 return tem;
3170 }
3171 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3172}
400fbf9f 3173\f
bbbbb16a
ILT
3174/* Convert the argument expressions in the vector VALUES
3175 to the types in the list TYPELIST.
400fbf9f
JW
3176
3177 If TYPELIST is exhausted, or when an element has NULL as its type,
3178 perform the default conversions.
3179
bbbbb16a
ILT
3180 ORIGTYPES is the original types of the expressions in VALUES. This
3181 holds the type of enum values which have been converted to integral
3182 types. It may be NULL.
400fbf9f 3183
03dafa61
JM
3184 FUNCTION is a tree for the called function. It is used only for
3185 error messages, where it is formatted with %qE.
400fbf9f
JW
3186
3187 This is also where warnings about wrong number of args are generated.
3188
81e5eca8
MP
3189 ARG_LOC are locations of function arguments (if any).
3190
94a0dd7b 3191 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3192 than the length of VALUES in some error situations), or -1 on
3193 failure. */
94a0dd7b
SL
3194
3195static int
81e5eca8
MP
3196convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3197 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3198 tree function, tree fundecl)
400fbf9f 3199{
bbbbb16a
ILT
3200 tree typetail, val;
3201 unsigned int parmnum;
06302a02 3202 bool error_args = false;
b5d32c25 3203 const bool type_generic = fundecl
81e5eca8 3204 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3205 bool type_generic_remove_excess_precision = false;
44a845ca 3206 bool type_generic_overflow_p = false;
03dafa61 3207 tree selector;
03dafa61 3208
2ac2f164
JM
3209 /* Change pointer to function to the function itself for
3210 diagnostics. */
03dafa61
JM
3211 if (TREE_CODE (function) == ADDR_EXPR
3212 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3213 function = TREE_OPERAND (function, 0);
03dafa61
JM
3214
3215 /* Handle an ObjC selector specially for diagnostics. */
3216 selector = objc_message_selector ();
400fbf9f 3217
8ce94e44
JM
3218 /* For type-generic built-in functions, determine whether excess
3219 precision should be removed (classification) or not
3220 (comparison). */
3221 if (type_generic
3222 && DECL_BUILT_IN (fundecl)
3223 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3224 {
3225 switch (DECL_FUNCTION_CODE (fundecl))
3226 {
3227 case BUILT_IN_ISFINITE:
3228 case BUILT_IN_ISINF:
3229 case BUILT_IN_ISINF_SIGN:
3230 case BUILT_IN_ISNAN:
3231 case BUILT_IN_ISNORMAL:
3232 case BUILT_IN_FPCLASSIFY:
3233 type_generic_remove_excess_precision = true;
3234 break;
3235
44a845ca
MS
3236 case BUILT_IN_ADD_OVERFLOW_P:
3237 case BUILT_IN_SUB_OVERFLOW_P:
3238 case BUILT_IN_MUL_OVERFLOW_P:
3239 /* The last argument of these type-generic builtins
3240 should not be promoted. */
3241 type_generic_overflow_p = true;
3242 break;
3243
8ce94e44 3244 default:
8ce94e44
JM
3245 break;
3246 }
3247 }
3248
400fbf9f 3249 /* Scan the given expressions and types, producing individual
bbbbb16a 3250 converted arguments. */
400fbf9f 3251
bbbbb16a 3252 for (typetail = typelist, parmnum = 0;
9771b263 3253 values && values->iterate (parmnum, &val);
bbbbb16a 3254 ++parmnum)
400fbf9f 3255 {
b3694847 3256 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3257 tree valtype = TREE_TYPE (val);
03dafa61
JM
3258 tree rname = function;
3259 int argnum = parmnum + 1;
4d3e6fae 3260 const char *invalid_func_diag;
8ce94e44 3261 bool excess_precision = false;
928c19bb 3262 bool npc;
bbbbb16a 3263 tree parmval;
5c1bc275
MP
3264 /* Some __atomic_* builtins have additional hidden argument at
3265 position 0. */
3266 location_t ploc
3267 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3268 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3269 : input_location;
400fbf9f
JW
3270
3271 if (type == void_type_node)
3272 {
19dc6d01 3273 if (selector)
68fca595 3274 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3275 else
68fca595 3276 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3277 inform_declaration (fundecl);
d38f7dce 3278 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3279 }
3280
03dafa61
JM
3281 if (selector && argnum > 2)
3282 {
3283 rname = selector;
3284 argnum -= 2;
3285 }
3286
928c19bb 3287 npc = null_pointer_constant_p (val);
8ce94e44
JM
3288
3289 /* If there is excess precision and a prototype, convert once to
3290 the required type rather than converting via the semantic
3291 type. Likewise without a prototype a float value represented
3292 as long double should be converted once to double. But for
3293 type-generic classification functions excess precision must
3294 be removed here. */
3295 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3296 && (type || !type_generic || !type_generic_remove_excess_precision))
3297 {
3298 val = TREE_OPERAND (val, 0);
3299 excess_precision = true;
3300 }
928c19bb 3301 val = c_fully_fold (val, false, NULL);
ed248cf7 3302 STRIP_TYPE_NOPS (val);
400fbf9f 3303
4f2e1536 3304 val = require_complete_type (ploc, val);
400fbf9f 3305
c65699ef
JM
3306 /* Some floating-point arguments must be promoted to double when
3307 no type is specified by a prototype. This applies to
3308 arguments of type float, and to architecture-specific types
3309 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3310 bool promote_float_arg = false;
3311 if (type == NULL_TREE
3312 && TREE_CODE (valtype) == REAL_TYPE
3313 && (TYPE_PRECISION (valtype)
3314 <= TYPE_PRECISION (double_type_node))
3315 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3316 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3317 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3318 {
3319 /* Promote this argument, unless it has a _FloatN or
3320 _FloatNx type. */
3321 promote_float_arg = true;
3322 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3323 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3324 {
3325 promote_float_arg = false;
3326 break;
3327 }
3328 }
3329
3fa8871b 3330 if (type != NULL_TREE)
400fbf9f
JW
3331 {
3332 /* Formal parm type is specified by a function prototype. */
400fbf9f 3333
20913689 3334 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3335 {
5c1bc275
MP
3336 error_at (ploc, "type of formal parameter %d is incomplete",
3337 parmnum + 1);
400fbf9f
JW
3338 parmval = val;
3339 }
3340 else
3341 {
bbbbb16a
ILT
3342 tree origtype;
3343
d45cf215
RS
3344 /* Optionally warn about conversions that
3345 differ from the default conversions. */
05170031 3346 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3347 {
e3a64162 3348 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3349
aae43c5f 3350 if (INTEGRAL_TYPE_P (type)
8ce94e44 3351 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3352 warning_at (ploc, OPT_Wtraditional_conversion,
3353 "passing argument %d of %qE as integer rather "
3354 "than floating due to prototype",
3355 argnum, rname);
03829ad2 3356 if (INTEGRAL_TYPE_P (type)
8ce94e44 3357 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3358 warning_at (ploc, OPT_Wtraditional_conversion,
3359 "passing argument %d of %qE as integer rather "
3360 "than complex due to prototype",
3361 argnum, rname);
aae43c5f 3362 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3363 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3364 warning_at (ploc, OPT_Wtraditional_conversion,
3365 "passing argument %d of %qE as complex rather "
3366 "than floating due to prototype",
3367 argnum, rname);
400fbf9f 3368 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3369 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3370 warning_at (ploc, OPT_Wtraditional_conversion,
3371 "passing argument %d of %qE as floating rather "
3372 "than integer due to prototype",
3373 argnum, rname);
03829ad2 3374 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3375 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3376 warning_at (ploc, OPT_Wtraditional_conversion,
3377 "passing argument %d of %qE as complex rather "
3378 "than integer due to prototype",
3379 argnum, rname);
aae43c5f 3380 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3381 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3382 warning_at (ploc, OPT_Wtraditional_conversion,
3383 "passing argument %d of %qE as floating rather "
3384 "than complex due to prototype",
3385 argnum, rname);
aae43c5f
RK
3386 /* ??? At some point, messages should be written about
3387 conversions between complex types, but that's too messy
3388 to do now. */
d45cf215 3389 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3390 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3391 {
3392 /* Warn if any argument is passed as `float',
047de90b 3393 since without a prototype it would be `double'. */
9a8ce21f
JG
3394 if (formal_prec == TYPE_PRECISION (float_type_node)
3395 && type != dfloat32_type_node)
5c1bc275
MP
3396 warning_at (ploc, 0,
3397 "passing argument %d of %qE as %<float%> "
3398 "rather than %<double%> due to prototype",
3399 argnum, rname);
9a8ce21f
JG
3400
3401 /* Warn if mismatch between argument and prototype
3402 for decimal float types. Warn of conversions with
3403 binary float types and of precision narrowing due to
3404 prototype. */
8ce94e44 3405 else if (type != valtype
9a8ce21f
JG
3406 && (type == dfloat32_type_node
3407 || type == dfloat64_type_node
c22cacf3 3408 || type == dfloat128_type_node
8ce94e44
JM
3409 || valtype == dfloat32_type_node
3410 || valtype == dfloat64_type_node
3411 || valtype == dfloat128_type_node)
c22cacf3 3412 && (formal_prec
8ce94e44 3413 <= TYPE_PRECISION (valtype)
9a8ce21f 3414 || (type == dfloat128_type_node
8ce94e44 3415 && (valtype
c22cacf3 3416 != dfloat64_type_node
8ce94e44 3417 && (valtype
9a8ce21f
JG
3418 != dfloat32_type_node)))
3419 || (type == dfloat64_type_node
8ce94e44 3420 && (valtype
9a8ce21f 3421 != dfloat32_type_node))))
5c1bc275
MP
3422 warning_at (ploc, 0,
3423 "passing argument %d of %qE as %qT "
3424 "rather than %qT due to prototype",
3425 argnum, rname, type, valtype);
9a8ce21f 3426
d45cf215 3427 }
3ed56f8a
KG
3428 /* Detect integer changing in width or signedness.
3429 These warnings are only activated with
05170031 3430 -Wtraditional-conversion, not with -Wtraditional. */
61ac5ebe
MP
3431 else if (warn_traditional_conversion
3432 && INTEGRAL_TYPE_P (type)
8ce94e44 3433 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3434 {
d45cf215
RS
3435 tree would_have_been = default_conversion (val);
3436 tree type1 = TREE_TYPE (would_have_been);
3437
61ac5ebe
MP
3438 if (val == error_mark_node)
3439 /* VAL could have been of incomplete type. */;
3440 else if (TREE_CODE (type) == ENUMERAL_TYPE
3441 && (TYPE_MAIN_VARIANT (type)
3442 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3443 /* No warning if function asks for enum
3444 and the actual arg is that enum type. */
3445 ;
3446 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3447 warning_at (ploc, OPT_Wtraditional_conversion,
3448 "passing argument %d of %qE "
3449 "with different width due to prototype",
3450 argnum, rname);
8df83eae 3451 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3452 ;
800cd3b9
RS
3453 /* Don't complain if the formal parameter type
3454 is an enum, because we can't tell now whether
3455 the value was an enum--even the same enum. */
3456 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3457 ;
400fbf9f
JW
3458 else if (TREE_CODE (val) == INTEGER_CST
3459 && int_fits_type_p (val, type))
3460 /* Change in signedness doesn't matter
3461 if a constant value is unaffected. */
3462 ;
ce9895ae
RS
3463 /* If the value is extended from a narrower
3464 unsigned type, it doesn't matter whether we
3465 pass it as signed or unsigned; the value
3466 certainly is the same either way. */
8ce94e44
JM
3467 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3468 && TYPE_UNSIGNED (valtype))
ce9895ae 3469 ;
8df83eae 3470 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3471 warning_at (ploc, OPT_Wtraditional_conversion,
3472 "passing argument %d of %qE "
3473 "as unsigned due to prototype",
3474 argnum, rname);
3ed56f8a 3475 else
5c1bc275
MP
3476 warning_at (ploc, OPT_Wtraditional_conversion,
3477 "passing argument %d of %qE "
3478 "as signed due to prototype",
3479 argnum, rname);
400fbf9f
JW
3480 }
3481 }
3482
8ce94e44
JM
3483 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3484 sake of better warnings from convert_and_check. */
3485 if (excess_precision)
3486 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3487 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3488 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3489 val, origtype, ic_argpass,
3490 npc, fundecl, function,
2ac2f164 3491 parmnum + 1);
2f6e4e97 3492
61f71b34 3493 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3494 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3495 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3496 parmval = default_conversion (parmval);
400fbf9f 3497 }
400fbf9f 3498 }
c65699ef 3499 else if (promote_float_arg)
b5d32c25
KG
3500 {
3501 if (type_generic)
bbbbb16a 3502 parmval = val;
b5d32c25 3503 else
0a0b3574
MM
3504 {
3505 /* Convert `float' to `double'. */
3506 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3507 warning_at (ploc, OPT_Wdouble_promotion,
3508 "implicit conversion from %qT to %qT when passing "
3509 "argument to function",
3510 valtype, double_type_node);
0a0b3574
MM
3511 parmval = convert (double_type_node, val);
3512 }
b5d32c25 3513 }
44a845ca
MS
3514 else if ((excess_precision && !type_generic)
3515 || (type_generic_overflow_p && parmnum == 2))
8ce94e44 3516 /* A "double" argument with excess precision being passed
44a845ca
MS
3517 without a prototype or in variable arguments.
3518 The last argument of __builtin_*_overflow_p should not be
3519 promoted. */
bbbbb16a 3520 parmval = convert (valtype, val);
c22cacf3
MS
3521 else if ((invalid_func_diag =
3522 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3523 {
3524 error (invalid_func_diag);
94a0dd7b 3525 return -1;
4d3e6fae 3526 }
1807ffc1
MS
3527 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3528 {
3529 return -1;
3530 }
400fbf9f
JW
3531 else
3532 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3533 parmval = default_conversion (val);
3534
9771b263 3535 (*values)[parmnum] = parmval;
06302a02
JJ
3536 if (parmval == error_mark_node)
3537 error_args = true;
400fbf9f
JW
3538
3539 if (typetail)
3540 typetail = TREE_CHAIN (typetail);
3541 }
3542
9771b263 3543 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3544
3fa8871b 3545 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3789b316 3546 {
68fca595 3547 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3548 inform_declaration (fundecl);
3549 return -1;
3789b316 3550 }
400fbf9f 3551
06302a02 3552 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3553}
3554\f
43f6dfd3
RS
3555/* This is the entry point used by the parser to build unary operators
3556 in the input. CODE, a tree_code, specifies the unary operator, and
3557 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3558 CONVERT_EXPR for code.
3559
3560 LOC is the location to use for the tree generated.
3561*/
43f6dfd3
RS
3562
3563struct c_expr
c2255bc4 3564parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3565{
3566 struct c_expr result;
3567
100d537d 3568 result.original_code = code;
6866c6e8
ILT
3569 result.original_type = NULL;
3570
1807ffc1
MS
3571 if (reject_gcc_builtin (arg.value))
3572 {
3573 result.value = error_mark_node;
3574 }
3575 else
3576 {
e51fbec3 3577 result.value = build_unary_op (loc, code, arg.value, false);
1807ffc1 3578
e9892350 3579 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3cd211af 3580 overflow_warning (loc, result.value, arg.value);
1807ffc1 3581 }
59c0753d 3582
ebedc9a3
DM
3583 /* We are typically called when parsing a prefix token at LOC acting on
3584 ARG. Reflect this by updating the source range of the result to
3585 start at LOC and end at the end of ARG. */
3586 set_c_expr_source_range (&result,
3587 loc, arg.get_finish ());
3588
43f6dfd3
RS
3589 return result;
3590}
3591
a9342885
MP
3592/* Returns true if TYPE is a character type, *not* including wchar_t. */
3593
3594static bool
3595char_type_p (tree type)
3596{
3597 return (type == char_type_node
3598 || type == unsigned_char_type_node
3599 || type == signed_char_type_node
3600 || type == char16_type_node
3601 || type == char32_type_node);
3602}
3603
43f6dfd3
RS
3604/* This is the entry point used by the parser to build binary operators
3605 in the input. CODE, a tree_code, specifies the binary operator, and
3606 ARG1 and ARG2 are the operands. In addition to constructing the
3607 expression, we check for operands that were written with other binary
ba47d38d
AH
3608 operators in a way that is likely to confuse the user.
3609
3610 LOCATION is the location of the binary operator. */
edc7c4ec 3611
487a92fe 3612struct c_expr
ba47d38d
AH
3613parser_build_binary_op (location_t location, enum tree_code code,
3614 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3615{
487a92fe 3616 struct c_expr result;
400fbf9f 3617
487a92fe
JM
3618 enum tree_code code1 = arg1.original_code;
3619 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3620 tree type1 = (arg1.original_type
3621 ? arg1.original_type
3622 : TREE_TYPE (arg1.value));
3623 tree type2 = (arg2.original_type
3624 ? arg2.original_type
3625 : TREE_TYPE (arg2.value));
400fbf9f 3626
ba47d38d 3627 result.value = build_binary_op (location, code,
30af3a2b 3628 arg1.value, arg2.value, true);
487a92fe 3629 result.original_code = code;
6866c6e8 3630 result.original_type = NULL;
58bf601b 3631
487a92fe 3632 if (TREE_CODE (result.value) == ERROR_MARK)
8a40fef3
DM
3633 {
3634 set_c_expr_source_range (&result,
3635 arg1.get_start (),
3636 arg2.get_finish ());
3637 return result;
3638 }
400fbf9f 3639
ba47d38d
AH
3640 if (location != UNKNOWN_LOCATION)
3641 protected_set_expr_location (result.value, location);
3642
ebedc9a3
DM
3643 set_c_expr_source_range (&result,
3644 arg1.get_start (),
3645 arg2.get_finish ());
3646
400fbf9f 3647 /* Check for cases such as x+y<<z which users are likely
487a92fe 3648 to misinterpret. */
400fbf9f 3649 if (warn_parentheses)
393e8e8b
MP
3650 warn_about_parentheses (location, code, code1, arg1.value, code2,
3651 arg2.value);
001af587 3652
ca409efd 3653 if (warn_logical_op)
393e8e8b 3654 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3655 code1, arg1.value, code2, arg2.value);
63a08740 3656
05b28fd6 3657 if (warn_tautological_compare)
433068cc
MP
3658 {
3659 tree lhs = arg1.value;
3660 tree rhs = arg2.value;
3661 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3662 {
3663 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3664 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3665 lhs = NULL_TREE;
3666 else
3667 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3668 }
3669 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3670 {
3671 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3672 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3673 rhs = NULL_TREE;
3674 else
3675 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3676 }
3677 if (lhs != NULL_TREE && rhs != NULL_TREE)
3678 warn_tautological_cmp (location, code, lhs, rhs);
3679 }
05b28fd6 3680
742938c9 3681 if (warn_logical_not_paren
7ccb1a11 3682 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3683 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3684 && code2 != TRUTH_NOT_EXPR
3685 /* Avoid warning for !!x == y. */
3686 && (TREE_CODE (arg1.value) != NE_EXPR
3687 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3688 {
3689 /* Avoid warning for !b == y where b has _Bool type. */
3690 tree t = integer_zero_node;
3691 if (TREE_CODE (arg1.value) == EQ_EXPR
3692 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3693 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3694 {
3695 t = TREE_OPERAND (arg1.value, 0);
3696 do
3697 {
3698 if (TREE_TYPE (t) != integer_type_node)
3699 break;
3700 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3701 t = C_MAYBE_CONST_EXPR_EXPR (t);
3702 else if (CONVERT_EXPR_P (t))
3703 t = TREE_OPERAND (t, 0);
3704 else
3705 break;
3706 }
3707 while (1);
3708 }
3709 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
ebef225f 3710 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
01177669 3711 }
742938c9 3712
e994a705
RS
3713 /* Warn about comparisons against string literals, with the exception
3714 of testing for equality or inequality of a string literal with NULL. */
3715 if (code == EQ_EXPR || code == NE_EXPR)
3716 {
66756373
JJ
3717 if ((code1 == STRING_CST
3718 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3719 || (code2 == STRING_CST
3720 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
c2255bc4
AH
3721 warning_at (location, OPT_Waddress,
3722 "comparison with string literal results in unspecified behavior");
a9342885
MP
3723 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3724 if (POINTER_TYPE_P (type1)
3725 && null_pointer_constant_p (arg2.value)
3726 && char_type_p (type2)
3727 && warning_at (location, OPT_Wpointer_compare,
3728 "comparison between pointer and zero character "
3729 "constant"))
3730 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3731 else if (POINTER_TYPE_P (type2)
3732 && null_pointer_constant_p (arg1.value)
3733 && char_type_p (type1)
3734 && warning_at (location, OPT_Wpointer_compare,
3735 "comparison between pointer and zero character "
3736 "constant"))
3737 inform (arg2.get_start (), "did you mean to dereference the pointer?");
e994a705
RS
3738 }
3739 else if (TREE_CODE_CLASS (code) == tcc_comparison
3740 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3741 warning_at (location, OPT_Waddress,
3742 "comparison with string literal results in unspecified behavior");
e994a705 3743
b8698a0f
L
3744 if (TREE_OVERFLOW_P (result.value)
3745 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3746 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3747 overflow_warning (location, result.value);
400fbf9f 3748
6866c6e8
ILT
3749 /* Warn about comparisons of different enum types. */
3750 if (warn_enum_compare
3751 && TREE_CODE_CLASS (code) == tcc_comparison
3752 && TREE_CODE (type1) == ENUMERAL_TYPE
3753 && TREE_CODE (type2) == ENUMERAL_TYPE
3754 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3755 warning_at (location, OPT_Wenum_compare,
3756 "comparison between %qT and %qT",
3757 type1, type2);
3758
400fbf9f
JW
3759 return result;
3760}
3e4093b6 3761\f
3e4093b6 3762/* Return a tree for the difference of pointers OP0 and OP1.
1af4ebf5 3763 The resulting tree has type ptrdiff_t. */
293c9fdd 3764
3e4093b6 3765static tree
db3927fb 3766pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3767{
3e4093b6 3768 tree restype = ptrdiff_type_node;
36c5e70a 3769 tree result, inttype;
400fbf9f 3770
36c5e70a
BE
3771 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3772 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3773 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3774 tree orig_op1 = op1;
400fbf9f 3775
36c5e70a
BE
3776 /* If the operands point into different address spaces, we need to
3777 explicitly convert them to pointers into the common address space
3778 before we can subtract the numerical address values. */
3779 if (as0 != as1)
3780 {
3781 addr_space_t as_common;
3782 tree common_type;
3783
3784 /* Determine the common superset address space. This is guaranteed
3785 to exist because the caller verified that comp_target_types
3786 returned non-zero. */
3787 if (!addr_space_superset (as0, as1, &as_common))
3788 gcc_unreachable ();
3789
3790 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3791 op0 = convert (common_type, op0);
3792 op1 = convert (common_type, op1);
3793 }
3794
1af4ebf5 3795 /* Determine integer type result of the subtraction. This will usually
36c5e70a
BE
3796 be the same as the result type (ptrdiff_t), but may need to be a wider
3797 type if pointers for the address space are wider than ptrdiff_t. */
3798 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3799 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3800 else
3801 inttype = restype;
3802
fcf73884 3803 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3804 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3805 "pointer of type %<void *%> used in subtraction");
3806 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3807 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3808 "pointer to a function used in subtraction");
400fbf9f 3809
1af4ebf5
MG
3810 /* First do the subtraction, then build the divide operator
3811 and only convert at the very end.
3812 Do not do default conversions in case restype is a short type. */
3813
3814 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3815 pointers. If some platform cannot provide that, or has a larger
3816 ptrdiff_type to support differences larger than half the address
3817 space, cast the pointers to some larger integer type and do the
3818 computations in that type. */
3819 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3820 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3821 convert (inttype, op1), false);
3822 else
3823 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
400fbf9f 3824
3e4093b6
RS
3825 /* This generates an error if op1 is pointer to incomplete type. */
3826 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3827 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3828
3e4093b6 3829 op1 = c_size_in_bytes (target_type);
400fbf9f 3830
f04dda30
MP
3831 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3832 error_at (loc, "arithmetic on pointer to an empty aggregate");
3833
3e4093b6 3834 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3835 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3836 op0, convert (inttype, op1));
3837
3838 /* Convert to final result type if necessary. */
3839 return convert (restype, result);
3e4093b6
RS
3840}
3841\f
2fe0a208
MP
3842/* Expand atomic compound assignments into an appropriate sequence as
3843 specified by the C11 standard section 6.5.16.2.
3844
267bac10
JM
3845 _Atomic T1 E1
3846 T2 E2
3847 E1 op= E2
3848
3849 This sequence is used for all types for which these operations are
3850 supported.
3851
3852 In addition, built-in versions of the 'fe' prefixed routines may
3853 need to be invoked for floating point (real, complex or vector) when
3854 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3855
3856 T1 newval;
3857 T1 old;
3858 T1 *addr
3859 T2 val
3860 fenv_t fenv
3861
3862 addr = &E1;
3863 val = (E2);
3864 __atomic_load (addr, &old, SEQ_CST);
3865 feholdexcept (&fenv);
3866loop:
3867 newval = old op val;
3868 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3869 SEQ_CST))
3870 goto done;
3871 feclearexcept (FE_ALL_EXCEPT);
3872 goto loop:
3873done:
3874 feupdateenv (&fenv);
3875
2fe0a208
MP
3876 The compiler will issue the __atomic_fetch_* built-in when possible,
3877 otherwise it will generate the generic form of the atomic operations.
3878 This requires temp(s) and has their address taken. The atomic processing
3879 is smart enough to figure out when the size of an object can utilize
3880 a lock-free version, and convert the built-in call to the appropriate
3881 lock-free routine. The optimizers will then dispose of any temps that
3882 are no longer required, and lock-free implementations are utilized as
3883 long as there is target support for the required size.
267bac10
JM
3884
3885 If the operator is NOP_EXPR, then this is a simple assignment, and
3886 an __atomic_store is issued to perform the assignment rather than
2fe0a208 3887 the above loop. */
267bac10
JM
3888
3889/* Build an atomic assignment at LOC, expanding into the proper
3890 sequence to store LHS MODIFYCODE= RHS. Return a value representing
2fe0a208 3891 the result of the operation, unless RETURN_OLD_P, in which case
267bac10
JM
3892 return the old value of LHS (this is only for postincrement and
3893 postdecrement). */
2fe0a208 3894
267bac10
JM
3895static tree
3896build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3897 tree rhs, bool return_old_p)
3898{
3899 tree fndecl, func_call;
3900 vec<tree, va_gc> *params;
3901 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3902 tree old, old_addr;
3903 tree compound_stmt;
3904 tree stmt, goto_stmt;
3905 tree loop_label, loop_decl, done_label, done_decl;
3906
3907 tree lhs_type = TREE_TYPE (lhs);
e51fbec3 3908 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
267bac10 3909 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
e035be33
JM
3910 tree rhs_semantic_type = TREE_TYPE (rhs);
3911 tree nonatomic_rhs_semantic_type;
3912 tree rhs_type;
267bac10
JM
3913
3914 gcc_assert (TYPE_ATOMIC (lhs_type));
3915
3916 if (return_old_p)
3917 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3918
3919 /* Allocate enough vector items for a compare_exchange. */
3920 vec_alloc (params, 6);
3921
3922 /* Create a compound statement to hold the sequence of statements
3923 with a loop. */
3924 compound_stmt = c_begin_compound_stmt (false);
3925
e035be33
JM
3926 /* Remove any excess precision (which is only present here in the
3927 case of compound assignments). */
3928 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3929 {
3930 gcc_assert (modifycode != NOP_EXPR);
3931 rhs = TREE_OPERAND (rhs, 0);
3932 }
3933 rhs_type = TREE_TYPE (rhs);
3934
267bac10
JM
3935 /* Fold the RHS if it hasn't already been folded. */
3936 if (modifycode != NOP_EXPR)
3937 rhs = c_fully_fold (rhs, false, NULL);
3938
3939 /* Remove the qualifiers for the rest of the expressions and create
3940 the VAL temp variable to hold the RHS. */
3941 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3942 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
e035be33
JM
3943 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3944 TYPE_UNQUALIFIED);
5c4abbb8 3945 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3946 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3947 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3948 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3949 NULL_TREE);
267bac10
JM
3950 SET_EXPR_LOCATION (rhs, loc);
3951 add_stmt (rhs);
3952
3953 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3954 an atomic_store. */
3955 if (modifycode == NOP_EXPR)
3956 {
3957 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
e51fbec3 3958 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
267bac10
JM
3959 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3960 params->quick_push (lhs_addr);
3961 params->quick_push (rhs);
3962 params->quick_push (seq_cst);
8edbfaa6 3963 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3964 add_stmt (func_call);
3965
3966 /* Finish the compound statement. */
3967 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3968
3969 /* VAL is the value which was stored, return a COMPOUND_STMT of
3970 the statement and that value. */
3971 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3972 }
3973
2fe0a208
MP
3974 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3975 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3976 isn't applicable for such builtins. ??? Do we want to handle enums? */
3977 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3978 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3979 {
3980 built_in_function fncode;
3981 switch (modifycode)
3982 {
3983 case PLUS_EXPR:
3984 case POINTER_PLUS_EXPR:
3985 fncode = (return_old_p
3986 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3987 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3988 break;
3989 case MINUS_EXPR:
3990 fncode = (return_old_p
3991 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3992 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3993 break;
3994 case BIT_AND_EXPR:
3995 fncode = (return_old_p
3996 ? BUILT_IN_ATOMIC_FETCH_AND_N
3997 : BUILT_IN_ATOMIC_AND_FETCH_N);
3998 break;
3999 case BIT_IOR_EXPR:
4000 fncode = (return_old_p
4001 ? BUILT_IN_ATOMIC_FETCH_OR_N
4002 : BUILT_IN_ATOMIC_OR_FETCH_N);
4003 break;
4004 case BIT_XOR_EXPR:
4005 fncode = (return_old_p
4006 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4007 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4008 break;
4009 default:
4010 goto cas_loop;
4011 }
4012
4013 /* We can only use "_1" through "_16" variants of the atomic fetch
4014 built-ins. */
4015 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4016 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4017 goto cas_loop;
4018
4019 /* If this is a pointer type, we need to multiply by the size of
4020 the pointer target type. */
4021 if (POINTER_TYPE_P (lhs_type))
4022 {
4023 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4024 /* ??? This would introduce -Wdiscarded-qualifiers
4025 warning: __atomic_fetch_* expect volatile void *
4026 type as the first argument. (Assignments between
4027 atomic and non-atomic objects are OK.) */
4028 || TYPE_RESTRICT (lhs_type))
4029 goto cas_loop;
4030 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4031 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4032 convert (ptrdiff_type_node, rhs),
4033 convert (ptrdiff_type_node, sz));
4034 }
4035
4036 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4037 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4038 fndecl = builtin_decl_explicit (fncode);
4039 params->quick_push (lhs_addr);
4040 params->quick_push (rhs);
4041 params->quick_push (seq_cst);
4042 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4043
4044 newval = create_tmp_var_raw (nonatomic_lhs_type);
4045 TREE_ADDRESSABLE (newval) = 1;
4046 TREE_NO_WARNING (newval) = 1;
4047 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4048 NULL_TREE, NULL_TREE);
4049 SET_EXPR_LOCATION (rhs, loc);
4050 add_stmt (rhs);
4051
4052 /* Finish the compound statement. */
4053 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4054
4055 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4056 the statement and that value. */
4057 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4058 }
4059
4060cas_loop:
267bac10 4061 /* Create the variables and labels required for the op= form. */
5c4abbb8 4062 old = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4063 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
cc28fc7f
MP
4064 TREE_ADDRESSABLE (old) = 1;
4065 TREE_NO_WARNING (old) = 1;
267bac10 4066
5c4abbb8 4067 newval = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4068 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
267bac10 4069 TREE_ADDRESSABLE (newval) = 1;
2994fb91 4070 TREE_NO_WARNING (newval) = 1;
267bac10
JM
4071
4072 loop_decl = create_artificial_label (loc);
4073 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4074
4075 done_decl = create_artificial_label (loc);
4076 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4077
4078 /* __atomic_load (addr, &old, SEQ_CST). */
4079 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4080 params->quick_push (lhs_addr);
4081 params->quick_push (old_addr);
4082 params->quick_push (seq_cst);
8edbfaa6 4083 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
4084 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4085 NULL_TREE);
4086 add_stmt (old);
267bac10
JM
4087 params->truncate (0);
4088
4089 /* Create the expressions for floating-point environment
4090 manipulation, if required. */
4091 bool need_fenv = (flag_trapping_math
4092 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4093 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4094 if (need_fenv)
4095 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4096
4097 if (hold_call)
4098 add_stmt (hold_call);
4099
4100 /* loop: */
4101 add_stmt (loop_label);
4102
4103 /* newval = old + val; */
e035be33
JM
4104 if (rhs_type != rhs_semantic_type)
4105 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
30af3a2b 4106 rhs = build_binary_op (loc, modifycode, old, val, true);
e035be33
JM
4107 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4108 {
4109 tree eptype = TREE_TYPE (rhs);
4110 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4111 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4112 }
4113 else
4114 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
4115 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4116 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
4117 NULL_TREE, 0);
4118 if (rhs != error_mark_node)
4119 {
5c4abbb8
MP
4120 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4121 NULL_TREE);
267bac10
JM
4122 SET_EXPR_LOCATION (rhs, loc);
4123 add_stmt (rhs);
4124 }
4125
4126 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4127 goto done; */
4128 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4129 params->quick_push (lhs_addr);
4130 params->quick_push (old_addr);
4131 params->quick_push (newval_addr);
4132 params->quick_push (integer_zero_node);
4133 params->quick_push (seq_cst);
4134 params->quick_push (seq_cst);
8edbfaa6 4135 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
4136
4137 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4138 SET_EXPR_LOCATION (goto_stmt, loc);
4139
4140 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4141 SET_EXPR_LOCATION (stmt, loc);
4142 add_stmt (stmt);
5c4abbb8 4143
267bac10
JM
4144 if (clear_call)
4145 add_stmt (clear_call);
4146
4147 /* goto loop; */
4148 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4149 SET_EXPR_LOCATION (goto_stmt, loc);
4150 add_stmt (goto_stmt);
5c4abbb8 4151
267bac10
JM
4152 /* done: */
4153 add_stmt (done_label);
4154
4155 if (update_call)
4156 add_stmt (update_call);
4157
4158 /* Finish the compound statement. */
4159 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4160
4161 /* NEWVAL is the value that was successfully stored, return a
4162 COMPOUND_EXPR of the statement and the appropriate value. */
4163 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4164 return_old_p ? old : newval);
4165}
4166
3e4093b6
RS
4167/* Construct and perhaps optimize a tree representation
4168 for a unary operation. CODE, a tree_code, specifies the operation
4169 and XARG is the operand.
e51fbec3
MP
4170 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4171 promotions (such as from short to int).
4172 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4173 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4174 to pointers in C99.
c9f9eb5d
AH
4175
4176 LOCATION is the location of the operator. */
400fbf9f 4177
3e4093b6 4178tree
4c13ba17 4179build_unary_op (location_t location, enum tree_code code, tree xarg,
e51fbec3 4180 bool noconvert)
3e4093b6
RS
4181{
4182 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4183 tree arg = xarg;
3fa8871b 4184 tree argtype = NULL_TREE;
808d6eaa 4185 enum tree_code typecode;
3e4093b6 4186 tree val;
c9f9eb5d 4187 tree ret = error_mark_node;
8ce94e44 4188 tree eptype = NULL_TREE;
4de67c26 4189 const char *invalid_op_diag;
928c19bb
JM
4190 bool int_operands;
4191
4192 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
4193 if (int_operands)
4194 arg = remove_c_maybe_const_expr (arg);
400fbf9f 4195
808d6eaa 4196 if (code != ADDR_EXPR)
4f2e1536 4197 arg = require_complete_type (location, arg);
808d6eaa
JM
4198
4199 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
4200 if (typecode == ERROR_MARK)
4201 return error_mark_node;
4202 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4203 typecode = INTEGER_TYPE;
6c36d76b 4204
4de67c26
JM
4205 if ((invalid_op_diag
4206 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4207 {
c9f9eb5d 4208 error_at (location, invalid_op_diag);
4de67c26
JM
4209 return error_mark_node;
4210 }
4211
8ce94e44
JM
4212 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4213 {
4214 eptype = TREE_TYPE (arg);
4215 arg = TREE_OPERAND (arg, 0);
4216 }
4217
3e4093b6
RS
4218 switch (code)
4219 {
4220 case CONVERT_EXPR:
4221 /* This is used for unary plus, because a CONVERT_EXPR
4222 is enough to prevent anybody from looking inside for
4223 associativity, but won't generate any code. */
4224 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4225 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 4226 || typecode == VECTOR_TYPE))
400fbf9f 4227 {
c9f9eb5d 4228 error_at (location, "wrong type argument to unary plus");
3e4093b6 4229 return error_mark_node;
400fbf9f 4230 }
3e4093b6
RS
4231 else if (!noconvert)
4232 arg = default_conversion (arg);
db3927fb 4233 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
4234 break;
4235
3e4093b6
RS
4236 case NEGATE_EXPR:
4237 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4238 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
4239 || typecode == VECTOR_TYPE))
4240 {
c9f9eb5d 4241 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
4242 return error_mark_node;
4243 }
4244 else if (!noconvert)
4245 arg = default_conversion (arg);
400fbf9f
JW
4246 break;
4247
3e4093b6 4248 case BIT_NOT_EXPR:
462643f0
AP
4249 /* ~ works on integer types and non float vectors. */
4250 if (typecode == INTEGER_TYPE
4251 || (typecode == VECTOR_TYPE
4252 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 4253 {
9a2300e9
MP
4254 tree e = arg;
4255
4256 /* Warn if the expression has boolean value. */
4257 while (TREE_CODE (e) == COMPOUND_EXPR)
4258 e = TREE_OPERAND (e, 1);
4259
4260 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4261 || truth_value_p (TREE_CODE (e)))
4262 && warning_at (location, OPT_Wbool_operation,
4263 "%<~%> on a boolean expression"))
4264 {
4265 gcc_rich_location richloc (location);
4266 richloc.add_fixit_insert_before (location, "!");
64a5912c 4267 inform (&richloc, "did you mean to use logical not?");
9a2300e9 4268 }
3e4093b6
RS
4269 if (!noconvert)
4270 arg = default_conversion (arg);
03d5b1f5 4271 }
3e4093b6 4272 else if (typecode == COMPLEX_TYPE)
400fbf9f 4273 {
3e4093b6 4274 code = CONJ_EXPR;
c1771a20 4275 pedwarn (location, OPT_Wpedantic,
fcf73884 4276 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4277 if (!noconvert)
4278 arg = default_conversion (arg);
4279 }
4280 else
4281 {
c9f9eb5d 4282 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4283 return error_mark_node;
400fbf9f
JW
4284 }
4285 break;
4286
3e4093b6 4287 case ABS_EXPR:
11017cc7 4288 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4289 {
c9f9eb5d 4290 error_at (location, "wrong type argument to abs");
3e4093b6 4291 return error_mark_node;
400fbf9f 4292 }
3e4093b6
RS
4293 else if (!noconvert)
4294 arg = default_conversion (arg);
400fbf9f
JW
4295 break;
4296
3e4093b6
RS
4297 case CONJ_EXPR:
4298 /* Conjugating a real value is a no-op, but allow it anyway. */
4299 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4300 || typecode == COMPLEX_TYPE))
400fbf9f 4301 {
c9f9eb5d 4302 error_at (location, "wrong type argument to conjugation");
3e4093b6 4303 return error_mark_node;
400fbf9f 4304 }
3e4093b6
RS
4305 else if (!noconvert)
4306 arg = default_conversion (arg);
400fbf9f
JW
4307 break;
4308
3e4093b6 4309 case TRUTH_NOT_EXPR:
ab22c1fa 4310 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4311 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4312 && typecode != COMPLEX_TYPE)
400fbf9f 4313 {
c9f9eb5d
AH
4314 error_at (location,
4315 "wrong type argument to unary exclamation mark");
3e4093b6 4316 return error_mark_node;
400fbf9f 4317 }
a27d595d
JM
4318 if (int_operands)
4319 {
4320 arg = c_objc_common_truthvalue_conversion (location, xarg);
4321 arg = remove_c_maybe_const_expr (arg);
4322 }
4323 else
4324 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4325 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4326 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4327 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4328 location = EXPR_LOCATION (ret);
c9f9eb5d 4329 goto return_build_unary_op;
3e4093b6 4330
3e4093b6 4331 case REALPART_EXPR:
3e4093b6 4332 case IMAGPART_EXPR:
fb52b50a
NF
4333 ret = build_real_imag_expr (location, code, arg);
4334 if (ret == error_mark_node)
4335 return error_mark_node;
8ce94e44
JM
4336 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4337 eptype = TREE_TYPE (eptype);
c9f9eb5d 4338 goto return_build_unary_op;
3e4093b6
RS
4339
4340 case PREINCREMENT_EXPR:
4341 case POSTINCREMENT_EXPR:
4342 case PREDECREMENT_EXPR:
4343 case POSTDECREMENT_EXPR:
3e4093b6 4344
928c19bb
JM
4345 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4346 {
4347 tree inner = build_unary_op (location, code,
4c13ba17
MP
4348 C_MAYBE_CONST_EXPR_EXPR (arg),
4349 noconvert);
928c19bb
JM
4350 if (inner == error_mark_node)
4351 return error_mark_node;
4352 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4353 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4354 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4355 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4356 goto return_build_unary_op;
4357 }
4358
925e8657
NP
4359 /* Complain about anything that is not a true lvalue. In
4360 Objective-C, skip this check for property_refs. */
f90e8e2e 4361 if (!objc_is_property_ref (arg)
7bd11157
TT
4362 && !lvalue_or_else (location,
4363 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4364 || code == POSTINCREMENT_EXPR)
4365 ? lv_increment
4366 : lv_decrement)))
928c19bb
JM
4367 return error_mark_node;
4368
09639a83
ILT
4369 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4370 {
4371 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4372 warning_at (location, OPT_Wc___compat,
4373 "increment of enumeration value is invalid in C++");
4374 else
4375 warning_at (location, OPT_Wc___compat,
4376 "decrement of enumeration value is invalid in C++");
4377 }
4378
9a2300e9
MP
4379 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4380 {
4381 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4382 warning_at (location, OPT_Wbool_operation,
4383 "increment of a boolean expression");
4384 else
4385 warning_at (location, OPT_Wbool_operation,
4386 "decrement of a boolean expression");
4387 }
4388
928c19bb 4389 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
f9c59f7e 4390 arg = c_fully_fold (arg, false, NULL, true);
928c19bb 4391
267bac10
JM
4392 bool atomic_op;
4393 atomic_op = really_atomic_lvalue (arg);
4394
3e4093b6
RS
4395 /* Increment or decrement the real part of the value,
4396 and don't change the imaginary part. */
4397 if (typecode == COMPLEX_TYPE)
400fbf9f 4398 {
3e4093b6
RS
4399 tree real, imag;
4400
c1771a20 4401 pedwarn (location, OPT_Wpedantic,
509c9d60 4402 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4403
267bac10
JM
4404 if (!atomic_op)
4405 {
4406 arg = stabilize_reference (arg);
e51fbec3
MP
4407 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4408 true);
4409 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4410 true);
4411 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
267bac10
JM
4412 if (real == error_mark_node || imag == error_mark_node)
4413 return error_mark_node;
4414 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4415 real, imag);
4416 goto return_build_unary_op;
4417 }
400fbf9f 4418 }
3e4093b6
RS
4419
4420 /* Report invalid types. */
4421
ab22c1fa 4422 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4423 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4424 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4425 {
3e4093b6 4426 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4427 error_at (location, "wrong type argument to increment");
c22cacf3 4428 else
c9f9eb5d 4429 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4430
4431 return error_mark_node;
400fbf9f 4432 }
400fbf9f 4433
3e4093b6
RS
4434 {
4435 tree inc;
400fbf9f 4436
3e4093b6
RS
4437 argtype = TREE_TYPE (arg);
4438
4439 /* Compute the increment. */
4440
4441 if (typecode == POINTER_TYPE)
4442 {
a0e24419 4443 /* If pointer target is an incomplete type,
3e4093b6 4444 we just cannot know how to do the arithmetic. */
b70cef5d 4445 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4446 {
4447 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4448 error_at (location,
a0e24419
MP
4449 "increment of pointer to an incomplete type %qT",
4450 TREE_TYPE (argtype));
3e4093b6 4451 else
c9f9eb5d 4452 error_at (location,
a0e24419
MP
4453 "decrement of pointer to an incomplete type %qT",
4454 TREE_TYPE (argtype));
3e4093b6 4455 }
b70cef5d
JJ
4456 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4457 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4458 {
3e4093b6 4459 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4460 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4461 "wrong type argument to increment");
3e4093b6 4462 else
44d90fe1 4463 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4464 "wrong type argument to decrement");
3e4093b6
RS
4465 }
4466
b70cef5d 4467 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4468 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4469 }
b70cef5d 4470 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4471 {
4472 /* For signed fract types, we invert ++ to -- or
4473 -- to ++, and change inc from 1 to -1, because
4474 it is not possible to represent 1 in signed fract constants.
4475 For unsigned fract types, the result always overflows and
4476 we get an undefined (original) or the maximum value. */
4477 if (code == PREINCREMENT_EXPR)
4478 code = PREDECREMENT_EXPR;
4479 else if (code == PREDECREMENT_EXPR)
4480 code = PREINCREMENT_EXPR;
4481 else if (code == POSTINCREMENT_EXPR)
4482 code = POSTDECREMENT_EXPR;
4483 else /* code == POSTDECREMENT_EXPR */
4484 code = POSTINCREMENT_EXPR;
4485
4486 inc = integer_minus_one_node;
4487 inc = convert (argtype, inc);
4488 }
3e4093b6 4489 else
5be014d5 4490 {
241b71bb
TV
4491 inc = VECTOR_TYPE_P (argtype)
4492 ? build_one_cst (argtype)
4493 : integer_one_node;
5be014d5
AP
4494 inc = convert (argtype, inc);
4495 }
3e4093b6 4496
925e8657
NP
4497 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4498 need to ask Objective-C to build the increment or decrement
4499 expression for it. */
4500 if (objc_is_property_ref (arg))
f90e8e2e 4501 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4502 arg, inc);
4503
3e4093b6 4504 /* Report a read-only lvalue. */
f37acdf9 4505 if (TYPE_READONLY (argtype))
953ff289 4506 {
c02065fc 4507 readonly_error (location, arg,
953ff289
DN
4508 ((code == PREINCREMENT_EXPR
4509 || code == POSTINCREMENT_EXPR)
4510 ? lv_increment : lv_decrement));
4511 return error_mark_node;
4512 }
f37acdf9
JM
4513 else if (TREE_READONLY (arg))
4514 readonly_warning (arg,
4515 ((code == PREINCREMENT_EXPR
4516 || code == POSTINCREMENT_EXPR)
4517 ? lv_increment : lv_decrement));
3e4093b6 4518
267bac10
JM
4519 /* If the argument is atomic, use the special code sequences for
4520 atomic compound assignment. */
4521 if (atomic_op)
4522 {
4523 arg = stabilize_reference (arg);
4524 ret = build_atomic_assign (location, arg,
4525 ((code == PREINCREMENT_EXPR
4526 || code == POSTINCREMENT_EXPR)
4527 ? PLUS_EXPR
4528 : MINUS_EXPR),
4529 (FRACT_MODE_P (TYPE_MODE (argtype))
4530 ? inc
4531 : integer_one_node),
4532 (code == POSTINCREMENT_EXPR
4533 || code == POSTDECREMENT_EXPR));
4534 goto return_build_unary_op;
4535 }
4536
3e4093b6
RS
4537 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4538 val = boolean_increment (code, arg);
4539 else
53fb4de3 4540 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4541 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4542 if (TREE_CODE (val) != code)
6de9cd9a 4543 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4544 ret = val;
4545 goto return_build_unary_op;
3e4093b6
RS
4546 }
4547
4548 case ADDR_EXPR:
4549 /* Note that this operation never does default_conversion. */
4550
2b4b7036
JM
4551 /* The operand of unary '&' must be an lvalue (which excludes
4552 expressions of type void), or, in C99, the result of a [] or
4553 unary '*' operator. */
4554 if (VOID_TYPE_P (TREE_TYPE (arg))
4555 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4556 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4557 pedwarn (location, 0, "taking address of expression of type %<void%>");
4558
3e4093b6 4559 /* Let &* cancel out to simplify resulting code. */
22d03525 4560 if (INDIRECT_REF_P (arg))
400fbf9f 4561 {
3e4093b6
RS
4562 /* Don't let this be an lvalue. */
4563 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4564 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4565 ret = TREE_OPERAND (arg, 0);
4566 goto return_build_unary_op;
400fbf9f 4567 }
1eb8759b 4568
3e4093b6
RS
4569 /* Anything not already handled and not a true memory reference
4570 or a non-lvalue array is an error. */
4c13ba17 4571 if (typecode != FUNCTION_TYPE && !noconvert
ee45a32d 4572 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4573 return error_mark_node;
b6a10c9f 4574
928c19bb
JM
4575 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4576 folding later. */
4577 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4578 {
4579 tree inner = build_unary_op (location, code,
4c13ba17
MP
4580 C_MAYBE_CONST_EXPR_EXPR (arg),
4581 noconvert);
928c19bb
JM
4582 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4583 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4584 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4585 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4586 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4587 goto return_build_unary_op;
4588 }
4589
3e4093b6
RS
4590 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4591 argtype = TREE_TYPE (arg);
400fbf9f 4592
3e4093b6 4593 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4594 to which the address will point. This is only needed
f2c1da78 4595 for function types. */
6615c446 4596 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4597 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4598 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4599 {
4600 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4601 int quals = orig_quals;
4602
4603 if (TREE_READONLY (arg))
4604 quals |= TYPE_QUAL_CONST;
4605 if (TREE_THIS_VOLATILE (arg))
4606 quals |= TYPE_QUAL_VOLATILE;
4607
f2c1da78
JM
4608 argtype = c_build_qualified_type (argtype, quals);
4609 }
400fbf9f 4610
ee45a32d
EB
4611 switch (TREE_CODE (arg))
4612 {
4613 case COMPONENT_REF:
4614 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4615 {
949505a9
MP
4616 error_at (location, "cannot take address of bit-field %qD",
4617 TREE_OPERAND (arg, 1));
ee45a32d
EB
4618 return error_mark_node;
4619 }
4620
191816a3 4621 /* fall through */
ee45a32d
EB
4622
4623 case ARRAY_REF:
4624 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4625 {
4626 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4627 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4628 {
949505a9
MP
4629 error_at (location, "cannot take address of scalar with "
4630 "reverse storage order");
ee45a32d
EB
4631 return error_mark_node;
4632 }
4633
4634 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4635 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
949505a9
MP
4636 warning_at (location, OPT_Wscalar_storage_order,
4637 "address of array with reverse scalar storage "
4638 "order requested");
ee45a32d
EB
4639 }
4640
4641 default:
4642 break;
4643 }
4644
3e4093b6
RS
4645 if (!c_mark_addressable (arg))
4646 return error_mark_node;
400fbf9f 4647
abb54d14
JM
4648 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4649 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4650
5cc200fc 4651 argtype = build_pointer_type (argtype);
5e55f99d
RH
4652
4653 /* ??? Cope with user tricks that amount to offsetof. Delete this
4654 when we have proper support for integer constant expressions. */
4655 val = get_base_address (arg);
22d03525 4656 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4657 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4658 {
cf9e9959 4659 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4660 goto return_build_unary_op;
3aa2ddb8 4661 }
5e55f99d 4662
5cc200fc 4663 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4664
c9f9eb5d
AH
4665 ret = val;
4666 goto return_build_unary_op;
400fbf9f 4667
3e4093b6 4668 default:
1344f9a3 4669 gcc_unreachable ();
3e4093b6 4670 }
400fbf9f 4671
3fa8871b 4672 if (argtype == NULL_TREE)
3e4093b6 4673 argtype = TREE_TYPE (arg);
928c19bb
JM
4674 if (TREE_CODE (arg) == INTEGER_CST)
4675 ret = (require_constant_value
db3927fb
AH
4676 ? fold_build1_initializer_loc (location, code, argtype, arg)
4677 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4678 else
4679 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4680 return_build_unary_op:
4681 gcc_assert (ret != error_mark_node);
928c19bb
JM
4682 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4683 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4684 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4685 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4686 ret = note_integer_operands (ret);
8ce94e44
JM
4687 if (eptype)
4688 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4689 protected_set_expr_location (ret, location);
4690 return ret;
3e4093b6 4691}
400fbf9f 4692
3e4093b6
RS
4693/* Return nonzero if REF is an lvalue valid for this language.
4694 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4695 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4696
1e4ae551 4697bool
58f9752a 4698lvalue_p (const_tree ref)
3e4093b6 4699{
58f9752a 4700 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4701
3e4093b6
RS
4702 switch (code)
4703 {
4704 case REALPART_EXPR:
4705 case IMAGPART_EXPR:
4706 case COMPONENT_REF:
4707 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4708
928c19bb
JM
4709 case C_MAYBE_CONST_EXPR:
4710 return lvalue_p (TREE_OPERAND (ref, 1));
4711
3e4093b6
RS
4712 case COMPOUND_LITERAL_EXPR:
4713 case STRING_CST:
5b73d2ab 4714 return true;
400fbf9f 4715
3e4093b6
RS
4716 case INDIRECT_REF:
4717 case ARRAY_REF:
4718 case VAR_DECL:
4719 case PARM_DECL:
4720 case RESULT_DECL:
4721 case ERROR_MARK:
4722 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4723 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4724
3e4093b6 4725 case BIND_EXPR:
3e4093b6 4726 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4727
3e4093b6 4728 default:
5b73d2ab 4729 return false;
3e4093b6
RS
4730 }
4731}
400fbf9f 4732\f
f37acdf9
JM
4733/* Give a warning for storing in something that is read-only in GCC
4734 terms but not const in ISO C terms. */
4735
4736static void
4737readonly_warning (tree arg, enum lvalue_use use)
4738{
4739 switch (use)
4740 {
4741 case lv_assign:
4742 warning (0, "assignment of read-only location %qE", arg);
4743 break;
4744 case lv_increment:
4745 warning (0, "increment of read-only location %qE", arg);
4746 break;
4747 case lv_decrement:
4748 warning (0, "decrement of read-only location %qE", arg);
4749 break;
4750 default:
4751 gcc_unreachable ();
4752 }
4753 return;
4754}
4755
37dc0d8d
JM
4756
4757/* Return nonzero if REF is an lvalue valid for this language;
4758 otherwise, print an error message and return zero. USE says
7bd11157
TT
4759 how the lvalue is being used and so selects the error message.
4760 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4761
4762static int
7bd11157 4763lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4764{
4765 int win = lvalue_p (ref);
4766
4767 if (!win)
7bd11157 4768 lvalue_error (loc, use);
37dc0d8d
JM
4769
4770 return win;
4771}
3e4093b6
RS
4772\f
4773/* Mark EXP saying that we need to be able to take the
4774 address of it; it should not be allocated in a register.
a9e4a1a5
JJ
4775 Returns true if successful. ARRAY_REF_P is true if this
4776 is for ARRAY_REF construction - in that case we don't want
4777 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4778 it is fine to use ARRAY_REFs for vector subscripts on vector
4779 register variables. */
54c93c30 4780
3e4093b6 4781bool
a9e4a1a5 4782c_mark_addressable (tree exp, bool array_ref_p)
400fbf9f 4783{
3e4093b6 4784 tree x = exp;
95602da1 4785
3e4093b6
RS
4786 while (1)
4787 switch (TREE_CODE (x))
4788 {
a9e4a1a5
JJ
4789 case VIEW_CONVERT_EXPR:
4790 if (array_ref_p
4791 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4792 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4793 return true;
4794 /* FALLTHRU */
3e4093b6 4795 case COMPONENT_REF:
3e4093b6
RS
4796 case ADDR_EXPR:
4797 case ARRAY_REF:
4798 case REALPART_EXPR:
4799 case IMAGPART_EXPR:
4800 x = TREE_OPERAND (x, 0);
4801 break;
95602da1 4802
3e4093b6
RS
4803 case COMPOUND_LITERAL_EXPR:
4804 case CONSTRUCTOR:
4805 TREE_ADDRESSABLE (x) = 1;
4806 return true;
95602da1 4807
3e4093b6
RS
4808 case VAR_DECL:
4809 case CONST_DECL:
4810 case PARM_DECL:
4811 case RESULT_DECL:
5baeaac0 4812 if (C_DECL_REGISTER (x)
3e4093b6
RS
4813 && DECL_NONLOCAL (x))
4814 {
62f9079a 4815 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4816 {
0039fa55
AN
4817 error
4818 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4819 return false;
4820 }
509c9d60 4821 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4822 }
5baeaac0 4823 else if (C_DECL_REGISTER (x))
3e4093b6 4824 {
62f9079a 4825 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4826 error ("address of global register variable %qD requested", x);
4827 else
4828 error ("address of register variable %qD requested", x);
4829 return false;
3e4093b6 4830 }
400fbf9f 4831
191816a3 4832 /* FALLTHRU */
3e4093b6
RS
4833 case FUNCTION_DECL:
4834 TREE_ADDRESSABLE (x) = 1;
191816a3 4835 /* FALLTHRU */
3e4093b6
RS
4836 default:
4837 return true;
4838 }
4839}
4840\f
2d2e923f
MLI
4841/* Convert EXPR to TYPE, warning about conversion problems with
4842 constants. SEMANTIC_TYPE is the type this conversion would use
4843 without excess precision. If SEMANTIC_TYPE is NULL, this function
4844 is equivalent to convert_and_check. This function is a wrapper that
4845 handles conversions that may be different than
4846 the usual ones because of excess precision. */
4847
4848static tree
68fca595
MP
4849ep_convert_and_check (location_t loc, tree type, tree expr,
4850 tree semantic_type)
2d2e923f
MLI
4851{
4852 if (TREE_TYPE (expr) == type)
4853 return expr;
4854
bb75facd
JM
4855 /* For C11, integer conversions may have results with excess
4856 precision. */
4857 if (flag_isoc11 || !semantic_type)
68fca595 4858 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4859
4860 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4861 && TREE_TYPE (expr) != semantic_type)
4862 {
4863 /* For integers, we need to check the real conversion, not
4864 the conversion to the excess precision type. */
68fca595 4865 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4866 }
4867 /* Result type is the excess precision type, which should be
4868 large enough, so do not check. */
4869 return convert (type, expr);
4870}
4871
928c19bb
JM
4872/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4873 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4874 if folded to an integer constant then the unselected half may
4875 contain arbitrary operations not normally permitted in constant
c2255bc4 4876 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4877
4878tree
744aa42f 4879build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
a32c8316
MP
4880 tree op1, tree op1_original_type, location_t op1_loc,
4881 tree op2, tree op2_original_type, location_t op2_loc)
400fbf9f 4882{
3e4093b6
RS
4883 tree type1;
4884 tree type2;
4885 enum tree_code code1;
4886 enum tree_code code2;
4887 tree result_type = NULL;
2d2e923f 4888 tree semantic_result_type = NULL;
3e4093b6 4889 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4890 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4891 bool ifexp_int_operands;
928c19bb 4892 tree ret;
400fbf9f 4893
4d84fe7c
JM
4894 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4895 if (op1_int_operands)
4896 op1 = remove_c_maybe_const_expr (op1);
4897 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4898 if (op2_int_operands)
4899 op2 = remove_c_maybe_const_expr (op2);
4900 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4901 if (ifexp_int_operands)
4902 ifexp = remove_c_maybe_const_expr (ifexp);
4903
3e4093b6
RS
4904 /* Promote both alternatives. */
4905
4906 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4907 op1 = default_conversion (op1);
4908 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4909 op2 = default_conversion (op2);
4910
4911 if (TREE_CODE (ifexp) == ERROR_MARK
4912 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4913 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4914 return error_mark_node;
400fbf9f 4915
3e4093b6
RS
4916 type1 = TREE_TYPE (op1);
4917 code1 = TREE_CODE (type1);
4918 type2 = TREE_TYPE (op2);
4919 code2 = TREE_CODE (type2);
4920
1807ffc1
MS
4921 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4922 return error_mark_node;
4923
4924 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4925 return error_mark_node;
4926
b1adf557
JM
4927 /* C90 does not permit non-lvalue arrays in conditional expressions.
4928 In C99 they will be pointers by now. */
4929 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4930 {
744aa42f 4931 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4932 return error_mark_node;
4933 }
4934
8ce94e44
JM
4935 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4936 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4937 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4938 || code1 == COMPLEX_TYPE)
4939 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4940 || code2 == COMPLEX_TYPE))
4941 {
2d2e923f 4942 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4943 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4944 {
4945 op1 = TREE_OPERAND (op1, 0);
4946 type1 = TREE_TYPE (op1);
4947 gcc_assert (TREE_CODE (type1) == code1);
4948 }
4949 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4950 {
4951 op2 = TREE_OPERAND (op2, 0);
4952 type2 = TREE_TYPE (op2);
4953 gcc_assert (TREE_CODE (type2) == code2);
4954 }
4955 }
4956
d130ae11
ILT
4957 if (warn_cxx_compat)
4958 {
4959 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4960 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4961
4962 if (TREE_CODE (t1) == ENUMERAL_TYPE
4963 && TREE_CODE (t2) == ENUMERAL_TYPE
4964 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4965 warning_at (colon_loc, OPT_Wc___compat,
4966 ("different enum types in conditional is "
4967 "invalid in C++: %qT vs %qT"),
4968 t1, t2);
4969 }
4970
3e4093b6
RS
4971 /* Quickly detect the usual case where op1 and op2 have the same type
4972 after promotion. */
4973 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4974 {
3e4093b6
RS
4975 if (type1 == type2)
4976 result_type = type1;
4977 else
4978 result_type = TYPE_MAIN_VARIANT (type1);
4979 }
4980 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4981 || code1 == COMPLEX_TYPE)
4982 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4983 || code2 == COMPLEX_TYPE))
3e4093b6 4984 {
bb75facd
JM
4985 /* In C11, a conditional expression between a floating-point
4986 type and an integer type should convert the integer type to
4987 the evaluation format of the floating-point type, with
4988 possible excess precision. */
4989 tree eptype1 = type1;
4990 tree eptype2 = type2;
4991 if (flag_isoc11)
4992 {
4993 tree eptype;
4994 if (ANY_INTEGRAL_TYPE_P (type1)
4995 && (eptype = excess_precision_type (type2)) != NULL_TREE)
4996 {
4997 eptype2 = eptype;
4998 if (!semantic_result_type)
4999 semantic_result_type = c_common_type (type1, type2);
5000 }
5001 else if (ANY_INTEGRAL_TYPE_P (type2)
5002 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5003 {
5004 eptype1 = eptype;
5005 if (!semantic_result_type)
5006 semantic_result_type = c_common_type (type1, type2);
5007 }
5008 }
5009 result_type = c_common_type (eptype1, eptype2);
5a578671
JM
5010 if (result_type == error_mark_node)
5011 return error_mark_node;
0a0b3574
MM
5012 do_warn_double_promotion (result_type, type1, type2,
5013 "implicit conversion from %qT to %qT to "
5014 "match other result of conditional",
5015 colon_loc);
400fbf9f 5016
3e4093b6
RS
5017 /* If -Wsign-compare, warn here if type1 and type2 have
5018 different signedness. We'll promote the signed to unsigned
5019 and later code won't know it used to be different.
5020 Do this check on the original types, so that explicit casts
5021 will be considered, but default promotions won't. */
7d882b83 5022 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 5023 {
8df83eae
RK
5024 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5025 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 5026
3e4093b6
RS
5027 if (unsigned_op1 ^ unsigned_op2)
5028 {
6ac01510
ILT
5029 bool ovf;
5030
3e4093b6
RS
5031 /* Do not warn if the result type is signed, since the
5032 signed type will only be chosen if it can represent
5033 all the values of the unsigned type. */
3f75a254 5034 if (!TYPE_UNSIGNED (result_type))
3e4093b6 5035 /* OK */;
3e4093b6 5036 else
928c19bb
JM
5037 {
5038 bool op1_maybe_const = true;
5039 bool op2_maybe_const = true;
5040
5041 /* Do not warn if the signed quantity is an
5042 unsuffixed integer literal (or some static
5043 constant expression involving such literals) and
5044 it is non-negative. This warning requires the
5045 operands to be folded for best results, so do
5046 that folding in this case even without
5047 warn_sign_compare to avoid warning options
5048 possibly affecting code generation. */
f5178456
RS
5049 c_inhibit_evaluation_warnings
5050 += (ifexp == truthvalue_false_node);
928c19bb
JM
5051 op1 = c_fully_fold (op1, require_constant_value,
5052 &op1_maybe_const);
f5178456
RS
5053 c_inhibit_evaluation_warnings
5054 -= (ifexp == truthvalue_false_node);
5055
5056 c_inhibit_evaluation_warnings
5057 += (ifexp == truthvalue_true_node);
928c19bb
JM
5058 op2 = c_fully_fold (op2, require_constant_value,
5059 &op2_maybe_const);
f5178456
RS
5060 c_inhibit_evaluation_warnings
5061 -= (ifexp == truthvalue_true_node);
928c19bb
JM
5062
5063 if (warn_sign_compare)
5064 {
5065 if ((unsigned_op2
5066 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5067 || (unsigned_op1
5068 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5069 /* OK */;
a32c8316
MP
5070 else if (unsigned_op2)
5071 warning_at (op1_loc, OPT_Wsign_compare,
5072 "operand of ?: changes signedness from "
5073 "%qT to %qT due to unsignedness of other "
5074 "operand", TREE_TYPE (orig_op1),
5075 TREE_TYPE (orig_op2));
928c19bb 5076 else
a32c8316
MP
5077 warning_at (op2_loc, OPT_Wsign_compare,
5078 "operand of ?: changes signedness from "
5079 "%qT to %qT due to unsignedness of other "
5080 "operand", TREE_TYPE (orig_op2),
5081 TREE_TYPE (orig_op1));
928c19bb
JM
5082 }
5083 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 5084 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 5085 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 5086 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 5087 }
3e4093b6
RS
5088 }
5089 }
5090 }
5091 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5092 {
fcf73884 5093 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 5094 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 5095 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
5096 result_type = void_type_node;
5097 }
5098 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5099 {
36c5e70a
BE
5100 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5101 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5102 addr_space_t as_common;
5103
744aa42f 5104 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 5105 result_type = common_pointer_type (type1, type2);
6aa3c60d 5106 else if (null_pointer_constant_p (orig_op1))
36c5e70a 5107 result_type = type2;
6aa3c60d 5108 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
5109 result_type = type1;
5110 else if (!addr_space_superset (as1, as2, &as_common))
5111 {
5112 error_at (colon_loc, "pointers to disjoint address spaces "
5113 "used in conditional expression");
5114 return error_mark_node;
5115 }
267bac10
JM
5116 else if (VOID_TYPE_P (TREE_TYPE (type1))
5117 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 5118 {
768952be
MU
5119 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5120 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5121 & ~TYPE_QUALS (TREE_TYPE (type1))))
5122 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5123 "pointer to array loses qualifier "
5124 "in conditional expression");
5125
fcf73884 5126 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 5127 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5128 "ISO C forbids conditional expr between "
bda67431 5129 "%<void *%> and function pointer");
3e4093b6
RS
5130 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5131 TREE_TYPE (type2)));
34a80643 5132 }
267bac10
JM
5133 else if (VOID_TYPE_P (TREE_TYPE (type2))
5134 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 5135 {
768952be
MU
5136 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5137 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5138 & ~TYPE_QUALS (TREE_TYPE (type2))))
5139 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5140 "pointer to array loses qualifier "
5141 "in conditional expression");
5142
fcf73884 5143 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 5144 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5145 "ISO C forbids conditional expr between "
bda67431 5146 "%<void *%> and function pointer");
3e4093b6
RS
5147 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5148 TREE_TYPE (type1)));
1c2a9b35 5149 }
b581b85b
NP
5150 /* Objective-C pointer comparisons are a bit more lenient. */
5151 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5152 result_type = objc_common_type (type1, type2);
34a80643 5153 else
ab87f8c8 5154 {
36c5e70a
BE
5155 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5156
b581b85b
NP
5157 pedwarn (colon_loc, 0,
5158 "pointer type mismatch in conditional expression");
36c5e70a
BE
5159 result_type = build_pointer_type
5160 (build_qualified_type (void_type_node, qual));
ab87f8c8 5161 }
3e4093b6
RS
5162 }
5163 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5164 {
6aa3c60d 5165 if (!null_pointer_constant_p (orig_op2))
744aa42f 5166 pedwarn (colon_loc, 0,
509c9d60 5167 "pointer/integer type mismatch in conditional expression");
3e4093b6 5168 else
ab87f8c8 5169 {
3e4093b6 5170 op2 = null_pointer_node;
ab87f8c8 5171 }
3e4093b6
RS
5172 result_type = type1;
5173 }
5174 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5175 {
6aa3c60d 5176 if (!null_pointer_constant_p (orig_op1))
744aa42f 5177 pedwarn (colon_loc, 0,
509c9d60 5178 "pointer/integer type mismatch in conditional expression");
3e4093b6 5179 else
ab87f8c8 5180 {
3e4093b6 5181 op1 = null_pointer_node;
ab87f8c8 5182 }
3e4093b6
RS
5183 result_type = type2;
5184 }
1c2a9b35 5185
3e4093b6
RS
5186 if (!result_type)
5187 {
5188 if (flag_cond_mismatch)
5189 result_type = void_type_node;
5190 else
400fbf9f 5191 {
c2255bc4 5192 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 5193 return error_mark_node;
400fbf9f 5194 }
3e4093b6 5195 }
400fbf9f 5196
3e4093b6
RS
5197 /* Merge const and volatile flags of the incoming types. */
5198 result_type
5199 = build_type_variant (result_type,
afbd0665
AS
5200 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5201 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 5202
68fca595
MP
5203 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5204 semantic_result_type);
5205 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5206 semantic_result_type);
b6a10c9f 5207
928c19bb
JM
5208 if (ifexp_bcp && ifexp == truthvalue_true_node)
5209 {
5210 op2_int_operands = true;
5211 op1 = c_fully_fold (op1, require_constant_value, NULL);
5212 }
5213 if (ifexp_bcp && ifexp == truthvalue_false_node)
5214 {
5215 op1_int_operands = true;
5216 op2 = c_fully_fold (op2, require_constant_value, NULL);
5217 }
4d84fe7c 5218 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
5219 && op1_int_operands
5220 && op2_int_operands);
5221 if (int_operands)
5222 {
5223 int_const = ((ifexp == truthvalue_true_node
5224 && TREE_CODE (orig_op1) == INTEGER_CST
5225 && !TREE_OVERFLOW (orig_op1))
5226 || (ifexp == truthvalue_false_node
5227 && TREE_CODE (orig_op2) == INTEGER_CST
5228 && !TREE_OVERFLOW (orig_op2)));
5229 }
9f47c7e5
IE
5230
5231 /* Need to convert condition operand into a vector mask. */
5232 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5233 {
5234 tree vectype = TREE_TYPE (ifexp);
5235 tree elem_type = TREE_TYPE (vectype);
5236 tree zero = build_int_cst (elem_type, 0);
5237 tree zero_vec = build_vector_from_val (vectype, zero);
5238 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5239 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5240 }
5241
928c19bb 5242 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5243 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5244 else
5245 {
01c7ccbb
JM
5246 if (int_operands)
5247 {
f34f1c87
MP
5248 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5249 nested inside of the expression. */
5250 op1 = c_fully_fold (op1, false, NULL);
5251 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5252 }
928c19bb
JM
5253 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5254 if (int_operands)
5255 ret = note_integer_operands (ret);
5256 }
2d2e923f
MLI
5257 if (semantic_result_type)
5258 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5259
c2255bc4 5260 protected_set_expr_location (ret, colon_loc);
2ebd93e1
MP
5261
5262 /* If the OP1 and OP2 are the same and don't have side-effects,
5263 warn here, because the COND_EXPR will be turned into OP1. */
5264 if (warn_duplicated_branches
5265 && TREE_CODE (ret) == COND_EXPR
5266 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5267 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5268 "this condition has identical branches");
5269
928c19bb 5270 return ret;
3e4093b6
RS
5271}
5272\f
487a92fe 5273/* Return a compound expression that performs two expressions and
c2255bc4
AH
5274 returns the value of the second of them.
5275
5276 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5277
3e4093b6 5278tree
c2255bc4 5279build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5280{
4d84fe7c 5281 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5282 tree eptype = NULL_TREE;
928c19bb
JM
5283 tree ret;
5284
4d84fe7c
JM
5285 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5286 if (expr1_int_operands)
5287 expr1 = remove_c_maybe_const_expr (expr1);
5288 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5289 if (expr2_int_operands)
5290 expr2 = remove_c_maybe_const_expr (expr2);
5291
8ce94e44
JM
5292 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5293 expr1 = TREE_OPERAND (expr1, 0);
5294 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5295 {
5296 eptype = TREE_TYPE (expr2);
5297 expr2 = TREE_OPERAND (expr2, 0);
5298 }
5299
3f75a254 5300 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5301 {
5302 /* The left-hand operand of a comma expression is like an expression
c5409249 5303 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5304 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5305 if (warn_unused_value)
47aecf47 5306 {
e14a6540 5307 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5308 && CONVERT_EXPR_P (expr1))
47aecf47 5309 ; /* (void) a, b */
e14a6540
JM
5310 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5311 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5312 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5313 ; /* (void) a, (void) b, c */
5314 else
b8698a0f 5315 warning_at (loc, OPT_Wunused_value,
c2255bc4 5316 "left-hand operand of comma expression has no effect");
47aecf47 5317 }
3e4093b6 5318 }
789eadcd
MP
5319 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5320 && warn_unused_value)
5321 {
5322 tree r = expr1;
5323 location_t cloc = loc;
5324 while (TREE_CODE (r) == COMPOUND_EXPR)
5325 {
5326 if (EXPR_HAS_LOCATION (r))
5327 cloc = EXPR_LOCATION (r);
5328 r = TREE_OPERAND (r, 1);
5329 }
5330 if (!TREE_SIDE_EFFECTS (r)
5331 && !VOID_TYPE_P (TREE_TYPE (r))
5332 && !CONVERT_EXPR_P (r))
5333 warning_at (cloc, OPT_Wunused_value,
5334 "right-hand operand of comma expression has no effect");
5335 }
400fbf9f 5336
3e4093b6
RS
5337 /* With -Wunused, we should also warn if the left-hand operand does have
5338 side-effects, but computes a value which is not used. For example, in
5339 `foo() + bar(), baz()' the result of the `+' operator is not used,
5340 so we should issue a warning. */
5341 else if (warn_unused_value)
c2255bc4 5342 warn_if_unused_value (expr1, loc);
400fbf9f 5343
e63d6886
AP
5344 if (expr2 == error_mark_node)
5345 return error_mark_node;
5346
928c19bb
JM
5347 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5348
5349 if (flag_isoc99
4d84fe7c
JM
5350 && expr1_int_operands
5351 && expr2_int_operands)
928c19bb
JM
5352 ret = note_integer_operands (ret);
5353
8ce94e44
JM
5354 if (eptype)
5355 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5356
c2255bc4 5357 protected_set_expr_location (ret, loc);
928c19bb 5358 return ret;
3e4093b6 5359}
400fbf9f 5360
67165eb3
ILT
5361/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5362 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5363 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5364 of the cast. -Wcast-qual appeared on the command line. Named
5365 address space qualifiers are not handled here, because they result
5366 in different warnings. */
67165eb3
ILT
5367
5368static void
2ee3cb35 5369handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5370{
5371 tree in_type = type;
5372 tree in_otype = otype;
5373 int added = 0;
5374 int discarded = 0;
5375 bool is_const;
5376
5377 /* Check that the qualifiers on IN_TYPE are a superset of the
5378 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5379 nodes is uninteresting and we stop as soon as we hit a
5380 non-POINTER_TYPE node on either type. */
5381 do
5382 {
5383 in_otype = TREE_TYPE (in_otype);
5384 in_type = TREE_TYPE (in_type);
5385
5386 /* GNU C allows cv-qualified function types. 'const' means the
5387 function is very pure, 'volatile' means it can't return. We
5388 need to warn when such qualifiers are added, not when they're
5389 taken away. */
5390 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5391 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5392 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5393 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5394 else
36c5e70a
BE
5395 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5396 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5397 }
5398 while (TREE_CODE (in_type) == POINTER_TYPE
5399 && TREE_CODE (in_otype) == POINTER_TYPE);
5400
5401 if (added)
2ee3cb35
MLI
5402 warning_at (loc, OPT_Wcast_qual,
5403 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5404
5405 if (discarded)
5406 /* There are qualifiers present in IN_OTYPE that are not present
5407 in IN_TYPE. */
2ee3cb35 5408 warning_at (loc, OPT_Wcast_qual,
7485aeea 5409 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5410 discarded);
67165eb3
ILT
5411
5412 if (added || discarded)
5413 return;
5414
5415 /* A cast from **T to const **T is unsafe, because it can cause a
5416 const value to be changed with no additional warning. We only
5417 issue this warning if T is the same on both sides, and we only
5418 issue the warning if there are the same number of pointers on
5419 both sides, as otherwise the cast is clearly unsafe anyhow. A
5420 cast is unsafe when a qualifier is added at one level and const
5421 is not present at all outer levels.
5422
5423 To issue this warning, we check at each level whether the cast
5424 adds new qualifiers not already seen. We don't need to special
5425 case function types, as they won't have the same
5426 TYPE_MAIN_VARIANT. */
5427
5428 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5429 return;
5430 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5431 return;
5432
5433 in_type = type;
5434 in_otype = otype;
5435 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5436 do
5437 {
5438 in_type = TREE_TYPE (in_type);
5439 in_otype = TREE_TYPE (in_otype);
5440 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5441 && !is_const)
5442 {
2ee3cb35
MLI
5443 warning_at (loc, OPT_Wcast_qual,
5444 "to be safe all intermediate pointers in cast from "
5445 "%qT to %qT must be %<const%> qualified",
5446 otype, type);
67165eb3
ILT
5447 break;
5448 }
5449 if (is_const)
5450 is_const = TYPE_READONLY (in_type);
5451 }
5452 while (TREE_CODE (in_type) == POINTER_TYPE);
5453}
5454
b8698a0f 5455/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5456 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5457
3e4093b6 5458tree
c2255bc4 5459build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5460{
8ce94e44
JM
5461 tree value;
5462
5463 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5464 expr = TREE_OPERAND (expr, 0);
5465
5466 value = expr;
400fbf9f 5467
3e4093b6
RS
5468 if (type == error_mark_node || expr == error_mark_node)
5469 return error_mark_node;
400fbf9f 5470
3e4093b6
RS
5471 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5472 only in <protocol> qualifications. But when constructing cast expressions,
5473 the protocols do matter and must be kept around. */
700686fa
ZL
5474 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5475 return build1 (NOP_EXPR, type, expr);
5476
5477 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5478
3e4093b6
RS
5479 if (TREE_CODE (type) == ARRAY_TYPE)
5480 {
c2255bc4 5481 error_at (loc, "cast specifies array type");
3e4093b6
RS
5482 return error_mark_node;
5483 }
400fbf9f 5484
3e4093b6
RS
5485 if (TREE_CODE (type) == FUNCTION_TYPE)
5486 {
c2255bc4 5487 error_at (loc, "cast specifies function type");
3e4093b6
RS
5488 return error_mark_node;
5489 }
400fbf9f 5490
808d6eaa
JM
5491 if (!VOID_TYPE_P (type))
5492 {
4f2e1536 5493 value = require_complete_type (loc, value);
808d6eaa
JM
5494 if (value == error_mark_node)
5495 return error_mark_node;
5496 }
5497
3e4093b6
RS
5498 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5499 {
3e636daf 5500 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5501 pedwarn (loc, OPT_Wpedantic,
fcf73884 5502 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5503
5504 /* Convert to remove any qualifiers from VALUE's type. */
5505 value = convert (type, value);
3e4093b6
RS
5506 }
5507 else if (TREE_CODE (type) == UNION_TYPE)
5508 {
5509 tree field;
400fbf9f 5510
910ad8de 5511 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5512 if (TREE_TYPE (field) != error_mark_node
5513 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5514 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5515 break;
5516
5517 if (field)
400fbf9f 5518 {
3e4093b6 5519 tree t;
e616f54d 5520 bool maybe_const = true;
3e4093b6 5521
c1771a20 5522 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5523 t = c_fully_fold (value, false, &maybe_const);
5524 t = build_constructor_single (type, field, t);
5525 if (!maybe_const)
5526 t = c_wrap_maybe_const (t, true);
5527 t = digest_init (loc, type, t,
bbbbb16a 5528 NULL_TREE, false, true, 0);
3e4093b6
RS
5529 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5530 return t;
400fbf9f 5531 }
c2255bc4 5532 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5533 return error_mark_node;
5534 }
5535 else
5536 {
5537 tree otype, ovalue;
400fbf9f 5538
3e4093b6 5539 if (type == void_type_node)
c2255bc4
AH
5540 {
5541 tree t = build1 (CONVERT_EXPR, type, value);
5542 SET_EXPR_LOCATION (t, loc);
5543 return t;
5544 }
400fbf9f 5545
3e4093b6 5546 otype = TREE_TYPE (value);
400fbf9f 5547
3e4093b6 5548 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5549 if (warn_cast_qual
5550 && TREE_CODE (type) == POINTER_TYPE
5551 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5552 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5553
36c5e70a
BE
5554 /* Warn about conversions between pointers to disjoint
5555 address spaces. */
5556 if (TREE_CODE (type) == POINTER_TYPE
5557 && TREE_CODE (otype) == POINTER_TYPE
5558 && !null_pointer_constant_p (value))
5559 {
5560 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5561 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5562 addr_space_t as_common;
5563
5564 if (!addr_space_superset (as_to, as_from, &as_common))
5565 {
5566 if (ADDR_SPACE_GENERIC_P (as_from))
5567 warning_at (loc, 0, "cast to %s address space pointer "
5568 "from disjoint generic address space pointer",
5569 c_addr_space_name (as_to));
5570
5571 else if (ADDR_SPACE_GENERIC_P (as_to))
5572 warning_at (loc, 0, "cast to generic address space pointer "
5573 "from disjoint %s address space pointer",
5574 c_addr_space_name (as_from));
5575
5576 else
5577 warning_at (loc, 0, "cast to %s address space pointer "
5578 "from disjoint %s address space pointer",
5579 c_addr_space_name (as_to),
5580 c_addr_space_name (as_from));
5581 }
5582 }
5583
3e4093b6 5584 /* Warn about possible alignment problems. */
1d933576 5585 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
3e4093b6
RS
5586 && TREE_CODE (type) == POINTER_TYPE
5587 && TREE_CODE (otype) == POINTER_TYPE
5588 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5589 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5590 /* Don't warn about opaque types, where the actual alignment
5591 restriction is unknown. */
3e636daf 5592 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6 5593 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
1d933576
BE
5594 && min_align_of_type (TREE_TYPE (type))
5595 > min_align_of_type (TREE_TYPE (otype)))
c2255bc4
AH
5596 warning_at (loc, OPT_Wcast_align,
5597 "cast increases required alignment of target type");
e9a25f70 5598
3176a0c2 5599 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5600 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5601 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5602 /* Unlike conversion of integers to pointers, where the
5603 warning is disabled for converting constants because
5604 of cases such as SIG_*, warn about converting constant
5605 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5606 sign extension, and a warning is appropriate. */
c2255bc4
AH
5607 warning_at (loc, OPT_Wpointer_to_int_cast,
5608 "cast from pointer to integer of different size");
400fbf9f 5609
3176a0c2 5610 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5611 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5612 warning_at (loc, OPT_Wbad_function_cast,
5613 "cast from function call of type %qT "
5614 "to non-matching type %qT", otype, type);
400fbf9f 5615
3176a0c2 5616 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5617 && TREE_CODE (otype) == INTEGER_TYPE
5618 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5619 /* Don't warn about converting any constant. */
5620 && !TREE_CONSTANT (value))
c2255bc4
AH
5621 warning_at (loc,
5622 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5623 "of different size");
400fbf9f 5624
79bedddc
SR
5625 if (warn_strict_aliasing <= 2)
5626 strict_aliasing_warning (otype, type, expr);
400fbf9f 5627
3897f229
JM
5628 /* If pedantic, warn for conversions between function and object
5629 pointer types, except for converting a null pointer constant
5630 to function pointer type. */
5631 if (pedantic
5632 && TREE_CODE (type) == POINTER_TYPE
5633 && TREE_CODE (otype) == POINTER_TYPE
5634 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5635 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5636 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5637 "conversion of function pointer to object pointer type");
3897f229
JM
5638
5639 if (pedantic
5640 && TREE_CODE (type) == POINTER_TYPE
5641 && TREE_CODE (otype) == POINTER_TYPE
5642 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5643 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5644 && !null_pointer_constant_p (value))
c1771a20 5645 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5646 "conversion of object pointer to function pointer type");
3897f229 5647
3e4093b6 5648 ovalue = value;
3e4093b6 5649 value = convert (type, value);
400fbf9f 5650
3e4093b6 5651 /* Ignore any integer overflow caused by the cast. */
928c19bb 5652 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5653 {
8bcd6380 5654 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5655 {
8bcd6380
RS
5656 if (!TREE_OVERFLOW (value))
5657 {
5658 /* Avoid clobbering a shared constant. */
5659 value = copy_node (value);
5660 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5661 }
6414bad6 5662 }
8bcd6380 5663 else if (TREE_OVERFLOW (value))
d8e1f97b 5664 /* Reset VALUE's overflow flags, ensuring constant sharing. */
8e6cdc90 5665 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
3e4093b6
RS
5666 }
5667 }
400fbf9f 5668
53cd18ec 5669 /* Don't let a cast be an lvalue. */
9482b620 5670 if (lvalue_p (value))
db3927fb 5671 value = non_lvalue_loc (loc, value);
e9a25f70 5672
928c19bb
JM
5673 /* Don't allow the results of casting to floating-point or complex
5674 types be confused with actual constants, or casts involving
5675 integer and pointer types other than direct integer-to-integer
5676 and integer-to-pointer be confused with integer constant
5677 expressions and null pointer constants. */
5678 if (TREE_CODE (value) == REAL_CST
5679 || TREE_CODE (value) == COMPLEX_CST
5680 || (TREE_CODE (value) == INTEGER_CST
5681 && !((TREE_CODE (expr) == INTEGER_CST
5682 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5683 || TREE_CODE (expr) == REAL_CST
5684 || TREE_CODE (expr) == COMPLEX_CST)))
5685 value = build1 (NOP_EXPR, type, value);
5686
21ba0cea 5687 protected_set_expr_location (value, loc);
3e4093b6 5688 return value;
400fbf9f
JW
5689}
5690
c2255bc4
AH
5691/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5692 location of the open paren of the cast, or the position of the cast
5693 expr. */
3e4093b6 5694tree
c2255bc4 5695c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5696{
f8893e47 5697 tree type;
928c19bb
JM
5698 tree type_expr = NULL_TREE;
5699 bool type_expr_const = true;
5700 tree ret;
3e4093b6 5701 int saved_wsp = warn_strict_prototypes;
c5c76735 5702
3e4093b6
RS
5703 /* This avoids warnings about unprototyped casts on
5704 integers. E.g. "#define SIG_DFL (void(*)())0". */
5705 if (TREE_CODE (expr) == INTEGER_CST)
5706 warn_strict_prototypes = 0;
928c19bb 5707 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5708 warn_strict_prototypes = saved_wsp;
c5c76735 5709
1807ffc1
MS
5710 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5711 && reject_gcc_builtin (expr))
5712 return error_mark_node;
5713
c2255bc4 5714 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5715 if (type_expr)
5716 {
9f33203d
JM
5717 bool inner_expr_const = true;
5718 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5719 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5720 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5721 && inner_expr_const);
c2255bc4 5722 SET_EXPR_LOCATION (ret, loc);
928c19bb 5723 }
24b97832 5724
21ba0cea
MP
5725 if (!EXPR_HAS_LOCATION (ret))
5726 protected_set_expr_location (ret, loc);
24b97832 5727
9e5b2115
PB
5728 /* C++ does not permits types to be defined in a cast, but it
5729 allows references to incomplete types. */
5730 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5731 warning_at (loc, OPT_Wc___compat,
5732 "defining a type in a cast is invalid in C++");
5733
928c19bb 5734 return ret;
400fbf9f 5735}
3e4093b6
RS
5736\f
5737/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5738 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5739 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5740 MODIFYCODE is the code for a binary operator that we use
5741 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5742 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5743 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5744 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5745
c2255bc4
AH
5746 LOCATION is the location of the MODIFYCODE operator.
5747 RHS_LOC is the location of the RHS. */
2f6e4e97 5748
3e4093b6 5749tree
32e8bb8e 5750build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5751 enum tree_code modifycode,
c2255bc4 5752 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5753{
3e4093b6
RS
5754 tree result;
5755 tree newrhs;
241f845a 5756 tree rhseval = NULL_TREE;
3e4093b6
RS
5757 tree lhstype = TREE_TYPE (lhs);
5758 tree olhstype = lhstype;
928c19bb 5759 bool npc;
267bac10 5760 bool is_atomic_op;
e9a25f70 5761
3e4093b6 5762 /* Types that aren't fully specified cannot be used in assignments. */
4f2e1536 5763 lhs = require_complete_type (location, lhs);
e9a25f70 5764
3e4093b6
RS
5765 /* Avoid duplicate error messages from operands that had errors. */
5766 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5767 return error_mark_node;
400fbf9f 5768
4c2ecab0
JM
5769 /* Ensure an error for assigning a non-lvalue array to an array in
5770 C90. */
5771 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5772 {
5773 error_at (location, "assignment to expression with array type");
5774 return error_mark_node;
5775 }
5776
46a88c12 5777 /* For ObjC properties, defer this check. */
7bd11157 5778 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5779 return error_mark_node;
5780
267bac10
JM
5781 is_atomic_op = really_atomic_lvalue (lhs);
5782
3e4093b6 5783 newrhs = rhs;
400fbf9f 5784
928c19bb
JM
5785 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5786 {
5787 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5788 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5789 rhs_origtype);
928c19bb
JM
5790 if (inner == error_mark_node)
5791 return error_mark_node;
5792 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5793 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5794 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5795 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5796 protected_set_expr_location (result, location);
5797 return result;
5798 }
5799
3e4093b6
RS
5800 /* If a binary op has been requested, combine the old LHS value with the RHS
5801 producing the value we should actually store into the LHS. */
5802
5803 if (modifycode != NOP_EXPR)
400fbf9f 5804 {
f9c59f7e 5805 lhs = c_fully_fold (lhs, false, NULL, true);
3e4093b6 5806 lhs = stabilize_reference (lhs);
bbbbb16a 5807
267bac10
JM
5808 /* Construct the RHS for any non-atomic compound assignemnt. */
5809 if (!is_atomic_op)
5810 {
241f845a
JJ
5811 /* If in LHS op= RHS the RHS has side-effects, ensure they
5812 are preevaluated before the rest of the assignment expression's
5813 side-effects, because RHS could contain e.g. function calls
5814 that modify LHS. */
5815 if (TREE_SIDE_EFFECTS (rhs))
5816 {
e035be33
JM
5817 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5818 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5819 else
5820 newrhs = save_expr (rhs);
241f845a 5821 rhseval = newrhs;
e035be33
JM
5822 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5823 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5824 newrhs);
241f845a 5825 }
267bac10 5826 newrhs = build_binary_op (location,
30af3a2b 5827 modifycode, lhs, newrhs, true);
267bac10
JM
5828
5829 /* The original type of the right hand side is no longer
5830 meaningful. */
5831 rhs_origtype = NULL_TREE;
5832 }
400fbf9f 5833 }
400fbf9f 5834
668ea4b1
IS
5835 if (c_dialect_objc ())
5836 {
46a88c12
NP
5837 /* Check if we are modifying an Objective-C property reference;
5838 if so, we need to generate setter calls. */
e035be33
JM
5839 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5840 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5841 else
5842 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5843 if (result)
241f845a 5844 goto return_result;
46a88c12
NP
5845
5846 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5847 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5848 return error_mark_node;
5849 }
5850
9bf24266 5851 /* Give an error for storing in something that is 'const'. */
bbbd6700 5852
f37acdf9 5853 if (TYPE_READONLY (lhstype)
3e636daf 5854 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5855 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5856 {
c02065fc 5857 readonly_error (location, lhs, lv_assign);
953ff289
DN
5858 return error_mark_node;
5859 }
f37acdf9
JM
5860 else if (TREE_READONLY (lhs))
5861 readonly_warning (lhs, lv_assign);
bbbd6700 5862
3e4093b6
RS
5863 /* If storing into a structure or union member,
5864 it has probably been given type `int'.
5865 Compute the type that would go with
5866 the actual amount of storage the member occupies. */
bbbd6700 5867
3e4093b6
RS
5868 if (TREE_CODE (lhs) == COMPONENT_REF
5869 && (TREE_CODE (lhstype) == INTEGER_TYPE
5870 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5871 || TREE_CODE (lhstype) == REAL_TYPE
5872 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5873 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5874
3e4093b6
RS
5875 /* If storing in a field that is in actuality a short or narrower than one,
5876 we must store in the field in its actual type. */
5877
5878 if (lhstype != TREE_TYPE (lhs))
5879 {
5880 lhs = copy_node (lhs);
5881 TREE_TYPE (lhs) = lhstype;
400fbf9f 5882 }
400fbf9f 5883
32e8bb8e
ILT
5884 /* Issue -Wc++-compat warnings about an assignment to an enum type
5885 when LHS does not have its original type. This happens for,
5886 e.g., an enum bitfield in a struct. */
5887 if (warn_cxx_compat
5888 && lhs_origtype != NULL_TREE
5889 && lhs_origtype != lhstype
5890 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5891 {
5892 tree checktype = (rhs_origtype != NULL_TREE
5893 ? rhs_origtype
5894 : TREE_TYPE (rhs));
5895 if (checktype != error_mark_node
267bac10
JM
5896 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5897 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5898 warning_at (location, OPT_Wc___compat,
5899 "enum conversion in assignment is invalid in C++");
5900 }
5901
267bac10
JM
5902 /* If the lhs is atomic, remove that qualifier. */
5903 if (is_atomic_op)
5904 {
5905 lhstype = build_qualified_type (lhstype,
5906 (TYPE_QUALS (lhstype)
5907 & ~TYPE_QUAL_ATOMIC));
5908 olhstype = build_qualified_type (olhstype,
5909 (TYPE_QUALS (lhstype)
5910 & ~TYPE_QUAL_ATOMIC));
5911 }
5912
8ce94e44
JM
5913 /* Convert new value to destination type. Fold it first, then
5914 restore any excess precision information, for the sake of
5915 conversion warnings. */
400fbf9f 5916
267bac10
JM
5917 if (!(is_atomic_op && modifycode != NOP_EXPR))
5918 {
e035be33
JM
5919 tree rhs_semantic_type = NULL_TREE;
5920 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5921 {
5922 rhs_semantic_type = TREE_TYPE (newrhs);
5923 newrhs = TREE_OPERAND (newrhs, 0);
5924 }
267bac10
JM
5925 npc = null_pointer_constant_p (newrhs);
5926 newrhs = c_fully_fold (newrhs, false, NULL);
5927 if (rhs_semantic_type)
5928 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5929 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5930 rhs_origtype, ic_assign, npc,
5931 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5932 if (TREE_CODE (newrhs) == ERROR_MARK)
5933 return error_mark_node;
5934 }
400fbf9f 5935
6e955430
ZL
5936 /* Emit ObjC write barrier, if necessary. */
5937 if (c_dialect_objc () && flag_objc_gc)
5938 {
5939 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5940 if (result)
c9f9eb5d
AH
5941 {
5942 protected_set_expr_location (result, location);
241f845a 5943 goto return_result;
c9f9eb5d 5944 }
6e955430
ZL
5945 }
5946
ea4b7848 5947 /* Scan operands. */
400fbf9f 5948
267bac10
JM
5949 if (is_atomic_op)
5950 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5951 else
5952 {
5953 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5954 TREE_SIDE_EFFECTS (result) = 1;
5955 protected_set_expr_location (result, location);
5956 }
400fbf9f 5957
3e4093b6
RS
5958 /* If we got the LHS in a different type for storing in,
5959 convert the result back to the nominal type of LHS
5960 so that the value we return always has the same type
5961 as the LHS argument. */
e855c5ce 5962
3e4093b6 5963 if (olhstype == TREE_TYPE (result))
241f845a 5964 goto return_result;
c9f9eb5d 5965
68fca595
MP
5966 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5967 rhs_origtype, ic_assign, false, NULL_TREE,
5968 NULL_TREE, 0);
c9f9eb5d 5969 protected_set_expr_location (result, location);
241f845a
JJ
5970
5971return_result:
5972 if (rhseval)
5973 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5974 return result;
3e4093b6
RS
5975}
5976\f
478a1c5b
ILT
5977/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5978 This is used to implement -fplan9-extensions. */
5979
5980static bool
5981find_anonymous_field_with_type (tree struct_type, tree type)
5982{
5983 tree field;
5984 bool found;
5985
3e636daf 5986 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5987 found = false;
5988 for (field = TYPE_FIELDS (struct_type);
5989 field != NULL_TREE;
5990 field = TREE_CHAIN (field))
5991 {
267bac10
JM
5992 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5993 ? c_build_qualified_type (TREE_TYPE (field),
5994 TYPE_QUAL_ATOMIC)
5995 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5996 if (DECL_NAME (field) == NULL
267bac10 5997 && comptypes (type, fieldtype))
478a1c5b
ILT
5998 {
5999 if (found)
6000 return false;
6001 found = true;
6002 }
6003 else if (DECL_NAME (field) == NULL
3e636daf 6004 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
6005 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6006 {
6007 if (found)
6008 return false;
6009 found = true;
6010 }
6011 }
6012 return found;
6013}
6014
6015/* RHS is an expression whose type is pointer to struct. If there is
6016 an anonymous field in RHS with type TYPE, then return a pointer to
6017 that field in RHS. This is used with -fplan9-extensions. This
6018 returns NULL if no conversion could be found. */
6019
6020static tree
6021convert_to_anonymous_field (location_t location, tree type, tree rhs)
6022{
6023 tree rhs_struct_type, lhs_main_type;
6024 tree field, found_field;
6025 bool found_sub_field;
6026 tree ret;
6027
6028 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6029 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 6030 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
6031
6032 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
6033 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6034 ? c_build_qualified_type (TREE_TYPE (type),
6035 TYPE_QUAL_ATOMIC)
6036 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
6037
6038 found_field = NULL_TREE;
6039 found_sub_field = false;
6040 for (field = TYPE_FIELDS (rhs_struct_type);
6041 field != NULL_TREE;
6042 field = TREE_CHAIN (field))
6043 {
6044 if (DECL_NAME (field) != NULL_TREE
3e636daf 6045 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 6046 continue;
267bac10
JM
6047 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6048 ? c_build_qualified_type (TREE_TYPE (field),
6049 TYPE_QUAL_ATOMIC)
6050 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6051 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
6052 {
6053 if (found_field != NULL_TREE)
6054 return NULL_TREE;
6055 found_field = field;
6056 }
6057 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6058 lhs_main_type))
6059 {
6060 if (found_field != NULL_TREE)
6061 return NULL_TREE;
6062 found_field = field;
6063 found_sub_field = true;
6064 }
6065 }
6066
6067 if (found_field == NULL_TREE)
6068 return NULL_TREE;
6069
6070 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6071 build_fold_indirect_ref (rhs), found_field,
6072 NULL_TREE);
6073 ret = build_fold_addr_expr_loc (location, ret);
6074
6075 if (found_sub_field)
6076 {
6077 ret = convert_to_anonymous_field (location, type, ret);
6078 gcc_assert (ret != NULL_TREE);
6079 }
6080
6081 return ret;
6082}
6083
63bc4e87
MP
6084/* Issue an error message for a bad initializer component.
6085 GMSGID identifies the message.
6086 The component name is taken from the spelling stack. */
6087
6088static void
ea58ef42 6089error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
6090{
6091 char *ofwhat;
6092
6093 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 6094 error_at (loc, gmsgid);
63bc4e87
MP
6095 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6096 if (*ofwhat)
d7ff7ae5 6097 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6098}
6099
6100/* Issue a pedantic warning for a bad initializer component. OPT is
6101 the option OPT_* (from options.h) controlling this warning or 0 if
6102 it is unconditionally given. GMSGID identifies the message. The
6103 component name is taken from the spelling stack. */
6104
296c53ac
MP
6105static void ATTRIBUTE_GCC_DIAG (3,0)
6106pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
63bc4e87 6107{
79063edd
MS
6108 /* Use the location where a macro was expanded rather than where
6109 it was defined to make sure macros defined in system headers
6110 but used incorrectly elsewhere are diagnosed. */
6111 source_location exploc = expansion_point_location_if_in_system_header (loc);
6112
296c53ac
MP
6113 va_list ap;
6114 va_start (ap, gmsgid);
6115 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6116 va_end (ap);
6117 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6118 if (*ofwhat && warned)
79063edd 6119 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6120}
6121
6122/* Issue a warning for a bad initializer component.
6123
6124 OPT is the OPT_W* value corresponding to the warning option that
6125 controls this warning. GMSGID identifies the message. The
6126 component name is taken from the spelling stack. */
6127
6128static void
6129warning_init (location_t loc, int opt, const char *gmsgid)
6130{
6131 char *ofwhat;
d7ff7ae5 6132 bool warned;
63bc4e87 6133
79063edd
MS
6134 /* Use the location where a macro was expanded rather than where
6135 it was defined to make sure macros defined in system headers
6136 but used incorrectly elsewhere are diagnosed. */
6137 source_location exploc = expansion_point_location_if_in_system_header (loc);
6138
63bc4e87 6139 /* The gmsgid may be a format string with %< and %>. */
79063edd 6140 warned = warning_at (exploc, opt, gmsgid);
63bc4e87 6141 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6142 if (*ofwhat && warned)
79063edd 6143 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6144}
6145\f
6146/* If TYPE is an array type and EXPR is a parenthesized string
6147 constant, warn if pedantic that EXPR is being used to initialize an
6148 object of type TYPE. */
6149
6150void
d033409e 6151maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
6152{
6153 if (pedantic
6154 && TREE_CODE (type) == ARRAY_TYPE
6155 && TREE_CODE (expr.value) == STRING_CST
6156 && expr.original_code != STRING_CST)
d033409e 6157 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
6158 "array initialized from parenthesized string constant");
6159}
6160
8139a48e
DM
6161/* Attempt to locate the parameter with the given index within FNDECL,
6162 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6163
6164static location_t
6165get_fndecl_argument_location (tree fndecl, int argnum)
6166{
6167 int i;
6168 tree param;
6169
6170 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6171 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6172 i < argnum && param;
6173 i++, param = TREE_CHAIN (param))
6174 ;
6175
6176 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6177 return DECL_SOURCE_LOCATION (FNDECL). */
6178 if (param == NULL)
6179 return DECL_SOURCE_LOCATION (fndecl);
6180
6181 return DECL_SOURCE_LOCATION (param);
6182}
6183
6184/* Issue a note about a mismatching argument for parameter PARMNUM
6185 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6186 Attempt to issue the note at the pertinent parameter of the decl;
6187 failing that issue it at the location of FUNDECL; failing that
6188 issue it at PLOC. */
6189
6190static void
6191inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6192 tree expected_type, tree actual_type)
6193{
6194 location_t loc;
6195 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6196 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6197 else
6198 loc = ploc;
6199
6200 inform (loc,
6201 "expected %qT but argument is of type %qT",
6202 expected_type, actual_type);
6203}
6204
bbbbb16a
ILT
6205/* Convert value RHS to type TYPE as preparation for an assignment to
6206 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6207 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6208 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6209 constant before any folding.
3e4093b6
RS
6210 The real work of conversion is done by `convert'.
6211 The purpose of this function is to generate error messages
6212 for assignments that are not allowed in C.
2ac2f164
JM
6213 ERRTYPE says whether it is argument passing, assignment,
6214 initialization or return.
2f6e4e97 6215
aabef2de
MP
6216 In the following example, '~' denotes where EXPR_LOC and '^' where
6217 LOCATION point to:
6218
6219 f (var); [ic_argpass]
6220 ^ ~~~
6221 x = var; [ic_assign]
6222 ^ ~~~;
6223 int x = var; [ic_init]
6224 ^^^
6225 return x; [ic_return]
6226 ^
6227
2ac2f164 6228 FUNCTION is a tree for the function being called.
3e4093b6 6229 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 6230
3e4093b6 6231static tree
81e5eca8 6232convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 6233 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
6234 bool null_pointer_constant, tree fundecl,
6235 tree function, int parmnum)
3e4093b6
RS
6236{
6237 enum tree_code codel = TREE_CODE (type);
8ce94e44 6238 tree orig_rhs = rhs;
3e4093b6
RS
6239 tree rhstype;
6240 enum tree_code coder;
2ac2f164 6241 tree rname = NULL_TREE;
58393038 6242 bool objc_ok = false;
2ac2f164 6243
1c7485af
MP
6244 /* Use the expansion point location to handle cases such as user's
6245 function returning a wrong-type macro defined in a system header. */
6246 location = expansion_point_location_if_in_system_header (location);
6247
6b4ef5c1 6248 if (errtype == ic_argpass)
2ac2f164
JM
6249 {
6250 tree selector;
6251 /* Change pointer to function to the function itself for
6252 diagnostics. */
6253 if (TREE_CODE (function) == ADDR_EXPR
6254 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6255 function = TREE_OPERAND (function, 0);
6256
6257 /* Handle an ObjC selector specially for diagnostics. */
6258 selector = objc_message_selector ();
6259 rname = function;
6260 if (selector && parmnum > 2)
6261 {
6262 rname = selector;
6263 parmnum -= 2;
6264 }
6265 }
6266
6267 /* This macro is used to emit diagnostics to ensure that all format
6268 strings are complete sentences, visible to gettext and checked at
6269 compile time. */
768952be 6270#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
6271 do { \
6272 switch (errtype) \
6273 { \
6274 case ic_argpass: \
5c1bc275 6275 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8139a48e 6276 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
1e053dfe 6277 break; \
1e053dfe
MLI
6278 case ic_assign: \
6279 pedwarn (LOCATION, OPT, AS); \
6280 break; \
6281 case ic_init: \
6a8f4e12 6282 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
6283 break; \
6284 case ic_return: \
d033409e 6285 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
6286 break; \
6287 default: \
6288 gcc_unreachable (); \
6289 } \
2ac2f164 6290 } while (0)
cb3ca04e 6291
49706e39
MLI
6292 /* This macro is used to emit diagnostics to ensure that all format
6293 strings are complete sentences, visible to gettext and checked at
768952be 6294 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 6295 extra parameter to enumerate qualifiers. */
768952be 6296#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6297 do { \
6298 switch (errtype) \
6299 { \
6300 case ic_argpass: \
5c1bc275 6301 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8139a48e 6302 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
49706e39
MLI
6303 break; \
6304 case ic_assign: \
5c1bc275 6305 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6306 break; \
6307 case ic_init: \
5c1bc275 6308 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6309 break; \
6310 case ic_return: \
5c1bc275 6311 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6312 break; \
6313 default: \
6314 gcc_unreachable (); \
6315 } \
6316 } while (0)
6317
768952be
MU
6318 /* This macro is used to emit diagnostics to ensure that all format
6319 strings are complete sentences, visible to gettext and checked at
6320 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6321 warning_at instead of pedwarn. */
6322#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6323 do { \
6324 switch (errtype) \
6325 { \
6326 case ic_argpass: \
6327 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8139a48e 6328 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
768952be
MU
6329 break; \
6330 case ic_assign: \
6331 warning_at (LOCATION, OPT, AS, QUALS); \
6332 break; \
6333 case ic_init: \
6334 warning_at (LOCATION, OPT, IN, QUALS); \
6335 break; \
6336 case ic_return: \
6337 warning_at (LOCATION, OPT, RE, QUALS); \
6338 break; \
6339 default: \
6340 gcc_unreachable (); \
6341 } \
6342 } while (0)
6343
8ce94e44
JM
6344 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6345 rhs = TREE_OPERAND (rhs, 0);
6346
3e4093b6
RS
6347 rhstype = TREE_TYPE (rhs);
6348 coder = TREE_CODE (rhstype);
6349
6350 if (coder == ERROR_MARK)
6351 return error_mark_node;
6352
58393038
ZL
6353 if (c_dialect_objc ())
6354 {
6355 int parmno;
6356
6357 switch (errtype)
6358 {
6359 case ic_return:
6360 parmno = 0;
6361 break;
6362
6363 case ic_assign:
6364 parmno = -1;
6365 break;
6366
6367 case ic_init:
6368 parmno = -2;
6369 break;
6370
6371 default:
6372 parmno = parmnum;
6373 break;
6374 }
6375
6376 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6377 }
6378
bbbbb16a
ILT
6379 if (warn_cxx_compat)
6380 {
6381 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6382 if (checktype != error_mark_node
6383 && TREE_CODE (type) == ENUMERAL_TYPE
6384 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
296c53ac
MP
6385 switch (errtype)
6386 {
6387 case ic_argpass:
6388 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6389 "passing argument %d of %qE is invalid in C++",
6390 parmnum, rname))
6391 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6392 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6393 "expected %qT but argument is of type %qT",
6394 type, rhstype);
6395 break;
6396 case ic_assign:
6397 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6398 "%qT in assignment is invalid in C++", rhstype, type);
6399 break;
6400 case ic_init:
6401 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6402 "%qT to %qT in initialization is invalid in C++",
6403 rhstype, type);
6404 break;
6405 case ic_return:
6406 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6407 "%qT in return is invalid in C++", rhstype, type);
6408 break;
6409 default:
6410 gcc_unreachable ();
6411 }
bbbbb16a
ILT
6412 }
6413
3e4093b6 6414 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6415 return rhs;
3e4093b6
RS
6416
6417 if (coder == VOID_TYPE)
400fbf9f 6418 {
6dcc04b0
JM
6419 /* Except for passing an argument to an unprototyped function,
6420 this is a constraint violation. When passing an argument to
6421 an unprototyped function, it is compile-time undefined;
6422 making it a constraint in that case was rejected in
6423 DR#252. */
c2255bc4 6424 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6425 return error_mark_node;
400fbf9f 6426 }
4f2e1536 6427 rhs = require_complete_type (location, rhs);
808d6eaa
JM
6428 if (rhs == error_mark_node)
6429 return error_mark_node;
1807ffc1
MS
6430
6431 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6432 return error_mark_node;
6433
cd192ccc
MS
6434 /* A non-reference type can convert to a reference. This handles
6435 va_start, va_copy and possibly port built-ins. */
6436 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6437 {
3e4093b6 6438 if (!lvalue_p (rhs))
400fbf9f 6439 {
c2255bc4 6440 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6441 return error_mark_node;
400fbf9f 6442 }
3e4093b6
RS
6443 if (!c_mark_addressable (rhs))
6444 return error_mark_node;
6445 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6446 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6447
81e5eca8 6448 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6449 build_pointer_type (TREE_TYPE (type)),
6450 rhs, origtype, errtype,
6451 null_pointer_constant, fundecl, function,
6452 parmnum);
cd192ccc
MS
6453 if (rhs == error_mark_node)
6454 return error_mark_node;
3e4093b6
RS
6455
6456 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6457 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6458 return rhs;
400fbf9f 6459 }
3e4093b6 6460 /* Some types can interconvert without explicit casts. */
3274deff 6461 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6462 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6463 return convert (type, rhs);
6464 /* Arithmetic types all interconvert, and enum is treated like int. */
6465 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6466 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6467 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6468 || codel == BOOLEAN_TYPE)
6469 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6470 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6471 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6472 || coder == BOOLEAN_TYPE))
928c19bb
JM
6473 {
6474 tree ret;
6475 bool save = in_late_binary_op;
e5341100
JJ
6476 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6477 || (coder == REAL_TYPE
6478 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
45b2222a 6479 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
928c19bb 6480 in_late_binary_op = true;
81e5eca8
MP
6481 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6482 ? expr_loc : location, type, orig_rhs);
e5341100 6483 in_late_binary_op = save;
928c19bb
JM
6484 return ret;
6485 }
400fbf9f 6486
79077aea
JJ
6487 /* Aggregates in different TUs might need conversion. */
6488 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6489 && codel == coder
6490 && comptypes (type, rhstype))
81e5eca8
MP
6491 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6492 ? expr_loc : location, type, rhs);
79077aea 6493
ebf0bf7f 6494 /* Conversion to a transparent union or record from its member types.
3e4093b6 6495 This applies only to function arguments. */
ebf0bf7f
JJ
6496 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6497 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6498 && errtype == ic_argpass)
400fbf9f 6499 {
0257e383 6500 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6501
910ad8de 6502 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6503 {
0257e383 6504 tree memb_type = TREE_TYPE (memb);
400fbf9f 6505
3e4093b6 6506 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6507 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6508 break;
e58cd767 6509
3e4093b6
RS
6510 if (TREE_CODE (memb_type) != POINTER_TYPE)
6511 continue;
2f6e4e97 6512
3e4093b6
RS
6513 if (coder == POINTER_TYPE)
6514 {
6515 tree ttl = TREE_TYPE (memb_type);
6516 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6517
3e4093b6
RS
6518 /* Any non-function converts to a [const][volatile] void *
6519 and vice versa; otherwise, targets must be the same.
6520 Meanwhile, the lhs target must have all the qualifiers of
6521 the rhs. */
267bac10
JM
6522 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6523 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6524 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6525 {
267bac10
JM
6526 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6527 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6528 /* If this type won't generate any warnings, use it. */
267bac10 6529 if (lquals == rquals
3e4093b6
RS
6530 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6531 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6532 ? ((lquals | rquals) == rquals)
6533 : ((lquals | rquals) == lquals)))
3e4093b6 6534 break;
400fbf9f 6535
3e4093b6 6536 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6537 if (!marginal_memb)
6538 marginal_memb = memb;
3e4093b6
RS
6539 }
6540 }
82bde854 6541
3e4093b6 6542 /* Can convert integer zero to any pointer type. */
928c19bb 6543 if (null_pointer_constant)
3e4093b6
RS
6544 {
6545 rhs = null_pointer_node;
6546 break;
6547 }
6548 }
400fbf9f 6549
0257e383 6550 if (memb || marginal_memb)
3e4093b6 6551 {
0257e383 6552 if (!memb)
3e4093b6
RS
6553 {
6554 /* We have only a marginally acceptable member type;
6555 it needs a warning. */
0257e383 6556 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6557 tree ttr = TREE_TYPE (rhstype);
714a0864 6558
3e4093b6
RS
6559 /* Const and volatile mean something different for function
6560 types, so the usual warnings are not appropriate. */
6561 if (TREE_CODE (ttr) == FUNCTION_TYPE
6562 && TREE_CODE (ttl) == FUNCTION_TYPE)
6563 {
6564 /* Because const and volatile on functions are
6565 restrictions that say the function will not do
6566 certain things, it is okay to use a const or volatile
6567 function where an ordinary one is wanted, but not
6568 vice-versa. */
36c5e70a
BE
6569 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6570 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6571 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6572 OPT_Wdiscarded_qualifiers,
6573 G_("passing argument %d of %qE "
6574 "makes %q#v qualified function "
6575 "pointer from unqualified"),
6576 G_("assignment makes %q#v qualified "
6577 "function pointer from "
6578 "unqualified"),
6579 G_("initialization makes %q#v qualified "
6580 "function pointer from "
6581 "unqualified"),
6582 G_("return makes %q#v qualified function "
6583 "pointer from unqualified"),
6584 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6585 }
36c5e70a
BE
6586 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6587 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6588 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6589 OPT_Wdiscarded_qualifiers,
6590 G_("passing argument %d of %qE discards "
6591 "%qv qualifier from pointer target type"),
6592 G_("assignment discards %qv qualifier "
6593 "from pointer target type"),
6594 G_("initialization discards %qv qualifier "
6595 "from pointer target type"),
6596 G_("return discards %qv qualifier from "
6597 "pointer target type"),
6598 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6599
6600 memb = marginal_memb;
3e4093b6 6601 }
400fbf9f 6602
fcf73884 6603 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6604 pedwarn (location, OPT_Wpedantic,
fcf73884 6605 "ISO C prohibits argument conversion to union type");
0e7c47fa 6606
db3927fb 6607 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6608 return build_constructor_single (type, memb, rhs);
3e4093b6 6609 }
0e7c47fa
RK
6610 }
6611
3e4093b6
RS
6612 /* Conversions among pointers */
6613 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6614 && (coder == codel))
400fbf9f 6615 {
3e4093b6
RS
6616 tree ttl = TREE_TYPE (type);
6617 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6618 tree mvl = ttl;
6619 tree mvr = ttr;
3e4093b6 6620 bool is_opaque_pointer;
264fa2db 6621 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6622 addr_space_t asl;
6623 addr_space_t asr;
400fbf9f 6624
46df2823 6625 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6626 mvl = (TYPE_ATOMIC (mvl)
6627 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6628 TYPE_QUAL_ATOMIC)
6629 : TYPE_MAIN_VARIANT (mvl));
46df2823 6630 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6631 mvr = (TYPE_ATOMIC (mvr)
6632 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6633 TYPE_QUAL_ATOMIC)
6634 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6635 /* Opaque pointers are treated like void pointers. */
f83c7f63 6636 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6637
478a1c5b
ILT
6638 /* The Plan 9 compiler permits a pointer to a struct to be
6639 automatically converted into a pointer to an anonymous field
6640 within the struct. */
6641 if (flag_plan9_extensions
3e636daf
MP
6642 && RECORD_OR_UNION_TYPE_P (mvl)
6643 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6644 && mvl != mvr)
6645 {
6646 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6647 if (new_rhs != NULL_TREE)
6648 {
6649 rhs = new_rhs;
6650 rhstype = TREE_TYPE (rhs);
6651 coder = TREE_CODE (rhstype);
6652 ttr = TREE_TYPE (rhstype);
6653 mvr = TYPE_MAIN_VARIANT (ttr);
6654 }
6655 }
6656
b7e20b53 6657 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6658 for the purpose of reducing the number of false positives, we
6659 tolerate the special case of
b7e20b53 6660
c22cacf3 6661 int *p = NULL;
b7e20b53 6662
c22cacf3 6663 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6664 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6665 warning_at (errtype == ic_argpass ? expr_loc : location,
6666 OPT_Wc___compat,
6667 "request for implicit conversion "
c2255bc4 6668 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6669
36c5e70a
BE
6670 /* See if the pointers point to incompatible address spaces. */
6671 asl = TYPE_ADDR_SPACE (ttl);
6672 asr = TYPE_ADDR_SPACE (ttr);
6673 if (!null_pointer_constant_p (rhs)
6674 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6675 {
6676 switch (errtype)
6677 {
6678 case ic_argpass:
8ffcdea8 6679 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6680 "non-enclosed address space", parmnum, rname);
6681 break;
6682 case ic_assign:
6683 error_at (location, "assignment from pointer to "
6684 "non-enclosed address space");
6685 break;
6686 case ic_init:
6687 error_at (location, "initialization from pointer to "
6688 "non-enclosed address space");
6689 break;
6690 case ic_return:
6691 error_at (location, "return from pointer to "
6692 "non-enclosed address space");
6693 break;
6694 default:
6695 gcc_unreachable ();
6696 }
6697 return error_mark_node;
6698 }
6699
7876a414
KG
6700 /* Check if the right-hand side has a format attribute but the
6701 left-hand side doesn't. */
90137d8f 6702 if (warn_suggest_attribute_format
104f8784 6703 && check_missing_format_attribute (type, rhstype))
c22cacf3 6704 {
104f8784
KG
6705 switch (errtype)
6706 {
6707 case ic_argpass:
8ffcdea8 6708 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6709 "argument %d of %qE might be "
6710 "a candidate for a format attribute",
6711 parmnum, rname);
104f8784
KG
6712 break;
6713 case ic_assign:
90137d8f 6714 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6715 "assignment left-hand side might be "
6716 "a candidate for a format attribute");
104f8784
KG
6717 break;
6718 case ic_init:
90137d8f 6719 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6720 "initialization left-hand side might be "
6721 "a candidate for a format attribute");
104f8784
KG
6722 break;
6723 case ic_return:
90137d8f 6724 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6725 "return type might be "
6726 "a candidate for a format attribute");
104f8784
KG
6727 break;
6728 default:
6729 gcc_unreachable ();
6730 }
7876a414 6731 }
c22cacf3 6732
3e4093b6
RS
6733 /* Any non-function converts to a [const][volatile] void *
6734 and vice versa; otherwise, targets must be the same.
6735 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6736 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6737 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6738 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6739 || is_opaque_pointer
f8a93a2e
JJ
6740 || ((c_common_unsigned_type (mvl)
6741 == c_common_unsigned_type (mvr))
267bac10
JM
6742 && (c_common_signed_type (mvl)
6743 == c_common_signed_type (mvr))
6744 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6745 {
768952be
MU
6746 /* Warn about loss of qualifers from pointers to arrays with
6747 qualifiers on the element type. */
6748 if (TREE_CODE (ttr) == ARRAY_TYPE)
6749 {
6750 ttr = strip_array_types (ttr);
6751 ttl = strip_array_types (ttl);
6752
6753 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6754 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6755 WARNING_FOR_QUALIFIERS (location, expr_loc,
6756 OPT_Wdiscarded_array_qualifiers,
6757 G_("passing argument %d of %qE discards "
6758 "%qv qualifier from pointer target type"),
6759 G_("assignment discards %qv qualifier "
6760 "from pointer target type"),
6761 G_("initialization discards %qv qualifier "
6762 "from pointer target type"),
6763 G_("return discards %qv qualifier from "
6764 "pointer target type"),
6765 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6766 }
6767 else if (pedantic
3e4093b6
RS
6768 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6769 ||
6770 (VOID_TYPE_P (ttr)
928c19bb 6771 && !null_pointer_constant
3e4093b6 6772 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6773 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6774 G_("ISO C forbids passing argument %d of "
6775 "%qE between function pointer "
6776 "and %<void *%>"),
6777 G_("ISO C forbids assignment between "
6778 "function pointer and %<void *%>"),
6779 G_("ISO C forbids initialization between "
6780 "function pointer and %<void *%>"),
6781 G_("ISO C forbids return between function "
6782 "pointer and %<void *%>"));
3e4093b6
RS
6783 /* Const and volatile mean something different for function types,
6784 so the usual warnings are not appropriate. */
6785 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6786 && TREE_CODE (ttl) != FUNCTION_TYPE)
6787 {
768952be
MU
6788 /* Don't warn about loss of qualifier for conversions from
6789 qualified void* to pointers to arrays with corresponding
6790 qualifier on the element type. */
6791 if (!pedantic)
6792 ttl = strip_array_types (ttl);
6793
267bac10
JM
6794 /* Assignments between atomic and non-atomic objects are OK. */
6795 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6796 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6797 {
768952be
MU
6798 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6799 OPT_Wdiscarded_qualifiers,
6800 G_("passing argument %d of %qE discards "
6801 "%qv qualifier from pointer target type"),
6802 G_("assignment discards %qv qualifier "
6803 "from pointer target type"),
6804 G_("initialization discards %qv qualifier "
6805 "from pointer target type"),
6806 G_("return discards %qv qualifier from "
6807 "pointer target type"),
6808 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6809 }
3e4093b6
RS
6810 /* If this is not a case of ignoring a mismatch in signedness,
6811 no warning. */
6812 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6813 || target_cmp)
3e4093b6
RS
6814 ;
6815 /* If there is a mismatch, do warn. */
f2fd3821 6816 else if (warn_pointer_sign)
296c53ac
MP
6817 switch (errtype)
6818 {
6819 case ic_argpass:
6820 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6821 "pointer targets in passing argument %d of "
6822 "%qE differ in signedness", parmnum, rname))
6823 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6824 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6825 "expected %qT but argument is of type %qT",
6826 type, rhstype);
6827 break;
6828 case ic_assign:
6829 pedwarn (location, OPT_Wpointer_sign,
6830 "pointer targets in assignment from %qT to %qT "
6831 "differ in signedness", rhstype, type);
6832 break;
6833 case ic_init:
6834 pedwarn_init (location, OPT_Wpointer_sign,
6835 "pointer targets in initialization of %qT "
6836 "from %qT differ in signedness", type,
6837 rhstype);
6838 break;
6839 case ic_return:
6840 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6841 "returning %qT from a function with return type "
6842 "%qT differ in signedness", rhstype, type);
6843 break;
6844 default:
6845 gcc_unreachable ();
6846 }
3e4093b6
RS
6847 }
6848 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6849 && TREE_CODE (ttr) == FUNCTION_TYPE)
6850 {
6851 /* Because const and volatile on functions are restrictions
6852 that say the function will not do certain things,
6853 it is okay to use a const or volatile function
6854 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6855 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6856 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6857 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6858 OPT_Wdiscarded_qualifiers,
6859 G_("passing argument %d of %qE makes "
6860 "%q#v qualified function pointer "
6861 "from unqualified"),
6862 G_("assignment makes %q#v qualified function "
6863 "pointer from unqualified"),
6864 G_("initialization makes %q#v qualified "
6865 "function pointer from unqualified"),
6866 G_("return makes %q#v qualified function "
6867 "pointer from unqualified"),
6868 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6869 }
6870 }
296c53ac
MP
6871 /* Avoid warning about the volatile ObjC EH puts on decls. */
6872 else if (!objc_ok)
6873 {
6874 switch (errtype)
6875 {
6876 case ic_argpass:
6877 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6878 "passing argument %d of %qE from incompatible "
6879 "pointer type", parmnum, rname))
8139a48e 6880 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
296c53ac
MP
6881 break;
6882 case ic_assign:
6883 pedwarn (location, OPT_Wincompatible_pointer_types,
6884 "assignment to %qT from incompatible pointer type %qT",
6885 type, rhstype);
6886 break;
6887 case ic_init:
6888 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6889 "initialization of %qT from incompatible pointer "
6890 "type %qT", type, rhstype);
6891 break;
6892 case ic_return:
6893 pedwarn (location, OPT_Wincompatible_pointer_types,
6894 "returning %qT from a function with incompatible "
6895 "return type %qT", rhstype, type);
6896 break;
6897 default:
6898 gcc_unreachable ();
6899 }
6900 }
58393038 6901
3e4093b6
RS
6902 return convert (type, rhs);
6903 }
b494fd98
EB
6904 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6905 {
6dcc04b0
JM
6906 /* ??? This should not be an error when inlining calls to
6907 unprototyped functions. */
c2255bc4 6908 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6909 return error_mark_node;
6910 }
3e4093b6 6911 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6912 {
3e4093b6
RS
6913 /* An explicit constant 0 can convert to a pointer,
6914 or one that results from arithmetic, even including
6915 a cast to integer type. */
928c19bb 6916 if (!null_pointer_constant)
296c53ac
MP
6917 switch (errtype)
6918 {
6919 case ic_argpass:
6920 if (pedwarn (expr_loc, OPT_Wint_conversion,
6921 "passing argument %d of %qE makes pointer from "
6922 "integer without a cast", parmnum, rname))
8139a48e 6923 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
296c53ac
MP
6924 break;
6925 case ic_assign:
6926 pedwarn (location, OPT_Wint_conversion,
6927 "assignment to %qT from %qT makes pointer from integer "
6928 "without a cast", type, rhstype);
6929 break;
6930 case ic_init:
6931 pedwarn_init (location, OPT_Wint_conversion,
6932 "initialization of %qT from %qT makes pointer from "
6933 "integer without a cast", type, rhstype);
6934 break;
6935 case ic_return:
6936 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6937 "function with return type %qT makes pointer from "
6938 "integer without a cast", rhstype, type);
6939 break;
6940 default:
6941 gcc_unreachable ();
6942 }
b3006337
EB
6943
6944 return convert (type, rhs);
400fbf9f 6945 }
3e4093b6 6946 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6947 {
296c53ac
MP
6948 switch (errtype)
6949 {
6950 case ic_argpass:
6951 if (pedwarn (expr_loc, OPT_Wint_conversion,
6952 "passing argument %d of %qE makes integer from "
6953 "pointer without a cast", parmnum, rname))
8139a48e 6954 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
296c53ac
MP
6955 break;
6956 case ic_assign:
6957 pedwarn (location, OPT_Wint_conversion,
6958 "assignment to %qT from %qT makes integer from pointer "
6959 "without a cast", type, rhstype);
6960 break;
6961 case ic_init:
6962 pedwarn_init (location, OPT_Wint_conversion,
6963 "initialization of %qT from %qT makes integer from "
6964 "pointer without a cast", type, rhstype);
6965 break;
6966 case ic_return:
6967 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6968 "function with return type %qT makes integer from "
6969 "pointer without a cast", rhstype, type);
6970 break;
6971 default:
6972 gcc_unreachable ();
6973 }
6974
3e4093b6
RS
6975 return convert (type, rhs);
6976 }
6977 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6978 {
6979 tree ret;
6980 bool save = in_late_binary_op;
6981 in_late_binary_op = true;
6982 ret = convert (type, rhs);
6983 in_late_binary_op = save;
6984 return ret;
6985 }
400fbf9f 6986
2ac2f164 6987 switch (errtype)
3e4093b6 6988 {
2ac2f164 6989 case ic_argpass:
8ffcdea8
MP
6990 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6991 rname);
8139a48e 6992 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
2ac2f164
JM
6993 break;
6994 case ic_assign:
c2255bc4
AH
6995 error_at (location, "incompatible types when assigning to type %qT from "
6996 "type %qT", type, rhstype);
2ac2f164
JM
6997 break;
6998 case ic_init:
c2255bc4 6999 error_at (location,
8ffcdea8 7000 "incompatible types when initializing type %qT using type %qT",
c2255bc4 7001 type, rhstype);
2ac2f164
JM
7002 break;
7003 case ic_return:
c2255bc4 7004 error_at (location,
8ffcdea8 7005 "incompatible types when returning type %qT but %qT was "
c2255bc4 7006 "expected", rhstype, type);
2ac2f164
JM
7007 break;
7008 default:
7009 gcc_unreachable ();
400fbf9f 7010 }
53b01f59 7011
3e4093b6
RS
7012 return error_mark_node;
7013}
3e4093b6
RS
7014\f
7015/* If VALUE is a compound expr all of whose expressions are constant, then
7016 return its value. Otherwise, return error_mark_node.
15b732b2 7017
3e4093b6
RS
7018 This is for handling COMPOUND_EXPRs as initializer elements
7019 which is allowed with a warning when -pedantic is specified. */
15b732b2 7020
3e4093b6
RS
7021static tree
7022valid_compound_expr_initializer (tree value, tree endtype)
7023{
7024 if (TREE_CODE (value) == COMPOUND_EXPR)
7025 {
7026 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7027 == error_mark_node)
7028 return error_mark_node;
7029 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7030 endtype);
7031 }
116df786 7032 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
7033 return error_mark_node;
7034 else
7035 return value;
15b732b2 7036}
400fbf9f 7037\f
3e4093b6
RS
7038/* Perform appropriate conversions on the initial value of a variable,
7039 store it in the declaration DECL,
7040 and print any error messages that are appropriate.
bbbbb16a 7041 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
7042 If the init is invalid, store an ERROR_MARK.
7043
7044 INIT_LOC is the location of the initial value. */
400fbf9f 7045
3e4093b6 7046void
c2255bc4 7047store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 7048{
3e4093b6 7049 tree value, type;
928c19bb 7050 bool npc = false;
400fbf9f 7051
3e4093b6 7052 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 7053
3e4093b6
RS
7054 type = TREE_TYPE (decl);
7055 if (TREE_CODE (type) == ERROR_MARK)
7056 return;
400fbf9f 7057
3e4093b6 7058 /* Digest the specified initializer into an expression. */
400fbf9f 7059
928c19bb
JM
7060 if (init)
7061 npc = null_pointer_constant_p (init);
c2255bc4
AH
7062 value = digest_init (init_loc, type, init, origtype, npc,
7063 true, TREE_STATIC (decl));
400fbf9f 7064
3e4093b6 7065 /* Store the expression if valid; else report error. */
400fbf9f 7066
8400e75e 7067 if (!in_system_header_at (input_location)
3f75a254 7068 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
7069 warning (OPT_Wtraditional, "traditional C rejects automatic "
7070 "aggregate initialization");
2f6e4e97 7071
dea63e49
JJ
7072 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7073 DECL_INITIAL (decl) = value;
400fbf9f 7074
3e4093b6
RS
7075 /* ANSI wants warnings about out-of-range constant initializers. */
7076 STRIP_TYPE_NOPS (value);
b8698a0f 7077 if (TREE_STATIC (decl))
c2658540 7078 constant_expression_warning (value);
400fbf9f 7079
3e4093b6
RS
7080 /* Check if we need to set array size from compound literal size. */
7081 if (TREE_CODE (type) == ARRAY_TYPE
3fa8871b 7082 && TYPE_DOMAIN (type) == NULL_TREE
3e4093b6 7083 && value != error_mark_node)
400fbf9f 7084 {
3e4093b6
RS
7085 tree inside_init = init;
7086
ed248cf7 7087 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
7088 inside_init = fold (inside_init);
7089
7090 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7091 {
8d9f82d5 7092 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 7093
8d9f82d5 7094 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
7095 {
7096 /* For int foo[] = (int [3]){1}; we need to set array size
7097 now since later on array initializer will be just the
7098 brace enclosed list of the compound literal. */
e30ecc5d 7099 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 7100 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 7101 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 7102 layout_type (type);
8d9f82d5 7103 layout_decl (cldecl, 0);
e30ecc5d
JJ
7104 TREE_TYPE (decl)
7105 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
7106 }
7107 }
400fbf9f 7108 }
3e4093b6
RS
7109}
7110\f
7111/* Methods for storing and printing names for error messages. */
400fbf9f 7112
3e4093b6
RS
7113/* Implement a spelling stack that allows components of a name to be pushed
7114 and popped. Each element on the stack is this structure. */
400fbf9f 7115
3e4093b6
RS
7116struct spelling
7117{
7118 int kind;
7119 union
400fbf9f 7120 {
a0f0ab9f 7121 unsigned HOST_WIDE_INT i;
3e4093b6
RS
7122 const char *s;
7123 } u;
7124};
2f6e4e97 7125
3e4093b6
RS
7126#define SPELLING_STRING 1
7127#define SPELLING_MEMBER 2
7128#define SPELLING_BOUNDS 3
400fbf9f 7129
3e4093b6
RS
7130static struct spelling *spelling; /* Next stack element (unused). */
7131static struct spelling *spelling_base; /* Spelling stack base. */
7132static int spelling_size; /* Size of the spelling stack. */
400fbf9f 7133
3e4093b6
RS
7134/* Macros to save and restore the spelling stack around push_... functions.
7135 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 7136
3e4093b6
RS
7137#define SPELLING_DEPTH() (spelling - spelling_base)
7138#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 7139
3e4093b6
RS
7140/* Push an element on the spelling stack with type KIND and assign VALUE
7141 to MEMBER. */
400fbf9f 7142
3e4093b6
RS
7143#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7144{ \
7145 int depth = SPELLING_DEPTH (); \
7146 \
7147 if (depth >= spelling_size) \
7148 { \
7149 spelling_size += 10; \
cca8ead2
BI
7150 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7151 spelling_size); \
3e4093b6
RS
7152 RESTORE_SPELLING_DEPTH (depth); \
7153 } \
7154 \
7155 spelling->kind = (KIND); \
7156 spelling->MEMBER = (VALUE); \
7157 spelling++; \
7158}
400fbf9f 7159
3e4093b6 7160/* Push STRING on the stack. Printed literally. */
400fbf9f 7161
3e4093b6
RS
7162static void
7163push_string (const char *string)
7164{
7165 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7166}
400fbf9f 7167
3e4093b6 7168/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 7169
3e4093b6
RS
7170static void
7171push_member_name (tree decl)
7172{
7173 const char *const string
88388a52
JM
7174 = (DECL_NAME (decl)
7175 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7176 : _("<anonymous>"));
3e4093b6
RS
7177 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7178}
400fbf9f 7179
3e4093b6 7180/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 7181
3e4093b6 7182static void
a0f0ab9f 7183push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
7184{
7185 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7186}
bb58bec5 7187
3e4093b6 7188/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 7189
3e4093b6
RS
7190static int
7191spelling_length (void)
7192{
7193 int size = 0;
7194 struct spelling *p;
400fbf9f 7195
3e4093b6
RS
7196 for (p = spelling_base; p < spelling; p++)
7197 {
7198 if (p->kind == SPELLING_BOUNDS)
7199 size += 25;
7200 else
7201 size += strlen (p->u.s) + 1;
7202 }
7203
7204 return size;
400fbf9f 7205}
400fbf9f 7206
3e4093b6 7207/* Print the spelling to BUFFER and return it. */
400fbf9f 7208
3e4093b6
RS
7209static char *
7210print_spelling (char *buffer)
400fbf9f 7211{
3e4093b6
RS
7212 char *d = buffer;
7213 struct spelling *p;
400fbf9f 7214
3e4093b6
RS
7215 for (p = spelling_base; p < spelling; p++)
7216 if (p->kind == SPELLING_BOUNDS)
7217 {
a0f0ab9f 7218 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
7219 d += strlen (d);
7220 }
7221 else
7222 {
7223 const char *s;
7224 if (p->kind == SPELLING_MEMBER)
7225 *d++ = '.';
7226 for (s = p->u.s; (*d = *s++); d++)
7227 ;
7228 }
7229 *d++ = '\0';
7230 return buffer;
7231}
400fbf9f 7232
3e4093b6
RS
7233/* Digest the parser output INIT as an initializer for type TYPE.
7234 Return a C expression of type TYPE to represent the initial value.
7e842ef8 7235
bbbbb16a
ILT
7236 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7237
928c19bb
JM
7238 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7239
916c5919
JM
7240 If INIT is a string constant, STRICT_STRING is true if it is
7241 unparenthesized or we should not warn here for it being parenthesized.
7242 For other types of INIT, STRICT_STRING is not used.
7243
c2255bc4
AH
7244 INIT_LOC is the location of the INIT.
7245
3e4093b6
RS
7246 REQUIRE_CONSTANT requests an error if non-constant initializers or
7247 elements are seen. */
7e842ef8 7248
3e4093b6 7249static tree
c2255bc4
AH
7250digest_init (location_t init_loc, tree type, tree init, tree origtype,
7251 bool null_pointer_constant, bool strict_string,
7252 int require_constant)
3e4093b6
RS
7253{
7254 enum tree_code code = TREE_CODE (type);
7255 tree inside_init = init;
8ce94e44 7256 tree semantic_type = NULL_TREE;
928c19bb 7257 bool maybe_const = true;
7e842ef8 7258
3e4093b6 7259 if (type == error_mark_node
f01da1a5 7260 || !init
7a0ca710 7261 || error_operand_p (init))
3e4093b6 7262 return error_mark_node;
7e842ef8 7263
ed248cf7 7264 STRIP_TYPE_NOPS (inside_init);
7e842ef8 7265
8ce94e44
JM
7266 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7267 {
7268 semantic_type = TREE_TYPE (inside_init);
7269 inside_init = TREE_OPERAND (inside_init, 0);
7270 }
928c19bb 7271 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7e842ef8 7272
3e4093b6
RS
7273 /* Initialization of an array of chars from a string constant
7274 optionally enclosed in braces. */
7e842ef8 7275
197463ae
JM
7276 if (code == ARRAY_TYPE && inside_init
7277 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 7278 {
267bac10
JM
7279 tree typ1
7280 = (TYPE_ATOMIC (TREE_TYPE (type))
7281 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7282 TYPE_QUAL_ATOMIC)
7283 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
7284 /* Note that an array could be both an array of character type
7285 and an array of wchar_t if wchar_t is signed char or unsigned
7286 char. */
7287 bool char_array = (typ1 == char_type_node
7288 || typ1 == signed_char_type_node
7289 || typ1 == unsigned_char_type_node);
7290 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
7291 bool char16_array = !!comptypes (typ1, char16_type_node);
7292 bool char32_array = !!comptypes (typ1, char32_type_node);
7293
7294 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 7295 {
916c5919 7296 struct c_expr expr;
c466b2cd 7297 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
7298 expr.value = inside_init;
7299 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 7300 expr.original_type = NULL;
d033409e 7301 maybe_warn_string_init (init_loc, type, expr);
916c5919 7302
a45e580b 7303 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 7304 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
7305 "initialization of a flexible array member");
7306
3e4093b6 7307 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7308 TYPE_MAIN_VARIANT (type)))
3e4093b6 7309 return inside_init;
7e842ef8 7310
c466b2cd 7311 if (char_array)
3e4093b6 7312 {
c466b2cd
KVH
7313 if (typ2 != char_type_node)
7314 {
ea58ef42
MP
7315 error_init (init_loc, "char-array initialized from wide "
7316 "string");
c466b2cd
KVH
7317 return error_mark_node;
7318 }
3e4093b6 7319 }
c466b2cd 7320 else
3e4093b6 7321 {
c466b2cd
KVH
7322 if (typ2 == char_type_node)
7323 {
ea58ef42
MP
7324 error_init (init_loc, "wide character array initialized "
7325 "from non-wide string");
c466b2cd
KVH
7326 return error_mark_node;
7327 }
7328 else if (!comptypes(typ1, typ2))
7329 {
ea58ef42
MP
7330 error_init (init_loc, "wide character array initialized "
7331 "from incompatible wide string");
c466b2cd
KVH
7332 return error_mark_node;
7333 }
7e842ef8 7334 }
2f6e4e97 7335
3e4093b6 7336 TREE_TYPE (inside_init) = type;
3fa8871b
MP
7337 if (TYPE_DOMAIN (type) != NULL_TREE
7338 && TYPE_SIZE (type) != NULL_TREE
5eb4df45
ILT
7339 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7340 {
7341 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7342
c466b2cd 7343 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
7344 because it's ok to ignore the terminating null char
7345 that is counted in the length of the constant. */
5eb4df45
ILT
7346 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7347 (len
7348 - (TYPE_PRECISION (typ1)
7349 / BITS_PER_UNIT))))
7350 pedwarn_init (init_loc, 0,
7351 ("initializer-string for array of chars "
7352 "is too long"));
7353 else if (warn_cxx_compat
7354 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7355 warning_at (init_loc, OPT_Wc___compat,
7356 ("initializer-string for array chars "
7357 "is too long for C++"));
7358 }
7e842ef8 7359
3e4093b6 7360 return inside_init;
7e842ef8 7361 }
197463ae
JM
7362 else if (INTEGRAL_TYPE_P (typ1))
7363 {
ea58ef42 7364 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
7365 "from string constant");
7366 return error_mark_node;
7367 }
7e842ef8
PE
7368 }
7369
3e4093b6
RS
7370 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7371 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7372 below and handle as a constructor. */
e89be13b 7373 if (code == VECTOR_TYPE
31521951 7374 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 7375 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
7376 && TREE_CONSTANT (inside_init))
7377 {
7378 if (TREE_CODE (inside_init) == VECTOR_CST
7379 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7380 TYPE_MAIN_VARIANT (type)))
7381 return inside_init;
7382
7383 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7384 {
4038c495
GB
7385 unsigned HOST_WIDE_INT ix;
7386 tree value;
7387 bool constant_p = true;
e89be13b
JJ
7388
7389 /* Iterate through elements and check if all constructor
7390 elements are *_CSTs. */
4038c495
GB
7391 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7392 if (!CONSTANT_CLASS_P (value))
7393 {
7394 constant_p = false;
7395 break;
7396 }
e89be13b 7397
4038c495
GB
7398 if (constant_p)
7399 return build_vector_from_ctor (type,
7400 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7401 }
7402 }
6035d635 7403
ca085fd7
MLI
7404 if (warn_sequence_point)
7405 verify_sequence_points (inside_init);
7406
3e4093b6
RS
7407 /* Any type can be initialized
7408 from an expression of the same type, optionally with braces. */
400fbf9f 7409
3fa8871b 7410 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
3e4093b6 7411 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7412 TYPE_MAIN_VARIANT (type))
3e4093b6 7413 || (code == ARRAY_TYPE
132da1a5 7414 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7415 || (code == VECTOR_TYPE
132da1a5 7416 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7417 || (code == POINTER_TYPE
3897f229 7418 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7419 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7420 TREE_TYPE (type)))))
3e4093b6
RS
7421 {
7422 if (code == POINTER_TYPE)
b494fd98 7423 {
b494fd98
EB
7424 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7425 {
f2a71bbc
JM
7426 if (TREE_CODE (inside_init) == STRING_CST
7427 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7428 inside_init = array_to_pointer_conversion
7429 (init_loc, inside_init);
f2a71bbc
JM
7430 else
7431 {
ea58ef42 7432 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7433 return error_mark_node;
7434 }
b494fd98 7435 }
f2a71bbc 7436 }
b494fd98 7437
bae39a73
NS
7438 if (code == VECTOR_TYPE)
7439 /* Although the types are compatible, we may require a
7440 conversion. */
7441 inside_init = convert (type, inside_init);
3e4093b6 7442
ca58211b 7443 if (require_constant
3e4093b6 7444 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7445 {
3e4093b6
RS
7446 /* As an extension, allow initializing objects with static storage
7447 duration with compound literals (which are then treated just as
ca58211b
PB
7448 the brace enclosed list they contain). Also allow this for
7449 vectors, as we can only assign them with compound literals. */
7278465e
MP
7450 if (flag_isoc99 && code != VECTOR_TYPE)
7451 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7452 "is not constant");
3e4093b6
RS
7453 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7454 inside_init = DECL_INITIAL (decl);
400fbf9f 7455 }
3e4093b6
RS
7456
7457 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7458 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7459 {
ea58ef42
MP
7460 error_init (init_loc, "array initialized from non-constant array "
7461 "expression");
3e4093b6 7462 return error_mark_node;
400fbf9f 7463 }
400fbf9f 7464
c1771a20 7465 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7466 -pedantic-errors is specified. In the later case, we always want
7467 an error. In the former case, we simply want a warning. */
7468 if (require_constant && pedantic
7469 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7470 {
7471 inside_init
7472 = valid_compound_expr_initializer (inside_init,
7473 TREE_TYPE (inside_init));
7474 if (inside_init == error_mark_node)
ea58ef42 7475 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7476 else
c1771a20 7477 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7478 "initializer element is not constant");
3e4093b6
RS
7479 if (flag_pedantic_errors)
7480 inside_init = error_mark_node;
7481 }
7482 else if (require_constant
116df786
RH
7483 && !initializer_constant_valid_p (inside_init,
7484 TREE_TYPE (inside_init)))
3e4093b6 7485 {
ea58ef42 7486 error_init (init_loc, "initializer element is not constant");
3e4093b6 7487 inside_init = error_mark_node;
8b40563c 7488 }
928c19bb 7489 else if (require_constant && !maybe_const)
3aa3c9fc 7490 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7491 "initializer element is not a constant expression");
f735a153 7492
90137d8f 7493 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7494 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7495 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7496 type, inside_init, origtype,
bbbbb16a 7497 ic_init, null_pointer_constant,
928c19bb 7498 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7499 return inside_init;
7500 }
f735a153 7501
3e4093b6 7502 /* Handle scalar types, including conversions. */
400fbf9f 7503
ab22c1fa
CF
7504 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7505 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7506 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7507 {
f2a71bbc
JM
7508 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7509 && (TREE_CODE (init) == STRING_CST
7510 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7511 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7512 if (semantic_type)
7513 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7514 inside_init);
3e4093b6 7515 inside_init
68fca595
MP
7516 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7517 inside_init, origtype, ic_init,
7518 null_pointer_constant, NULL_TREE, NULL_TREE,
7519 0);
2f6e4e97 7520
3274deff
JW
7521 /* Check to see if we have already given an error message. */
7522 if (inside_init == error_mark_node)
7523 ;
3f75a254 7524 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7525 {
ea58ef42 7526 error_init (init_loc, "initializer element is not constant");
3e4093b6 7527 inside_init = error_mark_node;
400fbf9f 7528 }
3e4093b6 7529 else if (require_constant
116df786
RH
7530 && !initializer_constant_valid_p (inside_init,
7531 TREE_TYPE (inside_init)))
400fbf9f 7532 {
ea58ef42
MP
7533 error_init (init_loc, "initializer element is not computable at "
7534 "load time");
3e4093b6 7535 inside_init = error_mark_node;
400fbf9f 7536 }
928c19bb 7537 else if (require_constant && !maybe_const)
451b5e48 7538 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7539 "initializer element is not a constant expression");
3e4093b6
RS
7540
7541 return inside_init;
400fbf9f 7542 }
d9fc6069 7543
3e4093b6 7544 /* Come here only for records and arrays. */
d9fc6069 7545
3e4093b6 7546 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7547 {
ea58ef42 7548 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7549 return error_mark_node;
d9fc6069 7550 }
3e4093b6 7551
ea58ef42 7552 error_init (init_loc, "invalid initializer");
3e4093b6 7553 return error_mark_node;
d9fc6069 7554}
400fbf9f 7555\f
3e4093b6 7556/* Handle initializers that use braces. */
400fbf9f 7557
3e4093b6
RS
7558/* Type of object we are accumulating a constructor for.
7559 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7560static tree constructor_type;
400fbf9f 7561
3e4093b6
RS
7562/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7563 left to fill. */
7564static tree constructor_fields;
400fbf9f 7565
3e4093b6
RS
7566/* For an ARRAY_TYPE, this is the specified index
7567 at which to store the next element we get. */
7568static tree constructor_index;
400fbf9f 7569
3e4093b6
RS
7570/* For an ARRAY_TYPE, this is the maximum index. */
7571static tree constructor_max_index;
400fbf9f 7572
3e4093b6
RS
7573/* For a RECORD_TYPE, this is the first field not yet written out. */
7574static tree constructor_unfilled_fields;
400fbf9f 7575
3e4093b6
RS
7576/* For an ARRAY_TYPE, this is the index of the first element
7577 not yet written out. */
7578static tree constructor_unfilled_index;
895ea614 7579
3e4093b6
RS
7580/* In a RECORD_TYPE, the byte index of the next consecutive field.
7581 This is so we can generate gaps between fields, when appropriate. */
7582static tree constructor_bit_index;
10d5caec 7583
3e4093b6
RS
7584/* If we are saving up the elements rather than allocating them,
7585 this is the list of elements so far (in reverse order,
7586 most recent first). */
9771b263 7587static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7588
3e4093b6
RS
7589/* 1 if constructor should be incrementally stored into a constructor chain,
7590 0 if all the elements should be kept in AVL tree. */
7591static int constructor_incremental;
ad47f1e5 7592
3e4093b6
RS
7593/* 1 if so far this constructor's elements are all compile-time constants. */
7594static int constructor_constant;
ad47f1e5 7595
3e4093b6
RS
7596/* 1 if so far this constructor's elements are all valid address constants. */
7597static int constructor_simple;
ad47f1e5 7598
928c19bb
JM
7599/* 1 if this constructor has an element that cannot be part of a
7600 constant expression. */
7601static int constructor_nonconst;
7602
3e4093b6
RS
7603/* 1 if this constructor is erroneous so far. */
7604static int constructor_erroneous;
d45cf215 7605
9bac5cbb
G
7606/* 1 if this constructor is the universal zero initializer { 0 }. */
7607static int constructor_zeroinit;
7608
3e4093b6
RS
7609/* Structure for managing pending initializer elements, organized as an
7610 AVL tree. */
d45cf215 7611
3e4093b6 7612struct init_node
d45cf215 7613{
3e4093b6
RS
7614 struct init_node *left, *right;
7615 struct init_node *parent;
7616 int balance;
7617 tree purpose;
7618 tree value;
bbbbb16a 7619 tree origtype;
d45cf215
RS
7620};
7621
3e4093b6
RS
7622/* Tree of pending elements at this constructor level.
7623 These are elements encountered out of order
7624 which belong at places we haven't reached yet in actually
7625 writing the output.
7626 Will never hold tree nodes across GC runs. */
7627static struct init_node *constructor_pending_elts;
d45cf215 7628
3e4093b6
RS
7629/* The SPELLING_DEPTH of this constructor. */
7630static int constructor_depth;
d45cf215 7631
3e4093b6
RS
7632/* DECL node for which an initializer is being read.
7633 0 means we are reading a constructor expression
7634 such as (struct foo) {...}. */
7635static tree constructor_decl;
d45cf215 7636
3e4093b6
RS
7637/* Nonzero if this is an initializer for a top-level decl. */
7638static int constructor_top_level;
d45cf215 7639
3e4093b6
RS
7640/* Nonzero if there were any member designators in this initializer. */
7641static int constructor_designated;
d45cf215 7642
3e4093b6
RS
7643/* Nesting depth of designator list. */
7644static int designator_depth;
d45cf215 7645
3e4093b6 7646/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7647static int designator_erroneous;
d45cf215 7648
3e4093b6
RS
7649\f
7650/* This stack has a level for each implicit or explicit level of
7651 structuring in the initializer, including the outermost one. It
7652 saves the values of most of the variables above. */
d45cf215 7653
3e4093b6
RS
7654struct constructor_range_stack;
7655
7656struct constructor_stack
d45cf215 7657{
3e4093b6
RS
7658 struct constructor_stack *next;
7659 tree type;
7660 tree fields;
7661 tree index;
7662 tree max_index;
7663 tree unfilled_index;
7664 tree unfilled_fields;
7665 tree bit_index;
9771b263 7666 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7667 struct init_node *pending_elts;
7668 int offset;
7669 int depth;
916c5919 7670 /* If value nonzero, this value should replace the entire
3e4093b6 7671 constructor at this level. */
916c5919 7672 struct c_expr replacement_value;
3e4093b6
RS
7673 struct constructor_range_stack *range_stack;
7674 char constant;
7675 char simple;
928c19bb 7676 char nonconst;
3e4093b6
RS
7677 char implicit;
7678 char erroneous;
7679 char outer;
7680 char incremental;
7681 char designated;
976d5a22 7682 int designator_depth;
3e4093b6 7683};
d45cf215 7684
802415d1 7685static struct constructor_stack *constructor_stack;
d45cf215 7686
3e4093b6
RS
7687/* This stack represents designators from some range designator up to
7688 the last designator in the list. */
d45cf215 7689
3e4093b6
RS
7690struct constructor_range_stack
7691{
7692 struct constructor_range_stack *next, *prev;
7693 struct constructor_stack *stack;
7694 tree range_start;
7695 tree index;
7696 tree range_end;
7697 tree fields;
7698};
d45cf215 7699
802415d1 7700static struct constructor_range_stack *constructor_range_stack;
d45cf215 7701
3e4093b6
RS
7702/* This stack records separate initializers that are nested.
7703 Nested initializers can't happen in ANSI C, but GNU C allows them
7704 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7705
3e4093b6 7706struct initializer_stack
d45cf215 7707{
3e4093b6
RS
7708 struct initializer_stack *next;
7709 tree decl;
3e4093b6
RS
7710 struct constructor_stack *constructor_stack;
7711 struct constructor_range_stack *constructor_range_stack;
9771b263 7712 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7713 struct spelling *spelling;
7714 struct spelling *spelling_base;
7715 int spelling_size;
7716 char top_level;
7717 char require_constant_value;
7718 char require_constant_elements;
5dd9a9d0 7719 rich_location *missing_brace_richloc;
3e4093b6 7720};
d45cf215 7721
802415d1 7722static struct initializer_stack *initializer_stack;
3e4093b6
RS
7723\f
7724/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7725
7726void
5dd9a9d0
DM
7727start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7728 rich_location *richloc)
400fbf9f 7729{
3e4093b6 7730 const char *locus;
cceb1885 7731 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7732
3e4093b6 7733 p->decl = constructor_decl;
3e4093b6
RS
7734 p->require_constant_value = require_constant_value;
7735 p->require_constant_elements = require_constant_elements;
7736 p->constructor_stack = constructor_stack;
7737 p->constructor_range_stack = constructor_range_stack;
7738 p->elements = constructor_elements;
7739 p->spelling = spelling;
7740 p->spelling_base = spelling_base;
7741 p->spelling_size = spelling_size;
7742 p->top_level = constructor_top_level;
7743 p->next = initializer_stack;
5dd9a9d0 7744 p->missing_brace_richloc = richloc;
3e4093b6 7745 initializer_stack = p;
400fbf9f 7746
3e4093b6 7747 constructor_decl = decl;
3e4093b6
RS
7748 constructor_designated = 0;
7749 constructor_top_level = top_level;
400fbf9f 7750
3fa8871b 7751 if (decl != NULL_TREE && decl != error_mark_node)
3e4093b6
RS
7752 {
7753 require_constant_value = TREE_STATIC (decl);
7754 require_constant_elements
7755 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7756 /* For a scalar, you can always use any value to initialize,
7757 even within braces. */
296a8c2f 7758 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7759 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7760 }
7761 else
7762 {
7763 require_constant_value = 0;
7764 require_constant_elements = 0;
88388a52 7765 locus = _("(anonymous)");
3e4093b6 7766 }
b71c7f8a 7767
3e4093b6
RS
7768 constructor_stack = 0;
7769 constructor_range_stack = 0;
b71c7f8a 7770
9bac5cbb 7771 found_missing_braces = 0;
3e4093b6
RS
7772
7773 spelling_base = 0;
7774 spelling_size = 0;
7775 RESTORE_SPELLING_DEPTH (0);
7776
7777 if (locus)
7778 push_string (locus);
7779}
7780
7781void
7782finish_init (void)
b71c7f8a 7783{
3e4093b6 7784 struct initializer_stack *p = initializer_stack;
b71c7f8a 7785
3e4093b6
RS
7786 /* Free the whole constructor stack of this initializer. */
7787 while (constructor_stack)
7788 {
7789 struct constructor_stack *q = constructor_stack;
7790 constructor_stack = q->next;
7791 free (q);
7792 }
7793
366de0ce 7794 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7795
7796 /* Pop back to the data of the outer initializer (if any). */
36579663 7797 free (spelling_base);
3aeb3655 7798
3e4093b6 7799 constructor_decl = p->decl;
3e4093b6
RS
7800 require_constant_value = p->require_constant_value;
7801 require_constant_elements = p->require_constant_elements;
7802 constructor_stack = p->constructor_stack;
7803 constructor_range_stack = p->constructor_range_stack;
7804 constructor_elements = p->elements;
7805 spelling = p->spelling;
7806 spelling_base = p->spelling_base;
7807 spelling_size = p->spelling_size;
7808 constructor_top_level = p->top_level;
7809 initializer_stack = p->next;
7810 free (p);
b71c7f8a 7811}
400fbf9f 7812\f
3e4093b6
RS
7813/* Call here when we see the initializer is surrounded by braces.
7814 This is instead of a call to push_init_level;
7815 it is matched by a call to pop_init_level.
400fbf9f 7816
3e4093b6
RS
7817 TYPE is the type to initialize, for a constructor expression.
7818 For an initializer for a decl, TYPE is zero. */
400fbf9f 7819
3e4093b6
RS
7820void
7821really_start_incremental_init (tree type)
400fbf9f 7822{
5d038c4c 7823 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7824
3fa8871b 7825 if (type == NULL_TREE)
3e4093b6 7826 type = TREE_TYPE (constructor_decl);
400fbf9f 7827
31521951 7828 if (VECTOR_TYPE_P (type)
b6fc2cdb 7829 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7830 error ("opaque vector types cannot be initialized");
400fbf9f 7831
3e4093b6
RS
7832 p->type = constructor_type;
7833 p->fields = constructor_fields;
7834 p->index = constructor_index;
7835 p->max_index = constructor_max_index;
7836 p->unfilled_index = constructor_unfilled_index;
7837 p->unfilled_fields = constructor_unfilled_fields;
7838 p->bit_index = constructor_bit_index;
7839 p->elements = constructor_elements;
7840 p->constant = constructor_constant;
7841 p->simple = constructor_simple;
928c19bb 7842 p->nonconst = constructor_nonconst;
3e4093b6
RS
7843 p->erroneous = constructor_erroneous;
7844 p->pending_elts = constructor_pending_elts;
7845 p->depth = constructor_depth;
916c5919
JM
7846 p->replacement_value.value = 0;
7847 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7848 p->replacement_value.original_type = NULL;
3e4093b6
RS
7849 p->implicit = 0;
7850 p->range_stack = 0;
7851 p->outer = 0;
7852 p->incremental = constructor_incremental;
7853 p->designated = constructor_designated;
976d5a22 7854 p->designator_depth = designator_depth;
3e4093b6
RS
7855 p->next = 0;
7856 constructor_stack = p;
b13aca19 7857
3e4093b6
RS
7858 constructor_constant = 1;
7859 constructor_simple = 1;
928c19bb 7860 constructor_nonconst = 0;
3e4093b6 7861 constructor_depth = SPELLING_DEPTH ();
9771b263 7862 constructor_elements = NULL;
3e4093b6
RS
7863 constructor_pending_elts = 0;
7864 constructor_type = type;
7865 constructor_incremental = 1;
7866 constructor_designated = 0;
9bac5cbb 7867 constructor_zeroinit = 1;
3e4093b6 7868 designator_depth = 0;
b06df647 7869 designator_erroneous = 0;
400fbf9f 7870
3e636daf 7871 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7872 {
3e4093b6
RS
7873 constructor_fields = TYPE_FIELDS (constructor_type);
7874 /* Skip any nameless bit fields at the beginning. */
3fa8871b
MP
7875 while (constructor_fields != NULL_TREE
7876 && DECL_C_BIT_FIELD (constructor_fields)
7877 && DECL_NAME (constructor_fields) == NULL_TREE)
910ad8de 7878 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7879
3e4093b6
RS
7880 constructor_unfilled_fields = constructor_fields;
7881 constructor_bit_index = bitsize_zero_node;
400fbf9f 7882 }
3e4093b6
RS
7883 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7884 {
7885 if (TYPE_DOMAIN (constructor_type))
7886 {
7887 constructor_max_index
7888 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7889
3e4093b6
RS
7890 /* Detect non-empty initializations of zero-length arrays. */
7891 if (constructor_max_index == NULL_TREE
7892 && TYPE_SIZE (constructor_type))
9a9d280e 7893 constructor_max_index = integer_minus_one_node;
400fbf9f 7894
3e4093b6
RS
7895 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7896 to initialize VLAs will cause a proper error; avoid tree
7897 checking errors as well by setting a safe value. */
7898 if (constructor_max_index
7899 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7900 constructor_max_index = integer_minus_one_node;
59c83dbf 7901
3e4093b6
RS
7902 constructor_index
7903 = convert (bitsizetype,
7904 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7905 }
3e4093b6 7906 else
493179da
JM
7907 {
7908 constructor_index = bitsize_zero_node;
7909 constructor_max_index = NULL_TREE;
7910 }
59c83dbf 7911
3e4093b6
RS
7912 constructor_unfilled_index = constructor_index;
7913 }
31521951 7914 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7915 {
7916 /* Vectors are like simple fixed-size arrays. */
7917 constructor_max_index =
c62c040f 7918 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7919 constructor_index = bitsize_zero_node;
3e4093b6
RS
7920 constructor_unfilled_index = constructor_index;
7921 }
7922 else
7923 {
7924 /* Handle the case of int x = {5}; */
7925 constructor_fields = constructor_type;
7926 constructor_unfilled_fields = constructor_type;
7927 }
7928}
7929\f
5dd9a9d0
DM
7930extern location_t last_init_list_comma;
7931
16595a1f
BS
7932/* Called when we see an open brace for a nested initializer. Finish
7933 off any pending levels with implicit braces. */
7934void
7935finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7936{
7937 while (constructor_stack->implicit)
7938 {
7939 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3fa8871b 7940 && constructor_fields == NULL_TREE)
16595a1f 7941 process_init_element (input_location,
5dd9a9d0
DM
7942 pop_init_level (loc, 1, braced_init_obstack,
7943 last_init_list_comma),
16595a1f
BS
7944 true, braced_init_obstack);
7945 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7946 && constructor_max_index
7947 && tree_int_cst_lt (constructor_max_index,
7948 constructor_index))
7949 process_init_element (input_location,
5dd9a9d0
DM
7950 pop_init_level (loc, 1, braced_init_obstack,
7951 last_init_list_comma),
16595a1f
BS
7952 true, braced_init_obstack);
7953 else
7954 break;
7955 }
7956}
7957
3e4093b6
RS
7958/* Push down into a subobject, for initialization.
7959 If this is for an explicit set of braces, IMPLICIT is 0.
7960 If it is because the next element belongs at a lower level,
7961 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7962
3e4093b6 7963void
ea58ef42
MP
7964push_init_level (location_t loc, int implicit,
7965 struct obstack *braced_init_obstack)
3e4093b6
RS
7966{
7967 struct constructor_stack *p;
7968 tree value = NULL_TREE;
400fbf9f 7969
3e4093b6
RS
7970 /* Unless this is an explicit brace, we need to preserve previous
7971 content if any. */
7972 if (implicit)
7973 {
3e636daf 7974 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7975 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7976 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7977 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7978 }
7979
5d038c4c 7980 p = XNEW (struct constructor_stack);
3e4093b6
RS
7981 p->type = constructor_type;
7982 p->fields = constructor_fields;
7983 p->index = constructor_index;
7984 p->max_index = constructor_max_index;
7985 p->unfilled_index = constructor_unfilled_index;
7986 p->unfilled_fields = constructor_unfilled_fields;
7987 p->bit_index = constructor_bit_index;
7988 p->elements = constructor_elements;
7989 p->constant = constructor_constant;
7990 p->simple = constructor_simple;
928c19bb 7991 p->nonconst = constructor_nonconst;
3e4093b6
RS
7992 p->erroneous = constructor_erroneous;
7993 p->pending_elts = constructor_pending_elts;
7994 p->depth = constructor_depth;
3fa8871b 7995 p->replacement_value.value = NULL_TREE;
916c5919 7996 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7997 p->replacement_value.original_type = NULL;
3e4093b6
RS
7998 p->implicit = implicit;
7999 p->outer = 0;
8000 p->incremental = constructor_incremental;
8001 p->designated = constructor_designated;
976d5a22 8002 p->designator_depth = designator_depth;
3e4093b6
RS
8003 p->next = constructor_stack;
8004 p->range_stack = 0;
8005 constructor_stack = p;
400fbf9f 8006
3e4093b6
RS
8007 constructor_constant = 1;
8008 constructor_simple = 1;
928c19bb 8009 constructor_nonconst = 0;
3e4093b6 8010 constructor_depth = SPELLING_DEPTH ();
9771b263 8011 constructor_elements = NULL;
3e4093b6
RS
8012 constructor_incremental = 1;
8013 constructor_designated = 0;
8014 constructor_pending_elts = 0;
8015 if (!implicit)
400fbf9f 8016 {
3e4093b6
RS
8017 p->range_stack = constructor_range_stack;
8018 constructor_range_stack = 0;
8019 designator_depth = 0;
b06df647 8020 designator_erroneous = 0;
3e4093b6 8021 }
400fbf9f 8022
3e4093b6
RS
8023 /* Don't die if an entire brace-pair level is superfluous
8024 in the containing level. */
3fa8871b 8025 if (constructor_type == NULL_TREE)
3e4093b6 8026 ;
3e636daf 8027 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8028 {
8029 /* Don't die if there are extra init elts at the end. */
3fa8871b
MP
8030 if (constructor_fields == NULL_TREE)
8031 constructor_type = NULL_TREE;
3e4093b6 8032 else
400fbf9f 8033 {
3e4093b6
RS
8034 constructor_type = TREE_TYPE (constructor_fields);
8035 push_member_name (constructor_fields);
8036 constructor_depth++;
400fbf9f 8037 }
6a358dcb
MP
8038 /* If upper initializer is designated, then mark this as
8039 designated too to prevent bogus warnings. */
8040 constructor_designated = p->designated;
3e4093b6
RS
8041 }
8042 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8043 {
8044 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 8045 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 8046 constructor_depth++;
400fbf9f
JW
8047 }
8048
3fa8871b 8049 if (constructor_type == NULL_TREE)
400fbf9f 8050 {
ea58ef42 8051 error_init (loc, "extra brace group at end of initializer");
3fa8871b
MP
8052 constructor_fields = NULL_TREE;
8053 constructor_unfilled_fields = NULL_TREE;
3e4093b6 8054 return;
400fbf9f
JW
8055 }
8056
3e4093b6
RS
8057 if (value && TREE_CODE (value) == CONSTRUCTOR)
8058 {
8059 constructor_constant = TREE_CONSTANT (value);
8060 constructor_simple = TREE_STATIC (value);
928c19bb 8061 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 8062 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 8063 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
8064 && (TREE_CODE (constructor_type) == RECORD_TYPE
8065 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 8066 set_nonincremental_init (braced_init_obstack);
3e4093b6 8067 }
400fbf9f 8068
9bac5cbb 8069 if (implicit == 1)
5dd9a9d0
DM
8070 {
8071 found_missing_braces = 1;
8072 if (initializer_stack->missing_brace_richloc)
8073 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8074 (loc, "{");
8075 }
400fbf9f 8076
3e636daf 8077 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8078 {
8079 constructor_fields = TYPE_FIELDS (constructor_type);
8080 /* Skip any nameless bit fields at the beginning. */
3fa8871b
MP
8081 while (constructor_fields != NULL_TREE
8082 && DECL_C_BIT_FIELD (constructor_fields)
8083 && DECL_NAME (constructor_fields) == NULL_TREE)
f7587ed0 8084 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 8085
3e4093b6
RS
8086 constructor_unfilled_fields = constructor_fields;
8087 constructor_bit_index = bitsize_zero_node;
8088 }
31521951 8089 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8090 {
8091 /* Vectors are like simple fixed-size arrays. */
8092 constructor_max_index =
c62c040f
RG
8093 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8094 constructor_index = bitsize_int (0);
3e4093b6
RS
8095 constructor_unfilled_index = constructor_index;
8096 }
8097 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8098 {
8099 if (TYPE_DOMAIN (constructor_type))
8100 {
8101 constructor_max_index
8102 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 8103
3e4093b6
RS
8104 /* Detect non-empty initializations of zero-length arrays. */
8105 if (constructor_max_index == NULL_TREE
8106 && TYPE_SIZE (constructor_type))
9a9d280e 8107 constructor_max_index = integer_minus_one_node;
de520661 8108
3e4093b6
RS
8109 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8110 to initialize VLAs will cause a proper error; avoid tree
8111 checking errors as well by setting a safe value. */
8112 if (constructor_max_index
8113 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 8114 constructor_max_index = integer_minus_one_node;
b62acd60 8115
3e4093b6
RS
8116 constructor_index
8117 = convert (bitsizetype,
8118 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8119 }
8120 else
8121 constructor_index = bitsize_zero_node;
de520661 8122
3e4093b6
RS
8123 constructor_unfilled_index = constructor_index;
8124 if (value && TREE_CODE (value) == STRING_CST)
8125 {
8126 /* We need to split the char/wchar array into individual
8127 characters, so that we don't have to special case it
8128 everywhere. */
a1e3b3d9 8129 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
8130 }
8131 }
8132 else
8133 {
b4519d39 8134 if (constructor_type != error_mark_node)
96b40f8d 8135 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
8136 constructor_fields = constructor_type;
8137 constructor_unfilled_fields = constructor_type;
8138 }
8139}
8b6a5902 8140
3e4093b6 8141/* At the end of an implicit or explicit brace level,
916c5919
JM
8142 finish up that level of constructor. If a single expression
8143 with redundant braces initialized that level, return the
8144 c_expr structure for that expression. Otherwise, the original_code
8145 element is set to ERROR_MARK.
8146 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 8147 from inner levels (process_init_element ignores that),
916c5919 8148 but return error_mark_node as the value from the outermost level
3e4093b6 8149 (that's what we want to put in DECL_INITIAL).
916c5919 8150 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 8151
916c5919 8152struct c_expr
ea58ef42 8153pop_init_level (location_t loc, int implicit,
5dd9a9d0
DM
8154 struct obstack *braced_init_obstack,
8155 location_t insert_before)
3e4093b6
RS
8156{
8157 struct constructor_stack *p;
916c5919 8158 struct c_expr ret;
3fa8871b 8159 ret.value = NULL_TREE;
916c5919 8160 ret.original_code = ERROR_MARK;
6866c6e8 8161 ret.original_type = NULL;
de520661 8162
3e4093b6
RS
8163 if (implicit == 0)
8164 {
8165 /* When we come to an explicit close brace,
8166 pop any inner levels that didn't have explicit braces. */
8167 while (constructor_stack->implicit)
34cf811f 8168 process_init_element (input_location,
5dd9a9d0
DM
8169 pop_init_level (loc, 1, braced_init_obstack,
8170 insert_before),
34cf811f 8171 true, braced_init_obstack);
366de0ce 8172 gcc_assert (!constructor_range_stack);
3e4093b6 8173 }
5dd9a9d0
DM
8174 else
8175 if (initializer_stack->missing_brace_richloc)
8176 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8177 (insert_before, "}");
e5e809f4 8178
0066ef9c
RH
8179 /* Now output all pending elements. */
8180 constructor_incremental = 1;
a1e3b3d9 8181 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 8182
3e4093b6 8183 p = constructor_stack;
e5e809f4 8184
3e4093b6
RS
8185 /* Error for initializing a flexible array member, or a zero-length
8186 array member in an inappropriate context. */
8187 if (constructor_type && constructor_fields
8188 && TREE_CODE (constructor_type) == ARRAY_TYPE
8189 && TYPE_DOMAIN (constructor_type)
3f75a254 8190 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
8191 {
8192 /* Silently discard empty initializations. The parser will
8193 already have pedwarned for empty brackets. */
8194 if (integer_zerop (constructor_unfilled_index))
8195 constructor_type = NULL_TREE;
366de0ce 8196 else
3e4093b6 8197 {
366de0ce 8198 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 8199
3e4093b6 8200 if (constructor_depth > 2)
ea58ef42 8201 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 8202 else
d033409e 8203 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 8204 "initialization of a flexible array member");
de520661 8205
3e4093b6
RS
8206 /* We have already issued an error message for the existence
8207 of a flexible array member not at the end of the structure.
535a42b1 8208 Discard the initializer so that we do not die later. */
910ad8de 8209 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
8210 constructor_type = NULL_TREE;
8211 }
3e4093b6 8212 }
de520661 8213
7b33f0c8
MP
8214 switch (vec_safe_length (constructor_elements))
8215 {
8216 case 0:
8217 /* Initialization with { } counts as zeroinit. */
8218 constructor_zeroinit = 1;
8219 break;
8220 case 1:
8221 /* This might be zeroinit as well. */
8222 if (integer_zerop ((*constructor_elements)[0].value))
8223 constructor_zeroinit = 1;
8224 break;
8225 default:
8226 /* If the constructor has more than one element, it can't be { 0 }. */
8227 constructor_zeroinit = 0;
8228 break;
8229 }
9bac5cbb
G
8230
8231 /* Warn when some structs are initialized with direct aggregation. */
8232 if (!implicit && found_missing_braces && warn_missing_braces
8233 && !constructor_zeroinit)
5dd9a9d0
DM
8234 {
8235 gcc_assert (initializer_stack->missing_brace_richloc);
64a5912c
DM
8236 warning_at (initializer_stack->missing_brace_richloc,
8237 OPT_Wmissing_braces,
8238 "missing braces around initializer");
5dd9a9d0 8239 }
9bac5cbb 8240
3e4093b6 8241 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 8242 if (warn_missing_field_initializers
3e4093b6
RS
8243 && constructor_type
8244 && TREE_CODE (constructor_type) == RECORD_TYPE
8245 && constructor_unfilled_fields)
8246 {
8247 /* Do not warn for flexible array members or zero-length arrays. */
8248 while (constructor_unfilled_fields
3f75a254 8249 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 8250 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 8251 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 8252
49819fef
AM
8253 if (constructor_unfilled_fields
8254 /* Do not warn if this level of the initializer uses member
8255 designators; it is likely to be deliberate. */
8256 && !constructor_designated
84937de2 8257 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 8258 && !constructor_zeroinit)
3e4093b6 8259 {
32397f22
MLI
8260 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8261 "missing initializer for field %qD of %qT",
8262 constructor_unfilled_fields,
8263 constructor_type))
8264 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 8265 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
8266 }
8267 }
de520661 8268
3e4093b6 8269 /* Pad out the end of the structure. */
916c5919 8270 if (p->replacement_value.value)
3e4093b6
RS
8271 /* If this closes a superfluous brace pair,
8272 just pass out the element between them. */
916c5919 8273 ret = p->replacement_value;
3fa8871b 8274 else if (constructor_type == NULL_TREE)
3e4093b6 8275 ;
3e636daf 8276 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8277 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 8278 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8279 {
8280 /* A nonincremental scalar initializer--just return
8281 the element, after verifying there is just one. */
9771b263 8282 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
8283 {
8284 if (!constructor_erroneous)
ea58ef42 8285 error_init (loc, "empty scalar initializer");
916c5919 8286 ret.value = error_mark_node;
3e4093b6 8287 }
9771b263 8288 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 8289 {
ea58ef42 8290 error_init (loc, "extra elements in scalar initializer");
9771b263 8291 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8292 }
8293 else
9771b263 8294 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8295 }
8296 else
8297 {
8298 if (constructor_erroneous)
916c5919 8299 ret.value = error_mark_node;
3e4093b6
RS
8300 else
8301 {
916c5919 8302 ret.value = build_constructor (constructor_type,
4038c495 8303 constructor_elements);
3e4093b6 8304 if (constructor_constant)
51eed280 8305 TREE_CONSTANT (ret.value) = 1;
3e4093b6 8306 if (constructor_constant && constructor_simple)
916c5919 8307 TREE_STATIC (ret.value) = 1;
928c19bb
JM
8308 if (constructor_nonconst)
8309 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
8310 }
8311 }
de520661 8312
928c19bb
JM
8313 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8314 {
8315 if (constructor_nonconst)
8316 ret.original_code = C_MAYBE_CONST_EXPR;
8317 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8318 ret.original_code = ERROR_MARK;
8319 }
8320
3e4093b6
RS
8321 constructor_type = p->type;
8322 constructor_fields = p->fields;
8323 constructor_index = p->index;
8324 constructor_max_index = p->max_index;
8325 constructor_unfilled_index = p->unfilled_index;
8326 constructor_unfilled_fields = p->unfilled_fields;
8327 constructor_bit_index = p->bit_index;
8328 constructor_elements = p->elements;
8329 constructor_constant = p->constant;
8330 constructor_simple = p->simple;
928c19bb 8331 constructor_nonconst = p->nonconst;
3e4093b6
RS
8332 constructor_erroneous = p->erroneous;
8333 constructor_incremental = p->incremental;
8334 constructor_designated = p->designated;
976d5a22 8335 designator_depth = p->designator_depth;
3e4093b6
RS
8336 constructor_pending_elts = p->pending_elts;
8337 constructor_depth = p->depth;
8338 if (!p->implicit)
8339 constructor_range_stack = p->range_stack;
8340 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 8341
3e4093b6
RS
8342 constructor_stack = p->next;
8343 free (p);
b621a4dd 8344
3fa8871b 8345 if (ret.value == NULL_TREE && constructor_stack == 0)
5d5e98dc 8346 ret.value = error_mark_node;
916c5919 8347 return ret;
3e4093b6 8348}
8b6a5902 8349
3e4093b6 8350/* Common handling for both array range and field name designators.
e3455240 8351 ARRAY argument is nonzero for array ranges. Returns false for success. */
400fbf9f 8352
e3455240 8353static bool
30af3a2b 8354set_designator (location_t loc, bool array,
ea58ef42 8355 struct obstack *braced_init_obstack)
de520661 8356{
3e4093b6
RS
8357 tree subtype;
8358 enum tree_code subcode;
de520661 8359
3e4093b6
RS
8360 /* Don't die if an entire brace-pair level is superfluous
8361 in the containing level. */
3fa8871b 8362 if (constructor_type == NULL_TREE)
e3455240 8363 return true;
de520661 8364
366de0ce
NS
8365 /* If there were errors in this designator list already, bail out
8366 silently. */
b06df647 8367 if (designator_erroneous)
e3455240 8368 return true;
e28cae4f 8369
3e4093b6
RS
8370 if (!designator_depth)
8371 {
366de0ce 8372 gcc_assert (!constructor_range_stack);
de520661 8373
3e4093b6
RS
8374 /* Designator list starts at the level of closest explicit
8375 braces. */
8376 while (constructor_stack->implicit)
34cf811f 8377 process_init_element (input_location,
5dd9a9d0
DM
8378 pop_init_level (loc, 1, braced_init_obstack,
8379 last_init_list_comma),
34cf811f 8380 true, braced_init_obstack);
3e4093b6 8381 constructor_designated = 1;
e3455240 8382 return false;
3e4093b6 8383 }
de520661 8384
366de0ce 8385 switch (TREE_CODE (constructor_type))
3c3fa147 8386 {
366de0ce
NS
8387 case RECORD_TYPE:
8388 case UNION_TYPE:
3e4093b6
RS
8389 subtype = TREE_TYPE (constructor_fields);
8390 if (subtype != error_mark_node)
8391 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
8392 break;
8393 case ARRAY_TYPE:
3e4093b6 8394 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
8395 break;
8396 default:
8397 gcc_unreachable ();
de520661 8398 }
400fbf9f 8399
3e4093b6
RS
8400 subcode = TREE_CODE (subtype);
8401 if (array && subcode != ARRAY_TYPE)
8402 {
ea58ef42 8403 error_init (loc, "array index in non-array initializer");
e3455240 8404 return true;
3e4093b6
RS
8405 }
8406 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8407 {
ea58ef42 8408 error_init (loc, "field name not in record or union initializer");
e3455240 8409 return true;
3e4093b6 8410 }
d45cf215 8411
3e4093b6 8412 constructor_designated = 1;
16595a1f 8413 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 8414 push_init_level (loc, 2, braced_init_obstack);
e3455240 8415 return false;
de520661 8416}
400fbf9f 8417
3e4093b6
RS
8418/* If there are range designators in designator list, push a new designator
8419 to constructor_range_stack. RANGE_END is end of such stack range or
8420 NULL_TREE if there is no range designator at this level. */
400fbf9f 8421
3e4093b6 8422static void
a1e3b3d9 8423push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8424{
8425 struct constructor_range_stack *p;
400fbf9f 8426
a1e3b3d9
LB
8427 p = (struct constructor_range_stack *)
8428 obstack_alloc (braced_init_obstack,
8429 sizeof (struct constructor_range_stack));
3e4093b6
RS
8430 p->prev = constructor_range_stack;
8431 p->next = 0;
8432 p->fields = constructor_fields;
8433 p->range_start = constructor_index;
8434 p->index = constructor_index;
8435 p->stack = constructor_stack;
8436 p->range_end = range_end;
8b6a5902 8437 if (constructor_range_stack)
3e4093b6
RS
8438 constructor_range_stack->next = p;
8439 constructor_range_stack = p;
de520661 8440}
400fbf9f 8441
3e4093b6
RS
8442/* Within an array initializer, specify the next index to be initialized.
8443 FIRST is that index. If LAST is nonzero, then initialize a range
8444 of indices, running from FIRST through LAST. */
5a7ec9d9 8445
de520661 8446void
ea58ef42 8447set_init_index (location_t loc, tree first, tree last,
d033409e 8448 struct obstack *braced_init_obstack)
de520661 8449{
30af3a2b 8450 if (set_designator (loc, true, braced_init_obstack))
3e4093b6 8451 return;
de520661 8452
b06df647 8453 designator_erroneous = 1;
de520661 8454
3ea8cd06
JM
8455 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8456 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8457 {
ea58ef42 8458 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8459 return;
8460 }
8461
2b6da65c
JM
8462 if (TREE_CODE (first) != INTEGER_CST)
8463 {
8464 first = c_fully_fold (first, false, NULL);
8465 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8466 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8467 "array index in initializer is not "
8468 "an integer constant expression");
8469 }
8470
8471 if (last && TREE_CODE (last) != INTEGER_CST)
8472 {
8473 last = c_fully_fold (last, false, NULL);
8474 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8475 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8476 "array index in initializer is not "
8477 "an integer constant expression");
8478 }
8479
3e4093b6 8480 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8481 error_init (loc, "nonconstant array index in initializer");
3fa8871b 8482 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8483 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8484 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8485 error_init (loc, "array index in non-array initializer");
622adc7e 8486 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8487 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8488 else if (constructor_max_index
8489 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8490 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8491 else
de520661 8492 {
928c19bb
JM
8493 constant_expression_warning (first);
8494 if (last)
8495 constant_expression_warning (last);
3e4093b6 8496 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8497 if (tree_int_cst_lt (constructor_index, first))
8498 {
8499 constructor_index = copy_node (constructor_index);
8500 TREE_OVERFLOW (constructor_index) = 1;
8501 }
665f2503 8502
3e4093b6 8503 if (last)
2bede729 8504 {
3e4093b6 8505 if (tree_int_cst_equal (first, last))
3fa8871b 8506 last = NULL_TREE;
3e4093b6
RS
8507 else if (tree_int_cst_lt (last, first))
8508 {
ea58ef42 8509 error_init (loc, "empty index range in initializer");
3fa8871b 8510 last = NULL_TREE;
3e4093b6
RS
8511 }
8512 else
8513 {
8514 last = convert (bitsizetype, last);
3fa8871b 8515 if (constructor_max_index != NULL_TREE
3e4093b6
RS
8516 && tree_int_cst_lt (constructor_max_index, last))
8517 {
ea58ef42
MP
8518 error_init (loc, "array index range in initializer exceeds "
8519 "array bounds");
3fa8871b 8520 last = NULL_TREE;
3e4093b6
RS
8521 }
8522 }
2bede729 8523 }
fed3cef0 8524
3e4093b6 8525 designator_depth++;
b06df647 8526 designator_erroneous = 0;
3e4093b6 8527 if (constructor_range_stack || last)
a1e3b3d9 8528 push_range_stack (last, braced_init_obstack);
de520661 8529 }
de520661 8530}
3e4093b6
RS
8531
8532/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8533
de520661 8534void
f7e4f2e3 8535set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
ea58ef42 8536 struct obstack *braced_init_obstack)
de520661 8537{
0fb96aa4 8538 tree field;
94ba5069 8539
30af3a2b 8540 if (set_designator (loc, false, braced_init_obstack))
3e4093b6
RS
8541 return;
8542
b06df647 8543 designator_erroneous = 1;
3e4093b6 8544
3e636daf 8545 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8546 {
ea58ef42 8547 error_init (loc, "field name not in record or union initializer");
3e4093b6 8548 return;
94ba5069
RS
8549 }
8550
0fb96aa4 8551 field = lookup_field (constructor_type, fieldname);
8b6a5902 8552
3fa8871b 8553 if (field == NULL_TREE)
f7e4f2e3
DM
8554 {
8555 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8556 if (guessed_id)
8557 {
264757fb
DM
8558 gcc_rich_location rich_loc (fieldname_loc);
8559 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
64a5912c
DM
8560 error_at (&rich_loc,
8561 "%qT has no member named %qE; did you mean %qE?",
8562 constructor_type, fieldname, guessed_id);
f7e4f2e3
DM
8563 }
8564 else
8565 error_at (fieldname_loc, "%qT has no member named %qE",
8566 constructor_type, fieldname);
8567 }
3e4093b6 8568 else
0fb96aa4
JM
8569 do
8570 {
8571 constructor_fields = TREE_VALUE (field);
8572 designator_depth++;
8573 designator_erroneous = 0;
8574 if (constructor_range_stack)
8575 push_range_stack (NULL_TREE, braced_init_obstack);
8576 field = TREE_CHAIN (field);
8577 if (field)
8578 {
30af3a2b 8579 if (set_designator (loc, false, braced_init_obstack))
0fb96aa4
JM
8580 return;
8581 }
8582 }
8583 while (field != NULL_TREE);
3e4093b6
RS
8584}
8585\f
8586/* Add a new initializer to the tree of pending initializers. PURPOSE
8587 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8588 VALUE is the value of that index or field. If ORIGTYPE is not
8589 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8590
8591 IMPLICIT is true if value comes from pop_init_level (1),
8592 the new initializer has been merged with the existing one
8593 and thus no warnings should be emitted about overriding an
8594 existing initializer. */
de520661 8595
3e4093b6 8596static void
96b40f8d
MP
8597add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8598 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8599{
8600 struct init_node *p, **q, *r;
8601
8602 q = &constructor_pending_elts;
8603 p = 0;
8604
8605 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8606 {
3e4093b6 8607 while (*q != 0)
91fa3c30 8608 {
3e4093b6
RS
8609 p = *q;
8610 if (tree_int_cst_lt (purpose, p->purpose))
8611 q = &p->left;
8612 else if (tree_int_cst_lt (p->purpose, purpose))
8613 q = &p->right;
8614 else
8615 {
b295aee2
JJ
8616 if (!implicit)
8617 {
8618 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8619 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8620 "initialized field with side-effects "
8621 "overwritten");
b295aee2 8622 else if (warn_override_init)
96b40f8d
MP
8623 warning_init (loc, OPT_Woverride_init,
8624 "initialized field overwritten");
b295aee2 8625 }
3e4093b6 8626 p->value = value;
bbbbb16a 8627 p->origtype = origtype;
3e4093b6
RS
8628 return;
8629 }
91fa3c30 8630 }
de520661 8631 }
3e4093b6 8632 else
de520661 8633 {
3e4093b6 8634 tree bitpos;
400fbf9f 8635
3e4093b6
RS
8636 bitpos = bit_position (purpose);
8637 while (*q != NULL)
8638 {
8639 p = *q;
8640 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8641 q = &p->left;
8642 else if (p->purpose != purpose)
8643 q = &p->right;
8644 else
8645 {
b295aee2
JJ
8646 if (!implicit)
8647 {
8648 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8649 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8650 "initialized field with side-effects "
8651 "overwritten");
b295aee2 8652 else if (warn_override_init)
96b40f8d
MP
8653 warning_init (loc, OPT_Woverride_init,
8654 "initialized field overwritten");
b295aee2 8655 }
3e4093b6 8656 p->value = value;
bbbbb16a 8657 p->origtype = origtype;
3e4093b6
RS
8658 return;
8659 }
8660 }
91fa3c30 8661 }
b71c7f8a 8662
a1e3b3d9
LB
8663 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8664 sizeof (struct init_node));
3e4093b6
RS
8665 r->purpose = purpose;
8666 r->value = value;
bbbbb16a 8667 r->origtype = origtype;
8b6a5902 8668
3e4093b6
RS
8669 *q = r;
8670 r->parent = p;
8671 r->left = 0;
8672 r->right = 0;
8673 r->balance = 0;
b71c7f8a 8674
3e4093b6 8675 while (p)
de520661 8676 {
3e4093b6 8677 struct init_node *s;
665f2503 8678
3e4093b6 8679 if (r == p->left)
2bede729 8680 {
3e4093b6
RS
8681 if (p->balance == 0)
8682 p->balance = -1;
8683 else if (p->balance < 0)
8684 {
8685 if (r->balance < 0)
8686 {
8687 /* L rotation. */
8688 p->left = r->right;
8689 if (p->left)
8690 p->left->parent = p;
8691 r->right = p;
e7b6a0ee 8692
3e4093b6
RS
8693 p->balance = 0;
8694 r->balance = 0;
39bc99c2 8695
3e4093b6
RS
8696 s = p->parent;
8697 p->parent = r;
8698 r->parent = s;
8699 if (s)
8700 {
8701 if (s->left == p)
8702 s->left = r;
8703 else
8704 s->right = r;
8705 }
8706 else
8707 constructor_pending_elts = r;
8708 }
8709 else
8710 {
8711 /* LR rotation. */
8712 struct init_node *t = r->right;
e7b6a0ee 8713
3e4093b6
RS
8714 r->right = t->left;
8715 if (r->right)
8716 r->right->parent = r;
8717 t->left = r;
8718
8719 p->left = t->right;
8720 if (p->left)
8721 p->left->parent = p;
8722 t->right = p;
8723
8724 p->balance = t->balance < 0;
8725 r->balance = -(t->balance > 0);
8726 t->balance = 0;
8727
8728 s = p->parent;
8729 p->parent = t;
8730 r->parent = t;
8731 t->parent = s;
8732 if (s)
8733 {
8734 if (s->left == p)
8735 s->left = t;
8736 else
8737 s->right = t;
8738 }
8739 else
8740 constructor_pending_elts = t;
8741 }
8742 break;
8743 }
8744 else
8745 {
8746 /* p->balance == +1; growth of left side balances the node. */
8747 p->balance = 0;
8748 break;
8749 }
2bede729 8750 }
3e4093b6
RS
8751 else /* r == p->right */
8752 {
8753 if (p->balance == 0)
8754 /* Growth propagation from right side. */
8755 p->balance++;
8756 else if (p->balance > 0)
8757 {
8758 if (r->balance > 0)
8759 {
8760 /* R rotation. */
8761 p->right = r->left;
8762 if (p->right)
8763 p->right->parent = p;
8764 r->left = p;
8765
8766 p->balance = 0;
8767 r->balance = 0;
8768
8769 s = p->parent;
8770 p->parent = r;
8771 r->parent = s;
8772 if (s)
8773 {
8774 if (s->left == p)
8775 s->left = r;
8776 else
8777 s->right = r;
8778 }
8779 else
8780 constructor_pending_elts = r;
8781 }
8782 else /* r->balance == -1 */
8783 {
8784 /* RL rotation */
8785 struct init_node *t = r->left;
8786
8787 r->left = t->right;
8788 if (r->left)
8789 r->left->parent = r;
8790 t->right = r;
8791
8792 p->right = t->left;
8793 if (p->right)
8794 p->right->parent = p;
8795 t->left = p;
8796
8797 r->balance = (t->balance < 0);
8798 p->balance = -(t->balance > 0);
8799 t->balance = 0;
8800
8801 s = p->parent;
8802 p->parent = t;
8803 r->parent = t;
8804 t->parent = s;
8805 if (s)
8806 {
8807 if (s->left == p)
8808 s->left = t;
8809 else
8810 s->right = t;
8811 }
8812 else
8813 constructor_pending_elts = t;
8814 }
8815 break;
8816 }
8817 else
8818 {
8819 /* p->balance == -1; growth of right side balances the node. */
8820 p->balance = 0;
8821 break;
8822 }
8823 }
8824
8825 r = p;
8826 p = p->parent;
8827 }
8828}
8829
8830/* Build AVL tree from a sorted chain. */
8831
8832static void
a1e3b3d9 8833set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8834{
4038c495
GB
8835 unsigned HOST_WIDE_INT ix;
8836 tree index, value;
3e4093b6
RS
8837
8838 if (TREE_CODE (constructor_type) != RECORD_TYPE
8839 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8840 return;
8841
4038c495 8842 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8843 add_pending_init (input_location, index, value, NULL_TREE, true,
8844 braced_init_obstack);
9771b263 8845 constructor_elements = NULL;
3e4093b6
RS
8846 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8847 {
8848 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8849 /* Skip any nameless bit fields at the beginning. */
3fa8871b 8850 while (constructor_unfilled_fields != NULL_TREE
3e4093b6 8851 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
3fa8871b 8852 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
3e4093b6 8853 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8854
de520661 8855 }
3e4093b6 8856 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8857 {
3e4093b6
RS
8858 if (TYPE_DOMAIN (constructor_type))
8859 constructor_unfilled_index
8860 = convert (bitsizetype,
8861 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8862 else
8863 constructor_unfilled_index = bitsize_zero_node;
de520661 8864 }
3e4093b6 8865 constructor_incremental = 0;
de520661 8866}
400fbf9f 8867
3e4093b6 8868/* Build AVL tree from a string constant. */
de520661 8869
3e4093b6 8870static void
a1e3b3d9
LB
8871set_nonincremental_init_from_string (tree str,
8872 struct obstack * braced_init_obstack)
de520661 8873{
3e4093b6
RS
8874 tree value, purpose, type;
8875 HOST_WIDE_INT val[2];
8876 const char *p, *end;
8877 int byte, wchar_bytes, charwidth, bitpos;
de520661 8878
366de0ce 8879 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8880
c466b2cd 8881 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6 8882 charwidth = TYPE_PRECISION (char_type_node);
2f1364c2
JJ
8883 gcc_assert ((size_t) wchar_bytes * charwidth
8884 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
3e4093b6
RS
8885 type = TREE_TYPE (constructor_type);
8886 p = TREE_STRING_POINTER (str);
8887 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8888
3e4093b6 8889 for (purpose = bitsize_zero_node;
8824edff
JJ
8890 p < end
8891 && !(constructor_max_index
8892 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8893 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8894 {
3e4093b6 8895 if (wchar_bytes == 1)
ffc5c6a9 8896 {
807e902e
KZ
8897 val[0] = (unsigned char) *p++;
8898 val[1] = 0;
ffc5c6a9
RH
8899 }
8900 else
3e4093b6 8901 {
3e4093b6 8902 val[1] = 0;
807e902e 8903 val[0] = 0;
3e4093b6
RS
8904 for (byte = 0; byte < wchar_bytes; byte++)
8905 {
8906 if (BYTES_BIG_ENDIAN)
8907 bitpos = (wchar_bytes - byte - 1) * charwidth;
8908 else
8909 bitpos = byte * charwidth;
2f1364c2 8910 val[bitpos / HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8911 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8912 << (bitpos % HOST_BITS_PER_WIDE_INT);
8913 }
8914 }
584ef5fe 8915
8df83eae 8916 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8917 {
8918 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8919 if (bitpos < HOST_BITS_PER_WIDE_INT)
8920 {
e3fe09c1 8921 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
3e4093b6 8922 {
aa256c4a 8923 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8924 val[1] = -1;
3e4093b6
RS
8925 }
8926 }
8927 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8928 {
807e902e
KZ
8929 if (val[0] < 0)
8930 val[1] = -1;
3e4093b6 8931 }
e3fe09c1 8932 else if (val[1] & (HOST_WIDE_INT_1
3e4093b6 8933 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8934 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8935 }
ffc5c6a9 8936
807e902e
KZ
8937 value = wide_int_to_tree (type,
8938 wide_int::from_array (val, 2,
8939 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8940 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8941 braced_init_obstack);
9dfcc8db
BH
8942 }
8943
3e4093b6
RS
8944 constructor_incremental = 0;
8945}
de520661 8946
6574d78e 8947/* Return value of FIELD in pending initializer or NULL_TREE if the field was
3e4093b6
RS
8948 not initialized yet. */
8949
8950static tree
a1e3b3d9 8951find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8952{
8953 struct init_node *p;
8954
8955 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8956 {
3e4093b6
RS
8957 if (constructor_incremental
8958 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8959 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8960
8961 p = constructor_pending_elts;
8962 while (p)
19d76e60 8963 {
3e4093b6
RS
8964 if (tree_int_cst_lt (field, p->purpose))
8965 p = p->left;
8966 else if (tree_int_cst_lt (p->purpose, field))
8967 p = p->right;
8968 else
8969 return p->value;
19d76e60 8970 }
19d76e60 8971 }
3e4093b6 8972 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8973 {
3e4093b6 8974 tree bitpos = bit_position (field);
de520661 8975
3e4093b6
RS
8976 if (constructor_incremental
8977 && (!constructor_unfilled_fields
8978 || tree_int_cst_lt (bitpos,
8979 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8980 set_nonincremental_init (braced_init_obstack);
de520661 8981
3e4093b6
RS
8982 p = constructor_pending_elts;
8983 while (p)
8984 {
8985 if (field == p->purpose)
8986 return p->value;
8987 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8988 p = p->left;
8989 else
8990 p = p->right;
8991 }
8992 }
8993 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8994 {
9771b263
DN
8995 if (!vec_safe_is_empty (constructor_elements)
8996 && (constructor_elements->last ().index == field))
8997 return constructor_elements->last ().value;
de520661 8998 }
6574d78e 8999 return NULL_TREE;
de520661
RS
9000}
9001
3e4093b6
RS
9002/* "Output" the next constructor element.
9003 At top level, really output it to assembler code now.
9004 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 9005 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
9006 TYPE is the data type that the containing data type wants here.
9007 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
9008 If VALUE is a string constant, STRICT_STRING is true if it is
9009 unparenthesized or we should not warn here for it being parenthesized.
9010 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 9011
30af3a2b
MP
9012 PENDING if true means output pending elements that belong
9013 right after this element. (PENDING is normally true;
9014 it is false while outputting pending elements, to avoid recursion.)
b295aee2
JJ
9015
9016 IMPLICIT is true if value comes from pop_init_level (1),
9017 the new initializer has been merged with the existing one
9018 and thus no warnings should be emitted about overriding an
9019 existing initializer. */
8b6a5902 9020
3e4093b6 9021static void
34cf811f 9022output_init_element (location_t loc, tree value, tree origtype,
30af3a2b 9023 bool strict_string, tree type, tree field, bool pending,
34cf811f 9024 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 9025{
8ce94e44 9026 tree semantic_type = NULL_TREE;
928c19bb
JM
9027 bool maybe_const = true;
9028 bool npc;
4038c495 9029
0a880880 9030 if (type == error_mark_node || value == error_mark_node)
8b6a5902 9031 {
3e4093b6
RS
9032 constructor_erroneous = 1;
9033 return;
8b6a5902 9034 }
46bdb9cf
JM
9035 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9036 && (TREE_CODE (value) == STRING_CST
9037 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9038 && !(TREE_CODE (value) == STRING_CST
9039 && TREE_CODE (type) == ARRAY_TYPE
9040 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9041 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9042 TYPE_MAIN_VARIANT (type)))
c2255bc4 9043 value = array_to_pointer_conversion (input_location, value);
8b6a5902 9044
3e4093b6 9045 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 9046 && require_constant_value && pending)
8b6a5902 9047 {
3e4093b6
RS
9048 /* As an extension, allow initializing objects with static storage
9049 duration with compound literals (which are then treated just as
9050 the brace enclosed list they contain). */
4435bb92
MP
9051 if (flag_isoc99)
9052 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9053 "constant");
3e4093b6
RS
9054 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9055 value = DECL_INITIAL (decl);
8b6a5902
JJ
9056 }
9057
928c19bb 9058 npc = null_pointer_constant_p (value);
8ce94e44
JM
9059 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9060 {
9061 semantic_type = TREE_TYPE (value);
9062 value = TREE_OPERAND (value, 0);
9063 }
928c19bb
JM
9064 value = c_fully_fold (value, require_constant_value, &maybe_const);
9065
3e4093b6
RS
9066 if (value == error_mark_node)
9067 constructor_erroneous = 1;
9068 else if (!TREE_CONSTANT (value))
9069 constructor_constant = 0;
ee45a32d
EB
9070 else if (!initializer_constant_valid_p (value,
9071 TREE_TYPE (value),
9072 AGGREGATE_TYPE_P (constructor_type)
9073 && TYPE_REVERSE_STORAGE_ORDER
9074 (constructor_type))
3e636daf 9075 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
9076 && DECL_C_BIT_FIELD (field)
9077 && TREE_CODE (value) != INTEGER_CST))
9078 constructor_simple = 0;
928c19bb
JM
9079 if (!maybe_const)
9080 constructor_nonconst = 1;
3e4093b6 9081
4378d117
MS
9082 /* Digest the initializer and issue any errors about incompatible
9083 types before issuing errors about non-constant initializers. */
9084 tree new_value = value;
9085 if (semantic_type)
9086 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9087 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9088 require_constant_value);
9089 if (new_value == error_mark_node)
9090 {
9091 constructor_erroneous = 1;
9092 return;
9093 }
9094 if (require_constant_value || require_constant_elements)
9095 constant_expression_warning (new_value);
9096
9097 /* Proceed to check the constness of the original initializer. */
116df786 9098 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 9099 {
116df786
RH
9100 if (require_constant_value)
9101 {
ea58ef42 9102 error_init (loc, "initializer element is not constant");
116df786
RH
9103 value = error_mark_node;
9104 }
9105 else if (require_constant_elements)
8337d1db 9106 pedwarn (loc, OPT_Wpedantic,
509c9d60 9107 "initializer element is not computable at load time");
8b6a5902 9108 }
928c19bb
JM
9109 else if (!maybe_const
9110 && (require_constant_value || require_constant_elements))
8337d1db 9111 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 9112 "initializer element is not a constant expression");
3e4093b6 9113
81f40b79
ILT
9114 /* Issue -Wc++-compat warnings about initializing a bitfield with
9115 enum type. */
9116 if (warn_cxx_compat
9117 && field != NULL_TREE
9118 && TREE_CODE (field) == FIELD_DECL
9119 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9120 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9121 != TYPE_MAIN_VARIANT (type))
9122 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9123 {
9124 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9125 if (checktype != error_mark_node
9126 && (TYPE_MAIN_VARIANT (checktype)
9127 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 9128 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
9129 "enum conversion in initialization is invalid in C++");
9130 }
9131
3e4093b6
RS
9132 /* If this field is empty (and not at the end of structure),
9133 don't do anything other than checking the initializer. */
9134 if (field
9135 && (TREE_TYPE (field) == error_mark_node
9136 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9137 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9138 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 9139 || DECL_CHAIN (field)))))
3e4093b6
RS
9140 return;
9141
4378d117
MS
9142 /* Finally, set VALUE to the initializer value digested above. */
9143 value = new_value;
8b6a5902 9144
3e4093b6
RS
9145 /* If this element doesn't come next in sequence,
9146 put it on constructor_pending_elts. */
9147 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9148 && (!constructor_incremental
9149 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 9150 {
3e4093b6
RS
9151 if (constructor_incremental
9152 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 9153 set_nonincremental_init (braced_init_obstack);
3e4093b6 9154
96b40f8d 9155 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 9156 braced_init_obstack);
3e4093b6 9157 return;
8b6a5902 9158 }
3e4093b6
RS
9159 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9160 && (!constructor_incremental
9161 || field != constructor_unfilled_fields))
8b6a5902 9162 {
3e4093b6
RS
9163 /* We do this for records but not for unions. In a union,
9164 no matter which field is specified, it can be initialized
9165 right away since it starts at the beginning of the union. */
9166 if (constructor_incremental)
9167 {
9168 if (!constructor_unfilled_fields)
a1e3b3d9 9169 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
9170 else
9171 {
9172 tree bitpos, unfillpos;
9173
9174 bitpos = bit_position (field);
9175 unfillpos = bit_position (constructor_unfilled_fields);
9176
9177 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 9178 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
9179 }
9180 }
9181
96b40f8d 9182 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 9183 braced_init_obstack);
3e4093b6 9184 return;
8b6a5902 9185 }
3e4093b6 9186 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 9187 && !vec_safe_is_empty (constructor_elements))
3e4093b6 9188 {
b295aee2
JJ
9189 if (!implicit)
9190 {
9771b263 9191 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 9192 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
9193 "initialized field with side-effects overwritten");
9194 else if (warn_override_init)
96b40f8d
MP
9195 warning_init (loc, OPT_Woverride_init,
9196 "initialized field overwritten");
b295aee2 9197 }
8b6a5902 9198
3e4093b6 9199 /* We can have just one union field set. */
9771b263 9200 constructor_elements = NULL;
3e4093b6 9201 }
8b6a5902 9202
3e4093b6
RS
9203 /* Otherwise, output this element either to
9204 constructor_elements or to the assembler file. */
8b6a5902 9205
f32682ca 9206 constructor_elt celt = {field, value};
9771b263 9207 vec_safe_push (constructor_elements, celt);
8b6a5902 9208
3e4093b6
RS
9209 /* Advance the variable that indicates sequential elements output. */
9210 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9211 constructor_unfilled_index
db3927fb
AH
9212 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9213 bitsize_one_node);
3e4093b6
RS
9214 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9215 {
9216 constructor_unfilled_fields
910ad8de 9217 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 9218
3e4093b6 9219 /* Skip any nameless bit fields. */
3fa8871b 9220 while (constructor_unfilled_fields != NULL_TREE
3e4093b6 9221 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
3fa8871b 9222 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
3e4093b6 9223 constructor_unfilled_fields =
910ad8de 9224 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
9225 }
9226 else if (TREE_CODE (constructor_type) == UNION_TYPE)
3fa8871b 9227 constructor_unfilled_fields = NULL_TREE;
de520661 9228
3e4093b6
RS
9229 /* Now output any pending elements which have become next. */
9230 if (pending)
a1e3b3d9 9231 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 9232}
8b6a5902 9233
3e4093b6
RS
9234/* Output any pending elements which have become next.
9235 As we output elements, constructor_unfilled_{fields,index}
9236 advances, which may cause other elements to become next;
9237 if so, they too are output.
8b6a5902 9238
3e4093b6
RS
9239 If ALL is 0, we return when there are
9240 no more pending elements to output now.
665f2503 9241
3e4093b6
RS
9242 If ALL is 1, we output space as necessary so that
9243 we can output all the pending elements. */
3e4093b6 9244static void
a1e3b3d9 9245output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
9246{
9247 struct init_node *elt = constructor_pending_elts;
9248 tree next;
de520661 9249
3e4093b6
RS
9250 retry:
9251
ba228239 9252 /* Look through the whole pending tree.
3e4093b6
RS
9253 If we find an element that should be output now,
9254 output it. Otherwise, set NEXT to the element
9255 that comes first among those still pending. */
9256
3fa8871b 9257 next = NULL_TREE;
3e4093b6
RS
9258 while (elt)
9259 {
9260 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 9261 {
3e4093b6
RS
9262 if (tree_int_cst_equal (elt->purpose,
9263 constructor_unfilled_index))
34cf811f
MP
9264 output_init_element (input_location, elt->value, elt->origtype,
9265 true, TREE_TYPE (constructor_type),
30af3a2b 9266 constructor_unfilled_index, false, false,
a1e3b3d9 9267 braced_init_obstack);
3e4093b6
RS
9268 else if (tree_int_cst_lt (constructor_unfilled_index,
9269 elt->purpose))
8b6a5902 9270 {
3e4093b6
RS
9271 /* Advance to the next smaller node. */
9272 if (elt->left)
9273 elt = elt->left;
9274 else
9275 {
9276 /* We have reached the smallest node bigger than the
9277 current unfilled index. Fill the space first. */
9278 next = elt->purpose;
9279 break;
9280 }
8b6a5902 9281 }
ce662d4c
JJ
9282 else
9283 {
3e4093b6
RS
9284 /* Advance to the next bigger node. */
9285 if (elt->right)
9286 elt = elt->right;
9287 else
ce662d4c 9288 {
3e4093b6
RS
9289 /* We have reached the biggest node in a subtree. Find
9290 the parent of it, which is the next bigger node. */
9291 while (elt->parent && elt->parent->right == elt)
9292 elt = elt->parent;
9293 elt = elt->parent;
9294 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9295 elt->purpose))
9296 {
9297 next = elt->purpose;
9298 break;
9299 }
ce662d4c
JJ
9300 }
9301 }
8b6a5902 9302 }
3e636daf 9303 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9304 {
9305 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 9306
3e4093b6 9307 /* If the current record is complete we are done. */
3fa8871b 9308 if (constructor_unfilled_fields == NULL_TREE)
3e4093b6 9309 break;
de520661 9310
3e4093b6
RS
9311 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9312 elt_bitpos = bit_position (elt->purpose);
9313 /* We can't compare fields here because there might be empty
9314 fields in between. */
9315 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9316 {
9317 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
9318 output_init_element (input_location, elt->value, elt->origtype,
9319 true, TREE_TYPE (elt->purpose),
30af3a2b 9320 elt->purpose, false, false,
a1e3b3d9 9321 braced_init_obstack);
3e4093b6
RS
9322 }
9323 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9324 {
9325 /* Advance to the next smaller node. */
9326 if (elt->left)
9327 elt = elt->left;
9328 else
9329 {
9330 /* We have reached the smallest node bigger than the
9331 current unfilled field. Fill the space first. */
9332 next = elt->purpose;
9333 break;
9334 }
9335 }
9336 else
9337 {
9338 /* Advance to the next bigger node. */
9339 if (elt->right)
9340 elt = elt->right;
9341 else
9342 {
9343 /* We have reached the biggest node in a subtree. Find
9344 the parent of it, which is the next bigger node. */
9345 while (elt->parent && elt->parent->right == elt)
9346 elt = elt->parent;
9347 elt = elt->parent;
9348 if (elt
9349 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9350 bit_position (elt->purpose))))
9351 {
9352 next = elt->purpose;
9353 break;
9354 }
9355 }
9356 }
9357 }
9358 }
de520661 9359
3e4093b6
RS
9360 /* Ordinarily return, but not if we want to output all
9361 and there are elements left. */
3fa8871b 9362 if (!(all && next != NULL_TREE))
e5cfb88f
RK
9363 return;
9364
3e4093b6
RS
9365 /* If it's not incremental, just skip over the gap, so that after
9366 jumping to retry we will output the next successive element. */
3e636daf 9367 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9368 constructor_unfilled_fields = next;
9369 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9370 constructor_unfilled_index = next;
de520661 9371
3e4093b6
RS
9372 /* ELT now points to the node in the pending tree with the next
9373 initializer to output. */
9374 goto retry;
de520661
RS
9375}
9376\f
3e4093b6
RS
9377/* Add one non-braced element to the current constructor level.
9378 This adjusts the current position within the constructor's type.
9379 This may also start or terminate implicit levels
9380 to handle a partly-braced initializer.
e5e809f4 9381
3e4093b6 9382 Once this has found the correct level for the new element,
b295aee2
JJ
9383 it calls output_init_element.
9384
9385 IMPLICIT is true if value comes from pop_init_level (1),
9386 the new initializer has been merged with the existing one
9387 and thus no warnings should be emitted about overriding an
9388 existing initializer. */
3e4093b6
RS
9389
9390void
34cf811f 9391process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 9392 struct obstack * braced_init_obstack)
e5e809f4 9393{
916c5919 9394 tree orig_value = value.value;
3fa8871b
MP
9395 int string_flag
9396 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
916c5919 9397 bool strict_string = value.original_code == STRING_CST;
340baef7 9398 bool was_designated = designator_depth != 0;
e5e809f4 9399
3e4093b6 9400 designator_depth = 0;
b06df647 9401 designator_erroneous = 0;
e5e809f4 9402
9bac5cbb
G
9403 if (!implicit && value.value && !integer_zerop (value.value))
9404 constructor_zeroinit = 0;
9405
3e4093b6
RS
9406 /* Handle superfluous braces around string cst as in
9407 char x[] = {"foo"}; */
9408 if (string_flag
9409 && constructor_type
340baef7 9410 && !was_designated
3e4093b6 9411 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 9412 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 9413 && integer_zerop (constructor_unfilled_index))
e5e809f4 9414 {
916c5919 9415 if (constructor_stack->replacement_value.value)
ea58ef42 9416 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
9417 constructor_stack->replacement_value = value;
9418 return;
e5e809f4 9419 }
8b6a5902 9420
3fa8871b 9421 if (constructor_stack->replacement_value.value != NULL_TREE)
3e4093b6 9422 {
ea58ef42 9423 error_init (loc, "excess elements in struct initializer");
3e4093b6 9424 return;
e5e809f4
JL
9425 }
9426
3e4093b6
RS
9427 /* Ignore elements of a brace group if it is entirely superfluous
9428 and has already been diagnosed. */
3fa8871b 9429 if (constructor_type == NULL_TREE)
3e4093b6 9430 return;
e5e809f4 9431
976d5a22
TT
9432 if (!implicit && warn_designated_init && !was_designated
9433 && TREE_CODE (constructor_type) == RECORD_TYPE
9434 && lookup_attribute ("designated_init",
9435 TYPE_ATTRIBUTES (constructor_type)))
9436 warning_init (loc,
9437 OPT_Wdesignated_init,
9438 "positional initialization of field "
9439 "in %<struct%> declared with %<designated_init%> attribute");
9440
3e4093b6
RS
9441 /* If we've exhausted any levels that didn't have braces,
9442 pop them now. */
9443 while (constructor_stack->implicit)
9444 {
3e636daf 9445 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3fa8871b 9446 && constructor_fields == NULL_TREE)
ea58ef42 9447 process_init_element (loc,
5dd9a9d0
DM
9448 pop_init_level (loc, 1, braced_init_obstack,
9449 last_init_list_comma),
a1e3b3d9 9450 true, braced_init_obstack);
53650abe 9451 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9452 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9453 && constructor_max_index
9454 && tree_int_cst_lt (constructor_max_index,
9455 constructor_index))
ea58ef42 9456 process_init_element (loc,
5dd9a9d0
DM
9457 pop_init_level (loc, 1, braced_init_obstack,
9458 last_init_list_comma),
a1e3b3d9 9459 true, braced_init_obstack);
3e4093b6
RS
9460 else
9461 break;
9462 }
e5e809f4 9463
3e4093b6
RS
9464 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9465 if (constructor_range_stack)
e5e809f4 9466 {
3e4093b6
RS
9467 /* If value is a compound literal and we'll be just using its
9468 content, don't put it into a SAVE_EXPR. */
916c5919 9469 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9470 || !require_constant_value)
8ce94e44
JM
9471 {
9472 tree semantic_type = NULL_TREE;
9473 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9474 {
9475 semantic_type = TREE_TYPE (value.value);
9476 value.value = TREE_OPERAND (value.value, 0);
9477 }
b2fa0a8b 9478 value.value = save_expr (value.value);
8ce94e44
JM
9479 if (semantic_type)
9480 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9481 value.value);
9482 }
3e4093b6 9483 }
e5e809f4 9484
3e4093b6
RS
9485 while (1)
9486 {
9487 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9488 {
3e4093b6
RS
9489 tree fieldtype;
9490 enum tree_code fieldcode;
e5e809f4 9491
3fa8871b 9492 if (constructor_fields == NULL_TREE)
3e4093b6 9493 {
ea58ef42 9494 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9495 break;
9496 }
e5e809f4 9497
3e4093b6
RS
9498 fieldtype = TREE_TYPE (constructor_fields);
9499 if (fieldtype != error_mark_node)
9500 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9501 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9502
3e4093b6
RS
9503 /* Error for non-static initialization of a flexible array member. */
9504 if (fieldcode == ARRAY_TYPE
9505 && !require_constant_value
9506 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9507 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9508 {
ea58ef42
MP
9509 error_init (loc, "non-static initialization of a flexible "
9510 "array member");
3e4093b6
RS
9511 break;
9512 }
e5e809f4 9513
2cc901dc
MP
9514 /* Error for initialization of a flexible array member with
9515 a string constant if the structure is in an array. E.g.:
9516 struct S { int x; char y[]; };
9517 struct S s[] = { { 1, "foo" } };
9518 is invalid. */
9519 if (string_flag
9520 && fieldcode == ARRAY_TYPE
9521 && constructor_depth > 1
9522 && TYPE_SIZE (fieldtype) == NULL_TREE
9523 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9524 {
9525 bool in_array_p = false;
9526 for (struct constructor_stack *p = constructor_stack;
9527 p && p->type; p = p->next)
9528 if (TREE_CODE (p->type) == ARRAY_TYPE)
9529 {
9530 in_array_p = true;
9531 break;
9532 }
9533 if (in_array_p)
9534 {
9535 error_init (loc, "initialization of flexible array "
9536 "member in a nested context");
9537 break;
9538 }
9539 }
9540
3e4093b6 9541 /* Accept a string constant to initialize a subarray. */
3fa8871b 9542 if (value.value != NULL_TREE
3e4093b6 9543 && fieldcode == ARRAY_TYPE
197463ae 9544 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9545 && string_flag)
916c5919 9546 value.value = orig_value;
3e4093b6
RS
9547 /* Otherwise, if we have come to a subaggregate,
9548 and we don't have an element of its type, push into it. */
3fa8871b 9549 else if (value.value != NULL_TREE
916c5919
JM
9550 && value.value != error_mark_node
9551 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9552 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9553 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9554 {
ea58ef42 9555 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9556 continue;
9557 }
e5e809f4 9558
916c5919 9559 if (value.value)
3e4093b6
RS
9560 {
9561 push_member_name (constructor_fields);
34cf811f 9562 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9563 strict_string, fieldtype,
30af3a2b 9564 constructor_fields, true, implicit,
a1e3b3d9 9565 braced_init_obstack);
3e4093b6 9566 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9567 }
9568 else
3e4093b6
RS
9569 /* Do the bookkeeping for an element that was
9570 directly output as a constructor. */
e5e809f4 9571 {
3e4093b6
RS
9572 /* For a record, keep track of end position of last field. */
9573 if (DECL_SIZE (constructor_fields))
c22cacf3 9574 constructor_bit_index
db3927fb
AH
9575 = size_binop_loc (input_location, PLUS_EXPR,
9576 bit_position (constructor_fields),
9577 DECL_SIZE (constructor_fields));
3e4093b6
RS
9578
9579 /* If the current field was the first one not yet written out,
9580 it isn't now, so update. */
9581 if (constructor_unfilled_fields == constructor_fields)
9582 {
910ad8de 9583 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9584 /* Skip any nameless bit fields. */
9585 while (constructor_unfilled_fields != 0
9586 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9587 && DECL_NAME (constructor_unfilled_fields) == 0)
9588 constructor_unfilled_fields =
910ad8de 9589 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9590 }
e5e809f4 9591 }
3e4093b6 9592
910ad8de 9593 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6 9594 /* Skip any nameless bit fields at the beginning. */
3fa8871b 9595 while (constructor_fields != NULL_TREE
3e4093b6 9596 && DECL_C_BIT_FIELD (constructor_fields)
3fa8871b 9597 && DECL_NAME (constructor_fields) == NULL_TREE)
910ad8de 9598 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9599 }
3e4093b6 9600 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9601 {
3e4093b6
RS
9602 tree fieldtype;
9603 enum tree_code fieldcode;
e5e809f4 9604
3fa8871b 9605 if (constructor_fields == NULL_TREE)
3e4093b6 9606 {
d033409e 9607 pedwarn_init (loc, 0,
509c9d60 9608 "excess elements in union initializer");
3e4093b6
RS
9609 break;
9610 }
e5e809f4 9611
3e4093b6
RS
9612 fieldtype = TREE_TYPE (constructor_fields);
9613 if (fieldtype != error_mark_node)
9614 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9615 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9616
3e4093b6
RS
9617 /* Warn that traditional C rejects initialization of unions.
9618 We skip the warning if the value is zero. This is done
9619 under the assumption that the zero initializer in user
9620 code appears conditioned on e.g. __STDC__ to avoid
9621 "missing initializer" warnings and relies on default
9622 initialization to zero in the traditional C case.
9623 We also skip the warning if the initializer is designated,
9624 again on the assumption that this must be conditional on
9625 __STDC__ anyway (and we've already complained about the
9626 member-designator already). */
8400e75e 9627 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9628 && !(value.value && (integer_zerop (value.value)
9629 || real_zerop (value.value))))
3176a0c2
DD
9630 warning (OPT_Wtraditional, "traditional C rejects initialization "
9631 "of unions");
e5e809f4 9632
3e4093b6 9633 /* Accept a string constant to initialize a subarray. */
3fa8871b 9634 if (value.value != NULL_TREE
3e4093b6 9635 && fieldcode == ARRAY_TYPE
197463ae 9636 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9637 && string_flag)
916c5919 9638 value.value = orig_value;
3e4093b6
RS
9639 /* Otherwise, if we have come to a subaggregate,
9640 and we don't have an element of its type, push into it. */
3fa8871b 9641 else if (value.value != NULL_TREE
916c5919
JM
9642 && value.value != error_mark_node
9643 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9644 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9645 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9646 {
ea58ef42 9647 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9648 continue;
9649 }
e5e809f4 9650
916c5919 9651 if (value.value)
3e4093b6
RS
9652 {
9653 push_member_name (constructor_fields);
34cf811f 9654 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9655 strict_string, fieldtype,
30af3a2b 9656 constructor_fields, true, implicit,
a1e3b3d9 9657 braced_init_obstack);
3e4093b6 9658 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9659 }
9660 else
3e4093b6
RS
9661 /* Do the bookkeeping for an element that was
9662 directly output as a constructor. */
e5e809f4 9663 {
3e4093b6 9664 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9665 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9666 }
e5e809f4 9667
3fa8871b 9668 constructor_fields = NULL_TREE;
3e4093b6
RS
9669 }
9670 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9671 {
9672 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9673 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9674
3e4093b6 9675 /* Accept a string constant to initialize a subarray. */
3fa8871b 9676 if (value.value != NULL_TREE
3e4093b6 9677 && eltcode == ARRAY_TYPE
197463ae 9678 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9679 && string_flag)
916c5919 9680 value.value = orig_value;
3e4093b6
RS
9681 /* Otherwise, if we have come to a subaggregate,
9682 and we don't have an element of its type, push into it. */
3fa8871b 9683 else if (value.value != NULL_TREE
916c5919
JM
9684 && value.value != error_mark_node
9685 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9686 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9687 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9688 {
ea58ef42 9689 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9690 continue;
9691 }
8b6a5902 9692
3fa8871b 9693 if (constructor_max_index != NULL_TREE
3e4093b6
RS
9694 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9695 || integer_all_onesp (constructor_max_index)))
9696 {
d033409e 9697 pedwarn_init (loc, 0,
509c9d60 9698 "excess elements in array initializer");
3e4093b6
RS
9699 break;
9700 }
8b6a5902 9701
3e4093b6 9702 /* Now output the actual element. */
916c5919 9703 if (value.value)
3e4093b6 9704 {
ae7e9ddd 9705 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9706 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9707 strict_string, elttype,
30af3a2b 9708 constructor_index, true, implicit,
a1e3b3d9 9709 braced_init_obstack);
3e4093b6
RS
9710 RESTORE_SPELLING_DEPTH (constructor_depth);
9711 }
2f6e4e97 9712
3e4093b6 9713 constructor_index
db3927fb
AH
9714 = size_binop_loc (input_location, PLUS_EXPR,
9715 constructor_index, bitsize_one_node);
8b6a5902 9716
916c5919 9717 if (!value.value)
3e4093b6
RS
9718 /* If we are doing the bookkeeping for an element that was
9719 directly output as a constructor, we must update
9720 constructor_unfilled_index. */
9721 constructor_unfilled_index = constructor_index;
9722 }
31521951 9723 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9724 {
9725 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9726
c22cacf3
MS
9727 /* Do a basic check of initializer size. Note that vectors
9728 always have a fixed size derived from their type. */
3e4093b6
RS
9729 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9730 {
d033409e 9731 pedwarn_init (loc, 0,
509c9d60 9732 "excess elements in vector initializer");
3e4093b6
RS
9733 break;
9734 }
8b6a5902 9735
3e4093b6 9736 /* Now output the actual element. */
916c5919 9737 if (value.value)
53650abe
AP
9738 {
9739 if (TREE_CODE (value.value) == VECTOR_CST)
9740 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9741 output_init_element (loc, value.value, value.original_type,
53650abe 9742 strict_string, elttype,
30af3a2b 9743 constructor_index, true, implicit,
a1e3b3d9 9744 braced_init_obstack);
53650abe 9745 }
8b6a5902 9746
3e4093b6 9747 constructor_index
db3927fb
AH
9748 = size_binop_loc (input_location,
9749 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9750
916c5919 9751 if (!value.value)
3e4093b6
RS
9752 /* If we are doing the bookkeeping for an element that was
9753 directly output as a constructor, we must update
9754 constructor_unfilled_index. */
9755 constructor_unfilled_index = constructor_index;
9756 }
8b6a5902 9757
3e4093b6
RS
9758 /* Handle the sole element allowed in a braced initializer
9759 for a scalar variable. */
b4519d39 9760 else if (constructor_type != error_mark_node
3fa8871b 9761 && constructor_fields == NULL_TREE)
8b6a5902 9762 {
d033409e 9763 pedwarn_init (loc, 0,
509c9d60 9764 "excess elements in scalar initializer");
3e4093b6 9765 break;
8b6a5902
JJ
9766 }
9767 else
9768 {
916c5919 9769 if (value.value)
34cf811f 9770 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9771 strict_string, constructor_type,
30af3a2b 9772 NULL_TREE, true, implicit,
a1e3b3d9 9773 braced_init_obstack);
3fa8871b 9774 constructor_fields = NULL_TREE;
8b6a5902
JJ
9775 }
9776
3e4093b6
RS
9777 /* Handle range initializers either at this level or anywhere higher
9778 in the designator stack. */
9779 if (constructor_range_stack)
8b6a5902 9780 {
3e4093b6
RS
9781 struct constructor_range_stack *p, *range_stack;
9782 int finish = 0;
9783
9784 range_stack = constructor_range_stack;
9785 constructor_range_stack = 0;
9786 while (constructor_stack != range_stack->stack)
8b6a5902 9787 {
366de0ce 9788 gcc_assert (constructor_stack->implicit);
34cf811f 9789 process_init_element (loc,
ea58ef42 9790 pop_init_level (loc, 1,
5dd9a9d0
DM
9791 braced_init_obstack,
9792 last_init_list_comma),
a1e3b3d9 9793 true, braced_init_obstack);
8b6a5902 9794 }
3e4093b6
RS
9795 for (p = range_stack;
9796 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9797 p = p->prev)
8b6a5902 9798 {
366de0ce 9799 gcc_assert (constructor_stack->implicit);
34cf811f 9800 process_init_element (loc,
ea58ef42 9801 pop_init_level (loc, 1,
5dd9a9d0
DM
9802 braced_init_obstack,
9803 last_init_list_comma),
a1e3b3d9 9804 true, braced_init_obstack);
8b6a5902 9805 }
3e4093b6 9806
db3927fb
AH
9807 p->index = size_binop_loc (input_location,
9808 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9809 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9810 finish = 1;
9811
9812 while (1)
9813 {
9814 constructor_index = p->index;
9815 constructor_fields = p->fields;
9816 if (finish && p->range_end && p->index == p->range_start)
9817 {
9818 finish = 0;
9819 p->prev = 0;
9820 }
9821 p = p->next;
9822 if (!p)
9823 break;
16595a1f 9824 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 9825 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9826 p->stack = constructor_stack;
9827 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9828 p->index = p->range_start;
9829 }
9830
9831 if (!finish)
9832 constructor_range_stack = range_stack;
9833 continue;
8b6a5902
JJ
9834 }
9835
3e4093b6 9836 break;
8b6a5902
JJ
9837 }
9838
3e4093b6
RS
9839 constructor_range_stack = 0;
9840}
9841\f
9f0e2d86
ZW
9842/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9843 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9844 an ASM_EXPR node). */
3e4093b6 9845tree
9f0e2d86 9846build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9847{
6de9cd9a
DN
9848 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9849 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9850 return add_stmt (args);
8b6a5902
JJ
9851}
9852
9f0e2d86
ZW
9853/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9854 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9855 SIMPLE indicates whether there was anything at all after the
9856 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9857 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9858tree
c2255bc4 9859build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9860 tree clobbers, tree labels, bool simple)
e5e809f4 9861{
3e4093b6 9862 tree tail;
9f0e2d86 9863 tree args;
6de9cd9a
DN
9864 int i;
9865 const char *constraint;
74f0c611 9866 const char **oconstraints;
6de9cd9a 9867 bool allows_mem, allows_reg, is_inout;
74f0c611 9868 int ninputs, noutputs;
6de9cd9a
DN
9869
9870 ninputs = list_length (inputs);
9871 noutputs = list_length (outputs);
74f0c611
RH
9872 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9873
1c384bf1 9874 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9875
6de9cd9a
DN
9876 /* Remove output conversions that change the type but not the mode. */
9877 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9878 {
3e4093b6 9879 tree output = TREE_VALUE (tail);
74f0c611 9880
f9c59f7e 9881 output = c_fully_fold (output, false, NULL, true);
eadd3d0d 9882
74f0c611
RH
9883 /* ??? Really, this should not be here. Users should be using a
9884 proper lvalue, dammit. But there's a long history of using casts
9885 in the output operands. In cases like longlong.h, this becomes a
9886 primitive form of typechecking -- if the cast can be removed, then
9887 the output operand had a type of the proper width; otherwise we'll
9888 get an error. Gross, but ... */
3e4093b6 9889 STRIP_NOPS (output);
74f0c611 9890
7bd11157 9891 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9892 output = error_mark_node;
8b6a5902 9893
5544530a
PB
9894 if (output != error_mark_node
9895 && (TREE_READONLY (output)
9896 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9897 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9898 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9899 readonly_error (loc, output, lv_asm);
5544530a 9900
6de9cd9a 9901 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9902 oconstraints[i] = constraint;
9903
9904 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9905 &allows_mem, &allows_reg, &is_inout))
9906 {
9907 /* If the operand is going to end up in memory,
9908 mark it addressable. */
9909 if (!allows_reg && !c_mark_addressable (output))
9910 output = error_mark_node;
bae5cddf
JJ
9911 if (!(!allows_reg && allows_mem)
9912 && output != error_mark_node
9913 && VOID_TYPE_P (TREE_TYPE (output)))
9914 {
9915 error_at (loc, "invalid use of void expression");
9916 output = error_mark_node;
9917 }
74f0c611
RH
9918 }
9919 else
c22cacf3 9920 output = error_mark_node;
3e4093b6 9921
74f0c611 9922 TREE_VALUE (tail) = output;
8b6a5902 9923 }
3e4093b6 9924
74f0c611
RH
9925 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9926 {
9927 tree input;
9928
9929 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9930 input = TREE_VALUE (tail);
9931
74f0c611
RH
9932 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9933 oconstraints, &allows_mem, &allows_reg))
9934 {
9935 /* If the operand is going to end up in memory,
9936 mark it addressable. */
b4c33883
AP
9937 if (!allows_reg && allows_mem)
9938 {
f9c59f7e 9939 input = c_fully_fold (input, false, NULL, true);
eadd3d0d 9940
b4c33883
AP
9941 /* Strip the nops as we allow this case. FIXME, this really
9942 should be rejected or made deprecated. */
9943 STRIP_NOPS (input);
9944 if (!c_mark_addressable (input))
9945 input = error_mark_node;
bae5cddf 9946 }
eadd3d0d 9947 else
bae5cddf 9948 {
eadd3d0d
JJ
9949 struct c_expr expr;
9950 memset (&expr, 0, sizeof (expr));
9951 expr.value = input;
267bac10 9952 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9953 input = c_fully_fold (expr.value, false, NULL);
9954
9955 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9956 {
9957 error_at (loc, "invalid use of void expression");
9958 input = error_mark_node;
9959 }
bae5cddf 9960 }
74f0c611
RH
9961 }
9962 else
9963 input = error_mark_node;
9964
9965 TREE_VALUE (tail) = input;
9966 }
3e4093b6 9967
1c384bf1
RH
9968 /* ASMs with labels cannot have outputs. This should have been
9969 enforced by the parser. */
9970 gcc_assert (outputs == NULL || labels == NULL);
9971
9972 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9973
5544530a
PB
9974 /* asm statements without outputs, including simple ones, are treated
9975 as volatile. */
9976 ASM_INPUT_P (args) = simple;
9977 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9978
9f0e2d86 9979 return args;
e5e809f4 9980}
3e4093b6 9981\f
c2255bc4
AH
9982/* Generate a goto statement to LABEL. LOC is the location of the
9983 GOTO. */
506e2710
RH
9984
9985tree
c2255bc4 9986c_finish_goto_label (location_t loc, tree label)
506e2710 9987{
e1b7793c 9988 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9989 if (!decl)
9990 return NULL_TREE;
506e2710 9991 TREE_USED (decl) = 1;
c2255bc4 9992 {
7fef86d3 9993 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
c2255bc4
AH
9994 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9995 SET_EXPR_LOCATION (t, loc);
9996 return add_stmt (t);
9997 }
506e2710
RH
9998}
9999
c2255bc4
AH
10000/* Generate a computed goto statement to EXPR. LOC is the location of
10001 the GOTO. */
506e2710
RH
10002
10003tree
c2255bc4 10004c_finish_goto_ptr (location_t loc, tree expr)
506e2710 10005{
c2255bc4 10006 tree t;
c1771a20 10007 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 10008 expr = c_fully_fold (expr, false, NULL);
506e2710 10009 expr = convert (ptr_type_node, expr);
c2255bc4
AH
10010 t = build1 (GOTO_EXPR, void_type_node, expr);
10011 SET_EXPR_LOCATION (t, loc);
10012 return add_stmt (t);
506e2710
RH
10013}
10014
5088b058 10015/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 10016 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
10017 the location of the return statement, or the location of the expression,
10018 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 10019 is the original type of RETVAL. */
de520661 10020
506e2710 10021tree
c2255bc4 10022c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 10023{
0c9b182b
JJ
10024 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10025 bool no_warning = false;
928c19bb 10026 bool npc = false;
3e4093b6 10027
de8ddd5f
MP
10028 /* Use the expansion point to handle cases such as returning NULL
10029 in a function returning void. */
10030 source_location xloc = expansion_point_location_if_in_system_header (loc);
10031
3e4093b6 10032 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 10033 warning_at (xloc, 0,
c2255bc4 10034 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 10035
928c19bb
JM
10036 if (retval)
10037 {
8ce94e44 10038 tree semantic_type = NULL_TREE;
928c19bb 10039 npc = null_pointer_constant_p (retval);
8ce94e44
JM
10040 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10041 {
10042 semantic_type = TREE_TYPE (retval);
10043 retval = TREE_OPERAND (retval, 0);
10044 }
928c19bb 10045 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
10046 if (semantic_type)
10047 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
10048 }
10049
3e4093b6 10050 if (!retval)
de520661 10051 {
3e4093b6
RS
10052 current_function_returns_null = 1;
10053 if ((warn_return_type || flag_isoc99)
3fa8871b 10054 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 10055 {
94c40e19 10056 bool warned_here;
35aff4fb 10057 if (flag_isoc99)
94c40e19
DM
10058 warned_here = pedwarn
10059 (loc, 0,
10060 "%<return%> with no value, in function returning non-void");
35aff4fb 10061 else
94c40e19
DM
10062 warned_here = warning_at
10063 (loc, OPT_Wreturn_type,
10064 "%<return%> with no value, in function returning non-void");
0c9b182b 10065 no_warning = true;
94c40e19
DM
10066 if (warned_here)
10067 inform (DECL_SOURCE_LOCATION (current_function_decl),
10068 "declared here");
0c9b182b 10069 }
400fbf9f 10070 }
3fa8871b 10071 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
de520661 10072 {
3e4093b6 10073 current_function_returns_null = 1;
94c40e19 10074 bool warned_here;
2397c575 10075 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19
DM
10076 warned_here = pedwarn
10077 (xloc, 0,
10078 "%<return%> with a value, in function returning void");
b8698a0f 10079 else
94c40e19
DM
10080 warned_here = pedwarn
10081 (xloc, OPT_Wpedantic, "ISO C forbids "
10082 "%<return%> with expression, in function returning void");
10083 if (warned_here)
10084 inform (DECL_SOURCE_LOCATION (current_function_decl),
10085 "declared here");
de520661 10086 }
3e4093b6 10087 else
de520661 10088 {
68fca595
MP
10089 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10090 retval, origtype, ic_return,
c2255bc4 10091 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
10092 tree res = DECL_RESULT (current_function_decl);
10093 tree inner;
9feb29df 10094 bool save;
3e4093b6
RS
10095
10096 current_function_returns_value = 1;
10097 if (t == error_mark_node)
506e2710 10098 return NULL_TREE;
3e4093b6 10099
9feb29df
JJ
10100 save = in_late_binary_op;
10101 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
10102 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10103 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10104 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10105 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
45b2222a 10106 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
9feb29df 10107 in_late_binary_op = true;
3e4093b6 10108 inner = t = convert (TREE_TYPE (res), t);
9feb29df 10109 in_late_binary_op = save;
3e4093b6
RS
10110
10111 /* Strip any conversions, additions, and subtractions, and see if
10112 we are returning the address of a local variable. Warn if so. */
10113 while (1)
8b6a5902 10114 {
3e4093b6 10115 switch (TREE_CODE (inner))
8b6a5902 10116 {
849421a3
JJ
10117 CASE_CONVERT:
10118 case NON_LVALUE_EXPR:
3e4093b6 10119 case PLUS_EXPR:
849421a3 10120 case POINTER_PLUS_EXPR:
3e4093b6
RS
10121 inner = TREE_OPERAND (inner, 0);
10122 continue;
10123
10124 case MINUS_EXPR:
10125 /* If the second operand of the MINUS_EXPR has a pointer
10126 type (or is converted from it), this may be valid, so
10127 don't give a warning. */
10128 {
10129 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 10130
3f75a254 10131 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
10132 && (CONVERT_EXPR_P (op1)
10133 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 10134 op1 = TREE_OPERAND (op1, 0);
8b6a5902 10135
3e4093b6
RS
10136 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10137 break;
8b6a5902 10138
3e4093b6
RS
10139 inner = TREE_OPERAND (inner, 0);
10140 continue;
10141 }
400fbf9f 10142
3e4093b6
RS
10143 case ADDR_EXPR:
10144 inner = TREE_OPERAND (inner, 0);
c2f4acb7 10145
6615c446 10146 while (REFERENCE_CLASS_P (inner)
22d03525 10147 && !INDIRECT_REF_P (inner))
3e4093b6 10148 inner = TREE_OPERAND (inner, 0);
8b6a5902 10149
a2f1f4c3 10150 if (DECL_P (inner)
3f75a254
JM
10151 && !DECL_EXTERNAL (inner)
10152 && !TREE_STATIC (inner)
3e4093b6 10153 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
10154 {
10155 if (TREE_CODE (inner) == LABEL_DECL)
10156 warning_at (loc, OPT_Wreturn_local_addr,
10157 "function returns address of label");
10158 else
b4dfdc11
MG
10159 {
10160 warning_at (loc, OPT_Wreturn_local_addr,
10161 "function returns address of local variable");
10162 tree zero = build_zero_cst (TREE_TYPE (res));
10163 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10164 }
19fc9faa 10165 }
3e4093b6 10166 break;
8b6a5902 10167
3e4093b6
RS
10168 default:
10169 break;
10170 }
de520661 10171
3e4093b6
RS
10172 break;
10173 }
10174
53fb4de3 10175 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 10176 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
10177
10178 if (warn_sequence_point)
10179 verify_sequence_points (retval);
de520661 10180 }
8b6a5902 10181
c2255bc4 10182 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
10183 TREE_NO_WARNING (ret_stmt) |= no_warning;
10184 return add_stmt (ret_stmt);
de520661 10185}
3e4093b6
RS
10186\f
10187struct c_switch {
604f5adf
ILT
10188 /* The SWITCH_EXPR being built. */
10189 tree switch_expr;
a6c0a76c 10190
89dbed81 10191 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
10192 default conversion is applied. */
10193 tree orig_type;
10194
3e4093b6
RS
10195 /* A splay-tree mapping the low element of a case range to the high
10196 element, or NULL_TREE if there is no high element. Used to
10197 determine whether or not a new case label duplicates an old case
10198 label. We need a tree, rather than simply a hash table, because
10199 of the GNU case range extension. */
10200 splay_tree cases;
a6c0a76c 10201
e1b7793c
ILT
10202 /* The bindings at the point of the switch. This is used for
10203 warnings crossing decls when branching to a case label. */
10204 struct c_spot_bindings *bindings;
187230a7 10205
3e4093b6
RS
10206 /* The next node on the stack. */
10207 struct c_switch *next;
b155cfd9
MP
10208
10209 /* Remember whether the controlling expression had boolean type
10210 before integer promotions for the sake of -Wswitch-bool. */
10211 bool bool_cond_p;
10212
10213 /* Remember whether there was a case value that is outside the
10214 range of the ORIG_TYPE. */
10215 bool outside_range_p;
3e4093b6 10216};
400fbf9f 10217
3e4093b6
RS
10218/* A stack of the currently active switch statements. The innermost
10219 switch statement is on the top of the stack. There is no need to
10220 mark the stack for garbage collection because it is only active
10221 during the processing of the body of a function, and we never
10222 collect at that point. */
de520661 10223
506e2710 10224struct c_switch *c_switch_stack;
de520661 10225
3e4093b6 10226/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 10227 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 10228 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 10229 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 10230
3e4093b6 10231tree
c2255bc4
AH
10232c_start_case (location_t switch_loc,
10233 location_t switch_cond_loc,
fedfecef 10234 tree exp, bool explicit_cast_p)
de520661 10235{
c58e8676 10236 tree orig_type = error_mark_node;
b155cfd9 10237 bool bool_cond_p = false;
3e4093b6 10238 struct c_switch *cs;
2f6e4e97 10239
3e4093b6 10240 if (exp != error_mark_node)
de520661 10241 {
3e4093b6
RS
10242 orig_type = TREE_TYPE (exp);
10243
c58e8676 10244 if (!INTEGRAL_TYPE_P (orig_type))
de520661 10245 {
c58e8676
VR
10246 if (orig_type != error_mark_node)
10247 {
c2255bc4 10248 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
10249 orig_type = error_mark_node;
10250 }
3e4093b6 10251 exp = integer_zero_node;
de520661 10252 }
3e4093b6 10253 else
de520661 10254 {
c58e8676 10255 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
10256 tree e = exp;
10257
10258 /* Warn if the condition has boolean value. */
10259 while (TREE_CODE (e) == COMPOUND_EXPR)
10260 e = TREE_OPERAND (e, 1);
10261
10262 if ((TREE_CODE (type) == BOOLEAN_TYPE
10263 || truth_value_p (TREE_CODE (e)))
10264 /* Explicit cast to int suppresses this warning. */
10265 && !(TREE_CODE (type) == INTEGER_TYPE
10266 && explicit_cast_p))
b155cfd9 10267 bool_cond_p = true;
8b6a5902 10268
8400e75e 10269 if (!in_system_header_at (input_location)
3e4093b6
RS
10270 && (type == long_integer_type_node
10271 || type == long_unsigned_type_node))
c2255bc4
AH
10272 warning_at (switch_cond_loc,
10273 OPT_Wtraditional, "%<long%> switch expression not "
10274 "converted to %<int%> in ISO C");
8b6a5902 10275
928c19bb 10276 exp = c_fully_fold (exp, false, NULL);
3e4093b6 10277 exp = default_conversion (exp);
ca085fd7
MLI
10278
10279 if (warn_sequence_point)
10280 verify_sequence_points (exp);
3e4093b6
RS
10281 }
10282 }
10283
604f5adf 10284 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 10285 cs = XNEW (struct c_switch);
9e851845 10286 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
c2255bc4 10287 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 10288 cs->orig_type = orig_type;
3e4093b6 10289 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 10290 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
10291 cs->bool_cond_p = bool_cond_p;
10292 cs->outside_range_p = false;
506e2710
RH
10293 cs->next = c_switch_stack;
10294 c_switch_stack = cs;
3e4093b6 10295
604f5adf 10296 return add_stmt (cs->switch_expr);
3e4093b6
RS
10297}
10298
c2255bc4 10299/* Process a case label at location LOC. */
3e4093b6
RS
10300
10301tree
c2255bc4 10302do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
10303{
10304 tree label = NULL_TREE;
10305
17cede2e
JM
10306 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10307 {
10308 low_value = c_fully_fold (low_value, false, NULL);
10309 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 10310 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
10311 "case label is not an integer constant expression");
10312 }
10313
10314 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10315 {
10316 high_value = c_fully_fold (high_value, false, NULL);
10317 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 10318 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
10319 "case label is not an integer constant expression");
10320 }
10321
e1b7793c 10322 if (c_switch_stack == NULL)
187230a7
JM
10323 {
10324 if (low_value)
e1b7793c 10325 error_at (loc, "case label not within a switch statement");
187230a7 10326 else
e1b7793c
ILT
10327 error_at (loc, "%<default%> label not within a switch statement");
10328 return NULL_TREE;
187230a7 10329 }
de520661 10330
e1b7793c
ILT
10331 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10332 EXPR_LOCATION (c_switch_stack->switch_expr),
10333 loc))
10334 return NULL_TREE;
10335
10336 label = c_add_case_label (loc, c_switch_stack->cases,
10337 SWITCH_COND (c_switch_stack->switch_expr),
10338 c_switch_stack->orig_type,
b155cfd9
MP
10339 low_value, high_value,
10340 &c_switch_stack->outside_range_p);
e1b7793c
ILT
10341 if (label == error_mark_node)
10342 label = NULL_TREE;
3e4093b6
RS
10343 return label;
10344}
de520661 10345
083e891e
MP
10346/* Finish the switch statement. TYPE is the original type of the
10347 controlling expression of the switch, or NULL_TREE. */
de520661 10348
3e4093b6 10349void
083e891e 10350c_finish_case (tree body, tree type)
3e4093b6 10351{
506e2710 10352 struct c_switch *cs = c_switch_stack;
fbc315db 10353 location_t switch_location;
3e4093b6 10354
604f5adf 10355 SWITCH_BODY (cs->switch_expr) = body;
325c3691 10356
6de9cd9a 10357 /* Emit warnings as needed. */
c2255bc4 10358 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 10359 c_do_switch_warnings (cs->cases, switch_location,
083e891e 10360 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
10361 SWITCH_COND (cs->switch_expr),
10362 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 10363
3e4093b6 10364 /* Pop the stack. */
506e2710 10365 c_switch_stack = cs->next;
3e4093b6 10366 splay_tree_delete (cs->cases);
e1b7793c 10367 c_release_switch_bindings (cs->bindings);
5d038c4c 10368 XDELETE (cs);
de520661 10369}
325c3691 10370\f
506e2710
RH
10371/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10372 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
99cd9857 10373 may be null. */
325c3691 10374
9e51cf9d 10375void
506e2710 10376c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
99cd9857 10377 tree else_block)
325c3691 10378{
506e2710 10379 tree stmt;
325c3691 10380
2214de30 10381 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10382 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10383 add_stmt (stmt);
325c3691
RH
10384}
10385
506e2710
RH
10386/* Emit a general-purpose loop construct. START_LOCUS is the location of
10387 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10388 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10389 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10390 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10391
10392void
506e2710
RH
10393c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10394 tree blab, tree clab, bool cond_is_first)
325c3691 10395{
506e2710
RH
10396 tree entry = NULL, exit = NULL, t;
10397
28af952a
RS
10398 /* If the condition is zero don't generate a loop construct. */
10399 if (cond && integer_zerop (cond))
10400 {
10401 if (cond_is_first)
10402 {
10403 t = build_and_jump (&blab);
10404 SET_EXPR_LOCATION (t, start_locus);
10405 add_stmt (t);
10406 }
10407 }
10408 else
506e2710
RH
10409 {
10410 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10411
506e2710 10412 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10413 out of the loop, or to the top of it. If there's no exit condition,
10414 then we just build a jump back to the top. */
506e2710 10415 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10416
28af952a 10417 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10418 {
10419 /* Canonicalize the loop condition to the end. This means
10420 generating a branch to the loop condition. Reuse the
10421 continue label, if possible. */
10422 if (cond_is_first)
10423 {
10424 if (incr || !clab)
10425 {
10426 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10427 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10428 }
10429 else
10430 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10431 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10432 add_stmt (t);
10433 }
10434
506e2710 10435 t = build_and_jump (&blab);
506e2710 10436 if (cond_is_first)
db3927fb
AH
10437 exit = fold_build3_loc (start_locus,
10438 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10439 else
db3927fb
AH
10440 exit = fold_build3_loc (input_location,
10441 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10442 }
fc402eec
AA
10443 else
10444 {
10445 /* For the backward-goto's location of an unconditional loop
10446 use the beginning of the body, or, if there is none, the
10447 top of the loop. */
10448 location_t loc = EXPR_LOCATION (expr_first (body));
10449 if (loc == UNKNOWN_LOCATION)
10450 loc = start_locus;
10451 SET_EXPR_LOCATION (exit, loc);
10452 }
c22cacf3 10453
506e2710
RH
10454 add_stmt (top);
10455 }
c22cacf3 10456
506e2710
RH
10457 if (body)
10458 add_stmt (body);
10459 if (clab)
10460 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10461 if (incr)
10462 add_stmt (incr);
10463 if (entry)
10464 add_stmt (entry);
10465 if (exit)
10466 add_stmt (exit);
10467 if (blab)
10468 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10469}
325c3691
RH
10470
10471tree
c2255bc4 10472c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10473{
089efaa4 10474 bool skip;
506e2710 10475 tree label = *label_p;
325c3691 10476
089efaa4
ILT
10477 /* In switch statements break is sometimes stylistically used after
10478 a return statement. This can lead to spurious warnings about
10479 control reaching the end of a non-void function when it is
10480 inlined. Note that we are calling block_may_fallthru with
10481 language specific tree nodes; this works because
10482 block_may_fallthru returns true when given something it does not
10483 understand. */
10484 skip = !block_may_fallthru (cur_stmt_list);
10485
506e2710 10486 if (!label)
089efaa4
ILT
10487 {
10488 if (!skip)
c2255bc4 10489 *label_p = label = create_artificial_label (loc);
089efaa4 10490 }
953ff289
DN
10491 else if (TREE_CODE (label) == LABEL_DECL)
10492 ;
10493 else switch (TREE_INT_CST_LOW (label))
506e2710 10494 {
953ff289 10495 case 0:
506e2710 10496 if (is_break)
c2255bc4 10497 error_at (loc, "break statement not within loop or switch");
506e2710 10498 else
c2255bc4 10499 error_at (loc, "continue statement not within a loop");
506e2710 10500 return NULL_TREE;
953ff289
DN
10501
10502 case 1:
10503 gcc_assert (is_break);
c2255bc4 10504 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10505 return NULL_TREE;
10506
c02065fc
AH
10507 case 2:
10508 if (is_break)
10509 error ("break statement within %<#pragma simd%> loop body");
10510 else
10511 error ("continue statement within %<#pragma simd%> loop body");
10512 return NULL_TREE;
10513
953ff289
DN
10514 default:
10515 gcc_unreachable ();
506e2710 10516 }
325c3691 10517
089efaa4
ILT
10518 if (skip)
10519 return NULL_TREE;
10520
2e28e797
JH
10521 if (!is_break)
10522 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10523
53fb4de3 10524 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10525}
10526
506e2710 10527/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10528
10529static void
c2255bc4 10530emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10531{
e6b5a630
RH
10532 if (expr == error_mark_node)
10533 ;
10534 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10535 {
10536 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10537 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10538 }
789eadcd
MP
10539 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10540 {
10541 tree r = expr;
10542 location_t cloc = loc;
10543 while (TREE_CODE (r) == COMPOUND_EXPR)
10544 {
10545 if (EXPR_HAS_LOCATION (r))
10546 cloc = EXPR_LOCATION (r);
10547 r = TREE_OPERAND (r, 1);
10548 }
10549 if (!TREE_SIDE_EFFECTS (r)
10550 && !VOID_TYPE_P (TREE_TYPE (r))
10551 && !CONVERT_EXPR_P (r)
cc28fc7f 10552 && !TREE_NO_WARNING (r)
789eadcd
MP
10553 && !TREE_NO_WARNING (expr))
10554 warning_at (cloc, OPT_Wunused_value,
10555 "right-hand operand of comma expression has no effect");
10556 }
27f33b15 10557 else
c2255bc4 10558 warn_if_unused_value (expr, loc);
3a5b9284
RH
10559}
10560
506e2710 10561/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10562 diagnostics, but do not call ADD_STMT. LOC is the location of the
10563 statement. */
3a5b9284 10564
506e2710 10565tree
c2255bc4 10566c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10567{
056928b2
JJ
10568 tree exprv;
10569
3a5b9284 10570 if (!expr)
506e2710 10571 return NULL_TREE;
3a5b9284 10572
928c19bb
JM
10573 expr = c_fully_fold (expr, false, NULL);
10574
3a5b9284
RH
10575 if (warn_sequence_point)
10576 verify_sequence_points (expr);
10577
10578 if (TREE_TYPE (expr) != error_mark_node
10579 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10580 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10581 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10582
10583 /* If we're not processing a statement expression, warn about unused values.
10584 Warnings for statement expressions will be emitted later, once we figure
10585 out which is the result. */
10586 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10587 && warn_unused_value)
d06f8b75 10588 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10589
056928b2
JJ
10590 exprv = expr;
10591 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10592 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10593 while (CONVERT_EXPR_P (exprv))
10594 exprv = TREE_OPERAND (exprv, 0);
10595 if (DECL_P (exprv)
10596 || handled_component_p (exprv)
10597 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10598 mark_exp_read (exprv);
fa8351f8 10599
3a5b9284
RH
10600 /* If the expression is not of a type to which we cannot assign a line
10601 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10602 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10603 {
10604 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10605 SET_EXPR_LOCATION (expr, loc);
10606 }
506e2710
RH
10607
10608 return expr;
10609}
10610
c2255bc4
AH
10611/* Emit an expression as a statement. LOC is the location of the
10612 expression. */
506e2710
RH
10613
10614tree
c2255bc4 10615c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10616{
10617 if (expr)
c2255bc4 10618 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10619 else
10620 return NULL;
3a5b9284
RH
10621}
10622
10623/* Do the opposite and emit a statement as an expression. To begin,
10624 create a new binding level and return it. */
325c3691
RH
10625
10626tree
10627c_begin_stmt_expr (void)
10628{
10629 tree ret;
10630
10631 /* We must force a BLOCK for this level so that, if it is not expanded
10632 later, there is a way to turn off the entire subtree of blocks that
10633 are contained in it. */
10634 keep_next_level ();
10635 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10636
10637 c_bindings_start_stmt_expr (c_switch_stack == NULL
10638 ? NULL
10639 : c_switch_stack->bindings);
325c3691
RH
10640
10641 /* Mark the current statement list as belonging to a statement list. */
10642 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10643
10644 return ret;
10645}
10646
c2255bc4
AH
10647/* LOC is the location of the compound statement to which this body
10648 belongs. */
10649
325c3691 10650tree
c2255bc4 10651c_finish_stmt_expr (location_t loc, tree body)
325c3691 10652{
3a5b9284 10653 tree last, type, tmp, val;
325c3691
RH
10654 tree *last_p;
10655
c2255bc4 10656 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10657
10658 c_bindings_end_stmt_expr (c_switch_stack == NULL
10659 ? NULL
10660 : c_switch_stack->bindings);
325c3691 10661
3a5b9284
RH
10662 /* Locate the last statement in BODY. See c_end_compound_stmt
10663 about always returning a BIND_EXPR. */
10664 last_p = &BIND_EXPR_BODY (body);
10665 last = BIND_EXPR_BODY (body);
10666
10667 continue_searching:
325c3691
RH
10668 if (TREE_CODE (last) == STATEMENT_LIST)
10669 {
3a5b9284
RH
10670 tree_stmt_iterator i;
10671
10672 /* This can happen with degenerate cases like ({ }). No value. */
10673 if (!TREE_SIDE_EFFECTS (last))
10674 return body;
10675
10676 /* If we're supposed to generate side effects warnings, process
10677 all of the statements except the last. */
27f33b15 10678 if (warn_unused_value)
325c3691 10679 {
3a5b9284 10680 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10681 {
10682 location_t tloc;
10683 tree t = tsi_stmt (i);
10684
10685 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10686 emit_side_effect_warnings (tloc, t);
10687 }
325c3691
RH
10688 }
10689 else
3a5b9284
RH
10690 i = tsi_last (last);
10691 last_p = tsi_stmt_ptr (i);
10692 last = *last_p;
325c3691
RH
10693 }
10694
3a5b9284
RH
10695 /* If the end of the list is exception related, then the list was split
10696 by a call to push_cleanup. Continue searching. */
10697 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10698 || TREE_CODE (last) == TRY_CATCH_EXPR)
10699 {
10700 last_p = &TREE_OPERAND (last, 0);
10701 last = *last_p;
10702 goto continue_searching;
10703 }
10704
26d8af35
JM
10705 if (last == error_mark_node)
10706 return last;
10707
3a5b9284
RH
10708 /* In the case that the BIND_EXPR is not necessary, return the
10709 expression out from inside it. */
26d8af35
JM
10710 if (last == BIND_EXPR_BODY (body)
10711 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10712 {
928c19bb
JM
10713 /* Even if this looks constant, do not allow it in a constant
10714 expression. */
e5a94231 10715 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10716 /* Do not warn if the return value of a statement expression is
10717 unused. */
928c19bb 10718 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10719 return last;
10720 }
325c3691
RH
10721
10722 /* Extract the type of said expression. */
10723 type = TREE_TYPE (last);
325c3691 10724
3a5b9284
RH
10725 /* If we're not returning a value at all, then the BIND_EXPR that
10726 we already have is a fine expression to return. */
10727 if (!type || VOID_TYPE_P (type))
10728 return body;
10729
10730 /* Now that we've located the expression containing the value, it seems
10731 silly to make voidify_wrapper_expr repeat the process. Create a
10732 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10733 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10734
10735 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10736 tree_expr_nonnegative_p giving up immediately. */
10737 val = last;
10738 if (TREE_CODE (val) == NOP_EXPR
10739 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10740 val = TREE_OPERAND (val, 0);
10741
53fb4de3 10742 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10743 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10744
c2255bc4
AH
10745 {
10746 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10747 SET_EXPR_LOCATION (t, loc);
10748 return t;
10749 }
325c3691
RH
10750}
10751\f
10752/* Begin and end compound statements. This is as simple as pushing
10753 and popping new statement lists from the tree. */
10754
10755tree
10756c_begin_compound_stmt (bool do_scope)
10757{
10758 tree stmt = push_stmt_list ();
10759 if (do_scope)
4dfa0342 10760 push_scope ();
325c3691
RH
10761 return stmt;
10762}
10763
c2255bc4
AH
10764/* End a compound statement. STMT is the statement. LOC is the
10765 location of the compound statement-- this is usually the location
10766 of the opening brace. */
10767
325c3691 10768tree
c2255bc4 10769c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10770{
10771 tree block = NULL;
10772
10773 if (do_scope)
10774 {
10775 if (c_dialect_objc ())
10776 objc_clear_super_receiver ();
10777 block = pop_scope ();
10778 }
10779
10780 stmt = pop_stmt_list (stmt);
c2255bc4 10781 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10782
10783 /* If this compound statement is nested immediately inside a statement
10784 expression, then force a BIND_EXPR to be created. Otherwise we'll
10785 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10786 STATEMENT_LISTs merge, and thus we can lose track of what statement
10787 was really last. */
38e01f9e 10788 if (building_stmt_list_p ()
325c3691
RH
10789 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10790 && TREE_CODE (stmt) != BIND_EXPR)
10791 {
53fb4de3 10792 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10793 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10794 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10795 }
10796
10797 return stmt;
10798}
5a508662
RH
10799
10800/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10801 when the current scope is exited. EH_ONLY is true when this is not
10802 meant to apply to normal control flow transfer. */
10803
10804void
c2255bc4 10805push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10806{
3a5b9284
RH
10807 enum tree_code code;
10808 tree stmt, list;
10809 bool stmt_expr;
10810
10811 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10812 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10813 add_stmt (stmt);
3a5b9284
RH
10814 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10815 list = push_stmt_list ();
10816 TREE_OPERAND (stmt, 0) = list;
10817 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10818}
325c3691 10819\f
9f47c7e5
IE
10820/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10821 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10822
10823static tree
10824build_vec_cmp (tree_code code, tree type,
10825 tree arg0, tree arg1)
10826{
10827 tree zero_vec = build_zero_cst (type);
10828 tree minus_one_vec = build_minus_one_cst (type);
10829 tree cmp_type = build_same_sized_truth_vector_type (type);
10830 tree cmp = build2 (code, cmp_type, arg0, arg1);
10831 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10832}
10833
3e4093b6
RS
10834/* Build a binary-operation expression without default conversions.
10835 CODE is the kind of expression to build.
ba47d38d 10836 LOCATION is the operator's location.
3e4093b6
RS
10837 This function differs from `build' in several ways:
10838 the data type of the result is computed and recorded in it,
10839 warnings are generated if arg data types are invalid,
10840 special handling for addition and subtraction of pointers is known,
10841 and some optimization is done (operations on narrow ints
10842 are done in the narrower type when that gives the same result).
10843 Constant folding is also done before the result is returned.
de520661 10844
3e4093b6
RS
10845 Note that the operands will never have enumeral types, or function
10846 or array types, because either they will have the default conversions
10847 performed or they have both just been converted to some other type in which
10848 the arithmetic is to be done. */
10849
10850tree
ba47d38d 10851build_binary_op (location_t location, enum tree_code code,
30af3a2b 10852 tree orig_op0, tree orig_op1, bool convert_p)
de520661 10853{
8ce94e44
JM
10854 tree type0, type1, orig_type0, orig_type1;
10855 tree eptype;
3e4093b6
RS
10856 enum tree_code code0, code1;
10857 tree op0, op1;
c9f9eb5d 10858 tree ret = error_mark_node;
4de67c26 10859 const char *invalid_op_diag;
4d84fe7c 10860 bool op0_int_operands, op1_int_operands;
928c19bb 10861 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10862
3e4093b6
RS
10863 /* Expression code to give to the expression when it is built.
10864 Normally this is CODE, which is what the caller asked for,
10865 but in some special cases we change it. */
10866 enum tree_code resultcode = code;
8b6a5902 10867
3e4093b6
RS
10868 /* Data type in which the computation is to be performed.
10869 In the simplest cases this is the common type of the arguments. */
10870 tree result_type = NULL;
10871
8ce94e44
JM
10872 /* When the computation is in excess precision, the type of the
10873 final EXCESS_PRECISION_EXPR. */
2d2e923f 10874 tree semantic_result_type = NULL;
8ce94e44 10875
3e4093b6
RS
10876 /* Nonzero means operands have already been type-converted
10877 in whatever way is necessary.
10878 Zero means they need to be converted to RESULT_TYPE. */
10879 int converted = 0;
10880
10881 /* Nonzero means create the expression with this type, rather than
10882 RESULT_TYPE. */
3fa8871b 10883 tree build_type = NULL_TREE;
3e4093b6
RS
10884
10885 /* Nonzero means after finally constructing the expression
10886 convert it to this type. */
3fa8871b 10887 tree final_type = NULL_TREE;
3e4093b6
RS
10888
10889 /* Nonzero if this is an operation like MIN or MAX which can
10890 safely be computed in short if both args are promoted shorts.
10891 Also implies COMMON.
10892 -1 indicates a bitwise operation; this makes a difference
10893 in the exact conditions for when it is safe to do the operation
10894 in a narrower mode. */
10895 int shorten = 0;
10896
10897 /* Nonzero if this is a comparison operation;
10898 if both args are promoted shorts, compare the original shorts.
10899 Also implies COMMON. */
10900 int short_compare = 0;
10901
10902 /* Nonzero if this is a right-shift operation, which can be computed on the
10903 original short and then promoted if the operand is a promoted short. */
10904 int short_shift = 0;
10905
10906 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10907 int common = 0;
10908
58393038
ZL
10909 /* True means types are compatible as far as ObjC is concerned. */
10910 bool objc_ok;
10911
8ce94e44
JM
10912 /* True means this is an arithmetic operation that may need excess
10913 precision. */
10914 bool may_need_excess_precision;
10915
180f8dbb
JM
10916 /* True means this is a boolean operation that converts both its
10917 operands to truth-values. */
10918 bool boolean_op = false;
10919
de5a5fa1
MP
10920 /* Remember whether we're doing / or %. */
10921 bool doing_div_or_mod = false;
10922
10923 /* Remember whether we're doing << or >>. */
10924 bool doing_shift = false;
10925
10926 /* Tree holding instrumentation expression. */
10927 tree instrument_expr = NULL;
10928
ba47d38d
AH
10929 if (location == UNKNOWN_LOCATION)
10930 location = input_location;
10931
4d84fe7c
JM
10932 op0 = orig_op0;
10933 op1 = orig_op1;
10934
10935 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10936 if (op0_int_operands)
10937 op0 = remove_c_maybe_const_expr (op0);
10938 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10939 if (op1_int_operands)
10940 op1 = remove_c_maybe_const_expr (op1);
10941 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10942 if (int_operands)
10943 {
10944 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10945 && TREE_CODE (orig_op1) == INTEGER_CST);
10946 int_const = (int_const_or_overflow
10947 && !TREE_OVERFLOW (orig_op0)
10948 && !TREE_OVERFLOW (orig_op1));
10949 }
10950 else
10951 int_const = int_const_or_overflow = false;
10952
0e3a99ae 10953 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10954 if (convert_p
31521951 10955 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10956 {
4d84fe7c
JM
10957 op0 = default_conversion (op0);
10958 op1 = default_conversion (op1);
790e9490
RS
10959 }
10960
5e9d6aa4 10961 orig_type0 = type0 = TREE_TYPE (op0);
36536d79 10962
5e9d6aa4 10963 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10964
3e4093b6
RS
10965 /* The expression codes of the data types of the arguments tell us
10966 whether the arguments are integers, floating, pointers, etc. */
10967 code0 = TREE_CODE (type0);
10968 code1 = TREE_CODE (type1);
10969
10970 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10971 STRIP_TYPE_NOPS (op0);
10972 STRIP_TYPE_NOPS (op1);
10973
10974 /* If an error was already reported for one of the arguments,
10975 avoid reporting another error. */
10976
10977 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10978 return error_mark_node;
10979
1807ffc1
MS
10980 if (code0 == POINTER_TYPE
10981 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10982 return error_mark_node;
10983
10984 if (code1 == POINTER_TYPE
10985 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10986 return error_mark_node;
10987
4de67c26
JM
10988 if ((invalid_op_diag
10989 = targetm.invalid_binary_op (code, type0, type1)))
10990 {
ba47d38d 10991 error_at (location, invalid_op_diag);
4de67c26
JM
10992 return error_mark_node;
10993 }
10994
8ce94e44
JM
10995 switch (code)
10996 {
10997 case PLUS_EXPR:
10998 case MINUS_EXPR:
10999 case MULT_EXPR:
11000 case TRUNC_DIV_EXPR:
11001 case CEIL_DIV_EXPR:
11002 case FLOOR_DIV_EXPR:
11003 case ROUND_DIV_EXPR:
11004 case EXACT_DIV_EXPR:
11005 may_need_excess_precision = true;
11006 break;
11007 default:
11008 may_need_excess_precision = false;
11009 break;
11010 }
11011 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11012 {
11013 op0 = TREE_OPERAND (op0, 0);
11014 type0 = TREE_TYPE (op0);
11015 }
11016 else if (may_need_excess_precision
11017 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11018 {
11019 type0 = eptype;
11020 op0 = convert (eptype, op0);
11021 }
11022 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11023 {
11024 op1 = TREE_OPERAND (op1, 0);
11025 type1 = TREE_TYPE (op1);
11026 }
11027 else if (may_need_excess_precision
11028 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11029 {
11030 type1 = eptype;
11031 op1 = convert (eptype, op1);
11032 }
11033
58393038
ZL
11034 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11035
0e3a99ae
AS
11036 /* In case when one of the operands of the binary operation is
11037 a vector and another is a scalar -- convert scalar to vector. */
11038 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11039 {
a212e43f
MG
11040 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11041 true);
f90e8e2e 11042
0e3a99ae
AS
11043 switch (convert_flag)
11044 {
11045 case stv_error:
11046 return error_mark_node;
11047 case stv_firstarg:
11048 {
11049 bool maybe_const = true;
11050 tree sc;
11051 sc = c_fully_fold (op0, false, &maybe_const);
11052 sc = save_expr (sc);
11053 sc = convert (TREE_TYPE (type1), sc);
11054 op0 = build_vector_from_val (type1, sc);
11055 if (!maybe_const)
11056 op0 = c_wrap_maybe_const (op0, true);
11057 orig_type0 = type0 = TREE_TYPE (op0);
11058 code0 = TREE_CODE (type0);
11059 converted = 1;
11060 break;
11061 }
11062 case stv_secondarg:
11063 {
11064 bool maybe_const = true;
11065 tree sc;
11066 sc = c_fully_fold (op1, false, &maybe_const);
11067 sc = save_expr (sc);
11068 sc = convert (TREE_TYPE (type0), sc);
11069 op1 = build_vector_from_val (type0, sc);
11070 if (!maybe_const)
54b9f838 11071 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
11072 orig_type1 = type1 = TREE_TYPE (op1);
11073 code1 = TREE_CODE (type1);
11074 converted = 1;
11075 break;
11076 }
11077 default:
11078 break;
11079 }
11080 }
11081
3e4093b6 11082 switch (code)
de520661 11083 {
3e4093b6
RS
11084 case PLUS_EXPR:
11085 /* Handle the pointer + int case. */
11086 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 11087 {
db3927fb 11088 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
11089 goto return_build_binary_op;
11090 }
3e4093b6 11091 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 11092 {
db3927fb 11093 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
11094 goto return_build_binary_op;
11095 }
fe67cf58 11096 else
3e4093b6
RS
11097 common = 1;
11098 break;
400fbf9f 11099
3e4093b6
RS
11100 case MINUS_EXPR:
11101 /* Subtraction of two similar pointers.
11102 We must subtract them as integers, then divide by object size. */
11103 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 11104 && comp_target_types (location, type0, type1))
c9f9eb5d 11105 {
db3927fb 11106 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
11107 goto return_build_binary_op;
11108 }
3e4093b6
RS
11109 /* Handle pointer minus int. Just like pointer plus int. */
11110 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 11111 {
db3927fb 11112 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
11113 goto return_build_binary_op;
11114 }
3e4093b6
RS
11115 else
11116 common = 1;
11117 break;
8b6a5902 11118
3e4093b6
RS
11119 case MULT_EXPR:
11120 common = 1;
11121 break;
11122
11123 case TRUNC_DIV_EXPR:
11124 case CEIL_DIV_EXPR:
11125 case FLOOR_DIV_EXPR:
11126 case ROUND_DIV_EXPR:
11127 case EXACT_DIV_EXPR:
de5a5fa1 11128 doing_div_or_mod = true;
c9f9eb5d 11129 warn_for_div_by_zero (location, op1);
3e4093b6
RS
11130
11131 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11132 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
11133 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11134 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11135 || code1 == FIXED_POINT_TYPE
3e4093b6 11136 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11137 {
5bed876a
AH
11138 enum tree_code tcode0 = code0, tcode1 = code1;
11139
3a021db2 11140 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 11141 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 11142 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 11143 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 11144
ab22c1fa
CF
11145 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11146 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
11147 resultcode = RDIV_EXPR;
11148 else
11149 /* Although it would be tempting to shorten always here, that
11150 loses on some targets, since the modulo instruction is
11151 undefined if the quotient can't be represented in the
11152 computation mode. We shorten only if unsigned or if
11153 dividing by something we know != -1. */
8df83eae 11154 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 11155 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 11156 && !integer_all_onesp (op1)));
3e4093b6
RS
11157 common = 1;
11158 }
11159 break;
de520661 11160
3e4093b6 11161 case BIT_AND_EXPR:
3e4093b6
RS
11162 case BIT_IOR_EXPR:
11163 case BIT_XOR_EXPR:
11164 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11165 shorten = -1;
9ef0c8d9
AP
11166 /* Allow vector types which are not floating point types. */
11167 else if (code0 == VECTOR_TYPE
11168 && code1 == VECTOR_TYPE
11169 && !VECTOR_FLOAT_TYPE_P (type0)
11170 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
11171 common = 1;
11172 break;
11173
11174 case TRUNC_MOD_EXPR:
11175 case FLOOR_MOD_EXPR:
de5a5fa1 11176 doing_div_or_mod = true;
c9f9eb5d 11177 warn_for_div_by_zero (location, op1);
de520661 11178
5cfd5d9b
AP
11179 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11180 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11181 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11182 common = 1;
11183 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
11184 {
11185 /* Although it would be tempting to shorten always here, that loses
11186 on some targets, since the modulo instruction is undefined if the
11187 quotient can't be represented in the computation mode. We shorten
11188 only if unsigned or if dividing by something we know != -1. */
8df83eae 11189 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 11190 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 11191 && !integer_all_onesp (op1)));
3e4093b6
RS
11192 common = 1;
11193 }
11194 break;
de520661 11195
3e4093b6
RS
11196 case TRUTH_ANDIF_EXPR:
11197 case TRUTH_ORIF_EXPR:
11198 case TRUTH_AND_EXPR:
11199 case TRUTH_OR_EXPR:
11200 case TRUTH_XOR_EXPR:
11201 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
11202 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11203 || code0 == FIXED_POINT_TYPE)
3e4093b6 11204 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
11205 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11206 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11207 {
11208 /* Result of these operations is always an int,
11209 but that does not mean the operands should be
11210 converted to ints! */
11211 result_type = integer_type_node;
a27d595d
JM
11212 if (op0_int_operands)
11213 {
11214 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11215 op0 = remove_c_maybe_const_expr (op0);
11216 }
11217 else
11218 op0 = c_objc_common_truthvalue_conversion (location, op0);
11219 if (op1_int_operands)
11220 {
11221 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11222 op1 = remove_c_maybe_const_expr (op1);
11223 }
11224 else
11225 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 11226 converted = 1;
180f8dbb 11227 boolean_op = true;
3e4093b6 11228 }
928c19bb
JM
11229 if (code == TRUTH_ANDIF_EXPR)
11230 {
11231 int_const_or_overflow = (int_operands
11232 && TREE_CODE (orig_op0) == INTEGER_CST
11233 && (op0 == truthvalue_false_node
11234 || TREE_CODE (orig_op1) == INTEGER_CST));
11235 int_const = (int_const_or_overflow
11236 && !TREE_OVERFLOW (orig_op0)
11237 && (op0 == truthvalue_false_node
11238 || !TREE_OVERFLOW (orig_op1)));
11239 }
11240 else if (code == TRUTH_ORIF_EXPR)
11241 {
11242 int_const_or_overflow = (int_operands
11243 && TREE_CODE (orig_op0) == INTEGER_CST
11244 && (op0 == truthvalue_true_node
11245 || TREE_CODE (orig_op1) == INTEGER_CST));
11246 int_const = (int_const_or_overflow
11247 && !TREE_OVERFLOW (orig_op0)
11248 && (op0 == truthvalue_true_node
11249 || !TREE_OVERFLOW (orig_op1)));
11250 }
3e4093b6 11251 break;
eba80994 11252
3e4093b6
RS
11253 /* Shift operations: result has same type as first operand;
11254 always convert second operand to int.
11255 Also set SHORT_SHIFT if shifting rightward. */
de520661 11256
3e4093b6 11257 case RSHIFT_EXPR:
76e2c821
JB
11258 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11259 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11260 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11261 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11262 {
11263 result_type = type0;
11264 converted = 1;
11265 }
76e2c821
JB
11266 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11267 || code0 == VECTOR_TYPE)
451b5e48 11268 && code1 == INTEGER_TYPE)
3e4093b6 11269 {
de5a5fa1 11270 doing_shift = true;
928c19bb 11271 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 11272 {
3e4093b6 11273 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11274 {
11275 int_const = false;
7d882b83 11276 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11277 warning_at (location, OPT_Wshift_count_negative,
11278 "right shift count is negative");
928c19bb 11279 }
76e2c821
JB
11280 else if (code0 == VECTOR_TYPE)
11281 {
11282 if (compare_tree_int (op1,
11283 TYPE_PRECISION (TREE_TYPE (type0)))
11284 >= 0)
11285 {
11286 int_const = false;
11287 if (c_inhibit_evaluation_warnings == 0)
11288 warning_at (location, OPT_Wshift_count_overflow,
11289 "right shift count >= width of vector element");
11290 }
11291 }
3e4093b6 11292 else
bbb818c6 11293 {
3f75a254 11294 if (!integer_zerop (op1))
3e4093b6
RS
11295 short_shift = 1;
11296
11297 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11298 {
11299 int_const = false;
7d882b83 11300 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11301 warning_at (location, OPT_Wshift_count_overflow,
11302 "right shift count >= width of type");
928c19bb 11303 }
bbb818c6 11304 }
b62acd60 11305 }
de520661 11306
3e4093b6
RS
11307 /* Use the type of the value to be shifted. */
11308 result_type = type0;
3e4093b6
RS
11309 /* Avoid converting op1 to result_type later. */
11310 converted = 1;
400fbf9f 11311 }
3e4093b6 11312 break;
253b6b82 11313
3e4093b6 11314 case LSHIFT_EXPR:
76e2c821
JB
11315 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11316 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11317 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11318 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11319 {
11320 result_type = type0;
11321 converted = 1;
11322 }
76e2c821
JB
11323 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11324 || code0 == VECTOR_TYPE)
451b5e48 11325 && code1 == INTEGER_TYPE)
3e4093b6 11326 {
de5a5fa1 11327 doing_shift = true;
0173bd2a
MP
11328 if (TREE_CODE (op0) == INTEGER_CST
11329 && tree_int_cst_sgn (op0) < 0)
11330 {
11331 /* Don't reject a left shift of a negative value in a context
11332 where a constant expression is needed in C90. */
11333 if (flag_isoc99)
11334 int_const = false;
11335 if (c_inhibit_evaluation_warnings == 0)
11336 warning_at (location, OPT_Wshift_negative_value,
11337 "left shift of negative value");
11338 }
928c19bb 11339 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11340 {
3e4093b6 11341 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11342 {
11343 int_const = false;
7d882b83 11344 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11345 warning_at (location, OPT_Wshift_count_negative,
11346 "left shift count is negative");
928c19bb 11347 }
76e2c821
JB
11348 else if (code0 == VECTOR_TYPE)
11349 {
11350 if (compare_tree_int (op1,
11351 TYPE_PRECISION (TREE_TYPE (type0)))
11352 >= 0)
11353 {
11354 int_const = false;
11355 if (c_inhibit_evaluation_warnings == 0)
11356 warning_at (location, OPT_Wshift_count_overflow,
11357 "left shift count >= width of vector element");
11358 }
11359 }
3e4093b6 11360 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11361 {
11362 int_const = false;
7d882b83 11363 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11364 warning_at (location, OPT_Wshift_count_overflow,
11365 "left shift count >= width of type");
928c19bb 11366 }
451b5e48
MP
11367 else if (TREE_CODE (op0) == INTEGER_CST
11368 && maybe_warn_shift_overflow (location, op0, op1)
11369 && flag_isoc99)
11370 int_const = false;
94ba5069 11371 }
de520661 11372
3e4093b6
RS
11373 /* Use the type of the value to be shifted. */
11374 result_type = type0;
3e4093b6
RS
11375 /* Avoid converting op1 to result_type later. */
11376 converted = 1;
400fbf9f 11377 }
3e4093b6 11378 break;
de520661 11379
3e4093b6
RS
11380 case EQ_EXPR:
11381 case NE_EXPR:
d246ab4f
AS
11382 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11383 {
11384 tree intt;
0af94e6f 11385 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11386 {
11387 error_at (location, "comparing vectors with different "
11388 "element types");
11389 return error_mark_node;
11390 }
11391
11392 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11393 {
11394 error_at (location, "comparing vectors with different "
11395 "number of elements");
11396 return error_mark_node;
11397 }
11398
fa74a4bc
MP
11399 /* It's not precisely specified how the usual arithmetic
11400 conversions apply to the vector types. Here, we use
11401 the unsigned type if one of the operands is signed and
11402 the other one is unsigned. */
11403 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11404 {
11405 if (!TYPE_UNSIGNED (type0))
11406 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11407 else
11408 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11409 warning_at (location, OPT_Wsign_compare, "comparison between "
11410 "types %qT and %qT", type0, type1);
11411 }
11412
d246ab4f
AS
11413 /* Always construct signed integer vector type. */
11414 intt = c_common_type_for_size (GET_MODE_BITSIZE
b397965c
RS
11415 (SCALAR_TYPE_MODE
11416 (TREE_TYPE (type0))), 0);
d246ab4f
AS
11417 result_type = build_opaque_vector_type (intt,
11418 TYPE_VECTOR_SUBPARTS (type0));
11419 converted = 1;
9f47c7e5
IE
11420 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11421 goto return_build_binary_op;
d246ab4f 11422 }
ae311566 11423 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11424 warning_at (location,
11425 OPT_Wfloat_equal,
11426 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11427 /* Result of comparison is always int,
11428 but don't convert the args to int! */
11429 build_type = integer_type_node;
11430 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11431 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11432 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11433 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11434 short_compare = 1;
637f1455
SZ
11435 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11436 {
11437 if (TREE_CODE (op0) == ADDR_EXPR
de55efd5
MP
11438 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11439 && !from_macro_expansion_at (location))
637f1455
SZ
11440 {
11441 if (code == EQ_EXPR)
11442 warning_at (location,
11443 OPT_Waddress,
11444 "the comparison will always evaluate as %<false%> "
11445 "for the address of %qD will never be NULL",
11446 TREE_OPERAND (op0, 0));
11447 else
11448 warning_at (location,
11449 OPT_Waddress,
11450 "the comparison will always evaluate as %<true%> "
11451 "for the address of %qD will never be NULL",
11452 TREE_OPERAND (op0, 0));
11453 }
11454 result_type = type0;
11455 }
11456 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11457 {
11458 if (TREE_CODE (op1) == ADDR_EXPR
de55efd5
MP
11459 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11460 && !from_macro_expansion_at (location))
637f1455
SZ
11461 {
11462 if (code == EQ_EXPR)
11463 warning_at (location,
f90e8e2e 11464 OPT_Waddress,
637f1455
SZ
11465 "the comparison will always evaluate as %<false%> "
11466 "for the address of %qD will never be NULL",
11467 TREE_OPERAND (op1, 0));
11468 else
11469 warning_at (location,
11470 OPT_Waddress,
11471 "the comparison will always evaluate as %<true%> "
11472 "for the address of %qD will never be NULL",
11473 TREE_OPERAND (op1, 0));
11474 }
11475 result_type = type1;
11476 }
3e4093b6
RS
11477 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11478 {
11479 tree tt0 = TREE_TYPE (type0);
11480 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11481 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11482 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11483 addr_space_t as_common = ADDR_SPACE_GENERIC;
11484
3e4093b6
RS
11485 /* Anything compares with void *. void * compares with anything.
11486 Otherwise, the targets must be compatible
11487 and both must be object or both incomplete. */
744aa42f 11488 if (comp_target_types (location, type0, type1))
10bc1b1b 11489 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11490 else if (!addr_space_superset (as0, as1, &as_common))
11491 {
11492 error_at (location, "comparison of pointers to "
11493 "disjoint address spaces");
11494 return error_mark_node;
11495 }
267bac10 11496 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11497 {
36c5e70a 11498 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11499 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11500 "comparison of %<void *%> with function pointer");
ee2990e7 11501 }
267bac10 11502 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11503 {
36c5e70a 11504 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11505 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11506 "comparison of %<void *%> with function pointer");
e6834654 11507 }
3e4093b6 11508 else
58393038
ZL
11509 /* Avoid warning about the volatile ObjC EH puts on decls. */
11510 if (!objc_ok)
ba47d38d 11511 pedwarn (location, 0,
509c9d60 11512 "comparison of distinct pointer types lacks a cast");
e6834654 11513
3e4093b6 11514 if (result_type == NULL_TREE)
36c5e70a
BE
11515 {
11516 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11517 result_type = build_pointer_type
11518 (build_qualified_type (void_type_node, qual));
11519 }
e6834654 11520 }
3e4093b6 11521 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11522 {
3e4093b6 11523 result_type = type0;
ba47d38d 11524 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11525 }
3e4093b6 11526 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11527 {
3e4093b6 11528 result_type = type1;
ba47d38d 11529 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11530 }
04159acf
MP
11531 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11532 || truth_value_p (TREE_CODE (orig_op0)))
11533 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11534 || truth_value_p (TREE_CODE (orig_op1))))
11535 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11536 break;
8b6a5902 11537
3e4093b6
RS
11538 case LE_EXPR:
11539 case GE_EXPR:
11540 case LT_EXPR:
11541 case GT_EXPR:
d246ab4f
AS
11542 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11543 {
11544 tree intt;
0af94e6f 11545 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11546 {
11547 error_at (location, "comparing vectors with different "
11548 "element types");
11549 return error_mark_node;
11550 }
11551
11552 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11553 {
11554 error_at (location, "comparing vectors with different "
11555 "number of elements");
11556 return error_mark_node;
11557 }
11558
fa74a4bc
MP
11559 /* It's not precisely specified how the usual arithmetic
11560 conversions apply to the vector types. Here, we use
11561 the unsigned type if one of the operands is signed and
11562 the other one is unsigned. */
11563 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11564 {
11565 if (!TYPE_UNSIGNED (type0))
11566 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11567 else
11568 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11569 warning_at (location, OPT_Wsign_compare, "comparison between "
11570 "types %qT and %qT", type0, type1);
11571 }
11572
d246ab4f
AS
11573 /* Always construct signed integer vector type. */
11574 intt = c_common_type_for_size (GET_MODE_BITSIZE
b397965c
RS
11575 (SCALAR_TYPE_MODE
11576 (TREE_TYPE (type0))), 0);
d246ab4f
AS
11577 result_type = build_opaque_vector_type (intt,
11578 TYPE_VECTOR_SUBPARTS (type0));
11579 converted = 1;
9f47c7e5
IE
11580 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11581 goto return_build_binary_op;
d246ab4f 11582 }
3e4093b6 11583 build_type = integer_type_node;
ab22c1fa
CF
11584 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11585 || code0 == FIXED_POINT_TYPE)
11586 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11587 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11588 short_compare = 1;
11589 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11590 {
36c5e70a
BE
11591 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11592 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11593 addr_space_t as_common;
11594
744aa42f 11595 if (comp_target_types (location, type0, type1))
3e4093b6 11596 {
10bc1b1b 11597 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11598 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11599 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11600 pedwarn (location, 0,
509c9d60 11601 "comparison of complete and incomplete pointers");
fcf73884 11602 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11603 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11604 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11605 else if (null_pointer_constant_p (orig_op0)
11606 || null_pointer_constant_p (orig_op1))
11607 warning_at (location, OPT_Wextra,
11608 "ordered comparison of pointer with null pointer");
11609
3e4093b6 11610 }
36c5e70a
BE
11611 else if (!addr_space_superset (as0, as1, &as_common))
11612 {
11613 error_at (location, "comparison of pointers to "
11614 "disjoint address spaces");
11615 return error_mark_node;
11616 }
3e4093b6
RS
11617 else
11618 {
36c5e70a
BE
11619 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11620 result_type = build_pointer_type
11621 (build_qualified_type (void_type_node, qual));
ba47d38d 11622 pedwarn (location, 0,
509c9d60 11623 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11624 }
11625 }
6aa3c60d 11626 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11627 {
11628 result_type = type0;
fcf73884 11629 if (pedantic)
c1771a20 11630 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11631 "ordered comparison of pointer with integer zero");
11632 else if (extra_warnings)
ba47d38d 11633 warning_at (location, OPT_Wextra,
d42ba3b8 11634 "ordered comparison of pointer with integer zero");
3e4093b6 11635 }
6aa3c60d 11636 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11637 {
11638 result_type = type1;
d42ba3b8 11639 if (pedantic)
c1771a20 11640 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11641 "ordered comparison of pointer with integer zero");
11642 else if (extra_warnings)
11643 warning_at (location, OPT_Wextra,
11644 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11645 }
11646 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11647 {
11648 result_type = type0;
ba47d38d 11649 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11650 }
11651 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11652 {
11653 result_type = type1;
ba47d38d 11654 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11655 }
04159acf
MP
11656 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11657 || truth_value_p (TREE_CODE (orig_op0)))
11658 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11659 || truth_value_p (TREE_CODE (orig_op1))))
11660 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11661 break;
64094f6a 11662
3e4093b6 11663 default:
37b2f290 11664 gcc_unreachable ();
c9fe6f9f 11665 }
8f17b5c5 11666
e57e265b
PB
11667 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11668 return error_mark_node;
11669
5bed876a
AH
11670 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11671 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11672 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11673 {
745e411d
DM
11674 gcc_rich_location richloc (location);
11675 richloc.maybe_add_expr (orig_op0);
11676 richloc.maybe_add_expr (orig_op1);
11677 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11678 return error_mark_node;
11679 }
11680
3e4093b6 11681 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11682 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11683 &&
11684 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11685 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11686 {
2ca862e9
JM
11687 bool first_complex = (code0 == COMPLEX_TYPE);
11688 bool second_complex = (code1 == COMPLEX_TYPE);
11689 int none_complex = (!first_complex && !second_complex);
39b726dd 11690
3e4093b6 11691 if (shorten || common || short_compare)
3bf6bfcc
JJ
11692 {
11693 result_type = c_common_type (type0, type1);
0a0b3574
MM
11694 do_warn_double_promotion (result_type, type0, type1,
11695 "implicit conversion from %qT to %qT "
11696 "to match other operand of binary "
11697 "expression",
11698 location);
3bf6bfcc
JJ
11699 if (result_type == error_mark_node)
11700 return error_mark_node;
11701 }
400fbf9f 11702
2ca862e9
JM
11703 if (first_complex != second_complex
11704 && (code == PLUS_EXPR
11705 || code == MINUS_EXPR
11706 || code == MULT_EXPR
11707 || (code == TRUNC_DIV_EXPR && first_complex))
11708 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11709 && flag_signed_zeros)
11710 {
11711 /* An operation on mixed real/complex operands must be
11712 handled specially, but the language-independent code can
11713 more easily optimize the plain complex arithmetic if
11714 -fno-signed-zeros. */
11715 tree real_type = TREE_TYPE (result_type);
11716 tree real, imag;
11717 if (type0 != orig_type0 || type1 != orig_type1)
11718 {
11719 gcc_assert (may_need_excess_precision && common);
2d2e923f 11720 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11721 }
11722 if (first_complex)
11723 {
11724 if (TREE_TYPE (op0) != result_type)
68fca595 11725 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11726 if (TREE_TYPE (op1) != real_type)
68fca595 11727 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11728 }
11729 else
11730 {
11731 if (TREE_TYPE (op0) != real_type)
68fca595 11732 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11733 if (TREE_TYPE (op1) != result_type)
68fca595 11734 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11735 }
11736 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11737 return error_mark_node;
11738 if (first_complex)
11739 {
b2fa0a8b 11740 op0 = save_expr (op0);
2ca862e9 11741 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
e51fbec3 11742 op0, true);
2ca862e9 11743 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
e51fbec3 11744 op0, true);
2ca862e9
JM
11745 switch (code)
11746 {
11747 case MULT_EXPR:
11748 case TRUNC_DIV_EXPR:
b2fa0a8b 11749 op1 = save_expr (op1);
2ca862e9
JM
11750 imag = build2 (resultcode, real_type, imag, op1);
11751 /* Fall through. */
11752 case PLUS_EXPR:
11753 case MINUS_EXPR:
11754 real = build2 (resultcode, real_type, real, op1);
11755 break;
11756 default:
11757 gcc_unreachable();
11758 }
11759 }
11760 else
11761 {
b2fa0a8b 11762 op1 = save_expr (op1);
2ca862e9 11763 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
e51fbec3 11764 op1, true);
2ca862e9 11765 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
e51fbec3 11766 op1, true);
2ca862e9
JM
11767 switch (code)
11768 {
11769 case MULT_EXPR:
b2fa0a8b 11770 op0 = save_expr (op0);
2ca862e9
JM
11771 imag = build2 (resultcode, real_type, op0, imag);
11772 /* Fall through. */
11773 case PLUS_EXPR:
11774 real = build2 (resultcode, real_type, op0, real);
11775 break;
11776 case MINUS_EXPR:
11777 real = build2 (resultcode, real_type, op0, real);
11778 imag = build1 (NEGATE_EXPR, real_type, imag);
11779 break;
11780 default:
11781 gcc_unreachable();
11782 }
11783 }
11784 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11785 goto return_build_binary_op;
11786 }
11787
3e4093b6
RS
11788 /* For certain operations (which identify themselves by shorten != 0)
11789 if both args were extended from the same smaller type,
11790 do the arithmetic in that type and then extend.
400fbf9f 11791
3e4093b6
RS
11792 shorten !=0 and !=1 indicates a bitwise operation.
11793 For them, this optimization is safe only if
11794 both args are zero-extended or both are sign-extended.
11795 Otherwise, we might change the result.
11796 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11797 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11798
3e4093b6
RS
11799 if (shorten && none_complex)
11800 {
3e4093b6 11801 final_type = result_type;
b8698a0f 11802 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11803 shorten == -1);
3e4093b6 11804 }
88a3dbc1 11805
3e4093b6 11806 /* Shifts can be shortened if shifting right. */
2f6e4e97 11807
3e4093b6
RS
11808 if (short_shift)
11809 {
11810 int unsigned_arg;
11811 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11812
3e4093b6 11813 final_type = result_type;
abe80e6d 11814
3e4093b6 11815 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11816 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11817
3e4093b6 11818 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11819 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11820 /* We can shorten only if the shift count is less than the
11821 number of bits in the smaller type size. */
11822 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11823 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11824 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11825 {
11826 /* Do an unsigned shift if the operand was zero-extended. */
11827 result_type
11828 = c_common_signed_or_unsigned_type (unsigned_arg,
11829 TREE_TYPE (arg0));
11830 /* Convert value-to-be-shifted to that type. */
11831 if (TREE_TYPE (op0) != result_type)
11832 op0 = convert (result_type, op0);
11833 converted = 1;
abe80e6d 11834 }
88a3dbc1
RK
11835 }
11836
3e4093b6
RS
11837 /* Comparison operations are shortened too but differently.
11838 They identify themselves by setting short_compare = 1. */
56cb9733 11839
3e4093b6
RS
11840 if (short_compare)
11841 {
11842 /* Don't write &op0, etc., because that would prevent op0
11843 from being kept in a register.
11844 Instead, make copies of the our local variables and
11845 pass the copies by reference, then copy them back afterward. */
11846 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11847 enum tree_code xresultcode = resultcode;
11848 tree val
393e8e8b
MP
11849 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11850 &xresultcode);
8f17b5c5 11851
3fa8871b 11852 if (val != NULL_TREE)
c9f9eb5d
AH
11853 {
11854 ret = val;
11855 goto return_build_binary_op;
11856 }
8f17b5c5 11857
3e4093b6
RS
11858 op0 = xop0, op1 = xop1;
11859 converted = 1;
11860 resultcode = xresultcode;
8f17b5c5 11861
7d882b83 11862 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11863 {
11864 bool op0_maybe_const = true;
11865 bool op1_maybe_const = true;
11866 tree orig_op0_folded, orig_op1_folded;
11867
11868 if (in_late_binary_op)
11869 {
11870 orig_op0_folded = orig_op0;
11871 orig_op1_folded = orig_op1;
11872 }
11873 else
11874 {
11875 /* Fold for the sake of possible warnings, as in
11876 build_conditional_expr. This requires the
11877 "original" values to be folded, not just op0 and
11878 op1. */
f5178456 11879 c_inhibit_evaluation_warnings++;
928c19bb
JM
11880 op0 = c_fully_fold (op0, require_constant_value,
11881 &op0_maybe_const);
11882 op1 = c_fully_fold (op1, require_constant_value,
11883 &op1_maybe_const);
f5178456 11884 c_inhibit_evaluation_warnings--;
928c19bb
JM
11885 orig_op0_folded = c_fully_fold (orig_op0,
11886 require_constant_value,
11887 NULL);
11888 orig_op1_folded = c_fully_fold (orig_op1,
11889 require_constant_value,
11890 NULL);
11891 }
11892
11893 if (warn_sign_compare)
11894 warn_for_sign_compare (location, orig_op0_folded,
11895 orig_op1_folded, op0, op1,
11896 result_type, resultcode);
5c2f94b4 11897 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11898 {
11899 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11900 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11901 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11902 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11903 }
3e4093b6 11904 }
2ad1815d 11905 }
64094f6a 11906 }
64094f6a 11907
3e4093b6
RS
11908 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11909 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11910 Then the expression will be built.
11911 It will be given type FINAL_TYPE if that is nonzero;
11912 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11913
3e4093b6
RS
11914 if (!result_type)
11915 {
745e411d
DM
11916 gcc_rich_location richloc (location);
11917 richloc.maybe_add_expr (orig_op0);
11918 richloc.maybe_add_expr (orig_op1);
11919 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11920 return error_mark_node;
11921 }
400fbf9f 11922
3e4093b6 11923 if (build_type == NULL_TREE)
8ce94e44
JM
11924 {
11925 build_type = result_type;
180f8dbb
JM
11926 if ((type0 != orig_type0 || type1 != orig_type1)
11927 && !boolean_op)
8ce94e44
JM
11928 {
11929 gcc_assert (may_need_excess_precision && common);
2d2e923f 11930 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11931 }
11932 }
400fbf9f 11933
2d2e923f
MLI
11934 if (!converted)
11935 {
68fca595
MP
11936 op0 = ep_convert_and_check (location, result_type, op0,
11937 semantic_result_type);
11938 op1 = ep_convert_and_check (location, result_type, op1,
11939 semantic_result_type);
2d2e923f
MLI
11940
11941 /* This can happen if one operand has a vector type, and the other
11942 has a different type. */
11943 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11944 return error_mark_node;
11945 }
11946
45b2222a
ML
11947 if (sanitize_flags_p ((SANITIZE_SHIFT
11948 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
f34ebeb2 11949 && current_function_decl != NULL_TREE
b2aaf235
MP
11950 && (doing_div_or_mod || doing_shift)
11951 && !require_constant_value)
de5a5fa1
MP
11952 {
11953 /* OP0 and/or OP1 might have side-effects. */
b2fa0a8b
MP
11954 op0 = save_expr (op0);
11955 op1 = save_expr (op1);
de5a5fa1
MP
11956 op0 = c_fully_fold (op0, false, NULL);
11957 op1 = c_fully_fold (op1, false, NULL);
45b2222a
ML
11958 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11959 | SANITIZE_FLOAT_DIVIDE))))
de5a5fa1 11960 instrument_expr = ubsan_instrument_division (location, op0, op1);
45b2222a 11961 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
de5a5fa1
MP
11962 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11963 }
11964
c9f9eb5d 11965 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11966 if (int_const_or_overflow)
11967 ret = (require_constant_value
db3927fb
AH
11968 ? fold_build2_initializer_loc (location, resultcode, build_type,
11969 op0, op1)
11970 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11971 else
11972 ret = build2 (resultcode, build_type, op0, op1);
3fa8871b 11973 if (final_type != NULL_TREE)
c9f9eb5d
AH
11974 ret = convert (final_type, ret);
11975
11976 return_build_binary_op:
11977 gcc_assert (ret != error_mark_node);
928c19bb
JM
11978 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11979 ret = (int_operands
11980 ? note_integer_operands (ret)
11981 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11982 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11983 && !in_late_binary_op)
11984 ret = note_integer_operands (ret);
c9f9eb5d 11985 protected_set_expr_location (ret, location);
de5a5fa1 11986
a24d975c 11987 if (instrument_expr != NULL)
de5a5fa1
MP
11988 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11989 instrument_expr, ret);
11990
4d1b8e70
JJ
11991 if (semantic_result_type)
11992 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
11993 semantic_result_type, ret);
11994
c9f9eb5d 11995 return ret;
400fbf9f 11996}
85498824
JM
11997
11998
11999/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 12000 purpose. LOCATION is the source location for the expression. */
85498824
JM
12001
12002tree
ba47d38d 12003c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 12004{
928c19bb
JM
12005 bool int_const, int_operands;
12006
85498824
JM
12007 switch (TREE_CODE (TREE_TYPE (expr)))
12008 {
12009 case ARRAY_TYPE:
ba47d38d 12010 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
12011 return error_mark_node;
12012
12013 case RECORD_TYPE:
ba47d38d 12014 error_at (location, "used struct type value where scalar is required");
85498824
JM
12015 return error_mark_node;
12016
12017 case UNION_TYPE:
ba47d38d 12018 error_at (location, "used union type value where scalar is required");
85498824
JM
12019 return error_mark_node;
12020
04af8788
NP
12021 case VOID_TYPE:
12022 error_at (location, "void value not ignored as it ought to be");
12023 return error_mark_node;
12024
1807ffc1
MS
12025 case POINTER_TYPE:
12026 if (reject_gcc_builtin (expr))
12027 return error_mark_node;
12028 break;
12029
46bdb9cf
JM
12030 case FUNCTION_TYPE:
12031 gcc_unreachable ();
12032
d246ab4f
AS
12033 case VECTOR_TYPE:
12034 error_at (location, "used vector type where scalar is required");
12035 return error_mark_node;
12036
85498824
JM
12037 default:
12038 break;
12039 }
12040
928c19bb
JM
12041 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12042 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
12043 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12044 {
12045 expr = remove_c_maybe_const_expr (expr);
12046 expr = build2 (NE_EXPR, integer_type_node, expr,
12047 convert (TREE_TYPE (expr), integer_zero_node));
12048 expr = note_integer_operands (expr);
12049 }
12050 else
12051 /* ??? Should we also give an error for vectors rather than leaving
12052 those to give errors later? */
12053 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
12054
12055 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12056 {
12057 if (TREE_OVERFLOW (expr))
12058 return expr;
12059 else
12060 return note_integer_operands (expr);
12061 }
12062 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12063 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12064 return expr;
85498824 12065}
73f397d4
JM
12066\f
12067
12068/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12069 required. */
12070
12071tree
51eed280 12072c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
12073{
12074 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12075 {
12076 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12077 /* Executing a compound literal inside a function reinitializes
12078 it. */
12079 if (!TREE_STATIC (decl))
12080 *se = true;
12081 return decl;
12082 }
12083 else
12084 return expr;
12085}
953ff289 12086\f
88bae6f4
TS
12087/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12088 statement. LOC is the location of the construct. */
41dbbb37
TS
12089
12090tree
88bae6f4
TS
12091c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12092 tree clauses)
41dbbb37 12093{
88bae6f4 12094 body = c_end_compound_stmt (loc, body, true);
41dbbb37 12095
88bae6f4 12096 tree stmt = make_node (code);
41dbbb37 12097 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
12098 OMP_BODY (stmt) = body;
12099 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
12100 SET_EXPR_LOCATION (stmt, loc);
12101
12102 return add_stmt (stmt);
12103}
12104
12105/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12106 statement. LOC is the location of the OACC_DATA. */
12107
12108tree
12109c_finish_oacc_data (location_t loc, tree clauses, tree block)
12110{
12111 tree stmt;
12112
12113 block = c_end_compound_stmt (loc, block, true);
12114
12115 stmt = make_node (OACC_DATA);
12116 TREE_TYPE (stmt) = void_type_node;
12117 OACC_DATA_CLAUSES (stmt) = clauses;
12118 OACC_DATA_BODY (stmt) = block;
12119 SET_EXPR_LOCATION (stmt, loc);
12120
12121 return add_stmt (stmt);
12122}
12123
37d5ad46
JB
12124/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12125 statement. LOC is the location of the OACC_HOST_DATA. */
12126
12127tree
12128c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12129{
12130 tree stmt;
12131
12132 block = c_end_compound_stmt (loc, block, true);
12133
12134 stmt = make_node (OACC_HOST_DATA);
12135 TREE_TYPE (stmt) = void_type_node;
12136 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12137 OACC_HOST_DATA_BODY (stmt) = block;
12138 SET_EXPR_LOCATION (stmt, loc);
12139
12140 return add_stmt (stmt);
12141}
12142
c0220ea4 12143/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
12144
12145tree
12146c_begin_omp_parallel (void)
12147{
12148 tree block;
12149
12150 keep_next_level ();
12151 block = c_begin_compound_stmt (true);
12152
12153 return block;
12154}
12155
c2255bc4
AH
12156/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12157 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 12158
953ff289 12159tree
c2255bc4 12160c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
12161{
12162 tree stmt;
12163
c2255bc4 12164 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
12165
12166 stmt = make_node (OMP_PARALLEL);
12167 TREE_TYPE (stmt) = void_type_node;
12168 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12169 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 12170 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
12171
12172 return add_stmt (stmt);
12173}
12174
a68ab351
JJ
12175/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12176
12177tree
12178c_begin_omp_task (void)
12179{
12180 tree block;
12181
12182 keep_next_level ();
12183 block = c_begin_compound_stmt (true);
12184
12185 return block;
12186}
12187
c2255bc4
AH
12188/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12189 statement. LOC is the location of the #pragma. */
a68ab351
JJ
12190
12191tree
c2255bc4 12192c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
12193{
12194 tree stmt;
12195
c2255bc4 12196 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
12197
12198 stmt = make_node (OMP_TASK);
12199 TREE_TYPE (stmt) = void_type_node;
12200 OMP_TASK_CLAUSES (stmt) = clauses;
12201 OMP_TASK_BODY (stmt) = block;
c2255bc4 12202 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
12203
12204 return add_stmt (stmt);
12205}
12206
acf0174b
JJ
12207/* Generate GOMP_cancel call for #pragma omp cancel. */
12208
12209void
12210c_finish_omp_cancel (location_t loc, tree clauses)
12211{
12212 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12213 int mask = 0;
629b3d75 12214 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12215 mask = 1;
629b3d75 12216 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12217 mask = 2;
629b3d75 12218 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12219 mask = 4;
629b3d75 12220 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12221 mask = 8;
12222 else
12223 {
54d19c3b 12224 error_at (loc, "%<#pragma omp cancel%> must specify one of "
acf0174b
JJ
12225 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12226 "clauses");
12227 return;
12228 }
629b3d75 12229 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
acf0174b
JJ
12230 if (ifc != NULL_TREE)
12231 {
12232 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12233 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12234 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12235 build_zero_cst (type));
12236 }
12237 else
12238 ifc = boolean_true_node;
12239 tree stmt = build_call_expr_loc (loc, fn, 2,
12240 build_int_cst (integer_type_node, mask),
12241 ifc);
12242 add_stmt (stmt);
12243}
12244
12245/* Generate GOMP_cancellation_point call for
12246 #pragma omp cancellation point. */
12247
12248void
12249c_finish_omp_cancellation_point (location_t loc, tree clauses)
12250{
12251 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12252 int mask = 0;
629b3d75 12253 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12254 mask = 1;
629b3d75 12255 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12256 mask = 2;
629b3d75 12257 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12258 mask = 4;
629b3d75 12259 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12260 mask = 8;
12261 else
12262 {
54d19c3b 12263 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
acf0174b
JJ
12264 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12265 "clauses");
12266 return;
12267 }
12268 tree stmt = build_call_expr_loc (loc, fn, 1,
12269 build_int_cst (integer_type_node, mask));
12270 add_stmt (stmt);
12271}
12272
12273/* Helper function for handle_omp_array_sections. Called recursively
12274 to handle multiple array-section-subscripts. C is the clause,
12275 T current expression (initially OMP_CLAUSE_DECL), which is either
12276 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12277 expression if specified, TREE_VALUE length expression if specified,
12278 TREE_CHAIN is what it has been specified after, or some decl.
12279 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12280 set to true if any of the array-section-subscript could have length
12281 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12282 first array-section-subscript which is known not to have length
12283 of one. Given say:
12284 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12285 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12286 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 12287 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
12288 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12289 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12290 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12291 case though, as some lengths could be zero. */
12292
12293static tree
12294handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32 12295 bool &maybe_zero_len, unsigned int &first_non_one,
e46c7770 12296 enum c_omp_region_type ort)
acf0174b
JJ
12297{
12298 tree ret, low_bound, length, type;
12299 if (TREE_CODE (t) != TREE_LIST)
12300 {
7a0ca710 12301 if (error_operand_p (t))
acf0174b 12302 return error_mark_node;
d9a6bd32 12303 ret = t;
9dc5773f
JJ
12304 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12305 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12306 {
12307 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12308 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12309 return error_mark_node;
12310 }
d9a6bd32 12311 if (TREE_CODE (t) == COMPONENT_REF
e46c7770 12312 && ort == C_ORT_OMP
d9a6bd32
JJ
12313 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12314 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12315 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12316 {
12317 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12318 {
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "bit-field %qE in %qs clause",
12321 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12322 return error_mark_node;
12323 }
12324 while (TREE_CODE (t) == COMPONENT_REF)
12325 {
12326 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12327 {
12328 error_at (OMP_CLAUSE_LOCATION (c),
12329 "%qE is a member of a union", t);
12330 return error_mark_node;
12331 }
12332 t = TREE_OPERAND (t, 0);
12333 }
12334 }
0ae9bd27 12335 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12336 {
12337 if (DECL_P (t))
12338 error_at (OMP_CLAUSE_LOCATION (c),
12339 "%qD is not a variable in %qs clause", t,
12340 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12341 else
12342 error_at (OMP_CLAUSE_LOCATION (c),
12343 "%qE is not a variable in %qs clause", t,
12344 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12345 return error_mark_node;
12346 }
12347 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
9dc5773f
JJ
12348 && TYPE_ATOMIC (TREE_TYPE (t)))
12349 {
12350 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12351 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12352 return error_mark_node;
12353 }
12354 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12355 && VAR_P (t)
12356 && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12357 {
12358 error_at (OMP_CLAUSE_LOCATION (c),
12359 "%qD is threadprivate variable in %qs clause", t,
12360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12361 return error_mark_node;
12362 }
9dc5773f
JJ
12363 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12364 && TYPE_ATOMIC (TREE_TYPE (t))
12365 && POINTER_TYPE_P (TREE_TYPE (t)))
12366 {
12367 /* If the array section is pointer based and the pointer
12368 itself is _Atomic qualified, we need to atomically load
12369 the pointer. */
12370 c_expr expr;
12371 memset (&expr, 0, sizeof (expr));
12372 expr.value = ret;
12373 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12374 expr, false, false);
12375 ret = expr.value;
12376 }
d9a6bd32 12377 return ret;
acf0174b
JJ
12378 }
12379
12380 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
e46c7770 12381 maybe_zero_len, first_non_one, ort);
acf0174b
JJ
12382 if (ret == error_mark_node || ret == NULL_TREE)
12383 return ret;
12384
12385 type = TREE_TYPE (ret);
12386 low_bound = TREE_PURPOSE (t);
12387 length = TREE_VALUE (t);
12388
12389 if (low_bound == error_mark_node || length == error_mark_node)
12390 return error_mark_node;
12391
12392 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12393 {
12394 error_at (OMP_CLAUSE_LOCATION (c),
12395 "low bound %qE of array section does not have integral type",
12396 low_bound);
12397 return error_mark_node;
12398 }
12399 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12400 {
12401 error_at (OMP_CLAUSE_LOCATION (c),
12402 "length %qE of array section does not have integral type",
12403 length);
12404 return error_mark_node;
12405 }
12406 if (low_bound
12407 && TREE_CODE (low_bound) == INTEGER_CST
12408 && TYPE_PRECISION (TREE_TYPE (low_bound))
12409 > TYPE_PRECISION (sizetype))
12410 low_bound = fold_convert (sizetype, low_bound);
12411 if (length
12412 && TREE_CODE (length) == INTEGER_CST
12413 && TYPE_PRECISION (TREE_TYPE (length))
12414 > TYPE_PRECISION (sizetype))
12415 length = fold_convert (sizetype, length);
12416 if (low_bound == NULL_TREE)
12417 low_bound = integer_zero_node;
12418
12419 if (length != NULL_TREE)
12420 {
12421 if (!integer_nonzerop (length))
d9a6bd32
JJ
12422 {
12423 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12424 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12425 {
12426 if (integer_zerop (length))
12427 {
12428 error_at (OMP_CLAUSE_LOCATION (c),
12429 "zero length array section in %qs clause",
12430 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12431 return error_mark_node;
12432 }
12433 }
12434 else
12435 maybe_zero_len = true;
12436 }
acf0174b
JJ
12437 if (first_non_one == types.length ()
12438 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12439 first_non_one++;
12440 }
12441 if (TREE_CODE (type) == ARRAY_TYPE)
12442 {
12443 if (length == NULL_TREE
12444 && (TYPE_DOMAIN (type) == NULL_TREE
12445 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12446 {
12447 error_at (OMP_CLAUSE_LOCATION (c),
12448 "for unknown bound array type length expression must "
12449 "be specified");
12450 return error_mark_node;
12451 }
12452 if (TREE_CODE (low_bound) == INTEGER_CST
12453 && tree_int_cst_sgn (low_bound) == -1)
12454 {
12455 error_at (OMP_CLAUSE_LOCATION (c),
12456 "negative low bound in array section in %qs clause",
12457 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12458 return error_mark_node;
12459 }
12460 if (length != NULL_TREE
12461 && TREE_CODE (length) == INTEGER_CST
12462 && tree_int_cst_sgn (length) == -1)
12463 {
12464 error_at (OMP_CLAUSE_LOCATION (c),
12465 "negative length in array section in %qs clause",
12466 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12467 return error_mark_node;
12468 }
12469 if (TYPE_DOMAIN (type)
12470 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12471 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12472 == INTEGER_CST)
12473 {
8ab7005b
JJ
12474 tree size
12475 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12476 size = size_binop (PLUS_EXPR, size, size_one_node);
acf0174b
JJ
12477 if (TREE_CODE (low_bound) == INTEGER_CST)
12478 {
12479 if (tree_int_cst_lt (size, low_bound))
12480 {
12481 error_at (OMP_CLAUSE_LOCATION (c),
12482 "low bound %qE above array section size "
12483 "in %qs clause", low_bound,
12484 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12485 return error_mark_node;
12486 }
12487 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12488 {
12489 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12490 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12491 {
12492 error_at (OMP_CLAUSE_LOCATION (c),
12493 "zero length array section in %qs clause",
12494 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12495 return error_mark_node;
12496 }
12497 maybe_zero_len = true;
12498 }
acf0174b
JJ
12499 else if (length == NULL_TREE
12500 && first_non_one == types.length ()
12501 && tree_int_cst_equal
12502 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12503 low_bound))
12504 first_non_one++;
12505 }
12506 else if (length == NULL_TREE)
12507 {
d9a6bd32
JJ
12508 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12509 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12510 maybe_zero_len = true;
acf0174b
JJ
12511 if (first_non_one == types.length ())
12512 first_non_one++;
12513 }
12514 if (length && TREE_CODE (length) == INTEGER_CST)
12515 {
12516 if (tree_int_cst_lt (size, length))
12517 {
12518 error_at (OMP_CLAUSE_LOCATION (c),
12519 "length %qE above array section size "
12520 "in %qs clause", length,
12521 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12522 return error_mark_node;
12523 }
12524 if (TREE_CODE (low_bound) == INTEGER_CST)
12525 {
12526 tree lbpluslen
12527 = size_binop (PLUS_EXPR,
12528 fold_convert (sizetype, low_bound),
12529 fold_convert (sizetype, length));
12530 if (TREE_CODE (lbpluslen) == INTEGER_CST
12531 && tree_int_cst_lt (size, lbpluslen))
12532 {
12533 error_at (OMP_CLAUSE_LOCATION (c),
12534 "high bound %qE above array section size "
12535 "in %qs clause", lbpluslen,
12536 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12537 return error_mark_node;
12538 }
12539 }
12540 }
12541 }
12542 else if (length == NULL_TREE)
12543 {
d9a6bd32
JJ
12544 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12545 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12546 maybe_zero_len = true;
acf0174b
JJ
12547 if (first_non_one == types.length ())
12548 first_non_one++;
12549 }
12550
12551 /* For [lb:] we will need to evaluate lb more than once. */
12552 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12553 {
b2fa0a8b 12554 tree lb = save_expr (low_bound);
acf0174b
JJ
12555 if (lb != low_bound)
12556 {
12557 TREE_PURPOSE (t) = lb;
12558 low_bound = lb;
12559 }
12560 }
12561 }
12562 else if (TREE_CODE (type) == POINTER_TYPE)
12563 {
12564 if (length == NULL_TREE)
12565 {
12566 error_at (OMP_CLAUSE_LOCATION (c),
12567 "for pointer type length expression must be specified");
12568 return error_mark_node;
12569 }
d9a6bd32
JJ
12570 if (length != NULL_TREE
12571 && TREE_CODE (length) == INTEGER_CST
12572 && tree_int_cst_sgn (length) == -1)
12573 {
12574 error_at (OMP_CLAUSE_LOCATION (c),
12575 "negative length in array section in %qs clause",
12576 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12577 return error_mark_node;
12578 }
acf0174b
JJ
12579 /* If there is a pointer type anywhere but in the very first
12580 array-section-subscript, the array section can't be contiguous. */
12581 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12582 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12583 {
12584 error_at (OMP_CLAUSE_LOCATION (c),
12585 "array section is not contiguous in %qs clause",
12586 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12587 return error_mark_node;
12588 }
12589 }
12590 else
12591 {
12592 error_at (OMP_CLAUSE_LOCATION (c),
12593 "%qE does not have pointer or array type", ret);
12594 return error_mark_node;
12595 }
12596 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12597 types.safe_push (TREE_TYPE (ret));
12598 /* We will need to evaluate lb more than once. */
b2fa0a8b 12599 tree lb = save_expr (low_bound);
acf0174b
JJ
12600 if (lb != low_bound)
12601 {
12602 TREE_PURPOSE (t) = lb;
12603 low_bound = lb;
12604 }
12605 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12606 return ret;
12607}
12608
12609/* Handle array sections for clause C. */
12610
12611static bool
e46c7770 12612handle_omp_array_sections (tree c, enum c_omp_region_type ort)
acf0174b
JJ
12613{
12614 bool maybe_zero_len = false;
12615 unsigned int first_non_one = 0;
d9a6bd32 12616 auto_vec<tree, 10> types;
acf0174b 12617 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32 12618 maybe_zero_len, first_non_one,
e46c7770 12619 ort);
acf0174b 12620 if (first == error_mark_node)
d9a6bd32 12621 return true;
acf0174b 12622 if (first == NULL_TREE)
d9a6bd32 12623 return false;
acf0174b
JJ
12624 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12625 {
12626 tree t = OMP_CLAUSE_DECL (c);
12627 tree tem = NULL_TREE;
acf0174b
JJ
12628 /* Need to evaluate side effects in the length expressions
12629 if any. */
12630 while (TREE_CODE (t) == TREE_LIST)
12631 {
12632 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12633 {
12634 if (tem == NULL_TREE)
12635 tem = TREE_VALUE (t);
12636 else
12637 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12638 TREE_VALUE (t), tem);
12639 }
12640 t = TREE_CHAIN (t);
12641 }
12642 if (tem)
12643 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
f9c59f7e 12644 first = c_fully_fold (first, false, NULL, true);
acf0174b
JJ
12645 OMP_CLAUSE_DECL (c) = first;
12646 }
12647 else
12648 {
12649 unsigned int num = types.length (), i;
12650 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12651 tree condition = NULL_TREE;
12652
12653 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12654 maybe_zero_len = true;
12655
12656 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12657 t = TREE_CHAIN (t))
12658 {
12659 tree low_bound = TREE_PURPOSE (t);
12660 tree length = TREE_VALUE (t);
12661
12662 i--;
12663 if (low_bound
12664 && TREE_CODE (low_bound) == INTEGER_CST
12665 && TYPE_PRECISION (TREE_TYPE (low_bound))
12666 > TYPE_PRECISION (sizetype))
12667 low_bound = fold_convert (sizetype, low_bound);
12668 if (length
12669 && TREE_CODE (length) == INTEGER_CST
12670 && TYPE_PRECISION (TREE_TYPE (length))
12671 > TYPE_PRECISION (sizetype))
12672 length = fold_convert (sizetype, length);
12673 if (low_bound == NULL_TREE)
12674 low_bound = integer_zero_node;
12675 if (!maybe_zero_len && i > first_non_one)
12676 {
12677 if (integer_nonzerop (low_bound))
12678 goto do_warn_noncontiguous;
12679 if (length != NULL_TREE
12680 && TREE_CODE (length) == INTEGER_CST
12681 && TYPE_DOMAIN (types[i])
12682 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12683 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12684 == INTEGER_CST)
12685 {
12686 tree size;
12687 size = size_binop (PLUS_EXPR,
12688 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12689 size_one_node);
12690 if (!tree_int_cst_equal (length, size))
12691 {
12692 do_warn_noncontiguous:
12693 error_at (OMP_CLAUSE_LOCATION (c),
12694 "array section is not contiguous in %qs "
12695 "clause",
12696 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12697 return true;
12698 }
12699 }
12700 if (length != NULL_TREE
12701 && TREE_SIDE_EFFECTS (length))
12702 {
12703 if (side_effects == NULL_TREE)
12704 side_effects = length;
12705 else
12706 side_effects = build2 (COMPOUND_EXPR,
12707 TREE_TYPE (side_effects),
12708 length, side_effects);
12709 }
12710 }
12711 else
12712 {
12713 tree l;
12714
d9a6bd32
JJ
12715 if (i > first_non_one
12716 && ((length && integer_nonzerop (length))
12717 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12718 continue;
12719 if (length)
12720 l = fold_convert (sizetype, length);
12721 else
12722 {
12723 l = size_binop (PLUS_EXPR,
12724 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12725 size_one_node);
12726 l = size_binop (MINUS_EXPR, l,
12727 fold_convert (sizetype, low_bound));
12728 }
12729 if (i > first_non_one)
12730 {
12731 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12732 size_zero_node);
12733 if (condition == NULL_TREE)
12734 condition = l;
12735 else
12736 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12737 l, condition);
12738 }
12739 else if (size == NULL_TREE)
12740 {
12741 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12742 tree eltype = TREE_TYPE (types[num - 1]);
12743 while (TREE_CODE (eltype) == ARRAY_TYPE)
12744 eltype = TREE_TYPE (eltype);
12745 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12746 {
12747 if (integer_zerop (size)
12748 || integer_zerop (size_in_bytes (eltype)))
12749 {
12750 error_at (OMP_CLAUSE_LOCATION (c),
12751 "zero length array section in %qs clause",
12752 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12753 return error_mark_node;
12754 }
12755 size = size_binop (EXACT_DIV_EXPR, size,
12756 size_in_bytes (eltype));
12757 }
acf0174b
JJ
12758 size = size_binop (MULT_EXPR, size, l);
12759 if (condition)
12760 size = fold_build3 (COND_EXPR, sizetype, condition,
12761 size, size_zero_node);
12762 }
12763 else
12764 size = size_binop (MULT_EXPR, size, l);
12765 }
12766 }
acf0174b
JJ
12767 if (side_effects)
12768 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12769 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12770 {
12771 size = size_binop (MINUS_EXPR, size, size_one_node);
12772 size = c_fully_fold (size, false, NULL);
12773 tree index_type = build_index_type (size);
12774 tree eltype = TREE_TYPE (first);
12775 while (TREE_CODE (eltype) == ARRAY_TYPE)
12776 eltype = TREE_TYPE (eltype);
12777 tree type = build_array_type (eltype, index_type);
12778 tree ptype = build_pointer_type (eltype);
12779 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12780 t = build_fold_addr_expr (t);
e01d41e5
JJ
12781 tree t2 = build_fold_addr_expr (first);
12782 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12783 ptrdiff_type_node, t2);
12784 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12785 ptrdiff_type_node, t2,
12786 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12787 ptrdiff_type_node, t));
12788 t2 = c_fully_fold (t2, false, NULL);
12789 if (tree_fits_shwi_p (t2))
12790 t = build2 (MEM_REF, type, t,
12791 build_int_cst (ptype, tree_to_shwi (t2)));
12792 else
12793 {
12794 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12795 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12796 TREE_TYPE (t), t, t2);
12797 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12798 }
d9a6bd32
JJ
12799 OMP_CLAUSE_DECL (c) = t;
12800 return false;
12801 }
acf0174b
JJ
12802 first = c_fully_fold (first, false, NULL);
12803 OMP_CLAUSE_DECL (c) = first;
12804 if (size)
12805 size = c_fully_fold (size, false, NULL);
12806 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12807 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12808 || (TREE_CODE (t) == COMPONENT_REF
12809 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12810 return false;
41dbbb37 12811 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
e46c7770 12812 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
d9a6bd32
JJ
12813 switch (OMP_CLAUSE_MAP_KIND (c))
12814 {
12815 case GOMP_MAP_ALLOC:
12816 case GOMP_MAP_TO:
12817 case GOMP_MAP_FROM:
12818 case GOMP_MAP_TOFROM:
12819 case GOMP_MAP_ALWAYS_TO:
12820 case GOMP_MAP_ALWAYS_FROM:
12821 case GOMP_MAP_ALWAYS_TOFROM:
12822 case GOMP_MAP_RELEASE:
12823 case GOMP_MAP_DELETE:
b1c9c068
CP
12824 case GOMP_MAP_FORCE_TO:
12825 case GOMP_MAP_FORCE_FROM:
12826 case GOMP_MAP_FORCE_TOFROM:
12827 case GOMP_MAP_FORCE_PRESENT:
d9a6bd32
JJ
12828 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12829 break;
12830 default:
12831 break;
12832 }
acf0174b 12833 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e46c7770 12834 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
e01d41e5
JJ
12835 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12836 else if (TREE_CODE (t) == COMPONENT_REF)
12837 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12838 else
12839 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12840 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12841 && !c_mark_addressable (t))
acf0174b
JJ
12842 return false;
12843 OMP_CLAUSE_DECL (c2) = t;
12844 t = build_fold_addr_expr (first);
12845 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12846 tree ptr = OMP_CLAUSE_DECL (c2);
12847 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12848 ptr = build_fold_addr_expr (ptr);
12849 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12850 ptrdiff_type_node, t,
12851 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12852 ptrdiff_type_node, ptr));
12853 t = c_fully_fold (t, false, NULL);
12854 OMP_CLAUSE_SIZE (c2) = t;
12855 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12856 OMP_CLAUSE_CHAIN (c) = c2;
12857 }
12858 return false;
12859}
12860
12861/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12862 an inline call. But, remap
12863 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12864 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12865
12866static tree
12867c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12868 tree decl, tree placeholder)
12869{
12870 copy_body_data id;
b787e7a2 12871 hash_map<tree, tree> decl_map;
acf0174b 12872
b787e7a2
TS
12873 decl_map.put (omp_decl1, placeholder);
12874 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12875 memset (&id, 0, sizeof (id));
12876 id.src_fn = DECL_CONTEXT (omp_decl1);
12877 id.dst_fn = current_function_decl;
12878 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12879 id.decl_map = &decl_map;
acf0174b
JJ
12880
12881 id.copy_decl = copy_decl_no_change;
12882 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12883 id.transform_new_cfg = true;
12884 id.transform_return_to_modify = false;
12885 id.transform_lang_insert_block = NULL;
12886 id.eh_lp_nr = 0;
12887 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12888 return stmt;
12889}
12890
12891/* Helper function of c_finish_omp_clauses, called via walk_tree.
12892 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12893
12894static tree
12895c_find_omp_placeholder_r (tree *tp, int *, void *data)
12896{
12897 if (*tp == (tree) data)
12898 return *tp;
12899 return NULL_TREE;
12900}
12901
41dbbb37 12902/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12903 Remove any elements from the list that are invalid. */
12904
12905tree
77886428 12906c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
953ff289
DN
12907{
12908 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e46c7770 12909 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
d9a6bd32
JJ
12910 tree c, t, type, *pc;
12911 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12912 bool branch_seen = false;
12913 bool copyprivate_seen = false;
e01d41e5 12914 bool linear_variable_step_check = false;
acf0174b 12915 tree *nowait_clause = NULL;
e01d41e5
JJ
12916 bool ordered_seen = false;
12917 tree schedule_clause = NULL_TREE;
b605f663 12918 bool oacc_async = false;
953ff289
DN
12919
12920 bitmap_obstack_initialize (NULL);
12921 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12922 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12923 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12924 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12925 bitmap_initialize (&map_head, &bitmap_default_obstack);
12926 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
e46c7770 12927 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
953ff289 12928
b605f663
CLT
12929 if (ort & C_ORT_ACC)
12930 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12931 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12932 {
12933 oacc_async = true;
12934 break;
12935 }
9dc5773f 12936
953ff289
DN
12937 for (pc = &clauses, c = clauses; c ; c = *pc)
12938 {
12939 bool remove = false;
12940 bool need_complete = false;
12941 bool need_implicitly_determined = false;
12942
aaf46ef9 12943 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12944 {
12945 case OMP_CLAUSE_SHARED:
953ff289
DN
12946 need_implicitly_determined = true;
12947 goto check_dup_generic;
12948
12949 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12950 need_complete = true;
12951 need_implicitly_determined = true;
12952 goto check_dup_generic;
12953
12954 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12955 need_implicitly_determined = true;
12956 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12957 if (TREE_CODE (t) == TREE_LIST)
12958 {
e46c7770 12959 if (handle_omp_array_sections (c, ort))
d9a6bd32
JJ
12960 {
12961 remove = true;
12962 break;
12963 }
12964
12965 t = OMP_CLAUSE_DECL (c);
12966 }
4f2e1536 12967 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
d9a6bd32
JJ
12968 if (t == error_mark_node)
12969 {
12970 remove = true;
12971 break;
12972 }
b605f663
CLT
12973 if (oacc_async)
12974 c_mark_addressable (t);
d9a6bd32
JJ
12975 type = TREE_TYPE (t);
12976 if (TREE_CODE (t) == MEM_REF)
12977 type = TREE_TYPE (type);
12978 if (TREE_CODE (type) == ARRAY_TYPE)
12979 {
12980 tree oatype = type;
12981 gcc_assert (TREE_CODE (t) != MEM_REF);
12982 while (TREE_CODE (type) == ARRAY_TYPE)
12983 type = TREE_TYPE (type);
12984 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12985 {
12986 error_at (OMP_CLAUSE_LOCATION (c),
12987 "%qD in %<reduction%> clause is a zero size array",
12988 t);
12989 remove = true;
12990 break;
12991 }
12992 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12993 TYPE_SIZE_UNIT (type));
12994 if (integer_zerop (size))
12995 {
12996 error_at (OMP_CLAUSE_LOCATION (c),
12997 "%qD in %<reduction%> clause is a zero size array",
12998 t);
12999 remove = true;
13000 break;
13001 }
13002 size = size_binop (MINUS_EXPR, size, size_one_node);
13003 tree index_type = build_index_type (size);
13004 tree atype = build_array_type (type, index_type);
13005 tree ptype = build_pointer_type (type);
13006 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13007 t = build_fold_addr_expr (t);
13008 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13009 OMP_CLAUSE_DECL (c) = t;
13010 }
9dc5773f
JJ
13011 if (TYPE_ATOMIC (type))
13012 {
13013 error_at (OMP_CLAUSE_LOCATION (c),
13014 "%<_Atomic%> %qE in %<reduction%> clause", t);
13015 remove = true;
13016 break;
13017 }
acf0174b 13018 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
13019 && (FLOAT_TYPE_P (type)
13020 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
13021 {
13022 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13023 const char *r_name = NULL;
13024
13025 switch (r_code)
13026 {
13027 case PLUS_EXPR:
13028 case MULT_EXPR:
13029 case MINUS_EXPR:
652fea39 13030 break;
20906c66 13031 case MIN_EXPR:
d9a6bd32 13032 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
13033 r_name = "min";
13034 break;
20906c66 13035 case MAX_EXPR:
d9a6bd32 13036 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 13037 r_name = "max";
953ff289
DN
13038 break;
13039 case BIT_AND_EXPR:
13040 r_name = "&";
13041 break;
13042 case BIT_XOR_EXPR:
13043 r_name = "^";
13044 break;
13045 case BIT_IOR_EXPR:
13046 r_name = "|";
13047 break;
13048 case TRUTH_ANDIF_EXPR:
d9a6bd32 13049 if (FLOAT_TYPE_P (type))
652fea39 13050 r_name = "&&";
953ff289
DN
13051 break;
13052 case TRUTH_ORIF_EXPR:
d9a6bd32 13053 if (FLOAT_TYPE_P (type))
652fea39 13054 r_name = "||";
953ff289
DN
13055 break;
13056 default:
13057 gcc_unreachable ();
13058 }
13059 if (r_name)
13060 {
c2255bc4
AH
13061 error_at (OMP_CLAUSE_LOCATION (c),
13062 "%qE has invalid type for %<reduction(%s)%>",
13063 t, r_name);
953ff289 13064 remove = true;
ee1d5a02 13065 break;
953ff289
DN
13066 }
13067 }
acf0174b
JJ
13068 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13069 {
13070 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 13071 "user defined reduction not found for %qE", t);
acf0174b 13072 remove = true;
ee1d5a02 13073 break;
acf0174b
JJ
13074 }
13075 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13076 {
13077 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 13078 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
13079 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13080 VAR_DECL, NULL_TREE, type);
d9a6bd32 13081 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
13082 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13083 DECL_ARTIFICIAL (placeholder) = 1;
13084 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
13085 if (TREE_CODE (t) == MEM_REF)
13086 {
13087 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13088 VAR_DECL, NULL_TREE, type);
13089 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13090 DECL_ARTIFICIAL (decl_placeholder) = 1;
13091 DECL_IGNORED_P (decl_placeholder) = 1;
13092 }
acf0174b
JJ
13093 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13094 c_mark_addressable (placeholder);
13095 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
13096 c_mark_addressable (decl_placeholder ? decl_placeholder
13097 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
13098 OMP_CLAUSE_REDUCTION_MERGE (c)
13099 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13100 TREE_VEC_ELT (list, 0),
13101 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
13102 decl_placeholder ? decl_placeholder
13103 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
13104 OMP_CLAUSE_REDUCTION_MERGE (c)
13105 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13106 void_type_node, NULL_TREE,
d9a6bd32 13107 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
13108 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13109 if (TREE_VEC_LENGTH (list) == 6)
13110 {
13111 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
13112 c_mark_addressable (decl_placeholder ? decl_placeholder
13113 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
13114 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13115 c_mark_addressable (placeholder);
13116 tree init = TREE_VEC_ELT (list, 5);
13117 if (init == error_mark_node)
13118 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13119 OMP_CLAUSE_REDUCTION_INIT (c)
13120 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13121 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
13122 decl_placeholder ? decl_placeholder
13123 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 13124 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
13125 {
13126 tree v = decl_placeholder ? decl_placeholder : t;
13127 OMP_CLAUSE_REDUCTION_INIT (c)
13128 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13129 OMP_CLAUSE_REDUCTION_INIT (c));
13130 }
acf0174b
JJ
13131 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13132 c_find_omp_placeholder_r,
13133 placeholder, NULL))
13134 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13135 }
13136 else
13137 {
13138 tree init;
d9a6bd32
JJ
13139 tree v = decl_placeholder ? decl_placeholder : t;
13140 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13141 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 13142 else
d9a6bd32 13143 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 13144 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 13145 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
13146 }
13147 OMP_CLAUSE_REDUCTION_INIT (c)
13148 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13149 void_type_node, NULL_TREE,
13150 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13151 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13152 }
d9a6bd32
JJ
13153 if (TREE_CODE (t) == MEM_REF)
13154 {
13155 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13156 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13157 != INTEGER_CST)
13158 {
13159 sorry ("variable length element type in array "
13160 "%<reduction%> clause");
13161 remove = true;
13162 break;
13163 }
13164 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
13165 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13166 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
13167 if (TREE_CODE (t) == ADDR_EXPR)
13168 t = TREE_OPERAND (t, 0);
13169 }
13170 goto check_dup_generic_t;
953ff289
DN
13171
13172 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
13173 copyprivate_seen = true;
13174 if (nowait_clause)
13175 {
13176 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13177 "%<nowait%> clause must not be used together "
13178 "with %<copyprivate%>");
13179 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13180 nowait_clause = NULL;
13181 }
953ff289
DN
13182 goto check_dup_generic;
13183
13184 case OMP_CLAUSE_COPYIN:
953ff289 13185 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13186 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 13187 {
c2255bc4
AH
13188 error_at (OMP_CLAUSE_LOCATION (c),
13189 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 13190 remove = true;
ee1d5a02 13191 break;
953ff289
DN
13192 }
13193 goto check_dup_generic;
13194
acf0174b 13195 case OMP_CLAUSE_LINEAR:
77886428 13196 if (ort != C_ORT_OMP_DECLARE_SIMD)
d9a6bd32 13197 need_implicitly_determined = true;
acf0174b 13198 t = OMP_CLAUSE_DECL (c);
77886428 13199 if (ort != C_ORT_OMP_DECLARE_SIMD
d9a6bd32
JJ
13200 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13201 {
13202 error_at (OMP_CLAUSE_LOCATION (c),
13203 "modifier should not be specified in %<linear%> "
13204 "clause on %<simd%> or %<for%> constructs");
13205 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13206 }
5e9d6aa4
JK
13207 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13208 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
acf0174b 13209 {
5e9d6aa4
JK
13210 error_at (OMP_CLAUSE_LOCATION (c),
13211 "linear clause applied to non-integral non-pointer "
13212 "variable with type %qT", TREE_TYPE (t));
13213 remove = true;
13214 break;
477d4906 13215 }
5e9d6aa4 13216 if (TYPE_ATOMIC (TREE_TYPE (t)))
477d4906 13217 {
5e9d6aa4
JK
13218 error_at (OMP_CLAUSE_LOCATION (c),
13219 "%<_Atomic%> %qD in %<linear%> clause", t);
13220 remove = true;
13221 break;
acf0174b 13222 }
77886428 13223 if (ort == C_ORT_OMP_DECLARE_SIMD)
e01d41e5
JJ
13224 {
13225 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13226 if (TREE_CODE (s) == PARM_DECL)
13227 {
13228 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13229 /* map_head bitmap is used as uniform_head if
13230 declare_simd. */
13231 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13232 linear_variable_step_check = true;
13233 goto check_dup_generic;
13234 }
13235 if (TREE_CODE (s) != INTEGER_CST)
13236 {
13237 error_at (OMP_CLAUSE_LOCATION (c),
13238 "%<linear%> clause step %qE is neither constant "
13239 "nor a parameter", s);
13240 remove = true;
13241 break;
13242 }
13243 }
acf0174b
JJ
13244 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13245 {
13246 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13247 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13248 OMP_CLAUSE_DECL (c), s);
13249 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13250 sizetype, fold_convert (sizetype, s),
13251 fold_convert
13252 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
13253 if (s == error_mark_node)
13254 s = size_one_node;
13255 OMP_CLAUSE_LINEAR_STEP (c) = s;
13256 }
da6f124d
JJ
13257 else
13258 OMP_CLAUSE_LINEAR_STEP (c)
13259 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
13260 goto check_dup_generic;
13261
953ff289
DN
13262 check_dup_generic:
13263 t = OMP_CLAUSE_DECL (c);
d9a6bd32 13264 check_dup_generic_t:
0ae9bd27 13265 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13266 {
c2255bc4 13267 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
13268 "%qE is not a variable in clause %qs", t,
13269 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13270 remove = true;
13271 }
e46c7770
CP
13272 else if (ort == C_ORT_ACC
13273 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13274 {
13275 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13276 {
13277 error ("%qD appears more than once in reduction clauses", t);
13278 remove = true;
13279 }
13280 else
13281 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13282 }
953ff289
DN
13283 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13284 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13285 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13286 {
c2255bc4
AH
13287 error_at (OMP_CLAUSE_LOCATION (c),
13288 "%qE appears more than once in data clauses", t);
953ff289
DN
13289 remove = true;
13290 }
e01d41e5
JJ
13291 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13292 && bitmap_bit_p (&map_head, DECL_UID (t)))
13293 {
e46c7770
CP
13294 if (ort == C_ORT_ACC)
13295 error ("%qD appears more than once in data clauses", t);
13296 else
13297 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13298 remove = true;
13299 }
953ff289
DN
13300 else
13301 bitmap_set_bit (&generic_head, DECL_UID (t));
13302 break;
13303
13304 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
13305 t = OMP_CLAUSE_DECL (c);
13306 need_complete = true;
13307 need_implicitly_determined = true;
0ae9bd27 13308 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13309 {
c2255bc4
AH
13310 error_at (OMP_CLAUSE_LOCATION (c),
13311 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
13312 remove = true;
13313 }
13314 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13315 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13316 {
c2255bc4
AH
13317 error_at (OMP_CLAUSE_LOCATION (c),
13318 "%qE appears more than once in data clauses", t);
953ff289
DN
13319 remove = true;
13320 }
e01d41e5
JJ
13321 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13322 {
e46c7770
CP
13323 if (ort == C_ORT_ACC)
13324 error ("%qD appears more than once in data clauses", t);
13325 else
13326 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13327 remove = true;
13328 }
953ff289
DN
13329 else
13330 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13331 break;
13332
13333 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
13334 t = OMP_CLAUSE_DECL (c);
13335 need_complete = true;
13336 need_implicitly_determined = true;
0ae9bd27 13337 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13338 {
c2255bc4
AH
13339 error_at (OMP_CLAUSE_LOCATION (c),
13340 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
13341 remove = true;
13342 }
13343 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13344 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13345 {
c2255bc4
AH
13346 error_at (OMP_CLAUSE_LOCATION (c),
13347 "%qE appears more than once in data clauses", t);
953ff289
DN
13348 remove = true;
13349 }
13350 else
13351 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13352 break;
13353
acf0174b
JJ
13354 case OMP_CLAUSE_ALIGNED:
13355 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13356 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13357 {
13358 error_at (OMP_CLAUSE_LOCATION (c),
13359 "%qE is not a variable in %<aligned%> clause", t);
13360 remove = true;
13361 }
5a9785fb
JJ
13362 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13363 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13364 {
13365 error_at (OMP_CLAUSE_LOCATION (c),
13366 "%qE in %<aligned%> clause is neither a pointer nor "
13367 "an array", t);
13368 remove = true;
13369 }
9dc5773f
JJ
13370 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13371 {
13372 error_at (OMP_CLAUSE_LOCATION (c),
13373 "%<_Atomic%> %qD in %<aligned%> clause", t);
13374 remove = true;
13375 break;
13376 }
acf0174b
JJ
13377 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13378 {
13379 error_at (OMP_CLAUSE_LOCATION (c),
13380 "%qE appears more than once in %<aligned%> clauses",
13381 t);
13382 remove = true;
13383 }
13384 else
13385 bitmap_set_bit (&aligned_head, DECL_UID (t));
13386 break;
13387
13388 case OMP_CLAUSE_DEPEND:
13389 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13390 if (t == NULL_TREE)
13391 {
13392 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13393 == OMP_CLAUSE_DEPEND_SOURCE);
13394 break;
13395 }
13396 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13397 {
13398 gcc_assert (TREE_CODE (t) == TREE_LIST);
13399 for (; t; t = TREE_CHAIN (t))
13400 {
13401 tree decl = TREE_VALUE (t);
13402 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13403 {
13404 tree offset = TREE_PURPOSE (t);
8e6cdc90 13405 bool neg = wi::neg_p (wi::to_wide (offset));
d9a6bd32
JJ
13406 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13407 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13408 neg ? MINUS_EXPR : PLUS_EXPR,
13409 decl, offset);
13410 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13411 sizetype,
13412 fold_convert (sizetype, t2),
13413 fold_convert (sizetype, decl));
d9a6bd32
JJ
13414 if (t2 == error_mark_node)
13415 {
13416 remove = true;
13417 break;
13418 }
13419 TREE_PURPOSE (t) = t2;
13420 }
13421 }
13422 break;
13423 }
acf0174b
JJ
13424 if (TREE_CODE (t) == TREE_LIST)
13425 {
e46c7770 13426 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13427 remove = true;
13428 break;
13429 }
13430 if (t == error_mark_node)
13431 remove = true;
0ae9bd27 13432 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13433 {
13434 error_at (OMP_CLAUSE_LOCATION (c),
13435 "%qE is not a variable in %<depend%> clause", t);
13436 remove = true;
13437 }
13438 else if (!c_mark_addressable (t))
13439 remove = true;
13440 break;
13441
13442 case OMP_CLAUSE_MAP:
13443 case OMP_CLAUSE_TO:
13444 case OMP_CLAUSE_FROM:
41dbbb37 13445 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
13446 t = OMP_CLAUSE_DECL (c);
13447 if (TREE_CODE (t) == TREE_LIST)
13448 {
e46c7770 13449 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13450 remove = true;
13451 else
13452 {
13453 t = OMP_CLAUSE_DECL (c);
b17a8b07 13454 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13455 {
13456 error_at (OMP_CLAUSE_LOCATION (c),
13457 "array section does not have mappable type "
13458 "in %qs clause",
13459 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13460 remove = true;
13461 }
9dc5773f
JJ
13462 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13463 {
13464 error_at (OMP_CLAUSE_LOCATION (c),
13465 "%<_Atomic%> %qE in %qs clause", t,
13466 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13467 remove = true;
13468 }
d9a6bd32
JJ
13469 while (TREE_CODE (t) == ARRAY_REF)
13470 t = TREE_OPERAND (t, 0);
13471 if (TREE_CODE (t) == COMPONENT_REF
13472 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13473 {
13474 while (TREE_CODE (t) == COMPONENT_REF)
13475 t = TREE_OPERAND (t, 0);
13476 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13477 break;
13478 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13479 {
13480 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13481 error ("%qD appears more than once in motion"
13482 " clauses", t);
e46c7770
CP
13483 else if (ort == C_ORT_ACC)
13484 error ("%qD appears more than once in data"
13485 " clauses", t);
d9a6bd32
JJ
13486 else
13487 error ("%qD appears more than once in map"
13488 " clauses", t);
13489 remove = true;
13490 }
13491 else
13492 {
13493 bitmap_set_bit (&map_head, DECL_UID (t));
13494 bitmap_set_bit (&map_field_head, DECL_UID (t));
13495 }
13496 }
acf0174b
JJ
13497 }
13498 break;
13499 }
13500 if (t == error_mark_node)
d9a6bd32
JJ
13501 {
13502 remove = true;
13503 break;
13504 }
13505 if (TREE_CODE (t) == COMPONENT_REF
77886428 13506 && (ort & C_ORT_OMP)
d9a6bd32
JJ
13507 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13508 {
13509 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13510 {
13511 error_at (OMP_CLAUSE_LOCATION (c),
13512 "bit-field %qE in %qs clause",
13513 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13514 remove = true;
13515 }
13516 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13517 {
13518 error_at (OMP_CLAUSE_LOCATION (c),
13519 "%qE does not have a mappable type in %qs clause",
13520 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13521 remove = true;
13522 }
9dc5773f
JJ
13523 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13524 {
13525 error_at (OMP_CLAUSE_LOCATION (c),
13526 "%<_Atomic%> %qE in %qs clause", t,
13527 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13528 remove = true;
13529 }
d9a6bd32
JJ
13530 while (TREE_CODE (t) == COMPONENT_REF)
13531 {
13532 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13533 == UNION_TYPE)
13534 {
13535 error_at (OMP_CLAUSE_LOCATION (c),
13536 "%qE is a member of a union", t);
13537 remove = true;
13538 break;
13539 }
13540 t = TREE_OPERAND (t, 0);
13541 }
13542 if (remove)
13543 break;
13544 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13545 {
e01d41e5 13546 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13547 break;
13548 }
13549 }
13550 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13551 {
13552 error_at (OMP_CLAUSE_LOCATION (c),
13553 "%qE is not a variable in %qs clause", t,
13554 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13555 remove = true;
13556 }
0ae9bd27 13557 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13558 {
13559 error_at (OMP_CLAUSE_LOCATION (c),
13560 "%qD is threadprivate variable in %qs clause", t,
13561 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13562 remove = true;
13563 }
e4606348
JJ
13564 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13565 || (OMP_CLAUSE_MAP_KIND (c)
13566 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13567 && !c_mark_addressable (t))
acf0174b 13568 remove = true;
b17a8b07 13569 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13570 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13571 || (OMP_CLAUSE_MAP_KIND (c)
13572 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13573 || (OMP_CLAUSE_MAP_KIND (c)
13574 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13575 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13576 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13577 {
13578 error_at (OMP_CLAUSE_LOCATION (c),
13579 "%qD does not have a mappable type in %qs clause", t,
13580 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13581 remove = true;
13582 }
9dc5773f
JJ
13583 else if (TREE_TYPE (t) == error_mark_node)
13584 remove = true;
13585 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13586 {
13587 error_at (OMP_CLAUSE_LOCATION (c),
13588 "%<_Atomic%> %qE in %qs clause", t,
13589 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13590 remove = true;
13591 }
d9a6bd32
JJ
13592 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13593 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13594 {
13595 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13596 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13597 {
13598 error ("%qD appears more than once in data clauses", t);
13599 remove = true;
13600 }
e01d41e5 13601 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13602 {
e46c7770
CP
13603 if (ort == C_ORT_ACC)
13604 error ("%qD appears more than once in data clauses", t);
13605 else
13606 error ("%qD appears both in data and map clauses", t);
e01d41e5 13607 remove = true;
d9a6bd32 13608 }
e01d41e5
JJ
13609 else
13610 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13611 }
13612 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13613 {
13614 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13615 error ("%qD appears more than once in motion clauses", t);
e46c7770
CP
13616 else if (ort == C_ORT_ACC)
13617 error ("%qD appears more than once in data clauses", t);
acf0174b
JJ
13618 else
13619 error ("%qD appears more than once in map clauses", t);
13620 remove = true;
13621 }
e01d41e5
JJ
13622 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13623 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13624 {
e46c7770
CP
13625 if (ort == C_ORT_ACC)
13626 error ("%qD appears more than once in data clauses", t);
13627 else
13628 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13629 remove = true;
13630 }
acf0174b 13631 else
d9a6bd32
JJ
13632 {
13633 bitmap_set_bit (&map_head, DECL_UID (t));
13634 if (t != OMP_CLAUSE_DECL (c)
13635 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13636 bitmap_set_bit (&map_field_head, DECL_UID (t));
13637 }
13638 break;
13639
13640 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13641 case OMP_CLAUSE_LINK:
13642 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13643 if (TREE_CODE (t) == FUNCTION_DECL
13644 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13645 ;
13646 else if (!VAR_P (t))
d9a6bd32 13647 {
e01d41e5
JJ
13648 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13649 error_at (OMP_CLAUSE_LOCATION (c),
13650 "%qE is neither a variable nor a function name in "
13651 "clause %qs", t,
13652 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13653 else
13654 error_at (OMP_CLAUSE_LOCATION (c),
13655 "%qE is not a variable in clause %qs", t,
13656 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13657 remove = true;
13658 }
13659 else if (DECL_THREAD_LOCAL_P (t))
13660 {
13661 error_at (OMP_CLAUSE_LOCATION (c),
13662 "%qD is threadprivate variable in %qs clause", t,
13663 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13664 remove = true;
13665 }
13666 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13667 {
13668 error_at (OMP_CLAUSE_LOCATION (c),
13669 "%qD does not have a mappable type in %qs clause", t,
13670 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13671 remove = true;
13672 }
e01d41e5
JJ
13673 if (remove)
13674 break;
13675 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13676 {
13677 error_at (OMP_CLAUSE_LOCATION (c),
13678 "%qE appears more than once on the same "
13679 "%<declare target%> directive", t);
13680 remove = true;
13681 }
13682 else
13683 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13684 break;
13685
13686 case OMP_CLAUSE_UNIFORM:
13687 t = OMP_CLAUSE_DECL (c);
13688 if (TREE_CODE (t) != PARM_DECL)
13689 {
13690 if (DECL_P (t))
13691 error_at (OMP_CLAUSE_LOCATION (c),
13692 "%qD is not an argument in %<uniform%> clause", t);
13693 else
13694 error_at (OMP_CLAUSE_LOCATION (c),
13695 "%qE is not an argument in %<uniform%> clause", t);
13696 remove = true;
ee1d5a02 13697 break;
acf0174b 13698 }
e01d41e5
JJ
13699 /* map_head bitmap is used as uniform_head if declare_simd. */
13700 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13701 goto check_dup_generic;
acf0174b 13702
d9a6bd32
JJ
13703 case OMP_CLAUSE_IS_DEVICE_PTR:
13704 case OMP_CLAUSE_USE_DEVICE_PTR:
13705 t = OMP_CLAUSE_DECL (c);
13706 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13707 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13708 {
13709 error_at (OMP_CLAUSE_LOCATION (c),
13710 "%qs variable is neither a pointer nor an array",
13711 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13712 remove = true;
13713 }
13714 goto check_dup_generic;
13715
acf0174b
JJ
13716 case OMP_CLAUSE_NOWAIT:
13717 if (copyprivate_seen)
13718 {
13719 error_at (OMP_CLAUSE_LOCATION (c),
13720 "%<nowait%> clause must not be used together "
13721 "with %<copyprivate%>");
13722 remove = true;
13723 break;
13724 }
13725 nowait_clause = pc;
13726 pc = &OMP_CLAUSE_CHAIN (c);
13727 continue;
13728
953ff289
DN
13729 case OMP_CLAUSE_IF:
13730 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13731 case OMP_CLAUSE_NUM_TEAMS:
13732 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13733 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13734 case OMP_CLAUSE_UNTIED:
13735 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13736 case OMP_CLAUSE_FINAL:
13737 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13738 case OMP_CLAUSE_DEVICE:
13739 case OMP_CLAUSE_DIST_SCHEDULE:
13740 case OMP_CLAUSE_PARALLEL:
13741 case OMP_CLAUSE_FOR:
13742 case OMP_CLAUSE_SECTIONS:
13743 case OMP_CLAUSE_TASKGROUP:
13744 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13745 case OMP_CLAUSE_PRIORITY:
13746 case OMP_CLAUSE_GRAINSIZE:
13747 case OMP_CLAUSE_NUM_TASKS:
13748 case OMP_CLAUSE_NOGROUP:
13749 case OMP_CLAUSE_THREADS:
13750 case OMP_CLAUSE_SIMD:
13751 case OMP_CLAUSE_HINT:
13752 case OMP_CLAUSE_DEFAULTMAP:
41dbbb37
TS
13753 case OMP_CLAUSE_NUM_GANGS:
13754 case OMP_CLAUSE_NUM_WORKERS:
13755 case OMP_CLAUSE_VECTOR_LENGTH:
13756 case OMP_CLAUSE_ASYNC:
13757 case OMP_CLAUSE_WAIT:
13758 case OMP_CLAUSE_AUTO:
7a5e4956 13759 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13760 case OMP_CLAUSE_SEQ:
13761 case OMP_CLAUSE_GANG:
13762 case OMP_CLAUSE_WORKER:
13763 case OMP_CLAUSE_VECTOR:
7a5e4956 13764 case OMP_CLAUSE_TILE:
acf0174b
JJ
13765 pc = &OMP_CLAUSE_CHAIN (c);
13766 continue;
13767
e01d41e5
JJ
13768 case OMP_CLAUSE_SCHEDULE:
13769 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13770 {
13771 const char *p = NULL;
13772 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13773 {
13774 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13775 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13776 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13777 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13778 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13779 default: gcc_unreachable ();
13780 }
13781 if (p)
13782 {
13783 error_at (OMP_CLAUSE_LOCATION (c),
13784 "%<nonmonotonic%> modifier specified for %qs "
13785 "schedule kind", p);
13786 OMP_CLAUSE_SCHEDULE_KIND (c)
13787 = (enum omp_clause_schedule_kind)
13788 (OMP_CLAUSE_SCHEDULE_KIND (c)
13789 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13790 }
13791 }
13792 schedule_clause = c;
13793 pc = &OMP_CLAUSE_CHAIN (c);
13794 continue;
13795
13796 case OMP_CLAUSE_ORDERED:
13797 ordered_seen = true;
13798 pc = &OMP_CLAUSE_CHAIN (c);
13799 continue;
13800
d9a6bd32
JJ
13801 case OMP_CLAUSE_SAFELEN:
13802 safelen = c;
13803 pc = &OMP_CLAUSE_CHAIN (c);
13804 continue;
13805 case OMP_CLAUSE_SIMDLEN:
13806 simdlen = c;
13807 pc = &OMP_CLAUSE_CHAIN (c);
13808 continue;
13809
acf0174b
JJ
13810 case OMP_CLAUSE_INBRANCH:
13811 case OMP_CLAUSE_NOTINBRANCH:
13812 if (branch_seen)
13813 {
13814 error_at (OMP_CLAUSE_LOCATION (c),
13815 "%<inbranch%> clause is incompatible with "
13816 "%<notinbranch%>");
13817 remove = true;
13818 break;
13819 }
13820 branch_seen = true;
953ff289
DN
13821 pc = &OMP_CLAUSE_CHAIN (c);
13822 continue;
13823
13824 default:
13825 gcc_unreachable ();
13826 }
13827
13828 if (!remove)
13829 {
13830 t = OMP_CLAUSE_DECL (c);
13831
13832 if (need_complete)
13833 {
4f2e1536 13834 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
953ff289
DN
13835 if (t == error_mark_node)
13836 remove = true;
13837 }
13838
13839 if (need_implicitly_determined)
13840 {
13841 const char *share_name = NULL;
13842
0ae9bd27 13843 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13844 share_name = "threadprivate";
13845 else switch (c_omp_predetermined_sharing (t))
13846 {
13847 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13848 break;
13849 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13850 /* const vars may be specified in firstprivate clause. */
13851 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13852 && TREE_READONLY (t))
13853 break;
953ff289
DN
13854 share_name = "shared";
13855 break;
13856 case OMP_CLAUSE_DEFAULT_PRIVATE:
13857 share_name = "private";
13858 break;
13859 default:
13860 gcc_unreachable ();
13861 }
13862 if (share_name)
13863 {
c2255bc4
AH
13864 error_at (OMP_CLAUSE_LOCATION (c),
13865 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13866 t, share_name,
13867 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13868 remove = true;
13869 }
13870 }
13871 }
13872
13873 if (remove)
13874 *pc = OMP_CLAUSE_CHAIN (c);
13875 else
13876 pc = &OMP_CLAUSE_CHAIN (c);
13877 }
13878
d9a6bd32
JJ
13879 if (simdlen
13880 && safelen
13881 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13882 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13883 {
13884 error_at (OMP_CLAUSE_LOCATION (simdlen),
13885 "%<simdlen%> clause value is bigger than "
13886 "%<safelen%> clause value");
13887 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13888 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13889 }
13890
e01d41e5
JJ
13891 if (ordered_seen
13892 && schedule_clause
13893 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13894 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13895 {
13896 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13897 "%<nonmonotonic%> schedule modifier specified together "
13898 "with %<ordered%> clause");
13899 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13900 = (enum omp_clause_schedule_kind)
13901 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13902 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13903 }
13904
13905 if (linear_variable_step_check)
13906 for (pc = &clauses, c = clauses; c ; c = *pc)
13907 {
13908 bool remove = false;
13909 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13910 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13911 && !bitmap_bit_p (&map_head,
13912 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13913 {
13914 error_at (OMP_CLAUSE_LOCATION (c),
13915 "%<linear%> clause step is a parameter %qD not "
13916 "specified in %<uniform%> clause",
13917 OMP_CLAUSE_LINEAR_STEP (c));
13918 remove = true;
13919 }
13920
13921 if (remove)
13922 *pc = OMP_CLAUSE_CHAIN (c);
13923 else
13924 pc = &OMP_CLAUSE_CHAIN (c);
13925 }
13926
953ff289
DN
13927 bitmap_obstack_release (NULL);
13928 return clauses;
13929}
9ae165a0 13930
9dc5773f
JJ
13931/* Return code to initialize DST with a copy constructor from SRC.
13932 C doesn't have copy constructors nor assignment operators, only for
13933 _Atomic vars we need to perform __atomic_load from src into a temporary
13934 followed by __atomic_store of the temporary to dst. */
13935
13936tree
13937c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13938{
13939 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13940 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13941
13942 location_t loc = OMP_CLAUSE_LOCATION (clause);
13943 tree type = TREE_TYPE (dst);
13944 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13945 tree tmp = create_tmp_var (nonatomic_type);
13946 tree tmp_addr = build_fold_addr_expr (tmp);
13947 TREE_ADDRESSABLE (tmp) = 1;
13948 TREE_NO_WARNING (tmp) = 1;
13949 tree src_addr = build_fold_addr_expr (src);
13950 tree dst_addr = build_fold_addr_expr (dst);
13951 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13952 vec<tree, va_gc> *params;
13953 /* Expansion of a generic atomic load may require an addition
13954 element, so allocate enough to prevent a resize. */
13955 vec_alloc (params, 4);
13956
13957 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13958 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13959 params->quick_push (src_addr);
13960 params->quick_push (tmp_addr);
13961 params->quick_push (seq_cst);
13962 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13963
13964 vec_alloc (params, 4);
13965
13966 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13967 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13968 params->quick_push (dst_addr);
13969 params->quick_push (tmp_addr);
13970 params->quick_push (seq_cst);
13971 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13972 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13973}
13974
0a35513e
AH
13975/* Create a transaction node. */
13976
13977tree
13978c_finish_transaction (location_t loc, tree block, int flags)
13979{
13980 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13981 if (flags & TM_STMT_ATTR_OUTER)
13982 TRANSACTION_EXPR_OUTER (stmt) = 1;
13983 if (flags & TM_STMT_ATTR_RELAXED)
13984 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13985 return add_stmt (stmt);
13986}
13987
9ae165a0 13988/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
13989 down to the element type of an array. If ORIG_QUAL_TYPE is not
13990 NULL, then it should be used as the qualified type
13991 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13992 preserve information about the typedef name from which an array
13993 type was derived). */
9ae165a0
DG
13994
13995tree
e9e32ee6
JM
13996c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13997 size_t orig_qual_indirect)
9ae165a0
DG
13998{
13999 if (type == error_mark_node)
14000 return type;
14001
14002 if (TREE_CODE (type) == ARRAY_TYPE)
14003 {
14004 tree t;
14005 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
14006 type_quals, orig_qual_type,
14007 orig_qual_indirect - 1);
9ae165a0
DG
14008
14009 /* See if we already have an identically qualified type. */
e9e32ee6
JM
14010 if (orig_qual_type && orig_qual_indirect == 0)
14011 t = orig_qual_type;
14012 else
14013 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14014 {
14015 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14016 && TYPE_NAME (t) == TYPE_NAME (type)
14017 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14018 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14019 TYPE_ATTRIBUTES (type)))
14020 break;
14021 }
9ae165a0
DG
14022 if (!t)
14023 {
14024 tree domain = TYPE_DOMAIN (type);
14025
14026 t = build_variant_type_copy (type);
14027 TREE_TYPE (t) = element_type;
14028
14029 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14030 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14031 SET_TYPE_STRUCTURAL_EQUALITY (t);
14032 else if (TYPE_CANONICAL (element_type) != element_type
14033 || (domain && TYPE_CANONICAL (domain) != domain))
14034 {
b8698a0f 14035 tree unqualified_canon
9ae165a0 14036 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 14037 domain? TYPE_CANONICAL (domain)
9ae165a0 14038 : NULL_TREE);
ee45a32d
EB
14039 if (TYPE_REVERSE_STORAGE_ORDER (type))
14040 {
14041 unqualified_canon
14042 = build_distinct_type_copy (unqualified_canon);
14043 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14044 }
b8698a0f 14045 TYPE_CANONICAL (t)
9ae165a0
DG
14046 = c_build_qualified_type (unqualified_canon, type_quals);
14047 }
14048 else
14049 TYPE_CANONICAL (t) = t;
14050 }
14051 return t;
14052 }
14053
14054 /* A restrict-qualified pointer type must be a pointer to object or
14055 incomplete type. Note that the use of POINTER_TYPE_P also allows
14056 REFERENCE_TYPEs, which is appropriate for C++. */
14057 if ((type_quals & TYPE_QUAL_RESTRICT)
14058 && (!POINTER_TYPE_P (type)
14059 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14060 {
14061 error ("invalid use of %<restrict%>");
14062 type_quals &= ~TYPE_QUAL_RESTRICT;
14063 }
14064
e9e32ee6
JM
14065 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14066 ? orig_qual_type
14067 : build_qualified_type (type, type_quals));
e922069e
JW
14068 /* A variant type does not inherit the list of incomplete vars from the
14069 type main variant. */
152ef731
JJ
14070 if (RECORD_OR_UNION_TYPE_P (var_type)
14071 && TYPE_MAIN_VARIANT (var_type) != var_type)
e922069e
JW
14072 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14073 return var_type;
9ae165a0 14074}
72b5577d
ILT
14075
14076/* Build a VA_ARG_EXPR for the C parser. */
14077
14078tree
f187980b 14079c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 14080{
ec3fba51
MP
14081 if (error_operand_p (type))
14082 return error_mark_node;
f187980b
EB
14083 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14084 order because it takes the address of the expression. */
14085 else if (handled_component_p (expr)
14086 && reverse_storage_order_for_component_p (expr))
14087 {
14088 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14089 return error_mark_node;
14090 }
ec3fba51 14091 else if (!COMPLETE_TYPE_P (type))
4e81b788 14092 {
f187980b 14093 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 14094 "type %qT", type);
4e81b788
MP
14095 return error_mark_node;
14096 }
ec3fba51 14097 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 14098 warning_at (loc2, OPT_Wc___compat,
ec3fba51 14099 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 14100 return build_va_arg (loc2, expr, type);
72b5577d 14101}
acf0174b
JJ
14102
14103/* Return truthvalue of whether T1 is the same tree structure as T2.
6574d78e 14104 Return 1 if they are the same. Return false if they are different. */
acf0174b
JJ
14105
14106bool
14107c_tree_equal (tree t1, tree t2)
14108{
14109 enum tree_code code1, code2;
14110
14111 if (t1 == t2)
14112 return true;
14113 if (!t1 || !t2)
14114 return false;
14115
14116 for (code1 = TREE_CODE (t1);
14117 CONVERT_EXPR_CODE_P (code1)
14118 || code1 == NON_LVALUE_EXPR;
14119 code1 = TREE_CODE (t1))
14120 t1 = TREE_OPERAND (t1, 0);
14121 for (code2 = TREE_CODE (t2);
14122 CONVERT_EXPR_CODE_P (code2)
14123 || code2 == NON_LVALUE_EXPR;
14124 code2 = TREE_CODE (t2))
14125 t2 = TREE_OPERAND (t2, 0);
14126
14127 /* They might have become equal now. */
14128 if (t1 == t2)
14129 return true;
14130
14131 if (code1 != code2)
14132 return false;
14133
14134 switch (code1)
14135 {
14136 case INTEGER_CST:
8e6cdc90 14137 return wi::to_wide (t1) == wi::to_wide (t2);
acf0174b
JJ
14138
14139 case REAL_CST:
624d31fe 14140 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
14141
14142 case STRING_CST:
14143 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14144 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14145 TREE_STRING_LENGTH (t1));
14146
14147 case FIXED_CST:
14148 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14149 TREE_FIXED_CST (t2));
14150
14151 case COMPLEX_CST:
14152 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14153 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14154
14155 case VECTOR_CST:
14156 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14157
14158 case CONSTRUCTOR:
14159 /* We need to do this when determining whether or not two
14160 non-type pointer to member function template arguments
14161 are the same. */
14162 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14163 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14164 return false;
14165 {
14166 tree field, value;
14167 unsigned int i;
14168 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14169 {
14170 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14171 if (!c_tree_equal (field, elt2->index)
14172 || !c_tree_equal (value, elt2->value))
14173 return false;
14174 }
14175 }
14176 return true;
14177
14178 case TREE_LIST:
14179 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14180 return false;
14181 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14182 return false;
14183 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14184
14185 case SAVE_EXPR:
14186 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14187
14188 case CALL_EXPR:
14189 {
14190 tree arg1, arg2;
14191 call_expr_arg_iterator iter1, iter2;
14192 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14193 return false;
14194 for (arg1 = first_call_expr_arg (t1, &iter1),
14195 arg2 = first_call_expr_arg (t2, &iter2);
14196 arg1 && arg2;
14197 arg1 = next_call_expr_arg (&iter1),
14198 arg2 = next_call_expr_arg (&iter2))
14199 if (!c_tree_equal (arg1, arg2))
14200 return false;
14201 if (arg1 || arg2)
14202 return false;
14203 return true;
14204 }
14205
14206 case TARGET_EXPR:
14207 {
14208 tree o1 = TREE_OPERAND (t1, 0);
14209 tree o2 = TREE_OPERAND (t2, 0);
14210
14211 /* Special case: if either target is an unallocated VAR_DECL,
14212 it means that it's going to be unified with whatever the
14213 TARGET_EXPR is really supposed to initialize, so treat it
14214 as being equivalent to anything. */
0ae9bd27 14215 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
14216 && !DECL_RTL_SET_P (o1))
14217 /*Nop*/;
0ae9bd27 14218 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
14219 && !DECL_RTL_SET_P (o2))
14220 /*Nop*/;
14221 else if (!c_tree_equal (o1, o2))
14222 return false;
14223
14224 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14225 }
14226
14227 case COMPONENT_REF:
14228 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14229 return false;
14230 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14231
14232 case PARM_DECL:
14233 case VAR_DECL:
14234 case CONST_DECL:
14235 case FIELD_DECL:
14236 case FUNCTION_DECL:
14237 case IDENTIFIER_NODE:
14238 case SSA_NAME:
14239 return false;
14240
14241 case TREE_VEC:
14242 {
14243 unsigned ix;
14244 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14245 return false;
14246 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14247 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14248 TREE_VEC_ELT (t2, ix)))
14249 return false;
14250 return true;
14251 }
14252
14253 default:
14254 break;
14255 }
14256
14257 switch (TREE_CODE_CLASS (code1))
14258 {
14259 case tcc_unary:
14260 case tcc_binary:
14261 case tcc_comparison:
14262 case tcc_expression:
14263 case tcc_vl_exp:
14264 case tcc_reference:
14265 case tcc_statement:
14266 {
14267 int i, n = TREE_OPERAND_LENGTH (t1);
14268
14269 switch (code1)
14270 {
14271 case PREINCREMENT_EXPR:
14272 case PREDECREMENT_EXPR:
14273 case POSTINCREMENT_EXPR:
14274 case POSTDECREMENT_EXPR:
14275 n = 1;
14276 break;
14277 case ARRAY_REF:
14278 n = 2;
14279 break;
14280 default:
14281 break;
14282 }
14283
14284 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14285 && n != TREE_OPERAND_LENGTH (t2))
14286 return false;
14287
14288 for (i = 0; i < n; ++i)
14289 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14290 return false;
14291
14292 return true;
14293 }
14294
14295 case tcc_type:
14296 return comptypes (t1, t2);
14297 default:
14298 gcc_unreachable ();
14299 }
14300 /* We can get here with --disable-checking. */
14301 return false;
14302}
12893402 14303
1807ffc1
MS
14304/* Returns true when the function declaration FNDECL is implicit,
14305 introduced as a result of a call to an otherwise undeclared
14306 function, and false otherwise. */
14307
14308bool
14309c_decl_implicit (const_tree fndecl)
14310{
14311 return C_DECL_IMPLICIT (fndecl);
14312}