1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Alex Samuel <sameul@codesourcery.com>
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This file implements mangling of C++ names according to the IA64
23 C++ ABI specification. A mangled name encodes a function or
24 variable's name, scope, type, and/or template arguments into a text
25 identifier. This identifier is used as the function's or
26 variable's linkage name, to preserve compatibility between C++'s
27 language features (templates, scoping, and overloading) and C
30 Additionally, g++ uses mangled names internally. To support this,
31 mangling of types is allowed, even though the mangled name of a
32 type should not appear by itself as an exported name. Ditto for
33 uninstantiated templates.
35 The primary entry point for this module is mangle_decl, which
36 returns an identifier containing the mangled name for a decl.
37 Additional entry points are provided to build mangled names of
38 particular constructs when the appropriate decl for that construct
39 is not available. These are:
41 mangle_typeinfo_for_type: typeinfo data
42 mangle_typeinfo_string_for_type: typeinfo type name
43 mangle_vtbl_for_type: virtual table data
44 mangle_vtt_for_type: VTT data
45 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
46 mangle_thunk: thunk function or entry
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 int hwint_to_ascii
PARAMS ((unsigned HOST_WIDE_INT
, unsigned int, char *, unsigned));
157 static void write_number
PARAMS ((unsigned HOST_WIDE_INT
, int,
159 static void write_integer_cst
PARAMS ((tree
));
160 static void write_identifier
PARAMS ((const char *));
161 static void write_special_name_constructor
PARAMS ((tree
));
162 static void write_special_name_destructor
PARAMS ((tree
));
163 static void write_type
PARAMS ((tree
));
164 static int write_CV_qualifiers_for_type
PARAMS ((tree
));
165 static void write_builtin_type
PARAMS ((tree
));
166 static void write_function_type
PARAMS ((tree
));
167 static void write_bare_function_type
PARAMS ((tree
, int, tree
));
168 static void write_method_parms
PARAMS ((tree
, int, tree
));
169 static void write_class_enum_type
PARAMS ((tree
));
170 static void write_template_args
PARAMS ((tree
));
171 static void write_expression
PARAMS ((tree
));
172 static void write_template_arg_literal
PARAMS ((tree
));
173 static void write_template_arg
PARAMS ((tree
));
174 static void write_template_template_arg
PARAMS ((tree
));
175 static void write_array_type
PARAMS ((tree
));
176 static void write_pointer_to_member_type
PARAMS ((tree
));
177 static void write_template_param
PARAMS ((tree
));
178 static void write_template_template_param
PARAMS ((tree
));
179 static void write_substitution
PARAMS ((int));
180 static int discriminator_for_local_entity
PARAMS ((tree
));
181 static int discriminator_for_string_literal
PARAMS ((tree
, tree
));
182 static void write_discriminator
PARAMS ((int));
183 static void write_local_name
PARAMS ((tree
, tree
, tree
));
184 static void dump_substitution_candidates
PARAMS ((void));
185 static const char *mangle_decl_string
PARAMS ((tree
));
187 /* Control functions. */
189 static inline void start_mangling
PARAMS ((void));
190 static inline const char *finish_mangling
PARAMS ((void));
191 static tree mangle_special_for_type
PARAMS ((tree
, const char *));
193 /* Foreign language functions. */
195 static void write_java_integer_type_codes
PARAMS ((tree
));
197 /* Append a single character to the end of the mangled
199 #define write_char(CHAR) \
200 obstack_1grow (&G.name_obstack, (CHAR))
202 /* Append a sized buffer to the end of the mangled representation. */
203 #define write_chars(CHAR, LEN) \
204 obstack_grow (&G.name_obstack, (CHAR), (LEN))
206 /* Append a NUL-terminated string to the end of the mangled
208 #define write_string(STRING) \
209 obstack_grow (&G.name_obstack, (STRING), strlen (STRING))
211 /* Return the position at which the next character will be appended to
212 the mangled representation. */
213 #define mangled_position() \
214 obstack_object_size (&G.name_obstack)
216 /* Non-zero if NODE1 and NODE2 are both TREE_LIST nodes and have the
217 same purpose (context, which may be a type) and value (template
218 decl). See write_template_prefix for more information on what this
220 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
221 (TREE_CODE (NODE1) == TREE_LIST \
222 && TREE_CODE (NODE2) == TREE_LIST \
223 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
224 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
225 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
226 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
228 /* Write out a signed quantity in base 10. */
229 #define write_signed_number(NUMBER) \
230 write_number ((NUMBER), /*unsigned_p=*/0, 10)
232 /* Write out an unsigned quantity in base 10. */
233 #define write_unsigned_number(NUMBER) \
234 write_number ((NUMBER), /*unsigned_p=*/1, 10)
236 /* If DECL is a template instance, return non-zero and, if
237 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
238 Otherwise return zero. */
241 decl_is_template_id (decl
, template_info
)
245 if (TREE_CODE (decl
) == TYPE_DECL
)
247 /* TYPE_DECLs are handled specially. Look at its type to decide
248 if this is a template instantiation. */
249 tree type
= TREE_TYPE (decl
);
251 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_ID_P (type
))
253 if (template_info
!= NULL
)
254 /* For a templated TYPE_DECL, the template info is hanging
256 *template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
262 /* Check if this is a primary template. */
263 if (DECL_LANG_SPECIFIC (decl
) != NULL
264 && DECL_USE_TEMPLATE (decl
)
265 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
))
266 && TREE_CODE (decl
) != TEMPLATE_DECL
)
268 if (template_info
!= NULL
)
269 /* For most templated decls, the template info is hanging
271 *template_info
= DECL_TEMPLATE_INFO (decl
);
276 /* It's not a template id. */
280 /* Produce debugging output of current substitution candidates. */
283 dump_substitution_candidates ()
287 fprintf (stderr
, " ++ substitutions ");
288 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (G
.substitutions
); ++i
)
290 tree el
= VARRAY_TREE (G
.substitutions
, i
);
291 const char *name
= "???";
294 fprintf (stderr
, " ");
296 name
= IDENTIFIER_POINTER (DECL_NAME (el
));
297 else if (TREE_CODE (el
) == TREE_LIST
)
298 name
= IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el
)));
299 else if (TYPE_NAME (el
))
300 name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el
)));
301 fprintf (stderr
, " S%d_ = ", i
- 1);
303 (CP_TYPE_RESTRICT_P (el
)
304 || CP_TYPE_VOLATILE_P (el
)
305 || CP_TYPE_CONST_P (el
)))
306 fprintf (stderr
, "CV-");
307 fprintf (stderr
, "%s (%s at %p)\n",
308 name
, tree_code_name
[TREE_CODE (el
)], (void *) el
);
312 /* Both decls and types can be substitution candidates, but sometimes
313 they refer to the same thing. For instance, a TYPE_DECL and
314 RECORD_TYPE for the same class refer to the same thing, and should
315 be treated accordinginly in substitutions. This function returns a
316 canonicalized tree node representing NODE that is used when adding
317 and substitution candidates and finding matches. */
320 canonicalize_for_substitution (node
)
323 /* For a TYPE_DECL, use the type instead. */
324 if (TREE_CODE (node
) == TYPE_DECL
)
325 node
= TREE_TYPE (node
);
327 node
= canonical_type_variant (node
);
332 /* Add NODE as a substitution candidate. NODE must not already be on
333 the list of candidates. */
336 add_substitution (node
)
342 fprintf (stderr
, " ++ add_substitution (%s at %10p)\n",
343 tree_code_name
[TREE_CODE (node
)], (void *) node
);
345 /* Get the canonicalized substitution candidate for NODE. */
346 c
= canonicalize_for_substitution (node
);
347 if (DEBUG_MANGLE
&& c
!= node
)
348 fprintf (stderr
, " ++ using candidate (%s at %10p)\n",
349 tree_code_name
[TREE_CODE (node
)], (void *) node
);
353 /* Make sure NODE isn't already a candidate. */
356 for (i
= VARRAY_ACTIVE_SIZE (G
.substitutions
); --i
>= 0; )
358 tree candidate
= VARRAY_TREE (G
.substitutions
, i
);
360 && node
== candidate
)
362 && TYPE_P (candidate
)
363 && same_type_p (node
, candidate
)))
367 #endif /* ENABLE_CHECKING */
369 /* Put the decl onto the varray of substitution candidates. */
370 VARRAY_PUSH_TREE (G
.substitutions
, node
);
373 dump_substitution_candidates ();
376 /* Helper function for find_substitution. Returns non-zero if NODE,
377 which may be a decl or a CLASS_TYPE, is a template-id with template
378 name of substitution_index[INDEX] in the ::std namespace. */
381 is_std_substitution (node
, index
)
383 substitution_identifier_index_t index
;
390 type
= TREE_TYPE (node
);
393 else if (CLASS_TYPE_P (node
))
396 decl
= TYPE_NAME (node
);
399 /* These are not the droids you're looking for. */
402 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl
))
403 && TYPE_LANG_SPECIFIC (type
)
404 && CLASSTYPE_TEMPLATE_INFO (type
)
405 && (DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
))
406 == subst_identifiers
[index
]));
409 /* Helper function for find_substitution. Returns non-zero if NODE,
410 which may be a decl or a CLASS_TYPE, is the template-id
411 ::std::identifier<char>, where identifier is
412 substitution_index[INDEX]. */
415 is_std_substitution_char (node
, index
)
417 substitution_identifier_index_t index
;
420 /* Check NODE's name is ::std::identifier. */
421 if (!is_std_substitution (node
, index
))
423 /* Figure out its template args. */
425 args
= DECL_TI_ARGS (node
);
426 else if (CLASS_TYPE_P (node
))
427 args
= CLASSTYPE_TI_ARGS (node
);
429 /* Oops, not a template. */
431 /* NODE's template arg list should be <char>. */
433 TREE_VEC_LENGTH (args
) == 1
434 && TREE_VEC_ELT (args
, 0) == char_type_node
;
437 /* Check whether a substitution should be used to represent NODE in
440 First, check standard special-case substitutions.
442 <substitution> ::= St
449 # ::std::basic_string
452 # ::std::basic_string<char,
453 ::std::char_traits<char>,
454 ::std::allocator<char> >
457 # ::std::basic_istream<char, ::std::char_traits<char> >
460 # ::std::basic_ostream<char, ::std::char_traits<char> >
463 # ::std::basic_iostream<char, ::std::char_traits<char> >
465 Then examine the stack of currently available substitution
466 candidates for entities appearing earlier in the same mangling
468 If a substitution is found, write its mangled representation and
469 return non-zero. If none is found, just return zero. */
472 find_substitution (node
)
476 int size
= VARRAY_ACTIVE_SIZE (G
.substitutions
);
481 fprintf (stderr
, " ++ find_substitution (%s at %p)\n",
482 tree_code_name
[TREE_CODE (node
)], (void *) node
);
484 /* Obtain the canonicalized substitution representation for NODE.
485 This is what we'll compare against. */
486 node
= canonicalize_for_substitution (node
);
488 /* Check for builtin substitutions. */
490 decl
= TYPE_P (node
) ? TYPE_NAME (node
) : node
;
491 type
= TYPE_P (node
) ? node
: TREE_TYPE (node
);
493 /* Check for std::allocator. */
495 && is_std_substitution (decl
, SUBID_ALLOCATOR
)
496 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)))
502 /* Check for std::basic_string. */
503 if (decl
&& is_std_substitution (decl
, SUBID_BASIC_STRING
))
507 /* If this is a type (i.e. a fully-qualified template-id),
509 std::basic_string <char,
510 std::char_traits<char>,
511 std::allocator<char> > . */
512 if (cp_type_quals (type
) == TYPE_UNQUALIFIED
513 && CLASSTYPE_USE_TEMPLATE (type
))
515 tree args
= CLASSTYPE_TI_ARGS (type
);
516 if (TREE_VEC_LENGTH (args
) == 3
517 && same_type_p (TREE_VEC_ELT (args
, 0), char_type_node
)
518 && is_std_substitution_char (TREE_VEC_ELT (args
, 1),
520 && is_std_substitution_char (TREE_VEC_ELT (args
, 2),
529 /* Substitute for the template name only if this isn't a type. */
536 /* Check for basic_{i,o,io}stream. */
538 && cp_type_quals (type
) == TYPE_UNQUALIFIED
539 && CLASS_TYPE_P (type
)
540 && CLASSTYPE_USE_TEMPLATE (type
)
541 && CLASSTYPE_TEMPLATE_INFO (type
) != NULL
)
543 /* First, check for the template
544 args <char, std::char_traits<char> > . */
545 tree args
= CLASSTYPE_TI_ARGS (type
);
546 if (TREE_VEC_LENGTH (args
) == 2
547 && same_type_p (TREE_VEC_ELT (args
, 0), char_type_node
)
548 && is_std_substitution_char (TREE_VEC_ELT (args
, 1),
551 /* Got them. Is this basic_istream? */
552 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
553 if (name
== subst_identifiers
[SUBID_BASIC_ISTREAM
])
558 /* Or basic_ostream? */
559 else if (name
== subst_identifiers
[SUBID_BASIC_OSTREAM
])
564 /* Or basic_iostream? */
565 else if (name
== subst_identifiers
[SUBID_BASIC_IOSTREAM
])
573 /* Check for namespace std. */
574 if (decl
&& DECL_NAMESPACE_STD_P (decl
))
580 /* Now check the list of available substitutions for this mangling
582 for (i
= 0; i
< size
; ++i
)
584 tree candidate
= VARRAY_TREE (G
.substitutions
, i
);
585 /* NODE is a matched to a candidate if it's the same decl node or
586 if it's the same type. */
587 if (decl
== candidate
588 || (TYPE_P (candidate
) && type
&& TYPE_P (type
)
589 && same_type_p (type
, candidate
))
590 || NESTED_TEMPLATE_MATCH (node
, candidate
))
592 write_substitution (i
);
597 /* No substitution found. */
602 /* <mangled-name> ::= _Z <encoding> */
605 write_mangled_name (decl
)
608 MANGLE_TRACE_TREE ("mangled-name", decl
);
610 if (DECL_LANG_SPECIFIC (decl
)
611 && DECL_EXTERN_C_FUNCTION_P (decl
)
612 && ! DECL_OVERLOADED_OPERATOR_P (decl
))
613 /* The standard notes:
614 "The <encoding> of an extern "C" function is treated like
615 global-scope data, i.e. as its <source-name> without a type."
616 We cannot write overloaded operators that way though,
617 because it contains characters invalid in assembler. */
618 write_source_name (DECL_NAME (decl
));
620 /* C++ name; needs to be mangled. */
623 write_encoding (decl
);
627 /* <encoding> ::= <function name> <bare-function-type>
631 write_encoding (decl
)
634 MANGLE_TRACE_TREE ("encoding", decl
);
636 if (DECL_LANG_SPECIFIC (decl
) && DECL_EXTERN_C_FUNCTION_P (decl
))
638 /* For overloaded operators write just the mangled name
639 without arguments. */
640 if (DECL_OVERLOADED_OPERATOR_P (decl
))
641 write_name (decl
, /*ignore_local_scope=*/0);
643 write_source_name (DECL_NAME (decl
));
647 write_name (decl
, /*ignore_local_scope=*/0);
648 if (TREE_CODE (decl
) == FUNCTION_DECL
)
652 if (decl_is_template_id (decl
, NULL
))
653 fn_type
= get_mostly_instantiated_function_type (decl
, NULL
, NULL
);
655 fn_type
= TREE_TYPE (decl
);
657 write_bare_function_type (fn_type
,
658 (!DECL_CONSTRUCTOR_P (decl
)
659 && !DECL_DESTRUCTOR_P (decl
)
660 && !DECL_CONV_FN_P (decl
)
661 && decl_is_template_id (decl
, NULL
)),
666 /* <name> ::= <unscoped-name>
667 ::= <unscoped-template-name> <template-args>
671 If IGNORE_LOCAL_SCOPE is non-zero, this production of <name> is
672 called from <local-name>, which mangles the enclosing scope
673 elsewhere and then uses this function to mangle just the part
674 underneath the function scope. So don't use the <local-name>
675 production, to avoid an infinite recursion. */
678 write_name (decl
, ignore_local_scope
)
680 int ignore_local_scope
;
684 MANGLE_TRACE_TREE ("name", decl
);
686 if (TREE_CODE (decl
) == TYPE_DECL
)
688 /* In case this is a typedef, fish out the corresponding
689 TYPE_DECL for the main variant. */
690 decl
= TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl
)));
691 context
= TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl
)));
694 context
= (DECL_CONTEXT (decl
) == NULL
) ? NULL
: CP_DECL_CONTEXT (decl
);
696 /* A decl in :: or ::std scope is treated specially. The former is
697 mangled using <unscoped-name> or <unscoped-template-name>, the
698 latter with a special substitution. Also, a name that is
699 directly in a local function scope is also mangled with
700 <unscoped-name> rather than a full <nested-name>. */
702 || context
== global_namespace
703 || DECL_NAMESPACE_STD_P (context
)
704 || (ignore_local_scope
&& TREE_CODE (context
) == FUNCTION_DECL
))
707 /* Is this a template instance? */
708 if (decl_is_template_id (decl
, &template_info
))
710 /* Yes: use <unscoped-template-name>. */
711 write_unscoped_template_name (TI_TEMPLATE (template_info
));
712 write_template_args (TI_ARGS (template_info
));
715 /* Everything else gets an <unqualified-name>. */
716 write_unscoped_name (decl
);
720 /* Handle local names, unless we asked not to (that is, invoked
721 under <local-name>, to handle only the part of the name under
723 if (!ignore_local_scope
)
725 /* Scan up the list of scope context, looking for a
726 function. If we find one, this entity is in local
727 function scope. local_entity tracks context one scope
728 level down, so it will contain the element that's
729 directly in that function's scope, either decl or one of
730 its enclosing scopes. */
731 tree local_entity
= decl
;
732 while (context
!= NULL
&& context
!= global_namespace
)
734 /* Make sure we're always dealing with decls. */
735 if (context
!= NULL
&& TYPE_P (context
))
736 context
= TYPE_NAME (context
);
737 /* Is this a function? */
738 if (TREE_CODE (context
) == FUNCTION_DECL
)
740 /* Yes, we have local scope. Use the <local-name>
741 production for the innermost function scope. */
742 write_local_name (context
, local_entity
, decl
);
745 /* Up one scope level. */
746 local_entity
= context
;
747 context
= CP_DECL_CONTEXT (context
);
750 /* No local scope found? Fall through to <nested-name>. */
753 /* Other decls get a <nested-name> to encode their scope. */
754 write_nested_name (decl
);
758 /* <unscoped-name> ::= <unqualified-name>
759 ::= St <unqualified-name> # ::std:: */
762 write_unscoped_name (decl
)
765 tree context
= CP_DECL_CONTEXT (decl
);
767 MANGLE_TRACE_TREE ("unscoped-name", decl
);
769 /* Is DECL in ::std? */
770 if (DECL_NAMESPACE_STD_P (context
))
773 write_unqualified_name (decl
);
775 /* If not, it should be either in the global namespace, or directly
776 in a local function scope. */
777 else if (context
== global_namespace
779 || TREE_CODE (context
) == FUNCTION_DECL
)
780 write_unqualified_name (decl
);
785 /* <unscoped-template-name> ::= <unscoped-name>
786 ::= <substitution> */
789 write_unscoped_template_name (decl
)
792 MANGLE_TRACE_TREE ("unscoped-template-name", decl
);
794 if (find_substitution (decl
))
796 write_unscoped_name (decl
);
797 add_substitution (decl
);
800 /* Write the nested name, including CV-qualifiers, of DECL.
802 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
803 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
805 <CV-qualifiers> ::= [r] [V] [K] */
808 write_nested_name (decl
)
813 MANGLE_TRACE_TREE ("nested-name", decl
);
817 /* Write CV-qualifiers, if this is a member function. */
818 if (TREE_CODE (decl
) == FUNCTION_DECL
819 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
821 if (DECL_VOLATILE_MEMFUNC_P (decl
))
823 if (DECL_CONST_MEMFUNC_P (decl
))
827 /* Is this a template instance? */
828 if (decl_is_template_id (decl
, &template_info
))
830 /* Yes, use <template-prefix>. */
831 write_template_prefix (decl
);
832 write_template_args (TI_ARGS (template_info
));
836 /* No, just use <prefix> */
837 write_prefix (DECL_CONTEXT (decl
));
838 write_unqualified_name (decl
);
843 /* <prefix> ::= <prefix> <unqualified-name>>
844 ::= <template-prefix> <template-args>
846 ::= <substitution> */
853 /* Non-NULL if NODE represents a template-id. */
854 tree template_info
= NULL
;
856 MANGLE_TRACE_TREE ("prefix", node
);
859 || node
== global_namespace
)
862 if (find_substitution (node
))
866 /* Node is a decl. */
868 /* If this is a function decl, that means we've hit function
869 scope, so this prefix must be for a local name. In this
870 case, we're under the <local-name> production, which encodes
871 the enclosing function scope elsewhere. So don't continue
873 if (TREE_CODE (node
) == FUNCTION_DECL
)
877 decl_is_template_id (decl
, &template_info
);
880 /* Node is a type. */
882 decl
= TYPE_NAME (node
);
883 if (CLASSTYPE_TEMPLATE_ID_P (node
))
884 template_info
= CLASSTYPE_TEMPLATE_INFO (node
);
887 if (template_info
!= NULL
)
890 write_template_prefix (decl
);
891 write_template_args (TI_ARGS (template_info
));
896 write_prefix (CP_DECL_CONTEXT (decl
));
897 write_unqualified_name (decl
);
900 add_substitution (node
);
903 /* <template-prefix> ::= <prefix> <template component>
904 ::= <substitution> */
907 write_template_prefix (node
)
910 tree decl
= DECL_P (node
) ? node
: TYPE_NAME (node
);
911 tree type
= DECL_P (node
) ? TREE_TYPE (node
) : node
;
912 tree context
= CP_DECL_CONTEXT (decl
);
917 MANGLE_TRACE_TREE ("template-prefix", node
);
919 /* Find the template decl. */
920 if (decl_is_template_id (decl
, &template_info
))
921 template = TI_TEMPLATE (template_info
);
922 else if (CLASSTYPE_TEMPLATE_ID_P (type
))
923 template = CLASSTYPE_TI_TEMPLATE (type
);
925 /* Oops, not a template. */
928 /* For a member template, though, the template name for the
929 innermost name must have all the outer template levels
930 instantiated. For instance, consider
932 template<typename T> struct Outer {
933 template<typename U> struct Inner {};
936 The template name for `Inner' in `Outer<int>::Inner<float>' is
937 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
938 levels separately, so there's no TEMPLATE_DECL available for this
939 (there's only `Outer<T>::Inner<U>').
941 In order to get the substitutions right, we create a special
942 TREE_LIST to represent the substitution candidate for a nested
943 template. The TREE_PURPOSE is the template's context, fully
944 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
947 So, for the example above, `Outer<int>::Inner' is represented as a
948 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
949 and whose value is `Outer<T>::Inner<U>'. */
950 if (TYPE_P (context
))
951 substitution
= build_tree_list (context
, template);
953 substitution
= template;
955 if (find_substitution (substitution
))
958 write_prefix (context
);
959 write_unqualified_name (decl
);
961 add_substitution (substitution
);
964 /* We don't need to handle thunks, vtables, or VTTs here. Those are
965 mangled through special entry points.
967 <unqualified-name> ::= <operator-name>
972 write_unqualified_name (decl
)
975 MANGLE_TRACE_TREE ("unqualified-name", decl
);
977 if (DECL_LANG_SPECIFIC (decl
) != NULL
&& DECL_CONSTRUCTOR_P (decl
))
978 write_special_name_constructor (decl
);
979 else if (DECL_LANG_SPECIFIC (decl
) != NULL
&& DECL_DESTRUCTOR_P (decl
))
980 write_special_name_destructor (decl
);
981 else if (DECL_CONV_FN_P (decl
))
983 /* Conversion operator. Handle it right here.
984 <operator> ::= cv <type> */
986 write_type (TREE_TYPE (DECL_NAME (decl
)));
988 else if (DECL_OVERLOADED_OPERATOR_P (decl
))
990 operator_name_info_t
*oni
;
991 if (DECL_ASSIGNMENT_OPERATOR_P (decl
))
992 oni
= assignment_operator_name_info
;
994 oni
= operator_name_info
;
996 write_string (oni
[DECL_OVERLOADED_OPERATOR_P (decl
)].mangled_name
);
999 write_source_name (DECL_NAME (decl
));
1002 /* Non-termial <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1004 <source-name> ::= </length/ number> <identifier> */
1007 write_source_name (identifier
)
1010 MANGLE_TRACE_TREE ("source-name", identifier
);
1012 /* Never write the whole template-id name including the template
1013 arguments; we only want the template name. */
1014 if (IDENTIFIER_TEMPLATE (identifier
))
1015 identifier
= IDENTIFIER_TEMPLATE (identifier
);
1017 write_unsigned_number (IDENTIFIER_LENGTH (identifier
));
1018 write_identifier (IDENTIFIER_POINTER (identifier
));
1021 /* Convert NUMBER to ascii using base BASE and generating at least
1022 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1023 into which to store the characters. Returns the number of
1024 characters generated (these will be layed out in advance of where
1028 hwint_to_ascii (number
, base
, buffer
, min_digits
)
1029 unsigned HOST_WIDE_INT number
;
1032 unsigned min_digits
;
1034 static const char base_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1035 unsigned digits
= 0;
1039 unsigned HOST_WIDE_INT d
= number
/ base
;
1041 *--buffer
= base_digits
[number
- d
* base
];
1045 while (digits
< min_digits
)
1047 *--buffer
= base_digits
[0];
1053 /* Non-terminal <number>.
1055 <number> ::= [n] </decimal integer/> */
1058 write_number (number
, unsigned_p
, base
)
1059 unsigned HOST_WIDE_INT number
;
1063 char buffer
[sizeof (HOST_WIDE_INT
) * 8];
1066 if (!unsigned_p
&& (HOST_WIDE_INT
) number
< 0)
1069 number
= -((HOST_WIDE_INT
) number
);
1071 count
= hwint_to_ascii (number
, base
, buffer
+ sizeof (buffer
), 1);
1072 write_chars (buffer
+ sizeof (buffer
) - count
, count
);
1075 /* Write out an integral CST in decimal. Most numbers are small, and
1076 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1077 bigger than that, which we must deal with. */
1080 write_integer_cst (cst
)
1083 int sign
= tree_int_cst_sgn (cst
);
1085 if (TREE_INT_CST_HIGH (cst
) + (sign
< 0))
1087 /* A bignum. We do this in chunks, each of which fits in a
1089 char buffer
[sizeof (HOST_WIDE_INT
) * 8 * 2];
1090 unsigned HOST_WIDE_INT chunk
;
1091 unsigned chunk_digits
;
1092 char *ptr
= buffer
+ sizeof (buffer
);
1097 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1102 if (sizeof (HOST_WIDE_INT
) >= 8)
1104 /* It is at least 64 bits, so 10^18 is representable. */
1109 type
= signed_or_unsigned_type (1, TREE_TYPE (cst
));
1110 base
= build_int_2 (chunk
, 0);
1111 n
= build_int_2 (TREE_INT_CST_LOW (cst
), TREE_INT_CST_HIGH (cst
));
1112 TREE_TYPE (n
) = TREE_TYPE (base
) = type
;
1117 n
= fold (build1 (NEGATE_EXPR
, type
, n
));
1121 tree d
= fold (build (FLOOR_DIV_EXPR
, type
, n
, base
));
1122 tree tmp
= fold (build (MULT_EXPR
, type
, d
, base
));
1125 done
= integer_zerop (d
);
1126 tmp
= fold (build (MINUS_EXPR
, type
, n
, tmp
));
1127 c
= hwint_to_ascii (TREE_INT_CST_LOW (tmp
), 10, ptr
,
1128 done
? 1 : chunk_digits
);
1134 write_chars (ptr
, count
);
1139 unsigned HOST_WIDE_INT low
= TREE_INT_CST_LOW (cst
);
1146 write_unsigned_number (low
);
1150 /* Non-terminal <identifier>.
1152 <identifier> ::= </unqualified source code identifier> */
1155 write_identifier (identifier
)
1156 const char *identifier
;
1158 MANGLE_TRACE ("identifier", identifier
);
1159 write_string (identifier
);
1162 /* Handle constructor productions of non-terminal <special-name>.
1163 CTOR is a constructor FUNCTION_DECL.
1165 <special-name> ::= C1 # complete object constructor
1166 ::= C2 # base object constructor
1167 ::= C3 # complete object allocating constructor
1169 Currently, allocating constructors are never used.
1171 We also need to provide mangled names for the maybe-in-charge
1172 constructor, so we treat it here too. mangle_decl_string will
1173 append *INTERNAL* to that, to make sure we never emit it. */
1176 write_special_name_constructor (ctor
)
1179 if (DECL_COMPLETE_CONSTRUCTOR_P (ctor
)
1180 /* Even though we don't ever emit a definition of the
1181 old-style destructor, we still have to consider entities
1182 (like static variables) nested inside it. */
1183 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor
))
1184 write_string ("C1");
1185 else if (DECL_BASE_CONSTRUCTOR_P (ctor
))
1186 write_string ("C2");
1191 /* Handle destructor productions of non-terminal <special-name>.
1192 DTOR is a destructor FUNCTION_DECL.
1194 <special-name> ::= D0 # deleting (in-charge) destructor
1195 ::= D1 # complete object (in-charge) destructor
1196 ::= D2 # base object (not-in-charge) destructor
1198 We also need to provide mangled names for the maybe-incharge
1199 destructor, so we treat it here too. mangle_decl_string will
1200 append *INTERNAL* to that, to make sure we never emit it. */
1203 write_special_name_destructor (dtor
)
1206 if (DECL_DELETING_DESTRUCTOR_P (dtor
))
1207 write_string ("D0");
1208 else if (DECL_COMPLETE_DESTRUCTOR_P (dtor
)
1209 /* Even though we don't ever emit a definition of the
1210 old-style destructor, we still have to consider entities
1211 (like static variables) nested inside it. */
1212 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor
))
1213 write_string ("D1");
1214 else if (DECL_BASE_DESTRUCTOR_P (dtor
))
1215 write_string ("D2");
1220 /* Return the discriminator for ENTITY appearing inside
1221 FUNCTION. The discriminator is the lexical ordinal of VAR among
1222 entities with the same name in the same FUNCTION. */
1225 discriminator_for_local_entity (entity
)
1231 /* Assume this is the only local entity with this name. */
1234 if (DECL_DISCRIMINATOR_P (entity
) && DECL_LANG_SPECIFIC (entity
))
1235 discriminator
= DECL_DISCRIMINATOR (entity
);
1236 else if (TREE_CODE (entity
) == TYPE_DECL
)
1238 /* Scan the list of local classes. */
1239 entity
= TREE_TYPE (entity
);
1240 for (type
= &VARRAY_TREE (local_classes
, 0); *type
!= entity
; ++type
)
1241 if (TYPE_IDENTIFIER (*type
) == TYPE_IDENTIFIER (entity
)
1242 && TYPE_CONTEXT (*type
) == TYPE_CONTEXT (entity
))
1246 return discriminator
;
1249 /* Return the discriminator for STRING, a string literal used inside
1250 FUNCTION. The disciminator is the lexical ordinal of STRING among
1251 string literals used in FUNCTION. */
1254 discriminator_for_string_literal (function
, string
)
1255 tree function ATTRIBUTE_UNUSED
;
1256 tree string ATTRIBUTE_UNUSED
;
1258 /* For now, we don't discriminate amongst string literals. */
1262 /* <discriminator> := _ <number>
1264 The discriminator is used only for the second and later occurrences
1265 of the same name within a single function. In this case <number> is
1266 n - 2, if this is the nth occurrence, in lexical order. */
1269 write_discriminator (discriminator
)
1272 /* If discriminator is zero, don't write anything. Otherwise... */
1273 if (discriminator
> 0)
1276 write_unsigned_number (discriminator
- 1);
1280 /* Mangle the name of a function-scope entity. FUNCTION is the
1281 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1282 the entity itself. LOCAL_ENTITY is the entity that's directly
1283 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1286 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1287 := Z <function encoding> E s [<discriminator>] */
1290 write_local_name (function
, local_entity
, entity
)
1295 MANGLE_TRACE_TREE ("local-name", entity
);
1298 write_encoding (function
);
1300 if (TREE_CODE (entity
) == STRING_CST
)
1303 write_discriminator (discriminator_for_string_literal (function
,
1308 /* Now the <entity name>. Let write_name know its being called
1309 from <local-name>, so it doesn't try to process the enclosing
1310 function scope again. */
1311 write_name (entity
, /*ignore_local_scope=*/1);
1312 write_discriminator (discriminator_for_local_entity (local_entity
));
1316 /* Non-terminals <type> and <CV-qualifier>.
1318 <type> ::= <builtin-type>
1320 ::= <class-enum-type>
1322 ::= <pointer-to-member-type>
1323 ::= <template-param>
1326 ::= P <type> # pointer-to
1327 ::= R <type> # reference-to
1328 ::= C <type> # complex pair (C 2000)
1329 ::= G <type> # imaginary (C 2000) [not supported]
1330 ::= U <source-name> <type> # vendor extended type qualifier
1332 TYPE is a type node. */
1338 /* This gets set to non-zero if TYPE turns out to be a (possibly
1339 CV-qualified) builtin type. */
1340 int is_builtin_type
= 0;
1342 MANGLE_TRACE_TREE ("type", type
);
1344 if (type
== error_mark_node
)
1347 if (find_substitution (type
))
1350 if (write_CV_qualifiers_for_type (type
) > 0)
1351 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1352 mangle the unqualified type. The recursive call is needed here
1353 since both the qualified and uqualified types are substitution
1355 write_type (TYPE_MAIN_VARIANT (type
));
1356 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1357 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1358 so that the cv-qualification of the element type is available
1359 in write_array_type. */
1360 write_array_type (type
);
1363 /* See through any typedefs. */
1364 type
= TYPE_MAIN_VARIANT (type
);
1366 switch (TREE_CODE (type
))
1370 case INTEGER_TYPE
: /* Includes wchar_t. */
1372 /* If this is a typedef, TYPE may not be one of
1373 the standard builtin type nodes, but an alias of one. Use
1374 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1375 write_builtin_type (TYPE_MAIN_VARIANT (type
));
1381 write_type (TREE_TYPE (type
));
1386 write_function_type (type
);
1392 /* A pointer-to-member function is represented as a special
1393 RECORD_TYPE, so check for this first. */
1394 if (TYPE_PTRMEMFUNC_P (type
))
1395 write_pointer_to_member_type (type
);
1397 write_class_enum_type (type
);
1401 case UNBOUND_CLASS_TEMPLATE
:
1402 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1403 ordinary nested names. */
1404 write_nested_name (TYPE_STUB_DECL (type
));
1408 /* A pointer-to-member variable is represented by a POINTER_TYPE
1409 to an OFFSET_TYPE, so check for this first. */
1410 if (TYPE_PTRMEM_P (type
))
1411 write_pointer_to_member_type (type
);
1415 write_type (TREE_TYPE (type
));
1419 case REFERENCE_TYPE
:
1421 write_type (TREE_TYPE (type
));
1424 case TEMPLATE_TYPE_PARM
:
1425 case TEMPLATE_PARM_INDEX
:
1426 write_template_param (type
);
1429 case TEMPLATE_TEMPLATE_PARM
:
1430 write_template_template_param (type
);
1433 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1434 write_template_template_param (type
);
1436 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type
)));
1440 write_pointer_to_member_type (build_pointer_type (type
));
1444 write_string ("U8__vector");
1445 write_type (TREE_TYPE (type
));
1453 /* Types other than builtin types are substitution candidates. */
1454 if (!is_builtin_type
)
1455 add_substitution (type
);
1458 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1459 CV-qualifiers written for TYPE.
1461 <CV-qualifiers> ::= [r] [V] [K] */
1464 write_CV_qualifiers_for_type (type
)
1467 int num_qualifiers
= 0;
1469 /* The order is specified by:
1471 "In cases where multiple order-insensitive qualifiers are
1472 present, they should be ordered 'K' (closest to the base type),
1473 'V', 'r', and 'U' (farthest from the base type) ..."
1475 Note that we do not use cp_type_quals below; given "const
1476 int[3]", the "const" is emitted with the "int", not with the
1479 if (TYPE_QUALS (type
) & TYPE_QUAL_RESTRICT
)
1484 if (TYPE_QUALS (type
) & TYPE_QUAL_VOLATILE
)
1489 if (TYPE_QUALS (type
) & TYPE_QUAL_CONST
)
1495 return num_qualifiers
;
1498 /* Non-terminal <builtin-type>.
1500 <builtin-type> ::= v # void
1505 ::= h # unsigned char
1507 ::= t # unsigned short
1509 ::= j # unsigned int
1511 ::= m # unsigned long
1512 ::= x # long long, __int64
1513 ::= y # unsigned long long, __int64
1515 ::= o # unsigned __int128
1518 ::= e # long double, __float80
1519 ::= g # __float128 [not supported]
1520 ::= u <source-name> # vendor extended type */
1523 write_builtin_type (type
)
1526 switch (TREE_CODE (type
))
1537 /* If this is size_t, get the underlying int type. */
1538 if (TYPE_IS_SIZETYPE (type
))
1539 type
= TYPE_DOMAIN (type
);
1541 /* TYPE may still be wchar_t, since that isn't in
1542 integer_type_nodes. */
1543 if (type
== wchar_type_node
)
1545 else if (TYPE_FOR_JAVA (type
))
1546 write_java_integer_type_codes (type
);
1550 /* Assume TYPE is one of the shared integer type nodes. Find
1551 it in the array of these nodes. */
1553 for (itk
= 0; itk
< itk_none
; ++itk
)
1554 if (type
== integer_types
[itk
])
1556 /* Print the corresponding single-letter code. */
1557 write_char (integer_type_codes
[itk
]);
1561 if (itk
== itk_none
)
1563 tree t
= type_for_mode (TYPE_MODE (type
), TREE_UNSIGNED (type
));
1566 if (TYPE_PRECISION (type
) == 128)
1567 write_char (TREE_UNSIGNED (type
) ? 'o' : 'n');
1569 /* Couldn't find this type. */
1582 if (type
== float_type_node
1583 || type
== java_float_type_node
)
1585 else if (type
== double_type_node
1586 || type
== java_double_type_node
)
1588 else if (type
== long_double_type_node
)
1599 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1600 METHOD_TYPE. The return type is mangled before the parameter
1603 <function-type> ::= F [Y] <bare-function-type> E */
1606 write_function_type (type
)
1609 MANGLE_TRACE_TREE ("function-type", type
);
1611 /* For a pointer to member function, the function type may have
1612 cv-qualifiers, indicating the quals for the artificial 'this'
1614 if (TREE_CODE (type
) == METHOD_TYPE
)
1616 /* The first parameter must be a POINTER_TYPE pointing to the
1617 `this' parameter. */
1618 tree this_type
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type
)));
1619 write_CV_qualifiers_for_type (this_type
);
1623 /* We don't track whether or not a type is `extern "C"'. Note that
1624 you can have an `extern "C"' function that does not have
1625 `extern "C"' type, and vice versa:
1627 extern "C" typedef void function_t();
1628 function_t f; // f has C++ linkage, but its type is
1631 typedef void function_t();
1632 extern "C" function_t f; // Vice versa.
1635 write_bare_function_type (type
, /*include_return_type_p=*/1,
1640 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1641 METHOD_TYPE. If INCLUDE_RETURN_TYPE is non-zero, the return value
1642 is mangled before the parameter types. If non-NULL, DECL is
1643 FUNCTION_DECL for the function whose type is being emitted.
1645 <bare-function-type> ::= </signature/ type>+ */
1648 write_bare_function_type (type
, include_return_type_p
, decl
)
1650 int include_return_type_p
;
1653 MANGLE_TRACE_TREE ("bare-function-type", type
);
1655 /* Mangle the return type, if requested. */
1656 if (include_return_type_p
)
1657 write_type (TREE_TYPE (type
));
1659 /* Now mangle the types of the arguments. */
1660 write_method_parms (TYPE_ARG_TYPES (type
),
1661 TREE_CODE (type
) == METHOD_TYPE
,
1665 /* Write the mangled representation of a method parameter list of
1666 types given in PARM_TYPES. If METHOD_P is non-zero, the function is
1667 considered a non-static method, and the this parameter is omitted.
1668 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1669 parameters are being emitted. */
1672 write_method_parms (parm_types
, method_p
, decl
)
1677 tree first_parm_type
;
1678 tree parm_decl
= decl
? DECL_ARGUMENTS (decl
) : NULL_TREE
;
1680 /* Assume this parameter type list is variable-length. If it ends
1681 with a void type, then it's not. */
1684 /* If this is a member function, skip the first arg, which is the
1686 "Member functions do not encode the type of their implicit this
1689 Similarly, there's no need to mangle artificial parameters, like
1690 the VTT parameters for constructors and destructors. */
1693 parm_types
= TREE_CHAIN (parm_types
);
1694 parm_decl
= parm_decl
? TREE_CHAIN (parm_decl
) : NULL_TREE
;
1696 while (parm_decl
&& DECL_ARTIFICIAL (parm_decl
))
1698 parm_types
= TREE_CHAIN (parm_types
);
1699 parm_decl
= TREE_CHAIN (parm_decl
);
1703 for (first_parm_type
= parm_types
;
1705 parm_types
= TREE_CHAIN (parm_types
))
1707 tree parm
= TREE_VALUE (parm_types
);
1708 if (parm
== void_type_node
)
1710 /* "Empty parameter lists, whether declared as () or
1711 conventionally as (void), are encoded with a void parameter
1713 if (parm_types
== first_parm_type
)
1715 /* If the parm list is terminated with a void type, it's
1718 /* A void type better be the last one. */
1719 my_friendly_assert (TREE_CHAIN (parm_types
) == NULL
, 20000523);
1726 /* <builtin-type> ::= z # ellipsis */
1730 /* <class-enum-type> ::= <name> */
1733 write_class_enum_type (type
)
1736 write_name (TYPE_NAME (type
), /*ignore_local_scope=*/0);
1739 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1742 <template-args> ::= I <template-arg>+ E */
1745 write_template_args (args
)
1749 int length
= TREE_VEC_LENGTH (args
);
1751 MANGLE_TRACE_TREE ("template-args", args
);
1753 my_friendly_assert (length
> 0, 20000422);
1755 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
1757 /* We have nested template args. We want the innermost template
1759 args
= TREE_VEC_ELT (args
, length
- 1);
1760 length
= TREE_VEC_LENGTH (args
);
1764 for (i
= 0; i
< length
; ++i
)
1765 write_template_arg (TREE_VEC_ELT (args
, i
));
1769 /* <expression> ::= <unary operator-name> <expression>
1770 ::= <binary operator-name> <expression> <expression>
1773 <expr-primary> ::= <template-param>
1774 ::= L <type> <value number> E # literal
1775 ::= L <mangled-name> E # external name */
1778 write_expression (expr
)
1781 enum tree_code code
;
1783 code
= TREE_CODE (expr
);
1785 /* Handle pointers-to-members by making them look like expression
1787 if (code
== PTRMEM_CST
)
1789 expr
= build_nt (ADDR_EXPR
,
1790 build_nt (SCOPE_REF
,
1791 PTRMEM_CST_CLASS (expr
),
1792 PTRMEM_CST_MEMBER (expr
)));
1793 code
= TREE_CODE (expr
);
1796 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1797 is converted (via qualification conversions) to another
1799 while (TREE_CODE (expr
) == NOP_EXPR
1800 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
1802 expr
= TREE_OPERAND (expr
, 0);
1803 code
= TREE_CODE (expr
);
1806 /* Handle template parameters. */
1807 if (code
== TEMPLATE_TYPE_PARM
1808 || code
== TEMPLATE_TEMPLATE_PARM
1809 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
1810 || code
== TEMPLATE_PARM_INDEX
)
1811 write_template_param (expr
);
1812 /* Handle literals. */
1813 else if (TREE_CODE_CLASS (code
) == 'c')
1814 write_template_arg_literal (expr
);
1815 else if (DECL_P (expr
))
1818 write_mangled_name (expr
);
1821 else if (TREE_CODE (expr
) == SIZEOF_EXPR
1822 && TYPE_P (TREE_OPERAND (expr
, 0)))
1824 write_string ("st");
1825 write_type (TREE_OPERAND (expr
, 0));
1831 /* When we bind a variable or function to a non-type template
1832 argument with reference type, we create an ADDR_EXPR to show
1833 the fact that the entity's address has been taken. But, we
1834 don't actually want to output a mangling code for the `&'. */
1835 if (TREE_CODE (expr
) == ADDR_EXPR
1837 && TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
)
1839 expr
= TREE_OPERAND (expr
, 0);
1842 write_expression (expr
);
1846 code
= TREE_CODE (expr
);
1849 /* If it wasn't any of those, recursively expand the expression. */
1850 write_string (operator_name_info
[(int) code
].mangled_name
);
1855 write_type (TREE_TYPE (expr
));
1856 write_expression (TREE_VALUE (TREE_OPERAND (expr
, 0)));
1859 case STATIC_CAST_EXPR
:
1860 case CONST_CAST_EXPR
:
1861 write_type (TREE_TYPE (expr
));
1862 write_expression (TREE_OPERAND (expr
, 0));
1866 /* Handle pointers-to-members specially. */
1868 write_type (TREE_OPERAND (expr
, 0));
1869 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == IDENTIFIER_NODE
)
1870 write_source_name (TREE_OPERAND (expr
, 1));
1872 write_encoding (TREE_OPERAND (expr
, 1));
1876 for (i
= 0; i
< TREE_CODE_LENGTH (code
); ++i
)
1877 write_expression (TREE_OPERAND (expr
, i
));
1882 /* Literal subcase of non-terminal <template-arg>.
1884 "Literal arguments, e.g. "A<42L>", are encoded with their type
1885 and value. Negative integer values are preceded with "n"; for
1886 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
1887 encoded as 0, true as 1. If floating-point arguments are accepted
1888 as an extension, their values should be encoded using a
1889 fixed-length lowercase hexadecimal string corresponding to the
1890 internal representation (IEEE on IA-64), high-order bytes first,
1891 without leading zeroes. For example: "Lfbff000000E" is -1.0f." */
1894 write_template_arg_literal (value
)
1897 tree type
= TREE_TYPE (value
);
1901 if (TREE_CODE (value
) == CONST_DECL
)
1902 write_integer_cst (DECL_INITIAL (value
));
1903 else if (TREE_CODE (value
) == INTEGER_CST
)
1905 if (same_type_p (type
, boolean_type_node
))
1907 if (value
== boolean_false_node
|| integer_zerop (value
))
1908 write_unsigned_number (0);
1909 else if (value
== boolean_true_node
)
1910 write_unsigned_number (1);
1915 write_integer_cst (value
);
1917 else if (TREE_CODE (value
) == REAL_CST
)
1919 #ifdef CROSS_COMPILE
1920 static int explained
;
1924 sorry ("real-valued template parameters when cross-compiling");
1929 for (i
= 0; i
< sizeof (TREE_REAL_CST (value
)); ++i
)
1930 write_number (((unsigned char *)
1931 &TREE_REAL_CST (value
))[i
],
1942 /* Non-terminal <tempalate-arg>.
1944 <template-arg> ::= <type> # type
1945 ::= L <type> </value/ number> E # literal
1946 ::= LZ <name> E # external name
1947 ::= X <expression> E # expression */
1950 write_template_arg (node
)
1953 enum tree_code code
= TREE_CODE (node
);
1955 MANGLE_TRACE_TREE ("template-arg", node
);
1957 /* A template template paramter's argument list contains TREE_LIST
1958 nodes of which the value field is the the actual argument. */
1959 if (code
== TREE_LIST
)
1961 node
= TREE_VALUE (node
);
1962 /* If it's a decl, deal with its type instead. */
1965 node
= TREE_TYPE (node
);
1966 code
= TREE_CODE (node
);
1972 else if (code
== TEMPLATE_DECL
)
1973 /* A template appearing as a template arg is a template template arg. */
1974 write_template_template_arg (node
);
1975 else if (DECL_P (node
))
1979 write_encoding (node
);
1982 else if (TREE_CODE_CLASS (code
) == 'c' && code
!= PTRMEM_CST
)
1983 write_template_arg_literal (node
);
1986 /* Template arguments may be expressions. */
1988 write_expression (node
);
1993 /* <template-template-arg>
1995 ::= <substitution> */
1998 write_template_template_arg (tree decl
)
2000 MANGLE_TRACE_TREE ("template-template-arg", decl
);
2002 if (find_substitution (decl
))
2004 write_name (decl
, /*ignore_local_scope=*/0);
2005 add_substitution (decl
);
2009 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
2011 <array-type> ::= A [</dimension/ number>] _ </element/ type>
2012 ::= A <expression> _ </element/ type>
2014 "Array types encode the dimension (number of elements) and the
2015 element type. For variable length arrays, the dimension (but not
2016 the '_' separator) is omitted." */
2019 write_array_type (type
)
2023 if (TYPE_DOMAIN (type
))
2028 index_type
= TYPE_DOMAIN (type
);
2029 /* The INDEX_TYPE gives the upper and lower bounds of the
2031 max
= TYPE_MAX_VALUE (index_type
);
2032 if (TREE_CODE (max
) == INTEGER_CST
)
2034 /* The ABI specifies that we should mangle the number of
2035 elements in the array, not the largest allowed index. */
2036 max
= size_binop (PLUS_EXPR
, max
, size_one_node
);
2037 write_unsigned_number (tree_low_cst (max
, 1));
2040 write_expression (TREE_OPERAND (max
, 0));
2043 write_type (TREE_TYPE (type
));
2046 /* Non-terminal <pointer-to-member-type> for pointer-to-member
2047 variables. TYPE is a pointer-to-member POINTER_TYPE.
2049 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2052 write_pointer_to_member_type (type
)
2056 write_type (TYPE_PTRMEM_CLASS_TYPE (type
));
2057 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
2060 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
2061 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2062 TEMPLATE_PARM_INDEX.
2064 <template-param> ::= T </parameter/ number> _ */
2067 write_template_param (parm
)
2072 MANGLE_TRACE_TREE ("template-parm", parm
);
2074 switch (TREE_CODE (parm
))
2076 case TEMPLATE_TYPE_PARM
:
2077 case TEMPLATE_TEMPLATE_PARM
:
2078 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2079 parm_index
= TEMPLATE_TYPE_IDX (parm
);
2082 case TEMPLATE_PARM_INDEX
:
2083 parm_index
= TEMPLATE_PARM_IDX (parm
);
2091 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2092 earliest template param denoted by `_'. */
2094 write_unsigned_number (parm_index
- 1);
2098 /* <template-template-param>
2099 ::= <template-param>
2100 ::= <substitution> */
2103 write_template_template_param (parm
)
2106 tree
template = NULL_TREE
;
2108 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2109 template template parameter. The substitution candidate here is
2110 only the template. */
2111 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
2114 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
));
2115 if (find_substitution (template))
2119 /* <template-param> encodes only the template parameter position,
2120 not its template arguments, which is fine here. */
2121 write_template_param (parm
);
2123 add_substitution (template);
2126 /* Non-terminal <substitution>.
2128 <substitution> ::= S <seq-id> _
2132 write_substitution (seq_id
)
2135 MANGLE_TRACE ("substitution", "");
2139 write_number (seq_id
- 1, /*unsigned=*/1, 36);
2143 /* Start mangling a new name or type. */
2148 obstack_free (&G
.name_obstack
, obstack_base (&G
.name_obstack
));
2151 /* Done with mangling. Return the generated mangled name. */
2153 static inline const char *
2156 /* Clear all the substitutions. */
2157 VARRAY_POP_ALL (G
.substitutions
);
2159 /* Null-terminate the string. */
2162 return (const char *) obstack_base (&G
.name_obstack
);
2165 /* Initialize data structures for mangling. */
2170 gcc_obstack_init (&G
.name_obstack
);
2171 VARRAY_TREE_INIT (G
.substitutions
, 1, "mangling substitutions");
2173 /* Cache these identifiers for quick comparison when checking for
2174 standard substitutions. */
2175 subst_identifiers
[SUBID_ALLOCATOR
] = get_identifier ("allocator");
2176 subst_identifiers
[SUBID_BASIC_STRING
] = get_identifier ("basic_string");
2177 subst_identifiers
[SUBID_CHAR_TRAITS
] = get_identifier ("char_traits");
2178 subst_identifiers
[SUBID_BASIC_ISTREAM
] = get_identifier ("basic_istream");
2179 subst_identifiers
[SUBID_BASIC_OSTREAM
] = get_identifier ("basic_ostream");
2180 subst_identifiers
[SUBID_BASIC_IOSTREAM
] = get_identifier ("basic_iostream");
2183 /* Generate the mangled name of DECL. */
2186 mangle_decl_string (decl
)
2193 if (TREE_CODE (decl
) == TYPE_DECL
)
2194 write_type (TREE_TYPE (decl
));
2195 else if (/* The names of `extern "C"' functions are not mangled. */
2196 (DECL_EXTERN_C_FUNCTION_P (decl
)
2197 /* But overloaded operator names *are* mangled. */
2198 && !DECL_OVERLOADED_OPERATOR_P (decl
))
2199 /* The names of global variables aren't mangled either. */
2200 || (TREE_CODE (decl
) == VAR_DECL
2201 && CP_DECL_CONTEXT (decl
) == global_namespace
)
2202 /* And neither are `extern "C"' variables. */
2203 || (TREE_CODE (decl
) == VAR_DECL
2204 && DECL_EXTERN_C_P (decl
)))
2205 write_string (IDENTIFIER_POINTER (DECL_NAME (decl
)));
2208 write_mangled_name (decl
);
2209 if (DECL_LANG_SPECIFIC (decl
)
2210 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl
)
2211 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl
)))
2212 /* We need a distinct mangled name for these entities, but
2213 we should never actually output it. So, we append some
2214 characters the assembler won't like. */
2215 write_string (" *INTERNAL* ");
2218 result
= finish_mangling ();
2220 fprintf (stderr
, "mangle_decl_string = '%s'\n\n", result
);
2224 /* Create an identifier for the external mangled name of DECL. */
2230 tree id
= get_identifier (mangle_decl_string (decl
));
2232 SET_DECL_ASSEMBLER_NAME (decl
, id
);
2235 /* Generate the mangled representation of TYPE. */
2238 mangle_type_string (type
)
2245 result
= finish_mangling ();
2247 fprintf (stderr
, "mangle_type_string = '%s'\n\n", result
);
2251 /* Create an identifier for the mangled representation of TYPE. */
2257 return get_identifier (mangle_type_string (type
));
2260 /* Create an identifier for the mangled name of a special component
2261 for belonging to TYPE. CODE is the ABI-specified code for this
2265 mangle_special_for_type (type
, code
)
2271 /* We don't have an actual decl here for the special component, so
2272 we can't just process the <encoded-name>. Instead, fake it. */
2275 /* Start the mangling. */
2276 write_string ("_Z");
2277 write_string (code
);
2281 result
= finish_mangling ();
2284 fprintf (stderr
, "mangle_special_for_type = %s\n\n", result
);
2286 return get_identifier (result
);
2289 /* Create an identifier for the mangled representation of the typeinfo
2290 structure for TYPE. */
2293 mangle_typeinfo_for_type (type
)
2296 return mangle_special_for_type (type
, "TI");
2299 /* Create an identifier for the mangled name of the NTBS containing
2300 the mangled name of TYPE. */
2303 mangle_typeinfo_string_for_type (type
)
2306 return mangle_special_for_type (type
, "TS");
2309 /* Create an identifier for the mangled name of the vtable for TYPE. */
2312 mangle_vtbl_for_type (type
)
2315 return mangle_special_for_type (type
, "TV");
2318 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2321 mangle_vtt_for_type (type
)
2324 return mangle_special_for_type (type
, "TT");
2327 /* Return an identifier for a construction vtable group. TYPE is
2328 the most derived class in the hierarchy; BINFO is the base
2329 subobject for which this construction vtable group will be used.
2331 This mangling isn't part of the ABI specification; in the ABI
2332 specification, the vtable group is dumped in the same COMDAT as the
2333 main vtable, and is referenced only from that vtable, so it doesn't
2334 need an external name. For binary formats without COMDAT sections,
2335 though, we need external names for the vtable groups.
2337 We use the production
2339 <special-name> ::= CT <type> <offset number> _ <base type> */
2342 mangle_ctor_vtbl_for_type (type
, binfo
)
2350 write_string ("_Z");
2351 write_string ("TC");
2353 write_integer_cst (BINFO_OFFSET (binfo
));
2355 write_type (BINFO_TYPE (binfo
));
2357 result
= finish_mangling ();
2359 fprintf (stderr
, "mangle_ctor_vtbl_for_type = %s\n\n", result
);
2360 return get_identifier (result
);
2363 /* Return an identifier for the mangled name of a thunk to FN_DECL.
2364 OFFSET is the initial adjustment to this used to find the vptr. If
2365 VCALL_OFFSET is non-NULL, this is a virtual thunk, and it is the
2366 vtbl offset in bytes.
2368 <special-name> ::= Th <offset number> _ <base encoding>
2369 ::= Tv <offset number> _ <vcall offset number> _
2374 mangle_thunk (fn_decl
, offset
, vcall_offset
)
2383 write_string ("_Z");
2384 /* The <special-name> for virtual thunks is Tv, for non-virtual
2387 if (vcall_offset
!= 0)
2392 /* For either flavor, write the offset to this. */
2393 write_integer_cst (offset
);
2396 /* For a virtual thunk, add the vcall offset. */
2399 /* Virtual thunk. Write the vcall offset and base type name. */
2400 write_integer_cst (vcall_offset
);
2405 write_encoding (fn_decl
);
2407 result
= finish_mangling ();
2409 fprintf (stderr
, "mangle_thunk = %s\n\n", result
);
2410 return get_identifier (result
);
2413 /* Return an identifier for the mangled unqualified name for a
2414 conversion operator to TYPE. This mangling is not specified by the
2415 ABI spec; it is only used internally. */
2418 mangle_conv_op_name_for_type (type
)
2423 /* Build the mangling for TYPE. */
2424 const char *mangled_type
= mangle_type_string (type
);
2425 /* Allocate a temporary buffer for the complete name. */
2426 char *op_name
= concat ("operator ", mangled_type
, NULL
);
2427 /* Find or create an identifier. */
2428 identifier
= get_identifier (op_name
);
2429 /* Done with the temporary buffer. */
2431 /* Set bits on the identifier so we know later it's a conversion. */
2432 IDENTIFIER_OPNAME_P (identifier
) = 1;
2433 IDENTIFIER_TYPENAME_P (identifier
) = 1;
2434 /* Hang TYPE off the identifier so it can be found easily later when
2435 performing conversions. */
2436 TREE_TYPE (identifier
) = type
;
2441 /* Return an identifier for the name of an initialization guard
2442 variable for indicated VARIABLE. */
2445 mangle_guard_variable (variable
)
2449 write_string ("_ZGV");
2450 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable
)), "_ZGR", 4) == 0)
2451 /* The name of a guard variable for a reference temporary should refer
2452 to the reference, not the temporary. */
2453 write_string (IDENTIFIER_POINTER (DECL_NAME (variable
)) + 4);
2455 write_name (variable
, /*ignore_local_scope=*/0);
2456 return get_identifier (finish_mangling ());
2459 /* Return an identifier for the name of a temporary variable used to
2460 initialize a static reference. This isn't part of the ABI, but we might
2461 as well call them something readable. */
2464 mangle_ref_init_variable (variable
)
2468 write_string ("_ZGR");
2469 write_name (variable
, /*ignore_local_scope=*/0);
2470 return get_identifier (finish_mangling ());
2474 /* Foreign language type mangling section. */
2476 /* How to write the type codes for the integer Java type. */
2479 write_java_integer_type_codes (type
)
2482 if (type
== java_int_type_node
)
2484 else if (type
== java_short_type_node
)
2486 else if (type
== java_byte_type_node
)
2488 else if (type
== java_char_type_node
)
2490 else if (type
== java_long_type_node
)
2492 else if (type
== java_boolean_type_node
)