xref: /openbsd-src/gnu/gcc/gcc/cp/mangle.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Name mangling for the 3.0 C++ ABI.
2*404b540aSrobert    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3*404b540aSrobert    Free Software Foundation, Inc.
4*404b540aSrobert    Written by Alex Samuel <samuel@codesourcery.com>
5*404b540aSrobert 
6*404b540aSrobert    This file is part of GCC.
7*404b540aSrobert 
8*404b540aSrobert    GCC is free software; you can redistribute it and/or modify it
9*404b540aSrobert    under the terms of the GNU General Public License as published by
10*404b540aSrobert    the Free Software Foundation; either version 2, or (at your option)
11*404b540aSrobert    any later version.
12*404b540aSrobert 
13*404b540aSrobert    GCC is distributed in the hope that it will be useful, but
14*404b540aSrobert    WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*404b540aSrobert    General Public License for more details.
17*404b540aSrobert 
18*404b540aSrobert    You should have received a copy of the GNU General Public License
19*404b540aSrobert    along with GCC; see the file COPYING.  If not, write to the Free
20*404b540aSrobert    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21*404b540aSrobert    02110-1301, USA.  */
22*404b540aSrobert 
23*404b540aSrobert /* This file implements mangling of C++ names according to the IA64
24*404b540aSrobert    C++ ABI specification.  A mangled name encodes a function or
25*404b540aSrobert    variable's name, scope, type, and/or template arguments into a text
26*404b540aSrobert    identifier.  This identifier is used as the function's or
27*404b540aSrobert    variable's linkage name, to preserve compatibility between C++'s
28*404b540aSrobert    language features (templates, scoping, and overloading) and C
29*404b540aSrobert    linkers.
30*404b540aSrobert 
31*404b540aSrobert    Additionally, g++ uses mangled names internally.  To support this,
32*404b540aSrobert    mangling of types is allowed, even though the mangled name of a
33*404b540aSrobert    type should not appear by itself as an exported name.  Ditto for
34*404b540aSrobert    uninstantiated templates.
35*404b540aSrobert 
36*404b540aSrobert    The primary entry point for this module is mangle_decl, which
37*404b540aSrobert    returns an identifier containing the mangled name for a decl.
38*404b540aSrobert    Additional entry points are provided to build mangled names of
39*404b540aSrobert    particular constructs when the appropriate decl for that construct
40*404b540aSrobert    is not available.  These are:
41*404b540aSrobert 
42*404b540aSrobert      mangle_typeinfo_for_type:		typeinfo data
43*404b540aSrobert      mangle_typeinfo_string_for_type:	typeinfo type name
44*404b540aSrobert      mangle_vtbl_for_type:		virtual table data
45*404b540aSrobert      mangle_vtt_for_type:		VTT data
46*404b540aSrobert      mangle_ctor_vtbl_for_type:		`C-in-B' constructor virtual table data
47*404b540aSrobert      mangle_thunk:			thunk function or entry  */
48*404b540aSrobert 
49*404b540aSrobert #include "config.h"
50*404b540aSrobert #include "system.h"
51*404b540aSrobert #include "coretypes.h"
52*404b540aSrobert #include "tm.h"
53*404b540aSrobert #include "tree.h"
54*404b540aSrobert #include "tm_p.h"
55*404b540aSrobert #include "cp-tree.h"
56*404b540aSrobert #include "real.h"
57*404b540aSrobert #include "obstack.h"
58*404b540aSrobert #include "toplev.h"
59*404b540aSrobert #include "varray.h"
60*404b540aSrobert #include "flags.h"
61*404b540aSrobert #include "target.h"
62*404b540aSrobert 
63*404b540aSrobert /* Debugging support.  */
64*404b540aSrobert 
65*404b540aSrobert /* Define DEBUG_MANGLE to enable very verbose trace messages.  */
66*404b540aSrobert #ifndef DEBUG_MANGLE
67*404b540aSrobert #define DEBUG_MANGLE 0
68*404b540aSrobert #endif
69*404b540aSrobert 
70*404b540aSrobert /* Macros for tracing the write_* functions.  */
71*404b540aSrobert #if DEBUG_MANGLE
72*404b540aSrobert # define MANGLE_TRACE(FN, INPUT) \
73*404b540aSrobert   fprintf (stderr, "  %-24s: %-24s\n", (FN), (INPUT))
74*404b540aSrobert # define MANGLE_TRACE_TREE(FN, NODE) \
75*404b540aSrobert   fprintf (stderr, "  %-24s: %-24s (%p)\n", \
76*404b540aSrobert 	   (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
77*404b540aSrobert #else
78*404b540aSrobert # define MANGLE_TRACE(FN, INPUT)
79*404b540aSrobert # define MANGLE_TRACE_TREE(FN, NODE)
80*404b540aSrobert #endif
81*404b540aSrobert 
82*404b540aSrobert /* Nonzero if NODE is a class template-id.  We can't rely on
83*404b540aSrobert    CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
84*404b540aSrobert    that hard to distinguish A<T> from A, where A<T> is the type as
85*404b540aSrobert    instantiated outside of the template, and A is the type used
86*404b540aSrobert    without parameters inside the template.  */
87*404b540aSrobert #define CLASSTYPE_TEMPLATE_ID_P(NODE)					\
88*404b540aSrobert   (TYPE_LANG_SPECIFIC (NODE) != NULL					\
89*404b540aSrobert    && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM			\
90*404b540aSrobert        || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL			\
91*404b540aSrobert 	   && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
92*404b540aSrobert 
93*404b540aSrobert /* Things we only need one of.  This module is not reentrant.  */
94*404b540aSrobert typedef struct globals GTY(())
95*404b540aSrobert {
96*404b540aSrobert   /* An array of the current substitution candidates, in the order
97*404b540aSrobert      we've seen them.  */
98*404b540aSrobert   VEC(tree,gc) *substitutions;
99*404b540aSrobert 
100*404b540aSrobert   /* The entity that is being mangled.  */
101*404b540aSrobert   tree GTY ((skip)) entity;
102*404b540aSrobert 
103*404b540aSrobert   /* True if the mangling will be different in a future version of the
104*404b540aSrobert      ABI.  */
105*404b540aSrobert   bool need_abi_warning;
106*404b540aSrobert } globals;
107*404b540aSrobert 
108*404b540aSrobert static GTY (()) globals G;
109*404b540aSrobert 
110*404b540aSrobert /* The obstack on which we build mangled names.  */
111*404b540aSrobert static struct obstack *mangle_obstack;
112*404b540aSrobert 
113*404b540aSrobert /* The obstack on which we build mangled names that are not going to
114*404b540aSrobert    be IDENTIFIER_NODEs.  */
115*404b540aSrobert static struct obstack name_obstack;
116*404b540aSrobert 
117*404b540aSrobert /* The first object on the name_obstack; we use this to free memory
118*404b540aSrobert    allocated on the name_obstack.  */
119*404b540aSrobert static void *name_base;
120*404b540aSrobert 
121*404b540aSrobert /* An incomplete mangled name.  There will be no NUL terminator.  If
122*404b540aSrobert    there is no incomplete mangled name, this variable is NULL.  */
123*404b540aSrobert static char *partially_mangled_name;
124*404b540aSrobert 
125*404b540aSrobert /* The number of characters in the PARTIALLY_MANGLED_NAME.  */
126*404b540aSrobert static size_t partially_mangled_name_len;
127*404b540aSrobert 
128*404b540aSrobert /* Indices into subst_identifiers.  These are identifiers used in
129*404b540aSrobert    special substitution rules.  */
130*404b540aSrobert typedef enum
131*404b540aSrobert {
132*404b540aSrobert   SUBID_ALLOCATOR,
133*404b540aSrobert   SUBID_BASIC_STRING,
134*404b540aSrobert   SUBID_CHAR_TRAITS,
135*404b540aSrobert   SUBID_BASIC_ISTREAM,
136*404b540aSrobert   SUBID_BASIC_OSTREAM,
137*404b540aSrobert   SUBID_BASIC_IOSTREAM,
138*404b540aSrobert   SUBID_MAX
139*404b540aSrobert }
140*404b540aSrobert substitution_identifier_index_t;
141*404b540aSrobert 
142*404b540aSrobert /* For quick substitution checks, look up these common identifiers
143*404b540aSrobert    once only.  */
144*404b540aSrobert static GTY(()) tree subst_identifiers[SUBID_MAX];
145*404b540aSrobert 
146*404b540aSrobert /* Single-letter codes for builtin integer types, defined in
147*404b540aSrobert    <builtin-type>.  These are indexed by integer_type_kind values.  */
148*404b540aSrobert static const char
149*404b540aSrobert integer_type_codes[itk_none] =
150*404b540aSrobert {
151*404b540aSrobert   'c',  /* itk_char */
152*404b540aSrobert   'a',  /* itk_signed_char */
153*404b540aSrobert   'h',  /* itk_unsigned_char */
154*404b540aSrobert   's',  /* itk_short */
155*404b540aSrobert   't',  /* itk_unsigned_short */
156*404b540aSrobert   'i',  /* itk_int */
157*404b540aSrobert   'j',  /* itk_unsigned_int */
158*404b540aSrobert   'l',  /* itk_long */
159*404b540aSrobert   'm',  /* itk_unsigned_long */
160*404b540aSrobert   'x',  /* itk_long_long */
161*404b540aSrobert   'y'   /* itk_unsigned_long_long */
162*404b540aSrobert };
163*404b540aSrobert 
164*404b540aSrobert static int decl_is_template_id (const tree, tree* const);
165*404b540aSrobert 
166*404b540aSrobert /* Functions for handling substitutions.  */
167*404b540aSrobert 
168*404b540aSrobert static inline tree canonicalize_for_substitution (tree);
169*404b540aSrobert static void add_substitution (tree);
170*404b540aSrobert static inline int is_std_substitution (const tree,
171*404b540aSrobert 				       const substitution_identifier_index_t);
172*404b540aSrobert static inline int is_std_substitution_char (const tree,
173*404b540aSrobert 					    const substitution_identifier_index_t);
174*404b540aSrobert static int find_substitution (tree);
175*404b540aSrobert static void mangle_call_offset (const tree, const tree);
176*404b540aSrobert 
177*404b540aSrobert /* Functions for emitting mangled representations of things.  */
178*404b540aSrobert 
179*404b540aSrobert static void write_mangled_name (const tree, bool);
180*404b540aSrobert static void write_encoding (const tree);
181*404b540aSrobert static void write_name (tree, const int);
182*404b540aSrobert static void write_unscoped_name (const tree);
183*404b540aSrobert static void write_unscoped_template_name (const tree);
184*404b540aSrobert static void write_nested_name (const tree);
185*404b540aSrobert static void write_prefix (const tree);
186*404b540aSrobert static void write_template_prefix (const tree);
187*404b540aSrobert static void write_unqualified_name (const tree);
188*404b540aSrobert static void write_conversion_operator_name (const tree);
189*404b540aSrobert static void write_source_name (tree);
190*404b540aSrobert static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
191*404b540aSrobert 			   const unsigned int);
192*404b540aSrobert static void write_number (unsigned HOST_WIDE_INT, const int,
193*404b540aSrobert 			  const unsigned int);
194*404b540aSrobert static void write_integer_cst (const tree);
195*404b540aSrobert static void write_real_cst (const tree);
196*404b540aSrobert static void write_identifier (const char *);
197*404b540aSrobert static void write_special_name_constructor (const tree);
198*404b540aSrobert static void write_special_name_destructor (const tree);
199*404b540aSrobert static void write_type (tree);
200*404b540aSrobert static int write_CV_qualifiers_for_type (const tree);
201*404b540aSrobert static void write_builtin_type (tree);
202*404b540aSrobert static void write_function_type (const tree);
203*404b540aSrobert static void write_bare_function_type (const tree, const int, const tree);
204*404b540aSrobert static void write_method_parms (tree, const int, const tree);
205*404b540aSrobert static void write_class_enum_type (const tree);
206*404b540aSrobert static void write_template_args (tree);
207*404b540aSrobert static void write_expression (tree);
208*404b540aSrobert static void write_template_arg_literal (const tree);
209*404b540aSrobert static void write_template_arg (tree);
210*404b540aSrobert static void write_template_template_arg (const tree);
211*404b540aSrobert static void write_array_type (const tree);
212*404b540aSrobert static void write_pointer_to_member_type (const tree);
213*404b540aSrobert static void write_template_param (const tree);
214*404b540aSrobert static void write_template_template_param (const tree);
215*404b540aSrobert static void write_substitution (const int);
216*404b540aSrobert static int discriminator_for_local_entity (tree);
217*404b540aSrobert static int discriminator_for_string_literal (tree, tree);
218*404b540aSrobert static void write_discriminator (const int);
219*404b540aSrobert static void write_local_name (const tree, const tree, const tree);
220*404b540aSrobert static void dump_substitution_candidates (void);
221*404b540aSrobert static const char *mangle_decl_string (const tree);
222*404b540aSrobert 
223*404b540aSrobert /* Control functions.  */
224*404b540aSrobert 
225*404b540aSrobert static inline void start_mangling (const tree, bool);
226*404b540aSrobert static inline const char *finish_mangling (const bool);
227*404b540aSrobert static tree mangle_special_for_type (const tree, const char *);
228*404b540aSrobert 
229*404b540aSrobert /* Foreign language functions.  */
230*404b540aSrobert 
231*404b540aSrobert static void write_java_integer_type_codes (const tree);
232*404b540aSrobert 
233*404b540aSrobert /* Append a single character to the end of the mangled
234*404b540aSrobert    representation.  */
235*404b540aSrobert #define write_char(CHAR)						\
236*404b540aSrobert   obstack_1grow (mangle_obstack, (CHAR))
237*404b540aSrobert 
238*404b540aSrobert /* Append a sized buffer to the end of the mangled representation.  */
239*404b540aSrobert #define write_chars(CHAR, LEN)						\
240*404b540aSrobert   obstack_grow (mangle_obstack, (CHAR), (LEN))
241*404b540aSrobert 
242*404b540aSrobert /* Append a NUL-terminated string to the end of the mangled
243*404b540aSrobert    representation.  */
244*404b540aSrobert #define write_string(STRING)						\
245*404b540aSrobert   obstack_grow (mangle_obstack, (STRING), strlen (STRING))
246*404b540aSrobert 
247*404b540aSrobert /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
248*404b540aSrobert    same purpose (context, which may be a type) and value (template
249*404b540aSrobert    decl).  See write_template_prefix for more information on what this
250*404b540aSrobert    is used for.  */
251*404b540aSrobert #define NESTED_TEMPLATE_MATCH(NODE1, NODE2)				\
252*404b540aSrobert   (TREE_CODE (NODE1) == TREE_LIST					\
253*404b540aSrobert    && TREE_CODE (NODE2) == TREE_LIST					\
254*404b540aSrobert    && ((TYPE_P (TREE_PURPOSE (NODE1))					\
255*404b540aSrobert 	&& same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))	\
256*404b540aSrobert        || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2))			\
257*404b540aSrobert    && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
258*404b540aSrobert 
259*404b540aSrobert /* Write out an unsigned quantity in base 10.  */
260*404b540aSrobert #define write_unsigned_number(NUMBER)					\
261*404b540aSrobert   write_number ((NUMBER), /*unsigned_p=*/1, 10)
262*404b540aSrobert 
263*404b540aSrobert /* Save the current (incomplete) mangled name and release the obstack
264*404b540aSrobert    storage holding it.  This function should be used during mangling
265*404b540aSrobert    when making a call that could result in a call to get_identifier,
266*404b540aSrobert    as such a call will clobber the same obstack being used for
267*404b540aSrobert    mangling.  This function may not be called twice without an
268*404b540aSrobert    intervening call to restore_partially_mangled_name.  */
269*404b540aSrobert 
270*404b540aSrobert static void
save_partially_mangled_name(void)271*404b540aSrobert save_partially_mangled_name (void)
272*404b540aSrobert {
273*404b540aSrobert   if (mangle_obstack == &ident_hash->stack)
274*404b540aSrobert     {
275*404b540aSrobert       gcc_assert (!partially_mangled_name);
276*404b540aSrobert       partially_mangled_name_len = obstack_object_size (mangle_obstack);
277*404b540aSrobert       partially_mangled_name = XNEWVEC (char, partially_mangled_name_len);
278*404b540aSrobert       memcpy (partially_mangled_name, obstack_base (mangle_obstack),
279*404b540aSrobert 	      partially_mangled_name_len);
280*404b540aSrobert       obstack_free (mangle_obstack, obstack_finish (mangle_obstack));
281*404b540aSrobert     }
282*404b540aSrobert }
283*404b540aSrobert 
284*404b540aSrobert /* Restore the incomplete mangled name saved with
285*404b540aSrobert    save_partially_mangled_name.  */
286*404b540aSrobert 
287*404b540aSrobert static void
restore_partially_mangled_name(void)288*404b540aSrobert restore_partially_mangled_name (void)
289*404b540aSrobert {
290*404b540aSrobert   if (partially_mangled_name)
291*404b540aSrobert     {
292*404b540aSrobert       obstack_grow (mangle_obstack, partially_mangled_name,
293*404b540aSrobert 		    partially_mangled_name_len);
294*404b540aSrobert       free (partially_mangled_name);
295*404b540aSrobert       partially_mangled_name = NULL;
296*404b540aSrobert     }
297*404b540aSrobert }
298*404b540aSrobert 
299*404b540aSrobert /* If DECL is a template instance, return nonzero and, if
300*404b540aSrobert    TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
301*404b540aSrobert    Otherwise return zero.  */
302*404b540aSrobert 
303*404b540aSrobert static int
decl_is_template_id(const tree decl,tree * const template_info)304*404b540aSrobert decl_is_template_id (const tree decl, tree* const template_info)
305*404b540aSrobert {
306*404b540aSrobert   if (TREE_CODE (decl) == TYPE_DECL)
307*404b540aSrobert     {
308*404b540aSrobert       /* TYPE_DECLs are handled specially.  Look at its type to decide
309*404b540aSrobert 	 if this is a template instantiation.  */
310*404b540aSrobert       const tree type = TREE_TYPE (decl);
311*404b540aSrobert 
312*404b540aSrobert       if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
313*404b540aSrobert 	{
314*404b540aSrobert 	  if (template_info != NULL)
315*404b540aSrobert 	    /* For a templated TYPE_DECL, the template info is hanging
316*404b540aSrobert 	       off the type.  */
317*404b540aSrobert 	    *template_info = TYPE_TEMPLATE_INFO (type);
318*404b540aSrobert 	  return 1;
319*404b540aSrobert 	}
320*404b540aSrobert     }
321*404b540aSrobert   else
322*404b540aSrobert     {
323*404b540aSrobert       /* Check if this is a primary template.  */
324*404b540aSrobert       if (DECL_LANG_SPECIFIC (decl) != NULL
325*404b540aSrobert 	  && DECL_USE_TEMPLATE (decl)
326*404b540aSrobert 	  && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
327*404b540aSrobert 	  && TREE_CODE (decl) != TEMPLATE_DECL)
328*404b540aSrobert 	{
329*404b540aSrobert 	  if (template_info != NULL)
330*404b540aSrobert 	    /* For most templated decls, the template info is hanging
331*404b540aSrobert 	       off the decl.  */
332*404b540aSrobert 	    *template_info = DECL_TEMPLATE_INFO (decl);
333*404b540aSrobert 	  return 1;
334*404b540aSrobert 	}
335*404b540aSrobert     }
336*404b540aSrobert 
337*404b540aSrobert   /* It's not a template id.  */
338*404b540aSrobert   return 0;
339*404b540aSrobert }
340*404b540aSrobert 
341*404b540aSrobert /* Produce debugging output of current substitution candidates.  */
342*404b540aSrobert 
343*404b540aSrobert static void
dump_substitution_candidates(void)344*404b540aSrobert dump_substitution_candidates (void)
345*404b540aSrobert {
346*404b540aSrobert   unsigned i;
347*404b540aSrobert   tree el;
348*404b540aSrobert 
349*404b540aSrobert   fprintf (stderr, "  ++ substitutions  ");
350*404b540aSrobert   for (i = 0; VEC_iterate (tree, G.substitutions, i, el); ++i)
351*404b540aSrobert     {
352*404b540aSrobert       const char *name = "???";
353*404b540aSrobert 
354*404b540aSrobert       if (i > 0)
355*404b540aSrobert 	fprintf (stderr, "                    ");
356*404b540aSrobert       if (DECL_P (el))
357*404b540aSrobert 	name = IDENTIFIER_POINTER (DECL_NAME (el));
358*404b540aSrobert       else if (TREE_CODE (el) == TREE_LIST)
359*404b540aSrobert 	name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
360*404b540aSrobert       else if (TYPE_NAME (el))
361*404b540aSrobert 	name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
362*404b540aSrobert       fprintf (stderr, " S%d_ = ", i - 1);
363*404b540aSrobert       if (TYPE_P (el) &&
364*404b540aSrobert 	  (CP_TYPE_RESTRICT_P (el)
365*404b540aSrobert 	   || CP_TYPE_VOLATILE_P (el)
366*404b540aSrobert 	   || CP_TYPE_CONST_P (el)))
367*404b540aSrobert 	fprintf (stderr, "CV-");
368*404b540aSrobert       fprintf (stderr, "%s (%s at %p)\n",
369*404b540aSrobert 	       name, tree_code_name[TREE_CODE (el)], (void *) el);
370*404b540aSrobert     }
371*404b540aSrobert }
372*404b540aSrobert 
373*404b540aSrobert /* Both decls and types can be substitution candidates, but sometimes
374*404b540aSrobert    they refer to the same thing.  For instance, a TYPE_DECL and
375*404b540aSrobert    RECORD_TYPE for the same class refer to the same thing, and should
376*404b540aSrobert    be treated accordingly in substitutions.  This function returns a
377*404b540aSrobert    canonicalized tree node representing NODE that is used when adding
378*404b540aSrobert    and substitution candidates and finding matches.  */
379*404b540aSrobert 
380*404b540aSrobert static inline tree
canonicalize_for_substitution(tree node)381*404b540aSrobert canonicalize_for_substitution (tree node)
382*404b540aSrobert {
383*404b540aSrobert   /* For a TYPE_DECL, use the type instead.  */
384*404b540aSrobert   if (TREE_CODE (node) == TYPE_DECL)
385*404b540aSrobert     node = TREE_TYPE (node);
386*404b540aSrobert   if (TYPE_P (node))
387*404b540aSrobert     node = canonical_type_variant (node);
388*404b540aSrobert 
389*404b540aSrobert   return node;
390*404b540aSrobert }
391*404b540aSrobert 
392*404b540aSrobert /* Add NODE as a substitution candidate.  NODE must not already be on
393*404b540aSrobert    the list of candidates.  */
394*404b540aSrobert 
395*404b540aSrobert static void
add_substitution(tree node)396*404b540aSrobert add_substitution (tree node)
397*404b540aSrobert {
398*404b540aSrobert   tree c;
399*404b540aSrobert 
400*404b540aSrobert   if (DEBUG_MANGLE)
401*404b540aSrobert     fprintf (stderr, "  ++ add_substitution (%s at %10p)\n",
402*404b540aSrobert 	     tree_code_name[TREE_CODE (node)], (void *) node);
403*404b540aSrobert 
404*404b540aSrobert   /* Get the canonicalized substitution candidate for NODE.  */
405*404b540aSrobert   c = canonicalize_for_substitution (node);
406*404b540aSrobert   if (DEBUG_MANGLE && c != node)
407*404b540aSrobert     fprintf (stderr, "  ++ using candidate (%s at %10p)\n",
408*404b540aSrobert 	     tree_code_name[TREE_CODE (node)], (void *) node);
409*404b540aSrobert   node = c;
410*404b540aSrobert 
411*404b540aSrobert #if ENABLE_CHECKING
412*404b540aSrobert   /* Make sure NODE isn't already a candidate.  */
413*404b540aSrobert   {
414*404b540aSrobert     int i;
415*404b540aSrobert     tree candidate;
416*404b540aSrobert 
417*404b540aSrobert     for (i = 0; VEC_iterate (tree, G.substitutions, i, candidate); i++)
418*404b540aSrobert       {
419*404b540aSrobert 	gcc_assert (!(DECL_P (node) && node == candidate));
420*404b540aSrobert 	gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
421*404b540aSrobert 		      && same_type_p (node, candidate)));
422*404b540aSrobert       }
423*404b540aSrobert   }
424*404b540aSrobert #endif /* ENABLE_CHECKING */
425*404b540aSrobert 
426*404b540aSrobert   /* Put the decl onto the varray of substitution candidates.  */
427*404b540aSrobert   VEC_safe_push (tree, gc, G.substitutions, node);
428*404b540aSrobert 
429*404b540aSrobert   if (DEBUG_MANGLE)
430*404b540aSrobert     dump_substitution_candidates ();
431*404b540aSrobert }
432*404b540aSrobert 
433*404b540aSrobert /* Helper function for find_substitution.  Returns nonzero if NODE,
434*404b540aSrobert    which may be a decl or a CLASS_TYPE, is a template-id with template
435*404b540aSrobert    name of substitution_index[INDEX] in the ::std namespace.  */
436*404b540aSrobert 
437*404b540aSrobert static inline int
is_std_substitution(const tree node,const substitution_identifier_index_t index)438*404b540aSrobert is_std_substitution (const tree node,
439*404b540aSrobert 		     const substitution_identifier_index_t index)
440*404b540aSrobert {
441*404b540aSrobert   tree type = NULL;
442*404b540aSrobert   tree decl = NULL;
443*404b540aSrobert 
444*404b540aSrobert   if (DECL_P (node))
445*404b540aSrobert     {
446*404b540aSrobert       type = TREE_TYPE (node);
447*404b540aSrobert       decl = node;
448*404b540aSrobert     }
449*404b540aSrobert   else if (CLASS_TYPE_P (node))
450*404b540aSrobert     {
451*404b540aSrobert       type = node;
452*404b540aSrobert       decl = TYPE_NAME (node);
453*404b540aSrobert     }
454*404b540aSrobert   else
455*404b540aSrobert     /* These are not the droids you're looking for.  */
456*404b540aSrobert     return 0;
457*404b540aSrobert 
458*404b540aSrobert   return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
459*404b540aSrobert 	  && TYPE_LANG_SPECIFIC (type)
460*404b540aSrobert 	  && TYPE_TEMPLATE_INFO (type)
461*404b540aSrobert 	  && (DECL_NAME (TYPE_TI_TEMPLATE (type))
462*404b540aSrobert 	      == subst_identifiers[index]));
463*404b540aSrobert }
464*404b540aSrobert 
465*404b540aSrobert /* Helper function for find_substitution.  Returns nonzero if NODE,
466*404b540aSrobert    which may be a decl or a CLASS_TYPE, is the template-id
467*404b540aSrobert    ::std::identifier<char>, where identifier is
468*404b540aSrobert    substitution_index[INDEX].  */
469*404b540aSrobert 
470*404b540aSrobert static inline int
is_std_substitution_char(const tree node,const substitution_identifier_index_t index)471*404b540aSrobert is_std_substitution_char (const tree node,
472*404b540aSrobert 			  const substitution_identifier_index_t index)
473*404b540aSrobert {
474*404b540aSrobert   tree args;
475*404b540aSrobert   /* Check NODE's name is ::std::identifier.  */
476*404b540aSrobert   if (!is_std_substitution (node, index))
477*404b540aSrobert     return 0;
478*404b540aSrobert   /* Figure out its template args.  */
479*404b540aSrobert   if (DECL_P (node))
480*404b540aSrobert     args = DECL_TI_ARGS (node);
481*404b540aSrobert   else if (CLASS_TYPE_P (node))
482*404b540aSrobert     args = CLASSTYPE_TI_ARGS (node);
483*404b540aSrobert   else
484*404b540aSrobert     /* Oops, not a template.  */
485*404b540aSrobert     return 0;
486*404b540aSrobert   /* NODE's template arg list should be <char>.  */
487*404b540aSrobert   return
488*404b540aSrobert     TREE_VEC_LENGTH (args) == 1
489*404b540aSrobert     && TREE_VEC_ELT (args, 0) == char_type_node;
490*404b540aSrobert }
491*404b540aSrobert 
492*404b540aSrobert /* Check whether a substitution should be used to represent NODE in
493*404b540aSrobert    the mangling.
494*404b540aSrobert 
495*404b540aSrobert    First, check standard special-case substitutions.
496*404b540aSrobert 
497*404b540aSrobert      <substitution> ::= St
498*404b540aSrobert 	 # ::std
499*404b540aSrobert 
500*404b540aSrobert 		    ::= Sa
501*404b540aSrobert 	 # ::std::allocator
502*404b540aSrobert 
503*404b540aSrobert 		    ::= Sb
504*404b540aSrobert 	 # ::std::basic_string
505*404b540aSrobert 
506*404b540aSrobert 		    ::= Ss
507*404b540aSrobert 	 # ::std::basic_string<char,
508*404b540aSrobert 			       ::std::char_traits<char>,
509*404b540aSrobert 			       ::std::allocator<char> >
510*404b540aSrobert 
511*404b540aSrobert 		    ::= Si
512*404b540aSrobert 	 # ::std::basic_istream<char, ::std::char_traits<char> >
513*404b540aSrobert 
514*404b540aSrobert 		    ::= So
515*404b540aSrobert 	 # ::std::basic_ostream<char, ::std::char_traits<char> >
516*404b540aSrobert 
517*404b540aSrobert 		    ::= Sd
518*404b540aSrobert 	 # ::std::basic_iostream<char, ::std::char_traits<char> >
519*404b540aSrobert 
520*404b540aSrobert    Then examine the stack of currently available substitution
521*404b540aSrobert    candidates for entities appearing earlier in the same mangling
522*404b540aSrobert 
523*404b540aSrobert    If a substitution is found, write its mangled representation and
524*404b540aSrobert    return nonzero.  If none is found, just return zero.  */
525*404b540aSrobert 
526*404b540aSrobert static int
find_substitution(tree node)527*404b540aSrobert find_substitution (tree node)
528*404b540aSrobert {
529*404b540aSrobert   int i;
530*404b540aSrobert   const int size = VEC_length (tree, G.substitutions);
531*404b540aSrobert   tree decl;
532*404b540aSrobert   tree type;
533*404b540aSrobert 
534*404b540aSrobert   if (DEBUG_MANGLE)
535*404b540aSrobert     fprintf (stderr, "  ++ find_substitution (%s at %p)\n",
536*404b540aSrobert 	     tree_code_name[TREE_CODE (node)], (void *) node);
537*404b540aSrobert 
538*404b540aSrobert   /* Obtain the canonicalized substitution representation for NODE.
539*404b540aSrobert      This is what we'll compare against.  */
540*404b540aSrobert   node = canonicalize_for_substitution (node);
541*404b540aSrobert 
542*404b540aSrobert   /* Check for builtin substitutions.  */
543*404b540aSrobert 
544*404b540aSrobert   decl = TYPE_P (node) ? TYPE_NAME (node) : node;
545*404b540aSrobert   type = TYPE_P (node) ? node : TREE_TYPE (node);
546*404b540aSrobert 
547*404b540aSrobert   /* Check for std::allocator.  */
548*404b540aSrobert   if (decl
549*404b540aSrobert       && is_std_substitution (decl, SUBID_ALLOCATOR)
550*404b540aSrobert       && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
551*404b540aSrobert     {
552*404b540aSrobert       write_string ("Sa");
553*404b540aSrobert       return 1;
554*404b540aSrobert     }
555*404b540aSrobert 
556*404b540aSrobert   /* Check for std::basic_string.  */
557*404b540aSrobert   if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
558*404b540aSrobert     {
559*404b540aSrobert       if (TYPE_P (node))
560*404b540aSrobert 	{
561*404b540aSrobert 	  /* If this is a type (i.e. a fully-qualified template-id),
562*404b540aSrobert 	     check for
563*404b540aSrobert 		 std::basic_string <char,
564*404b540aSrobert 				    std::char_traits<char>,
565*404b540aSrobert 				    std::allocator<char> > .  */
566*404b540aSrobert 	  if (cp_type_quals (type) == TYPE_UNQUALIFIED
567*404b540aSrobert 	      && CLASSTYPE_USE_TEMPLATE (type))
568*404b540aSrobert 	    {
569*404b540aSrobert 	      tree args = CLASSTYPE_TI_ARGS (type);
570*404b540aSrobert 	      if (TREE_VEC_LENGTH (args) == 3
571*404b540aSrobert 		  && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
572*404b540aSrobert 		  && is_std_substitution_char (TREE_VEC_ELT (args, 1),
573*404b540aSrobert 					       SUBID_CHAR_TRAITS)
574*404b540aSrobert 		  && is_std_substitution_char (TREE_VEC_ELT (args, 2),
575*404b540aSrobert 					       SUBID_ALLOCATOR))
576*404b540aSrobert 		{
577*404b540aSrobert 		  write_string ("Ss");
578*404b540aSrobert 		  return 1;
579*404b540aSrobert 		}
580*404b540aSrobert 	    }
581*404b540aSrobert 	}
582*404b540aSrobert       else
583*404b540aSrobert 	/* Substitute for the template name only if this isn't a type.  */
584*404b540aSrobert 	{
585*404b540aSrobert 	  write_string ("Sb");
586*404b540aSrobert 	  return 1;
587*404b540aSrobert 	}
588*404b540aSrobert     }
589*404b540aSrobert 
590*404b540aSrobert   /* Check for basic_{i,o,io}stream.  */
591*404b540aSrobert   if (TYPE_P (node)
592*404b540aSrobert       && cp_type_quals (type) == TYPE_UNQUALIFIED
593*404b540aSrobert       && CLASS_TYPE_P (type)
594*404b540aSrobert       && CLASSTYPE_USE_TEMPLATE (type)
595*404b540aSrobert       && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
596*404b540aSrobert     {
597*404b540aSrobert       /* First, check for the template
598*404b540aSrobert 	 args <char, std::char_traits<char> > .  */
599*404b540aSrobert       tree args = CLASSTYPE_TI_ARGS (type);
600*404b540aSrobert       if (TREE_VEC_LENGTH (args) == 2
601*404b540aSrobert 	  && TYPE_P (TREE_VEC_ELT (args, 0))
602*404b540aSrobert 	  && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
603*404b540aSrobert 	  && is_std_substitution_char (TREE_VEC_ELT (args, 1),
604*404b540aSrobert 				       SUBID_CHAR_TRAITS))
605*404b540aSrobert 	{
606*404b540aSrobert 	  /* Got them.  Is this basic_istream?  */
607*404b540aSrobert 	  if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
608*404b540aSrobert 	    {
609*404b540aSrobert 	      write_string ("Si");
610*404b540aSrobert 	      return 1;
611*404b540aSrobert 	    }
612*404b540aSrobert 	  /* Or basic_ostream?  */
613*404b540aSrobert 	  else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
614*404b540aSrobert 	    {
615*404b540aSrobert 	      write_string ("So");
616*404b540aSrobert 	      return 1;
617*404b540aSrobert 	    }
618*404b540aSrobert 	  /* Or basic_iostream?  */
619*404b540aSrobert 	  else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
620*404b540aSrobert 	    {
621*404b540aSrobert 	      write_string ("Sd");
622*404b540aSrobert 	      return 1;
623*404b540aSrobert 	    }
624*404b540aSrobert 	}
625*404b540aSrobert     }
626*404b540aSrobert 
627*404b540aSrobert   /* Check for namespace std.  */
628*404b540aSrobert   if (decl && DECL_NAMESPACE_STD_P (decl))
629*404b540aSrobert     {
630*404b540aSrobert       write_string ("St");
631*404b540aSrobert       return 1;
632*404b540aSrobert     }
633*404b540aSrobert 
634*404b540aSrobert   /* Now check the list of available substitutions for this mangling
635*404b540aSrobert      operation.  */
636*404b540aSrobert   for (i = 0; i < size; ++i)
637*404b540aSrobert     {
638*404b540aSrobert       tree candidate = VEC_index (tree, G.substitutions, i);
639*404b540aSrobert       /* NODE is a matched to a candidate if it's the same decl node or
640*404b540aSrobert 	 if it's the same type.  */
641*404b540aSrobert       if (decl == candidate
642*404b540aSrobert 	  || (TYPE_P (candidate) && type && TYPE_P (type)
643*404b540aSrobert 	      && same_type_p (type, candidate))
644*404b540aSrobert 	  || NESTED_TEMPLATE_MATCH (node, candidate))
645*404b540aSrobert 	{
646*404b540aSrobert 	  write_substitution (i);
647*404b540aSrobert 	  return 1;
648*404b540aSrobert 	}
649*404b540aSrobert     }
650*404b540aSrobert 
651*404b540aSrobert   /* No substitution found.  */
652*404b540aSrobert   return 0;
653*404b540aSrobert }
654*404b540aSrobert 
655*404b540aSrobert 
656*404b540aSrobert /* TOP_LEVEL is true, if this is being called at outermost level of
657*404b540aSrobert   mangling. It should be false when mangling a decl appearing in an
658*404b540aSrobert   expression within some other mangling.
659*404b540aSrobert 
660*404b540aSrobert   <mangled-name>      ::= _Z <encoding>  */
661*404b540aSrobert 
662*404b540aSrobert static void
write_mangled_name(const tree decl,bool top_level)663*404b540aSrobert write_mangled_name (const tree decl, bool top_level)
664*404b540aSrobert {
665*404b540aSrobert   MANGLE_TRACE_TREE ("mangled-name", decl);
666*404b540aSrobert 
667*404b540aSrobert   if (/* The names of `extern "C"' functions are not mangled.  */
668*404b540aSrobert       DECL_EXTERN_C_FUNCTION_P (decl)
669*404b540aSrobert       /* But overloaded operator names *are* mangled.  */
670*404b540aSrobert       && !DECL_OVERLOADED_OPERATOR_P (decl))
671*404b540aSrobert     {
672*404b540aSrobert     unmangled_name:;
673*404b540aSrobert 
674*404b540aSrobert       if (top_level)
675*404b540aSrobert 	write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
676*404b540aSrobert       else
677*404b540aSrobert 	{
678*404b540aSrobert 	  /* The standard notes: "The <encoding> of an extern "C"
679*404b540aSrobert 	     function is treated like global-scope data, i.e. as its
680*404b540aSrobert 	     <source-name> without a type."  We cannot write
681*404b540aSrobert 	     overloaded operators that way though, because it contains
682*404b540aSrobert 	     characters invalid in assembler.  */
683*404b540aSrobert 	  if (abi_version_at_least (2))
684*404b540aSrobert 	    write_string ("_Z");
685*404b540aSrobert 	  else
686*404b540aSrobert 	    G.need_abi_warning = true;
687*404b540aSrobert 	  write_source_name (DECL_NAME (decl));
688*404b540aSrobert 	}
689*404b540aSrobert     }
690*404b540aSrobert   else if (TREE_CODE (decl) == VAR_DECL
691*404b540aSrobert 	   /* The names of global variables aren't mangled.  */
692*404b540aSrobert 	   && (CP_DECL_CONTEXT (decl) == global_namespace
693*404b540aSrobert 	       /* And neither are `extern "C"' variables.  */
694*404b540aSrobert 	       || DECL_EXTERN_C_P (decl)))
695*404b540aSrobert     {
696*404b540aSrobert       if (top_level || abi_version_at_least (2))
697*404b540aSrobert 	goto unmangled_name;
698*404b540aSrobert       else
699*404b540aSrobert 	{
700*404b540aSrobert 	  G.need_abi_warning = true;
701*404b540aSrobert 	  goto mangled_name;
702*404b540aSrobert 	}
703*404b540aSrobert     }
704*404b540aSrobert   else
705*404b540aSrobert     {
706*404b540aSrobert     mangled_name:;
707*404b540aSrobert       write_string ("_Z");
708*404b540aSrobert       write_encoding (decl);
709*404b540aSrobert       if (DECL_LANG_SPECIFIC (decl)
710*404b540aSrobert 	  && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
711*404b540aSrobert 	      || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
712*404b540aSrobert 	/* We need a distinct mangled name for these entities, but
713*404b540aSrobert 	   we should never actually output it.  So, we append some
714*404b540aSrobert 	   characters the assembler won't like.  */
715*404b540aSrobert 	write_string (" *INTERNAL* ");
716*404b540aSrobert     }
717*404b540aSrobert }
718*404b540aSrobert 
719*404b540aSrobert /*   <encoding>		::= <function name> <bare-function-type>
720*404b540aSrobert 			::= <data name>  */
721*404b540aSrobert 
722*404b540aSrobert static void
write_encoding(const tree decl)723*404b540aSrobert write_encoding (const tree decl)
724*404b540aSrobert {
725*404b540aSrobert   MANGLE_TRACE_TREE ("encoding", decl);
726*404b540aSrobert 
727*404b540aSrobert   if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
728*404b540aSrobert     {
729*404b540aSrobert       /* For overloaded operators write just the mangled name
730*404b540aSrobert 	 without arguments.  */
731*404b540aSrobert       if (DECL_OVERLOADED_OPERATOR_P (decl))
732*404b540aSrobert 	write_name (decl, /*ignore_local_scope=*/0);
733*404b540aSrobert       else
734*404b540aSrobert 	write_source_name (DECL_NAME (decl));
735*404b540aSrobert       return;
736*404b540aSrobert     }
737*404b540aSrobert 
738*404b540aSrobert   write_name (decl, /*ignore_local_scope=*/0);
739*404b540aSrobert   if (TREE_CODE (decl) == FUNCTION_DECL)
740*404b540aSrobert     {
741*404b540aSrobert       tree fn_type;
742*404b540aSrobert       tree d;
743*404b540aSrobert 
744*404b540aSrobert       if (decl_is_template_id (decl, NULL))
745*404b540aSrobert 	{
746*404b540aSrobert 	  save_partially_mangled_name ();
747*404b540aSrobert 	  fn_type = get_mostly_instantiated_function_type (decl);
748*404b540aSrobert 	  restore_partially_mangled_name ();
749*404b540aSrobert 	  /* FN_TYPE will not have parameter types for in-charge or
750*404b540aSrobert 	     VTT parameters.  Therefore, we pass NULL_TREE to
751*404b540aSrobert 	     write_bare_function_type -- otherwise, it will get
752*404b540aSrobert 	     confused about which artificial parameters to skip.  */
753*404b540aSrobert 	  d = NULL_TREE;
754*404b540aSrobert 	}
755*404b540aSrobert       else
756*404b540aSrobert 	{
757*404b540aSrobert 	  fn_type = TREE_TYPE (decl);
758*404b540aSrobert 	  d = decl;
759*404b540aSrobert 	}
760*404b540aSrobert 
761*404b540aSrobert       write_bare_function_type (fn_type,
762*404b540aSrobert 				(!DECL_CONSTRUCTOR_P (decl)
763*404b540aSrobert 				 && !DECL_DESTRUCTOR_P (decl)
764*404b540aSrobert 				 && !DECL_CONV_FN_P (decl)
765*404b540aSrobert 				 && decl_is_template_id (decl, NULL)),
766*404b540aSrobert 				d);
767*404b540aSrobert     }
768*404b540aSrobert }
769*404b540aSrobert 
770*404b540aSrobert /* <name> ::= <unscoped-name>
771*404b540aSrobert 	  ::= <unscoped-template-name> <template-args>
772*404b540aSrobert 	  ::= <nested-name>
773*404b540aSrobert 	  ::= <local-name>
774*404b540aSrobert 
775*404b540aSrobert    If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
776*404b540aSrobert    called from <local-name>, which mangles the enclosing scope
777*404b540aSrobert    elsewhere and then uses this function to mangle just the part
778*404b540aSrobert    underneath the function scope.  So don't use the <local-name>
779*404b540aSrobert    production, to avoid an infinite recursion.  */
780*404b540aSrobert 
781*404b540aSrobert static void
write_name(tree decl,const int ignore_local_scope)782*404b540aSrobert write_name (tree decl, const int ignore_local_scope)
783*404b540aSrobert {
784*404b540aSrobert   tree context;
785*404b540aSrobert 
786*404b540aSrobert   MANGLE_TRACE_TREE ("name", decl);
787*404b540aSrobert 
788*404b540aSrobert   if (TREE_CODE (decl) == TYPE_DECL)
789*404b540aSrobert     {
790*404b540aSrobert       /* In case this is a typedef, fish out the corresponding
791*404b540aSrobert 	 TYPE_DECL for the main variant.  */
792*404b540aSrobert       decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
793*404b540aSrobert       context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
794*404b540aSrobert     }
795*404b540aSrobert   else
796*404b540aSrobert     context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
797*404b540aSrobert 
798*404b540aSrobert   /* A decl in :: or ::std scope is treated specially.  The former is
799*404b540aSrobert      mangled using <unscoped-name> or <unscoped-template-name>, the
800*404b540aSrobert      latter with a special substitution.  Also, a name that is
801*404b540aSrobert      directly in a local function scope is also mangled with
802*404b540aSrobert      <unscoped-name> rather than a full <nested-name>.  */
803*404b540aSrobert   if (context == NULL
804*404b540aSrobert       || context == global_namespace
805*404b540aSrobert       || DECL_NAMESPACE_STD_P (context)
806*404b540aSrobert       || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
807*404b540aSrobert     {
808*404b540aSrobert       tree template_info;
809*404b540aSrobert       /* Is this a template instance?  */
810*404b540aSrobert       if (decl_is_template_id (decl, &template_info))
811*404b540aSrobert 	{
812*404b540aSrobert 	  /* Yes: use <unscoped-template-name>.  */
813*404b540aSrobert 	  write_unscoped_template_name (TI_TEMPLATE (template_info));
814*404b540aSrobert 	  write_template_args (TI_ARGS (template_info));
815*404b540aSrobert 	}
816*404b540aSrobert       else
817*404b540aSrobert 	/* Everything else gets an <unqualified-name>.  */
818*404b540aSrobert 	write_unscoped_name (decl);
819*404b540aSrobert     }
820*404b540aSrobert   else
821*404b540aSrobert     {
822*404b540aSrobert       /* Handle local names, unless we asked not to (that is, invoked
823*404b540aSrobert 	 under <local-name>, to handle only the part of the name under
824*404b540aSrobert 	 the local scope).  */
825*404b540aSrobert       if (!ignore_local_scope)
826*404b540aSrobert 	{
827*404b540aSrobert 	  /* Scan up the list of scope context, looking for a
828*404b540aSrobert 	     function.  If we find one, this entity is in local
829*404b540aSrobert 	     function scope.  local_entity tracks context one scope
830*404b540aSrobert 	     level down, so it will contain the element that's
831*404b540aSrobert 	     directly in that function's scope, either decl or one of
832*404b540aSrobert 	     its enclosing scopes.  */
833*404b540aSrobert 	  tree local_entity = decl;
834*404b540aSrobert 	  while (context != NULL && context != global_namespace)
835*404b540aSrobert 	    {
836*404b540aSrobert 	      /* Make sure we're always dealing with decls.  */
837*404b540aSrobert 	      if (context != NULL && TYPE_P (context))
838*404b540aSrobert 		context = TYPE_NAME (context);
839*404b540aSrobert 	      /* Is this a function?  */
840*404b540aSrobert 	      if (TREE_CODE (context) == FUNCTION_DECL)
841*404b540aSrobert 		{
842*404b540aSrobert 		  /* Yes, we have local scope.  Use the <local-name>
843*404b540aSrobert 		     production for the innermost function scope.  */
844*404b540aSrobert 		  write_local_name (context, local_entity, decl);
845*404b540aSrobert 		  return;
846*404b540aSrobert 		}
847*404b540aSrobert 	      /* Up one scope level.  */
848*404b540aSrobert 	      local_entity = context;
849*404b540aSrobert 	      context = CP_DECL_CONTEXT (context);
850*404b540aSrobert 	    }
851*404b540aSrobert 
852*404b540aSrobert 	  /* No local scope found?  Fall through to <nested-name>.  */
853*404b540aSrobert 	}
854*404b540aSrobert 
855*404b540aSrobert       /* Other decls get a <nested-name> to encode their scope.  */
856*404b540aSrobert       write_nested_name (decl);
857*404b540aSrobert     }
858*404b540aSrobert }
859*404b540aSrobert 
860*404b540aSrobert /* <unscoped-name> ::= <unqualified-name>
861*404b540aSrobert 		   ::= St <unqualified-name>   # ::std::  */
862*404b540aSrobert 
863*404b540aSrobert static void
write_unscoped_name(const tree decl)864*404b540aSrobert write_unscoped_name (const tree decl)
865*404b540aSrobert {
866*404b540aSrobert   tree context = CP_DECL_CONTEXT (decl);
867*404b540aSrobert 
868*404b540aSrobert   MANGLE_TRACE_TREE ("unscoped-name", decl);
869*404b540aSrobert 
870*404b540aSrobert   /* Is DECL in ::std?  */
871*404b540aSrobert   if (DECL_NAMESPACE_STD_P (context))
872*404b540aSrobert     {
873*404b540aSrobert       write_string ("St");
874*404b540aSrobert       write_unqualified_name (decl);
875*404b540aSrobert     }
876*404b540aSrobert   else
877*404b540aSrobert     {
878*404b540aSrobert       /* If not, it should be either in the global namespace, or directly
879*404b540aSrobert 	 in a local function scope.  */
880*404b540aSrobert       gcc_assert (context == global_namespace
881*404b540aSrobert 		  || context == NULL
882*404b540aSrobert 		  || TREE_CODE (context) == FUNCTION_DECL);
883*404b540aSrobert 
884*404b540aSrobert       write_unqualified_name (decl);
885*404b540aSrobert     }
886*404b540aSrobert }
887*404b540aSrobert 
888*404b540aSrobert /* <unscoped-template-name> ::= <unscoped-name>
889*404b540aSrobert 			    ::= <substitution>  */
890*404b540aSrobert 
891*404b540aSrobert static void
write_unscoped_template_name(const tree decl)892*404b540aSrobert write_unscoped_template_name (const tree decl)
893*404b540aSrobert {
894*404b540aSrobert   MANGLE_TRACE_TREE ("unscoped-template-name", decl);
895*404b540aSrobert 
896*404b540aSrobert   if (find_substitution (decl))
897*404b540aSrobert     return;
898*404b540aSrobert   write_unscoped_name (decl);
899*404b540aSrobert   add_substitution (decl);
900*404b540aSrobert }
901*404b540aSrobert 
902*404b540aSrobert /* Write the nested name, including CV-qualifiers, of DECL.
903*404b540aSrobert 
904*404b540aSrobert    <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
905*404b540aSrobert 		 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
906*404b540aSrobert 
907*404b540aSrobert    <CV-qualifiers> ::= [r] [V] [K]  */
908*404b540aSrobert 
909*404b540aSrobert static void
write_nested_name(const tree decl)910*404b540aSrobert write_nested_name (const tree decl)
911*404b540aSrobert {
912*404b540aSrobert   tree template_info;
913*404b540aSrobert 
914*404b540aSrobert   MANGLE_TRACE_TREE ("nested-name", decl);
915*404b540aSrobert 
916*404b540aSrobert   write_char ('N');
917*404b540aSrobert 
918*404b540aSrobert   /* Write CV-qualifiers, if this is a member function.  */
919*404b540aSrobert   if (TREE_CODE (decl) == FUNCTION_DECL
920*404b540aSrobert       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
921*404b540aSrobert     {
922*404b540aSrobert       if (DECL_VOLATILE_MEMFUNC_P (decl))
923*404b540aSrobert 	write_char ('V');
924*404b540aSrobert       if (DECL_CONST_MEMFUNC_P (decl))
925*404b540aSrobert 	write_char ('K');
926*404b540aSrobert     }
927*404b540aSrobert 
928*404b540aSrobert   /* Is this a template instance?  */
929*404b540aSrobert   if (decl_is_template_id (decl, &template_info))
930*404b540aSrobert     {
931*404b540aSrobert       /* Yes, use <template-prefix>.  */
932*404b540aSrobert       write_template_prefix (decl);
933*404b540aSrobert       write_template_args (TI_ARGS (template_info));
934*404b540aSrobert     }
935*404b540aSrobert   else
936*404b540aSrobert     {
937*404b540aSrobert       /* No, just use <prefix>  */
938*404b540aSrobert       write_prefix (DECL_CONTEXT (decl));
939*404b540aSrobert       write_unqualified_name (decl);
940*404b540aSrobert     }
941*404b540aSrobert   write_char ('E');
942*404b540aSrobert }
943*404b540aSrobert 
944*404b540aSrobert /* <prefix> ::= <prefix> <unqualified-name>
945*404b540aSrobert 	    ::= <template-param>
946*404b540aSrobert 	    ::= <template-prefix> <template-args>
947*404b540aSrobert 	    ::= # empty
948*404b540aSrobert 	    ::= <substitution>  */
949*404b540aSrobert 
950*404b540aSrobert static void
write_prefix(const tree node)951*404b540aSrobert write_prefix (const tree node)
952*404b540aSrobert {
953*404b540aSrobert   tree decl;
954*404b540aSrobert   /* Non-NULL if NODE represents a template-id.  */
955*404b540aSrobert   tree template_info = NULL;
956*404b540aSrobert 
957*404b540aSrobert   MANGLE_TRACE_TREE ("prefix", node);
958*404b540aSrobert 
959*404b540aSrobert   if (node == NULL
960*404b540aSrobert       || node == global_namespace)
961*404b540aSrobert     return;
962*404b540aSrobert 
963*404b540aSrobert   if (find_substitution (node))
964*404b540aSrobert     return;
965*404b540aSrobert 
966*404b540aSrobert   if (DECL_P (node))
967*404b540aSrobert     {
968*404b540aSrobert       /* If this is a function decl, that means we've hit function
969*404b540aSrobert 	 scope, so this prefix must be for a local name.  In this
970*404b540aSrobert 	 case, we're under the <local-name> production, which encodes
971*404b540aSrobert 	 the enclosing function scope elsewhere.  So don't continue
972*404b540aSrobert 	 here.  */
973*404b540aSrobert       if (TREE_CODE (node) == FUNCTION_DECL)
974*404b540aSrobert 	return;
975*404b540aSrobert 
976*404b540aSrobert       decl = node;
977*404b540aSrobert       decl_is_template_id (decl, &template_info);
978*404b540aSrobert     }
979*404b540aSrobert   else
980*404b540aSrobert     {
981*404b540aSrobert       /* Node is a type.  */
982*404b540aSrobert       decl = TYPE_NAME (node);
983*404b540aSrobert       if (CLASSTYPE_TEMPLATE_ID_P (node))
984*404b540aSrobert 	template_info = TYPE_TEMPLATE_INFO (node);
985*404b540aSrobert     }
986*404b540aSrobert 
987*404b540aSrobert   /* In G++ 3.2, the name of the template parameter was used.  */
988*404b540aSrobert   if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
989*404b540aSrobert       && !abi_version_at_least (2))
990*404b540aSrobert     G.need_abi_warning = true;
991*404b540aSrobert 
992*404b540aSrobert   if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
993*404b540aSrobert       && abi_version_at_least (2))
994*404b540aSrobert     write_template_param (node);
995*404b540aSrobert   else if (template_info != NULL)
996*404b540aSrobert     /* Templated.  */
997*404b540aSrobert     {
998*404b540aSrobert       write_template_prefix (decl);
999*404b540aSrobert       write_template_args (TI_ARGS (template_info));
1000*404b540aSrobert     }
1001*404b540aSrobert   else
1002*404b540aSrobert     /* Not templated.  */
1003*404b540aSrobert     {
1004*404b540aSrobert       write_prefix (CP_DECL_CONTEXT (decl));
1005*404b540aSrobert       write_unqualified_name (decl);
1006*404b540aSrobert     }
1007*404b540aSrobert 
1008*404b540aSrobert   add_substitution (node);
1009*404b540aSrobert }
1010*404b540aSrobert 
1011*404b540aSrobert /* <template-prefix> ::= <prefix> <template component>
1012*404b540aSrobert 		     ::= <template-param>
1013*404b540aSrobert 		     ::= <substitution>  */
1014*404b540aSrobert 
1015*404b540aSrobert static void
write_template_prefix(const tree node)1016*404b540aSrobert write_template_prefix (const tree node)
1017*404b540aSrobert {
1018*404b540aSrobert   tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1019*404b540aSrobert   tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1020*404b540aSrobert   tree context = CP_DECL_CONTEXT (decl);
1021*404b540aSrobert   tree template_info;
1022*404b540aSrobert   tree template;
1023*404b540aSrobert   tree substitution;
1024*404b540aSrobert 
1025*404b540aSrobert   MANGLE_TRACE_TREE ("template-prefix", node);
1026*404b540aSrobert 
1027*404b540aSrobert   /* Find the template decl.  */
1028*404b540aSrobert   if (decl_is_template_id (decl, &template_info))
1029*404b540aSrobert     template = TI_TEMPLATE (template_info);
1030*404b540aSrobert   else
1031*404b540aSrobert     {
1032*404b540aSrobert       gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1033*404b540aSrobert 
1034*404b540aSrobert       template = TYPE_TI_TEMPLATE (type);
1035*404b540aSrobert     }
1036*404b540aSrobert 
1037*404b540aSrobert   /* For a member template, though, the template name for the
1038*404b540aSrobert      innermost name must have all the outer template levels
1039*404b540aSrobert      instantiated.  For instance, consider
1040*404b540aSrobert 
1041*404b540aSrobert        template<typename T> struct Outer {
1042*404b540aSrobert 	 template<typename U> struct Inner {};
1043*404b540aSrobert        };
1044*404b540aSrobert 
1045*404b540aSrobert      The template name for `Inner' in `Outer<int>::Inner<float>' is
1046*404b540aSrobert      `Outer<int>::Inner<U>'.  In g++, we don't instantiate the template
1047*404b540aSrobert      levels separately, so there's no TEMPLATE_DECL available for this
1048*404b540aSrobert      (there's only `Outer<T>::Inner<U>').
1049*404b540aSrobert 
1050*404b540aSrobert      In order to get the substitutions right, we create a special
1051*404b540aSrobert      TREE_LIST to represent the substitution candidate for a nested
1052*404b540aSrobert      template.  The TREE_PURPOSE is the template's context, fully
1053*404b540aSrobert      instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1054*404b540aSrobert      template.
1055*404b540aSrobert 
1056*404b540aSrobert      So, for the example above, `Outer<int>::Inner' is represented as a
1057*404b540aSrobert      substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1058*404b540aSrobert      and whose value is `Outer<T>::Inner<U>'.  */
1059*404b540aSrobert   if (TYPE_P (context))
1060*404b540aSrobert     substitution = build_tree_list (context, template);
1061*404b540aSrobert   else
1062*404b540aSrobert     substitution = template;
1063*404b540aSrobert 
1064*404b540aSrobert   if (find_substitution (substitution))
1065*404b540aSrobert     return;
1066*404b540aSrobert 
1067*404b540aSrobert   /* In G++ 3.2, the name of the template template parameter was used.  */
1068*404b540aSrobert   if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1069*404b540aSrobert       && !abi_version_at_least (2))
1070*404b540aSrobert     G.need_abi_warning = true;
1071*404b540aSrobert 
1072*404b540aSrobert   if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1073*404b540aSrobert       && abi_version_at_least (2))
1074*404b540aSrobert     write_template_param (TREE_TYPE (template));
1075*404b540aSrobert   else
1076*404b540aSrobert     {
1077*404b540aSrobert       write_prefix (context);
1078*404b540aSrobert       write_unqualified_name (decl);
1079*404b540aSrobert     }
1080*404b540aSrobert 
1081*404b540aSrobert   add_substitution (substitution);
1082*404b540aSrobert }
1083*404b540aSrobert 
1084*404b540aSrobert /* We don't need to handle thunks, vtables, or VTTs here.  Those are
1085*404b540aSrobert    mangled through special entry points.
1086*404b540aSrobert 
1087*404b540aSrobert     <unqualified-name>  ::= <operator-name>
1088*404b540aSrobert 			::= <special-name>
1089*404b540aSrobert 			::= <source-name>  */
1090*404b540aSrobert 
1091*404b540aSrobert static void
write_unqualified_name(const tree decl)1092*404b540aSrobert write_unqualified_name (const tree decl)
1093*404b540aSrobert {
1094*404b540aSrobert   MANGLE_TRACE_TREE ("unqualified-name", decl);
1095*404b540aSrobert 
1096*404b540aSrobert   if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
1097*404b540aSrobert     write_special_name_constructor (decl);
1098*404b540aSrobert   else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
1099*404b540aSrobert     write_special_name_destructor (decl);
1100*404b540aSrobert   else if (DECL_NAME (decl) == NULL_TREE)
1101*404b540aSrobert     write_source_name (DECL_ASSEMBLER_NAME (decl));
1102*404b540aSrobert   else if (DECL_CONV_FN_P (decl))
1103*404b540aSrobert     {
1104*404b540aSrobert       /* Conversion operator. Handle it right here.
1105*404b540aSrobert 	   <operator> ::= cv <type>  */
1106*404b540aSrobert       tree type;
1107*404b540aSrobert       if (decl_is_template_id (decl, NULL))
1108*404b540aSrobert 	{
1109*404b540aSrobert 	  tree fn_type;
1110*404b540aSrobert 	  save_partially_mangled_name ();
1111*404b540aSrobert 	  fn_type = get_mostly_instantiated_function_type (decl);
1112*404b540aSrobert 	  restore_partially_mangled_name ();
1113*404b540aSrobert 	  type = TREE_TYPE (fn_type);
1114*404b540aSrobert 	}
1115*404b540aSrobert       else
1116*404b540aSrobert 	type = DECL_CONV_FN_TYPE (decl);
1117*404b540aSrobert       write_conversion_operator_name (type);
1118*404b540aSrobert     }
1119*404b540aSrobert   else if (DECL_OVERLOADED_OPERATOR_P (decl))
1120*404b540aSrobert     {
1121*404b540aSrobert       operator_name_info_t *oni;
1122*404b540aSrobert       if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1123*404b540aSrobert 	oni = assignment_operator_name_info;
1124*404b540aSrobert       else
1125*404b540aSrobert 	oni = operator_name_info;
1126*404b540aSrobert 
1127*404b540aSrobert       write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1128*404b540aSrobert     }
1129*404b540aSrobert   else
1130*404b540aSrobert     write_source_name (DECL_NAME (decl));
1131*404b540aSrobert }
1132*404b540aSrobert 
1133*404b540aSrobert /* Write the unqualified-name for a conversion operator to TYPE.  */
1134*404b540aSrobert 
1135*404b540aSrobert static void
write_conversion_operator_name(const tree type)1136*404b540aSrobert write_conversion_operator_name (const tree type)
1137*404b540aSrobert {
1138*404b540aSrobert   write_string ("cv");
1139*404b540aSrobert   write_type (type);
1140*404b540aSrobert }
1141*404b540aSrobert 
1142*404b540aSrobert /* Non-terminal <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.
1143*404b540aSrobert 
1144*404b540aSrobert      <source-name> ::= </length/ number> <identifier>  */
1145*404b540aSrobert 
1146*404b540aSrobert static void
write_source_name(tree identifier)1147*404b540aSrobert write_source_name (tree identifier)
1148*404b540aSrobert {
1149*404b540aSrobert   MANGLE_TRACE_TREE ("source-name", identifier);
1150*404b540aSrobert 
1151*404b540aSrobert   /* Never write the whole template-id name including the template
1152*404b540aSrobert      arguments; we only want the template name.  */
1153*404b540aSrobert   if (IDENTIFIER_TEMPLATE (identifier))
1154*404b540aSrobert     identifier = IDENTIFIER_TEMPLATE (identifier);
1155*404b540aSrobert 
1156*404b540aSrobert   write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1157*404b540aSrobert   write_identifier (IDENTIFIER_POINTER (identifier));
1158*404b540aSrobert }
1159*404b540aSrobert 
1160*404b540aSrobert /* Convert NUMBER to ascii using base BASE and generating at least
1161*404b540aSrobert    MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1162*404b540aSrobert    into which to store the characters. Returns the number of
1163*404b540aSrobert    characters generated (these will be layed out in advance of where
1164*404b540aSrobert    BUFFER points).  */
1165*404b540aSrobert 
1166*404b540aSrobert static int
hwint_to_ascii(unsigned HOST_WIDE_INT number,const unsigned int base,char * buffer,const unsigned int min_digits)1167*404b540aSrobert hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1168*404b540aSrobert 		char *buffer, const unsigned int min_digits)
1169*404b540aSrobert {
1170*404b540aSrobert   static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1171*404b540aSrobert   unsigned digits = 0;
1172*404b540aSrobert 
1173*404b540aSrobert   while (number)
1174*404b540aSrobert     {
1175*404b540aSrobert       unsigned HOST_WIDE_INT d = number / base;
1176*404b540aSrobert 
1177*404b540aSrobert       *--buffer = base_digits[number - d * base];
1178*404b540aSrobert       digits++;
1179*404b540aSrobert       number = d;
1180*404b540aSrobert     }
1181*404b540aSrobert   while (digits < min_digits)
1182*404b540aSrobert     {
1183*404b540aSrobert       *--buffer = base_digits[0];
1184*404b540aSrobert       digits++;
1185*404b540aSrobert     }
1186*404b540aSrobert   return digits;
1187*404b540aSrobert }
1188*404b540aSrobert 
1189*404b540aSrobert /* Non-terminal <number>.
1190*404b540aSrobert 
1191*404b540aSrobert      <number> ::= [n] </decimal integer/>  */
1192*404b540aSrobert 
1193*404b540aSrobert static void
write_number(unsigned HOST_WIDE_INT number,const int unsigned_p,const unsigned int base)1194*404b540aSrobert write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1195*404b540aSrobert 	      const unsigned int base)
1196*404b540aSrobert {
1197*404b540aSrobert   char buffer[sizeof (HOST_WIDE_INT) * 8];
1198*404b540aSrobert   unsigned count = 0;
1199*404b540aSrobert 
1200*404b540aSrobert   if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1201*404b540aSrobert     {
1202*404b540aSrobert       write_char ('n');
1203*404b540aSrobert       number = -((HOST_WIDE_INT) number);
1204*404b540aSrobert     }
1205*404b540aSrobert   count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1206*404b540aSrobert   write_chars (buffer + sizeof (buffer) - count, count);
1207*404b540aSrobert }
1208*404b540aSrobert 
1209*404b540aSrobert /* Write out an integral CST in decimal. Most numbers are small, and
1210*404b540aSrobert    representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1211*404b540aSrobert    bigger than that, which we must deal with.  */
1212*404b540aSrobert 
1213*404b540aSrobert static inline void
write_integer_cst(const tree cst)1214*404b540aSrobert write_integer_cst (const tree cst)
1215*404b540aSrobert {
1216*404b540aSrobert   int sign = tree_int_cst_sgn (cst);
1217*404b540aSrobert 
1218*404b540aSrobert   if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1219*404b540aSrobert     {
1220*404b540aSrobert       /* A bignum. We do this in chunks, each of which fits in a
1221*404b540aSrobert 	 HOST_WIDE_INT.  */
1222*404b540aSrobert       char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1223*404b540aSrobert       unsigned HOST_WIDE_INT chunk;
1224*404b540aSrobert       unsigned chunk_digits;
1225*404b540aSrobert       char *ptr = buffer + sizeof (buffer);
1226*404b540aSrobert       unsigned count = 0;
1227*404b540aSrobert       tree n, base, type;
1228*404b540aSrobert       int done;
1229*404b540aSrobert 
1230*404b540aSrobert       /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1231*404b540aSrobert 	 representable.  */
1232*404b540aSrobert       chunk = 1000000000;
1233*404b540aSrobert       chunk_digits = 9;
1234*404b540aSrobert 
1235*404b540aSrobert       if (sizeof (HOST_WIDE_INT) >= 8)
1236*404b540aSrobert 	{
1237*404b540aSrobert 	  /* It is at least 64 bits, so 10^18 is representable.  */
1238*404b540aSrobert 	  chunk_digits = 18;
1239*404b540aSrobert 	  chunk *= chunk;
1240*404b540aSrobert 	}
1241*404b540aSrobert 
1242*404b540aSrobert       type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1243*404b540aSrobert       base = build_int_cstu (type, chunk);
1244*404b540aSrobert       n = build_int_cst_wide (type,
1245*404b540aSrobert 			      TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1246*404b540aSrobert 
1247*404b540aSrobert       if (sign < 0)
1248*404b540aSrobert 	{
1249*404b540aSrobert 	  write_char ('n');
1250*404b540aSrobert 	  n = fold_build1 (NEGATE_EXPR, type, n);
1251*404b540aSrobert 	}
1252*404b540aSrobert       do
1253*404b540aSrobert 	{
1254*404b540aSrobert 	  tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
1255*404b540aSrobert 	  tree tmp = fold_build2 (MULT_EXPR, type, d, base);
1256*404b540aSrobert 	  unsigned c;
1257*404b540aSrobert 
1258*404b540aSrobert 	  done = integer_zerop (d);
1259*404b540aSrobert 	  tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
1260*404b540aSrobert 	  c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1261*404b540aSrobert 			      done ? 1 : chunk_digits);
1262*404b540aSrobert 	  ptr -= c;
1263*404b540aSrobert 	  count += c;
1264*404b540aSrobert 	  n = d;
1265*404b540aSrobert 	}
1266*404b540aSrobert       while (!done);
1267*404b540aSrobert       write_chars (ptr, count);
1268*404b540aSrobert     }
1269*404b540aSrobert   else
1270*404b540aSrobert     {
1271*404b540aSrobert       /* A small num.  */
1272*404b540aSrobert       unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1273*404b540aSrobert 
1274*404b540aSrobert       if (sign < 0)
1275*404b540aSrobert 	{
1276*404b540aSrobert 	  write_char ('n');
1277*404b540aSrobert 	  low = -low;
1278*404b540aSrobert 	}
1279*404b540aSrobert       write_unsigned_number (low);
1280*404b540aSrobert     }
1281*404b540aSrobert }
1282*404b540aSrobert 
1283*404b540aSrobert /* Write out a floating-point literal.
1284*404b540aSrobert 
1285*404b540aSrobert     "Floating-point literals are encoded using the bit pattern of the
1286*404b540aSrobert     target processor's internal representation of that number, as a
1287*404b540aSrobert     fixed-length lowercase hexadecimal string, high-order bytes first
1288*404b540aSrobert     (even if the target processor would store low-order bytes first).
1289*404b540aSrobert     The "n" prefix is not used for floating-point literals; the sign
1290*404b540aSrobert     bit is encoded with the rest of the number.
1291*404b540aSrobert 
1292*404b540aSrobert     Here are some examples, assuming the IEEE standard representation
1293*404b540aSrobert     for floating point numbers.  (Spaces are for readability, not
1294*404b540aSrobert     part of the encoding.)
1295*404b540aSrobert 
1296*404b540aSrobert 	1.0f			Lf 3f80 0000 E
1297*404b540aSrobert        -1.0f			Lf bf80 0000 E
1298*404b540aSrobert 	1.17549435e-38f		Lf 0080 0000 E
1299*404b540aSrobert 	1.40129846e-45f		Lf 0000 0001 E
1300*404b540aSrobert 	0.0f			Lf 0000 0000 E"
1301*404b540aSrobert 
1302*404b540aSrobert    Caller is responsible for the Lx and the E.  */
1303*404b540aSrobert static void
write_real_cst(const tree value)1304*404b540aSrobert write_real_cst (const tree value)
1305*404b540aSrobert {
1306*404b540aSrobert   if (abi_version_at_least (2))
1307*404b540aSrobert     {
1308*404b540aSrobert       long target_real[4];  /* largest supported float */
1309*404b540aSrobert       char buffer[9];       /* eight hex digits in a 32-bit number */
1310*404b540aSrobert       int i, limit, dir;
1311*404b540aSrobert 
1312*404b540aSrobert       tree type = TREE_TYPE (value);
1313*404b540aSrobert       int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1314*404b540aSrobert 
1315*404b540aSrobert       real_to_target (target_real, &TREE_REAL_CST (value),
1316*404b540aSrobert 		      TYPE_MODE (type));
1317*404b540aSrobert 
1318*404b540aSrobert       /* The value in target_real is in the target word order,
1319*404b540aSrobert 	 so we must write it out backward if that happens to be
1320*404b540aSrobert 	 little-endian.  write_number cannot be used, it will
1321*404b540aSrobert 	 produce uppercase.  */
1322*404b540aSrobert       if (FLOAT_WORDS_BIG_ENDIAN)
1323*404b540aSrobert 	i = 0, limit = words, dir = 1;
1324*404b540aSrobert       else
1325*404b540aSrobert 	i = words - 1, limit = -1, dir = -1;
1326*404b540aSrobert 
1327*404b540aSrobert       for (; i != limit; i += dir)
1328*404b540aSrobert 	{
1329*404b540aSrobert 	  sprintf (buffer, "%08lx", target_real[i]);
1330*404b540aSrobert 	  write_chars (buffer, 8);
1331*404b540aSrobert 	}
1332*404b540aSrobert     }
1333*404b540aSrobert   else
1334*404b540aSrobert     {
1335*404b540aSrobert       /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1336*404b540aSrobert 	 literally.  Note that compatibility with 3.2 is impossible,
1337*404b540aSrobert 	 because the old floating-point emulator used a different
1338*404b540aSrobert 	 format for REAL_VALUE_TYPE.  */
1339*404b540aSrobert       size_t i;
1340*404b540aSrobert       for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1341*404b540aSrobert 	write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1342*404b540aSrobert 		      /*unsigned_p*/ 1,
1343*404b540aSrobert 		      /*base*/ 16);
1344*404b540aSrobert       G.need_abi_warning = 1;
1345*404b540aSrobert     }
1346*404b540aSrobert }
1347*404b540aSrobert 
1348*404b540aSrobert /* Non-terminal <identifier>.
1349*404b540aSrobert 
1350*404b540aSrobert      <identifier> ::= </unqualified source code identifier>  */
1351*404b540aSrobert 
1352*404b540aSrobert static void
write_identifier(const char * identifier)1353*404b540aSrobert write_identifier (const char *identifier)
1354*404b540aSrobert {
1355*404b540aSrobert   MANGLE_TRACE ("identifier", identifier);
1356*404b540aSrobert   write_string (identifier);
1357*404b540aSrobert }
1358*404b540aSrobert 
1359*404b540aSrobert /* Handle constructor productions of non-terminal <special-name>.
1360*404b540aSrobert    CTOR is a constructor FUNCTION_DECL.
1361*404b540aSrobert 
1362*404b540aSrobert      <special-name> ::= C1   # complete object constructor
1363*404b540aSrobert 		    ::= C2   # base object constructor
1364*404b540aSrobert 		    ::= C3   # complete object allocating constructor
1365*404b540aSrobert 
1366*404b540aSrobert    Currently, allocating constructors are never used.
1367*404b540aSrobert 
1368*404b540aSrobert    We also need to provide mangled names for the maybe-in-charge
1369*404b540aSrobert    constructor, so we treat it here too.  mangle_decl_string will
1370*404b540aSrobert    append *INTERNAL* to that, to make sure we never emit it.  */
1371*404b540aSrobert 
1372*404b540aSrobert static void
write_special_name_constructor(const tree ctor)1373*404b540aSrobert write_special_name_constructor (const tree ctor)
1374*404b540aSrobert {
1375*404b540aSrobert   if (DECL_BASE_CONSTRUCTOR_P (ctor))
1376*404b540aSrobert     write_string ("C2");
1377*404b540aSrobert   else
1378*404b540aSrobert     {
1379*404b540aSrobert       gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1380*404b540aSrobert 		  /* Even though we don't ever emit a definition of
1381*404b540aSrobert 		     the old-style destructor, we still have to
1382*404b540aSrobert 		     consider entities (like static variables) nested
1383*404b540aSrobert 		     inside it.  */
1384*404b540aSrobert 		  || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor));
1385*404b540aSrobert       write_string ("C1");
1386*404b540aSrobert     }
1387*404b540aSrobert }
1388*404b540aSrobert 
1389*404b540aSrobert /* Handle destructor productions of non-terminal <special-name>.
1390*404b540aSrobert    DTOR is a destructor FUNCTION_DECL.
1391*404b540aSrobert 
1392*404b540aSrobert      <special-name> ::= D0 # deleting (in-charge) destructor
1393*404b540aSrobert 		    ::= D1 # complete object (in-charge) destructor
1394*404b540aSrobert 		    ::= D2 # base object (not-in-charge) destructor
1395*404b540aSrobert 
1396*404b540aSrobert    We also need to provide mangled names for the maybe-incharge
1397*404b540aSrobert    destructor, so we treat it here too.  mangle_decl_string will
1398*404b540aSrobert    append *INTERNAL* to that, to make sure we never emit it.  */
1399*404b540aSrobert 
1400*404b540aSrobert static void
write_special_name_destructor(const tree dtor)1401*404b540aSrobert write_special_name_destructor (const tree dtor)
1402*404b540aSrobert {
1403*404b540aSrobert   if (DECL_DELETING_DESTRUCTOR_P (dtor))
1404*404b540aSrobert     write_string ("D0");
1405*404b540aSrobert   else if (DECL_BASE_DESTRUCTOR_P (dtor))
1406*404b540aSrobert     write_string ("D2");
1407*404b540aSrobert   else
1408*404b540aSrobert     {
1409*404b540aSrobert       gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1410*404b540aSrobert 		  /* Even though we don't ever emit a definition of
1411*404b540aSrobert 		     the old-style destructor, we still have to
1412*404b540aSrobert 		     consider entities (like static variables) nested
1413*404b540aSrobert 		     inside it.  */
1414*404b540aSrobert 		  || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1415*404b540aSrobert       write_string ("D1");
1416*404b540aSrobert     }
1417*404b540aSrobert }
1418*404b540aSrobert 
1419*404b540aSrobert /* Return the discriminator for ENTITY appearing inside
1420*404b540aSrobert    FUNCTION.  The discriminator is the lexical ordinal of VAR among
1421*404b540aSrobert    entities with the same name in the same FUNCTION.  */
1422*404b540aSrobert 
1423*404b540aSrobert static int
discriminator_for_local_entity(tree entity)1424*404b540aSrobert discriminator_for_local_entity (tree entity)
1425*404b540aSrobert {
1426*404b540aSrobert   /* Assume this is the only local entity with this name.  */
1427*404b540aSrobert   int discriminator = 0;
1428*404b540aSrobert 
1429*404b540aSrobert   if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1430*404b540aSrobert     discriminator = DECL_DISCRIMINATOR (entity);
1431*404b540aSrobert   else if (TREE_CODE (entity) == TYPE_DECL)
1432*404b540aSrobert     {
1433*404b540aSrobert       int ix;
1434*404b540aSrobert 
1435*404b540aSrobert       /* Scan the list of local classes.  */
1436*404b540aSrobert       entity = TREE_TYPE (entity);
1437*404b540aSrobert       for (ix = 0; ; ix++)
1438*404b540aSrobert 	{
1439*404b540aSrobert 	  tree type = VEC_index (tree, local_classes, ix);
1440*404b540aSrobert 	  if (type == entity)
1441*404b540aSrobert 	    break;
1442*404b540aSrobert 	  if (TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (entity)
1443*404b540aSrobert 	      && TYPE_CONTEXT (type) == TYPE_CONTEXT (entity))
1444*404b540aSrobert 	    ++discriminator;
1445*404b540aSrobert 	}
1446*404b540aSrobert     }
1447*404b540aSrobert 
1448*404b540aSrobert   return discriminator;
1449*404b540aSrobert }
1450*404b540aSrobert 
1451*404b540aSrobert /* Return the discriminator for STRING, a string literal used inside
1452*404b540aSrobert    FUNCTION.  The discriminator is the lexical ordinal of STRING among
1453*404b540aSrobert    string literals used in FUNCTION.  */
1454*404b540aSrobert 
1455*404b540aSrobert static int
discriminator_for_string_literal(tree function ATTRIBUTE_UNUSED,tree string ATTRIBUTE_UNUSED)1456*404b540aSrobert discriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
1457*404b540aSrobert 				  tree string ATTRIBUTE_UNUSED)
1458*404b540aSrobert {
1459*404b540aSrobert   /* For now, we don't discriminate amongst string literals.  */
1460*404b540aSrobert   return 0;
1461*404b540aSrobert }
1462*404b540aSrobert 
1463*404b540aSrobert /*   <discriminator> := _ <number>
1464*404b540aSrobert 
1465*404b540aSrobert    The discriminator is used only for the second and later occurrences
1466*404b540aSrobert    of the same name within a single function. In this case <number> is
1467*404b540aSrobert    n - 2, if this is the nth occurrence, in lexical order.  */
1468*404b540aSrobert 
1469*404b540aSrobert static void
write_discriminator(const int discriminator)1470*404b540aSrobert write_discriminator (const int discriminator)
1471*404b540aSrobert {
1472*404b540aSrobert   /* If discriminator is zero, don't write anything.  Otherwise...  */
1473*404b540aSrobert   if (discriminator > 0)
1474*404b540aSrobert     {
1475*404b540aSrobert       write_char ('_');
1476*404b540aSrobert       write_unsigned_number (discriminator - 1);
1477*404b540aSrobert     }
1478*404b540aSrobert }
1479*404b540aSrobert 
1480*404b540aSrobert /* Mangle the name of a function-scope entity.  FUNCTION is the
1481*404b540aSrobert    FUNCTION_DECL for the enclosing function.  ENTITY is the decl for
1482*404b540aSrobert    the entity itself.  LOCAL_ENTITY is the entity that's directly
1483*404b540aSrobert    scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1484*404b540aSrobert    of ENTITY.
1485*404b540aSrobert 
1486*404b540aSrobert      <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1487*404b540aSrobert 		  := Z <function encoding> E s [<discriminator>]  */
1488*404b540aSrobert 
1489*404b540aSrobert static void
write_local_name(const tree function,const tree local_entity,const tree entity)1490*404b540aSrobert write_local_name (const tree function, const tree local_entity,
1491*404b540aSrobert 		  const tree entity)
1492*404b540aSrobert {
1493*404b540aSrobert   MANGLE_TRACE_TREE ("local-name", entity);
1494*404b540aSrobert 
1495*404b540aSrobert   write_char ('Z');
1496*404b540aSrobert   write_encoding (function);
1497*404b540aSrobert   write_char ('E');
1498*404b540aSrobert   if (TREE_CODE (entity) == STRING_CST)
1499*404b540aSrobert     {
1500*404b540aSrobert       write_char ('s');
1501*404b540aSrobert       write_discriminator (discriminator_for_string_literal (function,
1502*404b540aSrobert 							     entity));
1503*404b540aSrobert     }
1504*404b540aSrobert   else
1505*404b540aSrobert     {
1506*404b540aSrobert       /* Now the <entity name>.  Let write_name know its being called
1507*404b540aSrobert 	 from <local-name>, so it doesn't try to process the enclosing
1508*404b540aSrobert 	 function scope again.  */
1509*404b540aSrobert       write_name (entity, /*ignore_local_scope=*/1);
1510*404b540aSrobert       write_discriminator (discriminator_for_local_entity (local_entity));
1511*404b540aSrobert     }
1512*404b540aSrobert }
1513*404b540aSrobert 
1514*404b540aSrobert /* Non-terminals <type> and <CV-qualifier>.
1515*404b540aSrobert 
1516*404b540aSrobert      <type> ::= <builtin-type>
1517*404b540aSrobert 	    ::= <function-type>
1518*404b540aSrobert 	    ::= <class-enum-type>
1519*404b540aSrobert 	    ::= <array-type>
1520*404b540aSrobert 	    ::= <pointer-to-member-type>
1521*404b540aSrobert 	    ::= <template-param>
1522*404b540aSrobert 	    ::= <substitution>
1523*404b540aSrobert 	    ::= <CV-qualifier>
1524*404b540aSrobert 	    ::= P <type>    # pointer-to
1525*404b540aSrobert 	    ::= R <type>    # reference-to
1526*404b540aSrobert 	    ::= C <type>    # complex pair (C 2000)
1527*404b540aSrobert 	    ::= G <type>    # imaginary (C 2000)     [not supported]
1528*404b540aSrobert 	    ::= U <source-name> <type>   # vendor extended type qualifier
1529*404b540aSrobert 
1530*404b540aSrobert    TYPE is a type node.  */
1531*404b540aSrobert 
1532*404b540aSrobert static void
write_type(tree type)1533*404b540aSrobert write_type (tree type)
1534*404b540aSrobert {
1535*404b540aSrobert   /* This gets set to nonzero if TYPE turns out to be a (possibly
1536*404b540aSrobert      CV-qualified) builtin type.  */
1537*404b540aSrobert   int is_builtin_type = 0;
1538*404b540aSrobert 
1539*404b540aSrobert   MANGLE_TRACE_TREE ("type", type);
1540*404b540aSrobert 
1541*404b540aSrobert   if (type == error_mark_node)
1542*404b540aSrobert     return;
1543*404b540aSrobert 
1544*404b540aSrobert   if (find_substitution (type))
1545*404b540aSrobert     return;
1546*404b540aSrobert 
1547*404b540aSrobert   if (write_CV_qualifiers_for_type (type) > 0)
1548*404b540aSrobert     /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1549*404b540aSrobert        mangle the unqualified type.  The recursive call is needed here
1550*404b540aSrobert        since both the qualified and unqualified types are substitution
1551*404b540aSrobert        candidates.  */
1552*404b540aSrobert     write_type (TYPE_MAIN_VARIANT (type));
1553*404b540aSrobert   else if (TREE_CODE (type) == ARRAY_TYPE)
1554*404b540aSrobert     /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1555*404b540aSrobert        so that the cv-qualification of the element type is available
1556*404b540aSrobert        in write_array_type.  */
1557*404b540aSrobert     write_array_type (type);
1558*404b540aSrobert   else
1559*404b540aSrobert     {
1560*404b540aSrobert       /* See through any typedefs.  */
1561*404b540aSrobert       type = TYPE_MAIN_VARIANT (type);
1562*404b540aSrobert 
1563*404b540aSrobert       if (TYPE_PTRMEM_P (type))
1564*404b540aSrobert 	write_pointer_to_member_type (type);
1565*404b540aSrobert       else switch (TREE_CODE (type))
1566*404b540aSrobert 	{
1567*404b540aSrobert 	case VOID_TYPE:
1568*404b540aSrobert 	case BOOLEAN_TYPE:
1569*404b540aSrobert 	case INTEGER_TYPE:  /* Includes wchar_t.  */
1570*404b540aSrobert 	case REAL_TYPE:
1571*404b540aSrobert 	{
1572*404b540aSrobert 	  /* Handle any target-specific fundamental types.  */
1573*404b540aSrobert 	  const char *target_mangling
1574*404b540aSrobert 	    = targetm.mangle_fundamental_type (type);
1575*404b540aSrobert 
1576*404b540aSrobert 	  if (target_mangling)
1577*404b540aSrobert 	    {
1578*404b540aSrobert 	      write_string (target_mangling);
1579*404b540aSrobert 	      return;
1580*404b540aSrobert 	    }
1581*404b540aSrobert 
1582*404b540aSrobert 	  /* If this is a typedef, TYPE may not be one of
1583*404b540aSrobert 	     the standard builtin type nodes, but an alias of one.  Use
1584*404b540aSrobert 	     TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
1585*404b540aSrobert 	  write_builtin_type (TYPE_MAIN_VARIANT (type));
1586*404b540aSrobert 	  ++is_builtin_type;
1587*404b540aSrobert 	  break;
1588*404b540aSrobert 	}
1589*404b540aSrobert 
1590*404b540aSrobert 	case COMPLEX_TYPE:
1591*404b540aSrobert 	  write_char ('C');
1592*404b540aSrobert 	  write_type (TREE_TYPE (type));
1593*404b540aSrobert 	  break;
1594*404b540aSrobert 
1595*404b540aSrobert 	case FUNCTION_TYPE:
1596*404b540aSrobert 	case METHOD_TYPE:
1597*404b540aSrobert 	  write_function_type (type);
1598*404b540aSrobert 	  break;
1599*404b540aSrobert 
1600*404b540aSrobert 	case UNION_TYPE:
1601*404b540aSrobert 	case RECORD_TYPE:
1602*404b540aSrobert 	case ENUMERAL_TYPE:
1603*404b540aSrobert 	  /* A pointer-to-member function is represented as a special
1604*404b540aSrobert 	     RECORD_TYPE, so check for this first.  */
1605*404b540aSrobert 	  if (TYPE_PTRMEMFUNC_P (type))
1606*404b540aSrobert 	    write_pointer_to_member_type (type);
1607*404b540aSrobert 	  else
1608*404b540aSrobert 	    write_class_enum_type (type);
1609*404b540aSrobert 	  break;
1610*404b540aSrobert 
1611*404b540aSrobert 	case TYPENAME_TYPE:
1612*404b540aSrobert 	case UNBOUND_CLASS_TEMPLATE:
1613*404b540aSrobert 	  /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1614*404b540aSrobert 	     ordinary nested names.  */
1615*404b540aSrobert 	  write_nested_name (TYPE_STUB_DECL (type));
1616*404b540aSrobert 	  break;
1617*404b540aSrobert 
1618*404b540aSrobert 	case POINTER_TYPE:
1619*404b540aSrobert 	  write_char ('P');
1620*404b540aSrobert 	  write_type (TREE_TYPE (type));
1621*404b540aSrobert 	  break;
1622*404b540aSrobert 
1623*404b540aSrobert 	case REFERENCE_TYPE:
1624*404b540aSrobert 	  write_char ('R');
1625*404b540aSrobert 	  write_type (TREE_TYPE (type));
1626*404b540aSrobert 	  break;
1627*404b540aSrobert 
1628*404b540aSrobert 	case TEMPLATE_TYPE_PARM:
1629*404b540aSrobert 	case TEMPLATE_PARM_INDEX:
1630*404b540aSrobert 	  write_template_param (type);
1631*404b540aSrobert 	  break;
1632*404b540aSrobert 
1633*404b540aSrobert 	case TEMPLATE_TEMPLATE_PARM:
1634*404b540aSrobert 	  write_template_template_param (type);
1635*404b540aSrobert 	  break;
1636*404b540aSrobert 
1637*404b540aSrobert 	case BOUND_TEMPLATE_TEMPLATE_PARM:
1638*404b540aSrobert 	  write_template_template_param (type);
1639*404b540aSrobert 	  write_template_args
1640*404b540aSrobert 	    (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1641*404b540aSrobert 	  break;
1642*404b540aSrobert 
1643*404b540aSrobert 	case VECTOR_TYPE:
1644*404b540aSrobert 	  write_string ("U8__vector");
1645*404b540aSrobert 	  write_type (TREE_TYPE (type));
1646*404b540aSrobert 	  break;
1647*404b540aSrobert 
1648*404b540aSrobert 	default:
1649*404b540aSrobert 	  gcc_unreachable ();
1650*404b540aSrobert 	}
1651*404b540aSrobert     }
1652*404b540aSrobert 
1653*404b540aSrobert   /* Types other than builtin types are substitution candidates.  */
1654*404b540aSrobert   if (!is_builtin_type)
1655*404b540aSrobert     add_substitution (type);
1656*404b540aSrobert }
1657*404b540aSrobert 
1658*404b540aSrobert /* Non-terminal <CV-qualifiers> for type nodes.  Returns the number of
1659*404b540aSrobert    CV-qualifiers written for TYPE.
1660*404b540aSrobert 
1661*404b540aSrobert      <CV-qualifiers> ::= [r] [V] [K]  */
1662*404b540aSrobert 
1663*404b540aSrobert static int
write_CV_qualifiers_for_type(const tree type)1664*404b540aSrobert write_CV_qualifiers_for_type (const tree type)
1665*404b540aSrobert {
1666*404b540aSrobert   int num_qualifiers = 0;
1667*404b540aSrobert 
1668*404b540aSrobert   /* The order is specified by:
1669*404b540aSrobert 
1670*404b540aSrobert        "In cases where multiple order-insensitive qualifiers are
1671*404b540aSrobert        present, they should be ordered 'K' (closest to the base type),
1672*404b540aSrobert        'V', 'r', and 'U' (farthest from the base type) ..."
1673*404b540aSrobert 
1674*404b540aSrobert      Note that we do not use cp_type_quals below; given "const
1675*404b540aSrobert      int[3]", the "const" is emitted with the "int", not with the
1676*404b540aSrobert      array.  */
1677*404b540aSrobert 
1678*404b540aSrobert   if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1679*404b540aSrobert     {
1680*404b540aSrobert       write_char ('r');
1681*404b540aSrobert       ++num_qualifiers;
1682*404b540aSrobert     }
1683*404b540aSrobert   if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1684*404b540aSrobert     {
1685*404b540aSrobert       write_char ('V');
1686*404b540aSrobert       ++num_qualifiers;
1687*404b540aSrobert     }
1688*404b540aSrobert   if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1689*404b540aSrobert     {
1690*404b540aSrobert       write_char ('K');
1691*404b540aSrobert       ++num_qualifiers;
1692*404b540aSrobert     }
1693*404b540aSrobert 
1694*404b540aSrobert   return num_qualifiers;
1695*404b540aSrobert }
1696*404b540aSrobert 
1697*404b540aSrobert /* Non-terminal <builtin-type>.
1698*404b540aSrobert 
1699*404b540aSrobert      <builtin-type> ::= v   # void
1700*404b540aSrobert 		    ::= b   # bool
1701*404b540aSrobert 		    ::= w   # wchar_t
1702*404b540aSrobert 		    ::= c   # char
1703*404b540aSrobert 		    ::= a   # signed char
1704*404b540aSrobert 		    ::= h   # unsigned char
1705*404b540aSrobert 		    ::= s   # short
1706*404b540aSrobert 		    ::= t   # unsigned short
1707*404b540aSrobert 		    ::= i   # int
1708*404b540aSrobert 		    ::= j   # unsigned int
1709*404b540aSrobert 		    ::= l   # long
1710*404b540aSrobert 		    ::= m   # unsigned long
1711*404b540aSrobert 		    ::= x   # long long, __int64
1712*404b540aSrobert 		    ::= y   # unsigned long long, __int64
1713*404b540aSrobert 		    ::= n   # __int128
1714*404b540aSrobert 		    ::= o   # unsigned __int128
1715*404b540aSrobert 		    ::= f   # float
1716*404b540aSrobert 		    ::= d   # double
1717*404b540aSrobert 		    ::= e   # long double, __float80
1718*404b540aSrobert 		    ::= g   # __float128          [not supported]
1719*404b540aSrobert 		    ::= u <source-name>  # vendor extended type */
1720*404b540aSrobert 
1721*404b540aSrobert static void
write_builtin_type(tree type)1722*404b540aSrobert write_builtin_type (tree type)
1723*404b540aSrobert {
1724*404b540aSrobert   switch (TREE_CODE (type))
1725*404b540aSrobert     {
1726*404b540aSrobert     case VOID_TYPE:
1727*404b540aSrobert       write_char ('v');
1728*404b540aSrobert       break;
1729*404b540aSrobert 
1730*404b540aSrobert     case BOOLEAN_TYPE:
1731*404b540aSrobert       write_char ('b');
1732*404b540aSrobert       break;
1733*404b540aSrobert 
1734*404b540aSrobert     case INTEGER_TYPE:
1735*404b540aSrobert       /* If this is size_t, get the underlying int type.  */
1736*404b540aSrobert       if (TYPE_IS_SIZETYPE (type))
1737*404b540aSrobert 	type = TYPE_DOMAIN (type);
1738*404b540aSrobert 
1739*404b540aSrobert       /* TYPE may still be wchar_t, since that isn't in
1740*404b540aSrobert 	 integer_type_nodes.  */
1741*404b540aSrobert       if (type == wchar_type_node)
1742*404b540aSrobert 	write_char ('w');
1743*404b540aSrobert       else if (TYPE_FOR_JAVA (type))
1744*404b540aSrobert 	write_java_integer_type_codes (type);
1745*404b540aSrobert       else
1746*404b540aSrobert 	{
1747*404b540aSrobert 	  size_t itk;
1748*404b540aSrobert 	  /* Assume TYPE is one of the shared integer type nodes.  Find
1749*404b540aSrobert 	     it in the array of these nodes.  */
1750*404b540aSrobert 	iagain:
1751*404b540aSrobert 	  for (itk = 0; itk < itk_none; ++itk)
1752*404b540aSrobert 	    if (type == integer_types[itk])
1753*404b540aSrobert 	      {
1754*404b540aSrobert 		/* Print the corresponding single-letter code.  */
1755*404b540aSrobert 		write_char (integer_type_codes[itk]);
1756*404b540aSrobert 		break;
1757*404b540aSrobert 	      }
1758*404b540aSrobert 
1759*404b540aSrobert 	  if (itk == itk_none)
1760*404b540aSrobert 	    {
1761*404b540aSrobert 	      tree t = c_common_type_for_mode (TYPE_MODE (type),
1762*404b540aSrobert 					       TYPE_UNSIGNED (type));
1763*404b540aSrobert 	      if (type != t)
1764*404b540aSrobert 		{
1765*404b540aSrobert 		  type = t;
1766*404b540aSrobert 		  goto iagain;
1767*404b540aSrobert 		}
1768*404b540aSrobert 
1769*404b540aSrobert 	      if (TYPE_PRECISION (type) == 128)
1770*404b540aSrobert 		write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
1771*404b540aSrobert 	      else
1772*404b540aSrobert 		{
1773*404b540aSrobert 		  /* Allow for cases where TYPE is not one of the shared
1774*404b540aSrobert 		     integer type nodes and write a "vendor extended builtin
1775*404b540aSrobert 		     type" with a name the form intN or uintN, respectively.
1776*404b540aSrobert 		     Situations like this can happen if you have an
1777*404b540aSrobert 		     __attribute__((__mode__(__SI__))) type and use exotic
1778*404b540aSrobert 		     switches like '-mint8' on AVR.  Of course, this is
1779*404b540aSrobert 		     undefined by the C++ ABI (and '-mint8' is not even
1780*404b540aSrobert 		     Standard C conforming), but when using such special
1781*404b540aSrobert 		     options you're pretty much in nowhere land anyway.  */
1782*404b540aSrobert 		  const char *prefix;
1783*404b540aSrobert 		  char prec[11];	/* up to ten digits for an unsigned */
1784*404b540aSrobert 
1785*404b540aSrobert 		  prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
1786*404b540aSrobert 		  sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
1787*404b540aSrobert 		  write_char ('u');	/* "vendor extended builtin type" */
1788*404b540aSrobert 		  write_unsigned_number (strlen (prefix) + strlen (prec));
1789*404b540aSrobert 		  write_string (prefix);
1790*404b540aSrobert 		  write_string (prec);
1791*404b540aSrobert 		}
1792*404b540aSrobert 	    }
1793*404b540aSrobert 	}
1794*404b540aSrobert       break;
1795*404b540aSrobert 
1796*404b540aSrobert     case REAL_TYPE:
1797*404b540aSrobert       if (type == float_type_node
1798*404b540aSrobert 	  || type == java_float_type_node)
1799*404b540aSrobert 	write_char ('f');
1800*404b540aSrobert       else if (type == double_type_node
1801*404b540aSrobert 	       || type == java_double_type_node)
1802*404b540aSrobert 	write_char ('d');
1803*404b540aSrobert       else if (type == long_double_type_node)
1804*404b540aSrobert 	write_char ('e');
1805*404b540aSrobert       else
1806*404b540aSrobert 	gcc_unreachable ();
1807*404b540aSrobert       break;
1808*404b540aSrobert 
1809*404b540aSrobert     default:
1810*404b540aSrobert       gcc_unreachable ();
1811*404b540aSrobert     }
1812*404b540aSrobert }
1813*404b540aSrobert 
1814*404b540aSrobert /* Non-terminal <function-type>.  NODE is a FUNCTION_TYPE or
1815*404b540aSrobert    METHOD_TYPE.  The return type is mangled before the parameter
1816*404b540aSrobert    types.
1817*404b540aSrobert 
1818*404b540aSrobert      <function-type> ::= F [Y] <bare-function-type> E   */
1819*404b540aSrobert 
1820*404b540aSrobert static void
write_function_type(const tree type)1821*404b540aSrobert write_function_type (const tree type)
1822*404b540aSrobert {
1823*404b540aSrobert   MANGLE_TRACE_TREE ("function-type", type);
1824*404b540aSrobert 
1825*404b540aSrobert   /* For a pointer to member function, the function type may have
1826*404b540aSrobert      cv-qualifiers, indicating the quals for the artificial 'this'
1827*404b540aSrobert      parameter.  */
1828*404b540aSrobert   if (TREE_CODE (type) == METHOD_TYPE)
1829*404b540aSrobert     {
1830*404b540aSrobert       /* The first parameter must be a POINTER_TYPE pointing to the
1831*404b540aSrobert 	 `this' parameter.  */
1832*404b540aSrobert       tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1833*404b540aSrobert       write_CV_qualifiers_for_type (this_type);
1834*404b540aSrobert     }
1835*404b540aSrobert 
1836*404b540aSrobert   write_char ('F');
1837*404b540aSrobert   /* We don't track whether or not a type is `extern "C"'.  Note that
1838*404b540aSrobert      you can have an `extern "C"' function that does not have
1839*404b540aSrobert      `extern "C"' type, and vice versa:
1840*404b540aSrobert 
1841*404b540aSrobert        extern "C" typedef void function_t();
1842*404b540aSrobert        function_t f; // f has C++ linkage, but its type is
1843*404b540aSrobert 		     // `extern "C"'
1844*404b540aSrobert 
1845*404b540aSrobert        typedef void function_t();
1846*404b540aSrobert        extern "C" function_t f; // Vice versa.
1847*404b540aSrobert 
1848*404b540aSrobert      See [dcl.link].  */
1849*404b540aSrobert   write_bare_function_type (type, /*include_return_type_p=*/1,
1850*404b540aSrobert 			    /*decl=*/NULL);
1851*404b540aSrobert   write_char ('E');
1852*404b540aSrobert }
1853*404b540aSrobert 
1854*404b540aSrobert /* Non-terminal <bare-function-type>.  TYPE is a FUNCTION_TYPE or
1855*404b540aSrobert    METHOD_TYPE.  If INCLUDE_RETURN_TYPE is nonzero, the return value
1856*404b540aSrobert    is mangled before the parameter types.  If non-NULL, DECL is
1857*404b540aSrobert    FUNCTION_DECL for the function whose type is being emitted.
1858*404b540aSrobert 
1859*404b540aSrobert    If DECL is a member of a Java type, then a literal 'J'
1860*404b540aSrobert    is output and the return type is mangled as if INCLUDE_RETURN_TYPE
1861*404b540aSrobert    were nonzero.
1862*404b540aSrobert 
1863*404b540aSrobert      <bare-function-type> ::= [J]</signature/ type>+  */
1864*404b540aSrobert 
1865*404b540aSrobert static void
write_bare_function_type(const tree type,const int include_return_type_p,const tree decl)1866*404b540aSrobert write_bare_function_type (const tree type, const int include_return_type_p,
1867*404b540aSrobert 			  const tree decl)
1868*404b540aSrobert {
1869*404b540aSrobert   int java_method_p;
1870*404b540aSrobert 
1871*404b540aSrobert   MANGLE_TRACE_TREE ("bare-function-type", type);
1872*404b540aSrobert 
1873*404b540aSrobert   /* Detect Java methods and emit special encoding.  */
1874*404b540aSrobert   if (decl != NULL
1875*404b540aSrobert       && DECL_FUNCTION_MEMBER_P (decl)
1876*404b540aSrobert       && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
1877*404b540aSrobert       && !DECL_CONSTRUCTOR_P (decl)
1878*404b540aSrobert       && !DECL_DESTRUCTOR_P (decl)
1879*404b540aSrobert       && !DECL_CONV_FN_P (decl))
1880*404b540aSrobert     {
1881*404b540aSrobert       java_method_p = 1;
1882*404b540aSrobert       write_char ('J');
1883*404b540aSrobert     }
1884*404b540aSrobert   else
1885*404b540aSrobert     {
1886*404b540aSrobert       java_method_p = 0;
1887*404b540aSrobert     }
1888*404b540aSrobert 
1889*404b540aSrobert   /* Mangle the return type, if requested.  */
1890*404b540aSrobert   if (include_return_type_p || java_method_p)
1891*404b540aSrobert     write_type (TREE_TYPE (type));
1892*404b540aSrobert 
1893*404b540aSrobert   /* Now mangle the types of the arguments.  */
1894*404b540aSrobert   write_method_parms (TYPE_ARG_TYPES (type),
1895*404b540aSrobert 		      TREE_CODE (type) == METHOD_TYPE,
1896*404b540aSrobert 		      decl);
1897*404b540aSrobert }
1898*404b540aSrobert 
1899*404b540aSrobert /* Write the mangled representation of a method parameter list of
1900*404b540aSrobert    types given in PARM_TYPES.  If METHOD_P is nonzero, the function is
1901*404b540aSrobert    considered a non-static method, and the this parameter is omitted.
1902*404b540aSrobert    If non-NULL, DECL is the FUNCTION_DECL for the function whose
1903*404b540aSrobert    parameters are being emitted.  */
1904*404b540aSrobert 
1905*404b540aSrobert static void
write_method_parms(tree parm_types,const int method_p,const tree decl)1906*404b540aSrobert write_method_parms (tree parm_types, const int method_p, const tree decl)
1907*404b540aSrobert {
1908*404b540aSrobert   tree first_parm_type;
1909*404b540aSrobert   tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1910*404b540aSrobert 
1911*404b540aSrobert   /* Assume this parameter type list is variable-length.  If it ends
1912*404b540aSrobert      with a void type, then it's not.  */
1913*404b540aSrobert   int varargs_p = 1;
1914*404b540aSrobert 
1915*404b540aSrobert   /* If this is a member function, skip the first arg, which is the
1916*404b540aSrobert      this pointer.
1917*404b540aSrobert        "Member functions do not encode the type of their implicit this
1918*404b540aSrobert        parameter."
1919*404b540aSrobert 
1920*404b540aSrobert      Similarly, there's no need to mangle artificial parameters, like
1921*404b540aSrobert      the VTT parameters for constructors and destructors.  */
1922*404b540aSrobert   if (method_p)
1923*404b540aSrobert     {
1924*404b540aSrobert       parm_types = TREE_CHAIN (parm_types);
1925*404b540aSrobert       parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1926*404b540aSrobert 
1927*404b540aSrobert       while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1928*404b540aSrobert 	{
1929*404b540aSrobert 	  parm_types = TREE_CHAIN (parm_types);
1930*404b540aSrobert 	  parm_decl = TREE_CHAIN (parm_decl);
1931*404b540aSrobert 	}
1932*404b540aSrobert     }
1933*404b540aSrobert 
1934*404b540aSrobert   for (first_parm_type = parm_types;
1935*404b540aSrobert        parm_types;
1936*404b540aSrobert        parm_types = TREE_CHAIN (parm_types))
1937*404b540aSrobert     {
1938*404b540aSrobert       tree parm = TREE_VALUE (parm_types);
1939*404b540aSrobert       if (parm == void_type_node)
1940*404b540aSrobert 	{
1941*404b540aSrobert 	  /* "Empty parameter lists, whether declared as () or
1942*404b540aSrobert 	     conventionally as (void), are encoded with a void parameter
1943*404b540aSrobert 	     (v)."  */
1944*404b540aSrobert 	  if (parm_types == first_parm_type)
1945*404b540aSrobert 	    write_type (parm);
1946*404b540aSrobert 	  /* If the parm list is terminated with a void type, it's
1947*404b540aSrobert 	     fixed-length.  */
1948*404b540aSrobert 	  varargs_p = 0;
1949*404b540aSrobert 	  /* A void type better be the last one.  */
1950*404b540aSrobert 	  gcc_assert (TREE_CHAIN (parm_types) == NULL);
1951*404b540aSrobert 	}
1952*404b540aSrobert       else
1953*404b540aSrobert 	write_type (parm);
1954*404b540aSrobert     }
1955*404b540aSrobert 
1956*404b540aSrobert   if (varargs_p)
1957*404b540aSrobert     /* <builtin-type> ::= z  # ellipsis  */
1958*404b540aSrobert     write_char ('z');
1959*404b540aSrobert }
1960*404b540aSrobert 
1961*404b540aSrobert /* <class-enum-type> ::= <name>  */
1962*404b540aSrobert 
1963*404b540aSrobert static void
write_class_enum_type(const tree type)1964*404b540aSrobert write_class_enum_type (const tree type)
1965*404b540aSrobert {
1966*404b540aSrobert   write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1967*404b540aSrobert }
1968*404b540aSrobert 
1969*404b540aSrobert /* Non-terminal <template-args>.  ARGS is a TREE_VEC of template
1970*404b540aSrobert    arguments.
1971*404b540aSrobert 
1972*404b540aSrobert      <template-args> ::= I <template-arg>+ E  */
1973*404b540aSrobert 
1974*404b540aSrobert static void
write_template_args(tree args)1975*404b540aSrobert write_template_args (tree args)
1976*404b540aSrobert {
1977*404b540aSrobert   int i;
1978*404b540aSrobert   int length = TREE_VEC_LENGTH (args);
1979*404b540aSrobert 
1980*404b540aSrobert   MANGLE_TRACE_TREE ("template-args", args);
1981*404b540aSrobert 
1982*404b540aSrobert   write_char ('I');
1983*404b540aSrobert 
1984*404b540aSrobert   gcc_assert (length > 0);
1985*404b540aSrobert 
1986*404b540aSrobert   if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1987*404b540aSrobert     {
1988*404b540aSrobert       /* We have nested template args.  We want the innermost template
1989*404b540aSrobert 	 argument list.  */
1990*404b540aSrobert       args = TREE_VEC_ELT (args, length - 1);
1991*404b540aSrobert       length = TREE_VEC_LENGTH (args);
1992*404b540aSrobert     }
1993*404b540aSrobert   for (i = 0; i < length; ++i)
1994*404b540aSrobert     write_template_arg (TREE_VEC_ELT (args, i));
1995*404b540aSrobert 
1996*404b540aSrobert   write_char ('E');
1997*404b540aSrobert }
1998*404b540aSrobert 
1999*404b540aSrobert /* <expression> ::= <unary operator-name> <expression>
2000*404b540aSrobert 		::= <binary operator-name> <expression> <expression>
2001*404b540aSrobert 		::= <expr-primary>
2002*404b540aSrobert 
2003*404b540aSrobert    <expr-primary> ::= <template-param>
2004*404b540aSrobert 		  ::= L <type> <value number> E		# literal
2005*404b540aSrobert 		  ::= L <mangled-name> E		# external name
2006*404b540aSrobert 		  ::= sr <type> <unqualified-name>
2007*404b540aSrobert 		  ::= sr <type> <unqualified-name> <template-args> */
2008*404b540aSrobert 
2009*404b540aSrobert static void
write_expression(tree expr)2010*404b540aSrobert write_expression (tree expr)
2011*404b540aSrobert {
2012*404b540aSrobert   enum tree_code code;
2013*404b540aSrobert 
2014*404b540aSrobert   code = TREE_CODE (expr);
2015*404b540aSrobert 
2016*404b540aSrobert   /* Skip NOP_EXPRs.  They can occur when (say) a pointer argument
2017*404b540aSrobert      is converted (via qualification conversions) to another
2018*404b540aSrobert      type.  */
2019*404b540aSrobert   while (TREE_CODE (expr) == NOP_EXPR
2020*404b540aSrobert 	 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2021*404b540aSrobert     {
2022*404b540aSrobert       expr = TREE_OPERAND (expr, 0);
2023*404b540aSrobert       code = TREE_CODE (expr);
2024*404b540aSrobert     }
2025*404b540aSrobert 
2026*404b540aSrobert   if (code == BASELINK)
2027*404b540aSrobert     {
2028*404b540aSrobert       expr = BASELINK_FUNCTIONS (expr);
2029*404b540aSrobert       code = TREE_CODE (expr);
2030*404b540aSrobert     }
2031*404b540aSrobert 
2032*404b540aSrobert   /* Handle pointers-to-members by making them look like expression
2033*404b540aSrobert      nodes.  */
2034*404b540aSrobert   if (code == PTRMEM_CST)
2035*404b540aSrobert     {
2036*404b540aSrobert       expr = build_nt (ADDR_EXPR,
2037*404b540aSrobert 		       build_qualified_name (/*type=*/NULL_TREE,
2038*404b540aSrobert 					     PTRMEM_CST_CLASS (expr),
2039*404b540aSrobert 					     PTRMEM_CST_MEMBER (expr),
2040*404b540aSrobert 					     /*template_p=*/false));
2041*404b540aSrobert       code = TREE_CODE (expr);
2042*404b540aSrobert     }
2043*404b540aSrobert 
2044*404b540aSrobert   /* Handle template parameters.  */
2045*404b540aSrobert   if (code == TEMPLATE_TYPE_PARM
2046*404b540aSrobert       || code == TEMPLATE_TEMPLATE_PARM
2047*404b540aSrobert       || code == BOUND_TEMPLATE_TEMPLATE_PARM
2048*404b540aSrobert       || code == TEMPLATE_PARM_INDEX)
2049*404b540aSrobert     write_template_param (expr);
2050*404b540aSrobert   /* Handle literals.  */
2051*404b540aSrobert   else if (TREE_CODE_CLASS (code) == tcc_constant
2052*404b540aSrobert 	   || (abi_version_at_least (2) && code == CONST_DECL))
2053*404b540aSrobert     write_template_arg_literal (expr);
2054*404b540aSrobert   else if (DECL_P (expr))
2055*404b540aSrobert     {
2056*404b540aSrobert       /* G++ 3.2 incorrectly mangled non-type template arguments of
2057*404b540aSrobert 	 enumeration type using their names.  */
2058*404b540aSrobert       if (code == CONST_DECL)
2059*404b540aSrobert 	G.need_abi_warning = 1;
2060*404b540aSrobert       write_char ('L');
2061*404b540aSrobert       write_mangled_name (expr, false);
2062*404b540aSrobert       write_char ('E');
2063*404b540aSrobert     }
2064*404b540aSrobert   else if (TREE_CODE (expr) == SIZEOF_EXPR
2065*404b540aSrobert 	   && TYPE_P (TREE_OPERAND (expr, 0)))
2066*404b540aSrobert     {
2067*404b540aSrobert       write_string ("st");
2068*404b540aSrobert       write_type (TREE_OPERAND (expr, 0));
2069*404b540aSrobert     }
2070*404b540aSrobert   else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
2071*404b540aSrobert     {
2072*404b540aSrobert       tree scope = TREE_OPERAND (expr, 0);
2073*404b540aSrobert       tree member = TREE_OPERAND (expr, 1);
2074*404b540aSrobert 
2075*404b540aSrobert       /* If the MEMBER is a real declaration, then the qualifying
2076*404b540aSrobert 	 scope was not dependent.  Ideally, we would not have a
2077*404b540aSrobert 	 SCOPE_REF in those cases, but sometimes we do.  If the second
2078*404b540aSrobert 	 argument is a DECL, then the name must not have been
2079*404b540aSrobert 	 dependent.  */
2080*404b540aSrobert       if (DECL_P (member))
2081*404b540aSrobert 	write_expression (member);
2082*404b540aSrobert       else
2083*404b540aSrobert 	{
2084*404b540aSrobert 	  tree template_args;
2085*404b540aSrobert 
2086*404b540aSrobert 	  write_string ("sr");
2087*404b540aSrobert 	  write_type (scope);
2088*404b540aSrobert 	  /* If MEMBER is a template-id, separate the template
2089*404b540aSrobert 	     from the arguments.  */
2090*404b540aSrobert 	  if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2091*404b540aSrobert 	    {
2092*404b540aSrobert 	      template_args = TREE_OPERAND (member, 1);
2093*404b540aSrobert 	      member = TREE_OPERAND (member, 0);
2094*404b540aSrobert 	    }
2095*404b540aSrobert 	  else
2096*404b540aSrobert 	    template_args = NULL_TREE;
2097*404b540aSrobert 	  /* Write out the name of the MEMBER.  */
2098*404b540aSrobert 	  if (IDENTIFIER_TYPENAME_P (member))
2099*404b540aSrobert 	    write_conversion_operator_name (TREE_TYPE (member));
2100*404b540aSrobert 	  else if (IDENTIFIER_OPNAME_P (member))
2101*404b540aSrobert 	    {
2102*404b540aSrobert 	      int i;
2103*404b540aSrobert 	      const char *mangled_name = NULL;
2104*404b540aSrobert 
2105*404b540aSrobert 	      /* Unfortunately, there is no easy way to go from the
2106*404b540aSrobert 		 name of the operator back to the corresponding tree
2107*404b540aSrobert 		 code.  */
2108*404b540aSrobert 	      for (i = 0; i < LAST_CPLUS_TREE_CODE; ++i)
2109*404b540aSrobert 		if (operator_name_info[i].identifier == member)
2110*404b540aSrobert 		  {
2111*404b540aSrobert 		    /* The ABI says that we prefer binary operator
2112*404b540aSrobert 		       names to unary operator names.  */
2113*404b540aSrobert 		    if (operator_name_info[i].arity == 2)
2114*404b540aSrobert 		      {
2115*404b540aSrobert 			mangled_name = operator_name_info[i].mangled_name;
2116*404b540aSrobert 			break;
2117*404b540aSrobert 		      }
2118*404b540aSrobert 		    else if (!mangled_name)
2119*404b540aSrobert 		      mangled_name = operator_name_info[i].mangled_name;
2120*404b540aSrobert 		  }
2121*404b540aSrobert 		else if (assignment_operator_name_info[i].identifier
2122*404b540aSrobert 			 == member)
2123*404b540aSrobert 		  {
2124*404b540aSrobert 		    mangled_name
2125*404b540aSrobert 		      = assignment_operator_name_info[i].mangled_name;
2126*404b540aSrobert 		    break;
2127*404b540aSrobert 		  }
2128*404b540aSrobert 	      write_string (mangled_name);
2129*404b540aSrobert 	    }
2130*404b540aSrobert 	  else
2131*404b540aSrobert 	    write_source_name (member);
2132*404b540aSrobert 	  /* Write out the template arguments.  */
2133*404b540aSrobert 	  if (template_args)
2134*404b540aSrobert 	    write_template_args (template_args);
2135*404b540aSrobert 	}
2136*404b540aSrobert     }
2137*404b540aSrobert   else
2138*404b540aSrobert     {
2139*404b540aSrobert       int i;
2140*404b540aSrobert 
2141*404b540aSrobert       /* When we bind a variable or function to a non-type template
2142*404b540aSrobert 	 argument with reference type, we create an ADDR_EXPR to show
2143*404b540aSrobert 	 the fact that the entity's address has been taken.  But, we
2144*404b540aSrobert 	 don't actually want to output a mangling code for the `&'.  */
2145*404b540aSrobert       if (TREE_CODE (expr) == ADDR_EXPR
2146*404b540aSrobert 	  && TREE_TYPE (expr)
2147*404b540aSrobert 	  && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2148*404b540aSrobert 	{
2149*404b540aSrobert 	  expr = TREE_OPERAND (expr, 0);
2150*404b540aSrobert 	  if (DECL_P (expr))
2151*404b540aSrobert 	    {
2152*404b540aSrobert 	      write_expression (expr);
2153*404b540aSrobert 	      return;
2154*404b540aSrobert 	    }
2155*404b540aSrobert 
2156*404b540aSrobert 	  code = TREE_CODE (expr);
2157*404b540aSrobert 	}
2158*404b540aSrobert 
2159*404b540aSrobert       /* If it wasn't any of those, recursively expand the expression.  */
2160*404b540aSrobert       write_string (operator_name_info[(int) code].mangled_name);
2161*404b540aSrobert 
2162*404b540aSrobert       switch (code)
2163*404b540aSrobert 	{
2164*404b540aSrobert 	case CALL_EXPR:
2165*404b540aSrobert 	  sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
2166*404b540aSrobert 	  break;
2167*404b540aSrobert 
2168*404b540aSrobert 	case CAST_EXPR:
2169*404b540aSrobert 	  write_type (TREE_TYPE (expr));
2170*404b540aSrobert 	  /* There is no way to mangle a zero-operand cast like
2171*404b540aSrobert 	     "T()".  */
2172*404b540aSrobert 	  if (!TREE_OPERAND (expr, 0))
2173*404b540aSrobert 	    sorry ("zero-operand casts cannot be mangled due to a defect "
2174*404b540aSrobert 		   "in the C++ ABI");
2175*404b540aSrobert 	  else
2176*404b540aSrobert 	    write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2177*404b540aSrobert 	  break;
2178*404b540aSrobert 
2179*404b540aSrobert 	case STATIC_CAST_EXPR:
2180*404b540aSrobert 	case CONST_CAST_EXPR:
2181*404b540aSrobert 	  write_type (TREE_TYPE (expr));
2182*404b540aSrobert 	  write_expression (TREE_OPERAND (expr, 0));
2183*404b540aSrobert 	  break;
2184*404b540aSrobert 
2185*404b540aSrobert 
2186*404b540aSrobert 	/* Handle pointers-to-members specially.  */
2187*404b540aSrobert 	case SCOPE_REF:
2188*404b540aSrobert 	  write_type (TREE_OPERAND (expr, 0));
2189*404b540aSrobert 	  if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
2190*404b540aSrobert 	    write_source_name (TREE_OPERAND (expr, 1));
2191*404b540aSrobert 	  else if (TREE_CODE (TREE_OPERAND (expr, 1)) == TEMPLATE_ID_EXPR)
2192*404b540aSrobert 	    {
2193*404b540aSrobert 	      tree template_id;
2194*404b540aSrobert 	      tree name;
2195*404b540aSrobert 
2196*404b540aSrobert 	      template_id = TREE_OPERAND (expr, 1);
2197*404b540aSrobert 	      name = TREE_OPERAND (template_id, 0);
2198*404b540aSrobert 	      /* FIXME: What about operators?  */
2199*404b540aSrobert 	      gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2200*404b540aSrobert 	      write_source_name (TREE_OPERAND (template_id, 0));
2201*404b540aSrobert 	      write_template_args (TREE_OPERAND (template_id, 1));
2202*404b540aSrobert 	    }
2203*404b540aSrobert 	  else
2204*404b540aSrobert 	    {
2205*404b540aSrobert 	      /* G++ 3.2 incorrectly put out both the "sr" code and
2206*404b540aSrobert 		 the nested name of the qualified name.  */
2207*404b540aSrobert 	      G.need_abi_warning = 1;
2208*404b540aSrobert 	      write_encoding (TREE_OPERAND (expr, 1));
2209*404b540aSrobert 	    }
2210*404b540aSrobert 	  break;
2211*404b540aSrobert 
2212*404b540aSrobert 	default:
2213*404b540aSrobert 	  for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
2214*404b540aSrobert 	    {
2215*404b540aSrobert 	      tree operand = TREE_OPERAND (expr, i);
2216*404b540aSrobert 	      /* As a GNU extension, the middle operand of a
2217*404b540aSrobert 		 conditional may be omitted.  Since expression
2218*404b540aSrobert 		 manglings are supposed to represent the input token
2219*404b540aSrobert 		 stream, there's no good way to mangle such an
2220*404b540aSrobert 		 expression without extending the C++ ABI.  */
2221*404b540aSrobert 	      if (code == COND_EXPR && i == 1 && !operand)
2222*404b540aSrobert 		{
2223*404b540aSrobert 		  error ("omitted middle operand to %<?:%> operand "
2224*404b540aSrobert 			 "cannot be mangled");
2225*404b540aSrobert 		  continue;
2226*404b540aSrobert 		}
2227*404b540aSrobert 	      write_expression (operand);
2228*404b540aSrobert 	    }
2229*404b540aSrobert 	}
2230*404b540aSrobert     }
2231*404b540aSrobert }
2232*404b540aSrobert 
2233*404b540aSrobert /* Literal subcase of non-terminal <template-arg>.
2234*404b540aSrobert 
2235*404b540aSrobert      "Literal arguments, e.g. "A<42L>", are encoded with their type
2236*404b540aSrobert      and value. Negative integer values are preceded with "n"; for
2237*404b540aSrobert      example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2238*404b540aSrobert      encoded as 0, true as 1."  */
2239*404b540aSrobert 
2240*404b540aSrobert static void
write_template_arg_literal(const tree value)2241*404b540aSrobert write_template_arg_literal (const tree value)
2242*404b540aSrobert {
2243*404b540aSrobert   write_char ('L');
2244*404b540aSrobert   write_type (TREE_TYPE (value));
2245*404b540aSrobert 
2246*404b540aSrobert   switch (TREE_CODE (value))
2247*404b540aSrobert     {
2248*404b540aSrobert     case CONST_DECL:
2249*404b540aSrobert       write_integer_cst (DECL_INITIAL (value));
2250*404b540aSrobert       break;
2251*404b540aSrobert 
2252*404b540aSrobert     case INTEGER_CST:
2253*404b540aSrobert       gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
2254*404b540aSrobert 		  || integer_zerop (value) || integer_onep (value));
2255*404b540aSrobert       write_integer_cst (value);
2256*404b540aSrobert       break;
2257*404b540aSrobert 
2258*404b540aSrobert     case REAL_CST:
2259*404b540aSrobert       write_real_cst (value);
2260*404b540aSrobert       break;
2261*404b540aSrobert 
2262*404b540aSrobert     default:
2263*404b540aSrobert       gcc_unreachable ();
2264*404b540aSrobert     }
2265*404b540aSrobert 
2266*404b540aSrobert   write_char ('E');
2267*404b540aSrobert }
2268*404b540aSrobert 
2269*404b540aSrobert /* Non-terminal <template-arg>.
2270*404b540aSrobert 
2271*404b540aSrobert      <template-arg> ::= <type>				# type
2272*404b540aSrobert 		    ::= L <type> </value/ number> E	# literal
2273*404b540aSrobert 		    ::= LZ <name> E			# external name
2274*404b540aSrobert 		    ::= X <expression> E		# expression  */
2275*404b540aSrobert 
2276*404b540aSrobert static void
write_template_arg(tree node)2277*404b540aSrobert write_template_arg (tree node)
2278*404b540aSrobert {
2279*404b540aSrobert   enum tree_code code = TREE_CODE (node);
2280*404b540aSrobert 
2281*404b540aSrobert   MANGLE_TRACE_TREE ("template-arg", node);
2282*404b540aSrobert 
2283*404b540aSrobert   /* A template template parameter's argument list contains TREE_LIST
2284*404b540aSrobert      nodes of which the value field is the actual argument.  */
2285*404b540aSrobert   if (code == TREE_LIST)
2286*404b540aSrobert     {
2287*404b540aSrobert       node = TREE_VALUE (node);
2288*404b540aSrobert       /* If it's a decl, deal with its type instead.  */
2289*404b540aSrobert       if (DECL_P (node))
2290*404b540aSrobert 	{
2291*404b540aSrobert 	  node = TREE_TYPE (node);
2292*404b540aSrobert 	  code = TREE_CODE (node);
2293*404b540aSrobert 	}
2294*404b540aSrobert     }
2295*404b540aSrobert 
2296*404b540aSrobert   if (TREE_CODE (node) == NOP_EXPR
2297*404b540aSrobert       && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
2298*404b540aSrobert     {
2299*404b540aSrobert       /* Template parameters can be of reference type. To maintain
2300*404b540aSrobert 	 internal consistency, such arguments use a conversion from
2301*404b540aSrobert 	 address of object to reference type.  */
2302*404b540aSrobert       gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
2303*404b540aSrobert       if (abi_version_at_least (2))
2304*404b540aSrobert 	node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
2305*404b540aSrobert       else
2306*404b540aSrobert 	G.need_abi_warning = 1;
2307*404b540aSrobert     }
2308*404b540aSrobert 
2309*404b540aSrobert   if (TYPE_P (node))
2310*404b540aSrobert     write_type (node);
2311*404b540aSrobert   else if (code == TEMPLATE_DECL)
2312*404b540aSrobert     /* A template appearing as a template arg is a template template arg.  */
2313*404b540aSrobert     write_template_template_arg (node);
2314*404b540aSrobert   else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
2315*404b540aSrobert 	   || (abi_version_at_least (2) && code == CONST_DECL))
2316*404b540aSrobert     write_template_arg_literal (node);
2317*404b540aSrobert   else if (DECL_P (node))
2318*404b540aSrobert     {
2319*404b540aSrobert       /* Until ABI version 2, non-type template arguments of
2320*404b540aSrobert 	 enumeration type were mangled using their names.  */
2321*404b540aSrobert       if (code == CONST_DECL && !abi_version_at_least (2))
2322*404b540aSrobert 	G.need_abi_warning = 1;
2323*404b540aSrobert       write_char ('L');
2324*404b540aSrobert       /* Until ABI version 3, the underscore before the mangled name
2325*404b540aSrobert 	 was incorrectly omitted.  */
2326*404b540aSrobert       if (!abi_version_at_least (3))
2327*404b540aSrobert 	{
2328*404b540aSrobert 	  G.need_abi_warning = 1;
2329*404b540aSrobert 	  write_char ('Z');
2330*404b540aSrobert 	}
2331*404b540aSrobert       else
2332*404b540aSrobert 	write_string ("_Z");
2333*404b540aSrobert       write_encoding (node);
2334*404b540aSrobert       write_char ('E');
2335*404b540aSrobert     }
2336*404b540aSrobert   else
2337*404b540aSrobert     {
2338*404b540aSrobert       /* Template arguments may be expressions.  */
2339*404b540aSrobert       write_char ('X');
2340*404b540aSrobert       write_expression (node);
2341*404b540aSrobert       write_char ('E');
2342*404b540aSrobert     }
2343*404b540aSrobert }
2344*404b540aSrobert 
2345*404b540aSrobert /*  <template-template-arg>
2346*404b540aSrobert 			::= <name>
2347*404b540aSrobert 			::= <substitution>  */
2348*404b540aSrobert 
2349*404b540aSrobert static void
write_template_template_arg(const tree decl)2350*404b540aSrobert write_template_template_arg (const tree decl)
2351*404b540aSrobert {
2352*404b540aSrobert   MANGLE_TRACE_TREE ("template-template-arg", decl);
2353*404b540aSrobert 
2354*404b540aSrobert   if (find_substitution (decl))
2355*404b540aSrobert     return;
2356*404b540aSrobert   write_name (decl, /*ignore_local_scope=*/0);
2357*404b540aSrobert   add_substitution (decl);
2358*404b540aSrobert }
2359*404b540aSrobert 
2360*404b540aSrobert 
2361*404b540aSrobert /* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.
2362*404b540aSrobert 
2363*404b540aSrobert      <array-type> ::= A [</dimension/ number>] _ </element/ type>
2364*404b540aSrobert 		  ::= A <expression> _ </element/ type>
2365*404b540aSrobert 
2366*404b540aSrobert      "Array types encode the dimension (number of elements) and the
2367*404b540aSrobert      element type. For variable length arrays, the dimension (but not
2368*404b540aSrobert      the '_' separator) is omitted."  */
2369*404b540aSrobert 
2370*404b540aSrobert static void
write_array_type(const tree type)2371*404b540aSrobert write_array_type (const tree type)
2372*404b540aSrobert {
2373*404b540aSrobert   write_char ('A');
2374*404b540aSrobert   if (TYPE_DOMAIN (type))
2375*404b540aSrobert     {
2376*404b540aSrobert       tree index_type;
2377*404b540aSrobert       tree max;
2378*404b540aSrobert 
2379*404b540aSrobert       index_type = TYPE_DOMAIN (type);
2380*404b540aSrobert       /* The INDEX_TYPE gives the upper and lower bounds of the
2381*404b540aSrobert 	 array.  */
2382*404b540aSrobert       max = TYPE_MAX_VALUE (index_type);
2383*404b540aSrobert       if (TREE_CODE (max) == INTEGER_CST)
2384*404b540aSrobert 	{
2385*404b540aSrobert 	  /* The ABI specifies that we should mangle the number of
2386*404b540aSrobert 	     elements in the array, not the largest allowed index.  */
2387*404b540aSrobert 	  max = size_binop (PLUS_EXPR, max, size_one_node);
2388*404b540aSrobert 	  write_unsigned_number (tree_low_cst (max, 1));
2389*404b540aSrobert 	}
2390*404b540aSrobert       else
2391*404b540aSrobert 	{
2392*404b540aSrobert 	  max = TREE_OPERAND (max, 0);
2393*404b540aSrobert 	  if (!abi_version_at_least (2))
2394*404b540aSrobert 	    {
2395*404b540aSrobert 	      /* value_dependent_expression_p presumes nothing is
2396*404b540aSrobert 		 dependent when PROCESSING_TEMPLATE_DECL is zero.  */
2397*404b540aSrobert 	      ++processing_template_decl;
2398*404b540aSrobert 	      if (!value_dependent_expression_p (max))
2399*404b540aSrobert 		G.need_abi_warning = 1;
2400*404b540aSrobert 	      --processing_template_decl;
2401*404b540aSrobert 	    }
2402*404b540aSrobert 	  write_expression (max);
2403*404b540aSrobert 	}
2404*404b540aSrobert 
2405*404b540aSrobert     }
2406*404b540aSrobert   write_char ('_');
2407*404b540aSrobert   write_type (TREE_TYPE (type));
2408*404b540aSrobert }
2409*404b540aSrobert 
2410*404b540aSrobert /* Non-terminal <pointer-to-member-type> for pointer-to-member
2411*404b540aSrobert    variables.  TYPE is a pointer-to-member POINTER_TYPE.
2412*404b540aSrobert 
2413*404b540aSrobert      <pointer-to-member-type> ::= M </class/ type> </member/ type>  */
2414*404b540aSrobert 
2415*404b540aSrobert static void
write_pointer_to_member_type(const tree type)2416*404b540aSrobert write_pointer_to_member_type (const tree type)
2417*404b540aSrobert {
2418*404b540aSrobert   write_char ('M');
2419*404b540aSrobert   write_type (TYPE_PTRMEM_CLASS_TYPE (type));
2420*404b540aSrobert   write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2421*404b540aSrobert }
2422*404b540aSrobert 
2423*404b540aSrobert /* Non-terminal <template-param>.  PARM is a TEMPLATE_TYPE_PARM,
2424*404b540aSrobert    TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2425*404b540aSrobert    TEMPLATE_PARM_INDEX.
2426*404b540aSrobert 
2427*404b540aSrobert      <template-param> ::= T </parameter/ number> _  */
2428*404b540aSrobert 
2429*404b540aSrobert static void
write_template_param(const tree parm)2430*404b540aSrobert write_template_param (const tree parm)
2431*404b540aSrobert {
2432*404b540aSrobert   int parm_index;
2433*404b540aSrobert   int parm_level;
2434*404b540aSrobert   tree parm_type = NULL_TREE;
2435*404b540aSrobert 
2436*404b540aSrobert   MANGLE_TRACE_TREE ("template-parm", parm);
2437*404b540aSrobert 
2438*404b540aSrobert   switch (TREE_CODE (parm))
2439*404b540aSrobert     {
2440*404b540aSrobert     case TEMPLATE_TYPE_PARM:
2441*404b540aSrobert     case TEMPLATE_TEMPLATE_PARM:
2442*404b540aSrobert     case BOUND_TEMPLATE_TEMPLATE_PARM:
2443*404b540aSrobert       parm_index = TEMPLATE_TYPE_IDX (parm);
2444*404b540aSrobert       parm_level = TEMPLATE_TYPE_LEVEL (parm);
2445*404b540aSrobert       break;
2446*404b540aSrobert 
2447*404b540aSrobert     case TEMPLATE_PARM_INDEX:
2448*404b540aSrobert       parm_index = TEMPLATE_PARM_IDX (parm);
2449*404b540aSrobert       parm_level = TEMPLATE_PARM_LEVEL (parm);
2450*404b540aSrobert       parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
2451*404b540aSrobert       break;
2452*404b540aSrobert 
2453*404b540aSrobert     default:
2454*404b540aSrobert       gcc_unreachable ();
2455*404b540aSrobert     }
2456*404b540aSrobert 
2457*404b540aSrobert   write_char ('T');
2458*404b540aSrobert   /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2459*404b540aSrobert      earliest template param denoted by `_'.  */
2460*404b540aSrobert   if (parm_index > 0)
2461*404b540aSrobert     write_unsigned_number (parm_index - 1);
2462*404b540aSrobert   write_char ('_');
2463*404b540aSrobert }
2464*404b540aSrobert 
2465*404b540aSrobert /*  <template-template-param>
2466*404b540aSrobert 			::= <template-param>
2467*404b540aSrobert 			::= <substitution>  */
2468*404b540aSrobert 
2469*404b540aSrobert static void
write_template_template_param(const tree parm)2470*404b540aSrobert write_template_template_param (const tree parm)
2471*404b540aSrobert {
2472*404b540aSrobert   tree template = NULL_TREE;
2473*404b540aSrobert 
2474*404b540aSrobert   /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2475*404b540aSrobert      template template parameter.  The substitution candidate here is
2476*404b540aSrobert      only the template.  */
2477*404b540aSrobert   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2478*404b540aSrobert     {
2479*404b540aSrobert       template
2480*404b540aSrobert 	= TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2481*404b540aSrobert       if (find_substitution (template))
2482*404b540aSrobert 	return;
2483*404b540aSrobert     }
2484*404b540aSrobert 
2485*404b540aSrobert   /* <template-param> encodes only the template parameter position,
2486*404b540aSrobert      not its template arguments, which is fine here.  */
2487*404b540aSrobert   write_template_param (parm);
2488*404b540aSrobert   if (template)
2489*404b540aSrobert     add_substitution (template);
2490*404b540aSrobert }
2491*404b540aSrobert 
2492*404b540aSrobert /* Non-terminal <substitution>.
2493*404b540aSrobert 
2494*404b540aSrobert       <substitution> ::= S <seq-id> _
2495*404b540aSrobert 		     ::= S_  */
2496*404b540aSrobert 
2497*404b540aSrobert static void
write_substitution(const int seq_id)2498*404b540aSrobert write_substitution (const int seq_id)
2499*404b540aSrobert {
2500*404b540aSrobert   MANGLE_TRACE ("substitution", "");
2501*404b540aSrobert 
2502*404b540aSrobert   write_char ('S');
2503*404b540aSrobert   if (seq_id > 0)
2504*404b540aSrobert     write_number (seq_id - 1, /*unsigned=*/1, 36);
2505*404b540aSrobert   write_char ('_');
2506*404b540aSrobert }
2507*404b540aSrobert 
2508*404b540aSrobert /* Start mangling ENTITY.  */
2509*404b540aSrobert 
2510*404b540aSrobert static inline void
start_mangling(const tree entity,const bool ident_p)2511*404b540aSrobert start_mangling (const tree entity, const bool ident_p)
2512*404b540aSrobert {
2513*404b540aSrobert   G.entity = entity;
2514*404b540aSrobert   G.need_abi_warning = false;
2515*404b540aSrobert   if (!ident_p)
2516*404b540aSrobert     {
2517*404b540aSrobert       obstack_free (&name_obstack, name_base);
2518*404b540aSrobert       mangle_obstack = &name_obstack;
2519*404b540aSrobert       name_base = obstack_alloc (&name_obstack, 0);
2520*404b540aSrobert     }
2521*404b540aSrobert   else
2522*404b540aSrobert     mangle_obstack = &ident_hash->stack;
2523*404b540aSrobert }
2524*404b540aSrobert 
2525*404b540aSrobert /* Done with mangling.  Return the generated mangled name.  If WARN is
2526*404b540aSrobert    true, and the name of G.entity will be mangled differently in a
2527*404b540aSrobert    future version of the ABI, issue a warning.  */
2528*404b540aSrobert 
2529*404b540aSrobert static inline const char *
finish_mangling(const bool warn)2530*404b540aSrobert finish_mangling (const bool warn)
2531*404b540aSrobert {
2532*404b540aSrobert   if (warn_abi && warn && G.need_abi_warning)
2533*404b540aSrobert     warning (OPT_Wabi, "the mangled name of %qD will change in a future "
2534*404b540aSrobert 	     "version of GCC",
2535*404b540aSrobert 	     G.entity);
2536*404b540aSrobert 
2537*404b540aSrobert   /* Clear all the substitutions.  */
2538*404b540aSrobert   VEC_truncate (tree, G.substitutions, 0);
2539*404b540aSrobert 
2540*404b540aSrobert   /* Null-terminate the string.  */
2541*404b540aSrobert   write_char ('\0');
2542*404b540aSrobert 
2543*404b540aSrobert   return (const char *) obstack_finish (mangle_obstack);
2544*404b540aSrobert }
2545*404b540aSrobert 
2546*404b540aSrobert /* Initialize data structures for mangling.  */
2547*404b540aSrobert 
2548*404b540aSrobert void
init_mangle(void)2549*404b540aSrobert init_mangle (void)
2550*404b540aSrobert {
2551*404b540aSrobert   gcc_obstack_init (&name_obstack);
2552*404b540aSrobert   name_base = obstack_alloc (&name_obstack, 0);
2553*404b540aSrobert   G.substitutions = NULL;
2554*404b540aSrobert 
2555*404b540aSrobert   /* Cache these identifiers for quick comparison when checking for
2556*404b540aSrobert      standard substitutions.  */
2557*404b540aSrobert   subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2558*404b540aSrobert   subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2559*404b540aSrobert   subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2560*404b540aSrobert   subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2561*404b540aSrobert   subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2562*404b540aSrobert   subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2563*404b540aSrobert }
2564*404b540aSrobert 
2565*404b540aSrobert /* Generate the mangled name of DECL.  */
2566*404b540aSrobert 
2567*404b540aSrobert static const char *
mangle_decl_string(const tree decl)2568*404b540aSrobert mangle_decl_string (const tree decl)
2569*404b540aSrobert {
2570*404b540aSrobert   const char *result;
2571*404b540aSrobert 
2572*404b540aSrobert   start_mangling (decl, /*ident_p=*/true);
2573*404b540aSrobert 
2574*404b540aSrobert   if (TREE_CODE (decl) == TYPE_DECL)
2575*404b540aSrobert     write_type (TREE_TYPE (decl));
2576*404b540aSrobert   else
2577*404b540aSrobert     write_mangled_name (decl, true);
2578*404b540aSrobert 
2579*404b540aSrobert   result = finish_mangling (/*warn=*/true);
2580*404b540aSrobert   if (DEBUG_MANGLE)
2581*404b540aSrobert     fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2582*404b540aSrobert   return result;
2583*404b540aSrobert }
2584*404b540aSrobert 
2585*404b540aSrobert /* Like get_identifier, except that NAME is assumed to have been
2586*404b540aSrobert    allocated on the obstack used by the identifier hash table.  */
2587*404b540aSrobert 
2588*404b540aSrobert static inline tree
get_identifier_nocopy(const char * name)2589*404b540aSrobert get_identifier_nocopy (const char *name)
2590*404b540aSrobert {
2591*404b540aSrobert   hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name,
2592*404b540aSrobert 				strlen (name), HT_ALLOCED);
2593*404b540aSrobert   return HT_IDENT_TO_GCC_IDENT (ht_node);
2594*404b540aSrobert }
2595*404b540aSrobert 
2596*404b540aSrobert /* Create an identifier for the external mangled name of DECL.  */
2597*404b540aSrobert 
2598*404b540aSrobert void
mangle_decl(const tree decl)2599*404b540aSrobert mangle_decl (const tree decl)
2600*404b540aSrobert {
2601*404b540aSrobert   SET_DECL_ASSEMBLER_NAME (decl,
2602*404b540aSrobert 			   get_identifier_nocopy (mangle_decl_string (decl)));
2603*404b540aSrobert }
2604*404b540aSrobert 
2605*404b540aSrobert /* Generate the mangled representation of TYPE.  */
2606*404b540aSrobert 
2607*404b540aSrobert const char *
mangle_type_string(const tree type)2608*404b540aSrobert mangle_type_string (const tree type)
2609*404b540aSrobert {
2610*404b540aSrobert   const char *result;
2611*404b540aSrobert 
2612*404b540aSrobert   start_mangling (type, /*ident_p=*/false);
2613*404b540aSrobert   write_type (type);
2614*404b540aSrobert   result = finish_mangling (/*warn=*/false);
2615*404b540aSrobert   if (DEBUG_MANGLE)
2616*404b540aSrobert     fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2617*404b540aSrobert   return result;
2618*404b540aSrobert }
2619*404b540aSrobert 
2620*404b540aSrobert /* Create an identifier for the mangled name of a special component
2621*404b540aSrobert    for belonging to TYPE.  CODE is the ABI-specified code for this
2622*404b540aSrobert    component.  */
2623*404b540aSrobert 
2624*404b540aSrobert static tree
mangle_special_for_type(const tree type,const char * code)2625*404b540aSrobert mangle_special_for_type (const tree type, const char *code)
2626*404b540aSrobert {
2627*404b540aSrobert   const char *result;
2628*404b540aSrobert 
2629*404b540aSrobert   /* We don't have an actual decl here for the special component, so
2630*404b540aSrobert      we can't just process the <encoded-name>.  Instead, fake it.  */
2631*404b540aSrobert   start_mangling (type, /*ident_p=*/true);
2632*404b540aSrobert 
2633*404b540aSrobert   /* Start the mangling.  */
2634*404b540aSrobert   write_string ("_Z");
2635*404b540aSrobert   write_string (code);
2636*404b540aSrobert 
2637*404b540aSrobert   /* Add the type.  */
2638*404b540aSrobert   write_type (type);
2639*404b540aSrobert   result = finish_mangling (/*warn=*/false);
2640*404b540aSrobert 
2641*404b540aSrobert   if (DEBUG_MANGLE)
2642*404b540aSrobert     fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2643*404b540aSrobert 
2644*404b540aSrobert   return get_identifier_nocopy (result);
2645*404b540aSrobert }
2646*404b540aSrobert 
2647*404b540aSrobert /* Create an identifier for the mangled representation of the typeinfo
2648*404b540aSrobert    structure for TYPE.  */
2649*404b540aSrobert 
2650*404b540aSrobert tree
mangle_typeinfo_for_type(const tree type)2651*404b540aSrobert mangle_typeinfo_for_type (const tree type)
2652*404b540aSrobert {
2653*404b540aSrobert   return mangle_special_for_type (type, "TI");
2654*404b540aSrobert }
2655*404b540aSrobert 
2656*404b540aSrobert /* Create an identifier for the mangled name of the NTBS containing
2657*404b540aSrobert    the mangled name of TYPE.  */
2658*404b540aSrobert 
2659*404b540aSrobert tree
mangle_typeinfo_string_for_type(const tree type)2660*404b540aSrobert mangle_typeinfo_string_for_type (const tree type)
2661*404b540aSrobert {
2662*404b540aSrobert   return mangle_special_for_type (type, "TS");
2663*404b540aSrobert }
2664*404b540aSrobert 
2665*404b540aSrobert /* Create an identifier for the mangled name of the vtable for TYPE.  */
2666*404b540aSrobert 
2667*404b540aSrobert tree
mangle_vtbl_for_type(const tree type)2668*404b540aSrobert mangle_vtbl_for_type (const tree type)
2669*404b540aSrobert {
2670*404b540aSrobert   return mangle_special_for_type (type, "TV");
2671*404b540aSrobert }
2672*404b540aSrobert 
2673*404b540aSrobert /* Returns an identifier for the mangled name of the VTT for TYPE.  */
2674*404b540aSrobert 
2675*404b540aSrobert tree
mangle_vtt_for_type(const tree type)2676*404b540aSrobert mangle_vtt_for_type (const tree type)
2677*404b540aSrobert {
2678*404b540aSrobert   return mangle_special_for_type (type, "TT");
2679*404b540aSrobert }
2680*404b540aSrobert 
2681*404b540aSrobert /* Return an identifier for a construction vtable group.  TYPE is
2682*404b540aSrobert    the most derived class in the hierarchy; BINFO is the base
2683*404b540aSrobert    subobject for which this construction vtable group will be used.
2684*404b540aSrobert 
2685*404b540aSrobert    This mangling isn't part of the ABI specification; in the ABI
2686*404b540aSrobert    specification, the vtable group is dumped in the same COMDAT as the
2687*404b540aSrobert    main vtable, and is referenced only from that vtable, so it doesn't
2688*404b540aSrobert    need an external name.  For binary formats without COMDAT sections,
2689*404b540aSrobert    though, we need external names for the vtable groups.
2690*404b540aSrobert 
2691*404b540aSrobert    We use the production
2692*404b540aSrobert 
2693*404b540aSrobert     <special-name> ::= CT <type> <offset number> _ <base type>  */
2694*404b540aSrobert 
2695*404b540aSrobert tree
mangle_ctor_vtbl_for_type(const tree type,const tree binfo)2696*404b540aSrobert mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
2697*404b540aSrobert {
2698*404b540aSrobert   const char *result;
2699*404b540aSrobert 
2700*404b540aSrobert   start_mangling (type, /*ident_p=*/true);
2701*404b540aSrobert 
2702*404b540aSrobert   write_string ("_Z");
2703*404b540aSrobert   write_string ("TC");
2704*404b540aSrobert   write_type (type);
2705*404b540aSrobert   write_integer_cst (BINFO_OFFSET (binfo));
2706*404b540aSrobert   write_char ('_');
2707*404b540aSrobert   write_type (BINFO_TYPE (binfo));
2708*404b540aSrobert 
2709*404b540aSrobert   result = finish_mangling (/*warn=*/false);
2710*404b540aSrobert   if (DEBUG_MANGLE)
2711*404b540aSrobert     fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2712*404b540aSrobert   return get_identifier_nocopy (result);
2713*404b540aSrobert }
2714*404b540aSrobert 
2715*404b540aSrobert /* Mangle a this pointer or result pointer adjustment.
2716*404b540aSrobert 
2717*404b540aSrobert    <call-offset> ::= h <fixed offset number> _
2718*404b540aSrobert 		 ::= v <fixed offset number> _ <virtual offset number> _ */
2719*404b540aSrobert 
2720*404b540aSrobert static void
mangle_call_offset(const tree fixed_offset,const tree virtual_offset)2721*404b540aSrobert mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
2722*404b540aSrobert {
2723*404b540aSrobert   write_char (virtual_offset ? 'v' : 'h');
2724*404b540aSrobert 
2725*404b540aSrobert   /* For either flavor, write the fixed offset.  */
2726*404b540aSrobert   write_integer_cst (fixed_offset);
2727*404b540aSrobert   write_char ('_');
2728*404b540aSrobert 
2729*404b540aSrobert   /* For a virtual thunk, add the virtual offset.  */
2730*404b540aSrobert   if (virtual_offset)
2731*404b540aSrobert     {
2732*404b540aSrobert       write_integer_cst (virtual_offset);
2733*404b540aSrobert       write_char ('_');
2734*404b540aSrobert     }
2735*404b540aSrobert }
2736*404b540aSrobert 
2737*404b540aSrobert /* Return an identifier for the mangled name of a this-adjusting or
2738*404b540aSrobert    covariant thunk to FN_DECL.  FIXED_OFFSET is the initial adjustment
2739*404b540aSrobert    to this used to find the vptr.  If VIRTUAL_OFFSET is non-NULL, this
2740*404b540aSrobert    is a virtual thunk, and it is the vtbl offset in
2741*404b540aSrobert    bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
2742*404b540aSrobert    zero for a covariant thunk. Note, that FN_DECL might be a covariant
2743*404b540aSrobert    thunk itself. A covariant thunk name always includes the adjustment
2744*404b540aSrobert    for the this pointer, even if there is none.
2745*404b540aSrobert 
2746*404b540aSrobert    <special-name> ::= T <call-offset> <base encoding>
2747*404b540aSrobert 		  ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2748*404b540aSrobert 					<base encoding>  */
2749*404b540aSrobert 
2750*404b540aSrobert tree
mangle_thunk(tree fn_decl,const int this_adjusting,tree fixed_offset,tree virtual_offset)2751*404b540aSrobert mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
2752*404b540aSrobert 	      tree virtual_offset)
2753*404b540aSrobert {
2754*404b540aSrobert   const char *result;
2755*404b540aSrobert 
2756*404b540aSrobert   start_mangling (fn_decl, /*ident_p=*/true);
2757*404b540aSrobert 
2758*404b540aSrobert   write_string ("_Z");
2759*404b540aSrobert   write_char ('T');
2760*404b540aSrobert 
2761*404b540aSrobert   if (!this_adjusting)
2762*404b540aSrobert     {
2763*404b540aSrobert       /* Covariant thunk with no this adjustment */
2764*404b540aSrobert       write_char ('c');
2765*404b540aSrobert       mangle_call_offset (integer_zero_node, NULL_TREE);
2766*404b540aSrobert       mangle_call_offset (fixed_offset, virtual_offset);
2767*404b540aSrobert     }
2768*404b540aSrobert   else if (!DECL_THUNK_P (fn_decl))
2769*404b540aSrobert     /* Plain this adjusting thunk.  */
2770*404b540aSrobert     mangle_call_offset (fixed_offset, virtual_offset);
2771*404b540aSrobert   else
2772*404b540aSrobert     {
2773*404b540aSrobert       /* This adjusting thunk to covariant thunk.  */
2774*404b540aSrobert       write_char ('c');
2775*404b540aSrobert       mangle_call_offset (fixed_offset, virtual_offset);
2776*404b540aSrobert       fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2777*404b540aSrobert       virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2778*404b540aSrobert       if (virtual_offset)
2779*404b540aSrobert 	virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2780*404b540aSrobert       mangle_call_offset (fixed_offset, virtual_offset);
2781*404b540aSrobert       fn_decl = THUNK_TARGET (fn_decl);
2782*404b540aSrobert     }
2783*404b540aSrobert 
2784*404b540aSrobert   /* Scoped name.  */
2785*404b540aSrobert   write_encoding (fn_decl);
2786*404b540aSrobert 
2787*404b540aSrobert   result = finish_mangling (/*warn=*/false);
2788*404b540aSrobert   if (DEBUG_MANGLE)
2789*404b540aSrobert     fprintf (stderr, "mangle_thunk = %s\n\n", result);
2790*404b540aSrobert   return get_identifier_nocopy (result);
2791*404b540aSrobert }
2792*404b540aSrobert 
2793*404b540aSrobert /* This hash table maps TYPEs to the IDENTIFIER for a conversion
2794*404b540aSrobert    operator to TYPE.  The nodes are IDENTIFIERs whose TREE_TYPE is the
2795*404b540aSrobert    TYPE.  */
2796*404b540aSrobert 
2797*404b540aSrobert static GTY ((param_is (union tree_node))) htab_t conv_type_names;
2798*404b540aSrobert 
2799*404b540aSrobert /* Hash a node (VAL1) in the table.  */
2800*404b540aSrobert 
2801*404b540aSrobert static hashval_t
hash_type(const void * val)2802*404b540aSrobert hash_type (const void *val)
2803*404b540aSrobert {
2804*404b540aSrobert   return (hashval_t) TYPE_UID (TREE_TYPE ((tree) val));
2805*404b540aSrobert }
2806*404b540aSrobert 
2807*404b540aSrobert /* Compare VAL1 (a node in the table) with VAL2 (a TYPE).  */
2808*404b540aSrobert 
2809*404b540aSrobert static int
compare_type(const void * val1,const void * val2)2810*404b540aSrobert compare_type (const void *val1, const void *val2)
2811*404b540aSrobert {
2812*404b540aSrobert   return TREE_TYPE ((tree) val1) == (tree) val2;
2813*404b540aSrobert }
2814*404b540aSrobert 
2815*404b540aSrobert /* Return an identifier for the mangled unqualified name for a
2816*404b540aSrobert    conversion operator to TYPE.  This mangling is not specified by the
2817*404b540aSrobert    ABI spec; it is only used internally.  */
2818*404b540aSrobert 
2819*404b540aSrobert tree
mangle_conv_op_name_for_type(const tree type)2820*404b540aSrobert mangle_conv_op_name_for_type (const tree type)
2821*404b540aSrobert {
2822*404b540aSrobert   void **slot;
2823*404b540aSrobert   tree identifier;
2824*404b540aSrobert 
2825*404b540aSrobert   if (type == error_mark_node)
2826*404b540aSrobert     return error_mark_node;
2827*404b540aSrobert 
2828*404b540aSrobert   if (conv_type_names == NULL)
2829*404b540aSrobert     conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
2830*404b540aSrobert 
2831*404b540aSrobert   slot = htab_find_slot_with_hash (conv_type_names, type,
2832*404b540aSrobert 				   (hashval_t) TYPE_UID (type), INSERT);
2833*404b540aSrobert   identifier = (tree)*slot;
2834*404b540aSrobert   if (!identifier)
2835*404b540aSrobert     {
2836*404b540aSrobert       char buffer[64];
2837*404b540aSrobert 
2838*404b540aSrobert        /* Create a unique name corresponding to TYPE.  */
2839*404b540aSrobert       sprintf (buffer, "operator %lu",
2840*404b540aSrobert 	       (unsigned long) htab_elements (conv_type_names));
2841*404b540aSrobert       identifier = get_identifier (buffer);
2842*404b540aSrobert       *slot = identifier;
2843*404b540aSrobert 
2844*404b540aSrobert       /* Hang TYPE off the identifier so it can be found easily later
2845*404b540aSrobert 	 when performing conversions.  */
2846*404b540aSrobert       TREE_TYPE (identifier) = type;
2847*404b540aSrobert 
2848*404b540aSrobert       /* Set bits on the identifier so we know later it's a conversion.  */
2849*404b540aSrobert       IDENTIFIER_OPNAME_P (identifier) = 1;
2850*404b540aSrobert       IDENTIFIER_TYPENAME_P (identifier) = 1;
2851*404b540aSrobert     }
2852*404b540aSrobert 
2853*404b540aSrobert   return identifier;
2854*404b540aSrobert }
2855*404b540aSrobert 
2856*404b540aSrobert /* Return an identifier for the name of an initialization guard
2857*404b540aSrobert    variable for indicated VARIABLE.  */
2858*404b540aSrobert 
2859*404b540aSrobert tree
mangle_guard_variable(const tree variable)2860*404b540aSrobert mangle_guard_variable (const tree variable)
2861*404b540aSrobert {
2862*404b540aSrobert   start_mangling (variable, /*ident_p=*/true);
2863*404b540aSrobert   write_string ("_ZGV");
2864*404b540aSrobert   if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
2865*404b540aSrobert     /* The name of a guard variable for a reference temporary should refer
2866*404b540aSrobert        to the reference, not the temporary.  */
2867*404b540aSrobert     write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
2868*404b540aSrobert   else
2869*404b540aSrobert     write_name (variable, /*ignore_local_scope=*/0);
2870*404b540aSrobert   return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2871*404b540aSrobert }
2872*404b540aSrobert 
2873*404b540aSrobert /* Return an identifier for the name of a temporary variable used to
2874*404b540aSrobert    initialize a static reference.  This isn't part of the ABI, but we might
2875*404b540aSrobert    as well call them something readable.  */
2876*404b540aSrobert 
2877*404b540aSrobert tree
mangle_ref_init_variable(const tree variable)2878*404b540aSrobert mangle_ref_init_variable (const tree variable)
2879*404b540aSrobert {
2880*404b540aSrobert   start_mangling (variable, /*ident_p=*/true);
2881*404b540aSrobert   write_string ("_ZGR");
2882*404b540aSrobert   write_name (variable, /*ignore_local_scope=*/0);
2883*404b540aSrobert   return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2884*404b540aSrobert }
2885*404b540aSrobert 
2886*404b540aSrobert 
2887*404b540aSrobert /* Foreign language type mangling section.  */
2888*404b540aSrobert 
2889*404b540aSrobert /* How to write the type codes for the integer Java type.  */
2890*404b540aSrobert 
2891*404b540aSrobert static void
write_java_integer_type_codes(const tree type)2892*404b540aSrobert write_java_integer_type_codes (const tree type)
2893*404b540aSrobert {
2894*404b540aSrobert   if (type == java_int_type_node)
2895*404b540aSrobert     write_char ('i');
2896*404b540aSrobert   else if (type == java_short_type_node)
2897*404b540aSrobert     write_char ('s');
2898*404b540aSrobert   else if (type == java_byte_type_node)
2899*404b540aSrobert     write_char ('c');
2900*404b540aSrobert   else if (type == java_char_type_node)
2901*404b540aSrobert     write_char ('w');
2902*404b540aSrobert   else if (type == java_long_type_node)
2903*404b540aSrobert     write_char ('x');
2904*404b540aSrobert   else if (type == java_boolean_type_node)
2905*404b540aSrobert     write_char ('b');
2906*404b540aSrobert   else
2907*404b540aSrobert     gcc_unreachable ();
2908*404b540aSrobert }
2909*404b540aSrobert 
2910*404b540aSrobert #include "gt-cp-mangle.h"
2911