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, 2007, 2008, 2009, 2010
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
30 #include "coretypes.h"
33 #include "langhooks.h"
41 #include "tree-iterator.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
54 /* Whether we are building a boolean conversion inside
55 convert_for_assignment, or some other late binary operation. If
56 build_binary_op is called (from code shared with C++) in this case,
57 then the operands have already been folded and the result will not
58 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
59 bool in_late_binary_op;
61 /* The level of nesting inside "__alignof__". */
64 /* The level of nesting inside "sizeof". */
67 /* The level of nesting inside "typeof". */
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
81 static int comp_target_types (location_t, tree, tree);
82 static int function_types_compatible_p (const_tree, const_tree, bool *,
84 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
85 static tree lookup_field (tree, tree);
86 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
88 static tree pointer_diff (location_t, tree, tree);
89 static tree convert_for_assignment (location_t, tree, tree, tree,
90 enum impl_conv, bool, tree, tree, int);
91 static tree valid_compound_expr_initializer (tree, tree);
92 static void push_string (const char *);
93 static void push_member_name (tree);
94 static int spelling_length (void);
95 static char *print_spelling (char *);
96 static void warning_init (int, const char *);
97 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
98 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
100 static void output_pending_init_elements (int, struct obstack *);
101 static int set_designator (int, struct obstack *);
102 static void push_range_stack (tree, struct obstack *);
103 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
104 static void set_nonincremental_init (struct obstack *);
105 static void set_nonincremental_init_from_string (tree, struct obstack *);
106 static tree find_init_member (tree, struct obstack *);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
136 note_integer_operands (tree expr)
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
223 type_code_string = "struct";
227 type_code_string = "union";
231 type_code_string = "enum";
235 error ("invalid use of void expression");
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
246 type = TREE_TYPE (type);
249 error ("invalid use of array with unspecified bounds");
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
298 else if (targetm.addr_space.subset_p (as1, as2))
303 else if (targetm.addr_space.subset_p (as2, as1))
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
327 error ("%qT and %qT are in disjoint named address spaces",
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
368 if (t2 == error_mark_node)
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
387 gcc_assert (code1 == code2);
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type (target);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
483 return build_type_attribute_variant (t1, attributes);
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
518 /* Tell global_bindings_p to return false so that variable_size
519 doesn't die on VLAs in parameter types. */
520 c_override_global_bindings_to_false = true;
522 len = list_length (p1);
525 for (i = 0; i < len; i++)
526 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
531 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
533 /* A null type means arg type is not specified.
534 Take whatever the other function type has. */
535 if (TREE_VALUE (p1) == 0)
537 TREE_VALUE (n) = TREE_VALUE (p2);
540 if (TREE_VALUE (p2) == 0)
542 TREE_VALUE (n) = TREE_VALUE (p1);
546 /* Given wait (union {union wait *u; int *i} *)
547 and wait (union wait *),
548 prefer union wait * as type of parm. */
549 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550 && TREE_VALUE (p1) != TREE_VALUE (p2))
553 tree mv2 = TREE_VALUE (p2);
554 if (mv2 && mv2 != error_mark_node
555 && TREE_CODE (mv2) != ARRAY_TYPE)
556 mv2 = TYPE_MAIN_VARIANT (mv2);
557 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
558 memb; memb = DECL_CHAIN (memb))
560 tree mv3 = TREE_TYPE (memb);
561 if (mv3 && mv3 != error_mark_node
562 && TREE_CODE (mv3) != ARRAY_TYPE)
563 mv3 = TYPE_MAIN_VARIANT (mv3);
564 if (comptypes (mv3, mv2))
566 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
568 pedwarn (input_location, OPT_pedantic,
569 "function types not truly compatible in ISO C");
574 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575 && TREE_VALUE (p2) != TREE_VALUE (p1))
578 tree mv1 = TREE_VALUE (p1);
579 if (mv1 && mv1 != error_mark_node
580 && TREE_CODE (mv1) != ARRAY_TYPE)
581 mv1 = TYPE_MAIN_VARIANT (mv1);
582 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
583 memb; memb = DECL_CHAIN (memb))
585 tree mv3 = TREE_TYPE (memb);
586 if (mv3 && mv3 != error_mark_node
587 && TREE_CODE (mv3) != ARRAY_TYPE)
588 mv3 = TYPE_MAIN_VARIANT (mv3);
589 if (comptypes (mv3, mv1))
591 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
593 pedwarn (input_location, OPT_pedantic,
594 "function types not truly compatible in ISO C");
599 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
603 c_override_global_bindings_to_false = false;
604 t1 = build_function_type (valtype, newargs);
605 t1 = qualify_type (t1, t2);
606 /* ... falls through ... */
610 return build_type_attribute_variant (t1, attributes);
615 /* Return the type of a conditional expression between pointers to
616 possibly differently qualified versions of compatible types.
618 We assume that comp_target_types has already been done and returned
619 nonzero; if that isn't so, this may crash. */
622 common_pointer_type (tree t1, tree t2)
625 tree pointed_to_1, mv1;
626 tree pointed_to_2, mv2;
628 unsigned target_quals;
629 addr_space_t as1, as2, as_common;
632 /* Save time if the two types are the same. */
634 if (t1 == t2) return t1;
636 /* If one type is nonsense, use the other. */
637 if (t1 == error_mark_node)
639 if (t2 == error_mark_node)
642 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643 && TREE_CODE (t2) == POINTER_TYPE);
645 /* Merge the attributes. */
646 attributes = targetm.merge_type_attributes (t1, t2);
648 /* Find the composite type of the target types, and combine the
649 qualifiers of the two types' targets. Do not lose qualifiers on
650 array element types by taking the TYPE_MAIN_VARIANT. */
651 mv1 = pointed_to_1 = TREE_TYPE (t1);
652 mv2 = pointed_to_2 = TREE_TYPE (t2);
653 if (TREE_CODE (mv1) != ARRAY_TYPE)
654 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655 if (TREE_CODE (mv2) != ARRAY_TYPE)
656 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657 target = composite_type (mv1, mv2);
659 /* For function types do not merge const qualifiers, but drop them
660 if used inconsistently. The middle-end uses these to mark const
661 and noreturn functions. */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
665 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
666 target_quals = (quals1 & quals2);
668 target_quals = (quals1 | quals2);
670 /* If the two named address spaces are different, determine the common
671 superset address space. This is guaranteed to exist due to the
672 assumption that comp_target_type returned non-zero. */
673 as1 = TYPE_ADDR_SPACE (pointed_to_1);
674 as2 = TYPE_ADDR_SPACE (pointed_to_2);
675 if (!addr_space_superset (as1, as2, &as_common))
678 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
680 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
681 return build_type_attribute_variant (t1, attributes);
684 /* Return the common type for two arithmetic types under the usual
685 arithmetic conversions. The default conversions have already been
686 applied, and enumerated types converted to their compatible integer
687 types. The resulting type is unqualified and has no attributes.
689 This is the type for the result of most arithmetic operations
690 if the operands have the given two types. */
693 c_common_type (tree t1, tree t2)
695 enum tree_code code1;
696 enum tree_code code2;
698 /* If one type is nonsense, use the other. */
699 if (t1 == error_mark_node)
701 if (t2 == error_mark_node)
704 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705 t1 = TYPE_MAIN_VARIANT (t1);
707 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708 t2 = TYPE_MAIN_VARIANT (t2);
710 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711 t1 = build_type_attribute_variant (t1, NULL_TREE);
713 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714 t2 = build_type_attribute_variant (t2, NULL_TREE);
716 /* Save time if the two types are the same. */
718 if (t1 == t2) return t1;
720 code1 = TREE_CODE (t1);
721 code2 = TREE_CODE (t2);
723 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
724 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725 || code1 == INTEGER_TYPE);
726 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
727 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728 || code2 == INTEGER_TYPE);
730 /* When one operand is a decimal float type, the other operand cannot be
731 a generic float type or a complex type. We also disallow vector types
733 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
736 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
738 error ("can%'t mix operands of decimal float and vector types");
739 return error_mark_node;
741 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
743 error ("can%'t mix operands of decimal float and complex types");
744 return error_mark_node;
746 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
748 error ("can%'t mix operands of decimal float and other float types");
749 return error_mark_node;
753 /* If one type is a vector type, return that type. (How the usual
754 arithmetic conversions apply to the vector types extension is not
755 precisely specified.) */
756 if (code1 == VECTOR_TYPE)
759 if (code2 == VECTOR_TYPE)
762 /* If one type is complex, form the common type of the non-complex
763 components, then make that complex. Use T1 or T2 if it is the
765 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
767 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
769 tree subtype = c_common_type (subtype1, subtype2);
771 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
773 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
776 return build_complex_type (subtype);
779 /* If only one is real, use it as the result. */
781 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
784 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
787 /* If both are real and either are decimal floating point types, use
788 the decimal floating point type with the greater precision. */
790 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
792 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794 return dfloat128_type_node;
795 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797 return dfloat64_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800 return dfloat32_type_node;
803 /* Deal with fixed-point types. */
804 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
806 unsigned int unsignedp = 0, satp = 0;
807 enum machine_mode m1, m2;
808 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
813 /* If one input type is saturating, the result type is saturating. */
814 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
817 /* If both fixed-point types are unsigned, the result type is unsigned.
818 When mixing fixed-point and integer types, follow the sign of the
820 Otherwise, the result type is signed. */
821 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t1))
825 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t2)))
829 /* The result type is signed. */
832 /* If the input type is unsigned, we need to convert to the
834 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
836 enum mode_class mclass = (enum mode_class) 0;
837 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
839 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
843 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
845 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
847 enum mode_class mclass = (enum mode_class) 0;
848 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
850 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
854 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
858 if (code1 == FIXED_POINT_TYPE)
860 fbit1 = GET_MODE_FBIT (m1);
861 ibit1 = GET_MODE_IBIT (m1);
866 /* Signed integers need to subtract one sign bit. */
867 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
870 if (code2 == FIXED_POINT_TYPE)
872 fbit2 = GET_MODE_FBIT (m2);
873 ibit2 = GET_MODE_IBIT (m2);
878 /* Signed integers need to subtract one sign bit. */
879 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
882 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
883 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
884 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
888 /* Both real or both integers; use the one with greater precision. */
890 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
892 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
895 /* Same precision. Prefer long longs to longs to ints when the
896 same precision, following the C99 rules on integer type rank
897 (which are equivalent to the C90 rules for C90 types). */
899 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901 return long_long_unsigned_type_node;
903 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
906 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907 return long_long_unsigned_type_node;
909 return long_long_integer_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914 return long_unsigned_type_node;
916 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
919 /* But preserve unsignedness from the other type,
920 since long cannot hold all the values of an unsigned int. */
921 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922 return long_unsigned_type_node;
924 return long_integer_type_node;
927 /* Likewise, prefer long double to double even if same size. */
928 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930 return long_double_type_node;
932 /* Otherwise prefer the unsigned one. */
934 if (TYPE_UNSIGNED (t1))
940 /* Wrapper around c_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions. ENUMERAL_TYPEs
942 are allowed here and are converted to their compatible integer types.
943 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944 preferably a non-Boolean type as the common type. */
946 common_type (tree t1, tree t2)
948 if (TREE_CODE (t1) == ENUMERAL_TYPE)
949 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950 if (TREE_CODE (t2) == ENUMERAL_TYPE)
951 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
953 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
954 if (TREE_CODE (t1) == BOOLEAN_TYPE
955 && TREE_CODE (t2) == BOOLEAN_TYPE)
956 return boolean_type_node;
958 /* If either type is BOOLEAN_TYPE, then return the other. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE)
961 if (TREE_CODE (t2) == BOOLEAN_TYPE)
964 return c_common_type (t1, t2);
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968 or various other operations. Return 2 if they are compatible
969 but a warning may be needed if you use them together. */
972 comptypes (tree type1, tree type2)
974 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
977 val = comptypes_internal (type1, type2, NULL, NULL);
978 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
983 /* Like comptypes, but if it returns non-zero because enum and int are
984 compatible, it sets *ENUM_AND_INT_P to true. */
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
992 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
998 /* Like comptypes, but if it returns nonzero for different types, it
999 sets *DIFFERENT_TYPES_P to true. */
1002 comptypes_check_different_types (tree type1, tree type2,
1003 bool *different_types_p)
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1008 val = comptypes_internal (type1, type2, NULL, different_types_p);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015 or various other operations. Return 2 if they are compatible
1016 but a warning may be needed if you use them together. If
1017 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1020 NULL, and the types are compatible but different enough not to be
1021 permitted in C1X typedef redeclarations, then this sets
1022 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023 false, but may or may not be set if the types are incompatible.
1024 This differs from comptypes, in that we don't free the seen
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029 bool *different_types_p)
1031 const_tree t1 = type1;
1032 const_tree t2 = type2;
1035 /* Suppress errors caused by previously reported errors. */
1037 if (t1 == t2 || !t1 || !t2
1038 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1041 /* If either type is the internal version of sizetype, return the
1042 language version. */
1043 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
1044 && TYPE_ORIG_SIZE_TYPE (t1))
1045 t1 = TYPE_ORIG_SIZE_TYPE (t1);
1047 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
1048 && TYPE_ORIG_SIZE_TYPE (t2))
1049 t2 = TYPE_ORIG_SIZE_TYPE (t2);
1052 /* Enumerated types are compatible with integer types, but this is
1053 not transitive: two enumerated types in the same translation unit
1054 are compatible with each other only if they are the same type. */
1056 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1058 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1059 if (TREE_CODE (t2) != VOID_TYPE)
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1067 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1069 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1070 if (TREE_CODE (t1) != VOID_TYPE)
1072 if (enum_and_int_p != NULL)
1073 *enum_and_int_p = true;
1074 if (different_types_p != NULL)
1075 *different_types_p = true;
1082 /* Different classes of types can't be compatible. */
1084 if (TREE_CODE (t1) != TREE_CODE (t2))
1087 /* Qualifiers must match. C99 6.7.3p9 */
1089 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1092 /* Allow for two different type nodes which have essentially the same
1093 definition. Note that we already checked for equality of the type
1094 qualifiers (just above). */
1096 if (TREE_CODE (t1) != ARRAY_TYPE
1097 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1100 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1101 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1107 switch (TREE_CODE (t1))
1110 /* Do not remove mode or aliasing information. */
1111 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1112 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1114 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1115 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1116 enum_and_int_p, different_types_p));
1120 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1126 tree d1 = TYPE_DOMAIN (t1);
1127 tree d2 = TYPE_DOMAIN (t2);
1128 bool d1_variable, d2_variable;
1129 bool d1_zero, d2_zero;
1132 /* Target types must match incl. qualifiers. */
1133 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1134 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1136 different_types_p)))
1139 if (different_types_p != NULL
1140 && (d1 == 0) != (d2 == 0))
1141 *different_types_p = true;
1142 /* Sizes must match unless one is missing or variable. */
1143 if (d1 == 0 || d2 == 0 || d1 == d2)
1146 d1_zero = !TYPE_MAX_VALUE (d1);
1147 d2_zero = !TYPE_MAX_VALUE (d2);
1149 d1_variable = (!d1_zero
1150 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1151 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1152 d2_variable = (!d2_zero
1153 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1154 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1155 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1156 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1158 if (different_types_p != NULL
1159 && d1_variable != d2_variable)
1160 *different_types_p = true;
1161 if (d1_variable || d2_variable)
1163 if (d1_zero && d2_zero)
1165 if (d1_zero || d2_zero
1166 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1167 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1176 if (val != 1 && !same_translation_unit_p (t1, t2))
1178 tree a1 = TYPE_ATTRIBUTES (t1);
1179 tree a2 = TYPE_ATTRIBUTES (t2);
1181 if (! attribute_list_contained (a1, a2)
1182 && ! attribute_list_contained (a2, a1))
1186 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1188 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1195 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1196 enum_and_int_p, different_types_p));
1202 return attrval == 2 && val == 1 ? 2 : val;
1205 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1206 their qualifiers, except for named address spaces. If the pointers point to
1207 different named addresses, then we must determine if one address space is a
1208 subset of the other. */
1211 comp_target_types (location_t location, tree ttl, tree ttr)
1214 tree mvl = TREE_TYPE (ttl);
1215 tree mvr = TREE_TYPE (ttr);
1216 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1217 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1218 addr_space_t as_common;
1219 bool enum_and_int_p;
1221 /* Fail if pointers point to incompatible address spaces. */
1222 if (!addr_space_superset (asl, asr, &as_common))
1225 /* Do not lose qualifiers on element types of array types that are
1226 pointer targets by taking their TYPE_MAIN_VARIANT. */
1227 if (TREE_CODE (mvl) != ARRAY_TYPE)
1228 mvl = TYPE_MAIN_VARIANT (mvl);
1229 if (TREE_CODE (mvr) != ARRAY_TYPE)
1230 mvr = TYPE_MAIN_VARIANT (mvr);
1231 enum_and_int_p = false;
1232 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1235 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1237 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1238 warning_at (location, OPT_Wc___compat,
1239 "pointer target types incompatible in C++");
1244 /* Subroutines of `comptypes'. */
1246 /* Determine whether two trees derive from the same translation unit.
1247 If the CONTEXT chain ends in a null, that tree's context is still
1248 being parsed, so if two trees have context chains ending in null,
1249 they're in the same translation unit. */
1251 same_translation_unit_p (const_tree t1, const_tree t2)
1253 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1254 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1256 case tcc_declaration:
1257 t1 = DECL_CONTEXT (t1); break;
1259 t1 = TYPE_CONTEXT (t1); break;
1260 case tcc_exceptional:
1261 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1262 default: gcc_unreachable ();
1265 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1266 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1268 case tcc_declaration:
1269 t2 = DECL_CONTEXT (t2); break;
1271 t2 = TYPE_CONTEXT (t2); break;
1272 case tcc_exceptional:
1273 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1274 default: gcc_unreachable ();
1280 /* Allocate the seen two types, assuming that they are compatible. */
1282 static struct tagged_tu_seen_cache *
1283 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1285 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1286 tu->next = tagged_tu_seen_base;
1290 tagged_tu_seen_base = tu;
1292 /* The C standard says that two structures in different translation
1293 units are compatible with each other only if the types of their
1294 fields are compatible (among other things). We assume that they
1295 are compatible until proven otherwise when building the cache.
1296 An example where this can occur is:
1301 If we are comparing this against a similar struct in another TU,
1302 and did not assume they were compatible, we end up with an infinite
1308 /* Free the seen types until we get to TU_TIL. */
1311 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1313 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1314 while (tu != tu_til)
1316 const struct tagged_tu_seen_cache *const tu1
1317 = (const struct tagged_tu_seen_cache *) tu;
1319 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1321 tagged_tu_seen_base = tu_til;
1324 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1325 compatible. If the two types are not the same (which has been
1326 checked earlier), this can only happen when multiple translation
1327 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1328 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1329 comptypes_internal. */
1332 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1333 bool *enum_and_int_p, bool *different_types_p)
1336 bool needs_warning = false;
1338 /* We have to verify that the tags of the types are the same. This
1339 is harder than it looks because this may be a typedef, so we have
1340 to go look at the original type. It may even be a typedef of a
1342 In the case of compiler-created builtin structs the TYPE_DECL
1343 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1344 while (TYPE_NAME (t1)
1345 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1346 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1347 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1349 while (TYPE_NAME (t2)
1350 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1351 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1352 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1354 /* C90 didn't have the requirement that the two tags be the same. */
1355 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1358 /* C90 didn't say what happened if one or both of the types were
1359 incomplete; we choose to follow C99 rules here, which is that they
1361 if (TYPE_SIZE (t1) == NULL
1362 || TYPE_SIZE (t2) == NULL)
1366 const struct tagged_tu_seen_cache * tts_i;
1367 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1368 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1372 switch (TREE_CODE (t1))
1376 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1377 /* Speed up the case where the type values are in the same order. */
1378 tree tv1 = TYPE_VALUES (t1);
1379 tree tv2 = TYPE_VALUES (t2);
1386 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1388 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1390 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1397 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1401 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1407 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1413 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1415 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1417 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1428 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1429 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1435 /* Speed up the common case where the fields are in the same order. */
1436 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1437 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1441 if (DECL_NAME (s1) != DECL_NAME (s2))
1443 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1444 enum_and_int_p, different_types_p);
1446 if (result != 1 && !DECL_NAME (s1))
1454 needs_warning = true;
1456 if (TREE_CODE (s1) == FIELD_DECL
1457 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1458 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1466 tu->val = needs_warning ? 2 : 1;
1470 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1474 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1475 if (DECL_NAME (s1) == DECL_NAME (s2))
1479 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1483 if (result != 1 && !DECL_NAME (s1))
1491 needs_warning = true;
1493 if (TREE_CODE (s1) == FIELD_DECL
1494 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1495 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1507 tu->val = needs_warning ? 2 : 10;
1513 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1515 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1517 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1520 if (TREE_CODE (s1) != TREE_CODE (s2)
1521 || DECL_NAME (s1) != DECL_NAME (s2))
1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1524 enum_and_int_p, different_types_p);
1528 needs_warning = true;
1530 if (TREE_CODE (s1) == FIELD_DECL
1531 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1532 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1538 tu->val = needs_warning ? 2 : 1;
1547 /* Return 1 if two function types F1 and F2 are compatible.
1548 If either type specifies no argument types,
1549 the other must specify a fixed number of self-promoting arg types.
1550 Otherwise, if one type specifies only the number of arguments,
1551 the other must specify that number of self-promoting arg types.
1552 Otherwise, the argument types must match.
1553 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1556 function_types_compatible_p (const_tree f1, const_tree f2,
1557 bool *enum_and_int_p, bool *different_types_p)
1560 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1565 ret1 = TREE_TYPE (f1);
1566 ret2 = TREE_TYPE (f2);
1568 /* 'volatile' qualifiers on a function's return type used to mean
1569 the function is noreturn. */
1570 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1571 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1572 if (TYPE_VOLATILE (ret1))
1573 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1574 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1575 if (TYPE_VOLATILE (ret2))
1576 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1577 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1578 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1582 args1 = TYPE_ARG_TYPES (f1);
1583 args2 = TYPE_ARG_TYPES (f2);
1585 if (different_types_p != NULL
1586 && (args1 == 0) != (args2 == 0))
1587 *different_types_p = true;
1589 /* An unspecified parmlist matches any specified parmlist
1590 whose argument types don't need default promotions. */
1594 if (!self_promoting_args_p (args2))
1596 /* If one of these types comes from a non-prototype fn definition,
1597 compare that with the other type's arglist.
1598 If they don't match, ask for a warning (but no error). */
1599 if (TYPE_ACTUAL_ARG_TYPES (f1)
1600 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1601 enum_and_int_p, different_types_p))
1607 if (!self_promoting_args_p (args1))
1609 if (TYPE_ACTUAL_ARG_TYPES (f2)
1610 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1611 enum_and_int_p, different_types_p))
1616 /* Both types have argument lists: compare them and propagate results. */
1617 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1619 return val1 != 1 ? val1 : val;
1622 /* Check two lists of types for compatibility, returning 0 for
1623 incompatible, 1 for compatible, or 2 for compatible with
1624 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1625 comptypes_internal. */
1628 type_lists_compatible_p (const_tree args1, const_tree args2,
1629 bool *enum_and_int_p, bool *different_types_p)
1631 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1637 tree a1, mv1, a2, mv2;
1638 if (args1 == 0 && args2 == 0)
1640 /* If one list is shorter than the other,
1641 they fail to match. */
1642 if (args1 == 0 || args2 == 0)
1644 mv1 = a1 = TREE_VALUE (args1);
1645 mv2 = a2 = TREE_VALUE (args2);
1646 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1647 mv1 = TYPE_MAIN_VARIANT (mv1);
1648 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1649 mv2 = TYPE_MAIN_VARIANT (mv2);
1650 /* A null pointer instead of a type
1651 means there is supposed to be an argument
1652 but nothing is specified about what type it has.
1653 So match anything that self-promotes. */
1654 if (different_types_p != NULL
1655 && (a1 == 0) != (a2 == 0))
1656 *different_types_p = true;
1659 if (c_type_promotes_to (a2) != a2)
1664 if (c_type_promotes_to (a1) != a1)
1667 /* If one of the lists has an error marker, ignore this arg. */
1668 else if (TREE_CODE (a1) == ERROR_MARK
1669 || TREE_CODE (a2) == ERROR_MARK)
1671 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1672 different_types_p)))
1674 if (different_types_p != NULL)
1675 *different_types_p = true;
1676 /* Allow wait (union {union wait *u; int *i} *)
1677 and wait (union wait *) to be compatible. */
1678 if (TREE_CODE (a1) == UNION_TYPE
1679 && (TYPE_NAME (a1) == 0
1680 || TYPE_TRANSPARENT_AGGR (a1))
1681 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1682 && tree_int_cst_equal (TYPE_SIZE (a1),
1686 for (memb = TYPE_FIELDS (a1);
1687 memb; memb = DECL_CHAIN (memb))
1689 tree mv3 = TREE_TYPE (memb);
1690 if (mv3 && mv3 != error_mark_node
1691 && TREE_CODE (mv3) != ARRAY_TYPE)
1692 mv3 = TYPE_MAIN_VARIANT (mv3);
1693 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1700 else if (TREE_CODE (a2) == UNION_TYPE
1701 && (TYPE_NAME (a2) == 0
1702 || TYPE_TRANSPARENT_AGGR (a2))
1703 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1704 && tree_int_cst_equal (TYPE_SIZE (a2),
1708 for (memb = TYPE_FIELDS (a2);
1709 memb; memb = DECL_CHAIN (memb))
1711 tree mv3 = TREE_TYPE (memb);
1712 if (mv3 && mv3 != error_mark_node
1713 && TREE_CODE (mv3) != ARRAY_TYPE)
1714 mv3 = TYPE_MAIN_VARIANT (mv3);
1715 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1726 /* comptypes said ok, but record if it said to warn. */
1730 args1 = TREE_CHAIN (args1);
1731 args2 = TREE_CHAIN (args2);
1735 /* Compute the size to increment a pointer by. */
1738 c_size_in_bytes (const_tree type)
1740 enum tree_code code = TREE_CODE (type);
1742 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1743 return size_one_node;
1745 if (!COMPLETE_OR_VOID_TYPE_P (type))
1747 error ("arithmetic on pointer to an incomplete type");
1748 return size_one_node;
1751 /* Convert in case a char is more than one unit. */
1752 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1753 size_int (TYPE_PRECISION (char_type_node)
1757 /* Return either DECL or its known constant value (if it has one). */
1760 decl_constant_value (tree decl)
1762 if (/* Don't change a variable array bound or initial value to a constant
1763 in a place where a variable is invalid. Note that DECL_INITIAL
1764 isn't valid for a PARM_DECL. */
1765 current_function_decl != 0
1766 && TREE_CODE (decl) != PARM_DECL
1767 && !TREE_THIS_VOLATILE (decl)
1768 && TREE_READONLY (decl)
1769 && DECL_INITIAL (decl) != 0
1770 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1771 /* This is invalid if initial value is not constant.
1772 If it has either a function call, a memory reference,
1773 or a variable, then re-evaluating it could give different results. */
1774 && TREE_CONSTANT (DECL_INITIAL (decl))
1775 /* Check for cases where this is sub-optimal, even though valid. */
1776 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1777 return DECL_INITIAL (decl);
1781 /* Convert the array expression EXP to a pointer. */
1783 array_to_pointer_conversion (location_t loc, tree exp)
1785 tree orig_exp = exp;
1786 tree type = TREE_TYPE (exp);
1788 tree restype = TREE_TYPE (type);
1791 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1793 STRIP_TYPE_NOPS (exp);
1795 if (TREE_NO_WARNING (orig_exp))
1796 TREE_NO_WARNING (exp) = 1;
1798 ptrtype = build_pointer_type (restype);
1800 if (TREE_CODE (exp) == INDIRECT_REF)
1801 return convert (ptrtype, TREE_OPERAND (exp, 0));
1803 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1804 return convert (ptrtype, adr);
1807 /* Convert the function expression EXP to a pointer. */
1809 function_to_pointer_conversion (location_t loc, tree exp)
1811 tree orig_exp = exp;
1813 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1815 STRIP_TYPE_NOPS (exp);
1817 if (TREE_NO_WARNING (orig_exp))
1818 TREE_NO_WARNING (exp) = 1;
1820 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1823 /* Mark EXP as read, not just set, for set but not used -Wunused
1824 warning purposes. */
1827 mark_exp_read (tree exp)
1829 switch (TREE_CODE (exp))
1833 DECL_READ_P (exp) = 1;
1842 mark_exp_read (TREE_OPERAND (exp, 0));
1845 case C_MAYBE_CONST_EXPR:
1846 mark_exp_read (TREE_OPERAND (exp, 1));
1853 /* Perform the default conversion of arrays and functions to pointers.
1854 Return the result of converting EXP. For any other expression, just
1857 LOC is the location of the expression. */
1860 default_function_array_conversion (location_t loc, struct c_expr exp)
1862 tree orig_exp = exp.value;
1863 tree type = TREE_TYPE (exp.value);
1864 enum tree_code code = TREE_CODE (type);
1870 bool not_lvalue = false;
1871 bool lvalue_array_p;
1873 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1874 || CONVERT_EXPR_P (exp.value))
1875 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1877 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1879 exp.value = TREE_OPERAND (exp.value, 0);
1882 if (TREE_NO_WARNING (orig_exp))
1883 TREE_NO_WARNING (exp.value) = 1;
1885 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1886 if (!flag_isoc99 && !lvalue_array_p)
1888 /* Before C99, non-lvalue arrays do not decay to pointers.
1889 Normally, using such an array would be invalid; but it can
1890 be used correctly inside sizeof or as a statement expression.
1891 Thus, do not give an error here; an error will result later. */
1895 exp.value = array_to_pointer_conversion (loc, exp.value);
1899 exp.value = function_to_pointer_conversion (loc, exp.value);
1909 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1911 mark_exp_read (exp.value);
1912 return default_function_array_conversion (loc, exp);
1915 /* EXP is an expression of integer type. Apply the integer promotions
1916 to it and return the promoted value. */
1919 perform_integral_promotions (tree exp)
1921 tree type = TREE_TYPE (exp);
1922 enum tree_code code = TREE_CODE (type);
1924 gcc_assert (INTEGRAL_TYPE_P (type));
1926 /* Normally convert enums to int,
1927 but convert wide enums to something wider. */
1928 if (code == ENUMERAL_TYPE)
1930 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1931 TYPE_PRECISION (integer_type_node)),
1932 ((TYPE_PRECISION (type)
1933 >= TYPE_PRECISION (integer_type_node))
1934 && TYPE_UNSIGNED (type)));
1936 return convert (type, exp);
1939 /* ??? This should no longer be needed now bit-fields have their
1941 if (TREE_CODE (exp) == COMPONENT_REF
1942 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1943 /* If it's thinner than an int, promote it like a
1944 c_promoting_integer_type_p, otherwise leave it alone. */
1945 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1946 TYPE_PRECISION (integer_type_node)))
1947 return convert (integer_type_node, exp);
1949 if (c_promoting_integer_type_p (type))
1951 /* Preserve unsignedness if not really getting any wider. */
1952 if (TYPE_UNSIGNED (type)
1953 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1954 return convert (unsigned_type_node, exp);
1956 return convert (integer_type_node, exp);
1963 /* Perform default promotions for C data used in expressions.
1964 Enumeral types or short or char are converted to int.
1965 In addition, manifest constants symbols are replaced by their values. */
1968 default_conversion (tree exp)
1971 tree type = TREE_TYPE (exp);
1972 enum tree_code code = TREE_CODE (type);
1975 mark_exp_read (exp);
1977 /* Functions and arrays have been converted during parsing. */
1978 gcc_assert (code != FUNCTION_TYPE);
1979 if (code == ARRAY_TYPE)
1982 /* Constants can be used directly unless they're not loadable. */
1983 if (TREE_CODE (exp) == CONST_DECL)
1984 exp = DECL_INITIAL (exp);
1986 /* Strip no-op conversions. */
1988 STRIP_TYPE_NOPS (exp);
1990 if (TREE_NO_WARNING (orig_exp))
1991 TREE_NO_WARNING (exp) = 1;
1993 if (code == VOID_TYPE)
1995 error ("void value not ignored as it ought to be");
1996 return error_mark_node;
1999 exp = require_complete_type (exp);
2000 if (exp == error_mark_node)
2001 return error_mark_node;
2003 promoted_type = targetm.promoted_type (type);
2005 return convert (promoted_type, exp);
2007 if (INTEGRAL_TYPE_P (type))
2008 return perform_integral_promotions (exp);
2013 /* Look up COMPONENT in a structure or union TYPE.
2015 If the component name is not found, returns NULL_TREE. Otherwise,
2016 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2017 stepping down the chain to the component, which is in the last
2018 TREE_VALUE of the list. Normally the list is of length one, but if
2019 the component is embedded within (nested) anonymous structures or
2020 unions, the list steps down the chain to the component. */
2023 lookup_field (tree type, tree component)
2027 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2028 to the field elements. Use a binary search on this array to quickly
2029 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2030 will always be set for structures which have many elements. */
2032 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2035 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2037 field = TYPE_FIELDS (type);
2039 top = TYPE_LANG_SPECIFIC (type)->s->len;
2040 while (top - bot > 1)
2042 half = (top - bot + 1) >> 1;
2043 field = field_array[bot+half];
2045 if (DECL_NAME (field) == NULL_TREE)
2047 /* Step through all anon unions in linear fashion. */
2048 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2050 field = field_array[bot++];
2051 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2052 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2054 tree anon = lookup_field (TREE_TYPE (field), component);
2057 return tree_cons (NULL_TREE, field, anon);
2061 /* Entire record is only anon unions. */
2065 /* Restart the binary search, with new lower bound. */
2069 if (DECL_NAME (field) == component)
2071 if (DECL_NAME (field) < component)
2077 if (DECL_NAME (field_array[bot]) == component)
2078 field = field_array[bot];
2079 else if (DECL_NAME (field) != component)
2084 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2086 if (DECL_NAME (field) == NULL_TREE
2087 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2088 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2090 tree anon = lookup_field (TREE_TYPE (field), component);
2093 return tree_cons (NULL_TREE, field, anon);
2096 if (DECL_NAME (field) == component)
2100 if (field == NULL_TREE)
2104 return tree_cons (NULL_TREE, field, NULL_TREE);
2107 /* Make an expression to refer to the COMPONENT field of structure or
2108 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2109 location of the COMPONENT_REF. */
2112 build_component_ref (location_t loc, tree datum, tree component)
2114 tree type = TREE_TYPE (datum);
2115 enum tree_code code = TREE_CODE (type);
2118 bool datum_lvalue = lvalue_p (datum);
2120 if (!objc_is_public (datum, component))
2121 return error_mark_node;
2123 /* See if there is a field or component with name COMPONENT. */
2125 if (code == RECORD_TYPE || code == UNION_TYPE)
2127 if (!COMPLETE_TYPE_P (type))
2129 c_incomplete_type_error (NULL_TREE, type);
2130 return error_mark_node;
2133 field = lookup_field (type, component);
2137 error_at (loc, "%qT has no member named %qE", type, component);
2138 return error_mark_node;
2141 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2142 This might be better solved in future the way the C++ front
2143 end does it - by giving the anonymous entities each a
2144 separate name and type, and then have build_component_ref
2145 recursively call itself. We can't do that here. */
2148 tree subdatum = TREE_VALUE (field);
2151 bool use_datum_quals;
2153 if (TREE_TYPE (subdatum) == error_mark_node)
2154 return error_mark_node;
2156 /* If this is an rvalue, it does not have qualifiers in C
2157 standard terms and we must avoid propagating such
2158 qualifiers down to a non-lvalue array that is then
2159 converted to a pointer. */
2160 use_datum_quals = (datum_lvalue
2161 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2163 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2164 if (use_datum_quals)
2165 quals |= TYPE_QUALS (TREE_TYPE (datum));
2166 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2168 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2170 SET_EXPR_LOCATION (ref, loc);
2171 if (TREE_READONLY (subdatum)
2172 || (use_datum_quals && TREE_READONLY (datum)))
2173 TREE_READONLY (ref) = 1;
2174 if (TREE_THIS_VOLATILE (subdatum)
2175 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2176 TREE_THIS_VOLATILE (ref) = 1;
2178 if (TREE_DEPRECATED (subdatum))
2179 warn_deprecated_use (subdatum, NULL_TREE);
2183 field = TREE_CHAIN (field);
2189 else if (code != ERROR_MARK)
2191 "request for member %qE in something not a structure or union",
2194 return error_mark_node;
2197 /* Given an expression PTR for a pointer, return an expression
2198 for the value pointed to.
2199 ERRORSTRING is the name of the operator to appear in error messages.
2201 LOC is the location to use for the generated tree. */
2204 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2206 tree pointer = default_conversion (ptr);
2207 tree type = TREE_TYPE (pointer);
2210 if (TREE_CODE (type) == POINTER_TYPE)
2212 if (CONVERT_EXPR_P (pointer)
2213 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2215 /* If a warning is issued, mark it to avoid duplicates from
2216 the backend. This only needs to be done at
2217 warn_strict_aliasing > 2. */
2218 if (warn_strict_aliasing > 2)
2219 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2220 type, TREE_OPERAND (pointer, 0)))
2221 TREE_NO_WARNING (pointer) = 1;
2224 if (TREE_CODE (pointer) == ADDR_EXPR
2225 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2226 == TREE_TYPE (type)))
2228 ref = TREE_OPERAND (pointer, 0);
2229 protected_set_expr_location (ref, loc);
2234 tree t = TREE_TYPE (type);
2236 ref = build1 (INDIRECT_REF, t, pointer);
2238 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2240 error_at (loc, "dereferencing pointer to incomplete type");
2241 return error_mark_node;
2243 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2244 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2246 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2247 so that we get the proper error message if the result is used
2248 to assign to. Also, &* is supposed to be a no-op.
2249 And ANSI C seems to specify that the type of the result
2250 should be the const type. */
2251 /* A de-reference of a pointer to const is not a const. It is valid
2252 to change it via some other pointer. */
2253 TREE_READONLY (ref) = TYPE_READONLY (t);
2254 TREE_SIDE_EFFECTS (ref)
2255 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2256 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2257 protected_set_expr_location (ref, loc);
2261 else if (TREE_CODE (pointer) != ERROR_MARK)
2264 case RO_ARRAY_INDEXING:
2266 "invalid type argument of array indexing (have %qT)",
2271 "invalid type argument of unary %<*%> (have %qT)",
2276 "invalid type argument of %<->%> (have %qT)",
2282 return error_mark_node;
2285 /* This handles expressions of the form "a[i]", which denotes
2288 This is logically equivalent in C to *(a+i), but we may do it differently.
2289 If A is a variable or a member, we generate a primitive ARRAY_REF.
2290 This avoids forcing the array out of registers, and can work on
2291 arrays that are not lvalues (for example, members of structures returned
2294 LOC is the location to use for the returned expression. */
2297 build_array_ref (location_t loc, tree array, tree index)
2300 bool swapped = false;
2301 if (TREE_TYPE (array) == error_mark_node
2302 || TREE_TYPE (index) == error_mark_node)
2303 return error_mark_node;
2305 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2306 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2309 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2310 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2312 error_at (loc, "subscripted value is neither array nor pointer");
2313 return error_mark_node;
2321 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2323 error_at (loc, "array subscript is not an integer");
2324 return error_mark_node;
2327 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2329 error_at (loc, "subscripted value is pointer to function");
2330 return error_mark_node;
2333 /* ??? Existing practice has been to warn only when the char
2334 index is syntactically the index, not for char[array]. */
2336 warn_array_subscript_with_type_char (index);
2338 /* Apply default promotions *after* noticing character types. */
2339 index = default_conversion (index);
2341 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2343 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2347 /* An array that is indexed by a non-constant
2348 cannot be stored in a register; we must be able to do
2349 address arithmetic on its address.
2350 Likewise an array of elements of variable size. */
2351 if (TREE_CODE (index) != INTEGER_CST
2352 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2353 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2355 if (!c_mark_addressable (array))
2356 return error_mark_node;
2358 /* An array that is indexed by a constant value which is not within
2359 the array bounds cannot be stored in a register either; because we
2360 would get a crash in store_bit_field/extract_bit_field when trying
2361 to access a non-existent part of the register. */
2362 if (TREE_CODE (index) == INTEGER_CST
2363 && TYPE_DOMAIN (TREE_TYPE (array))
2364 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2366 if (!c_mark_addressable (array))
2367 return error_mark_node;
2373 while (TREE_CODE (foo) == COMPONENT_REF)
2374 foo = TREE_OPERAND (foo, 0);
2375 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2376 pedwarn (loc, OPT_pedantic,
2377 "ISO C forbids subscripting %<register%> array");
2378 else if (!flag_isoc99 && !lvalue_p (foo))
2379 pedwarn (loc, OPT_pedantic,
2380 "ISO C90 forbids subscripting non-lvalue array");
2383 type = TREE_TYPE (TREE_TYPE (array));
2384 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2385 /* Array ref is const/volatile if the array elements are
2386 or if the array is. */
2387 TREE_READONLY (rval)
2388 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2389 | TREE_READONLY (array));
2390 TREE_SIDE_EFFECTS (rval)
2391 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2392 | TREE_SIDE_EFFECTS (array));
2393 TREE_THIS_VOLATILE (rval)
2394 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2395 /* This was added by rms on 16 Nov 91.
2396 It fixes vol struct foo *a; a->elts[1]
2397 in an inline function.
2398 Hope it doesn't break something else. */
2399 | TREE_THIS_VOLATILE (array));
2400 ret = require_complete_type (rval);
2401 protected_set_expr_location (ret, loc);
2406 tree ar = default_conversion (array);
2408 if (ar == error_mark_node)
2411 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2412 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2414 return build_indirect_ref
2415 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2420 /* Build an external reference to identifier ID. FUN indicates
2421 whether this will be used for a function call. LOC is the source
2422 location of the identifier. This sets *TYPE to the type of the
2423 identifier, which is not the same as the type of the returned value
2424 for CONST_DECLs defined as enum constants. If the type of the
2425 identifier is not available, *TYPE is set to NULL. */
2427 build_external_ref (location_t loc, tree id, int fun, tree *type)
2430 tree decl = lookup_name (id);
2432 /* In Objective-C, an instance variable (ivar) may be preferred to
2433 whatever lookup_name() found. */
2434 decl = objc_lookup_ivar (decl, id);
2437 if (decl && decl != error_mark_node)
2440 *type = TREE_TYPE (ref);
2443 /* Implicit function declaration. */
2444 ref = implicitly_declare (loc, id);
2445 else if (decl == error_mark_node)
2446 /* Don't complain about something that's already been
2447 complained about. */
2448 return error_mark_node;
2451 undeclared_variable (loc, id);
2452 return error_mark_node;
2455 if (TREE_TYPE (ref) == error_mark_node)
2456 return error_mark_node;
2458 if (TREE_DEPRECATED (ref))
2459 warn_deprecated_use (ref, NULL_TREE);
2461 /* Recursive call does not count as usage. */
2462 if (ref != current_function_decl)
2464 TREE_USED (ref) = 1;
2467 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2469 if (!in_sizeof && !in_typeof)
2470 C_DECL_USED (ref) = 1;
2471 else if (DECL_INITIAL (ref) == 0
2472 && DECL_EXTERNAL (ref)
2473 && !TREE_PUBLIC (ref))
2474 record_maybe_used_decl (ref);
2477 if (TREE_CODE (ref) == CONST_DECL)
2479 used_types_insert (TREE_TYPE (ref));
2482 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2483 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2485 warning_at (loc, OPT_Wc___compat,
2486 ("enum constant defined in struct or union "
2487 "is not visible in C++"));
2488 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2491 ref = DECL_INITIAL (ref);
2492 TREE_CONSTANT (ref) = 1;
2494 else if (current_function_decl != 0
2495 && !DECL_FILE_SCOPE_P (current_function_decl)
2496 && (TREE_CODE (ref) == VAR_DECL
2497 || TREE_CODE (ref) == PARM_DECL
2498 || TREE_CODE (ref) == FUNCTION_DECL))
2500 tree context = decl_function_context (ref);
2502 if (context != 0 && context != current_function_decl)
2503 DECL_NONLOCAL (ref) = 1;
2505 /* C99 6.7.4p3: An inline definition of a function with external
2506 linkage ... shall not contain a reference to an identifier with
2507 internal linkage. */
2508 else if (current_function_decl != 0
2509 && DECL_DECLARED_INLINE_P (current_function_decl)
2510 && DECL_EXTERNAL (current_function_decl)
2511 && VAR_OR_FUNCTION_DECL_P (ref)
2512 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2513 && ! TREE_PUBLIC (ref)
2514 && DECL_CONTEXT (ref) != current_function_decl)
2515 record_inline_static (loc, current_function_decl, ref,
2521 /* Record details of decls possibly used inside sizeof or typeof. */
2522 struct maybe_used_decl
2526 /* The level seen at (in_sizeof + in_typeof). */
2528 /* The next one at this level or above, or NULL. */
2529 struct maybe_used_decl *next;
2532 static struct maybe_used_decl *maybe_used_decls;
2534 /* Record that DECL, an undefined static function reference seen
2535 inside sizeof or typeof, might be used if the operand of sizeof is
2536 a VLA type or the operand of typeof is a variably modified
2540 record_maybe_used_decl (tree decl)
2542 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2544 t->level = in_sizeof + in_typeof;
2545 t->next = maybe_used_decls;
2546 maybe_used_decls = t;
2549 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2550 USED is false, just discard them. If it is true, mark them used
2551 (if no longer inside sizeof or typeof) or move them to the next
2552 level up (if still inside sizeof or typeof). */
2555 pop_maybe_used (bool used)
2557 struct maybe_used_decl *p = maybe_used_decls;
2558 int cur_level = in_sizeof + in_typeof;
2559 while (p && p->level > cur_level)
2564 C_DECL_USED (p->decl) = 1;
2566 p->level = cur_level;
2570 if (!used || cur_level == 0)
2571 maybe_used_decls = p;
2574 /* Return the result of sizeof applied to EXPR. */
2577 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2580 if (expr.value == error_mark_node)
2582 ret.value = error_mark_node;
2583 ret.original_code = ERROR_MARK;
2584 ret.original_type = NULL;
2585 pop_maybe_used (false);
2589 bool expr_const_operands = true;
2590 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2591 &expr_const_operands);
2592 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2593 ret.original_code = ERROR_MARK;
2594 ret.original_type = NULL;
2595 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2597 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2598 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2599 folded_expr, ret.value);
2600 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2601 SET_EXPR_LOCATION (ret.value, loc);
2603 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2608 /* Return the result of sizeof applied to T, a structure for the type
2609 name passed to sizeof (rather than the type itself). LOC is the
2610 location of the original expression. */
2613 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2617 tree type_expr = NULL_TREE;
2618 bool type_expr_const = true;
2619 type = groktypename (t, &type_expr, &type_expr_const);
2620 ret.value = c_sizeof (loc, type);
2621 ret.original_code = ERROR_MARK;
2622 ret.original_type = NULL;
2623 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2624 && c_vla_type_p (type))
2626 /* If the type is a [*] array, it is a VLA but is represented as
2627 having a size of zero. In such a case we must ensure that
2628 the result of sizeof does not get folded to a constant by
2629 c_fully_fold, because if the size is evaluated the result is
2630 not constant and so constraints on zero or negative size
2631 arrays must not be applied when this sizeof call is inside
2632 another array declarator. */
2634 type_expr = integer_zero_node;
2635 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2636 type_expr, ret.value);
2637 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2639 pop_maybe_used (type != error_mark_node
2640 ? C_TYPE_VARIABLE_SIZE (type) : false);
2644 /* Build a function call to function FUNCTION with parameters PARAMS.
2645 The function call is at LOC.
2646 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2647 TREE_VALUE of each node is a parameter-expression.
2648 FUNCTION's data type may be a function type or a pointer-to-function. */
2651 build_function_call (location_t loc, tree function, tree params)
2656 vec = VEC_alloc (tree, gc, list_length (params));
2657 for (; params; params = TREE_CHAIN (params))
2658 VEC_quick_push (tree, vec, TREE_VALUE (params));
2659 ret = build_function_call_vec (loc, function, vec, NULL);
2660 VEC_free (tree, gc, vec);
2664 /* Build a function call to function FUNCTION with parameters PARAMS.
2665 ORIGTYPES, if not NULL, is a vector of types; each element is
2666 either NULL or the original type of the corresponding element in
2667 PARAMS. The original type may differ from TREE_TYPE of the
2668 parameter for enums. FUNCTION's data type may be a function type
2669 or pointer-to-function. This function changes the elements of
2673 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2674 VEC(tree,gc) *origtypes)
2676 tree fntype, fundecl = 0;
2677 tree name = NULL_TREE, result;
2683 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2684 STRIP_TYPE_NOPS (function);
2686 /* Convert anything with function type to a pointer-to-function. */
2687 if (TREE_CODE (function) == FUNCTION_DECL)
2689 /* Implement type-directed function overloading for builtins.
2690 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2691 handle all the type checking. The result is a complete expression
2692 that implements this function call. */
2693 tem = resolve_overloaded_builtin (loc, function, params);
2697 name = DECL_NAME (function);
2700 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2701 function = function_to_pointer_conversion (loc, function);
2703 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2704 expressions, like those used for ObjC messenger dispatches. */
2705 if (!VEC_empty (tree, params))
2706 function = objc_rewrite_function_call (function,
2707 VEC_index (tree, params, 0));
2709 function = c_fully_fold (function, false, NULL);
2711 fntype = TREE_TYPE (function);
2713 if (TREE_CODE (fntype) == ERROR_MARK)
2714 return error_mark_node;
2716 if (!(TREE_CODE (fntype) == POINTER_TYPE
2717 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2719 error_at (loc, "called object %qE is not a function", function);
2720 return error_mark_node;
2723 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2724 current_function_returns_abnormally = 1;
2726 /* fntype now gets the type of function pointed to. */
2727 fntype = TREE_TYPE (fntype);
2729 /* Convert the parameters to the types declared in the
2730 function prototype, or apply default promotions. */
2732 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2735 return error_mark_node;
2737 /* Check that the function is called through a compatible prototype.
2738 If it is not, replace the call by a trap, wrapped up in a compound
2739 expression if necessary. This has the nice side-effect to prevent
2740 the tree-inliner from generating invalid assignment trees which may
2741 blow up in the RTL expander later. */
2742 if (CONVERT_EXPR_P (function)
2743 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2744 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2745 && !comptypes (fntype, TREE_TYPE (tem)))
2747 tree return_type = TREE_TYPE (fntype);
2748 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2752 /* This situation leads to run-time undefined behavior. We can't,
2753 therefore, simply error unless we can prove that all possible
2754 executions of the program must execute the code. */
2755 if (warning_at (loc, 0, "function called through a non-compatible type"))
2756 /* We can, however, treat "undefined" any way we please.
2757 Call abort to encourage the user to fix the program. */
2758 inform (loc, "if this code is reached, the program will abort");
2759 /* Before the abort, allow the function arguments to exit or
2761 for (i = 0; i < nargs; i++)
2762 trap = build2 (COMPOUND_EXPR, void_type_node,
2763 VEC_index (tree, params, i), trap);
2765 if (VOID_TYPE_P (return_type))
2767 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2769 "function with qualified void return type called");
2776 if (AGGREGATE_TYPE_P (return_type))
2777 rhs = build_compound_literal (loc, return_type,
2778 build_constructor (return_type, 0),
2781 rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2783 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2788 argarray = VEC_address (tree, params);
2790 /* Check that arguments to builtin functions match the expectations. */
2792 && DECL_BUILT_IN (fundecl)
2793 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2794 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2795 return error_mark_node;
2797 /* Check that the arguments to the function are valid. */
2798 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2799 TYPE_ARG_TYPES (fntype));
2801 if (name != NULL_TREE
2802 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2804 if (require_constant_value)
2806 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2807 function, nargs, argarray);
2809 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2810 function, nargs, argarray);
2811 if (TREE_CODE (result) == NOP_EXPR
2812 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2813 STRIP_TYPE_NOPS (result);
2816 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2817 function, nargs, argarray);
2819 if (VOID_TYPE_P (TREE_TYPE (result)))
2821 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2823 "function with qualified void return type called");
2826 return require_complete_type (result);
2829 /* Convert the argument expressions in the vector VALUES
2830 to the types in the list TYPELIST.
2832 If TYPELIST is exhausted, or when an element has NULL as its type,
2833 perform the default conversions.
2835 ORIGTYPES is the original types of the expressions in VALUES. This
2836 holds the type of enum values which have been converted to integral
2837 types. It may be NULL.
2839 FUNCTION is a tree for the called function. It is used only for
2840 error messages, where it is formatted with %qE.
2842 This is also where warnings about wrong number of args are generated.
2844 Returns the actual number of arguments processed (which may be less
2845 than the length of VALUES in some error situations), or -1 on
2849 convert_arguments (tree typelist, VEC(tree,gc) *values,
2850 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2853 unsigned int parmnum;
2854 bool error_args = false;
2855 const bool type_generic = fundecl
2856 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2857 bool type_generic_remove_excess_precision = false;
2860 /* Change pointer to function to the function itself for
2862 if (TREE_CODE (function) == ADDR_EXPR
2863 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2864 function = TREE_OPERAND (function, 0);
2866 /* Handle an ObjC selector specially for diagnostics. */
2867 selector = objc_message_selector ();
2869 /* For type-generic built-in functions, determine whether excess
2870 precision should be removed (classification) or not
2873 && DECL_BUILT_IN (fundecl)
2874 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2876 switch (DECL_FUNCTION_CODE (fundecl))
2878 case BUILT_IN_ISFINITE:
2879 case BUILT_IN_ISINF:
2880 case BUILT_IN_ISINF_SIGN:
2881 case BUILT_IN_ISNAN:
2882 case BUILT_IN_ISNORMAL:
2883 case BUILT_IN_FPCLASSIFY:
2884 type_generic_remove_excess_precision = true;
2888 type_generic_remove_excess_precision = false;
2893 /* Scan the given expressions and types, producing individual
2894 converted arguments. */
2896 for (typetail = typelist, parmnum = 0;
2897 VEC_iterate (tree, values, parmnum, val);
2900 tree type = typetail ? TREE_VALUE (typetail) : 0;
2901 tree valtype = TREE_TYPE (val);
2902 tree rname = function;
2903 int argnum = parmnum + 1;
2904 const char *invalid_func_diag;
2905 bool excess_precision = false;
2909 if (type == void_type_node)
2911 error_at (input_location,
2912 "too many arguments to function %qE", function);
2913 if (fundecl && !DECL_BUILT_IN (fundecl))
2914 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2918 if (selector && argnum > 2)
2924 npc = null_pointer_constant_p (val);
2926 /* If there is excess precision and a prototype, convert once to
2927 the required type rather than converting via the semantic
2928 type. Likewise without a prototype a float value represented
2929 as long double should be converted once to double. But for
2930 type-generic classification functions excess precision must
2932 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2933 && (type || !type_generic || !type_generic_remove_excess_precision))
2935 val = TREE_OPERAND (val, 0);
2936 excess_precision = true;
2938 val = c_fully_fold (val, false, NULL);
2939 STRIP_TYPE_NOPS (val);
2941 val = require_complete_type (val);
2945 /* Formal parm type is specified by a function prototype. */
2947 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2949 error ("type of formal parameter %d is incomplete", parmnum + 1);
2956 /* Optionally warn about conversions that
2957 differ from the default conversions. */
2958 if (warn_traditional_conversion || warn_traditional)
2960 unsigned int formal_prec = TYPE_PRECISION (type);
2962 if (INTEGRAL_TYPE_P (type)
2963 && TREE_CODE (valtype) == REAL_TYPE)
2964 warning (0, "passing argument %d of %qE as integer "
2965 "rather than floating due to prototype",
2967 if (INTEGRAL_TYPE_P (type)
2968 && TREE_CODE (valtype) == COMPLEX_TYPE)
2969 warning (0, "passing argument %d of %qE as integer "
2970 "rather than complex due to prototype",
2972 else if (TREE_CODE (type) == COMPLEX_TYPE
2973 && TREE_CODE (valtype) == REAL_TYPE)
2974 warning (0, "passing argument %d of %qE as complex "
2975 "rather than floating due to prototype",
2977 else if (TREE_CODE (type) == REAL_TYPE
2978 && INTEGRAL_TYPE_P (valtype))
2979 warning (0, "passing argument %d of %qE as floating "
2980 "rather than integer due to prototype",
2982 else if (TREE_CODE (type) == COMPLEX_TYPE
2983 && INTEGRAL_TYPE_P (valtype))
2984 warning (0, "passing argument %d of %qE as complex "
2985 "rather than integer due to prototype",
2987 else if (TREE_CODE (type) == REAL_TYPE
2988 && TREE_CODE (valtype) == COMPLEX_TYPE)
2989 warning (0, "passing argument %d of %qE as floating "
2990 "rather than complex due to prototype",
2992 /* ??? At some point, messages should be written about
2993 conversions between complex types, but that's too messy
2995 else if (TREE_CODE (type) == REAL_TYPE
2996 && TREE_CODE (valtype) == REAL_TYPE)
2998 /* Warn if any argument is passed as `float',
2999 since without a prototype it would be `double'. */
3000 if (formal_prec == TYPE_PRECISION (float_type_node)
3001 && type != dfloat32_type_node)
3002 warning (0, "passing argument %d of %qE as %<float%> "
3003 "rather than %<double%> due to prototype",
3006 /* Warn if mismatch between argument and prototype
3007 for decimal float types. Warn of conversions with
3008 binary float types and of precision narrowing due to
3010 else if (type != valtype
3011 && (type == dfloat32_type_node
3012 || type == dfloat64_type_node
3013 || type == dfloat128_type_node
3014 || valtype == dfloat32_type_node
3015 || valtype == dfloat64_type_node
3016 || valtype == dfloat128_type_node)
3018 <= TYPE_PRECISION (valtype)
3019 || (type == dfloat128_type_node
3021 != dfloat64_type_node
3023 != dfloat32_type_node)))
3024 || (type == dfloat64_type_node
3026 != dfloat32_type_node))))
3027 warning (0, "passing argument %d of %qE as %qT "
3028 "rather than %qT due to prototype",
3029 argnum, rname, type, valtype);
3032 /* Detect integer changing in width or signedness.
3033 These warnings are only activated with
3034 -Wtraditional-conversion, not with -Wtraditional. */
3035 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3036 && INTEGRAL_TYPE_P (valtype))
3038 tree would_have_been = default_conversion (val);
3039 tree type1 = TREE_TYPE (would_have_been);
3041 if (TREE_CODE (type) == ENUMERAL_TYPE
3042 && (TYPE_MAIN_VARIANT (type)
3043 == TYPE_MAIN_VARIANT (valtype)))
3044 /* No warning if function asks for enum
3045 and the actual arg is that enum type. */
3047 else if (formal_prec != TYPE_PRECISION (type1))
3048 warning (OPT_Wtraditional_conversion,
3049 "passing argument %d of %qE "
3050 "with different width due to prototype",
3052 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3054 /* Don't complain if the formal parameter type
3055 is an enum, because we can't tell now whether
3056 the value was an enum--even the same enum. */
3057 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3059 else if (TREE_CODE (val) == INTEGER_CST
3060 && int_fits_type_p (val, type))
3061 /* Change in signedness doesn't matter
3062 if a constant value is unaffected. */
3064 /* If the value is extended from a narrower
3065 unsigned type, it doesn't matter whether we
3066 pass it as signed or unsigned; the value
3067 certainly is the same either way. */
3068 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3069 && TYPE_UNSIGNED (valtype))
3071 else if (TYPE_UNSIGNED (type))
3072 warning (OPT_Wtraditional_conversion,
3073 "passing argument %d of %qE "
3074 "as unsigned due to prototype",
3077 warning (OPT_Wtraditional_conversion,
3078 "passing argument %d of %qE "
3079 "as signed due to prototype", argnum, rname);
3083 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3084 sake of better warnings from convert_and_check. */
3085 if (excess_precision)
3086 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3087 origtype = (origtypes == NULL
3089 : VEC_index (tree, origtypes, parmnum));
3090 parmval = convert_for_assignment (input_location, type, val,
3091 origtype, ic_argpass, npc,
3095 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3096 && INTEGRAL_TYPE_P (type)
3097 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3098 parmval = default_conversion (parmval);
3101 else if (TREE_CODE (valtype) == REAL_TYPE
3102 && (TYPE_PRECISION (valtype)
3103 < TYPE_PRECISION (double_type_node))
3104 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3110 /* Convert `float' to `double'. */
3111 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3112 warning (OPT_Wdouble_promotion,
3113 "implicit conversion from %qT to %qT when passing "
3114 "argument to function",
3115 valtype, double_type_node);
3116 parmval = convert (double_type_node, val);
3119 else if (excess_precision && !type_generic)
3120 /* A "double" argument with excess precision being passed
3121 without a prototype or in variable arguments. */
3122 parmval = convert (valtype, val);
3123 else if ((invalid_func_diag =
3124 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3126 error (invalid_func_diag);
3130 /* Convert `short' and `char' to full-size `int'. */
3131 parmval = default_conversion (val);
3133 VEC_replace (tree, values, parmnum, parmval);
3134 if (parmval == error_mark_node)
3138 typetail = TREE_CHAIN (typetail);
3141 gcc_assert (parmnum == VEC_length (tree, values));
3143 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3145 error_at (input_location,
3146 "too few arguments to function %qE", function);
3147 if (fundecl && !DECL_BUILT_IN (fundecl))
3148 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3152 return error_args ? -1 : (int) parmnum;
3155 /* This is the entry point used by the parser to build unary operators
3156 in the input. CODE, a tree_code, specifies the unary operator, and
3157 ARG is the operand. For unary plus, the C parser currently uses
3158 CONVERT_EXPR for code.
3160 LOC is the location to use for the tree generated.
3164 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3166 struct c_expr result;
3168 result.value = build_unary_op (loc, code, arg.value, 0);
3169 result.original_code = code;
3170 result.original_type = NULL;
3172 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3173 overflow_warning (loc, result.value);
3178 /* This is the entry point used by the parser to build binary operators
3179 in the input. CODE, a tree_code, specifies the binary operator, and
3180 ARG1 and ARG2 are the operands. In addition to constructing the
3181 expression, we check for operands that were written with other binary
3182 operators in a way that is likely to confuse the user.
3184 LOCATION is the location of the binary operator. */
3187 parser_build_binary_op (location_t location, enum tree_code code,
3188 struct c_expr arg1, struct c_expr arg2)
3190 struct c_expr result;
3192 enum tree_code code1 = arg1.original_code;
3193 enum tree_code code2 = arg2.original_code;
3194 tree type1 = (arg1.original_type
3195 ? arg1.original_type
3196 : TREE_TYPE (arg1.value));
3197 tree type2 = (arg2.original_type
3198 ? arg2.original_type
3199 : TREE_TYPE (arg2.value));
3201 result.value = build_binary_op (location, code,
3202 arg1.value, arg2.value, 1);
3203 result.original_code = code;
3204 result.original_type = NULL;
3206 if (TREE_CODE (result.value) == ERROR_MARK)
3209 if (location != UNKNOWN_LOCATION)
3210 protected_set_expr_location (result.value, location);
3212 /* Check for cases such as x+y<<z which users are likely
3214 if (warn_parentheses)
3215 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3217 if (warn_logical_op)
3218 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3219 code1, arg1.value, code2, arg2.value);
3221 /* Warn about comparisons against string literals, with the exception
3222 of testing for equality or inequality of a string literal with NULL. */
3223 if (code == EQ_EXPR || code == NE_EXPR)
3225 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3226 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3227 warning_at (location, OPT_Waddress,
3228 "comparison with string literal results in unspecified behavior");
3230 else if (TREE_CODE_CLASS (code) == tcc_comparison
3231 && (code1 == STRING_CST || code2 == STRING_CST))
3232 warning_at (location, OPT_Waddress,
3233 "comparison with string literal results in unspecified behavior");
3235 if (TREE_OVERFLOW_P (result.value)
3236 && !TREE_OVERFLOW_P (arg1.value)
3237 && !TREE_OVERFLOW_P (arg2.value))
3238 overflow_warning (location, result.value);
3240 /* Warn about comparisons of different enum types. */
3241 if (warn_enum_compare
3242 && TREE_CODE_CLASS (code) == tcc_comparison
3243 && TREE_CODE (type1) == ENUMERAL_TYPE
3244 && TREE_CODE (type2) == ENUMERAL_TYPE
3245 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3246 warning_at (location, OPT_Wenum_compare,
3247 "comparison between %qT and %qT",
3253 /* Return a tree for the difference of pointers OP0 and OP1.
3254 The resulting tree has type int. */
3257 pointer_diff (location_t loc, tree op0, tree op1)
3259 tree restype = ptrdiff_type_node;
3260 tree result, inttype;
3262 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3263 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3264 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3265 tree con0, con1, lit0, lit1;
3266 tree orig_op1 = op1;
3268 /* If the operands point into different address spaces, we need to
3269 explicitly convert them to pointers into the common address space
3270 before we can subtract the numerical address values. */
3273 addr_space_t as_common;
3276 /* Determine the common superset address space. This is guaranteed
3277 to exist because the caller verified that comp_target_types
3278 returned non-zero. */
3279 if (!addr_space_superset (as0, as1, &as_common))
3282 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3283 op0 = convert (common_type, op0);
3284 op1 = convert (common_type, op1);
3287 /* Determine integer type to perform computations in. This will usually
3288 be the same as the result type (ptrdiff_t), but may need to be a wider
3289 type if pointers for the address space are wider than ptrdiff_t. */
3290 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3291 inttype = lang_hooks.types.type_for_size
3292 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3297 if (TREE_CODE (target_type) == VOID_TYPE)
3298 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3299 "pointer of type %<void *%> used in subtraction");
3300 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3301 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3302 "pointer to a function used in subtraction");
3304 /* If the conversion to ptrdiff_type does anything like widening or
3305 converting a partial to an integral mode, we get a convert_expression
3306 that is in the way to do any simplifications.
3307 (fold-const.c doesn't know that the extra bits won't be needed.
3308 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3309 different mode in place.)
3310 So first try to find a common term here 'by hand'; we want to cover
3311 at least the cases that occur in legal static initializers. */
3312 if (CONVERT_EXPR_P (op0)
3313 && (TYPE_PRECISION (TREE_TYPE (op0))
3314 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3315 con0 = TREE_OPERAND (op0, 0);
3318 if (CONVERT_EXPR_P (op1)
3319 && (TYPE_PRECISION (TREE_TYPE (op1))
3320 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3321 con1 = TREE_OPERAND (op1, 0);
3325 if (TREE_CODE (con0) == PLUS_EXPR)
3327 lit0 = TREE_OPERAND (con0, 1);
3328 con0 = TREE_OPERAND (con0, 0);
3331 lit0 = integer_zero_node;
3333 if (TREE_CODE (con1) == PLUS_EXPR)
3335 lit1 = TREE_OPERAND (con1, 1);
3336 con1 = TREE_OPERAND (con1, 0);
3339 lit1 = integer_zero_node;
3341 if (operand_equal_p (con0, con1, 0))
3348 /* First do the subtraction as integers;
3349 then drop through to build the divide operator.
3350 Do not do default conversions on the minus operator
3351 in case restype is a short type. */
3353 op0 = build_binary_op (loc,
3354 MINUS_EXPR, convert (inttype, op0),
3355 convert (inttype, op1), 0);
3356 /* This generates an error if op1 is pointer to incomplete type. */
3357 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3358 error_at (loc, "arithmetic on pointer to an incomplete type");
3360 /* This generates an error if op0 is pointer to incomplete type. */
3361 op1 = c_size_in_bytes (target_type);
3363 /* Divide by the size, in easiest possible way. */
3364 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3365 op0, convert (inttype, op1));
3367 /* Convert to final result type if necessary. */
3368 return convert (restype, result);
3371 /* Construct and perhaps optimize a tree representation
3372 for a unary operation. CODE, a tree_code, specifies the operation
3373 and XARG is the operand.
3374 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3375 the default promotions (such as from short to int).
3376 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3377 allows non-lvalues; this is only used to handle conversion of non-lvalue
3378 arrays to pointers in C99.
3380 LOCATION is the location of the operator. */
3383 build_unary_op (location_t location,
3384 enum tree_code code, tree xarg, int flag)
3386 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3389 enum tree_code typecode;
3391 tree ret = error_mark_node;
3392 tree eptype = NULL_TREE;
3393 int noconvert = flag;
3394 const char *invalid_op_diag;
3397 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3399 arg = remove_c_maybe_const_expr (arg);
3401 if (code != ADDR_EXPR)
3402 arg = require_complete_type (arg);
3404 typecode = TREE_CODE (TREE_TYPE (arg));
3405 if (typecode == ERROR_MARK)
3406 return error_mark_node;
3407 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3408 typecode = INTEGER_TYPE;
3410 if ((invalid_op_diag
3411 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3413 error_at (location, invalid_op_diag);
3414 return error_mark_node;
3417 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3419 eptype = TREE_TYPE (arg);
3420 arg = TREE_OPERAND (arg, 0);
3426 /* This is used for unary plus, because a CONVERT_EXPR
3427 is enough to prevent anybody from looking inside for
3428 associativity, but won't generate any code. */
3429 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3430 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE