OSDN Git Service

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