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. */
30 #include "coretypes.h"
34 #include "langhooks.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
47 /* The level of nesting inside "__alignof__". */
50 /* The level of nesting inside "sizeof". */
53 /* The level of nesting inside "typeof". */
56 /* Nonzero if we've already printed a "missing braces around initializer"
57 message within this initializer. */
58 static int missing_braces_mentioned
;
60 static int require_constant_value
;
61 static int require_constant_elements
;
63 static tree
qualify_type (tree
, tree
);
64 static int tagged_types_tu_compatible_p (tree
, tree
);
65 static int comp_target_types (tree
, tree
, int);
66 static int function_types_compatible_p (tree
, tree
);
67 static int type_lists_compatible_p (tree
, tree
);
68 static tree
decl_constant_value_for_broken_optimization (tree
);
69 static tree
default_function_array_conversion (tree
);
70 static tree
lookup_field (tree
, tree
);
71 static tree
convert_arguments (tree
, tree
, tree
, tree
);
72 static tree
pointer_diff (tree
, tree
);
73 static tree
convert_for_assignment (tree
, tree
, const char *, tree
, tree
,
75 static void warn_for_assignment (const char *, const char *, tree
, int);
76 static tree
valid_compound_expr_initializer (tree
, tree
);
77 static void push_string (const char *);
78 static void push_member_name (tree
);
79 static void push_array_bounds (int);
80 static int spelling_length (void);
81 static char *print_spelling (char *);
82 static void warning_init (const char *);
83 static tree
digest_init (tree
, tree
, bool, int);
84 static void output_init_element (tree
, bool, tree
, tree
, int);
85 static void output_pending_init_elements (int);
86 static int set_designator (int);
87 static void push_range_stack (tree
);
88 static void add_pending_init (tree
, tree
);
89 static void set_nonincremental_init (void);
90 static void set_nonincremental_init_from_string (tree
);
91 static tree
find_init_member (tree
);
92 static int lvalue_or_else (tree
, const char *);
94 /* Do `exp = require_complete_type (exp);' to make sure exp
95 does not have an incomplete type. (That includes void types.) */
98 require_complete_type (tree value
)
100 tree type
= TREE_TYPE (value
);
102 if (value
== error_mark_node
|| type
== error_mark_node
)
103 return error_mark_node
;
105 /* First, detect a valid value with a complete type. */
106 if (COMPLETE_TYPE_P (type
))
109 c_incomplete_type_error (value
, type
);
110 return error_mark_node
;
113 /* Print an error message for invalid use of an incomplete type.
114 VALUE is the expression that was used (or 0 if that isn't known)
115 and TYPE is the type that was invalid. */
118 c_incomplete_type_error (tree value
, tree type
)
120 const char *type_code_string
;
122 /* Avoid duplicate error message. */
123 if (TREE_CODE (type
) == ERROR_MARK
)
126 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
127 || TREE_CODE (value
) == PARM_DECL
))
128 error ("%qs has an incomplete type",
129 IDENTIFIER_POINTER (DECL_NAME (value
)));
133 /* We must print an error message. Be clever about what it says. */
135 switch (TREE_CODE (type
))
138 type_code_string
= "struct";
142 type_code_string
= "union";
146 type_code_string
= "enum";
150 error ("invalid use of void expression");
154 if (TYPE_DOMAIN (type
))
156 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
158 error ("invalid use of flexible array member");
161 type
= TREE_TYPE (type
);
164 error ("invalid use of array with unspecified bounds");
171 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
172 error ("invalid use of undefined type %<%s %s%>",
173 type_code_string
, IDENTIFIER_POINTER (TYPE_NAME (type
)));
175 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
176 error ("invalid use of incomplete typedef %qs",
177 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
))));
181 /* Given a type, apply default promotions wrt unnamed function
182 arguments and return the new type. */
185 c_type_promotes_to (tree type
)
187 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
188 return double_type_node
;
190 if (c_promoting_integer_type_p (type
))
192 /* Preserve unsignedness if not really getting any wider. */
193 if (TYPE_UNSIGNED (type
)
194 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
195 return unsigned_type_node
;
196 return integer_type_node
;
202 /* Return a variant of TYPE which has all the type qualifiers of LIKE
203 as well as those of TYPE. */
206 qualify_type (tree type
, tree like
)
208 return c_build_qualified_type (type
,
209 TYPE_QUALS (type
) | TYPE_QUALS (like
));
212 /* Return the composite type of two compatible types.
214 We assume that comptypes has already been done and returned
215 nonzero; if that isn't so, this may crash. In particular, we
216 assume that qualifiers match. */
219 composite_type (tree t1
, tree t2
)
221 enum tree_code code1
;
222 enum tree_code code2
;
225 /* Save time if the two types are the same. */
227 if (t1
== t2
) return t1
;
229 /* If one type is nonsense, use the other. */
230 if (t1
== error_mark_node
)
232 if (t2
== error_mark_node
)
235 code1
= TREE_CODE (t1
);
236 code2
= TREE_CODE (t2
);
238 /* Merge the attributes. */
239 attributes
= targetm
.merge_type_attributes (t1
, t2
);
241 /* If one is an enumerated type and the other is the compatible
242 integer type, the composite type might be either of the two
243 (DR#013 question 3). For consistency, use the enumerated type as
244 the composite type. */
246 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
248 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
251 gcc_assert (code1
== code2
);
256 /* For two pointers, do this recursively on the target type. */
258 tree pointed_to_1
= TREE_TYPE (t1
);
259 tree pointed_to_2
= TREE_TYPE (t2
);
260 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
261 t1
= build_pointer_type (target
);
262 t1
= build_type_attribute_variant (t1
, attributes
);
263 return qualify_type (t1
, t2
);
268 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
270 /* We should not have any type quals on arrays at all. */
271 gcc_assert (!TYPE_QUALS (t1
) && !TYPE_QUALS (t2
));
273 /* Save space: see if the result is identical to one of the args. */
274 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
275 return build_type_attribute_variant (t1
, attributes
);
276 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
277 return build_type_attribute_variant (t2
, attributes
);
279 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
280 return build_type_attribute_variant (t1
, attributes
);
281 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
282 return build_type_attribute_variant (t2
, attributes
);
284 /* Merge the element types, and have a size if either arg has one. */
285 t1
= build_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
286 return build_type_attribute_variant (t1
, attributes
);
290 /* Function types: prefer the one that specified arg types.
291 If both do, merge the arg types. Also merge the return types. */
293 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
294 tree p1
= TYPE_ARG_TYPES (t1
);
295 tree p2
= TYPE_ARG_TYPES (t2
);
300 /* Save space: see if the result is identical to one of the args. */
301 if (valtype
== TREE_TYPE (t1
) && ! TYPE_ARG_TYPES (t2
))
302 return build_type_attribute_variant (t1
, attributes
);
303 if (valtype
== TREE_TYPE (t2
) && ! TYPE_ARG_TYPES (t1
))
304 return build_type_attribute_variant (t2
, attributes
);
306 /* Simple way if one arg fails to specify argument types. */
307 if (TYPE_ARG_TYPES (t1
) == 0)
309 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
310 t1
= build_type_attribute_variant (t1
, attributes
);
311 return qualify_type (t1
, t2
);
313 if (TYPE_ARG_TYPES (t2
) == 0)
315 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
316 t1
= build_type_attribute_variant (t1
, attributes
);
317 return qualify_type (t1
, t2
);
320 /* If both args specify argument types, we must merge the two
321 lists, argument by argument. */
322 /* Tell global_bindings_p to return false so that variable_size
323 doesn't abort on VLAs in parameter types. */
324 c_override_global_bindings_to_false
= true;
326 len
= list_length (p1
);
329 for (i
= 0; i
< len
; i
++)
330 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
335 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
337 /* A null type means arg type is not specified.
338 Take whatever the other function type has. */
339 if (TREE_VALUE (p1
) == 0)
341 TREE_VALUE (n
) = TREE_VALUE (p2
);
344 if (TREE_VALUE (p2
) == 0)
346 TREE_VALUE (n
) = TREE_VALUE (p1
);
350 /* Given wait (union {union wait *u; int *i} *)
351 and wait (union wait *),
352 prefer union wait * as type of parm. */
353 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
354 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
357 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
358 memb
; memb
= TREE_CHAIN (memb
))
359 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p2
)))
361 TREE_VALUE (n
) = TREE_VALUE (p2
);
363 pedwarn ("function types not truly compatible in ISO C");
367 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
368 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
371 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
372 memb
; memb
= TREE_CHAIN (memb
))
373 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p1
)))
375 TREE_VALUE (n
) = TREE_VALUE (p1
);
377 pedwarn ("function types not truly compatible in ISO C");
381 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
385 c_override_global_bindings_to_false
= false;
386 t1
= build_function_type (valtype
, newargs
);
387 t1
= qualify_type (t1
, t2
);
388 /* ... falls through ... */
392 return build_type_attribute_variant (t1
, attributes
);
397 /* Return the type of a conditional expression between pointers to
398 possibly differently qualified versions of compatible types.
400 We assume that comp_target_types has already been done and returned
401 nonzero; if that isn't so, this may crash. */
404 common_pointer_type (tree t1
, tree t2
)
411 /* Save time if the two types are the same. */
413 if (t1
== t2
) return t1
;
415 /* If one type is nonsense, use the other. */
416 if (t1
== error_mark_node
)
418 if (t2
== error_mark_node
)
421 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
422 && TREE_CODE (t2
) == POINTER_TYPE
);
424 /* Merge the attributes. */
425 attributes
= targetm
.merge_type_attributes (t1
, t2
);
427 /* Find the composite type of the target types, and combine the
428 qualifiers of the two types' targets. */
429 pointed_to_1
= TREE_TYPE (t1
);
430 pointed_to_2
= TREE_TYPE (t2
);
431 target
= composite_type (TYPE_MAIN_VARIANT (pointed_to_1
),
432 TYPE_MAIN_VARIANT (pointed_to_2
));
433 t1
= build_pointer_type (c_build_qualified_type
435 TYPE_QUALS (pointed_to_1
) |
436 TYPE_QUALS (pointed_to_2
)));
437 return build_type_attribute_variant (t1
, attributes
);
440 /* Return the common type for two arithmetic types under the usual
441 arithmetic conversions. The default conversions have already been
442 applied, and enumerated types converted to their compatible integer
443 types. The resulting type is unqualified and has no attributes.
445 This is the type for the result of most arithmetic operations
446 if the operands have the given two types. */
449 common_type (tree t1
, tree t2
)
451 enum tree_code code1
;
452 enum tree_code code2
;
454 /* If one type is nonsense, use the other. */
455 if (t1
== error_mark_node
)
457 if (t2
== error_mark_node
)
460 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
461 t1
= TYPE_MAIN_VARIANT (t1
);
463 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
464 t2
= TYPE_MAIN_VARIANT (t2
);
466 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
467 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
469 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
470 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
472 /* Save time if the two types are the same. */
474 if (t1
== t2
) return t1
;
476 code1
= TREE_CODE (t1
);
477 code2
= TREE_CODE (t2
);
479 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
480 || code1
== REAL_TYPE
|| code1
== INTEGER_TYPE
);
481 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
482 || code2
== REAL_TYPE
|| code2
== INTEGER_TYPE
);
484 /* If one type is a vector type, return that type. (How the usual
485 arithmetic conversions apply to the vector types extension is not
486 precisely specified.) */
487 if (code1
== VECTOR_TYPE
)
490 if (code2
== VECTOR_TYPE
)
493 /* If one type is complex, form the common type of the non-complex
494 components, then make that complex. Use T1 or T2 if it is the
496 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
498 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
499 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
500 tree subtype
= common_type (subtype1
, subtype2
);
502 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
504 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
507 return build_complex_type (subtype
);
510 /* If only one is real, use it as the result. */
512 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
515 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
518 /* Both real or both integers; use the one with greater precision. */
520 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
522 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
525 /* Same precision. Prefer long longs to longs to ints when the
526 same precision, following the C99 rules on integer type rank
527 (which are equivalent to the C90 rules for C90 types). */
529 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
530 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
531 return long_long_unsigned_type_node
;
533 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
534 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
536 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
537 return long_long_unsigned_type_node
;
539 return long_long_integer_type_node
;
542 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
543 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
544 return long_unsigned_type_node
;
546 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
547 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
549 /* But preserve unsignedness from the other type,
550 since long cannot hold all the values of an unsigned int. */
551 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
552 return long_unsigned_type_node
;
554 return long_integer_type_node
;
557 /* Likewise, prefer long double to double even if same size. */
558 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
559 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
560 return long_double_type_node
;
562 /* Otherwise prefer the unsigned one. */
564 if (TYPE_UNSIGNED (t1
))
570 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
571 or various other operations. Return 2 if they are compatible
572 but a warning may be needed if you use them together. */
575 comptypes (tree type1
, tree type2
)
581 /* Suppress errors caused by previously reported errors. */
583 if (t1
== t2
|| !t1
|| !t2
584 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
587 /* If either type is the internal version of sizetype, return the
589 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
590 && TYPE_ORIG_SIZE_TYPE (t1
))
591 t1
= TYPE_ORIG_SIZE_TYPE (t1
);
593 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
594 && TYPE_ORIG_SIZE_TYPE (t2
))
595 t2
= TYPE_ORIG_SIZE_TYPE (t2
);
598 /* Enumerated types are compatible with integer types, but this is
599 not transitive: two enumerated types in the same translation unit
600 are compatible with each other only if they are the same type. */
602 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
603 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
604 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
605 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
610 /* Different classes of types can't be compatible. */
612 if (TREE_CODE (t1
) != TREE_CODE (t2
))
615 /* Qualifiers must match. C99 6.7.3p9 */
617 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
620 /* Allow for two different type nodes which have essentially the same
621 definition. Note that we already checked for equality of the type
622 qualifiers (just above). */
624 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
627 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
628 if (! (attrval
= targetm
.comp_type_attributes (t1
, t2
)))
631 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
634 switch (TREE_CODE (t1
))
637 /* We must give ObjC the first crack at comparing pointers, since
638 protocol qualifiers may be involved. */
639 if (c_dialect_objc () && (val
= objc_comptypes (t1
, t2
, 0)) >= 0)
641 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
642 ? 1 : comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
)));
646 val
= function_types_compatible_p (t1
, t2
);
651 tree d1
= TYPE_DOMAIN (t1
);
652 tree d2
= TYPE_DOMAIN (t2
);
653 bool d1_variable
, d2_variable
;
654 bool d1_zero
, d2_zero
;
657 /* Target types must match incl. qualifiers. */
658 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
659 && 0 == (val
= comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))))
662 /* Sizes must match unless one is missing or variable. */
663 if (d1
== 0 || d2
== 0 || d1
== d2
)
666 d1_zero
= ! TYPE_MAX_VALUE (d1
);
667 d2_zero
= ! TYPE_MAX_VALUE (d2
);
669 d1_variable
= (! d1_zero
670 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
671 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
672 d2_variable
= (! d2_zero
673 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
674 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
676 if (d1_variable
|| d2_variable
)
678 if (d1_zero
&& d2_zero
)
680 if (d1_zero
|| d2_zero
681 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
682 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
689 /* We are dealing with two distinct structs. In assorted Objective-C
690 corner cases, however, these can still be deemed equivalent. */
691 if (c_dialect_objc () && objc_comptypes (t1
, t2
, 0) == 1)
696 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
697 val
= tagged_types_tu_compatible_p (t1
, t2
);
701 val
= TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
702 && comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
));
708 return attrval
== 2 && val
== 1 ? 2 : val
;
711 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
712 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
713 to 1 or 0 depending if the check of the pointer types is meant to
714 be reflexive or not (typically, assignments are not reflexive,
715 while comparisons are reflexive).
719 comp_target_types (tree ttl
, tree ttr
, int reflexive
)
723 /* Give objc_comptypes a crack at letting these types through. */
724 if ((val
= objc_comptypes (ttl
, ttr
, reflexive
)) >= 0)
727 val
= comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl
)),
728 TYPE_MAIN_VARIANT (TREE_TYPE (ttr
)));
730 if (val
== 2 && pedantic
)
731 pedwarn ("types are not quite compatible");
735 /* Subroutines of `comptypes'. */
737 /* Determine whether two trees derive from the same translation unit.
738 If the CONTEXT chain ends in a null, that tree's context is still
739 being parsed, so if two trees have context chains ending in null,
740 they're in the same translation unit. */
742 same_translation_unit_p (tree t1
, tree t2
)
744 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
745 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
747 case 'd': t1
= DECL_CONTEXT (t1
); break;
748 case 't': t1
= TYPE_CONTEXT (t1
); break;
749 case 'x': t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
750 default: gcc_unreachable ();
753 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
754 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
756 case 'd': t2
= DECL_CONTEXT (t2
); break;
757 case 't': t2
= TYPE_CONTEXT (t2
); break;
758 case 'x': t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
759 default: gcc_unreachable ();
765 /* The C standard says that two structures in different translation
766 units are compatible with each other only if the types of their
767 fields are compatible (among other things). So, consider two copies
768 of this structure: */
770 struct tagged_tu_seen
{
771 const struct tagged_tu_seen
* next
;
776 /* Can they be compatible with each other? We choose to break the
777 recursion by allowing those types to be compatible. */
779 static const struct tagged_tu_seen
* tagged_tu_seen_base
;
781 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
782 compatible. If the two types are not the same (which has been
783 checked earlier), this can only happen when multiple translation
784 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
788 tagged_types_tu_compatible_p (tree t1
, tree t2
)
791 bool needs_warning
= false;
793 /* We have to verify that the tags of the types are the same. This
794 is harder than it looks because this may be a typedef, so we have
795 to go look at the original type. It may even be a typedef of a
797 In the case of compiler-created builtin structs the TYPE_DECL
798 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
799 while (TYPE_NAME (t1
)
800 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
801 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
802 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
804 while (TYPE_NAME (t2
)
805 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
806 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
807 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
809 /* C90 didn't have the requirement that the two tags be the same. */
810 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
813 /* C90 didn't say what happened if one or both of the types were
814 incomplete; we choose to follow C99 rules here, which is that they
816 if (TYPE_SIZE (t1
) == NULL
817 || TYPE_SIZE (t2
) == NULL
)
821 const struct tagged_tu_seen
* tts_i
;
822 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
823 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
827 switch (TREE_CODE (t1
))
832 /* Speed up the case where the type values are in the same order. */
833 tree tv1
= TYPE_VALUES (t1
);
834 tree tv2
= TYPE_VALUES (t2
);
839 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
841 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
843 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
847 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
849 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
852 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
855 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
857 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
859 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
867 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
870 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= TREE_CHAIN (s1
))
873 struct tagged_tu_seen tts
;
875 tts
.next
= tagged_tu_seen_base
;
878 tagged_tu_seen_base
= &tts
;
880 if (DECL_NAME (s1
) != NULL
)
881 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= TREE_CHAIN (s2
))
882 if (DECL_NAME (s1
) == DECL_NAME (s2
))
885 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
));
889 needs_warning
= true;
891 if (TREE_CODE (s1
) == FIELD_DECL
892 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
893 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
899 tagged_tu_seen_base
= tts
.next
;
903 return needs_warning
? 2 : 1;
908 struct tagged_tu_seen tts
;
910 tts
.next
= tagged_tu_seen_base
;
913 tagged_tu_seen_base
= &tts
;
915 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
917 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
920 if (TREE_CODE (s1
) != TREE_CODE (s2
)
921 || DECL_NAME (s1
) != DECL_NAME (s2
))
923 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
));
927 needs_warning
= true;
929 if (TREE_CODE (s1
) == FIELD_DECL
930 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
931 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
934 tagged_tu_seen_base
= tts
.next
;
937 return needs_warning
? 2 : 1;
945 /* Return 1 if two function types F1 and F2 are compatible.
946 If either type specifies no argument types,
947 the other must specify a fixed number of self-promoting arg types.
948 Otherwise, if one type specifies only the number of arguments,
949 the other must specify that number of self-promoting arg types.
950 Otherwise, the argument types must match. */
953 function_types_compatible_p (tree f1
, tree f2
)
956 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
961 ret1
= TREE_TYPE (f1
);
962 ret2
= TREE_TYPE (f2
);
964 /* 'volatile' qualifiers on a function's return type used to mean
965 the function is noreturn. */
966 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
967 pedwarn ("function return types not compatible due to %<volatile%>");
968 if (TYPE_VOLATILE (ret1
))
969 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
970 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
971 if (TYPE_VOLATILE (ret2
))
972 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
973 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
974 val
= comptypes (ret1
, ret2
);
978 args1
= TYPE_ARG_TYPES (f1
);
979 args2
= TYPE_ARG_TYPES (f2
);
981 /* An unspecified parmlist matches any specified parmlist
982 whose argument types don't need default promotions. */
986 if (!self_promoting_args_p (args2
))
988 /* If one of these types comes from a non-prototype fn definition,
989 compare that with the other type's arglist.
990 If they don't match, ask for a warning (but no error). */
991 if (TYPE_ACTUAL_ARG_TYPES (f1
)
992 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
)))
998 if (!self_promoting_args_p (args1
))
1000 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1001 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
)))
1006 /* Both types have argument lists: compare them and propagate results. */
1007 val1
= type_lists_compatible_p (args1
, args2
);
1008 return val1
!= 1 ? val1
: val
;
1011 /* Check two lists of types for compatibility,
1012 returning 0 for incompatible, 1 for compatible,
1013 or 2 for compatible with warning. */
1016 type_lists_compatible_p (tree args1
, tree args2
)
1018 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1024 if (args1
== 0 && args2
== 0)
1026 /* If one list is shorter than the other,
1027 they fail to match. */
1028 if (args1
== 0 || args2
== 0)
1030 /* A null pointer instead of a type
1031 means there is supposed to be an argument
1032 but nothing is specified about what type it has.
1033 So match anything that self-promotes. */
1034 if (TREE_VALUE (args1
) == 0)
1036 if (c_type_promotes_to (TREE_VALUE (args2
)) != TREE_VALUE (args2
))
1039 else if (TREE_VALUE (args2
) == 0)
1041 if (c_type_promotes_to (TREE_VALUE (args1
)) != TREE_VALUE (args1
))
1044 /* If one of the lists has an error marker, ignore this arg. */
1045 else if (TREE_CODE (TREE_VALUE (args1
)) == ERROR_MARK
1046 || TREE_CODE (TREE_VALUE (args2
)) == ERROR_MARK
)
1048 else if (! (newval
= comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1
)),
1049 TYPE_MAIN_VARIANT (TREE_VALUE (args2
)))))
1051 /* Allow wait (union {union wait *u; int *i} *)
1052 and wait (union wait *) to be compatible. */
1053 if (TREE_CODE (TREE_VALUE (args1
)) == UNION_TYPE
1054 && (TYPE_NAME (TREE_VALUE (args1
)) == 0
1055 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1
)))
1056 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1
))) == INTEGER_CST
1057 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1
)),
1058 TYPE_SIZE (TREE_VALUE (args2
))))
1061 for (memb
= TYPE_FIELDS (TREE_VALUE (args1
));
1062 memb
; memb
= TREE_CHAIN (memb
))
1063 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args2
)))
1068 else if (TREE_CODE (TREE_VALUE (args2
)) == UNION_TYPE
1069 && (TYPE_NAME (TREE_VALUE (args2
)) == 0
1070 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2
)))
1071 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2
))) == INTEGER_CST
1072 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2
)),
1073 TYPE_SIZE (TREE_VALUE (args1
))))
1076 for (memb
= TYPE_FIELDS (TREE_VALUE (args2
));
1077 memb
; memb
= TREE_CHAIN (memb
))
1078 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args1
)))
1087 /* comptypes said ok, but record if it said to warn. */
1091 args1
= TREE_CHAIN (args1
);
1092 args2
= TREE_CHAIN (args2
);
1096 /* Compute the size to increment a pointer by. */
1099 c_size_in_bytes (tree type
)
1101 enum tree_code code
= TREE_CODE (type
);
1103 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
1104 return size_one_node
;
1106 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1108 error ("arithmetic on pointer to an incomplete type");
1109 return size_one_node
;
1112 /* Convert in case a char is more than one unit. */
1113 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1114 size_int (TYPE_PRECISION (char_type_node
)
1118 /* Return either DECL or its known constant value (if it has one). */
1121 decl_constant_value (tree decl
)
1123 if (/* Don't change a variable array bound or initial value to a constant
1124 in a place where a variable is invalid. Note that DECL_INITIAL
1125 isn't valid for a PARM_DECL. */
1126 current_function_decl
!= 0
1127 && TREE_CODE (decl
) != PARM_DECL
1128 && ! TREE_THIS_VOLATILE (decl
)
1129 && TREE_READONLY (decl
)
1130 && DECL_INITIAL (decl
) != 0
1131 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1132 /* This is invalid if initial value is not constant.
1133 If it has either a function call, a memory reference,
1134 or a variable, then re-evaluating it could give different results. */
1135 && TREE_CONSTANT (DECL_INITIAL (decl
))
1136 /* Check for cases where this is sub-optimal, even though valid. */
1137 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1138 return DECL_INITIAL (decl
);
1142 /* Return either DECL or its known constant value (if it has one), but
1143 return DECL if pedantic or DECL has mode BLKmode. This is for
1144 bug-compatibility with the old behavior of decl_constant_value
1145 (before GCC 3.0); every use of this function is a bug and it should
1146 be removed before GCC 3.1. It is not appropriate to use pedantic
1147 in a way that affects optimization, and BLKmode is probably not the
1148 right test for avoiding misoptimizations either. */
1151 decl_constant_value_for_broken_optimization (tree decl
)
1153 if (pedantic
|| DECL_MODE (decl
) == BLKmode
)
1156 return decl_constant_value (decl
);
1160 /* Perform the default conversion of arrays and functions to pointers.
1161 Return the result of converting EXP. For any other expression, just
1165 default_function_array_conversion (tree exp
)
1168 tree type
= TREE_TYPE (exp
);
1169 enum tree_code code
= TREE_CODE (type
);
1172 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1175 Do not use STRIP_NOPS here! It will remove conversions from pointer
1176 to integer and cause infinite recursion. */
1178 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1179 || (TREE_CODE (exp
) == NOP_EXPR
1180 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1182 if (TREE_CODE (exp
) == NON_LVALUE_EXPR
)
1184 exp
= TREE_OPERAND (exp
, 0);
1187 if (TREE_NO_WARNING (orig_exp
))
1188 TREE_NO_WARNING (exp
) = 1;
1190 if (code
== FUNCTION_TYPE
)
1192 return build_unary_op (ADDR_EXPR
, exp
, 0);
1194 if (code
== ARRAY_TYPE
)
1197 tree restype
= TREE_TYPE (type
);
1203 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r' || DECL_P (exp
))
1205 constp
= TREE_READONLY (exp
);
1206 volatilep
= TREE_THIS_VOLATILE (exp
);
1209 if (TYPE_QUALS (type
) || constp
|| volatilep
)
1211 = c_build_qualified_type (restype
,
1213 | (constp
* TYPE_QUAL_CONST
)
1214 | (volatilep
* TYPE_QUAL_VOLATILE
));
1216 if (TREE_CODE (exp
) == INDIRECT_REF
)
1217 return convert (build_pointer_type (restype
),
1218 TREE_OPERAND (exp
, 0));
1220 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1222 tree op1
= default_conversion (TREE_OPERAND (exp
, 1));
1223 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1224 TREE_OPERAND (exp
, 0), op1
);
1227 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
);
1228 if (!flag_isoc99
&& !lvalue_array_p
)
1230 /* Before C99, non-lvalue arrays do not decay to pointers.
1231 Normally, using such an array would be invalid; but it can
1232 be used correctly inside sizeof or as a statement expression.
1233 Thus, do not give an error here; an error will result later. */
1237 ptrtype
= build_pointer_type (restype
);
1239 if (TREE_CODE (exp
) == VAR_DECL
)
1241 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1242 ADDR_EXPR because it's the best way of representing what
1243 happens in C when we take the address of an array and place
1244 it in a pointer to the element type. */
1245 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1246 if (!c_mark_addressable (exp
))
1247 return error_mark_node
;
1248 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1251 /* This way is better for a COMPONENT_REF since it can
1252 simplify the offset for a component. */
1253 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1254 return convert (ptrtype
, adr
);
1259 /* Perform default promotions for C data used in expressions.
1260 Arrays and functions are converted to pointers;
1261 enumeral types or short or char, to int.
1262 In addition, manifest constants symbols are replaced by their values. */
1265 default_conversion (tree exp
)
1268 tree type
= TREE_TYPE (exp
);
1269 enum tree_code code
= TREE_CODE (type
);
1271 if (code
== FUNCTION_TYPE
|| code
== ARRAY_TYPE
)
1272 return default_function_array_conversion (exp
);
1274 /* Constants can be used directly unless they're not loadable. */
1275 if (TREE_CODE (exp
) == CONST_DECL
)
1276 exp
= DECL_INITIAL (exp
);
1278 /* Replace a nonvolatile const static variable with its value unless
1279 it is an array, in which case we must be sure that taking the
1280 address of the array produces consistent results. */
1281 else if (optimize
&& TREE_CODE (exp
) == VAR_DECL
&& code
!= ARRAY_TYPE
)
1283 exp
= decl_constant_value_for_broken_optimization (exp
);
1284 type
= TREE_TYPE (exp
);
1287 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1290 Do not use STRIP_NOPS here! It will remove conversions from pointer
1291 to integer and cause infinite recursion. */
1293 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1294 || (TREE_CODE (exp
) == NOP_EXPR
1295 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1296 exp
= TREE_OPERAND (exp
, 0);
1298 if (TREE_NO_WARNING (orig_exp
))
1299 TREE_NO_WARNING (exp
) = 1;
1301 /* Normally convert enums to int,
1302 but convert wide enums to something wider. */
1303 if (code
== ENUMERAL_TYPE
)
1305 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
1306 TYPE_PRECISION (integer_type_node
)),
1307 ((TYPE_PRECISION (type
)
1308 >= TYPE_PRECISION (integer_type_node
))
1309 && TYPE_UNSIGNED (type
)));
1311 return convert (type
, exp
);
1314 if (TREE_CODE (exp
) == COMPONENT_REF
1315 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
1316 /* If it's thinner than an int, promote it like a
1317 c_promoting_integer_type_p, otherwise leave it alone. */
1318 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
1319 TYPE_PRECISION (integer_type_node
)))
1320 return convert (integer_type_node
, exp
);
1322 if (c_promoting_integer_type_p (type
))
1324 /* Preserve unsignedness if not really getting any wider. */
1325 if (TYPE_UNSIGNED (type
)
1326 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1327 return convert (unsigned_type_node
, exp
);
1329 return convert (integer_type_node
, exp
);
1332 if (code
== VOID_TYPE
)
1334 error ("void value not ignored as it ought to be");
1335 return error_mark_node
;
1340 /* Look up COMPONENT in a structure or union DECL.
1342 If the component name is not found, returns NULL_TREE. Otherwise,
1343 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1344 stepping down the chain to the component, which is in the last
1345 TREE_VALUE of the list. Normally the list is of length one, but if
1346 the component is embedded within (nested) anonymous structures or
1347 unions, the list steps down the chain to the component. */
1350 lookup_field (tree decl
, tree component
)
1352 tree type
= TREE_TYPE (decl
);
1355 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1356 to the field elements. Use a binary search on this array to quickly
1357 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1358 will always be set for structures which have many elements. */
1360 if (TYPE_LANG_SPECIFIC (type
))
1363 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
1365 field
= TYPE_FIELDS (type
);
1367 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
1368 while (top
- bot
> 1)
1370 half
= (top
- bot
+ 1) >> 1;
1371 field
= field_array
[bot
+half
];
1373 if (DECL_NAME (field
) == NULL_TREE
)
1375 /* Step through all anon unions in linear fashion. */
1376 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
1378 field
= field_array
[bot
++];
1379 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1380 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1382 tree anon
= lookup_field (field
, component
);
1385 return tree_cons (NULL_TREE
, field
, anon
);
1389 /* Entire record is only anon unions. */
1393 /* Restart the binary search, with new lower bound. */
1397 if (DECL_NAME (field
) == component
)
1399 if (DECL_NAME (field
) < component
)
1405 if (DECL_NAME (field_array
[bot
]) == component
)
1406 field
= field_array
[bot
];
1407 else if (DECL_NAME (field
) != component
)
1412 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1414 if (DECL_NAME (field
) == NULL_TREE
1415 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1416 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
1418 tree anon
= lookup_field (field
, component
);
1421 return tree_cons (NULL_TREE
, field
, anon
);
1424 if (DECL_NAME (field
) == component
)
1428 if (field
== NULL_TREE
)
1432 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
1435 /* Make an expression to refer to the COMPONENT field of
1436 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1439 build_component_ref (tree datum
, tree component
)
1441 tree type
= TREE_TYPE (datum
);
1442 enum tree_code code
= TREE_CODE (type
);
1446 if (!objc_is_public (datum
, component
))
1447 return error_mark_node
;
1449 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1450 Ensure that the arguments are not lvalues; otherwise,
1451 if the component is an array, it would wrongly decay to a pointer in
1453 We cannot do this with a COND_EXPR, because in a conditional expression
1454 the default promotions are applied to both sides, and this would yield
1455 the wrong type of the result; for example, if the components have
1457 switch (TREE_CODE (datum
))
1461 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
);
1462 return build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
1463 TREE_OPERAND (datum
, 0), non_lvalue (value
));
1469 /* See if there is a field or component with name COMPONENT. */
1471 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1473 if (!COMPLETE_TYPE_P (type
))
1475 c_incomplete_type_error (NULL_TREE
, type
);
1476 return error_mark_node
;
1479 field
= lookup_field (datum
, component
);
1483 error ("%s has no member named %qs",
1484 code
== RECORD_TYPE
? "structure" : "union",
1485 IDENTIFIER_POINTER (component
));
1486 return error_mark_node
;
1489 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1490 This might be better solved in future the way the C++ front
1491 end does it - by giving the anonymous entities each a
1492 separate name and type, and then have build_component_ref
1493 recursively call itself. We can't do that here. */
1496 tree subdatum
= TREE_VALUE (field
);
1498 if (TREE_TYPE (subdatum
) == error_mark_node
)
1499 return error_mark_node
;
1501 ref
= build3 (COMPONENT_REF
, TREE_TYPE (subdatum
), datum
, subdatum
,
1503 if (TREE_READONLY (datum
) || TREE_READONLY (subdatum
))
1504 TREE_READONLY (ref
) = 1;
1505 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (subdatum
))
1506 TREE_THIS_VOLATILE (ref
) = 1;
1508 if (TREE_DEPRECATED (subdatum
))
1509 warn_deprecated_use (subdatum
);
1513 field
= TREE_CHAIN (field
);
1519 else if (code
!= ERROR_MARK
)
1520 error ("request for member %qs in something not a structure or union",
1521 IDENTIFIER_POINTER (component
));
1523 return error_mark_node
;
1526 /* Given an expression PTR for a pointer, return an expression
1527 for the value pointed to.
1528 ERRORSTRING is the name of the operator to appear in error messages. */
1531 build_indirect_ref (tree ptr
, const char *errorstring
)
1533 tree pointer
= default_conversion (ptr
);
1534 tree type
= TREE_TYPE (pointer
);
1536 if (TREE_CODE (type
) == POINTER_TYPE
)
1538 if (TREE_CODE (pointer
) == ADDR_EXPR
1539 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
1540 == TREE_TYPE (type
)))
1541 return TREE_OPERAND (pointer
, 0);
1544 tree t
= TREE_TYPE (type
);
1545 tree ref
= build1 (INDIRECT_REF
, TYPE_MAIN_VARIANT (t
), pointer
);
1547 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
1549 error ("dereferencing pointer to incomplete type");
1550 return error_mark_node
;
1552 if (VOID_TYPE_P (t
) && skip_evaluation
== 0)
1553 warning ("dereferencing %<void *%> pointer");
1555 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1556 so that we get the proper error message if the result is used
1557 to assign to. Also, &* is supposed to be a no-op.
1558 And ANSI C seems to specify that the type of the result
1559 should be the const type. */
1560 /* A de-reference of a pointer to const is not a const. It is valid
1561 to change it via some other pointer. */
1562 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1563 TREE_SIDE_EFFECTS (ref
)
1564 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1565 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1569 else if (TREE_CODE (pointer
) != ERROR_MARK
)
1570 error ("invalid type argument of %qs", errorstring
);
1571 return error_mark_node
;
1574 /* This handles expressions of the form "a[i]", which denotes
1577 This is logically equivalent in C to *(a+i), but we may do it differently.
1578 If A is a variable or a member, we generate a primitive ARRAY_REF.
1579 This avoids forcing the array out of registers, and can work on
1580 arrays that are not lvalues (for example, members of structures returned
1584 build_array_ref (tree array
, tree index
)
1588 error ("subscript missing in array reference");
1589 return error_mark_node
;
1592 if (TREE_TYPE (array
) == error_mark_node
1593 || TREE_TYPE (index
) == error_mark_node
)
1594 return error_mark_node
;
1596 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
1600 /* Subscripting with type char is likely to lose
1601 on a machine where chars are signed.
1602 So warn on any machine, but optionally.
1603 Don't warn for unsigned char since that type is safe.
1604 Don't warn for signed char because anyone who uses that
1605 must have done so deliberately. */
1606 if (warn_char_subscripts
1607 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1608 warning ("array subscript has type %<char%>");
1610 /* Apply default promotions *after* noticing character types. */
1611 index
= default_conversion (index
);
1613 /* Require integer *after* promotion, for sake of enums. */
1614 if (TREE_CODE (TREE_TYPE (index
)) != INTEGER_TYPE
)
1616 error ("array subscript is not an integer");
1617 return error_mark_node
;
1620 /* An array that is indexed by a non-constant
1621 cannot be stored in a register; we must be able to do
1622 address arithmetic on its address.
1623 Likewise an array of elements of variable size. */
1624 if (TREE_CODE (index
) != INTEGER_CST
1625 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
1626 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1628 if (!c_mark_addressable (array
))
1629 return error_mark_node
;
1631 /* An array that is indexed by a constant value which is not within
1632 the array bounds cannot be stored in a register either; because we
1633 would get a crash in store_bit_field/extract_bit_field when trying
1634 to access a non-existent part of the register. */
1635 if (TREE_CODE (index
) == INTEGER_CST
1636 && TYPE_DOMAIN (TREE_TYPE (array
))
1637 && ! int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
1639 if (!c_mark_addressable (array
))
1640 return error_mark_node
;
1646 while (TREE_CODE (foo
) == COMPONENT_REF
)
1647 foo
= TREE_OPERAND (foo
, 0);
1648 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
1649 pedwarn ("ISO C forbids subscripting %<register%> array");
1650 else if (! flag_isoc99
&& ! lvalue_p (foo
))
1651 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1654 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1655 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
1656 /* Array ref is const/volatile if the array elements are
1657 or if the array is. */
1658 TREE_READONLY (rval
)
1659 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
1660 | TREE_READONLY (array
));
1661 TREE_SIDE_EFFECTS (rval
)
1662 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1663 | TREE_SIDE_EFFECTS (array
));
1664 TREE_THIS_VOLATILE (rval
)
1665 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1666 /* This was added by rms on 16 Nov 91.
1667 It fixes vol struct foo *a; a->elts[1]
1668 in an inline function.
1669 Hope it doesn't break something else. */
1670 | TREE_THIS_VOLATILE (array
));
1671 return require_complete_type (fold (rval
));
1675 tree ar
= default_conversion (array
);
1676 tree ind
= default_conversion (index
);
1678 /* Do the same warning check as above, but only on the part that's
1679 syntactically the index and only if it is also semantically
1681 if (warn_char_subscripts
1682 && TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
1683 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1684 warning ("subscript has type %<char%>");
1686 /* Put the integer in IND to simplify error checking. */
1687 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
1694 if (ar
== error_mark_node
)
1697 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
1698 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) == FUNCTION_TYPE
)
1700 error ("subscripted value is neither array nor pointer");
1701 return error_mark_node
;
1703 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
1705 error ("array subscript is not an integer");
1706 return error_mark_node
;
1709 return build_indirect_ref (build_binary_op (PLUS_EXPR
, ar
, ind
, 0),
1714 /* Build an external reference to identifier ID. FUN indicates
1715 whether this will be used for a function call. */
1717 build_external_ref (tree id
, int fun
)
1720 tree decl
= lookup_name (id
);
1721 tree objc_ivar
= objc_lookup_ivar (id
);
1723 if (decl
&& decl
!= error_mark_node
)
1725 /* Properly declared variable or function reference. */
1728 else if (decl
!= objc_ivar
&& !DECL_FILE_SCOPE_P (decl
))
1730 warning ("local declaration of %qs hides instance variable",
1731 IDENTIFIER_POINTER (id
));
1740 /* Implicit function declaration. */
1741 ref
= implicitly_declare (id
);
1742 else if (decl
== error_mark_node
)
1743 /* Don't complain about something that's already been
1744 complained about. */
1745 return error_mark_node
;
1748 undeclared_variable (id
);
1749 return error_mark_node
;
1752 if (TREE_TYPE (ref
) == error_mark_node
)
1753 return error_mark_node
;
1755 if (TREE_DEPRECATED (ref
))
1756 warn_deprecated_use (ref
);
1758 if (!skip_evaluation
)
1759 assemble_external (ref
);
1760 TREE_USED (ref
) = 1;
1762 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
1764 if (!in_sizeof
&& !in_typeof
)
1765 C_DECL_USED (ref
) = 1;
1766 else if (DECL_INITIAL (ref
) == 0
1767 && DECL_EXTERNAL (ref
)
1768 && !TREE_PUBLIC (ref
))
1769 record_maybe_used_decl (ref
);
1772 if (TREE_CODE (ref
) == CONST_DECL
)
1774 ref
= DECL_INITIAL (ref
);
1775 TREE_CONSTANT (ref
) = 1;
1776 TREE_INVARIANT (ref
) = 1;
1778 else if (current_function_decl
!= 0
1779 && !DECL_FILE_SCOPE_P (current_function_decl
)
1780 && (TREE_CODE (ref
) == VAR_DECL
1781 || TREE_CODE (ref
) == PARM_DECL
1782 || TREE_CODE (ref
) == FUNCTION_DECL
))
1784 tree context
= decl_function_context (ref
);
1786 if (context
!= 0 && context
!= current_function_decl
)
1787 DECL_NONLOCAL (ref
) = 1;
1793 /* Record details of decls possibly used inside sizeof or typeof. */
1794 struct maybe_used_decl
1798 /* The level seen at (in_sizeof + in_typeof). */
1800 /* The next one at this level or above, or NULL. */
1801 struct maybe_used_decl
*next
;
1804 static struct maybe_used_decl
*maybe_used_decls
;
1806 /* Record that DECL, an undefined static function reference seen
1807 inside sizeof or typeof, might be used if the operand of sizeof is
1808 a VLA type or the operand of typeof is a variably modified
1812 record_maybe_used_decl (tree decl
)
1814 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
1816 t
->level
= in_sizeof
+ in_typeof
;
1817 t
->next
= maybe_used_decls
;
1818 maybe_used_decls
= t
;
1821 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1822 USED is false, just discard them. If it is true, mark them used
1823 (if no longer inside sizeof or typeof) or move them to the next
1824 level up (if still inside sizeof or typeof). */
1827 pop_maybe_used (bool used
)
1829 struct maybe_used_decl
*p
= maybe_used_decls
;
1830 int cur_level
= in_sizeof
+ in_typeof
;
1831 while (p
&& p
->level
> cur_level
)
1836 C_DECL_USED (p
->decl
) = 1;
1838 p
->level
= cur_level
;
1842 if (!used
|| cur_level
== 0)
1843 maybe_used_decls
= p
;
1846 /* Return the result of sizeof applied to EXPR. */
1849 c_expr_sizeof_expr (struct c_expr expr
)
1852 ret
.value
= c_sizeof (TREE_TYPE (expr
.value
));
1853 ret
.original_code
= ERROR_MARK
;
1854 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr
.value
)));
1858 /* Return the result of sizeof applied to T, a structure for the type
1859 name passed to sizeof (rather than the type itself). */
1862 c_expr_sizeof_type (struct c_type_name
*t
)
1866 type
= groktypename (t
);
1867 ret
.value
= c_sizeof (type
);
1868 ret
.original_code
= ERROR_MARK
;
1869 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type
));
1873 /* Build a function call to function FUNCTION with parameters PARAMS.
1874 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1875 TREE_VALUE of each node is a parameter-expression.
1876 FUNCTION's data type may be a function type or a pointer-to-function. */
1879 build_function_call (tree function
, tree params
)
1881 tree fntype
, fundecl
= 0;
1882 tree coerced_params
;
1883 tree name
= NULL_TREE
, result
;
1886 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1887 STRIP_TYPE_NOPS (function
);
1889 /* Convert anything with function type to a pointer-to-function. */
1890 if (TREE_CODE (function
) == FUNCTION_DECL
)
1892 name
= DECL_NAME (function
);
1894 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1895 (because calling an inline function does not mean the function
1896 needs to be separately compiled). */
1897 fntype
= build_type_variant (TREE_TYPE (function
),
1898 TREE_READONLY (function
),
1899 TREE_THIS_VOLATILE (function
));
1901 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
1904 function
= default_conversion (function
);
1906 fntype
= TREE_TYPE (function
);
1908 if (TREE_CODE (fntype
) == ERROR_MARK
)
1909 return error_mark_node
;
1911 if (!(TREE_CODE (fntype
) == POINTER_TYPE
1912 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
1914 error ("called object is not a function");
1915 return error_mark_node
;
1918 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
1919 current_function_returns_abnormally
= 1;
1921 /* fntype now gets the type of function pointed to. */
1922 fntype
= TREE_TYPE (fntype
);
1924 /* Check that the function is called through a compatible prototype.
1925 If it is not, replace the call by a trap, wrapped up in a compound
1926 expression if necessary. This has the nice side-effect to prevent
1927 the tree-inliner from generating invalid assignment trees which may
1928 blow up in the RTL expander later.
1930 ??? This doesn't work for Objective-C because objc_comptypes
1931 refuses to compare function prototypes, yet the compiler appears
1932 to build calls that are flagged as invalid by C's comptypes. */
1933 if (! c_dialect_objc ()
1934 && TREE_CODE (function
) == NOP_EXPR
1935 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
1936 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
1937 && ! comptypes (fntype
, TREE_TYPE (tem
)))
1939 tree return_type
= TREE_TYPE (fntype
);
1940 tree trap
= build_function_call (built_in_decls
[BUILT_IN_TRAP
],
1943 /* This situation leads to run-time undefined behavior. We can't,
1944 therefore, simply error unless we can prove that all possible
1945 executions of the program must execute the code. */
1946 warning ("function called through a non-compatible type");
1948 /* We can, however, treat "undefined" any way we please.
1949 Call abort to encourage the user to fix the program. */
1950 inform ("if this code is reached, the program will abort");
1952 if (VOID_TYPE_P (return_type
))
1958 if (AGGREGATE_TYPE_P (return_type
))
1959 rhs
= build_compound_literal (return_type
,
1960 build_constructor (return_type
,
1963 rhs
= fold (build1 (NOP_EXPR
, return_type
, integer_zero_node
));
1965 return build2 (COMPOUND_EXPR
, return_type
, trap
, rhs
);
1969 /* Convert the parameters to the types declared in the
1970 function prototype, or apply default promotions. */
1973 = convert_arguments (TYPE_ARG_TYPES (fntype
), params
, name
, fundecl
);
1975 /* Check that the arguments to the function are valid. */
1977 check_function_arguments (TYPE_ATTRIBUTES (fntype
), coerced_params
);
1979 result
= build3 (CALL_EXPR
, TREE_TYPE (fntype
),
1980 function
, coerced_params
, NULL_TREE
);
1981 TREE_SIDE_EFFECTS (result
) = 1;
1983 if (require_constant_value
)
1985 result
= fold_initializer (result
);
1987 if (TREE_CONSTANT (result
)
1988 && (name
== NULL_TREE
1989 || strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10) != 0))
1990 pedwarn_init ("initializer element is not constant");
1993 result
= fold (result
);
1995 if (VOID_TYPE_P (TREE_TYPE (result
)))
1997 return require_complete_type (result
);
2000 /* Convert the argument expressions in the list VALUES
2001 to the types in the list TYPELIST. The result is a list of converted
2002 argument expressions.
2004 If TYPELIST is exhausted, or when an element has NULL as its type,
2005 perform the default conversions.
2007 PARMLIST is the chain of parm decls for the function being called.
2008 It may be 0, if that info is not available.
2009 It is used only for generating error messages.
2011 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2013 This is also where warnings about wrong number of args are generated.
2015 Both VALUES and the returned value are chains of TREE_LIST nodes
2016 with the elements of the list in the TREE_VALUE slots of those nodes. */
2019 convert_arguments (tree typelist
, tree values
, tree name
, tree fundecl
)
2021 tree typetail
, valtail
;
2025 /* Scan the given expressions and types, producing individual
2026 converted arguments and pushing them on RESULT in reverse order. */
2028 for (valtail
= values
, typetail
= typelist
, parmnum
= 0;
2030 valtail
= TREE_CHAIN (valtail
), parmnum
++)
2032 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2033 tree val
= TREE_VALUE (valtail
);
2035 if (type
== void_type_node
)
2038 error ("too many arguments to function %qs",
2039 IDENTIFIER_POINTER (name
));
2041 error ("too many arguments to function");
2045 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2046 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2047 to convert automatically to a pointer. */
2048 if (TREE_CODE (val
) == NON_LVALUE_EXPR
)
2049 val
= TREE_OPERAND (val
, 0);
2051 val
= default_function_array_conversion (val
);
2053 val
= require_complete_type (val
);
2057 /* Formal parm type is specified by a function prototype. */
2060 if (!COMPLETE_TYPE_P (type
))
2062 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
2067 /* Optionally warn about conversions that
2068 differ from the default conversions. */
2069 if (warn_conversion
|| warn_traditional
)
2071 unsigned int formal_prec
= TYPE_PRECISION (type
);
2073 if (INTEGRAL_TYPE_P (type
)
2074 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2075 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
2076 if (INTEGRAL_TYPE_P (type
)
2077 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
2078 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
2079 else if (TREE_CODE (type
) == COMPLEX_TYPE
2080 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2081 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
2082 else if (TREE_CODE (type
) == REAL_TYPE
2083 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2084 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
2085 else if (TREE_CODE (type
) == COMPLEX_TYPE
2086 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2087 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
2088 else if (TREE_CODE (type
) == REAL_TYPE
2089 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
2090 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
2091 /* ??? At some point, messages should be written about
2092 conversions between complex types, but that's too messy
2094 else if (TREE_CODE (type
) == REAL_TYPE
2095 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2097 /* Warn if any argument is passed as `float',
2098 since without a prototype it would be `double'. */
2099 if (formal_prec
== TYPE_PRECISION (float_type_node
))
2100 warn_for_assignment ("%s as %<float%> rather than "
2101 "%<double%> due to prototype",
2102 (char *) 0, name
, parmnum
+ 1);
2104 /* Detect integer changing in width or signedness.
2105 These warnings are only activated with
2106 -Wconversion, not with -Wtraditional. */
2107 else if (warn_conversion
&& INTEGRAL_TYPE_P (type
)
2108 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2110 tree would_have_been
= default_conversion (val
);
2111 tree type1
= TREE_TYPE (would_have_been
);
2113 if (TREE_CODE (type
) == ENUMERAL_TYPE
2114 && (TYPE_MAIN_VARIANT (type
)
2115 == TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
2116 /* No warning if function asks for enum
2117 and the actual arg is that enum type. */
2119 else if (formal_prec
!= TYPE_PRECISION (type1
))
2120 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name
, parmnum
+ 1);
2121 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
2123 /* Don't complain if the formal parameter type
2124 is an enum, because we can't tell now whether
2125 the value was an enum--even the same enum. */
2126 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
2128 else if (TREE_CODE (val
) == INTEGER_CST
2129 && int_fits_type_p (val
, type
))
2130 /* Change in signedness doesn't matter
2131 if a constant value is unaffected. */
2133 /* Likewise for a constant in a NOP_EXPR. */
2134 else if (TREE_CODE (val
) == NOP_EXPR
2135 && TREE_CODE (TREE_OPERAND (val
, 0)) == INTEGER_CST
2136 && int_fits_type_p (TREE_OPERAND (val
, 0), type
))
2138 /* If the value is extended from a narrower
2139 unsigned type, it doesn't matter whether we
2140 pass it as signed or unsigned; the value
2141 certainly is the same either way. */
2142 else if (TYPE_PRECISION (TREE_TYPE (val
)) < TYPE_PRECISION (type
)
2143 && TYPE_UNSIGNED (TREE_TYPE (val
)))
2145 else if (TYPE_UNSIGNED (type
))
2146 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name
, parmnum
+ 1);
2148 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name
, parmnum
+ 1);
2152 parmval
= convert_for_assignment (type
, val
,
2153 (char *) 0, /* arg passing */
2154 fundecl
, name
, parmnum
+ 1);
2156 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
2157 && INTEGRAL_TYPE_P (type
)
2158 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2159 parmval
= default_conversion (parmval
);
2161 result
= tree_cons (NULL_TREE
, parmval
, result
);
2163 else if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2164 && (TYPE_PRECISION (TREE_TYPE (val
))
2165 < TYPE_PRECISION (double_type_node
)))
2166 /* Convert `float' to `double'. */
2167 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2169 /* Convert `short' and `char' to full-size `int'. */
2170 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2173 typetail
= TREE_CHAIN (typetail
);
2176 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
2179 error ("too few arguments to function %qs",
2180 IDENTIFIER_POINTER (name
));
2182 error ("too few arguments to function");
2185 return nreverse (result
);
2188 /* This is the entry point used by the parser
2189 for binary operators in the input.
2190 In addition to constructing the expression,
2191 we check for operands that were written with other binary operators
2192 in a way that is likely to confuse the user. */
2195 parser_build_binary_op (enum tree_code code
, struct c_expr arg1
,
2198 struct c_expr result
;
2200 enum tree_code code1
= arg1
.original_code
;
2201 enum tree_code code2
= arg2
.original_code
;
2203 result
.value
= build_binary_op (code
, arg1
.value
, arg2
.value
, 1);
2204 result
.original_code
= code
;
2206 if (TREE_CODE (result
.value
) == ERROR_MARK
)
2209 /* Check for cases such as x+y<<z which users are likely
2211 if (warn_parentheses
)
2213 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
2215 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2216 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2217 warning ("suggest parentheses around + or - inside shift");
2220 if (code
== TRUTH_ORIF_EXPR
)
2222 if (code1
== TRUTH_ANDIF_EXPR
2223 || code2
== TRUTH_ANDIF_EXPR
)
2224 warning ("suggest parentheses around && within ||");
2227 if (code
== BIT_IOR_EXPR
)
2229 if (code1
== BIT_AND_EXPR
|| code1
== BIT_XOR_EXPR
2230 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2231 || code2
== BIT_AND_EXPR
|| code2
== BIT_XOR_EXPR
2232 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2233 warning ("suggest parentheses around arithmetic in operand of |");
2234 /* Check cases like x|y==z */
2235 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2236 warning ("suggest parentheses around comparison in operand of |");
2239 if (code
== BIT_XOR_EXPR
)
2241 if (code1
== BIT_AND_EXPR
2242 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2243 || code2
== BIT_AND_EXPR
2244 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2245 warning ("suggest parentheses around arithmetic in operand of ^");
2246 /* Check cases like x^y==z */
2247 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2248 warning ("suggest parentheses around comparison in operand of ^");
2251 if (code
== BIT_AND_EXPR
)
2253 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2254 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2255 warning ("suggest parentheses around + or - in operand of &");
2256 /* Check cases like x&y==z */
2257 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2258 warning ("suggest parentheses around comparison in operand of &");
2260 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2261 if (TREE_CODE_CLASS (code
) == '<'
2262 && (TREE_CODE_CLASS (code1
) == '<'
2263 || TREE_CODE_CLASS (code2
) == '<'))
2264 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2268 unsigned_conversion_warning (result
.value
, arg1
.value
);
2269 unsigned_conversion_warning (result
.value
, arg2
.value
);
2270 overflow_warning (result
.value
);
2275 /* Return a tree for the difference of pointers OP0 and OP1.
2276 The resulting tree has type int. */
2279 pointer_diff (tree op0
, tree op1
)
2281 tree restype
= ptrdiff_type_node
;
2283 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
2284 tree con0
, con1
, lit0
, lit1
;
2285 tree orig_op1
= op1
;
2287 if (pedantic
|| warn_pointer_arith
)
2289 if (TREE_CODE (target_type
) == VOID_TYPE
)
2290 pedwarn ("pointer of type %<void *%> used in subtraction");
2291 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
2292 pedwarn ("pointer to a function used in subtraction");
2295 /* If the conversion to ptrdiff_type does anything like widening or
2296 converting a partial to an integral mode, we get a convert_expression
2297 that is in the way to do any simplifications.
2298 (fold-const.c doesn't know that the extra bits won't be needed.
2299 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2300 different mode in place.)
2301 So first try to find a common term here 'by hand'; we want to cover
2302 at least the cases that occur in legal static initializers. */
2303 con0
= TREE_CODE (op0
) == NOP_EXPR
? TREE_OPERAND (op0
, 0) : op0
;
2304 con1
= TREE_CODE (op1
) == NOP_EXPR
? TREE_OPERAND (op1
, 0) : op1
;
2306 if (TREE_CODE (con0
) == PLUS_EXPR
)
2308 lit0
= TREE_OPERAND (con0
, 1);
2309 con0
= TREE_OPERAND (con0
, 0);
2312 lit0
= integer_zero_node
;
2314 if (TREE_CODE (con1
) == PLUS_EXPR
)
2316 lit1
= TREE_OPERAND (con1
, 1);
2317 con1
= TREE_OPERAND (con1
, 0);
2320 lit1
= integer_zero_node
;
2322 if (operand_equal_p (con0
, con1
, 0))
2329 /* First do the subtraction as integers;
2330 then drop through to build the divide operator.
2331 Do not do default conversions on the minus operator
2332 in case restype is a short type. */
2334 op0
= build_binary_op (MINUS_EXPR
, convert (restype
, op0
),
2335 convert (restype
, op1
), 0);
2336 /* This generates an error if op1 is pointer to incomplete type. */
2337 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
2338 error ("arithmetic on pointer to an incomplete type");
2340 /* This generates an error if op0 is pointer to incomplete type. */
2341 op1
= c_size_in_bytes (target_type
);
2343 /* Divide by the size, in easiest possible way. */
2344 return fold (build2 (EXACT_DIV_EXPR
, restype
, op0
, convert (restype
, op1
)));
2347 /* Construct and perhaps optimize a tree representation
2348 for a unary operation. CODE, a tree_code, specifies the operation
2349 and XARG is the operand.
2350 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2351 the default promotions (such as from short to int).
2352 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2353 allows non-lvalues; this is only used to handle conversion of non-lvalue
2354 arrays to pointers in C99. */
2357 build_unary_op (enum tree_code code
, tree xarg
, int flag
)
2359 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2362 enum tree_code typecode
= TREE_CODE (TREE_TYPE (arg
));
2364 int noconvert
= flag
;
2366 if (typecode
== ERROR_MARK
)
2367 return error_mark_node
;
2368 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
2369 typecode
= INTEGER_TYPE
;
2374 /* This is used for unary plus, because a CONVERT_EXPR
2375 is enough to prevent anybody from looking inside for
2376 associativity, but won't generate any code. */
2377 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2378 || typecode
== COMPLEX_TYPE
2379 || typecode
== VECTOR_TYPE
))
2381 error ("wrong type argument to unary plus");
2382 return error_mark_node
;
2384 else if (!noconvert
)
2385 arg
= default_conversion (arg
);
2386 arg
= non_lvalue (arg
);
2390 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2391 || typecode
== COMPLEX_TYPE
2392 || typecode
== VECTOR_TYPE
))
2394 error ("wrong type argument to unary minus");
2395 return error_mark_node
;
2397 else if (!noconvert
)
2398 arg
= default_conversion (arg
);
2402 if (typecode
== INTEGER_TYPE
|| typecode
== VECTOR_TYPE
)
2405 arg
= default_conversion (arg
);
2407 else if (typecode
== COMPLEX_TYPE
)
2411 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2413 arg
= default_conversion (arg
);
2417 error ("wrong type argument to bit-complement");
2418 return error_mark_node
;
2423 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2425 error ("wrong type argument to abs");
2426 return error_mark_node
;
2428 else if (!noconvert
)
2429 arg
= default_conversion (arg
);
2433 /* Conjugating a real value is a no-op, but allow it anyway. */
2434 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2435 || typecode
== COMPLEX_TYPE
))
2437 error ("wrong type argument to conjugation");
2438 return error_mark_node
;
2440 else if (!noconvert
)
2441 arg
= default_conversion (arg
);
2444 case TRUTH_NOT_EXPR
:
2445 if (typecode
!= INTEGER_TYPE
2446 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
2447 && typecode
!= COMPLEX_TYPE
2448 /* These will convert to a pointer. */
2449 && typecode
!= ARRAY_TYPE
&& typecode
!= FUNCTION_TYPE
)
2451 error ("wrong type argument to unary exclamation mark");
2452 return error_mark_node
;
2454 arg
= lang_hooks
.truthvalue_conversion (arg
);
2455 return invert_truthvalue (arg
);
2461 if (TREE_CODE (arg
) == COMPLEX_CST
)
2462 return TREE_REALPART (arg
);
2463 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2464 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2469 if (TREE_CODE (arg
) == COMPLEX_CST
)
2470 return TREE_IMAGPART (arg
);
2471 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2472 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2474 return convert (TREE_TYPE (arg
), integer_zero_node
);
2476 case PREINCREMENT_EXPR
:
2477 case POSTINCREMENT_EXPR
:
2478 case PREDECREMENT_EXPR
:
2479 case POSTDECREMENT_EXPR
:
2481 /* Increment or decrement the real part of the value,
2482 and don't change the imaginary part. */
2483 if (typecode
== COMPLEX_TYPE
)
2488 pedwarn ("ISO C does not support %<++%> and %<--%>"
2489 " on complex types");
2491 arg
= stabilize_reference (arg
);
2492 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
2493 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
2494 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
2495 build_unary_op (code
, real
, 1), imag
);
2498 /* Report invalid types. */
2500 if (typecode
!= POINTER_TYPE
2501 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
2503 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2504 error ("wrong type argument to increment");
2506 error ("wrong type argument to decrement");
2508 return error_mark_node
;
2513 tree result_type
= TREE_TYPE (arg
);
2515 arg
= get_unwidened (arg
, 0);
2516 argtype
= TREE_TYPE (arg
);
2518 /* Compute the increment. */
2520 if (typecode
== POINTER_TYPE
)
2522 /* If pointer target is an undefined struct,
2523 we just cannot know how to do the arithmetic. */
2524 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type
)))
2526 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2527 error ("increment of pointer to unknown structure");
2529 error ("decrement of pointer to unknown structure");
2531 else if ((pedantic
|| warn_pointer_arith
)
2532 && (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
2533 || TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
))
2535 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2536 pedwarn ("wrong type argument to increment");
2538 pedwarn ("wrong type argument to decrement");
2541 inc
= c_size_in_bytes (TREE_TYPE (result_type
));
2544 inc
= integer_one_node
;
2546 inc
= convert (argtype
, inc
);
2548 /* Complain about anything else that is not a true lvalue. */
2549 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
2550 || code
== POSTINCREMENT_EXPR
)
2551 ? "invalid lvalue in increment"
2552 : "invalid lvalue in decrement")))
2553 return error_mark_node
;
2555 /* Report a read-only lvalue. */
2556 if (TREE_READONLY (arg
))
2557 readonly_error (arg
,
2558 ((code
== PREINCREMENT_EXPR
2559 || code
== POSTINCREMENT_EXPR
)
2560 ? "increment" : "decrement"));
2562 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
2563 val
= boolean_increment (code
, arg
);
2565 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
2566 TREE_SIDE_EFFECTS (val
) = 1;
2567 val
= convert (result_type
, val
);
2568 if (TREE_CODE (val
) != code
)
2569 TREE_NO_WARNING (val
) = 1;
2574 /* Note that this operation never does default_conversion. */
2576 /* Let &* cancel out to simplify resulting code. */
2577 if (TREE_CODE (arg
) == INDIRECT_REF
)
2579 /* Don't let this be an lvalue. */
2580 if (lvalue_p (TREE_OPERAND (arg
, 0)))
2581 return non_lvalue (TREE_OPERAND (arg
, 0));
2582 return TREE_OPERAND (arg
, 0);
2585 /* For &x[y], return x+y */
2586 if (TREE_CODE (arg
) == ARRAY_REF
)
2588 if (!c_mark_addressable (TREE_OPERAND (arg
, 0)))
2589 return error_mark_node
;
2590 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
2591 TREE_OPERAND (arg
, 1), 1);
2594 /* Anything not already handled and not a true memory reference
2595 or a non-lvalue array is an error. */
2596 else if (typecode
!= FUNCTION_TYPE
&& !flag
2597 && !lvalue_or_else (arg
, "invalid lvalue in unary %<&%>"))
2598 return error_mark_node
;
2600 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2601 argtype
= TREE_TYPE (arg
);
2603 /* If the lvalue is const or volatile, merge that into the type
2604 to which the address will point. Note that you can't get a
2605 restricted pointer by taking the address of something, so we
2606 only have to deal with `const' and `volatile' here. */
2607 if ((DECL_P (arg
) || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
2608 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
)))
2609 argtype
= c_build_type_variant (argtype
,
2610 TREE_READONLY (arg
),
2611 TREE_THIS_VOLATILE (arg
));
2613 if (!c_mark_addressable (arg
))
2614 return error_mark_node
;
2616 if (TREE_CODE (arg
) == COMPONENT_REF
2617 && DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
2619 error ("attempt to take address of bit-field structure member %qD",
2620 TREE_OPERAND (arg
, 1));
2621 return error_mark_node
;
2624 argtype
= build_pointer_type (argtype
);
2626 /* ??? Cope with user tricks that amount to offsetof. Delete this
2627 when we have proper support for integer constant expressions. */
2628 val
= get_base_address (arg
);
2629 if (val
&& TREE_CODE (val
) == INDIRECT_REF
2630 && integer_zerop (TREE_OPERAND (val
, 0)))
2631 return fold_convert (argtype
, fold_offsetof (arg
));
2633 val
= build1 (ADDR_EXPR
, argtype
, arg
);
2635 if (TREE_CODE (arg
) == COMPOUND_LITERAL_EXPR
)
2636 TREE_INVARIANT (val
) = TREE_CONSTANT (val
) = 1;
2645 argtype
= TREE_TYPE (arg
);
2646 val
= build1 (code
, argtype
, arg
);
2647 return require_constant_value
? fold_initializer (val
) : fold (val
);
2650 /* Return nonzero if REF is an lvalue valid for this language.
2651 Lvalues can be assigned, unless their type has TYPE_READONLY.
2652 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2657 enum tree_code code
= TREE_CODE (ref
);
2664 return lvalue_p (TREE_OPERAND (ref
, 0));
2666 case COMPOUND_LITERAL_EXPR
:
2676 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
2677 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
2680 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
2687 /* Return nonzero if REF is an lvalue valid for this language;
2688 otherwise, print an error message and return zero. */
2691 lvalue_or_else (tree ref
, const char *msgid
)
2693 int win
= lvalue_p (ref
);
2696 error ("%s", msgid
);
2702 /* Warn about storing in something that is `const'. */
2705 readonly_error (tree arg
, const char *msgid
)
2707 if (TREE_CODE (arg
) == COMPONENT_REF
)
2709 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
2710 readonly_error (TREE_OPERAND (arg
, 0), msgid
);
2712 error ("%s of read-only member %qs", _(msgid
),
2713 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg
, 1))));
2715 else if (TREE_CODE (arg
) == VAR_DECL
)
2716 error ("%s of read-only variable %qs", _(msgid
),
2717 IDENTIFIER_POINTER (DECL_NAME (arg
)));
2719 error ("%s of read-only location", _(msgid
));
2722 /* Mark EXP saying that we need to be able to take the
2723 address of it; it should not be allocated in a register.
2724 Returns true if successful. */
2727 c_mark_addressable (tree exp
)
2732 switch (TREE_CODE (x
))
2735 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
2737 error ("cannot take address of bit-field %qs",
2738 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x
, 1))));
2742 /* ... fall through ... */
2748 x
= TREE_OPERAND (x
, 0);
2751 case COMPOUND_LITERAL_EXPR
:
2753 TREE_ADDRESSABLE (x
) = 1;
2760 if (C_DECL_REGISTER (x
)
2761 && DECL_NONLOCAL (x
))
2763 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
2765 error ("global register variable %qs used in nested function",
2766 IDENTIFIER_POINTER (DECL_NAME (x
)));
2769 pedwarn ("register variable %qs used in nested function",
2770 IDENTIFIER_POINTER (DECL_NAME (x
)));
2772 else if (C_DECL_REGISTER (x
))
2774 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
2776 error ("address of global register variable %qs requested",
2777 IDENTIFIER_POINTER (DECL_NAME (x
)));
2781 pedwarn ("address of register variable %qs requested",
2782 IDENTIFIER_POINTER (DECL_NAME (x
)));
2787 TREE_ADDRESSABLE (x
) = 1;
2794 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2797 build_conditional_expr (tree ifexp
, tree op1
, tree op2
)
2801 enum tree_code code1
;
2802 enum tree_code code2
;
2803 tree result_type
= NULL
;
2804 tree orig_op1
= op1
, orig_op2
= op2
;
2806 ifexp
= lang_hooks
.truthvalue_conversion (default_conversion (ifexp
));
2808 /* Promote both alternatives. */
2810 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
2811 op1
= default_conversion (op1
);
2812 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
2813 op2
= default_conversion (op2
);
2815 if (TREE_CODE (ifexp
) == ERROR_MARK
2816 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
2817 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
2818 return error_mark_node
;
2820 type1
= TREE_TYPE (op1
);
2821 code1
= TREE_CODE (type1
);
2822 type2
= TREE_TYPE (op2
);
2823 code2
= TREE_CODE (type2
);
2825 /* C90 does not permit non-lvalue arrays in conditional expressions.
2826 In C99 they will be pointers by now. */
2827 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
2829 error ("non-lvalue array in conditional expression");
2830 return error_mark_node
;
2833 /* Quickly detect the usual case where op1 and op2 have the same type
2835 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
2838 result_type
= type1
;
2840 result_type
= TYPE_MAIN_VARIANT (type1
);
2842 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2843 || code1
== COMPLEX_TYPE
)
2844 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
2845 || code2
== COMPLEX_TYPE
))
2847 result_type
= common_type (type1
, type2
);
2849 /* If -Wsign-compare, warn here if type1 and type2 have
2850 different signedness. We'll promote the signed to unsigned
2851 and later code won't know it used to be different.
2852 Do this check on the original types, so that explicit casts
2853 will be considered, but default promotions won't. */
2854 if (warn_sign_compare
&& !skip_evaluation
)
2856 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
2857 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
2859 if (unsigned_op1
^ unsigned_op2
)
2861 /* Do not warn if the result type is signed, since the
2862 signed type will only be chosen if it can represent
2863 all the values of the unsigned type. */
2864 if (! TYPE_UNSIGNED (result_type
))
2866 /* Do not warn if the signed quantity is an unsuffixed
2867 integer literal (or some static constant expression
2868 involving such literals) and it is non-negative. */
2869 else if ((unsigned_op2
&& tree_expr_nonnegative_p (op1
))
2870 || (unsigned_op1
&& tree_expr_nonnegative_p (op2
)))
2873 warning ("signed and unsigned type in conditional expression");
2877 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
2879 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
2880 pedwarn ("ISO C forbids conditional expr with only one void side");
2881 result_type
= void_type_node
;
2883 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
2885 if (comp_target_types (type1
, type2
, 1))
2886 result_type
= common_pointer_type (type1
, type2
);
2887 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
2888 && TREE_CODE (orig_op1
) != NOP_EXPR
)
2889 result_type
= qualify_type (type2
, type1
);
2890 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
2891 && TREE_CODE (orig_op2
) != NOP_EXPR
)
2892 result_type
= qualify_type (type1
, type2
);
2893 else if (VOID_TYPE_P (TREE_TYPE (type1
)))
2895 if (pedantic
&& TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
2896 pedwarn ("ISO C forbids conditional expr between "
2897 "%<void *%> and function pointer");
2898 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
2899 TREE_TYPE (type2
)));
2901 else if (VOID_TYPE_P (TREE_TYPE (type2
)))
2903 if (pedantic
&& TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
2904 pedwarn ("ISO C forbids conditional expr between "
2905 "%<void *%> and function pointer");
2906 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
2907 TREE_TYPE (type1
)));
2911 pedwarn ("pointer type mismatch in conditional expression");
2912 result_type
= build_pointer_type (void_type_node
);
2915 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
2917 if (! integer_zerop (op2
))
2918 pedwarn ("pointer/integer type mismatch in conditional expression");
2921 op2
= null_pointer_node
;
2923 result_type
= type1
;
2925 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2927 if (!integer_zerop (op1
))
2928 pedwarn ("pointer/integer type mismatch in conditional expression");
2931 op1
= null_pointer_node
;
2933 result_type
= type2
;
2938 if (flag_cond_mismatch
)
2939 result_type
= void_type_node
;
2942 error ("type mismatch in conditional expression");
2943 return error_mark_node
;
2947 /* Merge const and volatile flags of the incoming types. */
2949 = build_type_variant (result_type
,
2950 TREE_READONLY (op1
) || TREE_READONLY (op2
),
2951 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
2953 if (result_type
!= TREE_TYPE (op1
))
2954 op1
= convert_and_check (result_type
, op1
);
2955 if (result_type
!= TREE_TYPE (op2
))
2956 op2
= convert_and_check (result_type
, op2
);
2958 if (TREE_CODE (ifexp
) == INTEGER_CST
)
2959 return non_lvalue (integer_zerop (ifexp
) ? op2
: op1
);
2961 return fold (build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
));
2964 /* Return a compound expression that performs two expressions and
2965 returns the value of the second of them. */
2968 build_compound_expr (tree expr1
, tree expr2
)
2970 /* Convert arrays and functions to pointers. */
2971 expr2
= default_function_array_conversion (expr2
);
2973 /* Don't let (0, 0) be null pointer constant. */
2974 if (integer_zerop (expr2
))
2975 expr2
= non_lvalue (expr2
);
2977 if (! TREE_SIDE_EFFECTS (expr1
))
2979 /* The left-hand operand of a comma expression is like an expression
2980 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2981 any side-effects, unless it was explicitly cast to (void). */
2982 if (warn_unused_value
2983 && ! (TREE_CODE (expr1
) == CONVERT_EXPR
2984 && VOID_TYPE_P (TREE_TYPE (expr1
))))
2985 warning ("left-hand operand of comma expression has no effect");
2988 /* With -Wunused, we should also warn if the left-hand operand does have
2989 side-effects, but computes a value which is not used. For example, in
2990 `foo() + bar(), baz()' the result of the `+' operator is not used,
2991 so we should issue a warning. */
2992 else if (warn_unused_value
)
2993 warn_if_unused_value (expr1
, input_location
);
2995 return build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
2998 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3001 build_c_cast (tree type
, tree expr
)
3005 if (type
== error_mark_node
|| expr
== error_mark_node
)
3006 return error_mark_node
;
3008 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3009 only in <protocol> qualifications. But when constructing cast expressions,
3010 the protocols do matter and must be kept around. */
3011 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
3012 return build1 (NOP_EXPR
, type
, expr
);
3014 type
= TYPE_MAIN_VARIANT (type
);
3016 if (TREE_CODE (type
) == ARRAY_TYPE
)
3018 error ("cast specifies array type");
3019 return error_mark_node
;
3022 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3024 error ("cast specifies function type");
3025 return error_mark_node
;
3028 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
3032 if (TREE_CODE (type
) == RECORD_TYPE
3033 || TREE_CODE (type
) == UNION_TYPE
)
3034 pedwarn ("ISO C forbids casting nonscalar to the same type");
3037 else if (TREE_CODE (type
) == UNION_TYPE
)
3040 value
= default_function_array_conversion (value
);
3042 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3043 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
3044 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
3052 pedwarn ("ISO C forbids casts to union type");
3053 t
= digest_init (type
,
3054 build_constructor (type
,
3055 build_tree_list (field
, value
)),
3057 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
3058 TREE_INVARIANT (t
) = TREE_INVARIANT (value
);
3061 error ("cast to union type from type not present in union");
3062 return error_mark_node
;
3068 /* If casting to void, avoid the error that would come
3069 from default_conversion in the case of a non-lvalue array. */
3070 if (type
== void_type_node
)
3071 return build1 (CONVERT_EXPR
, type
, value
);
3073 /* Convert functions and arrays to pointers,
3074 but don't convert any other types. */
3075 value
= default_function_array_conversion (value
);
3076 otype
= TREE_TYPE (value
);
3078 /* Optionally warn about potentially worrisome casts. */
3081 && TREE_CODE (type
) == POINTER_TYPE
3082 && TREE_CODE (otype
) == POINTER_TYPE
)
3084 tree in_type
= type
;
3085 tree in_otype
= otype
;
3089 /* Check that the qualifiers on IN_TYPE are a superset of
3090 the qualifiers of IN_OTYPE. The outermost level of
3091 POINTER_TYPE nodes is uninteresting and we stop as soon
3092 as we hit a non-POINTER_TYPE node on either type. */
3095 in_otype
= TREE_TYPE (in_otype
);
3096 in_type
= TREE_TYPE (in_type
);
3098 /* GNU C allows cv-qualified function types. 'const'
3099 means the function is very pure, 'volatile' means it
3100 can't return. We need to warn when such qualifiers
3101 are added, not when they're taken away. */
3102 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
3103 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
3104 added
|= (TYPE_QUALS (in_type
) & ~TYPE_QUALS (in_otype
));
3106 discarded
|= (TYPE_QUALS (in_otype
) & ~TYPE_QUALS (in_type
));
3108 while (TREE_CODE (in_type
) == POINTER_TYPE
3109 && TREE_CODE (in_otype
) == POINTER_TYPE
);
3112 warning ("cast adds new qualifiers to function type");
3115 /* There are qualifiers present in IN_OTYPE that are not
3116 present in IN_TYPE. */
3117 warning ("cast discards qualifiers from pointer target type");
3120 /* Warn about possible alignment problems. */
3121 if (STRICT_ALIGNMENT
&& warn_cast_align
3122 && TREE_CODE (type
) == POINTER_TYPE
3123 && TREE_CODE (otype
) == POINTER_TYPE
3124 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
3125 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3126 /* Don't warn about opaque types, where the actual alignment
3127 restriction is unknown. */
3128 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
3129 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
3130 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
3131 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
3132 warning ("cast increases required alignment of target type");
3134 if (TREE_CODE (type
) == INTEGER_TYPE
3135 && TREE_CODE (otype
) == POINTER_TYPE
3136 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3137 && !TREE_CONSTANT (value
))
3138 warning ("cast from pointer to integer of different size");
3140 if (warn_bad_function_cast
3141 && TREE_CODE (value
) == CALL_EXPR
3142 && TREE_CODE (type
) != TREE_CODE (otype
))
3143 warning ("cast does not match function type");
3145 if (TREE_CODE (type
) == POINTER_TYPE
3146 && TREE_CODE (otype
) == INTEGER_TYPE
3147 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3148 /* Don't warn about converting any constant. */
3149 && !TREE_CONSTANT (value
))
3150 warning ("cast to pointer from integer of different size");
3152 if (TREE_CODE (type
) == POINTER_TYPE
3153 && TREE_CODE (otype
) == POINTER_TYPE
3154 && TREE_CODE (expr
) == ADDR_EXPR
3155 && DECL_P (TREE_OPERAND (expr
, 0))
3156 && flag_strict_aliasing
&& warn_strict_aliasing
3157 && !VOID_TYPE_P (TREE_TYPE (type
)))
3159 /* Casting the address of a decl to non void pointer. Warn
3160 if the cast breaks type based aliasing. */
3161 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)))
3162 warning ("type-punning to incomplete type might break strict-aliasing rules");
3165 HOST_WIDE_INT set1
= get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
3166 HOST_WIDE_INT set2
= get_alias_set (TREE_TYPE (type
));
3168 if (!alias_sets_conflict_p (set1
, set2
))
3169 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3170 else if (warn_strict_aliasing
> 1
3171 && !alias_sets_might_conflict_p (set1
, set2
))
3172 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3176 /* If pedantic, warn for conversions between function and object
3177 pointer types, except for converting a null pointer constant
3178 to function pointer type. */
3180 && TREE_CODE (type
) == POINTER_TYPE
3181 && TREE_CODE (otype
) == POINTER_TYPE
3182 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
3183 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
3184 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3187 && TREE_CODE (type
) == POINTER_TYPE
3188 && TREE_CODE (otype
) == POINTER_TYPE
3189 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
3190 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3191 && !(integer_zerop (value
) && TREE_TYPE (otype
) == void_type_node
3192 && TREE_CODE (expr
) != NOP_EXPR
))
3193 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3196 /* Replace a nonvolatile const static variable with its value. */
3197 if (optimize
&& TREE_CODE (value
) == VAR_DECL
)
3198 value
= decl_constant_value (value
);
3199 value
= convert (type
, value
);
3201 /* Ignore any integer overflow caused by the cast. */
3202 if (TREE_CODE (value
) == INTEGER_CST
)
3204 if (EXPR_P (ovalue
))
3205 /* If OVALUE had overflow set, then so will VALUE, so it
3206 is safe to overwrite. */
3207 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
3209 TREE_OVERFLOW (value
) = 0;
3211 if (TREE_CODE_CLASS (TREE_CODE (ovalue
)) == 'c')
3212 /* Similarly, constant_overflow cannot have become
3214 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
3218 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3219 if (TREE_CODE (value
) == INTEGER_CST
3220 && TREE_CODE (expr
) == INTEGER_CST
3221 && TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
)
3222 value
= non_lvalue (value
);
3224 /* Don't let a cast be an lvalue. */
3226 value
= non_lvalue (value
);
3231 /* Interpret a cast of expression EXPR to type TYPE. */
3233 c_cast_expr (struct c_type_name
*type_name
, tree expr
)
3236 int saved_wsp
= warn_strict_prototypes
;
3238 /* This avoids warnings about unprototyped casts on
3239 integers. E.g. "#define SIG_DFL (void(*)())0". */
3240 if (TREE_CODE (expr
) == INTEGER_CST
)
3241 warn_strict_prototypes
= 0;
3242 type
= groktypename (type_name
);
3243 warn_strict_prototypes
= saved_wsp
;
3245 return build_c_cast (type
, expr
);
3249 /* Build an assignment expression of lvalue LHS from value RHS.
3250 MODIFYCODE is the code for a binary operator that we use
3251 to combine the old value of LHS with RHS to get the new value.
3252 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3255 build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
)
3259 tree lhstype
= TREE_TYPE (lhs
);
3260 tree olhstype
= lhstype
;
3262 /* Types that aren't fully specified cannot be used in assignments. */
3263 lhs
= require_complete_type (lhs
);
3265 /* Avoid duplicate error messages from operands that had errors. */
3266 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
3267 return error_mark_node
;
3269 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3270 /* Do not use STRIP_NOPS here. We do not want an enumerator
3271 whose value is 0 to count as a null pointer constant. */
3272 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3273 rhs
= TREE_OPERAND (rhs
, 0);
3277 /* If a binary op has been requested, combine the old LHS value with the RHS
3278 producing the value we should actually store into the LHS. */
3280 if (modifycode
!= NOP_EXPR
)
3282 lhs
= stabilize_reference (lhs
);
3283 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
3286 if (!lvalue_or_else (lhs
, "invalid lvalue in assignment"))
3287 return error_mark_node
;
3289 /* Warn about storing in something that is `const'. */
3291 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
3292 || ((TREE_CODE (lhstype
) == RECORD_TYPE
3293 || TREE_CODE (lhstype
) == UNION_TYPE
)
3294 && C_TYPE_FIELDS_READONLY (lhstype
)))
3295 readonly_error (lhs
, "assignment");
3297 /* If storing into a structure or union member,
3298 it has probably been given type `int'.
3299 Compute the type that would go with
3300 the actual amount of storage the member occupies. */
3302 if (TREE_CODE (lhs
) == COMPONENT_REF
3303 && (TREE_CODE (lhstype
) == INTEGER_TYPE
3304 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
3305 || TREE_CODE (lhstype
) == REAL_TYPE
3306 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
3307 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
3309 /* If storing in a field that is in actuality a short or narrower than one,
3310 we must store in the field in its actual type. */
3312 if (lhstype
!= TREE_TYPE (lhs
))
3314 lhs
= copy_node (lhs
);
3315 TREE_TYPE (lhs
) = lhstype
;
3318 /* Convert new value to destination type. */
3320 newrhs
= convert_for_assignment (lhstype
, newrhs
, _("assignment"),
3321 NULL_TREE
, NULL_TREE
, 0);
3322 if (TREE_CODE (newrhs
) == ERROR_MARK
)
3323 return error_mark_node
;
3327 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
3328 TREE_SIDE_EFFECTS (result
) = 1;
3330 /* If we got the LHS in a different type for storing in,
3331 convert the result back to the nominal type of LHS
3332 so that the value we return always has the same type
3333 as the LHS argument. */
3335 if (olhstype
== TREE_TYPE (result
))
3337 return convert_for_assignment (olhstype
, result
, _("assignment"),
3338 NULL_TREE
, NULL_TREE
, 0);
3341 /* Convert value RHS to type TYPE as preparation for an assignment
3342 to an lvalue of type TYPE.
3343 The real work of conversion is done by `convert'.
3344 The purpose of this function is to generate error messages
3345 for assignments that are not allowed in C.
3346 ERRTYPE is a string to use in error messages:
3347 "assignment", "return", etc. If it is null, this is parameter passing
3348 for a function call (and different error messages are output).
3350 FUNNAME is the name of the function being called,
3351 as an IDENTIFIER_NODE, or null.
3352 PARMNUM is the number of the argument, for printing in error messages. */
3355 convert_for_assignment (tree type
, tree rhs
, const char *errtype
,
3356 tree fundecl
, tree funname
, int parmnum
)
3358 enum tree_code codel
= TREE_CODE (type
);
3360 enum tree_code coder
;
3362 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3363 /* Do not use STRIP_NOPS here. We do not want an enumerator
3364 whose value is 0 to count as a null pointer constant. */
3365 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3366 rhs
= TREE_OPERAND (rhs
, 0);
3368 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
3369 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
)
3370 rhs
= default_conversion (rhs
);
3371 else if (optimize
&& TREE_CODE (rhs
) == VAR_DECL
)
3372 rhs
= decl_constant_value_for_broken_optimization (rhs
);
3374 rhstype
= TREE_TYPE (rhs
);
3375 coder
= TREE_CODE (rhstype
);
3377 if (coder
== ERROR_MARK
)
3378 return error_mark_node
;
3380 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
3382 overflow_warning (rhs
);
3383 /* Check for Objective-C protocols. This will automatically
3384 issue a warning if there are protocol violations. No need to
3385 use the return value. */
3386 if (c_dialect_objc ())
3387 objc_comptypes (type
, rhstype
, 0);
3391 if (coder
== VOID_TYPE
)
3393 error ("void value not ignored as it ought to be");
3394 return error_mark_node
;
3396 /* A type converts to a reference to it.
3397 This code doesn't fully support references, it's just for the
3398 special case of va_start and va_copy. */
3399 if (codel
== REFERENCE_TYPE
3400 && comptypes (TREE_TYPE (type
), TREE_TYPE (rhs
)) == 1)
3402 if (!lvalue_p (rhs
))
3404 error ("cannot pass rvalue to reference parameter");
3405 return error_mark_node
;
3407 if (!c_mark_addressable (rhs
))
3408 return error_mark_node
;
3409 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
3411 /* We already know that these two types are compatible, but they
3412 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3413 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3414 likely to be va_list, a typedef to __builtin_va_list, which
3415 is different enough that it will cause problems later. */
3416 if (TREE_TYPE (TREE_TYPE (rhs
)) != TREE_TYPE (type
))
3417 rhs
= build1 (NOP_EXPR
, build_pointer_type (TREE_TYPE (type
)), rhs
);
3419 rhs
= build1 (NOP_EXPR
, type
, rhs
);
3422 /* Some types can interconvert without explicit casts. */
3423 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
3424 && vector_types_convertible_p (type
, TREE_TYPE (rhs
)))
3425 return convert (type
, rhs
);
3426 /* Arithmetic types all interconvert, and enum is treated like int. */
3427 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
3428 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
3429 || codel
== BOOLEAN_TYPE
)
3430 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
3431 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
3432 || coder
== BOOLEAN_TYPE
))
3433 return convert_and_check (type
, rhs
);
3435 /* Conversion to a transparent union from its member types.
3436 This applies only to function arguments. */
3437 else if (codel
== UNION_TYPE
&& TYPE_TRANSPARENT_UNION (type
) && ! errtype
)
3440 tree marginal_memb_type
= 0;
3442 for (memb_types
= TYPE_FIELDS (type
); memb_types
;
3443 memb_types
= TREE_CHAIN (memb_types
))
3445 tree memb_type
= TREE_TYPE (memb_types
);
3447 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
3448 TYPE_MAIN_VARIANT (rhstype
)))
3451 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
3454 if (coder
== POINTER_TYPE
)
3456 tree ttl
= TREE_TYPE (memb_type
);
3457 tree ttr
= TREE_TYPE (rhstype
);
3459 /* Any non-function converts to a [const][volatile] void *
3460 and vice versa; otherwise, targets must be the same.
3461 Meanwhile, the lhs target must have all the qualifiers of
3463 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3464 || comp_target_types (memb_type
, rhstype
, 0))
3466 /* If this type won't generate any warnings, use it. */
3467 if (TYPE_QUALS (ttl
) == TYPE_QUALS (ttr
)
3468 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
3469 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3470 ? ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3471 == TYPE_QUALS (ttr
))
3472 : ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3473 == TYPE_QUALS (ttl
))))
3476 /* Keep looking for a better type, but remember this one. */
3477 if (! marginal_memb_type
)
3478 marginal_memb_type
= memb_type
;
3482 /* Can convert integer zero to any pointer type. */
3483 if (integer_zerop (rhs
)
3484 || (TREE_CODE (rhs
) == NOP_EXPR
3485 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3487 rhs
= null_pointer_node
;
3492 if (memb_types
|| marginal_memb_type
)
3496 /* We have only a marginally acceptable member type;
3497 it needs a warning. */
3498 tree ttl
= TREE_TYPE (marginal_memb_type
);
3499 tree ttr
= TREE_TYPE (rhstype
);
3501 /* Const and volatile mean something different for function
3502 types, so the usual warnings are not appropriate. */
3503 if (TREE_CODE (ttr
) == FUNCTION_TYPE
3504 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3506 /* Because const and volatile on functions are
3507 restrictions that say the function will not do
3508 certain things, it is okay to use a const or volatile
3509 function where an ordinary one is wanted, but not
3511 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3512 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3513 errtype
, funname
, parmnum
);
3515 else if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3516 warn_for_assignment ("%s discards qualifiers from pointer target type",
3521 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (fundecl
))
3522 pedwarn ("ISO C prohibits argument conversion to union type");
3524 return build1 (NOP_EXPR
, type
, rhs
);
3528 /* Conversions among pointers */
3529 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
3530 && (coder
== codel
))
3532 tree ttl
= TREE_TYPE (type
);
3533 tree ttr
= TREE_TYPE (rhstype
);
3534 bool is_opaque_pointer
;
3535 int target_cmp
= 0; /* Cache comp_target_types () result. */
3537 /* Opaque pointers are treated like void pointers. */
3538 is_opaque_pointer
= (targetm
.vector_opaque_p (type
)
3539 || targetm
.vector_opaque_p (rhstype
))
3540 && TREE_CODE (ttl
) == VECTOR_TYPE
3541 && TREE_CODE (ttr
) == VECTOR_TYPE
;
3543 /* Any non-function converts to a [const][volatile] void *
3544 and vice versa; otherwise, targets must be the same.
3545 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3546 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3547 || (target_cmp
= comp_target_types (type
, rhstype
, 0))
3548 || is_opaque_pointer
3549 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl
))
3550 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
3553 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3556 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3557 which are not ANSI null ptr constants. */
3558 && (!integer_zerop (rhs
) || TREE_CODE (rhs
) == NOP_EXPR
)
3559 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
3560 warn_for_assignment ("ISO C forbids %s between function "
3561 "pointer and %<void *%>",
3562 errtype
, funname
, parmnum
);
3563 /* Const and volatile mean something different for function types,
3564 so the usual warnings are not appropriate. */
3565 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
3566 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
3568 if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3569 warn_for_assignment ("%s discards qualifiers from pointer target type",
3570 errtype
, funname
, parmnum
);
3571 /* If this is not a case of ignoring a mismatch in signedness,
3573 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3576 /* If there is a mismatch, do warn. */
3578 warn_for_assignment ("pointer targets in %s differ in signedness",
3579 errtype
, funname
, parmnum
);
3581 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
3582 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3584 /* Because const and volatile on functions are restrictions
3585 that say the function will not do certain things,
3586 it is okay to use a const or volatile function
3587 where an ordinary one is wanted, but not vice-versa. */
3588 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3589 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3590 errtype
, funname
, parmnum
);
3594 warn_for_assignment ("%s from incompatible pointer type",
3595 errtype
, funname
, parmnum
);
3596 return convert (type
, rhs
);
3598 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
3600 error ("invalid use of non-lvalue array");
3601 return error_mark_node
;
3603 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
3605 /* An explicit constant 0 can convert to a pointer,
3606 or one that results from arithmetic, even including
3607 a cast to integer type. */
3608 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
))
3610 ! (TREE_CODE (rhs
) == NOP_EXPR
3611 && TREE_CODE (TREE_TYPE (rhs
)) == INTEGER_TYPE
3612 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == INTEGER_CST
3613 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3614 warn_for_assignment ("%s makes pointer from integer without a cast",
3615 errtype
, funname
, parmnum
);
3617 return convert (type
, rhs
);
3619 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
3621 warn_for_assignment ("%s makes integer from pointer without a cast",
3622 errtype
, funname
, parmnum
);
3623 return convert (type
, rhs
);
3625 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
3626 return convert (type
, rhs
);
3632 tree selector
= objc_message_selector ();
3634 if (selector
&& parmnum
> 2)
3635 error ("incompatible type for argument %d of %qs",
3636 parmnum
- 2, IDENTIFIER_POINTER (selector
));
3638 error ("incompatible type for argument %d of %qs",
3639 parmnum
, IDENTIFIER_POINTER (funname
));
3642 error ("incompatible type for argument %d of indirect function call",
3646 error ("incompatible types in %s", errtype
);
3648 return error_mark_node
;
3651 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3652 is used for error and waring reporting and indicates which argument
3653 is being processed. */
3656 c_convert_parm_for_inlining (tree parm
, tree value
, tree fn
, int argnum
)
3660 /* If FN was prototyped, the value has been converted already
3661 in convert_arguments. */
3662 if (! value
|| TYPE_ARG_TYPES (TREE_TYPE (fn
)))
3665 type
= TREE_TYPE (parm
);
3666 ret
= convert_for_assignment (type
, value
,
3667 (char *) 0 /* arg passing */, fn
,
3668 DECL_NAME (fn
), argnum
);
3669 if (targetm
.calls
.promote_prototypes (TREE_TYPE (fn
))
3670 && INTEGRAL_TYPE_P (type
)
3671 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3672 ret
= default_conversion (ret
);
3676 /* Print a warning using MSGID.
3677 It gets OPNAME as its one parameter.
3678 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3679 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3680 FUNCTION and ARGNUM are handled specially if we are building an
3681 Objective-C selector. */
3684 warn_for_assignment (const char *msgid
, const char *opname
, tree function
,
3689 tree selector
= objc_message_selector ();
3692 if (selector
&& argnum
> 2)
3694 function
= selector
;
3701 /* Function name is known; supply it. */
3702 const char *const argstring
= _("passing arg of '%s'");
3703 new_opname
= (char *) alloca (IDENTIFIER_LENGTH (function
)
3704 + strlen (argstring
) + 1 + 1);
3705 sprintf (new_opname
, argstring
,
3706 IDENTIFIER_POINTER (function
));
3710 /* Function name unknown (call through ptr). */
3711 const char *const argnofun
= _("passing arg of pointer to function");
3712 new_opname
= (char *) alloca (strlen (argnofun
) + 1 + 1);
3713 sprintf (new_opname
, argnofun
);
3718 /* Function name is known; supply it. */
3719 const char *const argstring
= _("passing arg %d of '%s'");
3720 new_opname
= (char *) alloca (IDENTIFIER_LENGTH (function
)
3721 + strlen (argstring
) + 1 + 25 /*%d*/ + 1);
3722 sprintf (new_opname
, argstring
, argnum
,
3723 IDENTIFIER_POINTER (function
));
3727 /* Function name unknown (call through ptr); just give arg number. */
3728 const char *const argnofun
= _("passing arg %d of pointer to function");
3729 new_opname
= (char *) alloca (strlen (argnofun
) + 1 + 25 /*%d*/ + 1);
3730 sprintf (new_opname
, argnofun
, argnum
);
3732 opname
= new_opname
;
3734 pedwarn (msgid
, opname
);
3737 /* If VALUE is a compound expr all of whose expressions are constant, then
3738 return its value. Otherwise, return error_mark_node.
3740 This is for handling COMPOUND_EXPRs as initializer elements
3741 which is allowed with a warning when -pedantic is specified. */
3744 valid_compound_expr_initializer (tree value
, tree endtype
)
3746 if (TREE_CODE (value
) == COMPOUND_EXPR
)
3748 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
3750 return error_mark_node
;
3751 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
3754 else if (!initializer_constant_valid_p (value
, endtype
))
3755 return error_mark_node
;
3760 /* Perform appropriate conversions on the initial value of a variable,
3761 store it in the declaration DECL,
3762 and print any error messages that are appropriate.
3763 If the init is invalid, store an ERROR_MARK. */
3766 store_init_value (tree decl
, tree init
)
3770 /* If variable's type was invalidly declared, just ignore it. */
3772 type
= TREE_TYPE (decl
);
3773 if (TREE_CODE (type
) == ERROR_MARK
)
3776 /* Digest the specified initializer into an expression. */
3778 value
= digest_init (type
, init
, true, TREE_STATIC (decl
));
3780 /* Store the expression if valid; else report error. */
3782 if (warn_traditional
&& !in_system_header
3783 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && ! TREE_STATIC (decl
))
3784 warning ("traditional C rejects automatic aggregate initialization");
3786 DECL_INITIAL (decl
) = value
;
3788 /* ANSI wants warnings about out-of-range constant initializers. */
3789 STRIP_TYPE_NOPS (value
);
3790 constant_expression_warning (value
);
3792 /* Check if we need to set array size from compound literal size. */
3793 if (TREE_CODE (type
) == ARRAY_TYPE
3794 && TYPE_DOMAIN (type
) == 0
3795 && value
!= error_mark_node
)
3797 tree inside_init
= init
;
3799 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
3800 inside_init
= TREE_OPERAND (init
, 0);
3801 inside_init
= fold (inside_init
);
3803 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
3805 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
3807 if (TYPE_DOMAIN (TREE_TYPE (decl
)))
3809 /* For int foo[] = (int [3]){1}; we need to set array size
3810 now since later on array initializer will be just the
3811 brace enclosed list of the compound literal. */
3812 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (decl
));
3814 layout_decl (decl
, 0);
3820 /* Methods for storing and printing names for error messages. */
3822 /* Implement a spelling stack that allows components of a name to be pushed
3823 and popped. Each element on the stack is this structure. */
3835 #define SPELLING_STRING 1
3836 #define SPELLING_MEMBER 2
3837 #define SPELLING_BOUNDS 3
3839 static struct spelling
*spelling
; /* Next stack element (unused). */
3840 static struct spelling
*spelling_base
; /* Spelling stack base. */
3841 static int spelling_size
; /* Size of the spelling stack. */
3843 /* Macros to save and restore the spelling stack around push_... functions.
3844 Alternative to SAVE_SPELLING_STACK. */
3846 #define SPELLING_DEPTH() (spelling - spelling_base)
3847 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3849 /* Push an element on the spelling stack with type KIND and assign VALUE
3852 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3854 int depth = SPELLING_DEPTH (); \
3856 if (depth >= spelling_size) \
3858 spelling_size += 10; \
3859 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3861 RESTORE_SPELLING_DEPTH (depth); \
3864 spelling->kind = (KIND); \
3865 spelling->MEMBER = (VALUE); \
3869 /* Push STRING on the stack. Printed literally. */
3872 push_string (const char *string
)
3874 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
3877 /* Push a member name on the stack. Printed as '.' STRING. */
3880 push_member_name (tree decl
)
3882 const char *const string
3883 = DECL_NAME (decl
) ? IDENTIFIER_POINTER (DECL_NAME (decl
)) : "<anonymous>";
3884 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
3887 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3890 push_array_bounds (int bounds
)
3892 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
3895 /* Compute the maximum size in bytes of the printed spelling. */
3898 spelling_length (void)
3903 for (p
= spelling_base
; p
< spelling
; p
++)
3905 if (p
->kind
== SPELLING_BOUNDS
)
3908 size
+= strlen (p
->u
.s
) + 1;
3914 /* Print the spelling to BUFFER and return it. */
3917 print_spelling (char *buffer
)
3922 for (p
= spelling_base
; p
< spelling
; p
++)
3923 if (p
->kind
== SPELLING_BOUNDS
)
3925 sprintf (d
, "[%d]", p
->u
.i
);
3931 if (p
->kind
== SPELLING_MEMBER
)
3933 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
3940 /* Issue an error message for a bad initializer component.
3941 MSGID identifies the message.
3942 The component name is taken from the spelling stack. */
3945 error_init (const char *msgid
)
3949 error ("%s", _(msgid
));
3950 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
3952 error ("(near initialization for %qs)", ofwhat
);
3955 /* Issue a pedantic warning for a bad initializer component.
3956 MSGID identifies the message.
3957 The component name is taken from the spelling stack. */
3960 pedwarn_init (const char *msgid
)
3964 pedwarn ("%s", _(msgid
));
3965 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
3967 pedwarn ("(near initialization for %qs)", ofwhat
);
3970 /* Issue a warning for a bad initializer component.
3971 MSGID identifies the message.
3972 The component name is taken from the spelling stack. */
3975 warning_init (const char *msgid
)
3979 warning ("%s", _(msgid
));
3980 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
3982 warning ("(near initialization for %qs)", ofwhat
);
3985 /* If TYPE is an array type and EXPR is a parenthesized string
3986 constant, warn if pedantic that EXPR is being used to initialize an
3987 object of type TYPE. */
3990 maybe_warn_string_init (tree type
, struct c_expr expr
)
3993 && TREE_CODE (type
) == ARRAY_TYPE
3994 && TREE_CODE (expr
.value
) == STRING_CST
3995 && expr
.original_code
!= STRING_CST
)
3996 pedwarn_init ("array initialized from parenthesized string constant");
3999 /* Digest the parser output INIT as an initializer for type TYPE.
4000 Return a C expression of type TYPE to represent the initial value.
4002 If INIT is a string constant, STRICT_STRING is true if it is
4003 unparenthesized or we should not warn here for it being parenthesized.
4004 For other types of INIT, STRICT_STRING is not used.
4006 REQUIRE_CONSTANT requests an error if non-constant initializers or
4007 elements are seen. */
4010 digest_init (tree type
, tree init
, bool strict_string
, int require_constant
)
4012 enum tree_code code
= TREE_CODE (type
);
4013 tree inside_init
= init
;
4015 if (type
== error_mark_node
4016 || init
== error_mark_node
4017 || TREE_TYPE (init
) == error_mark_node
)
4018 return error_mark_node
;
4020 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4021 /* Do not use STRIP_NOPS here. We do not want an enumerator
4022 whose value is 0 to count as a null pointer constant. */
4023 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
4024 inside_init
= TREE_OPERAND (init
, 0);
4026 inside_init
= fold (inside_init
);
4028 /* Initialization of an array of chars from a string constant
4029 optionally enclosed in braces. */
4031 if (code
== ARRAY_TYPE
&& inside_init
4032 && TREE_CODE (inside_init
) == STRING_CST
)
4034 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
4035 /* Note that an array could be both an array of character type
4036 and an array of wchar_t if wchar_t is signed char or unsigned
4038 bool char_array
= (typ1
== char_type_node
4039 || typ1
== signed_char_type_node
4040 || typ1
== unsigned_char_type_node
);
4041 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
4042 if (char_array
|| wchar_array
)
4046 expr
.value
= inside_init
;
4047 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
4048 maybe_warn_string_init (type
, expr
);
4051 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)))
4054 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4055 TYPE_MAIN_VARIANT (type
)))
4058 if (!wchar_array
&& !char_string
)
4060 error_init ("char-array initialized from wide string");
4061 return error_mark_node
;
4063 if (char_string
&& !char_array
)
4065 error_init ("wchar_t-array initialized from non-wide string");
4066 return error_mark_node
;
4069 TREE_TYPE (inside_init
) = type
;
4070 if (TYPE_DOMAIN (type
) != 0
4071 && TYPE_SIZE (type
) != 0
4072 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
4073 /* Subtract 1 (or sizeof (wchar_t))
4074 because it's ok to ignore the terminating null char
4075 that is counted in the length of the constant. */
4076 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
4077 TREE_STRING_LENGTH (inside_init
)
4078 - ((TYPE_PRECISION (typ1
)
4079 != TYPE_PRECISION (char_type_node
))
4080 ? (TYPE_PRECISION (wchar_type_node
)
4083 pedwarn_init ("initializer-string for array of chars is too long");
4087 else if (INTEGRAL_TYPE_P (typ1
))
4089 error_init ("array of inappropriate type initialized "
4090 "from string constant");
4091 return error_mark_node
;
4095 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4096 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4097 below and handle as a constructor. */
4098 if (code
== VECTOR_TYPE
4099 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
4100 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
)
4101 && TREE_CONSTANT (inside_init
))
4103 if (TREE_CODE (inside_init
) == VECTOR_CST
4104 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4105 TYPE_MAIN_VARIANT (type
)))
4108 return build_vector (type
, CONSTRUCTOR_ELTS (inside_init
));
4111 /* Any type can be initialized
4112 from an expression of the same type, optionally with braces. */
4114 if (inside_init
&& TREE_TYPE (inside_init
) != 0
4115 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4116 TYPE_MAIN_VARIANT (type
))
4117 || (code
== ARRAY_TYPE
4118 && comptypes (TREE_TYPE (inside_init
), type
))
4119 || (code
== VECTOR_TYPE
4120 && comptypes (TREE_TYPE (inside_init
), type
))
4121 || (code
== POINTER_TYPE
4122 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4123 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
4125 || (code
== POINTER_TYPE
4126 && TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
4127 && comptypes (TREE_TYPE (inside_init
),
4128 TREE_TYPE (type
)))))
4130 if (code
== POINTER_TYPE
)
4132 inside_init
= default_function_array_conversion (inside_init
);
4134 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
4136 error_init ("invalid use of non-lvalue array");
4137 return error_mark_node
;
4141 if (code
== VECTOR_TYPE
)
4142 /* Although the types are compatible, we may require a
4144 inside_init
= convert (type
, inside_init
);
4146 if (require_constant
&& !flag_isoc99
4147 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
4149 /* As an extension, allow initializing objects with static storage
4150 duration with compound literals (which are then treated just as
4151 the brace enclosed list they contain). */
4152 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
4153 inside_init
= DECL_INITIAL (decl
);
4156 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
4157 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
4159 error_init ("array initialized from non-constant array expression");
4160 return error_mark_node
;
4163 if (optimize
&& TREE_CODE (inside_init
) == VAR_DECL
)
4164 inside_init
= decl_constant_value_for_broken_optimization (inside_init
);
4166 /* Compound expressions can only occur here if -pedantic or
4167 -pedantic-errors is specified. In the later case, we always want
4168 an error. In the former case, we simply want a warning. */
4169 if (require_constant
&& pedantic
4170 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
4173 = valid_compound_expr_initializer (inside_init
,
4174 TREE_TYPE (inside_init
));
4175 if (inside_init
== error_mark_node
)
4176 error_init ("initializer element is not constant");
4178 pedwarn_init ("initializer element is not constant");
4179 if (flag_pedantic_errors
)
4180 inside_init
= error_mark_node
;
4182 else if (require_constant
4183 && !initializer_constant_valid_p (inside_init
,
4184 TREE_TYPE (inside_init
)))
4186 error_init ("initializer element is not constant");
4187 inside_init
= error_mark_node
;
4193 /* Handle scalar types, including conversions. */
4195 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
4196 || code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
|| code
== COMPLEX_TYPE
4197 || code
== VECTOR_TYPE
)
4199 /* Note that convert_for_assignment calls default_conversion
4200 for arrays and functions. We must not call it in the
4201 case where inside_init is a null pointer constant. */
4203 = convert_for_assignment (type
, init
, _("initialization"),
4204 NULL_TREE
, NULL_TREE
, 0);
4206 /* Check to see if we have already given an error message. */
4207 if (inside_init
== error_mark_node
)
4209 else if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4211 error_init ("initializer element is not constant");
4212 inside_init
= error_mark_node
;
4214 else if (require_constant
4215 && !initializer_constant_valid_p (inside_init
,
4216 TREE_TYPE (inside_init
)))
4218 error_init ("initializer element is not computable at load time");
4219 inside_init
= error_mark_node
;
4225 /* Come here only for records and arrays. */
4227 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
4229 error_init ("variable-sized object may not be initialized");
4230 return error_mark_node
;
4233 error_init ("invalid initializer");
4234 return error_mark_node
;
4237 /* Handle initializers that use braces. */
4239 /* Type of object we are accumulating a constructor for.
4240 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4241 static tree constructor_type
;
4243 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4245 static tree constructor_fields
;
4247 /* For an ARRAY_TYPE, this is the specified index
4248 at which to store the next element we get. */
4249 static tree constructor_index
;
4251 /* For an ARRAY_TYPE, this is the maximum index. */
4252 static tree constructor_max_index
;
4254 /* For a RECORD_TYPE, this is the first field not yet written out. */
4255 static tree constructor_unfilled_fields
;
4257 /* For an ARRAY_TYPE, this is the index of the first element
4258 not yet written out. */
4259 static tree constructor_unfilled_index
;
4261 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4262 This is so we can generate gaps between fields, when appropriate. */
4263 static tree constructor_bit_index
;
4265 /* If we are saving up the elements rather than allocating them,
4266 this is the list of elements so far (in reverse order,
4267 most recent first). */
4268 static tree constructor_elements
;
4270 /* 1 if constructor should be incrementally stored into a constructor chain,
4271 0 if all the elements should be kept in AVL tree. */
4272 static int constructor_incremental
;
4274 /* 1 if so far this constructor's elements are all compile-time constants. */
4275 static int constructor_constant
;
4277 /* 1 if so far this constructor's elements are all valid address constants. */
4278 static int constructor_simple
;
4280 /* 1 if this constructor is erroneous so far. */
4281 static int constructor_erroneous
;
4283 /* Structure for managing pending initializer elements, organized as an
4288 struct init_node
*left
, *right
;
4289 struct init_node
*parent
;
4295 /* Tree of pending elements at this constructor level.
4296 These are elements encountered out of order
4297 which belong at places we haven't reached yet in actually
4299 Will never hold tree nodes across GC runs. */
4300 static struct init_node
*constructor_pending_elts
;
4302 /* The SPELLING_DEPTH of this constructor. */
4303 static int constructor_depth
;
4305 /* 0 if implicitly pushing constructor levels is allowed. */
4306 int constructor_no_implicit
= 0; /* 0 for C; 1 for some other languages. */
4308 /* DECL node for which an initializer is being read.
4309 0 means we are reading a constructor expression
4310 such as (struct foo) {...}. */
4311 static tree constructor_decl
;
4313 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4314 static const char *constructor_asmspec
;
4316 /* Nonzero if this is an initializer for a top-level decl. */
4317 static int constructor_top_level
;
4319 /* Nonzero if there were any member designators in this initializer. */
4320 static int constructor_designated
;
4322 /* Nesting depth of designator list. */
4323 static int designator_depth
;
4325 /* Nonzero if there were diagnosed errors in this designator list. */
4326 static int designator_errorneous
;
4329 /* This stack has a level for each implicit or explicit level of
4330 structuring in the initializer, including the outermost one. It
4331 saves the values of most of the variables above. */
4333 struct constructor_range_stack
;
4335 struct constructor_stack
4337 struct constructor_stack
*next
;
4342 tree unfilled_index
;
4343 tree unfilled_fields
;
4346 struct init_node
*pending_elts
;
4349 /* If value nonzero, this value should replace the entire
4350 constructor at this level. */
4351 struct c_expr replacement_value
;
4352 struct constructor_range_stack
*range_stack
;
4362 struct constructor_stack
*constructor_stack
;
4364 /* This stack represents designators from some range designator up to
4365 the last designator in the list. */
4367 struct constructor_range_stack
4369 struct constructor_range_stack
*next
, *prev
;
4370 struct constructor_stack
*stack
;
4377 struct constructor_range_stack
*constructor_range_stack
;
4379 /* This stack records separate initializers that are nested.
4380 Nested initializers can't happen in ANSI C, but GNU C allows them
4381 in cases like { ... (struct foo) { ... } ... }. */
4383 struct initializer_stack
4385 struct initializer_stack
*next
;
4387 const char *asmspec
;
4388 struct constructor_stack
*constructor_stack
;
4389 struct constructor_range_stack
*constructor_range_stack
;
4391 struct spelling
*spelling
;
4392 struct spelling
*spelling_base
;
4395 char require_constant_value
;
4396 char require_constant_elements
;
4399 struct initializer_stack
*initializer_stack
;
4401 /* Prepare to parse and output the initializer for variable DECL. */
4404 start_init (tree decl
, tree asmspec_tree
, int top_level
)
4407 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
4408 const char *asmspec
= 0;
4411 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
4413 p
->decl
= constructor_decl
;
4414 p
->asmspec
= constructor_asmspec
;
4415 p
->require_constant_value
= require_constant_value
;
4416 p
->require_constant_elements
= require_constant_elements
;
4417 p
->constructor_stack
= constructor_stack
;
4418 p
->constructor_range_stack
= constructor_range_stack
;
4419 p
->elements
= constructor_elements
;
4420 p
->spelling
= spelling
;
4421 p
->spelling_base
= spelling_base
;
4422 p
->spelling_size
= spelling_size
;
4423 p
->top_level
= constructor_top_level
;
4424 p
->next
= initializer_stack
;
4425 initializer_stack
= p
;
4427 constructor_decl
= decl
;
4428 constructor_asmspec
= asmspec
;
4429 constructor_designated
= 0;
4430 constructor_top_level
= top_level
;
4434 require_constant_value
= TREE_STATIC (decl
);
4435 require_constant_elements
4436 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
4437 /* For a scalar, you can always use any value to initialize,
4438 even within braces. */
4439 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
4440 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
4441 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
4442 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
4443 locus
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4447 require_constant_value
= 0;
4448 require_constant_elements
= 0;
4449 locus
= "(anonymous)";
4452 constructor_stack
= 0;
4453 constructor_range_stack
= 0;
4455 missing_braces_mentioned
= 0;
4459 RESTORE_SPELLING_DEPTH (0);
4462 push_string (locus
);
4468 struct initializer_stack
*p
= initializer_stack
;
4470 /* Free the whole constructor stack of this initializer. */
4471 while (constructor_stack
)
4473 struct constructor_stack
*q
= constructor_stack
;
4474 constructor_stack
= q
->next
;
4478 gcc_assert (!constructor_range_stack
);
4480 /* Pop back to the data of the outer initializer (if any). */
4481 free (spelling_base
);
4483 constructor_decl
= p
->decl
;
4484 constructor_asmspec
= p
->asmspec
;
4485 require_constant_value
= p
->require_constant_value
;
4486 require_constant_elements
= p
->require_constant_elements
;
4487 constructor_stack
= p
->constructor_stack
;
4488 constructor_range_stack
= p
->constructor_range_stack
;
4489 constructor_elements
= p
->elements
;
4490 spelling
= p
->spelling
;
4491 spelling_base
= p
->spelling_base
;
4492 spelling_size
= p
->spelling_size
;
4493 constructor_top_level
= p
->top_level
;
4494 initializer_stack
= p
->next
;
4498 /* Call here when we see the initializer is surrounded by braces.
4499 This is instead of a call to push_init_level;
4500 it is matched by a call to pop_init_level.
4502 TYPE is the type to initialize, for a constructor expression.
4503 For an initializer for a decl, TYPE is zero. */
4506 really_start_incremental_init (tree type
)
4508 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
4511 type
= TREE_TYPE (constructor_decl
);
4513 if (targetm
.vector_opaque_p (type
))
4514 error ("opaque vector types cannot be initialized");
4516 p
->type
= constructor_type
;
4517 p
->fields
= constructor_fields
;
4518 p
->index
= constructor_index
;
4519 p
->max_index
= constructor_max_index
;
4520 p
->unfilled_index
= constructor_unfilled_index
;
4521 p
->unfilled_fields
= constructor_unfilled_fields
;
4522 p
->bit_index
= constructor_bit_index
;
4523 p
->elements
= constructor_elements
;
4524 p
->constant
= constructor_constant
;
4525 p
->simple
= constructor_simple
;
4526 p
->erroneous
= constructor_erroneous
;
4527 p
->pending_elts
= constructor_pending_elts
;
4528 p
->depth
= constructor_depth
;
4529 p
->replacement_value
.value
= 0;
4530 p
->replacement_value
.original_code
= ERROR_MARK
;
4534 p
->incremental
= constructor_incremental
;
4535 p
->designated
= constructor_designated
;
4537 constructor_stack
= p
;
4539 constructor_constant
= 1;
4540 constructor_simple
= 1;
4541 constructor_depth
= SPELLING_DEPTH ();
4542 constructor_elements
= 0;
4543 constructor_pending_elts
= 0;
4544 constructor_type
= type
;
4545 constructor_incremental
= 1;
4546 constructor_designated
= 0;
4547 designator_depth
= 0;
4548 designator_errorneous
= 0;
4550 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4551 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4553 constructor_fields
= TYPE_FIELDS (constructor_type
);
4554 /* Skip any nameless bit fields at the beginning. */
4555 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4556 && DECL_NAME (constructor_fields
) == 0)
4557 constructor_fields
= TREE_CHAIN (constructor_fields
);
4559 constructor_unfilled_fields
= constructor_fields
;
4560 constructor_bit_index
= bitsize_zero_node
;
4562 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4564 if (TYPE_DOMAIN (constructor_type
))
4566 constructor_max_index
4567 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4569 /* Detect non-empty initializations of zero-length arrays. */
4570 if (constructor_max_index
== NULL_TREE
4571 && TYPE_SIZE (constructor_type
))
4572 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4574 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4575 to initialize VLAs will cause a proper error; avoid tree
4576 checking errors as well by setting a safe value. */
4577 if (constructor_max_index
4578 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4579 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4582 = convert (bitsizetype
,
4583 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4586 constructor_index
= bitsize_zero_node
;
4588 constructor_unfilled_index
= constructor_index
;
4590 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4592 /* Vectors are like simple fixed-size arrays. */
4593 constructor_max_index
=
4594 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
4595 constructor_index
= convert (bitsizetype
, bitsize_zero_node
);
4596 constructor_unfilled_index
= constructor_index
;
4600 /* Handle the case of int x = {5}; */
4601 constructor_fields
= constructor_type
;
4602 constructor_unfilled_fields
= constructor_type
;
4606 /* Push down into a subobject, for initialization.
4607 If this is for an explicit set of braces, IMPLICIT is 0.
4608 If it is because the next element belongs at a lower level,
4609 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4612 push_init_level (int implicit
)
4614 struct constructor_stack
*p
;
4615 tree value
= NULL_TREE
;
4617 /* If we've exhausted any levels that didn't have braces,
4619 while (constructor_stack
->implicit
)
4621 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4622 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4623 && constructor_fields
== 0)
4624 process_init_element (pop_init_level (1));
4625 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
4626 && constructor_max_index
4627 && tree_int_cst_lt (constructor_max_index
, constructor_index
))
4628 process_init_element (pop_init_level (1));
4633 /* Unless this is an explicit brace, we need to preserve previous
4637 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4638 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4639 && constructor_fields
)
4640 value
= find_init_member (constructor_fields
);
4641 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4642 value
= find_init_member (constructor_index
);
4645 p
= XNEW (struct constructor_stack
);
4646 p
->type
= constructor_type
;
4647 p
->fields
= constructor_fields
;
4648 p
->index
= constructor_index
;
4649 p
->max_index
= constructor_max_index
;
4650 p
->unfilled_index
= constructor_unfilled_index
;
4651 p
->unfilled_fields
= constructor_unfilled_fields
;
4652 p
->bit_index
= constructor_bit_index
;
4653 p
->elements
= constructor_elements
;
4654 p
->constant
= constructor_constant
;
4655 p
->simple
= constructor_simple
;
4656 p
->erroneous
= constructor_erroneous
;
4657 p
->pending_elts
= constructor_pending_elts
;
4658 p
->depth
= constructor_depth
;
4659 p
->replacement_value
.value
= 0;
4660 p
->replacement_value
.original_code
= ERROR_MARK
;
4661 p
->implicit
= implicit
;
4663 p
->incremental
= constructor_incremental
;
4664 p
->designated
= constructor_designated
;
4665 p
->next
= constructor_stack
;
4667 constructor_stack
= p
;
4669 constructor_constant
= 1;
4670 constructor_simple
= 1;
4671 constructor_depth
= SPELLING_DEPTH ();
4672 constructor_elements
= 0;
4673 constructor_incremental
= 1;
4674 constructor_designated
= 0;
4675 constructor_pending_elts
= 0;
4678 p
->range_stack
= constructor_range_stack
;
4679 constructor_range_stack
= 0;
4680 designator_depth
= 0;
4681 designator_errorneous
= 0;
4684 /* Don't die if an entire brace-pair level is superfluous
4685 in the containing level. */
4686 if (constructor_type
== 0)
4688 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
4689 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4691 /* Don't die if there are extra init elts at the end. */
4692 if (constructor_fields
== 0)
4693 constructor_type
= 0;
4696 constructor_type
= TREE_TYPE (constructor_fields
);
4697 push_member_name (constructor_fields
);
4698 constructor_depth
++;
4701 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4703 constructor_type
= TREE_TYPE (constructor_type
);
4704 push_array_bounds (tree_low_cst (constructor_index
, 0));
4705 constructor_depth
++;
4708 if (constructor_type
== 0)
4710 error_init ("extra brace group at end of initializer");
4711 constructor_fields
= 0;
4712 constructor_unfilled_fields
= 0;
4716 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
4718 constructor_constant
= TREE_CONSTANT (value
);
4719 constructor_simple
= TREE_STATIC (value
);
4720 constructor_elements
= CONSTRUCTOR_ELTS (value
);
4721 if (constructor_elements
4722 && (TREE_CODE (constructor_type
) == RECORD_TYPE
4723 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
4724 set_nonincremental_init ();
4727 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
4729 missing_braces_mentioned
= 1;
4730 warning_init ("missing braces around initializer");
4733 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4734 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4736 constructor_fields
= TYPE_FIELDS (constructor_type
);
4737 /* Skip any nameless bit fields at the beginning. */
4738 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4739 && DECL_NAME (constructor_fields
) == 0)
4740 constructor_fields
= TREE_CHAIN (constructor_fields
);
4742 constructor_unfilled_fields
= constructor_fields
;
4743 constructor_bit_index
= bitsize_zero_node
;
4745 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4747 /* Vectors are like simple fixed-size arrays. */
4748 constructor_max_index
=
4749 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
4750 constructor_index
= convert (bitsizetype
, integer_zero_node
);
4751 constructor_unfilled_index
= constructor_index
;
4753 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4755 if (TYPE_DOMAIN (constructor_type
))
4757 constructor_max_index
4758 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4760 /* Detect non-empty initializations of zero-length arrays. */
4761 if (constructor_max_index
== NULL_TREE
4762 && TYPE_SIZE (constructor_type
))
4763 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4765 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4766 to initialize VLAs will cause a proper error; avoid tree
4767 checking errors as well by setting a safe value. */
4768 if (constructor_max_index
4769 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4770 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4773 = convert (bitsizetype
,
4774 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4777 constructor_index
= bitsize_zero_node
;
4779 constructor_unfilled_index
= constructor_index
;
4780 if (value
&& TREE_CODE (value
) == STRING_CST
)
4782 /* We need to split the char/wchar array into individual
4783 characters, so that we don't have to special case it
4785 set_nonincremental_init_from_string (value
);
4790 warning_init ("braces around scalar initializer");
4791 constructor_fields
= constructor_type
;
4792 constructor_unfilled_fields
= constructor_type
;
4796 /* At the end of an implicit or explicit brace level,
4797 finish up that level of constructor. If a single expression
4798 with redundant braces initialized that level, return the
4799 c_expr structure for that expression. Otherwise, the original_code
4800 element is set to ERROR_MARK.
4801 If we were outputting the elements as they are read, return 0 as the value
4802 from inner levels (process_init_element ignores that),
4803 but return error_mark_node as the value from the outermost level
4804 (that's what we want to put in DECL_INITIAL).
4805 Otherwise, return a CONSTRUCTOR expression as the value. */
4808 pop_init_level (int implicit
)
4810 struct constructor_stack
*p
;
4813 ret
.original_code
= ERROR_MARK
;
4817 /* When we come to an explicit close brace,
4818 pop any inner levels that didn't have explicit braces. */
4819 while (constructor_stack
->implicit
)
4820 process_init_element (pop_init_level (1));
4822 gcc_assert (!constructor_range_stack
);
4825 /* Now output all pending elements. */
4826 constructor_incremental
= 1;
4827 output_pending_init_elements (1);
4829 p
= constructor_stack
;
4831 /* Error for initializing a flexible array member, or a zero-length
4832 array member in an inappropriate context. */
4833 if (constructor_type
&& constructor_fields
4834 && TREE_CODE (constructor_type
) == ARRAY_TYPE
4835 && TYPE_DOMAIN (constructor_type
)
4836 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
4838 /* Silently discard empty initializations. The parser will
4839 already have pedwarned for empty brackets. */
4840 if (integer_zerop (constructor_unfilled_index
))
4841 constructor_type
= NULL_TREE
;
4844 gcc_assert (!TYPE_SIZE (constructor_type
));
4846 if (constructor_depth
> 2)
4847 error_init ("initialization of flexible array member in a nested context");
4849 pedwarn_init ("initialization of a flexible array member");
4851 /* We have already issued an error message for the existence
4852 of a flexible array member not at the end of the structure.
4853 Discard the initializer so that we do not abort later. */
4854 if (TREE_CHAIN (constructor_fields
) != NULL_TREE
)
4855 constructor_type
= NULL_TREE
;
4859 /* Warn when some struct elements are implicitly initialized to zero. */
4860 if (warn_missing_field_initializers
4862 && TREE_CODE (constructor_type
) == RECORD_TYPE
4863 && constructor_unfilled_fields
)
4865 /* Do not warn for flexible array members or zero-length arrays. */
4866 while (constructor_unfilled_fields
4867 && (! DECL_SIZE (constructor_unfilled_fields
)
4868 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
4869 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
4871 /* Do not warn if this level of the initializer uses member
4872 designators; it is likely to be deliberate. */
4873 if (constructor_unfilled_fields
&& !constructor_designated
)
4875 push_member_name (constructor_unfilled_fields
);
4876 warning_init ("missing initializer");
4877 RESTORE_SPELLING_DEPTH (constructor_depth
);
4881 /* Pad out the end of the structure. */
4882 if (p
->replacement_value
.value
)
4883 /* If this closes a superfluous brace pair,
4884 just pass out the element between them. */
4885 ret
= p
->replacement_value
;
4886 else if (constructor_type
== 0)
4888 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
4889 && TREE_CODE (constructor_type
) != UNION_TYPE
4890 && TREE_CODE (constructor_type
) != ARRAY_TYPE
4891 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
4893 /* A nonincremental scalar initializer--just return
4894 the element, after verifying there is just one. */
4895 if (constructor_elements
== 0)
4897 if (!constructor_erroneous
)
4898 error_init ("empty scalar initializer");
4899 ret
.value
= error_mark_node
;
4901 else if (TREE_CHAIN (constructor_elements
) != 0)
4903 error_init ("extra elements in scalar initializer");
4904 ret
.value
= TREE_VALUE (constructor_elements
);
4907 ret
.value
= TREE_VALUE (constructor_elements
);
4911 if (constructor_erroneous
)
4912 ret
.value
= error_mark_node
;
4915 ret
.value
= build_constructor (constructor_type
,
4916 nreverse (constructor_elements
));
4917 if (constructor_constant
)
4918 TREE_CONSTANT (ret
.value
) = TREE_INVARIANT (ret
.value
) = 1;
4919 if (constructor_constant
&& constructor_simple
)
4920 TREE_STATIC (ret
.value
) = 1;
4924 constructor_type
= p
->type
;
4925 constructor_fields
= p
->fields
;
4926 constructor_index
= p
->index
;
4927 constructor_max_index
= p
->max_index
;
4928 constructor_unfilled_index
= p
->unfilled_index
;
4929 constructor_unfilled_fields
= p
->unfilled_fields
;
4930 constructor_bit_index
= p
->bit_index
;
4931 constructor_elements
= p
->elements
;
4932 constructor_constant
= p
->constant
;
4933 constructor_simple
= p
->simple
;
4934 constructor_erroneous
= p
->erroneous
;
4935 constructor_incremental
= p
->incremental
;
4936 constructor_designated
= p
->designated
;
4937 constructor_pending_elts
= p
->pending_elts
;
4938 constructor_depth
= p
->depth
;
4940 constructor_range_stack
= p
->range_stack
;
4941 RESTORE_SPELLING_DEPTH (constructor_depth
);
4943 constructor_stack
= p
->next
;
4948 if (constructor_stack
== 0)
4950 ret
.value
= error_mark_node
;
4958 /* Common handling for both array range and field name designators.
4959 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4962 set_designator (int array
)
4965 enum tree_code subcode
;
4967 /* Don't die if an entire brace-pair level is superfluous
4968 in the containing level. */
4969 if (constructor_type
== 0)
4972 /* If there were errors in this designator list already, bail out
4974 if (designator_errorneous
)
4977 if (!designator_depth
)
4979 gcc_assert (!constructor_range_stack
);
4981 /* Designator list starts at the level of closest explicit
4983 while (constructor_stack
->implicit
)
4984 process_init_element (pop_init_level (1));
4985 constructor_designated
= 1;
4989 if (constructor_no_implicit
)
4991 error_init ("initialization designators may not nest");
4995 switch (TREE_CODE (constructor_type
))
4999 subtype
= TREE_TYPE (constructor_fields
);
5000 if (subtype
!= error_mark_node
)
5001 subtype
= TYPE_MAIN_VARIANT (subtype
);
5004 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
5010 subcode
= TREE_CODE (subtype
);
5011 if (array
&& subcode
!= ARRAY_TYPE
)
5013 error_init ("array index in non-array initializer");
5016 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
5018 error_init ("field name not in record or union initializer");
5022 constructor_designated
= 1;
5023 push_init_level (2);
5027 /* If there are range designators in designator list, push a new designator
5028 to constructor_range_stack. RANGE_END is end of such stack range or
5029 NULL_TREE if there is no range designator at this level. */
5032 push_range_stack (tree range_end
)
5034 struct constructor_range_stack
*p
;
5036 p
= GGC_NEW (struct constructor_range_stack
);
5037 p
->prev
= constructor_range_stack
;
5039 p
->fields
= constructor_fields
;
5040 p
->range_start
= constructor_index
;
5041 p
->index
= constructor_index
;
5042 p
->stack
= constructor_stack
;
5043 p
->range_end
= range_end
;
5044 if (constructor_range_stack
)
5045 constructor_range_stack
->next
= p
;
5046 constructor_range_stack
= p
;
5049 /* Within an array initializer, specify the next index to be initialized.
5050 FIRST is that index. If LAST is nonzero, then initialize a range
5051 of indices, running from FIRST through LAST. */
5054 set_init_index (tree first
, tree last
)
5056 if (set_designator (1))
5059 designator_errorneous
= 1;
5061 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
5062 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
5064 error_init ("array index in initializer not of integer type");
5068 while ((TREE_CODE (first
) == NOP_EXPR
5069 || TREE_CODE (first
) == CONVERT_EXPR
5070 || TREE_CODE (first
) == NON_LVALUE_EXPR
)
5071 && (TYPE_MODE (TREE_TYPE (first
))
5072 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first
, 0)))))
5073 first
= TREE_OPERAND (first
, 0);
5076 while ((TREE_CODE (last
) == NOP_EXPR
5077 || TREE_CODE (last
) == CONVERT_EXPR
5078 || TREE_CODE (last
) == NON_LVALUE_EXPR
)
5079 && (TYPE_MODE (TREE_TYPE (last
))
5080 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last
, 0)))))
5081 last
= TREE_OPERAND (last
, 0);
5083 if (TREE_CODE (first
) != INTEGER_CST
)
5084 error_init ("nonconstant array index in initializer");
5085 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
5086 error_init ("nonconstant array index in initializer");
5087 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5088 error_init ("array index in non-array initializer");
5089 else if (tree_int_cst_sgn (first
) == -1)
5090 error_init ("array index in initializer exceeds array bounds");
5091 else if (constructor_max_index
5092 && tree_int_cst_lt (constructor_max_index
, first
))
5093 error_init ("array index in initializer exceeds array bounds");
5096 constructor_index
= convert (bitsizetype
, first
);
5100 if (tree_int_cst_equal (first
, last
))
5102 else if (tree_int_cst_lt (last
, first
))
5104 error_init ("empty index range in initializer");
5109 last
= convert (bitsizetype
, last
);
5110 if (constructor_max_index
!= 0
5111 && tree_int_cst_lt (constructor_max_index
, last
))
5113 error_init ("array index range in initializer exceeds array bounds");
5120 designator_errorneous
= 0;
5121 if (constructor_range_stack
|| last
)
5122 push_range_stack (last
);
5126 /* Within a struct initializer, specify the next field to be initialized. */
5129 set_init_label (tree fieldname
)
5133 if (set_designator (0))
5136 designator_errorneous
= 1;
5138 if (TREE_CODE (constructor_type
) != RECORD_TYPE
5139 && TREE_CODE (constructor_type
) != UNION_TYPE
)
5141 error_init ("field name not in record or union initializer");
5145 for (tail
= TYPE_FIELDS (constructor_type
); tail
;
5146 tail
= TREE_CHAIN (tail
))
5148 if (DECL_NAME (tail
) == fieldname
)
5153 error ("unknown field %qs specified in initializer",
5154 IDENTIFIER_POINTER (fieldname
));
5157 constructor_fields
= tail
;
5159 designator_errorneous
= 0;
5160 if (constructor_range_stack
)
5161 push_range_stack (NULL_TREE
);
5165 /* Add a new initializer to the tree of pending initializers. PURPOSE
5166 identifies the initializer, either array index or field in a structure.
5167 VALUE is the value of that index or field. */
5170 add_pending_init (tree purpose
, tree value
)
5172 struct init_node
*p
, **q
, *r
;
5174 q
= &constructor_pending_elts
;
5177 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5182 if (tree_int_cst_lt (purpose
, p
->purpose
))
5184 else if (tree_int_cst_lt (p
->purpose
, purpose
))
5188 if (TREE_SIDE_EFFECTS (p
->value
))
5189 warning_init ("initialized field with side-effects overwritten");
5199 bitpos
= bit_position (purpose
);
5203 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5205 else if (p
->purpose
!= purpose
)
5209 if (TREE_SIDE_EFFECTS (p
->value
))
5210 warning_init ("initialized field with side-effects overwritten");
5217 r
= GGC_NEW (struct init_node
);
5218 r
->purpose
= purpose
;
5229 struct init_node
*s
;
5233 if (p
->balance
== 0)
5235 else if (p
->balance
< 0)
5242 p
->left
->parent
= p
;
5259 constructor_pending_elts
= r
;
5264 struct init_node
*t
= r
->right
;
5268 r
->right
->parent
= r
;
5273 p
->left
->parent
= p
;
5276 p
->balance
= t
->balance
< 0;
5277 r
->balance
= -(t
->balance
> 0);
5292 constructor_pending_elts
= t
;
5298 /* p->balance == +1; growth of left side balances the node. */
5303 else /* r == p->right */
5305 if (p
->balance
== 0)
5306 /* Growth propagation from right side. */
5308 else if (p
->balance
> 0)
5315 p
->right
->parent
= p
;
5332 constructor_pending_elts
= r
;
5334 else /* r->balance == -1 */
5337 struct init_node
*t
= r
->left
;
5341 r
->left
->parent
= r
;
5346 p
->right
->parent
= p
;
5349 r
->balance
= (t
->balance
< 0);
5350 p
->balance
= -(t
->balance
> 0);
5365 constructor_pending_elts
= t
;
5371 /* p->balance == -1; growth of right side balances the node. */
5382 /* Build AVL tree from a sorted chain. */
5385 set_nonincremental_init (void)
5389 if (TREE_CODE (constructor_type
) != RECORD_TYPE
5390 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5393 for (chain
= constructor_elements
; chain
; chain
= TREE_CHAIN (chain
))
5394 add_pending_init (TREE_PURPOSE (chain
), TREE_VALUE (chain
));
5395 constructor_elements
= 0;
5396 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5398 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
5399 /* Skip any nameless bit fields at the beginning. */
5400 while (constructor_unfilled_fields
!= 0
5401 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5402 && DECL_NAME (constructor_unfilled_fields
) == 0)
5403 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
5406 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5408 if (TYPE_DOMAIN (constructor_type
))
5409 constructor_unfilled_index
5410 = convert (bitsizetype
,
5411 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
5413 constructor_unfilled_index
= bitsize_zero_node
;
5415 constructor_incremental
= 0;
5418 /* Build AVL tree from a string constant. */
5421 set_nonincremental_init_from_string (tree str
)
5423 tree value
, purpose
, type
;
5424 HOST_WIDE_INT val
[2];
5425 const char *p
, *end
;
5426 int byte
, wchar_bytes
, charwidth
, bitpos
;
5428 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
5430 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5431 == TYPE_PRECISION (char_type_node
))
5435 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5436 == TYPE_PRECISION (wchar_type_node
));
5437 wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
5439 charwidth
= TYPE_PRECISION (char_type_node
);
5440 type
= TREE_TYPE (constructor_type
);
5441 p
= TREE_STRING_POINTER (str
);
5442 end
= p
+ TREE_STRING_LENGTH (str
);
5444 for (purpose
= bitsize_zero_node
;
5445 p
< end
&& !tree_int_cst_lt (constructor_max_index
, purpose
);
5446 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
5448 if (wchar_bytes
== 1)
5450 val
[1] = (unsigned char) *p
++;
5457 for (byte
= 0; byte
< wchar_bytes
; byte
++)
5459 if (BYTES_BIG_ENDIAN
)
5460 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
5462 bitpos
= byte
* charwidth
;
5463 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
5464 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
5465 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
5469 if (!TYPE_UNSIGNED (type
))
5471 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
5472 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
5474 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
5476 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
5480 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
5485 else if (val
[0] & (((HOST_WIDE_INT
) 1)
5486 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
5487 val
[0] |= ((HOST_WIDE_INT
) -1)
5488 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
5491 value
= build_int_cst_wide (type
, val
[1], val
[0]);
5492 add_pending_init (purpose
, value
);
5495 constructor_incremental
= 0;
5498 /* Return value of FIELD in pending initializer or zero if the field was
5499 not initialized yet. */
5502 find_init_member (tree field
)
5504 struct init_node
*p
;
5506 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5508 if (constructor_incremental
5509 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5510 set_nonincremental_init ();
5512 p
= constructor_pending_elts
;
5515 if (tree_int_cst_lt (field
, p
->purpose
))
5517 else if (tree_int_cst_lt (p
->purpose
, field
))
5523 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5525 tree bitpos
= bit_position (field
);
5527 if (constructor_incremental
5528 && (!constructor_unfilled_fields
5529 || tree_int_cst_lt (bitpos
,
5530 bit_position (constructor_unfilled_fields
))))
5531 set_nonincremental_init ();
5533 p
= constructor_pending_elts
;
5536 if (field
== p
->purpose
)
5538 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5544 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5546 if (constructor_elements
5547 && TREE_PURPOSE (constructor_elements
) == field
)
5548 return TREE_VALUE (constructor_elements
);
5553 /* "Output" the next constructor element.
5554 At top level, really output it to assembler code now.
5555 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5556 TYPE is the data type that the containing data type wants here.
5557 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5558 If VALUE is a string constant, STRICT_STRING is true if it is
5559 unparenthesized or we should not warn here for it being parenthesized.
5560 For other types of VALUE, STRICT_STRING is not used.
5562 PENDING if non-nil means output pending elements that belong
5563 right after this element. (PENDING is normally 1;
5564 it is 0 while outputting pending elements, to avoid recursion.) */
5567 output_init_element (tree value
, bool strict_string
, tree type
, tree field
,
5570 if (type
== error_mark_node
)
5572 constructor_erroneous
= 1;
5575 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5576 || (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5577 && !(TREE_CODE (value
) == STRING_CST
5578 && TREE_CODE (type
) == ARRAY_TYPE
5579 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
5580 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
5581 TYPE_MAIN_VARIANT (type
))))
5582 value
= default_conversion (value
);
5584 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
5585 && require_constant_value
&& !flag_isoc99
&& pending
)
5587 /* As an extension, allow initializing objects with static storage
5588 duration with compound literals (which are then treated just as
5589 the brace enclosed list they contain). */
5590 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
5591 value
= DECL_INITIAL (decl
);
5594 if (value
== error_mark_node
)
5595 constructor_erroneous
= 1;
5596 else if (!TREE_CONSTANT (value
))
5597 constructor_constant
= 0;
5598 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
5599 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
5600 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5601 && DECL_C_BIT_FIELD (field
)
5602 && TREE_CODE (value
) != INTEGER_CST
))
5603 constructor_simple
= 0;
5605 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
5607 if (require_constant_value
)
5609 error_init ("initializer element is not constant");
5610 value
= error_mark_node
;
5612 else if (require_constant_elements
)
5613 pedwarn ("initializer element is not computable at load time");
5616 /* If this field is empty (and not at the end of structure),
5617 don't do anything other than checking the initializer. */
5619 && (TREE_TYPE (field
) == error_mark_node
5620 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
5621 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
5622 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
5623 || TREE_CHAIN (field
)))))
5626 value
= digest_init (type
, value
, strict_string
, require_constant_value
);
5627 if (value
== error_mark_node
)
5629 constructor_erroneous
= 1;
5633 /* If this element doesn't come next in sequence,
5634 put it on constructor_pending_elts. */
5635 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5636 && (!constructor_incremental
5637 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
5639 if (constructor_incremental
5640 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5641 set_nonincremental_init ();
5643 add_pending_init (field
, value
);
5646 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5647 && (!constructor_incremental
5648 || field
!= constructor_unfilled_fields
))
5650 /* We do this for records but not for unions. In a union,
5651 no matter which field is specified, it can be initialized
5652 right away since it starts at the beginning of the union. */
5653 if (constructor_incremental
)
5655 if (!constructor_unfilled_fields
)
5656 set_nonincremental_init ();
5659 tree bitpos
, unfillpos
;
5661 bitpos
= bit_position (field
);
5662 unfillpos
= bit_position (constructor_unfilled_fields
);
5664 if (tree_int_cst_lt (bitpos
, unfillpos
))
5665 set_nonincremental_init ();
5669 add_pending_init (field
, value
);
5672 else if (TREE_CODE (constructor_type
) == UNION_TYPE
5673 && constructor_elements
)
5675 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements
)))
5676 warning_init ("initialized field with side-effects overwritten");
5678 /* We can have just one union field set. */
5679 constructor_elements
= 0;
5682 /* Otherwise, output this element either to
5683 constructor_elements or to the assembler file. */
5685 if (field
&& TREE_CODE (field
) == INTEGER_CST
)
5686 field
= copy_node (field
);
5687 constructor_elements
5688 = tree_cons (field
, value
, constructor_elements
);
5690 /* Advance the variable that indicates sequential elements output. */
5691 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5692 constructor_unfilled_index
5693 = size_binop (PLUS_EXPR
, constructor_unfilled_index
,
5695 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5697 constructor_unfilled_fields
5698 = TREE_CHAIN (constructor_unfilled_fields
);
5700 /* Skip any nameless bit fields. */
5701 while (constructor_unfilled_fields
!= 0
5702 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5703 && DECL_NAME (constructor_unfilled_fields
) == 0)
5704 constructor_unfilled_fields
=
5705 TREE_CHAIN (constructor_unfilled_fields
);
5707 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5708 constructor_unfilled_fields
= 0;
5710 /* Now output any pending elements which have become next. */
5712 output_pending_init_elements (0);
5715 /* Output any pending elements which have become next.
5716 As we output elements, constructor_unfilled_{fields,index}
5717 advances, which may cause other elements to become next;
5718 if so, they too are output.
5720 If ALL is 0, we return when there are
5721 no more pending elements to output now.
5723 If ALL is 1, we output space as necessary so that
5724 we can output all the pending elements. */
5727 output_pending_init_elements (int all
)
5729 struct init_node
*elt
= constructor_pending_elts
;
5734 /* Look through the whole pending tree.
5735 If we find an element that should be output now,
5736 output it. Otherwise, set NEXT to the element
5737 that comes first among those still pending. */
5742 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5744 if (tree_int_cst_equal (elt
->purpose
,
5745 constructor_unfilled_index
))
5746 output_init_element (elt
->value
, true,
5747 TREE_TYPE (constructor_type
),
5748 constructor_unfilled_index
, 0);
5749 else if (tree_int_cst_lt (constructor_unfilled_index
,
5752 /* Advance to the next smaller node. */
5757 /* We have reached the smallest node bigger than the
5758 current unfilled index. Fill the space first. */
5759 next
= elt
->purpose
;
5765 /* Advance to the next bigger node. */
5770 /* We have reached the biggest node in a subtree. Find
5771 the parent of it, which is the next bigger node. */
5772 while (elt
->parent
&& elt
->parent
->right
== elt
)
5775 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
5778 next
= elt
->purpose
;
5784 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5785 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5787 tree ctor_unfilled_bitpos
, elt_bitpos
;
5789 /* If the current record is complete we are done. */
5790 if (constructor_unfilled_fields
== 0)
5793 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
5794 elt_bitpos
= bit_position (elt
->purpose
);
5795 /* We can't compare fields here because there might be empty
5796 fields in between. */
5797 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
5799 constructor_unfilled_fields
= elt
->purpose
;
5800 output_init_element (elt
->value
, true, TREE_TYPE (elt
->purpose
),
5803 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
5805 /* Advance to the next smaller node. */
5810 /* We have reached the smallest node bigger than the
5811 current unfilled field. Fill the space first. */
5812 next
= elt
->purpose
;
5818 /* Advance to the next bigger node. */
5823 /* We have reached the biggest node in a subtree. Find
5824 the parent of it, which is the next bigger node. */
5825 while (elt
->parent
&& elt
->parent
->right
== elt
)
5829 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
5830 bit_position (elt
->purpose
))))
5832 next
= elt
->purpose
;
5840 /* Ordinarily return, but not if we want to output all
5841 and there are elements left. */
5842 if (! (all
&& next
!= 0))
5845 /* If it's not incremental, just skip over the gap, so that after
5846 jumping to retry we will output the next successive element. */
5847 if (TREE_CODE (constructor_type
) == RECORD_TYPE
5848 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5849 constructor_unfilled_fields
= next
;
5850 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5851 constructor_unfilled_index
= next
;
5853 /* ELT now points to the node in the pending tree with the next
5854 initializer to output. */
5858 /* Add one non-braced element to the current constructor level.
5859 This adjusts the current position within the constructor's type.
5860 This may also start or terminate implicit levels
5861 to handle a partly-braced initializer.
5863 Once this has found the correct level for the new element,
5864 it calls output_init_element. */
5867 process_init_element (struct c_expr value
)
5869 tree orig_value
= value
.value
;
5870 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
5871 bool strict_string
= value
.original_code
== STRING_CST
;
5873 designator_depth
= 0;
5874 designator_errorneous
= 0;
5876 /* Handle superfluous braces around string cst as in
5877 char x[] = {"foo"}; */
5880 && TREE_CODE (constructor_type
) == ARRAY_TYPE
5881 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
5882 && integer_zerop (constructor_unfilled_index
))
5884 if (constructor_stack
->replacement_value
.value
)
5885 error_init ("excess elements in char array initializer");
5886 constructor_stack
->replacement_value
= value
;
5890 if (constructor_stack
->replacement_value
.value
!= 0)
5892 error_init ("excess elements in struct initializer");
5896 /* Ignore elements of a brace group if it is entirely superfluous
5897 and has already been diagnosed. */
5898 if (constructor_type
== 0)
5901 /* If we've exhausted any levels that didn't have braces,
5903 while (constructor_stack
->implicit
)
5905 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
5906 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5907 && constructor_fields
== 0)
5908 process_init_element (pop_init_level (1));
5909 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5910 && (constructor_max_index
== 0
5911 || tree_int_cst_lt (constructor_max_index
,
5912 constructor_index
)))
5913 process_init_element (pop_init_level (1));
5918 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5919 if (constructor_range_stack
)
5921 /* If value is a compound literal and we'll be just using its
5922 content, don't put it into a SAVE_EXPR. */
5923 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
5924 || !require_constant_value
5926 value
.value
= save_expr (value
.value
);
5931 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5934 enum tree_code fieldcode
;
5936 if (constructor_fields
== 0)
5938 pedwarn_init ("excess elements in struct initializer");
5942 fieldtype
= TREE_TYPE (constructor_fields
);
5943 if (fieldtype
!= error_mark_node
)
5944 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
5945 fieldcode
= TREE_CODE (fieldtype
);
5947 /* Error for non-static initialization of a flexible array member. */
5948 if (fieldcode
== ARRAY_TYPE
5949 && !require_constant_value
5950 && TYPE_SIZE (fieldtype
) == NULL_TREE
5951 && TREE_CHAIN (constructor_fields
) == NULL_TREE
)
5953 error_init ("non-static initialization of a flexible array member");
5957 /* Accept a string constant to initialize a subarray. */
5958 if (value
.value
!= 0
5959 && fieldcode
== ARRAY_TYPE
5960 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
5962 value
.value
= orig_value
;
5963 /* Otherwise, if we have come to a subaggregate,
5964 and we don't have an element of its type, push into it. */
5965 else if (value
.value
!= 0 && !constructor_no_implicit
5966 && value
.value
!= error_mark_node
5967 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
5968 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
5969 || fieldcode
== UNION_TYPE
))
5971 push_init_level (1);
5977 push_member_name (constructor_fields
);
5978 output_init_element (value
.value
, strict_string
,
5979 fieldtype
, constructor_fields
, 1);
5980 RESTORE_SPELLING_DEPTH (constructor_depth
);
5983 /* Do the bookkeeping for an element that was
5984 directly output as a constructor. */
5986 /* For a record, keep track of end position of last field. */
5987 if (DECL_SIZE (constructor_fields
))
5988 constructor_bit_index
5989 = size_binop (PLUS_EXPR
,
5990 bit_position (constructor_fields
),
5991 DECL_SIZE (constructor_fields
));
5993 /* If the current field was the first one not yet written out,
5994 it isn't now, so update. */
5995 if (constructor_unfilled_fields
== constructor_fields
)
5997 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
5998 /* Skip any nameless bit fields. */
5999 while (constructor_unfilled_fields
!= 0
6000 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
6001 && DECL_NAME (constructor_unfilled_fields
) == 0)
6002 constructor_unfilled_fields
=
6003 TREE_CHAIN (constructor_unfilled_fields
);
6007 constructor_fields
= TREE_CHAIN (constructor_fields
);
6008 /* Skip any nameless bit fields at the beginning. */
6009 while (constructor_fields
!= 0
6010 && DECL_C_BIT_FIELD (constructor_fields
)
6011 && DECL_NAME (constructor_fields
) == 0)
6012 constructor_fields
= TREE_CHAIN (constructor_fields
);
6014 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
6017 enum tree_code fieldcode
;
6019 if (constructor_fields
== 0)
6021 pedwarn_init ("excess elements in union initializer");
6025 fieldtype
= TREE_TYPE (constructor_fields
);
6026 if (fieldtype
!= error_mark_node
)
6027 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
6028 fieldcode
= TREE_CODE (fieldtype
);
6030 /* Warn that traditional C rejects initialization of unions.
6031 We skip the warning if the value is zero. This is done
6032 under the assumption that the zero initializer in user
6033 code appears conditioned on e.g. __STDC__ to avoid
6034 "missing initializer" warnings and relies on default
6035 initialization to zero in the traditional C case.
6036 We also skip the warning if the initializer is designated,
6037 again on the assumption that this must be conditional on
6038 __STDC__ anyway (and we've already complained about the
6039 member-designator already). */
6040 if (warn_traditional
&& !in_system_header
&& !constructor_designated
6041 && !(value
.value
&& (integer_zerop (value
.value
)
6042 || real_zerop (value
.value
))))
6043 warning ("traditional C rejects initialization of unions");
6045 /* Accept a string constant to initialize a subarray. */
6046 if (value
.value
!= 0
6047 && fieldcode
== ARRAY_TYPE
6048 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
6050 value
.value
= orig_value
;
6051 /* Otherwise, if we have come to a subaggregate,
6052 and we don't have an element of its type, push into it. */
6053 else if (value
.value
!= 0 && !constructor_no_implicit
6054 && value
.value
!= error_mark_node
6055 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
6056 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
6057 || fieldcode
== UNION_TYPE
))
6059 push_init_level (1);
6065 push_member_name (constructor_fields
);
6066 output_init_element (value
.value
, strict_string
,
6067 fieldtype
, constructor_fields
, 1);
6068 RESTORE_SPELLING_DEPTH (constructor_depth
);
6071 /* Do the bookkeeping for an element that was
6072 directly output as a constructor. */
6074 constructor_bit_index
= DECL_SIZE (constructor_fields
);
6075 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
6078 constructor_fields
= 0;
6080 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6082 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6083 enum tree_code eltcode
= TREE_CODE (elttype
);
6085 /* Accept a string constant to initialize a subarray. */
6086 if (value
.value
!= 0
6087 && eltcode
== ARRAY_TYPE
6088 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
6090 value
.value
= orig_value
;
6091 /* Otherwise, if we have come to a subaggregate,
6092 and we don't have an element of its type, push into it. */
6093 else if (value
.value
!= 0 && !constructor_no_implicit
6094 && value
.value
!= error_mark_node
6095 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
6096 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
6097 || eltcode
== UNION_TYPE
))
6099 push_init_level (1);
6103 if (constructor_max_index
!= 0
6104 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
6105 || integer_all_onesp (constructor_max_index
)))
6107 pedwarn_init ("excess elements in array initializer");
6111 /* Now output the actual element. */
6114 push_array_bounds (tree_low_cst (constructor_index
, 0));
6115 output_init_element (value
.value
, strict_string
,
6116 elttype
, constructor_index
, 1);
6117 RESTORE_SPELLING_DEPTH (constructor_depth
);
6121 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
6124 /* If we are doing the bookkeeping for an element that was
6125 directly output as a constructor, we must update
6126 constructor_unfilled_index. */
6127 constructor_unfilled_index
= constructor_index
;
6129 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6131 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6133 /* Do a basic check of initializer size. Note that vectors
6134 always have a fixed size derived from their type. */
6135 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
6137 pedwarn_init ("excess elements in vector initializer");
6141 /* Now output the actual element. */
6143 output_init_element (value
.value
, strict_string
,
6144 elttype
, constructor_index
, 1);
6147 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
6150 /* If we are doing the bookkeeping for an element that was
6151 directly output as a constructor, we must update
6152 constructor_unfilled_index. */
6153 constructor_unfilled_index
= constructor_index
;
6156 /* Handle the sole element allowed in a braced initializer
6157 for a scalar variable. */
6158 else if (constructor_fields
== 0)
6160 pedwarn_init ("excess elements in scalar initializer");
6166 output_init_element (value
.value
, strict_string
,
6167 constructor_type
, NULL_TREE
, 1);
6168 constructor_fields
= 0;
6171 /* Handle range initializers either at this level or anywhere higher
6172 in the designator stack. */
6173 if (constructor_range_stack
)
6175 struct constructor_range_stack
*p
, *range_stack
;
6178 range_stack
= constructor_range_stack
;
6179 constructor_range_stack
= 0;
6180 while (constructor_stack
!= range_stack
->stack
)
6182 gcc_assert (constructor_stack
->implicit
);
6183 process_init_element (pop_init_level (1));
6185 for (p
= range_stack
;
6186 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
6189 gcc_assert (constructor_stack
->implicit
);
6190 process_init_element (pop_init_level (1));
6193 p
->index
= size_binop (PLUS_EXPR
, p
->index
, bitsize_one_node
);
6194 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
6199 constructor_index
= p
->index
;
6200 constructor_fields
= p
->fields
;
6201 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
6209 push_init_level (2);
6210 p
->stack
= constructor_stack
;
6211 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
6212 p
->index
= p
->range_start
;
6216 constructor_range_stack
= range_stack
;
6223 constructor_range_stack
= 0;
6226 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6227 (guaranteed to be 'volatile' or null) and ARGS (represented using
6228 an ASM_EXPR node). */
6230 build_asm_stmt (tree cv_qualifier
, tree args
)
6232 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
6233 ASM_VOLATILE_P (args
) = 1;
6234 return add_stmt (args
);
6237 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6238 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6239 SIMPLE indicates whether there was anything at all after the
6240 string in the asm expression -- asm("blah") and asm("blah" : )
6241 are subtly different. We use a ASM_EXPR node to represent this. */
6243 build_asm_expr (tree string
, tree outputs
, tree inputs
, tree clobbers
,
6249 const char *constraint
;
6250 bool allows_mem
, allows_reg
, is_inout
;
6254 ninputs
= list_length (inputs
);
6255 noutputs
= list_length (outputs
);
6257 /* Remove output conversions that change the type but not the mode. */
6258 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
6260 tree output
= TREE_VALUE (tail
);
6261 STRIP_NOPS (output
);
6262 TREE_VALUE (tail
) = output
;
6263 lvalue_or_else (output
, "invalid lvalue in asm statement");
6265 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
6267 if (!parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
6268 &allows_mem
, &allows_reg
, &is_inout
))
6270 /* By marking this operand as erroneous, we will not try
6271 to process this operand again in expand_asm_operands. */
6272 TREE_VALUE (tail
) = error_mark_node
;
6276 /* If the operand is a DECL that is going to end up in
6277 memory, assume it is addressable. This is a bit more
6278 conservative than it would ideally be; the exact test is
6279 buried deep in expand_asm_operands and depends on the
6280 DECL_RTL for the OPERAND -- which we don't have at this
6282 if (!allows_reg
&& DECL_P (output
))
6283 c_mark_addressable (output
);
6286 /* Perform default conversions on array and function inputs.
6287 Don't do this for other types as it would screw up operands
6288 expected to be in memory. */
6289 for (tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
))
6290 TREE_VALUE (tail
) = default_function_array_conversion (TREE_VALUE (tail
));
6292 args
= build_stmt (ASM_EXPR
, string
, outputs
, inputs
, clobbers
);
6294 /* Simple asm statements are treated as volatile. */
6297 ASM_VOLATILE_P (args
) = 1;
6298 ASM_INPUT_P (args
) = 1;
6303 /* Generate a goto statement to LABEL. */
6306 c_finish_goto_label (tree label
)
6308 tree decl
= lookup_label (label
);
6312 TREE_USED (decl
) = 1;
6313 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, decl
));
6316 /* Generate a computed goto statement to EXPR. */
6319 c_finish_goto_ptr (tree expr
)
6322 pedwarn ("ISO C forbids %<goto *expr;%>");
6323 expr
= convert (ptr_type_node
, expr
);
6324 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, expr
));
6327 /* Generate a C `return' statement. RETVAL is the expression for what
6328 to return, or a null pointer for `return;' with no value. */
6331 c_finish_return (tree retval
)
6333 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6335 if (TREE_THIS_VOLATILE (current_function_decl
))
6336 warning ("function declared %<noreturn%> has a %<return%> statement");
6340 current_function_returns_null
= 1;
6341 if ((warn_return_type
|| flag_isoc99
)
6342 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
6343 pedwarn_c99 ("%<return%> with no value, in "
6344 "function returning non-void");
6346 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
6348 current_function_returns_null
= 1;
6349 if (pedantic
|| TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
6350 pedwarn ("%<return%> with a value, in function returning void");
6354 tree t
= convert_for_assignment (valtype
, retval
, _("return"),
6355 NULL_TREE
, NULL_TREE
, 0);
6356 tree res
= DECL_RESULT (current_function_decl
);
6359 current_function_returns_value
= 1;
6360 if (t
== error_mark_node
)
6363 inner
= t
= convert (TREE_TYPE (res
), t
);
6365 /* Strip any conversions, additions, and subtractions, and see if
6366 we are returning the address of a local variable. Warn if so. */
6369 switch (TREE_CODE (inner
))
6371 case NOP_EXPR
: case NON_LVALUE_EXPR
: case CONVERT_EXPR
:
6373 inner
= TREE_OPERAND (inner
, 0);
6377 /* If the second operand of the MINUS_EXPR has a pointer
6378 type (or is converted from it), this may be valid, so
6379 don't give a warning. */
6381 tree op1
= TREE_OPERAND (inner
, 1);
6383 while (! POINTER_TYPE_P (TREE_TYPE (op1
))
6384 && (TREE_CODE (op1
) == NOP_EXPR
6385 || TREE_CODE (op1
) == NON_LVALUE_EXPR
6386 || TREE_CODE (op1
) == CONVERT_EXPR
))
6387 op1
= TREE_OPERAND (op1
, 0);
6389 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
6392 inner
= TREE_OPERAND (inner
, 0);
6397 inner
= TREE_OPERAND (inner
, 0);
6399 while (TREE_CODE_CLASS (TREE_CODE (inner
)) == 'r'
6400 && TREE_CODE (inner
) != INDIRECT_REF
)
6401 inner
= TREE_OPERAND (inner
, 0);
6404 && ! DECL_EXTERNAL (inner
)
6405 && ! TREE_STATIC (inner
)
6406 && DECL_CONTEXT (inner
) == current_function_decl
)
6407 warning ("function returns address of local variable");
6417 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
6420 return add_stmt (build_stmt (RETURN_EXPR
, retval
));
6424 /* The SWITCH_STMT being built. */
6427 /* The original type of the testing expression, i.e. before the
6428 default conversion is applied. */
6431 /* A splay-tree mapping the low element of a case range to the high
6432 element, or NULL_TREE if there is no high element. Used to
6433 determine whether or not a new case label duplicates an old case
6434 label. We need a tree, rather than simply a hash table, because
6435 of the GNU case range extension. */
6438 /* The next node on the stack. */
6439 struct c_switch
*next
;
6442 /* A stack of the currently active switch statements. The innermost
6443 switch statement is on the top of the stack. There is no need to
6444 mark the stack for garbage collection because it is only active
6445 during the processing of the body of a function, and we never
6446 collect at that point. */
6448 struct c_switch
*c_switch_stack
;
6450 /* Start a C switch statement, testing expression EXP. Return the new
6454 c_start_case (tree exp
)
6456 enum tree_code code
;
6457 tree type
, orig_type
= error_mark_node
;
6458 struct c_switch
*cs
;
6460 if (exp
!= error_mark_node
)
6462 code
= TREE_CODE (TREE_TYPE (exp
));
6463 orig_type
= TREE_TYPE (exp
);
6465 if (! INTEGRAL_TYPE_P (orig_type
)
6466 && code
!= ERROR_MARK
)
6468 error ("switch quantity not an integer");
6469 exp
= integer_zero_node
;
6473 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
6475 if (warn_traditional
&& !in_system_header
6476 && (type
== long_integer_type_node
6477 || type
== long_unsigned_type_node
))
6478 warning ("%<long%> switch expression not converted to "
6479 "%<int%> in ISO C");
6481 exp
= default_conversion (exp
);
6482 type
= TREE_TYPE (exp
);
6486 /* Add this new SWITCH_STMT to the stack. */
6487 cs
= XNEW (struct c_switch
);
6488 cs
->switch_stmt
= build_stmt ((enum tree_code
) SWITCH_STMT
, exp
, NULL_TREE
,
6490 cs
->orig_type
= orig_type
;
6491 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
6492 cs
->next
= c_switch_stack
;
6493 c_switch_stack
= cs
;
6495 return add_stmt (cs
->switch_stmt
);
6498 /* Process a case label. */
6501 do_case (tree low_value
, tree high_value
)
6503 tree label
= NULL_TREE
;
6507 label
= c_add_case_label (c_switch_stack
->cases
,
6508 SWITCH_COND (c_switch_stack
->switch_stmt
),
6509 c_switch_stack
->orig_type
,
6510 low_value
, high_value
);
6511 if (label
== error_mark_node
)
6515 error ("case label not within a switch statement");
6517 error ("%<default%> label not within a switch statement");
6522 /* Finish the switch statement. */
6525 c_finish_case (tree body
)
6527 struct c_switch
*cs
= c_switch_stack
;
6529 SWITCH_BODY (cs
->switch_stmt
) = body
;
6531 /* Emit warnings as needed. */
6532 c_do_switch_warnings (cs
->cases
, cs
->switch_stmt
);
6534 /* Pop the stack. */
6535 c_switch_stack
= cs
->next
;
6536 splay_tree_delete (cs
->cases
);
6540 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6541 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6542 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6543 statement, and was not surrounded with parenthesis. */
6546 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
6547 tree else_block
, bool nested_if
)
6551 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6552 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
6554 tree inner_if
= then_block
;
6556 /* We know from the grammar productions that there is an IF nested
6557 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6558 it might not be exactly THEN_BLOCK, but should be the last
6559 non-container statement within. */
6561 switch (TREE_CODE (inner_if
))
6566 inner_if
= BIND_EXPR_BODY (inner_if
);
6568 case STATEMENT_LIST
:
6569 inner_if
= expr_last (then_block
);
6571 case TRY_FINALLY_EXPR
:
6572 case TRY_CATCH_EXPR
:
6573 inner_if
= TREE_OPERAND (inner_if
, 0);
6580 if (COND_EXPR_ELSE (inner_if
))
6581 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6585 /* Diagnose ";" via the special empty statement node that we create. */
6588 if (TREE_CODE (then_block
) == NOP_EXPR
&& !TREE_TYPE (then_block
))
6591 warning ("%Hempty body in an if-statement",
6592 EXPR_LOCUS (then_block
));
6593 then_block
= alloc_stmt_list ();
6596 && TREE_CODE (else_block
) == NOP_EXPR
6597 && !TREE_TYPE (else_block
))
6599 warning ("%Hempty body in an else-statement",
6600 EXPR_LOCUS (else_block
));
6601 else_block
= alloc_stmt_list ();
6605 stmt
= build3 (COND_EXPR
, NULL_TREE
, cond
, then_block
, else_block
);
6606 SET_EXPR_LOCATION (stmt
, if_locus
);
6610 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6611 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6612 is false for DO loops. INCR is the FOR increment expression. BODY is
6613 the statement controlled by the loop. BLAB is the break label. CLAB is
6614 the continue label. Everything is allowed to be NULL. */
6617 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
6618 tree blab
, tree clab
, bool cond_is_first
)
6620 tree entry
= NULL
, exit
= NULL
, t
;
6622 /* Detect do { ... } while (0) and don't generate loop construct. */
6623 if (cond
&& !cond_is_first
&& integer_zerop (cond
))
6625 if (cond_is_first
|| cond
)
6627 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
6629 /* If we have an exit condition, then we build an IF with gotos either
6630 out of the loop, or to the top of it. If there's no exit condition,
6631 then we just build a jump back to the top. */
6632 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
6636 /* Canonicalize the loop condition to the end. This means
6637 generating a branch to the loop condition. Reuse the
6638 continue label, if possible. */
6643 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
6644 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
6647 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
6648 SET_EXPR_LOCATION (t
, start_locus
);
6652 t
= build_and_jump (&blab
);
6653 exit
= build3 (COND_EXPR
, void_type_node
, cond
, exit
, t
);
6656 SET_EXPR_LOCATION (exit
, start_locus
);
6658 SET_EXPR_LOCATION (exit
, input_location
);
6667 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
6675 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
6679 c_finish_bc_stmt (tree
*label_p
, bool is_break
)
6681 tree label
= *label_p
;
6684 *label_p
= label
= create_artificial_label ();
6685 else if (TREE_CODE (label
) != LABEL_DECL
)
6688 error ("break statement not within loop or switch");
6690 error ("continue statement not within a loop");
6694 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
6697 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6700 emit_side_effect_warnings (tree expr
)
6702 if (expr
== error_mark_node
)
6704 else if (!TREE_SIDE_EFFECTS (expr
))
6706 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
6707 warning ("%Hstatement with no effect",
6708 EXPR_HAS_LOCATION (expr
) ? EXPR_LOCUS (expr
) : &input_location
);
6710 else if (warn_unused_value
)
6711 warn_if_unused_value (expr
, input_location
);
6714 /* Process an expression as if it were a complete statement. Emit
6715 diagnostics, but do not call ADD_STMT. */
6718 c_process_expr_stmt (tree expr
)
6723 /* Do default conversion if safe and possibly important,
6724 in case within ({...}). */
6725 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
6726 && (flag_isoc99
|| lvalue_p (expr
)))
6727 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
6728 expr
= default_conversion (expr
);
6730 if (warn_sequence_point
)
6731 verify_sequence_points (expr
);
6733 if (TREE_TYPE (expr
) != error_mark_node
6734 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
6735 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
6736 error ("expression statement has incomplete type");
6738 /* If we're not processing a statement expression, warn about unused values.
6739 Warnings for statement expressions will be emitted later, once we figure
6740 out which is the result. */
6741 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
6742 && (extra_warnings
|| warn_unused_value
))
6743 emit_side_effect_warnings (expr
);
6745 /* If the expression is not of a type to which we cannot assign a line
6746 number, wrap the thing in a no-op NOP_EXPR. */
6747 if (DECL_P (expr
) || TREE_CODE_CLASS (TREE_CODE (expr
)) == 'c')
6748 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
6751 SET_EXPR_LOCATION (expr
, input_location
);
6756 /* Emit an expression as a statement. */
6759 c_finish_expr_stmt (tree expr
)
6762 return add_stmt (c_process_expr_stmt (expr
));
6767 /* Do the opposite and emit a statement as an expression. To begin,
6768 create a new binding level and return it. */
6771 c_begin_stmt_expr (void)
6775 /* We must force a BLOCK for this level so that, if it is not expanded
6776 later, there is a way to turn off the entire subtree of blocks that
6777 are contained in it. */
6779 ret
= c_begin_compound_stmt (true);
6781 /* Mark the current statement list as belonging to a statement list. */
6782 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
6788 c_finish_stmt_expr (tree body
)
6790 tree last
, type
, tmp
, val
;
6793 body
= c_end_compound_stmt (body
, true);
6795 /* Locate the last statement in BODY. See c_end_compound_stmt
6796 about always returning a BIND_EXPR. */
6797 last_p
= &BIND_EXPR_BODY (body
);
6798 last
= BIND_EXPR_BODY (body
);
6801 if (TREE_CODE (last
) == STATEMENT_LIST
)
6803 tree_stmt_iterator i
;
6805 /* This can happen with degenerate cases like ({ }). No value. */
6806 if (!TREE_SIDE_EFFECTS (last
))
6809 /* If we're supposed to generate side effects warnings, process
6810 all of the statements except the last. */
6811 if (extra_warnings
|| warn_unused_value
)
6813 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
6814 emit_side_effect_warnings (tsi_stmt (i
));
6817 i
= tsi_last (last
);
6818 last_p
= tsi_stmt_ptr (i
);
6822 /* If the end of the list is exception related, then the list was split
6823 by a call to push_cleanup. Continue searching. */
6824 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
6825 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
6827 last_p
= &TREE_OPERAND (last
, 0);
6829 goto continue_searching
;
6832 /* In the case that the BIND_EXPR is not necessary, return the
6833 expression out from inside it. */
6834 if (last
== error_mark_node
6835 || (last
== BIND_EXPR_BODY (body
)
6836 && BIND_EXPR_VARS (body
) == NULL
))
6839 /* Extract the type of said expression. */
6840 type
= TREE_TYPE (last
);
6842 /* If we're not returning a value at all, then the BIND_EXPR that
6843 we already have is a fine expression to return. */
6844 if (!type
|| VOID_TYPE_P (type
))
6847 /* Now that we've located the expression containing the value, it seems
6848 silly to make voidify_wrapper_expr repeat the process. Create a
6849 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6850 tmp
= create_tmp_var_raw (type
, NULL
);
6852 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6853 tree_expr_nonnegative_p giving up immediately. */
6855 if (TREE_CODE (val
) == NOP_EXPR
6856 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
6857 val
= TREE_OPERAND (val
, 0);
6859 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
6860 SET_EXPR_LOCUS (*last_p
, EXPR_LOCUS (last
));
6862 return build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
6865 /* Begin and end compound statements. This is as simple as pushing
6866 and popping new statement lists from the tree. */
6869 c_begin_compound_stmt (bool do_scope
)
6871 tree stmt
= push_stmt_list ();
6878 c_end_compound_stmt (tree stmt
, bool do_scope
)
6884 if (c_dialect_objc ())
6885 objc_clear_super_receiver ();
6886 block
= pop_scope ();
6889 stmt
= pop_stmt_list (stmt
);
6890 stmt
= c_build_bind_expr (block
, stmt
);
6892 /* If this compound statement is nested immediately inside a statement
6893 expression, then force a BIND_EXPR to be created. Otherwise we'll
6894 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6895 STATEMENT_LISTs merge, and thus we can lose track of what statement
6898 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
6899 && TREE_CODE (stmt
) != BIND_EXPR
)
6901 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
6902 TREE_SIDE_EFFECTS (stmt
) = 1;
6908 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6909 when the current scope is exited. EH_ONLY is true when this is not
6910 meant to apply to normal control flow transfer. */
6913 push_cleanup (tree
ARG_UNUSED (decl
), tree cleanup
, bool eh_only
)
6915 enum tree_code code
;
6919 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
6920 stmt
= build_stmt (code
, NULL
, cleanup
);
6922 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
6923 list
= push_stmt_list ();
6924 TREE_OPERAND (stmt
, 0) = list
;
6925 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
6928 /* Build a binary-operation expression without default conversions.
6929 CODE is the kind of expression to build.
6930 This function differs from `build' in several ways:
6931 the data type of the result is computed and recorded in it,
6932 warnings are generated if arg data types are invalid,
6933 special handling for addition and subtraction of pointers is known,
6934 and some optimization is done (operations on narrow ints
6935 are done in the narrower type when that gives the same result).
6936 Constant folding is also done before the result is returned.
6938 Note that the operands will never have enumeral types, or function
6939 or array types, because either they will have the default conversions
6940 performed or they have both just been converted to some other type in which
6941 the arithmetic is to be done. */
6944 build_binary_op (enum tree_code code
, tree orig_op0
, tree orig_op1
,
6948 enum tree_code code0
, code1
;
6951 /* Expression code to give to the expression when it is built.
6952 Normally this is CODE, which is what the caller asked for,
6953 but in some special cases we change it. */
6954 enum tree_code resultcode
= code
;
6956 /* Data type in which the computation is to be performed.
6957 In the simplest cases this is the common type of the arguments. */
6958 tree result_type
= NULL
;
6960 /* Nonzero means operands have already been type-converted
6961 in whatever way is necessary.
6962 Zero means they need to be converted to RESULT_TYPE. */
6965 /* Nonzero means create the expression with this type, rather than
6967 tree build_type
= 0;
6969 /* Nonzero means after finally constructing the expression
6970 convert it to this type. */
6971 tree final_type
= 0;
6973 /* Nonzero if this is an operation like MIN or MAX which can
6974 safely be computed in short if both args are promoted shorts.
6975 Also implies COMMON.
6976 -1 indicates a bitwise operation; this makes a difference
6977 in the exact conditions for when it is safe to do the operation
6978 in a narrower mode. */
6981 /* Nonzero if this is a comparison operation;
6982 if both args are promoted shorts, compare the original shorts.
6983 Also implies COMMON. */
6984 int short_compare
= 0;
6986 /* Nonzero if this is a right-shift operation, which can be computed on the
6987 original short and then promoted if the operand is a promoted short. */
6988 int short_shift
= 0;
6990 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6995 op0
= default_conversion (orig_op0
);
6996 op1
= default_conversion (orig_op1
);
7004 type0
= TREE_TYPE (op0
);
7005 type1
= TREE_TYPE (op1
);
7007 /* The expression codes of the data types of the arguments tell us
7008 whether the arguments are integers, floating, pointers, etc. */
7009 code0
= TREE_CODE (type0
);
7010 code1
= TREE_CODE (type1
);
7012 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7013 STRIP_TYPE_NOPS (op0
);
7014 STRIP_TYPE_NOPS (op1
);
7016 /* If an error was already reported for one of the arguments,
7017 avoid reporting another error. */
7019 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
7020 return error_mark_node
;
7025 /* Handle the pointer + int case. */
7026 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7027 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
7028 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
7029 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
7035 /* Subtraction of two similar pointers.
7036 We must subtract them as integers, then divide by object size. */
7037 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
7038 && comp_target_types (type0
, type1
, 1))
7039 return pointer_diff (op0
, op1
);
7040 /* Handle pointer minus int. Just like pointer plus int. */
7041 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7042 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
7051 case TRUNC_DIV_EXPR
:
7053 case FLOOR_DIV_EXPR
:
7054 case ROUND_DIV_EXPR
:
7055 case EXACT_DIV_EXPR
:
7056 /* Floating point division by zero is a legitimate way to obtain
7057 infinities and NaNs. */
7058 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
7059 warning ("division by zero");
7061 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
7062 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
7063 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
7064 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
7066 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
7067 code0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
7068 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
7069 code1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
7071 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
7072 resultcode
= RDIV_EXPR
;
7074 /* Although it would be tempting to shorten always here, that
7075 loses on some targets, since the modulo instruction is
7076 undefined if the quotient can't be represented in the
7077 computation mode. We shorten only if unsigned or if
7078 dividing by something we know != -1. */
7079 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
7080 || (TREE_CODE (op1
) == INTEGER_CST
7081 && ! integer_all_onesp (op1
)));
7089 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7091 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
7095 case TRUNC_MOD_EXPR
:
7096 case FLOOR_MOD_EXPR
:
7097 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
7098 warning ("division by zero");
7100 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7102 /* Although it would be tempting to shorten always here, that loses
7103 on some targets, since the modulo instruction is undefined if the
7104 quotient can't be represented in the computation mode. We shorten
7105 only if unsigned or if dividing by something we know != -1. */
7106 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
7107 || (TREE_CODE (op1
) == INTEGER_CST
7108 && ! integer_all_onesp (op1
)));
7113 case TRUTH_ANDIF_EXPR
:
7114 case TRUTH_ORIF_EXPR
:
7115 case TRUTH_AND_EXPR
:
7117 case TRUTH_XOR_EXPR
:
7118 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
7119 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
7120 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
7121 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
7123 /* Result of these operations is always an int,
7124 but that does not mean the operands should be
7125 converted to ints! */
7126 result_type
= integer_type_node
;
7127 op0
= lang_hooks
.truthvalue_conversion (op0
);
7128 op1
= lang_hooks
.truthvalue_conversion (op1
);
7133 /* Shift operations: result has same type as first operand;
7134 always convert second operand to int.
7135 Also set SHORT_SHIFT if shifting rightward. */
7138 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7140 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
7142 if (tree_int_cst_sgn (op1
) < 0)
7143 warning ("right shift count is negative");
7146 if (! integer_zerop (op1
))
7149 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
7150 warning ("right shift count >= width of type");
7154 /* Use the type of the value to be shifted. */
7155 result_type
= type0
;
7156 /* Convert the shift-count to an integer, regardless of size
7157 of value being shifted. */
7158 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
7159 op1
= convert (integer_type_node
, op1
);
7160 /* Avoid converting op1 to result_type later. */
7166 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7168 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
7170 if (tree_int_cst_sgn (op1
) < 0)
7171 warning ("left shift count is negative");
7173 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
7174 warning ("left shift count >= width of type");
7177 /* Use the type of the value to be shifted. */
7178 result_type
= type0
;
7179 /* Convert the shift-count to an integer, regardless of size
7180 of value being shifted. */
7181 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
7182 op1
= convert (integer_type_node
, op1
);
7183 /* Avoid converting op1 to result_type later. */
7190 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7192 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
7194 if (tree_int_cst_sgn (op1
) < 0)
7195 warning ("shift count is negative");
7196 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
7197 warning ("shift count >= width of type");
7200 /* Use the type of the value to be shifted. */
7201 result_type
= type0
;
7202 /* Convert the shift-count to an integer, regardless of size
7203 of value being shifted. */
7204 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
7205 op1
= convert (integer_type_node
, op1
);
7206 /* Avoid converting op1 to result_type later. */
7213 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
7214 warning ("comparing floating point with == or != is unsafe");
7215 /* Result of comparison is always int,
7216 but don't convert the args to int! */
7217 build_type
= integer_type_node
;
7218 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
7219 || code0
== COMPLEX_TYPE
)
7220 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
7221 || code1
== COMPLEX_TYPE
))
7223 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
7225 tree tt0
= TREE_TYPE (type0
);
7226 tree tt1
= TREE_TYPE (type1
);
7227 /* Anything compares with void *. void * compares with anything.
7228 Otherwise, the targets must be compatible
7229 and both must be object or both incomplete. */
7230 if (comp_target_types (type0
, type1
, 1))
7231 result_type
= common_pointer_type (type0
, type1
);
7232 else if (VOID_TYPE_P (tt0
))
7234 /* op0 != orig_op0 detects the case of something
7235 whose value is 0 but which isn't a valid null ptr const. */
7236 if (pedantic
&& (!integer_zerop (op0
) || op0
!= orig_op0
)
7237 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
7238 pedwarn ("ISO C forbids comparison of %<void *%>"
7239 " with function pointer");
7241 else if (VOID_TYPE_P (tt1
))
7243 if (pedantic
&& (!integer_zerop (op1
) || op1
!= orig_op1
)
7244 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
7245 pedwarn ("ISO C forbids comparison of %<void *%>"
7246 " with function pointer");
7249 pedwarn ("comparison of distinct pointer types lacks a cast");
7251 if (result_type
== NULL_TREE
)
7252 result_type
= ptr_type_node
;
7254 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
7255 && integer_zerop (op1
))
7256 result_type
= type0
;
7257 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
7258 && integer_zerop (op0
))
7259 result_type
= type1
;
7260 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7262 result_type
= type0
;
7263 pedwarn ("comparison between pointer and integer");
7265 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
7267 result_type
= type1
;
7268 pedwarn ("comparison between pointer and integer");
7274 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
7275 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
7277 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
7279 if (comp_target_types (type0
, type1
, 1))
7281 result_type
= common_pointer_type (type0
, type1
);
7283 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
7284 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7288 result_type
= ptr_type_node
;
7289 pedwarn ("comparison of distinct pointer types lacks a cast");
7298 build_type
= integer_type_node
;
7299 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
7300 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
7302 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
7304 if (comp_target_types (type0
, type1
, 1))
7306 result_type
= common_pointer_type (type0
, type1
);
7307 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
7308 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
7309 pedwarn ("comparison of complete and incomplete pointers");
7311 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
7312 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7316 result_type
= ptr_type_node
;
7317 pedwarn ("comparison of distinct pointer types lacks a cast");
7320 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
7321 && integer_zerop (op1
))
7323 result_type
= type0
;
7324 if (pedantic
|| extra_warnings
)
7325 pedwarn ("ordered comparison of pointer with integer zero");
7327 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
7328 && integer_zerop (op0
))
7330 result_type
= type1
;
7332 pedwarn ("ordered comparison of pointer with integer zero");
7334 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7336 result_type
= type0
;
7337 pedwarn ("comparison between pointer and integer");
7339 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
7341 result_type
= type1
;
7342 pedwarn ("comparison between pointer and integer");
7346 case UNORDERED_EXPR
:
7354 build_type
= integer_type_node
;
7355 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
7357 error ("unordered comparison on non-floating point argument");
7358 return error_mark_node
;
7367 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
7368 return error_mark_node
;
7370 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
7371 || code0
== VECTOR_TYPE
)
7373 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
7374 || code1
== VECTOR_TYPE
))
7376 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
7378 if (shorten
|| common
|| short_compare
)
7379 result_type
= common_type (type0
, type1
);
7381 /* For certain operations (which identify themselves by shorten != 0)
7382 if both args were extended from the same smaller type,
7383 do the arithmetic in that type and then extend.
7385 shorten !=0 and !=1 indicates a bitwise operation.
7386 For them, this optimization is safe only if
7387 both args are zero-extended or both are sign-extended.
7388 Otherwise, we might change the result.
7389 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7390 but calculated in (unsigned short) it would be (unsigned short)-1. */
7392 if (shorten
&& none_complex
)
7394 int unsigned0
, unsigned1
;
7395 tree arg0
= get_narrower (op0
, &unsigned0
);
7396 tree arg1
= get_narrower (op1
, &unsigned1
);
7397 /* UNS is 1 if the operation to be done is an unsigned one. */
7398 int uns
= TYPE_UNSIGNED (result_type
);
7401 final_type
= result_type
;
7403 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7404 but it *requires* conversion to FINAL_TYPE. */
7406 if ((TYPE_PRECISION (TREE_TYPE (op0
))
7407 == TYPE_PRECISION (TREE_TYPE (arg0
)))
7408 && TREE_TYPE (op0
) != final_type
)
7409 unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
7410 if ((TYPE_PRECISION (TREE_TYPE (op1
))
7411 == TYPE_PRECISION (TREE_TYPE (arg1
)))
7412 && TREE_TYPE (op1
) != final_type
)
7413 unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
7415 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7417 /* For bitwise operations, signedness of nominal type
7418 does not matter. Consider only how operands were extended. */
7422 /* Note that in all three cases below we refrain from optimizing
7423 an unsigned operation on sign-extended args.
7424 That would not be valid. */
7426 /* Both args variable: if both extended in same way
7427 from same width, do it in that width.
7428 Do it unsigned if args were zero-extended. */
7429 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
7430 < TYPE_PRECISION (result_type
))
7431 && (TYPE_PRECISION (TREE_TYPE (arg1
))
7432 == TYPE_PRECISION (TREE_TYPE (arg0
)))
7433 && unsigned0
== unsigned1
7434 && (unsigned0
|| !uns
))
7436 = c_common_signed_or_unsigned_type
7437 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
7438 else if (TREE_CODE (arg0
) == INTEGER_CST
7439 && (unsigned1
|| !uns
)
7440 && (TYPE_PRECISION (TREE_TYPE (arg1
))
7441 < TYPE_PRECISION (result_type
))
7443 = c_common_signed_or_unsigned_type (unsigned1
,
7445 int_fits_type_p (arg0
, type
)))
7447 else if (TREE_CODE (arg1
) == INTEGER_CST
7448 && (unsigned0
|| !uns
)
7449 && (TYPE_PRECISION (TREE_TYPE (arg0
))
7450 < TYPE_PRECISION (result_type
))
7452 = c_common_signed_or_unsigned_type (unsigned0
,
7454 int_fits_type_p (arg1
, type
)))
7458 /* Shifts can be shortened if shifting right. */
7463 tree arg0
= get_narrower (op0
, &unsigned_arg
);
7465 final_type
= result_type
;
7467 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
7468 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
7470 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
7471 /* We can shorten only if the shift count is less than the
7472 number of bits in the smaller type size. */
7473 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
7474 /* We cannot drop an unsigned shift after sign-extension. */
7475 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
7477 /* Do an unsigned shift if the operand was zero-extended. */
7479 = c_common_signed_or_unsigned_type (unsigned_arg
,
7481 /* Convert value-to-be-shifted to that type. */
7482 if (TREE_TYPE (op0
) != result_type
)
7483 op0
= convert (result_type
, op0
);
7488 /* Comparison operations are shortened too but differently.
7489 They identify themselves by setting short_compare = 1. */
7493 /* Don't write &op0, etc., because that would prevent op0
7494 from being kept in a register.
7495 Instead, make copies of the our local variables and
7496 pass the copies by reference, then copy them back afterward. */
7497 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
7498 enum tree_code xresultcode
= resultcode
;
7500 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
7505 op0
= xop0
, op1
= xop1
;
7507 resultcode
= xresultcode
;
7509 if (warn_sign_compare
&& skip_evaluation
== 0)
7511 int op0_signed
= ! TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
7512 int op1_signed
= ! TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
7513 int unsignedp0
, unsignedp1
;
7514 tree primop0
= get_narrower (op0
, &unsignedp0
);
7515 tree primop1
= get_narrower (op1
, &unsignedp1
);
7519 STRIP_TYPE_NOPS (xop0
);
7520 STRIP_TYPE_NOPS (xop1
);
7522 /* Give warnings for comparisons between signed and unsigned
7523 quantities that may fail.
7525 Do the checking based on the original operand trees, so that
7526 casts will be considered, but default promotions won't be.
7528 Do not warn if the comparison is being done in a signed type,
7529 since the signed type will only be chosen if it can represent
7530 all the values of the unsigned type. */
7531 if (! TYPE_UNSIGNED (result_type
))
7533 /* Do not warn if both operands are the same signedness. */
7534 else if (op0_signed
== op1_signed
)
7541 sop
= xop0
, uop
= xop1
;
7543 sop
= xop1
, uop
= xop0
;
7545 /* Do not warn if the signed quantity is an
7546 unsuffixed integer literal (or some static
7547 constant expression involving such literals or a
7548 conditional expression involving such literals)
7549 and it is non-negative. */
7550 if (tree_expr_nonnegative_p (sop
))
7552 /* Do not warn if the comparison is an equality operation,
7553 the unsigned quantity is an integral constant, and it
7554 would fit in the result if the result were signed. */
7555 else if (TREE_CODE (uop
) == INTEGER_CST
7556 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
7558 (uop
, c_common_signed_type (result_type
)))
7560 /* Do not warn if the unsigned quantity is an enumeration
7561 constant and its maximum value would fit in the result
7562 if the result were signed. */
7563 else if (TREE_CODE (uop
) == INTEGER_CST
7564 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
7566 (TYPE_MAX_VALUE (TREE_TYPE(uop
)),
7567 c_common_signed_type (result_type
)))
7570 warning ("comparison between signed and unsigned");
7573 /* Warn if two unsigned values are being compared in a size
7574 larger than their original size, and one (and only one) is the
7575 result of a `~' operator. This comparison will always fail.
7577 Also warn if one operand is a constant, and the constant
7578 does not have all bits set that are set in the ~ operand
7579 when it is extended. */
7581 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7582 != (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
7584 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7585 primop0
= get_narrower (TREE_OPERAND (primop0
, 0),
7588 primop1
= get_narrower (TREE_OPERAND (primop1
, 0),
7591 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
7594 HOST_WIDE_INT constant
, mask
;
7595 int unsignedp
, bits
;
7597 if (host_integerp (primop0
, 0))
7600 unsignedp
= unsignedp1
;
7601 constant
= tree_low_cst (primop0
, 0);
7606 unsignedp
= unsignedp0
;
7607 constant
= tree_low_cst (primop1
, 0);
7610 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
7611 if (bits
< TYPE_PRECISION (result_type
)
7612 && bits
< HOST_BITS_PER_WIDE_INT
&& unsignedp
)
7614 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
7615 if ((mask
& constant
) != mask
)
7616 warning ("comparison of promoted ~unsigned with constant");
7619 else if (unsignedp0
&& unsignedp1
7620 && (TYPE_PRECISION (TREE_TYPE (primop0
))
7621 < TYPE_PRECISION (result_type
))
7622 && (TYPE_PRECISION (TREE_TYPE (primop1
))
7623 < TYPE_PRECISION (result_type
)))
7624 warning ("comparison of promoted ~unsigned with unsigned");
7630 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7631 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7632 Then the expression will be built.
7633 It will be given type FINAL_TYPE if that is nonzero;
7634 otherwise, it will be given type RESULT_TYPE. */
7638 binary_op_error (code
);
7639 return error_mark_node
;
7644 if (TREE_TYPE (op0
) != result_type
)
7645 op0
= convert (result_type
, op0
);
7646 if (TREE_TYPE (op1
) != result_type
)
7647 op1
= convert (result_type
, op1
);
7649 /* This can happen if one operand has a vector type, and the other
7650 has a different type. */
7651 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
7652 return error_mark_node
;
7655 if (build_type
== NULL_TREE
)
7656 build_type
= result_type
;
7659 tree result
= build2 (resultcode
, build_type
, op0
, op1
);
7661 /* Treat expressions in initializers specially as they can't trap. */
7662 result
= require_constant_value
? fold_initializer (result
)
7665 if (final_type
!= 0)
7666 result
= convert (final_type
, result
);