OSDN Git Service

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