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