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, 59 Temple Place - Suite 330, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
30 #include "coretypes.h"
34 #include "langhooks.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
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, int);
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 default_function_array_conversion (tree);
84 static tree lookup_field (tree, tree);
85 static tree convert_arguments (tree, tree, tree, tree);
86 static tree pointer_diff (tree, tree);
87 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
92 static void push_array_bounds (int);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (const char *);
96 static tree digest_init (tree, tree, bool, int);
97 static void output_init_element (tree, bool, tree, tree, int);
98 static void output_pending_init_elements (int);
99 static int set_designator (int);
100 static void push_range_stack (tree);
101 static void add_pending_init (tree, tree);
102 static void set_nonincremental_init (void);
103 static void set_nonincremental_init_from_string (tree);
104 static tree find_init_member (tree);
105 static void readonly_error (tree, enum lvalue_use);
106 static int lvalue_or_else (tree, enum lvalue_use);
107 static int lvalue_p (tree);
108 static void record_maybe_used_decl (tree);
110 /* Do `exp = require_complete_type (exp);' to make sure exp
111 does not have an incomplete type. (That includes void types.) */
114 require_complete_type (tree value)
116 tree type = TREE_TYPE (value);
118 if (value == error_mark_node || type == error_mark_node)
119 return error_mark_node;
121 /* First, detect a valid value with a complete type. */
122 if (COMPLETE_TYPE_P (type))
125 c_incomplete_type_error (value, type);
126 return error_mark_node;
129 /* Print an error message for invalid use of an incomplete type.
130 VALUE is the expression that was used (or 0 if that isn't known)
131 and TYPE is the type that was invalid. */
134 c_incomplete_type_error (tree value, tree type)
136 const char *type_code_string;
138 /* Avoid duplicate error message. */
139 if (TREE_CODE (type) == ERROR_MARK)
142 if (value != 0 && (TREE_CODE (value) == VAR_DECL
143 || TREE_CODE (value) == PARM_DECL))
144 error ("%qD has an incomplete type", value);
148 /* We must print an error message. Be clever about what it says. */
150 switch (TREE_CODE (type))
153 type_code_string = "struct";
157 type_code_string = "union";
161 type_code_string = "enum";
165 error ("invalid use of void expression");
169 if (TYPE_DOMAIN (type))
171 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
173 error ("invalid use of flexible array member");
176 type = TREE_TYPE (type);
179 error ("invalid use of array with unspecified bounds");
186 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
187 error ("invalid use of undefined type %<%s %E%>",
188 type_code_string, TYPE_NAME (type));
190 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
191 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
195 /* Given a type, apply default promotions wrt unnamed function
196 arguments and return the new type. */
199 c_type_promotes_to (tree type)
201 if (TYPE_MAIN_VARIANT (type) == float_type_node)
202 return double_type_node;
204 if (c_promoting_integer_type_p (type))
206 /* Preserve unsignedness if not really getting any wider. */
207 if (TYPE_UNSIGNED (type)
208 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
209 return unsigned_type_node;
210 return integer_type_node;
216 /* Return a variant of TYPE which has all the type qualifiers of LIKE
217 as well as those of TYPE. */
220 qualify_type (tree type, tree like)
222 return c_build_qualified_type (type,
223 TYPE_QUALS (type) | TYPE_QUALS (like));
226 /* Return the composite type of two compatible types.
228 We assume that comptypes has already been done and returned
229 nonzero; if that isn't so, this may crash. In particular, we
230 assume that qualifiers match. */
233 composite_type (tree t1, tree t2)
235 enum tree_code code1;
236 enum tree_code code2;
239 /* Save time if the two types are the same. */
241 if (t1 == t2) return t1;
243 /* If one type is nonsense, use the other. */
244 if (t1 == error_mark_node)
246 if (t2 == error_mark_node)
249 code1 = TREE_CODE (t1);
250 code2 = TREE_CODE (t2);
252 /* Merge the attributes. */
253 attributes = targetm.merge_type_attributes (t1, t2);
255 /* If one is an enumerated type and the other is the compatible
256 integer type, the composite type might be either of the two
257 (DR#013 question 3). For consistency, use the enumerated type as
258 the composite type. */
260 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
262 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
265 gcc_assert (code1 == code2);
270 /* For two pointers, do this recursively on the target type. */
272 tree pointed_to_1 = TREE_TYPE (t1);
273 tree pointed_to_2 = TREE_TYPE (t2);
274 tree target = composite_type (pointed_to_1, pointed_to_2);
275 t1 = build_pointer_type (target);
276 t1 = build_type_attribute_variant (t1, attributes);
277 return qualify_type (t1, t2);
282 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
286 /* We should not have any type quals on arrays at all. */
287 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
289 /* Save space: see if the result is identical to one of the args. */
290 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
291 return build_type_attribute_variant (t1, attributes);
292 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
293 return build_type_attribute_variant (t2, attributes);
295 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
296 return build_type_attribute_variant (t1, attributes);
297 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
298 return build_type_attribute_variant (t2, attributes);
300 /* Merge the element types, and have a size if either arg has
301 one. We may have qualifiers on the element types. To set
302 up TYPE_MAIN_VARIANT correctly, we need to form the
303 composite of the unqualified types and add the qualifiers
305 quals = TYPE_QUALS (strip_array_types (elt));
306 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
307 t1 = build_array_type (unqual_elt,
308 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
309 t1 = c_build_qualified_type (t1, quals);
310 return build_type_attribute_variant (t1, attributes);
314 /* Function types: prefer the one that specified arg types.
315 If both do, merge the arg types. Also merge the return types. */
317 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
318 tree p1 = TYPE_ARG_TYPES (t1);
319 tree p2 = TYPE_ARG_TYPES (t2);
324 /* Save space: see if the result is identical to one of the args. */
325 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
326 return build_type_attribute_variant (t1, attributes);
327 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
328 return build_type_attribute_variant (t2, attributes);
330 /* Simple way if one arg fails to specify argument types. */
331 if (TYPE_ARG_TYPES (t1) == 0)
333 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
334 t1 = build_type_attribute_variant (t1, attributes);
335 return qualify_type (t1, t2);
337 if (TYPE_ARG_TYPES (t2) == 0)
339 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
340 t1 = build_type_attribute_variant (t1, attributes);
341 return qualify_type (t1, t2);
344 /* If both args specify argument types, we must merge the two
345 lists, argument by argument. */
346 /* Tell global_bindings_p to return false so that variable_size
347 doesn't die on VLAs in parameter types. */
348 c_override_global_bindings_to_false = true;
350 len = list_length (p1);
353 for (i = 0; i < len; i++)
354 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
359 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
361 /* A null type means arg type is not specified.
362 Take whatever the other function type has. */
363 if (TREE_VALUE (p1) == 0)
365 TREE_VALUE (n) = TREE_VALUE (p2);
368 if (TREE_VALUE (p2) == 0)
370 TREE_VALUE (n) = TREE_VALUE (p1);
374 /* Given wait (union {union wait *u; int *i} *)
375 and wait (union wait *),
376 prefer union wait * as type of parm. */
377 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
378 && TREE_VALUE (p1) != TREE_VALUE (p2))
381 tree mv2 = TREE_VALUE (p2);
382 if (mv2 && mv2 != error_mark_node
383 && TREE_CODE (mv2) != ARRAY_TYPE)
384 mv2 = TYPE_MAIN_VARIANT (mv2);
385 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
386 memb; memb = TREE_CHAIN (memb))
388 tree mv3 = TREE_TYPE (memb);
389 if (mv3 && mv3 != error_mark_node
390 && TREE_CODE (mv3) != ARRAY_TYPE)
391 mv3 = TYPE_MAIN_VARIANT (mv3);
392 if (comptypes (mv3, mv2))
394 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
397 pedwarn ("function types not truly compatible in ISO C");
402 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
403 && TREE_VALUE (p2) != TREE_VALUE (p1))
406 tree mv1 = TREE_VALUE (p1);
407 if (mv1 && mv1 != error_mark_node
408 && TREE_CODE (mv1) != ARRAY_TYPE)
409 mv1 = TYPE_MAIN_VARIANT (mv1);
410 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
411 memb; memb = TREE_CHAIN (memb))
413 tree mv3 = TREE_TYPE (memb);
414 if (mv3 && mv3 != error_mark_node
415 && TREE_CODE (mv3) != ARRAY_TYPE)
416 mv3 = TYPE_MAIN_VARIANT (mv3);
417 if (comptypes (mv3, mv1))
419 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
422 pedwarn ("function types not truly compatible in ISO C");
427 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
431 c_override_global_bindings_to_false = false;
432 t1 = build_function_type (valtype, newargs);
433 t1 = qualify_type (t1, t2);
434 /* ... falls through ... */
438 return build_type_attribute_variant (t1, attributes);
443 /* Return the type of a conditional expression between pointers to
444 possibly differently qualified versions of compatible types.
446 We assume that comp_target_types has already been done and returned
447 nonzero; if that isn't so, this may crash. */
450 common_pointer_type (tree t1, tree t2)
453 tree pointed_to_1, mv1;
454 tree pointed_to_2, mv2;
457 /* Save time if the two types are the same. */
459 if (t1 == t2) return t1;
461 /* If one type is nonsense, use the other. */
462 if (t1 == error_mark_node)
464 if (t2 == error_mark_node)
467 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
468 && TREE_CODE (t2) == POINTER_TYPE);
470 /* Merge the attributes. */
471 attributes = targetm.merge_type_attributes (t1, t2);
473 /* Find the composite type of the target types, and combine the
474 qualifiers of the two types' targets. Do not lose qualifiers on
475 array element types by taking the TYPE_MAIN_VARIANT. */
476 mv1 = pointed_to_1 = TREE_TYPE (t1);
477 mv2 = pointed_to_2 = TREE_TYPE (t2);
478 if (TREE_CODE (mv1) != ARRAY_TYPE)
479 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
480 if (TREE_CODE (mv2) != ARRAY_TYPE)
481 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
482 target = composite_type (mv1, mv2);
483 t1 = build_pointer_type (c_build_qualified_type
485 TYPE_QUALS (pointed_to_1) |
486 TYPE_QUALS (pointed_to_2)));
487 return build_type_attribute_variant (t1, attributes);
490 /* Return the common type for two arithmetic types under the usual
491 arithmetic conversions. The default conversions have already been
492 applied, and enumerated types converted to their compatible integer
493 types. The resulting type is unqualified and has no attributes.
495 This is the type for the result of most arithmetic operations
496 if the operands have the given two types. */
499 c_common_type (tree t1, tree t2)
501 enum tree_code code1;
502 enum tree_code code2;
504 /* If one type is nonsense, use the other. */
505 if (t1 == error_mark_node)
507 if (t2 == error_mark_node)
510 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
511 t1 = TYPE_MAIN_VARIANT (t1);
513 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
514 t2 = TYPE_MAIN_VARIANT (t2);
516 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
517 t1 = build_type_attribute_variant (t1, NULL_TREE);
519 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
520 t2 = build_type_attribute_variant (t2, NULL_TREE);
522 /* Save time if the two types are the same. */
524 if (t1 == t2) return t1;
526 code1 = TREE_CODE (t1);
527 code2 = TREE_CODE (t2);
529 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
530 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
531 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
532 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
534 /* If one type is a vector type, return that type. (How the usual
535 arithmetic conversions apply to the vector types extension is not
536 precisely specified.) */
537 if (code1 == VECTOR_TYPE)
540 if (code2 == VECTOR_TYPE)
543 /* If one type is complex, form the common type of the non-complex
544 components, then make that complex. Use T1 or T2 if it is the
546 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
548 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
549 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
550 tree subtype = c_common_type (subtype1, subtype2);
552 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
554 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
557 return build_complex_type (subtype);
560 /* If only one is real, use it as the result. */
562 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
565 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
568 /* Both real or both integers; use the one with greater precision. */
570 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
572 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
575 /* Same precision. Prefer long longs to longs to ints when the
576 same precision, following the C99 rules on integer type rank
577 (which are equivalent to the C90 rules for C90 types). */
579 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
580 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
581 return long_long_unsigned_type_node;
583 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
584 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
586 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
587 return long_long_unsigned_type_node;
589 return long_long_integer_type_node;
592 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
593 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
594 return long_unsigned_type_node;
596 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
597 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
599 /* But preserve unsignedness from the other type,
600 since long cannot hold all the values of an unsigned int. */
601 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
602 return long_unsigned_type_node;
604 return long_integer_type_node;
607 /* Likewise, prefer long double to double even if same size. */
608 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
609 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
610 return long_double_type_node;
612 /* Otherwise prefer the unsigned one. */
614 if (TYPE_UNSIGNED (t1))
620 /* Wrapper around c_common_type that is used by c-common.c. ENUMERAL_TYPEs
621 are allowed here and are converted to their compatible integer types.
622 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
623 preferably a non-Boolean type as the common type. */
625 common_type (tree t1, tree t2)
627 if (TREE_CODE (t1) == ENUMERAL_TYPE)
628 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
629 if (TREE_CODE (t2) == ENUMERAL_TYPE)
630 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
632 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
633 if (TREE_CODE (t1) == BOOLEAN_TYPE
634 && TREE_CODE (t2) == BOOLEAN_TYPE)
635 return boolean_type_node;
637 /* If either type is BOOLEAN_TYPE, then return the other. */
638 if (TREE_CODE (t1) == BOOLEAN_TYPE)
640 if (TREE_CODE (t2) == BOOLEAN_TYPE)
643 return c_common_type (t1, t2);
646 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
647 or various other operations. Return 2 if they are compatible
648 but a warning may be needed if you use them together. */
651 comptypes (tree type1, tree type2)
657 /* Suppress errors caused by previously reported errors. */
659 if (t1 == t2 || !t1 || !t2
660 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
663 /* If either type is the internal version of sizetype, return the
665 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
666 && TYPE_ORIG_SIZE_TYPE (t1))
667 t1 = TYPE_ORIG_SIZE_TYPE (t1);
669 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
670 && TYPE_ORIG_SIZE_TYPE (t2))
671 t2 = TYPE_ORIG_SIZE_TYPE (t2);
674 /* Enumerated types are compatible with integer types, but this is
675 not transitive: two enumerated types in the same translation unit
676 are compatible with each other only if they are the same type. */
678 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
679 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
680 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
681 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
686 /* Different classes of types can't be compatible. */
688 if (TREE_CODE (t1) != TREE_CODE (t2))
691 /* Qualifiers must match. C99 6.7.3p9 */
693 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
696 /* Allow for two different type nodes which have essentially the same
697 definition. Note that we already checked for equality of the type
698 qualifiers (just above). */
700 if (TREE_CODE (t1) != ARRAY_TYPE
701 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
704 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
705 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
708 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
711 switch (TREE_CODE (t1))
714 /* We must give ObjC the first crack at comparing pointers, since
715 protocol qualifiers may be involved. */
716 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
718 /* Do not remove mode or aliasing information. */
719 if (TYPE_MODE (t1) != TYPE_MODE (t2)
720 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
722 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
723 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
727 val = function_types_compatible_p (t1, t2);
732 tree d1 = TYPE_DOMAIN (t1);
733 tree d2 = TYPE_DOMAIN (t2);
734 bool d1_variable, d2_variable;
735 bool d1_zero, d2_zero;
738 /* Target types must match incl. qualifiers. */
739 if (TREE_TYPE (t1) != TREE_TYPE (t2)
740 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
743 /* Sizes must match unless one is missing or variable. */
744 if (d1 == 0 || d2 == 0 || d1 == d2)
747 d1_zero = !TYPE_MAX_VALUE (d1);
748 d2_zero = !TYPE_MAX_VALUE (d2);
750 d1_variable = (!d1_zero
751 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
752 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
753 d2_variable = (!d2_zero
754 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
755 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
757 if (d1_variable || d2_variable)
759 if (d1_zero && d2_zero)
761 if (d1_zero || d2_zero
762 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
763 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
770 /* We are dealing with two distinct structs. In assorted Objective-C
771 corner cases, however, these can still be deemed equivalent. */
772 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
777 if (val != 1 && !same_translation_unit_p (t1, t2))
778 val = tagged_types_tu_compatible_p (t1, t2);
782 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
783 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
789 return attrval == 2 && val == 1 ? 2 : val;
792 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
793 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
794 to 1 or 0 depending if the check of the pointer types is meant to
795 be reflexive or not (typically, assignments are not reflexive,
796 while comparisons are reflexive).
800 comp_target_types (tree ttl, tree ttr, int reflexive)
805 /* Give objc_comptypes a crack at letting these types through. */
806 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
809 /* Do not lose qualifiers on element types of array types that are
810 pointer targets by taking their TYPE_MAIN_VARIANT. */
811 mvl = TREE_TYPE (ttl);
812 mvr = TREE_TYPE (ttr);
813 if (TREE_CODE (mvl) != ARRAY_TYPE)
814 mvl = TYPE_MAIN_VARIANT (mvl);
815 if (TREE_CODE (mvr) != ARRAY_TYPE)
816 mvr = TYPE_MAIN_VARIANT (mvr);
817 val = comptypes (mvl, mvr);
819 if (val == 2 && pedantic)
820 pedwarn ("types are not quite compatible");
824 /* Subroutines of `comptypes'. */
826 /* Determine whether two trees derive from the same translation unit.
827 If the CONTEXT chain ends in a null, that tree's context is still
828 being parsed, so if two trees have context chains ending in null,
829 they're in the same translation unit. */
831 same_translation_unit_p (tree t1, tree t2)
833 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
834 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
836 case tcc_declaration:
837 t1 = DECL_CONTEXT (t1); break;
839 t1 = TYPE_CONTEXT (t1); break;
840 case tcc_exceptional:
841 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
842 default: gcc_unreachable ();
845 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
846 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
848 case tcc_declaration:
849 t2 = DECL_CONTEXT (t2); break;
851 t2 = TYPE_CONTEXT (t2); break;
852 case tcc_exceptional:
853 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
854 default: gcc_unreachable ();
860 /* The C standard says that two structures in different translation
861 units are compatible with each other only if the types of their
862 fields are compatible (among other things). So, consider two copies
863 of this structure: */
865 struct tagged_tu_seen {
866 const struct tagged_tu_seen * next;
871 /* Can they be compatible with each other? We choose to break the
872 recursion by allowing those types to be compatible. */
874 static const struct tagged_tu_seen * tagged_tu_seen_base;
876 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
877 compatible. If the two types are not the same (which has been
878 checked earlier), this can only happen when multiple translation
879 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
883 tagged_types_tu_compatible_p (tree t1, tree t2)
886 bool needs_warning = false;
888 /* We have to verify that the tags of the types are the same. This
889 is harder than it looks because this may be a typedef, so we have
890 to go look at the original type. It may even be a typedef of a
892 In the case of compiler-created builtin structs the TYPE_DECL
893 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
894 while (TYPE_NAME (t1)
895 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
896 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
897 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
899 while (TYPE_NAME (t2)
900 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
901 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
902 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
904 /* C90 didn't have the requirement that the two tags be the same. */
905 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
908 /* C90 didn't say what happened if one or both of the types were
909 incomplete; we choose to follow C99 rules here, which is that they
911 if (TYPE_SIZE (t1) == NULL
912 || TYPE_SIZE (t2) == NULL)
916 const struct tagged_tu_seen * tts_i;
917 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
918 if (tts_i->t1 == t1 && tts_i->t2 == t2)
922 switch (TREE_CODE (t1))
927 /* Speed up the case where the type values are in the same order. */
928 tree tv1 = TYPE_VALUES (t1);
929 tree tv2 = TYPE_VALUES (t2);
934 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
936 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
938 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
942 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
944 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
947 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
950 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
952 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
954 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
962 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
965 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
968 struct tagged_tu_seen tts;
970 tts.next = tagged_tu_seen_base;
973 tagged_tu_seen_base = &tts;
975 if (DECL_NAME (s1) != NULL)
976 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
977 if (DECL_NAME (s1) == DECL_NAME (s2))
980 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
984 needs_warning = true;
986 if (TREE_CODE (s1) == FIELD_DECL
987 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
988 DECL_FIELD_BIT_OFFSET (s2)) != 1)
994 tagged_tu_seen_base = tts.next;
998 return needs_warning ? 2 : 1;
1003 struct tagged_tu_seen tts;
1005 tts.next = tagged_tu_seen_base;
1008 tagged_tu_seen_base = &tts;
1010 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1012 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1015 if (TREE_CODE (s1) != TREE_CODE (s2)
1016 || DECL_NAME (s1) != DECL_NAME (s2))
1018 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
1022 needs_warning = true;
1024 if (TREE_CODE (s1) == FIELD_DECL
1025 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1026 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1029 tagged_tu_seen_base = tts.next;
1032 return needs_warning ? 2 : 1;
1040 /* Return 1 if two function types F1 and F2 are compatible.
1041 If either type specifies no argument types,
1042 the other must specify a fixed number of self-promoting arg types.
1043 Otherwise, if one type specifies only the number of arguments,
1044 the other must specify that number of self-promoting arg types.
1045 Otherwise, the argument types must match. */
1048 function_types_compatible_p (tree f1, tree f2)
1051 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1056 ret1 = TREE_TYPE (f1);
1057 ret2 = TREE_TYPE (f2);
1059 /* 'volatile' qualifiers on a function's return type used to mean
1060 the function is noreturn. */
1061 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1062 pedwarn ("function return types not compatible due to %<volatile%>");
1063 if (TYPE_VOLATILE (ret1))
1064 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1065 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1066 if (TYPE_VOLATILE (ret2))
1067 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1068 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1069 val = comptypes (ret1, ret2);
1073 args1 = TYPE_ARG_TYPES (f1);
1074 args2 = TYPE_ARG_TYPES (f2);
1076 /* An unspecified parmlist matches any specified parmlist
1077 whose argument types don't need default promotions. */
1081 if (!self_promoting_args_p (args2))
1083 /* If one of these types comes from a non-prototype fn definition,
1084 compare that with the other type's arglist.
1085 If they don't match, ask for a warning (0, but no error). */
1086 if (TYPE_ACTUAL_ARG_TYPES (f1)
1087 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1093 if (!self_promoting_args_p (args1))
1095 if (TYPE_ACTUAL_ARG_TYPES (f2)
1096 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1101 /* Both types have argument lists: compare them and propagate results. */
1102 val1 = type_lists_compatible_p (args1, args2);
1103 return val1 != 1 ? val1 : val;
1106 /* Check two lists of types for compatibility,
1107 returning 0 for incompatible, 1 for compatible,
1108 or 2 for compatible with warning. */
1111 type_lists_compatible_p (tree args1, tree args2)
1113 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1119 tree a1, mv1, a2, mv2;
1120 if (args1 == 0 && args2 == 0)
1122 /* If one list is shorter than the other,
1123 they fail to match. */
1124 if (args1 == 0 || args2 == 0)
1126 mv1 = a1 = TREE_VALUE (args1);
1127 mv2 = a2 = TREE_VALUE (args2);
1128 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1129 mv1 = TYPE_MAIN_VARIANT (mv1);
1130 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1131 mv2 = TYPE_MAIN_VARIANT (mv2);
1132 /* A null pointer instead of a type
1133 means there is supposed to be an argument
1134 but nothing is specified about what type it has.
1135 So match anything that self-promotes. */
1138 if (c_type_promotes_to (a2) != a2)
1143 if (c_type_promotes_to (a1) != a1)
1146 /* If one of the lists has an error marker, ignore this arg. */
1147 else if (TREE_CODE (a1) == ERROR_MARK
1148 || TREE_CODE (a2) == ERROR_MARK)
1150 else if (!(newval = comptypes (mv1, mv2)))
1152 /* Allow wait (union {union wait *u; int *i} *)
1153 and wait (union wait *) to be compatible. */
1154 if (TREE_CODE (a1) == UNION_TYPE
1155 && (TYPE_NAME (a1) == 0
1156 || TYPE_TRANSPARENT_UNION (a1))
1157 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1158 && tree_int_cst_equal (TYPE_SIZE (a1),
1162 for (memb = TYPE_FIELDS (a1);
1163 memb; memb = TREE_CHAIN (memb))
1165 tree mv3 = TREE_TYPE (memb);
1166 if (mv3 && mv3 != error_mark_node
1167 && TREE_CODE (mv3) != ARRAY_TYPE)
1168 mv3 = TYPE_MAIN_VARIANT (mv3);
1169 if (comptypes (mv3, mv2))
1175 else if (TREE_CODE (a2) == UNION_TYPE
1176 && (TYPE_NAME (a2) == 0
1177 || TYPE_TRANSPARENT_UNION (a2))
1178 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1179 && tree_int_cst_equal (TYPE_SIZE (a2),
1183 for (memb = TYPE_FIELDS (a2);
1184 memb; memb = TREE_CHAIN (memb))
1186 tree mv3 = TREE_TYPE (memb);
1187 if (mv3 && mv3 != error_mark_node
1188 && TREE_CODE (mv3) != ARRAY_TYPE)
1189 mv3 = TYPE_MAIN_VARIANT (mv3);
1190 if (comptypes (mv3, mv1))
1200 /* comptypes said ok, but record if it said to warn. */
1204 args1 = TREE_CHAIN (args1);
1205 args2 = TREE_CHAIN (args2);
1209 /* Compute the size to increment a pointer by. */
1212 c_size_in_bytes (tree type)
1214 enum tree_code code = TREE_CODE (type);
1216 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1217 return size_one_node;
1219 if (!COMPLETE_OR_VOID_TYPE_P (type))
1221 error ("arithmetic on pointer to an incomplete type");
1222 return size_one_node;
1225 /* Convert in case a char is more than one unit. */
1226 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1227 size_int (TYPE_PRECISION (char_type_node)
1231 /* Return either DECL or its known constant value (if it has one). */
1234 decl_constant_value (tree decl)
1236 if (/* Don't change a variable array bound or initial value to a constant
1237 in a place where a variable is invalid. Note that DECL_INITIAL
1238 isn't valid for a PARM_DECL. */
1239 current_function_decl != 0
1240 && TREE_CODE (decl) != PARM_DECL
1241 && !TREE_THIS_VOLATILE (decl)
1242 && TREE_READONLY (decl)
1243 && DECL_INITIAL (decl) != 0
1244 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1245 /* This is invalid if initial value is not constant.
1246 If it has either a function call, a memory reference,
1247 or a variable, then re-evaluating it could give different results. */
1248 && TREE_CONSTANT (DECL_INITIAL (decl))
1249 /* Check for cases where this is sub-optimal, even though valid. */
1250 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1251 return DECL_INITIAL (decl);
1255 /* Return either DECL or its known constant value (if it has one), but
1256 return DECL if pedantic or DECL has mode BLKmode. This is for
1257 bug-compatibility with the old behavior of decl_constant_value
1258 (before GCC 3.0); every use of this function is a bug and it should
1259 be removed before GCC 3.1. It is not appropriate to use pedantic
1260 in a way that affects optimization, and BLKmode is probably not the
1261 right test for avoiding misoptimizations either. */
1264 decl_constant_value_for_broken_optimization (tree decl)
1268 if (pedantic || DECL_MODE (decl) == BLKmode)
1271 ret = decl_constant_value (decl);
1272 /* Avoid unwanted tree sharing between the initializer and current
1273 function's body where the tree can be modified e.g. by the
1275 if (ret != decl && TREE_STATIC (decl))
1276 ret = unshare_expr (ret);
1281 /* Perform the default conversion of arrays and functions to pointers.
1282 Return the result of converting EXP. For any other expression, just
1286 default_function_array_conversion (tree exp)
1289 tree type = TREE_TYPE (exp);
1290 enum tree_code code = TREE_CODE (type);
1293 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1296 Do not use STRIP_NOPS here! It will remove conversions from pointer
1297 to integer and cause infinite recursion. */
1299 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1300 || (TREE_CODE (exp) == NOP_EXPR
1301 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1303 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1305 exp = TREE_OPERAND (exp, 0);
1308 if (TREE_NO_WARNING (orig_exp))
1309 TREE_NO_WARNING (exp) = 1;
1311 if (code == FUNCTION_TYPE)
1313 return build_unary_op (ADDR_EXPR, exp, 0);
1315 if (code == ARRAY_TYPE)
1318 tree restype = TREE_TYPE (type);
1324 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1326 constp = TREE_READONLY (exp);
1327 volatilep = TREE_THIS_VOLATILE (exp);
1330 if (TYPE_QUALS (type) || constp || volatilep)
1332 = c_build_qualified_type (restype,
1334 | (constp * TYPE_QUAL_CONST)
1335 | (volatilep * TYPE_QUAL_VOLATILE));
1337 if (TREE_CODE (exp) == INDIRECT_REF)
1338 return convert (build_pointer_type (restype),
1339 TREE_OPERAND (exp, 0));
1341 if (TREE_CODE (exp) == COMPOUND_EXPR)
1343 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1344 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1345 TREE_OPERAND (exp, 0), op1);
1348 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1349 if (!flag_isoc99 && !lvalue_array_p)
1351 /* Before C99, non-lvalue arrays do not decay to pointers.
1352 Normally, using such an array would be invalid; but it can
1353 be used correctly inside sizeof or as a statement expression.
1354 Thus, do not give an error here; an error will result later. */
1358 ptrtype = build_pointer_type (restype);
1360 if (TREE_CODE (exp) == VAR_DECL)
1362 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1363 ADDR_EXPR because it's the best way of representing what
1364 happens in C when we take the address of an array and place
1365 it in a pointer to the element type. */
1366 adr = build1 (ADDR_EXPR, ptrtype, exp);
1367 if (!c_mark_addressable (exp))
1368 return error_mark_node;
1369 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1372 /* This way is better for a COMPONENT_REF since it can
1373 simplify the offset for a component. */
1374 adr = build_unary_op (ADDR_EXPR, exp, 1);
1375 return convert (ptrtype, adr);
1381 /* EXP is an expression of integer type. Apply the integer promotions
1382 to it and return the promoted value. */
1385 perform_integral_promotions (tree exp)
1387 tree type = TREE_TYPE (exp);
1388 enum tree_code code = TREE_CODE (type);
1390 gcc_assert (INTEGRAL_TYPE_P (type));
1392 /* Normally convert enums to int,
1393 but convert wide enums to something wider. */
1394 if (code == ENUMERAL_TYPE)
1396 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1397 TYPE_PRECISION (integer_type_node)),
1398 ((TYPE_PRECISION (type)
1399 >= TYPE_PRECISION (integer_type_node))
1400 && TYPE_UNSIGNED (type)));
1402 return convert (type, exp);
1405 /* ??? This should no longer be needed now bit-fields have their
1407 if (TREE_CODE (exp) == COMPONENT_REF
1408 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1409 /* If it's thinner than an int, promote it like a
1410 c_promoting_integer_type_p, otherwise leave it alone. */
1411 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1412 TYPE_PRECISION (integer_type_node)))
1413 return convert (integer_type_node, exp);
1415 if (c_promoting_integer_type_p (type))
1417 /* Preserve unsignedness if not really getting any wider. */
1418 if (TYPE_UNSIGNED (type)
1419 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1420 return convert (unsigned_type_node, exp);
1422 return convert (integer_type_node, exp);
1429 /* Perform default promotions for C data used in expressions.
1430 Arrays and functions are converted to pointers;
1431 enumeral types or short or char, to int.
1432 In addition, manifest constants symbols are replaced by their values. */
1435 default_conversion (tree exp)
1438 tree type = TREE_TYPE (exp);
1439 enum tree_code code = TREE_CODE (type);
1441 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1442 return default_function_array_conversion (exp);
1444 /* Constants can be used directly unless they're not loadable. */
1445 if (TREE_CODE (exp) == CONST_DECL)
1446 exp = DECL_INITIAL (exp);
1448 /* Replace a nonvolatile const static variable with its value unless
1449 it is an array, in which case we must be sure that taking the
1450 address of the array produces consistent results. */
1451 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1453 exp = decl_constant_value_for_broken_optimization (exp);
1454 type = TREE_TYPE (exp);
1457 /* Strip no-op conversions. */
1459 STRIP_TYPE_NOPS (exp);
1461 if (TREE_NO_WARNING (orig_exp))
1462 TREE_NO_WARNING (exp) = 1;
1464 if (INTEGRAL_TYPE_P (type))
1465 return perform_integral_promotions (exp);
1467 if (code == VOID_TYPE)
1469 error ("void value not ignored as it ought to be");
1470 return error_mark_node;
1475 /* Look up COMPONENT in a structure or union DECL.
1477 If the component name is not found, returns NULL_TREE. Otherwise,
1478 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1479 stepping down the chain to the component, which is in the last
1480 TREE_VALUE of the list. Normally the list is of length one, but if
1481 the component is embedded within (nested) anonymous structures or
1482 unions, the list steps down the chain to the component. */
1485 lookup_field (tree decl, tree component)
1487 tree type = TREE_TYPE (decl);
1490 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1491 to the field elements. Use a binary search on this array to quickly
1492 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1493 will always be set for structures which have many elements. */
1495 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1498 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1500 field = TYPE_FIELDS (type);
1502 top = TYPE_LANG_SPECIFIC (type)->s->len;
1503 while (top - bot > 1)
1505 half = (top - bot + 1) >> 1;
1506 field = field_array[bot+half];
1508 if (DECL_NAME (field) == NULL_TREE)
1510 /* Step through all anon unions in linear fashion. */
1511 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1513 field = field_array[bot++];
1514 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1515 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1517 tree anon = lookup_field (field, component);
1520 return tree_cons (NULL_TREE, field, anon);
1524 /* Entire record is only anon unions. */
1528 /* Restart the binary search, with new lower bound. */
1532 if (DECL_NAME (field) == component)
1534 if (DECL_NAME (field) < component)
1540 if (DECL_NAME (field_array[bot]) == component)
1541 field = field_array[bot];
1542 else if (DECL_NAME (field) != component)
1547 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1549 if (DECL_NAME (field) == NULL_TREE
1550 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1551 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1553 tree anon = lookup_field (field, component);
1556 return tree_cons (NULL_TREE, field, anon);
1559 if (DECL_NAME (field) == component)
1563 if (field == NULL_TREE)
1567 return tree_cons (NULL_TREE, field, NULL_TREE);
1570 /* Make an expression to refer to the COMPONENT field of
1571 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1574 build_component_ref (tree datum, tree component)
1576 tree type = TREE_TYPE (datum);
1577 enum tree_code code = TREE_CODE (type);
1581 if (!objc_is_public (datum, component))
1582 return error_mark_node;
1584 /* See if there is a field or component with name COMPONENT. */
1586 if (code == RECORD_TYPE || code == UNION_TYPE)
1588 if (!COMPLETE_TYPE_P (type))
1590 c_incomplete_type_error (NULL_TREE, type);
1591 return error_mark_node;
1594 field = lookup_field (datum, component);
1598 error ("%qT has no member named %qE", type, component);
1599 return error_mark_node;
1602 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1603 This might be better solved in future the way the C++ front
1604 end does it - by giving the anonymous entities each a
1605 separate name and type, and then have build_component_ref
1606 recursively call itself. We can't do that here. */
1609 tree subdatum = TREE_VALUE (field);
1611 if (TREE_TYPE (subdatum) == error_mark_node)
1612 return error_mark_node;
1614 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1616 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1617 TREE_READONLY (ref) = 1;
1618 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1619 TREE_THIS_VOLATILE (ref) = 1;
1621 if (TREE_DEPRECATED (subdatum))
1622 warn_deprecated_use (subdatum);
1626 field = TREE_CHAIN (field);
1632 else if (code != ERROR_MARK)
1633 error ("request for member %qE in something not a structure or union",
1636 return error_mark_node;
1639 /* Given an expression PTR for a pointer, return an expression
1640 for the value pointed to.
1641 ERRORSTRING is the name of the operator to appear in error messages. */
1644 build_indirect_ref (tree ptr, const char *errorstring)
1646 tree pointer = default_conversion (ptr);
1647 tree type = TREE_TYPE (pointer);
1649 if (TREE_CODE (type) == POINTER_TYPE)
1651 if (TREE_CODE (pointer) == ADDR_EXPR
1652 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1653 == TREE_TYPE (type)))
1654 return TREE_OPERAND (pointer, 0);
1657 tree t = TREE_TYPE (type);
1661 if (TREE_CODE (mvt) != ARRAY_TYPE)
1662 mvt = TYPE_MAIN_VARIANT (mvt);
1663 ref = build1 (INDIRECT_REF, mvt, pointer);
1665 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1667 error ("dereferencing pointer to incomplete type");
1668 return error_mark_node;
1670 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1671 warning (0, "dereferencing %<void *%> pointer");
1673 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1674 so that we get the proper error message if the result is used
1675 to assign to. Also, &* is supposed to be a no-op.
1676 And ANSI C seems to specify that the type of the result
1677 should be the const type. */
1678 /* A de-reference of a pointer to const is not a const. It is valid
1679 to change it via some other pointer. */
1680 TREE_READONLY (ref) = TYPE_READONLY (t);
1681 TREE_SIDE_EFFECTS (ref)
1682 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1683 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1687 else if (TREE_CODE (pointer) != ERROR_MARK)
1688 error ("invalid type argument of %qs", errorstring);
1689 return error_mark_node;
1692 /* This handles expressions of the form "a[i]", which denotes
1695 This is logically equivalent in C to *(a+i), but we may do it differently.
1696 If A is a variable or a member, we generate a primitive ARRAY_REF.
1697 This avoids forcing the array out of registers, and can work on
1698 arrays that are not lvalues (for example, members of structures returned
1702 build_array_ref (tree array, tree index)
1704 bool swapped = false;
1705 if (TREE_TYPE (array) == error_mark_node
1706 || TREE_TYPE (index) == error_mark_node)
1707 return error_mark_node;
1709 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1710 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1713 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1714 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1716 error ("subscripted value is neither array nor pointer");
1717 return error_mark_node;
1725 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1727 error ("array subscript is not an integer");
1728 return error_mark_node;
1731 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1733 error ("subscripted value is pointer to function");
1734 return error_mark_node;
1737 /* Subscripting with type char is likely to lose on a machine where
1738 chars are signed. So warn on any machine, but optionally. Don't
1739 warn for unsigned char since that type is safe. Don't warn for
1740 signed char because anyone who uses that must have done so
1741 deliberately. ??? Existing practice has also been to warn only
1742 when the char index is syntactically the index, not for
1744 if (warn_char_subscripts && !swapped
1745 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1746 warning (0, "array subscript has type %<char%>");
1748 /* Apply default promotions *after* noticing character types. */
1749 index = default_conversion (index);
1751 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1753 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1757 /* An array that is indexed by a non-constant
1758 cannot be stored in a register; we must be able to do
1759 address arithmetic on its address.
1760 Likewise an array of elements of variable size. */
1761 if (TREE_CODE (index) != INTEGER_CST
1762 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1763 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1765 if (!c_mark_addressable (array))
1766 return error_mark_node;
1768 /* An array that is indexed by a constant value which is not within
1769 the array bounds cannot be stored in a register either; because we
1770 would get a crash in store_bit_field/extract_bit_field when trying
1771 to access a non-existent part of the register. */
1772 if (TREE_CODE (index) == INTEGER_CST
1773 && TYPE_DOMAIN (TREE_TYPE (array))
1774 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1776 if (!c_mark_addressable (array))
1777 return error_mark_node;
1783 while (TREE_CODE (foo) == COMPONENT_REF)
1784 foo = TREE_OPERAND (foo, 0);
1785 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1786 pedwarn ("ISO C forbids subscripting %<register%> array");
1787 else if (!flag_isoc99 && !lvalue_p (foo))
1788 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1791 type = TREE_TYPE (TREE_TYPE (array));
1792 if (TREE_CODE (type) != ARRAY_TYPE)
1793 type = TYPE_MAIN_VARIANT (type);
1794 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1795 /* Array ref is const/volatile if the array elements are
1796 or if the array is. */
1797 TREE_READONLY (rval)
1798 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1799 | TREE_READONLY (array));
1800 TREE_SIDE_EFFECTS (rval)
1801 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1802 | TREE_SIDE_EFFECTS (array));
1803 TREE_THIS_VOLATILE (rval)
1804 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1805 /* This was added by rms on 16 Nov 91.
1806 It fixes vol struct foo *a; a->elts[1]
1807 in an inline function.
1808 Hope it doesn't break something else. */
1809 | TREE_THIS_VOLATILE (array));
1810 return require_complete_type (fold (rval));
1814 tree ar = default_conversion (array);
1816 if (ar == error_mark_node)
1819 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1820 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1822 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1827 /* Build an external reference to identifier ID. FUN indicates
1828 whether this will be used for a function call. LOC is the source
1829 location of the identifier. */
1831 build_external_ref (tree id, int fun, location_t loc)
1834 tree decl = lookup_name (id);
1836 /* In Objective-C, an instance variable (ivar) may be preferred to
1837 whatever lookup_name() found. */
1838 decl = objc_lookup_ivar (decl, id);
1840 if (decl && decl != error_mark_node)
1843 /* Implicit function declaration. */
1844 ref = implicitly_declare (id);
1845 else if (decl == error_mark_node)
1846 /* Don't complain about something that's already been
1847 complained about. */
1848 return error_mark_node;
1851 undeclared_variable (id, loc);
1852 return error_mark_node;
1855 if (TREE_TYPE (ref) == error_mark_node)
1856 return error_mark_node;
1858 if (TREE_DEPRECATED (ref))
1859 warn_deprecated_use (ref);
1861 if (!skip_evaluation)
1862 assemble_external (ref);
1863 TREE_USED (ref) = 1;
1865 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1867 if (!in_sizeof && !in_typeof)
1868 C_DECL_USED (ref) = 1;
1869 else if (DECL_INITIAL (ref) == 0
1870 && DECL_EXTERNAL (ref)
1871 && !TREE_PUBLIC (ref))
1872 record_maybe_used_decl (ref);
1875 if (TREE_CODE (ref) == CONST_DECL)
1877 ref = DECL_INITIAL (ref);
1878 TREE_CONSTANT (ref) = 1;
1879 TREE_INVARIANT (ref) = 1;
1881 else if (current_function_decl != 0
1882 && !DECL_FILE_SCOPE_P (current_function_decl)
1883 && (TREE_CODE (ref) == VAR_DECL
1884 || TREE_CODE (ref) == PARM_DECL
1885 || TREE_CODE (ref) == FUNCTION_DECL))
1887 tree context = decl_function_context (ref);
1889 if (context != 0 && context != current_function_decl)
1890 DECL_NONLOCAL (ref) = 1;
1896 /* Record details of decls possibly used inside sizeof or typeof. */
1897 struct maybe_used_decl
1901 /* The level seen at (in_sizeof + in_typeof). */
1903 /* The next one at this level or above, or NULL. */
1904 struct maybe_used_decl *next;
1907 static struct maybe_used_decl *maybe_used_decls;
1909 /* Record that DECL, an undefined static function reference seen
1910 inside sizeof or typeof, might be used if the operand of sizeof is
1911 a VLA type or the operand of typeof is a variably modified
1915 record_maybe_used_decl (tree decl)
1917 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1919 t->level = in_sizeof + in_typeof;
1920 t->next = maybe_used_decls;
1921 maybe_used_decls = t;
1924 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1925 USED is false, just discard them. If it is true, mark them used
1926 (if no longer inside sizeof or typeof) or move them to the next
1927 level up (if still inside sizeof or typeof). */
1930 pop_maybe_used (bool used)
1932 struct maybe_used_decl *p = maybe_used_decls;
1933 int cur_level = in_sizeof + in_typeof;
1934 while (p && p->level > cur_level)
1939 C_DECL_USED (p->decl) = 1;
1941 p->level = cur_level;
1945 if (!used || cur_level == 0)
1946 maybe_used_decls = p;
1949 /* Return the result of sizeof applied to EXPR. */
1952 c_expr_sizeof_expr (struct c_expr expr)
1955 if (expr.value == error_mark_node)
1957 ret.value = error_mark_node;
1958 ret.original_code = ERROR_MARK;
1959 pop_maybe_used (false);
1963 ret.value = c_sizeof (TREE_TYPE (expr.value));
1964 ret.original_code = ERROR_MARK;
1965 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1970 /* Return the result of sizeof applied to T, a structure for the type
1971 name passed to sizeof (rather than the type itself). */
1974 c_expr_sizeof_type (struct c_type_name *t)
1978 type = groktypename (t);
1979 ret.value = c_sizeof (type);
1980 ret.original_code = ERROR_MARK;
1981 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1985 /* Build a function call to function FUNCTION with parameters PARAMS.
1986 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1987 TREE_VALUE of each node is a parameter-expression.
1988 FUNCTION's data type may be a function type or a pointer-to-function. */
1991 build_function_call (tree function, tree params)
1993 tree fntype, fundecl = 0;
1994 tree coerced_params;
1995 tree name = NULL_TREE, result;
1998 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1999 STRIP_TYPE_NOPS (function);
2001 /* Convert anything with function type to a pointer-to-function. */
2002 if (TREE_CODE (function) == FUNCTION_DECL)
2004 /* Implement type-directed function overloading for builtins.
2005 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2006 handle all the type checking. The result is a complete expression
2007 that implements this function call. */
2008 tem = resolve_overloaded_builtin (function, params);
2012 name = DECL_NAME (function);
2014 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2015 (because calling an inline function does not mean the function
2016 needs to be separately compiled). */
2017 fntype = build_type_variant (TREE_TYPE (function),
2018 TREE_READONLY (function),
2019 TREE_THIS_VOLATILE (function));
2021 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2024 function = default_conversion (function);
2026 fntype = TREE_TYPE (function);
2028 if (TREE_CODE (fntype) == ERROR_MARK)
2029 return error_mark_node;
2031 if (!(TREE_CODE (fntype) == POINTER_TYPE
2032 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2034 error ("called object %qE is not a function", function);
2035 return error_mark_node;
2038 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2039 current_function_returns_abnormally = 1;
2041 /* fntype now gets the type of function pointed to. */
2042 fntype = TREE_TYPE (fntype);
2044 /* Check that the function is called through a compatible prototype.
2045 If it is not, replace the call by a trap, wrapped up in a compound
2046 expression if necessary. This has the nice side-effect to prevent
2047 the tree-inliner from generating invalid assignment trees which may
2048 blow up in the RTL expander later.
2050 ??? This doesn't work for Objective-C because objc_comptypes
2051 refuses to compare function prototypes, yet the compiler appears
2052 to build calls that are flagged as invalid by C's comptypes. */
2053 if (!c_dialect_objc ()
2054 && TREE_CODE (function) == NOP_EXPR
2055 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2056 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2057 && !comptypes (fntype, TREE_TYPE (tem)))
2059 tree return_type = TREE_TYPE (fntype);
2060 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2063 /* This situation leads to run-time undefined behavior. We can't,
2064 therefore, simply error unless we can prove that all possible
2065 executions of the program must execute the code. */
2066 warning (0, "function called through a non-compatible type");
2068 /* We can, however, treat "undefined" any way we please.
2069 Call abort to encourage the user to fix the program. */
2070 inform ("if this code is reached, the program will abort");
2072 if (VOID_TYPE_P (return_type))
2078 if (AGGREGATE_TYPE_P (return_type))
2079 rhs = build_compound_literal (return_type,
2080 build_constructor (return_type,
2083 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2085 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2089 /* Convert the parameters to the types declared in the
2090 function prototype, or apply default promotions. */
2093 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2095 if (coerced_params == error_mark_node)
2096 return error_mark_node;
2098 /* Check that the arguments to the function are valid. */
2100 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2102 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2103 function, coerced_params, NULL_TREE);
2104 TREE_SIDE_EFFECTS (result) = 1;
2106 if (require_constant_value)
2108 result = fold_initializer (result);
2110 if (TREE_CONSTANT (result)
2111 && (name == NULL_TREE
2112 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2113 pedwarn_init ("initializer element is not constant");
2116 result = fold (result);
2118 if (VOID_TYPE_P (TREE_TYPE (result)))
2120 return require_complete_type (result);
2123 /* Convert the argument expressions in the list VALUES
2124 to the types in the list TYPELIST. The result is a list of converted
2125 argument expressions, unless there are too few arguments in which
2126 case it is error_mark_node.
2128 If TYPELIST is exhausted, or when an element has NULL as its type,
2129 perform the default conversions.
2131 PARMLIST is the chain of parm decls for the function being called.
2132 It may be 0, if that info is not available.
2133 It is used only for generating error messages.
2135 FUNCTION is a tree for the called function. It is used only for
2136 error messages, where it is formatted with %qE.
2138 This is also where warnings about wrong number of args are generated.
2140 Both VALUES and the returned value are chains of TREE_LIST nodes
2141 with the elements of the list in the TREE_VALUE slots of those nodes. */
2144 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2146 tree typetail, valtail;
2151 /* Change pointer to function to the function itself for
2153 if (TREE_CODE (function) == ADDR_EXPR
2154 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2155 function = TREE_OPERAND (function, 0);
2157 /* Handle an ObjC selector specially for diagnostics. */
2158 selector = objc_message_selector ();
2160 /* Scan the given expressions and types, producing individual
2161 converted arguments and pushing them on RESULT in reverse order. */
2163 for (valtail = values, typetail = typelist, parmnum = 0;
2165 valtail = TREE_CHAIN (valtail), parmnum++)
2167 tree type = typetail ? TREE_VALUE (typetail) : 0;
2168 tree val = TREE_VALUE (valtail);
2169 tree rname = function;
2170 int argnum = parmnum + 1;
2171 const char *invalid_func_diag;
2173 if (type == void_type_node)
2175 error ("too many arguments to function %qE", function);
2179 if (selector && argnum > 2)
2185 STRIP_TYPE_NOPS (val);
2187 val = default_function_array_conversion (val);
2189 val = require_complete_type (val);
2193 /* Formal parm type is specified by a function prototype. */
2196 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2198 error ("type of formal parameter %d is incomplete", parmnum + 1);
2203 /* Optionally warn about conversions that
2204 differ from the default conversions. */
2205 if (warn_conversion || warn_traditional)
2207 unsigned int formal_prec = TYPE_PRECISION (type);
2209 if (INTEGRAL_TYPE_P (type)
2210 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2211 warning (0, "passing argument %d of %qE as integer "
2212 "rather than floating due to prototype",
2214 if (INTEGRAL_TYPE_P (type)
2215 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2216 warning (0, "passing argument %d of %qE as integer "
2217 "rather than complex due to prototype",
2219 else if (TREE_CODE (type) == COMPLEX_TYPE
2220 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2221 warning (0, "passing argument %d of %qE as complex "
2222 "rather than floating due to prototype",
2224 else if (TREE_CODE (type) == REAL_TYPE
2225 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2226 warning (0, "passing argument %d of %qE as floating "
2227 "rather than integer due to prototype",
2229 else if (TREE_CODE (type) == COMPLEX_TYPE
2230 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2231 warning (0, "passing argument %d of %qE as complex "
2232 "rather than integer due to prototype",
2234 else if (TREE_CODE (type) == REAL_TYPE
2235 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2236 warning (0, "passing argument %d of %qE as floating "
2237 "rather than complex due to prototype",
2239 /* ??? At some point, messages should be written about
2240 conversions between complex types, but that's too messy
2242 else if (TREE_CODE (type) == REAL_TYPE
2243 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2245 /* Warn if any argument is passed as `float',
2246 since without a prototype it would be `double'. */
2247 if (formal_prec == TYPE_PRECISION (float_type_node))
2248 warning (0, "passing argument %d of %qE as %<float%> "
2249 "rather than %<double%> due to prototype",
2252 /* Detect integer changing in width or signedness.
2253 These warnings are only activated with
2254 -Wconversion, not with -Wtraditional. */
2255 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2256 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2258 tree would_have_been = default_conversion (val);
2259 tree type1 = TREE_TYPE (would_have_been);
2261 if (TREE_CODE (type) == ENUMERAL_TYPE
2262 && (TYPE_MAIN_VARIANT (type)
2263 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2264 /* No warning if function asks for enum
2265 and the actual arg is that enum type. */
2267 else if (formal_prec != TYPE_PRECISION (type1))
2268 warning (0, "passing argument %d of %qE with different "
2269 "width due to prototype", argnum, rname);
2270 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2272 /* Don't complain if the formal parameter type
2273 is an enum, because we can't tell now whether
2274 the value was an enum--even the same enum. */
2275 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2277 else if (TREE_CODE (val) == INTEGER_CST
2278 && int_fits_type_p (val, type))
2279 /* Change in signedness doesn't matter
2280 if a constant value is unaffected. */
2282 /* If the value is extended from a narrower
2283 unsigned type, it doesn't matter whether we
2284 pass it as signed or unsigned; the value
2285 certainly is the same either way. */
2286 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2287 && TYPE_UNSIGNED (TREE_TYPE (val)))
2289 else if (TYPE_UNSIGNED (type))
2290 warning (0, "passing argument %d of %qE as unsigned "
2291 "due to prototype", argnum, rname);
2293 warning (0, "passing argument %d of %qE as signed "
2294 "due to prototype", argnum, rname);
2298 parmval = convert_for_assignment (type, val, ic_argpass,
2302 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2303 && INTEGRAL_TYPE_P (type)
2304 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2305 parmval = default_conversion (parmval);
2307 result = tree_cons (NULL_TREE, parmval, result);
2309 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2310 && (TYPE_PRECISION (TREE_TYPE (val))
2311 < TYPE_PRECISION (double_type_node)))
2312 /* Convert `float' to `double'. */
2313 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2314 else if ((invalid_func_diag =
2315 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2317 error (invalid_func_diag);
2318 return error_mark_node;
2321 /* Convert `short' and `char' to full-size `int'. */
2322 result = tree_cons (NULL_TREE, default_conversion (val), result);
2325 typetail = TREE_CHAIN (typetail);
2328 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2330 error ("too few arguments to function %qE", function);
2331 return error_mark_node;
2334 return nreverse (result);
2337 /* This is the entry point used by the parser to build unary operators
2338 in the input. CODE, a tree_code, specifies the unary operator, and
2339 ARG is the operand. For unary plus, the C parser currently uses
2340 CONVERT_EXPR for code. */
2343 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2345 struct c_expr result;
2347 result.original_code = ERROR_MARK;
2348 result.value = build_unary_op (code, arg.value, 0);
2349 overflow_warning (result.value);
2353 /* This is the entry point used by the parser to build binary operators
2354 in the input. CODE, a tree_code, specifies the binary operator, and
2355 ARG1 and ARG2 are the operands. In addition to constructing the
2356 expression, we check for operands that were written with other binary
2357 operators in a way that is likely to confuse the user. */
2360 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2363 struct c_expr result;
2365 enum tree_code code1 = arg1.original_code;
2366 enum tree_code code2 = arg2.original_code;
2368 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2369 result.original_code = code;
2371 if (TREE_CODE (result.value) == ERROR_MARK)
2374 /* Check for cases such as x+y<<z which users are likely
2376 if (warn_parentheses)
2378 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2380 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2381 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2382 warning (0, "suggest parentheses around + or - inside shift");
2385 if (code == TRUTH_ORIF_EXPR)
2387 if (code1 == TRUTH_ANDIF_EXPR
2388 || code2 == TRUTH_ANDIF_EXPR)
2389 warning (0, "suggest parentheses around && within ||");
2392 if (code == BIT_IOR_EXPR)
2394 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2395 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2396 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2397 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2398 warning (0, "suggest parentheses around arithmetic in operand of |");
2399 /* Check cases like x|y==z */
2400 if (TREE_CODE_CLASS (code1) == tcc_comparison
2401 || TREE_CODE_CLASS (code2) == tcc_comparison)
2402 warning (0, "suggest parentheses around comparison in operand of |");
2405 if (code == BIT_XOR_EXPR)
2407 if (code1 == BIT_AND_EXPR
2408 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2409 || code2 == BIT_AND_EXPR
2410 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2411 warning (0, "suggest parentheses around arithmetic in operand of ^");
2412 /* Check cases like x^y==z */
2413 if (TREE_CODE_CLASS (code1) == tcc_comparison
2414 || TREE_CODE_CLASS (code2) == tcc_comparison)
2415 warning (0, "suggest parentheses around comparison in operand of ^");
2418 if (code == BIT_AND_EXPR)
2420 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2421 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2422 warning (0, "suggest parentheses around + or - in operand of &");
2423 /* Check cases like x&y==z */
2424 if (TREE_CODE_CLASS (code1) == tcc_comparison
2425 || TREE_CODE_CLASS (code2) == tcc_comparison)
2426 warning (0, "suggest parentheses around comparison in operand of &");
2428 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2429 if (TREE_CODE_CLASS (code) == tcc_comparison
2430 && (TREE_CODE_CLASS (code1) == tcc_comparison
2431 || TREE_CODE_CLASS (code2) == tcc_comparison))
2432 warning (0, "comparisons like X<=Y<=Z do not have their mathematical meaning");
2436 unsigned_conversion_warning (result.value, arg1.value);
2437 unsigned_conversion_warning (result.value, arg2.value);
2438 overflow_warning (result.value);
2443 /* Return a tree for the difference of pointers OP0 and OP1.
2444 The resulting tree has type int. */
2447 pointer_diff (tree op0, tree op1)
2449 tree restype = ptrdiff_type_node;
2451 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2452 tree con0, con1, lit0, lit1;
2453 tree orig_op1 = op1;
2455 if (pedantic || warn_pointer_arith)
2457 if (TREE_CODE (target_type) == VOID_TYPE)
2458 pedwarn ("pointer of type %<void *%> used in subtraction");
2459 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2460 pedwarn ("pointer to a function used in subtraction");
2463 /* If the conversion to ptrdiff_type does anything like widening or
2464 converting a partial to an integral mode, we get a convert_expression
2465 that is in the way to do any simplifications.
2466 (fold-const.c doesn't know that the extra bits won't be needed.
2467 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2468 different mode in place.)
2469 So first try to find a common term here 'by hand'; we want to cover
2470 at least the cases that occur in legal static initializers. */
2471 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2472 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2474 if (TREE_CODE (con0) == PLUS_EXPR)
2476 lit0 = TREE_OPERAND (con0, 1);
2477 con0 = TREE_OPERAND (con0, 0);
2480 lit0 = integer_zero_node;
2482 if (TREE_CODE (con1) == PLUS_EXPR)
2484 lit1 = TREE_OPERAND (con1, 1);
2485 con1 = TREE_OPERAND (con1, 0);
2488 lit1 = integer_zero_node;
2490 if (operand_equal_p (con0, con1, 0))
2497 /* First do the subtraction as integers;
2498 then drop through to build the divide operator.
2499 Do not do default conversions on the minus operator
2500 in case restype is a short type. */
2502 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2503 convert (restype, op1), 0);
2504 /* This generates an error if op1 is pointer to incomplete type. */
2505 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2506 error ("arithmetic on pointer to an incomplete type");
2508 /* This generates an error if op0 is pointer to incomplete type. */
2509 op1 = c_size_in_bytes (target_type);
2511 /* Divide by the size, in easiest possible way. */
2512 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2515 /* Construct and perhaps optimize a tree representation
2516 for a unary operation. CODE, a tree_code, specifies the operation
2517 and XARG is the operand.
2518 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2519 the default promotions (such as from short to int).
2520 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2521 allows non-lvalues; this is only used to handle conversion of non-lvalue
2522 arrays to pointers in C99. */
2525 build_unary_op (enum tree_code code, tree xarg, int flag)
2527 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2530 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2532 int noconvert = flag;
2534 if (typecode == ERROR_MARK)
2535 return error_mark_node;
2536 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2537 typecode = INTEGER_TYPE;
2542 /* This is used for unary plus, because a CONVERT_EXPR
2543 is enough to prevent anybody from looking inside for
2544 associativity, but won't generate any code. */
2545 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2546 || typecode == COMPLEX_TYPE
2547 || typecode == VECTOR_TYPE))
2549 error ("wrong type argument to unary plus");
2550 return error_mark_node;
2552 else if (!noconvert)
2553 arg = default_conversion (arg);
2554 arg = non_lvalue (arg);
2558 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2559 || typecode == COMPLEX_TYPE
2560 || typecode == VECTOR_TYPE))
2562 error ("wrong type argument to unary minus");
2563 return error_mark_node;
2565 else if (!noconvert)
2566 arg = default_conversion (arg);
2570 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2573 arg = default_conversion (arg);
2575 else if (typecode == COMPLEX_TYPE)
2579 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2581 arg = default_conversion (arg);
2585 error ("wrong type argument to bit-complement");
2586 return error_mark_node;
2591 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2593 error ("wrong type argument to abs");
2594 return error_mark_node;
2596 else if (!noconvert)
2597 arg = default_conversion (arg);
2601 /* Conjugating a real value is a no-op, but allow it anyway. */
2602 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2603 || typecode == COMPLEX_TYPE))
2605 error ("wrong type argument to conjugation");
2606 return error_mark_node;
2608 else if (!noconvert)
2609 arg = default_conversion (arg);
2612 case TRUTH_NOT_EXPR:
2613 /* ??? Why do most validation here but that for non-lvalue arrays
2614 in c_objc_common_truthvalue_conversion? */
2615 if (typecode != INTEGER_TYPE
2616 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2617 && typecode != COMPLEX_TYPE
2618 /* These will convert to a pointer. */
2619 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2621 error ("wrong type argument to unary exclamation mark");
2622 return error_mark_node;
2624 arg = c_objc_common_truthvalue_conversion (arg);
2625 return invert_truthvalue (arg);
2631 if (TREE_CODE (arg) == COMPLEX_CST)
2632 return TREE_REALPART (arg);
2633 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2634 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2639 if (TREE_CODE (arg) == COMPLEX_CST)
2640 return TREE_IMAGPART (arg);
2641 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2642 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2644 return convert (TREE_TYPE (arg), integer_zero_node);
2646 case PREINCREMENT_EXPR:
2647 case POSTINCREMENT_EXPR:
2648 case PREDECREMENT_EXPR:
2649 case POSTDECREMENT_EXPR:
2651 /* Increment or decrement the real part of the value,
2652 and don't change the imaginary part. */
2653 if (typecode == COMPLEX_TYPE)
2658 pedwarn ("ISO C does not support %<++%> and %<--%>"
2659 " on complex types");
2661 arg = stabilize_reference (arg);
2662 real = build_unary_op (REALPART_EXPR, arg, 1);
2663 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2664 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2665 build_unary_op (code, real, 1), imag);
2668 /* Report invalid types. */
2670 if (typecode != POINTER_TYPE
2671 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2673 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2674 error ("wrong type argument to increment");
2676 error ("wrong type argument to decrement");
2678 return error_mark_node;
2683 tree result_type = TREE_TYPE (arg);
2685 arg = get_unwidened (arg, 0);
2686 argtype = TREE_TYPE (arg);
2688 /* Compute the increment. */
2690 if (typecode == POINTER_TYPE)
2692 /* If pointer target is an undefined struct,
2693 we just cannot know how to do the arithmetic. */
2694 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2696 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2697 error ("increment of pointer to unknown structure");
2699 error ("decrement of pointer to unknown structure");
2701 else if ((pedantic || warn_pointer_arith)
2702 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2703 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2705 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2706 pedwarn ("wrong type argument to increment");
2708 pedwarn ("wrong type argument to decrement");
2711 inc = c_size_in_bytes (TREE_TYPE (result_type));
2714 inc = integer_one_node;
2716 inc = convert (argtype, inc);
2718 /* Complain about anything else that is not a true lvalue. */
2719 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2720 || code == POSTINCREMENT_EXPR)
2723 return error_mark_node;
2725 /* Report a read-only lvalue. */
2726 if (TREE_READONLY (arg))
2727 readonly_error (arg,
2728 ((code == PREINCREMENT_EXPR
2729 || code == POSTINCREMENT_EXPR)
2730 ? lv_increment : lv_decrement));
2732 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2733 val = boolean_increment (code, arg);
2735 val = build2 (code, TREE_TYPE (arg), arg, inc);
2736 TREE_SIDE_EFFECTS (val) = 1;
2737 val = convert (result_type, val);
2738 if (TREE_CODE (val) != code)
2739 TREE_NO_WARNING (val) = 1;
2744 /* Note that this operation never does default_conversion. */
2746 /* Let &* cancel out to simplify resulting code. */
2747 if (TREE_CODE (arg) == INDIRECT_REF)
2749 /* Don't let this be an lvalue. */
2750 if (lvalue_p (TREE_OPERAND (arg, 0)))
2751 return non_lvalue (TREE_OPERAND (arg, 0));
2752 return TREE_OPERAND (arg, 0);
2755 /* For &x[y], return x+y */
2756 if (TREE_CODE (arg) == ARRAY_REF)
2758 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2759 return error_mark_node;
2760 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2761 TREE_OPERAND (arg, 1), 1);
2764 /* Anything not already handled and not a true memory reference
2765 or a non-lvalue array is an error. */
2766 else if (typecode != FUNCTION_TYPE && !flag
2767 && !lvalue_or_else (arg, lv_addressof))
2768 return error_mark_node;
2770 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2771 argtype = TREE_TYPE (arg);
2773 /* If the lvalue is const or volatile, merge that into the type
2774 to which the address will point. Note that you can't get a
2775 restricted pointer by taking the address of something, so we
2776 only have to deal with `const' and `volatile' here. */
2777 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2778 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2779 argtype = c_build_type_variant (argtype,
2780 TREE_READONLY (arg),
2781 TREE_THIS_VOLATILE (arg));
2783 if (!c_mark_addressable (arg))
2784 return error_mark_node;
2786 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2787 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2789 argtype = build_pointer_type (argtype);
2791 /* ??? Cope with user tricks that amount to offsetof. Delete this
2792 when we have proper support for integer constant expressions. */
2793 val = get_base_address (arg);
2794 if (val && TREE_CODE (val) == INDIRECT_REF
2795 && integer_zerop (TREE_OPERAND (val, 0)))
2796 return fold_convert (argtype, fold_offsetof (arg));
2798 val = build1 (ADDR_EXPR, argtype, arg);
2800 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2801 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2810 argtype = TREE_TYPE (arg);
2811 val = build1 (code, argtype, arg);
2812 return require_constant_value ? fold_initializer (val) : fold (val);
2815 /* Return nonzero if REF is an lvalue valid for this language.
2816 Lvalues can be assigned, unless their type has TYPE_READONLY.
2817 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2822 enum tree_code code = TREE_CODE (ref);
2829 return lvalue_p (TREE_OPERAND (ref, 0));
2831 case COMPOUND_LITERAL_EXPR:
2841 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2842 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2845 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2852 /* Give an error for storing in something that is 'const'. */
2855 readonly_error (tree arg, enum lvalue_use use)
2857 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2858 /* Using this macro rather than (for example) arrays of messages
2859 ensures that all the format strings are checked at compile
2861 #define READONLY_MSG(A, I, D) (use == lv_assign \
2863 : (use == lv_increment ? (I) : (D)))
2864 if (TREE_CODE (arg) == COMPONENT_REF)
2866 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2867 readonly_error (TREE_OPERAND (arg, 0), use);
2869 error (READONLY_MSG (N_("assignment of read-only member %qD"),
2870 N_("increment of read-only member %qD"),
2871 N_("decrement of read-only member %qD")),
2872 TREE_OPERAND (arg, 1));
2874 else if (TREE_CODE (arg) == VAR_DECL)
2875 error (READONLY_MSG (N_("assignment of read-only variable %qD"),
2876 N_("increment of read-only variable %qD"),
2877 N_("decrement of read-only variable %qD")),
2880 error (READONLY_MSG (N_("assignment of read-only location"),
2881 N_("increment of read-only location"),
2882 N_("decrement of read-only location")));
2886 /* Return nonzero if REF is an lvalue valid for this language;
2887 otherwise, print an error message and return zero. USE says
2888 how the lvalue is being used and so selects the error message. */
2891 lvalue_or_else (tree ref, enum lvalue_use use)
2893 int win = lvalue_p (ref);
2901 /* Mark EXP saying that we need to be able to take the
2902 address of it; it should not be allocated in a register.
2903 Returns true if successful. */
2906 c_mark_addressable (tree exp)
2911 switch (TREE_CODE (x))
2914 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2917 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2921 /* ... fall through ... */
2927 x = TREE_OPERAND (x, 0);
2930 case COMPOUND_LITERAL_EXPR:
2932 TREE_ADDRESSABLE (x) = 1;
2939 if (C_DECL_REGISTER (x)
2940 && DECL_NONLOCAL (x))
2942 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2945 ("global register variable %qD used in nested function", x);
2948 pedwarn ("register variable %qD used in nested function", x);
2950 else if (C_DECL_REGISTER (x))
2952 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2953 error ("address of global register variable %qD requested", x);
2955 error ("address of register variable %qD requested", x);
2961 TREE_ADDRESSABLE (x) = 1;
2968 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2971 build_conditional_expr (tree ifexp, tree op1, tree op2)
2975 enum tree_code code1;
2976 enum tree_code code2;
2977 tree result_type = NULL;
2978 tree orig_op1 = op1, orig_op2 = op2;
2980 /* Promote both alternatives. */
2982 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2983 op1 = default_conversion (op1);
2984 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2985 op2 = default_conversion (op2);
2987 if (TREE_CODE (ifexp) == ERROR_MARK
2988 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2989 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2990 return error_mark_node;
2992 type1 = TREE_TYPE (op1);
2993 code1 = TREE_CODE (type1);
2994 type2 = TREE_TYPE (op2);
2995 code2 = TREE_CODE (type2);
2997 /* C90 does not permit non-lvalue arrays in conditional expressions.
2998 In C99 they will be pointers by now. */
2999 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3001 error ("non-lvalue array in conditional expression");
3002 return error_mark_node;
3005 /* Quickly detect the usual case where op1 and op2 have the same type
3007 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3010 result_type = type1;
3012 result_type = TYPE_MAIN_VARIANT (type1);
3014 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3015 || code1 == COMPLEX_TYPE)
3016 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3017 || code2 == COMPLEX_TYPE))
3019 result_type = c_common_type (type1, type2);
3021 /* If -Wsign-compare, warn here if type1 and type2 have
3022 different signedness. We'll promote the signed to unsigned
3023 and later code won't know it used to be different.
3024 Do this check on the original types, so that explicit casts
3025 will be considered, but default promotions won't. */
3026 if (warn_sign_compare && !skip_evaluation)
3028 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3029 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3031 if (unsigned_op1 ^ unsigned_op2)
3033 /* Do not warn if the result type is signed, since the
3034 signed type will only be chosen if it can represent
3035 all the values of the unsigned type. */
3036 if (!TYPE_UNSIGNED (result_type))
3038 /* Do not warn if the signed quantity is an unsuffixed
3039 integer literal (or some static constant expression
3040 involving such literals) and it is non-negative. */
3041 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3042 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3045 warning (0, "signed and unsigned type in conditional expression");
3049 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3051 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3052 pedwarn ("ISO C forbids conditional expr with only one void side");
3053 result_type = void_type_node;
3055 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3057 if (comp_target_types (type1, type2, 1))
3058 result_type = common_pointer_type (type1, type2);
3059 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3060 && TREE_CODE (orig_op1) != NOP_EXPR)
3061 result_type = qualify_type (type2, type1);
3062 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3063 && TREE_CODE (orig_op2) != NOP_EXPR)
3064 result_type = qualify_type (type1, type2);
3065 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3067 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3068 pedwarn ("ISO C forbids conditional expr between "
3069 "%<void *%> and function pointer");
3070 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3071 TREE_TYPE (type2)));
3073 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3075 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3076 pedwarn ("ISO C forbids conditional expr between "
3077 "%<void *%> and function pointer");
3078 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3079 TREE_TYPE (type1)));
3083 pedwarn ("pointer type mismatch in conditional expression");
3084 result_type = build_pointer_type (void_type_node);
3087 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3089 if (!integer_zerop (op2))
3090 pedwarn ("pointer/integer type mismatch in conditional expression");
3093 op2 = null_pointer_node;
3095 result_type = type1;
3097 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3099 if (!integer_zerop (op1))
3100 pedwarn ("pointer/integer type mismatch in conditional expression");
3103 op1 = null_pointer_node;
3105 result_type = type2;
3110 if (flag_cond_mismatch)
3111 result_type = void_type_node;
3114 error ("type mismatch in conditional expression");
3115 return error_mark_node;
3119 /* Merge const and volatile flags of the incoming types. */
3121 = build_type_variant (result_type,
3122 TREE_READONLY (op1) || TREE_READONLY (op2),
3123 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3125 if (result_type != TREE_TYPE (op1))
3126 op1 = convert_and_check (result_type, op1);
3127 if (result_type != TREE_TYPE (op2))
3128 op2 = convert_and_check (result_type, op2);
3130 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3133 /* Return a compound expression that performs two expressions and
3134 returns the value of the second of them. */
3137 build_compound_expr (tree expr1, tree expr2)
3139 /* Convert arrays and functions to pointers. */
3140 expr2 = default_function_array_conversion (expr2);
3142 if (!TREE_SIDE_EFFECTS (expr1))
3144 /* The left-hand operand of a comma expression is like an expression
3145 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3146 any side-effects, unless it was explicitly cast to (void). */
3147 if (warn_unused_value)
3149 if (VOID_TYPE_P (TREE_TYPE (expr1))
3150 && TREE_CODE (expr1) == CONVERT_EXPR)
3152 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3153 && TREE_CODE (expr1) == COMPOUND_EXPR
3154 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3155 ; /* (void) a, (void) b, c */
3157 warning (0, "left-hand operand of comma expression has no effect");
3161 /* With -Wunused, we should also warn if the left-hand operand does have
3162 side-effects, but computes a value which is not used. For example, in
3163 `foo() + bar(), baz()' the result of the `+' operator is not used,
3164 so we should issue a warning. */
3165 else if (warn_unused_value)
3166 warn_if_unused_value (expr1, input_location);
3168 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3171 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3174 build_c_cast (tree type, tree expr)
3178 if (type == error_mark_node || expr == error_mark_node)
3179 return error_mark_node;
3181 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3182 only in <protocol> qualifications. But when constructing cast expressions,
3183 the protocols do matter and must be kept around. */
3184 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3185 return build1 (NOP_EXPR, type, expr);
3187 type = TYPE_MAIN_VARIANT (type);
3189 if (TREE_CODE (type) == ARRAY_TYPE)
3191 error ("cast specifies array type");
3192 return error_mark_node;
3195 if (TREE_CODE (type) == FUNCTION_TYPE)
3197 error ("cast specifies function type");
3198 return error_mark_node;
3201 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3205 if (TREE_CODE (type) == RECORD_TYPE
3206 || TREE_CODE (type) == UNION_TYPE)
3207 pedwarn ("ISO C forbids casting nonscalar to the same type");
3210 else if (TREE_CODE (type) == UNION_TYPE)
3213 value = default_function_array_conversion (value);
3215 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3216 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3217 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3225 pedwarn ("ISO C forbids casts to union type");
3226 t = digest_init (type,
3227 build_constructor (type,
3228 build_tree_list (field, value)),
3230 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3231 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3234 error ("cast to union type from type not present in union");
3235 return error_mark_node;
3241 /* If casting to void, avoid the error that would come
3242 from default_conversion in the case of a non-lvalue array. */
3243 if (type == void_type_node)
3244 return build1 (CONVERT_EXPR, type, value);
3246 /* Convert functions and arrays to pointers,
3247 but don't convert any other types. */
3248 value = default_function_array_conversion (value);
3249 otype = TREE_TYPE (value);
3251 /* Optionally warn about potentially worrisome casts. */
3254 && TREE_CODE (type) == POINTER_TYPE
3255 && TREE_CODE (otype) == POINTER_TYPE)
3257 tree in_type = type;
3258 tree in_otype = otype;
3262 /* Check that the qualifiers on IN_TYPE are a superset of
3263 the qualifiers of IN_OTYPE. The outermost level of
3264 POINTER_TYPE nodes is uninteresting and we stop as soon
3265 as we hit a non-POINTER_TYPE node on either type. */
3268 in_otype = TREE_TYPE (in_otype);
3269 in_type = TREE_TYPE (in_type);
3271 /* GNU C allows cv-qualified function types. 'const'
3272 means the function is very pure, 'volatile' means it
3273 can't return. We need to warn when such qualifiers
3274 are added, not when they're taken away. */
3275 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3276 && TREE_CODE (in_type) == FUNCTION_TYPE)
3277 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3279 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3281 while (TREE_CODE (in_type) == POINTER_TYPE
3282 && TREE_CODE (in_otype) == POINTER_TYPE);
3285 warning (0, "cast adds new qualifiers to function type");
3288 /* There are qualifiers present in IN_OTYPE that are not
3289 present in IN_TYPE. */
3290 warning (0, "cast discards qualifiers from pointer target type");
3293 /* Warn about possible alignment problems. */
3294 if (STRICT_ALIGNMENT && warn_cast_align
3295 && TREE_CODE (type) == POINTER_TYPE
3296 && TREE_CODE (otype) == POINTER_TYPE
3297 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3298 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3299 /* Don't warn about opaque types, where the actual alignment
3300 restriction is unknown. */
3301 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3302 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3303 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3304 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3305 warning (0, "cast increases required alignment of target type");
3307 if (warn_pointer_to_int_cast
3308 && TREE_CODE (type) == INTEGER_TYPE
3309 && TREE_CODE (otype) == POINTER_TYPE
3310 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3311 && !TREE_CONSTANT (value))
3312 warning (0, "cast from pointer to integer of different size");
3314 if (warn_bad_function_cast
3315 && TREE_CODE (value) == CALL_EXPR
3316 && TREE_CODE (type) != TREE_CODE (otype))
3317 warning (0, "cast from function call of type %qT to non-matching "
3318 "type %qT", otype, type);
3320 if (warn_int_to_pointer_cast
3321 && TREE_CODE (type) == POINTER_TYPE
3322 && TREE_CODE (otype) == INTEGER_TYPE
3323 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3324 /* Don't warn about converting any constant. */
3325 && !TREE_CONSTANT (value))
3326 warning (0, "cast to pointer from integer of different size");
3328 if (TREE_CODE (type) == POINTER_TYPE
3329 && TREE_CODE (otype) == POINTER_TYPE
3330 && TREE_CODE (expr) == ADDR_EXPR
3331 && DECL_P (TREE_OPERAND (expr, 0))
3332 && flag_strict_aliasing && warn_strict_aliasing
3333 && !VOID_TYPE_P (TREE_TYPE (type)))
3335 /* Casting the address of a decl to non void pointer. Warn
3336 if the cast breaks type based aliasing. */
3337 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3338 warning (0, "type-punning to incomplete type might break strict-aliasing rules");
3341 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3342 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3344 if (!alias_sets_conflict_p (set1, set2))
3345 warning (0, "dereferencing type-punned pointer will break strict-aliasing rules");
3346 else if (warn_strict_aliasing > 1
3347 && !alias_sets_might_conflict_p (set1, set2))
3348 warning (0, "dereferencing type-punned pointer might break strict-aliasing rules");
3352 /* If pedantic, warn for conversions between function and object
3353 pointer types, except for converting a null pointer constant
3354 to function pointer type. */
3356 && TREE_CODE (type) == POINTER_TYPE
3357 && TREE_CODE (otype) == POINTER_TYPE
3358 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3359 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3360 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3363 && TREE_CODE (type) == POINTER_TYPE
3364 && TREE_CODE (otype) == POINTER_TYPE
3365 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3366 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3367 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3368 && TREE_CODE (expr) != NOP_EXPR))
3369 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3372 value = convert (type, value);
3374 /* Ignore any integer overflow caused by the cast. */
3375 if (TREE_CODE (value) == INTEGER_CST)
3377 if (EXPR_P (ovalue))
3378 /* If OVALUE had overflow set, then so will VALUE, so it
3379 is safe to overwrite. */
3380 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3382 TREE_OVERFLOW (value) = 0;
3384 if (CONSTANT_CLASS_P (ovalue))
3385 /* Similarly, constant_overflow cannot have become
3387 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3391 /* Don't let a cast be an lvalue. */
3393 value = non_lvalue (value);
3398 /* Interpret a cast of expression EXPR to type TYPE. */
3400 c_cast_expr (struct c_type_name *type_name, tree expr)
3403 int saved_wsp = warn_strict_prototypes;
3405 /* This avoids warnings about unprototyped casts on
3406 integers. E.g. "#define SIG_DFL (void(*)())0". */
3407 if (TREE_CODE (expr) == INTEGER_CST)
3408 warn_strict_prototypes = 0;
3409 type = groktypename (type_name);
3410 warn_strict_prototypes = saved_wsp;
3412 return build_c_cast (type, expr);
3416 /* Build an assignment expression of lvalue LHS from value RHS.
3417 MODIFYCODE is the code for a binary operator that we use
3418 to combine the old value of LHS with RHS to get the new value.
3419 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3422 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3426 tree lhstype = TREE_TYPE (lhs);
3427 tree olhstype = lhstype;
3429 /* Types that aren't fully specified cannot be used in assignments. */
3430 lhs = require_complete_type (lhs);
3432 /* Avoid duplicate error messages from operands that had errors. */
3433 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3434 return error_mark_node;
3436 STRIP_TYPE_NOPS (rhs);
3440 /* If a binary op has been requested, combine the old LHS value with the RHS
3441 producing the value we should actually store into the LHS. */
3443 if (modifycode != NOP_EXPR)
3445 lhs = stabilize_reference (lhs);
3446 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3449 if (!lvalue_or_else (lhs, lv_assign))
3450 return error_mark_node;
3452 /* Give an error for storing in something that is 'const'. */
3454 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3455 || ((TREE_CODE (lhstype) == RECORD_TYPE
3456 || TREE_CODE (lhstype) == UNION_TYPE)
3457 && C_TYPE_FIELDS_READONLY (lhstype)))
3458 readonly_error (lhs, lv_assign);
3460 /* If storing into a structure or union member,
3461 it has probably been given type `int'.
3462 Compute the type that would go with
3463 the actual amount of storage the member occupies. */
3465 if (TREE_CODE (lhs) == COMPONENT_REF
3466 && (TREE_CODE (lhstype) == INTEGER_TYPE
3467 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3468 || TREE_CODE (lhstype) == REAL_TYPE
3469 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3470 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3472 /* If storing in a field that is in actuality a short or narrower than one,
3473 we must store in the field in its actual type. */
3475 if (lhstype != TREE_TYPE (lhs))
3477 lhs = copy_node (lhs);
3478 TREE_TYPE (lhs) = lhstype;
3481 /* Convert new value to destination type. */
3483 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3484 NULL_TREE, NULL_TREE, 0);
3485 if (TREE_CODE (newrhs) == ERROR_MARK)
3486 return error_mark_node;
3488 /* Scan operands. */
3490 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3491 TREE_SIDE_EFFECTS (result) = 1;
3493 /* If we got the LHS in a different type for storing in,
3494 convert the result back to the nominal type of LHS
3495 so that the value we return always has the same type
3496 as the LHS argument. */
3498 if (olhstype == TREE_TYPE (result))
3500 return convert_for_assignment (olhstype, result, ic_assign,
3501 NULL_TREE, NULL_TREE, 0);
3504 /* Convert value RHS to type TYPE as preparation for an assignment
3505 to an lvalue of type TYPE.
3506 The real work of conversion is done by `convert'.
3507 The purpose of this function is to generate error messages
3508 for assignments that are not allowed in C.
3509 ERRTYPE says whether it is argument passing, assignment,
3510 initialization or return.
3512 FUNCTION is a tree for the function being called.
3513 PARMNUM is the number of the argument, for printing in error messages. */
3516 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3517 tree fundecl, tree function, int parmnum)
3519 enum tree_code codel = TREE_CODE (type);
3521 enum tree_code coder;
3522 tree rname = NULL_TREE;
3524 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3527 /* Change pointer to function to the function itself for
3529 if (TREE_CODE (function) == ADDR_EXPR
3530 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3531 function = TREE_OPERAND (function, 0);
3533 /* Handle an ObjC selector specially for diagnostics. */
3534 selector = objc_message_selector ();
3536 if (selector && parmnum > 2)
3543 /* This macro is used to emit diagnostics to ensure that all format
3544 strings are complete sentences, visible to gettext and checked at
3546 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3551 pedwarn (AR, parmnum, rname); \
3553 case ic_argpass_nonproto: \
3554 warning (0, AR, parmnum, rname); \
3566 gcc_unreachable (); \
3570 STRIP_TYPE_NOPS (rhs);
3572 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3573 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3574 rhs = default_conversion (rhs);
3575 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3576 rhs = decl_constant_value_for_broken_optimization (rhs);
3578 rhstype = TREE_TYPE (rhs);
3579 coder = TREE_CODE (rhstype);
3581 if (coder == ERROR_MARK)
3582 return error_mark_node;
3584 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3586 overflow_warning (rhs);
3587 /* Check for Objective-C protocols. This will automatically
3588 issue a warning if there are protocol violations. No need to
3589 use the return value. */
3590 if (c_dialect_objc ())
3591 objc_comptypes (type, rhstype, 0);
3595 if (coder == VOID_TYPE)
3597 /* Except for passing an argument to an unprototyped function,
3598 this is a constraint violation. When passing an argument to
3599 an unprototyped function, it is compile-time undefined;
3600 making it a constraint in that case was rejected in
3602 error ("void value not ignored as it ought to be");
3603 return error_mark_node;
3605 /* A type converts to a reference to it.
3606 This code doesn't fully support references, it's just for the
3607 special case of va_start and va_copy. */
3608 if (codel == REFERENCE_TYPE
3609 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3611 if (!lvalue_p (rhs))
3613 error ("cannot pass rvalue to reference parameter");
3614 return error_mark_node;
3616 if (!c_mark_addressable (rhs))
3617 return error_mark_node;
3618 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3620 /* We already know that these two types are compatible, but they
3621 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3622 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3623 likely to be va_list, a typedef to __builtin_va_list, which
3624 is different enough that it will cause problems later. */
3625 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3626 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3628 rhs = build1 (NOP_EXPR, type, rhs);
3631 /* Some types can interconvert without explicit casts. */
3632 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3633 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3634 return convert (type, rhs);
3635 /* Arithmetic types all interconvert, and enum is treated like int. */
3636 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3637 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3638 || codel == BOOLEAN_TYPE)
3639 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3640 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3641 || coder == BOOLEAN_TYPE))
3642 return convert_and_check (type, rhs);
3644 /* Conversion to a transparent union from its member types.
3645 This applies only to function arguments. */
3646 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3647 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3650 tree marginal_memb_type = 0;
3652 for (memb_types = TYPE_FIELDS (type); memb_types;
3653 memb_types = TREE_CHAIN (memb_types))
3655 tree memb_type = TREE_TYPE (memb_types);
3657 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3658 TYPE_MAIN_VARIANT (rhstype)))
3661 if (TREE_CODE (memb_type) != POINTER_TYPE)
3664 if (coder == POINTER_TYPE)
3666 tree ttl = TREE_TYPE (memb_type);
3667 tree ttr = TREE_TYPE (rhstype);
3669 /* Any non-function converts to a [const][volatile] void *
3670 and vice versa; otherwise, targets must be the same.
3671 Meanwhile, the lhs target must have all the qualifiers of
3673 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3674 || comp_target_types (memb_type, rhstype, 0))
3676 /* If this type won't generate any warnings, use it. */
3677 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3678 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3679 && TREE_CODE (ttl) == FUNCTION_TYPE)
3680 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3681 == TYPE_QUALS (ttr))
3682 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3683 == TYPE_QUALS (ttl))))
3686 /* Keep looking for a better type, but remember this one. */
3687 if (!marginal_memb_type)
3688 marginal_memb_type = memb_type;
3692 /* Can convert integer zero to any pointer type. */
3693 if (integer_zerop (rhs)
3694 || (TREE_CODE (rhs) == NOP_EXPR
3695 && integer_zerop (TREE_OPERAND (rhs, 0))))
3697 rhs = null_pointer_node;
3702 if (memb_types || marginal_memb_type)
3706 /* We have only a marginally acceptable member type;
3707 it needs a warning. */
3708 tree ttl = TREE_TYPE (marginal_memb_type);
3709 tree ttr = TREE_TYPE (rhstype);
3711 /* Const and volatile mean something different for function
3712 types, so the usual warnings are not appropriate. */
3713 if (TREE_CODE (ttr) == FUNCTION_TYPE
3714 && TREE_CODE (ttl) == FUNCTION_TYPE)
3716 /* Because const and volatile on functions are
3717 restrictions that say the function will not do
3718 certain things, it is okay to use a const or volatile
3719 function where an ordinary one is wanted, but not
3721 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3722 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3723 "makes qualified function "
3724 "pointer from unqualified"),
3725 N_("assignment makes qualified "
3726 "function pointer from "
3728 N_("initialization makes qualified "
3729 "function pointer from "
3731 N_("return makes qualified function "
3732 "pointer from unqualified"));
3734 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3735 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3736 "qualifiers from pointer target type"),
3737 N_("assignment discards qualifiers "
3738 "from pointer target type"),
3739 N_("initialization discards qualifiers "
3740 "from pointer target type"),
3741 N_("return discards qualifiers from "
3742 "pointer target type"));
3745 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3746 pedwarn ("ISO C prohibits argument conversion to union type");
3748 return build1 (NOP_EXPR, type, rhs);
3752 /* Conversions among pointers */
3753 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3754 && (coder == codel))
3756 tree ttl = TREE_TYPE (type);
3757 tree ttr = TREE_TYPE (rhstype);
3760 bool is_opaque_pointer;
3761 int target_cmp = 0; /* Cache comp_target_types () result. */
3763 if (TREE_CODE (mvl) != ARRAY_TYPE)
3764 mvl = TYPE_MAIN_VARIANT (mvl);
3765 if (TREE_CODE (mvr) != ARRAY_TYPE)
3766 mvr = TYPE_MAIN_VARIANT (mvr);
3767 /* Opaque pointers are treated like void pointers. */
3768 is_opaque_pointer = (targetm.vector_opaque_p (type)
3769 || targetm.vector_opaque_p (rhstype))
3770 && TREE_CODE (ttl) == VECTOR_TYPE
3771 && TREE_CODE (ttr) == VECTOR_TYPE;
3773 /* Any non-function converts to a [const][volatile] void *
3774 and vice versa; otherwise, targets must be the same.
3775 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3776 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3777 || (target_cmp = comp_target_types (type, rhstype, 0))
3778 || is_opaque_pointer
3779 || (c_common_unsigned_type (mvl)
3780 == c_common_unsigned_type (mvr)))
3783 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3786 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3787 which are not ANSI null ptr constants. */
3788 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3789 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3790 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3791 "%qE between function pointer "
3793 N_("ISO C forbids assignment between "
3794 "function pointer and %<void *%>"),
3795 N_("ISO C forbids initialization between "
3796 "function pointer and %<void *%>"),
3797 N_("ISO C forbids return between function "
3798 "pointer and %<void *%>"));
3799 /* Const and volatile mean something different for function types,
3800 so the usual warnings are not appropriate. */
3801 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3802 && TREE_CODE (ttl) != FUNCTION_TYPE)
3804 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3805 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3806 "qualifiers from pointer target type"),
3807 N_("assignment discards qualifiers "
3808 "from pointer target type"),
3809 N_("initialization discards qualifiers "
3810 "from pointer target type"),
3811 N_("return discards qualifiers from "
3812 "pointer target type"));
3813 /* If this is not a case of ignoring a mismatch in signedness,
3815 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3818 /* If there is a mismatch, do warn. */
3819 else if (warn_pointer_sign)
3820 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3821 "%d of %qE differ in signedness"),
3822 N_("pointer targets in assignment "
3823 "differ in signedness"),
3824 N_("pointer targets in initialization "
3825 "differ in signedness"),
3826 N_("pointer targets in return differ "
3829 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3830 && TREE_CODE (ttr) == FUNCTION_TYPE)
3832 /* Because const and volatile on functions are restrictions
3833 that say the function will not do certain things,
3834 it is okay to use a const or volatile function
3835 where an ordinary one is wanted, but not vice-versa. */
3836 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3837 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3838 "qualified function pointer "
3839 "from unqualified"),
3840 N_("assignment makes qualified function "
3841 "pointer from unqualified"),
3842 N_("initialization makes qualified "
3843 "function pointer from unqualified"),
3844 N_("return makes qualified function "
3845 "pointer from unqualified"));
3849 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3850 "incompatible pointer type"),
3851 N_("assignment from incompatible pointer type"),
3852 N_("initialization from incompatible "
3854 N_("return from incompatible pointer type"));
3855 return convert (type, rhs);
3857 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3859 /* ??? This should not be an error when inlining calls to
3860 unprototyped functions. */
3861 error ("invalid use of non-lvalue array");
3862 return error_mark_node;
3864 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3866 /* An explicit constant 0 can convert to a pointer,
3867 or one that results from arithmetic, even including
3868 a cast to integer type. */
3869 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3871 !(TREE_CODE (rhs) == NOP_EXPR
3872 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3873 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3874 && integer_zerop (TREE_OPERAND (rhs, 0))))
3875 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3876 "pointer from integer without a cast"),
3877 N_("assignment makes pointer from integer "
3879 N_("initialization makes pointer from "
3880 "integer without a cast"),
3881 N_("return makes pointer from integer "
3884 return convert (type, rhs);
3886 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3888 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3889 "from pointer without a cast"),
3890 N_("assignment makes integer from pointer "
3892 N_("initialization makes integer from pointer "
3894 N_("return makes integer from pointer "
3896 return convert (type, rhs);
3898 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3899 return convert (type, rhs);
3904 case ic_argpass_nonproto:
3905 /* ??? This should not be an error when inlining calls to
3906 unprototyped functions. */
3907 error ("incompatible type for argument %d of %qE", parmnum, rname);
3910 error ("incompatible types in assignment");
3913 error ("incompatible types in initialization");
3916 error ("incompatible types in return");
3922 return error_mark_node;
3925 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3926 is used for error and waring reporting and indicates which argument
3927 is being processed. */
3930 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3934 /* If FN was prototyped, the value has been converted already
3935 in convert_arguments. */
3936 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3939 type = TREE_TYPE (parm);
3940 ret = convert_for_assignment (type, value,
3941 ic_argpass_nonproto, fn,
3943 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3944 && INTEGRAL_TYPE_P (type)
3945 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3946 ret = default_conversion (ret);
3950 /* If VALUE is a compound expr all of whose expressions are constant, then
3951 return its value. Otherwise, return error_mark_node.
3953 This is for handling COMPOUND_EXPRs as initializer elements
3954 which is allowed with a warning when -pedantic is specified. */
3957 valid_compound_expr_initializer (tree value, tree endtype)