1 /* Name mangling for the new standard C++ ABI.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Written by Alex Samuel <sameul@codesourcery.com>
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 ((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));
167 static void write_method_parms PARAMS ((tree, int));
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 /* Append a single character to the end of the mangled
194 #define write_char(CHAR) \
195 obstack_1grow (&G.name_obstack, (CHAR))
197 /* Append a NUL-terminated string to the end of the mangled
199 #define write_string(STRING) \
200 obstack_grow (&G.name_obstack, (STRING), strlen (STRING))
202 /* Return the position at which the next character will be appended to
203 the mangled representation. */
204 #define mangled_position() \
205 obstack_object_size (&G.name_obstack)
207 /* Non-zero if NODE1 and NODE2 are both TREE_LIST nodes and have the
208 same purpose (context, which may be a type) and value (template
209 decl). See write_template_prefix for more information on what this
211 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
212 (TREE_CODE (NODE1) == TREE_LIST \
213 && TREE_CODE (NODE2) == TREE_LIST \
214 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
215 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
216 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
217 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
219 /* Write out a signed quantity in base 10. */
220 #define write_signed_number(NUMBER) \
221 write_number (NUMBER, /*unsigned_p=*/0, 10)
223 /* Write out an unsigned quantity in base 10. */
224 #define write_unsigned_number(NUMBER) \
225 write_number (NUMBER, /*unsigned_p=*/1, 10)
227 /* If DECL is a template instance, return non-zero and, if
228 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
229 Otherwise return zero. */
232 decl_is_template_id (decl, template_info)
236 if (TREE_CODE (decl) == TYPE_DECL)
238 /* TYPE_DECLs are handled specially. Look at its type to decide
239 if this is a template instantiation. */
240 tree type = TREE_TYPE (decl);
242 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
244 if (template_info != NULL)
245 /* For a templated TYPE_DECL, the template info is hanging
247 *template_info = CLASSTYPE_TEMPLATE_INFO (type);
253 /* Check if this is a primary template. */
254 if (DECL_LANG_SPECIFIC (decl) != NULL
255 && DECL_USE_TEMPLATE (decl)
256 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
257 && TREE_CODE (decl) != TEMPLATE_DECL)
259 if (template_info != NULL)
260 /* For most templated decls, the template info is hanging
262 *template_info = DECL_TEMPLATE_INFO (decl);
267 /* It's not a template id. */
271 /* Produce debugging output of current substitution candidates. */
274 dump_substitution_candidates ()
278 fprintf (stderr, " ++ substitutions ");
279 for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
281 tree el = VARRAY_TREE (G.substitutions, i);
282 const char *name = "???";
285 fprintf (stderr, " ");
287 name = IDENTIFIER_POINTER (DECL_NAME (el));
288 else if (TREE_CODE (el) == TREE_LIST)
289 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
290 else if (TYPE_NAME (el))
291 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
292 fprintf (stderr, " S%d_ = ", i - 1);
294 (CP_TYPE_RESTRICT_P (el)
295 || CP_TYPE_VOLATILE_P (el)
296 || CP_TYPE_CONST_P (el)))
297 fprintf (stderr, "CV-");
298 fprintf (stderr, "%s (%s at %p)\n",
299 name, tree_code_name[TREE_CODE (el)], (void *) el);
303 /* Both decls and types can be substitution candidates, but sometimes
304 they refer to the same thing. For instance, a TYPE_DECL and
305 RECORD_TYPE for the same class refer to the same thing, and should
306 be treated accordinginly in substitutions. This function returns a
307 canonicalized tree node representing NODE that is used when adding
308 and substitution candidates and finding matches. */
311 canonicalize_for_substitution (node)
314 /* For a TYPE_DECL, use the type instead. */
315 if (TREE_CODE (node) == TYPE_DECL)
316 node = TREE_TYPE (node);
318 node = canonical_type_variant (node);
323 /* Add NODE as a substitution candidate. NODE must not already be on
324 the list of candidates. */
327 add_substitution (node)
333 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
334 tree_code_name[TREE_CODE (node)], (void *) node);
336 /* Get the canonicalized substitution candidate for NODE. */
337 c = canonicalize_for_substitution (node);
338 if (DEBUG_MANGLE && c != node)
339 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
340 tree_code_name[TREE_CODE (node)], (void *) node);
344 /* Make sure NODE isn't already a candidate. */
347 for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
349 tree candidate = VARRAY_TREE (G.substitutions, i);
351 && node == candidate)
353 && TYPE_P (candidate)
354 && same_type_p (node, candidate)))
355 my_friendly_abort (20000524);
358 #endif /* ENABLE_CHECKING */
360 /* Put the decl onto the varray of substitution candidates. */
361 VARRAY_PUSH_TREE (G.substitutions, node);
364 dump_substitution_candidates ();
367 /* Helper function for find_substitution. Returns non-zero if NODE,
368 which may be a decl or a CLASS_TYPE, is a template-id with template
369 name of substitution_index[INDEX] in the ::std namespace. */
372 is_std_substitution (node, index)
374 substitution_identifier_index_t index;
381 type = TREE_TYPE (node);
384 else if (CLASS_TYPE_P (node))
387 decl = TYPE_NAME (node);
390 /* These are not the droids you're looking for. */
393 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
394 && TYPE_LANG_SPECIFIC (type)
395 && CLASSTYPE_TEMPLATE_INFO (type)
396 && (DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
397 == subst_identifiers[index]));
400 /* Helper function for find_substitution. Returns non-zero if NODE,
401 which may be a decl or a CLASS_TYPE, is the template-id
402 ::std::identifier<char>, where identifier is
403 substitution_index[INDEX]. */
406 is_std_substitution_char (node, index)
408 substitution_identifier_index_t index;
411 /* Check NODE's name is ::std::identifier. */
412 if (!is_std_substitution (node, index))
414 /* Figure out its template args. */
416 args = DECL_TI_ARGS (node);
417 else if (CLASS_TYPE_P (node))
418 args = CLASSTYPE_TI_ARGS (node);
420 /* Oops, not a template. */
422 /* NODE's template arg list should be <char>. */
424 TREE_VEC_LENGTH (args) == 1
425 && TREE_VEC_ELT (args, 0) == char_type_node;
428 /* Check whether a substitution should be used to represent NODE in
431 First, check standard special-case substitutions.
433 <substitution> ::= St
440 # ::std::basic_string
443 # ::std::basic_string<char,
444 ::std::char_traits<char>,
445 ::std::allocator<char> >
448 # ::std::basic_istream<char, ::std::char_traits<char> >
451 # ::std::basic_ostream<char, ::std::char_traits<char> >
454 # ::std::basic_iostream<char, ::std::char_traits<char> >
456 Then examine the stack of currently available substitution
457 candidates for entities appearing earlier in the same mangling
459 If a substitution is found, write its mangled representation and
460 return non-zero. If none is found, just return zero. */
463 find_substitution (node)
467 int size = VARRAY_ACTIVE_SIZE (G.substitutions);
472 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
473 tree_code_name[TREE_CODE (node)], (void *) node);
475 /* Obtain the canonicalized substitution representation for NODE.
476 This is what we'll compare against. */
477 node = canonicalize_for_substitution (node);
479 /* Check for builtin substitutions. */
481 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
482 type = TYPE_P (node) ? node : TREE_TYPE (node);
484 /* Check for std::allocator. */
486 && is_std_substitution (decl, SUBID_ALLOCATOR)
487 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
493 /* Check for std::basic_string. */
494 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
498 /* If this is a type (i.e. a fully-qualified template-id),
500 std::basic_string <char,
501 std::char_traits<char>,
502 std::allocator<char> > . */
503 if (CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED
504 && CLASSTYPE_USE_TEMPLATE (type))
506 tree args = CLASSTYPE_TI_ARGS (type);
507 if (TREE_VEC_LENGTH (args) == 3
508 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
509 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
511 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
520 /* Substitute for the template name only if this isn't a type. */
527 /* Check for basic_{i,o,io}stream. */
529 && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED
530 && CLASS_TYPE_P (type)
531 && CLASSTYPE_USE_TEMPLATE (type)
532 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
534 /* First, check for the template
535 args <char, std::char_traits<char> > . */
536 tree args = CLASSTYPE_TI_ARGS (type);
537 if (TREE_VEC_LENGTH (args) == 2
538 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
539 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
542 /* Got them. Is this basic_istream? */
543 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
544 if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
549 /* Or basic_ostream? */
550 else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
555 /* Or basic_iostream? */
556 else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
564 /* Check for namespace std. */
565 if (decl && DECL_NAMESPACE_STD_P (decl))
571 /* Now check the list of available substitutions for this mangling
573 for (i = 0; i < size; ++i)
575 tree candidate = VARRAY_TREE (G.substitutions, i);
576 /* NODE is a matched to a candidate if it's the same decl node or
577 if it's the same type. */
578 if (decl == candidate
579 || (TYPE_P (candidate) && type && TYPE_P (type)
580 && same_type_p (type, candidate))
581 || NESTED_TEMPLATE_MATCH (node, candidate))
583 write_substitution (i);
588 /* No substitution found. */
593 /* <mangled-name> ::= _Z <encoding> */
596 write_mangled_name (decl)
599 MANGLE_TRACE_TREE ("mangled-name", decl);
601 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
602 /* The standard notes:
603 "The <encoding> of an extern "C" function is treated like
604 global-scope data, i.e. as its <source-name> without a type." */
605 write_source_name (DECL_NAME (decl));
607 /* C++ name; needs to be mangled. */
610 write_encoding (decl);
614 /* <encoding> ::= <function name> <bare-function-type>
618 write_encoding (decl)
621 MANGLE_TRACE_TREE ("encoding", decl);
623 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
625 write_source_name (DECL_NAME (decl));
629 write_name (decl, /*ignore_local_scope=*/0);
630 if (TREE_CODE (decl) == FUNCTION_DECL)
634 if (decl_is_template_id (decl, NULL))
635 fn_type = get_mostly_instantiated_function_type (decl, NULL, NULL);
637 fn_type = TREE_TYPE (decl);
639 write_bare_function_type (fn_type,
640 (!DECL_CONSTRUCTOR_P (decl)
641 && !DECL_DESTRUCTOR_P (decl)
642 && !DECL_CONV_FN_P (decl)
643 && decl_is_template_id (decl, NULL)));
647 /* <name> ::= <unscoped-name>
648 ::= <unscoped-template-name> <template-args>
652 If IGNORE_LOCAL_SCOPE is non-zero, this production of <name> is
653 called from <local-name>, which mangles the enclosing scope
654 elsewhere and then uses this function to mangle just the part
655 underneath the function scope. So don't use the <local-name>
656 production, to avoid an infinite recursion. */
659 write_name (decl, ignore_local_scope)
661 int ignore_local_scope;
665 MANGLE_TRACE_TREE ("name", decl);
667 if (TREE_CODE (decl) == TYPE_DECL)
669 /* In case this is a typedef, fish out the corresponding
670 TYPE_DECL for the main variant. */
671 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
672 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
675 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
677 /* Decls in :: or ::std scope are treated specially. */
679 || context == global_namespace
680 || DECL_NAMESPACE_STD_P (context))
683 /* Is this a template instance? */
684 if (decl_is_template_id (decl, &template_info))
686 /* Yes: use <unscoped-template-name>. */
687 write_unscoped_template_name (TI_TEMPLATE (template_info));
688 write_template_args (TI_ARGS (template_info));
691 /* Everything else gets an <unqualified-name>. */
692 write_unscoped_name (decl);
696 /* Handle local names, unless we asked not to (that is, invoked
697 under <local-name>, to handle only the part of the name under
699 if (!ignore_local_scope)
701 /* Scan up the list of scope context, looking for a
702 function. If we find one, this entity is in local
703 function scope. local_entity tracks context one scope
704 level down, so it will contain the element that's
705 directly in that function's scope, either decl or one of
706 its enclosing scopes. */
707 tree local_entity = decl;
708 while (context != NULL && context != global_namespace)
710 /* Make sure we're always dealing with decls. */
711 if (context != NULL && TYPE_P (context))
712 context = TYPE_NAME (context);
713 /* Is this a function? */
714 if (TREE_CODE (context) == FUNCTION_DECL)
716 /* Yes, we have local scope. Use the <local-name>
717 production for the innermost function scope. */
718 write_local_name (context, local_entity, decl);
721 /* Up one scope level. */
722 local_entity = context;
723 context = CP_DECL_CONTEXT (context);
726 /* No local scope found? Fall through to <nested-name>. */
729 /* Other decls get a <nested-name> to encode their scope. */
730 write_nested_name (decl);
734 /* <unscoped-name> ::= <unqualified-name>
735 ::= St <unqualified-name> # ::std:: */
738 write_unscoped_name (decl)
741 tree context = CP_DECL_CONTEXT (decl);
743 MANGLE_TRACE_TREE ("unscoped-name", decl);
745 /* Is DECL in ::std? */
746 if (DECL_NAMESPACE_STD_P (context))
749 write_unqualified_name (decl);
751 /* If not, it should be in the global namespace. */
752 else if (context == global_namespace || context == NULL)
753 write_unqualified_name (decl);
755 my_friendly_abort (20000521);
758 /* <unscoped-template-name> ::= <unscoped-name>
759 ::= <substitution> */
762 write_unscoped_template_name (decl)
765 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
767 if (find_substitution (decl))
769 write_unscoped_name (decl);
770 add_substitution (decl);
773 /* Write the nested name, including CV-qualifiers, of DECL.
775 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
776 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
778 <CV-qualifiers> ::= [r] [V] [K] */
781 write_nested_name (decl)
786 MANGLE_TRACE_TREE ("nested-name", decl);
790 /* Write CV-qualifiers, if this is a member function. */
791 if (TREE_CODE (decl) == FUNCTION_DECL
792 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
794 if (DECL_VOLATILE_MEMFUNC_P (decl))
796 if (DECL_CONST_MEMFUNC_P (decl))
800 /* Is this a template instance? */
801 if (decl_is_template_id (decl, &template_info))
803 /* Yes, use <template-prefix>. */
804 write_template_prefix (decl);
805 write_template_args (TI_ARGS (template_info));
809 /* No, just use <prefix> */
810 write_prefix (DECL_CONTEXT (decl));
811 write_unqualified_name (decl);
816 /* <prefix> ::= <prefix> <unqualified-name>>
817 ::= <template-prefix> <template-args>
819 ::= <substitution> */
826 /* Non-NULL if NODE represents a template-id. */
827 tree template_info = NULL;
829 MANGLE_TRACE_TREE ("prefix", node);
832 || node == global_namespace)
835 if (find_substitution (node))
839 /* Node is a decl. */
841 /* If this is a function decl, that means we've hit function
842 scope, so this prefix must be for a local name. In this
843 case, we're under the <local-name> production, which encodes
844 the enclosing function scope elsewhere. So don't continue
846 if (TREE_CODE (node) == FUNCTION_DECL)
850 decl_is_template_id (decl, &template_info);
853 /* Node is a type. */
855 decl = TYPE_NAME (node);
856 if (CLASSTYPE_TEMPLATE_ID_P (node))
857 template_info = CLASSTYPE_TEMPLATE_INFO (node);
860 if (template_info != NULL)
863 write_template_prefix (decl);
864 write_template_args (TI_ARGS (template_info));
869 write_prefix (CP_DECL_CONTEXT (decl));
870 write_unqualified_name (decl);
873 add_substitution (node);
876 /* <template-prefix> ::= <prefix> <template component>
877 ::= <substitution> */
880 write_template_prefix (node)
883 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
884 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
885 tree context = CP_DECL_CONTEXT (decl);
890 MANGLE_TRACE_TREE ("template-prefix", node);
892 /* Find the template decl. */
893 if (decl_is_template_id (decl, &template_info))
894 template = TI_TEMPLATE (template_info);
895 else if (CLASSTYPE_TEMPLATE_ID_P (type))
896 template = CLASSTYPE_TI_TEMPLATE (type);
898 /* Oops, not a template. */
899 my_friendly_abort (20000524);
901 /* For a member template, though, the template name for the
902 innermost name must have all the outer template levels
903 instantiated. For instance, consider
905 template<typename T> struct Outer {
906 template<typename U> struct Inner {};
909 The template name for `Inner' in `Outer<int>::Inner<float>' is
910 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
911 levels separately, so there's no TEMPLATE_DECL available for this
912 (there's only `Outer<T>::Inner<U>').
914 In order to get the substitutions right, we create a special
915 TREE_LIST to represent the substitution candidate for a nested
916 template. The TREE_PURPOSE is the template's context, fully
917 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
920 So, for the example above, `Outer<int>::Inner' is represented as a
921 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
922 and whose value is `Outer<T>::Inner<U>'. */
923 if (TYPE_P (context))
924 substitution = build_tree_list (context, template);
926 substitution = template;
928 if (find_substitution (substitution))
931 write_prefix (context);
932 write_unqualified_name (decl);
934 add_substitution (substitution);
937 /* We don't need to handle thunks, vtables, or VTTs here. Those are
938 mangled through special entry points.
940 <unqualified-name> ::= <operator-name>
945 write_unqualified_name (decl)
948 MANGLE_TRACE_TREE ("unqualified-name", decl);
950 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
951 write_special_name_constructor (decl);
952 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
953 write_special_name_destructor (decl);
954 else if (DECL_CONV_FN_P (decl))
956 /* Conversion operator. Handle it right here.
957 <operator> ::= cv <type> */
959 write_type (TREE_TYPE (DECL_NAME (decl)));
961 else if (DECL_OVERLOADED_OPERATOR_P (decl))
963 operator_name_info_t *oni;
964 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
965 oni = assignment_operator_name_info;
967 oni = operator_name_info;
969 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
972 write_source_name (DECL_NAME (decl));
975 /* Non-termial <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
977 <source-name> ::= </length/ number> <identifier> */
980 write_source_name (identifier)
983 MANGLE_TRACE_TREE ("source-name", identifier);
985 /* Never write the whole template-id name including the template
986 arguments; we only want the template name. */
987 if (IDENTIFIER_TEMPLATE (identifier))
988 identifier = IDENTIFIER_TEMPLATE (identifier);
990 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
991 write_identifier (IDENTIFIER_POINTER (identifier));
994 /* Non-terminal <number>.
996 <number> ::= [n] </decimal integer/> */
999 write_number (number, unsigned_p, base)
1000 unsigned HOST_WIDE_INT number;
1004 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1005 unsigned HOST_WIDE_INT n;
1006 unsigned HOST_WIDE_INT m = 1;
1008 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1011 number = -((HOST_WIDE_INT) number);
1014 /* Figure out how many digits there are. */
1022 /* Write them out. */
1025 int digit = number / m;
1026 write_char (digits[digit]);
1027 number -= digit * m;
1031 my_friendly_assert (number == 0, 20000407);
1034 /* Write out an integeral CST in decimal. */
1037 write_integer_cst (cst)
1040 if (tree_int_cst_sgn (cst) >= 0)
1042 if (TREE_INT_CST_HIGH (cst) != 0)
1043 sorry ("mangling very large integers");
1044 write_unsigned_number (TREE_INT_CST_LOW (cst));
1047 write_signed_number (tree_low_cst (cst, 0));
1050 /* Non-terminal <identifier>.
1052 <identifier> ::= </unqualified source code identifier> */
1055 write_identifier (identifier)
1058 MANGLE_TRACE ("identifier", identifier);
1059 write_string (identifier);
1062 /* Handle constructor productions of non-terminal <special-name>.
1063 CTOR is a constructor FUNCTION_DECL.
1065 <special-name> ::= C1 # complete object constructor
1066 ::= C2 # base object constructor
1067 ::= C3 # complete object allocating constructor
1069 Currently, allocating constructors are never used.
1071 We also need to provide unique mangled names (which should never be
1072 exported) for the constructor that takes an in-charge parameter,
1073 and for a constructor whose name is the same as its class's name.
1074 We use "C*INTERNAL*" for these. */
1077 write_special_name_constructor (ctor)
1080 if (DECL_COMPLETE_CONSTRUCTOR_P (ctor))
1081 write_string ("C1");
1082 else if (DECL_BASE_CONSTRUCTOR_P (ctor))
1083 write_string ("C2");
1085 write_string ("C*INTERNAL*");
1088 /* Handle destructor productions of non-terminal <special-name>.
1089 DTOR is a denstructor FUNCTION_DECL.
1091 <special-name> ::= D0 # deleting (in-charge) destructor
1092 ::= D1 # complete object (in-charge) destructor
1093 ::= D2 # base object (not-in-charge) destructor
1095 We also need to provide unique mngled names for old-ABI
1096 destructors, sometimes. These should only be used internally. We
1097 use "D*INTERNAL*" for these. */
1100 write_special_name_destructor (dtor)
1103 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1104 write_string ("D0");
1105 else if (DECL_COMPLETE_DESTRUCTOR_P (dtor))
1106 write_string ("D1");
1107 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1108 write_string ("D2");
1110 /* Old-ABI destructor. */
1111 write_string ("D*INTERNAL*");
1114 /* Return the discriminator for ENTITY appearing inside
1115 FUNCTION. The discriminator is the lexical ordinal of VAR among
1116 entities with the same name in the same FUNCTION. */
1119 discriminator_for_local_entity (entity)
1125 /* Assume this is the only local entity with this name. */
1128 /* For now, we don't discriminate amongst local variables. */
1129 if (TREE_CODE (entity) != TYPE_DECL)
1132 /* Scan the list of local classes. */
1133 entity = TREE_TYPE (entity);
1134 for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
1135 if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1136 && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1139 return discriminator;
1142 /* Return the discriminator for STRING, a string literal used inside
1143 FUNCTION. The disciminator is the lexical ordinal of STRING among
1144 string literals used in FUNCTION. */
1147 discriminator_for_string_literal (function, string)
1148 tree function ATTRIBUTE_UNUSED;
1149 tree string ATTRIBUTE_UNUSED;
1151 /* For now, we don't discriminate amongst string literals. */
1155 /* <discriminator> := _ <number>
1157 The discriminator is used only for the second and later occurrences
1158 of the same name within a single function. In this case <number> is
1159 n - 2, if this is the nth occurrence, in lexical order. */
1162 write_discriminator (discriminator)
1165 /* If discriminator is zero, don't write anything. Otherwise... */
1166 if (discriminator > 0)
1169 /* The number is omitted for discriminator == 1. Beyond 1, the
1170 numbering starts at 0. */
1171 if (discriminator > 1)
1172 write_unsigned_number (discriminator - 2);
1176 /* Mangle the name of a function-scope entity. FUNCTION is the
1177 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1178 the entity itself. LOCAL_ENTITY is the entity that's directly
1179 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1182 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1183 := Z <function encoding> E s [<discriminator>] */
1186 write_local_name (function, local_entity, entity)
1191 MANGLE_TRACE_TREE ("local-name", entity);
1194 write_encoding (function);
1196 if (TREE_CODE (entity) == STRING_CST)
1199 write_discriminator (discriminator_for_string_literal (function,
1204 /* Now the <entity name>. Let write_name know its being called
1205 from <local-name>, so it doesn't try to process the enclosing
1206 function scope again. */
1207 write_name (entity, /*ignore_local_scope=*/1);
1208 write_discriminator (discriminator_for_local_entity (local_entity));
1212 /* Non-terminals <type> and <CV-qualifier>.
1214 <type> ::= <builtin-type>
1216 ::= <class-enum-type>
1218 ::= <pointer-to-member-type>
1219 ::= <template-param>
1222 ::= P <type> # pointer-to
1223 ::= R <type> # reference-to
1224 ::= C <type> # complex pair (C 2000) [not supported]
1225 ::= G <type> # imaginary (C 2000) [not supported]
1226 ::= U <source-name> <type> # vendor extended type qualifier
1229 TYPE is a type node. */
1235 /* This gets set to non-zero if TYPE turns out to be a (possibly
1236 CV-qualified) builtin type. */
1237 int is_builtin_type = 0;
1239 MANGLE_TRACE_TREE ("type", type);
1241 if (find_substitution (type))
1244 if (write_CV_qualifiers_for_type (type) > 0)
1245 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1246 mangle the unqualified type. The recursive call is needed here
1247 since both the qualified and uqualified types are substitution
1249 write_type (TYPE_MAIN_VARIANT (type));
1252 /* See through any typedefs. */
1253 type = TYPE_MAIN_VARIANT (type);
1255 switch (TREE_CODE (type))
1259 case INTEGER_TYPE: /* Includes wchar_t. */
1261 /* If this is a typedef, TYPE may not be one of
1262 the standard builtin type nodes, but an alias of one. Use
1263 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1264 write_builtin_type (TYPE_MAIN_VARIANT (type));
1270 write_type (TREE_TYPE (type));
1275 write_function_type (type);
1281 /* A pointer-to-member function is represented as a special
1282 RECORD_TYPE, so check for this first. */
1283 if (TYPE_PTRMEMFUNC_P (type))
1284 write_pointer_to_member_type (type);
1286 write_class_enum_type (type);
1290 /* We handle TYPENAME_TYPEs like ordinary nested names. */
1291 write_nested_name (TYPE_STUB_DECL (type));
1295 write_array_type (type);
1299 /* A pointer-to-member variable is represented by a POINTER_TYPE
1300 to an OFFSET_TYPE, so check for this first. */
1301 if (TYPE_PTRMEM_P (type))
1302 write_pointer_to_member_type (type);
1306 write_type (TREE_TYPE (type));
1310 case REFERENCE_TYPE:
1312 write_type (TREE_TYPE (type));
1315 case TEMPLATE_TYPE_PARM:
1316 case TEMPLATE_PARM_INDEX:
1317 write_template_param (type);
1320 case TEMPLATE_TEMPLATE_PARM:
1321 write_template_template_param (type);
1322 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type))
1324 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1328 write_pointer_to_member_type (build_pointer_type (type));
1332 my_friendly_abort (20000409);
1336 /* Types other than builtin types are substitution candidates. */
1337 if (!is_builtin_type)
1338 add_substitution (type);
1341 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1342 CV-qualifiers written for TYPE.
1344 <CV-qualifiers> ::= [r] [V] [K] */
1347 write_CV_qualifiers_for_type (type)
1350 int num_qualifiers = 0;
1352 /* The order is specified by:
1354 "In cases where multiple order-insensitive qualifiers are
1355 present, they should be ordered 'K' (closest to the base type),
1356 'V', 'r', and 'U' (farthest from the base type) ..." */
1358 if (CP_TYPE_RESTRICT_P (type))
1363 if (CP_TYPE_VOLATILE_P (type))
1368 if (CP_TYPE_CONST_P (type))
1374 return num_qualifiers;
1377 /* Non-terminal <builtin-type>.
1379 <builtin-type> ::= v # void
1384 ::= h # unsigned char
1386 ::= t # unsigned short
1388 ::= j # unsigned int
1390 ::= m # unsigned long
1391 ::= x # long long, __int64
1392 ::= y # unsigned long long, __int64
1393 ::= n # __int128 [not supported]
1394 ::= o # unsigned __int128 [not supported]
1397 ::= e # long double, __float80
1398 ::= g # __float128 [not supported] */
1401 write_builtin_type (type)
1404 switch (TREE_CODE (type))
1415 /* If this is size_t, get the underlying int type. */
1416 if (TYPE_IS_SIZETYPE (type))
1417 type = TYPE_DOMAIN (type);
1419 /* TYPE may still be wchar_t, since that isn't in
1420 integer_type_nodes. */
1421 if (type == wchar_type_node)
1426 /* Assume TYPE is one of the shared integer type nodes. Find
1427 it in the array of these nodes. */
1428 for (itk = 0; itk < itk_none; ++itk)
1429 if (type == integer_types[itk])
1431 /* Print the corresponding single-letter code. */
1432 write_char (integer_type_codes[itk]);
1436 if (itk == itk_none)
1437 /* Couldn't find this type. */
1438 my_friendly_abort (20000408);
1443 if (type == float_type_node)
1445 else if (type == double_type_node)
1447 else if (type == long_double_type_node)
1450 my_friendly_abort (20000409);
1454 my_friendly_abort (20000509);
1458 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1459 METHOD_TYPE. The return type is mangled before the parameter
1462 <function-type> ::= F [Y] <bare-function-type> E */
1465 write_function_type (type)
1468 MANGLE_TRACE_TREE ("function-type", type);
1471 /* We don't track whether or not a type is `extern "C"'. Note that
1472 you can have an `extern "C"' function that does not have
1473 `extern "C"' type, and vice versa:
1475 extern "C" typedef void function_t();
1476 function_t f; // f has C++ linkage, but its type is
1479 typedef void function_t();
1480 extern "C" function_t f; // Vice versa.
1483 write_bare_function_type (type, /*include_return_type_p=*/1);
1487 /* Non-terminal <bare-function-type>. NODE is a FUNCTION_DECL or a
1488 METHOD_TYPE. If INCLUDE_RETURN_TYPE is non-zero, the return value
1489 is mangled before the parameter types.
1491 <bare-function-type> ::= </signature/ type>+ */
1494 write_bare_function_type (type, include_return_type_p)
1496 int include_return_type_p;
1498 MANGLE_TRACE_TREE ("bare-function-type", type);
1500 /* Mangle the return type, if requested. */
1501 if (include_return_type_p)
1502 write_type (TREE_TYPE (type));
1504 /* Now mangle the types of the arguments. */
1505 write_method_parms (TYPE_ARG_TYPES (type),
1506 TREE_CODE (type) == METHOD_TYPE);
1509 /* Write the mangled representation of a method parameter list of
1510 types given in PARM_LIST. If METHOD_P is non-zero, the function is
1511 considered a non-static method, and the this parameter is omitted. */
1514 write_method_parms (parm_list, method_p)
1519 /* Assume this parameter type list is variable-length. If it ends
1520 with a void type, then it's not. */
1523 /* If this is a member function, skip the first arg, which is the
1525 "Member functions do not encode the type of their implicit this
1528 parm_list = TREE_CHAIN (parm_list);
1530 for (first_parm = parm_list;
1532 parm_list = TREE_CHAIN (parm_list))
1534 tree parm = TREE_VALUE (parm_list);
1536 if (parm == void_type_node)
1538 /* "Empty parameter lists, whether declared as () or
1539 conventionally as (void), are encoded with a void parameter
1541 if (parm_list == first_parm)
1543 /* If the parm list is terminated with a void type, it's
1546 /* A void type better be the last one. */
1547 my_friendly_assert (TREE_CHAIN (parm_list) == NULL, 20000523);
1554 /* <builtin-type> ::= z # ellipsis */
1558 /* <class-enum-type> ::= <name> */
1561 write_class_enum_type (type)
1564 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1567 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1570 <template-args> ::= I <template-arg>+ E */
1573 write_template_args (args)
1577 int length = TREE_VEC_LENGTH (args);
1579 MANGLE_TRACE_TREE ("template-args", args);
1581 my_friendly_assert (length > 0, 20000422);
1583 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1585 /* We have nested template args. We want the innermost template
1587 args = TREE_VEC_ELT (args, length - 1);
1588 length = TREE_VEC_LENGTH (args);
1592 for (i = 0; i < length; ++i)
1593 write_template_arg (TREE_VEC_ELT (args, i));
1597 /* <expression> ::= <unary operator-name> <expression>
1598 ::= <binary operator-name> <expression> <expression>
1601 <expr-primary> ::= <template-param>
1602 ::= L <type> <value number> E # literal
1603 ::= L <mangled-name> E # external name */
1606 write_expression (expr)
1609 enum tree_code code;
1611 code = TREE_CODE (expr);
1613 /* Handle pointers-to-members by making them look like expression
1615 if (code == PTRMEM_CST)
1617 expr = build_nt (ADDR_EXPR,
1618 build_nt (SCOPE_REF,
1619 PTRMEM_CST_CLASS (expr),
1620 PTRMEM_CST_MEMBER (expr)));
1621 code = TREE_CODE (expr);
1624 /* Handle template parameters. */
1625 if (code == TEMPLATE_TYPE_PARM
1626 || code == TEMPLATE_TEMPLATE_PARM
1627 || code == TEMPLATE_PARM_INDEX)
1628 write_template_param (expr);
1629 /* Handle literals. */
1630 else if (TREE_CODE_CLASS (code) == 'c')
1631 write_template_arg_literal (expr);
1632 else if (DECL_P (expr))
1635 write_mangled_name (expr);
1642 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1643 is converted (via qualification conversions) to another
1645 while (TREE_CODE (expr) == NOP_EXPR)
1647 expr = TREE_OPERAND (expr, 0);
1648 code = TREE_CODE (expr);
1651 /* When we bind a variable or function to a non-type template
1652 argument with reference type, we create an ADDR_EXPR to show
1653 the fact that the entity's address has been taken. But, we
1654 don't actually want to output a mangling code for the `&'. */
1655 if (TREE_CODE (expr) == ADDR_EXPR
1657 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
1659 expr = TREE_OPERAND (expr, 0);
1662 write_expression (expr);
1666 code = TREE_CODE (expr);
1669 /* If it wasn't any of those, recursively expand the expression. */
1670 write_string (operator_name_info[(int) code].mangled_name);
1672 /* Handle pointers-to-members specially. */
1673 if (code == SCOPE_REF)
1675 write_type (TREE_OPERAND (expr, 0));
1676 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
1677 write_source_name (TREE_OPERAND (expr, 1));
1679 write_encoding (TREE_OPERAND (expr, 1));
1682 for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
1683 write_expression (TREE_OPERAND (expr, i));
1687 /* Literal subcase of non-terminal <template-arg>.
1689 "Literal arguments, e.g. "A<42L>", are encoded with their type
1690 and value. Negative integer values are preceded with "n"; for
1691 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
1692 encoded as 0, true as 1. If floating-point arguments are accepted
1693 as an extension, their values should be encoded using a
1694 fixed-length lowercase hexadecimal string corresponding to the
1695 internal representation (IEEE on IA-64), high-order bytes first,
1696 without leading zeroes. For example: "Lfbff000000E" is -1.0f." */
1699 write_template_arg_literal (value)
1702 tree type = TREE_TYPE (value);
1706 if (TREE_CODE (value) == CONST_DECL)
1707 write_integer_cst (DECL_INITIAL (value));
1708 else if (TREE_CODE (value) == INTEGER_CST)
1710 if (same_type_p (type, boolean_type_node))
1712 if (value == boolean_false_node || integer_zerop (value))
1713 write_unsigned_number (0);
1714 else if (value == boolean_true_node)
1715 write_unsigned_number (1);
1717 my_friendly_abort (20000412);
1720 write_integer_cst (value);
1722 else if (TREE_CODE (value) == REAL_CST)
1724 #ifdef CROSS_COMPILE
1725 static int explained;
1729 sorry ("real-valued template parameters when cross-compiling");
1734 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1735 write_number (((unsigned char *)
1736 &TREE_REAL_CST (value))[i],
1742 my_friendly_abort (20000412);
1747 /* Non-terminal <tempalate-arg>.
1749 <template-arg> ::= <type> # type
1750 ::= L <type> </value/ number> E # literal
1751 ::= LZ <name> E # external name
1752 ::= X <expression> E # expression */
1755 write_template_arg (node)
1758 enum tree_code code = TREE_CODE (node);
1760 MANGLE_TRACE_TREE ("template-arg", node);
1762 /* A template template paramter's argument list contains TREE_LIST
1763 nodes of which the value field is the the actual argument. */
1764 if (code == TREE_LIST)
1766 node = TREE_VALUE (node);
1767 /* If it's a decl, deal with its type instead. */
1770 node = TREE_TYPE (node);
1771 code = TREE_CODE (node);
1777 else if (code == TEMPLATE_DECL)
1778 /* A template appearing as a template arg is a template template arg. */
1779 write_template_template_arg (node);
1780 else if (DECL_P (node))
1784 write_encoding (node);
1787 else if (TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST)
1788 write_template_arg_literal (node);
1791 /* Template arguments may be expressions. */
1793 write_expression (node);
1798 /* <template-template-arg>
1800 ::= <substitution> */
1803 write_template_template_arg (tree decl)
1805 MANGLE_TRACE_TREE ("template-template-arg", decl);
1807 if (find_substitution (decl))
1809 write_name (decl, /*ignore_local_scope=*/0);
1810 add_substitution (decl);
1814 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
1816 <array-type> ::= A [</dimension/ number>] _ </element/ type>
1817 ::= A <expression> _ </element/ type>
1819 "Array types encode the dimension (number of elements) and the
1820 element type. For variable length arrays, the dimension (but not
1821 the '_' separator) is omitted." */
1824 write_array_type (type)
1828 if (TYPE_DOMAIN (type))
1833 index_type = TYPE_DOMAIN (type);
1834 /* The INDEX_TYPE gives the upper and lower bounds of the
1836 max = TYPE_MAX_VALUE (index_type);
1837 if (TREE_CODE (max) == INTEGER_CST)
1839 /* The ABI specifies that we should mangle the number of
1840 elements in the array, not the largest allowed index. */
1841 max = size_binop (PLUS_EXPR, max, size_one_node);
1842 write_unsigned_number (tree_low_cst (max, 1));
1845 write_expression (TREE_OPERAND (max, 0));
1848 write_type (TREE_TYPE (type));
1851 /* Non-terminal <pointer-to-member-type> for pointer-to-member
1852 variables. TYPE is a pointer-to-member POINTER_TYPE.
1854 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
1857 write_pointer_to_member_type (type)
1861 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
1862 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
1865 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
1866 TEMPLATE_TEMPLATE_PARM, or a TEMPLATE_PARM_INDEX.
1868 <template-param> ::= T </parameter/ number> _ */
1871 write_template_param (parm)
1876 MANGLE_TRACE_TREE ("template-parm", parm);
1878 switch (TREE_CODE (parm))
1880 case TEMPLATE_TYPE_PARM:
1881 case TEMPLATE_TEMPLATE_PARM:
1882 parm_index = TEMPLATE_TYPE_IDX (parm);
1885 case TEMPLATE_PARM_INDEX:
1886 parm_index = TEMPLATE_PARM_IDX (parm);
1890 my_friendly_abort (20000523);
1894 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
1895 earliest template param denoted by `_'. */
1897 write_unsigned_number (parm_index - 1);
1901 /* <template-template-param>
1902 ::= <template-param>
1903 ::= <substitution> */
1906 write_template_template_param (parm)
1909 tree template = NULL_TREE;
1911 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
1912 template template parameter. The substitution candidate here is
1913 only the template. */
1914 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm))
1917 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
1918 if (find_substitution (template))
1922 /* <template-param> encodes only the template parameter position,
1923 not its template arguments, which is fine here. */
1924 write_template_param (parm);
1926 add_substitution (template);
1929 /* Non-terminal <substitution>.
1931 <substitution> ::= S <seq-id> _
1935 write_substitution (seq_id)
1938 MANGLE_TRACE ("substitution", "");
1942 write_number (seq_id - 1, /*unsigned=*/1, 36);
1946 /* Start mangling a new name or type. */
1951 obstack_free (&G.name_obstack, obstack_base (&G.name_obstack));
1954 /* Done with mangling. Return the generated mangled name. */
1956 static inline const char *
1959 /* Clear all the substitutions. */
1960 VARRAY_POP_ALL (G.substitutions);
1962 /* Null-terminate the string. */
1965 return (const char *) obstack_base (&G.name_obstack);
1968 /* Initialize data structures for mangling. */
1973 gcc_obstack_init (&G.name_obstack);
1974 VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
1976 /* Cache these identifiers for quick comparison when checking for
1977 standard substitutions. */
1978 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
1979 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
1980 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
1981 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
1982 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
1983 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
1986 /* Generate the mangled name of DECL. */
1989 mangle_decl_string (decl)
1996 if (TREE_CODE (decl) == TYPE_DECL)
1997 write_type (TREE_TYPE (decl));
1999 write_mangled_name (decl);
2001 result = finish_mangling ();
2003 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2007 /* Create an identifier for the external mangled name of DECL. */
2013 return get_identifier (mangle_decl_string (decl));
2016 /* Generate the mangled representation of TYPE. */
2019 mangle_type_string (type)
2026 result = finish_mangling ();
2028 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2032 /* Create an identifier for the mangled representation of TYPE. */
2038 return get_identifier (mangle_type_string (type));
2041 /* Create an identifier for the mangled name of a special component
2042 for belonging to TYPE. CODE is the ABI-specified code for this
2046 mangle_special_for_type (type, code)
2052 /* We don't have an actual decl here for the special component, so
2053 we can't just process the <encoded-name>. Instead, fake it. */
2056 /* Start the mangling. */
2057 write_string ("_Z");
2058 write_string (code);
2062 result = finish_mangling ();
2065 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2067 return get_identifier (result);
2070 /* Create an identifier for the mangled representation of the typeinfo
2071 structure for TYPE. */
2074 mangle_typeinfo_for_type (type)
2077 return mangle_special_for_type (type, "TI");
2080 /* Create an identifier for the mangled name of the NTBS containing
2081 the mangled name of TYPE. */
2084 mangle_typeinfo_string_for_type (type)
2087 return mangle_special_for_type (type, "TS");
2090 /* Create an identifier for the mangled name of the vtable for TYPE. */
2093 mangle_vtbl_for_type (type)
2096 return mangle_special_for_type (type, "TV");
2099 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2102 mangle_vtt_for_type (type)
2105 return mangle_special_for_type (type, "TT");
2108 /* Return an identifier for a construction vtable group. TYPE is
2109 the most derived class in the hierarchy; BINFO is the base
2110 subobject for which this construction vtable group will be used.
2112 This mangling isn't part of the ABI specification; in the ABI
2113 specification, the vtable group is dumped in the same COMDAT as the
2114 main vtable, and is referenced only from that vtable, so it doesn't
2115 need an external name. For binary formats without COMDAT sections,
2116 though, we need external names for the vtable groups.
2118 We use the production
2120 <special-name> ::= CT <type> <offset number> _ <base type> */
2123 mangle_ctor_vtbl_for_type (type, binfo)
2131 write_string ("_Z");
2132 write_string ("TC");
2134 write_integer_cst (BINFO_OFFSET (binfo));
2136 write_type (BINFO_TYPE (binfo));
2138 result = finish_mangling ();
2140 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2141 return get_identifier (result);
2144 /* Return an identifier for the mangled name of a thunk to FN_DECL.
2145 OFFSET is the initial adjustment to this used to find the vptr. If
2146 VCALL_OFFSET is non-NULL, this is a virtual thunk, and it is the
2147 vtbl offset in bytes.
2149 <special-name> ::= Th <offset number> _ <base encoding>
2150 ::= Tv <offset number> _ <vcall offset number> _
2155 mangle_thunk (fn_decl, offset, vcall_offset)
2164 write_string ("_Z");
2165 /* The <special-name> for virtual thunks is Tv, for non-virtual
2168 if (vcall_offset != 0)
2173 /* For either flavor, write the offset to this. */
2174 write_integer_cst (offset);
2177 /* For a virtual thunk, add the vcall offset. */
2180 /* Virtual thunk. Write the vcall offset and base type name. */
2181 write_integer_cst (vcall_offset);
2186 write_encoding (fn_decl);
2188 result = finish_mangling ();
2190 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2191 return get_identifier (result);
2194 /* Return an identifier for the mangled unqualified name for a
2195 conversion operator to TYPE. This mangling is not specified by the
2196 ABI spec; it is only used internally.
2198 For compatibility with existing conversion operator mechanisms,
2199 the mangled form is `__op<type>' where <type> is the mangled
2200 representation of TYPE.
2202 FIXME: Though identifiers with starting with __op are reserved for
2203 the implementation, it would eventually be nice to use inaccessible
2204 names for these operators. */
2207 mangle_conv_op_name_for_type (type)
2212 /* Build the mangling for TYPE. */
2213 const char *mangled_type = mangle_type_string (type);
2214 /* Allocate a temporary buffer for the complete name. */
2215 char *op_name = (char *) xmalloc (strlen (OPERATOR_TYPENAME_FORMAT)
2216 + strlen (mangled_type) + 1);
2217 /* Assemble the mangling. */
2218 strcpy (op_name, OPERATOR_TYPENAME_FORMAT);
2219 strcat (op_name, mangled_type);
2220 /* Find or create an identifier. */
2221 identifier = get_identifier (op_name);
2222 /* Done with the temporary buffer. */
2224 /* Set bits on the identifier so we know later it's a conversion. */
2225 IDENTIFIER_OPNAME_P (identifier) = 1;
2226 IDENTIFIER_TYPENAME_P (identifier) = 1;
2227 /* Hang TYPE off the identifier so it can be found easily later when
2228 performing conversions. */
2229 TREE_TYPE (identifier) = type;
2234 /* Return an identifier for the name of an initialization guard
2235 variable for indicated VARIABLE. */
2238 mangle_guard_variable (variable)
2242 write_string ("_ZGV");
2243 write_name (variable, /*ignore_local_scope=*/0);
2244 return get_identifier (finish_mangling ());