#include "system.h"
#include "coretypes.h"
#include "tm.h"
-#include "hash-set.h"
-#include "machmode.h"
-#include "hash-map.h"
-#include "vec.h"
-#include "double-int.h"
-#include "input.h"
#include "alias.h"
#include "symtab.h"
-#include "wide-int.h"
-#include "inchash.h"
#include "tree.h"
#include "fold-const.h"
#include "print-tree.h"
#include "calls.h"
#include "predict.h"
#include "basic-block.h"
-#include "is-a.h"
-#include "plugin-api.h"
#include "hard-reg-set.h"
#include "function.h"
-#include "ipa-ref.h"
#include "cgraph.h"
-#include "hashtab.h"
#include "rtl.h"
#include "flags.h"
-#include "statistics.h"
-#include "real.h"
-#include "fixed-value.h"
#include "insn-config.h"
#include "expmed.h"
#include "dojump.h"
#include "expr.h"
#include "tree-pass.h"
#include "target.h"
-#include "hash-table.h"
#include "tree-pretty-print.h"
#include "ipa-utils.h"
#include "tree-ssa-alias.h"
tree second;
} type_pair;
-struct pair_traits : default_hashset_traits
+template <>
+struct default_hash_traits <type_pair> : typed_noop_remove <type_pair>
{
+ typedef type_pair value_type;
+ typedef type_pair compare_type;
static hashval_t
hash (type_pair p)
{
};
static bool odr_types_equivalent_p (tree, tree, bool, bool *,
- hash_set<type_pair,pair_traits> *);
+ hash_set<type_pair> *,
+ location_t, location_t);
static bool odr_violation_reported = false;
bool rtti_broken;
};
+/* Return true if T is a type with linkage defined. */
+
+bool
+type_with_linkage_p (const_tree t)
+{
+ /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL. */
+ if (!TYPE_CONTEXT (t)
+ || !TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL
+ || !TYPE_STUB_DECL (t))
+ return false;
+
+ /* In LTO do not get confused by non-C++ produced types or types built
+ with -fno-lto-odr-type-merigng. */
+ if (in_lto_p)
+ {
+ /* To support -fno-lto-odr-type-merigng recognize types with vtables
+ to have linkage. */
+ if (RECORD_OR_UNION_TYPE_P (t)
+ && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
+ return true;
+ /* Do not accept any other types - we do not know if they were produced
+ by C++ FE. */
+ if (!DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
+ return false;
+ }
+
+ return (RECORD_OR_UNION_TYPE_P (t)
+ || TREE_CODE (t) == ENUMERAL_TYPE);
+}
+
+/* Return true if T is in anonymous namespace.
+ This works only on those C++ types with linkage defined. */
+
+bool
+type_in_anonymous_namespace_p (const_tree t)
+{
+ gcc_assert (type_with_linkage_p (t));
+
+ /* Keep -fno-lto-odr-type-merging working by recognizing classes with vtables
+ properly into anonymous namespaces. */
+ if (RECORD_OR_UNION_TYPE_P (t)
+ && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
+ return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
+
+ if (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)))
+ {
+ /* C++ FE uses magic <anon> as assembler names of anonymous types.
+ verify that this match with type_in_anonymous_namespace_p. */
+#ifdef ENABLE_CHECKING
+ if (in_lto_p)
+ gcc_assert (!strcmp ("<anon>",
+ IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t)))));
+#endif
+ return true;
+ }
+ return false;
+}
+
+/* Return true of T is type with One Definition Rule info attached.
+ It means that either it is anonymous type or it has assembler name
+ set. */
+
+bool
+odr_type_p (const_tree t)
+{
+ /* We do not have this information when not in LTO, but we do not need
+ to care, since it is used only for type merging. */
+ gcc_checking_assert (in_lto_p || flag_lto);
+
+ /* To support -fno-lto-odr-type-merging consider types with vtables ODR. */
+ if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
+ return true;
+
+ if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
+ && (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))))
+ {
+#ifdef ENABLE_CHECKING
+ /* C++ FE uses magic <anon> as assembler names of anonymous types.
+ verify that this match with type_in_anonymous_namespace_p. */
+ gcc_assert (!type_with_linkage_p (t)
+ || strcmp ("<anon>",
+ IDENTIFIER_POINTER
+ (DECL_ASSEMBLER_NAME (TYPE_NAME (t))))
+ || type_in_anonymous_namespace_p (t));
+#endif
+ return true;
+ }
+ return false;
+}
+
/* Return TRUE if all derived types of T are known and thus
we may consider the walk of derived type complete.
/* Hash used to unify ODR types based on their mangled name and for anonymous
namespace types. */
-struct odr_name_hasher
+struct odr_name_hasher : pointer_hash <odr_type_d>
{
- typedef odr_type_d *value_type;
typedef union tree_node *compare_type;
static inline hashval_t hash (const odr_type_d *);
static inline bool equal (const odr_type_d *, const tree_node *);
static bool
can_be_name_hashed_p (tree t)
{
- return (!in_lto_p || type_in_anonymous_namespace_p (t)
- || (TYPE_NAME (t) && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))));
+ return (!in_lto_p || odr_type_p (t));
}
/* Hash type by its ODR name. */
return htab_hash_pointer (t);
/* Anonymous types are unique. */
- if (type_in_anonymous_namespace_p (t))
+ if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
return htab_hash_pointer (t);
gcc_checking_assert (TYPE_NAME (t)
if (TYPE_MAIN_VARIANT (t) != t)
return false;
/* Anonymous namespace types are always handled by name hash. */
- if (type_in_anonymous_namespace_p (t))
+ if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
return false;
return (TREE_CODE (t) == RECORD_TYPE
&& TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
/* Check for anonymous namespaces. Those have !TREE_PUBLIC
on the corresponding TYPE_STUB_DECL. */
- if (type_in_anonymous_namespace_p (type1)
- || type_in_anonymous_namespace_p (type2))
+ if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
+ || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
return false;
types_odr_comparable (tree t1, tree t2, bool strict)
{
return (!in_lto_p
- || (strict ? main_odr_variant (t1) == main_odr_variant (t2)
+ || (strict ? (main_odr_variant (t1) == main_odr_variant (t2)
+ && main_odr_variant (t1))
: TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
|| (odr_type_p (t1) && odr_type_p (t2))
|| (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
}
+/* If T is compound type, return type it is based on. */
+
+static tree
+compound_type_base (const_tree t)
+{
+ if (TREE_CODE (t) == ARRAY_TYPE
+ || POINTER_TYPE_P (t)
+ || TREE_CODE (t) == COMPLEX_TYPE
+ || VECTOR_TYPE_P (t))
+ return TREE_TYPE (t);
+ if (TREE_CODE (t) == METHOD_TYPE)
+ return TYPE_METHOD_BASETYPE (t);
+ if (TREE_CODE (t) == OFFSET_TYPE)
+ return TYPE_OFFSET_BASETYPE (t);
+ return NULL_TREE;
+}
+
+/* Return true if T is either ODR type or compound type based from it.
+ If the function return true, we know that T is a type originating from C++
+ source even at link-time. */
+
+bool
+odr_or_derived_type_p (const_tree t)
+{
+ do
+ {
+ if (odr_type_p (t))
+ return true;
+ /* Function type is a tricky one. Basically we can consider it
+ ODR derived if return type or any of the parameters is.
+ We need to check all parameters because LTO streaming merges
+ common types (such as void) and they are not considered ODR then. */
+ if (TREE_CODE (t) == FUNCTION_TYPE)
+ {
+ if (TYPE_METHOD_BASETYPE (t))
+ t = TYPE_METHOD_BASETYPE (t);
+ else
+ {
+ if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
+ return true;
+ for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
+ if (odr_or_derived_type_p (TREE_VALUE (t)))
+ return true;
+ return false;
+ }
+ }
+ else
+ t = compound_type_base (t);
+ }
+ while (t);
+ return t;
+}
+
/* Compare types T1 and T2 and return true if they are
equivalent. */
return false;
/* Check for anonymous namespaces. Those have !TREE_PUBLIC
on the corresponding TYPE_STUB_DECL. */
- if (type_in_anonymous_namespace_p (t1)
- || type_in_anonymous_namespace_p (t2))
+ if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+ || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
return false;
gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
static bool
odr_subtypes_equivalent_p (tree t1, tree t2,
- hash_set<type_pair,pair_traits> *visited)
+ hash_set<type_pair> *visited,
+ location_t loc1, location_t loc2)
{
- bool an1, an2;
/* This can happen in incomplete types that should be handled earlier. */
gcc_assert (t1 && t2);
return true;
/* Anonymous namespace types must match exactly. */
- an1 = type_in_anonymous_namespace_p (t1);
- an2 = type_in_anonymous_namespace_p (t2);
- if (an1 != an2 || an1)
+ if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+ || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
return false;
/* For ODR types be sure to compare their names.
return false;
/* Limit recursion: If subtypes are ODR types and we know
that they are same, be happy. */
- if (!get_odr_type (t1, true)->odr_violated)
+ if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated)
return true;
}
have to be compared structurally. */
if (TREE_CODE (t1) != TREE_CODE (t2))
return false;
- if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
+ if (AGGREGATE_TYPE_P (t1)
+ && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
return false;
type_pair pair={t1,t2};
}
if (visited->add (pair))
return true;
- return odr_types_equivalent_p (t1, t2, false, NULL, visited);
+ return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2);
}
/* Compare two virtual tables, PREVAILING and VTABLE and output ODR
lto_location_cache::current_cache->apply_location_cache ();
if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
- "type %qT violates one definition rule",
+ "type %qT violates the C++ One Definition Rule",
t1))
return;
if (!st1 && !st2)
*warned = true;
}
-/* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
- because they are used on same place in ODR matching types.
- They are not; inform the user. */
+/* Return ture if T1 and T2 are incompatible and we want to recusively
+ dive into them from warn_type_mismatch to give sensible answer. */
-void
-warn_types_mismatch (tree t1, tree t2)
+static bool
+type_mismatch_p (tree t1, tree t2)
{
- /* If types have names and they are different, it is most informative to
- output those. */
+ if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
+ && !odr_types_equivalent_p (t1, t2))
+ return true;
+ return !types_compatible_p (t1, t2);
+}
+
+
+/* Types T1 and T2 was found to be incompatible in a context they can't
+ (either used to declare a symbol of same assembler name or unified by
+ ODR rule). We already output warning about this, but if possible, output
+ extra information on how the types mismatch.
+
+ This is hard to do in general. We basically handle the common cases.
+
+ If LOC1 and LOC2 are meaningful locations, use it in the case the types
+ themselves do no thave one.*/
+
+void
+warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
+{
+ /* Location of type is known only if it has TYPE_NAME and the name is
+ TYPE_DECL. */
+ location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
+ ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
+ : UNKNOWN_LOCATION;
+ location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
+ ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
+ : UNKNOWN_LOCATION;
+ bool loc_t2_useful = false;
+
+ /* With LTO it is a common case that the location of both types match.
+ See if T2 has a location that is different from T1. If so, we will
+ inform user about the location.
+ Do not consider the location passed to us in LOC1/LOC2 as those are
+ already output. */
+ if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
+ {
+ if (loc_t1 <= BUILTINS_LOCATION)
+ loc_t2_useful = true;
+ else
+ {
+ expanded_location xloc1 = expand_location (loc_t1);
+ expanded_location xloc2 = expand_location (loc_t2);
+
+ if (strcmp (xloc1.file, xloc2.file)
+ || xloc1.line != xloc2.line
+ || xloc1.column != xloc2.column)
+ loc_t2_useful = true;
+ }
+ }
+
+ if (loc_t1 <= BUILTINS_LOCATION)
+ loc_t1 = loc1;
+ if (loc_t2 <= BUILTINS_LOCATION)
+ loc_t2 = loc2;
+
+ location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
+
+ /* It is a quite common bug to reference anonymous namespace type in
+ non-anonymous namespace class. */
+ if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+ || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
+ {
+ if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1))
+ {
+ std::swap (t1, t2);
+ std::swap (loc_t1, loc_t2);
+ }
+ gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
+ && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
+ && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
+ /* Most of the time, the type names will match, do not be unnecesarily
+ verbose. */
+ if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1)))
+ != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
+ inform (loc_t1,
+ "type %qT defined in anonymous namespace can not match "
+ "type %qT across the translation unit boundary",
+ t1, t2);
+ else
+ inform (loc_t1,
+ "type %qT defined in anonymous namespace can not match "
+ "across the translation unit boundary",
+ t1);
+ if (loc_t2_useful)
+ inform (loc_t2,
+ "the incompatible type defined in another translation unit");
+ return;
+ }
+ /* If types have mangled ODR names and they are different, it is most
+ informative to output those.
+ This also covers types defined in different namespaces. */
if (TYPE_NAME (t1) && TYPE_NAME (t2)
+ && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
+ && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
&& DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
&& DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
&& DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
if (name1 && name2 && strcmp (name1, name2))
{
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
+ inform (loc_t1,
"type name %<%s%> should match type name %<%s%>",
name1, name2);
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
- "the incompatible type is defined here");
+ if (loc_t2_useful)
+ inform (loc_t2,
+ "the incompatible type is defined here");
free (name1);
return;
}
free (name1);
}
- /* It is a quite common bug to reference anonymous namespace type in
- non-anonymous namespace class. */
- if (type_in_anonymous_namespace_p (t1)
- || type_in_anonymous_namespace_p (t2))
- {
- if (!type_in_anonymous_namespace_p (t1))
- {
- tree tmp = t1;;
- t1 = t2;
- t2 = tmp;
- }
- if (TYPE_NAME (t1) && TYPE_NAME (t2))
- {
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
- "type %qT defined in anonymous namespace can not match "
- "type %qT",
- t1, t2);
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
- "the incompatible type defined in anonymous namespace in "
- "another translation unit");
- }
- else
- inform (UNKNOWN_LOCATION,
- "types in anonymous namespace does not match across "
- "translation unit boundary");
- return;
- }
- /* A tricky case are component types. Often they appear the same in source
+ /* A tricky case are compound types. Often they appear the same in source
code and the mismatch is dragged in by type they are build from.
Look for those differences in subtypes and try to be informative. In other
cases just output nothing because the source code is probably different
{
if (TREE_CODE (t1) == TREE_CODE (t2))
{
- hash_set<type_pair,pair_traits> visited;
if (TREE_CODE (t1) == ARRAY_TYPE
&& COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
{
&& !operand_equal_p (TYPE_MAX_VALUE (i1),
TYPE_MAX_VALUE (i2), 0))
{
- inform (UNKNOWN_LOCATION,
+ inform (loc,
"array types have different bounds");
return;
}
}
if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
- && !odr_subtypes_equivalent_p (TREE_TYPE (t1),
- TREE_TYPE (t2),
- &visited))
- warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+ && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+ warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
else if (TREE_CODE (t1) == METHOD_TYPE
|| TREE_CODE (t1) == FUNCTION_TYPE)
{
- tree parms1, parms2;
+ tree parms1 = NULL, parms2 = NULL;
int count = 1;
- if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
- &visited))
+ if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
{
- inform (UNKNOWN_LOCATION, "return value type mismatch");
- warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+ inform (loc, "return value type mismatch");
+ warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
+ loc_t2);
return;
}
- for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
- parms1 && parms2;
- parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
- count++)
- {
- if (!odr_subtypes_equivalent_p
- (TREE_VALUE (parms1), TREE_VALUE (parms2), &visited))
- {
- inform (UNKNOWN_LOCATION,
- "type mismatch in parameter %i", count);
- warn_types_mismatch (TREE_VALUE (parms1),
- TREE_VALUE (parms2));
- return;
- }
- }
+ if (prototype_p (t1) && prototype_p (t2))
+ for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
+ parms1 && parms2;
+ parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
+ count++)
+ {
+ if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
+ {
+ if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
+ inform (loc,
+ "implicit this pointer type mismatch");
+ else
+ inform (loc,
+ "type mismatch in parameter %i",
+ count - (TREE_CODE (t1) == METHOD_TYPE));
+ warn_types_mismatch (TREE_VALUE (parms1),
+ TREE_VALUE (parms2),
+ loc_t1, loc_t2);
+ return;
+ }
+ }
if (parms1 || parms2)
{
- inform (UNKNOWN_LOCATION,
+ inform (loc,
"types have different parameter counts");
return;
}
}
return;
}
- /* This should not happen but if it does, the warning would not be helpful.
- TODO: turn it into assert next stage1. */
- if (TYPE_NAME (t1) == TYPE_NAME (t2))
+
+ if (types_odr_comparable (t1, t2, true)
+ && types_same_for_odr (t1, t2, true))
+ inform (loc_t1,
+ "type %qT itself violate the C++ One Definition Rule", t1);
+ /* Prevent pointless warnings like "struct aa" should match "struct aa". */
+ else if (TYPE_NAME (t1) == TYPE_NAME (t2)
+ && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
return;
- /* In Firefox it is a common bug to have same types but in
- different namespaces. Be a bit more informative on
- this. */
- if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
- && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
- != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
- || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
- && (DECL_NAME (TYPE_CONTEXT (t1)) !=
- DECL_NAME (TYPE_CONTEXT (t2))))))
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
- "type %qT should match type %qT but is defined "
- "in different namespace ",
- t1, t2);
- else if (types_odr_comparable (t1, t2, true)
- && types_same_for_odr (t1, t2, true))
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
- "type %qT should match type %qT that itself violate "
- "one definition rule",
- t1, t2);
else
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
- "type %qT should match type %qT",
+ inform (loc_t1, "type %qT should match type %qT",
t1, t2);
- if (DECL_SOURCE_LOCATION (TYPE_NAME (t2)) > BUILTINS_LOCATION)
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
- "the incompatible type is defined here");
+ if (loc_t2_useful)
+ inform (loc_t2, "the incompatible type is defined here");
}
/* Compare T1 and T2, report ODR violations if WARN is true and set
WARNED to true if anything is reported. Return true if types match.
If true is returned, the types are also compatible in the sense of
- gimple_canonical_types_compatible_p. */
+ gimple_canonical_types_compatible_p.
+ If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
+ about the type if the type itself do not have location. */
static bool
odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
- hash_set<type_pair,pair_traits> *visited)
+ hash_set<type_pair> *visited,
+ location_t loc1, location_t loc2)
{
/* Check first for the obvious case of pointer identity. */
if (t1 == t2)
return true;
- gcc_assert (!type_in_anonymous_namespace_p (t1));
- gcc_assert (!type_in_anonymous_namespace_p (t2));
+ gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
+ gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
/* Can't be the same type if the types don't have the same code. */
if (TREE_CODE (t1) != TREE_CODE (t2))
return false;
}
+ if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+ || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
+ {
+ /* We can not trip this when comparing ODR types, only when trying to
+ match different ODR derivations from different declarations.
+ So WARN should be always false. */
+ gcc_assert (!warn);
+ return false;
+ }
+
if (comp_type_attributes (t1, t2) != 1)
{
warn_odr (t1, t2, NULL, NULL, warn, warned,
- G_("a type with attributes "
+ G_("a type with different attributes "
"is defined in another translation unit"));
return false;
}
return false;
}
- if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+ if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+ visited, loc1, loc2))
{
warn_odr (t1, t2, NULL, NULL, warn, warned,
G_("it is defined as a pointer to different type "
"in another translation unit"));
if (warn && warned)
- warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+ warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
+ loc1, loc2);
return false;
}
}
if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
- && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+ && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+ visited, loc1, loc2))
{
/* Probably specific enough. */
warn_odr (t1, t2, NULL, NULL, warn, warned,
G_("a different type is defined "
"in another translation unit"));
if (warn && warned)
- warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+ warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
return false;
}
}
{
/* Array types are the same if the element types are the same and
the number of elements are the same. */
- if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+ if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+ visited, loc1, loc2))
{
warn_odr (t1, t2, NULL, NULL, warn, warned,
G_("a different type is defined in another "
"translation unit"));
if (warn && warned)
- warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+ warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
}
gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
case FUNCTION_TYPE:
/* Function types are the same if the return type and arguments types
are the same. */
- if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+ if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+ visited, loc1, loc2))
{
warn_odr (t1, t2, NULL, NULL, warn, warned,
G_("has different return value "
"in another translation unit"));
if (warn && warned)
- warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+ warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
return false;
}
- if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
+ if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
+ || !prototype_p (t1) || !prototype_p (t2))
return true;
else
{
parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
{
if (!odr_subtypes_equivalent_p
- (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
+ (TREE_VALUE (parms1), TREE_VALUE (parms2), visited,
+ loc1, loc2))
{
warn_odr (t1, t2, NULL, NULL, warn, warned,
G_("has different parameters in another "
"translation unit"));
if (warn && warned)
warn_types_mismatch (TREE_VALUE (parms1),
- TREE_VALUE (parms2));
+ TREE_VALUE (parms2), loc1, loc2);
return false;
}
}
return false;
}
if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
- TREE_TYPE (f2), visited))
+ TREE_TYPE (f2), visited,
+ loc1, loc2))
{
/* Do not warn about artificial fields and just go into
generic field mismatch warning. */
G_("a field of same name but different type "
"is defined in another translation unit"));
if (warn && warned)
- warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
+ warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
return false;
}
if (!gimple_compare_field_offset (f1, f2))
return false;
}
if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
+ && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1))
+ && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2))
+ && odr_type_p (TYPE_MAIN_VARIANT (t1))
+ && odr_type_p (TYPE_MAIN_VARIANT (t2))
&& (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
!= TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
{
- for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
- f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
- f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
- {
- if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
- {
- warn_odr (t1, t2, f1, f2, warn, warned,
- G_("a different method of same type "
- "is defined in another translation unit"));
- return false;
- }
- if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
- {
- warn_odr (t1, t2, f1, f2, warn, warned,
- G_("s definition that differs by virtual "
- "keyword in another translation unit"));
- return false;
- }
- if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
- {
- warn_odr (t1, t2, f1, f2, warn, warned,
- G_("virtual table layout differs in another "
- "translation unit"));
- return false;
- }
- if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
- {
- warn_odr (t1, t2, f1, f2, warn, warned,
- G_("method with incompatible type is defined "
- "in another translation unit"));
- return false;
- }
- }
- if (f1 || f2)
+ /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
+ if it is non-NULL so this loop will never realy execute. */
+ if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1)) != error_mark_node
+ && TYPE_METHODS (TYPE_MAIN_VARIANT (t2)) != error_mark_node)
+ for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
+ f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
+ f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
+ {
+ if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
+ {
+ warn_odr (t1, t2, f1, f2, warn, warned,
+ G_("a different method of same type "
+ "is defined in another "
+ "translation unit"));
+ return false;
+ }
+ if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
+ {
+ warn_odr (t1, t2, f1, f2, warn, warned,
+ G_("s definition that differs by virtual "
+ "keyword in another translation unit"));
+ return false;
+ }
+ if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
+ {
+ warn_odr (t1, t2, f1, f2, warn, warned,
+ G_("virtual table layout differs "
+ "in another translation unit"));
+ return false;
+ }
+ if (odr_subtypes_equivalent_p (TREE_TYPE (f1),
+ TREE_TYPE (f2), visited,
+ loc1, loc2))
+ {
+ warn_odr (t1, t2, f1, f2, warn, warned,
+ G_("method with incompatible type is "
+ "defined in another translation unit"));
+ return false;
+ }
+ }
+ if ((f1 == NULL) != (f2 == NULL))
{
warn_odr (t1, t2, NULL, NULL, warn, warned,
G_("a type with different number of methods "
break;
}
case VOID_TYPE:
+ case NULLPTR_TYPE:
break;
default:
return true;
}
+/* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
+
+bool
+odr_types_equivalent_p (tree type1, tree type2)
+{
+ hash_set<type_pair> visited;
+
+#ifdef ENABLE_CHECKING
+ gcc_assert (odr_or_derived_type_p (type1) && odr_or_derived_type_p (type2));
+#endif
+ return odr_types_equivalent_p (type1, type2, false, NULL,
+ &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
+}
+
/* TYPE is equivalent to VAL by ODR, but its tree representation differs
from VAL->type. This may happen in LTO where tree merging did not merge
all variants of the same type or due to ODR violation.
}
if (prevail)
- {
- tree tmp = type;
-
- type = val->type;
- val->type = tmp;
- }
+ std::swap (val->type, type);
val->types_set->add (type);
bool base_mismatch = false;
unsigned int i;
bool warned = false;
- hash_set<type_pair,pair_traits> visited;
+ hash_set<type_pair> visited;
gcc_assert (in_lto_p);
vec_safe_push (val->types, type);
base_mismatch = true;
}
else
- {
- hash_set<type_pair,pair_traits> visited;
- if (!odr_types_equivalent_p (type1, type2, false, NULL,
- &visited))
- base_mismatch = true;
- }
+ if (!odr_types_equivalent_p (type1, type2))
+ base_mismatch = true;
if (base_mismatch)
{
if (!warned && !val->odr_violated)
"a type with the same name but different base "
"type is defined in another translation unit");
if (warned)
- warn_types_mismatch (type1, type2);
+ warn_types_mismatch (type1, type2,
+ UNKNOWN_LOCATION, UNKNOWN_LOCATION);
}
break;
}
/* Next compare memory layout. */
if (!odr_types_equivalent_p (val->type, type,
!flag_ltrans && !val->odr_violated && !warned,
- &warned, &visited))
+ &warned, &visited,
+ DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
+ DECL_SOURCE_LOCATION (TYPE_NAME (type))))
{
merge = false;
odr_violation_reported = true;
val->type = type;
val->bases = vNULL;
val->derived_types = vNULL;
- val->anonymous_namespace = type_in_anonymous_namespace_p (type);
+ if (type_with_linkage_p (type))
+ val->anonymous_namespace = type_in_anonymous_namespace_p (type);
+ else
+ val->anonymous_namespace = 0;
build_bases = COMPLETE_TYPE_P (val->type);
insert_to_odr_array = true;
if (slot)
}
if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
+ && type_with_linkage_p (type)
&& type == TYPE_MAIN_VARIANT (type))
{
tree binfo = TYPE_BINFO (type);
if (in_lto_p)
odr_vtable_hash = new odr_vtable_hash_type (23);
}
- /* Arrange things to be nicer and insert main variants first. */
- if (odr_type_p (TYPE_MAIN_VARIANT (type)))
+ /* Arrange things to be nicer and insert main variants first.
+ ??? fundamental prerecorded types do not have mangled names; this
+ makes it possible that non-ODR type is main_odr_variant of ODR type.
+ Things may get smoother if LTO FE set mangled name of those types same
+ way as C++ FE does. */
+ if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type)))
+ && odr_type_p (TYPE_MAIN_VARIANT (type)))
get_odr_type (TYPE_MAIN_VARIANT (type), true);
- if (TYPE_MAIN_VARIANT (type) != type)
+ if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
get_odr_type (type, true);
}
/* Return true if type is known to have no derivations. */
bool
-type_known_to_have_no_deriavations_p (tree t)
+type_known_to_have_no_derivations_p (tree t)
{
return (type_all_derivations_known_p (t)
&& (TYPE_FINAL_P (t)
}
}
-/* Given method type T, return type of class it belongs to.
- Look up this pointer and get its type. */
-
-tree
-method_class_type (const_tree t)
-{
- tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
- gcc_assert (TREE_CODE (t) == METHOD_TYPE);
-
- return TREE_TYPE (first_parm_type);
-}
-
/* Initialize IPA devirt and build inheritance tree graph. */
void
if (is_a <cgraph_node *> (n)
&& DECL_VIRTUAL_P (n->decl)
&& n->real_symbol_p ())
- get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
- true);
+ get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
/* Look also for virtual tables of types that do not define any methods.
/* Polymorphic call target cache helpers. */
-struct polymorphic_call_target_hasher
+struct polymorphic_call_target_hasher
+ : pointer_hash <polymorphic_call_target_d>
{
- typedef polymorphic_call_target_d *value_type;
- typedef polymorphic_call_target_d *compare_type;
static inline hashval_t hash (const polymorphic_call_target_d *);
static inline bool equal (const polymorphic_call_target_d *,
const polymorphic_call_target_d *);
if (DECL_VIRTUAL_P (n->decl)
&& !n->definition
&& n->real_symbol_p ())
- get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
- true);
+ get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
timevar_pop (TV_IPA_INHERITANCE);
}