]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
i386.c (ix86_expand_sse2_abs): Rename function arguments.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
d1e082c2 2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
e57e265b 31#include "langhooks.h"
400fbf9f 32#include "c-tree.h"
29cc57cf 33#include "c-lang.h"
400fbf9f 34#include "flags.h"
ab87f8c8 35#include "intl.h"
672a6f42 36#include "target.h"
325c3691 37#include "tree-iterator.h"
6662d794
MLI
38#include "bitmap.h"
39#include "gimple.h"
acf0174b 40#include "tree-inline.h"
0645c1a2 41#include "omp-low.h"
61d3ce20 42#include "c-family/c-objc.h"
a212e43f 43#include "c-family/c-common.h"
de5a5fa1 44#include "c-family/c-ubsan.h"
325c3691 45
2ac2f164
JM
46/* Possible cases of implicit bad conversions. Used to select
47 diagnostic messages in convert_for_assignment. */
48enum impl_conv {
49 ic_argpass,
50 ic_assign,
51 ic_init,
52 ic_return
53};
54
bc4b653b
JM
55/* The level of nesting inside "__alignof__". */
56int in_alignof;
57
58/* The level of nesting inside "sizeof". */
59int in_sizeof;
60
61/* The level of nesting inside "typeof". */
62int in_typeof;
400fbf9f 63
1a4049e7
JJ
64/* The argument of last parsed sizeof expression, only to be tested
65 if expr.original_code == SIZEOF_EXPR. */
66tree c_last_sizeof_arg;
67
b71c7f8a 68/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 69 message within this initializer. */
b71c7f8a 70static int missing_braces_mentioned;
103b7b17 71
bf730f15
RS
72static int require_constant_value;
73static int require_constant_elements;
74
58f9752a 75static bool null_pointer_constant_p (const_tree);
f55ade6e 76static tree qualify_type (tree, tree);
dc5027f4
JM
77static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
78 bool *);
744aa42f 79static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
80static int function_types_compatible_p (const_tree, const_tree, bool *,
81 bool *);
82static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 83static tree lookup_field (tree, tree);
9771b263
DN
84static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
85 tree, tree);
db3927fb 86static tree pointer_diff (location_t, tree, tree);
744aa42f
ILT
87static tree convert_for_assignment (location_t, tree, tree, tree,
88 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
89static tree valid_compound_expr_initializer (tree, tree);
90static void push_string (const char *);
91static void push_member_name (tree);
f55ade6e
AJ
92static int spelling_length (void);
93static char *print_spelling (char *);
683d6ff9 94static void warning_init (int, const char *);
c2255bc4 95static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
a1e3b3d9
LB
96static void output_init_element (tree, tree, bool, tree, tree, int, bool,
97 struct obstack *);
98static void output_pending_init_elements (int, struct obstack *);
99static int set_designator (int, struct obstack *);
100static void push_range_stack (tree, struct obstack *);
101static void add_pending_init (tree, tree, tree, bool, struct obstack *);
102static void set_nonincremental_init (struct obstack *);
103static void set_nonincremental_init_from_string (tree, struct obstack *);
104static tree find_init_member (tree, struct obstack *);
f37acdf9 105static void readonly_warning (tree, enum lvalue_use);
7bd11157 106static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 107static void record_maybe_used_decl (tree);
dc5027f4 108static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
109\f
110/* Return true if EXP is a null pointer constant, false otherwise. */
111
112static bool
58f9752a 113null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
114{
115 /* This should really operate on c_expr structures, but they aren't
116 yet available everywhere required. */
117 tree type = TREE_TYPE (expr);
118 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 119 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
120 && integer_zerop (expr)
121 && (INTEGRAL_TYPE_P (type)
122 || (TREE_CODE (type) == POINTER_TYPE
123 && VOID_TYPE_P (TREE_TYPE (type))
124 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
125}
928c19bb
JM
126
127/* EXPR may appear in an unevaluated part of an integer constant
128 expression, but not in an evaluated part. Wrap it in a
129 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
130 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
131
132static tree
133note_integer_operands (tree expr)
134{
135 tree ret;
136 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
137 {
138 ret = copy_node (expr);
139 TREE_OVERFLOW (ret) = 1;
140 }
141 else
142 {
143 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
144 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
145 }
146 return ret;
147}
148
4d84fe7c
JM
149/* Having checked whether EXPR may appear in an unevaluated part of an
150 integer constant expression and found that it may, remove any
151 C_MAYBE_CONST_EXPR noting this fact and return the resulting
152 expression. */
153
154static inline tree
155remove_c_maybe_const_expr (tree expr)
156{
157 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
158 return C_MAYBE_CONST_EXPR_EXPR (expr);
159 else
160 return expr;
161}
162
f13c9b2c
AP
163\f/* This is a cache to hold if two types are compatible or not. */
164
165struct tagged_tu_seen_cache {
166 const struct tagged_tu_seen_cache * next;
58f9752a
KG
167 const_tree t1;
168 const_tree t2;
f13c9b2c
AP
169 /* The return value of tagged_types_tu_compatible_p if we had seen
170 these two types already. */
171 int val;
172};
173
174static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
175static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
176
400fbf9f
JW
177/* Do `exp = require_complete_type (exp);' to make sure exp
178 does not have an incomplete type. (That includes void types.) */
179
180tree
2f6e4e97 181require_complete_type (tree value)
400fbf9f
JW
182{
183 tree type = TREE_TYPE (value);
184
c3d5c3fa 185 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
186 return error_mark_node;
187
400fbf9f 188 /* First, detect a valid value with a complete type. */
d0f062fb 189 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
190 return value;
191
7a228918 192 c_incomplete_type_error (value, type);
400fbf9f
JW
193 return error_mark_node;
194}
195
196/* Print an error message for invalid use of an incomplete type.
197 VALUE is the expression that was used (or 0 if that isn't known)
198 and TYPE is the type that was invalid. */
199
200void
ac7d7749 201c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 202{
5d5993dd 203 const char *type_code_string;
400fbf9f
JW
204
205 /* Avoid duplicate error message. */
206 if (TREE_CODE (type) == ERROR_MARK)
207 return;
208
209 if (value != 0 && (TREE_CODE (value) == VAR_DECL
210 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 211 error ("%qD has an incomplete type", value);
400fbf9f
JW
212 else
213 {
214 retry:
215 /* We must print an error message. Be clever about what it says. */
216
217 switch (TREE_CODE (type))
218 {
219 case RECORD_TYPE:
ab87f8c8 220 type_code_string = "struct";
400fbf9f
JW
221 break;
222
223 case UNION_TYPE:
ab87f8c8 224 type_code_string = "union";
400fbf9f
JW
225 break;
226
227 case ENUMERAL_TYPE:
ab87f8c8 228 type_code_string = "enum";
400fbf9f
JW
229 break;
230
231 case VOID_TYPE:
232 error ("invalid use of void expression");
233 return;
234
235 case ARRAY_TYPE:
236 if (TYPE_DOMAIN (type))
237 {
fba78abb
RH
238 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
239 {
240 error ("invalid use of flexible array member");
241 return;
242 }
400fbf9f
JW
243 type = TREE_TYPE (type);
244 goto retry;
245 }
246 error ("invalid use of array with unspecified bounds");
247 return;
248
249 default:
366de0ce 250 gcc_unreachable ();
400fbf9f
JW
251 }
252
253 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
254 error ("invalid use of undefined type %<%s %E%>",
255 type_code_string, TYPE_NAME (type));
400fbf9f
JW
256 else
257 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 258 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
259 }
260}
261
ab393bf1
NB
262/* Given a type, apply default promotions wrt unnamed function
263 arguments and return the new type. */
264
265tree
2f6e4e97 266c_type_promotes_to (tree type)
ab393bf1
NB
267{
268 if (TYPE_MAIN_VARIANT (type) == float_type_node)
269 return double_type_node;
270
271 if (c_promoting_integer_type_p (type))
272 {
273 /* Preserve unsignedness if not really getting any wider. */
8df83eae 274 if (TYPE_UNSIGNED (type)
c22cacf3
MS
275 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
276 return unsigned_type_node;
ab393bf1
NB
277 return integer_type_node;
278 }
279
280 return type;
281}
282
36c5e70a
BE
283/* Return true if between two named address spaces, whether there is a superset
284 named address space that encompasses both address spaces. If there is a
285 superset, return which address space is the superset. */
286
287static bool
288addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
289{
290 if (as1 == as2)
291 {
292 *common = as1;
293 return true;
294 }
295 else if (targetm.addr_space.subset_p (as1, as2))
296 {
297 *common = as2;
298 return true;
299 }
300 else if (targetm.addr_space.subset_p (as2, as1))
301 {
302 *common = as1;
303 return true;
304 }
305 else
306 return false;
307}
308
400fbf9f
JW
309/* Return a variant of TYPE which has all the type qualifiers of LIKE
310 as well as those of TYPE. */
311
312static tree
2f6e4e97 313qualify_type (tree type, tree like)
400fbf9f 314{
36c5e70a
BE
315 addr_space_t as_type = TYPE_ADDR_SPACE (type);
316 addr_space_t as_like = TYPE_ADDR_SPACE (like);
317 addr_space_t as_common;
318
319 /* If the two named address spaces are different, determine the common
320 superset address space. If there isn't one, raise an error. */
321 if (!addr_space_superset (as_type, as_like, &as_common))
322 {
323 as_common = as_type;
324 error ("%qT and %qT are in disjoint named address spaces",
325 type, like);
326 }
327
2f6e4e97 328 return c_build_qualified_type (type,
36c5e70a
BE
329 TYPE_QUALS_NO_ADDR_SPACE (type)
330 | TYPE_QUALS_NO_ADDR_SPACE (like)
331 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 332}
52ffd86e
MS
333
334/* Return true iff the given tree T is a variable length array. */
335
336bool
ac7d7749 337c_vla_type_p (const_tree t)
52ffd86e
MS
338{
339 if (TREE_CODE (t) == ARRAY_TYPE
340 && C_TYPE_VARIABLE_SIZE (t))
341 return true;
342 return false;
343}
400fbf9f 344\f
10bc1b1b 345/* Return the composite type of two compatible types.
5305f6d7 346
10bc1b1b
JM
347 We assume that comptypes has already been done and returned
348 nonzero; if that isn't so, this may crash. In particular, we
349 assume that qualifiers match. */
400fbf9f
JW
350
351tree
10bc1b1b 352composite_type (tree t1, tree t2)
400fbf9f 353{
b3694847
SS
354 enum tree_code code1;
355 enum tree_code code2;
4b027d16 356 tree attributes;
400fbf9f
JW
357
358 /* Save time if the two types are the same. */
359
360 if (t1 == t2) return t1;
361
362 /* If one type is nonsense, use the other. */
363 if (t1 == error_mark_node)
364 return t2;
365 if (t2 == error_mark_node)
366 return t1;
367
10bc1b1b
JM
368 code1 = TREE_CODE (t1);
369 code2 = TREE_CODE (t2);
370
d9525bec 371 /* Merge the attributes. */
5fd9b178 372 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 373
10bc1b1b
JM
374 /* If one is an enumerated type and the other is the compatible
375 integer type, the composite type might be either of the two
376 (DR#013 question 3). For consistency, use the enumerated type as
377 the composite type. */
400fbf9f 378
10bc1b1b
JM
379 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
380 return t1;
381 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
382 return t2;
75326e8c 383
366de0ce 384 gcc_assert (code1 == code2);
b6a10c9f 385
400fbf9f
JW
386 switch (code1)
387 {
400fbf9f 388 case POINTER_TYPE:
10bc1b1b 389 /* For two pointers, do this recursively on the target type. */
400fbf9f 390 {
3932261a
MM
391 tree pointed_to_1 = TREE_TYPE (t1);
392 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 393 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 394 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
395 t1 = build_type_attribute_variant (t1, attributes);
396 return qualify_type (t1, t2);
400fbf9f 397 }
400fbf9f
JW
398
399 case ARRAY_TYPE:
400 {
10bc1b1b 401 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
402 int quals;
403 tree unqual_elt;
ca8bdb78
JM
404 tree d1 = TYPE_DOMAIN (t1);
405 tree d2 = TYPE_DOMAIN (t2);
406 bool d1_variable, d2_variable;
407 bool d1_zero, d2_zero;
f6294de7 408 bool t1_complete, t2_complete;
46df2823 409
de46b2fe 410 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
411 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
412 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 413
f6294de7
JM
414 t1_complete = COMPLETE_TYPE_P (t1);
415 t2_complete = COMPLETE_TYPE_P (t2);
416
ca8bdb78
JM
417 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
418 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
419
420 d1_variable = (!d1_zero
421 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
422 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
423 d2_variable = (!d2_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
426 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
427 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 428
400fbf9f 429 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
430 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
431 && (d2_variable || d2_zero || !d1_variable))
4b027d16 432 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
433 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
434 && (d1_variable || d1_zero || !d2_variable))
4b027d16 435 return build_type_attribute_variant (t2, attributes);
c22cacf3 436
de46b2fe
AP
437 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
438 return build_type_attribute_variant (t1, attributes);
439 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
440 return build_type_attribute_variant (t2, attributes);
c22cacf3 441
46df2823
JM
442 /* Merge the element types, and have a size if either arg has
443 one. We may have qualifiers on the element types. To set
444 up TYPE_MAIN_VARIANT correctly, we need to form the
445 composite of the unqualified types and add the qualifiers
446 back at the end. */
447 quals = TYPE_QUALS (strip_array_types (elt));
448 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
449 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
450 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
451 && (d2_variable
452 || d2_zero
453 || !d1_variable))
454 ? t1
455 : t2));
f6294de7
JM
456 /* Ensure a composite type involving a zero-length array type
457 is a zero-length type not an incomplete type. */
458 if (d1_zero && d2_zero
459 && (t1_complete || t2_complete)
460 && !COMPLETE_TYPE_P (t1))
461 {
462 TYPE_SIZE (t1) = bitsize_zero_node;
463 TYPE_SIZE_UNIT (t1) = size_zero_node;
464 }
46df2823 465 t1 = c_build_qualified_type (t1, quals);
de46b2fe 466 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
467 }
468
fcb99e7b
JJ
469 case ENUMERAL_TYPE:
470 case RECORD_TYPE:
471 case UNION_TYPE:
472 if (attributes != NULL)
473 {
474 /* Try harder not to create a new aggregate type. */
475 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
476 return t1;
477 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
478 return t2;
479 }
480 return build_type_attribute_variant (t1, attributes);
481
400fbf9f
JW
482 case FUNCTION_TYPE:
483 /* Function types: prefer the one that specified arg types.
484 If both do, merge the arg types. Also merge the return types. */
485 {
10bc1b1b 486 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
487 tree p1 = TYPE_ARG_TYPES (t1);
488 tree p2 = TYPE_ARG_TYPES (t2);
489 int len;
490 tree newargs, n;
491 int i;
492
493 /* Save space: see if the result is identical to one of the args. */
3f75a254 494 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 495 return build_type_attribute_variant (t1, attributes);
3f75a254 496 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 497 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
498
499 /* Simple way if one arg fails to specify argument types. */
500 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 501 {
fe7080d2
AP
502 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
503 t1 = build_type_attribute_variant (t1, attributes);
504 return qualify_type (t1, t2);
4b027d16 505 }
400fbf9f 506 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
507 {
508 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
509 t1 = build_type_attribute_variant (t1, attributes);
510 return qualify_type (t1, t2);
4b027d16 511 }
400fbf9f
JW
512
513 /* If both args specify argument types, we must merge the two
514 lists, argument by argument. */
2f4e8f2b 515
400fbf9f
JW
516 len = list_length (p1);
517 newargs = 0;
518
519 for (i = 0; i < len; i++)
8d9bfdc5 520 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
521
522 n = newargs;
523
524 for (; p1;
525 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
526 {
527 /* A null type means arg type is not specified.
528 Take whatever the other function type has. */
529 if (TREE_VALUE (p1) == 0)
530 {
531 TREE_VALUE (n) = TREE_VALUE (p2);
532 goto parm_done;
533 }
534 if (TREE_VALUE (p2) == 0)
535 {
536 TREE_VALUE (n) = TREE_VALUE (p1);
537 goto parm_done;
538 }
2f6e4e97 539
400fbf9f
JW
540 /* Given wait (union {union wait *u; int *i} *)
541 and wait (union wait *),
542 prefer union wait * as type of parm. */
543 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
544 && TREE_VALUE (p1) != TREE_VALUE (p2))
545 {
546 tree memb;
58cb41e6
JJ
547 tree mv2 = TREE_VALUE (p2);
548 if (mv2 && mv2 != error_mark_node
549 && TREE_CODE (mv2) != ARRAY_TYPE)
550 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 551 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 552 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
553 {
554 tree mv3 = TREE_TYPE (memb);
555 if (mv3 && mv3 != error_mark_node
556 && TREE_CODE (mv3) != ARRAY_TYPE)
557 mv3 = TYPE_MAIN_VARIANT (mv3);
558 if (comptypes (mv3, mv2))
559 {
560 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
561 TREE_VALUE (p2));
c1771a20 562 pedwarn (input_location, OPT_Wpedantic,
fcf73884 563 "function types not truly compatible in ISO C");
58cb41e6
JJ
564 goto parm_done;
565 }
566 }
400fbf9f
JW
567 }
568 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
569 && TREE_VALUE (p2) != TREE_VALUE (p1))
570 {
571 tree memb;
58cb41e6
JJ
572 tree mv1 = TREE_VALUE (p1);
573 if (mv1 && mv1 != error_mark_node
574 && TREE_CODE (mv1) != ARRAY_TYPE)
575 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 576 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 577 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
578 {
579 tree mv3 = TREE_TYPE (memb);
580 if (mv3 && mv3 != error_mark_node
581 && TREE_CODE (mv3) != ARRAY_TYPE)
582 mv3 = TYPE_MAIN_VARIANT (mv3);
583 if (comptypes (mv3, mv1))
584 {
585 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
586 TREE_VALUE (p1));
c1771a20 587 pedwarn (input_location, OPT_Wpedantic,
fcf73884 588 "function types not truly compatible in ISO C");
58cb41e6
JJ
589 goto parm_done;
590 }
591 }
400fbf9f 592 }
10bc1b1b 593 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
594 parm_done: ;
595 }
596
4b027d16 597 t1 = build_function_type (valtype, newargs);
fe7080d2 598 t1 = qualify_type (t1, t2);
0f41302f 599 /* ... falls through ... */
400fbf9f
JW
600 }
601
602 default:
4b027d16 603 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
604 }
605
606}
10bc1b1b
JM
607
608/* Return the type of a conditional expression between pointers to
609 possibly differently qualified versions of compatible types.
610
611 We assume that comp_target_types has already been done and returned
612 nonzero; if that isn't so, this may crash. */
613
614static tree
615common_pointer_type (tree t1, tree t2)
616{
617 tree attributes;
46df2823
JM
618 tree pointed_to_1, mv1;
619 tree pointed_to_2, mv2;
10bc1b1b 620 tree target;
eb1387a0 621 unsigned target_quals;
36c5e70a
BE
622 addr_space_t as1, as2, as_common;
623 int quals1, quals2;
10bc1b1b
JM
624
625 /* Save time if the two types are the same. */
626
627 if (t1 == t2) return t1;
628
629 /* If one type is nonsense, use the other. */
630 if (t1 == error_mark_node)
631 return t2;
632 if (t2 == error_mark_node)
633 return t1;
634
366de0ce 635 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 636 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
637
638 /* Merge the attributes. */
639 attributes = targetm.merge_type_attributes (t1, t2);
640
641 /* Find the composite type of the target types, and combine the
46df2823
JM
642 qualifiers of the two types' targets. Do not lose qualifiers on
643 array element types by taking the TYPE_MAIN_VARIANT. */
644 mv1 = pointed_to_1 = TREE_TYPE (t1);
645 mv2 = pointed_to_2 = TREE_TYPE (t2);
646 if (TREE_CODE (mv1) != ARRAY_TYPE)
647 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
648 if (TREE_CODE (mv2) != ARRAY_TYPE)
649 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
650 target = composite_type (mv1, mv2);
eb1387a0
RG
651
652 /* For function types do not merge const qualifiers, but drop them
653 if used inconsistently. The middle-end uses these to mark const
654 and noreturn functions. */
36c5e70a
BE
655 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
656 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
657
eb1387a0 658 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 659 target_quals = (quals1 & quals2);
eb1387a0 660 else
36c5e70a
BE
661 target_quals = (quals1 | quals2);
662
663 /* If the two named address spaces are different, determine the common
664 superset address space. This is guaranteed to exist due to the
665 assumption that comp_target_type returned non-zero. */
666 as1 = TYPE_ADDR_SPACE (pointed_to_1);
667 as2 = TYPE_ADDR_SPACE (pointed_to_2);
668 if (!addr_space_superset (as1, as2, &as_common))
669 gcc_unreachable ();
670
671 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
672
eb1387a0 673 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
674 return build_type_attribute_variant (t1, attributes);
675}
676
677/* Return the common type for two arithmetic types under the usual
678 arithmetic conversions. The default conversions have already been
679 applied, and enumerated types converted to their compatible integer
680 types. The resulting type is unqualified and has no attributes.
681
682 This is the type for the result of most arithmetic operations
683 if the operands have the given two types. */
684
ccf7f880
JJ
685static tree
686c_common_type (tree t1, tree t2)
10bc1b1b
JM
687{
688 enum tree_code code1;
689 enum tree_code code2;
690
691 /* If one type is nonsense, use the other. */
692 if (t1 == error_mark_node)
693 return t2;
694 if (t2 == error_mark_node)
695 return t1;
696
697 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
698 t1 = TYPE_MAIN_VARIANT (t1);
699
700 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
701 t2 = TYPE_MAIN_VARIANT (t2);
702
703 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
704 t1 = build_type_attribute_variant (t1, NULL_TREE);
705
706 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
707 t2 = build_type_attribute_variant (t2, NULL_TREE);
708
709 /* Save time if the two types are the same. */
710
711 if (t1 == t2) return t1;
712
713 code1 = TREE_CODE (t1);
714 code2 = TREE_CODE (t2);
715
366de0ce 716 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
717 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
718 || code1 == INTEGER_TYPE);
366de0ce 719 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
720 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
721 || code2 == INTEGER_TYPE);
10bc1b1b 722
5fc89bfd
JJ
723 /* When one operand is a decimal float type, the other operand cannot be
724 a generic float type or a complex type. We also disallow vector types
725 here. */
726 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
727 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
728 {
729 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
730 {
731 error ("can%'t mix operands of decimal float and vector types");
732 return error_mark_node;
733 }
734 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
735 {
736 error ("can%'t mix operands of decimal float and complex types");
737 return error_mark_node;
738 }
739 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
740 {
741 error ("can%'t mix operands of decimal float and other float types");
742 return error_mark_node;
743 }
744 }
745
10bc1b1b
JM
746 /* If one type is a vector type, return that type. (How the usual
747 arithmetic conversions apply to the vector types extension is not
748 precisely specified.) */
749 if (code1 == VECTOR_TYPE)
750 return t1;
751
752 if (code2 == VECTOR_TYPE)
753 return t2;
754
755 /* If one type is complex, form the common type of the non-complex
756 components, then make that complex. Use T1 or T2 if it is the
757 required type. */
758 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
759 {
760 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
761 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 762 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
763
764 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
765 return t1;
766 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
767 return t2;
768 else
769 return build_complex_type (subtype);
770 }
771
772 /* If only one is real, use it as the result. */
773
774 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
775 return t1;
776
777 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
778 return t2;
779
9a8ce21f
JG
780 /* If both are real and either are decimal floating point types, use
781 the decimal floating point type with the greater precision. */
782
783 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
784 {
785 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
786 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
787 return dfloat128_type_node;
788 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
790 return dfloat64_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
793 return dfloat32_type_node;
794 }
795
ab22c1fa
CF
796 /* Deal with fixed-point types. */
797 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
798 {
799 unsigned int unsignedp = 0, satp = 0;
800 enum machine_mode m1, m2;
801 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
802
803 m1 = TYPE_MODE (t1);
804 m2 = TYPE_MODE (t2);
805
806 /* If one input type is saturating, the result type is saturating. */
807 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
808 satp = 1;
809
810 /* If both fixed-point types are unsigned, the result type is unsigned.
811 When mixing fixed-point and integer types, follow the sign of the
812 fixed-point type.
813 Otherwise, the result type is signed. */
814 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
815 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
816 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
817 && TYPE_UNSIGNED (t1))
818 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
819 && TYPE_UNSIGNED (t2)))
820 unsignedp = 1;
821
822 /* The result type is signed. */
823 if (unsignedp == 0)
824 {
825 /* If the input type is unsigned, we need to convert to the
826 signed type. */
827 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
828 {
d75d71e0 829 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
830 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
831 mclass = MODE_FRACT;
832 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
833 mclass = MODE_ACCUM;
834 else
835 gcc_unreachable ();
836 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
837 }
838 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
839 {
d75d71e0 840 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
841 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
848 }
849 }
850
851 if (code1 == FIXED_POINT_TYPE)
852 {
853 fbit1 = GET_MODE_FBIT (m1);
854 ibit1 = GET_MODE_IBIT (m1);
855 }
856 else
857 {
858 fbit1 = 0;
859 /* Signed integers need to subtract one sign bit. */
860 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
861 }
862
863 if (code2 == FIXED_POINT_TYPE)
864 {
865 fbit2 = GET_MODE_FBIT (m2);
866 ibit2 = GET_MODE_IBIT (m2);
867 }
868 else
869 {
870 fbit2 = 0;
871 /* Signed integers need to subtract one sign bit. */
872 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
873 }
874
875 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
876 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
877 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
878 satp);
879 }
880
10bc1b1b
JM
881 /* Both real or both integers; use the one with greater precision. */
882
883 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
884 return t1;
885 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
886 return t2;
887
888 /* Same precision. Prefer long longs to longs to ints when the
889 same precision, following the C99 rules on integer type rank
890 (which are equivalent to the C90 rules for C90 types). */
891
892 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
893 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
894 return long_long_unsigned_type_node;
895
896 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
897 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
898 {
899 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
900 return long_long_unsigned_type_node;
901 else
c22cacf3 902 return long_long_integer_type_node;
10bc1b1b
JM
903 }
904
905 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
907 return long_unsigned_type_node;
908
909 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
911 {
912 /* But preserve unsignedness from the other type,
913 since long cannot hold all the values of an unsigned int. */
914 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
915 return long_unsigned_type_node;
916 else
917 return long_integer_type_node;
918 }
919
920 /* Likewise, prefer long double to double even if same size. */
921 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
923 return long_double_type_node;
924
2531a1d9
JR
925 /* Likewise, prefer double to float even if same size.
926 We got a couple of embedded targets with 32 bit doubles, and the
927 pdp11 might have 64 bit floats. */
928 if (TYPE_MAIN_VARIANT (t1) == double_type_node
929 || TYPE_MAIN_VARIANT (t2) == double_type_node)
930 return double_type_node;
931
10bc1b1b
JM
932 /* Otherwise prefer the unsigned one. */
933
934 if (TYPE_UNSIGNED (t1))
935 return t1;
936 else
937 return t2;
938}
400fbf9f 939\f
5922c215
JM
940/* Wrapper around c_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
942 are allowed here and are converted to their compatible integer types.
943 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
945tree
946common_type (tree t1, tree t2)
947{
948 if (TREE_CODE (t1) == ENUMERAL_TYPE)
949 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950 if (TREE_CODE (t2) == ENUMERAL_TYPE)
951 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
952
953 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
954 if (TREE_CODE (t1) == BOOLEAN_TYPE
955 && TREE_CODE (t2) == BOOLEAN_TYPE)
956 return boolean_type_node;
957
958 /* If either type is BOOLEAN_TYPE, then return the other. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE)
960 return t2;
961 if (TREE_CODE (t2) == BOOLEAN_TYPE)
962 return t1;
963
ccf7f880
JJ
964 return c_common_type (t1, t2);
965}
f13c9b2c 966
400fbf9f
JW
967/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968 or various other operations. Return 2 if they are compatible
969 but a warning may be needed if you use them together. */
970
971int
132da1a5 972comptypes (tree type1, tree type2)
f13c9b2c
AP
973{
974 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
975 int val;
976
dc5027f4 977 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
978 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
979
980 return val;
981}
982
983/* Like comptypes, but if it returns non-zero because enum and int are
984 compatible, it sets *ENUM_AND_INT_P to true. */
985
986static int
987comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
988{
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990 int val;
991
dc5027f4
JM
992 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994
995 return val;
996}
997
998/* Like comptypes, but if it returns nonzero for different types, it
999 sets *DIFFERENT_TYPES_P to true. */
1000
1001int
1002comptypes_check_different_types (tree type1, tree type2,
1003 bool *different_types_p)
1004{
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1007
1008 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1010
f13c9b2c 1011 return val;
c22cacf3
MS
1012}
1013\f
f13c9b2c
AP
1014/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1016 but a warning may be needed if you use them together. If
1017 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1019 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1020 NULL, and the types are compatible but different enough not to be
48b0b196 1021 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1022 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023 false, but may or may not be set if the types are incompatible.
1024 This differs from comptypes, in that we don't free the seen
1025 types. */
f13c9b2c
AP
1026
1027static int
dc5027f4
JM
1028comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029 bool *different_types_p)
400fbf9f 1030{
58f9752a
KG
1031 const_tree t1 = type1;
1032 const_tree t2 = type2;
4b027d16 1033 int attrval, val;
400fbf9f
JW
1034
1035 /* Suppress errors caused by previously reported errors. */
1036
8d47dfc5
RH
1037 if (t1 == t2 || !t1 || !t2
1038 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1039 return 1;
1040
bca63328
JM
1041 /* Enumerated types are compatible with integer types, but this is
1042 not transitive: two enumerated types in the same translation unit
1043 are compatible with each other only if they are the same type. */
400fbf9f 1044
bca63328 1045 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1046 {
1047 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1048 if (TREE_CODE (t2) != VOID_TYPE)
1049 {
1050 if (enum_and_int_p != NULL)
1051 *enum_and_int_p = true;
1052 if (different_types_p != NULL)
1053 *different_types_p = true;
1054 }
744aa42f 1055 }
bca63328 1056 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1057 {
1058 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1059 if (TREE_CODE (t1) != VOID_TYPE)
1060 {
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1065 }
744aa42f 1066 }
400fbf9f
JW
1067
1068 if (t1 == t2)
1069 return 1;
1070
1071 /* Different classes of types can't be compatible. */
1072
3aeb3655
EC
1073 if (TREE_CODE (t1) != TREE_CODE (t2))
1074 return 0;
400fbf9f 1075
118a3a8b 1076 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1077
3932261a 1078 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1079 return 0;
1080
08632da2
RS
1081 /* Allow for two different type nodes which have essentially the same
1082 definition. Note that we already checked for equality of the type
38e01259 1083 qualifiers (just above). */
400fbf9f 1084
46df2823
JM
1085 if (TREE_CODE (t1) != ARRAY_TYPE
1086 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1087 return 1;
1088
4b027d16 1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1090 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1091 return 0;
1092
1093 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1094 val = 0;
1095
400fbf9f
JW
1096 switch (TREE_CODE (t1))
1097 {
1098 case POINTER_TYPE:
106f5de5
UW
1099 /* Do not remove mode or aliasing information. */
1100 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1101 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1102 break;
4b027d16 1103 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1104 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1105 enum_and_int_p, different_types_p));
4b027d16 1106 break;
400fbf9f
JW
1107
1108 case FUNCTION_TYPE:
dc5027f4
JM
1109 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1110 different_types_p);
4b027d16 1111 break;
400fbf9f
JW
1112
1113 case ARRAY_TYPE:
1114 {
400fbf9f
JW
1115 tree d1 = TYPE_DOMAIN (t1);
1116 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1117 bool d1_variable, d2_variable;
1118 bool d1_zero, d2_zero;
4b027d16 1119 val = 1;
400fbf9f
JW
1120
1121 /* Target types must match incl. qualifiers. */
1122 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1123 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1124 enum_and_int_p,
1125 different_types_p)))
400fbf9f
JW
1126 return 0;
1127
dc5027f4
JM
1128 if (different_types_p != NULL
1129 && (d1 == 0) != (d2 == 0))
1130 *different_types_p = true;
400fbf9f 1131 /* Sizes must match unless one is missing or variable. */
3f85558f 1132 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1133 break;
400fbf9f 1134
3f75a254
JM
1135 d1_zero = !TYPE_MAX_VALUE (d1);
1136 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1137
3f75a254 1138 d1_variable = (!d1_zero
3f85558f
RH
1139 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1140 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1141 d2_variable = (!d2_zero
3f85558f
RH
1142 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1143 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1144 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1145 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1146
dc5027f4
JM
1147 if (different_types_p != NULL
1148 && d1_variable != d2_variable)
1149 *different_types_p = true;
3f85558f
RH
1150 if (d1_variable || d2_variable)
1151 break;
1152 if (d1_zero && d2_zero)
1153 break;
1154 if (d1_zero || d2_zero
3f75a254
JM
1155 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1156 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1157 val = 0;
1158
c22cacf3 1159 break;
400fbf9f
JW
1160 }
1161
d1bd0ded 1162 case ENUMERAL_TYPE:
58393038 1163 case RECORD_TYPE:
d1bd0ded 1164 case UNION_TYPE:
766beae1 1165 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1166 {
fcb99e7b
JJ
1167 tree a1 = TYPE_ATTRIBUTES (t1);
1168 tree a2 = TYPE_ATTRIBUTES (t2);
1169
1170 if (! attribute_list_contained (a1, a2)
1171 && ! attribute_list_contained (a2, a1))
1172 break;
1173
f13c9b2c 1174 if (attrval != 2)
dc5027f4
JM
1175 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1176 different_types_p);
1177 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1178 different_types_p);
f13c9b2c 1179 }
4b027d16 1180 break;
e9a25f70 1181
62e1dfcf 1182 case VECTOR_TYPE:
744aa42f
ILT
1183 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1184 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1185 enum_and_int_p, different_types_p));
62e1dfcf
NC
1186 break;
1187
e9a25f70
JL
1188 default:
1189 break;
400fbf9f 1190 }
4b027d16 1191 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1192}
1193
36c5e70a
BE
1194/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1195 their qualifiers, except for named address spaces. If the pointers point to
1196 different named addresses, then we must determine if one address space is a
1197 subset of the other. */
400fbf9f
JW
1198
1199static int
744aa42f 1200comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1201{
392202b0 1202 int val;
36c5e70a
BE
1203 tree mvl = TREE_TYPE (ttl);
1204 tree mvr = TREE_TYPE (ttr);
1205 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1206 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1207 addr_space_t as_common;
744aa42f 1208 bool enum_and_int_p;
8b40563c 1209
36c5e70a
BE
1210 /* Fail if pointers point to incompatible address spaces. */
1211 if (!addr_space_superset (asl, asr, &as_common))
1212 return 0;
1213
46df2823
JM
1214 /* Do not lose qualifiers on element types of array types that are
1215 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823
JM
1216 if (TREE_CODE (mvl) != ARRAY_TYPE)
1217 mvl = TYPE_MAIN_VARIANT (mvl);
1218 if (TREE_CODE (mvr) != ARRAY_TYPE)
1219 mvr = TYPE_MAIN_VARIANT (mvr);
744aa42f
ILT
1220 enum_and_int_p = false;
1221 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1222
fcf73884 1223 if (val == 2)
c1771a20 1224 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1225
1226 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1227 warning_at (location, OPT_Wc___compat,
1228 "pointer target types incompatible in C++");
1229
400fbf9f
JW
1230 return val;
1231}
1232\f
1233/* Subroutines of `comptypes'. */
1234
f75fbaf7
ZW
1235/* Determine whether two trees derive from the same translation unit.
1236 If the CONTEXT chain ends in a null, that tree's context is still
1237 being parsed, so if two trees have context chains ending in null,
766beae1 1238 they're in the same translation unit. */
f75fbaf7 1239int
58f9752a 1240same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1241{
1242 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1243 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1244 {
6615c446
JO
1245 case tcc_declaration:
1246 t1 = DECL_CONTEXT (t1); break;
1247 case tcc_type:
1248 t1 = TYPE_CONTEXT (t1); break;
1249 case tcc_exceptional:
1250 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1251 default: gcc_unreachable ();
766beae1
ZW
1252 }
1253
1254 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1255 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1256 {
6615c446
JO
1257 case tcc_declaration:
1258 t2 = DECL_CONTEXT (t2); break;
1259 case tcc_type:
1260 t2 = TYPE_CONTEXT (t2); break;
1261 case tcc_exceptional:
1262 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1263 default: gcc_unreachable ();
766beae1
ZW
1264 }
1265
1266 return t1 == t2;
1267}
1268
f13c9b2c 1269/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1270
f13c9b2c 1271static struct tagged_tu_seen_cache *
58f9752a 1272alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1273{
cceb1885 1274 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1275 tu->next = tagged_tu_seen_base;
1276 tu->t1 = t1;
1277 tu->t2 = t2;
c22cacf3 1278
f13c9b2c 1279 tagged_tu_seen_base = tu;
c22cacf3 1280
f13c9b2c
AP
1281 /* The C standard says that two structures in different translation
1282 units are compatible with each other only if the types of their
1283 fields are compatible (among other things). We assume that they
1284 are compatible until proven otherwise when building the cache.
1285 An example where this can occur is:
1286 struct a
1287 {
1288 struct a *next;
1289 };
1290 If we are comparing this against a similar struct in another TU,
c83eecad 1291 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1292 loop. */
1293 tu->val = 1;
1294 return tu;
1295}
d1bd0ded 1296
f13c9b2c 1297/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1298
f13c9b2c
AP
1299static void
1300free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1301{
1302 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1303 while (tu != tu_til)
1304 {
741ac903
KG
1305 const struct tagged_tu_seen_cache *const tu1
1306 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1307 tu = tu1->next;
b1d5455a 1308 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1309 }
1310 tagged_tu_seen_base = tu_til;
1311}
d1bd0ded
GK
1312
1313/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1314 compatible. If the two types are not the same (which has been
1315 checked earlier), this can only happen when multiple translation
1316 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1317 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1318 comptypes_internal. */
d1bd0ded
GK
1319
1320static int
744aa42f 1321tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1322 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1323{
1324 tree s1, s2;
1325 bool needs_warning = false;
3aeb3655 1326
d1bd0ded
GK
1327 /* We have to verify that the tags of the types are the same. This
1328 is harder than it looks because this may be a typedef, so we have
1329 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1330 typedef...
1331 In the case of compiler-created builtin structs the TYPE_DECL
1332 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1333 while (TYPE_NAME (t1)
1334 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1335 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1336 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1337
dea984dc
ILT
1338 while (TYPE_NAME (t2)
1339 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1340 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1341 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1342
1343 /* C90 didn't have the requirement that the two tags be the same. */
1344 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1345 return 0;
3aeb3655 1346
d1bd0ded
GK
1347 /* C90 didn't say what happened if one or both of the types were
1348 incomplete; we choose to follow C99 rules here, which is that they
1349 are compatible. */
1350 if (TYPE_SIZE (t1) == NULL
1351 || TYPE_SIZE (t2) == NULL)
1352 return 1;
3aeb3655 1353
d1bd0ded 1354 {
f13c9b2c 1355 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1356 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1357 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1358 return tts_i->val;
d1bd0ded 1359 }
3aeb3655 1360
d1bd0ded
GK
1361 switch (TREE_CODE (t1))
1362 {
1363 case ENUMERAL_TYPE:
1364 {
f13c9b2c 1365 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1366 /* Speed up the case where the type values are in the same order. */
1367 tree tv1 = TYPE_VALUES (t1);
1368 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1369
c22cacf3 1370 if (tv1 == tv2)
f13c9b2c
AP
1371 {
1372 return 1;
1373 }
3aeb3655 1374
c22cacf3
MS
1375 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1376 {
1377 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1378 break;
1379 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1380 {
c22cacf3 1381 tu->val = 0;
f13c9b2c
AP
1382 return 0;
1383 }
c22cacf3 1384 }
3aeb3655 1385
c22cacf3 1386 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1387 {
1388 return 1;
1389 }
c22cacf3 1390 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1391 {
1392 tu->val = 0;
1393 return 0;
1394 }
3aeb3655 1395
d1bd0ded 1396 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1397 {
1398 tu->val = 0;
1399 return 0;
1400 }
3aeb3655 1401
d1bd0ded
GK
1402 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1403 {
1404 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1405 if (s2 == NULL
1406 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1407 {
1408 tu->val = 0;
1409 return 0;
1410 }
d1bd0ded
GK
1411 }
1412 return 1;
1413 }
1414
1415 case UNION_TYPE:
1416 {
f13c9b2c 1417 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1418 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1419 {
1420 tu->val = 0;
1421 return 0;
1422 }
c22cacf3 1423
f13c9b2c
AP
1424 /* Speed up the common case where the fields are in the same order. */
1425 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1426 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1427 {
1428 int result;
c22cacf3 1429
3ae4d3cc 1430 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1431 break;
744aa42f 1432 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1433 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1434
1435 if (result != 1 && !DECL_NAME (s1))
1436 break;
f13c9b2c
AP
1437 if (result == 0)
1438 {
1439 tu->val = 0;
1440 return 0;
1441 }
1442 if (result == 2)
1443 needs_warning = true;
1444
1445 if (TREE_CODE (s1) == FIELD_DECL
1446 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1447 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1448 {
1449 tu->val = 0;
1450 return 0;
1451 }
1452 }
1453 if (!s1 && !s2)
1454 {
1455 tu->val = needs_warning ? 2 : 1;
1456 return tu->val;
1457 }
d1bd0ded 1458
910ad8de 1459 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1460 {
1461 bool ok = false;
3aeb3655 1462
910ad8de 1463 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1464 if (DECL_NAME (s1) == DECL_NAME (s2))
1465 {
1466 int result;
1467
744aa42f 1468 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1469 enum_and_int_p,
1470 different_types_p);
3ae4d3cc
AO
1471
1472 if (result != 1 && !DECL_NAME (s1))
1473 continue;
1474 if (result == 0)
1475 {
1476 tu->val = 0;
1477 return 0;
1478 }
1479 if (result == 2)
1480 needs_warning = true;
1481
1482 if (TREE_CODE (s1) == FIELD_DECL
1483 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1484 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1485 break;
3ae4d3cc
AO
1486
1487 ok = true;
1488 break;
1489 }
3f75a254 1490 if (!ok)
f13c9b2c
AP
1491 {
1492 tu->val = 0;
1493 return 0;
1494 }
d1bd0ded 1495 }
f13c9b2c
AP
1496 tu->val = needs_warning ? 2 : 10;
1497 return tu->val;
d1bd0ded
GK
1498 }
1499
1500 case RECORD_TYPE:
1501 {
c22cacf3 1502 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1503
1504 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1505 s1 && s2;
910ad8de 1506 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1507 {
1508 int result;
1509 if (TREE_CODE (s1) != TREE_CODE (s2)
1510 || DECL_NAME (s1) != DECL_NAME (s2))
1511 break;
744aa42f 1512 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1513 enum_and_int_p, different_types_p);
d1bd0ded
GK
1514 if (result == 0)
1515 break;
1516 if (result == 2)
1517 needs_warning = true;
3aeb3655 1518
d1bd0ded
GK
1519 if (TREE_CODE (s1) == FIELD_DECL
1520 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1521 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1522 break;
1523 }
d1bd0ded 1524 if (s1 && s2)
f13c9b2c
AP
1525 tu->val = 0;
1526 else
1527 tu->val = needs_warning ? 2 : 1;
1528 return tu->val;
d1bd0ded
GK
1529 }
1530
1531 default:
366de0ce 1532 gcc_unreachable ();
d1bd0ded
GK
1533 }
1534}
1535
400fbf9f
JW
1536/* Return 1 if two function types F1 and F2 are compatible.
1537 If either type specifies no argument types,
1538 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1539 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1540 the other must specify that number of self-promoting arg types.
744aa42f 1541 Otherwise, the argument types must match.
dc5027f4 1542 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1543
1544static int
744aa42f 1545function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1546 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1547{
1548 tree args1, args2;
1549 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1550 int val = 1;
1551 int val1;
a6fdc086
GK
1552 tree ret1, ret2;
1553
1554 ret1 = TREE_TYPE (f1);
1555 ret2 = TREE_TYPE (f2);
1556
e508a019
JM
1557 /* 'volatile' qualifiers on a function's return type used to mean
1558 the function is noreturn. */
1559 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1560 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1561 if (TYPE_VOLATILE (ret1))
1562 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1563 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1564 if (TYPE_VOLATILE (ret2))
1565 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1566 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1567 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1568 if (val == 0)
400fbf9f
JW
1569 return 0;
1570
1571 args1 = TYPE_ARG_TYPES (f1);
1572 args2 = TYPE_ARG_TYPES (f2);
1573
dc5027f4
JM
1574 if (different_types_p != NULL
1575 && (args1 == 0) != (args2 == 0))
1576 *different_types_p = true;
1577
400fbf9f
JW
1578 /* An unspecified parmlist matches any specified parmlist
1579 whose argument types don't need default promotions. */
1580
1581 if (args1 == 0)
1582 {
1583 if (!self_promoting_args_p (args2))
1584 return 0;
1585 /* If one of these types comes from a non-prototype fn definition,
1586 compare that with the other type's arglist.
3176a0c2 1587 If they don't match, ask for a warning (but no error). */
400fbf9f 1588 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1589 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1590 enum_and_int_p, different_types_p))
400fbf9f
JW
1591 val = 2;
1592 return val;
1593 }
1594 if (args2 == 0)
1595 {
1596 if (!self_promoting_args_p (args1))
1597 return 0;
1598 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1599 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1600 enum_and_int_p, different_types_p))
400fbf9f
JW
1601 val = 2;
1602 return val;
1603 }
1604
1605 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1606 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1607 different_types_p);
400fbf9f
JW
1608 return val1 != 1 ? val1 : val;
1609}
1610
744aa42f
ILT
1611/* Check two lists of types for compatibility, returning 0 for
1612 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1613 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1614 comptypes_internal. */
400fbf9f
JW
1615
1616static int
744aa42f 1617type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1618 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1619{
1620 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1621 int val = 1;
9d5f3e49 1622 int newval = 0;
400fbf9f
JW
1623
1624 while (1)
1625 {
46df2823 1626 tree a1, mv1, a2, mv2;
400fbf9f
JW
1627 if (args1 == 0 && args2 == 0)
1628 return val;
1629 /* If one list is shorter than the other,
1630 they fail to match. */
1631 if (args1 == 0 || args2 == 0)
1632 return 0;
46df2823
JM
1633 mv1 = a1 = TREE_VALUE (args1);
1634 mv2 = a2 = TREE_VALUE (args2);
1635 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1636 mv1 = TYPE_MAIN_VARIANT (mv1);
1637 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1638 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f
JW
1639 /* A null pointer instead of a type
1640 means there is supposed to be an argument
1641 but nothing is specified about what type it has.
1642 So match anything that self-promotes. */
dc5027f4
JM
1643 if (different_types_p != NULL
1644 && (a1 == 0) != (a2 == 0))
1645 *different_types_p = true;
46df2823 1646 if (a1 == 0)
400fbf9f 1647 {
46df2823 1648 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1649 return 0;
1650 }
46df2823 1651 else if (a2 == 0)
400fbf9f 1652 {
46df2823 1653 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1654 return 0;
1655 }
8f5b6d29 1656 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1657 else if (TREE_CODE (a1) == ERROR_MARK
1658 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1659 ;
dc5027f4
JM
1660 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1661 different_types_p)))
400fbf9f 1662 {
dc5027f4
JM
1663 if (different_types_p != NULL)
1664 *different_types_p = true;
400fbf9f
JW
1665 /* Allow wait (union {union wait *u; int *i} *)
1666 and wait (union wait *) to be compatible. */
46df2823
JM
1667 if (TREE_CODE (a1) == UNION_TYPE
1668 && (TYPE_NAME (a1) == 0
ebf0bf7f 1669 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1670 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1671 && tree_int_cst_equal (TYPE_SIZE (a1),
1672 TYPE_SIZE (a2)))
400fbf9f
JW
1673 {
1674 tree memb;
46df2823 1675 for (memb = TYPE_FIELDS (a1);
910ad8de 1676 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1677 {
1678 tree mv3 = TREE_TYPE (memb);
1679 if (mv3 && mv3 != error_mark_node
1680 && TREE_CODE (mv3) != ARRAY_TYPE)
1681 mv3 = TYPE_MAIN_VARIANT (mv3);
dc5027f4
JM
1682 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1683 different_types_p))
58cb41e6
JJ
1684 break;
1685 }
400fbf9f
JW
1686 if (memb == 0)
1687 return 0;
1688 }
46df2823
JM
1689 else if (TREE_CODE (a2) == UNION_TYPE
1690 && (TYPE_NAME (a2) == 0
ebf0bf7f 1691 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1692 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1693 && tree_int_cst_equal (TYPE_SIZE (a2),
1694 TYPE_SIZE (a1)))
400fbf9f
JW
1695 {
1696 tree memb;
46df2823 1697 for (memb = TYPE_FIELDS (a2);
910ad8de 1698 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1699 {
1700 tree mv3 = TREE_TYPE (memb);
1701 if (mv3 && mv3 != error_mark_node
1702 && TREE_CODE (mv3) != ARRAY_TYPE)
1703 mv3 = TYPE_MAIN_VARIANT (mv3);
dc5027f4
JM
1704 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1705 different_types_p))
58cb41e6
JJ
1706 break;
1707 }
400fbf9f
JW
1708 if (memb == 0)
1709 return 0;
1710 }
1711 else
1712 return 0;
1713 }
1714
1715 /* comptypes said ok, but record if it said to warn. */
1716 if (newval > val)
1717 val = newval;
1718
1719 args1 = TREE_CHAIN (args1);
1720 args2 = TREE_CHAIN (args2);
1721 }
1722}
400fbf9f 1723\f
400fbf9f
JW
1724/* Compute the size to increment a pointer by. */
1725
4e2fb7de 1726static tree
58f9752a 1727c_size_in_bytes (const_tree type)
400fbf9f
JW
1728{
1729 enum tree_code code = TREE_CODE (type);
1730
fed3cef0
RK
1731 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1732 return size_one_node;
1733
d0f062fb 1734 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1735 {
1736 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1737 return size_one_node;
400fbf9f
JW
1738 }
1739
1740 /* Convert in case a char is more than one unit. */
db3927fb
AH
1741 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1742 size_int (TYPE_PRECISION (char_type_node)
1743 / BITS_PER_UNIT));
400fbf9f 1744}
400fbf9f 1745\f
400fbf9f
JW
1746/* Return either DECL or its known constant value (if it has one). */
1747
56cb9733 1748tree
2f6e4e97 1749decl_constant_value (tree decl)
400fbf9f 1750{
a7c1916a 1751 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1752 in a place where a variable is invalid. Note that DECL_INITIAL
1753 isn't valid for a PARM_DECL. */
a7c1916a 1754 current_function_decl != 0
4f976745 1755 && TREE_CODE (decl) != PARM_DECL
3f75a254 1756 && !TREE_THIS_VOLATILE (decl)
83bab8db 1757 && TREE_READONLY (decl)
400fbf9f
JW
1758 && DECL_INITIAL (decl) != 0
1759 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1760 /* This is invalid if initial value is not constant.
1761 If it has either a function call, a memory reference,
1762 or a variable, then re-evaluating it could give different results. */
1763 && TREE_CONSTANT (DECL_INITIAL (decl))
1764 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1765 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1766 return DECL_INITIAL (decl);
1767 return decl;
1768}
1769
f2a71bbc
JM
1770/* Convert the array expression EXP to a pointer. */
1771static tree
c2255bc4 1772array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1773{
f2a71bbc 1774 tree orig_exp = exp;
207bf485 1775 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1776 tree adr;
1777 tree restype = TREE_TYPE (type);
1778 tree ptrtype;
207bf485 1779
f2a71bbc 1780 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1781
f2a71bbc 1782 STRIP_TYPE_NOPS (exp);
207bf485 1783
487a92fe
JM
1784 if (TREE_NO_WARNING (orig_exp))
1785 TREE_NO_WARNING (exp) = 1;
207bf485 1786
f2a71bbc
JM
1787 ptrtype = build_pointer_type (restype);
1788
1789 if (TREE_CODE (exp) == INDIRECT_REF)
1790 return convert (ptrtype, TREE_OPERAND (exp, 0));
1791
1f37c583
JM
1792 /* In C++ array compound literals are temporary objects unless they are
1793 const or appear in namespace scope, so they are destroyed too soon
1794 to use them for much of anything (c++/53220). */
1795 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1796 {
1797 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1798 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1799 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1800 "converting an array compound literal to a pointer "
1801 "is ill-formed in C++");
1802 }
1803
c2255bc4 1804 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1805 return convert (ptrtype, adr);
1806}
207bf485 1807
f2a71bbc
JM
1808/* Convert the function expression EXP to a pointer. */
1809static tree
c2255bc4 1810function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1811{
1812 tree orig_exp = exp;
207bf485 1813
f2a71bbc 1814 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1815
f2a71bbc 1816 STRIP_TYPE_NOPS (exp);
207bf485 1817
f2a71bbc
JM
1818 if (TREE_NO_WARNING (orig_exp))
1819 TREE_NO_WARNING (exp) = 1;
207bf485 1820
c2255bc4 1821 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1822}
207bf485 1823
ebfbbdc5
JJ
1824/* Mark EXP as read, not just set, for set but not used -Wunused
1825 warning purposes. */
1826
1827void
1828mark_exp_read (tree exp)
1829{
1830 switch (TREE_CODE (exp))
1831 {
1832 case VAR_DECL:
1833 case PARM_DECL:
1834 DECL_READ_P (exp) = 1;
1835 break;
1836 case ARRAY_REF:
1837 case COMPONENT_REF:
1838 case MODIFY_EXPR:
1839 case REALPART_EXPR:
1840 case IMAGPART_EXPR:
1841 CASE_CONVERT:
1842 case ADDR_EXPR:
1843 mark_exp_read (TREE_OPERAND (exp, 0));
1844 break;
1845 case COMPOUND_EXPR:
82c3c067 1846 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1847 mark_exp_read (TREE_OPERAND (exp, 1));
1848 break;
1849 default:
1850 break;
1851 }
1852}
1853
f2a71bbc
JM
1854/* Perform the default conversion of arrays and functions to pointers.
1855 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1856 return EXP.
1857
1858 LOC is the location of the expression. */
f2a71bbc
JM
1859
1860struct c_expr
c2255bc4 1861default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1862{
1863 tree orig_exp = exp.value;
1864 tree type = TREE_TYPE (exp.value);
1865 enum tree_code code = TREE_CODE (type);
1866
1867 switch (code)
1868 {
1869 case ARRAY_TYPE:
1870 {
1871 bool not_lvalue = false;
1872 bool lvalue_array_p;
1873
1874 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1875 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1876 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1877 {
1878 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1879 not_lvalue = true;
1880 exp.value = TREE_OPERAND (exp.value, 0);
1881 }
1882
1883 if (TREE_NO_WARNING (orig_exp))
1884 TREE_NO_WARNING (exp.value) = 1;
1885
1886 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1887 if (!flag_isoc99 && !lvalue_array_p)
1888 {
1889 /* Before C99, non-lvalue arrays do not decay to pointers.
1890 Normally, using such an array would be invalid; but it can
1891 be used correctly inside sizeof or as a statement expression.
1892 Thus, do not give an error here; an error will result later. */
1893 return exp;
1894 }
1895
c2255bc4 1896 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1897 }
1898 break;
1899 case FUNCTION_TYPE:
c2255bc4 1900 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1901 break;
1902 default:
f2a71bbc 1903 break;
207bf485 1904 }
f2a71bbc 1905
207bf485
JM
1906 return exp;
1907}
1908
ebfbbdc5
JJ
1909struct c_expr
1910default_function_array_read_conversion (location_t loc, struct c_expr exp)
1911{
1912 mark_exp_read (exp.value);
1913 return default_function_array_conversion (loc, exp);
1914}
522ddfa2
JM
1915
1916/* EXP is an expression of integer type. Apply the integer promotions
1917 to it and return the promoted value. */
400fbf9f
JW
1918
1919tree
522ddfa2 1920perform_integral_promotions (tree exp)
400fbf9f 1921{
b3694847
SS
1922 tree type = TREE_TYPE (exp);
1923 enum tree_code code = TREE_CODE (type);
400fbf9f 1924
522ddfa2 1925 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 1926
400fbf9f
JW
1927 /* Normally convert enums to int,
1928 but convert wide enums to something wider. */
1929 if (code == ENUMERAL_TYPE)
1930 {
b0c48229
NB
1931 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1932 TYPE_PRECISION (integer_type_node)),
1933 ((TYPE_PRECISION (type)
1934 >= TYPE_PRECISION (integer_type_node))
8df83eae 1935 && TYPE_UNSIGNED (type)));
05bccae2 1936
400fbf9f
JW
1937 return convert (type, exp);
1938 }
1939
522ddfa2
JM
1940 /* ??? This should no longer be needed now bit-fields have their
1941 proper types. */
9753f113 1942 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 1943 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 1944 /* If it's thinner than an int, promote it like a
d72040f5 1945 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
1946 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1947 TYPE_PRECISION (integer_type_node)))
f458d1d5 1948 return convert (integer_type_node, exp);
9753f113 1949
d72040f5 1950 if (c_promoting_integer_type_p (type))
400fbf9f 1951 {
f458d1d5 1952 /* Preserve unsignedness if not really getting any wider. */
8df83eae 1953 if (TYPE_UNSIGNED (type)
f458d1d5 1954 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 1955 return convert (unsigned_type_node, exp);
05bccae2 1956
400fbf9f
JW
1957 return convert (integer_type_node, exp);
1958 }
05bccae2 1959
522ddfa2
JM
1960 return exp;
1961}
1962
1963
1964/* Perform default promotions for C data used in expressions.
46bdb9cf 1965 Enumeral types or short or char are converted to int.
522ddfa2
JM
1966 In addition, manifest constants symbols are replaced by their values. */
1967
1968tree
1969default_conversion (tree exp)
1970{
1971 tree orig_exp;
1972 tree type = TREE_TYPE (exp);
1973 enum tree_code code = TREE_CODE (type);
40449a90 1974 tree promoted_type;
522ddfa2 1975
ebfbbdc5
JJ
1976 mark_exp_read (exp);
1977
46bdb9cf
JM
1978 /* Functions and arrays have been converted during parsing. */
1979 gcc_assert (code != FUNCTION_TYPE);
1980 if (code == ARRAY_TYPE)
1981 return exp;
522ddfa2
JM
1982
1983 /* Constants can be used directly unless they're not loadable. */
1984 if (TREE_CODE (exp) == CONST_DECL)
1985 exp = DECL_INITIAL (exp);
1986
522ddfa2
JM
1987 /* Strip no-op conversions. */
1988 orig_exp = exp;
1989 STRIP_TYPE_NOPS (exp);
1990
1991 if (TREE_NO_WARNING (orig_exp))
1992 TREE_NO_WARNING (exp) = 1;
1993
400fbf9f
JW
1994 if (code == VOID_TYPE)
1995 {
1996 error ("void value not ignored as it ought to be");
1997 return error_mark_node;
1998 }
808d6eaa
JM
1999
2000 exp = require_complete_type (exp);
2001 if (exp == error_mark_node)
2002 return error_mark_node;
2003
40449a90
SL
2004 promoted_type = targetm.promoted_type (type);
2005 if (promoted_type)
2006 return convert (promoted_type, exp);
2007
808d6eaa
JM
2008 if (INTEGRAL_TYPE_P (type))
2009 return perform_integral_promotions (exp);
2010
400fbf9f
JW
2011 return exp;
2012}
2013\f
0fb96aa4 2014/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2015
2016 If the component name is not found, returns NULL_TREE. Otherwise,
2017 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2018 stepping down the chain to the component, which is in the last
2019 TREE_VALUE of the list. Normally the list is of length one, but if
2020 the component is embedded within (nested) anonymous structures or
2021 unions, the list steps down the chain to the component. */
2f6e4e97 2022
2f2d13da 2023static tree
0fb96aa4 2024lookup_field (tree type, tree component)
2f2d13da
DE
2025{
2026 tree field;
2027
2028 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2029 to the field elements. Use a binary search on this array to quickly
2030 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2031 will always be set for structures which have many elements. */
2032
22a0b85f 2033 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2034 {
2035 int bot, top, half;
d07605f5 2036 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2037
2038 field = TYPE_FIELDS (type);
2039 bot = 0;
d07605f5 2040 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2041 while (top - bot > 1)
2042 {
2f2d13da
DE
2043 half = (top - bot + 1) >> 1;
2044 field = field_array[bot+half];
2045
2046 if (DECL_NAME (field) == NULL_TREE)
2047 {
2048 /* Step through all anon unions in linear fashion. */
2049 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2050 {
2f2d13da 2051 field = field_array[bot++];
a68b98cf
RK
2052 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2053 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2054 {
0fb96aa4 2055 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2056
2057 if (anon)
2058 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2059
2060 /* The Plan 9 compiler permits referring
2061 directly to an anonymous struct/union field
2062 using a typedef name. */
2063 if (flag_plan9_extensions
2064 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2065 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2066 == TYPE_DECL)
2067 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2068 == component))
2069 break;
2f6e4e97 2070 }
2f2d13da
DE
2071 }
2072
2073 /* Entire record is only anon unions. */
2074 if (bot > top)
2075 return NULL_TREE;
2076
2077 /* Restart the binary search, with new lower bound. */
2078 continue;
2079 }
2080
e8b87aac 2081 if (DECL_NAME (field) == component)
2f2d13da 2082 break;
e8b87aac 2083 if (DECL_NAME (field) < component)
2f2d13da
DE
2084 bot += half;
2085 else
2086 top = bot + half;
2087 }
2088
2089 if (DECL_NAME (field_array[bot]) == component)
2090 field = field_array[bot];
2091 else if (DECL_NAME (field) != component)
e9b2c823 2092 return NULL_TREE;
2f2d13da
DE
2093 }
2094 else
2095 {
910ad8de 2096 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2097 {
e9b2c823
NB
2098 if (DECL_NAME (field) == NULL_TREE
2099 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2100 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2101 {
0fb96aa4 2102 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2103
e9b2c823
NB
2104 if (anon)
2105 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2106
2107 /* The Plan 9 compiler permits referring directly to an
2108 anonymous struct/union field using a typedef
2109 name. */
2110 if (flag_plan9_extensions
2111 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2112 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2113 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2114 == component))
2115 break;
2f2d13da
DE
2116 }
2117
2118 if (DECL_NAME (field) == component)
2119 break;
2120 }
e9b2c823
NB
2121
2122 if (field == NULL_TREE)
2123 return NULL_TREE;
2f2d13da
DE
2124 }
2125
e9b2c823 2126 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2127}
2128
c2255bc4
AH
2129/* Make an expression to refer to the COMPONENT field of structure or
2130 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2131 location of the COMPONENT_REF. */
400fbf9f
JW
2132
2133tree
c2255bc4 2134build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2135{
b3694847
SS
2136 tree type = TREE_TYPE (datum);
2137 enum tree_code code = TREE_CODE (type);
2138 tree field = NULL;
2139 tree ref;
1e57bf47 2140 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2141
7a3ea201
RH
2142 if (!objc_is_public (datum, component))
2143 return error_mark_node;
2144
46a88c12 2145 /* Detect Objective-C property syntax object.property. */
668ea4b1 2146 if (c_dialect_objc ()
46a88c12 2147 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2148 return ref;
2149
400fbf9f
JW
2150 /* See if there is a field or component with name COMPONENT. */
2151
2152 if (code == RECORD_TYPE || code == UNION_TYPE)
2153 {
d0f062fb 2154 if (!COMPLETE_TYPE_P (type))
400fbf9f 2155 {
7a228918 2156 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2157 return error_mark_node;
2158 }
2159
0fb96aa4 2160 field = lookup_field (type, component);
400fbf9f
JW
2161
2162 if (!field)
2163 {
c2255bc4 2164 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2165 return error_mark_node;
2166 }
400fbf9f 2167
e9b2c823
NB
2168 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2169 This might be better solved in future the way the C++ front
2170 end does it - by giving the anonymous entities each a
2171 separate name and type, and then have build_component_ref
2172 recursively call itself. We can't do that here. */
46ea50cb 2173 do
19d76e60 2174 {
e9b2c823 2175 tree subdatum = TREE_VALUE (field);
efed193e
JM
2176 int quals;
2177 tree subtype;
1e57bf47 2178 bool use_datum_quals;
e9b2c823
NB
2179
2180 if (TREE_TYPE (subdatum) == error_mark_node)
2181 return error_mark_node;
2182
1e57bf47
JM
2183 /* If this is an rvalue, it does not have qualifiers in C
2184 standard terms and we must avoid propagating such
2185 qualifiers down to a non-lvalue array that is then
2186 converted to a pointer. */
2187 use_datum_quals = (datum_lvalue
2188 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2189
efed193e 2190 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2191 if (use_datum_quals)
2192 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2193 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2194
2195 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2196 NULL_TREE);
c2255bc4 2197 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2198 if (TREE_READONLY (subdatum)
2199 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2200 TREE_READONLY (ref) = 1;
1e57bf47
JM
2201 if (TREE_THIS_VOLATILE (subdatum)
2202 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2203 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2204
2205 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2206 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2207
19d76e60 2208 datum = ref;
46ea50cb
RS
2209
2210 field = TREE_CHAIN (field);
19d76e60 2211 }
46ea50cb 2212 while (field);
19d76e60 2213
400fbf9f
JW
2214 return ref;
2215 }
2216 else if (code != ERROR_MARK)
c2255bc4
AH
2217 error_at (loc,
2218 "request for member %qE in something not a structure or union",
2219 component);
400fbf9f
JW
2220
2221 return error_mark_node;
2222}
2223\f
2224/* Given an expression PTR for a pointer, return an expression
2225 for the value pointed to.
6a3799eb
AH
2226 ERRORSTRING is the name of the operator to appear in error messages.
2227
2228 LOC is the location to use for the generated tree. */
400fbf9f
JW
2229
2230tree
dd865ef6 2231build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2232{
b3694847
SS
2233 tree pointer = default_conversion (ptr);
2234 tree type = TREE_TYPE (pointer);
6a3799eb 2235 tree ref;
400fbf9f
JW
2236
2237 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2238 {
1043771b 2239 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2240 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2241 {
2242 /* If a warning is issued, mark it to avoid duplicates from
2243 the backend. This only needs to be done at
2244 warn_strict_aliasing > 2. */
2245 if (warn_strict_aliasing > 2)
2246 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2247 type, TREE_OPERAND (pointer, 0)))
2248 TREE_NO_WARNING (pointer) = 1;
2249 }
2250
870cc33b 2251 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2252 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2253 == TREE_TYPE (type)))
6a3799eb
AH
2254 {
2255 ref = TREE_OPERAND (pointer, 0);
2256 protected_set_expr_location (ref, loc);
2257 return ref;
2258 }
870cc33b
RS
2259 else
2260 {
2261 tree t = TREE_TYPE (type);
46df2823 2262
984dfd8c 2263 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2264
baae9b65 2265 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2266 {
c9f9eb5d 2267 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2268 return error_mark_node;
2269 }
7d882b83 2270 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2271 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2272
2273 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2274 so that we get the proper error message if the result is used
2275 to assign to. Also, &* is supposed to be a no-op.
2276 And ANSI C seems to specify that the type of the result
2277 should be the const type. */
2278 /* A de-reference of a pointer to const is not a const. It is valid
2279 to change it via some other pointer. */
2280 TREE_READONLY (ref) = TYPE_READONLY (t);
2281 TREE_SIDE_EFFECTS (ref)
271bd540 2282 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2283 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2284 protected_set_expr_location (ref, loc);
870cc33b
RS
2285 return ref;
2286 }
2287 }
400fbf9f 2288 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2289 invalid_indirection_error (loc, type, errstring);
2290
400fbf9f
JW
2291 return error_mark_node;
2292}
2293
2294/* This handles expressions of the form "a[i]", which denotes
2295 an array reference.
2296
2297 This is logically equivalent in C to *(a+i), but we may do it differently.
2298 If A is a variable or a member, we generate a primitive ARRAY_REF.
2299 This avoids forcing the array out of registers, and can work on
2300 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2301 by functions).
2302
30cd1c5d
AS
2303 For vector types, allow vector[i] but not i[vector], and create
2304 *(((type*)&vectortype) + i) for the expression.
2305
6a3799eb 2306 LOC is the location to use for the returned expression. */
400fbf9f
JW
2307
2308tree
c2255bc4 2309build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2310{
6a3799eb 2311 tree ret;
a4ab7973 2312 bool swapped = false;
400fbf9f
JW
2313 if (TREE_TYPE (array) == error_mark_node
2314 || TREE_TYPE (index) == error_mark_node)
2315 return error_mark_node;
2316
36536d79
BI
2317 if (flag_enable_cilkplus && contains_array_notation_expr (index))
2318 {
2319 size_t rank = 0;
2320 if (!find_rank (loc, index, index, true, &rank))
2321 return error_mark_node;
2322 if (rank > 1)
2323 {
2324 error_at (loc, "rank of the array's index is greater than 1");
2325 return error_mark_node;
2326 }
2327 }
a4ab7973 2328 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2329 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2330 /* Allow vector[index] but not index[vector]. */
2331 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2332 {
a4ab7973
JM
2333 tree temp;
2334 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2335 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2336 {
f90e8e2e 2337 error_at (loc,
30cd1c5d
AS
2338 "subscripted value is neither array nor pointer nor vector");
2339
fdeefd49
RS
2340 return error_mark_node;
2341 }
a4ab7973
JM
2342 temp = array;
2343 array = index;
2344 index = temp;
2345 swapped = true;
2346 }
2347
2348 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2349 {
a63068b6 2350 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2351 return error_mark_node;
2352 }
2353
2354 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2355 {
a63068b6 2356 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2357 return error_mark_node;
2358 }
2359
ff6b6641
GDR
2360 /* ??? Existing practice has been to warn only when the char
2361 index is syntactically the index, not for char[array]. */
2362 if (!swapped)
2363 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2364
2365 /* Apply default promotions *after* noticing character types. */
2366 index = default_conversion (index);
2367
2368 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2369
7edaa4d2 2370 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2371
2372 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2373 {
e4d35515 2374 tree rval, type;
fdeefd49 2375
400fbf9f
JW
2376 /* An array that is indexed by a non-constant
2377 cannot be stored in a register; we must be able to do
2378 address arithmetic on its address.
2379 Likewise an array of elements of variable size. */
2380 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2381 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2382 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2383 {
dffd7eb6 2384 if (!c_mark_addressable (array))
400fbf9f
JW
2385 return error_mark_node;
2386 }
e6d52559
JW
2387 /* An array that is indexed by a constant value which is not within
2388 the array bounds cannot be stored in a register either; because we
2389 would get a crash in store_bit_field/extract_bit_field when trying
2390 to access a non-existent part of the register. */
2391 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2392 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2393 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2394 {
dffd7eb6 2395 if (!c_mark_addressable (array))
e6d52559
JW
2396 return error_mark_node;
2397 }
400fbf9f 2398
400fbf9f
JW
2399 if (pedantic)
2400 {
2401 tree foo = array;
2402 while (TREE_CODE (foo) == COMPONENT_REF)
2403 foo = TREE_OPERAND (foo, 0);
5baeaac0 2404 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2405 pedwarn (loc, OPT_Wpedantic,
fcf73884 2406 "ISO C forbids subscripting %<register%> array");
3f75a254 2407 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2408 pedwarn (loc, OPT_Wpedantic,
fcf73884 2409 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2410 }
2411
46df2823 2412 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2413 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2414 /* Array ref is const/volatile if the array elements are
c22cacf3 2415 or if the array is. */
400fbf9f
JW
2416 TREE_READONLY (rval)
2417 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2418 | TREE_READONLY (array));
2419 TREE_SIDE_EFFECTS (rval)
2420 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2421 | TREE_SIDE_EFFECTS (array));
2422 TREE_THIS_VOLATILE (rval)
2423 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2424 /* This was added by rms on 16 Nov 91.
2f6e4e97 2425 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2426 in an inline function.
2427 Hope it doesn't break something else. */
2428 | TREE_THIS_VOLATILE (array));
928c19bb 2429 ret = require_complete_type (rval);
6a3799eb
AH
2430 protected_set_expr_location (ret, loc);
2431 return ret;
400fbf9f 2432 }
a4ab7973
JM
2433 else
2434 {
2435 tree ar = default_conversion (array);
400fbf9f 2436
a4ab7973
JM
2437 if (ar == error_mark_node)
2438 return ar;
400fbf9f 2439
a4ab7973
JM
2440 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2441 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2442
ba47d38d 2443 return build_indirect_ref
c9f9eb5d 2444 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2445 RO_ARRAY_INDEXING);
a4ab7973 2446 }
400fbf9f
JW
2447}
2448\f
7e585d16 2449/* Build an external reference to identifier ID. FUN indicates
766beb40 2450 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2451 location of the identifier. This sets *TYPE to the type of the
2452 identifier, which is not the same as the type of the returned value
2453 for CONST_DECLs defined as enum constants. If the type of the
2454 identifier is not available, *TYPE is set to NULL. */
7e585d16 2455tree
c2255bc4 2456build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2457{
2458 tree ref;
2459 tree decl = lookup_name (id);
16b34ad6
ZL
2460
2461 /* In Objective-C, an instance variable (ivar) may be preferred to
2462 whatever lookup_name() found. */
2463 decl = objc_lookup_ivar (decl, id);
7e585d16 2464
6866c6e8 2465 *type = NULL;
339a28b9 2466 if (decl && decl != error_mark_node)
6866c6e8
ILT
2467 {
2468 ref = decl;
2469 *type = TREE_TYPE (ref);
2470 }
339a28b9
ZW
2471 else if (fun)
2472 /* Implicit function declaration. */
c2255bc4 2473 ref = implicitly_declare (loc, id);
339a28b9
ZW
2474 else if (decl == error_mark_node)
2475 /* Don't complain about something that's already been
2476 complained about. */
2477 return error_mark_node;
2478 else
2479 {
c2255bc4 2480 undeclared_variable (loc, id);
339a28b9
ZW
2481 return error_mark_node;
2482 }
7e585d16
ZW
2483
2484 if (TREE_TYPE (ref) == error_mark_node)
2485 return error_mark_node;
2486
339a28b9 2487 if (TREE_DEPRECATED (ref))
9b86d6bb 2488 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2489
ad960f56 2490 /* Recursive call does not count as usage. */
b8698a0f 2491 if (ref != current_function_decl)
ad960f56 2492 {
ad960f56
MLI
2493 TREE_USED (ref) = 1;
2494 }
7e585d16 2495
bc4b653b
JM
2496 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2497 {
2498 if (!in_sizeof && !in_typeof)
2499 C_DECL_USED (ref) = 1;
2500 else if (DECL_INITIAL (ref) == 0
2501 && DECL_EXTERNAL (ref)
2502 && !TREE_PUBLIC (ref))
2503 record_maybe_used_decl (ref);
2504 }
2505
7e585d16
ZW
2506 if (TREE_CODE (ref) == CONST_DECL)
2507 {
6193b8b7 2508 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2509
2510 if (warn_cxx_compat
2511 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2512 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2513 {
2514 warning_at (loc, OPT_Wc___compat,
2515 ("enum constant defined in struct or union "
2516 "is not visible in C++"));
2517 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2518 }
2519
7e585d16
ZW
2520 ref = DECL_INITIAL (ref);
2521 TREE_CONSTANT (ref) = 1;
2522 }
6a29edea 2523 else if (current_function_decl != 0
4b1e44be 2524 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2525 && (TREE_CODE (ref) == VAR_DECL
2526 || TREE_CODE (ref) == PARM_DECL
2527 || TREE_CODE (ref) == FUNCTION_DECL))
2528 {
2529 tree context = decl_function_context (ref);
2f6e4e97 2530
6a29edea
EB
2531 if (context != 0 && context != current_function_decl)
2532 DECL_NONLOCAL (ref) = 1;
2533 }
71113fcd
GK
2534 /* C99 6.7.4p3: An inline definition of a function with external
2535 linkage ... shall not contain a reference to an identifier with
2536 internal linkage. */
2537 else if (current_function_decl != 0
2538 && DECL_DECLARED_INLINE_P (current_function_decl)
2539 && DECL_EXTERNAL (current_function_decl)
2540 && VAR_OR_FUNCTION_DECL_P (ref)
2541 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2542 && ! TREE_PUBLIC (ref)
2543 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2544 record_inline_static (loc, current_function_decl, ref,
2545 csi_internal);
7e585d16
ZW
2546
2547 return ref;
2548}
2549
bc4b653b
JM
2550/* Record details of decls possibly used inside sizeof or typeof. */
2551struct maybe_used_decl
2552{
2553 /* The decl. */
2554 tree decl;
2555 /* The level seen at (in_sizeof + in_typeof). */
2556 int level;
2557 /* The next one at this level or above, or NULL. */
2558 struct maybe_used_decl *next;
2559};
2560
2561static struct maybe_used_decl *maybe_used_decls;
2562
2563/* Record that DECL, an undefined static function reference seen
2564 inside sizeof or typeof, might be used if the operand of sizeof is
2565 a VLA type or the operand of typeof is a variably modified
2566 type. */
2567
4e2fb7de 2568static void
bc4b653b
JM
2569record_maybe_used_decl (tree decl)
2570{
2571 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2572 t->decl = decl;
2573 t->level = in_sizeof + in_typeof;
2574 t->next = maybe_used_decls;
2575 maybe_used_decls = t;
2576}
2577
2578/* Pop the stack of decls possibly used inside sizeof or typeof. If
2579 USED is false, just discard them. If it is true, mark them used
2580 (if no longer inside sizeof or typeof) or move them to the next
2581 level up (if still inside sizeof or typeof). */
2582
2583void
2584pop_maybe_used (bool used)
2585{
2586 struct maybe_used_decl *p = maybe_used_decls;
2587 int cur_level = in_sizeof + in_typeof;
2588 while (p && p->level > cur_level)
2589 {
2590 if (used)
2591 {
2592 if (cur_level == 0)
2593 C_DECL_USED (p->decl) = 1;
2594 else
2595 p->level = cur_level;
2596 }
2597 p = p->next;
2598 }
2599 if (!used || cur_level == 0)
2600 maybe_used_decls = p;
2601}
2602
2603/* Return the result of sizeof applied to EXPR. */
2604
2605struct c_expr
c2255bc4 2606c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2607{
2608 struct c_expr ret;
ad97f4be
JM
2609 if (expr.value == error_mark_node)
2610 {
2611 ret.value = error_mark_node;
2612 ret.original_code = ERROR_MARK;
6866c6e8 2613 ret.original_type = NULL;
ad97f4be
JM
2614 pop_maybe_used (false);
2615 }
2616 else
2617 {
928c19bb
JM
2618 bool expr_const_operands = true;
2619 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2620 &expr_const_operands);
c2255bc4 2621 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2622 c_last_sizeof_arg = expr.value;
2623 ret.original_code = SIZEOF_EXPR;
6866c6e8 2624 ret.original_type = NULL;
928c19bb 2625 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2626 {
2627 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2628 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2629 folded_expr, ret.value);
2630 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2631 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2632 }
928c19bb 2633 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2634 }
bc4b653b
JM
2635 return ret;
2636}
2637
2638/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2639 name passed to sizeof (rather than the type itself). LOC is the
2640 location of the original expression. */
bc4b653b
JM
2641
2642struct c_expr
c2255bc4 2643c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2644{
2645 tree type;
2646 struct c_expr ret;
928c19bb
JM
2647 tree type_expr = NULL_TREE;
2648 bool type_expr_const = true;
2649 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2650 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2651 c_last_sizeof_arg = type;
2652 ret.original_code = SIZEOF_EXPR;
6866c6e8 2653 ret.original_type = NULL;
24070fcb
JM
2654 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2655 && c_vla_type_p (type))
928c19bb 2656 {
24070fcb
JM
2657 /* If the type is a [*] array, it is a VLA but is represented as
2658 having a size of zero. In such a case we must ensure that
2659 the result of sizeof does not get folded to a constant by
2660 c_fully_fold, because if the size is evaluated the result is
2661 not constant and so constraints on zero or negative size
2662 arrays must not be applied when this sizeof call is inside
2663 another array declarator. */
2664 if (!type_expr)
2665 type_expr = integer_zero_node;
928c19bb
JM
2666 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2667 type_expr, ret.value);
2668 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2669 }
16464cc1
VR
2670 pop_maybe_used (type != error_mark_node
2671 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2672 return ret;
2673}
2674
400fbf9f 2675/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2676 The function call is at LOC.
400fbf9f
JW
2677 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2678 TREE_VALUE of each node is a parameter-expression.
2679 FUNCTION's data type may be a function type or a pointer-to-function. */
2680
2681tree
c2255bc4 2682build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2683{
9771b263 2684 vec<tree, va_gc> *v;
bbbbb16a
ILT
2685 tree ret;
2686
9771b263 2687 vec_alloc (v, list_length (params));
bbbbb16a 2688 for (; params; params = TREE_CHAIN (params))
9771b263
DN
2689 v->quick_push (TREE_VALUE (params));
2690 ret = build_function_call_vec (loc, function, v, NULL);
2691 vec_free (v);
bbbbb16a
ILT
2692 return ret;
2693}
2694
ae52741c
MLI
2695/* Give a note about the location of the declaration of DECL. */
2696
2697static void inform_declaration (tree decl)
2698{
2699 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2700 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2701}
2702
bbbbb16a
ILT
2703/* Build a function call to function FUNCTION with parameters PARAMS.
2704 ORIGTYPES, if not NULL, is a vector of types; each element is
2705 either NULL or the original type of the corresponding element in
2706 PARAMS. The original type may differ from TREE_TYPE of the
2707 parameter for enums. FUNCTION's data type may be a function type
2708 or pointer-to-function. This function changes the elements of
2709 PARAMS. */
2710
2711tree
9771b263
DN
2712build_function_call_vec (location_t loc, tree function,
2713 vec<tree, va_gc> *params,
2714 vec<tree, va_gc> *origtypes)
400fbf9f 2715{
b3694847 2716 tree fntype, fundecl = 0;
4977bab6 2717 tree name = NULL_TREE, result;
c96f4f73 2718 tree tem;
94a0dd7b
SL
2719 int nargs;
2720 tree *argarray;
b8698a0f 2721
400fbf9f 2722
fc76e425 2723 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2724 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2725
2726 /* Convert anything with function type to a pointer-to-function. */
2727 if (TREE_CODE (function) == FUNCTION_DECL)
2728 {
58646b77
PB
2729 /* Implement type-directed function overloading for builtins.
2730 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2731 handle all the type checking. The result is a complete expression
2732 that implements this function call. */
c2255bc4 2733 tem = resolve_overloaded_builtin (loc, function, params);
58646b77
PB
2734 if (tem)
2735 return tem;
48ae6c13 2736
400fbf9f 2737 name = DECL_NAME (function);
0a35513e
AH
2738
2739 if (flag_tm)
2740 tm_malloc_replacement (function);
a5eadacc 2741 fundecl = function;
86951993
AM
2742 /* Atomic functions have type checking/casting already done. They are
2743 often rewritten and don't match the original parameter list. */
2744 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2745 origtypes = NULL;
36536d79
BI
2746
2747 if (flag_enable_cilkplus
2748 && is_cilkplus_reduce_builtin (function))
2749 origtypes = NULL;
400fbf9f 2750 }
f2a71bbc 2751 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2752 function = function_to_pointer_conversion (loc, function);
400fbf9f 2753
6e955430
ZL
2754 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2755 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2756 if (params && !params->is_empty ())
2757 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2758
928c19bb
JM
2759 function = c_fully_fold (function, false, NULL);
2760
400fbf9f
JW
2761 fntype = TREE_TYPE (function);
2762
2763 if (TREE_CODE (fntype) == ERROR_MARK)
2764 return error_mark_node;
2765
2766 if (!(TREE_CODE (fntype) == POINTER_TYPE
2767 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2768 {
ae52741c
MLI
2769 if (!flag_diagnostics_show_caret)
2770 error_at (loc,
2771 "called object %qE is not a function or function pointer",
2772 function);
2773 else if (DECL_P (function))
2774 {
2775 error_at (loc,
2776 "called object %qD is not a function or function pointer",
2777 function);
2778 inform_declaration (function);
2779 }
2780 else
2781 error_at (loc,
2782 "called object is not a function or function pointer");
400fbf9f
JW
2783 return error_mark_node;
2784 }
2785
5ce89b2e
JM
2786 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2787 current_function_returns_abnormally = 1;
2788
400fbf9f
JW
2789 /* fntype now gets the type of function pointed to. */
2790 fntype = TREE_TYPE (fntype);
2791
ab4194da
JM
2792 /* Convert the parameters to the types declared in the
2793 function prototype, or apply default promotions. */
2794
bbbbb16a
ILT
2795 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2796 function, fundecl);
ab4194da
JM
2797 if (nargs < 0)
2798 return error_mark_node;
2799
c96f4f73
EB
2800 /* Check that the function is called through a compatible prototype.
2801 If it is not, replace the call by a trap, wrapped up in a compound
2802 expression if necessary. This has the nice side-effect to prevent
2803 the tree-inliner from generating invalid assignment trees which may
58393038 2804 blow up in the RTL expander later. */
1043771b 2805 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2806 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2807 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2808 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2809 {
2810 tree return_type = TREE_TYPE (fntype);
e79983f4
MM
2811 tree trap = build_function_call (loc,
2812 builtin_decl_explicit (BUILT_IN_TRAP),
c96f4f73 2813 NULL_TREE);
ab4194da 2814 int i;
c96f4f73
EB
2815
2816 /* This situation leads to run-time undefined behavior. We can't,
2817 therefore, simply error unless we can prove that all possible
2818 executions of the program must execute the code. */
c2255bc4 2819 if (warning_at (loc, 0, "function called through a non-compatible type"))
71205d17
MLI
2820 /* We can, however, treat "undefined" any way we please.
2821 Call abort to encourage the user to fix the program. */
c2255bc4 2822 inform (loc, "if this code is reached, the program will abort");
ab4194da
JM
2823 /* Before the abort, allow the function arguments to exit or
2824 call longjmp. */
2825 for (i = 0; i < nargs; i++)
9771b263 2826 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
bba745c1 2827
c96f4f73 2828 if (VOID_TYPE_P (return_type))
3ce62965
JM
2829 {
2830 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
db3927fb 2831 pedwarn (loc, 0,
3ce62965
JM
2832 "function with qualified void return type called");
2833 return trap;
2834 }
c96f4f73
EB
2835 else
2836 {
2837 tree rhs;
2838
2839 if (AGGREGATE_TYPE_P (return_type))
c2255bc4 2840 rhs = build_compound_literal (loc, return_type,
9771b263
DN
2841 build_constructor (return_type,
2842 NULL),
928c19bb 2843 false);
c96f4f73 2844 else
e8160c9a 2845 rhs = build_zero_cst (return_type);
c96f4f73 2846
3ce62965
JM
2847 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2848 trap, rhs));
c96f4f73
EB
2849 }
2850 }
2851
9771b263 2852 argarray = vec_safe_address (params);
bbbbb16a 2853
83322951
RG
2854 /* Check that arguments to builtin functions match the expectations. */
2855 if (fundecl
2856 && DECL_BUILT_IN (fundecl)
2857 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2858 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2859 return error_mark_node;
400fbf9f 2860
83322951 2861 /* Check that the arguments to the function are valid. */
dde05067 2862 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2863
928c19bb
JM
2864 if (name != NULL_TREE
2865 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2866 {
928c19bb 2867 if (require_constant_value)
b8698a0f 2868 result =
db3927fb
AH
2869 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2870 function, nargs, argarray);
928c19bb 2871 else
db3927fb
AH
2872 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2873 function, nargs, argarray);
928c19bb
JM
2874 if (TREE_CODE (result) == NOP_EXPR
2875 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2876 STRIP_TYPE_NOPS (result);
bf730f15
RS
2877 }
2878 else
db3927fb
AH
2879 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2880 function, nargs, argarray);
b0b3afb2 2881
71653180 2882 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2883 {
2884 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2885 pedwarn (loc, 0,
3ce62965
JM
2886 "function with qualified void return type called");
2887 return result;
2888 }
1eb8759b 2889 return require_complete_type (result);
400fbf9f
JW
2890}
2891\f
bbbbb16a
ILT
2892/* Convert the argument expressions in the vector VALUES
2893 to the types in the list TYPELIST.
400fbf9f
JW
2894
2895 If TYPELIST is exhausted, or when an element has NULL as its type,
2896 perform the default conversions.
2897
bbbbb16a
ILT
2898 ORIGTYPES is the original types of the expressions in VALUES. This
2899 holds the type of enum values which have been converted to integral
2900 types. It may be NULL.
400fbf9f 2901
03dafa61
JM
2902 FUNCTION is a tree for the called function. It is used only for
2903 error messages, where it is formatted with %qE.
400fbf9f
JW
2904
2905 This is also where warnings about wrong number of args are generated.
2906
94a0dd7b 2907 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
2908 than the length of VALUES in some error situations), or -1 on
2909 failure. */
94a0dd7b
SL
2910
2911static int
9771b263
DN
2912convert_arguments (tree typelist, vec<tree, va_gc> *values,
2913 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
400fbf9f 2914{
bbbbb16a
ILT
2915 tree typetail, val;
2916 unsigned int parmnum;
06302a02 2917 bool error_args = false;
b5d32c25
KG
2918 const bool type_generic = fundecl
2919 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
8ce94e44 2920 bool type_generic_remove_excess_precision = false;
03dafa61 2921 tree selector;
03dafa61 2922
2ac2f164
JM
2923 /* Change pointer to function to the function itself for
2924 diagnostics. */
03dafa61
JM
2925 if (TREE_CODE (function) == ADDR_EXPR
2926 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 2927 function = TREE_OPERAND (function, 0);
03dafa61
JM
2928
2929 /* Handle an ObjC selector specially for diagnostics. */
2930 selector = objc_message_selector ();
400fbf9f 2931
8ce94e44
JM
2932 /* For type-generic built-in functions, determine whether excess
2933 precision should be removed (classification) or not
2934 (comparison). */
2935 if (type_generic
2936 && DECL_BUILT_IN (fundecl)
2937 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2938 {
2939 switch (DECL_FUNCTION_CODE (fundecl))
2940 {
2941 case BUILT_IN_ISFINITE:
2942 case BUILT_IN_ISINF:
2943 case BUILT_IN_ISINF_SIGN:
2944 case BUILT_IN_ISNAN:
2945 case BUILT_IN_ISNORMAL:
2946 case BUILT_IN_FPCLASSIFY:
2947 type_generic_remove_excess_precision = true;
2948 break;
2949
2950 default:
2951 type_generic_remove_excess_precision = false;
2952 break;
2953 }
2954 }
6d6efbb3
BI
2955 if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
2956 return vec_safe_length (values);
8ce94e44 2957
400fbf9f 2958 /* Scan the given expressions and types, producing individual
bbbbb16a 2959 converted arguments. */
400fbf9f 2960
bbbbb16a 2961 for (typetail = typelist, parmnum = 0;
9771b263 2962 values && values->iterate (parmnum, &val);
bbbbb16a 2963 ++parmnum)
400fbf9f 2964 {
b3694847 2965 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 2966 tree valtype = TREE_TYPE (val);
03dafa61
JM
2967 tree rname = function;
2968 int argnum = parmnum + 1;
4d3e6fae 2969 const char *invalid_func_diag;
8ce94e44 2970 bool excess_precision = false;
928c19bb 2971 bool npc;
bbbbb16a 2972 tree parmval;
400fbf9f
JW
2973
2974 if (type == void_type_node)
2975 {
19dc6d01
NP
2976 if (selector)
2977 error_at (input_location,
2978 "too many arguments to method %qE", selector);
2979 else
2980 error_at (input_location,
2981 "too many arguments to function %qE", function);
ae52741c 2982 inform_declaration (fundecl);
94a0dd7b 2983 return parmnum;
400fbf9f
JW
2984 }
2985
03dafa61
JM
2986 if (selector && argnum > 2)
2987 {
2988 rname = selector;
2989 argnum -= 2;
2990 }
2991
928c19bb 2992 npc = null_pointer_constant_p (val);
8ce94e44
JM
2993
2994 /* If there is excess precision and a prototype, convert once to
2995 the required type rather than converting via the semantic
2996 type. Likewise without a prototype a float value represented
2997 as long double should be converted once to double. But for
2998 type-generic classification functions excess precision must
2999 be removed here. */
3000 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3001 && (type || !type_generic || !type_generic_remove_excess_precision))
3002 {
3003 val = TREE_OPERAND (val, 0);
3004 excess_precision = true;
3005 }
928c19bb 3006 val = c_fully_fold (val, false, NULL);
ed248cf7 3007 STRIP_TYPE_NOPS (val);
400fbf9f 3008
400fbf9f
JW
3009 val = require_complete_type (val);
3010
3011 if (type != 0)
3012 {
3013 /* Formal parm type is specified by a function prototype. */
400fbf9f 3014
20913689 3015 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
3016 {
3017 error ("type of formal parameter %d is incomplete", parmnum + 1);
3018 parmval = val;
3019 }
3020 else
3021 {
bbbbb16a
ILT
3022 tree origtype;
3023
d45cf215
RS
3024 /* Optionally warn about conversions that
3025 differ from the default conversions. */
05170031 3026 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3027 {
e3a64162 3028 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3029
aae43c5f 3030 if (INTEGRAL_TYPE_P (type)
8ce94e44 3031 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3032 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3033 "rather than floating due to prototype",
3034 argnum, rname);
03829ad2 3035 if (INTEGRAL_TYPE_P (type)
8ce94e44 3036 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3037 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3038 "rather than complex due to prototype",
3039 argnum, rname);
aae43c5f 3040 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3041 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3042 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3043 "rather than floating due to prototype",
3044 argnum, rname);
400fbf9f 3045 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3046 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3047 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3048 "rather than integer due to prototype",
3049 argnum, rname);
03829ad2 3050 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3051 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3052 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3053 "rather than integer due to prototype",
3054 argnum, rname);
aae43c5f 3055 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3056 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3057 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3058 "rather than complex due to prototype",
3059 argnum, rname);
aae43c5f
RK
3060 /* ??? At some point, messages should be written about
3061 conversions between complex types, but that's too messy
3062 to do now. */
d45cf215 3063 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3064 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3065 {
3066 /* Warn if any argument is passed as `float',
047de90b 3067 since without a prototype it would be `double'. */
9a8ce21f
JG
3068 if (formal_prec == TYPE_PRECISION (float_type_node)
3069 && type != dfloat32_type_node)
d4ee4d25 3070 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
3071 "rather than %<double%> due to prototype",
3072 argnum, rname);
9a8ce21f
JG
3073
3074 /* Warn if mismatch between argument and prototype
3075 for decimal float types. Warn of conversions with
3076 binary float types and of precision narrowing due to
3077 prototype. */
8ce94e44 3078 else if (type != valtype
9a8ce21f
JG
3079 && (type == dfloat32_type_node
3080 || type == dfloat64_type_node
c22cacf3 3081 || type == dfloat128_type_node
8ce94e44
JM
3082 || valtype == dfloat32_type_node
3083 || valtype == dfloat64_type_node
3084 || valtype == dfloat128_type_node)
c22cacf3 3085 && (formal_prec
8ce94e44 3086 <= TYPE_PRECISION (valtype)
9a8ce21f 3087 || (type == dfloat128_type_node
8ce94e44 3088 && (valtype
c22cacf3 3089 != dfloat64_type_node
8ce94e44 3090 && (valtype
9a8ce21f
JG
3091 != dfloat32_type_node)))
3092 || (type == dfloat64_type_node
8ce94e44 3093 && (valtype
9a8ce21f
JG
3094 != dfloat32_type_node))))
3095 warning (0, "passing argument %d of %qE as %qT "
3096 "rather than %qT due to prototype",
8ce94e44 3097 argnum, rname, type, valtype);
9a8ce21f 3098
d45cf215 3099 }
3ed56f8a
KG
3100 /* Detect integer changing in width or signedness.
3101 These warnings are only activated with
05170031
MLI
3102 -Wtraditional-conversion, not with -Wtraditional. */
3103 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3104 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3105 {
d45cf215
RS
3106 tree would_have_been = default_conversion (val);
3107 tree type1 = TREE_TYPE (would_have_been);
3108
754a4d82 3109 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3110 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3111 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3112 /* No warning if function asks for enum
3113 and the actual arg is that enum type. */
3114 ;
3115 else if (formal_prec != TYPE_PRECISION (type1))
c2255bc4
AH
3116 warning (OPT_Wtraditional_conversion,
3117 "passing argument %d of %qE "
3176a0c2
DD
3118 "with different width due to prototype",
3119 argnum, rname);
8df83eae 3120 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3121 ;
800cd3b9
RS
3122 /* Don't complain if the formal parameter type
3123 is an enum, because we can't tell now whether
3124 the value was an enum--even the same enum. */
3125 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3126 ;
400fbf9f
JW
3127 else if (TREE_CODE (val) == INTEGER_CST
3128 && int_fits_type_p (val, type))
3129 /* Change in signedness doesn't matter
3130 if a constant value is unaffected. */
3131 ;
ce9895ae
RS
3132 /* If the value is extended from a narrower
3133 unsigned type, it doesn't matter whether we
3134 pass it as signed or unsigned; the value
3135 certainly is the same either way. */
8ce94e44
JM
3136 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3137 && TYPE_UNSIGNED (valtype))
ce9895ae 3138 ;
8df83eae 3139 else if (TYPE_UNSIGNED (type))
c2255bc4
AH
3140 warning (OPT_Wtraditional_conversion,
3141 "passing argument %d of %qE "
3176a0c2
DD
3142 "as unsigned due to prototype",
3143 argnum, rname);
3ed56f8a 3144 else
c2255bc4
AH
3145 warning (OPT_Wtraditional_conversion,
3146 "passing argument %d of %qE "
3176a0c2 3147 "as signed due to prototype", argnum, rname);
400fbf9f
JW
3148 }
3149 }
3150
8ce94e44
JM
3151 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3152 sake of better warnings from convert_and_check. */
3153 if (excess_precision)
3154 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3155 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
744aa42f
ILT
3156 parmval = convert_for_assignment (input_location, type, val,
3157 origtype, ic_argpass, npc,
2ac2f164
JM
3158 fundecl, function,
3159 parmnum + 1);
2f6e4e97 3160
61f71b34 3161 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3162 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3163 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3164 parmval = default_conversion (parmval);
400fbf9f 3165 }
400fbf9f 3166 }
8ce94e44
JM
3167 else if (TREE_CODE (valtype) == REAL_TYPE
3168 && (TYPE_PRECISION (valtype)
2531a1d9 3169 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3170 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3171 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3172 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3173 {
3174 if (type_generic)
bbbbb16a 3175 parmval = val;
b5d32c25 3176 else
0a0b3574
MM
3177 {
3178 /* Convert `float' to `double'. */
3179 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3180 warning (OPT_Wdouble_promotion,
3181 "implicit conversion from %qT to %qT when passing "
3182 "argument to function",
3183 valtype, double_type_node);
3184 parmval = convert (double_type_node, val);
3185 }
b5d32c25 3186 }
8ce94e44
JM
3187 else if (excess_precision && !type_generic)
3188 /* A "double" argument with excess precision being passed
3189 without a prototype or in variable arguments. */
bbbbb16a 3190 parmval = convert (valtype, val);
c22cacf3
MS
3191 else if ((invalid_func_diag =
3192 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3193 {
3194 error (invalid_func_diag);
94a0dd7b 3195 return -1;
4d3e6fae 3196 }
400fbf9f
JW
3197 else
3198 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3199 parmval = default_conversion (val);
3200
9771b263 3201 (*values)[parmnum] = parmval;
06302a02
JJ
3202 if (parmval == error_mark_node)
3203 error_args = true;
400fbf9f
JW
3204
3205 if (typetail)
3206 typetail = TREE_CHAIN (typetail);
3207 }
3208
9771b263 3209 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3210
400fbf9f 3211 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3212 {
6d6efbb3
BI
3213 error_at (input_location,
3214 "too few arguments to function %qE", function);
3215 inform_declaration (fundecl);
3216 return -1;
3789b316 3217 }
400fbf9f 3218
06302a02 3219 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3220}
3221\f
43f6dfd3
RS
3222/* This is the entry point used by the parser to build unary operators
3223 in the input. CODE, a tree_code, specifies the unary operator, and
3224 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3225 CONVERT_EXPR for code.
3226
3227 LOC is the location to use for the tree generated.
3228*/
43f6dfd3
RS
3229
3230struct c_expr
c2255bc4 3231parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3232{
3233 struct c_expr result;
3234
c9f9eb5d 3235 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3236 result.original_code = code;
6866c6e8
ILT
3237 result.original_type = NULL;
3238
59c0753d 3239 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3240 overflow_warning (loc, result.value);
59c0753d 3241
43f6dfd3
RS
3242 return result;
3243}
3244
3245/* This is the entry point used by the parser to build binary operators
3246 in the input. CODE, a tree_code, specifies the binary operator, and
3247 ARG1 and ARG2 are the operands. In addition to constructing the
3248 expression, we check for operands that were written with other binary
ba47d38d
AH
3249 operators in a way that is likely to confuse the user.
3250
3251 LOCATION is the location of the binary operator. */
edc7c4ec 3252
487a92fe 3253struct c_expr
ba47d38d
AH
3254parser_build_binary_op (location_t location, enum tree_code code,
3255 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3256{
487a92fe 3257 struct c_expr result;
400fbf9f 3258
487a92fe
JM
3259 enum tree_code code1 = arg1.original_code;
3260 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3261 tree type1 = (arg1.original_type
3262 ? arg1.original_type
3263 : TREE_TYPE (arg1.value));
3264 tree type2 = (arg2.original_type
3265 ? arg2.original_type
3266 : TREE_TYPE (arg2.value));
400fbf9f 3267
ba47d38d
AH
3268 result.value = build_binary_op (location, code,
3269 arg1.value, arg2.value, 1);
487a92fe 3270 result.original_code = code;
6866c6e8 3271 result.original_type = NULL;
58bf601b 3272
487a92fe
JM
3273 if (TREE_CODE (result.value) == ERROR_MARK)
3274 return result;
400fbf9f 3275
ba47d38d
AH
3276 if (location != UNKNOWN_LOCATION)
3277 protected_set_expr_location (result.value, location);
3278
400fbf9f 3279 /* Check for cases such as x+y<<z which users are likely
487a92fe 3280 to misinterpret. */
400fbf9f 3281 if (warn_parentheses)
5d9de0d0
PC
3282 warn_about_parentheses (input_location, code,
3283 code1, arg1.value, code2, arg2.value);
001af587 3284
ca409efd 3285 if (warn_logical_op)
a243fb4a 3286 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
ca409efd 3287 code1, arg1.value, code2, arg2.value);
63a08740 3288
e994a705
RS
3289 /* Warn about comparisons against string literals, with the exception
3290 of testing for equality or inequality of a string literal with NULL. */
3291 if (code == EQ_EXPR || code == NE_EXPR)
3292 {
3293 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3294 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3295 warning_at (location, OPT_Waddress,
3296 "comparison with string literal results in unspecified behavior");
e994a705
RS
3297 }
3298 else if (TREE_CODE_CLASS (code) == tcc_comparison
3299 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3300 warning_at (location, OPT_Waddress,
3301 "comparison with string literal results in unspecified behavior");
e994a705 3302
b8698a0f
L
3303 if (TREE_OVERFLOW_P (result.value)
3304 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3305 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3306 overflow_warning (location, result.value);
400fbf9f 3307
6866c6e8
ILT
3308 /* Warn about comparisons of different enum types. */
3309 if (warn_enum_compare
3310 && TREE_CODE_CLASS (code) == tcc_comparison
3311 && TREE_CODE (type1) == ENUMERAL_TYPE
3312 && TREE_CODE (type2) == ENUMERAL_TYPE
3313 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3314 warning_at (location, OPT_Wenum_compare,
3315 "comparison between %qT and %qT",
3316 type1, type2);
3317
400fbf9f
JW
3318 return result;
3319}
3e4093b6 3320\f
3e4093b6
RS
3321/* Return a tree for the difference of pointers OP0 and OP1.
3322 The resulting tree has type int. */
293c9fdd 3323
3e4093b6 3324static tree
db3927fb 3325pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3326{
3e4093b6 3327 tree restype = ptrdiff_type_node;
36c5e70a 3328 tree result, inttype;
400fbf9f 3329
36c5e70a
BE
3330 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3331 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3332 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3333 tree con0, con1, lit0, lit1;
3334 tree orig_op1 = op1;
400fbf9f 3335
36c5e70a
BE
3336 /* If the operands point into different address spaces, we need to
3337 explicitly convert them to pointers into the common address space
3338 before we can subtract the numerical address values. */
3339 if (as0 != as1)
3340 {
3341 addr_space_t as_common;
3342 tree common_type;
3343
3344 /* Determine the common superset address space. This is guaranteed
3345 to exist because the caller verified that comp_target_types
3346 returned non-zero. */
3347 if (!addr_space_superset (as0, as1, &as_common))
3348 gcc_unreachable ();
3349
3350 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3351 op0 = convert (common_type, op0);
3352 op1 = convert (common_type, op1);
3353 }
3354
3355 /* Determine integer type to perform computations in. This will usually
3356 be the same as the result type (ptrdiff_t), but may need to be a wider
3357 type if pointers for the address space are wider than ptrdiff_t. */
3358 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3359 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3360 else
3361 inttype = restype;
3362
3363
fcf73884 3364 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3365 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3366 "pointer of type %<void *%> used in subtraction");
3367 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3368 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3369 "pointer to a function used in subtraction");
400fbf9f 3370
3e4093b6
RS
3371 /* If the conversion to ptrdiff_type does anything like widening or
3372 converting a partial to an integral mode, we get a convert_expression
3373 that is in the way to do any simplifications.
3374 (fold-const.c doesn't know that the extra bits won't be needed.
3375 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3376 different mode in place.)
3377 So first try to find a common term here 'by hand'; we want to cover
3378 at least the cases that occur in legal static initializers. */
1043771b 3379 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3380 && (TYPE_PRECISION (TREE_TYPE (op0))
3381 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3382 con0 = TREE_OPERAND (op0, 0);
3383 else
3384 con0 = op0;
1043771b 3385 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3386 && (TYPE_PRECISION (TREE_TYPE (op1))
3387 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3388 con1 = TREE_OPERAND (op1, 0);
3389 else
3390 con1 = op1;
400fbf9f 3391
5de72424 3392 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3393 {
3394 lit0 = TREE_OPERAND (con0, 1);
3395 con0 = TREE_OPERAND (con0, 0);
3396 }
3397 else
3398 lit0 = integer_zero_node;
400fbf9f 3399
5de72424 3400 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3401 {
3e4093b6
RS
3402 lit1 = TREE_OPERAND (con1, 1);
3403 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3404 }
3405 else
3e4093b6
RS
3406 lit1 = integer_zero_node;
3407
3408 if (operand_equal_p (con0, con1, 0))
400fbf9f 3409 {
3e4093b6
RS
3410 op0 = lit0;
3411 op1 = lit1;
400fbf9f
JW
3412 }
3413
400fbf9f 3414
3e4093b6
RS
3415 /* First do the subtraction as integers;
3416 then drop through to build the divide operator.
3417 Do not do default conversions on the minus operator
3418 in case restype is a short type. */
400fbf9f 3419
db3927fb 3420 op0 = build_binary_op (loc,
36c5e70a
BE
3421 MINUS_EXPR, convert (inttype, op0),
3422 convert (inttype, op1), 0);
3e4093b6
RS
3423 /* This generates an error if op1 is pointer to incomplete type. */
3424 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3425 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3426
3e4093b6
RS
3427 /* This generates an error if op0 is pointer to incomplete type. */
3428 op1 = c_size_in_bytes (target_type);
400fbf9f 3429
3e4093b6 3430 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3431 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3432 op0, convert (inttype, op1));
3433
3434 /* Convert to final result type if necessary. */
3435 return convert (restype, result);
3e4093b6
RS
3436}
3437\f
3438/* Construct and perhaps optimize a tree representation
3439 for a unary operation. CODE, a tree_code, specifies the operation
3440 and XARG is the operand.
3441 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3442 the default promotions (such as from short to int).
3443 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3444 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3445 arrays to pointers in C99.
3446
3447 LOCATION is the location of the operator. */
400fbf9f 3448
3e4093b6 3449tree
c9f9eb5d
AH
3450build_unary_op (location_t location,
3451 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3452{
3453 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3454 tree arg = xarg;
3455 tree argtype = 0;
808d6eaa 3456 enum tree_code typecode;
3e4093b6 3457 tree val;
c9f9eb5d 3458 tree ret = error_mark_node;
8ce94e44 3459 tree eptype = NULL_TREE;
3e4093b6 3460 int noconvert = flag;
4de67c26 3461 const char *invalid_op_diag;
928c19bb
JM
3462 bool int_operands;
3463
3464 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3465 if (int_operands)
3466 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3467
808d6eaa
JM
3468 if (code != ADDR_EXPR)
3469 arg = require_complete_type (arg);
3470
3471 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3472 if (typecode == ERROR_MARK)
3473 return error_mark_node;
3474 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3475 typecode = INTEGER_TYPE;
6c36d76b 3476
4de67c26
JM
3477 if ((invalid_op_diag
3478 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3479 {
c9f9eb5d 3480 error_at (location, invalid_op_diag);
4de67c26
JM
3481 return error_mark_node;
3482 }
3483
8ce94e44
JM
3484 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3485 {
3486 eptype = TREE_TYPE (arg);
3487 arg = TREE_OPERAND (arg, 0);
3488 }
3489
3e4093b6
RS
3490 switch (code)
3491 {
3492 case CONVERT_EXPR:
3493 /* This is used for unary plus, because a CONVERT_EXPR
3494 is enough to prevent anybody from looking inside for
3495 associativity, but won't generate any code. */
3496 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3497 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3498 || typecode == VECTOR_TYPE))
400fbf9f 3499 {
c9f9eb5d 3500 error_at (location, "wrong type argument to unary plus");
3e4093b6 3501 return error_mark_node;
400fbf9f 3502 }
3e4093b6
RS
3503 else if (!noconvert)
3504 arg = default_conversion (arg);
db3927fb 3505 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3506 break;
3507
3e4093b6
RS
3508 case NEGATE_EXPR:
3509 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3510 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3511 || typecode == VECTOR_TYPE))
3512 {
c9f9eb5d 3513 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3514 return error_mark_node;
3515 }
3516 else if (!noconvert)
3517 arg = default_conversion (arg);
400fbf9f
JW
3518 break;
3519
3e4093b6 3520 case BIT_NOT_EXPR:
462643f0
AP
3521 /* ~ works on integer types and non float vectors. */
3522 if (typecode == INTEGER_TYPE
3523 || (typecode == VECTOR_TYPE
3524 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3525 {
3e4093b6
RS
3526 if (!noconvert)
3527 arg = default_conversion (arg);
03d5b1f5 3528 }
3e4093b6 3529 else if (typecode == COMPLEX_TYPE)
400fbf9f 3530 {
3e4093b6 3531 code = CONJ_EXPR;
c1771a20 3532 pedwarn (location, OPT_Wpedantic,
fcf73884 3533 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3534 if (!noconvert)
3535 arg = default_conversion (arg);
3536 }
3537 else
3538 {
c9f9eb5d 3539 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3540 return error_mark_node;
400fbf9f
JW
3541 }
3542 break;
3543
3e4093b6 3544 case ABS_EXPR:
11017cc7 3545 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3546 {
c9f9eb5d 3547 error_at (location, "wrong type argument to abs");
3e4093b6 3548 return error_mark_node;
400fbf9f 3549 }
3e4093b6
RS
3550 else if (!noconvert)
3551 arg = default_conversion (arg);
400fbf9f
JW
3552 break;
3553
3e4093b6
RS
3554 case CONJ_EXPR:
3555 /* Conjugating a real value is a no-op, but allow it anyway. */
3556 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3557 || typecode == COMPLEX_TYPE))
400fbf9f 3558 {
c9f9eb5d 3559 error_at (location, "wrong type argument to conjugation");
3e4093b6 3560 return error_mark_node;
400fbf9f 3561 }
3e4093b6
RS
3562 else if (!noconvert)
3563 arg = default_conversion (arg);
400fbf9f
JW
3564 break;
3565
3e4093b6 3566 case TRUTH_NOT_EXPR:
ab22c1fa 3567 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3568 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3569 && typecode != COMPLEX_TYPE)
400fbf9f 3570 {
c9f9eb5d
AH
3571 error_at (location,
3572 "wrong type argument to unary exclamation mark");
3e4093b6 3573 return error_mark_node;
400fbf9f 3574 }
a27d595d
JM
3575 if (int_operands)
3576 {
3577 arg = c_objc_common_truthvalue_conversion (location, xarg);
3578 arg = remove_c_maybe_const_expr (arg);
3579 }
3580 else
3581 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3582 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3583 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3584 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3585 location = EXPR_LOCATION (ret);
c9f9eb5d 3586 goto return_build_unary_op;
3e4093b6 3587
3e4093b6 3588 case REALPART_EXPR:
3e4093b6 3589 case IMAGPART_EXPR:
fb52b50a
NF
3590 ret = build_real_imag_expr (location, code, arg);
3591 if (ret == error_mark_node)
3592 return error_mark_node;
8ce94e44
JM
3593 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3594 eptype = TREE_TYPE (eptype);
c9f9eb5d 3595 goto return_build_unary_op;
3e4093b6
RS
3596
3597 case PREINCREMENT_EXPR:
3598 case POSTINCREMENT_EXPR:
3599 case PREDECREMENT_EXPR:
3600 case POSTDECREMENT_EXPR:
3e4093b6 3601
928c19bb
JM
3602 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3603 {
3604 tree inner = build_unary_op (location, code,
3605 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3606 if (inner == error_mark_node)
3607 return error_mark_node;
3608 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3609 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3610 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3611 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3612 goto return_build_unary_op;
3613 }
3614
925e8657
NP
3615 /* Complain about anything that is not a true lvalue. In
3616 Objective-C, skip this check for property_refs. */
f90e8e2e 3617 if (!objc_is_property_ref (arg)
7bd11157
TT
3618 && !lvalue_or_else (location,
3619 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3620 || code == POSTINCREMENT_EXPR)
3621 ? lv_increment
3622 : lv_decrement)))
928c19bb
JM
3623 return error_mark_node;
3624
09639a83
ILT
3625 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3626 {
3627 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3628 warning_at (location, OPT_Wc___compat,
3629 "increment of enumeration value is invalid in C++");
3630 else
3631 warning_at (location, OPT_Wc___compat,
3632 "decrement of enumeration value is invalid in C++");
3633 }
3634
928c19bb
JM
3635 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3636 arg = c_fully_fold (arg, false, NULL);
3637
3e4093b6
RS
3638 /* Increment or decrement the real part of the value,
3639 and don't change the imaginary part. */
3640 if (typecode == COMPLEX_TYPE)
400fbf9f 3641 {
3e4093b6
RS
3642 tree real, imag;
3643
c1771a20 3644 pedwarn (location, OPT_Wpedantic,
509c9d60 3645 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6
RS
3646
3647 arg = stabilize_reference (arg);
c9f9eb5d
AH
3648 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3649 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3650 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
a87db577
AP
3651 if (real == error_mark_node || imag == error_mark_node)
3652 return error_mark_node;
c9f9eb5d
AH
3653 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3654 real, imag);
3655 goto return_build_unary_op;
400fbf9f 3656 }
3e4093b6
RS
3657
3658 /* Report invalid types. */
3659
ab22c1fa 3660 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3661 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
400fbf9f 3662 {
3e4093b6 3663 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3664 error_at (location, "wrong type argument to increment");
c22cacf3 3665 else
c9f9eb5d 3666 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3667
3668 return error_mark_node;
400fbf9f 3669 }
400fbf9f 3670
3e4093b6
RS
3671 {
3672 tree inc;
400fbf9f 3673
3e4093b6
RS
3674 argtype = TREE_TYPE (arg);
3675
3676 /* Compute the increment. */
3677
3678 if (typecode == POINTER_TYPE)
3679 {
3680 /* If pointer target is an undefined struct,
3681 we just cannot know how to do the arithmetic. */
b70cef5d 3682 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
3683 {
3684 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d
AH
3685 error_at (location,
3686 "increment of pointer to unknown structure");
3e4093b6 3687 else
c9f9eb5d
AH
3688 error_at (location,
3689 "decrement of pointer to unknown structure");
3e4093b6 3690 }
b70cef5d
JJ
3691 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3692 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 3693 {
3e4093b6 3694 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 3695 pedwarn (location, OPT_Wpointer_arith,
fcf73884 3696 "wrong type argument to increment");
3e4093b6 3697 else
44d90fe1 3698 pedwarn (location, OPT_Wpointer_arith,
fcf73884 3699 "wrong type argument to decrement");
3e4093b6
RS
3700 }
3701
b70cef5d 3702 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 3703 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 3704 }
b70cef5d 3705 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
3706 {
3707 /* For signed fract types, we invert ++ to -- or
3708 -- to ++, and change inc from 1 to -1, because
3709 it is not possible to represent 1 in signed fract constants.
3710 For unsigned fract types, the result always overflows and
3711 we get an undefined (original) or the maximum value. */
3712 if (code == PREINCREMENT_EXPR)
3713 code = PREDECREMENT_EXPR;
3714 else if (code == PREDECREMENT_EXPR)
3715 code = PREINCREMENT_EXPR;
3716 else if (code == POSTINCREMENT_EXPR)
3717 code = POSTDECREMENT_EXPR;
3718 else /* code == POSTDECREMENT_EXPR */
3719 code = POSTINCREMENT_EXPR;
3720
3721 inc = integer_minus_one_node;
3722 inc = convert (argtype, inc);
3723 }
3e4093b6 3724 else
5be014d5
AP
3725 {
3726 inc = integer_one_node;
3727 inc = convert (argtype, inc);
3728 }
3e4093b6 3729
925e8657
NP
3730 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3731 need to ask Objective-C to build the increment or decrement
3732 expression for it. */
3733 if (objc_is_property_ref (arg))
f90e8e2e 3734 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
3735 arg, inc);
3736
3e4093b6 3737 /* Report a read-only lvalue. */
f37acdf9 3738 if (TYPE_READONLY (argtype))
953ff289
DN
3739 {
3740 readonly_error (arg,
3741 ((code == PREINCREMENT_EXPR
3742 || code == POSTINCREMENT_EXPR)
3743 ? lv_increment : lv_decrement));
3744 return error_mark_node;
3745 }
f37acdf9
JM
3746 else if (TREE_READONLY (arg))
3747 readonly_warning (arg,
3748 ((code == PREINCREMENT_EXPR
3749 || code == POSTINCREMENT_EXPR)
3750 ? lv_increment : lv_decrement));
3e4093b6
RS
3751
3752 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3753 val = boolean_increment (code, arg);
3754 else
53fb4de3 3755 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 3756 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 3757 if (TREE_CODE (val) != code)
6de9cd9a 3758 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
3759 ret = val;
3760 goto return_build_unary_op;
3e4093b6
RS
3761 }
3762
3763 case ADDR_EXPR:
3764 /* Note that this operation never does default_conversion. */
3765
2b4b7036
JM
3766 /* The operand of unary '&' must be an lvalue (which excludes
3767 expressions of type void), or, in C99, the result of a [] or
3768 unary '*' operator. */
3769 if (VOID_TYPE_P (TREE_TYPE (arg))
3770 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3771 && (TREE_CODE (arg) != INDIRECT_REF
3772 || !flag_isoc99))
3773 pedwarn (location, 0, "taking address of expression of type %<void%>");
3774
3e4093b6
RS
3775 /* Let &* cancel out to simplify resulting code. */
3776 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 3777 {
3e4093b6
RS
3778 /* Don't let this be an lvalue. */
3779 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 3780 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
3781 ret = TREE_OPERAND (arg, 0);
3782 goto return_build_unary_op;
400fbf9f 3783 }
1eb8759b 3784
7c672dfc 3785 /* For &x[y], return x+y */
3e4093b6 3786 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 3787 {
f2a71bbc
JM
3788 tree op0 = TREE_OPERAND (arg, 0);
3789 if (!c_mark_addressable (op0))
3e4093b6 3790 return error_mark_node;
1eb8759b 3791 }
1eb8759b 3792
3e4093b6
RS
3793 /* Anything not already handled and not a true memory reference
3794 or a non-lvalue array is an error. */
3795 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 3796 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 3797 return error_mark_node;
b6a10c9f 3798
928c19bb
JM
3799 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3800 folding later. */
3801 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3802 {
3803 tree inner = build_unary_op (location, code,
3804 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3805 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3806 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3807 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3808 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3809 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3810 goto return_build_unary_op;
3811 }
3812
3e4093b6
RS
3813 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3814 argtype = TREE_TYPE (arg);
400fbf9f 3815
3e4093b6 3816 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 3817 to which the address will point. This is only needed
f2c1da78 3818 for function types. */
6615c446 3819 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
3820 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3821 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
3822 {
3823 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3824 int quals = orig_quals;
3825
3826 if (TREE_READONLY (arg))
3827 quals |= TYPE_QUAL_CONST;
3828 if (TREE_THIS_VOLATILE (arg))
3829 quals |= TYPE_QUAL_VOLATILE;
3830
f2c1da78
JM
3831 argtype = c_build_qualified_type (argtype, quals);
3832 }
400fbf9f 3833
3e4093b6
RS
3834 if (!c_mark_addressable (arg))
3835 return error_mark_node;
400fbf9f 3836
abb54d14
JM
3837 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3838 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 3839
5cc200fc 3840 argtype = build_pointer_type (argtype);
5e55f99d
RH
3841
3842 /* ??? Cope with user tricks that amount to offsetof. Delete this
3843 when we have proper support for integer constant expressions. */
3844 val = get_base_address (arg);
3845 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
3846 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3847 {
cf9e9959 3848 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 3849 goto return_build_unary_op;
3aa2ddb8 3850 }
5e55f99d 3851
5cc200fc 3852 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 3853
c9f9eb5d
AH
3854 ret = val;
3855 goto return_build_unary_op;
400fbf9f 3856
3e4093b6 3857 default:
1344f9a3 3858 gcc_unreachable ();
3e4093b6 3859 }
400fbf9f 3860
3e4093b6
RS
3861 if (argtype == 0)
3862 argtype = TREE_TYPE (arg);
928c19bb
JM
3863 if (TREE_CODE (arg) == INTEGER_CST)
3864 ret = (require_constant_value
db3927fb
AH
3865 ? fold_build1_initializer_loc (location, code, argtype, arg)
3866 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
3867 else
3868 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
3869 return_build_unary_op:
3870 gcc_assert (ret != error_mark_node);
928c19bb
JM
3871 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3872 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3873 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3874 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3875 ret = note_integer_operands (ret);
8ce94e44
JM
3876 if (eptype)
3877 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
3878 protected_set_expr_location (ret, location);
3879 return ret;
3e4093b6 3880}
400fbf9f 3881
3e4093b6
RS
3882/* Return nonzero if REF is an lvalue valid for this language.
3883 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 3884 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 3885
1e4ae551 3886bool
58f9752a 3887lvalue_p (const_tree ref)
3e4093b6 3888{
58f9752a 3889 const enum tree_code code = TREE_CODE (ref);
400fbf9f 3890
3e4093b6
RS
3891 switch (code)
3892 {
3893 case REALPART_EXPR:
3894 case IMAGPART_EXPR:
3895 case COMPONENT_REF:
3896 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 3897
928c19bb
JM
3898 case C_MAYBE_CONST_EXPR:
3899 return lvalue_p (TREE_OPERAND (ref, 1));
3900
3e4093b6
RS
3901 case COMPOUND_LITERAL_EXPR:
3902 case STRING_CST:
3903 return 1;
400fbf9f 3904
3e4093b6
RS
3905 case INDIRECT_REF:
3906 case ARRAY_REF:
36536d79 3907 case ARRAY_NOTATION_REF:
3e4093b6
RS
3908 case VAR_DECL:
3909 case PARM_DECL:
3910 case RESULT_DECL:
3911 case ERROR_MARK:
3912 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3913 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 3914
3e4093b6 3915 case BIND_EXPR:
3e4093b6 3916 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 3917
3e4093b6
RS
3918 default:
3919 return 0;
3920 }
3921}
400fbf9f 3922\f
f37acdf9
JM
3923/* Give a warning for storing in something that is read-only in GCC
3924 terms but not const in ISO C terms. */
3925
3926static void
3927readonly_warning (tree arg, enum lvalue_use use)
3928{
3929 switch (use)
3930 {
3931 case lv_assign:
3932 warning (0, "assignment of read-only location %qE", arg);
3933 break;
3934 case lv_increment:
3935 warning (0, "increment of read-only location %qE", arg);
3936 break;
3937 case lv_decrement:
3938 warning (0, "decrement of read-only location %qE", arg);
3939 break;
3940 default:
3941 gcc_unreachable ();
3942 }
3943 return;
3944}
3945
37dc0d8d
JM
3946
3947/* Return nonzero if REF is an lvalue valid for this language;
3948 otherwise, print an error message and return zero. USE says
7bd11157
TT
3949 how the lvalue is being used and so selects the error message.
3950 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
3951
3952static int
7bd11157 3953lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
3954{
3955 int win = lvalue_p (ref);
3956
3957 if (!win)
7bd11157 3958 lvalue_error (loc, use);
37dc0d8d
JM
3959
3960 return win;
3961}
3e4093b6
RS
3962\f
3963/* Mark EXP saying that we need to be able to take the
3964 address of it; it should not be allocated in a register.
3965 Returns true if successful. */
54c93c30 3966
3e4093b6
RS
3967bool
3968c_mark_addressable (tree exp)
400fbf9f 3969{
3e4093b6 3970 tree x = exp;
95602da1 3971
3e4093b6
RS
3972 while (1)
3973 switch (TREE_CODE (x))
3974 {
3975 case COMPONENT_REF:
3976 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3977 {
0039fa55
AN
3978 error
3979 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
3980 return false;
3981 }
95602da1 3982
3e4093b6 3983 /* ... fall through ... */
95602da1 3984
3e4093b6
RS
3985 case ADDR_EXPR:
3986 case ARRAY_REF:
3987 case REALPART_EXPR:
3988 case IMAGPART_EXPR:
3989 x = TREE_OPERAND (x, 0);
3990 break;
95602da1 3991
3e4093b6
RS
3992 case COMPOUND_LITERAL_EXPR:
3993 case CONSTRUCTOR:
3994 TREE_ADDRESSABLE (x) = 1;
3995 return true;
95602da1 3996
3e4093b6
RS
3997 case VAR_DECL:
3998 case CONST_DECL:
3999 case PARM_DECL:
4000 case RESULT_DECL:
5baeaac0 4001 if (C_DECL_REGISTER (x)
3e4093b6
RS
4002 && DECL_NONLOCAL (x))
4003 {
e697b20f 4004 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4005 {
0039fa55
AN
4006 error
4007 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4008 return false;
4009 }
509c9d60 4010 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4011 }
5baeaac0 4012 else if (C_DECL_REGISTER (x))
3e4093b6 4013 {
e697b20f 4014 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4015 error ("address of global register variable %qD requested", x);
4016 else
4017 error ("address of register variable %qD requested", x);
4018 return false;
3e4093b6 4019 }
400fbf9f 4020
3e4093b6
RS
4021 /* drops in */
4022 case FUNCTION_DECL:
4023 TREE_ADDRESSABLE (x) = 1;
4024 /* drops out */
4025 default:
4026 return true;
4027 }
4028}
4029\f
2d2e923f
MLI
4030/* Convert EXPR to TYPE, warning about conversion problems with
4031 constants. SEMANTIC_TYPE is the type this conversion would use
4032 without excess precision. If SEMANTIC_TYPE is NULL, this function
4033 is equivalent to convert_and_check. This function is a wrapper that
4034 handles conversions that may be different than
4035 the usual ones because of excess precision. */
4036
4037static tree
4038ep_convert_and_check (tree type, tree expr, tree semantic_type)
4039{
4040 if (TREE_TYPE (expr) == type)
4041 return expr;
4042
4043 if (!semantic_type)
4044 return convert_and_check (type, expr);
4045
4046 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4047 && TREE_TYPE (expr) != semantic_type)
4048 {
4049 /* For integers, we need to check the real conversion, not
4050 the conversion to the excess precision type. */
4051 expr = convert_and_check (semantic_type, expr);
4052 }
4053 /* Result type is the excess precision type, which should be
4054 large enough, so do not check. */
4055 return convert (type, expr);
4056}
4057
928c19bb
JM
4058/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4059 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4060 if folded to an integer constant then the unselected half may
4061 contain arbitrary operations not normally permitted in constant
c2255bc4 4062 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4063
4064tree
744aa42f 4065build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4066 tree op1, tree op1_original_type, tree op2,
4067 tree op2_original_type)
400fbf9f 4068{
3e4093b6
RS
4069 tree type1;
4070 tree type2;
4071 enum tree_code code1;
4072 enum tree_code code2;
4073 tree result_type = NULL;
2d2e923f 4074 tree semantic_result_type = NULL;
3e4093b6 4075 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4076 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4077 bool ifexp_int_operands;
928c19bb 4078 tree ret;
400fbf9f 4079
4d84fe7c
JM
4080 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4081 if (op1_int_operands)
4082 op1 = remove_c_maybe_const_expr (op1);
4083 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4084 if (op2_int_operands)
4085 op2 = remove_c_maybe_const_expr (op2);
4086 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4087 if (ifexp_int_operands)
4088 ifexp = remove_c_maybe_const_expr (ifexp);
4089
3e4093b6
RS
4090 /* Promote both alternatives. */
4091
4092 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4093 op1 = default_conversion (op1);
4094 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4095 op2 = default_conversion (op2);
4096
4097 if (TREE_CODE (ifexp) == ERROR_MARK
4098 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4099 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4100 return error_mark_node;
400fbf9f 4101
3e4093b6
RS
4102 type1 = TREE_TYPE (op1);
4103 code1 = TREE_CODE (type1);
4104 type2 = TREE_TYPE (op2);
4105 code2 = TREE_CODE (type2);
4106
b1adf557
JM
4107 /* C90 does not permit non-lvalue arrays in conditional expressions.
4108 In C99 they will be pointers by now. */
4109 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4110 {
744aa42f 4111 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4112 return error_mark_node;
4113 }
4114
8ce94e44
JM
4115 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4116 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4117 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4118 || code1 == COMPLEX_TYPE)
4119 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4120 || code2 == COMPLEX_TYPE))
4121 {
2d2e923f 4122 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4123 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4124 {
4125 op1 = TREE_OPERAND (op1, 0);
4126 type1 = TREE_TYPE (op1);
4127 gcc_assert (TREE_CODE (type1) == code1);
4128 }
4129 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4130 {
4131 op2 = TREE_OPERAND (op2, 0);
4132 type2 = TREE_TYPE (op2);
4133 gcc_assert (TREE_CODE (type2) == code2);
4134 }
4135 }
4136
d130ae11
ILT
4137 if (warn_cxx_compat)
4138 {
4139 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4140 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4141
4142 if (TREE_CODE (t1) == ENUMERAL_TYPE
4143 && TREE_CODE (t2) == ENUMERAL_TYPE
4144 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4145 warning_at (colon_loc, OPT_Wc___compat,
4146 ("different enum types in conditional is "
4147 "invalid in C++: %qT vs %qT"),
4148 t1, t2);
4149 }
4150
3e4093b6
RS
4151 /* Quickly detect the usual case where op1 and op2 have the same type
4152 after promotion. */
4153 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4154 {
3e4093b6
RS
4155 if (type1 == type2)
4156 result_type = type1;
4157 else
4158 result_type = TYPE_MAIN_VARIANT (type1);
4159 }
4160 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4161 || code1 == COMPLEX_TYPE)
4162 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4163 || code2 == COMPLEX_TYPE))
3e4093b6 4164 {
ccf7f880 4165 result_type = c_common_type (type1, type2);
0a0b3574
MM
4166 do_warn_double_promotion (result_type, type1, type2,
4167 "implicit conversion from %qT to %qT to "
4168 "match other result of conditional",
4169 colon_loc);
400fbf9f 4170
3e4093b6
RS
4171 /* If -Wsign-compare, warn here if type1 and type2 have
4172 different signedness. We'll promote the signed to unsigned
4173 and later code won't know it used to be different.
4174 Do this check on the original types, so that explicit casts
4175 will be considered, but default promotions won't. */
7d882b83 4176 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4177 {
8df83eae
RK
4178 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4179 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4180
3e4093b6
RS
4181 if (unsigned_op1 ^ unsigned_op2)
4182 {
6ac01510
ILT
4183 bool ovf;
4184
3e4093b6
RS
4185 /* Do not warn if the result type is signed, since the
4186 signed type will only be chosen if it can represent
4187 all the values of the unsigned type. */
3f75a254 4188 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4189 /* OK */;
3e4093b6 4190 else
928c19bb
JM
4191 {
4192 bool op1_maybe_const = true;
4193 bool op2_maybe_const = true;
4194
4195 /* Do not warn if the signed quantity is an
4196 unsuffixed integer literal (or some static
4197 constant expression involving such literals) and
4198 it is non-negative. This warning requires the
4199 operands to be folded for best results, so do
4200 that folding in this case even without
4201 warn_sign_compare to avoid warning options
4202 possibly affecting code generation. */
f5178456
RS
4203 c_inhibit_evaluation_warnings
4204 += (ifexp == truthvalue_false_node);
928c19bb
JM
4205 op1 = c_fully_fold (op1, require_constant_value,
4206 &op1_maybe_const);
f5178456
RS
4207 c_inhibit_evaluation_warnings
4208 -= (ifexp == truthvalue_false_node);
4209
4210 c_inhibit_evaluation_warnings
4211 += (ifexp == truthvalue_true_node);
928c19bb
JM
4212 op2 = c_fully_fold (op2, require_constant_value,
4213 &op2_maybe_const);
f5178456
RS
4214 c_inhibit_evaluation_warnings
4215 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4216
4217 if (warn_sign_compare)
4218 {
4219 if ((unsigned_op2
4220 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4221 || (unsigned_op1
4222 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4223 /* OK */;
4224 else
744aa42f
ILT
4225 warning_at (colon_loc, OPT_Wsign_compare,
4226 ("signed and unsigned type in "
4227 "conditional expression"));
928c19bb
JM
4228 }
4229 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4230 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4231 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4232 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4233 }
3e4093b6
RS
4234 }
4235 }
4236 }
4237 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4238 {
fcf73884 4239 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4240 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4241 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4242 result_type = void_type_node;
4243 }
4244 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4245 {
36c5e70a
BE
4246 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4247 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4248 addr_space_t as_common;
4249
744aa42f 4250 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4251 result_type = common_pointer_type (type1, type2);
6aa3c60d 4252 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4253 result_type = type2;
6aa3c60d 4254 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4255 result_type = type1;
4256 else if (!addr_space_superset (as1, as2, &as_common))
4257 {
4258 error_at (colon_loc, "pointers to disjoint address spaces "
4259 "used in conditional expression");
4260 return error_mark_node;
4261 }
3e4093b6 4262 else if (VOID_TYPE_P (TREE_TYPE (type1)))
34a80643 4263 {
fcf73884 4264 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4265 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4266 "ISO C forbids conditional expr between "
bda67431 4267 "%<void *%> and function pointer");
3e4093b6
RS
4268 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4269 TREE_TYPE (type2)));
34a80643 4270 }
3e4093b6 4271 else if (VOID_TYPE_P (TREE_TYPE (type2)))
1c2a9b35 4272 {
fcf73884 4273 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4274 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4275 "ISO C forbids conditional expr between "
bda67431 4276 "%<void *%> and function pointer");
3e4093b6
RS
4277 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4278 TREE_TYPE (type1)));
1c2a9b35 4279 }
b581b85b
NP
4280 /* Objective-C pointer comparisons are a bit more lenient. */
4281 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4282 result_type = objc_common_type (type1, type2);
34a80643 4283 else
ab87f8c8 4284 {
36c5e70a
BE
4285 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4286
b581b85b
NP
4287 pedwarn (colon_loc, 0,
4288 "pointer type mismatch in conditional expression");
36c5e70a
BE
4289 result_type = build_pointer_type
4290 (build_qualified_type (void_type_node, qual));
ab87f8c8 4291 }
3e4093b6
RS
4292 }
4293 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4294 {
6aa3c60d 4295 if (!null_pointer_constant_p (orig_op2))
744aa42f 4296 pedwarn (colon_loc, 0,
509c9d60 4297 "pointer/integer type mismatch in conditional expression");
3e4093b6 4298 else
ab87f8c8 4299 {
3e4093b6 4300 op2 = null_pointer_node;
ab87f8c8 4301 }
3e4093b6
RS
4302 result_type = type1;
4303 }
4304 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4305 {
6aa3c60d 4306 if (!null_pointer_constant_p (orig_op1))
744aa42f 4307 pedwarn (colon_loc, 0,
509c9d60 4308 "pointer/integer type mismatch in conditional expression");
3e4093b6 4309 else
ab87f8c8 4310 {
3e4093b6 4311 op1 = null_pointer_node;
ab87f8c8 4312 }
3e4093b6
RS
4313 result_type = type2;
4314 }
1c2a9b35 4315
3e4093b6
RS
4316 if (!result_type)
4317 {
4318 if (flag_cond_mismatch)
4319 result_type = void_type_node;
4320 else
400fbf9f 4321 {
c2255bc4 4322 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4323 return error_mark_node;
400fbf9f 4324 }
3e4093b6 4325 }
400fbf9f 4326
3e4093b6
RS
4327 /* Merge const and volatile flags of the incoming types. */
4328 result_type
4329 = build_type_variant (result_type,
afbd0665
AS
4330 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4331 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4332
2d2e923f
MLI
4333 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4334 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
b6a10c9f 4335
928c19bb
JM
4336 if (ifexp_bcp && ifexp == truthvalue_true_node)
4337 {
4338 op2_int_operands = true;
4339 op1 = c_fully_fold (op1, require_constant_value, NULL);
4340 }
4341 if (ifexp_bcp && ifexp == truthvalue_false_node)
4342 {
4343 op1_int_operands = true;
4344 op2 = c_fully_fold (op2, require_constant_value, NULL);
4345 }
4d84fe7c 4346 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4347 && op1_int_operands
4348 && op2_int_operands);
4349 if (int_operands)
4350 {
4351 int_const = ((ifexp == truthvalue_true_node
4352 && TREE_CODE (orig_op1) == INTEGER_CST
4353 && !TREE_OVERFLOW (orig_op1))
4354 || (ifexp == truthvalue_false_node
4355 && TREE_CODE (orig_op2) == INTEGER_CST
4356 && !TREE_OVERFLOW (orig_op2)));
4357 }
4358 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4359 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4360 else
4361 {
01c7ccbb
JM
4362 if (int_operands)
4363 {
4364 op1 = remove_c_maybe_const_expr (op1);
4365 op2 = remove_c_maybe_const_expr (op2);
4366 }
928c19bb
JM
4367 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4368 if (int_operands)
4369 ret = note_integer_operands (ret);
4370 }
2d2e923f
MLI
4371 if (semantic_result_type)
4372 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4373
c2255bc4 4374 protected_set_expr_location (ret, colon_loc);
928c19bb 4375 return ret;
3e4093b6
RS
4376}
4377\f
487a92fe 4378/* Return a compound expression that performs two expressions and
c2255bc4
AH
4379 returns the value of the second of them.
4380
4381 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4382
3e4093b6 4383tree
c2255bc4 4384build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4385{
4d84fe7c 4386 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4387 tree eptype = NULL_TREE;
928c19bb
JM
4388 tree ret;
4389
939b37da
BI
4390 if (flag_enable_cilkplus
4391 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4392 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4393 {
4394 error_at (loc,
4395 "spawned function call cannot be part of a comma expression");
4396 return error_mark_node;
4397 }
4d84fe7c
JM
4398 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4399 if (expr1_int_operands)
4400 expr1 = remove_c_maybe_const_expr (expr1);
4401 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4402 if (expr2_int_operands)
4403 expr2 = remove_c_maybe_const_expr (expr2);
4404
8ce94e44
JM
4405 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4406 expr1 = TREE_OPERAND (expr1, 0);
4407 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4408 {
4409 eptype = TREE_TYPE (expr2);
4410 expr2 = TREE_OPERAND (expr2, 0);
4411 }
4412
3f75a254 4413 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4414 {
4415 /* The left-hand operand of a comma expression is like an expression
c5409249 4416 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4417 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4418 if (warn_unused_value)
47aecf47 4419 {
e14a6540 4420 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4421 && CONVERT_EXPR_P (expr1))
47aecf47 4422 ; /* (void) a, b */
e14a6540
JM
4423 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4424 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4425 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4426 ; /* (void) a, (void) b, c */
4427 else
b8698a0f 4428 warning_at (loc, OPT_Wunused_value,
c2255bc4 4429 "left-hand operand of comma expression has no effect");
47aecf47 4430 }
3e4093b6 4431 }
400fbf9f 4432
3e4093b6
RS
4433 /* With -Wunused, we should also warn if the left-hand operand does have
4434 side-effects, but computes a value which is not used. For example, in
4435 `foo() + bar(), baz()' the result of the `+' operator is not used,
4436 so we should issue a warning. */
4437 else if (warn_unused_value)
c2255bc4 4438 warn_if_unused_value (expr1, loc);
400fbf9f 4439
e63d6886
AP
4440 if (expr2 == error_mark_node)
4441 return error_mark_node;
4442
928c19bb
JM
4443 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4444
4445 if (flag_isoc99
4d84fe7c
JM
4446 && expr1_int_operands
4447 && expr2_int_operands)
928c19bb
JM
4448 ret = note_integer_operands (ret);
4449
8ce94e44
JM
4450 if (eptype)
4451 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4452
c2255bc4 4453 protected_set_expr_location (ret, loc);
928c19bb 4454 return ret;
3e4093b6 4455}
400fbf9f 4456
67165eb3
ILT
4457/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4458 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4459 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4460 of the cast. -Wcast-qual appeared on the command line. Named
4461 address space qualifiers are not handled here, because they result
4462 in different warnings. */
67165eb3
ILT
4463
4464static void
2ee3cb35 4465handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4466{
4467 tree in_type = type;
4468 tree in_otype = otype;
4469 int added = 0;
4470 int discarded = 0;
4471 bool is_const;
4472
4473 /* Check that the qualifiers on IN_TYPE are a superset of the
4474 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4475 nodes is uninteresting and we stop as soon as we hit a
4476 non-POINTER_TYPE node on either type. */
4477 do
4478 {
4479 in_otype = TREE_TYPE (in_otype);
4480 in_type = TREE_TYPE (in_type);
4481
4482 /* GNU C allows cv-qualified function types. 'const' means the
4483 function is very pure, 'volatile' means it can't return. We
4484 need to warn when such qualifiers are added, not when they're
4485 taken away. */
4486 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4487 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4488 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4489 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4490 else
36c5e70a
BE
4491 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4492 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4493 }
4494 while (TREE_CODE (in_type) == POINTER_TYPE
4495 && TREE_CODE (in_otype) == POINTER_TYPE);
4496
4497 if (added)
2ee3cb35
MLI
4498 warning_at (loc, OPT_Wcast_qual,
4499 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4500
4501 if (discarded)
4502 /* There are qualifiers present in IN_OTYPE that are not present
4503 in IN_TYPE. */
2ee3cb35
MLI
4504 warning_at (loc, OPT_Wcast_qual,
4505 "cast discards %q#v qualifier from pointer target type",
4506 discarded);
67165eb3
ILT
4507
4508 if (added || discarded)
4509 return;
4510
4511 /* A cast from **T to const **T is unsafe, because it can cause a
4512 const value to be changed with no additional warning. We only
4513 issue this warning if T is the same on both sides, and we only
4514 issue the warning if there are the same number of pointers on
4515 both sides, as otherwise the cast is clearly unsafe anyhow. A
4516 cast is unsafe when a qualifier is added at one level and const
4517 is not present at all outer levels.
4518
4519 To issue this warning, we check at each level whether the cast
4520 adds new qualifiers not already seen. We don't need to special
4521 case function types, as they won't have the same
4522 TYPE_MAIN_VARIANT. */
4523
4524 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4525 return;
4526 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4527 return;
4528
4529 in_type = type;
4530 in_otype = otype;
4531 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4532 do
4533 {
4534 in_type = TREE_TYPE (in_type);
4535 in_otype = TREE_TYPE (in_otype);
4536 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4537 && !is_const)
4538 {
2ee3cb35
MLI
4539 warning_at (loc, OPT_Wcast_qual,
4540 "to be safe all intermediate pointers in cast from "
4541 "%qT to %qT must be %<const%> qualified",
4542 otype, type);
67165eb3
ILT
4543 break;
4544 }
4545 if (is_const)
4546 is_const = TYPE_READONLY (in_type);
4547 }
4548 while (TREE_CODE (in_type) == POINTER_TYPE);
4549}
4550
b8698a0f 4551/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4552 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4553
3e4093b6 4554tree
c2255bc4 4555build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4556{
8ce94e44
JM
4557 tree value;
4558
4559 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4560 expr = TREE_OPERAND (expr, 0);
4561
4562 value = expr;
400fbf9f 4563
3e4093b6
RS
4564 if (type == error_mark_node || expr == error_mark_node)
4565 return error_mark_node;
400fbf9f 4566
3e4093b6
RS
4567 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4568 only in <protocol> qualifications. But when constructing cast expressions,
4569 the protocols do matter and must be kept around. */
700686fa
ZL
4570 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4571 return build1 (NOP_EXPR, type, expr);
4572
4573 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4574
3e4093b6
RS
4575 if (TREE_CODE (type) == ARRAY_TYPE)
4576 {
c2255bc4 4577 error_at (loc, "cast specifies array type");
3e4093b6
RS
4578 return error_mark_node;
4579 }
400fbf9f 4580
3e4093b6
RS
4581 if (TREE_CODE (type) == FUNCTION_TYPE)
4582 {
c2255bc4 4583 error_at (loc, "cast specifies function type");
3e4093b6
RS
4584 return error_mark_node;
4585 }
400fbf9f 4586
808d6eaa
JM
4587 if (!VOID_TYPE_P (type))
4588 {
4589 value = require_complete_type (value);
4590 if (value == error_mark_node)
4591 return error_mark_node;
4592 }
4593
3e4093b6
RS
4594 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4595 {
fcf73884
MLI
4596 if (TREE_CODE (type) == RECORD_TYPE
4597 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4598 pedwarn (loc, OPT_Wpedantic,
fcf73884 4599 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4600 }
4601 else if (TREE_CODE (type) == UNION_TYPE)
4602 {
4603 tree field;
400fbf9f 4604
910ad8de 4605 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4606 if (TREE_TYPE (field) != error_mark_node
4607 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4608 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4609 break;
4610
4611 if (field)
400fbf9f 4612 {
3e4093b6 4613 tree t;
e616f54d 4614 bool maybe_const = true;
3e4093b6 4615
c1771a20 4616 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4617 t = c_fully_fold (value, false, &maybe_const);
4618 t = build_constructor_single (type, field, t);
4619 if (!maybe_const)
4620 t = c_wrap_maybe_const (t, true);
4621 t = digest_init (loc, type, t,
bbbbb16a 4622 NULL_TREE, false, true, 0);
3e4093b6
RS
4623 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4624 return t;
400fbf9f 4625 }
c2255bc4 4626 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4627 return error_mark_node;
4628 }
4629 else
4630 {
4631 tree otype, ovalue;
400fbf9f 4632
3e4093b6 4633 if (type == void_type_node)
c2255bc4
AH
4634 {
4635 tree t = build1 (CONVERT_EXPR, type, value);
4636 SET_EXPR_LOCATION (t, loc);
4637 return t;
4638 }
400fbf9f 4639
3e4093b6 4640 otype = TREE_TYPE (value);
400fbf9f 4641
3e4093b6 4642 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
4643 if (warn_cast_qual
4644 && TREE_CODE (type) == POINTER_TYPE
4645 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 4646 handle_warn_cast_qual (loc, type, otype);
400fbf9f 4647
36c5e70a
BE
4648 /* Warn about conversions between pointers to disjoint
4649 address spaces. */
4650 if (TREE_CODE (type) == POINTER_TYPE
4651 && TREE_CODE (otype) == POINTER_TYPE
4652 && !null_pointer_constant_p (value))
4653 {
4654 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4655 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4656 addr_space_t as_common;
4657
4658 if (!addr_space_superset (as_to, as_from, &as_common))
4659 {
4660 if (ADDR_SPACE_GENERIC_P (as_from))
4661 warning_at (loc, 0, "cast to %s address space pointer "
4662 "from disjoint generic address space pointer",
4663 c_addr_space_name (as_to));
4664
4665 else if (ADDR_SPACE_GENERIC_P (as_to))
4666 warning_at (loc, 0, "cast to generic address space pointer "
4667 "from disjoint %s address space pointer",
4668 c_addr_space_name (as_from));
4669
4670 else
4671 warning_at (loc, 0, "cast to %s address space pointer "
4672 "from disjoint %s address space pointer",
4673 c_addr_space_name (as_to),
4674 c_addr_space_name (as_from));
4675 }
4676 }
4677
3e4093b6 4678 /* Warn about possible alignment problems. */
3176a0c2 4679 if (STRICT_ALIGNMENT
3e4093b6
RS
4680 && TREE_CODE (type) == POINTER_TYPE
4681 && TREE_CODE (otype) == POINTER_TYPE
4682 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4683 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4684 /* Don't warn about opaque types, where the actual alignment
4685 restriction is unknown. */
4686 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4687 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4688 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4689 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
4690 warning_at (loc, OPT_Wcast_align,
4691 "cast increases required alignment of target type");
e9a25f70 4692
3176a0c2 4693 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 4694 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 4695 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
4696 /* Unlike conversion of integers to pointers, where the
4697 warning is disabled for converting constants because
4698 of cases such as SIG_*, warn about converting constant
4699 pointers to integers. In some cases it may cause unwanted
8d1c7aef 4700 sign extension, and a warning is appropriate. */
c2255bc4
AH
4701 warning_at (loc, OPT_Wpointer_to_int_cast,
4702 "cast from pointer to integer of different size");
400fbf9f 4703
3176a0c2 4704 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 4705 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
4706 warning_at (loc, OPT_Wbad_function_cast,
4707 "cast from function call of type %qT "
4708 "to non-matching type %qT", otype, type);
400fbf9f 4709
3176a0c2 4710 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
4711 && TREE_CODE (otype) == INTEGER_TYPE
4712 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4713 /* Don't warn about converting any constant. */
4714 && !TREE_CONSTANT (value))
c2255bc4
AH
4715 warning_at (loc,
4716 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4717 "of different size");
400fbf9f 4718
79bedddc
SR
4719 if (warn_strict_aliasing <= 2)
4720 strict_aliasing_warning (otype, type, expr);
400fbf9f 4721
3897f229
JM
4722 /* If pedantic, warn for conversions between function and object
4723 pointer types, except for converting a null pointer constant
4724 to function pointer type. */
4725 if (pedantic
4726 && TREE_CODE (type) == POINTER_TYPE
4727 && TREE_CODE (otype) == POINTER_TYPE
4728 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4729 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 4730 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 4731 "conversion of function pointer to object pointer type");
3897f229
JM
4732
4733 if (pedantic
4734 && TREE_CODE (type) == POINTER_TYPE
4735 && TREE_CODE (otype) == POINTER_TYPE
4736 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4737 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 4738 && !null_pointer_constant_p (value))
c1771a20 4739 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 4740 "conversion of object pointer to function pointer type");
3897f229 4741
3e4093b6 4742 ovalue = value;
3e4093b6 4743 value = convert (type, value);
400fbf9f 4744
3e4093b6 4745 /* Ignore any integer overflow caused by the cast. */
928c19bb 4746 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 4747 {
8bcd6380 4748 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 4749 {
8bcd6380
RS
4750 if (!TREE_OVERFLOW (value))
4751 {
4752 /* Avoid clobbering a shared constant. */
4753 value = copy_node (value);
4754 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4755 }
6414bad6 4756 }
8bcd6380 4757 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
4758 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4759 value = build_int_cst_wide (TREE_TYPE (value),
4760 TREE_INT_CST_LOW (value),
4761 TREE_INT_CST_HIGH (value));
3e4093b6
RS
4762 }
4763 }
400fbf9f 4764
53cd18ec
JM
4765 /* Don't let a cast be an lvalue. */
4766 if (value == expr)
db3927fb 4767 value = non_lvalue_loc (loc, value);
e9a25f70 4768
928c19bb
JM
4769 /* Don't allow the results of casting to floating-point or complex
4770 types be confused with actual constants, or casts involving
4771 integer and pointer types other than direct integer-to-integer
4772 and integer-to-pointer be confused with integer constant
4773 expressions and null pointer constants. */
4774 if (TREE_CODE (value) == REAL_CST
4775 || TREE_CODE (value) == COMPLEX_CST
4776 || (TREE_CODE (value) == INTEGER_CST
4777 && !((TREE_CODE (expr) == INTEGER_CST
4778 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4779 || TREE_CODE (expr) == REAL_CST
4780 || TREE_CODE (expr) == COMPLEX_CST)))
4781 value = build1 (NOP_EXPR, type, value);
4782
c2255bc4
AH
4783 if (CAN_HAVE_LOCATION_P (value))
4784 SET_EXPR_LOCATION (value, loc);
3e4093b6 4785 return value;
400fbf9f
JW
4786}
4787
c2255bc4
AH
4788/* Interpret a cast of expression EXPR to type TYPE. LOC is the
4789 location of the open paren of the cast, or the position of the cast
4790 expr. */
3e4093b6 4791tree
c2255bc4 4792c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 4793{
f8893e47 4794 tree type;
928c19bb
JM
4795 tree type_expr = NULL_TREE;
4796 bool type_expr_const = true;
4797 tree ret;
3e4093b6 4798 int saved_wsp = warn_strict_prototypes;
c5c76735 4799
3e4093b6
RS
4800 /* This avoids warnings about unprototyped casts on
4801 integers. E.g. "#define SIG_DFL (void(*)())0". */
4802 if (TREE_CODE (expr) == INTEGER_CST)
4803 warn_strict_prototypes = 0;
928c19bb 4804 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 4805 warn_strict_prototypes = saved_wsp;
c5c76735 4806
c2255bc4 4807 ret = build_c_cast (loc, type, expr);
928c19bb
JM
4808 if (type_expr)
4809 {
9f33203d
JM
4810 bool inner_expr_const = true;
4811 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 4812 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
4813 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4814 && inner_expr_const);
c2255bc4 4815 SET_EXPR_LOCATION (ret, loc);
928c19bb 4816 }
24b97832
ILT
4817
4818 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4819 SET_EXPR_LOCATION (ret, loc);
4820
9e5b2115
PB
4821 /* C++ does not permits types to be defined in a cast, but it
4822 allows references to incomplete types. */
4823 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
4824 warning_at (loc, OPT_Wc___compat,
4825 "defining a type in a cast is invalid in C++");
4826
928c19bb 4827 return ret;
400fbf9f 4828}
3e4093b6
RS
4829\f
4830/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
4831 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4832 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
4833 MODIFYCODE is the code for a binary operator that we use
4834 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 4835 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
4836 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4837 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 4838
c2255bc4
AH
4839 LOCATION is the location of the MODIFYCODE operator.
4840 RHS_LOC is the location of the RHS. */
2f6e4e97 4841
3e4093b6 4842tree
32e8bb8e 4843build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 4844 enum tree_code modifycode,
c2255bc4 4845 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 4846{
3e4093b6
RS
4847 tree result;
4848 tree newrhs;
8ce94e44 4849 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
4850 tree lhstype = TREE_TYPE (lhs);
4851 tree olhstype = lhstype;
928c19bb 4852 bool npc;
e9a25f70 4853
3e4093b6
RS
4854 /* Types that aren't fully specified cannot be used in assignments. */
4855 lhs = require_complete_type (lhs);
e9a25f70 4856
3e4093b6
RS
4857 /* Avoid duplicate error messages from operands that had errors. */
4858 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4859 return error_mark_node;
400fbf9f 4860
46a88c12 4861 /* For ObjC properties, defer this check. */
7bd11157 4862 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
4863 return error_mark_node;
4864
8ce94e44
JM
4865 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4866 {
4867 rhs_semantic_type = TREE_TYPE (rhs);
4868 rhs = TREE_OPERAND (rhs, 0);
4869 }
4870
3e4093b6 4871 newrhs = rhs;
400fbf9f 4872
928c19bb
JM
4873 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4874 {
4875 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 4876 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 4877 rhs_origtype);
928c19bb
JM
4878 if (inner == error_mark_node)
4879 return error_mark_node;
4880 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4881 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4882 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4883 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4884 protected_set_expr_location (result, location);
4885 return result;
4886 }
4887
3e4093b6
RS
4888 /* If a binary op has been requested, combine the old LHS value with the RHS
4889 producing the value we should actually store into the LHS. */
4890
4891 if (modifycode != NOP_EXPR)
400fbf9f 4892 {
928c19bb 4893 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 4894 lhs = stabilize_reference (lhs);
c9f9eb5d 4895 newrhs = build_binary_op (location,
ba47d38d 4896 modifycode, lhs, rhs, 1);
bbbbb16a
ILT
4897
4898 /* The original type of the right hand side is no longer
4899 meaningful. */
4900 rhs_origtype = NULL_TREE;
400fbf9f 4901 }
400fbf9f 4902
668ea4b1
IS
4903 if (c_dialect_objc ())
4904 {
46a88c12
NP
4905 /* Check if we are modifying an Objective-C property reference;
4906 if so, we need to generate setter calls. */
4907 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1
IS
4908 if (result)
4909 return result;
46a88c12
NP
4910
4911 /* Else, do the check that we postponed for Objective-C. */
7bd11157 4912 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
4913 return error_mark_node;
4914 }
4915
9bf24266 4916 /* Give an error for storing in something that is 'const'. */
bbbd6700 4917
f37acdf9 4918 if (TYPE_READONLY (lhstype)
3e4093b6
RS
4919 || ((TREE_CODE (lhstype) == RECORD_TYPE
4920 || TREE_CODE (lhstype) == UNION_TYPE)
4921 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289
DN
4922 {
4923 readonly_error (lhs, lv_assign);
4924 return error_mark_node;
4925 }
f37acdf9
JM
4926 else if (TREE_READONLY (lhs))
4927 readonly_warning (lhs, lv_assign);
bbbd6700 4928
3e4093b6
RS
4929 /* If storing into a structure or union member,
4930 it has probably been given type `int'.
4931 Compute the type that would go with
4932 the actual amount of storage the member occupies. */
bbbd6700 4933
3e4093b6
RS
4934 if (TREE_CODE (lhs) == COMPONENT_REF
4935 && (TREE_CODE (lhstype) == INTEGER_TYPE
4936 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4937 || TREE_CODE (lhstype) == REAL_TYPE
4938 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4939 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 4940
3e4093b6
RS
4941 /* If storing in a field that is in actuality a short or narrower than one,
4942 we must store in the field in its actual type. */
4943
4944 if (lhstype != TREE_TYPE (lhs))
4945 {
4946 lhs = copy_node (lhs);
4947 TREE_TYPE (lhs) = lhstype;
400fbf9f 4948 }
400fbf9f 4949
32e8bb8e
ILT
4950 /* Issue -Wc++-compat warnings about an assignment to an enum type
4951 when LHS does not have its original type. This happens for,
4952 e.g., an enum bitfield in a struct. */
4953 if (warn_cxx_compat
4954 && lhs_origtype != NULL_TREE
4955 && lhs_origtype != lhstype
4956 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4957 {
4958 tree checktype = (rhs_origtype != NULL_TREE
4959 ? rhs_origtype
4960 : TREE_TYPE (rhs));
4961 if (checktype != error_mark_node
4962 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4963 warning_at (location, OPT_Wc___compat,
4964 "enum conversion in assignment is invalid in C++");
4965 }
4966
8ce94e44
JM
4967 /* Convert new value to destination type. Fold it first, then
4968 restore any excess precision information, for the sake of
4969 conversion warnings. */
400fbf9f 4970
928c19bb
JM
4971 npc = null_pointer_constant_p (newrhs);
4972 newrhs = c_fully_fold (newrhs, false, NULL);
8ce94e44
JM
4973 if (rhs_semantic_type)
4974 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
744aa42f
ILT
4975 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4976 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
4977 if (TREE_CODE (newrhs) == ERROR_MARK)
4978 return error_mark_node;
400fbf9f 4979
6e955430
ZL
4980 /* Emit ObjC write barrier, if necessary. */
4981 if (c_dialect_objc () && flag_objc_gc)
4982 {
4983 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4984 if (result)
c9f9eb5d
AH
4985 {
4986 protected_set_expr_location (result, location);
4987 return result;
4988 }
6e955430
ZL
4989 }
4990
ea4b7848 4991 /* Scan operands. */
400fbf9f 4992
53fb4de3 4993 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3e4093b6 4994 TREE_SIDE_EFFECTS (result) = 1;
c9f9eb5d 4995 protected_set_expr_location (result, location);
400fbf9f 4996
3e4093b6
RS
4997 /* If we got the LHS in a different type for storing in,
4998 convert the result back to the nominal type of LHS
4999 so that the value we return always has the same type
5000 as the LHS argument. */
e855c5ce 5001
3e4093b6
RS
5002 if (olhstype == TREE_TYPE (result))
5003 return result;
c9f9eb5d 5004
744aa42f
ILT
5005 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5006 ic_assign, false, NULL_TREE, NULL_TREE, 0);
c9f9eb5d
AH
5007 protected_set_expr_location (result, location);
5008 return result;
3e4093b6
RS
5009}
5010\f
478a1c5b
ILT
5011/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5012 This is used to implement -fplan9-extensions. */
5013
5014static bool
5015find_anonymous_field_with_type (tree struct_type, tree type)
5016{
5017 tree field;
5018 bool found;
5019
5020 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5021 || TREE_CODE (struct_type) == UNION_TYPE);
5022 found = false;
5023 for (field = TYPE_FIELDS (struct_type);
5024 field != NULL_TREE;
5025 field = TREE_CHAIN (field))
5026 {
5027 if (DECL_NAME (field) == NULL
5028 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5029 {
5030 if (found)
5031 return false;
5032 found = true;
5033 }
5034 else if (DECL_NAME (field) == NULL
5035 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5036 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5037 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5038 {
5039 if (found)
5040 return false;
5041 found = true;
5042 }
5043 }
5044 return found;
5045}
5046
5047/* RHS is an expression whose type is pointer to struct. If there is
5048 an anonymous field in RHS with type TYPE, then return a pointer to
5049 that field in RHS. This is used with -fplan9-extensions. This
5050 returns NULL if no conversion could be found. */
5051
5052static tree
5053convert_to_anonymous_field (location_t location, tree type, tree rhs)
5054{
5055 tree rhs_struct_type, lhs_main_type;
5056 tree field, found_field;
5057 bool found_sub_field;
5058 tree ret;
5059
5060 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5061 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5062 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5063 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5064
5065 gcc_assert (POINTER_TYPE_P (type));
5066 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5067
5068 found_field = NULL_TREE;
5069 found_sub_field = false;
5070 for (field = TYPE_FIELDS (rhs_struct_type);
5071 field != NULL_TREE;
5072 field = TREE_CHAIN (field))
5073 {
5074 if (DECL_NAME (field) != NULL_TREE
5075 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5076 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5077 continue;
5078 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5079 {
5080 if (found_field != NULL_TREE)
5081 return NULL_TREE;
5082 found_field = field;
5083 }
5084 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5085 lhs_main_type))
5086 {
5087 if (found_field != NULL_TREE)
5088 return NULL_TREE;
5089 found_field = field;
5090 found_sub_field = true;
5091 }
5092 }
5093
5094 if (found_field == NULL_TREE)
5095 return NULL_TREE;
5096
5097 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5098 build_fold_indirect_ref (rhs), found_field,
5099 NULL_TREE);
5100 ret = build_fold_addr_expr_loc (location, ret);
5101
5102 if (found_sub_field)
5103 {
5104 ret = convert_to_anonymous_field (location, type, ret);
5105 gcc_assert (ret != NULL_TREE);
5106 }
5107
5108 return ret;
5109}
5110
bbbbb16a
ILT
5111/* Convert value RHS to type TYPE as preparation for an assignment to
5112 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5113 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5114 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5115 constant before any folding.
3e4093b6
RS
5116 The real work of conversion is done by `convert'.
5117 The purpose of this function is to generate error messages
5118 for assignments that are not allowed in C.
2ac2f164
JM
5119 ERRTYPE says whether it is argument passing, assignment,
5120 initialization or return.
2f6e4e97 5121
c2255bc4 5122 LOCATION is the location of the RHS.
2ac2f164 5123 FUNCTION is a tree for the function being called.
3e4093b6 5124 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5125
3e4093b6 5126static tree
744aa42f
ILT
5127convert_for_assignment (location_t location, tree type, tree rhs,
5128 tree origtype, enum impl_conv errtype,
5129 bool null_pointer_constant, tree fundecl,
5130 tree function, int parmnum)
3e4093b6
RS
5131{
5132 enum tree_code codel = TREE_CODE (type);
8ce94e44 5133 tree orig_rhs = rhs;
3e4093b6
RS
5134 tree rhstype;
5135 enum tree_code coder;
2ac2f164 5136 tree rname = NULL_TREE;
58393038 5137 bool objc_ok = false;
2ac2f164 5138
6b4ef5c1 5139 if (errtype == ic_argpass)
2ac2f164
JM
5140 {
5141 tree selector;
5142 /* Change pointer to function to the function itself for
5143 diagnostics. */
5144 if (TREE_CODE (function) == ADDR_EXPR
5145 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5146 function = TREE_OPERAND (function, 0);
5147
5148 /* Handle an ObjC selector specially for diagnostics. */
5149 selector = objc_message_selector ();
5150 rname = function;
5151 if (selector && parmnum > 2)
5152 {
5153 rname = selector;
5154 parmnum -= 2;
5155 }
5156 }
5157
5158 /* This macro is used to emit diagnostics to ensure that all format
5159 strings are complete sentences, visible to gettext and checked at
5160 compile time. */
c2255bc4 5161#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5162 do { \
5163 switch (errtype) \
5164 { \
5165 case ic_argpass: \
5166 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
c2255bc4
AH
5167 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5168 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
1e053dfe
MLI
5169 "expected %qT but argument is of type %qT", \
5170 type, rhstype); \
5171 break; \
1e053dfe
MLI
5172 case ic_assign: \
5173 pedwarn (LOCATION, OPT, AS); \
5174 break; \
5175 case ic_init: \
6a8f4e12 5176 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5177 break; \
5178 case ic_return: \
c2255bc4 5179 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5180 break; \
5181 default: \
5182 gcc_unreachable (); \
5183 } \
2ac2f164 5184 } while (0)
cb3ca04e 5185
49706e39
MLI
5186 /* This macro is used to emit diagnostics to ensure that all format
5187 strings are complete sentences, visible to gettext and checked at
5188 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5189 extra parameter to enumerate qualifiers. */
5190
5191#define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5192 do { \
5193 switch (errtype) \
5194 { \
5195 case ic_argpass: \
5196 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5197 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5198 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5199 "expected %qT but argument is of type %qT", \
5200 type, rhstype); \
5201 break; \
5202 case ic_assign: \
5203 pedwarn (LOCATION, OPT, AS, QUALS); \
5204 break; \
5205 case ic_init: \
5206 pedwarn (LOCATION, OPT, IN, QUALS); \
5207 break; \
5208 case ic_return: \
5209 pedwarn (LOCATION, OPT, RE, QUALS); \
5210 break; \
5211 default: \
5212 gcc_unreachable (); \
5213 } \
5214 } while (0)
5215
8ce94e44
JM
5216 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5217 rhs = TREE_OPERAND (rhs, 0);
5218
3e4093b6
RS
5219 rhstype = TREE_TYPE (rhs);
5220 coder = TREE_CODE (rhstype);
5221
5222 if (coder == ERROR_MARK)
5223 return error_mark_node;
5224
58393038
ZL
5225 if (c_dialect_objc ())
5226 {
5227 int parmno;
5228
5229 switch (errtype)
5230 {
5231 case ic_return:
5232 parmno = 0;
5233 break;
5234
5235 case ic_assign:
5236 parmno = -1;
5237 break;
5238
5239 case ic_init:
5240 parmno = -2;
5241 break;
5242
5243 default:
5244 parmno = parmnum;
5245 break;
5246 }
5247
5248 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5249 }
5250
bbbbb16a
ILT
5251 if (warn_cxx_compat)
5252 {
5253 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5254 if (checktype != error_mark_node
5255 && TREE_CODE (type) == ENUMERAL_TYPE
5256 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5257 {
81f40b79
ILT
5258 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5259 G_("enum conversion when passing argument "
5260 "%d of %qE is invalid in C++"),
5261 G_("enum conversion in assignment is "
5262 "invalid in C++"),
5263 G_("enum conversion in initialization is "
5264 "invalid in C++"),
5265 G_("enum conversion in return is "
5266 "invalid in C++"));
bbbbb16a
ILT
5267 }
5268 }
5269
3e4093b6 5270 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5271 return rhs;
3e4093b6
RS
5272
5273 if (coder == VOID_TYPE)
400fbf9f 5274 {
6dcc04b0
JM
5275 /* Except for passing an argument to an unprototyped function,
5276 this is a constraint violation. When passing an argument to
5277 an unprototyped function, it is compile-time undefined;
5278 making it a constraint in that case was rejected in
5279 DR#252. */
c2255bc4 5280 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5281 return error_mark_node;
400fbf9f 5282 }
808d6eaa
JM
5283 rhs = require_complete_type (rhs);
5284 if (rhs == error_mark_node)
5285 return error_mark_node;
cd192ccc
MS
5286 /* A non-reference type can convert to a reference. This handles
5287 va_start, va_copy and possibly port built-ins. */
5288 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5289 {
3e4093b6 5290 if (!lvalue_p (rhs))
400fbf9f 5291 {
c2255bc4 5292 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5293 return error_mark_node;
400fbf9f 5294 }
3e4093b6
RS
5295 if (!c_mark_addressable (rhs))
5296 return error_mark_node;
5297 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5298 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5299
cd192ccc
MS
5300 rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)),
5301 rhs, origtype, errtype, null_pointer_constant,
5302 fundecl, function, parmnum);
5303 if (rhs == error_mark_node)
5304 return error_mark_node;
3e4093b6
RS
5305
5306 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5307 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5308 return rhs;
400fbf9f 5309 }
3e4093b6 5310 /* Some types can interconvert without explicit casts. */
3274deff 5311 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5312 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5313 return convert (type, rhs);
5314 /* Arithmetic types all interconvert, and enum is treated like int. */
5315 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5316 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5317 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5318 || codel == BOOLEAN_TYPE)
5319 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5320 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5321 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5322 || coder == BOOLEAN_TYPE))
928c19bb
JM
5323 {
5324 tree ret;
5325 bool save = in_late_binary_op;
dfe776dd 5326 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5327 in_late_binary_op = true;
8ce94e44 5328 ret = convert_and_check (type, orig_rhs);
dfe776dd 5329 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5330 in_late_binary_op = save;
5331 return ret;
5332 }
400fbf9f 5333
79077aea
JJ
5334 /* Aggregates in different TUs might need conversion. */
5335 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5336 && codel == coder
5337 && comptypes (type, rhstype))
5338 return convert_and_check (type, rhs);
5339
ebf0bf7f 5340 /* Conversion to a transparent union or record from its member types.
3e4093b6 5341 This applies only to function arguments. */
ebf0bf7f
JJ
5342 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5343 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5344 && errtype == ic_argpass)
400fbf9f 5345 {
0257e383 5346 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5347
910ad8de 5348 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5349 {
0257e383 5350 tree memb_type = TREE_TYPE (memb);
400fbf9f 5351
3e4093b6 5352 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5353 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5354 break;
e58cd767 5355
3e4093b6
RS
5356 if (TREE_CODE (memb_type) != POINTER_TYPE)
5357 continue;
2f6e4e97 5358
3e4093b6
RS
5359 if (coder == POINTER_TYPE)
5360 {
5361 tree ttl = TREE_TYPE (memb_type);
5362 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5363
3e4093b6
RS
5364 /* Any non-function converts to a [const][volatile] void *
5365 and vice versa; otherwise, targets must be the same.
5366 Meanwhile, the lhs target must have all the qualifiers of
5367 the rhs. */
5368 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
744aa42f 5369 || comp_target_types (location, memb_type, rhstype))
3e4093b6
RS
5370 {
5371 /* If this type won't generate any warnings, use it. */
5372 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5373 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5374 && TREE_CODE (ttl) == FUNCTION_TYPE)
5375 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5376 == TYPE_QUALS (ttr))
5377 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5378 == TYPE_QUALS (ttl))))
5379 break;
400fbf9f 5380
3e4093b6 5381 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5382 if (!marginal_memb)
5383 marginal_memb = memb;
3e4093b6
RS
5384 }
5385 }
82bde854 5386
3e4093b6 5387 /* Can convert integer zero to any pointer type. */
928c19bb 5388 if (null_pointer_constant)
3e4093b6
RS
5389 {
5390 rhs = null_pointer_node;
5391 break;
5392 }
5393 }
400fbf9f 5394
0257e383 5395 if (memb || marginal_memb)
3e4093b6 5396 {
0257e383 5397 if (!memb)
3e4093b6
RS
5398 {
5399 /* We have only a marginally acceptable member type;
5400 it needs a warning. */
0257e383 5401 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5402 tree ttr = TREE_TYPE (rhstype);
714a0864 5403
3e4093b6
RS
5404 /* Const and volatile mean something different for function
5405 types, so the usual warnings are not appropriate. */
5406 if (TREE_CODE (ttr) == FUNCTION_TYPE
5407 && TREE_CODE (ttl) == FUNCTION_TYPE)
5408 {
5409 /* Because const and volatile on functions are
5410 restrictions that say the function will not do
5411 certain things, it is okay to use a const or volatile
5412 function where an ordinary one is wanted, but not
5413 vice-versa. */
36c5e70a
BE
5414 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5415 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5416 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5417 G_("passing argument %d of %qE "
49706e39 5418 "makes %q#v qualified function "
2ac2f164 5419 "pointer from unqualified"),
49706e39 5420 G_("assignment makes %q#v qualified "
2ac2f164
JM
5421 "function pointer from "
5422 "unqualified"),
49706e39 5423 G_("initialization makes %q#v qualified "
2ac2f164
JM
5424 "function pointer from "
5425 "unqualified"),
49706e39
MLI
5426 G_("return makes %q#v qualified function "
5427 "pointer from unqualified"),
5428 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5429 }
36c5e70a
BE
5430 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5431 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
49706e39 5432 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5433 G_("passing argument %d of %qE discards "
49706e39
MLI
5434 "%qv qualifier from pointer target type"),
5435 G_("assignment discards %qv qualifier "
2ac2f164 5436 "from pointer target type"),
49706e39 5437 G_("initialization discards %qv qualifier "
2ac2f164 5438 "from pointer target type"),
49706e39
MLI
5439 G_("return discards %qv qualifier from "
5440 "pointer target type"),
5441 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5442
5443 memb = marginal_memb;
3e4093b6 5444 }
400fbf9f 5445
fcf73884 5446 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5447 pedwarn (location, OPT_Wpedantic,
fcf73884 5448 "ISO C prohibits argument conversion to union type");
0e7c47fa 5449
db3927fb 5450 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5451 return build_constructor_single (type, memb, rhs);
3e4093b6 5452 }
0e7c47fa
RK
5453 }
5454
3e4093b6
RS
5455 /* Conversions among pointers */
5456 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5457 && (coder == codel))
400fbf9f 5458 {
3e4093b6
RS
5459 tree ttl = TREE_TYPE (type);
5460 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5461 tree mvl = ttl;
5462 tree mvr = ttr;
3e4093b6 5463 bool is_opaque_pointer;
264fa2db 5464 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5465 addr_space_t asl;
5466 addr_space_t asr;
400fbf9f 5467
46df2823
JM
5468 if (TREE_CODE (mvl) != ARRAY_TYPE)
5469 mvl = TYPE_MAIN_VARIANT (mvl);
5470 if (TREE_CODE (mvr) != ARRAY_TYPE)
5471 mvr = TYPE_MAIN_VARIANT (mvr);
3e4093b6 5472 /* Opaque pointers are treated like void pointers. */
f83c7f63 5473 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5474
478a1c5b
ILT
5475 /* The Plan 9 compiler permits a pointer to a struct to be
5476 automatically converted into a pointer to an anonymous field
5477 within the struct. */
5478 if (flag_plan9_extensions
5479 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5480 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5481 && mvl != mvr)
5482 {
5483 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5484 if (new_rhs != NULL_TREE)
5485 {
5486 rhs = new_rhs;
5487 rhstype = TREE_TYPE (rhs);
5488 coder = TREE_CODE (rhstype);
5489 ttr = TREE_TYPE (rhstype);
5490 mvr = TYPE_MAIN_VARIANT (ttr);
5491 }
5492 }
5493
b7e20b53 5494 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5495 for the purpose of reducing the number of false positives, we
5496 tolerate the special case of
b7e20b53 5497
c22cacf3 5498 int *p = NULL;
b7e20b53 5499
c22cacf3 5500 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5501 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c2255bc4
AH
5502 warning_at (location, OPT_Wc___compat,
5503 "request for implicit conversion "
5504 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 5505
36c5e70a
BE
5506 /* See if the pointers point to incompatible address spaces. */
5507 asl = TYPE_ADDR_SPACE (ttl);
5508 asr = TYPE_ADDR_SPACE (ttr);
5509 if (!null_pointer_constant_p (rhs)
5510 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5511 {
5512 switch (errtype)
5513 {
5514 case ic_argpass:
5515 error_at (location, "passing argument %d of %qE from pointer to "
5516 "non-enclosed address space", parmnum, rname);
5517 break;
5518 case ic_assign:
5519 error_at (location, "assignment from pointer to "
5520 "non-enclosed address space");
5521 break;
5522 case ic_init:
5523 error_at (location, "initialization from pointer to "
5524 "non-enclosed address space");
5525 break;
5526 case ic_return:
5527 error_at (location, "return from pointer to "
5528 "non-enclosed address space");
5529 break;
5530 default:
5531 gcc_unreachable ();
5532 }
5533 return error_mark_node;
5534 }
5535
7876a414
KG
5536 /* Check if the right-hand side has a format attribute but the
5537 left-hand side doesn't. */
90137d8f 5538 if (warn_suggest_attribute_format
104f8784 5539 && check_missing_format_attribute (type, rhstype))
c22cacf3 5540 {
104f8784
KG
5541 switch (errtype)
5542 {
5543 case ic_argpass:
90137d8f 5544 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5545 "argument %d of %qE might be "
5546 "a candidate for a format attribute",
5547 parmnum, rname);
104f8784
KG
5548 break;
5549 case ic_assign:
90137d8f 5550 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5551 "assignment left-hand side might be "
5552 "a candidate for a format attribute");
104f8784
KG
5553 break;
5554 case ic_init:
90137d8f 5555 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5556 "initialization left-hand side might be "
5557 "a candidate for a format attribute");
104f8784
KG
5558 break;
5559 case ic_return:
90137d8f 5560 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5561 "return type might be "
5562 "a candidate for a format attribute");
104f8784
KG
5563 break;
5564 default:
5565 gcc_unreachable ();
5566 }
7876a414 5567 }
c22cacf3 5568
3e4093b6
RS
5569 /* Any non-function converts to a [const][volatile] void *
5570 and vice versa; otherwise, targets must be the same.
5571 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5572 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
744aa42f 5573 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 5574 || is_opaque_pointer
f8a93a2e
JJ
5575 || ((c_common_unsigned_type (mvl)
5576 == c_common_unsigned_type (mvr))
5577 && c_common_signed_type (mvl)
5578 == c_common_signed_type (mvr)))
3e4093b6
RS
5579 {
5580 if (pedantic
5581 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5582 ||
5583 (VOID_TYPE_P (ttr)
928c19bb 5584 && !null_pointer_constant
3e4093b6 5585 && TREE_CODE (ttl) == FUNCTION_TYPE)))
c1771a20 5586 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
509c9d60 5587 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
5588 "%qE between function pointer "
5589 "and %<void *%>"),
4b794eaf 5590 G_("ISO C forbids assignment between "
2ac2f164 5591 "function pointer and %<void *%>"),
4b794eaf 5592 G_("ISO C forbids initialization between "
2ac2f164 5593 "function pointer and %<void *%>"),
4b794eaf 5594 G_("ISO C forbids return between function "
2ac2f164 5595 "pointer and %<void *%>"));
3e4093b6
RS
5596 /* Const and volatile mean something different for function types,
5597 so the usual warnings are not appropriate. */
5598 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5599 && TREE_CODE (ttl) != FUNCTION_TYPE)
5600 {
36c5e70a
BE
5601 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5602 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
58393038 5603 {
6c39e757
NP
5604 WARN_FOR_QUALIFIERS (location, 0,
5605 G_("passing argument %d of %qE discards "
5606 "%qv qualifier from pointer target type"),
5607 G_("assignment discards %qv qualifier "
5608 "from pointer target type"),
5609 G_("initialization discards %qv qualifier "
5610 "from pointer target type"),
5611 G_("return discards %qv qualifier from "
5612 "pointer target type"),
5613 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 5614 }
3e4093b6
RS
5615 /* If this is not a case of ignoring a mismatch in signedness,
5616 no warning. */
5617 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 5618 || target_cmp)
3e4093b6
RS
5619 ;
5620 /* If there is a mismatch, do warn. */
f2fd3821 5621 else if (warn_pointer_sign)
c2255bc4 5622 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
509c9d60 5623 G_("pointer targets in passing argument "
2ac2f164 5624 "%d of %qE differ in signedness"),
4b794eaf 5625 G_("pointer targets in assignment "
2ac2f164 5626 "differ in signedness"),
4b794eaf 5627 G_("pointer targets in initialization "
2ac2f164 5628 "differ in signedness"),
4b794eaf 5629 G_("pointer targets in return differ "
2ac2f164 5630 "in signedness"));
3e4093b6
RS
5631 }
5632 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5633 && TREE_CODE (ttr) == FUNCTION_TYPE)
5634 {
5635 /* Because const and volatile on functions are restrictions
5636 that say the function will not do certain things,
5637 it is okay to use a const or volatile function
5638 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
5639 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5640 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5641 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5642 G_("passing argument %d of %qE makes "
49706e39 5643 "%q#v qualified function pointer "
2ac2f164 5644 "from unqualified"),
49706e39 5645 G_("assignment makes %q#v qualified function "
2ac2f164 5646 "pointer from unqualified"),
49706e39 5647 G_("initialization makes %q#v qualified "
2ac2f164 5648 "function pointer from unqualified"),
49706e39
MLI
5649 G_("return makes %q#v qualified function "
5650 "pointer from unqualified"),
5651 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
5652 }
5653 }
5654 else
58393038
ZL
5655 /* Avoid warning about the volatile ObjC EH puts on decls. */
5656 if (!objc_ok)
c2255bc4 5657 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 5658 G_("passing argument %d of %qE from "
58393038 5659 "incompatible pointer type"),
4b794eaf
JJ
5660 G_("assignment from incompatible pointer type"),
5661 G_("initialization from incompatible "
58393038 5662 "pointer type"),
4b794eaf 5663 G_("return from incompatible pointer type"));
58393038 5664
3e4093b6
RS
5665 return convert (type, rhs);
5666 }
b494fd98
EB
5667 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5668 {
6dcc04b0
JM
5669 /* ??? This should not be an error when inlining calls to
5670 unprototyped functions. */
c2255bc4 5671 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
5672 return error_mark_node;
5673 }
3e4093b6 5674 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 5675 {
3e4093b6
RS
5676 /* An explicit constant 0 can convert to a pointer,
5677 or one that results from arithmetic, even including
5678 a cast to integer type. */
928c19bb 5679 if (!null_pointer_constant)
c2255bc4 5680 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 5681 G_("passing argument %d of %qE makes "
2ac2f164 5682 "pointer from integer without a cast"),
4b794eaf 5683 G_("assignment makes pointer from integer "
2ac2f164 5684 "without a cast"),
4b794eaf 5685 G_("initialization makes pointer from "
2ac2f164 5686 "integer without a cast"),
4b794eaf 5687 G_("return makes pointer from integer "
2ac2f164 5688 "without a cast"));
b3006337
EB
5689
5690 return convert (type, rhs);
400fbf9f 5691 }
3e4093b6 5692 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 5693 {
c2255bc4 5694 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 5695 G_("passing argument %d of %qE makes integer "
2ac2f164 5696 "from pointer without a cast"),
4b794eaf 5697 G_("assignment makes integer from pointer "
2ac2f164 5698 "without a cast"),
4b794eaf 5699 G_("initialization makes integer from pointer "
2ac2f164 5700 "without a cast"),
4b794eaf 5701 G_("return makes integer from pointer "
2ac2f164 5702 "without a cast"));
3e4093b6
RS
5703 return convert (type, rhs);
5704 }
5705 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
5706 {
5707 tree ret;
5708 bool save = in_late_binary_op;
5709 in_late_binary_op = true;
5710 ret = convert (type, rhs);
5711 in_late_binary_op = save;
5712 return ret;
5713 }
400fbf9f 5714
2ac2f164 5715 switch (errtype)
3e4093b6 5716 {
2ac2f164 5717 case ic_argpass:
c2255bc4 5718 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
a7da8b42
MLI
5719 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5720 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5721 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
5722 break;
5723 case ic_assign:
c2255bc4
AH
5724 error_at (location, "incompatible types when assigning to type %qT from "
5725 "type %qT", type, rhstype);
2ac2f164
JM
5726 break;
5727 case ic_init:
c2255bc4
AH
5728 error_at (location,
5729 "incompatible types when initializing type %qT using type %qT",
5730 type, rhstype);
2ac2f164
JM
5731 break;
5732 case ic_return:
c2255bc4
AH
5733 error_at (location,
5734 "incompatible types when returning type %qT but %qT was "
5735 "expected", rhstype, type);
2ac2f164
JM
5736 break;
5737 default:
5738 gcc_unreachable ();
400fbf9f 5739 }
53b01f59 5740
3e4093b6
RS
5741 return error_mark_node;
5742}
3e4093b6
RS
5743\f
5744/* If VALUE is a compound expr all of whose expressions are constant, then
5745 return its value. Otherwise, return error_mark_node.
15b732b2 5746
3e4093b6
RS
5747 This is for handling COMPOUND_EXPRs as initializer elements
5748 which is allowed with a warning when -pedantic is specified. */
15b732b2 5749
3e4093b6
RS
5750static tree
5751valid_compound_expr_initializer (tree value, tree endtype)
5752{
5753 if (TREE_CODE (value) == COMPOUND_EXPR)
5754 {
5755 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5756 == error_mark_node)
5757 return error_mark_node;
5758 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5759 endtype);
5760 }
116df786 5761 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
5762 return error_mark_node;
5763 else
5764 return value;
15b732b2 5765}
400fbf9f 5766\f
3e4093b6
RS
5767/* Perform appropriate conversions on the initial value of a variable,
5768 store it in the declaration DECL,
5769 and print any error messages that are appropriate.
bbbbb16a 5770 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
5771 If the init is invalid, store an ERROR_MARK.
5772
5773 INIT_LOC is the location of the initial value. */
400fbf9f 5774
3e4093b6 5775void
c2255bc4 5776store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 5777{
3e4093b6 5778 tree value, type;
928c19bb 5779 bool npc = false;
400fbf9f 5780
3e4093b6 5781 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 5782
3e4093b6
RS
5783 type = TREE_TYPE (decl);
5784 if (TREE_CODE (type) == ERROR_MARK)
5785 return;
400fbf9f 5786
3e4093b6 5787 /* Digest the specified initializer into an expression. */
400fbf9f 5788
928c19bb
JM
5789 if (init)
5790 npc = null_pointer_constant_p (init);
c2255bc4
AH
5791 value = digest_init (init_loc, type, init, origtype, npc,
5792 true, TREE_STATIC (decl));
400fbf9f 5793
3e4093b6 5794 /* Store the expression if valid; else report error. */
400fbf9f 5795
3176a0c2 5796 if (!in_system_header
3f75a254 5797 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
5798 warning (OPT_Wtraditional, "traditional C rejects automatic "
5799 "aggregate initialization");
2f6e4e97 5800
3e4093b6 5801 DECL_INITIAL (decl) = value;
400fbf9f 5802
3e4093b6
RS
5803 /* ANSI wants warnings about out-of-range constant initializers. */
5804 STRIP_TYPE_NOPS (value);
b8698a0f 5805 if (TREE_STATIC (decl))
c2658540 5806 constant_expression_warning (value);
400fbf9f 5807
3e4093b6
RS
5808 /* Check if we need to set array size from compound literal size. */
5809 if (TREE_CODE (type) == ARRAY_TYPE
5810 && TYPE_DOMAIN (type) == 0
5811 && value != error_mark_node)
400fbf9f 5812 {
3e4093b6
RS
5813 tree inside_init = init;
5814
ed248cf7 5815 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
5816 inside_init = fold (inside_init);
5817
5818 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5819 {
8d9f82d5 5820 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 5821
8d9f82d5 5822 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
5823 {
5824 /* For int foo[] = (int [3]){1}; we need to set array size
5825 now since later on array initializer will be just the
5826 brace enclosed list of the compound literal. */
e30ecc5d 5827 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 5828 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 5829 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 5830 layout_type (type);
8d9f82d5 5831 layout_decl (cldecl, 0);
e30ecc5d
JJ
5832 TREE_TYPE (decl)
5833 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
5834 }
5835 }
400fbf9f 5836 }
3e4093b6
RS
5837}
5838\f
5839/* Methods for storing and printing names for error messages. */
400fbf9f 5840
3e4093b6
RS
5841/* Implement a spelling stack that allows components of a name to be pushed
5842 and popped. Each element on the stack is this structure. */
400fbf9f 5843
3e4093b6
RS
5844struct spelling
5845{
5846 int kind;
5847 union
400fbf9f 5848 {
a0f0ab9f 5849 unsigned HOST_WIDE_INT i;
3e4093b6
RS
5850 const char *s;
5851 } u;
5852};
2f6e4e97 5853
3e4093b6
RS
5854#define SPELLING_STRING 1
5855#define SPELLING_MEMBER 2
5856#define SPELLING_BOUNDS 3
400fbf9f 5857
3e4093b6
RS
5858static struct spelling *spelling; /* Next stack element (unused). */
5859static struct spelling *spelling_base; /* Spelling stack base. */
5860static int spelling_size; /* Size of the spelling stack. */
400fbf9f 5861
3e4093b6
RS
5862/* Macros to save and restore the spelling stack around push_... functions.
5863 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 5864
3e4093b6
RS
5865#define SPELLING_DEPTH() (spelling - spelling_base)
5866#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 5867
3e4093b6
RS
5868/* Push an element on the spelling stack with type KIND and assign VALUE
5869 to MEMBER. */
400fbf9f 5870
3e4093b6
RS
5871#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5872{ \
5873 int depth = SPELLING_DEPTH (); \
5874 \
5875 if (depth >= spelling_size) \
5876 { \
5877 spelling_size += 10; \
cca8ead2
BI
5878 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5879 spelling_size); \
3e4093b6
RS
5880 RESTORE_SPELLING_DEPTH (depth); \
5881 } \
5882 \
5883 spelling->kind = (KIND); \
5884 spelling->MEMBER = (VALUE); \
5885 spelling++; \
5886}
400fbf9f 5887
3e4093b6 5888/* Push STRING on the stack. Printed literally. */
400fbf9f 5889
3e4093b6
RS
5890static void
5891push_string (const char *string)
5892{
5893 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5894}
400fbf9f 5895
3e4093b6 5896/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 5897
3e4093b6
RS
5898static void
5899push_member_name (tree decl)
5900{
5901 const char *const string
88388a52
JM
5902 = (DECL_NAME (decl)
5903 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5904 : _("<anonymous>"));
3e4093b6
RS
5905 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5906}
400fbf9f 5907
3e4093b6 5908/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 5909
3e4093b6 5910static void
a0f0ab9f 5911push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
5912{
5913 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5914}
bb58bec5 5915
3e4093b6 5916/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 5917
3e4093b6
RS
5918static int
5919spelling_length (void)
5920{
5921 int size = 0;
5922 struct spelling *p;
400fbf9f 5923
3e4093b6
RS
5924 for (p = spelling_base; p < spelling; p++)
5925 {
5926 if (p->kind == SPELLING_BOUNDS)
5927 size += 25;
5928 else
5929 size += strlen (p->u.s) + 1;
5930 }
5931
5932 return size;
400fbf9f 5933}
400fbf9f 5934
3e4093b6 5935/* Print the spelling to BUFFER and return it. */
400fbf9f 5936
3e4093b6
RS
5937static char *
5938print_spelling (char *buffer)
400fbf9f 5939{
3e4093b6
RS
5940 char *d = buffer;
5941 struct spelling *p;
400fbf9f 5942
3e4093b6
RS
5943 for (p = spelling_base; p < spelling; p++)
5944 if (p->kind == SPELLING_BOUNDS)
5945 {
a0f0ab9f 5946 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
5947 d += strlen (d);
5948 }
5949 else
5950 {
5951 const char *s;
5952 if (p->kind == SPELLING_MEMBER)
5953 *d++ = '.';
5954 for (s = p->u.s; (*d = *s++); d++)
5955 ;
5956 }
5957 *d++ = '\0';
5958 return buffer;
5959}
400fbf9f 5960
3e4093b6 5961/* Issue an error message for a bad initializer component.
6a8f4e12 5962 GMSGID identifies the message.
3e4093b6 5963 The component name is taken from the spelling stack. */
400fbf9f 5964
3e4093b6 5965void
6a8f4e12 5966error_init (const char *gmsgid)
3e4093b6
RS
5967{
5968 char *ofwhat;
400fbf9f 5969
6a8f4e12
AP
5970 /* The gmsgid may be a format string with %< and %>. */
5971 error (gmsgid);
28dab132 5972 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 5973 if (*ofwhat)
bda67431 5974 error ("(near initialization for %qs)", ofwhat);
3e4093b6 5975}
400fbf9f 5976
fcf73884
MLI
5977/* Issue a pedantic warning for a bad initializer component. OPT is
5978 the option OPT_* (from options.h) controlling this warning or 0 if
6a8f4e12 5979 it is unconditionally given. GMSGID identifies the message. The
fcf73884 5980 component name is taken from the spelling stack. */
400fbf9f 5981
3e4093b6 5982void
6a8f4e12 5983pedwarn_init (location_t location, int opt, const char *gmsgid)
3e4093b6
RS
5984{
5985 char *ofwhat;
f90e8e2e 5986
6a8f4e12
AP
5987 /* The gmsgid may be a format string with %< and %>. */
5988 pedwarn (location, opt, gmsgid);
28dab132 5989 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 5990 if (*ofwhat)
509c9d60 5991 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
3e4093b6 5992}
9f720c3e 5993
b8698a0f 5994/* Issue a warning for a bad initializer component.
683d6ff9
MLI
5995
5996 OPT is the OPT_W* value corresponding to the warning option that
6a8f4e12 5997 controls this warning. GMSGID identifies the message. The
683d6ff9 5998 component name is taken from the spelling stack. */
61179109 5999
3e4093b6 6000static void
6a8f4e12 6001warning_init (int opt, const char *gmsgid)
3e4093b6
RS
6002{
6003 char *ofwhat;
7e842ef8 6004
6a8f4e12
AP
6005 /* The gmsgid may be a format string with %< and %>. */
6006 warning (opt, gmsgid);
28dab132 6007 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6008 if (*ofwhat)
683d6ff9 6009 warning (opt, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
6010}
6011\f
916c5919
JM
6012/* If TYPE is an array type and EXPR is a parenthesized string
6013 constant, warn if pedantic that EXPR is being used to initialize an
6014 object of type TYPE. */
6015
6016void
6017maybe_warn_string_init (tree type, struct c_expr expr)
6018{
6019 if (pedantic
6020 && TREE_CODE (type) == ARRAY_TYPE
6021 && TREE_CODE (expr.value) == STRING_CST
6022 && expr.original_code != STRING_CST)
c1771a20 6023 pedwarn_init (input_location, OPT_Wpedantic,
fcf73884 6024 "array initialized from parenthesized string constant");
916c5919
JM
6025}
6026
3e4093b6
RS
6027/* Digest the parser output INIT as an initializer for type TYPE.
6028 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6029
bbbbb16a
ILT
6030 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6031
928c19bb
JM
6032 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6033
916c5919
JM
6034 If INIT is a string constant, STRICT_STRING is true if it is
6035 unparenthesized or we should not warn here for it being parenthesized.
6036 For other types of INIT, STRICT_STRING is not used.
6037
c2255bc4
AH
6038 INIT_LOC is the location of the INIT.
6039
3e4093b6
RS
6040 REQUIRE_CONSTANT requests an error if non-constant initializers or
6041 elements are seen. */
7e842ef8 6042
3e4093b6 6043static tree
c2255bc4
AH
6044digest_init (location_t init_loc, tree type, tree init, tree origtype,
6045 bool null_pointer_constant, bool strict_string,
6046 int require_constant)
3e4093b6
RS
6047{
6048 enum tree_code code = TREE_CODE (type);
6049 tree inside_init = init;
8ce94e44 6050 tree semantic_type = NULL_TREE;
928c19bb 6051 bool maybe_const = true;
7e842ef8 6052
3e4093b6 6053 if (type == error_mark_node
f01da1a5 6054 || !init
3e4093b6
RS
6055 || init == error_mark_node
6056 || TREE_TYPE (init) == error_mark_node)
6057 return error_mark_node;
7e842ef8 6058
ed248cf7 6059 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6060
8ce94e44
JM
6061 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6062 {
6063 semantic_type = TREE_TYPE (inside_init);
6064 inside_init = TREE_OPERAND (inside_init, 0);
6065 }
928c19bb
JM
6066 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6067 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6068
3e4093b6
RS
6069 /* Initialization of an array of chars from a string constant
6070 optionally enclosed in braces. */
7e842ef8 6071
197463ae
JM
6072 if (code == ARRAY_TYPE && inside_init
6073 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6
RS
6074 {
6075 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
197463ae
JM
6076 /* Note that an array could be both an array of character type
6077 and an array of wchar_t if wchar_t is signed char or unsigned
6078 char. */
6079 bool char_array = (typ1 == char_type_node
6080 || typ1 == signed_char_type_node
6081 || typ1 == unsigned_char_type_node);
6082 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6083 bool char16_array = !!comptypes (typ1, char16_type_node);
6084 bool char32_array = !!comptypes (typ1, char32_type_node);
6085
6086 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6087 {
916c5919 6088 struct c_expr expr;
c466b2cd 6089 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6090 expr.value = inside_init;
6091 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6092 expr.original_type = NULL;
916c5919
JM
6093 maybe_warn_string_init (type, expr);
6094
a45e580b 6095 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6096 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6097 "initialization of a flexible array member");
6098
3e4093b6 6099 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6100 TYPE_MAIN_VARIANT (type)))
3e4093b6 6101 return inside_init;
7e842ef8 6102
c466b2cd 6103 if (char_array)
3e4093b6 6104 {
c466b2cd
KVH
6105 if (typ2 != char_type_node)
6106 {
6107 error_init ("char-array initialized from wide string");
6108 return error_mark_node;
6109 }
3e4093b6 6110 }
c466b2cd 6111 else
3e4093b6 6112 {
c466b2cd
KVH
6113 if (typ2 == char_type_node)
6114 {
6115 error_init ("wide character array initialized from non-wide "
6116 "string");
6117 return error_mark_node;
6118 }
6119 else if (!comptypes(typ1, typ2))
6120 {
6121 error_init ("wide character array initialized from "
6122 "incompatible wide string");
6123 return error_mark_node;
6124 }
7e842ef8 6125 }
2f6e4e97 6126
3e4093b6
RS
6127 TREE_TYPE (inside_init) = type;
6128 if (TYPE_DOMAIN (type) != 0
6129 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6130 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6131 {
6132 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6133
c466b2cd 6134 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6135 because it's ok to ignore the terminating null char
6136 that is counted in the length of the constant. */
5eb4df45
ILT
6137 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6138 (len
6139 - (TYPE_PRECISION (typ1)
6140 / BITS_PER_UNIT))))
6141 pedwarn_init (init_loc, 0,
6142 ("initializer-string for array of chars "
6143 "is too long"));
6144 else if (warn_cxx_compat
6145 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6146 warning_at (init_loc, OPT_Wc___compat,
6147 ("initializer-string for array chars "
6148 "is too long for C++"));
6149 }
7e842ef8 6150
3e4093b6 6151 return inside_init;
7e842ef8 6152 }
197463ae
JM
6153 else if (INTEGRAL_TYPE_P (typ1))
6154 {
6155 error_init ("array of inappropriate type initialized "
6156 "from string constant");
6157 return error_mark_node;
6158 }
7e842ef8
PE
6159 }
6160
3e4093b6
RS
6161 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6162 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6163 below and handle as a constructor. */
e89be13b
JJ
6164 if (code == VECTOR_TYPE
6165 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6166 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6167 && TREE_CONSTANT (inside_init))
6168 {
6169 if (TREE_CODE (inside_init) == VECTOR_CST
6170 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6171 TYPE_MAIN_VARIANT (type)))
6172 return inside_init;
6173
6174 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6175 {
4038c495
GB
6176 unsigned HOST_WIDE_INT ix;
6177 tree value;
6178 bool constant_p = true;
e89be13b
JJ
6179
6180 /* Iterate through elements and check if all constructor
6181 elements are *_CSTs. */
4038c495
GB
6182 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6183 if (!CONSTANT_CLASS_P (value))
6184 {
6185 constant_p = false;
6186 break;
6187 }
e89be13b 6188
4038c495
GB
6189 if (constant_p)
6190 return build_vector_from_ctor (type,
6191 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6192 }
6193 }
6035d635 6194
ca085fd7
MLI
6195 if (warn_sequence_point)
6196 verify_sequence_points (inside_init);
6197
3e4093b6
RS
6198 /* Any type can be initialized
6199 from an expression of the same type, optionally with braces. */
400fbf9f 6200
3e4093b6
RS
6201 if (inside_init && TREE_TYPE (inside_init) != 0
6202 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6203 TYPE_MAIN_VARIANT (type))
3e4093b6 6204 || (code == ARRAY_TYPE
132da1a5 6205 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6206 || (code == VECTOR_TYPE
132da1a5 6207 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6208 || (code == POINTER_TYPE
3897f229 6209 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6210 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6211 TREE_TYPE (type)))))
3e4093b6
RS
6212 {
6213 if (code == POINTER_TYPE)
b494fd98 6214 {
b494fd98
EB
6215 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6216 {
f2a71bbc
JM
6217 if (TREE_CODE (inside_init) == STRING_CST
6218 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6219 inside_init = array_to_pointer_conversion
6220 (init_loc, inside_init);
f2a71bbc
JM
6221 else
6222 {
6223 error_init ("invalid use of non-lvalue array");
6224 return error_mark_node;
6225 }
b494fd98 6226 }
f2a71bbc 6227 }
b494fd98 6228
bae39a73
NS
6229 if (code == VECTOR_TYPE)
6230 /* Although the types are compatible, we may require a
6231 conversion. */
6232 inside_init = convert (type, inside_init);
3e4093b6 6233
ca58211b
PB
6234 if (require_constant
6235 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6236 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6237 {
3e4093b6
RS
6238 /* As an extension, allow initializing objects with static storage
6239 duration with compound literals (which are then treated just as
ca58211b
PB
6240 the brace enclosed list they contain). Also allow this for
6241 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6242 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6243 inside_init = DECL_INITIAL (decl);
400fbf9f 6244 }
3e4093b6
RS
6245
6246 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6247 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6248 {
3e4093b6
RS
6249 error_init ("array initialized from non-constant array expression");
6250 return error_mark_node;
400fbf9f 6251 }
400fbf9f 6252
c1771a20 6253 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6254 -pedantic-errors is specified. In the later case, we always want
6255 an error. In the former case, we simply want a warning. */
6256 if (require_constant && pedantic
6257 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6258 {
6259 inside_init
6260 = valid_compound_expr_initializer (inside_init,
6261 TREE_TYPE (inside_init));
6262 if (inside_init == error_mark_node)
6263 error_init ("initializer element is not constant");
2f6e4e97 6264 else
c1771a20 6265 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6266 "initializer element is not constant");
3e4093b6
RS
6267 if (flag_pedantic_errors)
6268 inside_init = error_mark_node;
6269 }
6270 else if (require_constant
116df786
RH
6271 && !initializer_constant_valid_p (inside_init,
6272 TREE_TYPE (inside_init)))
3e4093b6
RS
6273 {
6274 error_init ("initializer element is not constant");
6275 inside_init = error_mark_node;
8b40563c 6276 }
928c19bb 6277 else if (require_constant && !maybe_const)
c2255bc4 6278 pedwarn_init (init_loc, 0,
928c19bb 6279 "initializer element is not a constant expression");
f735a153 6280
90137d8f 6281 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6282 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
c2255bc4
AH
6283 inside_init = convert_for_assignment (init_loc, type, inside_init,
6284 origtype,
bbbbb16a 6285 ic_init, null_pointer_constant,
928c19bb 6286 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6287 return inside_init;
6288 }
f735a153 6289
3e4093b6 6290 /* Handle scalar types, including conversions. */
400fbf9f 6291
ab22c1fa
CF
6292 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6293 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6294 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6295 {
f2a71bbc
JM
6296 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6297 && (TREE_CODE (init) == STRING_CST
6298 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6299 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6300 if (semantic_type)
6301 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6302 inside_init);
3e4093b6 6303 inside_init
c2255bc4
AH
6304 = convert_for_assignment (init_loc, type, inside_init, origtype,
6305 ic_init, null_pointer_constant,
3e4093b6 6306 NULL_TREE, NULL_TREE, 0);
2f6e4e97 6307
3274deff
JW
6308 /* Check to see if we have already given an error message. */
6309 if (inside_init == error_mark_node)
6310 ;
3f75a254 6311 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6312 {
3e4093b6
RS
6313 error_init ("initializer element is not constant");
6314 inside_init = error_mark_node;
400fbf9f 6315 }
3e4093b6 6316 else if (require_constant
116df786
RH
6317 && !initializer_constant_valid_p (inside_init,
6318 TREE_TYPE (inside_init)))
400fbf9f 6319 {
3e4093b6
RS
6320 error_init ("initializer element is not computable at load time");
6321 inside_init = error_mark_node;
400fbf9f 6322 }
928c19bb 6323 else if (require_constant && !maybe_const)
c2255bc4 6324 pedwarn_init (init_loc, 0,
928c19bb 6325 "initializer element is not a constant expression");
3e4093b6
RS
6326
6327 return inside_init;
400fbf9f 6328 }
d9fc6069 6329
3e4093b6 6330 /* Come here only for records and arrays. */
d9fc6069 6331
3e4093b6 6332 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6333 {
3e4093b6
RS
6334 error_init ("variable-sized object may not be initialized");
6335 return error_mark_node;
d9fc6069 6336 }
3e4093b6
RS
6337
6338 error_init ("invalid initializer");
6339 return error_mark_node;
d9fc6069 6340}
400fbf9f 6341\f
3e4093b6 6342/* Handle initializers that use braces. */
400fbf9f 6343
3e4093b6
RS
6344/* Type of object we are accumulating a constructor for.
6345 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6346static tree constructor_type;
400fbf9f 6347
3e4093b6
RS
6348/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6349 left to fill. */
6350static tree constructor_fields;
400fbf9f 6351
3e4093b6
RS
6352/* For an ARRAY_TYPE, this is the specified index
6353 at which to store the next element we get. */
6354static tree constructor_index;
400fbf9f 6355
3e4093b6
RS
6356/* For an ARRAY_TYPE, this is the maximum index. */
6357static tree constructor_max_index;
400fbf9f 6358
3e4093b6
RS
6359/* For a RECORD_TYPE, this is the first field not yet written out. */
6360static tree constructor_unfilled_fields;
400fbf9f 6361
3e4093b6
RS
6362/* For an ARRAY_TYPE, this is the index of the first element
6363 not yet written out. */
6364static tree constructor_unfilled_index;
895ea614 6365
3e4093b6
RS
6366/* In a RECORD_TYPE, the byte index of the next consecutive field.
6367 This is so we can generate gaps between fields, when appropriate. */
6368static tree constructor_bit_index;
10d5caec 6369
3e4093b6
RS
6370/* If we are saving up the elements rather than allocating them,
6371 this is the list of elements so far (in reverse order,
6372 most recent first). */
9771b263 6373static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6374
3e4093b6
RS
6375/* 1 if constructor should be incrementally stored into a constructor chain,
6376 0 if all the elements should be kept in AVL tree. */
6377static int constructor_incremental;
ad47f1e5 6378
3e4093b6
RS
6379/* 1 if so far this constructor's elements are all compile-time constants. */
6380static int constructor_constant;
ad47f1e5 6381
3e4093b6
RS
6382/* 1 if so far this constructor's elements are all valid address constants. */
6383static int constructor_simple;
ad47f1e5 6384
928c19bb
JM
6385/* 1 if this constructor has an element that cannot be part of a
6386 constant expression. */
6387static int constructor_nonconst;
6388
3e4093b6
RS
6389/* 1 if this constructor is erroneous so far. */
6390static int constructor_erroneous;
d45cf215 6391
3e4093b6
RS
6392/* Structure for managing pending initializer elements, organized as an
6393 AVL tree. */
d45cf215 6394
3e4093b6 6395struct init_node
d45cf215 6396{
3e4093b6
RS
6397 struct init_node *left, *right;
6398 struct init_node *parent;
6399 int balance;
6400 tree purpose;
6401 tree value;
bbbbb16a 6402 tree origtype;
d45cf215
RS
6403};
6404
3e4093b6
RS
6405/* Tree of pending elements at this constructor level.
6406 These are elements encountered out of order
6407 which belong at places we haven't reached yet in actually
6408 writing the output.
6409 Will never hold tree nodes across GC runs. */
6410static struct init_node *constructor_pending_elts;
d45cf215 6411
3e4093b6
RS
6412/* The SPELLING_DEPTH of this constructor. */
6413static int constructor_depth;
d45cf215 6414
3e4093b6
RS
6415/* DECL node for which an initializer is being read.
6416 0 means we are reading a constructor expression
6417 such as (struct foo) {...}. */
6418static tree constructor_decl;
d45cf215 6419
3e4093b6
RS
6420/* Nonzero if this is an initializer for a top-level decl. */
6421static int constructor_top_level;
d45cf215 6422
3e4093b6
RS
6423/* Nonzero if there were any member designators in this initializer. */
6424static int constructor_designated;
d45cf215 6425
3e4093b6
RS
6426/* Nesting depth of designator list. */
6427static int designator_depth;
d45cf215 6428
3e4093b6 6429/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6430static int designator_erroneous;
d45cf215 6431
3e4093b6
RS
6432\f
6433/* This stack has a level for each implicit or explicit level of
6434 structuring in the initializer, including the outermost one. It
6435 saves the values of most of the variables above. */
d45cf215 6436
3e4093b6
RS
6437struct constructor_range_stack;
6438
6439struct constructor_stack
d45cf215 6440{
3e4093b6
RS
6441 struct constructor_stack *next;
6442 tree type;
6443 tree fields;
6444 tree index;
6445 tree max_index;
6446 tree unfilled_index;
6447 tree unfilled_fields;
6448 tree bit_index;
9771b263 6449 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6450 struct init_node *pending_elts;
6451 int offset;
6452 int depth;
916c5919 6453 /* If value nonzero, this value should replace the entire
3e4093b6 6454 constructor at this level. */
916c5919 6455 struct c_expr replacement_value;
3e4093b6
RS
6456 struct constructor_range_stack *range_stack;
6457 char constant;
6458 char simple;
928c19bb 6459 char nonconst;
3e4093b6
RS
6460 char implicit;
6461 char erroneous;
6462 char outer;
6463 char incremental;
6464 char designated;
6465};
d45cf215 6466
802415d1 6467static struct constructor_stack *constructor_stack;
d45cf215 6468
3e4093b6
RS
6469/* This stack represents designators from some range designator up to
6470 the last designator in the list. */
d45cf215 6471
3e4093b6
RS
6472struct constructor_range_stack
6473{
6474 struct constructor_range_stack *next, *prev;
6475 struct constructor_stack *stack;
6476 tree range_start;
6477 tree index;
6478 tree range_end;
6479 tree fields;
6480};
d45cf215 6481
802415d1 6482static struct constructor_range_stack *constructor_range_stack;
d45cf215 6483
3e4093b6
RS
6484/* This stack records separate initializers that are nested.
6485 Nested initializers can't happen in ANSI C, but GNU C allows them
6486 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6487
3e4093b6 6488struct initializer_stack
d45cf215 6489{
3e4093b6
RS
6490 struct initializer_stack *next;
6491 tree decl;
3e4093b6
RS
6492 struct constructor_stack *constructor_stack;
6493 struct constructor_range_stack *constructor_range_stack;
9771b263 6494 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6495 struct spelling *spelling;
6496 struct spelling *spelling_base;
6497 int spelling_size;
6498 char top_level;
6499 char require_constant_value;
6500 char require_constant_elements;
6501};
d45cf215 6502
802415d1 6503static struct initializer_stack *initializer_stack;
3e4093b6
RS
6504\f
6505/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6506
6507void
a396f8ae 6508start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6509{
3e4093b6 6510 const char *locus;
cceb1885 6511 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6512
3e4093b6 6513 p->decl = constructor_decl;
3e4093b6
RS
6514 p->require_constant_value = require_constant_value;
6515 p->require_constant_elements = require_constant_elements;
6516 p->constructor_stack = constructor_stack;
6517 p->constructor_range_stack = constructor_range_stack;
6518 p->elements = constructor_elements;
6519 p->spelling = spelling;
6520 p->spelling_base = spelling_base;
6521 p->spelling_size = spelling_size;
6522 p->top_level = constructor_top_level;
6523 p->next = initializer_stack;
6524 initializer_stack = p;
400fbf9f 6525
3e4093b6 6526 constructor_decl = decl;
3e4093b6
RS
6527 constructor_designated = 0;
6528 constructor_top_level = top_level;
400fbf9f 6529
6f17bbcf 6530 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6531 {
6532 require_constant_value = TREE_STATIC (decl);
6533 require_constant_elements
6534 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6535 /* For a scalar, you can always use any value to initialize,
6536 even within braces. */
6537 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6538 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6539 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6540 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6541 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6542 }
6543 else
6544 {
6545 require_constant_value = 0;
6546 require_constant_elements = 0;
88388a52 6547 locus = _("(anonymous)");
3e4093b6 6548 }
b71c7f8a 6549
3e4093b6
RS
6550 constructor_stack = 0;
6551 constructor_range_stack = 0;
b71c7f8a 6552
3e4093b6
RS
6553 missing_braces_mentioned = 0;
6554
6555 spelling_base = 0;
6556 spelling_size = 0;
6557 RESTORE_SPELLING_DEPTH (0);
6558
6559 if (locus)
6560 push_string (locus);
6561}
6562
6563void
6564finish_init (void)
b71c7f8a 6565{
3e4093b6 6566 struct initializer_stack *p = initializer_stack;
b71c7f8a 6567
3e4093b6
RS
6568 /* Free the whole constructor stack of this initializer. */
6569 while (constructor_stack)
6570 {
6571 struct constructor_stack *q = constructor_stack;
6572 constructor_stack = q->next;
6573 free (q);
6574 }
6575
366de0ce 6576 gcc_assert (!constructor_range_stack);
3e4093b6
RS
6577
6578 /* Pop back to the data of the outer initializer (if any). */
36579663 6579 free (spelling_base);
3aeb3655 6580
3e4093b6 6581 constructor_decl = p->decl;
3e4093b6
RS
6582 require_constant_value = p->require_constant_value;
6583 require_constant_elements = p->require_constant_elements;
6584 constructor_stack = p->constructor_stack;
6585 constructor_range_stack = p->constructor_range_stack;
6586 constructor_elements = p->elements;
6587 spelling = p->spelling;
6588 spelling_base = p->spelling_base;
6589 spelling_size = p->spelling_size;
6590 constructor_top_level = p->top_level;
6591 initializer_stack = p->next;
6592 free (p);
b71c7f8a 6593}
400fbf9f 6594\f
3e4093b6
RS
6595/* Call here when we see the initializer is surrounded by braces.
6596 This is instead of a call to push_init_level;
6597 it is matched by a call to pop_init_level.
400fbf9f 6598
3e4093b6
RS
6599 TYPE is the type to initialize, for a constructor expression.
6600 For an initializer for a decl, TYPE is zero. */
400fbf9f 6601
3e4093b6
RS
6602void
6603really_start_incremental_init (tree type)
400fbf9f 6604{
5d038c4c 6605 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 6606
3e4093b6
RS
6607 if (type == 0)
6608 type = TREE_TYPE (constructor_decl);
400fbf9f 6609
b6fc2cdb
PB
6610 if (TREE_CODE (type) == VECTOR_TYPE
6611 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 6612 error ("opaque vector types cannot be initialized");
400fbf9f 6613
3e4093b6
RS
6614 p->type = constructor_type;
6615 p->fields = constructor_fields;
6616 p->index = constructor_index;
6617 p->max_index = constructor_max_index;
6618 p->unfilled_index = constructor_unfilled_index;
6619 p->unfilled_fields = constructor_unfilled_fields;
6620 p->bit_index = constructor_bit_index;
6621 p->elements = constructor_elements;
6622 p->constant = constructor_constant;
6623 p->simple = constructor_simple;
928c19bb 6624 p->nonconst = constructor_nonconst;
3e4093b6
RS
6625 p->erroneous = constructor_erroneous;
6626 p->pending_elts = constructor_pending_elts;
6627 p->depth = constructor_depth;
916c5919
JM
6628 p->replacement_value.value = 0;
6629 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 6630 p->replacement_value.original_type = NULL;
3e4093b6
RS
6631 p->implicit = 0;
6632 p->range_stack = 0;
6633 p->outer = 0;
6634 p->incremental = constructor_incremental;
6635 p->designated = constructor_designated;
6636 p->next = 0;
6637 constructor_stack = p;
b13aca19 6638
3e4093b6
RS
6639 constructor_constant = 1;
6640 constructor_simple = 1;
928c19bb 6641 constructor_nonconst = 0;
3e4093b6 6642 constructor_depth = SPELLING_DEPTH ();
9771b263 6643 constructor_elements = NULL;
3e4093b6
RS
6644 constructor_pending_elts = 0;
6645 constructor_type = type;
6646 constructor_incremental = 1;
6647 constructor_designated = 0;
6648 designator_depth = 0;
b06df647 6649 designator_erroneous = 0;
400fbf9f 6650
3e4093b6
RS
6651 if (TREE_CODE (constructor_type) == RECORD_TYPE
6652 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 6653 {
3e4093b6
RS
6654 constructor_fields = TYPE_FIELDS (constructor_type);
6655 /* Skip any nameless bit fields at the beginning. */
6656 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6657 && DECL_NAME (constructor_fields) == 0)
910ad8de 6658 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 6659
3e4093b6
RS
6660 constructor_unfilled_fields = constructor_fields;
6661 constructor_bit_index = bitsize_zero_node;
400fbf9f 6662 }
3e4093b6
RS
6663 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6664 {
6665 if (TYPE_DOMAIN (constructor_type))
6666 {
6667 constructor_max_index
6668 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 6669
3e4093b6
RS
6670 /* Detect non-empty initializations of zero-length arrays. */
6671 if (constructor_max_index == NULL_TREE
6672 && TYPE_SIZE (constructor_type))
9a9d280e 6673 constructor_max_index = integer_minus_one_node;
400fbf9f 6674
3e4093b6
RS
6675 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6676 to initialize VLAs will cause a proper error; avoid tree
6677 checking errors as well by setting a safe value. */
6678 if (constructor_max_index
6679 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 6680 constructor_max_index = integer_minus_one_node;
59c83dbf 6681
3e4093b6
RS
6682 constructor_index
6683 = convert (bitsizetype,
6684 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 6685 }
3e4093b6 6686 else
493179da
JM
6687 {
6688 constructor_index = bitsize_zero_node;
6689 constructor_max_index = NULL_TREE;
6690 }
59c83dbf 6691
3e4093b6
RS
6692 constructor_unfilled_index = constructor_index;
6693 }
6694 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6695 {
6696 /* Vectors are like simple fixed-size arrays. */
6697 constructor_max_index =
c62c040f 6698 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 6699 constructor_index = bitsize_zero_node;
3e4093b6
RS
6700 constructor_unfilled_index = constructor_index;
6701 }
6702 else
6703 {
6704 /* Handle the case of int x = {5}; */
6705 constructor_fields = constructor_type;
6706 constructor_unfilled_fields = constructor_type;
6707 }
6708}
6709\f
6710/* Push down into a subobject, for initialization.
6711 If this is for an explicit set of braces, IMPLICIT is 0.
6712 If it is because the next element belongs at a lower level,
6713 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 6714
3e4093b6 6715void
a1e3b3d9 6716push_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
6717{
6718 struct constructor_stack *p;
6719 tree value = NULL_TREE;
400fbf9f 6720
3e4093b6 6721 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
6722 pop them now. If implicit == 1, this will have been done in
6723 process_init_element; do not repeat it here because in the case
6724 of excess initializers for an empty aggregate this leads to an
6725 infinite cycle of popping a level and immediately recreating
6726 it. */
6727 if (implicit != 1)
3e4093b6 6728 {
472d98b4
JM
6729 while (constructor_stack->implicit)
6730 {
6731 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6732 || TREE_CODE (constructor_type) == UNION_TYPE)
6733 && constructor_fields == 0)
a1e3b3d9
LB
6734 process_init_element (pop_init_level (1, braced_init_obstack),
6735 true, braced_init_obstack);
472d98b4
JM
6736 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6737 && constructor_max_index
6738 && tree_int_cst_lt (constructor_max_index,
6739 constructor_index))
a1e3b3d9
LB
6740 process_init_element (pop_init_level (1, braced_init_obstack),
6741 true, braced_init_obstack);
472d98b4
JM
6742 else
6743 break;
6744 }
3e4093b6 6745 }
400fbf9f 6746
3e4093b6
RS
6747 /* Unless this is an explicit brace, we need to preserve previous
6748 content if any. */
6749 if (implicit)
6750 {
6751 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6752 || TREE_CODE (constructor_type) == UNION_TYPE)
6753 && constructor_fields)
a1e3b3d9 6754 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 6755 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 6756 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
6757 }
6758
5d038c4c 6759 p = XNEW (struct constructor_stack);
3e4093b6
RS
6760 p->type = constructor_type;
6761 p->fields = constructor_fields;
6762 p->index = constructor_index;
6763 p->max_index = constructor_max_index;
6764 p->unfilled_index = constructor_unfilled_index;
6765 p->unfilled_fields = constructor_unfilled_fields;
6766 p->bit_index = constructor_bit_index;
6767 p->elements = constructor_elements;
6768 p->constant = constructor_constant;
6769 p->simple = constructor_simple;
928c19bb 6770 p->nonconst = constructor_nonconst;
3e4093b6
RS
6771 p->erroneous = constructor_erroneous;
6772 p->pending_elts = constructor_pending_elts;
6773 p->depth = constructor_depth;
916c5919
JM
6774 p->replacement_value.value = 0;
6775 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 6776 p->replacement_value.original_type = NULL;
3e4093b6
RS
6777 p->implicit = implicit;
6778 p->outer = 0;
6779 p->incremental = constructor_incremental;
6780 p->designated = constructor_designated;
6781 p->next = constructor_stack;
6782 p->range_stack = 0;
6783 constructor_stack = p;
400fbf9f 6784
3e4093b6
RS
6785 constructor_constant = 1;
6786 constructor_simple = 1;
928c19bb 6787 constructor_nonconst = 0;
3e4093b6 6788 constructor_depth = SPELLING_DEPTH ();
9771b263 6789 constructor_elements = NULL;
3e4093b6
RS
6790 constructor_incremental = 1;
6791 constructor_designated = 0;
6792 constructor_pending_elts = 0;
6793 if (!implicit)
400fbf9f 6794 {
3e4093b6
RS
6795 p->range_stack = constructor_range_stack;
6796 constructor_range_stack = 0;
6797 designator_depth = 0;
b06df647 6798 designator_erroneous = 0;
3e4093b6 6799 }
400fbf9f 6800
3e4093b6
RS
6801 /* Don't die if an entire brace-pair level is superfluous
6802 in the containing level. */
6803 if (constructor_type == 0)
6804 ;
6805 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6806 || TREE_CODE (constructor_type) == UNION_TYPE)
6807 {
6808 /* Don't die if there are extra init elts at the end. */
6809 if (constructor_fields == 0)
6810 constructor_type = 0;
6811 else
400fbf9f 6812 {
3e4093b6
RS
6813 constructor_type = TREE_TYPE (constructor_fields);
6814 push_member_name (constructor_fields);
6815 constructor_depth++;
400fbf9f 6816 }
3e4093b6
RS
6817 }
6818 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6819 {
6820 constructor_type = TREE_TYPE (constructor_type);
a0f0ab9f 6821 push_array_bounds (tree_low_cst (constructor_index, 1));
3e4093b6 6822 constructor_depth++;
400fbf9f
JW
6823 }
6824
3e4093b6 6825 if (constructor_type == 0)
400fbf9f 6826 {
3e4093b6
RS
6827 error_init ("extra brace group at end of initializer");
6828 constructor_fields = 0;
6829 constructor_unfilled_fields = 0;
6830 return;
400fbf9f
JW
6831 }
6832
3e4093b6
RS
6833 if (value && TREE_CODE (value) == CONSTRUCTOR)
6834 {
6835 constructor_constant = TREE_CONSTANT (value);
6836 constructor_simple = TREE_STATIC (value);
928c19bb 6837 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 6838 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 6839 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
6840 && (TREE_CODE (constructor_type) == RECORD_TYPE
6841 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 6842 set_nonincremental_init (braced_init_obstack);
3e4093b6 6843 }
400fbf9f 6844
3e4093b6
RS
6845 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6846 {
6847 missing_braces_mentioned = 1;
683d6ff9 6848 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
3e4093b6 6849 }
400fbf9f 6850
3e4093b6
RS
6851 if (TREE_CODE (constructor_type) == RECORD_TYPE
6852 || TREE_CODE (constructor_type) == UNION_TYPE)
6853 {
6854 constructor_fields = TYPE_FIELDS (constructor_type);
6855 /* Skip any nameless bit fields at the beginning. */
6856 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6857 && DECL_NAME (constructor_fields) == 0)
f7587ed0 6858 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 6859
3e4093b6
RS
6860 constructor_unfilled_fields = constructor_fields;
6861 constructor_bit_index = bitsize_zero_node;
6862 }
6863 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6864 {
6865 /* Vectors are like simple fixed-size arrays. */
6866 constructor_max_index =
c62c040f
RG
6867 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6868 constructor_index = bitsize_int (0);
3e4093b6
RS
6869 constructor_unfilled_index = constructor_index;
6870 }
6871 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6872 {
6873 if (TYPE_DOMAIN (constructor_type))
6874 {
6875 constructor_max_index
6876 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 6877
3e4093b6
RS
6878 /* Detect non-empty initializations of zero-length arrays. */
6879 if (constructor_max_index == NULL_TREE
6880 && TYPE_SIZE (constructor_type))
9a9d280e 6881 constructor_max_index = integer_minus_one_node;
de520661 6882
3e4093b6
RS
6883 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6884 to initialize VLAs will cause a proper error; avoid tree
6885 checking errors as well by setting a safe value. */
6886 if (constructor_max_index
6887 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 6888 constructor_max_index = integer_minus_one_node;
b62acd60 6889
3e4093b6
RS
6890 constructor_index
6891 = convert (bitsizetype,
6892 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6893 }
6894 else
6895 constructor_index = bitsize_zero_node;
de520661 6896
3e4093b6
RS
6897 constructor_unfilled_index = constructor_index;
6898 if (value && TREE_CODE (value) == STRING_CST)
6899 {
6900 /* We need to split the char/wchar array into individual
6901 characters, so that we don't have to special case it
6902 everywhere. */
a1e3b3d9 6903 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
6904 }
6905 }
6906 else
6907 {
b4519d39 6908 if (constructor_type != error_mark_node)
683d6ff9 6909 warning_init (0, "braces around scalar initializer");
3e4093b6
RS
6910 constructor_fields = constructor_type;
6911 constructor_unfilled_fields = constructor_type;
6912 }
6913}
8b6a5902 6914
3e4093b6 6915/* At the end of an implicit or explicit brace level,
916c5919
JM
6916 finish up that level of constructor. If a single expression
6917 with redundant braces initialized that level, return the
6918 c_expr structure for that expression. Otherwise, the original_code
6919 element is set to ERROR_MARK.
6920 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 6921 from inner levels (process_init_element ignores that),
916c5919 6922 but return error_mark_node as the value from the outermost level
3e4093b6 6923 (that's what we want to put in DECL_INITIAL).
916c5919 6924 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 6925
916c5919 6926struct c_expr
a1e3b3d9 6927pop_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
6928{
6929 struct constructor_stack *p;
916c5919
JM
6930 struct c_expr ret;
6931 ret.value = 0;
6932 ret.original_code = ERROR_MARK;
6866c6e8 6933 ret.original_type = NULL;
de520661 6934
3e4093b6
RS
6935 if (implicit == 0)
6936 {
6937 /* When we come to an explicit close brace,
6938 pop any inner levels that didn't have explicit braces. */
6939 while (constructor_stack->implicit)
a1e3b3d9
LB
6940 {
6941 process_init_element (pop_init_level (1, braced_init_obstack),
6942 true, braced_init_obstack);
6943 }
366de0ce 6944 gcc_assert (!constructor_range_stack);
3e4093b6 6945 }
e5e809f4 6946
0066ef9c
RH
6947 /* Now output all pending elements. */
6948 constructor_incremental = 1;
a1e3b3d9 6949 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 6950
3e4093b6 6951 p = constructor_stack;
e5e809f4 6952
3e4093b6
RS
6953 /* Error for initializing a flexible array member, or a zero-length
6954 array member in an inappropriate context. */
6955 if (constructor_type && constructor_fields
6956 && TREE_CODE (constructor_type) == ARRAY_TYPE
6957 && TYPE_DOMAIN (constructor_type)
3f75a254 6958 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
6959 {
6960 /* Silently discard empty initializations. The parser will
6961 already have pedwarned for empty brackets. */
6962 if (integer_zerop (constructor_unfilled_index))
6963 constructor_type = NULL_TREE;
366de0ce 6964 else
3e4093b6 6965 {
366de0ce 6966 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 6967
3e4093b6
RS
6968 if (constructor_depth > 2)
6969 error_init ("initialization of flexible array member in a nested context");
fcf73884 6970 else
c1771a20 6971 pedwarn_init (input_location, OPT_Wpedantic,
509c9d60 6972 "initialization of a flexible array member");
de520661 6973
3e4093b6
RS
6974 /* We have already issued an error message for the existence
6975 of a flexible array member not at the end of the structure.
535a42b1 6976 Discard the initializer so that we do not die later. */
910ad8de 6977 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
6978 constructor_type = NULL_TREE;
6979 }
3e4093b6 6980 }
de520661 6981
3e4093b6 6982 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 6983 if (warn_missing_field_initializers
3e4093b6
RS
6984 && constructor_type
6985 && TREE_CODE (constructor_type) == RECORD_TYPE
6986 && constructor_unfilled_fields)
6987 {
49819fef 6988 bool constructor_zeroinit =
9771b263
DN
6989 (vec_safe_length (constructor_elements) == 1
6990 && integer_zerop ((*constructor_elements)[0].value));
49819fef 6991
3e4093b6
RS
6992 /* Do not warn for flexible array members or zero-length arrays. */
6993 while (constructor_unfilled_fields
3f75a254 6994 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 6995 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 6996 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 6997
49819fef
AM
6998 if (constructor_unfilled_fields
6999 /* Do not warn if this level of the initializer uses member
7000 designators; it is likely to be deliberate. */
7001 && !constructor_designated
7002 /* Do not warn about initializing with ` = {0}'. */
7003 && !constructor_zeroinit)
3e4093b6 7004 {
32397f22
MLI
7005 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7006 "missing initializer for field %qD of %qT",
7007 constructor_unfilled_fields,
7008 constructor_type))
7009 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7010 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7011 }
7012 }
de520661 7013
3e4093b6 7014 /* Pad out the end of the structure. */
916c5919 7015 if (p->replacement_value.value)
3e4093b6
RS
7016 /* If this closes a superfluous brace pair,
7017 just pass out the element between them. */
916c5919 7018 ret = p->replacement_value;
3e4093b6
RS
7019 else if (constructor_type == 0)
7020 ;
7021 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7022 && TREE_CODE (constructor_type) != UNION_TYPE
7023 && TREE_CODE (constructor_type) != ARRAY_TYPE
7024 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7025 {
7026 /* A nonincremental scalar initializer--just return
7027 the element, after verifying there is just one. */
9771b263 7028 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7029 {
7030 if (!constructor_erroneous)
7031 error_init ("empty scalar initializer");
916c5919 7032 ret.value = error_mark_node;
3e4093b6 7033 }
9771b263 7034 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6
RS
7035 {
7036 error_init ("extra elements in scalar initializer");
9771b263 7037 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7038 }
7039 else
9771b263 7040 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7041 }
7042 else
7043 {
7044 if (constructor_erroneous)
916c5919 7045 ret.value = error_mark_node;
3e4093b6
RS
7046 else
7047 {
916c5919 7048 ret.value = build_constructor (constructor_type,
4038c495 7049 constructor_elements);
3e4093b6 7050 if (constructor_constant)
51eed280 7051 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7052 if (constructor_constant && constructor_simple)
916c5919 7053 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7054 if (constructor_nonconst)
7055 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7056 }
7057 }
de520661 7058
928c19bb
JM
7059 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7060 {
7061 if (constructor_nonconst)
7062 ret.original_code = C_MAYBE_CONST_EXPR;
7063 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7064 ret.original_code = ERROR_MARK;
7065 }
7066
3e4093b6
RS
7067 constructor_type = p->type;
7068 constructor_fields = p->fields;
7069 constructor_index = p->index;
7070 constructor_max_index = p->max_index;
7071 constructor_unfilled_index = p->unfilled_index;
7072 constructor_unfilled_fields = p->unfilled_fields;
7073 constructor_bit_index = p->bit_index;
7074 constructor_elements = p->elements;
7075 constructor_constant = p->constant;
7076 constructor_simple = p->simple;
928c19bb 7077 constructor_nonconst = p->nonconst;
3e4093b6
RS
7078 constructor_erroneous = p->erroneous;
7079 constructor_incremental = p->incremental;
7080 constructor_designated = p->designated;
7081 constructor_pending_elts = p->pending_elts;
7082 constructor_depth = p->depth;
7083 if (!p->implicit)
7084 constructor_range_stack = p->range_stack;
7085 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7086
3e4093b6
RS
7087 constructor_stack = p->next;
7088 free (p);
b621a4dd 7089
5d5e98dc
VR
7090 if (ret.value == 0 && constructor_stack == 0)
7091 ret.value = error_mark_node;
916c5919 7092 return ret;
3e4093b6 7093}
8b6a5902 7094
3e4093b6
RS
7095/* Common handling for both array range and field name designators.
7096 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7097
3e4093b6 7098static int
a1e3b3d9 7099set_designator (int array, struct obstack * braced_init_obstack)
de520661 7100{
3e4093b6
RS
7101 tree subtype;
7102 enum tree_code subcode;
de520661 7103
3e4093b6
RS
7104 /* Don't die if an entire brace-pair level is superfluous
7105 in the containing level. */
7106 if (constructor_type == 0)
7107 return 1;
de520661 7108
366de0ce
NS
7109 /* If there were errors in this designator list already, bail out
7110 silently. */
b06df647 7111 if (designator_erroneous)
3e4093b6 7112 return 1;
e28cae4f 7113
3e4093b6
RS
7114 if (!designator_depth)
7115 {
366de0ce 7116 gcc_assert (!constructor_range_stack);
de520661 7117
3e4093b6
RS
7118 /* Designator list starts at the level of closest explicit
7119 braces. */
7120 while (constructor_stack->implicit)
a1e3b3d9
LB
7121 {
7122 process_init_element (pop_init_level (1, braced_init_obstack),
7123 true, braced_init_obstack);
7124 }
3e4093b6
RS
7125 constructor_designated = 1;
7126 return 0;
7127 }
de520661 7128
366de0ce 7129 switch (TREE_CODE (constructor_type))
3c3fa147 7130 {
366de0ce
NS
7131 case RECORD_TYPE:
7132 case UNION_TYPE:
3e4093b6
RS
7133 subtype = TREE_TYPE (constructor_fields);
7134 if (subtype != error_mark_node)
7135 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7136 break;
7137 case ARRAY_TYPE:
3e4093b6 7138 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7139 break;
7140 default:
7141 gcc_unreachable ();
de520661 7142 }
400fbf9f 7143
3e4093b6
RS
7144 subcode = TREE_CODE (subtype);
7145 if (array && subcode != ARRAY_TYPE)
7146 {
7147 error_init ("array index in non-array initializer");
7148 return 1;
7149 }
7150 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7151 {
7152 error_init ("field name not in record or union initializer");
7153 return 1;
7154 }
d45cf215 7155
3e4093b6 7156 constructor_designated = 1;
a1e3b3d9 7157 push_init_level (2, braced_init_obstack);
3e4093b6 7158 return 0;
de520661 7159}
400fbf9f 7160
3e4093b6
RS
7161/* If there are range designators in designator list, push a new designator
7162 to constructor_range_stack. RANGE_END is end of such stack range or
7163 NULL_TREE if there is no range designator at this level. */
400fbf9f 7164
3e4093b6 7165static void
a1e3b3d9 7166push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7167{
7168 struct constructor_range_stack *p;
400fbf9f 7169
a1e3b3d9
LB
7170 p = (struct constructor_range_stack *)
7171 obstack_alloc (braced_init_obstack,
7172 sizeof (struct constructor_range_stack));
3e4093b6
RS
7173 p->prev = constructor_range_stack;
7174 p->next = 0;
7175 p->fields = constructor_fields;
7176 p->range_start = constructor_index;
7177 p->index = constructor_index;
7178 p->stack = constructor_stack;
7179 p->range_end = range_end;
8b6a5902 7180 if (constructor_range_stack)
3e4093b6
RS
7181 constructor_range_stack->next = p;
7182 constructor_range_stack = p;
de520661 7183}
400fbf9f 7184
3e4093b6
RS
7185/* Within an array initializer, specify the next index to be initialized.
7186 FIRST is that index. If LAST is nonzero, then initialize a range
7187 of indices, running from FIRST through LAST. */
5a7ec9d9 7188
de520661 7189void
a1e3b3d9
LB
7190set_init_index (tree first, tree last,
7191 struct obstack * braced_init_obstack)
de520661 7192{
a1e3b3d9 7193 if (set_designator (1, braced_init_obstack))
3e4093b6 7194 return;
de520661 7195
b06df647 7196 designator_erroneous = 1;
de520661 7197
3ea8cd06
JM
7198 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7199 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7200 {
7201 error_init ("array index in initializer not of integer type");
7202 return;
7203 }
7204
2b6da65c
JM
7205 if (TREE_CODE (first) != INTEGER_CST)
7206 {
7207 first = c_fully_fold (first, false, NULL);
7208 if (TREE_CODE (first) == INTEGER_CST)
c1771a20 7209 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7210 "array index in initializer is not "
7211 "an integer constant expression");
7212 }
7213
7214 if (last && TREE_CODE (last) != INTEGER_CST)
7215 {
7216 last = c_fully_fold (last, false, NULL);
7217 if (TREE_CODE (last) == INTEGER_CST)
c1771a20 7218 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7219 "array index in initializer is not "
7220 "an integer constant expression");
7221 }
7222
3e4093b6
RS
7223 if (TREE_CODE (first) != INTEGER_CST)
7224 error_init ("nonconstant array index in initializer");
7225 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7226 error_init ("nonconstant array index in initializer");
7227 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7228 error_init ("array index in non-array initializer");
622adc7e
MK
7229 else if (tree_int_cst_sgn (first) == -1)
7230 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
7231 else if (constructor_max_index
7232 && tree_int_cst_lt (constructor_max_index, first))
7233 error_init ("array index in initializer exceeds array bounds");
7234 else
de520661 7235 {
928c19bb
JM
7236 constant_expression_warning (first);
7237 if (last)
7238 constant_expression_warning (last);
3e4093b6 7239 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7240 if (tree_int_cst_lt (constructor_index, first))
7241 {
7242 constructor_index = copy_node (constructor_index);
7243 TREE_OVERFLOW (constructor_index) = 1;
7244 }
665f2503 7245
3e4093b6 7246 if (last)
2bede729 7247 {
3e4093b6
RS
7248 if (tree_int_cst_equal (first, last))
7249 last = 0;
7250 else if (tree_int_cst_lt (last, first))
7251 {
7252 error_init ("empty index range in initializer");
7253 last = 0;
7254 }
7255 else
7256 {
7257 last = convert (bitsizetype, last);
7258 if (constructor_max_index != 0
7259 && tree_int_cst_lt (constructor_max_index, last))
7260 {
7261 error_init ("array index range in initializer exceeds array bounds");
7262 last = 0;
7263 }
7264 }
2bede729 7265 }
fed3cef0 7266
3e4093b6 7267 designator_depth++;
b06df647 7268 designator_erroneous = 0;
3e4093b6 7269 if (constructor_range_stack || last)
a1e3b3d9 7270 push_range_stack (last, braced_init_obstack);
de520661 7271 }
de520661 7272}
3e4093b6
RS
7273
7274/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7275
de520661 7276void
a1e3b3d9 7277set_init_label (tree fieldname, struct obstack * braced_init_obstack)
de520661 7278{
0fb96aa4 7279 tree field;
94ba5069 7280
a1e3b3d9 7281 if (set_designator (0, braced_init_obstack))
3e4093b6
RS
7282 return;
7283
b06df647 7284 designator_erroneous = 1;
3e4093b6
RS
7285
7286 if (TREE_CODE (constructor_type) != RECORD_TYPE
7287 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7288 {
3e4093b6
RS
7289 error_init ("field name not in record or union initializer");
7290 return;
94ba5069
RS
7291 }
7292
0fb96aa4 7293 field = lookup_field (constructor_type, fieldname);
8b6a5902 7294
0fb96aa4 7295 if (field == 0)
c51a1ba9 7296 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7297 else
0fb96aa4
JM
7298 do
7299 {
7300 constructor_fields = TREE_VALUE (field);
7301 designator_depth++;
7302 designator_erroneous = 0;
7303 if (constructor_range_stack)
7304 push_range_stack (NULL_TREE, braced_init_obstack);
7305 field = TREE_CHAIN (field);
7306 if (field)
7307 {
7308 if (set_designator (0, braced_init_obstack))
7309 return;
7310 }
7311 }
7312 while (field != NULL_TREE);
3e4093b6
RS
7313}
7314\f
7315/* Add a new initializer to the tree of pending initializers. PURPOSE
7316 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7317 VALUE is the value of that index or field. If ORIGTYPE is not
7318 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7319
7320 IMPLICIT is true if value comes from pop_init_level (1),
7321 the new initializer has been merged with the existing one
7322 and thus no warnings should be emitted about overriding an
7323 existing initializer. */
de520661 7324
3e4093b6 7325static void
a1e3b3d9
LB
7326add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7327 struct obstack * braced_init_obstack)
3e4093b6
RS
7328{
7329 struct init_node *p, **q, *r;
7330
7331 q = &constructor_pending_elts;
7332 p = 0;
7333
7334 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7335 {
3e4093b6 7336 while (*q != 0)
91fa3c30 7337 {
3e4093b6
RS
7338 p = *q;
7339 if (tree_int_cst_lt (purpose, p->purpose))
7340 q = &p->left;
7341 else if (tree_int_cst_lt (p->purpose, purpose))
7342 q = &p->right;
7343 else
7344 {
b295aee2
JJ
7345 if (!implicit)
7346 {
7347 if (TREE_SIDE_EFFECTS (p->value))
7348 warning_init (0, "initialized field with side-effects overwritten");
7349 else if (warn_override_init)
7350 warning_init (OPT_Woverride_init, "initialized field overwritten");
7351 }
3e4093b6 7352 p->value = value;
bbbbb16a 7353 p->origtype = origtype;
3e4093b6
RS
7354 return;
7355 }
91fa3c30 7356 }
de520661 7357 }
3e4093b6 7358 else
de520661 7359 {
3e4093b6 7360 tree bitpos;
400fbf9f 7361
3e4093b6
RS
7362 bitpos = bit_position (purpose);
7363 while (*q != NULL)
7364 {
7365 p = *q;
7366 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7367 q = &p->left;
7368 else if (p->purpose != purpose)
7369 q = &p->right;
7370 else
7371 {
b295aee2
JJ
7372 if (!implicit)
7373 {
7374 if (TREE_SIDE_EFFECTS (p->value))
7375 warning_init (0, "initialized field with side-effects overwritten");
7376 else if (warn_override_init)
7377 warning_init (OPT_Woverride_init, "initialized field overwritten");
7378 }
3e4093b6 7379 p->value = value;
bbbbb16a 7380 p->origtype = origtype;
3e4093b6
RS
7381 return;
7382 }
7383 }
91fa3c30 7384 }
b71c7f8a 7385
a1e3b3d9
LB
7386 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7387 sizeof (struct init_node));
3e4093b6
RS
7388 r->purpose = purpose;
7389 r->value = value;
bbbbb16a 7390 r->origtype = origtype;
8b6a5902 7391
3e4093b6
RS
7392 *q = r;
7393 r->parent = p;
7394 r->left = 0;
7395 r->right = 0;
7396 r->balance = 0;
b71c7f8a 7397
3e4093b6 7398 while (p)
de520661 7399 {
3e4093b6 7400 struct init_node *s;
665f2503 7401
3e4093b6 7402 if (r == p->left)
2bede729 7403 {
3e4093b6
RS
7404 if (p->balance == 0)
7405 p->balance = -1;
7406 else if (p->balance < 0)
7407 {
7408 if (r->balance < 0)
7409 {
7410 /* L rotation. */
7411 p->left = r->right;
7412 if (p->left)
7413 p->left->parent = p;
7414 r->right = p;
e7b6a0ee 7415
3e4093b6
RS
7416 p->balance = 0;
7417 r->balance = 0;
39bc99c2 7418
3e4093b6
RS
7419 s = p->parent;
7420 p->parent = r;
7421 r->parent = s;
7422 if (s)
7423 {
7424 if (s->left == p)
7425 s->left = r;
7426 else
7427 s->right = r;
7428 }
7429 else
7430 constructor_pending_elts = r;
7431 }
7432 else
7433 {
7434 /* LR rotation. */
7435 struct init_node *t = r->right;
e7b6a0ee 7436
3e4093b6
RS
7437 r->right = t->left;
7438 if (r->right)
7439 r->right->parent = r;
7440 t->left = r;
7441
7442 p->left = t->right;
7443 if (p->left)
7444 p->left->parent = p;
7445 t->right = p;
7446
7447 p->balance = t->balance < 0;
7448 r->balance = -(t->balance > 0);
7449 t->balance = 0;
7450
7451 s = p->parent;
7452 p->parent = t;
7453 r->parent = t;
7454 t->parent = s;
7455 if (s)
7456 {
7457 if (s->left == p)
7458 s->left = t;
7459 else
7460 s->right = t;
7461 }
7462 else
7463 constructor_pending_elts = t;
7464 }
7465 break;
7466 }
7467 else
7468 {
7469 /* p->balance == +1; growth of left side balances the node. */
7470 p->balance = 0;
7471 break;
7472 }
2bede729 7473 }
3e4093b6
RS
7474 else /* r == p->right */
7475 {
7476 if (p->balance == 0)
7477 /* Growth propagation from right side. */
7478 p->balance++;
7479 else if (p->balance > 0)
7480 {
7481 if (r->balance > 0)
7482 {
7483 /* R rotation. */
7484 p->right = r->left;
7485 if (p->right)
7486 p->right->parent = p;
7487 r->left = p;
7488
7489 p->balance = 0;
7490 r->balance = 0;
7491
7492 s = p->parent;
7493 p->parent = r;
7494 r->parent = s;
7495 if (s)
7496 {
7497 if (s->left == p)
7498 s->left = r;
7499 else
7500 s->right = r;
7501 }
7502 else
7503 constructor_pending_elts = r;
7504 }
7505 else /* r->balance == -1 */
7506 {
7507 /* RL rotation */
7508 struct init_node *t = r->left;
7509
7510 r->left = t->right;
7511 if (r->left)
7512 r->left->parent = r;
7513 t->right = r;
7514
7515 p->right = t->left;
7516 if (p->right)
7517 p->right->parent = p;
7518 t->left = p;
7519
7520 r->balance = (t->balance < 0);
7521 p->balance = -(t->balance > 0);
7522 t->balance = 0;
7523
7524 s = p->parent;
7525 p->parent = t;
7526 r->parent = t;
7527 t->parent = s;
7528 if (s)
7529 {
7530 if (s->left == p)
7531 s->left = t;
7532 else
7533 s->right = t;
7534 }
7535 else
7536 constructor_pending_elts = t;
7537 }
7538 break;
7539 }
7540 else
7541 {
7542 /* p->balance == -1; growth of right side balances the node. */
7543 p->balance = 0;
7544 break;
7545 }
7546 }
7547
7548 r = p;
7549 p = p->parent;
7550 }
7551}
7552
7553/* Build AVL tree from a sorted chain. */
7554
7555static void
a1e3b3d9 7556set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 7557{
4038c495
GB
7558 unsigned HOST_WIDE_INT ix;
7559 tree index, value;
3e4093b6
RS
7560
7561 if (TREE_CODE (constructor_type) != RECORD_TYPE
7562 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7563 return;
7564
4038c495 7565 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
a1e3b3d9 7566 {
fdce1719 7567 add_pending_init (index, value, NULL_TREE, true,
a1e3b3d9
LB
7568 braced_init_obstack);
7569 }
9771b263 7570 constructor_elements = NULL;
3e4093b6
RS
7571 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7572 {
7573 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7574 /* Skip any nameless bit fields at the beginning. */
7575 while (constructor_unfilled_fields != 0
7576 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7577 && DECL_NAME (constructor_unfilled_fields) == 0)
7578 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 7579
de520661 7580 }
3e4093b6 7581 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7582 {
3e4093b6
RS
7583 if (TYPE_DOMAIN (constructor_type))
7584 constructor_unfilled_index
7585 = convert (bitsizetype,
7586 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7587 else
7588 constructor_unfilled_index = bitsize_zero_node;
de520661 7589 }
3e4093b6 7590 constructor_incremental = 0;
de520661 7591}
400fbf9f 7592
3e4093b6 7593/* Build AVL tree from a string constant. */
de520661 7594
3e4093b6 7595static void
a1e3b3d9
LB
7596set_nonincremental_init_from_string (tree str,
7597 struct obstack * braced_init_obstack)
de520661 7598{
3e4093b6
RS
7599 tree value, purpose, type;
7600 HOST_WIDE_INT val[2];
7601 const char *p, *end;
7602 int byte, wchar_bytes, charwidth, bitpos;
de520661 7603
366de0ce 7604 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 7605
c466b2cd 7606 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
7607 charwidth = TYPE_PRECISION (char_type_node);
7608 type = TREE_TYPE (constructor_type);
7609 p = TREE_STRING_POINTER (str);
7610 end = p + TREE_STRING_LENGTH (str);
91fa3c30 7611
3e4093b6 7612 for (purpose = bitsize_zero_node;
8824edff
JJ
7613 p < end
7614 && !(constructor_max_index
7615 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 7616 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 7617 {
3e4093b6 7618 if (wchar_bytes == 1)
ffc5c6a9 7619 {
3e4093b6
RS
7620 val[1] = (unsigned char) *p++;
7621 val[0] = 0;
ffc5c6a9
RH
7622 }
7623 else
3e4093b6
RS
7624 {
7625 val[0] = 0;
7626 val[1] = 0;
7627 for (byte = 0; byte < wchar_bytes; byte++)
7628 {
7629 if (BYTES_BIG_ENDIAN)
7630 bitpos = (wchar_bytes - byte - 1) * charwidth;
7631 else
7632 bitpos = byte * charwidth;
7633 val[bitpos < HOST_BITS_PER_WIDE_INT]
7634 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7635 << (bitpos % HOST_BITS_PER_WIDE_INT);
7636 }
7637 }
584ef5fe 7638
8df83eae 7639 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
7640 {
7641 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7642 if (bitpos < HOST_BITS_PER_WIDE_INT)
7643 {
7644 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7645 {
7646 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7647 val[0] = -1;
7648 }
7649 }
7650 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7651 {
7652 if (val[1] < 0)
c22cacf3 7653 val[0] = -1;
3e4093b6
RS
7654 }
7655 else if (val[0] & (((HOST_WIDE_INT) 1)
7656 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7657 val[0] |= ((HOST_WIDE_INT) -1)
7658 << (bitpos - HOST_BITS_PER_WIDE_INT);
7659 }
ffc5c6a9 7660
7d60be94 7661 value = build_int_cst_wide (type, val[1], val[0]);
fdce1719 7662 add_pending_init (purpose, value, NULL_TREE, true,
a1e3b3d9 7663 braced_init_obstack);
9dfcc8db
BH
7664 }
7665
3e4093b6
RS
7666 constructor_incremental = 0;
7667}
de520661 7668
3e4093b6
RS
7669/* Return value of FIELD in pending initializer or zero if the field was
7670 not initialized yet. */
7671
7672static tree
a1e3b3d9 7673find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
7674{
7675 struct init_node *p;
7676
7677 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 7678 {
3e4093b6
RS
7679 if (constructor_incremental
7680 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 7681 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
7682
7683 p = constructor_pending_elts;
7684 while (p)
19d76e60 7685 {
3e4093b6
RS
7686 if (tree_int_cst_lt (field, p->purpose))
7687 p = p->left;
7688 else if (tree_int_cst_lt (p->purpose, field))
7689 p = p->right;
7690 else
7691 return p->value;
19d76e60 7692 }
19d76e60 7693 }
3e4093b6 7694 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 7695 {
3e4093b6 7696 tree bitpos = bit_position (field);
de520661 7697
3e4093b6
RS
7698 if (constructor_incremental
7699 && (!constructor_unfilled_fields
7700 || tree_int_cst_lt (bitpos,
7701 bit_position (constructor_unfilled_fields))))
a1e3b3d9 7702 set_nonincremental_init (braced_init_obstack);
de520661 7703
3e4093b6
RS
7704 p = constructor_pending_elts;
7705 while (p)
7706 {
7707 if (field == p->purpose)
7708 return p->value;
7709 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7710 p = p->left;
7711 else
7712 p = p->right;
7713 }
7714 }
7715 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 7716 {
9771b263
DN
7717 if (!vec_safe_is_empty (constructor_elements)
7718 && (constructor_elements->last ().index == field))
7719 return constructor_elements->last ().value;
de520661 7720 }
3e4093b6 7721 return 0;
de520661
RS
7722}
7723
3e4093b6
RS
7724/* "Output" the next constructor element.
7725 At top level, really output it to assembler code now.
7726 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 7727 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
7728 TYPE is the data type that the containing data type wants here.
7729 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
7730 If VALUE is a string constant, STRICT_STRING is true if it is
7731 unparenthesized or we should not warn here for it being parenthesized.
7732 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 7733
3e4093b6
RS
7734 PENDING if non-nil means output pending elements that belong
7735 right after this element. (PENDING is normally 1;
b295aee2
JJ
7736 it is 0 while outputting pending elements, to avoid recursion.)
7737
7738 IMPLICIT is true if value comes from pop_init_level (1),
7739 the new initializer has been merged with the existing one
7740 and thus no warnings should be emitted about overriding an
7741 existing initializer. */
8b6a5902 7742
3e4093b6 7743static void
bbbbb16a 7744output_init_element (tree value, tree origtype, bool strict_string, tree type,
a1e3b3d9
LB
7745 tree field, int pending, bool implicit,
7746 struct obstack * braced_init_obstack)
3e4093b6 7747{
8ce94e44 7748 tree semantic_type = NULL_TREE;
928c19bb
JM
7749 bool maybe_const = true;
7750 bool npc;
4038c495 7751
0a880880 7752 if (type == error_mark_node || value == error_mark_node)
8b6a5902 7753 {
3e4093b6
RS
7754 constructor_erroneous = 1;
7755 return;
8b6a5902 7756 }
46bdb9cf
JM
7757 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7758 && (TREE_CODE (value) == STRING_CST
7759 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7760 && !(TREE_CODE (value) == STRING_CST
7761 && TREE_CODE (type) == ARRAY_TYPE
7762 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7763 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7764 TYPE_MAIN_VARIANT (type)))
c2255bc4 7765 value = array_to_pointer_conversion (input_location, value);
8b6a5902 7766
3e4093b6
RS
7767 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7768 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 7769 {
3e4093b6
RS
7770 /* As an extension, allow initializing objects with static storage
7771 duration with compound literals (which are then treated just as
7772 the brace enclosed list they contain). */
7773 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7774 value = DECL_INITIAL (decl);
8b6a5902
JJ
7775 }
7776
928c19bb 7777 npc = null_pointer_constant_p (value);
8ce94e44
JM
7778 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7779 {
7780 semantic_type = TREE_TYPE (value);
7781 value = TREE_OPERAND (value, 0);
7782 }
928c19bb
JM
7783 value = c_fully_fold (value, require_constant_value, &maybe_const);
7784
3e4093b6
RS
7785 if (value == error_mark_node)
7786 constructor_erroneous = 1;
7787 else if (!TREE_CONSTANT (value))
7788 constructor_constant = 0;
116df786 7789 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
7790 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7791 || TREE_CODE (constructor_type) == UNION_TYPE)
7792 && DECL_C_BIT_FIELD (field)
7793 && TREE_CODE (value) != INTEGER_CST))
7794 constructor_simple = 0;
928c19bb
JM
7795 if (!maybe_const)
7796 constructor_nonconst = 1;
3e4093b6 7797
116df786 7798 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 7799 {
116df786
RH
7800 if (require_constant_value)
7801 {
7802 error_init ("initializer element is not constant");
7803 value = error_mark_node;
7804 }
7805 else if (require_constant_elements)
509c9d60
MLI
7806 pedwarn (input_location, 0,
7807 "initializer element is not computable at load time");
8b6a5902 7808 }
928c19bb
JM
7809 else if (!maybe_const
7810 && (require_constant_value || require_constant_elements))
7811 pedwarn_init (input_location, 0,
7812 "initializer element is not a constant expression");
3e4093b6 7813
81f40b79
ILT
7814 /* Issue -Wc++-compat warnings about initializing a bitfield with
7815 enum type. */
7816 if (warn_cxx_compat
7817 && field != NULL_TREE
7818 && TREE_CODE (field) == FIELD_DECL
7819 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7820 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7821 != TYPE_MAIN_VARIANT (type))
7822 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7823 {
7824 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7825 if (checktype != error_mark_node
7826 && (TYPE_MAIN_VARIANT (checktype)
7827 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7828 warning_init (OPT_Wc___compat,
7829 "enum conversion in initialization is invalid in C++");
7830 }
7831
3e4093b6
RS
7832 /* If this field is empty (and not at the end of structure),
7833 don't do anything other than checking the initializer. */
7834 if (field
7835 && (TREE_TYPE (field) == error_mark_node
7836 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7837 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7838 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 7839 || DECL_CHAIN (field)))))
3e4093b6
RS
7840 return;
7841
8ce94e44
JM
7842 if (semantic_type)
7843 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
c2255bc4
AH
7844 value = digest_init (input_location, type, value, origtype, npc,
7845 strict_string, require_constant_value);
3e4093b6 7846 if (value == error_mark_node)
8b6a5902 7847 {
3e4093b6
RS
7848 constructor_erroneous = 1;
7849 return;
8b6a5902 7850 }
928c19bb
JM
7851 if (require_constant_value || require_constant_elements)
7852 constant_expression_warning (value);
8b6a5902 7853
3e4093b6
RS
7854 /* If this element doesn't come next in sequence,
7855 put it on constructor_pending_elts. */
7856 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7857 && (!constructor_incremental
7858 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 7859 {
3e4093b6
RS
7860 if (constructor_incremental
7861 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 7862 set_nonincremental_init (braced_init_obstack);
3e4093b6 7863
a1e3b3d9
LB
7864 add_pending_init (field, value, origtype, implicit,
7865 braced_init_obstack);
3e4093b6 7866 return;
8b6a5902 7867 }
3e4093b6
RS
7868 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7869 && (!constructor_incremental
7870 || field != constructor_unfilled_fields))
8b6a5902 7871 {
3e4093b6
RS
7872 /* We do this for records but not for unions. In a union,
7873 no matter which field is specified, it can be initialized
7874 right away since it starts at the beginning of the union. */
7875 if (constructor_incremental)
7876 {
7877 if (!constructor_unfilled_fields)
a1e3b3d9 7878 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
7879 else
7880 {
7881 tree bitpos, unfillpos;
7882
7883 bitpos = bit_position (field);
7884 unfillpos = bit_position (constructor_unfilled_fields);
7885
7886 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 7887 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
7888 }
7889 }
7890
a1e3b3d9
LB
7891 add_pending_init (field, value, origtype, implicit,
7892 braced_init_obstack);
3e4093b6 7893 return;
8b6a5902 7894 }
3e4093b6 7895 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 7896 && !vec_safe_is_empty (constructor_elements))
3e4093b6 7897 {
b295aee2
JJ
7898 if (!implicit)
7899 {
9771b263 7900 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
b295aee2
JJ
7901 warning_init (0,
7902 "initialized field with side-effects overwritten");
7903 else if (warn_override_init)
7904 warning_init (OPT_Woverride_init, "initialized field overwritten");
7905 }
8b6a5902 7906
3e4093b6 7907 /* We can have just one union field set. */
9771b263 7908 constructor_elements = NULL;
3e4093b6 7909 }
8b6a5902 7910
3e4093b6
RS
7911 /* Otherwise, output this element either to
7912 constructor_elements or to the assembler file. */
8b6a5902 7913
f32682ca 7914 constructor_elt celt = {field, value};
9771b263 7915 vec_safe_push (constructor_elements, celt);
8b6a5902 7916
3e4093b6
RS
7917 /* Advance the variable that indicates sequential elements output. */
7918 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7919 constructor_unfilled_index
db3927fb
AH
7920 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7921 bitsize_one_node);
3e4093b6
RS
7922 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7923 {
7924 constructor_unfilled_fields
910ad8de 7925 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 7926
3e4093b6
RS
7927 /* Skip any nameless bit fields. */
7928 while (constructor_unfilled_fields != 0
7929 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7930 && DECL_NAME (constructor_unfilled_fields) == 0)
7931 constructor_unfilled_fields =
910ad8de 7932 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
7933 }
7934 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7935 constructor_unfilled_fields = 0;
de520661 7936
3e4093b6
RS
7937 /* Now output any pending elements which have become next. */
7938 if (pending)
a1e3b3d9 7939 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 7940}
8b6a5902 7941
3e4093b6
RS
7942/* Output any pending elements which have become next.
7943 As we output elements, constructor_unfilled_{fields,index}
7944 advances, which may cause other elements to become next;
7945 if so, they too are output.
8b6a5902 7946
3e4093b6
RS
7947 If ALL is 0, we return when there are
7948 no more pending elements to output now.
665f2503 7949
3e4093b6
RS
7950 If ALL is 1, we output space as necessary so that
7951 we can output all the pending elements. */
3e4093b6 7952static void
a1e3b3d9 7953output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
7954{
7955 struct init_node *elt = constructor_pending_elts;
7956 tree next;
de520661 7957
3e4093b6
RS
7958 retry:
7959
ba228239 7960 /* Look through the whole pending tree.
3e4093b6
RS
7961 If we find an element that should be output now,
7962 output it. Otherwise, set NEXT to the element
7963 that comes first among those still pending. */
7964
7965 next = 0;
7966 while (elt)
7967 {
7968 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 7969 {
3e4093b6
RS
7970 if (tree_int_cst_equal (elt->purpose,
7971 constructor_unfilled_index))
bbbbb16a 7972 output_init_element (elt->value, elt->origtype, true,
3e4093b6 7973 TREE_TYPE (constructor_type),
a1e3b3d9
LB
7974 constructor_unfilled_index, 0, false,
7975 braced_init_obstack);
3e4093b6
RS
7976 else if (tree_int_cst_lt (constructor_unfilled_index,
7977 elt->purpose))
8b6a5902 7978 {
3e4093b6
RS
7979 /* Advance to the next smaller node. */
7980 if (elt->left)
7981 elt = elt->left;
7982 else
7983 {
7984 /* We have reached the smallest node bigger than the
7985 current unfilled index. Fill the space first. */
7986 next = elt->purpose;
7987 break;
7988 }
8b6a5902 7989 }
ce662d4c
JJ
7990 else
7991 {
3e4093b6
RS
7992 /* Advance to the next bigger node. */
7993 if (elt->right)
7994 elt = elt->right;
7995 else
ce662d4c 7996 {
3e4093b6
RS
7997 /* We have reached the biggest node in a subtree. Find
7998 the parent of it, which is the next bigger node. */
7999 while (elt->parent && elt->parent->right == elt)
8000 elt = elt->parent;
8001 elt = elt->parent;
8002 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8003 elt->purpose))
8004 {
8005 next = elt->purpose;
8006 break;
8007 }
ce662d4c
JJ
8008 }
8009 }
8b6a5902 8010 }
3e4093b6
RS
8011 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8012 || TREE_CODE (constructor_type) == UNION_TYPE)
8013 {
8014 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8015
3e4093b6
RS
8016 /* If the current record is complete we are done. */
8017 if (constructor_unfilled_fields == 0)
8018 break;
de520661 8019
3e4093b6
RS
8020 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8021 elt_bitpos = bit_position (elt->purpose);
8022 /* We can't compare fields here because there might be empty
8023 fields in between. */
8024 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8025 {
8026 constructor_unfilled_fields = elt->purpose;
bbbbb16a
ILT
8027 output_init_element (elt->value, elt->origtype, true,
8028 TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8029 elt->purpose, 0, false,
8030 braced_init_obstack);
3e4093b6
RS
8031 }
8032 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8033 {
8034 /* Advance to the next smaller node. */
8035 if (elt->left)
8036 elt = elt->left;
8037 else
8038 {
8039 /* We have reached the smallest node bigger than the
8040 current unfilled field. Fill the space first. */
8041 next = elt->purpose;
8042 break;
8043 }
8044 }
8045 else
8046 {
8047 /* Advance to the next bigger node. */
8048 if (elt->right)
8049 elt = elt->right;
8050 else
8051 {
8052 /* We have reached the biggest node in a subtree. Find
8053 the parent of it, which is the next bigger node. */
8054 while (elt->parent && elt->parent->right == elt)
8055 elt = elt->parent;
8056 elt = elt->parent;
8057 if (elt
8058 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8059 bit_position (elt->purpose))))
8060 {
8061 next = elt->purpose;
8062 break;
8063 }
8064 }
8065 }
8066 }
8067 }
de520661 8068
3e4093b6
RS
8069 /* Ordinarily return, but not if we want to output all
8070 and there are elements left. */
3f75a254 8071 if (!(all && next != 0))
e5cfb88f
RK
8072 return;
8073
3e4093b6
RS
8074 /* If it's not incremental, just skip over the gap, so that after
8075 jumping to retry we will output the next successive element. */
8076 if (TREE_CODE (constructor_type) == RECORD_TYPE
8077 || TREE_CODE (constructor_type) == UNION_TYPE)
8078 constructor_unfilled_fields = next;
8079 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8080 constructor_unfilled_index = next;
de520661 8081
3e4093b6
RS
8082 /* ELT now points to the node in the pending tree with the next
8083 initializer to output. */
8084 goto retry;
de520661
RS
8085}
8086\f
3e4093b6
RS
8087/* Add one non-braced element to the current constructor level.
8088 This adjusts the current position within the constructor's type.
8089 This may also start or terminate implicit levels
8090 to handle a partly-braced initializer.
e5e809f4 8091
3e4093b6 8092 Once this has found the correct level for the new element,
b295aee2
JJ
8093 it calls output_init_element.
8094
8095 IMPLICIT is true if value comes from pop_init_level (1),
8096 the new initializer has been merged with the existing one
8097 and thus no warnings should be emitted about overriding an
8098 existing initializer. */
3e4093b6
RS
8099
8100void
a1e3b3d9
LB
8101process_init_element (struct c_expr value, bool implicit,
8102 struct obstack * braced_init_obstack)
e5e809f4 8103{
916c5919
JM
8104 tree orig_value = value.value;
8105 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8106 bool strict_string = value.original_code == STRING_CST;
e5e809f4 8107
3e4093b6 8108 designator_depth = 0;
b06df647 8109 designator_erroneous = 0;
e5e809f4 8110
3e4093b6
RS
8111 /* Handle superfluous braces around string cst as in
8112 char x[] = {"foo"}; */
8113 if (string_flag
8114 && constructor_type
8115 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8116 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8117 && integer_zerop (constructor_unfilled_index))
e5e809f4 8118 {
916c5919 8119 if (constructor_stack->replacement_value.value)
c22cacf3 8120 error_init ("excess elements in char array initializer");
3e4093b6
RS
8121 constructor_stack->replacement_value = value;
8122 return;
e5e809f4 8123 }
8b6a5902 8124
916c5919 8125 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
8126 {
8127 error_init ("excess elements in struct initializer");
8128 return;
e5e809f4
JL
8129 }
8130
3e4093b6
RS
8131 /* Ignore elements of a brace group if it is entirely superfluous
8132 and has already been diagnosed. */
8133 if (constructor_type == 0)
8134 return;
e5e809f4 8135
3e4093b6
RS
8136 /* If we've exhausted any levels that didn't have braces,
8137 pop them now. */
8138 while (constructor_stack->implicit)
8139 {
8140 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8141 || TREE_CODE (constructor_type) == UNION_TYPE)
8142 && constructor_fields == 0)
a1e3b3d9
LB
8143 process_init_element (pop_init_level (1, braced_init_obstack),
8144 true, braced_init_obstack);
53650abe
AP
8145 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8146 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8147 && constructor_max_index
8148 && tree_int_cst_lt (constructor_max_index,
8149 constructor_index))
a1e3b3d9
LB
8150 process_init_element (pop_init_level (1, braced_init_obstack),
8151 true, braced_init_obstack);
3e4093b6
RS
8152 else
8153 break;
8154 }
e5e809f4 8155
3e4093b6
RS
8156 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8157 if (constructor_range_stack)
e5e809f4 8158 {
3e4093b6
RS
8159 /* If value is a compound literal and we'll be just using its
8160 content, don't put it into a SAVE_EXPR. */
916c5919 8161 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8162 || !require_constant_value
8163 || flag_isoc99)
8ce94e44
JM
8164 {
8165 tree semantic_type = NULL_TREE;
8166 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8167 {
8168 semantic_type = TREE_TYPE (value.value);
8169 value.value = TREE_OPERAND (value.value, 0);
8170 }
8171 value.value = c_save_expr (value.value);
8172 if (semantic_type)
8173 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8174 value.value);
8175 }
3e4093b6 8176 }
e5e809f4 8177
3e4093b6
RS
8178 while (1)
8179 {
8180 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8181 {
3e4093b6
RS
8182 tree fieldtype;
8183 enum tree_code fieldcode;
e5e809f4 8184
3e4093b6
RS
8185 if (constructor_fields == 0)
8186 {
509c9d60
MLI
8187 pedwarn_init (input_location, 0,
8188 "excess elements in struct initializer");
3e4093b6
RS
8189 break;
8190 }
e5e809f4 8191
3e4093b6
RS
8192 fieldtype = TREE_TYPE (constructor_fields);
8193 if (fieldtype != error_mark_node)
8194 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8195 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8196
3e4093b6
RS
8197 /* Error for non-static initialization of a flexible array member. */
8198 if (fieldcode == ARRAY_TYPE
8199 && !require_constant_value
8200 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8201 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6
RS
8202 {
8203 error_init ("non-static initialization of a flexible array member");
8204 break;
8205 }
e5e809f4 8206
3e4093b6 8207 /* Accept a string constant to initialize a subarray. */
916c5919 8208 if (value.value != 0
3e4093b6 8209 && fieldcode == ARRAY_TYPE
197463ae 8210 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8211 && string_flag)
916c5919 8212 value.value = orig_value;
3e4093b6
RS
8213 /* Otherwise, if we have come to a subaggregate,
8214 and we don't have an element of its type, push into it. */
0953878d 8215 else if (value.value != 0
916c5919
JM
8216 && value.value != error_mark_node
8217 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8218 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8219 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8220 {
a1e3b3d9 8221 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8222 continue;
8223 }
e5e809f4 8224
916c5919 8225 if (value.value)
3e4093b6
RS
8226 {
8227 push_member_name (constructor_fields);
bbbbb16a
ILT
8228 output_init_element (value.value, value.original_type,
8229 strict_string, fieldtype,
a1e3b3d9
LB
8230 constructor_fields, 1, implicit,
8231 braced_init_obstack);
3e4093b6 8232 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8233 }
8234 else
3e4093b6
RS
8235 /* Do the bookkeeping for an element that was
8236 directly output as a constructor. */
e5e809f4 8237 {
3e4093b6
RS
8238 /* For a record, keep track of end position of last field. */
8239 if (DECL_SIZE (constructor_fields))
c22cacf3 8240 constructor_bit_index
db3927fb
AH
8241 = size_binop_loc (input_location, PLUS_EXPR,
8242 bit_position (constructor_fields),
8243 DECL_SIZE (constructor_fields));
3e4093b6
RS
8244
8245 /* If the current field was the first one not yet written out,
8246 it isn't now, so update. */
8247 if (constructor_unfilled_fields == constructor_fields)
8248 {
910ad8de 8249 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8250 /* Skip any nameless bit fields. */
8251 while (constructor_unfilled_fields != 0
8252 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8253 && DECL_NAME (constructor_unfilled_fields) == 0)
8254 constructor_unfilled_fields =
910ad8de 8255 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8256 }
e5e809f4 8257 }
3e4093b6 8258
910ad8de 8259 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8260 /* Skip any nameless bit fields at the beginning. */
8261 while (constructor_fields != 0
8262 && DECL_C_BIT_FIELD (constructor_fields)
8263 && DECL_NAME (constructor_fields) == 0)
910ad8de 8264 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8265 }
3e4093b6 8266 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8267 {
3e4093b6
RS
8268 tree fieldtype;
8269 enum tree_code fieldcode;
e5e809f4 8270
3e4093b6
RS
8271 if (constructor_fields == 0)
8272 {
509c9d60
MLI
8273 pedwarn_init (input_location, 0,
8274 "excess elements in union initializer");
3e4093b6
RS
8275 break;
8276 }
e5e809f4 8277
3e4093b6
RS
8278 fieldtype = TREE_TYPE (constructor_fields);
8279 if (fieldtype != error_mark_node)
8280 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8281 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8282
3e4093b6
RS
8283 /* Warn that traditional C rejects initialization of unions.
8284 We skip the warning if the value is zero. This is done
8285 under the assumption that the zero initializer in user
8286 code appears conditioned on e.g. __STDC__ to avoid
8287 "missing initializer" warnings and relies on default
8288 initialization to zero in the traditional C case.
8289 We also skip the warning if the initializer is designated,
8290 again on the assumption that this must be conditional on
8291 __STDC__ anyway (and we've already complained about the
8292 member-designator already). */
3176a0c2 8293 if (!in_system_header && !constructor_designated
916c5919
JM
8294 && !(value.value && (integer_zerop (value.value)
8295 || real_zerop (value.value))))
3176a0c2
DD
8296 warning (OPT_Wtraditional, "traditional C rejects initialization "
8297 "of unions");
e5e809f4 8298
3e4093b6 8299 /* Accept a string constant to initialize a subarray. */
916c5919 8300 if (value.value != 0
3e4093b6 8301 && fieldcode == ARRAY_TYPE
197463ae 8302 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8303 && string_flag)
916c5919 8304 value.value = orig_value;
3e4093b6
RS
8305 /* Otherwise, if we have come to a subaggregate,
8306 and we don't have an element of its type, push into it. */
0953878d 8307 else if (value.value != 0
916c5919
JM
8308 && value.value != error_mark_node
8309 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8310 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8311 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8312 {
a1e3b3d9 8313 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8314 continue;
8315 }
e5e809f4 8316
916c5919 8317 if (value.value)
3e4093b6
RS
8318 {
8319 push_member_name (constructor_fields);
bbbbb16a
ILT
8320 output_init_element (value.value, value.original_type,
8321 strict_string, fieldtype,
a1e3b3d9
LB
8322 constructor_fields, 1, implicit,
8323 braced_init_obstack);
3e4093b6 8324 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8325 }
8326 else
3e4093b6
RS
8327 /* Do the bookkeeping for an element that was
8328 directly output as a constructor. */
e5e809f4 8329 {
3e4093b6 8330 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8331 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8332 }
e5e809f4 8333
3e4093b6
RS
8334 constructor_fields = 0;
8335 }
8336 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8337 {
8338 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8339 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8340
3e4093b6 8341 /* Accept a string constant to initialize a subarray. */
916c5919 8342 if (value.value != 0
3e4093b6 8343 && eltcode == ARRAY_TYPE
197463ae 8344 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8345 && string_flag)
916c5919 8346 value.value = orig_value;
3e4093b6
RS
8347 /* Otherwise, if we have come to a subaggregate,
8348 and we don't have an element of its type, push into it. */
0953878d 8349 else if (value.value != 0
916c5919
JM
8350 && value.value != error_mark_node
8351 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8352 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8353 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8354 {
a1e3b3d9 8355 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8356 continue;
8357 }
8b6a5902 8358
3e4093b6
RS
8359 if (constructor_max_index != 0
8360 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8361 || integer_all_onesp (constructor_max_index)))
8362 {
509c9d60
MLI
8363 pedwarn_init (input_location, 0,
8364 "excess elements in array initializer");
3e4093b6
RS
8365 break;
8366 }
8b6a5902 8367
3e4093b6 8368 /* Now output the actual element. */
916c5919 8369 if (value.value)
3e4093b6 8370 {
a0f0ab9f 8371 push_array_bounds (tree_low_cst (constructor_index, 1));
bbbbb16a
ILT
8372 output_init_element (value.value, value.original_type,
8373 strict_string, elttype,
a1e3b3d9
LB
8374 constructor_index, 1, implicit,
8375 braced_init_obstack);
3e4093b6
RS
8376 RESTORE_SPELLING_DEPTH (constructor_depth);
8377 }
2f6e4e97 8378
3e4093b6 8379 constructor_index
db3927fb
AH
8380 = size_binop_loc (input_location, PLUS_EXPR,
8381 constructor_index, bitsize_one_node);
8b6a5902 8382
916c5919 8383 if (!value.value)
3e4093b6
RS
8384 /* If we are doing the bookkeeping for an element that was
8385 directly output as a constructor, we must update
8386 constructor_unfilled_index. */
8387 constructor_unfilled_index = constructor_index;
8388 }
8389 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8390 {
8391 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8392
c22cacf3
MS
8393 /* Do a basic check of initializer size. Note that vectors
8394 always have a fixed size derived from their type. */
3e4093b6
RS
8395 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8396 {
509c9d60
MLI
8397 pedwarn_init (input_location, 0,
8398 "excess elements in vector initializer");
3e4093b6
RS
8399 break;
8400 }
8b6a5902 8401
3e4093b6 8402 /* Now output the actual element. */
916c5919 8403 if (value.value)
53650abe
AP
8404 {
8405 if (TREE_CODE (value.value) == VECTOR_CST)
8406 elttype = TYPE_MAIN_VARIANT (constructor_type);
8407 output_init_element (value.value, value.original_type,
8408 strict_string, elttype,
a1e3b3d9
LB
8409 constructor_index, 1, implicit,
8410 braced_init_obstack);
53650abe 8411 }
8b6a5902 8412
3e4093b6 8413 constructor_index
db3927fb
AH
8414 = size_binop_loc (input_location,
8415 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8416
916c5919 8417 if (!value.value)
3e4093b6
RS
8418 /* If we are doing the bookkeeping for an element that was
8419 directly output as a constructor, we must update
8420 constructor_unfilled_index. */
8421 constructor_unfilled_index = constructor_index;
8422 }
8b6a5902 8423
3e4093b6
RS
8424 /* Handle the sole element allowed in a braced initializer
8425 for a scalar variable. */
b4519d39
SB
8426 else if (constructor_type != error_mark_node
8427 && constructor_fields == 0)
8b6a5902 8428 {
509c9d60
MLI
8429 pedwarn_init (input_location, 0,
8430 "excess elements in scalar initializer");
3e4093b6 8431 break;
8b6a5902
JJ
8432 }
8433 else
8434 {
916c5919 8435 if (value.value)
bbbbb16a
ILT
8436 output_init_element (value.value, value.original_type,
8437 strict_string, constructor_type,
a1e3b3d9
LB
8438 NULL_TREE, 1, implicit,
8439 braced_init_obstack);
3e4093b6 8440 constructor_fields = 0;
8b6a5902
JJ
8441 }
8442
3e4093b6
RS
8443 /* Handle range initializers either at this level or anywhere higher
8444 in the designator stack. */
8445 if (constructor_range_stack)
8b6a5902 8446 {
3e4093b6
RS
8447 struct constructor_range_stack *p, *range_stack;
8448 int finish = 0;
8449
8450 range_stack = constructor_range_stack;
8451 constructor_range_stack = 0;
8452 while (constructor_stack != range_stack->stack)
8b6a5902 8453 {
366de0ce 8454 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8455 process_init_element (pop_init_level (1,
8456 braced_init_obstack),
8457 true, braced_init_obstack);
8b6a5902 8458 }
3e4093b6
RS
8459 for (p = range_stack;
8460 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8461 p = p->prev)
8b6a5902 8462 {
366de0ce 8463 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8464 process_init_element (pop_init_level (1, braced_init_obstack),
8465 true, braced_init_obstack);
8b6a5902 8466 }
3e4093b6 8467
db3927fb
AH
8468 p->index = size_binop_loc (input_location,
8469 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8470 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8471 finish = 1;
8472
8473 while (1)
8474 {
8475 constructor_index = p->index;
8476 constructor_fields = p->fields;
8477 if (finish && p->range_end && p->index == p->range_start)
8478 {
8479 finish = 0;
8480 p->prev = 0;
8481 }
8482 p = p->next;
8483 if (!p)
8484 break;
a1e3b3d9 8485 push_init_level (2, braced_init_obstack);
3e4093b6
RS
8486 p->stack = constructor_stack;
8487 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8488 p->index = p->range_start;
8489 }
8490
8491 if (!finish)
8492 constructor_range_stack = range_stack;
8493 continue;
8b6a5902
JJ
8494 }
8495
3e4093b6 8496 break;
8b6a5902
JJ
8497 }
8498
3e4093b6
RS
8499 constructor_range_stack = 0;
8500}
8501\f
9f0e2d86
ZW
8502/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8503 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8504 an ASM_EXPR node). */
3e4093b6 8505tree
9f0e2d86 8506build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 8507{
6de9cd9a
DN
8508 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8509 ASM_VOLATILE_P (args) = 1;
9f0e2d86 8510 return add_stmt (args);
8b6a5902
JJ
8511}
8512
9f0e2d86
ZW
8513/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8514 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8515 SIMPLE indicates whether there was anything at all after the
8516 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 8517 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 8518tree
c2255bc4 8519build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 8520 tree clobbers, tree labels, bool simple)
e5e809f4 8521{
3e4093b6 8522 tree tail;
9f0e2d86 8523 tree args;
6de9cd9a
DN
8524 int i;
8525 const char *constraint;
74f0c611 8526 const char **oconstraints;
6de9cd9a 8527 bool allows_mem, allows_reg, is_inout;
74f0c611 8528 int ninputs, noutputs;
6de9cd9a
DN
8529
8530 ninputs = list_length (inputs);
8531 noutputs = list_length (outputs);
74f0c611
RH
8532 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8533
1c384bf1 8534 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 8535
6de9cd9a
DN
8536 /* Remove output conversions that change the type but not the mode. */
8537 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 8538 {
3e4093b6 8539 tree output = TREE_VALUE (tail);
74f0c611 8540
eadd3d0d
JJ
8541 output = c_fully_fold (output, false, NULL);
8542
74f0c611
RH
8543 /* ??? Really, this should not be here. Users should be using a
8544 proper lvalue, dammit. But there's a long history of using casts
8545 in the output operands. In cases like longlong.h, this becomes a
8546 primitive form of typechecking -- if the cast can be removed, then
8547 the output operand had a type of the proper width; otherwise we'll
8548 get an error. Gross, but ... */
3e4093b6 8549 STRIP_NOPS (output);
74f0c611 8550
7bd11157 8551 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 8552 output = error_mark_node;
8b6a5902 8553
5544530a
PB
8554 if (output != error_mark_node
8555 && (TREE_READONLY (output)
8556 || TYPE_READONLY (TREE_TYPE (output))
8557 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8558 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8559 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8560 readonly_error (output, lv_asm);
8561
6de9cd9a 8562 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
8563 oconstraints[i] = constraint;
8564
8565 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8566 &allows_mem, &allows_reg, &is_inout))
8567 {
8568 /* If the operand is going to end up in memory,
8569 mark it addressable. */
8570 if (!allows_reg && !c_mark_addressable (output))
8571 output = error_mark_node;
bae5cddf
JJ
8572 if (!(!allows_reg && allows_mem)
8573 && output != error_mark_node
8574 && VOID_TYPE_P (TREE_TYPE (output)))
8575 {
8576 error_at (loc, "invalid use of void expression");
8577 output = error_mark_node;
8578 }
74f0c611
RH
8579 }
8580 else
c22cacf3 8581 output = error_mark_node;
3e4093b6 8582
74f0c611 8583 TREE_VALUE (tail) = output;
8b6a5902 8584 }
3e4093b6 8585
74f0c611
RH
8586 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8587 {
8588 tree input;
8589
8590 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8591 input = TREE_VALUE (tail);
8592
74f0c611
RH
8593 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8594 oconstraints, &allows_mem, &allows_reg))
8595 {
8596 /* If the operand is going to end up in memory,
8597 mark it addressable. */
b4c33883
AP
8598 if (!allows_reg && allows_mem)
8599 {
eadd3d0d
JJ
8600 input = c_fully_fold (input, false, NULL);
8601
b4c33883
AP
8602 /* Strip the nops as we allow this case. FIXME, this really
8603 should be rejected or made deprecated. */
8604 STRIP_NOPS (input);
8605 if (!c_mark_addressable (input))
8606 input = error_mark_node;
bae5cddf 8607 }
eadd3d0d 8608 else
bae5cddf 8609 {
eadd3d0d
JJ
8610 struct c_expr expr;
8611 memset (&expr, 0, sizeof (expr));
8612 expr.value = input;
8613 expr = default_function_array_conversion (loc, expr);
8614 input = c_fully_fold (expr.value, false, NULL);
8615
8616 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8617 {
8618 error_at (loc, "invalid use of void expression");
8619 input = error_mark_node;
8620 }
bae5cddf 8621 }
74f0c611
RH
8622 }
8623 else
8624 input = error_mark_node;
8625
8626 TREE_VALUE (tail) = input;
8627 }
3e4093b6 8628
1c384bf1
RH
8629 /* ASMs with labels cannot have outputs. This should have been
8630 enforced by the parser. */
8631 gcc_assert (outputs == NULL || labels == NULL);
8632
8633 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 8634
5544530a
PB
8635 /* asm statements without outputs, including simple ones, are treated
8636 as volatile. */
8637 ASM_INPUT_P (args) = simple;
8638 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 8639
9f0e2d86 8640 return args;
e5e809f4 8641}
3e4093b6 8642\f
c2255bc4
AH
8643/* Generate a goto statement to LABEL. LOC is the location of the
8644 GOTO. */
506e2710
RH
8645
8646tree
c2255bc4 8647c_finish_goto_label (location_t loc, tree label)
506e2710 8648{
e1b7793c 8649 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
8650 if (!decl)
8651 return NULL_TREE;
506e2710 8652 TREE_USED (decl) = 1;
c2255bc4
AH
8653 {
8654 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8655 SET_EXPR_LOCATION (t, loc);
8656 return add_stmt (t);
8657 }
506e2710
RH
8658}
8659
c2255bc4
AH
8660/* Generate a computed goto statement to EXPR. LOC is the location of
8661 the GOTO. */
506e2710
RH
8662
8663tree
c2255bc4 8664c_finish_goto_ptr (location_t loc, tree expr)
506e2710 8665{
c2255bc4 8666 tree t;
c1771a20 8667 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 8668 expr = c_fully_fold (expr, false, NULL);
506e2710 8669 expr = convert (ptr_type_node, expr);
c2255bc4
AH
8670 t = build1 (GOTO_EXPR, void_type_node, expr);
8671 SET_EXPR_LOCATION (t, loc);
8672 return add_stmt (t);
506e2710
RH
8673}
8674
5088b058 8675/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
8676 to return, or a null pointer for `return;' with no value. LOC is
8677 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8678 is the original type of RETVAL. */
de520661 8679
506e2710 8680tree
c2255bc4 8681c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 8682{
0c9b182b
JJ
8683 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8684 bool no_warning = false;
928c19bb 8685 bool npc = false;
36536d79 8686 size_t rank = 0;
3e4093b6
RS
8687
8688 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
8689 warning_at (loc, 0,
8690 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 8691
36536d79
BI
8692 if (flag_enable_cilkplus && contains_array_notation_expr (retval))
8693 {
8694 /* Array notations are allowed in a return statement if it is inside a
8695 built-in array notation reduction function. */
8696 if (!find_rank (loc, retval, retval, false, &rank))
8697 return error_mark_node;
8698 if (rank >= 1)
8699 {
8700 error_at (loc, "array notation expression cannot be used as a "
8701 "return value");
8702 return error_mark_node;
8703 }
8704 }
939b37da
BI
8705 if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
8706 {
8707 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
8708 "allowed");
8709 return error_mark_node;
8710 }
928c19bb
JM
8711 if (retval)
8712 {
8ce94e44 8713 tree semantic_type = NULL_TREE;
928c19bb 8714 npc = null_pointer_constant_p (retval);
8ce94e44
JM
8715 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8716 {
8717 semantic_type = TREE_TYPE (retval);
8718 retval = TREE_OPERAND (retval, 0);
8719 }
928c19bb 8720 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
8721 if (semantic_type)
8722 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
8723 }
8724
3e4093b6 8725 if (!retval)
de520661 8726 {
3e4093b6
RS
8727 current_function_returns_null = 1;
8728 if ((warn_return_type || flag_isoc99)
8729 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 8730 {
b8698a0f 8731 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 8732 "%<return%> with no value, in "
0c9b182b
JJ
8733 "function returning non-void");
8734 no_warning = true;
8735 }
400fbf9f 8736 }
3e4093b6 8737 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 8738 {
3e4093b6 8739 current_function_returns_null = 1;
2397c575 8740 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 8741 pedwarn (loc, 0,
509c9d60 8742 "%<return%> with a value, in function returning void");
b8698a0f 8743 else
c1771a20 8744 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 8745 "%<return%> with expression, in function returning void");
de520661 8746 }
3e4093b6 8747 else
de520661 8748 {
c2255bc4
AH
8749 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8750 ic_return,
8751 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
8752 tree res = DECL_RESULT (current_function_decl);
8753 tree inner;
9feb29df 8754 bool save;
3e4093b6
RS
8755
8756 current_function_returns_value = 1;
8757 if (t == error_mark_node)
506e2710 8758 return NULL_TREE;
3e4093b6 8759
9feb29df
JJ
8760 save = in_late_binary_op;
8761 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8762 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8763 in_late_binary_op = true;
3e4093b6 8764 inner = t = convert (TREE_TYPE (res), t);
9feb29df 8765 in_late_binary_op = save;
3e4093b6
RS
8766
8767 /* Strip any conversions, additions, and subtractions, and see if
8768 we are returning the address of a local variable. Warn if so. */
8769 while (1)
8b6a5902 8770 {
3e4093b6 8771 switch (TREE_CODE (inner))
8b6a5902 8772 {
849421a3
JJ
8773 CASE_CONVERT:
8774 case NON_LVALUE_EXPR:
3e4093b6 8775 case PLUS_EXPR:
849421a3 8776 case POINTER_PLUS_EXPR:
3e4093b6
RS
8777 inner = TREE_OPERAND (inner, 0);
8778 continue;
8779
8780 case MINUS_EXPR:
8781 /* If the second operand of the MINUS_EXPR has a pointer
8782 type (or is converted from it), this may be valid, so
8783 don't give a warning. */
8784 {
8785 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 8786
3f75a254 8787 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
8788 && (CONVERT_EXPR_P (op1)
8789 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 8790 op1 = TREE_OPERAND (op1, 0);
8b6a5902 8791
3e4093b6
RS
8792 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8793 break;
8b6a5902 8794
3e4093b6
RS
8795 inner = TREE_OPERAND (inner, 0);
8796 continue;
8797 }
400fbf9f 8798
3e4093b6
RS
8799 case ADDR_EXPR:
8800 inner = TREE_OPERAND (inner, 0);
c2f4acb7 8801
6615c446 8802 while (REFERENCE_CLASS_P (inner)
c22cacf3 8803 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 8804 inner = TREE_OPERAND (inner, 0);
8b6a5902 8805
a2f1f4c3 8806 if (DECL_P (inner)
3f75a254
JM
8807 && !DECL_EXTERNAL (inner)
8808 && !TREE_STATIC (inner)
3e4093b6 8809 && DECL_CONTEXT (inner) == current_function_decl)
c2255bc4 8810 warning_at (loc,
880661a4
JW
8811 OPT_Wreturn_local_addr, "function returns address "
8812 "of local variable");
3e4093b6 8813 break;
8b6a5902 8814
3e4093b6
RS
8815 default:
8816 break;
8817 }
de520661 8818
3e4093b6
RS
8819 break;
8820 }
8821
53fb4de3 8822 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 8823 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
8824
8825 if (warn_sequence_point)
8826 verify_sequence_points (retval);
de520661 8827 }
8b6a5902 8828
c2255bc4 8829 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
8830 TREE_NO_WARNING (ret_stmt) |= no_warning;
8831 return add_stmt (ret_stmt);
de520661 8832}
3e4093b6
RS
8833\f
8834struct c_switch {
604f5adf
ILT
8835 /* The SWITCH_EXPR being built. */
8836 tree switch_expr;
a6c0a76c 8837
89dbed81 8838 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
8839 default conversion is applied. */
8840 tree orig_type;
8841
3e4093b6
RS
8842 /* A splay-tree mapping the low element of a case range to the high
8843 element, or NULL_TREE if there is no high element. Used to
8844 determine whether or not a new case label duplicates an old case
8845 label. We need a tree, rather than simply a hash table, because
8846 of the GNU case range extension. */
8847 splay_tree cases;
a6c0a76c 8848
e1b7793c
ILT
8849 /* The bindings at the point of the switch. This is used for
8850 warnings crossing decls when branching to a case label. */
8851 struct c_spot_bindings *bindings;
187230a7 8852
3e4093b6
RS
8853 /* The next node on the stack. */
8854 struct c_switch *next;
8855};
400fbf9f 8856
3e4093b6
RS
8857/* A stack of the currently active switch statements. The innermost
8858 switch statement is on the top of the stack. There is no need to
8859 mark the stack for garbage collection because it is only active
8860 during the processing of the body of a function, and we never
8861 collect at that point. */
de520661 8862
506e2710 8863struct c_switch *c_switch_stack;
de520661 8864
3e4093b6 8865/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4
AH
8866 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8867 SWITCH_COND_LOC is the location of the switch's condition. */
de520661 8868
3e4093b6 8869tree
c2255bc4
AH
8870c_start_case (location_t switch_loc,
8871 location_t switch_cond_loc,
8872 tree exp)
de520661 8873{
c58e8676 8874 tree orig_type = error_mark_node;
3e4093b6 8875 struct c_switch *cs;
2f6e4e97 8876
3e4093b6 8877 if (exp != error_mark_node)
de520661 8878 {
3e4093b6
RS
8879 orig_type = TREE_TYPE (exp);
8880
c58e8676 8881 if (!INTEGRAL_TYPE_P (orig_type))
de520661 8882 {
c58e8676
VR
8883 if (orig_type != error_mark_node)
8884 {
c2255bc4 8885 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
8886 orig_type = error_mark_node;
8887 }
3e4093b6 8888 exp = integer_zero_node;
de520661 8889 }
3e4093b6 8890 else
de520661 8891 {
c58e8676 8892 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 8893
3176a0c2 8894 if (!in_system_header
3e4093b6
RS
8895 && (type == long_integer_type_node
8896 || type == long_unsigned_type_node))
c2255bc4
AH
8897 warning_at (switch_cond_loc,
8898 OPT_Wtraditional, "%<long%> switch expression not "
8899 "converted to %<int%> in ISO C");
8b6a5902 8900
928c19bb 8901 exp = c_fully_fold (exp, false, NULL);
3e4093b6 8902 exp = default_conversion (exp);
ca085fd7
MLI
8903
8904 if (warn_sequence_point)
8905 verify_sequence_points (exp);
3e4093b6
RS
8906 }
8907 }
8908
604f5adf 8909 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 8910 cs = XNEW (struct c_switch);
604f5adf 8911 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 8912 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 8913 cs->orig_type = orig_type;
3e4093b6 8914 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 8915 cs->bindings = c_get_switch_bindings ();
506e2710
RH
8916 cs->next = c_switch_stack;
8917 c_switch_stack = cs;
3e4093b6 8918
604f5adf 8919 return add_stmt (cs->switch_expr);
3e4093b6
RS
8920}
8921
c2255bc4 8922/* Process a case label at location LOC. */
3e4093b6
RS
8923
8924tree
c2255bc4 8925do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
8926{
8927 tree label = NULL_TREE;
8928
17cede2e
JM
8929 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8930 {
8931 low_value = c_fully_fold (low_value, false, NULL);
8932 if (TREE_CODE (low_value) == INTEGER_CST)
c1771a20 8933 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
8934 "case label is not an integer constant expression");
8935 }
8936
8937 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8938 {
8939 high_value = c_fully_fold (high_value, false, NULL);
8940 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 8941 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
8942 "case label is not an integer constant expression");
8943 }
8944
e1b7793c 8945 if (c_switch_stack == NULL)
187230a7
JM
8946 {
8947 if (low_value)
e1b7793c 8948 error_at (loc, "case label not within a switch statement");
187230a7 8949 else
e1b7793c
ILT
8950 error_at (loc, "%<default%> label not within a switch statement");
8951 return NULL_TREE;
187230a7 8952 }
de520661 8953
e1b7793c
ILT
8954 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8955 EXPR_LOCATION (c_switch_stack->switch_expr),
8956 loc))
8957 return NULL_TREE;
8958
8959 label = c_add_case_label (loc, c_switch_stack->cases,
8960 SWITCH_COND (c_switch_stack->switch_expr),
8961 c_switch_stack->orig_type,
8962 low_value, high_value);
8963 if (label == error_mark_node)
8964 label = NULL_TREE;
3e4093b6
RS
8965 return label;
8966}
de520661 8967
3e4093b6 8968/* Finish the switch statement. */
de520661 8969
3e4093b6 8970void
325c3691 8971c_finish_case (tree body)
3e4093b6 8972{
506e2710 8973 struct c_switch *cs = c_switch_stack;
fbc315db 8974 location_t switch_location;
3e4093b6 8975
604f5adf 8976 SWITCH_BODY (cs->switch_expr) = body;
325c3691 8977
6de9cd9a 8978 /* Emit warnings as needed. */
c2255bc4 8979 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
8980 c_do_switch_warnings (cs->cases, switch_location,
8981 TREE_TYPE (cs->switch_expr),
8982 SWITCH_COND (cs->switch_expr));
6de9cd9a 8983
3e4093b6 8984 /* Pop the stack. */
506e2710 8985 c_switch_stack = cs->next;
3e4093b6 8986 splay_tree_delete (cs->cases);
e1b7793c 8987 c_release_switch_bindings (cs->bindings);
5d038c4c 8988 XDELETE (cs);
de520661 8989}
325c3691 8990\f
506e2710
RH
8991/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8992 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8993 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8994 statement, and was not surrounded with parenthesis. */
325c3691 8995
9e51cf9d 8996void
506e2710
RH
8997c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8998 tree else_block, bool nested_if)
325c3691 8999{
506e2710 9000 tree stmt;
325c3691 9001
25c22937
BI
9002 /* If the condition has array notations, then the rank of the then_block and
9003 else_block must be either 0 or be equal to the rank of the condition. If
9004 the condition does not have array notations then break them up as it is
9005 broken up in a normal expression. */
9006 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9007 {
9008 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9009 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9010 return;
9011 if (then_block
9012 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9013 return;
9014 if (else_block
9015 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9016 return;
9017 if (cond_rank != then_rank && then_rank != 0)
9018 {
9019 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9020 " and the then-block");
9021 return;
9022 }
9023 else if (cond_rank != else_rank && else_rank != 0)
9024 {
9025 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9026 " and the else-block");
9027 return;
9028 }
9029 }
506e2710
RH
9030 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9031 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9032 {
506e2710 9033 tree inner_if = then_block;
16865eaa 9034
61ada8ae 9035 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9036 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9037 it might not be exactly THEN_BLOCK, but should be the last
9038 non-container statement within. */
9039 while (1)
9040 switch (TREE_CODE (inner_if))
9041 {
9042 case COND_EXPR:
9043 goto found;
9044 case BIND_EXPR:
9045 inner_if = BIND_EXPR_BODY (inner_if);
9046 break;
9047 case STATEMENT_LIST:
9048 inner_if = expr_last (then_block);
9049 break;
9050 case TRY_FINALLY_EXPR:
9051 case TRY_CATCH_EXPR:
9052 inner_if = TREE_OPERAND (inner_if, 0);
9053 break;
9054 default:
366de0ce 9055 gcc_unreachable ();
506e2710
RH
9056 }
9057 found:
16865eaa 9058
506e2710 9059 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9060 warning_at (if_locus, OPT_Wparentheses,
9061 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9062 }
16865eaa 9063
2214de30 9064 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9065 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9066 add_stmt (stmt);
325c3691
RH
9067}
9068
506e2710
RH
9069/* Emit a general-purpose loop construct. START_LOCUS is the location of
9070 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9071 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9072 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9073 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9074
9075void
506e2710
RH
9076c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9077 tree blab, tree clab, bool cond_is_first)
325c3691 9078{
506e2710
RH
9079 tree entry = NULL, exit = NULL, t;
9080
36536d79
BI
9081 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9082 {
9083 error_at (start_locus, "array notation expression cannot be used in a "
9084 "loop%'s condition");
9085 return;
9086 }
9087
28af952a
RS
9088 /* If the condition is zero don't generate a loop construct. */
9089 if (cond && integer_zerop (cond))
9090 {
9091 if (cond_is_first)
9092 {
9093 t = build_and_jump (&blab);
9094 SET_EXPR_LOCATION (t, start_locus);
9095 add_stmt (t);
9096 }
9097 }
9098 else
506e2710
RH
9099 {
9100 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9101
506e2710 9102 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9103 out of the loop, or to the top of it. If there's no exit condition,
9104 then we just build a jump back to the top. */
506e2710 9105 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9106
28af952a 9107 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9108 {
9109 /* Canonicalize the loop condition to the end. This means
9110 generating a branch to the loop condition. Reuse the
9111 continue label, if possible. */
9112 if (cond_is_first)
9113 {
9114 if (incr || !clab)
9115 {
9116 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9117 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9118 }
9119 else
9120 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9121 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9122 add_stmt (t);
9123 }
9124
506e2710 9125 t = build_and_jump (&blab);
506e2710 9126 if (cond_is_first)
db3927fb
AH
9127 exit = fold_build3_loc (start_locus,
9128 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9129 else
db3927fb
AH
9130 exit = fold_build3_loc (input_location,
9131 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9132 }
9133
506e2710
RH
9134 add_stmt (top);
9135 }
c22cacf3 9136
506e2710
RH
9137 if (body)
9138 add_stmt (body);
9139 if (clab)
9140 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9141 if (incr)
9142 add_stmt (incr);
9143 if (entry)
9144 add_stmt (entry);
9145 if (exit)
9146 add_stmt (exit);
9147 if (blab)
9148 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9149}
325c3691
RH
9150
9151tree
c2255bc4 9152c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9153{
089efaa4 9154 bool skip;
506e2710 9155 tree label = *label_p;
325c3691 9156
089efaa4
ILT
9157 /* In switch statements break is sometimes stylistically used after
9158 a return statement. This can lead to spurious warnings about
9159 control reaching the end of a non-void function when it is
9160 inlined. Note that we are calling block_may_fallthru with
9161 language specific tree nodes; this works because
9162 block_may_fallthru returns true when given something it does not
9163 understand. */
9164 skip = !block_may_fallthru (cur_stmt_list);
9165
506e2710 9166 if (!label)
089efaa4
ILT
9167 {
9168 if (!skip)
c2255bc4 9169 *label_p = label = create_artificial_label (loc);
089efaa4 9170 }
953ff289
DN
9171 else if (TREE_CODE (label) == LABEL_DECL)
9172 ;
9173 else switch (TREE_INT_CST_LOW (label))
506e2710 9174 {
953ff289 9175 case 0:
506e2710 9176 if (is_break)
c2255bc4 9177 error_at (loc, "break statement not within loop or switch");
506e2710 9178 else
c2255bc4 9179 error_at (loc, "continue statement not within a loop");
506e2710 9180 return NULL_TREE;
953ff289
DN
9181
9182 case 1:
9183 gcc_assert (is_break);
c2255bc4 9184 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9185 return NULL_TREE;
9186
9187 default:
9188 gcc_unreachable ();
506e2710 9189 }
325c3691 9190
089efaa4
ILT
9191 if (skip)
9192 return NULL_TREE;
9193
2e28e797
JH
9194 if (!is_break)
9195 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9196
53fb4de3 9197 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9198}
9199
506e2710 9200/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9201
9202static void
c2255bc4 9203emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9204{
e6b5a630
RH
9205 if (expr == error_mark_node)
9206 ;
9207 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9208 {
9209 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9210 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9211 }
27f33b15 9212 else
c2255bc4 9213 warn_if_unused_value (expr, loc);
3a5b9284
RH
9214}
9215
506e2710 9216/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9217 diagnostics, but do not call ADD_STMT. LOC is the location of the
9218 statement. */
3a5b9284 9219
506e2710 9220tree
c2255bc4 9221c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9222{
056928b2
JJ
9223 tree exprv;
9224
3a5b9284 9225 if (!expr)
506e2710 9226 return NULL_TREE;
3a5b9284 9227
928c19bb
JM
9228 expr = c_fully_fold (expr, false, NULL);
9229
3a5b9284
RH
9230 if (warn_sequence_point)
9231 verify_sequence_points (expr);
9232
9233 if (TREE_TYPE (expr) != error_mark_node
9234 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9235 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9236 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9237
9238 /* If we're not processing a statement expression, warn about unused values.
9239 Warnings for statement expressions will be emitted later, once we figure
9240 out which is the result. */
9241 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9242 && warn_unused_value)
c2255bc4 9243 emit_side_effect_warnings (loc, expr);
3a5b9284 9244
056928b2
JJ
9245 exprv = expr;
9246 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9247 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9248 while (CONVERT_EXPR_P (exprv))
9249 exprv = TREE_OPERAND (exprv, 0);
9250 if (DECL_P (exprv)
9251 || handled_component_p (exprv)
9252 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9253 mark_exp_read (exprv);
fa8351f8 9254
3a5b9284
RH
9255 /* If the expression is not of a type to which we cannot assign a line
9256 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9257 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9258 {
9259 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9260 SET_EXPR_LOCATION (expr, loc);
9261 }
506e2710
RH
9262
9263 return expr;
9264}
9265
c2255bc4
AH
9266/* Emit an expression as a statement. LOC is the location of the
9267 expression. */
506e2710
RH
9268
9269tree
c2255bc4 9270c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9271{
9272 if (expr)
c2255bc4 9273 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9274 else
9275 return NULL;
3a5b9284
RH
9276}
9277
9278/* Do the opposite and emit a statement as an expression. To begin,
9279 create a new binding level and return it. */
325c3691
RH
9280
9281tree
9282c_begin_stmt_expr (void)
9283{
9284 tree ret;
9285
9286 /* We must force a BLOCK for this level so that, if it is not expanded
9287 later, there is a way to turn off the entire subtree of blocks that
9288 are contained in it. */
9289 keep_next_level ();
9290 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9291
9292 c_bindings_start_stmt_expr (c_switch_stack == NULL
9293 ? NULL
9294 : c_switch_stack->bindings);
325c3691
RH
9295
9296 /* Mark the current statement list as belonging to a statement list. */
9297 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9298
9299 return ret;
9300}
9301
c2255bc4
AH
9302/* LOC is the location of the compound statement to which this body
9303 belongs. */
9304
325c3691 9305tree
c2255bc4 9306c_finish_stmt_expr (location_t loc, tree body)
325c3691 9307{
3a5b9284 9308 tree last, type, tmp, val;
325c3691
RH
9309 tree *last_p;
9310
c2255bc4 9311 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9312
9313 c_bindings_end_stmt_expr (c_switch_stack == NULL
9314 ? NULL
9315 : c_switch_stack->bindings);
325c3691 9316
3a5b9284
RH
9317 /* Locate the last statement in BODY. See c_end_compound_stmt
9318 about always returning a BIND_EXPR. */
9319 last_p = &BIND_EXPR_BODY (body);
9320 last = BIND_EXPR_BODY (body);
9321
9322 continue_searching:
325c3691
RH
9323 if (TREE_CODE (last) == STATEMENT_LIST)
9324 {
3a5b9284
RH
9325 tree_stmt_iterator i;
9326
9327 /* This can happen with degenerate cases like ({ }). No value. */
9328 if (!TREE_SIDE_EFFECTS (last))
9329 return body;
9330
9331 /* If we're supposed to generate side effects warnings, process
9332 all of the statements except the last. */
27f33b15 9333 if (warn_unused_value)
325c3691 9334 {
3a5b9284 9335 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9336 {
9337 location_t tloc;
9338 tree t = tsi_stmt (i);
9339
9340 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9341 emit_side_effect_warnings (tloc, t);
9342 }
325c3691
RH
9343 }
9344 else
3a5b9284
RH
9345 i = tsi_last (last);
9346 last_p = tsi_stmt_ptr (i);
9347 last = *last_p;
325c3691
RH
9348 }
9349
3a5b9284
RH
9350 /* If the end of the list is exception related, then the list was split
9351 by a call to push_cleanup. Continue searching. */
9352 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9353 || TREE_CODE (last) == TRY_CATCH_EXPR)
9354 {
9355 last_p = &TREE_OPERAND (last, 0);
9356 last = *last_p;
9357 goto continue_searching;
9358 }
9359
26d8af35
JM
9360 if (last == error_mark_node)
9361 return last;
9362
3a5b9284
RH
9363 /* In the case that the BIND_EXPR is not necessary, return the
9364 expression out from inside it. */
26d8af35
JM
9365 if (last == BIND_EXPR_BODY (body)
9366 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9367 {
928c19bb
JM
9368 /* Even if this looks constant, do not allow it in a constant
9369 expression. */
e5a94231 9370 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9371 /* Do not warn if the return value of a statement expression is
9372 unused. */
928c19bb 9373 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9374 return last;
9375 }
325c3691
RH
9376
9377 /* Extract the type of said expression. */
9378 type = TREE_TYPE (last);
325c3691 9379
3a5b9284
RH
9380 /* If we're not returning a value at all, then the BIND_EXPR that
9381 we already have is a fine expression to return. */
9382 if (!type || VOID_TYPE_P (type))
9383 return body;
9384
9385 /* Now that we've located the expression containing the value, it seems
9386 silly to make voidify_wrapper_expr repeat the process. Create a
9387 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9388 tmp = create_tmp_var_raw (type, NULL);
9389
9390 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9391 tree_expr_nonnegative_p giving up immediately. */
9392 val = last;
9393 if (TREE_CODE (val) == NOP_EXPR
9394 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9395 val = TREE_OPERAND (val, 0);
9396
53fb4de3 9397 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9398 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9399
c2255bc4
AH
9400 {
9401 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9402 SET_EXPR_LOCATION (t, loc);
9403 return t;
9404 }
325c3691
RH
9405}
9406\f
9407/* Begin and end compound statements. This is as simple as pushing
9408 and popping new statement lists from the tree. */
9409
9410tree
9411c_begin_compound_stmt (bool do_scope)
9412{
9413 tree stmt = push_stmt_list ();
9414 if (do_scope)
4dfa0342 9415 push_scope ();
325c3691
RH
9416 return stmt;
9417}
9418
c2255bc4
AH
9419/* End a compound statement. STMT is the statement. LOC is the
9420 location of the compound statement-- this is usually the location
9421 of the opening brace. */
9422
325c3691 9423tree
c2255bc4 9424c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9425{
9426 tree block = NULL;
9427
9428 if (do_scope)
9429 {
9430 if (c_dialect_objc ())
9431 objc_clear_super_receiver ();
9432 block = pop_scope ();
9433 }
9434
9435 stmt = pop_stmt_list (stmt);
c2255bc4 9436 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9437
9438 /* If this compound statement is nested immediately inside a statement
9439 expression, then force a BIND_EXPR to be created. Otherwise we'll
9440 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9441 STATEMENT_LISTs merge, and thus we can lose track of what statement
9442 was really last. */
38e01f9e 9443 if (building_stmt_list_p ()
325c3691
RH
9444 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9445 && TREE_CODE (stmt) != BIND_EXPR)
9446 {
53fb4de3 9447 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9448 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9449 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9450 }
9451
9452 return stmt;
9453}
5a508662
RH
9454
9455/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9456 when the current scope is exited. EH_ONLY is true when this is not
9457 meant to apply to normal control flow transfer. */
9458
9459void
c2255bc4 9460push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 9461{
3a5b9284
RH
9462 enum tree_code code;
9463 tree stmt, list;
9464 bool stmt_expr;
9465
9466 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 9467 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 9468 add_stmt (stmt);
3a5b9284
RH
9469 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9470 list = push_stmt_list ();
9471 TREE_OPERAND (stmt, 0) = list;
9472 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 9473}
325c3691 9474\f
3e4093b6
RS
9475/* Build a binary-operation expression without default conversions.
9476 CODE is the kind of expression to build.
ba47d38d 9477 LOCATION is the operator's location.
3e4093b6
RS
9478 This function differs from `build' in several ways:
9479 the data type of the result is computed and recorded in it,
9480 warnings are generated if arg data types are invalid,
9481 special handling for addition and subtraction of pointers is known,
9482 and some optimization is done (operations on narrow ints
9483 are done in the narrower type when that gives the same result).
9484 Constant folding is also done before the result is returned.
de520661 9485
3e4093b6
RS
9486 Note that the operands will never have enumeral types, or function
9487 or array types, because either they will have the default conversions
9488 performed or they have both just been converted to some other type in which
9489 the arithmetic is to be done. */
9490
9491tree
ba47d38d
AH
9492build_binary_op (location_t location, enum tree_code code,
9493 tree orig_op0, tree orig_op1, int convert_p)
de520661 9494{
8ce94e44
JM
9495 tree type0, type1, orig_type0, orig_type1;
9496 tree eptype;
3e4093b6
RS
9497 enum tree_code code0, code1;
9498 tree op0, op1;
c9f9eb5d 9499 tree ret = error_mark_node;
4de67c26 9500 const char *invalid_op_diag;
4d84fe7c 9501 bool op0_int_operands, op1_int_operands;
928c19bb 9502 bool int_const, int_const_or_overflow, int_operands;
b62acd60 9503
3e4093b6
RS
9504 /* Expression code to give to the expression when it is built.
9505 Normally this is CODE, which is what the caller asked for,
9506 but in some special cases we change it. */
9507 enum tree_code resultcode = code;
8b6a5902 9508
3e4093b6
RS
9509 /* Data type in which the computation is to be performed.
9510 In the simplest cases this is the common type of the arguments. */
9511 tree result_type = NULL;
9512
8ce94e44
JM
9513 /* When the computation is in excess precision, the type of the
9514 final EXCESS_PRECISION_EXPR. */
2d2e923f 9515 tree semantic_result_type = NULL;
8ce94e44 9516
3e4093b6
RS
9517 /* Nonzero means operands have already been type-converted
9518 in whatever way is necessary.
9519 Zero means they need to be converted to RESULT_TYPE. */
9520 int converted = 0;
9521
9522 /* Nonzero means create the expression with this type, rather than
9523 RESULT_TYPE. */
9524 tree build_type = 0;
9525
9526 /* Nonzero means after finally constructing the expression
9527 convert it to this type. */
9528 tree final_type = 0;
9529
9530 /* Nonzero if this is an operation like MIN or MAX which can
9531 safely be computed in short if both args are promoted shorts.
9532 Also implies COMMON.
9533 -1 indicates a bitwise operation; this makes a difference
9534 in the exact conditions for when it is safe to do the operation
9535 in a narrower mode. */
9536 int shorten = 0;
9537
9538 /* Nonzero if this is a comparison operation;
9539 if both args are promoted shorts, compare the original shorts.
9540 Also implies COMMON. */
9541 int short_compare = 0;
9542
9543 /* Nonzero if this is a right-shift operation, which can be computed on the
9544 original short and then promoted if the operand is a promoted short. */
9545 int short_shift = 0;
9546
9547 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9548 int common = 0;
9549
58393038
ZL
9550 /* True means types are compatible as far as ObjC is concerned. */
9551 bool objc_ok;
9552
8ce94e44
JM
9553 /* True means this is an arithmetic operation that may need excess
9554 precision. */
9555 bool may_need_excess_precision;
9556
180f8dbb
JM
9557 /* True means this is a boolean operation that converts both its
9558 operands to truth-values. */
9559 bool boolean_op = false;
9560
de5a5fa1
MP
9561 /* Remember whether we're doing / or %. */
9562 bool doing_div_or_mod = false;
9563
9564 /* Remember whether we're doing << or >>. */
9565 bool doing_shift = false;
9566
9567 /* Tree holding instrumentation expression. */
9568 tree instrument_expr = NULL;
9569
ba47d38d
AH
9570 if (location == UNKNOWN_LOCATION)
9571 location = input_location;
9572
4d84fe7c
JM
9573 op0 = orig_op0;
9574 op1 = orig_op1;
9575
9576 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9577 if (op0_int_operands)
9578 op0 = remove_c_maybe_const_expr (op0);
9579 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9580 if (op1_int_operands)
9581 op1 = remove_c_maybe_const_expr (op1);
9582 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
9583 if (int_operands)
9584 {
9585 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9586 && TREE_CODE (orig_op1) == INTEGER_CST);
9587 int_const = (int_const_or_overflow
9588 && !TREE_OVERFLOW (orig_op0)
9589 && !TREE_OVERFLOW (orig_op1));
9590 }
9591 else
9592 int_const = int_const_or_overflow = false;
9593
0e3a99ae 9594 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
9595 if (convert_p
9596 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 9597 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 9598 {
4d84fe7c
JM
9599 op0 = default_conversion (op0);
9600 op1 = default_conversion (op1);
790e9490
RS
9601 }
9602
36536d79
BI
9603 /* When Cilk Plus is enabled and there are array notations inside op0, then
9604 we check to see if there are builtin array notation functions. If
9605 so, then we take on the type of the array notation inside it. */
9606 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
9607 orig_type0 = type0 = find_correct_array_notation_type (op0);
9608 else
9609 orig_type0 = type0 = TREE_TYPE (op0);
9610
9611 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
9612 orig_type1 = type1 = find_correct_array_notation_type (op1);
9613 else
9614 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 9615
3e4093b6
RS
9616 /* The expression codes of the data types of the arguments tell us
9617 whether the arguments are integers, floating, pointers, etc. */
9618 code0 = TREE_CODE (type0);
9619 code1 = TREE_CODE (type1);
9620
9621 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9622 STRIP_TYPE_NOPS (op0);
9623 STRIP_TYPE_NOPS (op1);
9624
9625 /* If an error was already reported for one of the arguments,
9626 avoid reporting another error. */
9627
9628 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9629 return error_mark_node;
9630
4de67c26
JM
9631 if ((invalid_op_diag
9632 = targetm.invalid_binary_op (code, type0, type1)))
9633 {
ba47d38d 9634 error_at (location, invalid_op_diag);
4de67c26
JM
9635 return error_mark_node;
9636 }
9637
8ce94e44
JM
9638 switch (code)
9639 {
9640 case PLUS_EXPR:
9641 case MINUS_EXPR:
9642 case MULT_EXPR:
9643 case TRUNC_DIV_EXPR:
9644 case CEIL_DIV_EXPR:
9645 case FLOOR_DIV_EXPR:
9646 case ROUND_DIV_EXPR:
9647 case EXACT_DIV_EXPR:
9648 may_need_excess_precision = true;
9649 break;
9650 default:
9651 may_need_excess_precision = false;
9652 break;
9653 }
9654 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9655 {
9656 op0 = TREE_OPERAND (op0, 0);
9657 type0 = TREE_TYPE (op0);
9658 }
9659 else if (may_need_excess_precision
9660 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9661 {
9662 type0 = eptype;
9663 op0 = convert (eptype, op0);
9664 }
9665 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9666 {
9667 op1 = TREE_OPERAND (op1, 0);
9668 type1 = TREE_TYPE (op1);
9669 }
9670 else if (may_need_excess_precision
9671 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9672 {
9673 type1 = eptype;
9674 op1 = convert (eptype, op1);
9675 }
9676
58393038
ZL
9677 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9678
0e3a99ae
AS
9679 /* In case when one of the operands of the binary operation is
9680 a vector and another is a scalar -- convert scalar to vector. */
9681 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9682 {
a212e43f
MG
9683 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
9684 true);
f90e8e2e 9685
0e3a99ae
AS
9686 switch (convert_flag)
9687 {
9688 case stv_error:
9689 return error_mark_node;
9690 case stv_firstarg:
9691 {
9692 bool maybe_const = true;
9693 tree sc;
9694 sc = c_fully_fold (op0, false, &maybe_const);
9695 sc = save_expr (sc);
9696 sc = convert (TREE_TYPE (type1), sc);
9697 op0 = build_vector_from_val (type1, sc);
9698 if (!maybe_const)
9699 op0 = c_wrap_maybe_const (op0, true);
9700 orig_type0 = type0 = TREE_TYPE (op0);
9701 code0 = TREE_CODE (type0);
9702 converted = 1;
9703 break;
9704 }
9705 case stv_secondarg:
9706 {
9707 bool maybe_const = true;
9708 tree sc;
9709 sc = c_fully_fold (op1, false, &maybe_const);
9710 sc = save_expr (sc);
9711 sc = convert (TREE_TYPE (type0), sc);
9712 op1 = build_vector_from_val (type0, sc);
9713 if (!maybe_const)
54b9f838 9714 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
9715 orig_type1 = type1 = TREE_TYPE (op1);
9716 code1 = TREE_CODE (type1);
9717 converted = 1;
9718 break;
9719 }
9720 default:
9721 break;
9722 }
9723 }
9724
3e4093b6 9725 switch (code)
de520661 9726 {
3e4093b6
RS
9727 case PLUS_EXPR:
9728 /* Handle the pointer + int case. */
9729 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 9730 {
db3927fb 9731 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
9732 goto return_build_binary_op;
9733 }
3e4093b6 9734 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 9735 {
db3927fb 9736 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
9737 goto return_build_binary_op;
9738 }
fe67cf58 9739 else
3e4093b6
RS
9740 common = 1;
9741 break;
400fbf9f 9742
3e4093b6
RS
9743 case MINUS_EXPR:
9744 /* Subtraction of two similar pointers.
9745 We must subtract them as integers, then divide by object size. */
9746 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 9747 && comp_target_types (location, type0, type1))
c9f9eb5d 9748 {
db3927fb 9749 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
9750 goto return_build_binary_op;
9751 }
3e4093b6
RS
9752 /* Handle pointer minus int. Just like pointer plus int. */
9753 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 9754 {
db3927fb 9755 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
9756 goto return_build_binary_op;
9757 }
3e4093b6
RS
9758 else
9759 common = 1;
9760 break;
8b6a5902 9761
3e4093b6
RS
9762 case MULT_EXPR:
9763 common = 1;
9764 break;
9765
9766 case TRUNC_DIV_EXPR:
9767 case CEIL_DIV_EXPR:
9768 case FLOOR_DIV_EXPR:
9769 case ROUND_DIV_EXPR:
9770 case EXACT_DIV_EXPR:
de5a5fa1 9771 doing_div_or_mod = true;
c9f9eb5d 9772 warn_for_div_by_zero (location, op1);
3e4093b6
RS
9773
9774 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 9775 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
9776 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9777 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 9778 || code1 == FIXED_POINT_TYPE
3e4093b6 9779 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 9780 {
5bed876a
AH
9781 enum tree_code tcode0 = code0, tcode1 = code1;
9782
3a021db2 9783 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 9784 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 9785 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 9786 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 9787
ab22c1fa
CF
9788 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9789 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
9790 resultcode = RDIV_EXPR;
9791 else
9792 /* Although it would be tempting to shorten always here, that
9793 loses on some targets, since the modulo instruction is
9794 undefined if the quotient can't be represented in the
9795 computation mode. We shorten only if unsigned or if
9796 dividing by something we know != -1. */
8df83eae 9797 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 9798 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 9799 && !integer_all_onesp (op1)));
3e4093b6
RS
9800 common = 1;
9801 }
9802 break;
de520661 9803
3e4093b6 9804 case BIT_AND_EXPR:
3e4093b6
RS
9805 case BIT_IOR_EXPR:
9806 case BIT_XOR_EXPR:
9807 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9808 shorten = -1;
9ef0c8d9
AP
9809 /* Allow vector types which are not floating point types. */
9810 else if (code0 == VECTOR_TYPE
9811 && code1 == VECTOR_TYPE
9812 && !VECTOR_FLOAT_TYPE_P (type0)
9813 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
9814 common = 1;
9815 break;
9816
9817 case TRUNC_MOD_EXPR:
9818 case FLOOR_MOD_EXPR:
de5a5fa1 9819 doing_div_or_mod = true;
c9f9eb5d 9820 warn_for_div_by_zero (location, op1);
de520661 9821
5cfd5d9b
AP
9822 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9823 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9824 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9825 common = 1;
9826 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
9827 {
9828 /* Although it would be tempting to shorten always here, that loses
9829 on some targets, since the modulo instruction is undefined if the
9830 quotient can't be represented in the computation mode. We shorten
9831 only if unsigned or if dividing by something we know != -1. */
8df83eae 9832 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 9833 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 9834 && !integer_all_onesp (op1)));
3e4093b6
RS
9835 common = 1;
9836 }
9837 break;
de520661 9838
3e4093b6
RS
9839 case TRUTH_ANDIF_EXPR:
9840 case TRUTH_ORIF_EXPR:
9841 case TRUTH_AND_EXPR:
9842 case TRUTH_OR_EXPR:
9843 case TRUTH_XOR_EXPR:
9844 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
9845 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9846 || code0 == FIXED_POINT_TYPE)
3e4093b6 9847 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
9848 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9849 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
9850 {
9851 /* Result of these operations is always an int,
9852 but that does not mean the operands should be
9853 converted to ints! */
9854 result_type = integer_type_node;
a27d595d
JM
9855 if (op0_int_operands)
9856 {
9857 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
9858 op0 = remove_c_maybe_const_expr (op0);
9859 }
9860 else
9861 op0 = c_objc_common_truthvalue_conversion (location, op0);
9862 if (op1_int_operands)
9863 {
9864 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
9865 op1 = remove_c_maybe_const_expr (op1);
9866 }
9867 else
9868 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 9869 converted = 1;
180f8dbb 9870 boolean_op = true;
3e4093b6 9871 }
928c19bb
JM
9872 if (code == TRUTH_ANDIF_EXPR)
9873 {
9874 int_const_or_overflow = (int_operands
9875 && TREE_CODE (orig_op0) == INTEGER_CST
9876 && (op0 == truthvalue_false_node
9877 || TREE_CODE (orig_op1) == INTEGER_CST));
9878 int_const = (int_const_or_overflow
9879 && !TREE_OVERFLOW (orig_op0)
9880 && (op0 == truthvalue_false_node
9881 || !TREE_OVERFLOW (orig_op1)));
9882 }
9883 else if (code == TRUTH_ORIF_EXPR)
9884 {
9885 int_const_or_overflow = (int_operands
9886 && TREE_CODE (orig_op0) == INTEGER_CST
9887 && (op0 == truthvalue_true_node
9888 || TREE_CODE (orig_op1) == INTEGER_CST));
9889 int_const = (int_const_or_overflow
9890 && !TREE_OVERFLOW (orig_op0)
9891 && (op0 == truthvalue_true_node
9892 || !TREE_OVERFLOW (orig_op1)));
9893 }
3e4093b6 9894 break;
eba80994 9895
3e4093b6
RS
9896 /* Shift operations: result has same type as first operand;
9897 always convert second operand to int.
9898 Also set SHORT_SHIFT if shifting rightward. */
de520661 9899
3e4093b6 9900 case RSHIFT_EXPR:
f87bd04b
AS
9901 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9902 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9903 {
9904 result_type = type0;
9905 converted = 1;
9906 }
9907 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9908 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9909 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9910 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9911 {
9912 result_type = type0;
9913 converted = 1;
9914 }
9915 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 9916 && code1 == INTEGER_TYPE)
3e4093b6 9917 {
de5a5fa1 9918 doing_shift = true;
928c19bb 9919 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 9920 {
3e4093b6 9921 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
9922 {
9923 int_const = false;
7d882b83 9924 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
9925 warning (0, "right shift count is negative");
9926 }
3e4093b6 9927 else
bbb818c6 9928 {
3f75a254 9929 if (!integer_zerop (op1))
3e4093b6
RS
9930 short_shift = 1;
9931
9932 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
9933 {
9934 int_const = false;
7d882b83 9935 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
9936 warning (0, "right shift count >= width of type");
9937 }
bbb818c6 9938 }
b62acd60 9939 }
de520661 9940
3e4093b6
RS
9941 /* Use the type of the value to be shifted. */
9942 result_type = type0;
f87bd04b
AS
9943 /* Convert the non vector shift-count to an integer, regardless
9944 of size of value being shifted. */
9945 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9946 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
9947 op1 = convert (integer_type_node, op1);
9948 /* Avoid converting op1 to result_type later. */
9949 converted = 1;
400fbf9f 9950 }
3e4093b6 9951 break;
253b6b82 9952
3e4093b6 9953 case LSHIFT_EXPR:
f87bd04b
AS
9954 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9955 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9956 {
9957 result_type = type0;
9958 converted = 1;
9959 }
9960 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9961 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9962 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9963 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9964 {
9965 result_type = type0;
9966 converted = 1;
9967 }
9968 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 9969 && code1 == INTEGER_TYPE)
3e4093b6 9970 {
de5a5fa1 9971 doing_shift = true;
928c19bb 9972 if (TREE_CODE (op1) == INTEGER_CST)
de520661 9973 {
3e4093b6 9974 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
9975 {
9976 int_const = false;
7d882b83 9977 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
9978 warning (0, "left shift count is negative");
9979 }
de520661 9980
3e4093b6 9981 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
9982 {
9983 int_const = false;
7d882b83 9984 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
9985 warning (0, "left shift count >= width of type");
9986 }
94ba5069 9987 }
de520661 9988
3e4093b6
RS
9989 /* Use the type of the value to be shifted. */
9990 result_type = type0;
f87bd04b
AS
9991 /* Convert the non vector shift-count to an integer, regardless
9992 of size of value being shifted. */
9993 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9994 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
9995 op1 = convert (integer_type_node, op1);
9996 /* Avoid converting op1 to result_type later. */
9997 converted = 1;
400fbf9f 9998 }
3e4093b6 9999 break;
de520661 10000
3e4093b6
RS
10001 case EQ_EXPR:
10002 case NE_EXPR:
d246ab4f
AS
10003 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10004 {
10005 tree intt;
0af94e6f 10006 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10007 {
10008 error_at (location, "comparing vectors with different "
10009 "element types");
10010 return error_mark_node;
10011 }
10012
10013 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10014 {
10015 error_at (location, "comparing vectors with different "
10016 "number of elements");
10017 return error_mark_node;
10018 }
10019
10020 /* Always construct signed integer vector type. */
10021 intt = c_common_type_for_size (GET_MODE_BITSIZE
10022 (TYPE_MODE (TREE_TYPE (type0))), 0);
10023 result_type = build_opaque_vector_type (intt,
10024 TYPE_VECTOR_SUBPARTS (type0));
10025 converted = 1;
10026 break;
10027 }
ae311566 10028 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10029 warning_at (location,
10030 OPT_Wfloat_equal,
10031 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10032 /* Result of comparison is always int,
10033 but don't convert the args to int! */
10034 build_type = integer_type_node;
10035 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10036 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10037 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10038 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10039 short_compare = 1;
637f1455
SZ
10040 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10041 {
10042 if (TREE_CODE (op0) == ADDR_EXPR
10043 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10044 {
10045 if (code == EQ_EXPR)
10046 warning_at (location,
10047 OPT_Waddress,
10048 "the comparison will always evaluate as %<false%> "
10049 "for the address of %qD will never be NULL",
10050 TREE_OPERAND (op0, 0));
10051 else
10052 warning_at (location,
10053 OPT_Waddress,
10054 "the comparison will always evaluate as %<true%> "
10055 "for the address of %qD will never be NULL",
10056 TREE_OPERAND (op0, 0));
10057 }
10058 result_type = type0;
10059 }
10060 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10061 {
10062 if (TREE_CODE (op1) == ADDR_EXPR
10063 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10064 {
10065 if (code == EQ_EXPR)
10066 warning_at (location,
f90e8e2e 10067 OPT_Waddress,
637f1455
SZ
10068 "the comparison will always evaluate as %<false%> "
10069 "for the address of %qD will never be NULL",
10070 TREE_OPERAND (op1, 0));
10071 else
10072 warning_at (location,
10073 OPT_Waddress,
10074 "the comparison will always evaluate as %<true%> "
10075 "for the address of %qD will never be NULL",
10076 TREE_OPERAND (op1, 0));
10077 }
10078 result_type = type1;
10079 }
3e4093b6
RS
10080 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10081 {
10082 tree tt0 = TREE_TYPE (type0);
10083 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10084 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10085 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10086 addr_space_t as_common = ADDR_SPACE_GENERIC;
10087
3e4093b6
RS
10088 /* Anything compares with void *. void * compares with anything.
10089 Otherwise, the targets must be compatible
10090 and both must be object or both incomplete. */
744aa42f 10091 if (comp_target_types (location, type0, type1))
10bc1b1b 10092 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10093 else if (!addr_space_superset (as0, as1, &as_common))
10094 {
10095 error_at (location, "comparison of pointers to "
10096 "disjoint address spaces");
10097 return error_mark_node;
10098 }
3e4093b6 10099 else if (VOID_TYPE_P (tt0))
ee2990e7 10100 {
36c5e70a 10101 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10102 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10103 "comparison of %<void *%> with function pointer");
ee2990e7 10104 }
3e4093b6 10105 else if (VOID_TYPE_P (tt1))
e6834654 10106 {
36c5e70a 10107 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10108 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10109 "comparison of %<void *%> with function pointer");
e6834654 10110 }
3e4093b6 10111 else
58393038
ZL
10112 /* Avoid warning about the volatile ObjC EH puts on decls. */
10113 if (!objc_ok)
ba47d38d 10114 pedwarn (location, 0,
509c9d60 10115 "comparison of distinct pointer types lacks a cast");
e6834654 10116
3e4093b6 10117 if (result_type == NULL_TREE)
36c5e70a
BE
10118 {
10119 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10120 result_type = build_pointer_type
10121 (build_qualified_type (void_type_node, qual));
10122 }
e6834654 10123 }
3e4093b6 10124 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10125 {
3e4093b6 10126 result_type = type0;
ba47d38d 10127 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10128 }
3e4093b6 10129 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10130 {
3e4093b6 10131 result_type = type1;
ba47d38d 10132 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10133 }
3e4093b6 10134 break;
8b6a5902 10135
3e4093b6
RS
10136 case LE_EXPR:
10137 case GE_EXPR:
10138 case LT_EXPR:
10139 case GT_EXPR:
d246ab4f
AS
10140 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10141 {
10142 tree intt;
0af94e6f 10143 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10144 {
10145 error_at (location, "comparing vectors with different "
10146 "element types");
10147 return error_mark_node;
10148 }
10149
10150 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10151 {
10152 error_at (location, "comparing vectors with different "
10153 "number of elements");
10154 return error_mark_node;
10155 }
10156
10157 /* Always construct signed integer vector type. */
10158 intt = c_common_type_for_size (GET_MODE_BITSIZE
10159 (TYPE_MODE (TREE_TYPE (type0))), 0);
10160 result_type = build_opaque_vector_type (intt,
10161 TYPE_VECTOR_SUBPARTS (type0));
10162 converted = 1;
10163 break;
10164 }
3e4093b6 10165 build_type = integer_type_node;
ab22c1fa
CF
10166 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10167 || code0 == FIXED_POINT_TYPE)
10168 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10169 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10170 short_compare = 1;
10171 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10172 {
36c5e70a
BE
10173 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10174 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10175 addr_space_t as_common;
10176
744aa42f 10177 if (comp_target_types (location, type0, type1))
3e4093b6 10178 {
10bc1b1b 10179 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10180 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10181 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10182 pedwarn (location, 0,
509c9d60 10183 "comparison of complete and incomplete pointers");
fcf73884 10184 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10185 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10186 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10187 else if (null_pointer_constant_p (orig_op0)
10188 || null_pointer_constant_p (orig_op1))
10189 warning_at (location, OPT_Wextra,
10190 "ordered comparison of pointer with null pointer");
10191
3e4093b6 10192 }
36c5e70a
BE
10193 else if (!addr_space_superset (as0, as1, &as_common))
10194 {
10195 error_at (location, "comparison of pointers to "
10196 "disjoint address spaces");
10197 return error_mark_node;
10198 }
3e4093b6
RS
10199 else
10200 {
36c5e70a
BE
10201 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10202 result_type = build_pointer_type
10203 (build_qualified_type (void_type_node, qual));
ba47d38d 10204 pedwarn (location, 0,
509c9d60 10205 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10206 }
10207 }
6aa3c60d 10208 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10209 {
10210 result_type = type0;
fcf73884 10211 if (pedantic)
c1771a20 10212 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10213 "ordered comparison of pointer with integer zero");
10214 else if (extra_warnings)
ba47d38d 10215 warning_at (location, OPT_Wextra,
d42ba3b8 10216 "ordered comparison of pointer with integer zero");
3e4093b6 10217 }
6aa3c60d 10218 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10219 {
10220 result_type = type1;
d42ba3b8 10221 if (pedantic)
c1771a20 10222 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10223 "ordered comparison of pointer with integer zero");
10224 else if (extra_warnings)
10225 warning_at (location, OPT_Wextra,
10226 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10227 }
10228 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10229 {
10230 result_type = type0;
ba47d38d 10231 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10232 }
10233 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10234 {
10235 result_type = type1;
ba47d38d 10236 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10237 }
10238 break;
64094f6a 10239
3e4093b6 10240 default:
37b2f290 10241 gcc_unreachable ();
c9fe6f9f 10242 }
8f17b5c5 10243
e57e265b
PB
10244 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10245 return error_mark_node;
10246
5bed876a
AH
10247 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10248 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10249 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10250 {
ba47d38d 10251 binary_op_error (location, code, type0, type1);
5bed876a
AH
10252 return error_mark_node;
10253 }
10254
3e4093b6 10255 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10256 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10257 &&
10258 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10259 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10260 {
2ca862e9
JM
10261 bool first_complex = (code0 == COMPLEX_TYPE);
10262 bool second_complex = (code1 == COMPLEX_TYPE);
10263 int none_complex = (!first_complex && !second_complex);
39b726dd 10264
3e4093b6 10265 if (shorten || common || short_compare)
3bf6bfcc
JJ
10266 {
10267 result_type = c_common_type (type0, type1);
0a0b3574
MM
10268 do_warn_double_promotion (result_type, type0, type1,
10269 "implicit conversion from %qT to %qT "
10270 "to match other operand of binary "
10271 "expression",
10272 location);
3bf6bfcc
JJ
10273 if (result_type == error_mark_node)
10274 return error_mark_node;
10275 }
400fbf9f 10276
2ca862e9
JM
10277 if (first_complex != second_complex
10278 && (code == PLUS_EXPR
10279 || code == MINUS_EXPR
10280 || code == MULT_EXPR
10281 || (code == TRUNC_DIV_EXPR && first_complex))
10282 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10283 && flag_signed_zeros)
10284 {
10285 /* An operation on mixed real/complex operands must be
10286 handled specially, but the language-independent code can
10287 more easily optimize the plain complex arithmetic if
10288 -fno-signed-zeros. */
10289 tree real_type = TREE_TYPE (result_type);
10290 tree real, imag;
10291 if (type0 != orig_type0 || type1 != orig_type1)
10292 {
10293 gcc_assert (may_need_excess_precision && common);
2d2e923f 10294 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10295 }
10296 if (first_complex)
10297 {
10298 if (TREE_TYPE (op0) != result_type)
10299 op0 = convert_and_check (result_type, op0);
10300 if (TREE_TYPE (op1) != real_type)
10301 op1 = convert_and_check (real_type, op1);
10302 }
10303 else
10304 {
10305 if (TREE_TYPE (op0) != real_type)
10306 op0 = convert_and_check (real_type, op0);
10307 if (TREE_TYPE (op1) != result_type)
10308 op1 = convert_and_check (result_type, op1);
10309 }
10310 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10311 return error_mark_node;
10312 if (first_complex)
10313 {
10314 op0 = c_save_expr (op0);
10315 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10316 op0, 1);
10317 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10318 op0, 1);
10319 switch (code)
10320 {
10321 case MULT_EXPR:
10322 case TRUNC_DIV_EXPR:
c4603e7c 10323 op1 = c_save_expr (op1);
2ca862e9
JM
10324 imag = build2 (resultcode, real_type, imag, op1);
10325 /* Fall through. */
10326 case PLUS_EXPR:
10327 case MINUS_EXPR:
10328 real = build2 (resultcode, real_type, real, op1);
10329 break;
10330 default:
10331 gcc_unreachable();
10332 }
10333 }
10334 else
10335 {
10336 op1 = c_save_expr (op1);
10337 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10338 op1, 1);
10339 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10340 op1, 1);
10341 switch (code)
10342 {
10343 case MULT_EXPR:
c4603e7c 10344 op0 = c_save_expr (op0);
2ca862e9
JM
10345 imag = build2 (resultcode, real_type, op0, imag);
10346 /* Fall through. */
10347 case PLUS_EXPR:
10348 real = build2 (resultcode, real_type, op0, real);
10349 break;
10350 case MINUS_EXPR:
10351 real = build2 (resultcode, real_type, op0, real);
10352 imag = build1 (NEGATE_EXPR, real_type, imag);
10353 break;
10354 default:
10355 gcc_unreachable();
10356 }
10357 }
10358 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10359 goto return_build_binary_op;
10360 }
10361
3e4093b6
RS
10362 /* For certain operations (which identify themselves by shorten != 0)
10363 if both args were extended from the same smaller type,
10364 do the arithmetic in that type and then extend.
400fbf9f 10365
3e4093b6
RS
10366 shorten !=0 and !=1 indicates a bitwise operation.
10367 For them, this optimization is safe only if
10368 both args are zero-extended or both are sign-extended.
10369 Otherwise, we might change the result.
10370 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10371 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10372
3e4093b6
RS
10373 if (shorten && none_complex)
10374 {
3e4093b6 10375 final_type = result_type;
b8698a0f 10376 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10377 shorten == -1);
3e4093b6 10378 }
88a3dbc1 10379
3e4093b6 10380 /* Shifts can be shortened if shifting right. */
2f6e4e97 10381
3e4093b6
RS
10382 if (short_shift)
10383 {
10384 int unsigned_arg;
10385 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10386
3e4093b6 10387 final_type = result_type;
abe80e6d 10388
3e4093b6 10389 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10390 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10391
3e4093b6 10392 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10393 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10394 /* We can shorten only if the shift count is less than the
10395 number of bits in the smaller type size. */
10396 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10397 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10398 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10399 {
10400 /* Do an unsigned shift if the operand was zero-extended. */
10401 result_type
10402 = c_common_signed_or_unsigned_type (unsigned_arg,
10403 TREE_TYPE (arg0));
10404 /* Convert value-to-be-shifted to that type. */
10405 if (TREE_TYPE (op0) != result_type)
10406 op0 = convert (result_type, op0);
10407 converted = 1;
abe80e6d 10408 }
88a3dbc1
RK
10409 }
10410
3e4093b6
RS
10411 /* Comparison operations are shortened too but differently.
10412 They identify themselves by setting short_compare = 1. */
56cb9733 10413
3e4093b6
RS
10414 if (short_compare)
10415 {
10416 /* Don't write &op0, etc., because that would prevent op0
10417 from being kept in a register.
10418 Instead, make copies of the our local variables and
10419 pass the copies by reference, then copy them back afterward. */
10420 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10421 enum tree_code xresultcode = resultcode;
10422 tree val
10423 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 10424
3e4093b6 10425 if (val != 0)
c9f9eb5d
AH
10426 {
10427 ret = val;
10428 goto return_build_binary_op;
10429 }
8f17b5c5 10430
3e4093b6
RS
10431 op0 = xop0, op1 = xop1;
10432 converted = 1;
10433 resultcode = xresultcode;
8f17b5c5 10434
7d882b83 10435 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10436 {
10437 bool op0_maybe_const = true;
10438 bool op1_maybe_const = true;
10439 tree orig_op0_folded, orig_op1_folded;
10440
10441 if (in_late_binary_op)
10442 {
10443 orig_op0_folded = orig_op0;
10444 orig_op1_folded = orig_op1;
10445 }
10446 else
10447 {
10448 /* Fold for the sake of possible warnings, as in
10449 build_conditional_expr. This requires the
10450 "original" values to be folded, not just op0 and
10451 op1. */
f5178456 10452 c_inhibit_evaluation_warnings++;
928c19bb
JM
10453 op0 = c_fully_fold (op0, require_constant_value,
10454 &op0_maybe_const);
10455 op1 = c_fully_fold (op1, require_constant_value,
10456 &op1_maybe_const);
f5178456 10457 c_inhibit_evaluation_warnings--;
928c19bb
JM
10458 orig_op0_folded = c_fully_fold (orig_op0,
10459 require_constant_value,
10460 NULL);
10461 orig_op1_folded = c_fully_fold (orig_op1,
10462 require_constant_value,
10463 NULL);
10464 }
10465
10466 if (warn_sign_compare)
10467 warn_for_sign_compare (location, orig_op0_folded,
10468 orig_op1_folded, op0, op1,
10469 result_type, resultcode);
5c2f94b4 10470 if (!in_late_binary_op && !int_operands)
928c19bb
JM
10471 {
10472 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 10473 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 10474 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 10475 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 10476 }
3e4093b6 10477 }
2ad1815d 10478 }
64094f6a 10479 }
64094f6a 10480
3e4093b6
RS
10481 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10482 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10483 Then the expression will be built.
10484 It will be given type FINAL_TYPE if that is nonzero;
10485 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 10486
3e4093b6
RS
10487 if (!result_type)
10488 {
ba47d38d 10489 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
10490 return error_mark_node;
10491 }
400fbf9f 10492
3e4093b6 10493 if (build_type == NULL_TREE)
8ce94e44
JM
10494 {
10495 build_type = result_type;
180f8dbb
JM
10496 if ((type0 != orig_type0 || type1 != orig_type1)
10497 && !boolean_op)
8ce94e44
JM
10498 {
10499 gcc_assert (may_need_excess_precision && common);
2d2e923f 10500 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
10501 }
10502 }
400fbf9f 10503
2d2e923f
MLI
10504 if (!converted)
10505 {
10506 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10507 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10508
10509 /* This can happen if one operand has a vector type, and the other
10510 has a different type. */
10511 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10512 return error_mark_node;
10513 }
10514
a24d975c 10515 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
de5a5fa1 10516 && current_function_decl != 0
ce6923c5
MP
10517 && !lookup_attribute ("no_sanitize_undefined",
10518 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
10519 && (doing_div_or_mod || doing_shift))
10520 {
10521 /* OP0 and/or OP1 might have side-effects. */
10522 op0 = c_save_expr (op0);
10523 op1 = c_save_expr (op1);
10524 op0 = c_fully_fold (op0, false, NULL);
10525 op1 = c_fully_fold (op1, false, NULL);
a24d975c 10526 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
de5a5fa1 10527 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 10528 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
10529 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10530 }
10531
c9f9eb5d 10532 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
10533 if (int_const_or_overflow)
10534 ret = (require_constant_value
db3927fb
AH
10535 ? fold_build2_initializer_loc (location, resultcode, build_type,
10536 op0, op1)
10537 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
10538 else
10539 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
10540 if (final_type != 0)
10541 ret = convert (final_type, ret);
10542
10543 return_build_binary_op:
10544 gcc_assert (ret != error_mark_node);
928c19bb
JM
10545 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10546 ret = (int_operands
10547 ? note_integer_operands (ret)
10548 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10549 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10550 && !in_late_binary_op)
10551 ret = note_integer_operands (ret);
2d2e923f
MLI
10552 if (semantic_result_type)
10553 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 10554 protected_set_expr_location (ret, location);
de5a5fa1 10555
a24d975c 10556 if (instrument_expr != NULL)
de5a5fa1
MP
10557 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
10558 instrument_expr, ret);
10559
c9f9eb5d 10560 return ret;
400fbf9f 10561}
85498824
JM
10562
10563
10564/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 10565 purpose. LOCATION is the source location for the expression. */
85498824
JM
10566
10567tree
ba47d38d 10568c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 10569{
928c19bb
JM
10570 bool int_const, int_operands;
10571
85498824
JM
10572 switch (TREE_CODE (TREE_TYPE (expr)))
10573 {
10574 case ARRAY_TYPE:
ba47d38d 10575 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
10576 return error_mark_node;
10577
10578 case RECORD_TYPE:
ba47d38d 10579 error_at (location, "used struct type value where scalar is required");
85498824
JM
10580 return error_mark_node;
10581
10582 case UNION_TYPE:
ba47d38d 10583 error_at (location, "used union type value where scalar is required");
85498824
JM
10584 return error_mark_node;
10585
04af8788
NP
10586 case VOID_TYPE:
10587 error_at (location, "void value not ignored as it ought to be");
10588 return error_mark_node;
10589
46bdb9cf
JM
10590 case FUNCTION_TYPE:
10591 gcc_unreachable ();
10592
d246ab4f
AS
10593 case VECTOR_TYPE:
10594 error_at (location, "used vector type where scalar is required");
10595 return error_mark_node;
10596
85498824
JM
10597 default:
10598 break;
10599 }
10600
928c19bb
JM
10601 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10602 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
10603 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10604 {
10605 expr = remove_c_maybe_const_expr (expr);
10606 expr = build2 (NE_EXPR, integer_type_node, expr,
10607 convert (TREE_TYPE (expr), integer_zero_node));
10608 expr = note_integer_operands (expr);
10609 }
10610 else
10611 /* ??? Should we also give an error for vectors rather than leaving
10612 those to give errors later? */
10613 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
10614
10615 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10616 {
10617 if (TREE_OVERFLOW (expr))
10618 return expr;
10619 else
10620 return note_integer_operands (expr);
10621 }
10622 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10623 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10624 return expr;
85498824 10625}
73f397d4
JM
10626\f
10627
10628/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10629 required. */
10630
10631tree
51eed280 10632c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
10633{
10634 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10635 {
10636 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10637 /* Executing a compound literal inside a function reinitializes
10638 it. */
10639 if (!TREE_STATIC (decl))
10640 *se = true;
10641 return decl;
10642 }
10643 else
10644 return expr;
10645}
953ff289 10646\f
c0220ea4 10647/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
10648
10649tree
10650c_begin_omp_parallel (void)
10651{
10652 tree block;
10653
10654 keep_next_level ();
10655 block = c_begin_compound_stmt (true);
10656
10657 return block;
10658}
10659
c2255bc4
AH
10660/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10661 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 10662
953ff289 10663tree
c2255bc4 10664c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
10665{
10666 tree stmt;
10667
c2255bc4 10668 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
10669
10670 stmt = make_node (OMP_PARALLEL);
10671 TREE_TYPE (stmt) = void_type_node;
10672 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10673 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 10674 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
10675
10676 return add_stmt (stmt);
10677}
10678
a68ab351
JJ
10679/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10680
10681tree
10682c_begin_omp_task (void)
10683{
10684 tree block;
10685
10686 keep_next_level ();
10687 block = c_begin_compound_stmt (true);
10688
10689 return block;
10690}
10691
c2255bc4
AH
10692/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10693 statement. LOC is the location of the #pragma. */
a68ab351
JJ
10694
10695tree
c2255bc4 10696c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
10697{
10698 tree stmt;
10699
c2255bc4 10700 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
10701
10702 stmt = make_node (OMP_TASK);
10703 TREE_TYPE (stmt) = void_type_node;
10704 OMP_TASK_CLAUSES (stmt) = clauses;
10705 OMP_TASK_BODY (stmt) = block;
c2255bc4 10706 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
10707
10708 return add_stmt (stmt);
10709}
10710
acf0174b
JJ
10711/* Generate GOMP_cancel call for #pragma omp cancel. */
10712
10713void
10714c_finish_omp_cancel (location_t loc, tree clauses)
10715{
10716 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
10717 int mask = 0;
10718 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
10719 mask = 1;
10720 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
10721 mask = 2;
10722 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
10723 mask = 4;
10724 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
10725 mask = 8;
10726 else
10727 {
10728 error_at (loc, "%<#pragma omp cancel must specify one of "
10729 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
10730 "clauses");
10731 return;
10732 }
10733 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
10734 if (ifc != NULL_TREE)
10735 {
10736 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
10737 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
10738 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
10739 build_zero_cst (type));
10740 }
10741 else
10742 ifc = boolean_true_node;
10743 tree stmt = build_call_expr_loc (loc, fn, 2,
10744 build_int_cst (integer_type_node, mask),
10745 ifc);
10746 add_stmt (stmt);
10747}
10748
10749/* Generate GOMP_cancellation_point call for
10750 #pragma omp cancellation point. */
10751
10752void
10753c_finish_omp_cancellation_point (location_t loc, tree clauses)
10754{
10755 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
10756 int mask = 0;
10757 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
10758 mask = 1;
10759 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
10760 mask = 2;
10761 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
10762 mask = 4;
10763 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
10764 mask = 8;
10765 else
10766 {
10767 error_at (loc, "%<#pragma omp cancellation point must specify one of "
10768 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
10769 "clauses");
10770 return;
10771 }
10772 tree stmt = build_call_expr_loc (loc, fn, 1,
10773 build_int_cst (integer_type_node, mask));
10774 add_stmt (stmt);
10775}
10776
10777/* Helper function for handle_omp_array_sections. Called recursively
10778 to handle multiple array-section-subscripts. C is the clause,
10779 T current expression (initially OMP_CLAUSE_DECL), which is either
10780 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
10781 expression if specified, TREE_VALUE length expression if specified,
10782 TREE_CHAIN is what it has been specified after, or some decl.
10783 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
10784 set to true if any of the array-section-subscript could have length
10785 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
10786 first array-section-subscript which is known not to have length
10787 of one. Given say:
10788 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
10789 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
10790 all are or may have length of 1, array-section-subscript [:2] is the
10791 first one knonwn not to have length 1. For array-section-subscript
10792 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
10793 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
10794 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
10795 case though, as some lengths could be zero. */
10796
10797static tree
10798handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
10799 bool &maybe_zero_len, unsigned int &first_non_one)
10800{
10801 tree ret, low_bound, length, type;
10802 if (TREE_CODE (t) != TREE_LIST)
10803 {
10804 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
10805 return error_mark_node;
10806 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10807 {
10808 if (DECL_P (t))
10809 error_at (OMP_CLAUSE_LOCATION (c),
10810 "%qD is not a variable in %qs clause", t,
10811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10812 else
10813 error_at (OMP_CLAUSE_LOCATION (c),
10814 "%qE is not a variable in %qs clause", t,
10815 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10816 return error_mark_node;
10817 }
10818 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
10819 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10820 {
10821 error_at (OMP_CLAUSE_LOCATION (c),
10822 "%qD is threadprivate variable in %qs clause", t,
10823 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10824 return error_mark_node;
10825 }
10826 return t;
10827 }
10828
10829 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
10830 maybe_zero_len, first_non_one);
10831 if (ret == error_mark_node || ret == NULL_TREE)
10832 return ret;
10833
10834 type = TREE_TYPE (ret);
10835 low_bound = TREE_PURPOSE (t);
10836 length = TREE_VALUE (t);
10837
10838 if (low_bound == error_mark_node || length == error_mark_node)
10839 return error_mark_node;
10840
10841 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
10842 {
10843 error_at (OMP_CLAUSE_LOCATION (c),
10844 "low bound %qE of array section does not have integral type",
10845 low_bound);
10846 return error_mark_node;
10847 }
10848 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
10849 {
10850 error_at (OMP_CLAUSE_LOCATION (c),
10851 "length %qE of array section does not have integral type",
10852 length);
10853 return error_mark_node;
10854 }
10855 if (low_bound
10856 && TREE_CODE (low_bound) == INTEGER_CST
10857 && TYPE_PRECISION (TREE_TYPE (low_bound))
10858 > TYPE_PRECISION (sizetype))
10859 low_bound = fold_convert (sizetype, low_bound);
10860 if (length
10861 && TREE_CODE (length) == INTEGER_CST
10862 && TYPE_PRECISION (TREE_TYPE (length))
10863 > TYPE_PRECISION (sizetype))
10864 length = fold_convert (sizetype, length);
10865 if (low_bound == NULL_TREE)
10866 low_bound = integer_zero_node;
10867
10868 if (length != NULL_TREE)
10869 {
10870 if (!integer_nonzerop (length))
10871 maybe_zero_len = true;
10872 if (first_non_one == types.length ()
10873 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
10874 first_non_one++;
10875 }
10876 if (TREE_CODE (type) == ARRAY_TYPE)
10877 {
10878 if (length == NULL_TREE
10879 && (TYPE_DOMAIN (type) == NULL_TREE
10880 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
10881 {
10882 error_at (OMP_CLAUSE_LOCATION (c),
10883 "for unknown bound array type length expression must "
10884 "be specified");
10885 return error_mark_node;
10886 }
10887 if (TREE_CODE (low_bound) == INTEGER_CST
10888 && tree_int_cst_sgn (low_bound) == -1)
10889 {
10890 error_at (OMP_CLAUSE_LOCATION (c),
10891 "negative low bound in array section in %qs clause",
10892 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10893 return error_mark_node;
10894 }
10895 if (length != NULL_TREE
10896 && TREE_CODE (length) == INTEGER_CST
10897 && tree_int_cst_sgn (length) == -1)
10898 {
10899 error_at (OMP_CLAUSE_LOCATION (c),
10900 "negative length in array section in %qs clause",
10901 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10902 return error_mark_node;
10903 }
10904 if (TYPE_DOMAIN (type)
10905 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
10906 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
10907 == INTEGER_CST)
10908 {
10909 tree size = size_binop (PLUS_EXPR,
10910 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
10911 size_one_node);
10912 if (TREE_CODE (low_bound) == INTEGER_CST)
10913 {
10914 if (tree_int_cst_lt (size, low_bound))
10915 {
10916 error_at (OMP_CLAUSE_LOCATION (c),
10917 "low bound %qE above array section size "
10918 "in %qs clause", low_bound,
10919 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10920 return error_mark_node;
10921 }
10922 if (tree_int_cst_equal (size, low_bound))
10923 maybe_zero_len = true;
10924 else if (length == NULL_TREE
10925 && first_non_one == types.length ()
10926 && tree_int_cst_equal
10927 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
10928 low_bound))
10929 first_non_one++;
10930 }
10931 else if (length == NULL_TREE)
10932 {
10933 maybe_zero_len = true;
10934 if (first_non_one == types.length ())
10935 first_non_one++;
10936 }
10937 if (length && TREE_CODE (length) == INTEGER_CST)
10938 {
10939 if (tree_int_cst_lt (size, length))
10940 {
10941 error_at (OMP_CLAUSE_LOCATION (c),
10942 "length %qE above array section size "
10943 "in %qs clause", length,
10944 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10945 return error_mark_node;
10946 }
10947 if (TREE_CODE (low_bound) == INTEGER_CST)
10948 {
10949 tree lbpluslen
10950 = size_binop (PLUS_EXPR,
10951 fold_convert (sizetype, low_bound),
10952 fold_convert (sizetype, length));
10953 if (TREE_CODE (lbpluslen) == INTEGER_CST
10954 && tree_int_cst_lt (size, lbpluslen))
10955 {
10956 error_at (OMP_CLAUSE_LOCATION (c),
10957 "high bound %qE above array section size "
10958 "in %qs clause", lbpluslen,
10959 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10960 return error_mark_node;
10961 }
10962 }
10963 }
10964 }
10965 else if (length == NULL_TREE)
10966 {
10967 maybe_zero_len = true;
10968 if (first_non_one == types.length ())
10969 first_non_one++;
10970 }
10971
10972 /* For [lb:] we will need to evaluate lb more than once. */
10973 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
10974 {
10975 tree lb = c_save_expr (low_bound);
10976 if (lb != low_bound)
10977 {
10978 TREE_PURPOSE (t) = lb;
10979 low_bound = lb;
10980 }
10981 }
10982 }
10983 else if (TREE_CODE (type) == POINTER_TYPE)
10984 {
10985 if (length == NULL_TREE)
10986 {
10987 error_at (OMP_CLAUSE_LOCATION (c),
10988 "for pointer type length expression must be specified");
10989 return error_mark_node;
10990 }
10991 /* If there is a pointer type anywhere but in the very first
10992 array-section-subscript, the array section can't be contiguous. */
10993 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
10994 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
10995 {
10996 error_at (OMP_CLAUSE_LOCATION (c),
10997 "array section is not contiguous in %qs clause",
10998 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10999 return error_mark_node;
11000 }
11001 }
11002 else
11003 {
11004 error_at (OMP_CLAUSE_LOCATION (c),
11005 "%qE does not have pointer or array type", ret);
11006 return error_mark_node;
11007 }
11008 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11009 types.safe_push (TREE_TYPE (ret));
11010 /* We will need to evaluate lb more than once. */
11011 tree lb = c_save_expr (low_bound);
11012 if (lb != low_bound)
11013 {
11014 TREE_PURPOSE (t) = lb;
11015 low_bound = lb;
11016 }
11017 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11018 return ret;
11019}
11020
11021/* Handle array sections for clause C. */
11022
11023static bool
11024handle_omp_array_sections (tree c)
11025{
11026 bool maybe_zero_len = false;
11027 unsigned int first_non_one = 0;
11028 vec<tree> types = vNULL;
11029 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11030 maybe_zero_len, first_non_one);
11031 if (first == error_mark_node)
11032 {
11033 types.release ();
11034 return true;
11035 }
11036 if (first == NULL_TREE)
11037 {
11038 types.release ();
11039 return false;
11040 }
11041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11042 {
11043 tree t = OMP_CLAUSE_DECL (c);
11044 tree tem = NULL_TREE;
11045 types.release ();
11046 /* Need to evaluate side effects in the length expressions
11047 if any. */
11048 while (TREE_CODE (t) == TREE_LIST)
11049 {
11050 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11051 {
11052 if (tem == NULL_TREE)
11053 tem = TREE_VALUE (t);
11054 else
11055 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11056 TREE_VALUE (t), tem);
11057 }
11058 t = TREE_CHAIN (t);
11059 }
11060 if (tem)
11061 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11062 first = c_fully_fold (first, false, NULL);
11063 OMP_CLAUSE_DECL (c) = first;
11064 }
11065 else
11066 {
11067 unsigned int num = types.length (), i;
11068 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11069 tree condition = NULL_TREE;
11070
11071 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11072 maybe_zero_len = true;
11073
11074 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11075 t = TREE_CHAIN (t))
11076 {
11077 tree low_bound = TREE_PURPOSE (t);
11078 tree length = TREE_VALUE (t);
11079
11080 i--;
11081 if (low_bound
11082 && TREE_CODE (low_bound) == INTEGER_CST
11083 && TYPE_PRECISION (TREE_TYPE (low_bound))
11084 > TYPE_PRECISION (sizetype))
11085 low_bound = fold_convert (sizetype, low_bound);
11086 if (length
11087 && TREE_CODE (length) == INTEGER_CST
11088 && TYPE_PRECISION (TREE_TYPE (length))
11089 > TYPE_PRECISION (sizetype))
11090 length = fold_convert (sizetype, length);
11091 if (low_bound == NULL_TREE)
11092 low_bound = integer_zero_node;
11093 if (!maybe_zero_len && i > first_non_one)
11094 {
11095 if (integer_nonzerop (low_bound))
11096 goto do_warn_noncontiguous;
11097 if (length != NULL_TREE
11098 && TREE_CODE (length) == INTEGER_CST
11099 && TYPE_DOMAIN (types[i])
11100 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11101 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11102 == INTEGER_CST)
11103 {
11104 tree size;
11105 size = size_binop (PLUS_EXPR,
11106 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11107 size_one_node);
11108 if (!tree_int_cst_equal (length, size))
11109 {
11110 do_warn_noncontiguous:
11111 error_at (OMP_CLAUSE_LOCATION (c),
11112 "array section is not contiguous in %qs "
11113 "clause",
11114 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11115 types.release ();
11116 return true;
11117 }
11118 }
11119 if (length != NULL_TREE
11120 && TREE_SIDE_EFFECTS (length))
11121 {
11122 if (side_effects == NULL_TREE)
11123 side_effects = length;
11124 else
11125 side_effects = build2 (COMPOUND_EXPR,
11126 TREE_TYPE (side_effects),
11127 length, side_effects);
11128 }
11129 }
11130 else
11131 {
11132 tree l;
11133
11134 if (i > first_non_one && length && integer_nonzerop (length))
11135 continue;
11136 if (length)
11137 l = fold_convert (sizetype, length);
11138 else
11139 {
11140 l = size_binop (PLUS_EXPR,
11141 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11142 size_one_node);
11143 l = size_binop (MINUS_EXPR, l,
11144 fold_convert (sizetype, low_bound));
11145 }
11146 if (i > first_non_one)
11147 {
11148 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11149 size_zero_node);
11150 if (condition == NULL_TREE)
11151 condition = l;
11152 else
11153 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11154 l, condition);
11155 }
11156 else if (size == NULL_TREE)
11157 {
11158 size = size_in_bytes (TREE_TYPE (types[i]));
11159 size = size_binop (MULT_EXPR, size, l);
11160 if (condition)
11161 size = fold_build3 (COND_EXPR, sizetype, condition,
11162 size, size_zero_node);
11163 }
11164 else
11165 size = size_binop (MULT_EXPR, size, l);
11166 }
11167 }
11168 types.release ();
11169 if (side_effects)
11170 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11171 first = c_fully_fold (first, false, NULL);
11172 OMP_CLAUSE_DECL (c) = first;
11173 if (size)
11174 size = c_fully_fold (size, false, NULL);
11175 OMP_CLAUSE_SIZE (c) = size;
11176 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11177 return false;
11178 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11179 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11180 if (!c_mark_addressable (t))
11181 return false;
11182 OMP_CLAUSE_DECL (c2) = t;
11183 t = build_fold_addr_expr (first);
11184 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11185 tree ptr = OMP_CLAUSE_DECL (c2);
11186 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11187 ptr = build_fold_addr_expr (ptr);
11188 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11189 ptrdiff_type_node, t,
11190 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11191 ptrdiff_type_node, ptr));
11192 t = c_fully_fold (t, false, NULL);
11193 OMP_CLAUSE_SIZE (c2) = t;
11194 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11195 OMP_CLAUSE_CHAIN (c) = c2;
11196 }
11197 return false;
11198}
11199
11200/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11201 an inline call. But, remap
11202 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11203 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11204
11205static tree
11206c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11207 tree decl, tree placeholder)
11208{
11209 copy_body_data id;
11210 struct pointer_map_t *decl_map = pointer_map_create ();
11211
11212 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11213 *pointer_map_insert (decl_map, omp_decl2) = decl;
11214 memset (&id, 0, sizeof (id));
11215 id.src_fn = DECL_CONTEXT (omp_decl1);
11216 id.dst_fn = current_function_decl;
11217 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11218 id.decl_map = decl_map;
11219
11220 id.copy_decl = copy_decl_no_change;
11221 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11222 id.transform_new_cfg = true;
11223 id.transform_return_to_modify = false;
11224 id.transform_lang_insert_block = NULL;
11225 id.eh_lp_nr = 0;
11226 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11227 pointer_map_destroy (decl_map);
11228 return stmt;
11229}
11230
11231/* Helper function of c_finish_omp_clauses, called via walk_tree.
11232 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11233
11234static tree
11235c_find_omp_placeholder_r (tree *tp, int *, void *data)
11236{
11237 if (*tp == (tree) data)
11238 return *tp;
11239 return NULL_TREE;
11240}
11241
953ff289
DN
11242/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11243 Remove any elements from the list that are invalid. */
11244
11245tree
11246c_finish_omp_clauses (tree clauses)
11247{
11248 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11249 bitmap_head aligned_head;
953ff289 11250 tree c, t, *pc = &clauses;
acf0174b
JJ
11251 bool branch_seen = false;
11252 bool copyprivate_seen = false;
11253 tree *nowait_clause = NULL;
953ff289
DN
11254
11255 bitmap_obstack_initialize (NULL);
11256 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11257 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11258 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11259 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11260
11261 for (pc = &clauses, c = clauses; c ; c = *pc)
11262 {
11263 bool remove = false;
11264 bool need_complete = false;
11265 bool need_implicitly_determined = false;
11266
aaf46ef9 11267 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11268 {
11269 case OMP_CLAUSE_SHARED:
953ff289
DN
11270 need_implicitly_determined = true;
11271 goto check_dup_generic;
11272
11273 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11274 need_complete = true;
11275 need_implicitly_determined = true;
11276 goto check_dup_generic;
11277
11278 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11279 need_implicitly_determined = true;
11280 t = OMP_CLAUSE_DECL (c);
acf0174b
JJ
11281 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11282 && FLOAT_TYPE_P (TREE_TYPE (t)))
953ff289
DN
11283 {
11284 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11285 const char *r_name = NULL;
11286
11287 switch (r_code)
11288 {
11289 case PLUS_EXPR:
11290 case MULT_EXPR:
11291 case MINUS_EXPR:
20906c66
JJ
11292 case MIN_EXPR:
11293 case MAX_EXPR:
953ff289
DN
11294 break;
11295 case BIT_AND_EXPR:
11296 r_name = "&";
11297 break;
11298 case BIT_XOR_EXPR:
11299 r_name = "^";
11300 break;
11301 case BIT_IOR_EXPR:
11302 r_name = "|";
11303 break;
11304 case TRUTH_ANDIF_EXPR:
11305 r_name = "&&";
11306 break;
11307 case TRUTH_ORIF_EXPR:
11308 r_name = "||";
11309 break;
11310 default:
11311 gcc_unreachable ();
11312 }
11313 if (r_name)
11314 {
c2255bc4
AH
11315 error_at (OMP_CLAUSE_LOCATION (c),
11316 "%qE has invalid type for %<reduction(%s)%>",
11317 t, r_name);
953ff289
DN
11318 remove = true;
11319 }
11320 }
acf0174b
JJ
11321 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11322 {
11323 error_at (OMP_CLAUSE_LOCATION (c),
11324 "user defined reduction not found for %qD", t);
11325 remove = true;
11326 }
11327 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11328 {
11329 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11330 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11331 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11332 VAR_DECL, NULL_TREE, type);
11333 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11334 DECL_ARTIFICIAL (placeholder) = 1;
11335 DECL_IGNORED_P (placeholder) = 1;
11336 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11337 c_mark_addressable (placeholder);
11338 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11339 c_mark_addressable (OMP_CLAUSE_DECL (c));
11340 OMP_CLAUSE_REDUCTION_MERGE (c)
11341 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11342 TREE_VEC_ELT (list, 0),
11343 TREE_VEC_ELT (list, 1),
11344 OMP_CLAUSE_DECL (c), placeholder);
11345 OMP_CLAUSE_REDUCTION_MERGE (c)
11346 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11347 void_type_node, NULL_TREE,
11348 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11349 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11350 if (TREE_VEC_LENGTH (list) == 6)
11351 {
11352 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11353 c_mark_addressable (OMP_CLAUSE_DECL (c));
11354 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11355 c_mark_addressable (placeholder);
11356 tree init = TREE_VEC_ELT (list, 5);
11357 if (init == error_mark_node)
11358 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11359 OMP_CLAUSE_REDUCTION_INIT (c)
11360 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11361 TREE_VEC_ELT (list, 3),
11362 OMP_CLAUSE_DECL (c), placeholder);
11363 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11364 OMP_CLAUSE_REDUCTION_INIT (c)
11365 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11366 OMP_CLAUSE_REDUCTION_INIT (c));
11367 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11368 c_find_omp_placeholder_r,
11369 placeholder, NULL))
11370 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11371 }
11372 else
11373 {
11374 tree init;
11375 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11376 init = build_constructor (TREE_TYPE (t), NULL);
11377 else
11378 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11379 OMP_CLAUSE_REDUCTION_INIT (c)
11380 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11381 }
11382 OMP_CLAUSE_REDUCTION_INIT (c)
11383 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11384 void_type_node, NULL_TREE,
11385 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11386 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11387 }
953ff289
DN
11388 goto check_dup_generic;
11389
11390 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11391 copyprivate_seen = true;
11392 if (nowait_clause)
11393 {
11394 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11395 "%<nowait%> clause must not be used together "
11396 "with %<copyprivate%>");
11397 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11398 nowait_clause = NULL;
11399 }
953ff289
DN
11400 goto check_dup_generic;
11401
11402 case OMP_CLAUSE_COPYIN:
953ff289
DN
11403 t = OMP_CLAUSE_DECL (c);
11404 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11405 {
c2255bc4
AH
11406 error_at (OMP_CLAUSE_LOCATION (c),
11407 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289
DN
11408 remove = true;
11409 }
11410 goto check_dup_generic;
11411
acf0174b
JJ
11412 case OMP_CLAUSE_LINEAR:
11413 t = OMP_CLAUSE_DECL (c);
11414 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11415 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11416 {
11417 error_at (OMP_CLAUSE_LOCATION (c),
11418 "linear clause applied to non-integral non-pointer "
11419 "variable with type %qT", TREE_TYPE (t));
11420 remove = true;
11421 break;
11422 }
11423 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11424 {
11425 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11426 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11427 OMP_CLAUSE_DECL (c), s);
11428 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11429 sizetype, s, OMP_CLAUSE_DECL (c));
11430 if (s == error_mark_node)
11431 s = size_one_node;
11432 OMP_CLAUSE_LINEAR_STEP (c) = s;
11433 }
11434 goto check_dup_generic;
11435
953ff289
DN
11436 check_dup_generic:
11437 t = OMP_CLAUSE_DECL (c);
11438 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11439 {
c2255bc4 11440 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
11441 "%qE is not a variable in clause %qs", t,
11442 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
11443 remove = true;
11444 }
11445 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11446 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11447 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11448 {
c2255bc4
AH
11449 error_at (OMP_CLAUSE_LOCATION (c),
11450 "%qE appears more than once in data clauses", t);
953ff289
DN
11451 remove = true;
11452 }
11453 else
11454 bitmap_set_bit (&generic_head, DECL_UID (t));
11455 break;
11456
11457 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
11458 t = OMP_CLAUSE_DECL (c);
11459 need_complete = true;
11460 need_implicitly_determined = true;
11461 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11462 {
c2255bc4
AH
11463 error_at (OMP_CLAUSE_LOCATION (c),
11464 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
11465 remove = true;
11466 }
11467 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11468 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11469 {
c2255bc4
AH
11470 error_at (OMP_CLAUSE_LOCATION (c),
11471 "%qE appears more than once in data clauses", t);
953ff289
DN
11472 remove = true;
11473 }
11474 else
11475 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11476 break;
11477
11478 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
11479 t = OMP_CLAUSE_DECL (c);
11480 need_complete = true;
11481 need_implicitly_determined = true;
11482 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11483 {
c2255bc4
AH
11484 error_at (OMP_CLAUSE_LOCATION (c),
11485 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
11486 remove = true;
11487 }
11488 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11489 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11490 {
c2255bc4
AH
11491 error_at (OMP_CLAUSE_LOCATION (c),
11492 "%qE appears more than once in data clauses", t);
953ff289
DN
11493 remove = true;
11494 }
11495 else
11496 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11497 break;
11498
acf0174b
JJ
11499 case OMP_CLAUSE_ALIGNED:
11500 t = OMP_CLAUSE_DECL (c);
11501 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11502 {
11503 error_at (OMP_CLAUSE_LOCATION (c),
11504 "%qE is not a variable in %<aligned%> clause", t);
11505 remove = true;
11506 }
11507 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11508 {
11509 error_at (OMP_CLAUSE_LOCATION (c),
11510 "%qE appears more than once in %<aligned%> clauses",
11511 t);
11512 remove = true;
11513 }
11514 else
11515 bitmap_set_bit (&aligned_head, DECL_UID (t));
11516 break;
11517
11518 case OMP_CLAUSE_DEPEND:
11519 t = OMP_CLAUSE_DECL (c);
11520 if (TREE_CODE (t) == TREE_LIST)
11521 {
11522 if (handle_omp_array_sections (c))
11523 remove = true;
11524 break;
11525 }
11526 if (t == error_mark_node)
11527 remove = true;
11528 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11529 {
11530 error_at (OMP_CLAUSE_LOCATION (c),
11531 "%qE is not a variable in %<depend%> clause", t);
11532 remove = true;
11533 }
11534 else if (!c_mark_addressable (t))
11535 remove = true;
11536 break;
11537
11538 case OMP_CLAUSE_MAP:
11539 case OMP_CLAUSE_TO:
11540 case OMP_CLAUSE_FROM:
11541 t = OMP_CLAUSE_DECL (c);
11542 if (TREE_CODE (t) == TREE_LIST)
11543 {
11544 if (handle_omp_array_sections (c))
11545 remove = true;
11546 else
11547 {
11548 t = OMP_CLAUSE_DECL (c);
11549 if (!COMPLETE_TYPE_P (TREE_TYPE (t)))
11550 {
11551 error_at (OMP_CLAUSE_LOCATION (c),
11552 "array section does not have mappable type "
11553 "in %qs clause",
11554 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11555 remove = true;
11556 }
11557 }
11558 break;
11559 }
11560 if (t == error_mark_node)
11561 remove = true;
11562 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11563 {
11564 error_at (OMP_CLAUSE_LOCATION (c),
11565 "%qE is not a variable in %qs clause", t,
11566 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11567 remove = true;
11568 }
11569 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11570 {
11571 error_at (OMP_CLAUSE_LOCATION (c),
11572 "%qD is threadprivate variable in %qs clause", t,
11573 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11574 remove = true;
11575 }
11576 else if (!c_mark_addressable (t))
11577 remove = true;
11578 else if (!COMPLETE_TYPE_P (TREE_TYPE (t))
11579 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11580 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER))
11581 {
11582 error_at (OMP_CLAUSE_LOCATION (c),
11583 "%qD does not have a mappable type in %qs clause", t,
11584 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11585 remove = true;
11586 }
11587 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
11588 {
11589 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11590 error ("%qD appears more than once in motion clauses", t);
11591 else
11592 error ("%qD appears more than once in map clauses", t);
11593 remove = true;
11594 }
11595 else
11596 bitmap_set_bit (&generic_head, DECL_UID (t));
11597 break;
11598
11599 case OMP_CLAUSE_UNIFORM:
11600 t = OMP_CLAUSE_DECL (c);
11601 if (TREE_CODE (t) != PARM_DECL)
11602 {
11603 if (DECL_P (t))
11604 error_at (OMP_CLAUSE_LOCATION (c),
11605 "%qD is not an argument in %<uniform%> clause", t);
11606 else
11607 error_at (OMP_CLAUSE_LOCATION (c),
11608 "%qE is not an argument in %<uniform%> clause", t);
11609 remove = true;
11610 }
11611 break;
11612
11613 case OMP_CLAUSE_NOWAIT:
11614 if (copyprivate_seen)
11615 {
11616 error_at (OMP_CLAUSE_LOCATION (c),
11617 "%<nowait%> clause must not be used together "
11618 "with %<copyprivate%>");
11619 remove = true;
11620 break;
11621 }
11622 nowait_clause = pc;
11623 pc = &OMP_CLAUSE_CHAIN (c);
11624 continue;
11625
953ff289
DN
11626 case OMP_CLAUSE_IF:
11627 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
11628 case OMP_CLAUSE_NUM_TEAMS:
11629 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 11630 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
11631 case OMP_CLAUSE_ORDERED:
11632 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
11633 case OMP_CLAUSE_UNTIED:
11634 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
11635 case OMP_CLAUSE_FINAL:
11636 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
11637 case OMP_CLAUSE_SAFELEN:
11638 case OMP_CLAUSE_SIMDLEN:
11639 case OMP_CLAUSE_DEVICE:
11640 case OMP_CLAUSE_DIST_SCHEDULE:
11641 case OMP_CLAUSE_PARALLEL:
11642 case OMP_CLAUSE_FOR:
11643 case OMP_CLAUSE_SECTIONS:
11644 case OMP_CLAUSE_TASKGROUP:
11645 case OMP_CLAUSE_PROC_BIND:
11646 pc = &OMP_CLAUSE_CHAIN (c);
11647 continue;
11648
11649 case OMP_CLAUSE_INBRANCH:
11650 case OMP_CLAUSE_NOTINBRANCH:
11651 if (branch_seen)
11652 {
11653 error_at (OMP_CLAUSE_LOCATION (c),
11654 "%<inbranch%> clause is incompatible with "
11655 "%<notinbranch%>");
11656 remove = true;
11657 break;
11658 }
11659 branch_seen = true;
953ff289
DN
11660 pc = &OMP_CLAUSE_CHAIN (c);
11661 continue;
11662
11663 default:
11664 gcc_unreachable ();
11665 }
11666
11667 if (!remove)
11668 {
11669 t = OMP_CLAUSE_DECL (c);
11670
11671 if (need_complete)
11672 {
11673 t = require_complete_type (t);
11674 if (t == error_mark_node)
11675 remove = true;
11676 }
11677
11678 if (need_implicitly_determined)
11679 {
11680 const char *share_name = NULL;
11681
11682 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11683 share_name = "threadprivate";
11684 else switch (c_omp_predetermined_sharing (t))
11685 {
11686 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
11687 break;
11688 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
11689 /* const vars may be specified in firstprivate clause. */
11690 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11691 && TREE_READONLY (t))
11692 break;
953ff289
DN
11693 share_name = "shared";
11694 break;
11695 case OMP_CLAUSE_DEFAULT_PRIVATE:
11696 share_name = "private";
11697 break;
11698 default:
11699 gcc_unreachable ();
11700 }
11701 if (share_name)
11702 {
c2255bc4
AH
11703 error_at (OMP_CLAUSE_LOCATION (c),
11704 "%qE is predetermined %qs for %qs",
acf0174b
JJ
11705 t, share_name,
11706 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
11707 remove = true;
11708 }
11709 }
11710 }
11711
11712 if (remove)
11713 *pc = OMP_CLAUSE_CHAIN (c);
11714 else
11715 pc = &OMP_CLAUSE_CHAIN (c);
11716 }
11717
11718 bitmap_obstack_release (NULL);
11719 return clauses;
11720}
9ae165a0 11721
0a35513e
AH
11722/* Create a transaction node. */
11723
11724tree
11725c_finish_transaction (location_t loc, tree block, int flags)
11726{
11727 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
11728 if (flags & TM_STMT_ATTR_OUTER)
11729 TRANSACTION_EXPR_OUTER (stmt) = 1;
11730 if (flags & TM_STMT_ATTR_RELAXED)
11731 TRANSACTION_EXPR_RELAXED (stmt) = 1;
11732 return add_stmt (stmt);
11733}
11734
9ae165a0
DG
11735/* Make a variant type in the proper way for C/C++, propagating qualifiers
11736 down to the element type of an array. */
11737
11738tree
11739c_build_qualified_type (tree type, int type_quals)
11740{
11741 if (type == error_mark_node)
11742 return type;
11743
11744 if (TREE_CODE (type) == ARRAY_TYPE)
11745 {
11746 tree t;
11747 tree element_type = c_build_qualified_type (TREE_TYPE (type),
11748 type_quals);
11749
11750 /* See if we already have an identically qualified type. */
11751 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11752 {
11753 if (TYPE_QUALS (strip_array_types (t)) == type_quals
11754 && TYPE_NAME (t) == TYPE_NAME (type)
11755 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
11756 && attribute_list_equal (TYPE_ATTRIBUTES (t),
11757 TYPE_ATTRIBUTES (type)))
11758 break;
11759 }
11760 if (!t)
11761 {
11762 tree domain = TYPE_DOMAIN (type);
11763
11764 t = build_variant_type_copy (type);
11765 TREE_TYPE (t) = element_type;
11766
11767 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
11768 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
11769 SET_TYPE_STRUCTURAL_EQUALITY (t);
11770 else if (TYPE_CANONICAL (element_type) != element_type
11771 || (domain && TYPE_CANONICAL (domain) != domain))
11772 {
b8698a0f 11773 tree unqualified_canon
9ae165a0 11774 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 11775 domain? TYPE_CANONICAL (domain)
9ae165a0 11776 : NULL_TREE);
b8698a0f 11777 TYPE_CANONICAL (t)
9ae165a0
DG
11778 = c_build_qualified_type (unqualified_canon, type_quals);
11779 }
11780 else
11781 TYPE_CANONICAL (t) = t;
11782 }
11783 return t;
11784 }
11785
11786 /* A restrict-qualified pointer type must be a pointer to object or
11787 incomplete type. Note that the use of POINTER_TYPE_P also allows
11788 REFERENCE_TYPEs, which is appropriate for C++. */
11789 if ((type_quals & TYPE_QUAL_RESTRICT)
11790 && (!POINTER_TYPE_P (type)
11791 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11792 {
11793 error ("invalid use of %<restrict%>");
11794 type_quals &= ~TYPE_QUAL_RESTRICT;
11795 }
11796
11797 return build_qualified_type (type, type_quals);
11798}
72b5577d
ILT
11799
11800/* Build a VA_ARG_EXPR for the C parser. */
11801
11802tree
c2255bc4 11803c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
11804{
11805 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11806 warning_at (loc, OPT_Wc___compat,
11807 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 11808 return build_va_arg (loc, expr, type);
72b5577d 11809}
acf0174b
JJ
11810
11811/* Return truthvalue of whether T1 is the same tree structure as T2.
11812 Return 1 if they are the same. Return 0 if they are different. */
11813
11814bool
11815c_tree_equal (tree t1, tree t2)
11816{
11817 enum tree_code code1, code2;
11818
11819 if (t1 == t2)
11820 return true;
11821 if (!t1 || !t2)
11822 return false;
11823
11824 for (code1 = TREE_CODE (t1);
11825 CONVERT_EXPR_CODE_P (code1)
11826 || code1 == NON_LVALUE_EXPR;
11827 code1 = TREE_CODE (t1))
11828 t1 = TREE_OPERAND (t1, 0);
11829 for (code2 = TREE_CODE (t2);
11830 CONVERT_EXPR_CODE_P (code2)
11831 || code2 == NON_LVALUE_EXPR;
11832 code2 = TREE_CODE (t2))
11833 t2 = TREE_OPERAND (t2, 0);
11834
11835 /* They might have become equal now. */
11836 if (t1 == t2)
11837 return true;
11838
11839 if (code1 != code2)
11840 return false;
11841
11842 switch (code1)
11843 {
11844 case INTEGER_CST:
11845 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
11846 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
11847
11848 case REAL_CST:
11849 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
11850
11851 case STRING_CST:
11852 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
11853 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
11854 TREE_STRING_LENGTH (t1));
11855
11856 case FIXED_CST:
11857 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
11858 TREE_FIXED_CST (t2));
11859
11860 case COMPLEX_CST:
11861 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
11862 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
11863
11864 case VECTOR_CST:
11865 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
11866
11867 case CONSTRUCTOR:
11868 /* We need to do this when determining whether or not two
11869 non-type pointer to member function template arguments
11870 are the same. */
11871 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
11872 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
11873 return false;
11874 {
11875 tree field, value;
11876 unsigned int i;
11877 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
11878 {
11879 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
11880 if (!c_tree_equal (field, elt2->index)
11881 || !c_tree_equal (value, elt2->value))
11882 return false;
11883 }
11884 }
11885 return true;
11886
11887 case TREE_LIST:
11888 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
11889 return false;
11890 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
11891 return false;
11892 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
11893
11894 case SAVE_EXPR:
11895 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
11896
11897 case CALL_EXPR:
11898 {
11899 tree arg1, arg2;
11900 call_expr_arg_iterator iter1, iter2;
11901 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
11902 return false;
11903 for (arg1 = first_call_expr_arg (t1, &iter1),
11904 arg2 = first_call_expr_arg (t2, &iter2);
11905 arg1 && arg2;
11906 arg1 = next_call_expr_arg (&iter1),
11907 arg2 = next_call_expr_arg (&iter2))
11908 if (!c_tree_equal (arg1, arg2))
11909 return false;
11910 if (arg1 || arg2)
11911 return false;
11912 return true;
11913 }
11914
11915 case TARGET_EXPR:
11916 {
11917 tree o1 = TREE_OPERAND (t1, 0);
11918 tree o2 = TREE_OPERAND (t2, 0);
11919
11920 /* Special case: if either target is an unallocated VAR_DECL,
11921 it means that it's going to be unified with whatever the
11922 TARGET_EXPR is really supposed to initialize, so treat it
11923 as being equivalent to anything. */
11924 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
11925 && !DECL_RTL_SET_P (o1))
11926 /*Nop*/;
11927 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
11928 && !DECL_RTL_SET_P (o2))
11929 /*Nop*/;
11930 else if (!c_tree_equal (o1, o2))
11931 return false;
11932
11933 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
11934 }
11935
11936 case COMPONENT_REF:
11937 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
11938 return false;
11939 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
11940
11941 case PARM_DECL:
11942 case VAR_DECL:
11943 case CONST_DECL:
11944 case FIELD_DECL:
11945 case FUNCTION_DECL:
11946 case IDENTIFIER_NODE:
11947 case SSA_NAME:
11948 return false;
11949
11950 case TREE_VEC:
11951 {
11952 unsigned ix;
11953 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
11954 return false;
11955 for (ix = TREE_VEC_LENGTH (t1); ix--;)
11956 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
11957 TREE_VEC_ELT (t2, ix)))
11958 return false;
11959 return true;
11960 }
11961
11962 default:
11963 break;
11964 }
11965
11966 switch (TREE_CODE_CLASS (code1))
11967 {
11968 case tcc_unary:
11969 case tcc_binary:
11970 case tcc_comparison:
11971 case tcc_expression:
11972 case tcc_vl_exp:
11973 case tcc_reference:
11974 case tcc_statement:
11975 {
11976 int i, n = TREE_OPERAND_LENGTH (t1);
11977
11978 switch (code1)
11979 {
11980 case PREINCREMENT_EXPR:
11981 case PREDECREMENT_EXPR:
11982 case POSTINCREMENT_EXPR:
11983 case POSTDECREMENT_EXPR:
11984 n = 1;
11985 break;
11986 case ARRAY_REF:
11987 n = 2;
11988 break;
11989 default:
11990 break;
11991 }
11992
11993 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
11994 && n != TREE_OPERAND_LENGTH (t2))
11995 return false;
11996
11997 for (i = 0; i < n; ++i)
11998 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
11999 return false;
12000
12001 return true;
12002 }
12003
12004 case tcc_type:
12005 return comptypes (t1, t2);
12006 default:
12007 gcc_unreachable ();
12008 }
12009 /* We can get here with --disable-checking. */
12010 return false;
12011}