1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
38 #include "langhooks.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned
;
53 static tree
qualify_type (tree
, tree
);
54 static int same_translation_unit_p (tree
, tree
);
55 static int tagged_types_tu_compatible_p (tree
, tree
, int);
56 static int comp_target_types (tree
, tree
, int);
57 static int function_types_compatible_p (tree
, tree
, int);
58 static int type_lists_compatible_p (tree
, tree
, int);
59 static tree
decl_constant_value_for_broken_optimization (tree
);
60 static tree
default_function_array_conversion (tree
);
61 static tree
lookup_field (tree
, tree
);
62 static tree
convert_arguments (tree
, tree
, tree
, tree
);
63 static tree
pointer_diff (tree
, tree
);
64 static tree
internal_build_compound_expr (tree
, int);
65 static tree
convert_for_assignment (tree
, tree
, const char *, tree
, tree
,
67 static void warn_for_assignment (const char *, const char *, tree
, int);
68 static tree
valid_compound_expr_initializer (tree
, tree
);
69 static void push_string (const char *);
70 static void push_member_name (tree
);
71 static void push_array_bounds (int);
72 static int spelling_length (void);
73 static char *print_spelling (char *);
74 static void warning_init (const char *);
75 static tree
digest_init (tree
, tree
, int);
76 static void output_init_element (tree
, tree
, tree
, int);
77 static void output_pending_init_elements (int);
78 static int set_designator (int);
79 static void push_range_stack (tree
);
80 static void add_pending_init (tree
, tree
);
81 static void set_nonincremental_init (void);
82 static void set_nonincremental_init_from_string (tree
);
83 static tree
find_init_member (tree
);
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.) */
89 require_complete_type (tree value
)
91 tree type
= TREE_TYPE (value
);
93 if (value
== error_mark_node
|| type
== error_mark_node
)
94 return error_mark_node
;
96 /* First, detect a valid value with a complete type. */
97 if (COMPLETE_TYPE_P (type
))
100 c_incomplete_type_error (value
, type
);
101 return error_mark_node
;
104 /* Print an error message for invalid use of an incomplete type.
105 VALUE is the expression that was used (or 0 if that isn't known)
106 and TYPE is the type that was invalid. */
109 c_incomplete_type_error (tree value
, tree type
)
111 const char *type_code_string
;
113 /* Avoid duplicate error message. */
114 if (TREE_CODE (type
) == ERROR_MARK
)
117 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
118 || TREE_CODE (value
) == PARM_DECL
))
119 error ("`%s' has an incomplete type",
120 IDENTIFIER_POINTER (DECL_NAME (value
)));
124 /* We must print an error message. Be clever about what it says. */
126 switch (TREE_CODE (type
))
129 type_code_string
= "struct";
133 type_code_string
= "union";
137 type_code_string
= "enum";
141 error ("invalid use of void expression");
145 if (TYPE_DOMAIN (type
))
147 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
149 error ("invalid use of flexible array member");
152 type
= TREE_TYPE (type
);
155 error ("invalid use of array with unspecified bounds");
162 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
163 error ("invalid use of undefined type `%s %s'",
164 type_code_string
, IDENTIFIER_POINTER (TYPE_NAME (type
)));
166 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
167 error ("invalid use of incomplete typedef `%s'",
168 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
))));
172 /* Given a type, apply default promotions wrt unnamed function
173 arguments and return the new type. */
176 c_type_promotes_to (tree type
)
178 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
179 return double_type_node
;
181 if (c_promoting_integer_type_p (type
))
183 /* Preserve unsignedness if not really getting any wider. */
184 if (TREE_UNSIGNED (type
)
185 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
186 return unsigned_type_node
;
187 return integer_type_node
;
193 /* Return a variant of TYPE which has all the type qualifiers of LIKE
194 as well as those of TYPE. */
197 qualify_type (tree type
, tree like
)
199 return c_build_qualified_type (type
,
200 TYPE_QUALS (type
) | TYPE_QUALS (like
));
203 /* Return the common type of two types.
204 We assume that comptypes has already been done and returned 1;
205 if that isn't so, this may crash. In particular, we assume that qualifiers
208 This is the type for the result of most arithmetic operations
209 if the operands have the given two types. */
212 common_type (tree t1
, tree t2
)
214 enum tree_code code1
;
215 enum tree_code code2
;
218 /* Save time if the two types are the same. */
220 if (t1
== t2
) return t1
;
222 /* If one type is nonsense, use the other. */
223 if (t1
== error_mark_node
)
225 if (t2
== error_mark_node
)
228 /* Merge the attributes. */
229 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
231 /* Treat an enum type as the unsigned integer type of the same width. */
233 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
234 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
235 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
236 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
238 code1
= TREE_CODE (t1
);
239 code2
= TREE_CODE (t2
);
241 /* If one type is complex, form the common type of the non-complex
242 components, then make that complex. Use T1 or T2 if it is the
244 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
246 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
247 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
248 tree subtype
= common_type (subtype1
, subtype2
);
250 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
251 return build_type_attribute_variant (t1
, attributes
);
252 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
253 return build_type_attribute_variant (t2
, attributes
);
255 return build_type_attribute_variant (build_complex_type (subtype
),
263 /* If only one is real, use it as the result. */
265 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
266 return build_type_attribute_variant (t1
, attributes
);
268 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
269 return build_type_attribute_variant (t2
, attributes
);
271 /* Both real or both integers; use the one with greater precision. */
273 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
274 return build_type_attribute_variant (t1
, attributes
);
275 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
276 return build_type_attribute_variant (t2
, attributes
);
278 /* Same precision. Prefer longs to ints even when same size. */
280 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
281 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
282 return build_type_attribute_variant (long_unsigned_type_node
,
285 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
286 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
288 /* But preserve unsignedness from the other type,
289 since long cannot hold all the values of an unsigned int. */
290 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
291 t1
= long_unsigned_type_node
;
293 t1
= long_integer_type_node
;
294 return build_type_attribute_variant (t1
, attributes
);
297 /* Likewise, prefer long double to double even if same size. */
298 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
299 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
300 return build_type_attribute_variant (long_double_type_node
,
303 /* Otherwise prefer the unsigned one. */
305 if (TREE_UNSIGNED (t1
))
306 return build_type_attribute_variant (t1
, attributes
);
308 return build_type_attribute_variant (t2
, attributes
);
311 /* For two pointers, do this recursively on the target type,
312 and combine the qualifiers of the two types' targets. */
313 /* This code was turned off; I don't know why.
314 But ANSI C specifies doing this with the qualifiers.
315 So I turned it on again. */
317 tree pointed_to_1
= TREE_TYPE (t1
);
318 tree pointed_to_2
= TREE_TYPE (t2
);
319 tree target
= common_type (TYPE_MAIN_VARIANT (pointed_to_1
),
320 TYPE_MAIN_VARIANT (pointed_to_2
));
321 t1
= build_pointer_type (c_build_qualified_type
323 TYPE_QUALS (pointed_to_1
) |
324 TYPE_QUALS (pointed_to_2
)));
325 return build_type_attribute_variant (t1
, attributes
);
330 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
331 /* Save space: see if the result is identical to one of the args. */
332 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
333 return build_type_attribute_variant (t1
, attributes
);
334 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
335 return build_type_attribute_variant (t2
, attributes
);
336 /* Merge the element types, and have a size if either arg has one. */
337 t1
= build_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
338 return build_type_attribute_variant (t1
, attributes
);
342 /* Function types: prefer the one that specified arg types.
343 If both do, merge the arg types. Also merge the return types. */
345 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
346 tree p1
= TYPE_ARG_TYPES (t1
);
347 tree p2
= TYPE_ARG_TYPES (t2
);
352 /* Save space: see if the result is identical to one of the args. */
353 if (valtype
== TREE_TYPE (t1
) && ! TYPE_ARG_TYPES (t2
))
354 return build_type_attribute_variant (t1
, attributes
);
355 if (valtype
== TREE_TYPE (t2
) && ! TYPE_ARG_TYPES (t1
))
356 return build_type_attribute_variant (t2
, attributes
);
358 /* Simple way if one arg fails to specify argument types. */
359 if (TYPE_ARG_TYPES (t1
) == 0)
361 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
362 return build_type_attribute_variant (t1
, attributes
);
364 if (TYPE_ARG_TYPES (t2
) == 0)
366 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
367 return build_type_attribute_variant (t1
, attributes
);
370 /* If both args specify argument types, we must merge the two
371 lists, argument by argument. */
374 declare_parm_level ();
376 len
= list_length (p1
);
379 for (i
= 0; i
< len
; i
++)
380 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
385 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
387 /* A null type means arg type is not specified.
388 Take whatever the other function type has. */
389 if (TREE_VALUE (p1
) == 0)
391 TREE_VALUE (n
) = TREE_VALUE (p2
);
394 if (TREE_VALUE (p2
) == 0)
396 TREE_VALUE (n
) = TREE_VALUE (p1
);
400 /* Given wait (union {union wait *u; int *i} *)
401 and wait (union wait *),
402 prefer union wait * as type of parm. */
403 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
404 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
407 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
408 memb
; memb
= TREE_CHAIN (memb
))
409 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p2
),
412 TREE_VALUE (n
) = TREE_VALUE (p2
);
414 pedwarn ("function types not truly compatible in ISO C");
418 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
419 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
422 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
423 memb
; memb
= TREE_CHAIN (memb
))
424 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p1
),
427 TREE_VALUE (n
) = TREE_VALUE (p1
);
429 pedwarn ("function types not truly compatible in ISO C");
433 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
439 t1
= build_function_type (valtype
, newargs
);
440 /* ... falls through ... */
444 return build_type_attribute_variant (t1
, attributes
);
449 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
450 or various other operations. Return 2 if they are compatible
451 but a warning may be needed if you use them together. */
454 comptypes (tree type1
, tree type2
, int flags
)
460 /* Suppress errors caused by previously reported errors. */
462 if (t1
== t2
|| !t1
|| !t2
463 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
466 /* If either type is the internal version of sizetype, return the
468 if (TREE_CODE (t1
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t1
)
469 && TYPE_DOMAIN (t1
) != 0)
470 t1
= TYPE_DOMAIN (t1
);
472 if (TREE_CODE (t2
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (t2
)
473 && TYPE_DOMAIN (t2
) != 0)
474 t2
= TYPE_DOMAIN (t2
);
476 /* Enumerated types are compatible with integer types, but this is
477 not transitive: two enumerated types in the same translation unit
478 are compatible with each other only if they are the same type. */
480 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
481 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TREE_UNSIGNED (t1
));
482 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
483 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TREE_UNSIGNED (t2
));
488 /* Different classes of types can't be compatible. */
490 if (TREE_CODE (t1
) != TREE_CODE (t2
)) return 0;
492 /* Qualifiers must match. */
494 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
497 /* Allow for two different type nodes which have essentially the same
498 definition. Note that we already checked for equality of the type
499 qualifiers (just above). */
501 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
504 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
505 if (! (attrval
= (*targetm
.comp_type_attributes
) (t1
, t2
)))
508 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
511 switch (TREE_CODE (t1
))
514 /* We must give ObjC the first crack at comparing pointers, since
515 protocol qualifiers may be involved. */
516 if (c_dialect_objc () && (val
= objc_comptypes (t1
, t2
, 0)) >= 0)
518 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
519 ? 1 : comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
), flags
));
523 val
= function_types_compatible_p (t1
, t2
, flags
);
528 tree d1
= TYPE_DOMAIN (t1
);
529 tree d2
= TYPE_DOMAIN (t2
);
530 bool d1_variable
, d2_variable
;
531 bool d1_zero
, d2_zero
;
534 /* Target types must match incl. qualifiers. */
535 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
536 && 0 == (val
= comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
),
540 /* Sizes must match unless one is missing or variable. */
541 if (d1
== 0 || d2
== 0 || d1
== d2
)
544 d1_zero
= ! TYPE_MAX_VALUE (d1
);
545 d2_zero
= ! TYPE_MAX_VALUE (d2
);
547 d1_variable
= (! d1_zero
548 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
549 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
550 d2_variable
= (! d2_zero
551 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
552 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
554 if (d1_variable
|| d2_variable
)
556 if (d1_zero
&& d2_zero
)
558 if (d1_zero
|| d2_zero
559 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
560 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
567 /* We are dealing with two distinct structs. In assorted Objective-C
568 corner cases, however, these can still be deemed equivalent. */
569 if (c_dialect_objc () && objc_comptypes (t1
, t2
, 0) == 1)
574 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
575 val
= tagged_types_tu_compatible_p (t1
, t2
, flags
);
579 /* The target might allow certain vector types to be compatible. */
580 val
= (*targetm
.vector_opaque_p
) (t1
)
581 || (*targetm
.vector_opaque_p
) (t2
);
587 return attrval
== 2 && val
== 1 ? 2 : val
;
590 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
591 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
592 to 1 or 0 depending if the check of the pointer types is meant to
593 be reflexive or not (typically, assignments are not reflexive,
594 while comparisons are reflexive).
598 comp_target_types (tree ttl
, tree ttr
, int reflexive
)
602 /* Give objc_comptypes a crack at letting these types through. */
603 if ((val
= objc_comptypes (ttl
, ttr
, reflexive
)) >= 0)
606 val
= comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl
)),
607 TYPE_MAIN_VARIANT (TREE_TYPE (ttr
)), COMPARE_STRICT
);
609 if (val
== 2 && pedantic
)
610 pedwarn ("types are not quite compatible");
614 /* Subroutines of `comptypes'. */
616 /* Determine whether two types derive from the same translation unit.
617 If the CONTEXT chain ends in a null, that type's context is still
618 being parsed, so if two types have context chains ending in null,
619 they're in the same translation unit. */
621 same_translation_unit_p (tree t1
, tree t2
)
623 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
624 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
626 case 'd': t1
= DECL_CONTEXT (t1
); break;
627 case 't': t1
= TYPE_CONTEXT (t1
); break;
628 case 'b': t1
= BLOCK_SUPERCONTEXT (t1
); break;
632 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
633 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
635 case 'd': t2
= DECL_CONTEXT (t1
); break;
636 case 't': t2
= TYPE_CONTEXT (t2
); break;
637 case 'b': t2
= BLOCK_SUPERCONTEXT (t2
); break;
644 /* The C standard says that two structures in different translation
645 units are compatible with each other only if the types of their
646 fields are compatible (among other things). So, consider two copies
647 of this structure: */
649 struct tagged_tu_seen
{
650 const struct tagged_tu_seen
* next
;
655 /* Can they be compatible with each other? We choose to break the
656 recursion by allowing those types to be compatible. */
658 static const struct tagged_tu_seen
* tagged_tu_seen_base
;
660 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
661 compatible. If the two types are not the same (which has been
662 checked earlier), this can only happen when multiple translation
663 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
667 tagged_types_tu_compatible_p (tree t1
, tree t2
, int flags
)
670 bool needs_warning
= false;
672 /* We have to verify that the tags of the types are the same. This
673 is harder than it looks because this may be a typedef, so we have
674 to go look at the original type. It may even be a typedef of a
676 while (TYPE_NAME (t1
) && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
)
677 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
679 while (TYPE_NAME (t2
) && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
)
680 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
682 /* C90 didn't have the requirement that the two tags be the same. */
683 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
686 /* C90 didn't say what happened if one or both of the types were
687 incomplete; we choose to follow C99 rules here, which is that they
689 if (TYPE_SIZE (t1
) == NULL
690 || TYPE_SIZE (t2
) == NULL
)
694 const struct tagged_tu_seen
* tts_i
;
695 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
696 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
700 switch (TREE_CODE (t1
))
705 /* Speed up the case where the type values are in the same order. */
706 tree tv1
= TYPE_VALUES (t1
);
707 tree tv2
= TYPE_VALUES (t2
);
712 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv2
), tv2
= TREE_CHAIN (tv2
))
714 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
716 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
720 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
722 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
725 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
728 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
730 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
732 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
740 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
743 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= TREE_CHAIN (s1
))
746 struct tagged_tu_seen tts
;
748 tts
.next
= tagged_tu_seen_base
;
751 tagged_tu_seen_base
= &tts
;
753 if (DECL_NAME (s1
) != NULL
)
754 for (s2
= TYPE_VALUES (t2
); s2
; s2
= TREE_CHAIN (s2
))
755 if (DECL_NAME (s1
) == DECL_NAME (s2
))
758 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
), flags
);
762 needs_warning
= true;
764 if (TREE_CODE (s1
) == FIELD_DECL
765 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
766 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
772 tagged_tu_seen_base
= tts
.next
;
776 return needs_warning
? 2 : 1;
781 struct tagged_tu_seen tts
;
783 tts
.next
= tagged_tu_seen_base
;
786 tagged_tu_seen_base
= &tts
;
788 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
790 s1
= TREE_CHAIN (s1
), s2
= TREE_CHAIN (s2
))
793 if (TREE_CODE (s1
) != TREE_CODE (s2
)
794 || DECL_NAME (s1
) != DECL_NAME (s2
))
796 result
= comptypes (TREE_TYPE (s1
), TREE_TYPE (s2
), flags
);
800 needs_warning
= true;
802 if (TREE_CODE (s1
) == FIELD_DECL
803 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
804 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
807 tagged_tu_seen_base
= tts
.next
;
810 return needs_warning
? 2 : 1;
818 /* Return 1 if two function types F1 and F2 are compatible.
819 If either type specifies no argument types,
820 the other must specify a fixed number of self-promoting arg types.
821 Otherwise, if one type specifies only the number of arguments,
822 the other must specify that number of self-promoting arg types.
823 Otherwise, the argument types must match. */
826 function_types_compatible_p (tree f1
, tree f2
, int flags
)
829 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
834 ret1
= TREE_TYPE (f1
);
835 ret2
= TREE_TYPE (f2
);
837 /* 'volatile' qualifiers on a function's return type mean the function
839 if (pedantic
&& TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
840 pedwarn ("function return types not compatible due to `volatile'");
841 if (TYPE_VOLATILE (ret1
))
842 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
843 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
844 if (TYPE_VOLATILE (ret2
))
845 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
846 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
847 val
= comptypes (ret1
, ret2
, flags
);
851 args1
= TYPE_ARG_TYPES (f1
);
852 args2
= TYPE_ARG_TYPES (f2
);
854 /* An unspecified parmlist matches any specified parmlist
855 whose argument types don't need default promotions. */
859 if (!self_promoting_args_p (args2
))
861 /* If one of these types comes from a non-prototype fn definition,
862 compare that with the other type's arglist.
863 If they don't match, ask for a warning (but no error). */
864 if (TYPE_ACTUAL_ARG_TYPES (f1
)
865 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
872 if (!self_promoting_args_p (args1
))
874 if (TYPE_ACTUAL_ARG_TYPES (f2
)
875 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
881 /* Both types have argument lists: compare them and propagate results. */
882 val1
= type_lists_compatible_p (args1
, args2
, flags
);
883 return val1
!= 1 ? val1
: val
;
886 /* Check two lists of types for compatibility,
887 returning 0 for incompatible, 1 for compatible,
888 or 2 for compatible with warning. */
891 type_lists_compatible_p (tree args1
, tree args2
, int flags
)
893 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
899 if (args1
== 0 && args2
== 0)
901 /* If one list is shorter than the other,
902 they fail to match. */
903 if (args1
== 0 || args2
== 0)
905 /* A null pointer instead of a type
906 means there is supposed to be an argument
907 but nothing is specified about what type it has.
908 So match anything that self-promotes. */
909 if (TREE_VALUE (args1
) == 0)
911 if (c_type_promotes_to (TREE_VALUE (args2
)) != TREE_VALUE (args2
))
914 else if (TREE_VALUE (args2
) == 0)
916 if (c_type_promotes_to (TREE_VALUE (args1
)) != TREE_VALUE (args1
))
919 /* If one of the lists has an error marker, ignore this arg. */
920 else if (TREE_CODE (TREE_VALUE (args1
)) == ERROR_MARK
921 || TREE_CODE (TREE_VALUE (args2
)) == ERROR_MARK
)
923 else if (! (newval
= comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1
)),
924 TYPE_MAIN_VARIANT (TREE_VALUE (args2
)),
927 /* Allow wait (union {union wait *u; int *i} *)
928 and wait (union wait *) to be compatible. */
929 if (TREE_CODE (TREE_VALUE (args1
)) == UNION_TYPE
930 && (TYPE_NAME (TREE_VALUE (args1
)) == 0
931 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1
)))
932 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1
))) == INTEGER_CST
933 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1
)),
934 TYPE_SIZE (TREE_VALUE (args2
))))
937 for (memb
= TYPE_FIELDS (TREE_VALUE (args1
));
938 memb
; memb
= TREE_CHAIN (memb
))
939 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args2
),
945 else if (TREE_CODE (TREE_VALUE (args2
)) == UNION_TYPE
946 && (TYPE_NAME (TREE_VALUE (args2
)) == 0
947 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2
)))
948 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2
))) == INTEGER_CST
949 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2
)),
950 TYPE_SIZE (TREE_VALUE (args1
))))
953 for (memb
= TYPE_FIELDS (TREE_VALUE (args2
));
954 memb
; memb
= TREE_CHAIN (memb
))
955 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args1
),
965 /* comptypes said ok, but record if it said to warn. */
969 args1
= TREE_CHAIN (args1
);
970 args2
= TREE_CHAIN (args2
);
974 /* Compute the size to increment a pointer by. */
977 c_size_in_bytes (tree type
)
979 enum tree_code code
= TREE_CODE (type
);
981 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
982 return size_one_node
;
984 if (!COMPLETE_OR_VOID_TYPE_P (type
))
986 error ("arithmetic on pointer to an incomplete type");
987 return size_one_node
;
990 /* Convert in case a char is more than one unit. */
991 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
992 size_int (TYPE_PRECISION (char_type_node
)
996 /* Return either DECL or its known constant value (if it has one). */
999 decl_constant_value (tree decl
)
1001 if (/* Don't change a variable array bound or initial value to a constant
1002 in a place where a variable is invalid. */
1003 current_function_decl
!= 0
1004 && ! TREE_THIS_VOLATILE (decl
)
1005 && TREE_READONLY (decl
)
1006 && DECL_INITIAL (decl
) != 0
1007 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1008 /* This is invalid if initial value is not constant.
1009 If it has either a function call, a memory reference,
1010 or a variable, then re-evaluating it could give different results. */
1011 && TREE_CONSTANT (DECL_INITIAL (decl
))
1012 /* Check for cases where this is sub-optimal, even though valid. */
1013 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1014 return DECL_INITIAL (decl
);
1018 /* Return either DECL or its known constant value (if it has one), but
1019 return DECL if pedantic or DECL has mode BLKmode. This is for
1020 bug-compatibility with the old behavior of decl_constant_value
1021 (before GCC 3.0); every use of this function is a bug and it should
1022 be removed before GCC 3.1. It is not appropriate to use pedantic
1023 in a way that affects optimization, and BLKmode is probably not the
1024 right test for avoiding misoptimizations either. */
1027 decl_constant_value_for_broken_optimization (tree decl
)
1029 if (pedantic
|| DECL_MODE (decl
) == BLKmode
)
1032 return decl_constant_value (decl
);
1036 /* Perform the default conversion of arrays and functions to pointers.
1037 Return the result of converting EXP. For any other expression, just
1041 default_function_array_conversion (tree exp
)
1044 tree type
= TREE_TYPE (exp
);
1045 enum tree_code code
= TREE_CODE (type
);
1048 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1051 Do not use STRIP_NOPS here! It will remove conversions from pointer
1052 to integer and cause infinite recursion. */
1054 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1055 || (TREE_CODE (exp
) == NOP_EXPR
1056 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1058 if (TREE_CODE (exp
) == NON_LVALUE_EXPR
)
1060 exp
= TREE_OPERAND (exp
, 0);
1063 /* Preserve the original expression code. */
1064 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp
))))
1065 C_SET_EXP_ORIGINAL_CODE (exp
, C_EXP_ORIGINAL_CODE (orig_exp
));
1067 if (code
== FUNCTION_TYPE
)
1069 return build_unary_op (ADDR_EXPR
, exp
, 0);
1071 if (code
== ARRAY_TYPE
)
1074 tree restype
= TREE_TYPE (type
);
1080 if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'r' || DECL_P (exp
))
1082 constp
= TREE_READONLY (exp
);
1083 volatilep
= TREE_THIS_VOLATILE (exp
);
1086 if (TYPE_QUALS (type
) || constp
|| volatilep
)
1088 = c_build_qualified_type (restype
,
1090 | (constp
* TYPE_QUAL_CONST
)
1091 | (volatilep
* TYPE_QUAL_VOLATILE
));
1093 if (TREE_CODE (exp
) == INDIRECT_REF
)
1094 return convert (TYPE_POINTER_TO (restype
),
1095 TREE_OPERAND (exp
, 0));
1097 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1099 tree op1
= default_conversion (TREE_OPERAND (exp
, 1));
1100 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
1101 TREE_OPERAND (exp
, 0), op1
);
1104 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
);
1105 if (!flag_isoc99
&& !lvalue_array_p
)
1107 /* Before C99, non-lvalue arrays do not decay to pointers.
1108 Normally, using such an array would be invalid; but it can
1109 be used correctly inside sizeof or as a statement expression.
1110 Thus, do not give an error here; an error will result later. */
1114 ptrtype
= build_pointer_type (restype
);
1116 if (TREE_CODE (exp
) == VAR_DECL
)
1118 /* ??? This is not really quite correct
1119 in that the type of the operand of ADDR_EXPR
1120 is not the target type of the type of the ADDR_EXPR itself.
1121 Question is, can this lossage be avoided? */
1122 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
1123 if (!c_mark_addressable (exp
))
1124 return error_mark_node
;
1125 TREE_CONSTANT (adr
) = staticp (exp
);
1126 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
1129 /* This way is better for a COMPONENT_REF since it can
1130 simplify the offset for a component. */
1131 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
1132 return convert (ptrtype
, adr
);
1137 /* Perform default promotions for C data used in expressions.
1138 Arrays and functions are converted to pointers;
1139 enumeral types or short or char, to int.
1140 In addition, manifest constants symbols are replaced by their values. */
1143 default_conversion (tree exp
)
1146 tree type
= TREE_TYPE (exp
);
1147 enum tree_code code
= TREE_CODE (type
);
1149 if (code
== FUNCTION_TYPE
|| code
== ARRAY_TYPE
)
1150 return default_function_array_conversion (exp
);
1152 /* Constants can be used directly unless they're not loadable. */
1153 if (TREE_CODE (exp
) == CONST_DECL
)
1154 exp
= DECL_INITIAL (exp
);
1156 /* Replace a nonvolatile const static variable with its value unless
1157 it is an array, in which case we must be sure that taking the
1158 address of the array produces consistent results. */
1159 else if (optimize
&& TREE_CODE (exp
) == VAR_DECL
&& code
!= ARRAY_TYPE
)
1161 exp
= decl_constant_value_for_broken_optimization (exp
);
1162 type
= TREE_TYPE (exp
);
1165 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1168 Do not use STRIP_NOPS here! It will remove conversions from pointer
1169 to integer and cause infinite recursion. */
1171 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
1172 || (TREE_CODE (exp
) == NOP_EXPR
1173 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
1174 exp
= TREE_OPERAND (exp
, 0);
1176 /* Preserve the original expression code. */
1177 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp
))))
1178 C_SET_EXP_ORIGINAL_CODE (exp
, C_EXP_ORIGINAL_CODE (orig_exp
));
1180 /* Normally convert enums to int,
1181 but convert wide enums to something wider. */
1182 if (code
== ENUMERAL_TYPE
)
1184 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
1185 TYPE_PRECISION (integer_type_node
)),
1186 ((TYPE_PRECISION (type
)
1187 >= TYPE_PRECISION (integer_type_node
))
1188 && TREE_UNSIGNED (type
)));
1190 return convert (type
, exp
);
1193 if (TREE_CODE (exp
) == COMPONENT_REF
1194 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
1195 /* If it's thinner than an int, promote it like a
1196 c_promoting_integer_type_p, otherwise leave it alone. */
1197 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
1198 TYPE_PRECISION (integer_type_node
)))
1199 return convert (integer_type_node
, exp
);
1201 if (c_promoting_integer_type_p (type
))
1203 /* Preserve unsignedness if not really getting any wider. */
1204 if (TREE_UNSIGNED (type
)
1205 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1206 return convert (unsigned_type_node
, exp
);
1208 return convert (integer_type_node
, exp
);
1211 if (code
== VOID_TYPE
)
1213 error ("void value not ignored as it ought to be");
1214 return error_mark_node
;
1219 /* Look up COMPONENT in a structure or union DECL.
1221 If the component name is not found, returns NULL_TREE. Otherwise,
1222 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1223 stepping down the chain to the component, which is in the last
1224 TREE_VALUE of the list. Normally the list is of length one, but if
1225 the component is embedded within (nested) anonymous structures or
1226 unions, the list steps down the chain to the component. */
1229 lookup_field (tree decl
, tree component
)
1231 tree type
= TREE_TYPE (decl
);
1234 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1235 to the field elements. Use a binary search on this array to quickly
1236 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1237 will always be set for structures which have many elements. */
1239 if (TYPE_LANG_SPECIFIC (type
))
1242 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
1244 field
= TYPE_FIELDS (type
);
1246 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
1247 while (top
- bot
> 1)
1249 half
= (top
- bot
+ 1) >> 1;
1250 field
= field_array
[bot
+half
];
1252 if (DECL_NAME (field
) == NULL_TREE
)
1254 /* Step through all anon unions in linear fashion. */
1255 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
1257 field
= field_array
[bot
++];
1258 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1259 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
1261 tree anon
= lookup_field (field
, component
);
1264 return tree_cons (NULL_TREE
, field
, anon
);
1268 /* Entire record is only anon unions. */
1272 /* Restart the binary search, with new lower bound. */
1276 if (DECL_NAME (field
) == component
)
1278 if (DECL_NAME (field
) < component
)
1284 if (DECL_NAME (field_array
[bot
]) == component
)
1285 field
= field_array
[bot
];
1286 else if (DECL_NAME (field
) != component
)
1291 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1293 if (DECL_NAME (field
) == NULL_TREE
1294 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
1295 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
1297 tree anon
= lookup_field (field
, component
);
1300 return tree_cons (NULL_TREE
, field
, anon
);
1303 if (DECL_NAME (field
) == component
)
1307 if (field
== NULL_TREE
)
1311 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
1314 /* Make an expression to refer to the COMPONENT field of
1315 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1318 build_component_ref (tree datum
, tree component
)
1320 tree type
= TREE_TYPE (datum
);
1321 enum tree_code code
= TREE_CODE (type
);
1325 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1326 Ensure that the arguments are not lvalues; otherwise,
1327 if the component is an array, it would wrongly decay to a pointer in
1329 We cannot do this with a COND_EXPR, because in a conditional expression
1330 the default promotions are applied to both sides, and this would yield
1331 the wrong type of the result; for example, if the components have
1333 switch (TREE_CODE (datum
))
1337 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
);
1338 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1339 TREE_OPERAND (datum
, 0), non_lvalue (value
));
1345 /* See if there is a field or component with name COMPONENT. */
1347 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1349 if (!COMPLETE_TYPE_P (type
))
1351 c_incomplete_type_error (NULL_TREE
, type
);
1352 return error_mark_node
;
1355 field
= lookup_field (datum
, component
);
1359 error ("%s has no member named `%s'",
1360 code
== RECORD_TYPE
? "structure" : "union",
1361 IDENTIFIER_POINTER (component
));
1362 return error_mark_node
;
1365 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1366 This might be better solved in future the way the C++ front
1367 end does it - by giving the anonymous entities each a
1368 separate name and type, and then have build_component_ref
1369 recursively call itself. We can't do that here. */
1372 tree subdatum
= TREE_VALUE (field
);
1374 if (TREE_TYPE (subdatum
) == error_mark_node
)
1375 return error_mark_node
;
1377 ref
= build (COMPONENT_REF
, TREE_TYPE (subdatum
), datum
, subdatum
);
1378 if (TREE_READONLY (datum
) || TREE_READONLY (subdatum
))
1379 TREE_READONLY (ref
) = 1;
1380 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (subdatum
))
1381 TREE_THIS_VOLATILE (ref
) = 1;
1383 if (TREE_DEPRECATED (subdatum
))
1384 warn_deprecated_use (subdatum
);
1388 field
= TREE_CHAIN (field
);
1394 else if (code
!= ERROR_MARK
)
1395 error ("request for member `%s' in something not a structure or union",
1396 IDENTIFIER_POINTER (component
));
1398 return error_mark_node
;
1401 /* Given an expression PTR for a pointer, return an expression
1402 for the value pointed to.
1403 ERRORSTRING is the name of the operator to appear in error messages. */
1406 build_indirect_ref (tree ptr
, const char *errorstring
)
1408 tree pointer
= default_conversion (ptr
);
1409 tree type
= TREE_TYPE (pointer
);
1411 if (TREE_CODE (type
) == POINTER_TYPE
)
1413 if (TREE_CODE (pointer
) == ADDR_EXPR
1414 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
1415 == TREE_TYPE (type
)))
1416 return TREE_OPERAND (pointer
, 0);
1419 tree t
= TREE_TYPE (type
);
1420 tree ref
= build1 (INDIRECT_REF
, TYPE_MAIN_VARIANT (t
), pointer
);
1422 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
1424 error ("dereferencing pointer to incomplete type");
1425 return error_mark_node
;
1427 if (VOID_TYPE_P (t
) && skip_evaluation
== 0)
1428 warning ("dereferencing `void *' pointer");
1430 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1431 so that we get the proper error message if the result is used
1432 to assign to. Also, &* is supposed to be a no-op.
1433 And ANSI C seems to specify that the type of the result
1434 should be the const type. */
1435 /* A de-reference of a pointer to const is not a const. It is valid
1436 to change it via some other pointer. */
1437 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1438 TREE_SIDE_EFFECTS (ref
)
1439 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1440 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1444 else if (TREE_CODE (pointer
) != ERROR_MARK
)
1445 error ("invalid type argument of `%s'", errorstring
);
1446 return error_mark_node
;
1449 /* This handles expressions of the form "a[i]", which denotes
1452 This is logically equivalent in C to *(a+i), but we may do it differently.
1453 If A is a variable or a member, we generate a primitive ARRAY_REF.
1454 This avoids forcing the array out of registers, and can work on
1455 arrays that are not lvalues (for example, members of structures returned
1459 build_array_ref (tree array
, tree index
)
1463 error ("subscript missing in array reference");
1464 return error_mark_node
;
1467 if (TREE_TYPE (array
) == error_mark_node
1468 || TREE_TYPE (index
) == error_mark_node
)
1469 return error_mark_node
;
1471 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
1472 && TREE_CODE (array
) != INDIRECT_REF
)
1476 /* Subscripting with type char is likely to lose
1477 on a machine where chars are signed.
1478 So warn on any machine, but optionally.
1479 Don't warn for unsigned char since that type is safe.
1480 Don't warn for signed char because anyone who uses that
1481 must have done so deliberately. */
1482 if (warn_char_subscripts
1483 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1484 warning ("array subscript has type `char'");
1486 /* Apply default promotions *after* noticing character types. */
1487 index
= default_conversion (index
);
1489 /* Require integer *after* promotion, for sake of enums. */
1490 if (TREE_CODE (TREE_TYPE (index
)) != INTEGER_TYPE
)
1492 error ("array subscript is not an integer");
1493 return error_mark_node
;
1496 /* An array that is indexed by a non-constant
1497 cannot be stored in a register; we must be able to do
1498 address arithmetic on its address.
1499 Likewise an array of elements of variable size. */
1500 if (TREE_CODE (index
) != INTEGER_CST
1501 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
1502 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1504 if (!c_mark_addressable (array
))
1505 return error_mark_node
;
1507 /* An array that is indexed by a constant value which is not within
1508 the array bounds cannot be stored in a register either; because we
1509 would get a crash in store_bit_field/extract_bit_field when trying
1510 to access a non-existent part of the register. */
1511 if (TREE_CODE (index
) == INTEGER_CST
1512 && TYPE_VALUES (TREE_TYPE (array
))
1513 && ! int_fits_type_p (index
, TYPE_VALUES (TREE_TYPE (array
))))
1515 if (!c_mark_addressable (array
))
1516 return error_mark_node
;
1522 while (TREE_CODE (foo
) == COMPONENT_REF
)
1523 foo
= TREE_OPERAND (foo
, 0);
1524 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
1525 pedwarn ("ISO C forbids subscripting `register' array");
1526 else if (! flag_isoc99
&& ! lvalue_p (foo
))
1527 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1530 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1531 rval
= build (ARRAY_REF
, type
, array
, index
);
1532 /* Array ref is const/volatile if the array elements are
1533 or if the array is. */
1534 TREE_READONLY (rval
)
1535 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
1536 | TREE_READONLY (array
));
1537 TREE_SIDE_EFFECTS (rval
)
1538 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1539 | TREE_SIDE_EFFECTS (array
));
1540 TREE_THIS_VOLATILE (rval
)
1541 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1542 /* This was added by rms on 16 Nov 91.
1543 It fixes vol struct foo *a; a->elts[1]
1544 in an inline function.
1545 Hope it doesn't break something else. */
1546 | TREE_THIS_VOLATILE (array
));
1547 return require_complete_type (fold (rval
));
1551 tree ar
= default_conversion (array
);
1552 tree ind
= default_conversion (index
);
1554 /* Do the same warning check as above, but only on the part that's
1555 syntactically the index and only if it is also semantically
1557 if (warn_char_subscripts
1558 && TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
1559 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1560 warning ("subscript has type `char'");
1562 /* Put the integer in IND to simplify error checking. */
1563 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
1570 if (ar
== error_mark_node
)
1573 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
1574 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) == FUNCTION_TYPE
)
1576 error ("subscripted value is neither array nor pointer");
1577 return error_mark_node
;
1579 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
1581 error ("array subscript is not an integer");
1582 return error_mark_node
;
1585 return build_indirect_ref (build_binary_op (PLUS_EXPR
, ar
, ind
, 0),
1590 /* Build an external reference to identifier ID. FUN indicates
1591 whether this will be used for a function call. */
1593 build_external_ref (tree id
, int fun
)
1596 tree decl
= lookup_name (id
);
1597 tree objc_ivar
= lookup_objc_ivar (id
);
1599 if (decl
&& decl
!= error_mark_node
)
1601 /* Properly declared variable or function reference. */
1604 else if (decl
!= objc_ivar
&& !DECL_FILE_SCOPE_P (decl
))
1606 warning ("local declaration of `%s' hides instance variable",
1607 IDENTIFIER_POINTER (id
));
1616 /* Implicit function declaration. */
1617 ref
= implicitly_declare (id
);
1618 else if (decl
== error_mark_node
)
1619 /* Don't complain about something that's already been
1620 complained about. */
1621 return error_mark_node
;
1624 undeclared_variable (id
);
1625 return error_mark_node
;
1628 if (TREE_TYPE (ref
) == error_mark_node
)
1629 return error_mark_node
;
1631 if (TREE_DEPRECATED (ref
))
1632 warn_deprecated_use (ref
);
1634 if (!skip_evaluation
)
1635 assemble_external (ref
);
1636 TREE_USED (ref
) = 1;
1638 if (TREE_CODE (ref
) == CONST_DECL
)
1640 ref
= DECL_INITIAL (ref
);
1641 TREE_CONSTANT (ref
) = 1;
1643 else if (current_function_decl
!= 0
1644 && !DECL_FILE_SCOPE_P (current_function_decl
)
1645 && (TREE_CODE (ref
) == VAR_DECL
1646 || TREE_CODE (ref
) == PARM_DECL
1647 || TREE_CODE (ref
) == FUNCTION_DECL
))
1649 tree context
= decl_function_context (ref
);
1651 if (context
!= 0 && context
!= current_function_decl
)
1652 DECL_NONLOCAL (ref
) = 1;
1658 /* Build a function call to function FUNCTION with parameters PARAMS.
1659 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1660 TREE_VALUE of each node is a parameter-expression.
1661 FUNCTION's data type may be a function type or a pointer-to-function. */
1664 build_function_call (tree function
, tree params
)
1666 tree fntype
, fundecl
= 0;
1667 tree coerced_params
;
1668 tree name
= NULL_TREE
, result
;
1671 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1672 STRIP_TYPE_NOPS (function
);
1674 /* Convert anything with function type to a pointer-to-function. */
1675 if (TREE_CODE (function
) == FUNCTION_DECL
)
1677 name
= DECL_NAME (function
);
1679 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1680 (because calling an inline function does not mean the function
1681 needs to be separately compiled). */
1682 fntype
= build_type_variant (TREE_TYPE (function
),
1683 TREE_READONLY (function
),
1684 TREE_THIS_VOLATILE (function
));
1686 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
1689 function
= default_conversion (function
);
1691 fntype
= TREE_TYPE (function
);
1693 if (TREE_CODE (fntype
) == ERROR_MARK
)
1694 return error_mark_node
;
1696 if (!(TREE_CODE (fntype
) == POINTER_TYPE
1697 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
1699 error ("called object is not a function");
1700 return error_mark_node
;
1703 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
1704 current_function_returns_abnormally
= 1;
1706 /* fntype now gets the type of function pointed to. */
1707 fntype
= TREE_TYPE (fntype
);
1709 /* Check that the function is called through a compatible prototype.
1710 If it is not, replace the call by a trap, wrapped up in a compound
1711 expression if necessary. This has the nice side-effect to prevent
1712 the tree-inliner from generating invalid assignment trees which may
1713 blow up in the RTL expander later.
1715 ??? This doesn't work for Objective-C because objc_comptypes
1716 refuses to compare function prototypes, yet the compiler appears
1717 to build calls that are flagged as invalid by C's comptypes. */
1718 if (! c_dialect_objc ()
1719 && TREE_CODE (function
) == NOP_EXPR
1720 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
1721 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
1722 && ! comptypes (fntype
, TREE_TYPE (tem
), COMPARE_STRICT
))
1724 tree return_type
= TREE_TYPE (fntype
);
1725 tree trap
= build_function_call (built_in_decls
[BUILT_IN_TRAP
],
1728 /* This situation leads to run-time undefined behavior. We can't,
1729 therefore, simply error unless we can prove that all possible
1730 executions of the program must execute the code. */
1731 warning ("function called through a non-compatible type");
1733 /* We can, however, treat "undefined" any way we please.
1734 Call abort to encourage the user to fix the program. */
1735 inform ("if this code is reached, the program will abort");
1737 if (VOID_TYPE_P (return_type
))
1743 if (AGGREGATE_TYPE_P (return_type
))
1744 rhs
= build_compound_literal (return_type
,
1745 build_constructor (return_type
,
1748 rhs
= fold (build1 (NOP_EXPR
, return_type
, integer_zero_node
));
1750 return build (COMPOUND_EXPR
, return_type
, trap
, rhs
);
1754 /* Convert the parameters to the types declared in the
1755 function prototype, or apply default promotions. */
1758 = convert_arguments (TYPE_ARG_TYPES (fntype
), params
, name
, fundecl
);
1760 /* Check that the arguments to the function are valid. */
1762 check_function_arguments (TYPE_ATTRIBUTES (fntype
), coerced_params
);
1764 /* Recognize certain built-in functions so we can make tree-codes
1765 other than CALL_EXPR. We do this when it enables fold-const.c
1766 to do something useful. */
1768 if (TREE_CODE (function
) == ADDR_EXPR
1769 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1770 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
1772 result
= expand_tree_builtin (TREE_OPERAND (function
, 0),
1773 params
, coerced_params
);
1778 result
= build (CALL_EXPR
, TREE_TYPE (fntype
),
1779 function
, coerced_params
, NULL_TREE
);
1780 TREE_SIDE_EFFECTS (result
) = 1;
1781 result
= fold (result
);
1783 if (VOID_TYPE_P (TREE_TYPE (result
)))
1785 return require_complete_type (result
);
1788 /* Convert the argument expressions in the list VALUES
1789 to the types in the list TYPELIST. The result is a list of converted
1790 argument expressions.
1792 If TYPELIST is exhausted, or when an element has NULL as its type,
1793 perform the default conversions.
1795 PARMLIST is the chain of parm decls for the function being called.
1796 It may be 0, if that info is not available.
1797 It is used only for generating error messages.
1799 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1801 This is also where warnings about wrong number of args are generated.
1803 Both VALUES and the returned value are chains of TREE_LIST nodes
1804 with the elements of the list in the TREE_VALUE slots of those nodes. */
1807 convert_arguments (tree typelist
, tree values
, tree name
, tree fundecl
)
1809 tree typetail
, valtail
;
1813 /* Scan the given expressions and types, producing individual
1814 converted arguments and pushing them on RESULT in reverse order. */
1816 for (valtail
= values
, typetail
= typelist
, parmnum
= 0;
1818 valtail
= TREE_CHAIN (valtail
), parmnum
++)
1820 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
1821 tree val
= TREE_VALUE (valtail
);
1823 if (type
== void_type_node
)
1826 error ("too many arguments to function `%s'",
1827 IDENTIFIER_POINTER (name
));
1829 error ("too many arguments to function");
1833 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1834 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1835 to convert automatically to a pointer. */
1836 if (TREE_CODE (val
) == NON_LVALUE_EXPR
)
1837 val
= TREE_OPERAND (val
, 0);
1839 val
= default_function_array_conversion (val
);
1841 val
= require_complete_type (val
);
1845 /* Formal parm type is specified by a function prototype. */
1848 if (!COMPLETE_TYPE_P (type
))
1850 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
1855 /* Optionally warn about conversions that
1856 differ from the default conversions. */
1857 if (warn_conversion
|| warn_traditional
)
1859 int formal_prec
= TYPE_PRECISION (type
);
1861 if (INTEGRAL_TYPE_P (type
)
1862 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1863 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
1864 if (INTEGRAL_TYPE_P (type
)
1865 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
1866 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
1867 else if (TREE_CODE (type
) == COMPLEX_TYPE
1868 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1869 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
1870 else if (TREE_CODE (type
) == REAL_TYPE
1871 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1872 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
1873 else if (TREE_CODE (type
) == COMPLEX_TYPE
1874 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1875 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
1876 else if (TREE_CODE (type
) == REAL_TYPE
1877 && TREE_CODE (TREE_TYPE (val
)) == COMPLEX_TYPE
)
1878 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name
, parmnum
+ 1);
1879 /* ??? At some point, messages should be written about
1880 conversions between complex types, but that's too messy
1882 else if (TREE_CODE (type
) == REAL_TYPE
1883 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1885 /* Warn if any argument is passed as `float',
1886 since without a prototype it would be `double'. */
1887 if (formal_prec
== TYPE_PRECISION (float_type_node
))
1888 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name
, parmnum
+ 1);
1890 /* Detect integer changing in width or signedness.
1891 These warnings are only activated with
1892 -Wconversion, not with -Wtraditional. */
1893 else if (warn_conversion
&& INTEGRAL_TYPE_P (type
)
1894 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1896 tree would_have_been
= default_conversion (val
);
1897 tree type1
= TREE_TYPE (would_have_been
);
1899 if (TREE_CODE (type
) == ENUMERAL_TYPE
1900 && (TYPE_MAIN_VARIANT (type
)
1901 == TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
1902 /* No warning if function asks for enum
1903 and the actual arg is that enum type. */
1905 else if (formal_prec
!= TYPE_PRECISION (type1
))
1906 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name
, parmnum
+ 1);
1907 else if (TREE_UNSIGNED (type
) == TREE_UNSIGNED (type1
))
1909 /* Don't complain if the formal parameter type
1910 is an enum, because we can't tell now whether
1911 the value was an enum--even the same enum. */
1912 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1914 else if (TREE_CODE (val
) == INTEGER_CST
1915 && int_fits_type_p (val
, type
))
1916 /* Change in signedness doesn't matter
1917 if a constant value is unaffected. */
1919 /* Likewise for a constant in a NOP_EXPR. */
1920 else if (TREE_CODE (val
) == NOP_EXPR
1921 && TREE_CODE (TREE_OPERAND (val
, 0)) == INTEGER_CST
1922 && int_fits_type_p (TREE_OPERAND (val
, 0), type
))
1924 /* If the value is extended from a narrower
1925 unsigned type, it doesn't matter whether we
1926 pass it as signed or unsigned; the value
1927 certainly is the same either way. */
1928 else if (TYPE_PRECISION (TREE_TYPE (val
)) < TYPE_PRECISION (type
)
1929 && TREE_UNSIGNED (TREE_TYPE (val
)))
1931 else if (TREE_UNSIGNED (type
))
1932 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name
, parmnum
+ 1);
1934 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name
, parmnum
+ 1);
1938 parmval
= convert_for_assignment (type
, val
,
1939 (char *) 0, /* arg passing */
1940 fundecl
, name
, parmnum
+ 1);
1942 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
1943 && INTEGRAL_TYPE_P (type
)
1944 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
1945 parmval
= default_conversion (parmval
);
1947 result
= tree_cons (NULL_TREE
, parmval
, result
);
1949 else if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
1950 && (TYPE_PRECISION (TREE_TYPE (val
))
1951 < TYPE_PRECISION (double_type_node
)))
1952 /* Convert `float' to `double'. */
1953 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
1955 /* Convert `short' and `char' to full-size `int'. */
1956 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
1959 typetail
= TREE_CHAIN (typetail
);
1962 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
1965 error ("too few arguments to function `%s'",
1966 IDENTIFIER_POINTER (name
));
1968 error ("too few arguments to function");
1971 return nreverse (result
);
1974 /* This is the entry point used by the parser
1975 for binary operators in the input.
1976 In addition to constructing the expression,
1977 we check for operands that were written with other binary operators
1978 in a way that is likely to confuse the user. */
1981 parser_build_binary_op (enum tree_code code
, tree arg1
, tree arg2
)
1983 tree result
= build_binary_op (code
, arg1
, arg2
, 1);
1986 char class1
= TREE_CODE_CLASS (TREE_CODE (arg1
));
1987 char class2
= TREE_CODE_CLASS (TREE_CODE (arg2
));
1988 enum tree_code code1
= ERROR_MARK
;
1989 enum tree_code code2
= ERROR_MARK
;
1991 if (TREE_CODE (result
) == ERROR_MARK
)
1992 return error_mark_node
;
1994 if (IS_EXPR_CODE_CLASS (class1
))
1995 code1
= C_EXP_ORIGINAL_CODE (arg1
);
1996 if (IS_EXPR_CODE_CLASS (class2
))
1997 code2
= C_EXP_ORIGINAL_CODE (arg2
);
1999 /* Check for cases such as x+y<<z which users are likely
2000 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2001 is cleared to prevent these warnings. */
2002 if (warn_parentheses
)
2004 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
2006 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2007 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2008 warning ("suggest parentheses around + or - inside shift");
2011 if (code
== TRUTH_ORIF_EXPR
)
2013 if (code1
== TRUTH_ANDIF_EXPR
2014 || code2
== TRUTH_ANDIF_EXPR
)
2015 warning ("suggest parentheses around && within ||");
2018 if (code
== BIT_IOR_EXPR
)
2020 if (code1
== BIT_AND_EXPR
|| code1
== BIT_XOR_EXPR
2021 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2022 || code2
== BIT_AND_EXPR
|| code2
== BIT_XOR_EXPR
2023 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2024 warning ("suggest parentheses around arithmetic in operand of |");
2025 /* Check cases like x|y==z */
2026 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2027 warning ("suggest parentheses around comparison in operand of |");
2030 if (code
== BIT_XOR_EXPR
)
2032 if (code1
== BIT_AND_EXPR
2033 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2034 || code2
== BIT_AND_EXPR
2035 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2036 warning ("suggest parentheses around arithmetic in operand of ^");
2037 /* Check cases like x^y==z */
2038 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2039 warning ("suggest parentheses around comparison in operand of ^");
2042 if (code
== BIT_AND_EXPR
)
2044 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2045 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2046 warning ("suggest parentheses around + or - in operand of &");
2047 /* Check cases like x&y==z */
2048 if (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<')
2049 warning ("suggest parentheses around comparison in operand of &");
2053 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2054 if (TREE_CODE_CLASS (code
) == '<' && extra_warnings
2055 && (TREE_CODE_CLASS (code1
) == '<' || TREE_CODE_CLASS (code2
) == '<'))
2056 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2058 unsigned_conversion_warning (result
, arg1
);
2059 unsigned_conversion_warning (result
, arg2
);
2060 overflow_warning (result
);
2062 class = TREE_CODE_CLASS (TREE_CODE (result
));
2064 /* Record the code that was specified in the source,
2065 for the sake of warnings about confusing nesting. */
2066 if (IS_EXPR_CODE_CLASS (class))
2067 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2070 int flag
= TREE_CONSTANT (result
);
2071 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2072 so that convert_for_assignment wouldn't strip it.
2073 That way, we got warnings for things like p = (1 - 1).
2074 But it turns out we should not get those warnings. */
2075 result
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (result
), result
);
2076 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2077 TREE_CONSTANT (result
) = flag
;
2084 /* Return true if `t' is known to be non-negative. */
2087 c_tree_expr_nonnegative_p (tree t
)
2089 if (TREE_CODE (t
) == STMT_EXPR
)
2091 t
= COMPOUND_BODY (STMT_EXPR_STMT (t
));
2093 /* Find the last statement in the chain, ignoring the final
2094 * scope statement */
2095 while (TREE_CHAIN (t
) != NULL_TREE
2096 && TREE_CODE (TREE_CHAIN (t
)) != SCOPE_STMT
)
2098 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0));
2100 return tree_expr_nonnegative_p (t
);
2103 /* Return a tree for the difference of pointers OP0 and OP1.
2104 The resulting tree has type int. */
2107 pointer_diff (tree op0
, tree op1
)
2109 tree result
, folded
;
2110 tree restype
= ptrdiff_type_node
;
2112 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
2113 tree con0
, con1
, lit0
, lit1
;
2114 tree orig_op1
= op1
;
2116 if (pedantic
|| warn_pointer_arith
)
2118 if (TREE_CODE (target_type
) == VOID_TYPE
)
2119 pedwarn ("pointer of type `void *' used in subtraction");
2120 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
2121 pedwarn ("pointer to a function used in subtraction");
2124 /* If the conversion to ptrdiff_type does anything like widening or
2125 converting a partial to an integral mode, we get a convert_expression
2126 that is in the way to do any simplifications.
2127 (fold-const.c doesn't know that the extra bits won't be needed.
2128 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2129 different mode in place.)
2130 So first try to find a common term here 'by hand'; we want to cover
2131 at least the cases that occur in legal static initializers. */
2132 con0
= TREE_CODE (op0
) == NOP_EXPR
? TREE_OPERAND (op0
, 0) : op0
;
2133 con1
= TREE_CODE (op1
) == NOP_EXPR
? TREE_OPERAND (op1
, 0) : op1
;
2135 if (TREE_CODE (con0
) == PLUS_EXPR
)
2137 lit0
= TREE_OPERAND (con0
, 1);
2138 con0
= TREE_OPERAND (con0
, 0);
2141 lit0
= integer_zero_node
;
2143 if (TREE_CODE (con1
) == PLUS_EXPR
)
2145 lit1
= TREE_OPERAND (con1
, 1);
2146 con1
= TREE_OPERAND (con1
, 0);
2149 lit1
= integer_zero_node
;
2151 if (operand_equal_p (con0
, con1
, 0))
2158 /* First do the subtraction as integers;
2159 then drop through to build the divide operator.
2160 Do not do default conversions on the minus operator
2161 in case restype is a short type. */
2163 op0
= build_binary_op (MINUS_EXPR
, convert (restype
, op0
),
2164 convert (restype
, op1
), 0);
2165 /* This generates an error if op1 is pointer to incomplete type. */
2166 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
2167 error ("arithmetic on pointer to an incomplete type");
2169 /* This generates an error if op0 is pointer to incomplete type. */
2170 op1
= c_size_in_bytes (target_type
);
2172 /* Divide by the size, in easiest possible way. */
2174 result
= build (EXACT_DIV_EXPR
, restype
, op0
, convert (restype
, op1
));
2176 folded
= fold (result
);
2177 if (folded
== result
)
2178 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2182 /* Construct and perhaps optimize a tree representation
2183 for a unary operation. CODE, a tree_code, specifies the operation
2184 and XARG is the operand.
2185 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2186 the default promotions (such as from short to int).
2187 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2188 allows non-lvalues; this is only used to handle conversion of non-lvalue
2189 arrays to pointers in C99. */
2192 build_unary_op (enum tree_code code
, tree xarg
, int flag
)
2194 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2197 enum tree_code typecode
= TREE_CODE (TREE_TYPE (arg
));
2199 int noconvert
= flag
;
2201 if (typecode
== ERROR_MARK
)
2202 return error_mark_node
;
2203 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
2204 typecode
= INTEGER_TYPE
;
2209 /* This is used for unary plus, because a CONVERT_EXPR
2210 is enough to prevent anybody from looking inside for
2211 associativity, but won't generate any code. */
2212 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2213 || typecode
== COMPLEX_TYPE
))
2215 error ("wrong type argument to unary plus");
2216 return error_mark_node
;
2218 else if (!noconvert
)
2219 arg
= default_conversion (arg
);
2220 arg
= non_lvalue (arg
);
2224 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2225 || typecode
== COMPLEX_TYPE
2226 || typecode
== VECTOR_TYPE
))
2228 error ("wrong type argument to unary minus");
2229 return error_mark_node
;
2231 else if (!noconvert
)
2232 arg
= default_conversion (arg
);
2236 if (typecode
== INTEGER_TYPE
|| typecode
== VECTOR_TYPE
)
2239 arg
= default_conversion (arg
);
2241 else if (typecode
== COMPLEX_TYPE
)
2245 pedwarn ("ISO C does not support `~' for complex conjugation");
2247 arg
= default_conversion (arg
);
2251 error ("wrong type argument to bit-complement");
2252 return error_mark_node
;
2257 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2259 error ("wrong type argument to abs");
2260 return error_mark_node
;
2262 else if (!noconvert
)
2263 arg
= default_conversion (arg
);
2267 /* Conjugating a real value is a no-op, but allow it anyway. */
2268 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
2269 || typecode
== COMPLEX_TYPE
))
2271 error ("wrong type argument to conjugation");
2272 return error_mark_node
;
2274 else if (!noconvert
)
2275 arg
= default_conversion (arg
);
2278 case TRUTH_NOT_EXPR
:
2279 if (typecode
!= INTEGER_TYPE
2280 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
2281 && typecode
!= COMPLEX_TYPE
2282 /* These will convert to a pointer. */
2283 && typecode
!= ARRAY_TYPE
&& typecode
!= FUNCTION_TYPE
)
2285 error ("wrong type argument to unary exclamation mark");
2286 return error_mark_node
;
2288 arg
= (*lang_hooks
.truthvalue_conversion
) (arg
);
2289 return invert_truthvalue (arg
);
2295 if (TREE_CODE (arg
) == COMPLEX_CST
)
2296 return TREE_REALPART (arg
);
2297 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2298 return fold (build1 (REALPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2303 if (TREE_CODE (arg
) == COMPLEX_CST
)
2304 return TREE_IMAGPART (arg
);
2305 else if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
2306 return fold (build1 (IMAGPART_EXPR
, TREE_TYPE (TREE_TYPE (arg
)), arg
));
2308 return convert (TREE_TYPE (arg
), integer_zero_node
);
2310 case PREINCREMENT_EXPR
:
2311 case POSTINCREMENT_EXPR
:
2312 case PREDECREMENT_EXPR
:
2313 case POSTDECREMENT_EXPR
:
2315 /* Increment or decrement the real part of the value,
2316 and don't change the imaginary part. */
2317 if (typecode
== COMPLEX_TYPE
)
2322 pedwarn ("ISO C does not support `++' and `--' on complex types");
2324 arg
= stabilize_reference (arg
);
2325 real
= build_unary_op (REALPART_EXPR
, arg
, 1);
2326 imag
= build_unary_op (IMAGPART_EXPR
, arg
, 1);
2327 return build (COMPLEX_EXPR
, TREE_TYPE (arg
),
2328 build_unary_op (code
, real
, 1), imag
);
2331 /* Report invalid types. */
2333 if (typecode
!= POINTER_TYPE
2334 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
2336 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2337 error ("wrong type argument to increment");
2339 error ("wrong type argument to decrement");
2341 return error_mark_node
;
2346 tree result_type
= TREE_TYPE (arg
);
2348 arg
= get_unwidened (arg
, 0);
2349 argtype
= TREE_TYPE (arg
);
2351 /* Compute the increment. */
2353 if (typecode
== POINTER_TYPE
)
2355 /* If pointer target is an undefined struct,
2356 we just cannot know how to do the arithmetic. */
2357 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type
)))
2359 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2360 error ("increment of pointer to unknown structure");
2362 error ("decrement of pointer to unknown structure");
2364 else if ((pedantic
|| warn_pointer_arith
)
2365 && (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
2366 || TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
))
2368 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2369 pedwarn ("wrong type argument to increment");
2371 pedwarn ("wrong type argument to decrement");
2374 inc
= c_size_in_bytes (TREE_TYPE (result_type
));
2377 inc
= integer_one_node
;
2379 inc
= convert (argtype
, inc
);
2381 /* Complain about anything else that is not a true lvalue. */
2382 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
2383 || code
== POSTINCREMENT_EXPR
)
2384 ? "invalid lvalue in increment"
2385 : "invalid lvalue in decrement")))
2386 return error_mark_node
;
2388 /* Report a read-only lvalue. */
2389 if (TREE_READONLY (arg
))
2390 readonly_error (arg
,
2391 ((code
== PREINCREMENT_EXPR
2392 || code
== POSTINCREMENT_EXPR
)
2393 ? "increment" : "decrement"));
2395 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
2396 val
= boolean_increment (code
, arg
);
2398 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
2399 TREE_SIDE_EFFECTS (val
) = 1;
2400 val
= convert (result_type
, val
);
2401 if (TREE_CODE (val
) != code
)
2402 TREE_NO_UNUSED_WARNING (val
) = 1;
2407 /* Note that this operation never does default_conversion. */
2409 /* Let &* cancel out to simplify resulting code. */
2410 if (TREE_CODE (arg
) == INDIRECT_REF
)
2412 /* Don't let this be an lvalue. */
2413 if (lvalue_p (TREE_OPERAND (arg
, 0)))
2414 return non_lvalue (TREE_OPERAND (arg
, 0));
2415 return TREE_OPERAND (arg
, 0);
2418 /* For &x[y], return x+y */
2419 if (TREE_CODE (arg
) == ARRAY_REF
)
2421 if (!c_mark_addressable (TREE_OPERAND (arg
, 0)))
2422 return error_mark_node
;
2423 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
2424 TREE_OPERAND (arg
, 1), 1);
2427 /* Anything not already handled and not a true memory reference
2428 or a non-lvalue array is an error. */
2429 else if (typecode
!= FUNCTION_TYPE
&& !flag
2430 && !lvalue_or_else (arg
, "invalid lvalue in unary `&'"))
2431 return error_mark_node
;
2433 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2434 argtype
= TREE_TYPE (arg
);
2436 /* If the lvalue is const or volatile, merge that into the type
2437 to which the address will point. Note that you can't get a
2438 restricted pointer by taking the address of something, so we
2439 only have to deal with `const' and `volatile' here. */
2440 if ((DECL_P (arg
) || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
2441 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
)))
2442 argtype
= c_build_type_variant (argtype
,
2443 TREE_READONLY (arg
),
2444 TREE_THIS_VOLATILE (arg
));
2446 argtype
= build_pointer_type (argtype
);
2448 if (!c_mark_addressable (arg
))
2449 return error_mark_node
;
2454 if (TREE_CODE (arg
) == COMPONENT_REF
)
2456 tree field
= TREE_OPERAND (arg
, 1);
2458 addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), flag
);
2460 if (DECL_C_BIT_FIELD (field
))
2462 error ("attempt to take address of bit-field structure member `%s'",
2463 IDENTIFIER_POINTER (DECL_NAME (field
)));
2464 return error_mark_node
;
2467 addr
= fold (build (PLUS_EXPR
, argtype
,
2468 convert (argtype
, addr
),
2469 convert (argtype
, byte_position (field
))));
2472 addr
= build1 (code
, argtype
, arg
);
2474 /* Address of a static or external variable or
2475 file-scope function counts as a constant. */
2477 && ! (TREE_CODE (arg
) == FUNCTION_DECL
2478 && !DECL_FILE_SCOPE_P (arg
)))
2479 TREE_CONSTANT (addr
) = 1;
2488 argtype
= TREE_TYPE (arg
);
2489 return fold (build1 (code
, argtype
, arg
));
2492 /* Return nonzero if REF is an lvalue valid for this language.
2493 Lvalues can be assigned, unless their type has TYPE_READONLY.
2494 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2499 enum tree_code code
= TREE_CODE (ref
);
2506 return lvalue_p (TREE_OPERAND (ref
, 0));
2508 case COMPOUND_LITERAL_EXPR
:
2518 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
2519 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
2523 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
2530 /* Return nonzero if REF is an lvalue valid for this language;
2531 otherwise, print an error message and return zero. */
2534 lvalue_or_else (tree ref
, const char *msgid
)
2536 int win
= lvalue_p (ref
);
2539 error ("%s", msgid
);
2545 /* Warn about storing in something that is `const'. */
2548 readonly_error (tree arg
, const char *msgid
)
2550 if (TREE_CODE (arg
) == COMPONENT_REF
)
2552 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
2553 readonly_error (TREE_OPERAND (arg
, 0), msgid
);
2555 error ("%s of read-only member `%s'", _(msgid
),
2556 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg
, 1))));
2558 else if (TREE_CODE (arg
) == VAR_DECL
)
2559 error ("%s of read-only variable `%s'", _(msgid
),
2560 IDENTIFIER_POINTER (DECL_NAME (arg
)));
2562 error ("%s of read-only location", _(msgid
));
2565 /* Mark EXP saying that we need to be able to take the
2566 address of it; it should not be allocated in a register.
2567 Returns true if successful. */
2570 c_mark_addressable (tree exp
)
2575 switch (TREE_CODE (x
))
2578 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
2580 error ("cannot take address of bit-field `%s'",
2581 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x
, 1))));
2585 /* ... fall through ... */
2591 x
= TREE_OPERAND (x
, 0);
2594 case COMPOUND_LITERAL_EXPR
:
2596 TREE_ADDRESSABLE (x
) = 1;
2603 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
2604 && DECL_NONLOCAL (x
))
2606 if (TREE_PUBLIC (x
))
2608 error ("global register variable `%s' used in nested function",
2609 IDENTIFIER_POINTER (DECL_NAME (x
)));
2612 pedwarn ("register variable `%s' used in nested function",
2613 IDENTIFIER_POINTER (DECL_NAME (x
)));
2615 else if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
))
2617 if (TREE_PUBLIC (x
))
2619 error ("address of global register variable `%s' requested",
2620 IDENTIFIER_POINTER (DECL_NAME (x
)));
2624 /* If we are making this addressable due to its having
2625 volatile components, give a different error message. Also
2626 handle the case of an unnamed parameter by not trying
2627 to give the name. */
2629 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x
)))
2631 error ("cannot put object with volatile field into register");
2635 pedwarn ("address of register variable `%s' requested",
2636 IDENTIFIER_POINTER (DECL_NAME (x
)));
2638 put_var_into_stack (x
, /*rescan=*/true);
2642 TREE_ADDRESSABLE (x
) = 1;
2649 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2652 build_conditional_expr (tree ifexp
, tree op1
, tree op2
)
2656 enum tree_code code1
;
2657 enum tree_code code2
;
2658 tree result_type
= NULL
;
2659 tree orig_op1
= op1
, orig_op2
= op2
;
2661 ifexp
= (*lang_hooks
.truthvalue_conversion
) (default_conversion (ifexp
));
2663 /* Promote both alternatives. */
2665 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
2666 op1
= default_conversion (op1
);
2667 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
2668 op2
= default_conversion (op2
);
2670 if (TREE_CODE (ifexp
) == ERROR_MARK
2671 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
2672 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
2673 return error_mark_node
;
2675 type1
= TREE_TYPE (op1
);
2676 code1
= TREE_CODE (type1
);
2677 type2
= TREE_TYPE (op2
);
2678 code2
= TREE_CODE (type2
);
2680 /* C90 does not permit non-lvalue arrays in conditional expressions.
2681 In C99 they will be pointers by now. */
2682 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
2684 error ("non-lvalue array in conditional expression");
2685 return error_mark_node
;
2688 /* Quickly detect the usual case where op1 and op2 have the same type
2690 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
2693 result_type
= type1
;
2695 result_type
= TYPE_MAIN_VARIANT (type1
);
2697 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
2698 || code1
== COMPLEX_TYPE
)
2699 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
2700 || code2
== COMPLEX_TYPE
))
2702 result_type
= common_type (type1
, type2
);
2704 /* If -Wsign-compare, warn here if type1 and type2 have
2705 different signedness. We'll promote the signed to unsigned
2706 and later code won't know it used to be different.
2707 Do this check on the original types, so that explicit casts
2708 will be considered, but default promotions won't. */
2709 if (warn_sign_compare
&& !skip_evaluation
)
2711 int unsigned_op1
= TREE_UNSIGNED (TREE_TYPE (orig_op1
));
2712 int unsigned_op2
= TREE_UNSIGNED (TREE_TYPE (orig_op2
));
2714 if (unsigned_op1
^ unsigned_op2
)
2716 /* Do not warn if the result type is signed, since the
2717 signed type will only be chosen if it can represent
2718 all the values of the unsigned type. */
2719 if (! TREE_UNSIGNED (result_type
))
2721 /* Do not warn if the signed quantity is an unsuffixed
2722 integer literal (or some static constant expression
2723 involving such literals) and it is non-negative. */
2724 else if ((unsigned_op2
&& c_tree_expr_nonnegative_p (op1
))
2725 || (unsigned_op1
&& c_tree_expr_nonnegative_p (op2
)))
2728 warning ("signed and unsigned type in conditional expression");
2732 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
2734 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
2735 pedwarn ("ISO C forbids conditional expr with only one void side");
2736 result_type
= void_type_node
;
2738 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
2740 if (comp_target_types (type1
, type2
, 1))
2741 result_type
= common_type (type1
, type2
);
2742 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
2743 && TREE_CODE (orig_op1
) != NOP_EXPR
)
2744 result_type
= qualify_type (type2
, type1
);
2745 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
2746 && TREE_CODE (orig_op2
) != NOP_EXPR
)
2747 result_type
= qualify_type (type1
, type2
);
2748 else if (VOID_TYPE_P (TREE_TYPE (type1
)))
2750 if (pedantic
&& TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
2751 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2752 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
2753 TREE_TYPE (type2
)));
2755 else if (VOID_TYPE_P (TREE_TYPE (type2
)))
2757 if (pedantic
&& TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
2758 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2759 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
2760 TREE_TYPE (type1
)));
2764 pedwarn ("pointer type mismatch in conditional expression");
2765 result_type
= build_pointer_type (void_type_node
);
2768 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
2770 if (! integer_zerop (op2
))
2771 pedwarn ("pointer/integer type mismatch in conditional expression");
2774 op2
= null_pointer_node
;
2776 result_type
= type1
;
2778 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2780 if (!integer_zerop (op1
))
2781 pedwarn ("pointer/integer type mismatch in conditional expression");
2784 op1
= null_pointer_node
;
2786 result_type
= type2
;
2791 if (flag_cond_mismatch
)
2792 result_type
= void_type_node
;
2795 error ("type mismatch in conditional expression");
2796 return error_mark_node
;
2800 /* Merge const and volatile flags of the incoming types. */
2802 = build_type_variant (result_type
,
2803 TREE_READONLY (op1
) || TREE_READONLY (op2
),
2804 TREE_THIS_VOLATILE (op1
) || TREE_THIS_VOLATILE (op2
));
2806 if (result_type
!= TREE_TYPE (op1
))
2807 op1
= convert_and_check (result_type
, op1
);
2808 if (result_type
!= TREE_TYPE (op2
))
2809 op2
= convert_and_check (result_type
, op2
);
2811 if (TREE_CODE (ifexp
) == INTEGER_CST
)
2812 return non_lvalue (integer_zerop (ifexp
) ? op2
: op1
);
2814 return fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
));
2817 /* Given a list of expressions, return a compound expression
2818 that performs them all and returns the value of the last of them. */
2821 build_compound_expr (tree list
)
2823 return internal_build_compound_expr (list
, TRUE
);
2827 internal_build_compound_expr (tree list
, int first_p
)
2831 if (TREE_CHAIN (list
) == 0)
2833 /* Convert arrays and functions to pointers when there
2834 really is a comma operator. */
2837 = default_function_array_conversion (TREE_VALUE (list
));
2839 /* Don't let (0, 0) be null pointer constant. */
2840 if (!first_p
&& integer_zerop (TREE_VALUE (list
)))
2841 return non_lvalue (TREE_VALUE (list
));
2842 return TREE_VALUE (list
);
2845 rest
= internal_build_compound_expr (TREE_CHAIN (list
), FALSE
);
2847 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
2849 /* The left-hand operand of a comma expression is like an expression
2850 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2851 any side-effects, unless it was explicitly cast to (void). */
2852 if (warn_unused_value
2853 && ! (TREE_CODE (TREE_VALUE (list
)) == CONVERT_EXPR
2854 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list
)))))
2855 warning ("left-hand operand of comma expression has no effect");
2858 /* With -Wunused, we should also warn if the left-hand operand does have
2859 side-effects, but computes a value which is not used. For example, in
2860 `foo() + bar(), baz()' the result of the `+' operator is not used,
2861 so we should issue a warning. */
2862 else if (warn_unused_value
)
2863 warn_if_unused_value (TREE_VALUE (list
));
2865 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), TREE_VALUE (list
), rest
);
2868 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2871 build_c_cast (tree type
, tree expr
)
2875 if (type
== error_mark_node
|| expr
== error_mark_node
)
2876 return error_mark_node
;
2878 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2879 only in <protocol> qualifications. But when constructing cast expressions,
2880 the protocols do matter and must be kept around. */
2881 if (!c_dialect_objc () || !objc_is_object_ptr (type
))
2882 type
= TYPE_MAIN_VARIANT (type
);
2884 if (TREE_CODE (type
) == ARRAY_TYPE
)
2886 error ("cast specifies array type");
2887 return error_mark_node
;
2890 if (TREE_CODE (type
) == FUNCTION_TYPE
)
2892 error ("cast specifies function type");
2893 return error_mark_node
;
2896 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
2900 if (TREE_CODE (type
) == RECORD_TYPE
2901 || TREE_CODE (type
) == UNION_TYPE
)
2902 pedwarn ("ISO C forbids casting nonscalar to the same type");
2905 else if (TREE_CODE (type
) == UNION_TYPE
)
2908 value
= default_function_array_conversion (value
);
2910 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
2911 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
2912 TYPE_MAIN_VARIANT (TREE_TYPE (value
)), COMPARE_STRICT
))
2920 pedwarn ("ISO C forbids casts to union type");
2921 t
= digest_init (type
,
2922 build_constructor (type
,
2923 build_tree_list (field
, value
)),
2925 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
2928 error ("cast to union type from type not present in union");
2929 return error_mark_node
;
2935 /* If casting to void, avoid the error that would come
2936 from default_conversion in the case of a non-lvalue array. */
2937 if (type
== void_type_node
)
2938 return build1 (CONVERT_EXPR
, type
, value
);
2940 /* Convert functions and arrays to pointers,
2941 but don't convert any other types. */
2942 value
= default_function_array_conversion (value
);
2943 otype
= TREE_TYPE (value
);
2945 /* Optionally warn about potentially worrisome casts. */
2948 && TREE_CODE (type
) == POINTER_TYPE
2949 && TREE_CODE (otype
) == POINTER_TYPE
)
2951 tree in_type
= type
;
2952 tree in_otype
= otype
;
2956 /* Check that the qualifiers on IN_TYPE are a superset of
2957 the qualifiers of IN_OTYPE. The outermost level of
2958 POINTER_TYPE nodes is uninteresting and we stop as soon
2959 as we hit a non-POINTER_TYPE node on either type. */
2962 in_otype
= TREE_TYPE (in_otype
);
2963 in_type
= TREE_TYPE (in_type
);
2965 /* GNU C allows cv-qualified function types. 'const'
2966 means the function is very pure, 'volatile' means it
2967 can't return. We need to warn when such qualifiers
2968 are added, not when they're taken away. */
2969 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
2970 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
2971 added
|= (TYPE_QUALS (in_type
) & ~TYPE_QUALS (in_otype
));
2973 discarded
|= (TYPE_QUALS (in_otype
) & ~TYPE_QUALS (in_type
));
2975 while (TREE_CODE (in_type
) == POINTER_TYPE
2976 && TREE_CODE (in_otype
) == POINTER_TYPE
);
2979 warning ("cast adds new qualifiers to function type");
2982 /* There are qualifiers present in IN_OTYPE that are not
2983 present in IN_TYPE. */
2984 warning ("cast discards qualifiers from pointer target type");
2987 /* Warn about possible alignment problems. */
2988 if (STRICT_ALIGNMENT
&& warn_cast_align
2989 && TREE_CODE (type
) == POINTER_TYPE
2990 && TREE_CODE (otype
) == POINTER_TYPE
2991 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
2992 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
2993 /* Don't warn about opaque types, where the actual alignment
2994 restriction is unknown. */
2995 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
2996 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
2997 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
2998 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
2999 warning ("cast increases required alignment of target type");
3001 if (TREE_CODE (type
) == INTEGER_TYPE
3002 && TREE_CODE (otype
) == POINTER_TYPE
3003 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3004 && !TREE_CONSTANT (value
))
3005 warning ("cast from pointer to integer of different size");
3007 if (warn_bad_function_cast
3008 && TREE_CODE (value
) == CALL_EXPR
3009 && TREE_CODE (type
) != TREE_CODE (otype
))
3010 warning ("cast does not match function type");
3012 if (TREE_CODE (type
) == POINTER_TYPE
3013 && TREE_CODE (otype
) == INTEGER_TYPE
3014 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3015 /* Don't warn about converting any constant. */
3016 && !TREE_CONSTANT (value
))
3017 warning ("cast to pointer from integer of different size");
3019 if (TREE_CODE (type
) == POINTER_TYPE
3020 && TREE_CODE (otype
) == POINTER_TYPE
3021 && TREE_CODE (expr
) == ADDR_EXPR
3022 && DECL_P (TREE_OPERAND (expr
, 0))
3023 && flag_strict_aliasing
&& warn_strict_aliasing
3024 && !VOID_TYPE_P (TREE_TYPE (type
)))
3026 /* Casting the address of a decl to non void pointer. Warn
3027 if the cast breaks type based aliasing. */
3028 if (!COMPLETE_TYPE_P (TREE_TYPE (type
)))
3029 warning ("type-punning to incomplete type might break strict-aliasing rules");
3030 else if (!alias_sets_conflict_p
3031 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr
, 0))),
3032 get_alias_set (TREE_TYPE (type
))))
3033 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3036 /* If pedantic, warn for conversions between function and object
3037 pointer types, except for converting a null pointer constant
3038 to function pointer type. */
3040 && TREE_CODE (type
) == POINTER_TYPE
3041 && TREE_CODE (otype
) == POINTER_TYPE
3042 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
3043 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
3044 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3047 && TREE_CODE (type
) == POINTER_TYPE
3048 && TREE_CODE (otype
) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
3050 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3051 && !(integer_zerop (value
) && TREE_TYPE (otype
) == void_type_node
3052 && TREE_CODE (expr
) != NOP_EXPR
))
3053 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3056 /* Replace a nonvolatile const static variable with its value. */
3057 if (optimize
&& TREE_CODE (value
) == VAR_DECL
)
3058 value
= decl_constant_value (value
);
3059 value
= convert (type
, value
);
3061 /* Ignore any integer overflow caused by the cast. */
3062 if (TREE_CODE (value
) == INTEGER_CST
)
3064 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
3065 TREE_CONSTANT_OVERFLOW (value
) = TREE_CONSTANT_OVERFLOW (ovalue
);
3069 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3070 if (TREE_CODE (value
) == INTEGER_CST
3071 && TREE_CODE (expr
) == INTEGER_CST
3072 && TREE_CODE (TREE_TYPE (expr
)) != INTEGER_TYPE
)
3073 value
= non_lvalue (value
);
3075 /* Don't let a cast be an lvalue. */
3077 value
= non_lvalue (value
);
3082 /* Interpret a cast of expression EXPR to type TYPE. */
3084 c_cast_expr (tree type
, tree expr
)
3086 int saved_wsp
= warn_strict_prototypes
;
3088 /* This avoids warnings about unprototyped casts on
3089 integers. E.g. "#define SIG_DFL (void(*)())0". */
3090 if (TREE_CODE (expr
) == INTEGER_CST
)
3091 warn_strict_prototypes
= 0;
3092 type
= groktypename (type
);
3093 warn_strict_prototypes
= saved_wsp
;
3095 return build_c_cast (type
, expr
);
3099 /* Build an assignment expression of lvalue LHS from value RHS.
3100 MODIFYCODE is the code for a binary operator that we use
3101 to combine the old value of LHS with RHS to get the new value.
3102 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3105 build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
)
3109 tree lhstype
= TREE_TYPE (lhs
);
3110 tree olhstype
= lhstype
;
3112 /* Types that aren't fully specified cannot be used in assignments. */
3113 lhs
= require_complete_type (lhs
);
3115 /* Avoid duplicate error messages from operands that had errors. */
3116 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
3117 return error_mark_node
;
3119 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3120 /* Do not use STRIP_NOPS here. We do not want an enumerator
3121 whose value is 0 to count as a null pointer constant. */
3122 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3123 rhs
= TREE_OPERAND (rhs
, 0);
3127 /* If a binary op has been requested, combine the old LHS value with the RHS
3128 producing the value we should actually store into the LHS. */
3130 if (modifycode
!= NOP_EXPR
)
3132 lhs
= stabilize_reference (lhs
);
3133 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
3136 if (!lvalue_or_else (lhs
, "invalid lvalue in assignment"))
3137 return error_mark_node
;
3139 /* Warn about storing in something that is `const'. */
3141 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
3142 || ((TREE_CODE (lhstype
) == RECORD_TYPE
3143 || TREE_CODE (lhstype
) == UNION_TYPE
)
3144 && C_TYPE_FIELDS_READONLY (lhstype
)))
3145 readonly_error (lhs
, "assignment");
3147 /* If storing into a structure or union member,
3148 it has probably been given type `int'.
3149 Compute the type that would go with
3150 the actual amount of storage the member occupies. */
3152 if (TREE_CODE (lhs
) == COMPONENT_REF
3153 && (TREE_CODE (lhstype
) == INTEGER_TYPE
3154 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
3155 || TREE_CODE (lhstype
) == REAL_TYPE
3156 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
3157 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
3159 /* If storing in a field that is in actuality a short or narrower than one,
3160 we must store in the field in its actual type. */
3162 if (lhstype
!= TREE_TYPE (lhs
))
3164 lhs
= copy_node (lhs
);
3165 TREE_TYPE (lhs
) = lhstype
;
3168 /* Convert new value to destination type. */
3170 newrhs
= convert_for_assignment (lhstype
, newrhs
, _("assignment"),
3171 NULL_TREE
, NULL_TREE
, 0);
3172 if (TREE_CODE (newrhs
) == ERROR_MARK
)
3173 return error_mark_node
;
3177 result
= build (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
3178 TREE_SIDE_EFFECTS (result
) = 1;
3180 /* If we got the LHS in a different type for storing in,
3181 convert the result back to the nominal type of LHS
3182 so that the value we return always has the same type
3183 as the LHS argument. */
3185 if (olhstype
== TREE_TYPE (result
))
3187 return convert_for_assignment (olhstype
, result
, _("assignment"),
3188 NULL_TREE
, NULL_TREE
, 0);
3191 /* Convert value RHS to type TYPE as preparation for an assignment
3192 to an lvalue of type TYPE.
3193 The real work of conversion is done by `convert'.
3194 The purpose of this function is to generate error messages
3195 for assignments that are not allowed in C.
3196 ERRTYPE is a string to use in error messages:
3197 "assignment", "return", etc. If it is null, this is parameter passing
3198 for a function call (and different error messages are output).
3200 FUNNAME is the name of the function being called,
3201 as an IDENTIFIER_NODE, or null.
3202 PARMNUM is the number of the argument, for printing in error messages. */
3205 convert_for_assignment (tree type
, tree rhs
, const char *errtype
,
3206 tree fundecl
, tree funname
, int parmnum
)
3208 enum tree_code codel
= TREE_CODE (type
);
3210 enum tree_code coder
;
3212 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3213 /* Do not use STRIP_NOPS here. We do not want an enumerator
3214 whose value is 0 to count as a null pointer constant. */
3215 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3216 rhs
= TREE_OPERAND (rhs
, 0);
3218 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
3219 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
)
3220 rhs
= default_conversion (rhs
);
3221 else if (optimize
&& TREE_CODE (rhs
) == VAR_DECL
)
3222 rhs
= decl_constant_value_for_broken_optimization (rhs
);
3224 rhstype
= TREE_TYPE (rhs
);
3225 coder
= TREE_CODE (rhstype
);
3227 if (coder
== ERROR_MARK
)
3228 return error_mark_node
;
3230 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
3232 overflow_warning (rhs
);
3233 /* Check for Objective-C protocols. This will automatically
3234 issue a warning if there are protocol violations. No need to
3235 use the return value. */
3236 if (c_dialect_objc ())
3237 objc_comptypes (type
, rhstype
, 0);
3241 if (coder
== VOID_TYPE
)
3243 error ("void value not ignored as it ought to be");
3244 return error_mark_node
;
3246 /* A type converts to a reference to it.
3247 This code doesn't fully support references, it's just for the
3248 special case of va_start and va_copy. */
3249 if (codel
== REFERENCE_TYPE
3250 && comptypes (TREE_TYPE (type
), TREE_TYPE (rhs
), COMPARE_STRICT
) == 1)
3252 if (!lvalue_p (rhs
))
3254 error ("cannot pass rvalue to reference parameter");
3255 return error_mark_node
;
3257 if (!c_mark_addressable (rhs
))
3258 return error_mark_node
;
3259 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
3261 /* We already know that these two types are compatible, but they
3262 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3263 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3264 likely to be va_list, a typedef to __builtin_va_list, which
3265 is different enough that it will cause problems later. */
3266 if (TREE_TYPE (TREE_TYPE (rhs
)) != TREE_TYPE (type
))
3267 rhs
= build1 (NOP_EXPR
, build_pointer_type (TREE_TYPE (type
)), rhs
);
3269 rhs
= build1 (NOP_EXPR
, type
, rhs
);
3272 /* Some types can interconvert without explicit casts. */
3273 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
3274 && ((*targetm
.vector_opaque_p
) (type
)
3275 || (*targetm
.vector_opaque_p
) (rhstype
)))
3276 return convert (type
, rhs
);
3277 /* Arithmetic types all interconvert, and enum is treated like int. */
3278 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
3279 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
3280 || codel
== BOOLEAN_TYPE
)
3281 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
3282 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
3283 || coder
== BOOLEAN_TYPE
))
3284 return convert_and_check (type
, rhs
);
3286 /* Conversion to a transparent union from its member types.
3287 This applies only to function arguments. */
3288 else if (codel
== UNION_TYPE
&& TYPE_TRANSPARENT_UNION (type
) && ! errtype
)
3291 tree marginal_memb_type
= 0;
3293 for (memb_types
= TYPE_FIELDS (type
); memb_types
;
3294 memb_types
= TREE_CHAIN (memb_types
))
3296 tree memb_type
= TREE_TYPE (memb_types
);
3298 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
3299 TYPE_MAIN_VARIANT (rhstype
), COMPARE_STRICT
))
3302 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
3305 if (coder
== POINTER_TYPE
)
3307 tree ttl
= TREE_TYPE (memb_type
);
3308 tree ttr
= TREE_TYPE (rhstype
);
3310 /* Any non-function converts to a [const][volatile] void *
3311 and vice versa; otherwise, targets must be the same.
3312 Meanwhile, the lhs target must have all the qualifiers of
3314 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3315 || comp_target_types (memb_type
, rhstype
, 0))
3317 /* If this type won't generate any warnings, use it. */
3318 if (TYPE_QUALS (ttl
) == TYPE_QUALS (ttr
)
3319 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
3320 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3321 ? ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3322 == TYPE_QUALS (ttr
))
3323 : ((TYPE_QUALS (ttl
) | TYPE_QUALS (ttr
))
3324 == TYPE_QUALS (ttl
))))
3327 /* Keep looking for a better type, but remember this one. */
3328 if (! marginal_memb_type
)
3329 marginal_memb_type
= memb_type
;
3333 /* Can convert integer zero to any pointer type. */
3334 if (integer_zerop (rhs
)
3335 || (TREE_CODE (rhs
) == NOP_EXPR
3336 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3338 rhs
= null_pointer_node
;
3343 if (memb_types
|| marginal_memb_type
)
3347 /* We have only a marginally acceptable member type;
3348 it needs a warning. */
3349 tree ttl
= TREE_TYPE (marginal_memb_type
);
3350 tree ttr
= TREE_TYPE (rhstype
);
3352 /* Const and volatile mean something different for function
3353 types, so the usual warnings are not appropriate. */
3354 if (TREE_CODE (ttr
) == FUNCTION_TYPE
3355 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
3357 /* Because const and volatile on functions are
3358 restrictions that say the function will not do
3359 certain things, it is okay to use a const or volatile
3360 function where an ordinary one is wanted, but not
3362 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3363 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3364 errtype
, funname
, parmnum
);
3366 else if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3367 warn_for_assignment ("%s discards qualifiers from pointer target type",
3372 if (pedantic
&& ! DECL_IN_SYSTEM_HEADER (fundecl
))
3373 pedwarn ("ISO C prohibits argument conversion to union type");
3375 return build1 (NOP_EXPR
, type
, rhs
);
3379 /* Conversions among pointers */
3380 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
3381 && (coder
== codel
))
3383 tree ttl
= TREE_TYPE (type
);
3384 tree ttr
= TREE_TYPE (rhstype
);
3385 bool is_opaque_pointer
;
3386 int target_cmp
= 0; /* Cache comp_target_types () result. */
3388 /* Opaque pointers are treated like void pointers. */
3389 is_opaque_pointer
= ((*targetm
.vector_opaque_p
) (type
)
3390 || (*targetm
.vector_opaque_p
) (rhstype
))
3391 && TREE_CODE (ttl
) == VECTOR_TYPE
3392 && TREE_CODE (ttr
) == VECTOR_TYPE
;
3394 /* Any non-function converts to a [const][volatile] void *
3395 and vice versa; otherwise, targets must be the same.
3396 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3397 if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3398 || (target_cmp
= comp_target_types (type
, rhstype
, 0))
3399 || is_opaque_pointer
3400 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl
))
3401 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr
))))
3404 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3407 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3408 which are not ANSI null ptr constants. */
3409 && (!integer_zerop (rhs
) || TREE_CODE (rhs
) == NOP_EXPR
)
3410 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
3411 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3412 errtype
, funname
, parmnum
);
3413 /* Const and volatile mean something different for function types,
3414 so the usual warnings are not appropriate. */
3415 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
3416 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
3418 if (TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
))
3419 warn_for_assignment ("%s discards qualifiers from pointer target type",
3420 errtype
, funname
, parmnum
);
3421 /* If this is not a case of ignoring a mismatch in signedness,
3423 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
3426 /* If there is a mismatch, do warn. */
3428 warn_for_assignment ("pointer targets in %s differ in signedness",
3429 errtype
, funname
, parmnum
);
3431 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
3432 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3434 /* Because const and volatile on functions are restrictions
3435 that say the function will not do certain things,
3436 it is okay to use a const or volatile function
3437 where an ordinary one is wanted, but not vice-versa. */
3438 if (TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
))
3439 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3440 errtype
, funname
, parmnum
);
3444 warn_for_assignment ("%s from incompatible pointer type",
3445 errtype
, funname
, parmnum
);
3446 return convert (type
, rhs
);
3448 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
3450 error ("invalid use of non-lvalue array");
3451 return error_mark_node
;
3453 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
3455 /* An explicit constant 0 can convert to a pointer,
3456 or one that results from arithmetic, even including
3457 a cast to integer type. */
3458 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
))
3460 ! (TREE_CODE (rhs
) == NOP_EXPR
3461 && TREE_CODE (TREE_TYPE (rhs
)) == INTEGER_TYPE
3462 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == INTEGER_CST
3463 && integer_zerop (TREE_OPERAND (rhs
, 0))))
3464 warn_for_assignment ("%s makes pointer from integer without a cast",
3465 errtype
, funname
, parmnum
);
3467 return convert (type
, rhs
);
3469 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
3471 warn_for_assignment ("%s makes integer from pointer without a cast",
3472 errtype
, funname
, parmnum
);
3473 return convert (type
, rhs
);
3475 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
3476 return convert (type
, rhs
);
3482 tree selector
= objc_message_selector ();
3484 if (selector
&& parmnum
> 2)
3485 error ("incompatible type for argument %d of `%s'",
3486 parmnum
- 2, IDENTIFIER_POINTER (selector
));
3488 error ("incompatible type for argument %d of `%s'",
3489 parmnum
, IDENTIFIER_POINTER (funname
));
3492 error ("incompatible type for argument %d of indirect function call",
3496 error ("incompatible types in %s", errtype
);
3498 return error_mark_node
;
3501 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3502 is used for error and waring reporting and indicates which argument
3503 is being processed. */
3506 c_convert_parm_for_inlining (tree parm
, tree value
, tree fn
, int argnum
)
3510 /* If FN was prototyped, the value has been converted already
3511 in convert_arguments. */
3512 if (! value
|| TYPE_ARG_TYPES (TREE_TYPE (fn
)))
3515 type
= TREE_TYPE (parm
);
3516 ret
= convert_for_assignment (type
, value
,
3517 (char *) 0 /* arg passing */, fn
,
3518 DECL_NAME (fn
), argnum
);
3519 if (targetm
.calls
.promote_prototypes (TREE_TYPE (fn
))
3520 && INTEGRAL_TYPE_P (type
)
3521 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3522 ret
= default_conversion (ret
);
3526 /* Print a warning using MSGID.
3527 It gets OPNAME as its one parameter.
3528 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3529 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3530 FUNCTION and ARGNUM are handled specially if we are building an
3531 Objective-C selector. */
3534 warn_for_assignment (const char *msgid
, const char *opname
, tree function
,
3539 tree selector
= objc_message_selector ();
3542 if (selector
&& argnum
> 2)
3544 function
= selector
;
3551 /* Function name is known; supply it. */
3552 const char *const argstring
= _("passing arg of `%s'");
3553 new_opname
= alloca (IDENTIFIER_LENGTH (function
)
3554 + strlen (argstring
) + 1 + 1);
3555 sprintf (new_opname
, argstring
,
3556 IDENTIFIER_POINTER (function
));
3560 /* Function name unknown (call through ptr). */
3561 const char *const argnofun
= _("passing arg of pointer to function");
3562 new_opname
= alloca (strlen (argnofun
) + 1 + 1);
3563 sprintf (new_opname
, argnofun
);
3568 /* Function name is known; supply it. */
3569 const char *const argstring
= _("passing arg %d of `%s'");
3570 new_opname
= alloca (IDENTIFIER_LENGTH (function
)
3571 + strlen (argstring
) + 1 + 25 /*%d*/ + 1);
3572 sprintf (new_opname
, argstring
, argnum
,
3573 IDENTIFIER_POINTER (function
));
3577 /* Function name unknown (call through ptr); just give arg number. */
3578 const char *const argnofun
= _("passing arg %d of pointer to function");
3579 new_opname
= alloca (strlen (argnofun
) + 1 + 25 /*%d*/ + 1);
3580 sprintf (new_opname
, argnofun
, argnum
);
3582 opname
= new_opname
;
3584 pedwarn (msgid
, opname
);
3587 /* If VALUE is a compound expr all of whose expressions are constant, then
3588 return its value. Otherwise, return error_mark_node.
3590 This is for handling COMPOUND_EXPRs as initializer elements
3591 which is allowed with a warning when -pedantic is specified. */
3594 valid_compound_expr_initializer (tree value
, tree endtype
)
3596 if (TREE_CODE (value
) == COMPOUND_EXPR
)
3598 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
3600 return error_mark_node
;
3601 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
3604 else if (! TREE_CONSTANT (value
)
3605 && ! initializer_constant_valid_p (value
, endtype
))
3606 return error_mark_node
;
3611 /* Perform appropriate conversions on the initial value of a variable,
3612 store it in the declaration DECL,
3613 and print any error messages that are appropriate.
3614 If the init is invalid, store an ERROR_MARK. */
3617 store_init_value (tree decl
, tree init
)
3621 /* If variable's type was invalidly declared, just ignore it. */
3623 type
= TREE_TYPE (decl
);
3624 if (TREE_CODE (type
) == ERROR_MARK
)
3627 /* Digest the specified initializer into an expression. */
3629 value
= digest_init (type
, init
, TREE_STATIC (decl
));
3631 /* Store the expression if valid; else report error. */
3633 if (warn_traditional
&& !in_system_header
3634 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && ! TREE_STATIC (decl
))
3635 warning ("traditional C rejects automatic aggregate initialization");
3637 DECL_INITIAL (decl
) = value
;
3639 /* ANSI wants warnings about out-of-range constant initializers. */
3640 STRIP_TYPE_NOPS (value
);
3641 constant_expression_warning (value
);
3643 /* Check if we need to set array size from compound literal size. */
3644 if (TREE_CODE (type
) == ARRAY_TYPE
3645 && TYPE_DOMAIN (type
) == 0
3646 && value
!= error_mark_node
)
3648 tree inside_init
= init
;
3650 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
3651 inside_init
= TREE_OPERAND (init
, 0);
3652 inside_init
= fold (inside_init
);
3654 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
3656 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
3658 if (TYPE_DOMAIN (TREE_TYPE (decl
)))
3660 /* For int foo[] = (int [3]){1}; we need to set array size
3661 now since later on array initializer will be just the
3662 brace enclosed list of the compound literal. */
3663 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (decl
));
3665 layout_decl (decl
, 0);
3671 /* Methods for storing and printing names for error messages. */
3673 /* Implement a spelling stack that allows components of a name to be pushed
3674 and popped. Each element on the stack is this structure. */
3686 #define SPELLING_STRING 1
3687 #define SPELLING_MEMBER 2
3688 #define SPELLING_BOUNDS 3
3690 static struct spelling
*spelling
; /* Next stack element (unused). */
3691 static struct spelling
*spelling_base
; /* Spelling stack base. */
3692 static int spelling_size
; /* Size of the spelling stack. */
3694 /* Macros to save and restore the spelling stack around push_... functions.
3695 Alternative to SAVE_SPELLING_STACK. */
3697 #define SPELLING_DEPTH() (spelling - spelling_base)
3698 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3700 /* Push an element on the spelling stack with type KIND and assign VALUE
3703 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3705 int depth = SPELLING_DEPTH (); \
3707 if (depth >= spelling_size) \
3709 spelling_size += 10; \
3710 if (spelling_base == 0) \
3711 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3713 spelling_base = xrealloc (spelling_base, \
3714 spelling_size * sizeof (struct spelling)); \
3715 RESTORE_SPELLING_DEPTH (depth); \
3718 spelling->kind = (KIND); \
3719 spelling->MEMBER = (VALUE); \
3723 /* Push STRING on the stack. Printed literally. */
3726 push_string (const char *string
)
3728 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
3731 /* Push a member name on the stack. Printed as '.' STRING. */
3734 push_member_name (tree decl
)
3736 const char *const string
3737 = DECL_NAME (decl
) ? IDENTIFIER_POINTER (DECL_NAME (decl
)) : "<anonymous>";
3738 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
3741 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3744 push_array_bounds (int bounds
)
3746 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
3749 /* Compute the maximum size in bytes of the printed spelling. */
3752 spelling_length (void)
3757 for (p
= spelling_base
; p
< spelling
; p
++)
3759 if (p
->kind
== SPELLING_BOUNDS
)
3762 size
+= strlen (p
->u
.s
) + 1;
3768 /* Print the spelling to BUFFER and return it. */
3771 print_spelling (char *buffer
)
3776 for (p
= spelling_base
; p
< spelling
; p
++)
3777 if (p
->kind
== SPELLING_BOUNDS
)
3779 sprintf (d
, "[%d]", p
->u
.i
);
3785 if (p
->kind
== SPELLING_MEMBER
)
3787 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
3794 /* Issue an error message for a bad initializer component.
3795 MSGID identifies the message.
3796 The component name is taken from the spelling stack. */
3799 error_init (const char *msgid
)
3803 error ("%s", _(msgid
));
3804 ofwhat
= print_spelling (alloca (spelling_length () + 1));
3806 error ("(near initialization for `%s')", ofwhat
);
3809 /* Issue a pedantic warning for a bad initializer component.
3810 MSGID identifies the message.
3811 The component name is taken from the spelling stack. */
3814 pedwarn_init (const char *msgid
)
3818 pedwarn ("%s", _(msgid
));
3819 ofwhat
= print_spelling (alloca (spelling_length () + 1));
3821 pedwarn ("(near initialization for `%s')", ofwhat
);
3824 /* Issue a warning for a bad initializer component.
3825 MSGID identifies the message.
3826 The component name is taken from the spelling stack. */
3829 warning_init (const char *msgid
)
3833 warning ("%s", _(msgid
));
3834 ofwhat
= print_spelling (alloca (spelling_length () + 1));
3836 warning ("(near initialization for `%s')", ofwhat
);
3839 /* Digest the parser output INIT as an initializer for type TYPE.
3840 Return a C expression of type TYPE to represent the initial value.
3842 REQUIRE_CONSTANT requests an error if non-constant initializers or
3843 elements are seen. */
3846 digest_init (tree type
, tree init
, int require_constant
)
3848 enum tree_code code
= TREE_CODE (type
);
3849 tree inside_init
= init
;
3851 if (type
== error_mark_node
3852 || init
== error_mark_node
3853 || TREE_TYPE (init
) == error_mark_node
)
3854 return error_mark_node
;
3856 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3857 /* Do not use STRIP_NOPS here. We do not want an enumerator
3858 whose value is 0 to count as a null pointer constant. */
3859 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
3860 inside_init
= TREE_OPERAND (init
, 0);
3862 inside_init
= fold (inside_init
);
3864 /* Initialization of an array of chars from a string constant
3865 optionally enclosed in braces. */
3867 if (code
== ARRAY_TYPE
)
3869 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
3870 if ((typ1
== char_type_node
3871 || typ1
== signed_char_type_node
3872 || typ1
== unsigned_char_type_node
3873 || typ1
== unsigned_wchar_type_node
3874 || typ1
== signed_wchar_type_node
)
3875 && ((inside_init
&& TREE_CODE (inside_init
) == STRING_CST
)))
3877 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
3878 TYPE_MAIN_VARIANT (type
), COMPARE_STRICT
))
3881 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)))
3883 && TYPE_PRECISION (typ1
) == TYPE_PRECISION (char_type_node
))
3885 error_init ("char-array initialized from wide string");
3886 return error_mark_node
;
3888 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)))
3890 && TYPE_PRECISION (typ1
) != TYPE_PRECISION (char_type_node
))
3892 error_init ("int-array initialized from non-wide string");
3893 return error_mark_node
;
3896 TREE_TYPE (inside_init
) = type
;
3897 if (TYPE_DOMAIN (type
) != 0
3898 && TYPE_SIZE (type
) != 0
3899 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
3900 /* Subtract 1 (or sizeof (wchar_t))
3901 because it's ok to ignore the terminating null char
3902 that is counted in the length of the constant. */
3903 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
3904 TREE_STRING_LENGTH (inside_init
)
3905 - ((TYPE_PRECISION (typ1
)
3906 != TYPE_PRECISION (char_type_node
))
3907 ? (TYPE_PRECISION (wchar_type_node
)
3910 pedwarn_init ("initializer-string for array of chars is too long");
3916 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3917 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3918 below and handle as a constructor. */
3919 if (code
== VECTOR_TYPE
3920 && comptypes (TREE_TYPE (inside_init
), type
, COMPARE_STRICT
)
3921 && TREE_CONSTANT (inside_init
))
3923 if (TREE_CODE (inside_init
) == VECTOR_CST
3924 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
3925 TYPE_MAIN_VARIANT (type
),
3929 return build_vector (type
, CONSTRUCTOR_ELTS (inside_init
));
3932 /* Any type can be initialized
3933 from an expression of the same type, optionally with braces. */
3935 if (inside_init
&& TREE_TYPE (inside_init
) != 0
3936 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
3937 TYPE_MAIN_VARIANT (type
), COMPARE_STRICT
)
3938 || (code
== ARRAY_TYPE
3939 && comptypes (TREE_TYPE (inside_init
), type
, COMPARE_STRICT
))
3940 || (code
== VECTOR_TYPE
3941 && comptypes (TREE_TYPE (inside_init
), type
, COMPARE_STRICT
))
3942 || (code
== POINTER_TYPE
3943 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
3944 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
3945 TREE_TYPE (type
), COMPARE_STRICT
))
3946 || (code
== POINTER_TYPE
3947 && TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
3948 && comptypes (TREE_TYPE (inside_init
),
3949 TREE_TYPE (type
), COMPARE_STRICT
))))
3951 if (code
== POINTER_TYPE
)
3953 inside_init
= default_function_array_conversion (inside_init
);
3955 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
3957 error_init ("invalid use of non-lvalue array");
3958 return error_mark_node
;
3962 if (code
== VECTOR_TYPE
)
3963 /* Although the types are compatible, we may require a
3965 inside_init
= convert (type
, inside_init
);
3967 if (require_constant
&& !flag_isoc99
3968 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
3970 /* As an extension, allow initializing objects with static storage
3971 duration with compound literals (which are then treated just as
3972 the brace enclosed list they contain). */
3973 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
3974 inside_init
= DECL_INITIAL (decl
);
3977 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
3978 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
3980 error_init ("array initialized from non-constant array expression");
3981 return error_mark_node
;
3984 if (optimize
&& TREE_CODE (inside_init
) == VAR_DECL
)
3985 inside_init
= decl_constant_value_for_broken_optimization (inside_init
);
3987 /* Compound expressions can only occur here if -pedantic or
3988 -pedantic-errors is specified. In the later case, we always want
3989 an error. In the former case, we simply want a warning. */
3990 if (require_constant
&& pedantic
3991 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
3994 = valid_compound_expr_initializer (inside_init
,
3995 TREE_TYPE (inside_init
));
3996 if (inside_init
== error_mark_node
)
3997 error_init ("initializer element is not constant");
3999 pedwarn_init ("initializer element is not constant");
4000 if (flag_pedantic_errors
)
4001 inside_init
= error_mark_node
;
4003 else if (require_constant
4004 && (!TREE_CONSTANT (inside_init
)
4005 /* This test catches things like `7 / 0' which
4006 result in an expression for which TREE_CONSTANT
4007 is true, but which is not actually something
4008 that is a legal constant. We really should not
4009 be using this function, because it is a part of
4010 the back-end. Instead, the expression should
4011 already have been turned into ERROR_MARK_NODE. */
4012 || !initializer_constant_valid_p (inside_init
,
4013 TREE_TYPE (inside_init
))))
4015 error_init ("initializer element is not constant");
4016 inside_init
= error_mark_node
;
4022 /* Handle scalar types, including conversions. */
4024 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
4025 || code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
|| code
== COMPLEX_TYPE
)
4027 /* Note that convert_for_assignment calls default_conversion
4028 for arrays and functions. We must not call it in the
4029 case where inside_init is a null pointer constant. */
4031 = convert_for_assignment (type
, init
, _("initialization"),
4032 NULL_TREE
, NULL_TREE
, 0);
4034 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4036 error_init ("initializer element is not constant");
4037 inside_init
= error_mark_node
;
4039 else if (require_constant
4040 && initializer_constant_valid_p (inside_init
, TREE_TYPE (inside_init
)) == 0)
4042 error_init ("initializer element is not computable at load time");
4043 inside_init
= error_mark_node
;
4049 /* Come here only for records and arrays. */
4051 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
4053 error_init ("variable-sized object may not be initialized");
4054 return error_mark_node
;
4057 error_init ("invalid initializer");
4058 return error_mark_node
;
4061 /* Handle initializers that use braces. */
4063 /* Type of object we are accumulating a constructor for.
4064 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4065 static tree constructor_type
;
4067 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4069 static tree constructor_fields
;
4071 /* For an ARRAY_TYPE, this is the specified index
4072 at which to store the next element we get. */
4073 static tree constructor_index
;
4075 /* For an ARRAY_TYPE, this is the maximum index. */
4076 static tree constructor_max_index
;
4078 /* For a RECORD_TYPE, this is the first field not yet written out. */
4079 static tree constructor_unfilled_fields
;
4081 /* For an ARRAY_TYPE, this is the index of the first element
4082 not yet written out. */
4083 static tree constructor_unfilled_index
;
4085 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4086 This is so we can generate gaps between fields, when appropriate. */
4087 static tree constructor_bit_index
;
4089 /* If we are saving up the elements rather than allocating them,
4090 this is the list of elements so far (in reverse order,
4091 most recent first). */
4092 static tree constructor_elements
;
4094 /* 1 if constructor should be incrementally stored into a constructor chain,
4095 0 if all the elements should be kept in AVL tree. */
4096 static int constructor_incremental
;
4098 /* 1 if so far this constructor's elements are all compile-time constants. */
4099 static int constructor_constant
;
4101 /* 1 if so far this constructor's elements are all valid address constants. */
4102 static int constructor_simple
;
4104 /* 1 if this constructor is erroneous so far. */
4105 static int constructor_erroneous
;
4107 /* Structure for managing pending initializer elements, organized as an
4112 struct init_node
*left
, *right
;
4113 struct init_node
*parent
;
4119 /* Tree of pending elements at this constructor level.
4120 These are elements encountered out of order
4121 which belong at places we haven't reached yet in actually
4123 Will never hold tree nodes across GC runs. */
4124 static struct init_node
*constructor_pending_elts
;
4126 /* The SPELLING_DEPTH of this constructor. */
4127 static int constructor_depth
;
4129 /* 0 if implicitly pushing constructor levels is allowed. */
4130 int constructor_no_implicit
= 0; /* 0 for C; 1 for some other languages. */
4132 static int require_constant_value
;
4133 static int require_constant_elements
;
4135 /* DECL node for which an initializer is being read.
4136 0 means we are reading a constructor expression
4137 such as (struct foo) {...}. */
4138 static tree constructor_decl
;
4140 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4141 static const char *constructor_asmspec
;
4143 /* Nonzero if this is an initializer for a top-level decl. */
4144 static int constructor_top_level
;
4146 /* Nonzero if there were any member designators in this initializer. */
4147 static int constructor_designated
;
4149 /* Nesting depth of designator list. */
4150 static int designator_depth
;
4152 /* Nonzero if there were diagnosed errors in this designator list. */
4153 static int designator_errorneous
;
4156 /* This stack has a level for each implicit or explicit level of
4157 structuring in the initializer, including the outermost one. It
4158 saves the values of most of the variables above. */
4160 struct constructor_range_stack
;
4162 struct constructor_stack
4164 struct constructor_stack
*next
;
4169 tree unfilled_index
;
4170 tree unfilled_fields
;
4173 struct init_node
*pending_elts
;
4176 /* If nonzero, this value should replace the entire
4177 constructor at this level. */
4178 tree replacement_value
;
4179 struct constructor_range_stack
*range_stack
;
4189 struct constructor_stack
*constructor_stack
;
4191 /* This stack represents designators from some range designator up to
4192 the last designator in the list. */
4194 struct constructor_range_stack
4196 struct constructor_range_stack
*next
, *prev
;
4197 struct constructor_stack
*stack
;
4204 struct constructor_range_stack
*constructor_range_stack
;
4206 /* This stack records separate initializers that are nested.
4207 Nested initializers can't happen in ANSI C, but GNU C allows them
4208 in cases like { ... (struct foo) { ... } ... }. */
4210 struct initializer_stack
4212 struct initializer_stack
*next
;
4214 const char *asmspec
;
4215 struct constructor_stack
*constructor_stack
;
4216 struct constructor_range_stack
*constructor_range_stack
;
4218 struct spelling
*spelling
;
4219 struct spelling
*spelling_base
;
4222 char require_constant_value
;
4223 char require_constant_elements
;
4226 struct initializer_stack
*initializer_stack
;
4228 /* Prepare to parse and output the initializer for variable DECL. */
4231 start_init (tree decl
, tree asmspec_tree
, int top_level
)
4234 struct initializer_stack
*p
= xmalloc (sizeof (struct initializer_stack
));
4235 const char *asmspec
= 0;
4238 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
4240 p
->decl
= constructor_decl
;
4241 p
->asmspec
= constructor_asmspec
;
4242 p
->require_constant_value
= require_constant_value
;
4243 p
->require_constant_elements
= require_constant_elements
;
4244 p
->constructor_stack
= constructor_stack
;
4245 p
->constructor_range_stack
= constructor_range_stack
;
4246 p
->elements
= constructor_elements
;
4247 p
->spelling
= spelling
;
4248 p
->spelling_base
= spelling_base
;
4249 p
->spelling_size
= spelling_size
;
4250 p
->top_level
= constructor_top_level
;
4251 p
->next
= initializer_stack
;
4252 initializer_stack
= p
;
4254 constructor_decl
= decl
;
4255 constructor_asmspec
= asmspec
;
4256 constructor_designated
= 0;
4257 constructor_top_level
= top_level
;
4261 require_constant_value
= TREE_STATIC (decl
);
4262 require_constant_elements
4263 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
4264 /* For a scalar, you can always use any value to initialize,
4265 even within braces. */
4266 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
4267 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
4268 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
4269 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
4270 locus
= IDENTIFIER_POINTER (DECL_NAME (decl
));
4274 require_constant_value
= 0;
4275 require_constant_elements
= 0;
4276 locus
= "(anonymous)";
4279 constructor_stack
= 0;
4280 constructor_range_stack
= 0;
4282 missing_braces_mentioned
= 0;
4286 RESTORE_SPELLING_DEPTH (0);
4289 push_string (locus
);
4295 struct initializer_stack
*p
= initializer_stack
;
4297 /* Free the whole constructor stack of this initializer. */
4298 while (constructor_stack
)
4300 struct constructor_stack
*q
= constructor_stack
;
4301 constructor_stack
= q
->next
;
4305 if (constructor_range_stack
)
4308 /* Pop back to the data of the outer initializer (if any). */
4309 free (spelling_base
);
4311 constructor_decl
= p
->decl
;
4312 constructor_asmspec
= p
->asmspec
;
4313 require_constant_value
= p
->require_constant_value
;
4314 require_constant_elements
= p
->require_constant_elements
;
4315 constructor_stack
= p
->constructor_stack
;
4316 constructor_range_stack
= p
->constructor_range_stack
;
4317 constructor_elements
= p
->elements
;
4318 spelling
= p
->spelling
;
4319 spelling_base
= p
->spelling_base
;
4320 spelling_size
= p
->spelling_size
;
4321 constructor_top_level
= p
->top_level
;
4322 initializer_stack
= p
->next
;
4326 /* Call here when we see the initializer is surrounded by braces.
4327 This is instead of a call to push_init_level;
4328 it is matched by a call to pop_init_level.
4330 TYPE is the type to initialize, for a constructor expression.
4331 For an initializer for a decl, TYPE is zero. */
4334 really_start_incremental_init (tree type
)
4336 struct constructor_stack
*p
= xmalloc (sizeof (struct constructor_stack
));
4339 type
= TREE_TYPE (constructor_decl
);
4341 if ((*targetm
.vector_opaque_p
) (type
))
4342 error ("opaque vector types cannot be initialized");
4344 p
->type
= constructor_type
;
4345 p
->fields
= constructor_fields
;
4346 p
->index
= constructor_index
;
4347 p
->max_index
= constructor_max_index
;
4348 p
->unfilled_index
= constructor_unfilled_index
;
4349 p
->unfilled_fields
= constructor_unfilled_fields
;
4350 p
->bit_index
= constructor_bit_index
;
4351 p
->elements
= constructor_elements
;
4352 p
->constant
= constructor_constant
;
4353 p
->simple
= constructor_simple
;
4354 p
->erroneous
= constructor_erroneous
;
4355 p
->pending_elts
= constructor_pending_elts
;
4356 p
->depth
= constructor_depth
;
4357 p
->replacement_value
= 0;
4361 p
->incremental
= constructor_incremental
;
4362 p
->designated
= constructor_designated
;
4364 constructor_stack
= p
;
4366 constructor_constant
= 1;
4367 constructor_simple
= 1;
4368 constructor_depth
= SPELLING_DEPTH ();
4369 constructor_elements
= 0;
4370 constructor_pending_elts
= 0;
4371 constructor_type
= type
;
4372 constructor_incremental
= 1;
4373 constructor_designated
= 0;
4374 designator_depth
= 0;
4375 designator_errorneous
= 0;
4377 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4378 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4380 constructor_fields
= TYPE_FIELDS (constructor_type
);
4381 /* Skip any nameless bit fields at the beginning. */
4382 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4383 && DECL_NAME (constructor_fields
) == 0)
4384 constructor_fields
= TREE_CHAIN (constructor_fields
);
4386 constructor_unfilled_fields
= constructor_fields
;
4387 constructor_bit_index
= bitsize_zero_node
;
4389 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4391 if (TYPE_DOMAIN (constructor_type
))
4393 constructor_max_index
4394 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4396 /* Detect non-empty initializations of zero-length arrays. */
4397 if (constructor_max_index
== NULL_TREE
4398 && TYPE_SIZE (constructor_type
))
4399 constructor_max_index
= build_int_2 (-1, -1);
4401 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4402 to initialize VLAs will cause a proper error; avoid tree
4403 checking errors as well by setting a safe value. */
4404 if (constructor_max_index
4405 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4406 constructor_max_index
= build_int_2 (-1, -1);
4409 = convert (bitsizetype
,
4410 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4413 constructor_index
= bitsize_zero_node
;
4415 constructor_unfilled_index
= constructor_index
;
4417 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4419 /* Vectors are like simple fixed-size arrays. */
4420 constructor_max_index
=
4421 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1, 0);
4422 constructor_index
= convert (bitsizetype
, bitsize_zero_node
);
4423 constructor_unfilled_index
= constructor_index
;
4427 /* Handle the case of int x = {5}; */
4428 constructor_fields
= constructor_type
;
4429 constructor_unfilled_fields
= constructor_type
;
4433 /* Push down into a subobject, for initialization.
4434 If this is for an explicit set of braces, IMPLICIT is 0.
4435 If it is because the next element belongs at a lower level,
4436 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4439 push_init_level (int implicit
)
4441 struct constructor_stack
*p
;
4442 tree value
= NULL_TREE
;
4444 /* If we've exhausted any levels that didn't have braces,
4446 while (constructor_stack
->implicit
)
4448 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4449 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4450 && constructor_fields
== 0)
4451 process_init_element (pop_init_level (1));
4452 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
4453 && constructor_max_index
4454 && tree_int_cst_lt (constructor_max_index
, constructor_index
))
4455 process_init_element (pop_init_level (1));
4460 /* Unless this is an explicit brace, we need to preserve previous
4464 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
4465 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4466 && constructor_fields
)
4467 value
= find_init_member (constructor_fields
);
4468 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4469 value
= find_init_member (constructor_index
);
4472 p
= xmalloc (sizeof (struct constructor_stack
));
4473 p
->type
= constructor_type
;
4474 p
->fields
= constructor_fields
;
4475 p
->index
= constructor_index
;
4476 p
->max_index
= constructor_max_index
;
4477 p
->unfilled_index
= constructor_unfilled_index
;
4478 p
->unfilled_fields
= constructor_unfilled_fields
;
4479 p
->bit_index
= constructor_bit_index
;
4480 p
->elements
= constructor_elements
;
4481 p
->constant
= constructor_constant
;
4482 p
->simple
= constructor_simple
;
4483 p
->erroneous
= constructor_erroneous
;
4484 p
->pending_elts
= constructor_pending_elts
;
4485 p
->depth
= constructor_depth
;
4486 p
->replacement_value
= 0;
4487 p
->implicit
= implicit
;
4489 p
->incremental
= constructor_incremental
;
4490 p
->designated
= constructor_designated
;
4491 p
->next
= constructor_stack
;
4493 constructor_stack
= p
;
4495 constructor_constant
= 1;
4496 constructor_simple
= 1;
4497 constructor_depth
= SPELLING_DEPTH ();
4498 constructor_elements
= 0;
4499 constructor_incremental
= 1;
4500 constructor_designated
= 0;
4501 constructor_pending_elts
= 0;
4504 p
->range_stack
= constructor_range_stack
;
4505 constructor_range_stack
= 0;
4506 designator_depth
= 0;
4507 designator_errorneous
= 0;
4510 /* Don't die if an entire brace-pair level is superfluous
4511 in the containing level. */
4512 if (constructor_type
== 0)
4514 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
4515 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4517 /* Don't die if there are extra init elts at the end. */
4518 if (constructor_fields
== 0)
4519 constructor_type
= 0;
4522 constructor_type
= TREE_TYPE (constructor_fields
);
4523 push_member_name (constructor_fields
);
4524 constructor_depth
++;
4527 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4529 constructor_type
= TREE_TYPE (constructor_type
);
4530 push_array_bounds (tree_low_cst (constructor_index
, 0));
4531 constructor_depth
++;
4534 if (constructor_type
== 0)
4536 error_init ("extra brace group at end of initializer");
4537 constructor_fields
= 0;
4538 constructor_unfilled_fields
= 0;
4542 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
4544 constructor_constant
= TREE_CONSTANT (value
);
4545 constructor_simple
= TREE_STATIC (value
);
4546 constructor_elements
= CONSTRUCTOR_ELTS (value
);
4547 if (constructor_elements
4548 && (TREE_CODE (constructor_type
) == RECORD_TYPE
4549 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
4550 set_nonincremental_init ();
4553 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
4555 missing_braces_mentioned
= 1;
4556 warning_init ("missing braces around initializer");
4559 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4560 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4562 constructor_fields
= TYPE_FIELDS (constructor_type
);
4563 /* Skip any nameless bit fields at the beginning. */
4564 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
4565 && DECL_NAME (constructor_fields
) == 0)
4566 constructor_fields
= TREE_CHAIN (constructor_fields
);
4568 constructor_unfilled_fields
= constructor_fields
;
4569 constructor_bit_index
= bitsize_zero_node
;
4571 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
4573 /* Vectors are like simple fixed-size arrays. */
4574 constructor_max_index
=
4575 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1, 0);
4576 constructor_index
= convert (bitsizetype
, integer_zero_node
);
4577 constructor_unfilled_index
= constructor_index
;
4579 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4581 if (TYPE_DOMAIN (constructor_type
))
4583 constructor_max_index
4584 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
4586 /* Detect non-empty initializations of zero-length arrays. */
4587 if (constructor_max_index
== NULL_TREE
4588 && TYPE_SIZE (constructor_type
))
4589 constructor_max_index
= build_int_2 (-1, -1);
4591 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4592 to initialize VLAs will cause a proper error; avoid tree
4593 checking errors as well by setting a safe value. */
4594 if (constructor_max_index
4595 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
4596 constructor_max_index
= build_int_2 (-1, -1);
4599 = convert (bitsizetype
,
4600 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
4603 constructor_index
= bitsize_zero_node
;
4605 constructor_unfilled_index
= constructor_index
;
4606 if (value
&& TREE_CODE (value
) == STRING_CST
)
4608 /* We need to split the char/wchar array into individual
4609 characters, so that we don't have to special case it
4611 set_nonincremental_init_from_string (value
);
4616 warning_init ("braces around scalar initializer");
4617 constructor_fields
= constructor_type
;
4618 constructor_unfilled_fields
= constructor_type
;
4622 /* At the end of an implicit or explicit brace level,
4623 finish up that level of constructor.
4624 If we were outputting the elements as they are read, return 0
4625 from inner levels (process_init_element ignores that),
4626 but return error_mark_node from the outermost level
4627 (that's what we want to put in DECL_INITIAL).
4628 Otherwise, return a CONSTRUCTOR expression. */
4631 pop_init_level (int implicit
)
4633 struct constructor_stack
*p
;
4634 tree constructor
= 0;
4638 /* When we come to an explicit close brace,
4639 pop any inner levels that didn't have explicit braces. */
4640 while (constructor_stack
->implicit
)
4641 process_init_element (pop_init_level (1));
4643 if (constructor_range_stack
)
4647 p
= constructor_stack
;
4649 /* Error for initializing a flexible array member, or a zero-length
4650 array member in an inappropriate context. */
4651 if (constructor_type
&& constructor_fields
4652 && TREE_CODE (constructor_type
) == ARRAY_TYPE
4653 && TYPE_DOMAIN (constructor_type
)
4654 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
4656 /* Silently discard empty initializations. The parser will
4657 already have pedwarned for empty brackets. */
4658 if (integer_zerop (constructor_unfilled_index
))
4659 constructor_type
= NULL_TREE
;
4660 else if (! TYPE_SIZE (constructor_type
))
4662 if (constructor_depth
> 2)
4663 error_init ("initialization of flexible array member in a nested context");
4665 pedwarn_init ("initialization of a flexible array member");
4667 /* We have already issued an error message for the existence
4668 of a flexible array member not at the end of the structure.
4669 Discard the initializer so that we do not abort later. */
4670 if (TREE_CHAIN (constructor_fields
) != NULL_TREE
)
4671 constructor_type
= NULL_TREE
;
4674 /* Zero-length arrays are no longer special, so we should no longer
4679 /* Warn when some struct elements are implicitly initialized to zero. */
4682 && TREE_CODE (constructor_type
) == RECORD_TYPE
4683 && constructor_unfilled_fields
)
4685 /* Do not warn for flexible array members or zero-length arrays. */
4686 while (constructor_unfilled_fields
4687 && (! DECL_SIZE (constructor_unfilled_fields
)
4688 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
4689 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
4691 /* Do not warn if this level of the initializer uses member
4692 designators; it is likely to be deliberate. */
4693 if (constructor_unfilled_fields
&& !constructor_designated
)
4695 push_member_name (constructor_unfilled_fields
);
4696 warning_init ("missing initializer");
4697 RESTORE_SPELLING_DEPTH (constructor_depth
);
4701 /* Now output all pending elements. */
4702 constructor_incremental
= 1;
4703 output_pending_init_elements (1);
4705 /* Pad out the end of the structure. */
4706 if (p
->replacement_value
)
4707 /* If this closes a superfluous brace pair,
4708 just pass out the element between them. */
4709 constructor
= p
->replacement_value
;
4710 else if (constructor_type
== 0)
4712 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
4713 && TREE_CODE (constructor_type
) != UNION_TYPE
4714 && TREE_CODE (constructor_type
) != ARRAY_TYPE
4715 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
4717 /* A nonincremental scalar initializer--just return
4718 the element, after verifying there is just one. */
4719 if (constructor_elements
== 0)
4721 if (!constructor_erroneous
)
4722 error_init ("empty scalar initializer");
4723 constructor
= error_mark_node
;
4725 else if (TREE_CHAIN (constructor_elements
) != 0)
4727 error_init ("extra elements in scalar initializer");
4728 constructor
= TREE_VALUE (constructor_elements
);
4731 constructor
= TREE_VALUE (constructor_elements
);
4735 if (constructor_erroneous
)
4736 constructor
= error_mark_node
;
4739 constructor
= build_constructor (constructor_type
,
4740 nreverse (constructor_elements
));
4741 if (constructor_constant
)
4742 TREE_CONSTANT (constructor
) = 1;
4743 if (constructor_constant
&& constructor_simple
)
4744 TREE_STATIC (constructor
) = 1;
4748 constructor_type
= p
->type
;
4749 constructor_fields
= p
->fields
;
4750 constructor_index
= p
->index
;
4751 constructor_max_index
= p
->max_index
;
4752 constructor_unfilled_index
= p
->unfilled_index
;
4753 constructor_unfilled_fields
= p
->unfilled_fields
;
4754 constructor_bit_index
= p
->bit_index
;
4755 constructor_elements
= p
->elements
;
4756 constructor_constant
= p
->constant
;
4757 constructor_simple
= p
->simple
;
4758 constructor_erroneous
= p
->erroneous
;
4759 constructor_incremental
= p
->incremental
;
4760 constructor_designated
= p
->designated
;
4761 constructor_pending_elts
= p
->pending_elts
;
4762 constructor_depth
= p
->depth
;
4764 constructor_range_stack
= p
->range_stack
;
4765 RESTORE_SPELLING_DEPTH (constructor_depth
);
4767 constructor_stack
= p
->next
;
4770 if (constructor
== 0)
4772 if (constructor_stack
== 0)
4773 return error_mark_node
;
4779 /* Common handling for both array range and field name designators.
4780 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4783 set_designator (int array
)
4786 enum tree_code subcode
;
4788 /* Don't die if an entire brace-pair level is superfluous
4789 in the containing level. */
4790 if (constructor_type
== 0)
4793 /* If there were errors in this designator list already, bail out silently. */
4794 if (designator_errorneous
)
4797 if (!designator_depth
)
4799 if (constructor_range_stack
)
4802 /* Designator list starts at the level of closest explicit
4804 while (constructor_stack
->implicit
)
4805 process_init_element (pop_init_level (1));
4806 constructor_designated
= 1;
4810 if (constructor_no_implicit
)
4812 error_init ("initialization designators may not nest");
4816 if (TREE_CODE (constructor_type
) == RECORD_TYPE
4817 || TREE_CODE (constructor_type
) == UNION_TYPE
)
4819 subtype
= TREE_TYPE (constructor_fields
);
4820 if (subtype
!= error_mark_node
)
4821 subtype
= TYPE_MAIN_VARIANT (subtype
);
4823 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4825 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
4830 subcode
= TREE_CODE (subtype
);
4831 if (array
&& subcode
!= ARRAY_TYPE
)
4833 error_init ("array index in non-array initializer");
4836 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
4838 error_init ("field name not in record or union initializer");
4842 constructor_designated
= 1;
4843 push_init_level (2);
4847 /* If there are range designators in designator list, push a new designator
4848 to constructor_range_stack. RANGE_END is end of such stack range or
4849 NULL_TREE if there is no range designator at this level. */
4852 push_range_stack (tree range_end
)
4854 struct constructor_range_stack
*p
;
4856 p
= ggc_alloc (sizeof (struct constructor_range_stack
));
4857 p
->prev
= constructor_range_stack
;
4859 p
->fields
= constructor_fields
;
4860 p
->range_start
= constructor_index
;
4861 p
->index
= constructor_index
;
4862 p
->stack
= constructor_stack
;
4863 p
->range_end
= range_end
;
4864 if (constructor_range_stack
)
4865 constructor_range_stack
->next
= p
;
4866 constructor_range_stack
= p
;
4869 /* Within an array initializer, specify the next index to be initialized.
4870 FIRST is that index. If LAST is nonzero, then initialize a range
4871 of indices, running from FIRST through LAST. */
4874 set_init_index (tree first
, tree last
)
4876 if (set_designator (1))
4879 designator_errorneous
= 1;
4881 while ((TREE_CODE (first
) == NOP_EXPR
4882 || TREE_CODE (first
) == CONVERT_EXPR
4883 || TREE_CODE (first
) == NON_LVALUE_EXPR
)
4884 && (TYPE_MODE (TREE_TYPE (first
))
4885 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first
, 0)))))
4886 first
= TREE_OPERAND (first
, 0);
4889 while ((TREE_CODE (last
) == NOP_EXPR
4890 || TREE_CODE (last
) == CONVERT_EXPR
4891 || TREE_CODE (last
) == NON_LVALUE_EXPR
)
4892 && (TYPE_MODE (TREE_TYPE (last
))
4893 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last
, 0)))))
4894 last
= TREE_OPERAND (last
, 0);
4896 if (TREE_CODE (first
) != INTEGER_CST
)
4897 error_init ("nonconstant array index in initializer");
4898 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
4899 error_init ("nonconstant array index in initializer");
4900 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
4901 error_init ("array index in non-array initializer");
4902 else if (tree_int_cst_sgn (first
) == -1)
4903 error_init ("array index in initializer exceeds array bounds");
4904 else if (constructor_max_index
4905 && tree_int_cst_lt (constructor_max_index
, first
))
4906 error_init ("array index in initializer exceeds array bounds");
4909 constructor_index
= convert (bitsizetype
, first
);
4913 if (tree_int_cst_equal (first
, last
))
4915 else if (tree_int_cst_lt (last
, first
))
4917 error_init ("empty index range in initializer");
4922 last
= convert (bitsizetype
, last
);
4923 if (constructor_max_index
!= 0
4924 && tree_int_cst_lt (constructor_max_index
, last
))
4926 error_init ("array index range in initializer exceeds array bounds");
4933 designator_errorneous
= 0;
4934 if (constructor_range_stack
|| last
)
4935 push_range_stack (last
);
4939 /* Within a struct initializer, specify the next field to be initialized. */
4942 set_init_label (tree fieldname
)
4946 if (set_designator (0))
4949 designator_errorneous
= 1;
4951 if (TREE_CODE (constructor_type
) != RECORD_TYPE
4952 && TREE_CODE (constructor_type
) != UNION_TYPE
)
4954 error_init ("field name not in record or union initializer");
4958 for (tail
= TYPE_FIELDS (constructor_type
); tail
;
4959 tail
= TREE_CHAIN (tail
))
4961 if (DECL_NAME (tail
) == fieldname
)
4966 error ("unknown field `%s' specified in initializer",
4967 IDENTIFIER_POINTER (fieldname
));
4970 constructor_fields
= tail
;
4972 designator_errorneous
= 0;
4973 if (constructor_range_stack
)
4974 push_range_stack (NULL_TREE
);
4978 /* Add a new initializer to the tree of pending initializers. PURPOSE
4979 identifies the initializer, either array index or field in a structure.
4980 VALUE is the value of that index or field. */
4983 add_pending_init (tree purpose
, tree value
)
4985 struct init_node
*p
, **q
, *r
;
4987 q
= &constructor_pending_elts
;
4990 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
4995 if (tree_int_cst_lt (purpose
, p
->purpose
))
4997 else if (tree_int_cst_lt (p
->purpose
, purpose
))
5001 if (TREE_SIDE_EFFECTS (p
->value
))
5002 warning_init ("initialized field with side-effects overwritten");
5012 bitpos
= bit_position (purpose
);
5016 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5018 else if (p
->purpose
!= purpose
)
5022 if (TREE_SIDE_EFFECTS (p
->value
))
5023 warning_init ("initialized field with side-effects overwritten");
5030 r
= ggc_alloc (sizeof (struct init_node
));
5031 r
->purpose
= purpose
;
5042 struct init_node
*s
;
5046 if (p
->balance
== 0)
5048 else if (p
->balance
< 0)
5055 p
->left
->parent
= p
;
5072 constructor_pending_elts
= r
;
5077 struct init_node
*t
= r
->right
;
5081 r
->right
->parent
= r
;
5086 p
->left
->parent
= p
;
5089 p
->balance
= t
->balance
< 0;
5090 r
->balance
= -(t
->balance
> 0);
5105 constructor_pending_elts
= t
;
5111 /* p->balance == +1; growth of left side balances the node. */
5116 else /* r == p->right */
5118 if (p
->balance
== 0)
5119 /* Growth propagation from right side. */
5121 else if (p
->balance
> 0)
5128 p
->right
->parent
= p
;
5145 constructor_pending_elts
= r
;
5147 else /* r->balance == -1 */
5150 struct init_node
*t
= r
->left
;
5154 r
->left
->parent
= r
;
5159 p
->right
->parent
= p
;
5162 r
->balance
= (t
->balance
< 0);
5163 p
->balance
= -(t
->balance
> 0);
5178 constructor_pending_elts
= t
;
5184 /* p->balance == -1; growth of right side balances the node. */
5195 /* Build AVL tree from a sorted chain. */
5198 set_nonincremental_init (void)
5202 if (TREE_CODE (constructor_type
) != RECORD_TYPE
5203 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5206 for (chain
= constructor_elements
; chain
; chain
= TREE_CHAIN (chain
))
5207 add_pending_init (TREE_PURPOSE (chain
), TREE_VALUE (chain
));
5208 constructor_elements
= 0;
5209 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5211 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
5212 /* Skip any nameless bit fields at the beginning. */
5213 while (constructor_unfilled_fields
!= 0
5214 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5215 && DECL_NAME (constructor_unfilled_fields
) == 0)
5216 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
5219 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5221 if (TYPE_DOMAIN (constructor_type
))
5222 constructor_unfilled_index
5223 = convert (bitsizetype
,
5224 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
5226 constructor_unfilled_index
= bitsize_zero_node
;
5228 constructor_incremental
= 0;
5231 /* Build AVL tree from a string constant. */
5234 set_nonincremental_init_from_string (tree str
)
5236 tree value
, purpose
, type
;
5237 HOST_WIDE_INT val
[2];
5238 const char *p
, *end
;
5239 int byte
, wchar_bytes
, charwidth
, bitpos
;
5241 if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
5244 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5245 == TYPE_PRECISION (char_type_node
))
5247 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
)))
5248 == TYPE_PRECISION (wchar_type_node
))
5249 wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
5253 charwidth
= TYPE_PRECISION (char_type_node
);
5254 type
= TREE_TYPE (constructor_type
);
5255 p
= TREE_STRING_POINTER (str
);
5256 end
= p
+ TREE_STRING_LENGTH (str
);
5258 for (purpose
= bitsize_zero_node
;
5259 p
< end
&& !tree_int_cst_lt (constructor_max_index
, purpose
);
5260 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
5262 if (wchar_bytes
== 1)
5264 val
[1] = (unsigned char) *p
++;
5271 for (byte
= 0; byte
< wchar_bytes
; byte
++)
5273 if (BYTES_BIG_ENDIAN
)
5274 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
5276 bitpos
= byte
* charwidth
;
5277 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
5278 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
5279 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
5283 if (!TREE_UNSIGNED (type
))
5285 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
5286 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
5288 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
5290 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
5294 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
5299 else if (val
[0] & (((HOST_WIDE_INT
) 1)
5300 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
5301 val
[0] |= ((HOST_WIDE_INT
) -1)
5302 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
5305 value
= build_int_2 (val
[1], val
[0]);
5306 TREE_TYPE (value
) = type
;
5307 add_pending_init (purpose
, value
);
5310 constructor_incremental
= 0;
5313 /* Return value of FIELD in pending initializer or zero if the field was
5314 not initialized yet. */
5317 find_init_member (tree field
)
5319 struct init_node
*p
;
5321 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5323 if (constructor_incremental
5324 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5325 set_nonincremental_init ();
5327 p
= constructor_pending_elts
;
5330 if (tree_int_cst_lt (field
, p
->purpose
))
5332 else if (tree_int_cst_lt (p
->purpose
, field
))
5338 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5340 tree bitpos
= bit_position (field
);
5342 if (constructor_incremental
5343 && (!constructor_unfilled_fields
5344 || tree_int_cst_lt (bitpos
,
5345 bit_position (constructor_unfilled_fields
))))
5346 set_nonincremental_init ();
5348 p
= constructor_pending_elts
;
5351 if (field
== p
->purpose
)
5353 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
5359 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5361 if (constructor_elements
5362 && TREE_PURPOSE (constructor_elements
) == field
)
5363 return TREE_VALUE (constructor_elements
);
5368 /* "Output" the next constructor element.
5369 At top level, really output it to assembler code now.
5370 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5371 TYPE is the data type that the containing data type wants here.
5372 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5374 PENDING if non-nil means output pending elements that belong
5375 right after this element. (PENDING is normally 1;
5376 it is 0 while outputting pending elements, to avoid recursion.) */
5379 output_init_element (tree value
, tree type
, tree field
, int pending
)
5381 if (type
== error_mark_node
)
5383 constructor_erroneous
= 1;
5386 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
5387 || (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
5388 && !(TREE_CODE (value
) == STRING_CST
5389 && TREE_CODE (type
) == ARRAY_TYPE
5390 && TREE_CODE (TREE_TYPE (type
)) == INTEGER_TYPE
)
5391 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
5392 TYPE_MAIN_VARIANT (type
), COMPARE_STRICT
)))
5393 value
= default_conversion (value
);
5395 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
5396 && require_constant_value
&& !flag_isoc99
&& pending
)
5398 /* As an extension, allow initializing objects with static storage
5399 duration with compound literals (which are then treated just as
5400 the brace enclosed list they contain). */
5401 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
5402 value
= DECL_INITIAL (decl
);
5405 if (value
== error_mark_node
)
5406 constructor_erroneous
= 1;
5407 else if (!TREE_CONSTANT (value
))
5408 constructor_constant
= 0;
5409 else if (initializer_constant_valid_p (value
, TREE_TYPE (value
)) == 0
5410 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
5411 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5412 && DECL_C_BIT_FIELD (field
)
5413 && TREE_CODE (value
) != INTEGER_CST
))
5414 constructor_simple
= 0;
5416 if (require_constant_value
&& ! TREE_CONSTANT (value
))
5418 error_init ("initializer element is not constant");
5419 value
= error_mark_node
;
5421 else if (require_constant_elements
5422 && initializer_constant_valid_p (value
, TREE_TYPE (value
)) == 0)
5423 pedwarn ("initializer element is not computable at load time");
5425 /* If this field is empty (and not at the end of structure),
5426 don't do anything other than checking the initializer. */
5428 && (TREE_TYPE (field
) == error_mark_node
5429 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
5430 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
5431 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
5432 || TREE_CHAIN (field
)))))
5435 value
= digest_init (type
, value
, require_constant_value
);
5436 if (value
== error_mark_node
)
5438 constructor_erroneous
= 1;
5442 /* If this element doesn't come next in sequence,
5443 put it on constructor_pending_elts. */
5444 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5445 && (!constructor_incremental
5446 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
5448 if (constructor_incremental
5449 && tree_int_cst_lt (field
, constructor_unfilled_index
))
5450 set_nonincremental_init ();
5452 add_pending_init (field
, value
);
5455 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5456 && (!constructor_incremental
5457 || field
!= constructor_unfilled_fields
))
5459 /* We do this for records but not for unions. In a union,
5460 no matter which field is specified, it can be initialized
5461 right away since it starts at the beginning of the union. */
5462 if (constructor_incremental
)
5464 if (!constructor_unfilled_fields
)
5465 set_nonincremental_init ();
5468 tree bitpos
, unfillpos
;
5470 bitpos
= bit_position (field
);
5471 unfillpos
= bit_position (constructor_unfilled_fields
);
5473 if (tree_int_cst_lt (bitpos
, unfillpos
))
5474 set_nonincremental_init ();
5478 add_pending_init (field
, value
);
5481 else if (TREE_CODE (constructor_type
) == UNION_TYPE
5482 && constructor_elements
)
5484 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements
)))
5485 warning_init ("initialized field with side-effects overwritten");
5487 /* We can have just one union field set. */
5488 constructor_elements
= 0;
5491 /* Otherwise, output this element either to
5492 constructor_elements or to the assembler file. */
5494 if (field
&& TREE_CODE (field
) == INTEGER_CST
)
5495 field
= copy_node (field
);
5496 constructor_elements
5497 = tree_cons (field
, value
, constructor_elements
);
5499 /* Advance the variable that indicates sequential elements output. */
5500 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5501 constructor_unfilled_index
5502 = size_binop (PLUS_EXPR
, constructor_unfilled_index
,
5504 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5506 constructor_unfilled_fields
5507 = TREE_CHAIN (constructor_unfilled_fields
);
5509 /* Skip any nameless bit fields. */
5510 while (constructor_unfilled_fields
!= 0
5511 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5512 && DECL_NAME (constructor_unfilled_fields
) == 0)
5513 constructor_unfilled_fields
=
5514 TREE_CHAIN (constructor_unfilled_fields
);
5516 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5517 constructor_unfilled_fields
= 0;
5519 /* Now output any pending elements which have become next. */
5521 output_pending_init_elements (0);
5524 /* Output any pending elements which have become next.
5525 As we output elements, constructor_unfilled_{fields,index}
5526 advances, which may cause other elements to become next;
5527 if so, they too are output.
5529 If ALL is 0, we return when there are
5530 no more pending elements to output now.
5532 If ALL is 1, we output space as necessary so that
5533 we can output all the pending elements. */
5536 output_pending_init_elements (int all
)
5538 struct init_node
*elt
= constructor_pending_elts
;
5543 /* Look through the whole pending tree.
5544 If we find an element that should be output now,
5545 output it. Otherwise, set NEXT to the element
5546 that comes first among those still pending. */
5551 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5553 if (tree_int_cst_equal (elt
->purpose
,
5554 constructor_unfilled_index
))
5555 output_init_element (elt
->value
,
5556 TREE_TYPE (constructor_type
),
5557 constructor_unfilled_index
, 0);
5558 else if (tree_int_cst_lt (constructor_unfilled_index
,
5561 /* Advance to the next smaller node. */
5566 /* We have reached the smallest node bigger than the
5567 current unfilled index. Fill the space first. */
5568 next
= elt
->purpose
;
5574 /* Advance to the next bigger node. */
5579 /* We have reached the biggest node in a subtree. Find
5580 the parent of it, which is the next bigger node. */
5581 while (elt
->parent
&& elt
->parent
->right
== elt
)
5584 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
5587 next
= elt
->purpose
;
5593 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
5594 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5596 tree ctor_unfilled_bitpos
, elt_bitpos
;
5598 /* If the current record is complete we are done. */
5599 if (constructor_unfilled_fields
== 0)
5602 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
5603 elt_bitpos
= bit_position (elt
->purpose
);
5604 /* We can't compare fields here because there might be empty
5605 fields in between. */
5606 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
5608 constructor_unfilled_fields
= elt
->purpose
;
5609 output_init_element (elt
->value
, TREE_TYPE (elt
->purpose
),
5612 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
5614 /* Advance to the next smaller node. */
5619 /* We have reached the smallest node bigger than the
5620 current unfilled field. Fill the space first. */
5621 next
= elt
->purpose
;
5627 /* Advance to the next bigger node. */
5632 /* We have reached the biggest node in a subtree. Find
5633 the parent of it, which is the next bigger node. */
5634 while (elt
->parent
&& elt
->parent
->right
== elt
)
5638 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
5639 bit_position (elt
->purpose
))))
5641 next
= elt
->purpose
;
5649 /* Ordinarily return, but not if we want to output all
5650 and there are elements left. */
5651 if (! (all
&& next
!= 0))
5654 /* If it's not incremental, just skip over the gap, so that after
5655 jumping to retry we will output the next successive element. */
5656 if (TREE_CODE (constructor_type
) == RECORD_TYPE
5657 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5658 constructor_unfilled_fields
= next
;
5659 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5660 constructor_unfilled_index
= next
;
5662 /* ELT now points to the node in the pending tree with the next
5663 initializer to output. */
5667 /* Add one non-braced element to the current constructor level.
5668 This adjusts the current position within the constructor's type.
5669 This may also start or terminate implicit levels
5670 to handle a partly-braced initializer.
5672 Once this has found the correct level for the new element,
5673 it calls output_init_element. */
5676 process_init_element (tree value
)
5678 tree orig_value
= value
;
5679 int string_flag
= value
!= 0 && TREE_CODE (value
) == STRING_CST
;
5681 designator_depth
= 0;
5682 designator_errorneous
= 0;
5684 /* Handle superfluous braces around string cst as in
5685 char x[] = {"foo"}; */
5688 && TREE_CODE (constructor_type
) == ARRAY_TYPE
5689 && TREE_CODE (TREE_TYPE (constructor_type
)) == INTEGER_TYPE
5690 && integer_zerop (constructor_unfilled_index
))
5692 if (constructor_stack
->replacement_value
)
5693 error_init ("excess elements in char array initializer");
5694 constructor_stack
->replacement_value
= value
;
5698 if (constructor_stack
->replacement_value
!= 0)
5700 error_init ("excess elements in struct initializer");
5704 /* Ignore elements of a brace group if it is entirely superfluous
5705 and has already been diagnosed. */
5706 if (constructor_type
== 0)
5709 /* If we've exhausted any levels that didn't have braces,
5711 while (constructor_stack
->implicit
)
5713 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
5714 || TREE_CODE (constructor_type
) == UNION_TYPE
)
5715 && constructor_fields
== 0)
5716 process_init_element (pop_init_level (1));
5717 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
5718 && (constructor_max_index
== 0
5719 || tree_int_cst_lt (constructor_max_index
,
5720 constructor_index
)))
5721 process_init_element (pop_init_level (1));
5726 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5727 if (constructor_range_stack
)
5729 /* If value is a compound literal and we'll be just using its
5730 content, don't put it into a SAVE_EXPR. */
5731 if (TREE_CODE (value
) != COMPOUND_LITERAL_EXPR
5732 || !require_constant_value
5734 value
= save_expr (value
);
5739 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
5742 enum tree_code fieldcode
;
5744 if (constructor_fields
== 0)
5746 pedwarn_init ("excess elements in struct initializer");
5750 fieldtype
= TREE_TYPE (constructor_fields
);
5751 if (fieldtype
!= error_mark_node
)
5752 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
5753 fieldcode
= TREE_CODE (fieldtype
);
5755 /* Error for non-static initialization of a flexible array member. */
5756 if (fieldcode
== ARRAY_TYPE
5757 && !require_constant_value
5758 && TYPE_SIZE (fieldtype
) == NULL_TREE
5759 && TREE_CHAIN (constructor_fields
) == NULL_TREE
)
5761 error_init ("non-static initialization of a flexible array member");
5765 /* Accept a string constant to initialize a subarray. */
5767 && fieldcode
== ARRAY_TYPE
5768 && TREE_CODE (TREE_TYPE (fieldtype
)) == INTEGER_TYPE
5771 /* Otherwise, if we have come to a subaggregate,
5772 and we don't have an element of its type, push into it. */
5773 else if (value
!= 0 && !constructor_no_implicit
5774 && value
!= error_mark_node
5775 && TYPE_MAIN_VARIANT (TREE_TYPE (value
)) != fieldtype
5776 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
5777 || fieldcode
== UNION_TYPE
))
5779 push_init_level (1);
5785 push_member_name (constructor_fields
);
5786 output_init_element (value
, fieldtype
, constructor_fields
, 1);
5787 RESTORE_SPELLING_DEPTH (constructor_depth
);
5790 /* Do the bookkeeping for an element that was
5791 directly output as a constructor. */
5793 /* For a record, keep track of end position of last field. */
5794 if (DECL_SIZE (constructor_fields
))
5795 constructor_bit_index
5796 = size_binop (PLUS_EXPR
,
5797 bit_position (constructor_fields
),
5798 DECL_SIZE (constructor_fields
));
5800 /* If the current field was the first one not yet written out,
5801 it isn't now, so update. */
5802 if (constructor_unfilled_fields
== constructor_fields
)
5804 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
5805 /* Skip any nameless bit fields. */
5806 while (constructor_unfilled_fields
!= 0
5807 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
5808 && DECL_NAME (constructor_unfilled_fields
) == 0)
5809 constructor_unfilled_fields
=
5810 TREE_CHAIN (constructor_unfilled_fields
);
5814 constructor_fields
= TREE_CHAIN (constructor_fields
);
5815 /* Skip any nameless bit fields at the beginning. */
5816 while (constructor_fields
!= 0
5817 && DECL_C_BIT_FIELD (constructor_fields
)
5818 && DECL_NAME (constructor_fields
) == 0)
5819 constructor_fields
= TREE_CHAIN (constructor_fields
);
5821 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
5824 enum tree_code fieldcode
;
5826 if (constructor_fields
== 0)
5828 pedwarn_init ("excess elements in union initializer");
5832 fieldtype
= TREE_TYPE (constructor_fields
);
5833 if (fieldtype
!= error_mark_node
)
5834 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
5835 fieldcode
= TREE_CODE (fieldtype
);
5837 /* Warn that traditional C rejects initialization of unions.
5838 We skip the warning if the value is zero. This is done
5839 under the assumption that the zero initializer in user
5840 code appears conditioned on e.g. __STDC__ to avoid
5841 "missing initializer" warnings and relies on default
5842 initialization to zero in the traditional C case.
5843 We also skip the warning if the initializer is designated,
5844 again on the assumption that this must be conditional on
5845 __STDC__ anyway (and we've already complained about the
5846 member-designator already). */
5847 if (warn_traditional
&& !in_system_header
&& !constructor_designated
5848 && !(value
&& (integer_zerop (value
) || real_zerop (value
))))
5849 warning ("traditional C rejects initialization of unions");
5851 /* Accept a string constant to initialize a subarray. */
5853 && fieldcode
== ARRAY_TYPE
5854 && TREE_CODE (TREE_TYPE (fieldtype
)) == INTEGER_TYPE
5857 /* Otherwise, if we have come to a subaggregate,
5858 and we don't have an element of its type, push into it. */
5859 else if (value
!= 0 && !constructor_no_implicit
5860 && value
!= error_mark_node
5861 && TYPE_MAIN_VARIANT (TREE_TYPE (value
)) != fieldtype
5862 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
5863 || fieldcode
== UNION_TYPE
))
5865 push_init_level (1);
5871 push_member_name (constructor_fields
);
5872 output_init_element (value
, fieldtype
, constructor_fields
, 1);
5873 RESTORE_SPELLING_DEPTH (constructor_depth
);
5876 /* Do the bookkeeping for an element that was
5877 directly output as a constructor. */
5879 constructor_bit_index
= DECL_SIZE (constructor_fields
);
5880 constructor_unfilled_fields
= TREE_CHAIN (constructor_fields
);
5883 constructor_fields
= 0;
5885 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
5887 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
5888 enum tree_code eltcode
= TREE_CODE (elttype
);
5890 /* Accept a string constant to initialize a subarray. */
5892 && eltcode
== ARRAY_TYPE
5893 && TREE_CODE (TREE_TYPE (elttype
)) == INTEGER_TYPE
5896 /* Otherwise, if we have come to a subaggregate,
5897 and we don't have an element of its type, push into it. */
5898 else if (value
!= 0 && !constructor_no_implicit
5899 && value
!= error_mark_node
5900 && TYPE_MAIN_VARIANT (TREE_TYPE (value
)) != elttype
5901 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
5902 || eltcode
== UNION_TYPE
))
5904 push_init_level (1);
5908 if (constructor_max_index
!= 0
5909 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
5910 || integer_all_onesp (constructor_max_index
)))
5912 pedwarn_init ("excess elements in array initializer");
5916 /* Now output the actual element. */
5919 push_array_bounds (tree_low_cst (constructor_index
, 0));
5920 output_init_element (value
, elttype
, constructor_index
, 1);
5921 RESTORE_SPELLING_DEPTH (constructor_depth
);
5925 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
5928 /* If we are doing the bookkeeping for an element that was
5929 directly output as a constructor, we must update
5930 constructor_unfilled_index. */
5931 constructor_unfilled_index
= constructor_index
;
5933 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
5935 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
5937 /* Do a basic check of initializer size. Note that vectors
5938 always have a fixed size derived from their type. */
5939 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
5941 pedwarn_init ("excess elements in vector initializer");
5945 /* Now output the actual element. */
5947 output_init_element (value
, elttype
, constructor_index
, 1);
5950 = size_binop (PLUS_EXPR
, constructor_index
, bitsize_one_node
);
5953 /* If we are doing the bookkeeping for an element that was
5954 directly output as a constructor, we must update
5955 constructor_unfilled_index. */
5956 constructor_unfilled_index
= constructor_index
;
5959 /* Handle the sole element allowed in a braced initializer
5960 for a scalar variable. */
5961 else if (constructor_fields
== 0)
5963 pedwarn_init ("excess elements in scalar initializer");
5969 output_init_element (value
, constructor_type
, NULL_TREE
, 1);
5970 constructor_fields
= 0;
5973 /* Handle range initializers either at this level or anywhere higher
5974 in the designator stack. */
5975 if (constructor_range_stack
)
5977 struct constructor_range_stack
*p
, *range_stack
;
5980 range_stack
= constructor_range_stack
;
5981 constructor_range_stack
= 0;
5982 while (constructor_stack
!= range_stack
->stack
)
5984 if (!constructor_stack
->implicit
)
5986 process_init_element (pop_init_level (1));
5988 for (p
= range_stack
;
5989 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
5992 if (!constructor_stack
->implicit
)
5994 process_init_element (pop_init_level (1));
5997 p
->index
= size_binop (PLUS_EXPR
, p
->index
, bitsize_one_node
);
5998 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
6003 constructor_index
= p
->index
;
6004 constructor_fields
= p
->fields
;
6005 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
6013 push_init_level (2);
6014 p
->stack
= constructor_stack
;
6015 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
6016 p
->index
= p
->range_start
;
6020 constructor_range_stack
= range_stack
;
6027 constructor_range_stack
= 0;
6030 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6031 (guaranteed to be 'volatile' or null) and ARGS (represented using
6032 an ASM_STMT node). */
6034 build_asm_stmt (tree cv_qualifier
, tree args
)
6036 if (!TREE_OPERAND (args
, 0))
6037 TREE_OPERAND (args
, 0) = cv_qualifier
;
6038 return add_stmt (args
);
6041 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6042 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6043 SIMPLE indicates whether there was anything at all after the
6044 string in the asm expression -- asm("blah") and asm("blah" : )
6045 are subtly different. We use a ASM_STMT node to represent this. */
6047 build_asm_expr (tree string
, tree outputs
, tree inputs
, tree clobbers
,
6053 /* We can remove output conversions that change the type,
6054 but not the mode. */
6055 for (tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
))
6057 tree output
= TREE_VALUE (tail
);
6059 STRIP_NOPS (output
);
6060 TREE_VALUE (tail
) = output
;
6062 /* Allow conversions as LHS here. build_modify_expr as called below
6063 will do the right thing with them. */
6064 while (TREE_CODE (output
) == NOP_EXPR
6065 || TREE_CODE (output
) == CONVERT_EXPR
6066 || TREE_CODE (output
) == FLOAT_EXPR
6067 || TREE_CODE (output
) == FIX_TRUNC_EXPR
6068 || TREE_CODE (output
) == FIX_FLOOR_EXPR
6069 || TREE_CODE (output
) == FIX_ROUND_EXPR
6070 || TREE_CODE (output
) == FIX_CEIL_EXPR
)
6071 output
= TREE_OPERAND (output
, 0);
6073 lvalue_or_else (TREE_VALUE (tail
), "invalid lvalue in asm statement");
6076 /* Remove output conversions that change the type but not the mode. */
6077 for (tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
))
6079 tree output
= TREE_VALUE (tail
);
6080 STRIP_NOPS (output
);
6081 TREE_VALUE (tail
) = output
;
6084 /* Perform default conversions on array and function inputs.
6085 Don't do this for other types as it would screw up operands
6086 expected to be in memory. */
6087 for (tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
))
6088 TREE_VALUE (tail
) = default_function_array_conversion (TREE_VALUE (tail
));
6090 args
= build_stmt (ASM_STMT
, 0, string
, outputs
, inputs
, clobbers
);
6092 /* Simple asm statements are treated as volatile. */
6095 TREE_OPERAND (args
, 0) = ridpointers
[RID_VOLATILE
];
6096 ASM_INPUT_P (args
) = 1;
6101 /* Expand an ASM statement with operands, handling output operands
6102 that are not variables or INDIRECT_REFS by transforming such
6103 cases into cases that expand_asm_operands can handle.
6105 Arguments are same as for expand_asm_operands. */
6108 c_expand_asm_operands (tree string
, tree outputs
, tree inputs
,
6109 tree clobbers
, int vol
, location_t locus
)
6111 int noutputs
= list_length (outputs
);
6113 /* o[I] is the place that output number I should be written. */
6114 tree
*o
= alloca (noutputs
* sizeof (tree
));
6117 /* Record the contents of OUTPUTS before it is modified. */
6118 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6120 o
[i
] = TREE_VALUE (tail
);
6121 if (o
[i
] == error_mark_node
)
6125 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6126 OUTPUTS some trees for where the values were actually stored. */
6127 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, locus
);
6129 /* Copy all the intermediate outputs into the specified outputs. */
6130 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
6132 if (o
[i
] != TREE_VALUE (tail
))
6134 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
6135 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
6138 /* Restore the original value so that it's correct the next
6139 time we expand this function. */
6140 TREE_VALUE (tail
) = o
[i
];
6142 /* Detect modification of read-only values.
6143 (Otherwise done by build_modify_expr.) */
6146 tree type
= TREE_TYPE (o
[i
]);
6147 if (TREE_READONLY (o
[i
])
6148 || TYPE_READONLY (type
)
6149 || ((TREE_CODE (type
) == RECORD_TYPE
6150 || TREE_CODE (type
) == UNION_TYPE
)
6151 && C_TYPE_FIELDS_READONLY (type
)))
6152 readonly_error (o
[i
], "modification by `asm'");
6156 /* Those MODIFY_EXPRs could do autoincrements. */
6160 /* Expand a C `return' statement.
6161 RETVAL is the expression for what to return,
6162 or a null pointer for `return;' with no value. */
6165 c_expand_return (tree retval
)
6167 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
6169 if (TREE_THIS_VOLATILE (current_function_decl
))
6170 warning ("function declared `noreturn' has a `return' statement");
6174 current_function_returns_null
= 1;
6175 if ((warn_return_type
|| flag_isoc99
)
6176 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
6177 pedwarn_c99 ("`return' with no value, in function returning non-void");
6179 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
6181 current_function_returns_null
= 1;
6182 if (pedantic
|| TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
6183 pedwarn ("`return' with a value, in function returning void");
6187 tree t
= convert_for_assignment (valtype
, retval
, _("return"),
6188 NULL_TREE
, NULL_TREE
, 0);
6189 tree res
= DECL_RESULT (current_function_decl
);
6192 current_function_returns_value
= 1;
6193 if (t
== error_mark_node
)
6196 inner
= t
= convert (TREE_TYPE (res
), t
);
6198 /* Strip any conversions, additions, and subtractions, and see if
6199 we are returning the address of a local variable. Warn if so. */
6202 switch (TREE_CODE (inner
))
6204 case NOP_EXPR
: case NON_LVALUE_EXPR
: case CONVERT_EXPR
:
6206 inner
= TREE_OPERAND (inner
, 0);
6210 /* If the second operand of the MINUS_EXPR has a pointer
6211 type (or is converted from it), this may be valid, so
6212 don't give a warning. */
6214 tree op1
= TREE_OPERAND (inner
, 1);
6216 while (! POINTER_TYPE_P (TREE_TYPE (op1
))
6217 && (TREE_CODE (op1
) == NOP_EXPR
6218 || TREE_CODE (op1
) == NON_LVALUE_EXPR
6219 || TREE_CODE (op1
) == CONVERT_EXPR
))
6220 op1
= TREE_OPERAND (op1
, 0);
6222 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
6225 inner
= TREE_OPERAND (inner
, 0);
6230 inner
= TREE_OPERAND (inner
, 0);
6232 while (TREE_CODE_CLASS (TREE_CODE (inner
)) == 'r')
6233 inner
= TREE_OPERAND (inner
, 0);
6236 && ! DECL_EXTERNAL (inner
)
6237 && ! TREE_STATIC (inner
)
6238 && DECL_CONTEXT (inner
) == current_function_decl
)
6239 warning ("function returns address of local variable");
6249 retval
= build (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
6252 return add_stmt (build_return_stmt (retval
));
6256 /* The SWITCH_STMT being built. */
6258 /* A splay-tree mapping the low element of a case range to the high
6259 element, or NULL_TREE if there is no high element. Used to
6260 determine whether or not a new case label duplicates an old case
6261 label. We need a tree, rather than simply a hash table, because
6262 of the GNU case range extension. */
6264 /* The next node on the stack. */
6265 struct c_switch
*next
;
6268 /* A stack of the currently active switch statements. The innermost
6269 switch statement is on the top of the stack. There is no need to
6270 mark the stack for garbage collection because it is only active
6271 during the processing of the body of a function, and we never
6272 collect at that point. */
6274 static struct c_switch
*switch_stack
;
6276 /* Start a C switch statement, testing expression EXP. Return the new
6280 c_start_case (tree exp
)
6282 enum tree_code code
;
6283 tree type
, orig_type
= error_mark_node
;
6284 struct c_switch
*cs
;
6286 if (exp
!= error_mark_node
)
6288 code
= TREE_CODE (TREE_TYPE (exp
));
6289 orig_type
= TREE_TYPE (exp
);
6291 if (! INTEGRAL_TYPE_P (orig_type
)
6292 && code
!= ERROR_MARK
)
6294 error ("switch quantity not an integer");
6295 exp
= integer_zero_node
;
6299 type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
6301 if (warn_traditional
&& !in_system_header
6302 && (type
== long_integer_type_node
6303 || type
== long_unsigned_type_node
))
6304 warning ("`long' switch expression not converted to `int' in ISO C");
6306 exp
= default_conversion (exp
);
6307 type
= TREE_TYPE (exp
);
6311 /* Add this new SWITCH_STMT to the stack. */
6312 cs
= xmalloc (sizeof (*cs
));
6313 cs
->switch_stmt
= build_stmt (SWITCH_STMT
, exp
, NULL_TREE
, orig_type
);
6314 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
6315 cs
->next
= switch_stack
;
6318 return add_stmt (switch_stack
->switch_stmt
);
6321 /* Process a case label. */
6324 do_case (tree low_value
, tree high_value
)
6326 tree label
= NULL_TREE
;
6330 bool switch_was_empty_p
= (SWITCH_BODY (switch_stack
->switch_stmt
) == NULL_TREE
);
6332 label
= c_add_case_label (switch_stack
->cases
,
6333 SWITCH_COND (switch_stack
->switch_stmt
),
6334 low_value
, high_value
);
6335 if (label
== error_mark_node
)
6337 else if (switch_was_empty_p
)
6339 /* Attach the first case label to the SWITCH_BODY. */
6340 SWITCH_BODY (switch_stack
->switch_stmt
) = TREE_CHAIN (switch_stack
->switch_stmt
);
6341 TREE_CHAIN (switch_stack
->switch_stmt
) = NULL_TREE
;
6345 error ("case label not within a switch statement");
6347 error ("`default' label not within a switch statement");
6352 /* Finish the switch statement. */
6355 c_finish_case (void)
6357 struct c_switch
*cs
= switch_stack
;
6359 /* Rechain the next statements to the SWITCH_STMT. */
6360 last_tree
= cs
->switch_stmt
;
6362 /* Pop the stack. */
6363 switch_stack
= switch_stack
->next
;
6364 splay_tree_delete (cs
->cases
);
6368 /* Build a binary-operation expression without default conversions.
6369 CODE is the kind of expression to build.
6370 This function differs from `build' in several ways:
6371 the data type of the result is computed and recorded in it,
6372 warnings are generated if arg data types are invalid,
6373 special handling for addition and subtraction of pointers is known,
6374 and some optimization is done (operations on narrow ints
6375 are done in the narrower type when that gives the same result).
6376 Constant folding is also done before the result is returned.
6378 Note that the operands will never have enumeral types, or function
6379 or array types, because either they will have the default conversions
6380 performed or they have both just been converted to some other type in which
6381 the arithmetic is to be done. */
6384 build_binary_op (enum tree_code code
, tree orig_op0
, tree orig_op1
,
6388 enum tree_code code0
, code1
;
6391 /* Expression code to give to the expression when it is built.
6392 Normally this is CODE, which is what the caller asked for,
6393 but in some special cases we change it. */
6394 enum tree_code resultcode
= code
;
6396 /* Data type in which the computation is to be performed.
6397 In the simplest cases this is the common type of the arguments. */
6398 tree result_type
= NULL
;
6400 /* Nonzero means operands have already been type-converted
6401 in whatever way is necessary.
6402 Zero means they need to be converted to RESULT_TYPE. */
6405 /* Nonzero means create the expression with this type, rather than
6407 tree build_type
= 0;
6409 /* Nonzero means after finally constructing the expression
6410 convert it to this type. */
6411 tree final_type
= 0;
6413 /* Nonzero if this is an operation like MIN or MAX which can
6414 safely be computed in short if both args are promoted shorts.
6415 Also implies COMMON.
6416 -1 indicates a bitwise operation; this makes a difference
6417 in the exact conditions for when it is safe to do the operation
6418 in a narrower mode. */
6421 /* Nonzero if this is a comparison operation;
6422 if both args are promoted shorts, compare the original shorts.
6423 Also implies COMMON. */
6424 int short_compare
= 0;
6426 /* Nonzero if this is a right-shift operation, which can be computed on the
6427 original short and then promoted if the operand is a promoted short. */
6428 int short_shift
= 0;
6430 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6435 op0
= default_conversion (orig_op0
);
6436 op1
= default_conversion (orig_op1
);
6444 type0
= TREE_TYPE (op0
);
6445 type1
= TREE_TYPE (op1
);
6447 /* The expression codes of the data types of the arguments tell us
6448 whether the arguments are integers, floating, pointers, etc. */
6449 code0
= TREE_CODE (type0
);
6450 code1
= TREE_CODE (type1
);
6452 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6453 STRIP_TYPE_NOPS (op0
);
6454 STRIP_TYPE_NOPS (op1
);
6456 /* If an error was already reported for one of the arguments,
6457 avoid reporting another error. */
6459 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
6460 return error_mark_node
;
6465 /* Handle the pointer + int case. */
6466 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6467 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
6468 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
6469 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
6475 /* Subtraction of two similar pointers.
6476 We must subtract them as integers, then divide by object size. */
6477 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
6478 && comp_target_types (type0
, type1
, 1))
6479 return pointer_diff (op0
, op1
);
6480 /* Handle pointer minus int. Just like pointer plus int. */
6481 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6482 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
6491 case TRUNC_DIV_EXPR
:
6493 case FLOOR_DIV_EXPR
:
6494 case ROUND_DIV_EXPR
:
6495 case EXACT_DIV_EXPR
:
6496 /* Floating point division by zero is a legitimate way to obtain
6497 infinities and NaNs. */
6498 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
6499 warning ("division by zero");
6501 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
6502 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
6503 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6504 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
6506 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
6507 resultcode
= RDIV_EXPR
;
6509 /* Although it would be tempting to shorten always here, that
6510 loses on some targets, since the modulo instruction is
6511 undefined if the quotient can't be represented in the
6512 computation mode. We shorten only if unsigned or if
6513 dividing by something we know != -1. */
6514 shorten
= (TREE_UNSIGNED (TREE_TYPE (orig_op0
))
6515 || (TREE_CODE (op1
) == INTEGER_CST
6516 && ! integer_all_onesp (op1
)));
6524 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6526 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
6530 case TRUNC_MOD_EXPR
:
6531 case FLOOR_MOD_EXPR
:
6532 if (warn_div_by_zero
&& skip_evaluation
== 0 && integer_zerop (op1
))
6533 warning ("division by zero");
6535 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6537 /* Although it would be tempting to shorten always here, that loses
6538 on some targets, since the modulo instruction is undefined if the
6539 quotient can't be represented in the computation mode. We shorten
6540 only if unsigned or if dividing by something we know != -1. */
6541 shorten
= (TREE_UNSIGNED (TREE_TYPE (orig_op0
))
6542 || (TREE_CODE (op1
) == INTEGER_CST
6543 && ! integer_all_onesp (op1
)));
6548 case TRUTH_ANDIF_EXPR
:
6549 case TRUTH_ORIF_EXPR
:
6550 case TRUTH_AND_EXPR
:
6552 case TRUTH_XOR_EXPR
:
6553 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
6554 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
)
6555 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
6556 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
6558 /* Result of these operations is always an int,
6559 but that does not mean the operands should be
6560 converted to ints! */
6561 result_type
= integer_type_node
;
6562 op0
= (*lang_hooks
.truthvalue_conversion
) (op0
);
6563 op1
= (*lang_hooks
.truthvalue_conversion
) (op1
);
6568 /* Shift operations: result has same type as first operand;
6569 always convert second operand to int.
6570 Also set SHORT_SHIFT if shifting rightward. */
6573 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6575 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
6577 if (tree_int_cst_sgn (op1
) < 0)
6578 warning ("right shift count is negative");
6581 if (! integer_zerop (op1
))
6584 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
6585 warning ("right shift count >= width of type");
6589 /* Use the type of the value to be shifted. */
6590 result_type
= type0
;
6591 /* Convert the shift-count to an integer, regardless of size
6592 of value being shifted. */
6593 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
6594 op1
= convert (integer_type_node
, op1
);
6595 /* Avoid converting op1 to result_type later. */
6601 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6603 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
6605 if (tree_int_cst_sgn (op1
) < 0)
6606 warning ("left shift count is negative");
6608 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
6609 warning ("left shift count >= width of type");
6612 /* Use the type of the value to be shifted. */
6613 result_type
= type0
;
6614 /* Convert the shift-count to an integer, regardless of size
6615 of value being shifted. */
6616 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
6617 op1
= convert (integer_type_node
, op1
);
6618 /* Avoid converting op1 to result_type later. */
6625 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
6627 if (TREE_CODE (op1
) == INTEGER_CST
&& skip_evaluation
== 0)
6629 if (tree_int_cst_sgn (op1
) < 0)
6630 warning ("shift count is negative");
6631 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
6632 warning ("shift count >= width of type");
6635 /* Use the type of the value to be shifted. */
6636 result_type
= type0
;
6637 /* Convert the shift-count to an integer, regardless of size
6638 of value being shifted. */
6639 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
6640 op1
= convert (integer_type_node
, op1
);
6641 /* Avoid converting op1 to result_type later. */
6648 if (warn_float_equal
&& (code0
== REAL_TYPE
|| code1
== REAL_TYPE
))
6649 warning ("comparing floating point with == or != is unsafe");
6650 /* Result of comparison is always int,
6651 but don't convert the args to int! */
6652 build_type
= integer_type_node
;
6653 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
6654 || code0
== COMPLEX_TYPE
6655 || code0
== VECTOR_TYPE
)
6656 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6657 || code1
== COMPLEX_TYPE
6658 || code1
== VECTOR_TYPE
))
6660 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6662 tree tt0
= TREE_TYPE (type0
);
6663 tree tt1
= TREE_TYPE (type1
);
6664 /* Anything compares with void *. void * compares with anything.
6665 Otherwise, the targets must be compatible
6666 and both must be object or both incomplete. */
6667 if (comp_target_types (type0
, type1
, 1))
6668 result_type
= common_type (type0
, type1
);
6669 else if (VOID_TYPE_P (tt0
))
6671 /* op0 != orig_op0 detects the case of something
6672 whose value is 0 but which isn't a valid null ptr const. */
6673 if (pedantic
&& (!integer_zerop (op0
) || op0
!= orig_op0
)
6674 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
6675 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6677 else if (VOID_TYPE_P (tt1
))
6679 if (pedantic
&& (!integer_zerop (op1
) || op1
!= orig_op1
)
6680 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
6681 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6684 pedwarn ("comparison of distinct pointer types lacks a cast");
6686 if (result_type
== NULL_TREE
)
6687 result_type
= ptr_type_node
;
6689 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
6690 && integer_zerop (op1
))
6691 result_type
= type0
;
6692 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
6693 && integer_zerop (op0
))
6694 result_type
= type1
;
6695 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6697 result_type
= type0
;
6698 pedwarn ("comparison between pointer and integer");
6700 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
6702 result_type
= type1
;
6703 pedwarn ("comparison between pointer and integer");
6709 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
6710 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
6712 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6714 if (comp_target_types (type0
, type1
, 1))
6716 result_type
= common_type (type0
, type1
);
6718 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
6719 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6723 result_type
= ptr_type_node
;
6724 pedwarn ("comparison of distinct pointer types lacks a cast");
6733 build_type
= integer_type_node
;
6734 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
6735 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
6737 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6739 if (comp_target_types (type0
, type1
, 1))
6741 result_type
= common_type (type0
, type1
);
6742 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
6743 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
6744 pedwarn ("comparison of complete and incomplete pointers");
6746 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
6747 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6751 result_type
= ptr_type_node
;
6752 pedwarn ("comparison of distinct pointer types lacks a cast");
6755 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
6756 && integer_zerop (op1
))
6758 result_type
= type0
;
6759 if (pedantic
|| extra_warnings
)
6760 pedwarn ("ordered comparison of pointer with integer zero");
6762 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
6763 && integer_zerop (op0
))
6765 result_type
= type1
;
6767 pedwarn ("ordered comparison of pointer with integer zero");
6769 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6771 result_type
= type0
;
6772 pedwarn ("comparison between pointer and integer");
6774 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
6776 result_type
= type1
;
6777 pedwarn ("comparison between pointer and integer");
6781 case UNORDERED_EXPR
:
6788 build_type
= integer_type_node
;
6789 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
6791 error ("unordered comparison on non-floating point argument");
6792 return error_mark_node
;
6801 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
6802 return error_mark_node
;
6804 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
6805 || code0
== VECTOR_TYPE
)
6807 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
6808 || code1
== VECTOR_TYPE
))
6810 int none_complex
= (code0
!= COMPLEX_TYPE
&& code1
!= COMPLEX_TYPE
);
6812 if (shorten
|| common
|| short_compare
)
6813 result_type
= common_type (type0
, type1
);
6815 /* For certain operations (which identify themselves by shorten != 0)
6816 if both args were extended from the same smaller type,
6817 do the arithmetic in that type and then extend.
6819 shorten !=0 and !=1 indicates a bitwise operation.
6820 For them, this optimization is safe only if
6821 both args are zero-extended or both are sign-extended.
6822 Otherwise, we might change the result.
6823 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6824 but calculated in (unsigned short) it would be (unsigned short)-1. */
6826 if (shorten
&& none_complex
)
6828 int unsigned0
, unsigned1
;
6829 tree arg0
= get_narrower (op0
, &unsigned0
);
6830 tree arg1
= get_narrower (op1
, &unsigned1
);
6831 /* UNS is 1 if the operation to be done is an unsigned one. */
6832 int uns
= TREE_UNSIGNED (result_type
);
6835 final_type
= result_type
;
6837 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6838 but it *requires* conversion to FINAL_TYPE. */
6840 if ((TYPE_PRECISION (TREE_TYPE (op0
))
6841 == TYPE_PRECISION (TREE_TYPE (arg0
)))
6842 && TREE_TYPE (op0
) != final_type
)
6843 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
6844 if ((TYPE_PRECISION (TREE_TYPE (op1
))
6845 == TYPE_PRECISION (TREE_TYPE (arg1
)))
6846 && TREE_TYPE (op1
) != final_type
)
6847 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
6849 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6851 /* For bitwise operations, signedness of nominal type
6852 does not matter. Consider only how operands were extended. */
6856 /* Note that in all three cases below we refrain from optimizing
6857 an unsigned operation on sign-extended args.
6858 That would not be valid. */
6860 /* Both args variable: if both extended in same way
6861 from same width, do it in that width.
6862 Do it unsigned if args were zero-extended. */
6863 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
6864 < TYPE_PRECISION (result_type
))
6865 && (TYPE_PRECISION (TREE_TYPE (arg1
))
6866 == TYPE_PRECISION (TREE_TYPE (arg0
)))
6867 && unsigned0
== unsigned1
6868 && (unsigned0
|| !uns
))
6870 = c_common_signed_or_unsigned_type
6871 (unsigned0
, common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
6872 else if (TREE_CODE (arg0
) == INTEGER_CST
6873 && (unsigned1
|| !uns
)
6874 && (TYPE_PRECISION (TREE_TYPE (arg1
))
6875 < TYPE_PRECISION (result_type
))
6877 = c_common_signed_or_unsigned_type (unsigned1
,
6879 int_fits_type_p (arg0
, type
)))
6881 else if (TREE_CODE (arg1
) == INTEGER_CST
6882 && (unsigned0
|| !uns
)
6883 && (TYPE_PRECISION (TREE_TYPE (arg0
))
6884 < TYPE_PRECISION (result_type
))
6886 = c_common_signed_or_unsigned_type (unsigned0
,
6888 int_fits_type_p (arg1
, type
)))
6892 /* Shifts can be shortened if shifting right. */
6897 tree arg0
= get_narrower (op0
, &unsigned_arg
);
6899 final_type
= result_type
;
6901 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
6902 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
6904 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
6905 /* We can shorten only if the shift count is less than the
6906 number of bits in the smaller type size. */
6907 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
6908 /* We cannot drop an unsigned shift after sign-extension. */
6909 && (!TREE_UNSIGNED (final_type
) || unsigned_arg
))
6911 /* Do an unsigned shift if the operand was zero-extended. */
6913 = c_common_signed_or_unsigned_type (unsigned_arg
,
6915 /* Convert value-to-be-shifted to that type. */
6916 if (TREE_TYPE (op0
) != result_type
)
6917 op0
= convert (result_type
, op0
);
6922 /* Comparison operations are shortened too but differently.
6923 They identify themselves by setting short_compare = 1. */
6927 /* Don't write &op0, etc., because that would prevent op0
6928 from being kept in a register.
6929 Instead, make copies of the our local variables and
6930 pass the copies by reference, then copy them back afterward. */
6931 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
6932 enum tree_code xresultcode
= resultcode
;
6934 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
6939 op0
= xop0
, op1
= xop1
;
6941 resultcode
= xresultcode
;
6943 if (warn_sign_compare
&& skip_evaluation
== 0)
6945 int op0_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op0
));
6946 int op1_signed
= ! TREE_UNSIGNED (TREE_TYPE (orig_op1
));
6947 int unsignedp0
, unsignedp1
;
6948 tree primop0
= get_narrower (op0
, &unsignedp0
);
6949 tree primop1
= get_narrower (op1
, &unsignedp1
);
6953 STRIP_TYPE_NOPS (xop0
);
6954 STRIP_TYPE_NOPS (xop1
);
6956 /* Give warnings for comparisons between signed and unsigned
6957 quantities that may fail.
6959 Do the checking based on the original operand trees, so that
6960 casts will be considered, but default promotions won't be.
6962 Do not warn if the comparison is being done in a signed type,
6963 since the signed type will only be chosen if it can represent
6964 all the values of the unsigned type. */
6965 if (! TREE_UNSIGNED (result_type
))
6967 /* Do not warn if both operands are the same signedness. */
6968 else if (op0_signed
== op1_signed
)
6975 sop
= xop0
, uop
= xop1
;
6977 sop
= xop1
, uop
= xop0
;
6979 /* Do not warn if the signed quantity is an
6980 unsuffixed integer literal (or some static
6981 constant expression involving such literals or a
6982 conditional expression involving such literals)
6983 and it is non-negative. */
6984 if (c_tree_expr_nonnegative_p (sop
))
6986 /* Do not warn if the comparison is an equality operation,
6987 the unsigned quantity is an integral constant, and it
6988 would fit in the result if the result were signed. */
6989 else if (TREE_CODE (uop
) == INTEGER_CST
6990 && (resultcode
== EQ_EXPR
|| resultcode
== NE_EXPR
)
6992 (uop
, c_common_signed_type (result_type
)))
6994 /* Do not warn if the unsigned quantity is an enumeration
6995 constant and its maximum value would fit in the result
6996 if the result were signed. */
6997 else if (TREE_CODE (uop
) == INTEGER_CST
6998 && TREE_CODE (TREE_TYPE (uop
)) == ENUMERAL_TYPE
7000 (TYPE_MAX_VALUE (TREE_TYPE(uop
)),
7001 c_common_signed_type (result_type
)))
7004 warning ("comparison between signed and unsigned");
7007 /* Warn if two unsigned values are being compared in a size
7008 larger than their original size, and one (and only one) is the
7009 result of a `~' operator. This comparison will always fail.
7011 Also warn if one operand is a constant, and the constant
7012 does not have all bits set that are set in the ~ operand
7013 when it is extended. */
7015 if ((TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7016 != (TREE_CODE (primop1
) == BIT_NOT_EXPR
))
7018 if (TREE_CODE (primop0
) == BIT_NOT_EXPR
)
7019 primop0
= get_narrower (TREE_OPERAND (primop0
, 0),
7022 primop1
= get_narrower (TREE_OPERAND (primop1
, 0),
7025 if (host_integerp (primop0
, 0) || host_integerp (primop1
, 0))
7028 HOST_WIDE_INT constant
, mask
;
7029 int unsignedp
, bits
;
7031 if (host_integerp (primop0
, 0))
7034 unsignedp
= unsignedp1
;
7035 constant
= tree_low_cst (primop0
, 0);
7040 unsignedp
= unsignedp0
;
7041 constant
= tree_low_cst (primop1
, 0);
7044 bits
= TYPE_PRECISION (TREE_TYPE (primop
));
7045 if (bits
< TYPE_PRECISION (result_type
)
7046 && bits
< HOST_BITS_PER_WIDE_INT
&& unsignedp
)
7048 mask
= (~ (HOST_WIDE_INT
) 0) << bits
;
7049 if ((mask
& constant
) != mask
)
7050 warning ("comparison of promoted ~unsigned with constant");
7053 else if (unsignedp0
&& unsignedp1
7054 && (TYPE_PRECISION (TREE_TYPE (primop0
))
7055 < TYPE_PRECISION (result_type
))
7056 && (TYPE_PRECISION (TREE_TYPE (primop1
))
7057 < TYPE_PRECISION (result_type
)))
7058 warning ("comparison of promoted ~unsigned with unsigned");
7064 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7065 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7066 Then the expression will be built.
7067 It will be given type FINAL_TYPE if that is nonzero;
7068 otherwise, it will be given type RESULT_TYPE. */
7072 binary_op_error (code
);
7073 return error_mark_node
;
7078 if (TREE_TYPE (op0
) != result_type
)
7079 op0
= convert (result_type
, op0
);
7080 if (TREE_TYPE (op1
) != result_type
)
7081 op1
= convert (result_type
, op1
);
7084 if (build_type
== NULL_TREE
)
7085 build_type
= result_type
;
7088 tree result
= build (resultcode
, build_type
, op0
, op1
);
7091 /* Treat expressions in initializers specially as they can't trap. */
7092 folded
= initializer_stack
? fold_initializer (result
)
7094 if (folded
== result
)
7095 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
7096 if (final_type
!= 0)
7097 return convert (final_type
, folded
);