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