OSDN Git Service

* cp-tree.h (TYPE_PTRMEM_CLASS_TYPE): Improve documentation.
[pf3gnuchains/gcc-fork.git] / gcc / cp / mangle.c
1 /* Name mangling for the new standard C++ ABI.
2    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3    Written by Alex Samuel <sameul@codesourcery.com>
4
5    This file is part of GNU CC.
6
7    GNU CC 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    GNU CC 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 GNU CC; 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 "tree.h"
53 #include "cp-tree.h"
54 #include "obstack.h"
55 #include "toplev.h"
56 #include "varray.h"
57
58 /* Debugging support.  */
59
60 /* Define DEBUG_MANGLE to enable very verbose trace messages.  */
61 #ifndef DEBUG_MANGLE
62 #define DEBUG_MANGLE 0
63 #endif
64
65 /* Macros for tracing the write_* functions.  */
66 #if DEBUG_MANGLE
67 # define MANGLE_TRACE(FN, INPUT) \
68   fprintf (stderr, "  %-24s: %-24s\n", FN, INPUT)
69 # define MANGLE_TRACE_TREE(FN, NODE) \
70   fprintf (stderr, "  %-24s: %-24s (%p)\n", \
71            FN, tree_code_name[TREE_CODE (NODE)], (void *) NODE)
72 #else
73 # define MANGLE_TRACE(FN, INPUT)
74 # define MANGLE_TRACE_TREE(FN, NODE)
75 #endif
76
77 /* Non-zero if NODE is a class template-id.  We can't rely on
78    CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
79    that hard to distinguish A<T> from A, where A<T> is the type as
80    instantiated outside of the template, and A is the type used
81    without parameters inside the template.  */
82 #define CLASSTYPE_TEMPLATE_ID_P(NODE)                                 \
83   (TYPE_LANG_SPECIFIC (NODE) != NULL                                  \
84    && CLASSTYPE_TEMPLATE_INFO (NODE) != NULL                          \
85    && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))
86
87 /* Things we only need one of.  This module is not reentrant.  */
88 static struct globals
89 {
90   /* The name in which we're building the mangled name.  */
91   struct obstack name_obstack;
92
93   /* An array of the current substitution candidates, in the order
94      we've seen them.  */
95   varray_type substitutions;
96 } G;
97
98 /* Indices into subst_identifiers.  These are identifiers used in
99    special substitution rules.  */
100 typedef enum
101 {
102   SUBID_ALLOCATOR,
103   SUBID_BASIC_STRING,
104   SUBID_CHAR_TRAITS,
105   SUBID_BASIC_ISTREAM,
106   SUBID_BASIC_OSTREAM,
107   SUBID_BASIC_IOSTREAM,
108   SUBID_MAX
109 }
110 substitution_identifier_index_t;
111
112 /* For quick substitution checks, look up these common identifiers
113    once only.  */
114 static tree subst_identifiers[SUBID_MAX];
115
116 /* Single-letter codes for builtin integer types, defined in
117    <builtin-type>.  These are indexed by integer_type_kind values.  */
118 static char
119 integer_type_codes[itk_none] =
120 {
121   'c',  /* itk_char */
122   'a',  /* itk_signed_char */
123   'h',  /* itk_unsigned_char */
124   's',  /* itk_short */
125   't',  /* itk_unsigned_short */
126   'i',  /* itk_int */
127   'j',  /* itk_unsigned_int */
128   'l',  /* itk_long */
129   'm',  /* itk_unsigned_long */
130   'x',  /* itk_long_long */
131   'y'   /* itk_unsigned_long_long */
132 };
133
134 static int decl_is_template_id PARAMS ((tree, tree*));
135
136 /* Functions for handling substitutions.  */
137
138 static inline tree canonicalize_for_substitution PARAMS ((tree));
139 static void add_substitution PARAMS ((tree));
140 static inline int is_std_substitution PARAMS ((tree, substitution_identifier_index_t));
141 static inline int is_std_substitution_char PARAMS ((tree, substitution_identifier_index_t));
142 static int find_substitution PARAMS ((tree));
143
144 /* Functions for emitting mangled representations of things.  */
145
146 static void write_mangled_name PARAMS ((tree));
147 static void write_encoding PARAMS ((tree));
148 static void write_name PARAMS ((tree, int));
149 static void write_unscoped_name PARAMS ((tree));
150 static void write_unscoped_template_name PARAMS ((tree));
151 static void write_nested_name PARAMS ((tree));
152 static void write_prefix PARAMS ((tree));
153 static void write_template_prefix PARAMS ((tree));
154 static void write_unqualified_name PARAMS ((tree));
155 static void write_source_name PARAMS ((tree));
156 static void write_number PARAMS ((unsigned HOST_WIDE_INT, int,
157                                   unsigned int));
158 static void write_integer_cst PARAMS ((tree));
159 static void write_identifier PARAMS ((const char *));
160 static void write_special_name_constructor PARAMS ((tree));
161 static void write_special_name_destructor PARAMS ((tree));
162 static void write_type PARAMS ((tree));
163 static int write_CV_qualifiers_for_type PARAMS ((tree));
164 static void write_builtin_type PARAMS ((tree));
165 static void write_function_type PARAMS ((tree));
166 static void write_bare_function_type PARAMS ((tree, int, tree));
167 static void write_method_parms PARAMS ((tree, int, tree));
168 static void write_class_enum_type PARAMS ((tree));
169 static void write_template_args PARAMS ((tree));
170 static void write_expression PARAMS ((tree));
171 static void write_template_arg_literal PARAMS ((tree));
172 static void write_template_arg PARAMS ((tree));
173 static void write_template_template_arg PARAMS ((tree));
174 static void write_array_type PARAMS ((tree));
175 static void write_pointer_to_member_type PARAMS ((tree));
176 static void write_template_param PARAMS ((tree));
177 static void write_template_template_param PARAMS ((tree));
178 static void write_substitution PARAMS ((int));
179 static int discriminator_for_local_entity PARAMS ((tree));
180 static int discriminator_for_string_literal PARAMS ((tree, tree));
181 static void write_discriminator PARAMS ((int));
182 static void write_local_name PARAMS ((tree, tree, tree));
183 static void dump_substitution_candidates PARAMS ((void));
184 static const char *mangle_decl_string PARAMS ((tree));
185
186 /* Control functions.  */
187
188 static inline void start_mangling PARAMS ((void));
189 static inline const char *finish_mangling PARAMS ((void));
190 static tree mangle_special_for_type PARAMS ((tree, const char *));
191
192 /* Foreign language functions. */
193
194 static void write_java_integer_type_codes PARAMS ((tree));
195
196 /* Append a single character to the end of the mangled
197    representation.  */
198 #define write_char(CHAR)                                              \
199   obstack_1grow (&G.name_obstack, (CHAR))
200
201 /* Append a NUL-terminated string to the end of the mangled
202    representation.  */
203 #define write_string(STRING)                                          \
204   obstack_grow (&G.name_obstack, (STRING), strlen (STRING))
205
206 /* Return the position at which the next character will be appended to
207    the mangled representation.  */
208 #define mangled_position()                                              \
209   obstack_object_size (&G.name_obstack)
210
211 /* Non-zero if NODE1 and NODE2 are both TREE_LIST nodes and have the
212    same purpose (context, which may be a type) and value (template
213    decl).  See write_template_prefix for more information on what this
214    is used for.  */
215 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2)                         \
216   (TREE_CODE (NODE1) == TREE_LIST                                     \
217    && TREE_CODE (NODE2) == TREE_LIST                                  \
218    && ((TYPE_P (TREE_PURPOSE (NODE1))                                 \
219         && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
220        || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2))             \
221    && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
222
223 /* Write out a signed quantity in base 10.  */
224 #define write_signed_number(NUMBER) \
225   write_number (NUMBER, /*unsigned_p=*/0, 10)
226
227 /* Write out an unsigned quantity in base 10.  */
228 #define write_unsigned_number(NUMBER) \
229   write_number (NUMBER, /*unsigned_p=*/1, 10)
230
231 /* If DECL is a template instance, return non-zero and, if
232    TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
233    Otherwise return zero.  */
234
235 static int
236 decl_is_template_id (decl, template_info)
237      tree decl;
238      tree* template_info;
239 {
240   if (TREE_CODE (decl) == TYPE_DECL)
241     {
242       /* TYPE_DECLs are handled specially.  Look at its type to decide
243          if this is a template instantiation.  */
244       tree type = TREE_TYPE (decl);
245
246       if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
247         {
248           if (template_info != NULL)
249             /* For a templated TYPE_DECL, the template info is hanging
250                off the type.  */
251             *template_info = CLASSTYPE_TEMPLATE_INFO (type);
252           return 1;
253         }
254     } 
255   else
256     {
257       /* Check if this is a primary template.  */
258       if (DECL_LANG_SPECIFIC (decl) != NULL
259           && DECL_USE_TEMPLATE (decl)
260           && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
261           && TREE_CODE (decl) != TEMPLATE_DECL)
262         {
263           if (template_info != NULL)
264             /* For most templated decls, the template info is hanging
265                off the decl.  */
266             *template_info = DECL_TEMPLATE_INFO (decl);
267           return 1;
268         }
269     }
270
271   /* It's not a template id.  */
272   return 0;
273 }
274
275 /* Produce debugging output of current substitution candidates.  */
276
277 static void
278 dump_substitution_candidates ()
279 {
280   unsigned i;
281
282   fprintf (stderr, "  ++ substitutions  ");
283   for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
284     {
285       tree el = VARRAY_TREE (G.substitutions, i);
286       const char *name = "???";
287
288       if (i > 0)
289         fprintf (stderr, "                    ");
290       if (DECL_P (el))
291         name = IDENTIFIER_POINTER (DECL_NAME (el));
292       else if (TREE_CODE (el) == TREE_LIST)
293         name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
294       else if (TYPE_NAME (el))
295         name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
296       fprintf (stderr, " S%d_ = ", i - 1);
297       if (TYPE_P (el) && 
298           (CP_TYPE_RESTRICT_P (el) 
299            || CP_TYPE_VOLATILE_P (el) 
300            || CP_TYPE_CONST_P (el)))
301         fprintf (stderr, "CV-");
302       fprintf (stderr, "%s (%s at %p)\n", 
303                name, tree_code_name[TREE_CODE (el)], (void *) el);
304     }
305 }
306
307 /* Both decls and types can be substitution candidates, but sometimes
308    they refer to the same thing.  For instance, a TYPE_DECL and
309    RECORD_TYPE for the same class refer to the same thing, and should
310    be treated accordinginly in substitutions.  This function returns a
311    canonicalized tree node representing NODE that is used when adding
312    and substitution candidates and finding matches.  */
313
314 static inline tree
315 canonicalize_for_substitution (node)
316      tree node;
317 {
318   /* For a TYPE_DECL, use the type instead.  */
319   if (TREE_CODE (node) == TYPE_DECL)
320     node = TREE_TYPE (node);
321   if (TYPE_P (node))
322     node = canonical_type_variant (node);
323
324   return node;
325 }
326
327 /* Add NODE as a substitution candidate.  NODE must not already be on
328    the list of candidates.  */
329
330 static void
331 add_substitution (node)
332      tree node;
333 {
334   tree c;
335
336   if (DEBUG_MANGLE)
337     fprintf (stderr, "  ++ add_substitution (%s at %10p)\n", 
338              tree_code_name[TREE_CODE (node)], (void *) node);
339
340   /* Get the canonicalized substitution candidate for NODE.  */
341   c = canonicalize_for_substitution (node);
342   if (DEBUG_MANGLE && c != node)
343     fprintf (stderr, "  ++ using candidate (%s at %10p)\n",
344              tree_code_name[TREE_CODE (node)], (void *) node);
345   node = c;
346
347 #if ENABLE_CHECKING
348   /* Make sure NODE isn't already a candidate.  */
349   {
350     int i;
351     for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
352       {
353         tree candidate = VARRAY_TREE (G.substitutions, i);
354         if ((DECL_P (node) 
355              && node == candidate)
356             || (TYPE_P (node) 
357                 && TYPE_P (candidate) 
358                 && same_type_p (node, candidate)))
359           my_friendly_abort (20000524);
360       }
361   }
362 #endif /* ENABLE_CHECKING */
363
364   /* Put the decl onto the varray of substitution candidates.  */
365   VARRAY_PUSH_TREE (G.substitutions, node);
366
367   if (DEBUG_MANGLE)
368     dump_substitution_candidates ();
369 }
370
371 /* Helper function for find_substitution.  Returns non-zero if NODE,
372    which may be a decl or a CLASS_TYPE, is a template-id with template
373    name of substitution_index[INDEX] in the ::std namespace.  */
374
375 static inline int 
376 is_std_substitution (node, index)
377      tree node;
378      substitution_identifier_index_t index;
379 {
380   tree type = NULL;
381   tree decl = NULL;
382
383   if (DECL_P (node))
384     {
385       type = TREE_TYPE (node);
386       decl = node;
387     }
388   else if (CLASS_TYPE_P (node))
389     {
390       type = node;
391       decl = TYPE_NAME (node);
392     }
393   else 
394     /* These are not the droids you're looking for.  */
395     return 0;
396
397   return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
398           && TYPE_LANG_SPECIFIC (type) 
399           && CLASSTYPE_TEMPLATE_INFO (type)
400           && (DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) 
401               == subst_identifiers[index]));
402 }
403
404 /* Helper function for find_substitution.  Returns non-zero if NODE,
405    which may be a decl or a CLASS_TYPE, is the template-id
406    ::std::identifier<char>, where identifier is
407    substitution_index[INDEX].  */
408
409 static inline int
410 is_std_substitution_char (node, index)
411      tree node;
412      substitution_identifier_index_t index;
413 {
414   tree args;
415   /* Check NODE's name is ::std::identifier.  */
416   if (!is_std_substitution (node, index))
417     return 0;
418   /* Figure out its template args.  */
419   if (DECL_P (node))
420     args = DECL_TI_ARGS (node);  
421   else if (CLASS_TYPE_P (node))
422     args = CLASSTYPE_TI_ARGS (node);
423   else
424     /* Oops, not a template.  */
425     return 0;
426   /* NODE's template arg list should be <char>.  */
427   return 
428     TREE_VEC_LENGTH (args) == 1
429     && TREE_VEC_ELT (args, 0) == char_type_node;
430 }
431
432 /* Check whether a substitution should be used to represent NODE in
433    the mangling.
434
435    First, check standard special-case substitutions.
436
437      <substitution> ::= St     
438          # ::std
439
440                     ::= Sa     
441          # ::std::allocator
442
443                     ::= Sb     
444          # ::std::basic_string
445
446                     ::= Ss 
447          # ::std::basic_string<char,
448                                ::std::char_traits<char>,
449                                ::std::allocator<char> >
450
451                     ::= Si 
452          # ::std::basic_istream<char, ::std::char_traits<char> >
453
454                     ::= So 
455          # ::std::basic_ostream<char, ::std::char_traits<char> >
456
457                     ::= Sd 
458          # ::std::basic_iostream<char, ::std::char_traits<char> >   
459
460    Then examine the stack of currently available substitution
461    candidates for entities appearing earlier in the same mangling
462
463    If a substitution is found, write its mangled representation and
464    return non-zero.  If none is found, just return zero.  */
465
466 static int
467 find_substitution (node)
468      tree node;
469 {
470   int i;
471   int size = VARRAY_ACTIVE_SIZE (G.substitutions);
472   tree decl;
473   tree type;
474
475   if (DEBUG_MANGLE)
476     fprintf (stderr, "  ++ find_substitution (%s at %p)\n",
477              tree_code_name[TREE_CODE (node)], (void *) node);
478
479   /* Obtain the canonicalized substitution representation for NODE.
480      This is what we'll compare against.  */
481   node = canonicalize_for_substitution (node);
482
483   /* Check for builtin substitutions.  */
484
485   decl = TYPE_P (node) ? TYPE_NAME (node) : node;
486   type = TYPE_P (node) ? node : TREE_TYPE (node);
487
488   /* Check for std::allocator.  */
489   if (decl 
490       && is_std_substitution (decl, SUBID_ALLOCATOR)
491       && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
492     {
493       write_string ("Sa");
494       return 1;
495     }
496
497   /* Check for std::basic_string.  */
498   if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
499     {
500       if (TYPE_P (node))
501         {
502           /* If this is a type (i.e. a fully-qualified template-id), 
503              check for 
504                  std::basic_string <char,
505                                     std::char_traits<char>,
506                                     std::allocator<char> > .  */
507           if (CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED
508               && CLASSTYPE_USE_TEMPLATE (type))
509             {
510               tree args = CLASSTYPE_TI_ARGS (type);
511               if (TREE_VEC_LENGTH (args) == 3
512                   && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
513                   && is_std_substitution_char (TREE_VEC_ELT (args, 1),
514                                                SUBID_CHAR_TRAITS)
515                   && is_std_substitution_char (TREE_VEC_ELT (args, 2),
516                                                SUBID_ALLOCATOR))
517                 {
518                   write_string ("Ss");
519                   return 1;
520                 }
521             }
522         }
523       else
524         /* Substitute for the template name only if this isn't a type.  */
525         {
526           write_string ("Sb");
527           return 1;
528         }
529     }
530
531   /* Check for basic_{i,o,io}stream.  */
532   if (TYPE_P (node)
533       && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED
534       && CLASS_TYPE_P (type)
535       && CLASSTYPE_USE_TEMPLATE (type)
536       && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
537     {
538       /* First, check for the template 
539          args <char, std::char_traits<char> > .  */
540       tree args = CLASSTYPE_TI_ARGS (type);
541       if (TREE_VEC_LENGTH (args) == 2
542           && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
543           && is_std_substitution_char (TREE_VEC_ELT (args, 1),
544                                        SUBID_CHAR_TRAITS))
545         {
546           /* Got them.  Is this basic_istream?  */
547           tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
548           if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
549             {
550               write_string ("Si");
551               return 1;
552             }
553           /* Or basic_ostream?  */
554           else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
555             {
556               write_string ("So");
557               return 1;
558             }
559           /* Or basic_iostream?  */
560           else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
561             {
562               write_string ("Sd");
563               return 1;
564             }
565         }
566     }
567
568   /* Check for namespace std.  */
569   if (decl && DECL_NAMESPACE_STD_P (decl))
570     {
571       write_string ("St");
572       return 1;
573     }
574
575   /* Now check the list of available substitutions for this mangling
576      operation.    */
577   for (i = 0; i < size; ++i)
578     {
579       tree candidate = VARRAY_TREE (G.substitutions, i);
580       /* NODE is a matched to a candidate if it's the same decl node or
581          if it's the same type.  */
582       if (decl == candidate
583           || (TYPE_P (candidate) && type && TYPE_P (type)
584               && same_type_p (type, candidate))
585           || NESTED_TEMPLATE_MATCH (node, candidate))
586         {
587           write_substitution (i);
588           return 1;
589         }
590     }
591
592   /* No substitution found.  */
593   return 0;
594 }
595
596
597 /*  <mangled-name>      ::= _Z <encoding>  */
598
599 static inline void
600 write_mangled_name (decl)
601      tree decl;
602 {
603   MANGLE_TRACE_TREE ("mangled-name", decl);
604
605   if (DECL_LANG_SPECIFIC (decl)
606       && DECL_EXTERN_C_FUNCTION_P (decl)
607       && ! DECL_OVERLOADED_OPERATOR_P (decl))
608     /* The standard notes:
609          "The <encoding> of an extern "C" function is treated like
610          global-scope data, i.e. as its <source-name> without a type."
611        We cannot write overloaded operators that way though,
612        because it contains characters invalid in assembler.  */
613     write_source_name (DECL_NAME (decl));
614   else
615     /* C++ name; needs to be mangled.  */
616     {
617       write_string ("_Z");
618       write_encoding (decl);
619     }
620 }
621
622 /*   <encoding>         ::= <function name> <bare-function-type>
623                         ::= <data name>  */
624
625 static void
626 write_encoding (decl)
627      tree decl;
628 {
629   MANGLE_TRACE_TREE ("encoding", decl);
630
631   if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
632     {
633       /* For overloaded operators write just the mangled name
634          without arguments.  */
635       if (DECL_OVERLOADED_OPERATOR_P (decl))
636         write_name (decl, /*ignore_local_scope=*/0);
637       else
638         write_source_name (DECL_NAME (decl));
639       return;
640     }
641
642   write_name (decl, /*ignore_local_scope=*/0);
643   if (TREE_CODE (decl) == FUNCTION_DECL)
644     {
645       tree fn_type;
646
647       if (decl_is_template_id (decl, NULL))
648         fn_type = get_mostly_instantiated_function_type (decl, NULL, NULL);
649       else
650         fn_type = TREE_TYPE (decl);
651
652       write_bare_function_type (fn_type, 
653                                 (!DECL_CONSTRUCTOR_P (decl)
654                                  && !DECL_DESTRUCTOR_P (decl)
655                                  && !DECL_CONV_FN_P (decl)
656                                  && decl_is_template_id (decl, NULL)),
657                                 decl);
658     }
659 }
660
661 /* <name> ::= <unscoped-name>
662           ::= <unscoped-template-name> <template-args>
663           ::= <nested-name>
664           ::= <local-name>  
665
666    If IGNORE_LOCAL_SCOPE is non-zero, this production of <name> is
667    called from <local-name>, which mangles the enclosing scope
668    elsewhere and then uses this function to mangle just the part
669    underneath the function scope.  So don't use the <local-name>
670    production, to avoid an infinite recursion.  */
671
672 static void
673 write_name (decl, ignore_local_scope)
674      tree decl;
675      int ignore_local_scope;
676 {
677   tree context;
678
679   MANGLE_TRACE_TREE ("name", decl);
680
681   if (TREE_CODE (decl) == TYPE_DECL)
682     {
683       /* In case this is a typedef, fish out the corresponding
684          TYPE_DECL for the main variant.  */
685       decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
686       context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
687     }
688   else
689     context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
690
691   /* A decl in :: or ::std scope is treated specially.  The former is
692      mangled using <unscoped-name> or <unscoped-template-name>, the
693      latter with a special substitution.  Also, a name that is
694      directly in a local function scope is also mangled with
695      <unscoped-name> rather than a full <nested-name>.  */
696   if (context == NULL 
697       || context == global_namespace 
698       || DECL_NAMESPACE_STD_P (context)
699       || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
700     {
701       tree template_info;
702       /* Is this a template instance?  */
703       if (decl_is_template_id (decl, &template_info))
704         {
705           /* Yes: use <unscoped-template-name>.  */
706           write_unscoped_template_name (TI_TEMPLATE (template_info));
707           write_template_args (TI_ARGS (template_info));
708         }
709       else
710         /* Everything else gets an <unqualified-name>.  */
711         write_unscoped_name (decl);
712     }
713   else
714     {
715       /* Handle local names, unless we asked not to (that is, invoked
716          under <local-name>, to handle only the part of the name under
717          the local scope).  */
718       if (!ignore_local_scope)
719         {
720           /* Scan up the list of scope context, looking for a
721              function.  If we find one, this entity is in local
722              function scope.  local_entity tracks context one scope
723              level down, so it will contain the element that's
724              directly in that function's scope, either decl or one of
725              its enclosing scopes.  */
726           tree local_entity = decl;
727           while (context != NULL && context != global_namespace)
728             {
729               /* Make sure we're always dealing with decls.  */
730               if (context != NULL && TYPE_P (context))
731                 context = TYPE_NAME (context);
732               /* Is this a function?  */
733               if (TREE_CODE (context) == FUNCTION_DECL)
734                 {
735                   /* Yes, we have local scope.  Use the <local-name>
736                      production for the innermost function scope.  */
737                   write_local_name (context, local_entity, decl);
738                   return;
739                 }
740               /* Up one scope level.  */
741               local_entity = context;
742               context = CP_DECL_CONTEXT (context);
743             }
744
745           /* No local scope found?  Fall through to <nested-name>.  */
746         }
747
748       /* Other decls get a <nested-name> to encode their scope.  */
749       write_nested_name (decl);
750     }
751 }
752
753 /* <unscoped-name> ::= <unqualified-name>
754                    ::= St <unqualified-name>   # ::std::  */
755
756 static void
757 write_unscoped_name (decl)
758      tree decl;
759 {
760   tree context = CP_DECL_CONTEXT (decl);
761
762   MANGLE_TRACE_TREE ("unscoped-name", decl);
763
764   /* Is DECL in ::std?  */
765   if (DECL_NAMESPACE_STD_P (context))
766     {
767       write_string ("St");
768       write_unqualified_name (decl);
769     }
770   /* If not, it should be either in the global namespace, or directly
771      in a local function scope.  */
772   else if (context == global_namespace 
773            || context == NULL
774            || TREE_CODE (context) == FUNCTION_DECL)
775     write_unqualified_name (decl);
776   else 
777     my_friendly_abort (20000521);
778 }
779
780 /* <unscoped-template-name> ::= <unscoped-name>
781                             ::= <substitution>  */
782
783 static void
784 write_unscoped_template_name (decl)
785      tree decl;
786 {
787   MANGLE_TRACE_TREE ("unscoped-template-name", decl);
788
789   if (find_substitution (decl))
790     return;
791   write_unscoped_name (decl);
792   add_substitution (decl);
793 }
794
795 /* Write the nested name, including CV-qualifiers, of DECL.
796
797    <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E  
798                  ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
799
800    <CV-qualifiers> ::= [r] [V] [K]  */
801
802 static void
803 write_nested_name (decl)
804      tree decl;
805 {
806   tree template_info;
807
808   MANGLE_TRACE_TREE ("nested-name", decl);
809
810   write_char ('N');
811   
812   /* Write CV-qualifiers, if this is a member function.  */
813   if (TREE_CODE (decl) == FUNCTION_DECL 
814       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
815     {
816       if (DECL_VOLATILE_MEMFUNC_P (decl))
817         write_char ('V');
818       if (DECL_CONST_MEMFUNC_P (decl))
819         write_char ('K');
820     }
821
822   /* Is this a template instance?  */
823   if (decl_is_template_id (decl, &template_info))
824     {
825       /* Yes, use <template-prefix>.  */
826       write_template_prefix (decl);
827       write_template_args (TI_ARGS (template_info));
828     }
829   else
830     {
831       /* No, just use <prefix>  */
832       write_prefix (DECL_CONTEXT (decl));
833       write_unqualified_name (decl);
834     }
835   write_char ('E');
836 }
837
838 /* <prefix> ::= <prefix> <unqualified-name>>
839             ::= <template-prefix> <template-args>
840             ::= # empty
841             ::= <substitution>  */
842
843 static void
844 write_prefix (node)
845      tree node;
846 {
847   tree decl;
848   /* Non-NULL if NODE represents a template-id.  */
849   tree template_info = NULL;
850
851   MANGLE_TRACE_TREE ("prefix", node);
852
853   if (node == NULL
854       || node == global_namespace)
855     return;
856
857   if (find_substitution (node))
858     return;
859
860   if (DECL_P (node))
861     /* Node is a decl.  */
862     {
863       /* If this is a function decl, that means we've hit function
864          scope, so this prefix must be for a local name.  In this
865          case, we're under the <local-name> production, which encodes
866          the enclosing function scope elsewhere.  So don't continue
867          here.  */
868       if (TREE_CODE (node) == FUNCTION_DECL)
869         return;
870
871       decl = node;
872       decl_is_template_id (decl, &template_info);
873     }
874   else
875     /* Node is a type.  */
876     {
877       decl = TYPE_NAME (node);
878       if (CLASSTYPE_TEMPLATE_ID_P (node))
879         template_info = CLASSTYPE_TEMPLATE_INFO (node);
880     }
881
882   if (template_info != NULL)
883     /* Templated.  */
884     {
885       write_template_prefix (decl);
886       write_template_args (TI_ARGS (template_info));
887     }
888   else
889     /* Not templated.  */
890     {
891       write_prefix (CP_DECL_CONTEXT (decl));
892       write_unqualified_name (decl);
893     }
894
895   add_substitution (node);
896 }
897
898 /* <template-prefix> ::= <prefix> <template component>
899                      ::= <substitution>  */
900
901 static void
902 write_template_prefix (node)
903      tree node;
904 {
905   tree decl = DECL_P (node) ? node : TYPE_NAME (node);
906   tree type = DECL_P (node) ? TREE_TYPE (node) : node;
907   tree context = CP_DECL_CONTEXT (decl);
908   tree template_info;
909   tree template;
910   tree substitution;
911
912   MANGLE_TRACE_TREE ("template-prefix", node);
913
914   /* Find the template decl.  */
915   if (decl_is_template_id (decl, &template_info))
916     template = TI_TEMPLATE (template_info);
917   else if (CLASSTYPE_TEMPLATE_ID_P (type))
918     template = CLASSTYPE_TI_TEMPLATE (type);
919   else
920     /* Oops, not a template.  */
921     my_friendly_abort (20000524);
922
923   /* For a member template, though, the template name for the
924      innermost name must have all the outer template levels
925      instantiated.  For instance, consider
926
927        template<typename T> struct Outer {
928          template<typename U> struct Inner {};
929        };
930
931      The template name for `Inner' in `Outer<int>::Inner<float>' is
932      `Outer<int>::Inner<U>'.  In g++, we don't instantiate the template
933      levels separately, so there's no TEMPLATE_DECL available for this
934      (there's only `Outer<T>::Inner<U>').
935
936      In order to get the substitutions right, we create a special
937      TREE_LIST to represent the substitution candidate for a nested
938      template.  The TREE_PURPOSE is the template's context, fully
939      instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
940      template.
941
942      So, for the example above, `Outer<int>::Inner' is represented as a
943      substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
944      and whose value is `Outer<T>::Inner<U>'.  */
945   if (TYPE_P (context))
946     substitution = build_tree_list (context, template);
947   else
948     substitution = template;
949
950   if (find_substitution (substitution))
951     return;
952
953   write_prefix (context);
954   write_unqualified_name (decl);
955
956   add_substitution (substitution);
957 }
958
959 /* We don't need to handle thunks, vtables, or VTTs here.  Those are
960    mangled through special entry points.  
961
962     <unqualified-name>  ::= <operator-name>
963                         ::= <special-name>  
964                         ::= <source-name>  */
965
966 static void
967 write_unqualified_name (decl)
968      tree decl;
969 {
970   MANGLE_TRACE_TREE ("unqualified-name", decl);
971
972   if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
973     write_special_name_constructor (decl);
974   else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
975     write_special_name_destructor (decl);
976   else if (DECL_CONV_FN_P (decl)) 
977     {
978       /* Conversion operator. Handle it right here.  
979            <operator> ::= cv <type>  */
980       write_string ("cv");
981       write_type (TREE_TYPE (DECL_NAME (decl)));
982     }
983   else if (DECL_OVERLOADED_OPERATOR_P (decl))
984     {
985       operator_name_info_t *oni;
986       if (DECL_ASSIGNMENT_OPERATOR_P (decl))
987         oni = assignment_operator_name_info;
988       else
989         oni = operator_name_info;
990       
991       write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
992     }
993   else
994     write_source_name (DECL_NAME (decl));
995 }
996
997 /* Non-termial <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.  
998
999      <source-name> ::= </length/ number> <identifier>  */
1000
1001 static void
1002 write_source_name (identifier)
1003      tree identifier;
1004 {
1005   MANGLE_TRACE_TREE ("source-name", identifier);
1006
1007   /* Never write the whole template-id name including the template
1008      arguments; we only want the template name.  */
1009   if (IDENTIFIER_TEMPLATE (identifier))
1010     identifier = IDENTIFIER_TEMPLATE (identifier);
1011
1012   write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1013   write_identifier (IDENTIFIER_POINTER (identifier));
1014 }
1015
1016 /* Non-terminal <number>.
1017
1018      <number> ::= [n] </decimal integer/>  */
1019
1020 static void
1021 write_number (number, unsigned_p, base)
1022      unsigned HOST_WIDE_INT number;
1023      int unsigned_p;
1024      unsigned int base;
1025 {
1026   static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1027   unsigned HOST_WIDE_INT n;
1028   unsigned HOST_WIDE_INT m = 1;
1029
1030   if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1031     {
1032       write_char ('n');
1033       number = -((HOST_WIDE_INT) number);
1034     }
1035   
1036   /* Figure out how many digits there are.  */
1037   n = number;
1038   while (n >= base)
1039     {
1040       n /= base;
1041       m *= base;
1042     }
1043
1044   /* Write them out.  */
1045   while (m > 0)
1046     {
1047       int digit = number / m;
1048       write_char (digits[digit]);
1049       number -= digit * m;
1050       m /= base;
1051     }
1052
1053   my_friendly_assert (number == 0, 20000407);
1054 }
1055
1056 /* Write out an integeral CST in decimal.  */
1057
1058 static inline void
1059 write_integer_cst (cst)
1060      tree cst;
1061 {
1062   if (tree_int_cst_sgn (cst) >= 0) 
1063     {
1064       if (TREE_INT_CST_HIGH (cst) != 0)
1065         sorry ("mangling very large integers");
1066       write_unsigned_number (TREE_INT_CST_LOW (cst));
1067     }
1068   else
1069     write_signed_number (tree_low_cst (cst, 0));
1070 }
1071
1072 /* Non-terminal <identifier>.
1073
1074      <identifier> ::= </unqualified source code identifier>  */
1075
1076 static void
1077 write_identifier (identifier)
1078      const char *identifier;
1079 {
1080   MANGLE_TRACE ("identifier", identifier);
1081   write_string (identifier);
1082 }
1083
1084 /* Handle constructor productions of non-terminal <special-name>.
1085    CTOR is a constructor FUNCTION_DECL. 
1086
1087      <special-name> ::= C1   # complete object constructor
1088                     ::= C2   # base object constructor
1089                     ::= C3   # complete object allocating constructor
1090
1091    Currently, allocating constructors are never used. 
1092
1093    We also need to provide unique mangled names (which should never be
1094    exported) for the constructor that takes an in-charge parameter,
1095    and for a constructor whose name is the same as its class's name.
1096    We use "C*INTERNAL*" for these.  */
1097
1098 static void
1099 write_special_name_constructor (ctor)
1100      tree ctor;
1101 {
1102   if (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1103       /* Even though we don't ever emit a definition of the
1104          old-style destructor, we still have to consider entities
1105          (like static variables) nested inside it.  */
1106       || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1107     write_string ("C1");
1108   else if (DECL_BASE_CONSTRUCTOR_P (ctor))
1109     write_string ("C2");
1110   else
1111     my_friendly_abort (20001115);
1112 }
1113
1114 /* Handle destructor productions of non-terminal <special-name>.
1115    DTOR is a denstructor FUNCTION_DECL. 
1116
1117      <special-name> ::= D0 # deleting (in-charge) destructor
1118                     ::= D1 # complete object (in-charge) destructor
1119                     ::= D2 # base object (not-in-charge) destructor 
1120
1121    We also need to provide unique mngled names for old-ABI
1122    destructors, sometimes.  These should only be used internally.  We
1123    use "D*INTERNAL*" for these.  */
1124
1125 static void
1126 write_special_name_destructor (dtor)
1127      tree dtor;
1128 {
1129   if (DECL_DELETING_DESTRUCTOR_P (dtor))
1130     write_string ("D0");
1131   else if (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1132            /* Even though we don't ever emit a definition of the
1133               old-style destructor, we still have to consider entities
1134               (like static variables) nested inside it.  */
1135            || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1136     write_string ("D1");
1137   else if (DECL_BASE_DESTRUCTOR_P (dtor))
1138     write_string ("D2");
1139   else
1140     my_friendly_abort (20001115);
1141 }
1142
1143 /* Return the discriminator for ENTITY appearing inside
1144    FUNCTION.  The discriminator is the lexical ordinal of VAR among
1145    entities with the same name in the same FUNCTION.  */
1146
1147 static int
1148 discriminator_for_local_entity (entity)
1149      tree entity;
1150 {
1151   tree *type;
1152   int discriminator;
1153
1154   /* Assume this is the only local entity with this name.  */
1155   discriminator = 0;
1156
1157   if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1158     discriminator = DECL_DISCRIMINATOR (entity);
1159   else if (TREE_CODE (entity) == TYPE_DECL)
1160     {
1161       /* Scan the list of local classes.  */
1162       entity = TREE_TYPE (entity);
1163       for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
1164         if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1165             && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1166           ++discriminator;
1167     }  
1168
1169   return discriminator;
1170 }
1171
1172 /* Return the discriminator for STRING, a string literal used inside
1173    FUNCTION.  The disciminator is the lexical ordinal of STRING among
1174    string literals used in FUNCTION.  */
1175
1176 static int
1177 discriminator_for_string_literal (function, string)
1178      tree function ATTRIBUTE_UNUSED;
1179      tree string ATTRIBUTE_UNUSED;
1180 {
1181   /* For now, we don't discriminate amongst string literals.  */
1182   return 0;
1183 }
1184
1185 /*   <discriminator> := _ <number>   
1186
1187    The discriminator is used only for the second and later occurrences
1188    of the same name within a single function. In this case <number> is
1189    n - 2, if this is the nth occurrence, in lexical order.  */
1190
1191 static void
1192 write_discriminator (discriminator)
1193      int discriminator;
1194 {
1195   /* If discriminator is zero, don't write anything.  Otherwise... */
1196   if (discriminator > 0)
1197     {
1198       write_char ('_');
1199       write_unsigned_number (discriminator - 1);
1200     }
1201 }
1202
1203 /* Mangle the name of a function-scope entity.  FUNCTION is the
1204    FUNCTION_DECL for the enclosing function.  ENTITY is the decl for
1205    the entity itself.  LOCAL_ENTITY is the entity that's directly
1206    scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1207    of ENTITY.
1208
1209      <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1210                   := Z <function encoding> E s [<discriminator>]  */
1211
1212 static void
1213 write_local_name (function, local_entity, entity)
1214      tree function;
1215      tree local_entity;
1216      tree entity;
1217 {
1218   MANGLE_TRACE_TREE ("local-name", entity);
1219
1220   write_char ('Z');
1221   write_encoding (function);
1222   write_char ('E');
1223   if (TREE_CODE (entity) == STRING_CST)
1224     {
1225       write_char ('s');
1226       write_discriminator (discriminator_for_string_literal (function, 
1227                                                              entity));
1228     }
1229   else
1230     {
1231       /* Now the <entity name>.  Let write_name know its being called
1232          from <local-name>, so it doesn't try to process the enclosing
1233          function scope again.  */
1234       write_name (entity, /*ignore_local_scope=*/1);
1235       write_discriminator (discriminator_for_local_entity (local_entity));
1236     }
1237 }
1238
1239 /* Non-terminals <type> and <CV-qualifier>.  
1240
1241      <type> ::= <builtin-type>
1242             ::= <function-type>
1243             ::= <class-enum-type>
1244             ::= <array-type>
1245             ::= <pointer-to-member-type>
1246             ::= <template-param>
1247             ::= <substitution>
1248             ::= <CV-qualifier>
1249             ::= P <type>    # pointer-to
1250             ::= R <type>    # reference-to
1251             ::= C <type>    # complex pair (C 2000)
1252             ::= G <type>    # imaginary (C 2000)     [not supported]
1253             ::= U <source-name> <type>   # vendor extended type qualifier 
1254
1255    TYPE is a type node.  */
1256
1257 static void 
1258 write_type (type)
1259      tree type;
1260 {
1261   /* This gets set to non-zero if TYPE turns out to be a (possibly
1262      CV-qualified) builtin type.  */
1263   int is_builtin_type = 0;
1264
1265   MANGLE_TRACE_TREE ("type", type);
1266
1267   if (type == error_mark_node)
1268     return;
1269
1270   if (find_substitution (type))
1271     return;
1272   
1273   if (write_CV_qualifiers_for_type (type) > 0)
1274     /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1275        mangle the unqualified type.  The recursive call is needed here
1276        since both the qualified and uqualified types are substitution
1277        candidates.  */
1278     write_type (TYPE_MAIN_VARIANT (type));
1279   else
1280     {
1281       /* See through any typedefs.  */
1282       type = TYPE_MAIN_VARIANT (type);
1283
1284       switch (TREE_CODE (type))
1285         {
1286         case VOID_TYPE:
1287         case BOOLEAN_TYPE:
1288         case INTEGER_TYPE:  /* Includes wchar_t.  */
1289         case REAL_TYPE:
1290           /* If this is a typedef, TYPE may not be one of
1291              the standard builtin type nodes, but an alias of one.  Use
1292              TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
1293           write_builtin_type (TYPE_MAIN_VARIANT (type));
1294           ++is_builtin_type;
1295           break;
1296
1297         case COMPLEX_TYPE:
1298           write_char ('C');
1299           write_type (TREE_TYPE (type));
1300           break;
1301
1302         case FUNCTION_TYPE:
1303         case METHOD_TYPE:
1304           write_function_type (type);
1305           break;
1306
1307         case UNION_TYPE:
1308         case RECORD_TYPE:
1309         case ENUMERAL_TYPE:
1310           /* A pointer-to-member function is represented as a special
1311              RECORD_TYPE, so check for this first.  */
1312           if (TYPE_PTRMEMFUNC_P (type))
1313             write_pointer_to_member_type (type);
1314           else
1315             write_class_enum_type (type);
1316           break;
1317
1318         case TYPENAME_TYPE:
1319           /* We handle TYPENAME_TYPEs like ordinary nested names.  */
1320           write_nested_name (TYPE_STUB_DECL (type));
1321           break;
1322
1323         case ARRAY_TYPE:
1324           write_array_type (type);
1325           break;
1326
1327         case POINTER_TYPE:
1328           /* A pointer-to-member variable is represented by a POINTER_TYPE
1329              to an OFFSET_TYPE, so check for this first.  */
1330           if (TYPE_PTRMEM_P (type))
1331             write_pointer_to_member_type (type);
1332           else
1333             {
1334               write_char ('P');
1335               write_type (TREE_TYPE (type));
1336             }
1337           break;
1338
1339         case REFERENCE_TYPE:
1340           write_char ('R');
1341           write_type (TREE_TYPE (type));
1342           break;
1343
1344         case TEMPLATE_TYPE_PARM:
1345         case TEMPLATE_PARM_INDEX:
1346           write_template_param (type);
1347           break;
1348
1349         case TEMPLATE_TEMPLATE_PARM:
1350           write_template_template_param (type);
1351           break;
1352
1353         case BOUND_TEMPLATE_TEMPLATE_PARM:
1354           write_template_template_param (type);
1355           write_template_args 
1356             (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1357           break;
1358
1359         case OFFSET_TYPE:
1360           write_pointer_to_member_type (build_pointer_type (type));
1361           break;
1362
1363         case VECTOR_TYPE:
1364           write_string ("U8__vector");
1365           write_type (TREE_TYPE (type));
1366           break;
1367
1368         default:
1369           my_friendly_abort (20000409);
1370         }
1371     }
1372
1373   /* Types other than builtin types are substitution candidates.  */
1374   if (!is_builtin_type)
1375     add_substitution (type);
1376 }
1377
1378 /* Non-terminal <CV-qualifiers> for type nodes.  Returns the number of
1379    CV-qualifiers written for TYPE.
1380
1381      <CV-qualifiers> ::= [r] [V] [K]  */
1382
1383 static int
1384 write_CV_qualifiers_for_type (type)
1385      tree type;
1386 {
1387   int num_qualifiers = 0;
1388
1389   /* The order is specified by:
1390
1391        "In cases where multiple order-insensitive qualifiers are
1392        present, they should be ordered 'K' (closest to the base type),
1393        'V', 'r', and 'U' (farthest from the base type) ..."  */
1394
1395   if (CP_TYPE_RESTRICT_P (type))
1396     {
1397       write_char ('r');
1398       ++num_qualifiers;
1399     }
1400   if (CP_TYPE_VOLATILE_P (type))
1401     {
1402       write_char ('V');
1403       ++num_qualifiers;
1404     }
1405   if (CP_TYPE_CONST_P (type))
1406     {
1407       write_char ('K');
1408       ++num_qualifiers;
1409     }
1410
1411   return num_qualifiers;
1412 }
1413
1414 /* Non-terminal <builtin-type>. 
1415
1416      <builtin-type> ::= v   # void 
1417                     ::= b   # bool
1418                     ::= w   # wchar_t
1419                     ::= c   # char
1420                     ::= a   # signed char
1421                     ::= h   # unsigned char
1422                     ::= s   # short
1423                     ::= t   # unsigned short
1424                     ::= i   # int
1425                     ::= j   # unsigned int
1426                     ::= l   # long
1427                     ::= m   # unsigned long
1428                     ::= x   # long long, __int64
1429                     ::= y   # unsigned long long, __int64  
1430                     ::= n   # __int128            [not supported]
1431                     ::= o   # unsigned __int128   [not supported] 
1432                     ::= f   # float
1433                     ::= d   # double
1434                     ::= e   # long double, __float80 
1435                     ::= g   # __float128          [not supported]
1436                     ::= u <source-name>  # vendor extended type */
1437
1438 static void 
1439 write_builtin_type (type)
1440      tree type;
1441 {
1442   switch (TREE_CODE (type))
1443     {
1444     case VOID_TYPE:
1445       write_char ('v');
1446       break;
1447
1448     case BOOLEAN_TYPE:
1449       write_char ('b');
1450       break;
1451
1452     case INTEGER_TYPE:
1453       /* If this is size_t, get the underlying int type.  */
1454       if (TYPE_IS_SIZETYPE (type))
1455         type = TYPE_DOMAIN (type);
1456
1457       /* TYPE may still be wchar_t, since that isn't in
1458          integer_type_nodes.  */
1459       if (type == wchar_type_node)
1460         write_char ('w');
1461       else if (TYPE_FOR_JAVA (type))
1462         write_java_integer_type_codes (type);
1463       else
1464         {
1465           size_t itk;
1466           /* Assume TYPE is one of the shared integer type nodes.  Find
1467              it in the array of these nodes.  */
1468         iagain:
1469           for (itk = 0; itk < itk_none; ++itk)
1470             if (type == integer_types[itk])
1471               {
1472                 /* Print the corresponding single-letter code.  */
1473                 write_char (integer_type_codes[itk]);
1474                 break;
1475               }
1476           
1477           if (itk == itk_none)
1478             {
1479               tree t = type_for_mode (TYPE_MODE (type), TREE_UNSIGNED (type));
1480               if (type == t)
1481                 /* Couldn't find this type.  */
1482                 my_friendly_abort (20000408);
1483               type = t;
1484               goto iagain;
1485             }
1486         }
1487       break;
1488
1489     case REAL_TYPE:
1490       if (type == float_type_node
1491           || type == java_float_type_node)
1492         write_char ('f');
1493       else if (type == double_type_node
1494                || type == java_double_type_node)
1495         write_char ('d');
1496       else if (type == long_double_type_node)
1497         write_char ('e');
1498       else
1499         my_friendly_abort (20000409);
1500       break;
1501
1502     default:
1503       my_friendly_abort (20000509);
1504     }
1505 }
1506
1507 /* Non-terminal <function-type>.  NODE is a FUNCTION_TYPE or
1508    METHOD_TYPE.  The return type is mangled before the parameter
1509    types.
1510
1511      <function-type> ::= F [Y] <bare-function-type> E   */
1512
1513 static void
1514 write_function_type (type)
1515      tree type;
1516 {
1517   MANGLE_TRACE_TREE ("function-type", type);
1518
1519   write_char ('F');
1520   /* We don't track whether or not a type is `extern "C"'.  Note that
1521      you can have an `extern "C"' function that does not have
1522      `extern "C"' type, and vice versa:
1523
1524        extern "C" typedef void function_t();
1525        function_t f; // f has C++ linkage, but its type is
1526                      // `extern "C"'
1527
1528        typedef void function_t();
1529        extern "C" function_t f; // Vice versa.
1530
1531      See [dcl.link].  */
1532   write_bare_function_type (type, /*include_return_type_p=*/1, 
1533                             /*decl=*/NULL);
1534   write_char ('E');
1535 }
1536
1537 /* Non-terminal <bare-function-type>.  TYPE is a FUNCTION_TYPE or
1538    METHOD_TYPE.  If INCLUDE_RETURN_TYPE is non-zero, the return value
1539    is mangled before the parameter types.  If non-NULL, DECL is
1540    FUNCTION_DECL for the function whose type is being emitted.
1541
1542      <bare-function-type> ::= </signature/ type>+  */
1543
1544 static void
1545 write_bare_function_type (type, include_return_type_p, decl)
1546      tree type;
1547      int include_return_type_p;
1548      tree decl;
1549 {
1550   MANGLE_TRACE_TREE ("bare-function-type", type);
1551
1552   /* Mangle the return type, if requested.  */
1553   if (include_return_type_p)
1554     write_type (TREE_TYPE (type));
1555
1556   /* Now mangle the types of the arguments.  */
1557   write_method_parms (TYPE_ARG_TYPES (type), 
1558                       TREE_CODE (type) == METHOD_TYPE,
1559                       decl);
1560 }
1561
1562 /* Write the mangled representation of a method parameter list of
1563    types given in PARM_TYPES.  If METHOD_P is non-zero, the function is 
1564    considered a non-static method, and the this parameter is omitted.
1565    If non-NULL, DECL is the FUNCTION_DECL for the function whose
1566    parameters are being emitted.  */
1567
1568 static void
1569 write_method_parms (parm_types, method_p, decl)
1570      tree decl;
1571      tree parm_types;
1572      int method_p;
1573 {
1574   tree first_parm_type;
1575   tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1576
1577   /* Assume this parameter type list is variable-length.  If it ends
1578      with a void type, then it's not.  */
1579   int varargs_p = 1;
1580
1581   /* If this is a member function, skip the first arg, which is the
1582      this pointer.  
1583        "Member functions do not encode the type of their implicit this
1584        parameter."  
1585   
1586      Similarly, there's no need to mangle artificial parameters, like
1587      the VTT parameters for constructors and destructors.  */
1588   if (method_p)
1589     {
1590       parm_types = TREE_CHAIN (parm_types);
1591       parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1592
1593       while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1594         {
1595           parm_types = TREE_CHAIN (parm_types);
1596           parm_decl = TREE_CHAIN (parm_decl);
1597         }
1598     }
1599
1600   for (first_parm_type = parm_types; 
1601        parm_types; 
1602        parm_types = TREE_CHAIN (parm_types))
1603     {
1604       tree parm = TREE_VALUE (parm_types);
1605       if (parm == void_type_node)
1606         {
1607           /* "Empty parameter lists, whether declared as () or
1608              conventionally as (void), are encoded with a void parameter
1609              (v)."  */
1610           if (parm_types == first_parm_type)
1611             write_type (parm);
1612           /* If the parm list is terminated with a void type, it's
1613              fixed-length.  */
1614           varargs_p = 0;
1615           /* A void type better be the last one.  */
1616           my_friendly_assert (TREE_CHAIN (parm_types) == NULL, 20000523);
1617         }
1618       else
1619         write_type (parm);
1620     }
1621
1622   if (varargs_p)
1623     /* <builtin-type> ::= z  # ellipsis  */
1624     write_char ('z');
1625 }
1626
1627 /* <class-enum-type> ::= <name>  */
1628
1629 static void 
1630 write_class_enum_type (type)
1631      tree type;
1632 {
1633   write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1634 }
1635
1636 /* Non-terminal <template-args>.  ARGS is a TREE_VEC of template
1637    arguments.
1638
1639      <template-args> ::= I <template-arg>+ E  */
1640
1641 static void
1642 write_template_args (args)
1643      tree args;
1644 {
1645   int i;
1646   int length = TREE_VEC_LENGTH (args);
1647
1648   MANGLE_TRACE_TREE ("template-args", args);
1649
1650   my_friendly_assert (length > 0, 20000422);
1651
1652   if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1653     {
1654       /* We have nested template args.  We want the innermost template
1655          argument list.  */
1656       args = TREE_VEC_ELT (args, length - 1);
1657       length = TREE_VEC_LENGTH (args);
1658     }
1659
1660   write_char ('I');
1661   for (i = 0; i < length; ++i)
1662     write_template_arg (TREE_VEC_ELT (args, i));
1663   write_char ('E');
1664 }
1665
1666 /* <expression> ::= <unary operator-name> <expression>
1667                 ::= <binary operator-name> <expression> <expression>
1668                 ::= <expr-primary>
1669
1670    <expr-primary> ::= <template-param>
1671                   ::= L <type> <value number> E  # literal
1672                   ::= L <mangled-name> E         # external name  */
1673
1674 static void
1675 write_expression (expr)
1676      tree expr;
1677 {
1678   enum tree_code code;
1679
1680   code = TREE_CODE (expr);
1681
1682   /* Handle pointers-to-members by making them look like expression
1683      nodes.  */
1684   if (code == PTRMEM_CST)
1685     {
1686       expr = build_nt (ADDR_EXPR,
1687                        build_nt (SCOPE_REF,
1688                                  PTRMEM_CST_CLASS (expr),
1689                                  PTRMEM_CST_MEMBER (expr)));
1690       code = TREE_CODE (expr);
1691     }
1692
1693   /* Handle template parameters. */
1694   if (code == TEMPLATE_TYPE_PARM 
1695       || code == TEMPLATE_TEMPLATE_PARM
1696       || code == BOUND_TEMPLATE_TEMPLATE_PARM
1697       || code == TEMPLATE_PARM_INDEX)
1698     write_template_param (expr);
1699   /* Handle literals.  */
1700   else if (TREE_CODE_CLASS (code) == 'c')
1701     write_template_arg_literal (expr);
1702   else if (DECL_P (expr))
1703     {
1704       write_char ('L');
1705       write_mangled_name (expr);
1706       write_char ('E');
1707     }
1708   else
1709     {
1710       int i;
1711
1712       /* Skip NOP_EXPRs.  They can occur when (say) a pointer argument
1713          is converted (via qualification conversions) to another
1714          type.  */
1715       while (TREE_CODE (expr) == NOP_EXPR)
1716         {
1717           expr = TREE_OPERAND (expr, 0);
1718           code = TREE_CODE (expr);
1719         }
1720
1721       /* When we bind a variable or function to a non-type template
1722          argument with reference type, we create an ADDR_EXPR to show
1723          the fact that the entity's address has been taken.  But, we
1724          don't actually want to output a mangling code for the `&'.  */
1725       if (TREE_CODE (expr) == ADDR_EXPR
1726           && TREE_TYPE (expr)
1727           && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
1728         {
1729           expr = TREE_OPERAND (expr, 0);
1730           if (DECL_P (expr))
1731             {
1732               write_expression (expr);
1733               return;
1734             }
1735
1736           code = TREE_CODE (expr);
1737         }
1738       
1739       /* If it wasn't any of those, recursively expand the expression.  */
1740       write_string (operator_name_info[(int) code].mangled_name);
1741
1742       /* Handle pointers-to-members specially.  */
1743       if (code == SCOPE_REF)
1744         {
1745           write_type (TREE_OPERAND (expr, 0));
1746           if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
1747             write_source_name (TREE_OPERAND (expr, 1));
1748           else
1749             write_encoding (TREE_OPERAND (expr, 1));
1750         }
1751       else
1752         for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
1753           write_expression (TREE_OPERAND (expr, i));
1754     }
1755 }
1756
1757 /* Literal subcase of non-terminal <template-arg>.  
1758
1759      "Literal arguments, e.g. "A<42L>", are encoded with their type
1760      and value. Negative integer values are preceded with "n"; for
1761      example, "A<-42L>" becomes "1AILln42EE". The bool value false is
1762      encoded as 0, true as 1. If floating-point arguments are accepted
1763      as an extension, their values should be encoded using a
1764      fixed-length lowercase hexadecimal string corresponding to the
1765      internal representation (IEEE on IA-64), high-order bytes first,
1766      without leading zeroes. For example: "Lfbff000000E" is -1.0f."  */
1767
1768 static void
1769 write_template_arg_literal (value)
1770      tree value;
1771 {
1772   tree type = TREE_TYPE (value);
1773   write_char ('L');
1774   write_type (type);
1775
1776   if (TREE_CODE (value) == CONST_DECL)
1777     write_integer_cst (DECL_INITIAL (value));
1778   else if (TREE_CODE (value) == INTEGER_CST)
1779     {
1780       if (same_type_p (type, boolean_type_node))
1781         {
1782           if (value == boolean_false_node || integer_zerop (value))
1783             write_unsigned_number (0);
1784           else if (value == boolean_true_node)
1785             write_unsigned_number (1);
1786           else 
1787             my_friendly_abort (20000412);
1788         }
1789       else
1790         write_integer_cst (value);
1791     }
1792   else if (TREE_CODE (value) == REAL_CST)
1793     {
1794 #ifdef CROSS_COMPILE
1795       static int explained;
1796
1797       if (!explained) 
1798         {
1799           sorry ("real-valued template parameters when cross-compiling");
1800           explained = 1;
1801         }
1802 #else
1803       size_t i;
1804       for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1805         write_number (((unsigned char *) 
1806                        &TREE_REAL_CST (value))[i], 
1807                       /*unsigned_p=*/1,
1808                       16);
1809 #endif
1810     }
1811   else
1812     my_friendly_abort (20000412);
1813
1814   write_char ('E');
1815 }
1816
1817 /* Non-terminal <tempalate-arg>.  
1818
1819      <template-arg> ::= <type>                        # type
1820                     ::= L <type> </value/ number> E   # literal
1821                     ::= LZ <name> E                   # external name
1822                     ::= X <expression> E              # expression  */
1823
1824 static void
1825 write_template_arg (node)
1826      tree node;
1827 {
1828   enum tree_code code = TREE_CODE (node);
1829
1830   MANGLE_TRACE_TREE ("template-arg", node);
1831
1832   /* A template template paramter's argument list contains TREE_LIST
1833      nodes of which the value field is the the actual argument.  */
1834   if (code == TREE_LIST)
1835     {
1836       node = TREE_VALUE (node);
1837       /* If it's a decl, deal with its type instead.  */
1838       if (DECL_P (node))
1839         {
1840           node = TREE_TYPE (node);
1841           code = TREE_CODE (node);
1842         }
1843     }
1844
1845   if (TYPE_P (node))
1846     write_type (node);
1847   else if (code == TEMPLATE_DECL)
1848     /* A template appearing as a template arg is a template template arg.  */
1849     write_template_template_arg (node);
1850   else if (DECL_P (node))
1851     {
1852       write_char ('L');
1853       write_char ('Z');
1854       write_encoding (node);
1855       write_char ('E');
1856     }
1857   else if (TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST)
1858     write_template_arg_literal (node);
1859   else
1860     {
1861       /* Template arguments may be expressions.  */
1862       write_char ('X');
1863       write_expression (node);
1864       write_char ('E');
1865     }
1866 }
1867
1868 /*  <template-template-arg>
1869                         ::= <name>
1870                         ::= <substitution>  */
1871
1872 void
1873 write_template_template_arg (tree decl)
1874 {
1875   MANGLE_TRACE_TREE ("template-template-arg", decl);
1876
1877   if (find_substitution (decl))
1878     return;
1879   write_name (decl, /*ignore_local_scope=*/0);
1880   add_substitution (decl);
1881 }
1882
1883
1884 /* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.  
1885
1886      <array-type> ::= A [</dimension/ number>] _ </element/ type>  
1887                   ::= A <expression> _ </element/ type>
1888
1889      "Array types encode the dimension (number of elements) and the
1890      element type. For variable length arrays, the dimension (but not
1891      the '_' separator) is omitted."  */
1892
1893 static void 
1894 write_array_type (type)
1895   tree type;
1896 {
1897   write_char ('A');
1898   if (TYPE_DOMAIN (type))
1899     {
1900       tree index_type;
1901       tree max;
1902
1903       index_type = TYPE_DOMAIN (type);
1904       /* The INDEX_TYPE gives the upper and lower bounds of the
1905          array.  */
1906       max = TYPE_MAX_VALUE (index_type);
1907       if (TREE_CODE (max) == INTEGER_CST)
1908         {
1909           /* The ABI specifies that we should mangle the number of
1910              elements in the array, not the largest allowed index.  */
1911           max = size_binop (PLUS_EXPR, max, size_one_node);
1912           write_unsigned_number (tree_low_cst (max, 1));
1913         }
1914       else
1915         write_expression (TREE_OPERAND (max, 0));
1916     }
1917   write_char ('_');
1918   write_type (TREE_TYPE (type));
1919 }
1920
1921 /* Non-terminal <pointer-to-member-type> for pointer-to-member
1922    variables.  TYPE is a pointer-to-member POINTER_TYPE.
1923
1924      <pointer-to-member-type> ::= M </class/ type> </member/ type>  */
1925
1926 static void
1927 write_pointer_to_member_type (type)
1928      tree type;
1929 {
1930   write_char ('M');
1931   /* For a pointer-to-function member, the class type may be
1932      cv-qualified, bug that won't be reflected in
1933      TYPE_PTRMEM_CLASS_TYPE.  So, we go fishing around in
1934      TYPE_PTRMEM_POINTED_TO_TYPE instead.  */
1935   if (TYPE_PTRMEMFUNC_P (type))
1936     {
1937       tree fn_type;
1938       tree this_type;
1939
1940       fn_type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
1941       /* The first parameter must be a POINTER_TYPE pointing to the
1942          `this' parameter.  */
1943       this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fn_type)));
1944       write_type (this_type);
1945     }
1946   /* For a pointer-to-data member, things are simpler.  */
1947   else
1948     write_type (TYPE_PTRMEM_CLASS_TYPE (type));
1949   write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1950 }
1951
1952 /* Non-terminal <template-param>.  PARM is a TEMPLATE_TYPE_PARM,
1953    TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
1954    TEMPLATE_PARM_INDEX.
1955
1956      <template-param> ::= T </parameter/ number> _  */
1957
1958 static void
1959 write_template_param (parm)
1960      tree parm;
1961 {
1962   int parm_index;
1963
1964   MANGLE_TRACE_TREE ("template-parm", parm);
1965
1966   switch (TREE_CODE (parm))
1967     {
1968     case TEMPLATE_TYPE_PARM:
1969     case TEMPLATE_TEMPLATE_PARM:
1970     case BOUND_TEMPLATE_TEMPLATE_PARM:
1971       parm_index = TEMPLATE_TYPE_IDX (parm);
1972       break;
1973
1974     case TEMPLATE_PARM_INDEX:
1975       parm_index = TEMPLATE_PARM_IDX (parm);
1976       break;
1977
1978     default:
1979       my_friendly_abort (20000523);
1980     }
1981
1982   write_char ('T');
1983   /* NUMBER as it appears in the mangling is (-1)-indexed, with the
1984      earliest template param denoted by `_'.  */
1985   if (parm_index > 0)
1986     write_unsigned_number (parm_index - 1);
1987   write_char ('_');
1988 }
1989
1990 /*  <template-template-param>
1991                         ::= <template-param> 
1992                         ::= <substitution>  */
1993
1994 static void
1995 write_template_template_param (parm)
1996      tree parm;
1997 {
1998   tree template = NULL_TREE;
1999
2000   /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2001      template template parameter.  The substitution candidate here is
2002      only the template.  */
2003   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2004     {
2005       template 
2006         = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2007       if (find_substitution (template))
2008         return;
2009     }
2010
2011   /* <template-param> encodes only the template parameter position,
2012      not its template arguments, which is fine here.  */
2013   write_template_param (parm);
2014   if (template)
2015     add_substitution (template);
2016 }
2017
2018 /* Non-terminal <substitution>.  
2019
2020       <substitution> ::= S <seq-id> _
2021                      ::= S_  */
2022
2023 static void
2024 write_substitution (seq_id)
2025      int seq_id;
2026 {
2027   MANGLE_TRACE ("substitution", "");
2028
2029   write_char ('S');
2030   if (seq_id > 0)
2031     write_number (seq_id - 1, /*unsigned=*/1, 36);
2032   write_char ('_');
2033 }
2034
2035 /* Start mangling a new name or type.  */
2036
2037 static inline void
2038 start_mangling ()
2039 {
2040   obstack_free (&G.name_obstack, obstack_base (&G.name_obstack));
2041 }
2042
2043 /* Done with mangling.  Return the generated mangled name.  */
2044
2045 static inline const char *
2046 finish_mangling ()
2047 {
2048   /* Clear all the substitutions.  */
2049   VARRAY_POP_ALL (G.substitutions);
2050
2051   /* Null-terminate the string.  */
2052   write_char ('\0');
2053
2054   return (const char *) obstack_base (&G.name_obstack);
2055 }
2056
2057 /* Initialize data structures for mangling.  */
2058
2059 void
2060 init_mangle ()
2061 {
2062   gcc_obstack_init (&G.name_obstack);
2063   VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
2064
2065   /* Cache these identifiers for quick comparison when checking for
2066      standard substitutions.  */
2067   subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2068   subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2069   subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2070   subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2071   subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2072   subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2073 }
2074
2075 /* Generate the mangled name of DECL.  */
2076
2077 static const char *
2078 mangle_decl_string (decl)
2079      tree decl;
2080 {
2081   const char *result;
2082
2083   start_mangling ();
2084
2085   if (TREE_CODE (decl) == TYPE_DECL)
2086     write_type (TREE_TYPE (decl));
2087   else if (/* The names of `extern "C"' functions are not mangled.  */
2088            (TREE_CODE (decl) == FUNCTION_DECL 
2089             /* But overloaded operator names *are* mangled.  */
2090             && !DECL_OVERLOADED_OPERATOR_P (decl)
2091             /* If there's no DECL_LANG_SPECIFIC, it's a function built
2092                by language-independent code, which never builds
2093                functions with C++ linkage.  */
2094             && (!DECL_LANG_SPECIFIC (decl) 
2095                 || DECL_EXTERN_C_FUNCTION_P (decl)))
2096            /* The names of global variables aren't mangled either.  */
2097            || (TREE_CODE (decl) == VAR_DECL
2098                && CP_DECL_CONTEXT (decl) == global_namespace))
2099     write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
2100   else
2101     {
2102       write_mangled_name (decl);
2103       if (DECL_LANG_SPECIFIC (decl)
2104           && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
2105               || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
2106         /* We need a distinct mangled name for these entities, but
2107            we should never actually output it.  So, we append some
2108            characters the assembler won't like.  */
2109         write_string (" *INTERNAL* ");
2110     }
2111
2112   result = finish_mangling ();
2113   if (DEBUG_MANGLE)
2114     fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2115   return result;
2116 }
2117
2118 /* Create an identifier for the external mangled name of DECL.  */
2119
2120 void
2121 mangle_decl (decl)
2122      tree decl;
2123 {
2124   tree id = get_identifier (mangle_decl_string (decl));
2125
2126   SET_DECL_ASSEMBLER_NAME (decl, id);
2127 }
2128
2129 /* Generate the mangled representation of TYPE.  */
2130
2131 const char *
2132 mangle_type_string (type)
2133      tree type;
2134 {
2135   const char *result;
2136
2137   start_mangling ();
2138   write_type (type);
2139   result = finish_mangling ();
2140   if (DEBUG_MANGLE)
2141     fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2142   return result;
2143 }
2144
2145 /* Create an identifier for the mangled representation of TYPE.  */
2146
2147 tree
2148 mangle_type (type)
2149      tree type;
2150 {
2151   return get_identifier (mangle_type_string (type));
2152 }
2153
2154 /* Create an identifier for the mangled name of a special component
2155    for belonging to TYPE.  CODE is the ABI-specified code for this
2156    component.  */
2157
2158 static tree
2159 mangle_special_for_type (type, code)
2160      tree type;
2161      const char *code;
2162 {
2163   const char *result;
2164
2165   /* We don't have an actual decl here for the special component, so
2166      we can't just process the <encoded-name>.  Instead, fake it.  */
2167   start_mangling ();
2168
2169   /* Start the mangling.  */
2170   write_string ("_Z");
2171   write_string (code);
2172
2173   /* Add the type.  */
2174   write_type (type);
2175   result = finish_mangling ();
2176
2177   if (DEBUG_MANGLE)
2178     fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2179
2180   return get_identifier (result);
2181 }
2182
2183 /* Create an identifier for the mangled representation of the typeinfo
2184    structure for TYPE.  */
2185
2186 tree
2187 mangle_typeinfo_for_type (type)
2188      tree type;
2189 {
2190   return mangle_special_for_type (type, "TI");
2191 }
2192
2193 /* Create an identifier for the mangled name of the NTBS containing
2194    the mangled name of TYPE.  */
2195
2196 tree
2197 mangle_typeinfo_string_for_type (type)
2198      tree type;
2199 {
2200   return mangle_special_for_type (type, "TS");
2201 }
2202
2203 /* Create an identifier for the mangled name of the vtable for TYPE.  */
2204
2205 tree
2206 mangle_vtbl_for_type (type)
2207      tree type;
2208 {
2209   return mangle_special_for_type (type, "TV");
2210 }
2211
2212 /* Returns an identifier for the mangled name of the VTT for TYPE.  */
2213
2214 tree
2215 mangle_vtt_for_type (type)
2216      tree type;
2217 {
2218   return mangle_special_for_type (type, "TT");
2219 }
2220
2221 /* Return an identifier for a construction vtable group.  TYPE is
2222    the most derived class in the hierarchy; BINFO is the base
2223    subobject for which this construction vtable group will be used.  
2224
2225    This mangling isn't part of the ABI specification; in the ABI
2226    specification, the vtable group is dumped in the same COMDAT as the
2227    main vtable, and is referenced only from that vtable, so it doesn't
2228    need an external name.  For binary formats without COMDAT sections,
2229    though, we need external names for the vtable groups.  
2230
2231    We use the production
2232
2233     <special-name> ::= CT <type> <offset number> _ <base type>  */
2234
2235 tree
2236 mangle_ctor_vtbl_for_type (type, binfo)
2237      tree type;
2238      tree binfo;
2239 {
2240   const char *result;
2241
2242   start_mangling ();
2243
2244   write_string ("_Z");
2245   write_string ("TC");
2246   write_type (type);
2247   write_integer_cst (BINFO_OFFSET (binfo));
2248   write_char ('_');
2249   write_type (BINFO_TYPE (binfo));
2250
2251   result = finish_mangling ();
2252   if (DEBUG_MANGLE)
2253     fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2254   return get_identifier (result);
2255 }
2256
2257 /* Return an identifier for the mangled name of a thunk to FN_DECL.
2258    OFFSET is the initial adjustment to this used to find the vptr.  If
2259    VCALL_OFFSET is non-NULL, this is a virtual thunk, and it is the
2260    vtbl offset in bytes.  
2261
2262     <special-name> ::= Th <offset number> _ <base encoding>
2263                    ::= Tv <offset number> _ <vcall offset number> _
2264                                                             <base encoding>
2265 */
2266
2267 tree
2268 mangle_thunk (fn_decl, offset, vcall_offset)
2269      tree fn_decl;
2270      tree offset;
2271      tree vcall_offset;
2272 {
2273   const char *result;
2274   
2275   start_mangling ();
2276
2277   write_string ("_Z");
2278   /* The <special-name> for virtual thunks is Tv, for non-virtual
2279      thunks Th.  */
2280   write_char ('T');
2281   if (vcall_offset != 0)
2282     write_char ('v');
2283   else
2284     write_char ('h');
2285
2286   /* For either flavor, write the offset to this.  */
2287   write_integer_cst (offset);
2288   write_char ('_');
2289
2290   /* For a virtual thunk, add the vcall offset.  */
2291   if (vcall_offset)
2292     {
2293       /* Virtual thunk.  Write the vcall offset and base type name.  */
2294       write_integer_cst (vcall_offset);
2295       write_char ('_');
2296     }
2297
2298   /* Scoped name.  */
2299   write_encoding (fn_decl);
2300
2301   result = finish_mangling ();
2302   if (DEBUG_MANGLE)
2303     fprintf (stderr, "mangle_thunk = %s\n\n", result);
2304   return get_identifier (result);
2305 }
2306
2307 /* Return an identifier for the mangled unqualified name for a
2308    conversion operator to TYPE.  This mangling is not specified by the
2309    ABI spec; it is only used internally.  */
2310
2311 tree
2312 mangle_conv_op_name_for_type (type)
2313      tree type;
2314 {
2315   tree identifier;
2316
2317   /* Build the mangling for TYPE.  */
2318   const char *mangled_type = mangle_type_string (type);
2319   /* Allocate a temporary buffer for the complete name.  */
2320   char *op_name = (char *) xmalloc (strlen ("operator ") 
2321                                     + strlen (mangled_type) + 1);
2322   /* Assemble the mangling.  */
2323   strcpy (op_name, "operator ");
2324   strcat (op_name, mangled_type);
2325   /* Find or create an identifier.  */
2326   identifier = get_identifier (op_name);
2327   /* Done with the temporary buffer.  */
2328   free (op_name);
2329   /* Set bits on the identifier so we know later it's a conversion.  */
2330   IDENTIFIER_OPNAME_P (identifier) = 1;
2331   IDENTIFIER_TYPENAME_P (identifier) = 1;
2332   /* Hang TYPE off the identifier so it can be found easily later when
2333      performing conversions.  */
2334   TREE_TYPE (identifier) = type;
2335
2336   return identifier;
2337 }
2338
2339 /* Return an identifier for the name of an initialization guard
2340    variable for indicated VARIABLE.  */
2341
2342 tree
2343 mangle_guard_variable (variable)
2344      tree variable;
2345 {
2346   start_mangling ();
2347   write_string ("_ZGV");
2348   write_name (variable, /*ignore_local_scope=*/0);
2349   return get_identifier (finish_mangling ());
2350 }
2351
2352 \f
2353
2354 /* Foreign language type mangling section.  */
2355
2356 /* How to write the type codes for the integer Java type.  */
2357
2358 static void
2359 write_java_integer_type_codes (type)
2360      tree type;
2361 {
2362   if (type == java_int_type_node)
2363     write_char ('i');
2364   else if (type == java_short_type_node)
2365     write_char ('s');
2366   else if (type == java_byte_type_node)
2367     write_char ('c');
2368   else if (type == java_char_type_node)
2369     write_char ('w');
2370   else if (type == java_long_type_node)
2371     write_char ('x');
2372   else if (type == java_boolean_type_node)
2373     write_char ('b');
2374   else
2375     my_friendly_abort (20001207);
2376 }
2377