LCOV - code coverage report
Current view: top level - gcc/cp - mangle.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.2 % 2311 2154
Test Date: 2026-04-20 14:57:17 Functions: 97.3 % 112 109
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Name mangling for the 3.0 -*- C++ -*- ABI.
       2              :    Copyright (C) 2000-2026 Free Software Foundation, Inc.
       3              :    Written by Alex Samuel <samuel@codesourcery.com>
       4              : 
       5              :    This file is part of GCC.
       6              : 
       7              :    GCC is free software; you can redistribute it and/or modify it
       8              :    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, but
      13              :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              :    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              : /* This file implements mangling of C++ names according to the IA64
      22              :    C++ ABI specification.  A mangled name encodes a function or
      23              :    variable's name, scope, type, and/or template arguments into a text
      24              :    identifier.  This identifier is used as the function's or
      25              :    variable's linkage name, to preserve compatibility between C++'s
      26              :    language features (templates, scoping, and overloading) and C
      27              :    linkers.
      28              : 
      29              :    Additionally, g++ uses mangled names internally.  To support this,
      30              :    mangling of types is allowed, even though the mangled name of a
      31              :    type should not appear by itself as an exported name.  Ditto for
      32              :    uninstantiated templates.
      33              : 
      34              :    The primary entry point for this module is mangle_decl, which
      35              :    returns an identifier containing the mangled name for a decl.
      36              :    Additional entry points are provided to build mangled names of
      37              :    particular constructs when the appropriate decl for that construct
      38              :    is not available.  These are:
      39              : 
      40              :      mangle_typeinfo_for_type:          typeinfo data
      41              :      mangle_typeinfo_string_for_type:   typeinfo type name
      42              :      mangle_vtbl_for_type:              virtual table data
      43              :      mangle_vtt_for_type:               VTT data
      44              :      mangle_ctor_vtbl_for_type:         `C-in-B' constructor virtual table data
      45              :      mangle_thunk:                      thunk function or entry  */
      46              : 
      47              : #include "config.h"
      48              : #include "system.h"
      49              : #include "coretypes.h"
      50              : #include "target.h"
      51              : #include "vtable-verify.h"
      52              : #include "cp-tree.h"
      53              : #include "stringpool.h"
      54              : #include "cgraph.h"
      55              : #include "stor-layout.h"
      56              : #include "flags.h"
      57              : #include "attribs.h"
      58              : #include "contracts.h"
      59              : 
      60              : /* Debugging support.  */
      61              : 
      62              : /* Define DEBUG_MANGLE to enable very verbose trace messages.  */
      63              : #ifndef DEBUG_MANGLE
      64              : #define DEBUG_MANGLE 0
      65              : #endif
      66              : 
      67              : /* Macros for tracing the write_* functions.  */
      68              : #if DEBUG_MANGLE
      69              : # define MANGLE_TRACE(FN, INPUT) \
      70              :   fprintf (stderr, "  %-24s: %-24s\n", (FN), (INPUT))
      71              : # define MANGLE_TRACE_TREE(FN, NODE) \
      72              :   fprintf (stderr, "  %-24s: %-24s (%p)\n", \
      73              :            (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
      74              : #else
      75              : # define MANGLE_TRACE(FN, INPUT)
      76              : # define MANGLE_TRACE_TREE(FN, NODE)
      77              : #endif
      78              : 
      79              : /* Nonzero if NODE is a class template-id.  We can't rely on
      80              :    CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
      81              :    that hard to distinguish A<T> from A, where A<T> is the type as
      82              :    instantiated outside of the template, and A is the type used
      83              :    without parameters inside the template.  */
      84              : #define CLASSTYPE_TEMPLATE_ID_P(NODE)                                   \
      85              :   (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM                     \
      86              :    || (CLASS_TYPE_P (NODE)                                              \
      87              :        && CLASSTYPE_TEMPLATE_INFO (NODE) != NULL                        \
      88              :        && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))
      89              : 
      90              : /* For deciding whether to set G.need_abi_warning, we need to consider both
      91              :    warn_abi_version and flag_abi_compat_version.  */
      92              : #define abi_warn_or_compat_version_crosses(N) \
      93              :   (abi_version_crosses (N) || abi_compat_version_crosses (N))
      94              : 
      95              : /* And sometimes we can simplify the code path if we don't need to worry about
      96              :    previous ABIs.  */
      97              : #define abi_flag_at_least(flag,N) (flag == 0 || flag >= N)
      98              : #define any_abi_below(N) \
      99              :   (!abi_version_at_least (N) \
     100              :    || !abi_flag_at_least (warn_abi_version, (N)) \
     101              :    || !abi_flag_at_least (flag_abi_compat_version, (N)))
     102              : 
     103              : /* Things we only need one of.  This module is not reentrant.  */
     104              : struct GTY(()) globals {
     105              :   /* An array of the current substitution candidates, in the order
     106              :      we've seen them.  Contains NULLS, which correspond to module
     107              :      substitutions.  */
     108              :   vec<tree, va_gc> *substitutions;
     109              : 
     110              :   /* The entity that is being mangled.  */
     111              :   tree GTY ((skip)) entity;
     112              : 
     113              :   /* How many parameter scopes we are inside.  */
     114              :   int parm_depth;
     115              : 
     116              :   /* True if the mangling will be different in a future version of the
     117              :      ABI.  */
     118              :   bool need_abi_warning;
     119              : 
     120              :   /* True if the mangling will be different in C++17 mode.  */
     121              :   bool need_cxx17_warning;
     122              : 
     123              :   /* True if we mangled a module name.  */
     124              :   bool mod;
     125              : };
     126              : 
     127              : static GTY (()) globals G;
     128              : 
     129              : /* The obstack on which we build mangled names.  */
     130              : static struct obstack *mangle_obstack;
     131              : 
     132              : /* The obstack on which we build mangled names that are not going to
     133              :    be IDENTIFIER_NODEs.  */
     134              : static struct obstack name_obstack;
     135              : 
     136              : /* The first object on the name_obstack; we use this to free memory
     137              :    allocated on the name_obstack.  */
     138              : static void *name_base;
     139              : 
     140              : /* Indices into subst_identifiers.  These are identifiers used in
     141              :    special substitution rules.  */
     142              : typedef enum
     143              : {
     144              :   SUBID_ALLOCATOR,
     145              :   SUBID_BASIC_STRING,
     146              :   SUBID_CHAR_TRAITS,
     147              :   SUBID_BASIC_ISTREAM,
     148              :   SUBID_BASIC_OSTREAM,
     149              :   SUBID_BASIC_IOSTREAM,
     150              :   SUBID_MAX
     151              : }
     152              : substitution_identifier_index_t;
     153              : 
     154              : /* For quick substitution checks, look up these common identifiers
     155              :    once only.  */
     156              : static GTY(()) tree subst_identifiers[SUBID_MAX];
     157              : 
     158              : /* Single-letter codes for builtin integer types, defined in
     159              :    <builtin-type>.  These are indexed by integer_type_kind values.  */
     160              : static const char
     161              : integer_type_codes[itk_none] =
     162              : {
     163              :   'c',  /* itk_char */
     164              :   'a',  /* itk_signed_char */
     165              :   'h',  /* itk_unsigned_char */
     166              :   's',  /* itk_short */
     167              :   't',  /* itk_unsigned_short */
     168              :   'i',  /* itk_int */
     169              :   'j',  /* itk_unsigned_int */
     170              :   'l',  /* itk_long */
     171              :   'm',  /* itk_unsigned_long */
     172              :   'x',  /* itk_long_long */
     173              :   'y',  /* itk_unsigned_long_long */
     174              :   /* __intN types are handled separately */
     175              :   '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
     176              : };
     177              : 
     178              : static tree maybe_template_info (const tree);
     179              : 
     180              : /* Functions for handling substitutions.  */
     181              : 
     182              : static inline tree canonicalize_for_substitution (tree);
     183              : static void add_substitution (tree);
     184              : static inline bool is_std_substitution (const tree,
     185              :                                        const substitution_identifier_index_t);
     186              : static inline bool is_std_substitution_char (const tree,
     187              :                                             const substitution_identifier_index_t);
     188              : static int find_substitution (tree);
     189              : static void mangle_call_offset (const tree, const tree);
     190              : 
     191              : /* Functions for emitting mangled representations of things.  */
     192              : 
     193              : static void write_mangled_name (const tree, bool);
     194              : static void write_encoding (const tree);
     195              : static void write_name (tree, const int);
     196              : static void write_abi_tags (tree);
     197              : static void write_unscoped_name (const tree);
     198              : static void write_unscoped_template_name (const tree);
     199              : static void write_nested_name (const tree);
     200              : static void write_prefix (const tree);
     201              : static void write_template_prefix (const tree);
     202              : static void write_unqualified_name (tree);
     203              : static void write_conversion_operator_name (const tree);
     204              : static void write_source_name (tree);
     205              : static void write_literal_operator_name (tree);
     206              : static void write_unnamed_type_name (const tree);
     207              : static void write_unnamed_enum_name (const tree);
     208              : static void write_closure_type_name (const tree);
     209              : static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
     210              :                            const unsigned int);
     211              : static void write_number (unsigned HOST_WIDE_INT, const int,
     212              :                           const unsigned int);
     213              : static void write_compact_number (int num);
     214              : static void write_integer_cst (const tree);
     215              : static void write_real_cst (const tree);
     216              : static void write_identifier (const char *);
     217              : static void write_special_name_constructor (const tree);
     218              : static void write_special_name_destructor (const tree);
     219              : static void write_type (tree);
     220              : static int write_CV_qualifiers_for_type (const tree);
     221              : static void write_builtin_type (tree);
     222              : static void write_function_type (const tree);
     223              : static void write_bare_function_type (const tree, const int, const tree);
     224              : static void write_method_parms (tree, const int, const tree);
     225              : static void write_class_enum_type (const tree);
     226              : static void write_template_args (tree, tree = NULL_TREE);
     227              : static void write_expression (tree);
     228              : static void write_template_arg_literal (const tree);
     229              : static void write_template_arg (tree);
     230              : static void write_template_template_arg (const tree);
     231              : static void write_array_type (const tree);
     232              : static void write_pointer_to_member_type (const tree);
     233              : static void write_template_param (const tree);
     234              : static void write_template_template_param (const tree);
     235              : static void write_substitution (const int);
     236              : static int discriminator_for_local_entity (tree);
     237              : static int discriminator_for_string_literal (tree, tree);
     238              : static void write_discriminator (const int);
     239              : static void write_local_name (tree, const tree, const tree);
     240              : static void dump_substitution_candidates (void);
     241              : static tree mangle_decl_string (const tree);
     242              : static void maybe_check_abi_tags (tree, tree = NULL_TREE, int = 10);
     243              : static void write_splice (tree);
     244              : 
     245              : /* Control functions.  */
     246              : 
     247              : static inline void start_mangling (const tree);
     248              : static tree mangle_special_for_type (const tree, const char *);
     249              : 
     250              : /* Append a single character to the end of the mangled
     251              :    representation.  */
     252              : #define write_char(CHAR)                                                \
     253              :   obstack_1grow (mangle_obstack, (CHAR))
     254              : 
     255              : /* Append a sized buffer to the end of the mangled representation.  */
     256              : #define write_chars(CHAR, LEN)                                          \
     257              :   obstack_grow (mangle_obstack, (CHAR), (LEN))
     258              : 
     259              : /* Append a NUL-terminated string to the end of the mangled
     260              :    representation.  */
     261              : #define write_string(STRING)                                            \
     262              :   obstack_grow (mangle_obstack, (STRING), strlen (STRING))
     263              : 
     264              : /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
     265              :    same purpose (context, which may be a type) and value (template
     266              :    decl).  See write_template_prefix for more information on what this
     267              :    is used for.  */
     268              : #define NESTED_TEMPLATE_MATCH(NODE1, NODE2)                             \
     269              :   (TREE_CODE (NODE1) == TREE_LIST                                       \
     270              :    && TREE_CODE (NODE2) == TREE_LIST                                    \
     271              :    && ((TYPE_P (TREE_PURPOSE (NODE1))                                   \
     272              :         && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))    \
     273              :        || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2))                 \
     274              :    && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
     275              : 
     276              : /* Write out an unsigned quantity in base 10.  */
     277              : #define write_unsigned_number(NUMBER)                                   \
     278              :   write_number ((NUMBER), /*unsigned_p=*/1, 10)
     279              : 
     280              : /* Check for -fabi-version dependent mangling and also set the need_abi_warning
     281              :    flag as appropriate.  */
     282              : 
     283              : static bool
     284     13313016 : abi_check (int ver)
     285              : {
     286     66446774 :   if (abi_warn_or_compat_version_crosses (ver))
     287        62818 :     G.need_abi_warning = true;
     288     13313016 :   return abi_version_at_least (ver);
     289              : }
     290              : 
     291              : /* If DECL is a template instance (including the uninstantiated template
     292              :    itself), return its TEMPLATE_INFO.  Otherwise return NULL.  */
     293              : 
     294              : static tree
     295   1338518262 : maybe_template_info (const tree decl)
     296              : {
     297   1338518262 :   if (TREE_CODE (decl) == TYPE_DECL)
     298              :     {
     299              :       /* TYPE_DECLs are handled specially.  Look at its type to decide
     300              :          if this is a template instantiation.  */
     301    511923456 :       const tree type = TREE_TYPE (decl);
     302              : 
     303    511923456 :       if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
     304    406485064 :         return TYPE_TEMPLATE_INFO (type);
     305              :     }
     306              :   else
     307              :     {
     308              :       /* Check if the template is a primary template.  */
     309    826594806 :       if (DECL_LANG_SPECIFIC (decl) != NULL
     310    825317379 :           && VAR_OR_FUNCTION_DECL_P (decl)
     311    701583611 :           && DECL_TEMPLATE_INFO (decl)
     312   1375662983 :           && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
     313     81989458 :         return DECL_TEMPLATE_INFO (decl);
     314              :     }
     315              : 
     316              :   /* It's not a template id.  */
     317              :   return NULL_TREE;
     318              : }
     319              : 
     320              : /* Produce debugging output of current substitution candidates.  */
     321              : 
     322              : static void
     323            0 : dump_substitution_candidates (void)
     324              : {
     325            0 :   unsigned i;
     326            0 :   tree el;
     327              : 
     328            0 :   fprintf (stderr, "  ++ substitutions  ");
     329            0 :   FOR_EACH_VEC_ELT (*G.substitutions, i, el)
     330              :     {
     331            0 :       const char *name = "???";
     332              : 
     333            0 :       if (i > 0)
     334            0 :         fprintf (stderr, "                    ");
     335            0 :       if (!el)
     336              :         name = "module";
     337            0 :       else if (DECL_P (el))
     338            0 :         name = IDENTIFIER_POINTER (DECL_NAME (el));
     339            0 :       else if (TREE_CODE (el) == TREE_LIST)
     340            0 :         name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
     341            0 :       else if (TYPE_NAME (el))
     342            0 :         name = TYPE_NAME_STRING (el);
     343            0 :       fprintf (stderr, " S%d_ = ", i - 1);
     344            0 :       if (el)
     345              :         {
     346            0 :           if (TYPE_P (el) &&
     347            0 :               (CP_TYPE_RESTRICT_P (el)
     348            0 :                || CP_TYPE_VOLATILE_P (el)
     349            0 :                || CP_TYPE_CONST_P (el)))
     350            0 :             fprintf (stderr, "CV-");
     351            0 :           fprintf (stderr, "%s (%s at %p)",
     352            0 :                    name, get_tree_code_name (TREE_CODE (el)), (void *) el);
     353              :         }
     354            0 :       fprintf (stderr, "\n");
     355              :     }
     356            0 : }
     357              : 
     358              : /* <exception-spec> ::=
     359              :       Do  -- non-throwing exception specification
     360              :       DO <expression> E  -- computed (instantiation-dependent) noexcept
     361              :       Dw <type>* E  -- throw (types)  */
     362              : 
     363              : static void
     364      3113607 : write_exception_spec (tree spec)
     365              : {
     366              : 
     367      3113607 :   if (!spec || spec == noexcept_false_spec)
     368              :     /* Nothing.  */
     369              :     return;
     370              : 
     371        34058 :   if (!flag_noexcept_type)
     372              :     {
     373           17 :       G.need_cxx17_warning = true;
     374           17 :       return;
     375              :     }
     376              : 
     377        34041 :   if (spec == noexcept_true_spec || spec == empty_except_spec)
     378        34026 :     write_string ("Do");
     379           15 :   else if (tree expr = TREE_PURPOSE (spec))
     380              :     {
     381              :       /* noexcept (expr)  */
     382           15 :       gcc_assert (uses_template_parms (expr));
     383           15 :       write_string ("DO");
     384           15 :       write_expression (expr);
     385           15 :       write_char ('E');
     386              :     }
     387              :   else
     388              :     {
     389              :       /* throw (type-list) */
     390            0 :       write_string ("Dw");
     391            0 :       for (tree t = spec; t; t = TREE_CHAIN (t))
     392            0 :         write_type (TREE_VALUE (t));
     393            0 :       write_char ('E');
     394              :     }
     395              : }
     396              : 
     397              : /* Both decls and types can be substitution candidates, but sometimes
     398              :    they refer to the same thing.  For instance, a TYPE_DECL and
     399              :    RECORD_TYPE for the same class refer to the same thing, and should
     400              :    be treated accordingly in substitutions.  This function returns a
     401              :    canonicalized tree node representing NODE that is used when adding
     402              :    and substitution candidates and finding matches.  */
     403              : 
     404              : static inline tree
     405   4695233619 : canonicalize_for_substitution (tree node)
     406              : {
     407              :   /* For a TYPE_DECL, use the type instead.  */
     408   4695233619 :   if (TREE_CODE (node) == TYPE_DECL)
     409         2606 :     node = TREE_TYPE (node);
     410   4695233619 :   if (TYPE_P (node)
     411   3452218441 :       && TYPE_CANONICAL (node) != node
     412   5079375684 :       && TYPE_MAIN_VARIANT (node) != node)
     413              :     {
     414    109169582 :       tree orig = node;
     415              :       /* Here we want to strip the topmost typedef only.
     416              :          We need to do that so is_std_substitution can do proper
     417              :          name matching.  */
     418    109169582 :       if (TREE_CODE (node) == FUNCTION_TYPE)
     419              :         /* Use build_qualified_type and TYPE_QUALS here to preserve
     420              :            the old buggy mangling of attribute noreturn with abi<5.  */
     421        27376 :         node = build_qualified_type (TYPE_MAIN_VARIANT (node),
     422        27376 :                                      TYPE_QUALS (node));
     423              :       else
     424    109142206 :         node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
     425              :                                         cp_type_quals (node));
     426    109169582 :       if (FUNC_OR_METHOD_TYPE_P (node))
     427              :         {
     428        27385 :           node = build_ref_qualified_type (node, type_memfn_rqual (orig));
     429        27385 :           tree r = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (orig));
     430        27385 :           if (flag_noexcept_type)
     431        27341 :             node = build_exception_variant (node, r);
     432              :           else
     433              :             /* Set the warning flag if appropriate.  */
     434           44 :             write_exception_spec (r);
     435              :         }
     436              :     }
     437   4695233619 :   return node;
     438              : }
     439              : 
     440              : /* Add NODE as a substitution candidate.  NODE must not already be on
     441              :    the list of candidates.  */
     442              : 
     443              : static void
     444   1218089488 : add_substitution (tree node)
     445              : {
     446   1218089488 :   tree c;
     447              : 
     448   1218089488 :   if (DEBUG_MANGLE)
     449              :     fprintf (stderr, "  ++ add_substitution (%s at %10p)\n",
     450              :              get_tree_code_name (TREE_CODE (node)), (void *) node);
     451              : 
     452              :   /* Get the canonicalized substitution candidate for NODE.  */
     453   1218089488 :   c = canonicalize_for_substitution (node);
     454   1218089488 :   if (DEBUG_MANGLE && c != node)
     455              :     fprintf (stderr, "  ++ using candidate (%s at %10p)\n",
     456              :              get_tree_code_name (TREE_CODE (node)), (void *) node);
     457   1218089488 :   node = c;
     458              : 
     459              :   /* Make sure NODE isn't already a candidate.  */
     460   1218089488 :   if (flag_checking)
     461              :     {
     462              :       int i;
     463              :       tree candidate;
     464              : 
     465   6325959277 :       FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
     466   5107869905 :         if (candidate)
     467              :           {
     468   5107862800 :             gcc_assert (!(DECL_P (node) && node == candidate));
     469   5107862800 :             gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
     470              :                           && same_type_p (node, candidate)));
     471              :           }
     472              :     }
     473              : 
     474              :   /* Put the decl onto the varray of substitution candidates.  */
     475   1218089488 :   vec_safe_push (G.substitutions, node);
     476              : 
     477   1218089488 :   if (DEBUG_MANGLE)
     478              :     dump_substitution_candidates ();
     479   1218089488 : }
     480              : 
     481              : /* Helper function for find_substitution.  Returns nonzero if NODE,
     482              :    which may be a class or a class template, is a template-id with template
     483              :    name of substitution_index[INDEX] in the ::std namespace, with
     484              :    global module attachment.  */
     485              : 
     486              : static bool
     487   4118241788 : is_std_substitution (const tree node,
     488              :                      const substitution_identifier_index_t index)
     489              : {
     490   4118241788 :   tree type = NULL_TREE;
     491   4118241788 :   tree decl = NULL_TREE;
     492              : 
     493   8213897675 :   auto std_substitution_p = [&] (tree decl, tree type)
     494              :     {
     495   4095655887 :       if (!DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl)))
     496              :         return false;
     497              : 
     498   1589425133 :       if (!(TYPE_LANG_SPECIFIC (type) && TYPE_TEMPLATE_INFO (type)))
     499              :         return false;
     500              : 
     501   1183106349 :       tree tmpl = TYPE_TI_TEMPLATE (type);
     502   1183106349 :       if (DECL_NAME (tmpl) != subst_identifiers[index])
     503              :         return false;
     504              : 
     505    142225254 :       if (modules_p () && get_originating_module (tmpl, true) >= 0)
     506           27 :         return false;
     507              : 
     508              :       return true;
     509   4118241788 :     };
     510              : 
     511   4118241788 :   if (TREE_CODE (node) == TYPE_DECL || DECL_CLASS_TEMPLATE_P (node))
     512              :     {
     513   3255608009 :       type = TREE_TYPE (node);
     514   3255608009 :       decl = node;
     515              :     }
     516    862633779 :   else if (CLASS_TYPE_P (node))
     517              :     {
     518      4514593 :       type = node;
     519      4514593 :       decl = TYPE_NAME (node);
     520              :     }
     521              :   else
     522              :     {
     523              :       /* We used to accept all _DECL nodes in this function but now we
     524              :          only accept classes or class templates.  Verify that we don't
     525              :          return false for something that used to yield true.  */
     526    858119186 :       gcc_checking_assert (!DECL_P (node)
     527              :                            || !std_substitution_p (node, TREE_TYPE (node)));
     528              :       /* These are not the droids you're looking for.  */
     529              :       return false;
     530              :     }
     531              : 
     532   3260122602 :   return std_substitution_p (decl, type);
     533              : }
     534              : 
     535              : /* Return the ABI tags (the TREE_VALUE of the "abi_tag" attribute entry) for T,
     536              :    which can be a decl or type.  */
     537              : 
     538              : static tree
     539   1811846029 : get_abi_tags (tree t)
     540              : {
     541   1811846029 :   if (!t || TREE_CODE (t) == NAMESPACE_DECL)
     542              :     return NULL_TREE;
     543              : 
     544   1688944967 :   if (DECL_P (t) && DECL_DECLARES_TYPE_P (t))
     545    453777233 :     t = TREE_TYPE (t);
     546              : 
     547   1688944967 :   if (TREE_CODE (t) == TEMPLATE_DECL && DECL_TEMPLATE_RESULT (t))
     548              :     {
     549     16451742 :       tree tags = get_abi_tags (DECL_TEMPLATE_RESULT (t));
     550              :       /* We used to overlook abi_tag on function and variable templates.  */
     551     16451742 :       if (tags && abi_check (19))
     552              :         return tags;
     553              :       else
     554     16451732 :         return NULL_TREE;
     555              :     }
     556              : 
     557   1672493225 :   tree attrs;
     558   1672493225 :   if (TYPE_P (t))
     559   1455446953 :     attrs = TYPE_ATTRIBUTES (t);
     560              :   else
     561    217046272 :     attrs = DECL_ATTRIBUTES (t);
     562              : 
     563   1672493225 :   tree tags = lookup_attribute ("abi_tag", attrs);
     564   1672493225 :   if (tags)
     565     11798918 :     tags = TREE_VALUE (tags);
     566              :   return tags;
     567              : }
     568              : 
     569              : /* Helper function for find_substitution.  Returns nonzero if NODE,
     570              :    which may be a decl or a CLASS_TYPE, is the template-id
     571              :    ::std::identifier<char>, where identifier is
     572              :    substitution_index[INDEX].  */
     573              : 
     574              : static bool
     575      6455217 : is_std_substitution_char (const tree node,
     576              :                           const substitution_identifier_index_t index)
     577              : {
     578      6455217 :   tree args;
     579              :   /* Check NODE's name is ::std::identifier.  */
     580      6455217 :   if (!is_std_substitution (node, index))
     581              :     return 0;
     582              :   /* Figure out its template args.  */
     583      4094428 :   if (DECL_P (node))
     584            0 :     args = DECL_TI_ARGS (node);
     585      4094428 :   else if (CLASS_TYPE_P (node))
     586      4094428 :     args = CLASSTYPE_TI_ARGS (node);
     587              :   else
     588              :     /* Oops, not a template.  */
     589              :     return 0;
     590              :   /* NODE's template arg list should be <char>.  */
     591      4094428 :   return
     592      4094428 :     TREE_VEC_LENGTH (args) == 1
     593      4094428 :     && TREE_VEC_ELT (args, 0) == char_type_node;
     594              : }
     595              : 
     596              : /* Check whether a substitution should be used to represent NODE in
     597              :    the mangling.
     598              : 
     599              :    First, check standard special-case substitutions.
     600              : 
     601              :      <substitution> ::= St
     602              :          # ::std
     603              : 
     604              :                     ::= Sa
     605              :          # ::std::allocator
     606              : 
     607              :                     ::= Sb
     608              :          # ::std::basic_string
     609              : 
     610              :                     ::= Ss
     611              :          # ::std::basic_string<char,
     612              :                                ::std::char_traits<char>,
     613              :                                ::std::allocator<char> >
     614              : 
     615              :                     ::= Si
     616              :          # ::std::basic_istream<char, ::std::char_traits<char> >
     617              : 
     618              :                     ::= So
     619              :          # ::std::basic_ostream<char, ::std::char_traits<char> >
     620              : 
     621              :                     ::= Sd
     622              :          # ::std::basic_iostream<char, ::std::char_traits<char> >
     623              : 
     624              :    Then examine the stack of currently available substitution
     625              :    candidates for entities appearing earlier in the same mangling
     626              : 
     627              :    If a substitution is found, write its mangled representation and
     628              :    return nonzero.  If none is found, just return zero.  */
     629              : 
     630              : static int
     631   2241829325 : find_substitution (tree node)
     632              : {
     633   2241829325 :   int i;
     634   2241829325 :   const int size = vec_safe_length (G.substitutions);
     635   2241829325 :   tree decl;
     636   2241829325 :   tree type;
     637   2241829325 :   const char *abbr = NULL;
     638              : 
     639   2241829325 :   if (DEBUG_MANGLE)
     640              :     fprintf (stderr, "  ++ find_substitution (%s at %p)\n",
     641              :              get_tree_code_name (TREE_CODE (node)), (void *) node);
     642              : 
     643              :   /* Obtain the canonicalized substitution representation for NODE.
     644              :      This is what we'll compare against.  */
     645   2241829325 :   node = canonicalize_for_substitution (node);
     646              : 
     647              :   /* Check for builtin substitutions.  */
     648              : 
     649   2241829325 :   decl = TYPE_P (node) ? TYPE_NAME (node) : node;
     650   2241829325 :   type = TYPE_P (node) ? node : TREE_TYPE (node);
     651              : 
     652              :   /* Check for std::allocator.  */
     653   2241829325 :   if (decl
     654   2081706796 :       && is_std_substitution (decl, SUBID_ALLOCATOR)
     655   2378888702 :       && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
     656              :     abbr = "Sa";
     657              : 
     658              :   /* Check for std::basic_string.  */
     659   2179141161 :   else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
     660              :     {
     661       155083 :       if (TYPE_P (node))
     662              :         {
     663              :           /* If this is a type (i.e. a fully-qualified template-id),
     664              :              check for
     665              :                  std::basic_string <char,
     666              :                                     std::char_traits<char>,
     667              :                                     std::allocator<char> > .  */
     668       108250 :           if (cp_type_quals (type) == TYPE_UNQUALIFIED
     669       108250 :               && CLASSTYPE_USE_TEMPLATE (type))
     670              :             {
     671        96701 :               tree args = CLASSTYPE_TI_ARGS (type);
     672        96701 :               if (TREE_VEC_LENGTH (args) == 3
     673        96683 :                   && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
     674        39287 :                   && is_std_substitution_char (TREE_VEC_ELT (args, 1),
     675              :                                                SUBID_CHAR_TRAITS)
     676       135977 :                   && is_std_substitution_char (TREE_VEC_ELT (args, 2),
     677              :                                                SUBID_ALLOCATOR))
     678              :                 abbr = "Ss";
     679              :             }
     680              :         }
     681              :       else
     682              :         /* Substitute for the template name only if this isn't a type.  */
     683              :         abbr = "Sb";
     684              :     }
     685              : 
     686              :   /* Check for basic_{i,o,io}stream.  */
     687   2178986078 :   else if (TYPE_P (node)
     688   1448523234 :            && cp_type_quals (type) == TYPE_UNQUALIFIED
     689   1364867639 :            && CLASS_TYPE_P (type)
     690    578822407 :            && CLASSTYPE_USE_TEMPLATE (type)
     691   2603889298 :            && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
     692              :     {
     693              :       /* First, check for the template
     694              :          args <char, std::char_traits<char> > .  */
     695    424903220 :       tree args = CLASSTYPE_TI_ARGS (type);
     696    424903220 :       if (TREE_VEC_LENGTH (args) == 2
     697    132778586 :           && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
     698    431279874 :           && is_std_substitution_char (TREE_VEC_ELT (args, 1),
     699              :                                        SUBID_CHAR_TRAITS))
     700              :         {
     701              :           /* Got them.  Is this basic_istream?  */
     702      4015871 :           if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
     703              :             abbr = "Si";
     704              :           /* Or basic_ostream?  */
     705      3714796 :           else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
     706              :             abbr = "So";
     707              :           /* Or basic_iostream?  */
     708      3330476 :           else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
     709   2024264267 :             abbr = "Sd";
     710              :         }
     711              :     }
     712              : 
     713              :   /* Check for namespace std.  */
     714   1754082858 :   else if (decl && DECL_NAMESPACE_STD_P (decl))
     715              :     {
     716    217565058 :       write_string ("St");
     717    217565058 :       return 1;
     718              :     }
     719              : 
     720   2024264267 :   tree tags = NULL_TREE;
     721   2024264267 :   if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
     722   1001669720 :     tags = get_abi_tags (type);
     723              :   /* Now check the list of available substitutions for this mangling
     724              :      operation.  */
     725   2024264267 :   if (!abbr || tags)
     726   8777948094 :     for (i = 0; i < size; ++i)
     727   6990453082 :       if (tree candidate = (*G.substitutions)[i])
     728              :         {
     729              :           /* NODE is a matched to a candidate if it's the same decl node or
     730              :              if it's the same type.  */
     731   6990443401 :           if (decl == candidate
     732   6930498678 :               || (TYPE_P (candidate) && type && TYPE_P (node)
     733   3070084336 :                   && same_type_p (type, candidate))
     734  13808426792 :               || NESTED_TEMPLATE_MATCH (node, candidate))
     735              :             {
     736    173078652 :               write_substitution (i);
     737    173078652 :               return 1;
     738              :             }
     739              :         }
     740              : 
     741   1787495012 :   if (!abbr)
     742              :     /* No substitution found.  */
     743              :     return 0;
     744              : 
     745     63690609 :   write_string (abbr);
     746     63690609 :   if (tags)
     747              :     {
     748              :       /* If there are ABI tags on the abbreviation, it becomes
     749              :          a substitution candidate.  */
     750            6 :       write_abi_tags (tags);
     751            6 :       add_substitution (node);
     752              :     }
     753              :   return 1;
     754              : }
     755              : 
     756              : /* Returns whether DECL's symbol name should be the plain unqualified-id
     757              :    rather than a more complicated mangled name.  */
     758              : 
     759              : static bool
     760    212719269 : unmangled_name_p (const tree decl)
     761              : {
     762    212719269 :   if (TREE_CODE (decl) == FUNCTION_DECL)
     763              :     {
     764              :       /* The names of `extern "C"' functions are not mangled.  */
     765    180783909 :       return (DECL_EXTERN_C_FUNCTION_P (decl)
     766              :               /* But overloaded operator names *are* mangled.  */
     767      2224408 :               && !DECL_OVERLOADED_OPERATOR_P (decl));
     768              :     }
     769     31935360 :   else if (VAR_P (decl))
     770              :     {
     771              :       /* extern "C" declarations aren't mangled.  */
     772     31932052 :       if (DECL_NAMESPACE_SCOPE_P (decl) && DECL_EXTERN_C_P (decl))
     773              :         return true;
     774              : 
     775              :       /* static variables are mangled.  */
     776     31724814 :       if (!DECL_EXTERNAL_LINKAGE_P (decl))
     777              :         return false;
     778              : 
     779              :       /* Other variables at non-global scope are mangled.  */
     780     31514573 :       if (CP_DECL_CONTEXT (decl) != global_namespace)
     781              :         return false;
     782              : 
     783              :       /* Variable template instantiations are mangled.  */
     784       115119 :       if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
     785       113476 :           && variable_template_p (DECL_TI_TEMPLATE (decl)))
     786              :         return false;
     787              : 
     788              :       /* Declarations with ABI tags are mangled.  */
     789       106666 :       if (get_abi_tags (decl))
     790              :         return false;
     791              : 
     792              :       // Declarations attached to a named module are mangled
     793       106327 :       if (modules_p () && get_originating_module (decl, true) >= 0)
     794              :         return false;
     795              : 
     796              :       /* The names of non-static global variables aren't mangled.  */
     797              :       return true;
     798              :     }
     799              : 
     800              :   return false;
     801              : }
     802              : 
     803              : /* TOP_LEVEL is true, if this is being called at outermost level of
     804              :   mangling. It should be false when mangling a decl appearing in an
     805              :   expression within some other mangling.
     806              : 
     807              :   <mangled-name>      ::= _Z <encoding>  */
     808              : 
     809              : static void
     810    212719269 : write_mangled_name (const tree decl, bool top_level)
     811              : {
     812    212719269 :   MANGLE_TRACE_TREE ("mangled-name", decl);
     813              : 
     814    212719269 :   check_abi_tags (decl);
     815              : 
     816    212719269 :   if (unmangled_name_p (decl))
     817              :     {
     818      2537551 :       if (top_level)
     819      2537325 :         write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
     820              :       else
     821              :         {
     822              :           /* The standard notes: "The <encoding> of an extern "C"
     823              :              function is treated like global-scope data, i.e. as its
     824              :              <source-name> without a type."  We cannot write
     825              :              overloaded operators that way though, because it contains
     826              :              characters invalid in assembler.  */
     827          226 :           write_string ("_Z");
     828          226 :           write_source_name (DECL_NAME (decl));
     829              :         }
     830              :     }
     831              :   else
     832              :     {
     833    210181718 :       write_string ("_Z");
     834    210181718 :       write_encoding (decl);
     835              :     }
     836              : 
     837              :   /* If this is a coroutine helper, then append an appropriate string to
     838              :      identify which.  */
     839    212719269 :   if (tree ramp = DECL_RAMP_FN (decl))
     840              :     {
     841         3012 :       if (DECL_ACTOR_FN (ramp) == decl)
     842         1506 :         write_string (JOIN_STR "actor");
     843         1506 :       else if (DECL_DESTROY_FN (ramp) == decl)
     844         1506 :         write_string (JOIN_STR "destroy");
     845              :       else
     846            0 :         gcc_unreachable ();
     847              :     }
     848    212719269 : }
     849              : 
     850              : /* Returns true if the return type of DECL is part of its signature, and
     851              :    therefore its mangling.  */
     852              : 
     853              : bool
     854    361652293 : mangle_return_type_p (tree decl)
     855              : {
     856    361652293 :   return (!DECL_CONSTRUCTOR_P (decl)
     857    303336739 :           && !DECL_DESTRUCTOR_P (decl)
     858    289643138 :           && !DECL_CONV_FN_P (decl)
     859    648656300 :           && maybe_template_info (decl));
     860              : }
     861              : 
     862              : /* <constraint-expression> ::= <expression> */
     863              : 
     864              : static void
     865      2819564 : write_constraint_expression (tree expr)
     866              : {
     867            0 :   write_expression (expr);
     868       293476 : }
     869              : 
     870              : /* Mangle a requires-clause following a template-head, if any.
     871              : 
     872              :    Q <constraint_expression> E  */
     873              : 
     874              : static void
     875    401197081 : write_tparms_constraints (tree constraints)
     876              : {
     877              :   /* In a declaration with shorthand constraints in the template-head, followed
     878              :      by a requires-clause, followed by shorthand constraints in the
     879              :      function-parameter-list, the full constraints will be some && with the
     880              :      parameter constraints on the RHS, around an && with the requires-clause on
     881              :      the RHS.  Find the requires-clause, if any.
     882              : 
     883              :      This logic relies on the && and ... from combine_constraint_expressions,
     884              :      finish_shorthand_constraint, and convert_generic_types_to_packs having
     885              :      UNKNOWN_LOCATION.  If they need to have an actual location, we could move
     886              :      to using a TREE_LANG_FLAG.  */
     887    401197081 :   if (constraints && abi_check (19))
     888              :     {
     889              :       tree probe = constraints;
     890              :       while (probe
     891       316888 :              && !EXPR_LOCATION (probe)
     892       340303 :              && TREE_CODE (probe) == TRUTH_ANDIF_EXPR)
     893              :         {
     894          169 :           tree op1 = TREE_OPERAND (probe, 1);
     895          315 :           probe = (EXPR_LOCATION (op1) ? op1
     896          146 :                    : TREE_OPERAND (probe, 0));
     897              :         }
     898       316719 :       if (probe && EXPR_LOCATION (probe))
     899              :         {
     900       293473 :           write_char ('Q');
     901       293473 :           write_constraint_expression (probe);
     902              :         }
     903              :     }
     904    401197081 : }
     905              : 
     906              : /* <type-constraint> ::= <name> */
     907              : 
     908              : static void
     909       146540 : write_type_constraint (tree cnst)
     910              : {
     911       146540 :   if (!cnst)
     912              :     return;
     913              : 
     914       146540 :   gcc_checking_assert (TREE_CODE (cnst) == TEMPLATE_ID_EXPR);
     915              : 
     916       146540 :   tree concept_decl = get_concept_check_template (cnst);
     917       146540 :   write_name (concept_decl, 0);
     918       146540 :   tree args = TREE_OPERAND (cnst, 1);
     919       146540 :   if (TREE_VEC_LENGTH (args) > 1)
     920              :     {
     921        56668 :       TEMPLATE_ARGS_TYPE_CONSTRAINT_P (args) = true;
     922        56668 :       write_template_args (args);
     923              :     }
     924              : }
     925              : 
     926              : /*   <encoding>           ::= <function name> <bare-function-type>
     927              :                         ::= <data name>  */
     928              : 
     929              : static void
     930    215052034 : write_encoding (const tree decl)
     931              : {
     932    215052034 :   MANGLE_TRACE_TREE ("encoding", decl);
     933              : 
     934    215052034 :   if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
     935              :     {
     936              :       /* For overloaded operators write just the mangled name
     937              :          without arguments.  */
     938        10694 :       if (DECL_OVERLOADED_OPERATOR_P (decl))
     939            3 :         write_name (decl, /*ignore_local_scope=*/0);
     940              :       else
     941        10691 :         write_source_name (DECL_NAME (decl));
     942        10694 :       return;
     943              :     }
     944              : 
     945    215041340 :   write_name (decl, /*ignore_local_scope=*/0);
     946    215041340 :   if (TREE_CODE (decl) == FUNCTION_DECL)
     947              :     {
     948    183418927 :       tree fn_type;
     949    183418927 :       tree d;
     950              : 
     951    183418927 :       if (maybe_template_info (decl))
     952              :         {
     953     15940210 :           fn_type = get_mostly_instantiated_function_type (decl);
     954              :           /* FN_TYPE will not have parameter types for in-charge or
     955              :              VTT parameters.  Therefore, we pass NULL_TREE to
     956              :              write_bare_function_type -- otherwise, it will get
     957              :              confused about which artificial parameters to skip.  */
     958     15940210 :           d = NULL_TREE;
     959              :         }
     960              :       else
     961              :         {
     962    167478717 :           fn_type = TREE_TYPE (decl);
     963    167478717 :           d = decl;
     964              :         }
     965              : 
     966    183418927 :       write_bare_function_type (fn_type,
     967    183418927 :                                 mangle_return_type_p (decl),
     968              :                                 d);
     969              : 
     970    183418927 :       if (tree c = get_trailing_function_requirements (decl))
     971      2539355 :         if (abi_check (19))
     972              :           {
     973      2526088 :             ++G.parm_depth;
     974      2526088 :             write_char ('Q');
     975      2526088 :             write_constraint_expression (c);
     976      2526088 :             --G.parm_depth;
     977              :           }
     978              :     }
     979              : }
     980              : 
     981              : /* Interface to substitution and identifier mangling, used by the
     982              :    module name mangler.  */
     983              : 
     984              : void
     985          386 : mangle_module_substitution (int v)
     986              : {
     987          386 :   write_substitution (v - 1);
     988          386 : }
     989              : 
     990              : int
     991         8289 : mangle_module_component (tree comp, bool partition_p)
     992              : {
     993         8289 :   write_char ('W');
     994         8289 :   if (partition_p)
     995          202 :     write_char ('P');
     996         8289 :   write_source_name (comp);
     997              : 
     998              :   // Module substitutions use the same number-space as entity
     999              :   // substitutions, but are orthogonal.
    1000         8289 :   vec_safe_push (G.substitutions, NULL_TREE);
    1001         8289 :   return G.substitutions->length ();
    1002              : }
    1003              : 
    1004              : /* If the outermost non-namespace context (including DECL itself) is
    1005              :    a module-linkage decl, mangle the module information.  For module
    1006              :    global initializers we need to include the partition part.
    1007              : 
    1008              :    <module-name> ::= <module-sub>
    1009              :                  || <subst>
    1010              :                  || <module-name> <module-sub>
    1011              :    <module-sub> :: W [P] <unqualified-name>
    1012              : */
    1013              : 
    1014              : static void
    1015         8017 : write_module (int m, bool include_partition)
    1016              : {
    1017         8017 :   G.mod = true;
    1018            0 :   mangle_module (m, include_partition);
    1019         6009 : }
    1020              : 
    1021              : static void
    1022      1509408 : maybe_write_module (tree decl)
    1023              : {
    1024      1509408 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
    1025              :     return;
    1026              : 
    1027      1072583 :   if (!TREE_PUBLIC (STRIP_TEMPLATE (decl)))
    1028              :     return;
    1029              : 
    1030      1064452 :   if (TREE_CODE (decl) == NAMESPACE_DECL)
    1031              :     return;
    1032              : 
    1033       809095 :   int m = get_originating_module (decl, true);
    1034       809095 :   if (m >= 0)
    1035         6009 :     write_module (m, false);
    1036              : }
    1037              : 
    1038              : /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
    1039              :    or PARM_DECL context, which doesn't belong in DECL_CONTEXT.  */
    1040              : 
    1041              : static tree
    1042   2000147201 : decl_mangling_context (tree decl)
    1043              : {
    1044   2000147349 :   tree tcontext = targetm.cxx.decl_mangling_context (decl);
    1045              : 
    1046   2000147349 :   if (tcontext != NULL_TREE)
    1047              :     return tcontext;
    1048              : 
    1049   2000147349 :   if (TREE_CODE (decl) == TEMPLATE_DECL
    1050   2000147349 :       && DECL_TEMPLATE_RESULT (decl))
    1051              :     decl = DECL_TEMPLATE_RESULT (decl);
    1052              : 
    1053   2000147349 :   if (TREE_CODE (decl) == TYPE_DECL
    1054   2944690676 :       && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    1055              :     {
    1056      4634469 :       tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
    1057      4634469 :       if (extra)
    1058              :         return extra;
    1059         3798 :       tcontext = CP_DECL_CONTEXT (decl);
    1060         3968 :       if (LAMBDA_TYPE_P (tcontext))
    1061              :         /* Lambda type context means this lambda appears between the
    1062              :            lambda-introducer and the open brace of another lambda (c++/119175).
    1063              :            That isn't a real scope; look further into the enclosing scope.  */
    1064           60 :         return decl_mangling_context (TYPE_NAME (tcontext));
    1065              :     }
    1066   1995512880 :   else if (template_type_parameter_p (decl))
    1067              :      /* template type parms have no mangling context.  */
    1068              :       return NULL_TREE;
    1069              : 
    1070   1995515632 :   tcontext = CP_DECL_CONTEXT (decl);
    1071              : 
    1072   1995515632 :   if (member_like_constrained_friend_p (decl))
    1073       102760 :     tcontext = DECL_FRIEND_CONTEXT (decl);
    1074              : 
    1075              :   /* Ignore the artificial declare reduction functions.  */
    1076   1995515632 :   if (tcontext
    1077   1995515632 :       && TREE_CODE (tcontext) == FUNCTION_DECL
    1078   2001009133 :       && DECL_OMP_DECLARE_REDUCTION_P (tcontext))
    1079              :     return decl_mangling_context (tcontext);
    1080              : 
    1081              :   return tcontext;
    1082              : }
    1083              : 
    1084              : /* <name> ::= <unscoped-name>
    1085              :           ::= <unscoped-template-name> <template-args>
    1086              :           ::= <nested-name>
    1087              :           ::= <local-name>
    1088              : 
    1089              :    If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
    1090              :    called from <local-name>, which mangles the enclosing scope
    1091              :    elsewhere and then uses this function to mangle just the part
    1092              :    underneath the function scope.  So don't use the <local-name>
    1093              :    production, to avoid an infinite recursion.  */
    1094              : 
    1095              : static void
    1096    533806172 : write_name (tree decl, const int ignore_local_scope)
    1097              : {
    1098    533806172 :   tree context;
    1099              : 
    1100    533806172 :   MANGLE_TRACE_TREE ("name", decl);
    1101              : 
    1102    533806172 :   if (TREE_CODE (decl) == TYPE_DECL)
    1103              :     {
    1104              :       /* In case this is a typedef, fish out the corresponding
    1105              :          TYPE_DECL for the main variant.  */
    1106    314625442 :       decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
    1107              :     }
    1108              : 
    1109    533806172 :   context = decl_mangling_context (decl);
    1110              : 
    1111    533806172 :   gcc_assert (context != NULL_TREE);
    1112              : 
    1113   2668549955 :   if (abi_warn_or_compat_version_crosses (7)
    1114       160047 :       && ignore_local_scope
    1115           44 :       && TREE_CODE (context) == PARM_DECL)
    1116            0 :     G.need_abi_warning = 1;
    1117              : 
    1118              :   /* A decl in :: or ::std scope is treated specially.  The former is
    1119              :      mangled using <unscoped-name> or <unscoped-template-name>, the
    1120              :      latter with a special substitution.  Also, a name that is
    1121              :      directly in a local function scope is also mangled with
    1122              :      <unscoped-name> rather than a full <nested-name>.  */
    1123    533806172 :   if (context == global_namespace
    1124    522265241 :       || DECL_NAMESPACE_STD_P (context)
    1125    309793947 :       || (ignore_local_scope
    1126      4396181 :           && (TREE_CODE (context) == FUNCTION_DECL
    1127      3132114 :               || (abi_version_at_least (7)
    1128      3132114 :                   && TREE_CODE (context) == PARM_DECL))))
    1129              :     {
    1130              :       /* Is this a template instance?  */
    1131    225276322 :       if (tree info = maybe_template_info (decl))
    1132              :         {
    1133              :           /* Yes: use <unscoped-template-name>.  */
    1134    188156660 :           write_unscoped_template_name (TI_TEMPLATE (info));
    1135              :           /* Pass down the parms of a function template in case we need to
    1136              :              mangle them; we don't mangle the parms of a non-overloadable
    1137              :              template.  */
    1138    188156660 :           tree parms = (TREE_CODE (decl) == FUNCTION_DECL
    1139    192863875 :                         ? DECL_TEMPLATE_PARMS (TI_TEMPLATE (info))
    1140      4707215 :                         : NULL_TREE);
    1141    188156660 :           write_template_args (TI_ARGS (info), parms);
    1142              :         }
    1143              :       else
    1144              :         /* Everything else gets an <unqualified-name>.  */
    1145     37119662 :         write_unscoped_name (decl);
    1146              :     }
    1147              :   else
    1148              :     {
    1149              :       /* Handle local names, unless we asked not to (that is, invoked
    1150              :          under <local-name>, to handle only the part of the name under
    1151              :          the local scope).  */
    1152    308529850 :       if (!ignore_local_scope)
    1153              :         {
    1154              :           /* Scan up the list of scope context, looking for a
    1155              :              function.  If we find one, this entity is in local
    1156              :              function scope.  local_entity tracks context one scope
    1157              :              level down, so it will contain the element that's
    1158              :              directly in that function's scope, either decl or one of
    1159              :              its enclosing scopes.  */
    1160              :           tree local_entity = decl;
    1161    974204081 :           while (context != global_namespace)
    1162              :             {
    1163              :               /* Make sure we're always dealing with decls.  */
    1164    673202496 :               if (TYPE_P (context))
    1165    213539565 :                 context = TYPE_NAME (context);
    1166              :               /* Is this a function?  */
    1167    673202496 :               if (TREE_CODE (context) == FUNCTION_DECL
    1168    668806672 :                   || TREE_CODE (context) == PARM_DECL)
    1169              :                 {
    1170              :                   /* Yes, we have local scope.  Use the <local-name>
    1171              :                      production for the innermost function scope.  */
    1172      4396181 :                   write_local_name (context, local_entity, decl);
    1173      4396181 :                   return;
    1174              :                 }
    1175              :               /* Up one scope level.  */
    1176    668806315 :               local_entity = context;
    1177    668806315 :               context = decl_mangling_context (context);
    1178              :             }
    1179              : 
    1180              :           /* No local scope found?  Fall through to <nested-name>.  */
    1181              :         }
    1182              : 
    1183              :       /* Other decls get a <nested-name> to encode their scope.  */
    1184    304133669 :       write_nested_name (decl);
    1185              :     }
    1186              : }
    1187              : 
    1188              : /* <unscoped-name> ::= <unqualified-name>
    1189              :                    ::= St <unqualified-name>   # ::std::  */
    1190              : 
    1191              : static void
    1192    160025065 : write_unscoped_name (const tree decl)
    1193              : {
    1194    160025065 :   tree context = decl_mangling_context (decl);
    1195              : 
    1196    160025065 :   MANGLE_TRACE_TREE ("unscoped-name", decl);
    1197              : 
    1198              :   /* Is DECL in ::std?  */
    1199    160025065 :   if (DECL_NAMESPACE_STD_P (context))
    1200              :     {
    1201    150170424 :       write_string ("St");
    1202    150170424 :       write_unqualified_name (decl);
    1203              :     }
    1204              :   else
    1205              :     {
    1206              :       /* If not, it should be either in the global namespace, or directly
    1207              :          in a local function scope.  A lambda can also be mangled in the
    1208              :          scope of a default argument.  */
    1209      9854641 :       gcc_assert (context == global_namespace
    1210              :                   || TREE_CODE (context) == PARM_DECL
    1211              :                   || TREE_CODE (context) == FUNCTION_DECL);
    1212              : 
    1213      9854641 :       write_unqualified_name (decl);
    1214              :     }
    1215    160025065 : }
    1216              : 
    1217              : /* <unscoped-template-name> ::= <unscoped-name>
    1218              :                             ::= <substitution>  */
    1219              : 
    1220              : static void
    1221    188156660 : write_unscoped_template_name (const tree decl)
    1222              : {
    1223    188156660 :   MANGLE_TRACE_TREE ("unscoped-template-name", decl);
    1224              : 
    1225    188156660 :   if (find_substitution (decl))
    1226              :     return;
    1227    122905403 :   write_unscoped_name (decl);
    1228    122905403 :   add_substitution (decl);
    1229              : }
    1230              : 
    1231              : /* Write the nested name, including CV-qualifiers, of DECL.
    1232              : 
    1233              :    <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
    1234              :                  ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
    1235              : 
    1236              :    <ref-qualifier> ::= R # & ref-qualifier
    1237              :                    ::= O # && ref-qualifier
    1238              :    <CV-qualifiers> ::= [r] [V] [K]  */
    1239              : 
    1240              : static void
    1241    306529483 : write_nested_name (const tree decl)
    1242              : {
    1243    306529483 :   MANGLE_TRACE_TREE ("nested-name", decl);
    1244              : 
    1245    306529483 :   write_char ('N');
    1246              : 
    1247              :   /* Write CV-qualifiers, if this is an iobj member function.  */
    1248    306529483 :   if (TREE_CODE (decl) == FUNCTION_DECL
    1249    306529483 :       && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    1250              :     {
    1251    145662989 :       if (DECL_VOLATILE_MEMFUNC_P (decl))
    1252      8541916 :         write_char ('V');
    1253    145662989 :       if (DECL_CONST_MEMFUNC_P (decl))
    1254     44509047 :         write_char ('K');
    1255    145662989 :       if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
    1256              :         {
    1257       141249 :           if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
    1258        87108 :             write_char ('O');
    1259              :           else
    1260        54141 :             write_char ('R');
    1261              :         }
    1262              :     }
    1263    134288007 :   else if (DECL_DECLARES_FUNCTION_P (decl)
    1264    160866494 :            && DECL_XOBJ_MEMBER_FUNCTION_P (decl))
    1265         3602 :     write_char ('H');
    1266              : 
    1267              :   /* Is this a template instance?  */
    1268    306529483 :   if (tree info = maybe_template_info (decl))
    1269              :     {
    1270              :       /* Yes, use <template-prefix>.  */
    1271     51703244 :       write_template_prefix (decl);
    1272     51703244 :       write_template_args (TI_ARGS (info));
    1273              :     }
    1274    254826239 :   else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
    1275    330626994 :            && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
    1276              :     {
    1277      2395811 :       tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
    1278      2395811 :       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
    1279              :         {
    1280        41893 :           write_template_prefix (decl);
    1281        41893 :           write_template_args (TREE_OPERAND (name, 1));
    1282              :         }
    1283              :       else
    1284              :         {
    1285      2353918 :           write_prefix (decl_mangling_context (decl));
    1286      2353918 :           write_unqualified_name (decl);
    1287              :         }
    1288              :     }
    1289              :   else
    1290              :     {
    1291              :       /* No, just use <prefix>  */
    1292    252430428 :       write_prefix (decl_mangling_context (decl));
    1293    252430428 :       write_unqualified_name (decl);
    1294              :     }
    1295    306529483 :   write_char ('E');
    1296    306529483 : }
    1297              : 
    1298              : /* <prefix> ::= <prefix> <unqualified-name>
    1299              :             ::= <template-param>
    1300              :             ::= <template-prefix> <template-args>
    1301              :             ::= <decltype>
    1302              :             ::= # empty
    1303              :             ::= <substitution>
    1304              :             ::= <splice>      # C++26 dependent splice [proposed]  */
    1305              : 
    1306              : static void
    1307    631880715 : write_prefix (const tree node)
    1308              : {
    1309    631880715 :   tree decl;
    1310              : 
    1311    631880715 :   if (node == NULL
    1312    631880715 :       || node == global_namespace)
    1313              :     return;
    1314              : 
    1315    608672138 :   MANGLE_TRACE_TREE ("prefix", node);
    1316              : 
    1317    608672138 :   if (TREE_CODE (node) == DECLTYPE_TYPE
    1318    608671977 :       || TREE_CODE (node) == TRAIT_TYPE)
    1319              :     {
    1320          164 :       write_type (node);
    1321          164 :       return;
    1322              :     }
    1323              : 
    1324    608671974 :   if (TREE_CODE (node) == SPLICE_SCOPE)
    1325              :     {
    1326            5 :       write_splice (node);
    1327            5 :       return;
    1328              :     }
    1329              : 
    1330    608671969 :   if (find_substitution (node))
    1331              :     return;
    1332              : 
    1333    334120354 :   tree template_info = NULL_TREE;
    1334    334120354 :   if (DECL_P (node))
    1335              :     {
    1336              :       /* If this is a function or parm decl, that means we've hit function
    1337              :          scope, so this prefix must be for a local name.  In this
    1338              :          case, we're under the <local-name> production, which encodes
    1339              :          the enclosing function scope elsewhere.  So don't continue
    1340              :          here.  */
    1341    126066359 :       if (TREE_CODE (node) == FUNCTION_DECL
    1342    122935304 :           || TREE_CODE (node) == PARM_DECL)
    1343              :         return;
    1344              : 
    1345    122934977 :       decl = node;
    1346    122934977 :       template_info = maybe_template_info (decl);
    1347              :     }
    1348              :   else
    1349              :     {
    1350              :       /* Node is a type.  */
    1351    208053995 :       decl = TYPE_NAME (node);
    1352              :       /* The DECL might not point at the node.  */
    1353    208053995 :       if (CLASSTYPE_TEMPLATE_ID_P (node))
    1354    157046773 :         template_info = TYPE_TEMPLATE_INFO (node);
    1355              :     }
    1356              : 
    1357    330988972 :   if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
    1358         9827 :     write_template_param (node);
    1359    330979145 :   else if (template_info)
    1360              :     /* Templated.  */
    1361              :     {
    1362    157062496 :       write_template_prefix (decl);
    1363    157062496 :       write_template_args (TI_ARGS (template_info));
    1364              :     }
    1365    173916649 :   else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
    1366              :     {
    1367       122944 :       tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
    1368       122944 :       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
    1369              :         {
    1370       113219 :           write_template_prefix (decl);
    1371       113219 :           write_template_args (TREE_OPERAND (name, 1));
    1372              :         }
    1373              :       else
    1374              :         {
    1375         9725 :           write_prefix (decl_mangling_context (decl));
    1376         9725 :           write_unqualified_name (decl);
    1377              :         }
    1378              :     }
    1379              :   else
    1380              :     /* Not templated.  */
    1381              :     {
    1382    173793705 :       write_prefix (decl_mangling_context (decl));
    1383    173793705 :       write_unqualified_name (decl);
    1384    173793705 :       if (VAR_P (decl)
    1385    173793705 :           || TREE_CODE (decl) == FIELD_DECL)
    1386              :         {
    1387              :           /* <data-member-prefix> := <member source-name> M */
    1388        18047 :           write_char ('M');
    1389              : 
    1390              :           /* Before ABI 18, we did not count these as substitution
    1391              :              candidates.  This leads to incorrect demanglings (and
    1392              :              ABI divergence to other compilers).  */
    1393        18047 :           if (!abi_check (18))
    1394              :             return;
    1395              :         }
    1396              :     }
    1397              : 
    1398    330988840 :   add_substitution (node);
    1399              : }
    1400              : 
    1401              : /* <template-prefix> ::= <prefix> <template component>
    1402              :                      ::= <template-param>
    1403              :                      ::= <substitution>  */
    1404              : 
    1405              : static void
    1406    208920852 : write_template_prefix (const tree node)
    1407              : {
    1408    208920852 :   tree decl = DECL_P (node) ? node : TYPE_NAME (node);
    1409    208920852 :   tree type = DECL_P (node) ? TREE_TYPE (node) : node;
    1410    208920852 :   tree context = decl_mangling_context (decl);
    1411    208920852 :   tree templ;
    1412    208920852 :   tree substitution;
    1413              : 
    1414    208920852 :   MANGLE_TRACE_TREE ("template-prefix", node);
    1415              : 
    1416              :   /* Find the template decl.  */
    1417    208920852 :   if (tree info = maybe_template_info (decl))
    1418    208764754 :     templ = TI_TEMPLATE (info);
    1419       156098 :   else if (TREE_CODE (type) == TYPENAME_TYPE)
    1420              :     /* For a typename type, all we have is the name.  */
    1421       155112 :     templ = DECL_NAME (decl);
    1422              :   else
    1423              :     {
    1424          986 :       gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
    1425              : 
    1426          986 :       templ = TYPE_TI_TEMPLATE (type);
    1427              :     }
    1428              : 
    1429              :   /* For a member template, though, the template name for the
    1430              :      innermost name must have all the outer template levels
    1431              :      instantiated.  For instance, consider
    1432              : 
    1433              :        template<typename T> struct Outer {
    1434              :          template<typename U> struct Inner {};
    1435              :        };
    1436              : 
    1437              :      The template name for `Inner' in `Outer<int>::Inner<float>' is
    1438              :      `Outer<int>::Inner<U>'.  In g++, we don't instantiate the template
    1439              :      levels separately, so there's no TEMPLATE_DECL available for this
    1440              :      (there's only `Outer<T>::Inner<U>').
    1441              : 
    1442              :      In order to get the substitutions right, we create a special
    1443              :      TREE_LIST to represent the substitution candidate for a nested
    1444              :      template.  The TREE_PURPOSE is the template's context, fully
    1445              :      instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
    1446              :      template.
    1447              : 
    1448              :      So, for the example above, `Outer<int>::Inner' is represented as a
    1449              :      substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
    1450              :      and whose value is `Outer<T>::Inner<U>'.  */
    1451    208920852 :   if (context && TYPE_P (context))
    1452     10330463 :     substitution = build_tree_list (context, templ);
    1453              :   else
    1454              :     substitution = templ;
    1455              : 
    1456    208920852 :   if (find_substitution (substitution))
    1457              :     return;
    1458              : 
    1459    203293578 :   if (TREE_TYPE (templ)
    1460    203293578 :       && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
    1461          986 :     write_template_param (TREE_TYPE (templ));
    1462              :   else
    1463              :     {
    1464    203292592 :       write_prefix (context);
    1465    203292592 :       write_unqualified_name (decl);
    1466              :     }
    1467              : 
    1468    203293578 :   add_substitution (substitution);
    1469              : }
    1470              : 
    1471              : /* "For the purposes of mangling, the name of an anonymous union is considered
    1472              :    to be the name of the first named data member found by a pre-order,
    1473              :    depth-first, declaration-order walk of the data members of the anonymous
    1474              :    union. If there is no such data member (i.e., if all of the data members in
    1475              :    the union are unnamed), then there is no way for a program to refer to the
    1476              :    anonymous union, and there is therefore no need to mangle its name."  */
    1477              : 
    1478              : static tree
    1479           21 : anon_aggr_naming_decl (tree type)
    1480              : {
    1481           21 :   tree field = next_aggregate_field (TYPE_FIELDS (type));
    1482           21 :   for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
    1483              :     {
    1484           21 :       if (DECL_NAME (field))
    1485              :         return field;
    1486            0 :       if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
    1487            0 :         if (tree sub = anon_aggr_naming_decl (TREE_TYPE (field)))
    1488              :           return sub;
    1489              :     }
    1490              :   return NULL_TREE;
    1491              : }
    1492              : 
    1493              : /* We don't need to handle thunks, vtables, or VTTs here.  Those are
    1494              :    mangled through special entry points.
    1495              : 
    1496              :     <unqualified-name>  ::= [<module-name>] <operator-name>
    1497              :                         ::= <special-name>
    1498              :                         ::= [<module-name>] <source-name>
    1499              :                         ::= [<module-name>] <unnamed-type-name>
    1500              :                         ::= <local-source-name>
    1501              :                         ::= F <source-name> # member-like constrained friend
    1502              : 
    1503              :     <local-source-name>   ::= L <source-name> <discriminator> */
    1504              : 
    1505              : static void
    1506      4408383 : write_unqualified_id (tree identifier)
    1507              : {
    1508      4408383 :   if (IDENTIFIER_CONV_OP_P (identifier))
    1509           15 :     write_conversion_operator_name (TREE_TYPE (identifier));
    1510      4408368 :   else if (IDENTIFIER_OVL_OP_P (identifier))
    1511              :     {
    1512        47644 :       const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier);
    1513        47644 :       write_string (ovl_op->mangled_name);
    1514        47644 :     }
    1515      4360724 :   else if (UDLIT_OPER_P (identifier))
    1516            0 :     write_literal_operator_name (identifier);
    1517              :   else
    1518      4360724 :     write_source_name (identifier);
    1519      4408383 : }
    1520              : 
    1521              : static void
    1522    791908797 : write_unqualified_name (tree decl)
    1523              : {
    1524    791908797 :   MANGLE_TRACE_TREE ("unqualified-name", decl);
    1525              : 
    1526    791908797 :   if (modules_p ())
    1527      1509408 :     maybe_write_module (decl);
    1528              : 
    1529    791908797 :   if (identifier_p (decl))
    1530              :     {
    1531            0 :       write_unqualified_id (decl);
    1532            0 :       return;
    1533              :     }
    1534              : 
    1535    791908797 :   bool found = false;
    1536              : 
    1537    791908797 :   if (DECL_NAME (decl) == NULL_TREE
    1538    791908797 :       && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
    1539           21 :     decl = anon_aggr_naming_decl (TREE_TYPE (decl));
    1540    791908776 :   else if (DECL_NAME (decl) == NULL_TREE)
    1541              :     {
    1542        11615 :       found = true;
    1543        11615 :       gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
    1544        11615 :       write_source_name (DECL_ASSEMBLER_NAME (decl));
    1545              :     }
    1546    791897161 :   else if (DECL_DECLARES_FUNCTION_P (decl))
    1547              :     {
    1548    183418366 :       found = true;
    1549              : 
    1550              :       /* A constrained hidden friend is mangled like a member function, with
    1551              :          the name prefixed by 'F'.  */
    1552    183418366 :       if (member_like_constrained_friend_p (decl))
    1553        25690 :         write_char ('F');
    1554              : 
    1555    366836732 :       if (DECL_CONSTRUCTOR_P (decl))
    1556     29188471 :         write_special_name_constructor (decl);
    1557    154229895 :       else if (DECL_DESTRUCTOR_P (decl))
    1558      7081952 :         write_special_name_destructor (decl);
    1559    147147943 :       else if (DECL_CONV_FN_P (decl))
    1560              :         {
    1561              :           /* Conversion operator. Handle it right here.
    1562              :              <operator> ::= cv <type>  */
    1563      2633850 :           tree type;
    1564      2633850 :           if (maybe_template_info (decl))
    1565              :             {
    1566          521 :               tree fn_type;
    1567          521 :               fn_type = get_mostly_instantiated_function_type (decl);
    1568          521 :               type = TREE_TYPE (fn_type);
    1569              :             }
    1570      2633329 :           else if (FNDECL_USED_AUTO (decl))
    1571           60 :             type = DECL_SAVED_AUTO_RETURN_TYPE (decl);
    1572              :           else
    1573      2633269 :             type = DECL_CONV_FN_TYPE (decl);
    1574      2633850 :           write_conversion_operator_name (type);
    1575              :         }
    1576    144514093 :       else if (DECL_OVERLOADED_OPERATOR_P (decl))
    1577              :         {
    1578     33401455 :           tree t;
    1579     33401455 :           if (!(t = DECL_RAMP_FN (decl)))
    1580     33401067 :             t = decl;
    1581     33401455 :           const char *mangled_name
    1582     33401455 :             = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (t)]
    1583     33401455 :                [DECL_OVERLOADED_OPERATOR_CODE_RAW (t)].mangled_name);
    1584     33401455 :           write_string (mangled_name);
    1585              :         }
    1586    111112638 :       else if (UDLIT_OPER_P (DECL_NAME (decl)))
    1587       301739 :         write_literal_operator_name (DECL_NAME (decl));
    1588              :       else
    1589              :         found = false;
    1590              :     }
    1591              : 
    1592     72619103 :   if (found)
    1593              :     /* OK */;
    1594    719289715 :   else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
    1595       121274 :            && DECL_NAMESPACE_SCOPE_P (decl)
    1596    719359342 :            && decl_linkage (decl) == lk_internal)
    1597              :     {
    1598        59046 :       MANGLE_TRACE_TREE ("local-source-name", decl);
    1599        59046 :       write_char ('L');
    1600        59046 :       write_source_name (DECL_NAME (decl));
    1601              :       /* The default discriminator is 1, and that's all we ever use,
    1602              :          so there's no code to output one here.  */
    1603              :     }
    1604              :   else
    1605              :     {
    1606    719230669 :       tree type = TREE_TYPE (decl);
    1607              : 
    1608    719230669 :       if (TREE_CODE (decl) == TYPE_DECL
    1609    719230669 :           && enum_with_enumerator_for_linkage_p (type))
    1610           19 :         write_unnamed_enum_name (type);
    1611   1067524411 :       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_UNNAMED_P (type))
    1612         1384 :         write_unnamed_type_name (type);
    1613   1042606290 :       else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type))
    1614      1799832 :         write_closure_type_name (type);
    1615              :       else
    1616    717429434 :         write_source_name (DECL_NAME (decl));
    1617              :     }
    1618              : 
    1619              :   /* We use the ABI tags from the primary class template, ignoring tags on any
    1620              :      specializations.  This is necessary because C++ doesn't require a
    1621              :      specialization to be declared before it is used unless the use requires a
    1622              :      complete type, but we need to get the tags right on incomplete types as
    1623              :      well.  */
    1624    791908797 :   if (tree tmpl = most_general_template (decl))
    1625              :     {
    1626    464388507 :       tree res = DECL_TEMPLATE_RESULT (tmpl);
    1627    464388507 :       if (res == NULL_TREE)
    1628              :         /* UNBOUND_CLASS_TEMPLATE.  */;
    1629    464388504 :       else if (DECL_DECLARES_TYPE_P (decl))
    1630              :         decl = res;
    1631    160615077 :       else if (any_abi_below (11))
    1632              :         {
    1633              :           /* ABI v10 implicit tags on the template.  */
    1634        81558 :           tree mtags = missing_abi_tags (res);
    1635              :           /* Explicit tags on the template.  */
    1636        81558 :           tree ttags = get_abi_tags (res);
    1637              :           /* Tags on the instantiation.  */
    1638        81558 :           tree dtags = get_abi_tags (decl);
    1639              : 
    1640        81636 :           if (mtags && abi_warn_or_compat_version_crosses (10))
    1641            6 :             G.need_abi_warning = 1;
    1642              : 
    1643              :           /* Add the v10 tags to the explicit tags now.  */
    1644        81558 :           mtags = chainon (mtags, ttags);
    1645              : 
    1646        81558 :           if (!G.need_abi_warning
    1647       163018 :               && abi_warn_or_compat_version_crosses (11)
    1648       163022 :               && !equal_abi_tags (dtags, mtags))
    1649            6 :             G.need_abi_warning = 1;
    1650              : 
    1651        81558 :           if (!abi_version_at_least (10))
    1652              :             /* In abi <10, we only got the explicit tags.  */
    1653              :             decl = res;
    1654          901 :           else if (flag_abi_version == 10)
    1655              :             {
    1656              :               /* In ABI 10, we want explict and implicit tags.  */
    1657           72 :               write_abi_tags (mtags);
    1658           72 :               return;
    1659              :             }
    1660              :         }
    1661              :     }
    1662              : 
    1663    791908725 :   tree tags = get_abi_tags (decl);
    1664    178711372 :   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
    1665    794542575 :       && any_abi_below (11))
    1666         2731 :     if (tree mtags = missing_abi_tags (decl))
    1667              :       {
    1668            9 :         if (!abi_check (11))
    1669            6 :           tags = chainon (mtags, tags);
    1670              :       }
    1671    791908725 :   write_abi_tags (tags);
    1672              : }
    1673              : 
    1674              : /* Write the unqualified-name for a conversion operator to TYPE.  */
    1675              : 
    1676              : static void
    1677      2633865 : write_conversion_operator_name (const tree type)
    1678              : {
    1679      2633865 :   write_string ("cv");
    1680      2633865 :   write_type (type);
    1681      2633865 : }
    1682              : 
    1683              : /* Non-terminal <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.
    1684              : 
    1685              :      <source-name> ::= </length/ number> <identifier>  */
    1686              : 
    1687              : static void
    1688    721880380 : write_source_name (tree identifier)
    1689              : {
    1690    721880380 :   MANGLE_TRACE_TREE ("source-name", identifier);
    1691              : 
    1692    721880380 :   write_unsigned_number (IDENTIFIER_LENGTH (identifier));
    1693    721880380 :   write_identifier (IDENTIFIER_POINTER (identifier));
    1694    721880380 : }
    1695              : 
    1696              : /* Compare two TREE_STRINGs like strcmp.  */
    1697              : 
    1698              : static int
    1699          306 : tree_string_cmp (const void *p1, const void *p2)
    1700              : {
    1701          306 :   if (p1 == p2)
    1702              :     return 0;
    1703          306 :   tree s1 = *(const tree*)p1;
    1704          306 :   tree s2 = *(const tree*)p2;
    1705          306 :   return strcmp (TREE_STRING_POINTER (s1),
    1706          306 :                  TREE_STRING_POINTER (s2));
    1707              : }
    1708              : 
    1709              : /* Return the TREE_LIST of TAGS as a sorted VEC.  */
    1710              : 
    1711              : static vec<tree, va_gc> *
    1712       487749 : sorted_abi_tags (tree tags)
    1713              : {
    1714       487749 :   vec<tree, va_gc> * vec = make_tree_vector();
    1715              : 
    1716       812639 :   for (tree t = tags; t; t = TREE_CHAIN (t))
    1717              :     {
    1718       324890 :       if (ABI_TAG_IMPLICIT (t))
    1719            3 :         continue;
    1720       324887 :       tree str = TREE_VALUE (t);
    1721       324887 :       vec_safe_push (vec, str);
    1722              :     }
    1723              : 
    1724       487749 :   vec->qsort (tree_string_cmp);
    1725              : 
    1726       487749 :   return vec;
    1727              : }
    1728              : 
    1729              : /* ID is the name of a function or type with abi_tags attribute TAGS.
    1730              :    Write out the name, suitably decorated.  */
    1731              : 
    1732              : static void
    1733    791908833 : write_abi_tags (tree tags)
    1734              : {
    1735    791908833 :   if (tags == NULL_TREE)
    1736    791908833 :     return;
    1737              : 
    1738       324761 :   vec<tree, va_gc> * vec = sorted_abi_tags (tags);
    1739              : 
    1740       324761 :   unsigned i; tree str;
    1741       649549 :   FOR_EACH_VEC_ELT (*vec, i, str)
    1742              :     {
    1743       324788 :       write_string ("B");
    1744       324788 :       write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
    1745       324788 :       write_identifier (TREE_STRING_POINTER (str));
    1746              :     }
    1747              : 
    1748       324761 :   release_tree_vector (vec);
    1749              : }
    1750              : 
    1751              : /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent.  */
    1752              : 
    1753              : bool
    1754        81494 : equal_abi_tags (tree t1, tree t2)
    1755              : {
    1756        81494 :   releasing_vec v1 = sorted_abi_tags (t1);
    1757        81494 :   releasing_vec v2 = sorted_abi_tags (t2);
    1758              : 
    1759        81494 :   unsigned len1 = v1->length();
    1760        81494 :   if (len1 != v2->length())
    1761              :     return false;
    1762        81488 :   for (unsigned i = 0; i < len1; ++i)
    1763           21 :     if (strcmp (TREE_STRING_POINTER (v1[i]),
    1764           21 :                 TREE_STRING_POINTER (v2[i])) != 0)
    1765              :       return false;
    1766              :   return true;
    1767        81494 : }
    1768              : 
    1769              : /* Write a user-defined literal operator.
    1770              :           ::= li <source-name>    # "" <source-name>
    1771              :    IDENTIFIER is an LITERAL_IDENTIFIER_NODE.  */
    1772              : 
    1773              : static void
    1774       301739 : write_literal_operator_name (tree identifier)
    1775              : {
    1776       301739 :   const char* suffix = UDLIT_OP_SUFFIX (identifier);
    1777       301739 :   write_identifier (UDLIT_OP_MANGLED_PREFIX);
    1778       301739 :   write_unsigned_number (strlen (suffix));
    1779       301739 :   write_identifier (suffix);
    1780       301739 : }
    1781              : 
    1782              : /* Encode 0 as _, and 1+ as n-1_.  */
    1783              : 
    1784              : static void
    1785     29102253 : write_compact_number (int num)
    1786              : {
    1787     29102253 :   gcc_checking_assert (num >= 0);
    1788     29102253 :   if (num > 0)
    1789     11485367 :     write_unsigned_number (num - 1);
    1790     29102253 :   write_char ('_');
    1791     29102253 : }
    1792              : 
    1793              : /* Return how many unnamed types precede TYPE in its enclosing class.  */
    1794              : 
    1795              : static int
    1796          717 : nested_anon_class_index (tree type)
    1797              : {
    1798          717 :   int index = 0;
    1799          717 :   tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
    1800        20340 :   for (; member; member = DECL_CHAIN (member))
    1801        20340 :     if (DECL_IMPLICIT_TYPEDEF_P (member))
    1802              :       {
    1803         1112 :         tree memtype = TREE_TYPE (member);
    1804         1112 :         if (memtype == type)
    1805              :           return index;
    1806          986 :         else if (TYPE_UNNAMED_P (memtype))
    1807          196 :           ++index;
    1808              :       }
    1809              : 
    1810            0 :   if (seen_error ())
    1811              :     return -1;
    1812              : 
    1813            0 :   gcc_unreachable ();
    1814              : }
    1815              : 
    1816              : /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
    1817              : 
    1818              : static void
    1819         1384 : write_unnamed_type_name (const tree type)
    1820              : {
    1821         1384 :   int discriminator;
    1822         1384 :   MANGLE_TRACE_TREE ("unnamed-type-name", type);
    1823              : 
    1824         1384 :   if (TYPE_FUNCTION_SCOPE_P (type))
    1825          331 :     discriminator = discriminator_for_local_entity (TYPE_NAME (type));
    1826         1053 :   else if (TYPE_CLASS_SCOPE_P (type))
    1827          717 :     discriminator = nested_anon_class_index (type);
    1828              :   else
    1829              :     {
    1830          336 :       gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
    1831              :       /* Just use the old mangling at namespace scope.  */
    1832          336 :       write_source_name (TYPE_IDENTIFIER (type));
    1833          336 :       return;
    1834              :     }
    1835              : 
    1836         1048 :   write_string ("Ut");
    1837         1048 :   write_compact_number (discriminator);
    1838              : }
    1839              : 
    1840              : /* <unnamed-enum-name> ::= Ue <underlying type> <enumerator source-name> */
    1841              : 
    1842              : static void
    1843           19 : write_unnamed_enum_name (const tree type)
    1844              : {
    1845           19 :   MANGLE_TRACE_TREE ("unnamed-enum-name", type);
    1846           19 :   write_string ("Ue");
    1847           19 :   write_type (ENUM_UNDERLYING_TYPE (type));
    1848           19 :   write_source_name (DECL_NAME (TREE_VALUE (TYPE_VALUES (type))));
    1849           19 : }
    1850              : 
    1851              : /* ABI issue #47: if a function template parameter is not "natural" for its
    1852              :    argument we must mangle the parameter.  */
    1853              : 
    1854              : static bool
    1855      8177171 : template_parm_natural_p (tree arg, tree parm)
    1856              : {
    1857      8177171 :   tree decl = TREE_VALUE (parm);
    1858              : 
    1859              :   /* A template parameter is "natural" if: */
    1860              : 
    1861      8177171 :   if (template_parameter_pack_p (decl))
    1862              :     {
    1863       778629 :       tree args = ARGUMENT_PACK_ARGS (arg);
    1864       778629 :       if (TREE_VEC_LENGTH (args) == 0)
    1865              :         {
    1866              : #if 0
    1867              :           /* the argument is an empty pack and the parameter is an
    1868              :              unconstrained template type parameter pack; */
    1869              :           if (TREE_CODE (decl) != TYPE_DECL)
    1870              :             return false;
    1871              : #else
    1872              :           /* Defer changing the mangling of C++11 code like
    1873              :              template <int i> int max();
    1874              :              template <int i, int j, int... rest> int max();  */
    1875              :           return true;
    1876              : #endif
    1877              :         }
    1878              :       else
    1879              :         /* the argument is a non-empty pack and a non-pack variant of the
    1880              :            parameter would be natural for the first element of the pack; */
    1881       605296 :         arg = TREE_VEC_ELT (args, 0);
    1882              :     }
    1883              : 
    1884              :   /* the argument is a template and the parameter has the exact
    1885              :      same template head; */
    1886      8003838 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    1887         8288 :     return template_heads_equivalent_p (arg, decl);
    1888              : 
    1889              :   /* the argument is a type and the parameter is unconstrained; or */
    1890      7995550 :   else if (TREE_CODE (decl) == TYPE_DECL)
    1891      7027643 :     return !TEMPLATE_PARM_CONSTRAINTS (parm);
    1892              : 
    1893              :   /* the argument is a non-type template argument and the declared parameter
    1894              :      type neither is instantiation dependent nor contains deduced types.  */
    1895       967907 :   else if (TREE_CODE (decl) == PARM_DECL)
    1896              :     {
    1897              : #if 0
    1898              :       return !uses_template_parms (TREE_TYPE (decl));
    1899              : #else
    1900              :       /* Defer changing the mangling of C++98 code like
    1901              :          template <class T, T V> ....  */
    1902       967907 :       return !type_uses_auto (TREE_TYPE (decl));
    1903              : #endif
    1904              :     }
    1905              : 
    1906            0 :   gcc_unreachable ();
    1907              : }
    1908              : 
    1909              : /* Used for lambda template head and non-natural function template parameters.
    1910              : 
    1911              :    <template-param-decl> ::= Ty               # template type parameter
    1912              :         ::= Tk <type-constraint>              # constrained type parameter
    1913              :         ::= Tn <type>                         # template non-type parameter
    1914              :         ::= Tt <template-param-decl>* [Q <constraint-expression] E  # ttp
    1915              :         ::= Tp <non-pack template-param-decl> # template parameter pack */
    1916              : 
    1917              : static void
    1918        74021 : write_template_param_decl (tree parm)
    1919              : {
    1920        74021 :   tree decl = TREE_VALUE (parm);
    1921              : 
    1922        74021 :   if (template_parameter_pack_p (decl))
    1923        28374 :     write_string ("Tp");
    1924              : 
    1925        74021 :   switch (TREE_CODE (decl))
    1926              :     {
    1927        26072 :     case PARM_DECL:
    1928        26072 :       {
    1929        26072 :         write_string ("Tn");
    1930              : 
    1931        26072 :         tree type = TREE_TYPE (decl);
    1932              :         /* TODO: We need to also mangle constrained auto*, auto&, etc, but
    1933              :            it's not clear how.  See finish_constrained_parameter.  */
    1934        26072 :         if (tree c = (is_auto (type)
    1935        28750 :                       ? TEMPLATE_PARM_CONSTRAINTS (parm)
    1936         2678 :                       : NULL_TREE))
    1937              :           {
    1938           22 :             if (TREE_CODE (c) == UNARY_LEFT_FOLD_EXPR)
    1939              :               {
    1940            3 :                 c = FOLD_EXPR_PACK (c);
    1941            3 :                 c = PACK_EXPANSION_PATTERN (c);
    1942              :               }
    1943           22 :             if (AUTO_IS_DECLTYPE (type))
    1944            0 :               write_string ("DK");
    1945              :             else
    1946           22 :               write_string ("Dk");
    1947           22 :             write_type_constraint (c);
    1948              :           }
    1949              :         else
    1950        26050 :           write_type (type);
    1951              :       }
    1952              :       break;
    1953              : 
    1954          253 :     case TEMPLATE_DECL:
    1955          253 :       {
    1956          253 :         write_string ("Tt");
    1957          253 :         tree parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
    1958          602 :         for (tree node : tree_vec_range (parms))
    1959          349 :           write_template_param_decl (node);
    1960          253 :         write_char ('E');
    1961              :       }
    1962          253 :       break;
    1963              : 
    1964        47696 :     case TYPE_DECL:
    1965        47696 :       if (tree c = TEMPLATE_PARM_CONSTRAINTS (parm))
    1966              :         {
    1967        23386 :           if (TREE_CODE (c) == UNARY_LEFT_FOLD_EXPR)
    1968              :             {
    1969           50 :               c = FOLD_EXPR_PACK (c);
    1970           50 :               c = PACK_EXPANSION_PATTERN (c);
    1971              :             }
    1972        23386 :           if (TREE_CODE (decl) == TYPE_DECL)
    1973              :             {
    1974        23386 :               write_string ("Tk");
    1975        23386 :               write_type_constraint (c);
    1976              :             }
    1977              :         }
    1978              :       else
    1979        24310 :         write_string ("Ty");
    1980              :       break;
    1981              : 
    1982            0 :     default:
    1983            0 :       gcc_unreachable ();
    1984              :     }
    1985        74021 : }
    1986              : 
    1987              : // A template head, for templated lambdas.
    1988              : // New in ABI=18. Returns true iff we emitted anything -- used for ABI
    1989              : // version warning.
    1990              : 
    1991              : static bool
    1992       330183 : write_closure_template_head (tree tmpl)
    1993              : {
    1994       330183 :   bool any = false;
    1995              : 
    1996              :   // We only need one level of template parms
    1997       330183 :   tree parms = DECL_TEMPLATE_PARMS (tmpl);
    1998       330183 :   tree inner = INNERMOST_TEMPLATE_PARMS (parms);
    1999              : 
    2000       377919 :   for (int ix = 0, len = TREE_VEC_LENGTH (inner); ix != len; ix++)
    2001              :     {
    2002       338435 :       tree parm = TREE_VEC_ELT (inner, ix);
    2003       338435 :       if (parm == error_mark_node)
    2004            0 :         continue;
    2005              : 
    2006       338435 :       if (DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (parm)))
    2007              :         // A synthetic parm, we're done.
    2008              :         break;
    2009              : 
    2010        47736 :       any = true;
    2011        47736 :       if (abi_version_at_least (18))
    2012        47496 :         write_template_param_decl (parm);
    2013              :     }
    2014              : 
    2015       330183 :   write_tparms_constraints (TEMPLATE_PARMS_CONSTRAINTS (parms));
    2016              : 
    2017       330183 :   return any;
    2018              : }
    2019              : 
    2020              : /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
    2021              :    <lambda-sig> ::= <parameter type>+  # Parameter types or "v" if the lambda has no parameters */
    2022              : 
    2023              : static void
    2024      1799832 : write_closure_type_name (const tree type)
    2025              : {
    2026      1799832 :   tree fn = lambda_function (type);
    2027      1799832 :   tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
    2028      1799832 :   tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
    2029              : 
    2030      1799832 :   MANGLE_TRACE_TREE ("closure-type-name", type);
    2031              : 
    2032      1799832 :   write_string ("Ul");
    2033              : 
    2034      1799832 :   if (auto ti = maybe_template_info (fn))
    2035       330183 :     if (write_closure_template_head (TI_TEMPLATE (ti)))
    2036              :       // If there were any explicit template parms, we may need to
    2037              :       // issue a mangling diagnostic.
    2038       233685 :       if (abi_warn_or_compat_version_crosses (18))
    2039          198 :         G.need_abi_warning = true;
    2040              : 
    2041      1799832 :   write_method_parms (parms, TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE, fn);
    2042      1799832 :   write_char ('E');
    2043      1799832 :   if ((LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
    2044      1799832 :        != LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda))
    2045      2320684 :       && abi_warn_or_compat_version_crosses (18))
    2046          288 :     G.need_abi_warning = true;
    2047      3599664 :   write_compact_number (abi_version_at_least (18)
    2048      1799310 :                         ? LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
    2049          522 :                         : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda));
    2050      1799832 : }
    2051              : 
    2052              : /* Convert NUMBER to ascii using base BASE and generating at least
    2053              :    MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
    2054              :    into which to store the characters. Returns the number of
    2055              :    characters generated (these will be laid out in advance of where
    2056              :    BUFFER points).  */
    2057              : 
    2058              : static int
    2059    976201686 : hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
    2060              :                 char *buffer, const unsigned int min_digits)
    2061              : {
    2062    976201686 :   static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    2063    976201686 :   unsigned digits = 0;
    2064              : 
    2065   2422454094 :   while (number)
    2066              :     {
    2067   1446252408 :       unsigned HOST_WIDE_INT d = number / base;
    2068              : 
    2069   1446252408 :       *--buffer = base_digits[number - d * base];
    2070   1446252408 :       digits++;
    2071   1446252408 :       number = d;
    2072              :     }
    2073   1027712019 :   while (digits < min_digits)
    2074              :     {
    2075     51510333 :       *--buffer = base_digits[0];
    2076     51510333 :       digits++;
    2077              :     }
    2078    976201686 :   return digits;
    2079              : }
    2080              : 
    2081              : /* Non-terminal <number>.
    2082              : 
    2083              :      <number> ::= [n] </decimal integer/>  */
    2084              : 
    2085              : static void
    2086    976201686 : write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
    2087              :               const unsigned int base)
    2088              : {
    2089    976201686 :   char buffer[sizeof (HOST_WIDE_INT) * 8];
    2090    976201686 :   unsigned count = 0;
    2091              : 
    2092    976201686 :   if (!unsigned_p && (HOST_WIDE_INT) number < 0)
    2093              :     {
    2094            0 :       write_char ('n');
    2095            0 :       number = -((HOST_WIDE_INT) number);
    2096              :     }
    2097    976201686 :   count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
    2098    976201686 :   write_chars (buffer + sizeof (buffer) - count, count);
    2099    976201686 : }
    2100              : 
    2101              : /* Write out an integral CST in decimal. Most numbers are small, and
    2102              :    representable in a HOST_WIDE_INT. Occasionally we'll have numbers
    2103              :    bigger than that, which we must deal with.  */
    2104              : 
    2105              : static inline void
    2106     90664491 : write_integer_cst (const tree cst)
    2107              : {
    2108     90664491 :   int sign = tree_int_cst_sgn (cst);
    2109     90664491 :   widest_int abs_value = wi::abs (wi::to_widest (cst));
    2110     90664491 :   if (!wi::fits_uhwi_p (abs_value))
    2111              :     {
    2112              :       /* A bignum. We do this in chunks, each of which fits in a
    2113              :          HOST_WIDE_INT.  */
    2114            0 :       char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
    2115            0 :       unsigned HOST_WIDE_INT chunk;
    2116            0 :       unsigned chunk_digits;
    2117            0 :       char *ptr = buffer + sizeof (buffer);
    2118            0 :       unsigned count = 0;
    2119            0 :       tree n, base, type;
    2120            0 :       int done;
    2121              : 
    2122              :       /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
    2123              :          representable.  */
    2124            0 :       chunk = 1000000000;
    2125            0 :       chunk_digits = 9;
    2126              : 
    2127            0 :       if (sizeof (HOST_WIDE_INT) >= 8)
    2128              :         {
    2129              :           /* It is at least 64 bits, so 10^18 is representable.  */
    2130            0 :           chunk_digits = 18;
    2131            0 :           chunk *= chunk;
    2132              :         }
    2133              : 
    2134            0 :       type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
    2135            0 :       base = build_int_cstu (type, chunk);
    2136            0 :       n = wide_int_to_tree (type, wi::to_wide (cst));
    2137              : 
    2138            0 :       if (sign < 0)
    2139              :         {
    2140            0 :           write_char ('n');
    2141            0 :           n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
    2142              :         }
    2143            0 :       do
    2144              :         {
    2145            0 :           tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
    2146            0 :           tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
    2147            0 :           unsigned c;
    2148              : 
    2149            0 :           done = integer_zerop (d);
    2150            0 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
    2151            0 :           c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
    2152              :                               done ? 1 : chunk_digits);
    2153            0 :           ptr -= c;
    2154            0 :           count += c;
    2155            0 :           n = d;
    2156              :         }
    2157            0 :       while (!done);
    2158            0 :       write_chars (ptr, count);
    2159              :     }
    2160              :   else
    2161              :     {
    2162              :       /* A small num.  */
    2163     90664491 :       if (sign < 0)
    2164       477415 :         write_char ('n');
    2165     90664491 :       write_unsigned_number (abs_value.to_uhwi ());
    2166              :     }
    2167     90664491 : }
    2168              : 
    2169              : /* Write out a floating-point literal.
    2170              : 
    2171              :     "Floating-point literals are encoded using the bit pattern of the
    2172              :     target processor's internal representation of that number, as a
    2173              :     fixed-length lowercase hexadecimal string, high-order bytes first
    2174              :     (even if the target processor would store low-order bytes first).
    2175              :     The "n" prefix is not used for floating-point literals; the sign
    2176              :     bit is encoded with the rest of the number.
    2177              : 
    2178              :     Here are some examples, assuming the IEEE standard representation
    2179              :     for floating point numbers.  (Spaces are for readability, not
    2180              :     part of the encoding.)
    2181              : 
    2182              :         1.0f                    Lf 3f80 0000 E
    2183              :        -1.0f                    Lf bf80 0000 E
    2184              :         1.17549435e-38f         Lf 0080 0000 E
    2185              :         1.40129846e-45f         Lf 0000 0001 E
    2186              :         0.0f                    Lf 0000 0000 E"
    2187              : 
    2188              :    Caller is responsible for the Lx and the E.  */
    2189              : static void
    2190         1069 : write_real_cst (const tree value)
    2191              : {
    2192         1069 :   long target_real[4];  /* largest supported float */
    2193              :   /* Buffer for eight hex digits in a 32-bit number but big enough
    2194              :      even for 64-bit long to avoid warnings.  */
    2195         1069 :   char buffer[17];
    2196         1069 :   int i, limit, dir;
    2197              : 
    2198         1069 :   tree type = TREE_TYPE (value);
    2199         1069 :   int bits = GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type));
    2200         1069 :   int words = bits / 32;
    2201              : 
    2202         1069 :   real_to_target (target_real, &TREE_REAL_CST (value),
    2203         1069 :                   TYPE_MODE (type));
    2204              : 
    2205         1069 :   if (words == 0)
    2206              :     {
    2207              :       /* _Float16 and std::bfloat16_t are the only supported types smaller than
    2208              :          32 bits.  */
    2209            6 :       gcc_assert (bits == 16);
    2210            6 :       sprintf (buffer, "%04lx", (unsigned long) target_real[0]);
    2211            6 :       write_chars (buffer, 4);
    2212            6 :       return;
    2213              :     }
    2214              : 
    2215         1063 :   gcc_assert (bits % 32 == 0);
    2216              : 
    2217              :   /* The value in target_real is in the target word order,
    2218              :      so we must write it out backward if that happens to be
    2219              :      little-endian.  write_number cannot be used, it will
    2220              :      produce uppercase.  */
    2221         1063 :   if (FLOAT_WORDS_BIG_ENDIAN)
    2222              :     i = 0, limit = words, dir = 1;
    2223              :   else
    2224         1063 :     i = words - 1, limit = -1, dir = -1;
    2225              : 
    2226         1091 :   if (GET_MODE_PRECISION (SCALAR_FLOAT_TYPE_MODE (type)) == 80
    2227         1063 :       && abi_check (21))
    2228              :     {
    2229              :       /* For -fabi-version=21 and above mangle
    2230              :          Intel/Motorola extended format 1.0L as
    2231              :          3fff8000000000000000
    2232              :          rather than the previous
    2233              :          0000000000003fff8000000000000000 (x86_64)
    2234              :          00003fff8000000000000000 (ia32)
    2235              :          3fff00008000000000000000 (m68k -mc68020)
    2236              :          i.e. without any embedded padding bits.  */
    2237           30 :       if (words == 4)
    2238              :         i += dir;
    2239              :       else
    2240            0 :         gcc_assert (words == 3);
    2241           30 :       unsigned long val = (unsigned long) target_real[i];
    2242           30 :       if (REAL_MODE_FORMAT (SCALAR_FLOAT_TYPE_MODE (type))->signbit_ro == 95)
    2243            0 :         val >>= 16;
    2244           30 :       sprintf (buffer, "%04lx", val);
    2245           30 :       write_chars (buffer, 4);
    2246           30 :       i += dir;
    2247              :     }
    2248              : 
    2249         3013 :   for (; i != limit; i += dir)
    2250              :     {
    2251         1950 :       sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
    2252         1950 :       write_chars (buffer, 8);
    2253              :     }
    2254              : }
    2255              : 
    2256              : /* Non-terminal <identifier>.
    2257              : 
    2258              :      <identifier> ::= </unqualified source code identifier>  */
    2259              : 
    2260              : static void
    2261    723401444 : write_identifier (const char *identifier)
    2262              : {
    2263    723401444 :   MANGLE_TRACE ("identifier", identifier);
    2264    723401444 :   write_string (identifier);
    2265    723401444 : }
    2266              : 
    2267              : /* Handle constructor productions of non-terminal <special-name>.
    2268              :    CTOR is a constructor FUNCTION_DECL.
    2269              : 
    2270              :      <special-name> ::= C1   # complete object constructor
    2271              :                     ::= C2   # base object constructor
    2272              :                     ::= C3   # complete object allocating constructor
    2273              : 
    2274              :    Currently, allocating constructors are never used.  */
    2275              : 
    2276              : static void
    2277     29188471 : write_special_name_constructor (const tree ctor)
    2278              : {
    2279     29188471 :   write_char ('C');
    2280     29188471 :   bool new_inh = (flag_new_inheriting_ctors
    2281     58361215 :                   && DECL_INHERITED_CTOR (ctor));
    2282       153510 :   if (new_inh)
    2283       153510 :     write_char ('I');
    2284     29188471 :   if (DECL_BASE_CONSTRUCTOR_P (ctor))
    2285      6031786 :     write_char ('2');
    2286              :   /* This is the old-style "[unified]" constructor.
    2287              :      In some cases, we may emit this function and call
    2288              :      it from the clones in order to share code and save space.  */
    2289     23156685 :   else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
    2290     17128159 :     write_char ('4');
    2291              :   else
    2292              :     {
    2293      6028526 :       gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
    2294      6028526 :       write_char ('1');
    2295              :     }
    2296     29188471 :   if (new_inh)
    2297       307020 :     write_type (DECL_INHERITED_CTOR_BASE (ctor));
    2298     29188471 : }
    2299              : 
    2300              : /* Handle destructor productions of non-terminal <special-name>.
    2301              :    DTOR is a destructor FUNCTION_DECL.
    2302              : 
    2303              :      <special-name> ::= D0 # deleting (in-charge) destructor
    2304              :                     ::= D1 # complete object (in-charge) destructor
    2305              :                     ::= D2 # base object (not-in-charge) destructor  */
    2306              : 
    2307              : static void
    2308      7081952 : write_special_name_destructor (const tree dtor)
    2309              : {
    2310      7081952 :   if (DECL_DELETING_DESTRUCTOR_P (dtor))
    2311       609222 :     write_string ("D0");
    2312      6472730 :   else if (DECL_BASE_DESTRUCTOR_P (dtor))
    2313      1535882 :     write_string ("D2");
    2314      4936848 :   else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
    2315              :     /* This is the old-style "[unified]" destructor.
    2316              :        In some cases, we may emit this function and call
    2317              :        it from the clones in order to share code and save space.  */
    2318      2815739 :     write_string ("D4");
    2319              :   else
    2320              :     {
    2321      2121109 :       gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
    2322      2121109 :       write_string ("D1");
    2323              :     }
    2324      7081952 : }
    2325              : 
    2326              : /* Return the discriminator for ENTITY appearing inside
    2327              :    FUNCTION.  The discriminator is the lexical ordinal of VAR or TYPE among
    2328              :    entities with the same name and kind in the same FUNCTION.  */
    2329              : 
    2330              : static int
    2331      2631945 : discriminator_for_local_entity (tree entity)
    2332              : {
    2333      2631945 :   if (!DECL_LANG_SPECIFIC (entity))
    2334              :     {
    2335              :       /* Some decls, like __FUNCTION__, don't need a discriminator.  */
    2336        31004 :       gcc_checking_assert (DECL_ARTIFICIAL (entity));
    2337              :       return 0;
    2338              :     }
    2339      5062483 :   else if (tree disc = DECL_DISCRIMINATOR (entity))
    2340          469 :     return TREE_INT_CST_LOW (disc);
    2341              :   else
    2342              :     /* The first entity with a particular name doesn't get
    2343              :        DECL_DISCRIMINATOR set up.  */
    2344              :     return 0;
    2345              : }
    2346              : 
    2347              : /* Return the discriminator for STRING, a string literal used inside
    2348              :    FUNCTION.  The discriminator is the lexical ordinal of STRING among
    2349              :    string literals used in FUNCTION.  */
    2350              : 
    2351              : static int
    2352            0 : discriminator_for_string_literal (tree /*function*/,
    2353              :                                   tree /*string*/)
    2354              : {
    2355              :   /* For now, we don't discriminate amongst string literals.  */
    2356            0 :   return 0;
    2357              : }
    2358              : 
    2359              : /*   <discriminator> := _ <number>    # when number < 10
    2360              :                      := __ <number> _ # when number >= 10
    2361              : 
    2362              :    The discriminator is used only for the second and later occurrences
    2363              :    of the same name within a single function. In this case <number> is
    2364              :    n - 2, if this is the nth occurrence, in lexical order.  */
    2365              : 
    2366              : static void
    2367      2631614 : write_discriminator (const int discriminator)
    2368              : {
    2369              :   /* If discriminator is zero, don't write anything.  Otherwise...  */
    2370      2631614 :   if (discriminator > 0)
    2371              :     {
    2372          451 :       write_char ('_');
    2373          451 :       if (discriminator - 1 >= 10)
    2374              :         {
    2375            6 :           if (abi_check (11))
    2376            6 :             write_char ('_');
    2377              :         }
    2378          451 :       write_unsigned_number (discriminator - 1);
    2379          451 :       if (abi_version_at_least (11) && discriminator - 1 >= 10)
    2380            6 :         write_char ('_');
    2381              :     }
    2382      2631614 : }
    2383              : 
    2384              : /* Mangle the name of a function-scope entity.  FUNCTION is the
    2385              :    FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
    2386              :    default argument scope.  ENTITY is the decl for the entity itself.
    2387              :    LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
    2388              :    either ENTITY itself or an enclosing scope of ENTITY.
    2389              : 
    2390              :      <local-name> := Z <function encoding> E <entity name> [<discriminator>]
    2391              :                   := Z <function encoding> E s [<discriminator>]
    2392              :                   := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
    2393              : 
    2394              : static void
    2395      4396181 : write_local_name (tree function, const tree local_entity,
    2396              :                   const tree entity)
    2397              : {
    2398      4396181 :   tree parm = NULL_TREE;
    2399              : 
    2400      4396181 :   MANGLE_TRACE_TREE ("local-name", entity);
    2401              : 
    2402      4396181 :   if (TREE_CODE (function) == PARM_DECL)
    2403              :     {
    2404          357 :       parm = function;
    2405          357 :       function = DECL_CONTEXT (parm);
    2406              :     }
    2407              : 
    2408      4396181 :   write_char ('Z');
    2409      4396181 :   write_encoding (function);
    2410      4396181 :   write_char ('E');
    2411              : 
    2412              :   /* For this purpose, parameters are numbered from right-to-left.  */
    2413      4396181 :   if (parm)
    2414              :     {
    2415          357 :       int i = list_length (parm);
    2416          357 :       write_char ('d');
    2417          357 :       write_compact_number (i - 1);
    2418              :     }
    2419              : 
    2420      4396181 :   if (TREE_CODE (entity) == STRING_CST)
    2421              :     {
    2422            0 :       write_char ('s');
    2423            0 :       write_discriminator (discriminator_for_string_literal (function,
    2424              :                                                              entity));
    2425              :     }
    2426              :   else
    2427              :     {
    2428              :       /* Now the <entity name>.  Let write_name know its being called
    2429              :          from <local-name>, so it doesn't try to process the enclosing
    2430              :          function scope again.  */
    2431      4396181 :       write_name (entity, /*ignore_local_scope=*/1);
    2432      4396181 :       if (DECL_DISCRIMINATOR_P (local_entity)
    2433     13017547 :           && !(TREE_CODE (local_entity) == TYPE_DECL
    2434     12676839 :                && TYPE_ANON_P (TREE_TYPE (local_entity))))
    2435      2631351 :         write_discriminator (discriminator_for_local_entity (local_entity));
    2436              :     }
    2437      4396181 : }
    2438              : 
    2439              : /* Non-terminals <type> and <CV-qualifier>.
    2440              : 
    2441              :      <type> ::= <builtin-type>
    2442              :             ::= <function-type>
    2443              :             ::= <class-enum-type>
    2444              :             ::= <array-type>
    2445              :             ::= <pointer-to-member-type>
    2446              :             ::= <template-param>
    2447              :             ::= <substitution>
    2448              :             ::= <CV-qualifier>
    2449              :             ::= P <type>    # pointer-to
    2450              :             ::= R <type>    # reference-to
    2451              :             ::= C <type>    # complex pair (C 2000)
    2452              :             ::= G <type>    # imaginary (C 2000)     [not supported]
    2453              :             ::= U <source-name> <type>   # vendor extended type qualifier
    2454              : 
    2455              :    C++11 extensions
    2456              : 
    2457              :      <type> ::= RR <type>   # rvalue reference-to
    2458              :      <type> ::= Dt <expression> # decltype of an id-expression or
    2459              :                                 # class member access
    2460              :      <type> ::= DT <expression> # decltype of an expression
    2461              :      <type> ::= Dn              # decltype of nullptr
    2462              :      <type> ::= Dm                # decltype of ^^int
    2463              :      <type> ::= <splice>    # C++26 dependent splice [proposed]
    2464              : 
    2465              :    TYPE is a type node.  */
    2466              : 
    2467              : static void
    2468   1235314812 : write_type (tree type)
    2469              : {
    2470              :   /* This gets set to nonzero if TYPE turns out to be a (possibly
    2471              :      CV-qualified) builtin type.  */
    2472   1235314821 :   int is_builtin_type = 0;
    2473              : 
    2474   1235314821 :   MANGLE_TRACE_TREE ("type", type);
    2475              : 
    2476   1235314821 :   if (type == error_mark_node)
    2477              :     return;
    2478              : 
    2479   1235314806 :   type = canonicalize_for_substitution (type);
    2480   1235314806 :   if (find_substitution (type))
    2481              :     return;
    2482              : 
    2483              : 
    2484   1126490863 :   if (write_CV_qualifiers_for_type (type) > 0)
    2485              :     /* If TYPE was CV-qualified, we just wrote the qualifiers; now
    2486              :        mangle the unqualified type.  The recursive call is needed here
    2487              :        since both the qualified and unqualified types are substitution
    2488              :        candidates.  */
    2489              :     {
    2490     80305836 :       tree t = TYPE_MAIN_VARIANT (type);
    2491     80305836 :       if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t))
    2492              :         {
    2493           99 :           tree attrs = NULL_TREE;
    2494           99 :           if (tx_safe_fn_type_p (type))
    2495            3 :             attrs = tree_cons (get_identifier ("transaction_safe"),
    2496              :                                NULL_TREE, attrs);
    2497           99 :           t = cp_build_type_attribute_variant (t, attrs);
    2498              :         }
    2499     80305836 :       gcc_assert (t != type);
    2500     80305836 :       if (FUNC_OR_METHOD_TYPE_P (t))
    2501              :         {
    2502         3987 :           t = build_ref_qualified_type (t, type_memfn_rqual (type));
    2503         3987 :           if (flag_noexcept_type)
    2504              :             {
    2505         3966 :               tree r = TYPE_RAISES_EXCEPTIONS (type);
    2506         3966 :               t = build_exception_variant (t, r);
    2507              :             }
    2508         3987 :           if (abi_version_at_least (8)
    2509         4011 :               || type == TYPE_MAIN_VARIANT (type))
    2510              :             /* Avoid adding the unqualified function type as a substitution.  */
    2511         3963 :             write_function_type (t);
    2512              :           else
    2513           24 :             write_type (t);
    2514        19818 :           if (abi_warn_or_compat_version_crosses (8))
    2515           39 :             G.need_abi_warning = 1;
    2516              :         }
    2517              :       else
    2518     80301849 :         write_type (t);
    2519              :     }
    2520   1046185027 :   else if (TREE_CODE (type) == ARRAY_TYPE)
    2521              :     /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
    2522              :        so that the cv-qualification of the element type is available
    2523              :        in write_array_type.  */
    2524      1471652 :     write_array_type (type);
    2525              :   else
    2526              :     {
    2527   1044713375 :       tree type_orig = type;
    2528              : 
    2529              :       /* See through any typedefs.  */
    2530   1044713375 :       type = TYPE_MAIN_VARIANT (type);
    2531   1044713375 :       if (FUNC_OR_METHOD_TYPE_P (type))
    2532      3109600 :         type = cxx_copy_lang_qualifiers (type, type_orig);
    2533              : 
    2534              :       /* According to the C++ ABI, some library classes are passed the
    2535              :          same as the scalar type of their single member and use the same
    2536              :          mangling.  */
    2537   1044713375 :       if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
    2538        16206 :         type = TREE_TYPE (first_field (type));
    2539              : 
    2540   1044713375 :       if (TYPE_PTRDATAMEM_P (type))
    2541        11454 :         write_pointer_to_member_type (type);
    2542              :       else
    2543              :         {
    2544              :           /* Handle any target-specific fundamental types.  */
    2545   1044701921 :           const char *target_mangling
    2546   1044701921 :             = targetm.mangle_type (type_orig);
    2547              : 
    2548   1044701921 :           if (target_mangling)
    2549              :             {
    2550      9052092 :               write_string (target_mangling);
    2551              :               /* Add substitutions for types other than fundamental
    2552              :                  types.  */
    2553      9052092 :               if (!VOID_TYPE_P (type)
    2554              :                   && TREE_CODE (type) != INTEGER_TYPE
    2555              :                   && TREE_CODE (type) != REAL_TYPE
    2556              :                   && TREE_CODE (type) != BOOLEAN_TYPE)
    2557            0 :                 add_substitution (type);
    2558      9052092 :               return;
    2559              :             }
    2560              : 
    2561   1035649829 :           switch (TREE_CODE (type))
    2562              :             {
    2563    555775197 :             case VOID_TYPE:
    2564    555775197 :             case BOOLEAN_TYPE:
    2565    555775197 :             case INTEGER_TYPE:  /* Includes wchar_t.  */
    2566    555775197 :             case REAL_TYPE:
    2567    555775197 :             case FIXED_POINT_TYPE:
    2568    555775197 :               {
    2569              :                 /* If this is a typedef, TYPE may not be one of
    2570              :                    the standard builtin type nodes, but an alias of one.  Use
    2571              :                    TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
    2572    555775197 :                 write_builtin_type (TYPE_MAIN_VARIANT (type));
    2573    555775197 :                 ++is_builtin_type;
    2574              :               }
    2575    555775197 :               break;
    2576              : 
    2577       641250 :             case COMPLEX_TYPE:
    2578       641250 :               write_char ('C');
    2579       641250 :               write_type (TREE_TYPE (type));
    2580       641250 :               break;
    2581              : 
    2582      3109600 :             case FUNCTION_TYPE:
    2583      3109600 :             case METHOD_TYPE:
    2584      3109600 :               write_function_type (type);
    2585      3109600 :               break;
    2586              : 
    2587    313789435 :             case UNION_TYPE:
    2588    313789435 :             case RECORD_TYPE:
    2589    313789435 :             case ENUMERAL_TYPE:
    2590              :               /* A pointer-to-member function is represented as a special
    2591              :                  RECORD_TYPE, so check for this first.  */
    2592    313789435 :               if (TYPE_PTRMEMFUNC_P (type))
    2593       258905 :                 write_pointer_to_member_type (type);
    2594              :               else
    2595    313530530 :                 write_class_enum_type (type);
    2596              :               break;
    2597              : 
    2598      2395814 :             case TYPENAME_TYPE:
    2599      2395814 :             case UNBOUND_CLASS_TEMPLATE:
    2600              :               /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
    2601              :                  ordinary nested names.  */
    2602      2395814 :               write_nested_name (TYPE_STUB_DECL (type));
    2603      2395814 :               break;
    2604              : 
    2605    128639334 :             case POINTER_TYPE:
    2606    128639334 :             case REFERENCE_TYPE:
    2607    128639334 :               if (TYPE_PTR_P (type))
    2608     55206133 :                 write_char ('P');
    2609     73433201 :               else if (TYPE_REF_IS_RVALUE (type))
    2610     12336722 :                 write_char ('O');
    2611              :               else
    2612     61096479 :                 write_char ('R');
    2613    128639334 :               {
    2614    128639334 :                 tree target = TREE_TYPE (type);
    2615              :                 /* Attribute const/noreturn are not reflected in mangling.
    2616              :                    We strip them here rather than at a lower level because
    2617              :                    a typedef or template argument can have function type
    2618              :                    with function-cv-quals (that use the same representation),
    2619              :                    but you can't have a pointer/reference to such a type.  */
    2620    128639334 :                 if (TREE_CODE (target) == FUNCTION_TYPE)
    2621              :                   {
    2622     10551790 :                     if (abi_warn_or_compat_version_crosses (5)
    2623      2110712 :                         && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
    2624            3 :                       G.need_abi_warning = 1;
    2625      2110706 :                     if (abi_version_at_least (5))
    2626      2110277 :                       target = build_qualified_type (target, TYPE_UNQUALIFIED);
    2627              :                   }
    2628    128639334 :                 write_type (target);
    2629              :               }
    2630    128639334 :               break;
    2631              : 
    2632     26814387 :             case TEMPLATE_TYPE_PARM:
    2633     26814387 :               if (is_auto (type))
    2634              :                 {
    2635       592980 :                   if (template_placeholder_p (type)
    2636       592980 :                       && abi_check (19))
    2637              :                     {
    2638              :                       /* ABI #109: placeholder is mangled as its template.  */
    2639          182 :                       type = CLASS_PLACEHOLDER_TEMPLATE (type);
    2640          182 :                       if (find_substitution (type))
    2641              :                         return;
    2642           33 :                       write_name (type, 0);
    2643           33 :                       break;
    2644              :                     }
    2645       592798 :                   if (AUTO_IS_DECLTYPE (type))
    2646        17963 :                     write_identifier ("Dc");
    2647              :                   else
    2648       574835 :                     write_identifier ("Da");
    2649              :                   ++is_builtin_type;
    2650              :                   break;
    2651              :                 }
    2652              :               /* fall through.  */
    2653     26221407 :             case TEMPLATE_PARM_INDEX:
    2654     26221407 :               write_template_param (type);
    2655     26221407 :               break;
    2656              : 
    2657           93 :             case TEMPLATE_TEMPLATE_PARM:
    2658           93 :               write_template_template_param (type);
    2659           93 :               break;
    2660              : 
    2661          532 :             case BOUND_TEMPLATE_TEMPLATE_PARM:
    2662          532 :               write_template_template_param (type);
    2663          532 :               write_template_args
    2664          532 :                 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
    2665          532 :               break;
    2666              : 
    2667        20330 :             case VECTOR_TYPE:
    2668        20330 :               if (abi_version_at_least (4))
    2669              :                 {
    2670        20315 :                   write_string ("Dv");
    2671              :                   /* Non-constant vector size would be encoded with
    2672              :                      _ expression, but we don't support that yet.  */
    2673        20315 :                   write_unsigned_number (TYPE_VECTOR_SUBPARTS (type)
    2674              :                                          .to_constant ());
    2675        20315 :                   write_char ('_');
    2676        20315 :                 }
    2677              :               else
    2678           15 :                 write_string ("U8__vector");
    2679       101605 :               if (abi_warn_or_compat_version_crosses (4))
    2680           15 :                 G.need_abi_warning = 1;
    2681        20330 :               write_type (TREE_TYPE (type));
    2682        20330 :               break;
    2683              : 
    2684      3204409 :             case TYPE_PACK_EXPANSION:
    2685      3204409 :               write_string ("Dp");
    2686      3204409 :               write_type (PACK_EXPANSION_PATTERN (type));
    2687      3204409 :               break;
    2688              : 
    2689       405644 :             case DECLTYPE_TYPE:
    2690              :               /* These shouldn't make it into mangling.  */
    2691       405644 :               gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
    2692              :                           && !DECLTYPE_FOR_LAMBDA_PROXY (type));
    2693              : 
    2694              :               /* In ABI <5, we stripped decltype of a plain decl.  */
    2695       405644 :               if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
    2696              :                 {
    2697          228 :                   tree expr = DECLTYPE_TYPE_EXPR (type);
    2698          228 :                   tree etype = NULL_TREE;
    2699          228 :                   switch (TREE_CODE (expr))
    2700              :                     {
    2701          118 :                     case VAR_DECL:
    2702          118 :                     case PARM_DECL:
    2703          118 :                     case RESULT_DECL:
    2704          118 :                     case FUNCTION_DECL:
    2705          118 :                     case CONST_DECL:
    2706          118 :                     case TEMPLATE_PARM_INDEX:
    2707          118 :                       etype = TREE_TYPE (expr);
    2708          118 :                       break;
    2709              : 
    2710              :                     default:
    2711              :                       break;
    2712              :                     }
    2713              : 
    2714          118 :                   if (etype && !type_uses_auto (etype))
    2715              :                     {
    2716          117 :                       if (!abi_check (5))
    2717              :                         {
    2718              :                           write_type (etype);
    2719              :                           return;
    2720              :                         }
    2721              :                     }
    2722              :                 }
    2723              : 
    2724       405635 :               write_char ('D');
    2725       405635 :               if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
    2726          219 :                 write_char ('t');
    2727              :               else
    2728       405416 :                 write_char ('T');
    2729       405635 :               ++cp_unevaluated_operand;
    2730       405635 :               write_expression (DECLTYPE_TYPE_EXPR (type));
    2731       405635 :               --cp_unevaluated_operand;
    2732       405635 :               write_char ('E');
    2733       405635 :               break;
    2734              : 
    2735       767292 :             case NULLPTR_TYPE:
    2736       767292 :               write_string ("Dn");
    2737       767292 :               if (abi_check (7))
    2738              :                 ++is_builtin_type;
    2739              :               break;
    2740              : 
    2741        86442 :             case META_TYPE:
    2742        86442 :               write_string ("Dm");
    2743        86442 :               ++is_builtin_type;
    2744        86442 :               break;
    2745              : 
    2746           24 :             case SPLICE_SCOPE:
    2747           24 :               write_splice (type);
    2748           24 :               break;
    2749              : 
    2750            3 :             case TYPEOF_TYPE:
    2751            3 :               sorry ("mangling %<typeof%>, use %<decltype%> instead");
    2752            3 :               break;
    2753              : 
    2754           21 :             case TRAIT_TYPE:
    2755           21 :               error ("use of built-in trait %qT in function signature; "
    2756              :                      "use library traits instead", type);
    2757           21 :               break;
    2758              : 
    2759           22 :             case PACK_INDEX_TYPE:
    2760              :               /* https://github.com/itanium-cxx-abi/cxx-abi/issues/175.  */
    2761           22 :               write_string ("Dy");
    2762           22 :               if (TREE_CODE (PACK_INDEX_PACK (type)) == TREE_VEC)
    2763              :                 {
    2764            3 :                   write_char ('J');
    2765           20 :                   for (int i = 0; i < TREE_VEC_LENGTH (PACK_INDEX_PACK (type));
    2766              :                        ++i)
    2767            7 :                     write_template_arg (TREE_VEC_ELT (PACK_INDEX_PACK (type),
    2768              :                                                       i));
    2769            3 :                   write_char ('E');
    2770              :                 }
    2771              :               else
    2772              :                 /* Dy rather than DyDp.  */
    2773           19 :                 write_type (PACK_EXPANSION_PATTERN (PACK_INDEX_PACK (type)));
    2774           22 :               write_expression (PACK_INDEX_INDEX (type));
    2775           22 :               break;
    2776              : 
    2777            0 :             case LANG_TYPE:
    2778              :               /* fall through.  */
    2779              : 
    2780            0 :             default:
    2781            0 :               gcc_unreachable ();
    2782              :             }
    2783              :         }
    2784              :     }
    2785              : 
    2786              :   /* Types other than builtin types are substitution candidates.  */
    2787   1116074575 :   if (!is_builtin_type)
    2788    560216886 :     add_substitution (type);
    2789              : }
    2790              : 
    2791              : /* qsort callback for sorting a vector of attribute entries.  */
    2792              : 
    2793              : static int
    2794            0 : attr_strcmp (const void *p1, const void *p2)
    2795              : {
    2796            0 :   tree a1 = *(const tree*)p1;
    2797            0 :   tree a2 = *(const tree*)p2;
    2798              : 
    2799            0 :   const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1));
    2800            0 :   const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2));
    2801              : 
    2802            0 :   return strcmp (as1->name, as2->name);
    2803              : }
    2804              : 
    2805              : /* Return true if we should mangle a type attribute with name NAME.  */
    2806              : 
    2807              : static bool
    2808        12389 : mangle_type_attribute_p (tree name)
    2809              : {
    2810        12389 :   const attribute_spec *as = lookup_attribute_spec (name);
    2811        12389 :   if (!as || !as->affects_type_identity)
    2812              :     return false;
    2813              : 
    2814              :   /* Skip internal-only attributes, which are distinguished from others
    2815              :      by having a space.  At present, all internal-only attributes that
    2816              :      affect type identity are target-specific and are handled by
    2817              :      targetm.mangle_type instead.
    2818              : 
    2819              :      Another reason to do this is that a space isn't a valid identifier
    2820              :      character for most file formats.  */
    2821           57 :   if (strchr (IDENTIFIER_POINTER (name), ' '))
    2822              :     return false;
    2823              : 
    2824              :   /* The following attributes are mangled specially.  */
    2825           57 :   if (is_attribute_p ("transaction_safe", name))
    2826              :     return false;
    2827           33 :   if (is_attribute_p ("abi_tag", name))
    2828            0 :     return false;
    2829              : 
    2830              :   return true;
    2831              : }
    2832              : 
    2833              : /* Non-terminal <CV-qualifiers> for type nodes.  Returns the number of
    2834              :    CV-qualifiers written for TYPE.
    2835              : 
    2836              :      <CV-qualifiers> ::= [r] [V] [K]  */
    2837              : 
    2838              : static int
    2839   1126749768 : write_CV_qualifiers_for_type (const tree type)
    2840              : {
    2841   1126749768 :   int num_qualifiers = 0;
    2842              : 
    2843              :   /* The order is specified by:
    2844              : 
    2845              :        "In cases where multiple order-insensitive qualifiers are
    2846              :        present, they should be ordered 'K' (closest to the base type),
    2847              :        'V', 'r', and 'U' (farthest from the base type) ..."  */
    2848              : 
    2849              :   /* Mangle attributes that affect type identity as extended qualifiers.
    2850              : 
    2851              :      We don't do this with classes and enums because their attributes
    2852              :      are part of their definitions, not something added on.  */
    2853              : 
    2854   1126749768 :   if (!OVERLOAD_TYPE_P (type))
    2855              :     {
    2856    778366377 :       auto_vec<tree> vec;
    2857    778378766 :       for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
    2858        12389 :         if (mangle_type_attribute_p (get_attribute_name (a)))
    2859           33 :           vec.safe_push (a);
    2860   3891071782 :       if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
    2861            0 :         G.need_abi_warning = true;
    2862    778366377 :       if (abi_version_at_least (10))
    2863              :         {
    2864    778117748 :           vec.qsort (attr_strcmp);
    2865    778366443 :           while (!vec.is_empty())
    2866              :             {
    2867           33 :               tree a = vec.pop();
    2868           33 :               const attribute_spec *as
    2869           33 :                 = lookup_attribute_spec (get_attribute_name (a));
    2870              : 
    2871           33 :               write_char ('U');
    2872           33 :               write_unsigned_number (strlen (as->name));
    2873           33 :               write_string (as->name);
    2874           33 :               if (TREE_VALUE (a))
    2875              :                 {
    2876            3 :                   write_char ('I');
    2877            6 :                   for (tree args = TREE_VALUE (a); args;
    2878            3 :                        args = TREE_CHAIN (args))
    2879              :                     {
    2880            3 :                       tree arg = TREE_VALUE (args);
    2881            3 :                       write_template_arg (arg);
    2882              :                     }
    2883            3 :                   write_char ('E');
    2884              :                 }
    2885              : 
    2886           33 :               ++num_qualifiers;
    2887              :             }
    2888              :         }
    2889    778366377 :     }
    2890              : 
    2891              :   /* Note that we do not use cp_type_quals below; given "const
    2892              :      int[3]", the "const" is emitted with the "int", not with the
    2893              :      array.  */
    2894   1126749768 :   cp_cv_quals quals = TYPE_QUALS (type);
    2895              : 
    2896   1126749768 :   if (quals & TYPE_QUAL_RESTRICT)
    2897              :     {
    2898           43 :       write_char ('r');
    2899           43 :       ++num_qualifiers;
    2900              :     }
    2901   1126749768 :   if (quals & TYPE_QUAL_VOLATILE)
    2902              :     {
    2903       511765 :       write_char ('V');
    2904       511765 :       ++num_qualifiers;
    2905              :     }
    2906   1126749768 :   if (quals & TYPE_QUAL_CONST)
    2907              :     {
    2908     80242962 :       write_char ('K');
    2909     80242962 :       ++num_qualifiers;
    2910              :     }
    2911              : 
    2912   1126749768 :   return num_qualifiers;
    2913              : }
    2914              : 
    2915              : /* Non-terminal <builtin-type>.
    2916              : 
    2917              :      <builtin-type> ::= v   # void
    2918              :                     ::= b   # bool
    2919              :                     ::= w   # wchar_t
    2920              :                     ::= c   # char
    2921              :                     ::= a   # signed char
    2922              :                     ::= h   # unsigned char
    2923              :                     ::= s   # short
    2924              :                     ::= t   # unsigned short
    2925              :                     ::= i   # int
    2926              :                     ::= j   # unsigned int
    2927              :                     ::= l   # long
    2928              :                     ::= m   # unsigned long
    2929              :                     ::= x   # long long, __int64
    2930              :                     ::= y   # unsigned long long, __int64
    2931              :                     ::= n   # __int128
    2932              :                     ::= o   # unsigned __int128
    2933              :                     ::= f   # float
    2934              :                     ::= d   # double
    2935              :                     ::= e   # long double, __float80
    2936              :                     ::= g   # __float128          [not supported]
    2937              :                     ::= u <source-name>  # vendor extended type */
    2938              : 
    2939              : static void
    2940    555775197 : write_builtin_type (tree type)
    2941              : {
    2942    555775197 :   if (TYPE_CANONICAL (type))
    2943    555775197 :     type = TYPE_CANONICAL (type);
    2944              : 
    2945    555775197 :   switch (TREE_CODE (type))
    2946              :     {
    2947     75561610 :     case VOID_TYPE:
    2948     75561610 :       write_char ('v');
    2949     75561610 :       break;
    2950              : 
    2951     48566312 :     case BOOLEAN_TYPE:
    2952     48566312 :       write_char ('b');
    2953     48566312 :       break;
    2954              : 
    2955    414427518 :     case INTEGER_TYPE:
    2956              :       /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
    2957              :          isn't in integer_type_nodes.  */
    2958    414427518 :       if (type == wchar_type_node)
    2959     41400146 :         write_char ('w');
    2960    373027372 :       else if (type == char8_type_node)
    2961     22033427 :         write_string ("Du");
    2962    350993945 :       else if (type == char16_type_node)
    2963     21844180 :         write_string ("Ds");
    2964    329149765 :       else if (type == char32_type_node)
    2965     24207633 :         write_string ("Di");
    2966              :       else
    2967              :         {
    2968    304942145 :           size_t itk;
    2969              :           /* Assume TYPE is one of the shared integer type nodes.  Find
    2970              :              it in the array of these nodes.  */
    2971    304942132 :         iagain:
    2972   1934927394 :           for (itk = 0; itk < itk_none; ++itk)
    2973   1933491024 :             if (integer_types[itk] != NULL_TREE
    2974   1924872804 :                 && integer_type_codes[itk] != '\0'
    2975   1922000064 :                 && type == integer_types[itk])
    2976              :               {
    2977              :                 /* Print the corresponding single-letter code.  */
    2978    303505775 :                 write_char (integer_type_codes[itk]);
    2979    303505775 :                 break;
    2980              :               }
    2981              : 
    2982    304942145 :           if (itk == itk_none)
    2983              :             {
    2984      1436370 :               tree t = c_common_type_for_mode (TYPE_MODE (type),
    2985      1436370 :                                                TYPE_UNSIGNED (type));
    2986      1436370 :               if (type != t)
    2987              :                 {
    2988           13 :                   type = t;
    2989           13 :                   goto iagain;
    2990              :                 }
    2991              : 
    2992      1436357 :               if (TYPE_PRECISION (type) == 128)
    2993      2000474 :                 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
    2994              :               else
    2995              :                 {
    2996              :                   /* Allow for cases where TYPE is not one of the shared
    2997              :                      integer type nodes and write a "vendor extended builtin
    2998              :                      type" with a name the form intN or uintN, respectively.
    2999              :                      Situations like this can happen if you have an
    3000              :                      __attribute__((__mode__(__SI__))) type and use exotic
    3001              :                      switches like '-mint8' on AVR.  Of course, this is
    3002              :                      undefined by the C++ ABI (and '-mint8' is not even
    3003              :                      Standard C conforming), but when using such special
    3004              :                      options you're pretty much in nowhere land anyway.  */
    3005            0 :                   const char *prefix;
    3006            0 :                   char prec[11];        /* up to ten digits for an unsigned */
    3007              : 
    3008            0 :                   prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
    3009            0 :                   sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
    3010            0 :                   write_char ('u');     /* "vendor extended builtin type" */
    3011            0 :                   write_unsigned_number (strlen (prefix) + strlen (prec));
    3012            0 :                   write_string (prefix);
    3013            0 :                   write_string (prec);
    3014              :                 }
    3015              :             }
    3016              :         }
    3017              :       break;
    3018              : 
    3019     17219757 :     case REAL_TYPE:
    3020     17219757 :       if (type == float_type_node)
    3021      5688893 :         write_char ('f');
    3022     11530864 :       else if (type == double_type_node)
    3023      9457541 :         write_char ('d');
    3024      2073323 :       else if (type == long_double_type_node)
    3025            0 :         write_char ('e');
    3026      2073323 :       else if (type == dfloat32_type_node)
    3027         6152 :         write_string ("Df");
    3028      2067171 :       else if (type == dfloat64_type_node)
    3029         6041 :         write_string ("Dd");
    3030      2061130 :       else if (type == dfloat128_type_node)
    3031         6008 :         write_string ("De");
    3032      2055122 :       else if (type == float16_type_node)
    3033            0 :         write_string ("DF16_");
    3034      2055122 :       else if (type == float32_type_node)
    3035       671406 :         write_string ("DF32_");
    3036      1383716 :       else if (type == float64_type_node)
    3037       671436 :         write_string ("DF64_");
    3038       712280 :       else if (type == float128_type_node)
    3039       654850 :         write_string ("DF128_");
    3040        57430 :       else if (type == float32x_type_node)
    3041        28715 :         write_string ("DF32x");
    3042        28715 :       else if (type == float64x_type_node)
    3043        28715 :         write_string ("DF64x");
    3044            0 :       else if (type == float128x_type_node)
    3045            0 :         write_string ("DF128x");
    3046            0 :       else if (type == bfloat16_type_node)
    3047            0 :         write_string ("DF16b");
    3048              :       else
    3049            0 :         gcc_unreachable ();
    3050              :       break;
    3051              : 
    3052            0 :     default:
    3053            0 :       gcc_unreachable ();
    3054              :     }
    3055    555775197 : }
    3056              : 
    3057              : /* Non-terminal <function-type>.  NODE is a FUNCTION_TYPE or
    3058              :    METHOD_TYPE.  The return type is mangled before the parameter
    3059              :    types.
    3060              : 
    3061              :      <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E   */
    3062              : 
    3063              : static void
    3064      3113563 : write_function_type (const tree type)
    3065              : {
    3066      3113563 :   MANGLE_TRACE_TREE ("function-type", type);
    3067              : 
    3068              :   /* For a pointer to member function, the function type may have
    3069              :      cv-qualifiers, indicating the quals for the artificial 'this'
    3070              :      parameter.  */
    3071      3113563 :   if (TREE_CODE (type) == METHOD_TYPE)
    3072              :     {
    3073              :       /* The first parameter must be a POINTER_TYPE pointing to the
    3074              :          `this' parameter.  */
    3075       258905 :       tree this_type = class_of_this_parm (type);
    3076       258905 :       write_CV_qualifiers_for_type (this_type);
    3077              :     }
    3078              : 
    3079      3113563 :   write_exception_spec (TYPE_RAISES_EXCEPTIONS (type));
    3080              : 
    3081      3113563 :   if (tx_safe_fn_type_p (type))
    3082           24 :     write_string ("Dx");
    3083              : 
    3084      3113563 :   write_char ('F');
    3085              :   /* We don't track whether or not a type is `extern "C"'.  Note that
    3086              :      you can have an `extern "C"' function that does not have
    3087              :      `extern "C"' type, and vice versa:
    3088              : 
    3089              :        extern "C" typedef void function_t();
    3090              :        function_t f; // f has C++ linkage, but its type is
    3091              :                      // `extern "C"'
    3092              : 
    3093              :        typedef void function_t();
    3094              :        extern "C" function_t f; // Vice versa.
    3095              : 
    3096              :      See [dcl.link].  */
    3097      3113563 :   write_bare_function_type (type, /*include_return_type_p=*/1,
    3098              :                             /*decl=*/NULL);
    3099      3113563 :   if (FUNCTION_REF_QUALIFIED (type))
    3100              :     {
    3101         1567 :       if (FUNCTION_RVALUE_QUALIFIED (type))
    3102          643 :         write_char ('O');
    3103              :       else
    3104          924 :         write_char ('R');
    3105              :     }
    3106      3113563 :   write_char ('E');
    3107      3113563 : }
    3108              : 
    3109              : /* Non-terminal <bare-function-type>.  TYPE is a FUNCTION_TYPE or
    3110              :    METHOD_TYPE.  If INCLUDE_RETURN_TYPE is nonzero, the return value
    3111              :    is mangled before the parameter types.  If non-NULL, DECL is
    3112              :    FUNCTION_DECL for the function whose type is being emitted.  */
    3113              : 
    3114              : static void
    3115    186532490 : write_bare_function_type (const tree type, const int include_return_type_p,
    3116              :                           const tree decl)
    3117              : {
    3118    186532490 :   MANGLE_TRACE_TREE ("bare-function-type", type);
    3119              : 
    3120              :   /* Mangle the return type, if requested.  */
    3121    186532490 :   if (include_return_type_p)
    3122     16411107 :     write_type (TREE_TYPE (type));
    3123              : 
    3124              :   /* Now mangle the types of the arguments.  */
    3125    186532490 :   ++G.parm_depth;
    3126    186532490 :   write_method_parms (TYPE_ARG_TYPES (type),
    3127    186532490 :                       TREE_CODE (type) == METHOD_TYPE,
    3128              :                       decl);
    3129    186532490 :   --G.parm_depth;
    3130    186532490 : }
    3131              : 
    3132              : /* Write the mangled representation of a method parameter list of
    3133              :    types given in PARM_TYPES.  If METHOD_P is nonzero, the function is
    3134              :    considered a non-static method, and the this parameter is omitted.
    3135              :    If non-NULL, DECL is the FUNCTION_DECL for the function whose
    3136              :    parameters are being emitted.  */
    3137              : 
    3138              : static void
    3139    188332322 : write_method_parms (tree parm_types, const int method_p, const tree decl)
    3140              : {
    3141    188332322 :   tree first_parm_type;
    3142    357610871 :   tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
    3143              : 
    3144              :   /* Assume this parameter type list is variable-length.  If it ends
    3145              :      with a void type, then it's not.  */
    3146    188332322 :   int varargs_p = 1;
    3147              : 
    3148              :   /* If this is a member function, skip the first arg, which is the
    3149              :      this pointer.
    3150              :        "Member functions do not encode the type of their implicit this
    3151              :        parameter."
    3152              : 
    3153              :      Similarly, there's no need to mangle artificial parameters, like
    3154              :      the VTT parameters for constructors and destructors.  */
    3155    188332322 :   if (method_p)
    3156              :     {
    3157    147719856 :       parm_types = TREE_CHAIN (parm_types);
    3158    147719856 :       parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
    3159              : 
    3160    147760877 :       while (parm_decl && DECL_ARTIFICIAL (parm_decl))
    3161              :         {
    3162        41021 :           parm_types = TREE_CHAIN (parm_types);
    3163        41021 :           parm_decl = DECL_CHAIN (parm_decl);
    3164              :         }
    3165              : 
    3166    147719856 :       if (decl && ctor_omit_inherited_parms (decl))
    3167              :         /* Bring back parameters omitted from an inherited ctor.  */
    3168           54 :         parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl));
    3169              :     }
    3170              : 
    3171    188332322 :   for (first_parm_type = parm_types;
    3172    588567538 :        parm_types;
    3173    400235216 :        parm_types = TREE_CHAIN (parm_types))
    3174              :     {
    3175    400235216 :       tree parm = TREE_VALUE (parm_types);
    3176    400235216 :       if (parm == void_type_node)
    3177              :         {
    3178              :           /* "Empty parameter lists, whether declared as () or
    3179              :              conventionally as (void), are encoded with a void parameter
    3180              :              (v)."  */
    3181    188288286 :           if (parm_types == first_parm_type)
    3182     63361359 :             write_type (parm);
    3183              :           /* If the parm list is terminated with a void type, it's
    3184              :              fixed-length.  */
    3185    188288286 :           varargs_p = 0;
    3186              :           /* A void type better be the last one.  */
    3187    188288286 :           gcc_assert (TREE_CHAIN (parm_types) == NULL);
    3188              :         }
    3189              :       else
    3190    211946930 :         write_type (parm);
    3191              :     }
    3192              : 
    3193    188332322 :   if (varargs_p)
    3194              :     /* <builtin-type> ::= z  # ellipsis  */
    3195        44036 :     write_char ('z');
    3196    188332322 : }
    3197              : 
    3198              : /* <class-enum-type> ::= <name>  */
    3199              : 
    3200              : static void
    3201    313530530 : write_class_enum_type (const tree type)
    3202              : {
    3203    313530530 :   write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
    3204    313530530 : }
    3205              : 
    3206              : /* Mangle a requirement REQ in a requires-expression.  */
    3207              : 
    3208              : static void
    3209       442007 : write_requirement (tree req)
    3210              : {
    3211       442007 :   tree op = TREE_OPERAND (req, 0);
    3212              : 
    3213       442007 :   switch (tree_code code = TREE_CODE (req))
    3214              :     {
    3215              :       /* # simple-requirement or compound-requirement
    3216              :          <requirement> ::= X <expression> [ N ] [ R <type-constraint> ] */
    3217       441598 :     case SIMPLE_REQ:
    3218       441598 :     case COMPOUND_REQ:
    3219       441598 :       write_char ('X');
    3220       441598 :       write_expression (op);
    3221       441598 :       if (code == SIMPLE_REQ)
    3222              :         break;
    3223       123135 :       if (COMPOUND_REQ_NOEXCEPT_P (req))
    3224            3 :         write_char ('N');
    3225       123135 :       if (tree constr = TREE_OPERAND (req, 1))
    3226              :         {
    3227       123132 :           write_char ('R');
    3228       123132 :           write_type_constraint (PLACEHOLDER_TYPE_CONSTRAINTS (constr));
    3229              :         }
    3230              :       break;
    3231              : 
    3232              :       /* <requirement> ::= T <type> # type-requirement */
    3233          406 :     case TYPE_REQ:
    3234          406 :       write_char ('T');
    3235          406 :       write_type (op);
    3236          406 :       break;
    3237              : 
    3238              :       /* <requirement> ::= Q <constraint-expression> # nested-requirement */
    3239            3 :     case NESTED_REQ:
    3240            3 :       write_char ('Q');
    3241            3 :       write_constraint_expression (op);
    3242            3 :       break;
    3243              : 
    3244            0 :     default:
    3245            0 :       gcc_unreachable ();
    3246              :     }
    3247       442007 : }
    3248              : 
    3249              : /* # requires { ... }
    3250              :    <expression> ::= rq <requirement>+ E
    3251              :    # requires (...) { ... }
    3252              :    <expression> ::= rQ <bare-function-type> _ <requirement>+ E */
    3253              : 
    3254              : static void
    3255       408730 : write_requires_expr (tree expr)
    3256              : {
    3257       408730 :   tree parms = REQUIRES_EXPR_PARMS (expr);
    3258       408730 :   if (parms)
    3259              :     {
    3260        63506 :       write_string ("rQ");
    3261        63506 :       ++G.parm_depth;
    3262       127403 :       for (; parms; parms = DECL_CHAIN (parms))
    3263        63897 :         write_type (cv_unqualified (TREE_TYPE (parms)));
    3264        63506 :       --G.parm_depth;
    3265        63506 :       write_char ('_');
    3266              :     }
    3267              :   else
    3268       345224 :     write_string ("rq");
    3269              : 
    3270       850737 :   for (tree reqs = REQUIRES_EXPR_REQS (expr); reqs;
    3271       442007 :        reqs = TREE_CHAIN (reqs))
    3272       442007 :     write_requirement (TREE_VALUE (reqs));
    3273              : 
    3274       408730 :   write_char ('E');
    3275       408730 : }
    3276              : 
    3277              : /* Non-terminal <template-args>.  ARGS is a TREE_VEC of template
    3278              :    arguments.
    3279              : 
    3280              :      <template-args> ::= I <template-arg>* [Q <constraint-expr>] E  */
    3281              : 
    3282              : static void
    3283    400866898 : write_template_args (tree args, tree parms /*= NULL_TREE*/)
    3284              : {
    3285    400866898 :   int i;
    3286    400866898 :   int length = 0;
    3287              : 
    3288    400866898 :   MANGLE_TRACE_TREE ("template-args", args);
    3289              : 
    3290    400866898 :   write_char ('I');
    3291              : 
    3292    400866898 :   if (args)
    3293    400866898 :     length = TREE_VEC_LENGTH (args);
    3294              : 
    3295    400866898 :   tree constraints = NULL_TREE;
    3296    400866898 :   if (parms)
    3297              :     {
    3298      4707215 :       constraints = TEMPLATE_PARMS_CONSTRAINTS (parms);
    3299      4707215 :       parms = INNERMOST_TEMPLATE_PARMS (parms);
    3300              :     }
    3301              : 
    3302    400866898 :   if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
    3303              :     {
    3304              :       /* We have nested template args.  We want the innermost template
    3305              :          argument list.  */
    3306      5466431 :       args = TREE_VEC_ELT (args, length - 1);
    3307      5466431 :       length = TREE_VEC_LENGTH (args);
    3308              :     }
    3309    400866898 :   if (TEMPLATE_ARGS_TYPE_CONSTRAINT_P (args))
    3310              :     /* Skip the constrained type.  */
    3311              :     i = 1;
    3312              :   else
    3313    400810230 :     i = 0;
    3314              :   bool implicit_parm_scope = false;
    3315   1116249060 :   for (; i < length; ++i)
    3316              :     {
    3317    715382162 :       tree arg = TREE_VEC_ELT (args, i);
    3318    715382162 :       if (parms)
    3319              :         {
    3320      8177171 :           tree parm = TREE_VEC_ELT (parms, i);
    3321      8177171 :           tree decl = TREE_VALUE (parm);
    3322      8177171 :           if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl)
    3323      8177171 :               && !implicit_parm_scope)
    3324              :             {
    3325              :               /* The rest of the template parameters are based on generic
    3326              :                  function parameters, so any expressions in their
    3327              :                  type-constraints are in parameter scope.  */
    3328         5014 :               implicit_parm_scope = true;
    3329         5014 :               ++G.parm_depth;
    3330              :             }
    3331      8177171 :           if (!template_parm_natural_p (arg, parm)
    3332      8177171 :               && abi_check (19))
    3333        26176 :             write_template_param_decl (parm);
    3334              :         }
    3335    715382162 :       write_template_arg (arg);
    3336              :     }
    3337    400866898 :   if (implicit_parm_scope)
    3338         5014 :     --G.parm_depth;
    3339              : 
    3340    400866898 :   write_tparms_constraints (constraints);
    3341              : 
    3342    400866898 :   write_char ('E');
    3343    400866898 : }
    3344              : 
    3345              : /* Write out the
    3346              :    <unqualified-name>
    3347              :    <unqualified-name> <template-args>
    3348              :    part of SCOPE_REF or COMPONENT_REF mangling.  */
    3349              : 
    3350              : static void
    3351       487372 : write_member_name (tree member)
    3352              : {
    3353       487372 :   if (identifier_p (member))
    3354              :     {
    3355       348784 :       if (IDENTIFIER_ANY_OP_P (member))
    3356              :         {
    3357        47641 :           if (abi_check (11))
    3358        47617 :             write_string ("on");
    3359              :         }
    3360       348784 :       write_unqualified_id (member);
    3361              :     }
    3362       138588 :   else if (DECL_P (member))
    3363              :     {
    3364         1203 :       if (ANON_AGGR_TYPE_P (TREE_TYPE (member)))
    3365              :         ;
    3366         1197 :       else if (DECL_OVERLOADED_OPERATOR_P (member))
    3367              :         {
    3368           15 :           if (abi_check (16))
    3369            9 :             write_string ("on");
    3370              :         }
    3371         1203 :       write_unqualified_name (member);
    3372              :     }
    3373       137385 :   else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
    3374              :     {
    3375          271 :       tree name = TREE_OPERAND (member, 0);
    3376          271 :       name = OVL_FIRST (name);
    3377          271 :       write_member_name (name);
    3378          271 :       write_template_args (TREE_OPERAND (member, 1));
    3379              :     }
    3380              :   else
    3381       137114 :     write_expression (member);
    3382       487372 : }
    3383              : 
    3384              : /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for
    3385              :    converting to BASE, or just the conversion of EXPR if BASE is null.
    3386              : 
    3387              :    "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base
    3388              :    path Min(P) is defined as follows: let C_i be the last element for which the
    3389              :    conversion to C_0 is unambiguous; if that element is C_n, the minimized path
    3390              :    is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) ->
    3391              :    C_0."
    3392              : 
    3393              :    We mangle the conversion to C_i if it's different from C_n.  */
    3394              : 
    3395              : static bool
    3396       191632 : write_base_ref (tree expr, tree base = NULL_TREE)
    3397              : {
    3398       191632 :   if (TREE_CODE (expr) != COMPONENT_REF)
    3399              :     return false;
    3400              : 
    3401       191626 :   tree field = TREE_OPERAND (expr, 1);
    3402              : 
    3403       191626 :   if (TREE_CODE (field) != FIELD_DECL || !DECL_FIELD_IS_BASE (field))
    3404              :     return false;
    3405              : 
    3406           18 :   tree object = TREE_OPERAND (expr, 0);
    3407              : 
    3408           18 :   tree binfo = NULL_TREE;
    3409           18 :   if (base)
    3410              :     {
    3411            9 :       tree cur = TREE_TYPE (object);
    3412            9 :       binfo = lookup_base (cur, base, ba_unique, NULL, tf_none);
    3413              :     }
    3414              :   else
    3415              :     /* We're at the end of the base conversion chain, so it can't be
    3416              :        ambiguous.  */
    3417            9 :     base = TREE_TYPE (field);
    3418              : 
    3419           18 :   if (binfo == error_mark_node)
    3420              :     {
    3421              :       /* cur->base is ambiguous, so make the conversion to
    3422              :          last explicit, expressed as a cast (last&)object.  */
    3423            3 :       tree last = TREE_TYPE (expr);
    3424            3 :       write_string (OVL_OP_INFO (false, CAST_EXPR)->mangled_name);
    3425            3 :       write_type (build_reference_type (last));
    3426            3 :       write_expression (object);
    3427              :     }
    3428           15 :   else if (write_base_ref (object, base))
    3429              :     /* cur->base is unambiguous, but we had another base conversion
    3430              :        underneath and wrote it out.  */;
    3431              :   else
    3432              :     /* No more base conversions, just write out the object.  */
    3433            6 :     write_expression (object);
    3434              : 
    3435              :   return true;
    3436              : }
    3437              : 
    3438              : /* The number of elements spanned by a RANGE_EXPR.  */
    3439              : 
    3440              : unsigned HOST_WIDE_INT
    3441           24 : range_expr_nelts (tree expr)
    3442              : {
    3443           24 :   tree lo = TREE_OPERAND (expr, 0);
    3444           24 :   tree hi = TREE_OPERAND (expr, 1);
    3445           24 :   return tree_to_uhwi (hi) - tree_to_uhwi (lo) + 1;
    3446              : }
    3447              : 
    3448              : /* <expression> ::= <unary operator-name> <expression>
    3449              :                 ::= <binary operator-name> <expression> <expression>
    3450              :                 ::= <expr-primary>
    3451              : 
    3452              :    <expr-primary> ::= <template-param>
    3453              :                   ::= L <type> <value number> E             # literal
    3454              :                   ::= L <mangled-name> E          # external name
    3455              :                   ::= st <type>                           # sizeof
    3456              :                   ::= sr <type> <unqualified-name>  # dependent name
    3457              :                   ::= sr <type> <unqualified-name> <template-args>
    3458              :                   ::= L Dm <value reflection> E           # C++26 reflection
    3459              :                                                         # value [proposed]
    3460              :                   ::= <splice>            # C++26 dependent splice [proposed]  */
    3461              : 
    3462              : static void
    3463     10987539 : write_expression (tree expr)
    3464              : {
    3465     11455909 :   enum tree_code code = TREE_CODE (expr);
    3466              : 
    3467     11455909 :   if (TREE_CODE (expr) == TARGET_EXPR)
    3468              :     {
    3469            0 :       expr = TARGET_EXPR_INITIAL (expr);
    3470            0 :       code = TREE_CODE (expr);
    3471              :     }
    3472              : 
    3473              :   /* Skip NOP_EXPR and CONVERT_EXPR.  They can occur when (say) a pointer
    3474              :      argument is converted (via qualification conversions) to another type.  */
    3475     11946069 :   while (CONVERT_EXPR_CODE_P (code)
    3476     11837909 :          || code == IMPLICIT_CONV_EXPR
    3477     11837780 :          || location_wrapper_p (expr)
    3478              :          /* Parentheses aren't mangled.  */
    3479     11456275 :          || code == PAREN_EXPR
    3480     11456275 :          || code == NON_LVALUE_EXPR
    3481     23402344 :          || (code == VIEW_CONVERT_EXPR
    3482          366 :              && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
    3483              :     {
    3484       490160 :       expr = TREE_OPERAND (expr, 0);
    3485       490160 :       code = TREE_CODE (expr);
    3486              :     }
    3487              : 
    3488     11455909 :   if (code == BASELINK
    3489     11455909 :       && (!type_unknown_p (expr)
    3490       718580 :           || !BASELINK_QUALIFIED_P (expr)))
    3491              :     {
    3492       749709 :       expr = BASELINK_FUNCTIONS (expr);
    3493       749709 :       code = TREE_CODE (expr);
    3494              :     }
    3495              : 
    3496              :   /* Handle pointers-to-members by making them look like expression
    3497              :      nodes.  */
    3498     11455909 :   if (code == PTRMEM_CST)
    3499              :     {
    3500         7404 :       expr = build_nt (ADDR_EXPR,
    3501              :                        build_qualified_name (/*type=*/NULL_TREE,
    3502         3702 :                                              PTRMEM_CST_CLASS (expr),
    3503         3702 :                                              PTRMEM_CST_MEMBER (expr),
    3504              :                                              /*template_p=*/false));
    3505         3702 :       code = TREE_CODE (expr);
    3506              :     }
    3507              : 
    3508              :   /* Handle template parameters.  */
    3509     11455909 :   if (code == TEMPLATE_TYPE_PARM
    3510     11455909 :       || code == TEMPLATE_TEMPLATE_PARM
    3511     11455909 :       || code == BOUND_TEMPLATE_TEMPLATE_PARM
    3512     11453083 :       || code == TEMPLATE_PARM_INDEX)
    3513       667700 :     write_template_param (expr);
    3514              :   /* Handle literals.  */
    3515     10788209 :   else if (TREE_CODE_CLASS (code) == tcc_constant
    3516     10473620 :            || code == CONST_DECL
    3517     10473620 :            || code == REFLECT_EXPR)
    3518       337330 :     write_template_arg_literal (expr);
    3519     10450879 :   else if (code == EXCESS_PRECISION_EXPR
    3520     10450879 :            && TREE_CODE (TREE_OPERAND (expr, 0)) == REAL_CST)
    3521            0 :     write_template_arg_literal (fold_convert (TREE_TYPE (expr),
    3522              :                                               TREE_OPERAND (expr, 0)));
    3523     10450879 :   else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
    3524              :     {
    3525       107202 :       gcc_assert (id_equal (DECL_NAME (expr), "this"));
    3526       107202 :       write_string ("fpT");
    3527              :     }
    3528     10343677 :   else if (code == PARM_DECL)
    3529              :     {
    3530              :       /* A function parameter used in a late-specified return type.  */
    3531       375881 :       int index = DECL_PARM_INDEX (expr);
    3532       375881 :       int level = DECL_PARM_LEVEL (expr);
    3533       375881 :       int delta = G.parm_depth - level + 1;
    3534       375881 :       gcc_assert (index >= 1);
    3535       375881 :       write_char ('f');
    3536       375881 :       if (delta != 0)
    3537              :         {
    3538       205398 :           gcc_checking_assert (delta > 0);
    3539       205398 :           if (abi_check (5))
    3540              :             {
    3541              :               /* Let L be the number of function prototype scopes from the
    3542              :                  innermost one (in which the parameter reference occurs) up
    3543              :                  to (and including) the one containing the declaration of
    3544              :                  the referenced parameter.  If the parameter declaration
    3545              :                  clause of the innermost function prototype scope has been
    3546              :                  completely seen, it is not counted (in that case -- which
    3547              :                  is perhaps the most common -- L can be zero).  */
    3548       205395 :               write_char ('L');
    3549       205395 :               write_unsigned_number (delta - 1);
    3550              :             }
    3551              :         }
    3552       375881 :       write_char ('p');
    3553       375881 :       write_compact_number (index - 1);
    3554              :     }
    3555      9967796 :   else if (DECL_P (expr))
    3556              :     {
    3557       349467 :       write_char ('L');
    3558       349467 :       write_mangled_name (expr, false);
    3559       349467 :       write_char ('E');
    3560              :     }
    3561      9618329 :   else if (TREE_CODE (expr) == SIZEOF_EXPR)
    3562              :     {
    3563         4806 :       tree op = TREE_OPERAND (expr, 0);
    3564              : 
    3565         4806 :       if (PACK_EXPANSION_P (op))
    3566              :         {
    3567         3587 :     sizeof_pack:
    3568         3590 :           if (abi_check (11))
    3569              :             {
    3570              :               /* sZ rather than szDp.  */
    3571         3581 :               write_string ("sZ");
    3572         3581 :               write_expression (PACK_EXPANSION_PATTERN (op));
    3573         3581 :               return;
    3574              :             }
    3575              :         }
    3576              : 
    3577         1228 :       if (SIZEOF_EXPR_TYPE_P (expr))
    3578              :         {
    3579            0 :           write_string ("st");
    3580            0 :           write_type (TREE_TYPE (op));
    3581              :         }
    3582         1228 :       else if (ARGUMENT_PACK_P (op))
    3583              :         {
    3584           15 :           tree args = ARGUMENT_PACK_ARGS (op);
    3585           15 :           int length = TREE_VEC_LENGTH (args);
    3586           15 :           if (abi_check (10))
    3587              :             {
    3588              :               /* Before v19 we wrongly mangled all single pack expansions with
    3589              :                  sZ, but now only for expressions, as types ICEd (95298).  */
    3590           12 :               if (length == 1)
    3591              :                 {
    3592            9 :                   tree arg = TREE_VEC_ELT (args, 0);
    3593            9 :                   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION
    3594            9 :                       && !abi_check (19))
    3595              :                     {
    3596            3 :                       op = arg;
    3597            3 :                       goto sizeof_pack;
    3598              :                     }
    3599              :                 }
    3600              : 
    3601              :               /* sP <template-arg>* E # sizeof...(T), size of a captured
    3602              :                  template parameter pack from an alias template */
    3603            9 :               write_string ("sP");
    3604           24 :               for (int i = 0; i < length; ++i)
    3605           15 :                 write_template_arg (TREE_VEC_ELT (args, i));
    3606            9 :               write_char ('E');
    3607              :             }
    3608              :           else
    3609              :             {
    3610              :               /* In GCC 5 we represented this sizeof wrong, with the effect
    3611              :                  that we mangled it as the last element of the pack.  */
    3612            3 :               tree arg = TREE_VEC_ELT (args, length-1);
    3613            3 :               if (TYPE_P (op))
    3614              :                 {
    3615            3 :                   write_string ("st");
    3616            3 :                   write_type (arg);
    3617              :                 }
    3618              :               else
    3619              :                 {
    3620            0 :                   write_string ("sz");
    3621            0 :                   write_expression (arg);
    3622              :                 }
    3623              :             }
    3624              :         }
    3625         1213 :       else if (TYPE_P (TREE_OPERAND (expr, 0)))
    3626              :         {
    3627         1094 :           write_string ("st");
    3628         1094 :           write_type (TREE_OPERAND (expr, 0));
    3629              :         }
    3630              :       else
    3631          119 :         goto normal_expr;
    3632              :     }
    3633      9613523 :   else if (code == PACK_INDEX_EXPR)
    3634              :     {
    3635              :       /* https://github.com/itanium-cxx-abi/cxx-abi/issues/175.  */
    3636           32 :       write_string ("sy");
    3637           32 :       if (TREE_CODE (PACK_INDEX_PACK (expr)) == TREE_VEC)
    3638              :         {
    3639            1 :           write_char ('J');
    3640           10 :           for (int i = 0; i < TREE_VEC_LENGTH (PACK_INDEX_PACK (expr));
    3641              :                ++i)
    3642            4 :             write_template_arg (TREE_VEC_ELT (PACK_INDEX_PACK (expr), i));
    3643            1 :           write_char ('E');
    3644              :         }
    3645              :       else
    3646              :         /* sy rather than sysp.  */
    3647           31 :         write_expression (PACK_EXPANSION_PATTERN (PACK_INDEX_PACK (expr)));
    3648           32 :       write_expression (PACK_INDEX_INDEX (expr));
    3649              :     }
    3650      9613491 :   else if (TREE_CODE (expr) == ALIGNOF_EXPR)
    3651              :     {
    3652           36 :       if (!ALIGNOF_EXPR_STD_P (expr))
    3653              :         {
    3654           24 :           if (abi_check (16))
    3655              :             {
    3656              :               /* We used to mangle __alignof__ like alignof.  */
    3657           18 :               write_string ("u11__alignof__");
    3658           18 :               write_template_arg (TREE_OPERAND (expr, 0));
    3659           18 :               write_char ('E');
    3660           18 :               return;
    3661              :             }
    3662              :         }
    3663           18 :       if (TYPE_P (TREE_OPERAND (expr, 0)))
    3664              :         {
    3665            9 :           write_string ("at");
    3666            9 :           write_type (TREE_OPERAND (expr, 0));
    3667              :         }
    3668              :       else
    3669            9 :         goto normal_expr;
    3670              :     }
    3671      9613455 :   else if (code == SCOPE_REF
    3672      9613455 :            || code == BASELINK)
    3673              :     {
    3674       299194 :       tree scope, member;
    3675       299194 :       if (code == SCOPE_REF)
    3676              :         {
    3677       299161 :           scope = TREE_OPERAND (expr, 0);
    3678       299161 :           member = TREE_OPERAND (expr, 1);
    3679       299161 :           if (BASELINK_P (member))
    3680           21 :             member = BASELINK_FUNCTIONS (member);
    3681              :         }
    3682              :       else
    3683              :         {
    3684           33 :           scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
    3685           33 :           member = BASELINK_FUNCTIONS (expr);
    3686              :         }
    3687              : 
    3688              :       /* If the MEMBER is a real declaration, then the qualifying
    3689              :          scope was not dependent.  Ideally, we would not have a
    3690              :          SCOPE_REF in those cases, but sometimes we do.  If the second
    3691              :          argument is a DECL, then the name must not have been
    3692              :          dependent.  */
    3693       299194 :       if (DECL_P (member))
    3694              :         write_expression (member);
    3695              :       else
    3696              :         {
    3697       295246 :           gcc_assert (code != BASELINK || BASELINK_QUALIFIED_P (expr));
    3698       295213 :           write_string ("sr");
    3699       295213 :           write_type (scope);
    3700       295213 :           write_member_name (member);
    3701              :         }
    3702              :     }
    3703      9314261 :   else if (INDIRECT_REF_P (expr)
    3704       419499 :            && TREE_TYPE (TREE_OPERAND (expr, 0))
    3705      9732073 :            && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
    3706              :     {
    3707       309602 :       write_expression (TREE_OPERAND (expr, 0));
    3708              :     }
    3709      9004659 :   else if (identifier_p (expr))
    3710              :     {
    3711              :       /* An operator name appearing as a dependent name needs to be
    3712              :          specially marked to disambiguate between a use of the operator
    3713              :          name and a use of the operator in an expression.  */
    3714       221478 :       if (IDENTIFIER_ANY_OP_P (expr))
    3715            6 :         write_string ("on");
    3716       221478 :       write_unqualified_id (expr);
    3717              :     }
    3718      8783181 :   else if (dependent_splice_p (expr))
    3719           25 :     write_splice (expr);
    3720      8783156 :   else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
    3721              :     {
    3722      3731911 :       tree fn = TREE_OPERAND (expr, 0);
    3723      4943068 :       if (!identifier_p (fn))
    3724      3731908 :         fn = OVL_NAME (fn);
    3725      3731911 :       if (IDENTIFIER_ANY_OP_P (fn))
    3726            3 :         write_string ("on");
    3727      3731911 :       write_unqualified_id (fn);
    3728      3731911 :       write_template_args (TREE_OPERAND (expr, 1));
    3729              :     }
    3730      5051245 :   else if (TREE_CODE (expr) == MODOP_EXPR)
    3731              :     {
    3732           45 :       enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
    3733           45 :       const char *name = OVL_OP_INFO (true, subop)->mangled_name;
    3734              : 
    3735           45 :       write_string (name);
    3736           45 :       write_expression (TREE_OPERAND (expr, 0));
    3737           45 :       write_expression (TREE_OPERAND (expr, 2));
    3738              :     }
    3739      5051200 :   else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
    3740              :     {
    3741              :       /* ::= [gs] nw <expression>* _ <type> E
    3742              :          ::= [gs] nw <expression>* _ <type> <initializer>
    3743              :          ::= [gs] na <expression>* _ <type> E
    3744              :          ::= [gs] na <expression>* _ <type> <initializer>
    3745              :          <initializer> ::= pi <expression>* E  */
    3746       149056 :       tree placement = TREE_OPERAND (expr, 0);
    3747       149056 :       tree type = TREE_OPERAND (expr, 1);
    3748       149056 :       tree nelts = TREE_OPERAND (expr, 2);
    3749       149056 :       tree init = TREE_OPERAND (expr, 3);
    3750       149056 :       tree t;
    3751              : 
    3752       149056 :       gcc_assert (code == NEW_EXPR);
    3753       149056 :       if (TREE_OPERAND (expr, 2))
    3754           12 :         code = VEC_NEW_EXPR;
    3755              : 
    3756       149056 :       if (NEW_EXPR_USE_GLOBAL (expr))
    3757       149031 :         write_string ("gs");
    3758              : 
    3759       149056 :       write_string (OVL_OP_INFO (false, code)->mangled_name);
    3760              : 
    3761       298087 :       for (t = placement; t; t = TREE_CHAIN (t))
    3762       149031 :         write_expression (TREE_VALUE (t));
    3763              : 
    3764       149056 :       write_char ('_');
    3765              : 
    3766       149056 :       if (nelts)
    3767              :         {
    3768           12 :           ++processing_template_decl;
    3769              :           /* Avoid compute_array_index_type complaints about
    3770              :              non-constant nelts.  */
    3771           12 :           tree max = cp_build_binary_op (input_location, MINUS_EXPR,
    3772              :                                          fold_convert (sizetype, nelts),
    3773              :                                          size_one_node,
    3774              :                                          tf_warning_or_error);
    3775           12 :           max = maybe_constant_value (max);
    3776           12 :           tree domain = build_index_type (max);
    3777           12 :           type = build_cplus_array_type (type, domain);
    3778           12 :           --processing_template_decl;
    3779              :         }
    3780       149056 :       write_type (type);
    3781              : 
    3782       149044 :       if (init && TREE_CODE (init) == TREE_LIST
    3783       298094 :           && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
    3784              :         write_expression (TREE_VALUE (init));
    3785              :       else
    3786              :         {
    3787       149053 :           if (init)
    3788       149041 :             write_string ("pi");
    3789       149041 :           if (init && init != void_node)
    3790       298070 :             for (t = init; t; t = TREE_CHAIN (t))
    3791       149035 :               write_expression (TREE_VALUE (t));
    3792       149053 :           write_char ('E');
    3793              :         }
    3794              :     }
    3795              :   else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
    3796              :     {
    3797           12 :       gcc_assert (code == DELETE_EXPR);
    3798           12 :       if (DELETE_EXPR_USE_VEC (expr))
    3799            6 :         code = VEC_DELETE_EXPR;
    3800              : 
    3801           12 :       if (DELETE_EXPR_USE_GLOBAL (expr))
    3802            6 :         write_string ("gs");
    3803              : 
    3804           12 :       write_string (OVL_OP_INFO (false, code)->mangled_name);
    3805              : 
    3806           12 :       write_expression (TREE_OPERAND (expr, 0));
    3807              :     }
    3808              :   else if (code == THROW_EXPR)
    3809              :     {
    3810            8 :       tree op = TREE_OPERAND (expr, 0);
    3811            8 :       if (op)
    3812              :         {
    3813            5 :           write_string ("tw");
    3814            5 :           write_expression (op);
    3815              :         }
    3816              :       else
    3817            3 :         write_string ("tr");
    3818              :     }
    3819              :   else if (code == NOEXCEPT_EXPR)
    3820              :     {
    3821            6 :       write_string ("nx");
    3822            6 :       write_expression (TREE_OPERAND (expr, 0));
    3823              :     }
    3824              :   else if (code == CONSTRUCTOR)
    3825              :     {
    3826       122575 :       bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr);
    3827       122575 :       tree etype = TREE_TYPE (expr);
    3828              : 
    3829       122575 :       if (braced_init)
    3830           87 :         write_string ("il");
    3831              :       else
    3832              :         {
    3833       122488 :           write_string ("tl");
    3834       122488 :           write_type (etype);
    3835              :         }
    3836              : 
    3837              :       /* If this is an undigested initializer, mangle it as written.
    3838              :          COMPOUND_LITERAL_P doesn't actually distinguish between digested and
    3839              :          undigested braced casts, but it should work to use it to distinguish
    3840              :          between braced casts in a template signature (undigested) and template
    3841              :          parm object values (digested), and all CONSTRUCTORS that get here
    3842              :          should be one of those two cases.  */
    3843       122575 :       bool undigested = braced_init || COMPOUND_LITERAL_P (expr);
    3844       122205 :       if (undigested || !zero_init_expr_p (expr))
    3845              :         {
    3846              :           /* Convert braced initializer lists to STRING_CSTs so that
    3847              :              A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
    3848              :              still using the latter mangling for strings that
    3849              :              originated as braced initializer lists.  */
    3850       105906 :           expr = braced_lists_to_strings (etype, expr);
    3851              : 
    3852       105906 :           if (TREE_CODE (expr) == CONSTRUCTOR)
    3853              :             {
    3854       105902 :               vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
    3855       105902 :               unsigned last_nonzero = UINT_MAX;
    3856       105902 :               constructor_elt *ce;
    3857              : 
    3858       105902 :               if (!undigested)
    3859       256277 :                 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
    3860       150745 :                   if ((TREE_CODE (etype) == UNION_TYPE
    3861           35 :                        && ce->index != first_field (etype))
    3862       150770 :                       || !zero_init_expr_p (ce->value))
    3863              :                     last_nonzero = i;
    3864              : 
    3865       105902 :               tree prev_field = NULL_TREE;
    3866       105902 :               if (undigested || last_nonzero != UINT_MAX)
    3867       254497 :                 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
    3868              :                   {
    3869       150708 :                     if (i > last_nonzero)
    3870              :                       break;
    3871       148442 :                     if (!undigested && !CONSTRUCTOR_NO_CLEARING (expr)
    3872       148595 :                         && (TREE_CODE (etype) == RECORD_TYPE
    3873       147506 :                             || TREE_CODE (etype) == ARRAY_TYPE))
    3874              :                       {
    3875              :                         /* Write out any implicit non-trailing zeros
    3876              :                            (which we neglected to do before v21).  */
    3877       147471 :                         if (TREE_CODE (etype) == RECORD_TYPE)
    3878              :                           {
    3879        83993 :                             tree field;
    3880        83993 :                             if (i == 0)
    3881        79048 :                               field = first_field (etype);
    3882              :                             else
    3883         4945 :                               field = DECL_CHAIN (prev_field);
    3884        84253 :                             for (;;)
    3885              :                               {
    3886        84123 :                                 field = next_subobject_field (field);
    3887        84123 :                                 if (field == ce->index)
    3888              :                                   break;
    3889          130 :                                 if (abi_check (21))
    3890          125 :                                   write_expression (build_zero_cst
    3891          125 :                                                     (TREE_TYPE (field)));
    3892          130 :                                 field = DECL_CHAIN (field);
    3893              :                               }
    3894              :                           }
    3895        63478 :                         else if (TREE_CODE (etype) == ARRAY_TYPE)
    3896              :                           {
    3897        63478 :                             unsigned HOST_WIDE_INT j;
    3898        63478 :                             if (i == 0)
    3899              :                               j = 0;
    3900              :                             else
    3901        37341 :                               j = 1 + tree_to_uhwi (prev_field);
    3902        63478 :                             unsigned HOST_WIDE_INT k;
    3903        63478 :                             if (TREE_CODE (ce->index) == RANGE_EXPR)
    3904           12 :                               k = tree_to_uhwi (TREE_OPERAND (ce->index, 0));
    3905              :                             else
    3906        63466 :                               k = tree_to_uhwi (ce->index);
    3907        63478 :                             tree zero = NULL_TREE;
    3908        65248 :                             for (; j < k; ++j)
    3909         1770 :                               if (abi_check (21))
    3910              :                                 {
    3911         1188 :                                   if (!zero)
    3912           30 :                                     zero = build_zero_cst (TREE_TYPE (etype));
    3913         1188 :                                   write_expression (zero);
    3914              :                                 }
    3915              :                           }
    3916              :                       }
    3917              : 
    3918       148595 :                     if (!undigested && TREE_CODE (etype) == UNION_TYPE)
    3919              :                       {
    3920              :                         /* Express the active member as a designator.  */
    3921           35 :                         write_string ("di");
    3922           35 :                         write_unqualified_name (ce->index);
    3923              :                       }
    3924       148595 :                     unsigned reps = 1;
    3925       148595 :                     if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
    3926           12 :                       reps = range_expr_nelts (ce->index);
    3927       148595 :                     if (TREE_CODE (ce->value) == RAW_DATA_CST)
    3928              :                       {
    3929           30 :                         gcc_assert (reps == 1);
    3930           30 :                         unsigned int len = RAW_DATA_LENGTH (ce->value);
    3931              :                         /* If this is the last non-zero element, skip
    3932              :                            zeros at the end.  */
    3933           30 :                         if (i == last_nonzero)
    3934          282 :                           while (len)
    3935              :                             {
    3936          282 :                               if (RAW_DATA_POINTER (ce->value)[len - 1])
    3937              :                                 break;
    3938              :                               --len;
    3939              :                             }
    3940           30 :                         tree valtype = TREE_TYPE (ce->value);
    3941         3906 :                         for (unsigned int i = 0; i < len; ++i)
    3942              :                           {
    3943         3876 :                             write_char ('L');
    3944         3876 :                             write_type (valtype);
    3945         3876 :                             unsigned HOST_WIDE_INT v;
    3946         3876 :                             if (!TYPE_UNSIGNED (valtype)
    3947          780 :                                 && TYPE_PRECISION (valtype) == BITS_PER_UNIT
    3948         4656 :                                 && RAW_DATA_SCHAR_ELT (ce->value, i) < 0)
    3949              :                               {
    3950            0 :                                 write_char ('n');
    3951            0 :                                 v = -RAW_DATA_SCHAR_ELT (ce->value, i);
    3952              :                               }
    3953              :                             else
    3954         3876 :                               v = RAW_DATA_UCHAR_ELT (ce->value, i);
    3955         3876 :                             write_unsigned_number (v);
    3956         3876 :                             write_char ('E');
    3957              :                           }
    3958              :                       }
    3959              :                     else
    3960       297148 :                       for (unsigned j = 0; j < reps; ++j)
    3961       148583 :                         write_expression (ce->value);
    3962       148595 :                     prev_field = ce->index;
    3963       148595 :                     if (prev_field && TREE_CODE (prev_field) == RANGE_EXPR)
    3964           12 :                       prev_field = TREE_OPERAND (prev_field, 1);
    3965              :                   }
    3966              :             }
    3967              :           else
    3968              :             {
    3969            4 :               gcc_assert (TREE_CODE (expr) == STRING_CST);
    3970            4 :               write_expression (expr);
    3971              :             }
    3972              :         }
    3973       122575 :       write_char ('E');
    3974              :     }
    3975              :   else if (code == LAMBDA_EXPR)
    3976              :     {
    3977              :       /* [temp.over.link] Two lambda-expressions are never considered
    3978              :          equivalent.
    3979              : 
    3980              :          So just use the closure type mangling.  */
    3981          655 :       write_char ('L');
    3982          655 :       write_type (LAMBDA_EXPR_CLOSURE (expr));
    3983          655 :       write_char ('E');
    3984              :     }
    3985              :   else if (code == REQUIRES_EXPR)
    3986       408730 :     write_requires_expr (expr);
    3987      4370158 :   else if (dependent_name (expr))
    3988              :     {
    3989       106170 :       tree name = dependent_name (expr);
    3990       106170 :       if (IDENTIFIER_ANY_OP_P (name))
    3991              :         {
    3992            9 :           if (abi_check (16))
    3993            6 :             write_string ("on");
    3994              :         }
    3995       106170 :       write_unqualified_id (name);
    3996              :     }
    3997              :   else
    3998              :     {
    3999      4263988 :     normal_expr:
    4000      4264116 :       int i, len;
    4001      4264116 :       const char *name;
    4002              : 
    4003              :       /* When we bind a variable or function to a non-type template
    4004              :          argument with reference type, we create an ADDR_EXPR to show
    4005              :          the fact that the entity's address has been taken.  But, we
    4006              :          don't actually want to output a mangling code for the `&'.  */
    4007      4264116 :       if (TREE_CODE (expr) == ADDR_EXPR
    4008         5621 :           && TREE_TYPE (expr)
    4009      4266035 :           && TYPE_REF_P (TREE_TYPE (expr)))
    4010              :         {
    4011            0 :           expr = TREE_OPERAND (expr, 0);
    4012            0 :           if (DECL_P (expr))
    4013              :             {
    4014              :               write_expression (expr);
    4015              :               return;
    4016              :             }
    4017              : 
    4018            0 :           code = TREE_CODE (expr);
    4019              :         }
    4020              : 
    4021      4264116 :       if (code == COMPONENT_REF)
    4022              :         {
    4023       191897 :           tree ob = TREE_OPERAND (expr, 0);
    4024              : 
    4025       191897 :           if (TREE_CODE (ob) == ARROW_EXPR)
    4026              :             {
    4027          280 :               write_string (OVL_OP_INFO (false, code)->mangled_name);
    4028          280 :               ob = TREE_OPERAND (ob, 0);
    4029          280 :               write_expression (ob);
    4030              :             }
    4031       191617 :           else if (write_base_ref (expr))
    4032              :             return;
    4033       191608 :           else if (!is_dummy_object (ob))
    4034              :             {
    4035       191602 :               write_string ("dt");
    4036       191602 :               write_expression (ob);
    4037              :             }
    4038              :           /* else, for a non-static data member with no associated object (in
    4039              :              unevaluated context), use the unresolved-name mangling.  */
    4040              : 
    4041       191888 :           write_member_name (TREE_OPERAND (expr, 1));
    4042       191888 :           return;
    4043              :         }
    4044              : 
    4045              :       /* If it wasn't any of those, recursively expand the expression.  */
    4046      4072219 :       name = OVL_OP_INFO (false, code)->mangled_name;
    4047              : 
    4048              :       /* We used to mangle const_cast and static_cast like a C cast.  */
    4049      4072219 :       if (code == CONST_CAST_EXPR
    4050      4072219 :           || code == STATIC_CAST_EXPR)
    4051              :         {
    4052          263 :           if (!abi_check (6))
    4053           15 :             name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name;
    4054              :         }
    4055              : 
    4056      4072219 :       if (name == NULL)
    4057              :         {
    4058            3 :           switch (code)
    4059              :             {
    4060            3 :             case TRAIT_EXPR:
    4061            3 :               error ("use of built-in trait %qE in function signature; "
    4062              :                      "use library traits instead", expr);
    4063            3 :               break;
    4064              : 
    4065            0 :             default:
    4066            0 :               sorry ("mangling %C", code);
    4067            0 :               break;
    4068              :             }
    4069            3 :           return;
    4070              :         }
    4071              :       else
    4072      4072216 :         write_string (name);
    4073              : 
    4074      4072216 :       switch (code)
    4075              :         {
    4076      1882006 :         case CALL_EXPR:
    4077      1882006 :           {
    4078      1882006 :             tree fn = CALL_EXPR_FN (expr);
    4079              : 
    4080      1882006 :             if (TREE_CODE (fn) == ADDR_EXPR)
    4081            0 :               fn = TREE_OPERAND (fn, 0);
    4082              : 
    4083              :             /* Mangle a dependent name as the name, not whatever happens to
    4084              :                be the first function in the overload set.  */
    4085      1881353 :             if (OVL_P (fn)
    4086      2103350 :                 && type_dependent_expression_p_push (expr))
    4087       221443 :               fn = OVL_NAME (fn);
    4088              : 
    4089      1882006 :             write_expression (fn);
    4090              :           }
    4091              : 
    4092      4359671 :           for (i = 0; i < call_expr_nargs (expr); ++i)
    4093       595659 :             write_expression (CALL_EXPR_ARG (expr, i));
    4094      1882006 :           write_char ('E');
    4095      1882006 :           break;
    4096              : 
    4097       150977 :         case CAST_EXPR:
    4098       150977 :           write_type (TREE_TYPE (expr));
    4099       150977 :           if (list_length (TREE_OPERAND (expr, 0)) == 1)
    4100       150834 :             write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
    4101              :           else
    4102              :             {
    4103          143 :               tree args = TREE_OPERAND (expr, 0);
    4104          143 :               write_char ('_');
    4105          155 :               for (; args; args = TREE_CHAIN (args))
    4106           12 :                 write_expression (TREE_VALUE (args));
    4107          143 :               write_char ('E');
    4108              :             }
    4109              :           break;
    4110              : 
    4111          269 :         case DYNAMIC_CAST_EXPR:
    4112          269 :         case REINTERPRET_CAST_EXPR:
    4113          269 :         case STATIC_CAST_EXPR:
    4114          269 :         case CONST_CAST_EXPR:
    4115          269 :           write_type (TREE_TYPE (expr));
    4116          269 :           write_expression (TREE_OPERAND (expr, 0));
    4117          269 :           break;
    4118              : 
    4119         2521 :         case PREINCREMENT_EXPR:
    4120         2521 :         case PREDECREMENT_EXPR:
    4121         2521 :           if (abi_check (6))
    4122         2512 :             write_char ('_');
    4123              :           /* Fall through.  */
    4124              : 
    4125      2038964 :         default:
    4126              :           /* In the middle-end, some expressions have more operands than
    4127              :              they do in templates (and mangling).  */
    4128      2038964 :           len = cp_tree_operand_length (expr);
    4129              : 
    4130      5365353 :           for (i = 0; i < len; ++i)
    4131              :             {
    4132      3326389 :               tree operand = TREE_OPERAND (expr, i);
    4133              :               /* As a GNU extension, the middle operand of a
    4134              :                  conditional may be omitted.  Since expression
    4135              :                  manglings are supposed to represent the input token
    4136              :                  stream, there's no good way to mangle such an
    4137              :                  expression without extending the C++ ABI.  */
    4138      3326389 :               if (code == COND_EXPR && i == 1 && !operand)
    4139              :                 {
    4140            3 :                   error ("omitted middle operand to %<?:%> operand "
    4141              :                          "cannot be mangled");
    4142            3 :                   continue;
    4143              :                 }
    4144      3326386 :               else if (FOLD_EXPR_P (expr))
    4145              :                 {
    4146              :                   /* The first 'operand' of a fold-expression is the operator
    4147              :                      that it folds over.  */
    4148       297143 :                   if (i == 0)
    4149              :                     {
    4150       148480 :                       int fcode = TREE_INT_CST_LOW (operand);
    4151       148480 :                       write_string (OVL_OP_INFO (false, fcode)->mangled_name);
    4152       148480 :                       continue;
    4153       148480 :                     }
    4154       148663 :                   else if (code == BINARY_LEFT_FOLD_EXPR)
    4155              :                     {
    4156              :                       /* The order of operands of the binary left and right
    4157              :                          folds is the same, but we want to mangle them in
    4158              :                          lexical order, i.e. non-pack first.  */
    4159          360 :                       if (i == 1)
    4160          180 :                         operand = FOLD_EXPR_INIT (expr);
    4161              :                       else
    4162          180 :                         operand = FOLD_EXPR_PACK (expr);
    4163              :                     }
    4164       148663 :                   if (PACK_EXPANSION_P (operand))
    4165       148480 :                     operand = PACK_EXPANSION_PATTERN (operand);
    4166              :                 }
    4167      3177906 :               write_expression (operand);
    4168              :             }
    4169              :         }
    4170              :     }
    4171              : }
    4172              : 
    4173              : /* Non-terminal <reflection>.
    4174              : 
    4175              :      <reflection> ::= nu                          # null reflection
    4176              :                   ::= vl <expression>                     # value
    4177              :                   ::= ob <expression>                     # object
    4178              :                   ::= vr <variable name>          # variable
    4179              :                   ::= sb <sb name>                        # structured binding
    4180              :                   ::= fn <function encoding>              # function
    4181              :                   ::= pa [ <nonnegative number> ] _ <encoding>      # fn param
    4182              :                   ::= en <prefix> <unqualified-name>        # enumerator
    4183              :                   ::= an [ <nonnegative number> ] _       # annotation
    4184              :                   ::= ta <alias prefix> <alias unqualified-name>
    4185              :                       [ <alias template-args> ] _ <type> # type alias
    4186              :                   ::= ty <type>                           # type
    4187              :                   ::= dm <prefix> <unqualified-name>        # ns data member
    4188              :                   ::= un <prefix> [ <nonnegative number> ] _ # unnamed bitfld
    4189              :                   ::= ct [ <prefix> ] <unqualified-name> # class template
    4190              :                   ::= ft [ <prefix> ] <unqualified-name> # function template
    4191              :                   ::= vt [ <prefix> ] <unqualified-name> # variable template
    4192              :                   ::= at [ <prefix> ] <unqualified-name> # alias template
    4193              :                   ::= co [ <prefix> ] <unqualified-name> # concept
    4194              :                   ::= na [ <prefix> ] <unqualified-name> # namespace alias
    4195              :                   ::= ns [ <prefix> ] <unqualified-name> # namespace
    4196              :                   ::= gs                                 # ^^::
    4197              :                   ::= tt <template-template-param>         # templ templ param
    4198              :                   ::= de <expression>                      # dependent expr
    4199              :                   ::= ba [ <nonnegative number> ] _ <type> # dir. base cls rel
    4200              :                   ::= ds <type> _ [ <unqualified-name> ] _
    4201              :                       [ <alignment number> ] _ [ <bit-width number> ] _
    4202              :                       [ n ] [ <template-arg>* ]           # data member spec  */
    4203              : 
    4204              : static void
    4205          670 : write_reflection (tree refl)
    4206              : {
    4207          670 :   char prefix[3];
    4208          670 :   tree arg = reflection_mangle_prefix (refl, prefix);
    4209          670 :   write_string (prefix);
    4210              :   /* If there is no argument, nothing further needs to be mangled.  */
    4211          670 :   if (arg == NULL_TREE)
    4212           16 :     return;
    4213          654 :   if (strcmp (prefix, "vl") == 0 || strcmp (prefix, "ob") == 0)
    4214           11 :     write_expression (arg);
    4215          643 :   else if (strcmp (prefix, "vr") == 0 || strcmp (prefix, "sb") == 0)
    4216          115 :     write_name (arg, 0);
    4217          528 :   else if (strcmp (prefix, "fn") == 0)
    4218           52 :     write_encoding (arg);
    4219          476 :   else if (strcmp (prefix, "pa") == 0)
    4220              :     {
    4221           16 :       tree fn = DECL_CONTEXT (arg);
    4222           16 :       tree args = FUNCTION_FIRST_USER_PARM (fn);
    4223           16 :       int idx = 0;
    4224           36 :       while (arg != args)
    4225              :         {
    4226            4 :           args = DECL_CHAIN (args);
    4227            4 :           ++idx;
    4228              :         }
    4229           16 :       write_compact_number (idx);
    4230           16 :       write_encoding (fn);
    4231              :     }
    4232          460 :   else if (strcmp (prefix, "en") == 0)
    4233              :     {
    4234           13 :       write_prefix (decl_mangling_context (arg));
    4235           13 :       write_unqualified_name (arg);
    4236              :     }
    4237          447 :   else if (strcmp (prefix, "an") == 0)
    4238            5 :     write_compact_number (tree_to_uhwi (arg));
    4239          442 :   else if (strcmp (prefix, "ta") == 0)
    4240              :     {
    4241           12 :       arg = TYPE_NAME (arg);
    4242              :       /* Can't use write_prefix (arg) here instead of
    4243              :          write_prefix + write_unqualified_name + optional
    4244              :          write_template_args, it shouldn't be
    4245              :          remembered among substitutions.  */
    4246           12 :       write_prefix (decl_mangling_context (arg));
    4247           12 :       write_unqualified_name (arg);
    4248           12 :       tree template_info = maybe_template_info (arg);
    4249           12 :       if (template_info)
    4250            2 :         write_template_args (TI_ARGS (template_info));
    4251           12 :       write_char ('_');
    4252           12 :       write_type (DECL_ORIGINAL_TYPE (arg));
    4253              :     }
    4254          430 :   else if (strcmp (prefix, "ty") == 0)
    4255          146 :     write_type (arg);
    4256          284 :   else if (strcmp (prefix, "dm") == 0)
    4257              :     {
    4258           99 :       tree ctx = decl_mangling_context (arg);
    4259          202 :       while (ctx && ANON_UNION_TYPE_P (ctx))
    4260            4 :         ctx = decl_mangling_context (TYPE_NAME (ctx));
    4261           99 :       write_prefix (ctx);
    4262           99 :       write_unqualified_name (arg);
    4263              :     }
    4264          185 :   else if (strcmp (prefix, "un") == 0)
    4265              :     {
    4266            4 :       tree ctx = DECL_CONTEXT (arg);
    4267            4 :       int idx = 0;
    4268           71 :       for (tree f = TYPE_FIELDS (ctx); f; f = DECL_CHAIN (f))
    4269           71 :         if (f == arg)
    4270              :           break;
    4271           67 :         else if (TREE_CODE (f) == FIELD_DECL && DECL_UNNAMED_BIT_FIELD (f))
    4272            3 :           ++idx;
    4273            4 :       write_prefix (decl_mangling_context (arg));
    4274            4 :       write_compact_number (idx);
    4275              :     }
    4276          181 :   else if (strcmp (prefix, "ct") == 0
    4277          165 :            || strcmp (prefix, "ft") == 0
    4278          151 :            || strcmp (prefix, "vt") == 0
    4279          142 :            || strcmp (prefix, "at") == 0
    4280          134 :            || strcmp (prefix, "co") == 0
    4281          127 :            || strcmp (prefix, "na") == 0
    4282          122 :            || strcmp (prefix, "ns") == 0)
    4283              :     {
    4284          125 :       write_prefix (decl_mangling_context (arg));
    4285          125 :       write_unqualified_name (arg);
    4286              :     }
    4287           56 :   else if (strcmp (prefix, "ba") == 0)
    4288              :     {
    4289           11 :       gcc_assert (TREE_CODE (arg) == TREE_BINFO);
    4290              :       tree c = arg, base_binfo;
    4291           22 :       while (BINFO_INHERITANCE_CHAIN (c))
    4292              :         c = BINFO_INHERITANCE_CHAIN (c);
    4293              : 
    4294              :       unsigned idx;
    4295           12 :       for (idx = 0; BINFO_BASE_ITERATE (c, idx, base_binfo); idx++)
    4296           12 :         if (base_binfo == arg)
    4297              :           break;
    4298           11 :       write_compact_number (idx);
    4299           11 :       write_type (BINFO_TYPE (c));
    4300              :     }
    4301           45 :   else if (strcmp (prefix, "ds") == 0)
    4302              :     {
    4303           42 :       gcc_assert (TREE_CODE (arg) == TREE_VEC);
    4304           42 :       write_type (TREE_VEC_ELT (arg, 0));
    4305           42 :       write_char ('_');
    4306           42 :       if (TREE_VEC_ELT (arg, 1))
    4307           40 :         write_unqualified_id (TREE_VEC_ELT (arg, 1));
    4308           42 :       write_char ('_');
    4309           42 :       if (TREE_VEC_ELT (arg, 2))
    4310            1 :         write_number (tree_to_shwi (TREE_VEC_ELT (arg, 2)), 0, 10);
    4311           42 :       write_char ('_');
    4312           42 :       if (TREE_VEC_ELT (arg, 3))
    4313            3 :         write_number (tree_to_shwi (TREE_VEC_ELT (arg, 3)), 0, 10);
    4314           42 :       write_char ('_');
    4315           42 :       if (integer_nonzerop (TREE_VEC_ELT (arg, 4)))
    4316            1 :         write_char ('n');
    4317           45 :       for (int i = 5; i < TREE_VEC_LENGTH (arg); ++i)
    4318            3 :         write_template_arg (REFLECT_EXPR_HANDLE (TREE_VEC_ELT (arg, i)));
    4319              :     }
    4320            3 :   else if (strcmp (prefix, "tt") == 0)
    4321              :     {
    4322            1 :       gcc_assert (DECL_TEMPLATE_TEMPLATE_PARM_P (arg));
    4323            1 :       write_template_template_param (TREE_TYPE (arg));
    4324              :     }
    4325            2 :   else if (strcmp (prefix, "de") == 0)
    4326            2 :     write_expression (arg);
    4327              :   else
    4328            0 :     gcc_unreachable ();
    4329              : }
    4330              : 
    4331              : /* Mangle a dependent splice.
    4332              : 
    4333              :      <splice> ::= DS <expression> [ <template-args> ] E
    4334              : 
    4335              :    TODO: This is only a proposed mangling.
    4336              :    See <https://github.com/itanium-cxx-abi/cxx-abi/issues/208>.  */
    4337              : 
    4338              : static void
    4339           54 : write_splice (tree sp)
    4340              : {
    4341           54 :   write_string ("DS");
    4342              : 
    4343           54 :   if (TREE_CODE (sp) == SPLICE_SCOPE)
    4344           29 :     sp = SPLICE_SCOPE_EXPR (sp);
    4345           54 :   gcc_assert (dependent_splice_p (sp));
    4346           54 :   if (TREE_CODE (sp) == TEMPLATE_ID_EXPR)
    4347              :     {
    4348            2 :       write_expression (TREE_OPERAND (TREE_OPERAND (sp, 0), 0));
    4349            2 :       write_template_args (TREE_OPERAND (sp, 1));
    4350              :     }
    4351              :   else
    4352           52 :     write_expression (TREE_OPERAND (sp, 0));
    4353              : 
    4354           54 :   write_char ('E');
    4355           54 : }
    4356              : 
    4357              : /* Literal subcase of non-terminal <template-arg>.
    4358              : 
    4359              :      "Literal arguments, e.g. "A<42L>", are encoded with their type
    4360              :      and value. Negative integer values are preceded with "n"; for
    4361              :      example, "A<-42L>" becomes "1AILln42EE". The bool value false is
    4362              :      encoded as 0, true as 1."  */
    4363              : 
    4364              : static void
    4365     89590734 : write_template_arg_literal (const tree value)
    4366              : {
    4367     89590734 :   if (TREE_CODE (value) == STRING_CST)
    4368              :     /* Temporarily mangle strings as braced initializer lists.  */
    4369         1431 :     write_string ("tl");
    4370              :   else
    4371     89589303 :     write_char ('L');
    4372              : 
    4373     89590734 :   tree valtype = TREE_TYPE (value);
    4374     89590734 :   write_type (valtype);
    4375              : 
    4376              :   /* Write a null member pointer value as (type)0, regardless of its
    4377              :      real representation.  */
    4378     89590734 :   if (null_member_pointer_value_p (value))
    4379          162 :     write_integer_cst (integer_zero_node);
    4380              :   else
    4381     89590572 :     switch (TREE_CODE (value))
    4382              :       {
    4383        22669 :       case CONST_DECL:
    4384        22669 :         write_integer_cst (DECL_INITIAL (value));
    4385        22669 :         break;
    4386              : 
    4387     89564733 :       case INTEGER_CST:
    4388     89564733 :         gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
    4389              :                     || integer_zerop (value) || integer_onep (value));
    4390     89564733 :         if (!(abi_version_at_least (14)
    4391     89557863 :               && NULLPTR_TYPE_P (TREE_TYPE (value))))
    4392     89564678 :           write_integer_cst (value);
    4393              :         break;
    4394              : 
    4395         1063 :       case REAL_CST:
    4396         1063 :         write_real_cst (value);
    4397         1063 :         break;
    4398              : 
    4399            6 :       case COMPLEX_CST:
    4400            6 :         if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
    4401            6 :             && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
    4402              :           {
    4403            3 :             write_integer_cst (TREE_REALPART (value));
    4404            3 :             write_char ('_');
    4405            3 :             write_integer_cst (TREE_IMAGPART (value));
    4406              :           }
    4407            3 :         else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
    4408            3 :                  && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
    4409              :           {
    4410            3 :             write_real_cst (TREE_REALPART (value));
    4411            3 :             write_char ('_');
    4412            3 :             write_real_cst (TREE_IMAGPART (value));
    4413              :           }
    4414              :         else
    4415            0 :           gcc_unreachable ();
    4416              :         break;
    4417              : 
    4418         1431 :       case STRING_CST:
    4419         1431 :         {
    4420              :           /* Mangle strings the same as braced initializer lists.  */
    4421         1431 :           unsigned n = TREE_STRING_LENGTH (value);
    4422         1431 :           const char *str = TREE_STRING_POINTER (value);
    4423              : 
    4424              :           /* Count the number of trailing nuls and subtract them from
    4425              :              STRSIZE because they don't need to be mangled.  */
    4426         3839 :           for (const char *p = str + n - 1; ; --p)
    4427              :             {
    4428         3839 :               if (*p || p == str)
    4429              :                 {
    4430         1431 :                   n -= str + n - !!*p - p;
    4431         1431 :                   break;
    4432              :                 }
    4433              :             }
    4434         1431 :           tree eltype = TREE_TYPE (valtype);
    4435         7687 :           for (const char *p = str; n--; ++p)
    4436              :             {
    4437         6256 :               write_char ('L');
    4438         6256 :               write_type (eltype);
    4439         6256 :               write_unsigned_number (*(const unsigned char*)p);
    4440         6256 :               write_string ("E");
    4441              :             }
    4442              :           break;
    4443              :         }
    4444              : 
    4445          670 :       case REFLECT_EXPR:
    4446          670 :         write_reflection (value);
    4447          670 :         break;
    4448              : 
    4449            0 :       default:
    4450            0 :         gcc_unreachable ();
    4451              :       }
    4452              : 
    4453     89590734 :   write_char ('E');
    4454     89590734 : }
    4455              : 
    4456              : /* Non-terminal <template-arg>.
    4457              : 
    4458              :      <template-arg> ::= <type>                              # type
    4459              :                     ::= L <type> </value/ number> E # literal
    4460              :                     ::= LZ <name> E                       # external name
    4461              :                     ::= X <expression> E          # expression  */
    4462              : 
    4463              : static void
    4464    728811030 : write_template_arg (tree node)
    4465              : {
    4466    728811030 :   enum tree_code code = TREE_CODE (node);
    4467              : 
    4468    728811030 :   MANGLE_TRACE_TREE ("template-arg", node);
    4469              : 
    4470              :   /* A template template parameter's argument list contains TREE_LIST
    4471              :      nodes of which the value field is the actual argument.  */
    4472    728811030 :   if (code == TREE_LIST)
    4473              :     {
    4474            0 :       node = TREE_VALUE (node);
    4475              :       /* If it's a decl, deal with its type instead.  */
    4476            0 :       if (DECL_P (node))
    4477              :         {
    4478            0 :           node = TREE_TYPE (node);
    4479            0 :           code = TREE_CODE (node);
    4480              :         }
    4481              :     }
    4482              : 
    4483    728811030 :   if (VAR_P (node) && DECL_NTTP_OBJECT_P (node))
    4484              :     /* We want to mangle the argument, not the var we stored it in.  */
    4485        58745 :     node = tparm_object_argument (node);
    4486              : 
    4487              :   /* Strip a conversion added by convert_nontype_argument.  */
    4488    728811030 :   if (TREE_CODE (node) == IMPLICIT_CONV_EXPR)
    4489           54 :     node = TREE_OPERAND (node, 0);
    4490    728811030 :   if (REFERENCE_REF_P (node))
    4491          266 :     node = TREE_OPERAND (node, 0);
    4492    728811030 :   if (TREE_CODE (node) == NOP_EXPR
    4493    728811030 :       && TYPE_REF_P (TREE_TYPE (node)))
    4494              :     {
    4495              :       /* Template parameters can be of reference type. To maintain
    4496              :          internal consistency, such arguments use a conversion from
    4497              :          address of object to reference type.  */
    4498          266 :       gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
    4499          266 :       node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
    4500              :     }
    4501              : 
    4502    728811030 :   if (TREE_CODE (node) == BASELINK
    4503    728811030 :       && !type_unknown_p (node))
    4504              :     {
    4505              :       /* Before v6 we wrongly wrapped a class-scope function in X/E.  */
    4506           18 :       if (abi_check (6))
    4507           12 :         node = BASELINK_FUNCTIONS (node);
    4508              :     }
    4509              : 
    4510    728811030 :   if (ARGUMENT_PACK_P (node))
    4511              :     {
    4512              :       /* Expand the template argument pack. */
    4513      9346241 :       tree args = ARGUMENT_PACK_ARGS (node);
    4514      9346241 :       int i, length = TREE_VEC_LENGTH (args);
    4515      9346241 :       if (abi_check (6))
    4516      9346223 :         write_char ('J');
    4517              :       else
    4518           18 :         write_char ('I');
    4519     22775059 :       for (i = 0; i < length; ++i)
    4520     13428818 :         write_template_arg (TREE_VEC_ELT (args, i));
    4521      9346241 :       write_char ('E');
    4522              :     }
    4523    719464789 :   else if (TYPE_P (node))
    4524    628739011 :     write_type (node);
    4525     90725778 :   else if (code == TEMPLATE_DECL)
    4526              :     /* A template appearing as a template arg is a template template arg.  */
    4527       764324 :     write_template_template_arg (node);
    4528     89255946 :   else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
    4529       705508 :            || code == CONST_DECL
    4530       708728 :            || null_member_pointer_value_p (node)
    4531     90670102 :            || code == REFLECT_EXPR)
    4532     89253404 :     write_template_arg_literal (node);
    4533       708050 :   else if (code == EXCESS_PRECISION_EXPR
    4534       708050 :            && TREE_CODE (TREE_OPERAND (node, 0)) == REAL_CST)
    4535            0 :     write_template_arg_literal (fold_convert (TREE_TYPE (node),
    4536              :                                               TREE_OPERAND (node, 0)));
    4537       708050 :   else if (DECL_P (node))
    4538              :     {
    4539          286 :       write_char ('L');
    4540              :       /* Until ABI version 3, the underscore before the mangled name
    4541              :          was incorrectly omitted.  */
    4542          286 :       if (!abi_check (3))
    4543           21 :         write_char ('Z');
    4544              :       else
    4545          265 :         write_string ("_Z");
    4546          286 :       write_encoding (node);
    4547          286 :       write_char ('E');
    4548              :     }
    4549              :   else
    4550              :     {
    4551              :       /* Template arguments may be expressions.  */
    4552       707764 :       write_char ('X');
    4553       707764 :       write_expression (node);
    4554       707764 :       write_char ('E');
    4555              :     }
    4556    728811030 : }
    4557              : 
    4558              : /*  <template-template-arg>
    4559              :                         ::= <name>
    4560              :                         ::= <substitution>  */
    4561              : 
    4562              : static void
    4563       764324 : write_template_template_arg (const tree decl)
    4564              : {
    4565       764324 :   MANGLE_TRACE_TREE ("template-template-arg", decl);
    4566              : 
    4567       764324 :   if (find_substitution (decl))
    4568              :     return;
    4569       684243 :   write_name (decl, /*ignore_local_scope=*/0);
    4570       684243 :   add_substitution (decl);
    4571              : }
    4572              : 
    4573              : 
    4574              : /* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.
    4575              : 
    4576              :      <array-type> ::= A [</dimension/ number>] _ </element/ type>
    4577              :                   ::= A <expression> _ </element/ type>
    4578              : 
    4579              :      "Array types encode the dimension (number of elements) and the
    4580              :      element type.  For variable length arrays, the dimension (but not
    4581              :      the '_' separator) is omitted."
    4582              :      Note that for flexible array members, like for other arrays of
    4583              :      unspecified size, the dimension is also omitted.  */
    4584              : 
    4585              : static void
    4586      1471652 : write_array_type (const tree type)
    4587              : {
    4588      1471652 :   write_char ('A');
    4589      1471652 :   if (TYPE_DOMAIN (type))
    4590              :     {
    4591       348762 :       tree index_type;
    4592              : 
    4593       348762 :       index_type = TYPE_DOMAIN (type);
    4594              :       /* The INDEX_TYPE gives the upper and lower bounds of the array.
    4595              :          It's null for flexible array members which have no upper bound
    4596              :          (this is a change from GCC 5 and prior where such members were
    4597              :          incorrectly mangled as zero-length arrays).  */
    4598       348762 :       if (tree max = TYPE_MAX_VALUE (index_type))
    4599              :         {
    4600       348762 :           if (TREE_CODE (max) == INTEGER_CST)
    4601              :             {
    4602              :               /* The ABI specifies that we should mangle the number of
    4603              :                  elements in the array, not the largest allowed index.  */
    4604       197632 :               offset_int wmax = wi::to_offset (max) + 1;
    4605              :               /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
    4606              :                  number of elements as zero.  */
    4607       197632 :               wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
    4608       197632 :               gcc_assert (wi::fits_uhwi_p (wmax));
    4609       197632 :               write_unsigned_number (wmax.to_uhwi ());
    4610              :             }
    4611              :           else
    4612              :             {
    4613       151130 :               gcc_checking_assert (TREE_CODE (max) == MINUS_EXPR
    4614              :                                    && integer_onep (TREE_OPERAND (max, 1)));
    4615       151130 :               max = TREE_OPERAND (max, 0);
    4616       151130 :               write_expression (max);
    4617              :             }
    4618              :         }
    4619              :     }
    4620      1471652 :   write_char ('_');
    4621      1471652 :   write_type (TREE_TYPE (type));
    4622      1471652 : }
    4623              : 
    4624              : /* Non-terminal <pointer-to-member-type> for pointer-to-member
    4625              :    variables.  TYPE is a pointer-to-member POINTER_TYPE.
    4626              : 
    4627              :      <pointer-to-member-type> ::= M </class/ type> </member/ type>  */
    4628              : 
    4629              : static void
    4630       270359 : write_pointer_to_member_type (const tree type)
    4631              : {
    4632       270359 :   write_char ('M');
    4633       270359 :   write_type (TYPE_PTRMEM_CLASS_TYPE (type));
    4634       270359 :   write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
    4635       270359 : }
    4636              : 
    4637              : /* Non-terminal <template-param>.  PARM is a TEMPLATE_TYPE_PARM,
    4638              :    TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
    4639              :    TEMPLATE_PARM_INDEX.
    4640              : 
    4641              :      <template-param> ::= T </parameter/ number> _  */
    4642              : 
    4643              : static void
    4644     26900546 : write_template_param (const tree parm)
    4645              : {
    4646     26900546 :   int parm_index;
    4647     26900546 :   int level;
    4648              : 
    4649     26900546 :   MANGLE_TRACE_TREE ("template-parm", parm);
    4650              : 
    4651     26900546 :   switch (TREE_CODE (parm))
    4652              :     {
    4653     26235672 :     case TEMPLATE_TYPE_PARM:
    4654     26235672 :     case TEMPLATE_TEMPLATE_PARM:
    4655     26235672 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    4656     26235672 :       parm_index = TEMPLATE_TYPE_IDX (parm);
    4657     26235672 :       level = TEMPLATE_TYPE_LEVEL (parm);
    4658     26235672 :       break;
    4659              : 
    4660       664874 :     case TEMPLATE_PARM_INDEX:
    4661       664874 :       parm_index = TEMPLATE_PARM_IDX (parm);
    4662       664874 :       level = TEMPLATE_PARM_LEVEL (parm);
    4663       664874 :       break;
    4664              : 
    4665            0 :     default:
    4666            0 :       gcc_unreachable ();
    4667              :     }
    4668              : 
    4669     26900546 :   write_char ('T');
    4670     26900546 :   if (level > 1)
    4671              :     {
    4672        23837 :       if (abi_check (19))
    4673              :         {
    4674        23825 :           write_char ('L');
    4675        23825 :           write_compact_number (level - 1);
    4676              :         }
    4677              :     }
    4678              :   /* NUMBER as it appears in the mangling is (-1)-indexed, with the
    4679              :      earliest template param denoted by `_'.  */
    4680     26900546 :   write_compact_number (parm_index);
    4681     26900546 : }
    4682              : 
    4683              : /*  <template-template-param>
    4684              :                         ::= <template-param>
    4685              :                         ::= <substitution>  */
    4686              : 
    4687              : static void
    4688          626 : write_template_template_param (const tree parm)
    4689              : {
    4690          626 :   tree templ = NULL_TREE;
    4691              : 
    4692              :   /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
    4693              :      template template parameter.  The substitution candidate here is
    4694              :      only the template.  */
    4695          626 :   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
    4696              :     {
    4697          532 :       templ
    4698          532 :         = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
    4699          532 :       if (find_substitution (templ))
    4700              :         return;
    4701              :     }
    4702              : 
    4703              :   /* <template-param> encodes only the template parameter position,
    4704              :      not its template arguments, which is fine here.  */
    4705          626 :   write_template_param (parm);
    4706          626 :   if (templ)
    4707          532 :     add_substitution (templ);
    4708              : }
    4709              : 
    4710              : /* Non-terminal <substitution>.
    4711              : 
    4712              :       <substitution> ::= S <seq-id> _
    4713              :                      ::= S_  */
    4714              : 
    4715              : static void
    4716    173079038 : write_substitution (const int seq_id)
    4717              : {
    4718    173079038 :   MANGLE_TRACE ("substitution", "");
    4719              : 
    4720    173079038 :   write_char ('S');
    4721    173079038 :   if (seq_id > 0)
    4722    151110959 :     write_number (seq_id - 1, /*unsigned=*/1, 36);
    4723    173079038 :   write_char ('_');
    4724    173079038 : }
    4725              : 
    4726              : /* Start mangling ENTITY.  */
    4727              : 
    4728              : static inline void
    4729    219472496 : start_mangling (const tree entity)
    4730              : {
    4731    219472496 :   G = {};
    4732    219472496 :   G.entity = entity;
    4733    219472496 :   obstack_free (&name_obstack, name_base);
    4734    219472496 :   mangle_obstack = &name_obstack;
    4735    219472496 :   name_base = obstack_alloc (&name_obstack, 0);
    4736    219472496 : }
    4737              : 
    4738              : /* Done with mangling.  Release the data.  */
    4739              : 
    4740              : static void
    4741    219472496 : finish_mangling_internal (void)
    4742              : {
    4743              :   /* Clear all the substitutions.  */
    4744    219472496 :   vec_safe_truncate (G.substitutions, 0);
    4745              : 
    4746    219472496 :   if (G.mod)
    4747         7585 :     mangle_module_fini ();
    4748              : 
    4749              :   /* Null-terminate the string.  */
    4750    219472496 :   write_char ('\0');
    4751    219472496 : }
    4752              : 
    4753              : 
    4754              : /* Like finish_mangling_internal, but return the mangled string.  */
    4755              : 
    4756              : static inline const char *
    4757       434711 : finish_mangling (void)
    4758              : {
    4759       434711 :   finish_mangling_internal ();
    4760       434711 :   return (const char *) obstack_finish (mangle_obstack);
    4761              : }
    4762              : 
    4763              : /* Like finish_mangling_internal, but return an identifier.  */
    4764              : 
    4765              : static tree
    4766    219037785 : finish_mangling_get_identifier (void)
    4767              : {
    4768    219037785 :   finish_mangling_internal ();
    4769              :   /* Don't obstack_finish here, and the next start_mangling will
    4770              :      remove the identifier.  */
    4771    219037785 :   return get_identifier ((const char *) obstack_base (mangle_obstack));
    4772              : }
    4773              : 
    4774              : /* Initialize data structures for mangling.  */
    4775              : 
    4776              : void
    4777        98333 : init_mangle (void)
    4778              : {
    4779        98333 :   gcc_obstack_init (&name_obstack);
    4780        98333 :   name_base = obstack_alloc (&name_obstack, 0);
    4781        98333 :   vec_alloc (G.substitutions, 0);
    4782              : 
    4783              :   /* Cache these identifiers for quick comparison when checking for
    4784              :      standard substitutions.  */
    4785        98333 :   subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
    4786        98333 :   subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
    4787        98333 :   subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
    4788        98333 :   subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
    4789        98333 :   subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
    4790        98333 :   subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
    4791        98333 : }
    4792              : 
    4793              : /* Generate a mangling for MODULE's global initializer fn.  */
    4794              : 
    4795              : tree
    4796         2008 : mangle_module_global_init (int module)
    4797              : {
    4798         2008 :   start_mangling (NULL_TREE);
    4799              : 
    4800         2008 :   write_string ("_ZGI");
    4801         2008 :   write_module (module, true);
    4802              : 
    4803         2008 :   return finish_mangling_get_identifier ();
    4804              : }
    4805              : 
    4806              : /* Generate the mangled name of DECL.  */
    4807              : 
    4808              : static tree
    4809    212698632 : mangle_decl_string (const tree decl)
    4810              : {
    4811    212698632 :   tree result;
    4812    212698632 :   tree saved_fn = current_function_decl;
    4813              : 
    4814              :   /* We shouldn't be trying to mangle an uninstantiated template.  */
    4815    212698632 :   gcc_assert (!type_dependent_expression_p (decl));
    4816              : 
    4817    212698632 :   current_function_decl = NULL_TREE;
    4818    212698632 :   iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
    4819              : 
    4820    212698632 :   start_mangling (decl);
    4821              : 
    4822    212698632 :   if (TREE_CODE (decl) == TYPE_DECL)
    4823       328830 :     write_type (TREE_TYPE (decl));
    4824              :   else
    4825    212369802 :     write_mangled_name (decl, true);
    4826              : 
    4827    212698632 :   result = finish_mangling_get_identifier ();
    4828    212698632 :   if (DEBUG_MANGLE)
    4829              :     fprintf (stderr, "mangle_decl_string = '%s'\n\n",
    4830              :              IDENTIFIER_POINTER (result));
    4831              : 
    4832    212698632 :   current_function_decl = saved_fn;
    4833    212698632 :   return result;
    4834    212698632 : }
    4835              : 
    4836              : /* Return an identifier for the external mangled name of DECL.  */
    4837              : 
    4838              : static tree
    4839    158748395 : get_mangled_id (tree decl)
    4840              : {
    4841    158748395 :   tree id = mangle_decl_string (decl);
    4842    158748395 :   return targetm.mangle_decl_assembler_name (decl, id);
    4843              : }
    4844              : 
    4845              : /* Create an identifier for the external mangled name of DECL.  */
    4846              : 
    4847              : void
    4848    158756790 : mangle_decl (const tree decl)
    4849              : {
    4850    158756790 :   tree id;
    4851    158756790 :   bool dep;
    4852              : 
    4853              :   /* Don't bother mangling uninstantiated templates.  */
    4854    158756790 :   ++processing_template_decl;
    4855    158756790 :   if (TREE_CODE (decl) == TYPE_DECL)
    4856       329314 :     dep = dependent_type_p (TREE_TYPE (decl));
    4857              :   else
    4858    315567869 :     dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
    4859    264047581 :            && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
    4860    158756790 :   --processing_template_decl;
    4861    158756790 :   if (dep)
    4862              :     return;
    4863              : 
    4864              :   /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
    4865              :      It is not needed to assign names to anonymous namespace, but we use the
    4866              :      "<anon>" marker to be able to tell if type is C++ ODR type or type
    4867              :      produced by other language.  */
    4868    158748879 :   if (TREE_CODE (decl) == TYPE_DECL
    4869       329314 :       && TYPE_STUB_DECL (TREE_TYPE (decl))
    4870    159067622 :       && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
    4871          484 :     id = get_identifier ("<anon>");
    4872              :   else
    4873              :     {
    4874    158748395 :       gcc_assert (TREE_CODE (decl) != TYPE_DECL
    4875              :                   || !no_linkage_check (TREE_TYPE (decl), true));
    4876    158748395 :       if (abi_version_at_least (10))
    4877    158630357 :         if (tree fn = decl_function_context (decl))
    4878      3287428 :           maybe_check_abi_tags (fn, decl);
    4879    158748395 :       id = get_mangled_id (decl);
    4880              :     }
    4881    158748879 :   SET_DECL_ASSEMBLER_NAME (decl, id);
    4882              : 
    4883    158748879 :   if (G.need_cxx17_warning
    4884    158748879 :       && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl)))
    4885           10 :     warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type,
    4886              :                 "mangled name for %qD will change in C++17 because the "
    4887              :                 "exception specification is part of a function type",
    4888              :                 decl);
    4889              : 
    4890    158748879 :   if (id != DECL_NAME (decl)
    4891              :       /* Don't do this for a fake symbol we aren't going to emit anyway.  */
    4892    156211582 :       && TREE_CODE (decl) != TYPE_DECL
    4893    314631147 :       && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    4894              :     {
    4895    136007410 :       int save_ver = flag_abi_version;
    4896    136007410 :       tree id2 = NULL_TREE;
    4897              : 
    4898    136007410 :       if (!DECL_REALLY_EXTERN (decl))
    4899              :         {
    4900     81331935 :           record_mangling (decl, G.need_abi_warning);
    4901              : 
    4902     81331935 :           if (!G.need_abi_warning)
    4903              :             return;
    4904              : 
    4905        26488 :           flag_abi_version = flag_abi_compat_version;
    4906        26488 :           id2 = mangle_decl_string (decl);
    4907        26488 :           id2 = targetm.mangle_decl_assembler_name (decl, id2);
    4908        26488 :           flag_abi_version = save_ver;
    4909              : 
    4910        26488 :           if (id2 != id)
    4911        26372 :             note_mangling_alias (decl, id2);
    4912              :         }
    4913              : 
    4914     54701963 :       if (warn_abi)
    4915              :         {
    4916     53923944 :           const char fabi_version[] = "-fabi-version";
    4917              : 
    4918     53923944 :           if (flag_abi_compat_version != warn_abi_version
    4919     53923227 :               || id2 == NULL_TREE)
    4920              :             {
    4921     53923749 :               flag_abi_version = warn_abi_version;
    4922     53923749 :               id2 = mangle_decl_string (decl);
    4923     53923749 :               id2 = targetm.mangle_decl_assembler_name (decl, id2);
    4924              :             }
    4925     53923944 :           flag_abi_version = save_ver;
    4926              : 
    4927     53923944 :           if (id2 == id)
    4928              :             /* OK.  */;
    4929          266 :           else if (warn_abi_version != 0
    4930          266 :                    && abi_version_at_least (warn_abi_version))
    4931          222 :             warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
    4932              :                         "the mangled name of %qD changed between "
    4933              :                         "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
    4934              :                         G.entity, fabi_version, warn_abi_version, id2,
    4935              :                         fabi_version, save_ver, id);
    4936              :           else
    4937           44 :             warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
    4938              :                         "the mangled name of %qD changes between "
    4939              :                         "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
    4940              :                         G.entity, fabi_version, save_ver, id,
    4941              :                         fabi_version, warn_abi_version, id2);
    4942              :         }
    4943              : 
    4944     54701963 :       flag_abi_version = save_ver;
    4945              :     }
    4946              : }
    4947              : 
    4948              : /* Generate the mangled representation of TYPE.  */
    4949              : 
    4950              : const char *
    4951       434711 : mangle_type_string (const tree type)
    4952              : {
    4953       434711 :   const char *result;
    4954              : 
    4955       434711 :   start_mangling (type);
    4956       434711 :   write_type (type);
    4957       434711 :   result = finish_mangling ();
    4958       434711 :   if (DEBUG_MANGLE)
    4959              :     fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
    4960       434711 :   return result;
    4961              : }
    4962              : 
    4963              : /* Create an identifier for the mangled name of a special component
    4964              :    for belonging to TYPE.  CODE is the ABI-specified code for this
    4965              :    component.  */
    4966              : 
    4967              : static tree
    4968      5578050 : mangle_special_for_type (const tree type, const char *code)
    4969              : {
    4970      5578050 :   tree result;
    4971              : 
    4972              :   /* We don't have an actual decl here for the special component, so
    4973              :      we can't just process the <encoded-name>.  Instead, fake it.  */
    4974      5578050 :   start_mangling (type);
    4975              : 
    4976              :   /* Start the mangling.  */
    4977      5578050 :   write_string ("_Z");
    4978      5578050 :   write_string (code);
    4979              : 
    4980              :   /* Add the type.  */
    4981      5578050 :   write_type (type);
    4982      5578050 :   result = finish_mangling_get_identifier ();
    4983              : 
    4984      5578050 :   if (DEBUG_MANGLE)
    4985              :     fprintf (stderr, "mangle_special_for_type = %s\n\n",
    4986              :              IDENTIFIER_POINTER (result));
    4987              : 
    4988      5578050 :   return result;
    4989              : }
    4990              : 
    4991              : /* Create an identifier for the mangled representation of the typeinfo
    4992              :    structure for TYPE.  */
    4993              : 
    4994              : tree
    4995      3189240 : mangle_typeinfo_for_type (const tree type)
    4996              : {
    4997      3189240 :   return mangle_special_for_type (type, "TI");
    4998              : }
    4999              : 
    5000              : /* Create an identifier for the mangled name of the NTBS containing
    5001              :    the mangled name of TYPE.  */
    5002              : 
    5003              : tree
    5004       427070 : mangle_typeinfo_string_for_type (const tree type)
    5005              : {
    5006       427070 :   return mangle_special_for_type (type, "TS");
    5007              : }
    5008              : 
    5009              : /* Create an identifier for the mangled name of the vtable for TYPE.  */
    5010              : 
    5011              : tree
    5012      1780783 : mangle_vtbl_for_type (const tree type)
    5013              : {
    5014      1780783 :   return mangle_special_for_type (type, "TV");
    5015              : }
    5016              : 
    5017              : /* Returns an identifier for the mangled name of the VTT for TYPE.  */
    5018              : 
    5019              : tree
    5020       180957 : mangle_vtt_for_type (const tree type)
    5021              : {
    5022       180957 :   return mangle_special_for_type (type, "TT");
    5023              : }
    5024              : 
    5025              : /* Returns an identifier for the mangled name of the decomposition
    5026              :    artificial variable DECL.  DECLS is the vector of the VAR_DECLs
    5027              :    for the identifier-list.  */
    5028              : 
    5029              : tree
    5030          764 : mangle_decomp (const tree decl, vec<tree> &decls)
    5031              : {
    5032          764 :   gcc_assert (!type_dependent_expression_p (decl));
    5033              : 
    5034          764 :   location_t saved_loc = input_location;
    5035          764 :   input_location = DECL_SOURCE_LOCATION (decl);
    5036              : 
    5037          764 :   check_abi_tags (decl);
    5038          764 :   start_mangling (decl);
    5039          764 :   write_string ("_Z");
    5040              : 
    5041          764 :   tree context = decl_mangling_context (decl);
    5042          764 :   gcc_assert (context != NULL_TREE);
    5043              : 
    5044          764 :   bool nested = false;
    5045          764 :   bool local = false;
    5046          764 :   if (DECL_NAMESPACE_STD_P (context))
    5047            9 :     write_string ("St");
    5048          755 :   else if (TREE_CODE (context) == FUNCTION_DECL)
    5049              :     {
    5050          263 :       local = true;
    5051          263 :       write_char ('Z');
    5052          263 :       write_encoding (context);
    5053          263 :       write_char ('E');
    5054              :     }
    5055          492 :   else if (context != global_namespace)
    5056              :     {
    5057           94 :       nested = true;
    5058           94 :       write_char ('N');
    5059           94 :       write_prefix (context);
    5060              :     }
    5061              : 
    5062          764 :   write_string ("DC");
    5063          764 :   unsigned int i;
    5064          764 :   tree d;
    5065         2641 :   FOR_EACH_VEC_ELT (decls, i, d)
    5066         1877 :     write_unqualified_name (d);
    5067          764 :   write_char ('E');
    5068              : 
    5069          764 :   if (tree tags = get_abi_tags (decl))
    5070              :     {
    5071              :       /* We didn't emit ABI tags for structured bindings before ABI 19.  */
    5072           30 :       if (!G.need_abi_warning
    5073           30 :           && TREE_PUBLIC (decl)
    5074          120 :           && abi_warn_or_compat_version_crosses (19))
    5075           30 :         G.need_abi_warning = 1;
    5076              : 
    5077           30 :       if (abi_version_at_least (19))
    5078           30 :         write_abi_tags (tags);
    5079              :     }
    5080              : 
    5081          764 :   if (nested)
    5082           94 :     write_char ('E');
    5083          670 :   else if (local && DECL_DISCRIMINATOR_P (decl))
    5084          263 :     write_discriminator (discriminator_for_local_entity (decl));
    5085              : 
    5086          764 :   tree id = finish_mangling_get_identifier ();
    5087          764 :   if (DEBUG_MANGLE)
    5088              :     fprintf (stderr, "mangle_decomp = '%s'\n\n",
    5089              :              IDENTIFIER_POINTER (id));
    5090              : 
    5091          764 :   input_location = saved_loc;
    5092              : 
    5093          764 :   if (warn_abi && G.need_abi_warning)
    5094              :     {
    5095            0 :       const char fabi_version[] = "-fabi-version";
    5096            0 :       tree id2 = id;
    5097            0 :       int save_ver = flag_abi_version;
    5098              : 
    5099            0 :       if (flag_abi_version != warn_abi_version)
    5100              :         {
    5101            0 :           flag_abi_version = warn_abi_version;
    5102            0 :           id2 = mangle_decomp (decl, decls);
    5103            0 :           flag_abi_version = save_ver;
    5104              :         }
    5105              : 
    5106            0 :       if (id2 == id)
    5107              :         /* OK.  */;
    5108            0 :       else if (warn_abi_version != 0
    5109            0 :                && abi_version_at_least (warn_abi_version))
    5110            0 :         warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
    5111              :                     "the mangled name of %qD changed between "
    5112              :                     "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
    5113              :                     G.entity, fabi_version, warn_abi_version, id2,
    5114              :                     fabi_version, save_ver, id);
    5115              :       else
    5116            0 :         warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
    5117              :                     "the mangled name of %qD changes between "
    5118              :                     "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
    5119              :                     G.entity, fabi_version, save_ver, id,
    5120              :                     fabi_version, warn_abi_version, id2);
    5121              :     }
    5122              : 
    5123          764 :   return id;
    5124              : }
    5125              : 
    5126              : /* Return an identifier for a construction vtable group.  TYPE is
    5127              :    the most derived class in the hierarchy; BINFO is the base
    5128              :    subobject for which this construction vtable group will be used.
    5129              : 
    5130              :    This mangling isn't part of the ABI specification; in the ABI
    5131              :    specification, the vtable group is dumped in the same COMDAT as the
    5132              :    main vtable, and is referenced only from that vtable, so it doesn't
    5133              :    need an external name.  For binary formats without COMDAT sections,
    5134              :    though, we need external names for the vtable groups.
    5135              : 
    5136              :    We use the production
    5137              : 
    5138              :     <special-name> ::= CT <type> <offset number> _ <base type>  */
    5139              : 
    5140              : tree
    5141       248237 : mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
    5142              : {
    5143       248237 :   tree result;
    5144              : 
    5145       248237 :   start_mangling (type);
    5146              : 
    5147       248237 :   write_string ("_Z");
    5148       248237 :   write_string ("TC");
    5149       248237 :   write_type (type);
    5150       248237 :   write_integer_cst (BINFO_OFFSET (binfo));
    5151       248237 :   write_char ('_');
    5152       248237 :   write_type (BINFO_TYPE (binfo));
    5153              : 
    5154       248237 :   result = finish_mangling_get_identifier ();
    5155       248237 :   if (DEBUG_MANGLE)
    5156              :     fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
    5157              :              IDENTIFIER_POINTER (result));
    5158       248237 :   return result;
    5159              : }
    5160              : 
    5161              : /* Mangle a this pointer or result pointer adjustment.
    5162              : 
    5163              :    <call-offset> ::= h <fixed offset number> _
    5164              :                  ::= v <fixed offset number> _ <virtual offset number> _ */
    5165              : 
    5166              : static void
    5167       473899 : mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
    5168              : {
    5169       473899 :   write_char (virtual_offset ? 'v' : 'h');
    5170              : 
    5171              :   /* For either flavor, write the fixed offset.  */
    5172       473899 :   write_integer_cst (fixed_offset);
    5173       473899 :   write_char ('_');
    5174              : 
    5175              :   /* For a virtual thunk, add the virtual offset.  */
    5176       473899 :   if (virtual_offset)
    5177              :     {
    5178       354840 :       write_integer_cst (virtual_offset);
    5179       354840 :       write_char ('_');
    5180              :     }
    5181       473899 : }
    5182              : 
    5183              : /* Return an identifier for the mangled name of a this-adjusting or
    5184              :    covariant thunk to FN_DECL.  FIXED_OFFSET is the initial adjustment
    5185              :    to this used to find the vptr.  If VIRTUAL_OFFSET is non-NULL, this
    5186              :    is a virtual thunk, and it is the vtbl offset in
    5187              :    bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
    5188              :    zero for a covariant thunk. Note, that FN_DECL might be a covariant
    5189              :    thunk itself. A covariant thunk name always includes the adjustment
    5190              :    for the this pointer, even if there is none.
    5191              : 
    5192              :    <special-name> ::= T <call-offset> <base encoding>
    5193              :                   ::= Tc <this_adjust call-offset> <result_adjust call-offset>
    5194              :                                         <base encoding>  */
    5195              : 
    5196              : tree
    5197       473518 : mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
    5198              :               tree virtual_offset, tree thunk)
    5199              : {
    5200       473518 :   tree result;
    5201              : 
    5202       473518 :   if (abi_version_at_least (11))
    5203       473515 :     maybe_check_abi_tags (fn_decl, thunk, 11);
    5204              : 
    5205       473518 :   start_mangling (fn_decl);
    5206              : 
    5207       473518 :   write_string ("_Z");
    5208       473518 :   write_char ('T');
    5209              : 
    5210       473518 :   if (!this_adjusting)
    5211              :     {
    5212              :       /* Covariant thunk with no this adjustment */
    5213          214 :       write_char ('c');
    5214          214 :       mangle_call_offset (integer_zero_node, NULL_TREE);
    5215          214 :       mangle_call_offset (fixed_offset, virtual_offset);
    5216              :     }
    5217       473304 :   else if (!DECL_THUNK_P (fn_decl))
    5218              :     /* Plain this adjusting thunk.  */
    5219       473137 :     mangle_call_offset (fixed_offset, virtual_offset);
    5220              :   else
    5221              :     {
    5222              :       /* This adjusting thunk to covariant thunk.  */
    5223          167 :       write_char ('c');
    5224          167 :       mangle_call_offset (fixed_offset, virtual_offset);
    5225          167 :       fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
    5226          167 :       virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
    5227          167 :       if (virtual_offset)
    5228          124 :         virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
    5229          167 :       mangle_call_offset (fixed_offset, virtual_offset);
    5230          167 :       fn_decl = THUNK_TARGET (fn_decl);
    5231              :     }
    5232              : 
    5233              :   /* Scoped name.  */
    5234       473518 :   write_encoding (fn_decl);
    5235              : 
    5236       473518 :   result = finish_mangling_get_identifier ();
    5237       473518 :   if (DEBUG_MANGLE)
    5238              :     fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
    5239       473518 :   return result;
    5240              : }
    5241              : 
    5242              : /* Handle ABI backwards compatibility for past bugs where we didn't call
    5243              :    check_abi_tags in places where it's needed: call check_abi_tags and warn if
    5244              :    it makes a difference.  If FOR_DECL is non-null, it's the declaration
    5245              :    that we're actually trying to mangle; if it's null, we're mangling the
    5246              :    guard variable for T.  */
    5247              : 
    5248              : static void
    5249      3765732 : maybe_check_abi_tags (tree t, tree for_decl, int ver)
    5250              : {
    5251      3765732 :   if (DECL_ASSEMBLER_NAME_SET_P (t))
    5252              :     return;
    5253              : 
    5254       772648 :   tree oldtags = get_abi_tags (t);
    5255              : 
    5256       772648 :   mangle_decl (t);
    5257              : 
    5258       772648 :   tree newtags = get_abi_tags (t);
    5259       772648 :   if (newtags && newtags != oldtags
    5260           30 :       && abi_version_crosses (ver))
    5261              :     {
    5262           12 :       if (for_decl && DECL_THUNK_P (for_decl))
    5263            3 :         warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
    5264              :                     "the mangled name of a thunk for %qD changes between "
    5265              :                     "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
    5266              :                     t, flag_abi_version, warn_abi_version);
    5267            9 :       else if (for_decl)
    5268            6 :         warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
    5269              :                     "the mangled name of %qD changes between "
    5270              :                     "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
    5271              :                     for_decl, flag_abi_version, warn_abi_version);
    5272              :       else
    5273            3 :         warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
    5274              :                     "the mangled name of the initialization guard variable "
    5275              :                     "for %qD changes between %<-fabi-version=%d%> and "
    5276              :                     "%<-fabi-version=%d%>",
    5277              :                     t, flag_abi_version, warn_abi_version);
    5278              :     }
    5279              : }
    5280              : 
    5281              : /* Write out the appropriate string for this variable when generating
    5282              :    another mangled name based on this one.  */
    5283              : 
    5284              : static void
    5285         7462 : write_guarded_var_name (const tree variable)
    5286              : {
    5287         7462 :   if (DECL_NAME (variable)
    5288         7462 :       && startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR"))
    5289              :     /* The name of a guard variable for a reference temporary should refer
    5290              :        to the reference, not the temporary.  */
    5291            6 :     write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
    5292         7456 :   else if (DECL_DECOMPOSITION_P (variable)
    5293          746 :            && DECL_NAME (variable) == NULL_TREE
    5294         7725 :            && startswith (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (variable)),
    5295              :                           "_Z"))
    5296              :     /* The name of a guard variable for a structured binding needs special
    5297              :        casing.  */
    5298          269 :     write_string (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (variable)) + 2);
    5299              :   else
    5300         7187 :     write_name (variable, /*ignore_local_scope=*/0);
    5301         7462 : }
    5302              : 
    5303              : /* Return an identifier for the name of an initialization guard
    5304              :    variable for indicated VARIABLE.  */
    5305              : 
    5306              : tree
    5307         4795 : mangle_guard_variable (const tree variable)
    5308              : {
    5309         4795 :   if (abi_version_at_least (10))
    5310         4789 :     maybe_check_abi_tags (variable);
    5311         4795 :   start_mangling (variable);
    5312         4795 :   write_string ("_ZGV");
    5313         4795 :   write_guarded_var_name (variable);
    5314         4795 :   return finish_mangling_get_identifier ();
    5315              : }
    5316              : 
    5317              : /* Return an identifier for the name of a thread_local initialization
    5318              :    function for VARIABLE.  */
    5319              : 
    5320              : tree
    5321         1190 : mangle_tls_init_fn (const tree variable)
    5322              : {
    5323         1190 :   check_abi_tags (variable);
    5324         1190 :   start_mangling (variable);
    5325         1190 :   write_string ("_ZTH");
    5326         1190 :   write_guarded_var_name (variable);
    5327         1190 :   return finish_mangling_get_identifier ();
    5328              : }
    5329              : 
    5330              : /* Return an identifier for the name of a thread_local wrapper
    5331              :    function for VARIABLE.  */
    5332              : 
    5333              : #define TLS_WRAPPER_PREFIX "_ZTW"
    5334              : 
    5335              : tree
    5336          749 : mangle_tls_wrapper_fn (const tree variable)
    5337              : {
    5338          749 :   check_abi_tags (variable);
    5339          749 :   start_mangling (variable);
    5340          749 :   write_string (TLS_WRAPPER_PREFIX);
    5341          749 :   write_guarded_var_name (variable);
    5342          749 :   return finish_mangling_get_identifier ();
    5343              : }
    5344              : 
    5345              : /* Return true iff FN is a thread_local wrapper function.  */
    5346              : 
    5347              : bool
    5348      2289482 : decl_tls_wrapper_p (const tree fn)
    5349              : {
    5350      2289482 :   if (TREE_CODE (fn) != FUNCTION_DECL)
    5351              :     return false;
    5352      1775191 :   tree name = DECL_NAME (fn);
    5353      1775191 :   return startswith (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX);
    5354              : }
    5355              : 
    5356              : /* Return an identifier for the name of a temporary variable used to
    5357              :    initialize a static reference.  This is now part of the ABI.  */
    5358              : 
    5359              : tree
    5360          728 : mangle_ref_init_variable (const tree variable)
    5361              : {
    5362          728 :   start_mangling (variable);
    5363          728 :   write_string ("_ZGR");
    5364          728 :   check_abi_tags (variable);
    5365          728 :   write_guarded_var_name (variable);
    5366              :   /* Avoid name clashes with aggregate initialization of multiple
    5367              :      references at once.  */
    5368          728 :   write_compact_number (current_ref_temp_count++);
    5369          728 :   return finish_mangling_get_identifier ();
    5370              : }
    5371              : 
    5372              : /* Return an identifier for the mangled name of a C++20 template parameter
    5373              :    object for template argument EXPR.  */
    5374              : 
    5375              : tree
    5376        29114 : mangle_template_parm_object (tree expr)
    5377              : {
    5378        29114 :   start_mangling (expr);
    5379        29114 :   write_string ("_ZTAX");
    5380        29114 :   write_expression (expr);
    5381        29114 :   write_char ('E');
    5382        29114 :   return finish_mangling_get_identifier ();
    5383              : }
    5384              : 
    5385              : /* Given a CLASS_TYPE, such as a record for std::bad_exception this
    5386              :    function generates a mangled name for the vtable map variable of
    5387              :    the class type.  For example, if the class type is
    5388              :    "std::bad_exception", the mangled name for the class is
    5389              :    "St13bad_exception".  This function would generate the name
    5390              :    "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
    5391              :    "_VTV<std::bad_exception>::__vtable_map".  */
    5392              : 
    5393              : 
    5394              : char *
    5395            6 : get_mangled_vtable_map_var_name (tree class_type)
    5396              : {
    5397            6 :   char *var_name = NULL;
    5398            6 :   const char *prefix = "_ZN4_VTVI";
    5399            6 :   const char *postfix = "E12__vtable_mapE";
    5400              : 
    5401            6 :   gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
    5402              : 
    5403            6 :   tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
    5404              : 
    5405            6 :   if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
    5406              :     {
    5407            0 :       class_id = get_mangled_id (TYPE_NAME (class_type));
    5408            0 :       vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
    5409              :     }
    5410              : 
    5411            6 :   unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
    5412              :                      strlen (prefix) +
    5413            6 :                      strlen (postfix) + 1;
    5414              : 
    5415            6 :   var_name = (char *) xmalloc (len);
    5416              : 
    5417            6 :   sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
    5418              : 
    5419            6 :   return var_name;
    5420              : }
    5421              : 
    5422              : #include "gt-cp-mangle.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.