OSDN Git Service

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