1 /* Name mangling for the 3.0 C++ ABI.
2    Copyright (C) 2000-2014 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 "tm.h"
51 #include "tree.h"
52 #include "stor-layout.h"
53 #include "stringpool.h"
54 #include "tm_p.h"
55 #include "cp-tree.h"
56 #include "obstack.h"
57 #include "flags.h"
58 #include "target.h"
59 #include "cgraph.h"
60 
61 /* Debugging support.  */
62 
63 /* Define DEBUG_MANGLE to enable very verbose trace messages.  */
64 #ifndef DEBUG_MANGLE
65 #define DEBUG_MANGLE 0
66 #endif
67 
68 /* Macros for tracing the write_* functions.  */
69 #if DEBUG_MANGLE
70 # define MANGLE_TRACE(FN, INPUT) \
71   fprintf (stderr, "  %-24s: %-24s\n", (FN), (INPUT))
72 # define MANGLE_TRACE_TREE(FN, NODE) \
73   fprintf (stderr, "  %-24s: %-24s (%p)\n", \
74 	   (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
75 #else
76 # define MANGLE_TRACE(FN, INPUT)
77 # define MANGLE_TRACE_TREE(FN, NODE)
78 #endif
79 
80 /* Nonzero if NODE is a class template-id.  We can't rely on
81    CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
82    that hard to distinguish A<T> from A, where A<T> is the type as
83    instantiated outside of the template, and A is the type used
84    without parameters inside the template.  */
85 #define CLASSTYPE_TEMPLATE_ID_P(NODE)					\
86   (TYPE_LANG_SPECIFIC (NODE) != NULL					\
87    && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM			\
88        || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL			\
89 	   && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
90 
91 /* Things we only need one of.  This module is not reentrant.  */
92 typedef struct GTY(()) globals {
93   /* An array of the current substitution candidates, in the order
94      we've seen them.  */
95   vec<tree, va_gc> *substitutions;
96 
97   /* The entity that is being mangled.  */
98   tree GTY ((skip)) entity;
99 
100   /* How many parameter scopes we are inside.  */
101   int parm_depth;
102 
103   /* True if the mangling will be different in a future version of the
104      ABI.  */
105   bool need_abi_warning;
106 } globals;
107 
108 static GTY (()) globals G;
109 
110 /* The obstack on which we build mangled names.  */
111 static struct obstack *mangle_obstack;
112 
113 /* The obstack on which we build mangled names that are not going to
114    be IDENTIFIER_NODEs.  */
115 static struct obstack name_obstack;
116 
117 /* The first object on the name_obstack; we use this to free memory
118    allocated on the name_obstack.  */
119 static void *name_base;
120 
121 /* Indices into subst_identifiers.  These are identifiers used in
122    special substitution rules.  */
123 typedef enum
124 {
125   SUBID_ALLOCATOR,
126   SUBID_BASIC_STRING,
127   SUBID_CHAR_TRAITS,
128   SUBID_BASIC_ISTREAM,
129   SUBID_BASIC_OSTREAM,
130   SUBID_BASIC_IOSTREAM,
131   SUBID_MAX
132 }
133 substitution_identifier_index_t;
134 
135 /* For quick substitution checks, look up these common identifiers
136    once only.  */
137 static GTY(()) tree subst_identifiers[SUBID_MAX];
138 
139 /* Single-letter codes for builtin integer types, defined in
140    <builtin-type>.  These are indexed by integer_type_kind values.  */
141 static const char
142 integer_type_codes[itk_none] =
143 {
144   'c',  /* itk_char */
145   'a',  /* itk_signed_char */
146   'h',  /* itk_unsigned_char */
147   's',  /* itk_short */
148   't',  /* itk_unsigned_short */
149   'i',  /* itk_int */
150   'j',  /* itk_unsigned_int */
151   'l',  /* itk_long */
152   'm',  /* itk_unsigned_long */
153   'x',  /* itk_long_long */
154   'y',  /* itk_unsigned_long_long */
155   'n',  /* itk_int128 */
156   'o',  /* itk_unsigned_int128  */
157 };
158 
159 static int decl_is_template_id (const tree, tree* const);
160 
161 /* Functions for handling substitutions.  */
162 
163 static inline tree canonicalize_for_substitution (tree);
164 static void add_substitution (tree);
165 static inline int is_std_substitution (const tree,
166 				       const substitution_identifier_index_t);
167 static inline int is_std_substitution_char (const tree,
168 					    const substitution_identifier_index_t);
169 static int find_substitution (tree);
170 static void mangle_call_offset (const tree, const tree);
171 
172 /* Functions for emitting mangled representations of things.  */
173 
174 static void write_mangled_name (const tree, bool);
175 static void write_encoding (const tree);
176 static void write_name (tree, const int);
177 static void write_abi_tags (tree);
178 static void write_unscoped_name (const tree);
179 static void write_unscoped_template_name (const tree);
180 static void write_nested_name (const tree);
181 static void write_prefix (const tree);
182 static void write_template_prefix (const tree);
183 static void write_unqualified_name (tree);
184 static void write_conversion_operator_name (const tree);
185 static void write_source_name (tree);
186 static void write_literal_operator_name (tree);
187 static void write_unnamed_type_name (const tree);
188 static void write_closure_type_name (const tree);
189 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
190 			   const unsigned int);
191 static void write_number (unsigned HOST_WIDE_INT, const int,
192 			  const unsigned int);
193 static void write_compact_number (int num);
194 static void write_integer_cst (const tree);
195 static void write_real_cst (const tree);
196 static void write_identifier (const char *);
197 static void write_special_name_constructor (const tree);
198 static void write_special_name_destructor (const tree);
199 static void write_type (tree);
200 static int write_CV_qualifiers_for_type (const tree);
201 static void write_builtin_type (tree);
202 static void write_function_type (const tree);
203 static void write_bare_function_type (const tree, const int, const tree);
204 static void write_method_parms (tree, const int, const tree);
205 static void write_class_enum_type (const tree);
206 static void write_template_args (tree);
207 static void write_expression (tree);
208 static void write_template_arg_literal (const tree);
209 static void write_template_arg (tree);
210 static void write_template_template_arg (const tree);
211 static void write_array_type (const tree);
212 static void write_pointer_to_member_type (const tree);
213 static void write_template_param (const tree);
214 static void write_template_template_param (const tree);
215 static void write_substitution (const int);
216 static int discriminator_for_local_entity (tree);
217 static int discriminator_for_string_literal (tree, tree);
218 static void write_discriminator (const int);
219 static void write_local_name (tree, const tree, const tree);
220 static void dump_substitution_candidates (void);
221 static tree mangle_decl_string (const tree);
222 static int local_class_index (tree);
223 
224 /* Control functions.  */
225 
226 static inline void start_mangling (const tree);
227 static inline const char *finish_mangling (const bool);
228 static tree mangle_special_for_type (const tree, const char *);
229 
230 /* Foreign language functions.  */
231 
232 static void write_java_integer_type_codes (const tree);
233 
234 /* Append a single character to the end of the mangled
235    representation.  */
236 #define write_char(CHAR)						\
237   obstack_1grow (mangle_obstack, (CHAR))
238 
239 /* Append a sized buffer to the end of the mangled representation.  */
240 #define write_chars(CHAR, LEN)						\
241   obstack_grow (mangle_obstack, (CHAR), (LEN))
242 
243 /* Append a NUL-terminated string to the end of the mangled
244    representation.  */
245 #define write_string(STRING)						\
246   obstack_grow (mangle_obstack, (STRING), strlen (STRING))
247 
248 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
249    same purpose (context, which may be a type) and value (template
250    decl).  See write_template_prefix for more information on what this
251    is used for.  */
252 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2)				\
253   (TREE_CODE (NODE1) == TREE_LIST					\
254    && TREE_CODE (NODE2) == TREE_LIST					\
255    && ((TYPE_P (TREE_PURPOSE (NODE1))					\
256 	&& same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))	\
257        || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2))			\
258    && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
259 
260 /* Write out an unsigned quantity in base 10.  */
261 #define write_unsigned_number(NUMBER)					\
262   write_number ((NUMBER), /*unsigned_p=*/1, 10)
263 
264 /* If DECL is a template instance, return nonzero and, if
265    TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
266    Otherwise return zero.  */
267 
268 static int
decl_is_template_id(const tree decl,tree * const template_info)269 decl_is_template_id (const tree decl, tree* const template_info)
270 {
271   if (TREE_CODE (decl) == TYPE_DECL)
272     {
273       /* TYPE_DECLs are handled specially.  Look at its type to decide
274 	 if this is a template instantiation.  */
275       const tree type = TREE_TYPE (decl);
276 
277       if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
278 	{
279 	  if (template_info != NULL)
280 	    /* For a templated TYPE_DECL, the template info is hanging
281 	       off the type.  */
282 	    *template_info = TYPE_TEMPLATE_INFO (type);
283 	  return 1;
284 	}
285     }
286   else
287     {
288       /* Check if this is a primary template.  */
289       if (DECL_LANG_SPECIFIC (decl) != NULL
290 	  && DECL_USE_TEMPLATE (decl)
291 	  && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
292 	  && TREE_CODE (decl) != TEMPLATE_DECL)
293 	{
294 	  if (template_info != NULL)
295 	    /* For most templated decls, the template info is hanging
296 	       off the decl.  */
297 	    *template_info = DECL_TEMPLATE_INFO (decl);
298 	  return 1;
299 	}
300     }
301 
302   /* It's not a template id.  */
303   return 0;
304 }
305 
306 /* Produce debugging output of current substitution candidates.  */
307 
308 static void
dump_substitution_candidates(void)309 dump_substitution_candidates (void)
310 {
311   unsigned i;
312   tree el;
313 
314   fprintf (stderr, "  ++ substitutions  ");
315   FOR_EACH_VEC_ELT (*G.substitutions, i, el)
316     {
317       const char *name = "???";
318 
319       if (i > 0)
320 	fprintf (stderr, "                    ");
321       if (DECL_P (el))
322 	name = IDENTIFIER_POINTER (DECL_NAME (el));
323       else if (TREE_CODE (el) == TREE_LIST)
324 	name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
325       else if (TYPE_NAME (el))
326 	name = IDENTIFIER_POINTER (TYPE_IDENTIFIER (el));
327       fprintf (stderr, " S%d_ = ", i - 1);
328       if (TYPE_P (el) &&
329 	  (CP_TYPE_RESTRICT_P (el)
330 	   || CP_TYPE_VOLATILE_P (el)
331 	   || CP_TYPE_CONST_P (el)))
332 	fprintf (stderr, "CV-");
333       fprintf (stderr, "%s (%s at %p)\n",
334 	       name, get_tree_code_name (TREE_CODE (el)), (void *) el);
335     }
336 }
337 
338 /* Both decls and types can be substitution candidates, but sometimes
339    they refer to the same thing.  For instance, a TYPE_DECL and
340    RECORD_TYPE for the same class refer to the same thing, and should
341    be treated accordingly in substitutions.  This function returns a
342    canonicalized tree node representing NODE that is used when adding
343    and substitution candidates and finding matches.  */
344 
345 static inline tree
canonicalize_for_substitution(tree node)346 canonicalize_for_substitution (tree node)
347 {
348   /* For a TYPE_DECL, use the type instead.  */
349   if (TREE_CODE (node) == TYPE_DECL)
350     node = TREE_TYPE (node);
351   if (TYPE_P (node)
352       && TYPE_CANONICAL (node) != node
353       && TYPE_MAIN_VARIANT (node) != node)
354     {
355       tree orig = node;
356       /* Here we want to strip the topmost typedef only.
357          We need to do that so is_std_substitution can do proper
358          name matching.  */
359       if (TREE_CODE (node) == FUNCTION_TYPE)
360 	/* Use build_qualified_type and TYPE_QUALS here to preserve
361 	   the old buggy mangling of attribute noreturn with abi<5.  */
362 	node = build_qualified_type (TYPE_MAIN_VARIANT (node),
363 				     TYPE_QUALS (node));
364       else
365 	node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
366 					cp_type_quals (node));
367       if (TREE_CODE (node) == FUNCTION_TYPE
368 	  || TREE_CODE (node) == METHOD_TYPE)
369 	node = build_ref_qualified_type (node, type_memfn_rqual (orig));
370     }
371   return node;
372 }
373 
374 /* Add NODE as a substitution candidate.  NODE must not already be on
375    the list of candidates.  */
376 
377 static void
add_substitution(tree node)378 add_substitution (tree node)
379 {
380   tree c;
381 
382   if (DEBUG_MANGLE)
383     fprintf (stderr, "  ++ add_substitution (%s at %10p)\n",
384 	     get_tree_code_name (TREE_CODE (node)), (void *) node);
385 
386   /* Get the canonicalized substitution candidate for NODE.  */
387   c = canonicalize_for_substitution (node);
388   if (DEBUG_MANGLE && c != node)
389     fprintf (stderr, "  ++ using candidate (%s at %10p)\n",
390 	     get_tree_code_name (TREE_CODE (node)), (void *) node);
391   node = c;
392 
393 #if ENABLE_CHECKING
394   /* Make sure NODE isn't already a candidate.  */
395   {
396     int i;
397     tree candidate;
398 
399     FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
400       {
401 	gcc_assert (!(DECL_P (node) && node == candidate));
402 	gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
403 		      && same_type_p (node, candidate)));
404       }
405   }
406 #endif /* ENABLE_CHECKING */
407 
408   /* Put the decl onto the varray of substitution candidates.  */
409   vec_safe_push (G.substitutions, node);
410 
411   if (DEBUG_MANGLE)
412     dump_substitution_candidates ();
413 }
414 
415 /* Helper function for find_substitution.  Returns nonzero if NODE,
416    which may be a decl or a CLASS_TYPE, is a template-id with template
417    name of substitution_index[INDEX] in the ::std namespace.  */
418 
419 static inline int
is_std_substitution(const tree node,const substitution_identifier_index_t index)420 is_std_substitution (const tree node,
421 		     const substitution_identifier_index_t index)
422 {
423   tree type = NULL;
424   tree decl = NULL;
425 
426   if (DECL_P (node))
427     {
428       type = TREE_TYPE (node);
429       decl = node;
430     }
431   else if (CLASS_TYPE_P (node))
432     {
433       type = node;
434       decl = TYPE_NAME (node);
435     }
436   else
437     /* These are not the droids you're looking for.  */
438     return 0;
439 
440   return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
441 	  && TYPE_LANG_SPECIFIC (type)
442 	  && TYPE_TEMPLATE_INFO (type)
443 	  && (DECL_NAME (TYPE_TI_TEMPLATE (type))
444 	      == subst_identifiers[index]));
445 }
446 
447 /* Helper function for find_substitution.  Returns nonzero if NODE,
448    which may be a decl or a CLASS_TYPE, is the template-id
449    ::std::identifier<char>, where identifier is
450    substitution_index[INDEX].  */
451 
452 static inline int
is_std_substitution_char(const tree node,const substitution_identifier_index_t index)453 is_std_substitution_char (const tree node,
454 			  const substitution_identifier_index_t index)
455 {
456   tree args;
457   /* Check NODE's name is ::std::identifier.  */
458   if (!is_std_substitution (node, index))
459     return 0;
460   /* Figure out its template args.  */
461   if (DECL_P (node))
462     args = DECL_TI_ARGS (node);
463   else if (CLASS_TYPE_P (node))
464     args = CLASSTYPE_TI_ARGS (node);
465   else
466     /* Oops, not a template.  */
467     return 0;
468   /* NODE's template arg list should be <char>.  */
469   return
470     TREE_VEC_LENGTH (args) == 1
471     && TREE_VEC_ELT (args, 0) == char_type_node;
472 }
473 
474 /* Check whether a substitution should be used to represent NODE in
475    the mangling.
476 
477    First, check standard special-case substitutions.
478 
479      <substitution> ::= St
480 	 # ::std
481 
482 		    ::= Sa
483 	 # ::std::allocator
484 
485 		    ::= Sb
486 	 # ::std::basic_string
487 
488 		    ::= Ss
489 	 # ::std::basic_string<char,
490 			       ::std::char_traits<char>,
491 			       ::std::allocator<char> >
492 
493 		    ::= Si
494 	 # ::std::basic_istream<char, ::std::char_traits<char> >
495 
496 		    ::= So
497 	 # ::std::basic_ostream<char, ::std::char_traits<char> >
498 
499 		    ::= Sd
500 	 # ::std::basic_iostream<char, ::std::char_traits<char> >
501 
502    Then examine the stack of currently available substitution
503    candidates for entities appearing earlier in the same mangling
504 
505    If a substitution is found, write its mangled representation and
506    return nonzero.  If none is found, just return zero.  */
507 
508 static int
find_substitution(tree node)509 find_substitution (tree node)
510 {
511   int i;
512   const int size = vec_safe_length (G.substitutions);
513   tree decl;
514   tree type;
515 
516   if (DEBUG_MANGLE)
517     fprintf (stderr, "  ++ find_substitution (%s at %p)\n",
518 	     get_tree_code_name (TREE_CODE (node)), (void *) node);
519 
520   /* Obtain the canonicalized substitution representation for NODE.
521      This is what we'll compare against.  */
522   node = canonicalize_for_substitution (node);
523 
524   /* Check for builtin substitutions.  */
525 
526   decl = TYPE_P (node) ? TYPE_NAME (node) : node;
527   type = TYPE_P (node) ? node : TREE_TYPE (node);
528 
529   /* Check for std::allocator.  */
530   if (decl
531       && is_std_substitution (decl, SUBID_ALLOCATOR)
532       && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
533     {
534       write_string ("Sa");
535       return 1;
536     }
537 
538   /* Check for std::basic_string.  */
539   if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
540     {
541       if (TYPE_P (node))
542 	{
543 	  /* If this is a type (i.e. a fully-qualified template-id),
544 	     check for
545 		 std::basic_string <char,
546 				    std::char_traits<char>,
547 				    std::allocator<char> > .  */
548 	  if (cp_type_quals (type) == TYPE_UNQUALIFIED
549 	      && CLASSTYPE_USE_TEMPLATE (type))
550 	    {
551 	      tree args = CLASSTYPE_TI_ARGS (type);
552 	      if (TREE_VEC_LENGTH (args) == 3
553 		  && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
554 		  && is_std_substitution_char (TREE_VEC_ELT (args, 1),
555 					       SUBID_CHAR_TRAITS)
556 		  && is_std_substitution_char (TREE_VEC_ELT (args, 2),
557 					       SUBID_ALLOCATOR))
558 		{
559 		  write_string ("Ss");
560 		  return 1;
561 		}
562 	    }
563 	}
564       else
565 	/* Substitute for the template name only if this isn't a type.  */
566 	{
567 	  write_string ("Sb");
568 	  return 1;
569 	}
570     }
571 
572   /* Check for basic_{i,o,io}stream.  */
573   if (TYPE_P (node)
574       && cp_type_quals (type) == TYPE_UNQUALIFIED
575       && CLASS_TYPE_P (type)
576       && CLASSTYPE_USE_TEMPLATE (type)
577       && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
578     {
579       /* First, check for the template
580 	 args <char, std::char_traits<char> > .  */
581       tree args = CLASSTYPE_TI_ARGS (type);
582       if (TREE_VEC_LENGTH (args) == 2
583 	  && TYPE_P (TREE_VEC_ELT (args, 0))
584 	  && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
585 	  && is_std_substitution_char (TREE_VEC_ELT (args, 1),
586 				       SUBID_CHAR_TRAITS))
587 	{
588 	  /* Got them.  Is this basic_istream?  */
589 	  if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
590 	    {
591 	      write_string ("Si");
592 	      return 1;
593 	    }
594 	  /* Or basic_ostream?  */
595 	  else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
596 	    {
597 	      write_string ("So");
598 	      return 1;
599 	    }
600 	  /* Or basic_iostream?  */
601 	  else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
602 	    {
603 	      write_string ("Sd");
604 	      return 1;
605 	    }
606 	}
607     }
608 
609   /* Check for namespace std.  */
610   if (decl && DECL_NAMESPACE_STD_P (decl))
611     {
612       write_string ("St");
613       return 1;
614     }
615 
616   /* Now check the list of available substitutions for this mangling
617      operation.  */
618   for (i = 0; i < size; ++i)
619     {
620       tree candidate = (*G.substitutions)[i];
621       /* NODE is a matched to a candidate if it's the same decl node or
622 	 if it's the same type.  */
623       if (decl == candidate
624 	  || (TYPE_P (candidate) && type && TYPE_P (node)
625 	      && same_type_p (type, candidate))
626 	  || NESTED_TEMPLATE_MATCH (node, candidate))
627 	{
628 	  write_substitution (i);
629 	  return 1;
630 	}
631     }
632 
633   /* No substitution found.  */
634   return 0;
635 }
636 
637 
638 /* TOP_LEVEL is true, if this is being called at outermost level of
639   mangling. It should be false when mangling a decl appearing in an
640   expression within some other mangling.
641 
642   <mangled-name>      ::= _Z <encoding>  */
643 
644 static void
write_mangled_name(const tree decl,bool top_level)645 write_mangled_name (const tree decl, bool top_level)
646 {
647   MANGLE_TRACE_TREE ("mangled-name", decl);
648 
649   if (/* The names of `extern "C"' functions are not mangled.  */
650       DECL_EXTERN_C_FUNCTION_P (decl)
651       /* But overloaded operator names *are* mangled.  */
652       && !DECL_OVERLOADED_OPERATOR_P (decl))
653     {
654     unmangled_name:;
655 
656       if (top_level)
657 	write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
658       else
659 	{
660 	  /* The standard notes: "The <encoding> of an extern "C"
661 	     function is treated like global-scope data, i.e. as its
662 	     <source-name> without a type."  We cannot write
663 	     overloaded operators that way though, because it contains
664 	     characters invalid in assembler.  */
665 	  if (abi_version_at_least (2))
666 	    write_string ("_Z");
667 	  else
668 	    G.need_abi_warning = true;
669 	  write_source_name (DECL_NAME (decl));
670 	}
671     }
672   else if (VAR_P (decl)
673 	   /* The names of non-static global variables aren't mangled.  */
674 	   && DECL_EXTERNAL_LINKAGE_P (decl)
675 	   && (CP_DECL_CONTEXT (decl) == global_namespace
676 	       /* And neither are `extern "C"' variables.  */
677 	       || DECL_EXTERN_C_P (decl)))
678     {
679       if (top_level || abi_version_at_least (2))
680 	goto unmangled_name;
681       else
682 	{
683 	  G.need_abi_warning = true;
684 	  goto mangled_name;
685 	}
686     }
687   else
688     {
689     mangled_name:;
690       write_string ("_Z");
691       write_encoding (decl);
692     }
693 }
694 
695 /*   <encoding>		::= <function name> <bare-function-type>
696 			::= <data name>  */
697 
698 static void
write_encoding(const tree decl)699 write_encoding (const tree decl)
700 {
701   MANGLE_TRACE_TREE ("encoding", decl);
702 
703   if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
704     {
705       /* For overloaded operators write just the mangled name
706 	 without arguments.  */
707       if (DECL_OVERLOADED_OPERATOR_P (decl))
708 	write_name (decl, /*ignore_local_scope=*/0);
709       else
710 	write_source_name (DECL_NAME (decl));
711       return;
712     }
713 
714   write_name (decl, /*ignore_local_scope=*/0);
715   if (TREE_CODE (decl) == FUNCTION_DECL)
716     {
717       tree fn_type;
718       tree d;
719 
720       if (decl_is_template_id (decl, NULL))
721 	{
722 	  fn_type = get_mostly_instantiated_function_type (decl);
723 	  /* FN_TYPE will not have parameter types for in-charge or
724 	     VTT parameters.  Therefore, we pass NULL_TREE to
725 	     write_bare_function_type -- otherwise, it will get
726 	     confused about which artificial parameters to skip.  */
727 	  d = NULL_TREE;
728 	}
729       else
730 	{
731 	  fn_type = TREE_TYPE (decl);
732 	  d = decl;
733 	}
734 
735       write_bare_function_type (fn_type,
736 				(!DECL_CONSTRUCTOR_P (decl)
737 				 && !DECL_DESTRUCTOR_P (decl)
738 				 && !DECL_CONV_FN_P (decl)
739 				 && decl_is_template_id (decl, NULL)),
740 				d);
741     }
742 }
743 
744 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
745    or PARM_DECL context, which doesn't belong in DECL_CONTEXT.  */
746 
747 static tree
decl_mangling_context(tree decl)748 decl_mangling_context (tree decl)
749 {
750   tree tcontext = targetm.cxx.decl_mangling_context (decl);
751 
752   if (tcontext != NULL_TREE)
753     return tcontext;
754 
755   if (TREE_CODE (decl) == TEMPLATE_DECL
756       && DECL_TEMPLATE_RESULT (decl))
757     decl = DECL_TEMPLATE_RESULT (decl);
758 
759   if (TREE_CODE (decl) == TYPE_DECL
760       && LAMBDA_TYPE_P (TREE_TYPE (decl)))
761     {
762       tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
763       if (extra)
764 	return extra;
765     }
766     else if (TREE_CODE (decl) == TYPE_DECL
767 	     && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
768      /* template type parms have no mangling context.  */
769       return NULL_TREE;
770   return CP_DECL_CONTEXT (decl);
771 }
772 
773 /* <name> ::= <unscoped-name>
774 	  ::= <unscoped-template-name> <template-args>
775 	  ::= <nested-name>
776 	  ::= <local-name>
777 
778    If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
779    called from <local-name>, which mangles the enclosing scope
780    elsewhere and then uses this function to mangle just the part
781    underneath the function scope.  So don't use the <local-name>
782    production, to avoid an infinite recursion.  */
783 
784 static void
write_name(tree decl,const int ignore_local_scope)785 write_name (tree decl, const int ignore_local_scope)
786 {
787   tree context;
788 
789   MANGLE_TRACE_TREE ("name", decl);
790 
791   if (TREE_CODE (decl) == TYPE_DECL)
792     {
793       /* In case this is a typedef, fish out the corresponding
794 	 TYPE_DECL for the main variant.  */
795       decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
796     }
797 
798   context = decl_mangling_context (decl);
799 
800   gcc_assert (context != NULL_TREE);
801 
802   /* A decl in :: or ::std scope is treated specially.  The former is
803      mangled using <unscoped-name> or <unscoped-template-name>, the
804      latter with a special substitution.  Also, a name that is
805      directly in a local function scope is also mangled with
806      <unscoped-name> rather than a full <nested-name>.  */
807   if (context == global_namespace
808       || DECL_NAMESPACE_STD_P (context)
809       || (ignore_local_scope
810 	  && (TREE_CODE (context) == FUNCTION_DECL
811 	      || (abi_version_at_least (7)
812 		  && TREE_CODE (context) == PARM_DECL))))
813     {
814       tree template_info;
815       /* Is this a template instance?  */
816       if (decl_is_template_id (decl, &template_info))
817 	{
818 	  /* Yes: use <unscoped-template-name>.  */
819 	  write_unscoped_template_name (TI_TEMPLATE (template_info));
820 	  write_template_args (TI_ARGS (template_info));
821 	}
822       else
823 	/* Everything else gets an <unqualified-name>.  */
824 	write_unscoped_name (decl);
825     }
826   else
827     {
828       /* Handle local names, unless we asked not to (that is, invoked
829 	 under <local-name>, to handle only the part of the name under
830 	 the local scope).  */
831       if (!ignore_local_scope)
832 	{
833 	  /* Scan up the list of scope context, looking for a
834 	     function.  If we find one, this entity is in local
835 	     function scope.  local_entity tracks context one scope
836 	     level down, so it will contain the element that's
837 	     directly in that function's scope, either decl or one of
838 	     its enclosing scopes.  */
839 	  tree local_entity = decl;
840 	  while (context != global_namespace)
841 	    {
842 	      /* Make sure we're always dealing with decls.  */
843 	      if (TYPE_P (context))
844 		context = TYPE_NAME (context);
845 	      /* Is this a function?  */
846 	      if (TREE_CODE (context) == FUNCTION_DECL
847 		  || TREE_CODE (context) == PARM_DECL)
848 		{
849 		  /* Yes, we have local scope.  Use the <local-name>
850 		     production for the innermost function scope.  */
851 		  write_local_name (context, local_entity, decl);
852 		  return;
853 		}
854 	      /* Up one scope level.  */
855 	      local_entity = context;
856 	      context = decl_mangling_context (context);
857 	    }
858 
859 	  /* No local scope found?  Fall through to <nested-name>.  */
860 	}
861 
862       /* Other decls get a <nested-name> to encode their scope.  */
863       write_nested_name (decl);
864     }
865 }
866 
867 /* <unscoped-name> ::= <unqualified-name>
868 		   ::= St <unqualified-name>   # ::std::  */
869 
870 static void
write_unscoped_name(const tree decl)871 write_unscoped_name (const tree decl)
872 {
873   tree context = decl_mangling_context (decl);
874 
875   MANGLE_TRACE_TREE ("unscoped-name", decl);
876 
877   /* Is DECL in ::std?  */
878   if (DECL_NAMESPACE_STD_P (context))
879     {
880       write_string ("St");
881       write_unqualified_name (decl);
882     }
883   else
884     {
885       /* If not, it should be either in the global namespace, or directly
886 	 in a local function scope.  A lambda can also be mangled in the
887 	 scope of a default argument.  */
888       gcc_assert (context == global_namespace
889 		  || TREE_CODE (context) == PARM_DECL
890 		  || TREE_CODE (context) == FUNCTION_DECL);
891 
892       write_unqualified_name (decl);
893     }
894 }
895 
896 /* <unscoped-template-name> ::= <unscoped-name>
897 			    ::= <substitution>  */
898 
899 static void
write_unscoped_template_name(const tree decl)900 write_unscoped_template_name (const tree decl)
901 {
902   MANGLE_TRACE_TREE ("unscoped-template-name", decl);
903 
904   if (find_substitution (decl))
905     return;
906   write_unscoped_name (decl);
907   add_substitution (decl);
908 }
909 
910 /* Write the nested name, including CV-qualifiers, of DECL.
911 
912    <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
913 		 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
914 
915    <ref-qualifier> ::= R # & ref-qualifier
916                    ::= O # && ref-qualifier
917    <CV-qualifiers> ::= [r] [V] [K]  */
918 
919 static void
write_nested_name(const tree decl)920 write_nested_name (const tree decl)
921 {
922   tree template_info;
923 
924   MANGLE_TRACE_TREE ("nested-name", decl);
925 
926   write_char ('N');
927 
928   /* Write CV-qualifiers, if this is a member function.  */
929   if (TREE_CODE (decl) == FUNCTION_DECL
930       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
931     {
932       if (DECL_VOLATILE_MEMFUNC_P (decl))
933 	write_char ('V');
934       if (DECL_CONST_MEMFUNC_P (decl))
935 	write_char ('K');
936       if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
937 	{
938 	  if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
939 	    write_char ('O');
940 	  else
941 	    write_char ('R');
942 	}
943     }
944 
945   /* Is this a template instance?  */
946   if (decl_is_template_id (decl, &template_info))
947     {
948       /* Yes, use <template-prefix>.  */
949       write_template_prefix (decl);
950       write_template_args (TI_ARGS (template_info));
951     }
952   else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
953     {
954       tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
955       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
956 	{
957 	  write_template_prefix (decl);
958 	  write_template_args (TREE_OPERAND (name, 1));
959 	}
960       else
961 	{
962 	  write_prefix (decl_mangling_context (decl));
963 	  write_unqualified_name (decl);
964 	}
965     }
966   else
967     {
968       /* No, just use <prefix>  */
969       write_prefix (decl_mangling_context (decl));
970       write_unqualified_name (decl);
971     }
972   write_char ('E');
973 }
974 
975 /* <prefix> ::= <prefix> <unqualified-name>
976 	    ::= <template-param>
977 	    ::= <template-prefix> <template-args>
978 	    ::= <decltype>
979 	    ::= # empty
980 	    ::= <substitution>  */
981 
982 static void
write_prefix(const tree node)983 write_prefix (const tree node)
984 {
985   tree decl;
986   /* Non-NULL if NODE represents a template-id.  */
987   tree template_info = NULL;
988 
989   if (node == NULL
990       || node == global_namespace)
991     return;
992 
993   MANGLE_TRACE_TREE ("prefix", node);
994 
995   if (TREE_CODE (node) == DECLTYPE_TYPE)
996     {
997       write_type (node);
998       return;
999     }
1000 
1001   if (find_substitution (node))
1002     return;
1003 
1004   if (DECL_P (node))
1005     {
1006       /* If this is a function or parm decl, that means we've hit function
1007 	 scope, so this prefix must be for a local name.  In this
1008 	 case, we're under the <local-name> production, which encodes
1009 	 the enclosing function scope elsewhere.  So don't continue
1010 	 here.  */
1011       if (TREE_CODE (node) == FUNCTION_DECL
1012 	  || TREE_CODE (node) == PARM_DECL)
1013 	return;
1014 
1015       decl = node;
1016       decl_is_template_id (decl, &template_info);
1017     }
1018   else
1019     {
1020       /* Node is a type.  */
1021       decl = TYPE_NAME (node);
1022       if (CLASSTYPE_TEMPLATE_ID_P (node))
1023 	template_info = TYPE_TEMPLATE_INFO (node);
1024     }
1025 
1026   /* In G++ 3.2, the name of the template parameter was used.  */
1027   if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1028       && !abi_version_at_least (2))
1029     G.need_abi_warning = true;
1030 
1031   if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1032       && abi_version_at_least (2))
1033     write_template_param (node);
1034   else if (template_info != NULL)
1035     /* Templated.  */
1036     {
1037       write_template_prefix (decl);
1038       write_template_args (TI_ARGS (template_info));
1039     }
1040   else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1041     {
1042       tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1043       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1044 	{
1045 	  write_template_prefix (decl);
1046 	  write_template_args (TREE_OPERAND (name, 1));
1047 	}
1048       else
1049 	{
1050 	  write_prefix (decl_mangling_context (decl));
1051 	  write_unqualified_name (decl);
1052 	}
1053     }
1054   else
1055     /* Not templated.  */
1056     {
1057       write_prefix (decl_mangling_context (decl));
1058       write_unqualified_name (decl);
1059       if (VAR_P (decl)
1060 	  || TREE_CODE (decl) == FIELD_DECL)
1061 	{
1062 	  /* <data-member-prefix> := <member source-name> M */
1063 	  write_char ('M');
1064 	  return;
1065 	}
1066     }
1067 
1068   add_substitution (node);
1069 }
1070 
1071 /* <template-prefix> ::= <prefix> <template component>
1072 		     ::= <template-param>
1073 		     ::= <substitution>  */
1074 
1075 static void
write_template_prefix(const tree node)1076 write_template_prefix (const tree node)
1077 {
1078   tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1079   tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1080   tree context = decl_mangling_context (decl);
1081   tree template_info;
1082   tree templ;
1083   tree substitution;
1084 
1085   MANGLE_TRACE_TREE ("template-prefix", node);
1086 
1087   /* Find the template decl.  */
1088   if (decl_is_template_id (decl, &template_info))
1089     templ = TI_TEMPLATE (template_info);
1090   else if (TREE_CODE (type) == TYPENAME_TYPE)
1091     /* For a typename type, all we have is the name.  */
1092     templ = DECL_NAME (decl);
1093   else
1094     {
1095       gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1096 
1097       templ = TYPE_TI_TEMPLATE (type);
1098     }
1099 
1100   /* For a member template, though, the template name for the
1101      innermost name must have all the outer template levels
1102      instantiated.  For instance, consider
1103 
1104        template<typename T> struct Outer {
1105 	 template<typename U> struct Inner {};
1106        };
1107 
1108      The template name for `Inner' in `Outer<int>::Inner<float>' is
1109      `Outer<int>::Inner<U>'.  In g++, we don't instantiate the template
1110      levels separately, so there's no TEMPLATE_DECL available for this
1111      (there's only `Outer<T>::Inner<U>').
1112 
1113      In order to get the substitutions right, we create a special
1114      TREE_LIST to represent the substitution candidate for a nested
1115      template.  The TREE_PURPOSE is the template's context, fully
1116      instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1117      template.
1118 
1119      So, for the example above, `Outer<int>::Inner' is represented as a
1120      substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1121      and whose value is `Outer<T>::Inner<U>'.  */
1122   if (TYPE_P (context))
1123     substitution = build_tree_list (context, templ);
1124   else
1125     substitution = templ;
1126 
1127   if (find_substitution (substitution))
1128     return;
1129 
1130   /* In G++ 3.2, the name of the template template parameter was used.  */
1131   if (TREE_TYPE (templ)
1132       && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1133       && !abi_version_at_least (2))
1134     G.need_abi_warning = true;
1135 
1136   if (TREE_TYPE (templ)
1137       && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1138       && abi_version_at_least (2))
1139     write_template_param (TREE_TYPE (templ));
1140   else
1141     {
1142       write_prefix (context);
1143       write_unqualified_name (decl);
1144     }
1145 
1146   add_substitution (substitution);
1147 }
1148 
1149 /* We don't need to handle thunks, vtables, or VTTs here.  Those are
1150    mangled through special entry points.
1151 
1152     <unqualified-name>  ::= <operator-name>
1153 			::= <special-name>
1154 			::= <source-name>
1155 			::= <unnamed-type-name>
1156 			::= <local-source-name>
1157 
1158     <local-source-name>	::= L <source-name> <discriminator> */
1159 
1160 static void
write_unqualified_id(tree identifier)1161 write_unqualified_id (tree identifier)
1162 {
1163   if (IDENTIFIER_TYPENAME_P (identifier))
1164     write_conversion_operator_name (TREE_TYPE (identifier));
1165   else if (IDENTIFIER_OPNAME_P (identifier))
1166     {
1167       int i;
1168       const char *mangled_name = NULL;
1169 
1170       /* Unfortunately, there is no easy way to go from the
1171 	 name of the operator back to the corresponding tree
1172 	 code.  */
1173       for (i = 0; i < MAX_TREE_CODES; ++i)
1174 	if (operator_name_info[i].identifier == identifier)
1175 	  {
1176 	    /* The ABI says that we prefer binary operator
1177 	       names to unary operator names.  */
1178 	    if (operator_name_info[i].arity == 2)
1179 	      {
1180 		mangled_name = operator_name_info[i].mangled_name;
1181 		break;
1182 	      }
1183 	    else if (!mangled_name)
1184 	      mangled_name = operator_name_info[i].mangled_name;
1185 	  }
1186 	else if (assignment_operator_name_info[i].identifier
1187 		 == identifier)
1188 	  {
1189 	    mangled_name
1190 	      = assignment_operator_name_info[i].mangled_name;
1191 	    break;
1192 	  }
1193       write_string (mangled_name);
1194     }
1195   else if (UDLIT_OPER_P (identifier))
1196     write_literal_operator_name (identifier);
1197   else
1198     write_source_name (identifier);
1199 }
1200 
1201 static void
write_unqualified_name(tree decl)1202 write_unqualified_name (tree decl)
1203 {
1204   MANGLE_TRACE_TREE ("unqualified-name", decl);
1205 
1206   if (identifier_p (decl))
1207     {
1208       write_unqualified_id (decl);
1209       return;
1210     }
1211 
1212   bool found = false;
1213 
1214   if (DECL_NAME (decl) == NULL_TREE)
1215     {
1216       found = true;
1217       gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1218       write_source_name (DECL_ASSEMBLER_NAME (decl));
1219     }
1220   else if (DECL_DECLARES_FUNCTION_P (decl))
1221     {
1222       found = true;
1223       if (DECL_CONSTRUCTOR_P (decl))
1224 	write_special_name_constructor (decl);
1225       else if (DECL_DESTRUCTOR_P (decl))
1226 	write_special_name_destructor (decl);
1227       else if (DECL_CONV_FN_P (decl))
1228 	{
1229 	  /* Conversion operator. Handle it right here.
1230 	     <operator> ::= cv <type>  */
1231 	  tree type;
1232 	  if (decl_is_template_id (decl, NULL))
1233 	    {
1234 	      tree fn_type;
1235 	      fn_type = get_mostly_instantiated_function_type (decl);
1236 	      type = TREE_TYPE (fn_type);
1237 	    }
1238 	  else if (FNDECL_USED_AUTO (decl))
1239 	    type = (DECL_STRUCT_FUNCTION (decl)->language
1240 		    ->x_auto_return_pattern);
1241 	  else
1242 	    type = DECL_CONV_FN_TYPE (decl);
1243 	  write_conversion_operator_name (type);
1244 	}
1245       else if (DECL_OVERLOADED_OPERATOR_P (decl))
1246 	{
1247 	  operator_name_info_t *oni;
1248 	  if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1249 	    oni = assignment_operator_name_info;
1250 	  else
1251 	    oni = operator_name_info;
1252 
1253 	  write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1254 	}
1255       else if (UDLIT_OPER_P (DECL_NAME (decl)))
1256 	write_literal_operator_name (DECL_NAME (decl));
1257       else
1258 	found = false;
1259     }
1260 
1261   if (found)
1262     /* OK */;
1263   else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1264 	   && DECL_NAMESPACE_SCOPE_P (decl)
1265 	   && decl_linkage (decl) == lk_internal)
1266     {
1267       MANGLE_TRACE_TREE ("local-source-name", decl);
1268       write_char ('L');
1269       write_source_name (DECL_NAME (decl));
1270       /* The default discriminator is 1, and that's all we ever use,
1271 	 so there's no code to output one here.  */
1272     }
1273   else
1274     {
1275       tree type = TREE_TYPE (decl);
1276 
1277       if (TREE_CODE (decl) == TYPE_DECL
1278           && TYPE_ANONYMOUS_P (type))
1279         write_unnamed_type_name (type);
1280       else if (TREE_CODE (decl) == TYPE_DECL
1281                && LAMBDA_TYPE_P (type))
1282         write_closure_type_name (type);
1283       else
1284         write_source_name (DECL_NAME (decl));
1285     }
1286 
1287   /* We use the ABI tags from the primary template, ignoring tags on any
1288      specializations.  This is necessary because C++ doesn't require a
1289      specialization to be declared before it is used unless the use
1290      requires a complete type, but we need to get the tags right on
1291      incomplete types as well.  */
1292   if (tree tmpl = most_general_template (decl))
1293     decl = DECL_TEMPLATE_RESULT (tmpl);
1294   /* Don't crash on an unbound class template.  */
1295   if (decl)
1296     {
1297       tree attrs = (TREE_CODE (decl) == TYPE_DECL
1298 		    ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1299 		    : DECL_ATTRIBUTES (decl));
1300       write_abi_tags (lookup_attribute ("abi_tag", attrs));
1301     }
1302 }
1303 
1304 /* Write the unqualified-name for a conversion operator to TYPE.  */
1305 
1306 static void
write_conversion_operator_name(const tree type)1307 write_conversion_operator_name (const tree type)
1308 {
1309   write_string ("cv");
1310   write_type (type);
1311 }
1312 
1313 /* Non-terminal <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.
1314 
1315      <source-name> ::= </length/ number> <identifier>  */
1316 
1317 static void
write_source_name(tree identifier)1318 write_source_name (tree identifier)
1319 {
1320   MANGLE_TRACE_TREE ("source-name", identifier);
1321 
1322   /* Never write the whole template-id name including the template
1323      arguments; we only want the template name.  */
1324   if (IDENTIFIER_TEMPLATE (identifier))
1325     identifier = IDENTIFIER_TEMPLATE (identifier);
1326 
1327   write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1328   write_identifier (IDENTIFIER_POINTER (identifier));
1329 }
1330 
1331 /* Compare two TREE_STRINGs like strcmp.  */
1332 
1333 int
tree_string_cmp(const void * p1,const void * p2)1334 tree_string_cmp (const void *p1, const void *p2)
1335 {
1336   if (p1 == p2)
1337     return 0;
1338   tree s1 = *(const tree*)p1;
1339   tree s2 = *(const tree*)p2;
1340   return strcmp (TREE_STRING_POINTER (s1),
1341 		 TREE_STRING_POINTER (s2));
1342 }
1343 
1344 /* ID is the name of a function or type with abi_tags attribute TAGS.
1345    Write out the name, suitably decorated.  */
1346 
1347 static void
write_abi_tags(tree tags)1348 write_abi_tags (tree tags)
1349 {
1350   if (tags == NULL_TREE)
1351     return;
1352 
1353   tags = TREE_VALUE (tags);
1354 
1355   vec<tree, va_gc> * vec = make_tree_vector();
1356 
1357   for (tree t = tags; t; t = TREE_CHAIN (t))
1358     {
1359       if (ABI_TAG_IMPLICIT (t))
1360 	continue;
1361       tree str = TREE_VALUE (t);
1362       vec_safe_push (vec, str);
1363     }
1364 
1365   vec->qsort (tree_string_cmp);
1366 
1367   unsigned i; tree str;
1368   FOR_EACH_VEC_ELT (*vec, i, str)
1369     {
1370       write_string ("B");
1371       write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1372       write_identifier (TREE_STRING_POINTER (str));
1373     }
1374 
1375   release_tree_vector (vec);
1376 }
1377 
1378 /* Write a user-defined literal operator.
1379           ::= li <source-name>    # "" <source-name>
1380    IDENTIFIER is an LITERAL_IDENTIFIER_NODE.  */
1381 
1382 static void
write_literal_operator_name(tree identifier)1383 write_literal_operator_name (tree identifier)
1384 {
1385   const char* suffix = UDLIT_OP_SUFFIX (identifier);
1386   write_identifier (UDLIT_OP_MANGLED_PREFIX);
1387   write_unsigned_number (strlen (suffix));
1388   write_identifier (suffix);
1389 }
1390 
1391 /* Encode 0 as _, and 1+ as n-1_.  */
1392 
1393 static void
write_compact_number(int num)1394 write_compact_number (int num)
1395 {
1396   if (num > 0)
1397     write_unsigned_number (num - 1);
1398   write_char ('_');
1399 }
1400 
1401 /* Return how many unnamed types precede TYPE in its enclosing class.  */
1402 
1403 static int
nested_anon_class_index(tree type)1404 nested_anon_class_index (tree type)
1405 {
1406   int index = 0;
1407   tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1408   for (; member; member = DECL_CHAIN (member))
1409     if (DECL_IMPLICIT_TYPEDEF_P (member))
1410       {
1411 	tree memtype = TREE_TYPE (member);
1412 	if (memtype == type)
1413 	  return index;
1414 	else if (TYPE_ANONYMOUS_P (memtype))
1415 	  ++index;
1416       }
1417 
1418   gcc_unreachable ();
1419 }
1420 
1421 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1422 
1423 static void
write_unnamed_type_name(const tree type)1424 write_unnamed_type_name (const tree type)
1425 {
1426   int discriminator;
1427   MANGLE_TRACE_TREE ("unnamed-type-name", type);
1428 
1429   if (TYPE_FUNCTION_SCOPE_P (type))
1430     discriminator = local_class_index (type);
1431   else if (TYPE_CLASS_SCOPE_P (type))
1432     discriminator = nested_anon_class_index (type);
1433   else
1434     {
1435       gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1436       /* Just use the old mangling at namespace scope.  */
1437       write_source_name (TYPE_IDENTIFIER (type));
1438       return;
1439     }
1440 
1441   write_string ("Ut");
1442   write_compact_number (discriminator);
1443 }
1444 
1445 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1446    <lambda-sig> ::= <parameter type>+  # Parameter types or "v" if the lambda has no parameters */
1447 
1448 static void
write_closure_type_name(const tree type)1449 write_closure_type_name (const tree type)
1450 {
1451   tree fn = lambda_function (type);
1452   tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1453   tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1454 
1455   MANGLE_TRACE_TREE ("closure-type-name", type);
1456 
1457   write_string ("Ul");
1458   write_method_parms (parms, /*method_p=*/1, fn);
1459   write_char ('E');
1460   write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1461 }
1462 
1463 /* Convert NUMBER to ascii using base BASE and generating at least
1464    MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1465    into which to store the characters. Returns the number of
1466    characters generated (these will be laid out in advance of where
1467    BUFFER points).  */
1468 
1469 static int
hwint_to_ascii(unsigned HOST_WIDE_INT number,const unsigned int base,char * buffer,const unsigned int min_digits)1470 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1471 		char *buffer, const unsigned int min_digits)
1472 {
1473   static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1474   unsigned digits = 0;
1475 
1476   while (number)
1477     {
1478       unsigned HOST_WIDE_INT d = number / base;
1479 
1480       *--buffer = base_digits[number - d * base];
1481       digits++;
1482       number = d;
1483     }
1484   while (digits < min_digits)
1485     {
1486       *--buffer = base_digits[0];
1487       digits++;
1488     }
1489   return digits;
1490 }
1491 
1492 /* Non-terminal <number>.
1493 
1494      <number> ::= [n] </decimal integer/>  */
1495 
1496 static void
write_number(unsigned HOST_WIDE_INT number,const int unsigned_p,const unsigned int base)1497 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1498 	      const unsigned int base)
1499 {
1500   char buffer[sizeof (HOST_WIDE_INT) * 8];
1501   unsigned count = 0;
1502 
1503   if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1504     {
1505       write_char ('n');
1506       number = -((HOST_WIDE_INT) number);
1507     }
1508   count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1509   write_chars (buffer + sizeof (buffer) - count, count);
1510 }
1511 
1512 /* Write out an integral CST in decimal. Most numbers are small, and
1513    representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1514    bigger than that, which we must deal with.  */
1515 
1516 static inline void
write_integer_cst(const tree cst)1517 write_integer_cst (const tree cst)
1518 {
1519   int sign = tree_int_cst_sgn (cst);
1520 
1521   if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1522     {
1523       /* A bignum. We do this in chunks, each of which fits in a
1524 	 HOST_WIDE_INT.  */
1525       char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1526       unsigned HOST_WIDE_INT chunk;
1527       unsigned chunk_digits;
1528       char *ptr = buffer + sizeof (buffer);
1529       unsigned count = 0;
1530       tree n, base, type;
1531       int done;
1532 
1533       /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1534 	 representable.  */
1535       chunk = 1000000000;
1536       chunk_digits = 9;
1537 
1538       if (sizeof (HOST_WIDE_INT) >= 8)
1539 	{
1540 	  /* It is at least 64 bits, so 10^18 is representable.  */
1541 	  chunk_digits = 18;
1542 	  chunk *= chunk;
1543 	}
1544 
1545       type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1546       base = build_int_cstu (type, chunk);
1547       n = build_int_cst_wide (type,
1548 			      TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1549 
1550       if (sign < 0)
1551 	{
1552 	  write_char ('n');
1553 	  n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1554 	}
1555       do
1556 	{
1557 	  tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1558 	  tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1559 	  unsigned c;
1560 
1561 	  done = integer_zerop (d);
1562 	  tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1563 	  c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1564 			      done ? 1 : chunk_digits);
1565 	  ptr -= c;
1566 	  count += c;
1567 	  n = d;
1568 	}
1569       while (!done);
1570       write_chars (ptr, count);
1571     }
1572   else
1573     {
1574       /* A small num.  */
1575       unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1576 
1577       if (sign < 0)
1578 	{
1579 	  write_char ('n');
1580 	  low = -low;
1581 	}
1582       write_unsigned_number (low);
1583     }
1584 }
1585 
1586 /* Write out a floating-point literal.
1587 
1588     "Floating-point literals are encoded using the bit pattern of the
1589     target processor's internal representation of that number, as a
1590     fixed-length lowercase hexadecimal string, high-order bytes first
1591     (even if the target processor would store low-order bytes first).
1592     The "n" prefix is not used for floating-point literals; the sign
1593     bit is encoded with the rest of the number.
1594 
1595     Here are some examples, assuming the IEEE standard representation
1596     for floating point numbers.  (Spaces are for readability, not
1597     part of the encoding.)
1598 
1599 	1.0f			Lf 3f80 0000 E
1600        -1.0f			Lf bf80 0000 E
1601 	1.17549435e-38f		Lf 0080 0000 E
1602 	1.40129846e-45f		Lf 0000 0001 E
1603 	0.0f			Lf 0000 0000 E"
1604 
1605    Caller is responsible for the Lx and the E.  */
1606 static void
write_real_cst(const tree value)1607 write_real_cst (const tree value)
1608 {
1609   if (abi_version_at_least (2))
1610     {
1611       long target_real[4];  /* largest supported float */
1612       char buffer[9];       /* eight hex digits in a 32-bit number */
1613       int i, limit, dir;
1614 
1615       tree type = TREE_TYPE (value);
1616       int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1617 
1618       real_to_target (target_real, &TREE_REAL_CST (value),
1619 		      TYPE_MODE (type));
1620 
1621       /* The value in target_real is in the target word order,
1622 	 so we must write it out backward if that happens to be
1623 	 little-endian.  write_number cannot be used, it will
1624 	 produce uppercase.  */
1625       if (FLOAT_WORDS_BIG_ENDIAN)
1626 	i = 0, limit = words, dir = 1;
1627       else
1628 	i = words - 1, limit = -1, dir = -1;
1629 
1630       for (; i != limit; i += dir)
1631 	{
1632 	  sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1633 	  write_chars (buffer, 8);
1634 	}
1635     }
1636   else
1637     {
1638       /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1639 	 literally.  Note that compatibility with 3.2 is impossible,
1640 	 because the old floating-point emulator used a different
1641 	 format for REAL_VALUE_TYPE.  */
1642       size_t i;
1643       for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1644 	write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1645 		      /*unsigned_p*/ 1,
1646 		      /*base*/ 16);
1647       G.need_abi_warning = 1;
1648     }
1649 }
1650 
1651 /* Non-terminal <identifier>.
1652 
1653      <identifier> ::= </unqualified source code identifier>  */
1654 
1655 static void
write_identifier(const char * identifier)1656 write_identifier (const char *identifier)
1657 {
1658   MANGLE_TRACE ("identifier", identifier);
1659   write_string (identifier);
1660 }
1661 
1662 /* Handle constructor productions of non-terminal <special-name>.
1663    CTOR is a constructor FUNCTION_DECL.
1664 
1665      <special-name> ::= C1   # complete object constructor
1666 		    ::= C2   # base object constructor
1667 		    ::= C3   # complete object allocating constructor
1668 
1669    Currently, allocating constructors are never used.  */
1670 
1671 static void
write_special_name_constructor(const tree ctor)1672 write_special_name_constructor (const tree ctor)
1673 {
1674   if (DECL_BASE_CONSTRUCTOR_P (ctor))
1675     write_string ("C2");
1676   /* This is the old-style "[unified]" constructor.
1677      In some cases, we may emit this function and call
1678      it from the clones in order to share code and save space.  */
1679   else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1680     write_string ("C4");
1681   else
1682     {
1683       gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1684       write_string ("C1");
1685     }
1686 }
1687 
1688 /* Handle destructor productions of non-terminal <special-name>.
1689    DTOR is a destructor FUNCTION_DECL.
1690 
1691      <special-name> ::= D0 # deleting (in-charge) destructor
1692 		    ::= D1 # complete object (in-charge) destructor
1693 		    ::= D2 # base object (not-in-charge) destructor  */
1694 
1695 static void
write_special_name_destructor(const tree dtor)1696 write_special_name_destructor (const tree dtor)
1697 {
1698   if (DECL_DELETING_DESTRUCTOR_P (dtor))
1699     write_string ("D0");
1700   else if (DECL_BASE_DESTRUCTOR_P (dtor))
1701     write_string ("D2");
1702   else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1703     /* This is the old-style "[unified]" destructor.
1704        In some cases, we may emit this function and call
1705        it from the clones in order to share code and save space.  */
1706     write_string ("D4");
1707   else
1708     {
1709       gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1710       write_string ("D1");
1711     }
1712 }
1713 
1714 /* Scan the vector of local classes and return how many others with the
1715    same name (or same no name) and context precede ENTITY.  */
1716 
1717 static int
local_class_index(tree entity)1718 local_class_index (tree entity)
1719 {
1720   int ix, discriminator = 0;
1721   tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1722 	       : TYPE_IDENTIFIER (entity));
1723   tree ctx = TYPE_CONTEXT (entity);
1724   for (ix = 0; ; ix++)
1725     {
1726       tree type = (*local_classes)[ix];
1727       if (type == entity)
1728 	return discriminator;
1729       if (TYPE_CONTEXT (type) == ctx
1730 	  && (name ? TYPE_IDENTIFIER (type) == name
1731 	      : TYPE_ANONYMOUS_P (type)))
1732 	++discriminator;
1733     }
1734   gcc_unreachable ();
1735 }
1736 
1737 /* Return the discriminator for ENTITY appearing inside
1738    FUNCTION.  The discriminator is the lexical ordinal of VAR among
1739    entities with the same name in the same FUNCTION.  */
1740 
1741 static int
discriminator_for_local_entity(tree entity)1742 discriminator_for_local_entity (tree entity)
1743 {
1744   if (DECL_DISCRIMINATOR_P (entity))
1745     {
1746       if (DECL_DISCRIMINATOR_SET_P (entity))
1747 	return DECL_DISCRIMINATOR (entity);
1748       else
1749 	/* The first entity with a particular name doesn't get
1750 	   DECL_DISCRIMINATOR set up.  */
1751 	return 0;
1752     }
1753   else if (TREE_CODE (entity) == TYPE_DECL)
1754     {
1755       /* Scan the list of local classes.  */
1756       entity = TREE_TYPE (entity);
1757 
1758       /* Lambdas and unnamed types have their own discriminators.  */
1759       if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1760 	return 0;
1761 
1762       return local_class_index (entity);
1763     }
1764   else
1765     gcc_unreachable ();
1766 }
1767 
1768 /* Return the discriminator for STRING, a string literal used inside
1769    FUNCTION.  The discriminator is the lexical ordinal of STRING among
1770    string literals used in FUNCTION.  */
1771 
1772 static int
discriminator_for_string_literal(tree,tree)1773 discriminator_for_string_literal (tree /*function*/,
1774 				  tree /*string*/)
1775 {
1776   /* For now, we don't discriminate amongst string literals.  */
1777   return 0;
1778 }
1779 
1780 /*   <discriminator> := _ <number>
1781 
1782    The discriminator is used only for the second and later occurrences
1783    of the same name within a single function. In this case <number> is
1784    n - 2, if this is the nth occurrence, in lexical order.  */
1785 
1786 static void
write_discriminator(const int discriminator)1787 write_discriminator (const int discriminator)
1788 {
1789   /* If discriminator is zero, don't write anything.  Otherwise...  */
1790   if (discriminator > 0)
1791     {
1792       write_char ('_');
1793       write_unsigned_number (discriminator - 1);
1794     }
1795 }
1796 
1797 /* Mangle the name of a function-scope entity.  FUNCTION is the
1798    FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1799    default argument scope.  ENTITY is the decl for the entity itself.
1800    LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1801    either ENTITY itself or an enclosing scope of ENTITY.
1802 
1803      <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1804 		  := Z <function encoding> E s [<discriminator>]
1805 		  := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1806 
1807 static void
write_local_name(tree function,const tree local_entity,const tree entity)1808 write_local_name (tree function, const tree local_entity,
1809 		  const tree entity)
1810 {
1811   tree parm = NULL_TREE;
1812 
1813   MANGLE_TRACE_TREE ("local-name", entity);
1814 
1815   if (TREE_CODE (function) == PARM_DECL)
1816     {
1817       parm = function;
1818       function = DECL_CONTEXT (parm);
1819     }
1820 
1821   write_char ('Z');
1822   write_encoding (function);
1823   write_char ('E');
1824 
1825   /* For this purpose, parameters are numbered from right-to-left.  */
1826   if (parm)
1827     {
1828       tree t;
1829       int i = 0;
1830       for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1831 	{
1832 	  if (t == parm)
1833 	    i = 1;
1834 	  else if (i)
1835 	    ++i;
1836 	}
1837       write_char ('d');
1838       write_compact_number (i - 1);
1839     }
1840 
1841   if (TREE_CODE (entity) == STRING_CST)
1842     {
1843       write_char ('s');
1844       write_discriminator (discriminator_for_string_literal (function,
1845 							     entity));
1846     }
1847   else
1848     {
1849       /* Now the <entity name>.  Let write_name know its being called
1850 	 from <local-name>, so it doesn't try to process the enclosing
1851 	 function scope again.  */
1852       write_name (entity, /*ignore_local_scope=*/1);
1853       write_discriminator (discriminator_for_local_entity (local_entity));
1854     }
1855 }
1856 
1857 /* Non-terminals <type> and <CV-qualifier>.
1858 
1859      <type> ::= <builtin-type>
1860 	    ::= <function-type>
1861 	    ::= <class-enum-type>
1862 	    ::= <array-type>
1863 	    ::= <pointer-to-member-type>
1864 	    ::= <template-param>
1865 	    ::= <substitution>
1866 	    ::= <CV-qualifier>
1867 	    ::= P <type>    # pointer-to
1868 	    ::= R <type>    # reference-to
1869 	    ::= C <type>    # complex pair (C 2000)
1870 	    ::= G <type>    # imaginary (C 2000)     [not supported]
1871 	    ::= U <source-name> <type>   # vendor extended type qualifier
1872 
1873    C++0x extensions
1874 
1875      <type> ::= RR <type>   # rvalue reference-to
1876      <type> ::= Dt <expression> # decltype of an id-expression or
1877                                 # class member access
1878      <type> ::= DT <expression> # decltype of an expression
1879      <type> ::= Dn              # decltype of nullptr
1880 
1881    TYPE is a type node.  */
1882 
1883 static void
write_type(tree type)1884 write_type (tree type)
1885 {
1886   /* This gets set to nonzero if TYPE turns out to be a (possibly
1887      CV-qualified) builtin type.  */
1888   int is_builtin_type = 0;
1889 
1890   MANGLE_TRACE_TREE ("type", type);
1891 
1892   if (type == error_mark_node)
1893     return;
1894 
1895   type = canonicalize_for_substitution (type);
1896   if (find_substitution (type))
1897     return;
1898 
1899 
1900   if (write_CV_qualifiers_for_type (type) > 0)
1901     /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1902        mangle the unqualified type.  The recursive call is needed here
1903        since both the qualified and unqualified types are substitution
1904        candidates.  */
1905     {
1906       tree t = TYPE_MAIN_VARIANT (type);
1907       if (TREE_CODE (t) == FUNCTION_TYPE
1908 	  || TREE_CODE (t) == METHOD_TYPE)
1909 	{
1910 	  t = build_ref_qualified_type (t, type_memfn_rqual (type));
1911 	  if (abi_version_at_least (8))
1912 	    /* Avoid adding the unqualified function type as a substitution.  */
1913 	    write_function_type (t);
1914 	  else
1915 	    write_type (t);
1916 	}
1917       else
1918 	write_type (t);
1919     }
1920   else if (TREE_CODE (type) == ARRAY_TYPE)
1921     /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1922        so that the cv-qualification of the element type is available
1923        in write_array_type.  */
1924     write_array_type (type);
1925   else
1926     {
1927       tree type_orig = type;
1928 
1929       /* See through any typedefs.  */
1930       type = TYPE_MAIN_VARIANT (type);
1931       if (TREE_CODE (type) == FUNCTION_TYPE
1932 	  || TREE_CODE (type) == METHOD_TYPE)
1933 	type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1934 
1935       /* According to the C++ ABI, some library classes are passed the
1936 	 same as the scalar type of their single member and use the same
1937 	 mangling.  */
1938       if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1939 	type = TREE_TYPE (first_field (type));
1940 
1941       if (TYPE_PTRDATAMEM_P (type))
1942 	write_pointer_to_member_type (type);
1943       else
1944         {
1945 	  /* Handle any target-specific fundamental types.  */
1946 	  const char *target_mangling
1947 	    = targetm.mangle_type (type_orig);
1948 
1949 	  if (target_mangling)
1950 	    {
1951 	      write_string (target_mangling);
1952 	      /* Add substitutions for types other than fundamental
1953 		 types.  */
1954 	      if (!VOID_TYPE_P (type)
1955 		  && TREE_CODE (type) != INTEGER_TYPE
1956 		  && TREE_CODE (type) != REAL_TYPE
1957 		  && TREE_CODE (type) != BOOLEAN_TYPE)
1958 		add_substitution (type);
1959 	      return;
1960 	    }
1961 
1962 	  switch (TREE_CODE (type))
1963 	    {
1964 	    case VOID_TYPE:
1965 	    case BOOLEAN_TYPE:
1966 	    case INTEGER_TYPE:  /* Includes wchar_t.  */
1967 	    case REAL_TYPE:
1968 	    case FIXED_POINT_TYPE:
1969 	      {
1970 		/* If this is a typedef, TYPE may not be one of
1971 		   the standard builtin type nodes, but an alias of one.  Use
1972 		   TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
1973 		write_builtin_type (TYPE_MAIN_VARIANT (type));
1974 		++is_builtin_type;
1975 	      }
1976 	      break;
1977 
1978 	    case COMPLEX_TYPE:
1979 	      write_char ('C');
1980 	      write_type (TREE_TYPE (type));
1981 	      break;
1982 
1983 	    case FUNCTION_TYPE:
1984 	    case METHOD_TYPE:
1985 	      write_function_type (type);
1986 	      break;
1987 
1988 	    case UNION_TYPE:
1989 	    case RECORD_TYPE:
1990 	    case ENUMERAL_TYPE:
1991 	      /* A pointer-to-member function is represented as a special
1992 		 RECORD_TYPE, so check for this first.  */
1993 	      if (TYPE_PTRMEMFUNC_P (type))
1994 		write_pointer_to_member_type (type);
1995 	      else
1996 		write_class_enum_type (type);
1997 	      break;
1998 
1999 	    case TYPENAME_TYPE:
2000 	    case UNBOUND_CLASS_TEMPLATE:
2001 	      /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2002 		 ordinary nested names.  */
2003 	      write_nested_name (TYPE_STUB_DECL (type));
2004 	      break;
2005 
2006 	    case POINTER_TYPE:
2007 	    case REFERENCE_TYPE:
2008 	      if (TYPE_PTR_P (type))
2009 		write_char ('P');
2010 	      else if (TYPE_REF_IS_RVALUE (type))
2011 		write_char ('O');
2012               else
2013                 write_char ('R');
2014 	      {
2015 		tree target = TREE_TYPE (type);
2016 		/* Attribute const/noreturn are not reflected in mangling.
2017 		   We strip them here rather than at a lower level because
2018 		   a typedef or template argument can have function type
2019 		   with function-cv-quals (that use the same representation),
2020 		   but you can't have a pointer/reference to such a type.  */
2021 		if (abi_version_at_least (5)
2022 		    && TREE_CODE (target) == FUNCTION_TYPE)
2023 		  target = build_qualified_type (target, TYPE_UNQUALIFIED);
2024 		write_type (target);
2025 	      }
2026 	      break;
2027 
2028 	    case TEMPLATE_TYPE_PARM:
2029 	      if (is_auto (type))
2030 		{
2031 		  if (AUTO_IS_DECLTYPE (type))
2032 		    write_identifier ("Dc");
2033 		  else
2034 		    write_identifier ("Da");
2035 		  ++is_builtin_type;
2036 		  break;
2037 		}
2038 	      /* else fall through.  */
2039 	    case TEMPLATE_PARM_INDEX:
2040 	      write_template_param (type);
2041 	      break;
2042 
2043 	    case TEMPLATE_TEMPLATE_PARM:
2044 	      write_template_template_param (type);
2045 	      break;
2046 
2047 	    case BOUND_TEMPLATE_TEMPLATE_PARM:
2048 	      write_template_template_param (type);
2049 	      write_template_args
2050 		(TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2051 	      break;
2052 
2053 	    case VECTOR_TYPE:
2054 	      if (abi_version_at_least (4))
2055 		{
2056 		  write_string ("Dv");
2057 		  /* Non-constant vector size would be encoded with
2058 		     _ expression, but we don't support that yet.  */
2059 		  write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2060 		  write_char ('_');
2061 		}
2062 	      else
2063 		{
2064 		  G.need_abi_warning = 1;
2065 		  write_string ("U8__vector");
2066 		}
2067 	      write_type (TREE_TYPE (type));
2068 	      break;
2069 
2070             case TYPE_PACK_EXPANSION:
2071               write_string ("Dp");
2072               write_type (PACK_EXPANSION_PATTERN (type));
2073               break;
2074 
2075             case DECLTYPE_TYPE:
2076 	      /* These shouldn't make it into mangling.  */
2077 	      gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2078 			  && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2079 
2080 	      /* In ABI <5, we stripped decltype of a plain decl.  */
2081 	      if (!abi_version_at_least (5)
2082 		  && DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2083 		{
2084 		  tree expr = DECLTYPE_TYPE_EXPR (type);
2085 		  tree etype = NULL_TREE;
2086 		  switch (TREE_CODE (expr))
2087 		    {
2088 		    case VAR_DECL:
2089 		    case PARM_DECL:
2090 		    case RESULT_DECL:
2091 		    case FUNCTION_DECL:
2092 		    case CONST_DECL:
2093 		    case TEMPLATE_PARM_INDEX:
2094 		      etype = TREE_TYPE (expr);
2095 		      break;
2096 
2097 		    default:
2098 		      break;
2099 		    }
2100 
2101 		  if (etype && !type_uses_auto (etype))
2102 		    {
2103 		      G.need_abi_warning = 1;
2104 		      write_type (etype);
2105 		      return;
2106 		    }
2107 		}
2108 
2109               write_char ('D');
2110               if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2111                 write_char ('t');
2112               else
2113                 write_char ('T');
2114 	      ++cp_unevaluated_operand;
2115               write_expression (DECLTYPE_TYPE_EXPR (type));
2116 	      --cp_unevaluated_operand;
2117               write_char ('E');
2118               break;
2119 
2120 	    case NULLPTR_TYPE:
2121 	      write_string ("Dn");
2122 	      if (abi_version_at_least (7))
2123 		++is_builtin_type;
2124 	      break;
2125 
2126 	    case TYPEOF_TYPE:
2127 	      sorry ("mangling typeof, use decltype instead");
2128 	      break;
2129 
2130 	    case UNDERLYING_TYPE:
2131 	      sorry ("mangling __underlying_type");
2132 	      break;
2133 
2134 	    case LANG_TYPE:
2135 	      /* fall through.  */
2136 
2137 	    default:
2138 	      gcc_unreachable ();
2139 	    }
2140 	}
2141     }
2142 
2143   /* Types other than builtin types are substitution candidates.  */
2144   if (!is_builtin_type)
2145     add_substitution (type);
2146 }
2147 
2148 /* Non-terminal <CV-qualifiers> for type nodes.  Returns the number of
2149    CV-qualifiers written for TYPE.
2150 
2151      <CV-qualifiers> ::= [r] [V] [K]  */
2152 
2153 static int
write_CV_qualifiers_for_type(const tree type)2154 write_CV_qualifiers_for_type (const tree type)
2155 {
2156   int num_qualifiers = 0;
2157 
2158   /* The order is specified by:
2159 
2160        "In cases where multiple order-insensitive qualifiers are
2161        present, they should be ordered 'K' (closest to the base type),
2162        'V', 'r', and 'U' (farthest from the base type) ..."
2163 
2164      Note that we do not use cp_type_quals below; given "const
2165      int[3]", the "const" is emitted with the "int", not with the
2166      array.  */
2167   cp_cv_quals quals = TYPE_QUALS (type);
2168 
2169   if (quals & TYPE_QUAL_RESTRICT)
2170     {
2171       write_char ('r');
2172       ++num_qualifiers;
2173     }
2174   if (quals & TYPE_QUAL_VOLATILE)
2175     {
2176       write_char ('V');
2177       ++num_qualifiers;
2178     }
2179   if (quals & TYPE_QUAL_CONST)
2180     {
2181       write_char ('K');
2182       ++num_qualifiers;
2183     }
2184 
2185   return num_qualifiers;
2186 }
2187 
2188 /* Non-terminal <builtin-type>.
2189 
2190      <builtin-type> ::= v   # void
2191 		    ::= b   # bool
2192 		    ::= w   # wchar_t
2193 		    ::= c   # char
2194 		    ::= a   # signed char
2195 		    ::= h   # unsigned char
2196 		    ::= s   # short
2197 		    ::= t   # unsigned short
2198 		    ::= i   # int
2199 		    ::= j   # unsigned int
2200 		    ::= l   # long
2201 		    ::= m   # unsigned long
2202 		    ::= x   # long long, __int64
2203 		    ::= y   # unsigned long long, __int64
2204 		    ::= n   # __int128
2205 		    ::= o   # unsigned __int128
2206 		    ::= f   # float
2207 		    ::= d   # double
2208 		    ::= e   # long double, __float80
2209 		    ::= g   # __float128          [not supported]
2210 		    ::= u <source-name>  # vendor extended type */
2211 
2212 static void
write_builtin_type(tree type)2213 write_builtin_type (tree type)
2214 {
2215   if (TYPE_CANONICAL (type))
2216     type = TYPE_CANONICAL (type);
2217 
2218   switch (TREE_CODE (type))
2219     {
2220     case VOID_TYPE:
2221       write_char ('v');
2222       break;
2223 
2224     case BOOLEAN_TYPE:
2225       write_char ('b');
2226       break;
2227 
2228     case INTEGER_TYPE:
2229       /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2230 	 isn't in integer_type_nodes.  */
2231       if (type == wchar_type_node)
2232 	write_char ('w');
2233       else if (type == char16_type_node)
2234 	write_string ("Ds");
2235       else if (type == char32_type_node)
2236 	write_string ("Di");
2237       else if (TYPE_FOR_JAVA (type))
2238 	write_java_integer_type_codes (type);
2239       else
2240 	{
2241 	  size_t itk;
2242 	  /* Assume TYPE is one of the shared integer type nodes.  Find
2243 	     it in the array of these nodes.  */
2244 	iagain:
2245 	  for (itk = 0; itk < itk_none; ++itk)
2246 	    if (integer_types[itk] != NULL_TREE
2247 		&& type == integer_types[itk])
2248 	      {
2249 		/* Print the corresponding single-letter code.  */
2250 		write_char (integer_type_codes[itk]);
2251 		break;
2252 	      }
2253 
2254 	  if (itk == itk_none)
2255 	    {
2256 	      tree t = c_common_type_for_mode (TYPE_MODE (type),
2257 					       TYPE_UNSIGNED (type));
2258 	      if (type != t)
2259 		{
2260 		  type = t;
2261 		  goto iagain;
2262 		}
2263 
2264 	      if (TYPE_PRECISION (type) == 128)
2265 		write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2266 	      else
2267 		{
2268 		  /* Allow for cases where TYPE is not one of the shared
2269 		     integer type nodes and write a "vendor extended builtin
2270 		     type" with a name the form intN or uintN, respectively.
2271 		     Situations like this can happen if you have an
2272 		     __attribute__((__mode__(__SI__))) type and use exotic
2273 		     switches like '-mint8' on AVR.  Of course, this is
2274 		     undefined by the C++ ABI (and '-mint8' is not even
2275 		     Standard C conforming), but when using such special
2276 		     options you're pretty much in nowhere land anyway.  */
2277 		  const char *prefix;
2278 		  char prec[11];	/* up to ten digits for an unsigned */
2279 
2280 		  prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2281 		  sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2282 		  write_char ('u');	/* "vendor extended builtin type" */
2283 		  write_unsigned_number (strlen (prefix) + strlen (prec));
2284 		  write_string (prefix);
2285 		  write_string (prec);
2286 		}
2287 	    }
2288 	}
2289       break;
2290 
2291     case REAL_TYPE:
2292       if (type == float_type_node
2293 	  || type == java_float_type_node)
2294 	write_char ('f');
2295       else if (type == double_type_node
2296 	       || type == java_double_type_node)
2297 	write_char ('d');
2298       else if (type == long_double_type_node)
2299 	write_char ('e');
2300       else if (type == dfloat32_type_node)
2301 	write_string ("Df");
2302       else if (type == dfloat64_type_node)
2303 	write_string ("Dd");
2304       else if (type == dfloat128_type_node)
2305 	write_string ("De");
2306       else
2307 	gcc_unreachable ();
2308       break;
2309 
2310     case FIXED_POINT_TYPE:
2311       write_string ("DF");
2312       if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2313 	write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2314       if (type == fract_type_node
2315 	  || type == sat_fract_type_node
2316 	  || type == accum_type_node
2317 	  || type == sat_accum_type_node)
2318 	write_char ('i');
2319       else if (type == unsigned_fract_type_node
2320 	       || type == sat_unsigned_fract_type_node
2321 	       || type == unsigned_accum_type_node
2322 	       || type == sat_unsigned_accum_type_node)
2323 	write_char ('j');
2324       else if (type == short_fract_type_node
2325 	       || type == sat_short_fract_type_node
2326 	       || type == short_accum_type_node
2327 	       || type == sat_short_accum_type_node)
2328 	write_char ('s');
2329       else if (type == unsigned_short_fract_type_node
2330 	       || type == sat_unsigned_short_fract_type_node
2331 	       || type == unsigned_short_accum_type_node
2332 	       || type == sat_unsigned_short_accum_type_node)
2333 	write_char ('t');
2334       else if (type == long_fract_type_node
2335 	       || type == sat_long_fract_type_node
2336 	       || type == long_accum_type_node
2337 	       || type == sat_long_accum_type_node)
2338 	write_char ('l');
2339       else if (type == unsigned_long_fract_type_node
2340 	       || type == sat_unsigned_long_fract_type_node
2341 	       || type == unsigned_long_accum_type_node
2342 	       || type == sat_unsigned_long_accum_type_node)
2343 	write_char ('m');
2344       else if (type == long_long_fract_type_node
2345 	       || type == sat_long_long_fract_type_node
2346 	       || type == long_long_accum_type_node
2347 	       || type == sat_long_long_accum_type_node)
2348 	write_char ('x');
2349       else if (type == unsigned_long_long_fract_type_node
2350 	       || type == sat_unsigned_long_long_fract_type_node
2351 	       || type == unsigned_long_long_accum_type_node
2352 	       || type == sat_unsigned_long_long_accum_type_node)
2353 	write_char ('y');
2354       else
2355 	sorry ("mangling unknown fixed point type");
2356       write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2357       if (TYPE_SATURATING (type))
2358 	write_char ('s');
2359       else
2360 	write_char ('n');
2361       break;
2362 
2363     default:
2364       gcc_unreachable ();
2365     }
2366 }
2367 
2368 /* Non-terminal <function-type>.  NODE is a FUNCTION_TYPE or
2369    METHOD_TYPE.  The return type is mangled before the parameter
2370    types.
2371 
2372      <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E   */
2373 
2374 static void
write_function_type(const tree type)2375 write_function_type (const tree type)
2376 {
2377   MANGLE_TRACE_TREE ("function-type", type);
2378 
2379   /* For a pointer to member function, the function type may have
2380      cv-qualifiers, indicating the quals for the artificial 'this'
2381      parameter.  */
2382   if (TREE_CODE (type) == METHOD_TYPE)
2383     {
2384       /* The first parameter must be a POINTER_TYPE pointing to the
2385 	 `this' parameter.  */
2386       tree this_type = class_of_this_parm (type);
2387       write_CV_qualifiers_for_type (this_type);
2388     }
2389 
2390   write_char ('F');
2391   /* We don't track whether or not a type is `extern "C"'.  Note that
2392      you can have an `extern "C"' function that does not have
2393      `extern "C"' type, and vice versa:
2394 
2395        extern "C" typedef void function_t();
2396        function_t f; // f has C++ linkage, but its type is
2397 		     // `extern "C"'
2398 
2399        typedef void function_t();
2400        extern "C" function_t f; // Vice versa.
2401 
2402      See [dcl.link].  */
2403   write_bare_function_type (type, /*include_return_type_p=*/1,
2404 			    /*decl=*/NULL);
2405   if (FUNCTION_REF_QUALIFIED (type))
2406     {
2407       if (FUNCTION_RVALUE_QUALIFIED (type))
2408 	write_char ('O');
2409       else
2410 	write_char ('R');
2411     }
2412   write_char ('E');
2413 }
2414 
2415 /* Non-terminal <bare-function-type>.  TYPE is a FUNCTION_TYPE or
2416    METHOD_TYPE.  If INCLUDE_RETURN_TYPE is nonzero, the return value
2417    is mangled before the parameter types.  If non-NULL, DECL is
2418    FUNCTION_DECL for the function whose type is being emitted.
2419 
2420    If DECL is a member of a Java type, then a literal 'J'
2421    is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2422    were nonzero.
2423 
2424      <bare-function-type> ::= [J]</signature/ type>+  */
2425 
2426 static void
write_bare_function_type(const tree type,const int include_return_type_p,const tree decl)2427 write_bare_function_type (const tree type, const int include_return_type_p,
2428 			  const tree decl)
2429 {
2430   int java_method_p;
2431 
2432   MANGLE_TRACE_TREE ("bare-function-type", type);
2433 
2434   /* Detect Java methods and emit special encoding.  */
2435   if (decl != NULL
2436       && DECL_FUNCTION_MEMBER_P (decl)
2437       && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2438       && !DECL_CONSTRUCTOR_P (decl)
2439       && !DECL_DESTRUCTOR_P (decl)
2440       && !DECL_CONV_FN_P (decl))
2441     {
2442       java_method_p = 1;
2443       write_char ('J');
2444     }
2445   else
2446     {
2447       java_method_p = 0;
2448     }
2449 
2450   /* Mangle the return type, if requested.  */
2451   if (include_return_type_p || java_method_p)
2452     write_type (TREE_TYPE (type));
2453 
2454   /* Now mangle the types of the arguments.  */
2455   ++G.parm_depth;
2456   write_method_parms (TYPE_ARG_TYPES (type),
2457 		      TREE_CODE (type) == METHOD_TYPE,
2458 		      decl);
2459   --G.parm_depth;
2460 }
2461 
2462 /* Write the mangled representation of a method parameter list of
2463    types given in PARM_TYPES.  If METHOD_P is nonzero, the function is
2464    considered a non-static method, and the this parameter is omitted.
2465    If non-NULL, DECL is the FUNCTION_DECL for the function whose
2466    parameters are being emitted.  */
2467 
2468 static void
write_method_parms(tree parm_types,const int method_p,const tree decl)2469 write_method_parms (tree parm_types, const int method_p, const tree decl)
2470 {
2471   tree first_parm_type;
2472   tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2473 
2474   /* Assume this parameter type list is variable-length.  If it ends
2475      with a void type, then it's not.  */
2476   int varargs_p = 1;
2477 
2478   /* If this is a member function, skip the first arg, which is the
2479      this pointer.
2480        "Member functions do not encode the type of their implicit this
2481        parameter."
2482 
2483      Similarly, there's no need to mangle artificial parameters, like
2484      the VTT parameters for constructors and destructors.  */
2485   if (method_p)
2486     {
2487       parm_types = TREE_CHAIN (parm_types);
2488       parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2489 
2490       while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2491 	{
2492 	  parm_types = TREE_CHAIN (parm_types);
2493 	  parm_decl = DECL_CHAIN (parm_decl);
2494 	}
2495     }
2496 
2497   for (first_parm_type = parm_types;
2498        parm_types;
2499        parm_types = TREE_CHAIN (parm_types))
2500     {
2501       tree parm = TREE_VALUE (parm_types);
2502       if (parm == void_type_node)
2503 	{
2504 	  /* "Empty parameter lists, whether declared as () or
2505 	     conventionally as (void), are encoded with a void parameter
2506 	     (v)."  */
2507 	  if (parm_types == first_parm_type)
2508 	    write_type (parm);
2509 	  /* If the parm list is terminated with a void type, it's
2510 	     fixed-length.  */
2511 	  varargs_p = 0;
2512 	  /* A void type better be the last one.  */
2513 	  gcc_assert (TREE_CHAIN (parm_types) == NULL);
2514 	}
2515       else
2516 	write_type (parm);
2517     }
2518 
2519   if (varargs_p)
2520     /* <builtin-type> ::= z  # ellipsis  */
2521     write_char ('z');
2522 }
2523 
2524 /* <class-enum-type> ::= <name>  */
2525 
2526 static void
write_class_enum_type(const tree type)2527 write_class_enum_type (const tree type)
2528 {
2529   write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2530 }
2531 
2532 /* Non-terminal <template-args>.  ARGS is a TREE_VEC of template
2533    arguments.
2534 
2535      <template-args> ::= I <template-arg>* E  */
2536 
2537 static void
write_template_args(tree args)2538 write_template_args (tree args)
2539 {
2540   int i;
2541   int length = 0;
2542 
2543   MANGLE_TRACE_TREE ("template-args", args);
2544 
2545   write_char ('I');
2546 
2547   if (args)
2548     length = TREE_VEC_LENGTH (args);
2549 
2550   if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2551     {
2552       /* We have nested template args.  We want the innermost template
2553 	 argument list.  */
2554       args = TREE_VEC_ELT (args, length - 1);
2555       length = TREE_VEC_LENGTH (args);
2556     }
2557   for (i = 0; i < length; ++i)
2558     write_template_arg (TREE_VEC_ELT (args, i));
2559 
2560   write_char ('E');
2561 }
2562 
2563 /* Write out the
2564    <unqualified-name>
2565    <unqualified-name> <template-args>
2566    part of SCOPE_REF or COMPONENT_REF mangling.  */
2567 
2568 static void
write_member_name(tree member)2569 write_member_name (tree member)
2570 {
2571   if (identifier_p (member))
2572     write_unqualified_id (member);
2573   else if (DECL_P (member))
2574     write_unqualified_name (member);
2575   else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2576     {
2577       tree name = TREE_OPERAND (member, 0);
2578       if (TREE_CODE (name) == OVERLOAD)
2579 	name = OVL_FUNCTION (name);
2580       write_member_name (name);
2581       write_template_args (TREE_OPERAND (member, 1));
2582     }
2583   else
2584     write_expression (member);
2585 }
2586 
2587 /* <expression> ::= <unary operator-name> <expression>
2588 		::= <binary operator-name> <expression> <expression>
2589 		::= <expr-primary>
2590 
2591    <expr-primary> ::= <template-param>
2592 		  ::= L <type> <value number> E		# literal
2593 		  ::= L <mangled-name> E		# external name
2594 		  ::= st <type>				# sizeof
2595 		  ::= sr <type> <unqualified-name>	# dependent name
2596 		  ::= sr <type> <unqualified-name> <template-args> */
2597 
2598 static void
write_expression(tree expr)2599 write_expression (tree expr)
2600 {
2601   enum tree_code code = TREE_CODE (expr);
2602 
2603   /* Skip NOP_EXPRs.  They can occur when (say) a pointer argument
2604      is converted (via qualification conversions) to another
2605      type.  */
2606   while (TREE_CODE (expr) == NOP_EXPR
2607 	 /* Parentheses aren't mangled.  */
2608 	 || code == PAREN_EXPR
2609 	 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2610     {
2611       expr = TREE_OPERAND (expr, 0);
2612       code = TREE_CODE (expr);
2613     }
2614 
2615   if (code == BASELINK
2616       && (!type_unknown_p (expr)
2617 	  || !BASELINK_QUALIFIED_P (expr)))
2618     {
2619       expr = BASELINK_FUNCTIONS (expr);
2620       code = TREE_CODE (expr);
2621     }
2622 
2623   /* Handle pointers-to-members by making them look like expression
2624      nodes.  */
2625   if (code == PTRMEM_CST)
2626     {
2627       expr = build_nt (ADDR_EXPR,
2628 		       build_qualified_name (/*type=*/NULL_TREE,
2629 					     PTRMEM_CST_CLASS (expr),
2630 					     PTRMEM_CST_MEMBER (expr),
2631 					     /*template_p=*/false));
2632       code = TREE_CODE (expr);
2633     }
2634 
2635   /* Handle template parameters.  */
2636   if (code == TEMPLATE_TYPE_PARM
2637       || code == TEMPLATE_TEMPLATE_PARM
2638       || code == BOUND_TEMPLATE_TEMPLATE_PARM
2639       || code == TEMPLATE_PARM_INDEX)
2640     write_template_param (expr);
2641   /* Handle literals.  */
2642   else if (TREE_CODE_CLASS (code) == tcc_constant
2643 	   || (abi_version_at_least (2) && code == CONST_DECL))
2644     write_template_arg_literal (expr);
2645   else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2646     {
2647       gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2648       write_string ("fpT");
2649     }
2650   else if (code == PARM_DECL)
2651     {
2652       /* A function parameter used in a late-specified return type.  */
2653       int index = DECL_PARM_INDEX (expr);
2654       int level = DECL_PARM_LEVEL (expr);
2655       int delta = G.parm_depth - level + 1;
2656       gcc_assert (index >= 1);
2657       write_char ('f');
2658       if (delta != 0)
2659 	{
2660 	  if (abi_version_at_least (5))
2661 	    {
2662 	      /* Let L be the number of function prototype scopes from the
2663 		 innermost one (in which the parameter reference occurs) up
2664 		 to (and including) the one containing the declaration of
2665 		 the referenced parameter.  If the parameter declaration
2666 		 clause of the innermost function prototype scope has been
2667 		 completely seen, it is not counted (in that case -- which
2668 		 is perhaps the most common -- L can be zero).  */
2669 	      write_char ('L');
2670 	      write_unsigned_number (delta - 1);
2671 	    }
2672 	  else
2673 	    G.need_abi_warning = true;
2674 	}
2675       write_char ('p');
2676       write_compact_number (index - 1);
2677     }
2678   else if (DECL_P (expr))
2679     {
2680       /* G++ 3.2 incorrectly mangled non-type template arguments of
2681 	 enumeration type using their names.  */
2682       if (code == CONST_DECL)
2683 	G.need_abi_warning = 1;
2684       write_char ('L');
2685       write_mangled_name (expr, false);
2686       write_char ('E');
2687     }
2688   else if (TREE_CODE (expr) == SIZEOF_EXPR
2689 	   && SIZEOF_EXPR_TYPE_P (expr))
2690     {
2691       write_string ("st");
2692       write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2693     }
2694   else if (TREE_CODE (expr) == SIZEOF_EXPR
2695 	   && TYPE_P (TREE_OPERAND (expr, 0)))
2696     {
2697       write_string ("st");
2698       write_type (TREE_OPERAND (expr, 0));
2699     }
2700   else if (TREE_CODE (expr) == ALIGNOF_EXPR
2701 	   && TYPE_P (TREE_OPERAND (expr, 0)))
2702     {
2703       write_string ("at");
2704       write_type (TREE_OPERAND (expr, 0));
2705     }
2706   else if (code == SCOPE_REF
2707 	   || code == BASELINK)
2708     {
2709       tree scope, member;
2710       if (code == SCOPE_REF)
2711 	{
2712 	  scope = TREE_OPERAND (expr, 0);
2713 	  member = TREE_OPERAND (expr, 1);
2714 	}
2715       else
2716 	{
2717 	  scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2718 	  member = BASELINK_FUNCTIONS (expr);
2719 	}
2720 
2721       if (!abi_version_at_least (2) && DECL_P (member))
2722 	{
2723 	  write_string ("sr");
2724 	  write_type (scope);
2725 	  /* G++ 3.2 incorrectly put out both the "sr" code and
2726 	     the nested name of the qualified name.  */
2727 	  G.need_abi_warning = 1;
2728 	  write_encoding (member);
2729 	}
2730 
2731       /* If the MEMBER is a real declaration, then the qualifying
2732 	 scope was not dependent.  Ideally, we would not have a
2733 	 SCOPE_REF in those cases, but sometimes we do.  If the second
2734 	 argument is a DECL, then the name must not have been
2735 	 dependent.  */
2736       else if (DECL_P (member))
2737 	write_expression (member);
2738       else
2739 	{
2740 	  write_string ("sr");
2741 	  write_type (scope);
2742 	  write_member_name (member);
2743 	}
2744     }
2745   else if (INDIRECT_REF_P (expr)
2746 	   && TREE_TYPE (TREE_OPERAND (expr, 0))
2747 	   && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2748     {
2749       write_expression (TREE_OPERAND (expr, 0));
2750     }
2751   else if (identifier_p (expr))
2752     {
2753       /* An operator name appearing as a dependent name needs to be
2754 	 specially marked to disambiguate between a use of the operator
2755 	 name and a use of the operator in an expression.  */
2756       if (IDENTIFIER_OPNAME_P (expr))
2757 	write_string ("on");
2758       write_unqualified_id (expr);
2759     }
2760   else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2761     {
2762       tree fn = TREE_OPERAND (expr, 0);
2763       if (is_overloaded_fn (fn))
2764 	fn = DECL_NAME (get_first_fn (fn));
2765       if (IDENTIFIER_OPNAME_P (fn))
2766 	write_string ("on");
2767       write_unqualified_id (fn);
2768       write_template_args (TREE_OPERAND (expr, 1));
2769     }
2770   else if (TREE_CODE (expr) == MODOP_EXPR)
2771     {
2772       enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2773       const char *name = (assignment_operator_name_info[(int) subop]
2774 			  .mangled_name);
2775       write_string (name);
2776       write_expression (TREE_OPERAND (expr, 0));
2777       write_expression (TREE_OPERAND (expr, 2));
2778     }
2779   else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2780     {
2781       /* ::= [gs] nw <expression>* _ <type> E
2782 	 ::= [gs] nw <expression>* _ <type> <initializer>
2783 	 ::= [gs] na <expression>* _ <type> E
2784 	 ::= [gs] na <expression>* _ <type> <initializer>
2785 	 <initializer> ::= pi <expression>* E  */
2786       tree placement = TREE_OPERAND (expr, 0);
2787       tree type = TREE_OPERAND (expr, 1);
2788       tree nelts = TREE_OPERAND (expr, 2);
2789       tree init = TREE_OPERAND (expr, 3);
2790       tree t;
2791 
2792       gcc_assert (code == NEW_EXPR);
2793       if (TREE_OPERAND (expr, 2))
2794 	code = VEC_NEW_EXPR;
2795 
2796       if (NEW_EXPR_USE_GLOBAL (expr))
2797 	write_string ("gs");
2798 
2799       write_string (operator_name_info[(int) code].mangled_name);
2800 
2801       for (t = placement; t; t = TREE_CHAIN (t))
2802 	write_expression (TREE_VALUE (t));
2803 
2804       write_char ('_');
2805 
2806       if (nelts)
2807 	{
2808 	  tree domain;
2809 	  ++processing_template_decl;
2810 	  domain = compute_array_index_type (NULL_TREE, nelts,
2811 					     tf_warning_or_error);
2812 	  type = build_cplus_array_type (type, domain);
2813 	  --processing_template_decl;
2814 	}
2815       write_type (type);
2816 
2817       if (init && TREE_CODE (init) == TREE_LIST
2818 	  && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
2819 	  && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
2820 	write_expression (TREE_VALUE (init));
2821       else
2822 	{
2823 	  if (init)
2824 	    write_string ("pi");
2825 	  if (init && init != void_zero_node)
2826 	    for (t = init; t; t = TREE_CHAIN (t))
2827 	      write_expression (TREE_VALUE (t));
2828 	  write_char ('E');
2829 	}
2830     }
2831   else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2832     {
2833       gcc_assert (code == DELETE_EXPR);
2834       if (DELETE_EXPR_USE_VEC (expr))
2835 	code = VEC_DELETE_EXPR;
2836 
2837       if (DELETE_EXPR_USE_GLOBAL (expr))
2838 	write_string ("gs");
2839 
2840       write_string (operator_name_info[(int) code].mangled_name);
2841 
2842       write_expression (TREE_OPERAND (expr, 0));
2843     }
2844   else if (code == THROW_EXPR)
2845     {
2846       tree op = TREE_OPERAND (expr, 0);
2847       if (op)
2848 	{
2849 	  write_string ("tw");
2850 	  write_expression (op);
2851 	}
2852       else
2853 	write_string ("tr");
2854     }
2855   else if (code == CONSTRUCTOR)
2856     {
2857       vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2858       unsigned i; tree val;
2859 
2860       if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2861 	write_string ("il");
2862       else
2863 	{
2864 	  write_string ("tl");
2865 	  write_type (TREE_TYPE (expr));
2866 	}
2867       FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2868 	write_expression (val);
2869       write_char ('E');
2870     }
2871   else if (dependent_name (expr))
2872     {
2873       write_unqualified_id (dependent_name (expr));
2874     }
2875   else
2876     {
2877       int i, len;
2878       const char *name;
2879 
2880       /* When we bind a variable or function to a non-type template
2881 	 argument with reference type, we create an ADDR_EXPR to show
2882 	 the fact that the entity's address has been taken.  But, we
2883 	 don't actually want to output a mangling code for the `&'.  */
2884       if (TREE_CODE (expr) == ADDR_EXPR
2885 	  && TREE_TYPE (expr)
2886 	  && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2887 	{
2888 	  expr = TREE_OPERAND (expr, 0);
2889 	  if (DECL_P (expr))
2890 	    {
2891 	      write_expression (expr);
2892 	      return;
2893 	    }
2894 
2895 	  code = TREE_CODE (expr);
2896 	}
2897 
2898       if (code == COMPONENT_REF)
2899 	{
2900 	  tree ob = TREE_OPERAND (expr, 0);
2901 
2902 	  if (TREE_CODE (ob) == ARROW_EXPR)
2903 	    {
2904 	      write_string (operator_name_info[(int)code].mangled_name);
2905 	      ob = TREE_OPERAND (ob, 0);
2906 	    }
2907 	  else
2908 	    write_string ("dt");
2909 
2910 	  write_expression (ob);
2911 	  write_member_name (TREE_OPERAND (expr, 1));
2912 	  return;
2913 	}
2914 
2915       /* If it wasn't any of those, recursively expand the expression.  */
2916       name = operator_name_info[(int) code].mangled_name;
2917 
2918       /* We used to mangle const_cast and static_cast like a C cast.  */
2919       if (!abi_version_at_least (6)
2920 	  && (code == CONST_CAST_EXPR
2921 	      || code == STATIC_CAST_EXPR))
2922 	{
2923 	  name = operator_name_info[CAST_EXPR].mangled_name;
2924 	  G.need_abi_warning = 1;
2925 	}
2926 
2927       if (name == NULL)
2928 	{
2929 	  switch (code)
2930 	    {
2931 	    case TRAIT_EXPR:
2932 	      error ("use of built-in trait %qE in function signature; "
2933 		     "use library traits instead", expr);
2934 	      break;
2935 
2936 	    default:
2937 	      sorry ("mangling %C", code);
2938 	      break;
2939 	    }
2940 	  return;
2941 	}
2942       else
2943 	write_string (name);
2944 
2945       switch (code)
2946 	{
2947 	case CALL_EXPR:
2948 	  {
2949 	    tree fn = CALL_EXPR_FN (expr);
2950 
2951 	    if (TREE_CODE (fn) == ADDR_EXPR)
2952 	      fn = TREE_OPERAND (fn, 0);
2953 
2954 	    /* Mangle a dependent name as the name, not whatever happens to
2955 	       be the first function in the overload set.  */
2956 	    if ((TREE_CODE (fn) == FUNCTION_DECL
2957 		 || TREE_CODE (fn) == OVERLOAD)
2958 		&& type_dependent_expression_p_push (expr))
2959 	      fn = DECL_NAME (get_first_fn (fn));
2960 
2961 	    write_expression (fn);
2962 	  }
2963 
2964 	  for (i = 0; i < call_expr_nargs (expr); ++i)
2965 	    write_expression (CALL_EXPR_ARG (expr, i));
2966 	  write_char ('E');
2967 	  break;
2968 
2969 	case CAST_EXPR:
2970 	  write_type (TREE_TYPE (expr));
2971 	  if (list_length (TREE_OPERAND (expr, 0)) == 1)
2972 	    write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2973 	  else
2974 	    {
2975 	      tree args = TREE_OPERAND (expr, 0);
2976 	      write_char ('_');
2977 	      for (; args; args = TREE_CHAIN (args))
2978 		write_expression (TREE_VALUE (args));
2979 	      write_char ('E');
2980 	    }
2981 	  break;
2982 
2983 	case DYNAMIC_CAST_EXPR:
2984 	case REINTERPRET_CAST_EXPR:
2985 	case STATIC_CAST_EXPR:
2986 	case CONST_CAST_EXPR:
2987 	  write_type (TREE_TYPE (expr));
2988 	  write_expression (TREE_OPERAND (expr, 0));
2989 	  break;
2990 
2991 	case PREINCREMENT_EXPR:
2992 	case PREDECREMENT_EXPR:
2993 	  if (abi_version_at_least (6))
2994 	    write_char ('_');
2995 	  else
2996 	    G.need_abi_warning = 1;
2997 	  /* Fall through.  */
2998 
2999 	default:
3000 	  /* In the middle-end, some expressions have more operands than
3001 	     they do in templates (and mangling).  */
3002 	  len = cp_tree_operand_length (expr);
3003 
3004 	  for (i = 0; i < len; ++i)
3005 	    {
3006 	      tree operand = TREE_OPERAND (expr, i);
3007 	      /* As a GNU extension, the middle operand of a
3008 		 conditional may be omitted.  Since expression
3009 		 manglings are supposed to represent the input token
3010 		 stream, there's no good way to mangle such an
3011 		 expression without extending the C++ ABI.  */
3012 	      if (code == COND_EXPR && i == 1 && !operand)
3013 		{
3014 		  error ("omitted middle operand to %<?:%> operand "
3015 			 "cannot be mangled");
3016 		  continue;
3017 		}
3018 	      write_expression (operand);
3019 	    }
3020 	}
3021     }
3022 }
3023 
3024 /* Literal subcase of non-terminal <template-arg>.
3025 
3026      "Literal arguments, e.g. "A<42L>", are encoded with their type
3027      and value. Negative integer values are preceded with "n"; for
3028      example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3029      encoded as 0, true as 1."  */
3030 
3031 static void
write_template_arg_literal(const tree value)3032 write_template_arg_literal (const tree value)
3033 {
3034   write_char ('L');
3035   write_type (TREE_TYPE (value));
3036 
3037   /* Write a null member pointer value as (type)0, regardless of its
3038      real representation.  */
3039   if (null_member_pointer_value_p (value))
3040     write_integer_cst (integer_zero_node);
3041   else
3042     switch (TREE_CODE (value))
3043       {
3044       case CONST_DECL:
3045 	write_integer_cst (DECL_INITIAL (value));
3046 	break;
3047 
3048       case INTEGER_CST:
3049 	gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3050 		    || integer_zerop (value) || integer_onep (value));
3051 	write_integer_cst (value);
3052 	break;
3053 
3054       case REAL_CST:
3055 	write_real_cst (value);
3056 	break;
3057 
3058       case COMPLEX_CST:
3059 	if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3060 	    && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3061 	  {
3062 	    write_integer_cst (TREE_REALPART (value));
3063 	    write_char ('_');
3064 	    write_integer_cst (TREE_IMAGPART (value));
3065 	  }
3066 	else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3067 		 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3068 	  {
3069 	    write_real_cst (TREE_REALPART (value));
3070 	    write_char ('_');
3071 	    write_real_cst (TREE_IMAGPART (value));
3072 	  }
3073 	else
3074 	  gcc_unreachable ();
3075 	break;
3076 
3077       case STRING_CST:
3078 	sorry ("string literal in function template signature");
3079 	break;
3080 
3081       default:
3082 	gcc_unreachable ();
3083       }
3084 
3085   write_char ('E');
3086 }
3087 
3088 /* Non-terminal <template-arg>.
3089 
3090      <template-arg> ::= <type>				# type
3091 		    ::= L <type> </value/ number> E	# literal
3092 		    ::= LZ <name> E			# external name
3093 		    ::= X <expression> E		# expression  */
3094 
3095 static void
write_template_arg(tree node)3096 write_template_arg (tree node)
3097 {
3098   enum tree_code code = TREE_CODE (node);
3099 
3100   MANGLE_TRACE_TREE ("template-arg", node);
3101 
3102   /* A template template parameter's argument list contains TREE_LIST
3103      nodes of which the value field is the actual argument.  */
3104   if (code == TREE_LIST)
3105     {
3106       node = TREE_VALUE (node);
3107       /* If it's a decl, deal with its type instead.  */
3108       if (DECL_P (node))
3109 	{
3110 	  node = TREE_TYPE (node);
3111 	  code = TREE_CODE (node);
3112 	}
3113     }
3114 
3115   if (TREE_CODE (node) == NOP_EXPR
3116       && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3117     {
3118       /* Template parameters can be of reference type. To maintain
3119 	 internal consistency, such arguments use a conversion from
3120 	 address of object to reference type.  */
3121       gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3122       if (abi_version_at_least (2))
3123 	node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3124       else
3125 	G.need_abi_warning = 1;
3126     }
3127 
3128   if (TREE_CODE (node) == BASELINK
3129       && !type_unknown_p (node))
3130     {
3131       if (abi_version_at_least (6))
3132 	node = BASELINK_FUNCTIONS (node);
3133       else
3134 	/* We wrongly wrapped a class-scope function in X/E.  */
3135 	G.need_abi_warning = 1;
3136     }
3137 
3138   if (ARGUMENT_PACK_P (node))
3139     {
3140       /* Expand the template argument pack. */
3141       tree args = ARGUMENT_PACK_ARGS (node);
3142       int i, length = TREE_VEC_LENGTH (args);
3143       if (abi_version_at_least (6))
3144 	write_char ('J');
3145       else
3146 	{
3147 	  write_char ('I');
3148 	  G.need_abi_warning = 1;
3149 	}
3150       for (i = 0; i < length; ++i)
3151         write_template_arg (TREE_VEC_ELT (args, i));
3152       write_char ('E');
3153     }
3154   else if (TYPE_P (node))
3155     write_type (node);
3156   else if (code == TEMPLATE_DECL)
3157     /* A template appearing as a template arg is a template template arg.  */
3158     write_template_template_arg (node);
3159   else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3160 	   || (abi_version_at_least (2) && code == CONST_DECL)
3161 	   || null_member_pointer_value_p (node))
3162     write_template_arg_literal (node);
3163   else if (DECL_P (node))
3164     {
3165       /* Until ABI version 2, non-type template arguments of
3166 	 enumeration type were mangled using their names.  */
3167       if (code == CONST_DECL && !abi_version_at_least (2))
3168 	G.need_abi_warning = 1;
3169       write_char ('L');
3170       /* Until ABI version 3, the underscore before the mangled name
3171 	 was incorrectly omitted.  */
3172       if (!abi_version_at_least (3))
3173 	{
3174 	  G.need_abi_warning = 1;
3175 	  write_char ('Z');
3176 	}
3177       else
3178 	write_string ("_Z");
3179       write_encoding (node);
3180       write_char ('E');
3181     }
3182   else
3183     {
3184       /* Template arguments may be expressions.  */
3185       write_char ('X');
3186       write_expression (node);
3187       write_char ('E');
3188     }
3189 }
3190 
3191 /*  <template-template-arg>
3192 			::= <name>
3193 			::= <substitution>  */
3194 
3195 static void
write_template_template_arg(const tree decl)3196 write_template_template_arg (const tree decl)
3197 {
3198   MANGLE_TRACE_TREE ("template-template-arg", decl);
3199 
3200   if (find_substitution (decl))
3201     return;
3202   write_name (decl, /*ignore_local_scope=*/0);
3203   add_substitution (decl);
3204 }
3205 
3206 
3207 /* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.
3208 
3209      <array-type> ::= A [</dimension/ number>] _ </element/ type>
3210 		  ::= A <expression> _ </element/ type>
3211 
3212      "Array types encode the dimension (number of elements) and the
3213      element type. For variable length arrays, the dimension (but not
3214      the '_' separator) is omitted."  */
3215 
3216 static void
write_array_type(const tree type)3217 write_array_type (const tree type)
3218 {
3219   write_char ('A');
3220   if (TYPE_DOMAIN (type))
3221     {
3222       tree index_type;
3223       tree max;
3224 
3225       index_type = TYPE_DOMAIN (type);
3226       /* The INDEX_TYPE gives the upper and lower bounds of the
3227 	 array.  */
3228       max = TYPE_MAX_VALUE (index_type);
3229       if (TREE_CODE (max) == INTEGER_CST)
3230 	{
3231 	  /* The ABI specifies that we should mangle the number of
3232 	     elements in the array, not the largest allowed index.  */
3233 	  double_int dmax = tree_to_double_int (max) + double_int_one;
3234 	  /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3235 	     number of elements as zero.  */
3236 	  dmax = dmax.zext (TYPE_PRECISION (TREE_TYPE (max)));
3237 	  gcc_assert (dmax.fits_uhwi ());
3238 	  write_unsigned_number (dmax.low);
3239 	}
3240       else
3241 	{
3242 	  max = TREE_OPERAND (max, 0);
3243 	  if (!abi_version_at_least (2))
3244 	    {
3245 	      /* value_dependent_expression_p presumes nothing is
3246 		 dependent when PROCESSING_TEMPLATE_DECL is zero.  */
3247 	      ++processing_template_decl;
3248 	      if (!value_dependent_expression_p (max))
3249 		G.need_abi_warning = 1;
3250 	      --processing_template_decl;
3251 	    }
3252 	  write_expression (max);
3253 	}
3254 
3255     }
3256   write_char ('_');
3257   write_type (TREE_TYPE (type));
3258 }
3259 
3260 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3261    variables.  TYPE is a pointer-to-member POINTER_TYPE.
3262 
3263      <pointer-to-member-type> ::= M </class/ type> </member/ type>  */
3264 
3265 static void
write_pointer_to_member_type(const tree type)3266 write_pointer_to_member_type (const tree type)
3267 {
3268   write_char ('M');
3269   write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3270   write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3271 }
3272 
3273 /* Non-terminal <template-param>.  PARM is a TEMPLATE_TYPE_PARM,
3274    TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3275    TEMPLATE_PARM_INDEX.
3276 
3277      <template-param> ::= T </parameter/ number> _  */
3278 
3279 static void
write_template_param(const tree parm)3280 write_template_param (const tree parm)
3281 {
3282   int parm_index;
3283 
3284   MANGLE_TRACE_TREE ("template-parm", parm);
3285 
3286   switch (TREE_CODE (parm))
3287     {
3288     case TEMPLATE_TYPE_PARM:
3289     case TEMPLATE_TEMPLATE_PARM:
3290     case BOUND_TEMPLATE_TEMPLATE_PARM:
3291       parm_index = TEMPLATE_TYPE_IDX (parm);
3292       break;
3293 
3294     case TEMPLATE_PARM_INDEX:
3295       parm_index = TEMPLATE_PARM_IDX (parm);
3296       break;
3297 
3298     default:
3299       gcc_unreachable ();
3300     }
3301 
3302   write_char ('T');
3303   /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3304      earliest template param denoted by `_'.  */
3305   write_compact_number (parm_index);
3306 }
3307 
3308 /*  <template-template-param>
3309 			::= <template-param>
3310 			::= <substitution>  */
3311 
3312 static void
write_template_template_param(const tree parm)3313 write_template_template_param (const tree parm)
3314 {
3315   tree templ = NULL_TREE;
3316 
3317   /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3318      template template parameter.  The substitution candidate here is
3319      only the template.  */
3320   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3321     {
3322       templ
3323 	= TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3324       if (find_substitution (templ))
3325 	return;
3326     }
3327 
3328   /* <template-param> encodes only the template parameter position,
3329      not its template arguments, which is fine here.  */
3330   write_template_param (parm);
3331   if (templ)
3332     add_substitution (templ);
3333 }
3334 
3335 /* Non-terminal <substitution>.
3336 
3337       <substitution> ::= S <seq-id> _
3338 		     ::= S_  */
3339 
3340 static void
write_substitution(const int seq_id)3341 write_substitution (const int seq_id)
3342 {
3343   MANGLE_TRACE ("substitution", "");
3344 
3345   write_char ('S');
3346   if (seq_id > 0)
3347     write_number (seq_id - 1, /*unsigned=*/1, 36);
3348   write_char ('_');
3349 }
3350 
3351 /* Start mangling ENTITY.  */
3352 
3353 static inline void
start_mangling(const tree entity)3354 start_mangling (const tree entity)
3355 {
3356   G.entity = entity;
3357   G.need_abi_warning = false;
3358   obstack_free (&name_obstack, name_base);
3359   mangle_obstack = &name_obstack;
3360   name_base = obstack_alloc (&name_obstack, 0);
3361 }
3362 
3363 /* Done with mangling. If WARN is true, and the name of G.entity will
3364    be mangled differently in a future version of the ABI, issue a
3365    warning.  */
3366 
3367 static void
finish_mangling_internal(const bool warn)3368 finish_mangling_internal (const bool warn)
3369 {
3370   if (warn_abi && warn && G.need_abi_warning)
3371     warning (OPT_Wabi, "the mangled name of %qD will change in a future "
3372 	     "version of GCC",
3373 	     G.entity);
3374 
3375   /* Clear all the substitutions.  */
3376   vec_safe_truncate (G.substitutions, 0);
3377 
3378   /* Null-terminate the string.  */
3379   write_char ('\0');
3380 }
3381 
3382 
3383 /* Like finish_mangling_internal, but return the mangled string.  */
3384 
3385 static inline const char *
finish_mangling(const bool warn)3386 finish_mangling (const bool warn)
3387 {
3388   finish_mangling_internal (warn);
3389   return (const char *) obstack_finish (mangle_obstack);
3390 }
3391 
3392 /* Like finish_mangling_internal, but return an identifier.  */
3393 
3394 static tree
finish_mangling_get_identifier(const bool warn)3395 finish_mangling_get_identifier (const bool warn)
3396 {
3397   finish_mangling_internal (warn);
3398   /* Don't obstack_finish here, and the next start_mangling will
3399      remove the identifier.  */
3400   return get_identifier ((const char *) obstack_base (mangle_obstack));
3401 }
3402 
3403 /* Initialize data structures for mangling.  */
3404 
3405 void
init_mangle(void)3406 init_mangle (void)
3407 {
3408   gcc_obstack_init (&name_obstack);
3409   name_base = obstack_alloc (&name_obstack, 0);
3410   vec_alloc (G.substitutions, 0);
3411 
3412   /* Cache these identifiers for quick comparison when checking for
3413      standard substitutions.  */
3414   subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3415   subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3416   subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3417   subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3418   subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3419   subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3420 }
3421 
3422 /* Generate the mangled name of DECL.  */
3423 
3424 static tree
mangle_decl_string(const tree decl)3425 mangle_decl_string (const tree decl)
3426 {
3427   tree result;
3428   location_t saved_loc = input_location;
3429   tree saved_fn = NULL_TREE;
3430   bool template_p = false;
3431 
3432   /* We shouldn't be trying to mangle an uninstantiated template.  */
3433   gcc_assert (!type_dependent_expression_p (decl));
3434 
3435   if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3436     {
3437       struct tinst_level *tl = current_instantiation ();
3438       if ((!tl || tl->decl != decl)
3439 	  && push_tinst_level (decl))
3440 	{
3441 	  template_p = true;
3442 	  saved_fn = current_function_decl;
3443 	  current_function_decl = NULL_TREE;
3444 	}
3445     }
3446   input_location = DECL_SOURCE_LOCATION (decl);
3447 
3448   start_mangling (decl);
3449 
3450   if (TREE_CODE (decl) == TYPE_DECL)
3451     write_type (TREE_TYPE (decl));
3452   else
3453     write_mangled_name (decl, true);
3454 
3455   result = finish_mangling_get_identifier (/*warn=*/true);
3456   if (DEBUG_MANGLE)
3457     fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3458 	     IDENTIFIER_POINTER (result));
3459 
3460   if (template_p)
3461     {
3462       pop_tinst_level ();
3463       current_function_decl = saved_fn;
3464     }
3465   input_location = saved_loc;
3466 
3467   return result;
3468 }
3469 
3470 /* Return an identifier for the external mangled name of DECL.  */
3471 
3472 static tree
get_mangled_id(tree decl)3473 get_mangled_id (tree decl)
3474 {
3475   tree id = mangle_decl_string (decl);
3476   return targetm.mangle_decl_assembler_name (decl, id);
3477 }
3478 
3479 /* Create an identifier for the external mangled name of DECL.  */
3480 
3481 void
mangle_decl(const tree decl)3482 mangle_decl (const tree decl)
3483 {
3484   tree id;
3485   bool dep;
3486 
3487   /* Don't bother mangling uninstantiated templates.  */
3488   ++processing_template_decl;
3489   if (TREE_CODE (decl) == TYPE_DECL)
3490     dep = dependent_type_p (TREE_TYPE (decl));
3491   else
3492     dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3493 	   && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3494   --processing_template_decl;
3495   if (dep)
3496     return;
3497 
3498   id = get_mangled_id (decl);
3499   SET_DECL_ASSEMBLER_NAME (decl, id);
3500 
3501   if (G.need_abi_warning
3502       /* Don't do this for a fake symbol we aren't going to emit anyway.  */
3503       && TREE_CODE (decl) != TYPE_DECL
3504       && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3505       && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3506     {
3507 #ifdef ASM_OUTPUT_DEF
3508       /* If the mangling will change in the future, emit an alias with the
3509 	 future mangled name for forward-compatibility.  */
3510       int save_ver;
3511       tree id2, alias;
3512 #endif
3513 
3514       SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3515       if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3516 	inform (DECL_SOURCE_LOCATION (decl), "-fabi-version=6 (or =0) "
3517 		"avoids this error with a change in mangling");
3518 
3519 #ifdef ASM_OUTPUT_DEF
3520       save_ver = flag_abi_version;
3521       flag_abi_version = 0;
3522       id2 = mangle_decl_string (decl);
3523       id2 = targetm.mangle_decl_assembler_name (decl, id2);
3524       flag_abi_version = save_ver;
3525 
3526       alias = make_alias_for (decl, id2);
3527       DECL_IGNORED_P (alias) = 1;
3528       TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3529       DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3530       if (vague_linkage_p (decl))
3531 	DECL_WEAK (alias) = 1;
3532       if (TREE_CODE (decl) == FUNCTION_DECL)
3533 	cgraph_same_body_alias (cgraph_get_create_node (decl), alias, decl);
3534       else
3535 	varpool_extra_name_alias (alias, decl);
3536 #endif
3537     }
3538 }
3539 
3540 /* Generate the mangled representation of TYPE.  */
3541 
3542 const char *
mangle_type_string(const tree type)3543 mangle_type_string (const tree type)
3544 {
3545   const char *result;
3546 
3547   start_mangling (type);
3548   write_type (type);
3549   result = finish_mangling (/*warn=*/false);
3550   if (DEBUG_MANGLE)
3551     fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3552   return result;
3553 }
3554 
3555 /* Create an identifier for the mangled name of a special component
3556    for belonging to TYPE.  CODE is the ABI-specified code for this
3557    component.  */
3558 
3559 static tree
mangle_special_for_type(const tree type,const char * code)3560 mangle_special_for_type (const tree type, const char *code)
3561 {
3562   tree result;
3563 
3564   /* We don't have an actual decl here for the special component, so
3565      we can't just process the <encoded-name>.  Instead, fake it.  */
3566   start_mangling (type);
3567 
3568   /* Start the mangling.  */
3569   write_string ("_Z");
3570   write_string (code);
3571 
3572   /* Add the type.  */
3573   write_type (type);
3574   result = finish_mangling_get_identifier (/*warn=*/false);
3575 
3576   if (DEBUG_MANGLE)
3577     fprintf (stderr, "mangle_special_for_type = %s\n\n",
3578 	     IDENTIFIER_POINTER (result));
3579 
3580   return result;
3581 }
3582 
3583 /* Create an identifier for the mangled representation of the typeinfo
3584    structure for TYPE.  */
3585 
3586 tree
mangle_typeinfo_for_type(const tree type)3587 mangle_typeinfo_for_type (const tree type)
3588 {
3589   return mangle_special_for_type (type, "TI");
3590 }
3591 
3592 /* Create an identifier for the mangled name of the NTBS containing
3593    the mangled name of TYPE.  */
3594 
3595 tree
mangle_typeinfo_string_for_type(const tree type)3596 mangle_typeinfo_string_for_type (const tree type)
3597 {
3598   return mangle_special_for_type (type, "TS");
3599 }
3600 
3601 /* Create an identifier for the mangled name of the vtable for TYPE.  */
3602 
3603 tree
mangle_vtbl_for_type(const tree type)3604 mangle_vtbl_for_type (const tree type)
3605 {
3606   return mangle_special_for_type (type, "TV");
3607 }
3608 
3609 /* Returns an identifier for the mangled name of the VTT for TYPE.  */
3610 
3611 tree
mangle_vtt_for_type(const tree type)3612 mangle_vtt_for_type (const tree type)
3613 {
3614   return mangle_special_for_type (type, "TT");
3615 }
3616 
3617 /* Return an identifier for a construction vtable group.  TYPE is
3618    the most derived class in the hierarchy; BINFO is the base
3619    subobject for which this construction vtable group will be used.
3620 
3621    This mangling isn't part of the ABI specification; in the ABI
3622    specification, the vtable group is dumped in the same COMDAT as the
3623    main vtable, and is referenced only from that vtable, so it doesn't
3624    need an external name.  For binary formats without COMDAT sections,
3625    though, we need external names for the vtable groups.
3626 
3627    We use the production
3628 
3629     <special-name> ::= CT <type> <offset number> _ <base type>  */
3630 
3631 tree
mangle_ctor_vtbl_for_type(const tree type,const tree binfo)3632 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3633 {
3634   tree result;
3635 
3636   start_mangling (type);
3637 
3638   write_string ("_Z");
3639   write_string ("TC");
3640   write_type (type);
3641   write_integer_cst (BINFO_OFFSET (binfo));
3642   write_char ('_');
3643   write_type (BINFO_TYPE (binfo));
3644 
3645   result = finish_mangling_get_identifier (/*warn=*/false);
3646   if (DEBUG_MANGLE)
3647     fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3648 	     IDENTIFIER_POINTER (result));
3649   return result;
3650 }
3651 
3652 /* Mangle a this pointer or result pointer adjustment.
3653 
3654    <call-offset> ::= h <fixed offset number> _
3655 		 ::= v <fixed offset number> _ <virtual offset number> _ */
3656 
3657 static void
mangle_call_offset(const tree fixed_offset,const tree virtual_offset)3658 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3659 {
3660   write_char (virtual_offset ? 'v' : 'h');
3661 
3662   /* For either flavor, write the fixed offset.  */
3663   write_integer_cst (fixed_offset);
3664   write_char ('_');
3665 
3666   /* For a virtual thunk, add the virtual offset.  */
3667   if (virtual_offset)
3668     {
3669       write_integer_cst (virtual_offset);
3670       write_char ('_');
3671     }
3672 }
3673 
3674 /* Return an identifier for the mangled name of a this-adjusting or
3675    covariant thunk to FN_DECL.  FIXED_OFFSET is the initial adjustment
3676    to this used to find the vptr.  If VIRTUAL_OFFSET is non-NULL, this
3677    is a virtual thunk, and it is the vtbl offset in
3678    bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3679    zero for a covariant thunk. Note, that FN_DECL might be a covariant
3680    thunk itself. A covariant thunk name always includes the adjustment
3681    for the this pointer, even if there is none.
3682 
3683    <special-name> ::= T <call-offset> <base encoding>
3684 		  ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3685 					<base encoding>  */
3686 
3687 tree
mangle_thunk(tree fn_decl,const int this_adjusting,tree fixed_offset,tree virtual_offset)3688 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3689 	      tree virtual_offset)
3690 {
3691   tree result;
3692 
3693   start_mangling (fn_decl);
3694 
3695   write_string ("_Z");
3696   write_char ('T');
3697 
3698   if (!this_adjusting)
3699     {
3700       /* Covariant thunk with no this adjustment */
3701       write_char ('c');
3702       mangle_call_offset (integer_zero_node, NULL_TREE);
3703       mangle_call_offset (fixed_offset, virtual_offset);
3704     }
3705   else if (!DECL_THUNK_P (fn_decl))
3706     /* Plain this adjusting thunk.  */
3707     mangle_call_offset (fixed_offset, virtual_offset);
3708   else
3709     {
3710       /* This adjusting thunk to covariant thunk.  */
3711       write_char ('c');
3712       mangle_call_offset (fixed_offset, virtual_offset);
3713       fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3714       virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3715       if (virtual_offset)
3716 	virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3717       mangle_call_offset (fixed_offset, virtual_offset);
3718       fn_decl = THUNK_TARGET (fn_decl);
3719     }
3720 
3721   /* Scoped name.  */
3722   write_encoding (fn_decl);
3723 
3724   result = finish_mangling_get_identifier (/*warn=*/false);
3725   if (DEBUG_MANGLE)
3726     fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3727   return result;
3728 }
3729 
3730 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3731    operator to TYPE.  The nodes are IDENTIFIERs whose TREE_TYPE is the
3732    TYPE.  */
3733 
3734 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
3735 
3736 /* Hash a node (VAL1) in the table.  */
3737 
3738 static hashval_t
hash_type(const void * val)3739 hash_type (const void *val)
3740 {
3741   return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
3742 }
3743 
3744 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE).  */
3745 
3746 static int
compare_type(const void * val1,const void * val2)3747 compare_type (const void *val1, const void *val2)
3748 {
3749   return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
3750 }
3751 
3752 /* Return an identifier for the mangled unqualified name for a
3753    conversion operator to TYPE.  This mangling is not specified by the
3754    ABI spec; it is only used internally.  */
3755 
3756 tree
mangle_conv_op_name_for_type(const tree type)3757 mangle_conv_op_name_for_type (const tree type)
3758 {
3759   void **slot;
3760   tree identifier;
3761 
3762   if (type == error_mark_node)
3763     return error_mark_node;
3764 
3765   if (conv_type_names == NULL)
3766     conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
3767 
3768   slot = htab_find_slot_with_hash (conv_type_names, type,
3769 				   (hashval_t) TYPE_UID (type), INSERT);
3770   identifier = (tree)*slot;
3771   if (!identifier)
3772     {
3773       char buffer[64];
3774 
3775        /* Create a unique name corresponding to TYPE.  */
3776       sprintf (buffer, "operator %lu",
3777 	       (unsigned long) htab_elements (conv_type_names));
3778       identifier = get_identifier (buffer);
3779       *slot = identifier;
3780 
3781       /* Hang TYPE off the identifier so it can be found easily later
3782 	 when performing conversions.  */
3783       TREE_TYPE (identifier) = type;
3784 
3785       /* Set bits on the identifier so we know later it's a conversion.  */
3786       IDENTIFIER_OPNAME_P (identifier) = 1;
3787       IDENTIFIER_TYPENAME_P (identifier) = 1;
3788     }
3789 
3790   return identifier;
3791 }
3792 
3793 /* Write out the appropriate string for this variable when generating
3794    another mangled name based on this one.  */
3795 
3796 static void
write_guarded_var_name(const tree variable)3797 write_guarded_var_name (const tree variable)
3798 {
3799   if (DECL_NAME (variable)
3800       && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3801     /* The name of a guard variable for a reference temporary should refer
3802        to the reference, not the temporary.  */
3803     write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3804   else
3805     write_name (variable, /*ignore_local_scope=*/0);
3806 }
3807 
3808 /* Return an identifier for the name of an initialization guard
3809    variable for indicated VARIABLE.  */
3810 
3811 tree
mangle_guard_variable(const tree variable)3812 mangle_guard_variable (const tree variable)
3813 {
3814   start_mangling (variable);
3815   write_string ("_ZGV");
3816   write_guarded_var_name (variable);
3817   return finish_mangling_get_identifier (/*warn=*/false);
3818 }
3819 
3820 /* Return an identifier for the name of a thread_local initialization
3821    function for VARIABLE.  */
3822 
3823 tree
mangle_tls_init_fn(const tree variable)3824 mangle_tls_init_fn (const tree variable)
3825 {
3826   start_mangling (variable);
3827   write_string ("_ZTH");
3828   write_guarded_var_name (variable);
3829   return finish_mangling_get_identifier (/*warn=*/false);
3830 }
3831 
3832 /* Return an identifier for the name of a thread_local wrapper
3833    function for VARIABLE.  */
3834 
3835 #define TLS_WRAPPER_PREFIX "_ZTW"
3836 
3837 tree
mangle_tls_wrapper_fn(const tree variable)3838 mangle_tls_wrapper_fn (const tree variable)
3839 {
3840   start_mangling (variable);
3841   write_string (TLS_WRAPPER_PREFIX);
3842   write_guarded_var_name (variable);
3843   return finish_mangling_get_identifier (/*warn=*/false);
3844 }
3845 
3846 /* Return true iff FN is a thread_local wrapper function.  */
3847 
3848 bool
decl_tls_wrapper_p(const tree fn)3849 decl_tls_wrapper_p (const tree fn)
3850 {
3851   if (TREE_CODE (fn) != FUNCTION_DECL)
3852     return false;
3853   tree name = DECL_NAME (fn);
3854   return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3855 		  strlen (TLS_WRAPPER_PREFIX)) == 0;
3856 }
3857 
3858 /* Return an identifier for the name of a temporary variable used to
3859    initialize a static reference.  This isn't part of the ABI, but we might
3860    as well call them something readable.  */
3861 
3862 static GTY(()) int temp_count;
3863 
3864 tree
mangle_ref_init_variable(const tree variable)3865 mangle_ref_init_variable (const tree variable)
3866 {
3867   start_mangling (variable);
3868   write_string ("_ZGR");
3869   write_name (variable, /*ignore_local_scope=*/0);
3870   /* Avoid name clashes with aggregate initialization of multiple
3871      references at once.  */
3872   write_unsigned_number (temp_count++);
3873   return finish_mangling_get_identifier (/*warn=*/false);
3874 }
3875 
3876 
3877 /* Foreign language type mangling section.  */
3878 
3879 /* How to write the type codes for the integer Java type.  */
3880 
3881 static void
write_java_integer_type_codes(const tree type)3882 write_java_integer_type_codes (const tree type)
3883 {
3884   if (type == java_int_type_node)
3885     write_char ('i');
3886   else if (type == java_short_type_node)
3887     write_char ('s');
3888   else if (type == java_byte_type_node)
3889     write_char ('c');
3890   else if (type == java_char_type_node)
3891     write_char ('w');
3892   else if (type == java_long_type_node)
3893     write_char ('x');
3894   else if (type == java_boolean_type_node)
3895     write_char ('b');
3896   else
3897     gcc_unreachable ();
3898 }
3899 
3900 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3901    function generates a mangled name for the vtable map variable of
3902    the class type.  For example, if the class type is
3903    "std::bad_exception", the mangled name for the class is
3904    "St13bad_exception".  This function would generate the name
3905    "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3906    "_VTV<std::bad_exception>::__vtable_map".  */
3907 
3908 
3909 char *
get_mangled_vtable_map_var_name(tree class_type)3910 get_mangled_vtable_map_var_name (tree class_type)
3911 {
3912   char *var_name = NULL;
3913   const char *prefix = "_ZN4_VTVI";
3914   const char *postfix = "E12__vtable_mapE";
3915 
3916   gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3917 
3918   tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3919   unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3920                      strlen (prefix) +
3921                      strlen (postfix) + 1;
3922 
3923   var_name = (char *) xmalloc (len);
3924 
3925   sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3926 
3927   return var_name;
3928 }
3929 
3930 #include "gt-cp-mangle.h"
3931