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 tcc_declaration
:
748 t1
= DECL_CONTEXT (t1
); break;
750 t1
= TYPE_CONTEXT (t1
); break;
751 case tcc_exceptional
:
752 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
753 default: gcc_unreachable ();
756 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
757 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
759 case tcc_declaration
:
760 t2
= DECL_CONTEXT (t2
); break;
762 t2
= TYPE_CONTEXT (t2
); break;
763 case tcc_exceptional
:
764 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
765 default: gcc_unreachable ();
771 /* The C standard says that two structures in different translation
772 units are compatible with each other only if the types of their
773 fields are compatible (among other things). So, consider two copies
774 of this structure: */
776 struct tagged_tu_seen
{
777 const struct tagged_tu_seen
* next
;
782 /* Can they be compatible with each other? We choose to break the
783 recursion by allowing those types to be compatible. */
785 static const struct tagged_tu_seen
* tagged_tu_seen_base
;
787 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
788 compatible. If the two types are not the same (which has been
789 checked earlier), this can only happen when multiple translation
790 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
794 tagged_types_tu_compatible_p (tree t1
, tree t2
)
797 bool needs_warning
= false;
799 /* We have to verify that the tags of the types are the same. This
800 is harder than it looks because this may be a typedef, so we have
801 to go look at the original type. It may even be a typedef of a
803 In the case of compiler-created builtin structs the TYPE_DECL
804 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
805 while (TYPE_NAME (t1
)
806 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
807 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
808 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
810 while (TYPE_NAME (t2
)
811 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
812 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
813 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
815 /* C90 didn't have the requirement that the two tags be the same. */
816 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
819 /* C90 didn't say what happened if one or both of the types were
820 incomplete; we choose to follow C99 rules here, which is that they
822 if (TYPE_SIZE (t1
) == NULL
823 || TYPE_SIZE (t2
) == NULL
)
827 const struct tagged_tu_seen
* tts_i
;
828 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
829 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
833 switch (TREE_CODE (t1
))
838 /* Speed up the case where the type values are in the same order. */
839 tree tv1
= TYPE_VALUES (t1
);
840 tree tv2
= TYPE_VALUES (t2
);
845 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
847 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
849 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
853 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
855 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
858 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
861 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
863 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
865 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
873 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
876 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= TREE_CHAIN (s1
))
879 struct tagged_tu_seen tts
;
881 tts
.next
= tagged_tu_seen_base
;
884 tagged_tu_seen_base
= &tts
;
886 if (DECL_NAME (s1
) != NULL
)
887 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= TREE_CHAIN (s2
))
888 if (DECL_NAME (s1
) == DECL_NAME (s2
))
891 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
));
895 needs_warning
= true;
897 if (TREE_CODE (s1
) == FIELD_DECL
898 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
899 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
905 tagged_tu_seen_base
= tts
.next
;
909 return needs_warning
? 2 : 1;
914 struct tagged_tu_seen tts
;
916 tts
.next
= tagged_tu_seen_base
;
919 tagged_tu_seen_base
= &tts
;
921 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
923 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
926 if (TREE_CODE (s1
) != TREE_CODE (s2
)
927 || DECL_NAME (s1
) != DECL_NAME (s2
))
929 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
));
933 needs_warning
= true;
935 if (TREE_CODE (s1
) == FIELD_DECL
936 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
937 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
940 tagged_tu_seen_base
= tts
.next
;
943 return needs_warning
? 2 : 1;
951 /* Return 1 if two function types F1 and F2 are compatible.
952 If either type specifies no argument types,
953 the other must specify a fixed number of self-promoting arg types.
954 Otherwise, if one type specifies only the number of arguments,
955 the other must specify that number of self-promoting arg types.
956 Otherwise, the argument types must match. */
959 function_types_compatible_p (tree f1
, tree f2
)
962 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
967 ret1
= TREE_TYPE (f1
);
968 ret2
= TREE_TYPE (f2
);
970 /* 'volatile' qualifiers on a function's return type used to mean
971 the function is noreturn. */
972 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
973 pedwarn ("function return types not compatible due to %<volatile%>");
974 if (TYPE_VOLATILE (ret1
))
975 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
976 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
977 if (TYPE_VOLATILE (ret2
))
978 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
979 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
980 val
= comptypes (ret1
, ret2
);
984 args1
= TYPE_ARG_TYPES (f1
);
985 args2
= TYPE_ARG_TYPES (f2
);
987 /* An unspecified parmlist matches any specified parmlist
988 whose argument types don't need default promotions. */
992 if (!self_promoting_args_p (args2
))
994 /* If one of these types comes from a non-prototype fn definition,
995 compare that with the other type's arglist.
996 If they don't match, ask for a warning (but no error). */
997 if (TYPE_ACTUAL_ARG_TYPES (f1
)
998 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
)))
1004 if (!self_promoting_args_p (args1
))
1006 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1007 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
)))
1012 /* Both types have argument lists: compare them and propagate results. */
1013 val1
= type_lists_compatible_p (args1
, args2
);
1014 return val1
!= 1 ? val1
: val
;
1017 /* Check two lists of types for compatibility,
1018 returning 0 for incompatible, 1 for compatible,
1019 or 2 for compatible with warning. */
1022 type_lists_compatible_p (tree args1
, tree args2
)
1024 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1030 if (args1
== 0 && args2
== 0)
1032 /* If one list is shorter than the other,
1033 they fail to match. */
1034 if (args1
== 0 || args2
== 0)
1036 /* A null pointer instead of a type
1037 means there is supposed to be an argument
1038 but nothing is specified about what type it has.
1039 So match anything that self-promotes. */
1040 if (TREE_VALUE (args1
) == 0)
1042 if (c_type_promotes_to (TREE_VALUE (args2
)) != TREE_VALUE (args2
))
1045 else if (TREE_VALUE (args2
) == 0)
1047 if (c_type_promotes_to (TREE_VALUE (args1
)) != TREE_VALUE (args1
))
1050 /* If one of the lists has an error marker, ignore this arg. */
1051 else if (TREE_CODE (TREE_VALUE (args1
)) == ERROR_MARK
1052 || TREE_CODE (TREE_VALUE (args2
)) == ERROR_MARK
)
1054 else if (! (newval
= comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1
)),
1055 TYPE_MAIN_VARIANT (TREE_VALUE (args2
)))))
1057 /* Allow wait (union {union wait *u; int *i} *)
1058 and wait (union wait *) to be compatible. */
1059 if (TREE_CODE (TREE_VALUE (args1
)) == UNION_TYPE
1060 && (TYPE_NAME (TREE_VALUE (args1
)) == 0
1061 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1
)))
1062 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1
))) == INTEGER_CST
1063 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1
)),
1064 TYPE_SIZE (TREE_VALUE (args2
))))
1067 for (memb
= TYPE_FIELDS (TREE_VALUE (args1
));
1068 memb
; memb
= TREE_CHAIN (memb
))
1069 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args2
)))
1074 else if (TREE_CODE (TREE_VALUE (args2
)) == UNION_TYPE
1075 && (TYPE_NAME (TREE_VALUE (args2
)) == 0
1076 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2
)))
1077 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2
))) == INTEGER_CST
1078 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2
)),
1079 TYPE_SIZE (TREE_VALUE (args1
))))
1082 for (memb
= TYPE_FIELDS (TREE_VALUE (args2
));
1083 memb
; memb
= TREE_CHAIN (memb
))
1084 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args1
)))
1093 /* comptypes said ok, but record if it said to warn. */
1097 args1
= TREE_CHAIN (args1
);
1098 args2
= TREE_CHAIN (args2
);
1102 /* Compute the size to increment a pointer by. */
1105 c_size_in_bytes (tree type
)
1107 enum tree_code code
= TREE_CODE (type
);
1109 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
1110 return size_one_node
;
1112 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1114 error ("arithmetic on pointer to an incomplete type");
1115 return size_one_node
;
1118 /* Convert in case a char is more than one unit. */
1119 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1120 size_int (TYPE_PRECISION (char_type_node
)
1124 /* Return either DECL or its known constant value (if it has one). */
1127 decl_constant_value (tree decl
)
1129 if (/* Don't change a variable array bound or initial value to a constant
1130 in a place where a variable is invalid. Note that DECL_INITIAL
1131 isn't valid for a PARM_DECL. */
1132 current_function_decl
!= 0
1133 && TREE_CODE (decl
) != PARM_DECL
1134 && ! TREE_THIS_VOLATILE (decl
)
1135 && TREE_READONLY (decl
)
1136 && DECL_INITIAL (decl
) != 0
1137 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1138 /* This is invalid if initial value is not constant.
1139 If it has either a function call, a memory reference,
1140 or a variable, then re-evaluating it could give different results. */
1141 && TREE_CONSTANT (DECL_INITIAL (decl
))
1142 /* Check for cases where this is sub-optimal, even though valid. */
1143 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1144 return DECL_INITIAL (decl
);
1148 /* Return either DECL or its known constant value (if it has one), but
1149 return DECL if pedantic or DECL has mode BLKmode. This is for
1150 bug-compatibility with the old behavior of decl_constant_value
1151 (before GCC 3.0); every use of this function is a bug and it should
1152 be removed before GCC 3.1. It is not appropriate to use pedantic
1153 in a way that affects optimization, and BLKmode is probably not the
1154 right test for avoiding misoptimizations either. */
1157 decl_constant_value_for_broken_optimization (tree decl
)
1159 if (pedantic
|| DECL_MODE (decl
) == BLKmode
)
1162 return decl_constant_value (decl
);
1166 /* Perform the default conversion of arrays and functions to pointers.
1167 Return the result of converting EXP. For any other expression, just
1171 default_function_array_conversion (tree exp
)
1174 tree type
= TREE_TYPE (exp
);
1175 enum tree_code code
= TREE_CODE (type
);
1178 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1181 Do not use STRIP_NOPS here! It will remove conversions from pointer
1182 to integer and cause infinite recursion. */
1184 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1185 || (TREE_CODE (exp
) == NOP_EXPR
1186 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1188 if (TREE_CODE (exp
) == NON_LVALUE_EXPR
)
1190 exp
= TREE_OPERAND (exp
, 0);
1193 if (TREE_NO_WARNING (orig_exp
))
1194 TREE_NO_WARNING (exp
) = 1;
1196 if (code
== FUNCTION_TYPE
)
1198 return build_unary_op (ADDR_EXPR
, exp
, 0);
1200 if (code
== ARRAY_TYPE
)
1203 tree restype
= TREE_TYPE (type
);
1209 if (REFERENCE_CLASS_P (exp
) || DECL_P (exp
))
1211 constp
= TREE_READONLY (exp
);
1212 volatilep
= TREE_THIS_VOLATILE (exp
);
1215 if (TYPE_QUALS (type
) || constp
|| volatilep
)
1217 = c_build_qualified_type (restype
,
1219 | (constp
* TYPE_QUAL_CONST
)
1220 | (volatilep
* TYPE_QUAL_VOLATILE
));
1222 if (TREE_CODE (exp
) == INDIRECT_REF
)
1223 return convert (build_pointer_type (restype
),
1224 TREE_OPERAND (exp
, 0));
1226 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1228 tree op1
= default_conversion (TREE_OPERAND (exp
, 1));
1229 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1230 TREE_OPERAND (exp
, 0), op1
);
1233 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
);
1234 if (!flag_isoc99
&& !lvalue_array_p
)
1236 /* Before C99, non-lvalue arrays do not decay to pointers.
1237 Normally, using such an array would be invalid; but it can
1238 be used correctly inside sizeof or as a statement expression.
1239 Thus, do not give an error here; an error will result later. */
1243 ptrtype
= build_pointer_type (restype
);
1245 if (TREE_CODE (exp
) == VAR_DECL
)
1247 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1248 ADDR_EXPR because it's the best way of representing what
1249 happens in C when we take the address of an array and place
1250 it in a pointer to the element type. */
1251 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1252 if (!c_mark_addressable (exp
))
1253 return error_mark_node
;
1254 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1257 /* This way is better for a COMPONENT_REF since it can
1258 simplify the offset for a component. */
1259 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1260 return convert (ptrtype
, adr
);
1265 /* Perform default promotions for C data used in expressions.
1266 Arrays and functions are converted to pointers;
1267 enumeral types or short or char, to int.
1268 In addition, manifest constants symbols are replaced by their values. */
1271 default_conversion (tree exp
)
1274 tree type
= TREE_TYPE (exp
);
1275 enum tree_code code
= TREE_CODE (type
);
1277 if (code
== FUNCTION_TYPE
|| code
== ARRAY_TYPE
)
1278 return default_function_array_conversion (exp
);
1280 /* Constants can be used directly unless they're not loadable. */
1281 if (TREE_CODE (exp
) == CONST_DECL
)
1282 exp
= DECL_INITIAL (exp
);
1284 /* Replace a nonvolatile const static variable with its value unless
1285 it is an array, in which case we must be sure that taking the
1286 address of the array produces consistent results. */
1287 else if (optimize
&& TREE_CODE (exp
) == VAR_DECL
&& code
!= ARRAY_TYPE
)
1289 exp
= decl_constant_value_for_broken_optimization (exp
);
1290 type
= TREE_TYPE (exp
);
1293 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1296 Do not use STRIP_NOPS here! It will remove conversions from pointer
1297 to integer and cause infinite recursion. */
1299 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1300 || (TREE_CODE (exp
) == NOP_EXPR
1301 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1302 exp
= TREE_OPERAND (exp
, 0);
1304 if (TREE_NO_WARNING (orig_exp
))
1305 TREE_NO_WARNING (exp
) = 1;
1307 /* Normally convert enums to int,
1308 but convert wide enums to something wider. */
1309 if (code
== ENUMERAL_TYPE
)
1311 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
1312 TYPE_PRECISION (integer_type_node
)),
1313 ((TYPE_PRECISION (type
)
1314 >= TYPE_PRECISION (integer_type_node
))
1315 && TYPE_UNSIGNED (type
)));
1317 return convert (type
, exp
);
1320 if (TREE_CODE (exp
) == COMPONENT_REF
1321 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
1322 /* If it's thinner than an int, promote it like a
1323 c_promoting_integer_type_p, otherwise leave it alone. */
1324 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
1325 TYPE_PRECISION (integer_type_node
)))
1326 return convert (integer_type_node
, exp
);
1328 if (c_promoting_integer_type_p (type
))
1330 /* Preserve unsignedness if not really getting any wider. */
1331 if (TYPE_UNSIGNED (type
)
1332 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1333 return convert (unsigned_type_node
, exp
);
1335 return convert (integer_type_node
, exp
);
1338 if (code
== VOID_TYPE
)
1340 error ("void value not ignored as it ought to be");
1341 return error_mark_node
;
1346 /* Look up COMPONENT in a structure or union DECL.
1348 If the component name is not found, returns NULL_TREE. Otherwise,
1349 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1350 stepping down the chain to the component, which is in the last
1351 TREE_VALUE of the list. Normally the list is of length one, but if
1352 the component is embedded within (nested) anonymous structures or
1353 unions, the list steps down the chain to the component. */
1356 lookup_field (tree decl
, tree component
)
1358 tree type
= TREE_TYPE (decl
);
1361 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1362 to the field elements. Use a binary search on this array to quickly
1363 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1364 will always be set for structures which have many elements. */
1366 if (TYPE_LANG_SPECIFIC (type
))
1369 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
1371 field
= TYPE_FIELDS (type
);
1373 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
1374 while (top
- bot
> 1)
1376 half
= (top
- bot
+ 1) >> 1;
1377 field
= field_array
[bot
+half
];
1379 if (DECL_NAME (field
) == NULL_TREE
)
1381 /* Step through all anon unions in linear fashion. */
1382 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
1384 field
= field_array
[bot
++];
1385 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1386 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1388 tree anon
= lookup_field (field
, component
);
1391 return tree_cons (NULL_TREE
, field
, anon
);
1395 /* Entire record is only anon unions. */
1399 /* Restart the binary search, with new lower bound. */
1403 if (DECL_NAME (field
) == component
)
1405 if (DECL_NAME (field
) < component
)
1411 if (DECL_NAME (field_array
[bot
]) == component
)
1412 field
= field_array
[bot
];
1413 else if (DECL_NAME (field
) != component
)
1418 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1420 if (DECL_NAME (field
) == NULL_TREE
1421 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1422 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
1424 tree anon
= lookup_field (field
, component
);
1427 return tree_cons (NULL_TREE
, field
, anon
);
1430 if (DECL_NAME (field
) == component
)
1434 if (field
== NULL_TREE
)
1438 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
1441 /* Make an expression to refer to the COMPONENT field of
1442 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1445 build_component_ref (tree datum
, tree component
)
1447 tree type
= TREE_TYPE (datum
);
1448 enum tree_code code
= TREE_CODE (type
);
1452 if (!objc_is_public (datum
, component
))
1453 return error_mark_node
;
1455 /* See if there is a field or component with name COMPONENT. */
1457 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1459 if (!COMPLETE_TYPE_P (type
))
1461 c_incomplete_type_error (NULL_TREE
, type
);
1462 return error_mark_node
;
1465 field
= lookup_field (datum
, component
);
1469 error ("%s has no member named %qs",
1470 code
== RECORD_TYPE
? "structure" : "union",
1471 IDENTIFIER_POINTER (component
));
1472 return error_mark_node
;
1475 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1476 This might be better solved in future the way the C++ front
1477 end does it - by giving the anonymous entities each a
1478 separate name and type, and then have build_component_ref
1479 recursively call itself. We can't do that here. */
1482 tree subdatum
= TREE_VALUE (field
);
1484 if (TREE_TYPE (subdatum
) == error_mark_node
)
1485 return error_mark_node
;
1487 ref
= build3 (COMPONENT_REF
, TREE_TYPE (subdatum
), datum
, subdatum
,
1489 if (TREE_READONLY (datum
) || TREE_READONLY (subdatum
))
1490 TREE_READONLY (ref
) = 1;
1491 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (subdatum
))
1492 TREE_THIS_VOLATILE (ref
) = 1;
1494 if (TREE_DEPRECATED (subdatum
))
1495 warn_deprecated_use (subdatum
);
1499 field
= TREE_CHAIN (field
);
1505 else if (code
!= ERROR_MARK
)
1506 error ("request for member %qs in something not a structure or union",
1507 IDENTIFIER_POINTER (component
));
1509 return error_mark_node
;
1512 /* Given an expression PTR for a pointer, return an expression
1513 for the value pointed to.
1514 ERRORSTRING is the name of the operator to appear in error messages. */
1517 build_indirect_ref (tree ptr
, const char *errorstring
)
1519 tree pointer
= default_conversion (ptr
);
1520 tree type
= TREE_TYPE (pointer
);
1522 if (TREE_CODE (type
) == POINTER_TYPE
)
1524 if (TREE_CODE (pointer
) == ADDR_EXPR
1525 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
1526 == TREE_TYPE (type
)))
1527 return TREE_OPERAND (pointer
, 0);
1530 tree t
= TREE_TYPE (type
);
1531 tree ref
= build1 (INDIRECT_REF
, TYPE_MAIN_VARIANT (t
), pointer
);
1533 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
1535 error ("dereferencing pointer to incomplete type");
1536 return error_mark_node
;
1538 if (VOID_TYPE_P (t
) && skip_evaluation
== 0)
1539 warning ("dereferencing %<void *%> pointer");
1541 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1542 so that we get the proper error message if the result is used
1543 to assign to. Also, &* is supposed to be a no-op.
1544 And ANSI C seems to specify that the type of the result
1545 should be the const type. */
1546 /* A de-reference of a pointer to const is not a const. It is valid
1547 to change it via some other pointer. */
1548 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1549 TREE_SIDE_EFFECTS (ref
)
1550 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1551 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1555 else if (TREE_CODE (pointer
) != ERROR_MARK
)
1556 error ("invalid type argument of %qs", errorstring
);
1557 return error_mark_node
;
1560 /* This handles expressions of the form "a[i]", which denotes
1563 This is logically equivalent in C to *(a+i), but we may do it differently.
1564 If A is a variable or a member, we generate a primitive ARRAY_REF.
1565 This avoids forcing the array out of registers, and can work on
1566 arrays that are not lvalues (for example, members of structures returned
1570 build_array_ref (tree array
, tree index
)
1574 error ("subscript missing in array reference");
1575 return error_mark_node
;
1578 if (TREE_TYPE (array
) == error_mark_node
1579 || TREE_TYPE (index
) == error_mark_node
)
1580 return error_mark_node
;
1582 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
1586 /* Subscripting with type char is likely to lose
1587 on a machine where chars are signed.
1588 So warn on any machine, but optionally.
1589 Don't warn for unsigned char since that type is safe.
1590 Don't warn for signed char because anyone who uses that
1591 must have done so deliberately. */
1592 if (warn_char_subscripts
1593 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1594 warning ("array subscript has type %<char%>");
1596 /* Apply default promotions *after* noticing character types. */
1597 index
= default_conversion (index
);
1599 /* Require integer *after* promotion, for sake of enums. */
1600 if (TREE_CODE (TREE_TYPE (index
)) != INTEGER_TYPE
)
1602 error ("array subscript is not an integer");
1603 return error_mark_node
;
1606 /* An array that is indexed by a non-constant
1607 cannot be stored in a register; we must be able to do
1608 address arithmetic on its address.
1609 Likewise an array of elements of variable size. */
1610 if (TREE_CODE (index
) != INTEGER_CST
1611 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
1612 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1614 if (!c_mark_addressable (array
))
1615 return error_mark_node
;
1617 /* An array that is indexed by a constant value which is not within
1618 the array bounds cannot be stored in a register either; because we
1619 would get a crash in store_bit_field/extract_bit_field when trying
1620 to access a non-existent part of the register. */
1621 if (TREE_CODE (index
) == INTEGER_CST
1622 && TYPE_DOMAIN (TREE_TYPE (array
))
1623 && ! int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
1625 if (!c_mark_addressable (array
))
1626 return error_mark_node
;
1632 while (TREE_CODE (foo
) == COMPONENT_REF
)
1633 foo
= TREE_OPERAND (foo
, 0);
1634 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
1635 pedwarn ("ISO C forbids subscripting %<register%> array");
1636 else if (! flag_isoc99
&& ! lvalue_p (foo
))
1637 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1640 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1641 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
1642 /* Array ref is const/volatile if the array elements are
1643 or if the array is. */
1644 TREE_READONLY (rval
)
1645 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
1646 | TREE_READONLY (array
));
1647 TREE_SIDE_EFFECTS (rval
)
1648 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1649 | TREE_SIDE_EFFECTS (array
));
1650 TREE_THIS_VOLATILE (rval
)
1651 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1652 /* This was added by rms on 16 Nov 91.
1653 It fixes vol struct foo *a; a->elts[1]
1654 in an inline function.
1655 Hope it doesn't break something else. */
1656 | TREE_THIS_VOLATILE (array
));
1657 return require_complete_type (fold (rval
));
1661 tree ar
= default_conversion (array
);
1662 tree ind
= default_conversion (index
);
1664 /* Do the same warning check as above, but only on the part that's
1665 syntactically the index and only if it is also semantically
1667 if (warn_char_subscripts
1668 && TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
1669 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1670 warning ("subscript has type %<char%>");
1672 /* Put the integer in IND to simplify error checking. */
1673 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
1680 if (ar
== error_mark_node
)
1683 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
1684 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) == FUNCTION_TYPE
)
1686 error ("subscripted value is neither array nor pointer");
1687 return error_mark_node
;
1689 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
1691 error ("array subscript is not an integer");
1692 return error_mark_node
;
1695 return build_indirect_ref (build_binary_op (PLUS_EXPR
, ar
, ind
, 0),
1700 /* Build an external reference to identifier ID. FUN indicates
1701 whether this will be used for a function call. */
1703 build_external_ref (tree id
, int fun
)
1706 tree decl
= lookup_name (id
);
1707 tree objc_ivar
= objc_lookup_ivar (id
);
1709 if (decl
&& decl
!= error_mark_node
)
1711 /* Properly declared variable or function reference. */
1714 else if (decl
!= objc_ivar
&& !DECL_FILE_SCOPE_P (decl
))
1716 warning ("local declaration of %qs hides instance variable",
1717 IDENTIFIER_POINTER (id
));
1726 /* Implicit function declaration. */
1727 ref
= implicitly_declare (id
);
1728 else if (decl
== error_mark_node
)
1729 /* Don't complain about something that's already been
1730 complained about. */
1731 return error_mark_node
;
1734 undeclared_variable (id
);
1735 return error_mark_node
;
1738 if (TREE_TYPE (ref
) == error_mark_node
)
1739 return error_mark_node
;
1741 if (TREE_DEPRECATED (ref
))
1742 warn_deprecated_use (ref
);
1744 if (!skip_evaluation
)
1745 assemble_external (ref
);
1746 TREE_USED (ref
) = 1;
1748 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
1750 if (!in_sizeof
&& !in_typeof
)
1751 C_DECL_USED (ref
) = 1;
1752 else if (DECL_INITIAL (ref
) == 0
1753 && DECL_EXTERNAL (ref
)
1754 && !TREE_PUBLIC (ref
))
1755 record_maybe_used_decl (ref
);
1758 if (TREE_CODE (ref
) == CONST_DECL
)
1760 ref
= DECL_INITIAL (ref
);
1761 TREE_CONSTANT (ref
) = 1;
1762 TREE_INVARIANT (ref
) = 1;
1764 else if (current_function_decl
!= 0
1765 && !DECL_FILE_SCOPE_P (current_function_decl
)
1766 && (TREE_CODE (ref
) == VAR_DECL
1767 || TREE_CODE (ref
) == PARM_DECL
1768 || TREE_CODE (ref
) == FUNCTION_DECL
))
1770 tree context
= decl_function_context (ref
);
1772 if (context
!= 0 && context
!= current_function_decl
)
1773 DECL_NONLOCAL (ref
) = 1;
1779 /* Record details of decls possibly used inside sizeof or typeof. */
1780 struct maybe_used_decl
1784 /* The level seen at (in_sizeof + in_typeof). */
1786 /* The next one at this level or above, or NULL. */
1787 struct maybe_used_decl
*next
;
1790 static struct maybe_used_decl
*maybe_used_decls
;
1792 /* Record that DECL, an undefined static function reference seen
1793 inside sizeof or typeof, might be used if the operand of sizeof is
1794 a VLA type or the operand of typeof is a variably modified
1798 record_maybe_used_decl (tree decl
)
1800 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
1802 t
->level
= in_sizeof
+ in_typeof
;
1803 t
->next
= maybe_used_decls
;
1804 maybe_used_decls
= t
;
1807 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1808 USED is false, just discard them. If it is true, mark them used
1809 (if no longer inside sizeof or typeof) or move them to the next
1810 level up (if still inside sizeof or typeof). */
1813 pop_maybe_used (bool used
)
1815 struct maybe_used_decl
*p
= maybe_used_decls
;
1816 int cur_level
= in_sizeof
+ in_typeof
;
1817 while (p
&& p
->level
> cur_level
)
1822 C_DECL_USED (p
->decl
) = 1;
1824 p
->level
= cur_level
;
1828 if (!used
|| cur_level
== 0)
1829 maybe_used_decls
= p
;
1832 /* Return the result of sizeof applied to EXPR. */
1835 c_expr_sizeof_expr (struct c_expr expr
)
1838 if (expr
.value
== error_mark_node
)
1840 ret
.value
= error_mark_node
;
1841 ret
.original_code
= ERROR_MARK
;
1842 pop_maybe_used (false);
1846 ret
.value
= c_sizeof (TREE_TYPE (expr
.value
));
1847 ret
.original_code
= ERROR_MARK
;
1848 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr
.value
)));
1853 /* Return the result of sizeof applied to T, a structure for the type
1854 name passed to sizeof (rather than the type itself). */
1857 c_expr_sizeof_type (struct c_type_name
*t
)
1861 type
= groktypename (t
);
1862 ret
.value
= c_sizeof (type
);
1863 ret
.original_code
= ERROR_MARK
;
1864 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type
));
1868 /* Build a function call to function FUNCTION with parameters PARAMS.
1869 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1870 TREE_VALUE of each node is a parameter-expression.
1871 FUNCTION's data type may be a function type or a pointer-to-function. */
1874 build_function_call (tree function
, tree params
)
1876 tree fntype
, fundecl
= 0;
1877 tree coerced_params
;
1878 tree name
= NULL_TREE
, result
;
1881 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1882 STRIP_TYPE_NOPS (function
);
1884 /* Convert anything with function type to a pointer-to-function. */
1885 if (TREE_CODE (function
) == FUNCTION_DECL
)
1887 name
= DECL_NAME (function
);
1889 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1890 (because calling an inline function does not mean the function
1891 needs to be separately compiled). */
1892 fntype
= build_type_variant (TREE_TYPE (function
),
1893 TREE_READONLY (function
),
1894 TREE_THIS_VOLATILE (function
));
1896 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
1899 function
= default_conversion (function
);
1901 fntype
= TREE_TYPE (function
);
1903 if (TREE_CODE (fntype
) == ERROR_MARK
)
1904 return error_mark_node
;
1906 if (!(TREE_CODE (fntype
) == POINTER_TYPE
1907 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
1909 error ("called object %qE is not a function", function
);
1910 return error_mark_node
;
1913 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
1914 current_function_returns_abnormally
= 1;
1916 /* fntype now gets the type of function pointed to. */
1917 fntype
= TREE_TYPE (fntype
);
1919 /* Check that the function is called through a compatible prototype.
1920 If it is not, replace the call by a trap, wrapped up in a compound
1921 expression if necessary. This has the nice side-effect to prevent
1922 the tree-inliner from generating invalid assignment trees which may
1923 blow up in the RTL expander later.
1925 ??? This doesn't work for Objective-C because objc_comptypes
1926 refuses to compare function prototypes, yet the compiler appears
1927 to build calls that are flagged as invalid by C's comptypes. */
1928 if (! c_dialect_objc ()
1929 && TREE_CODE (function
) == NOP_EXPR
1930 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
1931 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
1932 && ! comptypes (fntype
, TREE_TYPE (tem
)))
1934 tree return_type
= TREE_TYPE (fntype
);
1935 tree trap
= build_function_call (built_in_decls
[BUILT_IN_TRAP
],
1938 /* This situation leads to run-time undefined behavior. We can't,
1939 therefore, simply error unless we can prove that all possible
1940 executions of the program must execute the code. */
1941 warning ("function called through a non-compatible type");
1943 /* We can, however, treat "undefined" any way we please.
1944 Call abort to encourage the user to fix the program. */
1945 inform ("if this code is reached, the program will abort");
1947 if (VOID_TYPE_P (return_type
))
1953 if (AGGREGATE_TYPE_P (return_type
))
1954 rhs
= build_compound_literal (return_type
,
1955 build_constructor (return_type
,
1958 rhs
= fold (build1 (NOP_EXPR
, return_type
, integer_zero_node
));
1960 return build2 (COMPOUND_EXPR
, return_type
, trap
, rhs
);
1964 /* Convert the parameters to the types declared in the
1965 function prototype, or apply default promotions. */
1968 = convert_arguments (TYPE_ARG_TYPES (fntype
), params
, name
, fundecl
);
1970 /* Check that the arguments to the function are valid. */
1972 check_function_arguments (TYPE_ATTRIBUTES (fntype
), coerced_params
);
1974 result
= build3 (CALL_EXPR
, TREE_TYPE (fntype
),
1975 function
, coerced_params
, NULL_TREE
);
1976 TREE_SIDE_EFFECTS (result
) = 1;
1978 if (require_constant_value
)
1980 result
= fold_initializer (result
);
1982 if (TREE_CONSTANT (result
)
1983 && (name
== NULL_TREE
1984 || strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10) != 0))
1985 pedwarn_init ("initializer element is not constant");
1988 result
= fold (result
);
1990 if (VOID_TYPE_P (TREE_TYPE (result
)))
1992 return require_complete_type (result
);
1995 /* Convert the argument expressions in the list VALUES
1996 to the types in the list TYPELIST. The result is a list of converted
1997 argument expressions.
1999 If TYPELIST is exhausted, or when an element has NULL as its type,
2000 perform the default conversions.
2002 PARMLIST is the chain of parm decls for the function being called.
2003 It may be 0, if that info is not available.
2004 It is used only for generating error messages.
2006 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2008 This is also where warnings about wrong number of args are generated.
2010 Both VALUES and the returned value are chains of TREE_LIST nodes
2011 with the elements of the list in the TREE_VALUE slots of those nodes. */
2014 convert_arguments (tree typelist
, tree values
, tree name
, tree fundecl
)
2016 tree typetail
, valtail
;
2020 /* Scan the given expressions and types, producing individual
2021 converted arguments and pushing them on RESULT in reverse order. */
2023 for (valtail
= values
, typetail
= typelist
, parmnum
= 0;
2025 valtail
= TREE_CHAIN (valtail
), parmnum
++)
2027 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
2028 tree val
= TREE_VALUE (valtail
);
2030 if (type
== void_type_node
)
2033 error ("too many arguments to function %qs",
2034 IDENTIFIER_POINTER (name
));
2036 error ("too many arguments to function");
2040 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2041 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2042 to convert automatically to a pointer. */
2043 if (TREE_CODE (val
) == NON_LVALUE_EXPR
)
2044 val
= TREE_OPERAND (val
, 0);
2046 val
= default_function_array_conversion (val
);
2048 val
= require_complete_type (val
);
2052 /* Formal parm type is specified by a function prototype. */
2055 if (!COMPLETE_TYPE_P (type
))
2057 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
2062 /* Optionally warn about conversions that
2063 differ from the default conversions. */
2064 if (warn_conversion
|| warn_traditional
)
2066 unsigned int formal_prec
= TYPE_PRECISION (type
);
2068 if (INTEGRAL_TYPE_P (type
)
2069 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2070 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
2071 if (INTEGRAL_TYPE_P (type
)
2072 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
2073 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
2074 else if (TREE_CODE (type
) == COMPLEX_TYPE
2075 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2076 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
2077 else if (TREE_CODE (type
) == REAL_TYPE
2078 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2079 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
2080 else if (TREE_CODE (type
) == COMPLEX_TYPE
2081 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2082 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
2083 else if (TREE_CODE (type
) == REAL_TYPE
2084 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
2085 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
2086 /* ??? At some point, messages should be written about
2087 conversions between complex types, but that's too messy
2089 else if (TREE_CODE (type
) == REAL_TYPE
2090 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
2092 /* Warn if any argument is passed as `float',
2093 since without a prototype it would be `double'. */
2094 if (formal_prec
== TYPE_PRECISION (float_type_node
))
2095 warn_for_assignment ("%s as %<float%> rather than "
2096 "%<double%> due to prototype",
2097 (char *) 0, name
, parmnum
+ 1);
2099 /* Detect integer changing in width or signedness.
2100 These warnings are only activated with
2101 -Wconversion, not with -Wtraditional. */
2102 else if (warn_conversion
&& INTEGRAL_TYPE_P (type
)
2103 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
2105 tree would_have_been
= default_conversion (val
);
2106 tree type1
= TREE_TYPE (would_have_been
);
2108 if (TREE_CODE (type
) == ENUMERAL_TYPE
2109 && (TYPE_MAIN_VARIANT (type
)
2110 == TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
2111 /* No warning if function asks for enum
2112 and the actual arg is that enum type. */
2114 else if (formal_prec
!= TYPE_PRECISION (type1
))
2115 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name
, parmnum
+ 1);
2116 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
2118 /* Don't complain if the formal parameter type
2119 is an enum, because we can't tell now whether
2120 the value was an enum--even the same enum. */
2121 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
2123 else if (TREE_CODE (val
) == INTEGER_CST
2124 && int_fits_type_p (val
, type
))
2125 /* Change in signedness doesn't matter
2126 if a constant value is unaffected. */
2128 /* Likewise for a constant in a NOP_EXPR. */
2129 else if (TREE_CODE (val
) == NOP_EXPR
2130 && TREE_CODE (TREE_OPERAND (val
, 0)) == INTEGER_CST
2131 && int_fits_type_p (TREE_OPERAND (val
, 0), type
))
2133 /* If the value is extended from a narrower
2134 unsigned type, it doesn't matter whether we
2135 pass it as signed or unsigned; the value
2136 certainly is the same either way. */
2137 else if (TYPE_PRECISION (TREE_TYPE (val
)) < TYPE_PRECISION (type
)
2138 && TYPE_UNSIGNED (TREE_TYPE (val
)))
2140 else if (TYPE_UNSIGNED (type
))
2141 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name
, parmnum
+ 1);
2143 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name
, parmnum
+ 1);
2147 parmval
= convert_for_assignment (type
, val
,
2148 (char *) 0, /* arg passing */
2149 fundecl
, name
, parmnum
+ 1);
2151 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
2152 && INTEGRAL_TYPE_P (type
)
2153 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2154 parmval
= default_conversion (parmval
);
2156 result
= tree_cons (NULL_TREE
, parmval
, result
);
2158 else if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2159 && (TYPE_PRECISION (TREE_TYPE (val
))
2160 < TYPE_PRECISION (double_type_node
)))
2161 /* Convert `float' to `double'. */
2162 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2164 /* Convert `short' and `char' to full-size `int'. */
2165 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2168 typetail
= TREE_CHAIN (typetail
);
2171 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
2174 error ("too few arguments to function %qs",
2175 IDENTIFIER_POINTER (name
));
2177 error ("too few arguments to function");
2180 return nreverse (result
);
2183 /* This is the entry point used by the parser
2184 for binary operators in the input.
2185 In addition to constructing the expression,
2186 we check for operands that were written with other binary operators
2187 in a way that is likely to confuse the user. */
2190 parser_build_binary_op (enum tree_code code
, struct c_expr arg1
,
2193 struct c_expr result
;
2195 enum tree_code code1
= arg1
.original_code
;
2196 enum tree_code code2
= arg2
.original_code
;
2198 result
.value
= build_binary_op (code
, arg1
.value
, arg2
.value
, 1);
2199 result
.original_code
= code
;
2201 if (TREE_CODE (result
.value
) == ERROR_MARK
)
2204 /* Check for cases such as x+y<<z which users are likely
2206 if (warn_parentheses
)
2208 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
2210 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2211 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2212 warning ("suggest parentheses around + or - inside shift");
2215 if (code
== TRUTH_ORIF_EXPR
)
2217 if (code1
== TRUTH_ANDIF_EXPR
2218 || code2
== TRUTH_ANDIF_EXPR
)
2219 warning ("suggest parentheses around && within ||");
2222 if (code
== BIT_IOR_EXPR
)
2224 if (code1
== BIT_AND_EXPR
|| code1
== BIT_XOR_EXPR
2225 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2226 || code2
== BIT_AND_EXPR
|| code2
== BIT_XOR_EXPR
2227 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2228 warning ("suggest parentheses around arithmetic in operand of |");
2229 /* Check cases like x|y==z */
2230 if (TREE_CODE_CLASS (code1
) == tcc_comparison
2231 || TREE_CODE_CLASS (code2
) == tcc_comparison
)
2232 warning ("suggest parentheses around comparison in operand of |");
2235 if (code
== BIT_XOR_EXPR
)
2237 if (code1
== BIT_AND_EXPR
2238 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2239 || code2
== BIT_AND_EXPR
2240 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2241 warning ("suggest parentheses around arithmetic in operand of ^");
2242 /* Check cases like x^y==z */
2243 if (TREE_CODE_CLASS (code1
) == tcc_comparison
2244 || TREE_CODE_CLASS (code2
) == tcc_comparison
)
2245 warning ("suggest parentheses around comparison in operand of ^");
2248 if (code
== BIT_AND_EXPR
)
2250 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2251 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2252 warning ("suggest parentheses around + or - in operand of &");
2253 /* Check cases like x&y==z */
2254 if (TREE_CODE_CLASS (code1
) == tcc_comparison
2255 || TREE_CODE_CLASS (code2
) == tcc_comparison
)
2256 warning ("suggest parentheses around comparison in operand of &");
2258 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2259 if (TREE_CODE_CLASS (code
) == tcc_comparison
2260 && (TREE_CODE_CLASS (code1
) == tcc_comparison
2261 || TREE_CODE_CLASS (code2
) == tcc_comparison
))
2262 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2266 unsigned_conversion_warning (result
.value
, arg1
.value
);
2267 unsigned_conversion_warning (result
.value
, arg2
.value
);
2268 overflow_warning (result
.value
);
2273 /* Return a tree for the difference of pointers OP0 and OP1.
2274 The resulting tree has type int. */
2277 pointer_diff (tree op0
, tree op1
)
2279 tree restype
= ptrdiff_type_node
;
2281 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
2282 tree con0
, con1
, lit0
, lit1
;
2283 tree orig_op1
= op1
;
2285 if (pedantic
|| warn_pointer_arith
)
2287 if (TREE_CODE (target_type
) == VOID_TYPE
)
2288 pedwarn ("pointer of type %<void *%> used in subtraction");
2289 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
2290 pedwarn ("pointer to a function used in subtraction");
2293 /* If the conversion to ptrdiff_type does anything like widening or
2294 converting a partial to an integral mode, we get a convert_expression
2295 that is in the way to do any simplifications.
2296 (fold-const.c doesn't know that the extra bits won't be needed.
2297 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2298 different mode in place.)
2299 So first try to find a common term here 'by hand'; we want to cover
2300 at least the cases that occur in legal static initializers. */
2301 con0
= TREE_CODE (op0
) == NOP_EXPR
? TREE_OPERAND (op0
, 0) : op0
;
2302 con1
= TREE_CODE (op1
) == NOP_EXPR
? TREE_OPERAND (op1
, 0) : op1
;
2304 if (TREE_CODE (con0
) == PLUS_EXPR
)
2306 lit0
= TREE_OPERAND (con0
, 1);
2307 con0
= TREE_OPERAND (con0
, 0);
2310 lit0
= integer_zero_node
;
2312 if (TREE_CODE (con1
) == PLUS_EXPR
)
2314 lit1
= TREE_OPERAND (con1
, 1);
2315 con1
= TREE_OPERAND (con1
, 0);
2318 lit1
= integer_zero_node
;
2320 if (operand_equal_p (con0
, con1
, 0))
2327 /* First do the subtraction as integers;
2328 then drop through to build the divide operator.
2329 Do not do default conversions on the minus operator
2330 in case restype is a short type. */
2332 op0
= build_binary_op (MINUS_EXPR
, convert (restype
, op0
),
2333 convert (restype
, op1
), 0);
2334 /* This generates an error if op1 is pointer to incomplete type. */
2335 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
2336 error ("arithmetic on pointer to an incomplete type");
2338 /* This generates an error if op0 is pointer to incomplete type. */
2339 op1
= c_size_in_bytes (target_type
);
2341 /* Divide by the size, in easiest possible way. */
2342 return fold (build2 (EXACT_DIV_EXPR
, restype
, op0
, convert (restype
, op1
)));
2345 /* Construct and perhaps optimize a tree representation
2346 for a unary operation. CODE, a tree_code, specifies the operation
2347 and XARG is the operand.
2348 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2349 the default promotions (such as from short to int).
2350 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2351 allows non-lvalues; this is only used to handle conversion of non-lvalue
2352 arrays to pointers in C99. */
2355 build_unary_op (enum tree_code code
, tree xarg
, int flag
)
2357 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2360 enum tree_code typecode
= TREE_CODE (TREE_TYPE (arg
));
2362 int noconvert
= flag
;
2364 if (typecode
== ERROR_MARK
)
2365 return error_mark_node
;
2366 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
2367 typecode
= INTEGER_TYPE
;
2372 /* This is used for unary plus, because a CONVERT_EXPR
2373 is enough to prevent anybody from looking inside for
2374 associativity, but won't generate any code. */
2375 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2376 || typecode
== COMPLEX_TYPE
2377 || typecode
== VECTOR_TYPE
))
2379 error ("wrong type argument to unary plus");
2380 return error_mark_node
;
2382 else if (!noconvert
)
2383 arg
= default_conversion (arg
);
2384 arg
= non_lvalue (arg
);
2388 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2389 || typecode
== COMPLEX_TYPE
2390 || typecode
== VECTOR_TYPE
))
2392 error ("wrong type argument to unary minus");
2393 return error_mark_node
;
2395 else if (!noconvert
)
2396 arg
= default_conversion (arg
);
2400 if (typecode
== INTEGER_TYPE
|| typecode
== VECTOR_TYPE
)
2403 arg
= default_conversion (arg
);
2405 else if (typecode
== COMPLEX_TYPE
)
2409 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2411 arg
= default_conversion (arg
);
2415 error ("wrong type argument to bit-complement");
2416 return error_mark_node
;
2421 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2423 error ("wrong type argument to abs");
2424 return error_mark_node
;
2426 else if (!noconvert
)
2427 arg
= default_conversion (arg
);
2431 /* Conjugating a real value is a no-op, but allow it anyway. */
2432 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2433 || typecode
== COMPLEX_TYPE
))
2435 error ("wrong type argument to conjugation");
2436 return error_mark_node
;
2438 else if (!noconvert
)
2439 arg
= default_conversion (arg
);
2442 case TRUTH_NOT_EXPR
:
2443 if (typecode
!= INTEGER_TYPE
2444 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
2445 && typecode
!= COMPLEX_TYPE
2446 /* These will convert to a pointer. */
2447 && typecode
!= ARRAY_TYPE
&& typecode
!= FUNCTION_TYPE
)
2449 error ("wrong type argument to unary exclamation mark");
2450 return error_mark_node
;
2452 arg
= lang_hooks
.truthvalue_conversion (arg
);
2453 return invert_truthvalue (arg
);
2459 if (TREE_CODE (arg
) == COMPLEX_CST
)
2460 return TREE_REALPART (arg
);
2461 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2462 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2467 if (TREE_CODE (arg
) == COMPLEX_CST
)
2468 return TREE_IMAGPART (arg
);
2469 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2470 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2472 return convert (TREE_TYPE (arg
), integer_zero_node
);
2474 case PREINCREMENT_EXPR
:
2475 case POSTINCREMENT_EXPR
:
2476 case PREDECREMENT_EXPR
:
2477 case POSTDECREMENT_EXPR
:
2479 /* Increment or decrement the real part of the value,
2480 and don't change the imaginary part. */
2481 if (typecode
== COMPLEX_TYPE
)
2486 pedwarn ("ISO C does not support %<++%> and %<--%>"
2487 " on complex types");
2489 arg
= stabilize_reference (arg
);
2490 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
2491 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
2492 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
2493 build_unary_op (code
, real
, 1), imag
);
2496 /* Report invalid types. */
2498 if (typecode
!= POINTER_TYPE
2499 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
2501 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2502 error ("wrong type argument to increment");
2504 error ("wrong type argument to decrement");
2506 return error_mark_node
;
2511 tree result_type
= TREE_TYPE (arg
);
2513 arg
= get_unwidened (arg
, 0);
2514 argtype
= TREE_TYPE (arg
);
2516 /* Compute the increment. */
2518 if (typecode
== POINTER_TYPE
)
2520 /* If pointer target is an undefined struct,
2521 we just cannot know how to do the arithmetic. */
2522 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type
)))
2524 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2525 error ("increment of pointer to unknown structure");
2527 error ("decrement of pointer to unknown structure");
2529 else if ((pedantic
|| warn_pointer_arith
)
2530 && (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
2531 || TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
))
2533 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2534 pedwarn ("wrong type argument to increment");
2536 pedwarn ("wrong type argument to decrement");
2539 inc
= c_size_in_bytes (TREE_TYPE (result_type
));
2542 inc
= integer_one_node
;
2544 inc
= convert (argtype
, inc
);
2546 /* Complain about anything else that is not a true lvalue. */
2547 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
2548 || code
== POSTINCREMENT_EXPR
)
2549 ? "invalid lvalue in increment"
2550 : "invalid lvalue in decrement")))
2551 return error_mark_node
;
2553 /* Report a read-only lvalue. */
2554 if (TREE_READONLY (arg
))
2555 readonly_error (arg
,
2556 ((code
== PREINCREMENT_EXPR
2557 || code
== POSTINCREMENT_EXPR
)
2558 ? "increment" : "decrement"));
2560 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
2561 val
= boolean_increment (code
, arg
);
2563 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
2564 TREE_SIDE_EFFECTS (val
) = 1;
2565 val
= convert (result_type
, val
);
2566 if (TREE_CODE (val
) != code
)
2567 TREE_NO_WARNING (val
) = 1;
2572 /* Note that this operation never does default_conversion. */
2574 /* Let &* cancel out to simplify resulting code. */
2575 if (TREE_CODE (arg
) == INDIRECT_REF
)
2577 /* Don't let this be an lvalue. */
2578 if (lvalue_p (TREE_OPERAND (arg
, 0)))
2579 return non_lvalue (TREE_OPERAND (arg
, 0));
2580 return TREE_OPERAND (arg
, 0);
2583 /* For &x[y], return x+y */
2584 if (TREE_CODE (arg
) == ARRAY_REF
)
2586 if (!c_mark_addressable (TREE_OPERAND (arg
, 0)))
2587 return error_mark_node
;
2588 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
2589 TREE_OPERAND (arg
, 1), 1);
2592 /* Anything not already handled and not a true memory reference
2593 or a non-lvalue array is an error. */
2594 else if (typecode
!= FUNCTION_TYPE
&& !flag
2595 && !lvalue_or_else (arg
, "invalid lvalue in unary %<&%>"))
2596 return error_mark_node
;
2598 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2599 argtype
= TREE_TYPE (arg
);
2601 /* If the lvalue is const or volatile, merge that into the type
2602 to which the address will point. Note that you can't get a
2603 restricted pointer by taking the address of something, so we
2604 only have to deal with `const' and `volatile' here. */
2605 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
2606 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
)))
2607 argtype
= c_build_type_variant (argtype
,
2608 TREE_READONLY (arg
),
2609 TREE_THIS_VOLATILE (arg
));
2611 if (!c_mark_addressable (arg
))
2612 return error_mark_node
;
2614 if (TREE_CODE (arg
) == COMPONENT_REF
2615 && DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
2617 error ("attempt to take address of bit-field structure member %qD",
2618 TREE_OPERAND (arg
, 1));
2619 return error_mark_node
;
2622 argtype
= build_pointer_type (argtype
);
2624 /* ??? Cope with user tricks that amount to offsetof. Delete this
2625 when we have proper support for integer constant expressions. */
2626 val
= get_base_address (arg
);
2627 if (val
&& TREE_CODE (val
) == INDIRECT_REF
2628 && integer_zerop (TREE_OPERAND (val
, 0)))
2629 return fold_convert (argtype
, fold_offsetof (arg
));
2631 val
= build1 (ADDR_EXPR
, argtype
, arg
);
2633 if (TREE_CODE (arg
) == COMPOUND_LITERAL_EXPR
)
2634 TREE_INVARIANT (val
) = TREE_CONSTANT (val
) = 1;
2643 argtype
= TREE_TYPE (arg
);
2644 val
= build1 (code
, argtype
, arg
);
2645 return require_constant_value
? fold_initializer (val
) : fold (val
);
2648 /* Return nonzero if REF is an lvalue valid for this language.
2649 Lvalues can be assigned, unless their type has TYPE_READONLY.
2650 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2655 enum tree_code code
= TREE_CODE (ref
);
2662 return lvalue_p (TREE_OPERAND (ref
, 0));
2664 case COMPOUND_LITERAL_EXPR
:
2674 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
2675 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
2678 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
2685 /* Return nonzero if REF is an lvalue valid for this language;
2686 otherwise, print an error message and return zero. */
2689 lvalue_or_else (tree ref
, const char *msgid
)
2691 int win
= lvalue_p (ref
);
2694 error ("%s", msgid
);
2700 /* Warn about storing in something that is `const'. */
2703 readonly_error (tree arg
, const char *msgid
)
2705 if (TREE_CODE (arg
) == COMPONENT_REF
)
2707 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
2708 readonly_error (TREE_OPERAND (arg
, 0), msgid
);
2710 error ("%s of read-only member %qs", _(msgid
),
2711 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg
, 1))));
2713 else if (TREE_CODE (arg
) == VAR_DECL
)
2714 error ("%s of read-only variable %qs", _(msgid
),
2715 IDENTIFIER_POINTER (DECL_NAME (arg
)));
2717 error ("%s of read-only location", _(msgid
));
2720 /* Mark EXP saying that we need to be able to take the
2721 address of it; it should not be allocated in a register.
2722 Returns true if successful. */
2725 c_mark_addressable (tree exp
)
2730 switch (TREE_CODE (x
))
2733 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
2735 error ("cannot take address of bit-field %qs",
2736 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x
, 1))));
2740 /* ... fall through ... */
2746 x
= TREE_OPERAND (x
, 0);
2749 case COMPOUND_LITERAL_EXPR
:
2751 TREE_ADDRESSABLE (x
) = 1;
2758 if (C_DECL_REGISTER (x
)
2759 && DECL_NONLOCAL (x
))
2761 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
2763 error ("global register variable %qs used in nested function",
2764 IDENTIFIER_POINTER (DECL_NAME (x
)));
2767 pedwarn ("register variable %qs used in nested function",
2768 IDENTIFIER_POINTER (DECL_NAME (x
)));
2770 else if (C_DECL_REGISTER (x
))
2772 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
2774 error ("address of global register variable %qs requested",
2775 IDENTIFIER_POINTER (DECL_NAME (x
)));
2779 pedwarn ("address of register variable %qs requested",
2780 IDENTIFIER_POINTER (DECL_NAME (x
)));
2785 TREE_ADDRESSABLE (x
) = 1;
2792 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2795 build_conditional_expr (tree ifexp
, tree op1
, tree op2
)
2799 enum tree_code code1
;
2800 enum tree_code code2
;
2801 tree result_type
= NULL
;
2802 tree orig_op1
= op1
, orig_op2
= op2
;
2804 ifexp
= lang_hooks
.truthvalue_conversion (default_conversion (ifexp
));
2806 /* Promote both alternatives. */
2808 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
2809 op1
= default_conversion (op1
);
2810 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
2811 op2
= default_conversion (op2
);
2813 if (TREE_CODE (ifexp
) == ERROR_MARK
2814 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
2815 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
2816 return error_mark_node
;
2818 type1
= TREE_TYPE (op1
);
2819 code1
= TREE_CODE (type1
);
2820 type2
= TREE_TYPE (op2
);
2821 code2
= TREE_CODE (type2
);
2823 /* C90 does not permit non-lvalue arrays in conditional expressions.
2824 In C99 they will be pointers by now. */
2825 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
2827 error ("non-lvalue array in conditional expression");
2828 return error_mark_node
;
2831 /* Quickly detect the usual case where op1 and op2 have the same type
2833 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
2836 result_type
= type1
;
2838 result_type
= TYPE_MAIN_VARIANT (type1
);
2840 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2841 || code1
== COMPLEX_TYPE
)
2842 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
2843 || code2
== COMPLEX_TYPE
))
2845 result_type
= common_type (type1
, type2
);
2847 /* If -Wsign-compare, warn here if type1 and type2 have
2848 different signedness. We'll promote the signed to unsigned
2849 and later code won't know it used to be different.
2850 Do this check on the original types, so that explicit casts
2851 will be considered, but default promotions won't. */
2852 if (warn_sign_compare
&& !skip_evaluation
)
2854 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
2855 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
2857 if (unsigned_op1
^ unsigned_op2
)
2859 /* Do not warn if the result type is signed, since the
2860 signed type will only be chosen if it can represent
2861 all the values of the unsigned type. */
2862 if (! TYPE_UNSIGNED (result_type
))
2864 /* Do not warn if the signed quantity is an unsuffixed
2865 integer literal (or some static constant expression
2866 involving such literals) and it is non-negative. */
2867 else if ((unsigned_op2
&& tree_expr_nonnegative_p (op1
))
2868 || (unsigned_op1
&& tree_expr_nonnegative_p (op2
)))
2871 warning ("signed and unsigned type in conditional expression");
2875 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
2877 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
2878 pedwarn ("ISO C forbids conditional expr with only one void side");
2879 result_type
= void_type_node
;
2881 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
2883 if (comp_target_types (type1
, type2
, 1))
2884 result_type
= common_pointer_type (type1
, type2
);
2885 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
2886 && TREE_CODE (orig_op1
) != NOP_EXPR
)
2887 result_type
= qualify_type (type2
, type1
);
2888 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
2889 && TREE_CODE (orig_op2
) != NOP_EXPR
)
2890 result_type
= qualify_type (type1
, type2
);
2891 else if (VOID_TYPE_P (TREE_TYPE (type1
)))
2893 if (pedantic
&& TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
2894 pedwarn ("ISO C forbids conditional expr between "
2895 "%<void *%> and function pointer");
2896 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
2897 TREE_TYPE (type2
)));
2899 else if (VOID_TYPE_P (TREE_TYPE (type2
)))
2901 if (pedantic
&& TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
2902 pedwarn ("ISO C forbids conditional expr between "
2903 "%<void *%> and function pointer");
2904 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
2905 TREE_TYPE (type1
)));
2909 pedwarn ("pointer type mismatch in conditional expression");
2910 result_type
= build_pointer_type (void_type_node
);
2913 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
2915 if (! integer_zerop (op2
))
2916 pedwarn ("pointer/integer type mismatch in conditional expression");
2919 op2
= null_pointer_node
;
2921 result_type
= type1
;
2923 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2925 if (!integer_zerop (op1
))
2926 pedwarn ("pointer/integer type mismatch in conditional expression");
2929 op1
= null_pointer_node
;
2931 result_type
= type2
;
2936 if (flag_cond_mismatch
)
2937 result_type
= void_type_node
;
2940 error ("type mismatch in conditional expression");
2941 return error_mark_node
;
2945 /* Merge const and volatile flags of the incoming types. */
2947 = build_type_variant (result_type
,
2948 TREE_READONLY (op1
) || TREE_READONLY (op2
),
2949 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
2951 if (result_type
!= TREE_TYPE (op1
))
2952 op1
= convert_and_check (result_type
, op1
);
2953 if (result_type
!= TREE_TYPE (op2
))
2954 op2
= convert_and_check (result_type
, op2
);
2956 if (TREE_CODE (ifexp
) == INTEGER_CST
)
2957 return non_lvalue (integer_zerop (ifexp
) ? op2
: op1
);
2959 return fold (build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
));
2962 /* Return a compound expression that performs two expressions and
2963 returns the value of the second of them. */
2966 build_compound_expr (tree expr1
, tree expr2
)
2968 /* Convert arrays and functions to pointers. */
2969 expr2
= default_function_array_conversion (expr2
);
2971 /* Don't let (0, 0) be null pointer constant. */
2972 if (integer_zerop (expr2
))
2973 expr2
= non_lvalue (expr2
);
2975 if (! TREE_SIDE_EFFECTS (expr1
))
2977 /* The left-hand operand of a comma expression is like an expression
2978 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2979 any side-effects, unless it was explicitly cast to (void). */
2980 if (warn_unused_value
2981 && ! (TREE_CODE (expr1
) == CONVERT_EXPR
2982 && VOID_TYPE_P (TREE_TYPE (expr1
))))
2983 warning ("left-hand operand of comma expression has no effect");
2986 /* With -Wunused, we should also warn if the left-hand operand does have
2987 side-effects, but computes a value which is not used. For example, in
2988 `foo() + bar(), baz()' the result of the `+' operator is not used,
2989 so we should issue a warning. */
2990 else if (warn_unused_value
)
2991 warn_if_unused_value (expr1
, input_location
);
2993 return build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
2996 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2999 build_c_cast (tree type
, tree expr
)
3003 if (type
== error_mark_node
|| expr
== error_mark_node
)
3004 return error_mark_node
;
3006 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3007 only in <protocol> qualifications. But when constructing cast expressions,
3008 the protocols do matter and must be kept around. */
3009 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
3010 return build1 (NOP_EXPR
, type
, expr
);
3012 type
= TYPE_MAIN_VARIANT (type
);
3014 if (TREE_CODE (type
) == ARRAY_TYPE
)
3016 error ("cast specifies array type");
3017 return error_mark_node
;
3020 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3022 error ("cast specifies function type");
3023 return error_mark_node
;
3026 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
3030 if (TREE_CODE (type
) == RECORD_TYPE
3031 || TREE_CODE (type
) == UNION_TYPE
)
3032 pedwarn ("ISO C forbids casting nonscalar to the same type");
3035 else if (TREE_CODE (type
) == UNION_TYPE
)
3038 value
= default_function_array_conversion (value
);
3040 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3041 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
3042 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
3050 pedwarn ("ISO C forbids casts to union type");
3051 t
= digest_init (type
,
3052 build_constructor (type
,
3053 build_tree_list (field
, value
)),
3055 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
3056 TREE_INVARIANT (t
) = TREE_INVARIANT (value
);
3059 error ("cast to union type from type not present in union");
3060 return error_mark_node
;
3066 /* If casting to void, avoid the error that would come
3067 from default_conversion in the case of a non-lvalue array. */
3068 if (type
== void_type_node
)
3069 return build1 (CONVERT_EXPR
, type
, value
);
3071 /* Convert functions and arrays to pointers,
3072 but don't convert any other types. */
3073 value
= default_function_array_conversion (value
);
3074 otype
= TREE_TYPE (value
);
3076 /* Optionally warn about potentially worrisome casts. */
3079 && TREE_CODE (type
) == POINTER_TYPE
3080 && TREE_CODE (otype
) == POINTER_TYPE
)
3082 tree in_type
= type
;
3083 tree in_otype
= otype
;
3087 /* Check that the qualifiers on IN_TYPE are a superset of
3088 the qualifiers of IN_OTYPE. The outermost level of
3089 POINTER_TYPE nodes is uninteresting and we stop as soon
3090 as we hit a non-POINTER_TYPE node on either type. */
3093 in_otype
= TREE_TYPE (in_otype
);
3094 in_type
= TREE_TYPE (in_type
);
3096 /* GNU C allows cv-qualified function types. 'const'
3097 means the function is very pure, 'volatile' means it
3098 can't return. We need to warn when such qualifiers
3099 are added, not when they're taken away. */
3100 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
3101 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
3102 added
|= (TYPE_QUALS (in_type
) & ~TYPE_QUALS (in_otype
));
3104 discarded
|= (TYPE_QUALS (in_otype
) & ~TYPE_QUALS (in_type
));
3106 while (TREE_CODE (in_type
) == POINTER_TYPE
3107 && TREE_CODE (in_otype
) == POINTER_TYPE
);
3110 warning ("cast adds new qualifiers to function type");
3113 /* There are qualifiers present in IN_OTYPE that are not
3114 present in IN_TYPE. */
3115 warning ("cast discards qualifiers from pointer target type");
3118 /* Warn about possible alignment problems. */
3119 if (STRICT_ALIGNMENT
&& warn_cast_align
3120 && TREE_CODE (type
) == POINTER_TYPE
3121 && TREE_CODE (otype
) == POINTER_TYPE
3122 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
3123 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3124 /* Don't warn about opaque types, where the actual alignment
3125 restriction is unknown. */
3126 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
3127 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
3128 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
3129 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
3130 warning ("cast increases required alignment of target type");
3132 if (TREE_CODE (type
) == INTEGER_TYPE
3133 && TREE_CODE (otype
) == POINTER_TYPE
3134 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3135 && !TREE_CONSTANT (value
))
3136 warning ("cast from pointer to integer of different size");
3138 if (warn_bad_function_cast
3139 && TREE_CODE (value
) == CALL_EXPR
3140 && TREE_CODE (type
) != TREE_CODE (otype
))
3141 warning ("cast from function call of type %qT to non-matching "
3142 "type %qT", otype
, type
);
3144 if (TREE_CODE (type
) == POINTER_TYPE
3145 && TREE_CODE (otype
) == INTEGER_TYPE
3146 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3147 /* Don't warn about converting any constant. */
3148 && !TREE_CONSTANT (value
))
3149 warning ("cast to pointer from integer of different size");
3151 if (TREE_CODE (type
) == POINTER_TYPE
3152 && TREE_CODE (otype
) == POINTER_TYPE
3153 && TREE_CODE (expr
) == ADDR_EXPR
3154 && DECL_P (TREE_OPERAND (expr
, 0))
3155 && flag_strict_aliasing
&& warn_strict_aliasing
3156 && !VOID_TYPE_P (TREE_TYPE (type
)))
3158 /* Casting the address of a decl to non void pointer. Warn
3159 if the cast breaks type based aliasing. */
3160 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)))
3161 warning ("type-punning to incomplete type might break strict-aliasing rules");
3164 HOST_WIDE_INT set1
= get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0)));
3165 HOST_WIDE_INT set2
= get_alias_set (TREE_TYPE (type
));
3167 if (!alias_sets_conflict_p (set1
, set2
))
3168 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3169 else if (warn_strict_aliasing
> 1
3170 && !alias_sets_might_conflict_p (set1
, set2
))
3171 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3175 /* If pedantic, warn for conversions between function and object
3176 pointer types, except for converting a null pointer constant
3177 to function pointer type. */
3179 && TREE_CODE (type
) == POINTER_TYPE
3180 && TREE_CODE (otype
) == POINTER_TYPE
3181 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
3182 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
3183 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3186 && TREE_CODE (type
) == POINTER_TYPE
3187 && TREE_CODE (otype
) == POINTER_TYPE
3188 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
3189 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3190 && !(integer_zerop (value
) && TREE_TYPE (otype
) == void_type_node
3191 && TREE_CODE (expr
) != NOP_EXPR
))
3192 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3195 /* Replace a nonvolatile const static variable with its value. */
3196 if (optimize
&& TREE_CODE (value
) == VAR_DECL
)
3197 value
= decl_constant_value (value
);
3198 value
= convert (type
, value
);
3200 /* Ignore any integer overflow caused by the cast. */
3201 if (TREE_CODE (value
) == INTEGER_CST
)
3203 if (EXPR_P (ovalue
))
3204 /* If OVALUE had overflow set, then so will VALUE, so it
3205 is safe to overwrite. */
3206 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
3208 TREE_OVERFLOW (value
) = 0;
3210 if (CONSTANT_CLASS_P (ovalue
))
3211 /* Similarly, constant_overflow cannot have become
3213 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
3217 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3218 if (TREE_CODE (value
) == INTEGER_CST
3219 && TREE_CODE (expr
) == INTEGER_CST
3220 && TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
)
3221 value
= non_lvalue (value
);
3223 /* Don't let a cast be an lvalue. */
3225 value
= non_lvalue (value
);
3230 /* Interpret a cast of expression EXPR to type TYPE. */
3232 c_cast_expr (struct c_type_name
*type_name
, tree expr
)
3235 int saved_wsp
= warn_strict_prototypes
;
3237 /* This avoids warnings about unprototyped casts on
3238 integers. E.g. "#define SIG_DFL (void(*)())0". */
3239 if (TREE_CODE (expr
) == INTEGER_CST
)
3240 warn_strict_prototypes
= 0;
3241 type
= groktypename (type_name
);
3242 warn_strict_prototypes
= saved_wsp
;
3244 return build_c_cast (type
, expr
);
3248 /* Build an assignment expression of lvalue LHS from value RHS.
3249 MODIFYCODE is the code for a binary operator that we use
3250 to combine the old value of LHS with RHS to get the new value.
3251 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3254 build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
)
3258 tree lhstype
= TREE_TYPE (lhs
);
3259 tree olhstype
= lhstype
;
3261 /* Types that aren't fully specified cannot be used in assignments. */
3262 lhs
= require_complete_type (lhs
);
3264 /* Avoid duplicate error messages from operands that had errors. */
3265 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
3266 return error_mark_node
;
3268 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3269 /* Do not use STRIP_NOPS here. We do not want an enumerator
3270 whose value is 0 to count as a null pointer constant. */
3271 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3272 rhs
= TREE_OPERAND (rhs
, 0);
3276 /* If a binary op has been requested, combine the old LHS value with the RHS
3277 producing the value we should actually store into the LHS. */
3279 if (modifycode
!= NOP_EXPR
)
3281 lhs
= stabilize_reference (lhs
);
3282 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
3285 if (!lvalue_or_else (lhs
, "invalid lvalue in assignment"))
3286 return error_mark_node
;
3288 /* Warn about storing in something that is `const'. */
3290 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
3291 || ((TREE_CODE (lhstype
) == RECORD_TYPE
3292 || TREE_CODE (lhstype
) == UNION_TYPE
)
3293 && C_TYPE_FIELDS_READONLY (lhstype
)))
3294 readonly_error (lhs
, "assignment");
3296 /* If storing into a structure or union member,
3297 it has probably been given type `int'.
3298 Compute the type that would go with
3299 the actual amount of storage the member occupies. */
3301 if (TREE_CODE (lhs
) == COMPONENT_REF
3302 && (TREE_CODE (lhstype
) == INTEGER_TYPE
3303 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
3304 || TREE_CODE (lhstype
) == REAL_TYPE
3305 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
3306 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
3308 /* If storing in a field that is in actuality a short or narrower than one,
3309 we must store in the field in its actual type. */
3311 if (lhstype
!= TREE_TYPE (lhs
))
3313 lhs
= copy_node (lhs
);
3314 TREE_TYPE (lhs
) = lhstype
;
3317 /* Convert new value to destination type. */
3319 newrhs
= convert_for_assignment (lhstype
, newrhs
, _("assignment"),
3320 NULL_TREE
, NULL_TREE
, 0);
3321 if (TREE_CODE (newrhs
) == ERROR_MARK
)
3322 return error_mark_node
;
3326 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
3327 TREE_SIDE_EFFECTS (result
) = 1;
3329 /* If we got the LHS in a different type for storing in,
3330 convert the result back to the nominal type of LHS
3331 so that the value we return always has the same type
3332 as the LHS argument. */
3334 if (olhstype
== TREE_TYPE (result
))
3336 return convert_for_assignment (olhstype
, result
, _("assignment"),
3337 NULL_TREE
, NULL_TREE
, 0);
3340 /* Convert value RHS to type TYPE as preparation for an assignment
3341 to an lvalue of type TYPE.
3342 The real work of conversion is done by `convert'.
3343 The purpose of this function is to generate error messages
3344 for assignments that are not allowed in C.
3345 ERRTYPE is a string to use in error messages:
3346 "assignment", "return", etc. If it is null, this is parameter passing
3347 for a function call (and different error messages are output).
3349 FUNNAME is the name of the function being called,
3350 as an IDENTIFIER_NODE, or null.
3351 PARMNUM is the number of the argument, for printing in error messages. */
3354 convert_for_assignment (tree type
, tree rhs
, const char *errtype
,
3355 tree fundecl
, tree funname
, int parmnum
)
3357 enum tree_code codel
= TREE_CODE (type
);
3359 enum tree_code coder
;
3361 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3362 /* Do not use STRIP_NOPS here. We do not want an enumerator
3363 whose value is 0 to count as a null pointer constant. */
3364 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3365 rhs
= TREE_OPERAND (rhs
, 0);
3367 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
3368 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
)
3369 rhs
= default_conversion (rhs
);
3370 else if (optimize
&& TREE_CODE (rhs
) == VAR_DECL
)
3371 rhs
= decl_constant_value_for_broken_optimization (rhs
);
3373 rhstype
= TREE_TYPE (rhs
);
3374 coder
= TREE_CODE (rhstype
);
3376 if (coder
== ERROR_MARK
)
3377 return error_mark_node
;
3379 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
3381 overflow_warning (rhs
);
3382 /* Check for Objective-C protocols. This will automatically
3383 issue a warning if there are protocol violations. No need to
3384 use the return value. */
3385 if (c_dialect_objc ())
3386 objc_comptypes (type
, rhstype
, 0);
3390 if (coder
== VOID_TYPE
)
3392 error ("void value not ignored as it ought to be");
3393 return error_mark_node
;
3395 /* A type converts to a reference to it.
3396 This code doesn't fully support references, it's just for the
3397 special case of va_start and va_copy. */
3398 if (codel
== REFERENCE_TYPE
3399 && comptypes (TREE_TYPE (type
), TREE_TYPE (rhs
)) == 1)
3401 if (!lvalue_p (rhs
))
3403 error ("cannot pass rvalue to reference parameter");
3404 return error_mark_node
;
3406 if (!c_mark_addressable (rhs
))
3407 return error_mark_node
;
3408 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
3410 /* We already know that these two types are compatible, but they
3411 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3412 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3413 likely to be va_list, a typedef to __builtin_va_list, which
3414 is different enough that it will cause problems later. */
3415 if (TREE_TYPE (TREE_TYPE (rhs
)) != TREE_TYPE (type
))
3416 rhs
= build1 (NOP_EXPR
, build_pointer_type (TREE_TYPE (type
)), rhs
);
3418 rhs
= build1 (NOP_EXPR
, type
, rhs
);
3421 /* Some types can interconvert without explicit casts. */
3422 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
3423 && vector_types_convertible_p (type
, TREE_TYPE (rhs
)))
3424 return convert (type
, rhs
);
3425 /* Arithmetic types all interconvert, and enum is treated like int. */
3426 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
3427 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
3428 || codel
== BOOLEAN_TYPE
)
3429 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
3430 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
3431 || coder
== BOOLEAN_TYPE
))
3432 return convert_and_check (type
, rhs
);
3434 /* Conversion to a transparent union from its member types.
3435 This applies only to function arguments. */
3436 else if (codel
== UNION_TYPE
&& TYPE_TRANSPARENT_UNION (type
) && ! errtype
)
3439 tree marginal_memb_type
= 0;
3441 for (memb_types
= TYPE_FIELDS (type
); memb_types
;
3442 memb_types
= TREE_CHAIN (memb_types
))
3444 tree memb_type
= TREE_TYPE (memb_types
);
3446 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
3447 TYPE_MAIN_VARIANT (rhstype
)))
3450 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
3453 if (coder
== POINTER_TYPE
)
3455 tree ttl
= TREE_TYPE (memb_type
);
3456 tree ttr
= TREE_TYPE (rhstype
);
3458 /* Any non-function converts to a [const][volatile] void *
3459 and vice versa; otherwise, targets must be the same.
3460 Meanwhile, the lhs target must have all the qualifiers of
3462 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3463 || comp_target_types (memb_type
, rhstype
, 0))
3465 /* If this type won't generate any warnings, use it. */
3466 if (TYPE_QUALS (ttl
) == TYPE_QUALS (ttr
)
3467 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
3468 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3469 ? ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3470 == TYPE_QUALS (ttr
))
3471 : ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3472 == TYPE_QUALS (ttl
))))
3475 /* Keep looking for a better type, but remember this one. */
3476 if (! marginal_memb_type
)
3477 marginal_memb_type
= memb_type
;
3481 /* Can convert integer zero to any pointer type. */
3482 if (integer_zerop (rhs
)
3483 || (TREE_CODE (rhs
) == NOP_EXPR
3484 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3486 rhs
= null_pointer_node
;
3491 if (memb_types
|| marginal_memb_type
)
3495 /* We have only a marginally acceptable member type;
3496 it needs a warning. */
3497 tree ttl
= TREE_TYPE (marginal_memb_type
);
3498 tree ttr
= TREE_TYPE (rhstype
);
3500 /* Const and volatile mean something different for function
3501 types, so the usual warnings are not appropriate. */
3502 if (TREE_CODE (ttr
) == FUNCTION_TYPE
3503 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3505 /* Because const and volatile on functions are
3506 restrictions that say the function will not do
3507 certain things, it is okay to use a const or volatile
3508 function where an ordinary one is wanted, but not
3510 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3511 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3512 errtype
, funname
, parmnum
);
3514 else if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3515 warn_for_assignment ("%s discards qualifiers from pointer target type",
3520 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (fundecl
))
3521 pedwarn ("ISO C prohibits argument conversion to union type");
3523 return build1 (NOP_EXPR
, type
, rhs
);
3527 /* Conversions among pointers */
3528 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
3529 && (coder
== codel
))
3531 tree ttl
= TREE_TYPE (type
);
3532 tree ttr
= TREE_TYPE (rhstype
);
3533 bool is_opaque_pointer
;
3534 int target_cmp
= 0; /* Cache comp_target_types () result. */
3536 /* Opaque pointers are treated like void pointers. */
3537 is_opaque_pointer
= (targetm
.vector_opaque_p (type
)
3538 || targetm
.vector_opaque_p (rhstype
))
3539 && TREE_CODE (ttl
) == VECTOR_TYPE
3540 && TREE_CODE (ttr
) == VECTOR_TYPE
;
3542 /* Any non-function converts to a [const][volatile] void *
3543 and vice versa; otherwise, targets must be the same.
3544 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3545 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3546 || (target_cmp
= comp_target_types (type
, rhstype
, 0))
3547 || is_opaque_pointer
3548 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl
))
3549 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
3552 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3555 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3556 which are not ANSI null ptr constants. */
3557 && (!integer_zerop (rhs
) || TREE_CODE (rhs
) == NOP_EXPR
)
3558 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
3559 warn_for_assignment ("ISO C forbids %s between function "
3560 "pointer and %<void *%>",
3561 errtype
, funname
, parmnum
);
3562 /* Const and volatile mean something different for function types,
3563 so the usual warnings are not appropriate. */
3564 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
3565 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
3567 if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3568 warn_for_assignment ("%s discards qualifiers from pointer target type",
3569 errtype
, funname
, parmnum
);
3570 /* If this is not a case of ignoring a mismatch in signedness,
3572 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3575 /* If there is a mismatch, do warn. */
3577 warn_for_assignment ("pointer targets in %s differ in signedness",
3578 errtype
, funname
, parmnum
);
3580 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
3581 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3583 /* Because const and volatile on functions are restrictions
3584 that say the function will not do certain things,
3585 it is okay to use a const or volatile function
3586 where an ordinary one is wanted, but not vice-versa. */
3587 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3588 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3589 errtype
, funname
, parmnum
);
3593 warn_for_assignment ("%s from incompatible pointer type",
3594 errtype
, funname
, parmnum
);
3595 return convert (type
, rhs
);
3597 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
3599 error ("invalid use of non-lvalue array");
3600 return error_mark_node
;
3602 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
3604 /* An explicit constant 0 can convert to a pointer,
3605 or one that results from arithmetic, even including
3606 a cast to integer type. */
3607 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
))
3609 ! (TREE_CODE (rhs
) == NOP_EXPR
3610 && TREE_CODE (TREE_TYPE (rhs
)) == INTEGER_TYPE
3611 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == INTEGER_CST
3612 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3613 warn_for_assignment ("%s makes pointer from integer without a cast",
3614 errtype
, funname
, parmnum
);
3616 return convert (type
, rhs
);
3618 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
3620 warn_for_assignment ("%s makes integer from pointer without a cast",
3621 errtype
, funname
, parmnum
);
3622 return convert (type
, rhs
);
3624 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
3625 return convert (type
, rhs
);
3631 tree selector
= objc_message_selector ();
3633 if (selector
&& parmnum
> 2)
3634 error ("incompatible type for argument %d of %qs",
3635 parmnum
- 2, IDENTIFIER_POINTER (selector
));
3637 error ("incompatible type for argument %d of %qs",
3638 parmnum
, IDENTIFIER_POINTER (funname
));
3641 error ("incompatible type for argument %d of indirect function call",
3645 error ("incompatible types in %s", errtype
);
3647 return error_mark_node
;
3650 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3651 is used for error and waring reporting and indicates which argument
3652 is being processed. */
3655 c_convert_parm_for_inlining (tree parm
, tree value
, tree fn
, int argnum
)
3659 /* If FN was prototyped, the value has been converted already
3660 in convert_arguments. */
3661 if (! value
|| TYPE_ARG_TYPES (TREE_TYPE (fn
)))
3664 type
= TREE_TYPE (parm
);
3665 ret
= convert_for_assignment (type
, value
,
3666 (char *) 0 /* arg passing */, fn
,
3667 DECL_NAME (fn
), argnum
);
3668 if (targetm
.calls
.promote_prototypes (TREE_TYPE (fn
))
3669 && INTEGRAL_TYPE_P (type
)
3670 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3671 ret
= default_conversion (ret
);
3675 /* Print a warning using MSGID.
3676 It gets OPNAME as its one parameter.
3677 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3678 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3679 FUNCTION and ARGNUM are handled specially if we are building an
3680 Objective-C selector. */
3683 warn_for_assignment (const char *msgid
, const char *opname
, tree function
,
3688 tree selector
= objc_message_selector ();
3691 if (selector
&& argnum
> 2)
3693 function
= selector
;
3700 /* Function name is known; supply it. */
3701 const char *const argstring
= _("passing arg of '%s'");
3702 new_opname
= (char *) alloca (IDENTIFIER_LENGTH (function
)
3703 + strlen (argstring
) + 1 + 1);
3704 sprintf (new_opname
, argstring
,
3705 IDENTIFIER_POINTER (function
));
3709 /* Function name unknown (call through ptr). */
3710 const char *const argnofun
= _("passing arg of pointer to function");
3711 new_opname
= (char *) alloca (strlen (argnofun
) + 1 + 1);
3712 sprintf (new_opname
, argnofun
);
3717 /* Function name is known; supply it. */
3718 const char *const argstring
= _("passing arg %d of '%s'");
3719 new_opname
= (char *) alloca (IDENTIFIER_LENGTH (function
)
3720 + strlen (argstring
) + 1 + 25 /*%d*/ + 1);
3721 sprintf (new_opname
, argstring
, argnum
,
3722 IDENTIFIER_POINTER (function
));
3726 /* Function name unknown (call through ptr); just give arg number. */
3727 const char *const argnofun
= _("passing arg %d of pointer to function");
3728 new_opname
= (char *) alloca (strlen (argnofun
) + 1 + 25 /*%d*/ + 1);
3729 sprintf (new_opname
, argnofun
, argnum
);
3731 opname
= new_opname
;
3733 pedwarn (msgid
, opname
);
3736 /* If VALUE is a compound expr all of whose expressions are constant, then
3737 return its value. Otherwise, return error_mark_node.
3739 This is for handling COMPOUND_EXPRs as initializer elements
3740 which is allowed with a warning when -pedantic is specified. */
3743 valid_compound_expr_initializer (tree value
, tree endtype
)
3745 if (TREE_CODE (value
) == COMPOUND_EXPR
)
3747 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
3749 return error_mark_node
;
3750 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
3753 else if (!initializer_constant_valid_p (value
, endtype
))
3754 return error_mark_node
;
3759 /* Perform appropriate conversions on the initial value of a variable,
3760 store it in the declaration DECL,
3761 and print any error messages that are appropriate.
3762 If the init is invalid, store an ERROR_MARK. */
3765 store_init_value (tree decl
, tree init
)
3769 /* If variable's type was invalidly declared, just ignore it. */
3771 type
= TREE_TYPE (decl
);
3772 if (TREE_CODE (type
) == ERROR_MARK
)
3775 /* Digest the specified initializer into an expression. */
3777 value
= digest_init (type
, init
, true, TREE_STATIC (decl
));
3779 /* Store the expression if valid; else report error. */
3781 if (warn_traditional
&& !in_system_header
3782 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && ! TREE_STATIC (decl
))
3783 warning ("traditional C rejects automatic aggregate initialization");
3785 DECL_INITIAL (decl
) = value
;
3787 /* ANSI wants warnings about out-of-range constant initializers. */
3788 STRIP_TYPE_NOPS (value
);
3789 constant_expression_warning (value
);
3791 /* Check if we need to set array size from compound literal size. */
3792 if (TREE_CODE (type
) == ARRAY_TYPE
3793 && TYPE_DOMAIN (type
) == 0
3794 && value
!= error_mark_node
)
3796 tree inside_init
= init
;
3798 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
3799 inside_init
= TREE_OPERAND (init
, 0);
3800 inside_init
= fold (inside_init
);
3802 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
3804 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
3806 if (TYPE_DOMAIN (TREE_TYPE (decl
)))
3808 /* For int foo[] = (int [3]){1}; we need to set array size
3809 now since later on array initializer will be just the
3810 brace enclosed list of the compound literal. */
3811 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (decl
));
3813 layout_decl (decl
, 0);
3819 /* Methods for storing and printing names for error messages. */
3821 /* Implement a spelling stack that allows components of a name to be pushed
3822 and popped. Each element on the stack is this structure. */
3834 #define SPELLING_STRING 1
3835 #define SPELLING_MEMBER 2
3836 #define SPELLING_BOUNDS 3
3838 static struct spelling
*spelling
; /* Next stack element (unused). */
3839 static struct spelling
*spelling_base
; /* Spelling stack base. */
3840 static int spelling_size
; /* Size of the spelling stack. */
3842 /* Macros to save and restore the spelling stack around push_... functions.
3843 Alternative to SAVE_SPELLING_STACK. */
3845 #define SPELLING_DEPTH() (spelling - spelling_base)
3846 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3848 /* Push an element on the spelling stack with type KIND and assign VALUE
3851 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3853 int depth = SPELLING_DEPTH (); \
3855 if (depth >= spelling_size) \
3857 spelling_size += 10; \
3858 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3860 RESTORE_SPELLING_DEPTH (depth); \
3863 spelling->kind = (KIND); \
3864 spelling->MEMBER = (VALUE); \
3868 /* Push STRING on the stack. Printed literally. */
3871 push_string (const char *string
)
3873 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
3876 /* Push a member name on the stack. Printed as '.' STRING. */
3879 push_member_name (tree decl
)
3881 const char *const string
3882 = DECL_NAME (decl
) ? IDENTIFIER_POINTER (DECL_NAME (decl
)) : "<anonymous>";
3883 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
3886 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3889 push_array_bounds (int bounds
)
3891 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
3894 /* Compute the maximum size in bytes of the printed spelling. */
3897 spelling_length (void)
3902 for (p
= spelling_base
; p
< spelling
; p
++)
3904 if (p
->kind
== SPELLING_BOUNDS
)
3907 size
+= strlen (p
->u
.s
) + 1;
3913 /* Print the spelling to BUFFER and return it. */
3916 print_spelling (char *buffer
)
3921 for (p
= spelling_base
; p
< spelling
; p
++)
3922 if (p
->kind
== SPELLING_BOUNDS
)
3924 sprintf (d
, "[%d]", p
->u
.i
);
3930 if (p
->kind
== SPELLING_MEMBER
)
3932 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
3939 /* Issue an error message for a bad initializer component.
3940 MSGID identifies the message.
3941 The component name is taken from the spelling stack. */
3944 error_init (const char *msgid
)
3948 error ("%s", _(msgid
));
3949 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
3951 error ("(near initialization for %qs)", ofwhat
);
3954 /* Issue a pedantic warning for a bad initializer component.
3955 MSGID identifies the message.
3956 The component name is taken from the spelling stack. */
3959 pedwarn_init (const char *msgid
)
3963 pedwarn ("%s", _(msgid
));
3964 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
3966 pedwarn ("(near initialization for %qs)", ofwhat
);
3969 /* Issue a warning for a bad initializer component.
3970 MSGID identifies the message.
3971 The component name is taken from the spelling stack. */
3974 warning_init (const char *msgid
)
3978 warning ("%s", _(msgid
));
3979 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
3981 warning ("(near initialization for %qs)", ofwhat
);
3984 /* If TYPE is an array type and EXPR is a parenthesized string
3985 constant, warn if pedantic that EXPR is being used to initialize an
3986 object of type TYPE. */
3989 maybe_warn_string_init (tree type
, struct c_expr expr
)
3992 && TREE_CODE (type
) == ARRAY_TYPE
3993 && TREE_CODE (expr
.value
) == STRING_CST
3994 && expr
.original_code
!= STRING_CST
)
3995 pedwarn_init ("array initialized from parenthesized string constant");
3998 /* Digest the parser output INIT as an initializer for type TYPE.
3999 Return a C expression of type TYPE to represent the initial value.
4001 If INIT is a string constant, STRICT_STRING is true if it is
4002 unparenthesized or we should not warn here for it being parenthesized.
4003 For other types of INIT, STRICT_STRING is not used.
4005 REQUIRE_CONSTANT requests an error if non-constant initializers or
4006 elements are seen. */
4009 digest_init (tree type
, tree init
, bool strict_string
, int require_constant
)
4011 enum tree_code code
= TREE_CODE (type
);
4012 tree inside_init
= init
;
4014 if (type
== error_mark_node
4015 || init
== error_mark_node
4016 || TREE_TYPE (init
) == error_mark_node
)
4017 return error_mark_node
;
4019 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4020 /* Do not use STRIP_NOPS here. We do not want an enumerator
4021 whose value is 0 to count as a null pointer constant. */
4022 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
4023 inside_init
= TREE_OPERAND (init
, 0);
4025 inside_init
= fold (inside_init
);
4027 /* Initialization of an array of chars from a string constant
4028 optionally enclosed in braces. */
4030 if (code
== ARRAY_TYPE
&& inside_init
4031 && TREE_CODE (inside_init
) == STRING_CST
)
4033 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
4034 /* Note that an array could be both an array of character type
4035 and an array of wchar_t if wchar_t is signed char or unsigned
4037 bool char_array
= (typ1
== char_type_node
4038 || typ1
== signed_char_type_node
4039 || typ1
== unsigned_char_type_node
);
4040 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
4041 if (char_array
|| wchar_array
)
4045 expr
.value
= inside_init
;
4046 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
4047 maybe_warn_string_init (type
, expr
);
4050 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)))
4053 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4054 TYPE_MAIN_VARIANT (type
)))
4057 if (!wchar_array
&& !char_string
)
4059 error_init ("char-array initialized from wide string");
4060 return error_mark_node
;
4062 if (char_string
&& !char_array
)
4064 error_init ("wchar_t-array initialized from non-wide string");
4065 return error_mark_node
;
4068 TREE_TYPE (inside_init
) = type
;
4069 if (TYPE_DOMAIN (type
) != 0
4070 && TYPE_SIZE (type
) != 0
4071 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
4072 /* Subtract 1 (or sizeof (wchar_t))
4073 because it's ok to ignore the terminating null char
4074 that is counted in the length of the constant. */
4075 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
4076 TREE_STRING_LENGTH (inside_init
)
4077 - ((TYPE_PRECISION (typ1
)
4078 != TYPE_PRECISION (char_type_node
))
4079 ? (TYPE_PRECISION (wchar_type_node
)
4082 pedwarn_init ("initializer-string for array of chars is too long");
4086 else if (INTEGRAL_TYPE_P (typ1
))
4088 error_init ("array of inappropriate type initialized "
4089 "from string constant");
4090 return error_mark_node
;
4094 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4095 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4096 below and handle as a constructor. */
4097 if (code
== VECTOR_TYPE
4098 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
4099 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
)
4100 && TREE_CONSTANT (inside_init
))
4102 if (TREE_CODE (inside_init
) == VECTOR_CST
4103 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4104 TYPE_MAIN_VARIANT (type
)))
4107 return build_vector (type
, CONSTRUCTOR_ELTS (inside_init
));
4110 /* Any type can be initialized
4111 from an expression of the same type, optionally with braces. */
4113 if (inside_init
&& TREE_TYPE (inside_init
) != 0
4114 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
4115 TYPE_MAIN_VARIANT (type
))
4116 || (code
== ARRAY_TYPE
4117 && comptypes (TREE_TYPE (inside_init
), type
))
4118 || (code
== VECTOR_TYPE
4119 && comptypes (TREE_TYPE (inside_init
), type
))
4120 || (code
== POINTER_TYPE
4121 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4122 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
4124 || (code
== POINTER_TYPE
4125 && TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
4126 && comptypes (TREE_TYPE (inside_init
),
4127 TREE_TYPE (type
)))))
4129 if (code
== POINTER_TYPE
)
4131 inside_init
= default_function_array_conversion (inside_init
);
4133 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
4135 error_init ("invalid use of non-lvalue array");
4136 return error_mark_node
;
4140 if (code
== VECTOR_TYPE
)
4141 /* Although the types are compatible, we may require a
4143 inside_init
= convert (type
, inside_init
);
4145 if (require_constant
&& !flag_isoc99
4146 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
4148 /* As an extension, allow initializing objects with static storage
4149 duration with compound literals (which are then treated just as
4150 the brace enclosed list they contain). */
4151 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
4152 inside_init
= DECL_INITIAL (decl
);
4155 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
4156 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
4158 error_init ("array initialized from non-constant array expression");
4159 return error_mark_node
;
4162 if (optimize
&& TREE_CODE (inside_init
) == VAR_DECL
)
4163 inside_init
= decl_constant_value_for_broken_optimization (inside_init
);
4165 /* Compound expressions can only occur here if -pedantic or
4166 -pedantic-errors is specified. In the later case, we always want
4167 an error. In the former case, we simply want a warning. */
4168 if (require_constant
&& pedantic
4169 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
4172 = valid_compound_expr_initializer (inside_init
,
4173 TREE_TYPE (inside_init
));
4174 if (inside_init
== error_mark_node
)
4175 error_init ("initializer element is not constant");
4177 pedwarn_init ("initializer element is not constant");
4178 if (flag_pedantic_errors
)
4179 inside_init
= error_mark_node
;
4181 else if (require_constant
4182 && !initializer_constant_valid_p (inside_init
,
4183 TREE_TYPE (inside_init
)))
4185 error_init ("initializer element is not constant");
4186 inside_init
= error_mark_node
;
4192 /* Handle scalar types, including conversions. */
4194 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
4195 || code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
|| code
== COMPLEX_TYPE
4196 || code
== VECTOR_TYPE
)
4198 /* Note that convert_for_assignment calls default_conversion
4199 for arrays and functions. We must not call it in the
4200 case where inside_init is a null pointer constant. */
4202 = convert_for_assignment (type
, init
, _("initialization"),
4203 NULL_TREE
, NULL_TREE
, 0);
4205 /* Check to see if we have already given an error message. */
4206 if (inside_init
== error_mark_node
)
4208 else if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4210 error_init ("initializer element is not constant");
4211 inside_init
= error_mark_node
;
4213 else if (require_constant
4214 && !initializer_constant_valid_p (inside_init
,
4215 TREE_TYPE (inside_init
)))
4217 error_init ("initializer element is not computable at load time");
4218 inside_init
= error_mark_node
;
4224 /* Come here only for records and arrays. */
4226 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
4228 error_init ("variable-sized object may not be initialized");
4229 return error_mark_node
;
4232 error_init ("invalid initializer");
4233 return error_mark_node
;
4236 /* Handle initializers that use braces. */
4238 /* Type of object we are accumulating a constructor for.
4239 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4240 static tree constructor_type
;
4242 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4244 static tree constructor_fields
;
4246 /* For an ARRAY_TYPE, this is the specified index
4247 at which to store the next element we get. */
4248 static tree constructor_index
;
4250 /* For an ARRAY_TYPE, this is the maximum index. */
4251 static tree constructor_max_index
;
4253 /* For a RECORD_TYPE, this is the first field not yet written out. */
4254 static tree constructor_unfilled_fields
;
4256 /* For an ARRAY_TYPE, this is the index of the first element
4257 not yet written out. */
4258 static tree constructor_unfilled_index
;
4260 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4261 This is so we can generate gaps between fields, when appropriate. */
4262 static tree constructor_bit_index
;
4264 /* If we are saving up the elements rather than allocating them,
4265 this is the list of elements so far (in reverse order,
4266 most recent first). */
4267 static tree constructor_elements
;
4269 /* 1 if constructor should be incrementally stored into a constructor chain,
4270 0 if all the elements should be kept in AVL tree. */
4271 static int constructor_incremental
;
4273 /* 1 if so far this constructor's elements are all compile-time constants. */
4274 static int constructor_constant
;
4276 /* 1 if so far this constructor's elements are all valid address constants. */
4277 static int constructor_simple
;
4279 /* 1 if this constructor is erroneous so far. */
4280 static int constructor_erroneous
;
4282 /* Structure for managing pending initializer elements, organized as an
4287 struct init_node
*left
, *right
;
4288 struct init_node
*parent
;
4294 /* Tree of pending elements at this constructor level.
4295 These are elements encountered out of order
4296 which belong at places we haven't reached yet in actually
4298 Will never hold tree nodes across GC runs. */
4299 static struct init_node
*constructor_pending_elts
;
4301 /* The SPELLING_DEPTH of this constructor. */
4302 static int constructor_depth
;
4304 /* 0 if implicitly pushing constructor levels is allowed. */
4305 int constructor_no_implicit
= 0; /* 0 for C; 1 for some other languages. */
4307 /* DECL node for which an initializer is being read.
4308 0 means we are reading a constructor expression
4309 such as (struct foo) {...}. */
4310 static tree constructor_decl
;
4312 /* Nonzero if this is an initializer for a top-level decl. */
4313 static int constructor_top_level
;
4315 /* Nonzero if there were any member designators in this initializer. */
4316 static int constructor_designated
;
4318 /* Nesting depth of designator list. */
4319 static int designator_depth
;
4321 /* Nonzero if there were diagnosed errors in this designator list. */
4322 static int designator_errorneous
;
4325 /* This stack has a level for each implicit or explicit level of
4326 structuring in the initializer, including the outermost one. It
4327 saves the values of most of the variables above. */
4329 struct constructor_range_stack
;
4331 struct constructor_stack
4333 struct constructor_stack
*next
;
4338 tree unfilled_index
;
4339 tree unfilled_fields
;
4342 struct init_node
*pending_elts
;
4345 /* If value nonzero, this value should replace the entire
4346 constructor at this level. */
4347 struct c_expr replacement_value
;
4348 struct constructor_range_stack
*range_stack
;
4358 struct constructor_stack
*constructor_stack
;
4360 /* This stack represents designators from some range designator up to
4361 the last designator in the list. */
4363 struct constructor_range_stack
4365 struct constructor_range_stack
*next
, *prev
;
4366 struct constructor_stack
*stack
;
4373 struct constructor_range_stack
*constructor_range_stack
;
4375 /* This stack records separate initializers that are nested.
4376 Nested initializers can't happen in ANSI C, but GNU C allows them
4377 in cases like { ... (struct foo) { ... } ... }. */
4379 struct initializer_stack
4381 struct initializer_stack
*next
;
4383 struct constructor_stack
*constructor_stack
;
4384 struct constructor_range_stack
*constructor_range_stack
;
4386 struct spelling
*spelling
;
4387 struct spelling
*spelling_base
;
4390 char require_constant_value
;
4391 char require_constant_elements
;
4394 struct initializer_stack
*initializer_stack
;
4396 /* Prepare to parse and output the initializer for variable DECL. */
4399 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
4402 struct initializer_stack
*p
= xmalloc (sizeof (struct initializer_stack
));
4404 p
->decl
= constructor_decl
;
4405 p
->require_constant_value
= require_constant_value
;
4406 p
->require_constant_elements
= require_constant_elements
;
4407 p
->constructor_stack
= constructor_stack
;
4408 p
->constructor_range_stack
= constructor_range_stack
;
4409 p
->elements
= constructor_elements
;
4410 p
->spelling
= spelling
;
4411 p
->spelling_base
= spelling_base
;
4412 p
->spelling_size
= spelling_size
;
4413 p
->top_level
= constructor_top_level
;
4414 p
->next
= initializer_stack
;
4415 initializer_stack
= p
;
4417 constructor_decl
= decl
;
4418 constructor_designated
= 0;
4419 constructor_top_level
= top_level
;
4423 require_constant_value
= TREE_STATIC (decl
);
4424 require_constant_elements
4425 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
4426 /* For a scalar, you can always use any value to initialize,
4427 even within braces. */
4428 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
4429 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
4430 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
4431 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
4432 locus
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4436 require_constant_value
= 0;
4437 require_constant_elements
= 0;
4438 locus
= "(anonymous)";
4441 constructor_stack
= 0;
4442 constructor_range_stack
= 0;
4444 missing_braces_mentioned
= 0;
4448 RESTORE_SPELLING_DEPTH (0);
4451 push_string (locus
);
4457 struct initializer_stack
*p
= initializer_stack
;
4459 /* Free the whole constructor stack of this initializer. */
4460 while (constructor_stack
)
4462 struct constructor_stack
*q
= constructor_stack
;
4463 constructor_stack
= q
->next
;
4467 gcc_assert (!constructor_range_stack
);
4469 /* Pop back to the data of the outer initializer (if any). */
4470 free (spelling_base
);
4472 constructor_decl
= p
->decl
;
4473 require_constant_value
= p
->require_constant_value
;
4474 require_constant_elements
= p
->require_constant_elements
;
4475 constructor_stack
= p
->constructor_stack
;
4476 constructor_range_stack
= p
->constructor_range_stack
;
4477 constructor_elements
= p
->elements
;
4478 spelling
= p
->spelling
;
4479 spelling_base
= p
->spelling_base
;
4480 spelling_size
= p
->spelling_size
;
4481 constructor_top_level
= p
->top_level
;
4482 initializer_stack
= p
->next
;
4486 /* Call here when we see the initializer is surrounded by braces.
4487 This is instead of a call to push_init_level;
4488 it is matched by a call to pop_init_level.
4490 TYPE is the type to initialize, for a constructor expression.
4491 For an initializer for a decl, TYPE is zero. */
4494 really_start_incremental_init (tree type
)
4496 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
4499 type
= TREE_TYPE (constructor_decl
);
4501 if (targetm
.vector_opaque_p (type
))
4502 error ("opaque vector types cannot be initialized");
4504 p
->type
= constructor_type
;
4505 p
->fields
= constructor_fields
;
4506 p
->index
= constructor_index
;
4507 p
->max_index
= constructor_max_index
;
4508 p
->unfilled_index
= constructor_unfilled_index
;
4509 p
->unfilled_fields
= constructor_unfilled_fields
;
4510 p
->bit_index
= constructor_bit_index
;
4511 p
->elements
= constructor_elements
;
4512 p
->constant
= constructor_constant
;
4513 p
->simple
= constructor_simple
;
4514 p
->erroneous
= constructor_erroneous
;
4515 p
->pending_elts
= constructor_pending_elts
;
4516 p
->depth
= constructor_depth
;
4517 p
->replacement_value
.value
= 0;
4518 p
->replacement_value
.original_code
= ERROR_MARK
;
4522 p
->incremental
= constructor_incremental
;
4523 p
->designated
= constructor_designated
;
4525 constructor_stack
= p
;
4527 constructor_constant
= 1;
4528 constructor_simple
= 1;
4529 constructor_depth
= SPELLING_DEPTH ();
4530 constructor_elements
= 0;
4531 constructor_pending_elts
= 0;
4532 constructor_type
= type
;
4533 constructor_incremental
= 1;
4534 constructor_designated
= 0;
4535 designator_depth
= 0;
4536 designator_errorneous
= 0;
4538 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4539 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4541 constructor_fields
= TYPE_FIELDS (constructor_type
);
4542 /* Skip any nameless bit fields at the beginning. */
4543 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4544 && DECL_NAME (constructor_fields
) == 0)
4545 constructor_fields
= TREE_CHAIN (constructor_fields
);
4547 constructor_unfilled_fields
= constructor_fields
;
4548 constructor_bit_index
= bitsize_zero_node
;
4550 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4552 if (TYPE_DOMAIN (constructor_type
))
4554 constructor_max_index
4555 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4557 /* Detect non-empty initializations of zero-length arrays. */
4558 if (constructor_max_index
== NULL_TREE
4559 && TYPE_SIZE (constructor_type
))
4560 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4562 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4563 to initialize VLAs will cause a proper error; avoid tree
4564 checking errors as well by setting a safe value. */
4565 if (constructor_max_index
4566 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4567 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4570 = convert (bitsizetype
,
4571 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4574 constructor_index
= bitsize_zero_node
;
4576 constructor_unfilled_index
= constructor_index
;
4578 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4580 /* Vectors are like simple fixed-size arrays. */
4581 constructor_max_index
=
4582 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
4583 constructor_index
= convert (bitsizetype
, bitsize_zero_node
);
4584 constructor_unfilled_index
= constructor_index
;
4588 /* Handle the case of int x = {5}; */
4589 constructor_fields
= constructor_type
;
4590 constructor_unfilled_fields
= constructor_type
;
4594 /* Push down into a subobject, for initialization.
4595 If this is for an explicit set of braces, IMPLICIT is 0.
4596 If it is because the next element belongs at a lower level,
4597 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4600 push_init_level (int implicit
)
4602 struct constructor_stack
*p
;
4603 tree value
= NULL_TREE
;
4605 /* If we've exhausted any levels that didn't have braces,
4607 while (constructor_stack
->implicit
)
4609 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4610 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4611 && constructor_fields
== 0)
4612 process_init_element (pop_init_level (1));
4613 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
4614 && constructor_max_index
4615 && tree_int_cst_lt (constructor_max_index
, constructor_index
))
4616 process_init_element (pop_init_level (1));
4621 /* Unless this is an explicit brace, we need to preserve previous
4625 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4626 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4627 && constructor_fields
)
4628 value
= find_init_member (constructor_fields
);
4629 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4630 value
= find_init_member (constructor_index
);
4633 p
= XNEW (struct constructor_stack
);
4634 p
->type
= constructor_type
;
4635 p
->fields
= constructor_fields
;
4636 p
->index
= constructor_index
;
4637 p
->max_index
= constructor_max_index
;
4638 p
->unfilled_index
= constructor_unfilled_index
;
4639 p
->unfilled_fields
= constructor_unfilled_fields
;
4640 p
->bit_index
= constructor_bit_index
;
4641 p
->elements
= constructor_elements
;
4642 p
->constant
= constructor_constant
;
4643 p
->simple
= constructor_simple
;
4644 p
->erroneous
= constructor_erroneous
;
4645 p
->pending_elts
= constructor_pending_elts
;
4646 p
->depth
= constructor_depth
;
4647 p
->replacement_value
.value
= 0;
4648 p
->replacement_value
.original_code
= ERROR_MARK
;
4649 p
->implicit
= implicit
;
4651 p
->incremental
= constructor_incremental
;
4652 p
->designated
= constructor_designated
;
4653 p
->next
= constructor_stack
;
4655 constructor_stack
= p
;
4657 constructor_constant
= 1;
4658 constructor_simple
= 1;
4659 constructor_depth
= SPELLING_DEPTH ();
4660 constructor_elements
= 0;
4661 constructor_incremental
= 1;
4662 constructor_designated
= 0;
4663 constructor_pending_elts
= 0;
4666 p
->range_stack
= constructor_range_stack
;
4667 constructor_range_stack
= 0;
4668 designator_depth
= 0;
4669 designator_errorneous
= 0;
4672 /* Don't die if an entire brace-pair level is superfluous
4673 in the containing level. */
4674 if (constructor_type
== 0)
4676 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
4677 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4679 /* Don't die if there are extra init elts at the end. */
4680 if (constructor_fields
== 0)
4681 constructor_type
= 0;
4684 constructor_type
= TREE_TYPE (constructor_fields
);
4685 push_member_name (constructor_fields
);
4686 constructor_depth
++;
4689 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4691 constructor_type
= TREE_TYPE (constructor_type
);
4692 push_array_bounds (tree_low_cst (constructor_index
, 0));
4693 constructor_depth
++;
4696 if (constructor_type
== 0)
4698 error_init ("extra brace group at end of initializer");
4699 constructor_fields
= 0;
4700 constructor_unfilled_fields
= 0;
4704 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
4706 constructor_constant
= TREE_CONSTANT (value
);
4707 constructor_simple
= TREE_STATIC (value
);
4708 constructor_elements
= CONSTRUCTOR_ELTS (value
);
4709 if (constructor_elements
4710 && (TREE_CODE (constructor_type
) == RECORD_TYPE
4711 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
4712 set_nonincremental_init ();
4715 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
4717 missing_braces_mentioned
= 1;
4718 warning_init ("missing braces around initializer");
4721 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4722 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4724 constructor_fields
= TYPE_FIELDS (constructor_type
);
4725 /* Skip any nameless bit fields at the beginning. */
4726 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4727 && DECL_NAME (constructor_fields
) == 0)
4728 constructor_fields
= TREE_CHAIN (constructor_fields
);
4730 constructor_unfilled_fields
= constructor_fields
;
4731 constructor_bit_index
= bitsize_zero_node
;
4733 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4735 /* Vectors are like simple fixed-size arrays. */
4736 constructor_max_index
=
4737 build_int_cst (NULL_TREE
, TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
4738 constructor_index
= convert (bitsizetype
, integer_zero_node
);
4739 constructor_unfilled_index
= constructor_index
;
4741 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4743 if (TYPE_DOMAIN (constructor_type
))
4745 constructor_max_index
4746 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4748 /* Detect non-empty initializations of zero-length arrays. */
4749 if (constructor_max_index
== NULL_TREE
4750 && TYPE_SIZE (constructor_type
))
4751 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4753 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4754 to initialize VLAs will cause a proper error; avoid tree
4755 checking errors as well by setting a safe value. */
4756 if (constructor_max_index
4757 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4758 constructor_max_index
= build_int_cst (NULL_TREE
, -1);
4761 = convert (bitsizetype
,
4762 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4765 constructor_index
= bitsize_zero_node
;
4767 constructor_unfilled_index
= constructor_index
;
4768 if (value
&& TREE_CODE (value
) == STRING_CST
)
4770 /* We need to split the char/wchar array into individual
4771 characters, so that we don't have to special case it
4773 set_nonincremental_init_from_string (value
);
4778 warning_init ("braces around scalar initializer");
4779 constructor_fields
= constructor_type
;
4780 constructor_unfilled_fields
= constructor_type
;
4784 /* At the end of an implicit or explicit brace level,
4785 finish up that level of constructor. If a single expression
4786 with redundant braces initialized that level, return the
4787 c_expr structure for that expression. Otherwise, the original_code
4788 element is set to ERROR_MARK.
4789 If we were outputting the elements as they are read, return 0 as the value
4790 from inner levels (process_init_element ignores that),
4791 but return error_mark_node as the value from the outermost level
4792 (that's what we want to put in DECL_INITIAL).
4793 Otherwise, return a CONSTRUCTOR expression as the value. */
4796 pop_init_level (int implicit
)
4798 struct constructor_stack
*p
;
4801 ret
.original_code
= ERROR_MARK
;
4805 /* When we come to an explicit close brace,
4806 pop any inner levels that didn't have explicit braces. */
4807 while (constructor_stack
->implicit
)
4808 process_init_element (pop_init_level (1));
4810 gcc_assert (!constructor_range_stack
);
4813 /* Now output all pending elements. */
4814 constructor_incremental
= 1;
4815 output_pending_init_elements (1);
4817 p
= constructor_stack
;
4819 /* Error for initializing a flexible array member, or a zero-length
4820 array member in an inappropriate context. */
4821 if (constructor_type
&& constructor_fields
4822 && TREE_CODE (constructor_type
) == ARRAY_TYPE
4823 && TYPE_DOMAIN (constructor_type
)
4824 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
4826 /* Silently discard empty initializations. The parser will
4827 already have pedwarned for empty brackets. */
4828 if (integer_zerop (constructor_unfilled_index
))
4829 constructor_type
= NULL_TREE
;
4832 gcc_assert (!TYPE_SIZE (constructor_type
));
4834 if (constructor_depth
> 2)
4835 error_init ("initialization of flexible array member in a nested context");
4837 pedwarn_init ("initialization of a flexible array member");
4839 /* We have already issued an error message for the existence
4840 of a flexible array member not at the end of the structure.
4841 Discard the initializer so that we do not abort later. */
4842 if (TREE_CHAIN (constructor_fields
) != NULL_TREE
)
4843 constructor_type
= NULL_TREE
;
4847 /* Warn when some struct elements are implicitly initialized to zero. */
4848 if (warn_missing_field_initializers
4850 && TREE_CODE (constructor_type
) == RECORD_TYPE
4851 && constructor_unfilled_fields
)
4853 /* Do not warn for flexible array members or zero-length arrays. */
4854 while (constructor_unfilled_fields
4855 && (! DECL_SIZE (constructor_unfilled_fields
)
4856 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
4857 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
4859 /* Do not warn if this level of the initializer uses member
4860 designators; it is likely to be deliberate. */
4861 if (constructor_unfilled_fields
&& !constructor_designated
)
4863 push_member_name (constructor_unfilled_fields
);
4864 warning_init ("missing initializer");
4865 RESTORE_SPELLING_DEPTH (constructor_depth
);
4869 /* Pad out the end of the structure. */
4870 if (p
->replacement_value
.value
)
4871 /* If this closes a superfluous brace pair,
4872 just pass out the element between them. */
4873 ret
= p
->replacement_value
;
4874 else if (constructor_type
== 0)
4876 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
4877 && TREE_CODE (constructor_type
) != UNION_TYPE
4878 && TREE_CODE (constructor_type
) != ARRAY_TYPE
4879 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
4881 /* A nonincremental scalar initializer--just return
4882 the element, after verifying there is just one. */
4883 if (constructor_elements
== 0)
4885 if (!constructor_erroneous
)
4886 error_init ("empty scalar initializer");
4887 ret
.value
= error_mark_node
;
4889 else if (TREE_CHAIN (constructor_elements
) != 0)
4891 error_init ("extra elements in scalar initializer");
4892 ret
.value
= TREE_VALUE (constructor_elements
);
4895 ret
.value
= TREE_VALUE (constructor_elements
);
4899 if (constructor_erroneous
)
4900 ret
.value
= error_mark_node
;
4903 ret
.value
= build_constructor (constructor_type
,
4904 nreverse (constructor_elements
));
4905 if (constructor_constant
)
4906 TREE_CONSTANT (ret
.value
) = TREE_INVARIANT (ret
.value
) = 1;
4907 if (constructor_constant
&& constructor_simple
)
4908 TREE_STATIC (ret
.value
) = 1;
4912 constructor_type
= p
->type
;
4913 constructor_fields
= p
->fields
;
4914 constructor_index
= p
->index
;
4915 constructor_max_index
= p
->max_index
;
4916 constructor_unfilled_index
= p
->unfilled_index
;
4917 constructor_unfilled_fields
= p
->unfilled_fields
;
4918 constructor_bit_index
= p
->bit_index
;
4919 constructor_elements
= p
->elements
;
4920 constructor_constant
= p
->constant
;
4921 constructor_simple
= p
->simple
;
4922 constructor_erroneous
= p
->erroneous
;
4923 constructor_incremental
= p
->incremental
;
4924 constructor_designated
= p
->designated
;
4925 constructor_pending_elts
= p
->pending_elts
;
4926 constructor_depth
= p
->depth
;
4928 constructor_range_stack
= p
->range_stack
;
4929 RESTORE_SPELLING_DEPTH (constructor_depth
);
4931 constructor_stack
= p
->next
;
4936 if (constructor_stack
== 0)
4938 ret
.value
= error_mark_node
;
4946 /* Common handling for both array range and field name designators.
4947 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4950 set_designator (int array
)
4953 enum tree_code subcode
;
4955 /* Don't die if an entire brace-pair level is superfluous
4956 in the containing level. */
4957 if (constructor_type
== 0)
4960 /* If there were errors in this designator list already, bail out
4962 if (designator_errorneous
)
4965 if (!designator_depth
)
4967 gcc_assert (!constructor_range_stack
);
4969 /* Designator list starts at the level of closest explicit
4971 while (constructor_stack
->implicit
)
4972 process_init_element (pop_init_level (1));
4973 constructor_designated
= 1;
4977 if (constructor_no_implicit
)
4979 error_init ("initialization designators may not nest");
4983 switch (TREE_CODE (constructor_type
))
4987 subtype
= TREE_TYPE (constructor_fields
);
4988 if (subtype
!= error_mark_node
)
4989 subtype
= TYPE_MAIN_VARIANT (subtype
);
4992 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
4998 subcode
= TREE_CODE (subtype
);
4999 if (array
&& subcode
!= ARRAY_TYPE
)
5001 error_init ("array index in non-array initializer");
5004 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
5006 error_init ("field name not in record or union initializer");
5010 constructor_designated
= 1;
5011 push_init_level (2);
5015 /* If there are range designators in designator list, push a new designator
5016 to constructor_range_stack. RANGE_END is end of such stack range or
5017 NULL_TREE if there is no range designator at this level. */
5020 push_range_stack (tree range_end
)
5022 struct constructor_range_stack
*p
;
5024 p
= GGC_NEW (struct constructor_range_stack
);
5025 p
->prev
= constructor_range_stack
;
5027 p
->fields
= constructor_fields
;
5028 p
->range_start
= constructor_index
;
5029 p
->index
= constructor_index
;
5030 p
->stack
= constructor_stack
;
5031 p
->range_end
= range_end
;
5032 if (constructor_range_stack
)
5033 constructor_range_stack
->next
= p
;
5034 constructor_range_stack
= p
;
5037 /* Within an array initializer, specify the next index to be initialized.
5038 FIRST is that index. If LAST is nonzero, then initialize a range
5039 of indices, running from FIRST through LAST. */
5042 set_init_index (tree first
, tree last
)
5044 if (set_designator (1))
5047 designator_errorneous
= 1;
5049 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
5050 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
5052 error_init ("array index in initializer not of integer type");
5056 while ((TREE_CODE (first
) == NOP_EXPR
5057 || TREE_CODE (first
) == CONVERT_EXPR
5058 || TREE_CODE (first
) == NON_LVALUE_EXPR
)
5059 && (TYPE_MODE (TREE_TYPE (first
))
5060 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first
, 0)))))
5061 first
= TREE_OPERAND (first
, 0);
5064 while ((TREE_CODE (last
) == NOP_EXPR
5065 || TREE_CODE (last
) == CONVERT_EXPR
5066 || TREE_CODE (last
) == NON_LVALUE_EXPR
)
5067 && (TYPE_MODE (TREE_TYPE (last
))
5068 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last
, 0)))))
5069 last
= TREE_OPERAND (last
, 0);
5071 if (TREE_CODE (first
) != INTEGER_CST
)
5072 error_init ("nonconstant array index in initializer");
5073 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
5074 error_init ("nonconstant array index in initializer");
5075 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5076 error_init ("array index in non-array initializer");
5077 else if (tree_int_cst_sgn (first
) == -1)
5078 error_init ("array index in initializer exceeds array bounds");
5079 else if (constructor_max_index
5080 && tree_int_cst_lt (constructor_max_index
, first
))
5081 error_init ("array index in initializer exceeds array bounds");
5084 constructor_index
= convert (bitsizetype
, first
);
5088 if (tree_int_cst_equal (first
, last
))
5090 else if (tree_int_cst_lt (last
, first
))
5092 error_init ("empty index range in initializer");
5097 last
= convert (bitsizetype
, last
);
5098 if (constructor_max_index
!= 0
5099 && tree_int_cst_lt (constructor_max_index
, last
))
5101 error_init ("array index range in initializer exceeds array bounds");
5108 designator_errorneous
= 0;
5109 if (constructor_range_stack
|| last
)
5110 push_range_stack (last
);
5114 /* Within a struct initializer, specify the next field to be initialized. */
5117 set_init_label (tree fieldname
)
5121 if (set_designator (0))
5124 designator_errorneous
= 1;
5126 if (TREE_CODE (constructor_type
) != RECORD_TYPE
5127 && TREE_CODE (constructor_type
) != UNION_TYPE
)
5129 error_init ("field name not in record or union initializer");
5133 for (tail
= TYPE_FIELDS (constructor_type
); tail
;
5134 tail
= TREE_CHAIN (tail
))
5136 if (DECL_NAME (tail
) == fieldname
)
5141 error ("unknown field %qs specified in initializer",
5142 IDENTIFIER_POINTER (fieldname
));
5145 constructor_fields
= tail
;
5147 designator_errorneous
= 0;
5148 if (constructor_range_stack
)
5149 push_range_stack (NULL_TREE
);
5153 /* Add a new initializer to the tree of pending initializers. PURPOSE
5154 identifies the initializer, either array index or field in a structure.
5155 VALUE is the value of that index or field. */
5158 add_pending_init (tree purpose
, tree value
)
5160 struct init_node
*p
, **q
, *r
;
5162 q
= &constructor_pending_elts
;
5165 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5170 if (tree_int_cst_lt (purpose
, p
->purpose
))
5172 else if (tree_int_cst_lt (p
->purpose
, purpose
))
5176 if (TREE_SIDE_EFFECTS (p
->value
))
5177 warning_init ("initialized field with side-effects overwritten");
5187 bitpos
= bit_position (purpose
);
5191 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5193 else if (p
->purpose
!= purpose
)
5197 if (TREE_SIDE_EFFECTS (p
->value
))
5198 warning_init ("initialized field with side-effects overwritten");
5205 r
= GGC_NEW (struct init_node
);
5206 r
->purpose
= purpose
;
5217 struct init_node
*s
;
5221 if (p
->balance
== 0)
5223 else if (p
->balance
< 0)
5230 p
->left
->parent
= p
;
5247 constructor_pending_elts
= r
;
5252 struct init_node
*t
= r
->right
;
5256 r
->right
->parent
= r
;
5261 p
->left
->parent
= p
;
5264 p
->balance
= t
->balance
< 0;
5265 r
->balance
= -(t
->balance
> 0);
5280 constructor_pending_elts
= t
;
5286 /* p->balance == +1; growth of left side balances the node. */
5291 else /* r == p->right */
5293 if (p
->balance
== 0)
5294 /* Growth propagation from right side. */
5296 else if (p
->balance
> 0)
5303 p
->right
->parent
= p
;
5320 constructor_pending_elts
= r
;
5322 else /* r->balance == -1 */
5325 struct init_node
*t
= r
->left
;
5329 r
->left
->parent
= r
;
5334 p
->right
->parent
= p
;
5337 r
->balance
= (t
->balance
< 0);
5338 p
->balance
= -(t
->balance
> 0);
5353 constructor_pending_elts
= t
;
5359 /* p->balance == -1; growth of right side balances the node. */
5370 /* Build AVL tree from a sorted chain. */
5373 set_nonincremental_init (void)
5377 if (TREE_CODE (constructor_type
) != RECORD_TYPE
5378 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5381 for (chain
= constructor_elements
; chain
; chain
= TREE_CHAIN (chain
))
5382 add_pending_init (TREE_PURPOSE (chain
), TREE_VALUE (chain
));
5383 constructor_elements
= 0;
5384 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5386 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
5387 /* Skip any nameless bit fields at the beginning. */
5388 while (constructor_unfilled_fields
!= 0
5389 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5390 && DECL_NAME (constructor_unfilled_fields
) == 0)
5391 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
5394 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5396 if (TYPE_DOMAIN (constructor_type
))
5397 constructor_unfilled_index
5398 = convert (bitsizetype
,
5399 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
5401 constructor_unfilled_index
= bitsize_zero_node
;
5403 constructor_incremental
= 0;
5406 /* Build AVL tree from a string constant. */
5409 set_nonincremental_init_from_string (tree str
)
5411 tree value
, purpose
, type
;
5412 HOST_WIDE_INT val
[2];
5413 const char *p
, *end
;
5414 int byte
, wchar_bytes
, charwidth
, bitpos
;
5416 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
5418 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5419 == TYPE_PRECISION (char_type_node
))
5423 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5424 == TYPE_PRECISION (wchar_type_node
));
5425 wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
5427 charwidth
= TYPE_PRECISION (char_type_node
);
5428 type
= TREE_TYPE (constructor_type
);
5429 p
= TREE_STRING_POINTER (str
);
5430 end
= p
+ TREE_STRING_LENGTH (str
);
5432 for (purpose
= bitsize_zero_node
;
5433 p
< end
&& !tree_int_cst_lt (constructor_max_index
, purpose
);
5434 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
5436 if (wchar_bytes
== 1)
5438 val
[1] = (unsigned char) *p
++;
5445 for (byte
= 0; byte
< wchar_bytes
; byte
++)
5447 if (BYTES_BIG_ENDIAN
)
5448 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
5450 bitpos
= byte
* charwidth
;
5451 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
5452 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
5453 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
5457 if (!TYPE_UNSIGNED (type
))
5459 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
5460 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
5462 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
5464 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
5468 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
5473 else if (val
[0] & (((HOST_WIDE_INT
) 1)
5474 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
5475 val
[0] |= ((HOST_WIDE_INT
) -1)
5476 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
5479 value
= build_int_cst_wide (type
, val
[1], val
[0]);
5480 add_pending_init (purpose
, value
);
5483 constructor_incremental
= 0;
5486 /* Return value of FIELD in pending initializer or zero if the field was
5487 not initialized yet. */
5490 find_init_member (tree field
)
5492 struct init_node
*p
;
5494 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5496 if (constructor_incremental
5497 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5498 set_nonincremental_init ();
5500 p
= constructor_pending_elts
;
5503 if (tree_int_cst_lt (field
, p
->purpose
))
5505 else if (tree_int_cst_lt (p
->purpose
, field
))
5511 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5513 tree bitpos
= bit_position (field
);
5515 if (constructor_incremental
5516 && (!constructor_unfilled_fields
5517 || tree_int_cst_lt (bitpos
,
5518 bit_position (constructor_unfilled_fields
))))
5519 set_nonincremental_init ();
5521 p
= constructor_pending_elts
;
5524 if (field
== p
->purpose
)
5526 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5532 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5534 if (constructor_elements
5535 && TREE_PURPOSE (constructor_elements
) == field
)
5536 return TREE_VALUE (constructor_elements
);
5541 /* "Output" the next constructor element.
5542 At top level, really output it to assembler code now.
5543 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5544 TYPE is the data type that the containing data type wants here.
5545 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5546 If VALUE is a string constant, STRICT_STRING is true if it is
5547 unparenthesized or we should not warn here for it being parenthesized.
5548 For other types of VALUE, STRICT_STRING is not used.
5550 PENDING if non-nil means output pending elements that belong
5551 right after this element. (PENDING is normally 1;
5552 it is 0 while outputting pending elements, to avoid recursion.) */
5555 output_init_element (tree value
, bool strict_string
, tree type
, tree field
,
5558 if (type
== error_mark_node
)
5560 constructor_erroneous
= 1;
5563 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5564 || (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5565 && !(TREE_CODE (value
) == STRING_CST
5566 && TREE_CODE (type
) == ARRAY_TYPE
5567 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
5568 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
5569 TYPE_MAIN_VARIANT (type
))))
5570 value
= default_conversion (value
);
5572 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
5573 && require_constant_value
&& !flag_isoc99
&& pending
)
5575 /* As an extension, allow initializing objects with static storage
5576 duration with compound literals (which are then treated just as
5577 the brace enclosed list they contain). */
5578 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
5579 value
= DECL_INITIAL (decl
);
5582 if (value
== error_mark_node
)
5583 constructor_erroneous
= 1;
5584 else if (!TREE_CONSTANT (value
))
5585 constructor_constant
= 0;
5586 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
5587 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
5588 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5589 && DECL_C_BIT_FIELD (field
)
5590 && TREE_CODE (value
) != INTEGER_CST
))
5591 constructor_simple
= 0;
5593 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
5595 if (require_constant_value
)
5597 error_init ("initializer element is not constant");
5598 value
= error_mark_node
;
5600 else if (require_constant_elements
)
5601 pedwarn ("initializer element is not computable at load time");
5604 /* If this field is empty (and not at the end of structure),
5605 don't do anything other than checking the initializer. */
5607 && (TREE_TYPE (field
) == error_mark_node
5608 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
5609 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
5610 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
5611 || TREE_CHAIN (field
)))))
5614 value
= digest_init (type
, value
, strict_string
, require_constant_value
);
5615 if (value
== error_mark_node
)
5617 constructor_erroneous
= 1;
5621 /* If this element doesn't come next in sequence,
5622 put it on constructor_pending_elts. */
5623 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5624 && (!constructor_incremental
5625 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
5627 if (constructor_incremental
5628 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5629 set_nonincremental_init ();
5631 add_pending_init (field
, value
);
5634 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5635 && (!constructor_incremental
5636 || field
!= constructor_unfilled_fields
))
5638 /* We do this for records but not for unions. In a union,
5639 no matter which field is specified, it can be initialized
5640 right away since it starts at the beginning of the union. */
5641 if (constructor_incremental
)
5643 if (!constructor_unfilled_fields
)
5644 set_nonincremental_init ();
5647 tree bitpos
, unfillpos
;
5649 bitpos
= bit_position (field
);
5650 unfillpos
= bit_position (constructor_unfilled_fields
);
5652 if (tree_int_cst_lt (bitpos
, unfillpos
))
5653 set_nonincremental_init ();
5657 add_pending_init (field
, value
);
5660 else if (TREE_CODE (constructor_type
) == UNION_TYPE
5661 && constructor_elements
)
5663 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements
)))
5664 warning_init ("initialized field with side-effects overwritten");
5666 /* We can have just one union field set. */
5667 constructor_elements
= 0;
5670 /* Otherwise, output this element either to
5671 constructor_elements or to the assembler file. */
5673 if (field
&& TREE_CODE (field
) == INTEGER_CST
)
5674 field
= copy_node (field
);
5675 constructor_elements
5676 = tree_cons (field
, value
, constructor_elements
);
5678 /* Advance the variable that indicates sequential elements output. */
5679 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5680 constructor_unfilled_index
5681 = size_binop (PLUS_EXPR
, constructor_unfilled_index
,
5683 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5685 constructor_unfilled_fields
5686 = TREE_CHAIN (constructor_unfilled_fields
);
5688 /* Skip any nameless bit fields. */
5689 while (constructor_unfilled_fields
!= 0
5690 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5691 && DECL_NAME (constructor_unfilled_fields
) == 0)
5692 constructor_unfilled_fields
=
5693 TREE_CHAIN (constructor_unfilled_fields
);
5695 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5696 constructor_unfilled_fields
= 0;
5698 /* Now output any pending elements which have become next. */
5700 output_pending_init_elements (0);
5703 /* Output any pending elements which have become next.
5704 As we output elements, constructor_unfilled_{fields,index}
5705 advances, which may cause other elements to become next;
5706 if so, they too are output.
5708 If ALL is 0, we return when there are
5709 no more pending elements to output now.
5711 If ALL is 1, we output space as necessary so that
5712 we can output all the pending elements. */
5715 output_pending_init_elements (int all
)
5717 struct init_node
*elt
= constructor_pending_elts
;
5722 /* Look through the whole pending tree.
5723 If we find an element that should be output now,
5724 output it. Otherwise, set NEXT to the element
5725 that comes first among those still pending. */
5730 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5732 if (tree_int_cst_equal (elt
->purpose
,
5733 constructor_unfilled_index
))
5734 output_init_element (elt
->value
, true,
5735 TREE_TYPE (constructor_type
),
5736 constructor_unfilled_index
, 0);
5737 else if (tree_int_cst_lt (constructor_unfilled_index
,
5740 /* Advance to the next smaller node. */
5745 /* We have reached the smallest node bigger than the
5746 current unfilled index. Fill the space first. */
5747 next
= elt
->purpose
;
5753 /* Advance to the next bigger node. */
5758 /* We have reached the biggest node in a subtree. Find
5759 the parent of it, which is the next bigger node. */
5760 while (elt
->parent
&& elt
->parent
->right
== elt
)
5763 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
5766 next
= elt
->purpose
;
5772 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5773 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5775 tree ctor_unfilled_bitpos
, elt_bitpos
;
5777 /* If the current record is complete we are done. */
5778 if (constructor_unfilled_fields
== 0)
5781 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
5782 elt_bitpos
= bit_position (elt
->purpose
);
5783 /* We can't compare fields here because there might be empty
5784 fields in between. */
5785 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
5787 constructor_unfilled_fields
= elt
->purpose
;
5788 output_init_element (elt
->value
, true, TREE_TYPE (elt
->purpose
),
5791 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
5793 /* Advance to the next smaller node. */
5798 /* We have reached the smallest node bigger than the
5799 current unfilled field. Fill the space first. */
5800 next
= elt
->purpose
;
5806 /* Advance to the next bigger node. */
5811 /* We have reached the biggest node in a subtree. Find
5812 the parent of it, which is the next bigger node. */
5813 while (elt
->parent
&& elt
->parent
->right
== elt
)
5817 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
5818 bit_position (elt
->purpose
))))
5820 next
= elt
->purpose
;
5828 /* Ordinarily return, but not if we want to output all
5829 and there are elements left. */
5830 if (! (all
&& next
!= 0))
5833 /* If it's not incremental, just skip over the gap, so that after
5834 jumping to retry we will output the next successive element. */
5835 if (TREE_CODE (constructor_type
) == RECORD_TYPE
5836 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5837 constructor_unfilled_fields
= next
;
5838 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5839 constructor_unfilled_index
= next
;
5841 /* ELT now points to the node in the pending tree with the next
5842 initializer to output. */
5846 /* Add one non-braced element to the current constructor level.
5847 This adjusts the current position within the constructor's type.
5848 This may also start or terminate implicit levels
5849 to handle a partly-braced initializer.
5851 Once this has found the correct level for the new element,
5852 it calls output_init_element. */
5855 process_init_element (struct c_expr value
)
5857 tree orig_value
= value
.value
;
5858 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
5859 bool strict_string
= value
.original_code
== STRING_CST
;
5861 designator_depth
= 0;
5862 designator_errorneous
= 0;
5864 /* Handle superfluous braces around string cst as in
5865 char x[] = {"foo"}; */
5868 && TREE_CODE (constructor_type
) == ARRAY_TYPE
5869 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
5870 && integer_zerop (constructor_unfilled_index
))
5872 if (constructor_stack
->replacement_value
.value
)
5873 error_init ("excess elements in char array initializer");
5874 constructor_stack
->replacement_value
= value
;
5878 if (constructor_stack
->replacement_value
.value
!= 0)
5880 error_init ("excess elements in struct initializer");
5884 /* Ignore elements of a brace group if it is entirely superfluous
5885 and has already been diagnosed. */
5886 if (constructor_type
== 0)
5889 /* If we've exhausted any levels that didn't have braces,
5891 while (constructor_stack
->implicit
)
5893 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
5894 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5895 && constructor_fields
== 0)
5896 process_init_element (pop_init_level (1));
5897 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5898 && (constructor_max_index
== 0
5899 || tree_int_cst_lt (constructor_max_index
,
5900 constructor_index
)))
5901 process_init_element (pop_init_level (1));
5906 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5907 if (constructor_range_stack
)
5909 /* If value is a compound literal and we'll be just using its
5910 content, don't put it into a SAVE_EXPR. */
5911 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
5912 || !require_constant_value
5914 value
.value
= save_expr (value
.value
);
5919 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5922 enum tree_code fieldcode
;
5924 if (constructor_fields
== 0)
5926 pedwarn_init ("excess elements in struct initializer");
5930 fieldtype
= TREE_TYPE (constructor_fields
);
5931 if (fieldtype
!= error_mark_node
)
5932 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
5933 fieldcode
= TREE_CODE (fieldtype
);
5935 /* Error for non-static initialization of a flexible array member. */
5936 if (fieldcode
== ARRAY_TYPE
5937 && !require_constant_value
5938 && TYPE_SIZE (fieldtype
) == NULL_TREE
5939 && TREE_CHAIN (constructor_fields
) == NULL_TREE
)
5941 error_init ("non-static initialization of a flexible array member");
5945 /* Accept a string constant to initialize a subarray. */
5946 if (value
.value
!= 0
5947 && fieldcode
== ARRAY_TYPE
5948 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
5950 value
.value
= orig_value
;
5951 /* Otherwise, if we have come to a subaggregate,
5952 and we don't have an element of its type, push into it. */
5953 else if (value
.value
!= 0 && !constructor_no_implicit
5954 && value
.value
!= error_mark_node
5955 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
5956 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
5957 || fieldcode
== UNION_TYPE
))
5959 push_init_level (1);
5965 push_member_name (constructor_fields
);
5966 output_init_element (value
.value
, strict_string
,
5967 fieldtype
, constructor_fields
, 1);
5968 RESTORE_SPELLING_DEPTH (constructor_depth
);
5971 /* Do the bookkeeping for an element that was
5972 directly output as a constructor. */
5974 /* For a record, keep track of end position of last field. */
5975 if (DECL_SIZE (constructor_fields
))
5976 constructor_bit_index
5977 = size_binop (PLUS_EXPR
,
5978 bit_position (constructor_fields
),
5979 DECL_SIZE (constructor_fields
));
5981 /* If the current field was the first one not yet written out,
5982 it isn't now, so update. */
5983 if (constructor_unfilled_fields
== constructor_fields
)
5985 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
5986 /* Skip any nameless bit fields. */
5987 while (constructor_unfilled_fields
!= 0
5988 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5989 && DECL_NAME (constructor_unfilled_fields
) == 0)
5990 constructor_unfilled_fields
=
5991 TREE_CHAIN (constructor_unfilled_fields
);
5995 constructor_fields
= TREE_CHAIN (constructor_fields
);
5996 /* Skip any nameless bit fields at the beginning. */
5997 while (constructor_fields
!= 0
5998 && DECL_C_BIT_FIELD (constructor_fields
)
5999 && DECL_NAME (constructor_fields
) == 0)
6000 constructor_fields
= TREE_CHAIN (constructor_fields
);
6002 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
6005 enum tree_code fieldcode
;
6007 if (constructor_fields
== 0)
6009 pedwarn_init ("excess elements in union initializer");
6013 fieldtype
= TREE_TYPE (constructor_fields
);
6014 if (fieldtype
!= error_mark_node
)
6015 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
6016 fieldcode
= TREE_CODE (fieldtype
);
6018 /* Warn that traditional C rejects initialization of unions.
6019 We skip the warning if the value is zero. This is done
6020 under the assumption that the zero initializer in user
6021 code appears conditioned on e.g. __STDC__ to avoid
6022 "missing initializer" warnings and relies on default
6023 initialization to zero in the traditional C case.
6024 We also skip the warning if the initializer is designated,
6025 again on the assumption that this must be conditional on
6026 __STDC__ anyway (and we've already complained about the
6027 member-designator already). */
6028 if (warn_traditional
&& !in_system_header
&& !constructor_designated
6029 && !(value
.value
&& (integer_zerop (value
.value
)
6030 || real_zerop (value
.value
))))
6031 warning ("traditional C rejects initialization of unions");
6033 /* Accept a string constant to initialize a subarray. */
6034 if (value
.value
!= 0
6035 && fieldcode
== ARRAY_TYPE
6036 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
6038 value
.value
= orig_value
;
6039 /* Otherwise, if we have come to a subaggregate,
6040 and we don't have an element of its type, push into it. */
6041 else if (value
.value
!= 0 && !constructor_no_implicit
6042 && value
.value
!= error_mark_node
6043 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
6044 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
6045 || fieldcode
== UNION_TYPE
))
6047 push_init_level (1);
6053 push_member_name (constructor_fields
);
6054 output_init_element (value
.value
, strict_string
,
6055 fieldtype
, constructor_fields
, 1);
6056 RESTORE_SPELLING_DEPTH (constructor_depth
);
6059 /* Do the bookkeeping for an element that was
6060 directly output as a constructor. */
6062 constructor_bit_index
= DECL_SIZE (constructor_fields
);
6063 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
6066 constructor_fields
= 0;
6068 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
6070 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6071 enum tree_code eltcode
= TREE_CODE (elttype
);
6073 /* Accept a string constant to initialize a subarray. */
6074 if (value
.value
!= 0
6075 && eltcode
== ARRAY_TYPE
6076 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
6078 value
.value
= orig_value
;
6079 /* Otherwise, if we have come to a subaggregate,
6080 and we don't have an element of its type, push into it. */
6081 else if (value
.value
!= 0 && !constructor_no_implicit
6082 && value
.value
!= error_mark_node
6083 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
6084 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
6085 || eltcode
== UNION_TYPE
))
6087 push_init_level (1);
6091 if (constructor_max_index
!= 0
6092 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
6093 || integer_all_onesp (constructor_max_index
)))
6095 pedwarn_init ("excess elements in array initializer");
6099 /* Now output the actual element. */
6102 push_array_bounds (tree_low_cst (constructor_index
, 0));
6103 output_init_element (value
.value
, strict_string
,
6104 elttype
, constructor_index
, 1);
6105 RESTORE_SPELLING_DEPTH (constructor_depth
);
6109 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
6112 /* If we are doing the bookkeeping for an element that was
6113 directly output as a constructor, we must update
6114 constructor_unfilled_index. */
6115 constructor_unfilled_index
= constructor_index
;
6117 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
6119 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
6121 /* Do a basic check of initializer size. Note that vectors
6122 always have a fixed size derived from their type. */
6123 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
6125 pedwarn_init ("excess elements in vector initializer");
6129 /* Now output the actual element. */
6131 output_init_element (value
.value
, strict_string
,
6132 elttype
, constructor_index
, 1);
6135 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
6138 /* If we are doing the bookkeeping for an element that was
6139 directly output as a constructor, we must update
6140 constructor_unfilled_index. */
6141 constructor_unfilled_index
= constructor_index
;
6144 /* Handle the sole element allowed in a braced initializer
6145 for a scalar variable. */
6146 else if (constructor_fields
== 0)
6148 pedwarn_init ("excess elements in scalar initializer");
6154 output_init_element (value
.value
, strict_string
,
6155 constructor_type
, NULL_TREE
, 1);
6156 constructor_fields
= 0;
6159 /* Handle range initializers either at this level or anywhere higher
6160 in the designator stack. */
6161 if (constructor_range_stack
)
6163 struct constructor_range_stack
*p
, *range_stack
;
6166 range_stack
= constructor_range_stack
;
6167 constructor_range_stack
= 0;
6168 while (constructor_stack
!= range_stack
->stack
)
6170 gcc_assert (constructor_stack
->implicit
);
6171 process_init_element (pop_init_level (1));
6173 for (p
= range_stack
;
6174 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
6177 gcc_assert (constructor_stack
->implicit
);
6178 process_init_element (pop_init_level (1));
6181 p
->index
= size_binop (PLUS_EXPR
, p
->index
, bitsize_one_node
);
6182 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
6187 constructor_index
= p
->index
;
6188 constructor_fields
= p
->fields
;
6189 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
6197 push_init_level (2);
6198 p
->stack
= constructor_stack
;
6199 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
6200 p
->index
= p
->range_start
;
6204 constructor_range_stack
= range_stack
;
6211 constructor_range_stack
= 0;
6214 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6215 (guaranteed to be 'volatile' or null) and ARGS (represented using
6216 an ASM_EXPR node). */
6218 build_asm_stmt (tree cv_qualifier
, tree args
)
6220 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
6221 ASM_VOLATILE_P (args
) = 1;
6222 return add_stmt (args
);
6225 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6226 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6227 SIMPLE indicates whether there was anything at all after the
6228 string in the asm expression -- asm("blah") and asm("blah" : )
6229 are subtly different. We use a ASM_EXPR node to represent this. */
6231 build_asm_expr (tree string
, tree outputs
, tree inputs
, tree clobbers
,
6237 const char *constraint
;
6238 bool allows_mem
, allows_reg
, is_inout
;
6242 ninputs
= list_length (inputs
);
6243 noutputs
= list_length (outputs
);
6245 /* Remove output conversions that change the type but not the mode. */
6246 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
6248 tree output
= TREE_VALUE (tail
);
6249 STRIP_NOPS (output
);
6250 TREE_VALUE (tail
) = output
;
6251 lvalue_or_else (output
, "invalid lvalue in asm statement");
6253 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
6255 if (!parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
6256 &allows_mem
, &allows_reg
, &is_inout
))
6258 /* By marking this operand as erroneous, we will not try
6259 to process this operand again in expand_asm_operands. */
6260 TREE_VALUE (tail
) = error_mark_node
;
6264 /* If the operand is a DECL that is going to end up in
6265 memory, assume it is addressable. This is a bit more
6266 conservative than it would ideally be; the exact test is
6267 buried deep in expand_asm_operands and depends on the
6268 DECL_RTL for the OPERAND -- which we don't have at this
6270 if (!allows_reg
&& DECL_P (output
))
6271 c_mark_addressable (output
);
6274 /* Perform default conversions on array and function inputs.
6275 Don't do this for other types as it would screw up operands
6276 expected to be in memory. */
6277 for (tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
))
6278 TREE_VALUE (tail
) = default_function_array_conversion (TREE_VALUE (tail
));
6280 args
= build_stmt (ASM_EXPR
, string
, outputs
, inputs
, clobbers
);
6282 /* Simple asm statements are treated as volatile. */
6285 ASM_VOLATILE_P (args
) = 1;
6286 ASM_INPUT_P (args
) = 1;
6291 /* Generate a goto statement to LABEL. */
6294 c_finish_goto_label (tree label
)
6296 tree decl
= lookup_label (label
);
6300 TREE_USED (decl
) = 1;
6301 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, decl
));
6304 /* Generate a computed goto statement to EXPR. */
6307 c_finish_goto_ptr (tree expr
)
6310 pedwarn ("ISO C forbids %<goto *expr;%>");
6311 expr
= convert (ptr_type_node
, expr
);
6312 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, expr
));
6315 /* Generate a C `return' statement. RETVAL is the expression for what
6316 to return, or a null pointer for `return;' with no value. */
6319 c_finish_return (tree retval
)
6321 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6323 if (TREE_THIS_VOLATILE (current_function_decl
))
6324 warning ("function declared %<noreturn%> has a %<return%> statement");
6328 current_function_returns_null
= 1;
6329 if ((warn_return_type
|| flag_isoc99
)
6330 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
6331 pedwarn_c99 ("%<return%> with no value, in "
6332 "function returning non-void");
6334 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
6336 current_function_returns_null
= 1;
6337 if (pedantic
|| TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
6338 pedwarn ("%<return%> with a value, in function returning void");
6342 tree t
= convert_for_assignment (valtype
, retval
, _("return"),
6343 NULL_TREE
, NULL_TREE
, 0);
6344 tree res
= DECL_RESULT (current_function_decl
);
6347 current_function_returns_value
= 1;
6348 if (t
== error_mark_node
)
6351 inner
= t
= convert (TREE_TYPE (res
), t
);
6353 /* Strip any conversions, additions, and subtractions, and see if
6354 we are returning the address of a local variable. Warn if so. */
6357 switch (TREE_CODE (inner
))
6359 case NOP_EXPR
: case NON_LVALUE_EXPR
: case CONVERT_EXPR
:
6361 inner
= TREE_OPERAND (inner
, 0);
6365 /* If the second operand of the MINUS_EXPR has a pointer
6366 type (or is converted from it), this may be valid, so
6367 don't give a warning. */
6369 tree op1
= TREE_OPERAND (inner
, 1);
6371 while (! POINTER_TYPE_P (TREE_TYPE (op1
))
6372 && (TREE_CODE (op1
) == NOP_EXPR
6373 || TREE_CODE (op1
) == NON_LVALUE_EXPR
6374 || TREE_CODE (op1
) == CONVERT_EXPR
))
6375 op1
= TREE_OPERAND (op1
, 0);
6377 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
6380 inner
= TREE_OPERAND (inner
, 0);
6385 inner
= TREE_OPERAND (inner
, 0);
6387 while (REFERENCE_CLASS_P (inner
)
6388 && TREE_CODE (inner
) != INDIRECT_REF
)
6389 inner
= TREE_OPERAND (inner
, 0);
6392 && ! DECL_EXTERNAL (inner
)
6393 && ! TREE_STATIC (inner
)
6394 && DECL_CONTEXT (inner
) == current_function_decl
)
6395 warning ("function returns address of local variable");
6405 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
6408 return add_stmt (build_stmt (RETURN_EXPR
, retval
));
6412 /* The SWITCH_STMT being built. */
6415 /* The original type of the testing expression, i.e. before the
6416 default conversion is applied. */
6419 /* A splay-tree mapping the low element of a case range to the high
6420 element, or NULL_TREE if there is no high element. Used to
6421 determine whether or not a new case label duplicates an old case
6422 label. We need a tree, rather than simply a hash table, because
6423 of the GNU case range extension. */
6426 /* The next node on the stack. */
6427 struct c_switch
*next
;
6430 /* A stack of the currently active switch statements. The innermost
6431 switch statement is on the top of the stack. There is no need to
6432 mark the stack for garbage collection because it is only active
6433 during the processing of the body of a function, and we never
6434 collect at that point. */
6436 struct c_switch
*c_switch_stack
;
6438 /* Start a C switch statement, testing expression EXP. Return the new
6442 c_start_case (tree exp
)
6444 enum tree_code code
;
6445 tree type
, orig_type
= error_mark_node
;
6446 struct c_switch
*cs
;
6448 if (exp
!= error_mark_node
)
6450 code
= TREE_CODE (TREE_TYPE (exp
));
6451 orig_type
= TREE_TYPE (exp
);
6453 if (! INTEGRAL_TYPE_P (orig_type
)
6454 && code
!= ERROR_MARK
)
6456 error ("switch quantity not an integer");
6457 exp
= integer_zero_node
;
6461 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
6463 if (warn_traditional
&& !in_system_header
6464 && (type
== long_integer_type_node
6465 || type
== long_unsigned_type_node
))
6466 warning ("%<long%> switch expression not converted to "
6467 "%<int%> in ISO C");
6469 exp
= default_conversion (exp
);
6470 type
= TREE_TYPE (exp
);
6474 /* Add this new SWITCH_STMT to the stack. */
6475 cs
= XNEW (struct c_switch
);
6476 cs
->switch_stmt
= build_stmt ((enum tree_code
) SWITCH_STMT
, exp
, NULL_TREE
,
6478 cs
->orig_type
= orig_type
;
6479 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
6480 cs
->next
= c_switch_stack
;
6481 c_switch_stack
= cs
;
6483 return add_stmt (cs
->switch_stmt
);
6486 /* Process a case label. */
6489 do_case (tree low_value
, tree high_value
)
6491 tree label
= NULL_TREE
;
6495 label
= c_add_case_label (c_switch_stack
->cases
,
6496 SWITCH_COND (c_switch_stack
->switch_stmt
),
6497 c_switch_stack
->orig_type
,
6498 low_value
, high_value
);
6499 if (label
== error_mark_node
)
6503 error ("case label not within a switch statement");
6505 error ("%<default%> label not within a switch statement");
6510 /* Finish the switch statement. */
6513 c_finish_case (tree body
)
6515 struct c_switch
*cs
= c_switch_stack
;
6517 SWITCH_BODY (cs
->switch_stmt
) = body
;
6519 /* Emit warnings as needed. */
6520 c_do_switch_warnings (cs
->cases
, cs
->switch_stmt
);
6522 /* Pop the stack. */
6523 c_switch_stack
= cs
->next
;
6524 splay_tree_delete (cs
->cases
);
6528 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6529 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6530 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6531 statement, and was not surrounded with parenthesis. */
6534 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
6535 tree else_block
, bool nested_if
)
6539 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6540 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
6542 tree inner_if
= then_block
;
6544 /* We know from the grammar productions that there is an IF nested
6545 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6546 it might not be exactly THEN_BLOCK, but should be the last
6547 non-container statement within. */
6549 switch (TREE_CODE (inner_if
))
6554 inner_if
= BIND_EXPR_BODY (inner_if
);
6556 case STATEMENT_LIST
:
6557 inner_if
= expr_last (then_block
);
6559 case TRY_FINALLY_EXPR
:
6560 case TRY_CATCH_EXPR
:
6561 inner_if
= TREE_OPERAND (inner_if
, 0);
6568 if (COND_EXPR_ELSE (inner_if
))
6569 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6573 /* Diagnose ";" via the special empty statement node that we create. */
6576 if (TREE_CODE (then_block
) == NOP_EXPR
&& !TREE_TYPE (then_block
))
6579 warning ("%Hempty body in an if-statement",
6580 EXPR_LOCUS (then_block
));
6581 then_block
= alloc_stmt_list ();
6584 && TREE_CODE (else_block
) == NOP_EXPR
6585 && !TREE_TYPE (else_block
))
6587 warning ("%Hempty body in an else-statement",
6588 EXPR_LOCUS (else_block
));
6589 else_block
= alloc_stmt_list ();
6593 stmt
= build3 (COND_EXPR
, NULL_TREE
, cond
, then_block
, else_block
);
6594 SET_EXPR_LOCATION (stmt
, if_locus
);
6598 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6599 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6600 is false for DO loops. INCR is the FOR increment expression. BODY is
6601 the statement controlled by the loop. BLAB is the break label. CLAB is
6602 the continue label. Everything is allowed to be NULL. */
6605 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
6606 tree blab
, tree clab
, bool cond_is_first
)
6608 tree entry
= NULL
, exit
= NULL
, t
;
6610 /* Detect do { ... } while (0) and don't generate loop construct. */
6611 if (cond
&& !cond_is_first
&& integer_zerop (cond
))
6613 if (cond_is_first
|| cond
)
6615 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
6617 /* If we have an exit condition, then we build an IF with gotos either
6618 out of the loop, or to the top of it. If there's no exit condition,
6619 then we just build a jump back to the top. */
6620 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
6624 /* Canonicalize the loop condition to the end. This means
6625 generating a branch to the loop condition. Reuse the
6626 continue label, if possible. */
6631 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
6632 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
6635 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
6636 SET_EXPR_LOCATION (t
, start_locus
);
6640 t
= build_and_jump (&blab
);
6641 exit
= build3 (COND_EXPR
, void_type_node
, cond
, exit
, t
);
6644 SET_EXPR_LOCATION (exit
, start_locus
);
6646 SET_EXPR_LOCATION (exit
, input_location
);
6655 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
6663 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
6667 c_finish_bc_stmt (tree
*label_p
, bool is_break
)
6669 tree label
= *label_p
;
6672 *label_p
= label
= create_artificial_label ();
6673 else if (TREE_CODE (label
) != LABEL_DECL
)
6676 error ("break statement not within loop or switch");
6678 error ("continue statement not within a loop");
6682 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
6685 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6688 emit_side_effect_warnings (tree expr
)
6690 if (expr
== error_mark_node
)
6692 else if (!TREE_SIDE_EFFECTS (expr
))
6694 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
6695 warning ("%Hstatement with no effect",
6696 EXPR_HAS_LOCATION (expr
) ? EXPR_LOCUS (expr
) : &input_location
);
6698 else if (warn_unused_value
)
6699 warn_if_unused_value (expr
, input_location
);
6702 /* Process an expression as if it were a complete statement. Emit
6703 diagnostics, but do not call ADD_STMT. */
6706 c_process_expr_stmt (tree expr
)
6711 /* Do default conversion if safe and possibly important,
6712 in case within ({...}). */
6713 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
6714 && (flag_isoc99
|| lvalue_p (expr
)))
6715 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
6716 expr
= default_conversion (expr
);
6718 if (warn_sequence_point
)
6719 verify_sequence_points (expr
);
6721 if (TREE_TYPE (expr
) != error_mark_node
6722 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
6723 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
6724 error ("expression statement has incomplete type");
6726 /* If we're not processing a statement expression, warn about unused values.
6727 Warnings for statement expressions will be emitted later, once we figure
6728 out which is the result. */
6729 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
6730 && (extra_warnings
|| warn_unused_value
))
6731 emit_side_effect_warnings (expr
);
6733 /* If the expression is not of a type to which we cannot assign a line
6734 number, wrap the thing in a no-op NOP_EXPR. */
6735 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
6736 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
6739 SET_EXPR_LOCATION (expr
, input_location
);
6744 /* Emit an expression as a statement. */
6747 c_finish_expr_stmt (tree expr
)
6750 return add_stmt (c_process_expr_stmt (expr
));
6755 /* Do the opposite and emit a statement as an expression. To begin,
6756 create a new binding level and return it. */
6759 c_begin_stmt_expr (void)
6763 /* We must force a BLOCK for this level so that, if it is not expanded
6764 later, there is a way to turn off the entire subtree of blocks that
6765 are contained in it. */
6767 ret
= c_begin_compound_stmt (true);
6769 /* Mark the current statement list as belonging to a statement list. */
6770 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
6776 c_finish_stmt_expr (tree body
)
6778 tree last
, type
, tmp
, val
;
6781 body
= c_end_compound_stmt (body
, true);
6783 /* Locate the last statement in BODY. See c_end_compound_stmt
6784 about always returning a BIND_EXPR. */
6785 last_p
= &BIND_EXPR_BODY (body
);
6786 last
= BIND_EXPR_BODY (body
);
6789 if (TREE_CODE (last
) == STATEMENT_LIST
)
6791 tree_stmt_iterator i
;
6793 /* This can happen with degenerate cases like ({ }). No value. */
6794 if (!TREE_SIDE_EFFECTS (last
))
6797 /* If we're supposed to generate side effects warnings, process
6798 all of the statements except the last. */
6799 if (extra_warnings
|| warn_unused_value
)
6801 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
6802 emit_side_effect_warnings (tsi_stmt (i
));
6805 i
= tsi_last (last
);
6806 last_p
= tsi_stmt_ptr (i
);
6810 /* If the end of the list is exception related, then the list was split
6811 by a call to push_cleanup. Continue searching. */
6812 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
6813 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
6815 last_p
= &TREE_OPERAND (last
, 0);
6817 goto continue_searching
;
6820 /* In the case that the BIND_EXPR is not necessary, return the
6821 expression out from inside it. */
6822 if (last
== error_mark_node
6823 || (last
== BIND_EXPR_BODY (body
)
6824 && BIND_EXPR_VARS (body
) == NULL
))
6827 /* Extract the type of said expression. */
6828 type
= TREE_TYPE (last
);
6830 /* If we're not returning a value at all, then the BIND_EXPR that
6831 we already have is a fine expression to return. */
6832 if (!type
|| VOID_TYPE_P (type
))
6835 /* Now that we've located the expression containing the value, it seems
6836 silly to make voidify_wrapper_expr repeat the process. Create a
6837 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6838 tmp
= create_tmp_var_raw (type
, NULL
);
6840 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6841 tree_expr_nonnegative_p giving up immediately. */
6843 if (TREE_CODE (val
) == NOP_EXPR
6844 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
6845 val
= TREE_OPERAND (val
, 0);
6847 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
6848 SET_EXPR_LOCUS (*last_p
, EXPR_LOCUS (last
));
6850 return build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
6853 /* Begin and end compound statements. This is as simple as pushing
6854 and popping new statement lists from the tree. */
6857 c_begin_compound_stmt (bool do_scope
)
6859 tree stmt
= push_stmt_list ();
6866 c_end_compound_stmt (tree stmt
, bool do_scope
)
6872 if (c_dialect_objc ())
6873 objc_clear_super_receiver ();
6874 block
= pop_scope ();
6877 stmt
= pop_stmt_list (stmt
);
6878 stmt
= c_build_bind_expr (block
, stmt
);
6880 /* If this compound statement is nested immediately inside a statement
6881 expression, then force a BIND_EXPR to be created. Otherwise we'll
6882 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6883 STATEMENT_LISTs merge, and thus we can lose track of what statement
6886 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
6887 && TREE_CODE (stmt
) != BIND_EXPR
)
6889 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
6890 TREE_SIDE_EFFECTS (stmt
) = 1;
6896 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6897 when the current scope is exited. EH_ONLY is true when this is not
6898 meant to apply to normal control flow transfer. */
6901 push_cleanup (tree
ARG_UNUSED (decl
), tree cleanup
, bool eh_only
)
6903 enum tree_code code
;
6907 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
6908 stmt
= build_stmt (code
, NULL
, cleanup
);
6910 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
6911 list
= push_stmt_list ();
6912 TREE_OPERAND (stmt
, 0) = list
;
6913 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
6916 /* Build a binary-operation expression without default conversions.
6917 CODE is the kind of expression to build.
6918 This function differs from `build' in several ways:
6919 the data type of the result is computed and recorded in it,
6920 warnings are generated if arg data types are invalid,
6921 special handling for addition and subtraction of pointers is known,
6922 and some optimization is done (operations on narrow ints
6923 are done in the narrower type when that gives the same result).
6924 Constant folding is also done before the result is returned.
6926 Note that the operands will never have enumeral types, or function
6927 or array types, because either they will have the default conversions
6928 performed or they have both just been converted to some other type in which
6929 the arithmetic is to be done. */
6932 build_binary_op (enum tree_code code
, tree orig_op0
, tree orig_op1
,
6936 enum tree_code code0
, code1
;
6939 /* Expression code to give to the expression when it is built.
6940 Normally this is CODE, which is what the caller asked for,
6941 but in some special cases we change it. */
6942 enum tree_code resultcode
= code
;
6944 /* Data type in which the computation is to be performed.
6945 In the simplest cases this is the common type of the arguments. */
6946 tree result_type
= NULL
;
6948 /* Nonzero means operands have already been type-converted
6949 in whatever way is necessary.
6950 Zero means they need to be converted to RESULT_TYPE. */
6953 /* Nonzero means create the expression with this type, rather than
6955 tree build_type
= 0;
6957 /* Nonzero means after finally constructing the expression
6958 convert it to this type. */
6959 tree final_type
= 0;
6961 /* Nonzero if this is an operation like MIN or MAX which can
6962 safely be computed in short if both args are promoted shorts.
6963 Also implies COMMON.
6964 -1 indicates a bitwise operation; this makes a difference
6965 in the exact conditions for when it is safe to do the operation
6966 in a narrower mode. */
6969 /* Nonzero if this is a comparison operation;
6970 if both args are promoted shorts, compare the original shorts.
6971 Also implies COMMON. */
6972 int short_compare
= 0;
6974 /* Nonzero if this is a right-shift operation, which can be computed on the
6975 original short and then promoted if the operand is a promoted short. */
6976 int short_shift
= 0;
6978 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6983 op0
= default_conversion (orig_op0
);
6984 op1
= default_conversion (orig_op1
);
6992 type0
= TREE_TYPE (op0
);
6993 type1
= TREE_TYPE (op1
);
6995 /* The expression codes of the data types of the arguments tell us
6996 whether the arguments are integers, floating, pointers, etc. */
6997 code0
= TREE_CODE (type0
);
6998 code1
= TREE_CODE (type1
);
7000 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7001 STRIP_TYPE_NOPS (op0
);
7002 STRIP_TYPE_NOPS (op1
);
7004 /* If an error was already reported for one of the arguments,
7005 avoid reporting another error. */
7007 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
7008 return error_mark_node
;
7013 /* Handle the pointer + int case. */
7014 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7015 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
7016 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
7017 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
7023 /* Subtraction of two similar pointers.
7024 We must subtract them as integers, then divide by object size. */
7025 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
7026 && comp_target_types (type0
, type1
, 1))
7027 return pointer_diff (op0
, op1
);
7028 /* Handle pointer minus int. Just like pointer plus int. */
7029 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7030 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
7039 case TRUNC_DIV_EXPR
:
7041 case FLOOR_DIV_EXPR
:
7042 case ROUND_DIV_EXPR
:
7043 case EXACT_DIV_EXPR
:
7044 /* Floating point division by zero is a legitimate way to obtain
7045 infinities and NaNs. */
7046 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
7047 warning ("division by zero");
7049 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
7050 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
7051 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
7052 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
7054 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
7055 code0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
7056 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
7057 code1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
7059 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
7060 resultcode
= RDIV_EXPR
;
7062 /* Although it would be tempting to shorten always here, that
7063 loses on some targets, since the modulo instruction is
7064 undefined if the quotient can't be represented in the
7065 computation mode. We shorten only if unsigned or if
7066 dividing by something we know != -1. */
7067 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
7068 || (TREE_CODE (op1
) == INTEGER_CST
7069 && ! integer_all_onesp (op1
)));
7077 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7079 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
7083 case TRUNC_MOD_EXPR
:
7084 case FLOOR_MOD_EXPR
:
7085 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
7086 warning ("division by zero");
7088 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7090 /* Although it would be tempting to shorten always here, that loses
7091 on some targets, since the modulo instruction is undefined if the
7092 quotient can't be represented in the computation mode. We shorten
7093 only if unsigned or if dividing by something we know != -1. */
7094 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
7095 || (TREE_CODE (op1
) == INTEGER_CST
7096 && ! integer_all_onesp (op1
)));
7101 case TRUTH_ANDIF_EXPR
:
7102 case TRUTH_ORIF_EXPR
:
7103 case TRUTH_AND_EXPR
:
7105 case TRUTH_XOR_EXPR
:
7106 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
7107 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
7108 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
7109 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
7111 /* Result of these operations is always an int,
7112 but that does not mean the operands should be
7113 converted to ints! */
7114 result_type
= integer_type_node
;
7115 op0
= lang_hooks
.truthvalue_conversion (op0
);
7116 op1
= lang_hooks
.truthvalue_conversion (op1
);
7121 /* Shift operations: result has same type as first operand;
7122 always convert second operand to int.
7123 Also set SHORT_SHIFT if shifting rightward. */
7126 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7128 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
7130 if (tree_int_cst_sgn (op1
) < 0)
7131 warning ("right shift count is negative");
7134 if (! integer_zerop (op1
))
7137 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
7138 warning ("right shift count >= width of type");
7142 /* Use the type of the value to be shifted. */
7143 result_type
= type0
;
7144 /* Convert the shift-count to an integer, regardless of size
7145 of value being shifted. */
7146 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
7147 op1
= convert (integer_type_node
, op1
);
7148 /* Avoid converting op1 to result_type later. */
7154 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7156 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
7158 if (tree_int_cst_sgn (op1
) < 0)
7159 warning ("left shift count is negative");
7161 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
7162 warning ("left shift count >= width of type");
7165 /* Use the type of the value to be shifted. */
7166 result_type
= type0
;
7167 /* Convert the shift-count to an integer, regardless of size
7168 of value being shifted. */
7169 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
7170 op1
= convert (integer_type_node
, op1
);
7171 /* Avoid converting op1 to result_type later. */
7178 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
7180 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
7182 if (tree_int_cst_sgn (op1
) < 0)
7183 warning ("shift count is negative");
7184 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
7185 warning ("shift count >= width of type");
7188 /* Use the type of the value to be shifted. */
7189 result_type
= type0
;
7190 /* Convert the shift-count to an integer, regardless of size
7191 of value being shifted. */
7192 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
7193 op1
= convert (integer_type_node
, op1
);
7194 /* Avoid converting op1 to result_type later. */
7201 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
7202 warning ("comparing floating point with == or != is unsafe");
7203 /* Result of comparison is always int,
7204 but don't convert the args to int! */
7205 build_type
= integer_type_node
;
7206 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
7207 || code0
== COMPLEX_TYPE
)
7208 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
7209 || code1
== COMPLEX_TYPE
))
7211 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
7213 tree tt0
= TREE_TYPE (type0
);
7214 tree tt1
= TREE_TYPE (type1
);
7215 /* Anything compares with void *. void * compares with anything.
7216 Otherwise, the targets must be compatible
7217 and both must be object or both incomplete. */
7218 if (comp_target_types (type0
, type1
, 1))
7219 result_type
= common_pointer_type (type0
, type1
);
7220 else if (VOID_TYPE_P (tt0
))
7222 /* op0 != orig_op0 detects the case of something
7223 whose value is 0 but which isn't a valid null ptr const. */
7224 if (pedantic
&& (!integer_zerop (op0
) || op0
!= orig_op0
)
7225 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
7226 pedwarn ("ISO C forbids comparison of %<void *%>"
7227 " with function pointer");
7229 else if (VOID_TYPE_P (tt1
))
7231 if (pedantic
&& (!integer_zerop (op1
) || op1
!= orig_op1
)
7232 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
7233 pedwarn ("ISO C forbids comparison of %<void *%>"
7234 " with function pointer");
7237 pedwarn ("comparison of distinct pointer types lacks a cast");
7239 if (result_type
== NULL_TREE
)
7240 result_type
= ptr_type_node
;
7242 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
7243 && integer_zerop (op1
))
7244 result_type
= type0
;
7245 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
7246 && integer_zerop (op0
))
7247 result_type
= type1
;
7248 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7250 result_type
= type0
;
7251 pedwarn ("comparison between pointer and integer");
7253 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
7255 result_type
= type1
;
7256 pedwarn ("comparison between pointer and integer");
7262 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
7263 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
7265 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
7267 if (comp_target_types (type0
, type1
, 1))
7269 result_type
= common_pointer_type (type0
, type1
);
7271 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
7272 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7276 result_type
= ptr_type_node
;
7277 pedwarn ("comparison of distinct pointer types lacks a cast");
7286 build_type
= integer_type_node
;
7287 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
7288 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
7290 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
7292 if (comp_target_types (type0
, type1
, 1))
7294 result_type
= common_pointer_type (type0
, type1
);
7295 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
7296 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
7297 pedwarn ("comparison of complete and incomplete pointers");
7299 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
7300 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7304 result_type
= ptr_type_node
;
7305 pedwarn ("comparison of distinct pointer types lacks a cast");
7308 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
7309 && integer_zerop (op1
))
7311 result_type
= type0
;
7312 if (pedantic
|| extra_warnings
)
7313 pedwarn ("ordered comparison of pointer with integer zero");
7315 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
7316 && integer_zerop (op0
))
7318 result_type
= type1
;
7320 pedwarn ("ordered comparison of pointer with integer zero");
7322 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
7324 result_type
= type0
;
7325 pedwarn ("comparison between pointer and integer");
7327 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
7329 result_type
= type1
;
7330 pedwarn ("comparison between pointer and integer");
7334 case UNORDERED_EXPR
:
7342 build_type
= integer_type_node
;
7343 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
7345 error ("unordered comparison on non-floating point argument");
7346 return error_mark_node
;
7355 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
7356 return error_mark_node
;
7358 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
7359 || code0
== VECTOR_TYPE
)
7361 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
7362 || code1
== VECTOR_TYPE
))
7364 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
7366 if (shorten
|| common
|| short_compare
)
7367 result_type
= common_type (type0
, type1
);
7369 /* For certain operations (which identify themselves by shorten != 0)
7370 if both args were extended from the same smaller type,
7371 do the arithmetic in that type and then extend.
7373 shorten !=0 and !=1 indicates a bitwise operation.
7374 For them, this optimization is safe only if
7375 both args are zero-extended or both are sign-extended.
7376 Otherwise, we might change the result.
7377 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7378 but calculated in (unsigned short) it would be (unsigned short)-1. */
7380 if (shorten
&& none_complex
)
7382 int unsigned0
, unsigned1
;
7383 tree arg0
= get_narrower (op0
, &unsigned0
);
7384 tree arg1
= get_narrower (op1
, &unsigned1
);
7385 /* UNS is 1 if the operation to be done is an unsigned one. */
7386 int uns
= TYPE_UNSIGNED (result_type
);
7389 final_type
= result_type
;
7391 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7392 but it *requires* conversion to FINAL_TYPE. */
7394 if ((TYPE_PRECISION (TREE_TYPE (op0
))
7395 == TYPE_PRECISION (TREE_TYPE (arg0
)))
7396 && TREE_TYPE (op0
) != final_type
)
7397 unsigned0
= TYPE_UNSIGNED (TREE_TYPE (op0
));
7398 if ((TYPE_PRECISION (TREE_TYPE (op1
))
7399 == TYPE_PRECISION (TREE_TYPE (arg1
)))
7400 && TREE_TYPE (op1
) != final_type
)
7401 unsigned1
= TYPE_UNSIGNED (TREE_TYPE (op1
));
7403 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7405 /* For bitwise operations, signedness of nominal type
7406 does not matter. Consider only how operands were extended. */
7410 /* Note that in all three cases below we refrain from optimizing
7411 an unsigned operation on sign-extended args.
7412 That would not be valid. */
7414 /* Both args variable: if both extended in same way
7415 from same width, do it in that width.
7416 Do it unsigned if args were zero-extended. */
7417 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
7418 < TYPE_PRECISION (result_type
))
7419 && (TYPE_PRECISION (TREE_TYPE (arg1
))
7420 == TYPE_PRECISION (TREE_TYPE (arg0
)))
7421 && unsigned0
== unsigned1
7422 && (unsigned0
|| !uns
))
7424 = c_common_signed_or_unsigned_type
7425 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
7426 else if (TREE_CODE (arg0
) == INTEGER_CST
7427 && (unsigned1
|| !uns
)
7428 && (TYPE_PRECISION (TREE_TYPE (arg1
))
7429 < TYPE_PRECISION (result_type
))
7431 = c_common_signed_or_unsigned_type (unsigned1
,
7433 int_fits_type_p (arg0
, type
)))
7435 else if (TREE_CODE (arg1
) == INTEGER_CST
7436 && (unsigned0
|| !uns
)
7437 && (TYPE_PRECISION (TREE_TYPE (arg0
))
7438 < TYPE_PRECISION (result_type
))
7440 = c_common_signed_or_unsigned_type (unsigned0
,
7442 int_fits_type_p (arg1
, type
)))
7446 /* Shifts can be shortened if shifting right. */
7451 tree arg0
= get_narrower (op0
, &unsigned_arg
);
7453 final_type
= result_type
;
7455 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
7456 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
7458 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
7459 /* We can shorten only if the shift count is less than the
7460 number of bits in the smaller type size. */
7461 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
7462 /* We cannot drop an unsigned shift after sign-extension. */
7463 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
7465 /* Do an unsigned shift if the operand was zero-extended. */
7467 = c_common_signed_or_unsigned_type (unsigned_arg
,
7469 /* Convert value-to-be-shifted to that type. */
7470 if (TREE_TYPE (op0
) != result_type
)
7471 op0
= convert (result_type
, op0
);
7476 /* Comparison operations are shortened too but differently.
7477 They identify themselves by setting short_compare = 1. */
7481 /* Don't write &op0, etc., because that would prevent op0
7482 from being kept in a register.
7483 Instead, make copies of the our local variables and
7484 pass the copies by reference, then copy them back afterward. */
7485 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
7486 enum tree_code xresultcode
= resultcode
;
7488 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
7493 op0
= xop0
, op1
= xop1
;
7495 resultcode
= xresultcode
;
7497 if (warn_sign_compare
&& skip_evaluation
== 0)
7499 int op0_signed
= ! TYPE_UNSIGNED (TREE_TYPE (orig_op0
));
7500 int op1_signed
= ! TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
7501 int unsignedp0
, unsignedp1
;
7502 tree primop0
= get_narrower (op0
, &unsignedp0
);
7503 tree primop1
= get_narrower (op1
, &unsignedp1
);
7507 STRIP_TYPE_NOPS (xop0
);
7508 STRIP_TYPE_NOPS (xop1
);
7510 /* Give warnings for comparisons between signed and unsigned
7511 quantities that may fail.
7513 Do the checking based on the original operand trees, so that
7514 casts will be considered, but default promotions won't be.
7516 Do not warn if the comparison is being done in a signed type,
7517 since the signed type will only be chosen if it can represent
7518 all the values of the unsigned type. */
7519 if (! TYPE_UNSIGNED (result_type
))
7521 /* Do not warn if both operands are the same signedness. */
7522 else if (op0_signed
== op1_signed
)
7529 sop
= xop0
, uop
= xop1
;
7531 sop
= xop1
, uop
= xop0
;
7533 /* Do not warn if the signed quantity is an
7534 unsuffixed integer literal (or some static
7535 constant expression involving such literals or a
7536 conditional expression involving such literals)
7537 and it is non-negative. */
7538 if (tree_expr_nonnegative_p (sop
))
7540 /* Do not warn if the comparison is an equality operation,
7541 the unsigned quantity is an integral constant, and it
7542 would fit in the result if the result were signed. */
7543 else if (TREE_CODE (uop
) == INTEGER_CST
7544 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
7546 (uop
, c_common_signed_type (result_type
)))
7548 /* Do not warn if the unsigned quantity is an enumeration
7549 constant and its maximum value would fit in the result
7550 if the result were signed. */
7551 else if (TREE_CODE (uop
) == INTEGER_CST
7552 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
7554 (TYPE_MAX_VALUE (TREE_TYPE(uop
)),
7555 c_common_signed_type (result_type
)))
7558 warning ("comparison between signed and unsigned");
7561 /* Warn if two unsigned values are being compared in a size
7562 larger than their original size, and one (and only one) is the
7563 result of a `~' operator. This comparison will always fail.
7565 Also warn if one operand is a constant, and the constant
7566 does not have all bits set that are set in the ~ operand
7567 when it is extended. */
7569 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7570 != (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
7572 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7573 primop0
= get_narrower (TREE_OPERAND (primop0
, 0),
7576 primop1
= get_narrower (TREE_OPERAND (primop1
, 0),
7579 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
7582 HOST_WIDE_INT constant
, mask
;
7583 int unsignedp
, bits
;
7585 if (host_integerp (primop0
, 0))
7588 unsignedp
= unsignedp1
;
7589 constant
= tree_low_cst (primop0
, 0);
7594 unsignedp
= unsignedp0
;
7595 constant
= tree_low_cst (primop1
, 0);
7598 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
7599 if (bits
< TYPE_PRECISION (result_type
)
7600 && bits
< HOST_BITS_PER_WIDE_INT
&& unsignedp
)
7602 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
7603 if ((mask
& constant
) != mask
)
7604 warning ("comparison of promoted ~unsigned with constant");
7607 else if (unsignedp0
&& unsignedp1
7608 && (TYPE_PRECISION (TREE_TYPE (primop0
))
7609 < TYPE_PRECISION (result_type
))
7610 && (TYPE_PRECISION (TREE_TYPE (primop1
))
7611 < TYPE_PRECISION (result_type
)))
7612 warning ("comparison of promoted ~unsigned with unsigned");
7618 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7619 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7620 Then the expression will be built.
7621 It will be given type FINAL_TYPE if that is nonzero;
7622 otherwise, it will be given type RESULT_TYPE. */
7626 binary_op_error (code
);
7627 return error_mark_node
;
7632 if (TREE_TYPE (op0
) != result_type
)
7633 op0
= convert (result_type
, op0
);
7634 if (TREE_TYPE (op1
) != result_type
)
7635 op1
= convert (result_type
, op1
);
7637 /* This can happen if one operand has a vector type, and the other
7638 has a different type. */
7639 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
7640 return error_mark_node
;
7643 if (build_type
== NULL_TREE
)
7644 build_type
= result_type
;
7647 tree result
= build2 (resultcode
, build_type
, op0
, op1
);
7649 /* Treat expressions in initializers specially as they can't trap. */
7650 result
= require_constant_value
? fold_initializer (result
)
7653 if (final_type
!= 0)
7654 result
= convert (final_type
, result
);