OSDN Git Service

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