1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Alex Samuel <sameul@codesourcery.com>
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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
59 /* Debugging support. */
61 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
63 #define DEBUG_MANGLE 0
66 /* Macros for tracing the write_* functions. */
68 # define MANGLE_TRACE(FN, INPUT) \
69 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
70 # define MANGLE_TRACE_TREE(FN, NODE) \
71 fprintf (stderr, " %-24s: %-24s (%p)\n", \
72 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
74 # define MANGLE_TRACE(FN, INPUT)
75 # define MANGLE_TRACE_TREE(FN, NODE)
78 /* Nonzero if NODE is a class template-id. We can't rely on
79 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
80 that hard to distinguish A<T> from A, where A<T> is the type as
81 instantiated outside of the template, and A is the type used
82 without parameters inside the template. */
83 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
84 (TYPE_LANG_SPECIFIC (NODE) != NULL \
85 && CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
86 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))
88 /* Things we only need one of. This module is not reentrant. */
91 /* The name in which we're building the mangled name. */
92 struct obstack name_obstack;
94 /* An array of the current substitution candidates, in the order
96 varray_type substitutions;
98 /* We are mangling an internal symbol. It is important to keep those
99 involving template parmeters distinct by distinguishing their level
100 and, for non-type parms, their type. */
101 bool internal_mangling_p;
104 /* Indices into subst_identifiers. These are identifiers used in
105 special substitution rules. */
113 SUBID_BASIC_IOSTREAM,
116 substitution_identifier_index_t;
118 /* For quick substitution checks, look up these common identifiers
120 static tree subst_identifiers[SUBID_MAX];
122 /* Single-letter codes for builtin integer types, defined in
123 <builtin-type>. These are indexed by integer_type_kind values. */
125 integer_type_codes[itk_none] =
128 'a', /* itk_signed_char */
129 'h', /* itk_unsigned_char */
131 't', /* itk_unsigned_short */
133 'j', /* itk_unsigned_int */
135 'm', /* itk_unsigned_long */
136 'x', /* itk_long_long */
137 'y' /* itk_unsigned_long_long */
140 static int decl_is_template_id PARAMS ((tree, tree*));
142 /* Functions for handling substitutions. */
144 static inline tree canonicalize_for_substitution PARAMS ((tree));
145 static void add_substitution PARAMS ((tree));
146 static inline int is_std_substitution PARAMS ((tree, substitution_identifier_index_t));
147 static inline int is_std_substitution_char PARAMS ((tree, substitution_identifier_index_t));
148 static int find_substitution PARAMS ((tree));
150 /* Functions for emitting mangled representations of things. */
152 static void write_mangled_name PARAMS ((tree));
153 static void write_encoding PARAMS ((tree));
154 static void write_name PARAMS ((tree, int));
155 static void write_unscoped_name PARAMS ((tree));
156 static void write_unscoped_template_name PARAMS ((tree));
157 static void write_nested_name PARAMS ((tree));
158 static void write_prefix PARAMS ((tree));
159 static void write_template_prefix PARAMS ((tree));
160 static void write_unqualified_name PARAMS ((tree));
161 static void write_source_name PARAMS ((tree));
162 static int hwint_to_ascii PARAMS ((unsigned HOST_WIDE_INT, unsigned int, char *, unsigned));
163 static void write_number PARAMS ((unsigned HOST_WIDE_INT, int,
165 static void write_integer_cst PARAMS ((tree));
166 static void write_identifier PARAMS ((const char *));
167 static void write_special_name_constructor PARAMS ((tree));
168 static void write_special_name_destructor PARAMS ((tree));
169 static void write_type PARAMS ((tree));
170 static int write_CV_qualifiers_for_type PARAMS ((tree));
171 static void write_builtin_type PARAMS ((tree));
172 static void write_function_type PARAMS ((tree));
173 static void write_bare_function_type PARAMS ((tree, int, tree));
174 static void write_method_parms PARAMS ((tree, int, tree));
175 static void write_class_enum_type PARAMS ((tree));
176 static void write_template_args PARAMS ((tree));
177 static void write_expression PARAMS ((tree));
178 static void write_template_arg_literal PARAMS ((tree));
179 static void write_template_arg PARAMS ((tree));
180 static void write_template_template_arg PARAMS ((tree));
181 static void write_array_type PARAMS ((tree));
182 static void write_pointer_to_member_type PARAMS ((tree));
183 static void write_template_param PARAMS ((tree));
184 static void write_template_template_param PARAMS ((tree));
185 static void write_substitution PARAMS ((int));
186 static int discriminator_for_local_entity PARAMS ((tree));
187 static int discriminator_for_string_literal PARAMS ((tree, tree));
188 static void write_discriminator PARAMS ((int));
189 static void write_local_name PARAMS ((tree, tree, tree));
190 static void dump_substitution_candidates PARAMS ((void));
191 static const char *mangle_decl_string PARAMS ((tree));
193 /* Control functions. */
195 static inline void start_mangling PARAMS ((void));
196 static inline const char *finish_mangling PARAMS ((void));
197 static tree mangle_special_for_type PARAMS ((tree, const char *));
199 /* Foreign language functions. */
201 static void write_java_integer_type_codes PARAMS ((tree));
203 /* Append a single character to the end of the mangled
205 #define write_char(CHAR) \
206 obstack_1grow (&G.name_obstack, (CHAR))
208 /* Append a sized buffer to the end of the mangled representation. */
209 #define write_chars(CHAR, LEN) \
210 obstack_grow (&G.name_obstack, (CHAR), (LEN))
212 /* Append a NUL-terminated string to the end of the mangled
214 #define write_string(STRING) \
215 obstack_grow (&G.name_obstack, (STRING), strlen (STRING))
217 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
218 same purpose (context, which may be a type) and value (template
219 decl). See write_template_prefix for more information on what this
221 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
222 (TREE_CODE (NODE1) == TREE_LIST \
223 && TREE_CODE (NODE2) == TREE_LIST \
224 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
225 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
226 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
227 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
229 /* Write out an unsigned quantity in base 10. */
230 #define write_unsigned_number(NUMBER) \
231 write_number ((NUMBER), /*unsigned_p=*/1, 10)
233 /* If DECL is a template instance, return nonzero and, if
234 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
235 Otherwise return zero. */
238 decl_is_template_id (decl, template_info)
242 if (TREE_CODE (decl) == TYPE_DECL)
244 /* TYPE_DECLs are handled specially. Look at its type to decide
245 if this is a template instantiation. */
246 tree type = TREE_TYPE (decl);
248 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
250 if (template_info != NULL)
251 /* For a templated TYPE_DECL, the template info is hanging
253 *template_info = CLASSTYPE_TEMPLATE_INFO (type);
259 /* Check if this is a primary template. */
260 if (DECL_LANG_SPECIFIC (decl) != NULL
261 && DECL_USE_TEMPLATE (decl)
262 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
263 && TREE_CODE (decl) != TEMPLATE_DECL)
265 if (template_info != NULL)
266 /* For most templated decls, the template info is hanging
268 *template_info = DECL_TEMPLATE_INFO (decl);
273 /* It's not a template id. */
277 /* Produce debugging output of current substitution candidates. */
280 dump_substitution_candidates ()
284 fprintf (stderr, " ++ substitutions ");
285 for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
287 tree el = VARRAY_TREE (G.substitutions, i);
288 const char *name = "???";
291 fprintf (stderr, " ");
293 name = IDENTIFIER_POINTER (DECL_NAME (el));
294 else if (TREE_CODE (el) == TREE_LIST)
295 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
296 else if (TYPE_NAME (el))
297 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
298 fprintf (stderr, " S%d_ = ", i - 1);
300 (CP_TYPE_RESTRICT_P (el)
301 || CP_TYPE_VOLATILE_P (el)
302 || CP_TYPE_CONST_P (el)))
303 fprintf (stderr, "CV-");
304 fprintf (stderr, "%s (%s at %p)\n",
305 name, tree_code_name[TREE_CODE (el)], (void *) el);
309 /* Both decls and types can be substitution candidates, but sometimes
310 they refer to the same thing. For instance, a TYPE_DECL and
311 RECORD_TYPE for the same class refer to the same thing, and should
312 be treated accordinginly in substitutions. This function returns a
313 canonicalized tree node representing NODE that is used when adding
314 and substitution candidates and finding matches. */
317 canonicalize_for_substitution (node)
320 /* For a TYPE_DECL, use the type instead. */
321 if (TREE_CODE (node) == TYPE_DECL)
322 node = TREE_TYPE (node);
324 node = canonical_type_variant (node);
329 /* Add NODE as a substitution candidate. NODE must not already be on
330 the list of candidates. */
333 add_substitution (node)
339 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
340 tree_code_name[TREE_CODE (node)], (void *) node);
342 /* Get the canonicalized substitution candidate for NODE. */
343 c = canonicalize_for_substitution (node);
344 if (DEBUG_MANGLE && c != node)
345 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
346 tree_code_name[TREE_CODE (node)], (void *) node);
350 /* Make sure NODE isn't already a candidate. */
353 for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
355 tree candidate = VARRAY_TREE (G.substitutions, i);
357 && node == candidate)
359 && TYPE_P (candidate)
360 && same_type_p (node, candidate)))
364 #endif /* ENABLE_CHECKING */
366 /* Put the decl onto the varray of substitution candidates. */
367 VARRAY_PUSH_TREE (G.substitutions, node);
370 dump_substitution_candidates ();
373 /* Helper function for find_substitution. Returns nonzero if NODE,
374 which may be a decl or a CLASS_TYPE, is a template-id with template
375 name of substitution_index[INDEX] in the ::std namespace. */
378 is_std_substitution (node, index)
380 substitution_identifier_index_t index;
387 type = TREE_TYPE (node);
390 else if (CLASS_TYPE_P (node))
393 decl = TYPE_NAME (node);
396 /* These are not the droids you're looking for. */
399 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
400 && TYPE_LANG_SPECIFIC (type)
401 && CLASSTYPE_TEMPLATE_INFO (type)
402 && (DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
403 == subst_identifiers[index]));
406 /* Helper function for find_substitution. Returns nonzero if NODE,
407 which may be a decl or a CLASS_TYPE, is the template-id
408 ::std::identifier<char>, where identifier is
409 substitution_index[INDEX]. */
412 is_std_substitution_char (node, index)
414 substitution_identifier_index_t index;
417 /* Check NODE's name is ::std::identifier. */
418 if (!is_std_substitution (node, index))
420 /* Figure out its template args. */
422 args = DECL_TI_ARGS (node);
423 else if (CLASS_TYPE_P (node))
424 args = CLASSTYPE_TI_ARGS (node);
426 /* Oops, not a template. */
428 /* NODE's template arg list should be <char>. */
430 TREE_VEC_LENGTH (args) == 1
431 && TREE_VEC_ELT (args, 0) == char_type_node;
434 /* Check whether a substitution should be used to represent NODE in
437 First, check standard special-case substitutions.
439 <substitution> ::= St
446 # ::std::basic_string
449 # ::std::basic_string<char,
450 ::std::char_traits<char>,
451 ::std::allocator<char> >
454 # ::std::basic_istream<char, ::std::char_traits<char> >
457 # ::std::basic_ostream<char, ::std::char_traits<char> >
460 # ::std::basic_iostream<char, ::std::char_traits<char> >
462 Then examine the stack of currently available substitution
463 candidates for entities appearing earlier in the same mangling
465 If a substitution is found, write its mangled representation and
466 return nonzero. If none is found, just return zero. */
469 find_substitution (node)
473 int size = VARRAY_ACTIVE_SIZE (G.substitutions);
478 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
479 tree_code_name[TREE_CODE (node)], (void *) node);
481 /* Obtain the canonicalized substitution representation for NODE.
482 This is what we'll compare against. */
483 node = canonicalize_for_substitution (node);
485 /* Check for builtin substitutions. */
487 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
488 type = TYPE_P (node) ? node : TREE_TYPE (node);
490 /* Check for std::allocator. */
492 && is_std_substitution (decl, SUBID_ALLOCATOR)
493 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
499 /* Check for std::basic_string. */
500 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
504 /* If this is a type (i.e. a fully-qualified template-id),
506 std::basic_string <char,
507 std::char_traits<char>,
508 std::allocator<char> > . */
509 if (cp_type_quals (type) == TYPE_UNQUALIFIED
510 && CLASSTYPE_USE_TEMPLATE (type))
512 tree args = CLASSTYPE_TI_ARGS (type);
513 if (TREE_VEC_LENGTH (args) == 3
514 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
515 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
517 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
526 /* Substitute for the template name only if this isn't a type. */
533 /* Check for basic_{i,o,io}stream. */
535 && cp_type_quals (type) == TYPE_UNQUALIFIED
536 && CLASS_TYPE_P (type)
537 && CLASSTYPE_USE_TEMPLATE (type)
538 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
540 /* First, check for the template
541 args <char, std::char_traits<char> > . */
542 tree args = CLASSTYPE_TI_ARGS (type);
543 if (TREE_VEC_LENGTH (args) == 2
544 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
545 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
548 /* Got them. Is this basic_istream? */
549 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
550 if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
555 /* Or basic_ostream? */
556 else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
561 /* Or basic_iostream? */
562 else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
570 /* Check for namespace std. */
571 if (decl && DECL_NAMESPACE_STD_P (decl))
577 /* Now check the list of available substitutions for this mangling
579 for (i = 0; i < size; ++i)
581 tree candidate = VARRAY_TREE (G.substitutions, i);
582 /* NODE is a matched to a candidate if it's the same decl node or
583 if it's the same type. */
584 if (decl == candidate
585 || (TYPE_P (candidate) && type && TYPE_P (type)
586 && same_type_p (type, candidate))
587 || NESTED_TEMPLATE_MATCH (node, candidate))
589 write_substitution (i);
594 /* No substitution found. */
599 /* <mangled-name> ::= _Z <encoding> */
602 write_mangled_name (decl)
605 MANGLE_TRACE_TREE ("mangled-name", decl);
607 if (DECL_LANG_SPECIFIC (decl)
608 && DECL_EXTERN_C_FUNCTION_P (decl)
609 && ! DECL_OVERLOADED_OPERATOR_P (decl))
610 /* The standard notes:
611 "The <encoding> of an extern "C" function is treated like
612 global-scope data, i.e. as its <source-name> without a type."
613 We cannot write overloaded operators that way though,
614 because it contains characters invalid in assembler. */
615 write_source_name (DECL_NAME (decl));
617 /* C++ name; needs to be mangled. */
620 write_encoding (decl);
624 /* <encoding> ::= <function name> <bare-function-type>
628 write_encoding (decl)
631 MANGLE_TRACE_TREE ("encoding", decl);
633 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
635 /* For overloaded operators write just the mangled name
636 without arguments. */
637 if (DECL_OVERLOADED_OPERATOR_P (decl))
638 write_name (decl, /*ignore_local_scope=*/0);
640 write_source_name (DECL_NAME (decl));
644 write_name (decl, /*ignore_local_scope=*/0);
645 if (TREE_CODE (decl) == FUNCTION_DECL)
649 if (decl_is_template_id (decl, NULL))
650 fn_type = get_mostly_instantiated_function_type (decl, NULL, NULL);
652 fn_type = TREE_TYPE (decl);
654 write_bare_function_type (fn_type,
655 (!DECL_CONSTRUCTOR_P (decl)
656 && !DECL_DESTRUCTOR_P (decl)
657 && !DECL_CONV_FN_P (decl)
658 && decl_is_template_id (decl, NULL)),
663 /* <name> ::= <unscoped-name>
664 ::= <unscoped-template-name> <template-args>
668 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
669 called from <local-name>, which mangles the enclosing scope
670 elsewhere and then uses this function to mangle just the part
671 underneath the function scope. So don't use the <local-name>
672 production, to avoid an infinite recursion. */
675 write_name (decl, ignore_local_scope)
677 int ignore_local_scope;
681 MANGLE_TRACE_TREE ("name", decl);
683 if (TREE_CODE (decl) == TYPE_DECL)
685 /* In case this is a typedef, fish out the corresponding
686 TYPE_DECL for the main variant. */
687 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
688 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
691 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
693 /* A decl in :: or ::std scope is treated specially. The former is
694 mangled using <unscoped-name> or <unscoped-template-name>, the
695 latter with a special substitution. Also, a name that is
696 directly in a local function scope is also mangled with
697 <unscoped-name> rather than a full <nested-name>. */
699 || context == global_namespace
700 || DECL_NAMESPACE_STD_P (context)
701 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
704 /* Is this a template instance? */
705 if (decl_is_template_id (decl, &template_info))
707 /* Yes: use <unscoped-template-name>. */
708 write_unscoped_template_name (TI_TEMPLATE (template_info));
709 write_template_args (TI_ARGS (template_info));
712 /* Everything else gets an <unqualified-name>. */
713 write_unscoped_name (decl);
717 /* Handle local names, unless we asked not to (that is, invoked
718 under <local-name>, to handle only the part of the name under
720 if (!ignore_local_scope)
722 /* Scan up the list of scope context, looking for a
723 function. If we find one, this entity is in local
724 function scope. local_entity tracks context one scope
725 level down, so it will contain the element that's
726 directly in that function's scope, either decl or one of
727 its enclosing scopes. */
728 tree local_entity = decl;
729 while (context != NULL && context != global_namespace)
731 /* Make sure we're always dealing with decls. */
732 if (context != NULL && TYPE_P (context))
733 context = TYPE_NAME (context);
734 /* Is this a function? */
735 if (TREE_CODE (context) == FUNCTION_DECL)
737 /* Yes, we have local scope. Use the <local-name>
738 production for the innermost function scope. */
739 write_local_name (context, local_entity, decl);
742 /* Up one scope level. */
743 local_entity = context;
744 context = CP_DECL_CONTEXT (context);
747 /* No local scope found? Fall through to <nested-name>. */
750 /* Other decls get a <nested-name> to encode their scope. */
751 write_nested_name (decl);
755 /* <unscoped-name> ::= <unqualified-name>
756 ::= St <unqualified-name> # ::std:: */
759 write_unscoped_name (decl)
762 tree context = CP_DECL_CONTEXT (decl);
764 MANGLE_TRACE_TREE ("unscoped-name", decl);
766 /* Is DECL in ::std? */
767 if (DECL_NAMESPACE_STD_P (context))
770 write_unqualified_name (decl);
772 /* If not, it should be either in the global namespace, or directly
773 in a local function scope. */
774 else if (context == global_namespace
776 || TREE_CODE (context) == FUNCTION_DECL)
777 write_unqualified_name (decl);
782 /* <unscoped-template-name> ::= <unscoped-name>
783 ::= <substitution> */
786 write_unscoped_template_name (decl)
789 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
791 if (find_substitution (decl))
793 write_unscoped_name (decl);
794 add_substitution (decl);
797 /* Write the nested name, including CV-qualifiers, of DECL.
799 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
800 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
802 <CV-qualifiers> ::= [r] [V] [K] */
805 write_nested_name (decl)
810 MANGLE_TRACE_TREE ("nested-name", decl);
814 /* Write CV-qualifiers, if this is a member function. */
815 if (TREE_CODE (decl) == FUNCTION_DECL
816 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
818 if (DECL_VOLATILE_MEMFUNC_P (decl))
820 if (DECL_CONST_MEMFUNC_P (decl))
824 /* Is this a template instance? */
825 if (decl_is_template_id (decl, &template_info))
827 /* Yes, use <template-prefix>. */
828 write_template_prefix (decl);
829 write_template_args (TI_ARGS (template_info));
833 /* No, just use <prefix> */
834 write_prefix (DECL_CONTEXT (decl));
835 write_unqualified_name (decl);
840 /* <prefix> ::= <prefix> <unqualified-name>>
841 ::= <template-prefix> <template-args>
843 ::= <substitution> */
850 /* Non-NULL if NODE represents a template-id. */
851 tree template_info = NULL;
853 MANGLE_TRACE_TREE ("prefix", node);
856 || node == global_namespace)
859 if (find_substitution (node))
863 /* Node is a decl. */
865 /* If this is a function decl, that means we've hit function
866 scope, so this prefix must be for a local name. In this
867 case, we're under the <local-name> production, which encodes
868 the enclosing function scope elsewhere. So don't continue
870 if (TREE_CODE (node) == FUNCTION_DECL)
874 decl_is_template_id (decl, &template_info);
877 /* Node is a type. */
879 decl = TYPE_NAME (node);
880 if (CLASSTYPE_TEMPLATE_ID_P (node))
881 template_info = CLASSTYPE_TEMPLATE_INFO (node);
884 if (template_info != NULL)
887 write_template_prefix (decl);
888 write_template_args (TI_ARGS (template_info));
893 write_prefix (CP_DECL_CONTEXT (decl));
894 write_unqualified_name (decl);
897 add_substitution (node);
900 /* <template-prefix> ::= <prefix> <template component>
901 ::= <substitution> */
904 write_template_prefix (node)
907 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
908 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
909 tree context = CP_DECL_CONTEXT (decl);
914 MANGLE_TRACE_TREE ("template-prefix", node);
916 /* Find the template decl. */
917 if (decl_is_template_id (decl, &template_info))
918 template = TI_TEMPLATE (template_info);
919 else if (CLASSTYPE_TEMPLATE_ID_P (type))
920 template = CLASSTYPE_TI_TEMPLATE (type);
922 /* Oops, not a template. */
925 /* For a member template, though, the template name for the
926 innermost name must have all the outer template levels
927 instantiated. For instance, consider
929 template<typename T> struct Outer {
930 template<typename U> struct Inner {};
933 The template name for `Inner' in `Outer<int>::Inner<float>' is
934 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
935 levels separately, so there's no TEMPLATE_DECL available for this
936 (there's only `Outer<T>::Inner<U>').
938 In order to get the substitutions right, we create a special
939 TREE_LIST to represent the substitution candidate for a nested
940 template. The TREE_PURPOSE is the template's context, fully
941 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
944 So, for the example above, `Outer<int>::Inner' is represented as a
945 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
946 and whose value is `Outer<T>::Inner<U>'. */
947 if (TYPE_P (context))
948 substitution = build_tree_list (context, template);
950 substitution = template;
952 if (find_substitution (substitution))
955 write_prefix (context);
956 write_unqualified_name (decl);
958 add_substitution (substitution);
961 /* We don't need to handle thunks, vtables, or VTTs here. Those are
962 mangled through special entry points.
964 <unqualified-name> ::= <operator-name>
969 write_unqualified_name (decl)
972 MANGLE_TRACE_TREE ("unqualified-name", decl);
974 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
975 write_special_name_constructor (decl);
976 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
977 write_special_name_destructor (decl);
978 else if (DECL_CONV_FN_P (decl))
980 /* Conversion operator. Handle it right here.
981 <operator> ::= cv <type> */
983 if (decl_is_template_id (decl, NULL))
985 tree fn_type = get_mostly_instantiated_function_type (decl, NULL,
987 type = TREE_TYPE (fn_type);
990 type = TREE_TYPE (DECL_NAME (decl));
994 else if (DECL_OVERLOADED_OPERATOR_P (decl))
996 operator_name_info_t *oni;
997 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
998 oni = assignment_operator_name_info;
1000 oni = operator_name_info;
1002 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1005 write_source_name (DECL_NAME (decl));
1008 /* Non-termial <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1010 <source-name> ::= </length/ number> <identifier> */
1013 write_source_name (identifier)
1016 MANGLE_TRACE_TREE ("source-name", identifier);
1018 /* Never write the whole template-id name including the template
1019 arguments; we only want the template name. */
1020 if (IDENTIFIER_TEMPLATE (identifier))
1021 identifier = IDENTIFIER_TEMPLATE (identifier);
1023 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1024 write_identifier (IDENTIFIER_POINTER (identifier));
1027 /* Convert NUMBER to ascii using base BASE and generating at least
1028 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1029 into which to store the characters. Returns the number of
1030 characters generated (these will be layed out in advance of where
1034 hwint_to_ascii (number, base, buffer, min_digits)
1035 unsigned HOST_WIDE_INT number;
1038 unsigned min_digits;
1040 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1041 unsigned digits = 0;
1045 unsigned HOST_WIDE_INT d = number / base;
1047 *--buffer = base_digits[number - d * base];
1051 while (digits < min_digits)
1053 *--buffer = base_digits[0];
1059 /* Non-terminal <number>.
1061 <number> ::= [n] </decimal integer/> */
1064 write_number (number, unsigned_p, base)
1065 unsigned HOST_WIDE_INT number;
1069 char buffer[sizeof (HOST_WIDE_INT) * 8];
1072 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1075 number = -((HOST_WIDE_INT) number);
1077 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1078 write_chars (buffer + sizeof (buffer) - count, count);
1081 /* Write out an integral CST in decimal. Most numbers are small, and
1082 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1083 bigger than that, which we must deal with. */
1086 write_integer_cst (cst)
1089 int sign = tree_int_cst_sgn (cst);
1091 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1093 /* A bignum. We do this in chunks, each of which fits in a
1095 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1096 unsigned HOST_WIDE_INT chunk;
1097 unsigned chunk_digits;
1098 char *ptr = buffer + sizeof (buffer);
1103 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1108 if (sizeof (HOST_WIDE_INT) >= 8)
1110 /* It is at least 64 bits, so 10^18 is representable. */
1115 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1116 base = build_int_2 (chunk, 0);
1117 n = build_int_2 (TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1118 TREE_TYPE (n) = TREE_TYPE (base) = type;
1123 n = fold (build1 (NEGATE_EXPR, type, n));
1127 tree d = fold (build (FLOOR_DIV_EXPR, type, n, base));
1128 tree tmp = fold (build (MULT_EXPR, type, d, base));
1131 done = integer_zerop (d);
1132 tmp = fold (build (MINUS_EXPR, type, n, tmp));
1133 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1134 done ? 1 : chunk_digits);
1140 write_chars (ptr, count);
1145 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1152 write_unsigned_number (low);
1156 /* Non-terminal <identifier>.
1158 <identifier> ::= </unqualified source code identifier> */
1161 write_identifier (identifier)
1162 const char *identifier;
1164 MANGLE_TRACE ("identifier", identifier);
1165 write_string (identifier);
1168 /* Handle constructor productions of non-terminal <special-name>.
1169 CTOR is a constructor FUNCTION_DECL.
1171 <special-name> ::= C1 # complete object constructor
1172 ::= C2 # base object constructor
1173 ::= C3 # complete object allocating constructor
1175 Currently, allocating constructors are never used.
1177 We also need to provide mangled names for the maybe-in-charge
1178 constructor, so we treat it here too. mangle_decl_string will
1179 append *INTERNAL* to that, to make sure we never emit it. */
1182 write_special_name_constructor (ctor)
1185 if (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1186 /* Even though we don't ever emit a definition of the
1187 old-style destructor, we still have to consider entities
1188 (like static variables) nested inside it. */
1189 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1190 write_string ("C1");
1191 else if (DECL_BASE_CONSTRUCTOR_P (ctor))
1192 write_string ("C2");
1197 /* Handle destructor productions of non-terminal <special-name>.
1198 DTOR is a destructor FUNCTION_DECL.
1200 <special-name> ::= D0 # deleting (in-charge) destructor
1201 ::= D1 # complete object (in-charge) destructor
1202 ::= D2 # base object (not-in-charge) destructor
1204 We also need to provide mangled names for the maybe-incharge
1205 destructor, so we treat it here too. mangle_decl_string will
1206 append *INTERNAL* to that, to make sure we never emit it. */
1209 write_special_name_destructor (dtor)
1212 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1213 write_string ("D0");
1214 else if (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1215 /* Even though we don't ever emit a definition of the
1216 old-style destructor, we still have to consider entities
1217 (like static variables) nested inside it. */
1218 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1219 write_string ("D1");
1220 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1221 write_string ("D2");
1226 /* Return the discriminator for ENTITY appearing inside
1227 FUNCTION. The discriminator is the lexical ordinal of VAR among
1228 entities with the same name in the same FUNCTION. */
1231 discriminator_for_local_entity (entity)
1237 /* Assume this is the only local entity with this name. */
1240 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1241 discriminator = DECL_DISCRIMINATOR (entity);
1242 else if (TREE_CODE (entity) == TYPE_DECL)
1244 /* Scan the list of local classes. */
1245 entity = TREE_TYPE (entity);
1246 for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
1247 if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1248 && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1252 return discriminator;
1255 /* Return the discriminator for STRING, a string literal used inside
1256 FUNCTION. The disciminator is the lexical ordinal of STRING among
1257 string literals used in FUNCTION. */
1260 discriminator_for_string_literal (function, string)
1261 tree function ATTRIBUTE_UNUSED;
1262 tree string ATTRIBUTE_UNUSED;
1264 /* For now, we don't discriminate amongst string literals. */
1268 /* <discriminator> := _ <number>
1270 The discriminator is used only for the second and later occurrences
1271 of the same name within a single function. In this case <number> is
1272 n - 2, if this is the nth occurrence, in lexical order. */
1275 write_discriminator (discriminator)
1278 /* If discriminator is zero, don't write anything. Otherwise... */
1279 if (discriminator > 0)
1282 write_unsigned_number (discriminator - 1);
1286 /* Mangle the name of a function-scope entity. FUNCTION is the
1287 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1288 the entity itself. LOCAL_ENTITY is the entity that's directly
1289 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1292 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1293 := Z <function encoding> E s [<discriminator>] */
1296 write_local_name (function, local_entity, entity)
1301 MANGLE_TRACE_TREE ("local-name", entity);
1304 write_encoding (function);
1306 if (TREE_CODE (entity) == STRING_CST)
1309 write_discriminator (discriminator_for_string_literal (function,
1314 /* Now the <entity name>. Let write_name know its being called
1315 from <local-name>, so it doesn't try to process the enclosing
1316 function scope again. */
1317 write_name (entity, /*ignore_local_scope=*/1);
1318 write_discriminator (discriminator_for_local_entity (local_entity));
1322 /* Non-terminals <type> and <CV-qualifier>.
1324 <type> ::= <builtin-type>
1326 ::= <class-enum-type>
1328 ::= <pointer-to-member-type>
1329 ::= <template-param>
1332 ::= P <type> # pointer-to
1333 ::= R <type> # reference-to
1334 ::= C <type> # complex pair (C 2000)
1335 ::= G <type> # imaginary (C 2000) [not supported]
1336 ::= U <source-name> <type> # vendor extended type qualifier
1338 TYPE is a type node. */
1344 /* This gets set to nonzero if TYPE turns out to be a (possibly
1345 CV-qualified) builtin type. */
1346 int is_builtin_type = 0;
1348 MANGLE_TRACE_TREE ("type", type);
1350 if (type == error_mark_node)
1353 if (find_substitution (type))
1356 if (write_CV_qualifiers_for_type (type) > 0)
1357 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1358 mangle the unqualified type. The recursive call is needed here
1359 since both the qualified and uqualified types are substitution
1361 write_type (TYPE_MAIN_VARIANT (type));
1362 else if (TREE_CODE (type) == ARRAY_TYPE)
1363 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1364 so that the cv-qualification of the element type is available
1365 in write_array_type. */
1366 write_array_type (type);
1369 /* See through any typedefs. */
1370 type = TYPE_MAIN_VARIANT (type);
1372 switch (TREE_CODE (type))
1376 case INTEGER_TYPE: /* Includes wchar_t. */
1378 /* If this is a typedef, TYPE may not be one of
1379 the standard builtin type nodes, but an alias of one. Use
1380 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1381 write_builtin_type (TYPE_MAIN_VARIANT (type));
1387 write_type (TREE_TYPE (type));
1392 write_function_type (type);
1398 /* A pointer-to-member function is represented as a special
1399 RECORD_TYPE, so check for this first. */
1400 if (TYPE_PTRMEMFUNC_P (type))
1401 write_pointer_to_member_type (type);
1403 write_class_enum_type (type);
1407 case UNBOUND_CLASS_TEMPLATE:
1408 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1409 ordinary nested names. */
1410 write_nested_name (TYPE_STUB_DECL (type));
1414 /* A pointer-to-member variable is represented by a POINTER_TYPE
1415 to an OFFSET_TYPE, so check for this first. */
1416 if (TYPE_PTRMEM_P (type))
1417 write_pointer_to_member_type (type);
1421 write_type (TREE_TYPE (type));
1425 case REFERENCE_TYPE:
1427 write_type (TREE_TYPE (type));
1430 case TEMPLATE_TYPE_PARM:
1431 case TEMPLATE_PARM_INDEX:
1432 write_template_param (type);
1435 case TEMPLATE_TEMPLATE_PARM:
1436 write_template_template_param (type);
1439 case BOUND_TEMPLATE_TEMPLATE_PARM:
1440 write_template_template_param (type);
1442 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1446 write_pointer_to_member_type (build_pointer_type (type));
1450 write_string ("U8__vector");
1451 write_type (TREE_TYPE (type));
1459 /* Types other than builtin types are substitution candidates. */
1460 if (!is_builtin_type)
1461 add_substitution (type);
1464 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1465 CV-qualifiers written for TYPE.
1467 <CV-qualifiers> ::= [r] [V] [K] */
1470 write_CV_qualifiers_for_type (type)
1473 int num_qualifiers = 0;
1475 /* The order is specified by:
1477 "In cases where multiple order-insensitive qualifiers are
1478 present, they should be ordered 'K' (closest to the base type),
1479 'V', 'r', and 'U' (farthest from the base type) ..."
1481 Note that we do not use cp_type_quals below; given "const
1482 int[3]", the "const" is emitted with the "int", not with the
1485 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1490 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1495 if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1501 return num_qualifiers;
1504 /* Non-terminal <builtin-type>.
1506 <builtin-type> ::= v # void
1511 ::= h # unsigned char
1513 ::= t # unsigned short
1515 ::= j # unsigned int
1517 ::= m # unsigned long
1518 ::= x # long long, __int64
1519 ::= y # unsigned long long, __int64
1521 ::= o # unsigned __int128
1524 ::= e # long double, __float80
1525 ::= g # __float128 [not supported]
1526 ::= u <source-name> # vendor extended type */
1529 write_builtin_type (type)
1532 switch (TREE_CODE (type))
1543 /* If this is size_t, get the underlying int type. */
1544 if (TYPE_IS_SIZETYPE (type))
1545 type = TYPE_DOMAIN (type);
1547 /* TYPE may still be wchar_t, since that isn't in
1548 integer_type_nodes. */
1549 if (type == wchar_type_node)
1551 else if (TYPE_FOR_JAVA (type))
1552 write_java_integer_type_codes (type);
1556 /* Assume TYPE is one of the shared integer type nodes. Find
1557 it in the array of these nodes. */
1559 for (itk = 0; itk < itk_none; ++itk)
1560 if (type == integer_types[itk])
1562 /* Print the corresponding single-letter code. */
1563 write_char (integer_type_codes[itk]);
1567 if (itk == itk_none)
1569 tree t = c_common_type_for_mode (TYPE_MODE (type),
1570 TREE_UNSIGNED (type));
1573 if (TYPE_PRECISION (type) == 128)
1574 write_char (TREE_UNSIGNED (type) ? 'o' : 'n');
1576 /* Couldn't find this type. */
1589 if (type == float_type_node
1590 || type == java_float_type_node)
1592 else if (type == double_type_node
1593 || type == java_double_type_node)
1595 else if (type == long_double_type_node)
1606 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1607 METHOD_TYPE. The return type is mangled before the parameter
1610 <function-type> ::= F [Y] <bare-function-type> E */
1613 write_function_type (type)
1616 MANGLE_TRACE_TREE ("function-type", type);
1618 /* For a pointer to member function, the function type may have
1619 cv-qualifiers, indicating the quals for the artificial 'this'
1621 if (TREE_CODE (type) == METHOD_TYPE)
1623 /* The first parameter must be a POINTER_TYPE pointing to the
1624 `this' parameter. */
1625 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1626 write_CV_qualifiers_for_type (this_type);
1630 /* We don't track whether or not a type is `extern "C"'. Note that
1631 you can have an `extern "C"' function that does not have
1632 `extern "C"' type, and vice versa:
1634 extern "C" typedef void function_t();
1635 function_t f; // f has C++ linkage, but its type is
1638 typedef void function_t();
1639 extern "C" function_t f; // Vice versa.
1642 write_bare_function_type (type, /*include_return_type_p=*/1,
1647 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1648 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
1649 is mangled before the parameter types. If non-NULL, DECL is
1650 FUNCTION_DECL for the function whose type is being emitted.
1652 <bare-function-type> ::= </signature/ type>+ */
1655 write_bare_function_type (type, include_return_type_p, decl)
1657 int include_return_type_p;
1660 MANGLE_TRACE_TREE ("bare-function-type", type);
1662 /* Mangle the return type, if requested. */
1663 if (include_return_type_p)
1664 write_type (TREE_TYPE (type));
1666 /* Now mangle the types of the arguments. */
1667 write_method_parms (TYPE_ARG_TYPES (type),
1668 TREE_CODE (type) == METHOD_TYPE,
1672 /* Write the mangled representation of a method parameter list of
1673 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
1674 considered a non-static method, and the this parameter is omitted.
1675 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1676 parameters are being emitted. */
1679 write_method_parms (parm_types, method_p, decl)
1684 tree first_parm_type;
1685 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1687 /* Assume this parameter type list is variable-length. If it ends
1688 with a void type, then it's not. */
1691 /* If this is a member function, skip the first arg, which is the
1693 "Member functions do not encode the type of their implicit this
1696 Similarly, there's no need to mangle artificial parameters, like
1697 the VTT parameters for constructors and destructors. */
1700 parm_types = TREE_CHAIN (parm_types);
1701 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1703 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1705 parm_types = TREE_CHAIN (parm_types);
1706 parm_decl = TREE_CHAIN (parm_decl);
1710 for (first_parm_type = parm_types;
1712 parm_types = TREE_CHAIN (parm_types))
1714 tree parm = TREE_VALUE (parm_types);
1715 if (parm == void_type_node)
1717 /* "Empty parameter lists, whether declared as () or
1718 conventionally as (void), are encoded with a void parameter
1720 if (parm_types == first_parm_type)
1722 /* If the parm list is terminated with a void type, it's
1725 /* A void type better be the last one. */
1726 my_friendly_assert (TREE_CHAIN (parm_types) == NULL, 20000523);
1733 /* <builtin-type> ::= z # ellipsis */
1737 /* <class-enum-type> ::= <name> */
1740 write_class_enum_type (type)
1743 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1746 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1749 <template-args> ::= I <template-arg>+ E */
1752 write_template_args (args)
1756 int length = TREE_VEC_LENGTH (args);
1758 MANGLE_TRACE_TREE ("template-args", args);
1760 my_friendly_assert (length > 0, 20000422);
1762 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1764 /* We have nested template args. We want the innermost template
1766 args = TREE_VEC_ELT (args, length - 1);
1767 length = TREE_VEC_LENGTH (args);
1771 for (i = 0; i < length; ++i)
1772 write_template_arg (TREE_VEC_ELT (args, i));
1776 /* <expression> ::= <unary operator-name> <expression>
1777 ::= <binary operator-name> <expression> <expression>
1780 <expr-primary> ::= <template-param>
1781 ::= L <type> <value number> E # literal
1782 ::= L <mangled-name> E # external name */
1785 write_expression (expr)
1788 enum tree_code code;
1790 code = TREE_CODE (expr);
1792 /* Handle pointers-to-members by making them look like expression
1794 if (code == PTRMEM_CST)
1796 expr = build_nt (ADDR_EXPR,
1797 build_nt (SCOPE_REF,
1798 PTRMEM_CST_CLASS (expr),
1799 PTRMEM_CST_MEMBER (expr)));
1800 code = TREE_CODE (expr);
1803 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1804 is converted (via qualification conversions) to another
1806 while (TREE_CODE (expr) == NOP_EXPR
1807 || TREE_CODE (expr) == NON_LVALUE_EXPR)
1809 expr = TREE_OPERAND (expr, 0);
1810 code = TREE_CODE (expr);
1813 /* Handle template parameters. */
1814 if (code == TEMPLATE_TYPE_PARM
1815 || code == TEMPLATE_TEMPLATE_PARM
1816 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1817 || code == TEMPLATE_PARM_INDEX)
1818 write_template_param (expr);
1819 /* Handle literals. */
1820 else if (TREE_CODE_CLASS (code) == 'c')
1821 write_template_arg_literal (expr);
1822 else if (DECL_P (expr))
1825 write_mangled_name (expr);
1828 else if (TREE_CODE (expr) == SIZEOF_EXPR
1829 && TYPE_P (TREE_OPERAND (expr, 0)))
1831 write_string ("st");
1832 write_type (TREE_OPERAND (expr, 0));
1838 /* When we bind a variable or function to a non-type template
1839 argument with reference type, we create an ADDR_EXPR to show
1840 the fact that the entity's address has been taken. But, we
1841 don't actually want to output a mangling code for the `&'. */
1842 if (TREE_CODE (expr) == ADDR_EXPR
1844 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
1846 expr = TREE_OPERAND (expr, 0);
1849 write_expression (expr);
1853 code = TREE_CODE (expr);
1856 /* If it wasn't any of those, recursively expand the expression. */
1857 write_string (operator_name_info[(int) code].mangled_name);
1862 write_type (TREE_TYPE (expr));
1863 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
1866 case STATIC_CAST_EXPR:
1867 case CONST_CAST_EXPR:
1868 write_type (TREE_TYPE (expr));
1869 write_expression (TREE_OPERAND (expr, 0));
1873 /* Handle pointers-to-members specially. */
1875 write_type (TREE_OPERAND (expr, 0));
1876 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
1877 write_source_name (TREE_OPERAND (expr, 1));
1879 write_encoding (TREE_OPERAND (expr, 1));
1883 for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
1884 write_expression (TREE_OPERAND (expr, i));
1889 /* Literal subcase of non-terminal <template-arg>.
1891 "Literal arguments, e.g. "A<42L>", are encoded with their type
1892 and value. Negative integer values are preceded with "n"; for
1893 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
1894 encoded as 0, true as 1. If floating-point arguments are accepted
1895 as an extension, their values should be encoded using a
1896 fixed-length lowercase hexadecimal string corresponding to the
1897 internal representation (IEEE on IA-64), high-order bytes first,
1898 without leading zeroes. For example: "Lfbff000000E" is -1.0f." */
1901 write_template_arg_literal (value)
1904 tree type = TREE_TYPE (value);
1908 if (TREE_CODE (value) == CONST_DECL)
1909 write_integer_cst (DECL_INITIAL (value));
1910 else if (TREE_CODE (value) == INTEGER_CST)
1912 if (same_type_p (type, boolean_type_node))
1914 if (value == boolean_false_node || integer_zerop (value))
1915 write_unsigned_number (0);
1916 else if (value == boolean_true_node)
1917 write_unsigned_number (1);
1922 write_integer_cst (value);
1924 else if (TREE_CODE (value) == REAL_CST)
1926 #ifdef CROSS_COMPILE
1927 static int explained;
1931 sorry ("real-valued template parameters when cross-compiling");
1936 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1937 write_number (((unsigned char *)
1938 &TREE_REAL_CST (value))[i],
1949 /* Non-terminal <tempalate-arg>.
1951 <template-arg> ::= <type> # type
1952 ::= L <type> </value/ number> E # literal
1953 ::= LZ <name> E # external name
1954 ::= X <expression> E # expression */
1957 write_template_arg (node)
1960 enum tree_code code = TREE_CODE (node);
1962 MANGLE_TRACE_TREE ("template-arg", node);
1964 /* A template template paramter's argument list contains TREE_LIST
1965 nodes of which the value field is the the actual argument. */
1966 if (code == TREE_LIST)
1968 node = TREE_VALUE (node);
1969 /* If it's a decl, deal with its type instead. */
1972 node = TREE_TYPE (node);
1973 code = TREE_CODE (node);
1979 else if (code == TEMPLATE_DECL)
1980 /* A template appearing as a template arg is a template template arg. */
1981 write_template_template_arg (node);
1982 else if (DECL_P (node))
1986 write_encoding (node);
1989 else if (TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST)
1990 write_template_arg_literal (node);
1993 /* Template arguments may be expressions. */
1995 write_expression (node);
2000 /* <template-template-arg>
2002 ::= <substitution> */
2005 write_template_template_arg (tree decl)
2007 MANGLE_TRACE_TREE ("template-template-arg", decl);
2009 if (find_substitution (decl))
2011 write_name (decl, /*ignore_local_scope=*/0);
2012 add_substitution (decl);
2016 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
2018 <array-type> ::= A [</dimension/ number>] _ </element/ type>
2019 ::= A <expression> _ </element/ type>
2021 "Array types encode the dimension (number of elements) and the
2022 element type. For variable length arrays, the dimension (but not
2023 the '_' separator) is omitted." */
2026 write_array_type (type)
2030 if (TYPE_DOMAIN (type))
2035 index_type = TYPE_DOMAIN (type);
2036 /* The INDEX_TYPE gives the upper and lower bounds of the
2038 max = TYPE_MAX_VALUE (index_type);
2039 if (TREE_CODE (max) == INTEGER_CST)
2041 /* The ABI specifies that we should mangle the number of
2042 elements in the array, not the largest allowed index. */
2043 max = size_binop (PLUS_EXPR, max, size_one_node);
2044 write_unsigned_number (tree_low_cst (max, 1));
2047 write_expression (TREE_OPERAND (max, 0));
2050 write_type (TREE_TYPE (type));
2053 /* Non-terminal <pointer-to-member-type> for pointer-to-member
2054 variables. TYPE is a pointer-to-member POINTER_TYPE.
2056 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2059 write_pointer_to_member_type (type)
2063 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
2064 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2067 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
2068 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2069 TEMPLATE_PARM_INDEX.
2071 <template-param> ::= T </parameter/ number> _
2073 If we are internally mangling then we distinguish level and, for
2074 non-type parms, type too. The mangling appends
2076 </level/ number> _ </non-type type/ type> _
2078 This is used by mangle_conv_op_name_for_type. */
2081 write_template_param (parm)
2086 tree parm_type = NULL_TREE;
2088 MANGLE_TRACE_TREE ("template-parm", parm);
2090 switch (TREE_CODE (parm))
2092 case TEMPLATE_TYPE_PARM:
2093 case TEMPLATE_TEMPLATE_PARM:
2094 case BOUND_TEMPLATE_TEMPLATE_PARM:
2095 parm_index = TEMPLATE_TYPE_IDX (parm);
2096 parm_level = TEMPLATE_TYPE_LEVEL (parm);
2099 case TEMPLATE_PARM_INDEX:
2100 parm_index = TEMPLATE_PARM_IDX (parm);
2101 parm_level = TEMPLATE_PARM_LEVEL (parm);
2102 parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
2110 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2111 earliest template param denoted by `_'. */
2113 write_unsigned_number (parm_index - 1);
2115 if (G.internal_mangling_p)
2118 write_unsigned_number (parm_level - 1);
2121 write_type (parm_type);
2126 /* <template-template-param>
2127 ::= <template-param>
2128 ::= <substitution> */
2131 write_template_template_param (parm)
2134 tree template = NULL_TREE;
2136 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2137 template template parameter. The substitution candidate here is
2138 only the template. */
2139 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2142 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2143 if (find_substitution (template))
2147 /* <template-param> encodes only the template parameter position,
2148 not its template arguments, which is fine here. */
2149 write_template_param (parm);
2151 add_substitution (template);
2154 /* Non-terminal <substitution>.
2156 <substitution> ::= S <seq-id> _
2160 write_substitution (seq_id)
2163 MANGLE_TRACE ("substitution", "");
2167 write_number (seq_id - 1, /*unsigned=*/1, 36);
2171 /* Start mangling a new name or type. */
2176 VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
2177 obstack_free (&G.name_obstack, obstack_base (&G.name_obstack));
2180 /* Done with mangling. Return the generated mangled name. */
2182 static inline const char *
2185 /* Clear all the substitutions. */
2186 G.substitutions = 0;
2188 /* Null-terminate the string. */
2191 return (const char *) obstack_base (&G.name_obstack);
2194 /* Initialize data structures for mangling. */
2199 gcc_obstack_init (&G.name_obstack);
2201 /* Cache these identifiers for quick comparison when checking for
2202 standard substitutions. */
2203 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2204 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2205 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2206 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2207 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2208 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2211 /* Generate the mangled name of DECL. */
2214 mangle_decl_string (decl)
2221 if (TREE_CODE (decl) == TYPE_DECL)
2222 write_type (TREE_TYPE (decl));
2223 else if (/* The names of `extern "C"' functions are not mangled. */
2224 (DECL_EXTERN_C_FUNCTION_P (decl)
2225 /* But overloaded operator names *are* mangled. */
2226 && !DECL_OVERLOADED_OPERATOR_P (decl))
2227 /* The names of global variables aren't mangled either. */
2228 || (TREE_CODE (decl) == VAR_DECL
2229 && CP_DECL_CONTEXT (decl) == global_namespace)
2230 /* And neither are `extern "C"' variables. */
2231 || (TREE_CODE (decl) == VAR_DECL
2232 && DECL_EXTERN_C_P (decl)))
2233 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
2236 write_mangled_name (decl);
2237 if (DECL_LANG_SPECIFIC (decl)
2238 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
2239 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
2240 /* We need a distinct mangled name for these entities, but
2241 we should never actually output it. So, we append some
2242 characters the assembler won't like. */
2243 write_string (" *INTERNAL* ");
2246 result = finish_mangling ();
2248 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2252 /* Create an identifier for the external mangled name of DECL. */
2258 tree id = get_identifier (mangle_decl_string (decl));
2260 SET_DECL_ASSEMBLER_NAME (decl, id);
2263 /* Generate the mangled representation of TYPE. */
2266 mangle_type_string (type)
2273 result = finish_mangling ();
2275 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2279 /* Create an identifier for the mangled representation of TYPE. */
2285 return get_identifier (mangle_type_string (type));
2288 /* Create an identifier for the mangled name of a special component
2289 for belonging to TYPE. CODE is the ABI-specified code for this
2293 mangle_special_for_type (type, code)
2299 /* We don't have an actual decl here for the special component, so
2300 we can't just process the <encoded-name>. Instead, fake it. */
2303 /* Start the mangling. */
2304 write_string ("_Z");
2305 write_string (code);
2309 result = finish_mangling ();
2312 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2314 return get_identifier (result);
2317 /* Create an identifier for the mangled representation of the typeinfo
2318 structure for TYPE. */
2321 mangle_typeinfo_for_type (type)
2324 return mangle_special_for_type (type, "TI");
2327 /* Create an identifier for the mangled name of the NTBS containing
2328 the mangled name of TYPE. */
2331 mangle_typeinfo_string_for_type (type)
2334 return mangle_special_for_type (type, "TS");
2337 /* Create an identifier for the mangled name of the vtable for TYPE. */
2340 mangle_vtbl_for_type (type)
2343 return mangle_special_for_type (type, "TV");
2346 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2349 mangle_vtt_for_type (type)
2352 return mangle_special_for_type (type, "TT");
2355 /* Return an identifier for a construction vtable group. TYPE is
2356 the most derived class in the hierarchy; BINFO is the base
2357 subobject for which this construction vtable group will be used.
2359 This mangling isn't part of the ABI specification; in the ABI
2360 specification, the vtable group is dumped in the same COMDAT as the
2361 main vtable, and is referenced only from that vtable, so it doesn't
2362 need an external name. For binary formats without COMDAT sections,
2363 though, we need external names for the vtable groups.
2365 We use the production
2367 <special-name> ::= CT <type> <offset number> _ <base type> */
2370 mangle_ctor_vtbl_for_type (type, binfo)
2378 write_string ("_Z");
2379 write_string ("TC");
2381 write_integer_cst (BINFO_OFFSET (binfo));
2383 write_type (BINFO_TYPE (binfo));
2385 result = finish_mangling ();
2387 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2388 return get_identifier (result);
2391 /* Return an identifier for the mangled name of a thunk to FN_DECL.
2392 OFFSET is the initial adjustment to this used to find the vptr. If
2393 VCALL_OFFSET is non-NULL, this is a virtual thunk, and it is the
2394 vtbl offset in bytes.
2396 <special-name> ::= Th <offset number> _ <base encoding>
2397 ::= Tv <offset number> _ <vcall offset number> _
2402 mangle_thunk (fn_decl, offset, vcall_offset)
2411 write_string ("_Z");
2412 /* The <special-name> for virtual thunks is Tv, for non-virtual
2415 if (vcall_offset != 0)
2420 /* For either flavor, write the offset to this. */
2421 write_integer_cst (offset);
2424 /* For a virtual thunk, add the vcall offset. */
2427 /* Virtual thunk. Write the vcall offset and base type name. */
2428 write_integer_cst (vcall_offset);
2433 write_encoding (fn_decl);
2435 result = finish_mangling ();
2437 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2438 return get_identifier (result);
2441 /* Return an identifier for the mangled unqualified name for a
2442 conversion operator to TYPE. This mangling is not specified by the
2443 ABI spec; it is only used internally. */
2446 mangle_conv_op_name_for_type (type)
2450 const char *mangled_type;
2453 /* Build the internal mangling for TYPE. */
2454 G.internal_mangling_p = true;
2455 mangled_type = mangle_type_string (type);
2456 G.internal_mangling_p = false;
2458 /* Allocate a temporary buffer for the complete name. */
2459 op_name = concat ("operator ", mangled_type, NULL);
2460 /* Find or create an identifier. */
2461 identifier = get_identifier (op_name);
2462 /* Done with the temporary buffer. */
2465 /* It had better be a unique mangling for the type. */
2466 my_friendly_assert (!IDENTIFIER_TYPENAME_P (identifier)
2467 || same_type_p (type, TREE_TYPE (identifier)),
2470 /* Set bits on the identifier so we know later it's a conversion. */
2471 IDENTIFIER_OPNAME_P (identifier) = 1;
2472 IDENTIFIER_TYPENAME_P (identifier) = 1;
2473 /* Hang TYPE off the identifier so it can be found easily later when
2474 performing conversions. */
2475 TREE_TYPE (identifier) = type;
2480 /* Return an identifier for the name of an initialization guard
2481 variable for indicated VARIABLE. */
2484 mangle_guard_variable (variable)
2488 write_string ("_ZGV");
2489 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
2490 /* The name of a guard variable for a reference temporary should refer
2491 to the reference, not the temporary. */
2492 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
2494 write_name (variable, /*ignore_local_scope=*/0);
2495 return get_identifier (finish_mangling ());
2498 /* Return an identifier for the name of a temporary variable used to
2499 initialize a static reference. This isn't part of the ABI, but we might
2500 as well call them something readable. */
2503 mangle_ref_init_variable (variable)
2507 write_string ("_ZGR");
2508 write_name (variable, /*ignore_local_scope=*/0);
2509 return get_identifier (finish_mangling ());
2513 /* Foreign language type mangling section. */
2515 /* How to write the type codes for the integer Java type. */
2518 write_java_integer_type_codes (type)
2521 if (type == java_int_type_node)
2523 else if (type == java_short_type_node)
2525 else if (type == java_byte_type_node)
2527 else if (type == java_char_type_node)
2529 else if (type == java_long_type_node)
2531 else if (type == java_boolean_type_node)