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