bool useless_type_conversion_p (tree outer_type, tree inner_type) { /* Do the following before stripping toplevel qualifiers. */ if (POINTER_TYPE_P (inner_type) && POINTER_TYPE_P (outer_type)) { /* Do not lose casts between pointers to different address spaces. */ if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))) return false; /* Do not lose casts to function pointer types. */ if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE) && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE)) return false; } /* From now on qualifiers on value types do not matter. */ inner_type = TYPE_MAIN_VARIANT (inner_type); outer_type = TYPE_MAIN_VARIANT (outer_type); if (inner_type == outer_type) return true; /* Changes in machine mode are never useless conversions because the RTL middle-end expects explicit conversions between modes. */ if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)) return false; /* If both the inner and outer types are integral types, then the conversion is not necessary if they have the same mode and signedness and precision, and both or neither are boolean. */ if (INTEGRAL_TYPE_P (inner_type) && INTEGRAL_TYPE_P (outer_type)) { /* Preserve changes in signedness or precision. */ if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type) || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type)) return false; /* Preserve conversions to/from BOOLEAN_TYPE if types are not of precision one. */ if (((TREE_CODE (inner_type) == BOOLEAN_TYPE) != (TREE_CODE (outer_type) == BOOLEAN_TYPE)) && TYPE_PRECISION (outer_type) != 1) return false; /* We don't need to preserve changes in the types minimum or maximum value in general as these do not generate code unless the types precisions are different. */ return true; } /* Scalar floating point types with the same mode are compatible. */ else if (SCALAR_FLOAT_TYPE_P (inner_type) && SCALAR_FLOAT_TYPE_P (outer_type)) return true; /* Fixed point types with the same mode are compatible. */ else if (FIXED_POINT_TYPE_P (inner_type) && FIXED_POINT_TYPE_P (outer_type)) return true; /* We need to take special care recursing to pointed-to types. */ else if (POINTER_TYPE_P (inner_type) && POINTER_TYPE_P (outer_type)) { /* We do not care for const qualification of the pointed-to types as const qualification has no semantic value to the middle-end. */ /* Otherwise pointers/references are equivalent. */ return true; } /* Recurse for complex types. */ else if (TREE_CODE (inner_type) == COMPLEX_TYPE && TREE_CODE (outer_type) == COMPLEX_TYPE) return useless_type_conversion_p (TREE_TYPE (outer_type), TREE_TYPE (inner_type)); /* Recurse for vector types with the same number of subparts. */ else if (TREE_CODE (inner_type) == VECTOR_TYPE && TREE_CODE (outer_type) == VECTOR_TYPE && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type)) return useless_type_conversion_p (TREE_TYPE (outer_type), TREE_TYPE (inner_type)); else if (TREE_CODE (inner_type) == ARRAY_TYPE && TREE_CODE (outer_type) == ARRAY_TYPE) { /* Preserve various attributes. */ if (TYPE_REVERSE_STORAGE_ORDER (inner_type) != TYPE_REVERSE_STORAGE_ORDER (outer_type)) return false; if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type)) return false; /* Conversions from array types with unknown extent to array types with known extent are not useless. */ if (!TYPE_DOMAIN (inner_type) && TYPE_DOMAIN (outer_type)) return false; /* Nor are conversions from array types with non-constant size to array types with constant size or to different size. */ if (TYPE_SIZE (outer_type) && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST && (!TYPE_SIZE (inner_type) || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST || !tree_int_cst_equal (TYPE_SIZE (outer_type), TYPE_SIZE (inner_type)))) return false; /* Check conversions between arrays with partially known extents. If the array min/max values are constant they have to match. Otherwise allow conversions to unknown and variable extents. In particular this declares conversions that may change the mode to BLKmode as useless. */ if (TYPE_DOMAIN (inner_type) && TYPE_DOMAIN (outer_type) && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type)) { tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type)); tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type)); tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type)); tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type)); /* After gimplification a variable min/max value carries no additional information compared to a NULL value. All that matters has been lowered to be part of the IL. */ if (inner_min && TREE_CODE (inner_min) != INTEGER_CST) inner_min = NULL_TREE; if (outer_min && TREE_CODE (outer_min) != INTEGER_CST) outer_min = NULL_TREE; if (inner_max && TREE_CODE (inner_max) != INTEGER_CST) inner_max = NULL_TREE; if (outer_max && TREE_CODE (outer_max) != INTEGER_CST) outer_max = NULL_TREE; /* Conversions NULL / variable <- cst are useless, but not the other way around. */ if (outer_min && (!inner_min || !tree_int_cst_equal (inner_min, outer_min))) return false; if (outer_max && (!inner_max || !tree_int_cst_equal (inner_max, outer_max))) return false; } /* Recurse on the element check. */ return useless_type_conversion_p (TREE_TYPE (outer_type), TREE_TYPE (inner_type)); } else if ((TREE_CODE (inner_type) == FUNCTION_TYPE || TREE_CODE (inner_type) == METHOD_TYPE) && TREE_CODE (inner_type) == TREE_CODE (outer_type)) { tree outer_parm, inner_parm; /* If the return types are not compatible bail out. */ if (!useless_type_conversion_p (TREE_TYPE (outer_type), TREE_TYPE (inner_type))) return false; /* Method types should belong to a compatible base class. */ if (TREE_CODE (inner_type) == METHOD_TYPE && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type), TYPE_METHOD_BASETYPE (inner_type))) return false; /* A conversion to an unprototyped argument list is ok. */ if (!prototype_p (outer_type)) return true; /* If the unqualified argument types are compatible the conversion is useless. */ if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type)) return true; for (outer_parm = TYPE_ARG_TYPES (outer_type), inner_parm = TYPE_ARG_TYPES (inner_type); outer_parm && inner_parm; outer_parm = TREE_CHAIN (outer_parm), inner_parm = TREE_CHAIN (inner_parm)) if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)), TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm)))) return false; /* If there is a mismatch in the number of arguments the functions are not compatible. */ if (outer_parm || inner_parm) return false; /* Defer to the target if necessary. */ if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type)) return comp_type_attributes (outer_type, inner_type) != 0; return true; } /* For aggregates we rely on TYPE_CANONICAL exclusively and require explicit conversions for types involving to be structurally compared types. */ else if (AGGREGATE_TYPE_P (inner_type) && TREE_CODE (inner_type) == TREE_CODE (outer_type)) return TYPE_CANONICAL (inner_type) && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type); else if (TREE_CODE (inner_type) == OFFSET_TYPE && TREE_CODE (outer_type) == OFFSET_TYPE) return useless_type_conversion_p (TREE_TYPE (outer_type), TREE_TYPE (inner_type)) && useless_type_conversion_p (TYPE_OFFSET_BASETYPE (outer_type), TYPE_OFFSET_BASETYPE (inner_type)); return false; }
static int warn_type_compatibility_p (tree prevailing_type, tree type, bool common_or_extern) { int lev = 0; bool odr_p = odr_or_derived_type_p (prevailing_type) && odr_or_derived_type_p (type); if (prevailing_type == type) return 0; /* C++ provide a robust way to check for type compatibility via the ODR rule. */ if (odr_p && !odr_types_equivalent_p (prevailing_type, type)) lev |= 2; /* Function types needs special care, because types_compatible_p never thinks prototype is compatible to non-prototype. */ if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE) { if (TREE_CODE (type) != TREE_CODE (prevailing_type)) lev |= 1; lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type), TREE_TYPE (type), false); if (TREE_CODE (type) == METHOD_TYPE && TREE_CODE (prevailing_type) == METHOD_TYPE) lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type), TYPE_METHOD_BASETYPE (type), false); if (prototype_p (prevailing_type) && prototype_p (type) && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type)) { tree parm1, parm2; for (parm1 = TYPE_ARG_TYPES (prevailing_type), parm2 = TYPE_ARG_TYPES (type); parm1 && parm2; parm1 = TREE_CHAIN (parm1), parm2 = TREE_CHAIN (parm2)) lev |= warn_type_compatibility_p (TREE_VALUE (parm1), TREE_VALUE (parm2), false); if (parm1 || parm2) lev |= odr_p ? 3 : 1; } if (comp_type_attributes (prevailing_type, type) == 0) lev |= 1; return lev; } /* Get complete type. */ prevailing_type = TYPE_MAIN_VARIANT (prevailing_type); type = TYPE_MAIN_VARIANT (type); /* We can not use types_compatible_p because we permit some changes across types. For example unsigned size_t and "signed size_t" may be compatible when merging C and Fortran types. */ if (COMPLETE_TYPE_P (prevailing_type) && COMPLETE_TYPE_P (type) /* While global declarations are never variadic, we can recurse here for function parameter types. */ && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type))) { /* As a special case do not warn about merging int a[]; and int a[]={1,2,3}; here the first declaration is COMMON or EXTERN and sizeof(a) == sizeof (int). */ if (!common_or_extern || TREE_CODE (type) != ARRAY_TYPE || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type))) lev |= 1; } /* Verify TBAA compatibility. Take care of alias set 0 and the fact that we make ptr_type_node to TBAA compatible with every other type. */ if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type)) { alias_set_type set1 = get_alias_set (type); alias_set_type set2 = get_alias_set (prevailing_type); if (set1 && set2 && set1 != set2 && (!POINTER_TYPE_P (type) || !POINTER_TYPE_P (prevailing_type) || (set1 != TYPE_ALIAS_SET (ptr_type_node) && set2 != TYPE_ALIAS_SET (ptr_type_node)))) lev |= 5; } return lev; }