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