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