-static bool maybe_merge_incomplete_and_complete_type (tree, tree);
-
-/* Try to merge an incomplete type INCOMPLETE with a complete type
- COMPLETE of same kinds.
- Return true if they were merged, false otherwise. */
-
-static bool
-merge_incomplete_and_complete_type (tree incomplete, tree complete)
-{
- /* For merging array types do some extra sanity checking. */
- if (TREE_CODE (incomplete) == ARRAY_TYPE
- && !maybe_merge_incomplete_and_complete_type (TREE_TYPE (incomplete),
- TREE_TYPE (complete))
- && !gimple_types_compatible_p (TREE_TYPE (incomplete),
- TREE_TYPE (complete)))
- return false;
-
- /* ??? Ideally we would do this by means of a common canonical type, but
- that's difficult as we do not have links from the canonical type
- back to all its children. */
- gimple_force_type_merge (incomplete, complete);
-
- return true;
-}
-
-/* Try to merge a maybe complete / incomplete type pair TYPE1 and TYPE2.
- Return true if they were merged, false otherwise. */
-
-static bool
-maybe_merge_incomplete_and_complete_type (tree type1, tree type2)
-{
- bool res = false;
-
- if (TREE_CODE (type1) != TREE_CODE (type2))
- return false;
-
- if (!COMPLETE_TYPE_P (type1) && COMPLETE_TYPE_P (type2))
- res = merge_incomplete_and_complete_type (type1, type2);
- else if (COMPLETE_TYPE_P (type1) && !COMPLETE_TYPE_P (type2))
- res = merge_incomplete_and_complete_type (type2, type1);
-
- /* Recurse on pointer targets. */
- if (!res
- && POINTER_TYPE_P (type1)
- && POINTER_TYPE_P (type2))
- res = maybe_merge_incomplete_and_complete_type (TREE_TYPE (type1),
- TREE_TYPE (type2));
-
- return res;
-}
-
-/* Check if OLD_DECL and NEW_DECL are compatible. */
-
-static bool
-lto_symtab_compatible (tree old_decl, tree new_decl)
-{
- tree old_type, new_type;
-
- if (TREE_CODE (old_decl) != TREE_CODE (new_decl))
- {
- switch (TREE_CODE (new_decl))
- {
- case VAR_DECL:
- gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL);
- error_at (DECL_SOURCE_LOCATION (new_decl),
- "function %qD redeclared as variable", new_decl);
- inform (DECL_SOURCE_LOCATION (old_decl),
- "previously declared here");
- return false;
-
- case FUNCTION_DECL:
- gcc_assert (TREE_CODE (old_decl) == VAR_DECL);
- error_at (DECL_SOURCE_LOCATION (new_decl),
- "variable %qD redeclared as function", new_decl);
- inform (DECL_SOURCE_LOCATION (old_decl),
- "previously declared here");
- return false;
-
- default:
- gcc_unreachable ();
- }
- }
-
- if (TREE_CODE (new_decl) == FUNCTION_DECL)
- {
- if (!gimple_types_compatible_p (TREE_TYPE (old_decl),
- TREE_TYPE (new_decl)))
- /* If we don't have a merged type yet...sigh. The linker
- wouldn't complain if the types were mismatched, so we
- probably shouldn't either. Just use the type from
- whichever decl appears to be associated with the
- definition. If for some odd reason neither decl is, the
- older one wins. */
- (void) 0;
-
- return true;
- }
-
- /* Now we exclusively deal with VAR_DECLs. */
-
- /* Handle external declarations with incomplete type or pointed-to
- incomplete types by forcefully merging the types.
- ??? In principle all types involved in the two decls should
- be merged forcefully, for example without considering type or
- field names. */
- old_type = TREE_TYPE (old_decl);
- new_type = TREE_TYPE (new_decl);
-
- if (DECL_EXTERNAL (old_decl) || DECL_EXTERNAL (new_decl))
- maybe_merge_incomplete_and_complete_type (old_type, new_type);
- else if (POINTER_TYPE_P (old_type)
- && POINTER_TYPE_P (new_type))
- maybe_merge_incomplete_and_complete_type (TREE_TYPE (old_type),
- TREE_TYPE (new_type));
-
- /* For array types we have to accept external declarations with
- different sizes than the actual definition (164.gzip).
- ??? We could emit a warning here. */
- if (TREE_CODE (old_type) == TREE_CODE (new_type)
- && TREE_CODE (old_type) == ARRAY_TYPE
- && COMPLETE_TYPE_P (old_type)
- && COMPLETE_TYPE_P (new_type)
- && tree_int_cst_compare (TYPE_SIZE (old_type),
- TYPE_SIZE (new_type)) != 0
- && gimple_types_compatible_p (TREE_TYPE (old_type),
- TREE_TYPE (new_type)))
- {
- /* If only one is external use the type of the non-external decl.
- Else use the larger one and also adjust the decl size.
- ??? Directional merging would allow us to simply pick the
- larger one instead of rewriting it. */
- if (DECL_EXTERNAL (old_decl) ^ DECL_EXTERNAL (new_decl))
- {
- if (DECL_EXTERNAL (old_decl))
- TREE_TYPE (old_decl) = new_type;
- else if (DECL_EXTERNAL (new_decl))
- TREE_TYPE (new_decl) = old_type;
- }
- else
- {
- if (tree_int_cst_compare (TYPE_SIZE (old_type),
- TYPE_SIZE (new_type)) < 0)
- {
- TREE_TYPE (old_decl) = new_type;
- DECL_SIZE (old_decl) = DECL_SIZE (new_decl);
- DECL_SIZE_UNIT (old_decl) = DECL_SIZE_UNIT (new_decl);
- }
- else
- {
- TREE_TYPE (new_decl) = old_type;
- DECL_SIZE (new_decl) = DECL_SIZE (old_decl);
- DECL_SIZE_UNIT (new_decl) = DECL_SIZE_UNIT (old_decl);
- }
- }
- }
-
- /* We can tolerate differences in type qualification, the
- qualification of the prevailing definition will prevail. */
- old_type = TYPE_MAIN_VARIANT (TREE_TYPE (old_decl));
- new_type = TYPE_MAIN_VARIANT (TREE_TYPE (new_decl));
- if (!gimple_types_compatible_p (old_type, new_type))
- {
- if (warning_at (DECL_SOURCE_LOCATION (new_decl), 0,
- "type of %qD does not match original declaration",
- new_decl))
- inform (DECL_SOURCE_LOCATION (old_decl),
- "previously declared here");
- return false;
- }
-
- /* ??? We might want to emit a warning here if type qualification
- differences were spotted. Do not do this unconditionally though. */
-
- /* There is no point in comparing too many details of the decls here.
- The type compatibility checks or the completing of types has properly
- dealt with most issues. */
-
- /* The following should all not invoke fatal errors as in non-LTO
- mode the linker wouldn't complain either. Just emit warnings. */
-
- /* Report a warning if user-specified alignments do not match. */
- if ((DECL_USER_ALIGN (old_decl) && DECL_USER_ALIGN (new_decl))
- && DECL_ALIGN (old_decl) != DECL_ALIGN (new_decl))
- {
- warning_at (DECL_SOURCE_LOCATION (new_decl), 0,
- "alignment of %qD does not match original declaration",
- new_decl);
- inform (DECL_SOURCE_LOCATION (old_decl), "previously declared here");
- return false;
- }
-
- return true;
-}
-