1 /* Name mangling for the new standard C++ ABI.
2 Copyright (C) 2000, 2001 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
58 /* Debugging support. */
60 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
62 #define DEBUG_MANGLE 0
65 /* Macros for tracing the write_* functions. */
67 # define MANGLE_TRACE(FN, INPUT) \
68 fprintf (stderr, " %-24s: %-24s\n", FN, INPUT)
69 # define MANGLE_TRACE_TREE(FN, NODE) \
70 fprintf (stderr, " %-24s: %-24s (%p)\n", \
71 FN, tree_code_name[TREE_CODE (NODE)], (void *) NODE)
73 # define MANGLE_TRACE(FN, INPUT)
74 # define MANGLE_TRACE_TREE(FN, NODE)
77 /* Non-zero if NODE is a class template-id. We can't rely on
78 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
79 that hard to distinguish A<T> from A, where A<T> is the type as
80 instantiated outside of the template, and A is the type used
81 without parameters inside the template. */
82 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
83 (TYPE_LANG_SPECIFIC (NODE) != NULL \
84 && CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
85 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))
87 /* Things we only need one of. This module is not reentrant. */
90 /* The name in which we're building the mangled name. */
91 struct obstack name_obstack;
93 /* An array of the current substitution candidates, in the order
95 varray_type substitutions;
98 /* Indices into subst_identifiers. These are identifiers used in
99 special substitution rules. */
107 SUBID_BASIC_IOSTREAM,
110 substitution_identifier_index_t;
112 /* For quick substitution checks, look up these common identifiers
114 static tree subst_identifiers[SUBID_MAX];
116 /* Single-letter codes for builtin integer types, defined in
117 <builtin-type>. These are indexed by integer_type_kind values. */
119 integer_type_codes[itk_none] =
122 'a', /* itk_signed_char */
123 'h', /* itk_unsigned_char */
125 't', /* itk_unsigned_short */
127 'j', /* itk_unsigned_int */
129 'm', /* itk_unsigned_long */
130 'x', /* itk_long_long */
131 'y' /* itk_unsigned_long_long */
134 static int decl_is_template_id PARAMS ((tree, tree*));
136 /* Functions for handling substitutions. */
138 static inline tree canonicalize_for_substitution PARAMS ((tree));
139 static void add_substitution PARAMS ((tree));
140 static inline int is_std_substitution PARAMS ((tree, substitution_identifier_index_t));
141 static inline int is_std_substitution_char PARAMS ((tree, substitution_identifier_index_t));
142 static int find_substitution PARAMS ((tree));
144 /* Functions for emitting mangled representations of things. */
146 static void write_mangled_name PARAMS ((tree));
147 static void write_encoding PARAMS ((tree));
148 static void write_name PARAMS ((tree, int));
149 static void write_unscoped_name PARAMS ((tree));
150 static void write_unscoped_template_name PARAMS ((tree));
151 static void write_nested_name PARAMS ((tree));
152 static void write_prefix PARAMS ((tree));
153 static void write_template_prefix PARAMS ((tree));
154 static void write_unqualified_name PARAMS ((tree));
155 static void write_source_name PARAMS ((tree));
156 static void write_number PARAMS ((unsigned HOST_WIDE_INT, int,
158 static void write_integer_cst PARAMS ((tree));
159 static void write_identifier PARAMS ((const char *));
160 static void write_special_name_constructor PARAMS ((tree));
161 static void write_special_name_destructor PARAMS ((tree));
162 static void write_type PARAMS ((tree));
163 static int write_CV_qualifiers_for_type PARAMS ((tree));
164 static void write_builtin_type PARAMS ((tree));
165 static void write_function_type PARAMS ((tree));
166 static void write_bare_function_type PARAMS ((tree, int, tree));
167 static void write_method_parms PARAMS ((tree, int, tree));
168 static void write_class_enum_type PARAMS ((tree));
169 static void write_template_args PARAMS ((tree));
170 static void write_expression PARAMS ((tree));
171 static void write_template_arg_literal PARAMS ((tree));
172 static void write_template_arg PARAMS ((tree));
173 static void write_template_template_arg PARAMS ((tree));
174 static void write_array_type PARAMS ((tree));
175 static void write_pointer_to_member_type PARAMS ((tree));
176 static void write_template_param PARAMS ((tree));
177 static void write_template_template_param PARAMS ((tree));
178 static void write_substitution PARAMS ((int));
179 static int discriminator_for_local_entity PARAMS ((tree));
180 static int discriminator_for_string_literal PARAMS ((tree, tree));
181 static void write_discriminator PARAMS ((int));
182 static void write_local_name PARAMS ((tree, tree, tree));
183 static void dump_substitution_candidates PARAMS ((void));
184 static const char *mangle_decl_string PARAMS ((tree));
186 /* Control functions. */
188 static inline void start_mangling PARAMS ((void));
189 static inline const char *finish_mangling PARAMS ((void));
190 static tree mangle_special_for_type PARAMS ((tree, const char *));
192 /* Foreign language functions. */
194 static void write_java_integer_type_codes PARAMS ((tree));
196 /* Append a single character to the end of the mangled
198 #define write_char(CHAR) \
199 obstack_1grow (&G.name_obstack, (CHAR))
201 /* Append a NUL-terminated string to the end of the mangled
203 #define write_string(STRING) \
204 obstack_grow (&G.name_obstack, (STRING), strlen (STRING))
206 /* Return the position at which the next character will be appended to
207 the mangled representation. */
208 #define mangled_position() \
209 obstack_object_size (&G.name_obstack)
211 /* Non-zero if NODE1 and NODE2 are both TREE_LIST nodes and have the
212 same purpose (context, which may be a type) and value (template
213 decl). See write_template_prefix for more information on what this
215 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
216 (TREE_CODE (NODE1) == TREE_LIST \
217 && TREE_CODE (NODE2) == TREE_LIST \
218 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
219 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
220 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
221 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
223 /* Write out a signed quantity in base 10. */
224 #define write_signed_number(NUMBER) \
225 write_number (NUMBER, /*unsigned_p=*/0, 10)
227 /* Write out an unsigned quantity in base 10. */
228 #define write_unsigned_number(NUMBER) \
229 write_number (NUMBER, /*unsigned_p=*/1, 10)
231 /* If DECL is a template instance, return non-zero and, if
232 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
233 Otherwise return zero. */
236 decl_is_template_id (decl, template_info)
240 if (TREE_CODE (decl) == TYPE_DECL)
242 /* TYPE_DECLs are handled specially. Look at its type to decide
243 if this is a template instantiation. */
244 tree type = TREE_TYPE (decl);
246 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
248 if (template_info != NULL)
249 /* For a templated TYPE_DECL, the template info is hanging
251 *template_info = CLASSTYPE_TEMPLATE_INFO (type);
257 /* Check if this is a primary template. */
258 if (DECL_LANG_SPECIFIC (decl) != NULL
259 && DECL_USE_TEMPLATE (decl)
260 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
261 && TREE_CODE (decl) != TEMPLATE_DECL)
263 if (template_info != NULL)
264 /* For most templated decls, the template info is hanging
266 *template_info = DECL_TEMPLATE_INFO (decl);
271 /* It's not a template id. */
275 /* Produce debugging output of current substitution candidates. */
278 dump_substitution_candidates ()
282 fprintf (stderr, " ++ substitutions ");
283 for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
285 tree el = VARRAY_TREE (G.substitutions, i);
286 const char *name = "???";
289 fprintf (stderr, " ");
291 name = IDENTIFIER_POINTER (DECL_NAME (el));
292 else if (TREE_CODE (el) == TREE_LIST)
293 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
294 else if (TYPE_NAME (el))
295 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
296 fprintf (stderr, " S%d_ = ", i - 1);
298 (CP_TYPE_RESTRICT_P (el)
299 || CP_TYPE_VOLATILE_P (el)
300 || CP_TYPE_CONST_P (el)))
301 fprintf (stderr, "CV-");
302 fprintf (stderr, "%s (%s at %p)\n",
303 name, tree_code_name[TREE_CODE (el)], (void *) el);
307 /* Both decls and types can be substitution candidates, but sometimes
308 they refer to the same thing. For instance, a TYPE_DECL and
309 RECORD_TYPE for the same class refer to the same thing, and should
310 be treated accordinginly in substitutions. This function returns a
311 canonicalized tree node representing NODE that is used when adding
312 and substitution candidates and finding matches. */
315 canonicalize_for_substitution (node)
318 /* For a TYPE_DECL, use the type instead. */
319 if (TREE_CODE (node) == TYPE_DECL)
320 node = TREE_TYPE (node);
322 node = canonical_type_variant (node);
327 /* Add NODE as a substitution candidate. NODE must not already be on
328 the list of candidates. */
331 add_substitution (node)
337 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
338 tree_code_name[TREE_CODE (node)], (void *) node);
340 /* Get the canonicalized substitution candidate for NODE. */
341 c = canonicalize_for_substitution (node);
342 if (DEBUG_MANGLE && c != node)
343 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
344 tree_code_name[TREE_CODE (node)], (void *) node);
348 /* Make sure NODE isn't already a candidate. */
351 for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
353 tree candidate = VARRAY_TREE (G.substitutions, i);
355 && node == candidate)
357 && TYPE_P (candidate)
358 && same_type_p (node, candidate)))
359 my_friendly_abort (20000524);
362 #endif /* ENABLE_CHECKING */
364 /* Put the decl onto the varray of substitution candidates. */
365 VARRAY_PUSH_TREE (G.substitutions, node);
368 dump_substitution_candidates ();
371 /* Helper function for find_substitution. Returns non-zero if NODE,
372 which may be a decl or a CLASS_TYPE, is a template-id with template
373 name of substitution_index[INDEX] in the ::std namespace. */
376 is_std_substitution (node, index)
378 substitution_identifier_index_t index;
385 type = TREE_TYPE (node);
388 else if (CLASS_TYPE_P (node))
391 decl = TYPE_NAME (node);
394 /* These are not the droids you're looking for. */
397 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
398 && TYPE_LANG_SPECIFIC (type)
399 && CLASSTYPE_TEMPLATE_INFO (type)
400 && (DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
401 == subst_identifiers[index]));
404 /* Helper function for find_substitution. Returns non-zero if NODE,
405 which may be a decl or a CLASS_TYPE, is the template-id
406 ::std::identifier<char>, where identifier is
407 substitution_index[INDEX]. */
410 is_std_substitution_char (node, index)
412 substitution_identifier_index_t index;
415 /* Check NODE's name is ::std::identifier. */
416 if (!is_std_substitution (node, index))
418 /* Figure out its template args. */
420 args = DECL_TI_ARGS (node);
421 else if (CLASS_TYPE_P (node))
422 args = CLASSTYPE_TI_ARGS (node);
424 /* Oops, not a template. */
426 /* NODE's template arg list should be <char>. */
428 TREE_VEC_LENGTH (args) == 1
429 && TREE_VEC_ELT (args, 0) == char_type_node;
432 /* Check whether a substitution should be used to represent NODE in
435 First, check standard special-case substitutions.
437 <substitution> ::= St
444 # ::std::basic_string
447 # ::std::basic_string<char,
448 ::std::char_traits<char>,
449 ::std::allocator<char> >
452 # ::std::basic_istream<char, ::std::char_traits<char> >
455 # ::std::basic_ostream<char, ::std::char_traits<char> >
458 # ::std::basic_iostream<char, ::std::char_traits<char> >
460 Then examine the stack of currently available substitution
461 candidates for entities appearing earlier in the same mangling
463 If a substitution is found, write its mangled representation and
464 return non-zero. If none is found, just return zero. */
467 find_substitution (node)
471 int size = VARRAY_ACTIVE_SIZE (G.substitutions);
476 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
477 tree_code_name[TREE_CODE (node)], (void *) node);
479 /* Obtain the canonicalized substitution representation for NODE.
480 This is what we'll compare against. */
481 node = canonicalize_for_substitution (node);
483 /* Check for builtin substitutions. */
485 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
486 type = TYPE_P (node) ? node : TREE_TYPE (node);
488 /* Check for std::allocator. */
490 && is_std_substitution (decl, SUBID_ALLOCATOR)
491 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
497 /* Check for std::basic_string. */
498 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
502 /* If this is a type (i.e. a fully-qualified template-id),
504 std::basic_string <char,
505 std::char_traits<char>,
506 std::allocator<char> > . */
507 if (CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED
508 && CLASSTYPE_USE_TEMPLATE (type))
510 tree args = CLASSTYPE_TI_ARGS (type);
511 if (TREE_VEC_LENGTH (args) == 3
512 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
513 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
515 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
524 /* Substitute for the template name only if this isn't a type. */
531 /* Check for basic_{i,o,io}stream. */
533 && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED
534 && CLASS_TYPE_P (type)
535 && CLASSTYPE_USE_TEMPLATE (type)
536 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
538 /* First, check for the template
539 args <char, std::char_traits<char> > . */
540 tree args = CLASSTYPE_TI_ARGS (type);
541 if (TREE_VEC_LENGTH (args) == 2
542 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
543 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
546 /* Got them. Is this basic_istream? */
547 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
548 if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
553 /* Or basic_ostream? */
554 else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
559 /* Or basic_iostream? */
560 else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
568 /* Check for namespace std. */
569 if (decl && DECL_NAMESPACE_STD_P (decl))
575 /* Now check the list of available substitutions for this mangling
577 for (i = 0; i < size; ++i)
579 tree candidate = VARRAY_TREE (G.substitutions, i);
580 /* NODE is a matched to a candidate if it's the same decl node or
581 if it's the same type. */
582 if (decl == candidate
583 || (TYPE_P (candidate) && type && TYPE_P (type)
584 && same_type_p (type, candidate))
585 || NESTED_TEMPLATE_MATCH (node, candidate))
587 write_substitution (i);
592 /* No substitution found. */
597 /* <mangled-name> ::= _Z <encoding> */
600 write_mangled_name (decl)
603 MANGLE_TRACE_TREE ("mangled-name", decl);
605 if (DECL_LANG_SPECIFIC (decl)
606 && DECL_EXTERN_C_FUNCTION_P (decl)
607 && ! DECL_OVERLOADED_OPERATOR_P (decl))
608 /* The standard notes:
609 "The <encoding> of an extern "C" function is treated like
610 global-scope data, i.e. as its <source-name> without a type."
611 We cannot write overloaded operators that way though,
612 because it contains characters invalid in assembler. */
613 write_source_name (DECL_NAME (decl));
615 /* C++ name; needs to be mangled. */
618 write_encoding (decl);
622 /* <encoding> ::= <function name> <bare-function-type>
626 write_encoding (decl)
629 MANGLE_TRACE_TREE ("encoding", decl);
631 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
633 /* For overloaded operators write just the mangled name
634 without arguments. */
635 if (DECL_OVERLOADED_OPERATOR_P (decl))
636 write_name (decl, /*ignore_local_scope=*/0);
638 write_source_name (DECL_NAME (decl));
642 write_name (decl, /*ignore_local_scope=*/0);
643 if (TREE_CODE (decl) == FUNCTION_DECL)
647 if (decl_is_template_id (decl, NULL))
648 fn_type = get_mostly_instantiated_function_type (decl, NULL, NULL);
650 fn_type = TREE_TYPE (decl);
652 write_bare_function_type (fn_type,
653 (!DECL_CONSTRUCTOR_P (decl)
654 && !DECL_DESTRUCTOR_P (decl)
655 && !DECL_CONV_FN_P (decl)
656 && decl_is_template_id (decl, NULL)),
661 /* <name> ::= <unscoped-name>
662 ::= <unscoped-template-name> <template-args>
666 If IGNORE_LOCAL_SCOPE is non-zero, this production of <name> is
667 called from <local-name>, which mangles the enclosing scope
668 elsewhere and then uses this function to mangle just the part
669 underneath the function scope. So don't use the <local-name>
670 production, to avoid an infinite recursion. */
673 write_name (decl, ignore_local_scope)
675 int ignore_local_scope;
679 MANGLE_TRACE_TREE ("name", decl);
681 if (TREE_CODE (decl) == TYPE_DECL)
683 /* In case this is a typedef, fish out the corresponding
684 TYPE_DECL for the main variant. */
685 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
686 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
689 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
691 /* A decl in :: or ::std scope is treated specially. The former is
692 mangled using <unscoped-name> or <unscoped-template-name>, the
693 latter with a special substitution. Also, a name that is
694 directly in a local function scope is also mangled with
695 <unscoped-name> rather than a full <nested-name>. */
697 || context == global_namespace
698 || DECL_NAMESPACE_STD_P (context)
699 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
702 /* Is this a template instance? */
703 if (decl_is_template_id (decl, &template_info))
705 /* Yes: use <unscoped-template-name>. */
706 write_unscoped_template_name (TI_TEMPLATE (template_info));
707 write_template_args (TI_ARGS (template_info));
710 /* Everything else gets an <unqualified-name>. */
711 write_unscoped_name (decl);
715 /* Handle local names, unless we asked not to (that is, invoked
716 under <local-name>, to handle only the part of the name under
718 if (!ignore_local_scope)
720 /* Scan up the list of scope context, looking for a
721 function. If we find one, this entity is in local
722 function scope. local_entity tracks context one scope
723 level down, so it will contain the element that's
724 directly in that function's scope, either decl or one of
725 its enclosing scopes. */
726 tree local_entity = decl;
727 while (context != NULL && context != global_namespace)
729 /* Make sure we're always dealing with decls. */
730 if (context != NULL && TYPE_P (context))
731 context = TYPE_NAME (context);
732 /* Is this a function? */
733 if (TREE_CODE (context) == FUNCTION_DECL)
735 /* Yes, we have local scope. Use the <local-name>
736 production for the innermost function scope. */
737 write_local_name (context, local_entity, decl);
740 /* Up one scope level. */
741 local_entity = context;
742 context = CP_DECL_CONTEXT (context);
745 /* No local scope found? Fall through to <nested-name>. */
748 /* Other decls get a <nested-name> to encode their scope. */
749 write_nested_name (decl);
753 /* <unscoped-name> ::= <unqualified-name>
754 ::= St <unqualified-name> # ::std:: */
757 write_unscoped_name (decl)
760 tree context = CP_DECL_CONTEXT (decl);
762 MANGLE_TRACE_TREE ("unscoped-name", decl);
764 /* Is DECL in ::std? */
765 if (DECL_NAMESPACE_STD_P (context))
768 write_unqualified_name (decl);
770 /* If not, it should be either in the global namespace, or directly
771 in a local function scope. */
772 else if (context == global_namespace
774 || TREE_CODE (context) == FUNCTION_DECL)
775 write_unqualified_name (decl);
777 my_friendly_abort (20000521);
780 /* <unscoped-template-name> ::= <unscoped-name>
781 ::= <substitution> */
784 write_unscoped_template_name (decl)
787 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
789 if (find_substitution (decl))
791 write_unscoped_name (decl);
792 add_substitution (decl);
795 /* Write the nested name, including CV-qualifiers, of DECL.
797 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
798 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
800 <CV-qualifiers> ::= [r] [V] [K] */
803 write_nested_name (decl)
808 MANGLE_TRACE_TREE ("nested-name", decl);
812 /* Write CV-qualifiers, if this is a member function. */
813 if (TREE_CODE (decl) == FUNCTION_DECL
814 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
816 if (DECL_VOLATILE_MEMFUNC_P (decl))
818 if (DECL_CONST_MEMFUNC_P (decl))
822 /* Is this a template instance? */
823 if (decl_is_template_id (decl, &template_info))
825 /* Yes, use <template-prefix>. */
826 write_template_prefix (decl);
827 write_template_args (TI_ARGS (template_info));
831 /* No, just use <prefix> */
832 write_prefix (DECL_CONTEXT (decl));
833 write_unqualified_name (decl);
838 /* <prefix> ::= <prefix> <unqualified-name>>
839 ::= <template-prefix> <template-args>
841 ::= <substitution> */
848 /* Non-NULL if NODE represents a template-id. */
849 tree template_info = NULL;
851 MANGLE_TRACE_TREE ("prefix", node);
854 || node == global_namespace)
857 if (find_substitution (node))
861 /* Node is a decl. */
863 /* If this is a function decl, that means we've hit function
864 scope, so this prefix must be for a local name. In this
865 case, we're under the <local-name> production, which encodes
866 the enclosing function scope elsewhere. So don't continue
868 if (TREE_CODE (node) == FUNCTION_DECL)
872 decl_is_template_id (decl, &template_info);
875 /* Node is a type. */
877 decl = TYPE_NAME (node);
878 if (CLASSTYPE_TEMPLATE_ID_P (node))
879 template_info = CLASSTYPE_TEMPLATE_INFO (node);
882 if (template_info != NULL)
885 write_template_prefix (decl);
886 write_template_args (TI_ARGS (template_info));
891 write_prefix (CP_DECL_CONTEXT (decl));
892 write_unqualified_name (decl);
895 add_substitution (node);
898 /* <template-prefix> ::= <prefix> <template component>
899 ::= <substitution> */
902 write_template_prefix (node)
905 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
906 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
907 tree context = CP_DECL_CONTEXT (decl);
912 MANGLE_TRACE_TREE ("template-prefix", node);
914 /* Find the template decl. */
915 if (decl_is_template_id (decl, &template_info))
916 template = TI_TEMPLATE (template_info);
917 else if (CLASSTYPE_TEMPLATE_ID_P (type))
918 template = CLASSTYPE_TI_TEMPLATE (type);
920 /* Oops, not a template. */
921 my_friendly_abort (20000524);
923 /* For a member template, though, the template name for the
924 innermost name must have all the outer template levels
925 instantiated. For instance, consider
927 template<typename T> struct Outer {
928 template<typename U> struct Inner {};
931 The template name for `Inner' in `Outer<int>::Inner<float>' is
932 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
933 levels separately, so there's no TEMPLATE_DECL available for this
934 (there's only `Outer<T>::Inner<U>').
936 In order to get the substitutions right, we create a special
937 TREE_LIST to represent the substitution candidate for a nested
938 template. The TREE_PURPOSE is the template's context, fully
939 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
942 So, for the example above, `Outer<int>::Inner' is represented as a
943 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
944 and whose value is `Outer<T>::Inner<U>'. */
945 if (TYPE_P (context))
946 substitution = build_tree_list (context, template);
948 substitution = template;
950 if (find_substitution (substitution))
953 write_prefix (context);
954 write_unqualified_name (decl);
956 add_substitution (substitution);
959 /* We don't need to handle thunks, vtables, or VTTs here. Those are
960 mangled through special entry points.
962 <unqualified-name> ::= <operator-name>
967 write_unqualified_name (decl)
970 MANGLE_TRACE_TREE ("unqualified-name", decl);
972 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
973 write_special_name_constructor (decl);
974 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
975 write_special_name_destructor (decl);
976 else if (DECL_CONV_FN_P (decl))
978 /* Conversion operator. Handle it right here.
979 <operator> ::= cv <type> */
981 write_type (TREE_TYPE (DECL_NAME (decl)));
983 else if (DECL_OVERLOADED_OPERATOR_P (decl))
985 operator_name_info_t *oni;
986 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
987 oni = assignment_operator_name_info;
989 oni = operator_name_info;
991 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
994 write_source_name (DECL_NAME (decl));
997 /* Non-termial <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
999 <source-name> ::= </length/ number> <identifier> */
1002 write_source_name (identifier)
1005 MANGLE_TRACE_TREE ("source-name", identifier);
1007 /* Never write the whole template-id name including the template
1008 arguments; we only want the template name. */
1009 if (IDENTIFIER_TEMPLATE (identifier))
1010 identifier = IDENTIFIER_TEMPLATE (identifier);
1012 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1013 write_identifier (IDENTIFIER_POINTER (identifier));
1016 /* Non-terminal <number>.
1018 <number> ::= [n] </decimal integer/> */
1021 write_number (number, unsigned_p, base)
1022 unsigned HOST_WIDE_INT number;
1026 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1027 unsigned HOST_WIDE_INT n;
1028 unsigned HOST_WIDE_INT m = 1;
1030 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1033 number = -((HOST_WIDE_INT) number);
1036 /* Figure out how many digits there are. */
1044 /* Write them out. */
1047 int digit = number / m;
1048 write_char (digits[digit]);
1049 number -= digit * m;
1053 my_friendly_assert (number == 0, 20000407);
1056 /* Write out an integeral CST in decimal. */
1059 write_integer_cst (cst)
1062 if (tree_int_cst_sgn (cst) >= 0)
1064 if (TREE_INT_CST_HIGH (cst) != 0)
1065 sorry ("mangling very large integers");
1066 write_unsigned_number (TREE_INT_CST_LOW (cst));
1069 write_signed_number (tree_low_cst (cst, 0));
1072 /* Non-terminal <identifier>.
1074 <identifier> ::= </unqualified source code identifier> */
1077 write_identifier (identifier)
1078 const char *identifier;
1080 MANGLE_TRACE ("identifier", identifier);
1081 write_string (identifier);
1084 /* Handle constructor productions of non-terminal <special-name>.
1085 CTOR is a constructor FUNCTION_DECL.
1087 <special-name> ::= C1 # complete object constructor
1088 ::= C2 # base object constructor
1089 ::= C3 # complete object allocating constructor
1091 Currently, allocating constructors are never used.
1093 We also need to provide unique mangled names (which should never be
1094 exported) for the constructor that takes an in-charge parameter,
1095 and for a constructor whose name is the same as its class's name.
1096 We use "C*INTERNAL*" for these. */
1099 write_special_name_constructor (ctor)
1102 if (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1103 /* Even though we don't ever emit a definition of the
1104 old-style destructor, we still have to consider entities
1105 (like static variables) nested inside it. */
1106 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1107 write_string ("C1");
1108 else if (DECL_BASE_CONSTRUCTOR_P (ctor))
1109 write_string ("C2");
1111 my_friendly_abort (20001115);
1114 /* Handle destructor productions of non-terminal <special-name>.
1115 DTOR is a denstructor FUNCTION_DECL.
1117 <special-name> ::= D0 # deleting (in-charge) destructor
1118 ::= D1 # complete object (in-charge) destructor
1119 ::= D2 # base object (not-in-charge) destructor
1121 We also need to provide unique mngled names for old-ABI
1122 destructors, sometimes. These should only be used internally. We
1123 use "D*INTERNAL*" for these. */
1126 write_special_name_destructor (dtor)
1129 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1130 write_string ("D0");
1131 else if (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1132 /* Even though we don't ever emit a definition of the
1133 old-style destructor, we still have to consider entities
1134 (like static variables) nested inside it. */
1135 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1136 write_string ("D1");
1137 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1138 write_string ("D2");
1140 my_friendly_abort (20001115);
1143 /* Return the discriminator for ENTITY appearing inside
1144 FUNCTION. The discriminator is the lexical ordinal of VAR among
1145 entities with the same name in the same FUNCTION. */
1148 discriminator_for_local_entity (entity)
1154 /* Assume this is the only local entity with this name. */
1157 /* For now, we don't discriminate amongst local variables. */
1158 if (TREE_CODE (entity) != TYPE_DECL)
1161 /* Scan the list of local classes. */
1162 entity = TREE_TYPE (entity);
1163 for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
1164 if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1165 && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1168 return discriminator;
1171 /* Return the discriminator for STRING, a string literal used inside
1172 FUNCTION. The disciminator is the lexical ordinal of STRING among
1173 string literals used in FUNCTION. */
1176 discriminator_for_string_literal (function, string)
1177 tree function ATTRIBUTE_UNUSED;
1178 tree string ATTRIBUTE_UNUSED;
1180 /* For now, we don't discriminate amongst string literals. */
1184 /* <discriminator> := _ <number>
1186 The discriminator is used only for the second and later occurrences
1187 of the same name within a single function. In this case <number> is
1188 n - 2, if this is the nth occurrence, in lexical order. */
1191 write_discriminator (discriminator)
1194 /* If discriminator is zero, don't write anything. Otherwise... */
1195 if (discriminator > 0)
1198 /* The number is omitted for discriminator == 1. Beyond 1, the
1199 numbering starts at 0. */
1200 if (discriminator > 1)
1201 write_unsigned_number (discriminator - 2);
1205 /* Mangle the name of a function-scope entity. FUNCTION is the
1206 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1207 the entity itself. LOCAL_ENTITY is the entity that's directly
1208 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1211 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1212 := Z <function encoding> E s [<discriminator>] */
1215 write_local_name (function, local_entity, entity)
1220 MANGLE_TRACE_TREE ("local-name", entity);
1223 write_encoding (function);
1225 if (TREE_CODE (entity) == STRING_CST)
1228 write_discriminator (discriminator_for_string_literal (function,
1233 /* Now the <entity name>. Let write_name know its being called
1234 from <local-name>, so it doesn't try to process the enclosing
1235 function scope again. */
1236 write_name (entity, /*ignore_local_scope=*/1);
1237 write_discriminator (discriminator_for_local_entity (local_entity));
1241 /* Non-terminals <type> and <CV-qualifier>.
1243 <type> ::= <builtin-type>
1245 ::= <class-enum-type>
1247 ::= <pointer-to-member-type>
1248 ::= <template-param>
1251 ::= P <type> # pointer-to
1252 ::= R <type> # reference-to
1253 ::= C <type> # complex pair (C 2000)
1254 ::= G <type> # imaginary (C 2000) [not supported]
1255 ::= U <source-name> <type> # vendor extended type qualifier
1257 TYPE is a type node. */
1263 /* This gets set to non-zero if TYPE turns out to be a (possibly
1264 CV-qualified) builtin type. */
1265 int is_builtin_type = 0;
1267 MANGLE_TRACE_TREE ("type", type);
1269 if (type == error_mark_node)
1272 if (find_substitution (type))
1275 if (write_CV_qualifiers_for_type (type) > 0)
1276 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1277 mangle the unqualified type. The recursive call is needed here
1278 since both the qualified and uqualified types are substitution
1280 write_type (TYPE_MAIN_VARIANT (type));
1283 /* See through any typedefs. */
1284 type = TYPE_MAIN_VARIANT (type);
1286 switch (TREE_CODE (type))
1290 case INTEGER_TYPE: /* Includes wchar_t. */
1292 /* If this is a typedef, TYPE may not be one of
1293 the standard builtin type nodes, but an alias of one. Use
1294 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1295 write_builtin_type (TYPE_MAIN_VARIANT (type));
1301 write_type (TREE_TYPE (type));
1306 write_function_type (type);
1312 /* A pointer-to-member function is represented as a special
1313 RECORD_TYPE, so check for this first. */
1314 if (TYPE_PTRMEMFUNC_P (type))
1315 write_pointer_to_member_type (type);
1317 write_class_enum_type (type);
1321 /* We handle TYPENAME_TYPEs like ordinary nested names. */
1322 write_nested_name (TYPE_STUB_DECL (type));
1326 write_array_type (type);
1330 /* A pointer-to-member variable is represented by a POINTER_TYPE
1331 to an OFFSET_TYPE, so check for this first. */
1332 if (TYPE_PTRMEM_P (type))
1333 write_pointer_to_member_type (type);
1337 write_type (TREE_TYPE (type));
1341 case REFERENCE_TYPE:
1343 write_type (TREE_TYPE (type));
1346 case TEMPLATE_TYPE_PARM:
1347 case TEMPLATE_PARM_INDEX:
1348 write_template_param (type);
1351 case TEMPLATE_TEMPLATE_PARM:
1352 write_template_template_param (type);
1355 case BOUND_TEMPLATE_TEMPLATE_PARM:
1356 write_template_template_param (type);
1358 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1362 write_pointer_to_member_type (build_pointer_type (type));
1366 write_string ("U8__vector");
1367 write_type (TREE_TYPE (type));
1371 my_friendly_abort (20000409);
1375 /* Types other than builtin types are substitution candidates. */
1376 if (!is_builtin_type)
1377 add_substitution (type);
1380 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1381 CV-qualifiers written for TYPE.
1383 <CV-qualifiers> ::= [r] [V] [K] */
1386 write_CV_qualifiers_for_type (type)
1389 int num_qualifiers = 0;
1391 /* The order is specified by:
1393 "In cases where multiple order-insensitive qualifiers are
1394 present, they should be ordered 'K' (closest to the base type),
1395 'V', 'r', and 'U' (farthest from the base type) ..." */
1397 if (CP_TYPE_RESTRICT_P (type))
1402 if (CP_TYPE_VOLATILE_P (type))
1407 if (CP_TYPE_CONST_P (type))
1413 return num_qualifiers;
1416 /* Non-terminal <builtin-type>.
1418 <builtin-type> ::= v # void
1423 ::= h # unsigned char
1425 ::= t # unsigned short
1427 ::= j # unsigned int
1429 ::= m # unsigned long
1430 ::= x # long long, __int64
1431 ::= y # unsigned long long, __int64
1432 ::= n # __int128 [not supported]
1433 ::= o # unsigned __int128 [not supported]
1436 ::= e # long double, __float80
1437 ::= g # __float128 [not supported]
1438 ::= u <source-name> # vendor extended type */
1441 write_builtin_type (type)
1444 switch (TREE_CODE (type))
1455 /* If this is size_t, get the underlying int type. */
1456 if (TYPE_IS_SIZETYPE (type))
1457 type = TYPE_DOMAIN (type);
1459 /* TYPE may still be wchar_t, since that isn't in
1460 integer_type_nodes. */
1461 if (type == wchar_type_node)
1463 else if (TYPE_FOR_JAVA (type))
1464 write_java_integer_type_codes (type);
1468 /* Assume TYPE is one of the shared integer type nodes. Find
1469 it in the array of these nodes. */
1471 for (itk = 0; itk < itk_none; ++itk)
1472 if (type == integer_types[itk])
1474 /* Print the corresponding single-letter code. */
1475 write_char (integer_type_codes[itk]);
1479 if (itk == itk_none)
1481 tree t = type_for_mode (TYPE_MODE (type), TREE_UNSIGNED (type));
1483 /* Couldn't find this type. */
1484 my_friendly_abort (20000408);
1492 if (type == float_type_node
1493 || type == java_float_type_node)
1495 else if (type == double_type_node
1496 || type == java_double_type_node)
1498 else if (type == long_double_type_node)
1501 my_friendly_abort (20000409);
1505 my_friendly_abort (20000509);
1509 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1510 METHOD_TYPE. The return type is mangled before the parameter
1513 <function-type> ::= F [Y] <bare-function-type> E */
1516 write_function_type (type)
1519 MANGLE_TRACE_TREE ("function-type", type);
1522 /* We don't track whether or not a type is `extern "C"'. Note that
1523 you can have an `extern "C"' function that does not have
1524 `extern "C"' type, and vice versa:
1526 extern "C" typedef void function_t();
1527 function_t f; // f has C++ linkage, but its type is
1530 typedef void function_t();
1531 extern "C" function_t f; // Vice versa.
1534 write_bare_function_type (type, /*include_return_type_p=*/1,
1539 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1540 METHOD_TYPE. If INCLUDE_RETURN_TYPE is non-zero, the return value
1541 is mangled before the parameter types. If non-NULL, DECL is
1542 FUNCTION_DECL for the function whose type is being emitted.
1544 <bare-function-type> ::= </signature/ type>+ */
1547 write_bare_function_type (type, include_return_type_p, decl)
1549 int include_return_type_p;
1552 MANGLE_TRACE_TREE ("bare-function-type", type);
1554 /* Mangle the return type, if requested. */
1555 if (include_return_type_p)
1556 write_type (TREE_TYPE (type));
1558 /* Now mangle the types of the arguments. */
1559 write_method_parms (TYPE_ARG_TYPES (type),
1560 TREE_CODE (type) == METHOD_TYPE,
1564 /* Write the mangled representation of a method parameter list of
1565 types given in PARM_TYPES. If METHOD_P is non-zero, the function is
1566 considered a non-static method, and the this parameter is omitted.
1567 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1568 parameters are being emitted. */
1571 write_method_parms (parm_types, method_p, decl)
1576 tree first_parm_type;
1577 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1579 /* Assume this parameter type list is variable-length. If it ends
1580 with a void type, then it's not. */
1583 /* If this is a member function, skip the first arg, which is the
1585 "Member functions do not encode the type of their implicit this
1588 Similarly, there's no need to mangle artificial parameters, like
1589 the VTT parameters for constructors and destructors. */
1592 parm_types = TREE_CHAIN (parm_types);
1593 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1595 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1597 parm_types = TREE_CHAIN (parm_types);
1598 parm_decl = TREE_CHAIN (parm_decl);
1602 for (first_parm_type = parm_types;
1604 parm_types = TREE_CHAIN (parm_types))
1606 tree parm = TREE_VALUE (parm_types);
1607 if (parm == void_type_node)
1609 /* "Empty parameter lists, whether declared as () or
1610 conventionally as (void), are encoded with a void parameter
1612 if (parm_types == first_parm_type)
1614 /* If the parm list is terminated with a void type, it's
1617 /* A void type better be the last one. */
1618 my_friendly_assert (TREE_CHAIN (parm_types) == NULL, 20000523);
1625 /* <builtin-type> ::= z # ellipsis */
1629 /* <class-enum-type> ::= <name> */
1632 write_class_enum_type (type)
1635 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1638 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1641 <template-args> ::= I <template-arg>+ E */
1644 write_template_args (args)
1648 int length = TREE_VEC_LENGTH (args);
1650 MANGLE_TRACE_TREE ("template-args", args);
1652 my_friendly_assert (length > 0, 20000422);
1654 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1656 /* We have nested template args. We want the innermost template
1658 args = TREE_VEC_ELT (args, length - 1);
1659 length = TREE_VEC_LENGTH (args);
1663 for (i = 0; i < length; ++i)
1664 write_template_arg (TREE_VEC_ELT (args, i));
1668 /* <expression> ::= <unary operator-name> <expression>
1669 ::= <binary operator-name> <expression> <expression>
1672 <expr-primary> ::= <template-param>
1673 ::= L <type> <value number> E # literal
1674 ::= L <mangled-name> E # external name */
1677 write_expression (expr)
1680 enum tree_code code;
1682 code = TREE_CODE (expr);
1684 /* Handle pointers-to-members by making them look like expression
1686 if (code == PTRMEM_CST)
1688 expr = build_nt (ADDR_EXPR,
1689 build_nt (SCOPE_REF,
1690 PTRMEM_CST_CLASS (expr),
1691 PTRMEM_CST_MEMBER (expr)));
1692 code = TREE_CODE (expr);
1695 /* Handle template parameters. */
1696 if (code == TEMPLATE_TYPE_PARM
1697 || code == TEMPLATE_TEMPLATE_PARM
1698 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1699 || code == TEMPLATE_PARM_INDEX)
1700 write_template_param (expr);
1701 /* Handle literals. */
1702 else if (TREE_CODE_CLASS (code) == 'c')
1703 write_template_arg_literal (expr);
1704 else if (DECL_P (expr))
1707 write_mangled_name (expr);
1714 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1715 is converted (via qualification conversions) to another
1717 while (TREE_CODE (expr) == NOP_EXPR)
1719 expr = TREE_OPERAND (expr, 0);
1720 code = TREE_CODE (expr);
1723 /* When we bind a variable or function to a non-type template
1724 argument with reference type, we create an ADDR_EXPR to show
1725 the fact that the entity's address has been taken. But, we
1726 don't actually want to output a mangling code for the `&'. */
1727 if (TREE_CODE (expr) == ADDR_EXPR
1729 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
1731 expr = TREE_OPERAND (expr, 0);
1734 write_expression (expr);
1738 code = TREE_CODE (expr);
1741 /* If it wasn't any of those, recursively expand the expression. */
1742 write_string (operator_name_info[(int) code].mangled_name);
1744 /* Handle pointers-to-members specially. */
1745 if (code == SCOPE_REF)
1747 write_type (TREE_OPERAND (expr, 0));
1748 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
1749 write_source_name (TREE_OPERAND (expr, 1));
1751 write_encoding (TREE_OPERAND (expr, 1));
1754 for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
1755 write_expression (TREE_OPERAND (expr, i));
1759 /* Literal subcase of non-terminal <template-arg>.
1761 "Literal arguments, e.g. "A<42L>", are encoded with their type
1762 and value. Negative integer values are preceded with "n"; for
1763 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
1764 encoded as 0, true as 1. If floating-point arguments are accepted
1765 as an extension, their values should be encoded using a
1766 fixed-length lowercase hexadecimal string corresponding to the
1767 internal representation (IEEE on IA-64), high-order bytes first,
1768 without leading zeroes. For example: "Lfbff000000E" is -1.0f." */
1771 write_template_arg_literal (value)
1774 tree type = TREE_TYPE (value);
1778 if (TREE_CODE (value) == CONST_DECL)
1779 write_integer_cst (DECL_INITIAL (value));
1780 else if (TREE_CODE (value) == INTEGER_CST)
1782 if (same_type_p (type, boolean_type_node))
1784 if (value == boolean_false_node || integer_zerop (value))
1785 write_unsigned_number (0);
1786 else if (value == boolean_true_node)
1787 write_unsigned_number (1);
1789 my_friendly_abort (20000412);
1792 write_integer_cst (value);
1794 else if (TREE_CODE (value) == REAL_CST)
1796 #ifdef CROSS_COMPILE
1797 static int explained;
1801 sorry ("real-valued template parameters when cross-compiling");
1806 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1807 write_number (((unsigned char *)
1808 &TREE_REAL_CST (value))[i],
1814 my_friendly_abort (20000412);
1819 /* Non-terminal <tempalate-arg>.
1821 <template-arg> ::= <type> # type
1822 ::= L <type> </value/ number> E # literal
1823 ::= LZ <name> E # external name
1824 ::= X <expression> E # expression */
1827 write_template_arg (node)
1830 enum tree_code code = TREE_CODE (node);
1832 MANGLE_TRACE_TREE ("template-arg", node);
1834 /* A template template paramter's argument list contains TREE_LIST
1835 nodes of which the value field is the the actual argument. */
1836 if (code == TREE_LIST)
1838 node = TREE_VALUE (node);
1839 /* If it's a decl, deal with its type instead. */
1842 node = TREE_TYPE (node);
1843 code = TREE_CODE (node);
1849 else if (code == TEMPLATE_DECL)
1850 /* A template appearing as a template arg is a template template arg. */
1851 write_template_template_arg (node);
1852 else if (DECL_P (node))
1856 write_encoding (node);
1859 else if (TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST)
1860 write_template_arg_literal (node);
1863 /* Template arguments may be expressions. */
1865 write_expression (node);
1870 /* <template-template-arg>
1872 ::= <substitution> */
1875 write_template_template_arg (tree decl)
1877 MANGLE_TRACE_TREE ("template-template-arg", decl);
1879 if (find_substitution (decl))
1881 write_name (decl, /*ignore_local_scope=*/0);
1882 add_substitution (decl);
1886 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
1888 <array-type> ::= A [</dimension/ number>] _ </element/ type>
1889 ::= A <expression> _ </element/ type>
1891 "Array types encode the dimension (number of elements) and the
1892 element type. For variable length arrays, the dimension (but not
1893 the '_' separator) is omitted." */
1896 write_array_type (type)
1900 if (TYPE_DOMAIN (type))
1905 index_type = TYPE_DOMAIN (type);
1906 /* The INDEX_TYPE gives the upper and lower bounds of the
1908 max = TYPE_MAX_VALUE (index_type);
1909 if (TREE_CODE (max) == INTEGER_CST)
1911 /* The ABI specifies that we should mangle the number of
1912 elements in the array, not the largest allowed index. */
1913 max = size_binop (PLUS_EXPR, max, size_one_node);
1914 write_unsigned_number (tree_low_cst (max, 1));
1917 write_expression (TREE_OPERAND (max, 0));
1920 write_type (TREE_TYPE (type));
1923 /* Non-terminal <pointer-to-member-type> for pointer-to-member
1924 variables. TYPE is a pointer-to-member POINTER_TYPE.
1926 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
1929 write_pointer_to_member_type (type)
1933 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
1934 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1937 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
1938 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
1939 TEMPLATE_PARM_INDEX.
1941 <template-param> ::= T </parameter/ number> _ */
1944 write_template_param (parm)
1949 MANGLE_TRACE_TREE ("template-parm", parm);
1951 switch (TREE_CODE (parm))
1953 case TEMPLATE_TYPE_PARM:
1954 case TEMPLATE_TEMPLATE_PARM:
1955 case BOUND_TEMPLATE_TEMPLATE_PARM:
1956 parm_index = TEMPLATE_TYPE_IDX (parm);
1959 case TEMPLATE_PARM_INDEX:
1960 parm_index = TEMPLATE_PARM_IDX (parm);
1964 my_friendly_abort (20000523);
1968 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
1969 earliest template param denoted by `_'. */
1971 write_unsigned_number (parm_index - 1);
1975 /* <template-template-param>
1976 ::= <template-param>
1977 ::= <substitution> */
1980 write_template_template_param (parm)
1983 tree template = NULL_TREE;
1985 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
1986 template template parameter. The substitution candidate here is
1987 only the template. */
1988 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
1991 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
1992 if (find_substitution (template))
1996 /* <template-param> encodes only the template parameter position,
1997 not its template arguments, which is fine here. */
1998 write_template_param (parm);
2000 add_substitution (template);
2003 /* Non-terminal <substitution>.
2005 <substitution> ::= S <seq-id> _
2009 write_substitution (seq_id)
2012 MANGLE_TRACE ("substitution", "");
2016 write_number (seq_id - 1, /*unsigned=*/1, 36);
2020 /* Start mangling a new name or type. */
2025 obstack_free (&G.name_obstack, obstack_base (&G.name_obstack));
2028 /* Done with mangling. Return the generated mangled name. */
2030 static inline const char *
2033 /* Clear all the substitutions. */
2034 VARRAY_POP_ALL (G.substitutions);
2036 /* Null-terminate the string. */
2039 return (const char *) obstack_base (&G.name_obstack);
2042 /* Initialize data structures for mangling. */
2047 gcc_obstack_init (&G.name_obstack);
2048 VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
2050 /* Cache these identifiers for quick comparison when checking for
2051 standard substitutions. */
2052 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2053 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2054 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2055 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2056 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2057 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2060 /* Generate the mangled name of DECL. */
2063 mangle_decl_string (decl)
2070 if (TREE_CODE (decl) == TYPE_DECL)
2071 write_type (TREE_TYPE (decl));
2072 else if (/* The names of `extern "C"' functions are not mangled. */
2073 (TREE_CODE (decl) == FUNCTION_DECL
2074 /* If there's no DECL_LANG_SPECIFIC, it's a function built
2075 by language-independent code, which never builds
2076 functions with C++ linkage. */
2077 && (!DECL_LANG_SPECIFIC (decl)
2078 || DECL_EXTERN_C_FUNCTION_P (decl)))
2079 /* The names of global variables aren't mangled either. */
2080 || (TREE_CODE (decl) == VAR_DECL
2081 && CP_DECL_CONTEXT (decl) == global_namespace))
2082 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
2085 write_mangled_name (decl);
2086 if (DECL_LANG_SPECIFIC (decl)
2087 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
2088 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
2089 /* We need a distinct mangled name for these entities, but
2090 we should never actually output it. So, we append some
2091 characters the assembler won't like. */
2092 write_string (" *INTERNAL* ");
2095 result = finish_mangling ();
2097 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2101 /* Create an identifier for the external mangled name of DECL. */
2107 tree id = get_identifier (mangle_decl_string (decl));
2109 SET_DECL_ASSEMBLER_NAME (decl, id);
2112 /* Generate the mangled representation of TYPE. */
2115 mangle_type_string (type)
2122 result = finish_mangling ();
2124 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2128 /* Create an identifier for the mangled representation of TYPE. */
2134 return get_identifier (mangle_type_string (type));
2137 /* Create an identifier for the mangled name of a special component
2138 for belonging to TYPE. CODE is the ABI-specified code for this
2142 mangle_special_for_type (type, code)
2148 /* We don't have an actual decl here for the special component, so
2149 we can't just process the <encoded-name>. Instead, fake it. */
2152 /* Start the mangling. */
2153 write_string ("_Z");
2154 write_string (code);
2158 result = finish_mangling ();
2161 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2163 return get_identifier (result);
2166 /* Create an identifier for the mangled representation of the typeinfo
2167 structure for TYPE. */
2170 mangle_typeinfo_for_type (type)
2173 return mangle_special_for_type (type, "TI");
2176 /* Create an identifier for the mangled name of the NTBS containing
2177 the mangled name of TYPE. */
2180 mangle_typeinfo_string_for_type (type)
2183 return mangle_special_for_type (type, "TS");
2186 /* Create an identifier for the mangled name of the vtable for TYPE. */
2189 mangle_vtbl_for_type (type)
2192 return mangle_special_for_type (type, "TV");
2195 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2198 mangle_vtt_for_type (type)
2201 return mangle_special_for_type (type, "TT");
2204 /* Return an identifier for a construction vtable group. TYPE is
2205 the most derived class in the hierarchy; BINFO is the base
2206 subobject for which this construction vtable group will be used.
2208 This mangling isn't part of the ABI specification; in the ABI
2209 specification, the vtable group is dumped in the same COMDAT as the
2210 main vtable, and is referenced only from that vtable, so it doesn't
2211 need an external name. For binary formats without COMDAT sections,
2212 though, we need external names for the vtable groups.
2214 We use the production
2216 <special-name> ::= CT <type> <offset number> _ <base type> */
2219 mangle_ctor_vtbl_for_type (type, binfo)
2227 write_string ("_Z");
2228 write_string ("TC");
2230 write_integer_cst (BINFO_OFFSET (binfo));
2232 write_type (BINFO_TYPE (binfo));
2234 result = finish_mangling ();
2236 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2237 return get_identifier (result);
2240 /* Return an identifier for the mangled name of a thunk to FN_DECL.
2241 OFFSET is the initial adjustment to this used to find the vptr. If
2242 VCALL_OFFSET is non-NULL, this is a virtual thunk, and it is the
2243 vtbl offset in bytes.
2245 <special-name> ::= Th <offset number> _ <base encoding>
2246 ::= Tv <offset number> _ <vcall offset number> _
2251 mangle_thunk (fn_decl, offset, vcall_offset)
2260 write_string ("_Z");
2261 /* The <special-name> for virtual thunks is Tv, for non-virtual
2264 if (vcall_offset != 0)
2269 /* For either flavor, write the offset to this. */
2270 write_integer_cst (offset);
2273 /* For a virtual thunk, add the vcall offset. */
2276 /* Virtual thunk. Write the vcall offset and base type name. */
2277 write_integer_cst (vcall_offset);
2282 write_encoding (fn_decl);
2284 result = finish_mangling ();
2286 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2287 return get_identifier (result);
2290 /* Return an identifier for the mangled unqualified name for a
2291 conversion operator to TYPE. This mangling is not specified by the
2292 ABI spec; it is only used internally. */
2295 mangle_conv_op_name_for_type (type)
2300 /* Build the mangling for TYPE. */
2301 const char *mangled_type = mangle_type_string (type);
2302 /* Allocate a temporary buffer for the complete name. */
2303 char *op_name = (char *) xmalloc (strlen ("operator ")
2304 + strlen (mangled_type) + 1);
2305 /* Assemble the mangling. */
2306 strcpy (op_name, "operator ");
2307 strcat (op_name, mangled_type);
2308 /* Find or create an identifier. */
2309 identifier = get_identifier (op_name);
2310 /* Done with the temporary buffer. */
2312 /* Set bits on the identifier so we know later it's a conversion. */
2313 IDENTIFIER_OPNAME_P (identifier) = 1;
2314 IDENTIFIER_TYPENAME_P (identifier) = 1;
2315 /* Hang TYPE off the identifier so it can be found easily later when
2316 performing conversions. */
2317 TREE_TYPE (identifier) = type;
2322 /* Return an identifier for the name of an initialization guard
2323 variable for indicated VARIABLE. */
2326 mangle_guard_variable (variable)
2330 write_string ("_ZGV");
2331 write_name (variable, /*ignore_local_scope=*/0);
2332 return get_identifier (finish_mangling ());
2337 /* Foreign language type mangling section. */
2339 /* How to write the type codes for the integer Java type. */
2342 write_java_integer_type_codes (type)
2345 if (type == java_int_type_node)
2347 else if (type == java_short_type_node)
2349 else if (type == java_byte_type_node)
2351 else if (type == java_char_type_node)
2353 else if (type == java_long_type_node)
2355 else if (type == java_boolean_type_node)
2358 my_friendly_abort (20001207);