OSDN Git Service

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