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