OSDN Git Service

2001-01-07 Alexandre Petit-Bianco <apbianco@cygnus.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / mangle.c
1 /* Name mangling for the new standard C++ ABI.
2    Copyright (C) 2000 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));
167 static void write_method_parms PARAMS ((tree, int));
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) && DECL_EXTERN_C_FUNCTION_P (decl))
606     /* The standard notes:
607          "The <encoding> of an extern "C" function is treated like
608          global-scope data, i.e. as its <source-name> without a type."  */
609     write_source_name (DECL_NAME (decl));
610   else
611     /* C++ name; needs to be mangled.  */
612     {
613       write_string ("_Z");
614       write_encoding (decl);
615     }
616 }
617
618 /*   <encoding>         ::= <function name> <bare-function-type>
619                         ::= <data name>  */
620
621 static void
622 write_encoding (decl)
623      tree decl;
624 {
625   MANGLE_TRACE_TREE ("encoding", decl);
626
627   if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
628     {
629       write_source_name (DECL_NAME (decl));
630       return;
631     }
632
633   write_name (decl, /*ignore_local_scope=*/0);
634   if (TREE_CODE (decl) == FUNCTION_DECL)
635     {
636       tree fn_type;
637
638       if (decl_is_template_id (decl, NULL))
639         fn_type = get_mostly_instantiated_function_type (decl, NULL, NULL);
640       else
641         fn_type = TREE_TYPE (decl);
642
643       write_bare_function_type (fn_type, 
644                                 (!DECL_CONSTRUCTOR_P (decl)
645                                  && !DECL_DESTRUCTOR_P (decl)
646                                  && !DECL_CONV_FN_P (decl)
647                                  && decl_is_template_id (decl, NULL)));
648     }
649 }
650
651 /* <name> ::= <unscoped-name>
652           ::= <unscoped-template-name> <template-args>
653           ::= <nested-name>
654           ::= <local-name>  
655
656    If IGNORE_LOCAL_SCOPE is non-zero, this production of <name> is
657    called from <local-name>, which mangles the enclosing scope
658    elsewhere and then uses this function to mangle just the part
659    underneath the function scope.  So don't use the <local-name>
660    production, to avoid an infinite recursion.  */
661
662 static void
663 write_name (decl, ignore_local_scope)
664      tree decl;
665      int ignore_local_scope;
666 {
667   tree context;
668
669   MANGLE_TRACE_TREE ("name", decl);
670
671   if (TREE_CODE (decl) == TYPE_DECL)
672     {
673       /* In case this is a typedef, fish out the corresponding
674          TYPE_DECL for the main variant.  */
675       decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
676       context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
677     }
678   else
679     context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
680
681   /* A decl in :: or ::std scope is treated specially.  The former is
682      mangled using <unscoped-name> or <unscoped-template-name>, the
683      latter with a special substitution.  Also, a name that is
684      directly in a local function scope is also mangled with
685      <unscoped-name> rather than a full <nested-name>.  */
686   if (context == NULL 
687       || context == global_namespace 
688       || DECL_NAMESPACE_STD_P (context)
689       || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
690     {
691       tree template_info;
692       /* Is this a template instance?  */
693       if (decl_is_template_id (decl, &template_info))
694         {
695           /* Yes: use <unscoped-template-name>.  */
696           write_unscoped_template_name (TI_TEMPLATE (template_info));
697           write_template_args (TI_ARGS (template_info));
698         }
699       else
700         /* Everything else gets an <unqualified-name>.  */
701         write_unscoped_name (decl);
702     }
703   else
704     {
705       /* Handle local names, unless we asked not to (that is, invoked
706          under <local-name>, to handle only the part of the name under
707          the local scope).  */
708       if (!ignore_local_scope)
709         {
710           /* Scan up the list of scope context, looking for a
711              function.  If we find one, this entity is in local
712              function scope.  local_entity tracks context one scope
713              level down, so it will contain the element that's
714              directly in that function's scope, either decl or one of
715              its enclosing scopes.  */
716           tree local_entity = decl;
717           while (context != NULL && context != global_namespace)
718             {
719               /* Make sure we're always dealing with decls.  */
720               if (context != NULL && TYPE_P (context))
721                 context = TYPE_NAME (context);
722               /* Is this a function?  */
723               if (TREE_CODE (context) == FUNCTION_DECL)
724                 {
725                   /* Yes, we have local scope.  Use the <local-name>
726                      production for the innermost function scope.  */
727                   write_local_name (context, local_entity, decl);
728                   return;
729                 }
730               /* Up one scope level.  */
731               local_entity = context;
732               context = CP_DECL_CONTEXT (context);
733             }
734
735           /* No local scope found?  Fall through to <nested-name>.  */
736         }
737
738       /* Other decls get a <nested-name> to encode their scope.  */
739       write_nested_name (decl);
740     }
741 }
742
743 /* <unscoped-name> ::= <unqualified-name>
744                    ::= St <unqualified-name>   # ::std::  */
745
746 static void
747 write_unscoped_name (decl)
748      tree decl;
749 {
750   tree context = CP_DECL_CONTEXT (decl);
751
752   MANGLE_TRACE_TREE ("unscoped-name", decl);
753
754   /* Is DECL in ::std?  */
755   if (DECL_NAMESPACE_STD_P (context))
756     {
757       write_string ("St");
758       write_unqualified_name (decl);
759     }
760   /* If not, it should be either in the global namespace, or directly
761      in a local function scope.  */
762   else if (context == global_namespace 
763            || context == NULL
764            || TREE_CODE (context) == FUNCTION_DECL)
765     write_unqualified_name (decl);
766   else 
767     my_friendly_abort (20000521);
768 }
769
770 /* <unscoped-template-name> ::= <unscoped-name>
771                             ::= <substitution>  */
772
773 static void
774 write_unscoped_template_name (decl)
775      tree decl;
776 {
777   MANGLE_TRACE_TREE ("unscoped-template-name", decl);
778
779   if (find_substitution (decl))
780     return;
781   write_unscoped_name (decl);
782   add_substitution (decl);
783 }
784
785 /* Write the nested name, including CV-qualifiers, of DECL.
786
787    <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E  
788                  ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
789
790    <CV-qualifiers> ::= [r] [V] [K]  */
791
792 static void
793 write_nested_name (decl)
794      tree decl;
795 {
796   tree template_info;
797
798   MANGLE_TRACE_TREE ("nested-name", decl);
799
800   write_char ('N');
801   
802   /* Write CV-qualifiers, if this is a member function.  */
803   if (TREE_CODE (decl) == FUNCTION_DECL 
804       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
805     {
806       if (DECL_VOLATILE_MEMFUNC_P (decl))
807         write_char ('V');
808       if (DECL_CONST_MEMFUNC_P (decl))
809         write_char ('K');
810     }
811
812   /* Is this a template instance?  */
813   if (decl_is_template_id (decl, &template_info))
814     {
815       /* Yes, use <template-prefix>.  */
816       write_template_prefix (decl);
817       write_template_args (TI_ARGS (template_info));
818     }
819   else
820     {
821       /* No, just use <prefix>  */
822       write_prefix (DECL_CONTEXT (decl));
823       write_unqualified_name (decl);
824     }
825   write_char ('E');
826 }
827
828 /* <prefix> ::= <prefix> <unqualified-name>>
829             ::= <template-prefix> <template-args>
830             ::= # empty
831             ::= <substitution>  */
832
833 static void
834 write_prefix (node)
835      tree node;
836 {
837   tree decl;
838   /* Non-NULL if NODE represents a template-id.  */
839   tree template_info = NULL;
840
841   MANGLE_TRACE_TREE ("prefix", node);
842
843   if (node == NULL
844       || node == global_namespace)
845     return;
846
847   if (find_substitution (node))
848     return;
849
850   if (DECL_P (node))
851     /* Node is a decl.  */
852     {
853       /* If this is a function decl, that means we've hit function
854          scope, so this prefix must be for a local name.  In this
855          case, we're under the <local-name> production, which encodes
856          the enclosing function scope elsewhere.  So don't continue
857          here.  */
858       if (TREE_CODE (node) == FUNCTION_DECL)
859         return;
860
861       decl = node;
862       decl_is_template_id (decl, &template_info);
863     }
864   else
865     /* Node is a type.  */
866     {
867       decl = TYPE_NAME (node);
868       if (CLASSTYPE_TEMPLATE_ID_P (node))
869         template_info = CLASSTYPE_TEMPLATE_INFO (node);
870     }
871
872   if (template_info != NULL)
873     /* Templated.  */
874     {
875       write_template_prefix (decl);
876       write_template_args (TI_ARGS (template_info));
877     }
878   else
879     /* Not templated.  */
880     {
881       write_prefix (CP_DECL_CONTEXT (decl));
882       write_unqualified_name (decl);
883     }
884
885   add_substitution (node);
886 }
887
888 /* <template-prefix> ::= <prefix> <template component>
889                      ::= <substitution>  */
890
891 static void
892 write_template_prefix (node)
893      tree node;
894 {
895   tree decl = DECL_P (node) ? node : TYPE_NAME (node);
896   tree type = DECL_P (node) ? TREE_TYPE (node) : node;
897   tree context = CP_DECL_CONTEXT (decl);
898   tree template_info;
899   tree template;
900   tree substitution;
901
902   MANGLE_TRACE_TREE ("template-prefix", node);
903
904   /* Find the template decl.  */
905   if (decl_is_template_id (decl, &template_info))
906     template = TI_TEMPLATE (template_info);
907   else if (CLASSTYPE_TEMPLATE_ID_P (type))
908     template = CLASSTYPE_TI_TEMPLATE (type);
909   else
910     /* Oops, not a template.  */
911     my_friendly_abort (20000524);
912
913   /* For a member template, though, the template name for the
914      innermost name must have all the outer template levels
915      instantiated.  For instance, consider
916
917        template<typename T> struct Outer {
918          template<typename U> struct Inner {};
919        };
920
921      The template name for `Inner' in `Outer<int>::Inner<float>' is
922      `Outer<int>::Inner<U>'.  In g++, we don't instantiate the template
923      levels separately, so there's no TEMPLATE_DECL available for this
924      (there's only `Outer<T>::Inner<U>').
925
926      In order to get the substitutions right, we create a special
927      TREE_LIST to represent the substitution candidate for a nested
928      template.  The TREE_PURPOSE is the template's context, fully
929      instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
930      template.
931
932      So, for the example above, `Outer<int>::Inner' is represented as a
933      substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
934      and whose value is `Outer<T>::Inner<U>'.  */
935   if (TYPE_P (context))
936     substitution = build_tree_list (context, template);
937   else
938     substitution = template;
939
940   if (find_substitution (substitution))
941     return;
942
943   write_prefix (context);
944   write_unqualified_name (decl);
945
946   add_substitution (substitution);
947 }
948
949 /* We don't need to handle thunks, vtables, or VTTs here.  Those are
950    mangled through special entry points.  
951
952     <unqualified-name>  ::= <operator-name>
953                         ::= <special-name>  
954                         ::= <source-name>  */
955
956 static void
957 write_unqualified_name (decl)
958      tree decl;
959 {
960   MANGLE_TRACE_TREE ("unqualified-name", decl);
961
962   if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
963     write_special_name_constructor (decl);
964   else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
965     write_special_name_destructor (decl);
966   else if (DECL_CONV_FN_P (decl)) 
967     {
968       /* Conversion operator. Handle it right here.  
969            <operator> ::= cv <type>  */
970       write_string ("cv");
971       write_type (TREE_TYPE (DECL_NAME (decl)));
972     }
973   else if (DECL_OVERLOADED_OPERATOR_P (decl))
974     {
975       operator_name_info_t *oni;
976       if (DECL_ASSIGNMENT_OPERATOR_P (decl))
977         oni = assignment_operator_name_info;
978       else
979         oni = operator_name_info;
980       
981       write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
982     }
983   else
984     write_source_name (DECL_NAME (decl));
985 }
986
987 /* Non-termial <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.  
988
989      <source-name> ::= </length/ number> <identifier>  */
990
991 static void
992 write_source_name (identifier)
993      tree identifier;
994 {
995   MANGLE_TRACE_TREE ("source-name", identifier);
996
997   /* Never write the whole template-id name including the template
998      arguments; we only want the template name.  */
999   if (IDENTIFIER_TEMPLATE (identifier))
1000     identifier = IDENTIFIER_TEMPLATE (identifier);
1001
1002   write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1003   write_identifier (IDENTIFIER_POINTER (identifier));
1004 }
1005
1006 /* Non-terminal <number>.
1007
1008      <number> ::= [n] </decimal integer/>  */
1009
1010 static void
1011 write_number (number, unsigned_p, base)
1012      unsigned HOST_WIDE_INT number;
1013      int unsigned_p;
1014      unsigned int base;
1015 {
1016   static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1017   unsigned HOST_WIDE_INT n;
1018   unsigned HOST_WIDE_INT m = 1;
1019
1020   if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1021     {
1022       write_char ('n');
1023       number = -((HOST_WIDE_INT) number);
1024     }
1025   
1026   /* Figure out how many digits there are.  */
1027   n = number;
1028   while (n >= base)
1029     {
1030       n /= base;
1031       m *= base;
1032     }
1033
1034   /* Write them out.  */
1035   while (m > 0)
1036     {
1037       int digit = number / m;
1038       write_char (digits[digit]);
1039       number -= digit * m;
1040       m /= base;
1041     }
1042
1043   my_friendly_assert (number == 0, 20000407);
1044 }
1045
1046 /* Write out an integeral CST in decimal.  */
1047
1048 static inline void
1049 write_integer_cst (cst)
1050      tree cst;
1051 {
1052   if (tree_int_cst_sgn (cst) >= 0) 
1053     {
1054       if (TREE_INT_CST_HIGH (cst) != 0)
1055         sorry ("mangling very large integers");
1056       write_unsigned_number (TREE_INT_CST_LOW (cst));
1057     }
1058   else
1059     write_signed_number (tree_low_cst (cst, 0));
1060 }
1061
1062 /* Non-terminal <identifier>.
1063
1064      <identifier> ::= </unqualified source code identifier>  */
1065
1066 static void
1067 write_identifier (identifier)
1068      const char *identifier;
1069 {
1070   MANGLE_TRACE ("identifier", identifier);
1071   write_string (identifier);
1072 }
1073
1074 /* Handle constructor productions of non-terminal <special-name>.
1075    CTOR is a constructor FUNCTION_DECL. 
1076
1077      <special-name> ::= C1   # complete object constructor
1078                     ::= C2   # base object constructor
1079                     ::= C3   # complete object allocating constructor
1080
1081    Currently, allocating constructors are never used. 
1082
1083    We also need to provide unique mangled names (which should never be
1084    exported) for the constructor that takes an in-charge parameter,
1085    and for a constructor whose name is the same as its class's name.
1086    We use "C*INTERNAL*" for these.  */
1087
1088 static void
1089 write_special_name_constructor (ctor)
1090      tree ctor;
1091 {
1092   if (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1093       /* Even though we don't ever emit a definition of the
1094          old-style destructor, we still have to consider entities
1095          (like static variables) nested inside it.  */
1096       || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1097     write_string ("C1");
1098   else if (DECL_BASE_CONSTRUCTOR_P (ctor))
1099     write_string ("C2");
1100   else
1101     my_friendly_abort (20001115);
1102 }
1103
1104 /* Handle destructor productions of non-terminal <special-name>.
1105    DTOR is a denstructor FUNCTION_DECL. 
1106
1107      <special-name> ::= D0 # deleting (in-charge) destructor
1108                     ::= D1 # complete object (in-charge) destructor
1109                     ::= D2 # base object (not-in-charge) destructor 
1110
1111    We also need to provide unique mngled names for old-ABI
1112    destructors, sometimes.  These should only be used internally.  We
1113    use "D*INTERNAL*" for these.  */
1114
1115 static void
1116 write_special_name_destructor (dtor)
1117      tree dtor;
1118 {
1119   if (DECL_DELETING_DESTRUCTOR_P (dtor))
1120     write_string ("D0");
1121   else if (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1122            /* Even though we don't ever emit a definition of the
1123               old-style destructor, we still have to consider entities
1124               (like static variables) nested inside it.  */
1125            || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1126     write_string ("D1");
1127   else if (DECL_BASE_DESTRUCTOR_P (dtor))
1128     write_string ("D2");
1129   else
1130     my_friendly_abort (20001115);
1131 }
1132
1133 /* Return the discriminator for ENTITY appearing inside
1134    FUNCTION.  The discriminator is the lexical ordinal of VAR among
1135    entities with the same name in the same FUNCTION.  */
1136
1137 static int
1138 discriminator_for_local_entity (entity)
1139      tree entity;
1140 {
1141   tree *type;
1142   int discriminator;
1143
1144   /* Assume this is the only local entity with this name.  */
1145   discriminator = 0;
1146
1147   /* For now, we don't discriminate amongst local variables.  */
1148   if (TREE_CODE (entity) != TYPE_DECL)
1149     return 0;
1150
1151   /* Scan the list of local classes.  */
1152   entity = TREE_TYPE (entity);
1153   for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
1154     if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1155         && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1156       ++discriminator;
1157
1158   return discriminator;
1159 }
1160
1161 /* Return the discriminator for STRING, a string literal used inside
1162    FUNCTION.  The disciminator is the lexical ordinal of STRING among
1163    string literals used in FUNCTION.  */
1164
1165 static int
1166 discriminator_for_string_literal (function, string)
1167      tree function ATTRIBUTE_UNUSED;
1168      tree string ATTRIBUTE_UNUSED;
1169 {
1170   /* For now, we don't discriminate amongst string literals.  */
1171   return 0;
1172 }
1173
1174 /*   <discriminator> := _ <number>   
1175
1176    The discriminator is used only for the second and later occurrences
1177    of the same name within a single function. In this case <number> is
1178    n - 2, if this is the nth occurrence, in lexical order.  */
1179
1180 static void
1181 write_discriminator (discriminator)
1182      int discriminator;
1183 {
1184   /* If discriminator is zero, don't write anything.  Otherwise... */
1185   if (discriminator > 0)
1186     {
1187       write_char ('_');
1188       /* The number is omitted for discriminator == 1.  Beyond 1, the
1189          numbering starts at 0.  */
1190       if (discriminator > 1)
1191         write_unsigned_number (discriminator - 2);
1192     }
1193 }
1194
1195 /* Mangle the name of a function-scope entity.  FUNCTION is the
1196    FUNCTION_DECL for the enclosing function.  ENTITY is the decl for
1197    the entity itself.  LOCAL_ENTITY is the entity that's directly
1198    scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1199    of ENTITY.
1200
1201      <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1202                   := Z <function encoding> E s [<discriminator>]  */
1203
1204 static void
1205 write_local_name (function, local_entity, entity)
1206      tree function;
1207      tree local_entity;
1208      tree entity;
1209 {
1210   MANGLE_TRACE_TREE ("local-name", entity);
1211
1212   write_char ('Z');
1213   write_encoding (function);
1214   write_char ('E');
1215   if (TREE_CODE (entity) == STRING_CST)
1216     {
1217       write_char ('s');
1218       write_discriminator (discriminator_for_string_literal (function, 
1219                                                              entity));
1220     }
1221   else
1222     {
1223       /* Now the <entity name>.  Let write_name know its being called
1224          from <local-name>, so it doesn't try to process the enclosing
1225          function scope again.  */
1226       write_name (entity, /*ignore_local_scope=*/1);
1227       write_discriminator (discriminator_for_local_entity (local_entity));
1228     }
1229 }
1230
1231 /* Non-terminals <type> and <CV-qualifier>.  
1232
1233      <type> ::= <builtin-type>
1234             ::= <function-type>
1235             ::= <class-enum-type>
1236             ::= <array-type>
1237             ::= <pointer-to-member-type>
1238             ::= <template-param>
1239             ::= <substitution>
1240             ::= <CV-qualifier>
1241             ::= P <type>    # pointer-to
1242             ::= R <type>    # reference-to
1243             ::= C <type>    # complex pair (C 2000)
1244             ::= G <type>    # imaginary (C 2000)     [not supported]
1245             ::= U <source-name> <type>   # vendor extended type qualifier 
1246
1247    TYPE is a type node.  */
1248
1249 static void 
1250 write_type (type)
1251      tree type;
1252 {
1253   /* This gets set to non-zero if TYPE turns out to be a (possibly
1254      CV-qualified) builtin type.  */
1255   int is_builtin_type = 0;
1256
1257   MANGLE_TRACE_TREE ("type", type);
1258
1259   if (type == error_mark_node)
1260     return;
1261
1262   if (find_substitution (type))
1263     return;
1264   
1265   if (write_CV_qualifiers_for_type (type) > 0)
1266     /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1267        mangle the unqualified type.  The recursive call is needed here
1268        since both the qualified and uqualified types are substitution
1269        candidates.  */
1270     write_type (TYPE_MAIN_VARIANT (type));
1271   else
1272     {
1273       /* See through any typedefs.  */
1274       type = TYPE_MAIN_VARIANT (type);
1275
1276       switch (TREE_CODE (type))
1277         {
1278         case VOID_TYPE:
1279         case BOOLEAN_TYPE:
1280         case INTEGER_TYPE:  /* Includes wchar_t.  */
1281         case REAL_TYPE:
1282           /* If this is a typedef, TYPE may not be one of
1283              the standard builtin type nodes, but an alias of one.  Use
1284              TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
1285           write_builtin_type (TYPE_MAIN_VARIANT (type));
1286           ++is_builtin_type;
1287           break;
1288
1289         case COMPLEX_TYPE:
1290           write_char ('C');
1291           write_type (TREE_TYPE (type));
1292           break;
1293
1294         case FUNCTION_TYPE:
1295         case METHOD_TYPE:
1296           write_function_type (type);
1297           break;
1298
1299         case UNION_TYPE:
1300         case RECORD_TYPE:
1301         case ENUMERAL_TYPE:
1302           /* A pointer-to-member function is represented as a special
1303              RECORD_TYPE, so check for this first.  */
1304           if (TYPE_PTRMEMFUNC_P (type))
1305             write_pointer_to_member_type (type);
1306           else
1307             write_class_enum_type (type);
1308           break;
1309
1310         case TYPENAME_TYPE:
1311           /* We handle TYPENAME_TYPEs like ordinary nested names.  */
1312           write_nested_name (TYPE_STUB_DECL (type));
1313           break;
1314
1315         case ARRAY_TYPE:
1316           write_array_type (type);
1317           break;
1318
1319         case POINTER_TYPE:
1320           /* A pointer-to-member variable is represented by a POINTER_TYPE
1321              to an OFFSET_TYPE, so check for this first.  */
1322           if (TYPE_PTRMEM_P (type))
1323             write_pointer_to_member_type (type);
1324           else
1325             {
1326               write_char ('P');
1327               write_type (TREE_TYPE (type));
1328             }
1329           break;
1330
1331         case REFERENCE_TYPE:
1332           write_char ('R');
1333           write_type (TREE_TYPE (type));
1334           break;
1335
1336         case TEMPLATE_TYPE_PARM:
1337         case TEMPLATE_PARM_INDEX:
1338           write_template_param (type);
1339           break;
1340
1341         case TEMPLATE_TEMPLATE_PARM:
1342           write_template_template_param (type);
1343           break;
1344
1345         case BOUND_TEMPLATE_TEMPLATE_PARM:
1346           write_template_template_param (type);
1347           write_template_args 
1348             (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1349           break;
1350
1351         case OFFSET_TYPE:
1352           write_pointer_to_member_type (build_pointer_type (type));
1353           break;
1354
1355         case VECTOR_TYPE:
1356           write_string ("U8__vector");
1357           write_type (TREE_TYPE (type));
1358           break;
1359
1360         default:
1361           my_friendly_abort (20000409);
1362         }
1363     }
1364
1365   /* Types other than builtin types are substitution candidates.  */
1366   if (!is_builtin_type)
1367     add_substitution (type);
1368 }
1369
1370 /* Non-terminal <CV-qualifiers> for type nodes.  Returns the number of
1371    CV-qualifiers written for TYPE.
1372
1373      <CV-qualifiers> ::= [r] [V] [K]  */
1374
1375 static int
1376 write_CV_qualifiers_for_type (type)
1377      tree type;
1378 {
1379   int num_qualifiers = 0;
1380
1381   /* The order is specified by:
1382
1383        "In cases where multiple order-insensitive qualifiers are
1384        present, they should be ordered 'K' (closest to the base type),
1385        'V', 'r', and 'U' (farthest from the base type) ..."  */
1386
1387   if (CP_TYPE_RESTRICT_P (type))
1388     {
1389       write_char ('r');
1390       ++num_qualifiers;
1391     }
1392   if (CP_TYPE_VOLATILE_P (type))
1393     {
1394       write_char ('V');
1395       ++num_qualifiers;
1396     }
1397   if (CP_TYPE_CONST_P (type))
1398     {
1399       write_char ('K');
1400       ++num_qualifiers;
1401     }
1402
1403   return num_qualifiers;
1404 }
1405
1406 /* Non-terminal <builtin-type>. 
1407
1408      <builtin-type> ::= v   # void 
1409                     ::= b   # bool
1410                     ::= w   # wchar_t
1411                     ::= c   # char
1412                     ::= a   # signed char
1413                     ::= h   # unsigned char
1414                     ::= s   # short
1415                     ::= t   # unsigned short
1416                     ::= i   # int
1417                     ::= j   # unsigned int
1418                     ::= l   # long
1419                     ::= m   # unsigned long
1420                     ::= x   # long long, __int64
1421                     ::= y   # unsigned long long, __int64  
1422                     ::= n   # __int128            [not supported]
1423                     ::= o   # unsigned __int128   [not supported] 
1424                     ::= f   # float
1425                     ::= d   # double
1426                     ::= e   # long double, __float80 
1427                     ::= g   # __float128          [not supported]
1428                     ::= u <source-name>  # vendor extended type */
1429
1430 static void 
1431 write_builtin_type (type)
1432      tree type;
1433 {
1434   switch (TREE_CODE (type))
1435     {
1436     case VOID_TYPE:
1437       write_char ('v');
1438       break;
1439
1440     case BOOLEAN_TYPE:
1441       write_char ('b');
1442       break;
1443
1444     case INTEGER_TYPE:
1445       /* If this is size_t, get the underlying int type.  */
1446       if (TYPE_IS_SIZETYPE (type))
1447         type = TYPE_DOMAIN (type);
1448
1449       /* TYPE may still be wchar_t, since that isn't in
1450          integer_type_nodes.  */
1451       if (type == wchar_type_node)
1452         write_char ('w');
1453       if (TYPE_FOR_JAVA (type))
1454         write_java_integer_type_codes (type);
1455       else
1456         {
1457           size_t itk;
1458           /* Assume TYPE is one of the shared integer type nodes.  Find
1459              it in the array of these nodes.  */
1460         iagain:
1461           for (itk = 0; itk < itk_none; ++itk)
1462             if (type == integer_types[itk])
1463               {
1464                 /* Print the corresponding single-letter code.  */
1465                 write_char (integer_type_codes[itk]);
1466                 break;
1467               }
1468           
1469           if (itk == itk_none)
1470             {
1471               tree t = type_for_mode (TYPE_MODE (type), TREE_UNSIGNED (type));
1472               if (type == t)
1473                 /* Couldn't find this type.  */
1474                 my_friendly_abort (20000408);
1475               type = t;
1476               goto iagain;
1477             }
1478         }
1479       break;
1480
1481     case REAL_TYPE:
1482       if (type == float_type_node
1483           || type == java_float_type_node)
1484         write_char ('f');
1485       else if (type == double_type_node
1486                || type == java_double_type_node)
1487         write_char ('d');
1488       else if (type == long_double_type_node)
1489         write_char ('e');
1490       else
1491         my_friendly_abort (20000409);
1492       break;
1493
1494     default:
1495       my_friendly_abort (20000509);
1496     }
1497 }
1498
1499 /* Non-terminal <function-type>.  NODE is a FUNCTION_TYPE or
1500    METHOD_TYPE.  The return type is mangled before the parameter
1501    types.
1502
1503      <function-type> ::= F [Y] <bare-function-type> E   */
1504
1505 static void
1506 write_function_type (type)
1507      tree type;
1508 {
1509   MANGLE_TRACE_TREE ("function-type", type);
1510
1511   write_char ('F');
1512   /* We don't track whether or not a type is `extern "C"'.  Note that
1513      you can have an `extern "C"' function that does not have
1514      `extern "C"' type, and vice versa:
1515
1516        extern "C" typedef void function_t();
1517        function_t f; // f has C++ linkage, but its type is
1518                      // `extern "C"'
1519
1520        typedef void function_t();
1521        extern "C" function_t f; // Vice versa.
1522
1523      See [dcl.link].  */
1524   write_bare_function_type (type, /*include_return_type_p=*/1);
1525   write_char ('E');
1526 }
1527
1528 /* Non-terminal <bare-function-type>.  NODE is a FUNCTION_DECL or a
1529    METHOD_TYPE.  If INCLUDE_RETURN_TYPE is non-zero, the return value
1530    is mangled before the parameter types.
1531
1532      <bare-function-type> ::= </signature/ type>+  */
1533
1534 static void
1535 write_bare_function_type (type, include_return_type_p)
1536      tree type;
1537      int include_return_type_p;
1538 {
1539   MANGLE_TRACE_TREE ("bare-function-type", type);
1540
1541   /* Mangle the return type, if requested.  */
1542   if (include_return_type_p)
1543     write_type (TREE_TYPE (type));
1544
1545   /* Now mangle the types of the arguments.  */
1546   write_method_parms (TYPE_ARG_TYPES (type), 
1547                       TREE_CODE (type) == METHOD_TYPE);
1548 }
1549
1550 /* Write the mangled representation of a method parameter list of
1551    types given in PARM_LIST.  If METHOD_P is non-zero, the function is 
1552    considered a non-static method, and the this parameter is omitted.  */
1553
1554 static void
1555 write_method_parms (parm_list, method_p)
1556      tree parm_list;
1557      int method_p;
1558 {
1559   tree first_parm;
1560   /* Assume this parameter type list is variable-length.  If it ends
1561      with a void type, then it's not.  */
1562   int varargs_p = 1;
1563
1564   /* If this is a member function, skip the first arg, which is the
1565      this pointer.  
1566        "Member functions do not encode the type of their implicit this
1567        parameter."  */
1568   if (method_p)
1569     parm_list = TREE_CHAIN (parm_list);
1570
1571   for (first_parm = parm_list; 
1572        parm_list; 
1573        parm_list = TREE_CHAIN (parm_list))
1574     {
1575       tree parm = TREE_VALUE (parm_list);
1576
1577       if (parm == void_type_node)
1578         {
1579           /* "Empty parameter lists, whether declared as () or
1580              conventionally as (void), are encoded with a void parameter
1581              (v)."  */
1582           if (parm_list == first_parm)
1583             write_type (parm);
1584           /* If the parm list is terminated with a void type, it's
1585              fixed-length.  */
1586           varargs_p = 0;
1587           /* A void type better be the last one.  */
1588           my_friendly_assert (TREE_CHAIN (parm_list) == NULL, 20000523);
1589         }
1590       else
1591         write_type (parm);
1592     }
1593
1594   if (varargs_p)
1595     /* <builtin-type> ::= z  # ellipsis  */
1596     write_char ('z');
1597 }
1598
1599 /* <class-enum-type> ::= <name>  */
1600
1601 static void 
1602 write_class_enum_type (type)
1603      tree type;
1604 {
1605   write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1606 }
1607
1608 /* Non-terminal <template-args>.  ARGS is a TREE_VEC of template
1609    arguments.
1610
1611      <template-args> ::= I <template-arg>+ E  */
1612
1613 static void
1614 write_template_args (args)
1615      tree args;
1616 {
1617   int i;
1618   int length = TREE_VEC_LENGTH (args);
1619
1620   MANGLE_TRACE_TREE ("template-args", args);
1621
1622   my_friendly_assert (length > 0, 20000422);
1623
1624   if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1625     {
1626       /* We have nested template args.  We want the innermost template
1627          argument list.  */
1628       args = TREE_VEC_ELT (args, length - 1);
1629       length = TREE_VEC_LENGTH (args);
1630     }
1631
1632   write_char ('I');
1633   for (i = 0; i < length; ++i)
1634     write_template_arg (TREE_VEC_ELT (args, i));
1635   write_char ('E');
1636 }
1637
1638 /* <expression> ::= <unary operator-name> <expression>
1639                 ::= <binary operator-name> <expression> <expression>
1640                 ::= <expr-primary>
1641
1642    <expr-primary> ::= <template-param>
1643                   ::= L <type> <value number> E  # literal
1644                   ::= L <mangled-name> E         # external name  */
1645
1646 static void
1647 write_expression (expr)
1648      tree expr;
1649 {
1650   enum tree_code code;
1651
1652   code = TREE_CODE (expr);
1653
1654   /* Handle pointers-to-members by making them look like expression
1655      nodes.  */
1656   if (code == PTRMEM_CST)
1657     {
1658       expr = build_nt (ADDR_EXPR,
1659                        build_nt (SCOPE_REF,
1660                                  PTRMEM_CST_CLASS (expr),
1661                                  PTRMEM_CST_MEMBER (expr)));
1662       code = TREE_CODE (expr);
1663     }
1664
1665   /* Handle template parameters. */
1666   if (code == TEMPLATE_TYPE_PARM 
1667       || code == TEMPLATE_TEMPLATE_PARM
1668       || code == BOUND_TEMPLATE_TEMPLATE_PARM
1669       || code == TEMPLATE_PARM_INDEX)
1670     write_template_param (expr);
1671   /* Handle literals.  */
1672   else if (TREE_CODE_CLASS (code) == 'c')
1673     write_template_arg_literal (expr);
1674   else if (DECL_P (expr))
1675     {
1676       write_char ('L');
1677       write_mangled_name (expr);
1678       write_char ('E');
1679     }
1680   else
1681     {
1682       int i;
1683
1684       /* Skip NOP_EXPRs.  They can occur when (say) a pointer argument
1685          is converted (via qualification conversions) to another
1686          type.  */
1687       while (TREE_CODE (expr) == NOP_EXPR)
1688         {
1689           expr = TREE_OPERAND (expr, 0);
1690           code = TREE_CODE (expr);
1691         }
1692
1693       /* When we bind a variable or function to a non-type template
1694          argument with reference type, we create an ADDR_EXPR to show
1695          the fact that the entity's address has been taken.  But, we
1696          don't actually want to output a mangling code for the `&'.  */
1697       if (TREE_CODE (expr) == ADDR_EXPR
1698           && TREE_TYPE (expr)
1699           && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
1700         {
1701           expr = TREE_OPERAND (expr, 0);
1702           if (DECL_P (expr))
1703             {
1704               write_expression (expr);
1705               return;
1706             }
1707
1708           code = TREE_CODE (expr);
1709         }
1710       
1711       /* If it wasn't any of those, recursively expand the expression.  */
1712       write_string (operator_name_info[(int) code].mangled_name);
1713
1714       /* Handle pointers-to-members specially.  */
1715       if (code == SCOPE_REF)
1716         {
1717           write_type (TREE_OPERAND (expr, 0));
1718           if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
1719             write_source_name (TREE_OPERAND (expr, 1));
1720           else
1721             write_encoding (TREE_OPERAND (expr, 1));
1722         }
1723       else
1724         for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
1725           write_expression (TREE_OPERAND (expr, i));
1726     }
1727 }
1728
1729 /* Literal subcase of non-terminal <template-arg>.  
1730
1731      "Literal arguments, e.g. "A<42L>", are encoded with their type
1732      and value. Negative integer values are preceded with "n"; for
1733      example, "A<-42L>" becomes "1AILln42EE". The bool value false is
1734      encoded as 0, true as 1. If floating-point arguments are accepted
1735      as an extension, their values should be encoded using a
1736      fixed-length lowercase hexadecimal string corresponding to the
1737      internal representation (IEEE on IA-64), high-order bytes first,
1738      without leading zeroes. For example: "Lfbff000000E" is -1.0f."  */
1739
1740 static void
1741 write_template_arg_literal (value)
1742      tree value;
1743 {
1744   tree type = TREE_TYPE (value);
1745   write_char ('L');
1746   write_type (type);
1747
1748   if (TREE_CODE (value) == CONST_DECL)
1749     write_integer_cst (DECL_INITIAL (value));
1750   else if (TREE_CODE (value) == INTEGER_CST)
1751     {
1752       if (same_type_p (type, boolean_type_node))
1753         {
1754           if (value == boolean_false_node || integer_zerop (value))
1755             write_unsigned_number (0);
1756           else if (value == boolean_true_node)
1757             write_unsigned_number (1);
1758           else 
1759             my_friendly_abort (20000412);
1760         }
1761       else
1762         write_integer_cst (value);
1763     }
1764   else if (TREE_CODE (value) == REAL_CST)
1765     {
1766 #ifdef CROSS_COMPILE
1767       static int explained;
1768
1769       if (!explained) 
1770         {
1771           sorry ("real-valued template parameters when cross-compiling");
1772           explained = 1;
1773         }
1774 #else
1775       size_t i;
1776       for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1777         write_number (((unsigned char *) 
1778                        &TREE_REAL_CST (value))[i], 
1779                       /*unsigned_p=*/1,
1780                       16);
1781 #endif
1782     }
1783   else
1784     my_friendly_abort (20000412);
1785
1786   write_char ('E');
1787 }
1788
1789 /* Non-terminal <tempalate-arg>.  
1790
1791      <template-arg> ::= <type>                        # type
1792                     ::= L <type> </value/ number> E   # literal
1793                     ::= LZ <name> E                   # external name
1794                     ::= X <expression> E              # expression  */
1795
1796 static void
1797 write_template_arg (node)
1798      tree node;
1799 {
1800   enum tree_code code = TREE_CODE (node);
1801
1802   MANGLE_TRACE_TREE ("template-arg", node);
1803
1804   /* A template template paramter's argument list contains TREE_LIST
1805      nodes of which the value field is the the actual argument.  */
1806   if (code == TREE_LIST)
1807     {
1808       node = TREE_VALUE (node);
1809       /* If it's a decl, deal with its type instead.  */
1810       if (DECL_P (node))
1811         {
1812           node = TREE_TYPE (node);
1813           code = TREE_CODE (node);
1814         }
1815     }
1816
1817   if (TYPE_P (node))
1818     write_type (node);
1819   else if (code == TEMPLATE_DECL)
1820     /* A template appearing as a template arg is a template template arg.  */
1821     write_template_template_arg (node);
1822   else if (DECL_P (node))
1823     {
1824       write_char ('L');
1825       write_char ('Z');
1826       write_encoding (node);
1827       write_char ('E');
1828     }
1829   else if (TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST)
1830     write_template_arg_literal (node);
1831   else
1832     {
1833       /* Template arguments may be expressions.  */
1834       write_char ('X');
1835       write_expression (node);
1836       write_char ('E');
1837     }
1838 }
1839
1840 /*  <template-template-arg>
1841                         ::= <name>
1842                         ::= <substitution>  */
1843
1844 void
1845 write_template_template_arg (tree decl)
1846 {
1847   MANGLE_TRACE_TREE ("template-template-arg", decl);
1848
1849   if (find_substitution (decl))
1850     return;
1851   write_name (decl, /*ignore_local_scope=*/0);
1852   add_substitution (decl);
1853 }
1854
1855
1856 /* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.  
1857
1858      <array-type> ::= A [</dimension/ number>] _ </element/ type>  
1859                   ::= A <expression> _ </element/ type>
1860
1861      "Array types encode the dimension (number of elements) and the
1862      element type. For variable length arrays, the dimension (but not
1863      the '_' separator) is omitted."  */
1864
1865 static void 
1866 write_array_type (type)
1867   tree type;
1868 {
1869   write_char ('A');
1870   if (TYPE_DOMAIN (type))
1871     {
1872       tree index_type;
1873       tree max;
1874
1875       index_type = TYPE_DOMAIN (type);
1876       /* The INDEX_TYPE gives the upper and lower bounds of the
1877          array.  */
1878       max = TYPE_MAX_VALUE (index_type);
1879       if (TREE_CODE (max) == INTEGER_CST)
1880         {
1881           /* The ABI specifies that we should mangle the number of
1882              elements in the array, not the largest allowed index.  */
1883           max = size_binop (PLUS_EXPR, max, size_one_node);
1884           write_unsigned_number (tree_low_cst (max, 1));
1885         }
1886       else
1887         write_expression (TREE_OPERAND (max, 0));
1888     }
1889   write_char ('_');
1890   write_type (TREE_TYPE (type));
1891 }
1892
1893 /* Non-terminal <pointer-to-member-type> for pointer-to-member
1894    variables.  TYPE is a pointer-to-member POINTER_TYPE.
1895
1896      <pointer-to-member-type> ::= M </class/ type> </member/ type>  */
1897
1898 static void
1899 write_pointer_to_member_type (type)
1900      tree type;
1901 {
1902   write_char ('M');
1903   write_type (TYPE_PTRMEM_CLASS_TYPE (type));
1904   write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1905 }
1906
1907 /* Non-terminal <template-param>.  PARM is a TEMPLATE_TYPE_PARM,
1908    TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
1909    TEMPLATE_PARM_INDEX.
1910
1911      <template-param> ::= T </parameter/ number> _  */
1912
1913 static void
1914 write_template_param (parm)
1915      tree parm;
1916 {
1917   int parm_index;
1918
1919   MANGLE_TRACE_TREE ("template-parm", parm);
1920
1921   switch (TREE_CODE (parm))
1922     {
1923     case TEMPLATE_TYPE_PARM:
1924     case TEMPLATE_TEMPLATE_PARM:
1925     case BOUND_TEMPLATE_TEMPLATE_PARM:
1926       parm_index = TEMPLATE_TYPE_IDX (parm);
1927       break;
1928
1929     case TEMPLATE_PARM_INDEX:
1930       parm_index = TEMPLATE_PARM_IDX (parm);
1931       break;
1932
1933     default:
1934       my_friendly_abort (20000523);
1935     }
1936
1937   write_char ('T');
1938   /* NUMBER as it appears in the mangling is (-1)-indexed, with the
1939      earliest template param denoted by `_'.  */
1940   if (parm_index > 0)
1941     write_unsigned_number (parm_index - 1);
1942   write_char ('_');
1943 }
1944
1945 /*  <template-template-param>
1946                         ::= <template-param> 
1947                         ::= <substitution>  */
1948
1949 static void
1950 write_template_template_param (parm)
1951      tree parm;
1952 {
1953   tree template = NULL_TREE;
1954
1955   /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
1956      template template parameter.  The substitution candidate here is
1957      only the template.  */
1958   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
1959     {
1960       template 
1961         = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
1962       if (find_substitution (template))
1963         return;
1964     }
1965
1966   /* <template-param> encodes only the template parameter position,
1967      not its template arguments, which is fine here.  */
1968   write_template_param (parm);
1969   if (template)
1970     add_substitution (template);
1971 }
1972
1973 /* Non-terminal <substitution>.  
1974
1975       <substitution> ::= S <seq-id> _
1976                      ::= S_  */
1977
1978 static void
1979 write_substitution (seq_id)
1980      int seq_id;
1981 {
1982   MANGLE_TRACE ("substitution", "");
1983
1984   write_char ('S');
1985   if (seq_id > 0)
1986     write_number (seq_id - 1, /*unsigned=*/1, 36);
1987   write_char ('_');
1988 }
1989
1990 /* Start mangling a new name or type.  */
1991
1992 static inline void
1993 start_mangling ()
1994 {
1995   obstack_free (&G.name_obstack, obstack_base (&G.name_obstack));
1996 }
1997
1998 /* Done with mangling.  Return the generated mangled name.  */
1999
2000 static inline const char *
2001 finish_mangling ()
2002 {
2003   /* Clear all the substitutions.  */
2004   VARRAY_POP_ALL (G.substitutions);
2005
2006   /* Null-terminate the string.  */
2007   write_char ('\0');
2008
2009   return (const char *) obstack_base (&G.name_obstack);
2010 }
2011
2012 /* Initialize data structures for mangling.  */
2013
2014 void
2015 init_mangle ()
2016 {
2017   gcc_obstack_init (&G.name_obstack);
2018   VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
2019
2020   /* Cache these identifiers for quick comparison when checking for
2021      standard substitutions.  */
2022   subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2023   subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2024   subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2025   subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2026   subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2027   subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2028 }
2029
2030 /* Generate the mangled name of DECL.  */
2031
2032 static const char *
2033 mangle_decl_string (decl)
2034      tree decl;
2035 {
2036   const char *result;
2037
2038   start_mangling ();
2039
2040   if (TREE_CODE (decl) == TYPE_DECL)
2041     write_type (TREE_TYPE (decl));
2042   else
2043     {
2044       write_mangled_name (decl);
2045       if (DECL_LANG_SPECIFIC (decl)
2046           && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
2047               || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
2048         /* We need a distinct mangled name for these entities, but
2049            we should never actually output it.  So, we append some
2050            characters the assembler won't like.  */
2051         write_string (" *INTERNAL* ");
2052     }
2053
2054   result = finish_mangling ();
2055   if (DEBUG_MANGLE)
2056     fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2057   return result;
2058 }
2059
2060 /* Create an identifier for the external mangled name of DECL.  */
2061
2062 tree
2063 mangle_decl (decl)
2064      tree decl;
2065 {
2066   return get_identifier (mangle_decl_string (decl));
2067 }
2068
2069 /* Generate the mangled representation of TYPE.  */
2070
2071 const char *
2072 mangle_type_string (type)
2073      tree type;
2074 {
2075   const char *result;
2076
2077   start_mangling ();
2078   write_type (type);
2079   result = finish_mangling ();
2080   if (DEBUG_MANGLE)
2081     fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2082   return result;
2083 }
2084
2085 /* Create an identifier for the mangled representation of TYPE.  */
2086
2087 tree
2088 mangle_type (type)
2089      tree type;
2090 {
2091   return get_identifier (mangle_type_string (type));
2092 }
2093
2094 /* Create an identifier for the mangled name of a special component
2095    for belonging to TYPE.  CODE is the ABI-specified code for this
2096    component.  */
2097
2098 static tree
2099 mangle_special_for_type (type, code)
2100      tree type;
2101      const char *code;
2102 {
2103   const char *result;
2104
2105   /* We don't have an actual decl here for the special component, so
2106      we can't just process the <encoded-name>.  Instead, fake it.  */
2107   start_mangling ();
2108
2109   /* Start the mangling.  */
2110   write_string ("_Z");
2111   write_string (code);
2112
2113   /* Add the type.  */
2114   write_type (type);
2115   result = finish_mangling ();
2116
2117   if (DEBUG_MANGLE)
2118     fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2119
2120   return get_identifier (result);
2121 }
2122
2123 /* Create an identifier for the mangled representation of the typeinfo
2124    structure for TYPE.  */
2125
2126 tree
2127 mangle_typeinfo_for_type (type)
2128      tree type;
2129 {
2130   return mangle_special_for_type (type, "TI");
2131 }
2132
2133 /* Create an identifier for the mangled name of the NTBS containing
2134    the mangled name of TYPE.  */
2135
2136 tree
2137 mangle_typeinfo_string_for_type (type)
2138      tree type;
2139 {
2140   return mangle_special_for_type (type, "TS");
2141 }
2142
2143 /* Create an identifier for the mangled name of the vtable for TYPE.  */
2144
2145 tree
2146 mangle_vtbl_for_type (type)
2147      tree type;
2148 {
2149   return mangle_special_for_type (type, "TV");
2150 }
2151
2152 /* Returns an identifier for the mangled name of the VTT for TYPE.  */
2153
2154 tree
2155 mangle_vtt_for_type (type)
2156      tree type;
2157 {
2158   return mangle_special_for_type (type, "TT");
2159 }
2160
2161 /* Return an identifier for a construction vtable group.  TYPE is
2162    the most derived class in the hierarchy; BINFO is the base
2163    subobject for which this construction vtable group will be used.  
2164
2165    This mangling isn't part of the ABI specification; in the ABI
2166    specification, the vtable group is dumped in the same COMDAT as the
2167    main vtable, and is referenced only from that vtable, so it doesn't
2168    need an external name.  For binary formats without COMDAT sections,
2169    though, we need external names for the vtable groups.  
2170
2171    We use the production
2172
2173     <special-name> ::= CT <type> <offset number> _ <base type>  */
2174
2175 tree
2176 mangle_ctor_vtbl_for_type (type, binfo)
2177      tree type;
2178      tree binfo;
2179 {
2180   const char *result;
2181
2182   start_mangling ();
2183
2184   write_string ("_Z");
2185   write_string ("TC");
2186   write_type (type);
2187   write_integer_cst (BINFO_OFFSET (binfo));
2188   write_char ('_');
2189   write_type (BINFO_TYPE (binfo));
2190
2191   result = finish_mangling ();
2192   if (DEBUG_MANGLE)
2193     fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2194   return get_identifier (result);
2195 }
2196
2197 /* Return an identifier for the mangled name of a thunk to FN_DECL.
2198    OFFSET is the initial adjustment to this used to find the vptr.  If
2199    VCALL_OFFSET is non-NULL, this is a virtual thunk, and it is the
2200    vtbl offset in bytes.  
2201
2202     <special-name> ::= Th <offset number> _ <base encoding>
2203                    ::= Tv <offset number> _ <vcall offset number> _
2204                                                             <base encoding>
2205 */
2206
2207 tree
2208 mangle_thunk (fn_decl, offset, vcall_offset)
2209      tree fn_decl;
2210      tree offset;
2211      tree vcall_offset;
2212 {
2213   const char *result;
2214   
2215   start_mangling ();
2216
2217   write_string ("_Z");
2218   /* The <special-name> for virtual thunks is Tv, for non-virtual
2219      thunks Th.  */
2220   write_char ('T');
2221   if (vcall_offset != 0)
2222     write_char ('v');
2223   else
2224     write_char ('h');
2225
2226   /* For either flavor, write the offset to this.  */
2227   write_integer_cst (offset);
2228   write_char ('_');
2229
2230   /* For a virtual thunk, add the vcall offset.  */
2231   if (vcall_offset)
2232     {
2233       /* Virtual thunk.  Write the vcall offset and base type name.  */
2234       write_integer_cst (vcall_offset);
2235       write_char ('_');
2236     }
2237
2238   /* Scoped name.  */
2239   write_encoding (fn_decl);
2240
2241   result = finish_mangling ();
2242   if (DEBUG_MANGLE)
2243     fprintf (stderr, "mangle_thunk = %s\n\n", result);
2244   return get_identifier (result);
2245 }
2246
2247 /* Return an identifier for the mangled unqualified name for a
2248    conversion operator to TYPE.  This mangling is not specified by the
2249    ABI spec; it is only used internally.  */
2250
2251 tree
2252 mangle_conv_op_name_for_type (type)
2253      tree type;
2254 {
2255   tree identifier;
2256
2257   /* Build the mangling for TYPE.  */
2258   const char *mangled_type = mangle_type_string (type);
2259   /* Allocate a temporary buffer for the complete name.  */
2260   char *op_name = (char *) xmalloc (strlen ("operator ") 
2261                                     + strlen (mangled_type) + 1);
2262   /* Assemble the mangling.  */
2263   strcpy (op_name, "operator ");
2264   strcat (op_name, mangled_type);
2265   /* Find or create an identifier.  */
2266   identifier = get_identifier (op_name);
2267   /* Done with the temporary buffer.  */
2268   free (op_name);
2269   /* Set bits on the identifier so we know later it's a conversion.  */
2270   IDENTIFIER_OPNAME_P (identifier) = 1;
2271   IDENTIFIER_TYPENAME_P (identifier) = 1;
2272   /* Hang TYPE off the identifier so it can be found easily later when
2273      performing conversions.  */
2274   TREE_TYPE (identifier) = type;
2275
2276   return identifier;
2277 }
2278
2279 /* Return an identifier for the name of an initialization guard
2280    variable for indicated VARIABLE.  */
2281
2282 tree
2283 mangle_guard_variable (variable)
2284      tree variable;
2285 {
2286   start_mangling ();
2287   write_string ("_ZGV");
2288   write_name (variable, /*ignore_local_scope=*/0);
2289   return get_identifier (finish_mangling ());
2290 }
2291
2292 \f
2293
2294 /* Foreign language type mangling section.  */
2295
2296 /* How to write the type codes for the integer Java type.  */
2297
2298 static void
2299 write_java_integer_type_codes (type)
2300      tree type;
2301 {
2302   if (type == java_int_type_node)
2303     write_char ('i');
2304   else if (type == java_short_type_node)
2305     write_char ('s');
2306   else if (type == java_byte_type_node)
2307     write_char ('c');
2308   else if (type == java_char_type_node)
2309     write_char ('w');
2310   else if (type == java_long_type_node)
2311     write_char ('x');
2312   else if (type == java_boolean_type_node)
2313     write_char ('b');
2314   else
2315     my_friendly_abort (20001207);
2316 }
2317