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, 2006
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 /* This file is part of the C front end.
25 It contains routines to build C expressions given their operands,
26 including computing the types of the result, C-specific error checks,
27 and some optimization. */
31 #include "coretypes.h"
35 #include "langhooks.h"
45 #include "tree-iterator.h"
46 #include "tree-gimple.h"
47 #include "tree-flow.h"
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
59 /* The level of nesting inside "__alignof__". */
62 /* The level of nesting inside "sizeof". */
65 /* The level of nesting inside "typeof". */
68 struct c_label_context_se *label_context_stack_se;
69 struct c_label_context_vm *label_context_stack_vm;
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
75 static int require_constant_value;
76 static int require_constant_elements;
78 static bool null_pointer_constant_p (tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (tree, tree);
81 static int comp_target_types (tree, tree);
82 static int function_types_compatible_p (tree, tree);
83 static int type_lists_compatible_p (tree, tree);
84 static tree decl_constant_value_for_broken_optimization (tree);
85 static tree lookup_field (tree, tree);
86 static tree convert_arguments (tree, tree, tree, tree);
87 static tree pointer_diff (tree, tree);
88 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
90 static tree valid_compound_expr_initializer (tree, tree);
91 static void push_string (const char *);
92 static void push_member_name (tree);
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);
109 static int comptypes_internal (tree, tree);
111 /* Return true if EXP is a null pointer constant, false otherwise. */
114 null_pointer_constant_p (tree expr)
116 /* This should really operate on c_expr structures, but they aren't
117 yet available everywhere required. */
118 tree type = TREE_TYPE (expr);
119 return (TREE_CODE (expr) == INTEGER_CST
120 && !TREE_CONSTANT_OVERFLOW (expr)
121 && integer_zerop (expr)
122 && (INTEGRAL_TYPE_P (type)
123 || (TREE_CODE (type) == POINTER_TYPE
124 && VOID_TYPE_P (TREE_TYPE (type))
125 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
127 \f/* This is a cache to hold if two types are compatible or not. */
129 struct tagged_tu_seen_cache {
130 const struct tagged_tu_seen_cache * next;
133 /* The return value of tagged_types_tu_compatible_p if we had seen
134 these two types already. */
138 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
139 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
141 /* Do `exp = require_complete_type (exp);' to make sure exp
142 does not have an incomplete type. (That includes void types.) */
145 require_complete_type (tree value)
147 tree type = TREE_TYPE (value);
149 if (value == error_mark_node || type == error_mark_node)
150 return error_mark_node;
152 /* First, detect a valid value with a complete type. */
153 if (COMPLETE_TYPE_P (type))
156 c_incomplete_type_error (value, type);
157 return error_mark_node;
160 /* Print an error message for invalid use of an incomplete type.
161 VALUE is the expression that was used (or 0 if that isn't known)
162 and TYPE is the type that was invalid. */
165 c_incomplete_type_error (tree value, tree type)
167 const char *type_code_string;
169 /* Avoid duplicate error message. */
170 if (TREE_CODE (type) == ERROR_MARK)
173 if (value != 0 && (TREE_CODE (value) == VAR_DECL
174 || TREE_CODE (value) == PARM_DECL))
175 error ("%qD has an incomplete type", value);
179 /* We must print an error message. Be clever about what it says. */
181 switch (TREE_CODE (type))
184 type_code_string = "struct";
188 type_code_string = "union";
192 type_code_string = "enum";
196 error ("invalid use of void expression");
200 if (TYPE_DOMAIN (type))
202 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
204 error ("invalid use of flexible array member");
207 type = TREE_TYPE (type);
210 error ("invalid use of array with unspecified bounds");
217 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
218 error ("invalid use of undefined type %<%s %E%>",
219 type_code_string, TYPE_NAME (type));
221 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
222 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
226 /* Given a type, apply default promotions wrt unnamed function
227 arguments and return the new type. */
230 c_type_promotes_to (tree type)
232 if (TYPE_MAIN_VARIANT (type) == float_type_node)
233 return double_type_node;
235 if (c_promoting_integer_type_p (type))
237 /* Preserve unsignedness if not really getting any wider. */
238 if (TYPE_UNSIGNED (type)
239 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
240 return unsigned_type_node;
241 return integer_type_node;
247 /* Return a variant of TYPE which has all the type qualifiers of LIKE
248 as well as those of TYPE. */
251 qualify_type (tree type, tree like)
253 return c_build_qualified_type (type,
254 TYPE_QUALS (type) | TYPE_QUALS (like));
257 /* Return true iff the given tree T is a variable length array. */
260 c_vla_type_p (tree t)
262 if (TREE_CODE (t) == ARRAY_TYPE
263 && C_TYPE_VARIABLE_SIZE (t))
268 /* Return the composite type of two compatible types.
270 We assume that comptypes has already been done and returned
271 nonzero; if that isn't so, this may crash. In particular, we
272 assume that qualifiers match. */
275 composite_type (tree t1, tree t2)
277 enum tree_code code1;
278 enum tree_code code2;
281 /* Save time if the two types are the same. */
283 if (t1 == t2) return t1;
285 /* If one type is nonsense, use the other. */
286 if (t1 == error_mark_node)
288 if (t2 == error_mark_node)
291 code1 = TREE_CODE (t1);
292 code2 = TREE_CODE (t2);
294 /* Merge the attributes. */
295 attributes = targetm.merge_type_attributes (t1, t2);
297 /* If one is an enumerated type and the other is the compatible
298 integer type, the composite type might be either of the two
299 (DR#013 question 3). For consistency, use the enumerated type as
300 the composite type. */
302 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
304 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
307 gcc_assert (code1 == code2);
312 /* For two pointers, do this recursively on the target type. */
314 tree pointed_to_1 = TREE_TYPE (t1);
315 tree pointed_to_2 = TREE_TYPE (t2);
316 tree target = composite_type (pointed_to_1, pointed_to_2);
317 t1 = build_pointer_type (target);
318 t1 = build_type_attribute_variant (t1, attributes);
319 return qualify_type (t1, t2);
324 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
327 tree d1 = TYPE_DOMAIN (t1);
328 tree d2 = TYPE_DOMAIN (t2);
329 bool d1_variable, d2_variable;
330 bool d1_zero, d2_zero;
332 /* We should not have any type quals on arrays at all. */
333 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
335 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
336 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
338 d1_variable = (!d1_zero
339 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
340 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
341 d2_variable = (!d2_zero
342 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
343 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
344 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
345 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
347 /* Save space: see if the result is identical to one of the args. */
348 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
349 && (d2_variable || d2_zero || !d1_variable))
350 return build_type_attribute_variant (t1, attributes);
351 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
352 && (d1_variable || d1_zero || !d2_variable))
353 return build_type_attribute_variant (t2, attributes);
355 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
356 return build_type_attribute_variant (t1, attributes);
357 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
358 return build_type_attribute_variant (t2, attributes);
360 /* Merge the element types, and have a size if either arg has
361 one. We may have qualifiers on the element types. To set
362 up TYPE_MAIN_VARIANT correctly, we need to form the
363 composite of the unqualified types and add the qualifiers
365 quals = TYPE_QUALS (strip_array_types (elt));
366 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
367 t1 = build_array_type (unqual_elt,
368 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
374 t1 = c_build_qualified_type (t1, quals);
375 return build_type_attribute_variant (t1, attributes);
379 /* Function types: prefer the one that specified arg types.
380 If both do, merge the arg types. Also merge the return types. */
382 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
383 tree p1 = TYPE_ARG_TYPES (t1);
384 tree p2 = TYPE_ARG_TYPES (t2);
389 /* Save space: see if the result is identical to one of the args. */
390 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
391 return build_type_attribute_variant (t1, attributes);
392 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
393 return build_type_attribute_variant (t2, attributes);
395 /* Simple way if one arg fails to specify argument types. */
396 if (TYPE_ARG_TYPES (t1) == 0)
398 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
399 t1 = build_type_attribute_variant (t1, attributes);
400 return qualify_type (t1, t2);
402 if (TYPE_ARG_TYPES (t2) == 0)
404 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
405 t1 = build_type_attribute_variant (t1, attributes);
406 return qualify_type (t1, t2);
409 /* If both args specify argument types, we must merge the two
410 lists, argument by argument. */
411 /* Tell global_bindings_p to return false so that variable_size
412 doesn't die on VLAs in parameter types. */
413 c_override_global_bindings_to_false = true;
415 len = list_length (p1);
418 for (i = 0; i < len; i++)
419 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
424 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
426 /* A null type means arg type is not specified.
427 Take whatever the other function type has. */
428 if (TREE_VALUE (p1) == 0)
430 TREE_VALUE (n) = TREE_VALUE (p2);
433 if (TREE_VALUE (p2) == 0)
435 TREE_VALUE (n) = TREE_VALUE (p1);
439 /* Given wait (union {union wait *u; int *i} *)
440 and wait (union wait *),
441 prefer union wait * as type of parm. */
442 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
443 && TREE_VALUE (p1) != TREE_VALUE (p2))
446 tree mv2 = TREE_VALUE (p2);
447 if (mv2 && mv2 != error_mark_node
448 && TREE_CODE (mv2) != ARRAY_TYPE)
449 mv2 = TYPE_MAIN_VARIANT (mv2);
450 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
451 memb; memb = TREE_CHAIN (memb))
453 tree mv3 = TREE_TYPE (memb);
454 if (mv3 && mv3 != error_mark_node
455 && TREE_CODE (mv3) != ARRAY_TYPE)
456 mv3 = TYPE_MAIN_VARIANT (mv3);
457 if (comptypes (mv3, mv2))
459 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
462 pedwarn ("function types not truly compatible in ISO C");
467 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
468 && TREE_VALUE (p2) != TREE_VALUE (p1))
471 tree mv1 = TREE_VALUE (p1);
472 if (mv1 && mv1 != error_mark_node
473 && TREE_CODE (mv1) != ARRAY_TYPE)
474 mv1 = TYPE_MAIN_VARIANT (mv1);
475 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
476 memb; memb = TREE_CHAIN (memb))
478 tree mv3 = TREE_TYPE (memb);
479 if (mv3 && mv3 != error_mark_node
480 && TREE_CODE (mv3) != ARRAY_TYPE)
481 mv3 = TYPE_MAIN_VARIANT (mv3);
482 if (comptypes (mv3, mv1))
484 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
487 pedwarn ("function types not truly compatible in ISO C");
492 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
496 c_override_global_bindings_to_false = false;
497 t1 = build_function_type (valtype, newargs);
498 t1 = qualify_type (t1, t2);
499 /* ... falls through ... */
503 return build_type_attribute_variant (t1, attributes);
508 /* Return the type of a conditional expression between pointers to
509 possibly differently qualified versions of compatible types.
511 We assume that comp_target_types has already been done and returned
512 nonzero; if that isn't so, this may crash. */
515 common_pointer_type (tree t1, tree t2)
518 tree pointed_to_1, mv1;
519 tree pointed_to_2, mv2;
522 /* Save time if the two types are the same. */
524 if (t1 == t2) return t1;
526 /* If one type is nonsense, use the other. */
527 if (t1 == error_mark_node)
529 if (t2 == error_mark_node)
532 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
533 && TREE_CODE (t2) == POINTER_TYPE);
535 /* Merge the attributes. */
536 attributes = targetm.merge_type_attributes (t1, t2);
538 /* Find the composite type of the target types, and combine the
539 qualifiers of the two types' targets. Do not lose qualifiers on
540 array element types by taking the TYPE_MAIN_VARIANT. */
541 mv1 = pointed_to_1 = TREE_TYPE (t1);
542 mv2 = pointed_to_2 = TREE_TYPE (t2);
543 if (TREE_CODE (mv1) != ARRAY_TYPE)
544 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
545 if (TREE_CODE (mv2) != ARRAY_TYPE)
546 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
547 target = composite_type (mv1, mv2);
548 t1 = build_pointer_type (c_build_qualified_type
550 TYPE_QUALS (pointed_to_1) |
551 TYPE_QUALS (pointed_to_2)));
552 return build_type_attribute_variant (t1, attributes);
555 /* Return the common type for two arithmetic types under the usual
556 arithmetic conversions. The default conversions have already been
557 applied, and enumerated types converted to their compatible integer
558 types. The resulting type is unqualified and has no attributes.
560 This is the type for the result of most arithmetic operations
561 if the operands have the given two types. */
564 c_common_type (tree t1, tree t2)
566 enum tree_code code1;
567 enum tree_code code2;
569 /* If one type is nonsense, use the other. */
570 if (t1 == error_mark_node)
572 if (t2 == error_mark_node)
575 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
576 t1 = TYPE_MAIN_VARIANT (t1);
578 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
579 t2 = TYPE_MAIN_VARIANT (t2);
581 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
582 t1 = build_type_attribute_variant (t1, NULL_TREE);
584 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
585 t2 = build_type_attribute_variant (t2, NULL_TREE);
587 /* Save time if the two types are the same. */
589 if (t1 == t2) return t1;
591 code1 = TREE_CODE (t1);
592 code2 = TREE_CODE (t2);
594 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
595 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
596 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
597 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
599 /* If one type is a vector type, return that type. (How the usual
600 arithmetic conversions apply to the vector types extension is not
601 precisely specified.) */
602 if (code1 == VECTOR_TYPE)
605 if (code2 == VECTOR_TYPE)
608 /* If one type is complex, form the common type of the non-complex
609 components, then make that complex. Use T1 or T2 if it is the
611 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
613 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
614 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
615 tree subtype = c_common_type (subtype1, subtype2);
617 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
619 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
622 return build_complex_type (subtype);
625 /* If only one is real, use it as the result. */
627 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
630 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
633 /* If both are real and either are decimal floating point types, use
634 the decimal floating point type with the greater precision. */
636 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
638 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
639 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
640 return dfloat128_type_node;
641 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
642 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
643 return dfloat64_type_node;
644 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
645 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
646 return dfloat32_type_node;
649 /* Both real or both integers; use the one with greater precision. */
651 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
653 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
656 /* Same precision. Prefer long longs to longs to ints when the
657 same precision, following the C99 rules on integer type rank
658 (which are equivalent to the C90 rules for C90 types). */
660 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
661 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
662 return long_long_unsigned_type_node;
664 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
665 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
667 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
668 return long_long_unsigned_type_node;
670 return long_long_integer_type_node;
673 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
674 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
675 return long_unsigned_type_node;
677 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
678 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
680 /* But preserve unsignedness from the other type,
681 since long cannot hold all the values of an unsigned int. */
682 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
683 return long_unsigned_type_node;
685 return long_integer_type_node;
688 /* Likewise, prefer long double to double even if same size. */
689 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
690 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
691 return long_double_type_node;
693 /* Otherwise prefer the unsigned one. */
695 if (TYPE_UNSIGNED (t1))
701 /* Wrapper around c_common_type that is used by c-common.c and other
702 front end optimizations that remove promotions. ENUMERAL_TYPEs
703 are allowed here and are converted to their compatible integer types.
704 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
705 preferably a non-Boolean type as the common type. */
707 common_type (tree t1, tree t2)
709 if (TREE_CODE (t1) == ENUMERAL_TYPE)
710 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
711 if (TREE_CODE (t2) == ENUMERAL_TYPE)
712 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
714 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
715 if (TREE_CODE (t1) == BOOLEAN_TYPE
716 && TREE_CODE (t2) == BOOLEAN_TYPE)
717 return boolean_type_node;
719 /* If either type is BOOLEAN_TYPE, then return the other. */
720 if (TREE_CODE (t1) == BOOLEAN_TYPE)
722 if (TREE_CODE (t2) == BOOLEAN_TYPE)
725 return c_common_type (t1, t2);
728 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
729 or various other operations. Return 2 if they are compatible
730 but a warning may be needed if you use them together. */
733 comptypes (tree type1, tree type2)
735 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
738 val = comptypes_internal (type1, type2);
739 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
744 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
745 or various other operations. Return 2 if they are compatible
746 but a warning may be needed if you use them together. This
747 differs from comptypes, in that we don't free the seen types. */
750 comptypes_internal (tree type1, tree type2)
756 /* Suppress errors caused by previously reported errors. */
758 if (t1 == t2 || !t1 || !t2
759 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
762 /* If either type is the internal version of sizetype, return the
764 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
765 && TYPE_ORIG_SIZE_TYPE (t1))
766 t1 = TYPE_ORIG_SIZE_TYPE (t1);
768 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
769 && TYPE_ORIG_SIZE_TYPE (t2))
770 t2 = TYPE_ORIG_SIZE_TYPE (t2);
773 /* Enumerated types are compatible with integer types, but this is
774 not transitive: two enumerated types in the same translation unit
775 are compatible with each other only if they are the same type. */
777 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
778 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
779 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
780 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
785 /* Different classes of types can't be compatible. */
787 if (TREE_CODE (t1) != TREE_CODE (t2))
790 /* Qualifiers must match. C99 6.7.3p9 */
792 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
795 /* Allow for two different type nodes which have essentially the same
796 definition. Note that we already checked for equality of the type
797 qualifiers (just above). */
799 if (TREE_CODE (t1) != ARRAY_TYPE
800 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
803 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
804 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
807 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
810 switch (TREE_CODE (t1))
813 /* Do not remove mode or aliasing information. */
814 if (TYPE_MODE (t1) != TYPE_MODE (t2)
815 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
817 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
818 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
822 val = function_types_compatible_p (t1, t2);
827 tree d1 = TYPE_DOMAIN (t1);
828 tree d2 = TYPE_DOMAIN (t2);
829 bool d1_variable, d2_variable;
830 bool d1_zero, d2_zero;
833 /* Target types must match incl. qualifiers. */
834 if (TREE_TYPE (t1) != TREE_TYPE (t2)
835 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
838 /* Sizes must match unless one is missing or variable. */
839 if (d1 == 0 || d2 == 0 || d1 == d2)
842 d1_zero = !TYPE_MAX_VALUE (d1);
843 d2_zero = !TYPE_MAX_VALUE (d2);
845 d1_variable = (!d1_zero
846 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
847 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
848 d2_variable = (!d2_zero
849 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
850 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
851 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
852 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
854 if (d1_variable || d2_variable)
856 if (d1_zero && d2_zero)
858 if (d1_zero || d2_zero
859 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
860 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
869 if (val != 1 && !same_translation_unit_p (t1, t2))
872 return tagged_types_tu_compatible_p (t1, t2);
873 val = tagged_types_tu_compatible_p (t1, t2);
878 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
879 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
885 return attrval == 2 && val == 1 ? 2 : val;
888 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
889 ignoring their qualifiers. */
892 comp_target_types (tree ttl, tree ttr)
897 /* Do not lose qualifiers on element types of array types that are
898 pointer targets by taking their TYPE_MAIN_VARIANT. */
899 mvl = TREE_TYPE (ttl);
900 mvr = TREE_TYPE (ttr);
901 if (TREE_CODE (mvl) != ARRAY_TYPE)
902 mvl = TYPE_MAIN_VARIANT (mvl);
903 if (TREE_CODE (mvr) != ARRAY_TYPE)
904 mvr = TYPE_MAIN_VARIANT (mvr);
905 val = comptypes (mvl, mvr);
907 if (val == 2 && pedantic)
908 pedwarn ("types are not quite compatible");
912 /* Subroutines of `comptypes'. */
914 /* Determine whether two trees derive from the same translation unit.
915 If the CONTEXT chain ends in a null, that tree's context is still
916 being parsed, so if two trees have context chains ending in null,
917 they're in the same translation unit. */
919 same_translation_unit_p (tree t1, tree t2)
921 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
922 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
924 case tcc_declaration:
925 t1 = DECL_CONTEXT (t1); break;
927 t1 = TYPE_CONTEXT (t1); break;
928 case tcc_exceptional:
929 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
930 default: gcc_unreachable ();
933 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
934 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
936 case tcc_declaration:
937 t2 = DECL_CONTEXT (t2); break;
939 t2 = TYPE_CONTEXT (t2); break;
940 case tcc_exceptional:
941 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
942 default: gcc_unreachable ();
948 /* Allocate the seen two types, assuming that they are compatible. */
950 static struct tagged_tu_seen_cache *
951 alloc_tagged_tu_seen_cache (tree t1, tree t2)
953 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
954 tu->next = tagged_tu_seen_base;
958 tagged_tu_seen_base = tu;
960 /* The C standard says that two structures in different translation
961 units are compatible with each other only if the types of their
962 fields are compatible (among other things). We assume that they
963 are compatible until proven otherwise when building the cache.
964 An example where this can occur is:
969 If we are comparing this against a similar struct in another TU,
970 and did not assume they were compatible, we end up with an infinite
976 /* Free the seen types until we get to TU_TIL. */
979 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
981 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
984 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
988 tagged_tu_seen_base = tu_til;
991 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
992 compatible. If the two types are not the same (which has been
993 checked earlier), this can only happen when multiple translation
994 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
998 tagged_types_tu_compatible_p (tree t1, tree t2)
1001 bool needs_warning = false;
1003 /* We have to verify that the tags of the types are the same. This
1004 is harder than it looks because this may be a typedef, so we have
1005 to go look at the original type. It may even be a typedef of a
1007 In the case of compiler-created builtin structs the TYPE_DECL
1008 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1009 while (TYPE_NAME (t1)
1010 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1011 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1012 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1014 while (TYPE_NAME (t2)
1015 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1016 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1017 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1019 /* C90 didn't have the requirement that the two tags be the same. */
1020 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1023 /* C90 didn't say what happened if one or both of the types were
1024 incomplete; we choose to follow C99 rules here, which is that they
1026 if (TYPE_SIZE (t1) == NULL
1027 || TYPE_SIZE (t2) == NULL)
1031 const struct tagged_tu_seen_cache * tts_i;
1032 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1033 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1037 switch (TREE_CODE (t1))
1041 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1042 /* Speed up the case where the type values are in the same order. */
1043 tree tv1 = TYPE_VALUES (t1);
1044 tree tv2 = TYPE_VALUES (t2);
1051 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1053 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1055 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1062 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1066 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1072 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1078 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1080 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1082 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1093 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1094 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1100 /* Speed up the common case where the fields are in the same order. */
1101 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1102 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1107 if (DECL_NAME (s1) == NULL
1108 || DECL_NAME (s1) != DECL_NAME (s2))
1110 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1117 needs_warning = true;
1119 if (TREE_CODE (s1) == FIELD_DECL
1120 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1121 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1129 tu->val = needs_warning ? 2 : 1;
1133 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1137 if (DECL_NAME (s1) != NULL)
1138 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1139 if (DECL_NAME (s1) == DECL_NAME (s2))
1142 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1149 needs_warning = true;
1151 if (TREE_CODE (s1) == FIELD_DECL
1152 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1153 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1165 tu->val = needs_warning ? 2 : 10;
1171 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1173 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1175 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1178 if (TREE_CODE (s1) != TREE_CODE (s2)
1179 || DECL_NAME (s1) != DECL_NAME (s2))
1181 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1185 needs_warning = true;
1187 if (TREE_CODE (s1) == FIELD_DECL
1188 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1189 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1195 tu->val = needs_warning ? 2 : 1;
1204 /* Return 1 if two function types F1 and F2 are compatible.
1205 If either type specifies no argument types,
1206 the other must specify a fixed number of self-promoting arg types.
1207 Otherwise, if one type specifies only the number of arguments,
1208 the other must specify that number of self-promoting arg types.
1209 Otherwise, the argument types must match. */
1212 function_types_compatible_p (tree f1, tree f2)
1215 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1220 ret1 = TREE_TYPE (f1);
1221 ret2 = TREE_TYPE (f2);
1223 /* 'volatile' qualifiers on a function's return type used to mean
1224 the function is noreturn. */
1225 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1226 pedwarn ("function return types not compatible due to %<volatile%>");
1227 if (TYPE_VOLATILE (ret1))
1228 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1229 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1230 if (TYPE_VOLATILE (ret2))
1231 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1232 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1233 val = comptypes_internal (ret1, ret2);
1237 args1 = TYPE_ARG_TYPES (f1);
1238 args2 = TYPE_ARG_TYPES (f2);
1240 /* An unspecified parmlist matches any specified parmlist
1241 whose argument types don't need default promotions. */
1245 if (!self_promoting_args_p (args2))
1247 /* If one of these types comes from a non-prototype fn definition,
1248 compare that with the other type's arglist.
1249 If they don't match, ask for a warning (but no error). */
1250 if (TYPE_ACTUAL_ARG_TYPES (f1)
1251 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1257 if (!self_promoting_args_p (args1))
1259 if (TYPE_ACTUAL_ARG_TYPES (f2)
1260 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1265 /* Both types have argument lists: compare them and propagate results. */
1266 val1 = type_lists_compatible_p (args1, args2);
1267 return val1 != 1 ? val1 : val;
1270 /* Check two lists of types for compatibility,
1271 returning 0 for incompatible, 1 for compatible,
1272 or 2 for compatible with warning. */
1275 type_lists_compatible_p (tree args1, tree args2)
1277 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1283 tree a1, mv1, a2, mv2;
1284 if (args1 == 0 && args2 == 0)
1286 /* If one list is shorter than the other,
1287 they fail to match. */
1288 if (args1 == 0 || args2 == 0)
1290 mv1 = a1 = TREE_VALUE (args1);
1291 mv2 = a2 = TREE_VALUE (args2);
1292 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1293 mv1 = TYPE_MAIN_VARIANT (mv1);
1294 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1295 mv2 = TYPE_MAIN_VARIANT (mv2);
1296 /* A null pointer instead of a type
1297 means there is supposed to be an argument
1298 but nothing is specified about what type it has.
1299 So match anything that self-promotes. */
1302 if (c_type_promotes_to (a2) != a2)
1307 if (c_type_promotes_to (a1) != a1)
1310 /* If one of the lists has an error marker, ignore this arg. */
1311 else if (TREE_CODE (a1) == ERROR_MARK
1312 || TREE_CODE (a2) == ERROR_MARK)
1314 else if (!(newval = comptypes_internal (mv1, mv2)))
1316 /* Allow wait (union {union wait *u; int *i} *)
1317 and wait (union wait *) to be compatible. */
1318 if (TREE_CODE (a1) == UNION_TYPE
1319 && (TYPE_NAME (a1) == 0
1320 || TYPE_TRANSPARENT_UNION (a1))
1321 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1322 && tree_int_cst_equal (TYPE_SIZE (a1),
1326 for (memb = TYPE_FIELDS (a1);
1327 memb; memb = TREE_CHAIN (memb))
1329 tree mv3 = TREE_TYPE (memb);
1330 if (mv3 && mv3 != error_mark_node
1331 && TREE_CODE (mv3) != ARRAY_TYPE)
1332 mv3 = TYPE_MAIN_VARIANT (mv3);
1333 if (comptypes_internal (mv3, mv2))
1339 else if (TREE_CODE (a2) == UNION_TYPE
1340 && (TYPE_NAME (a2) == 0
1341 || TYPE_TRANSPARENT_UNION (a2))
1342 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1343 && tree_int_cst_equal (TYPE_SIZE (a2),
1347 for (memb = TYPE_FIELDS (a2);
1348 memb; memb = TREE_CHAIN (memb))
1350 tree mv3 = TREE_TYPE (memb);
1351 if (mv3 && mv3 != error_mark_node
1352 && TREE_CODE (mv3) != ARRAY_TYPE)
1353 mv3 = TYPE_MAIN_VARIANT (mv3);
1354 if (comptypes_internal (mv3, mv1))
1364 /* comptypes said ok, but record if it said to warn. */
1368 args1 = TREE_CHAIN (args1);
1369 args2 = TREE_CHAIN (args2);
1373 /* Compute the size to increment a pointer by. */
1376 c_size_in_bytes (tree type)
1378 enum tree_code code = TREE_CODE (type);
1380 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1381 return size_one_node;
1383 if (!COMPLETE_OR_VOID_TYPE_P (type))
1385 error ("arithmetic on pointer to an incomplete type");
1386 return size_one_node;
1389 /* Convert in case a char is more than one unit. */
1390 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1391 size_int (TYPE_PRECISION (char_type_node)
1395 /* Return either DECL or its known constant value (if it has one). */
1398 decl_constant_value (tree decl)
1400 if (/* Don't change a variable array bound or initial value to a constant
1401 in a place where a variable is invalid. Note that DECL_INITIAL
1402 isn't valid for a PARM_DECL. */
1403 current_function_decl != 0
1404 && TREE_CODE (decl) != PARM_DECL
1405 && !TREE_THIS_VOLATILE (decl)
1406 && TREE_READONLY (decl)
1407 && DECL_INITIAL (decl) != 0
1408 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1409 /* This is invalid if initial value is not constant.
1410 If it has either a function call, a memory reference,
1411 or a variable, then re-evaluating it could give different results. */
1412 && TREE_CONSTANT (DECL_INITIAL (decl))
1413 /* Check for cases where this is sub-optimal, even though valid. */
1414 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1415 return DECL_INITIAL (decl);
1419 /* Return either DECL or its known constant value (if it has one), but
1420 return DECL if pedantic or DECL has mode BLKmode. This is for
1421 bug-compatibility with the old behavior of decl_constant_value
1422 (before GCC 3.0); every use of this function is a bug and it should
1423 be removed before GCC 3.1. It is not appropriate to use pedantic
1424 in a way that affects optimization, and BLKmode is probably not the
1425 right test for avoiding misoptimizations either. */
1428 decl_constant_value_for_broken_optimization (tree decl)
1432 if (pedantic || DECL_MODE (decl) == BLKmode)
1435 ret = decl_constant_value (decl);
1436 /* Avoid unwanted tree sharing between the initializer and current
1437 function's body where the tree can be modified e.g. by the
1439 if (ret != decl && TREE_STATIC (decl))
1440 ret = unshare_expr (ret);
1444 /* Convert the array expression EXP to a pointer. */
1446 array_to_pointer_conversion (tree exp)
1448 tree orig_exp = exp;
1449 tree type = TREE_TYPE (exp);
1451 tree restype = TREE_TYPE (type);
1454 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1456 STRIP_TYPE_NOPS (exp);
1458 if (TREE_NO_WARNING (orig_exp))
1459 TREE_NO_WARNING (exp) = 1;
1461 ptrtype = build_pointer_type (restype);
1463 if (TREE_CODE (exp) == INDIRECT_REF)
1464 return convert (ptrtype, TREE_OPERAND (exp, 0));
1466 if (TREE_CODE (exp) == VAR_DECL)
1468 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1469 ADDR_EXPR because it's the best way of representing what
1470 happens in C when we take the address of an array and place
1471 it in a pointer to the element type. */
1472 adr = build1 (ADDR_EXPR, ptrtype, exp);
1473 if (!c_mark_addressable (exp))
1474 return error_mark_node;
1475 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1479 /* This way is better for a COMPONENT_REF since it can
1480 simplify the offset for a component. */
1481 adr = build_unary_op (ADDR_EXPR, exp, 1);
1482 return convert (ptrtype, adr);
1485 /* Convert the function expression EXP to a pointer. */
1487 function_to_pointer_conversion (tree exp)
1489 tree orig_exp = exp;
1491 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1493 STRIP_TYPE_NOPS (exp);
1495 if (TREE_NO_WARNING (orig_exp))
1496 TREE_NO_WARNING (exp) = 1;
1498 return build_unary_op (ADDR_EXPR, exp, 0);
1501 /* Perform the default conversion of arrays and functions to pointers.
1502 Return the result of converting EXP. For any other expression, just
1503 return EXP after removing NOPs. */
1506 default_function_array_conversion (struct c_expr exp)
1508 tree orig_exp = exp.value;
1509 tree type = TREE_TYPE (exp.value);
1510 enum tree_code code = TREE_CODE (type);
1516 bool not_lvalue = false;
1517 bool lvalue_array_p;
1519 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1520 || TREE_CODE (exp.value) == NOP_EXPR
1521 || TREE_CODE (exp.value) == CONVERT_EXPR)
1522 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1524 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1526 exp.value = TREE_OPERAND (exp.value, 0);
1529 if (TREE_NO_WARNING (orig_exp))
1530 TREE_NO_WARNING (exp.value) = 1;
1532 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1533 if (!flag_isoc99 && !lvalue_array_p)
1535 /* Before C99, non-lvalue arrays do not decay to pointers.
1536 Normally, using such an array would be invalid; but it can
1537 be used correctly inside sizeof or as a statement expression.
1538 Thus, do not give an error here; an error will result later. */
1542 exp.value = array_to_pointer_conversion (exp.value);
1546 exp.value = function_to_pointer_conversion (exp.value);
1549 STRIP_TYPE_NOPS (exp.value);
1550 if (TREE_NO_WARNING (orig_exp))
1551 TREE_NO_WARNING (exp.value) = 1;
1559 /* EXP is an expression of integer type. Apply the integer promotions
1560 to it and return the promoted value. */
1563 perform_integral_promotions (tree exp)
1565 tree type = TREE_TYPE (exp);
1566 enum tree_code code = TREE_CODE (type);
1568 gcc_assert (INTEGRAL_TYPE_P (type));
1570 /* Normally convert enums to int,
1571 but convert wide enums to something wider. */
1572 if (code == ENUMERAL_TYPE)
1574 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1575 TYPE_PRECISION (integer_type_node)),
1576 ((TYPE_PRECISION (type)
1577 >= TYPE_PRECISION (integer_type_node))
1578 && TYPE_UNSIGNED (type)));
1580 return convert (type, exp);
1583 /* ??? This should no longer be needed now bit-fields have their
1585 if (TREE_CODE (exp) == COMPONENT_REF
1586 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1587 /* If it's thinner than an int, promote it like a
1588 c_promoting_integer_type_p, otherwise leave it alone. */
1589 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1590 TYPE_PRECISION (integer_type_node)))
1591 return convert (integer_type_node, exp);
1593 if (c_promoting_integer_type_p (type))
1595 /* Preserve unsignedness if not really getting any wider. */
1596 if (TYPE_UNSIGNED (type)
1597 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1598 return convert (unsigned_type_node, exp);
1600 return convert (integer_type_node, exp);
1607 /* Perform default promotions for C data used in expressions.
1608 Enumeral types or short or char are converted to int.
1609 In addition, manifest constants symbols are replaced by their values. */
1612 default_conversion (tree exp)
1615 tree type = TREE_TYPE (exp);
1616 enum tree_code code = TREE_CODE (type);
1618 /* Functions and arrays have been converted during parsing. */
1619 gcc_assert (code != FUNCTION_TYPE);
1620 if (code == ARRAY_TYPE)
1623 /* Constants can be used directly unless they're not loadable. */
1624 if (TREE_CODE (exp) == CONST_DECL)
1625 exp = DECL_INITIAL (exp);
1627 /* Replace a nonvolatile const static variable with its value unless
1628 it is an array, in which case we must be sure that taking the
1629 address of the array produces consistent results. */
1630 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1632 exp = decl_constant_value_for_broken_optimization (exp);
1633 type = TREE_TYPE (exp);
1636 /* Strip no-op conversions. */
1638 STRIP_TYPE_NOPS (exp);
1640 if (TREE_NO_WARNING (orig_exp))
1641 TREE_NO_WARNING (exp) = 1;
1643 if (INTEGRAL_TYPE_P (type))
1644 return perform_integral_promotions (exp);
1646 if (code == VOID_TYPE)
1648 error ("void value not ignored as it ought to be");
1649 return error_mark_node;
1654 /* Look up COMPONENT in a structure or union DECL.
1656 If the component name is not found, returns NULL_TREE. Otherwise,
1657 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1658 stepping down the chain to the component, which is in the last
1659 TREE_VALUE of the list. Normally the list is of length one, but if
1660 the component is embedded within (nested) anonymous structures or
1661 unions, the list steps down the chain to the component. */
1664 lookup_field (tree decl, tree component)
1666 tree type = TREE_TYPE (decl);
1669 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1670 to the field elements. Use a binary search on this array to quickly
1671 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1672 will always be set for structures which have many elements. */
1674 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1677 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1679 field = TYPE_FIELDS (type);
1681 top = TYPE_LANG_SPECIFIC (type)->s->len;
1682 while (top - bot > 1)
1684 half = (top - bot + 1) >> 1;
1685 field = field_array[bot+half];
1687 if (DECL_NAME (field) == NULL_TREE)
1689 /* Step through all anon unions in linear fashion. */
1690 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1692 field = field_array[bot++];
1693 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1694 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1696 tree anon = lookup_field (field, component);
1699 return tree_cons (NULL_TREE, field, anon);
1703 /* Entire record is only anon unions. */
1707 /* Restart the binary search, with new lower bound. */
1711 if (DECL_NAME (field) == component)
1713 if (DECL_NAME (field) < component)
1719 if (DECL_NAME (field_array[bot]) == component)
1720 field = field_array[bot];
1721 else if (DECL_NAME (field) != component)
1726 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1728 if (DECL_NAME (field) == NULL_TREE
1729 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1730 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1732 tree anon = lookup_field (field, component);
1735 return tree_cons (NULL_TREE, field, anon);
1738 if (DECL_NAME (field) == component)
1742 if (field == NULL_TREE)
1746 return tree_cons (NULL_TREE, field, NULL_TREE);
1749 /* Make an expression to refer to the COMPONENT field of
1750 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1753 build_component_ref (tree datum, tree component)
1755 tree type = TREE_TYPE (datum);
1756 enum tree_code code = TREE_CODE (type);
1760 if (!objc_is_public (datum, component))
1761 return error_mark_node;
1763 /* See if there is a field or component with name COMPONENT. */
1765 if (code == RECORD_TYPE || code == UNION_TYPE)
1767 if (!COMPLETE_TYPE_P (type))
1769 c_incomplete_type_error (NULL_TREE, type);
1770 return error_mark_node;
1773 field = lookup_field (datum, component);
1777 error ("%qT has no member named %qE", type, component);
1778 return error_mark_node;
1781 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1782 This might be better solved in future the way the C++ front
1783 end does it - by giving the anonymous entities each a
1784 separate name and type, and then have build_component_ref
1785 recursively call itself. We can't do that here. */
1788 tree subdatum = TREE_VALUE (field);
1790 if (TREE_TYPE (subdatum) == error_mark_node)
1791 return error_mark_node;
1793 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1795 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1796 TREE_READONLY (ref) = 1;
1797 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1798 TREE_THIS_VOLATILE (ref) = 1;
1800 if (TREE_DEPRECATED (subdatum))
1801 warn_deprecated_use (subdatum);
1805 field = TREE_CHAIN (field);
1811 else if (code != ERROR_MARK)
1812 error ("request for member %qE in something not a structure or union",
1815 return error_mark_node;
1818 /* Given an expression PTR for a pointer, return an expression
1819 for the value pointed to.
1820 ERRORSTRING is the name of the operator to appear in error messages. */
1823 build_indirect_ref (tree ptr, const char *errorstring)
1825 tree pointer = default_conversion (ptr);
1826 tree type = TREE_TYPE (pointer);
1828 if (TREE_CODE (type) == POINTER_TYPE)
1830 if (TREE_CODE (pointer) == ADDR_EXPR
1831 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1832 == TREE_TYPE (type)))
1833 return TREE_OPERAND (pointer, 0);
1836 tree t = TREE_TYPE (type);
1839 ref = build1 (INDIRECT_REF, t, pointer);
1841 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1843 error ("dereferencing pointer to incomplete type");
1844 return error_mark_node;
1846 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1847 warning (0, "dereferencing %<void *%> pointer");
1849 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1850 so that we get the proper error message if the result is used
1851 to assign to. Also, &* is supposed to be a no-op.
1852 And ANSI C seems to specify that the type of the result
1853 should be the const type. */
1854 /* A de-reference of a pointer to const is not a const. It is valid
1855 to change it via some other pointer. */
1856 TREE_READONLY (ref) = TYPE_READONLY (t);
1857 TREE_SIDE_EFFECTS (ref)
1858 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1859 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1863 else if (TREE_CODE (pointer) != ERROR_MARK)
1864 error ("invalid type argument of %qs", errorstring);
1865 return error_mark_node;
1868 /* This handles expressions of the form "a[i]", which denotes
1871 This is logically equivalent in C to *(a+i), but we may do it differently.
1872 If A is a variable or a member, we generate a primitive ARRAY_REF.
1873 This avoids forcing the array out of registers, and can work on
1874 arrays that are not lvalues (for example, members of structures returned
1878 build_array_ref (tree array, tree index)
1880 bool swapped = false;
1881 if (TREE_TYPE (array) == error_mark_node
1882 || TREE_TYPE (index) == error_mark_node)
1883 return error_mark_node;
1885 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1886 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1889 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1890 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1892 error ("subscripted value is neither array nor pointer");
1893 return error_mark_node;
1901 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1903 error ("array subscript is not an integer");
1904 return error_mark_node;
1907 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1909 error ("subscripted value is pointer to function");
1910 return error_mark_node;
1913 /* ??? Existing practice has been to warn only when the char
1914 index is syntactically the index, not for char[array]. */
1916 warn_array_subscript_with_type_char (index);
1918 /* Apply default promotions *after* noticing character types. */
1919 index = default_conversion (index);
1921 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1923 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1927 /* An array that is indexed by a non-constant
1928 cannot be stored in a register; we must be able to do
1929 address arithmetic on its address.
1930 Likewise an array of elements of variable size. */
1931 if (TREE_CODE (index) != INTEGER_CST
1932 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1933 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1935 if (!c_mark_addressable (array))
1936 return error_mark_node;
1938 /* An array that is indexed by a constant value which is not within
1939 the array bounds cannot be stored in a register either; because we
1940 would get a crash in store_bit_field/extract_bit_field when trying
1941 to access a non-existent part of the register. */
1942 if (TREE_CODE (index) == INTEGER_CST
1943 && TYPE_DOMAIN (TREE_TYPE (array))
1944 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1946 if (!c_mark_addressable (array))
1947 return error_mark_node;
1953 while (TREE_CODE (foo) == COMPONENT_REF)
1954 foo = TREE_OPERAND (foo, 0);
1955 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1956 pedwarn ("ISO C forbids subscripting %<register%> array");
1957 else if (!flag_isoc99 && !lvalue_p (foo))
1958 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1961 type = TREE_TYPE (TREE_TYPE (array));
1962 if (TREE_CODE (type) != ARRAY_TYPE)
1963 type = TYPE_MAIN_VARIANT (type);
1964 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1965 /* Array ref is const/volatile if the array elements are
1966 or if the array is. */
1967 TREE_READONLY (rval)
1968 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1969 | TREE_READONLY (array));
1970 TREE_SIDE_EFFECTS (rval)
1971 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1972 | TREE_SIDE_EFFECTS (array));
1973 TREE_THIS_VOLATILE (rval)
1974 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1975 /* This was added by rms on 16 Nov 91.
1976 It fixes vol struct foo *a; a->elts[1]
1977 in an inline function.
1978 Hope it doesn't break something else. */
1979 | TREE_THIS_VOLATILE (array));
1980 return require_complete_type (fold (rval));
1984 tree ar = default_conversion (array);
1986 if (ar == error_mark_node)
1989 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1990 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1992 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1997 /* Build an external reference to identifier ID. FUN indicates
1998 whether this will be used for a function call. LOC is the source
1999 location of the identifier. */
2001 build_external_ref (tree id, int fun, location_t loc)
2004 tree decl = lookup_name (id);
2006 /* In Objective-C, an instance variable (ivar) may be preferred to
2007 whatever lookup_name() found. */
2008 decl = objc_lookup_ivar (decl, id);
2010 if (decl && decl != error_mark_node)
2013 /* Implicit function declaration. */
2014 ref = implicitly_declare (id);
2015 else if (decl == error_mark_node)
2016 /* Don't complain about something that's already been
2017 complained about. */
2018 return error_mark_node;
2021 undeclared_variable (id, loc);
2022 return error_mark_node;
2025 if (TREE_TYPE (ref) == error_mark_node)
2026 return error_mark_node;
2028 if (TREE_DEPRECATED (ref))
2029 warn_deprecated_use (ref);
2031 if (!skip_evaluation)
2032 assemble_external (ref);
2033 TREE_USED (ref) = 1;
2035 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2037 if (!in_sizeof && !in_typeof)
2038 C_DECL_USED (ref) = 1;
2039 else if (DECL_INITIAL (ref) == 0
2040 && DECL_EXTERNAL (ref)
2041 && !TREE_PUBLIC (ref))
2042 record_maybe_used_decl (ref);
2045 if (TREE_CODE (ref) == CONST_DECL)
2047 ref = DECL_INITIAL (ref);
2048 TREE_CONSTANT (ref) = 1;
2049 TREE_INVARIANT (ref) = 1;
2051 else if (current_function_decl != 0
2052 && !DECL_FILE_SCOPE_P (current_function_decl)
2053 && (TREE_CODE (ref) == VAR_DECL
2054 || TREE_CODE (ref) == PARM_DECL
2055 || TREE_CODE (ref) == FUNCTION_DECL))
2057 tree context = decl_function_context (ref);
2059 if (context != 0 && context != current_function_decl)
2060 DECL_NONLOCAL (ref) = 1;
2066 /* Record details of decls possibly used inside sizeof or typeof. */
2067 struct maybe_used_decl
2071 /* The level seen at (in_sizeof + in_typeof). */
2073 /* The next one at this level or above, or NULL. */
2074 struct maybe_used_decl *next;
2077 static struct maybe_used_decl *maybe_used_decls;
2079 /* Record that DECL, an undefined static function reference seen
2080 inside sizeof or typeof, might be used if the operand of sizeof is
2081 a VLA type or the operand of typeof is a variably modified
2085 record_maybe_used_decl (tree decl)
2087 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2089 t->level = in_sizeof + in_typeof;
2090 t->next = maybe_used_decls;
2091 maybe_used_decls = t;
2094 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2095 USED is false, just discard them. If it is true, mark them used
2096 (if no longer inside sizeof or typeof) or move them to the next
2097 level up (if still inside sizeof or typeof). */
2100 pop_maybe_used (bool used)
2102 struct maybe_used_decl *p = maybe_used_decls;
2103 int cur_level = in_sizeof + in_typeof;
2104 while (p && p->level > cur_level)
2109 C_DECL_USED (p->decl) = 1;
2111 p->level = cur_level;
2115 if (!used || cur_level == 0)
2116 maybe_used_decls = p;
2119 /* Return the result of sizeof applied to EXPR. */
2122 c_expr_sizeof_expr (struct c_expr expr)
2125 if (expr.value == error_mark_node)
2127 ret.value = error_mark_node;
2128 ret.original_code = ERROR_MARK;
2129 pop_maybe_used (false);
2133 ret.value = c_sizeof (TREE_TYPE (expr.value));
2134 ret.original_code = ERROR_MARK;
2135 if (c_vla_type_p (TREE_TYPE (expr.value)))
2137 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2138 ret.value = build2 (COMPOUND_EXPR, TREE_TYPE (ret.value), expr.value, ret.value);
2140 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2145 /* Return the result of sizeof applied to T, a structure for the type
2146 name passed to sizeof (rather than the type itself). */
2149 c_expr_sizeof_type (struct c_type_name *t)
2153 type = groktypename (t);
2154 ret.value = c_sizeof (type);
2155 ret.original_code = ERROR_MARK;
2156 pop_maybe_used (type != error_mark_node
2157 ? C_TYPE_VARIABLE_SIZE (type) : false);
2161 /* Build a function call to function FUNCTION with parameters PARAMS.
2162 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2163 TREE_VALUE of each node is a parameter-expression.
2164 FUNCTION's data type may be a function type or a pointer-to-function. */
2167 build_function_call (tree function, tree params)
2169 tree fntype, fundecl = 0;
2170 tree coerced_params;
2171 tree name = NULL_TREE, result;
2174 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2175 STRIP_TYPE_NOPS (function);
2177 /* Convert anything with function type to a pointer-to-function. */
2178 if (TREE_CODE (function) == FUNCTION_DECL)
2180 /* Implement type-directed function overloading for builtins.
2181 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2182 handle all the type checking. The result is a complete expression
2183 that implements this function call. */
2184 tem = resolve_overloaded_builtin (function, params);
2188 name = DECL_NAME (function);
2191 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2192 function = function_to_pointer_conversion (function);
2194 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2195 expressions, like those used for ObjC messenger dispatches. */
2196 function = objc_rewrite_function_call (function, params);
2198 fntype = TREE_TYPE (function);
2200 if (TREE_CODE (fntype) == ERROR_MARK)
2201 return error_mark_node;
2203 if (!(TREE_CODE (fntype) == POINTER_TYPE
2204 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2206 error ("called object %qE is not a function", function);
2207 return error_mark_node;
2210 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2211 current_function_returns_abnormally = 1;
2213 /* fntype now gets the type of function pointed to. */
2214 fntype = TREE_TYPE (fntype);
2216 /* Check that the function is called through a compatible prototype.
2217 If it is not, replace the call by a trap, wrapped up in a compound
2218 expression if necessary. This has the nice side-effect to prevent
2219 the tree-inliner from generating invalid assignment trees which may
2220 blow up in the RTL expander later. */
2221 if ((TREE_CODE (function) == NOP_EXPR
2222 || TREE_CODE (function) == CONVERT_EXPR)
2223 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2224 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2225 && !comptypes (fntype, TREE_TYPE (tem)))
2227 tree return_type = TREE_TYPE (fntype);
2228 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2231 /* This situation leads to run-time undefined behavior. We can't,
2232 therefore, simply error unless we can prove that all possible
2233 executions of the program must execute the code. */
2234 warning (0, "function called through a non-compatible type");
2236 /* We can, however, treat "undefined" any way we please.
2237 Call abort to encourage the user to fix the program. */
2238 inform ("if this code is reached, the program will abort");
2240 if (VOID_TYPE_P (return_type))
2246 if (AGGREGATE_TYPE_P (return_type))
2247 rhs = build_compound_literal (return_type,
2248 build_constructor (return_type, 0));
2250 rhs = fold_convert (return_type, integer_zero_node);
2252 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2256 /* Convert the parameters to the types declared in the
2257 function prototype, or apply default promotions. */
2260 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2262 if (coerced_params == error_mark_node)
2263 return error_mark_node;
2265 /* Check that the arguments to the function are valid. */
2267 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2268 TYPE_ARG_TYPES (fntype));
2270 if (require_constant_value)
2272 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2273 function, coerced_params, NULL_TREE);
2275 if (TREE_CONSTANT (result)
2276 && (name == NULL_TREE
2277 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2278 pedwarn_init ("initializer element is not constant");
2281 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2282 function, coerced_params, NULL_TREE);
2284 if (VOID_TYPE_P (TREE_TYPE (result)))
2286 return require_complete_type (result);
2289 /* Convert the argument expressions in the list VALUES
2290 to the types in the list TYPELIST. The result is a list of converted
2291 argument expressions, unless there are too few arguments in which
2292 case it is error_mark_node.
2294 If TYPELIST is exhausted, or when an element has NULL as its type,
2295 perform the default conversions.
2297 PARMLIST is the chain of parm decls for the function being called.
2298 It may be 0, if that info is not available.
2299 It is used only for generating error messages.
2301 FUNCTION is a tree for the called function. It is used only for
2302 error messages, where it is formatted with %qE.
2304 This is also where warnings about wrong number of args are generated.
2306 Both VALUES and the returned value are chains of TREE_LIST nodes
2307 with the elements of the list in the TREE_VALUE slots of those nodes. */
2310 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2312 tree typetail, valtail;
2317 /* Change pointer to function to the function itself for
2319 if (TREE_CODE (function) == ADDR_EXPR
2320 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2321 function = TREE_OPERAND (function, 0);
2323 /* Handle an ObjC selector specially for diagnostics. */
2324 selector = objc_message_selector ();
2326 /* Scan the given expressions and types, producing individual
2327 converted arguments and pushing them on RESULT in reverse order. */
2329 for (valtail = values, typetail = typelist, parmnum = 0;
2331 valtail = TREE_CHAIN (valtail), parmnum++)
2333 tree type = typetail ? TREE_VALUE (typetail) : 0;
2334 tree val = TREE_VALUE (valtail);
2335 tree rname = function;
2336 int argnum = parmnum + 1;
2337 const char *invalid_func_diag;
2339 if (type == void_type_node)
2341 error ("too many arguments to function %qE", function);
2345 if (selector && argnum > 2)
2351 STRIP_TYPE_NOPS (val);
2353 val = require_complete_type (val);
2357 /* Formal parm type is specified by a function prototype. */
2360 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2362 error ("type of formal parameter %d is incomplete", parmnum + 1);
2367 /* Optionally warn about conversions that
2368 differ from the default conversions. */
2369 if (warn_conversion || warn_traditional)
2371 unsigned int formal_prec = TYPE_PRECISION (type);
2373 if (INTEGRAL_TYPE_P (type)
2374 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2375 warning (0, "passing argument %d of %qE as integer "
2376 "rather than floating due to prototype",
2378 if (INTEGRAL_TYPE_P (type)
2379 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2380 warning (0, "passing argument %d of %qE as integer "
2381 "rather than complex due to prototype",
2383 else if (TREE_CODE (type) == COMPLEX_TYPE
2384 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2385 warning (0, "passing argument %d of %qE as complex "
2386 "rather than floating due to prototype",
2388 else if (TREE_CODE (type) == REAL_TYPE
2389 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2390 warning (0, "passing argument %d of %qE as floating "
2391 "rather than integer due to prototype",
2393 else if (TREE_CODE (type) == COMPLEX_TYPE
2394 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2395 warning (0, "passing argument %d of %qE as complex "
2396 "rather than integer due to prototype",
2398 else if (TREE_CODE (type) == REAL_TYPE
2399 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2400 warning (0, "passing argument %d of %qE as floating "
2401 "rather than complex due to prototype",
2403 /* ??? At some point, messages should be written about
2404 conversions between complex types, but that's too messy
2406 else if (TREE_CODE (type) == REAL_TYPE
2407 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2409 /* Warn if any argument is passed as `float',
2410 since without a prototype it would be `double'. */
2411 if (formal_prec == TYPE_PRECISION (float_type_node)
2412 && type != dfloat32_type_node)
2413 warning (0, "passing argument %d of %qE as %<float%> "
2414 "rather than %<double%> due to prototype",
2417 /* Warn if mismatch between argument and prototype
2418 for decimal float types. Warn of conversions with
2419 binary float types and of precision narrowing due to
2421 else if (type != TREE_TYPE (val)
2422 && (type == dfloat32_type_node
2423 || type == dfloat64_type_node
2424 || type == dfloat128_type_node
2425 || TREE_TYPE (val) == dfloat32_type_node
2426 || TREE_TYPE (val) == dfloat64_type_node
2427 || TREE_TYPE (val) == dfloat128_type_node)
2429 <= TYPE_PRECISION (TREE_TYPE (val))
2430 || (type == dfloat128_type_node
2432 != dfloat64_type_node
2434 != dfloat32_type_node)))
2435 || (type == dfloat64_type_node
2437 != dfloat32_type_node))))
2438 warning (0, "passing argument %d of %qE as %qT "
2439 "rather than %qT due to prototype",
2440 argnum, rname, type, TREE_TYPE (val));
2443 /* Detect integer changing in width or signedness.
2444 These warnings are only activated with
2445 -Wconversion, not with -Wtraditional. */
2446 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2447 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2449 tree would_have_been = default_conversion (val);
2450 tree type1 = TREE_TYPE (would_have_been);
2452 if (TREE_CODE (type) == ENUMERAL_TYPE
2453 && (TYPE_MAIN_VARIANT (type)
2454 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2455 /* No warning if function asks for enum
2456 and the actual arg is that enum type. */
2458 else if (formal_prec != TYPE_PRECISION (type1))
2459 warning (OPT_Wconversion, "passing argument %d of %qE "
2460 "with different width due to prototype",
2462 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2464 /* Don't complain if the formal parameter type
2465 is an enum, because we can't tell now whether
2466 the value was an enum--even the same enum. */
2467 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2469 else if (TREE_CODE (val) == INTEGER_CST
2470 && int_fits_type_p (val, type))
2471 /* Change in signedness doesn't matter
2472 if a constant value is unaffected. */
2474 /* If the value is extended from a narrower
2475 unsigned type, it doesn't matter whether we
2476 pass it as signed or unsigned; the value
2477 certainly is the same either way. */
2478 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2479 && TYPE_UNSIGNED (TREE_TYPE (val)))
2481 else if (TYPE_UNSIGNED (type))
2482 warning (OPT_Wconversion, "passing argument %d of %qE "
2483 "as unsigned due to prototype",
2486 warning (OPT_Wconversion, "passing argument %d of %qE "
2487 "as signed due to prototype", argnum, rname);
2491 parmval = convert_for_assignment (type, val, ic_argpass,
2495 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2496 && INTEGRAL_TYPE_P (type)
2497 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2498 parmval = default_conversion (parmval);
2500 result = tree_cons (NULL_TREE, parmval, result);
2502 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2503 && (TYPE_PRECISION (TREE_TYPE (val))
2504 < TYPE_PRECISION (double_type_node))
2505 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (val))))
2506 /* Convert `float' to `double'. */
2507 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2508 else if ((invalid_func_diag =
2509 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2511 error (invalid_func_diag);
2512 return error_mark_node;
2515 /* Convert `short' and `char' to full-size `int'. */
2516 result = tree_cons (NULL_TREE, default_conversion (val), result);
2519 typetail = TREE_CHAIN (typetail);
2522 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2524 error ("too few arguments to function %qE", function);
2525 return error_mark_node;
2528 return nreverse (result);
2531 /* This is the entry point used by the parser to build unary operators
2532 in the input. CODE, a tree_code, specifies the unary operator, and
2533 ARG is the operand. For unary plus, the C parser currently uses
2534 CONVERT_EXPR for code. */
2537 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2539 struct c_expr result;
2541 result.original_code = ERROR_MARK;
2542 result.value = build_unary_op (code, arg.value, 0);
2543 overflow_warning (result.value);
2547 /* This is the entry point used by the parser to build binary operators
2548 in the input. CODE, a tree_code, specifies the binary operator, and
2549 ARG1 and ARG2 are the operands. In addition to constructing the
2550 expression, we check for operands that were written with other binary
2551 operators in a way that is likely to confuse the user. */
2554 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2557 struct c_expr result;
2559 enum tree_code code1 = arg1.original_code;
2560 enum tree_code code2 = arg2.original_code;
2562 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2563 result.original_code = code;
2565 if (TREE_CODE (result.value) == ERROR_MARK)
2568 /* Check for cases such as x+y<<z which users are likely
2570 if (warn_parentheses)
2572 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2574 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2575 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2576 warning (OPT_Wparentheses,
2577 "suggest parentheses around + or - inside shift");
2580 if (code == TRUTH_ORIF_EXPR)
2582 if (code1 == TRUTH_ANDIF_EXPR
2583 || code2 == TRUTH_ANDIF_EXPR)
2584 warning (OPT_Wparentheses,
2585 "suggest parentheses around && within ||");
2588 if (code == BIT_IOR_EXPR)
2590 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2591 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2592 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2593 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2594 warning (OPT_Wparentheses,
2595 "suggest parentheses around arithmetic in operand of |");
2596 /* Check cases like x|y==z */
2597 if (TREE_CODE_CLASS (code1) == tcc_comparison
2598 || TREE_CODE_CLASS (code2) == tcc_comparison)
2599 warning (OPT_Wparentheses,
2600 "suggest parentheses around comparison in operand of |");
2603 if (code == BIT_XOR_EXPR)
2605 if (code1 == BIT_AND_EXPR
2606 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2607 || code2 == BIT_AND_EXPR
2608 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2609 warning (OPT_Wparentheses,
2610 "suggest parentheses around arithmetic in operand of ^");
2611 /* Check cases like x^y==z */
2612 if (TREE_CODE_CLASS (code1) == tcc_comparison
2613 || TREE_CODE_CLASS (code2) == tcc_comparison)
2614 warning (OPT_Wparentheses,
2615 "suggest parentheses around comparison in operand of ^");
2618 if (code == BIT_AND_EXPR)
2620 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2621 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2622 warning (OPT_Wparentheses,
2623 "suggest parentheses around + or - in operand of &");
2624 /* Check cases like x&y==z */
2625 if (TREE_CODE_CLASS (code1) == tcc_comparison
2626 || TREE_CODE_CLASS (code2) == tcc_comparison)
2627 warning (OPT_Wparentheses,
2628 "suggest parentheses around comparison in operand of &");
2630 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2631 if (TREE_CODE_CLASS (code) == tcc_comparison
2632 && (TREE_CODE_CLASS (code1) == tcc_comparison
2633 || TREE_CODE_CLASS (code2) == tcc_comparison))
2634 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2635 "have their mathematical meaning");
2639 /* Warn about comparisons against string literals, with the exception
2640 of testing for equality or inequality of a string literal with NULL. */
2641 if (code == EQ_EXPR || code == NE_EXPR)
2643 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
2644 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
2645 warning (OPT_Wstring_literal_comparison,
2646 "comparison with string literal");
2648 else if (TREE_CODE_CLASS (code) == tcc_comparison
2649 && (code1 == STRING_CST || code2 == STRING_CST))
2650 warning (OPT_Wstring_literal_comparison,
2651 "comparison with string literal");
2653 overflow_warning (result.value);
2658 /* Return a tree for the difference of pointers OP0 and OP1.
2659 The resulting tree has type int. */
2662 pointer_diff (tree op0, tree op1)
2664 tree restype = ptrdiff_type_node;
2666 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2667 tree con0, con1, lit0, lit1;
2668 tree orig_op1 = op1;
2670 if (pedantic || warn_pointer_arith)
2672 if (TREE_CODE (target_type) == VOID_TYPE)
2673 pedwarn ("pointer of type %<void *%> used in subtraction");
2674 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2675 pedwarn ("pointer to a function used in subtraction");
2678 /* If the conversion to ptrdiff_type does anything like widening or
2679 converting a partial to an integral mode, we get a convert_expression
2680 that is in the way to do any simplifications.
2681 (fold-const.c doesn't know that the extra bits won't be needed.
2682 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2683 different mode in place.)
2684 So first try to find a common term here 'by hand'; we want to cover
2685 at least the cases that occur in legal static initializers. */
2686 if ((TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == CONVERT_EXPR)
2687 && (TYPE_PRECISION (TREE_TYPE (op0))
2688 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
2689 con0 = TREE_OPERAND (op0, 0);
2692 if ((TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == CONVERT_EXPR)
2693 && (TYPE_PRECISION (TREE_TYPE (op1))
2694 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
2695 con1 = TREE_OPERAND (op1, 0);
2699 if (TREE_CODE (con0) == PLUS_EXPR)
2701 lit0 = TREE_OPERAND (con0, 1);
2702 con0 = TREE_OPERAND (con0, 0);
2705 lit0 = integer_zero_node;
2707 if (TREE_CODE (con1) == PLUS_EXPR)
2709 lit1 = TREE_OPERAND (con1, 1);
2710 con1 = TREE_OPERAND (con1, 0);
2713 lit1 = integer_zero_node;
2715 if (operand_equal_p (con0, con1, 0))
2722 /* First do the subtraction as integers;
2723 then drop through to build the divide operator.
2724 Do not do default conversions on the minus operator
2725 in case restype is a short type. */
2727 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2728 convert (restype, op1), 0);
2729 /* This generates an error if op1 is pointer to incomplete type. */
2730 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2731 error ("arithmetic on pointer to an incomplete type");
2733 /* This generates an error if op0 is pointer to incomplete type. */
2734 op1 = c_size_in_bytes (target_type);
2736 /* Divide by the size, in easiest possible way. */
2737 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2740 /* Construct and perhaps optimize a tree representation
2741 for a unary operation. CODE, a tree_code, specifies the operation
2742 and XARG is the operand.
2743 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2744 the default promotions (such as from short to int).
2745 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2746 allows non-lvalues; this is only used to handle conversion of non-lvalue
2747 arrays to pointers in C99. */
2750 build_unary_op (enum tree_code code, tree xarg, int flag)
2752 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2755 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2757 int noconvert = flag;
2758 const char *invalid_op_diag;
2760 if (typecode == ERROR_MARK)
2761 return error_mark_node;
2762 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2763 typecode = INTEGER_TYPE;
2765 if ((invalid_op_diag
2766 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2768 error (invalid_op_diag);
2769 return error_mark_node;
2775 /* This is used for unary plus, because a CONVERT_EXPR
2776 is enough to prevent anybody from looking inside for
2777 associativity, but won't generate any code. */
2778 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2779 || typecode == COMPLEX_TYPE
2780 || typecode == VECTOR_TYPE))
2782 error ("wrong type argument to unary plus");
2783 return error_mark_node;
2785 else if (!noconvert)
2786 arg = default_conversion (arg);
2787 arg = non_lvalue (arg);
2791 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2792 || typecode == COMPLEX_TYPE
2793 || typecode == VECTOR_TYPE))
2795 error ("wrong type argument to unary minus");
2796 return error_mark_node;
2798 else if (!noconvert)
2799 arg = default_conversion (arg);
2803 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2806 arg = default_conversion (arg);
2808 else if (typecode == COMPLEX_TYPE)
2812 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2814 arg = default_conversion (arg);
2818 error ("wrong type argument to bit-complement");
2819 return error_mark_node;
2824 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2826 error ("wrong type argument to abs");
2827 return error_mark_node;
2829 else if (!noconvert)
2830 arg = default_conversion (arg);
2834 /* Conjugating a real value is a no-op, but allow it anyway. */
2835 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2836 || typecode == COMPLEX_TYPE))
2838 error ("wrong type argument to conjugation");
2839 return error_mark_node;
2841 else if (!noconvert)
2842 arg = default_conversion (arg);
2845 case TRUTH_NOT_EXPR:
2846 if (typecode != INTEGER_TYPE
2847 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2848 && typecode != COMPLEX_TYPE)
2850 error ("wrong type argument to unary exclamation mark");
2851 return error_mark_node;
2853 arg = c_objc_common_truthvalue_conversion (arg);
2854 return invert_truthvalue (arg);
2857 if (TREE_CODE (arg) == COMPLEX_CST)
2858 return TREE_REALPART (arg);
2859 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2860 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2865 if (TREE_CODE (arg) == COMPLEX_CST)
2866 return TREE_IMAGPART (arg);
2867 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2868 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2870 return convert (TREE_TYPE (arg), integer_zero_node);
2872 case PREINCREMENT_EXPR:
2873 case POSTINCREMENT_EXPR:
2874 case PREDECREMENT_EXPR:
2875 case POSTDECREMENT_EXPR:
2877 /* Increment or decrement the real part of the value,
2878 and don't change the imaginary part. */
2879 if (typecode == COMPLEX_TYPE)
2884 pedwarn ("ISO C does not support %<++%> and %<--%>"
2885 " on complex types");
2887 arg = stabilize_reference (arg);
2888 real = build_unary_op (REALPART_EXPR, arg, 1);
2889 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2890 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2891 build_unary_op (code, real, 1), imag);
2894 /* Report invalid types. */
2896 if (typecode != POINTER_TYPE
2897 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2899 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2900 error ("wrong type argument to increment");
2902 error ("wrong type argument to decrement");
2904 return error_mark_node;
2909 tree result_type = TREE_TYPE (arg);
2911 arg = get_unwidened (arg, 0);
2912 argtype = TREE_TYPE (arg);
2914 /* Compute the increment. */
2916 if (typecode == POINTER_TYPE)
2918 /* If pointer target is an undefined struct,
2919 we just cannot know how to do the arithmetic. */
2920 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2922 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2923 error ("increment of pointer to unknown structure");
2925 error ("decrement of pointer to unknown structure");
2927 else if ((pedantic || warn_pointer_arith)
2928 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2929 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2931 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2932 pedwarn ("wrong type argument to increment");
2934 pedwarn ("wrong type argument to decrement");
2937 inc = c_size_in_bytes (TREE_TYPE (result_type));
2940 inc = integer_one_node;
2942 inc = convert (argtype, inc);
2944 /* Complain about anything else that is not a true lvalue. */
2945 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2946 || code == POSTINCREMENT_EXPR)
2949 return error_mark_node;
2951 /* Report a read-only lvalue. */
2952 if (TREE_READONLY (arg))
2954 readonly_error (arg,
2955 ((code == PREINCREMENT_EXPR
2956 || code == POSTINCREMENT_EXPR)
2957 ? lv_increment : lv_decrement));
2958 return error_mark_node;
2961 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2962 val = boolean_increment (code, arg);
2964 val = build2 (code, TREE_TYPE (arg), arg, inc);
2965 TREE_SIDE_EFFECTS (val) = 1;
2966 val = convert (result_type, val);
2967 if (TREE_CODE (val) != code)
2968 TREE_NO_WARNING (val) = 1;
2973 /* Note that this operation never does default_conversion. */
2975 /* Let &* cancel out to simplify resulting code. */
2976 if (TREE_CODE (arg) == INDIRECT_REF)
2978 /* Don't let this be an lvalue. */
2979 if (lvalue_p (TREE_OPERAND (arg, 0)))
2980 return non_lvalue (TREE_OPERAND (arg, 0));
2981 return TREE_OPERAND (arg, 0);
2984 /* For &x[y], return x+y */
2985 if (TREE_CODE (arg) == ARRAY_REF)
2987 tree op0 = TREE_OPERAND (arg, 0);
2988 if (!c_mark_addressable (op0))
2989 return error_mark_node;
2990 return build_binary_op (PLUS_EXPR,
2991 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
2992 ? array_to_pointer_conversion (op0)
2994 TREE_OPERAND (arg, 1), 1);
2997 /* Anything not already handled and not a true memory reference
2998 or a non-lvalue array is an error. */
2999 else if (typecode != FUNCTION_TYPE && !flag
3000 && !lvalue_or_else (arg, lv_addressof))
3001 return error_mark_node;
3003 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3004 argtype = TREE_TYPE (arg);
3006 /* If the lvalue is const or volatile, merge that into the type
3007 to which the address will point. Note that you can't get a
3008 restricted pointer by taking the address of something, so we
3009 only have to deal with `const' and `volatile' here. */
3010 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3011 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3012 argtype = c_build_type_variant (argtype,
3013 TREE_READONLY (arg),
3014 TREE_THIS_VOLATILE (arg));
3016 if (!c_mark_addressable (arg))
3017 return error_mark_node;
3019 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3020 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3022 argtype = build_pointer_type (argtype);
3024 /* ??? Cope with user tricks that amount to offsetof. Delete this
3025 when we have proper support for integer constant expressions. */
3026 val = get_base_address (arg);
3027 if (val && TREE_CODE (val) == INDIRECT_REF
3028 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3030 tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
3032 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
3033 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
3036 val = build1 (ADDR_EXPR, argtype, arg);
3045 argtype = TREE_TYPE (arg);
3046 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
3047 : fold_build1 (code, argtype, arg);
3050 /* Return nonzero if REF is an lvalue valid for this language.
3051 Lvalues can be assigned, unless their type has TYPE_READONLY.
3052 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3057 enum tree_code code = TREE_CODE (ref);
3064 return lvalue_p (TREE_OPERAND (ref, 0));
3066 case COMPOUND_LITERAL_EXPR:
3076 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3077 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3080 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3087 /* Give an error for storing in something that is 'const'. */
3090 readonly_error (tree arg, enum lvalue_use use)
3092 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3094 /* Using this macro rather than (for example) arrays of messages
3095 ensures that all the format strings are checked at compile
3097 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3098 : (use == lv_increment ? (I) \
3099 : (use == lv_decrement ? (D) : (AS))))
3100 if (TREE_CODE (arg) == COMPONENT_REF)
3102 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3103 readonly_error (TREE_OPERAND (arg, 0), use);
3105 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3106 G_("increment of read-only member %qD"),
3107 G_("decrement of read-only member %qD"),
3108 G_("read-only member %qD used as %<asm%> output")),
3109 TREE_OPERAND (arg, 1));
3111 else if (TREE_CODE (arg) == VAR_DECL)
3112 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3113 G_("increment of read-only variable %qD"),
3114 G_("decrement of read-only variable %qD"),
3115 G_("read-only variable %qD used as %<asm%> output")),
3118 error (READONLY_MSG (G_("assignment of read-only location"),
3119 G_("increment of read-only location"),
3120 G_("decrement of read-only location"),
3121 G_("read-only location used as %<asm%> output")));
3125 /* Return nonzero if REF is an lvalue valid for this language;
3126 otherwise, print an error message and return zero. USE says
3127 how the lvalue is being used and so selects the error message. */
3130 lvalue_or_else (tree ref, enum lvalue_use use)
3132 int win = lvalue_p (ref);
3140 /* Mark EXP saying that we need to be able to take the
3141 address of it; it should not be allocated in a register.
3142 Returns true if successful. */
3145 c_mark_addressable (tree exp)
3150 switch (TREE_CODE (x))
3153 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3156 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3160 /* ... fall through ... */
3166 x = TREE_OPERAND (x, 0);
3169 case COMPOUND_LITERAL_EXPR:
3171 TREE_ADDRESSABLE (x) = 1;
3178 if (C_DECL_REGISTER (x)
3179 && DECL_NONLOCAL (x))
3181 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3184 ("global register variable %qD used in nested function", x);
3187 pedwarn ("register variable %qD used in nested function", x);
3189 else if (C_DECL_REGISTER (x))
3191 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3192 error ("address of global register variable %qD requested", x);
3194 error ("address of register variable %qD requested", x);
3200 TREE_ADDRESSABLE (x) = 1;
3207 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3210 build_conditional_expr (tree ifexp, tree op1, tree op2)
3214 enum tree_code code1;
3215 enum tree_code code2;
3216 tree result_type = NULL;
3217 tree orig_op1 = op1, orig_op2 = op2;
3219 /* Promote both alternatives. */
3221 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3222 op1 = default_conversion (op1);
3223 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3224 op2 = default_conversion (op2);
3226 if (TREE_CODE (ifexp) == ERROR_MARK
3227 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3228 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3229 return error_mark_node;
3231 type1 = TREE_TYPE (op1);
3232 code1 = TREE_CODE (type1);
3233 type2 = TREE_TYPE (op2);
3234 code2 = TREE_CODE (type2);
3236 /* C90 does not permit non-lvalue arrays in conditional expressions.
3237 In C99 they will be pointers by now. */
3238 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3240 error ("non-lvalue array in conditional expression");
3241 return error_mark_node;
3244 /* Quickly detect the usual case where op1 and op2 have the same type
3246 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3249 result_type = type1;
3251 result_type = TYPE_MAIN_VARIANT (type1);
3253 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3254 || code1 == COMPLEX_TYPE)
3255 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3256 || code2 == COMPLEX_TYPE))
3258 result_type = c_common_type (type1, type2);
3260 /* If -Wsign-compare, warn here if type1 and type2 have
3261 different signedness. We'll promote the signed to unsigned
3262 and later code won't know it used to be different.
3263 Do this check on the original types, so that explicit casts
3264 will be considered, but default promotions won't. */
3265 if (warn_sign_compare && !skip_evaluation)
3267 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3268 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3270 if (unsigned_op1 ^ unsigned_op2)
3272 /* Do not warn if the result type is signed, since the
3273 signed type will only be chosen if it can represent
3274 all the values of the unsigned type. */
3275 if (!TYPE_UNSIGNED (result_type))
3277 /* Do not warn if the signed quantity is an unsuffixed
3278 integer literal (or some static constant expression
3279 involving such literals) and it is non-negative. */
3280 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3281 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3284 warning (0, "signed and unsigned type in conditional expression");
3288 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3290 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3291 pedwarn ("ISO C forbids conditional expr with only one void side");
3292 result_type = void_type_node;
3294 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3296 if (comp_target_types (type1, type2))
3297 result_type = common_pointer_type (type1, type2);
3298 else if (null_pointer_constant_p (orig_op1))
3299 result_type = qualify_type (type2, type1);
3300 else if (null_pointer_constant_p (orig_op2))
3301 result_type = qualify_type (type1, type2);
3302 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3304 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3305 pedwarn ("ISO C forbids conditional expr between "
3306 "%<void *%> and function pointer");
3307 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3308 TREE_TYPE (type2)));
3310 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3312 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3313 pedwarn ("ISO C forbids conditional expr between "
3314 "%<void *%> and function pointer");
3315 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3316 TREE_TYPE (type1)));
3320 pedwarn ("pointer type mismatch in conditional expression");
3321 result_type = build_pointer_type (void_type_node);
3324 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3326 if (!null_pointer_constant_p (orig_op2))
3327 pedwarn ("pointer/integer type mismatch in conditional expression");
3330 op2 = null_pointer_node;
3332 result_type = type1;
3334 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3336 if (!null_pointer_constant_p (orig_op1))
3337 pedwarn ("pointer/integer type mismatch in conditional expression");
3340 op1 = null_pointer_node;
3342 result_type = type2;
3347 if (flag_cond_mismatch)
3348 result_type = void_type_node;
3351 error ("type mismatch in conditional expression");
3352 return error_mark_node;
3356 /* Merge const and volatile flags of the incoming types. */
3358 = build_type_variant (result_type,
3359 TREE_READONLY (op1) || TREE_READONLY (op2),
3360 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3362 if (result_type != TREE_TYPE (op1))
3363 op1 = convert_and_check (result_type, op1);
3364 if (result_type != TREE_TYPE (op2))
3365 op2 = convert_and_check (result_type, op2);
3367 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3370 /* Return a compound expression that performs two expressions and
3371 returns the value of the second of them. */
3374 build_compound_expr (tree expr1, tree expr2)
3376 if (!TREE_SIDE_EFFECTS (expr1))
3378 /* The left-hand operand of a comma expression is like an expression
3379 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3380 any side-effects, unless it was explicitly cast to (void). */
3381 if (warn_unused_value)
3383 if (VOID_TYPE_P (TREE_TYPE (expr1))
3384 && (TREE_CODE (expr1) == NOP_EXPR
3385 || TREE_CODE (expr1) == CONVERT_EXPR))
3387 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3388 && TREE_CODE (expr1) == COMPOUND_EXPR
3389 && (TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR
3390 || TREE_CODE (TREE_OPERAND (expr1, 1)) == NOP_EXPR))
3391 ; /* (void) a, (void) b, c */
3393 warning (0, "left-hand operand of comma expression has no effect");
3397 /* With -Wunused, we should also warn if the left-hand operand does have
3398 side-effects, but computes a value which is not used. For example, in
3399 `foo() + bar(), baz()' the result of the `+' operator is not used,
3400 so we should issue a warning. */
3401 else if (warn_unused_value)
3402 warn_if_unused_value (expr1, input_location);
3404 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3407 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3410 build_c_cast (tree type, tree expr)
3414 if (type == error_mark_node || expr == error_mark_node)
3415 return error_mark_node;
3417 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3418 only in <protocol> qualifications. But when constructing cast expressions,
3419 the protocols do matter and must be kept around. */
3420 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3421 return build1 (NOP_EXPR, type, expr);
3423 type = TYPE_MAIN_VARIANT (type);
3425 if (TREE_CODE (type) == ARRAY_TYPE)
3427 error ("cast specifies array type");
3428 return error_mark_node;
3431 if (TREE_CODE (type) == FUNCTION_TYPE)
3433 error ("cast specifies function type");
3434 return error_mark_node;
3437 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3441 if (TREE_CODE (type) == RECORD_TYPE
3442 || TREE_CODE (type) == UNION_TYPE)
3443 pedwarn ("ISO C forbids casting nonscalar to the same type");
3446 else if (TREE_CODE (type) == UNION_TYPE)
3450 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3451 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3452 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3460 pedwarn ("ISO C forbids casts to union type");
3461 t = digest_init (type,
3462 build_constructor_single (type, field, value),
3464 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3465 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3468 error ("cast to union type from type not present in union");
3469 return error_mark_node;
3475 if (type == void_type_node)
3476 return build1 (CONVERT_EXPR, type, value);
3478 otype = TREE_TYPE (value);
3480 /* Optionally warn about potentially worrisome casts. */
3483 && TREE_CODE (type) == POINTER_TYPE
3484 && TREE_CODE (otype) == POINTER_TYPE)
3486 tree in_type = type;
3487 tree in_otype = otype;
3491 /* Check that the qualifiers on IN_TYPE are a superset of
3492 the qualifiers of IN_OTYPE. The outermost level of
3493 POINTER_TYPE nodes is uninteresting and we stop as soon
3494 as we hit a non-POINTER_TYPE node on either type. */
3497 in_otype = TREE_TYPE (in_otype);
3498 in_type = TREE_TYPE (in_type);
3500 /* GNU C allows cv-qualified function types. 'const'
3501 means the function is very pure, 'volatile' means it
3502 can't return. We need to warn when such qualifiers
3503 are added, not when they're taken away. */
3504 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3505 && TREE_CODE (in_type) == FUNCTION_TYPE)
3506 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3508 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3510 while (TREE_CODE (in_type) == POINTER_TYPE
3511 && TREE_CODE (in_otype) == POINTER_TYPE);
3514 warning (0, "cast adds new qualifiers to function type");
3517 /* There are qualifiers present in IN_OTYPE that are not
3518 present in IN_TYPE. */
3519 warning (0, "cast discards qualifiers from pointer target type");
3522 /* Warn about possible alignment problems. */
3523 if (STRICT_ALIGNMENT
3524 && TREE_CODE (type) == POINTER_TYPE
3525 && TREE_CODE (otype) == POINTER_TYPE
3526 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3527 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3528 /* Don't warn about opaque types, where the actual alignment
3529 restriction is unknown. */
3530 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3531 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3532 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3533 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3534 warning (OPT_Wcast_align,
3535 "cast increases required alignment of target type");
3537 if (TREE_CODE (type) == INTEGER_TYPE
3538 && TREE_CODE (otype) == POINTER_TYPE
3539 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
3540 /* Unlike conversion of integers to pointers, where the
3541 warning is disabled for converting constants because
3542 of cases such as SIG_*, warn about converting constant
3543 pointers to integers. In some cases it may cause unwanted
3544 sign extension, and a warning is appropriate. */
3545 warning (OPT_Wpointer_to_int_cast,
3546 "cast from pointer to integer of different size");
3548 if (TREE_CODE (value) == CALL_EXPR
3549 && TREE_CODE (type) != TREE_CODE (otype))
3550 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3551 "to non-matching type %qT", otype, type);
3553 if (TREE_CODE (type) == POINTER_TYPE
3554 && TREE_CODE (otype) == INTEGER_TYPE
3555 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3556 /* Don't warn about converting any constant. */
3557 && !TREE_CONSTANT (value))
3558 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3559 "of different size");
3561 strict_aliasing_warning (otype, type, expr);
3563 /* If pedantic, warn for conversions between function and object
3564 pointer types, except for converting a null pointer constant
3565 to function pointer type. */
3567 && TREE_CODE (type) == POINTER_TYPE
3568 && TREE_CODE (otype) == POINTER_TYPE
3569 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3570 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3571 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3574 && TREE_CODE (type) == POINTER_TYPE
3575 && TREE_CODE (otype) == POINTER_TYPE
3576 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3577 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3578 && !null_pointer_constant_p (value))
3579 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3582 value = convert (type, value);
3584 /* Ignore any integer overflow caused by the cast. */
3585 if (TREE_CODE (value) == INTEGER_CST)
3587 if (CONSTANT_CLASS_P (ovalue)
3588 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3590 /* Avoid clobbering a shared constant. */
3591 value = copy_node (value);
3592 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3593 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3595 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3596 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3597 value = build_int_cst_wide (TREE_TYPE (value),
3598 TREE_INT_CST_LOW (value),
3599 TREE_INT_CST_HIGH (value));
3603 /* Don't let a cast be an lvalue. */
3605 value = non_lvalue (value);
3610 /* Interpret a cast of expression EXPR to type TYPE. */
3612 c_cast_expr (struct c_type_name *type_name, tree expr)
3615 int saved_wsp = warn_strict_prototypes;
3617 /* This avoids warnings about unprototyped casts on
3618 integers. E.g. "#define SIG_DFL (void(*)())0". */
3619 if (TREE_CODE (expr) == INTEGER_CST)
3620 warn_strict_prototypes = 0;
3621 type = groktypename (type_name);
3622 warn_strict_prototypes = saved_wsp;
3624 return build_c_cast (type, expr);
3627 /* Build an assignment expression of lvalue LHS from value RHS.
3628 MODIFYCODE is the code for a binary operator that we use
3629 to combine the old value of LHS with RHS to get the new value.
3630 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3633 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3637 tree lhstype = TREE_TYPE (lhs);
3638 tree olhstype = lhstype;
3640 /* Types that aren't fully specified cannot be used in assignments. */
3641 lhs = require_complete_type (lhs);
3643 /* Avoid duplicate error messages from operands that had errors. */
3644 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3645 return error_mark_node;
3647 STRIP_TYPE_NOPS (rhs);
3651 /* If a binary op has been requested, combine the old LHS value with the RHS
3652 producing the value we should actually store into the LHS. */
3654 if (modifycode != NOP_EXPR)
3656 lhs = stabilize_reference (lhs);
3657 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3660 if (!lvalue_or_else (lhs, lv_assign))
3661 return error_mark_node;
3663 /* Give an error for storing in something that is 'const'. */
3665 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3666 || ((TREE_CODE (lhstype) == RECORD_TYPE
3667 || TREE_CODE (lhstype) == UNION_TYPE)
3668 && C_TYPE_FIELDS_READONLY (lhstype)))
3670 readonly_error (lhs, lv_assign);
3671 return error_mark_node;
3674 /* If storing into a structure or union member,
3675 it has probably been given type `int'.
3676 Compute the type that would go with
3677 the actual amount of storage the member occupies. */
3679 if (TREE_CODE (lhs) == COMPONENT_REF
3680 && (TREE_CODE (lhstype) == INTEGER_TYPE
3681 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3682 || TREE_CODE (lhstype) == REAL_TYPE
3683 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3684 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3686 /* If storing in a field that is in actuality a short or narrower than one,
3687 we must store in the field in its actual type. */
3689 if (lhstype != TREE_TYPE (lhs))
3691 lhs = copy_node (lhs);
3692 TREE_TYPE (lhs) = lhstype;
3695 /* Convert new value to destination type. */
3697 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3698 NULL_TREE, NULL_TREE, 0);
3699 if (TREE_CODE (newrhs) == ERROR_MARK)
3700 return error_mark_node;
3702 /* Emit ObjC write barrier, if necessary. */
3703 if (c_dialect_objc () && flag_objc_gc)
3705 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3710 /* Scan operands. */
3712 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3713 TREE_SIDE_EFFECTS (result) = 1;
3715 /* If we got the LHS in a different type for storing in,
3716 convert the result back to the nominal type of LHS
3717 so that the value we return always has the same type
3718 as the LHS argument. */
3720 if (olhstype == TREE_TYPE (result))
3722 return convert_for_assignment (olhstype, result, ic_assign,
3723 NULL_TREE, NULL_TREE, 0);
3726 /* Convert value RHS to type TYPE as preparation for an assignment
3727 to an lvalue of type TYPE.
3728 The real work of conversion is done by `convert'.
3729 The purpose of this function is to generate error messages
3730 for assignments that are not allowed in C.
3731 ERRTYPE says whether it is argument passing, assignment,
3732 initialization or return.
3734 FUNCTION is a tree for the function being called.
3735 PARMNUM is the number of the argument, for printing in error messages. */
3738 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3739 tree fundecl, tree function, int parmnum)
3741 enum tree_code codel = TREE_CODE (type);
3743 enum tree_code coder;
3744 tree rname = NULL_TREE;
3745 bool objc_ok = false;
3747 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3750 /* Change pointer to function to the function itself for
3752 if (TREE_CODE (function) == ADDR_EXPR
3753 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3754 function = TREE_OPERAND (function, 0);
3756 /* Handle an ObjC selector specially for diagnostics. */
3757 selector = objc_message_selector ();
3759 if (selector && parmnum > 2)
3766 /* This macro is used to emit diagnostics to ensure that all format
3767 strings are complete sentences, visible to gettext and checked at
3769 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3774 pedwarn (AR, parmnum, rname); \
3776 case ic_argpass_nonproto: \
3777 warning (0, AR, parmnum, rname); \
3789 gcc_unreachable (); \
3793 STRIP_TYPE_NOPS (rhs);
3795 if (optimize && TREE_CODE (rhs) == VAR_DECL
3796 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3797 rhs = decl_constant_value_for_broken_optimization (rhs);
3799 rhstype = TREE_TYPE (rhs);
3800 coder = TREE_CODE (rhstype);
3802 if (coder == ERROR_MARK)
3803 return error_mark_node;
3805 if (c_dialect_objc ())
3828 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3831 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3833 overflow_warning (rhs);
3837 if (coder == VOID_TYPE)
3839 /* Except for passing an argument to an unprototyped function,
3840 this is a constraint violation. When passing an argument to
3841 an unprototyped function, it is compile-time undefined;
3842 making it a constraint in that case was rejected in
3844 error ("void value not ignored as it ought to be");
3845 return error_mark_node;
3847 /* A type converts to a reference to it.
3848 This code doesn't fully support references, it's just for the
3849 special case of va_start and va_copy. */
3850 if (codel == REFERENCE_TYPE
3851 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3853 if (!lvalue_p (rhs))
3855 error ("cannot pass rvalue to reference parameter");
3856 return error_mark_node;
3858 if (!c_mark_addressable (rhs))
3859 return error_mark_node;
3860 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3862 /* We already know that these two types are compatible, but they
3863 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3864 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3865 likely to be va_list, a typedef to __builtin_va_list, which
3866 is different enough that it will cause problems later. */
3867 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3868 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3870 rhs = build1 (NOP_EXPR, type, rhs);
3873 /* Some types can interconvert without explicit casts. */
3874 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3875 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3876 return convert (type, rhs);
3877 /* Arithmetic types all interconvert, and enum is treated like int. */
3878 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3879 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3880 || codel == BOOLEAN_TYPE)
3881 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3882 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3883 || coder == BOOLEAN_TYPE))
3884 return convert_and_check (type, rhs);
3886 /* Conversion to a transparent union from its member types.
3887 This applies only to function arguments. */
3888 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3889 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3891 tree memb, marginal_memb = NULL_TREE;
3893 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3895 tree memb_type = TREE_TYPE (memb);
3897 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3898 TYPE_MAIN_VARIANT (rhstype)))
3901 if (TREE_CODE (memb_type) != POINTER_TYPE)
3904 if (coder == POINTER_TYPE)
3906 tree ttl = TREE_TYPE (memb_type);
3907 tree ttr = TREE_TYPE (rhstype);
3909 /* Any non-function converts to a [const][volatile] void *
3910 and vice versa; otherwise, targets must be the same.
3911 Meanwhile, the lhs target must have all the qualifiers of
3913 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3914 || comp_target_types (memb_type, rhstype))
3916 /* If this type won't generate any warnings, use it. */
3917 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3918 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3919 && TREE_CODE (ttl) == FUNCTION_TYPE)
3920 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3921 == TYPE_QUALS (ttr))
3922 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3923 == TYPE_QUALS (ttl))))
3926 /* Keep looking for a better type, but remember this one. */
3928 marginal_memb = memb;
3932 /* Can convert integer zero to any pointer type. */
3933 if (null_pointer_constant_p (rhs))
3935 rhs = null_pointer_node;
3940 if (memb || marginal_memb)
3944 /* We have only a marginally acceptable member type;
3945 it needs a warning. */
3946 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3947 tree ttr = TREE_TYPE (rhstype);
3949 /* Const and volatile mean something different for function
3950 types, so the usual warnings are not appropriate. */
3951 if (TREE_CODE (ttr) == FUNCTION_TYPE
3952 && TREE_CODE (ttl) == FUNCTION_TYPE)
3954 /* Because const and volatile on functions are
3955 restrictions that say the function will not do
3956 certain things, it is okay to use a const or volatile
3957 function where an ordinary one is wanted, but not
3959 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3960 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3961 "makes qualified function "
3962 "pointer from unqualified"),
3963 G_("assignment makes qualified "
3964 "function pointer from "
3966 G_("initialization makes qualified "
3967 "function pointer from "
3969 G_("return makes qualified function "
3970 "pointer from unqualified"));
3972 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3973 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3974 "qualifiers from pointer target type"),
3975 G_("assignment discards qualifiers "
3976 "from pointer target type"),
3977 G_("initialization discards qualifiers "
3978 "from pointer target type"),
3979 G_("return discards qualifiers from "
3980 "pointer target type"));
3982 memb = marginal_memb;
3985 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
3986 pedwarn ("ISO C prohibits argument conversion to union type");
3988 return build_constructor_single (type, memb, rhs);
3992 /* Conversions among pointers */
3993 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)