* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / cp / mangle.c
bloba105db18bb8da812002990f7ebaa40ce9034a4b5
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Alex Samuel <sameul@codesourcery.com>
5 This file is part of GCC.
7 GCC 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.
12 GCC 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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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.
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.
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:
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
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "tree.h"
55 #include "cp-tree.h"
56 #include "real.h"
57 #include "obstack.h"
58 #include "toplev.h"
59 #include "varray.h"
61 /* Debugging support. */
63 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
64 #ifndef DEBUG_MANGLE
65 #define DEBUG_MANGLE 0
66 #endif
68 /* Macros for tracing the write_* functions. */
69 #if DEBUG_MANGLE
70 # define MANGLE_TRACE(FN, INPUT) \
71 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
72 # define MANGLE_TRACE_TREE(FN, NODE) \
73 fprintf (stderr, " %-24s: %-24s (%p)\n", \
74 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
75 #else
76 # define MANGLE_TRACE(FN, INPUT)
77 # define MANGLE_TRACE_TREE(FN, NODE)
78 #endif
80 /* Nonzero if NODE is a class template-id. We can't rely on
81 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
82 that hard to distinguish A<T> from A, where A<T> is the type as
83 instantiated outside of the template, and A is the type used
84 without parameters inside the template. */
85 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
86 (TYPE_LANG_SPECIFIC (NODE) != NULL \
87 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
88 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
89 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
91 /* Things we only need one of. This module is not reentrant. */
92 static struct globals
94 /* The name in which we're building the mangled name. */
95 struct obstack name_obstack;
97 /* An array of the current substitution candidates, in the order
98 we've seen them. */
99 varray_type substitutions;
101 /* The entity that is being mangled. */
102 tree entity;
104 /* We are mangling an internal symbol. It is important to keep those
105 involving template parmeters distinct by distinguishing their level
106 and, for non-type parms, their type. */
107 bool internal_mangling_p;
109 /* True if the mangling will be different in a future version of the
110 ABI. */
111 bool need_abi_warning;
112 } G;
114 /* Indices into subst_identifiers. These are identifiers used in
115 special substitution rules. */
116 typedef enum
118 SUBID_ALLOCATOR,
119 SUBID_BASIC_STRING,
120 SUBID_CHAR_TRAITS,
121 SUBID_BASIC_ISTREAM,
122 SUBID_BASIC_OSTREAM,
123 SUBID_BASIC_IOSTREAM,
124 SUBID_MAX
126 substitution_identifier_index_t;
128 /* For quick substitution checks, look up these common identifiers
129 once only. */
130 static GTY(()) tree subst_identifiers[SUBID_MAX];
132 /* Single-letter codes for builtin integer types, defined in
133 <builtin-type>. These are indexed by integer_type_kind values. */
134 static const char
135 integer_type_codes[itk_none] =
137 'c', /* itk_char */
138 'a', /* itk_signed_char */
139 'h', /* itk_unsigned_char */
140 's', /* itk_short */
141 't', /* itk_unsigned_short */
142 'i', /* itk_int */
143 'j', /* itk_unsigned_int */
144 'l', /* itk_long */
145 'm', /* itk_unsigned_long */
146 'x', /* itk_long_long */
147 'y' /* itk_unsigned_long_long */
150 static int decl_is_template_id PARAMS ((tree, tree*));
152 /* Functions for handling substitutions. */
154 static inline tree canonicalize_for_substitution PARAMS ((tree));
155 static void add_substitution PARAMS ((tree));
156 static inline int is_std_substitution PARAMS ((tree, substitution_identifier_index_t));
157 static inline int is_std_substitution_char PARAMS ((tree, substitution_identifier_index_t));
158 static int find_substitution PARAMS ((tree));
159 static void mangle_call_offset PARAMS ((tree, tree));
161 /* Functions for emitting mangled representations of things. */
163 static void write_mangled_name PARAMS ((tree));
164 static void write_encoding PARAMS ((tree));
165 static void write_name PARAMS ((tree, int));
166 static void write_unscoped_name PARAMS ((tree));
167 static void write_unscoped_template_name PARAMS ((tree));
168 static void write_nested_name PARAMS ((tree));
169 static void write_prefix PARAMS ((tree));
170 static void write_template_prefix PARAMS ((tree));
171 static void write_unqualified_name PARAMS ((tree));
172 static void write_conversion_operator_name (tree);
173 static void write_source_name PARAMS ((tree));
174 static int hwint_to_ascii PARAMS ((unsigned HOST_WIDE_INT, unsigned int, char *, unsigned));
175 static void write_number PARAMS ((unsigned HOST_WIDE_INT, int,
176 unsigned int));
177 static void write_integer_cst PARAMS ((tree));
178 static void write_identifier PARAMS ((const char *));
179 static void write_special_name_constructor PARAMS ((tree));
180 static void write_special_name_destructor PARAMS ((tree));
181 static void write_type PARAMS ((tree));
182 static int write_CV_qualifiers_for_type PARAMS ((tree));
183 static void write_builtin_type PARAMS ((tree));
184 static void write_function_type PARAMS ((tree));
185 static void write_bare_function_type PARAMS ((tree, int, tree));
186 static void write_method_parms PARAMS ((tree, int, tree));
187 static void write_class_enum_type PARAMS ((tree));
188 static void write_template_args PARAMS ((tree));
189 static void write_expression PARAMS ((tree));
190 static void write_template_arg_literal PARAMS ((tree));
191 static void write_template_arg PARAMS ((tree));
192 static void write_template_template_arg PARAMS ((tree));
193 static void write_array_type PARAMS ((tree));
194 static void write_pointer_to_member_type PARAMS ((tree));
195 static void write_template_param PARAMS ((tree));
196 static void write_template_template_param PARAMS ((tree));
197 static void write_substitution PARAMS ((int));
198 static int discriminator_for_local_entity PARAMS ((tree));
199 static int discriminator_for_string_literal PARAMS ((tree, tree));
200 static void write_discriminator PARAMS ((int));
201 static void write_local_name PARAMS ((tree, tree, tree));
202 static void dump_substitution_candidates PARAMS ((void));
203 static const char *mangle_decl_string PARAMS ((tree));
205 /* Control functions. */
207 static inline void start_mangling (tree);
208 static inline const char *finish_mangling (bool);
209 static tree mangle_special_for_type PARAMS ((tree, const char *));
211 /* Foreign language functions. */
213 static void write_java_integer_type_codes PARAMS ((tree));
215 /* Append a single character to the end of the mangled
216 representation. */
217 #define write_char(CHAR) \
218 obstack_1grow (&G.name_obstack, (CHAR))
220 /* Append a sized buffer to the end of the mangled representation. */
221 #define write_chars(CHAR, LEN) \
222 obstack_grow (&G.name_obstack, (CHAR), (LEN))
224 /* Append a NUL-terminated string to the end of the mangled
225 representation. */
226 #define write_string(STRING) \
227 obstack_grow (&G.name_obstack, (STRING), strlen (STRING))
229 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
230 same purpose (context, which may be a type) and value (template
231 decl). See write_template_prefix for more information on what this
232 is used for. */
233 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
234 (TREE_CODE (NODE1) == TREE_LIST \
235 && TREE_CODE (NODE2) == TREE_LIST \
236 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
237 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
238 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
239 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
241 /* Write out an unsigned quantity in base 10. */
242 #define write_unsigned_number(NUMBER) \
243 write_number ((NUMBER), /*unsigned_p=*/1, 10)
245 /* If DECL is a template instance, return nonzero and, if
246 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
247 Otherwise return zero. */
249 static int
250 decl_is_template_id (decl, template_info)
251 tree decl;
252 tree* template_info;
254 if (TREE_CODE (decl) == TYPE_DECL)
256 /* TYPE_DECLs are handled specially. Look at its type to decide
257 if this is a template instantiation. */
258 tree type = TREE_TYPE (decl);
260 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
262 if (template_info != NULL)
263 /* For a templated TYPE_DECL, the template info is hanging
264 off the type. */
265 *template_info = TYPE_TEMPLATE_INFO (type);
266 return 1;
269 else
271 /* Check if this is a primary template. */
272 if (DECL_LANG_SPECIFIC (decl) != NULL
273 && DECL_USE_TEMPLATE (decl)
274 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
275 && TREE_CODE (decl) != TEMPLATE_DECL)
277 if (template_info != NULL)
278 /* For most templated decls, the template info is hanging
279 off the decl. */
280 *template_info = DECL_TEMPLATE_INFO (decl);
281 return 1;
285 /* It's not a template id. */
286 return 0;
289 /* Produce debugging output of current substitution candidates. */
291 static void
292 dump_substitution_candidates ()
294 unsigned i;
296 fprintf (stderr, " ++ substitutions ");
297 for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
299 tree el = VARRAY_TREE (G.substitutions, i);
300 const char *name = "???";
302 if (i > 0)
303 fprintf (stderr, " ");
304 if (DECL_P (el))
305 name = IDENTIFIER_POINTER (DECL_NAME (el));
306 else if (TREE_CODE (el) == TREE_LIST)
307 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
308 else if (TYPE_NAME (el))
309 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
310 fprintf (stderr, " S%d_ = ", i - 1);
311 if (TYPE_P (el) &&
312 (CP_TYPE_RESTRICT_P (el)
313 || CP_TYPE_VOLATILE_P (el)
314 || CP_TYPE_CONST_P (el)))
315 fprintf (stderr, "CV-");
316 fprintf (stderr, "%s (%s at %p)\n",
317 name, tree_code_name[TREE_CODE (el)], (void *) el);
321 /* Both decls and types can be substitution candidates, but sometimes
322 they refer to the same thing. For instance, a TYPE_DECL and
323 RECORD_TYPE for the same class refer to the same thing, and should
324 be treated accordinginly in substitutions. This function returns a
325 canonicalized tree node representing NODE that is used when adding
326 and substitution candidates and finding matches. */
328 static inline tree
329 canonicalize_for_substitution (node)
330 tree node;
332 /* For a TYPE_DECL, use the type instead. */
333 if (TREE_CODE (node) == TYPE_DECL)
334 node = TREE_TYPE (node);
335 if (TYPE_P (node))
336 node = canonical_type_variant (node);
338 return node;
341 /* Add NODE as a substitution candidate. NODE must not already be on
342 the list of candidates. */
344 static void
345 add_substitution (node)
346 tree node;
348 tree c;
350 if (DEBUG_MANGLE)
351 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
352 tree_code_name[TREE_CODE (node)], (void *) node);
354 /* Get the canonicalized substitution candidate for NODE. */
355 c = canonicalize_for_substitution (node);
356 if (DEBUG_MANGLE && c != node)
357 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
358 tree_code_name[TREE_CODE (node)], (void *) node);
359 node = c;
361 #if ENABLE_CHECKING
362 /* Make sure NODE isn't already a candidate. */
364 int i;
365 for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
367 tree candidate = VARRAY_TREE (G.substitutions, i);
368 if ((DECL_P (node)
369 && node == candidate)
370 || (TYPE_P (node)
371 && TYPE_P (candidate)
372 && same_type_p (node, candidate)))
373 abort ();
376 #endif /* ENABLE_CHECKING */
378 /* Put the decl onto the varray of substitution candidates. */
379 VARRAY_PUSH_TREE (G.substitutions, node);
381 if (DEBUG_MANGLE)
382 dump_substitution_candidates ();
385 /* Helper function for find_substitution. Returns nonzero if NODE,
386 which may be a decl or a CLASS_TYPE, is a template-id with template
387 name of substitution_index[INDEX] in the ::std namespace. */
389 static inline int
390 is_std_substitution (node, index)
391 tree node;
392 substitution_identifier_index_t index;
394 tree type = NULL;
395 tree decl = NULL;
397 if (DECL_P (node))
399 type = TREE_TYPE (node);
400 decl = node;
402 else if (CLASS_TYPE_P (node))
404 type = node;
405 decl = TYPE_NAME (node);
407 else
408 /* These are not the droids you're looking for. */
409 return 0;
411 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
412 && TYPE_LANG_SPECIFIC (type)
413 && TYPE_TEMPLATE_INFO (type)
414 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
415 == subst_identifiers[index]));
418 /* Helper function for find_substitution. Returns nonzero if NODE,
419 which may be a decl or a CLASS_TYPE, is the template-id
420 ::std::identifier<char>, where identifier is
421 substitution_index[INDEX]. */
423 static inline int
424 is_std_substitution_char (node, index)
425 tree node;
426 substitution_identifier_index_t index;
428 tree args;
429 /* Check NODE's name is ::std::identifier. */
430 if (!is_std_substitution (node, index))
431 return 0;
432 /* Figure out its template args. */
433 if (DECL_P (node))
434 args = DECL_TI_ARGS (node);
435 else if (CLASS_TYPE_P (node))
436 args = CLASSTYPE_TI_ARGS (node);
437 else
438 /* Oops, not a template. */
439 return 0;
440 /* NODE's template arg list should be <char>. */
441 return
442 TREE_VEC_LENGTH (args) == 1
443 && TREE_VEC_ELT (args, 0) == char_type_node;
446 /* Check whether a substitution should be used to represent NODE in
447 the mangling.
449 First, check standard special-case substitutions.
451 <substitution> ::= St
452 # ::std
454 ::= Sa
455 # ::std::allocator
457 ::= Sb
458 # ::std::basic_string
460 ::= Ss
461 # ::std::basic_string<char,
462 ::std::char_traits<char>,
463 ::std::allocator<char> >
465 ::= Si
466 # ::std::basic_istream<char, ::std::char_traits<char> >
468 ::= So
469 # ::std::basic_ostream<char, ::std::char_traits<char> >
471 ::= Sd
472 # ::std::basic_iostream<char, ::std::char_traits<char> >
474 Then examine the stack of currently available substitution
475 candidates for entities appearing earlier in the same mangling
477 If a substitution is found, write its mangled representation and
478 return nonzero. If none is found, just return zero. */
480 static int
481 find_substitution (node)
482 tree node;
484 int i;
485 int size = VARRAY_ACTIVE_SIZE (G.substitutions);
486 tree decl;
487 tree type;
489 if (DEBUG_MANGLE)
490 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
491 tree_code_name[TREE_CODE (node)], (void *) node);
493 /* Obtain the canonicalized substitution representation for NODE.
494 This is what we'll compare against. */
495 node = canonicalize_for_substitution (node);
497 /* Check for builtin substitutions. */
499 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
500 type = TYPE_P (node) ? node : TREE_TYPE (node);
502 /* Check for std::allocator. */
503 if (decl
504 && is_std_substitution (decl, SUBID_ALLOCATOR)
505 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
507 write_string ("Sa");
508 return 1;
511 /* Check for std::basic_string. */
512 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
514 if (TYPE_P (node))
516 /* If this is a type (i.e. a fully-qualified template-id),
517 check for
518 std::basic_string <char,
519 std::char_traits<char>,
520 std::allocator<char> > . */
521 if (cp_type_quals (type) == TYPE_UNQUALIFIED
522 && CLASSTYPE_USE_TEMPLATE (type))
524 tree args = CLASSTYPE_TI_ARGS (type);
525 if (TREE_VEC_LENGTH (args) == 3
526 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
527 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
528 SUBID_CHAR_TRAITS)
529 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
530 SUBID_ALLOCATOR))
532 write_string ("Ss");
533 return 1;
537 else
538 /* Substitute for the template name only if this isn't a type. */
540 write_string ("Sb");
541 return 1;
545 /* Check for basic_{i,o,io}stream. */
546 if (TYPE_P (node)
547 && cp_type_quals (type) == TYPE_UNQUALIFIED
548 && CLASS_TYPE_P (type)
549 && CLASSTYPE_USE_TEMPLATE (type)
550 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
552 /* First, check for the template
553 args <char, std::char_traits<char> > . */
554 tree args = CLASSTYPE_TI_ARGS (type);
555 if (TREE_VEC_LENGTH (args) == 2
556 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
557 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
558 SUBID_CHAR_TRAITS))
560 /* Got them. Is this basic_istream? */
561 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
562 if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
564 write_string ("Si");
565 return 1;
567 /* Or basic_ostream? */
568 else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
570 write_string ("So");
571 return 1;
573 /* Or basic_iostream? */
574 else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
576 write_string ("Sd");
577 return 1;
582 /* Check for namespace std. */
583 if (decl && DECL_NAMESPACE_STD_P (decl))
585 write_string ("St");
586 return 1;
589 /* Now check the list of available substitutions for this mangling
590 operation. */
591 for (i = 0; i < size; ++i)
593 tree candidate = VARRAY_TREE (G.substitutions, i);
594 /* NODE is a matched to a candidate if it's the same decl node or
595 if it's the same type. */
596 if (decl == candidate
597 || (TYPE_P (candidate) && type && TYPE_P (type)
598 && same_type_p (type, candidate))
599 || NESTED_TEMPLATE_MATCH (node, candidate))
601 write_substitution (i);
602 return 1;
606 /* No substitution found. */
607 return 0;
611 /* <mangled-name> ::= _Z <encoding> */
613 static inline void
614 write_mangled_name (decl)
615 tree decl;
617 MANGLE_TRACE_TREE ("mangled-name", decl);
619 if (DECL_LANG_SPECIFIC (decl)
620 && DECL_EXTERN_C_FUNCTION_P (decl)
621 && ! DECL_OVERLOADED_OPERATOR_P (decl))
622 /* The standard notes:
623 "The <encoding> of an extern "C" function is treated like
624 global-scope data, i.e. as its <source-name> without a type."
625 We cannot write overloaded operators that way though,
626 because it contains characters invalid in assembler. */
627 write_source_name (DECL_NAME (decl));
628 else
629 /* C++ name; needs to be mangled. */
631 write_string ("_Z");
632 write_encoding (decl);
636 /* <encoding> ::= <function name> <bare-function-type>
637 ::= <data name> */
639 static void
640 write_encoding (decl)
641 tree decl;
643 MANGLE_TRACE_TREE ("encoding", decl);
645 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
647 /* For overloaded operators write just the mangled name
648 without arguments. */
649 if (DECL_OVERLOADED_OPERATOR_P (decl))
650 write_name (decl, /*ignore_local_scope=*/0);
651 else
652 write_source_name (DECL_NAME (decl));
653 return;
656 write_name (decl, /*ignore_local_scope=*/0);
657 if (TREE_CODE (decl) == FUNCTION_DECL)
659 tree fn_type;
661 if (decl_is_template_id (decl, NULL))
662 fn_type = get_mostly_instantiated_function_type (decl);
663 else
664 fn_type = TREE_TYPE (decl);
666 write_bare_function_type (fn_type,
667 (!DECL_CONSTRUCTOR_P (decl)
668 && !DECL_DESTRUCTOR_P (decl)
669 && !DECL_CONV_FN_P (decl)
670 && decl_is_template_id (decl, NULL)),
671 decl);
675 /* <name> ::= <unscoped-name>
676 ::= <unscoped-template-name> <template-args>
677 ::= <nested-name>
678 ::= <local-name>
680 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
681 called from <local-name>, which mangles the enclosing scope
682 elsewhere and then uses this function to mangle just the part
683 underneath the function scope. So don't use the <local-name>
684 production, to avoid an infinite recursion. */
686 static void
687 write_name (decl, ignore_local_scope)
688 tree decl;
689 int ignore_local_scope;
691 tree context;
693 MANGLE_TRACE_TREE ("name", decl);
695 if (TREE_CODE (decl) == TYPE_DECL)
697 /* In case this is a typedef, fish out the corresponding
698 TYPE_DECL for the main variant. */
699 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
700 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
702 else
703 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
705 /* A decl in :: or ::std scope is treated specially. The former is
706 mangled using <unscoped-name> or <unscoped-template-name>, the
707 latter with a special substitution. Also, a name that is
708 directly in a local function scope is also mangled with
709 <unscoped-name> rather than a full <nested-name>. */
710 if (context == NULL
711 || context == global_namespace
712 || DECL_NAMESPACE_STD_P (context)
713 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
715 tree template_info;
716 /* Is this a template instance? */
717 if (decl_is_template_id (decl, &template_info))
719 /* Yes: use <unscoped-template-name>. */
720 write_unscoped_template_name (TI_TEMPLATE (template_info));
721 write_template_args (TI_ARGS (template_info));
723 else
724 /* Everything else gets an <unqualified-name>. */
725 write_unscoped_name (decl);
727 else
729 /* Handle local names, unless we asked not to (that is, invoked
730 under <local-name>, to handle only the part of the name under
731 the local scope). */
732 if (!ignore_local_scope)
734 /* Scan up the list of scope context, looking for a
735 function. If we find one, this entity is in local
736 function scope. local_entity tracks context one scope
737 level down, so it will contain the element that's
738 directly in that function's scope, either decl or one of
739 its enclosing scopes. */
740 tree local_entity = decl;
741 while (context != NULL && context != global_namespace)
743 /* Make sure we're always dealing with decls. */
744 if (context != NULL && TYPE_P (context))
745 context = TYPE_NAME (context);
746 /* Is this a function? */
747 if (TREE_CODE (context) == FUNCTION_DECL)
749 /* Yes, we have local scope. Use the <local-name>
750 production for the innermost function scope. */
751 write_local_name (context, local_entity, decl);
752 return;
754 /* Up one scope level. */
755 local_entity = context;
756 context = CP_DECL_CONTEXT (context);
759 /* No local scope found? Fall through to <nested-name>. */
762 /* Other decls get a <nested-name> to encode their scope. */
763 write_nested_name (decl);
767 /* <unscoped-name> ::= <unqualified-name>
768 ::= St <unqualified-name> # ::std:: */
770 static void
771 write_unscoped_name (decl)
772 tree decl;
774 tree context = CP_DECL_CONTEXT (decl);
776 MANGLE_TRACE_TREE ("unscoped-name", decl);
778 /* Is DECL in ::std? */
779 if (DECL_NAMESPACE_STD_P (context))
781 write_string ("St");
782 write_unqualified_name (decl);
784 /* If not, it should be either in the global namespace, or directly
785 in a local function scope. */
786 else if (context == global_namespace
787 || context == NULL
788 || TREE_CODE (context) == FUNCTION_DECL)
789 write_unqualified_name (decl);
790 else
791 abort ();
794 /* <unscoped-template-name> ::= <unscoped-name>
795 ::= <substitution> */
797 static void
798 write_unscoped_template_name (decl)
799 tree decl;
801 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
803 if (find_substitution (decl))
804 return;
805 write_unscoped_name (decl);
806 add_substitution (decl);
809 /* Write the nested name, including CV-qualifiers, of DECL.
811 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
812 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
814 <CV-qualifiers> ::= [r] [V] [K] */
816 static void
817 write_nested_name (decl)
818 tree decl;
820 tree template_info;
822 MANGLE_TRACE_TREE ("nested-name", decl);
824 write_char ('N');
826 /* Write CV-qualifiers, if this is a member function. */
827 if (TREE_CODE (decl) == FUNCTION_DECL
828 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
830 if (DECL_VOLATILE_MEMFUNC_P (decl))
831 write_char ('V');
832 if (DECL_CONST_MEMFUNC_P (decl))
833 write_char ('K');
836 /* Is this a template instance? */
837 if (decl_is_template_id (decl, &template_info))
839 /* Yes, use <template-prefix>. */
840 write_template_prefix (decl);
841 write_template_args (TI_ARGS (template_info));
843 else
845 /* No, just use <prefix> */
846 write_prefix (DECL_CONTEXT (decl));
847 write_unqualified_name (decl);
849 write_char ('E');
852 /* <prefix> ::= <prefix> <unqualified-name>
853 ::= <template-param>
854 ::= <template-prefix> <template-args>
855 ::= # empty
856 ::= <substitution> */
858 static void
859 write_prefix (node)
860 tree node;
862 tree decl;
863 /* Non-NULL if NODE represents a template-id. */
864 tree template_info = NULL;
866 MANGLE_TRACE_TREE ("prefix", node);
868 if (node == NULL
869 || node == global_namespace)
870 return;
872 if (find_substitution (node))
873 return;
875 if (DECL_P (node))
877 /* If this is a function decl, that means we've hit function
878 scope, so this prefix must be for a local name. In this
879 case, we're under the <local-name> production, which encodes
880 the enclosing function scope elsewhere. So don't continue
881 here. */
882 if (TREE_CODE (node) == FUNCTION_DECL)
883 return;
885 decl = node;
886 decl_is_template_id (decl, &template_info);
888 else
890 /* Node is a type. */
891 decl = TYPE_NAME (node);
892 if (CLASSTYPE_TEMPLATE_ID_P (node))
893 template_info = TYPE_TEMPLATE_INFO (node);
896 /* In G++ 3.2, the name of the template parameter was used. */
897 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
898 && !abi_version_at_least (2))
899 G.need_abi_warning = true;
901 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
902 && abi_version_at_least (2))
903 write_template_param (node);
904 else if (template_info != NULL)
905 /* Templated. */
907 write_template_prefix (decl);
908 write_template_args (TI_ARGS (template_info));
910 else
911 /* Not templated. */
913 write_prefix (CP_DECL_CONTEXT (decl));
914 write_unqualified_name (decl);
917 add_substitution (node);
920 /* <template-prefix> ::= <prefix> <template component>
921 ::= <template-param>
922 ::= <substitution> */
924 static void
925 write_template_prefix (node)
926 tree node;
928 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
929 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
930 tree context = CP_DECL_CONTEXT (decl);
931 tree template_info;
932 tree template;
933 tree substitution;
935 MANGLE_TRACE_TREE ("template-prefix", node);
937 /* Find the template decl. */
938 if (decl_is_template_id (decl, &template_info))
939 template = TI_TEMPLATE (template_info);
940 else if (CLASSTYPE_TEMPLATE_ID_P (type))
941 template = TYPE_TI_TEMPLATE (type);
942 else
943 /* Oops, not a template. */
944 abort ();
946 /* For a member template, though, the template name for the
947 innermost name must have all the outer template levels
948 instantiated. For instance, consider
950 template<typename T> struct Outer {
951 template<typename U> struct Inner {};
954 The template name for `Inner' in `Outer<int>::Inner<float>' is
955 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
956 levels separately, so there's no TEMPLATE_DECL available for this
957 (there's only `Outer<T>::Inner<U>').
959 In order to get the substitutions right, we create a special
960 TREE_LIST to represent the substitution candidate for a nested
961 template. The TREE_PURPOSE is the template's context, fully
962 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
963 template.
965 So, for the example above, `Outer<int>::Inner' is represented as a
966 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
967 and whose value is `Outer<T>::Inner<U>'. */
968 if (TYPE_P (context))
969 substitution = build_tree_list (context, template);
970 else
971 substitution = template;
973 if (find_substitution (substitution))
974 return;
976 /* In G++ 3.2, the name of the template template parameter was used. */
977 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
978 && !abi_version_at_least (2))
979 G.need_abi_warning = true;
981 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
982 && abi_version_at_least (2))
983 write_template_param (TREE_TYPE (template));
984 else
986 write_prefix (context);
987 write_unqualified_name (decl);
990 add_substitution (substitution);
993 /* We don't need to handle thunks, vtables, or VTTs here. Those are
994 mangled through special entry points.
996 <unqualified-name> ::= <operator-name>
997 ::= <special-name>
998 ::= <source-name> */
1000 static void
1001 write_unqualified_name (decl)
1002 tree decl;
1004 MANGLE_TRACE_TREE ("unqualified-name", decl);
1006 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
1007 write_special_name_constructor (decl);
1008 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
1009 write_special_name_destructor (decl);
1010 else if (DECL_CONV_FN_P (decl))
1012 /* Conversion operator. Handle it right here.
1013 <operator> ::= cv <type> */
1014 tree type;
1015 if (decl_is_template_id (decl, NULL))
1017 tree fn_type = get_mostly_instantiated_function_type (decl);
1018 type = TREE_TYPE (fn_type);
1020 else
1021 type = TREE_TYPE (DECL_NAME (decl));
1022 write_conversion_operator_name (type);
1024 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1026 operator_name_info_t *oni;
1027 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1028 oni = assignment_operator_name_info;
1029 else
1030 oni = operator_name_info;
1032 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1034 else
1035 write_source_name (DECL_NAME (decl));
1038 /* Write the unqualified-name for a conversion operator to TYPE. */
1040 static void
1041 write_conversion_operator_name (tree type)
1043 write_string ("cv");
1044 write_type (type);
1047 /* Non-termial <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1049 <source-name> ::= </length/ number> <identifier> */
1051 static void
1052 write_source_name (identifier)
1053 tree identifier;
1055 MANGLE_TRACE_TREE ("source-name", identifier);
1057 /* Never write the whole template-id name including the template
1058 arguments; we only want the template name. */
1059 if (IDENTIFIER_TEMPLATE (identifier))
1060 identifier = IDENTIFIER_TEMPLATE (identifier);
1062 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1063 write_identifier (IDENTIFIER_POINTER (identifier));
1066 /* Convert NUMBER to ascii using base BASE and generating at least
1067 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1068 into which to store the characters. Returns the number of
1069 characters generated (these will be layed out in advance of where
1070 BUFFER points). */
1072 static int
1073 hwint_to_ascii (number, base, buffer, min_digits)
1074 unsigned HOST_WIDE_INT number;
1075 unsigned int base;
1076 char *buffer;
1077 unsigned min_digits;
1079 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1080 unsigned digits = 0;
1082 while (number)
1084 unsigned HOST_WIDE_INT d = number / base;
1086 *--buffer = base_digits[number - d * base];
1087 digits++;
1088 number = d;
1090 while (digits < min_digits)
1092 *--buffer = base_digits[0];
1093 digits++;
1095 return digits;
1098 /* Non-terminal <number>.
1100 <number> ::= [n] </decimal integer/> */
1102 static void
1103 write_number (number, unsigned_p, base)
1104 unsigned HOST_WIDE_INT number;
1105 int unsigned_p;
1106 unsigned int base;
1108 char buffer[sizeof (HOST_WIDE_INT) * 8];
1109 unsigned count = 0;
1111 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1113 write_char ('n');
1114 number = -((HOST_WIDE_INT) number);
1116 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1117 write_chars (buffer + sizeof (buffer) - count, count);
1120 /* Write out an integral CST in decimal. Most numbers are small, and
1121 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1122 bigger than that, which we must deal with. */
1124 static inline void
1125 write_integer_cst (cst)
1126 tree cst;
1128 int sign = tree_int_cst_sgn (cst);
1130 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1132 /* A bignum. We do this in chunks, each of which fits in a
1133 HOST_WIDE_INT. */
1134 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1135 unsigned HOST_WIDE_INT chunk;
1136 unsigned chunk_digits;
1137 char *ptr = buffer + sizeof (buffer);
1138 unsigned count = 0;
1139 tree n, base, type;
1140 int done;
1142 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1143 representable. */
1144 chunk = 1000000000;
1145 chunk_digits = 9;
1147 if (sizeof (HOST_WIDE_INT) >= 8)
1149 /* It is at least 64 bits, so 10^18 is representable. */
1150 chunk_digits = 18;
1151 chunk *= chunk;
1154 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1155 base = build_int_2 (chunk, 0);
1156 n = build_int_2 (TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1157 TREE_TYPE (n) = TREE_TYPE (base) = type;
1159 if (sign < 0)
1161 write_char ('n');
1162 n = fold (build1 (NEGATE_EXPR, type, n));
1166 tree d = fold (build (FLOOR_DIV_EXPR, type, n, base));
1167 tree tmp = fold (build (MULT_EXPR, type, d, base));
1168 unsigned c;
1170 done = integer_zerop (d);
1171 tmp = fold (build (MINUS_EXPR, type, n, tmp));
1172 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1173 done ? 1 : chunk_digits);
1174 ptr -= c;
1175 count += c;
1176 n = d;
1178 while (!done);
1179 write_chars (ptr, count);
1181 else
1183 /* A small num. */
1184 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1186 if (sign < 0)
1188 write_char ('n');
1189 low = -low;
1191 write_unsigned_number (low);
1195 /* Non-terminal <identifier>.
1197 <identifier> ::= </unqualified source code identifier> */
1199 static void
1200 write_identifier (identifier)
1201 const char *identifier;
1203 MANGLE_TRACE ("identifier", identifier);
1204 write_string (identifier);
1207 /* Handle constructor productions of non-terminal <special-name>.
1208 CTOR is a constructor FUNCTION_DECL.
1210 <special-name> ::= C1 # complete object constructor
1211 ::= C2 # base object constructor
1212 ::= C3 # complete object allocating constructor
1214 Currently, allocating constructors are never used.
1216 We also need to provide mangled names for the maybe-in-charge
1217 constructor, so we treat it here too. mangle_decl_string will
1218 append *INTERNAL* to that, to make sure we never emit it. */
1220 static void
1221 write_special_name_constructor (ctor)
1222 tree ctor;
1224 if (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1225 /* Even though we don't ever emit a definition of the
1226 old-style destructor, we still have to consider entities
1227 (like static variables) nested inside it. */
1228 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1229 write_string ("C1");
1230 else if (DECL_BASE_CONSTRUCTOR_P (ctor))
1231 write_string ("C2");
1232 else
1233 abort ();
1236 /* Handle destructor productions of non-terminal <special-name>.
1237 DTOR is a destructor FUNCTION_DECL.
1239 <special-name> ::= D0 # deleting (in-charge) destructor
1240 ::= D1 # complete object (in-charge) destructor
1241 ::= D2 # base object (not-in-charge) destructor
1243 We also need to provide mangled names for the maybe-incharge
1244 destructor, so we treat it here too. mangle_decl_string will
1245 append *INTERNAL* to that, to make sure we never emit it. */
1247 static void
1248 write_special_name_destructor (dtor)
1249 tree dtor;
1251 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1252 write_string ("D0");
1253 else if (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1254 /* Even though we don't ever emit a definition of the
1255 old-style destructor, we still have to consider entities
1256 (like static variables) nested inside it. */
1257 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1258 write_string ("D1");
1259 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1260 write_string ("D2");
1261 else
1262 abort ();
1265 /* Return the discriminator for ENTITY appearing inside
1266 FUNCTION. The discriminator is the lexical ordinal of VAR among
1267 entities with the same name in the same FUNCTION. */
1269 static int
1270 discriminator_for_local_entity (entity)
1271 tree entity;
1273 tree *type;
1274 int discriminator;
1276 /* Assume this is the only local entity with this name. */
1277 discriminator = 0;
1279 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1280 discriminator = DECL_DISCRIMINATOR (entity);
1281 else if (TREE_CODE (entity) == TYPE_DECL)
1283 /* Scan the list of local classes. */
1284 entity = TREE_TYPE (entity);
1285 for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
1286 if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1287 && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1288 ++discriminator;
1291 return discriminator;
1294 /* Return the discriminator for STRING, a string literal used inside
1295 FUNCTION. The disciminator is the lexical ordinal of STRING among
1296 string literals used in FUNCTION. */
1298 static int
1299 discriminator_for_string_literal (function, string)
1300 tree function ATTRIBUTE_UNUSED;
1301 tree string ATTRIBUTE_UNUSED;
1303 /* For now, we don't discriminate amongst string literals. */
1304 return 0;
1307 /* <discriminator> := _ <number>
1309 The discriminator is used only for the second and later occurrences
1310 of the same name within a single function. In this case <number> is
1311 n - 2, if this is the nth occurrence, in lexical order. */
1313 static void
1314 write_discriminator (discriminator)
1315 int discriminator;
1317 /* If discriminator is zero, don't write anything. Otherwise... */
1318 if (discriminator > 0)
1320 write_char ('_');
1321 write_unsigned_number (discriminator - 1);
1325 /* Mangle the name of a function-scope entity. FUNCTION is the
1326 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1327 the entity itself. LOCAL_ENTITY is the entity that's directly
1328 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1329 of ENTITY.
1331 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1332 := Z <function encoding> E s [<discriminator>] */
1334 static void
1335 write_local_name (function, local_entity, entity)
1336 tree function;
1337 tree local_entity;
1338 tree entity;
1340 MANGLE_TRACE_TREE ("local-name", entity);
1342 write_char ('Z');
1343 write_encoding (function);
1344 write_char ('E');
1345 if (TREE_CODE (entity) == STRING_CST)
1347 write_char ('s');
1348 write_discriminator (discriminator_for_string_literal (function,
1349 entity));
1351 else
1353 /* Now the <entity name>. Let write_name know its being called
1354 from <local-name>, so it doesn't try to process the enclosing
1355 function scope again. */
1356 write_name (entity, /*ignore_local_scope=*/1);
1357 write_discriminator (discriminator_for_local_entity (local_entity));
1361 /* Non-terminals <type> and <CV-qualifier>.
1363 <type> ::= <builtin-type>
1364 ::= <function-type>
1365 ::= <class-enum-type>
1366 ::= <array-type>
1367 ::= <pointer-to-member-type>
1368 ::= <template-param>
1369 ::= <substitution>
1370 ::= <CV-qualifier>
1371 ::= P <type> # pointer-to
1372 ::= R <type> # reference-to
1373 ::= C <type> # complex pair (C 2000)
1374 ::= G <type> # imaginary (C 2000) [not supported]
1375 ::= U <source-name> <type> # vendor extended type qualifier
1377 TYPE is a type node. */
1379 static void
1380 write_type (type)
1381 tree type;
1383 /* This gets set to nonzero if TYPE turns out to be a (possibly
1384 CV-qualified) builtin type. */
1385 int is_builtin_type = 0;
1387 MANGLE_TRACE_TREE ("type", type);
1389 if (type == error_mark_node)
1390 return;
1392 if (find_substitution (type))
1393 return;
1395 if (write_CV_qualifiers_for_type (type) > 0)
1396 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1397 mangle the unqualified type. The recursive call is needed here
1398 since both the qualified and uqualified types are substitution
1399 candidates. */
1400 write_type (TYPE_MAIN_VARIANT (type));
1401 else if (TREE_CODE (type) == ARRAY_TYPE)
1402 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1403 so that the cv-qualification of the element type is available
1404 in write_array_type. */
1405 write_array_type (type);
1406 else
1408 /* See through any typedefs. */
1409 type = TYPE_MAIN_VARIANT (type);
1411 switch (TREE_CODE (type))
1413 case VOID_TYPE:
1414 case BOOLEAN_TYPE:
1415 case INTEGER_TYPE: /* Includes wchar_t. */
1416 case REAL_TYPE:
1417 /* If this is a typedef, TYPE may not be one of
1418 the standard builtin type nodes, but an alias of one. Use
1419 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1420 write_builtin_type (TYPE_MAIN_VARIANT (type));
1421 ++is_builtin_type;
1422 break;
1424 case COMPLEX_TYPE:
1425 write_char ('C');
1426 write_type (TREE_TYPE (type));
1427 break;
1429 case FUNCTION_TYPE:
1430 case METHOD_TYPE:
1431 write_function_type (type);
1432 break;
1434 case UNION_TYPE:
1435 case RECORD_TYPE:
1436 case ENUMERAL_TYPE:
1437 /* A pointer-to-member function is represented as a special
1438 RECORD_TYPE, so check for this first. */
1439 if (TYPE_PTRMEMFUNC_P (type))
1440 write_pointer_to_member_type (type);
1441 else
1442 write_class_enum_type (type);
1443 break;
1445 case TYPENAME_TYPE:
1446 case UNBOUND_CLASS_TEMPLATE:
1447 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1448 ordinary nested names. */
1449 write_nested_name (TYPE_STUB_DECL (type));
1450 break;
1452 case POINTER_TYPE:
1453 /* A pointer-to-member variable is represented by a POINTER_TYPE
1454 to an OFFSET_TYPE, so check for this first. */
1455 if (TYPE_PTRMEM_P (type))
1456 write_pointer_to_member_type (type);
1457 else
1459 write_char ('P');
1460 write_type (TREE_TYPE (type));
1462 break;
1464 case REFERENCE_TYPE:
1465 write_char ('R');
1466 write_type (TREE_TYPE (type));
1467 break;
1469 case TEMPLATE_TYPE_PARM:
1470 case TEMPLATE_PARM_INDEX:
1471 write_template_param (type);
1472 break;
1474 case TEMPLATE_TEMPLATE_PARM:
1475 write_template_template_param (type);
1476 break;
1478 case BOUND_TEMPLATE_TEMPLATE_PARM:
1479 write_template_template_param (type);
1480 write_template_args
1481 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1482 break;
1484 case OFFSET_TYPE:
1485 write_pointer_to_member_type (build_pointer_type (type));
1486 break;
1488 case VECTOR_TYPE:
1489 write_string ("U8__vector");
1490 write_type (TREE_TYPE (type));
1491 break;
1493 default:
1494 abort ();
1498 /* Types other than builtin types are substitution candidates. */
1499 if (!is_builtin_type)
1500 add_substitution (type);
1503 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1504 CV-qualifiers written for TYPE.
1506 <CV-qualifiers> ::= [r] [V] [K] */
1508 static int
1509 write_CV_qualifiers_for_type (type)
1510 tree type;
1512 int num_qualifiers = 0;
1514 /* The order is specified by:
1516 "In cases where multiple order-insensitive qualifiers are
1517 present, they should be ordered 'K' (closest to the base type),
1518 'V', 'r', and 'U' (farthest from the base type) ..."
1520 Note that we do not use cp_type_quals below; given "const
1521 int[3]", the "const" is emitted with the "int", not with the
1522 array. */
1524 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1526 write_char ('r');
1527 ++num_qualifiers;
1529 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1531 write_char ('V');
1532 ++num_qualifiers;
1534 if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1536 write_char ('K');
1537 ++num_qualifiers;
1540 return num_qualifiers;
1543 /* Non-terminal <builtin-type>.
1545 <builtin-type> ::= v # void
1546 ::= b # bool
1547 ::= w # wchar_t
1548 ::= c # char
1549 ::= a # signed char
1550 ::= h # unsigned char
1551 ::= s # short
1552 ::= t # unsigned short
1553 ::= i # int
1554 ::= j # unsigned int
1555 ::= l # long
1556 ::= m # unsigned long
1557 ::= x # long long, __int64
1558 ::= y # unsigned long long, __int64
1559 ::= n # __int128
1560 ::= o # unsigned __int128
1561 ::= f # float
1562 ::= d # double
1563 ::= e # long double, __float80
1564 ::= g # __float128 [not supported]
1565 ::= u <source-name> # vendor extended type */
1567 static void
1568 write_builtin_type (type)
1569 tree type;
1571 switch (TREE_CODE (type))
1573 case VOID_TYPE:
1574 write_char ('v');
1575 break;
1577 case BOOLEAN_TYPE:
1578 write_char ('b');
1579 break;
1581 case INTEGER_TYPE:
1582 /* If this is size_t, get the underlying int type. */
1583 if (TYPE_IS_SIZETYPE (type))
1584 type = TYPE_DOMAIN (type);
1586 /* TYPE may still be wchar_t, since that isn't in
1587 integer_type_nodes. */
1588 if (type == wchar_type_node)
1589 write_char ('w');
1590 else if (TYPE_FOR_JAVA (type))
1591 write_java_integer_type_codes (type);
1592 else
1594 size_t itk;
1595 /* Assume TYPE is one of the shared integer type nodes. Find
1596 it in the array of these nodes. */
1597 iagain:
1598 for (itk = 0; itk < itk_none; ++itk)
1599 if (type == integer_types[itk])
1601 /* Print the corresponding single-letter code. */
1602 write_char (integer_type_codes[itk]);
1603 break;
1606 if (itk == itk_none)
1608 tree t = c_common_type_for_mode (TYPE_MODE (type),
1609 TREE_UNSIGNED (type));
1610 if (type == t)
1612 if (TYPE_PRECISION (type) == 128)
1613 write_char (TREE_UNSIGNED (type) ? 'o' : 'n');
1614 else
1615 /* Couldn't find this type. */
1616 abort ();
1618 else
1620 type = t;
1621 goto iagain;
1625 break;
1627 case REAL_TYPE:
1628 if (type == float_type_node
1629 || type == java_float_type_node)
1630 write_char ('f');
1631 else if (type == double_type_node
1632 || type == java_double_type_node)
1633 write_char ('d');
1634 else if (type == long_double_type_node)
1635 write_char ('e');
1636 else
1637 abort ();
1638 break;
1640 default:
1641 abort ();
1645 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1646 METHOD_TYPE. The return type is mangled before the parameter
1647 types.
1649 <function-type> ::= F [Y] <bare-function-type> E */
1651 static void
1652 write_function_type (type)
1653 tree type;
1655 MANGLE_TRACE_TREE ("function-type", type);
1657 /* For a pointer to member function, the function type may have
1658 cv-qualifiers, indicating the quals for the artificial 'this'
1659 parameter. */
1660 if (TREE_CODE (type) == METHOD_TYPE)
1662 /* The first parameter must be a POINTER_TYPE pointing to the
1663 `this' parameter. */
1664 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1665 write_CV_qualifiers_for_type (this_type);
1668 write_char ('F');
1669 /* We don't track whether or not a type is `extern "C"'. Note that
1670 you can have an `extern "C"' function that does not have
1671 `extern "C"' type, and vice versa:
1673 extern "C" typedef void function_t();
1674 function_t f; // f has C++ linkage, but its type is
1675 // `extern "C"'
1677 typedef void function_t();
1678 extern "C" function_t f; // Vice versa.
1680 See [dcl.link]. */
1681 write_bare_function_type (type, /*include_return_type_p=*/1,
1682 /*decl=*/NULL);
1683 write_char ('E');
1686 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1687 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
1688 is mangled before the parameter types. If non-NULL, DECL is
1689 FUNCTION_DECL for the function whose type is being emitted.
1691 <bare-function-type> ::= </signature/ type>+ */
1693 static void
1694 write_bare_function_type (type, include_return_type_p, decl)
1695 tree type;
1696 int include_return_type_p;
1697 tree decl;
1699 MANGLE_TRACE_TREE ("bare-function-type", type);
1701 /* Mangle the return type, if requested. */
1702 if (include_return_type_p)
1703 write_type (TREE_TYPE (type));
1705 /* Now mangle the types of the arguments. */
1706 write_method_parms (TYPE_ARG_TYPES (type),
1707 TREE_CODE (type) == METHOD_TYPE,
1708 decl);
1711 /* Write the mangled representation of a method parameter list of
1712 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
1713 considered a non-static method, and the this parameter is omitted.
1714 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1715 parameters are being emitted. */
1717 static void
1718 write_method_parms (parm_types, method_p, decl)
1719 tree decl;
1720 tree parm_types;
1721 int method_p;
1723 tree first_parm_type;
1724 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1726 /* Assume this parameter type list is variable-length. If it ends
1727 with a void type, then it's not. */
1728 int varargs_p = 1;
1730 /* If this is a member function, skip the first arg, which is the
1731 this pointer.
1732 "Member functions do not encode the type of their implicit this
1733 parameter."
1735 Similarly, there's no need to mangle artificial parameters, like
1736 the VTT parameters for constructors and destructors. */
1737 if (method_p)
1739 parm_types = TREE_CHAIN (parm_types);
1740 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1742 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1744 parm_types = TREE_CHAIN (parm_types);
1745 parm_decl = TREE_CHAIN (parm_decl);
1749 for (first_parm_type = parm_types;
1750 parm_types;
1751 parm_types = TREE_CHAIN (parm_types))
1753 tree parm = TREE_VALUE (parm_types);
1754 if (parm == void_type_node)
1756 /* "Empty parameter lists, whether declared as () or
1757 conventionally as (void), are encoded with a void parameter
1758 (v)." */
1759 if (parm_types == first_parm_type)
1760 write_type (parm);
1761 /* If the parm list is terminated with a void type, it's
1762 fixed-length. */
1763 varargs_p = 0;
1764 /* A void type better be the last one. */
1765 my_friendly_assert (TREE_CHAIN (parm_types) == NULL, 20000523);
1767 else
1768 write_type (parm);
1771 if (varargs_p)
1772 /* <builtin-type> ::= z # ellipsis */
1773 write_char ('z');
1776 /* <class-enum-type> ::= <name> */
1778 static void
1779 write_class_enum_type (type)
1780 tree type;
1782 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1785 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1786 arguments.
1788 <template-args> ::= I <template-arg>+ E */
1790 static void
1791 write_template_args (args)
1792 tree args;
1794 MANGLE_TRACE_TREE ("template-args", args);
1796 write_char ('I');
1798 if (TREE_CODE (args) == TREE_VEC)
1800 int i;
1801 int length = TREE_VEC_LENGTH (args);
1802 my_friendly_assert (length > 0, 20000422);
1804 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1806 /* We have nested template args. We want the innermost template
1807 argument list. */
1808 args = TREE_VEC_ELT (args, length - 1);
1809 length = TREE_VEC_LENGTH (args);
1811 for (i = 0; i < length; ++i)
1812 write_template_arg (TREE_VEC_ELT (args, i));
1814 else
1816 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 20021014);
1818 while (args)
1820 write_template_arg (TREE_VALUE (args));
1821 args = TREE_CHAIN (args);
1825 write_char ('E');
1828 /* <expression> ::= <unary operator-name> <expression>
1829 ::= <binary operator-name> <expression> <expression>
1830 ::= <expr-primary>
1832 <expr-primary> ::= <template-param>
1833 ::= L <type> <value number> E # literal
1834 ::= L <mangled-name> E # external name
1835 ::= sr <type> <unqualified-name>
1836 ::= sr <type> <unqualified-name> <template-args> */
1838 static void
1839 write_expression (expr)
1840 tree expr;
1842 enum tree_code code;
1844 code = TREE_CODE (expr);
1846 /* Handle pointers-to-members by making them look like expression
1847 nodes. */
1848 if (code == PTRMEM_CST)
1850 expr = build_nt (ADDR_EXPR,
1851 build_nt (SCOPE_REF,
1852 PTRMEM_CST_CLASS (expr),
1853 PTRMEM_CST_MEMBER (expr)));
1854 code = TREE_CODE (expr);
1857 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1858 is converted (via qualification conversions) to another
1859 type. */
1860 while (TREE_CODE (expr) == NOP_EXPR
1861 || TREE_CODE (expr) == NON_LVALUE_EXPR)
1863 expr = TREE_OPERAND (expr, 0);
1864 code = TREE_CODE (expr);
1867 /* Handle template parameters. */
1868 if (code == TEMPLATE_TYPE_PARM
1869 || code == TEMPLATE_TEMPLATE_PARM
1870 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1871 || code == TEMPLATE_PARM_INDEX)
1872 write_template_param (expr);
1873 /* Handle literals. */
1874 else if (TREE_CODE_CLASS (code) == 'c'
1875 || (abi_version_at_least (2) && code == CONST_DECL))
1876 write_template_arg_literal (expr);
1877 else if (DECL_P (expr))
1879 /* G++ 3.2 incorrectly mangled non-type template arguments of
1880 enumeration type using their names. */
1881 if (code == CONST_DECL)
1882 G.need_abi_warning = 1;
1883 write_char ('L');
1884 write_mangled_name (expr);
1885 write_char ('E');
1887 else if (TREE_CODE (expr) == SIZEOF_EXPR
1888 && TYPE_P (TREE_OPERAND (expr, 0)))
1890 write_string ("st");
1891 write_type (TREE_OPERAND (expr, 0));
1893 else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
1895 tree scope = TREE_OPERAND (expr, 0);
1896 tree member = TREE_OPERAND (expr, 1);
1898 /* If the MEMBER is a real declaration, then the qualifying
1899 scope was not dependent. Ideally, we would not have a
1900 SCOPE_REF in those cases, but sometimes we do. If the second
1901 argument is a DECL, then the name must not have been
1902 dependent. */
1903 if (DECL_P (member))
1904 write_expression (member);
1905 else
1907 tree template_args;
1909 write_string ("sr");
1910 write_type (scope);
1911 /* If MEMBER is a template-id, separate the template
1912 from the arguments. */
1913 if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
1915 template_args = TREE_OPERAND (member, 1);
1916 member = TREE_OPERAND (member, 0);
1917 if (TREE_CODE (member) == LOOKUP_EXPR)
1918 member = TREE_OPERAND (member, 0);
1920 else
1921 template_args = NULL_TREE;
1922 /* Write out the name of the MEMBER. */
1923 if (IDENTIFIER_TYPENAME_P (member))
1924 write_conversion_operator_name (TREE_TYPE (member));
1925 else if (IDENTIFIER_OPNAME_P (member))
1927 int i;
1928 const char *mangled_name = NULL;
1930 /* Unfortunately, there is no easy way to go from the
1931 name of the operator back to the corresponding tree
1932 code. */
1933 for (i = 0; i < LAST_CPLUS_TREE_CODE; ++i)
1934 if (operator_name_info[i].identifier == member)
1936 /* The ABI says that we prefer binary operator
1937 names to unary operator names. */
1938 if (operator_name_info[i].arity == 2)
1940 mangled_name = operator_name_info[i].mangled_name;
1941 break;
1943 else if (!mangled_name)
1944 mangled_name = operator_name_info[i].mangled_name;
1946 else if (assignment_operator_name_info[i].identifier
1947 == member)
1949 mangled_name
1950 = assignment_operator_name_info[i].mangled_name;
1951 break;
1953 write_string (mangled_name);
1955 else
1956 write_source_name (member);
1957 /* Write out the template arguments. */
1958 if (template_args)
1959 write_template_args (template_args);
1962 else
1964 int i;
1966 /* When we bind a variable or function to a non-type template
1967 argument with reference type, we create an ADDR_EXPR to show
1968 the fact that the entity's address has been taken. But, we
1969 don't actually want to output a mangling code for the `&'. */
1970 if (TREE_CODE (expr) == ADDR_EXPR
1971 && TREE_TYPE (expr)
1972 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
1974 expr = TREE_OPERAND (expr, 0);
1975 if (DECL_P (expr))
1977 write_expression (expr);
1978 return;
1981 code = TREE_CODE (expr);
1984 /* If it wasn't any of those, recursively expand the expression. */
1985 write_string (operator_name_info[(int) code].mangled_name);
1987 switch (code)
1989 case CAST_EXPR:
1990 write_type (TREE_TYPE (expr));
1991 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
1992 break;
1994 case STATIC_CAST_EXPR:
1995 case CONST_CAST_EXPR:
1996 write_type (TREE_TYPE (expr));
1997 write_expression (TREE_OPERAND (expr, 0));
1998 break;
2001 /* Handle pointers-to-members specially. */
2002 case SCOPE_REF:
2003 write_type (TREE_OPERAND (expr, 0));
2004 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
2005 write_source_name (TREE_OPERAND (expr, 1));
2006 else
2008 /* G++ 3.2 incorrectly put out both the "sr" code and
2009 the nested name of the qualified name. */
2010 G.need_abi_warning = 1;
2011 write_encoding (TREE_OPERAND (expr, 1));
2013 break;
2015 default:
2016 for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
2017 write_expression (TREE_OPERAND (expr, i));
2022 /* Literal subcase of non-terminal <template-arg>.
2024 "Literal arguments, e.g. "A<42L>", are encoded with their type
2025 and value. Negative integer values are preceded with "n"; for
2026 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2027 encoded as 0, true as 1. If floating-point arguments are accepted
2028 as an extension, their values should be encoded using a
2029 fixed-length lowercase hexadecimal string corresponding to the
2030 internal representation (IEEE on IA-64), high-order bytes first,
2031 without leading zeroes. For example: "Lfbff000000E" is -1.0f." */
2033 static void
2034 write_template_arg_literal (value)
2035 tree value;
2037 tree type = TREE_TYPE (value);
2038 write_char ('L');
2039 write_type (type);
2041 if (TREE_CODE (value) == CONST_DECL)
2042 write_integer_cst (DECL_INITIAL (value));
2043 else if (TREE_CODE (value) == INTEGER_CST)
2045 if (same_type_p (type, boolean_type_node))
2047 if (value == boolean_false_node || integer_zerop (value))
2048 write_unsigned_number (0);
2049 else if (value == boolean_true_node)
2050 write_unsigned_number (1);
2051 else
2052 abort ();
2054 else
2055 write_integer_cst (value);
2057 else if (TREE_CODE (value) == REAL_CST)
2059 #ifdef CROSS_COMPILE
2060 static int explained;
2062 if (!explained)
2064 sorry ("real-valued template parameters when cross-compiling");
2065 explained = 1;
2067 #else
2068 size_t i;
2069 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
2070 write_number (((unsigned char *)
2071 &TREE_REAL_CST (value))[i],
2072 /*unsigned_p=*/1,
2073 16);
2074 #endif
2076 else
2077 abort ();
2079 write_char ('E');
2082 /* Non-terminal <tempalate-arg>.
2084 <template-arg> ::= <type> # type
2085 ::= L <type> </value/ number> E # literal
2086 ::= LZ <name> E # external name
2087 ::= X <expression> E # expression */
2089 static void
2090 write_template_arg (node)
2091 tree node;
2093 enum tree_code code = TREE_CODE (node);
2095 MANGLE_TRACE_TREE ("template-arg", node);
2097 /* A template template paramter's argument list contains TREE_LIST
2098 nodes of which the value field is the the actual argument. */
2099 if (code == TREE_LIST)
2101 node = TREE_VALUE (node);
2102 /* If it's a decl, deal with its type instead. */
2103 if (DECL_P (node))
2105 node = TREE_TYPE (node);
2106 code = TREE_CODE (node);
2110 if (TYPE_P (node))
2111 write_type (node);
2112 else if (code == TEMPLATE_DECL)
2113 /* A template appearing as a template arg is a template template arg. */
2114 write_template_template_arg (node);
2115 else if ((TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST)
2116 || (abi_version_at_least (2) && code == CONST_DECL))
2117 write_template_arg_literal (node);
2118 else if (DECL_P (node))
2120 /* G++ 3.2 incorrectly mangled non-type template arguments of
2121 enumeration type using their names. */
2122 if (code == CONST_DECL)
2123 G.need_abi_warning = 1;
2124 write_char ('L');
2125 write_char ('Z');
2126 write_encoding (node);
2127 write_char ('E');
2129 else
2131 /* Template arguments may be expressions. */
2132 write_char ('X');
2133 write_expression (node);
2134 write_char ('E');
2138 /* <template-template-arg>
2139 ::= <name>
2140 ::= <substitution> */
2142 void
2143 write_template_template_arg (tree decl)
2145 MANGLE_TRACE_TREE ("template-template-arg", decl);
2147 if (find_substitution (decl))
2148 return;
2149 write_name (decl, /*ignore_local_scope=*/0);
2150 add_substitution (decl);
2154 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
2156 <array-type> ::= A [</dimension/ number>] _ </element/ type>
2157 ::= A <expression> _ </element/ type>
2159 "Array types encode the dimension (number of elements) and the
2160 element type. For variable length arrays, the dimension (but not
2161 the '_' separator) is omitted." */
2163 static void
2164 write_array_type (type)
2165 tree type;
2167 write_char ('A');
2168 if (TYPE_DOMAIN (type))
2170 tree index_type;
2171 tree max;
2173 index_type = TYPE_DOMAIN (type);
2174 /* The INDEX_TYPE gives the upper and lower bounds of the
2175 array. */
2176 max = TYPE_MAX_VALUE (index_type);
2177 if (TREE_CODE (max) == INTEGER_CST)
2179 /* The ABI specifies that we should mangle the number of
2180 elements in the array, not the largest allowed index. */
2181 max = size_binop (PLUS_EXPR, max, size_one_node);
2182 write_unsigned_number (tree_low_cst (max, 1));
2184 else
2185 write_expression (TREE_OPERAND (max, 0));
2187 write_char ('_');
2188 write_type (TREE_TYPE (type));
2191 /* Non-terminal <pointer-to-member-type> for pointer-to-member
2192 variables. TYPE is a pointer-to-member POINTER_TYPE.
2194 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2196 static void
2197 write_pointer_to_member_type (type)
2198 tree type;
2200 write_char ('M');
2201 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
2202 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2205 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
2206 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2207 TEMPLATE_PARM_INDEX.
2209 <template-param> ::= T </parameter/ number> _
2211 If we are internally mangling then we distinguish level and, for
2212 non-type parms, type too. The mangling appends
2214 </level/ number> _ </non-type type/ type> _
2216 This is used by mangle_conv_op_name_for_type. */
2218 static void
2219 write_template_param (parm)
2220 tree parm;
2222 int parm_index;
2223 int parm_level;
2224 tree parm_type = NULL_TREE;
2226 MANGLE_TRACE_TREE ("template-parm", parm);
2228 switch (TREE_CODE (parm))
2230 case TEMPLATE_TYPE_PARM:
2231 case TEMPLATE_TEMPLATE_PARM:
2232 case BOUND_TEMPLATE_TEMPLATE_PARM:
2233 parm_index = TEMPLATE_TYPE_IDX (parm);
2234 parm_level = TEMPLATE_TYPE_LEVEL (parm);
2235 break;
2237 case TEMPLATE_PARM_INDEX:
2238 parm_index = TEMPLATE_PARM_IDX (parm);
2239 parm_level = TEMPLATE_PARM_LEVEL (parm);
2240 parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
2241 break;
2243 default:
2244 abort ();
2247 write_char ('T');
2248 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2249 earliest template param denoted by `_'. */
2250 if (parm_index > 0)
2251 write_unsigned_number (parm_index - 1);
2252 write_char ('_');
2253 if (G.internal_mangling_p)
2255 if (parm_level > 0)
2256 write_unsigned_number (parm_level - 1);
2257 write_char ('_');
2258 if (parm_type)
2259 write_type (parm_type);
2260 write_char ('_');
2264 /* <template-template-param>
2265 ::= <template-param>
2266 ::= <substitution> */
2268 static void
2269 write_template_template_param (parm)
2270 tree parm;
2272 tree template = NULL_TREE;
2274 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2275 template template parameter. The substitution candidate here is
2276 only the template. */
2277 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2279 template
2280 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2281 if (find_substitution (template))
2282 return;
2285 /* <template-param> encodes only the template parameter position,
2286 not its template arguments, which is fine here. */
2287 write_template_param (parm);
2288 if (template)
2289 add_substitution (template);
2292 /* Non-terminal <substitution>.
2294 <substitution> ::= S <seq-id> _
2295 ::= S_ */
2297 static void
2298 write_substitution (seq_id)
2299 int seq_id;
2301 MANGLE_TRACE ("substitution", "");
2303 write_char ('S');
2304 if (seq_id > 0)
2305 write_number (seq_id - 1, /*unsigned=*/1, 36);
2306 write_char ('_');
2309 /* Start mangling ENTITY. */
2311 static inline void
2312 start_mangling (tree entity)
2314 G.entity = entity;
2315 G.need_abi_warning = false;
2316 VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
2317 obstack_free (&G.name_obstack, obstack_base (&G.name_obstack));
2320 /* Done with mangling. Return the generated mangled name. If WARN is
2321 true, and the name of G.entity will be mangled differently in a
2322 future version of the ABI, issue a warning. */
2324 static inline const char *
2325 finish_mangling (bool warn)
2327 if (warn_abi && warn && G.need_abi_warning)
2328 warning ("the mangled name of `%D' will change in a future "
2329 "version of GCC",
2330 G.entity);
2332 /* Clear all the substitutions. */
2333 G.substitutions = 0;
2335 /* Null-terminate the string. */
2336 write_char ('\0');
2338 return (const char *) obstack_base (&G.name_obstack);
2341 /* Initialize data structures for mangling. */
2343 void
2344 init_mangle ()
2346 gcc_obstack_init (&G.name_obstack);
2348 /* Cache these identifiers for quick comparison when checking for
2349 standard substitutions. */
2350 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2351 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2352 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2353 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2354 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2355 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2358 /* Generate the mangled name of DECL. */
2360 static const char *
2361 mangle_decl_string (decl)
2362 tree decl;
2364 const char *result;
2366 start_mangling (decl);
2368 if (TREE_CODE (decl) == TYPE_DECL)
2369 write_type (TREE_TYPE (decl));
2370 else if (/* The names of `extern "C"' functions are not mangled. */
2371 (DECL_EXTERN_C_FUNCTION_P (decl)
2372 /* But overloaded operator names *are* mangled. */
2373 && !DECL_OVERLOADED_OPERATOR_P (decl))
2374 /* The names of global variables aren't mangled either. */
2375 || (TREE_CODE (decl) == VAR_DECL
2376 && CP_DECL_CONTEXT (decl) == global_namespace)
2377 /* And neither are `extern "C"' variables. */
2378 || (TREE_CODE (decl) == VAR_DECL
2379 && DECL_EXTERN_C_P (decl)))
2380 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
2381 else
2383 write_mangled_name (decl);
2384 if (DECL_LANG_SPECIFIC (decl)
2385 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
2386 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
2387 /* We need a distinct mangled name for these entities, but
2388 we should never actually output it. So, we append some
2389 characters the assembler won't like. */
2390 write_string (" *INTERNAL* ");
2393 result = finish_mangling (/*warn=*/true);
2394 if (DEBUG_MANGLE)
2395 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2396 return result;
2399 /* Create an identifier for the external mangled name of DECL. */
2401 void
2402 mangle_decl (decl)
2403 tree decl;
2405 tree id = get_identifier (mangle_decl_string (decl));
2407 SET_DECL_ASSEMBLER_NAME (decl, id);
2410 /* Generate the mangled representation of TYPE. */
2412 const char *
2413 mangle_type_string (type)
2414 tree type;
2416 const char *result;
2418 start_mangling (type);
2419 write_type (type);
2420 result = finish_mangling (/*warn=*/false);
2421 if (DEBUG_MANGLE)
2422 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2423 return result;
2426 /* Create an identifier for the mangled representation of TYPE. */
2428 tree
2429 mangle_type (type)
2430 tree type;
2432 return get_identifier (mangle_type_string (type));
2435 /* Create an identifier for the mangled name of a special component
2436 for belonging to TYPE. CODE is the ABI-specified code for this
2437 component. */
2439 static tree
2440 mangle_special_for_type (type, code)
2441 tree type;
2442 const char *code;
2444 const char *result;
2446 /* We don't have an actual decl here for the special component, so
2447 we can't just process the <encoded-name>. Instead, fake it. */
2448 start_mangling (type);
2450 /* Start the mangling. */
2451 write_string ("_Z");
2452 write_string (code);
2454 /* Add the type. */
2455 write_type (type);
2456 result = finish_mangling (/*warn=*/false);
2458 if (DEBUG_MANGLE)
2459 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2461 return get_identifier (result);
2464 /* Create an identifier for the mangled representation of the typeinfo
2465 structure for TYPE. */
2467 tree
2468 mangle_typeinfo_for_type (type)
2469 tree type;
2471 return mangle_special_for_type (type, "TI");
2474 /* Create an identifier for the mangled name of the NTBS containing
2475 the mangled name of TYPE. */
2477 tree
2478 mangle_typeinfo_string_for_type (type)
2479 tree type;
2481 return mangle_special_for_type (type, "TS");
2484 /* Create an identifier for the mangled name of the vtable for TYPE. */
2486 tree
2487 mangle_vtbl_for_type (type)
2488 tree type;
2490 return mangle_special_for_type (type, "TV");
2493 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2495 tree
2496 mangle_vtt_for_type (type)
2497 tree type;
2499 return mangle_special_for_type (type, "TT");
2502 /* Return an identifier for a construction vtable group. TYPE is
2503 the most derived class in the hierarchy; BINFO is the base
2504 subobject for which this construction vtable group will be used.
2506 This mangling isn't part of the ABI specification; in the ABI
2507 specification, the vtable group is dumped in the same COMDAT as the
2508 main vtable, and is referenced only from that vtable, so it doesn't
2509 need an external name. For binary formats without COMDAT sections,
2510 though, we need external names for the vtable groups.
2512 We use the production
2514 <special-name> ::= CT <type> <offset number> _ <base type> */
2516 tree
2517 mangle_ctor_vtbl_for_type (type, binfo)
2518 tree type;
2519 tree binfo;
2521 const char *result;
2523 start_mangling (type);
2525 write_string ("_Z");
2526 write_string ("TC");
2527 write_type (type);
2528 write_integer_cst (BINFO_OFFSET (binfo));
2529 write_char ('_');
2530 write_type (BINFO_TYPE (binfo));
2532 result = finish_mangling (/*warn=*/false);
2533 if (DEBUG_MANGLE)
2534 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2535 return get_identifier (result);
2538 /* Mangle a this pointer or result pointer adjustment.
2540 <call-offset> ::= h <fixed offset number> _
2541 ::= v <fixed offset number> _ <virtual offset number> _ */
2543 static void
2544 mangle_call_offset (fixed_offset, virtual_offset)
2545 tree fixed_offset;
2546 tree virtual_offset;
2548 write_char (virtual_offset ? 'v' : 'h');
2550 /* For either flavor, write the fixed offset. */
2551 write_integer_cst (fixed_offset);
2552 write_char ('_');
2554 /* For a virtual thunk, add the virtual offset. */
2555 if (virtual_offset)
2557 write_integer_cst (virtual_offset);
2558 write_char ('_');
2562 /* Return an identifier for the mangled name of a this-adjusting or
2563 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
2564 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
2565 is a virtual thunk, and it is the vtbl offset in
2566 bytes. THIS_ADJUSTING is non-zero for a this adjusting thunk and
2567 zero for a covariant thunk. Note, that FN_DECL might be a covariant
2568 thunk itself. A covariant thunk name always includes the adjustment
2569 for the this pointer, even if there is none.
2571 <special-name> ::= T <call-offset> <base encoding>
2572 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2573 <base encoding>
2576 tree
2577 mangle_thunk (fn_decl, this_adjusting, fixed_offset, virtual_offset)
2578 tree fn_decl;
2579 int this_adjusting;
2580 tree fixed_offset;
2581 tree virtual_offset;
2583 const char *result;
2585 start_mangling (fn_decl);
2587 write_string ("_Z");
2588 write_char ('T');
2590 if (!this_adjusting)
2592 /* Covariant thunk with no this adjustment */
2593 write_char ('c');
2594 mangle_call_offset (integer_zero_node, NULL_TREE);
2595 mangle_call_offset (fixed_offset, virtual_offset);
2597 else if (!DECL_THUNK_P (fn_decl))
2598 /* Plain this adjusting thunk. */
2599 mangle_call_offset (fixed_offset, virtual_offset);
2600 else
2602 /* This adjusting thunk to covariant thunk. */
2603 write_char ('c');
2604 mangle_call_offset (fixed_offset, virtual_offset);
2605 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2606 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2607 if (virtual_offset)
2608 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2609 mangle_call_offset (fixed_offset, virtual_offset);
2610 fn_decl = THUNK_TARGET (fn_decl);
2613 /* Scoped name. */
2614 write_encoding (fn_decl);
2616 result = finish_mangling (/*warn=*/false);
2617 if (DEBUG_MANGLE)
2618 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2619 return get_identifier (result);
2622 /* Return an identifier for the mangled unqualified name for a
2623 conversion operator to TYPE. This mangling is not specified by the
2624 ABI spec; it is only used internally. */
2626 tree
2627 mangle_conv_op_name_for_type (type)
2628 tree type;
2630 tree identifier;
2631 const char *mangled_type;
2632 char *op_name;
2634 /* Build the internal mangling for TYPE. */
2635 G.internal_mangling_p = true;
2636 mangled_type = mangle_type_string (type);
2637 G.internal_mangling_p = false;
2639 /* Allocate a temporary buffer for the complete name. */
2640 op_name = concat ("operator ", mangled_type, NULL);
2641 /* Find or create an identifier. */
2642 identifier = get_identifier (op_name);
2643 /* Done with the temporary buffer. */
2644 free (op_name);
2646 /* It had better be a unique mangling for the type. */
2647 if (IDENTIFIER_TYPENAME_P (identifier)
2648 && !same_type_p (type, TREE_TYPE (identifier)))
2650 /* In G++ 3.2, the name mangling scheme was ambiguous. In later
2651 versions of the ABI, this problem has been fixed. */
2652 if (abi_version_at_least (2))
2653 abort ();
2654 error ("due to a defect in the G++ 3.2 ABI, G++ has assigned the "
2655 "same mangled name to two different types");
2658 /* Set bits on the identifier so we know later it's a conversion. */
2659 IDENTIFIER_OPNAME_P (identifier) = 1;
2660 IDENTIFIER_TYPENAME_P (identifier) = 1;
2661 /* Hang TYPE off the identifier so it can be found easily later when
2662 performing conversions. */
2663 TREE_TYPE (identifier) = type;
2665 return identifier;
2668 /* Return an identifier for the name of an initialization guard
2669 variable for indicated VARIABLE. */
2671 tree
2672 mangle_guard_variable (variable)
2673 tree variable;
2675 start_mangling (variable);
2676 write_string ("_ZGV");
2677 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
2678 /* The name of a guard variable for a reference temporary should refer
2679 to the reference, not the temporary. */
2680 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
2681 else
2682 write_name (variable, /*ignore_local_scope=*/0);
2683 return get_identifier (finish_mangling (/*warn=*/false));
2686 /* Return an identifier for the name of a temporary variable used to
2687 initialize a static reference. This isn't part of the ABI, but we might
2688 as well call them something readable. */
2690 tree
2691 mangle_ref_init_variable (variable)
2692 tree variable;
2694 start_mangling (variable);
2695 write_string ("_ZGR");
2696 write_name (variable, /*ignore_local_scope=*/0);
2697 return get_identifier (finish_mangling (/*warn=*/false));
2701 /* Foreign language type mangling section. */
2703 /* How to write the type codes for the integer Java type. */
2705 static void
2706 write_java_integer_type_codes (type)
2707 tree type;
2709 if (type == java_int_type_node)
2710 write_char ('i');
2711 else if (type == java_short_type_node)
2712 write_char ('s');
2713 else if (type == java_byte_type_node)
2714 write_char ('c');
2715 else if (type == java_char_type_node)
2716 write_char ('w');
2717 else if (type == java_long_type_node)
2718 write_char ('x');
2719 else if (type == java_boolean_type_node)
2720 write_char ('b');
2721 else
2722 abort ();
2725 #include "gt-cp-mangle.h"