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