1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned
;
52 static tree
qualify_type (tree
, tree
);
53 static int same_translation_unit_p (tree
, tree
);
54 static int tagged_types_tu_compatible_p (tree
, tree
, int);
55 static int comp_target_types (tree
, tree
, int);
56 static int function_types_compatible_p (tree
, tree
, int);
57 static int type_lists_compatible_p (tree
, tree
, int);
58 static tree
decl_constant_value_for_broken_optimization (tree
);
59 static tree
default_function_array_conversion (tree
);
60 static tree
lookup_field (tree
, tree
);
61 static tree
convert_arguments (tree
, tree
, tree
, tree
);
62 static tree
pointer_diff (tree
, tree
);
63 static tree
unary_complex_lvalue (enum tree_code
, tree
, int);
64 static void pedantic_lvalue_warning (enum tree_code
);
65 static tree
internal_build_compound_expr (tree
, int);
66 static tree
convert_for_assignment (tree
, tree
, const char *, tree
, tree
,
68 static void warn_for_assignment (const char *, const char *, tree
, int);
69 static tree
valid_compound_expr_initializer (tree
, tree
);
70 static void push_string (const char *);
71 static void push_member_name (tree
);
72 static void push_array_bounds (int);
73 static int spelling_length (void);
74 static char *print_spelling (char *);
75 static void warning_init (const char *);
76 static tree
digest_init (tree
, tree
, int);
77 static void output_init_element (tree
, tree
, tree
, int);
78 static void output_pending_init_elements (int);
79 static int set_designator (int);
80 static void push_range_stack (tree
);
81 static void add_pending_init (tree
, tree
);
82 static void set_nonincremental_init (void);
83 static void set_nonincremental_init_from_string (tree
);
84 static tree
find_init_member (tree
);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
90 require_complete_type (tree value
)
92 tree type
= TREE_TYPE (value
);
94 if (value
== error_mark_node
|| type
== error_mark_node
)
95 return error_mark_node
;
97 /* First, detect a valid value with a complete type. */
98 if (COMPLETE_TYPE_P (type
))
101 c_incomplete_type_error (value
, type
);
102 return error_mark_node
;
105 /* Print an error message for invalid use of an incomplete type.
106 VALUE is the expression that was used (or 0 if that isn't known)
107 and TYPE is the type that was invalid. */
110 c_incomplete_type_error (tree value
, tree type
)
112 const char *type_code_string
;
114 /* Avoid duplicate error message. */
115 if (TREE_CODE (type
) == ERROR_MARK
)
118 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
119 || TREE_CODE (value
) == PARM_DECL
))
120 error ("`%s' has an incomplete type",
121 IDENTIFIER_POINTER (DECL_NAME (value
)));
125 /* We must print an error message. Be clever about what it says. */
127 switch (TREE_CODE (type
))
130 type_code_string
= "struct";
134 type_code_string
= "union";
138 type_code_string
= "enum";
142 error ("invalid use of void expression");
146 if (TYPE_DOMAIN (type
))
148 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
150 error ("invalid use of flexible array member");
153 type
= TREE_TYPE (type
);
156 error ("invalid use of array with unspecified bounds");
163 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
164 error ("invalid use of undefined type `%s %s'",
165 type_code_string
, IDENTIFIER_POINTER (TYPE_NAME (type
)));
167 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
168 error ("invalid use of incomplete typedef `%s'",
169 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
))));
173 /* Given a type, apply default promotions wrt unnamed function
174 arguments and return the new type. */
177 c_type_promotes_to (tree type
)
179 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
180 return double_type_node
;
182 if (c_promoting_integer_type_p (type
))
184 /* Preserve unsignedness if not really getting any wider. */
185 if (TREE_UNSIGNED (type
)
186 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
187 return unsigned_type_node
;
188 return integer_type_node
;
194 /* Return a variant of TYPE which has all the type qualifiers of LIKE
195 as well as those of TYPE. */
198 qualify_type (tree type
, tree like
)
200 return c_build_qualified_type (type
,
201 TYPE_QUALS (type
) | TYPE_QUALS (like
));
204 /* Return the common type of two types.
205 We assume that comptypes has already been done and returned 1;
206 if that isn't so, this may crash. In particular, we assume that qualifiers
209 This is the type for the result of most arithmetic operations
210 if the operands have the given two types. */
213 common_type (tree t1
, tree t2
)
215 enum tree_code code1
;
216 enum tree_code code2
;
219 /* Save time if the two types are the same. */
221 if (t1
== t2
) return t1
;
223 /* If one type is nonsense, use the other. */
224 if (t1
== error_mark_node
)
226 if (t2
== error_mark_node
)
229 /* Merge the attributes. */
230 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
232 /* Treat an enum type as the unsigned integer type of the same width. */
234 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
235 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
236 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
237 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
239 code1
= TREE_CODE (t1
);
240 code2
= TREE_CODE (t2
);
242 /* If one type is complex, form the common type of the non-complex
243 components, then make that complex. Use T1 or T2 if it is the
245 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
247 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
248 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
249 tree subtype
= common_type (subtype1
, subtype2
);
251 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
252 return build_type_attribute_variant (t1
, attributes
);
253 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
254 return build_type_attribute_variant (t2
, attributes
);
256 return build_type_attribute_variant (build_complex_type (subtype
),
264 /* If only one is real, use it as the result. */
266 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
267 return build_type_attribute_variant (t1
, attributes
);
269 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
270 return build_type_attribute_variant (t2
, attributes
);
272 /* Both real or both integers; use the one with greater precision. */
274 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
275 return build_type_attribute_variant (t1
, attributes
);
276 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
277 return build_type_attribute_variant (t2
, attributes
);
279 /* Same precision. Prefer longs to ints even when same size. */
281 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
282 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
283 return build_type_attribute_variant (long_unsigned_type_node
,
286 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
287 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
289 /* But preserve unsignedness from the other type,
290 since long cannot hold all the values of an unsigned int. */
291 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
292 t1
= long_unsigned_type_node
;
294 t1
= long_integer_type_node
;
295 return build_type_attribute_variant (t1
, attributes
);
298 /* Likewise, prefer long double to double even if same size. */
299 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
300 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
301 return build_type_attribute_variant (long_double_type_node
,
304 /* Otherwise prefer the unsigned one. */
306 if (TREE_UNSIGNED (t1
))
307 return build_type_attribute_variant (t1
, attributes
);
309 return build_type_attribute_variant (t2
, attributes
);
312 /* For two pointers, do this recursively on the target type,
313 and combine the qualifiers of the two types' targets. */
314 /* This code was turned off; I don't know why.
315 But ANSI C specifies doing this with the qualifiers.
316 So I turned it on again. */
318 tree pointed_to_1
= TREE_TYPE (t1
);
319 tree pointed_to_2
= TREE_TYPE (t2
);
320 tree target
= common_type (TYPE_MAIN_VARIANT (pointed_to_1
),
321 TYPE_MAIN_VARIANT (pointed_to_2
));
322 t1
= build_pointer_type (c_build_qualified_type
324 TYPE_QUALS (pointed_to_1
) |
325 TYPE_QUALS (pointed_to_2
)));
326 return build_type_attribute_variant (t1
, attributes
);
331 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
332 /* Save space: see if the result is identical to one of the args. */
333 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
334 return build_type_attribute_variant (t1
, attributes
);
335 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
336 return build_type_attribute_variant (t2
, attributes
);
337 /* Merge the element types, and have a size if either arg has one. */
338 t1
= build_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
339 return build_type_attribute_variant (t1
, attributes
);
343 /* Function types: prefer the one that specified arg types.
344 If both do, merge the arg types. Also merge the return types. */
346 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
347 tree p1
= TYPE_ARG_TYPES (t1
);
348 tree p2
= TYPE_ARG_TYPES (t2
);
353 /* Save space: see if the result is identical to one of the args. */
354 if (valtype
== TREE_TYPE (t1
) && ! TYPE_ARG_TYPES (t2
))
355 return build_type_attribute_variant (t1
, attributes
);
356 if (valtype
== TREE_TYPE (t2
) && ! TYPE_ARG_TYPES (t1
))
357 return build_type_attribute_variant (t2
, attributes
);
359 /* Simple way if one arg fails to specify argument types. */
360 if (TYPE_ARG_TYPES (t1
) == 0)
362 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
363 return build_type_attribute_variant (t1
, attributes
);
365 if (TYPE_ARG_TYPES (t2
) == 0)
367 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
368 return build_type_attribute_variant (t1
, attributes
);
371 /* If both args specify argument types, we must merge the two
372 lists, argument by argument. */
375 declare_parm_level ();
377 len
= list_length (p1
);
380 for (i
= 0; i
< len
; i
++)
381 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
386 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
388 /* A null type means arg type is not specified.
389 Take whatever the other function type has. */
390 if (TREE_VALUE (p1
) == 0)
392 TREE_VALUE (n
) = TREE_VALUE (p2
);
395 if (TREE_VALUE (p2
) == 0)
397 TREE_VALUE (n
) = TREE_VALUE (p1
);
401 /* Given wait (union {union wait *u; int *i} *)
402 and wait (union wait *),
403 prefer union wait * as type of parm. */
404 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
405 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
408 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
409 memb
; memb
= TREE_CHAIN (memb
))
410 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p2
),
413 TREE_VALUE (n
) = TREE_VALUE (p2
);
415 pedwarn ("function types not truly compatible in ISO C");
419 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
420 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
423 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
424 memb
; memb
= TREE_CHAIN (memb
))
425 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p1
),
428 TREE_VALUE (n
) = TREE_VALUE (p1
);
430 pedwarn ("function types not truly compatible in ISO C");
434 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
440 t1
= build_function_type (valtype
, newargs
);
441 /* ... falls through ... */
445 return build_type_attribute_variant (t1
, attributes
);
450 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
451 or various other operations. Return 2 if they are compatible
452 but a warning may be needed if you use them together. */
455 comptypes (tree type1
, tree type2
, int flags
)
461 /* Suppress errors caused by previously reported errors. */
463 if (t1
== t2
|| !t1
|| !t2
464 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
467 /* If either type is the internal version of sizetype, return the
469 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
470 && TYPE_DOMAIN (t1
) != 0)
471 t1
= TYPE_DOMAIN (t1
);
473 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
474 && TYPE_DOMAIN (t2
) != 0)
475 t2
= TYPE_DOMAIN (t2
);
477 /* Enumerated types are compatible with integer types, but this is
478 not transitive: two enumerated types in the same translation unit
479 are compatible with each other only if they are the same type. */
481 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
482 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TREE_UNSIGNED (t1
));
483 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
484 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TREE_UNSIGNED (t2
));
489 /* Different classes of types can't be compatible. */
491 if (TREE_CODE (t1
) != TREE_CODE (t2
)) return 0;
493 /* Qualifiers must match. */
495 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
498 /* Allow for two different type nodes which have essentially the same
499 definition. Note that we already checked for equality of the type
500 qualifiers (just above). */
502 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
505 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
506 if (! (attrval
= (*targetm
.comp_type_attributes
) (t1
, t2
)))
509 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
512 switch (TREE_CODE (t1
))
515 /* We must give ObjC the first crack at comparing pointers, since
516 protocol qualifiers may be involved. */
517 if (c_dialect_objc () && (val
= objc_comptypes (t1
, t2
, 0)) >= 0)
519 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
520 ? 1 : comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), flags
));
524 val
= function_types_compatible_p (t1
, t2
, flags
);
529 tree d1
= TYPE_DOMAIN (t1
);
530 tree d2
= TYPE_DOMAIN (t2
);
531 bool d1_variable
, d2_variable
;
532 bool d1_zero
, d2_zero
;
535 /* Target types must match incl. qualifiers. */
536 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
537 && 0 == (val
= comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
),
541 /* Sizes must match unless one is missing or variable. */
542 if (d1
== 0 || d2
== 0 || d1
== d2
)
545 d1_zero
= ! TYPE_MAX_VALUE (d1
);
546 d2_zero
= ! TYPE_MAX_VALUE (d2
);
548 d1_variable
= (! d1_zero
549 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
550 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
551 d2_variable
= (! d2_zero
552 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
553 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
555 if (d1_variable
|| d2_variable
)
557 if (d1_zero
&& d2_zero
)
559 if (d1_zero
|| d2_zero
560 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
561 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
568 /* We are dealing with two distinct structs. In assorted Objective-C
569 corner cases, however, these can still be deemed equivalent. */
570 if (c_dialect_objc () && objc_comptypes (t1
, t2
, 0) == 1)
575 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
576 val
= tagged_types_tu_compatible_p (t1
, t2
, flags
);
580 /* The target might allow certain vector types to be compatible. */
581 val
= (*targetm
.vector_opaque_p
) (t1
)
582 || (*targetm
.vector_opaque_p
) (t2
);
588 return attrval
== 2 && val
== 1 ? 2 : val
;
591 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
592 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
593 to 1 or 0 depending if the check of the pointer types is meant to
594 be reflexive or not (typically, assignments are not reflexive,
595 while comparisons are reflexive).
599 comp_target_types (tree ttl
, tree ttr
, int reflexive
)
603 /* Give objc_comptypes a crack at letting these types through. */
604 if ((val
= objc_comptypes (ttl
, ttr
, reflexive
)) >= 0)
607 val
= comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl
)),
608 TYPE_MAIN_VARIANT (TREE_TYPE (ttr
)), COMPARE_STRICT
);
610 if (val
== 2 && pedantic
)
611 pedwarn ("types are not quite compatible");
615 /* Subroutines of `comptypes'. */
617 /* Determine whether two types derive from the same translation unit.
618 If the CONTEXT chain ends in a null, that type's context is still
619 being parsed, so if two types have context chains ending in null,
620 they're in the same translation unit. */
622 same_translation_unit_p (tree t1
, tree t2
)
624 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
625 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
627 case 'd': t1
= DECL_CONTEXT (t1
); break;
628 case 't': t1
= TYPE_CONTEXT (t1
); break;
629 case 'b': t1
= BLOCK_SUPERCONTEXT (t1
); break;
633 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
634 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
636 case 'd': t2
= DECL_CONTEXT (t1
); break;
637 case 't': t2
= TYPE_CONTEXT (t2
); break;
638 case 'b': t2
= BLOCK_SUPERCONTEXT (t2
); break;
645 /* The C standard says that two structures in different translation
646 units are compatible with each other only if the types of their
647 fields are compatible (among other things). So, consider two copies
648 of this structure: */
650 struct tagged_tu_seen
{
651 const struct tagged_tu_seen
* next
;
656 /* Can they be compatible with each other? We choose to break the
657 recursion by allowing those types to be compatible. */
659 static const struct tagged_tu_seen
* tagged_tu_seen_base
;
661 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
662 compatible. If the two types are not the same (which has been
663 checked earlier), this can only happen when multiple translation
664 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
668 tagged_types_tu_compatible_p (tree t1
, tree t2
, int flags
)
671 bool needs_warning
= false;
673 /* We have to verify that the tags of the types are the same. This
674 is harder than it looks because this may be a typedef, so we have
675 to go look at the original type. It may even be a typedef of a
677 while (TYPE_NAME (t1
) && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
)
678 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
680 while (TYPE_NAME (t2
) && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
)
681 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
683 /* C90 didn't have the requirement that the two tags be the same. */
684 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
687 /* C90 didn't say what happened if one or both of the types were
688 incomplete; we choose to follow C99 rules here, which is that they
690 if (TYPE_SIZE (t1
) == NULL
691 || TYPE_SIZE (t2
) == NULL
)
695 const struct tagged_tu_seen
* tts_i
;
696 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
697 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
701 switch (TREE_CODE (t1
))
705 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
708 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
710 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
712 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
720 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
723 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= TREE_CHAIN (s1
))
726 struct tagged_tu_seen tts
;
728 tts
.next
= tagged_tu_seen_base
;
731 tagged_tu_seen_base
= &tts
;
733 if (DECL_NAME (s1
) != NULL
)
734 for (s2
= TYPE_VALUES (t2
); s2
; s2
= TREE_CHAIN (s2
))
735 if (DECL_NAME (s1
) == DECL_NAME (s2
))
738 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
), flags
);
742 needs_warning
= true;
744 if (TREE_CODE (s1
) == FIELD_DECL
745 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
746 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
752 tagged_tu_seen_base
= tts
.next
;
756 return needs_warning
? 2 : 1;
761 struct tagged_tu_seen tts
;
763 tts
.next
= tagged_tu_seen_base
;
766 tagged_tu_seen_base
= &tts
;
768 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
770 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
773 if (TREE_CODE (s1
) != TREE_CODE (s2
)
774 || DECL_NAME (s1
) != DECL_NAME (s2
))
776 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
), flags
);
780 needs_warning
= true;
782 if (TREE_CODE (s1
) == FIELD_DECL
783 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
784 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
787 tagged_tu_seen_base
= tts
.next
;
790 return needs_warning
? 2 : 1;
798 /* Return 1 if two function types F1 and F2 are compatible.
799 If either type specifies no argument types,
800 the other must specify a fixed number of self-promoting arg types.
801 Otherwise, if one type specifies only the number of arguments,
802 the other must specify that number of self-promoting arg types.
803 Otherwise, the argument types must match. */
806 function_types_compatible_p (tree f1
, tree f2
, int flags
)
809 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
814 ret1
= TREE_TYPE (f1
);
815 ret2
= TREE_TYPE (f2
);
817 /* 'volatile' qualifiers on a function's return type mean the function
819 if (pedantic
&& TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
820 pedwarn ("function return types not compatible due to `volatile'");
821 if (TYPE_VOLATILE (ret1
))
822 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
823 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
824 if (TYPE_VOLATILE (ret2
))
825 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
826 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
827 val
= comptypes (ret1
, ret2
, flags
);
831 args1
= TYPE_ARG_TYPES (f1
);
832 args2
= TYPE_ARG_TYPES (f2
);
834 /* An unspecified parmlist matches any specified parmlist
835 whose argument types don't need default promotions. */
839 if (!self_promoting_args_p (args2
))
841 /* If one of these types comes from a non-prototype fn definition,
842 compare that with the other type's arglist.
843 If they don't match, ask for a warning (but no error). */
844 if (TYPE_ACTUAL_ARG_TYPES (f1
)
845 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
852 if (!self_promoting_args_p (args1
))
854 if (TYPE_ACTUAL_ARG_TYPES (f2
)
855 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
861 /* Both types have argument lists: compare them and propagate results. */
862 val1
= type_lists_compatible_p (args1
, args2
, flags
);
863 return val1
!= 1 ? val1
: val
;
866 /* Check two lists of types for compatibility,
867 returning 0 for incompatible, 1 for compatible,
868 or 2 for compatible with warning. */
871 type_lists_compatible_p (tree args1
, tree args2
, int flags
)
873 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
879 if (args1
== 0 && args2
== 0)
881 /* If one list is shorter than the other,
882 they fail to match. */
883 if (args1
== 0 || args2
== 0)
885 /* A null pointer instead of a type
886 means there is supposed to be an argument
887 but nothing is specified about what type it has.
888 So match anything that self-promotes. */
889 if (TREE_VALUE (args1
) == 0)
891 if (c_type_promotes_to (TREE_VALUE (args2
)) != TREE_VALUE (args2
))
894 else if (TREE_VALUE (args2
) == 0)
896 if (c_type_promotes_to (TREE_VALUE (args1
)) != TREE_VALUE (args1
))
899 /* If one of the lists has an error marker, ignore this arg. */
900 else if (TREE_CODE (TREE_VALUE (args1
)) == ERROR_MARK
901 || TREE_CODE (TREE_VALUE (args2
)) == ERROR_MARK
)
903 else if (! (newval
= comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1
)),
904 TYPE_MAIN_VARIANT (TREE_VALUE (args2
)),
907 /* Allow wait (union {union wait *u; int *i} *)
908 and wait (union wait *) to be compatible. */
909 if (TREE_CODE (TREE_VALUE (args1
)) == UNION_TYPE
910 && (TYPE_NAME (TREE_VALUE (args1
)) == 0
911 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1
)))
912 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1
))) == INTEGER_CST
913 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1
)),
914 TYPE_SIZE (TREE_VALUE (args2
))))
917 for (memb
= TYPE_FIELDS (TREE_VALUE (args1
));
918 memb
; memb
= TREE_CHAIN (memb
))
919 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args2
),
925 else if (TREE_CODE (TREE_VALUE (args2
)) == UNION_TYPE
926 && (TYPE_NAME (TREE_VALUE (args2
)) == 0
927 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2
)))
928 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2
))) == INTEGER_CST
929 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2
)),
930 TYPE_SIZE (TREE_VALUE (args1
))))
933 for (memb
= TYPE_FIELDS (TREE_VALUE (args2
));
934 memb
; memb
= TREE_CHAIN (memb
))
935 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args1
),
945 /* comptypes said ok, but record if it said to warn. */
949 args1
= TREE_CHAIN (args1
);
950 args2
= TREE_CHAIN (args2
);
954 /* Compute the size to increment a pointer by. */
957 c_size_in_bytes (tree type
)
959 enum tree_code code
= TREE_CODE (type
);
961 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
962 return size_one_node
;
964 if (!COMPLETE_OR_VOID_TYPE_P (type
))
966 error ("arithmetic on pointer to an incomplete type");
967 return size_one_node
;
970 /* Convert in case a char is more than one unit. */
971 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
972 size_int (TYPE_PRECISION (char_type_node
)
976 /* Return either DECL or its known constant value (if it has one). */
979 decl_constant_value (tree decl
)
981 if (/* Don't change a variable array bound or initial value to a constant
982 in a place where a variable is invalid. */
983 current_function_decl
!= 0
984 && ! TREE_THIS_VOLATILE (decl
)
985 && TREE_READONLY (decl
)
986 && DECL_INITIAL (decl
) != 0
987 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
988 /* This is invalid if initial value is not constant.
989 If it has either a function call, a memory reference,
990 or a variable, then re-evaluating it could give different results. */
991 && TREE_CONSTANT (DECL_INITIAL (decl
))
992 /* Check for cases where this is sub-optimal, even though valid. */
993 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
994 return DECL_INITIAL (decl
);
998 /* Return either DECL or its known constant value (if it has one), but
999 return DECL if pedantic or DECL has mode BLKmode. This is for
1000 bug-compatibility with the old behavior of decl_constant_value
1001 (before GCC 3.0); every use of this function is a bug and it should
1002 be removed before GCC 3.1. It is not appropriate to use pedantic
1003 in a way that affects optimization, and BLKmode is probably not the
1004 right test for avoiding misoptimizations either. */
1007 decl_constant_value_for_broken_optimization (tree decl
)
1009 if (pedantic
|| DECL_MODE (decl
) == BLKmode
)
1012 return decl_constant_value (decl
);
1016 /* Perform the default conversion of arrays and functions to pointers.
1017 Return the result of converting EXP. For any other expression, just
1021 default_function_array_conversion (tree exp
)
1024 tree type
= TREE_TYPE (exp
);
1025 enum tree_code code
= TREE_CODE (type
);
1028 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1031 Do not use STRIP_NOPS here! It will remove conversions from pointer
1032 to integer and cause infinite recursion. */
1034 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1035 || (TREE_CODE (exp
) == NOP_EXPR
1036 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1038 if (TREE_CODE (exp
) == NON_LVALUE_EXPR
)
1040 exp
= TREE_OPERAND (exp
, 0);
1043 /* Preserve the original expression code. */
1044 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp
))))
1045 C_SET_EXP_ORIGINAL_CODE (exp
, C_EXP_ORIGINAL_CODE (orig_exp
));
1047 if (code
== FUNCTION_TYPE
)
1049 return build_unary_op (ADDR_EXPR
, exp
, 0);
1051 if (code
== ARRAY_TYPE
)
1054 tree restype
= TREE_TYPE (type
);
1060 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r' || DECL_P (exp
))
1062 constp
= TREE_READONLY (exp
);
1063 volatilep
= TREE_THIS_VOLATILE (exp
);
1066 if (TYPE_QUALS (type
) || constp
|| volatilep
)
1068 = c_build_qualified_type (restype
,
1070 | (constp
* TYPE_QUAL_CONST
)
1071 | (volatilep
* TYPE_QUAL_VOLATILE
));
1073 if (TREE_CODE (exp
) == INDIRECT_REF
)
1074 return convert (TYPE_POINTER_TO (restype
),
1075 TREE_OPERAND (exp
, 0));
1077 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1079 tree op1
= default_conversion (TREE_OPERAND (exp
, 1));
1080 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1081 TREE_OPERAND (exp
, 0), op1
);
1084 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
);
1085 if (!flag_isoc99
&& !lvalue_array_p
)
1087 /* Before C99, non-lvalue arrays do not decay to pointers.
1088 Normally, using such an array would be invalid; but it can
1089 be used correctly inside sizeof or as a statement expression.
1090 Thus, do not give an error here; an error will result later. */
1094 ptrtype
= build_pointer_type (restype
);
1096 if (TREE_CODE (exp
) == VAR_DECL
)
1098 /* ??? This is not really quite correct
1099 in that the type of the operand of ADDR_EXPR
1100 is not the target type of the type of the ADDR_EXPR itself.
1101 Question is, can this lossage be avoided? */
1102 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1103 if (!c_mark_addressable (exp
))
1104 return error_mark_node
;
1105 TREE_CONSTANT (adr
) = staticp (exp
);
1106 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1109 /* This way is better for a COMPONENT_REF since it can
1110 simplify the offset for a component. */
1111 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1112 return convert (ptrtype
, adr
);
1117 /* Perform default promotions for C data used in expressions.
1118 Arrays and functions are converted to pointers;
1119 enumeral types or short or char, to int.
1120 In addition, manifest constants symbols are replaced by their values. */
1123 default_conversion (tree exp
)
1126 tree type
= TREE_TYPE (exp
);
1127 enum tree_code code
= TREE_CODE (type
);
1129 if (code
== FUNCTION_TYPE
|| code
== ARRAY_TYPE
)
1130 return default_function_array_conversion (exp
);
1132 /* Constants can be used directly unless they're not loadable. */
1133 if (TREE_CODE (exp
) == CONST_DECL
)
1134 exp
= DECL_INITIAL (exp
);
1136 /* Replace a nonvolatile const static variable with its value unless
1137 it is an array, in which case we must be sure that taking the
1138 address of the array produces consistent results. */
1139 else if (optimize
&& TREE_CODE (exp
) == VAR_DECL
&& code
!= ARRAY_TYPE
)
1141 exp
= decl_constant_value_for_broken_optimization (exp
);
1142 type
= TREE_TYPE (exp
);
1145 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1148 Do not use STRIP_NOPS here! It will remove conversions from pointer
1149 to integer and cause infinite recursion. */
1151 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1152 || (TREE_CODE (exp
) == NOP_EXPR
1153 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1154 exp
= TREE_OPERAND (exp
, 0);
1156 /* Preserve the original expression code. */
1157 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp
))))
1158 C_SET_EXP_ORIGINAL_CODE (exp
, C_EXP_ORIGINAL_CODE (orig_exp
));
1160 /* Normally convert enums to int,
1161 but convert wide enums to something wider. */
1162 if (code
== ENUMERAL_TYPE
)
1164 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
1165 TYPE_PRECISION (integer_type_node
)),
1166 ((TYPE_PRECISION (type
)
1167 >= TYPE_PRECISION (integer_type_node
))
1168 && TREE_UNSIGNED (type
)));
1170 return convert (type
, exp
);
1173 if (TREE_CODE (exp
) == COMPONENT_REF
1174 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
1175 /* If it's thinner than an int, promote it like a
1176 c_promoting_integer_type_p, otherwise leave it alone. */
1177 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
1178 TYPE_PRECISION (integer_type_node
)))
1179 return convert (integer_type_node
, exp
);
1181 if (c_promoting_integer_type_p (type
))
1183 /* Preserve unsignedness if not really getting any wider. */
1184 if (TREE_UNSIGNED (type
)
1185 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1186 return convert (unsigned_type_node
, exp
);
1188 return convert (integer_type_node
, exp
);
1191 if (code
== VOID_TYPE
)
1193 error ("void value not ignored as it ought to be");
1194 return error_mark_node
;
1199 /* Look up COMPONENT in a structure or union DECL.
1201 If the component name is not found, returns NULL_TREE. Otherwise,
1202 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1203 stepping down the chain to the component, which is in the last
1204 TREE_VALUE of the list. Normally the list is of length one, but if
1205 the component is embedded within (nested) anonymous structures or
1206 unions, the list steps down the chain to the component. */
1209 lookup_field (tree decl
, tree component
)
1211 tree type
= TREE_TYPE (decl
);
1214 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1215 to the field elements. Use a binary search on this array to quickly
1216 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1217 will always be set for structures which have many elements. */
1219 if (TYPE_LANG_SPECIFIC (type
))
1222 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
1224 field
= TYPE_FIELDS (type
);
1226 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
1227 while (top
- bot
> 1)
1229 half
= (top
- bot
+ 1) >> 1;
1230 field
= field_array
[bot
+half
];
1232 if (DECL_NAME (field
) == NULL_TREE
)
1234 /* Step through all anon unions in linear fashion. */
1235 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
1237 field
= field_array
[bot
++];
1238 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1239 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1241 tree anon
= lookup_field (field
, component
);
1244 return tree_cons (NULL_TREE
, field
, anon
);
1248 /* Entire record is only anon unions. */
1252 /* Restart the binary search, with new lower bound. */
1256 if (DECL_NAME (field
) == component
)
1258 if (DECL_NAME (field
) < component
)
1264 if (DECL_NAME (field_array
[bot
]) == component
)
1265 field
= field_array
[bot
];
1266 else if (DECL_NAME (field
) != component
)
1271 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1273 if (DECL_NAME (field
) == NULL_TREE
1274 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1275 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
1277 tree anon
= lookup_field (field
, component
);
1280 return tree_cons (NULL_TREE
, field
, anon
);
1283 if (DECL_NAME (field
) == component
)
1287 if (field
== NULL_TREE
)
1291 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
1294 /* Make an expression to refer to the COMPONENT field of
1295 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1298 build_component_ref (tree datum
, tree component
)
1300 tree type
= TREE_TYPE (datum
);
1301 enum tree_code code
= TREE_CODE (type
);
1305 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1306 If pedantic ensure that the arguments are not lvalues; otherwise,
1307 if the component is an array, it would wrongly decay to a pointer in
1309 We cannot do this with a COND_EXPR, because in a conditional expression
1310 the default promotions are applied to both sides, and this would yield
1311 the wrong type of the result; for example, if the components have
1313 switch (TREE_CODE (datum
))
1317 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
);
1318 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1319 TREE_OPERAND (datum
, 0), pedantic_non_lvalue (value
));
1325 /* See if there is a field or component with name COMPONENT. */
1327 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1329 if (!COMPLETE_TYPE_P (type
))
1331 c_incomplete_type_error (NULL_TREE
, type
);
1332 return error_mark_node
;
1335 field
= lookup_field (datum
, component
);
1339 error ("%s has no member named `%s'",
1340 code
== RECORD_TYPE
? "structure" : "union",
1341 IDENTIFIER_POINTER (component
));
1342 return error_mark_node
;
1345 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1346 This might be better solved in future the way the C++ front
1347 end does it - by giving the anonymous entities each a
1348 separate name and type, and then have build_component_ref
1349 recursively call itself. We can't do that here. */
1352 tree subdatum
= TREE_VALUE (field
);
1354 if (TREE_TYPE (subdatum
) == error_mark_node
)
1355 return error_mark_node
;
1357 ref
= build (COMPONENT_REF
, TREE_TYPE (subdatum
), datum
, subdatum
);
1358 if (TREE_READONLY (datum
) || TREE_READONLY (subdatum
))
1359 TREE_READONLY (ref
) = 1;
1360 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (subdatum
))
1361 TREE_THIS_VOLATILE (ref
) = 1;
1363 if (TREE_DEPRECATED (subdatum
))
1364 warn_deprecated_use (subdatum
);
1368 field
= TREE_CHAIN (field
);
1374 else if (code
!= ERROR_MARK
)
1375 error ("request for member `%s' in something not a structure or union",
1376 IDENTIFIER_POINTER (component
));
1378 return error_mark_node
;
1381 /* Given an expression PTR for a pointer, return an expression
1382 for the value pointed to.
1383 ERRORSTRING is the name of the operator to appear in error messages. */
1386 build_indirect_ref (tree ptr
, const char *errorstring
)
1388 tree pointer
= default_conversion (ptr
);
1389 tree type
= TREE_TYPE (pointer
);
1391 if (TREE_CODE (type
) == POINTER_TYPE
)
1393 if (TREE_CODE (pointer
) == ADDR_EXPR
1394 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
1395 == TREE_TYPE (type
)))
1396 return TREE_OPERAND (pointer
, 0);
1399 tree t
= TREE_TYPE (type
);
1400 tree ref
= build1 (INDIRECT_REF
, TYPE_MAIN_VARIANT (t
), pointer
);
1402 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
1404 error ("dereferencing pointer to incomplete type");
1405 return error_mark_node
;
1407 if (VOID_TYPE_P (t
) && skip_evaluation
== 0)
1408 warning ("dereferencing `void *' pointer");
1410 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1411 so that we get the proper error message if the result is used
1412 to assign to. Also, &* is supposed to be a no-op.
1413 And ANSI C seems to specify that the type of the result
1414 should be the const type. */
1415 /* A de-reference of a pointer to const is not a const. It is valid
1416 to change it via some other pointer. */
1417 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1418 TREE_SIDE_EFFECTS (ref
)
1419 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1420 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1424 else if (TREE_CODE (pointer
) != ERROR_MARK
)
1425 error ("invalid type argument of `%s'", errorstring
);
1426 return error_mark_node
;
1429 /* This handles expressions of the form "a[i]", which denotes
1432 This is logically equivalent in C to *(a+i), but we may do it differently.
1433 If A is a variable or a member, we generate a primitive ARRAY_REF.
1434 This avoids forcing the array out of registers, and can work on
1435 arrays that are not lvalues (for example, members of structures returned
1439 build_array_ref (tree array
, tree index
)
1443 error ("subscript missing in array reference");
1444 return error_mark_node
;
1447 if (TREE_TYPE (array
) == error_mark_node
1448 || TREE_TYPE (index
) == error_mark_node
)
1449 return error_mark_node
;
1451 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
1452 && TREE_CODE (array
) != INDIRECT_REF
)
1456 /* Subscripting with type char is likely to lose
1457 on a machine where chars are signed.
1458 So warn on any machine, but optionally.
1459 Don't warn for unsigned char since that type is safe.
1460 Don't warn for signed char because anyone who uses that
1461 must have done so deliberately. */
1462 if (warn_char_subscripts
1463 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1464 warning ("array subscript has type `char'");
1466 /* Apply default promotions *after* noticing character types. */
1467 index
= default_conversion (index
);
1469 /* Require integer *after* promotion, for sake of enums. */
1470 if (TREE_CODE (TREE_TYPE (index
)) != INTEGER_TYPE
)
1472 error ("array subscript is not an integer");
1473 return error_mark_node
;
1476 /* An array that is indexed by a non-constant
1477 cannot be stored in a register; we must be able to do
1478 address arithmetic on its address.
1479 Likewise an array of elements of variable size. */
1480 if (TREE_CODE (index
) != INTEGER_CST
1481 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
1482 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1484 if (!c_mark_addressable (array
))
1485 return error_mark_node
;
1487 /* An array that is indexed by a constant value which is not within
1488 the array bounds cannot be stored in a register either; because we
1489 would get a crash in store_bit_field/extract_bit_field when trying
1490 to access a non-existent part of the register. */
1491 if (TREE_CODE (index
) == INTEGER_CST
1492 && TYPE_VALUES (TREE_TYPE (array
))
1493 && ! int_fits_type_p (index
, TYPE_VALUES (TREE_TYPE (array
))))
1495 if (!c_mark_addressable (array
))
1496 return error_mark_node
;
1502 while (TREE_CODE (foo
) == COMPONENT_REF
)
1503 foo
= TREE_OPERAND (foo
, 0);
1504 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
1505 pedwarn ("ISO C forbids subscripting `register' array");
1506 else if (! flag_isoc99
&& ! lvalue_p (foo
))
1507 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1510 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1511 rval
= build (ARRAY_REF
, type
, array
, index
);
1512 /* Array ref is const/volatile if the array elements are
1513 or if the array is. */
1514 TREE_READONLY (rval
)
1515 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
1516 | TREE_READONLY (array
));
1517 TREE_SIDE_EFFECTS (rval
)
1518 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1519 | TREE_SIDE_EFFECTS (array
));
1520 TREE_THIS_VOLATILE (rval
)
1521 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1522 /* This was added by rms on 16 Nov 91.
1523 It fixes vol struct foo *a; a->elts[1]
1524 in an inline function.
1525 Hope it doesn't break something else. */
1526 | TREE_THIS_VOLATILE (array
));
1527 return require_complete_type (fold (rval
));
1531 tree ar
= default_conversion (array
);
1532 tree ind
= default_conversion (index
);
1534 /* Do the same warning check as above, but only on the part that's
1535 syntactically the index and only if it is also semantically
1537 if (warn_char_subscripts
1538 && TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
1539 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1540 warning ("subscript has type `char'");
1542 /* Put the integer in IND to simplify error checking. */
1543 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
1550 if (ar
== error_mark_node
)
1553 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
1554 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) == FUNCTION_TYPE
)
1556 error ("subscripted value is neither array nor pointer");
1557 return error_mark_node
;
1559 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
1561 error ("array subscript is not an integer");
1562 return error_mark_node
;
1565 return build_indirect_ref (build_binary_op (PLUS_EXPR
, ar
, ind
, 0),
1570 /* Build an external reference to identifier ID. FUN indicates
1571 whether this will be used for a function call. */
1573 build_external_ref (tree id
, int fun
)
1576 tree decl
= lookup_name (id
);
1577 tree objc_ivar
= lookup_objc_ivar (id
);
1579 if (decl
&& decl
!= error_mark_node
)
1581 /* Properly declared variable or function reference. */
1584 else if (decl
!= objc_ivar
&& !DECL_FILE_SCOPE_P (decl
))
1586 warning ("local declaration of `%s' hides instance variable",
1587 IDENTIFIER_POINTER (id
));
1596 /* Implicit function declaration. */
1597 ref
= implicitly_declare (id
);
1598 else if (decl
== error_mark_node
)
1599 /* Don't complain about something that's already been
1600 complained about. */
1601 return error_mark_node
;
1604 undeclared_variable (id
);
1605 return error_mark_node
;
1608 if (TREE_TYPE (ref
) == error_mark_node
)
1609 return error_mark_node
;
1611 if (TREE_DEPRECATED (ref
))
1612 warn_deprecated_use (ref
);
1614 if (!skip_evaluation
)
1615 assemble_external (ref
);
1616 TREE_USED (ref
) = 1;
1618 if (TREE_CODE (ref
) == CONST_DECL
)
1620 ref
= DECL_INITIAL (ref
);
1621 TREE_CONSTANT (ref
) = 1;
1623 else if (current_function_decl
!= 0
1624 && !DECL_FILE_SCOPE_P (current_function_decl
)
1625 && (TREE_CODE (ref
) == VAR_DECL
1626 || TREE_CODE (ref
) == PARM_DECL
1627 || TREE_CODE (ref
) == FUNCTION_DECL
))
1629 tree context
= decl_function_context (ref
);
1631 if (context
!= 0 && context
!= current_function_decl
)
1632 DECL_NONLOCAL (ref
) = 1;
1638 /* Build a function call to function FUNCTION with parameters PARAMS.
1639 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1640 TREE_VALUE of each node is a parameter-expression.
1641 FUNCTION's data type may be a function type or a pointer-to-function. */
1644 build_function_call (tree function
, tree params
)
1646 tree fntype
, fundecl
= 0;
1647 tree coerced_params
;
1648 tree name
= NULL_TREE
, result
;
1651 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1652 STRIP_TYPE_NOPS (function
);
1654 /* Convert anything with function type to a pointer-to-function. */
1655 if (TREE_CODE (function
) == FUNCTION_DECL
)
1657 name
= DECL_NAME (function
);
1659 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1660 (because calling an inline function does not mean the function
1661 needs to be separately compiled). */
1662 fntype
= build_type_variant (TREE_TYPE (function
),
1663 TREE_READONLY (function
),
1664 TREE_THIS_VOLATILE (function
));
1666 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
1669 function
= default_conversion (function
);
1671 fntype
= TREE_TYPE (function
);
1673 if (TREE_CODE (fntype
) == ERROR_MARK
)
1674 return error_mark_node
;
1676 if (!(TREE_CODE (fntype
) == POINTER_TYPE
1677 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
1679 error ("called object is not a function");
1680 return error_mark_node
;
1683 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
1684 current_function_returns_abnormally
= 1;
1686 /* fntype now gets the type of function pointed to. */
1687 fntype
= TREE_TYPE (fntype
);
1689 /* Check that the function is called through a compatible prototype.
1690 If it is not, replace the call by a trap, wrapped up in a compound
1691 expression if necessary. This has the nice side-effect to prevent
1692 the tree-inliner from generating invalid assignment trees which may
1693 blow up in the RTL expander later.
1695 ??? This doesn't work for Objective-C because objc_comptypes
1696 refuses to compare function prototypes, yet the compiler appears
1697 to build calls that are flagged as invalid by C's comptypes. */
1698 if (! c_dialect_objc ()
1699 && TREE_CODE (function
) == NOP_EXPR
1700 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
1701 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
1702 && ! comptypes (fntype
, TREE_TYPE (tem
), COMPARE_STRICT
))
1704 tree return_type
= TREE_TYPE (fntype
);
1705 tree trap
= build_function_call (built_in_decls
[BUILT_IN_TRAP
],
1708 /* This situation leads to run-time undefined behavior. We can't,
1709 therefore, simply error unless we can prove that all possible
1710 executions of the program must execute the code. */
1711 warning ("function called through a non-compatible type");
1713 if (VOID_TYPE_P (return_type
))
1719 if (AGGREGATE_TYPE_P (return_type
))
1720 rhs
= build_compound_literal (return_type
,
1721 build_constructor (return_type
,
1724 rhs
= fold (build1 (NOP_EXPR
, return_type
, integer_zero_node
));
1726 return build (COMPOUND_EXPR
, return_type
, trap
, rhs
);
1730 /* Convert the parameters to the types declared in the
1731 function prototype, or apply default promotions. */
1734 = convert_arguments (TYPE_ARG_TYPES (fntype
), params
, name
, fundecl
);
1736 /* Check that the arguments to the function are valid. */
1738 check_function_arguments (TYPE_ATTRIBUTES (fntype
), coerced_params
);
1740 /* Recognize certain built-in functions so we can make tree-codes
1741 other than CALL_EXPR. We do this when it enables fold-const.c
1742 to do something useful. */
1744 if (TREE_CODE (function
) == ADDR_EXPR
1745 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1746 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
1748 result
= expand_tree_builtin (TREE_OPERAND (function
, 0),
1749 params
, coerced_params
);
1754 result
= build (CALL_EXPR
, TREE_TYPE (fntype
),
1755 function
, coerced_params
, NULL_TREE
);
1756 TREE_SIDE_EFFECTS (result
) = 1;
1757 result
= fold (result
);
1759 if (VOID_TYPE_P (TREE_TYPE (result
)))
1761 return require_complete_type (result
);
1764 /* Convert the argument expressions in the list VALUES
1765 to the types in the list TYPELIST. The result is a list of converted
1766 argument expressions.
1768 If TYPELIST is exhausted, or when an element has NULL as its type,
1769 perform the default conversions.
1771 PARMLIST is the chain of parm decls for the function being called.
1772 It may be 0, if that info is not available.
1773 It is used only for generating error messages.
1775 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1777 This is also where warnings about wrong number of args are generated.
1779 Both VALUES and the returned value are chains of TREE_LIST nodes
1780 with the elements of the list in the TREE_VALUE slots of those nodes. */
1783 convert_arguments (tree typelist
, tree values
, tree name
, tree fundecl
)
1785 tree typetail
, valtail
;
1789 /* Scan the given expressions and types, producing individual
1790 converted arguments and pushing them on RESULT in reverse order. */
1792 for (valtail
= values
, typetail
= typelist
, parmnum
= 0;
1794 valtail
= TREE_CHAIN (valtail
), parmnum
++)
1796 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
1797 tree val
= TREE_VALUE (valtail
);
1799 if (type
== void_type_node
)
1802 error ("too many arguments to function `%s'",
1803 IDENTIFIER_POINTER (name
));
1805 error ("too many arguments to function");
1809 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1810 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1811 to convert automatically to a pointer. */
1812 if (TREE_CODE (val
) == NON_LVALUE_EXPR
)
1813 val
= TREE_OPERAND (val
, 0);
1815 val
= default_function_array_conversion (val
);
1817 val
= require_complete_type (val
);
1821 /* Formal parm type is specified by a function prototype. */
1824 if (!COMPLETE_TYPE_P (type
))
1826 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
1831 /* Optionally warn about conversions that
1832 differ from the default conversions. */
1833 if (warn_conversion
|| warn_traditional
)
1835 int formal_prec
= TYPE_PRECISION (type
);
1837 if (INTEGRAL_TYPE_P (type
)
1838 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1839 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
1840 if (INTEGRAL_TYPE_P (type
)
1841 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
1842 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
1843 else if (TREE_CODE (type
) == COMPLEX_TYPE
1844 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1845 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
1846 else if (TREE_CODE (type
) == REAL_TYPE
1847 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1848 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
1849 else if (TREE_CODE (type
) == COMPLEX_TYPE
1850 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1851 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
1852 else if (TREE_CODE (type
) == REAL_TYPE
1853 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
1854 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
1855 /* ??? At some point, messages should be written about
1856 conversions between complex types, but that's too messy
1858 else if (TREE_CODE (type
) == REAL_TYPE
1859 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1861 /* Warn if any argument is passed as `float',
1862 since without a prototype it would be `double'. */
1863 if (formal_prec
== TYPE_PRECISION (float_type_node
))
1864 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name
, parmnum
+ 1);
1866 /* Detect integer changing in width or signedness.
1867 These warnings are only activated with
1868 -Wconversion, not with -Wtraditional. */
1869 else if (warn_conversion
&& INTEGRAL_TYPE_P (type
)
1870 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1872 tree would_have_been
= default_conversion (val
);
1873 tree type1
= TREE_TYPE (would_have_been
);
1875 if (TREE_CODE (type
) == ENUMERAL_TYPE
1876 && (TYPE_MAIN_VARIANT (type
)
1877 == TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
1878 /* No warning if function asks for enum
1879 and the actual arg is that enum type. */
1881 else if (formal_prec
!= TYPE_PRECISION (type1
))
1882 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name
, parmnum
+ 1);
1883 else if (TREE_UNSIGNED (type
) == TREE_UNSIGNED (type1
))
1885 /* Don't complain if the formal parameter type
1886 is an enum, because we can't tell now whether
1887 the value was an enum--even the same enum. */
1888 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1890 else if (TREE_CODE (val
) == INTEGER_CST
1891 && int_fits_type_p (val
, type
))
1892 /* Change in signedness doesn't matter
1893 if a constant value is unaffected. */
1895 /* Likewise for a constant in a NOP_EXPR. */
1896 else if (TREE_CODE (val
) == NOP_EXPR
1897 && TREE_CODE (TREE_OPERAND (val
, 0)) == INTEGER_CST
1898 && int_fits_type_p (TREE_OPERAND (val
, 0), type
))
1900 /* If the value is extended from a narrower
1901 unsigned type, it doesn't matter whether we
1902 pass it as signed or unsigned; the value
1903 certainly is the same either way. */
1904 else if (TYPE_PRECISION (TREE_TYPE (val
)) < TYPE_PRECISION (type
)
1905 && TREE_UNSIGNED (TREE_TYPE (val
)))
1907 else if (TREE_UNSIGNED (type
))
1908 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name
, parmnum
+ 1);
1910 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name
, parmnum
+ 1);
1914 parmval
= convert_for_assignment (type
, val
,
1915 (char *) 0, /* arg passing */
1916 fundecl
, name
, parmnum
+ 1);
1918 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
1919 && INTEGRAL_TYPE_P (type
)
1920 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
1921 parmval
= default_conversion (parmval
);
1923 result
= tree_cons (NULL_TREE
, parmval
, result
);
1925 else if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
1926 && (TYPE_PRECISION (TREE_TYPE (val
))
1927 < TYPE_PRECISION (double_type_node
)))
1928 /* Convert `float' to `double'. */
1929 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
1931 /* Convert `short' and `char' to full-size `int'. */
1932 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
1935 typetail
= TREE_CHAIN (typetail
);
1938 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
1941 error ("too few arguments to function `%s'",
1942 IDENTIFIER_POINTER (name
));
1944 error ("too few arguments to function");
1947 return nreverse (result
);
1950 /* This is the entry point used by the parser
1951 for binary operators in the input.
1952 In addition to constructing the expression,
1953 we check for operands that were written with other binary operators
1954 in a way that is likely to confuse the user. */
1957 parser_build_binary_op (enum tree_code code
, tree arg1
, tree arg2
)
1959 tree result
= build_binary_op (code
, arg1
, arg2
, 1);
1962 char class1
= TREE_CODE_CLASS (TREE_CODE (arg1
));
1963 char class2
= TREE_CODE_CLASS (TREE_CODE (arg2
));
1964 enum tree_code code1
= ERROR_MARK
;
1965 enum tree_code code2
= ERROR_MARK
;
1967 if (TREE_CODE (result
) == ERROR_MARK
)
1968 return error_mark_node
;
1970 if (IS_EXPR_CODE_CLASS (class1
))
1971 code1
= C_EXP_ORIGINAL_CODE (arg1
);
1972 if (IS_EXPR_CODE_CLASS (class2
))
1973 code2
= C_EXP_ORIGINAL_CODE (arg2
);
1975 /* Check for cases such as x+y<<z which users are likely
1976 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1977 is cleared to prevent these warnings. */
1978 if (warn_parentheses
)
1980 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
1982 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
1983 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
1984 warning ("suggest parentheses around + or - inside shift");
1987 if (code
== TRUTH_ORIF_EXPR
)
1989 if (code1
== TRUTH_ANDIF_EXPR
1990 || code2
== TRUTH_ANDIF_EXPR
)
1991 warning ("suggest parentheses around && within ||");
1994 if (code
== BIT_IOR_EXPR
)
1996 if (code1
== BIT_AND_EXPR
|| code1
== BIT_XOR_EXPR
1997 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
1998 || code2
== BIT_AND_EXPR
|| code2
== BIT_XOR_EXPR
1999 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2000 warning ("suggest parentheses around arithmetic in operand of |");
2001 /* Check cases like x|y==z */
2002 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2003 warning ("suggest parentheses around comparison in operand of |");
2006 if (code
== BIT_XOR_EXPR
)
2008 if (code1
== BIT_AND_EXPR
2009 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2010 || code2
== BIT_AND_EXPR
2011 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2012 warning ("suggest parentheses around arithmetic in operand of ^");
2013 /* Check cases like x^y==z */
2014 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2015 warning ("suggest parentheses around comparison in operand of ^");
2018 if (code
== BIT_AND_EXPR
)
2020 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2021 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2022 warning ("suggest parentheses around + or - in operand of &");
2023 /* Check cases like x&y==z */
2024 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2025 warning ("suggest parentheses around comparison in operand of &");
2029 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2030 if (TREE_CODE_CLASS (code
) == '<' && extra_warnings
2031 && (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<'))
2032 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2034 unsigned_conversion_warning (result
, arg1
);
2035 unsigned_conversion_warning (result
, arg2
);
2036 overflow_warning (result
);
2038 class = TREE_CODE_CLASS (TREE_CODE (result
));
2040 /* Record the code that was specified in the source,
2041 for the sake of warnings about confusing nesting. */
2042 if (IS_EXPR_CODE_CLASS (class))
2043 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2046 int flag
= TREE_CONSTANT (result
);
2047 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2048 so that convert_for_assignment wouldn't strip it.
2049 That way, we got warnings for things like p = (1 - 1).
2050 But it turns out we should not get those warnings. */
2051 result
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (result
), result
);
2052 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2053 TREE_CONSTANT (result
) = flag
;
2060 /* Return true if `t' is known to be non-negative. */
2063 c_tree_expr_nonnegative_p (tree t
)
2065 if (TREE_CODE (t
) == STMT_EXPR
)
2067 t
= COMPOUND_BODY (STMT_EXPR_STMT (t
));
2069 /* Find the last statement in the chain, ignoring the final
2070 * scope statement */
2071 while (TREE_CHAIN (t
) != NULL_TREE
2072 && TREE_CODE (TREE_CHAIN (t
)) != SCOPE_STMT
)
2074 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
2076 return tree_expr_nonnegative_p (t
);
2079 /* Return a tree for the difference of pointers OP0 and OP1.
2080 The resulting tree has type int. */
2083 pointer_diff (tree op0
, tree op1
)
2085 tree result
, folded
;
2086 tree restype
= ptrdiff_type_node
;
2088 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
2089 tree con0
, con1
, lit0
, lit1
;
2090 tree orig_op1
= op1
;
2092 if (pedantic
|| warn_pointer_arith
)
2094 if (TREE_CODE (target_type
) == VOID_TYPE
)
2095 pedwarn ("pointer of type `void *' used in subtraction");
2096 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
2097 pedwarn ("pointer to a function used in subtraction");
2100 /* If the conversion to ptrdiff_type does anything like widening or
2101 converting a partial to an integral mode, we get a convert_expression
2102 that is in the way to do any simplifications.
2103 (fold-const.c doesn't know that the extra bits won't be needed.
2104 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2105 different mode in place.)
2106 So first try to find a common term here 'by hand'; we want to cover
2107 at least the cases that occur in legal static initializers. */
2108 con0
= TREE_CODE (op0
) == NOP_EXPR
? TREE_OPERAND (op0
, 0) : op0
;
2109 con1
= TREE_CODE (op1
) == NOP_EXPR
? TREE_OPERAND (op1
, 0) : op1
;
2111 if (TREE_CODE (con0
) == PLUS_EXPR
)
2113 lit0
= TREE_OPERAND (con0
, 1);
2114 con0
= TREE_OPERAND (con0
, 0);
2117 lit0
= integer_zero_node
;
2119 if (TREE_CODE (con1
) == PLUS_EXPR
)
2121 lit1
= TREE_OPERAND (con1
, 1);
2122 con1
= TREE_OPERAND (con1
, 0);
2125 lit1
= integer_zero_node
;
2127 if (operand_equal_p (con0
, con1
, 0))
2134 /* First do the subtraction as integers;
2135 then drop through to build the divide operator.
2136 Do not do default conversions on the minus operator
2137 in case restype is a short type. */
2139 op0
= build_binary_op (MINUS_EXPR
, convert (restype
, op0
),
2140 convert (restype
, op1
), 0);
2141 /* This generates an error if op1 is pointer to incomplete type. */
2142 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
2143 error ("arithmetic on pointer to an incomplete type");
2145 /* This generates an error if op0 is pointer to incomplete type. */
2146 op1
= c_size_in_bytes (target_type
);
2148 /* Divide by the size, in easiest possible way. */
2150 result
= build (EXACT_DIV_EXPR
, restype
, op0
, convert (restype
, op1
));
2152 folded
= fold (result
);
2153 if (folded
== result
)
2154 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2158 /* Construct and perhaps optimize a tree representation
2159 for a unary operation. CODE, a tree_code, specifies the operation
2160 and XARG is the operand.
2161 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2162 the default promotions (such as from short to int).
2163 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2164 allows non-lvalues; this is only used to handle conversion of non-lvalue
2165 arrays to pointers in C99. */
2168 build_unary_op (enum tree_code code
, tree xarg
, int flag
)
2170 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2173 enum tree_code typecode
= TREE_CODE (TREE_TYPE (arg
));
2175 int noconvert
= flag
;
2177 if (typecode
== ERROR_MARK
)
2178 return error_mark_node
;
2179 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
2180 typecode
= INTEGER_TYPE
;
2185 /* This is used for unary plus, because a CONVERT_EXPR
2186 is enough to prevent anybody from looking inside for
2187 associativity, but won't generate any code. */
2188 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2189 || typecode
== COMPLEX_TYPE
))
2191 error ("wrong type argument to unary plus");
2192 return error_mark_node
;
2194 else if (!noconvert
)
2195 arg
= default_conversion (arg
);
2196 arg
= non_lvalue (arg
);
2200 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2201 || typecode
== COMPLEX_TYPE
2202 || typecode
== VECTOR_TYPE
))
2204 error ("wrong type argument to unary minus");
2205 return error_mark_node
;
2207 else if (!noconvert
)
2208 arg
= default_conversion (arg
);
2212 if (typecode
== INTEGER_TYPE
|| typecode
== VECTOR_TYPE
)
2215 arg
= default_conversion (arg
);
2217 else if (typecode
== COMPLEX_TYPE
)
2221 pedwarn ("ISO C does not support `~' for complex conjugation");
2223 arg
= default_conversion (arg
);
2227 error ("wrong type argument to bit-complement");
2228 return error_mark_node
;
2233 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2235 error ("wrong type argument to abs");
2236 return error_mark_node
;
2238 else if (!noconvert
)
2239 arg
= default_conversion (arg
);
2243 /* Conjugating a real value is a no-op, but allow it anyway. */
2244 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2245 || typecode
== COMPLEX_TYPE
))
2247 error ("wrong type argument to conjugation");
2248 return error_mark_node
;
2250 else if (!noconvert
)
2251 arg
= default_conversion (arg
);
2254 case TRUTH_NOT_EXPR
:
2255 if (typecode
!= INTEGER_TYPE
2256 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
2257 && typecode
!= COMPLEX_TYPE
2258 /* These will convert to a pointer. */
2259 && typecode
!= ARRAY_TYPE
&& typecode
!= FUNCTION_TYPE
)
2261 error ("wrong type argument to unary exclamation mark");
2262 return error_mark_node
;
2264 arg
= c_common_truthvalue_conversion (arg
);
2265 return invert_truthvalue (arg
);
2271 if (TREE_CODE (arg
) == COMPLEX_CST
)
2272 return TREE_REALPART (arg
);
2273 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2274 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2279 if (TREE_CODE (arg
) == COMPLEX_CST
)
2280 return TREE_IMAGPART (arg
);
2281 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2282 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2284 return convert (TREE_TYPE (arg
), integer_zero_node
);
2286 case PREINCREMENT_EXPR
:
2287 case POSTINCREMENT_EXPR
:
2288 case PREDECREMENT_EXPR
:
2289 case POSTDECREMENT_EXPR
:
2290 /* Handle complex lvalues (when permitted)
2291 by reduction to simpler cases. */
2293 val
= unary_complex_lvalue (code
, arg
, 0);
2297 /* Increment or decrement the real part of the value,
2298 and don't change the imaginary part. */
2299 if (typecode
== COMPLEX_TYPE
)
2304 pedwarn ("ISO C does not support `++' and `--' on complex types");
2306 arg
= stabilize_reference (arg
);
2307 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
2308 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
2309 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
2310 build_unary_op (code
, real
, 1), imag
);
2313 /* Report invalid types. */
2315 if (typecode
!= POINTER_TYPE
2316 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
2318 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2319 error ("wrong type argument to increment");
2321 error ("wrong type argument to decrement");
2323 return error_mark_node
;
2328 tree result_type
= TREE_TYPE (arg
);
2330 arg
= get_unwidened (arg
, 0);
2331 argtype
= TREE_TYPE (arg
);
2333 /* Compute the increment. */
2335 if (typecode
== POINTER_TYPE
)
2337 /* If pointer target is an undefined struct,
2338 we just cannot know how to do the arithmetic. */
2339 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type
)))
2341 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2342 error ("increment of pointer to unknown structure");
2344 error ("decrement of pointer to unknown structure");
2346 else if ((pedantic
|| warn_pointer_arith
)
2347 && (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
2348 || TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
))
2350 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2351 pedwarn ("wrong type argument to increment");
2353 pedwarn ("wrong type argument to decrement");
2356 inc
= c_size_in_bytes (TREE_TYPE (result_type
));
2359 inc
= integer_one_node
;
2361 inc
= convert (argtype
, inc
);
2363 /* Handle incrementing a cast-expression. */
2366 switch (TREE_CODE (arg
))
2371 case FIX_TRUNC_EXPR
:
2372 case FIX_FLOOR_EXPR
:
2373 case FIX_ROUND_EXPR
:
2375 pedantic_lvalue_warning (CONVERT_EXPR
);
2376 /* If the real type has the same machine representation
2377 as the type it is cast to, we can make better output
2378 by adding directly to the inside of the cast. */
2379 if ((TREE_CODE (TREE_TYPE (arg
))
2380 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))))
2381 && (TYPE_MODE (TREE_TYPE (arg
))
2382 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg
, 0)))))
2383 arg
= TREE_OPERAND (arg
, 0);
2386 tree incremented
, modify
, value
;
2387 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
2388 value
= boolean_increment (code
, arg
);
2391 arg
= stabilize_reference (arg
);
2392 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
2395 value
= save_expr (arg
);
2396 incremented
= build (((code
== PREINCREMENT_EXPR
2397 || code
== POSTINCREMENT_EXPR
)
2398 ? PLUS_EXPR
: MINUS_EXPR
),
2399 argtype
, value
, inc
);
2400 TREE_SIDE_EFFECTS (incremented
) = 1;
2401 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
2402 value
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
2404 TREE_USED (value
) = 1;
2414 /* Complain about anything else that is not a true lvalue. */
2415 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
2416 || code
== POSTINCREMENT_EXPR
)
2417 ? "invalid lvalue in increment"
2418 : "invalid lvalue in decrement")))
2419 return error_mark_node
;
2421 /* Report a read-only lvalue. */
2422 if (TREE_READONLY (arg
))
2423 readonly_warning (arg
,
2424 ((code
== PREINCREMENT_EXPR
2425 || code
== POSTINCREMENT_EXPR
)
2426 ? "increment" : "decrement"));
2428 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
2429 val
= boolean_increment (code
, arg
);
2431 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
2432 TREE_SIDE_EFFECTS (val
) = 1;
2433 val
= convert (result_type
, val
);
2434 if (TREE_CODE (val
) != code
)
2435 TREE_NO_UNUSED_WARNING (val
) = 1;
2440 /* Note that this operation never does default_conversion. */
2442 /* Let &* cancel out to simplify resulting code. */
2443 if (TREE_CODE (arg
) == INDIRECT_REF
)
2445 /* Don't let this be an lvalue. */
2446 if (lvalue_p (TREE_OPERAND (arg
, 0)))
2447 return non_lvalue (TREE_OPERAND (arg
, 0));
2448 return TREE_OPERAND (arg
, 0);
2451 /* For &x[y], return x+y */
2452 if (TREE_CODE (arg
) == ARRAY_REF
)
2454 if (!c_mark_addressable (TREE_OPERAND (arg
, 0)))
2455 return error_mark_node
;
2456 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
2457 TREE_OPERAND (arg
, 1), 1);
2460 /* Handle complex lvalues (when permitted)
2461 by reduction to simpler cases. */
2462 val
= unary_complex_lvalue (code
, arg
, flag
);
2466 /* Anything not already handled and not a true memory reference
2467 or a non-lvalue array is an error. */
2468 else if (typecode
!= FUNCTION_TYPE
&& !flag
2469 && !lvalue_or_else (arg
, "invalid lvalue in unary `&'"))
2470 return error_mark_node
;
2472 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2473 argtype
= TREE_TYPE (arg
);
2475 /* If the lvalue is const or volatile, merge that into the type
2476 to which the address will point. Note that you can't get a
2477 restricted pointer by taking the address of something, so we
2478 only have to deal with `const' and `volatile' here. */
2479 if ((DECL_P (arg
) || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
2480 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
)))
2481 argtype
= c_build_type_variant (argtype
,
2482 TREE_READONLY (arg
),
2483 TREE_THIS_VOLATILE (arg
));
2485 argtype
= build_pointer_type (argtype
);
2487 if (!c_mark_addressable (arg
))
2488 return error_mark_node
;
2493 if (TREE_CODE (arg
) == COMPONENT_REF
)
2495 tree field
= TREE_OPERAND (arg
, 1);
2497 addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), flag
);
2499 if (DECL_C_BIT_FIELD (field
))
2501 error ("attempt to take address of bit-field structure member `%s'",
2502 IDENTIFIER_POINTER (DECL_NAME (field
)));
2503 return error_mark_node
;
2506 addr
= fold (build (PLUS_EXPR
, argtype
,
2507 convert (argtype
, addr
),
2508 convert (argtype
, byte_position (field
))));
2511 addr
= build1 (code
, argtype
, arg
);
2513 /* Address of a static or external variable or
2514 file-scope function counts as a constant. */
2516 && ! (TREE_CODE (arg
) == FUNCTION_DECL
2517 && !DECL_FILE_SCOPE_P (arg
)))
2518 TREE_CONSTANT (addr
) = 1;
2527 argtype
= TREE_TYPE (arg
);
2528 return fold (build1 (code
, argtype
, arg
));
2531 /* Return nonzero if REF is an lvalue valid for this language.
2532 Lvalues can be assigned, unless their type has TYPE_READONLY.
2533 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2538 enum tree_code code
= TREE_CODE (ref
);
2545 return lvalue_p (TREE_OPERAND (ref
, 0));
2547 case COMPOUND_LITERAL_EXPR
:
2557 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
2558 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
2562 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
2569 /* Return nonzero if REF is an lvalue valid for this language;
2570 otherwise, print an error message and return zero. */
2573 lvalue_or_else (tree ref
, const char *msgid
)
2575 int win
= lvalue_p (ref
);
2578 error ("%s", msgid
);
2583 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2584 for certain kinds of expressions which are not really lvalues
2585 but which we can accept as lvalues. If FLAG is nonzero, then
2586 non-lvalues are OK since we may be converting a non-lvalue array to
2589 If ARG is not a kind of expression we can handle, return zero. */
2592 unary_complex_lvalue (enum tree_code code
, tree arg
, int flag
)
2594 /* Handle (a, b) used as an "lvalue". */
2595 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
2597 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
2599 /* If this returns a function type, it isn't really being used as
2600 an lvalue, so don't issue a warning about it. */
2601 if (TREE_CODE (TREE_TYPE (arg
)) != FUNCTION_TYPE
&& !flag
)
2602 pedantic_lvalue_warning (COMPOUND_EXPR
);
2604 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
2605 TREE_OPERAND (arg
, 0), real_result
);
2608 /* Handle (a ? b : c) used as an "lvalue". */
2609 if (TREE_CODE (arg
) == COND_EXPR
)
2612 pedantic_lvalue_warning (COND_EXPR
);
2613 if (TREE_CODE (TREE_TYPE (arg
)) != FUNCTION_TYPE
&& !flag
)
2614 pedantic_lvalue_warning (COMPOUND_EXPR
);
2616 return (build_conditional_expr
2617 (TREE_OPERAND (arg
, 0),
2618 build_unary_op (code
, TREE_OPERAND (arg
, 1), flag
),
2619 build_unary_op (code
, TREE_OPERAND (arg
, 2), flag
)));
2625 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2626 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2629 pedantic_lvalue_warning (enum tree_code code
)
2634 pedwarn ("use of conditional expressions as lvalues is deprecated");
2637 pedwarn ("use of compound expressions as lvalues is deprecated");
2640 pedwarn ("use of cast expressions as lvalues is deprecated");
2645 /* Warn about storing in something that is `const'. */
2648 readonly_warning (tree arg
, const char *msgid
)
2650 if (TREE_CODE (arg
) == COMPONENT_REF
)
2652 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
2653 readonly_warning (TREE_OPERAND (arg
, 0), msgid
);
2655 pedwarn ("%s of read-only member `%s'", _(msgid
),
2656 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg
, 1))));
2658 else if (TREE_CODE (arg
) == VAR_DECL
)
2659 pedwarn ("%s of read-only variable `%s'", _(msgid
),
2660 IDENTIFIER_POINTER (DECL_NAME (arg
)));
2662 pedwarn ("%s of read-only location", _(msgid
));
2665 /* Mark EXP saying that we need to be able to take the
2666 address of it; it should not be allocated in a register.
2667 Returns true if successful. */
2670 c_mark_addressable (tree exp
)
2675 switch (TREE_CODE (x
))
2678 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
2680 error ("cannot take address of bit-field `%s'",
2681 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x
, 1))));
2685 /* ... fall through ... */
2691 x
= TREE_OPERAND (x
, 0);
2694 case COMPOUND_LITERAL_EXPR
:
2696 TREE_ADDRESSABLE (x
) = 1;
2703 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
2704 && DECL_NONLOCAL (x
))
2706 if (TREE_PUBLIC (x
))
2708 error ("global register variable `%s' used in nested function",
2709 IDENTIFIER_POINTER (DECL_NAME (x
)));
2712 pedwarn ("register variable `%s' used in nested function",
2713 IDENTIFIER_POINTER (DECL_NAME (x
)));
2715 else if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
))
2717 if (TREE_PUBLIC (x
))
2719 error ("address of global register variable `%s' requested",
2720 IDENTIFIER_POINTER (DECL_NAME (x
)));
2724 /* If we are making this addressable due to its having
2725 volatile components, give a different error message. Also
2726 handle the case of an unnamed parameter by not trying
2727 to give the name. */
2729 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x
)))
2731 error ("cannot put object with volatile field into register");
2735 pedwarn ("address of register variable `%s' requested",
2736 IDENTIFIER_POINTER (DECL_NAME (x
)));
2738 put_var_into_stack (x
, /*rescan=*/true);
2742 TREE_ADDRESSABLE (x
) = 1;
2749 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2752 build_conditional_expr (tree ifexp
, tree op1
, tree op2
)
2756 enum tree_code code1
;
2757 enum tree_code code2
;
2758 tree result_type
= NULL
;
2759 tree orig_op1
= op1
, orig_op2
= op2
;
2761 ifexp
= c_common_truthvalue_conversion (default_conversion (ifexp
));
2763 /* Promote both alternatives. */
2765 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
2766 op1
= default_conversion (op1
);
2767 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
2768 op2
= default_conversion (op2
);
2770 if (TREE_CODE (ifexp
) == ERROR_MARK
2771 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
2772 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
2773 return error_mark_node
;
2775 type1
= TREE_TYPE (op1
);
2776 code1
= TREE_CODE (type1
);
2777 type2
= TREE_TYPE (op2
);
2778 code2
= TREE_CODE (type2
);
2780 /* Quickly detect the usual case where op1 and op2 have the same type
2782 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
2785 result_type
= type1
;
2787 result_type
= TYPE_MAIN_VARIANT (type1
);
2789 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2790 || code1
== COMPLEX_TYPE
)
2791 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
2792 || code2
== COMPLEX_TYPE
))
2794 result_type
= common_type (type1
, type2
);
2796 /* If -Wsign-compare, warn here if type1 and type2 have
2797 different signedness. We'll promote the signed to unsigned
2798 and later code won't know it used to be different.
2799 Do this check on the original types, so that explicit casts
2800 will be considered, but default promotions won't. */
2801 if (warn_sign_compare
&& !skip_evaluation
)
2803 int unsigned_op1
= TREE_UNSIGNED (TREE_TYPE (orig_op1
));
2804 int unsigned_op2
= TREE_UNSIGNED (TREE_TYPE (orig_op2
));
2806 if (unsigned_op1
^ unsigned_op2
)
2808 /* Do not warn if the result type is signed, since the
2809 signed type will only be chosen if it can represent
2810 all the values of the unsigned type. */
2811 if (! TREE_UNSIGNED (result_type
))
2813 /* Do not warn if the signed quantity is an unsuffixed
2814 integer literal (or some static constant expression
2815 involving such literals) and it is non-negative. */
2816 else if ((unsigned_op2
&& c_tree_expr_nonnegative_p (op1
))
2817 || (unsigned_op1
&& c_tree_expr_nonnegative_p (op2
)))
2820 warning ("signed and unsigned type in conditional expression");
2824 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
2826 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
2827 pedwarn ("ISO C forbids conditional expr with only one void side");
2828 result_type
= void_type_node
;
2830 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
2832 if (comp_target_types (type1
, type2
, 1))
2833 result_type
= common_type (type1
, type2
);
2834 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
2835 && TREE_CODE (orig_op1
) != NOP_EXPR
)
2836 result_type
= qualify_type (type2
, type1
);
2837 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
2838 && TREE_CODE (orig_op2
) != NOP_EXPR
)
2839 result_type
= qualify_type (type1
, type2
);
2840 else if (VOID_TYPE_P (TREE_TYPE (type1
)))
2842 if (pedantic
&& TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
2843 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2844 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
2845 TREE_TYPE (type2
)));
2847 else if (VOID_TYPE_P (TREE_TYPE (type2
)))
2849 if (pedantic
&& TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
2850 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2851 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
2852 TREE_TYPE (type1
)));
2856 pedwarn ("pointer type mismatch in conditional expression");
2857 result_type
= build_pointer_type (void_type_node
);
2860 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
2862 if (! integer_zerop (op2
))
2863 pedwarn ("pointer/integer type mismatch in conditional expression");
2866 op2
= null_pointer_node
;
2868 result_type
= type1
;
2870 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2872 if (!integer_zerop (op1
))
2873 pedwarn ("pointer/integer type mismatch in conditional expression");
2876 op1
= null_pointer_node
;
2878 result_type
= type2
;
2883 if (flag_cond_mismatch
)
2884 result_type
= void_type_node
;
2887 error ("type mismatch in conditional expression");
2888 return error_mark_node
;
2892 /* Merge const and volatile flags of the incoming types. */
2894 = build_type_variant (result_type
,
2895 TREE_READONLY (op1
) || TREE_READONLY (op2
),
2896 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
2898 if (result_type
!= TREE_TYPE (op1
))
2899 op1
= convert_and_check (result_type
, op1
);
2900 if (result_type
!= TREE_TYPE (op2
))
2901 op2
= convert_and_check (result_type
, op2
);
2903 if (TREE_CODE (ifexp
) == INTEGER_CST
)
2904 return pedantic_non_lvalue (integer_zerop (ifexp
) ? op2
: op1
);
2906 return fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
));
2909 /* Given a list of expressions, return a compound expression
2910 that performs them all and returns the value of the last of them. */
2913 build_compound_expr (tree list
)
2915 return internal_build_compound_expr (list
, TRUE
);
2919 internal_build_compound_expr (tree list
, int first_p
)
2923 if (TREE_CHAIN (list
) == 0)
2925 /* Convert arrays and functions to pointers when there
2926 really is a comma operator. */
2929 = default_function_array_conversion (TREE_VALUE (list
));
2931 /* Don't let (0, 0) be null pointer constant. */
2932 if (!first_p
&& integer_zerop (TREE_VALUE (list
)))
2933 return non_lvalue (TREE_VALUE (list
));
2934 return TREE_VALUE (list
);
2937 rest
= internal_build_compound_expr (TREE_CHAIN (list
), FALSE
);
2939 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
2941 /* The left-hand operand of a comma expression is like an expression
2942 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2943 any side-effects, unless it was explicitly cast to (void). */
2944 if (warn_unused_value
2945 && ! (TREE_CODE (TREE_VALUE (list
)) == CONVERT_EXPR
2946 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list
)))))
2947 warning ("left-hand operand of comma expression has no effect");
2950 /* With -Wunused, we should also warn if the left-hand operand does have
2951 side-effects, but computes a value which is not used. For example, in
2952 `foo() + bar(), baz()' the result of the `+' operator is not used,
2953 so we should issue a warning. */
2954 else if (warn_unused_value
)
2955 warn_if_unused_value (TREE_VALUE (list
));
2957 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), TREE_VALUE (list
), rest
);
2960 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2963 build_c_cast (tree type
, tree expr
)
2967 if (type
== error_mark_node
|| expr
== error_mark_node
)
2968 return error_mark_node
;
2970 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2971 only in <protocol> qualifications. But when constructing cast expressions,
2972 the protocols do matter and must be kept around. */
2973 if (!c_dialect_objc () || !objc_is_object_ptr (type
))
2974 type
= TYPE_MAIN_VARIANT (type
);
2976 if (TREE_CODE (type
) == ARRAY_TYPE
)
2978 error ("cast specifies array type");
2979 return error_mark_node
;
2982 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2984 error ("cast specifies function type");
2985 return error_mark_node
;
2988 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
2992 if (TREE_CODE (type
) == RECORD_TYPE
2993 || TREE_CODE (type
) == UNION_TYPE
)
2994 pedwarn ("ISO C forbids casting nonscalar to the same type");
2997 else if (TREE_CODE (type
) == UNION_TYPE
)
3000 value
= default_function_array_conversion (value
);
3002 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3003 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
3004 TYPE_MAIN_VARIANT (TREE_TYPE (value
)), COMPARE_STRICT
))
3012 pedwarn ("ISO C forbids casts to union type");
3013 t
= digest_init (type
,
3014 build_constructor (type
,
3015 build_tree_list (field
, value
)),
3017 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
3020 error ("cast to union type from type not present in union");
3021 return error_mark_node
;
3027 /* If casting to void, avoid the error that would come
3028 from default_conversion in the case of a non-lvalue array. */
3029 if (type
== void_type_node
)
3030 return build1 (CONVERT_EXPR
, type
, value
);
3032 /* Convert functions and arrays to pointers,
3033 but don't convert any other types. */
3034 value
= default_function_array_conversion (value
);
3035 otype
= TREE_TYPE (value
);
3037 /* Optionally warn about potentially worrisome casts. */
3040 && TREE_CODE (type
) == POINTER_TYPE
3041 && TREE_CODE (otype
) == POINTER_TYPE
)
3043 tree in_type
= type
;
3044 tree in_otype
= otype
;
3048 /* Check that the qualifiers on IN_TYPE are a superset of
3049 the qualifiers of IN_OTYPE. The outermost level of
3050 POINTER_TYPE nodes is uninteresting and we stop as soon
3051 as we hit a non-POINTER_TYPE node on either type. */
3054 in_otype
= TREE_TYPE (in_otype
);
3055 in_type
= TREE_TYPE (in_type
);
3057 /* GNU C allows cv-qualified function types. 'const'
3058 means the function is very pure, 'volatile' means it
3059 can't return. We need to warn when such qualifiers
3060 are added, not when they're taken away. */
3061 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
3062 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
3063 added
|= (TYPE_QUALS (in_type
) & ~TYPE_QUALS (in_otype
));
3065 discarded
|= (TYPE_QUALS (in_otype
) & ~TYPE_QUALS (in_type
));
3067 while (TREE_CODE (in_type
) == POINTER_TYPE
3068 && TREE_CODE (in_otype
) == POINTER_TYPE
);
3071 warning ("cast adds new qualifiers to function type");
3074 /* There are qualifiers present in IN_OTYPE that are not
3075 present in IN_TYPE. */
3076 warning ("cast discards qualifiers from pointer target type");
3079 /* Warn about possible alignment problems. */
3080 if (STRICT_ALIGNMENT
&& warn_cast_align
3081 && TREE_CODE (type
) == POINTER_TYPE
3082 && TREE_CODE (otype
) == POINTER_TYPE
3083 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
3084 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3085 /* Don't warn about opaque types, where the actual alignment
3086 restriction is unknown. */
3087 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
3088 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
3089 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
3090 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
3091 warning ("cast increases required alignment of target type");
3093 if (TREE_CODE (type
) == INTEGER_TYPE
3094 && TREE_CODE (otype
) == POINTER_TYPE
3095 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3096 && !TREE_CONSTANT (value
))
3097 warning ("cast from pointer to integer of different size");
3099 if (warn_bad_function_cast
3100 && TREE_CODE (value
) == CALL_EXPR
3101 && TREE_CODE (type
) != TREE_CODE (otype
))
3102 warning ("cast does not match function type");
3104 if (TREE_CODE (type
) == POINTER_TYPE
3105 && TREE_CODE (otype
) == INTEGER_TYPE
3106 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3107 /* Don't warn about converting any constant. */
3108 && !TREE_CONSTANT (value
))
3109 warning ("cast to pointer from integer of different size");
3111 if (TREE_CODE (type
) == POINTER_TYPE
3112 && TREE_CODE (otype
) == POINTER_TYPE
3113 && TREE_CODE (expr
) == ADDR_EXPR
3114 && DECL_P (TREE_OPERAND (expr
, 0))
3115 && flag_strict_aliasing
&& warn_strict_aliasing
3116 && !VOID_TYPE_P (TREE_TYPE (type
)))
3118 /* Casting the address of a decl to non void pointer. Warn
3119 if the cast breaks type based aliasing. */
3120 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)))
3121 warning ("type-punning to incomplete type might break strict-aliasing rules");
3122 else if (!alias_sets_conflict_p
3123 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0))),
3124 get_alias_set (TREE_TYPE (type
))))
3125 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3129 /* Replace a nonvolatile const static variable with its value. */
3130 if (optimize
&& TREE_CODE (value
) == VAR_DECL
)
3131 value
= decl_constant_value (value
);
3132 value
= convert (type
, value
);
3134 /* Ignore any integer overflow caused by the cast. */
3135 if (TREE_CODE (value
) == INTEGER_CST
)
3137 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
3138 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
3142 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3143 if (pedantic
&& TREE_CODE (value
) == INTEGER_CST
3144 && TREE_CODE (expr
) == INTEGER_CST
3145 && TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
)
3146 value
= non_lvalue (value
);
3148 /* If pedantic, don't let a cast be an lvalue. */
3149 if (value
== expr
&& pedantic
)
3150 value
= non_lvalue (value
);
3155 /* Interpret a cast of expression EXPR to type TYPE. */
3157 c_cast_expr (tree type
, tree expr
)
3159 int saved_wsp
= warn_strict_prototypes
;
3161 /* This avoids warnings about unprototyped casts on
3162 integers. E.g. "#define SIG_DFL (void(*)())0". */
3163 if (TREE_CODE (expr
) == INTEGER_CST
)
3164 warn_strict_prototypes
= 0;
3165 type
= groktypename (type
);
3166 warn_strict_prototypes
= saved_wsp
;
3168 return build_c_cast (type
, expr
);
3172 /* Build an assignment expression of lvalue LHS from value RHS.
3173 MODIFYCODE is the code for a binary operator that we use
3174 to combine the old value of LHS with RHS to get the new value.
3175 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3178 build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
)
3182 tree lhstype
= TREE_TYPE (lhs
);
3183 tree olhstype
= lhstype
;
3185 /* Types that aren't fully specified cannot be used in assignments. */
3186 lhs
= require_complete_type (lhs
);
3188 /* Avoid duplicate error messages from operands that had errors. */
3189 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
3190 return error_mark_node
;
3192 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3193 /* Do not use STRIP_NOPS here. We do not want an enumerator
3194 whose value is 0 to count as a null pointer constant. */
3195 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3196 rhs
= TREE_OPERAND (rhs
, 0);
3200 /* Handle control structure constructs used as "lvalues". */
3202 switch (TREE_CODE (lhs
))
3204 /* Handle (a, b) used as an "lvalue". */
3206 pedantic_lvalue_warning (COMPOUND_EXPR
);
3207 newrhs
= build_modify_expr (TREE_OPERAND (lhs
, 1), modifycode
, rhs
);
3208 if (TREE_CODE (newrhs
) == ERROR_MARK
)
3209 return error_mark_node
;
3210 return build (COMPOUND_EXPR
, lhstype
,
3211 TREE_OPERAND (lhs
, 0), newrhs
);
3213 /* Handle (a ? b : c) used as an "lvalue". */
3215 pedantic_lvalue_warning (COND_EXPR
);
3216 rhs
= save_expr (rhs
);
3218 /* Produce (a ? (b = rhs) : (c = rhs))
3219 except that the RHS goes through a save-expr
3220 so the code to compute it is only emitted once. */
3222 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
3223 build_modify_expr (TREE_OPERAND (lhs
, 1),
3225 build_modify_expr (TREE_OPERAND (lhs
, 2),
3227 if (TREE_CODE (cond
) == ERROR_MARK
)
3229 /* Make sure the code to compute the rhs comes out
3230 before the split. */
3231 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
3232 /* But cast it to void to avoid an "unused" error. */
3233 convert (void_type_node
, rhs
), cond
);
3239 /* If a binary op has been requested, combine the old LHS value with the RHS
3240 producing the value we should actually store into the LHS. */
3242 if (modifycode
!= NOP_EXPR
)
3244 lhs
= stabilize_reference (lhs
);
3245 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
3248 /* Handle a cast used as an "lvalue".
3249 We have already performed any binary operator using the value as cast.
3250 Now convert the result to the cast type of the lhs,
3251 and then true type of the lhs and store it there;
3252 then convert result back to the cast type to be the value
3253 of the assignment. */
3255 switch (TREE_CODE (lhs
))
3260 case FIX_TRUNC_EXPR
:
3261 case FIX_FLOOR_EXPR
:
3262 case FIX_ROUND_EXPR
:
3264 newrhs
= default_function_array_conversion (newrhs
);
3266 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
3268 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
3269 convert (TREE_TYPE (inner_lhs
),
3270 convert (lhstype
, newrhs
)));
3271 if (TREE_CODE (result
) == ERROR_MARK
)
3273 pedantic_lvalue_warning (CONVERT_EXPR
);
3274 return convert (TREE_TYPE (lhs
), result
);
3281 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3282 Reject anything strange now. */
3284 if (!lvalue_or_else (lhs
, "invalid lvalue in assignment"))
3285 return error_mark_node
;
3287 /* Warn about storing in something that is `const'. */
3289 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
3290 || ((TREE_CODE (lhstype
) == RECORD_TYPE
3291 || TREE_CODE (lhstype
) == UNION_TYPE
)
3292 && C_TYPE_FIELDS_READONLY (lhstype
)))
3293 readonly_warning (lhs
, "assignment");
3295 /* If storing into a structure or union member,
3296 it has probably been given type `int'.
3297 Compute the type that would go with
3298 the actual amount of storage the member occupies. */
3300 if (TREE_CODE (lhs
) == COMPONENT_REF
3301 && (TREE_CODE (lhstype
) == INTEGER_TYPE
3302 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
3303 || TREE_CODE (lhstype
) == REAL_TYPE
3304 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
3305 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
3307 /* If storing in a field that is in actuality a short or narrower than one,
3308 we must store in the field in its actual type. */
3310 if (lhstype
!= TREE_TYPE (lhs
))
3312 lhs
= copy_node (lhs
);
3313 TREE_TYPE (lhs
) = lhstype
;
3316 /* Convert new value to destination type. */
3318 newrhs
= convert_for_assignment (lhstype
, newrhs
, _("assignment"),
3319 NULL_TREE
, NULL_TREE
, 0);
3320 if (TREE_CODE (newrhs
) == ERROR_MARK
)
3321 return error_mark_node
;
3325 result
= build (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
3326 TREE_SIDE_EFFECTS (result
) = 1;
3328 /* If we got the LHS in a different type for storing in,
3329 convert the result back to the nominal type of LHS
3330 so that the value we return always has the same type
3331 as the LHS argument. */
3333 if (olhstype
== TREE_TYPE (result
))
3335 return convert_for_assignment (olhstype
, result
, _("assignment"),
3336 NULL_TREE
, NULL_TREE
, 0);
3339 /* Convert value RHS to type TYPE as preparation for an assignment
3340 to an lvalue of type TYPE.
3341 The real work of conversion is done by `convert'.
3342 The purpose of this function is to generate error messages
3343 for assignments that are not allowed in C.
3344 ERRTYPE is a string to use in error messages:
3345 "assignment", "return", etc. If it is null, this is parameter passing
3346 for a function call (and different error messages are output).
3348 FUNNAME is the name of the function being called,
3349 as an IDENTIFIER_NODE, or null.
3350 PARMNUM is the number of the argument, for printing in error messages. */
3353 convert_for_assignment (tree type
, tree rhs
, const char *errtype
,
3354 tree fundecl
, tree funname
, int parmnum
)
3356 enum tree_code codel
= TREE_CODE (type
);
3358 enum tree_code coder
;
3360 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3361 /* Do not use STRIP_NOPS here. We do not want an enumerator
3362 whose value is 0 to count as a null pointer constant. */
3363 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3364 rhs
= TREE_OPERAND (rhs
, 0);
3366 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
3367 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
)
3368 rhs
= default_conversion (rhs
);
3369 else if (optimize
&& TREE_CODE (rhs
) == VAR_DECL
)
3370 rhs
= decl_constant_value_for_broken_optimization (rhs
);
3372 rhstype
= TREE_TYPE (rhs
);
3373 coder
= TREE_CODE (rhstype
);
3375 if (coder
== ERROR_MARK
)
3376 return error_mark_node
;
3378 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
3380 overflow_warning (rhs
);
3381 /* Check for Objective-C protocols. This will automatically
3382 issue a warning if there are protocol violations. No need to
3383 use the return value. */
3384 if (c_dialect_objc ())
3385 objc_comptypes (type
, rhstype
, 0);
3389 if (coder
== VOID_TYPE
)
3391 error ("void value not ignored as it ought to be");
3392 return error_mark_node
;
3394 /* A type converts to a reference to it.
3395 This code doesn't fully support references, it's just for the
3396 special case of va_start and va_copy. */
3397 if (codel
== REFERENCE_TYPE
3398 && comptypes (TREE_TYPE (type
), TREE_TYPE (rhs
), COMPARE_STRICT
) == 1)
3400 if (!lvalue_p (rhs
))
3402 error ("cannot pass rvalue to reference parameter");
3403 return error_mark_node
;
3405 if (!c_mark_addressable (rhs
))
3406 return error_mark_node
;
3407 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
3409 /* We already know that these two types are compatible, but they
3410 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3411 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3412 likely to be va_list, a typedef to __builtin_va_list, which
3413 is different enough that it will cause problems later. */
3414 if (TREE_TYPE (TREE_TYPE (rhs
)) != TREE_TYPE (type
))
3415 rhs
= build1 (NOP_EXPR
, build_pointer_type (TREE_TYPE (type
)), rhs
);
3417 rhs
= build1 (NOP_EXPR
, type
, rhs
);
3420 /* Some types can interconvert without explicit casts. */
3421 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
3422 && ((*targetm
.vector_opaque_p
) (type
)
3423 || (*targetm
.vector_opaque_p
) (rhstype
)))
3424 return convert (type
, rhs
);
3425 /* Arithmetic types all interconvert, and enum is treated like int. */
3426 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
3427 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
3428 || codel
== BOOLEAN_TYPE
)
3429 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
3430 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
3431 || coder
== BOOLEAN_TYPE
))
3432 return convert_and_check (type
, rhs
);
3434 /* Conversion to a transparent union from its member types.
3435 This applies only to function arguments. */
3436 else if (codel
== UNION_TYPE
&& TYPE_TRANSPARENT_UNION (type
) && ! errtype
)
3439 tree marginal_memb_type
= 0;
3441 for (memb_types
= TYPE_FIELDS (type
); memb_types
;
3442 memb_types
= TREE_CHAIN (memb_types
))
3444 tree memb_type
= TREE_TYPE (memb_types
);
3446 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
3447 TYPE_MAIN_VARIANT (rhstype
), COMPARE_STRICT
))
3450 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
3453 if (coder
== POINTER_TYPE
)
3455 tree ttl
= TREE_TYPE (memb_type
);
3456 tree ttr
= TREE_TYPE (rhstype
);
3458 /* Any non-function converts to a [const][volatile] void *
3459 and vice versa; otherwise, targets must be the same.
3460 Meanwhile, the lhs target must have all the qualifiers of
3462 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3463 || comp_target_types (memb_type
, rhstype
, 0))
3465 /* If this type won't generate any warnings, use it. */
3466 if (TYPE_QUALS (ttl
) == TYPE_QUALS (ttr
)
3467 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
3468 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3469 ? ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3470 == TYPE_QUALS (ttr
))
3471 : ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3472 == TYPE_QUALS (ttl
))))
3475 /* Keep looking for a better type, but remember this one. */
3476 if (! marginal_memb_type
)
3477 marginal_memb_type
= memb_type
;
3481 /* Can convert integer zero to any pointer type. */
3482 if (integer_zerop (rhs
)
3483 || (TREE_CODE (rhs
) == NOP_EXPR
3484 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3486 rhs
= null_pointer_node
;
3491 if (memb_types
|| marginal_memb_type
)
3495 /* We have only a marginally acceptable member type;
3496 it needs a warning. */
3497 tree ttl
= TREE_TYPE (marginal_memb_type
);
3498 tree ttr
= TREE_TYPE (rhstype
);
3500 /* Const and volatile mean something different for function
3501 types, so the usual warnings are not appropriate. */
3502 if (TREE_CODE (ttr
) == FUNCTION_TYPE
3503 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3505 /* Because const and volatile on functions are
3506 restrictions that say the function will not do
3507 certain things, it is okay to use a const or volatile
3508 function where an ordinary one is wanted, but not
3510 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3511 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3512 errtype
, funname
, parmnum
);
3514 else if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3515 warn_for_assignment ("%s discards qualifiers from pointer target type",
3520 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (fundecl
))
3521 pedwarn ("ISO C prohibits argument conversion to union type");
3523 return build1 (NOP_EXPR
, type
, rhs
);
3527 /* Conversions among pointers */
3528 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
3529 && (coder
== codel
))
3531 tree ttl
= TREE_TYPE (type
);
3532 tree ttr
= TREE_TYPE (rhstype
);
3533 bool is_opaque_pointer
;
3534 int target_cmp
= 0; /* Cache comp_target_types () result. */
3536 /* Opaque pointers are treated like void pointers. */
3537 is_opaque_pointer
= ((*targetm
.vector_opaque_p
) (type
)
3538 || (*targetm
.vector_opaque_p
) (rhstype
))
3539 && TREE_CODE (ttl
) == VECTOR_TYPE
3540 && TREE_CODE (ttr
) == VECTOR_TYPE
;
3542 /* Any non-function converts to a [const][volatile] void *
3543 and vice versa; otherwise, targets must be the same.
3544 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3545 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3546 || (target_cmp
= comp_target_types (type
, rhstype
, 0))
3547 || is_opaque_pointer
3548 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl
))
3549 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
3552 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3555 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3556 which are not ANSI null ptr constants. */
3557 && (!integer_zerop (rhs
) || TREE_CODE (rhs
) == NOP_EXPR
)
3558 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
3559 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3560 errtype
, funname
, parmnum
);
3561 /* Const and volatile mean something different for function types,
3562 so the usual warnings are not appropriate. */
3563 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
3564 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
3566 if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3567 warn_for_assignment ("%s discards qualifiers from pointer target type",
3568 errtype
, funname
, parmnum
);
3569 /* If this is not a case of ignoring a mismatch in signedness,
3571 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3574 /* If there is a mismatch, do warn. */
3576 warn_for_assignment ("pointer targets in %s differ in signedness",
3577 errtype
, funname
, parmnum
);
3579 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
3580 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3582 /* Because const and volatile on functions are restrictions
3583 that say the function will not do certain things,
3584 it is okay to use a const or volatile function
3585 where an ordinary one is wanted, but not vice-versa. */
3586 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3587 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3588 errtype
, funname
, parmnum
);
3592 warn_for_assignment ("%s from incompatible pointer type",
3593 errtype
, funname
, parmnum
);
3594 return convert (type
, rhs
);
3596 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
3598 error ("invalid use of non-lvalue array");
3599 return error_mark_node
;
3601 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
3603 /* An explicit constant 0 can convert to a pointer,
3604 or one that results from arithmetic, even including
3605 a cast to integer type. */
3606 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
))
3608 ! (TREE_CODE (rhs
) == NOP_EXPR
3609 && TREE_CODE (TREE_TYPE (rhs
)) == INTEGER_TYPE
3610 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == INTEGER_CST
3611 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3612 warn_for_assignment ("%s makes pointer from integer without a cast",
3613 errtype
, funname
, parmnum
);
3615 return convert (type
, rhs
);
3617 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
3619 warn_for_assignment ("%s makes integer from pointer without a cast",
3620 errtype
, funname
, parmnum
);
3621 return convert (type
, rhs
);
3623 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
3624 return convert (type
, rhs
);
3630 tree selector
= objc_message_selector ();
3632 if (selector
&& parmnum
> 2)
3633 error ("incompatible type for argument %d of `%s'",
3634 parmnum
- 2, IDENTIFIER_POINTER (selector
));
3636 error ("incompatible type for argument %d of `%s'",
3637 parmnum
, IDENTIFIER_POINTER (funname
));
3640 error ("incompatible type for argument %d of indirect function call",
3644 error ("incompatible types in %s", errtype
);
3646 return error_mark_node
;
3649 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3650 is used for error and waring reporting and indicates which argument
3651 is being processed. */
3654 c_convert_parm_for_inlining (tree parm
, tree value
, tree fn
, int argnum
)
3658 /* If FN was prototyped, the value has been converted already
3659 in convert_arguments. */
3660 if (! value
|| TYPE_ARG_TYPES (TREE_TYPE (fn
)))
3663 type
= TREE_TYPE (parm
);
3664 ret
= convert_for_assignment (type
, value
,
3665 (char *) 0 /* arg passing */, fn
,
3666 DECL_NAME (fn
), argnum
);
3667 if (targetm
.calls
.promote_prototypes (TREE_TYPE (fn
))
3668 && INTEGRAL_TYPE_P (type
)
3669 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3670 ret
= default_conversion (ret
);
3674 /* Print a warning using MSGID.
3675 It gets OPNAME as its one parameter.
3676 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3677 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3678 FUNCTION and ARGNUM are handled specially if we are building an
3679 Objective-C selector. */
3682 warn_for_assignment (const char *msgid
, const char *opname
, tree function
,
3687 tree selector
= objc_message_selector ();
3690 if (selector
&& argnum
> 2)
3692 function
= selector
;
3699 /* Function name is known; supply it. */
3700 const char *const argstring
= _("passing arg of `%s'");
3701 new_opname
= alloca (IDENTIFIER_LENGTH (function
)
3702 + strlen (argstring
) + 1 + 1);
3703 sprintf (new_opname
, argstring
,
3704 IDENTIFIER_POINTER (function
));
3708 /* Function name unknown (call through ptr). */
3709 const char *const argnofun
= _("passing arg of pointer to function");
3710 new_opname
= alloca (strlen (argnofun
) + 1 + 1);
3711 sprintf (new_opname
, argnofun
);
3716 /* Function name is known; supply it. */
3717 const char *const argstring
= _("passing arg %d of `%s'");
3718 new_opname
= alloca (IDENTIFIER_LENGTH (function
)
3719 + strlen (argstring
) + 1 + 25 /*%d*/ + 1);
3720 sprintf (new_opname
, argstring
, argnum
,
3721 IDENTIFIER_POINTER (function
));
3725 /* Function name unknown (call through ptr); just give arg number. */
3726 const char *const argnofun
= _("passing arg %d of pointer to function");
3727 new_opname
= alloca (strlen (argnofun
) + 1 + 25 /*%d*/ + 1);
3728 sprintf (new_opname
, argnofun
, argnum
);
3730 opname
= new_opname
;
3732 pedwarn (msgid
, opname
);
3735 /* If VALUE is a compound expr all of whose expressions are constant, then
3736 return its value. Otherwise, return error_mark_node.
3738 This is for handling COMPOUND_EXPRs as initializer elements
3739 which is allowed with a warning when -pedantic is specified. */
3742 valid_compound_expr_initializer (tree value
, tree endtype
)
3744 if (TREE_CODE (value
) == COMPOUND_EXPR
)
3746 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
3748 return error_mark_node
;
3749 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
3752 else if (! TREE_CONSTANT (value
)
3753 && ! initializer_constant_valid_p (value
, endtype
))
3754 return error_mark_node
;
3759 /* Perform appropriate conversions on the initial value of a variable,
3760 store it in the declaration DECL,
3761 and print any error messages that are appropriate.
3762 If the init is invalid, store an ERROR_MARK. */
3765 store_init_value (tree decl
, tree init
)
3769 /* If variable's type was invalidly declared, just ignore it. */
3771 type
= TREE_TYPE (decl
);
3772 if (TREE_CODE (type
) == ERROR_MARK
)
3775 /* Digest the specified initializer into an expression. */
3777 value
= digest_init (type
, init
, TREE_STATIC (decl
));
3779 /* Store the expression if valid; else report error. */
3781 if (warn_traditional
&& !in_system_header
3782 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && ! TREE_STATIC (decl
))
3783 warning ("traditional C rejects automatic aggregate initialization");
3785 DECL_INITIAL (decl
) = value
;
3787 /* ANSI wants warnings about out-of-range constant initializers. */
3788 STRIP_TYPE_NOPS (value
);
3789 constant_expression_warning (value
);
3791 /* Check if we need to set array size from compound literal size. */
3792 if (TREE_CODE (type
) == ARRAY_TYPE
3793 && TYPE_DOMAIN (type
) == 0
3794 && value
!= error_mark_node
)
3796 tree inside_init
= init
;
3798 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
3799 inside_init
= TREE_OPERAND (init
, 0);
3800 inside_init
= fold (inside_init
);
3802 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
3804 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
3806 if (TYPE_DOMAIN (TREE_TYPE (decl
)))
3808 /* For int foo[] = (int [3]){1}; we need to set array size
3809 now since later on array initializer will be just the
3810 brace enclosed list of the compound literal. */
3811 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (decl
));
3813 layout_decl (decl
, 0);
3819 /* Methods for storing and printing names for error messages. */
3821 /* Implement a spelling stack that allows components of a name to be pushed
3822 and popped. Each element on the stack is this structure. */
3834 #define SPELLING_STRING 1
3835 #define SPELLING_MEMBER 2
3836 #define SPELLING_BOUNDS 3
3838 static struct spelling
*spelling
; /* Next stack element (unused). */
3839 static struct spelling
*spelling_base
; /* Spelling stack base. */
3840 static int spelling_size
; /* Size of the spelling stack. */
3842 /* Macros to save and restore the spelling stack around push_... functions.
3843 Alternative to SAVE_SPELLING_STACK. */
3845 #define SPELLING_DEPTH() (spelling - spelling_base)
3846 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3848 /* Push an element on the spelling stack with type KIND and assign VALUE
3851 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3853 int depth = SPELLING_DEPTH (); \
3855 if (depth >= spelling_size) \
3857 spelling_size += 10; \
3858 if (spelling_base == 0) \
3859 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3861 spelling_base = xrealloc (spelling_base, \
3862 spelling_size * sizeof (struct spelling)); \
3863 RESTORE_SPELLING_DEPTH (depth); \
3866 spelling->kind = (KIND); \
3867 spelling->MEMBER = (VALUE); \
3871 /* Push STRING on the stack. Printed literally. */
3874 push_string (const char *string
)
3876 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
3879 /* Push a member name on the stack. Printed as '.' STRING. */
3882 push_member_name (tree decl
)
3884 const char *const string
3885 = DECL_NAME (decl
) ? IDENTIFIER_POINTER (DECL_NAME (decl
)) : "<anonymous>";
3886 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
3889 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3892 push_array_bounds (int bounds
)
3894 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
3897 /* Compute the maximum size in bytes of the printed spelling. */
3900 spelling_length (void)
3905 for (p
= spelling_base
; p
< spelling
; p
++)
3907 if (p
->kind
== SPELLING_BOUNDS
)
3910 size
+= strlen (p
->u
.s
) + 1;
3916 /* Print the spelling to BUFFER and return it. */
3919 print_spelling (char *buffer
)
3924 for (p
= spelling_base
; p
< spelling
; p
++)
3925 if (p
->kind
== SPELLING_BOUNDS
)
3927 sprintf (d
, "[%d]", p
->u
.i
);
3933 if (p
->kind
== SPELLING_MEMBER
)
3935 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
3942 /* Issue an error message for a bad initializer component.
3943 MSGID identifies the message.
3944 The component name is taken from the spelling stack. */
3947 error_init (const char *msgid
)
3951 error ("%s", _(msgid
));
3952 ofwhat
= print_spelling (alloca (spelling_length () + 1));
3954 error ("(near initialization for `%s')", ofwhat
);
3957 /* Issue a pedantic warning for a bad initializer component.
3958 MSGID identifies the message.
3959 The component name is taken from the spelling stack. */
3962 pedwarn_init (const char *msgid
)
3966 pedwarn ("%s", _(msgid
));
3967 ofwhat
= print_spelling (alloca (spelling_length () + 1));
3969 pedwarn ("(near initialization for `%s')", ofwhat
);
3972 /* Issue a warning for a bad initializer component.
3973 MSGID identifies the message.
3974 The component name is taken from the spelling stack. */
3977 warning_init (const char *msgid
)
3981 warning ("%s", _(msgid
));
3982 ofwhat
= print_spelling (alloca (spelling_length () + 1));
3984 warning ("(near initialization for `%s')", ofwhat
);
3987 /* Digest the parser output INIT as an initializer for type TYPE.
3988 Return a C expression of type TYPE to represent the initial value.
3990 REQUIRE_CONSTANT requests an error if non-constant initializers or
3991 elements are seen. */
3994 digest_init (tree type
, tree init
, int require_constant
)
3996 enum tree_code code
= TREE_CODE (type
);
3997 tree inside_init
= init
;
3999 if (type
== error_mark_node
4000 || init
== error_mark_node
4001 || TREE_TYPE (init
) == error_mark_node
)
4002 return error_mark_node
;
4004 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4005 /* Do not use STRIP_NOPS here. We do not want an enumerator
4006 whose value is 0 to count as a null pointer constant. */
4007 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
4008 inside_init
= TREE_OPERAND (init
, 0);
4010 inside_init
= fold (inside_init
);
4012 /* Initialization of an array of chars from a string constant
4013 optionally enclosed in braces. */
4015 if (code
== ARRAY_TYPE
)
4017 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
4018 if ((typ1
== char_type_node
4019 || typ1
== signed_char_type_node
4020 || typ1
== unsigned_char_type_node
4021 || typ1
== unsigned_wchar_type_node
4022 || typ1
== signed_wchar_type_node
)
4023 && ((inside_init
&& TREE_CODE (inside_init
) == STRING_CST
)))
4025 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4026 TYPE_MAIN_VARIANT (type
), COMPARE_STRICT
))
4029 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)))
4031 && TYPE_PRECISION (typ1
) == TYPE_PRECISION (char_type_node
))
4033 error_init ("char-array initialized from wide string");
4034 return error_mark_node
;
4036 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)))
4038 && TYPE_PRECISION (typ1
) != TYPE_PRECISION (char_type_node
))
4040 error_init ("int-array initialized from non-wide string");
4041 return error_mark_node
;
4044 TREE_TYPE (inside_init
) = type
;
4045 if (TYPE_DOMAIN (type
) != 0
4046 && TYPE_SIZE (type
) != 0
4047 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
4048 /* Subtract 1 (or sizeof (wchar_t))
4049 because it's ok to ignore the terminating null char
4050 that is counted in the length of the constant. */
4051 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
4052 TREE_STRING_LENGTH (inside_init
)
4053 - ((TYPE_PRECISION (typ1
)
4054 != TYPE_PRECISION (char_type_node
))
4055 ? (TYPE_PRECISION (wchar_type_node
)
4058 pedwarn_init ("initializer-string for array of chars is too long");
4064 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4065 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4066 below and handle as a constructor. */
4067 if (code
== VECTOR_TYPE
4068 && comptypes (TREE_TYPE (inside_init
), type
, COMPARE_STRICT
)
4069 && TREE_CONSTANT (inside_init
))
4071 if (TREE_CODE (inside_init
) == VECTOR_CST
4072 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4073 TYPE_MAIN_VARIANT (type
),
4077 return build_vector (type
, CONSTRUCTOR_ELTS (inside_init
));
4080 /* Any type can be initialized
4081 from an expression of the same type, optionally with braces. */
4083 if (inside_init
&& TREE_TYPE (inside_init
) != 0
4084 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4085 TYPE_MAIN_VARIANT (type
), COMPARE_STRICT
)
4086 || (code
== ARRAY_TYPE
4087 && comptypes (TREE_TYPE (inside_init
), type
, COMPARE_STRICT
))
4088 || (code
== VECTOR_TYPE
4089 && comptypes (TREE_TYPE (inside_init
), type
, COMPARE_STRICT
))
4090 || (code
== POINTER_TYPE
4091 && (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4092 || TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
)
4093 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
4094 TREE_TYPE (type
), COMPARE_STRICT
))))
4096 if (code
== POINTER_TYPE
)
4098 inside_init
= default_function_array_conversion (inside_init
);
4100 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
4102 error_init ("invalid use of non-lvalue array");
4103 return error_mark_node
;
4107 if (code
== VECTOR_TYPE
)
4108 /* Although the types are compatible, we may require a
4110 inside_init
= convert (type
, inside_init
);
4112 if (require_constant
&& !flag_isoc99
4113 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
4115 /* As an extension, allow initializing objects with static storage
4116 duration with compound literals (which are then treated just as
4117 the brace enclosed list they contain). */
4118 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
4119 inside_init
= DECL_INITIAL (decl
);
4122 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
4123 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
4125 error_init ("array initialized from non-constant array expression");
4126 return error_mark_node
;
4129 if (optimize
&& TREE_CODE (inside_init
) == VAR_DECL
)
4130 inside_init
= decl_constant_value_for_broken_optimization (inside_init
);
4132 /* Compound expressions can only occur here if -pedantic or
4133 -pedantic-errors is specified. In the later case, we always want
4134 an error. In the former case, we simply want a warning. */
4135 if (require_constant
&& pedantic
4136 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
4139 = valid_compound_expr_initializer (inside_init
,
4140 TREE_TYPE (inside_init
));
4141 if (inside_init
== error_mark_node
)
4142 error_init ("initializer element is not constant");
4144 pedwarn_init ("initializer element is not constant");
4145 if (flag_pedantic_errors
)
4146 inside_init
= error_mark_node
;
4148 else if (require_constant
4149 && (!TREE_CONSTANT (inside_init
)
4150 /* This test catches things like `7 / 0' which
4151 result in an expression for which TREE_CONSTANT
4152 is true, but which is not actually something
4153 that is a legal constant. We really should not
4154 be using this function, because it is a part of
4155 the back-end. Instead, the expression should
4156 already have been turned into ERROR_MARK_NODE. */
4157 || !initializer_constant_valid_p (inside_init
,
4158 TREE_TYPE (inside_init
))))
4160 error_init ("initializer element is not constant");
4161 inside_init
= error_mark_node
;
4167 /* Handle scalar types, including conversions. */
4169 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
4170 || code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
|| code
== COMPLEX_TYPE
)
4172 /* Note that convert_for_assignment calls default_conversion
4173 for arrays and functions. We must not call it in the
4174 case where inside_init is a null pointer constant. */
4176 = convert_for_assignment (type
, init
, _("initialization"),
4177 NULL_TREE
, NULL_TREE
, 0);
4179 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4181 error_init ("initializer element is not constant");
4182 inside_init
= error_mark_node
;
4184 else if (require_constant
4185 && initializer_constant_valid_p (inside_init
, TREE_TYPE (inside_init
)) == 0)
4187 error_init ("initializer element is not computable at load time");
4188 inside_init
= error_mark_node
;
4194 /* Come here only for records and arrays. */
4196 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
4198 error_init ("variable-sized object may not be initialized");
4199 return error_mark_node
;
4202 error_init ("invalid initializer");
4203 return error_mark_node
;
4206 /* Handle initializers that use braces. */
4208 /* Type of object we are accumulating a constructor for.
4209 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4210 static tree constructor_type
;
4212 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4214 static tree constructor_fields
;
4216 /* For an ARRAY_TYPE, this is the specified index
4217 at which to store the next element we get. */
4218 static tree constructor_index
;
4220 /* For an ARRAY_TYPE, this is the maximum index. */
4221 static tree constructor_max_index
;
4223 /* For a RECORD_TYPE, this is the first field not yet written out. */
4224 static tree constructor_unfilled_fields
;
4226 /* For an ARRAY_TYPE, this is the index of the first element
4227 not yet written out. */
4228 static tree constructor_unfilled_index
;
4230 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4231 This is so we can generate gaps between fields, when appropriate. */
4232 static tree constructor_bit_index
;
4234 /* If we are saving up the elements rather than allocating them,
4235 this is the list of elements so far (in reverse order,
4236 most recent first). */
4237 static tree constructor_elements
;
4239 /* 1 if constructor should be incrementally stored into a constructor chain,
4240 0 if all the elements should be kept in AVL tree. */
4241 static int constructor_incremental
;
4243 /* 1 if so far this constructor's elements are all compile-time constants. */
4244 static int constructor_constant
;
4246 /* 1 if so far this constructor's elements are all valid address constants. */
4247 static int constructor_simple
;
4249 /* 1 if this constructor is erroneous so far. */
4250 static int constructor_erroneous
;
4252 /* Structure for managing pending initializer elements, organized as an
4257 struct init_node
*left
, *right
;
4258 struct init_node
*parent
;
4264 /* Tree of pending elements at this constructor level.
4265 These are elements encountered out of order
4266 which belong at places we haven't reached yet in actually
4268 Will never hold tree nodes across GC runs. */
4269 static struct init_node
*constructor_pending_elts
;
4271 /* The SPELLING_DEPTH of this constructor. */
4272 static int constructor_depth
;
4274 /* 0 if implicitly pushing constructor levels is allowed. */
4275 int constructor_no_implicit
= 0; /* 0 for C; 1 for some other languages. */
4277 static int require_constant_value
;
4278 static int require_constant_elements
;
4280 /* DECL node for which an initializer is being read.
4281 0 means we are reading a constructor expression
4282 such as (struct foo) {...}. */
4283 static tree constructor_decl
;
4285 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4286 static const char *constructor_asmspec
;
4288 /* Nonzero if this is an initializer for a top-level decl. */
4289 static int constructor_top_level
;
4291 /* Nonzero if there were any member designators in this initializer. */
4292 static int constructor_designated
;
4294 /* Nesting depth of designator list. */
4295 static int designator_depth
;
4297 /* Nonzero if there were diagnosed errors in this designator list. */
4298 static int designator_errorneous
;
4301 /* This stack has a level for each implicit or explicit level of
4302 structuring in the initializer, including the outermost one. It
4303 saves the values of most of the variables above. */
4305 struct constructor_range_stack
;
4307 struct constructor_stack
4309 struct constructor_stack
*next
;
4314 tree unfilled_index
;
4315 tree unfilled_fields
;
4318 struct init_node
*pending_elts
;
4321 /* If nonzero, this value should replace the entire
4322 constructor at this level. */
4323 tree replacement_value
;
4324 struct constructor_range_stack
*range_stack
;
4334 struct constructor_stack
*constructor_stack
;
4336 /* This stack represents designators from some range designator up to
4337 the last designator in the list. */
4339 struct constructor_range_stack
4341 struct constructor_range_stack
*next
, *prev
;
4342 struct constructor_stack
*stack
;
4349 struct constructor_range_stack
*constructor_range_stack
;
4351 /* This stack records separate initializers that are nested.
4352 Nested initializers can't happen in ANSI C, but GNU C allows them
4353 in cases like { ... (struct foo) { ... } ... }. */
4355 struct initializer_stack
4357 struct initializer_stack
*next
;
4359 const char *asmspec
;
4360 struct constructor_stack
*constructor_stack
;
4361 struct constructor_range_stack
*constructor_range_stack
;
4363 struct spelling
*spelling
;
4364 struct spelling
*spelling_base
;
4367 char require_constant_value
;
4368 char require_constant_elements
;
4371 struct initializer_stack
*initializer_stack
;
4373 /* Prepare to parse and output the initializer for variable DECL. */
4376 start_init (tree decl
, tree asmspec_tree
, int top_level
)
4379 struct initializer_stack
*p
= xmalloc (sizeof (struct initializer_stack
));
4380 const char *asmspec
= 0;
4383 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
4385 p
->decl
= constructor_decl
;
4386 p
->asmspec
= constructor_asmspec
;
4387 p
->require_constant_value
= require_constant_value
;
4388 p
->require_constant_elements
= require_constant_elements
;
4389 p
->constructor_stack
= constructor_stack
;
4390 p
->constructor_range_stack
= constructor_range_stack
;
4391 p
->elements
= constructor_elements
;
4392 p
->spelling
= spelling
;
4393 p
->spelling_base
= spelling_base
;
4394 p
->spelling_size
= spelling_size
;
4395 p
->top_level
= constructor_top_level
;
4396 p
->next
= initializer_stack
;
4397 initializer_stack
= p
;
4399 constructor_decl
= decl
;
4400 constructor_asmspec
= asmspec
;
4401 constructor_designated
= 0;
4402 constructor_top_level
= top_level
;
4406 require_constant_value
= TREE_STATIC (decl
);
4407 require_constant_elements
4408 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
4409 /* For a scalar, you can always use any value to initialize,
4410 even within braces. */
4411 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
4412 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
4413 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
4414 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
4415 locus
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4419 require_constant_value
= 0;
4420 require_constant_elements
= 0;
4421 locus
= "(anonymous)";
4424 constructor_stack
= 0;
4425 constructor_range_stack
= 0;
4427 missing_braces_mentioned
= 0;
4431 RESTORE_SPELLING_DEPTH (0);
4434 push_string (locus
);
4440 struct initializer_stack
*p
= initializer_stack
;
4442 /* Free the whole constructor stack of this initializer. */
4443 while (constructor_stack
)
4445 struct constructor_stack
*q
= constructor_stack
;
4446 constructor_stack
= q
->next
;
4450 if (constructor_range_stack
)
4453 /* Pop back to the data of the outer initializer (if any). */
4454 free (spelling_base
);
4456 constructor_decl
= p
->decl
;
4457 constructor_asmspec
= p
->asmspec
;
4458 require_constant_value
= p
->require_constant_value
;
4459 require_constant_elements
= p
->require_constant_elements
;
4460 constructor_stack
= p
->constructor_stack
;
4461 constructor_range_stack
= p
->constructor_range_stack
;
4462 constructor_elements
= p
->elements
;
4463 spelling
= p
->spelling
;
4464 spelling_base
= p
->spelling_base
;
4465 spelling_size
= p
->spelling_size
;
4466 constructor_top_level
= p
->top_level
;
4467 initializer_stack
= p
->next
;
4471 /* Call here when we see the initializer is surrounded by braces.
4472 This is instead of a call to push_init_level;
4473 it is matched by a call to pop_init_level.
4475 TYPE is the type to initialize, for a constructor expression.
4476 For an initializer for a decl, TYPE is zero. */
4479 really_start_incremental_init (tree type
)
4481 struct constructor_stack
*p
= xmalloc (sizeof (struct constructor_stack
));
4484 type
= TREE_TYPE (constructor_decl
);
4486 if ((*targetm
.vector_opaque_p
) (type
))
4487 error ("opaque vector types cannot be initialized");
4489 p
->type
= constructor_type
;
4490 p
->fields
= constructor_fields
;
4491 p
->index
= constructor_index
;
4492 p
->max_index
= constructor_max_index
;
4493 p
->unfilled_index
= constructor_unfilled_index
;
4494 p
->unfilled_fields
= constructor_unfilled_fields
;
4495 p
->bit_index
= constructor_bit_index
;
4496 p
->elements
= constructor_elements
;
4497 p
->constant
= constructor_constant
;
4498 p
->simple
= constructor_simple
;
4499 p
->erroneous
= constructor_erroneous
;
4500 p
->pending_elts
= constructor_pending_elts
;
4501 p
->depth
= constructor_depth
;
4502 p
->replacement_value
= 0;
4506 p
->incremental
= constructor_incremental
;
4507 p
->designated
= constructor_designated
;
4509 constructor_stack
= p
;
4511 constructor_constant
= 1;
4512 constructor_simple
= 1;
4513 constructor_depth
= SPELLING_DEPTH ();
4514 constructor_elements
= 0;
4515 constructor_pending_elts
= 0;
4516 constructor_type
= type
;
4517 constructor_incremental
= 1;
4518 constructor_designated
= 0;
4519 designator_depth
= 0;
4520 designator_errorneous
= 0;
4522 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4523 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4525 constructor_fields
= TYPE_FIELDS (constructor_type
);
4526 /* Skip any nameless bit fields at the beginning. */
4527 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4528 && DECL_NAME (constructor_fields
) == 0)
4529 constructor_fields
= TREE_CHAIN (constructor_fields
);
4531 constructor_unfilled_fields
= constructor_fields
;
4532 constructor_bit_index
= bitsize_zero_node
;
4534 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4536 if (TYPE_DOMAIN (constructor_type
))
4538 constructor_max_index
4539 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4541 /* Detect non-empty initializations of zero-length arrays. */
4542 if (constructor_max_index
== NULL_TREE
4543 && TYPE_SIZE (constructor_type
))
4544 constructor_max_index
= build_int_2 (-1, -1);
4546 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4547 to initialize VLAs will cause a proper error; avoid tree
4548 checking errors as well by setting a safe value. */
4549 if (constructor_max_index
4550 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4551 constructor_max_index
= build_int_2 (-1, -1);
4554 = convert (bitsizetype
,
4555 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4558 constructor_index
= bitsize_zero_node
;
4560 constructor_unfilled_index
= constructor_index
;
4562 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4564 /* Vectors are like simple fixed-size arrays. */
4565 constructor_max_index
=
4566 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1, 0);
4567 constructor_index
= convert (bitsizetype
, bitsize_zero_node
);
4568 constructor_unfilled_index
= constructor_index
;
4572 /* Handle the case of int x = {5}; */
4573 constructor_fields
= constructor_type
;
4574 constructor_unfilled_fields
= constructor_type
;
4578 /* Push down into a subobject, for initialization.
4579 If this is for an explicit set of braces, IMPLICIT is 0.
4580 If it is because the next element belongs at a lower level,
4581 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4584 push_init_level (int implicit
)
4586 struct constructor_stack
*p
;
4587 tree value
= NULL_TREE
;
4589 /* If we've exhausted any levels that didn't have braces,
4591 while (constructor_stack
->implicit
)
4593 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4594 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4595 && constructor_fields
== 0)
4596 process_init_element (pop_init_level (1));
4597 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
4598 && constructor_max_index
4599 && tree_int_cst_lt (constructor_max_index
, constructor_index
))
4600 process_init_element (pop_init_level (1));
4605 /* Unless this is an explicit brace, we need to preserve previous
4609 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4610 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4611 && constructor_fields
)
4612 value
= find_init_member (constructor_fields
);
4613 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4614 value
= find_init_member (constructor_index
);
4617 p
= xmalloc (sizeof (struct constructor_stack
));
4618 p
->type
= constructor_type
;
4619 p
->fields
= constructor_fields
;
4620 p
->index
= constructor_index
;
4621 p
->max_index
= constructor_max_index
;
4622 p
->unfilled_index
= constructor_unfilled_index
;
4623 p
->unfilled_fields
= constructor_unfilled_fields
;
4624 p
->bit_index
= constructor_bit_index
;
4625 p
->elements
= constructor_elements
;
4626 p
->constant
= constructor_constant
;
4627 p
->simple
= constructor_simple
;
4628 p
->erroneous
= constructor_erroneous
;
4629 p
->pending_elts
= constructor_pending_elts
;
4630 p
->depth
= constructor_depth
;
4631 p
->replacement_value
= 0;
4632 p
->implicit
= implicit
;
4634 p
->incremental
= constructor_incremental
;
4635 p
->designated
= constructor_designated
;
4636 p
->next
= constructor_stack
;
4638 constructor_stack
= p
;
4640 constructor_constant
= 1;
4641 constructor_simple
= 1;
4642 constructor_depth
= SPELLING_DEPTH ();
4643 constructor_elements
= 0;
4644 constructor_incremental
= 1;
4645 constructor_designated
= 0;
4646 constructor_pending_elts
= 0;
4649 p
->range_stack
= constructor_range_stack
;
4650 constructor_range_stack
= 0;
4651 designator_depth
= 0;
4652 designator_errorneous
= 0;
4655 /* Don't die if an entire brace-pair level is superfluous
4656 in the containing level. */
4657 if (constructor_type
== 0)
4659 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
4660 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4662 /* Don't die if there are extra init elts at the end. */
4663 if (constructor_fields
== 0)
4664 constructor_type
= 0;
4667 constructor_type
= TREE_TYPE (constructor_fields
);
4668 push_member_name (constructor_fields
);
4669 constructor_depth
++;
4672 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4674 constructor_type
= TREE_TYPE (constructor_type
);
4675 push_array_bounds (tree_low_cst (constructor_index
, 0));
4676 constructor_depth
++;
4679 if (constructor_type
== 0)
4681 error_init ("extra brace group at end of initializer");
4682 constructor_fields
= 0;
4683 constructor_unfilled_fields
= 0;
4687 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
4689 constructor_constant
= TREE_CONSTANT (value
);
4690 constructor_simple
= TREE_STATIC (value
);
4691 constructor_elements
= CONSTRUCTOR_ELTS (value
);
4692 if (constructor_elements
4693 && (TREE_CODE (constructor_type
) == RECORD_TYPE
4694 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
4695 set_nonincremental_init ();
4698 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
4700 missing_braces_mentioned
= 1;
4701 warning_init ("missing braces around initializer");
4704 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4705 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4707 constructor_fields
= TYPE_FIELDS (constructor_type
);
4708 /* Skip any nameless bit fields at the beginning. */
4709 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4710 && DECL_NAME (constructor_fields
) == 0)
4711 constructor_fields
= TREE_CHAIN (constructor_fields
);
4713 constructor_unfilled_fields
= constructor_fields
;
4714 constructor_bit_index
= bitsize_zero_node
;
4716 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4718 /* Vectors are like simple fixed-size arrays. */
4719 constructor_max_index
=
4720 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1, 0);
4721 constructor_index
= convert (bitsizetype
, integer_zero_node
);
4722 constructor_unfilled_index
= constructor_index
;
4724 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4726 if (TYPE_DOMAIN (constructor_type
))
4728 constructor_max_index
4729 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4731 /* Detect non-empty initializations of zero-length arrays. */
4732 if (constructor_max_index
== NULL_TREE
4733 && TYPE_SIZE (constructor_type
))
4734 constructor_max_index
= build_int_2 (-1, -1);
4736 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4737 to initialize VLAs will cause a proper error; avoid tree
4738 checking errors as well by setting a safe value. */
4739 if (constructor_max_index
4740 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4741 constructor_max_index
= build_int_2 (-1, -1);
4744 = convert (bitsizetype
,
4745 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4748 constructor_index
= bitsize_zero_node
;
4750 constructor_unfilled_index
= constructor_index
;
4751 if (value
&& TREE_CODE (value
) == STRING_CST
)
4753 /* We need to split the char/wchar array into individual
4754 characters, so that we don't have to special case it
4756 set_nonincremental_init_from_string (value
);
4761 warning_init ("braces around scalar initializer");
4762 constructor_fields
= constructor_type
;
4763 constructor_unfilled_fields
= constructor_type
;
4767 /* At the end of an implicit or explicit brace level,
4768 finish up that level of constructor.
4769 If we were outputting the elements as they are read, return 0
4770 from inner levels (process_init_element ignores that),
4771 but return error_mark_node from the outermost level
4772 (that's what we want to put in DECL_INITIAL).
4773 Otherwise, return a CONSTRUCTOR expression. */
4776 pop_init_level (int implicit
)
4778 struct constructor_stack
*p
;
4779 tree constructor
= 0;
4783 /* When we come to an explicit close brace,
4784 pop any inner levels that didn't have explicit braces. */
4785 while (constructor_stack
->implicit
)
4786 process_init_element (pop_init_level (1));
4788 if (constructor_range_stack
)
4792 p
= constructor_stack
;
4794 /* Error for initializing a flexible array member, or a zero-length
4795 array member in an inappropriate context. */
4796 if (constructor_type
&& constructor_fields
4797 && TREE_CODE (constructor_type
) == ARRAY_TYPE
4798 && TYPE_DOMAIN (constructor_type
)
4799 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
4801 /* Silently discard empty initializations. The parser will
4802 already have pedwarned for empty brackets. */
4803 if (integer_zerop (constructor_unfilled_index
))
4804 constructor_type
= NULL_TREE
;
4805 else if (! TYPE_SIZE (constructor_type
))
4807 if (constructor_depth
> 2)
4808 error_init ("initialization of flexible array member in a nested context");
4810 pedwarn_init ("initialization of a flexible array member");
4812 /* We have already issued an error message for the existence
4813 of a flexible array member not at the end of the structure.
4814 Discard the initializer so that we do not abort later. */
4815 if (TREE_CHAIN (constructor_fields
) != NULL_TREE
)
4816 constructor_type
= NULL_TREE
;
4819 /* Zero-length arrays are no longer special, so we should no longer
4824 /* Warn when some struct elements are implicitly initialized to zero. */
4827 && TREE_CODE (constructor_type
) == RECORD_TYPE
4828 && constructor_unfilled_fields
)
4830 /* Do not warn for flexible array members or zero-length arrays. */
4831 while (constructor_unfilled_fields
4832 && (! DECL_SIZE (constructor_unfilled_fields
)
4833 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
4834 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
4836 /* Do not warn if this level of the initializer uses member
4837 designators; it is likely to be deliberate. */
4838 if (constructor_unfilled_fields
&& !constructor_designated
)
4840 push_member_name (constructor_unfilled_fields
);
4841 warning_init ("missing initializer");
4842 RESTORE_SPELLING_DEPTH (constructor_depth
);
4846 /* Now output all pending elements. */
4847 constructor_incremental
= 1;
4848 output_pending_init_elements (1);
4850 /* Pad out the end of the structure. */
4851 if (p
->replacement_value
)
4852 /* If this closes a superfluous brace pair,
4853 just pass out the element between them. */
4854 constructor
= p
->replacement_value
;
4855 else if (constructor_type
== 0)
4857 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
4858 && TREE_CODE (constructor_type
) != UNION_TYPE
4859 && TREE_CODE (constructor_type
) != ARRAY_TYPE
4860 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
4862 /* A nonincremental scalar initializer--just return
4863 the element, after verifying there is just one. */
4864 if (constructor_elements
== 0)
4866 if (!constructor_erroneous
)
4867 error_init ("empty scalar initializer");
4868 constructor
= error_mark_node
;
4870 else if (TREE_CHAIN (constructor_elements
) != 0)
4872 error_init ("extra elements in scalar initializer");
4873 constructor
= TREE_VALUE (constructor_elements
);
4876 constructor
= TREE_VALUE (constructor_elements
);
4880 if (constructor_erroneous
)
4881 constructor
= error_mark_node
;
4884 constructor
= build_constructor (constructor_type
,
4885 nreverse (constructor_elements
));
4886 if (constructor_constant
)
4887 TREE_CONSTANT (constructor
) = 1;
4888 if (constructor_constant
&& constructor_simple
)
4889 TREE_STATIC (constructor
) = 1;
4893 constructor_type
= p
->type
;
4894 constructor_fields
= p
->fields
;
4895 constructor_index
= p
->index
;
4896 constructor_max_index
= p
->max_index
;
4897 constructor_unfilled_index
= p
->unfilled_index
;
4898 constructor_unfilled_fields
= p
->unfilled_fields
;
4899 constructor_bit_index
= p
->bit_index
;
4900 constructor_elements
= p
->elements
;
4901 constructor_constant
= p
->constant
;
4902 constructor_simple
= p
->simple
;
4903 constructor_erroneous
= p
->erroneous
;
4904 constructor_incremental
= p
->incremental
;
4905 constructor_designated
= p
->designated
;
4906 constructor_pending_elts
= p
->pending_elts
;
4907 constructor_depth
= p
->depth
;
4909 constructor_range_stack
= p
->range_stack
;
4910 RESTORE_SPELLING_DEPTH (constructor_depth
);
4912 constructor_stack
= p
->next
;
4915 if (constructor
== 0)
4917 if (constructor_stack
== 0)
4918 return error_mark_node
;
4924 /* Common handling for both array range and field name designators.
4925 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4928 set_designator (int array
)
4931 enum tree_code subcode
;
4933 /* Don't die if an entire brace-pair level is superfluous
4934 in the containing level. */
4935 if (constructor_type
== 0)
4938 /* If there were errors in this designator list already, bail out silently. */
4939 if (designator_errorneous
)
4942 if (!designator_depth
)
4944 if (constructor_range_stack
)
4947 /* Designator list starts at the level of closest explicit
4949 while (constructor_stack
->implicit
)
4950 process_init_element (pop_init_level (1));
4951 constructor_designated
= 1;
4955 if (constructor_no_implicit
)
4957 error_init ("initialization designators may not nest");
4961 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4962 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4964 subtype
= TREE_TYPE (constructor_fields
);
4965 if (subtype
!= error_mark_node
)
4966 subtype
= TYPE_MAIN_VARIANT (subtype
);
4968 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4970 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
4975 subcode
= TREE_CODE (subtype
);
4976 if (array
&& subcode
!= ARRAY_TYPE
)
4978 error_init ("array index in non-array initializer");
4981 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
4983 error_init ("field name not in record or union initializer");
4987 constructor_designated
= 1;
4988 push_init_level (2);
4992 /* If there are range designators in designator list, push a new designator
4993 to constructor_range_stack. RANGE_END is end of such stack range or
4994 NULL_TREE if there is no range designator at this level. */
4997 push_range_stack (tree range_end
)
4999 struct constructor_range_stack
*p
;
5001 p
= ggc_alloc (sizeof (struct constructor_range_stack
));
5002 p
->prev
= constructor_range_stack
;
5004 p
->fields
= constructor_fields
;
5005 p
->range_start
= constructor_index
;
5006 p
->index
= constructor_index
;
5007 p
->stack
= constructor_stack
;
5008 p
->range_end
= range_end
;
5009 if (constructor_range_stack
)
5010 constructor_range_stack
->next
= p
;
5011 constructor_range_stack
= p
;
5014 /* Within an array initializer, specify the next index to be initialized.
5015 FIRST is that index. If LAST is nonzero, then initialize a range
5016 of indices, running from FIRST through LAST. */
5019 set_init_index (tree first
, tree last
)
5021 if (set_designator (1))
5024 designator_errorneous
= 1;
5026 while ((TREE_CODE (first
) == NOP_EXPR
5027 || TREE_CODE (first
) == CONVERT_EXPR
5028 || TREE_CODE (first
) == NON_LVALUE_EXPR
)
5029 && (TYPE_MODE (TREE_TYPE (first
))
5030 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first
, 0)))))
5031 first
= TREE_OPERAND (first
, 0);
5034 while ((TREE_CODE (last
) == NOP_EXPR
5035 || TREE_CODE (last
) == CONVERT_EXPR
5036 || TREE_CODE (last
) == NON_LVALUE_EXPR
)
5037 && (TYPE_MODE (TREE_TYPE (last
))
5038 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last
, 0)))))
5039 last
= TREE_OPERAND (last
, 0);
5041 if (TREE_CODE (first
) != INTEGER_CST
)
5042 error_init ("nonconstant array index in initializer");
5043 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
5044 error_init ("nonconstant array index in initializer");
5045 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5046 error_init ("array index in non-array initializer");
5047 else if (tree_int_cst_sgn (first
) == -1)
5048 error_init ("array index in initializer exceeds array bounds");
5049 else if (constructor_max_index
5050 && tree_int_cst_lt (constructor_max_index
, first
))
5051 error_init ("array index in initializer exceeds array bounds");
5054 constructor_index
= convert (bitsizetype
, first
);
5058 if (tree_int_cst_equal (first
, last
))
5060 else if (tree_int_cst_lt (last
, first
))
5062 error_init ("empty index range in initializer");
5067 last
= convert (bitsizetype
, last
);
5068 if (constructor_max_index
!= 0
5069 && tree_int_cst_lt (constructor_max_index
, last
))
5071 error_init ("array index range in initializer exceeds array bounds");
5078 designator_errorneous
= 0;
5079 if (constructor_range_stack
|| last
)
5080 push_range_stack (last
);
5084 /* Within a struct initializer, specify the next field to be initialized. */
5087 set_init_label (tree fieldname
)
5091 if (set_designator (0))
5094 designator_errorneous
= 1;
5096 if (TREE_CODE (constructor_type
) != RECORD_TYPE
5097 && TREE_CODE (constructor_type
) != UNION_TYPE
)
5099 error_init ("field name not in record or union initializer");
5103 for (tail
= TYPE_FIELDS (constructor_type
); tail
;
5104 tail
= TREE_CHAIN (tail
))
5106 if (DECL_NAME (tail
) == fieldname
)
5111 error ("unknown field `%s' specified in initializer",
5112 IDENTIFIER_POINTER (fieldname
));
5115 constructor_fields
= tail
;
5117 designator_errorneous
= 0;
5118 if (constructor_range_stack
)
5119 push_range_stack (NULL_TREE
);
5123 /* Add a new initializer to the tree of pending initializers. PURPOSE
5124 identifies the initializer, either array index or field in a structure.
5125 VALUE is the value of that index or field. */
5128 add_pending_init (tree purpose
, tree value
)
5130 struct init_node
*p
, **q
, *r
;
5132 q
= &constructor_pending_elts
;
5135 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5140 if (tree_int_cst_lt (purpose
, p
->purpose
))
5142 else if (tree_int_cst_lt (p
->purpose
, purpose
))
5146 if (TREE_SIDE_EFFECTS (p
->value
))
5147 warning_init ("initialized field with side-effects overwritten");
5157 bitpos
= bit_position (purpose
);
5161 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5163 else if (p
->purpose
!= purpose
)
5167 if (TREE_SIDE_EFFECTS (p
->value
))
5168 warning_init ("initialized field with side-effects overwritten");
5175 r
= ggc_alloc (sizeof (struct init_node
));
5176 r
->purpose
= purpose
;
5187 struct init_node
*s
;
5191 if (p
->balance
== 0)
5193 else if (p
->balance
< 0)
5200 p
->left
->parent
= p
;
5217 constructor_pending_elts
= r
;
5222 struct init_node
*t
= r
->right
;
5226 r
->right
->parent
= r
;
5231 p
->left
->parent
= p
;
5234 p
->balance
= t
->balance
< 0;
5235 r
->balance
= -(t
->balance
> 0);
5250 constructor_pending_elts
= t
;
5256 /* p->balance == +1; growth of left side balances the node. */
5261 else /* r == p->right */
5263 if (p
->balance
== 0)
5264 /* Growth propagation from right side. */
5266 else if (p
->balance
> 0)
5273 p
->right
->parent
= p
;
5290 constructor_pending_elts
= r
;
5292 else /* r->balance == -1 */
5295 struct init_node
*t
= r
->left
;
5299 r
->left
->parent
= r
;
5304 p
->right
->parent
= p
;
5307 r
->balance
= (t
->balance
< 0);
5308 p
->balance
= -(t
->balance
> 0);
5323 constructor_pending_elts
= t
;
5329 /* p->balance == -1; growth of right side balances the node. */
5340 /* Build AVL tree from a sorted chain. */
5343 set_nonincremental_init (void)
5347 if (TREE_CODE (constructor_type
) != RECORD_TYPE
5348 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5351 for (chain
= constructor_elements
; chain
; chain
= TREE_CHAIN (chain
))
5352 add_pending_init (TREE_PURPOSE (chain
), TREE_VALUE (chain
));
5353 constructor_elements
= 0;
5354 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5356 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
5357 /* Skip any nameless bit fields at the beginning. */
5358 while (constructor_unfilled_fields
!= 0
5359 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5360 && DECL_NAME (constructor_unfilled_fields
) == 0)
5361 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
5364 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5366 if (TYPE_DOMAIN (constructor_type
))
5367 constructor_unfilled_index
5368 = convert (bitsizetype
,
5369 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
5371 constructor_unfilled_index
= bitsize_zero_node
;
5373 constructor_incremental
= 0;
5376 /* Build AVL tree from a string constant. */
5379 set_nonincremental_init_from_string (tree str
)
5381 tree value
, purpose
, type
;
5382 HOST_WIDE_INT val
[2];
5383 const char *p
, *end
;
5384 int byte
, wchar_bytes
, charwidth
, bitpos
;
5386 if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5389 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5390 == TYPE_PRECISION (char_type_node
))
5392 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5393 == TYPE_PRECISION (wchar_type_node
))
5394 wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
5398 charwidth
= TYPE_PRECISION (char_type_node
);
5399 type
= TREE_TYPE (constructor_type
);
5400 p
= TREE_STRING_POINTER (str
);
5401 end
= p
+ TREE_STRING_LENGTH (str
);
5403 for (purpose
= bitsize_zero_node
;
5404 p
< end
&& !tree_int_cst_lt (constructor_max_index
, purpose
);
5405 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
5407 if (wchar_bytes
== 1)
5409 val
[1] = (unsigned char) *p
++;
5416 for (byte
= 0; byte
< wchar_bytes
; byte
++)
5418 if (BYTES_BIG_ENDIAN
)
5419 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
5421 bitpos
= byte
* charwidth
;
5422 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
5423 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
5424 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
5428 if (!TREE_UNSIGNED (type
))
5430 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
5431 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
5433 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
5435 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
5439 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
5444 else if (val
[0] & (((HOST_WIDE_INT
) 1)
5445 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
5446 val
[0] |= ((HOST_WIDE_INT
) -1)
5447 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
5450 value
= build_int_2 (val
[1], val
[0]);
5451 TREE_TYPE (value
) = type
;
5452 add_pending_init (purpose
, value
);
5455 constructor_incremental
= 0;
5458 /* Return value of FIELD in pending initializer or zero if the field was
5459 not initialized yet. */
5462 find_init_member (tree field
)
5464 struct init_node
*p
;
5466 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5468 if (constructor_incremental
5469 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5470 set_nonincremental_init ();
5472 p
= constructor_pending_elts
;
5475 if (tree_int_cst_lt (field
, p
->purpose
))
5477 else if (tree_int_cst_lt (p
->purpose
, field
))
5483 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5485 tree bitpos
= bit_position (field
);
5487 if (constructor_incremental
5488 && (!constructor_unfilled_fields
5489 || tree_int_cst_lt (bitpos
,
5490 bit_position (constructor_unfilled_fields
))))
5491 set_nonincremental_init ();
5493 p
= constructor_pending_elts
;
5496 if (field
== p
->purpose
)
5498 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5504 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5506 if (constructor_elements
5507 && TREE_PURPOSE (constructor_elements
) == field
)
5508 return TREE_VALUE (constructor_elements
);
5513 /* "Output" the next constructor element.
5514 At top level, really output it to assembler code now.
5515 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5516 TYPE is the data type that the containing data type wants here.
5517 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5519 PENDING if non-nil means output pending elements that belong
5520 right after this element. (PENDING is normally 1;
5521 it is 0 while outputting pending elements, to avoid recursion.) */
5524 output_init_element (tree value
, tree type
, tree field
, int pending
)
5526 if (type
== error_mark_node
)
5528 constructor_erroneous
= 1;
5531 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5532 || (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5533 && !(TREE_CODE (value
) == STRING_CST
5534 && TREE_CODE (type
) == ARRAY_TYPE
5535 && TREE_CODE (TREE_TYPE (type
)) == INTEGER_TYPE
)
5536 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
5537 TYPE_MAIN_VARIANT (type
), COMPARE_STRICT
)))
5538 value
= default_conversion (value
);
5540 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
5541 && require_constant_value
&& !flag_isoc99
&& pending
)
5543 /* As an extension, allow initializing objects with static storage
5544 duration with compound literals (which are then treated just as
5545 the brace enclosed list they contain). */
5546 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
5547 value
= DECL_INITIAL (decl
);
5550 if (value
== error_mark_node
)
5551 constructor_erroneous
= 1;
5552 else if (!TREE_CONSTANT (value
))
5553 constructor_constant
= 0;
5554 else if (initializer_constant_valid_p (value
, TREE_TYPE (value
)) == 0
5555 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
5556 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5557 && DECL_C_BIT_FIELD (field
)
5558 && TREE_CODE (value
) != INTEGER_CST
))
5559 constructor_simple
= 0;
5561 if (require_constant_value
&& ! TREE_CONSTANT (value
))
5563 error_init ("initializer element is not constant");
5564 value
= error_mark_node
;
5566 else if (require_constant_elements
5567 && initializer_constant_valid_p (value
, TREE_TYPE (value
)) == 0)
5568 pedwarn ("initializer element is not computable at load time");
5570 /* If this field is empty (and not at the end of structure),
5571 don't do anything other than checking the initializer. */
5573 && (TREE_TYPE (field
) == error_mark_node
5574 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
5575 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
5576 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
5577 || TREE_CHAIN (field
)))))
5580 value
= digest_init (type
, value
, require_constant_value
);
5581 if (value
== error_mark_node
)
5583 constructor_erroneous
= 1;
5587 /* If this element doesn't come next in sequence,
5588 put it on constructor_pending_elts. */
5589 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5590 && (!constructor_incremental
5591 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
5593 if (constructor_incremental
5594 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5595 set_nonincremental_init ();
5597 add_pending_init (field
, value
);
5600 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5601 && (!constructor_incremental
5602 || field
!= constructor_unfilled_fields
))
5604 /* We do this for records but not for unions. In a union,
5605 no matter which field is specified, it can be initialized
5606 right away since it starts at the beginning of the union. */
5607 if (constructor_incremental
)
5609 if (!constructor_unfilled_fields
)
5610 set_nonincremental_init ();
5613 tree bitpos
, unfillpos
;
5615 bitpos
= bit_position (field
);
5616 unfillpos
= bit_position (constructor_unfilled_fields
);
5618 if (tree_int_cst_lt (bitpos
, unfillpos
))
5619 set_nonincremental_init ();
5623 add_pending_init (field
, value
);
5626 else if (TREE_CODE (constructor_type
) == UNION_TYPE
5627 && constructor_elements
)
5629 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements
)))
5630 warning_init ("initialized field with side-effects overwritten");
5632 /* We can have just one union field set. */
5633 constructor_elements
= 0;
5636 /* Otherwise, output this element either to
5637 constructor_elements or to the assembler file. */
5639 if (field
&& TREE_CODE (field
) == INTEGER_CST
)
5640 field
= copy_node (field
);
5641 constructor_elements
5642 = tree_cons (field
, value
, constructor_elements
);
5644 /* Advance the variable that indicates sequential elements output. */
5645 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5646 constructor_unfilled_index
5647 = size_binop (PLUS_EXPR
, constructor_unfilled_index
,
5649 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5651 constructor_unfilled_fields
5652 = TREE_CHAIN (constructor_unfilled_fields
);
5654 /* Skip any nameless bit fields. */
5655 while (constructor_unfilled_fields
!= 0
5656 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5657 && DECL_NAME (constructor_unfilled_fields
) == 0)
5658 constructor_unfilled_fields
=
5659 TREE_CHAIN (constructor_unfilled_fields
);
5661 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5662 constructor_unfilled_fields
= 0;
5664 /* Now output any pending elements which have become next. */
5666 output_pending_init_elements (0);
5669 /* Output any pending elements which have become next.
5670 As we output elements, constructor_unfilled_{fields,index}
5671 advances, which may cause other elements to become next;
5672 if so, they too are output.
5674 If ALL is 0, we return when there are
5675 no more pending elements to output now.
5677 If ALL is 1, we output space as necessary so that
5678 we can output all the pending elements. */
5681 output_pending_init_elements (int all
)
5683 struct init_node
*elt
= constructor_pending_elts
;
5688 /* Look through the whole pending tree.
5689 If we find an element that should be output now,
5690 output it. Otherwise, set NEXT to the element
5691 that comes first among those still pending. */
5696 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5698 if (tree_int_cst_equal (elt
->purpose
,
5699 constructor_unfilled_index
))
5700 output_init_element (elt
->value
,
5701 TREE_TYPE (constructor_type
),
5702 constructor_unfilled_index
, 0);
5703 else if (tree_int_cst_lt (constructor_unfilled_index
,
5706 /* Advance to the next smaller node. */
5711 /* We have reached the smallest node bigger than the
5712 current unfilled index. Fill the space first. */
5713 next
= elt
->purpose
;
5719 /* Advance to the next bigger node. */
5724 /* We have reached the biggest node in a subtree. Find
5725 the parent of it, which is the next bigger node. */
5726 while (elt
->parent
&& elt
->parent
->right
== elt
)
5729 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
5732 next
= elt
->purpose
;
5738 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5739 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5741 tree ctor_unfilled_bitpos
, elt_bitpos
;
5743 /* If the current record is complete we are done. */
5744 if (constructor_unfilled_fields
== 0)
5747 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
5748 elt_bitpos
= bit_position (elt
->purpose
);
5749 /* We can't compare fields here because there might be empty
5750 fields in between. */
5751 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
5753 constructor_unfilled_fields
= elt
->purpose
;
5754 output_init_element (elt
->value
, TREE_TYPE (elt
->purpose
),
5757 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
5759 /* Advance to the next smaller node. */
5764 /* We have reached the smallest node bigger than the
5765 current unfilled field. Fill the space first. */
5766 next
= elt
->purpose
;
5772 /* Advance to the next bigger node. */
5777 /* We have reached the biggest node in a subtree. Find
5778 the parent of it, which is the next bigger node. */
5779 while (elt
->parent
&& elt
->parent
->right
== elt
)
5783 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
5784 bit_position (elt
->purpose
))))
5786 next
= elt
->purpose
;
5794 /* Ordinarily return, but not if we want to output all
5795 and there are elements left. */
5796 if (! (all
&& next
!= 0))
5799 /* If it's not incremental, just skip over the gap, so that after
5800 jumping to retry we will output the next successive element. */
5801 if (TREE_CODE (constructor_type
) == RECORD_TYPE
5802 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5803 constructor_unfilled_fields
= next
;
5804 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5805 constructor_unfilled_index
= next
;
5807 /* ELT now points to the node in the pending tree with the next
5808 initializer to output. */
5812 /* Add one non-braced element to the current constructor level.
5813 This adjusts the current position within the constructor's type.
5814 This may also start or terminate implicit levels
5815 to handle a partly-braced initializer.
5817 Once this has found the correct level for the new element,
5818 it calls output_init_element. */
5821 process_init_element (tree value
)
5823 tree orig_value
= value
;
5824 int string_flag
= value
!= 0 && TREE_CODE (value
) == STRING_CST
;
5826 designator_depth
= 0;
5827 designator_errorneous
= 0;
5829 /* Handle superfluous braces around string cst as in
5830 char x[] = {"foo"}; */
5833 && TREE_CODE (constructor_type
) == ARRAY_TYPE
5834 && TREE_CODE (TREE_TYPE (constructor_type
)) == INTEGER_TYPE
5835 && integer_zerop (constructor_unfilled_index
))
5837 if (constructor_stack
->replacement_value
)
5838 error_init ("excess elements in char array initializer");
5839 constructor_stack
->replacement_value
= value
;
5843 if (constructor_stack
->replacement_value
!= 0)
5845 error_init ("excess elements in struct initializer");
5849 /* Ignore elements of a brace group if it is entirely superfluous
5850 and has already been diagnosed. */
5851 if (constructor_type
== 0)
5854 /* If we've exhausted any levels that didn't have braces,
5856 while (constructor_stack
->implicit
)
5858 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
5859 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5860 && constructor_fields
== 0)
5861 process_init_element (pop_init_level (1));
5862 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5863 && (constructor_max_index
== 0
5864 || tree_int_cst_lt (constructor_max_index
,
5865 constructor_index
)))
5866 process_init_element (pop_init_level (1));
5871 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5872 if (constructor_range_stack
)
5874 /* If value is a compound literal and we'll be just using its
5875 content, don't put it into a SAVE_EXPR. */
5876 if (TREE_CODE (value
) != COMPOUND_LITERAL_EXPR
5877 || !require_constant_value
5879 value
= save_expr (value
);
5884 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5887 enum tree_code fieldcode
;
5889 if (constructor_fields
== 0)
5891 pedwarn_init ("excess elements in struct initializer");
5895 fieldtype
= TREE_TYPE (constructor_fields
);
5896 if (fieldtype
!= error_mark_node
)
5897 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
5898 fieldcode
= TREE_CODE (fieldtype
);
5900 /* Error for non-static initialization of a flexible array member. */
5901 if (fieldcode
== ARRAY_TYPE
5902 && !require_constant_value
5903 && TYPE_SIZE (fieldtype
) == NULL_TREE
5904 && TREE_CHAIN (constructor_fields
) == NULL_TREE
)
5906 error_init ("non-static initialization of a flexible array member");
5910 /* Accept a string constant to initialize a subarray. */
5912 && fieldcode
== ARRAY_TYPE
5913 && TREE_CODE (TREE_TYPE (fieldtype
)) == INTEGER_TYPE
5916 /* Otherwise, if we have come to a subaggregate,
5917 and we don't have an element of its type, push into it. */
5918 else if (value
!= 0 && !constructor_no_implicit
5919 && value
!= error_mark_node
5920 && TYPE_MAIN_VARIANT (TREE_TYPE (value
)) != fieldtype
5921 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
5922 || fieldcode
== UNION_TYPE
))
5924 push_init_level (1);
5930 push_member_name (constructor_fields
);
5931 output_init_element (value
, fieldtype
, constructor_fields
, 1);
5932 RESTORE_SPELLING_DEPTH (constructor_depth
);
5935 /* Do the bookkeeping for an element that was
5936 directly output as a constructor. */
5938 /* For a record, keep track of end position of last field. */
5939 if (DECL_SIZE (constructor_fields
))
5940 constructor_bit_index
5941 = size_binop (PLUS_EXPR
,
5942 bit_position (constructor_fields
),
5943 DECL_SIZE (constructor_fields
));
5945 /* If the current field was the first one not yet written out,
5946 it isn't now, so update. */
5947 if (constructor_unfilled_fields
== constructor_fields
)
5949 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
5950 /* Skip any nameless bit fields. */
5951 while (constructor_unfilled_fields
!= 0
5952 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5953 && DECL_NAME (constructor_unfilled_fields
) == 0)
5954 constructor_unfilled_fields
=
5955 TREE_CHAIN (constructor_unfilled_fields
);
5959 constructor_fields
= TREE_CHAIN (constructor_fields
);
5960 /* Skip any nameless bit fields at the beginning. */
5961 while (constructor_fields
!= 0
5962 && DECL_C_BIT_FIELD (constructor_fields
)
5963 && DECL_NAME (constructor_fields
) == 0)
5964 constructor_fields
= TREE_CHAIN (constructor_fields
);
5966 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5969 enum tree_code fieldcode
;
5971 if (constructor_fields
== 0)
5973 pedwarn_init ("excess elements in union initializer");
5977 fieldtype
= TREE_TYPE (constructor_fields
);
5978 if (fieldtype
!= error_mark_node
)
5979 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
5980 fieldcode
= TREE_CODE (fieldtype
);
5982 /* Warn that traditional C rejects initialization of unions.
5983 We skip the warning if the value is zero. This is done
5984 under the assumption that the zero initializer in user
5985 code appears conditioned on e.g. __STDC__ to avoid
5986 "missing initializer" warnings and relies on default
5987 initialization to zero in the traditional C case.
5988 We also skip the warning if the initializer is designated,
5989 again on the assumption that this must be conditional on
5990 __STDC__ anyway (and we've already complained about the
5991 member-designator already). */
5992 if (warn_traditional
&& !in_system_header
&& !constructor_designated
5993 && !(value
&& (integer_zerop (value
) || real_zerop (value
))))
5994 warning ("traditional C rejects initialization of unions");
5996 /* Accept a string constant to initialize a subarray. */
5998 && fieldcode
== ARRAY_TYPE
5999 && TREE_CODE (TREE_TYPE (fieldtype
)) == INTEGER_TYPE
6002 /* Otherwise, if we have come to a subaggregate,
6003 and we don't have an element of its type, push into it. */
6004 else if (value
!= 0 && !constructor_no_implicit
6005 && value
!= error_mark_node
6006 && TYPE_MAIN_VARIANT (TREE_TYPE (value
)) != fieldtype
6007 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
6008 || fieldcode
== UNION_TYPE
))
6010 push_init_level (1);
6016 push_member_name (constructor_fields
);
6017 output_init_element (value
, fieldtype
, constructor_fields
, 1);
6018 RESTORE_SPELLING_DEPTH (constructor_depth
);
6021 /* Do the bookkeeping for an element that was
6022 directly output as a constructor. */
6024 constructor_bit_index
= DECL_SIZE (constructor_fields
);
6025 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
6028 constructor_fields
= 0;
6030 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6032 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6033 enum tree_code eltcode
= TREE_CODE (elttype
);
6035 /* Accept a string constant to initialize a subarray. */
6037 && eltcode
== ARRAY_TYPE
6038 && TREE_CODE (TREE_TYPE (elttype
)) == INTEGER_TYPE
6041 /* Otherwise, if we have come to a subaggregate,
6042 and we don't have an element of its type, push into it. */
6043 else if (value
!= 0 && !constructor_no_implicit
6044 && value
!= error_mark_node
6045 && TYPE_MAIN_VARIANT (TREE_TYPE (value
)) != elttype
6046 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
6047 || eltcode
== UNION_TYPE
))
6049 push_init_level (1);
6053 if (constructor_max_index
!= 0
6054 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
6055 || integer_all_onesp (constructor_max_index
)))
6057 pedwarn_init ("excess elements in array initializer");
6061 /* Now output the actual element. */
6064 push_array_bounds (tree_low_cst (constructor_index
, 0));
6065 output_init_element (value
, elttype
, constructor_index
, 1);
6066 RESTORE_SPELLING_DEPTH (constructor_depth
);
6070 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
6073 /* If we are doing the bookkeeping for an element that was
6074 directly output as a constructor, we must update
6075 constructor_unfilled_index. */
6076 constructor_unfilled_index
= constructor_index
;
6078 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6080 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6082 /* Do a basic check of initializer size. Note that vectors
6083 always have a fixed size derived from their type. */
6084 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
6086 pedwarn_init ("excess elements in vector initializer");
6090 /* Now output the actual element. */
6092 output_init_element (value
, elttype
, constructor_index
, 1);
6095 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
6098 /* If we are doing the bookkeeping for an element that was
6099 directly output as a constructor, we must update
6100 constructor_unfilled_index. */
6101 constructor_unfilled_index
= constructor_index
;
6104 /* Handle the sole element allowed in a braced initializer
6105 for a scalar variable. */
6106 else if (constructor_fields
== 0)
6108 pedwarn_init ("excess elements in scalar initializer");
6114 output_init_element (value
, constructor_type
, NULL_TREE
, 1);
6115 constructor_fields
= 0;
6118 /* Handle range initializers either at this level or anywhere higher
6119 in the designator stack. */
6120 if (constructor_range_stack
)
6122 struct constructor_range_stack
*p
, *range_stack
;
6125 range_stack
= constructor_range_stack
;
6126 constructor_range_stack
= 0;
6127 while (constructor_stack
!= range_stack
->stack
)
6129 if (!constructor_stack
->implicit
)
6131 process_init_element (pop_init_level (1));
6133 for (p
= range_stack
;
6134 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
6137 if (!constructor_stack
->implicit
)
6139 process_init_element (pop_init_level (1));
6142 p
->index
= size_binop (PLUS_EXPR
, p
->index
, bitsize_one_node
);
6143 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
6148 constructor_index
= p
->index
;
6149 constructor_fields
= p
->fields
;
6150 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
6158 push_init_level (2);
6159 p
->stack
= constructor_stack
;
6160 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
6161 p
->index
= p
->range_start
;
6165 constructor_range_stack
= range_stack
;
6172 constructor_range_stack
= 0;
6175 /* Build a simple asm-statement, from one string literal. */
6177 simple_asm_stmt (tree expr
)
6181 if (TREE_CODE (expr
) == ADDR_EXPR
)
6182 expr
= TREE_OPERAND (expr
, 0);
6184 if (TREE_CODE (expr
) == STRING_CST
)
6188 /* Simple asm statements are treated as volatile. */
6189 stmt
= add_stmt (build_stmt (ASM_STMT
, ridpointers
[(int) RID_VOLATILE
],
6190 expr
, NULL_TREE
, NULL_TREE
, NULL_TREE
));
6191 ASM_INPUT_P (stmt
) = 1;
6195 error ("argument of `asm' is not a constant string");
6199 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6200 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6203 build_asm_stmt (tree cv_qualifier
, tree string
, tree outputs
, tree inputs
,
6208 if (TREE_CODE (string
) != STRING_CST
)
6210 error ("asm template is not a string constant");
6214 if (cv_qualifier
!= NULL_TREE
6215 && cv_qualifier
!= ridpointers
[(int) RID_VOLATILE
])
6217 warning ("%s qualifier ignored on asm",
6218 IDENTIFIER_POINTER (cv_qualifier
));
6219 cv_qualifier
= NULL_TREE
;
6222 /* We can remove output conversions that change the type,
6223 but not the mode. */
6224 for (tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
))
6226 tree output
= TREE_VALUE (tail
);
6228 STRIP_NOPS (output
);
6229 TREE_VALUE (tail
) = output
;
6231 /* Allow conversions as LHS here. build_modify_expr as called below
6232 will do the right thing with them. */
6233 while (TREE_CODE (output
) == NOP_EXPR
6234 || TREE_CODE (output
) == CONVERT_EXPR
6235 || TREE_CODE (output
) == FLOAT_EXPR
6236 || TREE_CODE (output
) == FIX_TRUNC_EXPR
6237 || TREE_CODE (output
) == FIX_FLOOR_EXPR
6238 || TREE_CODE (output
) == FIX_ROUND_EXPR
6239 || TREE_CODE (output
) == FIX_CEIL_EXPR
)
6240 output
= TREE_OPERAND (output
, 0);
6242 lvalue_or_else (TREE_VALUE (tail
), "invalid lvalue in asm statement");
6245 /* Remove output conversions that change the type but not the mode. */
6246 for (tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
))
6248 tree output
= TREE_VALUE (tail
);
6249 STRIP_NOPS (output
);
6250 TREE_VALUE (tail
) = output
;
6253 /* Perform default conversions on array and function inputs.
6254 Don't do this for other types as it would screw up operands
6255 expected to be in memory. */
6256 for (tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
))
6257 TREE_VALUE (tail
) = default_function_array_conversion (TREE_VALUE (tail
));
6259 return add_stmt (build_stmt (ASM_STMT
, cv_qualifier
, string
,
6260 outputs
, inputs
, clobbers
));
6263 /* Expand an ASM statement with operands, handling output operands
6264 that are not variables or INDIRECT_REFS by transforming such
6265 cases into cases that expand_asm_operands can handle.
6267 Arguments are same as for expand_asm_operands. */
6270 c_expand_asm_operands (tree string
, tree outputs
, tree inputs
,
6271 tree clobbers
, int vol
, location_t locus
)
6273 int noutputs
= list_length (outputs
);
6275 /* o[I] is the place that output number I should be written. */
6276 tree
*o
= alloca (noutputs
* sizeof (tree
));
6279 /* Record the contents of OUTPUTS before it is modified. */
6280 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6282 o
[i
] = TREE_VALUE (tail
);
6283 if (o
[i
] == error_mark_node
)
6287 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6288 OUTPUTS some trees for where the values were actually stored. */
6289 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, locus
);
6291 /* Copy all the intermediate outputs into the specified outputs. */
6292 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6294 if (o
[i
] != TREE_VALUE (tail
))
6296 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6297 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
6300 /* Restore the original value so that it's correct the next
6301 time we expand this function. */
6302 TREE_VALUE (tail
) = o
[i
];
6304 /* Detect modification of read-only values.
6305 (Otherwise done by build_modify_expr.) */
6308 tree type
= TREE_TYPE (o
[i
]);
6309 if (TREE_READONLY (o
[i
])
6310 || TYPE_READONLY (type
)
6311 || ((TREE_CODE (type
) == RECORD_TYPE
6312 || TREE_CODE (type
) == UNION_TYPE
)
6313 && C_TYPE_FIELDS_READONLY (type
)))
6314 readonly_warning (o
[i
], "modification by `asm'");
6318 /* Those MODIFY_EXPRs could do autoincrements. */
6322 /* Expand a C `return' statement.
6323 RETVAL is the expression for what to return,
6324 or a null pointer for `return;' with no value. */
6327 c_expand_return (tree retval
)
6329 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6331 if (TREE_THIS_VOLATILE (current_function_decl
))
6332 warning ("function declared `noreturn' has a `return' statement");
6336 current_function_returns_null
= 1;
6337 if ((warn_return_type
|| flag_isoc99
)
6338 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
6339 pedwarn_c99 ("`return' with no value, in function returning non-void");
6341 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
6343 current_function_returns_null
= 1;
6344 if (pedantic
|| TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
6345 pedwarn ("`return' with a value, in function returning void");
6349 tree t
= convert_for_assignment (valtype
, retval
, _("return"),
6350 NULL_TREE
, NULL_TREE
, 0);
6351 tree res
= DECL_RESULT (current_function_decl
);
6354 current_function_returns_value
= 1;
6355 if (t
== error_mark_node
)
6358 inner
= t
= convert (TREE_TYPE (res
), t
);
6360 /* Strip any conversions, additions, and subtractions, and see if
6361 we are returning the address of a local variable. Warn if so. */
6364 switch (TREE_CODE (inner
))
6366 case NOP_EXPR
: case NON_LVALUE_EXPR
: case CONVERT_EXPR
:
6368 inner
= TREE_OPERAND (inner
, 0);
6372 /* If the second operand of the MINUS_EXPR has a pointer
6373 type (or is converted from it), this may be valid, so
6374 don't give a warning. */
6376 tree op1
= TREE_OPERAND (inner
, 1);
6378 while (! POINTER_TYPE_P (TREE_TYPE (op1
))
6379 && (TREE_CODE (op1
) == NOP_EXPR
6380 || TREE_CODE (op1
) == NON_LVALUE_EXPR
6381 || TREE_CODE (op1
) == CONVERT_EXPR
))
6382 op1
= TREE_OPERAND (op1
, 0);
6384 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
6387 inner
= TREE_OPERAND (inner
, 0);
6392 inner
= TREE_OPERAND (inner
, 0);
6394 while (TREE_CODE_CLASS (TREE_CODE (inner
)) == 'r')
6395 inner
= TREE_OPERAND (inner
, 0);
6397 if (TREE_CODE (inner
) == VAR_DECL
6398 && ! DECL_EXTERNAL (inner
)
6399 && ! TREE_STATIC (inner
)
6400 && DECL_CONTEXT (inner
) == current_function_decl
)
6401 warning ("function returns address of local variable");
6411 retval
= build (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
6414 return add_stmt (build_return_stmt (retval
));
6418 /* The SWITCH_STMT being built. */
6420 /* A splay-tree mapping the low element of a case range to the high
6421 element, or NULL_TREE if there is no high element. Used to
6422 determine whether or not a new case label duplicates an old case
6423 label. We need a tree, rather than simply a hash table, because
6424 of the GNU case range extension. */
6426 /* The next node on the stack. */
6427 struct c_switch
*next
;
6430 /* A stack of the currently active switch statements. The innermost
6431 switch statement is on the top of the stack. There is no need to
6432 mark the stack for garbage collection because it is only active
6433 during the processing of the body of a function, and we never
6434 collect at that point. */
6436 static struct c_switch
*switch_stack
;
6438 /* Start a C switch statement, testing expression EXP. Return the new
6442 c_start_case (tree exp
)
6444 enum tree_code code
;
6445 tree type
, orig_type
= error_mark_node
;
6446 struct c_switch
*cs
;
6448 if (exp
!= error_mark_node
)
6450 code
= TREE_CODE (TREE_TYPE (exp
));
6451 orig_type
= TREE_TYPE (exp
);
6453 if (! INTEGRAL_TYPE_P (orig_type
)
6454 && code
!= ERROR_MARK
)
6456 error ("switch quantity not an integer");
6457 exp
= integer_zero_node
;
6461 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
6463 if (warn_traditional
&& !in_system_header
6464 && (type
== long_integer_type_node
6465 || type
== long_unsigned_type_node
))
6466 warning ("`long' switch expression not converted to `int' in ISO C");
6468 exp
= default_conversion (exp
);
6469 type
= TREE_TYPE (exp
);
6473 /* Add this new SWITCH_STMT to the stack. */
6474 cs
= xmalloc (sizeof (*cs
));
6475 cs
->switch_stmt
= build_stmt (SWITCH_STMT
, exp
, NULL_TREE
, orig_type
);
6476 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
6477 cs
->next
= switch_stack
;
6480 return add_stmt (switch_stack
->switch_stmt
);
6483 /* Process a case label. */
6486 do_case (tree low_value
, tree high_value
)
6488 tree label
= NULL_TREE
;
6492 bool switch_was_empty_p
= (SWITCH_BODY (switch_stack
->switch_stmt
) == NULL_TREE
);
6494 label
= c_add_case_label (switch_stack
->cases
,
6495 SWITCH_COND (switch_stack
->switch_stmt
),
6496 low_value
, high_value
);
6497 if (label
== error_mark_node
)
6499 else if (switch_was_empty_p
)
6501 /* Attach the first case label to the SWITCH_BODY. */
6502 SWITCH_BODY (switch_stack
->switch_stmt
) = TREE_CHAIN (switch_stack
->switch_stmt
);
6503 TREE_CHAIN (switch_stack
->switch_stmt
) = NULL_TREE
;
6507 error ("case label not within a switch statement");
6509 error ("`default' label not within a switch statement");
6514 /* Finish the switch statement. */
6517 c_finish_case (void)
6519 struct c_switch
*cs
= switch_stack
;
6521 /* Rechain the next statements to the SWITCH_STMT. */
6522 last_tree
= cs
->switch_stmt
;
6524 /* Pop the stack. */
6525 switch_stack
= switch_stack
->next
;
6526 splay_tree_delete (cs
->cases
);
6530 /* Build a binary-operation expression without default conversions.
6531 CODE is the kind of expression to build.
6532 This function differs from `build' in several ways:
6533 the data type of the result is computed and recorded in it,
6534 warnings are generated if arg data types are invalid,
6535 special handling for addition and subtraction of pointers is known,
6536 and some optimization is done (operations on narrow ints
6537 are done in the narrower type when that gives the same result).
6538 Constant folding is also done before the result is returned.
6540 Note that the operands will never have enumeral types, or function
6541 or array types, because either they will have the default conversions
6542 performed or they have both just been converted to some other type in which
6543 the arithmetic is to be done. */
6546 build_binary_op (enum tree_code code
, tree orig_op0
, tree orig_op1
,
6550 enum tree_code code0
, code1
;
6553 /* Expression code to give to the expression when it is built.
6554 Normally this is CODE, which is what the caller asked for,
6555 but in some special cases we change it. */
6556 enum tree_code resultcode
= code
;
6558 /* Data type in which the computation is to be performed.
6559 In the simplest cases this is the common type of the arguments. */
6560 tree result_type
= NULL
;
6562 /* Nonzero means operands have already been type-converted
6563 in whatever way is necessary.
6564 Zero means they need to be converted to RESULT_TYPE. */
6567 /* Nonzero means create the expression with this type, rather than
6569 tree build_type
= 0;
6571 /* Nonzero means after finally constructing the expression
6572 convert it to this type. */
6573 tree final_type
= 0;
6575 /* Nonzero if this is an operation like MIN or MAX which can
6576 safely be computed in short if both args are promoted shorts.
6577 Also implies COMMON.
6578 -1 indicates a bitwise operation; this makes a difference
6579 in the exact conditions for when it is safe to do the operation
6580 in a narrower mode. */
6583 /* Nonzero if this is a comparison operation;
6584 if both args are promoted shorts, compare the original shorts.
6585 Also implies COMMON. */
6586 int short_compare
= 0;
6588 /* Nonzero if this is a right-shift operation, which can be computed on the
6589 original short and then promoted if the operand is a promoted short. */
6590 int short_shift
= 0;
6592 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6597 op0
= default_conversion (orig_op0
);
6598 op1
= default_conversion (orig_op1
);
6606 type0
= TREE_TYPE (op0
);
6607 type1
= TREE_TYPE (op1
);
6609 /* The expression codes of the data types of the arguments tell us
6610 whether the arguments are integers, floating, pointers, etc. */
6611 code0
= TREE_CODE (type0
);
6612 code1
= TREE_CODE (type1
);
6614 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6615 STRIP_TYPE_NOPS (op0
);
6616 STRIP_TYPE_NOPS (op1
);
6618 /* If an error was already reported for one of the arguments,
6619 avoid reporting another error. */
6621 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
6622 return error_mark_node
;
6627 /* Handle the pointer + int case. */
6628 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6629 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
6630 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
6631 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
6637 /* Subtraction of two similar pointers.
6638 We must subtract them as integers, then divide by object size. */
6639 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
6640 && comp_target_types (type0
, type1
, 1))
6641 return pointer_diff (op0
, op1
);
6642 /* Handle pointer minus int. Just like pointer plus int. */
6643 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6644 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
6653 case TRUNC_DIV_EXPR
:
6655 case FLOOR_DIV_EXPR
:
6656 case ROUND_DIV_EXPR
:
6657 case EXACT_DIV_EXPR
:
6658 /* Floating point division by zero is a legitimate way to obtain
6659 infinities and NaNs. */
6660 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
6661 warning ("division by zero");
6663 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
6664 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
6665 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6666 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
6668 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
6669 resultcode
= RDIV_EXPR
;
6671 /* Although it would be tempting to shorten always here, that
6672 loses on some targets, since the modulo instruction is
6673 undefined if the quotient can't be represented in the
6674 computation mode. We shorten only if unsigned or if
6675 dividing by something we know != -1. */
6676 shorten
= (TREE_UNSIGNED (TREE_TYPE (orig_op0
))
6677 || (TREE_CODE (op1
) == INTEGER_CST
6678 && ! integer_all_onesp (op1
)));
6686 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6688 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
6692 case TRUNC_MOD_EXPR
:
6693 case FLOOR_MOD_EXPR
:
6694 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
6695 warning ("division by zero");
6697 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6699 /* Although it would be tempting to shorten always here, that loses
6700 on some targets, since the modulo instruction is undefined if the
6701 quotient can't be represented in the computation mode. We shorten
6702 only if unsigned or if dividing by something we know != -1. */
6703 shorten
= (TREE_UNSIGNED (TREE_TYPE (orig_op0
))
6704 || (TREE_CODE (op1
) == INTEGER_CST
6705 && ! integer_all_onesp (op1
)));
6710 case TRUTH_ANDIF_EXPR
:
6711 case TRUTH_ORIF_EXPR
:
6712 case TRUTH_AND_EXPR
:
6714 case TRUTH_XOR_EXPR
:
6715 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
6716 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
6717 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
6718 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
6720 /* Result of these operations is always an int,
6721 but that does not mean the operands should be
6722 converted to ints! */
6723 result_type
= integer_type_node
;
6724 op0
= c_common_truthvalue_conversion (op0
);
6725 op1
= c_common_truthvalue_conversion (op1
);
6730 /* Shift operations: result has same type as first operand;
6731 always convert second operand to int.
6732 Also set SHORT_SHIFT if shifting rightward. */
6735 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6737 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
6739 if (tree_int_cst_sgn (op1
) < 0)
6740 warning ("right shift count is negative");
6743 if (! integer_zerop (op1
))
6746 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
6747 warning ("right shift count >= width of type");
6751 /* Use the type of the value to be shifted. */
6752 result_type
= type0
;
6753 /* Convert the shift-count to an integer, regardless of size
6754 of value being shifted. */
6755 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
6756 op1
= convert (integer_type_node
, op1
);
6757 /* Avoid converting op1 to result_type later. */
6763 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6765 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
6767 if (tree_int_cst_sgn (op1
) < 0)
6768 warning ("left shift count is negative");
6770 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
6771 warning ("left shift count >= width of type");
6774 /* Use the type of the value to be shifted. */
6775 result_type
= type0
;
6776 /* Convert the shift-count to an integer, regardless of size
6777 of value being shifted. */
6778 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
6779 op1
= convert (integer_type_node
, op1
);
6780 /* Avoid converting op1 to result_type later. */
6787 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6789 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
6791 if (tree_int_cst_sgn (op1
) < 0)
6792 warning ("shift count is negative");
6793 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
6794 warning ("shift count >= width of type");
6797 /* Use the type of the value to be shifted. */
6798 result_type
= type0
;
6799 /* Convert the shift-count to an integer, regardless of size
6800 of value being shifted. */
6801 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
6802 op1
= convert (integer_type_node
, op1
);
6803 /* Avoid converting op1 to result_type later. */
6810 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
6811 warning ("comparing floating point with == or != is unsafe");
6812 /* Result of comparison is always int,
6813 but don't convert the args to int! */
6814 build_type
= integer_type_node
;
6815 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
6816 || code0
== COMPLEX_TYPE
6817 || code0
== VECTOR_TYPE
)
6818 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6819 || code1
== COMPLEX_TYPE
6820 || code1
== VECTOR_TYPE
))
6822 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6824 tree tt0
= TREE_TYPE (type0
);
6825 tree tt1
= TREE_TYPE (type1
);
6826 /* Anything compares with void *. void * compares with anything.
6827 Otherwise, the targets must be compatible
6828 and both must be object or both incomplete. */
6829 if (comp_target_types (type0
, type1
, 1))
6830 result_type
= common_type (type0
, type1
);
6831 else if (VOID_TYPE_P (tt0
))
6833 /* op0 != orig_op0 detects the case of something
6834 whose value is 0 but which isn't a valid null ptr const. */
6835 if (pedantic
&& (!integer_zerop (op0
) || op0
!= orig_op0
)
6836 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
6837 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6839 else if (VOID_TYPE_P (tt1
))
6841 if (pedantic
&& (!integer_zerop (op1
) || op1
!= orig_op1
)
6842 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
6843 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6846 pedwarn ("comparison of distinct pointer types lacks a cast");
6848 if (result_type
== NULL_TREE
)
6849 result_type
= ptr_type_node
;
6851 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
6852 && integer_zerop (op1
))
6853 result_type
= type0
;
6854 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
6855 && integer_zerop (op0
))
6856 result_type
= type1
;
6857 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6859 result_type
= type0
;
6860 pedwarn ("comparison between pointer and integer");
6862 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
6864 result_type
= type1
;
6865 pedwarn ("comparison between pointer and integer");
6871 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
6872 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
6874 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6876 if (comp_target_types (type0
, type1
, 1))
6878 result_type
= common_type (type0
, type1
);
6880 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
6881 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6885 result_type
= ptr_type_node
;
6886 pedwarn ("comparison of distinct pointer types lacks a cast");
6895 build_type
= integer_type_node
;
6896 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
6897 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
6899 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6901 if (comp_target_types (type0
, type1
, 1))
6903 result_type
= common_type (type0
, type1
);
6904 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
6905 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
6906 pedwarn ("comparison of complete and incomplete pointers");
6908 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
6909 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6913 result_type
= ptr_type_node
;
6914 pedwarn ("comparison of distinct pointer types lacks a cast");
6917 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
6918 && integer_zerop (op1
))
6920 result_type
= type0
;
6921 if (pedantic
|| extra_warnings
)
6922 pedwarn ("ordered comparison of pointer with integer zero");
6924 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
6925 && integer_zerop (op0
))
6927 result_type
= type1
;
6929 pedwarn ("ordered comparison of pointer with integer zero");
6931 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6933 result_type
= type0
;
6934 pedwarn ("comparison between pointer and integer");
6936 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
6938 result_type
= type1
;
6939 pedwarn ("comparison between pointer and integer");
6943 case UNORDERED_EXPR
:
6950 build_type
= integer_type_node
;
6951 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
6953 error ("unordered comparison on non-floating point argument");
6954 return error_mark_node
;
6963 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
6964 || code0
== VECTOR_TYPE
)
6966 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
6967 || code1
== VECTOR_TYPE
))
6969 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
6971 if (shorten
|| common
|| short_compare
)
6972 result_type
= common_type (type0
, type1
);
6974 /* For certain operations (which identify themselves by shorten != 0)
6975 if both args were extended from the same smaller type,
6976 do the arithmetic in that type and then extend.
6978 shorten !=0 and !=1 indicates a bitwise operation.
6979 For them, this optimization is safe only if
6980 both args are zero-extended or both are sign-extended.
6981 Otherwise, we might change the result.
6982 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6983 but calculated in (unsigned short) it would be (unsigned short)-1. */
6985 if (shorten
&& none_complex
)
6987 int unsigned0
, unsigned1
;
6988 tree arg0
= get_narrower (op0
, &unsigned0
);
6989 tree arg1
= get_narrower (op1
, &unsigned1
);
6990 /* UNS is 1 if the operation to be done is an unsigned one. */
6991 int uns
= TREE_UNSIGNED (result_type
);
6994 final_type
= result_type
;
6996 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6997 but it *requires* conversion to FINAL_TYPE. */
6999 if ((TYPE_PRECISION (TREE_TYPE (op0
))
7000 == TYPE_PRECISION (TREE_TYPE (arg0
)))
7001 && TREE_TYPE (op0
) != final_type
)
7002 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
7003 if ((TYPE_PRECISION (TREE_TYPE (op1
))
7004 == TYPE_PRECISION (TREE_TYPE (arg1
)))
7005 && TREE_TYPE (op1
) != final_type
)
7006 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
7008 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7010 /* For bitwise operations, signedness of nominal type
7011 does not matter. Consider only how operands were extended. */
7015 /* Note that in all three cases below we refrain from optimizing
7016 an unsigned operation on sign-extended args.
7017 That would not be valid. */
7019 /* Both args variable: if both extended in same way
7020 from same width, do it in that width.
7021 Do it unsigned if args were zero-extended. */
7022 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
7023 < TYPE_PRECISION (result_type
))
7024 && (TYPE_PRECISION (TREE_TYPE (arg1
))
7025 == TYPE_PRECISION (TREE_TYPE (arg0
)))
7026 && unsigned0
== unsigned1
7027 && (unsigned0
|| !uns
))
7029 = c_common_signed_or_unsigned_type
7030 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
7031 else if (TREE_CODE (arg0
) == INTEGER_CST
7032 && (unsigned1
|| !uns
)
7033 && (TYPE_PRECISION (TREE_TYPE (arg1
))
7034 < TYPE_PRECISION (result_type
))
7036 = c_common_signed_or_unsigned_type (unsigned1
,
7038 int_fits_type_p (arg0
, type
)))
7040 else if (TREE_CODE (arg1
) == INTEGER_CST
7041 && (unsigned0
|| !uns
)
7042 && (TYPE_PRECISION (TREE_TYPE (arg0
))
7043 < TYPE_PRECISION (result_type
))
7045 = c_common_signed_or_unsigned_type (unsigned0
,
7047 int_fits_type_p (arg1
, type
)))
7051 /* Shifts can be shortened if shifting right. */
7056 tree arg0
= get_narrower (op0
, &unsigned_arg
);
7058 final_type
= result_type
;
7060 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
7061 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
7063 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
7064 /* We can shorten only if the shift count is less than the
7065 number of bits in the smaller type size. */
7066 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
7067 /* We cannot drop an unsigned shift after sign-extension. */
7068 && (!TREE_UNSIGNED (final_type
) || unsigned_arg
))
7070 /* Do an unsigned shift if the operand was zero-extended. */
7072 = c_common_signed_or_unsigned_type (unsigned_arg
,
7074 /* Convert value-to-be-shifted to that type. */
7075 if (TREE_TYPE (op0
) != result_type
)
7076 op0
= convert (result_type
, op0
);
7081 /* Comparison operations are shortened too but differently.
7082 They identify themselves by setting short_compare = 1. */
7086 /* Don't write &op0, etc., because that would prevent op0
7087 from being kept in a register.
7088 Instead, make copies of the our local variables and
7089 pass the copies by reference, then copy them back afterward. */
7090 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
7091 enum tree_code xresultcode
= resultcode
;
7093 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
7098 op0
= xop0
, op1
= xop1
;
7100 resultcode
= xresultcode
;
7102 if (warn_sign_compare
&& skip_evaluation
== 0)
7104 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
7105 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
7106 int unsignedp0
, unsignedp1
;
7107 tree primop0
= get_narrower (op0
, &unsignedp0
);
7108 tree primop1
= get_narrower (op1
, &unsignedp1
);
7112 STRIP_TYPE_NOPS (xop0
);
7113 STRIP_TYPE_NOPS (xop1
);
7115 /* Give warnings for comparisons between signed and unsigned
7116 quantities that may fail.
7118 Do the checking based on the original operand trees, so that
7119 casts will be considered, but default promotions won't be.
7121 Do not warn if the comparison is being done in a signed type,
7122 since the signed type will only be chosen if it can represent
7123 all the values of the unsigned type. */
7124 if (! TREE_UNSIGNED (result_type
))
7126 /* Do not warn if both operands are the same signedness. */
7127 else if (op0_signed
== op1_signed
)
7134 sop
= xop0
, uop
= xop1
;
7136 sop
= xop1
, uop
= xop0
;
7138 /* Do not warn if the signed quantity is an
7139 unsuffixed integer literal (or some static
7140 constant expression involving such literals or a
7141 conditional expression involving such literals)
7142 and it is non-negative. */
7143 if (c_tree_expr_nonnegative_p (sop
))
7145 /* Do not warn if the comparison is an equality operation,
7146 the unsigned quantity is an integral constant, and it
7147 would fit in the result if the result were signed. */
7148 else if (TREE_CODE (uop
) == INTEGER_CST
7149 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
7151 (uop
, c_common_signed_type (result_type
)))
7153 /* Do not warn if the unsigned quantity is an enumeration
7154 constant and its maximum value would fit in the result
7155 if the result were signed. */
7156 else if (TREE_CODE (uop
) == INTEGER_CST
7157 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
7159 (TYPE_MAX_VALUE (TREE_TYPE(uop
)),
7160 c_common_signed_type (result_type
)))
7163 warning ("comparison between signed and unsigned");
7166 /* Warn if two unsigned values are being compared in a size
7167 larger than their original size, and one (and only one) is the
7168 result of a `~' operator. This comparison will always fail.
7170 Also warn if one operand is a constant, and the constant
7171 does not have all bits set that are set in the ~ operand
7172 when it is extended. */
7174 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7175 != (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
7177 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7178 primop0
= get_narrower (TREE_OPERAND (primop0
, 0),
7181 primop1
= get_narrower (TREE_OPERAND (primop1
, 0),
7184 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
7187 HOST_WIDE_INT constant
, mask
;
7188 int unsignedp
, bits
;
7190 if (host_integerp (primop0
, 0))
7193 unsignedp
= unsignedp1
;
7194 constant
= tree_low_cst (primop0
, 0);
7199 unsignedp
= unsignedp0
;
7200 constant
= tree_low_cst (primop1
, 0);
7203 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
7204 if (bits
< TYPE_PRECISION (result_type
)
7205 && bits
< HOST_BITS_PER_WIDE_INT
&& unsignedp
)
7207 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
7208 if ((mask
& constant
) != mask
)
7209 warning ("comparison of promoted ~unsigned with constant");
7212 else if (unsignedp0
&& unsignedp1
7213 && (TYPE_PRECISION (TREE_TYPE (primop0
))
7214 < TYPE_PRECISION (result_type
))
7215 && (TYPE_PRECISION (TREE_TYPE (primop1
))
7216 < TYPE_PRECISION (result_type
)))
7217 warning ("comparison of promoted ~unsigned with unsigned");
7223 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7224 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7225 Then the expression will be built.
7226 It will be given type FINAL_TYPE if that is nonzero;
7227 otherwise, it will be given type RESULT_TYPE. */
7231 binary_op_error (code
);
7232 return error_mark_node
;
7237 if (TREE_TYPE (op0
) != result_type
)
7238 op0
= convert (result_type
, op0
);
7239 if (TREE_TYPE (op1
) != result_type
)
7240 op1
= convert (result_type
, op1
);
7243 if (build_type
== NULL_TREE
)
7244 build_type
= result_type
;
7247 tree result
= build (resultcode
, build_type
, op0
, op1
);
7250 /* Treat expressions in initializers specially as they can't trap. */
7251 folded
= initializer_stack
? fold_initializer (result
)
7253 if (folded
== result
)
7254 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
7255 if (final_type
!= 0)
7256 return convert (final_type
, folded
);