gcc/
[official-gcc.git] / gcc / cp / mangle.c
blob5452f6cce795220306dfb72d9f37c9e0a2a10366
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Written by Alex Samuel <samuel@codesourcery.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This file implements mangling of C++ names according to the IA64
24 C++ ABI specification. A mangled name encodes a function or
25 variable's name, scope, type, and/or template arguments into a text
26 identifier. This identifier is used as the function's or
27 variable's linkage name, to preserve compatibility between C++'s
28 language features (templates, scoping, and overloading) and C
29 linkers.
31 Additionally, g++ uses mangled names internally. To support this,
32 mangling of types is allowed, even though the mangled name of a
33 type should not appear by itself as an exported name. Ditto for
34 uninstantiated templates.
36 The primary entry point for this module is mangle_decl, which
37 returns an identifier containing the mangled name for a decl.
38 Additional entry points are provided to build mangled names of
39 particular constructs when the appropriate decl for that construct
40 is not available. These are:
42 mangle_typeinfo_for_type: typeinfo data
43 mangle_typeinfo_string_for_type: typeinfo type name
44 mangle_vtbl_for_type: virtual table data
45 mangle_vtt_for_type: VTT data
46 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
47 mangle_thunk: thunk function or entry */
49 #include "config.h"
50 #include "system.h"
51 #include "coretypes.h"
52 #include "tm.h"
53 #include "tree.h"
54 #include "tm_p.h"
55 #include "cp-tree.h"
56 #include "real.h"
57 #include "obstack.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "flags.h"
61 #include "target.h"
63 /* Debugging support. */
65 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
66 #ifndef DEBUG_MANGLE
67 #define DEBUG_MANGLE 0
68 #endif
70 /* Macros for tracing the write_* functions. */
71 #if DEBUG_MANGLE
72 # define MANGLE_TRACE(FN, INPUT) \
73 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
74 # define MANGLE_TRACE_TREE(FN, NODE) \
75 fprintf (stderr, " %-24s: %-24s (%p)\n", \
76 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
77 #else
78 # define MANGLE_TRACE(FN, INPUT)
79 # define MANGLE_TRACE_TREE(FN, NODE)
80 #endif
82 /* Nonzero if NODE is a class template-id. We can't rely on
83 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
84 that hard to distinguish A<T> from A, where A<T> is the type as
85 instantiated outside of the template, and A is the type used
86 without parameters inside the template. */
87 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
88 (TYPE_LANG_SPECIFIC (NODE) != NULL \
89 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
90 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
91 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
93 /* Things we only need one of. This module is not reentrant. */
94 typedef struct globals GTY(())
96 /* An array of the current substitution candidates, in the order
97 we've seen them. */
98 VEC(tree,gc) *substitutions;
100 /* The entity that is being mangled. */
101 tree GTY ((skip)) entity;
103 /* True if the mangling will be different in a future version of the
104 ABI. */
105 bool need_abi_warning;
106 } globals;
108 static GTY (()) globals G;
110 /* The obstack on which we build mangled names. */
111 static struct obstack *mangle_obstack;
113 /* The obstack on which we build mangled names that are not going to
114 be IDENTIFIER_NODEs. */
115 static struct obstack name_obstack;
117 /* The first object on the name_obstack; we use this to free memory
118 allocated on the name_obstack. */
119 static void *name_base;
121 /* An incomplete mangled name. There will be no NUL terminator. If
122 there is no incomplete mangled name, this variable is NULL. */
123 static char *partially_mangled_name;
125 /* The number of characters in the PARTIALLY_MANGLED_NAME. */
126 static size_t partially_mangled_name_len;
128 /* Indices into subst_identifiers. These are identifiers used in
129 special substitution rules. */
130 typedef enum
132 SUBID_ALLOCATOR,
133 SUBID_BASIC_STRING,
134 SUBID_CHAR_TRAITS,
135 SUBID_BASIC_ISTREAM,
136 SUBID_BASIC_OSTREAM,
137 SUBID_BASIC_IOSTREAM,
138 SUBID_MAX
140 substitution_identifier_index_t;
142 /* For quick substitution checks, look up these common identifiers
143 once only. */
144 static GTY(()) tree subst_identifiers[SUBID_MAX];
146 /* Single-letter codes for builtin integer types, defined in
147 <builtin-type>. These are indexed by integer_type_kind values. */
148 static const char
149 integer_type_codes[itk_none] =
151 'c', /* itk_char */
152 'a', /* itk_signed_char */
153 'h', /* itk_unsigned_char */
154 's', /* itk_short */
155 't', /* itk_unsigned_short */
156 'i', /* itk_int */
157 'j', /* itk_unsigned_int */
158 'l', /* itk_long */
159 'm', /* itk_unsigned_long */
160 'x', /* itk_long_long */
161 'y' /* itk_unsigned_long_long */
164 static int decl_is_template_id (const tree, tree* const);
166 /* Functions for handling substitutions. */
168 static inline tree canonicalize_for_substitution (tree);
169 static void add_substitution (tree);
170 static inline int is_std_substitution (const tree,
171 const substitution_identifier_index_t);
172 static inline int is_std_substitution_char (const tree,
173 const substitution_identifier_index_t);
174 static int find_substitution (tree);
175 static void mangle_call_offset (const tree, const tree);
177 /* Functions for emitting mangled representations of things. */
179 static void write_mangled_name (const tree, bool);
180 static void write_encoding (const tree);
181 static void write_name (tree, const int);
182 static void write_unscoped_name (const tree);
183 static void write_unscoped_template_name (const tree);
184 static void write_nested_name (const tree);
185 static void write_prefix (const tree);
186 static void write_template_prefix (const tree);
187 static void write_unqualified_name (const tree);
188 static void write_conversion_operator_name (const tree);
189 static void write_source_name (tree);
190 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
191 const unsigned int);
192 static void write_number (unsigned HOST_WIDE_INT, const int,
193 const unsigned int);
194 static void write_integer_cst (const tree);
195 static void write_real_cst (const tree);
196 static void write_identifier (const char *);
197 static void write_special_name_constructor (const tree);
198 static void write_special_name_destructor (const tree);
199 static void write_type (tree);
200 static int write_CV_qualifiers_for_type (const tree);
201 static void write_builtin_type (tree);
202 static void write_function_type (const tree);
203 static void write_bare_function_type (const tree, const int, const tree);
204 static void write_method_parms (tree, const int, const tree);
205 static void write_class_enum_type (const tree);
206 static void write_template_args (tree);
207 static void write_expression (tree);
208 static void write_template_arg_literal (const tree);
209 static void write_template_arg (tree);
210 static void write_template_template_arg (const tree);
211 static void write_array_type (const tree);
212 static void write_pointer_to_member_type (const tree);
213 static void write_template_param (const tree);
214 static void write_template_template_param (const tree);
215 static void write_substitution (const int);
216 static int discriminator_for_local_entity (tree);
217 static int discriminator_for_string_literal (tree, tree);
218 static void write_discriminator (const int);
219 static void write_local_name (const tree, const tree, const tree);
220 static void dump_substitution_candidates (void);
221 static const char *mangle_decl_string (const tree);
223 /* Control functions. */
225 static inline void start_mangling (const tree, bool);
226 static inline const char *finish_mangling (const bool);
227 static tree mangle_special_for_type (const tree, const char *);
229 /* Foreign language functions. */
231 static void write_java_integer_type_codes (const tree);
233 /* Append a single character to the end of the mangled
234 representation. */
235 #define write_char(CHAR) \
236 obstack_1grow (mangle_obstack, (CHAR))
238 /* Append a sized buffer to the end of the mangled representation. */
239 #define write_chars(CHAR, LEN) \
240 obstack_grow (mangle_obstack, (CHAR), (LEN))
242 /* Append a NUL-terminated string to the end of the mangled
243 representation. */
244 #define write_string(STRING) \
245 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
247 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
248 same purpose (context, which may be a type) and value (template
249 decl). See write_template_prefix for more information on what this
250 is used for. */
251 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
252 (TREE_CODE (NODE1) == TREE_LIST \
253 && TREE_CODE (NODE2) == TREE_LIST \
254 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
255 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
256 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
257 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
259 /* Write out an unsigned quantity in base 10. */
260 #define write_unsigned_number(NUMBER) \
261 write_number ((NUMBER), /*unsigned_p=*/1, 10)
263 /* Save the current (incomplete) mangled name and release the obstack
264 storage holding it. This function should be used during mangling
265 when making a call that could result in a call to get_identifier,
266 as such a call will clobber the same obstack being used for
267 mangling. This function may not be called twice without an
268 intervening call to restore_partially_mangled_name. */
270 static void
271 save_partially_mangled_name (void)
273 if (mangle_obstack == &ident_hash->stack)
275 gcc_assert (!partially_mangled_name);
276 partially_mangled_name_len = obstack_object_size (mangle_obstack);
277 partially_mangled_name = XNEWVEC (char, partially_mangled_name_len);
278 memcpy (partially_mangled_name, obstack_base (mangle_obstack),
279 partially_mangled_name_len);
280 obstack_free (mangle_obstack, obstack_finish (mangle_obstack));
284 /* Restore the incomplete mangled name saved with
285 save_partially_mangled_name. */
287 static void
288 restore_partially_mangled_name (void)
290 if (partially_mangled_name)
292 obstack_grow (mangle_obstack, partially_mangled_name,
293 partially_mangled_name_len);
294 free (partially_mangled_name);
295 partially_mangled_name = NULL;
299 /* If DECL is a template instance, return nonzero and, if
300 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
301 Otherwise return zero. */
303 static int
304 decl_is_template_id (const tree decl, tree* const template_info)
306 if (TREE_CODE (decl) == TYPE_DECL)
308 /* TYPE_DECLs are handled specially. Look at its type to decide
309 if this is a template instantiation. */
310 const tree type = TREE_TYPE (decl);
312 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
314 if (template_info != NULL)
315 /* For a templated TYPE_DECL, the template info is hanging
316 off the type. */
317 *template_info = TYPE_TEMPLATE_INFO (type);
318 return 1;
321 else
323 /* Check if this is a primary template. */
324 if (DECL_LANG_SPECIFIC (decl) != NULL
325 && DECL_USE_TEMPLATE (decl)
326 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
327 && TREE_CODE (decl) != TEMPLATE_DECL)
329 if (template_info != NULL)
330 /* For most templated decls, the template info is hanging
331 off the decl. */
332 *template_info = DECL_TEMPLATE_INFO (decl);
333 return 1;
337 /* It's not a template id. */
338 return 0;
341 /* Produce debugging output of current substitution candidates. */
343 static void
344 dump_substitution_candidates (void)
346 unsigned i;
347 tree el;
349 fprintf (stderr, " ++ substitutions ");
350 for (i = 0; VEC_iterate (tree, G.substitutions, i, el); ++i)
352 const char *name = "???";
354 if (i > 0)
355 fprintf (stderr, " ");
356 if (DECL_P (el))
357 name = IDENTIFIER_POINTER (DECL_NAME (el));
358 else if (TREE_CODE (el) == TREE_LIST)
359 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
360 else if (TYPE_NAME (el))
361 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
362 fprintf (stderr, " S%d_ = ", i - 1);
363 if (TYPE_P (el) &&
364 (CP_TYPE_RESTRICT_P (el)
365 || CP_TYPE_VOLATILE_P (el)
366 || CP_TYPE_CONST_P (el)))
367 fprintf (stderr, "CV-");
368 fprintf (stderr, "%s (%s at %p)\n",
369 name, tree_code_name[TREE_CODE (el)], (void *) el);
373 /* Both decls and types can be substitution candidates, but sometimes
374 they refer to the same thing. For instance, a TYPE_DECL and
375 RECORD_TYPE for the same class refer to the same thing, and should
376 be treated accordingly in substitutions. This function returns a
377 canonicalized tree node representing NODE that is used when adding
378 and substitution candidates and finding matches. */
380 static inline tree
381 canonicalize_for_substitution (tree node)
383 /* For a TYPE_DECL, use the type instead. */
384 if (TREE_CODE (node) == TYPE_DECL)
385 node = TREE_TYPE (node);
386 if (TYPE_P (node))
387 node = canonical_type_variant (node);
389 return node;
392 /* Add NODE as a substitution candidate. NODE must not already be on
393 the list of candidates. */
395 static void
396 add_substitution (tree node)
398 tree c;
400 if (DEBUG_MANGLE)
401 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
402 tree_code_name[TREE_CODE (node)], (void *) node);
404 /* Get the canonicalized substitution candidate for NODE. */
405 c = canonicalize_for_substitution (node);
406 if (DEBUG_MANGLE && c != node)
407 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
408 tree_code_name[TREE_CODE (node)], (void *) node);
409 node = c;
411 #if ENABLE_CHECKING
412 /* Make sure NODE isn't already a candidate. */
414 int i;
415 tree candidate;
417 for (i = 0; VEC_iterate (tree, G.substitutions, i, candidate); i++)
419 gcc_assert (!(DECL_P (node) && node == candidate));
420 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
421 && same_type_p (node, candidate)));
424 #endif /* ENABLE_CHECKING */
426 /* Put the decl onto the varray of substitution candidates. */
427 VEC_safe_push (tree, gc, G.substitutions, node);
429 if (DEBUG_MANGLE)
430 dump_substitution_candidates ();
433 /* Helper function for find_substitution. Returns nonzero if NODE,
434 which may be a decl or a CLASS_TYPE, is a template-id with template
435 name of substitution_index[INDEX] in the ::std namespace. */
437 static inline int
438 is_std_substitution (const tree node,
439 const substitution_identifier_index_t index)
441 tree type = NULL;
442 tree decl = NULL;
444 if (DECL_P (node))
446 type = TREE_TYPE (node);
447 decl = node;
449 else if (CLASS_TYPE_P (node))
451 type = node;
452 decl = TYPE_NAME (node);
454 else
455 /* These are not the droids you're looking for. */
456 return 0;
458 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
459 && TYPE_LANG_SPECIFIC (type)
460 && TYPE_TEMPLATE_INFO (type)
461 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
462 == subst_identifiers[index]));
465 /* Helper function for find_substitution. Returns nonzero if NODE,
466 which may be a decl or a CLASS_TYPE, is the template-id
467 ::std::identifier<char>, where identifier is
468 substitution_index[INDEX]. */
470 static inline int
471 is_std_substitution_char (const tree node,
472 const substitution_identifier_index_t index)
474 tree args;
475 /* Check NODE's name is ::std::identifier. */
476 if (!is_std_substitution (node, index))
477 return 0;
478 /* Figure out its template args. */
479 if (DECL_P (node))
480 args = DECL_TI_ARGS (node);
481 else if (CLASS_TYPE_P (node))
482 args = CLASSTYPE_TI_ARGS (node);
483 else
484 /* Oops, not a template. */
485 return 0;
486 /* NODE's template arg list should be <char>. */
487 return
488 TREE_VEC_LENGTH (args) == 1
489 && TREE_VEC_ELT (args, 0) == char_type_node;
492 /* Check whether a substitution should be used to represent NODE in
493 the mangling.
495 First, check standard special-case substitutions.
497 <substitution> ::= St
498 # ::std
500 ::= Sa
501 # ::std::allocator
503 ::= Sb
504 # ::std::basic_string
506 ::= Ss
507 # ::std::basic_string<char,
508 ::std::char_traits<char>,
509 ::std::allocator<char> >
511 ::= Si
512 # ::std::basic_istream<char, ::std::char_traits<char> >
514 ::= So
515 # ::std::basic_ostream<char, ::std::char_traits<char> >
517 ::= Sd
518 # ::std::basic_iostream<char, ::std::char_traits<char> >
520 Then examine the stack of currently available substitution
521 candidates for entities appearing earlier in the same mangling
523 If a substitution is found, write its mangled representation and
524 return nonzero. If none is found, just return zero. */
526 static int
527 find_substitution (tree node)
529 int i;
530 const int size = VEC_length (tree, G.substitutions);
531 tree decl;
532 tree type;
534 if (DEBUG_MANGLE)
535 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
536 tree_code_name[TREE_CODE (node)], (void *) node);
538 /* Obtain the canonicalized substitution representation for NODE.
539 This is what we'll compare against. */
540 node = canonicalize_for_substitution (node);
542 /* Check for builtin substitutions. */
544 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
545 type = TYPE_P (node) ? node : TREE_TYPE (node);
547 /* Check for std::allocator. */
548 if (decl
549 && is_std_substitution (decl, SUBID_ALLOCATOR)
550 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
552 write_string ("Sa");
553 return 1;
556 /* Check for std::basic_string. */
557 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
559 if (TYPE_P (node))
561 /* If this is a type (i.e. a fully-qualified template-id),
562 check for
563 std::basic_string <char,
564 std::char_traits<char>,
565 std::allocator<char> > . */
566 if (cp_type_quals (type) == TYPE_UNQUALIFIED
567 && CLASSTYPE_USE_TEMPLATE (type))
569 tree args = CLASSTYPE_TI_ARGS (type);
570 if (TREE_VEC_LENGTH (args) == 3
571 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
572 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
573 SUBID_CHAR_TRAITS)
574 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
575 SUBID_ALLOCATOR))
577 write_string ("Ss");
578 return 1;
582 else
583 /* Substitute for the template name only if this isn't a type. */
585 write_string ("Sb");
586 return 1;
590 /* Check for basic_{i,o,io}stream. */
591 if (TYPE_P (node)
592 && cp_type_quals (type) == TYPE_UNQUALIFIED
593 && CLASS_TYPE_P (type)
594 && CLASSTYPE_USE_TEMPLATE (type)
595 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
597 /* First, check for the template
598 args <char, std::char_traits<char> > . */
599 tree args = CLASSTYPE_TI_ARGS (type);
600 if (TREE_VEC_LENGTH (args) == 2
601 && TYPE_P (TREE_VEC_ELT (args, 0))
602 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
603 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
604 SUBID_CHAR_TRAITS))
606 /* Got them. Is this basic_istream? */
607 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
609 write_string ("Si");
610 return 1;
612 /* Or basic_ostream? */
613 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
615 write_string ("So");
616 return 1;
618 /* Or basic_iostream? */
619 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
621 write_string ("Sd");
622 return 1;
627 /* Check for namespace std. */
628 if (decl && DECL_NAMESPACE_STD_P (decl))
630 write_string ("St");
631 return 1;
634 /* Now check the list of available substitutions for this mangling
635 operation. */
636 for (i = 0; i < size; ++i)
638 tree candidate = VEC_index (tree, G.substitutions, i);
639 /* NODE is a matched to a candidate if it's the same decl node or
640 if it's the same type. */
641 if (decl == candidate
642 || (TYPE_P (candidate) && type && TYPE_P (type)
643 && same_type_p (type, candidate))
644 || NESTED_TEMPLATE_MATCH (node, candidate))
646 write_substitution (i);
647 return 1;
651 /* No substitution found. */
652 return 0;
656 /* TOP_LEVEL is true, if this is being called at outermost level of
657 mangling. It should be false when mangling a decl appearing in an
658 expression within some other mangling.
660 <mangled-name> ::= _Z <encoding> */
662 static void
663 write_mangled_name (const tree decl, bool top_level)
665 MANGLE_TRACE_TREE ("mangled-name", decl);
667 if (/* The names of `extern "C"' functions are not mangled. */
668 DECL_EXTERN_C_FUNCTION_P (decl)
669 /* But overloaded operator names *are* mangled. */
670 && !DECL_OVERLOADED_OPERATOR_P (decl))
672 unmangled_name:;
674 if (top_level)
675 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
676 else
678 /* The standard notes: "The <encoding> of an extern "C"
679 function is treated like global-scope data, i.e. as its
680 <source-name> without a type." We cannot write
681 overloaded operators that way though, because it contains
682 characters invalid in assembler. */
683 if (abi_version_at_least (2))
684 write_string ("_Z");
685 else
686 G.need_abi_warning = true;
687 write_source_name (DECL_NAME (decl));
690 else if (TREE_CODE (decl) == VAR_DECL
691 /* The names of non-static global variables aren't mangled. */
692 && DECL_EXTERNAL_LINKAGE_P (decl)
693 && (CP_DECL_CONTEXT (decl) == global_namespace
694 /* And neither are `extern "C"' variables. */
695 || DECL_EXTERN_C_P (decl)))
697 if (top_level || abi_version_at_least (2))
698 goto unmangled_name;
699 else
701 G.need_abi_warning = true;
702 goto mangled_name;
705 else
707 mangled_name:;
708 write_string ("_Z");
709 write_encoding (decl);
710 if (DECL_LANG_SPECIFIC (decl)
711 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
712 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
713 /* We need a distinct mangled name for these entities, but
714 we should never actually output it. So, we append some
715 characters the assembler won't like. */
716 write_string (" *INTERNAL* ");
720 /* <encoding> ::= <function name> <bare-function-type>
721 ::= <data name> */
723 static void
724 write_encoding (const tree decl)
726 MANGLE_TRACE_TREE ("encoding", decl);
728 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
730 /* For overloaded operators write just the mangled name
731 without arguments. */
732 if (DECL_OVERLOADED_OPERATOR_P (decl))
733 write_name (decl, /*ignore_local_scope=*/0);
734 else
735 write_source_name (DECL_NAME (decl));
736 return;
739 write_name (decl, /*ignore_local_scope=*/0);
740 if (TREE_CODE (decl) == FUNCTION_DECL)
742 tree fn_type;
743 tree d;
745 if (decl_is_template_id (decl, NULL))
747 save_partially_mangled_name ();
748 fn_type = get_mostly_instantiated_function_type (decl);
749 restore_partially_mangled_name ();
750 /* FN_TYPE will not have parameter types for in-charge or
751 VTT parameters. Therefore, we pass NULL_TREE to
752 write_bare_function_type -- otherwise, it will get
753 confused about which artificial parameters to skip. */
754 d = NULL_TREE;
756 else
758 fn_type = TREE_TYPE (decl);
759 d = decl;
762 write_bare_function_type (fn_type,
763 (!DECL_CONSTRUCTOR_P (decl)
764 && !DECL_DESTRUCTOR_P (decl)
765 && !DECL_CONV_FN_P (decl)
766 && decl_is_template_id (decl, NULL)),
771 /* <name> ::= <unscoped-name>
772 ::= <unscoped-template-name> <template-args>
773 ::= <nested-name>
774 ::= <local-name>
776 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
777 called from <local-name>, which mangles the enclosing scope
778 elsewhere and then uses this function to mangle just the part
779 underneath the function scope. So don't use the <local-name>
780 production, to avoid an infinite recursion. */
782 static void
783 write_name (tree decl, const int ignore_local_scope)
785 tree context;
787 MANGLE_TRACE_TREE ("name", decl);
789 if (TREE_CODE (decl) == TYPE_DECL)
791 /* In case this is a typedef, fish out the corresponding
792 TYPE_DECL for the main variant. */
793 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
794 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
796 else
797 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
799 /* A decl in :: or ::std scope is treated specially. The former is
800 mangled using <unscoped-name> or <unscoped-template-name>, the
801 latter with a special substitution. Also, a name that is
802 directly in a local function scope is also mangled with
803 <unscoped-name> rather than a full <nested-name>. */
804 if (context == NULL
805 || context == global_namespace
806 || DECL_NAMESPACE_STD_P (context)
807 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
809 tree template_info;
810 /* Is this a template instance? */
811 if (decl_is_template_id (decl, &template_info))
813 /* Yes: use <unscoped-template-name>. */
814 write_unscoped_template_name (TI_TEMPLATE (template_info));
815 write_template_args (TI_ARGS (template_info));
817 else
818 /* Everything else gets an <unqualified-name>. */
819 write_unscoped_name (decl);
821 else
823 /* Handle local names, unless we asked not to (that is, invoked
824 under <local-name>, to handle only the part of the name under
825 the local scope). */
826 if (!ignore_local_scope)
828 /* Scan up the list of scope context, looking for a
829 function. If we find one, this entity is in local
830 function scope. local_entity tracks context one scope
831 level down, so it will contain the element that's
832 directly in that function's scope, either decl or one of
833 its enclosing scopes. */
834 tree local_entity = decl;
835 while (context != NULL && context != global_namespace)
837 /* Make sure we're always dealing with decls. */
838 if (context != NULL && TYPE_P (context))
839 context = TYPE_NAME (context);
840 /* Is this a function? */
841 if (TREE_CODE (context) == FUNCTION_DECL)
843 /* Yes, we have local scope. Use the <local-name>
844 production for the innermost function scope. */
845 write_local_name (context, local_entity, decl);
846 return;
848 /* Up one scope level. */
849 local_entity = context;
850 context = CP_DECL_CONTEXT (context);
853 /* No local scope found? Fall through to <nested-name>. */
856 /* Other decls get a <nested-name> to encode their scope. */
857 write_nested_name (decl);
861 /* <unscoped-name> ::= <unqualified-name>
862 ::= St <unqualified-name> # ::std:: */
864 static void
865 write_unscoped_name (const tree decl)
867 tree context = CP_DECL_CONTEXT (decl);
869 MANGLE_TRACE_TREE ("unscoped-name", decl);
871 /* Is DECL in ::std? */
872 if (DECL_NAMESPACE_STD_P (context))
874 write_string ("St");
875 write_unqualified_name (decl);
877 else
879 /* If not, it should be either in the global namespace, or directly
880 in a local function scope. */
881 gcc_assert (context == global_namespace
882 || context == NULL
883 || TREE_CODE (context) == FUNCTION_DECL);
885 write_unqualified_name (decl);
889 /* <unscoped-template-name> ::= <unscoped-name>
890 ::= <substitution> */
892 static void
893 write_unscoped_template_name (const tree decl)
895 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
897 if (find_substitution (decl))
898 return;
899 write_unscoped_name (decl);
900 add_substitution (decl);
903 /* Write the nested name, including CV-qualifiers, of DECL.
905 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
906 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
908 <CV-qualifiers> ::= [r] [V] [K] */
910 static void
911 write_nested_name (const tree decl)
913 tree template_info;
915 MANGLE_TRACE_TREE ("nested-name", decl);
917 write_char ('N');
919 /* Write CV-qualifiers, if this is a member function. */
920 if (TREE_CODE (decl) == FUNCTION_DECL
921 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
923 if (DECL_VOLATILE_MEMFUNC_P (decl))
924 write_char ('V');
925 if (DECL_CONST_MEMFUNC_P (decl))
926 write_char ('K');
929 /* Is this a template instance? */
930 if (decl_is_template_id (decl, &template_info))
932 /* Yes, use <template-prefix>. */
933 write_template_prefix (decl);
934 write_template_args (TI_ARGS (template_info));
936 else
938 /* No, just use <prefix> */
939 write_prefix (DECL_CONTEXT (decl));
940 write_unqualified_name (decl);
942 write_char ('E');
945 /* <prefix> ::= <prefix> <unqualified-name>
946 ::= <template-param>
947 ::= <template-prefix> <template-args>
948 ::= # empty
949 ::= <substitution> */
951 static void
952 write_prefix (const tree node)
954 tree decl;
955 /* Non-NULL if NODE represents a template-id. */
956 tree template_info = NULL;
958 MANGLE_TRACE_TREE ("prefix", node);
960 if (node == NULL
961 || node == global_namespace)
962 return;
964 if (find_substitution (node))
965 return;
967 if (DECL_P (node))
969 /* If this is a function decl, that means we've hit function
970 scope, so this prefix must be for a local name. In this
971 case, we're under the <local-name> production, which encodes
972 the enclosing function scope elsewhere. So don't continue
973 here. */
974 if (TREE_CODE (node) == FUNCTION_DECL)
975 return;
977 decl = node;
978 decl_is_template_id (decl, &template_info);
980 else
982 /* Node is a type. */
983 decl = TYPE_NAME (node);
984 if (CLASSTYPE_TEMPLATE_ID_P (node))
985 template_info = TYPE_TEMPLATE_INFO (node);
988 /* In G++ 3.2, the name of the template parameter was used. */
989 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
990 && !abi_version_at_least (2))
991 G.need_abi_warning = true;
993 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
994 && abi_version_at_least (2))
995 write_template_param (node);
996 else if (template_info != NULL)
997 /* Templated. */
999 write_template_prefix (decl);
1000 write_template_args (TI_ARGS (template_info));
1002 else
1003 /* Not templated. */
1005 write_prefix (CP_DECL_CONTEXT (decl));
1006 write_unqualified_name (decl);
1009 add_substitution (node);
1012 /* <template-prefix> ::= <prefix> <template component>
1013 ::= <template-param>
1014 ::= <substitution> */
1016 static void
1017 write_template_prefix (const tree node)
1019 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1020 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1021 tree context = CP_DECL_CONTEXT (decl);
1022 tree template_info;
1023 tree template;
1024 tree substitution;
1026 MANGLE_TRACE_TREE ("template-prefix", node);
1028 /* Find the template decl. */
1029 if (decl_is_template_id (decl, &template_info))
1030 template = TI_TEMPLATE (template_info);
1031 else
1033 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1035 template = TYPE_TI_TEMPLATE (type);
1038 /* For a member template, though, the template name for the
1039 innermost name must have all the outer template levels
1040 instantiated. For instance, consider
1042 template<typename T> struct Outer {
1043 template<typename U> struct Inner {};
1046 The template name for `Inner' in `Outer<int>::Inner<float>' is
1047 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1048 levels separately, so there's no TEMPLATE_DECL available for this
1049 (there's only `Outer<T>::Inner<U>').
1051 In order to get the substitutions right, we create a special
1052 TREE_LIST to represent the substitution candidate for a nested
1053 template. The TREE_PURPOSE is the template's context, fully
1054 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1055 template.
1057 So, for the example above, `Outer<int>::Inner' is represented as a
1058 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1059 and whose value is `Outer<T>::Inner<U>'. */
1060 if (TYPE_P (context))
1061 substitution = build_tree_list (context, template);
1062 else
1063 substitution = template;
1065 if (find_substitution (substitution))
1066 return;
1068 /* In G++ 3.2, the name of the template template parameter was used. */
1069 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1070 && !abi_version_at_least (2))
1071 G.need_abi_warning = true;
1073 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1074 && abi_version_at_least (2))
1075 write_template_param (TREE_TYPE (template));
1076 else
1078 write_prefix (context);
1079 write_unqualified_name (decl);
1082 add_substitution (substitution);
1085 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1086 mangled through special entry points.
1088 <unqualified-name> ::= <operator-name>
1089 ::= <special-name>
1090 ::= <source-name>
1091 ::= <local-source-name>
1093 <local-source-name> ::= L <source-name> <discriminator> */
1095 static void
1096 write_unqualified_name (const tree decl)
1098 MANGLE_TRACE_TREE ("unqualified-name", decl);
1100 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
1101 write_special_name_constructor (decl);
1102 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
1103 write_special_name_destructor (decl);
1104 else if (DECL_NAME (decl) == NULL_TREE)
1105 write_source_name (DECL_ASSEMBLER_NAME (decl));
1106 else if (DECL_CONV_FN_P (decl))
1108 /* Conversion operator. Handle it right here.
1109 <operator> ::= cv <type> */
1110 tree type;
1111 if (decl_is_template_id (decl, NULL))
1113 tree fn_type;
1114 save_partially_mangled_name ();
1115 fn_type = get_mostly_instantiated_function_type (decl);
1116 restore_partially_mangled_name ();
1117 type = TREE_TYPE (fn_type);
1119 else
1120 type = DECL_CONV_FN_TYPE (decl);
1121 write_conversion_operator_name (type);
1123 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1125 operator_name_info_t *oni;
1126 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1127 oni = assignment_operator_name_info;
1128 else
1129 oni = operator_name_info;
1131 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1133 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1134 && DECL_NAMESPACE_SCOPE_P (decl)
1135 && decl_linkage (decl) == lk_internal)
1137 MANGLE_TRACE_TREE ("local-source-name", decl);
1138 write_char ('L');
1139 write_source_name (DECL_NAME (decl));
1140 /* The default discriminator is 1, and that's all we ever use,
1141 so there's no code to output one here. */
1143 else
1144 write_source_name (DECL_NAME (decl));
1147 /* Write the unqualified-name for a conversion operator to TYPE. */
1149 static void
1150 write_conversion_operator_name (const tree type)
1152 write_string ("cv");
1153 write_type (type);
1156 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1158 <source-name> ::= </length/ number> <identifier> */
1160 static void
1161 write_source_name (tree identifier)
1163 MANGLE_TRACE_TREE ("source-name", identifier);
1165 /* Never write the whole template-id name including the template
1166 arguments; we only want the template name. */
1167 if (IDENTIFIER_TEMPLATE (identifier))
1168 identifier = IDENTIFIER_TEMPLATE (identifier);
1170 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1171 write_identifier (IDENTIFIER_POINTER (identifier));
1174 /* Convert NUMBER to ascii using base BASE and generating at least
1175 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1176 into which to store the characters. Returns the number of
1177 characters generated (these will be layed out in advance of where
1178 BUFFER points). */
1180 static int
1181 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1182 char *buffer, const unsigned int min_digits)
1184 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1185 unsigned digits = 0;
1187 while (number)
1189 unsigned HOST_WIDE_INT d = number / base;
1191 *--buffer = base_digits[number - d * base];
1192 digits++;
1193 number = d;
1195 while (digits < min_digits)
1197 *--buffer = base_digits[0];
1198 digits++;
1200 return digits;
1203 /* Non-terminal <number>.
1205 <number> ::= [n] </decimal integer/> */
1207 static void
1208 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1209 const unsigned int base)
1211 char buffer[sizeof (HOST_WIDE_INT) * 8];
1212 unsigned count = 0;
1214 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1216 write_char ('n');
1217 number = -((HOST_WIDE_INT) number);
1219 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1220 write_chars (buffer + sizeof (buffer) - count, count);
1223 /* Write out an integral CST in decimal. Most numbers are small, and
1224 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1225 bigger than that, which we must deal with. */
1227 static inline void
1228 write_integer_cst (const tree cst)
1230 int sign = tree_int_cst_sgn (cst);
1232 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1234 /* A bignum. We do this in chunks, each of which fits in a
1235 HOST_WIDE_INT. */
1236 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1237 unsigned HOST_WIDE_INT chunk;
1238 unsigned chunk_digits;
1239 char *ptr = buffer + sizeof (buffer);
1240 unsigned count = 0;
1241 tree n, base, type;
1242 int done;
1244 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1245 representable. */
1246 chunk = 1000000000;
1247 chunk_digits = 9;
1249 if (sizeof (HOST_WIDE_INT) >= 8)
1251 /* It is at least 64 bits, so 10^18 is representable. */
1252 chunk_digits = 18;
1253 chunk *= chunk;
1256 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1257 base = build_int_cstu (type, chunk);
1258 n = build_int_cst_wide (type,
1259 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1261 if (sign < 0)
1263 write_char ('n');
1264 n = fold_build1 (NEGATE_EXPR, type, n);
1268 tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
1269 tree tmp = fold_build2 (MULT_EXPR, type, d, base);
1270 unsigned c;
1272 done = integer_zerop (d);
1273 tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
1274 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1275 done ? 1 : chunk_digits);
1276 ptr -= c;
1277 count += c;
1278 n = d;
1280 while (!done);
1281 write_chars (ptr, count);
1283 else
1285 /* A small num. */
1286 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1288 if (sign < 0)
1290 write_char ('n');
1291 low = -low;
1293 write_unsigned_number (low);
1297 /* Write out a floating-point literal.
1299 "Floating-point literals are encoded using the bit pattern of the
1300 target processor's internal representation of that number, as a
1301 fixed-length lowercase hexadecimal string, high-order bytes first
1302 (even if the target processor would store low-order bytes first).
1303 The "n" prefix is not used for floating-point literals; the sign
1304 bit is encoded with the rest of the number.
1306 Here are some examples, assuming the IEEE standard representation
1307 for floating point numbers. (Spaces are for readability, not
1308 part of the encoding.)
1310 1.0f Lf 3f80 0000 E
1311 -1.0f Lf bf80 0000 E
1312 1.17549435e-38f Lf 0080 0000 E
1313 1.40129846e-45f Lf 0000 0001 E
1314 0.0f Lf 0000 0000 E"
1316 Caller is responsible for the Lx and the E. */
1317 static void
1318 write_real_cst (const tree value)
1320 if (abi_version_at_least (2))
1322 long target_real[4]; /* largest supported float */
1323 char buffer[9]; /* eight hex digits in a 32-bit number */
1324 int i, limit, dir;
1326 tree type = TREE_TYPE (value);
1327 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1329 real_to_target (target_real, &TREE_REAL_CST (value),
1330 TYPE_MODE (type));
1332 /* The value in target_real is in the target word order,
1333 so we must write it out backward if that happens to be
1334 little-endian. write_number cannot be used, it will
1335 produce uppercase. */
1336 if (FLOAT_WORDS_BIG_ENDIAN)
1337 i = 0, limit = words, dir = 1;
1338 else
1339 i = words - 1, limit = -1, dir = -1;
1341 for (; i != limit; i += dir)
1343 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1344 write_chars (buffer, 8);
1347 else
1349 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1350 literally. Note that compatibility with 3.2 is impossible,
1351 because the old floating-point emulator used a different
1352 format for REAL_VALUE_TYPE. */
1353 size_t i;
1354 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1355 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1356 /*unsigned_p*/ 1,
1357 /*base*/ 16);
1358 G.need_abi_warning = 1;
1362 /* Non-terminal <identifier>.
1364 <identifier> ::= </unqualified source code identifier> */
1366 static void
1367 write_identifier (const char *identifier)
1369 MANGLE_TRACE ("identifier", identifier);
1370 write_string (identifier);
1373 /* Handle constructor productions of non-terminal <special-name>.
1374 CTOR is a constructor FUNCTION_DECL.
1376 <special-name> ::= C1 # complete object constructor
1377 ::= C2 # base object constructor
1378 ::= C3 # complete object allocating constructor
1380 Currently, allocating constructors are never used.
1382 We also need to provide mangled names for the maybe-in-charge
1383 constructor, so we treat it here too. mangle_decl_string will
1384 append *INTERNAL* to that, to make sure we never emit it. */
1386 static void
1387 write_special_name_constructor (const tree ctor)
1389 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1390 write_string ("C2");
1391 else
1393 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1394 /* Even though we don't ever emit a definition of
1395 the old-style destructor, we still have to
1396 consider entities (like static variables) nested
1397 inside it. */
1398 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor));
1399 write_string ("C1");
1403 /* Handle destructor productions of non-terminal <special-name>.
1404 DTOR is a destructor FUNCTION_DECL.
1406 <special-name> ::= D0 # deleting (in-charge) destructor
1407 ::= D1 # complete object (in-charge) destructor
1408 ::= D2 # base object (not-in-charge) destructor
1410 We also need to provide mangled names for the maybe-incharge
1411 destructor, so we treat it here too. mangle_decl_string will
1412 append *INTERNAL* to that, to make sure we never emit it. */
1414 static void
1415 write_special_name_destructor (const tree dtor)
1417 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1418 write_string ("D0");
1419 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1420 write_string ("D2");
1421 else
1423 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1424 /* Even though we don't ever emit a definition of
1425 the old-style destructor, we still have to
1426 consider entities (like static variables) nested
1427 inside it. */
1428 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1429 write_string ("D1");
1433 /* Return the discriminator for ENTITY appearing inside
1434 FUNCTION. The discriminator is the lexical ordinal of VAR among
1435 entities with the same name in the same FUNCTION. */
1437 static int
1438 discriminator_for_local_entity (tree entity)
1440 /* Assume this is the only local entity with this name. */
1441 int discriminator = 0;
1443 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1444 discriminator = DECL_DISCRIMINATOR (entity);
1445 else if (TREE_CODE (entity) == TYPE_DECL)
1447 int ix;
1449 /* Scan the list of local classes. */
1450 entity = TREE_TYPE (entity);
1451 for (ix = 0; ; ix++)
1453 tree type = VEC_index (tree, local_classes, ix);
1454 if (type == entity)
1455 break;
1456 if (TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (entity)
1457 && TYPE_CONTEXT (type) == TYPE_CONTEXT (entity))
1458 ++discriminator;
1462 return discriminator;
1465 /* Return the discriminator for STRING, a string literal used inside
1466 FUNCTION. The discriminator is the lexical ordinal of STRING among
1467 string literals used in FUNCTION. */
1469 static int
1470 discriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
1471 tree string ATTRIBUTE_UNUSED)
1473 /* For now, we don't discriminate amongst string literals. */
1474 return 0;
1477 /* <discriminator> := _ <number>
1479 The discriminator is used only for the second and later occurrences
1480 of the same name within a single function. In this case <number> is
1481 n - 2, if this is the nth occurrence, in lexical order. */
1483 static void
1484 write_discriminator (const int discriminator)
1486 /* If discriminator is zero, don't write anything. Otherwise... */
1487 if (discriminator > 0)
1489 write_char ('_');
1490 write_unsigned_number (discriminator - 1);
1494 /* Mangle the name of a function-scope entity. FUNCTION is the
1495 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1496 the entity itself. LOCAL_ENTITY is the entity that's directly
1497 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1498 of ENTITY.
1500 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1501 := Z <function encoding> E s [<discriminator>] */
1503 static void
1504 write_local_name (const tree function, const tree local_entity,
1505 const tree entity)
1507 MANGLE_TRACE_TREE ("local-name", entity);
1509 write_char ('Z');
1510 write_encoding (function);
1511 write_char ('E');
1512 if (TREE_CODE (entity) == STRING_CST)
1514 write_char ('s');
1515 write_discriminator (discriminator_for_string_literal (function,
1516 entity));
1518 else
1520 /* Now the <entity name>. Let write_name know its being called
1521 from <local-name>, so it doesn't try to process the enclosing
1522 function scope again. */
1523 write_name (entity, /*ignore_local_scope=*/1);
1524 write_discriminator (discriminator_for_local_entity (local_entity));
1528 /* Non-terminals <type> and <CV-qualifier>.
1530 <type> ::= <builtin-type>
1531 ::= <function-type>
1532 ::= <class-enum-type>
1533 ::= <array-type>
1534 ::= <pointer-to-member-type>
1535 ::= <template-param>
1536 ::= <substitution>
1537 ::= <CV-qualifier>
1538 ::= P <type> # pointer-to
1539 ::= R <type> # reference-to
1540 ::= C <type> # complex pair (C 2000)
1541 ::= G <type> # imaginary (C 2000) [not supported]
1542 ::= U <source-name> <type> # vendor extended type qualifier
1544 C++0x extensions
1546 <type> ::= RR <type> # rvalue reference-to
1548 TYPE is a type node. */
1550 static void
1551 write_type (tree type)
1553 /* This gets set to nonzero if TYPE turns out to be a (possibly
1554 CV-qualified) builtin type. */
1555 int is_builtin_type = 0;
1557 MANGLE_TRACE_TREE ("type", type);
1559 if (type == error_mark_node)
1560 return;
1562 if (find_substitution (type))
1563 return;
1565 if (write_CV_qualifiers_for_type (type) > 0)
1566 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1567 mangle the unqualified type. The recursive call is needed here
1568 since both the qualified and unqualified types are substitution
1569 candidates. */
1570 write_type (TYPE_MAIN_VARIANT (type));
1571 else if (TREE_CODE (type) == ARRAY_TYPE)
1572 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1573 so that the cv-qualification of the element type is available
1574 in write_array_type. */
1575 write_array_type (type);
1576 else
1578 tree type_orig = type;
1580 /* See through any typedefs. */
1581 type = TYPE_MAIN_VARIANT (type);
1583 if (TYPE_PTRMEM_P (type))
1584 write_pointer_to_member_type (type);
1585 else
1587 /* Handle any target-specific fundamental types. */
1588 const char *target_mangling
1589 = targetm.mangle_type (type_orig);
1591 if (target_mangling)
1593 write_string (target_mangling);
1594 return;
1597 switch (TREE_CODE (type))
1599 case VOID_TYPE:
1600 case BOOLEAN_TYPE:
1601 case INTEGER_TYPE: /* Includes wchar_t. */
1602 case REAL_TYPE:
1604 /* If this is a typedef, TYPE may not be one of
1605 the standard builtin type nodes, but an alias of one. Use
1606 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1607 write_builtin_type (TYPE_MAIN_VARIANT (type));
1608 ++is_builtin_type;
1610 break;
1612 case COMPLEX_TYPE:
1613 write_char ('C');
1614 write_type (TREE_TYPE (type));
1615 break;
1617 case FUNCTION_TYPE:
1618 case METHOD_TYPE:
1619 write_function_type (type);
1620 break;
1622 case UNION_TYPE:
1623 case RECORD_TYPE:
1624 case ENUMERAL_TYPE:
1625 /* A pointer-to-member function is represented as a special
1626 RECORD_TYPE, so check for this first. */
1627 if (TYPE_PTRMEMFUNC_P (type))
1628 write_pointer_to_member_type (type);
1629 else
1630 write_class_enum_type (type);
1631 break;
1633 case TYPENAME_TYPE:
1634 case UNBOUND_CLASS_TEMPLATE:
1635 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1636 ordinary nested names. */
1637 write_nested_name (TYPE_STUB_DECL (type));
1638 break;
1640 case POINTER_TYPE:
1641 write_char ('P');
1642 write_type (TREE_TYPE (type));
1643 break;
1645 case REFERENCE_TYPE:
1646 if (TYPE_REF_IS_RVALUE (type))
1647 write_char('R');
1648 write_char ('R');
1649 write_type (TREE_TYPE (type));
1650 break;
1652 case TEMPLATE_TYPE_PARM:
1653 case TEMPLATE_PARM_INDEX:
1654 write_template_param (type);
1655 break;
1657 case TEMPLATE_TEMPLATE_PARM:
1658 write_template_template_param (type);
1659 break;
1661 case BOUND_TEMPLATE_TEMPLATE_PARM:
1662 write_template_template_param (type);
1663 write_template_args
1664 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1665 break;
1667 case VECTOR_TYPE:
1668 write_string ("U8__vector");
1669 write_type (TREE_TYPE (type));
1670 break;
1672 case TYPE_PACK_EXPANSION:
1673 write_string ("U10__variadic");
1674 write_type (PACK_EXPANSION_PATTERN (type));
1675 break;
1677 default:
1678 gcc_unreachable ();
1683 /* Types other than builtin types are substitution candidates. */
1684 if (!is_builtin_type)
1685 add_substitution (type);
1688 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1689 CV-qualifiers written for TYPE.
1691 <CV-qualifiers> ::= [r] [V] [K] */
1693 static int
1694 write_CV_qualifiers_for_type (const tree type)
1696 int num_qualifiers = 0;
1698 /* The order is specified by:
1700 "In cases where multiple order-insensitive qualifiers are
1701 present, they should be ordered 'K' (closest to the base type),
1702 'V', 'r', and 'U' (farthest from the base type) ..."
1704 Note that we do not use cp_type_quals below; given "const
1705 int[3]", the "const" is emitted with the "int", not with the
1706 array. */
1708 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1710 write_char ('r');
1711 ++num_qualifiers;
1713 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1715 write_char ('V');
1716 ++num_qualifiers;
1718 if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1720 write_char ('K');
1721 ++num_qualifiers;
1724 return num_qualifiers;
1727 /* Non-terminal <builtin-type>.
1729 <builtin-type> ::= v # void
1730 ::= b # bool
1731 ::= w # wchar_t
1732 ::= c # char
1733 ::= a # signed char
1734 ::= h # unsigned char
1735 ::= s # short
1736 ::= t # unsigned short
1737 ::= i # int
1738 ::= j # unsigned int
1739 ::= l # long
1740 ::= m # unsigned long
1741 ::= x # long long, __int64
1742 ::= y # unsigned long long, __int64
1743 ::= n # __int128
1744 ::= o # unsigned __int128
1745 ::= f # float
1746 ::= d # double
1747 ::= e # long double, __float80
1748 ::= g # __float128 [not supported]
1749 ::= u <source-name> # vendor extended type */
1751 static void
1752 write_builtin_type (tree type)
1754 switch (TREE_CODE (type))
1756 case VOID_TYPE:
1757 write_char ('v');
1758 break;
1760 case BOOLEAN_TYPE:
1761 write_char ('b');
1762 break;
1764 case INTEGER_TYPE:
1765 /* TYPE may still be wchar_t, since that isn't in
1766 integer_type_nodes. */
1767 if (type == wchar_type_node)
1768 write_char ('w');
1769 else if (TYPE_FOR_JAVA (type))
1770 write_java_integer_type_codes (type);
1771 else
1773 size_t itk;
1774 /* Assume TYPE is one of the shared integer type nodes. Find
1775 it in the array of these nodes. */
1776 iagain:
1777 for (itk = 0; itk < itk_none; ++itk)
1778 if (type == integer_types[itk])
1780 /* Print the corresponding single-letter code. */
1781 write_char (integer_type_codes[itk]);
1782 break;
1785 if (itk == itk_none)
1787 tree t = c_common_type_for_mode (TYPE_MODE (type),
1788 TYPE_UNSIGNED (type));
1789 if (type != t)
1791 type = t;
1792 goto iagain;
1795 if (TYPE_PRECISION (type) == 128)
1796 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
1797 else
1799 /* Allow for cases where TYPE is not one of the shared
1800 integer type nodes and write a "vendor extended builtin
1801 type" with a name the form intN or uintN, respectively.
1802 Situations like this can happen if you have an
1803 __attribute__((__mode__(__SI__))) type and use exotic
1804 switches like '-mint8' on AVR. Of course, this is
1805 undefined by the C++ ABI (and '-mint8' is not even
1806 Standard C conforming), but when using such special
1807 options you're pretty much in nowhere land anyway. */
1808 const char *prefix;
1809 char prec[11]; /* up to ten digits for an unsigned */
1811 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
1812 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
1813 write_char ('u'); /* "vendor extended builtin type" */
1814 write_unsigned_number (strlen (prefix) + strlen (prec));
1815 write_string (prefix);
1816 write_string (prec);
1820 break;
1822 case REAL_TYPE:
1823 if (type == float_type_node
1824 || type == java_float_type_node)
1825 write_char ('f');
1826 else if (type == double_type_node
1827 || type == java_double_type_node)
1828 write_char ('d');
1829 else if (type == long_double_type_node)
1830 write_char ('e');
1831 else
1832 gcc_unreachable ();
1833 break;
1835 default:
1836 gcc_unreachable ();
1840 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1841 METHOD_TYPE. The return type is mangled before the parameter
1842 types.
1844 <function-type> ::= F [Y] <bare-function-type> E */
1846 static void
1847 write_function_type (const tree type)
1849 MANGLE_TRACE_TREE ("function-type", type);
1851 /* For a pointer to member function, the function type may have
1852 cv-qualifiers, indicating the quals for the artificial 'this'
1853 parameter. */
1854 if (TREE_CODE (type) == METHOD_TYPE)
1856 /* The first parameter must be a POINTER_TYPE pointing to the
1857 `this' parameter. */
1858 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1859 write_CV_qualifiers_for_type (this_type);
1862 write_char ('F');
1863 /* We don't track whether or not a type is `extern "C"'. Note that
1864 you can have an `extern "C"' function that does not have
1865 `extern "C"' type, and vice versa:
1867 extern "C" typedef void function_t();
1868 function_t f; // f has C++ linkage, but its type is
1869 // `extern "C"'
1871 typedef void function_t();
1872 extern "C" function_t f; // Vice versa.
1874 See [dcl.link]. */
1875 write_bare_function_type (type, /*include_return_type_p=*/1,
1876 /*decl=*/NULL);
1877 write_char ('E');
1880 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1881 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
1882 is mangled before the parameter types. If non-NULL, DECL is
1883 FUNCTION_DECL for the function whose type is being emitted.
1885 If DECL is a member of a Java type, then a literal 'J'
1886 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
1887 were nonzero.
1889 <bare-function-type> ::= [J]</signature/ type>+ */
1891 static void
1892 write_bare_function_type (const tree type, const int include_return_type_p,
1893 const tree decl)
1895 int java_method_p;
1897 MANGLE_TRACE_TREE ("bare-function-type", type);
1899 /* Detect Java methods and emit special encoding. */
1900 if (decl != NULL
1901 && DECL_FUNCTION_MEMBER_P (decl)
1902 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
1903 && !DECL_CONSTRUCTOR_P (decl)
1904 && !DECL_DESTRUCTOR_P (decl)
1905 && !DECL_CONV_FN_P (decl))
1907 java_method_p = 1;
1908 write_char ('J');
1910 else
1912 java_method_p = 0;
1915 /* Mangle the return type, if requested. */
1916 if (include_return_type_p || java_method_p)
1917 write_type (TREE_TYPE (type));
1919 /* Now mangle the types of the arguments. */
1920 write_method_parms (TYPE_ARG_TYPES (type),
1921 TREE_CODE (type) == METHOD_TYPE,
1922 decl);
1925 /* Write the mangled representation of a method parameter list of
1926 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
1927 considered a non-static method, and the this parameter is omitted.
1928 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1929 parameters are being emitted. */
1931 static void
1932 write_method_parms (tree parm_types, const int method_p, const tree decl)
1934 tree first_parm_type;
1935 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1937 /* Assume this parameter type list is variable-length. If it ends
1938 with a void type, then it's not. */
1939 int varargs_p = 1;
1941 /* If this is a member function, skip the first arg, which is the
1942 this pointer.
1943 "Member functions do not encode the type of their implicit this
1944 parameter."
1946 Similarly, there's no need to mangle artificial parameters, like
1947 the VTT parameters for constructors and destructors. */
1948 if (method_p)
1950 parm_types = TREE_CHAIN (parm_types);
1951 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1953 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1955 parm_types = TREE_CHAIN (parm_types);
1956 parm_decl = TREE_CHAIN (parm_decl);
1960 for (first_parm_type = parm_types;
1961 parm_types;
1962 parm_types = TREE_CHAIN (parm_types))
1964 tree parm = TREE_VALUE (parm_types);
1965 if (parm == void_type_node)
1967 /* "Empty parameter lists, whether declared as () or
1968 conventionally as (void), are encoded with a void parameter
1969 (v)." */
1970 if (parm_types == first_parm_type)
1971 write_type (parm);
1972 /* If the parm list is terminated with a void type, it's
1973 fixed-length. */
1974 varargs_p = 0;
1975 /* A void type better be the last one. */
1976 gcc_assert (TREE_CHAIN (parm_types) == NULL);
1978 else
1979 write_type (parm);
1982 if (varargs_p)
1983 /* <builtin-type> ::= z # ellipsis */
1984 write_char ('z');
1987 /* <class-enum-type> ::= <name> */
1989 static void
1990 write_class_enum_type (const tree type)
1992 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1995 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1996 arguments.
1998 <template-args> ::= I <template-arg>+ E */
2000 static void
2001 write_template_args (tree args)
2003 int i;
2004 int length = TREE_VEC_LENGTH (args);
2006 MANGLE_TRACE_TREE ("template-args", args);
2008 write_char ('I');
2010 gcc_assert (length > 0);
2012 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2014 /* We have nested template args. We want the innermost template
2015 argument list. */
2016 args = TREE_VEC_ELT (args, length - 1);
2017 length = TREE_VEC_LENGTH (args);
2019 for (i = 0; i < length; ++i)
2020 write_template_arg (TREE_VEC_ELT (args, i));
2022 write_char ('E');
2025 /* <expression> ::= <unary operator-name> <expression>
2026 ::= <binary operator-name> <expression> <expression>
2027 ::= <expr-primary>
2029 <expr-primary> ::= <template-param>
2030 ::= L <type> <value number> E # literal
2031 ::= L <mangled-name> E # external name
2032 ::= sr <type> <unqualified-name>
2033 ::= sr <type> <unqualified-name> <template-args> */
2035 static void
2036 write_expression (tree expr)
2038 enum tree_code code;
2040 code = TREE_CODE (expr);
2042 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2043 is converted (via qualification conversions) to another
2044 type. */
2045 while (TREE_CODE (expr) == NOP_EXPR
2046 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2048 expr = TREE_OPERAND (expr, 0);
2049 code = TREE_CODE (expr);
2052 if (code == BASELINK)
2054 expr = BASELINK_FUNCTIONS (expr);
2055 code = TREE_CODE (expr);
2058 /* Handle pointers-to-members by making them look like expression
2059 nodes. */
2060 if (code == PTRMEM_CST)
2062 expr = build_nt (ADDR_EXPR,
2063 build_qualified_name (/*type=*/NULL_TREE,
2064 PTRMEM_CST_CLASS (expr),
2065 PTRMEM_CST_MEMBER (expr),
2066 /*template_p=*/false));
2067 code = TREE_CODE (expr);
2070 /* Handle template parameters. */
2071 if (code == TEMPLATE_TYPE_PARM
2072 || code == TEMPLATE_TEMPLATE_PARM
2073 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2074 || code == TEMPLATE_PARM_INDEX)
2075 write_template_param (expr);
2076 /* Handle literals. */
2077 else if (TREE_CODE_CLASS (code) == tcc_constant
2078 || (abi_version_at_least (2) && code == CONST_DECL))
2079 write_template_arg_literal (expr);
2080 else if (DECL_P (expr))
2082 /* G++ 3.2 incorrectly mangled non-type template arguments of
2083 enumeration type using their names. */
2084 if (code == CONST_DECL)
2085 G.need_abi_warning = 1;
2086 write_char ('L');
2087 write_mangled_name (expr, false);
2088 write_char ('E');
2090 else if (TREE_CODE (expr) == SIZEOF_EXPR
2091 && TYPE_P (TREE_OPERAND (expr, 0)))
2093 write_string ("st");
2094 write_type (TREE_OPERAND (expr, 0));
2096 else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
2098 tree scope = TREE_OPERAND (expr, 0);
2099 tree member = TREE_OPERAND (expr, 1);
2101 /* If the MEMBER is a real declaration, then the qualifying
2102 scope was not dependent. Ideally, we would not have a
2103 SCOPE_REF in those cases, but sometimes we do. If the second
2104 argument is a DECL, then the name must not have been
2105 dependent. */
2106 if (DECL_P (member))
2107 write_expression (member);
2108 else
2110 tree template_args;
2112 write_string ("sr");
2113 write_type (scope);
2114 /* If MEMBER is a template-id, separate the template
2115 from the arguments. */
2116 if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2118 template_args = TREE_OPERAND (member, 1);
2119 member = TREE_OPERAND (member, 0);
2121 else
2122 template_args = NULL_TREE;
2123 /* Write out the name of the MEMBER. */
2124 if (IDENTIFIER_TYPENAME_P (member))
2125 write_conversion_operator_name (TREE_TYPE (member));
2126 else if (IDENTIFIER_OPNAME_P (member))
2128 int i;
2129 const char *mangled_name = NULL;
2131 /* Unfortunately, there is no easy way to go from the
2132 name of the operator back to the corresponding tree
2133 code. */
2134 for (i = 0; i < LAST_CPLUS_TREE_CODE; ++i)
2135 if (operator_name_info[i].identifier == member)
2137 /* The ABI says that we prefer binary operator
2138 names to unary operator names. */
2139 if (operator_name_info[i].arity == 2)
2141 mangled_name = operator_name_info[i].mangled_name;
2142 break;
2144 else if (!mangled_name)
2145 mangled_name = operator_name_info[i].mangled_name;
2147 else if (assignment_operator_name_info[i].identifier
2148 == member)
2150 mangled_name
2151 = assignment_operator_name_info[i].mangled_name;
2152 break;
2154 write_string (mangled_name);
2156 else
2157 write_source_name (member);
2158 /* Write out the template arguments. */
2159 if (template_args)
2160 write_template_args (template_args);
2163 else
2165 int i;
2167 /* When we bind a variable or function to a non-type template
2168 argument with reference type, we create an ADDR_EXPR to show
2169 the fact that the entity's address has been taken. But, we
2170 don't actually want to output a mangling code for the `&'. */
2171 if (TREE_CODE (expr) == ADDR_EXPR
2172 && TREE_TYPE (expr)
2173 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2175 expr = TREE_OPERAND (expr, 0);
2176 if (DECL_P (expr))
2178 write_expression (expr);
2179 return;
2182 code = TREE_CODE (expr);
2185 /* If it wasn't any of those, recursively expand the expression. */
2186 write_string (operator_name_info[(int) code].mangled_name);
2188 switch (code)
2190 case CALL_EXPR:
2191 sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
2192 break;
2194 case CAST_EXPR:
2195 write_type (TREE_TYPE (expr));
2196 /* There is no way to mangle a zero-operand cast like
2197 "T()". */
2198 if (!TREE_OPERAND (expr, 0))
2199 sorry ("zero-operand casts cannot be mangled due to a defect "
2200 "in the C++ ABI");
2201 else
2202 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2203 break;
2205 case STATIC_CAST_EXPR:
2206 case CONST_CAST_EXPR:
2207 write_type (TREE_TYPE (expr));
2208 write_expression (TREE_OPERAND (expr, 0));
2209 break;
2212 /* Handle pointers-to-members specially. */
2213 case SCOPE_REF:
2214 write_type (TREE_OPERAND (expr, 0));
2215 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
2216 write_source_name (TREE_OPERAND (expr, 1));
2217 else if (TREE_CODE (TREE_OPERAND (expr, 1)) == TEMPLATE_ID_EXPR)
2219 tree template_id;
2220 tree name;
2222 template_id = TREE_OPERAND (expr, 1);
2223 name = TREE_OPERAND (template_id, 0);
2224 /* FIXME: What about operators? */
2225 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2226 write_source_name (TREE_OPERAND (template_id, 0));
2227 write_template_args (TREE_OPERAND (template_id, 1));
2229 else
2231 /* G++ 3.2 incorrectly put out both the "sr" code and
2232 the nested name of the qualified name. */
2233 G.need_abi_warning = 1;
2234 write_encoding (TREE_OPERAND (expr, 1));
2236 break;
2238 default:
2239 for (i = 0; i < TREE_OPERAND_LENGTH (expr); ++i)
2241 tree operand = TREE_OPERAND (expr, i);
2242 /* As a GNU extension, the middle operand of a
2243 conditional may be omitted. Since expression
2244 manglings are supposed to represent the input token
2245 stream, there's no good way to mangle such an
2246 expression without extending the C++ ABI. */
2247 if (code == COND_EXPR && i == 1 && !operand)
2249 error ("omitted middle operand to %<?:%> operand "
2250 "cannot be mangled");
2251 continue;
2253 write_expression (operand);
2259 /* Literal subcase of non-terminal <template-arg>.
2261 "Literal arguments, e.g. "A<42L>", are encoded with their type
2262 and value. Negative integer values are preceded with "n"; for
2263 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2264 encoded as 0, true as 1." */
2266 static void
2267 write_template_arg_literal (const tree value)
2269 write_char ('L');
2270 write_type (TREE_TYPE (value));
2272 switch (TREE_CODE (value))
2274 case CONST_DECL:
2275 write_integer_cst (DECL_INITIAL (value));
2276 break;
2278 case INTEGER_CST:
2279 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
2280 || integer_zerop (value) || integer_onep (value));
2281 write_integer_cst (value);
2282 break;
2284 case REAL_CST:
2285 write_real_cst (value);
2286 break;
2288 default:
2289 gcc_unreachable ();
2292 write_char ('E');
2295 /* Non-terminal <template-arg>.
2297 <template-arg> ::= <type> # type
2298 ::= L <type> </value/ number> E # literal
2299 ::= LZ <name> E # external name
2300 ::= X <expression> E # expression */
2302 static void
2303 write_template_arg (tree node)
2305 enum tree_code code = TREE_CODE (node);
2307 MANGLE_TRACE_TREE ("template-arg", node);
2309 /* A template template parameter's argument list contains TREE_LIST
2310 nodes of which the value field is the actual argument. */
2311 if (code == TREE_LIST)
2313 node = TREE_VALUE (node);
2314 /* If it's a decl, deal with its type instead. */
2315 if (DECL_P (node))
2317 node = TREE_TYPE (node);
2318 code = TREE_CODE (node);
2322 if (TREE_CODE (node) == NOP_EXPR
2323 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
2325 /* Template parameters can be of reference type. To maintain
2326 internal consistency, such arguments use a conversion from
2327 address of object to reference type. */
2328 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
2329 if (abi_version_at_least (2))
2330 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
2331 else
2332 G.need_abi_warning = 1;
2335 if (ARGUMENT_PACK_P (node))
2337 /* Expand the template argument pack. */
2338 tree args = ARGUMENT_PACK_ARGS (node);
2339 int i, length = TREE_VEC_LENGTH (args);
2340 for (i = 0; i < length; ++i)
2341 write_template_arg (TREE_VEC_ELT (args, i));
2343 else if (TYPE_P (node))
2344 write_type (node);
2345 else if (code == TEMPLATE_DECL)
2346 /* A template appearing as a template arg is a template template arg. */
2347 write_template_template_arg (node);
2348 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
2349 || (abi_version_at_least (2) && code == CONST_DECL))
2350 write_template_arg_literal (node);
2351 else if (DECL_P (node))
2353 /* Until ABI version 2, non-type template arguments of
2354 enumeration type were mangled using their names. */
2355 if (code == CONST_DECL && !abi_version_at_least (2))
2356 G.need_abi_warning = 1;
2357 write_char ('L');
2358 /* Until ABI version 3, the underscore before the mangled name
2359 was incorrectly omitted. */
2360 if (!abi_version_at_least (3))
2362 G.need_abi_warning = 1;
2363 write_char ('Z');
2365 else
2366 write_string ("_Z");
2367 write_encoding (node);
2368 write_char ('E');
2370 else
2372 /* Template arguments may be expressions. */
2373 write_char ('X');
2374 write_expression (node);
2375 write_char ('E');
2379 /* <template-template-arg>
2380 ::= <name>
2381 ::= <substitution> */
2383 static void
2384 write_template_template_arg (const tree decl)
2386 MANGLE_TRACE_TREE ("template-template-arg", decl);
2388 if (find_substitution (decl))
2389 return;
2390 write_name (decl, /*ignore_local_scope=*/0);
2391 add_substitution (decl);
2395 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
2397 <array-type> ::= A [</dimension/ number>] _ </element/ type>
2398 ::= A <expression> _ </element/ type>
2400 "Array types encode the dimension (number of elements) and the
2401 element type. For variable length arrays, the dimension (but not
2402 the '_' separator) is omitted." */
2404 static void
2405 write_array_type (const tree type)
2407 write_char ('A');
2408 if (TYPE_DOMAIN (type))
2410 tree index_type;
2411 tree max;
2413 index_type = TYPE_DOMAIN (type);
2414 /* The INDEX_TYPE gives the upper and lower bounds of the
2415 array. */
2416 max = TYPE_MAX_VALUE (index_type);
2417 if (TREE_CODE (max) == INTEGER_CST)
2419 /* The ABI specifies that we should mangle the number of
2420 elements in the array, not the largest allowed index. */
2421 max = size_binop (PLUS_EXPR, max, size_one_node);
2422 write_unsigned_number (tree_low_cst (max, 1));
2424 else
2426 max = TREE_OPERAND (max, 0);
2427 if (!abi_version_at_least (2))
2429 /* value_dependent_expression_p presumes nothing is
2430 dependent when PROCESSING_TEMPLATE_DECL is zero. */
2431 ++processing_template_decl;
2432 if (!value_dependent_expression_p (max))
2433 G.need_abi_warning = 1;
2434 --processing_template_decl;
2436 write_expression (max);
2440 write_char ('_');
2441 write_type (TREE_TYPE (type));
2444 /* Non-terminal <pointer-to-member-type> for pointer-to-member
2445 variables. TYPE is a pointer-to-member POINTER_TYPE.
2447 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2449 static void
2450 write_pointer_to_member_type (const tree type)
2452 write_char ('M');
2453 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
2454 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2457 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
2458 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2459 TEMPLATE_PARM_INDEX.
2461 <template-param> ::= T </parameter/ number> _ */
2463 static void
2464 write_template_param (const tree parm)
2466 int parm_index;
2467 int parm_level;
2468 tree parm_type = NULL_TREE;
2470 MANGLE_TRACE_TREE ("template-parm", parm);
2472 switch (TREE_CODE (parm))
2474 case TEMPLATE_TYPE_PARM:
2475 case TEMPLATE_TEMPLATE_PARM:
2476 case BOUND_TEMPLATE_TEMPLATE_PARM:
2477 parm_index = TEMPLATE_TYPE_IDX (parm);
2478 parm_level = TEMPLATE_TYPE_LEVEL (parm);
2479 break;
2481 case TEMPLATE_PARM_INDEX:
2482 parm_index = TEMPLATE_PARM_IDX (parm);
2483 parm_level = TEMPLATE_PARM_LEVEL (parm);
2484 parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
2485 break;
2487 default:
2488 gcc_unreachable ();
2491 write_char ('T');
2492 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2493 earliest template param denoted by `_'. */
2494 if (parm_index > 0)
2495 write_unsigned_number (parm_index - 1);
2496 write_char ('_');
2499 /* <template-template-param>
2500 ::= <template-param>
2501 ::= <substitution> */
2503 static void
2504 write_template_template_param (const tree parm)
2506 tree template = NULL_TREE;
2508 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2509 template template parameter. The substitution candidate here is
2510 only the template. */
2511 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2513 template
2514 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2515 if (find_substitution (template))
2516 return;
2519 /* <template-param> encodes only the template parameter position,
2520 not its template arguments, which is fine here. */
2521 write_template_param (parm);
2522 if (template)
2523 add_substitution (template);
2526 /* Non-terminal <substitution>.
2528 <substitution> ::= S <seq-id> _
2529 ::= S_ */
2531 static void
2532 write_substitution (const int seq_id)
2534 MANGLE_TRACE ("substitution", "");
2536 write_char ('S');
2537 if (seq_id > 0)
2538 write_number (seq_id - 1, /*unsigned=*/1, 36);
2539 write_char ('_');
2542 /* Start mangling ENTITY. */
2544 static inline void
2545 start_mangling (const tree entity, const bool ident_p)
2547 G.entity = entity;
2548 G.need_abi_warning = false;
2549 if (!ident_p)
2551 obstack_free (&name_obstack, name_base);
2552 mangle_obstack = &name_obstack;
2553 name_base = obstack_alloc (&name_obstack, 0);
2555 else
2556 mangle_obstack = &ident_hash->stack;
2559 /* Done with mangling. Return the generated mangled name. If WARN is
2560 true, and the name of G.entity will be mangled differently in a
2561 future version of the ABI, issue a warning. */
2563 static inline const char *
2564 finish_mangling (const bool warn)
2566 if (warn_abi && warn && G.need_abi_warning)
2567 warning (OPT_Wabi, "the mangled name of %qD will change in a future "
2568 "version of GCC",
2569 G.entity);
2571 /* Clear all the substitutions. */
2572 VEC_truncate (tree, G.substitutions, 0);
2574 /* Null-terminate the string. */
2575 write_char ('\0');
2577 return (const char *) obstack_finish (mangle_obstack);
2580 /* Initialize data structures for mangling. */
2582 void
2583 init_mangle (void)
2585 gcc_obstack_init (&name_obstack);
2586 name_base = obstack_alloc (&name_obstack, 0);
2587 G.substitutions = NULL;
2589 /* Cache these identifiers for quick comparison when checking for
2590 standard substitutions. */
2591 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2592 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2593 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2594 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2595 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2596 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2599 /* Generate the mangled name of DECL. */
2601 static const char *
2602 mangle_decl_string (const tree decl)
2604 const char *result;
2606 start_mangling (decl, /*ident_p=*/true);
2608 if (TREE_CODE (decl) == TYPE_DECL)
2609 write_type (TREE_TYPE (decl));
2610 else
2611 write_mangled_name (decl, true);
2613 result = finish_mangling (/*warn=*/true);
2614 if (DEBUG_MANGLE)
2615 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2616 return result;
2619 /* Like get_identifier, except that NAME is assumed to have been
2620 allocated on the obstack used by the identifier hash table. */
2622 static inline tree
2623 get_identifier_nocopy (const char *name)
2625 hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name,
2626 strlen (name), HT_ALLOCED);
2627 return HT_IDENT_TO_GCC_IDENT (ht_node);
2630 /* Create an identifier for the external mangled name of DECL. */
2632 void
2633 mangle_decl (const tree decl)
2635 tree id = get_identifier_nocopy (mangle_decl_string (decl));
2636 id = targetm.mangle_decl_assembler_name (decl, id);
2637 SET_DECL_ASSEMBLER_NAME (decl, id);
2640 /* Generate the mangled representation of TYPE. */
2642 const char *
2643 mangle_type_string (const tree type)
2645 const char *result;
2647 start_mangling (type, /*ident_p=*/false);
2648 write_type (type);
2649 result = finish_mangling (/*warn=*/false);
2650 if (DEBUG_MANGLE)
2651 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2652 return result;
2655 /* Create an identifier for the mangled name of a special component
2656 for belonging to TYPE. CODE is the ABI-specified code for this
2657 component. */
2659 static tree
2660 mangle_special_for_type (const tree type, const char *code)
2662 const char *result;
2664 /* We don't have an actual decl here for the special component, so
2665 we can't just process the <encoded-name>. Instead, fake it. */
2666 start_mangling (type, /*ident_p=*/true);
2668 /* Start the mangling. */
2669 write_string ("_Z");
2670 write_string (code);
2672 /* Add the type. */
2673 write_type (type);
2674 result = finish_mangling (/*warn=*/false);
2676 if (DEBUG_MANGLE)
2677 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2679 return get_identifier_nocopy (result);
2682 /* Create an identifier for the mangled representation of the typeinfo
2683 structure for TYPE. */
2685 tree
2686 mangle_typeinfo_for_type (const tree type)
2688 return mangle_special_for_type (type, "TI");
2691 /* Create an identifier for the mangled name of the NTBS containing
2692 the mangled name of TYPE. */
2694 tree
2695 mangle_typeinfo_string_for_type (const tree type)
2697 return mangle_special_for_type (type, "TS");
2700 /* Create an identifier for the mangled name of the vtable for TYPE. */
2702 tree
2703 mangle_vtbl_for_type (const tree type)
2705 return mangle_special_for_type (type, "TV");
2708 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2710 tree
2711 mangle_vtt_for_type (const tree type)
2713 return mangle_special_for_type (type, "TT");
2716 /* Return an identifier for a construction vtable group. TYPE is
2717 the most derived class in the hierarchy; BINFO is the base
2718 subobject for which this construction vtable group will be used.
2720 This mangling isn't part of the ABI specification; in the ABI
2721 specification, the vtable group is dumped in the same COMDAT as the
2722 main vtable, and is referenced only from that vtable, so it doesn't
2723 need an external name. For binary formats without COMDAT sections,
2724 though, we need external names for the vtable groups.
2726 We use the production
2728 <special-name> ::= CT <type> <offset number> _ <base type> */
2730 tree
2731 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
2733 const char *result;
2735 start_mangling (type, /*ident_p=*/true);
2737 write_string ("_Z");
2738 write_string ("TC");
2739 write_type (type);
2740 write_integer_cst (BINFO_OFFSET (binfo));
2741 write_char ('_');
2742 write_type (BINFO_TYPE (binfo));
2744 result = finish_mangling (/*warn=*/false);
2745 if (DEBUG_MANGLE)
2746 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2747 return get_identifier_nocopy (result);
2750 /* Mangle a this pointer or result pointer adjustment.
2752 <call-offset> ::= h <fixed offset number> _
2753 ::= v <fixed offset number> _ <virtual offset number> _ */
2755 static void
2756 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
2758 write_char (virtual_offset ? 'v' : 'h');
2760 /* For either flavor, write the fixed offset. */
2761 write_integer_cst (fixed_offset);
2762 write_char ('_');
2764 /* For a virtual thunk, add the virtual offset. */
2765 if (virtual_offset)
2767 write_integer_cst (virtual_offset);
2768 write_char ('_');
2772 /* Return an identifier for the mangled name of a this-adjusting or
2773 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
2774 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
2775 is a virtual thunk, and it is the vtbl offset in
2776 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
2777 zero for a covariant thunk. Note, that FN_DECL might be a covariant
2778 thunk itself. A covariant thunk name always includes the adjustment
2779 for the this pointer, even if there is none.
2781 <special-name> ::= T <call-offset> <base encoding>
2782 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2783 <base encoding> */
2785 tree
2786 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
2787 tree virtual_offset)
2789 const char *result;
2791 start_mangling (fn_decl, /*ident_p=*/true);
2793 write_string ("_Z");
2794 write_char ('T');
2796 if (!this_adjusting)
2798 /* Covariant thunk with no this adjustment */
2799 write_char ('c');
2800 mangle_call_offset (integer_zero_node, NULL_TREE);
2801 mangle_call_offset (fixed_offset, virtual_offset);
2803 else if (!DECL_THUNK_P (fn_decl))
2804 /* Plain this adjusting thunk. */
2805 mangle_call_offset (fixed_offset, virtual_offset);
2806 else
2808 /* This adjusting thunk to covariant thunk. */
2809 write_char ('c');
2810 mangle_call_offset (fixed_offset, virtual_offset);
2811 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2812 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2813 if (virtual_offset)
2814 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2815 mangle_call_offset (fixed_offset, virtual_offset);
2816 fn_decl = THUNK_TARGET (fn_decl);
2819 /* Scoped name. */
2820 write_encoding (fn_decl);
2822 result = finish_mangling (/*warn=*/false);
2823 if (DEBUG_MANGLE)
2824 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2825 return get_identifier_nocopy (result);
2828 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
2829 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
2830 TYPE. */
2832 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
2834 /* Hash a node (VAL1) in the table. */
2836 static hashval_t
2837 hash_type (const void *val)
2839 return (hashval_t) TYPE_UID (TREE_TYPE ((tree) val));
2842 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
2844 static int
2845 compare_type (const void *val1, const void *val2)
2847 return TREE_TYPE ((tree) val1) == (tree) val2;
2850 /* Return an identifier for the mangled unqualified name for a
2851 conversion operator to TYPE. This mangling is not specified by the
2852 ABI spec; it is only used internally. */
2854 tree
2855 mangle_conv_op_name_for_type (const tree type)
2857 void **slot;
2858 tree identifier;
2860 if (type == error_mark_node)
2861 return error_mark_node;
2863 if (conv_type_names == NULL)
2864 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
2866 slot = htab_find_slot_with_hash (conv_type_names, type,
2867 (hashval_t) TYPE_UID (type), INSERT);
2868 identifier = (tree)*slot;
2869 if (!identifier)
2871 char buffer[64];
2873 /* Create a unique name corresponding to TYPE. */
2874 sprintf (buffer, "operator %lu",
2875 (unsigned long) htab_elements (conv_type_names));
2876 identifier = get_identifier (buffer);
2877 *slot = identifier;
2879 /* Hang TYPE off the identifier so it can be found easily later
2880 when performing conversions. */
2881 TREE_TYPE (identifier) = type;
2883 /* Set bits on the identifier so we know later it's a conversion. */
2884 IDENTIFIER_OPNAME_P (identifier) = 1;
2885 IDENTIFIER_TYPENAME_P (identifier) = 1;
2888 return identifier;
2891 /* Return an identifier for the name of an initialization guard
2892 variable for indicated VARIABLE. */
2894 tree
2895 mangle_guard_variable (const tree variable)
2897 start_mangling (variable, /*ident_p=*/true);
2898 write_string ("_ZGV");
2899 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
2900 /* The name of a guard variable for a reference temporary should refer
2901 to the reference, not the temporary. */
2902 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
2903 else
2904 write_name (variable, /*ignore_local_scope=*/0);
2905 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2908 /* Return an identifier for the name of a temporary variable used to
2909 initialize a static reference. This isn't part of the ABI, but we might
2910 as well call them something readable. */
2912 tree
2913 mangle_ref_init_variable (const tree variable)
2915 start_mangling (variable, /*ident_p=*/true);
2916 write_string ("_ZGR");
2917 write_name (variable, /*ignore_local_scope=*/0);
2918 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2922 /* Foreign language type mangling section. */
2924 /* How to write the type codes for the integer Java type. */
2926 static void
2927 write_java_integer_type_codes (const tree type)
2929 if (type == java_int_type_node)
2930 write_char ('i');
2931 else if (type == java_short_type_node)
2932 write_char ('s');
2933 else if (type == java_byte_type_node)
2934 write_char ('c');
2935 else if (type == java_char_type_node)
2936 write_char ('w');
2937 else if (type == java_long_type_node)
2938 write_char ('x');
2939 else if (type == java_boolean_type_node)
2940 write_char ('b');
2941 else
2942 gcc_unreachable ();
2945 #include "gt-cp-mangle.h"