1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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)
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
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
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
52 #include "coretypes.h"
64 /* Debugging support. */
66 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
68 #define DEBUG_MANGLE 0
71 /* Macros for tracing the write_* functions. */
73 # define MANGLE_TRACE(FN, INPUT) \
74 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
75 # define MANGLE_TRACE_TREE(FN, NODE) \
76 fprintf (stderr, " %-24s: %-24s (%p)\n", \
77 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
79 # define MANGLE_TRACE(FN, INPUT)
80 # define MANGLE_TRACE_TREE(FN, NODE)
83 /* Nonzero if NODE is a class template-id. We can't rely on
84 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
85 that hard to distinguish A<T> from A, where A<T> is the type as
86 instantiated outside of the template, and A is the type used
87 without parameters inside the template. */
88 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
89 (TYPE_LANG_SPECIFIC (NODE) != NULL \
90 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
91 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
92 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
94 /* Things we only need one of. This module is not reentrant. */
95 typedef struct globals GTY(())
97 /* An array of the current substitution candidates, in the order
99 varray_type substitutions;
101 /* The entity that is being mangled. */
102 tree GTY ((skip)) entity;
104 /* True if the mangling will be different in a future version of the
106 bool need_abi_warning;
109 static GTY (()) globals G;
111 /* The obstack on which we build mangled names. */
112 static struct obstack *mangle_obstack;
114 /* The obstack on which we build mangled names that are not going to
115 be IDENTIFIER_NODEs. */
116 static struct obstack name_obstack;
118 /* The first object on the name_obstack; we use this to free memory
119 allocated on the name_obstack. */
120 static void *name_base;
122 /* An incomplete mangled name. There will be no NUL terminator. If
123 there is no incomplete mangled name, this variable is NULL. */
124 static char *partially_mangled_name;
126 /* The number of characters in the PARTIALLY_MANGLED_NAME. */
127 static size_t partially_mangled_name_len;
129 /* Indices into subst_identifiers. These are identifiers used in
130 special substitution rules. */
138 SUBID_BASIC_IOSTREAM,
141 substitution_identifier_index_t;
143 /* For quick substitution checks, look up these common identifiers
145 static GTY(()) tree subst_identifiers[SUBID_MAX];
147 /* Single-letter codes for builtin integer types, defined in
148 <builtin-type>. These are indexed by integer_type_kind values. */
150 integer_type_codes[itk_none] =
153 'a', /* itk_signed_char */
154 'h', /* itk_unsigned_char */
156 't', /* itk_unsigned_short */
158 'j', /* itk_unsigned_int */
160 'm', /* itk_unsigned_long */
161 'x', /* itk_long_long */
162 'y' /* itk_unsigned_long_long */
165 static int decl_is_template_id (const tree, tree* const);
167 /* Functions for handling substitutions. */
169 static inline tree canonicalize_for_substitution (tree);
170 static void add_substitution (tree);
171 static inline int is_std_substitution (const tree,
172 const substitution_identifier_index_t);
173 static inline int is_std_substitution_char (const tree,
174 const substitution_identifier_index_t);
175 static int find_substitution (tree);
176 static void mangle_call_offset (const tree, const tree);
178 /* Functions for emitting mangled representations of things. */
180 static void write_mangled_name (const tree, bool);
181 static void write_encoding (const tree);
182 static void write_name (tree, const int);
183 static void write_unscoped_name (const tree);
184 static void write_unscoped_template_name (const tree);
185 static void write_nested_name (const tree);
186 static void write_prefix (const tree);
187 static void write_template_prefix (const tree);
188 static void write_unqualified_name (const tree);
189 static void write_conversion_operator_name (const tree);
190 static void write_source_name (tree);
191 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
193 static void write_number (unsigned HOST_WIDE_INT, const int,
195 static void write_integer_cst (const tree);
196 static void write_real_cst (const tree);
197 static void write_identifier (const char *);
198 static void write_special_name_constructor (const tree);
199 static void write_special_name_destructor (const tree);
200 static void write_type (tree);
201 static int write_CV_qualifiers_for_type (const tree);
202 static void write_builtin_type (tree);
203 static void write_function_type (const tree);
204 static void write_bare_function_type (const tree, const int, const tree);
205 static void write_method_parms (tree, const int, const tree);
206 static void write_class_enum_type (const tree);
207 static void write_template_args (tree);
208 static void write_expression (tree);
209 static void write_template_arg_literal (const tree);
210 static void write_template_arg (tree);
211 static void write_template_template_arg (const tree);
212 static void write_array_type (const tree);
213 static void write_pointer_to_member_type (const tree);
214 static void write_template_param (const tree);
215 static void write_template_template_param (const tree);
216 static void write_substitution (const int);
217 static int discriminator_for_local_entity (tree);
218 static int discriminator_for_string_literal (tree, tree);
219 static void write_discriminator (const int);
220 static void write_local_name (const tree, const tree, const tree);
221 static void dump_substitution_candidates (void);
222 static const char *mangle_decl_string (const tree);
224 /* Control functions. */
226 static inline void start_mangling (const tree, bool);
227 static inline const char *finish_mangling (const bool);
228 static tree mangle_special_for_type (const tree, const char *);
230 /* Foreign language functions. */
232 static void write_java_integer_type_codes (const tree);
234 /* Append a single character to the end of the mangled
236 #define write_char(CHAR) \
237 obstack_1grow (mangle_obstack, (CHAR))
239 /* Append a sized buffer to the end of the mangled representation. */
240 #define write_chars(CHAR, LEN) \
241 obstack_grow (mangle_obstack, (CHAR), (LEN))
243 /* Append a NUL-terminated string to the end of the mangled
245 #define write_string(STRING) \
246 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
248 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
249 same purpose (context, which may be a type) and value (template
250 decl). See write_template_prefix for more information on what this
252 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
253 (TREE_CODE (NODE1) == TREE_LIST \
254 && TREE_CODE (NODE2) == TREE_LIST \
255 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
256 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
257 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
258 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
260 /* Write out an unsigned quantity in base 10. */
261 #define write_unsigned_number(NUMBER) \
262 write_number ((NUMBER), /*unsigned_p=*/1, 10)
264 /* Save the current (incomplete) mangled name and release the obstack
265 storage holding it. This function should be used during mangling
266 when making a call that could result in a call to get_identifier,
267 as such a call will clobber the same obstack being used for
268 mangling. This function may not be called twice without an
269 intervening call to restore_partially_mangled_name. */
272 save_partially_mangled_name (void)
274 if (mangle_obstack == &ident_hash->stack)
276 gcc_assert (!partially_mangled_name);
277 partially_mangled_name_len = obstack_object_size (mangle_obstack);
278 partially_mangled_name = xmalloc (partially_mangled_name_len);
279 memcpy (partially_mangled_name, obstack_base (mangle_obstack),
280 partially_mangled_name_len);
281 obstack_free (mangle_obstack, obstack_finish (mangle_obstack));
285 /* Restore the incomplete mangled name saved with
286 save_partially_mangled_name. */
289 restore_partially_mangled_name (void)
291 if (partially_mangled_name)
293 obstack_grow (mangle_obstack, partially_mangled_name,
294 partially_mangled_name_len);
295 free (partially_mangled_name);
296 partially_mangled_name = NULL;
300 /* If DECL is a template instance, return nonzero and, if
301 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
302 Otherwise return zero. */
305 decl_is_template_id (const tree decl, tree* const template_info)
307 if (TREE_CODE (decl) == TYPE_DECL)
309 /* TYPE_DECLs are handled specially. Look at its type to decide
310 if this is a template instantiation. */
311 const tree type = TREE_TYPE (decl);
313 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
315 if (template_info != NULL)
316 /* For a templated TYPE_DECL, the template info is hanging
318 *template_info = TYPE_TEMPLATE_INFO (type);
324 /* Check if this is a primary template. */
325 if (DECL_LANG_SPECIFIC (decl) != NULL
326 && DECL_USE_TEMPLATE (decl)
327 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
328 && TREE_CODE (decl) != TEMPLATE_DECL)
330 if (template_info != NULL)
331 /* For most templated decls, the template info is hanging
333 *template_info = DECL_TEMPLATE_INFO (decl);
338 /* It's not a template id. */
342 /* Produce debugging output of current substitution candidates. */
345 dump_substitution_candidates (void)
349 fprintf (stderr, " ++ substitutions ");
350 for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
352 tree el = VARRAY_TREE (G.substitutions, i);
353 const char *name = "???";
356 fprintf (stderr, " ");
358 name = IDENTIFIER_POINTER (DECL_NAME (el));
359 else if (TREE_CODE (el) == TREE_LIST)
360 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
361 else if (TYPE_NAME (el))
362 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
363 fprintf (stderr, " S%d_ = ", i - 1);
365 (CP_TYPE_RESTRICT_P (el)
366 || CP_TYPE_VOLATILE_P (el)
367 || CP_TYPE_CONST_P (el)))
368 fprintf (stderr, "CV-");
369 fprintf (stderr, "%s (%s at %p)\n",
370 name, tree_code_name[TREE_CODE (el)], (void *) el);
374 /* Both decls and types can be substitution candidates, but sometimes
375 they refer to the same thing. For instance, a TYPE_DECL and
376 RECORD_TYPE for the same class refer to the same thing, and should
377 be treated accordingly in substitutions. This function returns a
378 canonicalized tree node representing NODE that is used when adding
379 and substitution candidates and finding matches. */
382 canonicalize_for_substitution (tree node)
384 /* For a TYPE_DECL, use the type instead. */
385 if (TREE_CODE (node) == TYPE_DECL)
386 node = TREE_TYPE (node);
388 node = canonical_type_variant (node);
393 /* Add NODE as a substitution candidate. NODE must not already be on
394 the list of candidates. */
397 add_substitution (tree node)
402 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
403 tree_code_name[TREE_CODE (node)], (void *) node);
405 /* Get the canonicalized substitution candidate for NODE. */
406 c = canonicalize_for_substitution (node);
407 if (DEBUG_MANGLE && c != node)
408 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
409 tree_code_name[TREE_CODE (node)], (void *) node);
413 /* Make sure NODE isn't already a candidate. */
416 for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
418 const tree candidate = VARRAY_TREE (G.substitutions, i);
420 gcc_assert (!(DECL_P (node) && node == candidate));
421 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
422 && same_type_p (node, candidate)));
425 #endif /* ENABLE_CHECKING */
427 /* Put the decl onto the varray of substitution candidates. */
428 VARRAY_PUSH_TREE (G.substitutions, node);
431 dump_substitution_candidates ();
434 /* Helper function for find_substitution. Returns nonzero if NODE,
435 which may be a decl or a CLASS_TYPE, is a template-id with template
436 name of substitution_index[INDEX] in the ::std namespace. */
439 is_std_substitution (const tree node,
440 const substitution_identifier_index_t index)
447 type = TREE_TYPE (node);
450 else if (CLASS_TYPE_P (node))
453 decl = TYPE_NAME (node);
456 /* These are not the droids you're looking for. */
459 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
460 && TYPE_LANG_SPECIFIC (type)
461 && TYPE_TEMPLATE_INFO (type)
462 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
463 == subst_identifiers[index]));
466 /* Helper function for find_substitution. Returns nonzero if NODE,
467 which may be a decl or a CLASS_TYPE, is the template-id
468 ::std::identifier<char>, where identifier is
469 substitution_index[INDEX]. */
472 is_std_substitution_char (const tree node,
473 const substitution_identifier_index_t index)
476 /* Check NODE's name is ::std::identifier. */
477 if (!is_std_substitution (node, index))
479 /* Figure out its template args. */
481 args = DECL_TI_ARGS (node);
482 else if (CLASS_TYPE_P (node))
483 args = CLASSTYPE_TI_ARGS (node);
485 /* Oops, not a template. */
487 /* NODE's template arg list should be <char>. */
489 TREE_VEC_LENGTH (args) == 1
490 && TREE_VEC_ELT (args, 0) == char_type_node;
493 /* Check whether a substitution should be used to represent NODE in
496 First, check standard special-case substitutions.
498 <substitution> ::= St
505 # ::std::basic_string
508 # ::std::basic_string<char,
509 ::std::char_traits<char>,
510 ::std::allocator<char> >
513 # ::std::basic_istream<char, ::std::char_traits<char> >
516 # ::std::basic_ostream<char, ::std::char_traits<char> >
519 # ::std::basic_iostream<char, ::std::char_traits<char> >
521 Then examine the stack of currently available substitution
522 candidates for entities appearing earlier in the same mangling
524 If a substitution is found, write its mangled representation and
525 return nonzero. If none is found, just return zero. */
528 find_substitution (tree node)
531 const int size = VARRAY_ACTIVE_SIZE (G.substitutions);
536 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
537 tree_code_name[TREE_CODE (node)], (void *) node);
539 /* Obtain the canonicalized substitution representation for NODE.
540 This is what we'll compare against. */
541 node = canonicalize_for_substitution (node);
543 /* Check for builtin substitutions. */
545 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
546 type = TYPE_P (node) ? node : TREE_TYPE (node);
548 /* Check for std::allocator. */
550 && is_std_substitution (decl, SUBID_ALLOCATOR)
551 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
557 /* Check for std::basic_string. */
558 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
562 /* If this is a type (i.e. a fully-qualified template-id),
564 std::basic_string <char,
565 std::char_traits<char>,
566 std::allocator<char> > . */
567 if (cp_type_quals (type) == TYPE_UNQUALIFIED
568 && CLASSTYPE_USE_TEMPLATE (type))
570 tree args = CLASSTYPE_TI_ARGS (type);
571 if (TREE_VEC_LENGTH (args) == 3
572 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
573 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
575 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
584 /* Substitute for the template name only if this isn't a type. */
591 /* Check for basic_{i,o,io}stream. */
593 && cp_type_quals (type) == TYPE_UNQUALIFIED
594 && CLASS_TYPE_P (type)
595 && CLASSTYPE_USE_TEMPLATE (type)
596 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
598 /* First, check for the template
599 args <char, std::char_traits<char> > . */
600 tree args = CLASSTYPE_TI_ARGS (type);
601 if (TREE_VEC_LENGTH (args) == 2
602 && TYPE_P (TREE_VEC_ELT (args, 0))
603 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
604 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
607 /* Got them. Is this basic_istream? */
608 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
609 if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
614 /* Or basic_ostream? */
615 else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
620 /* Or basic_iostream? */
621 else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
629 /* Check for namespace std. */
630 if (decl && DECL_NAMESPACE_STD_P (decl))
636 /* Now check the list of available substitutions for this mangling
638 for (i = 0; i < size; ++i)
640 tree candidate = VARRAY_TREE (G.substitutions, i);
641 /* NODE is a matched to a candidate if it's the same decl node or
642 if it's the same type. */
643 if (decl == candidate
644 || (TYPE_P (candidate) && type && TYPE_P (type)
645 && same_type_p (type, candidate))
646 || NESTED_TEMPLATE_MATCH (node, candidate))
648 write_substitution (i);
653 /* No substitution found. */
658 /* TOP_LEVEL is true, if this is being called at outermost level of
659 mangling. It should be false when mangling a decl appearing in an
660 expression within some other mangling.
662 <mangled-name> ::= _Z <encoding> */
665 write_mangled_name (const tree decl, bool top_level)
667 MANGLE_TRACE_TREE ("mangled-name", decl);
669 if (/* The names of `extern "C"' functions are not mangled. */
670 DECL_EXTERN_C_FUNCTION_P (decl)
671 /* But overloaded operator names *are* mangled. */
672 && !DECL_OVERLOADED_OPERATOR_P (decl))
677 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
680 /* The standard notes: "The <encoding> of an extern "C"
681 function is treated like global-scope data, i.e. as its
682 <source-name> without a type." We cannot write
683 overloaded operators that way though, because it contains
684 characters invalid in assembler. */
685 if (abi_version_at_least (2))
688 G.need_abi_warning = true;
689 write_source_name (DECL_NAME (decl));
692 else if (TREE_CODE (decl) == VAR_DECL
693 /* The names of global variables aren't mangled. */
694 && (CP_DECL_CONTEXT (decl) == global_namespace
695 /* And neither are `extern "C"' variables. */
696 || DECL_EXTERN_C_P (decl)))
698 if (top_level || abi_version_at_least (2))
702 G.need_abi_warning = true;
710 write_encoding (decl);
711 if (DECL_LANG_SPECIFIC (decl)
712 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
713 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
714 /* We need a distinct mangled name for these entities, but
715 we should never actually output it. So, we append some
716 characters the assembler won't like. */
717 write_string (" *INTERNAL* ");
721 /* <encoding> ::= <function name> <bare-function-type>
725 write_encoding (const tree decl)
727 MANGLE_TRACE_TREE ("encoding", decl);
729 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
731 /* For overloaded operators write just the mangled name
732 without arguments. */
733 if (DECL_OVERLOADED_OPERATOR_P (decl))
734 write_name (decl, /*ignore_local_scope=*/0);
736 write_source_name (DECL_NAME (decl));
740 write_name (decl, /*ignore_local_scope=*/0);
741 if (TREE_CODE (decl) == FUNCTION_DECL)
746 if (decl_is_template_id (decl, NULL))
748 save_partially_mangled_name ();
749 fn_type = get_mostly_instantiated_function_type (decl);
750 restore_partially_mangled_name ();
751 /* FN_TYPE will not have parameter types for in-charge or
752 VTT parameters. Therefore, we pass NULL_TREE to
753 write_bare_function_type -- otherwise, it will get
754 confused about which artificial parameters to skip. */
759 fn_type = TREE_TYPE (decl);
763 write_bare_function_type (fn_type,
764 (!DECL_CONSTRUCTOR_P (decl)
765 && !DECL_DESTRUCTOR_P (decl)
766 && !DECL_CONV_FN_P (decl)
767 && decl_is_template_id (decl, NULL)),
772 /* <name> ::= <unscoped-name>
773 ::= <unscoped-template-name> <template-args>
777 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
778 called from <local-name>, which mangles the enclosing scope
779 elsewhere and then uses this function to mangle just the part
780 underneath the function scope. So don't use the <local-name>
781 production, to avoid an infinite recursion. */
784 write_name (tree decl, const int ignore_local_scope)
788 MANGLE_TRACE_TREE ("name", decl);
790 if (TREE_CODE (decl) == TYPE_DECL)
792 /* In case this is a typedef, fish out the corresponding
793 TYPE_DECL for the main variant. */
794 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
795 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
798 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
800 /* A decl in :: or ::std scope is treated specially. The former is
801 mangled using <unscoped-name> or <unscoped-template-name>, the
802 latter with a special substitution. Also, a name that is
803 directly in a local function scope is also mangled with
804 <unscoped-name> rather than a full <nested-name>. */
806 || context == global_namespace
807 || DECL_NAMESPACE_STD_P (context)
808 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
811 /* Is this a template instance? */
812 if (decl_is_template_id (decl, &template_info))
814 /* Yes: use <unscoped-template-name>. */
815 write_unscoped_template_name (TI_TEMPLATE (template_info));
816 write_template_args (TI_ARGS (template_info));
819 /* Everything else gets an <unqualified-name>. */
820 write_unscoped_name (decl);
824 /* Handle local names, unless we asked not to (that is, invoked
825 under <local-name>, to handle only the part of the name under
827 if (!ignore_local_scope)
829 /* Scan up the list of scope context, looking for a
830 function. If we find one, this entity is in local
831 function scope. local_entity tracks context one scope
832 level down, so it will contain the element that's
833 directly in that function's scope, either decl or one of
834 its enclosing scopes. */
835 tree local_entity = decl;
836 while (context != NULL && context != global_namespace)
838 /* Make sure we're always dealing with decls. */
839 if (context != NULL && TYPE_P (context))
840 context = TYPE_NAME (context);
841 /* Is this a function? */
842 if (TREE_CODE (context) == FUNCTION_DECL)
844 /* Yes, we have local scope. Use the <local-name>
845 production for the innermost function scope. */
846 write_local_name (context, local_entity, decl);
849 /* Up one scope level. */
850 local_entity = context;
851 context = CP_DECL_CONTEXT (context);
854 /* No local scope found? Fall through to <nested-name>. */
857 /* Other decls get a <nested-name> to encode their scope. */
858 write_nested_name (decl);
862 /* <unscoped-name> ::= <unqualified-name>
863 ::= St <unqualified-name> # ::std:: */
866 write_unscoped_name (const tree decl)
868 tree context = CP_DECL_CONTEXT (decl);
870 MANGLE_TRACE_TREE ("unscoped-name", decl);
872 /* Is DECL in ::std? */
873 if (DECL_NAMESPACE_STD_P (context))
876 write_unqualified_name (decl);
880 /* If not, it should be either in the global namespace, or directly
881 in a local function scope. */
882 gcc_assert (context == global_namespace
884 || TREE_CODE (context) == FUNCTION_DECL);
886 write_unqualified_name (decl);
890 /* <unscoped-template-name> ::= <unscoped-name>
891 ::= <substitution> */
894 write_unscoped_template_name (const tree decl)
896 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
898 if (find_substitution (decl))
900 write_unscoped_name (decl);
901 add_substitution (decl);
904 /* Write the nested name, including CV-qualifiers, of DECL.
906 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
907 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
909 <CV-qualifiers> ::= [r] [V] [K] */
912 write_nested_name (const tree decl)
916 MANGLE_TRACE_TREE ("nested-name", decl);
920 /* Write CV-qualifiers, if this is a member function. */
921 if (TREE_CODE (decl) == FUNCTION_DECL
922 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
924 if (DECL_VOLATILE_MEMFUNC_P (decl))
926 if (DECL_CONST_MEMFUNC_P (decl))
930 /* Is this a template instance? */
931 if (decl_is_template_id (decl, &template_info))
933 /* Yes, use <template-prefix>. */
934 write_template_prefix (decl);
935 write_template_args (TI_ARGS (template_info));
939 /* No, just use <prefix> */
940 write_prefix (DECL_CONTEXT (decl));
941 write_unqualified_name (decl);
946 /* <prefix> ::= <prefix> <unqualified-name>
948 ::= <template-prefix> <template-args>
950 ::= <substitution> */
953 write_prefix (const tree node)
956 /* Non-NULL if NODE represents a template-id. */
957 tree template_info = NULL;
959 MANGLE_TRACE_TREE ("prefix", node);
962 || node == global_namespace)
965 if (find_substitution (node))
970 /* If this is a function decl, that means we've hit function
971 scope, so this prefix must be for a local name. In this
972 case, we're under the <local-name> production, which encodes
973 the enclosing function scope elsewhere. So don't continue
975 if (TREE_CODE (node) == FUNCTION_DECL)
979 decl_is_template_id (decl, &template_info);
983 /* Node is a type. */
984 decl = TYPE_NAME (node);
985 if (CLASSTYPE_TEMPLATE_ID_P (node))
986 template_info = TYPE_TEMPLATE_INFO (node);
989 /* In G++ 3.2, the name of the template parameter was used. */
990 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
991 && !abi_version_at_least (2))
992 G.need_abi_warning = true;
994 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
995 && abi_version_at_least (2))
996 write_template_param (node);
997 else if (template_info != NULL)
1000 write_template_prefix (decl);
1001 write_template_args (TI_ARGS (template_info));
1004 /* Not templated. */
1006 write_prefix (CP_DECL_CONTEXT (decl));
1007 write_unqualified_name (decl);
1010 add_substitution (node);
1013 /* <template-prefix> ::= <prefix> <template component>
1014 ::= <template-param>
1015 ::= <substitution> */
1018 write_template_prefix (const tree node)
1020 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1021 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1022 tree context = CP_DECL_CONTEXT (decl);
1027 MANGLE_TRACE_TREE ("template-prefix", node);
1029 /* Find the template decl. */
1030 if (decl_is_template_id (decl, &template_info))
1031 template = TI_TEMPLATE (template_info);
1034 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1036 template = TYPE_TI_TEMPLATE (type);
1039 /* For a member template, though, the template name for the
1040 innermost name must have all the outer template levels
1041 instantiated. For instance, consider
1043 template<typename T> struct Outer {
1044 template<typename U> struct Inner {};
1047 The template name for `Inner' in `Outer<int>::Inner<float>' is
1048 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1049 levels separately, so there's no TEMPLATE_DECL available for this
1050 (there's only `Outer<T>::Inner<U>').
1052 In order to get the substitutions right, we create a special
1053 TREE_LIST to represent the substitution candidate for a nested
1054 template. The TREE_PURPOSE is the template's context, fully
1055 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1058 So, for the example above, `Outer<int>::Inner' is represented as a
1059 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1060 and whose value is `Outer<T>::Inner<U>'. */
1061 if (TYPE_P (context))
1062 substitution = build_tree_list (context, template);
1064 substitution = template;
1066 if (find_substitution (substitution))
1069 /* In G++ 3.2, the name of the template template parameter was used. */
1070 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1071 && !abi_version_at_least (2))
1072 G.need_abi_warning = true;
1074 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1075 && abi_version_at_least (2))
1076 write_template_param (TREE_TYPE (template));
1079 write_prefix (context);
1080 write_unqualified_name (decl);
1083 add_substitution (substitution);
1086 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1087 mangled through special entry points.
1089 <unqualified-name> ::= <operator-name>
1091 ::= <source-name> */
1094 write_unqualified_name (const tree decl)
1096 MANGLE_TRACE_TREE ("unqualified-name", decl);
1098 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
1099 write_special_name_constructor (decl);
1100 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
1101 write_special_name_destructor (decl);
1102 else if (DECL_NAME (decl) == NULL_TREE)
1103 write_source_name (DECL_ASSEMBLER_NAME (decl));
1104 else if (DECL_CONV_FN_P (decl))
1106 /* Conversion operator. Handle it right here.
1107 <operator> ::= cv <type> */
1109 if (decl_is_template_id (decl, NULL))
1112 save_partially_mangled_name ();
1113 fn_type = get_mostly_instantiated_function_type (decl);
1114 restore_partially_mangled_name ();
1115 type = TREE_TYPE (fn_type);
1118 type = DECL_CONV_FN_TYPE (decl);
1119 write_conversion_operator_name (type);
1121 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1123 operator_name_info_t *oni;
1124 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1125 oni = assignment_operator_name_info;
1127 oni = operator_name_info;
1129 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1132 write_source_name (DECL_NAME (decl));
1135 /* Write the unqualified-name for a conversion operator to TYPE. */
1138 write_conversion_operator_name (const tree type)
1140 write_string ("cv");
1144 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1146 <source-name> ::= </length/ number> <identifier> */
1149 write_source_name (tree identifier)
1151 MANGLE_TRACE_TREE ("source-name", identifier);
1153 /* Never write the whole template-id name including the template
1154 arguments; we only want the template name. */
1155 if (IDENTIFIER_TEMPLATE (identifier))
1156 identifier = IDENTIFIER_TEMPLATE (identifier);
1158 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1159 write_identifier (IDENTIFIER_POINTER (identifier));
1162 /* Convert NUMBER to ascii using base BASE and generating at least
1163 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1164 into which to store the characters. Returns the number of
1165 characters generated (these will be layed out in advance of where
1169 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1170 char *buffer, const unsigned int min_digits)
1172 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1173 unsigned digits = 0;
1177 unsigned HOST_WIDE_INT d = number / base;
1179 *--buffer = base_digits[number - d * base];
1183 while (digits < min_digits)
1185 *--buffer = base_digits[0];
1191 /* Non-terminal <number>.
1193 <number> ::= [n] </decimal integer/> */
1196 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1197 const unsigned int base)
1199 char buffer[sizeof (HOST_WIDE_INT) * 8];
1202 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1205 number = -((HOST_WIDE_INT) number);
1207 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1208 write_chars (buffer + sizeof (buffer) - count, count);
1211 /* Write out an integral CST in decimal. Most numbers are small, and
1212 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1213 bigger than that, which we must deal with. */
1216 write_integer_cst (const tree cst)
1218 int sign = tree_int_cst_sgn (cst);
1220 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1222 /* A bignum. We do this in chunks, each of which fits in a
1224 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1225 unsigned HOST_WIDE_INT chunk;
1226 unsigned chunk_digits;
1227 char *ptr = buffer + sizeof (buffer);
1232 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1237 if (sizeof (HOST_WIDE_INT) >= 8)
1239 /* It is at least 64 bits, so 10^18 is representable. */
1244 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1245 base = build_int_cstu (type, chunk);
1246 n = build_int_cst_wide (type,
1247 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1252 n = fold_build1 (NEGATE_EXPR, type, n);
1256 tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
1257 tree tmp = fold_build2 (MULT_EXPR, type, d, base);
1260 done = integer_zerop (d);
1261 tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
1262 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1263 done ? 1 : chunk_digits);
1269 write_chars (ptr, count);
1274 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1281 write_unsigned_number (low);
1285 /* Write out a floating-point literal.
1287 "Floating-point literals are encoded using the bit pattern of the
1288 target processor's internal representation of that number, as a
1289 fixed-length lowercase hexadecimal string, high-order bytes first
1290 (even if the target processor would store low-order bytes first).
1291 The "n" prefix is not used for floating-point literals; the sign
1292 bit is encoded with the rest of the number.
1294 Here are some examples, assuming the IEEE standard representation
1295 for floating point numbers. (Spaces are for readability, not
1296 part of the encoding.)
1299 -1.0f Lf bf80 0000 E
1300 1.17549435e-38f Lf 0080 0000 E
1301 1.40129846e-45f Lf 0000 0001 E
1302 0.0f Lf 0000 0000 E"
1304 Caller is responsible for the Lx and the E. */
1306 write_real_cst (const tree value)
1308 if (abi_version_at_least (2))
1310 long target_real[4]; /* largest supported float */
1311 char buffer[9]; /* eight hex digits in a 32-bit number */
1314 tree type = TREE_TYPE (value);
1315 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1317 real_to_target (target_real, &TREE_REAL_CST (value),
1320 /* The value in target_real is in the target word order,
1321 so we must write it out backward if that happens to be
1322 little-endian. write_number cannot be used, it will
1323 produce uppercase. */
1324 if (FLOAT_WORDS_BIG_ENDIAN)
1325 i = 0, limit = words, dir = 1;
1327 i = words - 1, limit = -1, dir = -1;
1329 for (; i != limit; i += dir)
1331 sprintf (buffer, "%08lx", target_real[i]);
1332 write_chars (buffer, 8);
1337 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1338 literally. Note that compatibility with 3.2 is impossible,
1339 because the old floating-point emulator used a different
1340 format for REAL_VALUE_TYPE. */
1342 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1343 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1346 G.need_abi_warning = 1;
1350 /* Non-terminal <identifier>.
1352 <identifier> ::= </unqualified source code identifier> */
1355 write_identifier (const char *identifier)
1357 MANGLE_TRACE ("identifier", identifier);
1358 write_string (identifier);
1361 /* Handle constructor productions of non-terminal <special-name>.
1362 CTOR is a constructor FUNCTION_DECL.
1364 <special-name> ::= C1 # complete object constructor
1365 ::= C2 # base object constructor
1366 ::= C3 # complete object allocating constructor
1368 Currently, allocating constructors are never used.
1370 We also need to provide mangled names for the maybe-in-charge
1371 constructor, so we treat it here too. mangle_decl_string will
1372 append *INTERNAL* to that, to make sure we never emit it. */
1375 write_special_name_constructor (const tree ctor)
1377 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1378 write_string ("C2");
1381 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1382 /* Even though we don't ever emit a definition of
1383 the old-style destructor, we still have to
1384 consider entities (like static variables) nested
1386 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor));
1387 write_string ("C1");
1391 /* Handle destructor productions of non-terminal <special-name>.
1392 DTOR is a destructor FUNCTION_DECL.
1394 <special-name> ::= D0 # deleting (in-charge) destructor
1395 ::= D1 # complete object (in-charge) destructor
1396 ::= D2 # base object (not-in-charge) destructor
1398 We also need to provide mangled names for the maybe-incharge
1399 destructor, so we treat it here too. mangle_decl_string will
1400 append *INTERNAL* to that, to make sure we never emit it. */
1403 write_special_name_destructor (const tree dtor)
1405 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1406 write_string ("D0");
1407 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1408 write_string ("D2");
1411 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1412 /* Even though we don't ever emit a definition of
1413 the old-style destructor, we still have to
1414 consider entities (like static variables) nested
1416 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1417 write_string ("D1");
1421 /* Return the discriminator for ENTITY appearing inside
1422 FUNCTION. The discriminator is the lexical ordinal of VAR among
1423 entities with the same name in the same FUNCTION. */
1426 discriminator_for_local_entity (tree entity)
1430 /* Assume this is the only local entity with this name. */
1431 int discriminator = 0;
1433 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1434 discriminator = DECL_DISCRIMINATOR (entity);
1435 else if (TREE_CODE (entity) == TYPE_DECL)
1437 /* Scan the list of local classes. */
1438 entity = TREE_TYPE (entity);
1439 for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
1440 if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1441 && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1445 return discriminator;
1448 /* Return the discriminator for STRING, a string literal used inside
1449 FUNCTION. The discriminator is the lexical ordinal of STRING among
1450 string literals used in FUNCTION. */
1453 discriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
1454 tree string ATTRIBUTE_UNUSED)
1456 /* For now, we don't discriminate amongst string literals. */
1460 /* <discriminator> := _ <number>
1462 The discriminator is used only for the second and later occurrences
1463 of the same name within a single function. In this case <number> is
1464 n - 2, if this is the nth occurrence, in lexical order. */
1467 write_discriminator (const int discriminator)
1469 /* If discriminator is zero, don't write anything. Otherwise... */
1470 if (discriminator > 0)
1473 write_unsigned_number (discriminator - 1);
1477 /* Mangle the name of a function-scope entity. FUNCTION is the
1478 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1479 the entity itself. LOCAL_ENTITY is the entity that's directly
1480 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1483 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1484 := Z <function encoding> E s [<discriminator>] */
1487 write_local_name (const tree function, const tree local_entity,
1490 MANGLE_TRACE_TREE ("local-name", entity);
1493 write_encoding (function);
1495 if (TREE_CODE (entity) == STRING_CST)
1498 write_discriminator (discriminator_for_string_literal (function,
1503 /* Now the <entity name>. Let write_name know its being called
1504 from <local-name>, so it doesn't try to process the enclosing
1505 function scope again. */
1506 write_name (entity, /*ignore_local_scope=*/1);
1507 write_discriminator (discriminator_for_local_entity (local_entity));
1511 /* Non-terminals <type> and <CV-qualifier>.
1513 <type> ::= <builtin-type>
1515 ::= <class-enum-type>
1517 ::= <pointer-to-member-type>
1518 ::= <template-param>
1521 ::= P <type> # pointer-to
1522 ::= R <type> # reference-to
1523 ::= C <type> # complex pair (C 2000)
1524 ::= G <type> # imaginary (C 2000) [not supported]
1525 ::= U <source-name> <type> # vendor extended type qualifier
1527 TYPE is a type node. */
1530 write_type (tree type)
1532 /* This gets set to nonzero if TYPE turns out to be a (possibly
1533 CV-qualified) builtin type. */
1534 int is_builtin_type = 0;
1536 MANGLE_TRACE_TREE ("type", type);
1538 if (type == error_mark_node)
1541 if (find_substitution (type))
1544 if (write_CV_qualifiers_for_type (type) > 0)
1545 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1546 mangle the unqualified type. The recursive call is needed here
1547 since both the qualified and unqualified types are substitution
1549 write_type (TYPE_MAIN_VARIANT (type));
1550 else if (TREE_CODE (type) == ARRAY_TYPE)
1551 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1552 so that the cv-qualification of the element type is available
1553 in write_array_type. */
1554 write_array_type (type);
1557 /* See through any typedefs. */
1558 type = TYPE_MAIN_VARIANT (type);
1560 if (TYPE_PTRMEM_P (type))
1561 write_pointer_to_member_type (type);
1562 else switch (TREE_CODE (type))
1566 case INTEGER_TYPE: /* Includes wchar_t. */
1569 /* Handle any target-specific fundamental types. */
1570 const char *target_mangling
1571 = targetm.mangle_fundamental_type (type);
1573 if (target_mangling)
1575 write_string (target_mangling);
1579 /* If this is a typedef, TYPE may not be one of
1580 the standard builtin type nodes, but an alias of one. Use
1581 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1582 write_builtin_type (TYPE_MAIN_VARIANT (type));
1589 write_type (TREE_TYPE (type));
1594 write_function_type (type);
1600 /* A pointer-to-member function is represented as a special
1601 RECORD_TYPE, so check for this first. */
1602 if (TYPE_PTRMEMFUNC_P (type))
1603 write_pointer_to_member_type (type);
1605 write_class_enum_type (type);
1609 case UNBOUND_CLASS_TEMPLATE:
1610 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1611 ordinary nested names. */
1612 write_nested_name (TYPE_STUB_DECL (type));
1617 write_type (TREE_TYPE (type));
1620 case REFERENCE_TYPE:
1622 write_type (TREE_TYPE (type));
1625 case TEMPLATE_TYPE_PARM:
1626 case TEMPLATE_PARM_INDEX:
1627 write_template_param (type);
1630 case TEMPLATE_TEMPLATE_PARM:
1631 write_template_template_param (type);
1634 case BOUND_TEMPLATE_TEMPLATE_PARM:
1635 write_template_template_param (type);
1637 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1641 write_string ("U8__vector");
1642 write_type (TREE_TYPE (type));
1650 /* Types other than builtin types are substitution candidates. */
1651 if (!is_builtin_type)
1652 add_substitution (type);
1655 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1656 CV-qualifiers written for TYPE.
1658 <CV-qualifiers> ::= [r] [V] [K] */
1661 write_CV_qualifiers_for_type (const tree type)
1663 int num_qualifiers = 0;
1665 /* The order is specified by:
1667 "In cases where multiple order-insensitive qualifiers are
1668 present, they should be ordered 'K' (closest to the base type),
1669 'V', 'r', and 'U' (farthest from the base type) ..."
1671 Note that we do not use cp_type_quals below; given "const
1672 int[3]", the "const" is emitted with the "int", not with the
1675 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1680 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1685 if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1691 return num_qualifiers;
1694 /* Non-terminal <builtin-type>.
1696 <builtin-type> ::= v # void
1701 ::= h # unsigned char
1703 ::= t # unsigned short
1705 ::= j # unsigned int
1707 ::= m # unsigned long
1708 ::= x # long long, __int64
1709 ::= y # unsigned long long, __int64
1711 ::= o # unsigned __int128
1714 ::= e # long double, __float80
1715 ::= g # __float128 [not supported]
1716 ::= u <source-name> # vendor extended type */
1719 write_builtin_type (tree type)
1721 switch (TREE_CODE (type))
1732 /* If this is size_t, get the underlying int type. */
1733 if (TYPE_IS_SIZETYPE (type))
1734 type = TYPE_DOMAIN (type);
1736 /* TYPE may still be wchar_t, since that isn't in
1737 integer_type_nodes. */
1738 if (type == wchar_type_node)
1740 else if (TYPE_FOR_JAVA (type))
1741 write_java_integer_type_codes (type);
1745 /* Assume TYPE is one of the shared integer type nodes. Find
1746 it in the array of these nodes. */
1748 for (itk = 0; itk < itk_none; ++itk)
1749 if (type == integer_types[itk])
1751 /* Print the corresponding single-letter code. */
1752 write_char (integer_type_codes[itk]);
1756 if (itk == itk_none)
1758 tree t = c_common_type_for_mode (TYPE_MODE (type),
1759 TYPE_UNSIGNED (type));
1766 if (TYPE_PRECISION (type) == 128)
1767 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
1770 /* Allow for cases where TYPE is not one of the shared
1771 integer type nodes and write a "vendor extended builtin
1772 type" with a name the form intN or uintN, respectively.
1773 Situations like this can happen if you have an
1774 __attribute__((__mode__(__SI__))) type and use exotic
1775 switches like '-mint64' on MIPS or '-mint8' on AVR.
1776 Of course, this is undefined by the C++ ABI (and
1777 '-mint8' is not even Standard C conforming), but when
1778 using such special options you're pretty much in nowhere
1781 char prec[11]; /* up to ten digits for an unsigned */
1783 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
1784 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
1785 write_char ('u'); /* "vendor extended builtin type" */
1786 write_unsigned_number (strlen (prefix) + strlen (prec));
1787 write_string (prefix);
1788 write_string (prec);
1795 if (type == float_type_node
1796 || type == java_float_type_node)
1798 else if (type == double_type_node
1799 || type == java_double_type_node)
1801 else if (type == long_double_type_node)
1812 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1813 METHOD_TYPE. The return type is mangled before the parameter
1816 <function-type> ::= F [Y] <bare-function-type> E */
1819 write_function_type (const tree type)
1821 MANGLE_TRACE_TREE ("function-type", type);
1823 /* For a pointer to member function, the function type may have
1824 cv-qualifiers, indicating the quals for the artificial 'this'
1826 if (TREE_CODE (type) == METHOD_TYPE)
1828 /* The first parameter must be a POINTER_TYPE pointing to the
1829 `this' parameter. */
1830 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1831 write_CV_qualifiers_for_type (this_type);
1835 /* We don't track whether or not a type is `extern "C"'. Note that
1836 you can have an `extern "C"' function that does not have
1837 `extern "C"' type, and vice versa:
1839 extern "C" typedef void function_t();
1840 function_t f; // f has C++ linkage, but its type is
1843 typedef void function_t();
1844 extern "C" function_t f; // Vice versa.
1847 write_bare_function_type (type, /*include_return_type_p=*/1,
1852 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1853 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
1854 is mangled before the parameter types. If non-NULL, DECL is
1855 FUNCTION_DECL for the function whose type is being emitted.
1857 <bare-function-type> ::= </signature/ type>+ */
1860 write_bare_function_type (const tree type, const int include_return_type_p,
1863 MANGLE_TRACE_TREE ("bare-function-type", type);
1865 /* Mangle the return type, if requested. */
1866 if (include_return_type_p)
1867 write_type (TREE_TYPE (type));
1869 /* Now mangle the types of the arguments. */
1870 write_method_parms (TYPE_ARG_TYPES (type),
1871 TREE_CODE (type) == METHOD_TYPE,
1875 /* Write the mangled representation of a method parameter list of
1876 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
1877 considered a non-static method, and the this parameter is omitted.
1878 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1879 parameters are being emitted. */
1882 write_method_parms (tree parm_types, const int method_p, const tree decl)
1884 tree first_parm_type;
1885 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1887 /* Assume this parameter type list is variable-length. If it ends
1888 with a void type, then it's not. */
1891 /* If this is a member function, skip the first arg, which is the
1893 "Member functions do not encode the type of their implicit this
1896 Similarly, there's no need to mangle artificial parameters, like
1897 the VTT parameters for constructors and destructors. */
1900 parm_types = TREE_CHAIN (parm_types);
1901 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1903 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1905 parm_types = TREE_CHAIN (parm_types);
1906 parm_decl = TREE_CHAIN (parm_decl);
1910 for (first_parm_type = parm_types;
1912 parm_types = TREE_CHAIN (parm_types))
1914 tree parm = TREE_VALUE (parm_types);
1915 if (parm == void_type_node)
1917 /* "Empty parameter lists, whether declared as () or
1918 conventionally as (void), are encoded with a void parameter
1920 if (parm_types == first_parm_type)
1922 /* If the parm list is terminated with a void type, it's
1925 /* A void type better be the last one. */
1926 gcc_assert (TREE_CHAIN (parm_types) == NULL);
1933 /* <builtin-type> ::= z # ellipsis */
1937 /* <class-enum-type> ::= <name> */
1940 write_class_enum_type (const tree type)
1942 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1945 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1948 <template-args> ::= I <template-arg>+ E */
1951 write_template_args (tree args)
1954 int length = TREE_VEC_LENGTH (args);
1956 MANGLE_TRACE_TREE ("template-args", args);
1960 gcc_assert (length > 0);
1962 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1964 /* We have nested template args. We want the innermost template
1966 args = TREE_VEC_ELT (args, length - 1);
1967 length = TREE_VEC_LENGTH (args);
1969 for (i = 0; i < length; ++i)
1970 write_template_arg (TREE_VEC_ELT (args, i));
1975 /* <expression> ::= <unary operator-name> <expression>
1976 ::= <binary operator-name> <expression> <expression>
1979 <expr-primary> ::= <template-param>
1980 ::= L <type> <value number> E # literal
1981 ::= L <mangled-name> E # external name
1982 ::= sr <type> <unqualified-name>
1983 ::= sr <type> <unqualified-name> <template-args> */
1986 write_expression (tree expr)
1988 enum tree_code code;
1990 code = TREE_CODE (expr);
1992 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1993 is converted (via qualification conversions) to another
1995 while (TREE_CODE (expr) == NOP_EXPR
1996 || TREE_CODE (expr) == NON_LVALUE_EXPR)
1998 expr = TREE_OPERAND (expr, 0);
1999 code = TREE_CODE (expr);
2002 /* Handle pointers-to-members by making them look like expression
2004 if (code == PTRMEM_CST)
2006 expr = build_nt (ADDR_EXPR,
2007 build_nt (SCOPE_REF,
2008 PTRMEM_CST_CLASS (expr),
2009 PTRMEM_CST_MEMBER (expr)));
2010 code = TREE_CODE (expr);
2013 /* Handle template parameters. */
2014 if (code == TEMPLATE_TYPE_PARM
2015 || code == TEMPLATE_TEMPLATE_PARM
2016 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2017 || code == TEMPLATE_PARM_INDEX)
2018 write_template_param (expr);
2019 /* Handle literals. */
2020 else if (TREE_CODE_CLASS (code) == tcc_constant
2021 || (abi_version_at_least (2) && code == CONST_DECL))
2022 write_template_arg_literal (expr);
2023 else if (DECL_P (expr))
2025 /* G++ 3.2 incorrectly mangled non-type template arguments of
2026 enumeration type using their names. */
2027 if (code == CONST_DECL)
2028 G.need_abi_warning = 1;
2030 write_mangled_name (expr, false);
2033 else if (TREE_CODE (expr) == SIZEOF_EXPR
2034 && TYPE_P (TREE_OPERAND (expr, 0)))
2036 write_string ("st");
2037 write_type (TREE_OPERAND (expr, 0));
2039 else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
2041 tree scope = TREE_OPERAND (expr, 0);
2042 tree member = TREE_OPERAND (expr, 1);
2044 /* If the MEMBER is a real declaration, then the qualifying
2045 scope was not dependent. Ideally, we would not have a
2046 SCOPE_REF in those cases, but sometimes we do. If the second
2047 argument is a DECL, then the name must not have been
2049 if (DECL_P (member))
2050 write_expression (member);
2055 write_string ("sr");
2057 /* If MEMBER is a template-id, separate the template
2058 from the arguments. */
2059 if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2061 template_args = TREE_OPERAND (member, 1);
2062 member = TREE_OPERAND (member, 0);
2065 template_args = NULL_TREE;
2066 /* Write out the name of the MEMBER. */
2067 if (IDENTIFIER_TYPENAME_P (member))
2068 write_conversion_operator_name (TREE_TYPE (member));
2069 else if (IDENTIFIER_OPNAME_P (member))
2072 const char *mangled_name = NULL;
2074 /* Unfortunately, there is no easy way to go from the
2075 name of the operator back to the corresponding tree
2077 for (i = 0; i < LAST_CPLUS_TREE_CODE; ++i)
2078 if (operator_name_info[i].identifier == member)
2080 /* The ABI says that we prefer binary operator
2081 names to unary operator names. */
2082 if (operator_name_info[i].arity == 2)
2084 mangled_name = operator_name_info[i].mangled_name;
2087 else if (!mangled_name)
2088 mangled_name = operator_name_info[i].mangled_name;
2090 else if (assignment_operator_name_info[i].identifier
2094 = assignment_operator_name_info[i].mangled_name;
2097 write_string (mangled_name);
2100 write_source_name (member);
2101 /* Write out the template arguments. */
2103 write_template_args (template_args);
2110 /* When we bind a variable or function to a non-type template
2111 argument with reference type, we create an ADDR_EXPR to show
2112 the fact that the entity's address has been taken. But, we
2113 don't actually want to output a mangling code for the `&'. */
2114 if (TREE_CODE (expr) == ADDR_EXPR
2116 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2118 expr = TREE_OPERAND (expr, 0);
2121 write_expression (expr);
2125 code = TREE_CODE (expr);
2128 /* If it wasn't any of those, recursively expand the expression. */
2129 write_string (operator_name_info[(int) code].mangled_name);
2134 sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
2138 write_type (TREE_TYPE (expr));
2139 /* There is no way to mangle a zero-operand cast like
2141 if (!TREE_OPERAND (expr, 0))
2142 sorry ("zero-operand casts cannot be mangled due to a defect "
2145 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2148 case STATIC_CAST_EXPR:
2149 case CONST_CAST_EXPR:
2150 write_type (TREE_TYPE (expr));
2151 write_expression (TREE_OPERAND (expr, 0));
2155 /* Handle pointers-to-members specially. */
2157 write_type (TREE_OPERAND (expr, 0));
2158 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
2159 write_source_name (TREE_OPERAND (expr, 1));
2160 else if (TREE_CODE (TREE_OPERAND (expr, 1)) == TEMPLATE_ID_EXPR)
2165 template_id = TREE_OPERAND (expr, 1);
2166 name = TREE_OPERAND (template_id, 0);
2167 /* FIXME: What about operators? */
2168 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2169 write_source_name (TREE_OPERAND (template_id, 0));
2170 write_template_args (TREE_OPERAND (template_id, 1));
2174 /* G++ 3.2 incorrectly put out both the "sr" code and
2175 the nested name of the qualified name. */
2176 G.need_abi_warning = 1;
2177 write_encoding (TREE_OPERAND (expr, 1));
2182 for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
2184 tree operand = TREE_OPERAND (expr, i);
2185 /* As a GNU expression, the middle operand of a
2186 conditional may be omitted. Since expression
2187 manglings are supposed to represent the input token
2188 stream, there's no good way to mangle such an
2189 expression without extending the C++ ABI. */
2190 if (code == COND_EXPR && i == 1 && !operand)
2192 error ("omitted middle operand to %<?:%> operand "
2193 "cannot be mangled");
2196 write_expression (operand);
2202 /* Literal subcase of non-terminal <template-arg>.
2204 "Literal arguments, e.g. "A<42L>", are encoded with their type
2205 and value. Negative integer values are preceded with "n"; for
2206 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2207 encoded as 0, true as 1." */
2210 write_template_arg_literal (const tree value)
2213 write_type (TREE_TYPE (value));
2215 switch (TREE_CODE (value))
2218 write_integer_cst (DECL_INITIAL (value));
2222 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
2223 || integer_zerop (value) || integer_onep (value));
2224 write_integer_cst (value);
2228 write_real_cst (value);
2238 /* Non-terminal <template-arg>.
2240 <template-arg> ::= <type> # type
2241 ::= L <type> </value/ number> E # literal
2242 ::= LZ <name> E # external name
2243 ::= X <expression> E # expression */
2246 write_template_arg (tree node)
2248 enum tree_code code = TREE_CODE (node);
2250 MANGLE_TRACE_TREE ("template-arg", node);
2252 /* A template template parameter's argument list contains TREE_LIST
2253 nodes of which the value field is the actual argument. */
2254 if (code == TREE_LIST)
2256 node = TREE_VALUE (node);
2257 /* If it's a decl, deal with its type instead. */
2260 node = TREE_TYPE (node);
2261 code = TREE_CODE (node);
2265 if (TREE_CODE (node) == NOP_EXPR
2266 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
2268 /* Template parameters can be of reference type. To maintain
2269 internal consistency, such arguments use a conversion from
2270 address of object to reference type. */
2271 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
2272 if (abi_version_at_least (2))
2273 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
2275 G.need_abi_warning = 1;
2280 else if (code == TEMPLATE_DECL)
2281 /* A template appearing as a template arg is a template template arg. */
2282 write_template_template_arg (node);
2283 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
2284 || (abi_version_at_least (2) && code == CONST_DECL))
2285 write_template_arg_literal (node);
2286 else if (DECL_P (node))
2288 /* Until ABI version 2, non-type template arguments of
2289 enumeration type were mangled using their names. */
2290 if (code == CONST_DECL && !abi_version_at_least (2))
2291 G.need_abi_warning = 1;
2293 /* Until ABI version 3, the underscore before the mangled name
2294 was incorrectly omitted. */
2295 if (!abi_version_at_least (3))
2297 G.need_abi_warning = 1;
2301 write_string ("_Z");
2302 write_encoding (node);
2307 /* Template arguments may be expressions. */
2309 write_expression (node);
2314 /* <template-template-arg>
2316 ::= <substitution> */
2319 write_template_template_arg (const tree decl)
2321 MANGLE_TRACE_TREE ("template-template-arg", decl);
2323 if (find_substitution (decl))
2325 write_name (decl, /*ignore_local_scope=*/0);
2326 add_substitution (decl);
2330 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
2332 <array-type> ::= A [</dimension/ number>] _ </element/ type>
2333 ::= A <expression> _ </element/ type>
2335 "Array types encode the dimension (number of elements) and the
2336 element type. For variable length arrays, the dimension (but not
2337 the '_' separator) is omitted." */
2340 write_array_type (const tree type)
2343 if (TYPE_DOMAIN (type))
2348 index_type = TYPE_DOMAIN (type);
2349 /* The INDEX_TYPE gives the upper and lower bounds of the
2351 max = TYPE_MAX_VALUE (index_type);
2352 if (TREE_CODE (max) == INTEGER_CST)
2354 /* The ABI specifies that we should mangle the number of
2355 elements in the array, not the largest allowed index. */
2356 max = size_binop (PLUS_EXPR, max, size_one_node);
2357 write_unsigned_number (tree_low_cst (max, 1));
2361 max = TREE_OPERAND (max, 0);
2362 if (!abi_version_at_least (2))
2364 /* value_dependent_expression_p presumes nothing is
2365 dependent when PROCESSING_TEMPLATE_DECL is zero. */
2366 ++processing_template_decl;
2367 if (!value_dependent_expression_p (max))
2368 G.need_abi_warning = 1;
2369 --processing_template_decl;
2371 write_expression (max);
2376 write_type (TREE_TYPE (type));
2379 /* Non-terminal <pointer-to-member-type> for pointer-to-member
2380 variables. TYPE is a pointer-to-member POINTER_TYPE.
2382 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2385 write_pointer_to_member_type (const tree type)
2388 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
2389 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2392 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
2393 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2394 TEMPLATE_PARM_INDEX.
2396 <template-param> ::= T </parameter/ number> _ */
2399 write_template_param (const tree parm)
2403 tree parm_type = NULL_TREE;
2405 MANGLE_TRACE_TREE ("template-parm", parm);
2407 switch (TREE_CODE (parm))
2409 case TEMPLATE_TYPE_PARM:
2410 case TEMPLATE_TEMPLATE_PARM:
2411 case BOUND_TEMPLATE_TEMPLATE_PARM:
2412 parm_index = TEMPLATE_TYPE_IDX (parm);
2413 parm_level = TEMPLATE_TYPE_LEVEL (parm);
2416 case TEMPLATE_PARM_INDEX:
2417 parm_index = TEMPLATE_PARM_IDX (parm);
2418 parm_level = TEMPLATE_PARM_LEVEL (parm);
2419 parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
2427 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2428 earliest template param denoted by `_'. */
2430 write_unsigned_number (parm_index - 1);
2434 /* <template-template-param>
2435 ::= <template-param>
2436 ::= <substitution> */
2439 write_template_template_param (const tree parm)
2441 tree template = NULL_TREE;
2443 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2444 template template parameter. The substitution candidate here is
2445 only the template. */
2446 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2449 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2450 if (find_substitution (template))
2454 /* <template-param> encodes only the template parameter position,
2455 not its template arguments, which is fine here. */
2456 write_template_param (parm);
2458 add_substitution (template);
2461 /* Non-terminal <substitution>.
2463 <substitution> ::= S <seq-id> _
2467 write_substitution (const int seq_id)
2469 MANGLE_TRACE ("substitution", "");
2473 write_number (seq_id - 1, /*unsigned=*/1, 36);
2477 /* Start mangling ENTITY. */
2480 start_mangling (const tree entity, const bool ident_p)
2483 G.need_abi_warning = false;
2486 obstack_free (&name_obstack, name_base);
2487 mangle_obstack = &name_obstack;
2488 name_base = obstack_alloc (&name_obstack, 0);
2491 mangle_obstack = &ident_hash->stack;
2494 /* Done with mangling. Return the generated mangled name. If WARN is
2495 true, and the name of G.entity will be mangled differently in a
2496 future version of the ABI, issue a warning. */
2498 static inline const char *
2499 finish_mangling (const bool warn)
2501 if (warn_abi && warn && G.need_abi_warning)
2502 warning (0, "the mangled name of %qD will change in a future "
2506 /* Clear all the substitutions. */
2507 VARRAY_CLEAR (G.substitutions);
2509 /* Null-terminate the string. */
2512 return (const char *) obstack_finish (mangle_obstack);
2515 /* Initialize data structures for mangling. */
2520 gcc_obstack_init (&name_obstack);
2521 name_base = obstack_alloc (&name_obstack, 0);
2522 VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
2524 /* Cache these identifiers for quick comparison when checking for
2525 standard substitutions. */
2526 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2527 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2528 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2529 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2530 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2531 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2534 /* Generate the mangled name of DECL. */
2537 mangle_decl_string (const tree decl)
2541 start_mangling (decl, /*ident_p=*/true);
2543 if (TREE_CODE (decl) == TYPE_DECL)
2544 write_type (TREE_TYPE (decl));
2546 write_mangled_name (decl, true);
2548 result = finish_mangling (/*warn=*/true);
2550 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2554 /* Like get_identifier, except that NAME is assumed to have been
2555 allocated on the obstack used by the identifier hash table. */
2558 get_identifier_nocopy (const char *name)
2560 hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name,
2561 strlen (name), HT_ALLOCED);
2562 return HT_IDENT_TO_GCC_IDENT (ht_node);
2565 /* Create an identifier for the external mangled name of DECL. */
2568 mangle_decl (const tree decl)
2570 SET_DECL_ASSEMBLER_NAME (decl,
2571 get_identifier_nocopy (mangle_decl_string (decl)));
2574 /* Generate the mangled representation of TYPE. */
2577 mangle_type_string (const tree type)
2581 start_mangling (type, /*ident_p=*/false);
2583 result = finish_mangling (/*warn=*/false);
2585 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2589 /* Create an identifier for the mangled name of a special component
2590 for belonging to TYPE. CODE is the ABI-specified code for this
2594 mangle_special_for_type (const tree type, const char *code)
2598 /* We don't have an actual decl here for the special component, so
2599 we can't just process the <encoded-name>. Instead, fake it. */
2600 start_mangling (type, /*ident_p=*/true);
2602 /* Start the mangling. */
2603 write_string ("_Z");
2604 write_string (code);
2608 result = finish_mangling (/*warn=*/false);
2611 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2613 return get_identifier_nocopy (result);
2616 /* Create an identifier for the mangled representation of the typeinfo
2617 structure for TYPE. */
2620 mangle_typeinfo_for_type (const tree type)
2622 return mangle_special_for_type (type, "TI");
2625 /* Create an identifier for the mangled name of the NTBS containing
2626 the mangled name of TYPE. */
2629 mangle_typeinfo_string_for_type (const tree type)
2631 return mangle_special_for_type (type, "TS");
2634 /* Create an identifier for the mangled name of the vtable for TYPE. */
2637 mangle_vtbl_for_type (const tree type)
2639 return mangle_special_for_type (type, "TV");
2642 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2645 mangle_vtt_for_type (const tree type)
2647 return mangle_special_for_type (type, "TT");
2650 /* Return an identifier for a construction vtable group. TYPE is
2651 the most derived class in the hierarchy; BINFO is the base
2652 subobject for which this construction vtable group will be used.
2654 This mangling isn't part of the ABI specification; in the ABI
2655 specification, the vtable group is dumped in the same COMDAT as the
2656 main vtable, and is referenced only from that vtable, so it doesn't
2657 need an external name. For binary formats without COMDAT sections,
2658 though, we need external names for the vtable groups.
2660 We use the production
2662 <special-name> ::= CT <type> <offset number> _ <base type> */
2665 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
2669 start_mangling (type, /*ident_p=*/true);
2671 write_string ("_Z");
2672 write_string ("TC");
2674 write_integer_cst (BINFO_OFFSET (binfo));
2676 write_type (BINFO_TYPE (binfo));
2678 result = finish_mangling (/*warn=*/false);
2680 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2681 return get_identifier_nocopy (result);
2684 /* Mangle a this pointer or result pointer adjustment.
2686 <call-offset> ::= h <fixed offset number> _
2687 ::= v <fixed offset number> _ <virtual offset number> _ */
2690 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
2692 write_char (virtual_offset ? 'v' : 'h');
2694 /* For either flavor, write the fixed offset. */
2695 write_integer_cst (fixed_offset);
2698 /* For a virtual thunk, add the virtual offset. */
2701 write_integer_cst (virtual_offset);
2706 /* Return an identifier for the mangled name of a this-adjusting or
2707 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
2708 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
2709 is a virtual thunk, and it is the vtbl offset in
2710 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
2711 zero for a covariant thunk. Note, that FN_DECL might be a covariant
2712 thunk itself. A covariant thunk name always includes the adjustment
2713 for the this pointer, even if there is none.
2715 <special-name> ::= T <call-offset> <base encoding>
2716 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2721 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
2722 tree virtual_offset)
2726 start_mangling (fn_decl, /*ident_p=*/true);
2728 write_string ("_Z");
2731 if (!this_adjusting)
2733 /* Covariant thunk with no this adjustment */
2735 mangle_call_offset (integer_zero_node, NULL_TREE);
2736 mangle_call_offset (fixed_offset, virtual_offset);
2738 else if (!DECL_THUNK_P (fn_decl))
2739 /* Plain this adjusting thunk. */
2740 mangle_call_offset (fixed_offset, virtual_offset);
2743 /* This adjusting thunk to covariant thunk. */
2745 mangle_call_offset (fixed_offset, virtual_offset);
2746 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2747 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2749 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2750 mangle_call_offset (fixed_offset, virtual_offset);
2751 fn_decl = THUNK_TARGET (fn_decl);
2755 write_encoding (fn_decl);
2757 result = finish_mangling (/*warn=*/false);
2759 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2760 return get_identifier_nocopy (result);
2763 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
2764 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
2767 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
2769 /* Hash a node (VAL1) in the table. */
2772 hash_type (const void *val)
2774 return (hashval_t) TYPE_UID (TREE_TYPE ((tree) val));
2777 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
2780 compare_type (const void *val1, const void *val2)
2782 return TREE_TYPE ((tree) val1) == (tree) val2;
2785 /* Return an identifier for the mangled unqualified name for a
2786 conversion operator to TYPE. This mangling is not specified by the
2787 ABI spec; it is only used internally. */
2790 mangle_conv_op_name_for_type (const tree type)
2795 if (conv_type_names == NULL)
2796 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
2798 slot = htab_find_slot_with_hash (conv_type_names, type,
2799 (hashval_t) TYPE_UID (type), INSERT);
2800 identifier = (tree)*slot;
2805 /* Create a unique name corresponding to TYPE. */
2806 sprintf (buffer, "operator %lu",
2807 (unsigned long) htab_elements (conv_type_names));
2808 identifier = get_identifier (buffer);
2811 /* Hang TYPE off the identifier so it can be found easily later
2812 when performing conversions. */
2813 TREE_TYPE (identifier) = type;
2815 /* Set bits on the identifier so we know later it's a conversion. */
2816 IDENTIFIER_OPNAME_P (identifier) = 1;
2817 IDENTIFIER_TYPENAME_P (identifier) = 1;
2823 /* Return an identifier for the name of an initialization guard
2824 variable for indicated VARIABLE. */
2827 mangle_guard_variable (const tree variable)
2829 start_mangling (variable, /*ident_p=*/true);
2830 write_string ("_ZGV");
2831 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
2832 /* The name of a guard variable for a reference temporary should refer
2833 to the reference, not the temporary. */
2834 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
2836 write_name (variable, /*ignore_local_scope=*/0);
2837 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2840 /* Return an identifier for the name of a temporary variable used to
2841 initialize a static reference. This isn't part of the ABI, but we might
2842 as well call them something readable. */
2845 mangle_ref_init_variable (const tree variable)
2847 start_mangling (variable, /*ident_p=*/true);
2848 write_string ("_ZGR");
2849 write_name (variable, /*ignore_local_scope=*/0);
2850 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2854 /* Foreign language type mangling section. */
2856 /* How to write the type codes for the integer Java type. */
2859 write_java_integer_type_codes (const tree type)
2861 if (type == java_int_type_node)
2863 else if (type == java_short_type_node)
2865 else if (type == java_byte_type_node)
2867 else if (type == java_char_type_node)
2869 else if (type == java_long_type_node)
2871 else if (type == java_boolean_type_node)
2877 #include "gt-cp-mangle.h"