Hi, here is patch that adds TYPE_ODR_P to determine type that comply C++ ODR rules (i.e. ODR types themselves or structures/unions derived from them). I have decided to use STRING_FLAG which have meaning only for integers and arrays which forced me to add type checks on places where we check STRING_FLAG on other types.
The patch also let me to verify that all types we consider to have linkage actually are created by C++ FE which turned out to not be the case for Ada which I fixed in needs_assembler_name_p. Bootstrapped/regtested x86_64-linux, OK? * ipa-utils.h (type_with_linkage_p): Verify that type is CXX_ODR_P. (odr_type_p): Remove extra return. * lto-streamer-out.c (hash_tree): Hash TYPE_CXX_ODR_P; hash STRING_FLAG only for arrays and integers. * tree-stremaer-in.c (unpack_ts_type_common_value_fields): Update analogously. * tree-streamer-out.c (pack_ts_type_common_value_fields): Likewise. * print-tree.c (print_node): Print cxx-odr-p and string-flag. * tree.c (need_assembler_name_p): Also check that type is CXX_ODR_TYPE_P (verify_type_variant): Update verification of SRING_FLAG; also check CXX_ODR_P. * tree.h (ARRAY_OR_INTEGER_TYPE_CHECK): New macro. (TYPE_STRING_FLAG): Use it. (TYPE_CXX_ODR_P): New macro. * lto-common.c (compare_tree_sccs_1): Compare CXX_ODR_P; compare STRING_FLAG only for arrays and integers. * gcc-interface/decl.c (gnat_to_gnu_entity): Check that type is array or integer prior checking string flag. * gcc-interface/gigi.h (gnat_signed_type_for, maybe_character_value): Likewise. * c-common.c (braced_lists_to_strings): Check that type is array or integer prior checking string flag. * lex.c (cxx_make_type): Set TYPE_CXX_ODR_P. * dwarf2out.c (gen_array_type_die): First check that type is an array and then test string flag. * trans-expr.c (gfc_conv_substring): Check that type is array or integer prior checking string flag. (gfc_conv_string_parameter): Likewise. * trans-openmp.c (gfc_omp_scalar_p): Likewise. * trans.c (gfc_build_array_ref): Likewise. Index: ada/gcc-interface/decl.c =================================================================== --- ada/gcc-interface/decl.c (revision 272353) +++ ada/gcc-interface/decl.c (working copy) @@ -1855,7 +1855,9 @@ gnat_to_gnu_entity (Entity_Id gnat_entit = Has_Biased_Representation (gnat_entity); /* Do the same processing for Character subtypes as for types. */ - if (TYPE_STRING_FLAG (TREE_TYPE (gnu_type))) + if ((TREE_CODE (TREE_TYPE (gnu_type)) == INTEGER_TYPE + || TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE) + && TYPE_STRING_FLAG (TREE_TYPE (gnu_type))) { TYPE_NAME (gnu_type) = gnu_entity_name; TYPE_STRING_FLAG (gnu_type) = 1; Index: ada/gcc-interface/gigi.h =================================================================== --- ada/gcc-interface/gigi.h (revision 272353) +++ ada/gcc-interface/gigi.h (working copy) @@ -1138,7 +1138,8 @@ gnat_signed_type_for (tree type_node) static inline tree maybe_character_type (tree type) { - if (TYPE_STRING_FLAG (type) && !TYPE_UNSIGNED (type)) + if (TREE_CODE (type) == INTEGER_TYPE + && TYPE_STRING_FLAG (type) && !TYPE_UNSIGNED (type)) type = gnat_unsigned_type_for (type); return type; @@ -1151,7 +1152,8 @@ maybe_character_value (tree expr) { tree type = TREE_TYPE (expr); - if (TYPE_STRING_FLAG (type) && !TYPE_UNSIGNED (type)) + if (TREE_CODE (type) == INTEGER_TYPE + && TYPE_STRING_FLAG (type) && !TYPE_UNSIGNED (type)) { type = gnat_unsigned_type_for (type); expr = convert (type, expr); Index: c-family/c-common.c =================================================================== --- c-family/c-common.c (revision 272353) +++ c-family/c-common.c (working copy) @@ -8847,7 +8847,8 @@ braced_lists_to_strings (tree type, tree else return ctor; - if (TYPE_STRING_FLAG (ttp)) + if ((TREE_CODE (ttp) == ARRAY_TYPE || TREE_CODE (ttp) == INTEGER_TYPE) + && TYPE_STRING_FLAG (ttp)) return braced_list_to_string (type, ctor); code = TREE_CODE (ttp); Index: cp/lex.c =================================================================== --- cp/lex.c (revision 272353) +++ cp/lex.c (working copy) @@ -884,6 +884,9 @@ cxx_make_type (enum tree_code code MEM_S CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only; } + if (code == RECORD_TYPE || code == UNION_TYPE) + TYPE_CXX_ODR_P (t) = 1; + return t; } Index: dwarf2out.c =================================================================== --- dwarf2out.c (revision 272353) +++ dwarf2out.c (working copy) @@ -21850,8 +21850,8 @@ gen_array_type_die (tree type, dw_die_re /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as DW_TAG_string_type doesn't have DW_AT_type attribute). */ - if (TYPE_STRING_FLAG (type) - && TREE_CODE (type) == ARRAY_TYPE + if (TREE_CODE (type) == ARRAY_TYPE + && TYPE_STRING_FLAG (type) && is_fortran () && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node)) { Index: fortran/trans-expr.c =================================================================== --- fortran/trans-expr.c (revision 272353) +++ fortran/trans-expr.c (working copy) @@ -2309,7 +2309,9 @@ gfc_conv_substring (gfc_se * se, gfc_ref start.expr = gfc_evaluate_now (start.expr, &se->pre); /* Change the start of the string. */ - if (TYPE_STRING_FLAG (TREE_TYPE (se->expr))) + if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE + || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE) + && TYPE_STRING_FLAG (TREE_TYPE (se->expr))) tmp = se->expr; else tmp = build_fold_indirect_ref_loc (input_location, @@ -9479,7 +9481,9 @@ gfc_conv_string_parameter (gfc_se * se) return; } - if (TYPE_STRING_FLAG (TREE_TYPE (se->expr))) + if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE + || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE) + && TYPE_STRING_FLAG (TREE_TYPE (se->expr))) { if (TREE_CODE (se->expr) != INDIRECT_REF) { Index: fortran/trans-openmp.c =================================================================== --- fortran/trans-openmp.c (revision 272353) +++ fortran/trans-openmp.c (working copy) @@ -1222,7 +1222,8 @@ gfc_omp_scalar_p (tree decl) || GFC_CLASS_TYPE_P (type)) return false; } - if (TYPE_STRING_FLAG (type)) + if ((TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == INTEGER_TYPE) + && TYPE_STRING_FLAG (type)) return false; if (INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type) Index: fortran/trans.c =================================================================== --- fortran/trans.c (revision 272353) +++ fortran/trans.c (working copy) @@ -418,7 +418,8 @@ gfc_build_array_ref (tree base, tree off tmp = gfc_build_addr_expr (pvoid_type_node, base); tmp = fold_build_pointer_plus_loc (input_location, tmp, offset); tmp = fold_convert (build_pointer_type (type), tmp); - if (!TYPE_STRING_FLAG (type)) + if ((TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != ARRAY_TYPE) + || !TYPE_STRING_FLAG (type)) tmp = build_fold_indirect_ref_loc (input_location, tmp); return tmp; } Index: ipa-utils.h =================================================================== --- ipa-utils.h (revision 272353) +++ ipa-utils.h (working copy) @@ -205,6 +205,8 @@ type_with_linkage_p (const_tree t) if (!TYPE_CONTEXT (t)) return false; + gcc_checking_assert (TREE_CODE (t) == ENUMERAL_TYPE || TYPE_CXX_ODR_P (t)); + return true; } @@ -240,7 +242,6 @@ odr_type_p (const_tree t) gcc_checking_assert (in_lto_p || flag_lto); return TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)); - return false; } #endif /* GCC_IPA_UTILS_H */ Index: lto/lto-common.c =================================================================== --- lto/lto-common.c (revision 272353) +++ lto/lto-common.c (working copy) @@ -1124,15 +1124,17 @@ compare_tree_sccs_1 (tree t1, tree t2, t if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON)) { compare_values (TYPE_MODE); - compare_values (TYPE_STRING_FLAG); compare_values (TYPE_NEEDS_CONSTRUCTING); if (RECORD_OR_UNION_TYPE_P (t1)) { compare_values (TYPE_TRANSPARENT_AGGR); compare_values (TYPE_FINAL_P); + compare_values (TYPE_CXX_ODR_P); } else if (code == ARRAY_TYPE) compare_values (TYPE_NONALIASED_COMPONENT); + if (code == ARRAY_TYPE || code == INTEGER_TYPE) + compare_values (TYPE_STRING_FLAG); if (AGGREGATE_TYPE_P (t1)) compare_values (TYPE_TYPELESS_STORAGE); compare_values (TYPE_EMPTY_P); Index: lto-streamer-out.c =================================================================== --- lto-streamer-out.c (revision 272353) +++ lto-streamer-out.c (working copy) @@ -1143,7 +1143,6 @@ hash_tree (struct streamer_tree_cache_d if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON)) { hstate.add_hwi (TYPE_MODE (t)); - hstate.add_flag (TYPE_STRING_FLAG (t)); /* TYPE_NO_FORCE_BLK is private to stor-layout and need no streaming. */ hstate.add_flag (TYPE_PACKED (t)); @@ -1154,9 +1153,12 @@ hash_tree (struct streamer_tree_cache_d { hstate.add_flag (TYPE_TRANSPARENT_AGGR (t)); hstate.add_flag (TYPE_FINAL_P (t)); + hstate.add_flag (TYPE_CXX_ODR_P (t)); } else if (code == ARRAY_TYPE) hstate.add_flag (TYPE_NONALIASED_COMPONENT (t)); + if (code == ARRAY_TYPE || code == INTEGER_TYPE) + hstate.add_flag (TYPE_STRING_FLAG (t)); if (AGGREGATE_TYPE_P (t)) hstate.add_flag (TYPE_TYPELESS_STORAGE (t)); hstate.commit_flag (); Index: print-tree.c =================================================================== --- print-tree.c (revision 272353) +++ print-tree.c (working copy) @@ -601,7 +601,7 @@ print_node (FILE *file, const char *pref if (TYPE_NO_FORCE_BLK (node)) fputs (" no-force-blk", file); - if (TYPE_STRING_FLAG (node)) + if (code == ARRAY_TYPE && TYPE_STRING_FLAG (node)) fputs (" string-flag", file); if (TYPE_NEEDS_CONSTRUCTING (node)) @@ -614,6 +614,11 @@ print_node (FILE *file, const char *pref && TYPE_REVERSE_STORAGE_ORDER (node)) fputs (" reverse-storage-order", file); + if ((code == RECORD_TYPE + || code == UNION_TYPE) + && TYPE_CXX_ODR_P (node)) + fputs (" cxx-odr-p", file); + /* The transparent-union flag is used for different things in different nodes. */ if ((code == UNION_TYPE || code == RECORD_TYPE) Index: tree-streamer-in.c =================================================================== --- tree-streamer-in.c (revision 272353) +++ tree-streamer-in.c (working copy) @@ -372,7 +372,6 @@ unpack_ts_type_common_value_fields (stru mode = bp_unpack_machine_mode (bp); SET_TYPE_MODE (expr, mode); - TYPE_STRING_FLAG (expr) = (unsigned) bp_unpack_value (bp, 1); /* TYPE_NO_FORCE_BLK is private to stor-layout and need no streaming. */ TYPE_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1); @@ -383,9 +382,12 @@ unpack_ts_type_common_value_fields (stru { TYPE_TRANSPARENT_AGGR (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_FINAL_P (expr) = (unsigned) bp_unpack_value (bp, 1); + TYPE_CXX_ODR_P (expr) = (unsigned) bp_unpack_value (bp, 1); } else if (TREE_CODE (expr) == ARRAY_TYPE) TYPE_NONALIASED_COMPONENT (expr) = (unsigned) bp_unpack_value (bp, 1); + if (TREE_CODE (expr) == ARRAY_TYPE || TREE_CODE (expr) == INTEGER_TYPE) + TYPE_STRING_FLAG (expr) = (unsigned) bp_unpack_value (bp, 1); if (AGGREGATE_TYPE_P (expr)) TYPE_TYPELESS_STORAGE (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_EMPTY_P (expr) = (unsigned) bp_unpack_value (bp, 1); Index: tree-streamer-out.c =================================================================== --- tree-streamer-out.c (revision 272353) +++ tree-streamer-out.c (working copy) @@ -319,7 +319,6 @@ pack_ts_type_common_value_fields (struct not necessary valid in a global context. Use the raw value previously set by layout_type. */ bp_pack_machine_mode (bp, TYPE_MODE_RAW (expr)); - bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1); /* TYPE_NO_FORCE_BLK is private to stor-layout and need no streaming. */ bp_pack_value (bp, TYPE_PACKED (expr), 1); @@ -333,9 +332,12 @@ pack_ts_type_common_value_fields (struct { bp_pack_value (bp, TYPE_TRANSPARENT_AGGR (expr), 1); bp_pack_value (bp, TYPE_FINAL_P (expr), 1); + bp_pack_value (bp, TYPE_CXX_ODR_P (expr), 1); } else if (TREE_CODE (expr) == ARRAY_TYPE) bp_pack_value (bp, TYPE_NONALIASED_COMPONENT (expr), 1); + if (TREE_CODE (expr) == ARRAY_TYPE || TREE_CODE (expr) == INTEGER_TYPE) + bp_pack_value (bp, TYPE_STRING_FLAG (expr), 1); if (AGGREGATE_TYPE_P (expr)) bp_pack_value (bp, TYPE_TYPELESS_STORAGE (expr), 1); bp_pack_value (bp, TYPE_EMPTY_P (expr), 1); Index: tree.c =================================================================== --- tree.c (revision 272353) +++ tree.c (working copy) @@ -5598,6 +5598,9 @@ need_assembler_name_p (tree decl) && decl == TYPE_NAME (TREE_TYPE (decl)) && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TREE_TYPE (decl) && !TYPE_ARTIFICIAL (TREE_TYPE (decl)) + && ((TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE + && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE) + || TYPE_CXX_ODR_P (TREE_TYPE (decl))) && (type_with_linkage_p (TREE_TYPE (decl)) || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE) && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)) @@ -13881,7 +13884,10 @@ verify_type_variant (const_tree t, tree Ada also builds variants of types with different TYPE_CONTEXT. */ if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0) verify_variant_match (TYPE_CONTEXT); - verify_variant_match (TYPE_STRING_FLAG); + if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == INTEGER_TYPE) + verify_variant_match (TYPE_STRING_FLAG); + if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == UNION_TYPE) + verify_variant_match (TYPE_CXX_ODR_P); if (TYPE_ALIAS_SET_KNOWN_P (t)) { error ("type variant with %<TYPE_ALIAS_SET_KNOWN_P%>"); @@ -14627,12 +14633,6 @@ verify_type (const_tree t) error ("%<TYPE_CACHED_VALUES_P%> is set while it should not be"); error_found = true; } - if (TYPE_STRING_FLAG (t) - && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE) - { - error ("%<TYPE_STRING_FLAG%> is set on wrong type code"); - error_found = true; - } /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns Index: tree.h =================================================================== --- tree.h (revision 272353) +++ tree.h (working copy) @@ -439,6 +439,8 @@ extern void omp_clause_range_check_faile TREE_CHECK3 (T, RECORD_TYPE, UNION_TYPE, QUAL_UNION_TYPE) #define NOT_RECORD_OR_UNION_CHECK(T) \ TREE_NOT_CHECK3 (T, RECORD_TYPE, UNION_TYPE, QUAL_UNION_TYPE) +#define ARRAY_OR_INTEGER_TYPE_CHECK(T) \ + TREE_CHECK2 (T, ARRAY_TYPE, INTEGER_TYPE) #define NUMERICAL_TYPE_CHECK(T) \ TREE_CHECK5 (T, INTEGER_TYPE, ENUMERAL_TYPE, BOOLEAN_TYPE, REAL_TYPE, \ @@ -2118,7 +2120,14 @@ extern machine_mode vector_type_mode (co /* If set in an ARRAY_TYPE, indicates a string type (for languages that distinguish string from array of char). If set in a INTEGER_TYPE, indicates a character type. */ -#define TYPE_STRING_FLAG(NODE) (TYPE_CHECK (NODE)->type_common.string_flag) +#define TYPE_STRING_FLAG(NODE) \ + (ARRAY_OR_INTEGER_TYPE_CHECK (NODE)->type_common.string_flag) + +/* If set for RECORD_TYPE or UNION_TYPE it indicates that the type conforms + to the C++ one definition rule. This is used for LTO canonical type + computation. */ +#define TYPE_CXX_ODR_P(NODE) \ + (RECORD_OR_UNION_CHECK (NODE)->type_common.string_flag) /* Nonzero in a VECTOR_TYPE if the frontends should not emit warnings about missing conversions to other vector types of the same size. */