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