]> gcc.gnu.org Git - gcc.git/blame - gcc/c-typeck.c
c-common.h (default_conversion): Remove.
[gcc.git] / gcc / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
0953878d
JM
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
400fbf9f 4
1322177d 5This file is part of GCC.
400fbf9f 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
400fbf9f 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
400fbf9f
JW
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
400fbf9f
JW
21
22
23/* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
5088b058 26 and some optimization. */
400fbf9f
JW
27
28#include "config.h"
670ee920 29#include "system.h"
4977bab6
ZW
30#include "coretypes.h"
31#include "tm.h"
742b62e7 32#include "rtl.h"
400fbf9f 33#include "tree.h"
e57e265b 34#include "langhooks.h"
400fbf9f 35#include "c-tree.h"
6baf1cc8 36#include "tm_p.h"
400fbf9f 37#include "flags.h"
e14417fa 38#include "output.h"
234042f4 39#include "expr.h"
5f6da302 40#include "toplev.h"
ab87f8c8 41#include "intl.h"
4dd7201e 42#include "ggc.h"
672a6f42 43#include "target.h"
325c3691 44#include "tree-iterator.h"
3a5b9284 45#include "tree-gimple.h"
089efaa4 46#include "tree-flow.h"
325c3691 47
2ac2f164
JM
48/* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50enum impl_conv {
51 ic_argpass,
6dcc04b0 52 ic_argpass_nonproto,
2ac2f164
JM
53 ic_assign,
54 ic_init,
55 ic_return
56};
57
bc4b653b
JM
58/* The level of nesting inside "__alignof__". */
59int in_alignof;
60
61/* The level of nesting inside "sizeof". */
62int in_sizeof;
63
64/* The level of nesting inside "typeof". */
65int in_typeof;
400fbf9f 66
b71c7f8a 67/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 68 message within this initializer. */
b71c7f8a 69static int missing_braces_mentioned;
103b7b17 70
bf730f15
RS
71static int require_constant_value;
72static int require_constant_elements;
73
f55ade6e 74static tree qualify_type (tree, tree);
132da1a5 75static int tagged_types_tu_compatible_p (tree, tree);
f55ade6e 76static int comp_target_types (tree, tree, int);
132da1a5
JM
77static int function_types_compatible_p (tree, tree);
78static int type_lists_compatible_p (tree, tree);
f55ade6e
AJ
79static tree decl_constant_value_for_broken_optimization (tree);
80static tree default_function_array_conversion (tree);
81static tree lookup_field (tree, tree);
f55ade6e
AJ
82static tree convert_arguments (tree, tree, tree, tree);
83static tree pointer_diff (tree, tree);
2ac2f164 84static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
f55ade6e 85 int);
f55ade6e
AJ
86static tree valid_compound_expr_initializer (tree, tree);
87static void push_string (const char *);
88static void push_member_name (tree);
89static void push_array_bounds (int);
90static int spelling_length (void);
91static char *print_spelling (char *);
92static void warning_init (const char *);
916c5919
JM
93static tree digest_init (tree, tree, bool, int);
94static void output_init_element (tree, bool, tree, tree, int);
f55ade6e
AJ
95static void output_pending_init_elements (int);
96static int set_designator (int);
97static void push_range_stack (tree);
98static void add_pending_init (tree, tree);
99static void set_nonincremental_init (void);
100static void set_nonincremental_init_from_string (tree);
101static tree find_init_member (tree);
9bf24266 102static void readonly_error (tree, enum lvalue_use);
37dc0d8d
JM
103static int lvalue_or_else (tree, enum lvalue_use);
104static int lvalue_p (tree);
4e2fb7de 105static void record_maybe_used_decl (tree);
400fbf9f
JW
106\f
107/* Do `exp = require_complete_type (exp);' to make sure exp
108 does not have an incomplete type. (That includes void types.) */
109
110tree
2f6e4e97 111require_complete_type (tree value)
400fbf9f
JW
112{
113 tree type = TREE_TYPE (value);
114
c3d5c3fa 115 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
116 return error_mark_node;
117
400fbf9f 118 /* First, detect a valid value with a complete type. */
d0f062fb 119 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
120 return value;
121
7a228918 122 c_incomplete_type_error (value, type);
400fbf9f
JW
123 return error_mark_node;
124}
125
126/* Print an error message for invalid use of an incomplete type.
127 VALUE is the expression that was used (or 0 if that isn't known)
128 and TYPE is the type that was invalid. */
129
130void
2f6e4e97 131c_incomplete_type_error (tree value, tree type)
400fbf9f 132{
5d5993dd 133 const char *type_code_string;
400fbf9f
JW
134
135 /* Avoid duplicate error message. */
136 if (TREE_CODE (type) == ERROR_MARK)
137 return;
138
139 if (value != 0 && (TREE_CODE (value) == VAR_DECL
140 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 141 error ("%qD has an incomplete type", value);
400fbf9f
JW
142 else
143 {
144 retry:
145 /* We must print an error message. Be clever about what it says. */
146
147 switch (TREE_CODE (type))
148 {
149 case RECORD_TYPE:
ab87f8c8 150 type_code_string = "struct";
400fbf9f
JW
151 break;
152
153 case UNION_TYPE:
ab87f8c8 154 type_code_string = "union";
400fbf9f
JW
155 break;
156
157 case ENUMERAL_TYPE:
ab87f8c8 158 type_code_string = "enum";
400fbf9f
JW
159 break;
160
161 case VOID_TYPE:
162 error ("invalid use of void expression");
163 return;
164
165 case ARRAY_TYPE:
166 if (TYPE_DOMAIN (type))
167 {
fba78abb
RH
168 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
169 {
170 error ("invalid use of flexible array member");
171 return;
172 }
400fbf9f
JW
173 type = TREE_TYPE (type);
174 goto retry;
175 }
176 error ("invalid use of array with unspecified bounds");
177 return;
178
179 default:
366de0ce 180 gcc_unreachable ();
400fbf9f
JW
181 }
182
183 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
184 error ("invalid use of undefined type %<%s %E%>",
185 type_code_string, TYPE_NAME (type));
400fbf9f
JW
186 else
187 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 188 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
189 }
190}
191
ab393bf1
NB
192/* Given a type, apply default promotions wrt unnamed function
193 arguments and return the new type. */
194
195tree
2f6e4e97 196c_type_promotes_to (tree type)
ab393bf1
NB
197{
198 if (TYPE_MAIN_VARIANT (type) == float_type_node)
199 return double_type_node;
200
201 if (c_promoting_integer_type_p (type))
202 {
203 /* Preserve unsignedness if not really getting any wider. */
8df83eae 204 if (TYPE_UNSIGNED (type)
ab393bf1
NB
205 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
206 return unsigned_type_node;
207 return integer_type_node;
208 }
209
210 return type;
211}
212
400fbf9f
JW
213/* Return a variant of TYPE which has all the type qualifiers of LIKE
214 as well as those of TYPE. */
215
216static tree
2f6e4e97 217qualify_type (tree type, tree like)
400fbf9f 218{
2f6e4e97 219 return c_build_qualified_type (type,
afbadaa7 220 TYPE_QUALS (type) | TYPE_QUALS (like));
400fbf9f
JW
221}
222\f
10bc1b1b 223/* Return the composite type of two compatible types.
5305f6d7 224
10bc1b1b
JM
225 We assume that comptypes has already been done and returned
226 nonzero; if that isn't so, this may crash. In particular, we
227 assume that qualifiers match. */
400fbf9f
JW
228
229tree
10bc1b1b 230composite_type (tree t1, tree t2)
400fbf9f 231{
b3694847
SS
232 enum tree_code code1;
233 enum tree_code code2;
4b027d16 234 tree attributes;
400fbf9f
JW
235
236 /* Save time if the two types are the same. */
237
238 if (t1 == t2) return t1;
239
240 /* If one type is nonsense, use the other. */
241 if (t1 == error_mark_node)
242 return t2;
243 if (t2 == error_mark_node)
244 return t1;
245
10bc1b1b
JM
246 code1 = TREE_CODE (t1);
247 code2 = TREE_CODE (t2);
248
d9525bec 249 /* Merge the attributes. */
5fd9b178 250 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 251
10bc1b1b
JM
252 /* If one is an enumerated type and the other is the compatible
253 integer type, the composite type might be either of the two
254 (DR#013 question 3). For consistency, use the enumerated type as
255 the composite type. */
400fbf9f 256
10bc1b1b
JM
257 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
258 return t1;
259 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
260 return t2;
75326e8c 261
366de0ce 262 gcc_assert (code1 == code2);
b6a10c9f 263
400fbf9f
JW
264 switch (code1)
265 {
400fbf9f 266 case POINTER_TYPE:
10bc1b1b 267 /* For two pointers, do this recursively on the target type. */
400fbf9f 268 {
3932261a
MM
269 tree pointed_to_1 = TREE_TYPE (t1);
270 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b
JM
271 tree target = composite_type (pointed_to_1, pointed_to_2);
272 t1 = build_pointer_type (target);
fe7080d2
AP
273 t1 = build_type_attribute_variant (t1, attributes);
274 return qualify_type (t1, t2);
400fbf9f 275 }
400fbf9f
JW
276
277 case ARRAY_TYPE:
278 {
10bc1b1b 279 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
280 int quals;
281 tree unqual_elt;
282
de46b2fe 283 /* We should not have any type quals on arrays at all. */
366de0ce 284 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
de46b2fe 285
400fbf9f
JW
286 /* Save space: see if the result is identical to one of the args. */
287 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
4b027d16 288 return build_type_attribute_variant (t1, attributes);
400fbf9f 289 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
4b027d16 290 return build_type_attribute_variant (t2, attributes);
de46b2fe
AP
291
292 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
293 return build_type_attribute_variant (t1, attributes);
294 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
295 return build_type_attribute_variant (t2, attributes);
296
46df2823
JM
297 /* Merge the element types, and have a size if either arg has
298 one. We may have qualifiers on the element types. To set
299 up TYPE_MAIN_VARIANT correctly, we need to form the
300 composite of the unqualified types and add the qualifiers
301 back at the end. */
302 quals = TYPE_QUALS (strip_array_types (elt));
303 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
304 t1 = build_array_type (unqual_elt,
305 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
306 t1 = c_build_qualified_type (t1, quals);
de46b2fe 307 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
308 }
309
310 case FUNCTION_TYPE:
311 /* Function types: prefer the one that specified arg types.
312 If both do, merge the arg types. Also merge the return types. */
313 {
10bc1b1b 314 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
315 tree p1 = TYPE_ARG_TYPES (t1);
316 tree p2 = TYPE_ARG_TYPES (t2);
317 int len;
318 tree newargs, n;
319 int i;
320
321 /* Save space: see if the result is identical to one of the args. */
3f75a254 322 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 323 return build_type_attribute_variant (t1, attributes);
3f75a254 324 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 325 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
326
327 /* Simple way if one arg fails to specify argument types. */
328 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 329 {
fe7080d2
AP
330 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
331 t1 = build_type_attribute_variant (t1, attributes);
332 return qualify_type (t1, t2);
4b027d16 333 }
400fbf9f 334 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
335 {
336 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
337 t1 = build_type_attribute_variant (t1, attributes);
338 return qualify_type (t1, t2);
4b027d16 339 }
400fbf9f
JW
340
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
f75fbaf7
ZW
343 /* Tell global_bindings_p to return false so that variable_size
344 doesn't abort on VLAs in parameter types. */
345 c_override_global_bindings_to_false = true;
2f4e8f2b 346
400fbf9f
JW
347 len = list_length (p1);
348 newargs = 0;
349
350 for (i = 0; i < len; i++)
8d9bfdc5 351 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
352
353 n = newargs;
354
355 for (; p1;
356 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
357 {
358 /* A null type means arg type is not specified.
359 Take whatever the other function type has. */
360 if (TREE_VALUE (p1) == 0)
361 {
362 TREE_VALUE (n) = TREE_VALUE (p2);
363 goto parm_done;
364 }
365 if (TREE_VALUE (p2) == 0)
366 {
367 TREE_VALUE (n) = TREE_VALUE (p1);
368 goto parm_done;
369 }
2f6e4e97 370
400fbf9f
JW
371 /* Given wait (union {union wait *u; int *i} *)
372 and wait (union wait *),
373 prefer union wait * as type of parm. */
374 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
375 && TREE_VALUE (p1) != TREE_VALUE (p2))
376 {
377 tree memb;
58cb41e6
JJ
378 tree mv2 = TREE_VALUE (p2);
379 if (mv2 && mv2 != error_mark_node
380 && TREE_CODE (mv2) != ARRAY_TYPE)
381 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f
JW
382 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
383 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
384 {
385 tree mv3 = TREE_TYPE (memb);
386 if (mv3 && mv3 != error_mark_node
387 && TREE_CODE (mv3) != ARRAY_TYPE)
388 mv3 = TYPE_MAIN_VARIANT (mv3);
389 if (comptypes (mv3, mv2))
390 {
391 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
392 TREE_VALUE (p2));
393 if (pedantic)
394 pedwarn ("function types not truly compatible in ISO C");
395 goto parm_done;
396 }
397 }
400fbf9f
JW
398 }
399 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
400 && TREE_VALUE (p2) != TREE_VALUE (p1))
401 {
402 tree memb;
58cb41e6
JJ
403 tree mv1 = TREE_VALUE (p1);
404 if (mv1 && mv1 != error_mark_node
405 && TREE_CODE (mv1) != ARRAY_TYPE)
406 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f
JW
407 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
408 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
409 {
410 tree mv3 = TREE_TYPE (memb);
411 if (mv3 && mv3 != error_mark_node
412 && TREE_CODE (mv3) != ARRAY_TYPE)
413 mv3 = TYPE_MAIN_VARIANT (mv3);
414 if (comptypes (mv3, mv1))
415 {
416 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
417 TREE_VALUE (p1));
418 if (pedantic)
419 pedwarn ("function types not truly compatible in ISO C");
420 goto parm_done;
421 }
422 }
400fbf9f 423 }
10bc1b1b 424 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
425 parm_done: ;
426 }
427
f75fbaf7 428 c_override_global_bindings_to_false = false;
4b027d16 429 t1 = build_function_type (valtype, newargs);
fe7080d2 430 t1 = qualify_type (t1, t2);
0f41302f 431 /* ... falls through ... */
400fbf9f
JW
432 }
433
434 default:
4b027d16 435 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
436 }
437
438}
10bc1b1b
JM
439
440/* Return the type of a conditional expression between pointers to
441 possibly differently qualified versions of compatible types.
442
443 We assume that comp_target_types has already been done and returned
444 nonzero; if that isn't so, this may crash. */
445
446static tree
447common_pointer_type (tree t1, tree t2)
448{
449 tree attributes;
46df2823
JM
450 tree pointed_to_1, mv1;
451 tree pointed_to_2, mv2;
10bc1b1b
JM
452 tree target;
453
454 /* Save time if the two types are the same. */
455
456 if (t1 == t2) return t1;
457
458 /* If one type is nonsense, use the other. */
459 if (t1 == error_mark_node)
460 return t2;
461 if (t2 == error_mark_node)
462 return t1;
463
366de0ce
NS
464 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
465 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
466
467 /* Merge the attributes. */
468 attributes = targetm.merge_type_attributes (t1, t2);
469
470 /* Find the composite type of the target types, and combine the
46df2823
JM
471 qualifiers of the two types' targets. Do not lose qualifiers on
472 array element types by taking the TYPE_MAIN_VARIANT. */
473 mv1 = pointed_to_1 = TREE_TYPE (t1);
474 mv2 = pointed_to_2 = TREE_TYPE (t2);
475 if (TREE_CODE (mv1) != ARRAY_TYPE)
476 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
477 if (TREE_CODE (mv2) != ARRAY_TYPE)
478 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
479 target = composite_type (mv1, mv2);
10bc1b1b
JM
480 t1 = build_pointer_type (c_build_qualified_type
481 (target,
482 TYPE_QUALS (pointed_to_1) |
483 TYPE_QUALS (pointed_to_2)));
484 return build_type_attribute_variant (t1, attributes);
485}
486
487/* Return the common type for two arithmetic types under the usual
488 arithmetic conversions. The default conversions have already been
489 applied, and enumerated types converted to their compatible integer
490 types. The resulting type is unqualified and has no attributes.
491
492 This is the type for the result of most arithmetic operations
493 if the operands have the given two types. */
494
ccf7f880
JJ
495static tree
496c_common_type (tree t1, tree t2)
10bc1b1b
JM
497{
498 enum tree_code code1;
499 enum tree_code code2;
500
501 /* If one type is nonsense, use the other. */
502 if (t1 == error_mark_node)
503 return t2;
504 if (t2 == error_mark_node)
505 return t1;
506
507 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
508 t1 = TYPE_MAIN_VARIANT (t1);
509
510 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
511 t2 = TYPE_MAIN_VARIANT (t2);
512
513 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
514 t1 = build_type_attribute_variant (t1, NULL_TREE);
515
516 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
517 t2 = build_type_attribute_variant (t2, NULL_TREE);
518
519 /* Save time if the two types are the same. */
520
521 if (t1 == t2) return t1;
522
523 code1 = TREE_CODE (t1);
524 code2 = TREE_CODE (t2);
525
366de0ce
NS
526 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
527 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
528 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
529 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
10bc1b1b
JM
530
531 /* If one type is a vector type, return that type. (How the usual
532 arithmetic conversions apply to the vector types extension is not
533 precisely specified.) */
534 if (code1 == VECTOR_TYPE)
535 return t1;
536
537 if (code2 == VECTOR_TYPE)
538 return t2;
539
540 /* If one type is complex, form the common type of the non-complex
541 components, then make that complex. Use T1 or T2 if it is the
542 required type. */
543 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
544 {
545 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
546 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 547 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
548
549 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
550 return t1;
551 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
552 return t2;
553 else
554 return build_complex_type (subtype);
555 }
556
557 /* If only one is real, use it as the result. */
558
559 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
560 return t1;
561
562 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
563 return t2;
564
565 /* Both real or both integers; use the one with greater precision. */
566
567 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
568 return t1;
569 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
570 return t2;
571
572 /* Same precision. Prefer long longs to longs to ints when the
573 same precision, following the C99 rules on integer type rank
574 (which are equivalent to the C90 rules for C90 types). */
575
576 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
577 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
578 return long_long_unsigned_type_node;
579
580 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
581 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
582 {
583 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
584 return long_long_unsigned_type_node;
585 else
586 return long_long_integer_type_node;
587 }
588
589 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
590 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
591 return long_unsigned_type_node;
592
593 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
594 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
595 {
596 /* But preserve unsignedness from the other type,
597 since long cannot hold all the values of an unsigned int. */
598 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
599 return long_unsigned_type_node;
600 else
601 return long_integer_type_node;
602 }
603
604 /* Likewise, prefer long double to double even if same size. */
605 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
606 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
607 return long_double_type_node;
608
609 /* Otherwise prefer the unsigned one. */
610
611 if (TYPE_UNSIGNED (t1))
612 return t1;
613 else
614 return t2;
615}
400fbf9f 616\f
ccf7f880
JJ
617/* Wrapper around c_common_type that is used by c-common.c. ENUMERAL_TYPEs
618 are allowed here and are converted to their compatible integer types. */
619tree
620common_type (tree t1, tree t2)
621{
622 if (TREE_CODE (t1) == ENUMERAL_TYPE)
623 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
624 if (TREE_CODE (t2) == ENUMERAL_TYPE)
625 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
626 return c_common_type (t1, t2);
627}
628\f
400fbf9f
JW
629/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
630 or various other operations. Return 2 if they are compatible
631 but a warning may be needed if you use them together. */
632
633int
132da1a5 634comptypes (tree type1, tree type2)
400fbf9f 635{
b3694847
SS
636 tree t1 = type1;
637 tree t2 = type2;
4b027d16 638 int attrval, val;
400fbf9f
JW
639
640 /* Suppress errors caused by previously reported errors. */
641
8d47dfc5
RH
642 if (t1 == t2 || !t1 || !t2
643 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
644 return 1;
645
21318741
RK
646 /* If either type is the internal version of sizetype, return the
647 language version. */
648 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
eb1a2c88
DN
649 && TYPE_ORIG_SIZE_TYPE (t1))
650 t1 = TYPE_ORIG_SIZE_TYPE (t1);
21318741
RK
651
652 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
eb1a2c88
DN
653 && TYPE_ORIG_SIZE_TYPE (t2))
654 t2 = TYPE_ORIG_SIZE_TYPE (t2);
655
21318741 656
bca63328
JM
657 /* Enumerated types are compatible with integer types, but this is
658 not transitive: two enumerated types in the same translation unit
659 are compatible with each other only if they are the same type. */
400fbf9f 660
bca63328 661 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
8df83eae 662 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
bca63328 663 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
8df83eae 664 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
400fbf9f
JW
665
666 if (t1 == t2)
667 return 1;
668
669 /* Different classes of types can't be compatible. */
670
3aeb3655
EC
671 if (TREE_CODE (t1) != TREE_CODE (t2))
672 return 0;
400fbf9f 673
118a3a8b 674 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 675
3932261a 676 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
677 return 0;
678
08632da2
RS
679 /* Allow for two different type nodes which have essentially the same
680 definition. Note that we already checked for equality of the type
38e01259 681 qualifiers (just above). */
400fbf9f 682
46df2823
JM
683 if (TREE_CODE (t1) != ARRAY_TYPE
684 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
685 return 1;
686
4b027d16 687 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
3f75a254 688 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
4b027d16
RK
689 return 0;
690
691 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
692 val = 0;
693
400fbf9f
JW
694 switch (TREE_CODE (t1))
695 {
696 case POINTER_TYPE:
264fa2db
ZL
697 /* We must give ObjC the first crack at comparing pointers, since
698 protocol qualifiers may be involved. */
699 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
700 break;
106f5de5
UW
701 /* Do not remove mode or aliasing information. */
702 if (TYPE_MODE (t1) != TYPE_MODE (t2)
703 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
704 break;
4b027d16 705 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
132da1a5 706 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
4b027d16 707 break;
400fbf9f
JW
708
709 case FUNCTION_TYPE:
132da1a5 710 val = function_types_compatible_p (t1, t2);
4b027d16 711 break;
400fbf9f
JW
712
713 case ARRAY_TYPE:
714 {
400fbf9f
JW
715 tree d1 = TYPE_DOMAIN (t1);
716 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
717 bool d1_variable, d2_variable;
718 bool d1_zero, d2_zero;
4b027d16 719 val = 1;
400fbf9f
JW
720
721 /* Target types must match incl. qualifiers. */
722 if (TREE_TYPE (t1) != TREE_TYPE (t2)
132da1a5 723 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
400fbf9f
JW
724 return 0;
725
726 /* Sizes must match unless one is missing or variable. */
3f85558f 727 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 728 break;
400fbf9f 729
3f75a254
JM
730 d1_zero = !TYPE_MAX_VALUE (d1);
731 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 732
3f75a254 733 d1_variable = (!d1_zero
3f85558f
RH
734 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
735 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 736 d2_variable = (!d2_zero
3f85558f
RH
737 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
738 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
739
740 if (d1_variable || d2_variable)
741 break;
742 if (d1_zero && d2_zero)
743 break;
744 if (d1_zero || d2_zero
3f75a254
JM
745 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
746 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
747 val = 0;
748
4b027d16 749 break;
400fbf9f
JW
750 }
751
752 case RECORD_TYPE:
264fa2db
ZL
753 /* We are dealing with two distinct structs. In assorted Objective-C
754 corner cases, however, these can still be deemed equivalent. */
37fa72e9 755 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
4b027d16 756 val = 1;
d1bd0ded
GK
757
758 case ENUMERAL_TYPE:
759 case UNION_TYPE:
766beae1 760 if (val != 1 && !same_translation_unit_p (t1, t2))
132da1a5 761 val = tagged_types_tu_compatible_p (t1, t2);
4b027d16 762 break;
e9a25f70 763
62e1dfcf 764 case VECTOR_TYPE:
cc27e657 765 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
132da1a5 766 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
62e1dfcf
NC
767 break;
768
e9a25f70
JL
769 default:
770 break;
400fbf9f 771 }
4b027d16 772 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
773}
774
775/* Return 1 if TTL and TTR are pointers to types that are equivalent,
1074d9d4
NP
776 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
777 to 1 or 0 depending if the check of the pointer types is meant to
778 be reflexive or not (typically, assignments are not reflexive,
779 while comparisons are reflexive).
780*/
400fbf9f
JW
781
782static int
2f6e4e97 783comp_target_types (tree ttl, tree ttr, int reflexive)
400fbf9f 784{
392202b0 785 int val;
46df2823 786 tree mvl, mvr;
8b40563c 787
b50d021d 788 /* Give objc_comptypes a crack at letting these types through. */
1074d9d4 789 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
392202b0 790 return val;
8b40563c 791
46df2823
JM
792 /* Do not lose qualifiers on element types of array types that are
793 pointer targets by taking their TYPE_MAIN_VARIANT. */
794 mvl = TREE_TYPE (ttl);
795 mvr = TREE_TYPE (ttr);
796 if (TREE_CODE (mvl) != ARRAY_TYPE)
797 mvl = TYPE_MAIN_VARIANT (mvl);
798 if (TREE_CODE (mvr) != ARRAY_TYPE)
799 mvr = TYPE_MAIN_VARIANT (mvr);
800 val = comptypes (mvl, mvr);
8b40563c 801
400fbf9f
JW
802 if (val == 2 && pedantic)
803 pedwarn ("types are not quite compatible");
804 return val;
805}
806\f
807/* Subroutines of `comptypes'. */
808
f75fbaf7
ZW
809/* Determine whether two trees derive from the same translation unit.
810 If the CONTEXT chain ends in a null, that tree's context is still
811 being parsed, so if two trees have context chains ending in null,
766beae1 812 they're in the same translation unit. */
f75fbaf7 813int
766beae1
ZW
814same_translation_unit_p (tree t1, tree t2)
815{
816 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
817 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
818 {
6615c446
JO
819 case tcc_declaration:
820 t1 = DECL_CONTEXT (t1); break;
821 case tcc_type:
822 t1 = TYPE_CONTEXT (t1); break;
823 case tcc_exceptional:
824 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 825 default: gcc_unreachable ();
766beae1
ZW
826 }
827
828 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
829 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
830 {
6615c446
JO
831 case tcc_declaration:
832 t2 = DECL_CONTEXT (t2); break;
833 case tcc_type:
834 t2 = TYPE_CONTEXT (t2); break;
835 case tcc_exceptional:
836 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 837 default: gcc_unreachable ();
766beae1
ZW
838 }
839
840 return t1 == t2;
841}
842
d1bd0ded
GK
843/* The C standard says that two structures in different translation
844 units are compatible with each other only if the types of their
845 fields are compatible (among other things). So, consider two copies
846 of this structure: */
847
848struct tagged_tu_seen {
849 const struct tagged_tu_seen * next;
850 tree t1;
851 tree t2;
852};
853
854/* Can they be compatible with each other? We choose to break the
855 recursion by allowing those types to be compatible. */
856
857static const struct tagged_tu_seen * tagged_tu_seen_base;
858
859/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
860 compatible. If the two types are not the same (which has been
861 checked earlier), this can only happen when multiple translation
862 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
863 rules. */
864
865static int
132da1a5 866tagged_types_tu_compatible_p (tree t1, tree t2)
d1bd0ded
GK
867{
868 tree s1, s2;
869 bool needs_warning = false;
3aeb3655 870
d1bd0ded
GK
871 /* We have to verify that the tags of the types are the same. This
872 is harder than it looks because this may be a typedef, so we have
873 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
874 typedef...
875 In the case of compiler-created builtin structs the TYPE_DECL
876 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
877 while (TYPE_NAME (t1)
878 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
879 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
880 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
881
dea984dc
ILT
882 while (TYPE_NAME (t2)
883 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
884 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
885 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
886
887 /* C90 didn't have the requirement that the two tags be the same. */
888 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
889 return 0;
3aeb3655 890
d1bd0ded
GK
891 /* C90 didn't say what happened if one or both of the types were
892 incomplete; we choose to follow C99 rules here, which is that they
893 are compatible. */
894 if (TYPE_SIZE (t1) == NULL
895 || TYPE_SIZE (t2) == NULL)
896 return 1;
3aeb3655 897
d1bd0ded
GK
898 {
899 const struct tagged_tu_seen * tts_i;
900 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
901 if (tts_i->t1 == t1 && tts_i->t2 == t2)
902 return 1;
903 }
3aeb3655 904
d1bd0ded
GK
905 switch (TREE_CODE (t1))
906 {
907 case ENUMERAL_TYPE:
908 {
3aeb3655 909
71cc389b 910 /* Speed up the case where the type values are in the same order. */
dedbabed
AP
911 tree tv1 = TYPE_VALUES (t1);
912 tree tv2 = TYPE_VALUES (t2);
3aeb3655 913
dedbabed
AP
914 if (tv1 == tv2)
915 return 1;
3aeb3655 916
f38f747d 917 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
dedbabed 918 {
8cd6bdd1 919 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
dedbabed
AP
920 break;
921 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
922 return 0;
923 }
3aeb3655 924
dedbabed
AP
925 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
926 return 1;
927 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
928 return 0;
3aeb3655 929
d1bd0ded
GK
930 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
931 return 0;
3aeb3655 932
d1bd0ded
GK
933 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
934 {
935 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
936 if (s2 == NULL
937 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
938 return 0;
939 }
940 return 1;
941 }
942
943 case UNION_TYPE:
944 {
945 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
946 return 0;
947
948 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
949 {
950 bool ok = false;
951 struct tagged_tu_seen tts;
952
953 tts.next = tagged_tu_seen_base;
954 tts.t1 = t1;
955 tts.t2 = t2;
956 tagged_tu_seen_base = &tts;
3aeb3655 957
d1bd0ded 958 if (DECL_NAME (s1) != NULL)
398ce3dd 959 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
d1bd0ded
GK
960 if (DECL_NAME (s1) == DECL_NAME (s2))
961 {
962 int result;
132da1a5 963 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
d1bd0ded
GK
964 if (result == 0)
965 break;
966 if (result == 2)
967 needs_warning = true;
3aeb3655 968
d1bd0ded
GK
969 if (TREE_CODE (s1) == FIELD_DECL
970 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
971 DECL_FIELD_BIT_OFFSET (s2)) != 1)
972 break;
973
974 ok = true;
975 break;
976 }
977 tagged_tu_seen_base = tts.next;
3f75a254 978 if (!ok)
d1bd0ded
GK
979 return 0;
980 }
981 return needs_warning ? 2 : 1;
982 }
983
984 case RECORD_TYPE:
985 {
986 struct tagged_tu_seen tts;
3aeb3655 987
d1bd0ded
GK
988 tts.next = tagged_tu_seen_base;
989 tts.t1 = t1;
990 tts.t2 = t2;
991 tagged_tu_seen_base = &tts;
3aeb3655
EC
992
993 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded
GK
994 s1 && s2;
995 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
996 {
997 int result;
998 if (TREE_CODE (s1) != TREE_CODE (s2)
999 || DECL_NAME (s1) != DECL_NAME (s2))
1000 break;
132da1a5 1001 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
d1bd0ded
GK
1002 if (result == 0)
1003 break;
1004 if (result == 2)
1005 needs_warning = true;
3aeb3655 1006
d1bd0ded
GK
1007 if (TREE_CODE (s1) == FIELD_DECL
1008 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1009 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1010 break;
1011 }
1012 tagged_tu_seen_base = tts.next;
1013 if (s1 && s2)
1014 return 0;
1015 return needs_warning ? 2 : 1;
1016 }
1017
1018 default:
366de0ce 1019 gcc_unreachable ();
d1bd0ded
GK
1020 }
1021}
1022
400fbf9f
JW
1023/* Return 1 if two function types F1 and F2 are compatible.
1024 If either type specifies no argument types,
1025 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1026 Otherwise, if one type specifies only the number of arguments,
400fbf9f
JW
1027 the other must specify that number of self-promoting arg types.
1028 Otherwise, the argument types must match. */
1029
1030static int
132da1a5 1031function_types_compatible_p (tree f1, tree f2)
400fbf9f
JW
1032{
1033 tree args1, args2;
1034 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1035 int val = 1;
1036 int val1;
a6fdc086
GK
1037 tree ret1, ret2;
1038
1039 ret1 = TREE_TYPE (f1);
1040 ret2 = TREE_TYPE (f2);
1041
e508a019
JM
1042 /* 'volatile' qualifiers on a function's return type used to mean
1043 the function is noreturn. */
1044 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
bda67431 1045 pedwarn ("function return types not compatible due to %<volatile%>");
a6fdc086
GK
1046 if (TYPE_VOLATILE (ret1))
1047 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1048 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1049 if (TYPE_VOLATILE (ret2))
1050 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1051 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
132da1a5 1052 val = comptypes (ret1, ret2);
a6fdc086 1053 if (val == 0)
400fbf9f
JW
1054 return 0;
1055
1056 args1 = TYPE_ARG_TYPES (f1);
1057 args2 = TYPE_ARG_TYPES (f2);
1058
1059 /* An unspecified parmlist matches any specified parmlist
1060 whose argument types don't need default promotions. */
1061
1062 if (args1 == 0)
1063 {
1064 if (!self_promoting_args_p (args2))
1065 return 0;
1066 /* If one of these types comes from a non-prototype fn definition,
1067 compare that with the other type's arglist.
1068 If they don't match, ask for a warning (but no error). */
1069 if (TYPE_ACTUAL_ARG_TYPES (f1)
132da1a5 1070 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
400fbf9f
JW
1071 val = 2;
1072 return val;
1073 }
1074 if (args2 == 0)
1075 {
1076 if (!self_promoting_args_p (args1))
1077 return 0;
1078 if (TYPE_ACTUAL_ARG_TYPES (f2)
132da1a5 1079 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
400fbf9f
JW
1080 val = 2;
1081 return val;
1082 }
1083
1084 /* Both types have argument lists: compare them and propagate results. */
132da1a5 1085 val1 = type_lists_compatible_p (args1, args2);
400fbf9f
JW
1086 return val1 != 1 ? val1 : val;
1087}
1088
1089/* Check two lists of types for compatibility,
1090 returning 0 for incompatible, 1 for compatible,
1091 or 2 for compatible with warning. */
1092
1093static int
132da1a5 1094type_lists_compatible_p (tree args1, tree args2)
400fbf9f
JW
1095{
1096 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1097 int val = 1;
9d5f3e49 1098 int newval = 0;
400fbf9f
JW
1099
1100 while (1)
1101 {
46df2823 1102 tree a1, mv1, a2, mv2;
400fbf9f
JW
1103 if (args1 == 0 && args2 == 0)
1104 return val;
1105 /* If one list is shorter than the other,
1106 they fail to match. */
1107 if (args1 == 0 || args2 == 0)
1108 return 0;
46df2823
JM
1109 mv1 = a1 = TREE_VALUE (args1);
1110 mv2 = a2 = TREE_VALUE (args2);
1111 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1112 mv1 = TYPE_MAIN_VARIANT (mv1);
1113 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1114 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f
JW
1115 /* A null pointer instead of a type
1116 means there is supposed to be an argument
1117 but nothing is specified about what type it has.
1118 So match anything that self-promotes. */
46df2823 1119 if (a1 == 0)
400fbf9f 1120 {
46df2823 1121 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1122 return 0;
1123 }
46df2823 1124 else if (a2 == 0)
400fbf9f 1125 {
46df2823 1126 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1127 return 0;
1128 }
8f5b6d29 1129 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1130 else if (TREE_CODE (a1) == ERROR_MARK
1131 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1132 ;
46df2823 1133 else if (!(newval = comptypes (mv1, mv2)))
400fbf9f
JW
1134 {
1135 /* Allow wait (union {union wait *u; int *i} *)
1136 and wait (union wait *) to be compatible. */
46df2823
JM
1137 if (TREE_CODE (a1) == UNION_TYPE
1138 && (TYPE_NAME (a1) == 0
1139 || TYPE_TRANSPARENT_UNION (a1))
1140 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1141 && tree_int_cst_equal (TYPE_SIZE (a1),
1142 TYPE_SIZE (a2)))
400fbf9f
JW
1143 {
1144 tree memb;
46df2823 1145 for (memb = TYPE_FIELDS (a1);
400fbf9f 1146 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
1147 {
1148 tree mv3 = TREE_TYPE (memb);
1149 if (mv3 && mv3 != error_mark_node
1150 && TREE_CODE (mv3) != ARRAY_TYPE)
1151 mv3 = TYPE_MAIN_VARIANT (mv3);
1152 if (comptypes (mv3, mv2))
1153 break;
1154 }
400fbf9f
JW
1155 if (memb == 0)
1156 return 0;
1157 }
46df2823
JM
1158 else if (TREE_CODE (a2) == UNION_TYPE
1159 && (TYPE_NAME (a2) == 0
1160 || TYPE_TRANSPARENT_UNION (a2))
1161 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1162 && tree_int_cst_equal (TYPE_SIZE (a2),
1163 TYPE_SIZE (a1)))
400fbf9f
JW
1164 {
1165 tree memb;
46df2823 1166 for (memb = TYPE_FIELDS (a2);
400fbf9f 1167 memb; memb = TREE_CHAIN (memb))
58cb41e6
JJ
1168 {
1169 tree mv3 = TREE_TYPE (memb);
1170 if (mv3 && mv3 != error_mark_node
1171 && TREE_CODE (mv3) != ARRAY_TYPE)
1172 mv3 = TYPE_MAIN_VARIANT (mv3);
1173 if (comptypes (mv3, mv1))
1174 break;
1175 }
400fbf9f
JW
1176 if (memb == 0)
1177 return 0;
1178 }
1179 else
1180 return 0;
1181 }
1182
1183 /* comptypes said ok, but record if it said to warn. */
1184 if (newval > val)
1185 val = newval;
1186
1187 args1 = TREE_CHAIN (args1);
1188 args2 = TREE_CHAIN (args2);
1189 }
1190}
400fbf9f 1191\f
400fbf9f
JW
1192/* Compute the size to increment a pointer by. */
1193
4e2fb7de 1194static tree
2f6e4e97 1195c_size_in_bytes (tree type)
400fbf9f
JW
1196{
1197 enum tree_code code = TREE_CODE (type);
1198
fed3cef0
RK
1199 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1200 return size_one_node;
1201
d0f062fb 1202 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1203 {
1204 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1205 return size_one_node;
400fbf9f
JW
1206 }
1207
1208 /* Convert in case a char is more than one unit. */
fed3cef0
RK
1209 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1210 size_int (TYPE_PRECISION (char_type_node)
1211 / BITS_PER_UNIT));
400fbf9f 1212}
400fbf9f 1213\f
400fbf9f
JW
1214/* Return either DECL or its known constant value (if it has one). */
1215
56cb9733 1216tree
2f6e4e97 1217decl_constant_value (tree decl)
400fbf9f 1218{
a7c1916a 1219 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1220 in a place where a variable is invalid. Note that DECL_INITIAL
1221 isn't valid for a PARM_DECL. */
a7c1916a 1222 current_function_decl != 0
4f976745 1223 && TREE_CODE (decl) != PARM_DECL
3f75a254 1224 && !TREE_THIS_VOLATILE (decl)
83bab8db 1225 && TREE_READONLY (decl)
400fbf9f
JW
1226 && DECL_INITIAL (decl) != 0
1227 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1228 /* This is invalid if initial value is not constant.
1229 If it has either a function call, a memory reference,
1230 or a variable, then re-evaluating it could give different results. */
1231 && TREE_CONSTANT (DECL_INITIAL (decl))
1232 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1233 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1234 return DECL_INITIAL (decl);
1235 return decl;
1236}
1237
2f74f7e9
JM
1238/* Return either DECL or its known constant value (if it has one), but
1239 return DECL if pedantic or DECL has mode BLKmode. This is for
1240 bug-compatibility with the old behavior of decl_constant_value
1241 (before GCC 3.0); every use of this function is a bug and it should
1242 be removed before GCC 3.1. It is not appropriate to use pedantic
1243 in a way that affects optimization, and BLKmode is probably not the
1244 right test for avoiding misoptimizations either. */
1245
1246static tree
2f6e4e97 1247decl_constant_value_for_broken_optimization (tree decl)
2f74f7e9
JM
1248{
1249 if (pedantic || DECL_MODE (decl) == BLKmode)
1250 return decl;
1251 else
1252 return decl_constant_value (decl);
1253}
1254
207bf485
JM
1255
1256/* Perform the default conversion of arrays and functions to pointers.
1257 Return the result of converting EXP. For any other expression, just
1258 return EXP. */
1259
1260static tree
2f6e4e97 1261default_function_array_conversion (tree exp)
207bf485
JM
1262{
1263 tree orig_exp;
1264 tree type = TREE_TYPE (exp);
1265 enum tree_code code = TREE_CODE (type);
1266 int not_lvalue = 0;
1267
1268 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
2f6e4e97 1269 an lvalue.
207bf485
JM
1270
1271 Do not use STRIP_NOPS here! It will remove conversions from pointer
1272 to integer and cause infinite recursion. */
1273 orig_exp = exp;
1274 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1275 || (TREE_CODE (exp) == NOP_EXPR
1276 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1277 {
1278 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1279 not_lvalue = 1;
1280 exp = TREE_OPERAND (exp, 0);
1281 }
1282
487a92fe
JM
1283 if (TREE_NO_WARNING (orig_exp))
1284 TREE_NO_WARNING (exp) = 1;
207bf485
JM
1285
1286 if (code == FUNCTION_TYPE)
1287 {
1288 return build_unary_op (ADDR_EXPR, exp, 0);
1289 }
1290 if (code == ARRAY_TYPE)
1291 {
1292 tree adr;
1293 tree restype = TREE_TYPE (type);
1294 tree ptrtype;
1295 int constp = 0;
1296 int volatilep = 0;
1297 int lvalue_array_p;
1298
6615c446 1299 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
207bf485
JM
1300 {
1301 constp = TREE_READONLY (exp);
1302 volatilep = TREE_THIS_VOLATILE (exp);
1303 }
1304
1305 if (TYPE_QUALS (type) || constp || volatilep)
2f6e4e97 1306 restype
207bf485 1307 = c_build_qualified_type (restype,
2f6e4e97 1308 TYPE_QUALS (type)
207bf485
JM
1309 | (constp * TYPE_QUAL_CONST)
1310 | (volatilep * TYPE_QUAL_VOLATILE));
1311
1312 if (TREE_CODE (exp) == INDIRECT_REF)
019c8e80 1313 return convert (build_pointer_type (restype),
207bf485
JM
1314 TREE_OPERAND (exp, 0));
1315
1316 if (TREE_CODE (exp) == COMPOUND_EXPR)
1317 {
1318 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
53fb4de3
RS
1319 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1320 TREE_OPERAND (exp, 0), op1);
207bf485
JM
1321 }
1322
1323 lvalue_array_p = !not_lvalue && lvalue_p (exp);
db3acfa5 1324 if (!flag_isoc99 && !lvalue_array_p)
207bf485
JM
1325 {
1326 /* Before C99, non-lvalue arrays do not decay to pointers.
1327 Normally, using such an array would be invalid; but it can
1328 be used correctly inside sizeof or as a statement expression.
1329 Thus, do not give an error here; an error will result later. */
1330 return exp;
1331 }
1332
1333 ptrtype = build_pointer_type (restype);
1334
7c672dfc
AP
1335 if (TREE_CODE (exp) == VAR_DECL)
1336 {
1337 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1338 ADDR_EXPR because it's the best way of representing what
1339 happens in C when we take the address of an array and place
1340 it in a pointer to the element type. */
1341 adr = build1 (ADDR_EXPR, ptrtype, exp);
1342 if (!c_mark_addressable (exp))
1343 return error_mark_node;
1344 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1345 return adr;
1346 }
207bf485
JM
1347 /* This way is better for a COMPONENT_REF since it can
1348 simplify the offset for a component. */
7c672dfc 1349 adr = build_unary_op (ADDR_EXPR, exp, 1);
207bf485
JM
1350 return convert (ptrtype, adr);
1351 }
1352 return exp;
1353}
1354
522ddfa2
JM
1355
1356/* EXP is an expression of integer type. Apply the integer promotions
1357 to it and return the promoted value. */
400fbf9f
JW
1358
1359tree
522ddfa2 1360perform_integral_promotions (tree exp)
400fbf9f 1361{
b3694847
SS
1362 tree type = TREE_TYPE (exp);
1363 enum tree_code code = TREE_CODE (type);
400fbf9f 1364
522ddfa2 1365 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 1366
400fbf9f
JW
1367 /* Normally convert enums to int,
1368 but convert wide enums to something wider. */
1369 if (code == ENUMERAL_TYPE)
1370 {
b0c48229
NB
1371 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1372 TYPE_PRECISION (integer_type_node)),
1373 ((TYPE_PRECISION (type)
1374 >= TYPE_PRECISION (integer_type_node))
8df83eae 1375 && TYPE_UNSIGNED (type)));
05bccae2 1376
400fbf9f
JW
1377 return convert (type, exp);
1378 }
1379
522ddfa2
JM
1380 /* ??? This should no longer be needed now bit-fields have their
1381 proper types. */
9753f113 1382 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 1383 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 1384 /* If it's thinner than an int, promote it like a
d72040f5 1385 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
1386 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1387 TYPE_PRECISION (integer_type_node)))
f458d1d5 1388 return convert (integer_type_node, exp);
9753f113 1389
d72040f5 1390 if (c_promoting_integer_type_p (type))
400fbf9f 1391 {
f458d1d5 1392 /* Preserve unsignedness if not really getting any wider. */
8df83eae 1393 if (TYPE_UNSIGNED (type)
f458d1d5 1394 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 1395 return convert (unsigned_type_node, exp);
05bccae2 1396
400fbf9f
JW
1397 return convert (integer_type_node, exp);
1398 }
05bccae2 1399
522ddfa2
JM
1400 return exp;
1401}
1402
1403
1404/* Perform default promotions for C data used in expressions.
1405 Arrays and functions are converted to pointers;
1406 enumeral types or short or char, to int.
1407 In addition, manifest constants symbols are replaced by their values. */
1408
1409tree
1410default_conversion (tree exp)
1411{
1412 tree orig_exp;
1413 tree type = TREE_TYPE (exp);
1414 enum tree_code code = TREE_CODE (type);
1415
1416 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1417 return default_function_array_conversion (exp);
1418
1419 /* Constants can be used directly unless they're not loadable. */
1420 if (TREE_CODE (exp) == CONST_DECL)
1421 exp = DECL_INITIAL (exp);
1422
1423 /* Replace a nonvolatile const static variable with its value unless
1424 it is an array, in which case we must be sure that taking the
1425 address of the array produces consistent results. */
1426 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1427 {
1428 exp = decl_constant_value_for_broken_optimization (exp);
1429 type = TREE_TYPE (exp);
1430 }
1431
1432 /* Strip no-op conversions. */
1433 orig_exp = exp;
1434 STRIP_TYPE_NOPS (exp);
1435
1436 if (TREE_NO_WARNING (orig_exp))
1437 TREE_NO_WARNING (exp) = 1;
1438
1439 if (INTEGRAL_TYPE_P (type))
1440 return perform_integral_promotions (exp);
1441
400fbf9f
JW
1442 if (code == VOID_TYPE)
1443 {
1444 error ("void value not ignored as it ought to be");
1445 return error_mark_node;
1446 }
400fbf9f
JW
1447 return exp;
1448}
1449\f
e9b2c823
NB
1450/* Look up COMPONENT in a structure or union DECL.
1451
1452 If the component name is not found, returns NULL_TREE. Otherwise,
1453 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1454 stepping down the chain to the component, which is in the last
1455 TREE_VALUE of the list. Normally the list is of length one, but if
1456 the component is embedded within (nested) anonymous structures or
1457 unions, the list steps down the chain to the component. */
2f6e4e97 1458
2f2d13da 1459static tree
2f6e4e97 1460lookup_field (tree decl, tree component)
2f2d13da 1461{
e9b2c823 1462 tree type = TREE_TYPE (decl);
2f2d13da
DE
1463 tree field;
1464
1465 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1466 to the field elements. Use a binary search on this array to quickly
1467 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1468 will always be set for structures which have many elements. */
1469
22a0b85f 1470 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
1471 {
1472 int bot, top, half;
d07605f5 1473 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
1474
1475 field = TYPE_FIELDS (type);
1476 bot = 0;
d07605f5 1477 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
1478 while (top - bot > 1)
1479 {
2f2d13da
DE
1480 half = (top - bot + 1) >> 1;
1481 field = field_array[bot+half];
1482
1483 if (DECL_NAME (field) == NULL_TREE)
1484 {
1485 /* Step through all anon unions in linear fashion. */
1486 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1487 {
2f2d13da 1488 field = field_array[bot++];
a68b98cf
RK
1489 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1490 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 1491 {
e9b2c823
NB
1492 tree anon = lookup_field (field, component);
1493
1494 if (anon)
1495 return tree_cons (NULL_TREE, field, anon);
2f6e4e97 1496 }
2f2d13da
DE
1497 }
1498
1499 /* Entire record is only anon unions. */
1500 if (bot > top)
1501 return NULL_TREE;
1502
1503 /* Restart the binary search, with new lower bound. */
1504 continue;
1505 }
1506
e8b87aac 1507 if (DECL_NAME (field) == component)
2f2d13da 1508 break;
e8b87aac 1509 if (DECL_NAME (field) < component)
2f2d13da
DE
1510 bot += half;
1511 else
1512 top = bot + half;
1513 }
1514
1515 if (DECL_NAME (field_array[bot]) == component)
1516 field = field_array[bot];
1517 else if (DECL_NAME (field) != component)
e9b2c823 1518 return NULL_TREE;
2f2d13da
DE
1519 }
1520 else
1521 {
1522 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1523 {
e9b2c823
NB
1524 if (DECL_NAME (field) == NULL_TREE
1525 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1526 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 1527 {
e9b2c823 1528 tree anon = lookup_field (field, component);
a68b98cf 1529
e9b2c823
NB
1530 if (anon)
1531 return tree_cons (NULL_TREE, field, anon);
2f2d13da
DE
1532 }
1533
1534 if (DECL_NAME (field) == component)
1535 break;
1536 }
e9b2c823
NB
1537
1538 if (field == NULL_TREE)
1539 return NULL_TREE;
2f2d13da
DE
1540 }
1541
e9b2c823 1542 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
1543}
1544
400fbf9f
JW
1545/* Make an expression to refer to the COMPONENT field of
1546 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1547
1548tree
2f6e4e97 1549build_component_ref (tree datum, tree component)
400fbf9f 1550{
b3694847
SS
1551 tree type = TREE_TYPE (datum);
1552 enum tree_code code = TREE_CODE (type);
1553 tree field = NULL;
1554 tree ref;
400fbf9f 1555
7a3ea201
RH
1556 if (!objc_is_public (datum, component))
1557 return error_mark_node;
1558
400fbf9f
JW
1559 /* See if there is a field or component with name COMPONENT. */
1560
1561 if (code == RECORD_TYPE || code == UNION_TYPE)
1562 {
d0f062fb 1563 if (!COMPLETE_TYPE_P (type))
400fbf9f 1564 {
7a228918 1565 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
1566 return error_mark_node;
1567 }
1568
e9b2c823 1569 field = lookup_field (datum, component);
400fbf9f
JW
1570
1571 if (!field)
1572 {
c51a1ba9 1573 error ("%qT has no member named %qE", type, component);
400fbf9f
JW
1574 return error_mark_node;
1575 }
400fbf9f 1576
e9b2c823
NB
1577 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1578 This might be better solved in future the way the C++ front
1579 end does it - by giving the anonymous entities each a
1580 separate name and type, and then have build_component_ref
1581 recursively call itself. We can't do that here. */
46ea50cb 1582 do
19d76e60 1583 {
e9b2c823
NB
1584 tree subdatum = TREE_VALUE (field);
1585
1586 if (TREE_TYPE (subdatum) == error_mark_node)
1587 return error_mark_node;
1588
53fb4de3
RS
1589 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1590 NULL_TREE);
e9b2c823 1591 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
19d76e60 1592 TREE_READONLY (ref) = 1;
e9b2c823 1593 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
19d76e60 1594 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
1595
1596 if (TREE_DEPRECATED (subdatum))
1597 warn_deprecated_use (subdatum);
1598
19d76e60 1599 datum = ref;
46ea50cb
RS
1600
1601 field = TREE_CHAIN (field);
19d76e60 1602 }
46ea50cb 1603 while (field);
19d76e60 1604
400fbf9f
JW
1605 return ref;
1606 }
1607 else if (code != ERROR_MARK)
c51a1ba9
JM
1608 error ("request for member %qE in something not a structure or union",
1609 component);
400fbf9f
JW
1610
1611 return error_mark_node;
1612}
1613\f
1614/* Given an expression PTR for a pointer, return an expression
1615 for the value pointed to.
1616 ERRORSTRING is the name of the operator to appear in error messages. */
1617
1618tree
2f6e4e97 1619build_indirect_ref (tree ptr, const char *errorstring)
400fbf9f 1620{
b3694847
SS
1621 tree pointer = default_conversion (ptr);
1622 tree type = TREE_TYPE (pointer);
400fbf9f
JW
1623
1624 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b
RS
1625 {
1626 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
1627 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1628 == TREE_TYPE (type)))
1629 return TREE_OPERAND (pointer, 0);
1630 else
1631 {
1632 tree t = TREE_TYPE (type);
46df2823
JM
1633 tree mvt = t;
1634 tree ref;
1635
1636 if (TREE_CODE (mvt) != ARRAY_TYPE)
1637 mvt = TYPE_MAIN_VARIANT (mvt);
1638 ref = build1 (INDIRECT_REF, mvt, pointer);
400fbf9f 1639
baae9b65 1640 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b
RS
1641 {
1642 error ("dereferencing pointer to incomplete type");
1643 return error_mark_node;
1644 }
baae9b65 1645 if (VOID_TYPE_P (t) && skip_evaluation == 0)
bda67431 1646 warning ("dereferencing %<void *%> pointer");
870cc33b
RS
1647
1648 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1649 so that we get the proper error message if the result is used
1650 to assign to. Also, &* is supposed to be a no-op.
1651 And ANSI C seems to specify that the type of the result
1652 should be the const type. */
1653 /* A de-reference of a pointer to const is not a const. It is valid
1654 to change it via some other pointer. */
1655 TREE_READONLY (ref) = TYPE_READONLY (t);
1656 TREE_SIDE_EFFECTS (ref)
271bd540 1657 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 1658 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
870cc33b
RS
1659 return ref;
1660 }
1661 }
400fbf9f 1662 else if (TREE_CODE (pointer) != ERROR_MARK)
bda67431 1663 error ("invalid type argument of %qs", errorstring);
400fbf9f
JW
1664 return error_mark_node;
1665}
1666
1667/* This handles expressions of the form "a[i]", which denotes
1668 an array reference.
1669
1670 This is logically equivalent in C to *(a+i), but we may do it differently.
1671 If A is a variable or a member, we generate a primitive ARRAY_REF.
1672 This avoids forcing the array out of registers, and can work on
1673 arrays that are not lvalues (for example, members of structures returned
1674 by functions). */
1675
1676tree
2f6e4e97 1677build_array_ref (tree array, tree index)
400fbf9f 1678{
a4ab7973 1679 bool swapped = false;
400fbf9f
JW
1680 if (TREE_TYPE (array) == error_mark_node
1681 || TREE_TYPE (index) == error_mark_node)
1682 return error_mark_node;
1683
a4ab7973
JM
1684 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1685 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
400fbf9f 1686 {
a4ab7973
JM
1687 tree temp;
1688 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1689 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 1690 {
a4ab7973 1691 error ("subscripted value is neither array nor pointer");
fdeefd49
RS
1692 return error_mark_node;
1693 }
a4ab7973
JM
1694 temp = array;
1695 array = index;
1696 index = temp;
1697 swapped = true;
1698 }
1699
1700 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1701 {
1702 error ("array subscript is not an integer");
1703 return error_mark_node;
1704 }
1705
1706 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1707 {
1708 error ("subscripted value is pointer to function");
1709 return error_mark_node;
1710 }
1711
1712 /* Subscripting with type char is likely to lose on a machine where
1713 chars are signed. So warn on any machine, but optionally. Don't
1714 warn for unsigned char since that type is safe. Don't warn for
1715 signed char because anyone who uses that must have done so
1716 deliberately. ??? Existing practice has also been to warn only
1717 when the char index is syntactically the index, not for
1718 char[array]. */
1719 if (warn_char_subscripts && !swapped
1720 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1721 warning ("array subscript has type %<char%>");
1722
1723 /* Apply default promotions *after* noticing character types. */
1724 index = default_conversion (index);
1725
1726 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1727
1728 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1729 {
1730 tree rval, type;
fdeefd49 1731
400fbf9f
JW
1732 /* An array that is indexed by a non-constant
1733 cannot be stored in a register; we must be able to do
1734 address arithmetic on its address.
1735 Likewise an array of elements of variable size. */
1736 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 1737 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
1738 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1739 {
dffd7eb6 1740 if (!c_mark_addressable (array))
400fbf9f
JW
1741 return error_mark_node;
1742 }
e6d52559
JW
1743 /* An array that is indexed by a constant value which is not within
1744 the array bounds cannot be stored in a register either; because we
1745 would get a crash in store_bit_field/extract_bit_field when trying
1746 to access a non-existent part of the register. */
1747 if (TREE_CODE (index) == INTEGER_CST
eb34af89 1748 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 1749 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 1750 {
dffd7eb6 1751 if (!c_mark_addressable (array))
e6d52559
JW
1752 return error_mark_node;
1753 }
400fbf9f 1754
400fbf9f
JW
1755 if (pedantic)
1756 {
1757 tree foo = array;
1758 while (TREE_CODE (foo) == COMPONENT_REF)
1759 foo = TREE_OPERAND (foo, 0);
5baeaac0 1760 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
bda67431 1761 pedwarn ("ISO C forbids subscripting %<register%> array");
3f75a254 1762 else if (!flag_isoc99 && !lvalue_p (foo))
56508306 1763 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
1764 }
1765
46df2823
JM
1766 type = TREE_TYPE (TREE_TYPE (array));
1767 if (TREE_CODE (type) != ARRAY_TYPE)
1768 type = TYPE_MAIN_VARIANT (type);
53fb4de3 1769 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f
JW
1770 /* Array ref is const/volatile if the array elements are
1771 or if the array is. */
1772 TREE_READONLY (rval)
1773 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1774 | TREE_READONLY (array));
1775 TREE_SIDE_EFFECTS (rval)
1776 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1777 | TREE_SIDE_EFFECTS (array));
1778 TREE_THIS_VOLATILE (rval)
1779 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1780 /* This was added by rms on 16 Nov 91.
2f6e4e97 1781 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
1782 in an inline function.
1783 Hope it doesn't break something else. */
1784 | TREE_THIS_VOLATILE (array));
1785 return require_complete_type (fold (rval));
1786 }
a4ab7973
JM
1787 else
1788 {
1789 tree ar = default_conversion (array);
400fbf9f 1790
a4ab7973
JM
1791 if (ar == error_mark_node)
1792 return ar;
400fbf9f 1793
a4ab7973
JM
1794 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1795 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 1796
a4ab7973
JM
1797 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1798 "array indexing");
1799 }
400fbf9f
JW
1800}
1801\f
7e585d16 1802/* Build an external reference to identifier ID. FUN indicates
766beb40
JM
1803 whether this will be used for a function call. LOC is the source
1804 location of the identifier. */
7e585d16 1805tree
766beb40 1806build_external_ref (tree id, int fun, location_t loc)
7e585d16
ZW
1807{
1808 tree ref;
1809 tree decl = lookup_name (id);
16b34ad6
ZL
1810
1811 /* In Objective-C, an instance variable (ivar) may be preferred to
1812 whatever lookup_name() found. */
1813 decl = objc_lookup_ivar (decl, id);
7e585d16 1814
339a28b9 1815 if (decl && decl != error_mark_node)
16b34ad6 1816 ref = decl;
339a28b9
ZW
1817 else if (fun)
1818 /* Implicit function declaration. */
1819 ref = implicitly_declare (id);
1820 else if (decl == error_mark_node)
1821 /* Don't complain about something that's already been
1822 complained about. */
1823 return error_mark_node;
1824 else
1825 {
766beb40 1826 undeclared_variable (id, loc);
339a28b9
ZW
1827 return error_mark_node;
1828 }
7e585d16
ZW
1829
1830 if (TREE_TYPE (ref) == error_mark_node)
1831 return error_mark_node;
1832
339a28b9
ZW
1833 if (TREE_DEPRECATED (ref))
1834 warn_deprecated_use (ref);
1835
25587e40
AO
1836 if (!skip_evaluation)
1837 assemble_external (ref);
7e585d16
ZW
1838 TREE_USED (ref) = 1;
1839
bc4b653b
JM
1840 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1841 {
1842 if (!in_sizeof && !in_typeof)
1843 C_DECL_USED (ref) = 1;
1844 else if (DECL_INITIAL (ref) == 0
1845 && DECL_EXTERNAL (ref)
1846 && !TREE_PUBLIC (ref))
1847 record_maybe_used_decl (ref);
1848 }
1849
7e585d16
ZW
1850 if (TREE_CODE (ref) == CONST_DECL)
1851 {
1852 ref = DECL_INITIAL (ref);
1853 TREE_CONSTANT (ref) = 1;
6de9cd9a 1854 TREE_INVARIANT (ref) = 1;
7e585d16 1855 }
6a29edea 1856 else if (current_function_decl != 0
4b1e44be 1857 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
1858 && (TREE_CODE (ref) == VAR_DECL
1859 || TREE_CODE (ref) == PARM_DECL
1860 || TREE_CODE (ref) == FUNCTION_DECL))
1861 {
1862 tree context = decl_function_context (ref);
2f6e4e97 1863
6a29edea
EB
1864 if (context != 0 && context != current_function_decl)
1865 DECL_NONLOCAL (ref) = 1;
1866 }
7e585d16
ZW
1867
1868 return ref;
1869}
1870
bc4b653b
JM
1871/* Record details of decls possibly used inside sizeof or typeof. */
1872struct maybe_used_decl
1873{
1874 /* The decl. */
1875 tree decl;
1876 /* The level seen at (in_sizeof + in_typeof). */
1877 int level;
1878 /* The next one at this level or above, or NULL. */
1879 struct maybe_used_decl *next;
1880};
1881
1882static struct maybe_used_decl *maybe_used_decls;
1883
1884/* Record that DECL, an undefined static function reference seen
1885 inside sizeof or typeof, might be used if the operand of sizeof is
1886 a VLA type or the operand of typeof is a variably modified
1887 type. */
1888
4e2fb7de 1889static void
bc4b653b
JM
1890record_maybe_used_decl (tree decl)
1891{
1892 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1893 t->decl = decl;
1894 t->level = in_sizeof + in_typeof;
1895 t->next = maybe_used_decls;
1896 maybe_used_decls = t;
1897}
1898
1899/* Pop the stack of decls possibly used inside sizeof or typeof. If
1900 USED is false, just discard them. If it is true, mark them used
1901 (if no longer inside sizeof or typeof) or move them to the next
1902 level up (if still inside sizeof or typeof). */
1903
1904void
1905pop_maybe_used (bool used)
1906{
1907 struct maybe_used_decl *p = maybe_used_decls;
1908 int cur_level = in_sizeof + in_typeof;
1909 while (p && p->level > cur_level)
1910 {
1911 if (used)
1912 {
1913 if (cur_level == 0)
1914 C_DECL_USED (p->decl) = 1;
1915 else
1916 p->level = cur_level;
1917 }
1918 p = p->next;
1919 }
1920 if (!used || cur_level == 0)
1921 maybe_used_decls = p;
1922}
1923
1924/* Return the result of sizeof applied to EXPR. */
1925
1926struct c_expr
1927c_expr_sizeof_expr (struct c_expr expr)
1928{
1929 struct c_expr ret;
ad97f4be
JM
1930 if (expr.value == error_mark_node)
1931 {
1932 ret.value = error_mark_node;
1933 ret.original_code = ERROR_MARK;
1934 pop_maybe_used (false);
1935 }
1936 else
1937 {
1938 ret.value = c_sizeof (TREE_TYPE (expr.value));
1939 ret.original_code = ERROR_MARK;
1940 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1941 }
bc4b653b
JM
1942 return ret;
1943}
1944
1945/* Return the result of sizeof applied to T, a structure for the type
1946 name passed to sizeof (rather than the type itself). */
1947
1948struct c_expr
f8893e47 1949c_expr_sizeof_type (struct c_type_name *t)
bc4b653b
JM
1950{
1951 tree type;
1952 struct c_expr ret;
1953 type = groktypename (t);
1954 ret.value = c_sizeof (type);
1955 ret.original_code = ERROR_MARK;
1956 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1957 return ret;
1958}
1959
400fbf9f
JW
1960/* Build a function call to function FUNCTION with parameters PARAMS.
1961 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1962 TREE_VALUE of each node is a parameter-expression.
1963 FUNCTION's data type may be a function type or a pointer-to-function. */
1964
1965tree
2f6e4e97 1966build_function_call (tree function, tree params)
400fbf9f 1967{
b3694847
SS
1968 tree fntype, fundecl = 0;
1969 tree coerced_params;
4977bab6 1970 tree name = NULL_TREE, result;
c96f4f73 1971 tree tem;
400fbf9f 1972
fc76e425 1973 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 1974 STRIP_TYPE_NOPS (function);
400fbf9f
JW
1975
1976 /* Convert anything with function type to a pointer-to-function. */
1977 if (TREE_CODE (function) == FUNCTION_DECL)
1978 {
1979 name = DECL_NAME (function);
19d76e60 1980
400fbf9f
JW
1981 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1982 (because calling an inline function does not mean the function
1983 needs to be separately compiled). */
1984 fntype = build_type_variant (TREE_TYPE (function),
1985 TREE_READONLY (function),
1986 TREE_THIS_VOLATILE (function));
9b7267b8 1987 fundecl = function;
400fbf9f
JW
1988 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1989 }
1990 else
1991 function = default_conversion (function);
1992
1993 fntype = TREE_TYPE (function);
1994
1995 if (TREE_CODE (fntype) == ERROR_MARK)
1996 return error_mark_node;
1997
1998 if (!(TREE_CODE (fntype) == POINTER_TYPE
1999 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2000 {
f0c721ad 2001 error ("called object %qE is not a function", function);
400fbf9f
JW
2002 return error_mark_node;
2003 }
2004
5ce89b2e
JM
2005 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2006 current_function_returns_abnormally = 1;
2007
400fbf9f
JW
2008 /* fntype now gets the type of function pointed to. */
2009 fntype = TREE_TYPE (fntype);
2010
c96f4f73
EB
2011 /* Check that the function is called through a compatible prototype.
2012 If it is not, replace the call by a trap, wrapped up in a compound
2013 expression if necessary. This has the nice side-effect to prevent
2014 the tree-inliner from generating invalid assignment trees which may
2015 blow up in the RTL expander later.
2016
2017 ??? This doesn't work for Objective-C because objc_comptypes
2018 refuses to compare function prototypes, yet the compiler appears
2019 to build calls that are flagged as invalid by C's comptypes. */
3f75a254 2020 if (!c_dialect_objc ()
c96f4f73
EB
2021 && TREE_CODE (function) == NOP_EXPR
2022 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2023 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2024 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2025 {
2026 tree return_type = TREE_TYPE (fntype);
2027 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2028 NULL_TREE);
2029
2030 /* This situation leads to run-time undefined behavior. We can't,
2031 therefore, simply error unless we can prove that all possible
2032 executions of the program must execute the code. */
2033 warning ("function called through a non-compatible type");
2034
bba745c1
EB
2035 /* We can, however, treat "undefined" any way we please.
2036 Call abort to encourage the user to fix the program. */
2037 inform ("if this code is reached, the program will abort");
2038
c96f4f73
EB
2039 if (VOID_TYPE_P (return_type))
2040 return trap;
2041 else
2042 {
2043 tree rhs;
2044
2045 if (AGGREGATE_TYPE_P (return_type))
2046 rhs = build_compound_literal (return_type,
2047 build_constructor (return_type,
2048 NULL_TREE));
2049 else
2050 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2051
53fb4de3 2052 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
c96f4f73
EB
2053 }
2054 }
2055
400fbf9f
JW
2056 /* Convert the parameters to the types declared in the
2057 function prototype, or apply default promotions. */
2058
2059 coerced_params
03dafa61 2060 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
400fbf9f 2061
3789b316
JM
2062 if (coerced_params == error_mark_node)
2063 return error_mark_node;
2064
b34c7881 2065 /* Check that the arguments to the function are valid. */
400fbf9f 2066
b34c7881 2067 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
400fbf9f 2068
53fb4de3
RS
2069 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2070 function, coerced_params, NULL_TREE);
1eb8759b 2071 TREE_SIDE_EFFECTS (result) = 1;
bf730f15
RS
2072
2073 if (require_constant_value)
2074 {
2075 result = fold_initializer (result);
2076
2077 if (TREE_CONSTANT (result)
2078 && (name == NULL_TREE
2079 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2080 pedwarn_init ("initializer element is not constant");
2081 }
2082 else
2083 result = fold (result);
b0b3afb2 2084
71653180 2085 if (VOID_TYPE_P (TREE_TYPE (result)))
1eb8759b
RH
2086 return result;
2087 return require_complete_type (result);
400fbf9f
JW
2088}
2089\f
2090/* Convert the argument expressions in the list VALUES
2091 to the types in the list TYPELIST. The result is a list of converted
3789b316
JM
2092 argument expressions, unless there are too few arguments in which
2093 case it is error_mark_node.
400fbf9f
JW
2094
2095 If TYPELIST is exhausted, or when an element has NULL as its type,
2096 perform the default conversions.
2097
2098 PARMLIST is the chain of parm decls for the function being called.
2099 It may be 0, if that info is not available.
2100 It is used only for generating error messages.
2101
03dafa61
JM
2102 FUNCTION is a tree for the called function. It is used only for
2103 error messages, where it is formatted with %qE.
400fbf9f
JW
2104
2105 This is also where warnings about wrong number of args are generated.
2106
2107 Both VALUES and the returned value are chains of TREE_LIST nodes
2108 with the elements of the list in the TREE_VALUE slots of those nodes. */
2109
2110static tree
03dafa61 2111convert_arguments (tree typelist, tree values, tree function, tree fundecl)
400fbf9f 2112{
b3694847
SS
2113 tree typetail, valtail;
2114 tree result = NULL;
400fbf9f 2115 int parmnum;
03dafa61 2116 tree selector;
03dafa61 2117
2ac2f164
JM
2118 /* Change pointer to function to the function itself for
2119 diagnostics. */
03dafa61
JM
2120 if (TREE_CODE (function) == ADDR_EXPR
2121 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 2122 function = TREE_OPERAND (function, 0);
03dafa61
JM
2123
2124 /* Handle an ObjC selector specially for diagnostics. */
2125 selector = objc_message_selector ();
400fbf9f
JW
2126
2127 /* Scan the given expressions and types, producing individual
2128 converted arguments and pushing them on RESULT in reverse order. */
2129
2130 for (valtail = values, typetail = typelist, parmnum = 0;
2131 valtail;
2132 valtail = TREE_CHAIN (valtail), parmnum++)
2133 {
b3694847
SS
2134 tree type = typetail ? TREE_VALUE (typetail) : 0;
2135 tree val = TREE_VALUE (valtail);
03dafa61
JM
2136 tree rname = function;
2137 int argnum = parmnum + 1;
4d3e6fae 2138 const char *invalid_func_diag;
400fbf9f
JW
2139
2140 if (type == void_type_node)
2141 {
03dafa61 2142 error ("too many arguments to function %qE", function);
400fbf9f
JW
2143 break;
2144 }
2145
03dafa61
JM
2146 if (selector && argnum > 2)
2147 {
2148 rname = selector;
2149 argnum -= 2;
2150 }
2151
ed248cf7 2152 STRIP_TYPE_NOPS (val);
400fbf9f 2153
207bf485 2154 val = default_function_array_conversion (val);
400fbf9f
JW
2155
2156 val = require_complete_type (val);
2157
2158 if (type != 0)
2159 {
2160 /* Formal parm type is specified by a function prototype. */
2161 tree parmval;
2162
20913689 2163 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
2164 {
2165 error ("type of formal parameter %d is incomplete", parmnum + 1);
2166 parmval = val;
2167 }
2168 else
2169 {
d45cf215
RS
2170 /* Optionally warn about conversions that
2171 differ from the default conversions. */
03829ad2 2172 if (warn_conversion || warn_traditional)
400fbf9f 2173 {
e3a64162 2174 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 2175
aae43c5f 2176 if (INTEGRAL_TYPE_P (type)
400fbf9f 2177 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
03dafa61
JM
2178 warning ("passing argument %d of %qE as integer "
2179 "rather than floating due to prototype",
2180 argnum, rname);
03829ad2
KG
2181 if (INTEGRAL_TYPE_P (type)
2182 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
03dafa61
JM
2183 warning ("passing argument %d of %qE as integer "
2184 "rather than complex due to prototype",
2185 argnum, rname);
aae43c5f
RK
2186 else if (TREE_CODE (type) == COMPLEX_TYPE
2187 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
03dafa61
JM
2188 warning ("passing argument %d of %qE as complex "
2189 "rather than floating due to prototype",
2190 argnum, rname);
400fbf9f 2191 else if (TREE_CODE (type) == REAL_TYPE
aae43c5f 2192 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
03dafa61
JM
2193 warning ("passing argument %d of %qE as floating "
2194 "rather than integer due to prototype",
2195 argnum, rname);
03829ad2
KG
2196 else if (TREE_CODE (type) == COMPLEX_TYPE
2197 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
03dafa61
JM
2198 warning ("passing argument %d of %qE as complex "
2199 "rather than integer due to prototype",
2200 argnum, rname);
aae43c5f
RK
2201 else if (TREE_CODE (type) == REAL_TYPE
2202 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
03dafa61
JM
2203 warning ("passing argument %d of %qE as floating "
2204 "rather than complex due to prototype",
2205 argnum, rname);
aae43c5f
RK
2206 /* ??? At some point, messages should be written about
2207 conversions between complex types, but that's too messy
2208 to do now. */
d45cf215
RS
2209 else if (TREE_CODE (type) == REAL_TYPE
2210 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2211 {
2212 /* Warn if any argument is passed as `float',
047de90b 2213 since without a prototype it would be `double'. */
d45cf215 2214 if (formal_prec == TYPE_PRECISION (float_type_node))
03dafa61
JM
2215 warning ("passing argument %d of %qE as %<float%> "
2216 "rather than %<double%> due to prototype",
2217 argnum, rname);
d45cf215 2218 }
3ed56f8a
KG
2219 /* Detect integer changing in width or signedness.
2220 These warnings are only activated with
2221 -Wconversion, not with -Wtraditional. */
2222 else if (warn_conversion && INTEGRAL_TYPE_P (type)
aae43c5f 2223 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
400fbf9f 2224 {
d45cf215
RS
2225 tree would_have_been = default_conversion (val);
2226 tree type1 = TREE_TYPE (would_have_been);
2227
754a4d82 2228 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a
NB
2229 && (TYPE_MAIN_VARIANT (type)
2230 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
754a4d82
RS
2231 /* No warning if function asks for enum
2232 and the actual arg is that enum type. */
2233 ;
2234 else if (formal_prec != TYPE_PRECISION (type1))
03dafa61
JM
2235 warning ("passing argument %d of %qE with different "
2236 "width due to prototype", argnum, rname);
8df83eae 2237 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 2238 ;
800cd3b9
RS
2239 /* Don't complain if the formal parameter type
2240 is an enum, because we can't tell now whether
2241 the value was an enum--even the same enum. */
2242 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2243 ;
400fbf9f
JW
2244 else if (TREE_CODE (val) == INTEGER_CST
2245 && int_fits_type_p (val, type))
2246 /* Change in signedness doesn't matter
2247 if a constant value is unaffected. */
2248 ;
ce9895ae
RS
2249 /* If the value is extended from a narrower
2250 unsigned type, it doesn't matter whether we
2251 pass it as signed or unsigned; the value
2252 certainly is the same either way. */
2253 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
8df83eae 2254 && TYPE_UNSIGNED (TREE_TYPE (val)))
ce9895ae 2255 ;
8df83eae 2256 else if (TYPE_UNSIGNED (type))
03dafa61
JM
2257 warning ("passing argument %d of %qE as unsigned "
2258 "due to prototype", argnum, rname);
3ed56f8a 2259 else
03dafa61
JM
2260 warning ("passing argument %d of %qE as signed "
2261 "due to prototype", argnum, rname);
400fbf9f
JW
2262 }
2263 }
2264
2ac2f164
JM
2265 parmval = convert_for_assignment (type, val, ic_argpass,
2266 fundecl, function,
2267 parmnum + 1);
2f6e4e97 2268
61f71b34 2269 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 2270 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
2271 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2272 parmval = default_conversion (parmval);
400fbf9f 2273 }
8d9bfdc5 2274 result = tree_cons (NULL_TREE, parmval, result);
400fbf9f
JW
2275 }
2276 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2277 && (TYPE_PRECISION (TREE_TYPE (val))
2278 < TYPE_PRECISION (double_type_node)))
2279 /* Convert `float' to `double'. */
2280 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
4d3e6fae
FJ
2281 else if ((invalid_func_diag =
2282 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2283 {
2284 error (invalid_func_diag);
2285 return error_mark_node;
2286 }
400fbf9f
JW
2287 else
2288 /* Convert `short' and `char' to full-size `int'. */
2289 result = tree_cons (NULL_TREE, default_conversion (val), result);
2290
2291 if (typetail)
2292 typetail = TREE_CHAIN (typetail);
2293 }
2294
2295 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316
JM
2296 {
2297 error ("too few arguments to function %qE", function);
2298 return error_mark_node;
2299 }
400fbf9f
JW
2300
2301 return nreverse (result);
2302}
2303\f
2304/* This is the entry point used by the parser
2305 for binary operators in the input.
2306 In addition to constructing the expression,
2307 we check for operands that were written with other binary operators
2308 in a way that is likely to confuse the user. */
edc7c4ec 2309
487a92fe
JM
2310struct c_expr
2311parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2312 struct c_expr arg2)
400fbf9f 2313{
487a92fe 2314 struct c_expr result;
400fbf9f 2315
487a92fe
JM
2316 enum tree_code code1 = arg1.original_code;
2317 enum tree_code code2 = arg2.original_code;
400fbf9f 2318
487a92fe
JM
2319 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2320 result.original_code = code;
58bf601b 2321
487a92fe
JM
2322 if (TREE_CODE (result.value) == ERROR_MARK)
2323 return result;
400fbf9f
JW
2324
2325 /* Check for cases such as x+y<<z which users are likely
487a92fe 2326 to misinterpret. */
400fbf9f
JW
2327 if (warn_parentheses)
2328 {
2329 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2330 {
2331 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2332 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2333 warning ("suggest parentheses around + or - inside shift");
2334 }
2335
2336 if (code == TRUTH_ORIF_EXPR)
2337 {
2338 if (code1 == TRUTH_ANDIF_EXPR
2339 || code2 == TRUTH_ANDIF_EXPR)
2340 warning ("suggest parentheses around && within ||");
2341 }
2342
2343 if (code == BIT_IOR_EXPR)
2344 {
2345 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2346 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2347 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2348 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2349 warning ("suggest parentheses around arithmetic in operand of |");
7e9d002a 2350 /* Check cases like x|y==z */
6615c446
JO
2351 if (TREE_CODE_CLASS (code1) == tcc_comparison
2352 || TREE_CODE_CLASS (code2) == tcc_comparison)
7e9d002a 2353 warning ("suggest parentheses around comparison in operand of |");
400fbf9f
JW
2354 }
2355
2356 if (code == BIT_XOR_EXPR)
2357 {
2358 if (code1 == BIT_AND_EXPR
2359 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2360 || code2 == BIT_AND_EXPR
2361 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2362 warning ("suggest parentheses around arithmetic in operand of ^");
7e9d002a 2363 /* Check cases like x^y==z */
6615c446
JO
2364 if (TREE_CODE_CLASS (code1) == tcc_comparison
2365 || TREE_CODE_CLASS (code2) == tcc_comparison)
7e9d002a 2366 warning ("suggest parentheses around comparison in operand of ^");
400fbf9f
JW
2367 }
2368
2369 if (code == BIT_AND_EXPR)
2370 {
2371 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2372 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2373 warning ("suggest parentheses around + or - in operand of &");
7e9d002a 2374 /* Check cases like x&y==z */
6615c446
JO
2375 if (TREE_CODE_CLASS (code1) == tcc_comparison
2376 || TREE_CODE_CLASS (code2) == tcc_comparison)
7e9d002a 2377 warning ("suggest parentheses around comparison in operand of &");
400fbf9f 2378 }
3e3970a2 2379 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
6615c446
JO
2380 if (TREE_CODE_CLASS (code) == tcc_comparison
2381 && (TREE_CODE_CLASS (code1) == tcc_comparison
2382 || TREE_CODE_CLASS (code2) == tcc_comparison))
3e3970a2 2383 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
400fbf9f 2384
3e3970a2 2385 }
001af587 2386
487a92fe
JM
2387 unsigned_conversion_warning (result.value, arg1.value);
2388 unsigned_conversion_warning (result.value, arg2.value);
2389 overflow_warning (result.value);
400fbf9f
JW
2390
2391 return result;
2392}
3e4093b6 2393\f
3e4093b6
RS
2394/* Return a tree for the difference of pointers OP0 and OP1.
2395 The resulting tree has type int. */
293c9fdd 2396
3e4093b6
RS
2397static tree
2398pointer_diff (tree op0, tree op1)
2399{
3e4093b6 2400 tree restype = ptrdiff_type_node;
400fbf9f 2401
3e4093b6
RS
2402 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2403 tree con0, con1, lit0, lit1;
2404 tree orig_op1 = op1;
400fbf9f 2405
3e4093b6
RS
2406 if (pedantic || warn_pointer_arith)
2407 {
2408 if (TREE_CODE (target_type) == VOID_TYPE)
bda67431 2409 pedwarn ("pointer of type %<void *%> used in subtraction");
3e4093b6
RS
2410 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2411 pedwarn ("pointer to a function used in subtraction");
2412 }
400fbf9f 2413
3e4093b6
RS
2414 /* If the conversion to ptrdiff_type does anything like widening or
2415 converting a partial to an integral mode, we get a convert_expression
2416 that is in the way to do any simplifications.
2417 (fold-const.c doesn't know that the extra bits won't be needed.
2418 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2419 different mode in place.)
2420 So first try to find a common term here 'by hand'; we want to cover
2421 at least the cases that occur in legal static initializers. */
2422 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2423 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
400fbf9f 2424
3e4093b6
RS
2425 if (TREE_CODE (con0) == PLUS_EXPR)
2426 {
2427 lit0 = TREE_OPERAND (con0, 1);
2428 con0 = TREE_OPERAND (con0, 0);
2429 }
2430 else
2431 lit0 = integer_zero_node;
400fbf9f 2432
3e4093b6 2433 if (TREE_CODE (con1) == PLUS_EXPR)
400fbf9f 2434 {
3e4093b6
RS
2435 lit1 = TREE_OPERAND (con1, 1);
2436 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
2437 }
2438 else
3e4093b6
RS
2439 lit1 = integer_zero_node;
2440
2441 if (operand_equal_p (con0, con1, 0))
400fbf9f 2442 {
3e4093b6
RS
2443 op0 = lit0;
2444 op1 = lit1;
400fbf9f
JW
2445 }
2446
400fbf9f 2447
3e4093b6
RS
2448 /* First do the subtraction as integers;
2449 then drop through to build the divide operator.
2450 Do not do default conversions on the minus operator
2451 in case restype is a short type. */
400fbf9f 2452
3e4093b6
RS
2453 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2454 convert (restype, op1), 0);
2455 /* This generates an error if op1 is pointer to incomplete type. */
2456 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2457 error ("arithmetic on pointer to an incomplete type");
400fbf9f 2458
3e4093b6
RS
2459 /* This generates an error if op0 is pointer to incomplete type. */
2460 op1 = c_size_in_bytes (target_type);
400fbf9f 2461
3e4093b6 2462 /* Divide by the size, in easiest possible way. */
53fb4de3 2463 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
3e4093b6
RS
2464}
2465\f
2466/* Construct and perhaps optimize a tree representation
2467 for a unary operation. CODE, a tree_code, specifies the operation
2468 and XARG is the operand.
2469 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2470 the default promotions (such as from short to int).
2471 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2472 allows non-lvalues; this is only used to handle conversion of non-lvalue
2473 arrays to pointers in C99. */
400fbf9f 2474
3e4093b6
RS
2475tree
2476build_unary_op (enum tree_code code, tree xarg, int flag)
2477{
2478 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2479 tree arg = xarg;
2480 tree argtype = 0;
2481 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2482 tree val;
2483 int noconvert = flag;
400fbf9f 2484
3e4093b6
RS
2485 if (typecode == ERROR_MARK)
2486 return error_mark_node;
2487 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2488 typecode = INTEGER_TYPE;
6c36d76b 2489
3e4093b6
RS
2490 switch (code)
2491 {
2492 case CONVERT_EXPR:
2493 /* This is used for unary plus, because a CONVERT_EXPR
2494 is enough to prevent anybody from looking inside for
2495 associativity, but won't generate any code. */
2496 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
8a2cee38
JB
2497 || typecode == COMPLEX_TYPE
2498 || typecode == VECTOR_TYPE))
400fbf9f 2499 {
3e4093b6
RS
2500 error ("wrong type argument to unary plus");
2501 return error_mark_node;
400fbf9f 2502 }
3e4093b6
RS
2503 else if (!noconvert)
2504 arg = default_conversion (arg);
2505 arg = non_lvalue (arg);
400fbf9f
JW
2506 break;
2507
3e4093b6
RS
2508 case NEGATE_EXPR:
2509 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2510 || typecode == COMPLEX_TYPE
2511 || typecode == VECTOR_TYPE))
2512 {
2513 error ("wrong type argument to unary minus");
2514 return error_mark_node;
2515 }
2516 else if (!noconvert)
2517 arg = default_conversion (arg);
400fbf9f
JW
2518 break;
2519
3e4093b6
RS
2520 case BIT_NOT_EXPR:
2521 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
03d5b1f5 2522 {
3e4093b6
RS
2523 if (!noconvert)
2524 arg = default_conversion (arg);
03d5b1f5 2525 }
3e4093b6 2526 else if (typecode == COMPLEX_TYPE)
400fbf9f 2527 {
3e4093b6
RS
2528 code = CONJ_EXPR;
2529 if (pedantic)
bda67431 2530 pedwarn ("ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
2531 if (!noconvert)
2532 arg = default_conversion (arg);
2533 }
2534 else
2535 {
2536 error ("wrong type argument to bit-complement");
2537 return error_mark_node;
400fbf9f
JW
2538 }
2539 break;
2540
3e4093b6 2541 case ABS_EXPR:
11017cc7 2542 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 2543 {
3e4093b6
RS
2544 error ("wrong type argument to abs");
2545 return error_mark_node;
400fbf9f 2546 }
3e4093b6
RS
2547 else if (!noconvert)
2548 arg = default_conversion (arg);
400fbf9f
JW
2549 break;
2550
3e4093b6
RS
2551 case CONJ_EXPR:
2552 /* Conjugating a real value is a no-op, but allow it anyway. */
2553 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2554 || typecode == COMPLEX_TYPE))
400fbf9f 2555 {
3e4093b6
RS
2556 error ("wrong type argument to conjugation");
2557 return error_mark_node;
400fbf9f 2558 }
3e4093b6
RS
2559 else if (!noconvert)
2560 arg = default_conversion (arg);
400fbf9f
JW
2561 break;
2562
3e4093b6
RS
2563 case TRUTH_NOT_EXPR:
2564 if (typecode != INTEGER_TYPE
2565 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2566 && typecode != COMPLEX_TYPE
2567 /* These will convert to a pointer. */
2568 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
400fbf9f 2569 {
3e4093b6
RS
2570 error ("wrong type argument to unary exclamation mark");
2571 return error_mark_node;
400fbf9f 2572 }
ae2bcd98 2573 arg = lang_hooks.truthvalue_conversion (arg);
3e4093b6
RS
2574 return invert_truthvalue (arg);
2575
2576 case NOP_EXPR:
400fbf9f
JW
2577 break;
2578
3e4093b6
RS
2579 case REALPART_EXPR:
2580 if (TREE_CODE (arg) == COMPLEX_CST)
2581 return TREE_REALPART (arg);
2582 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2583 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2584 else
2585 return arg;
605a99f6 2586
3e4093b6
RS
2587 case IMAGPART_EXPR:
2588 if (TREE_CODE (arg) == COMPLEX_CST)
2589 return TREE_IMAGPART (arg);
2590 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2591 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2592 else
2593 return convert (TREE_TYPE (arg), integer_zero_node);
2594
2595 case PREINCREMENT_EXPR:
2596 case POSTINCREMENT_EXPR:
2597 case PREDECREMENT_EXPR:
2598 case POSTDECREMENT_EXPR:
3e4093b6
RS
2599
2600 /* Increment or decrement the real part of the value,
2601 and don't change the imaginary part. */
2602 if (typecode == COMPLEX_TYPE)
400fbf9f 2603 {
3e4093b6
RS
2604 tree real, imag;
2605
2606 if (pedantic)
bda67431
JM
2607 pedwarn ("ISO C does not support %<++%> and %<--%>"
2608 " on complex types");
3e4093b6
RS
2609
2610 arg = stabilize_reference (arg);
2611 real = build_unary_op (REALPART_EXPR, arg, 1);
2612 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
53fb4de3
RS
2613 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2614 build_unary_op (code, real, 1), imag);
400fbf9f 2615 }
3e4093b6
RS
2616
2617 /* Report invalid types. */
2618
2619 if (typecode != POINTER_TYPE
2620 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
400fbf9f 2621 {
3e4093b6
RS
2622 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2623 error ("wrong type argument to increment");
2624 else
2625 error ("wrong type argument to decrement");
2626
2627 return error_mark_node;
400fbf9f 2628 }
400fbf9f 2629
3e4093b6
RS
2630 {
2631 tree inc;
2632 tree result_type = TREE_TYPE (arg);
400fbf9f 2633
3e4093b6
RS
2634 arg = get_unwidened (arg, 0);
2635 argtype = TREE_TYPE (arg);
2636
2637 /* Compute the increment. */
2638
2639 if (typecode == POINTER_TYPE)
2640 {
2641 /* If pointer target is an undefined struct,
2642 we just cannot know how to do the arithmetic. */
2643 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2644 {
2645 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2646 error ("increment of pointer to unknown structure");
2647 else
2648 error ("decrement of pointer to unknown structure");
2649 }
2650 else if ((pedantic || warn_pointer_arith)
2651 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2652 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2653 {
2654 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2655 pedwarn ("wrong type argument to increment");
2656 else
2657 pedwarn ("wrong type argument to decrement");
2658 }
2659
2660 inc = c_size_in_bytes (TREE_TYPE (result_type));
2661 }
2662 else
2663 inc = integer_one_node;
2664
2665 inc = convert (argtype, inc);
2666
3e4093b6
RS
2667 /* Complain about anything else that is not a true lvalue. */
2668 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2669 || code == POSTINCREMENT_EXPR)
9bf24266
JM
2670 ? lv_increment
2671 : lv_decrement)))
3e4093b6
RS
2672 return error_mark_node;
2673
2674 /* Report a read-only lvalue. */
2675 if (TREE_READONLY (arg))
c5b6f18e
MM
2676 readonly_error (arg,
2677 ((code == PREINCREMENT_EXPR
2678 || code == POSTINCREMENT_EXPR)
9bf24266 2679 ? lv_increment : lv_decrement));
3e4093b6
RS
2680
2681 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2682 val = boolean_increment (code, arg);
2683 else
53fb4de3 2684 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6
RS
2685 TREE_SIDE_EFFECTS (val) = 1;
2686 val = convert (result_type, val);
2687 if (TREE_CODE (val) != code)
6de9cd9a 2688 TREE_NO_WARNING (val) = 1;
3e4093b6
RS
2689 return val;
2690 }
2691
2692 case ADDR_EXPR:
2693 /* Note that this operation never does default_conversion. */
2694
2695 /* Let &* cancel out to simplify resulting code. */
2696 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 2697 {
3e4093b6
RS
2698 /* Don't let this be an lvalue. */
2699 if (lvalue_p (TREE_OPERAND (arg, 0)))
2700 return non_lvalue (TREE_OPERAND (arg, 0));
2701 return TREE_OPERAND (arg, 0);
400fbf9f 2702 }
1eb8759b 2703
7c672dfc 2704 /* For &x[y], return x+y */
3e4093b6 2705 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 2706 {
3e4093b6
RS
2707 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2708 return error_mark_node;
7c672dfc
AP
2709 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2710 TREE_OPERAND (arg, 1), 1);
1eb8759b 2711 }
1eb8759b 2712
3e4093b6
RS
2713 /* Anything not already handled and not a true memory reference
2714 or a non-lvalue array is an error. */
2715 else if (typecode != FUNCTION_TYPE && !flag
9bf24266 2716 && !lvalue_or_else (arg, lv_addressof))
3e4093b6 2717 return error_mark_node;
b6a10c9f 2718
3e4093b6
RS
2719 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2720 argtype = TREE_TYPE (arg);
400fbf9f 2721
3e4093b6
RS
2722 /* If the lvalue is const or volatile, merge that into the type
2723 to which the address will point. Note that you can't get a
2724 restricted pointer by taking the address of something, so we
2725 only have to deal with `const' and `volatile' here. */
6615c446 2726 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3e4093b6
RS
2727 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2728 argtype = c_build_type_variant (argtype,
2729 TREE_READONLY (arg),
2730 TREE_THIS_VOLATILE (arg));
400fbf9f 2731
3e4093b6
RS
2732 if (!c_mark_addressable (arg))
2733 return error_mark_node;
400fbf9f 2734
abb54d14
JM
2735 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2736 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 2737
5cc200fc 2738 argtype = build_pointer_type (argtype);
5e55f99d
RH
2739
2740 /* ??? Cope with user tricks that amount to offsetof. Delete this
2741 when we have proper support for integer constant expressions. */
2742 val = get_base_address (arg);
2743 if (val && TREE_CODE (val) == INDIRECT_REF
2744 && integer_zerop (TREE_OPERAND (val, 0)))
2745 return fold_convert (argtype, fold_offsetof (arg));
2746
5cc200fc 2747 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 2748
5cc200fc
RH
2749 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2750 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
44de5aeb 2751
5cc200fc 2752 return val;
400fbf9f 2753
3e4093b6
RS
2754 default:
2755 break;
2756 }
400fbf9f 2757
3e4093b6
RS
2758 if (argtype == 0)
2759 argtype = TREE_TYPE (arg);
bf730f15
RS
2760 val = build1 (code, argtype, arg);
2761 return require_constant_value ? fold_initializer (val) : fold (val);
3e4093b6 2762}
400fbf9f 2763
3e4093b6
RS
2764/* Return nonzero if REF is an lvalue valid for this language.
2765 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 2766 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 2767
37dc0d8d 2768static int
3e4093b6
RS
2769lvalue_p (tree ref)
2770{
2771 enum tree_code code = TREE_CODE (ref);
400fbf9f 2772
3e4093b6
RS
2773 switch (code)
2774 {
2775 case REALPART_EXPR:
2776 case IMAGPART_EXPR:
2777 case COMPONENT_REF:
2778 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 2779
3e4093b6
RS
2780 case COMPOUND_LITERAL_EXPR:
2781 case STRING_CST:
2782 return 1;
400fbf9f 2783
3e4093b6
RS
2784 case INDIRECT_REF:
2785 case ARRAY_REF:
2786 case VAR_DECL:
2787 case PARM_DECL:
2788 case RESULT_DECL:
2789 case ERROR_MARK:
2790 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2791 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 2792
3e4093b6 2793 case BIND_EXPR:
3e4093b6 2794 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 2795
3e4093b6
RS
2796 default:
2797 return 0;
2798 }
2799}
400fbf9f 2800\f
9bf24266 2801/* Give an error for storing in something that is 'const'. */
54c93c30 2802
9bf24266
JM
2803static void
2804readonly_error (tree arg, enum lvalue_use use)
54c93c30 2805{
9bf24266
JM
2806 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2807 /* Using this macro rather than (for example) arrays of messages
2808 ensures that all the format strings are checked at compile
2809 time. */
2810#define READONLY_MSG(A, I, D) (use == lv_assign \
2811 ? (A) \
2812 : (use == lv_increment ? (I) : (D)))
3e4093b6 2813 if (TREE_CODE (arg) == COMPONENT_REF)
54c93c30 2814 {
3e4093b6 2815 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9bf24266 2816 readonly_error (TREE_OPERAND (arg, 0), use);
3e4093b6 2817 else
c51a1ba9
JM
2818 error (READONLY_MSG (N_("assignment of read-only member %qD"),
2819 N_("increment of read-only member %qD"),
2820 N_("decrement of read-only member %qD")),
2821 TREE_OPERAND (arg, 1));
54c93c30 2822 }
3e4093b6 2823 else if (TREE_CODE (arg) == VAR_DECL)
c51a1ba9
JM
2824 error (READONLY_MSG (N_("assignment of read-only variable %qD"),
2825 N_("increment of read-only variable %qD"),
2826 N_("decrement of read-only variable %qD")),
2827 arg);
3e4093b6 2828 else
9bf24266
JM
2829 error (READONLY_MSG (N_("assignment of read-only location"),
2830 N_("increment of read-only location"),
2831 N_("decrement of read-only location")));
54c93c30 2832}
37dc0d8d
JM
2833
2834
2835/* Return nonzero if REF is an lvalue valid for this language;
2836 otherwise, print an error message and return zero. USE says
2837 how the lvalue is being used and so selects the error message. */
2838
2839static int
2840lvalue_or_else (tree ref, enum lvalue_use use)
2841{
2842 int win = lvalue_p (ref);
2843
2844 if (!win)
2845 lvalue_error (use);
2846
2847 return win;
2848}
3e4093b6
RS
2849\f
2850/* Mark EXP saying that we need to be able to take the
2851 address of it; it should not be allocated in a register.
2852 Returns true if successful. */
54c93c30 2853
3e4093b6
RS
2854bool
2855c_mark_addressable (tree exp)
400fbf9f 2856{
3e4093b6 2857 tree x = exp;
95602da1 2858
3e4093b6
RS
2859 while (1)
2860 switch (TREE_CODE (x))
2861 {
2862 case COMPONENT_REF:
2863 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2864 {
0039fa55
AN
2865 error
2866 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
2867 return false;
2868 }
95602da1 2869
3e4093b6 2870 /* ... fall through ... */
95602da1 2871
3e4093b6
RS
2872 case ADDR_EXPR:
2873 case ARRAY_REF:
2874 case REALPART_EXPR:
2875 case IMAGPART_EXPR:
2876 x = TREE_OPERAND (x, 0);
2877 break;
95602da1 2878
3e4093b6
RS
2879 case COMPOUND_LITERAL_EXPR:
2880 case CONSTRUCTOR:
2881 TREE_ADDRESSABLE (x) = 1;
2882 return true;
95602da1 2883
3e4093b6
RS
2884 case VAR_DECL:
2885 case CONST_DECL:
2886 case PARM_DECL:
2887 case RESULT_DECL:
5baeaac0 2888 if (C_DECL_REGISTER (x)
3e4093b6
RS
2889 && DECL_NONLOCAL (x))
2890 {
e697b20f 2891 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 2892 {
0039fa55
AN
2893 error
2894 ("global register variable %qD used in nested function", x);
3e4093b6
RS
2895 return false;
2896 }
0039fa55 2897 pedwarn ("register variable %qD used in nested function", x);
3e4093b6 2898 }
5baeaac0 2899 else if (C_DECL_REGISTER (x))
3e4093b6 2900 {
e697b20f 2901 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
2902 error ("address of global register variable %qD requested", x);
2903 else
2904 error ("address of register variable %qD requested", x);
2905 return false;
3e4093b6 2906 }
400fbf9f 2907
3e4093b6
RS
2908 /* drops in */
2909 case FUNCTION_DECL:
2910 TREE_ADDRESSABLE (x) = 1;
2911 /* drops out */
2912 default:
2913 return true;
2914 }
2915}
2916\f
2917/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
400fbf9f
JW
2918
2919tree
3e4093b6 2920build_conditional_expr (tree ifexp, tree op1, tree op2)
400fbf9f 2921{
3e4093b6
RS
2922 tree type1;
2923 tree type2;
2924 enum tree_code code1;
2925 enum tree_code code2;
2926 tree result_type = NULL;
2927 tree orig_op1 = op1, orig_op2 = op2;
400fbf9f 2928
ae2bcd98 2929 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
3e4093b6
RS
2930
2931 /* Promote both alternatives. */
2932
2933 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2934 op1 = default_conversion (op1);
2935 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2936 op2 = default_conversion (op2);
2937
2938 if (TREE_CODE (ifexp) == ERROR_MARK
2939 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2940 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 2941 return error_mark_node;
400fbf9f 2942
3e4093b6
RS
2943 type1 = TREE_TYPE (op1);
2944 code1 = TREE_CODE (type1);
2945 type2 = TREE_TYPE (op2);
2946 code2 = TREE_CODE (type2);
2947
b1adf557
JM
2948 /* C90 does not permit non-lvalue arrays in conditional expressions.
2949 In C99 they will be pointers by now. */
2950 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2951 {
2952 error ("non-lvalue array in conditional expression");
2953 return error_mark_node;
2954 }
2955
3e4093b6
RS
2956 /* Quickly detect the usual case where op1 and op2 have the same type
2957 after promotion. */
2958 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 2959 {
3e4093b6
RS
2960 if (type1 == type2)
2961 result_type = type1;
2962 else
2963 result_type = TYPE_MAIN_VARIANT (type1);
2964 }
2965 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2966 || code1 == COMPLEX_TYPE)
2967 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2968 || code2 == COMPLEX_TYPE))
2969 {
ccf7f880 2970 result_type = c_common_type (type1, type2);
400fbf9f 2971
3e4093b6
RS
2972 /* If -Wsign-compare, warn here if type1 and type2 have
2973 different signedness. We'll promote the signed to unsigned
2974 and later code won't know it used to be different.
2975 Do this check on the original types, so that explicit casts
2976 will be considered, but default promotions won't. */
2977 if (warn_sign_compare && !skip_evaluation)
ab87f8c8 2978 {
8df83eae
RK
2979 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2980 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 2981
3e4093b6
RS
2982 if (unsigned_op1 ^ unsigned_op2)
2983 {
2984 /* Do not warn if the result type is signed, since the
2985 signed type will only be chosen if it can represent
2986 all the values of the unsigned type. */
3f75a254 2987 if (!TYPE_UNSIGNED (result_type))
3e4093b6
RS
2988 /* OK */;
2989 /* Do not warn if the signed quantity is an unsuffixed
2990 integer literal (or some static constant expression
2991 involving such literals) and it is non-negative. */
3a5b9284
RH
2992 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2993 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3e4093b6
RS
2994 /* OK */;
2995 else
2996 warning ("signed and unsigned type in conditional expression");
2997 }
2998 }
2999 }
3000 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3001 {
3002 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3003 pedwarn ("ISO C forbids conditional expr with only one void side");
3004 result_type = void_type_node;
3005 }
3006 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3007 {
3008 if (comp_target_types (type1, type2, 1))
10bc1b1b 3009 result_type = common_pointer_type (type1, type2);
3e4093b6
RS
3010 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3011 && TREE_CODE (orig_op1) != NOP_EXPR)
3012 result_type = qualify_type (type2, type1);
3013 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3014 && TREE_CODE (orig_op2) != NOP_EXPR)
3015 result_type = qualify_type (type1, type2);
3016 else if (VOID_TYPE_P (TREE_TYPE (type1)))
34a80643 3017 {
3e4093b6 3018 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
bda67431
JM
3019 pedwarn ("ISO C forbids conditional expr between "
3020 "%<void *%> and function pointer");
3e4093b6
RS
3021 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3022 TREE_TYPE (type2)));
34a80643 3023 }
3e4093b6 3024 else if (VOID_TYPE_P (TREE_TYPE (type2)))
1c2a9b35 3025 {
3e4093b6 3026 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
bda67431
JM
3027 pedwarn ("ISO C forbids conditional expr between "
3028 "%<void *%> and function pointer");
3e4093b6
RS
3029 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3030 TREE_TYPE (type1)));
1c2a9b35 3031 }
34a80643 3032 else
ab87f8c8 3033 {
3e4093b6
RS
3034 pedwarn ("pointer type mismatch in conditional expression");
3035 result_type = build_pointer_type (void_type_node);
ab87f8c8 3036 }
3e4093b6
RS
3037 }
3038 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3039 {
3f75a254 3040 if (!integer_zerop (op2))
3e4093b6
RS
3041 pedwarn ("pointer/integer type mismatch in conditional expression");
3042 else
ab87f8c8 3043 {
3e4093b6 3044 op2 = null_pointer_node;
ab87f8c8 3045 }
3e4093b6
RS
3046 result_type = type1;
3047 }
3048 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3049 {
3050 if (!integer_zerop (op1))
3051 pedwarn ("pointer/integer type mismatch in conditional expression");
3052 else
ab87f8c8 3053 {
3e4093b6 3054 op1 = null_pointer_node;
ab87f8c8 3055 }
3e4093b6
RS
3056 result_type = type2;
3057 }
1c2a9b35 3058
3e4093b6
RS
3059 if (!result_type)
3060 {
3061 if (flag_cond_mismatch)
3062 result_type = void_type_node;
3063 else
400fbf9f 3064 {
3e4093b6 3065 error ("type mismatch in conditional expression");
ab87f8c8 3066 return error_mark_node;
400fbf9f 3067 }
3e4093b6 3068 }
400fbf9f 3069
3e4093b6
RS
3070 /* Merge const and volatile flags of the incoming types. */
3071 result_type
3072 = build_type_variant (result_type,
3073 TREE_READONLY (op1) || TREE_READONLY (op2),
3074 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
b6a10c9f 3075
3e4093b6
RS
3076 if (result_type != TREE_TYPE (op1))
3077 op1 = convert_and_check (result_type, op1);
3078 if (result_type != TREE_TYPE (op2))
3079 op2 = convert_and_check (result_type, op2);
b6a10c9f 3080
3e4093b6 3081 if (TREE_CODE (ifexp) == INTEGER_CST)
53cd18ec 3082 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2f6e4e97 3083
53fb4de3 3084 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3e4093b6
RS
3085}
3086\f
487a92fe
JM
3087/* Return a compound expression that performs two expressions and
3088 returns the value of the second of them. */
400fbf9f 3089
3e4093b6 3090tree
487a92fe 3091build_compound_expr (tree expr1, tree expr2)
3e4093b6 3092{
487a92fe
JM
3093 /* Convert arrays and functions to pointers. */
3094 expr2 = default_function_array_conversion (expr2);
400fbf9f 3095
3f75a254 3096 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
3097 {
3098 /* The left-hand operand of a comma expression is like an expression
3099 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3100 any side-effects, unless it was explicitly cast to (void). */
3101 if (warn_unused_value
3f75a254 3102 && !(TREE_CODE (expr1) == CONVERT_EXPR
487a92fe 3103 && VOID_TYPE_P (TREE_TYPE (expr1))))
3e4093b6 3104 warning ("left-hand operand of comma expression has no effect");
3e4093b6 3105 }
400fbf9f 3106
3e4093b6
RS
3107 /* With -Wunused, we should also warn if the left-hand operand does have
3108 side-effects, but computes a value which is not used. For example, in
3109 `foo() + bar(), baz()' the result of the `+' operator is not used,
3110 so we should issue a warning. */
3111 else if (warn_unused_value)
487a92fe 3112 warn_if_unused_value (expr1, input_location);
400fbf9f 3113
53fb4de3 3114 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3e4093b6 3115}
400fbf9f 3116
3e4093b6 3117/* Build an expression representing a cast to type TYPE of expression EXPR. */
400fbf9f 3118
3e4093b6
RS
3119tree
3120build_c_cast (tree type, tree expr)
3121{
3122 tree value = expr;
400fbf9f 3123
3e4093b6
RS
3124 if (type == error_mark_node || expr == error_mark_node)
3125 return error_mark_node;
400fbf9f 3126
3e4093b6
RS
3127 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3128 only in <protocol> qualifications. But when constructing cast expressions,
3129 the protocols do matter and must be kept around. */
700686fa
ZL
3130 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3131 return build1 (NOP_EXPR, type, expr);
3132
3133 type = TYPE_MAIN_VARIANT (type);
400fbf9f 3134
3e4093b6
RS
3135 if (TREE_CODE (type) == ARRAY_TYPE)
3136 {
3137 error ("cast specifies array type");
3138 return error_mark_node;
3139 }
400fbf9f 3140
3e4093b6
RS
3141 if (TREE_CODE (type) == FUNCTION_TYPE)
3142 {
3143 error ("cast specifies function type");
3144 return error_mark_node;
3145 }
400fbf9f 3146
3e4093b6
RS
3147 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3148 {
3149 if (pedantic)
400fbf9f 3150 {
3e4093b6
RS
3151 if (TREE_CODE (type) == RECORD_TYPE
3152 || TREE_CODE (type) == UNION_TYPE)
3153 pedwarn ("ISO C forbids casting nonscalar to the same type");
400fbf9f 3154 }
3e4093b6
RS
3155 }
3156 else if (TREE_CODE (type) == UNION_TYPE)
3157 {
3158 tree field;
3159 value = default_function_array_conversion (value);
400fbf9f 3160
3e4093b6
RS
3161 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3162 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
132da1a5 3163 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
3164 break;
3165
3166 if (field)
400fbf9f 3167 {
3e4093b6
RS
3168 tree t;
3169
3170 if (pedantic)
3171 pedwarn ("ISO C forbids casts to union type");
3172 t = digest_init (type,
3173 build_constructor (type,
3174 build_tree_list (field, value)),
916c5919 3175 true, 0);
3e4093b6 3176 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6de9cd9a 3177 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3e4093b6 3178 return t;
400fbf9f 3179 }
3e4093b6
RS
3180 error ("cast to union type from type not present in union");
3181 return error_mark_node;
3182 }
3183 else
3184 {
3185 tree otype, ovalue;
400fbf9f 3186
3e4093b6
RS
3187 /* If casting to void, avoid the error that would come
3188 from default_conversion in the case of a non-lvalue array. */
3189 if (type == void_type_node)
3190 return build1 (CONVERT_EXPR, type, value);
400fbf9f 3191
3e4093b6
RS
3192 /* Convert functions and arrays to pointers,
3193 but don't convert any other types. */
3194 value = default_function_array_conversion (value);
3195 otype = TREE_TYPE (value);
400fbf9f 3196
3e4093b6 3197 /* Optionally warn about potentially worrisome casts. */
770ae6cc 3198
3e4093b6
RS
3199 if (warn_cast_qual
3200 && TREE_CODE (type) == POINTER_TYPE
3201 && TREE_CODE (otype) == POINTER_TYPE)
3202 {
3203 tree in_type = type;
3204 tree in_otype = otype;
3205 int added = 0;
3206 int discarded = 0;
400fbf9f 3207
3e4093b6
RS
3208 /* Check that the qualifiers on IN_TYPE are a superset of
3209 the qualifiers of IN_OTYPE. The outermost level of
3210 POINTER_TYPE nodes is uninteresting and we stop as soon
3211 as we hit a non-POINTER_TYPE node on either type. */
3212 do
3213 {
3214 in_otype = TREE_TYPE (in_otype);
3215 in_type = TREE_TYPE (in_type);
400fbf9f 3216
3e4093b6
RS
3217 /* GNU C allows cv-qualified function types. 'const'
3218 means the function is very pure, 'volatile' means it
3219 can't return. We need to warn when such qualifiers
3220 are added, not when they're taken away. */
3221 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3222 && TREE_CODE (in_type) == FUNCTION_TYPE)
3223 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3224 else
3225 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3226 }
3227 while (TREE_CODE (in_type) == POINTER_TYPE
3228 && TREE_CODE (in_otype) == POINTER_TYPE);
400fbf9f 3229
3e4093b6
RS
3230 if (added)
3231 warning ("cast adds new qualifiers to function type");
400fbf9f 3232
3e4093b6
RS
3233 if (discarded)
3234 /* There are qualifiers present in IN_OTYPE that are not
3235 present in IN_TYPE. */
3236 warning ("cast discards qualifiers from pointer target type");
3237 }
400fbf9f 3238
3e4093b6
RS
3239 /* Warn about possible alignment problems. */
3240 if (STRICT_ALIGNMENT && warn_cast_align
3241 && TREE_CODE (type) == POINTER_TYPE
3242 && TREE_CODE (otype) == POINTER_TYPE
3243 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3244 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3245 /* Don't warn about opaque types, where the actual alignment
3246 restriction is unknown. */
3247 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3248 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3249 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3250 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3251 warning ("cast increases required alignment of target type");
e9a25f70 3252
3e4093b6
RS
3253 if (TREE_CODE (type) == INTEGER_TYPE
3254 && TREE_CODE (otype) == POINTER_TYPE
3255 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3256 && !TREE_CONSTANT (value))
3257 warning ("cast from pointer to integer of different size");
400fbf9f 3258
3e4093b6
RS
3259 if (warn_bad_function_cast
3260 && TREE_CODE (value) == CALL_EXPR
3261 && TREE_CODE (type) != TREE_CODE (otype))
ff80e49f
JM
3262 warning ("cast from function call of type %qT to non-matching "
3263 "type %qT", otype, type);
400fbf9f 3264
3e4093b6
RS
3265 if (TREE_CODE (type) == POINTER_TYPE
3266 && TREE_CODE (otype) == INTEGER_TYPE
3267 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3268 /* Don't warn about converting any constant. */
3269 && !TREE_CONSTANT (value))
3270 warning ("cast to pointer from integer of different size");
400fbf9f 3271
3e4093b6
RS
3272 if (TREE_CODE (type) == POINTER_TYPE
3273 && TREE_CODE (otype) == POINTER_TYPE
3274 && TREE_CODE (expr) == ADDR_EXPR
3275 && DECL_P (TREE_OPERAND (expr, 0))
3276 && flag_strict_aliasing && warn_strict_aliasing
3277 && !VOID_TYPE_P (TREE_TYPE (type)))
3278 {
3279 /* Casting the address of a decl to non void pointer. Warn
3280 if the cast breaks type based aliasing. */
3281 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3282 warning ("type-punning to incomplete type might break strict-aliasing rules");
5399d643
JW
3283 else
3284 {
3285 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3286 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3287
3288 if (!alias_sets_conflict_p (set1, set2))
3289 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3290 else if (warn_strict_aliasing > 1
3291 && !alias_sets_might_conflict_p (set1, set2))
3292 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3293 }
3e4093b6 3294 }
400fbf9f 3295
3897f229
JM
3296 /* If pedantic, warn for conversions between function and object
3297 pointer types, except for converting a null pointer constant
3298 to function pointer type. */
3299 if (pedantic
3300 && TREE_CODE (type) == POINTER_TYPE
3301 && TREE_CODE (otype) == POINTER_TYPE
3302 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3303 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3304 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3305
3306 if (pedantic
3307 && TREE_CODE (type) == POINTER_TYPE
3308 && TREE_CODE (otype) == POINTER_TYPE
3309 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3310 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3311 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3312 && TREE_CODE (expr) != NOP_EXPR))
3313 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3314
3e4093b6 3315 ovalue = value;
3e4093b6 3316 value = convert (type, value);
400fbf9f 3317
3e4093b6
RS
3318 /* Ignore any integer overflow caused by the cast. */
3319 if (TREE_CODE (value) == INTEGER_CST)
3320 {
092313ae 3321 if (EXPR_P (ovalue))
89b0433e
NS
3322 /* If OVALUE had overflow set, then so will VALUE, so it
3323 is safe to overwrite. */
092313ae
NS
3324 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3325 else
3326 TREE_OVERFLOW (value) = 0;
3327
6615c446 3328 if (CONSTANT_CLASS_P (ovalue))
89b0433e
NS
3329 /* Similarly, constant_overflow cannot have become
3330 cleared. */
22421b79 3331 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3e4093b6
RS
3332 }
3333 }
400fbf9f 3334
53cd18ec
JM
3335 /* Don't let a cast be an lvalue. */
3336 if (value == expr)
3e4093b6 3337 value = non_lvalue (value);
e9a25f70 3338
3e4093b6 3339 return value;
400fbf9f
JW
3340}
3341
3e4093b6
RS
3342/* Interpret a cast of expression EXPR to type TYPE. */
3343tree
f8893e47 3344c_cast_expr (struct c_type_name *type_name, tree expr)
400fbf9f 3345{
f8893e47 3346 tree type;
3e4093b6 3347 int saved_wsp = warn_strict_prototypes;
c5c76735 3348
3e4093b6
RS
3349 /* This avoids warnings about unprototyped casts on
3350 integers. E.g. "#define SIG_DFL (void(*)())0". */
3351 if (TREE_CODE (expr) == INTEGER_CST)
3352 warn_strict_prototypes = 0;
f8893e47 3353 type = groktypename (type_name);
3e4093b6 3354 warn_strict_prototypes = saved_wsp;
c5c76735 3355
3e4093b6 3356 return build_c_cast (type, expr);
400fbf9f
JW
3357}
3358
3e4093b6
RS
3359\f
3360/* Build an assignment expression of lvalue LHS from value RHS.
3361 MODIFYCODE is the code for a binary operator that we use
3362 to combine the old value of LHS with RHS to get the new value.
3363 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
2f6e4e97 3364
3e4093b6
RS
3365tree
3366build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
400fbf9f 3367{
3e4093b6
RS
3368 tree result;
3369 tree newrhs;
3370 tree lhstype = TREE_TYPE (lhs);
3371 tree olhstype = lhstype;
e9a25f70 3372
3e4093b6
RS
3373 /* Types that aren't fully specified cannot be used in assignments. */
3374 lhs = require_complete_type (lhs);
e9a25f70 3375
3e4093b6
RS
3376 /* Avoid duplicate error messages from operands that had errors. */
3377 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3378 return error_mark_node;
400fbf9f 3379
ed248cf7 3380 STRIP_TYPE_NOPS (rhs);
e9a25f70 3381
3e4093b6 3382 newrhs = rhs;
400fbf9f 3383
3e4093b6
RS
3384 /* If a binary op has been requested, combine the old LHS value with the RHS
3385 producing the value we should actually store into the LHS. */
3386
3387 if (modifycode != NOP_EXPR)
400fbf9f 3388 {
3e4093b6
RS
3389 lhs = stabilize_reference (lhs);
3390 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
400fbf9f 3391 }
400fbf9f 3392
9bf24266 3393 if (!lvalue_or_else (lhs, lv_assign))
3e4093b6 3394 return error_mark_node;
400fbf9f 3395
9bf24266 3396 /* Give an error for storing in something that is 'const'. */
bbbd6700 3397
3e4093b6
RS
3398 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3399 || ((TREE_CODE (lhstype) == RECORD_TYPE
3400 || TREE_CODE (lhstype) == UNION_TYPE)
3401 && C_TYPE_FIELDS_READONLY (lhstype)))
9bf24266 3402 readonly_error (lhs, lv_assign);
bbbd6700 3403
3e4093b6
RS
3404 /* If storing into a structure or union member,
3405 it has probably been given type `int'.
3406 Compute the type that would go with
3407 the actual amount of storage the member occupies. */
bbbd6700 3408
3e4093b6
RS
3409 if (TREE_CODE (lhs) == COMPONENT_REF
3410 && (TREE_CODE (lhstype) == INTEGER_TYPE
3411 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3412 || TREE_CODE (lhstype) == REAL_TYPE
3413 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3414 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 3415
3e4093b6
RS
3416 /* If storing in a field that is in actuality a short or narrower than one,
3417 we must store in the field in its actual type. */
3418
3419 if (lhstype != TREE_TYPE (lhs))
3420 {
3421 lhs = copy_node (lhs);
3422 TREE_TYPE (lhs) = lhstype;
400fbf9f 3423 }
400fbf9f 3424
3e4093b6 3425 /* Convert new value to destination type. */
400fbf9f 3426
2ac2f164 3427 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3e4093b6
RS
3428 NULL_TREE, NULL_TREE, 0);
3429 if (TREE_CODE (newrhs) == ERROR_MARK)
3430 return error_mark_node;
400fbf9f 3431
ea4b7848 3432 /* Scan operands. */
400fbf9f 3433
53fb4de3 3434 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3e4093b6 3435 TREE_SIDE_EFFECTS (result) = 1;
400fbf9f 3436
3e4093b6
RS
3437 /* If we got the LHS in a different type for storing in,
3438 convert the result back to the nominal type of LHS
3439 so that the value we return always has the same type
3440 as the LHS argument. */
e855c5ce 3441
3e4093b6
RS
3442 if (olhstype == TREE_TYPE (result))
3443 return result;
2ac2f164 3444 return convert_for_assignment (olhstype, result, ic_assign,
3e4093b6
RS
3445 NULL_TREE, NULL_TREE, 0);
3446}
3447\f
3448/* Convert value RHS to type TYPE as preparation for an assignment
3449 to an lvalue of type TYPE.
3450 The real work of conversion is done by `convert'.
3451 The purpose of this function is to generate error messages
3452 for assignments that are not allowed in C.
2ac2f164
JM
3453 ERRTYPE says whether it is argument passing, assignment,
3454 initialization or return.
2f6e4e97 3455
2ac2f164 3456 FUNCTION is a tree for the function being called.
3e4093b6 3457 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 3458
3e4093b6 3459static tree
2ac2f164
JM
3460convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3461 tree fundecl, tree function, int parmnum)
3e4093b6
RS
3462{
3463 enum tree_code codel = TREE_CODE (type);
3464 tree rhstype;
3465 enum tree_code coder;
2ac2f164
JM
3466 tree rname = NULL_TREE;
3467
6dcc04b0 3468 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
2ac2f164
JM
3469 {
3470 tree selector;
3471 /* Change pointer to function to the function itself for
3472 diagnostics. */
3473 if (TREE_CODE (function) == ADDR_EXPR
3474 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3475 function = TREE_OPERAND (function, 0);
3476
3477 /* Handle an ObjC selector specially for diagnostics. */
3478 selector = objc_message_selector ();
3479 rname = function;
3480 if (selector && parmnum > 2)
3481 {
3482 rname = selector;
3483 parmnum -= 2;
3484 }
3485 }
3486
3487 /* This macro is used to emit diagnostics to ensure that all format
3488 strings are complete sentences, visible to gettext and checked at
3489 compile time. */
3490#define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3491 do { \
3492 switch (errtype) \
3493 { \
3494 case ic_argpass: \
3495 pedwarn (AR, parmnum, rname); \
3496 break; \
6dcc04b0
JM
3497 case ic_argpass_nonproto: \
3498 warning (AR, parmnum, rname); \
3499 break; \
2ac2f164
JM
3500 case ic_assign: \
3501 pedwarn (AS); \
3502 break; \
3503 case ic_init: \
3504 pedwarn (IN); \
3505 break; \
3506 case ic_return: \
3507 pedwarn (RE); \
3508 break; \
3509 default: \
3510 gcc_unreachable (); \
3511 } \
3512 } while (0)
cb3ca04e 3513
ed248cf7 3514 STRIP_TYPE_NOPS (rhs);
3e4093b6
RS
3515
3516 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3517 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3518 rhs = default_conversion (rhs);
3519 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3520 rhs = decl_constant_value_for_broken_optimization (rhs);
3521
3522 rhstype = TREE_TYPE (rhs);
3523 coder = TREE_CODE (rhstype);
3524
3525 if (coder == ERROR_MARK)
3526 return error_mark_node;
3527
3528 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
400fbf9f 3529 {
3e4093b6
RS
3530 overflow_warning (rhs);
3531 /* Check for Objective-C protocols. This will automatically
3532 issue a warning if there are protocol violations. No need to
3533 use the return value. */
3534 if (c_dialect_objc ())
3535 objc_comptypes (type, rhstype, 0);
3536 return rhs;
400fbf9f 3537 }
3e4093b6
RS
3538
3539 if (coder == VOID_TYPE)
400fbf9f 3540 {
6dcc04b0
JM
3541 /* Except for passing an argument to an unprototyped function,
3542 this is a constraint violation. When passing an argument to
3543 an unprototyped function, it is compile-time undefined;
3544 making it a constraint in that case was rejected in
3545 DR#252. */
3e4093b6
RS
3546 error ("void value not ignored as it ought to be");
3547 return error_mark_node;
400fbf9f 3548 }
3e4093b6
RS
3549 /* A type converts to a reference to it.
3550 This code doesn't fully support references, it's just for the
3551 special case of va_start and va_copy. */
3552 if (codel == REFERENCE_TYPE
132da1a5 3553 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
400fbf9f 3554 {
3e4093b6 3555 if (!lvalue_p (rhs))
400fbf9f 3556 {
3e4093b6
RS
3557 error ("cannot pass rvalue to reference parameter");
3558 return error_mark_node;
400fbf9f 3559 }
3e4093b6
RS
3560 if (!c_mark_addressable (rhs))
3561 return error_mark_node;
3562 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3563
3564 /* We already know that these two types are compatible, but they
3565 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3566 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3567 likely to be va_list, a typedef to __builtin_va_list, which
3568 is different enough that it will cause problems later. */
3569 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3570 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3571
3572 rhs = build1 (NOP_EXPR, type, rhs);
3573 return rhs;
400fbf9f 3574 }
3e4093b6 3575 /* Some types can interconvert without explicit casts. */
3274deff 3576 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
cc27e657 3577 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3e4093b6
RS
3578 return convert (type, rhs);
3579 /* Arithmetic types all interconvert, and enum is treated like int. */
3580 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3581 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3582 || codel == BOOLEAN_TYPE)
3583 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3584 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3585 || coder == BOOLEAN_TYPE))
3586 return convert_and_check (type, rhs);
400fbf9f 3587
3e4093b6
RS
3588 /* Conversion to a transparent union from its member types.
3589 This applies only to function arguments. */
2ac2f164 3590 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
6dcc04b0 3591 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
400fbf9f 3592 {
3e4093b6
RS
3593 tree memb_types;
3594 tree marginal_memb_type = 0;
3595
3596 for (memb_types = TYPE_FIELDS (type); memb_types;
3597 memb_types = TREE_CHAIN (memb_types))
400fbf9f 3598 {
3e4093b6 3599 tree memb_type = TREE_TYPE (memb_types);
400fbf9f 3600
3e4093b6 3601 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 3602 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 3603 break;
e58cd767 3604
3e4093b6
RS
3605 if (TREE_CODE (memb_type) != POINTER_TYPE)
3606 continue;
2f6e4e97 3607
3e4093b6
RS
3608 if (coder == POINTER_TYPE)
3609 {
3610 tree ttl = TREE_TYPE (memb_type);
3611 tree ttr = TREE_TYPE (rhstype);
400fbf9f 3612
3e4093b6
RS
3613 /* Any non-function converts to a [const][volatile] void *
3614 and vice versa; otherwise, targets must be the same.
3615 Meanwhile, the lhs target must have all the qualifiers of
3616 the rhs. */
3617 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3618 || comp_target_types (memb_type, rhstype, 0))
3619 {
3620 /* If this type won't generate any warnings, use it. */
3621 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3622 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3623 && TREE_CODE (ttl) == FUNCTION_TYPE)
3624 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3625 == TYPE_QUALS (ttr))
3626 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3627 == TYPE_QUALS (ttl))))
3628 break;
400fbf9f 3629
3e4093b6 3630 /* Keep looking for a better type, but remember this one. */
3f75a254 3631 if (!marginal_memb_type)
3e4093b6
RS
3632 marginal_memb_type = memb_type;
3633 }
3634 }
82bde854 3635
3e4093b6
RS
3636 /* Can convert integer zero to any pointer type. */
3637 if (integer_zerop (rhs)
3638 || (TREE_CODE (rhs) == NOP_EXPR
3639 && integer_zerop (TREE_OPERAND (rhs, 0))))
3640 {
3641 rhs = null_pointer_node;
3642 break;
3643 }
3644 }
400fbf9f 3645
3e4093b6
RS
3646 if (memb_types || marginal_memb_type)
3647 {
3f75a254 3648 if (!memb_types)
3e4093b6
RS
3649 {
3650 /* We have only a marginally acceptable member type;
3651 it needs a warning. */
3652 tree ttl = TREE_TYPE (marginal_memb_type);
3653 tree ttr = TREE_TYPE (rhstype);
714a0864 3654
3e4093b6
RS
3655 /* Const and volatile mean something different for function
3656 types, so the usual warnings are not appropriate. */
3657 if (TREE_CODE (ttr) == FUNCTION_TYPE
3658 && TREE_CODE (ttl) == FUNCTION_TYPE)
3659 {
3660 /* Because const and volatile on functions are
3661 restrictions that say the function will not do
3662 certain things, it is okay to use a const or volatile
3663 function where an ordinary one is wanted, but not
3664 vice-versa. */
3665 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
2ac2f164
JM
3666 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3667 "makes qualified function "
3668 "pointer from unqualified"),
3669 N_("assignment makes qualified "
3670 "function pointer from "
3671 "unqualified"),
3672 N_("initialization makes qualified "
3673 "function pointer from "
3674 "unqualified"),
3675 N_("return makes qualified function "
3676 "pointer from unqualified"));
3e4093b6
RS
3677 }
3678 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
2ac2f164
JM
3679 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3680 "qualifiers from pointer target type"),
3681 N_("assignment discards qualifiers "
3682 "from pointer target type"),
3683 N_("initialization discards qualifiers "
3684 "from pointer target type"),
3685 N_("return discards qualifiers from "
3686 "pointer target type"));
3e4093b6 3687 }
400fbf9f 3688
3f75a254 3689 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3e4093b6 3690 pedwarn ("ISO C prohibits argument conversion to union type");
0e7c47fa 3691
3e4093b6
RS
3692 return build1 (NOP_EXPR, type, rhs);
3693 }
0e7c47fa
RK
3694 }
3695
3e4093b6
RS
3696 /* Conversions among pointers */
3697 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3698 && (coder == codel))
400fbf9f 3699 {
3e4093b6
RS
3700 tree ttl = TREE_TYPE (type);
3701 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
3702 tree mvl = ttl;
3703 tree mvr = ttr;
3e4093b6 3704 bool is_opaque_pointer;
264fa2db 3705 int target_cmp = 0; /* Cache comp_target_types () result. */
400fbf9f 3706
46df2823
JM
3707 if (TREE_CODE (mvl) != ARRAY_TYPE)
3708 mvl = TYPE_MAIN_VARIANT (mvl);
3709 if (TREE_CODE (mvr) != ARRAY_TYPE)
3710 mvr = TYPE_MAIN_VARIANT (mvr);
3e4093b6 3711 /* Opaque pointers are treated like void pointers. */
5fd9b178
KH
3712 is_opaque_pointer = (targetm.vector_opaque_p (type)
3713 || targetm.vector_opaque_p (rhstype))
3e4093b6
RS
3714 && TREE_CODE (ttl) == VECTOR_TYPE
3715 && TREE_CODE (ttr) == VECTOR_TYPE;
400fbf9f 3716
3e4093b6
RS
3717 /* Any non-function converts to a [const][volatile] void *
3718 and vice versa; otherwise, targets must be the same.
3719 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3720 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 3721 || (target_cmp = comp_target_types (type, rhstype, 0))
3e4093b6 3722 || is_opaque_pointer
46df2823
JM
3723 || (c_common_unsigned_type (mvl)
3724 == c_common_unsigned_type (mvr)))
3e4093b6
RS
3725 {
3726 if (pedantic
3727 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3728 ||
3729 (VOID_TYPE_P (ttr)
3730 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3731 which are not ANSI null ptr constants. */
3732 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3733 && TREE_CODE (ttl) == FUNCTION_TYPE)))
2ac2f164
JM
3734 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3735 "%qE between function pointer "
3736 "and %<void *%>"),
3737 N_("ISO C forbids assignment between "
3738 "function pointer and %<void *%>"),
3739 N_("ISO C forbids initialization between "
3740 "function pointer and %<void *%>"),
3741 N_("ISO C forbids return between function "
3742 "pointer and %<void *%>"));
3e4093b6
RS
3743 /* Const and volatile mean something different for function types,
3744 so the usual warnings are not appropriate. */
3745 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3746 && TREE_CODE (ttl) != FUNCTION_TYPE)
3747 {
3748 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
2ac2f164
JM
3749 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3750 "qualifiers from pointer target type"),
3751 N_("assignment discards qualifiers "
3752 "from pointer target type"),
3753 N_("initialization discards qualifiers "
3754 "from pointer target type"),
3755 N_("return discards qualifiers from "
3756 "pointer target type"));
3e4093b6
RS
3757 /* If this is not a case of ignoring a mismatch in signedness,
3758 no warning. */
3759 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 3760 || target_cmp)
3e4093b6
RS
3761 ;
3762 /* If there is a mismatch, do warn. */
f2fd3821 3763 else if (warn_pointer_sign)
2ac2f164
JM
3764 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3765 "%d of %qE differ in signedness"),
3766 N_("pointer targets in assignment "
3767 "differ in signedness"),
3768 N_("pointer targets in initialization "
3769 "differ in signedness"),
3770 N_("pointer targets in return differ "
3771 "in signedness"));
3e4093b6
RS
3772 }
3773 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3774 && TREE_CODE (ttr) == FUNCTION_TYPE)
3775 {
3776 /* Because const and volatile on functions are restrictions
3777 that say the function will not do certain things,
3778 it is okay to use a const or volatile function
3779 where an ordinary one is wanted, but not vice-versa. */
3780 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
2ac2f164
JM
3781 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3782 "qualified function pointer "
3783 "from unqualified"),
3784 N_("assignment makes qualified function "
3785 "pointer from unqualified"),
3786 N_("initialization makes qualified "
3787 "function pointer from unqualified"),
3788 N_("return makes qualified function "
3789 "pointer from unqualified"));
3e4093b6
RS
3790 }
3791 }
3792 else
2ac2f164
JM
3793 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3794 "incompatible pointer type"),
3795 N_("assignment from incompatible pointer type"),
3796 N_("initialization from incompatible "
3797 "pointer type"),
3798 N_("return from incompatible pointer type"));
3e4093b6
RS
3799 return convert (type, rhs);
3800 }
b494fd98
EB
3801 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3802 {
6dcc04b0
JM
3803 /* ??? This should not be an error when inlining calls to
3804 unprototyped functions. */
b494fd98
EB
3805 error ("invalid use of non-lvalue array");
3806 return error_mark_node;
3807 }
3e4093b6 3808 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 3809 {
3e4093b6
RS
3810 /* An explicit constant 0 can convert to a pointer,
3811 or one that results from arithmetic, even including
3812 a cast to integer type. */
3f75a254 3813 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3e4093b6 3814 &&
3f75a254
JM
3815 !(TREE_CODE (rhs) == NOP_EXPR
3816 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3817 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3818 && integer_zerop (TREE_OPERAND (rhs, 0))))
2ac2f164
JM
3819 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3820 "pointer from integer without a cast"),
3821 N_("assignment makes pointer from integer "
3822 "without a cast"),
3823 N_("initialization makes pointer from "
3824 "integer without a cast"),
3825 N_("return makes pointer from integer "
3826 "without a cast"));
b3006337
EB
3827
3828 return convert (type, rhs);
400fbf9f 3829 }
3e4093b6 3830 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 3831 {
2ac2f164
JM
3832 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3833 "from pointer without a cast"),
3834 N_("assignment makes integer from pointer "
3835 "without a cast"),
3836 N_("initialization makes integer from pointer "
3837 "without a cast"),
3838 N_("return makes integer from pointer "
3839 "without a cast"));
3e4093b6
RS
3840 return convert (type, rhs);
3841 }
3842 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3843 return convert (type, rhs);
400fbf9f 3844
2ac2f164 3845 switch (errtype)
3e4093b6 3846 {
2ac2f164 3847 case ic_argpass:
6dcc04b0
JM
3848 case ic_argpass_nonproto:
3849 /* ??? This should not be an error when inlining calls to
3850 unprototyped functions. */
2ac2f164
JM
3851 error ("incompatible type for argument %d of %qE", parmnum, rname);
3852 break;
3853 case ic_assign:
3854 error ("incompatible types in assignment");
3855 break;
3856 case ic_init:
3857 error ("incompatible types in initialization");
3858 break;
3859 case ic_return:
3860 error ("incompatible types in return");
3861 break;
3862 default:
3863 gcc_unreachable ();
400fbf9f 3864 }
53b01f59 3865
3e4093b6
RS
3866 return error_mark_node;
3867}
53b01f59 3868
d5123bae
MS
3869/* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3870 is used for error and waring reporting and indicates which argument
3871 is being processed. */
400fbf9f 3872
3e4093b6 3873tree
d5123bae 3874c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3e4093b6
RS
3875{
3876 tree ret, type;
400fbf9f 3877
3e4093b6
RS
3878 /* If FN was prototyped, the value has been converted already
3879 in convert_arguments. */
3f75a254 3880 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3e4093b6 3881 return value;
f5963e61 3882
3e4093b6
RS
3883 type = TREE_TYPE (parm);
3884 ret = convert_for_assignment (type, value,
6dcc04b0 3885 ic_argpass_nonproto, fn,
2ac2f164 3886 fn, argnum);
61f71b34 3887 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3e4093b6
RS
3888 && INTEGRAL_TYPE_P (type)
3889 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3890 ret = default_conversion (ret);
3891 return ret;
3892}
3e4093b6
RS
3893\f
3894/* If VALUE is a compound expr all of whose expressions are constant, then
3895 return its value. Otherwise, return error_mark_node.
15b732b2 3896
3e4093b6
RS
3897 This is for handling COMPOUND_EXPRs as initializer elements
3898 which is allowed with a warning when -pedantic is specified. */
15b732b2 3899
3e4093b6
RS
3900static tree
3901valid_compound_expr_initializer (tree value, tree endtype)
3902{
3903 if (TREE_CODE (value) == COMPOUND_EXPR)
3904 {
3905 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3906 == error_mark_node)
3907 return error_mark_node;
3908 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3909 endtype);
3910 }
116df786 3911 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
3912 return error_mark_node;
3913 else
3914 return value;
15b732b2 3915}
400fbf9f 3916\f
3e4093b6
RS
3917/* Perform appropriate conversions on the initial value of a variable,
3918 store it in the declaration DECL,
3919 and print any error messages that are appropriate.
3920 If the init is invalid, store an ERROR_MARK. */
400fbf9f 3921
3e4093b6
RS
3922void
3923store_init_value (tree decl, tree init)
400fbf9f 3924{
3e4093b6 3925 tree value, type;
400fbf9f 3926
3e4093b6 3927 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 3928
3e4093b6
RS
3929 type = TREE_TYPE (decl);
3930 if (TREE_CODE (type) == ERROR_MARK)
3931 return;
400fbf9f 3932
3e4093b6 3933 /* Digest the specified initializer into an expression. */
400fbf9f 3934
916c5919 3935 value = digest_init (type, init, true, TREE_STATIC (decl));
400fbf9f 3936
3e4093b6 3937 /* Store the expression if valid; else report error. */
400fbf9f 3938
3e4093b6 3939 if (warn_traditional && !in_system_header
3f75a254 3940 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3e4093b6 3941 warning ("traditional C rejects automatic aggregate initialization");
2f6e4e97 3942
3e4093b6 3943 DECL_INITIAL (decl) = value;
400fbf9f 3944
3e4093b6
RS
3945 /* ANSI wants warnings about out-of-range constant initializers. */
3946 STRIP_TYPE_NOPS (value);
3947 constant_expression_warning (value);
400fbf9f 3948
3e4093b6
RS
3949 /* Check if we need to set array size from compound literal size. */
3950 if (TREE_CODE (type) == ARRAY_TYPE
3951 && TYPE_DOMAIN (type) == 0
3952 && value != error_mark_node)
400fbf9f 3953 {
3e4093b6
RS
3954 tree inside_init = init;
3955
ed248cf7 3956 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
3957 inside_init = fold (inside_init);
3958
3959 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3960 {
3961 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3962
3963 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3964 {
3965 /* For int foo[] = (int [3]){1}; we need to set array size
3966 now since later on array initializer will be just the
3967 brace enclosed list of the compound literal. */
3968 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3969 layout_type (type);
3970 layout_decl (decl, 0);
3971 }
3972 }
400fbf9f 3973 }
3e4093b6
RS
3974}
3975\f
3976/* Methods for storing and printing names for error messages. */
400fbf9f 3977
3e4093b6
RS
3978/* Implement a spelling stack that allows components of a name to be pushed
3979 and popped. Each element on the stack is this structure. */
400fbf9f 3980
3e4093b6
RS
3981struct spelling
3982{
3983 int kind;
3984 union
400fbf9f 3985 {
3e4093b6
RS
3986 int i;
3987 const char *s;
3988 } u;
3989};
2f6e4e97 3990
3e4093b6
RS
3991#define SPELLING_STRING 1
3992#define SPELLING_MEMBER 2
3993#define SPELLING_BOUNDS 3
400fbf9f 3994
3e4093b6
RS
3995static struct spelling *spelling; /* Next stack element (unused). */
3996static struct spelling *spelling_base; /* Spelling stack base. */
3997static int spelling_size; /* Size of the spelling stack. */
400fbf9f 3998
3e4093b6
RS
3999/* Macros to save and restore the spelling stack around push_... functions.
4000 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 4001
3e4093b6
RS
4002#define SPELLING_DEPTH() (spelling - spelling_base)
4003#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 4004
3e4093b6
RS
4005/* Push an element on the spelling stack with type KIND and assign VALUE
4006 to MEMBER. */
400fbf9f 4007
3e4093b6
RS
4008#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4009{ \
4010 int depth = SPELLING_DEPTH (); \
4011 \
4012 if (depth >= spelling_size) \
4013 { \
4014 spelling_size += 10; \
cca8ead2
BI
4015 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4016 spelling_size); \
3e4093b6
RS
4017 RESTORE_SPELLING_DEPTH (depth); \
4018 } \
4019 \
4020 spelling->kind = (KIND); \
4021 spelling->MEMBER = (VALUE); \
4022 spelling++; \
4023}
400fbf9f 4024
3e4093b6 4025/* Push STRING on the stack. Printed literally. */
400fbf9f 4026
3e4093b6
RS
4027static void
4028push_string (const char *string)
4029{
4030 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4031}
400fbf9f 4032
3e4093b6 4033/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 4034
3e4093b6
RS
4035static void
4036push_member_name (tree decl)
4037{
4038 const char *const string
4039 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4040 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4041}
400fbf9f 4042
3e4093b6 4043/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 4044
3e4093b6
RS
4045static void
4046push_array_bounds (int bounds)
4047{
4048 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4049}
bb58bec5 4050
3e4093b6 4051/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 4052
3e4093b6
RS
4053static int
4054spelling_length (void)
4055{
4056 int size = 0;
4057 struct spelling *p;
400fbf9f 4058
3e4093b6
RS
4059 for (p = spelling_base; p < spelling; p++)
4060 {
4061 if (p->kind == SPELLING_BOUNDS)
4062 size += 25;
4063 else
4064 size += strlen (p->u.s) + 1;
4065 }
4066
4067 return size;
400fbf9f 4068}
400fbf9f 4069
3e4093b6 4070/* Print the spelling to BUFFER and return it. */
400fbf9f 4071
3e4093b6
RS
4072static char *
4073print_spelling (char *buffer)
400fbf9f 4074{
3e4093b6
RS
4075 char *d = buffer;
4076 struct spelling *p;
400fbf9f 4077
3e4093b6
RS
4078 for (p = spelling_base; p < spelling; p++)
4079 if (p->kind == SPELLING_BOUNDS)
4080 {
4081 sprintf (d, "[%d]", p->u.i);
4082 d += strlen (d);
4083 }
4084 else
4085 {
4086 const char *s;
4087 if (p->kind == SPELLING_MEMBER)
4088 *d++ = '.';
4089 for (s = p->u.s; (*d = *s++); d++)
4090 ;
4091 }
4092 *d++ = '\0';
4093 return buffer;
4094}
400fbf9f 4095
3e4093b6
RS
4096/* Issue an error message for a bad initializer component.
4097 MSGID identifies the message.
4098 The component name is taken from the spelling stack. */
400fbf9f 4099
3e4093b6
RS
4100void
4101error_init (const char *msgid)
4102{
4103 char *ofwhat;
400fbf9f 4104
3e4093b6 4105 error ("%s", _(msgid));
28dab132 4106 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 4107 if (*ofwhat)
bda67431 4108 error ("(near initialization for %qs)", ofwhat);
3e4093b6 4109}
400fbf9f 4110
3e4093b6
RS
4111/* Issue a pedantic warning for a bad initializer component.
4112 MSGID identifies the message.
4113 The component name is taken from the spelling stack. */
400fbf9f 4114
3e4093b6
RS
4115void
4116pedwarn_init (const char *msgid)
4117{
4118 char *ofwhat;
9f720c3e 4119
3e4093b6 4120 pedwarn ("%s", _(msgid));
28dab132 4121 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 4122 if (*ofwhat)
bda67431 4123 pedwarn ("(near initialization for %qs)", ofwhat);
3e4093b6 4124}
9f720c3e 4125
3e4093b6
RS
4126/* Issue a warning for a bad initializer component.
4127 MSGID identifies the message.
4128 The component name is taken from the spelling stack. */
61179109 4129
3e4093b6
RS
4130static void
4131warning_init (const char *msgid)
4132{
4133 char *ofwhat;
7e842ef8 4134
3e4093b6 4135 warning ("%s", _(msgid));
28dab132 4136 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 4137 if (*ofwhat)
bda67431 4138 warning ("(near initialization for %qs)", ofwhat);
3e4093b6
RS
4139}
4140\f
916c5919
JM
4141/* If TYPE is an array type and EXPR is a parenthesized string
4142 constant, warn if pedantic that EXPR is being used to initialize an
4143 object of type TYPE. */
4144
4145void
4146maybe_warn_string_init (tree type, struct c_expr expr)
4147{
4148 if (pedantic
4149 && TREE_CODE (type) == ARRAY_TYPE
4150 && TREE_CODE (expr.value) == STRING_CST
4151 && expr.original_code != STRING_CST)
4152 pedwarn_init ("array initialized from parenthesized string constant");
4153}
4154
3e4093b6
RS
4155/* Digest the parser output INIT as an initializer for type TYPE.
4156 Return a C expression of type TYPE to represent the initial value.
7e842ef8 4157
916c5919
JM
4158 If INIT is a string constant, STRICT_STRING is true if it is
4159 unparenthesized or we should not warn here for it being parenthesized.
4160 For other types of INIT, STRICT_STRING is not used.
4161
3e4093b6
RS
4162 REQUIRE_CONSTANT requests an error if non-constant initializers or
4163 elements are seen. */
7e842ef8 4164
3e4093b6 4165static tree
916c5919 4166digest_init (tree type, tree init, bool strict_string, int require_constant)
3e4093b6
RS
4167{
4168 enum tree_code code = TREE_CODE (type);
4169 tree inside_init = init;
7e842ef8 4170
3e4093b6
RS
4171 if (type == error_mark_node
4172 || init == error_mark_node
4173 || TREE_TYPE (init) == error_mark_node)
4174 return error_mark_node;
7e842ef8 4175
ed248cf7 4176 STRIP_TYPE_NOPS (inside_init);
7e842ef8 4177
3e4093b6 4178 inside_init = fold (inside_init);
7e842ef8 4179
3e4093b6
RS
4180 /* Initialization of an array of chars from a string constant
4181 optionally enclosed in braces. */
7e842ef8 4182
197463ae
JM
4183 if (code == ARRAY_TYPE && inside_init
4184 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6
RS
4185 {
4186 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
197463ae
JM
4187 /* Note that an array could be both an array of character type
4188 and an array of wchar_t if wchar_t is signed char or unsigned
4189 char. */
4190 bool char_array = (typ1 == char_type_node
4191 || typ1 == signed_char_type_node
4192 || typ1 == unsigned_char_type_node);
4193 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4194 if (char_array || wchar_array)
7e842ef8 4195 {
916c5919 4196 struct c_expr expr;
197463ae 4197 bool char_string;
916c5919
JM
4198 expr.value = inside_init;
4199 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4200 maybe_warn_string_init (type, expr);
4201
197463ae
JM
4202 char_string
4203 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4204 == char_type_node);
4205
3e4093b6 4206 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 4207 TYPE_MAIN_VARIANT (type)))
3e4093b6 4208 return inside_init;
7e842ef8 4209
197463ae 4210 if (!wchar_array && !char_string)
3e4093b6
RS
4211 {
4212 error_init ("char-array initialized from wide string");
4213 return error_mark_node;
4214 }
197463ae 4215 if (char_string && !char_array)
3e4093b6 4216 {
197463ae 4217 error_init ("wchar_t-array initialized from non-wide string");
3e4093b6 4218 return error_mark_node;
7e842ef8 4219 }
2f6e4e97 4220
3e4093b6
RS
4221 TREE_TYPE (inside_init) = type;
4222 if (TYPE_DOMAIN (type) != 0
4223 && TYPE_SIZE (type) != 0
4224 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4225 /* Subtract 1 (or sizeof (wchar_t))
4226 because it's ok to ignore the terminating null char
4227 that is counted in the length of the constant. */
4228 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4229 TREE_STRING_LENGTH (inside_init)
4230 - ((TYPE_PRECISION (typ1)
4231 != TYPE_PRECISION (char_type_node))
4232 ? (TYPE_PRECISION (wchar_type_node)
4233 / BITS_PER_UNIT)
4234 : 1)))
4235 pedwarn_init ("initializer-string for array of chars is too long");
7e842ef8 4236
3e4093b6 4237 return inside_init;
7e842ef8 4238 }
197463ae
JM
4239 else if (INTEGRAL_TYPE_P (typ1))
4240 {
4241 error_init ("array of inappropriate type initialized "
4242 "from string constant");
4243 return error_mark_node;
4244 }
7e842ef8
PE
4245 }
4246
3e4093b6
RS
4247 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4248 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4249 below and handle as a constructor. */
e89be13b
JJ
4250 if (code == VECTOR_TYPE
4251 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4252 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4253 && TREE_CONSTANT (inside_init))
4254 {
4255 if (TREE_CODE (inside_init) == VECTOR_CST
4256 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4257 TYPE_MAIN_VARIANT (type)))
4258 return inside_init;
4259
4260 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4261 {
4262 tree link;
4263
4264 /* Iterate through elements and check if all constructor
4265 elements are *_CSTs. */
4266 for (link = CONSTRUCTOR_ELTS (inside_init);
4267 link;
4268 link = TREE_CHAIN (link))
4269 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4270 break;
4271
4272 if (link == NULL)
4273 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4274 }
4275 }
6035d635 4276
3e4093b6
RS
4277 /* Any type can be initialized
4278 from an expression of the same type, optionally with braces. */
400fbf9f 4279
3e4093b6
RS
4280 if (inside_init && TREE_TYPE (inside_init) != 0
4281 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 4282 TYPE_MAIN_VARIANT (type))
3e4093b6 4283 || (code == ARRAY_TYPE
132da1a5 4284 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 4285 || (code == VECTOR_TYPE
132da1a5 4286 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 4287 || (code == POINTER_TYPE
3897f229 4288 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 4289 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 4290 TREE_TYPE (type)))
3897f229
JM
4291 || (code == POINTER_TYPE
4292 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4293 && comptypes (TREE_TYPE (inside_init),
132da1a5 4294 TREE_TYPE (type)))))
3e4093b6
RS
4295 {
4296 if (code == POINTER_TYPE)
b494fd98
EB
4297 {
4298 inside_init = default_function_array_conversion (inside_init);
4299
4300 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4301 {
4302 error_init ("invalid use of non-lvalue array");
4303 return error_mark_node;
4304 }
4305 }
4306
bae39a73
NS
4307 if (code == VECTOR_TYPE)
4308 /* Although the types are compatible, we may require a
4309 conversion. */
4310 inside_init = convert (type, inside_init);
3e4093b6
RS
4311
4312 if (require_constant && !flag_isoc99
4313 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 4314 {
3e4093b6
RS
4315 /* As an extension, allow initializing objects with static storage
4316 duration with compound literals (which are then treated just as
4317 the brace enclosed list they contain). */
4318 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4319 inside_init = DECL_INITIAL (decl);
400fbf9f 4320 }
3e4093b6
RS
4321
4322 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4323 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 4324 {
3e4093b6
RS
4325 error_init ("array initialized from non-constant array expression");
4326 return error_mark_node;
400fbf9f 4327 }
400fbf9f 4328
3e4093b6
RS
4329 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4330 inside_init = decl_constant_value_for_broken_optimization (inside_init);
2f6e4e97 4331
3e4093b6
RS
4332 /* Compound expressions can only occur here if -pedantic or
4333 -pedantic-errors is specified. In the later case, we always want
4334 an error. In the former case, we simply want a warning. */
4335 if (require_constant && pedantic
4336 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4337 {
4338 inside_init
4339 = valid_compound_expr_initializer (inside_init,
4340 TREE_TYPE (inside_init));
4341 if (inside_init == error_mark_node)
4342 error_init ("initializer element is not constant");
2f6e4e97 4343 else
3e4093b6
RS
4344 pedwarn_init ("initializer element is not constant");
4345 if (flag_pedantic_errors)
4346 inside_init = error_mark_node;
4347 }
4348 else if (require_constant
116df786
RH
4349 && !initializer_constant_valid_p (inside_init,
4350 TREE_TYPE (inside_init)))
3e4093b6
RS
4351 {
4352 error_init ("initializer element is not constant");
4353 inside_init = error_mark_node;
8b40563c 4354 }
f735a153 4355
3e4093b6
RS
4356 return inside_init;
4357 }
f735a153 4358
3e4093b6 4359 /* Handle scalar types, including conversions. */
400fbf9f 4360
3e4093b6 4361 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
cc27e657
PB
4362 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4363 || code == VECTOR_TYPE)
400fbf9f 4364 {
3e4093b6
RS
4365 /* Note that convert_for_assignment calls default_conversion
4366 for arrays and functions. We must not call it in the
4367 case where inside_init is a null pointer constant. */
4368 inside_init
2ac2f164 4369 = convert_for_assignment (type, init, ic_init,
3e4093b6 4370 NULL_TREE, NULL_TREE, 0);
2f6e4e97 4371
3274deff
JW
4372 /* Check to see if we have already given an error message. */
4373 if (inside_init == error_mark_node)
4374 ;
3f75a254 4375 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 4376 {
3e4093b6
RS
4377 error_init ("initializer element is not constant");
4378 inside_init = error_mark_node;
400fbf9f 4379 }
3e4093b6 4380 else if (require_constant
116df786
RH
4381 && !initializer_constant_valid_p (inside_init,
4382 TREE_TYPE (inside_init)))
400fbf9f 4383 {
3e4093b6
RS
4384 error_init ("initializer element is not computable at load time");
4385 inside_init = error_mark_node;
400fbf9f 4386 }
3e4093b6
RS
4387
4388 return inside_init;
400fbf9f 4389 }
d9fc6069 4390
3e4093b6 4391 /* Come here only for records and arrays. */
d9fc6069 4392
3e4093b6 4393 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 4394 {
3e4093b6
RS
4395 error_init ("variable-sized object may not be initialized");
4396 return error_mark_node;
d9fc6069 4397 }
3e4093b6
RS
4398
4399 error_init ("invalid initializer");
4400 return error_mark_node;
d9fc6069 4401}
400fbf9f 4402\f
3e4093b6 4403/* Handle initializers that use braces. */
400fbf9f 4404
3e4093b6
RS
4405/* Type of object we are accumulating a constructor for.
4406 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4407static tree constructor_type;
400fbf9f 4408
3e4093b6
RS
4409/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4410 left to fill. */
4411static tree constructor_fields;
400fbf9f 4412
3e4093b6
RS
4413/* For an ARRAY_TYPE, this is the specified index
4414 at which to store the next element we get. */
4415static tree constructor_index;
400fbf9f 4416
3e4093b6
RS
4417/* For an ARRAY_TYPE, this is the maximum index. */
4418static tree constructor_max_index;
400fbf9f 4419
3e4093b6
RS
4420/* For a RECORD_TYPE, this is the first field not yet written out. */
4421static tree constructor_unfilled_fields;
400fbf9f 4422
3e4093b6
RS
4423/* For an ARRAY_TYPE, this is the index of the first element
4424 not yet written out. */
4425static tree constructor_unfilled_index;
895ea614 4426
3e4093b6
RS
4427/* In a RECORD_TYPE, the byte index of the next consecutive field.
4428 This is so we can generate gaps between fields, when appropriate. */
4429static tree constructor_bit_index;
10d5caec 4430
3e4093b6
RS
4431/* If we are saving up the elements rather than allocating them,
4432 this is the list of elements so far (in reverse order,
4433 most recent first). */
4434static tree constructor_elements;
ad47f1e5 4435
3e4093b6
RS
4436/* 1 if constructor should be incrementally stored into a constructor chain,
4437 0 if all the elements should be kept in AVL tree. */
4438static int constructor_incremental;
ad47f1e5 4439
3e4093b6
RS
4440/* 1 if so far this constructor's elements are all compile-time constants. */
4441static int constructor_constant;
ad47f1e5 4442
3e4093b6
RS
4443/* 1 if so far this constructor's elements are all valid address constants. */
4444static int constructor_simple;
ad47f1e5 4445
3e4093b6
RS
4446/* 1 if this constructor is erroneous so far. */
4447static int constructor_erroneous;
d45cf215 4448
3e4093b6
RS
4449/* Structure for managing pending initializer elements, organized as an
4450 AVL tree. */
d45cf215 4451
3e4093b6 4452struct init_node
d45cf215 4453{
3e4093b6
RS
4454 struct init_node *left, *right;
4455 struct init_node *parent;
4456 int balance;
4457 tree purpose;
4458 tree value;
d45cf215
RS
4459};
4460
3e4093b6
RS
4461/* Tree of pending elements at this constructor level.
4462 These are elements encountered out of order
4463 which belong at places we haven't reached yet in actually
4464 writing the output.
4465 Will never hold tree nodes across GC runs. */
4466static struct init_node *constructor_pending_elts;
d45cf215 4467
3e4093b6
RS
4468/* The SPELLING_DEPTH of this constructor. */
4469static int constructor_depth;
d45cf215 4470
3e4093b6
RS
4471/* DECL node for which an initializer is being read.
4472 0 means we are reading a constructor expression
4473 such as (struct foo) {...}. */
4474static tree constructor_decl;
d45cf215 4475
3e4093b6
RS
4476/* Nonzero if this is an initializer for a top-level decl. */
4477static int constructor_top_level;
d45cf215 4478
3e4093b6
RS
4479/* Nonzero if there were any member designators in this initializer. */
4480static int constructor_designated;
d45cf215 4481
3e4093b6
RS
4482/* Nesting depth of designator list. */
4483static int designator_depth;
d45cf215 4484
3e4093b6
RS
4485/* Nonzero if there were diagnosed errors in this designator list. */
4486static int designator_errorneous;
d45cf215 4487
3e4093b6
RS
4488\f
4489/* This stack has a level for each implicit or explicit level of
4490 structuring in the initializer, including the outermost one. It
4491 saves the values of most of the variables above. */
d45cf215 4492
3e4093b6
RS
4493struct constructor_range_stack;
4494
4495struct constructor_stack
d45cf215 4496{
3e4093b6
RS
4497 struct constructor_stack *next;
4498 tree type;
4499 tree fields;
4500 tree index;
4501 tree max_index;
4502 tree unfilled_index;
4503 tree unfilled_fields;
4504 tree bit_index;
4505 tree elements;
4506 struct init_node *pending_elts;
4507 int offset;
4508 int depth;
916c5919 4509 /* If value nonzero, this value should replace the entire
3e4093b6 4510 constructor at this level. */
916c5919 4511 struct c_expr replacement_value;
3e4093b6
RS
4512 struct constructor_range_stack *range_stack;
4513 char constant;
4514 char simple;
4515 char implicit;
4516 char erroneous;
4517 char outer;
4518 char incremental;
4519 char designated;
4520};
d45cf215 4521
802415d1 4522static struct constructor_stack *constructor_stack;
d45cf215 4523
3e4093b6
RS
4524/* This stack represents designators from some range designator up to
4525 the last designator in the list. */
d45cf215 4526
3e4093b6
RS
4527struct constructor_range_stack
4528{
4529 struct constructor_range_stack *next, *prev;
4530 struct constructor_stack *stack;
4531 tree range_start;
4532 tree index;
4533 tree range_end;
4534 tree fields;
4535};
d45cf215 4536
802415d1 4537static struct constructor_range_stack *constructor_range_stack;
d45cf215 4538
3e4093b6
RS
4539/* This stack records separate initializers that are nested.
4540 Nested initializers can't happen in ANSI C, but GNU C allows them
4541 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 4542
3e4093b6 4543struct initializer_stack
d45cf215 4544{
3e4093b6
RS
4545 struct initializer_stack *next;
4546 tree decl;
3e4093b6
RS
4547 struct constructor_stack *constructor_stack;
4548 struct constructor_range_stack *constructor_range_stack;
4549 tree elements;
4550 struct spelling *spelling;
4551 struct spelling *spelling_base;
4552 int spelling_size;
4553 char top_level;
4554 char require_constant_value;
4555 char require_constant_elements;
4556};
d45cf215 4557
802415d1 4558static struct initializer_stack *initializer_stack;
3e4093b6
RS
4559\f
4560/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
4561
4562void
a396f8ae 4563start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 4564{
3e4093b6 4565 const char *locus;
a396f8ae 4566 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
400fbf9f 4567
3e4093b6 4568 p->decl = constructor_decl;
3e4093b6
RS
4569 p->require_constant_value = require_constant_value;
4570 p->require_constant_elements = require_constant_elements;
4571 p->constructor_stack = constructor_stack;
4572 p->constructor_range_stack = constructor_range_stack;
4573 p->elements = constructor_elements;
4574 p->spelling = spelling;
4575 p->spelling_base = spelling_base;
4576 p->spelling_size = spelling_size;
4577 p->top_level = constructor_top_level;
4578 p->next = initializer_stack;
4579 initializer_stack = p;
400fbf9f 4580
3e4093b6 4581 constructor_decl = decl;
3e4093b6
RS
4582 constructor_designated = 0;
4583 constructor_top_level = top_level;
400fbf9f 4584
6f17bbcf 4585 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
4586 {
4587 require_constant_value = TREE_STATIC (decl);
4588 require_constant_elements
4589 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4590 /* For a scalar, you can always use any value to initialize,
4591 even within braces. */
4592 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4593 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4594 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4595 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4596 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4597 }
4598 else
4599 {
4600 require_constant_value = 0;
4601 require_constant_elements = 0;
4602 locus = "(anonymous)";
4603 }
b71c7f8a 4604
3e4093b6
RS
4605 constructor_stack = 0;
4606 constructor_range_stack = 0;
b71c7f8a 4607
3e4093b6
RS
4608 missing_braces_mentioned = 0;
4609
4610 spelling_base = 0;
4611 spelling_size = 0;
4612 RESTORE_SPELLING_DEPTH (0);
4613
4614 if (locus)
4615 push_string (locus);
4616}
4617
4618void
4619finish_init (void)
b71c7f8a 4620{
3e4093b6 4621 struct initializer_stack *p = initializer_stack;
b71c7f8a 4622
3e4093b6
RS
4623 /* Free the whole constructor stack of this initializer. */
4624 while (constructor_stack)
4625 {
4626 struct constructor_stack *q = constructor_stack;
4627 constructor_stack = q->next;
4628 free (q);
4629 }
4630
366de0ce 4631 gcc_assert (!constructor_range_stack);
3e4093b6
RS
4632
4633 /* Pop back to the data of the outer initializer (if any). */
36579663 4634 free (spelling_base);
3aeb3655 4635
3e4093b6 4636 constructor_decl = p->decl;
3e4093b6
RS
4637 require_constant_value = p->require_constant_value;
4638 require_constant_elements = p->require_constant_elements;
4639 constructor_stack = p->constructor_stack;
4640 constructor_range_stack = p->constructor_range_stack;
4641 constructor_elements = p->elements;
4642 spelling = p->spelling;
4643 spelling_base = p->spelling_base;
4644 spelling_size = p->spelling_size;
4645 constructor_top_level = p->top_level;
4646 initializer_stack = p->next;
4647 free (p);
b71c7f8a 4648}
400fbf9f 4649\f
3e4093b6
RS
4650/* Call here when we see the initializer is surrounded by braces.
4651 This is instead of a call to push_init_level;
4652 it is matched by a call to pop_init_level.
400fbf9f 4653
3e4093b6
RS
4654 TYPE is the type to initialize, for a constructor expression.
4655 For an initializer for a decl, TYPE is zero. */
400fbf9f 4656
3e4093b6
RS
4657void
4658really_start_incremental_init (tree type)
400fbf9f 4659{
5d038c4c 4660 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 4661
3e4093b6
RS
4662 if (type == 0)
4663 type = TREE_TYPE (constructor_decl);
400fbf9f 4664
5fd9b178 4665 if (targetm.vector_opaque_p (type))
3e4093b6 4666 error ("opaque vector types cannot be initialized");
400fbf9f 4667
3e4093b6
RS
4668 p->type = constructor_type;
4669 p->fields = constructor_fields;
4670 p->index = constructor_index;
4671 p->max_index = constructor_max_index;
4672 p->unfilled_index = constructor_unfilled_index;
4673 p->unfilled_fields = constructor_unfilled_fields;
4674 p->bit_index = constructor_bit_index;
4675 p->elements = constructor_elements;
4676 p->constant = constructor_constant;
4677 p->simple = constructor_simple;
4678 p->erroneous = constructor_erroneous;
4679 p->pending_elts = constructor_pending_elts;
4680 p->depth = constructor_depth;
916c5919
JM
4681 p->replacement_value.value = 0;
4682 p->replacement_value.original_code = ERROR_MARK;
3e4093b6
RS
4683 p->implicit = 0;
4684 p->range_stack = 0;
4685 p->outer = 0;
4686 p->incremental = constructor_incremental;
4687 p->designated = constructor_designated;
4688 p->next = 0;
4689 constructor_stack = p;
b13aca19 4690
3e4093b6
RS
4691 constructor_constant = 1;
4692 constructor_simple = 1;
4693 constructor_depth = SPELLING_DEPTH ();
4694 constructor_elements = 0;
4695 constructor_pending_elts = 0;
4696 constructor_type = type;
4697 constructor_incremental = 1;
4698 constructor_designated = 0;
4699 designator_depth = 0;
4700 designator_errorneous = 0;
400fbf9f 4701
3e4093b6
RS
4702 if (TREE_CODE (constructor_type) == RECORD_TYPE
4703 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 4704 {
3e4093b6
RS
4705 constructor_fields = TYPE_FIELDS (constructor_type);
4706 /* Skip any nameless bit fields at the beginning. */
4707 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4708 && DECL_NAME (constructor_fields) == 0)
4709 constructor_fields = TREE_CHAIN (constructor_fields);
05bccae2 4710
3e4093b6
RS
4711 constructor_unfilled_fields = constructor_fields;
4712 constructor_bit_index = bitsize_zero_node;
400fbf9f 4713 }
3e4093b6
RS
4714 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4715 {
4716 if (TYPE_DOMAIN (constructor_type))
4717 {
4718 constructor_max_index
4719 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 4720
3e4093b6
RS
4721 /* Detect non-empty initializations of zero-length arrays. */
4722 if (constructor_max_index == NULL_TREE
4723 && TYPE_SIZE (constructor_type))
7d60be94 4724 constructor_max_index = build_int_cst (NULL_TREE, -1);
400fbf9f 4725
3e4093b6
RS
4726 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4727 to initialize VLAs will cause a proper error; avoid tree
4728 checking errors as well by setting a safe value. */
4729 if (constructor_max_index
4730 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7d60be94 4731 constructor_max_index = build_int_cst (NULL_TREE, -1);
59c83dbf 4732
3e4093b6
RS
4733 constructor_index
4734 = convert (bitsizetype,
4735 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 4736 }
3e4093b6 4737 else
493179da
JM
4738 {
4739 constructor_index = bitsize_zero_node;
4740 constructor_max_index = NULL_TREE;
4741 }
59c83dbf 4742
3e4093b6
RS
4743 constructor_unfilled_index = constructor_index;
4744 }
4745 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4746 {
4747 /* Vectors are like simple fixed-size arrays. */
4748 constructor_max_index =
7d60be94 4749 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
3e4093b6
RS
4750 constructor_index = convert (bitsizetype, bitsize_zero_node);
4751 constructor_unfilled_index = constructor_index;
4752 }
4753 else
4754 {
4755 /* Handle the case of int x = {5}; */
4756 constructor_fields = constructor_type;
4757 constructor_unfilled_fields = constructor_type;
4758 }
4759}
4760\f
4761/* Push down into a subobject, for initialization.
4762 If this is for an explicit set of braces, IMPLICIT is 0.
4763 If it is because the next element belongs at a lower level,
4764 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 4765
3e4093b6
RS
4766void
4767push_init_level (int implicit)
4768{
4769 struct constructor_stack *p;
4770 tree value = NULL_TREE;
400fbf9f 4771
3e4093b6
RS
4772 /* If we've exhausted any levels that didn't have braces,
4773 pop them now. */
4774 while (constructor_stack->implicit)
4775 {
4776 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4777 || TREE_CODE (constructor_type) == UNION_TYPE)
4778 && constructor_fields == 0)
4779 process_init_element (pop_init_level (1));
4780 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4781 && constructor_max_index
4782 && tree_int_cst_lt (constructor_max_index, constructor_index))
4783 process_init_element (pop_init_level (1));
4784 else
4785 break;
4786 }
400fbf9f 4787
3e4093b6
RS
4788 /* Unless this is an explicit brace, we need to preserve previous
4789 content if any. */
4790 if (implicit)
4791 {
4792 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4793 || TREE_CODE (constructor_type) == UNION_TYPE)
4794 && constructor_fields)
4795 value = find_init_member (constructor_fields);
4796 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4797 value = find_init_member (constructor_index);
400fbf9f
JW
4798 }
4799
5d038c4c 4800 p = XNEW (struct constructor_stack);
3e4093b6
RS
4801 p->type = constructor_type;
4802 p->fields = constructor_fields;
4803 p->index = constructor_index;
4804 p->max_index = constructor_max_index;
4805 p->unfilled_index = constructor_unfilled_index;
4806 p->unfilled_fields = constructor_unfilled_fields;
4807 p->bit_index = constructor_bit_index;
4808 p->elements = constructor_elements;
4809 p->constant = constructor_constant;
4810 p->simple = constructor_simple;
4811 p->erroneous = constructor_erroneous;
4812 p->pending_elts = constructor_pending_elts;
4813 p->depth = constructor_depth;
916c5919
JM
4814 p->replacement_value.value = 0;
4815 p->replacement_value.original_code = ERROR_MARK;
3e4093b6
RS
4816 p->implicit = implicit;
4817 p->outer = 0;
4818 p->incremental = constructor_incremental;
4819 p->designated = constructor_designated;
4820 p->next = constructor_stack;
4821 p->range_stack = 0;
4822 constructor_stack = p;
400fbf9f 4823
3e4093b6
RS
4824 constructor_constant = 1;
4825 constructor_simple = 1;
4826 constructor_depth = SPELLING_DEPTH ();
4827 constructor_elements = 0;
4828 constructor_incremental = 1;
4829 constructor_designated = 0;
4830 constructor_pending_elts = 0;
4831 if (!implicit)
400fbf9f 4832 {
3e4093b6
RS
4833 p->range_stack = constructor_range_stack;
4834 constructor_range_stack = 0;
4835 designator_depth = 0;
4836 designator_errorneous = 0;
4837 }
400fbf9f 4838
3e4093b6
RS
4839 /* Don't die if an entire brace-pair level is superfluous
4840 in the containing level. */
4841 if (constructor_type == 0)
4842 ;
4843 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4844 || TREE_CODE (constructor_type) == UNION_TYPE)
4845 {
4846 /* Don't die if there are extra init elts at the end. */
4847 if (constructor_fields == 0)
4848 constructor_type = 0;
4849 else
400fbf9f 4850 {
3e4093b6
RS
4851 constructor_type = TREE_TYPE (constructor_fields);
4852 push_member_name (constructor_fields);
4853 constructor_depth++;
400fbf9f 4854 }
3e4093b6
RS
4855 }
4856 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4857 {
4858 constructor_type = TREE_TYPE (constructor_type);
4859 push_array_bounds (tree_low_cst (constructor_index, 0));
4860 constructor_depth++;
400fbf9f
JW
4861 }
4862
3e4093b6 4863 if (constructor_type == 0)
400fbf9f 4864 {
3e4093b6
RS
4865 error_init ("extra brace group at end of initializer");
4866 constructor_fields = 0;
4867 constructor_unfilled_fields = 0;
4868 return;
400fbf9f
JW
4869 }
4870
3e4093b6
RS
4871 if (value && TREE_CODE (value) == CONSTRUCTOR)
4872 {
4873 constructor_constant = TREE_CONSTANT (value);
4874 constructor_simple = TREE_STATIC (value);
4875 constructor_elements = CONSTRUCTOR_ELTS (value);
4876 if (constructor_elements
4877 && (TREE_CODE (constructor_type) == RECORD_TYPE
4878 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4879 set_nonincremental_init ();
4880 }
400fbf9f 4881
3e4093b6
RS
4882 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4883 {
4884 missing_braces_mentioned = 1;
4885 warning_init ("missing braces around initializer");
4886 }
400fbf9f 4887
3e4093b6
RS
4888 if (TREE_CODE (constructor_type) == RECORD_TYPE
4889 || TREE_CODE (constructor_type) == UNION_TYPE)
4890 {
4891 constructor_fields = TYPE_FIELDS (constructor_type);
4892 /* Skip any nameless bit fields at the beginning. */
4893 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4894 && DECL_NAME (constructor_fields) == 0)
4895 constructor_fields = TREE_CHAIN (constructor_fields);
103b7b17 4896
3e4093b6
RS
4897 constructor_unfilled_fields = constructor_fields;
4898 constructor_bit_index = bitsize_zero_node;
4899 }
4900 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4901 {
4902 /* Vectors are like simple fixed-size arrays. */
4903 constructor_max_index =
7d60be94 4904 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
3e4093b6
RS
4905 constructor_index = convert (bitsizetype, integer_zero_node);
4906 constructor_unfilled_index = constructor_index;
4907 }
4908 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4909 {
4910 if (TYPE_DOMAIN (constructor_type))
4911 {
4912 constructor_max_index
4913 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 4914
3e4093b6
RS
4915 /* Detect non-empty initializations of zero-length arrays. */
4916 if (constructor_max_index == NULL_TREE
4917 && TYPE_SIZE (constructor_type))
7d60be94 4918 constructor_max_index = build_int_cst (NULL_TREE, -1);
de520661 4919
3e4093b6
RS
4920 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4921 to initialize VLAs will cause a proper error; avoid tree
4922 checking errors as well by setting a safe value. */
4923 if (constructor_max_index
4924 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7d60be94 4925 constructor_max_index = build_int_cst (NULL_TREE, -1);
b62acd60 4926
3e4093b6
RS
4927 constructor_index
4928 = convert (bitsizetype,
4929 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4930 }
4931 else
4932 constructor_index = bitsize_zero_node;
de520661 4933
3e4093b6
RS
4934 constructor_unfilled_index = constructor_index;
4935 if (value && TREE_CODE (value) == STRING_CST)
4936 {
4937 /* We need to split the char/wchar array into individual
4938 characters, so that we don't have to special case it
4939 everywhere. */
4940 set_nonincremental_init_from_string (value);
4941 }
4942 }
4943 else
4944 {
b4519d39
SB
4945 if (constructor_type != error_mark_node)
4946 warning_init ("braces around scalar initializer");
3e4093b6
RS
4947 constructor_fields = constructor_type;
4948 constructor_unfilled_fields = constructor_type;
4949 }
4950}
8b6a5902 4951
3e4093b6 4952/* At the end of an implicit or explicit brace level,
916c5919
JM
4953 finish up that level of constructor. If a single expression
4954 with redundant braces initialized that level, return the
4955 c_expr structure for that expression. Otherwise, the original_code
4956 element is set to ERROR_MARK.
4957 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 4958 from inner levels (process_init_element ignores that),
916c5919 4959 but return error_mark_node as the value from the outermost level
3e4093b6 4960 (that's what we want to put in DECL_INITIAL).
916c5919 4961 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 4962
916c5919 4963struct c_expr
3e4093b6
RS
4964pop_init_level (int implicit)
4965{
4966 struct constructor_stack *p;
916c5919
JM
4967 struct c_expr ret;
4968 ret.value = 0;
4969 ret.original_code = ERROR_MARK;
de520661 4970
3e4093b6
RS
4971 if (implicit == 0)
4972 {
4973 /* When we come to an explicit close brace,
4974 pop any inner levels that didn't have explicit braces. */
4975 while (constructor_stack->implicit)
4976 process_init_element (pop_init_level (1));
de520661 4977
366de0ce 4978 gcc_assert (!constructor_range_stack);
3e4093b6 4979 }
e5e809f4 4980
0066ef9c
RH
4981 /* Now output all pending elements. */
4982 constructor_incremental = 1;
4983 output_pending_init_elements (1);
4984
3e4093b6 4985 p = constructor_stack;
e5e809f4 4986
3e4093b6
RS
4987 /* Error for initializing a flexible array member, or a zero-length
4988 array member in an inappropriate context. */
4989 if (constructor_type && constructor_fields
4990 && TREE_CODE (constructor_type) == ARRAY_TYPE
4991 && TYPE_DOMAIN (constructor_type)
3f75a254 4992 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
4993 {
4994 /* Silently discard empty initializations. The parser will
4995 already have pedwarned for empty brackets. */
4996 if (integer_zerop (constructor_unfilled_index))
4997 constructor_type = NULL_TREE;
366de0ce 4998 else
3e4093b6 4999 {
366de0ce
NS
5000 gcc_assert (!TYPE_SIZE (constructor_type));
5001
3e4093b6
RS
5002 if (constructor_depth > 2)
5003 error_init ("initialization of flexible array member in a nested context");
5004 else if (pedantic)
5005 pedwarn_init ("initialization of a flexible array member");
de520661 5006
3e4093b6
RS
5007 /* We have already issued an error message for the existence
5008 of a flexible array member not at the end of the structure.
5009 Discard the initializer so that we do not abort later. */
5010 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5011 constructor_type = NULL_TREE;
5012 }
3e4093b6 5013 }
de520661 5014
3e4093b6 5015 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 5016 if (warn_missing_field_initializers
3e4093b6
RS
5017 && constructor_type
5018 && TREE_CODE (constructor_type) == RECORD_TYPE
5019 && constructor_unfilled_fields)
5020 {
5021 /* Do not warn for flexible array members or zero-length arrays. */
5022 while (constructor_unfilled_fields
3f75a254 5023 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6
RS
5024 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5025 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
cc77d4d5 5026
3e4093b6
RS
5027 /* Do not warn if this level of the initializer uses member
5028 designators; it is likely to be deliberate. */
5029 if (constructor_unfilled_fields && !constructor_designated)
5030 {
5031 push_member_name (constructor_unfilled_fields);
5032 warning_init ("missing initializer");
5033 RESTORE_SPELLING_DEPTH (constructor_depth);
5034 }
5035 }
de520661 5036
3e4093b6 5037 /* Pad out the end of the structure. */
916c5919 5038 if (p->replacement_value.value)
3e4093b6
RS
5039 /* If this closes a superfluous brace pair,
5040 just pass out the element between them. */
916c5919 5041 ret = p->replacement_value;
3e4093b6
RS
5042 else if (constructor_type == 0)
5043 ;
5044 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5045 && TREE_CODE (constructor_type) != UNION_TYPE
5046 && TREE_CODE (constructor_type) != ARRAY_TYPE
5047 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5048 {
5049 /* A nonincremental scalar initializer--just return
5050 the element, after verifying there is just one. */
5051 if (constructor_elements == 0)
5052 {
5053 if (!constructor_erroneous)
5054 error_init ("empty scalar initializer");
916c5919 5055 ret.value = error_mark_node;
3e4093b6
RS
5056 }
5057 else if (TREE_CHAIN (constructor_elements) != 0)
5058 {
5059 error_init ("extra elements in scalar initializer");
916c5919 5060 ret.value = TREE_VALUE (constructor_elements);
3e4093b6
RS
5061 }
5062 else
916c5919 5063 ret.value = TREE_VALUE (constructor_elements);
3e4093b6
RS
5064 }
5065 else
5066 {
5067 if (constructor_erroneous)
916c5919 5068 ret.value = error_mark_node;
3e4093b6
RS
5069 else
5070 {
916c5919
JM
5071 ret.value = build_constructor (constructor_type,
5072 nreverse (constructor_elements));
3e4093b6 5073 if (constructor_constant)
916c5919 5074 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
3e4093b6 5075 if (constructor_constant && constructor_simple)
916c5919 5076 TREE_STATIC (ret.value) = 1;
3e4093b6
RS
5077 }
5078 }
de520661 5079
3e4093b6
RS
5080 constructor_type = p->type;
5081 constructor_fields = p->fields;
5082 constructor_index = p->index;
5083 constructor_max_index = p->max_index;
5084 constructor_unfilled_index = p->unfilled_index;
5085 constructor_unfilled_fields = p->unfilled_fields;
5086 constructor_bit_index = p->bit_index;
5087 constructor_elements = p->elements;
5088 constructor_constant = p->constant;
5089 constructor_simple = p->simple;
5090 constructor_erroneous = p->erroneous;
5091 constructor_incremental = p->incremental;
5092 constructor_designated = p->designated;
5093 constructor_pending_elts = p->pending_elts;
5094 constructor_depth = p->depth;
5095 if (!p->implicit)
5096 constructor_range_stack = p->range_stack;
5097 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 5098
3e4093b6
RS
5099 constructor_stack = p->next;
5100 free (p);
b621a4dd 5101
916c5919 5102 if (ret.value == 0)
3e4093b6
RS
5103 {
5104 if (constructor_stack == 0)
916c5919
JM
5105 {
5106 ret.value = error_mark_node;
5107 return ret;
5108 }
5109 return ret;
3e4093b6 5110 }
916c5919 5111 return ret;
3e4093b6 5112}
8b6a5902 5113
3e4093b6
RS
5114/* Common handling for both array range and field name designators.
5115 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 5116
3e4093b6
RS
5117static int
5118set_designator (int array)
de520661 5119{
3e4093b6
RS
5120 tree subtype;
5121 enum tree_code subcode;
de520661 5122
3e4093b6
RS
5123 /* Don't die if an entire brace-pair level is superfluous
5124 in the containing level. */
5125 if (constructor_type == 0)
5126 return 1;
de520661 5127
366de0ce
NS
5128 /* If there were errors in this designator list already, bail out
5129 silently. */
3e4093b6
RS
5130 if (designator_errorneous)
5131 return 1;
e28cae4f 5132
3e4093b6
RS
5133 if (!designator_depth)
5134 {
366de0ce 5135 gcc_assert (!constructor_range_stack);
de520661 5136
3e4093b6
RS
5137 /* Designator list starts at the level of closest explicit
5138 braces. */
5139 while (constructor_stack->implicit)
5140 process_init_element (pop_init_level (1));
5141 constructor_designated = 1;
5142 return 0;
5143 }
de520661 5144
366de0ce 5145 switch (TREE_CODE (constructor_type))
3c3fa147 5146 {
366de0ce
NS
5147 case RECORD_TYPE:
5148 case UNION_TYPE:
3e4093b6
RS
5149 subtype = TREE_TYPE (constructor_fields);
5150 if (subtype != error_mark_node)
5151 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
5152 break;
5153 case ARRAY_TYPE:
3e4093b6 5154 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
5155 break;
5156 default:
5157 gcc_unreachable ();
de520661 5158 }
400fbf9f 5159
3e4093b6
RS
5160 subcode = TREE_CODE (subtype);
5161 if (array && subcode != ARRAY_TYPE)
5162 {
5163 error_init ("array index in non-array initializer");
5164 return 1;
5165 }
5166 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5167 {
5168 error_init ("field name not in record or union initializer");
5169 return 1;
5170 }
d45cf215 5171
3e4093b6
RS
5172 constructor_designated = 1;
5173 push_init_level (2);
5174 return 0;
de520661 5175}
400fbf9f 5176
3e4093b6
RS
5177/* If there are range designators in designator list, push a new designator
5178 to constructor_range_stack. RANGE_END is end of such stack range or
5179 NULL_TREE if there is no range designator at this level. */
400fbf9f 5180
3e4093b6
RS
5181static void
5182push_range_stack (tree range_end)
5183{
5184 struct constructor_range_stack *p;
400fbf9f 5185
5d038c4c 5186 p = GGC_NEW (struct constructor_range_stack);
3e4093b6
RS
5187 p->prev = constructor_range_stack;
5188 p->next = 0;
5189 p->fields = constructor_fields;
5190 p->range_start = constructor_index;
5191 p->index = constructor_index;
5192 p->stack = constructor_stack;
5193 p->range_end = range_end;
8b6a5902 5194 if (constructor_range_stack)
3e4093b6
RS
5195 constructor_range_stack->next = p;
5196 constructor_range_stack = p;
de520661 5197}
400fbf9f 5198
3e4093b6
RS
5199/* Within an array initializer, specify the next index to be initialized.
5200 FIRST is that index. If LAST is nonzero, then initialize a range
5201 of indices, running from FIRST through LAST. */
5a7ec9d9 5202
de520661 5203void
3e4093b6 5204set_init_index (tree first, tree last)
de520661 5205{
3e4093b6
RS
5206 if (set_designator (1))
5207 return;
de520661 5208
3e4093b6 5209 designator_errorneous = 1;
de520661 5210
3ea8cd06
JM
5211 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5212 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5213 {
5214 error_init ("array index in initializer not of integer type");
5215 return;
5216 }
5217
3e4093b6
RS
5218 if (TREE_CODE (first) != INTEGER_CST)
5219 error_init ("nonconstant array index in initializer");
5220 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5221 error_init ("nonconstant array index in initializer");
5222 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5223 error_init ("array index in non-array initializer");
622adc7e
MK
5224 else if (tree_int_cst_sgn (first) == -1)
5225 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
5226 else if (constructor_max_index
5227 && tree_int_cst_lt (constructor_max_index, first))
5228 error_init ("array index in initializer exceeds array bounds");
5229 else
de520661 5230 {
3e4093b6 5231 constructor_index = convert (bitsizetype, first);
665f2503 5232
3e4093b6 5233 if (last)
2bede729 5234 {
3e4093b6
RS
5235 if (tree_int_cst_equal (first, last))
5236 last = 0;
5237 else if (tree_int_cst_lt (last, first))
5238 {
5239 error_init ("empty index range in initializer");
5240 last = 0;
5241 }
5242 else
5243 {
5244 last = convert (bitsizetype, last);
5245 if (constructor_max_index != 0
5246 && tree_int_cst_lt (constructor_max_index, last))
5247 {
5248 error_init ("array index range in initializer exceeds array bounds");
5249 last = 0;
5250 }
5251 }
2bede729 5252 }
fed3cef0 5253
3e4093b6
RS
5254 designator_depth++;
5255 designator_errorneous = 0;
5256 if (constructor_range_stack || last)
5257 push_range_stack (last);
de520661 5258 }
de520661 5259}
3e4093b6
RS
5260
5261/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 5262
de520661 5263void
3e4093b6 5264set_init_label (tree fieldname)
de520661 5265{
3e4093b6 5266 tree tail;
94ba5069 5267
3e4093b6
RS
5268 if (set_designator (0))
5269 return;
5270
5271 designator_errorneous = 1;
5272
5273 if (TREE_CODE (constructor_type) != RECORD_TYPE
5274 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 5275 {
3e4093b6
RS
5276 error_init ("field name not in record or union initializer");
5277 return;
94ba5069
RS
5278 }
5279
3e4093b6
RS
5280 for (tail = TYPE_FIELDS (constructor_type); tail;
5281 tail = TREE_CHAIN (tail))
8b6a5902 5282 {
3e4093b6
RS
5283 if (DECL_NAME (tail) == fieldname)
5284 break;
8b6a5902
JJ
5285 }
5286
3e4093b6 5287 if (tail == 0)
c51a1ba9 5288 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 5289 else
8b6a5902 5290 {
3e4093b6
RS
5291 constructor_fields = tail;
5292 designator_depth++;
8b6a5902 5293 designator_errorneous = 0;
3e4093b6
RS
5294 if (constructor_range_stack)
5295 push_range_stack (NULL_TREE);
8b6a5902 5296 }
3e4093b6
RS
5297}
5298\f
5299/* Add a new initializer to the tree of pending initializers. PURPOSE
5300 identifies the initializer, either array index or field in a structure.
5301 VALUE is the value of that index or field. */
de520661 5302
3e4093b6
RS
5303static void
5304add_pending_init (tree purpose, tree value)
5305{
5306 struct init_node *p, **q, *r;
5307
5308 q = &constructor_pending_elts;
5309 p = 0;
5310
5311 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 5312 {
3e4093b6 5313 while (*q != 0)
91fa3c30 5314 {
3e4093b6
RS
5315 p = *q;
5316 if (tree_int_cst_lt (purpose, p->purpose))
5317 q = &p->left;
5318 else if (tree_int_cst_lt (p->purpose, purpose))
5319 q = &p->right;
5320 else
5321 {
5322 if (TREE_SIDE_EFFECTS (p->value))
5323 warning_init ("initialized field with side-effects overwritten");
5324 p->value = value;
5325 return;
5326 }
91fa3c30 5327 }
de520661 5328 }
3e4093b6 5329 else
de520661 5330 {
3e4093b6 5331 tree bitpos;
400fbf9f 5332
3e4093b6
RS
5333 bitpos = bit_position (purpose);
5334 while (*q != NULL)
5335 {
5336 p = *q;
5337 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5338 q = &p->left;
5339 else if (p->purpose != purpose)
5340 q = &p->right;
5341 else
5342 {
5343 if (TREE_SIDE_EFFECTS (p->value))
5344 warning_init ("initialized field with side-effects overwritten");
5345 p->value = value;
5346 return;
5347 }
5348 }
91fa3c30 5349 }
b71c7f8a 5350
5d038c4c 5351 r = GGC_NEW (struct init_node);
3e4093b6
RS
5352 r->purpose = purpose;
5353 r->value = value;
8b6a5902 5354
3e4093b6
RS
5355 *q = r;
5356 r->parent = p;
5357 r->left = 0;
5358 r->right = 0;
5359 r->balance = 0;
b71c7f8a 5360
3e4093b6 5361 while (p)
de520661 5362 {
3e4093b6 5363 struct init_node *s;
665f2503 5364
3e4093b6 5365 if (r == p->left)
2bede729 5366 {
3e4093b6
RS
5367 if (p->balance == 0)
5368 p->balance = -1;
5369 else if (p->balance < 0)
5370 {
5371 if (r->balance < 0)
5372 {
5373 /* L rotation. */
5374 p->left = r->right;
5375 if (p->left)
5376 p->left->parent = p;
5377 r->right = p;
e7b6a0ee 5378
3e4093b6
RS
5379 p->balance = 0;
5380 r->balance = 0;
39bc99c2 5381
3e4093b6
RS
5382 s = p->parent;
5383 p->parent = r;
5384 r->parent = s;
5385 if (s)
5386 {
5387 if (s->left == p)
5388 s->left = r;
5389 else
5390 s->right = r;
5391 }
5392 else
5393 constructor_pending_elts = r;
5394 }
5395 else
5396 {
5397 /* LR rotation. */
5398 struct init_node *t = r->right;
e7b6a0ee 5399
3e4093b6
RS
5400 r->right = t->left;
5401 if (r->right)
5402 r->right->parent = r;
5403 t->left = r;
5404
5405 p->left = t->right;
5406 if (p->left)
5407 p->left->parent = p;
5408 t->right = p;
5409
5410 p->balance = t->balance < 0;
5411 r->balance = -(t->balance > 0);
5412 t->balance = 0;
5413
5414 s = p->parent;
5415 p->parent = t;
5416 r->parent = t;
5417 t->parent = s;
5418 if (s)
5419 {
5420 if (s->left == p)
5421 s->left = t;
5422 else
5423 s->right = t;
5424 }
5425 else
5426 constructor_pending_elts = t;
5427 }
5428 break;
5429 }
5430 else
5431 {
5432 /* p->balance == +1; growth of left side balances the node. */
5433 p->balance = 0;
5434 break;
5435 }
2bede729 5436 }
3e4093b6
RS
5437 else /* r == p->right */
5438 {
5439 if (p->balance == 0)
5440 /* Growth propagation from right side. */
5441 p->balance++;
5442 else if (p->balance > 0)
5443 {
5444 if (r->balance > 0)
5445 {
5446 /* R rotation. */
5447 p->right = r->left;
5448 if (p->right)
5449 p->right->parent = p;
5450 r->left = p;
5451
5452 p->balance = 0;
5453 r->balance = 0;
5454
5455 s = p->parent;
5456 p->parent = r;
5457 r->parent = s;
5458 if (s)
5459 {
5460 if (s->left == p)
5461 s->left = r;
5462 else
5463 s->right = r;
5464 }
5465 else
5466 constructor_pending_elts = r;
5467 }
5468 else /* r->balance == -1 */
5469 {
5470 /* RL rotation */
5471 struct init_node *t = r->left;
5472
5473 r->left = t->right;
5474 if (r->left)
5475 r->left->parent = r;
5476 t->right = r;
5477
5478 p->right = t->left;
5479 if (p->right)
5480 p->right->parent = p;
5481 t->left = p;
5482
5483 r->balance = (t->balance < 0);
5484 p->balance = -(t->balance > 0);
5485 t->balance = 0;
5486
5487 s = p->parent;
5488 p->parent = t;
5489 r->parent = t;
5490 t->parent = s;
5491 if (s)
5492 {
5493 if (s->left == p)
5494 s->left = t;
5495 else
5496 s->right = t;
5497 }
5498 else
5499 constructor_pending_elts = t;
5500 }
5501 break;
5502 }
5503 else
5504 {
5505 /* p->balance == -1; growth of right side balances the node. */
5506 p->balance = 0;
5507 break;
5508 }
5509 }
5510
5511 r = p;
5512 p = p->parent;
5513 }
5514}
5515
5516/* Build AVL tree from a sorted chain. */
5517
5518static void
5519set_nonincremental_init (void)
5520{
5521 tree chain;
5522
5523 if (TREE_CODE (constructor_type) != RECORD_TYPE
5524 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5525 return;
5526
5527 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5528 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5529 constructor_elements = 0;
5530 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5531 {
5532 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5533 /* Skip any nameless bit fields at the beginning. */
5534 while (constructor_unfilled_fields != 0
5535 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5536 && DECL_NAME (constructor_unfilled_fields) == 0)
5537 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 5538
de520661 5539 }
3e4093b6 5540 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 5541 {
3e4093b6
RS
5542 if (TYPE_DOMAIN (constructor_type))
5543 constructor_unfilled_index
5544 = convert (bitsizetype,
5545 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5546 else
5547 constructor_unfilled_index = bitsize_zero_node;
de520661 5548 }
3e4093b6 5549 constructor_incremental = 0;
de520661 5550}
400fbf9f 5551
3e4093b6 5552/* Build AVL tree from a string constant. */
de520661 5553
3e4093b6
RS
5554static void
5555set_nonincremental_init_from_string (tree str)
de520661 5556{
3e4093b6
RS
5557 tree value, purpose, type;
5558 HOST_WIDE_INT val[2];
5559 const char *p, *end;
5560 int byte, wchar_bytes, charwidth, bitpos;
de520661 5561
366de0ce 5562 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 5563
3e4093b6
RS
5564 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5565 == TYPE_PRECISION (char_type_node))
5566 wchar_bytes = 1;
3e4093b6 5567 else
366de0ce
NS
5568 {
5569 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5570 == TYPE_PRECISION (wchar_type_node));
5571 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5572 }
3e4093b6
RS
5573 charwidth = TYPE_PRECISION (char_type_node);
5574 type = TREE_TYPE (constructor_type);
5575 p = TREE_STRING_POINTER (str);
5576 end = p + TREE_STRING_LENGTH (str);
91fa3c30 5577
3e4093b6
RS
5578 for (purpose = bitsize_zero_node;
5579 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5580 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 5581 {
3e4093b6 5582 if (wchar_bytes == 1)
ffc5c6a9 5583 {
3e4093b6
RS
5584 val[1] = (unsigned char) *p++;
5585 val[0] = 0;
ffc5c6a9
RH
5586 }
5587 else
3e4093b6
RS
5588 {
5589 val[0] = 0;
5590 val[1] = 0;
5591 for (byte = 0; byte < wchar_bytes; byte++)
5592 {
5593 if (BYTES_BIG_ENDIAN)
5594 bitpos = (wchar_bytes - byte - 1) * charwidth;
5595 else
5596 bitpos = byte * charwidth;
5597 val[bitpos < HOST_BITS_PER_WIDE_INT]
5598 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5599 << (bitpos % HOST_BITS_PER_WIDE_INT);
5600 }
5601 }
584ef5fe 5602
8df83eae 5603 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
5604 {
5605 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5606 if (bitpos < HOST_BITS_PER_WIDE_INT)
5607 {
5608 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5609 {
5610 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5611 val[0] = -1;
5612 }
5613 }
5614 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5615 {
5616 if (val[1] < 0)
5617 val[0] = -1;
5618 }
5619 else if (val[0] & (((HOST_WIDE_INT) 1)
5620 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5621 val[0] |= ((HOST_WIDE_INT) -1)
5622 << (bitpos - HOST_BITS_PER_WIDE_INT);
5623 }
ffc5c6a9 5624
7d60be94 5625 value = build_int_cst_wide (type, val[1], val[0]);
3e4093b6 5626 add_pending_init (purpose, value);
9dfcc8db
BH
5627 }
5628
3e4093b6
RS
5629 constructor_incremental = 0;
5630}
de520661 5631
3e4093b6
RS
5632/* Return value of FIELD in pending initializer or zero if the field was
5633 not initialized yet. */
5634
5635static tree
5636find_init_member (tree field)
5637{
5638 struct init_node *p;
5639
5640 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 5641 {
3e4093b6
RS
5642 if (constructor_incremental
5643 && tree_int_cst_lt (field, constructor_unfilled_index))
5644 set_nonincremental_init ();
5645
5646 p = constructor_pending_elts;
5647 while (p)
19d76e60 5648 {
3e4093b6
RS
5649 if (tree_int_cst_lt (field, p->purpose))
5650 p = p->left;
5651 else if (tree_int_cst_lt (p->purpose, field))
5652 p = p->right;
5653 else
5654 return p->value;
19d76e60 5655 }
19d76e60 5656 }
3e4093b6 5657 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 5658 {
3e4093b6 5659 tree bitpos = bit_position (field);
de520661 5660
3e4093b6
RS
5661 if (constructor_incremental
5662 && (!constructor_unfilled_fields
5663 || tree_int_cst_lt (bitpos,
5664 bit_position (constructor_unfilled_fields))))
5665 set_nonincremental_init ();
de520661 5666
3e4093b6
RS
5667 p = constructor_pending_elts;
5668 while (p)
5669 {
5670 if (field == p->purpose)
5671 return p->value;
5672 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5673 p = p->left;
5674 else
5675 p = p->right;
5676 }
5677 }
5678 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 5679 {
3e4093b6
RS
5680 if (constructor_elements
5681 && TREE_PURPOSE (constructor_elements) == field)
5682 return TREE_VALUE (constructor_elements);
de520661 5683 }
3e4093b6 5684 return 0;
de520661
RS
5685}
5686
3e4093b6
RS
5687/* "Output" the next constructor element.
5688 At top level, really output it to assembler code now.
5689 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5690 TYPE is the data type that the containing data type wants here.
5691 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
5692 If VALUE is a string constant, STRICT_STRING is true if it is
5693 unparenthesized or we should not warn here for it being parenthesized.
5694 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 5695
3e4093b6
RS
5696 PENDING if non-nil means output pending elements that belong
5697 right after this element. (PENDING is normally 1;
5698 it is 0 while outputting pending elements, to avoid recursion.) */
8b6a5902 5699
3e4093b6 5700static void
916c5919
JM
5701output_init_element (tree value, bool strict_string, tree type, tree field,
5702 int pending)
3e4093b6 5703{
0a880880 5704 if (type == error_mark_node || value == error_mark_node)
8b6a5902 5705 {
3e4093b6
RS
5706 constructor_erroneous = 1;
5707 return;
8b6a5902 5708 }
3e4093b6
RS
5709 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5710 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5711 && !(TREE_CODE (value) == STRING_CST
5712 && TREE_CODE (type) == ARRAY_TYPE
197463ae 5713 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
3e4093b6 5714 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
132da1a5 5715 TYPE_MAIN_VARIANT (type))))
3e4093b6 5716 value = default_conversion (value);
8b6a5902 5717
3e4093b6
RS
5718 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5719 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 5720 {
3e4093b6
RS
5721 /* As an extension, allow initializing objects with static storage
5722 duration with compound literals (which are then treated just as
5723 the brace enclosed list they contain). */
5724 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5725 value = DECL_INITIAL (decl);
8b6a5902
JJ
5726 }
5727
3e4093b6
RS
5728 if (value == error_mark_node)
5729 constructor_erroneous = 1;
5730 else if (!TREE_CONSTANT (value))
5731 constructor_constant = 0;
116df786 5732 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
5733 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5734 || TREE_CODE (constructor_type) == UNION_TYPE)
5735 && DECL_C_BIT_FIELD (field)
5736 && TREE_CODE (value) != INTEGER_CST))
5737 constructor_simple = 0;
5738
116df786 5739 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 5740 {
116df786
RH
5741 if (require_constant_value)
5742 {
5743 error_init ("initializer element is not constant");
5744 value = error_mark_node;
5745 }
5746 else if (require_constant_elements)
5747 pedwarn ("initializer element is not computable at load time");
8b6a5902 5748 }
3e4093b6
RS
5749
5750 /* If this field is empty (and not at the end of structure),
5751 don't do anything other than checking the initializer. */
5752 if (field
5753 && (TREE_TYPE (field) == error_mark_node
5754 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5755 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5756 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5757 || TREE_CHAIN (field)))))
5758 return;
5759
916c5919 5760 value = digest_init (type, value, strict_string, require_constant_value);
3e4093b6 5761 if (value == error_mark_node)
8b6a5902 5762 {
3e4093b6
RS
5763 constructor_erroneous = 1;
5764 return;
8b6a5902 5765 }
8b6a5902 5766
3e4093b6
RS
5767 /* If this element doesn't come next in sequence,
5768 put it on constructor_pending_elts. */
5769 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5770 && (!constructor_incremental
5771 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 5772 {
3e4093b6
RS
5773 if (constructor_incremental
5774 && tree_int_cst_lt (field, constructor_unfilled_index))
5775 set_nonincremental_init ();
5776
5777 add_pending_init (field, value);
5778 return;
8b6a5902 5779 }
3e4093b6
RS
5780 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5781 && (!constructor_incremental
5782 || field != constructor_unfilled_fields))
8b6a5902 5783 {
3e4093b6
RS
5784 /* We do this for records but not for unions. In a union,
5785 no matter which field is specified, it can be initialized
5786 right away since it starts at the beginning of the union. */
5787 if (constructor_incremental)
5788 {
5789 if (!constructor_unfilled_fields)
5790 set_nonincremental_init ();
5791 else
5792 {
5793 tree bitpos, unfillpos;
5794
5795 bitpos = bit_position (field);
5796 unfillpos = bit_position (constructor_unfilled_fields);
5797
5798 if (tree_int_cst_lt (bitpos, unfillpos))
5799 set_nonincremental_init ();
5800 }
5801 }
5802
5803 add_pending_init (field, value);
5804 return;
8b6a5902 5805 }
3e4093b6
RS
5806 else if (TREE_CODE (constructor_type) == UNION_TYPE
5807 && constructor_elements)
5808 {
5809 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5810 warning_init ("initialized field with side-effects overwritten");
8b6a5902 5811
3e4093b6
RS
5812 /* We can have just one union field set. */
5813 constructor_elements = 0;
5814 }
8b6a5902 5815
3e4093b6
RS
5816 /* Otherwise, output this element either to
5817 constructor_elements or to the assembler file. */
8b6a5902 5818
3e4093b6
RS
5819 if (field && TREE_CODE (field) == INTEGER_CST)
5820 field = copy_node (field);
5821 constructor_elements
5822 = tree_cons (field, value, constructor_elements);
8b6a5902 5823
3e4093b6
RS
5824 /* Advance the variable that indicates sequential elements output. */
5825 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5826 constructor_unfilled_index
5827 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5828 bitsize_one_node);
5829 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5830 {
5831 constructor_unfilled_fields
5832 = TREE_CHAIN (constructor_unfilled_fields);
8b6a5902 5833
3e4093b6
RS
5834 /* Skip any nameless bit fields. */
5835 while (constructor_unfilled_fields != 0
5836 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5837 && DECL_NAME (constructor_unfilled_fields) == 0)
5838 constructor_unfilled_fields =
5839 TREE_CHAIN (constructor_unfilled_fields);
5840 }
5841 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5842 constructor_unfilled_fields = 0;
de520661 5843
3e4093b6
RS
5844 /* Now output any pending elements which have become next. */
5845 if (pending)
5846 output_pending_init_elements (0);
5847}
8b6a5902 5848
3e4093b6
RS
5849/* Output any pending elements which have become next.
5850 As we output elements, constructor_unfilled_{fields,index}
5851 advances, which may cause other elements to become next;
5852 if so, they too are output.
8b6a5902 5853
3e4093b6
RS
5854 If ALL is 0, we return when there are
5855 no more pending elements to output now.
665f2503 5856
3e4093b6
RS
5857 If ALL is 1, we output space as necessary so that
5858 we can output all the pending elements. */
19d76e60 5859
3e4093b6
RS
5860static void
5861output_pending_init_elements (int all)
5862{
5863 struct init_node *elt = constructor_pending_elts;
5864 tree next;
de520661 5865
3e4093b6
RS
5866 retry:
5867
ba228239 5868 /* Look through the whole pending tree.
3e4093b6
RS
5869 If we find an element that should be output now,
5870 output it. Otherwise, set NEXT to the element
5871 that comes first among those still pending. */
5872
5873 next = 0;
5874 while (elt)
5875 {
5876 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 5877 {
3e4093b6
RS
5878 if (tree_int_cst_equal (elt->purpose,
5879 constructor_unfilled_index))
916c5919 5880 output_init_element (elt->value, true,
3e4093b6
RS
5881 TREE_TYPE (constructor_type),
5882 constructor_unfilled_index, 0);
5883 else if (tree_int_cst_lt (constructor_unfilled_index,
5884 elt->purpose))
8b6a5902 5885 {
3e4093b6
RS
5886 /* Advance to the next smaller node. */
5887 if (elt->left)
5888 elt = elt->left;
5889 else
5890 {
5891 /* We have reached the smallest node bigger than the
5892 current unfilled index. Fill the space first. */
5893 next = elt->purpose;
5894 break;
5895 }
8b6a5902 5896 }
ce662d4c
JJ
5897 else
5898 {
3e4093b6
RS
5899 /* Advance to the next bigger node. */
5900 if (elt->right)
5901 elt = elt->right;
5902 else
ce662d4c 5903 {
3e4093b6
RS
5904 /* We have reached the biggest node in a subtree. Find
5905 the parent of it, which is the next bigger node. */
5906 while (elt->parent && elt->parent->right == elt)
5907 elt = elt->parent;
5908 elt = elt->parent;
5909 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5910 elt->purpose))
5911 {
5912 next = elt->purpose;
5913 break;
5914 }
ce662d4c
JJ
5915 }
5916 }
8b6a5902 5917 }
3e4093b6
RS
5918 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5919 || TREE_CODE (constructor_type) == UNION_TYPE)
5920 {
5921 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 5922
3e4093b6
RS
5923 /* If the current record is complete we are done. */
5924 if (constructor_unfilled_fields == 0)
5925 break;
de520661 5926
3e4093b6
RS
5927 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5928 elt_bitpos = bit_position (elt->purpose);
5929 /* We can't compare fields here because there might be empty
5930 fields in between. */
5931 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5932 {
5933 constructor_unfilled_fields = elt->purpose;
916c5919 5934 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
3e4093b6
RS
5935 elt->purpose, 0);
5936 }
5937 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5938 {
5939 /* Advance to the next smaller node. */
5940 if (elt->left)
5941 elt = elt->left;
5942 else
5943 {
5944 /* We have reached the smallest node bigger than the
5945 current unfilled field. Fill the space first. */
5946 next = elt->purpose;
5947 break;
5948 }
5949 }
5950 else
5951 {
5952 /* Advance to the next bigger node. */
5953 if (elt->right)
5954 elt = elt->right;
5955 else
5956 {
5957 /* We have reached the biggest node in a subtree. Find
5958 the parent of it, which is the next bigger node. */
5959 while (elt->parent && elt->parent->right == elt)
5960 elt = elt->parent;
5961 elt = elt->parent;
5962 if (elt
5963 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5964 bit_position (elt->purpose))))
5965 {
5966 next = elt->purpose;
5967 break;
5968 }
5969 }
5970 }
5971 }
5972 }
de520661 5973
3e4093b6
RS
5974 /* Ordinarily return, but not if we want to output all
5975 and there are elements left. */
3f75a254 5976 if (!(all && next != 0))
e5cfb88f
RK
5977 return;
5978
3e4093b6
RS
5979 /* If it's not incremental, just skip over the gap, so that after
5980 jumping to retry we will output the next successive element. */
5981 if (TREE_CODE (constructor_type) == RECORD_TYPE
5982 || TREE_CODE (constructor_type) == UNION_TYPE)
5983 constructor_unfilled_fields = next;
5984 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5985 constructor_unfilled_index = next;
de520661 5986
3e4093b6
RS
5987 /* ELT now points to the node in the pending tree with the next
5988 initializer to output. */
5989 goto retry;
de520661
RS
5990}
5991\f
3e4093b6
RS
5992/* Add one non-braced element to the current constructor level.
5993 This adjusts the current position within the constructor's type.
5994 This may also start or terminate implicit levels
5995 to handle a partly-braced initializer.
e5e809f4 5996
3e4093b6
RS
5997 Once this has found the correct level for the new element,
5998 it calls output_init_element. */
5999
6000void
916c5919 6001process_init_element (struct c_expr value)
e5e809f4 6002{
916c5919
JM
6003 tree orig_value = value.value;
6004 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6005 bool strict_string = value.original_code == STRING_CST;
e5e809f4 6006
3e4093b6
RS
6007 designator_depth = 0;
6008 designator_errorneous = 0;
e5e809f4 6009
3e4093b6
RS
6010 /* Handle superfluous braces around string cst as in
6011 char x[] = {"foo"}; */
6012 if (string_flag
6013 && constructor_type
6014 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 6015 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 6016 && integer_zerop (constructor_unfilled_index))
e5e809f4 6017 {
916c5919 6018 if (constructor_stack->replacement_value.value)
3e4093b6
RS
6019 error_init ("excess elements in char array initializer");
6020 constructor_stack->replacement_value = value;
6021 return;
e5e809f4 6022 }
8b6a5902 6023
916c5919 6024 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
6025 {
6026 error_init ("excess elements in struct initializer");
6027 return;
e5e809f4
JL
6028 }
6029
3e4093b6
RS
6030 /* Ignore elements of a brace group if it is entirely superfluous
6031 and has already been diagnosed. */
6032 if (constructor_type == 0)
6033 return;
e5e809f4 6034
3e4093b6
RS
6035 /* If we've exhausted any levels that didn't have braces,
6036 pop them now. */
6037 while (constructor_stack->implicit)
6038 {
6039 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6040 || TREE_CODE (constructor_type) == UNION_TYPE)
6041 && constructor_fields == 0)
6042 process_init_element (pop_init_level (1));
6043 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6044 && (constructor_max_index == 0
6045 || tree_int_cst_lt (constructor_max_index,
6046 constructor_index)))
6047 process_init_element (pop_init_level (1));
6048 else
6049 break;
6050 }
e5e809f4 6051
3e4093b6
RS
6052 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6053 if (constructor_range_stack)
e5e809f4 6054 {
3e4093b6
RS
6055 /* If value is a compound literal and we'll be just using its
6056 content, don't put it into a SAVE_EXPR. */
916c5919 6057 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
6058 || !require_constant_value
6059 || flag_isoc99)
916c5919 6060 value.value = save_expr (value.value);
3e4093b6 6061 }
e5e809f4 6062
3e4093b6
RS
6063 while (1)
6064 {
6065 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 6066 {
3e4093b6
RS
6067 tree fieldtype;
6068 enum tree_code fieldcode;
e5e809f4 6069
3e4093b6
RS
6070 if (constructor_fields == 0)
6071 {
6072 pedwarn_init ("excess elements in struct initializer");
6073 break;
6074 }
e5e809f4 6075
3e4093b6
RS
6076 fieldtype = TREE_TYPE (constructor_fields);
6077 if (fieldtype != error_mark_node)
6078 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6079 fieldcode = TREE_CODE (fieldtype);
e5e809f4 6080
3e4093b6
RS
6081 /* Error for non-static initialization of a flexible array member. */
6082 if (fieldcode == ARRAY_TYPE
6083 && !require_constant_value
6084 && TYPE_SIZE (fieldtype) == NULL_TREE
6085 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6086 {
6087 error_init ("non-static initialization of a flexible array member");
6088 break;
6089 }
e5e809f4 6090
3e4093b6 6091 /* Accept a string constant to initialize a subarray. */
916c5919 6092 if (value.value != 0
3e4093b6 6093 && fieldcode == ARRAY_TYPE
197463ae 6094 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 6095 && string_flag)
916c5919 6096 value.value = orig_value;
3e4093b6
RS
6097 /* Otherwise, if we have come to a subaggregate,
6098 and we don't have an element of its type, push into it. */
0953878d 6099 else if (value.value != 0
916c5919
JM
6100 && value.value != error_mark_node
6101 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6
RS
6102 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6103 || fieldcode == UNION_TYPE))
6104 {
6105 push_init_level (1);
6106 continue;
6107 }
e5e809f4 6108
916c5919 6109 if (value.value)
3e4093b6
RS
6110 {
6111 push_member_name (constructor_fields);
916c5919
JM
6112 output_init_element (value.value, strict_string,
6113 fieldtype, constructor_fields, 1);
3e4093b6 6114 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
6115 }
6116 else
3e4093b6
RS
6117 /* Do the bookkeeping for an element that was
6118 directly output as a constructor. */
e5e809f4 6119 {
3e4093b6
RS
6120 /* For a record, keep track of end position of last field. */
6121 if (DECL_SIZE (constructor_fields))
6122 constructor_bit_index
6123 = size_binop (PLUS_EXPR,
6124 bit_position (constructor_fields),
6125 DECL_SIZE (constructor_fields));
6126
6127 /* If the current field was the first one not yet written out,
6128 it isn't now, so update. */
6129 if (constructor_unfilled_fields == constructor_fields)
6130 {
6131 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6132 /* Skip any nameless bit fields. */
6133 while (constructor_unfilled_fields != 0
6134 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6135 && DECL_NAME (constructor_unfilled_fields) == 0)
6136 constructor_unfilled_fields =
6137 TREE_CHAIN (constructor_unfilled_fields);
6138 }
e5e809f4 6139 }
3e4093b6
RS
6140
6141 constructor_fields = TREE_CHAIN (constructor_fields);
6142 /* Skip any nameless bit fields at the beginning. */
6143 while (constructor_fields != 0
6144 && DECL_C_BIT_FIELD (constructor_fields)
6145 && DECL_NAME (constructor_fields) == 0)
6146 constructor_fields = TREE_CHAIN (constructor_fields);
e5e809f4 6147 }
3e4093b6 6148 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 6149 {
3e4093b6
RS
6150 tree fieldtype;
6151 enum tree_code fieldcode;
e5e809f4 6152
3e4093b6
RS
6153 if (constructor_fields == 0)
6154 {
6155 pedwarn_init ("excess elements in union initializer");
6156 break;
6157 }
e5e809f4 6158
3e4093b6
RS
6159 fieldtype = TREE_TYPE (constructor_fields);
6160 if (fieldtype != error_mark_node)
6161 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6162 fieldcode = TREE_CODE (fieldtype);
e5e809f4 6163
3e4093b6
RS
6164 /* Warn that traditional C rejects initialization of unions.
6165 We skip the warning if the value is zero. This is done
6166 under the assumption that the zero initializer in user
6167 code appears conditioned on e.g. __STDC__ to avoid
6168 "missing initializer" warnings and relies on default
6169 initialization to zero in the traditional C case.
6170 We also skip the warning if the initializer is designated,
6171 again on the assumption that this must be conditional on
6172 __STDC__ anyway (and we've already complained about the
6173 member-designator already). */
6174 if (warn_traditional && !in_system_header && !constructor_designated
916c5919
JM
6175 && !(value.value && (integer_zerop (value.value)
6176 || real_zerop (value.value))))
3e4093b6 6177 warning ("traditional C rejects initialization of unions");
e5e809f4 6178
3e4093b6 6179 /* Accept a string constant to initialize a subarray. */
916c5919 6180 if (value.value != 0
3e4093b6 6181 && fieldcode == ARRAY_TYPE
197463ae 6182 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 6183 && string_flag)
916c5919 6184 value.value = orig_value;
3e4093b6
RS
6185 /* Otherwise, if we have come to a subaggregate,
6186 and we don't have an element of its type, push into it. */
0953878d 6187 else if (value.value != 0
916c5919
JM
6188 && value.value != error_mark_node
6189 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6
RS
6190 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6191 || fieldcode == UNION_TYPE))
6192 {
6193 push_init_level (1);
6194 continue;
6195 }
e5e809f4 6196
916c5919 6197 if (value.value)
3e4093b6
RS
6198 {
6199 push_member_name (constructor_fields);
916c5919
JM
6200 output_init_element (value.value, strict_string,
6201 fieldtype, constructor_fields, 1);
3e4093b6 6202 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
6203 }
6204 else
3e4093b6
RS
6205 /* Do the bookkeeping for an element that was
6206 directly output as a constructor. */
e5e809f4 6207 {
3e4093b6
RS
6208 constructor_bit_index = DECL_SIZE (constructor_fields);
6209 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
e5e809f4 6210 }
e5e809f4 6211
3e4093b6
RS
6212 constructor_fields = 0;
6213 }
6214 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6215 {
6216 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6217 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 6218
3e4093b6 6219 /* Accept a string constant to initialize a subarray. */
916c5919 6220 if (value.value != 0
3e4093b6 6221 && eltcode == ARRAY_TYPE
197463ae 6222 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 6223 && string_flag)
916c5919 6224 value.value = orig_value;
3e4093b6
RS
6225 /* Otherwise, if we have come to a subaggregate,
6226 and we don't have an element of its type, push into it. */
0953878d 6227 else if (value.value != 0
916c5919
JM
6228 && value.value != error_mark_node
6229 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6
RS
6230 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6231 || eltcode == UNION_TYPE))
6232 {
6233 push_init_level (1);
6234 continue;
6235 }
8b6a5902 6236
3e4093b6
RS
6237 if (constructor_max_index != 0
6238 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6239 || integer_all_onesp (constructor_max_index)))
6240 {
6241 pedwarn_init ("excess elements in array initializer");
6242 break;
6243 }
8b6a5902 6244
3e4093b6 6245 /* Now output the actual element. */
916c5919 6246 if (value.value)
3e4093b6
RS
6247 {
6248 push_array_bounds (tree_low_cst (constructor_index, 0));
916c5919
JM
6249 output_init_element (value.value, strict_string,
6250 elttype, constructor_index, 1);
3e4093b6
RS
6251 RESTORE_SPELLING_DEPTH (constructor_depth);
6252 }
2f6e4e97 6253
3e4093b6
RS
6254 constructor_index
6255 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 6256
916c5919 6257 if (!value.value)
3e4093b6
RS
6258 /* If we are doing the bookkeeping for an element that was
6259 directly output as a constructor, we must update
6260 constructor_unfilled_index. */
6261 constructor_unfilled_index = constructor_index;
6262 }
6263 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6264 {
6265 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 6266
3e4093b6
RS
6267 /* Do a basic check of initializer size. Note that vectors
6268 always have a fixed size derived from their type. */
6269 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6270 {
6271 pedwarn_init ("excess elements in vector initializer");
6272 break;
6273 }
8b6a5902 6274
3e4093b6 6275 /* Now output the actual element. */
916c5919
JM
6276 if (value.value)
6277 output_init_element (value.value, strict_string,
6278 elttype, constructor_index, 1);
8b6a5902 6279
3e4093b6
RS
6280 constructor_index
6281 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 6282
916c5919 6283 if (!value.value)
3e4093b6
RS
6284 /* If we are doing the bookkeeping for an element that was
6285 directly output as a constructor, we must update
6286 constructor_unfilled_index. */
6287 constructor_unfilled_index = constructor_index;
6288 }
8b6a5902 6289
3e4093b6
RS
6290 /* Handle the sole element allowed in a braced initializer
6291 for a scalar variable. */
b4519d39
SB
6292 else if (constructor_type != error_mark_node
6293 && constructor_fields == 0)
8b6a5902 6294 {
3e4093b6
RS
6295 pedwarn_init ("excess elements in scalar initializer");
6296 break;
8b6a5902
JJ
6297 }
6298 else
6299 {
916c5919
JM
6300 if (value.value)
6301 output_init_element (value.value, strict_string,
6302 constructor_type, NULL_TREE, 1);
3e4093b6 6303 constructor_fields = 0;
8b6a5902
JJ
6304 }
6305
3e4093b6
RS
6306 /* Handle range initializers either at this level or anywhere higher
6307 in the designator stack. */
6308 if (constructor_range_stack)
8b6a5902 6309 {
3e4093b6
RS
6310 struct constructor_range_stack *p, *range_stack;
6311 int finish = 0;
6312
6313 range_stack = constructor_range_stack;
6314 constructor_range_stack = 0;
6315 while (constructor_stack != range_stack->stack)
8b6a5902 6316 {
366de0ce 6317 gcc_assert (constructor_stack->implicit);
3e4093b6 6318 process_init_element (pop_init_level (1));
8b6a5902 6319 }
3e4093b6
RS
6320 for (p = range_stack;
6321 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6322 p = p->prev)
8b6a5902 6323 {
366de0ce 6324 gcc_assert (constructor_stack->implicit);
3e4093b6 6325 process_init_element (pop_init_level (1));
8b6a5902 6326 }
3e4093b6
RS
6327
6328 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6329 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6330 finish = 1;
6331
6332 while (1)
6333 {
6334 constructor_index = p->index;
6335 constructor_fields = p->fields;
6336 if (finish && p->range_end && p->index == p->range_start)
6337 {
6338 finish = 0;
6339 p->prev = 0;
6340 }
6341 p = p->next;
6342 if (!p)
6343 break;
6344 push_init_level (2);
6345 p->stack = constructor_stack;
6346 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6347 p->index = p->range_start;
6348 }
6349
6350 if (!finish)
6351 constructor_range_stack = range_stack;
6352 continue;
8b6a5902
JJ
6353 }
6354
3e4093b6 6355 break;
8b6a5902
JJ
6356 }
6357
3e4093b6
RS
6358 constructor_range_stack = 0;
6359}
6360\f
9f0e2d86
ZW
6361/* Build a complete asm-statement, whose components are a CV_QUALIFIER
6362 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 6363 an ASM_EXPR node). */
3e4093b6 6364tree
9f0e2d86 6365build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 6366{
6de9cd9a
DN
6367 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6368 ASM_VOLATILE_P (args) = 1;
9f0e2d86 6369 return add_stmt (args);
8b6a5902
JJ
6370}
6371
9f0e2d86
ZW
6372/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6373 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6374 SIMPLE indicates whether there was anything at all after the
6375 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 6376 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 6377tree
9f0e2d86
ZW
6378build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6379 bool simple)
e5e809f4 6380{
3e4093b6 6381 tree tail;
9f0e2d86 6382 tree args;
6de9cd9a
DN
6383 int i;
6384 const char *constraint;
74f0c611 6385 const char **oconstraints;
6de9cd9a 6386 bool allows_mem, allows_reg, is_inout;
74f0c611 6387 int ninputs, noutputs;
6de9cd9a
DN
6388
6389 ninputs = list_length (inputs);
6390 noutputs = list_length (outputs);
74f0c611
RH
6391 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6392
6393 string = resolve_asm_operand_names (string, outputs, inputs);
3e4093b6 6394
6de9cd9a
DN
6395 /* Remove output conversions that change the type but not the mode. */
6396 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 6397 {
3e4093b6 6398 tree output = TREE_VALUE (tail);
74f0c611
RH
6399
6400 /* ??? Really, this should not be here. Users should be using a
6401 proper lvalue, dammit. But there's a long history of using casts
6402 in the output operands. In cases like longlong.h, this becomes a
6403 primitive form of typechecking -- if the cast can be removed, then
6404 the output operand had a type of the proper width; otherwise we'll
6405 get an error. Gross, but ... */
3e4093b6 6406 STRIP_NOPS (output);
74f0c611
RH
6407
6408 if (!lvalue_or_else (output, lv_asm))
6409 output = error_mark_node;
8b6a5902 6410
6de9cd9a 6411 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
6412 oconstraints[i] = constraint;
6413
6414 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6415 &allows_mem, &allows_reg, &is_inout))
6416 {
6417 /* If the operand is going to end up in memory,
6418 mark it addressable. */
6419 if (!allows_reg && !c_mark_addressable (output))
6420 output = error_mark_node;
6421 }
6422 else
6423 output = error_mark_node;
3e4093b6 6424
74f0c611 6425 TREE_VALUE (tail) = output;
8b6a5902 6426 }
3e4093b6
RS
6427
6428 /* Perform default conversions on array and function inputs.
6429 Don't do this for other types as it would screw up operands
6430 expected to be in memory. */
74f0c611
RH
6431 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6432 {
6433 tree input;
6434
6435 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6436 input = TREE_VALUE (tail);
6437
6438 input = default_function_array_conversion (input);
6439
6440 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6441 oconstraints, &allows_mem, &allows_reg))
6442 {
6443 /* If the operand is going to end up in memory,
6444 mark it addressable. */
b4c33883
AP
6445 if (!allows_reg && allows_mem)
6446 {
6447 /* Strip the nops as we allow this case. FIXME, this really
6448 should be rejected or made deprecated. */
6449 STRIP_NOPS (input);
6450 if (!c_mark_addressable (input))
6451 input = error_mark_node;
6452 }
74f0c611
RH
6453 }
6454 else
6455 input = error_mark_node;
6456
6457 TREE_VALUE (tail) = input;
6458 }
3e4093b6 6459
e130a54b 6460 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
9f0e2d86
ZW
6461
6462 /* Simple asm statements are treated as volatile. */
6463 if (simple)
6464 {
6de9cd9a 6465 ASM_VOLATILE_P (args) = 1;
9f0e2d86
ZW
6466 ASM_INPUT_P (args) = 1;
6467 }
74f0c611 6468
9f0e2d86 6469 return args;
e5e809f4 6470}
3e4093b6 6471\f
506e2710
RH
6472/* Generate a goto statement to LABEL. */
6473
6474tree
6475c_finish_goto_label (tree label)
6476{
6477 tree decl = lookup_label (label);
6478 if (!decl)
6479 return NULL_TREE;
6480
6481 TREE_USED (decl) = 1;
53fb4de3 6482 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
506e2710
RH
6483}
6484
6485/* Generate a computed goto statement to EXPR. */
6486
6487tree
6488c_finish_goto_ptr (tree expr)
6489{
6490 if (pedantic)
bda67431 6491 pedwarn ("ISO C forbids %<goto *expr;%>");
506e2710 6492 expr = convert (ptr_type_node, expr);
53fb4de3 6493 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
506e2710
RH
6494}
6495
5088b058
RH
6496/* Generate a C `return' statement. RETVAL is the expression for what
6497 to return, or a null pointer for `return;' with no value. */
de520661 6498
506e2710 6499tree
5088b058 6500c_finish_return (tree retval)
3e4093b6
RS
6501{
6502 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6503
6504 if (TREE_THIS_VOLATILE (current_function_decl))
bda67431 6505 warning ("function declared %<noreturn%> has a %<return%> statement");
3e4093b6
RS
6506
6507 if (!retval)
de520661 6508 {
3e4093b6
RS
6509 current_function_returns_null = 1;
6510 if ((warn_return_type || flag_isoc99)
6511 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
bda67431
JM
6512 pedwarn_c99 ("%<return%> with no value, in "
6513 "function returning non-void");
400fbf9f 6514 }
3e4093b6 6515 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 6516 {
3e4093b6
RS
6517 current_function_returns_null = 1;
6518 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
bda67431 6519 pedwarn ("%<return%> with a value, in function returning void");
de520661 6520 }
3e4093b6 6521 else
de520661 6522 {
2ac2f164 6523 tree t = convert_for_assignment (valtype, retval, ic_return,
3e4093b6
RS
6524 NULL_TREE, NULL_TREE, 0);
6525 tree res = DECL_RESULT (current_function_decl);
6526 tree inner;
6527
6528 current_function_returns_value = 1;
6529 if (t == error_mark_node)
506e2710 6530 return NULL_TREE;
3e4093b6
RS
6531
6532 inner = t = convert (TREE_TYPE (res), t);
6533
6534 /* Strip any conversions, additions, and subtractions, and see if
6535 we are returning the address of a local variable. Warn if so. */
6536 while (1)
8b6a5902 6537 {
3e4093b6 6538 switch (TREE_CODE (inner))
8b6a5902 6539 {
3e4093b6
RS
6540 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6541 case PLUS_EXPR:
6542 inner = TREE_OPERAND (inner, 0);
6543 continue;
6544
6545 case MINUS_EXPR:
6546 /* If the second operand of the MINUS_EXPR has a pointer
6547 type (or is converted from it), this may be valid, so
6548 don't give a warning. */
6549 {
6550 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 6551
3f75a254 6552 while (!POINTER_TYPE_P (TREE_TYPE (op1))
3e4093b6
RS
6553 && (TREE_CODE (op1) == NOP_EXPR
6554 || TREE_CODE (op1) == NON_LVALUE_EXPR
6555 || TREE_CODE (op1) == CONVERT_EXPR))
6556 op1 = TREE_OPERAND (op1, 0);
8b6a5902 6557
3e4093b6
RS
6558 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6559 break;
8b6a5902 6560
3e4093b6
RS
6561 inner = TREE_OPERAND (inner, 0);
6562 continue;
6563 }
400fbf9f 6564
3e4093b6
RS
6565 case ADDR_EXPR:
6566 inner = TREE_OPERAND (inner, 0);
c2f4acb7 6567
6615c446 6568 while (REFERENCE_CLASS_P (inner)
9fc3b39a 6569 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 6570 inner = TREE_OPERAND (inner, 0);
8b6a5902 6571
a2f1f4c3 6572 if (DECL_P (inner)
3f75a254
JM
6573 && !DECL_EXTERNAL (inner)
6574 && !TREE_STATIC (inner)
3e4093b6
RS
6575 && DECL_CONTEXT (inner) == current_function_decl)
6576 warning ("function returns address of local variable");
6577 break;
8b6a5902 6578
3e4093b6
RS
6579 default:
6580 break;
6581 }
de520661 6582
3e4093b6
RS
6583 break;
6584 }
6585
53fb4de3 6586 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
de520661 6587 }
8b6a5902 6588
506e2710 6589 return add_stmt (build_stmt (RETURN_EXPR, retval));
de520661 6590}
3e4093b6
RS
6591\f
6592struct c_switch {
6593 /* The SWITCH_STMT being built. */
6594 tree switch_stmt;
a6c0a76c 6595
89dbed81 6596 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
6597 default conversion is applied. */
6598 tree orig_type;
6599
3e4093b6
RS
6600 /* A splay-tree mapping the low element of a case range to the high
6601 element, or NULL_TREE if there is no high element. Used to
6602 determine whether or not a new case label duplicates an old case
6603 label. We need a tree, rather than simply a hash table, because
6604 of the GNU case range extension. */
6605 splay_tree cases;
a6c0a76c 6606
3e4093b6
RS
6607 /* The next node on the stack. */
6608 struct c_switch *next;
6609};
400fbf9f 6610
3e4093b6
RS
6611/* A stack of the currently active switch statements. The innermost
6612 switch statement is on the top of the stack. There is no need to
6613 mark the stack for garbage collection because it is only active
6614 during the processing of the body of a function, and we never
6615 collect at that point. */
de520661 6616
506e2710 6617struct c_switch *c_switch_stack;
de520661 6618
3e4093b6
RS
6619/* Start a C switch statement, testing expression EXP. Return the new
6620 SWITCH_STMT. */
de520661 6621
3e4093b6
RS
6622tree
6623c_start_case (tree exp)
de520661 6624{
3e4093b6
RS
6625 enum tree_code code;
6626 tree type, orig_type = error_mark_node;
6627 struct c_switch *cs;
2f6e4e97 6628
3e4093b6 6629 if (exp != error_mark_node)
de520661 6630 {
3e4093b6
RS
6631 code = TREE_CODE (TREE_TYPE (exp));
6632 orig_type = TREE_TYPE (exp);
6633
3f75a254 6634 if (!INTEGRAL_TYPE_P (orig_type)
3e4093b6 6635 && code != ERROR_MARK)
de520661 6636 {
3e4093b6
RS
6637 error ("switch quantity not an integer");
6638 exp = integer_zero_node;
02fbae83 6639 orig_type = error_mark_node;
de520661 6640 }
3e4093b6 6641 else
de520661 6642 {
3e4093b6 6643 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
8b6a5902 6644
3e4093b6
RS
6645 if (warn_traditional && !in_system_header
6646 && (type == long_integer_type_node
6647 || type == long_unsigned_type_node))
bda67431
JM
6648 warning ("%<long%> switch expression not converted to "
6649 "%<int%> in ISO C");
8b6a5902 6650
3e4093b6
RS
6651 exp = default_conversion (exp);
6652 type = TREE_TYPE (exp);
6653 }
6654 }
6655
6656 /* Add this new SWITCH_STMT to the stack. */
5d038c4c 6657 cs = XNEW (struct c_switch);
ebaae582 6658 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
a6c0a76c 6659 cs->orig_type = orig_type;
3e4093b6 6660 cs->cases = splay_tree_new (case_compare, NULL, NULL);
506e2710
RH
6661 cs->next = c_switch_stack;
6662 c_switch_stack = cs;
3e4093b6 6663
506e2710 6664 return add_stmt (cs->switch_stmt);
3e4093b6
RS
6665}
6666
6667/* Process a case label. */
6668
6669tree
6670do_case (tree low_value, tree high_value)
6671{
6672 tree label = NULL_TREE;
6673
506e2710 6674 if (c_switch_stack)
3e4093b6 6675 {
506e2710 6676 label = c_add_case_label (c_switch_stack->cases,
ebaae582 6677 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
a6c0a76c 6678 c_switch_stack->orig_type,
3e4093b6
RS
6679 low_value, high_value);
6680 if (label == error_mark_node)
6681 label = NULL_TREE;
de520661 6682 }
3e4093b6
RS
6683 else if (low_value)
6684 error ("case label not within a switch statement");
6685 else
bda67431 6686 error ("%<default%> label not within a switch statement");
de520661 6687
3e4093b6
RS
6688 return label;
6689}
de520661 6690
3e4093b6 6691/* Finish the switch statement. */
de520661 6692
3e4093b6 6693void
325c3691 6694c_finish_case (tree body)
3e4093b6 6695{
506e2710 6696 struct c_switch *cs = c_switch_stack;
3e4093b6 6697
ebaae582 6698 SWITCH_STMT_BODY (cs->switch_stmt) = body;
325c3691 6699
6de9cd9a
DN
6700 /* Emit warnings as needed. */
6701 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6702
3e4093b6 6703 /* Pop the stack. */
506e2710 6704 c_switch_stack = cs->next;
3e4093b6 6705 splay_tree_delete (cs->cases);
5d038c4c 6706 XDELETE (cs);
de520661 6707}
325c3691 6708\f
506e2710
RH
6709/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6710 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6711 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6712 statement, and was not surrounded with parenthesis. */
325c3691 6713
9e51cf9d 6714void
506e2710
RH
6715c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6716 tree else_block, bool nested_if)
325c3691 6717{
506e2710 6718 tree stmt;
325c3691 6719
506e2710
RH
6720 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6721 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 6722 {
506e2710 6723 tree inner_if = then_block;
16865eaa 6724
61ada8ae 6725 /* We know from the grammar productions that there is an IF nested
506e2710
RH
6726 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6727 it might not be exactly THEN_BLOCK, but should be the last
6728 non-container statement within. */
6729 while (1)
6730 switch (TREE_CODE (inner_if))
6731 {
6732 case COND_EXPR:
6733 goto found;
6734 case BIND_EXPR:
6735 inner_if = BIND_EXPR_BODY (inner_if);
6736 break;
6737 case STATEMENT_LIST:
6738 inner_if = expr_last (then_block);
6739 break;
6740 case TRY_FINALLY_EXPR:
6741 case TRY_CATCH_EXPR:
6742 inner_if = TREE_OPERAND (inner_if, 0);
6743 break;
6744 default:
366de0ce 6745 gcc_unreachable ();
506e2710
RH
6746 }
6747 found:
16865eaa 6748
506e2710 6749 if (COND_EXPR_ELSE (inner_if))
bda67431 6750 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
506e2710
RH
6751 &if_locus);
6752 }
16865eaa 6753
506e2710
RH
6754 /* Diagnose ";" via the special empty statement node that we create. */
6755 if (extra_warnings)
16865eaa 6756 {
506e2710
RH
6757 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6758 {
6759 if (!else_block)
6760 warning ("%Hempty body in an if-statement",
6761 EXPR_LOCUS (then_block));
6762 then_block = alloc_stmt_list ();
6763 }
6764 if (else_block
6765 && TREE_CODE (else_block) == NOP_EXPR
6766 && !TREE_TYPE (else_block))
6767 {
6768 warning ("%Hempty body in an else-statement",
6769 EXPR_LOCUS (else_block));
6770 else_block = alloc_stmt_list ();
6771 }
16865eaa 6772 }
325c3691 6773
506e2710 6774 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
a281759f 6775 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 6776 add_stmt (stmt);
325c3691
RH
6777}
6778
506e2710
RH
6779/* Emit a general-purpose loop construct. START_LOCUS is the location of
6780 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6781 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 6782 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 6783 the continue label. Everything is allowed to be NULL. */
325c3691
RH
6784
6785void
506e2710
RH
6786c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6787 tree blab, tree clab, bool cond_is_first)
325c3691 6788{
506e2710
RH
6789 tree entry = NULL, exit = NULL, t;
6790
28af952a
RS
6791 /* If the condition is zero don't generate a loop construct. */
6792 if (cond && integer_zerop (cond))
6793 {
6794 if (cond_is_first)
6795 {
6796 t = build_and_jump (&blab);
6797 SET_EXPR_LOCATION (t, start_locus);
6798 add_stmt (t);
6799 }
6800 }
6801 else
506e2710
RH
6802 {
6803 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6804
6805 /* If we have an exit condition, then we build an IF with gotos either
6806 out of the loop, or to the top of it. If there's no exit condition,
6807 then we just build a jump back to the top. */
6808 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6809
28af952a 6810 if (cond && !integer_nonzerop (cond))
506e2710
RH
6811 {
6812 /* Canonicalize the loop condition to the end. This means
6813 generating a branch to the loop condition. Reuse the
6814 continue label, if possible. */
6815 if (cond_is_first)
6816 {
6817 if (incr || !clab)
6818 {
6819 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6820 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6821 }
6822 else
6823 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 6824 SET_EXPR_LOCATION (t, start_locus);
506e2710
RH
6825 add_stmt (t);
6826 }
6827
6828 t = build_and_jump (&blab);
53fb4de3 6829 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
506e2710
RH
6830 exit = fold (exit);
6831 if (cond_is_first)
a281759f 6832 SET_EXPR_LOCATION (exit, start_locus);
506e2710 6833 else
a281759f 6834 SET_EXPR_LOCATION (exit, input_location);
506e2710
RH
6835 }
6836
6837 add_stmt (top);
6838 }
6839
6840 if (body)
6841 add_stmt (body);
6842 if (clab)
6843 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6844 if (incr)
6845 add_stmt (incr);
6846 if (entry)
6847 add_stmt (entry);
6848 if (exit)
6849 add_stmt (exit);
6850 if (blab)
6851 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 6852}
325c3691
RH
6853
6854tree
506e2710 6855c_finish_bc_stmt (tree *label_p, bool is_break)
325c3691 6856{
089efaa4 6857 bool skip;
506e2710 6858 tree label = *label_p;
325c3691 6859
089efaa4
ILT
6860 /* In switch statements break is sometimes stylistically used after
6861 a return statement. This can lead to spurious warnings about
6862 control reaching the end of a non-void function when it is
6863 inlined. Note that we are calling block_may_fallthru with
6864 language specific tree nodes; this works because
6865 block_may_fallthru returns true when given something it does not
6866 understand. */
6867 skip = !block_may_fallthru (cur_stmt_list);
6868
506e2710 6869 if (!label)
089efaa4
ILT
6870 {
6871 if (!skip)
6872 *label_p = label = create_artificial_label ();
6873 }
506e2710
RH
6874 else if (TREE_CODE (label) != LABEL_DECL)
6875 {
6876 if (is_break)
6877 error ("break statement not within loop or switch");
6878 else
6879 error ("continue statement not within a loop");
6880 return NULL_TREE;
6881 }
325c3691 6882
089efaa4
ILT
6883 if (skip)
6884 return NULL_TREE;
6885
53fb4de3 6886 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
6887}
6888
506e2710 6889/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
6890
6891static void
6892emit_side_effect_warnings (tree expr)
6893{
e6b5a630
RH
6894 if (expr == error_mark_node)
6895 ;
6896 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
6897 {
6898 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6899 warning ("%Hstatement with no effect",
607bdeaa 6900 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
3a5b9284
RH
6901 }
6902 else if (warn_unused_value)
6903 warn_if_unused_value (expr, input_location);
6904}
6905
506e2710
RH
6906/* Process an expression as if it were a complete statement. Emit
6907 diagnostics, but do not call ADD_STMT. */
3a5b9284 6908
506e2710
RH
6909tree
6910c_process_expr_stmt (tree expr)
3a5b9284
RH
6911{
6912 if (!expr)
506e2710 6913 return NULL_TREE;
3a5b9284
RH
6914
6915 /* Do default conversion if safe and possibly important,
6916 in case within ({...}). */
6917 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6918 && (flag_isoc99 || lvalue_p (expr)))
6919 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6920 expr = default_conversion (expr);
6921
6922 if (warn_sequence_point)
6923 verify_sequence_points (expr);
6924
6925 if (TREE_TYPE (expr) != error_mark_node
6926 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6927 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6928 error ("expression statement has incomplete type");
6929
6930 /* If we're not processing a statement expression, warn about unused values.
6931 Warnings for statement expressions will be emitted later, once we figure
6932 out which is the result. */
6933 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6934 && (extra_warnings || warn_unused_value))
6935 emit_side_effect_warnings (expr);
6936
6937 /* If the expression is not of a type to which we cannot assign a line
6938 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 6939 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
3a5b9284
RH
6940 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6941
506e2710 6942 if (EXPR_P (expr))
a281759f 6943 SET_EXPR_LOCATION (expr, input_location);
506e2710
RH
6944
6945 return expr;
6946}
6947
6948/* Emit an expression as a statement. */
6949
6950tree
6951c_finish_expr_stmt (tree expr)
6952{
6953 if (expr)
6954 return add_stmt (c_process_expr_stmt (expr));
6955 else
6956 return NULL;
3a5b9284
RH
6957}
6958
6959/* Do the opposite and emit a statement as an expression. To begin,
6960 create a new binding level and return it. */
325c3691
RH
6961
6962tree
6963c_begin_stmt_expr (void)
6964{
6965 tree ret;
6966
6967 /* We must force a BLOCK for this level so that, if it is not expanded
6968 later, there is a way to turn off the entire subtree of blocks that
6969 are contained in it. */
6970 keep_next_level ();
6971 ret = c_begin_compound_stmt (true);
6972
6973 /* Mark the current statement list as belonging to a statement list. */
6974 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6975
6976 return ret;
6977}
6978
6979tree
6980c_finish_stmt_expr (tree body)
6981{
3a5b9284 6982 tree last, type, tmp, val;
325c3691
RH
6983 tree *last_p;
6984
6985 body = c_end_compound_stmt (body, true);
6986
3a5b9284
RH
6987 /* Locate the last statement in BODY. See c_end_compound_stmt
6988 about always returning a BIND_EXPR. */
6989 last_p = &BIND_EXPR_BODY (body);
6990 last = BIND_EXPR_BODY (body);
6991
6992 continue_searching:
325c3691
RH
6993 if (TREE_CODE (last) == STATEMENT_LIST)
6994 {
3a5b9284
RH
6995 tree_stmt_iterator i;
6996
6997 /* This can happen with degenerate cases like ({ }). No value. */
6998 if (!TREE_SIDE_EFFECTS (last))
6999 return body;
7000
7001 /* If we're supposed to generate side effects warnings, process
7002 all of the statements except the last. */
7003 if (extra_warnings || warn_unused_value)
325c3691 7004 {
3a5b9284
RH
7005 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7006 emit_side_effect_warnings (tsi_stmt (i));
325c3691
RH
7007 }
7008 else
3a5b9284
RH
7009 i = tsi_last (last);
7010 last_p = tsi_stmt_ptr (i);
7011 last = *last_p;
325c3691
RH
7012 }
7013
3a5b9284
RH
7014 /* If the end of the list is exception related, then the list was split
7015 by a call to push_cleanup. Continue searching. */
7016 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7017 || TREE_CODE (last) == TRY_CATCH_EXPR)
7018 {
7019 last_p = &TREE_OPERAND (last, 0);
7020 last = *last_p;
7021 goto continue_searching;
7022 }
7023
7024 /* In the case that the BIND_EXPR is not necessary, return the
7025 expression out from inside it. */
e6b5a630
RH
7026 if (last == error_mark_node
7027 || (last == BIND_EXPR_BODY (body)
7028 && BIND_EXPR_VARS (body) == NULL))
3a5b9284 7029 return last;
325c3691
RH
7030
7031 /* Extract the type of said expression. */
7032 type = TREE_TYPE (last);
325c3691 7033
3a5b9284
RH
7034 /* If we're not returning a value at all, then the BIND_EXPR that
7035 we already have is a fine expression to return. */
7036 if (!type || VOID_TYPE_P (type))
7037 return body;
7038
7039 /* Now that we've located the expression containing the value, it seems
7040 silly to make voidify_wrapper_expr repeat the process. Create a
7041 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7042 tmp = create_tmp_var_raw (type, NULL);
7043
7044 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7045 tree_expr_nonnegative_p giving up immediately. */
7046 val = last;
7047 if (TREE_CODE (val) == NOP_EXPR
7048 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7049 val = TREE_OPERAND (val, 0);
7050
53fb4de3 7051 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
3a5b9284
RH
7052 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7053
53fb4de3 7054 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
325c3691
RH
7055}
7056\f
7057/* Begin and end compound statements. This is as simple as pushing
7058 and popping new statement lists from the tree. */
7059
7060tree
7061c_begin_compound_stmt (bool do_scope)
7062{
7063 tree stmt = push_stmt_list ();
7064 if (do_scope)
4dfa0342 7065 push_scope ();
325c3691
RH
7066 return stmt;
7067}
7068
7069tree
7070c_end_compound_stmt (tree stmt, bool do_scope)
7071{
7072 tree block = NULL;
7073
7074 if (do_scope)
7075 {
7076 if (c_dialect_objc ())
7077 objc_clear_super_receiver ();
7078 block = pop_scope ();
7079 }
7080
7081 stmt = pop_stmt_list (stmt);
7082 stmt = c_build_bind_expr (block, stmt);
7083
7084 /* If this compound statement is nested immediately inside a statement
7085 expression, then force a BIND_EXPR to be created. Otherwise we'll
7086 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7087 STATEMENT_LISTs merge, and thus we can lose track of what statement
7088 was really last. */
7089 if (cur_stmt_list
7090 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7091 && TREE_CODE (stmt) != BIND_EXPR)
7092 {
53fb4de3 7093 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691
RH
7094 TREE_SIDE_EFFECTS (stmt) = 1;
7095 }
7096
7097 return stmt;
7098}
5a508662
RH
7099
7100/* Queue a cleanup. CLEANUP is an expression/statement to be executed
7101 when the current scope is exited. EH_ONLY is true when this is not
7102 meant to apply to normal control flow transfer. */
7103
7104void
e18476eb 7105push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
5a508662 7106{
3a5b9284
RH
7107 enum tree_code code;
7108 tree stmt, list;
7109 bool stmt_expr;
7110
7111 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7112 stmt = build_stmt (code, NULL, cleanup);
5a508662 7113 add_stmt (stmt);
3a5b9284
RH
7114 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7115 list = push_stmt_list ();
7116 TREE_OPERAND (stmt, 0) = list;
7117 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 7118}
325c3691 7119\f
3e4093b6
RS
7120/* Build a binary-operation expression without default conversions.
7121 CODE is the kind of expression to build.
7122 This function differs from `build' in several ways:
7123 the data type of the result is computed and recorded in it,
7124 warnings are generated if arg data types are invalid,
7125 special handling for addition and subtraction of pointers is known,
7126 and some optimization is done (operations on narrow ints
7127 are done in the narrower type when that gives the same result).
7128 Constant folding is also done before the result is returned.
de520661 7129
3e4093b6
RS
7130 Note that the operands will never have enumeral types, or function
7131 or array types, because either they will have the default conversions
7132 performed or they have both just been converted to some other type in which
7133 the arithmetic is to be done. */
7134
7135tree
7136build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7137 int convert_p)
de520661 7138{
3e4093b6
RS
7139 tree type0, type1;
7140 enum tree_code code0, code1;
7141 tree op0, op1;
b62acd60 7142
3e4093b6
RS
7143 /* Expression code to give to the expression when it is built.
7144 Normally this is CODE, which is what the caller asked for,
7145 but in some special cases we change it. */
7146 enum tree_code resultcode = code;
8b6a5902 7147
3e4093b6
RS
7148 /* Data type in which the computation is to be performed.
7149 In the simplest cases this is the common type of the arguments. */
7150 tree result_type = NULL;
7151
7152 /* Nonzero means operands have already been type-converted
7153 in whatever way is necessary.
7154 Zero means they need to be converted to RESULT_TYPE. */
7155 int converted = 0;
7156
7157 /* Nonzero means create the expression with this type, rather than
7158 RESULT_TYPE. */
7159 tree build_type = 0;
7160
7161 /* Nonzero means after finally constructing the expression
7162 convert it to this type. */
7163 tree final_type = 0;
7164
7165 /* Nonzero if this is an operation like MIN or MAX which can
7166 safely be computed in short if both args are promoted shorts.
7167 Also implies COMMON.
7168 -1 indicates a bitwise operation; this makes a difference
7169 in the exact conditions for when it is safe to do the operation
7170 in a narrower mode. */
7171 int shorten = 0;
7172
7173 /* Nonzero if this is a comparison operation;
7174 if both args are promoted shorts, compare the original shorts.
7175 Also implies COMMON. */
7176 int short_compare = 0;
7177
7178 /* Nonzero if this is a right-shift operation, which can be computed on the
7179 original short and then promoted if the operand is a promoted short. */
7180 int short_shift = 0;
7181
7182 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7183 int common = 0;
7184
7185 if (convert_p)
790e9490 7186 {
3e4093b6
RS
7187 op0 = default_conversion (orig_op0);
7188 op1 = default_conversion (orig_op1);
790e9490 7189 }
3e4093b6 7190 else
790e9490 7191 {
3e4093b6
RS
7192 op0 = orig_op0;
7193 op1 = orig_op1;
790e9490
RS
7194 }
7195
3e4093b6
RS
7196 type0 = TREE_TYPE (op0);
7197 type1 = TREE_TYPE (op1);
91fa3c30 7198
3e4093b6
RS
7199 /* The expression codes of the data types of the arguments tell us
7200 whether the arguments are integers, floating, pointers, etc. */
7201 code0 = TREE_CODE (type0);
7202 code1 = TREE_CODE (type1);
7203
7204 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7205 STRIP_TYPE_NOPS (op0);
7206 STRIP_TYPE_NOPS (op1);
7207
7208 /* If an error was already reported for one of the arguments,
7209 avoid reporting another error. */
7210
7211 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7212 return error_mark_node;
7213
7214 switch (code)
de520661 7215 {
3e4093b6
RS
7216 case PLUS_EXPR:
7217 /* Handle the pointer + int case. */
7218 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7219 return pointer_int_sum (PLUS_EXPR, op0, op1);
7220 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7221 return pointer_int_sum (PLUS_EXPR, op1, op0);
fe67cf58 7222 else
3e4093b6
RS
7223 common = 1;
7224 break;
400fbf9f 7225
3e4093b6
RS
7226 case MINUS_EXPR:
7227 /* Subtraction of two similar pointers.
7228 We must subtract them as integers, then divide by object size. */
7229 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7230 && comp_target_types (type0, type1, 1))
7231 return pointer_diff (op0, op1);
7232 /* Handle pointer minus int. Just like pointer plus int. */
7233 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7234 return pointer_int_sum (MINUS_EXPR, op0, op1);
7235 else
7236 common = 1;
7237 break;
8b6a5902 7238
3e4093b6
RS
7239 case MULT_EXPR:
7240 common = 1;
7241 break;
7242
7243 case TRUNC_DIV_EXPR:
7244 case CEIL_DIV_EXPR:
7245 case FLOOR_DIV_EXPR:
7246 case ROUND_DIV_EXPR:
7247 case EXACT_DIV_EXPR:
7248 /* Floating point division by zero is a legitimate way to obtain
7249 infinities and NaNs. */
7250 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7251 warning ("division by zero");
7252
7253 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7254 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7255 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7256 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 7257 {
3a021db2
PB
7258 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7259 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7260 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7261 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7262
3e4093b6
RS
7263 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7264 resultcode = RDIV_EXPR;
7265 else
7266 /* Although it would be tempting to shorten always here, that
7267 loses on some targets, since the modulo instruction is
7268 undefined if the quotient can't be represented in the
7269 computation mode. We shorten only if unsigned or if
7270 dividing by something we know != -1. */
8df83eae 7271 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 7272 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 7273 && !integer_all_onesp (op1)));
3e4093b6
RS
7274 common = 1;
7275 }
7276 break;
de520661 7277
3e4093b6 7278 case BIT_AND_EXPR:
3e4093b6
RS
7279 case BIT_IOR_EXPR:
7280 case BIT_XOR_EXPR:
7281 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7282 shorten = -1;
7283 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7284 common = 1;
7285 break;
7286
7287 case TRUNC_MOD_EXPR:
7288 case FLOOR_MOD_EXPR:
7289 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7290 warning ("division by zero");
de520661 7291
3e4093b6
RS
7292 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7293 {
7294 /* Although it would be tempting to shorten always here, that loses
7295 on some targets, since the modulo instruction is undefined if the
7296 quotient can't be represented in the computation mode. We shorten
7297 only if unsigned or if dividing by something we know != -1. */
8df83eae 7298 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 7299 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 7300 && !integer_all_onesp (op1)));
3e4093b6
RS
7301 common = 1;
7302 }
7303 break;
de520661 7304
3e4093b6
RS
7305 case TRUTH_ANDIF_EXPR:
7306 case TRUTH_ORIF_EXPR:
7307 case TRUTH_AND_EXPR:
7308 case TRUTH_OR_EXPR:
7309 case TRUTH_XOR_EXPR:
7310 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7311 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7312 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7313 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7314 {
7315 /* Result of these operations is always an int,
7316 but that does not mean the operands should be
7317 converted to ints! */
7318 result_type = integer_type_node;
ae2bcd98
RS
7319 op0 = lang_hooks.truthvalue_conversion (op0);
7320 op1 = lang_hooks.truthvalue_conversion (op1);
3e4093b6
RS
7321 converted = 1;
7322 }
7323 break;
eba80994 7324
3e4093b6
RS
7325 /* Shift operations: result has same type as first operand;
7326 always convert second operand to int.
7327 Also set SHORT_SHIFT if shifting rightward. */
de520661 7328
3e4093b6
RS
7329 case RSHIFT_EXPR:
7330 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7331 {
7332 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
b62acd60 7333 {
3e4093b6
RS
7334 if (tree_int_cst_sgn (op1) < 0)
7335 warning ("right shift count is negative");
7336 else
bbb818c6 7337 {
3f75a254 7338 if (!integer_zerop (op1))
3e4093b6
RS
7339 short_shift = 1;
7340
7341 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7342 warning ("right shift count >= width of type");
bbb818c6 7343 }
b62acd60 7344 }
de520661 7345
3e4093b6
RS
7346 /* Use the type of the value to be shifted. */
7347 result_type = type0;
7348 /* Convert the shift-count to an integer, regardless of size
7349 of value being shifted. */
7350 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7351 op1 = convert (integer_type_node, op1);
7352 /* Avoid converting op1 to result_type later. */
7353 converted = 1;
400fbf9f 7354 }
3e4093b6 7355 break;
253b6b82 7356
3e4093b6
RS
7357 case LSHIFT_EXPR:
7358 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7359 {
7360 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
de520661 7361 {
3e4093b6
RS
7362 if (tree_int_cst_sgn (op1) < 0)
7363 warning ("left shift count is negative");
de520661 7364
3e4093b6
RS
7365 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7366 warning ("left shift count >= width of type");
94ba5069 7367 }
de520661 7368
3e4093b6
RS
7369 /* Use the type of the value to be shifted. */
7370 result_type = type0;
7371 /* Convert the shift-count to an integer, regardless of size
7372 of value being shifted. */
7373 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7374 op1 = convert (integer_type_node, op1);
7375 /* Avoid converting op1 to result_type later. */
7376 converted = 1;
400fbf9f 7377 }
3e4093b6 7378 break;
de520661 7379
3e4093b6
RS
7380 case EQ_EXPR:
7381 case NE_EXPR:
7382 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7383 warning ("comparing floating point with == or != is unsafe");
7384 /* Result of comparison is always int,
7385 but don't convert the args to int! */
7386 build_type = integer_type_node;
7387 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
eabe2b29 7388 || code0 == COMPLEX_TYPE)
3e4093b6 7389 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
eabe2b29 7390 || code1 == COMPLEX_TYPE))
3e4093b6
RS
7391 short_compare = 1;
7392 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7393 {
7394 tree tt0 = TREE_TYPE (type0);
7395 tree tt1 = TREE_TYPE (type1);
7396 /* Anything compares with void *. void * compares with anything.
7397 Otherwise, the targets must be compatible
7398 and both must be object or both incomplete. */
7399 if (comp_target_types (type0, type1, 1))
10bc1b1b 7400 result_type = common_pointer_type (type0, type1);
3e4093b6 7401 else if (VOID_TYPE_P (tt0))
ee2990e7 7402 {
3e4093b6
RS
7403 /* op0 != orig_op0 detects the case of something
7404 whose value is 0 but which isn't a valid null ptr const. */
7405 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7406 && TREE_CODE (tt1) == FUNCTION_TYPE)
bda67431
JM
7407 pedwarn ("ISO C forbids comparison of %<void *%>"
7408 " with function pointer");
ee2990e7 7409 }
3e4093b6 7410 else if (VOID_TYPE_P (tt1))
e6834654 7411 {
3e4093b6
RS
7412 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7413 && TREE_CODE (tt0) == FUNCTION_TYPE)
bda67431
JM
7414 pedwarn ("ISO C forbids comparison of %<void *%>"
7415 " with function pointer");
e6834654 7416 }
3e4093b6
RS
7417 else
7418 pedwarn ("comparison of distinct pointer types lacks a cast");
e6834654 7419
3e4093b6
RS
7420 if (result_type == NULL_TREE)
7421 result_type = ptr_type_node;
e6834654 7422 }
3e4093b6
RS
7423 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7424 && integer_zerop (op1))
7425 result_type = type0;
7426 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7427 && integer_zerop (op0))
7428 result_type = type1;
7429 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 7430 {
3e4093b6
RS
7431 result_type = type0;
7432 pedwarn ("comparison between pointer and integer");
de520661 7433 }
3e4093b6 7434 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 7435 {
3e4093b6
RS
7436 result_type = type1;
7437 pedwarn ("comparison between pointer and integer");
8b6a5902 7438 }
3e4093b6 7439 break;
8b6a5902 7440
3e4093b6
RS
7441 case LE_EXPR:
7442 case GE_EXPR:
7443 case LT_EXPR:
7444 case GT_EXPR:
7445 build_type = integer_type_node;
7446 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7447 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7448 short_compare = 1;
7449 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7450 {
7451 if (comp_target_types (type0, type1, 1))
7452 {
10bc1b1b 7453 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
7454 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7455 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7456 pedwarn ("comparison of complete and incomplete pointers");
7457 else if (pedantic
7458 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7459 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7460 }
7461 else
7462 {
7463 result_type = ptr_type_node;
7464 pedwarn ("comparison of distinct pointer types lacks a cast");
7465 }
7466 }
7467 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7468 && integer_zerop (op1))
7469 {
7470 result_type = type0;
7471 if (pedantic || extra_warnings)
7472 pedwarn ("ordered comparison of pointer with integer zero");
7473 }
7474 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7475 && integer_zerop (op0))
7476 {
7477 result_type = type1;
7478 if (pedantic)
7479 pedwarn ("ordered comparison of pointer with integer zero");
7480 }
7481 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7482 {
7483 result_type = type0;
7484 pedwarn ("comparison between pointer and integer");
7485 }
7486 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7487 {
7488 result_type = type1;
7489 pedwarn ("comparison between pointer and integer");
7490 }
7491 break;
64094f6a 7492
3e4093b6 7493 default:
37b2f290 7494 gcc_unreachable ();
c9fe6f9f 7495 }
8f17b5c5 7496
e57e265b
PB
7497 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7498 return error_mark_node;
7499
3e4093b6
RS
7500 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7501 || code0 == VECTOR_TYPE)
7502 &&
7503 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7504 || code1 == VECTOR_TYPE))
400fbf9f 7505 {
3e4093b6 7506 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
39b726dd 7507
3e4093b6 7508 if (shorten || common || short_compare)
ccf7f880 7509 result_type = c_common_type (type0, type1);
400fbf9f 7510
3e4093b6
RS
7511 /* For certain operations (which identify themselves by shorten != 0)
7512 if both args were extended from the same smaller type,
7513 do the arithmetic in that type and then extend.
400fbf9f 7514
3e4093b6
RS
7515 shorten !=0 and !=1 indicates a bitwise operation.
7516 For them, this optimization is safe only if
7517 both args are zero-extended or both are sign-extended.
7518 Otherwise, we might change the result.
7519 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7520 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 7521
3e4093b6
RS
7522 if (shorten && none_complex)
7523 {
7524 int unsigned0, unsigned1;
7525 tree arg0 = get_narrower (op0, &unsigned0);
7526 tree arg1 = get_narrower (op1, &unsigned1);
7527 /* UNS is 1 if the operation to be done is an unsigned one. */
8df83eae 7528 int uns = TYPE_UNSIGNED (result_type);
3e4093b6 7529 tree type;
400fbf9f 7530
3e4093b6 7531 final_type = result_type;
70768eda 7532
3e4093b6
RS
7533 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7534 but it *requires* conversion to FINAL_TYPE. */
70768eda 7535
3e4093b6
RS
7536 if ((TYPE_PRECISION (TREE_TYPE (op0))
7537 == TYPE_PRECISION (TREE_TYPE (arg0)))
7538 && TREE_TYPE (op0) != final_type)
8df83eae 7539 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3e4093b6
RS
7540 if ((TYPE_PRECISION (TREE_TYPE (op1))
7541 == TYPE_PRECISION (TREE_TYPE (arg1)))
7542 && TREE_TYPE (op1) != final_type)
8df83eae 7543 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
88a3dbc1 7544
3e4093b6 7545 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
abe80e6d 7546
3e4093b6
RS
7547 /* For bitwise operations, signedness of nominal type
7548 does not matter. Consider only how operands were extended. */
7549 if (shorten == -1)
7550 uns = unsigned0;
abe80e6d 7551
3e4093b6
RS
7552 /* Note that in all three cases below we refrain from optimizing
7553 an unsigned operation on sign-extended args.
7554 That would not be valid. */
abe80e6d 7555
3e4093b6
RS
7556 /* Both args variable: if both extended in same way
7557 from same width, do it in that width.
7558 Do it unsigned if args were zero-extended. */
7559 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7560 < TYPE_PRECISION (result_type))
7561 && (TYPE_PRECISION (TREE_TYPE (arg1))
7562 == TYPE_PRECISION (TREE_TYPE (arg0)))
7563 && unsigned0 == unsigned1
7564 && (unsigned0 || !uns))
7565 result_type
7566 = c_common_signed_or_unsigned_type
ccf7f880 7567 (unsigned0, c_common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
3e4093b6
RS
7568 else if (TREE_CODE (arg0) == INTEGER_CST
7569 && (unsigned1 || !uns)
7570 && (TYPE_PRECISION (TREE_TYPE (arg1))
7571 < TYPE_PRECISION (result_type))
7572 && (type
7573 = c_common_signed_or_unsigned_type (unsigned1,
7574 TREE_TYPE (arg1)),
7575 int_fits_type_p (arg0, type)))
7576 result_type = type;
7577 else if (TREE_CODE (arg1) == INTEGER_CST
7578 && (unsigned0 || !uns)
7579 && (TYPE_PRECISION (TREE_TYPE (arg0))
7580 < TYPE_PRECISION (result_type))
7581 && (type
7582 = c_common_signed_or_unsigned_type (unsigned0,
7583 TREE_TYPE (arg0)),
7584 int_fits_type_p (arg1, type)))
7585 result_type = type;
7586 }
88a3dbc1 7587
3e4093b6 7588 /* Shifts can be shortened if shifting right. */
2f6e4e97 7589
3e4093b6
RS
7590 if (short_shift)
7591 {
7592 int unsigned_arg;
7593 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 7594
3e4093b6 7595 final_type = result_type;
abe80e6d 7596
3e4093b6 7597 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 7598 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 7599
3e4093b6
RS
7600 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7601 /* We can shorten only if the shift count is less than the
7602 number of bits in the smaller type size. */
7603 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7604 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 7605 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
7606 {
7607 /* Do an unsigned shift if the operand was zero-extended. */
7608 result_type
7609 = c_common_signed_or_unsigned_type (unsigned_arg,
7610 TREE_TYPE (arg0));
7611 /* Convert value-to-be-shifted to that type. */
7612 if (TREE_TYPE (op0) != result_type)
7613 op0 = convert (result_type, op0);
7614 converted = 1;
abe80e6d 7615 }
88a3dbc1
RK
7616 }
7617
3e4093b6
RS
7618 /* Comparison operations are shortened too but differently.
7619 They identify themselves by setting short_compare = 1. */
56cb9733 7620
3e4093b6
RS
7621 if (short_compare)
7622 {
7623 /* Don't write &op0, etc., because that would prevent op0
7624 from being kept in a register.
7625 Instead, make copies of the our local variables and
7626 pass the copies by reference, then copy them back afterward. */
7627 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7628 enum tree_code xresultcode = resultcode;
7629 tree val
7630 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 7631
3e4093b6
RS
7632 if (val != 0)
7633 return val;
8f17b5c5 7634
3e4093b6
RS
7635 op0 = xop0, op1 = xop1;
7636 converted = 1;
7637 resultcode = xresultcode;
8f17b5c5 7638
3e4093b6
RS
7639 if (warn_sign_compare && skip_evaluation == 0)
7640 {
3f75a254
JM
7641 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7642 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3e4093b6
RS
7643 int unsignedp0, unsignedp1;
7644 tree primop0 = get_narrower (op0, &unsignedp0);
7645 tree primop1 = get_narrower (op1, &unsignedp1);
400fbf9f 7646
3e4093b6
RS
7647 xop0 = orig_op0;
7648 xop1 = orig_op1;
7649 STRIP_TYPE_NOPS (xop0);
7650 STRIP_TYPE_NOPS (xop1);
e89a9554 7651
3e4093b6
RS
7652 /* Give warnings for comparisons between signed and unsigned
7653 quantities that may fail.
e89a9554 7654
3e4093b6
RS
7655 Do the checking based on the original operand trees, so that
7656 casts will be considered, but default promotions won't be.
400fbf9f 7657
3e4093b6
RS
7658 Do not warn if the comparison is being done in a signed type,
7659 since the signed type will only be chosen if it can represent
7660 all the values of the unsigned type. */
3f75a254 7661 if (!TYPE_UNSIGNED (result_type))
3e4093b6
RS
7662 /* OK */;
7663 /* Do not warn if both operands are the same signedness. */
7664 else if (op0_signed == op1_signed)
7665 /* OK */;
7666 else
7667 {
7668 tree sop, uop;
8f17b5c5 7669
3e4093b6
RS
7670 if (op0_signed)
7671 sop = xop0, uop = xop1;
7672 else
7673 sop = xop1, uop = xop0;
8f17b5c5 7674
3e4093b6
RS
7675 /* Do not warn if the signed quantity is an
7676 unsuffixed integer literal (or some static
7677 constant expression involving such literals or a
7678 conditional expression involving such literals)
7679 and it is non-negative. */
3a5b9284 7680 if (tree_expr_nonnegative_p (sop))
3e4093b6
RS
7681 /* OK */;
7682 /* Do not warn if the comparison is an equality operation,
7683 the unsigned quantity is an integral constant, and it
7684 would fit in the result if the result were signed. */
7685 else if (TREE_CODE (uop) == INTEGER_CST
7686 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7687 && int_fits_type_p
7688 (uop, c_common_signed_type (result_type)))
7689 /* OK */;
7690 /* Do not warn if the unsigned quantity is an enumeration
7691 constant and its maximum value would fit in the result
7692 if the result were signed. */
7693 else if (TREE_CODE (uop) == INTEGER_CST
7694 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7695 && int_fits_type_p
3f75a254 7696 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
3e4093b6
RS
7697 c_common_signed_type (result_type)))
7698 /* OK */;
7699 else
7700 warning ("comparison between signed and unsigned");
7701 }
8f17b5c5 7702
3e4093b6
RS
7703 /* Warn if two unsigned values are being compared in a size
7704 larger than their original size, and one (and only one) is the
7705 result of a `~' operator. This comparison will always fail.
8f17b5c5 7706
3e4093b6
RS
7707 Also warn if one operand is a constant, and the constant
7708 does not have all bits set that are set in the ~ operand
7709 when it is extended. */
8f17b5c5 7710
3e4093b6
RS
7711 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7712 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7713 {
7714 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7715 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7716 &unsignedp0);
7717 else
7718 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7719 &unsignedp1);
64094f6a 7720
3e4093b6
RS
7721 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7722 {
7723 tree primop;
7724 HOST_WIDE_INT constant, mask;
7725 int unsignedp, bits;
2ad1815d 7726
3e4093b6
RS
7727 if (host_integerp (primop0, 0))
7728 {
7729 primop = primop1;
7730 unsignedp = unsignedp1;
7731 constant = tree_low_cst (primop0, 0);
7732 }
7733 else
7734 {
7735 primop = primop0;
7736 unsignedp = unsignedp0;
7737 constant = tree_low_cst (primop1, 0);
7738 }
7739
7740 bits = TYPE_PRECISION (TREE_TYPE (primop));
7741 if (bits < TYPE_PRECISION (result_type)
7742 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7743 {
3f75a254 7744 mask = (~(HOST_WIDE_INT) 0) << bits;
3e4093b6
RS
7745 if ((mask & constant) != mask)
7746 warning ("comparison of promoted ~unsigned with constant");
7747 }
7748 }
7749 else if (unsignedp0 && unsignedp1
7750 && (TYPE_PRECISION (TREE_TYPE (primop0))
7751 < TYPE_PRECISION (result_type))
7752 && (TYPE_PRECISION (TREE_TYPE (primop1))
7753 < TYPE_PRECISION (result_type)))
7754 warning ("comparison of promoted ~unsigned with unsigned");
7755 }
7756 }
2ad1815d 7757 }
64094f6a 7758 }
64094f6a 7759
3e4093b6
RS
7760 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7761 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7762 Then the expression will be built.
7763 It will be given type FINAL_TYPE if that is nonzero;
7764 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 7765
3e4093b6
RS
7766 if (!result_type)
7767 {
7768 binary_op_error (code);
7769 return error_mark_node;
7770 }
400fbf9f 7771
3f75a254 7772 if (!converted)
3e4093b6
RS
7773 {
7774 if (TREE_TYPE (op0) != result_type)
7775 op0 = convert (result_type, op0);
7776 if (TREE_TYPE (op1) != result_type)
7777 op1 = convert (result_type, op1);
d97c6333
JW
7778
7779 /* This can happen if one operand has a vector type, and the other
7780 has a different type. */
7781 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7782 return error_mark_node;
3e4093b6 7783 }
400fbf9f 7784
3e4093b6
RS
7785 if (build_type == NULL_TREE)
7786 build_type = result_type;
400fbf9f 7787
3e4093b6 7788 {
53fb4de3 7789 tree result = build2 (resultcode, build_type, op0, op1);
3e4093b6
RS
7790
7791 /* Treat expressions in initializers specially as they can't trap. */
bf730f15
RS
7792 result = require_constant_value ? fold_initializer (result)
7793 : fold (result);
6de9cd9a 7794
3e4093b6 7795 if (final_type != 0)
6de9cd9a
DN
7796 result = convert (final_type, result);
7797 return result;
3e4093b6 7798 }
400fbf9f 7799}
This page took 3.501633 seconds and 5 git commands to generate.