1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
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, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
30 #include "coretypes.h"
34 #include "langhooks.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
58 /* The level of nesting inside "__alignof__". */
61 /* The level of nesting inside "sizeof". */
64 /* The level of nesting inside "typeof". */
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static tree qualify_type (tree, tree);
78 static int tagged_types_tu_compatible_p (tree, tree);
79 static int comp_target_types (tree, tree);
80 static int function_types_compatible_p (tree, tree);
81 static int type_lists_compatible_p (tree, tree);
82 static tree decl_constant_value_for_broken_optimization (tree);
83 static tree lookup_field (tree, tree);
84 static tree convert_arguments (tree, tree, tree, tree);
85 static tree pointer_diff (tree, tree);
86 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88 static tree valid_compound_expr_initializer (tree, tree);
89 static void push_string (const char *);
90 static void push_member_name (tree);
91 static void push_array_bounds (int);
92 static int spelling_length (void);
93 static char *print_spelling (char *);
94 static void warning_init (const char *);
95 static tree digest_init (tree, tree, bool, int);
96 static void output_init_element (tree, bool, tree, tree, int);
97 static void output_pending_init_elements (int);
98 static int set_designator (int);
99 static void push_range_stack (tree);
100 static void add_pending_init (tree, tree);
101 static void set_nonincremental_init (void);
102 static void set_nonincremental_init_from_string (tree);
103 static tree find_init_member (tree);
104 static void readonly_error (tree, enum lvalue_use);
105 static int lvalue_or_else (tree, enum lvalue_use);
106 static int lvalue_p (tree);
107 static void record_maybe_used_decl (tree);
108 static int comptypes_internal (tree, tree);
109 \f/* This is a cache to hold if two types are compatible or not. */
111 struct tagged_tu_seen_cache {
112 const struct tagged_tu_seen_cache * next;
115 /* The return value of tagged_types_tu_compatible_p if we had seen
116 these two types already. */
120 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
121 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
123 /* Do `exp = require_complete_type (exp);' to make sure exp
124 does not have an incomplete type. (That includes void types.) */
127 require_complete_type (tree value)
129 tree type = TREE_TYPE (value);
131 if (value == error_mark_node || type == error_mark_node)
132 return error_mark_node;
134 /* First, detect a valid value with a complete type. */
135 if (COMPLETE_TYPE_P (type))
138 c_incomplete_type_error (value, type);
139 return error_mark_node;
142 /* Print an error message for invalid use of an incomplete type.
143 VALUE is the expression that was used (or 0 if that isn't known)
144 and TYPE is the type that was invalid. */
147 c_incomplete_type_error (tree value, tree type)
149 const char *type_code_string;
151 /* Avoid duplicate error message. */
152 if (TREE_CODE (type) == ERROR_MARK)
155 if (value != 0 && (TREE_CODE (value) == VAR_DECL
156 || TREE_CODE (value) == PARM_DECL))
157 error ("%qD has an incomplete type", value);
161 /* We must print an error message. Be clever about what it says. */
163 switch (TREE_CODE (type))
166 type_code_string = "struct";
170 type_code_string = "union";
174 type_code_string = "enum";
178 error ("invalid use of void expression");
182 if (TYPE_DOMAIN (type))
184 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
186 error ("invalid use of flexible array member");
189 type = TREE_TYPE (type);
192 error ("invalid use of array with unspecified bounds");
199 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
200 error ("invalid use of undefined type %<%s %E%>",
201 type_code_string, TYPE_NAME (type));
203 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
204 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
208 /* Given a type, apply default promotions wrt unnamed function
209 arguments and return the new type. */
212 c_type_promotes_to (tree type)
214 if (TYPE_MAIN_VARIANT (type) == float_type_node)
215 return double_type_node;
217 if (c_promoting_integer_type_p (type))
219 /* Preserve unsignedness if not really getting any wider. */
220 if (TYPE_UNSIGNED (type)
221 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
222 return unsigned_type_node;
223 return integer_type_node;
229 /* Return a variant of TYPE which has all the type qualifiers of LIKE
230 as well as those of TYPE. */
233 qualify_type (tree type, tree like)
235 return c_build_qualified_type (type,
236 TYPE_QUALS (type) | TYPE_QUALS (like));
239 /* Return the composite type of two compatible types.
241 We assume that comptypes has already been done and returned
242 nonzero; if that isn't so, this may crash. In particular, we
243 assume that qualifiers match. */
246 composite_type (tree t1, tree t2)
248 enum tree_code code1;
249 enum tree_code code2;
252 /* Save time if the two types are the same. */
254 if (t1 == t2) return t1;
256 /* If one type is nonsense, use the other. */
257 if (t1 == error_mark_node)
259 if (t2 == error_mark_node)
262 code1 = TREE_CODE (t1);
263 code2 = TREE_CODE (t2);
265 /* Merge the attributes. */
266 attributes = targetm.merge_type_attributes (t1, t2);
268 /* If one is an enumerated type and the other is the compatible
269 integer type, the composite type might be either of the two
270 (DR#013 question 3). For consistency, use the enumerated type as
271 the composite type. */
273 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
275 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
278 gcc_assert (code1 == code2);
283 /* For two pointers, do this recursively on the target type. */
285 tree pointed_to_1 = TREE_TYPE (t1);
286 tree pointed_to_2 = TREE_TYPE (t2);
287 tree target = composite_type (pointed_to_1, pointed_to_2);
288 t1 = build_pointer_type (target);
289 t1 = build_type_attribute_variant (t1, attributes);
290 return qualify_type (t1, t2);
295 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
298 tree d1 = TYPE_DOMAIN (t1);
299 tree d2 = TYPE_DOMAIN (t2);
300 bool d1_variable, d2_variable;
301 bool d1_zero, d2_zero;
303 /* We should not have any type quals on arrays at all. */
304 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
306 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
307 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
309 d1_variable = (!d1_zero
310 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
311 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
312 d2_variable = (!d2_zero
313 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
314 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
316 /* Save space: see if the result is identical to one of the args. */
317 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
318 && (d2_variable || d2_zero || !d1_variable))
319 return build_type_attribute_variant (t1, attributes);
320 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
321 && (d1_variable || d1_zero || !d2_variable))
322 return build_type_attribute_variant (t2, attributes);
324 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
325 return build_type_attribute_variant (t1, attributes);
326 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
327 return build_type_attribute_variant (t2, attributes);
329 /* Merge the element types, and have a size if either arg has
330 one. We may have qualifiers on the element types. To set
331 up TYPE_MAIN_VARIANT correctly, we need to form the
332 composite of the unqualified types and add the qualifiers
334 quals = TYPE_QUALS (strip_array_types (elt));
335 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
336 t1 = build_array_type (unqual_elt,
337 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
343 t1 = c_build_qualified_type (t1, quals);
344 return build_type_attribute_variant (t1, attributes);
348 /* Function types: prefer the one that specified arg types.
349 If both do, merge the arg types. Also merge the return types. */
351 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
352 tree p1 = TYPE_ARG_TYPES (t1);
353 tree p2 = TYPE_ARG_TYPES (t2);
358 /* Save space: see if the result is identical to one of the args. */
359 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
360 return build_type_attribute_variant (t1, attributes);
361 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
362 return build_type_attribute_variant (t2, attributes);
364 /* Simple way if one arg fails to specify argument types. */
365 if (TYPE_ARG_TYPES (t1) == 0)
367 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
368 t1 = build_type_attribute_variant (t1, attributes);
369 return qualify_type (t1, t2);
371 if (TYPE_ARG_TYPES (t2) == 0)
373 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
374 t1 = build_type_attribute_variant (t1, attributes);
375 return qualify_type (t1, t2);
378 /* If both args specify argument types, we must merge the two
379 lists, argument by argument. */
380 /* Tell global_bindings_p to return false so that variable_size
381 doesn't die on VLAs in parameter types. */
382 c_override_global_bindings_to_false = true;
384 len = list_length (p1);
387 for (i = 0; i < len; i++)
388 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
393 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
395 /* A null type means arg type is not specified.
396 Take whatever the other function type has. */
397 if (TREE_VALUE (p1) == 0)
399 TREE_VALUE (n) = TREE_VALUE (p2);
402 if (TREE_VALUE (p2) == 0)
404 TREE_VALUE (n) = TREE_VALUE (p1);
408 /* Given wait (union {union wait *u; int *i} *)
409 and wait (union wait *),
410 prefer union wait * as type of parm. */
411 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
412 && TREE_VALUE (p1) != TREE_VALUE (p2))
415 tree mv2 = TREE_VALUE (p2);
416 if (mv2 && mv2 != error_mark_node
417 && TREE_CODE (mv2) != ARRAY_TYPE)
418 mv2 = TYPE_MAIN_VARIANT (mv2);
419 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
420 memb; memb = TREE_CHAIN (memb))
422 tree mv3 = TREE_TYPE (memb);
423 if (mv3 && mv3 != error_mark_node
424 && TREE_CODE (mv3) != ARRAY_TYPE)
425 mv3 = TYPE_MAIN_VARIANT (mv3);
426 if (comptypes (mv3, mv2))
428 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
431 pedwarn ("function types not truly compatible in ISO C");
436 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
437 && TREE_VALUE (p2) != TREE_VALUE (p1))
440 tree mv1 = TREE_VALUE (p1);
441 if (mv1 && mv1 != error_mark_node
442 && TREE_CODE (mv1) != ARRAY_TYPE)
443 mv1 = TYPE_MAIN_VARIANT (mv1);
444 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
445 memb; memb = TREE_CHAIN (memb))
447 tree mv3 = TREE_TYPE (memb);
448 if (mv3 && mv3 != error_mark_node
449 && TREE_CODE (mv3) != ARRAY_TYPE)
450 mv3 = TYPE_MAIN_VARIANT (mv3);
451 if (comptypes (mv3, mv1))
453 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
456 pedwarn ("function types not truly compatible in ISO C");
461 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
465 c_override_global_bindings_to_false = false;
466 t1 = build_function_type (valtype, newargs);
467 t1 = qualify_type (t1, t2);
468 /* ... falls through ... */
472 return build_type_attribute_variant (t1, attributes);
477 /* Return the type of a conditional expression between pointers to
478 possibly differently qualified versions of compatible types.
480 We assume that comp_target_types has already been done and returned
481 nonzero; if that isn't so, this may crash. */
484 common_pointer_type (tree t1, tree t2)
487 tree pointed_to_1, mv1;
488 tree pointed_to_2, mv2;
491 /* Save time if the two types are the same. */
493 if (t1 == t2) return t1;
495 /* If one type is nonsense, use the other. */
496 if (t1 == error_mark_node)
498 if (t2 == error_mark_node)
501 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
502 && TREE_CODE (t2) == POINTER_TYPE);
504 /* Merge the attributes. */
505 attributes = targetm.merge_type_attributes (t1, t2);
507 /* Find the composite type of the target types, and combine the
508 qualifiers of the two types' targets. Do not lose qualifiers on
509 array element types by taking the TYPE_MAIN_VARIANT. */
510 mv1 = pointed_to_1 = TREE_TYPE (t1);
511 mv2 = pointed_to_2 = TREE_TYPE (t2);
512 if (TREE_CODE (mv1) != ARRAY_TYPE)
513 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
514 if (TREE_CODE (mv2) != ARRAY_TYPE)
515 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
516 target = composite_type (mv1, mv2);
517 t1 = build_pointer_type (c_build_qualified_type
519 TYPE_QUALS (pointed_to_1) |
520 TYPE_QUALS (pointed_to_2)));
521 return build_type_attribute_variant (t1, attributes);
524 /* Return the common type for two arithmetic types under the usual
525 arithmetic conversions. The default conversions have already been
526 applied, and enumerated types converted to their compatible integer
527 types. The resulting type is unqualified and has no attributes.
529 This is the type for the result of most arithmetic operations
530 if the operands have the given two types. */
533 c_common_type (tree t1, tree t2)
535 enum tree_code code1;
536 enum tree_code code2;
538 /* If one type is nonsense, use the other. */
539 if (t1 == error_mark_node)
541 if (t2 == error_mark_node)
544 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
545 t1 = TYPE_MAIN_VARIANT (t1);
547 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
548 t2 = TYPE_MAIN_VARIANT (t2);
550 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
551 t1 = build_type_attribute_variant (t1, NULL_TREE);
553 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
554 t2 = build_type_attribute_variant (t2, NULL_TREE);
556 /* Save time if the two types are the same. */
558 if (t1 == t2) return t1;
560 code1 = TREE_CODE (t1);
561 code2 = TREE_CODE (t2);
563 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
564 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
565 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
566 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
568 /* If one type is a vector type, return that type. (How the usual
569 arithmetic conversions apply to the vector types extension is not
570 precisely specified.) */
571 if (code1 == VECTOR_TYPE)
574 if (code2 == VECTOR_TYPE)
577 /* If one type is complex, form the common type of the non-complex
578 components, then make that complex. Use T1 or T2 if it is the
580 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
582 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
583 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
584 tree subtype = c_common_type (subtype1, subtype2);
586 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
588 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
591 return build_complex_type (subtype);
594 /* If only one is real, use it as the result. */
596 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
599 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
602 /* Both real or both integers; use the one with greater precision. */
604 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
606 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
609 /* Same precision. Prefer long longs to longs to ints when the
610 same precision, following the C99 rules on integer type rank
611 (which are equivalent to the C90 rules for C90 types). */
613 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
614 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
615 return long_long_unsigned_type_node;
617 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
618 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
620 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
621 return long_long_unsigned_type_node;
623 return long_long_integer_type_node;
626 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
627 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
628 return long_unsigned_type_node;
630 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
631 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
633 /* But preserve unsignedness from the other type,
634 since long cannot hold all the values of an unsigned int. */
635 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
636 return long_unsigned_type_node;
638 return long_integer_type_node;
641 /* Likewise, prefer long double to double even if same size. */
642 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
643 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
644 return long_double_type_node;
646 /* Otherwise prefer the unsigned one. */
648 if (TYPE_UNSIGNED (t1))
654 /* Wrapper around c_common_type that is used by c-common.c and other
655 front end optimizations that remove promotions. ENUMERAL_TYPEs
656 are allowed here and are converted to their compatible integer types.
657 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
658 preferably a non-Boolean type as the common type. */
660 common_type (tree t1, tree t2)
662 if (TREE_CODE (t1) == ENUMERAL_TYPE)
663 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
664 if (TREE_CODE (t2) == ENUMERAL_TYPE)
665 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
667 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
668 if (TREE_CODE (t1) == BOOLEAN_TYPE
669 && TREE_CODE (t2) == BOOLEAN_TYPE)
670 return boolean_type_node;
672 /* If either type is BOOLEAN_TYPE, then return the other. */
673 if (TREE_CODE (t1) == BOOLEAN_TYPE)
675 if (TREE_CODE (t2) == BOOLEAN_TYPE)
678 return c_common_type (t1, t2);
681 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
682 or various other operations. Return 2 if they are compatible
683 but a warning may be needed if you use them together. */
686 comptypes (tree type1, tree type2)
688 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
691 val = comptypes_internal (type1, type2);
692 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
696 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
697 or various other operations. Return 2 if they are compatible
698 but a warning may be needed if you use them together. This
699 differs from comptypes, in that we don't free the seen types. */
702 comptypes_internal (tree type1, tree type2)
708 /* Suppress errors caused by previously reported errors. */
710 if (t1 == t2 || !t1 || !t2
711 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
714 /* If either type is the internal version of sizetype, return the
716 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
717 && TYPE_ORIG_SIZE_TYPE (t1))
718 t1 = TYPE_ORIG_SIZE_TYPE (t1);
720 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
721 && TYPE_ORIG_SIZE_TYPE (t2))
722 t2 = TYPE_ORIG_SIZE_TYPE (t2);
725 /* Enumerated types are compatible with integer types, but this is
726 not transitive: two enumerated types in the same translation unit
727 are compatible with each other only if they are the same type. */
729 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
730 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
731 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
732 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
737 /* Different classes of types can't be compatible. */
739 if (TREE_CODE (t1) != TREE_CODE (t2))
742 /* Qualifiers must match. C99 6.7.3p9 */
744 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
747 /* Allow for two different type nodes which have essentially the same
748 definition. Note that we already checked for equality of the type
749 qualifiers (just above). */
751 if (TREE_CODE (t1) != ARRAY_TYPE
752 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
755 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
756 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
759 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
762 switch (TREE_CODE (t1))
765 /* Do not remove mode or aliasing information. */
766 if (TYPE_MODE (t1) != TYPE_MODE (t2)
767 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
769 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
770 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
774 val = function_types_compatible_p (t1, t2);
779 tree d1 = TYPE_DOMAIN (t1);
780 tree d2 = TYPE_DOMAIN (t2);
781 bool d1_variable, d2_variable;
782 bool d1_zero, d2_zero;
785 /* Target types must match incl. qualifiers. */
786 if (TREE_TYPE (t1) != TREE_TYPE (t2)
787 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
790 /* Sizes must match unless one is missing or variable. */
791 if (d1 == 0 || d2 == 0 || d1 == d2)
794 d1_zero = !TYPE_MAX_VALUE (d1);
795 d2_zero = !TYPE_MAX_VALUE (d2);
797 d1_variable = (!d1_zero
798 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
799 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
800 d2_variable = (!d2_zero
801 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
802 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
804 if (d1_variable || d2_variable)
806 if (d1_zero && d2_zero)
808 if (d1_zero || d2_zero
809 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
810 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
819 if (val != 1 && !same_translation_unit_p (t1, t2))
822 return tagged_types_tu_compatible_p (t1, t2);
823 val = tagged_types_tu_compatible_p (t1, t2);
828 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
829 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
835 return attrval == 2 && val == 1 ? 2 : val;
838 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
839 ignoring their qualifiers. */
842 comp_target_types (tree ttl, tree ttr)
847 /* Do not lose qualifiers on element types of array types that are
848 pointer targets by taking their TYPE_MAIN_VARIANT. */
849 mvl = TREE_TYPE (ttl);
850 mvr = TREE_TYPE (ttr);
851 if (TREE_CODE (mvl) != ARRAY_TYPE)
852 mvl = TYPE_MAIN_VARIANT (mvl);
853 if (TREE_CODE (mvr) != ARRAY_TYPE)
854 mvr = TYPE_MAIN_VARIANT (mvr);
855 val = comptypes (mvl, mvr);
857 if (val == 2 && pedantic)
858 pedwarn ("types are not quite compatible");
862 /* Subroutines of `comptypes'. */
864 /* Determine whether two trees derive from the same translation unit.
865 If the CONTEXT chain ends in a null, that tree's context is still
866 being parsed, so if two trees have context chains ending in null,
867 they're in the same translation unit. */
869 same_translation_unit_p (tree t1, tree t2)
871 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
872 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
874 case tcc_declaration:
875 t1 = DECL_CONTEXT (t1); break;
877 t1 = TYPE_CONTEXT (t1); break;
878 case tcc_exceptional:
879 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
880 default: gcc_unreachable ();
883 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
884 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
886 case tcc_declaration:
887 t2 = DECL_CONTEXT (t2); break;
889 t2 = TYPE_CONTEXT (t2); break;
890 case tcc_exceptional:
891 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
892 default: gcc_unreachable ();
898 /* Allocate the seen two types, assuming that they are compatible. */
900 static struct tagged_tu_seen_cache *
901 alloc_tagged_tu_seen_cache (tree t1, tree t2)
903 struct tagged_tu_seen_cache *tu = xmalloc (sizeof (struct tagged_tu_seen_cache));
904 tu->next = tagged_tu_seen_base;
908 tagged_tu_seen_base = tu;
910 /* The C standard says that two structures in different translation
911 units are compatible with each other only if the types of their
912 fields are compatible (among other things). We assume that they
913 are compatible until proven otherwise when building the cache.
914 An example where this can occur is:
919 If we are comparing this against a similar struct in another TU,
920 and did not assume they were compatible, we end up with an infinite
926 /* Free the seen types until we get to TU_TIL. */
929 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
931 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
934 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
938 tagged_tu_seen_base = tu_til;
941 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
942 compatible. If the two types are not the same (which has been
943 checked earlier), this can only happen when multiple translation
944 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
948 tagged_types_tu_compatible_p (tree t1, tree t2)
951 bool needs_warning = false;
953 /* We have to verify that the tags of the types are the same. This
954 is harder than it looks because this may be a typedef, so we have
955 to go look at the original type. It may even be a typedef of a
957 In the case of compiler-created builtin structs the TYPE_DECL
958 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
959 while (TYPE_NAME (t1)
960 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
961 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
962 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
964 while (TYPE_NAME (t2)
965 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
966 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
967 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
969 /* C90 didn't have the requirement that the two tags be the same. */
970 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
973 /* C90 didn't say what happened if one or both of the types were
974 incomplete; we choose to follow C99 rules here, which is that they
976 if (TYPE_SIZE (t1) == NULL
977 || TYPE_SIZE (t2) == NULL)
981 const struct tagged_tu_seen_cache * tts_i;
982 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
983 if (tts_i->t1 == t1 && tts_i->t2 == t2)
987 switch (TREE_CODE (t1))
991 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
992 /* Speed up the case where the type values are in the same order. */
993 tree tv1 = TYPE_VALUES (t1);
994 tree tv2 = TYPE_VALUES (t2);
1001 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1003 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1005 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1012 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1016 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1022 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1028 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1030 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1032 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1043 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1044 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1050 /* Speed up the common case where the fields are in the same order. */
1051 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1052 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1057 if (DECL_NAME (s1) == NULL
1058 || DECL_NAME (s1) != DECL_NAME (s2))
1060 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1067 needs_warning = true;
1069 if (TREE_CODE (s1) == FIELD_DECL
1070 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1071 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1079 tu->val = needs_warning ? 2 : 1;
1083 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1087 if (DECL_NAME (s1) != NULL)
1088 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1089 if (DECL_NAME (s1) == DECL_NAME (s2))
1092 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1099 needs_warning = true;
1101 if (TREE_CODE (s1) == FIELD_DECL
1102 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1103 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1115 tu->val = needs_warning ? 2 : 10;
1121 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1123 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1125 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1128 if (TREE_CODE (s1) != TREE_CODE (s2)
1129 || DECL_NAME (s1) != DECL_NAME (s2))
1131 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1135 needs_warning = true;
1137 if (TREE_CODE (s1) == FIELD_DECL
1138 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1139 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1145 tu->val = needs_warning ? 2 : 1;
1154 /* Return 1 if two function types F1 and F2 are compatible.
1155 If either type specifies no argument types,
1156 the other must specify a fixed number of self-promoting arg types.
1157 Otherwise, if one type specifies only the number of arguments,
1158 the other must specify that number of self-promoting arg types.
1159 Otherwise, the argument types must match. */
1162 function_types_compatible_p (tree f1, tree f2)
1165 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1170 ret1 = TREE_TYPE (f1);
1171 ret2 = TREE_TYPE (f2);
1173 /* 'volatile' qualifiers on a function's return type used to mean
1174 the function is noreturn. */
1175 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1176 pedwarn ("function return types not compatible due to %<volatile%>");
1177 if (TYPE_VOLATILE (ret1))
1178 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1179 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1180 if (TYPE_VOLATILE (ret2))
1181 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1182 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1183 val = comptypes_internal (ret1, ret2);
1187 args1 = TYPE_ARG_TYPES (f1);
1188 args2 = TYPE_ARG_TYPES (f2);
1190 /* An unspecified parmlist matches any specified parmlist
1191 whose argument types don't need default promotions. */
1195 if (!self_promoting_args_p (args2))
1197 /* If one of these types comes from a non-prototype fn definition,
1198 compare that with the other type's arglist.
1199 If they don't match, ask for a warning (but no error). */
1200 if (TYPE_ACTUAL_ARG_TYPES (f1)
1201 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1207 if (!self_promoting_args_p (args1))
1209 if (TYPE_ACTUAL_ARG_TYPES (f2)
1210 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1215 /* Both types have argument lists: compare them and propagate results. */
1216 val1 = type_lists_compatible_p (args1, args2);
1217 return val1 != 1 ? val1 : val;
1220 /* Check two lists of types for compatibility,
1221 returning 0 for incompatible, 1 for compatible,
1222 or 2 for compatible with warning. */
1225 type_lists_compatible_p (tree args1, tree args2)
1227 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1233 tree a1, mv1, a2, mv2;
1234 if (args1 == 0 && args2 == 0)
1236 /* If one list is shorter than the other,
1237 they fail to match. */
1238 if (args1 == 0 || args2 == 0)
1240 mv1 = a1 = TREE_VALUE (args1);
1241 mv2 = a2 = TREE_VALUE (args2);
1242 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1243 mv1 = TYPE_MAIN_VARIANT (mv1);
1244 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1245 mv2 = TYPE_MAIN_VARIANT (mv2);
1246 /* A null pointer instead of a type
1247 means there is supposed to be an argument
1248 but nothing is specified about what type it has.
1249 So match anything that self-promotes. */
1252 if (c_type_promotes_to (a2) != a2)
1257 if (c_type_promotes_to (a1) != a1)
1260 /* If one of the lists has an error marker, ignore this arg. */
1261 else if (TREE_CODE (a1) == ERROR_MARK
1262 || TREE_CODE (a2) == ERROR_MARK)
1264 else if (!(newval = comptypes_internal (mv1, mv2)))
1266 /* Allow wait (union {union wait *u; int *i} *)
1267 and wait (union wait *) to be compatible. */
1268 if (TREE_CODE (a1) == UNION_TYPE
1269 && (TYPE_NAME (a1) == 0
1270 || TYPE_TRANSPARENT_UNION (a1))
1271 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1272 && tree_int_cst_equal (TYPE_SIZE (a1),
1276 for (memb = TYPE_FIELDS (a1);
1277 memb; memb = TREE_CHAIN (memb))
1279 tree mv3 = TREE_TYPE (memb);
1280 if (mv3 && mv3 != error_mark_node
1281 && TREE_CODE (mv3) != ARRAY_TYPE)
1282 mv3 = TYPE_MAIN_VARIANT (mv3);
1283 if (comptypes_internal (mv3, mv2))
1289 else if (TREE_CODE (a2) == UNION_TYPE
1290 && (TYPE_NAME (a2) == 0
1291 || TYPE_TRANSPARENT_UNION (a2))
1292 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1293 && tree_int_cst_equal (TYPE_SIZE (a2),
1297 for (memb = TYPE_FIELDS (a2);
1298 memb; memb = TREE_CHAIN (memb))
1300 tree mv3 = TREE_TYPE (memb);
1301 if (mv3 && mv3 != error_mark_node
1302 && TREE_CODE (mv3) != ARRAY_TYPE)
1303 mv3 = TYPE_MAIN_VARIANT (mv3);
1304 if (comptypes_internal (mv3, mv1))
1314 /* comptypes said ok, but record if it said to warn. */
1318 args1 = TREE_CHAIN (args1);
1319 args2 = TREE_CHAIN (args2);
1323 /* Compute the size to increment a pointer by. */
1326 c_size_in_bytes (tree type)
1328 enum tree_code code = TREE_CODE (type);
1330 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1331 return size_one_node;
1333 if (!COMPLETE_OR_VOID_TYPE_P (type))
1335 error ("arithmetic on pointer to an incomplete type");
1336 return size_one_node;
1339 /* Convert in case a char is more than one unit. */
1340 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1341 size_int (TYPE_PRECISION (char_type_node)
1345 /* Return either DECL or its known constant value (if it has one). */
1348 decl_constant_value (tree decl)
1350 if (/* Don't change a variable array bound or initial value to a constant
1351 in a place where a variable is invalid. Note that DECL_INITIAL
1352 isn't valid for a PARM_DECL. */
1353 current_function_decl != 0
1354 && TREE_CODE (decl) != PARM_DECL
1355 && !TREE_THIS_VOLATILE (decl)
1356 && TREE_READONLY (decl)
1357 && DECL_INITIAL (decl) != 0
1358 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1359 /* This is invalid if initial value is not constant.
1360 If it has either a function call, a memory reference,
1361 or a variable, then re-evaluating it could give different results. */
1362 && TREE_CONSTANT (DECL_INITIAL (decl))
1363 /* Check for cases where this is sub-optimal, even though valid. */
1364 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1365 return DECL_INITIAL (decl);
1369 /* Return either DECL or its known constant value (if it has one), but
1370 return DECL if pedantic or DECL has mode BLKmode. This is for
1371 bug-compatibility with the old behavior of decl_constant_value
1372 (before GCC 3.0); every use of this function is a bug and it should
1373 be removed before GCC 3.1. It is not appropriate to use pedantic
1374 in a way that affects optimization, and BLKmode is probably not the
1375 right test for avoiding misoptimizations either. */
1378 decl_constant_value_for_broken_optimization (tree decl)
1382 if (pedantic || DECL_MODE (decl) == BLKmode)
1385 ret = decl_constant_value (decl);
1386 /* Avoid unwanted tree sharing between the initializer and current
1387 function's body where the tree can be modified e.g. by the
1389 if (ret != decl && TREE_STATIC (decl))
1390 ret = unshare_expr (ret);
1394 /* Convert the array expression EXP to a pointer. */
1396 array_to_pointer_conversion (tree exp)
1398 tree orig_exp = exp;
1399 tree type = TREE_TYPE (exp);
1401 tree restype = TREE_TYPE (type);
1404 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1406 STRIP_TYPE_NOPS (exp);
1408 if (TREE_NO_WARNING (orig_exp))
1409 TREE_NO_WARNING (exp) = 1;
1411 ptrtype = build_pointer_type (restype);
1413 if (TREE_CODE (exp) == INDIRECT_REF)
1414 return convert (ptrtype, TREE_OPERAND (exp, 0));
1416 if (TREE_CODE (exp) == VAR_DECL)
1418 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1419 ADDR_EXPR because it's the best way of representing what
1420 happens in C when we take the address of an array and place
1421 it in a pointer to the element type. */
1422 adr = build1 (ADDR_EXPR, ptrtype, exp);
1423 if (!c_mark_addressable (exp))
1424 return error_mark_node;
1425 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1429 /* This way is better for a COMPONENT_REF since it can
1430 simplify the offset for a component. */
1431 adr = build_unary_op (ADDR_EXPR, exp, 1);
1432 return convert (ptrtype, adr);
1435 /* Convert the function expression EXP to a pointer. */
1437 function_to_pointer_conversion (tree exp)
1439 tree orig_exp = exp;
1441 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1443 STRIP_TYPE_NOPS (exp);
1445 if (TREE_NO_WARNING (orig_exp))
1446 TREE_NO_WARNING (exp) = 1;
1448 return build_unary_op (ADDR_EXPR, exp, 0);
1451 /* Perform the default conversion of arrays and functions to pointers.
1452 Return the result of converting EXP. For any other expression, just
1453 return EXP after removing NOPs. */
1456 default_function_array_conversion (struct c_expr exp)
1458 tree orig_exp = exp.value;
1459 tree type = TREE_TYPE (exp.value);
1460 enum tree_code code = TREE_CODE (type);
1466 bool not_lvalue = false;
1467 bool lvalue_array_p;
1469 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1470 || TREE_CODE (exp.value) == NOP_EXPR)
1471 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1473 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1475 exp.value = TREE_OPERAND (exp.value, 0);
1478 if (TREE_NO_WARNING (orig_exp))
1479 TREE_NO_WARNING (exp.value) = 1;
1481 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1482 if (!flag_isoc99 && !lvalue_array_p)
1484 /* Before C99, non-lvalue arrays do not decay to pointers.
1485 Normally, using such an array would be invalid; but it can
1486 be used correctly inside sizeof or as a statement expression.
1487 Thus, do not give an error here; an error will result later. */
1491 exp.value = array_to_pointer_conversion (exp.value);
1495 exp.value = function_to_pointer_conversion (exp.value);
1498 STRIP_TYPE_NOPS (exp.value);
1499 if (TREE_NO_WARNING (orig_exp))
1500 TREE_NO_WARNING (exp.value) = 1;
1508 /* EXP is an expression of integer type. Apply the integer promotions
1509 to it and return the promoted value. */
1512 perform_integral_promotions (tree exp)
1514 tree type = TREE_TYPE (exp);
1515 enum tree_code code = TREE_CODE (type);
1517 gcc_assert (INTEGRAL_TYPE_P (type));
1519 /* Normally convert enums to int,
1520 but convert wide enums to something wider. */
1521 if (code == ENUMERAL_TYPE)
1523 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1524 TYPE_PRECISION (integer_type_node)),
1525 ((TYPE_PRECISION (type)
1526 >= TYPE_PRECISION (integer_type_node))
1527 && TYPE_UNSIGNED (type)));
1529 return convert (type, exp);
1532 /* ??? This should no longer be needed now bit-fields have their
1534 if (TREE_CODE (exp) == COMPONENT_REF
1535 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1536 /* If it's thinner than an int, promote it like a
1537 c_promoting_integer_type_p, otherwise leave it alone. */
1538 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1539 TYPE_PRECISION (integer_type_node)))
1540 return convert (integer_type_node, exp);
1542 if (c_promoting_integer_type_p (type))
1544 /* Preserve unsignedness if not really getting any wider. */
1545 if (TYPE_UNSIGNED (type)
1546 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1547 return convert (unsigned_type_node, exp);
1549 return convert (integer_type_node, exp);
1556 /* Perform default promotions for C data used in expressions.
1557 Enumeral types or short or char are converted to int.
1558 In addition, manifest constants symbols are replaced by their values. */
1561 default_conversion (tree exp)
1564 tree type = TREE_TYPE (exp);
1565 enum tree_code code = TREE_CODE (type);
1567 /* Functions and arrays have been converted during parsing. */
1568 gcc_assert (code != FUNCTION_TYPE);
1569 if (code == ARRAY_TYPE)
1572 /* Constants can be used directly unless they're not loadable. */
1573 if (TREE_CODE (exp) == CONST_DECL)
1574 exp = DECL_INITIAL (exp);
1576 /* Replace a nonvolatile const static variable with its value unless
1577 it is an array, in which case we must be sure that taking the
1578 address of the array produces consistent results. */
1579 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1581 exp = decl_constant_value_for_broken_optimization (exp);
1582 type = TREE_TYPE (exp);
1585 /* Strip no-op conversions. */
1587 STRIP_TYPE_NOPS (exp);
1589 if (TREE_NO_WARNING (orig_exp))
1590 TREE_NO_WARNING (exp) = 1;
1592 if (INTEGRAL_TYPE_P (type))
1593 return perform_integral_promotions (exp);
1595 if (code == VOID_TYPE)
1597 error ("void value not ignored as it ought to be");
1598 return error_mark_node;
1603 /* Look up COMPONENT in a structure or union DECL.
1605 If the component name is not found, returns NULL_TREE. Otherwise,
1606 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1607 stepping down the chain to the component, which is in the last
1608 TREE_VALUE of the list. Normally the list is of length one, but if
1609 the component is embedded within (nested) anonymous structures or
1610 unions, the list steps down the chain to the component. */
1613 lookup_field (tree decl, tree component)
1615 tree type = TREE_TYPE (decl);
1618 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1619 to the field elements. Use a binary search on this array to quickly
1620 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1621 will always be set for structures which have many elements. */
1623 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1626 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1628 field = TYPE_FIELDS (type);
1630 top = TYPE_LANG_SPECIFIC (type)->s->len;
1631 while (top - bot > 1)
1633 half = (top - bot + 1) >> 1;
1634 field = field_array[bot+half];
1636 if (DECL_NAME (field) == NULL_TREE)
1638 /* Step through all anon unions in linear fashion. */
1639 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1641 field = field_array[bot++];
1642 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1643 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1645 tree anon = lookup_field (field, component);
1648 return tree_cons (NULL_TREE, field, anon);
1652 /* Entire record is only anon unions. */
1656 /* Restart the binary search, with new lower bound. */
1660 if (DECL_NAME (field) == component)
1662 if (DECL_NAME (field) < component)
1668 if (DECL_NAME (field_array[bot]) == component)
1669 field = field_array[bot];
1670 else if (DECL_NAME (field) != component)
1675 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1677 if (DECL_NAME (field) == NULL_TREE
1678 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1679 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1681 tree anon = lookup_field (field, component);
1684 return tree_cons (NULL_TREE, field, anon);
1687 if (DECL_NAME (field) == component)
1691 if (field == NULL_TREE)
1695 return tree_cons (NULL_TREE, field, NULL_TREE);
1698 /* Make an expression to refer to the COMPONENT field of
1699 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1702 build_component_ref (tree datum, tree component)
1704 tree type = TREE_TYPE (datum);
1705 enum tree_code code = TREE_CODE (type);
1709 if (!objc_is_public (datum, component))
1710 return error_mark_node;
1712 /* See if there is a field or component with name COMPONENT. */
1714 if (code == RECORD_TYPE || code == UNION_TYPE)
1716 if (!COMPLETE_TYPE_P (type))
1718 c_incomplete_type_error (NULL_TREE, type);
1719 return error_mark_node;
1722 field = lookup_field (datum, component);
1726 error ("%qT has no member named %qE", type, component);
1727 return error_mark_node;
1730 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1731 This might be better solved in future the way the C++ front
1732 end does it - by giving the anonymous entities each a
1733 separate name and type, and then have build_component_ref
1734 recursively call itself. We can't do that here. */
1737 tree subdatum = TREE_VALUE (field);
1739 if (TREE_TYPE (subdatum) == error_mark_node)
1740 return error_mark_node;
1742 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1744 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1745 TREE_READONLY (ref) = 1;
1746 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1747 TREE_THIS_VOLATILE (ref) = 1;
1749 if (TREE_DEPRECATED (subdatum))
1750 warn_deprecated_use (subdatum);
1754 field = TREE_CHAIN (field);
1760 else if (code != ERROR_MARK)
1761 error ("request for member %qE in something not a structure or union",
1764 return error_mark_node;
1767 /* Given an expression PTR for a pointer, return an expression
1768 for the value pointed to.
1769 ERRORSTRING is the name of the operator to appear in error messages. */
1772 build_indirect_ref (tree ptr, const char *errorstring)
1774 tree pointer = default_conversion (ptr);
1775 tree type = TREE_TYPE (pointer);
1777 if (TREE_CODE (type) == POINTER_TYPE)
1779 if (TREE_CODE (pointer) == ADDR_EXPR
1780 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1781 == TREE_TYPE (type)))
1782 return TREE_OPERAND (pointer, 0);
1785 tree t = TREE_TYPE (type);
1788 ref = build1 (INDIRECT_REF, t, pointer);
1790 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1792 error ("dereferencing pointer to incomplete type");
1793 return error_mark_node;
1795 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1796 warning (0, "dereferencing %<void *%> pointer");
1798 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1799 so that we get the proper error message if the result is used
1800 to assign to. Also, &* is supposed to be a no-op.
1801 And ANSI C seems to specify that the type of the result
1802 should be the const type. */
1803 /* A de-reference of a pointer to const is not a const. It is valid
1804 to change it via some other pointer. */
1805 TREE_READONLY (ref) = TYPE_READONLY (t);
1806 TREE_SIDE_EFFECTS (ref)
1807 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1808 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1812 else if (TREE_CODE (pointer) != ERROR_MARK)
1813 error ("invalid type argument of %qs", errorstring);
1814 return error_mark_node;
1817 /* This handles expressions of the form "a[i]", which denotes
1820 This is logically equivalent in C to *(a+i), but we may do it differently.
1821 If A is a variable or a member, we generate a primitive ARRAY_REF.
1822 This avoids forcing the array out of registers, and can work on
1823 arrays that are not lvalues (for example, members of structures returned
1827 build_array_ref (tree array, tree index)
1829 bool swapped = false;
1830 if (TREE_TYPE (array) == error_mark_node
1831 || TREE_TYPE (index) == error_mark_node)
1832 return error_mark_node;
1834 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1835 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1838 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1839 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1841 error ("subscripted value is neither array nor pointer");
1842 return error_mark_node;
1850 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1852 error ("array subscript is not an integer");
1853 return error_mark_node;
1856 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1858 error ("subscripted value is pointer to function");
1859 return error_mark_node;
1862 /* ??? Existing practice has been to warn only when the char
1863 index is syntactically the index, not for char[array]. */
1865 warn_array_subscript_with_type_char (index);
1867 /* Apply default promotions *after* noticing character types. */
1868 index = default_conversion (index);
1870 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1872 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1876 /* An array that is indexed by a non-constant
1877 cannot be stored in a register; we must be able to do
1878 address arithmetic on its address.
1879 Likewise an array of elements of variable size. */
1880 if (TREE_CODE (index) != INTEGER_CST
1881 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1882 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1884 if (!c_mark_addressable (array))
1885 return error_mark_node;
1887 /* An array that is indexed by a constant value which is not within
1888 the array bounds cannot be stored in a register either; because we
1889 would get a crash in store_bit_field/extract_bit_field when trying
1890 to access a non-existent part of the register. */
1891 if (TREE_CODE (index) == INTEGER_CST
1892 && TYPE_DOMAIN (TREE_TYPE (array))
1893 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1895 if (!c_mark_addressable (array))
1896 return error_mark_node;
1902 while (TREE_CODE (foo) == COMPONENT_REF)
1903 foo = TREE_OPERAND (foo, 0);
1904 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1905 pedwarn ("ISO C forbids subscripting %<register%> array");
1906 else if (!flag_isoc99 && !lvalue_p (foo))
1907 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1910 type = TREE_TYPE (TREE_TYPE (array));
1911 if (TREE_CODE (type) != ARRAY_TYPE)
1912 type = TYPE_MAIN_VARIANT (type);
1913 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1914 /* Array ref is const/volatile if the array elements are
1915 or if the array is. */
1916 TREE_READONLY (rval)
1917 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1918 | TREE_READONLY (array));
1919 TREE_SIDE_EFFECTS (rval)
1920 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1921 | TREE_SIDE_EFFECTS (array));
1922 TREE_THIS_VOLATILE (rval)
1923 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1924 /* This was added by rms on 16 Nov 91.
1925 It fixes vol struct foo *a; a->elts[1]
1926 in an inline function.
1927 Hope it doesn't break something else. */
1928 | TREE_THIS_VOLATILE (array));
1929 return require_complete_type (fold (rval));
1933 tree ar = default_conversion (array);
1935 if (ar == error_mark_node)
1938 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1939 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1941 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1946 /* Build an external reference to identifier ID. FUN indicates
1947 whether this will be used for a function call. LOC is the source
1948 location of the identifier. */
1950 build_external_ref (tree id, int fun, location_t loc)
1953 tree decl = lookup_name (id);
1955 /* In Objective-C, an instance variable (ivar) may be preferred to
1956 whatever lookup_name() found. */
1957 decl = objc_lookup_ivar (decl, id);
1959 if (decl && decl != error_mark_node)
1962 /* Implicit function declaration. */
1963 ref = implicitly_declare (id);
1964 else if (decl == error_mark_node)
1965 /* Don't complain about something that's already been
1966 complained about. */
1967 return error_mark_node;
1970 undeclared_variable (id, loc);
1971 return error_mark_node;
1974 if (TREE_TYPE (ref) == error_mark_node)
1975 return error_mark_node;
1977 if (TREE_DEPRECATED (ref))
1978 warn_deprecated_use (ref);
1980 if (!skip_evaluation)
1981 assemble_external (ref);
1982 TREE_USED (ref) = 1;
1984 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1986 if (!in_sizeof && !in_typeof)
1987 C_DECL_USED (ref) = 1;
1988 else if (DECL_INITIAL (ref) == 0
1989 && DECL_EXTERNAL (ref)
1990 && !TREE_PUBLIC (ref))
1991 record_maybe_used_decl (ref);
1994 if (TREE_CODE (ref) == CONST_DECL)
1996 ref = DECL_INITIAL (ref);
1997 TREE_CONSTANT (ref) = 1;
1998 TREE_INVARIANT (ref) = 1;
2000 else if (current_function_decl != 0
2001 && !DECL_FILE_SCOPE_P (current_function_decl)
2002 && (TREE_CODE (ref) == VAR_DECL
2003 || TREE_CODE (ref) == PARM_DECL
2004 || TREE_CODE (ref) == FUNCTION_DECL))
2006 tree context = decl_function_context (ref);
2008 if (context != 0 && context != current_function_decl)
2009 DECL_NONLOCAL (ref) = 1;
2015 /* Record details of decls possibly used inside sizeof or typeof. */
2016 struct maybe_used_decl
2020 /* The level seen at (in_sizeof + in_typeof). */
2022 /* The next one at this level or above, or NULL. */
2023 struct maybe_used_decl *next;
2026 static struct maybe_used_decl *maybe_used_decls;
2028 /* Record that DECL, an undefined static function reference seen
2029 inside sizeof or typeof, might be used if the operand of sizeof is
2030 a VLA type or the operand of typeof is a variably modified
2034 record_maybe_used_decl (tree decl)
2036 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2038 t->level = in_sizeof + in_typeof;
2039 t->next = maybe_used_decls;
2040 maybe_used_decls = t;
2043 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2044 USED is false, just discard them. If it is true, mark them used
2045 (if no longer inside sizeof or typeof) or move them to the next
2046 level up (if still inside sizeof or typeof). */
2049 pop_maybe_used (bool used)
2051 struct maybe_used_decl *p = maybe_used_decls;
2052 int cur_level = in_sizeof + in_typeof;
2053 while (p && p->level > cur_level)
2058 C_DECL_USED (p->decl) = 1;
2060 p->level = cur_level;
2064 if (!used || cur_level == 0)
2065 maybe_used_decls = p;
2068 /* Return the result of sizeof applied to EXPR. */
2071 c_expr_sizeof_expr (struct c_expr expr)
2074 if (expr.value == error_mark_node)
2076 ret.value = error_mark_node;
2077 ret.original_code = ERROR_MARK;
2078 pop_maybe_used (false);
2082 ret.value = c_sizeof (TREE_TYPE (expr.value));
2083 ret.original_code = ERROR_MARK;
2084 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2089 /* Return the result of sizeof applied to T, a structure for the type
2090 name passed to sizeof (rather than the type itself). */
2093 c_expr_sizeof_type (struct c_type_name *t)
2097 type = groktypename (t);
2098 ret.value = c_sizeof (type);
2099 ret.original_code = ERROR_MARK;
2100 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
2104 /* Build a function call to function FUNCTION with parameters PARAMS.
2105 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2106 TREE_VALUE of each node is a parameter-expression.
2107 FUNCTION's data type may be a function type or a pointer-to-function. */
2110 build_function_call (tree function, tree params)
2112 tree fntype, fundecl = 0;
2113 tree coerced_params;
2114 tree name = NULL_TREE, result;
2117 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2118 STRIP_TYPE_NOPS (function);
2120 /* Convert anything with function type to a pointer-to-function. */
2121 if (TREE_CODE (function) == FUNCTION_DECL)
2123 /* Implement type-directed function overloading for builtins.
2124 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2125 handle all the type checking. The result is a complete expression
2126 that implements this function call. */
2127 tem = resolve_overloaded_builtin (function, params);
2131 name = DECL_NAME (function);
2134 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2135 function = function_to_pointer_conversion (function);
2137 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2138 expressions, like those used for ObjC messenger dispatches. */
2139 function = objc_rewrite_function_call (function, params);
2141 fntype = TREE_TYPE (function);
2143 if (TREE_CODE (fntype) == ERROR_MARK)
2144 return error_mark_node;
2146 if (!(TREE_CODE (fntype) == POINTER_TYPE
2147 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2149 error ("called object %qE is not a function", function);
2150 return error_mark_node;
2153 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2154 current_function_returns_abnormally = 1;
2156 /* fntype now gets the type of function pointed to. */
2157 fntype = TREE_TYPE (fntype);
2159 /* Check that the function is called through a compatible prototype.
2160 If it is not, replace the call by a trap, wrapped up in a compound
2161 expression if necessary. This has the nice side-effect to prevent
2162 the tree-inliner from generating invalid assignment trees which may
2163 blow up in the RTL expander later. */
2164 if (TREE_CODE (function) == NOP_EXPR
2165 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2166 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2167 && !comptypes (fntype, TREE_TYPE (tem)))
2169 tree return_type = TREE_TYPE (fntype);
2170 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2173 /* This situation leads to run-time undefined behavior. We can't,
2174 therefore, simply error unless we can prove that all possible
2175 executions of the program must execute the code. */
2176 warning (0, "function called through a non-compatible type");
2178 /* We can, however, treat "undefined" any way we please.
2179 Call abort to encourage the user to fix the program. */
2180 inform ("if this code is reached, the program will abort");
2182 if (VOID_TYPE_P (return_type))
2188 if (AGGREGATE_TYPE_P (return_type))
2189 rhs = build_compound_literal (return_type,
2190 build_constructor (return_type, 0));
2192 rhs = fold_build1 (NOP_EXPR, return_type, integer_zero_node);
2194 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2198 /* Convert the parameters to the types declared in the
2199 function prototype, or apply default promotions. */
2202 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2204 if (coerced_params == error_mark_node)
2205 return error_mark_node;
2207 /* Check that the arguments to the function are valid. */
2209 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2210 TYPE_ARG_TYPES (fntype));
2212 if (require_constant_value)
2214 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2215 function, coerced_params, NULL_TREE);
2217 if (TREE_CONSTANT (result)
2218 && (name == NULL_TREE
2219 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2220 pedwarn_init ("initializer element is not constant");
2223 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2224 function, coerced_params, NULL_TREE);
2226 if (VOID_TYPE_P (TREE_TYPE (result)))
2228 return require_complete_type (result);
2231 /* Convert the argument expressions in the list VALUES
2232 to the types in the list TYPELIST. The result is a list of converted
2233 argument expressions, unless there are too few arguments in which
2234 case it is error_mark_node.
2236 If TYPELIST is exhausted, or when an element has NULL as its type,
2237 perform the default conversions.
2239 PARMLIST is the chain of parm decls for the function being called.
2240 It may be 0, if that info is not available.
2241 It is used only for generating error messages.
2243 FUNCTION is a tree for the called function. It is used only for
2244 error messages, where it is formatted with %qE.
2246 This is also where warnings about wrong number of args are generated.
2248 Both VALUES and the returned value are chains of TREE_LIST nodes
2249 with the elements of the list in the TREE_VALUE slots of those nodes. */
2252 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2254 tree typetail, valtail;
2259 /* Change pointer to function to the function itself for
2261 if (TREE_CODE (function) == ADDR_EXPR
2262 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2263 function = TREE_OPERAND (function, 0);
2265 /* Handle an ObjC selector specially for diagnostics. */
2266 selector = objc_message_selector ();
2268 /* Scan the given expressions and types, producing individual
2269 converted arguments and pushing them on RESULT in reverse order. */
2271 for (valtail = values, typetail = typelist, parmnum = 0;
2273 valtail = TREE_CHAIN (valtail), parmnum++)
2275 tree type = typetail ? TREE_VALUE (typetail) : 0;
2276 tree val = TREE_VALUE (valtail);
2277 tree rname = function;
2278 int argnum = parmnum + 1;
2279 const char *invalid_func_diag;
2281 if (type == void_type_node)
2283 error ("too many arguments to function %qE", function);
2287 if (selector && argnum > 2)
2293 STRIP_TYPE_NOPS (val);
2295 val = require_complete_type (val);
2299 /* Formal parm type is specified by a function prototype. */
2302 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2304 error ("type of formal parameter %d is incomplete", parmnum + 1);
2309 /* Optionally warn about conversions that
2310 differ from the default conversions. */
2311 if (warn_conversion || warn_traditional)
2313 unsigned int formal_prec = TYPE_PRECISION (type);
2315 if (INTEGRAL_TYPE_P (type)
2316 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2317 warning (0, "passing argument %d of %qE as integer "
2318 "rather than floating due to prototype",
2320 if (INTEGRAL_TYPE_P (type)
2321 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2322 warning (0, "passing argument %d of %qE as integer "
2323 "rather than complex due to prototype",
2325 else if (TREE_CODE (type) == COMPLEX_TYPE
2326 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2327 warning (0, "passing argument %d of %qE as complex "
2328 "rather than floating due to prototype",
2330 else if (TREE_CODE (type) == REAL_TYPE
2331 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2332 warning (0, "passing argument %d of %qE as floating "
2333 "rather than integer due to prototype",
2335 else if (TREE_CODE (type) == COMPLEX_TYPE
2336 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2337 warning (0, "passing argument %d of %qE as complex "
2338 "rather than integer due to prototype",
2340 else if (TREE_CODE (type) == REAL_TYPE
2341 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2342 warning (0, "passing argument %d of %qE as floating "
2343 "rather than complex due to prototype",
2345 /* ??? At some point, messages should be written about
2346 conversions between complex types, but that's too messy
2348 else if (TREE_CODE (type) == REAL_TYPE
2349 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2351 /* Warn if any argument is passed as `float',
2352 since without a prototype it would be `double'. */
2353 if (formal_prec == TYPE_PRECISION (float_type_node))
2354 warning (0, "passing argument %d of %qE as %<float%> "
2355 "rather than %<double%> due to prototype",
2358 /* Detect integer changing in width or signedness.
2359 These warnings are only activated with
2360 -Wconversion, not with -Wtraditional. */
2361 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2362 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2364 tree would_have_been = default_conversion (val);
2365 tree type1 = TREE_TYPE (would_have_been);
2367 if (TREE_CODE (type) == ENUMERAL_TYPE
2368 && (TYPE_MAIN_VARIANT (type)
2369 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2370 /* No warning if function asks for enum
2371 and the actual arg is that enum type. */
2373 else if (formal_prec != TYPE_PRECISION (type1))
2374 warning (OPT_Wconversion, "passing argument %d of %qE "
2375 "with different width due to prototype",
2377 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2379 /* Don't complain if the formal parameter type
2380 is an enum, because we can't tell now whether
2381 the value was an enum--even the same enum. */
2382 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2384 else if (TREE_CODE (val) == INTEGER_CST
2385 && int_fits_type_p (val, type))
2386 /* Change in signedness doesn't matter
2387 if a constant value is unaffected. */
2389 /* If the value is extended from a narrower
2390 unsigned type, it doesn't matter whether we
2391 pass it as signed or unsigned; the value
2392 certainly is the same either way. */
2393 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2394 && TYPE_UNSIGNED (TREE_TYPE (val)))
2396 else if (TYPE_UNSIGNED (type))
2397 warning (OPT_Wconversion, "passing argument %d of %qE "
2398 "as unsigned due to prototype",
2401 warning (OPT_Wconversion, "passing argument %d of %qE "
2402 "as signed due to prototype", argnum, rname);
2406 parmval = convert_for_assignment (type, val, ic_argpass,
2410 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2411 && INTEGRAL_TYPE_P (type)
2412 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2413 parmval = default_conversion (parmval);
2415 result = tree_cons (NULL_TREE, parmval, result);
2417 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2418 && (TYPE_PRECISION (TREE_TYPE (val))
2419 < TYPE_PRECISION (double_type_node)))
2420 /* Convert `float' to `double'. */
2421 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2422 else if ((invalid_func_diag =
2423 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2425 error (invalid_func_diag);
2426 return error_mark_node;
2429 /* Convert `short' and `char' to full-size `int'. */
2430 result = tree_cons (NULL_TREE, default_conversion (val), result);
2433 typetail = TREE_CHAIN (typetail);
2436 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2438 error ("too few arguments to function %qE", function);
2439 return error_mark_node;
2442 return nreverse (result);
2445 /* This is the entry point used by the parser to build unary operators
2446 in the input. CODE, a tree_code, specifies the unary operator, and
2447 ARG is the operand. For unary plus, the C parser currently uses
2448 CONVERT_EXPR for code. */
2451 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2453 struct c_expr result;
2455 result.original_code = ERROR_MARK;
2456 result.value = build_unary_op (code, arg.value, 0);
2457 overflow_warning (result.value);
2461 /* This is the entry point used by the parser to build binary operators
2462 in the input. CODE, a tree_code, specifies the binary operator, and
2463 ARG1 and ARG2 are the operands. In addition to constructing the
2464 expression, we check for operands that were written with other binary
2465 operators in a way that is likely to confuse the user. */
2468 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2471 struct c_expr result;
2473 enum tree_code code1 = arg1.original_code;
2474 enum tree_code code2 = arg2.original_code;
2476 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2477 result.original_code = code;
2479 if (TREE_CODE (result.value) == ERROR_MARK)
2482 /* Check for cases such as x+y<<z which users are likely
2484 if (warn_parentheses)
2486 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2488 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2489 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2490 warning (OPT_Wparentheses,
2491 "suggest parentheses around + or - inside shift");
2494 if (code == TRUTH_ORIF_EXPR)
2496 if (code1 == TRUTH_ANDIF_EXPR
2497 || code2 == TRUTH_ANDIF_EXPR)
2498 warning (OPT_Wparentheses,
2499 "suggest parentheses around && within ||");
2502 if (code == BIT_IOR_EXPR)
2504 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2505 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2506 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2507 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2508 warning (OPT_Wparentheses,
2509 "suggest parentheses around arithmetic in operand of |");
2510 /* Check cases like x|y==z */
2511 if (TREE_CODE_CLASS (code1) == tcc_comparison
2512 || TREE_CODE_CLASS (code2) == tcc_comparison)
2513 warning (OPT_Wparentheses,
2514 "suggest parentheses around comparison in operand of |");
2517 if (code == BIT_XOR_EXPR)
2519 if (code1 == BIT_AND_EXPR
2520 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2521 || code2 == BIT_AND_EXPR
2522 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2523 warning (OPT_Wparentheses,
2524 "suggest parentheses around arithmetic in operand of ^");
2525 /* Check cases like x^y==z */
2526 if (TREE_CODE_CLASS (code1) == tcc_comparison
2527 || TREE_CODE_CLASS (code2) == tcc_comparison)
2528 warning (OPT_Wparentheses,
2529 "suggest parentheses around comparison in operand of ^");
2532 if (code == BIT_AND_EXPR)
2534 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2535 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2536 warning (OPT_Wparentheses,
2537 "suggest parentheses around + or - in operand of &");
2538 /* Check cases like x&y==z */
2539 if (TREE_CODE_CLASS (code1) == tcc_comparison
2540 || TREE_CODE_CLASS (code2) == tcc_comparison)
2541 warning (OPT_Wparentheses,
2542 "suggest parentheses around comparison in operand of &");
2544 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2545 if (TREE_CODE_CLASS (code) == tcc_comparison
2546 && (TREE_CODE_CLASS (code1) == tcc_comparison
2547 || TREE_CODE_CLASS (code2) == tcc_comparison))
2548 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2549 "have their mathematical meaning");
2553 unsigned_conversion_warning (result.value, arg1.value);
2554 unsigned_conversion_warning (result.value, arg2.value);
2555 overflow_warning (result.value);
2560 /* Return a tree for the difference of pointers OP0 and OP1.
2561 The resulting tree has type int. */
2564 pointer_diff (tree op0, tree op1)
2566 tree restype = ptrdiff_type_node;
2568 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2569 tree con0, con1, lit0, lit1;
2570 tree orig_op1 = op1;
2572 if (pedantic || warn_pointer_arith)
2574 if (TREE_CODE (target_type) == VOID_TYPE)
2575 pedwarn ("pointer of type %<void *%> used in subtraction");
2576 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2577 pedwarn ("pointer to a function used in subtraction");
2580 /* If the conversion to ptrdiff_type does anything like widening or
2581 converting a partial to an integral mode, we get a convert_expression
2582 that is in the way to do any simplifications.
2583 (fold-const.c doesn't know that the extra bits won't be needed.
2584 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2585 different mode in place.)
2586 So first try to find a common term here 'by hand'; we want to cover
2587 at least the cases that occur in legal static initializers. */
2588 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2589 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2591 if (TREE_CODE (con0) == PLUS_EXPR)
2593 lit0 = TREE_OPERAND (con0, 1);
2594 con0 = TREE_OPERAND (con0, 0);
2597 lit0 = integer_zero_node;
2599 if (TREE_CODE (con1) == PLUS_EXPR)
2601 lit1 = TREE_OPERAND (con1, 1);
2602 con1 = TREE_OPERAND (con1, 0);
2605 lit1 = integer_zero_node;
2607 if (operand_equal_p (con0, con1, 0))
2614 /* First do the subtraction as integers;
2615 then drop through to build the divide operator.
2616 Do not do default conversions on the minus operator
2617 in case restype is a short type. */
2619 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2620 convert (restype, op1), 0);
2621 /* This generates an error if op1 is pointer to incomplete type. */
2622 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2623 error ("arithmetic on pointer to an incomplete type");
2625 /* This generates an error if op0 is pointer to incomplete type. */
2626 op1 = c_size_in_bytes (target_type);
2628 /* Divide by the size, in easiest possible way. */
2629 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2632 /* Construct and perhaps optimize a tree representation
2633 for a unary operation. CODE, a tree_code, specifies the operation
2634 and XARG is the operand.
2635 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2636 the default promotions (such as from short to int).
2637 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2638 allows non-lvalues; this is only used to handle conversion of non-lvalue
2639 arrays to pointers in C99. */
2642 build_unary_op (enum tree_code code, tree xarg, int flag)
2644 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2647 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2649 int noconvert = flag;
2650 const char *invalid_op_diag;
2652 if (typecode == ERROR_MARK)
2653 return error_mark_node;
2654 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2655 typecode = INTEGER_TYPE;
2657 if ((invalid_op_diag
2658 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2660 error (invalid_op_diag);
2661 return error_mark_node;
2667 /* This is used for unary plus, because a CONVERT_EXPR
2668 is enough to prevent anybody from looking inside for
2669 associativity, but won't generate any code. */
2670 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2671 || typecode == COMPLEX_TYPE
2672 || typecode == VECTOR_TYPE))
2674 error ("wrong type argument to unary plus");
2675 return error_mark_node;
2677 else if (!noconvert)
2678 arg = default_conversion (arg);
2679 arg = non_lvalue (arg);
2683 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2684 || typecode == COMPLEX_TYPE
2685 || typecode == VECTOR_TYPE))
2687 error ("wrong type argument to unary minus");
2688 return error_mark_node;
2690 else if (!noconvert)
2691 arg = default_conversion (arg);
2695 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2698 arg = default_conversion (arg);
2700 else if (typecode == COMPLEX_TYPE)
2704 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2706 arg = default_conversion (arg);
2710 error ("wrong type argument to bit-complement");
2711 return error_mark_node;
2716 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2718 error ("wrong type argument to abs");
2719 return error_mark_node;
2721 else if (!noconvert)
2722 arg = default_conversion (arg);
2726 /* Conjugating a real value is a no-op, but allow it anyway. */
2727 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2728 || typecode == COMPLEX_TYPE))
2730 error ("wrong type argument to conjugation");
2731 return error_mark_node;
2733 else if (!noconvert)
2734 arg = default_conversion (arg);
2737 case TRUTH_NOT_EXPR:
2738 if (typecode != INTEGER_TYPE
2739 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2740 && typecode != COMPLEX_TYPE)
2742 error ("wrong type argument to unary exclamation mark");
2743 return error_mark_node;
2745 arg = c_objc_common_truthvalue_conversion (arg);
2746 return invert_truthvalue (arg);
2752 if (TREE_CODE (arg) == COMPLEX_CST)
2753 return TREE_REALPART (arg);
2754 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2755 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2760 if (TREE_CODE (arg) == COMPLEX_CST)
2761 return TREE_IMAGPART (arg);
2762 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2763 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2765 return convert (TREE_TYPE (arg), integer_zero_node);
2767 case PREINCREMENT_EXPR:
2768 case POSTINCREMENT_EXPR:
2769 case PREDECREMENT_EXPR:
2770 case POSTDECREMENT_EXPR:
2772 /* Increment or decrement the real part of the value,
2773 and don't change the imaginary part. */
2774 if (typecode == COMPLEX_TYPE)
2779 pedwarn ("ISO C does not support %<++%> and %<--%>"
2780 " on complex types");
2782 arg = stabilize_reference (arg);
2783 real = build_unary_op (REALPART_EXPR, arg, 1);
2784 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2785 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2786 build_unary_op (code, real, 1), imag);
2789 /* Report invalid types. */
2791 if (typecode != POINTER_TYPE
2792 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2794 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2795 error ("wrong type argument to increment");
2797 error ("wrong type argument to decrement");
2799 return error_mark_node;
2804 tree result_type = TREE_TYPE (arg);
2806 arg = get_unwidened (arg, 0);
2807 argtype = TREE_TYPE (arg);
2809 /* Compute the increment. */
2811 if (typecode == POINTER_TYPE)
2813 /* If pointer target is an undefined struct,
2814 we just cannot know how to do the arithmetic. */
2815 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2817 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2818 error ("increment of pointer to unknown structure");
2820 error ("decrement of pointer to unknown structure");
2822 else if ((pedantic || warn_pointer_arith)
2823 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2824 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2826 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2827 pedwarn ("wrong type argument to increment");
2829 pedwarn ("wrong type argument to decrement");
2832 inc = c_size_in_bytes (TREE_TYPE (result_type));
2835 inc = integer_one_node;
2837 inc = convert (argtype, inc);
2839 /* Complain about anything else that is not a true lvalue. */
2840 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2841 || code == POSTINCREMENT_EXPR)
2844 return error_mark_node;
2846 /* Report a read-only lvalue. */
2847 if (TREE_READONLY (arg))
2848 readonly_error (arg,
2849 ((code == PREINCREMENT_EXPR
2850 || code == POSTINCREMENT_EXPR)
2851 ? lv_increment : lv_decrement));
2853 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2854 val = boolean_increment (code, arg);
2856 val = build2 (code, TREE_TYPE (arg), arg, inc);
2857 TREE_SIDE_EFFECTS (val) = 1;
2858 val = convert (result_type, val);
2859 if (TREE_CODE (val) != code)
2860 TREE_NO_WARNING (val) = 1;
2865 /* Note that this operation never does default_conversion. */
2867 /* Let &* cancel out to simplify resulting code. */
2868 if (TREE_CODE (arg) == INDIRECT_REF)
2870 /* Don't let this be an lvalue. */
2871 if (lvalue_p (TREE_OPERAND (arg, 0)))
2872 return non_lvalue (TREE_OPERAND (arg, 0));
2873 return TREE_OPERAND (arg, 0);
2876 /* For &x[y], return x+y */
2877 if (TREE_CODE (arg) == ARRAY_REF)
2879 tree op0 = TREE_OPERAND (arg, 0);
2880 if (!c_mark_addressable (op0))
2881 return error_mark_node;
2882 return build_binary_op (PLUS_EXPR,
2883 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
2884 ? array_to_pointer_conversion (op0)
2886 TREE_OPERAND (arg, 1), 1);
2889 /* Anything not already handled and not a true memory reference
2890 or a non-lvalue array is an error. */
2891 else if (typecode != FUNCTION_TYPE && !flag
2892 && !lvalue_or_else (arg, lv_addressof))
2893 return error_mark_node;
2895 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2896 argtype = TREE_TYPE (arg);
2898 /* If the lvalue is const or volatile, merge that into the type
2899 to which the address will point. Note that you can't get a
2900 restricted pointer by taking the address of something, so we
2901 only have to deal with `const' and `volatile' here. */
2902 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2903 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2904 argtype = c_build_type_variant (argtype,
2905 TREE_READONLY (arg),
2906 TREE_THIS_VOLATILE (arg));
2908 if (!c_mark_addressable (arg))
2909 return error_mark_node;
2911 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2912 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2914 argtype = build_pointer_type (argtype);
2916 /* ??? Cope with user tricks that amount to offsetof. Delete this
2917 when we have proper support for integer constant expressions. */
2918 val = get_base_address (arg);
2919 if (val && TREE_CODE (val) == INDIRECT_REF
2920 && integer_zerop (TREE_OPERAND (val, 0)))
2921 return fold_convert (argtype, fold_offsetof (arg));
2923 val = build1 (ADDR_EXPR, argtype, arg);
2932 argtype = TREE_TYPE (arg);
2933 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
2934 : fold_build1 (code, argtype, arg);
2937 /* Return nonzero if REF is an lvalue valid for this language.
2938 Lvalues can be assigned, unless their type has TYPE_READONLY.
2939 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2944 enum tree_code code = TREE_CODE (ref);
2951 return lvalue_p (TREE_OPERAND (ref, 0));
2953 case COMPOUND_LITERAL_EXPR:
2963 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2964 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2967 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2974 /* Give an error for storing in something that is 'const'. */
2977 readonly_error (tree arg, enum lvalue_use use)
2979 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
2981 /* Using this macro rather than (for example) arrays of messages
2982 ensures that all the format strings are checked at compile
2984 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
2985 : (use == lv_increment ? (I) \
2986 : (use == lv_decrement ? (D) : (AS))))
2987 if (TREE_CODE (arg) == COMPONENT_REF)
2989 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2990 readonly_error (TREE_OPERAND (arg, 0), use);
2992 error (READONLY_MSG (G_("assignment of read-only member %qD"),
2993 G_("increment of read-only member %qD"),
2994 G_("decrement of read-only member %qD"),
2995 G_("read-only member %qD used as %<asm%> output")),
2996 TREE_OPERAND (arg, 1));
2998 else if (TREE_CODE (arg) == VAR_DECL)
2999 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3000 G_("increment of read-only variable %qD"),
3001 G_("decrement of read-only variable %qD"),
3002 G_("read-only variable %qD used as %<asm%> output")),
3005 error (READONLY_MSG (G_("assignment of read-only location"),
3006 G_("increment of read-only location"),
3007 G_("decrement of read-only location"),
3008 G_("read-only location used as %<asm%> output")));
3012 /* Return nonzero if REF is an lvalue valid for this language;
3013 otherwise, print an error message and return zero. USE says
3014 how the lvalue is being used and so selects the error message. */
3017 lvalue_or_else (tree ref, enum lvalue_use use)
3019 int win = lvalue_p (ref);
3027 /* Mark EXP saying that we need to be able to take the
3028 address of it; it should not be allocated in a register.
3029 Returns true if successful. */
3032 c_mark_addressable (tree exp)
3037 switch (TREE_CODE (x))
3040 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3043 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3047 /* ... fall through ... */
3053 x = TREE_OPERAND (x, 0);
3056 case COMPOUND_LITERAL_EXPR:
3058 TREE_ADDRESSABLE (x) = 1;
3065 if (C_DECL_REGISTER (x)
3066 && DECL_NONLOCAL (x))
3068 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3071 ("global register variable %qD used in nested function", x);
3074 pedwarn ("register variable %qD used in nested function", x);
3076 else if (C_DECL_REGISTER (x))
3078 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3079 error ("address of global register variable %qD requested", x);
3081 error ("address of register variable %qD requested", x);
3087 TREE_ADDRESSABLE (x) = 1;
3094 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3097 build_conditional_expr (tree ifexp, tree op1, tree op2)
3101 enum tree_code code1;
3102 enum tree_code code2;
3103 tree result_type = NULL;
3104 tree orig_op1 = op1, orig_op2 = op2;
3106 /* Promote both alternatives. */
3108 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3109 op1 = default_conversion (op1);
3110 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3111 op2 = default_conversion (op2);
3113 if (TREE_CODE (ifexp) == ERROR_MARK
3114 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3115 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3116 return error_mark_node;
3118 type1 = TREE_TYPE (op1);
3119 code1 = TREE_CODE (type1);
3120 type2 = TREE_TYPE (op2);
3121 code2 = TREE_CODE (type2);
3123 /* C90 does not permit non-lvalue arrays in conditional expressions.
3124 In C99 they will be pointers by now. */
3125 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3127 error ("non-lvalue array in conditional expression");
3128 return error_mark_node;
3131 /* Quickly detect the usual case where op1 and op2 have the same type
3133 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3136 result_type = type1;
3138 result_type = TYPE_MAIN_VARIANT (type1);
3140 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3141 || code1 == COMPLEX_TYPE)
3142 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3143 || code2 == COMPLEX_TYPE))
3145 result_type = c_common_type (type1, type2);
3147 /* If -Wsign-compare, warn here if type1 and type2 have
3148 different signedness. We'll promote the signed to unsigned
3149 and later code won't know it used to be different.
3150 Do this check on the original types, so that explicit casts
3151 will be considered, but default promotions won't. */
3152 if (warn_sign_compare && !skip_evaluation)
3154 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3155 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3157 if (unsigned_op1 ^ unsigned_op2)
3159 /* Do not warn if the result type is signed, since the
3160 signed type will only be chosen if it can represent
3161 all the values of the unsigned type. */
3162 if (!TYPE_UNSIGNED (result_type))
3164 /* Do not warn if the signed quantity is an unsuffixed
3165 integer literal (or some static constant expression
3166 involving such literals) and it is non-negative. */
3167 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3168 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3171 warning (0, "signed and unsigned type in conditional expression");
3175 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3177 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3178 pedwarn ("ISO C forbids conditional expr with only one void side");
3179 result_type = void_type_node;
3181 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3183 if (comp_target_types (type1, type2))
3184 result_type = common_pointer_type (type1, type2);
3185 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3186 && TREE_CODE (orig_op1) != NOP_EXPR)
3187 result_type = qualify_type (type2, type1);
3188 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3189 && TREE_CODE (orig_op2) != NOP_EXPR)
3190 result_type = qualify_type (type1, type2);
3191 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3193 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3194 pedwarn ("ISO C forbids conditional expr between "
3195 "%<void *%> and function pointer");
3196 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3197 TREE_TYPE (type2)));
3199 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3201 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3202 pedwarn ("ISO C forbids conditional expr between "
3203 "%<void *%> and function pointer");
3204 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3205 TREE_TYPE (type1)));
3209 pedwarn ("pointer type mismatch in conditional expression");
3210 result_type = build_pointer_type (void_type_node);
3213 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3215 if (!integer_zerop (op2))
3216 pedwarn ("pointer/integer type mismatch in conditional expression");
3219 op2 = null_pointer_node;
3221 result_type = type1;
3223 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3225 if (!integer_zerop (op1))
3226 pedwarn ("pointer/integer type mismatch in conditional expression");
3229 op1 = null_pointer_node;
3231 result_type = type2;
3236 if (flag_cond_mismatch)
3237 result_type = void_type_node;
3240 error ("type mismatch in conditional expression");
3241 return error_mark_node;
3245 /* Merge const and volatile flags of the incoming types. */
3247 = build_type_variant (result_type,
3248 TREE_READONLY (op1) || TREE_READONLY (op2),
3249 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3251 if (result_type != TREE_TYPE (op1))
3252 op1 = convert_and_check (result_type, op1);
3253 if (result_type != TREE_TYPE (op2))
3254 op2 = convert_and_check (result_type, op2);
3256 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3259 /* Return a compound expression that performs two expressions and
3260 returns the value of the second of them. */
3263 build_compound_expr (tree expr1, tree expr2)
3265 if (!TREE_SIDE_EFFECTS (expr1))
3267 /* The left-hand operand of a comma expression is like an expression
3268 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3269 any side-effects, unless it was explicitly cast to (void). */
3270 if (warn_unused_value)
3272 if (VOID_TYPE_P (TREE_TYPE (expr1))
3273 && TREE_CODE (expr1) == CONVERT_EXPR)
3275 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3276 && TREE_CODE (expr1) == COMPOUND_EXPR
3277 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3278 ; /* (void) a, (void) b, c */
3280 warning (0, "left-hand operand of comma expression has no effect");
3284 /* With -Wunused, we should also warn if the left-hand operand does have
3285 side-effects, but computes a value which is not used. For example, in
3286 `foo() + bar(), baz()' the result of the `+' operator is not used,
3287 so we should issue a warning. */
3288 else if (warn_unused_value)
3289 warn_if_unused_value (expr1, input_location);
3291 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3294 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3297 build_c_cast (tree type, tree expr)
3301 if (type == error_mark_node || expr == error_mark_node)
3302 return error_mark_node;
3304 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3305 only in <protocol> qualifications. But when constructing cast expressions,
3306 the protocols do matter and must be kept around. */
3307 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3308 return build1 (NOP_EXPR, type, expr);
3310 type = TYPE_MAIN_VARIANT (type);
3312 if (TREE_CODE (type) == ARRAY_TYPE)
3314 error ("cast specifies array type");
3315 return error_mark_node;
3318 if (TREE_CODE (type) == FUNCTION_TYPE)
3320 error ("cast specifies function type");
3321 return error_mark_node;
3324 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3328 if (TREE_CODE (type) == RECORD_TYPE
3329 || TREE_CODE (type) == UNION_TYPE)
3330 pedwarn ("ISO C forbids casting nonscalar to the same type");
3333 else if (TREE_CODE (type) == UNION_TYPE)
3337 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3338 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3339 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3347 pedwarn ("ISO C forbids casts to union type");
3348 t = digest_init (type,
3349 build_constructor_single (type, field, value),
3351 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3352 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3355 error ("cast to union type from type not present in union");
3356 return error_mark_node;
3362 if (type == void_type_node)
3363 return build1 (CONVERT_EXPR, type, value);
3365 otype = TREE_TYPE (value);
3367 /* Optionally warn about potentially worrisome casts. */
3370 && TREE_CODE (type) == POINTER_TYPE
3371 && TREE_CODE (otype) == POINTER_TYPE)
3373 tree in_type = type;
3374 tree in_otype = otype;
3378 /* Check that the qualifiers on IN_TYPE are a superset of
3379 the qualifiers of IN_OTYPE. The outermost level of
3380 POINTER_TYPE nodes is uninteresting and we stop as soon
3381 as we hit a non-POINTER_TYPE node on either type. */
3384 in_otype = TREE_TYPE (in_otype);
3385 in_type = TREE_TYPE (in_type);
3387 /* GNU C allows cv-qualified function types. 'const'
3388 means the function is very pure, 'volatile' means it
3389 can't return. We need to warn when such qualifiers
3390 are added, not when they're taken away. */
3391 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3392 && TREE_CODE (in_type) == FUNCTION_TYPE)
3393 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3395 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3397 while (TREE_CODE (in_type) == POINTER_TYPE
3398 && TREE_CODE (in_otype) == POINTER_TYPE);
3401 warning (0, "cast adds new qualifiers to function type");
3404 /* There are qualifiers present in IN_OTYPE that are not
3405 present in IN_TYPE. */
3406 warning (0, "cast discards qualifiers from pointer target type");
3409 /* Warn about possible alignment problems. */
3410 if (STRICT_ALIGNMENT
3411 && TREE_CODE (type) == POINTER_TYPE
3412 && TREE_CODE (otype) == POINTER_TYPE
3413 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3414 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3415 /* Don't warn about opaque types, where the actual alignment
3416 restriction is unknown. */
3417 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3418 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3419 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3420 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3421 warning (OPT_Wcast_align,
3422 "cast increases required alignment of target type");
3424 if (TREE_CODE (type) == INTEGER_TYPE
3425 && TREE_CODE (otype) == POINTER_TYPE
3426 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3427 && !TREE_CONSTANT (value))
3428 warning (OPT_Wpointer_to_int_cast,
3429 "cast from pointer to integer of different size");
3431 if (TREE_CODE (value) == CALL_EXPR
3432 && TREE_CODE (type) != TREE_CODE (otype))
3433 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3434 "to non-matching type %qT", otype, type);
3436 if (TREE_CODE (type) == POINTER_TYPE
3437 && TREE_CODE (otype) == INTEGER_TYPE
3438 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3439 /* Don't warn about converting any constant. */
3440 && !TREE_CONSTANT (value))
3441 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3442 "of different size");
3444 if (flag_strict_aliasing && warn_strict_aliasing
3445 && TREE_CODE (type) == POINTER_TYPE
3446 && TREE_CODE (otype) == POINTER_TYPE
3447 && TREE_CODE (expr) == ADDR_EXPR
3448 && (DECL_P (TREE_OPERAND (expr, 0))
3449 || TREE_CODE (TREE_OPERAND (expr, 0)) == COMPONENT_REF)
3450 && !VOID_TYPE_P (TREE_TYPE (type)))
3452 /* Casting the address of an object to non void pointer. Warn
3453 if the cast breaks type based aliasing. */
3454 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3455 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
3456 "might break strict-aliasing rules");
3459 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3460 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3462 if (!alias_sets_conflict_p (set1, set2))
3463 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
3464 "pointer will break strict-aliasing rules");
3465 else if (warn_strict_aliasing > 1
3466 && !alias_sets_might_conflict_p (set1, set2))
3467 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
3468 "pointer might break strict-aliasing rules");
3472 /* If pedantic, warn for conversions between function and object
3473 pointer types, except for converting a null pointer constant
3474 to function pointer type. */
3476 && TREE_CODE (type) == POINTER_TYPE
3477 && TREE_CODE (otype) == POINTER_TYPE
3478 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3479 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3480 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3483 && TREE_CODE (type) == POINTER_TYPE
3484 && TREE_CODE (otype) == POINTER_TYPE
3485 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3486 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3487 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3488 && TREE_CODE (expr) != NOP_EXPR))
3489 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3492 value = convert (type, value);
3494 /* Ignore any integer overflow caused by the cast. */
3495 if (TREE_CODE (value) == INTEGER_CST)
3497 if (CONSTANT_CLASS_P (ovalue)
3498 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3500 /* Avoid clobbering a shared constant. */
3501 value = copy_node (value);
3502 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3503 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3505 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3506 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3507 value = build_int_cst_wide (TREE_TYPE (value),
3508 TREE_INT_CST_LOW (value),
3509 TREE_INT_CST_HIGH (value));
3513 /* Don't let a cast be an lvalue. */
3515 value = non_lvalue (value);
3520 /* Interpret a cast of expression EXPR to type TYPE. */
3522 c_cast_expr (struct c_type_name *type_name, tree expr)
3525 int saved_wsp = warn_strict_prototypes;
3527 /* This avoids warnings about unprototyped casts on
3528 integers. E.g. "#define SIG_DFL (void(*)())0". */
3529 if (TREE_CODE (expr) == INTEGER_CST)
3530 warn_strict_prototypes = 0;
3531 type = groktypename (type_name);
3532 warn_strict_prototypes = saved_wsp;
3534 return build_c_cast (type, expr);
3538 /* Build an assignment expression of lvalue LHS from value RHS.
3539 MODIFYCODE is the code for a binary operator that we use
3540 to combine the old value of LHS with RHS to get the new value.
3541 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3544 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3548 tree lhstype = TREE_TYPE (lhs);
3549 tree olhstype = lhstype;
3551 /* Types that aren't fully specified cannot be used in assignments. */
3552 lhs = require_complete_type (lhs);
3554 /* Avoid duplicate error messages from operands that had errors. */
3555 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3556 return error_mark_node;
3558 STRIP_TYPE_NOPS (rhs);
3562 /* If a binary op has been requested, combine the old LHS value with the RHS
3563 producing the value we should actually store into the LHS. */
3565 if (modifycode != NOP_EXPR)
3567 lhs = stabilize_reference (lhs);
3568 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3571 if (!lvalue_or_else (lhs, lv_assign))
3572 return error_mark_node;
3574 /* Give an error for storing in something that is 'const'. */
3576 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3577 || ((TREE_CODE (lhstype) == RECORD_TYPE
3578 || TREE_CODE (lhstype) == UNION_TYPE)
3579 && C_TYPE_FIELDS_READONLY (lhstype)))
3580 readonly_error (lhs, lv_assign);
3582 /* If storing into a structure or union member,
3583 it has probably been given type `int'.
3584 Compute the type that would go with
3585 the actual amount of storage the member occupies. */
3587 if (TREE_CODE (lhs) == COMPONENT_REF
3588 && (TREE_CODE (lhstype) == INTEGER_TYPE
3589 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3590 || TREE_CODE (lhstype) == REAL_TYPE
3591 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3592 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3594 /* If storing in a field that is in actuality a short or narrower than one,
3595 we must store in the field in its actual type. */
3597 if (lhstype != TREE_TYPE (lhs))
3599 lhs = copy_node (lhs);
3600 TREE_TYPE (lhs) = lhstype;
3603 /* Convert new value to destination type. */
3605 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3606 NULL_TREE, NULL_TREE, 0);
3607 if (TREE_CODE (newrhs) == ERROR_MARK)
3608 return error_mark_node;
3610 /* Emit ObjC write barrier, if necessary. */
3611 if (c_dialect_objc () && flag_objc_gc)
3613 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3618 /* Scan operands. */
3620 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3621 TREE_SIDE_EFFECTS (result) = 1;
3623 /* If we got the LHS in a different type for storing in,
3624 convert the result back to the nominal type of LHS
3625 so that the value we return always has the same type
3626 as the LHS argument. */
3628 if (olhstype == TREE_TYPE (result))
3630 return convert_for_assignment (olhstype, result, ic_assign,
3631 NULL_TREE, NULL_TREE, 0);
3634 /* Convert value RHS to type TYPE as preparation for an assignment
3635 to an lvalue of type TYPE.
3636 The real work of conversion is done by `convert'.
3637 The purpose of this function is to generate error messages
3638 for assignments that are not allowed in C.
3639 ERRTYPE says whether it is argument passing, assignment,
3640 initialization or return.
3642 FUNCTION is a tree for the function being called.
3643 PARMNUM is the number of the argument, for printing in error messages. */
3646 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3647 tree fundecl, tree function, int parmnum)
3649 enum tree_code codel = TREE_CODE (type);
3651 enum tree_code coder;
3652 tree rname = NULL_TREE;
3653 bool objc_ok = false;
3655 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3658 /* Change pointer to function to the function itself for
3660 if (TREE_CODE (function) == ADDR_EXPR
3661 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3662 function = TREE_OPERAND (function, 0);
3664 /* Handle an ObjC selector specially for diagnostics. */
3665 selector = objc_message_selector ();
3667 if (selector && parmnum > 2)
3674 /* This macro is used to emit diagnostics to ensure that all format
3675 strings are complete sentences, visible to gettext and checked at
3677 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3682 pedwarn (AR, parmnum, rname); \
3684 case ic_argpass_nonproto: \
3685 warning (0, AR, parmnum, rname); \
3697 gcc_unreachable (); \
3701 STRIP_TYPE_NOPS (rhs);
3703 if (optimize && TREE_CODE (rhs) == VAR_DECL
3704 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3705 rhs = decl_constant_value_for_broken_optimization (rhs);
3707 rhstype = TREE_TYPE (rhs);
3708 coder = TREE_CODE (rhstype);
3710 if (coder == ERROR_MARK)
3711 return error_mark_node;
3713 if (c_dialect_objc ())
3736 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3739 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3741 overflow_warning (rhs);
3745 if (coder == VOID_TYPE)
3747 /* Except for passing an argument to an unprototyped function,
3748 this is a constraint violation. When passing an argument to
3749 an unprototyped function, it is compile-time undefined;
3750 making it a constraint in that case was rejected in
3752 error ("void value not ignored as it ought to be");
3753 return error_mark_node;
3755 /* A type converts to a reference to it.
3756 This code doesn't fully support references, it's just for the
3757 special case of va_start and va_copy. */
3758 if (codel == REFERENCE_TYPE
3759 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3761 if (!lvalue_p (rhs))
3763 error ("cannot pass rvalue to reference parameter");
3764 return error_mark_node;
3766 if (!c_mark_addressable (rhs))
3767 return error_mark_node;
3768 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3770 /* We already know that these two types are compatible, but they
3771 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3772 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3773 likely to be va_list, a typedef to __builtin_va_list, which
3774 is different enough that it will cause problems later. */
3775 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3776 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3778 rhs = build1 (NOP_EXPR, type, rhs);
3781 /* Some types can interconvert without explicit casts. */
3782 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3783 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3784 return convert (type, rhs);
3785 /* Arithmetic types all interconvert, and enum is treated like int. */
3786 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3787 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3788 || codel == BOOLEAN_TYPE)
3789 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3790 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3791 || coder == BOOLEAN_TYPE))
3792 return convert_and_check (type, rhs);
3794 /* Conversion to a transparent union from its member types.
3795 This applies only to function arguments. */
3796 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3797 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3799 tree memb, marginal_memb = NULL_TREE;
3801 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3803 tree memb_type = TREE_TYPE (memb);
3805 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3806 TYPE_MAIN_VARIANT (rhstype)))
3809 if (TREE_CODE (memb_type) != POINTER_TYPE)
3812 if (coder == POINTER_TYPE)
3814 tree ttl = TREE_TYPE (memb_type);
3815 tree ttr = TREE_TYPE (rhstype);
3817 /* Any non-function converts to a [const][volatile] void *
3818 and vice versa; otherwise, targets must be the same.
3819 Meanwhile, the lhs target must have all the qualifiers of
3821 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3822 || comp_target_types (memb_type, rhstype))
3824 /* If this type won't generate any warnings, use it. */
3825 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3826 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3827 && TREE_CODE (ttl) == FUNCTION_TYPE)
3828 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3829 == TYPE_QUALS (ttr))
3830 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3831 == TYPE_QUALS (ttl))))
3834 /* Keep looking for a better type, but remember this one. */
3836 marginal_memb = memb;
3840 /* Can convert integer zero to any pointer type. */
3841 if (integer_zerop (rhs)
3842 || (TREE_CODE (rhs) == NOP_EXPR
3843 && integer_zerop (TREE_OPERAND (rhs, 0))))
3845 rhs = null_pointer_node;
3850 if (memb || marginal_memb)
3854 /* We have only a marginally acceptable member type;
3855 it needs a warning. */
3856 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3857 tree ttr = TREE_TYPE (rhstype);
3859 /* Const and volatile mean something different for function
3860 types, so the usual warnings are not appropriate. */
3861 if (TREE_CODE (ttr) == FUNCTION_TYPE
3862 && TREE_CODE (ttl) == FUNCTION_TYPE)
3864 /* Because const and volatile on functions are
3865 restrictions that say the function will not do
3866 certain things, it is okay to use a const or volatile
3867 function where an ordinary one is wanted, but not
3869 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3870 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3871 "makes qualified function "
3872 "pointer from unqualified"),
3873 G_("assignment makes qualified "
3874 "function pointer from "
3876 G_("initialization makes qualified "
3877 "function pointer from "
3879 G_("return makes qualified function "
3880 "pointer from unqualified"));
3882 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3883 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3884 "qualifiers from pointer target type"),
3885 G_("assignment discards qualifiers "
3886 "from pointer target type"),
3887 G_("initialization discards qualifiers "
3888 "from pointer target type"),
3889 G_("return discards qualifiers from "
3890 "pointer target type"));
3892 memb = marginal_memb;
3895 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
3896 pedwarn ("ISO C prohibits argument conversion to union type");
3898 return build_constructor_single (type, memb, rhs);
3902 /* Conversions among pointers */
3903 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3904 && (coder == codel))
3906 tree ttl = TREE_TYPE (type);
3907 tree ttr = TREE_TYPE (rhstype);
3910 bool is_opaque_pointer;
3911 int target_cmp = 0; /* Cache comp_target_types () result. */
3913 if (TREE_CODE (mvl) != ARRAY_TYPE)
3914 mvl = TYPE_MAIN_VARIANT (mvl);
3915 if (TREE_CODE (mvr) != ARRAY_TYPE)
3916 mvr = TYPE_MAIN_VARIANT (mvr);
3917 /* Opaque pointers are treated like void pointers. */
3918 is_opaque_pointer = (targetm.vector_opaque_p (type)
3919 || targetm.vector_opaque_p (rhstype))
3920 && TREE_CODE (ttl) == VECTOR_TYPE
3921 && TREE_CODE (ttr) == VECTOR_TYPE;
3923 /* C++ does not allow the implicit conversion void* -> T*. However,
3924 for the purpose of reducing the number of false positives, we
3925 tolerate the special case of
3929 where NULL is typically defined in C to be '(void *) 0'. */
3930 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
3931 warning (OPT_Wc___compat, "request for implicit conversion from "
3932 "%qT to %qT not permitted in C++", rhstype, type);
3934 /* Check if the right-hand side has a format attribute but the
3935 left-hand side doesn't. */
3936 if (warn_missing_format_attribute
3937 && check_missing_format_attribute (type, rhstype))
3942 case ic_argpass_nonproto:
3943 warning (OPT_Wmissing_format_attribute,
3944 "argument %d of %qE might be "
3945 "a candidate for a format attribute",
3949 warning (OPT_Wmissing_format_attribute,
3950 "assignment left-hand side might be "
3951 "a candidate for a format attribute");
3954 warning (OPT_Wmissing_format_attribute,
3955 "initialization left-hand side might be "
3956 "a candidate for a format attribute");
3959 warning (OPT_Wmissing_format_attribute,
3960 "return type might be "
3961 "a candidate for a format attribute");
3968 /* Any non-function converts to a [const][volatile] void *
3969 and vice versa; otherwise, targets must be the same.
3970 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3971 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3972 || (target_cmp = comp_target_types (type, rhstype))
3973 || is_opaque_pointer
3974 || (c_common_unsigned_type (mvl)
3975 == c_common_unsigned_type (mvr)))
3978 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3981 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3982 which are not ANSI null ptr constants. */
3983 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3984 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3985 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
3986 "%qE between function pointer "
3988 G_("ISO C forbids assignment between "
3989 "function pointer and %<void *%>"),
3990 G_("ISO C forbids initialization between "