LCOV - code coverage report
Current view: top level - gcc/cp - rtti.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.4 % 782 754
Test Date: 2026-07-11 15:47:05 Functions: 100.0 % 34 34
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* RunTime Type Identification
       2              :    Copyright (C) 1995-2026 Free Software Foundation, Inc.
       3              :    Mostly written by Jason Merrill (jason@cygnus.com).
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify
       8              : it under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful,
      13              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15              : GNU General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "target.h"
      25              : #include "cp-tree.h"
      26              : #include "memmodel.h"
      27              : #include "tm_p.h"
      28              : #include "stringpool.h"
      29              : #include "intl.h"
      30              : #include "stor-layout.h"
      31              : #include "c-family/c-pragma.h"
      32              : #include "gcc-rich-location.h"
      33              : 
      34              : /* C++ returns type information to the user in struct type_info
      35              :    objects. We also use type information to implement dynamic_cast and
      36              :    exception handlers. Type information for a particular type is
      37              :    indicated with an ABI defined structure derived from type_info.
      38              :    This would all be very straight forward, but for the fact that the
      39              :    runtime library provides the definitions of the type_info structure
      40              :    and the ABI defined derived classes. We cannot build declarations
      41              :    of them directly in the compiler, but we need to layout objects of
      42              :    their type.  Somewhere we have to lie.
      43              : 
      44              :    We define layout compatible POD-structs with compiler-defined names
      45              :    and generate the appropriate initializations for them (complete
      46              :    with explicit mention of their vtable). When we have to provide a
      47              :    type_info to the user we reinterpret_cast the internal compiler
      48              :    type to type_info.  A well formed program can only explicitly refer
      49              :    to the type_infos of complete types (& cv void).  However, we chain
      50              :    pointer type_infos to the pointed-to-type, and that can be
      51              :    incomplete.  We only need the addresses of such incomplete
      52              :    type_info objects for static initialization.
      53              : 
      54              :    The type information VAR_DECL of a type is held on the
      55              :    get_global_binding of the type's mangled name. That VAR_DECL
      56              :    will be the internal type.  It will usually have the correct
      57              :    internal type reflecting the kind of type it represents (pointer,
      58              :    array, function, class, inherited class, etc).  When the type it
      59              :    represents is incomplete, it will have the internal type
      60              :    corresponding to type_info.  That will only happen at the end of
      61              :    translation, when we are emitting the type info objects.  */
      62              : 
      63              : /* Auxiliary data we hold for each type_info derived object we need.  */
      64              : struct GTY (()) tinfo_s {
      65              :   tree type;  /* The (const-qualified) RECORD_TYPE for this type_info object */
      66              : 
      67              :   tree vtable; /* The VAR_DECL of the vtable.  Only filled at end of
      68              :                   translation.  */
      69              : 
      70              :   tree name;  /* IDENTIFIER_NODE for the ABI specified name of
      71              :                  the type_info derived type.  */
      72              : };
      73              : 
      74              : 
      75              : enum tinfo_kind
      76              : {
      77              :   TK_TYPE_INFO_TYPE,    /* abi::__type_info_pseudo */
      78              :   TK_BASE_TYPE,         /* abi::__base_class_type_info */
      79              :   TK_DERIVED_TYPES,     /* Start of types derived from abi::__type_info  */
      80              :   TK_BUILTIN_TYPE = TK_DERIVED_TYPES,   /* abi::__fundamental_type_info */
      81              :   TK_ARRAY_TYPE,        /* abi::__array_type_info */
      82              :   TK_FUNCTION_TYPE,     /* abi::__function_type_info */
      83              :   TK_ENUMERAL_TYPE,     /* abi::__enum_type_info */
      84              :   TK_POINTER_TYPE,      /* abi::__pointer_type_info */
      85              :   TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
      86              :   TK_CLASS_TYPE,        /* abi::__class_type_info */
      87              :   TK_SI_CLASS_TYPE,     /* abi::__si_class_type_info */
      88              :   TK_VMI_CLASS_TYPES,   /* abi::__vmi_class_type_info<int> */
      89              :   TK_MAX
      90              : };
      91              : 
      92              : /* Names of the tinfo types.  Must be same order as TK enumeration
      93              :    above.  */
      94              : 
      95              : static const char *const tinfo_names[TK_MAX] =
      96              : {
      97              :   "__type_info",
      98              :   "__base_class_type_info",
      99              :   "__fundamental_type_info",
     100              :   "__array_type_info",
     101              :   "__function_type_info",
     102              :   "__enum_type_info",
     103              :   "__pointer_type_info",
     104              :   "__pointer_to_member_type_info",
     105              :   "__class_type_info",
     106              :   "__si_class_type_info",
     107              :   "__vmi_class_type_info"
     108              : };
     109              : 
     110              : /* Helper macro to get maximum scalar-width of pointer or of the 'long'-type.
     111              :    This of interest for llp64 targets.  */
     112              : #define LONGPTR_T \
     113              :   integer_types[(POINTER_SIZE <= TYPE_PRECISION (integer_types[itk_long]) \
     114              :                  ? itk_long : itk_long_long)]
     115              : 
     116              : /* A vector of all tinfo decls that haven't yet been emitted.  */
     117              : vec<tree, va_gc> *unemitted_tinfo_decls;
     118              : 
     119              : /* A vector of all type_info derived types we need.  The first few are
     120              :    fixed and created early. The remainder are for multiple inheritance
     121              :    and are generated as needed. */
     122              : static GTY (()) vec<tinfo_s, va_gc> *tinfo_descs;
     123              : 
     124              : static tree tinfo_name (tree, bool);
     125              : static tree build_dynamic_cast_1 (location_t, tree, tree, tsubst_flags_t);
     126              : static tree throw_bad_cast (void);
     127              : static tree throw_bad_typeid (void);
     128              : static bool typeid_ok_p (void);
     129              : static int qualifier_flags (tree);
     130              : static bool target_incomplete_p (tree);
     131              : static tree tinfo_base_init (tinfo_s *, tree);
     132              : static tree generic_initializer (tinfo_s *, tree);
     133              : static tree ptr_initializer (tinfo_s *, tree);
     134              : static tree ptm_initializer (tinfo_s *, tree);
     135              : static tree class_initializer (tinfo_s *, tree, unsigned, ...);
     136              : static tree get_pseudo_ti_init (tree, unsigned);
     137              : static unsigned get_pseudo_ti_index (tree);
     138              : static tinfo_s *get_tinfo_desc (unsigned);
     139              : static void create_tinfo_types (void);
     140              : static bool typeinfo_in_lib_p (tree);
     141              : 
     142              : static int doing_runtime = 0;
     143              : 
     144              : /* Create the internal versions of the ABI types.  */
     145              : 
     146              : void
     147       100241 : init_rtti_processing (void)
     148              : {
     149       100241 :   vec_alloc (unemitted_tinfo_decls, 124);
     150              : 
     151       100241 :   create_tinfo_types ();
     152       100241 : }
     153              : 
     154              : /* Given the expression EXP of type `class *', return the head of the
     155              :    object pointed to by EXP with type cv void*, if the class has any
     156              :    virtual functions (TYPE_POLYMORPHIC_P), else just return the
     157              :    expression.  */
     158              : 
     159              : tree
     160          526 : build_headof (tree exp)
     161              : {
     162          526 :   tree type = TREE_TYPE (exp);
     163          526 :   tree offset;
     164          526 :   tree index;
     165              : 
     166          526 :   gcc_assert (TYPE_PTR_P (type));
     167          526 :   type = TREE_TYPE (type);
     168              : 
     169          526 :   if (!CLASS_TYPE_P (type) || !TYPE_POLYMORPHIC_P (type))
     170              :     return exp;
     171              : 
     172              :   /* We use this a couple of times below, protect it.  */
     173          502 :   exp = save_expr (exp);
     174              : 
     175              :   /* The offset-to-top field is at index -2 from the vptr.  */
     176          502 :   index = build_int_cst (NULL_TREE,
     177              :                          -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
     178              : 
     179          502 :   offset = build_vtbl_ref (cp_build_fold_indirect_ref (exp),
     180              :                            index);
     181              : 
     182          502 :   cp_build_qualified_type (ptr_type_node,
     183          502 :                            cp_type_quals (TREE_TYPE (exp)));
     184          502 :   return fold_build_pointer_plus (exp, offset);
     185              : }
     186              : 
     187              : /* Get a bad_cast node for the program to throw...
     188              : 
     189              :    See 'libstdc++-v3/libsupc++/eh_aux_runtime.cc' for '__cxa_bad_cast'.  */
     190              : 
     191              : static tree
     192          229 : throw_bad_cast (void)
     193              : {
     194          229 :   static tree fn;
     195          229 :   if (!fn)
     196              :     {
     197           94 :       tree name = get_identifier ("__cxa_bad_cast");
     198           94 :       fn = get_global_binding (name);
     199           94 :       if (!fn)
     200           94 :         fn = push_throw_library_fn
     201           94 :           (name, build_function_type_list (void_type_node, NULL_TREE));
     202              :     }
     203              : 
     204          229 :   return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
     205              : }
     206              : 
     207              : /* See 'libstdc++-v3/libsupc++/eh_aux_runtime.cc' for '__cxa_bad_typeid'.  */
     208              : 
     209              : static tree
     210          134 : throw_bad_typeid (void)
     211              : {
     212          134 :   static tree fn;
     213          134 :   if (!fn)
     214              :     {
     215           54 :       tree name = get_identifier ("__cxa_bad_typeid");
     216           54 :       fn = get_global_binding (name);
     217           54 :       if (!fn)
     218           54 :         fn = push_throw_library_fn
     219           54 :           (name, build_function_type_list (void_type_node, NULL_TREE));
     220              :     }
     221              : 
     222          134 :   return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
     223              : }
     224              : 
     225              : /* const type_info*.  */
     226              : 
     227              : inline tree
     228        85573 : type_info_ptr_type ()
     229              : {
     230        85573 :   return build_pointer_type (const_type_info_type_node);
     231              : }
     232              : 
     233              : /* Return a pointer to a type_info object describing TYPE, suitably
     234              :    cast to the language defined type (for typeid) or void (for building
     235              :    up the descriptors).  */
     236              : 
     237              : static tree
     238       465216 : get_tinfo_ptr (tree type, bool voidp = false)
     239              : {
     240       465216 :   tree decl = get_tinfo_decl (type);
     241       465216 :   mark_used (decl);
     242              : 
     243       465216 :   tree ptype = voidp ? const_ptr_type_node : type_info_ptr_type ();
     244       465216 :   return build_nop (ptype, build_address (decl));
     245              : }
     246              : static inline tree
     247       380065 : get_void_tinfo_ptr (tree type)
     248              : {
     249       380065 :   return get_tinfo_ptr (type, true);
     250              : }
     251              : 
     252              : /* Return an lvalue expression whose type is "const std::type_info"
     253              :    and whose value indicates the type of the expression EXP.  If EXP
     254              :    is a reference to a polymorphic class, return the dynamic type;
     255              :    otherwise return the static type of the expression.  */
     256              : 
     257              : static tree
     258         1110 : get_tinfo_ptr_dynamic (tree exp, tsubst_flags_t complain)
     259              : {
     260         1110 :   tree type;
     261         1110 :   tree t;
     262              : 
     263         1110 :   if (error_operand_p (exp))
     264            3 :     return error_mark_node;
     265              : 
     266         1107 :   exp = resolve_nondeduced_context (exp, complain);
     267              : 
     268              :   /* Peel back references, so they match.  */
     269         1107 :   type = non_reference (unlowered_expr_type (exp));
     270              : 
     271              :   /* Peel off cv qualifiers.  */
     272         1107 :   type = cv_unqualified (type);
     273              : 
     274              :   /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
     275         1107 :   if (CLASS_TYPE_P (type) || type == unknown_type_node
     276         1500 :       || type == init_list_type_node)
     277          714 :     type = complete_type_or_maybe_complain (type, exp, complain);
     278              : 
     279         1107 :   if (!type)
     280           12 :     return error_mark_node;
     281              : 
     282              :   /* If exp is a reference to polymorphic type, get the real type_info.  */
     283          729 :   if (CLASS_TYPE_P (type)
     284          702 :       && TYPE_POLYMORPHIC_P (type)
     285         1574 :       && ! resolves_to_fixed_type_p (exp, 0))
     286              :     {
     287              :       /* build reference to type_info from vtable.  */
     288          422 :       tree index;
     289              : 
     290              :       /* The RTTI information is at index -1.  */
     291          422 :       index = build_int_cst (NULL_TREE,
     292              :                              -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
     293          422 :       t = build_vtbl_ref (exp, index);
     294          422 :       t = convert (type_info_ptr_type (), t);
     295              :     }
     296              :   else
     297              :     /* Otherwise return the type_info for the static type of the expr.  */
     298          673 :     t = get_tinfo_ptr (type);
     299              : 
     300              :   return t;
     301              : }
     302              : 
     303              : static bool
     304       229667 : typeid_ok_p (void)
     305              : {
     306       229667 :   if (! flag_rtti)
     307              :     {
     308            3 :       error ("cannot use %<typeid%> with %<-fno-rtti%>");
     309            3 :       return false;
     310              :     }
     311              : 
     312       229664 :   if (!const_type_info_type_node)
     313              :     {
     314        13340 :       tree name = get_identifier ("type_info");
     315        13340 :       tree decl = lookup_qualified_name (std_node, name);
     316        13340 :       if (TREE_CODE (decl) != TYPE_DECL)
     317              :         {
     318            3 :           gcc_rich_location richloc (input_location);
     319            3 :           maybe_add_include_fixit (&richloc, "<typeinfo>", false);
     320            3 :           error_at (&richloc,
     321              :                     "must %<#include <typeinfo>%> before using"
     322              :                     " %<typeid%>");
     323              : 
     324            3 :           return false;
     325            3 :         }
     326        13337 :       const_type_info_type_node
     327        13337 :         = cp_build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_CONST);
     328              :     }
     329              : 
     330       229661 :   tree pseudo = TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
     331       229661 :   tree real = TYPE_MAIN_VARIANT (const_type_info_type_node);
     332              : 
     333              :   /* Make sure abi::__type_info_pseudo has the same alias set
     334              :      as std::type_info.  */
     335       229661 :   if (! TYPE_ALIAS_SET_KNOWN_P (pseudo))
     336        13337 :     TYPE_ALIAS_SET (pseudo) = get_alias_set (real);
     337              :   else
     338       216324 :     gcc_assert (TYPE_ALIAS_SET (pseudo) == get_alias_set (real));
     339              : 
     340              :   return true;
     341              : }
     342              : 
     343              : /* True if EXP is a glvalue expression of polymorphic class type whose
     344              :    dynamic type is not known statically, so that typeid (EXP) must be
     345              :    evaluated per ([expr.typeid]/4).  */
     346              : 
     347              : bool
     348         2214 : typeid_evaluated_p (tree exp)
     349              : {
     350         2214 :   if (exp == error_mark_node)
     351              :     return false;
     352         2214 :   tree t = TREE_TYPE (exp);
     353         2214 :   if (!t || t == error_mark_node)
     354              :     return false;
     355         2208 :   if (TYPE_REF_P (t))
     356            0 :     t = TREE_TYPE (t);
     357         2208 :   if (TREE_CODE (t) != RECORD_TYPE && TREE_CODE (t) != UNION_TYPE)
     358              :     return false;
     359         1443 :   int nonnull = 0;
     360         1443 :   return (TYPE_POLYMORPHIC_P (t)
     361          950 :           && !resolves_to_fixed_type_p (exp, &nonnull)
     362              :           /* Only a glvalue operand is evaluated ([expr.typeid]/4).
     363              :              The following check is only necessary because
     364              :              resolves_to_fixed_type_p does not handle all
     365              :              prvalue cases such as COMPOUND_EXPR.  */
     366         2279 :           && glvalue_p (exp));
     367              : }
     368              : 
     369              : /* Return an expression for "typeid(EXP)".  The expression returned is
     370              :    an lvalue of type "const std::type_info".  */
     371              : 
     372              : tree
     373        12292 : build_typeid (tree exp, tsubst_flags_t complain)
     374              : {
     375        12292 :   tree cond = NULL_TREE, initial_expr = exp;
     376              : 
     377        12292 :   if (exp == error_mark_node || !typeid_ok_p ())
     378           32 :     return error_mark_node;
     379              : 
     380        12260 :   if (processing_template_decl)
     381        11150 :     return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
     382              : 
     383         1110 :   if (typeid_evaluated_p (exp))
     384              :     {
     385          421 :       int nonnull = 0;
     386          421 :       resolves_to_fixed_type_p (exp, &nonnull);
     387          421 :       if (!nonnull)
     388              :         {
     389              :           /* Make sure it isn't a null lvalue; evaluate it once.  */
     390          134 :           exp = cp_build_addr_expr (exp, complain);
     391          134 :           exp = save_expr (exp);
     392          134 :           cond = cp_convert (boolean_type_node, exp, complain);
     393          134 :           exp = cp_build_fold_indirect_ref (exp);
     394              :         }
     395              :     }
     396              : 
     397         1110 :   exp = get_tinfo_ptr_dynamic (exp, complain);
     398              : 
     399         1110 :   if (exp == error_mark_node)
     400              :     return error_mark_node;
     401              : 
     402         1089 :   if (cond)
     403              :     {
     404          134 :       tree bad = throw_bad_typeid ();
     405              : 
     406          134 :       exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
     407              :     }
     408              :   else
     409          955 :     mark_type_use (initial_expr);
     410              : 
     411         1089 :   return cp_build_fold_indirect_ref (exp);
     412              : }
     413              : 
     414              : /* Generate the NTBS name of a type.  If MARK_PRIVATE, put a '*' in front so that
     415              :    comparisons will be done by pointer rather than string comparison.  */
     416              : static tree
     417       444884 : tinfo_name (tree type, bool mark_private)
     418              : {
     419       444884 :   const char *name;
     420       444884 :   int length;
     421       444884 :   tree name_string;
     422              : 
     423       444884 :   name = mangle_type_string (type);
     424       444884 :   length = strlen (name);
     425              : 
     426       444884 :   if (mark_private)
     427              :     {
     428              :       /* Inject '*' at beginning of name to force pointer comparison.  */
     429          966 :       char* buf = (char*) XALLOCAVEC (char, length + 2);
     430          966 :       buf[0] = '*';
     431          966 :       memcpy (buf + 1, name, length + 1);
     432          966 :       name_string = build_string (length + 2, buf);
     433              :     }
     434              :   else
     435       443918 :     name_string = build_string (length + 1, name);
     436              : 
     437       444884 :   return fix_string_type (name_string);
     438              : }
     439              : 
     440              : /* Return a VAR_DECL for the internal ABI defined type_info object for
     441              :    TYPE. You must arrange that the decl is mark_used, if actually use
     442              :    it --- decls in vtables are only used if the vtable is output.  */
     443              : 
     444              : tree
     445      3319238 : get_tinfo_decl (tree type)
     446              : {
     447      3319238 :   if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
     448              :     {
     449            9 :       error ("cannot create type information for type %qT because "
     450              :              "it involves types of variable size",
     451              :              type);
     452            9 :       return error_mark_node;
     453              :     }
     454              : 
     455      3319229 :   if (TREE_CODE (type) == METHOD_TYPE)
     456           47 :     type = build_function_type (TREE_TYPE (type),
     457           47 :                                 TREE_CHAIN (TYPE_ARG_TYPES (type)));
     458              : 
     459      3319229 :   return get_tinfo_decl_direct (type, NULL, -1);
     460              : }
     461              : 
     462              : /* Get or create a tinfo VAR_DECL directly from the provided information.
     463              :    The caller must have already checked it is valid to do so.  */
     464              : 
     465              : tree
     466      3322321 : get_tinfo_decl_direct (tree type, tree name, int pseudo_ix)
     467              : {
     468              :   /* For a class type, the variable is cached in the type node
     469              :      itself.  */
     470      3322321 :   tree d = NULL_TREE;
     471              : 
     472      3322321 :   gcc_checking_assert (TREE_CODE (type) != METHOD_TYPE);
     473              : 
     474      3322321 :   if (CLASS_TYPE_P (type))
     475      6606500 :     d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
     476              : 
     477      3322321 :   if (!name)
     478      3319229 :     name = mangle_typeinfo_for_type (type);
     479              : 
     480      3322321 :   if (!CLASS_TYPE_P (type) || TYPE_TRANSPARENT_AGGR (type))
     481        19110 :     d = get_global_binding (name);
     482              : 
     483      3322321 :   if (!d)
     484              :     {
     485              :       /* Create it.  */
     486      1910903 :       if (pseudo_ix < 0)
     487      1909268 :         pseudo_ix = get_pseudo_ti_index (type);
     488              : 
     489      1910903 :       const tinfo_s *ti = get_tinfo_desc (pseudo_ix);
     490              : 
     491      1910903 :       d = build_lang_decl (VAR_DECL, name, ti->type);
     492      1910903 :       SET_DECL_ASSEMBLER_NAME (d, name);
     493              :       /* Remember the type it is for.  */
     494      1910903 :       TREE_TYPE (name) = type;
     495      1910903 :       DECL_TINFO_P (d) = 1;
     496      1910903 :       DECL_ARTIFICIAL (d) = 1;
     497      1910903 :       DECL_IGNORED_P (d) = 1;
     498      1910903 :       TREE_READONLY (d) = 1;
     499      1910903 :       TREE_STATIC (d) = 1;
     500      1910903 :       TREE_ADDRESSABLE (d) = 1;
     501              :       /* Tell equal_address_to that different tinfo decls never
     502              :          overlap.  */
     503      1910903 :       if (vec_safe_is_empty (unemitted_tinfo_decls))
     504        22848 :         DECL_ATTRIBUTES (d)
     505        45696 :           = build_tree_list (get_identifier ("non overlapping"),
     506              :                              NULL_TREE);
     507              :       else
     508      3776110 :         DECL_ATTRIBUTES (d)
     509      1888055 :           = DECL_ATTRIBUTES ((*unemitted_tinfo_decls)[0]);
     510              : 
     511              :       /* Mark the variable as undefined -- but remember that we can
     512              :          define it later if we need to do so.  */
     513      1910903 :       DECL_EXTERNAL (d) = 1;
     514      1910903 :       DECL_NOT_REALLY_EXTERN (d) = 1;
     515      1910903 :       set_linkage_according_to_type (type, d);
     516              : 
     517      1910903 :       d = pushdecl_top_level_and_finish (d, NULL_TREE);
     518      1910903 :       if (CLASS_TYPE_P (type))
     519      1897501 :         SET_CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type), d);
     520              : 
     521              :       /* Add decl to the global array of tinfo decls.  */
     522      1910903 :       vec_safe_push (unemitted_tinfo_decls, d);
     523              :     }
     524              : 
     525      3322321 :   return d;
     526              : }
     527              : 
     528              : /* Return the type_info object for TYPE.  */
     529              : 
     530              : tree
     531       217410 : get_typeid (tree type, tsubst_flags_t complain)
     532              : {
     533       217410 :   if (type == error_mark_node || !typeid_ok_p ())
     534            9 :     return error_mark_node;
     535              : 
     536       217401 :   if (processing_template_decl)
     537       132920 :     return build_min (TYPEID_EXPR, const_type_info_type_node, type);
     538              : 
     539              :   /* If the type of the type-id is a reference type, the result of the
     540              :      typeid expression refers to a type_info object representing the
     541              :      referenced type.  */
     542        84481 :   type = non_reference (type);
     543              : 
     544              :   /* This is not one of the uses of a qualified function type in 8.3.5.  */
     545        84481 :   if (TREE_CODE (type) == FUNCTION_TYPE
     546        84481 :       && (type_memfn_quals (type) != TYPE_UNQUALIFIED
     547            8 :           || type_memfn_rqual (type) != REF_QUAL_NONE))
     548              :     {
     549            3 :       if (complain & tf_error)
     550            3 :         error ("%<typeid%> of qualified function type %qT", type);
     551            3 :       return error_mark_node;
     552              :     }
     553              : 
     554              :   /* The top-level cv-qualifiers of the lvalue expression or the type-id
     555              :      that is the operand of typeid are always ignored.  */
     556        84478 :   type = cv_unqualified (type);
     557              : 
     558              :   /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics.  */
     559        84478 :   if (CLASS_TYPE_P (type) || type == unknown_type_node
     560        94759 :       || type == init_list_type_node)
     561        74197 :     type = complete_type_or_maybe_complain (type, NULL_TREE, complain);
     562              : 
     563        84478 :   if (!type)
     564            0 :     return error_mark_node;
     565              : 
     566        84478 :   return cp_build_fold_indirect_ref (get_tinfo_ptr (type));
     567              : }
     568              : 
     569              : /* Check whether TEST is null before returning RESULT.  If TEST is used in
     570              :    RESULT, it must have previously had a save_expr applied to it.  */
     571              : 
     572              : tree
     573         8314 : build_if_nonnull (tree test, tree result, tsubst_flags_t complain)
     574              : {
     575         8314 :   tree null_ptr = cp_convert (TREE_TYPE (test), nullptr_node, complain);
     576         8314 :   tree cond = build2 (NE_EXPR, boolean_type_node, test, null_ptr);
     577              : 
     578              :   /* This is a compiler generated comparison, don't emit
     579              :      e.g. -Wnonnull-compare warning for it.  */
     580         8314 :   suppress_warning (cond, OPT_Wnonnull);
     581              : 
     582         8314 :   null_ptr = cp_convert (TREE_TYPE (result), nullptr_node, complain);
     583         8314 :   cond = build3 (COND_EXPR, TREE_TYPE (result), cond, result, null_ptr);
     584              : 
     585              :   /* Likewise, don't emit -Wnonnull for using the result to call
     586              :      a member function.  */
     587         8314 :   suppress_warning (cond, OPT_Wnonnull);
     588         8314 :   return cond;
     589              : }
     590              : 
     591              : /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
     592              :    paper.  */
     593              : 
     594              : static tree
     595         7010 : build_dynamic_cast_1 (location_t loc, tree type, tree expr,
     596              :                       tsubst_flags_t complain)
     597              : {
     598         7010 :   enum tree_code tc = TREE_CODE (type);
     599         7010 :   tree exprtype;
     600         7010 :   tree dcast_fn;
     601         7010 :   tree old_expr = expr;
     602         7010 :   const char *errstr = NULL;
     603              : 
     604              :   /* Save casted types in the function's used types hash table.  */
     605         7010 :   used_types_insert (type);
     606              : 
     607              :   /* T shall be a pointer or reference to a complete class type, or
     608              :      `pointer to cv void''.  */
     609         7010 :   switch (tc)
     610              :     {
     611         6658 :     case POINTER_TYPE:
     612         6658 :       if (VOID_TYPE_P (TREE_TYPE (type)))
     613              :         break;
     614              :       /* Fall through.  */
     615         6499 :     case REFERENCE_TYPE:
     616         6499 :       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
     617              :         {
     618            0 :           errstr = _("target is not pointer or reference to class");
     619            0 :           goto fail;
     620              :         }
     621         6499 :       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
     622              :         {
     623            0 :           errstr = _("target is not pointer or reference to complete type");
     624            0 :           goto fail;
     625              :         }
     626              :       break;
     627              : 
     628           12 :     default:
     629           12 :       errstr = _("target is not pointer or reference");
     630           12 :       goto fail;
     631              :     }
     632              : 
     633         6998 :   if (tc == POINTER_TYPE)
     634              :     {
     635         6658 :       expr = decay_conversion (expr, complain);
     636         6658 :       exprtype = TREE_TYPE (expr);
     637              : 
     638              :       /* If T is a pointer type, v shall be an rvalue of a pointer to
     639              :          complete class type, and the result is an rvalue of type T.  */
     640              : 
     641         6658 :       expr = mark_rvalue_use (expr);
     642              : 
     643         6658 :       if (!TYPE_PTR_P (exprtype))
     644              :         {
     645            0 :           errstr = _("source is not a pointer");
     646            0 :           goto fail;
     647              :         }
     648         6658 :       if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
     649              :         {
     650            3 :           errstr = _("source is not a pointer to class");
     651            3 :           goto fail;
     652              :         }
     653         6655 :       if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
     654              :         {
     655            0 :           errstr = _("source is a pointer to incomplete type");
     656            0 :           goto fail;
     657              :         }
     658              :     }
     659              :   else
     660              :     {
     661          340 :       expr = mark_lvalue_use (expr);
     662          340 :       exprtype = TREE_TYPE (expr);
     663              : 
     664              :       /* T is a reference type, v shall be an lvalue of a complete class
     665              :          type, and the result is an lvalue of the type referred to by T.  */
     666          340 :       if (! MAYBE_CLASS_TYPE_P (exprtype))
     667              :         {
     668            3 :           errstr = _("source is not of class type");
     669            3 :           goto fail;
     670              :         }
     671          337 :       if (!COMPLETE_TYPE_P (complete_type (exprtype)))
     672              :         {
     673            0 :           errstr = _("source is of incomplete class type");
     674            0 :           goto fail;
     675              :         }
     676              : 
     677          337 :       exprtype = cp_build_reference_type (exprtype, !lvalue_p (expr));
     678              :     }
     679              : 
     680              :   /* The dynamic_cast operator shall not cast away constness.  */
     681         6992 :   if (!at_least_as_qualified_p (TREE_TYPE (type),
     682         6992 :                                 TREE_TYPE (exprtype)))
     683              :     {
     684           12 :       errstr = _("conversion casts away constness");
     685           12 :       goto fail;
     686              :     }
     687              : 
     688              :   /* If *type is an unambiguous accessible base class of *exprtype,
     689              :      convert statically.  */
     690         6980 :   {
     691         6980 :     tree binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
     692              :                               ba_check, NULL, complain);
     693         6980 :     if (binfo)
     694          398 :       return build_static_cast (loc, type, expr, complain);
     695              :   }
     696              : 
     697              :   /* Apply trivial conversion T -> T& for dereferenced ptrs.  */
     698         6582 :   if (tc == REFERENCE_TYPE)
     699          229 :     expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
     700              :                                  LOOKUP_NORMAL, NULL_TREE, complain);
     701              : 
     702              :   /* Otherwise *exprtype must be a polymorphic class (have a vtbl).  */
     703         6582 :   if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
     704              :     {
     705         6579 :       tree expr1;
     706              :       /* if TYPE is `void *', return pointer to complete object.  */
     707         6579 :       if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
     708              :         {
     709              :           /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b.  */
     710          490 :           if (TREE_CODE (expr) == ADDR_EXPR
     711            0 :               && VAR_P (TREE_OPERAND (expr, 0))
     712          490 :               && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
     713            0 :             return build1 (NOP_EXPR, type, expr);
     714              : 
     715              :           /* Since expr is used twice below, save it.  */
     716          490 :           expr = save_expr (expr);
     717              : 
     718          490 :           expr1 = build_headof (expr);
     719          490 :           if (TREE_TYPE (expr1) != type)
     720          490 :             expr1 = build1 (NOP_EXPR, type, expr1);
     721          490 :           return build_if_nonnull (expr, expr1, complain);
     722              :         }
     723              :       else
     724              :         {
     725         6089 :           tree retval;
     726         6089 :           tree result, td2, td3;
     727         6089 :           tree elems[4];
     728         6089 :           tree static_type, target_type, boff;
     729              : 
     730              :           /* If we got here, we can't convert statically.  Therefore,
     731              :              dynamic_cast<D&>(b) (b an object) cannot succeed.  */
     732         6089 :           if (tc == REFERENCE_TYPE)
     733              :             {
     734          229 :               if (VAR_P (old_expr)
     735          229 :                   && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
     736              :                 {
     737            0 :                   tree expr = throw_bad_cast ();
     738            0 :                   if (complain & tf_warning)
     739            0 :                     warning_at (loc, 0,
     740              :                                 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
     741              :                                 type, old_expr);
     742              :                   /* Bash it to the expected type.  */
     743            0 :                   TREE_TYPE (expr) = type;
     744            0 :                   return expr;
     745              :                 }
     746              :             }
     747              :           /* Ditto for dynamic_cast<D*>(&b).  */
     748         5860 :           else if (TREE_CODE (expr) == ADDR_EXPR)
     749              :             {
     750            3 :               tree op = TREE_OPERAND (expr, 0);
     751            3 :               if (VAR_P (op)
     752            3 :                   && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
     753              :                 {
     754            3 :                   if (complain & tf_warning)
     755            3 :                     warning_at (loc, 0,
     756              :                                 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
     757              :                                 type, op);
     758            3 :                   retval = build_int_cst (type, 0);
     759            3 :                   return retval;
     760              :                 }
     761              :             }
     762              : 
     763              :           /* Use of dynamic_cast when -fno-rtti is prohibited.  */
     764         6086 :           if (!flag_rtti)
     765              :             {
     766            3 :               if (complain & tf_error)
     767            3 :                 error_at (loc,
     768              :                           "%<dynamic_cast%> not permitted with %<-fno-rtti%>");
     769            3 :               return error_mark_node;
     770              :             }
     771              : 
     772         6083 :           target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
     773         6083 :           static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
     774         6083 :           td2 = get_tinfo_decl (target_type);
     775         6083 :           if (!mark_used (td2, complain) && !(complain & tf_error))
     776            0 :             return error_mark_node;
     777         6083 :           td2 = cp_build_addr_expr (td2, complain);
     778         6083 :           td3 = get_tinfo_decl (static_type);
     779         6083 :           if (!mark_used (td3, complain) && !(complain & tf_error))
     780            0 :             return error_mark_node;
     781         6083 :           td3 = cp_build_addr_expr (td3, complain);
     782              : 
     783              :           /* Determine how T and V are related.  */
     784         6083 :           boff = dcast_base_hint (static_type, target_type);
     785              : 
     786              :           /* Since expr is used twice below, save it.  */
     787         6083 :           expr = save_expr (expr);
     788              : 
     789         6083 :           expr1 = expr;
     790         6083 :           if (tc == REFERENCE_TYPE)
     791          229 :             expr1 = cp_build_addr_expr (expr1, complain);
     792              : 
     793         6083 :           elems[0] = expr1;
     794         6083 :           elems[1] = td3;
     795         6083 :           elems[2] = td2;
     796         6083 :           elems[3] = boff;
     797              : 
     798         6083 :           dcast_fn = dynamic_cast_node;
     799         6083 :           if (!dcast_fn)
     800              :             {
     801         2786 :               unsigned flags = push_abi_namespace ();
     802         2786 :               tree tinfo_ptr = xref_tag (class_type,
     803              :                                          get_identifier ("__class_type_info"));
     804         2786 :               tinfo_ptr = cp_build_qualified_type (tinfo_ptr, TYPE_QUAL_CONST);
     805         2786 :               tinfo_ptr = build_pointer_type (tinfo_ptr);
     806              : 
     807         2786 :               const char *fn_name = "__dynamic_cast";
     808              :               /* void *() (void const *, __class_type_info const *,
     809              :                            __class_type_info const *, ptrdiff_t)  */
     810         2786 :               tree fn_type = (build_function_type_list
     811         2786 :                               (ptr_type_node, const_ptr_type_node,
     812              :                                tinfo_ptr, tinfo_ptr, ptrdiff_type_node,
     813              :                                NULL_TREE));
     814         2786 :               dcast_fn = (build_library_fn_ptr
     815         2786 :                           (fn_name, fn_type, ECF_LEAF | ECF_PURE | ECF_NOTHROW));
     816              :               /* As with __cxa_atexit in get_atexit_node.  */
     817         2786 :               DECL_CONTEXT (dcast_fn) = FROB_CONTEXT (current_namespace);
     818         2786 :               DECL_SOURCE_LOCATION (dcast_fn) = BUILTINS_LOCATION;
     819         2786 :               dcast_fn = pushdecl (dcast_fn, /*hiding=*/true);
     820         2786 :               pop_abi_namespace (flags);
     821         2786 :               dynamic_cast_node = dcast_fn;
     822              :             }
     823         6083 :           if (dcast_fn == error_mark_node)
     824              :             return error_mark_node;
     825         6080 :           result = build_cxx_call (dcast_fn, 4, elems, complain);
     826         6080 :           SET_EXPR_LOCATION (result, loc);
     827              : 
     828         6080 :           if (tc == REFERENCE_TYPE)
     829              :             {
     830          229 :               tree bad = throw_bad_cast ();
     831          229 :               tree neq;
     832              : 
     833          229 :               result = save_expr (result);
     834          229 :               neq = cp_truthvalue_conversion (result, complain);
     835          229 :               return cp_convert (type,
     836          229 :                                  build3 (COND_EXPR, TREE_TYPE (result),
     837          229 :                                          neq, result, bad), complain);
     838              :             }
     839              : 
     840              :           /* Now back to the type we want from a void*.  */
     841         5851 :           result = cp_convert (type, result, complain);
     842         5851 :           return build_if_nonnull (expr, result, complain);
     843              :         }
     844              :     }
     845              :   else
     846            3 :     errstr = _("source type is not polymorphic");
     847              : 
     848           33 :  fail:
     849           33 :   if (complain & tf_error)
     850           18 :     error_at (loc, "cannot %<dynamic_cast%> %qE (of type %q#T) "
     851              :               "to type %q#T (%s)",
     852           18 :               old_expr, TREE_TYPE (old_expr), type, errstr);
     853           33 :   return error_mark_node;
     854              : }
     855              : 
     856              : tree
     857        72284 : build_dynamic_cast (location_t loc, tree type, tree expr,
     858              :                     tsubst_flags_t complain)
     859              : {
     860        72284 :   tree r;
     861              : 
     862        72284 :   if (type == error_mark_node || expr == error_mark_node)
     863              :     return error_mark_node;
     864              : 
     865        72279 :   if (processing_template_decl)
     866              :     {
     867        65269 :       expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
     868        65269 :       TREE_SIDE_EFFECTS (expr) = 1;
     869        65269 :       r = convert_from_reference (expr);
     870        65269 :       protected_set_expr_location (r, loc);
     871        65269 :       return r;
     872              :     }
     873              : 
     874         7010 :   r = convert_from_reference (build_dynamic_cast_1 (loc, type, expr,
     875              :                                                     complain));
     876         7010 :   if (r != error_mark_node)
     877         6965 :     maybe_warn_about_useless_cast (loc, type, expr, complain);
     878         7010 :   protected_set_expr_location (r, loc);
     879         7010 :   return r;
     880              : }
     881              : 
     882              : /* Return the runtime bit mask encoding the qualifiers of TYPE.  */
     883              : 
     884              : static int
     885         1066 : qualifier_flags (tree type)
     886              : {
     887         1066 :   int flags = 0;
     888         1066 :   int quals = cp_type_quals (type);
     889              : 
     890         1066 :   if (quals & TYPE_QUAL_CONST)
     891          239 :     flags |= 1;
     892         1066 :   if (quals & TYPE_QUAL_VOLATILE)
     893            6 :     flags |= 2;
     894         1066 :   if (quals & TYPE_QUAL_RESTRICT)
     895            6 :     flags |= 4;
     896         1066 :   return flags;
     897              : }
     898              : 
     899              : /* Return true, if the pointer chain TYPE ends at an incomplete type, or
     900              :    contains a pointer to member of an incomplete class.  */
     901              : 
     902              : static bool
     903         1796 : target_incomplete_p (tree type)
     904              : {
     905         1998 :   while (true)
     906         1998 :     if (TYPE_PTRDATAMEM_P (type))
     907              :       {
     908           16 :         if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
     909              :           return true;
     910           10 :         type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
     911              :       }
     912         1982 :     else if (TYPE_PTR_P (type))
     913          192 :       type = TREE_TYPE (type);
     914              :     else
     915         1790 :       return !COMPLETE_OR_VOID_TYPE_P (type);
     916              : }
     917              : 
     918              : /* Returns true if TYPE involves an incomplete class type; in that
     919              :    case, typeinfo variables for TYPE should be emitted with internal
     920              :    linkage.  */
     921              : 
     922              : static bool
     923      6005769 : involves_incomplete_p (tree type)
     924              : {
     925      6005769 :   switch (TREE_CODE (type))
     926              :     {
     927          637 :     case POINTER_TYPE:
     928          637 :       return target_incomplete_p (TREE_TYPE (type));
     929              : 
     930           93 :     case OFFSET_TYPE:
     931           93 :     ptrmem:
     932           93 :       return
     933           93 :         (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
     934           93 :          || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
     935              : 
     936      6004921 :     case RECORD_TYPE:
     937      6004921 :       if (TYPE_PTRMEMFUNC_P (type))
     938           47 :         goto ptrmem;
     939              :       /* Fall through.  */
     940      6004876 :     case UNION_TYPE:
     941      6004876 :       if (!COMPLETE_TYPE_P (type))
     942              :         return true;
     943              :       /* Fall through.  */
     944              :     default:
     945              :       /* All other types do not involve incomplete class types.  */
     946              :       return false;
     947              :     }
     948              : }
     949              : 
     950              : /* Return a CONSTRUCTOR for the common part of the type_info objects. This
     951              :    is the vtable pointer and NTBS name.  The NTBS name is emitted as a
     952              :    comdat const char array, so it becomes a unique key for the type. Generate
     953              :    and emit that VAR_DECL here.  (We can't always emit the type_info itself
     954              :    as comdat, because of pointers to incomplete.) */
     955              : 
     956              : static tree
     957       444884 : tinfo_base_init (tinfo_s *ti, tree target)
     958              : {
     959       444884 :   tree init;
     960       444884 :   tree name_decl;
     961       444884 :   tree vtable_ptr;
     962       444884 :   vec<constructor_elt, va_gc> *v;
     963              : 
     964       444884 :   {
     965       444884 :     tree name_name, name_string;
     966              : 
     967              :     /* Generate the NTBS array variable.  */
     968       444884 :     tree name_type = build_cplus_array_type
     969       444884 :                      (cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST),
     970              :                      NULL_TREE);
     971              : 
     972              :     /* Determine the name of the variable -- and remember with which
     973              :        type it is associated.  */
     974       444884 :     name_name = mangle_typeinfo_string_for_type (target);
     975       444884 :     TREE_TYPE (name_name) = target;
     976              : 
     977       444884 :     name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
     978       444884 :     SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
     979       444884 :     DECL_ARTIFICIAL (name_decl) = 1;
     980       444884 :     DECL_IGNORED_P (name_decl) = 1;
     981       444884 :     TREE_READONLY (name_decl) = 1;
     982       444884 :     TREE_STATIC (name_decl) = 1;
     983       444884 :     DECL_EXTERNAL (name_decl) = 0;
     984       444884 :     DECL_TINFO_P (name_decl) = 1;
     985       444884 :     set_linkage_according_to_type (target, name_decl);
     986       444884 :     import_export_decl (name_decl);
     987       444884 :     name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
     988       444884 :     DECL_INITIAL (name_decl) = name_string;
     989       444884 :     mark_used (name_decl);
     990       444884 :     pushdecl_top_level_and_finish (name_decl, name_string);
     991              :   }
     992              : 
     993       444884 :   vtable_ptr = ti->vtable;
     994       444884 :   if (!vtable_ptr)
     995              :     {
     996        52286 :       int flags = push_abi_namespace ();
     997        52286 :       tree real_type = xref_tag (class_type, ti->name);
     998        52286 :       tree real_decl = TYPE_NAME (real_type);
     999        52286 :       DECL_SOURCE_LOCATION (real_decl) = BUILTINS_LOCATION;
    1000        52286 :       pop_abi_namespace (flags);
    1001              : 
    1002        52286 :       if (!COMPLETE_TYPE_P (real_type))
    1003              :         {
    1004              :           /* We never saw a definition of this type, so we need to
    1005              :              tell the compiler that this is an exported class, as
    1006              :              indeed all of the __*_type_info classes are.  */
    1007        51390 :           SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
    1008        51390 :           CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
    1009              :         }
    1010              : 
    1011        52286 :       vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
    1012        52286 :       vtable_ptr = cp_build_addr_expr (vtable_ptr, tf_warning_or_error);
    1013              : 
    1014              :       /* We need to point into the middle of the vtable.  */
    1015        52286 :       vtable_ptr = fold_build_pointer_plus
    1016              :         (vtable_ptr,
    1017              :          size_binop (MULT_EXPR,
    1018              :                      size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
    1019              :                      TYPE_SIZE_UNIT (vtable_entry_type)));
    1020              : 
    1021        52286 :       ti->vtable = vtable_ptr;
    1022              :     }
    1023              : 
    1024       444884 :   vec_alloc (v, 2);
    1025       444884 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr);
    1026       444884 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    1027              :                           decay_conversion (name_decl, tf_warning_or_error));
    1028              : 
    1029       444884 :   init = build_constructor (init_list_type_node, v);
    1030       444884 :   TREE_CONSTANT (init) = 1;
    1031       444884 :   TREE_STATIC (init) = 1;
    1032              : 
    1033       444884 :   return init;
    1034              : }
    1035              : 
    1036              : /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
    1037              :    information about the particular type_info derivation, which adds no
    1038              :    additional fields to the type_info base.  */
    1039              : 
    1040              : static tree
    1041          331 : generic_initializer (tinfo_s *ti, tree target)
    1042              : {
    1043          331 :   tree init = tinfo_base_init (ti, target);
    1044              : 
    1045          331 :   init = build_constructor_single (init_list_type_node, NULL_TREE, init);
    1046          331 :   TREE_CONSTANT (init) = 1;
    1047          331 :   TREE_STATIC (init) = 1;
    1048          331 :   return init;
    1049              : }
    1050              : 
    1051              : /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
    1052              :    TI provides information about the particular type_info derivation,
    1053              :    which adds target type and qualifier flags members to the type_info base.  */
    1054              : 
    1055              : static tree
    1056          973 : ptr_initializer (tinfo_s *ti, tree target)
    1057              : {
    1058          973 :   tree init = tinfo_base_init (ti, target);
    1059          973 :   tree to = TREE_TYPE (target);
    1060          973 :   int flags = qualifier_flags (to);
    1061          973 :   bool incomplete = target_incomplete_p (to);
    1062          973 :   vec<constructor_elt, va_gc> *v;
    1063          973 :   vec_alloc (v, 3);
    1064              : 
    1065          973 :   if (incomplete)
    1066           64 :     flags |= 8;
    1067          973 :   if (tx_safe_fn_type_p (to))
    1068              :     {
    1069            2 :       flags |= 0x20;
    1070            2 :       to = tx_unsafe_fn_variant (to);
    1071              :     }
    1072          973 :   if (flag_noexcept_type
    1073          735 :       && FUNC_OR_METHOD_TYPE_P (to)
    1074         1048 :       && TYPE_NOTHROW_P (to))
    1075              :     {
    1076            9 :       flags |= 0x40;
    1077            9 :       to = build_exception_variant (to, NULL_TREE);
    1078              :     }
    1079          973 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
    1080          973 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
    1081          973 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    1082              :                           get_void_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
    1083              : 
    1084          973 :   init = build_constructor (init_list_type_node, v);
    1085          973 :   TREE_CONSTANT (init) = 1;
    1086          973 :   TREE_STATIC (init) = 1;
    1087          973 :   return init;
    1088              : }
    1089              : 
    1090              : /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
    1091              :    TI provides information about the particular type_info derivation,
    1092              :    which adds class, target type and qualifier flags members to the type_info
    1093              :    base.  */
    1094              : 
    1095              : static tree
    1096           93 : ptm_initializer (tinfo_s *ti, tree target)
    1097              : {
    1098           93 :   tree init = tinfo_base_init (ti, target);
    1099           93 :   tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
    1100           93 :   tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
    1101           93 :   int flags = qualifier_flags (to);
    1102           93 :   bool incomplete = target_incomplete_p (to);
    1103           93 :   vec<constructor_elt, va_gc> *v;
    1104           93 :   vec_alloc (v, 4);
    1105              : 
    1106           93 :   if (incomplete)
    1107            6 :     flags |= 0x8;
    1108           93 :   if (!COMPLETE_TYPE_P (klass))
    1109            6 :     flags |= 0x10;
    1110           93 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
    1111           93 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
    1112           93 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    1113              :                           get_void_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
    1114           93 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, get_void_tinfo_ptr (klass));
    1115              : 
    1116           93 :   init = build_constructor (init_list_type_node, v);
    1117           93 :   TREE_CONSTANT (init) = 1;
    1118           93 :   TREE_STATIC (init) = 1;
    1119           93 :   return init;
    1120              : }
    1121              : 
    1122              : /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
    1123              :    TI provides information about the particular __class_type_info derivation,
    1124              :    which adds hint flags and N extra initializers to the type_info base.  */
    1125              : 
    1126              : static tree
    1127       443487 : class_initializer (tinfo_s *ti, tree target, unsigned n, ...)
    1128              : {
    1129       443487 :   tree init = tinfo_base_init (ti, target);
    1130       443487 :   va_list extra_inits;
    1131       443487 :   unsigned i;
    1132       443487 :   vec<constructor_elt, va_gc> *v;
    1133       443487 :   vec_alloc (v, n+1);
    1134              : 
    1135       443487 :   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
    1136       443487 :   va_start (extra_inits, n);
    1137       893519 :   for (i = 0; i < n; i++)
    1138       450032 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, va_arg (extra_inits, tree));
    1139       443487 :   va_end (extra_inits);
    1140              : 
    1141       443487 :   init = build_constructor (init_list_type_node, v);
    1142       443487 :   TREE_CONSTANT (init) = 1;
    1143       443487 :   TREE_STATIC (init) = 1;
    1144       443487 :   return init;
    1145              : }
    1146              : 
    1147              : /* Returns true if the typeinfo for type should be placed in
    1148              :    the runtime library.  */
    1149              : 
    1150              : static bool
    1151      6048349 : typeinfo_in_lib_p (tree type)
    1152              : {
    1153              :   /* The typeinfo objects for `T*' and `const T*' are in the runtime
    1154              :      library for simple types T.  */
    1155      6048349 :   if (TYPE_PTR_P (type)
    1156      6048349 :       && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
    1157         1161 :           || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
    1158         1828 :     type = TREE_TYPE (type);
    1159              : 
    1160      6048349 :   switch (TREE_CODE (type))
    1161              :     {
    1162              :     case INTEGER_TYPE:
    1163              :     case BOOLEAN_TYPE:
    1164              :     case REAL_TYPE:
    1165              :     case VOID_TYPE:
    1166              :     case NULLPTR_TYPE:
    1167              :       return true;
    1168              : 
    1169      6005769 :     case LANG_TYPE:
    1170              :       /* fall through.  */
    1171              : 
    1172      6005769 :     default:
    1173      6005769 :       return false;
    1174              :     }
    1175              : }
    1176              : 
    1177              : /* Generate the initializer for the type info describing TYPE.  TK_INDEX is
    1178              :    the index of the descriptor in the tinfo_desc vector. */
    1179              : 
    1180              : static tree
    1181       444884 : get_pseudo_ti_init (tree type, unsigned tk_index)
    1182              : {
    1183       444884 :   tinfo_s *ti = get_tinfo_desc (tk_index);
    1184              : 
    1185       444884 :   gcc_assert (at_eof);
    1186       444884 :   switch (tk_index)
    1187              :     {
    1188           93 :     case TK_POINTER_MEMBER_TYPE:
    1189           93 :       return ptm_initializer (ti, type);
    1190              : 
    1191          973 :     case TK_POINTER_TYPE:
    1192          973 :       return ptr_initializer (ti, type);
    1193              : 
    1194          331 :     case TK_BUILTIN_TYPE:
    1195          331 :     case TK_ENUMERAL_TYPE:
    1196          331 :     case TK_FUNCTION_TYPE:
    1197          331 :     case TK_ARRAY_TYPE:
    1198          331 :       return generic_initializer (ti, type);
    1199              : 
    1200       110961 :     case TK_CLASS_TYPE:
    1201       110961 :       return class_initializer (ti, type, 0);
    1202              : 
    1203       273773 :     case TK_SI_CLASS_TYPE:
    1204       273773 :       {
    1205       273773 :         tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
    1206       273773 :         tree tinfo = get_void_tinfo_ptr (BINFO_TYPE (base_binfo));
    1207              : 
    1208              :         /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
    1209       273773 :         ti = &(*tinfo_descs)[tk_index];
    1210       273773 :         return class_initializer (ti, type, 1, tinfo);
    1211              :       }
    1212              : 
    1213        58753 :     default:
    1214        58753 :       {
    1215        58753 :         int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
    1216        58753 :                     | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
    1217        58753 :         tree binfo = TYPE_BINFO (type);
    1218        58753 :         unsigned nbases = BINFO_N_BASE_BINFOS (binfo);
    1219        58753 :         vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
    1220        58904 :         tree offset_type = LONGPTR_T;
    1221        58753 :         vec<constructor_elt, va_gc> *init_vec = NULL;
    1222              : 
    1223        58753 :         gcc_assert (tk_index - TK_VMI_CLASS_TYPES + 1 == nbases);
    1224              : 
    1225        58753 :         vec_safe_grow (init_vec, nbases, true);
    1226              :         /* Generate the base information initializer.  */
    1227       163886 :         for (unsigned ix = nbases; ix--;)
    1228              :           {
    1229       105133 :             tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
    1230       105133 :             int flags = 0;
    1231       105133 :             tree tinfo;
    1232       105133 :             tree offset;
    1233       105133 :             vec<constructor_elt, va_gc> *v;
    1234              : 
    1235       105133 :             if ((*base_accesses)[ix] == access_public_node)
    1236       102907 :               flags |= 2;
    1237       105133 :             tinfo = get_void_tinfo_ptr (BINFO_TYPE (base_binfo));
    1238       105133 :             if (BINFO_VIRTUAL_P (base_binfo))
    1239              :               {
    1240              :                 /* We store the vtable offset at which the virtual
    1241              :                    base offset can be found.  */
    1242         4616 :                 offset = BINFO_VPTR_FIELD (base_binfo);
    1243         4616 :                 flags |= 1;
    1244              :               }
    1245              :             else
    1246       100517 :               offset = BINFO_OFFSET (base_binfo);
    1247              : 
    1248              :             /* Combine offset and flags into one field.  */
    1249       105133 :             offset = fold_convert (offset_type, offset);
    1250       105133 :             offset = fold_build2_loc (input_location,
    1251              :                                   LSHIFT_EXPR, offset_type, offset,
    1252              :                                   build_int_cst (offset_type, 8));
    1253       105133 :             offset = fold_build2_loc (input_location,
    1254              :                                   BIT_IOR_EXPR, offset_type, offset,
    1255       105133 :                                   build_int_cst (offset_type, flags));
    1256       105133 :             vec_alloc (v, 2);
    1257       105133 :             CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo);
    1258       105133 :             CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset);
    1259       105133 :             tree base_init = build_constructor (init_list_type_node, v);
    1260       105133 :             constructor_elt *e = &(*init_vec)[ix];
    1261       105133 :             e->index = NULL_TREE;
    1262       105133 :             e->value = base_init;
    1263              :           }
    1264        58753 :         tree base_inits = build_constructor (init_list_type_node, init_vec);
    1265              : 
    1266              :         /* get_tinfo_ptr might have reallocated the tinfo_descs vector.  */
    1267        58753 :         ti = &(*tinfo_descs)[tk_index];
    1268        58753 :         return class_initializer (ti, type, 3,
    1269        58753 :                                   build_int_cst (NULL_TREE, hint),
    1270        58753 :                                   build_int_cst (NULL_TREE, nbases),
    1271              :                                   base_inits);
    1272              :       }
    1273              :     }
    1274              : }
    1275              : 
    1276              : /* Return the index of a pseudo type info type node used to describe
    1277              :    TYPE.  TYPE must be a complete type (or cv void), except at the end
    1278              :    of the translation unit.  */
    1279              : 
    1280              : static unsigned
    1281      2354152 : get_pseudo_ti_index (tree type)
    1282              : {
    1283      2354152 :   unsigned ix;
    1284              : 
    1285      2354152 :   switch (TREE_CODE (type))
    1286              :     {
    1287              :     case OFFSET_TYPE:
    1288              :       ix = TK_POINTER_MEMBER_TYPE;
    1289              :       break;
    1290              : 
    1291              :     case POINTER_TYPE:
    1292      2354152 :       ix = TK_POINTER_TYPE;
    1293              :       break;
    1294              : 
    1295           24 :     case ENUMERAL_TYPE:
    1296           24 :       ix = TK_ENUMERAL_TYPE;
    1297           24 :       break;
    1298              : 
    1299          232 :     case FUNCTION_TYPE:
    1300          232 :       ix = TK_FUNCTION_TYPE;
    1301          232 :       break;
    1302              : 
    1303           56 :     case ARRAY_TYPE:
    1304           56 :       ix = TK_ARRAY_TYPE;
    1305           56 :       break;
    1306              : 
    1307      2339459 :     case UNION_TYPE:
    1308      2339459 :     case RECORD_TYPE:
    1309      2339459 :       if (TYPE_PTRMEMFUNC_P (type))
    1310              :         ix = TK_POINTER_MEMBER_TYPE;
    1311      2339365 :       else if (!COMPLETE_TYPE_P (type))
    1312              :         {
    1313           55 :           if (!at_eof)
    1314            1 :             cxx_incomplete_type_error (NULL_TREE, type);
    1315              :           ix = TK_CLASS_TYPE;
    1316              :         }
    1317      2339310 :       else if (!TYPE_BINFO (type)
    1318      2339310 :                || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
    1319              :         ix = TK_CLASS_TYPE;
    1320              :       else
    1321              :         {
    1322      1967591 :           tree binfo = TYPE_BINFO (type);
    1323      1967591 :           vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
    1324      1967591 :           tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
    1325      1967591 :           int num_bases = BINFO_N_BASE_BINFOS (binfo);
    1326              : 
    1327      1967591 :           if (num_bases == 1
    1328      1727591 :               && (*base_accesses)[0] == access_public_node
    1329      1724089 :               && !BINFO_VIRTUAL_P (base_binfo)
    1330      3644317 :               && integer_zerop (BINFO_OFFSET (base_binfo)))
    1331              :             /* single non-virtual public.  */
    1332              :             ix = TK_SI_CLASS_TYPE;
    1333              :           else
    1334       310405 :             ix = TK_VMI_CLASS_TYPES + num_bases - 1;
    1335              :         }
    1336              :       break;
    1337              : 
    1338        12010 :     default:
    1339        12010 :       ix = TK_BUILTIN_TYPE;
    1340        12010 :       break;
    1341              :     }
    1342      2354152 :   return ix;
    1343              : }
    1344              : 
    1345              : /* Return pointer to tinfo descriptor.  Possibly creating the tinfo
    1346              :    descriptor in the first place.  */
    1347              : 
    1348              : static tinfo_s *
    1349      3135090 : get_tinfo_desc (unsigned ix)
    1350              : {
    1351      3135090 :   if (tinfo_descs->length () <= ix)
    1352              :     /* too short, extend.  */
    1353        54163 :     vec_safe_grow_cleared (tinfo_descs, ix + 1);
    1354              : 
    1355      3135090 :   tinfo_s *res = &(*tinfo_descs)[ix];
    1356              : 
    1357      3135090 :   if (res->type)
    1358              :     return res;
    1359              : 
    1360              :   /* Ok, we have to create it.  This layout must be consistent with
    1361              :      that defined in the runtime support.  We explicitly manage the
    1362              :      vtable member, and name it for real type as used in the runtime.
    1363              :      The RECORD type has a different name, to avoid collisions.  We
    1364              :      have to delay generating the VAR_DECL of the vtable until the end
    1365              :      of the translation, when we'll have seen the library definition,
    1366              :      if there was one.  */
    1367              : 
    1368              :   /* Fields to add, chained in reverse order.  */
    1369       113822 :   tree fields = NULL_TREE;
    1370              : 
    1371       113822 :   if (ix >= TK_DERIVED_TYPES)
    1372              :     {
    1373              :       /* First field is the pseudo type_info base class.  */
    1374        76708 :       tree fld_base = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
    1375        76708 :                                   get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
    1376              : 
    1377        76708 :       DECL_CHAIN (fld_base) = fields;
    1378        76708 :       fields = fld_base;
    1379              :     }
    1380              : 
    1381       113822 :   switch (ix)
    1382              :     {
    1383        22836 :     case TK_TYPE_INFO_TYPE:
    1384        22836 :       {
    1385        22836 :         tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1386              :                                    NULL_TREE, const_ptr_type_node);
    1387        22836 :         fields = fld_ptr;
    1388              : 
    1389        22836 :         tree fld_str = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1390              :                                    NULL_TREE, const_string_type_node);
    1391        22836 :         DECL_CHAIN (fld_str) = fields;
    1392        22836 :         fields = fld_str;
    1393        22836 :         break;
    1394              :       }
    1395              : 
    1396        14278 :     case TK_BASE_TYPE:
    1397        14278 :       {
    1398              :         /* Base class internal helper. Pointer to base type, offset to
    1399              :            base, flags.  */
    1400        14278 :         tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1401              :                                    NULL_TREE, const_ptr_type_node);
    1402        14278 :         DECL_CHAIN (fld_ptr) = fields;
    1403        14278 :         fields = fld_ptr;
    1404              : 
    1405        14278 :         tree fld_flag = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1406        14374 :                                     NULL_TREE, LONGPTR_T);
    1407        14278 :         DECL_CHAIN (fld_flag) = fields;
    1408        14278 :         fields = fld_flag;
    1409        14278 :         break;
    1410              :       }
    1411              : 
    1412              :     case TK_BUILTIN_TYPE:
    1413              :       /* Fundamental type_info */
    1414              :       break;
    1415              : 
    1416              :     case TK_ARRAY_TYPE:
    1417              :       break;
    1418              : 
    1419              :     case TK_FUNCTION_TYPE:
    1420              :       break;
    1421              : 
    1422              :     case TK_ENUMERAL_TYPE:
    1423              :       break;
    1424              : 
    1425          477 :     case TK_POINTER_TYPE:
    1426          477 :     case TK_POINTER_MEMBER_TYPE:
    1427          477 :       {
    1428              :         /* Pointer type_info. Adds two fields, qualification mask and
    1429              :            pointer to the pointed to type.  This is really a
    1430              :            descendant of __pbase_type_info.  */
    1431          477 :         tree fld_mask = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1432              :                                     NULL_TREE, integer_type_node);
    1433          477 :         DECL_CHAIN (fld_mask) = fields;
    1434          477 :         fields = fld_mask;
    1435              : 
    1436          477 :         tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1437              :                                    NULL_TREE, const_ptr_type_node);
    1438          477 :         DECL_CHAIN (fld_ptr) = fields;
    1439          477 :         fields = fld_ptr;
    1440              : 
    1441          477 :         if (ix == TK_POINTER_MEMBER_TYPE)
    1442              :           {
    1443              :             /* Add a pointer to the class too.  */
    1444           43 :             tree fld_cls = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1445              :                                    NULL_TREE, const_ptr_type_node);
    1446           43 :             DECL_CHAIN (fld_cls) = fields;
    1447           43 :             fields = fld_cls;
    1448              :           }
    1449              :         break;
    1450              :       }
    1451              : 
    1452              :     case TK_CLASS_TYPE:
    1453              :       /* Class type_info.  No additional fields.  */
    1454              :       break;
    1455              : 
    1456        17869 :     case TK_SI_CLASS_TYPE:
    1457        17869 :       {
    1458              :         /* Single public non-virtual base class. Add pointer to base
    1459              :            class.  This is really a descendant of
    1460              :            __class_type_info.  */
    1461        17869 :         tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1462              :                                    NULL_TREE, const_ptr_type_node);
    1463        17869 :         DECL_CHAIN (fld_ptr) = fields;
    1464        17869 :         fields = fld_ptr;
    1465        17869 :         break;
    1466              :       }
    1467              : 
    1468        26046 :     default: /* Multiple inheritance.  */
    1469        26046 :       {
    1470        26046 :         unsigned num_bases = ix - TK_VMI_CLASS_TYPES + 1;
    1471              : 
    1472        26046 :         tree fld_flg = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1473              :                                    NULL_TREE, integer_type_node);
    1474        26046 :         DECL_CHAIN (fld_flg) = fields;
    1475        26046 :         fields = fld_flg;
    1476              : 
    1477        26046 :         tree fld_cnt = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1478              :                                    NULL_TREE, integer_type_node);
    1479        26046 :         DECL_CHAIN (fld_cnt) = fields;
    1480        26046 :         fields = fld_cnt;
    1481              : 
    1482              :         /* Create the array of __base_class_type_info entries.  */
    1483        26046 :         tree domain = build_index_type (size_int (num_bases - 1));
    1484        26046 :         tree array = build_array_type (get_tinfo_desc (TK_BASE_TYPE)->type,
    1485              :                                        domain);
    1486        26046 :         tree fld_ary = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    1487              :                                    NULL_TREE, array);
    1488        26046 :         DECL_CHAIN (fld_ary) = fields;
    1489        26046 :         fields = fld_ary;
    1490        26046 :         break;
    1491              :       }
    1492              :     }
    1493              : 
    1494              :   /* Generate the pseudo type name.  */
    1495       113822 :   const char *real_name = tinfo_names[ix < TK_VMI_CLASS_TYPES
    1496       113822 :                                       ? ix : unsigned (TK_VMI_CLASS_TYPES)];
    1497       113822 :   size_t name_len = strlen (real_name);
    1498       113822 :   char *pseudo_name = (char *) alloca (name_len + 30);
    1499       113822 :   memcpy (pseudo_name, real_name, name_len);
    1500              :   /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well
    1501              :      apply it to all.  See get_peudo_tinfo_index where we make use of
    1502              :      this.  */
    1503       113822 :   sprintf (pseudo_name + name_len, "_pseudo_%d", ix);
    1504              : 
    1505              :   /* Create the pseudo type.  */
    1506       113822 :   tree pseudo_type = make_class_type (RECORD_TYPE);
    1507              :   /* Pass the fields chained in reverse.  */
    1508       113822 :   finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
    1509       113822 :   CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
    1510       113822 :   DECL_CONTEXT (TYPE_NAME (pseudo_type)) = FROB_CONTEXT (global_namespace);
    1511       113822 :   DECL_TINFO_P (TYPE_NAME (pseudo_type)) = true;
    1512       113822 :   xref_basetypes (pseudo_type, /*bases=*/NULL_TREE);
    1513              : 
    1514       113822 :   res->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
    1515       113822 :   res->name = get_identifier (real_name);
    1516              : 
    1517              :   /* Pretend this is public so determine_visibility doesn't give vtables
    1518              :      internal linkage.  */
    1519       113822 :   TREE_PUBLIC (TYPE_MAIN_DECL (res->type)) = 1;
    1520              : 
    1521       113822 :   return res;
    1522              : }
    1523              : 
    1524              : /* Return an identifying index for the pseudo type_info TYPE.
    1525              :    We wrote the index at the end of the name, so just scan it from
    1526              :    there.  This isn't critical, as it's only on the first use of this
    1527              :    type during module stream out.  */
    1528              : 
    1529              : unsigned
    1530        15589 : get_pseudo_tinfo_index (tree type)
    1531              : {
    1532        15589 :   tree name = DECL_NAME (TYPE_NAME (type));
    1533        15589 :   unsigned ix = 0, scale = 1;
    1534        15589 :   size_t len = IDENTIFIER_LENGTH (name);
    1535        15589 :   const char *ptr = IDENTIFIER_POINTER (name) + len;
    1536              : 
    1537        34023 :   for (; *--ptr != '_'; scale *= 10)
    1538              :     {
    1539        18434 :       len--;
    1540        18434 :       gcc_checking_assert (len && ISDIGIT (*ptr));
    1541        18434 :       ix += (*ptr - '0') * scale;
    1542              :     }
    1543              : 
    1544        15589 :   gcc_assert (len != IDENTIFIER_LENGTH (name));
    1545        15589 :   return ix;
    1546              : }
    1547              : 
    1548              : tree
    1549         2004 : get_pseudo_tinfo_type (unsigned ix)
    1550              : {
    1551         2004 :   return get_tinfo_desc (ix)->type;
    1552              : }
    1553              : 
    1554              : /* We lazily create the type info types.  */
    1555              : 
    1556              : static void
    1557       100241 : create_tinfo_types (void)
    1558              : {
    1559       100241 :   gcc_assert (!tinfo_descs);
    1560              : 
    1561       100241 :   vec_alloc (tinfo_descs, TK_MAX + 20);
    1562       100241 : }
    1563              : 
    1564              : /* Helper for emit_support_tinfos. Emits the type_info descriptor of
    1565              :    a single type.  */
    1566              : 
    1567              : void
    1568          183 : emit_support_tinfo_1 (tree bltn)
    1569              : {
    1570          183 :   tree types[3];
    1571              : 
    1572          183 :   if (bltn == NULL_TREE)
    1573            5 :     return;
    1574          178 :   types[0] = bltn;
    1575          178 :   types[1] = build_pointer_type (bltn);
    1576          178 :   types[2] = build_pointer_type (cp_build_qualified_type (bltn,
    1577              :                                                           TYPE_QUAL_CONST));
    1578              : 
    1579          712 :   for (int i = 0; i < 3; ++i)
    1580              :     {
    1581          534 :       tree tinfo = get_tinfo_decl (types[i]);
    1582          534 :       TREE_USED (tinfo) = 1;
    1583          534 :       mark_needed (tinfo);
    1584              :       /* The C++ ABI requires that these objects be COMDAT.  But,
    1585              :          On systems without weak symbols, initialized COMDAT
    1586              :          objects are emitted with internal linkage.  (See
    1587              :          comdat_linkage for details.)  Since we want these objects
    1588              :          to have external linkage so that copies do not have to be
    1589              :          emitted in code outside the runtime library, we make them
    1590              :          non-COMDAT here.
    1591              : 
    1592              :          It might also not be necessary to follow this detail of the
    1593              :          ABI.  */
    1594          534 :       if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
    1595              :         {
    1596            0 :           gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
    1597            0 :           DECL_INTERFACE_KNOWN (tinfo) = 1;
    1598              :         }
    1599              : 
    1600              :       /* Emit it right away if not emitted already.  */
    1601          534 :       if (DECL_INITIAL (tinfo) == NULL_TREE)
    1602              :         {
    1603          504 :           bool ok = emit_tinfo_decl (tinfo);
    1604          504 :           gcc_assert (ok);
    1605              :           /* When compiling libsupc++.a (fundamental_type_info.o),
    1606              :              unemitted_tinfo_decls->last () will be tinfo, so pop it
    1607              :              from the vector as it is emitted now.  If one uses typeid
    1608              :              etc. in the same TU as the definition of
    1609              :              ~fundamental_type_info (), the tinfo might be emitted
    1610              :              already earlier, in such case keep it in the vector
    1611              :              (as otherwise we'd need to walk the whole vector) and
    1612              :              let c_parse_final_cleanups ignore it when it will have
    1613              :              non-NULL DECL_INITIAL.  */
    1614          504 :           if (unemitted_tinfo_decls->last () == tinfo)
    1615          501 :             unemitted_tinfo_decls->pop ();
    1616              :         }
    1617              :     }
    1618              : }
    1619              : 
    1620              : /* Emit the type_info descriptors which are guaranteed to be in the runtime
    1621              :    support.  Generating them here guarantees consistency with the other
    1622              :    structures.  We use the following heuristic to determine when the runtime
    1623              :    is being generated.  If std::__fundamental_type_info is defined, and its
    1624              :    destructor is defined, then the runtime is being built.  */
    1625              : 
    1626              : void
    1627        98573 : emit_support_tinfos (void)
    1628              : {
    1629              :   /* Dummy static variable so we can put nullptr in the array; it will be
    1630              :      set before we actually start to walk the array.  */
    1631        98573 :   static tree *const fundamentals[] =
    1632              :   {
    1633              :     &void_type_node,
    1634              :     &boolean_type_node,
    1635              :     &wchar_type_node, &char8_type_node, &char16_type_node, &char32_type_node,
    1636              :     &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
    1637              :     &short_integer_type_node, &short_unsigned_type_node,
    1638              :     &integer_type_node, &unsigned_type_node,
    1639              :     &long_integer_type_node, &long_unsigned_type_node,
    1640              :     &long_long_integer_type_node, &long_long_unsigned_type_node,
    1641              :     &float_type_node, &double_type_node, &long_double_type_node,
    1642              :     &bfloat16_type_node, &float16_type_node, &float32_type_node,
    1643              :     &float64_type_node, &float128_type_node, &float32x_type_node,
    1644              :     &float64x_type_node, &float128x_type_node, &nullptr_type_node,
    1645              :     0
    1646              :   };
    1647              :   /* Similar, but for floating point types only which should get type info
    1648              :      regardless whether they are non-NULL or NULL.  */
    1649        98573 :   static tree *const fundamentals_with_fallback[] =
    1650              :   {
    1651              :     &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node,
    1652              :     0
    1653              :   };
    1654        98573 :   int ix;
    1655              : 
    1656              :   /* Look for a defined class.  */
    1657        98573 :   tree bltn_type = lookup_qualified_name
    1658        98573 :     (abi_node, "__fundamental_type_info", LOOK_want::TYPE, false);
    1659        98573 :   if (TREE_CODE (bltn_type) != TYPE_DECL)
    1660              :     return;
    1661              : 
    1662          377 :   bltn_type = TREE_TYPE (bltn_type);
    1663          377 :   if (!COMPLETE_TYPE_P (bltn_type))
    1664              :     return;
    1665          377 :   tree dtor = CLASSTYPE_DESTRUCTOR (bltn_type);
    1666          377 :   if (!dtor || DECL_EXTERNAL (dtor))
    1667              :     return;
    1668              : 
    1669              :   /* All these are really builtins.  So set the location.  */
    1670            5 :   location_t saved_loc = input_location;
    1671            5 :   input_location = BUILTINS_LOCATION;
    1672            5 :   doing_runtime = 1;
    1673            5 :   tree fallback = NULL_TREE;
    1674          150 :   for (ix = 0; fundamentals[ix]; ix++)
    1675          145 :     emit_support_tinfo_1 (*fundamentals[ix]);
    1676           20 :   for (ix = 0; fundamentals_with_fallback[ix]; ix++)
    1677           15 :     if (*fundamentals_with_fallback[ix])
    1678           15 :       emit_support_tinfo_1 (*fundamentals_with_fallback[ix]);
    1679              :     else
    1680              :       {
    1681            0 :         if (fallback == NULL_TREE)
    1682            0 :           fallback = make_node (REAL_TYPE);
    1683            0 :         *fundamentals_with_fallback[ix] = fallback;
    1684            0 :         emit_support_tinfo_1 (fallback);
    1685            0 :         *fundamentals_with_fallback[ix] = NULL_TREE;
    1686              :       }
    1687           10 :   for (ix = 0; ix < NUM_INT_N_ENTS; ix ++)
    1688            5 :     if (int_n_enabled_p[ix])
    1689              :       {
    1690            4 :         emit_support_tinfo_1 (int_n_trees[ix].signed_type);
    1691            4 :         emit_support_tinfo_1 (int_n_trees[ix].unsigned_type);
    1692              :       }
    1693           20 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
    1694           15 :     emit_support_tinfo_1 (TREE_VALUE (t));
    1695              : 
    1696              :   /* Emit additional typeinfos as requested by target.  */
    1697            5 :   targetm.emit_support_tinfos (emit_support_tinfo_1);
    1698              : 
    1699            5 :   input_location = saved_loc;
    1700              : }
    1701              : 
    1702              : /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
    1703              :    tinfo decl.  Determine whether it needs emitting, and if so
    1704              :    generate the initializer.  */
    1705              : 
    1706              : bool
    1707      6048349 : emit_tinfo_decl (tree decl)
    1708              : {
    1709      6048349 :   gcc_assert (DECL_TINFO_P (decl));
    1710              : 
    1711      6048349 :   tree type = TREE_TYPE (DECL_NAME (decl));
    1712      6048349 :   if (typeinfo_in_lib_p (type))
    1713              :     {
    1714        42580 :       if (doing_runtime)
    1715          504 :         DECL_EXTERNAL (decl) = 0;
    1716              :       else
    1717              :         {
    1718              :           /* If we're not in the runtime, then DECL (which is already
    1719              :              DECL_EXTERNAL) will not be defined here.  */
    1720        42076 :           DECL_INTERFACE_KNOWN (decl) = 1;
    1721        42076 :           return false;
    1722              :         }
    1723              :     }
    1724      6005769 :   else if (involves_incomplete_p (type))
    1725              :     {
    1726           99 :       if (!decl_needed_p (decl))
    1727              :         return false;
    1728              :       /* If TYPE involves an incomplete class type, then the typeinfo
    1729              :          object will be emitted with internal linkage.  There is no
    1730              :          way to know whether or not types are incomplete until the end
    1731              :          of the compilation, so this determination must be deferred
    1732              :          until this point.  */
    1733           99 :       TREE_PUBLIC (decl) = 0;
    1734           99 :       DECL_EXTERNAL (decl) = 0;
    1735           99 :       DECL_INTERFACE_KNOWN (decl) = 1;
    1736              :     }
    1737              : 
    1738      6006273 :   import_export_decl (decl);
    1739      6006273 :   if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
    1740              :     {
    1741       444884 :       tree init;
    1742              : 
    1743       444884 :       DECL_EXTERNAL (decl) = 0;
    1744       444884 :       int pseudo_ix = get_pseudo_ti_index (type);
    1745       444884 :       const tinfo_s *ti = get_tinfo_desc (pseudo_ix);
    1746       444884 :       if (TREE_TYPE (decl) != ti->type)
    1747              :         {
    1748              :           /* If the class became complete since we first called get_tinfo_decl,
    1749              :              its type_info descriptor may have switched from __class_type_info
    1750              :              to e.g. __si_class_type_info.  */
    1751            3 :           TREE_TYPE (decl) = ti->type;
    1752            3 :           relayout_decl (decl);
    1753              :         }
    1754       444884 :       init = get_pseudo_ti_init (type, pseudo_ix);
    1755       444884 :       DECL_INITIAL (decl) = init;
    1756       444884 :       mark_used (decl);
    1757       444884 :       cp_finish_decl (decl, init, false, NULL_TREE, 0);
    1758              :       /* Avoid targets optionally bumping up the alignment to improve
    1759              :          vector instruction accesses, tinfo are never accessed this way.  */
    1760              : #ifdef DATA_ABI_ALIGNMENT
    1761       444884 :       SET_DECL_ALIGN (decl, DATA_ABI_ALIGNMENT (TREE_TYPE (decl),
    1762              :                                                 TYPE_ALIGN (TREE_TYPE (decl))));
    1763       444884 :       DECL_USER_ALIGN (decl) = true;
    1764              : #endif
    1765       444884 :       return true;
    1766              :     }
    1767              :   else
    1768      5561389 :     return false;
    1769              : }
    1770              : 
    1771              : #include "gt-cp-rtti.h"
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.