* config/i386/i386.h (TARGET_SUPPORTS_WIDE_INT): New define.
[official-gcc.git] / gcc / cp / mangle.c
blobb0f72d1ff14e37c9c210e331cc2bb5a0e0e3d6b6
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Written by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
7 GCC 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 3, or (at your option)
10 any later version.
12 GCC 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 GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file implements mangling of C++ names according to the IA64
22 C++ ABI specification. A mangled name encodes a function or
23 variable's name, scope, type, and/or template arguments into a text
24 identifier. This identifier is used as the function's or
25 variable's linkage name, to preserve compatibility between C++'s
26 language features (templates, scoping, and overloading) and C
27 linkers.
29 Additionally, g++ uses mangled names internally. To support this,
30 mangling of types is allowed, even though the mangled name of a
31 type should not appear by itself as an exported name. Ditto for
32 uninstantiated templates.
34 The primary entry point for this module is mangle_decl, which
35 returns an identifier containing the mangled name for a decl.
36 Additional entry points are provided to build mangled names of
37 particular constructs when the appropriate decl for that construct
38 is not available. These are:
40 mangle_typeinfo_for_type: typeinfo data
41 mangle_typeinfo_string_for_type: typeinfo type name
42 mangle_vtbl_for_type: virtual table data
43 mangle_vtt_for_type: VTT data
44 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
45 mangle_thunk: thunk function or entry */
47 #include "config.h"
48 #include "system.h"
49 #include "coretypes.h"
50 #include "tm.h"
51 #include "hash-set.h"
52 #include "machmode.h"
53 #include "vec.h"
54 #include "double-int.h"
55 #include "input.h"
56 #include "alias.h"
57 #include "symtab.h"
58 #include "wide-int.h"
59 #include "inchash.h"
60 #include "tree.h"
61 #include "tree-hasher.h"
62 #include "stor-layout.h"
63 #include "stringpool.h"
64 #include "tm_p.h"
65 #include "cp-tree.h"
66 #include "obstack.h"
67 #include "flags.h"
68 #include "target.h"
69 #include "hash-map.h"
70 #include "is-a.h"
71 #include "plugin-api.h"
72 #include "hard-reg-set.h"
73 #include "input.h"
74 #include "function.h"
75 #include "ipa-ref.h"
76 #include "cgraph.h"
77 #include "wide-int.h"
79 /* Debugging support. */
81 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
82 #ifndef DEBUG_MANGLE
83 #define DEBUG_MANGLE 0
84 #endif
86 /* Macros for tracing the write_* functions. */
87 #if DEBUG_MANGLE
88 # define MANGLE_TRACE(FN, INPUT) \
89 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
90 # define MANGLE_TRACE_TREE(FN, NODE) \
91 fprintf (stderr, " %-24s: %-24s (%p)\n", \
92 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
93 #else
94 # define MANGLE_TRACE(FN, INPUT)
95 # define MANGLE_TRACE_TREE(FN, NODE)
96 #endif
98 /* Nonzero if NODE is a class template-id. We can't rely on
99 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
100 that hard to distinguish A<T> from A, where A<T> is the type as
101 instantiated outside of the template, and A is the type used
102 without parameters inside the template. */
103 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
104 (TYPE_LANG_SPECIFIC (NODE) != NULL \
105 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
106 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
107 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
109 /* Things we only need one of. This module is not reentrant. */
110 typedef struct GTY(()) globals {
111 /* An array of the current substitution candidates, in the order
112 we've seen them. */
113 vec<tree, va_gc> *substitutions;
115 /* The entity that is being mangled. */
116 tree GTY ((skip)) entity;
118 /* How many parameter scopes we are inside. */
119 int parm_depth;
121 /* True if the mangling will be different in a future version of the
122 ABI. */
123 bool need_abi_warning;
124 } globals;
126 static GTY (()) globals G;
128 /* The obstack on which we build mangled names. */
129 static struct obstack *mangle_obstack;
131 /* The obstack on which we build mangled names that are not going to
132 be IDENTIFIER_NODEs. */
133 static struct obstack name_obstack;
135 /* The first object on the name_obstack; we use this to free memory
136 allocated on the name_obstack. */
137 static void *name_base;
139 /* Indices into subst_identifiers. These are identifiers used in
140 special substitution rules. */
141 typedef enum
143 SUBID_ALLOCATOR,
144 SUBID_BASIC_STRING,
145 SUBID_CHAR_TRAITS,
146 SUBID_BASIC_ISTREAM,
147 SUBID_BASIC_OSTREAM,
148 SUBID_BASIC_IOSTREAM,
149 SUBID_MAX
151 substitution_identifier_index_t;
153 /* For quick substitution checks, look up these common identifiers
154 once only. */
155 static GTY(()) tree subst_identifiers[SUBID_MAX];
157 /* Single-letter codes for builtin integer types, defined in
158 <builtin-type>. These are indexed by integer_type_kind values. */
159 static const char
160 integer_type_codes[itk_none] =
162 'c', /* itk_char */
163 'a', /* itk_signed_char */
164 'h', /* itk_unsigned_char */
165 's', /* itk_short */
166 't', /* itk_unsigned_short */
167 'i', /* itk_int */
168 'j', /* itk_unsigned_int */
169 'l', /* itk_long */
170 'm', /* itk_unsigned_long */
171 'x', /* itk_long_long */
172 'y', /* itk_unsigned_long_long */
173 /* __intN types are handled separately */
174 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
177 static int decl_is_template_id (const tree, tree* const);
179 /* Functions for handling substitutions. */
181 static inline tree canonicalize_for_substitution (tree);
182 static void add_substitution (tree);
183 static inline int is_std_substitution (const tree,
184 const substitution_identifier_index_t);
185 static inline int is_std_substitution_char (const tree,
186 const substitution_identifier_index_t);
187 static int find_substitution (tree);
188 static void mangle_call_offset (const tree, const tree);
190 /* Functions for emitting mangled representations of things. */
192 static void write_mangled_name (const tree, bool);
193 static void write_encoding (const tree);
194 static void write_name (tree, const int);
195 static void write_abi_tags (tree);
196 static void write_unscoped_name (const tree);
197 static void write_unscoped_template_name (const tree);
198 static void write_nested_name (const tree);
199 static void write_prefix (const tree);
200 static void write_template_prefix (const tree);
201 static void write_unqualified_name (tree);
202 static void write_conversion_operator_name (const tree);
203 static void write_source_name (tree);
204 static void write_literal_operator_name (tree);
205 static void write_unnamed_type_name (const tree);
206 static void write_closure_type_name (const tree);
207 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
208 const unsigned int);
209 static void write_number (unsigned HOST_WIDE_INT, const int,
210 const unsigned int);
211 static void write_compact_number (int num);
212 static void write_integer_cst (const tree);
213 static void write_real_cst (const tree);
214 static void write_identifier (const char *);
215 static void write_special_name_constructor (const tree);
216 static void write_special_name_destructor (const tree);
217 static void write_type (tree);
218 static int write_CV_qualifiers_for_type (const tree);
219 static void write_builtin_type (tree);
220 static void write_function_type (const tree);
221 static void write_bare_function_type (const tree, const int, const tree);
222 static void write_method_parms (tree, const int, const tree);
223 static void write_class_enum_type (const tree);
224 static void write_template_args (tree);
225 static void write_expression (tree);
226 static void write_template_arg_literal (const tree);
227 static void write_template_arg (tree);
228 static void write_template_template_arg (const tree);
229 static void write_array_type (const tree);
230 static void write_pointer_to_member_type (const tree);
231 static void write_template_param (const tree);
232 static void write_template_template_param (const tree);
233 static void write_substitution (const int);
234 static int discriminator_for_local_entity (tree);
235 static int discriminator_for_string_literal (tree, tree);
236 static void write_discriminator (const int);
237 static void write_local_name (tree, const tree, const tree);
238 static void dump_substitution_candidates (void);
239 static tree mangle_decl_string (const tree);
240 static int local_class_index (tree);
242 /* Control functions. */
244 static inline void start_mangling (const tree);
245 static tree mangle_special_for_type (const tree, const char *);
247 /* Foreign language functions. */
249 static void write_java_integer_type_codes (const tree);
251 /* Append a single character to the end of the mangled
252 representation. */
253 #define write_char(CHAR) \
254 obstack_1grow (mangle_obstack, (CHAR))
256 /* Append a sized buffer to the end of the mangled representation. */
257 #define write_chars(CHAR, LEN) \
258 obstack_grow (mangle_obstack, (CHAR), (LEN))
260 /* Append a NUL-terminated string to the end of the mangled
261 representation. */
262 #define write_string(STRING) \
263 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
265 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
266 same purpose (context, which may be a type) and value (template
267 decl). See write_template_prefix for more information on what this
268 is used for. */
269 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
270 (TREE_CODE (NODE1) == TREE_LIST \
271 && TREE_CODE (NODE2) == TREE_LIST \
272 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
273 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
274 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
275 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
277 /* Write out an unsigned quantity in base 10. */
278 #define write_unsigned_number(NUMBER) \
279 write_number ((NUMBER), /*unsigned_p=*/1, 10)
281 /* If DECL is a template instance, return nonzero and, if
282 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
283 Otherwise return zero. */
285 static int
286 decl_is_template_id (const tree decl, tree* const template_info)
288 if (TREE_CODE (decl) == TYPE_DECL)
290 /* TYPE_DECLs are handled specially. Look at its type to decide
291 if this is a template instantiation. */
292 const tree type = TREE_TYPE (decl);
294 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
296 if (template_info != NULL)
297 /* For a templated TYPE_DECL, the template info is hanging
298 off the type. */
299 *template_info = TYPE_TEMPLATE_INFO (type);
300 return 1;
303 else
305 /* Check if this is a primary template. */
306 if (DECL_LANG_SPECIFIC (decl) != NULL
307 && DECL_USE_TEMPLATE (decl)
308 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
309 && TREE_CODE (decl) != TEMPLATE_DECL)
311 if (template_info != NULL)
312 /* For most templated decls, the template info is hanging
313 off the decl. */
314 *template_info = DECL_TEMPLATE_INFO (decl);
315 return 1;
319 /* It's not a template id. */
320 return 0;
323 /* Produce debugging output of current substitution candidates. */
325 static void
326 dump_substitution_candidates (void)
328 unsigned i;
329 tree el;
331 fprintf (stderr, " ++ substitutions ");
332 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
334 const char *name = "???";
336 if (i > 0)
337 fprintf (stderr, " ");
338 if (DECL_P (el))
339 name = IDENTIFIER_POINTER (DECL_NAME (el));
340 else if (TREE_CODE (el) == TREE_LIST)
341 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
342 else if (TYPE_NAME (el))
343 name = TYPE_NAME_STRING (el);
344 fprintf (stderr, " S%d_ = ", i - 1);
345 if (TYPE_P (el) &&
346 (CP_TYPE_RESTRICT_P (el)
347 || CP_TYPE_VOLATILE_P (el)
348 || CP_TYPE_CONST_P (el)))
349 fprintf (stderr, "CV-");
350 fprintf (stderr, "%s (%s at %p)\n",
351 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
355 /* Both decls and types can be substitution candidates, but sometimes
356 they refer to the same thing. For instance, a TYPE_DECL and
357 RECORD_TYPE for the same class refer to the same thing, and should
358 be treated accordingly in substitutions. This function returns a
359 canonicalized tree node representing NODE that is used when adding
360 and substitution candidates and finding matches. */
362 static inline tree
363 canonicalize_for_substitution (tree node)
365 /* For a TYPE_DECL, use the type instead. */
366 if (TREE_CODE (node) == TYPE_DECL)
367 node = TREE_TYPE (node);
368 if (TYPE_P (node)
369 && TYPE_CANONICAL (node) != node
370 && TYPE_MAIN_VARIANT (node) != node)
372 tree orig = node;
373 /* Here we want to strip the topmost typedef only.
374 We need to do that so is_std_substitution can do proper
375 name matching. */
376 if (TREE_CODE (node) == FUNCTION_TYPE)
377 /* Use build_qualified_type and TYPE_QUALS here to preserve
378 the old buggy mangling of attribute noreturn with abi<5. */
379 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
380 TYPE_QUALS (node));
381 else
382 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
383 cp_type_quals (node));
384 if (TREE_CODE (node) == FUNCTION_TYPE
385 || TREE_CODE (node) == METHOD_TYPE)
386 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
388 return node;
391 /* Add NODE as a substitution candidate. NODE must not already be on
392 the list of candidates. */
394 static void
395 add_substitution (tree node)
397 tree c;
399 if (DEBUG_MANGLE)
400 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
401 get_tree_code_name (TREE_CODE (node)), (void *) node);
403 /* Get the canonicalized substitution candidate for NODE. */
404 c = canonicalize_for_substitution (node);
405 if (DEBUG_MANGLE && c != node)
406 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
407 get_tree_code_name (TREE_CODE (node)), (void *) node);
408 node = c;
410 #if ENABLE_CHECKING
411 /* Make sure NODE isn't already a candidate. */
413 int i;
414 tree candidate;
416 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
418 gcc_assert (!(DECL_P (node) && node == candidate));
419 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
420 && same_type_p (node, candidate)));
423 #endif /* ENABLE_CHECKING */
425 /* Put the decl onto the varray of substitution candidates. */
426 vec_safe_push (G.substitutions, node);
428 if (DEBUG_MANGLE)
429 dump_substitution_candidates ();
432 /* Helper function for find_substitution. Returns nonzero if NODE,
433 which may be a decl or a CLASS_TYPE, is a template-id with template
434 name of substitution_index[INDEX] in the ::std namespace. */
436 static inline int
437 is_std_substitution (const tree node,
438 const substitution_identifier_index_t index)
440 tree type = NULL;
441 tree decl = NULL;
443 if (DECL_P (node))
445 type = TREE_TYPE (node);
446 decl = node;
448 else if (CLASS_TYPE_P (node))
450 type = node;
451 decl = TYPE_NAME (node);
453 else
454 /* These are not the droids you're looking for. */
455 return 0;
457 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
458 && TYPE_LANG_SPECIFIC (type)
459 && TYPE_TEMPLATE_INFO (type)
460 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
461 == subst_identifiers[index]));
464 /* Helper function for find_substitution. Returns nonzero if NODE,
465 which may be a decl or a CLASS_TYPE, is the template-id
466 ::std::identifier<char>, where identifier is
467 substitution_index[INDEX]. */
469 static inline int
470 is_std_substitution_char (const tree node,
471 const substitution_identifier_index_t index)
473 tree args;
474 /* Check NODE's name is ::std::identifier. */
475 if (!is_std_substitution (node, index))
476 return 0;
477 /* Figure out its template args. */
478 if (DECL_P (node))
479 args = DECL_TI_ARGS (node);
480 else if (CLASS_TYPE_P (node))
481 args = CLASSTYPE_TI_ARGS (node);
482 else
483 /* Oops, not a template. */
484 return 0;
485 /* NODE's template arg list should be <char>. */
486 return
487 TREE_VEC_LENGTH (args) == 1
488 && TREE_VEC_ELT (args, 0) == char_type_node;
491 /* Check whether a substitution should be used to represent NODE in
492 the mangling.
494 First, check standard special-case substitutions.
496 <substitution> ::= St
497 # ::std
499 ::= Sa
500 # ::std::allocator
502 ::= Sb
503 # ::std::basic_string
505 ::= Ss
506 # ::std::basic_string<char,
507 ::std::char_traits<char>,
508 ::std::allocator<char> >
510 ::= Si
511 # ::std::basic_istream<char, ::std::char_traits<char> >
513 ::= So
514 # ::std::basic_ostream<char, ::std::char_traits<char> >
516 ::= Sd
517 # ::std::basic_iostream<char, ::std::char_traits<char> >
519 Then examine the stack of currently available substitution
520 candidates for entities appearing earlier in the same mangling
522 If a substitution is found, write its mangled representation and
523 return nonzero. If none is found, just return zero. */
525 static int
526 find_substitution (tree node)
528 int i;
529 const int size = vec_safe_length (G.substitutions);
530 tree decl;
531 tree type;
532 const char *abbr = NULL;
534 if (DEBUG_MANGLE)
535 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
536 get_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)))
551 abbr = "Sa";
553 /* Check for std::basic_string. */
554 else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
556 if (TYPE_P (node))
558 /* If this is a type (i.e. a fully-qualified template-id),
559 check for
560 std::basic_string <char,
561 std::char_traits<char>,
562 std::allocator<char> > . */
563 if (cp_type_quals (type) == TYPE_UNQUALIFIED
564 && CLASSTYPE_USE_TEMPLATE (type))
566 tree args = CLASSTYPE_TI_ARGS (type);
567 if (TREE_VEC_LENGTH (args) == 3
568 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
569 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
570 SUBID_CHAR_TRAITS)
571 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
572 SUBID_ALLOCATOR))
573 abbr = "Ss";
576 else
577 /* Substitute for the template name only if this isn't a type. */
578 abbr = "Sb";
581 /* Check for basic_{i,o,io}stream. */
582 else if (TYPE_P (node)
583 && cp_type_quals (type) == TYPE_UNQUALIFIED
584 && CLASS_TYPE_P (type)
585 && CLASSTYPE_USE_TEMPLATE (type)
586 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
588 /* First, check for the template
589 args <char, std::char_traits<char> > . */
590 tree args = CLASSTYPE_TI_ARGS (type);
591 if (TREE_VEC_LENGTH (args) == 2
592 && TYPE_P (TREE_VEC_ELT (args, 0))
593 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
594 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
595 SUBID_CHAR_TRAITS))
597 /* Got them. Is this basic_istream? */
598 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
599 abbr = "Si";
600 /* Or basic_ostream? */
601 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
602 abbr = "So";
603 /* Or basic_iostream? */
604 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
605 abbr = "Sd";
609 /* Check for namespace std. */
610 else if (decl && DECL_NAMESPACE_STD_P (decl))
612 write_string ("St");
613 return 1;
616 tree tags = NULL_TREE;
617 if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
618 tags = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (type));
619 /* Now check the list of available substitutions for this mangling
620 operation. */
621 if (!abbr || tags) for (i = 0; i < size; ++i)
623 tree candidate = (*G.substitutions)[i];
624 /* NODE is a matched to a candidate if it's the same decl node or
625 if it's the same type. */
626 if (decl == candidate
627 || (TYPE_P (candidate) && type && TYPE_P (node)
628 && same_type_p (type, candidate))
629 || NESTED_TEMPLATE_MATCH (node, candidate))
631 write_substitution (i);
632 return 1;
636 if (!abbr)
637 /* No substitution found. */
638 return 0;
640 write_string (abbr);
641 if (tags)
643 /* If there are ABI tags on the abbreviation, it becomes
644 a substitution candidate. */
645 write_abi_tags (tags);
646 add_substitution (node);
648 return 1;
651 /* Returns whether DECL's symbol name should be the plain unqualified-id
652 rather than a more complicated mangled name. */
654 static bool
655 unmangled_name_p (const tree decl)
657 if (TREE_CODE (decl) == FUNCTION_DECL)
659 /* The names of `extern "C"' functions are not mangled. */
660 return (DECL_EXTERN_C_FUNCTION_P (decl)
661 /* But overloaded operator names *are* mangled. */
662 && !DECL_OVERLOADED_OPERATOR_P (decl));
664 else if (VAR_P (decl))
666 /* static variables are mangled. */
667 if (!DECL_EXTERNAL_LINKAGE_P (decl))
668 return false;
670 /* extern "C" declarations aren't mangled. */
671 if (DECL_EXTERN_C_P (decl))
672 return true;
674 /* Other variables at non-global scope are mangled. */
675 if (CP_DECL_CONTEXT (decl) != global_namespace)
676 return false;
678 /* Variable template instantiations are mangled. */
679 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
680 && variable_template_p (DECL_TI_TEMPLATE (decl)))
681 return false;
683 /* Declarations with ABI tags are mangled. */
684 if (lookup_attribute ("abi_tag", DECL_ATTRIBUTES (decl)))
685 return false;
687 /* The names of non-static global variables aren't mangled. */
688 return true;
691 return false;
694 /* TOP_LEVEL is true, if this is being called at outermost level of
695 mangling. It should be false when mangling a decl appearing in an
696 expression within some other mangling.
698 <mangled-name> ::= _Z <encoding> */
700 static void
701 write_mangled_name (const tree decl, bool top_level)
703 MANGLE_TRACE_TREE ("mangled-name", decl);
705 check_abi_tags (decl);
707 if (unmangled_name_p (decl))
709 if (top_level)
710 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
711 else
713 /* The standard notes: "The <encoding> of an extern "C"
714 function is treated like global-scope data, i.e. as its
715 <source-name> without a type." We cannot write
716 overloaded operators that way though, because it contains
717 characters invalid in assembler. */
718 write_string ("_Z");
719 write_source_name (DECL_NAME (decl));
722 else
724 write_string ("_Z");
725 write_encoding (decl);
729 /* Returns true if the return type of DECL is part of its signature, and
730 therefore its mangling. */
732 bool
733 mangle_return_type_p (tree decl)
735 return (!DECL_CONSTRUCTOR_P (decl)
736 && !DECL_DESTRUCTOR_P (decl)
737 && !DECL_CONV_FN_P (decl)
738 && decl_is_template_id (decl, NULL));
741 /* <encoding> ::= <function name> <bare-function-type>
742 ::= <data name> */
744 static void
745 write_encoding (const tree decl)
747 MANGLE_TRACE_TREE ("encoding", decl);
749 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
751 /* For overloaded operators write just the mangled name
752 without arguments. */
753 if (DECL_OVERLOADED_OPERATOR_P (decl))
754 write_name (decl, /*ignore_local_scope=*/0);
755 else
756 write_source_name (DECL_NAME (decl));
757 return;
760 write_name (decl, /*ignore_local_scope=*/0);
761 if (TREE_CODE (decl) == FUNCTION_DECL)
763 tree fn_type;
764 tree d;
766 if (decl_is_template_id (decl, NULL))
768 fn_type = get_mostly_instantiated_function_type (decl);
769 /* FN_TYPE will not have parameter types for in-charge or
770 VTT parameters. Therefore, we pass NULL_TREE to
771 write_bare_function_type -- otherwise, it will get
772 confused about which artificial parameters to skip. */
773 d = NULL_TREE;
775 else
777 fn_type = TREE_TYPE (decl);
778 d = decl;
781 write_bare_function_type (fn_type,
782 mangle_return_type_p (decl),
787 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
788 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
790 static tree
791 decl_mangling_context (tree decl)
793 tree tcontext = targetm.cxx.decl_mangling_context (decl);
795 if (tcontext != NULL_TREE)
796 return tcontext;
798 if (TREE_CODE (decl) == TEMPLATE_DECL
799 && DECL_TEMPLATE_RESULT (decl))
800 decl = DECL_TEMPLATE_RESULT (decl);
802 if (TREE_CODE (decl) == TYPE_DECL
803 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
805 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
806 if (extra)
807 return extra;
809 else if (template_type_parameter_p (decl))
810 /* template type parms have no mangling context. */
811 return NULL_TREE;
812 return CP_DECL_CONTEXT (decl);
815 /* <name> ::= <unscoped-name>
816 ::= <unscoped-template-name> <template-args>
817 ::= <nested-name>
818 ::= <local-name>
820 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
821 called from <local-name>, which mangles the enclosing scope
822 elsewhere and then uses this function to mangle just the part
823 underneath the function scope. So don't use the <local-name>
824 production, to avoid an infinite recursion. */
826 static void
827 write_name (tree decl, const int ignore_local_scope)
829 tree context;
831 MANGLE_TRACE_TREE ("name", decl);
833 if (TREE_CODE (decl) == TYPE_DECL)
835 /* In case this is a typedef, fish out the corresponding
836 TYPE_DECL for the main variant. */
837 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
840 context = decl_mangling_context (decl);
842 gcc_assert (context != NULL_TREE);
844 if (abi_version_crosses (7)
845 && ignore_local_scope
846 && TREE_CODE (context) == PARM_DECL)
847 G.need_abi_warning = 1;
849 /* A decl in :: or ::std scope is treated specially. The former is
850 mangled using <unscoped-name> or <unscoped-template-name>, the
851 latter with a special substitution. Also, a name that is
852 directly in a local function scope is also mangled with
853 <unscoped-name> rather than a full <nested-name>. */
854 if (context == global_namespace
855 || DECL_NAMESPACE_STD_P (context)
856 || (ignore_local_scope
857 && (TREE_CODE (context) == FUNCTION_DECL
858 || (abi_version_at_least (7)
859 && TREE_CODE (context) == PARM_DECL))))
861 tree template_info;
862 /* Is this a template instance? */
863 if (decl_is_template_id (decl, &template_info))
865 /* Yes: use <unscoped-template-name>. */
866 write_unscoped_template_name (TI_TEMPLATE (template_info));
867 write_template_args (TI_ARGS (template_info));
869 else
870 /* Everything else gets an <unqualified-name>. */
871 write_unscoped_name (decl);
873 else
875 /* Handle local names, unless we asked not to (that is, invoked
876 under <local-name>, to handle only the part of the name under
877 the local scope). */
878 if (!ignore_local_scope)
880 /* Scan up the list of scope context, looking for a
881 function. If we find one, this entity is in local
882 function scope. local_entity tracks context one scope
883 level down, so it will contain the element that's
884 directly in that function's scope, either decl or one of
885 its enclosing scopes. */
886 tree local_entity = decl;
887 while (context != global_namespace)
889 /* Make sure we're always dealing with decls. */
890 if (TYPE_P (context))
891 context = TYPE_NAME (context);
892 /* Is this a function? */
893 if (TREE_CODE (context) == FUNCTION_DECL
894 || TREE_CODE (context) == PARM_DECL)
896 /* Yes, we have local scope. Use the <local-name>
897 production for the innermost function scope. */
898 write_local_name (context, local_entity, decl);
899 return;
901 /* Up one scope level. */
902 local_entity = context;
903 context = decl_mangling_context (context);
906 /* No local scope found? Fall through to <nested-name>. */
909 /* Other decls get a <nested-name> to encode their scope. */
910 write_nested_name (decl);
914 /* <unscoped-name> ::= <unqualified-name>
915 ::= St <unqualified-name> # ::std:: */
917 static void
918 write_unscoped_name (const tree decl)
920 tree context = decl_mangling_context (decl);
922 MANGLE_TRACE_TREE ("unscoped-name", decl);
924 /* Is DECL in ::std? */
925 if (DECL_NAMESPACE_STD_P (context))
927 write_string ("St");
928 write_unqualified_name (decl);
930 else
932 /* If not, it should be either in the global namespace, or directly
933 in a local function scope. A lambda can also be mangled in the
934 scope of a default argument. */
935 gcc_assert (context == global_namespace
936 || TREE_CODE (context) == PARM_DECL
937 || TREE_CODE (context) == FUNCTION_DECL);
939 write_unqualified_name (decl);
943 /* <unscoped-template-name> ::= <unscoped-name>
944 ::= <substitution> */
946 static void
947 write_unscoped_template_name (const tree decl)
949 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
951 if (find_substitution (decl))
952 return;
953 write_unscoped_name (decl);
954 add_substitution (decl);
957 /* Write the nested name, including CV-qualifiers, of DECL.
959 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
960 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
962 <ref-qualifier> ::= R # & ref-qualifier
963 ::= O # && ref-qualifier
964 <CV-qualifiers> ::= [r] [V] [K] */
966 static void
967 write_nested_name (const tree decl)
969 tree template_info;
971 MANGLE_TRACE_TREE ("nested-name", decl);
973 write_char ('N');
975 /* Write CV-qualifiers, if this is a member function. */
976 if (TREE_CODE (decl) == FUNCTION_DECL
977 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
979 if (DECL_VOLATILE_MEMFUNC_P (decl))
980 write_char ('V');
981 if (DECL_CONST_MEMFUNC_P (decl))
982 write_char ('K');
983 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
985 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
986 write_char ('O');
987 else
988 write_char ('R');
992 /* Is this a template instance? */
993 if (decl_is_template_id (decl, &template_info))
995 /* Yes, use <template-prefix>. */
996 write_template_prefix (decl);
997 write_template_args (TI_ARGS (template_info));
999 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1001 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1002 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1004 write_template_prefix (decl);
1005 write_template_args (TREE_OPERAND (name, 1));
1007 else
1009 write_prefix (decl_mangling_context (decl));
1010 write_unqualified_name (decl);
1013 else
1015 /* No, just use <prefix> */
1016 write_prefix (decl_mangling_context (decl));
1017 write_unqualified_name (decl);
1019 write_char ('E');
1022 /* <prefix> ::= <prefix> <unqualified-name>
1023 ::= <template-param>
1024 ::= <template-prefix> <template-args>
1025 ::= <decltype>
1026 ::= # empty
1027 ::= <substitution> */
1029 static void
1030 write_prefix (const tree node)
1032 tree decl;
1033 /* Non-NULL if NODE represents a template-id. */
1034 tree template_info = NULL;
1036 if (node == NULL
1037 || node == global_namespace)
1038 return;
1040 MANGLE_TRACE_TREE ("prefix", node);
1042 if (TREE_CODE (node) == DECLTYPE_TYPE)
1044 write_type (node);
1045 return;
1048 if (find_substitution (node))
1049 return;
1051 if (DECL_P (node))
1053 /* If this is a function or parm decl, that means we've hit function
1054 scope, so this prefix must be for a local name. In this
1055 case, we're under the <local-name> production, which encodes
1056 the enclosing function scope elsewhere. So don't continue
1057 here. */
1058 if (TREE_CODE (node) == FUNCTION_DECL
1059 || TREE_CODE (node) == PARM_DECL)
1060 return;
1062 decl = node;
1063 decl_is_template_id (decl, &template_info);
1065 else
1067 /* Node is a type. */
1068 decl = TYPE_NAME (node);
1069 if (CLASSTYPE_TEMPLATE_ID_P (node))
1070 template_info = TYPE_TEMPLATE_INFO (node);
1073 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1074 write_template_param (node);
1075 else if (template_info != NULL)
1076 /* Templated. */
1078 write_template_prefix (decl);
1079 write_template_args (TI_ARGS (template_info));
1081 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1083 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1084 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1086 write_template_prefix (decl);
1087 write_template_args (TREE_OPERAND (name, 1));
1089 else
1091 write_prefix (decl_mangling_context (decl));
1092 write_unqualified_name (decl);
1095 else
1096 /* Not templated. */
1098 write_prefix (decl_mangling_context (decl));
1099 write_unqualified_name (decl);
1100 if (VAR_P (decl)
1101 || TREE_CODE (decl) == FIELD_DECL)
1103 /* <data-member-prefix> := <member source-name> M */
1104 write_char ('M');
1105 return;
1109 add_substitution (node);
1112 /* <template-prefix> ::= <prefix> <template component>
1113 ::= <template-param>
1114 ::= <substitution> */
1116 static void
1117 write_template_prefix (const tree node)
1119 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1120 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1121 tree context = decl_mangling_context (decl);
1122 tree template_info;
1123 tree templ;
1124 tree substitution;
1126 MANGLE_TRACE_TREE ("template-prefix", node);
1128 /* Find the template decl. */
1129 if (decl_is_template_id (decl, &template_info))
1130 templ = TI_TEMPLATE (template_info);
1131 else if (TREE_CODE (type) == TYPENAME_TYPE)
1132 /* For a typename type, all we have is the name. */
1133 templ = DECL_NAME (decl);
1134 else
1136 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1138 templ = TYPE_TI_TEMPLATE (type);
1141 /* For a member template, though, the template name for the
1142 innermost name must have all the outer template levels
1143 instantiated. For instance, consider
1145 template<typename T> struct Outer {
1146 template<typename U> struct Inner {};
1149 The template name for `Inner' in `Outer<int>::Inner<float>' is
1150 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1151 levels separately, so there's no TEMPLATE_DECL available for this
1152 (there's only `Outer<T>::Inner<U>').
1154 In order to get the substitutions right, we create a special
1155 TREE_LIST to represent the substitution candidate for a nested
1156 template. The TREE_PURPOSE is the template's context, fully
1157 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1158 template.
1160 So, for the example above, `Outer<int>::Inner' is represented as a
1161 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1162 and whose value is `Outer<T>::Inner<U>'. */
1163 if (TYPE_P (context))
1164 substitution = build_tree_list (context, templ);
1165 else
1166 substitution = templ;
1168 if (find_substitution (substitution))
1169 return;
1171 if (TREE_TYPE (templ)
1172 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1173 write_template_param (TREE_TYPE (templ));
1174 else
1176 write_prefix (context);
1177 write_unqualified_name (decl);
1180 add_substitution (substitution);
1183 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1184 mangled through special entry points.
1186 <unqualified-name> ::= <operator-name>
1187 ::= <special-name>
1188 ::= <source-name>
1189 ::= <unnamed-type-name>
1190 ::= <local-source-name>
1192 <local-source-name> ::= L <source-name> <discriminator> */
1194 static void
1195 write_unqualified_id (tree identifier)
1197 if (IDENTIFIER_TYPENAME_P (identifier))
1198 write_conversion_operator_name (TREE_TYPE (identifier));
1199 else if (IDENTIFIER_OPNAME_P (identifier))
1201 int i;
1202 const char *mangled_name = NULL;
1204 /* Unfortunately, there is no easy way to go from the
1205 name of the operator back to the corresponding tree
1206 code. */
1207 for (i = 0; i < MAX_TREE_CODES; ++i)
1208 if (operator_name_info[i].identifier == identifier)
1210 /* The ABI says that we prefer binary operator
1211 names to unary operator names. */
1212 if (operator_name_info[i].arity == 2)
1214 mangled_name = operator_name_info[i].mangled_name;
1215 break;
1217 else if (!mangled_name)
1218 mangled_name = operator_name_info[i].mangled_name;
1220 else if (assignment_operator_name_info[i].identifier
1221 == identifier)
1223 mangled_name
1224 = assignment_operator_name_info[i].mangled_name;
1225 break;
1227 write_string (mangled_name);
1229 else if (UDLIT_OPER_P (identifier))
1230 write_literal_operator_name (identifier);
1231 else
1232 write_source_name (identifier);
1235 static void
1236 write_unqualified_name (tree decl)
1238 MANGLE_TRACE_TREE ("unqualified-name", decl);
1240 if (identifier_p (decl))
1242 write_unqualified_id (decl);
1243 return;
1246 bool found = false;
1248 if (DECL_NAME (decl) == NULL_TREE)
1250 found = true;
1251 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1252 write_source_name (DECL_ASSEMBLER_NAME (decl));
1254 else if (DECL_DECLARES_FUNCTION_P (decl))
1256 found = true;
1257 if (DECL_CONSTRUCTOR_P (decl))
1258 write_special_name_constructor (decl);
1259 else if (DECL_DESTRUCTOR_P (decl))
1260 write_special_name_destructor (decl);
1261 else if (DECL_CONV_FN_P (decl))
1263 /* Conversion operator. Handle it right here.
1264 <operator> ::= cv <type> */
1265 tree type;
1266 if (decl_is_template_id (decl, NULL))
1268 tree fn_type;
1269 fn_type = get_mostly_instantiated_function_type (decl);
1270 type = TREE_TYPE (fn_type);
1272 else if (FNDECL_USED_AUTO (decl))
1273 type = (DECL_STRUCT_FUNCTION (decl)->language
1274 ->x_auto_return_pattern);
1275 else
1276 type = DECL_CONV_FN_TYPE (decl);
1277 write_conversion_operator_name (type);
1279 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1281 operator_name_info_t *oni;
1282 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1283 oni = assignment_operator_name_info;
1284 else
1285 oni = operator_name_info;
1287 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1289 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1290 write_literal_operator_name (DECL_NAME (decl));
1291 else
1292 found = false;
1295 if (found)
1296 /* OK */;
1297 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1298 && DECL_NAMESPACE_SCOPE_P (decl)
1299 && decl_linkage (decl) == lk_internal)
1301 MANGLE_TRACE_TREE ("local-source-name", decl);
1302 write_char ('L');
1303 write_source_name (DECL_NAME (decl));
1304 /* The default discriminator is 1, and that's all we ever use,
1305 so there's no code to output one here. */
1307 else
1309 tree type = TREE_TYPE (decl);
1311 if (TREE_CODE (decl) == TYPE_DECL
1312 && TYPE_ANONYMOUS_P (type))
1313 write_unnamed_type_name (type);
1314 else if (TREE_CODE (decl) == TYPE_DECL
1315 && LAMBDA_TYPE_P (type))
1316 write_closure_type_name (type);
1317 else
1318 write_source_name (DECL_NAME (decl));
1321 /* We use the ABI tags from the primary template, ignoring tags on any
1322 specializations. This is necessary because C++ doesn't require a
1323 specialization to be declared before it is used unless the use
1324 requires a complete type, but we need to get the tags right on
1325 incomplete types as well. */
1326 if (tree tmpl = most_general_template (decl))
1327 decl = DECL_TEMPLATE_RESULT (tmpl);
1328 /* Don't crash on an unbound class template. */
1329 if (decl && TREE_CODE (decl) != NAMESPACE_DECL)
1331 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1332 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1333 : DECL_ATTRIBUTES (decl));
1334 write_abi_tags (lookup_attribute ("abi_tag", attrs));
1338 /* Write the unqualified-name for a conversion operator to TYPE. */
1340 static void
1341 write_conversion_operator_name (const tree type)
1343 write_string ("cv");
1344 write_type (type);
1347 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1349 <source-name> ::= </length/ number> <identifier> */
1351 static void
1352 write_source_name (tree identifier)
1354 MANGLE_TRACE_TREE ("source-name", identifier);
1356 /* Never write the whole template-id name including the template
1357 arguments; we only want the template name. */
1358 if (IDENTIFIER_TEMPLATE (identifier))
1359 identifier = IDENTIFIER_TEMPLATE (identifier);
1361 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1362 write_identifier (IDENTIFIER_POINTER (identifier));
1365 /* Compare two TREE_STRINGs like strcmp. */
1368 tree_string_cmp (const void *p1, const void *p2)
1370 if (p1 == p2)
1371 return 0;
1372 tree s1 = *(const tree*)p1;
1373 tree s2 = *(const tree*)p2;
1374 return strcmp (TREE_STRING_POINTER (s1),
1375 TREE_STRING_POINTER (s2));
1378 /* ID is the name of a function or type with abi_tags attribute TAGS.
1379 Write out the name, suitably decorated. */
1381 static void
1382 write_abi_tags (tree tags)
1384 if (tags == NULL_TREE)
1385 return;
1387 tags = TREE_VALUE (tags);
1389 vec<tree, va_gc> * vec = make_tree_vector();
1391 for (tree t = tags; t; t = TREE_CHAIN (t))
1393 if (ABI_TAG_IMPLICIT (t))
1394 continue;
1395 tree str = TREE_VALUE (t);
1396 vec_safe_push (vec, str);
1399 vec->qsort (tree_string_cmp);
1401 unsigned i; tree str;
1402 FOR_EACH_VEC_ELT (*vec, i, str)
1404 write_string ("B");
1405 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1406 write_identifier (TREE_STRING_POINTER (str));
1409 release_tree_vector (vec);
1412 /* Write a user-defined literal operator.
1413 ::= li <source-name> # "" <source-name>
1414 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1416 static void
1417 write_literal_operator_name (tree identifier)
1419 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1420 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1421 write_unsigned_number (strlen (suffix));
1422 write_identifier (suffix);
1425 /* Encode 0 as _, and 1+ as n-1_. */
1427 static void
1428 write_compact_number (int num)
1430 if (num > 0)
1431 write_unsigned_number (num - 1);
1432 write_char ('_');
1435 /* Return how many unnamed types precede TYPE in its enclosing class. */
1437 static int
1438 nested_anon_class_index (tree type)
1440 int index = 0;
1441 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1442 for (; member; member = DECL_CHAIN (member))
1443 if (DECL_IMPLICIT_TYPEDEF_P (member))
1445 tree memtype = TREE_TYPE (member);
1446 if (memtype == type)
1447 return index;
1448 else if (TYPE_ANONYMOUS_P (memtype))
1449 ++index;
1452 gcc_unreachable ();
1455 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1457 static void
1458 write_unnamed_type_name (const tree type)
1460 int discriminator;
1461 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1463 if (TYPE_FUNCTION_SCOPE_P (type))
1464 discriminator = local_class_index (type);
1465 else if (TYPE_CLASS_SCOPE_P (type))
1466 discriminator = nested_anon_class_index (type);
1467 else
1469 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1470 /* Just use the old mangling at namespace scope. */
1471 write_source_name (TYPE_IDENTIFIER (type));
1472 return;
1475 write_string ("Ut");
1476 write_compact_number (discriminator);
1479 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1480 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1482 static void
1483 write_closure_type_name (const tree type)
1485 tree fn = lambda_function (type);
1486 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1487 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1489 MANGLE_TRACE_TREE ("closure-type-name", type);
1491 write_string ("Ul");
1492 write_method_parms (parms, /*method_p=*/1, fn);
1493 write_char ('E');
1494 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1497 /* Convert NUMBER to ascii using base BASE and generating at least
1498 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1499 into which to store the characters. Returns the number of
1500 characters generated (these will be laid out in advance of where
1501 BUFFER points). */
1503 static int
1504 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1505 char *buffer, const unsigned int min_digits)
1507 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1508 unsigned digits = 0;
1510 while (number)
1512 unsigned HOST_WIDE_INT d = number / base;
1514 *--buffer = base_digits[number - d * base];
1515 digits++;
1516 number = d;
1518 while (digits < min_digits)
1520 *--buffer = base_digits[0];
1521 digits++;
1523 return digits;
1526 /* Non-terminal <number>.
1528 <number> ::= [n] </decimal integer/> */
1530 static void
1531 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1532 const unsigned int base)
1534 char buffer[sizeof (HOST_WIDE_INT) * 8];
1535 unsigned count = 0;
1537 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1539 write_char ('n');
1540 number = -((HOST_WIDE_INT) number);
1542 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1543 write_chars (buffer + sizeof (buffer) - count, count);
1546 /* Write out an integral CST in decimal. Most numbers are small, and
1547 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1548 bigger than that, which we must deal with. */
1550 static inline void
1551 write_integer_cst (const tree cst)
1553 int sign = tree_int_cst_sgn (cst);
1554 widest_int abs_value = wi::abs (wi::to_widest (cst));
1555 if (!wi::fits_uhwi_p (abs_value))
1557 /* A bignum. We do this in chunks, each of which fits in a
1558 HOST_WIDE_INT. */
1559 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1560 unsigned HOST_WIDE_INT chunk;
1561 unsigned chunk_digits;
1562 char *ptr = buffer + sizeof (buffer);
1563 unsigned count = 0;
1564 tree n, base, type;
1565 int done;
1567 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1568 representable. */
1569 chunk = 1000000000;
1570 chunk_digits = 9;
1572 if (sizeof (HOST_WIDE_INT) >= 8)
1574 /* It is at least 64 bits, so 10^18 is representable. */
1575 chunk_digits = 18;
1576 chunk *= chunk;
1579 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1580 base = build_int_cstu (type, chunk);
1581 n = wide_int_to_tree (type, cst);
1583 if (sign < 0)
1585 write_char ('n');
1586 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1590 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1591 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1592 unsigned c;
1594 done = integer_zerop (d);
1595 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1596 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1597 done ? 1 : chunk_digits);
1598 ptr -= c;
1599 count += c;
1600 n = d;
1602 while (!done);
1603 write_chars (ptr, count);
1605 else
1607 /* A small num. */
1608 if (sign < 0)
1609 write_char ('n');
1610 write_unsigned_number (abs_value.to_uhwi ());
1614 /* Write out a floating-point literal.
1616 "Floating-point literals are encoded using the bit pattern of the
1617 target processor's internal representation of that number, as a
1618 fixed-length lowercase hexadecimal string, high-order bytes first
1619 (even if the target processor would store low-order bytes first).
1620 The "n" prefix is not used for floating-point literals; the sign
1621 bit is encoded with the rest of the number.
1623 Here are some examples, assuming the IEEE standard representation
1624 for floating point numbers. (Spaces are for readability, not
1625 part of the encoding.)
1627 1.0f Lf 3f80 0000 E
1628 -1.0f Lf bf80 0000 E
1629 1.17549435e-38f Lf 0080 0000 E
1630 1.40129846e-45f Lf 0000 0001 E
1631 0.0f Lf 0000 0000 E"
1633 Caller is responsible for the Lx and the E. */
1634 static void
1635 write_real_cst (const tree value)
1637 long target_real[4]; /* largest supported float */
1638 char buffer[9]; /* eight hex digits in a 32-bit number */
1639 int i, limit, dir;
1641 tree type = TREE_TYPE (value);
1642 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1644 real_to_target (target_real, &TREE_REAL_CST (value),
1645 TYPE_MODE (type));
1647 /* The value in target_real is in the target word order,
1648 so we must write it out backward if that happens to be
1649 little-endian. write_number cannot be used, it will
1650 produce uppercase. */
1651 if (FLOAT_WORDS_BIG_ENDIAN)
1652 i = 0, limit = words, dir = 1;
1653 else
1654 i = words - 1, limit = -1, dir = -1;
1656 for (; i != limit; i += dir)
1658 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1659 write_chars (buffer, 8);
1663 /* Non-terminal <identifier>.
1665 <identifier> ::= </unqualified source code identifier> */
1667 static void
1668 write_identifier (const char *identifier)
1670 MANGLE_TRACE ("identifier", identifier);
1671 write_string (identifier);
1674 /* Handle constructor productions of non-terminal <special-name>.
1675 CTOR is a constructor FUNCTION_DECL.
1677 <special-name> ::= C1 # complete object constructor
1678 ::= C2 # base object constructor
1679 ::= C3 # complete object allocating constructor
1681 Currently, allocating constructors are never used. */
1683 static void
1684 write_special_name_constructor (const tree ctor)
1686 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1687 write_string ("C2");
1688 /* This is the old-style "[unified]" constructor.
1689 In some cases, we may emit this function and call
1690 it from the clones in order to share code and save space. */
1691 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1692 write_string ("C4");
1693 else
1695 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1696 write_string ("C1");
1700 /* Handle destructor productions of non-terminal <special-name>.
1701 DTOR is a destructor FUNCTION_DECL.
1703 <special-name> ::= D0 # deleting (in-charge) destructor
1704 ::= D1 # complete object (in-charge) destructor
1705 ::= D2 # base object (not-in-charge) destructor */
1707 static void
1708 write_special_name_destructor (const tree dtor)
1710 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1711 write_string ("D0");
1712 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1713 write_string ("D2");
1714 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1715 /* This is the old-style "[unified]" destructor.
1716 In some cases, we may emit this function and call
1717 it from the clones in order to share code and save space. */
1718 write_string ("D4");
1719 else
1721 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1722 write_string ("D1");
1726 /* Scan the vector of local classes and return how many others with the
1727 same name (or same no name) and context precede ENTITY. */
1729 static int
1730 local_class_index (tree entity)
1732 int ix, discriminator = 0;
1733 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1734 : TYPE_IDENTIFIER (entity));
1735 tree ctx = TYPE_CONTEXT (entity);
1736 for (ix = 0; ; ix++)
1738 tree type = (*local_classes)[ix];
1739 if (type == entity)
1740 return discriminator;
1741 if (TYPE_CONTEXT (type) == ctx
1742 && (name ? TYPE_IDENTIFIER (type) == name
1743 : TYPE_ANONYMOUS_P (type)))
1744 ++discriminator;
1746 gcc_unreachable ();
1749 /* Return the discriminator for ENTITY appearing inside
1750 FUNCTION. The discriminator is the lexical ordinal of VAR among
1751 entities with the same name in the same FUNCTION. */
1753 static int
1754 discriminator_for_local_entity (tree entity)
1756 if (DECL_DISCRIMINATOR_P (entity))
1758 if (DECL_DISCRIMINATOR_SET_P (entity))
1759 return DECL_DISCRIMINATOR (entity);
1760 else
1761 /* The first entity with a particular name doesn't get
1762 DECL_DISCRIMINATOR set up. */
1763 return 0;
1765 else if (TREE_CODE (entity) == TYPE_DECL)
1767 /* Scan the list of local classes. */
1768 entity = TREE_TYPE (entity);
1770 /* Lambdas and unnamed types have their own discriminators. */
1771 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1772 return 0;
1774 return local_class_index (entity);
1776 else
1777 gcc_unreachable ();
1780 /* Return the discriminator for STRING, a string literal used inside
1781 FUNCTION. The discriminator is the lexical ordinal of STRING among
1782 string literals used in FUNCTION. */
1784 static int
1785 discriminator_for_string_literal (tree /*function*/,
1786 tree /*string*/)
1788 /* For now, we don't discriminate amongst string literals. */
1789 return 0;
1792 /* <discriminator> := _ <number>
1794 The discriminator is used only for the second and later occurrences
1795 of the same name within a single function. In this case <number> is
1796 n - 2, if this is the nth occurrence, in lexical order. */
1798 static void
1799 write_discriminator (const int discriminator)
1801 /* If discriminator is zero, don't write anything. Otherwise... */
1802 if (discriminator > 0)
1804 write_char ('_');
1805 write_unsigned_number (discriminator - 1);
1809 /* Mangle the name of a function-scope entity. FUNCTION is the
1810 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1811 default argument scope. ENTITY is the decl for the entity itself.
1812 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1813 either ENTITY itself or an enclosing scope of ENTITY.
1815 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1816 := Z <function encoding> E s [<discriminator>]
1817 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1819 static void
1820 write_local_name (tree function, const tree local_entity,
1821 const tree entity)
1823 tree parm = NULL_TREE;
1825 MANGLE_TRACE_TREE ("local-name", entity);
1827 if (TREE_CODE (function) == PARM_DECL)
1829 parm = function;
1830 function = DECL_CONTEXT (parm);
1833 write_char ('Z');
1834 write_encoding (function);
1835 write_char ('E');
1837 /* For this purpose, parameters are numbered from right-to-left. */
1838 if (parm)
1840 tree t;
1841 int i = 0;
1842 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1844 if (t == parm)
1845 i = 1;
1846 else if (i)
1847 ++i;
1849 write_char ('d');
1850 write_compact_number (i - 1);
1853 if (TREE_CODE (entity) == STRING_CST)
1855 write_char ('s');
1856 write_discriminator (discriminator_for_string_literal (function,
1857 entity));
1859 else
1861 /* Now the <entity name>. Let write_name know its being called
1862 from <local-name>, so it doesn't try to process the enclosing
1863 function scope again. */
1864 write_name (entity, /*ignore_local_scope=*/1);
1865 write_discriminator (discriminator_for_local_entity (local_entity));
1869 /* Non-terminals <type> and <CV-qualifier>.
1871 <type> ::= <builtin-type>
1872 ::= <function-type>
1873 ::= <class-enum-type>
1874 ::= <array-type>
1875 ::= <pointer-to-member-type>
1876 ::= <template-param>
1877 ::= <substitution>
1878 ::= <CV-qualifier>
1879 ::= P <type> # pointer-to
1880 ::= R <type> # reference-to
1881 ::= C <type> # complex pair (C 2000)
1882 ::= G <type> # imaginary (C 2000) [not supported]
1883 ::= U <source-name> <type> # vendor extended type qualifier
1885 C++0x extensions
1887 <type> ::= RR <type> # rvalue reference-to
1888 <type> ::= Dt <expression> # decltype of an id-expression or
1889 # class member access
1890 <type> ::= DT <expression> # decltype of an expression
1891 <type> ::= Dn # decltype of nullptr
1893 TYPE is a type node. */
1895 static void
1896 write_type (tree type)
1898 /* This gets set to nonzero if TYPE turns out to be a (possibly
1899 CV-qualified) builtin type. */
1900 int is_builtin_type = 0;
1902 MANGLE_TRACE_TREE ("type", type);
1904 if (type == error_mark_node)
1905 return;
1907 type = canonicalize_for_substitution (type);
1908 if (find_substitution (type))
1909 return;
1912 if (write_CV_qualifiers_for_type (type) > 0)
1913 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1914 mangle the unqualified type. The recursive call is needed here
1915 since both the qualified and unqualified types are substitution
1916 candidates. */
1918 tree t = TYPE_MAIN_VARIANT (type);
1919 if (TREE_CODE (t) == FUNCTION_TYPE
1920 || TREE_CODE (t) == METHOD_TYPE)
1922 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1923 if (abi_version_at_least (8))
1924 /* Avoid adding the unqualified function type as a substitution. */
1925 write_function_type (t);
1926 else
1927 write_type (t);
1928 if (abi_version_crosses (8))
1929 G.need_abi_warning = 1;
1931 else
1932 write_type (t);
1934 else if (TREE_CODE (type) == ARRAY_TYPE)
1935 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1936 so that the cv-qualification of the element type is available
1937 in write_array_type. */
1938 write_array_type (type);
1939 else
1941 tree type_orig = type;
1943 /* See through any typedefs. */
1944 type = TYPE_MAIN_VARIANT (type);
1945 if (TREE_CODE (type) == FUNCTION_TYPE
1946 || TREE_CODE (type) == METHOD_TYPE)
1947 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1949 /* According to the C++ ABI, some library classes are passed the
1950 same as the scalar type of their single member and use the same
1951 mangling. */
1952 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1953 type = TREE_TYPE (first_field (type));
1955 if (TYPE_PTRDATAMEM_P (type))
1956 write_pointer_to_member_type (type);
1957 else
1959 /* Handle any target-specific fundamental types. */
1960 const char *target_mangling
1961 = targetm.mangle_type (type_orig);
1963 if (target_mangling)
1965 write_string (target_mangling);
1966 /* Add substitutions for types other than fundamental
1967 types. */
1968 if (!VOID_TYPE_P (type)
1969 && TREE_CODE (type) != INTEGER_TYPE
1970 && TREE_CODE (type) != REAL_TYPE
1971 && TREE_CODE (type) != BOOLEAN_TYPE)
1972 add_substitution (type);
1973 return;
1976 switch (TREE_CODE (type))
1978 case VOID_TYPE:
1979 case BOOLEAN_TYPE:
1980 case INTEGER_TYPE: /* Includes wchar_t. */
1981 case REAL_TYPE:
1982 case FIXED_POINT_TYPE:
1984 /* If this is a typedef, TYPE may not be one of
1985 the standard builtin type nodes, but an alias of one. Use
1986 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1987 write_builtin_type (TYPE_MAIN_VARIANT (type));
1988 ++is_builtin_type;
1990 break;
1992 case COMPLEX_TYPE:
1993 write_char ('C');
1994 write_type (TREE_TYPE (type));
1995 break;
1997 case FUNCTION_TYPE:
1998 case METHOD_TYPE:
1999 write_function_type (type);
2000 break;
2002 case UNION_TYPE:
2003 case RECORD_TYPE:
2004 case ENUMERAL_TYPE:
2005 /* A pointer-to-member function is represented as a special
2006 RECORD_TYPE, so check for this first. */
2007 if (TYPE_PTRMEMFUNC_P (type))
2008 write_pointer_to_member_type (type);
2009 else
2010 write_class_enum_type (type);
2011 break;
2013 case TYPENAME_TYPE:
2014 case UNBOUND_CLASS_TEMPLATE:
2015 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2016 ordinary nested names. */
2017 write_nested_name (TYPE_STUB_DECL (type));
2018 break;
2020 case POINTER_TYPE:
2021 case REFERENCE_TYPE:
2022 if (TYPE_PTR_P (type))
2023 write_char ('P');
2024 else if (TYPE_REF_IS_RVALUE (type))
2025 write_char ('O');
2026 else
2027 write_char ('R');
2029 tree target = TREE_TYPE (type);
2030 /* Attribute const/noreturn are not reflected in mangling.
2031 We strip them here rather than at a lower level because
2032 a typedef or template argument can have function type
2033 with function-cv-quals (that use the same representation),
2034 but you can't have a pointer/reference to such a type. */
2035 if (TREE_CODE (target) == FUNCTION_TYPE)
2037 if (abi_version_crosses (5)
2038 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2039 G.need_abi_warning = 1;
2040 if (abi_version_at_least (5))
2041 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2043 write_type (target);
2045 break;
2047 case TEMPLATE_TYPE_PARM:
2048 if (is_auto (type))
2050 if (AUTO_IS_DECLTYPE (type))
2051 write_identifier ("Dc");
2052 else
2053 write_identifier ("Da");
2054 ++is_builtin_type;
2055 break;
2057 /* else fall through. */
2058 case TEMPLATE_PARM_INDEX:
2059 write_template_param (type);
2060 break;
2062 case TEMPLATE_TEMPLATE_PARM:
2063 write_template_template_param (type);
2064 break;
2066 case BOUND_TEMPLATE_TEMPLATE_PARM:
2067 write_template_template_param (type);
2068 write_template_args
2069 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2070 break;
2072 case VECTOR_TYPE:
2073 if (abi_version_at_least (4))
2075 write_string ("Dv");
2076 /* Non-constant vector size would be encoded with
2077 _ expression, but we don't support that yet. */
2078 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2079 write_char ('_');
2081 else
2082 write_string ("U8__vector");
2083 if (abi_version_crosses (4))
2084 G.need_abi_warning = 1;
2085 write_type (TREE_TYPE (type));
2086 break;
2088 case TYPE_PACK_EXPANSION:
2089 write_string ("Dp");
2090 write_type (PACK_EXPANSION_PATTERN (type));
2091 break;
2093 case DECLTYPE_TYPE:
2094 /* These shouldn't make it into mangling. */
2095 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2096 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2098 /* In ABI <5, we stripped decltype of a plain decl. */
2099 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2101 tree expr = DECLTYPE_TYPE_EXPR (type);
2102 tree etype = NULL_TREE;
2103 switch (TREE_CODE (expr))
2105 case VAR_DECL:
2106 case PARM_DECL:
2107 case RESULT_DECL:
2108 case FUNCTION_DECL:
2109 case CONST_DECL:
2110 case TEMPLATE_PARM_INDEX:
2111 etype = TREE_TYPE (expr);
2112 break;
2114 default:
2115 break;
2118 if (etype && !type_uses_auto (etype))
2120 if (abi_version_crosses (5))
2121 G.need_abi_warning = 1;
2122 if (!abi_version_at_least (5))
2124 write_type (etype);
2125 return;
2130 write_char ('D');
2131 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2132 write_char ('t');
2133 else
2134 write_char ('T');
2135 ++cp_unevaluated_operand;
2136 write_expression (DECLTYPE_TYPE_EXPR (type));
2137 --cp_unevaluated_operand;
2138 write_char ('E');
2139 break;
2141 case NULLPTR_TYPE:
2142 write_string ("Dn");
2143 if (abi_version_at_least (7))
2144 ++is_builtin_type;
2145 if (abi_version_crosses (7))
2146 G.need_abi_warning = 1;
2147 break;
2149 case TYPEOF_TYPE:
2150 sorry ("mangling typeof, use decltype instead");
2151 break;
2153 case UNDERLYING_TYPE:
2154 sorry ("mangling __underlying_type");
2155 break;
2157 case LANG_TYPE:
2158 /* fall through. */
2160 default:
2161 gcc_unreachable ();
2166 /* Types other than builtin types are substitution candidates. */
2167 if (!is_builtin_type)
2168 add_substitution (type);
2171 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2172 CV-qualifiers written for TYPE.
2174 <CV-qualifiers> ::= [r] [V] [K] */
2176 static int
2177 write_CV_qualifiers_for_type (const tree type)
2179 int num_qualifiers = 0;
2181 /* The order is specified by:
2183 "In cases where multiple order-insensitive qualifiers are
2184 present, they should be ordered 'K' (closest to the base type),
2185 'V', 'r', and 'U' (farthest from the base type) ..."
2187 Note that we do not use cp_type_quals below; given "const
2188 int[3]", the "const" is emitted with the "int", not with the
2189 array. */
2190 cp_cv_quals quals = TYPE_QUALS (type);
2192 if (quals & TYPE_QUAL_RESTRICT)
2194 write_char ('r');
2195 ++num_qualifiers;
2197 if (quals & TYPE_QUAL_VOLATILE)
2199 write_char ('V');
2200 ++num_qualifiers;
2202 if (quals & TYPE_QUAL_CONST)
2204 write_char ('K');
2205 ++num_qualifiers;
2208 return num_qualifiers;
2211 /* Non-terminal <builtin-type>.
2213 <builtin-type> ::= v # void
2214 ::= b # bool
2215 ::= w # wchar_t
2216 ::= c # char
2217 ::= a # signed char
2218 ::= h # unsigned char
2219 ::= s # short
2220 ::= t # unsigned short
2221 ::= i # int
2222 ::= j # unsigned int
2223 ::= l # long
2224 ::= m # unsigned long
2225 ::= x # long long, __int64
2226 ::= y # unsigned long long, __int64
2227 ::= n # __int128
2228 ::= o # unsigned __int128
2229 ::= f # float
2230 ::= d # double
2231 ::= e # long double, __float80
2232 ::= g # __float128 [not supported]
2233 ::= u <source-name> # vendor extended type */
2235 static void
2236 write_builtin_type (tree type)
2238 if (TYPE_CANONICAL (type))
2239 type = TYPE_CANONICAL (type);
2241 switch (TREE_CODE (type))
2243 case VOID_TYPE:
2244 write_char ('v');
2245 break;
2247 case BOOLEAN_TYPE:
2248 write_char ('b');
2249 break;
2251 case INTEGER_TYPE:
2252 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2253 isn't in integer_type_nodes. */
2254 if (type == wchar_type_node)
2255 write_char ('w');
2256 else if (type == char16_type_node)
2257 write_string ("Ds");
2258 else if (type == char32_type_node)
2259 write_string ("Di");
2260 else if (TYPE_FOR_JAVA (type))
2261 write_java_integer_type_codes (type);
2262 else
2264 size_t itk;
2265 /* Assume TYPE is one of the shared integer type nodes. Find
2266 it in the array of these nodes. */
2267 iagain:
2268 for (itk = 0; itk < itk_none; ++itk)
2269 if (integer_types[itk] != NULL_TREE
2270 && integer_type_codes[itk] != '\0'
2271 && type == integer_types[itk])
2273 /* Print the corresponding single-letter code. */
2274 write_char (integer_type_codes[itk]);
2275 break;
2278 if (itk == itk_none)
2280 tree t = c_common_type_for_mode (TYPE_MODE (type),
2281 TYPE_UNSIGNED (type));
2282 if (type != t)
2284 type = t;
2285 goto iagain;
2288 if (TYPE_PRECISION (type) == 128)
2289 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2290 else
2292 /* Allow for cases where TYPE is not one of the shared
2293 integer type nodes and write a "vendor extended builtin
2294 type" with a name the form intN or uintN, respectively.
2295 Situations like this can happen if you have an
2296 __attribute__((__mode__(__SI__))) type and use exotic
2297 switches like '-mint8' on AVR. Of course, this is
2298 undefined by the C++ ABI (and '-mint8' is not even
2299 Standard C conforming), but when using such special
2300 options you're pretty much in nowhere land anyway. */
2301 const char *prefix;
2302 char prec[11]; /* up to ten digits for an unsigned */
2304 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2305 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2306 write_char ('u'); /* "vendor extended builtin type" */
2307 write_unsigned_number (strlen (prefix) + strlen (prec));
2308 write_string (prefix);
2309 write_string (prec);
2313 break;
2315 case REAL_TYPE:
2316 if (type == float_type_node
2317 || type == java_float_type_node)
2318 write_char ('f');
2319 else if (type == double_type_node
2320 || type == java_double_type_node)
2321 write_char ('d');
2322 else if (type == long_double_type_node)
2323 write_char ('e');
2324 else if (type == dfloat32_type_node)
2325 write_string ("Df");
2326 else if (type == dfloat64_type_node)
2327 write_string ("Dd");
2328 else if (type == dfloat128_type_node)
2329 write_string ("De");
2330 else
2331 gcc_unreachable ();
2332 break;
2334 case FIXED_POINT_TYPE:
2335 write_string ("DF");
2336 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2337 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2338 if (type == fract_type_node
2339 || type == sat_fract_type_node
2340 || type == accum_type_node
2341 || type == sat_accum_type_node)
2342 write_char ('i');
2343 else if (type == unsigned_fract_type_node
2344 || type == sat_unsigned_fract_type_node
2345 || type == unsigned_accum_type_node
2346 || type == sat_unsigned_accum_type_node)
2347 write_char ('j');
2348 else if (type == short_fract_type_node
2349 || type == sat_short_fract_type_node
2350 || type == short_accum_type_node
2351 || type == sat_short_accum_type_node)
2352 write_char ('s');
2353 else if (type == unsigned_short_fract_type_node
2354 || type == sat_unsigned_short_fract_type_node
2355 || type == unsigned_short_accum_type_node
2356 || type == sat_unsigned_short_accum_type_node)
2357 write_char ('t');
2358 else if (type == long_fract_type_node
2359 || type == sat_long_fract_type_node
2360 || type == long_accum_type_node
2361 || type == sat_long_accum_type_node)
2362 write_char ('l');
2363 else if (type == unsigned_long_fract_type_node
2364 || type == sat_unsigned_long_fract_type_node
2365 || type == unsigned_long_accum_type_node
2366 || type == sat_unsigned_long_accum_type_node)
2367 write_char ('m');
2368 else if (type == long_long_fract_type_node
2369 || type == sat_long_long_fract_type_node
2370 || type == long_long_accum_type_node
2371 || type == sat_long_long_accum_type_node)
2372 write_char ('x');
2373 else if (type == unsigned_long_long_fract_type_node
2374 || type == sat_unsigned_long_long_fract_type_node
2375 || type == unsigned_long_long_accum_type_node
2376 || type == sat_unsigned_long_long_accum_type_node)
2377 write_char ('y');
2378 else
2379 sorry ("mangling unknown fixed point type");
2380 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2381 if (TYPE_SATURATING (type))
2382 write_char ('s');
2383 else
2384 write_char ('n');
2385 break;
2387 default:
2388 gcc_unreachable ();
2392 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2393 METHOD_TYPE. The return type is mangled before the parameter
2394 types.
2396 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2398 static void
2399 write_function_type (const tree type)
2401 MANGLE_TRACE_TREE ("function-type", type);
2403 /* For a pointer to member function, the function type may have
2404 cv-qualifiers, indicating the quals for the artificial 'this'
2405 parameter. */
2406 if (TREE_CODE (type) == METHOD_TYPE)
2408 /* The first parameter must be a POINTER_TYPE pointing to the
2409 `this' parameter. */
2410 tree this_type = class_of_this_parm (type);
2411 write_CV_qualifiers_for_type (this_type);
2414 write_char ('F');
2415 /* We don't track whether or not a type is `extern "C"'. Note that
2416 you can have an `extern "C"' function that does not have
2417 `extern "C"' type, and vice versa:
2419 extern "C" typedef void function_t();
2420 function_t f; // f has C++ linkage, but its type is
2421 // `extern "C"'
2423 typedef void function_t();
2424 extern "C" function_t f; // Vice versa.
2426 See [dcl.link]. */
2427 write_bare_function_type (type, /*include_return_type_p=*/1,
2428 /*decl=*/NULL);
2429 if (FUNCTION_REF_QUALIFIED (type))
2431 if (FUNCTION_RVALUE_QUALIFIED (type))
2432 write_char ('O');
2433 else
2434 write_char ('R');
2436 write_char ('E');
2439 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2440 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2441 is mangled before the parameter types. If non-NULL, DECL is
2442 FUNCTION_DECL for the function whose type is being emitted.
2444 If DECL is a member of a Java type, then a literal 'J'
2445 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2446 were nonzero.
2448 <bare-function-type> ::= [J]</signature/ type>+ */
2450 static void
2451 write_bare_function_type (const tree type, const int include_return_type_p,
2452 const tree decl)
2454 int java_method_p;
2456 MANGLE_TRACE_TREE ("bare-function-type", type);
2458 /* Detect Java methods and emit special encoding. */
2459 if (decl != NULL
2460 && DECL_FUNCTION_MEMBER_P (decl)
2461 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2462 && !DECL_CONSTRUCTOR_P (decl)
2463 && !DECL_DESTRUCTOR_P (decl)
2464 && !DECL_CONV_FN_P (decl))
2466 java_method_p = 1;
2467 write_char ('J');
2469 else
2471 java_method_p = 0;
2474 /* Mangle the return type, if requested. */
2475 if (include_return_type_p || java_method_p)
2476 write_type (TREE_TYPE (type));
2478 /* Now mangle the types of the arguments. */
2479 ++G.parm_depth;
2480 write_method_parms (TYPE_ARG_TYPES (type),
2481 TREE_CODE (type) == METHOD_TYPE,
2482 decl);
2483 --G.parm_depth;
2486 /* Write the mangled representation of a method parameter list of
2487 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2488 considered a non-static method, and the this parameter is omitted.
2489 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2490 parameters are being emitted. */
2492 static void
2493 write_method_parms (tree parm_types, const int method_p, const tree decl)
2495 tree first_parm_type;
2496 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2498 /* Assume this parameter type list is variable-length. If it ends
2499 with a void type, then it's not. */
2500 int varargs_p = 1;
2502 /* If this is a member function, skip the first arg, which is the
2503 this pointer.
2504 "Member functions do not encode the type of their implicit this
2505 parameter."
2507 Similarly, there's no need to mangle artificial parameters, like
2508 the VTT parameters for constructors and destructors. */
2509 if (method_p)
2511 parm_types = TREE_CHAIN (parm_types);
2512 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2514 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2516 parm_types = TREE_CHAIN (parm_types);
2517 parm_decl = DECL_CHAIN (parm_decl);
2521 for (first_parm_type = parm_types;
2522 parm_types;
2523 parm_types = TREE_CHAIN (parm_types))
2525 tree parm = TREE_VALUE (parm_types);
2526 if (parm == void_type_node)
2528 /* "Empty parameter lists, whether declared as () or
2529 conventionally as (void), are encoded with a void parameter
2530 (v)." */
2531 if (parm_types == first_parm_type)
2532 write_type (parm);
2533 /* If the parm list is terminated with a void type, it's
2534 fixed-length. */
2535 varargs_p = 0;
2536 /* A void type better be the last one. */
2537 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2539 else
2540 write_type (parm);
2543 if (varargs_p)
2544 /* <builtin-type> ::= z # ellipsis */
2545 write_char ('z');
2548 /* <class-enum-type> ::= <name> */
2550 static void
2551 write_class_enum_type (const tree type)
2553 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2556 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2557 arguments.
2559 <template-args> ::= I <template-arg>* E */
2561 static void
2562 write_template_args (tree args)
2564 int i;
2565 int length = 0;
2567 MANGLE_TRACE_TREE ("template-args", args);
2569 write_char ('I');
2571 if (args)
2572 length = TREE_VEC_LENGTH (args);
2574 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2576 /* We have nested template args. We want the innermost template
2577 argument list. */
2578 args = TREE_VEC_ELT (args, length - 1);
2579 length = TREE_VEC_LENGTH (args);
2581 for (i = 0; i < length; ++i)
2582 write_template_arg (TREE_VEC_ELT (args, i));
2584 write_char ('E');
2587 /* Write out the
2588 <unqualified-name>
2589 <unqualified-name> <template-args>
2590 part of SCOPE_REF or COMPONENT_REF mangling. */
2592 static void
2593 write_member_name (tree member)
2595 if (identifier_p (member))
2596 write_unqualified_id (member);
2597 else if (DECL_P (member))
2598 write_unqualified_name (member);
2599 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2601 tree name = TREE_OPERAND (member, 0);
2602 if (TREE_CODE (name) == OVERLOAD)
2603 name = OVL_FUNCTION (name);
2604 write_member_name (name);
2605 write_template_args (TREE_OPERAND (member, 1));
2607 else
2608 write_expression (member);
2611 /* <expression> ::= <unary operator-name> <expression>
2612 ::= <binary operator-name> <expression> <expression>
2613 ::= <expr-primary>
2615 <expr-primary> ::= <template-param>
2616 ::= L <type> <value number> E # literal
2617 ::= L <mangled-name> E # external name
2618 ::= st <type> # sizeof
2619 ::= sr <type> <unqualified-name> # dependent name
2620 ::= sr <type> <unqualified-name> <template-args> */
2622 static void
2623 write_expression (tree expr)
2625 enum tree_code code = TREE_CODE (expr);
2627 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2628 is converted (via qualification conversions) to another
2629 type. */
2630 while (TREE_CODE (expr) == NOP_EXPR
2631 /* Parentheses aren't mangled. */
2632 || code == PAREN_EXPR
2633 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2635 expr = TREE_OPERAND (expr, 0);
2636 code = TREE_CODE (expr);
2639 if (code == BASELINK
2640 && (!type_unknown_p (expr)
2641 || !BASELINK_QUALIFIED_P (expr)))
2643 expr = BASELINK_FUNCTIONS (expr);
2644 code = TREE_CODE (expr);
2647 /* Handle pointers-to-members by making them look like expression
2648 nodes. */
2649 if (code == PTRMEM_CST)
2651 expr = build_nt (ADDR_EXPR,
2652 build_qualified_name (/*type=*/NULL_TREE,
2653 PTRMEM_CST_CLASS (expr),
2654 PTRMEM_CST_MEMBER (expr),
2655 /*template_p=*/false));
2656 code = TREE_CODE (expr);
2659 /* Handle template parameters. */
2660 if (code == TEMPLATE_TYPE_PARM
2661 || code == TEMPLATE_TEMPLATE_PARM
2662 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2663 || code == TEMPLATE_PARM_INDEX)
2664 write_template_param (expr);
2665 /* Handle literals. */
2666 else if (TREE_CODE_CLASS (code) == tcc_constant
2667 || code == CONST_DECL)
2668 write_template_arg_literal (expr);
2669 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2671 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2672 write_string ("fpT");
2674 else if (code == PARM_DECL)
2676 /* A function parameter used in a late-specified return type. */
2677 int index = DECL_PARM_INDEX (expr);
2678 int level = DECL_PARM_LEVEL (expr);
2679 int delta = G.parm_depth - level + 1;
2680 gcc_assert (index >= 1);
2681 write_char ('f');
2682 if (delta != 0)
2684 if (abi_version_at_least (5))
2686 /* Let L be the number of function prototype scopes from the
2687 innermost one (in which the parameter reference occurs) up
2688 to (and including) the one containing the declaration of
2689 the referenced parameter. If the parameter declaration
2690 clause of the innermost function prototype scope has been
2691 completely seen, it is not counted (in that case -- which
2692 is perhaps the most common -- L can be zero). */
2693 write_char ('L');
2694 write_unsigned_number (delta - 1);
2696 if (abi_version_crosses (5))
2697 G.need_abi_warning = true;
2699 write_char ('p');
2700 write_compact_number (index - 1);
2702 else if (DECL_P (expr))
2704 write_char ('L');
2705 write_mangled_name (expr, false);
2706 write_char ('E');
2708 else if (TREE_CODE (expr) == SIZEOF_EXPR
2709 && SIZEOF_EXPR_TYPE_P (expr))
2711 write_string ("st");
2712 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2714 else if (TREE_CODE (expr) == SIZEOF_EXPR
2715 && TYPE_P (TREE_OPERAND (expr, 0)))
2717 write_string ("st");
2718 write_type (TREE_OPERAND (expr, 0));
2720 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2721 && TYPE_P (TREE_OPERAND (expr, 0)))
2723 write_string ("at");
2724 write_type (TREE_OPERAND (expr, 0));
2726 else if (code == SCOPE_REF
2727 || code == BASELINK)
2729 tree scope, member;
2730 if (code == SCOPE_REF)
2732 scope = TREE_OPERAND (expr, 0);
2733 member = TREE_OPERAND (expr, 1);
2735 else
2737 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2738 member = BASELINK_FUNCTIONS (expr);
2741 /* If the MEMBER is a real declaration, then the qualifying
2742 scope was not dependent. Ideally, we would not have a
2743 SCOPE_REF in those cases, but sometimes we do. If the second
2744 argument is a DECL, then the name must not have been
2745 dependent. */
2746 if (DECL_P (member))
2747 write_expression (member);
2748 else
2750 write_string ("sr");
2751 write_type (scope);
2752 write_member_name (member);
2755 else if (INDIRECT_REF_P (expr)
2756 && TREE_TYPE (TREE_OPERAND (expr, 0))
2757 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2759 write_expression (TREE_OPERAND (expr, 0));
2761 else if (identifier_p (expr))
2763 /* An operator name appearing as a dependent name needs to be
2764 specially marked to disambiguate between a use of the operator
2765 name and a use of the operator in an expression. */
2766 if (IDENTIFIER_OPNAME_P (expr))
2767 write_string ("on");
2768 write_unqualified_id (expr);
2770 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2772 tree fn = TREE_OPERAND (expr, 0);
2773 if (is_overloaded_fn (fn))
2774 fn = DECL_NAME (get_first_fn (fn));
2775 if (IDENTIFIER_OPNAME_P (fn))
2776 write_string ("on");
2777 write_unqualified_id (fn);
2778 write_template_args (TREE_OPERAND (expr, 1));
2780 else if (TREE_CODE (expr) == MODOP_EXPR)
2782 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2783 const char *name = (assignment_operator_name_info[(int) subop]
2784 .mangled_name);
2785 write_string (name);
2786 write_expression (TREE_OPERAND (expr, 0));
2787 write_expression (TREE_OPERAND (expr, 2));
2789 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2791 /* ::= [gs] nw <expression>* _ <type> E
2792 ::= [gs] nw <expression>* _ <type> <initializer>
2793 ::= [gs] na <expression>* _ <type> E
2794 ::= [gs] na <expression>* _ <type> <initializer>
2795 <initializer> ::= pi <expression>* E */
2796 tree placement = TREE_OPERAND (expr, 0);
2797 tree type = TREE_OPERAND (expr, 1);
2798 tree nelts = TREE_OPERAND (expr, 2);
2799 tree init = TREE_OPERAND (expr, 3);
2800 tree t;
2802 gcc_assert (code == NEW_EXPR);
2803 if (TREE_OPERAND (expr, 2))
2804 code = VEC_NEW_EXPR;
2806 if (NEW_EXPR_USE_GLOBAL (expr))
2807 write_string ("gs");
2809 write_string (operator_name_info[(int) code].mangled_name);
2811 for (t = placement; t; t = TREE_CHAIN (t))
2812 write_expression (TREE_VALUE (t));
2814 write_char ('_');
2816 if (nelts)
2818 tree domain;
2819 ++processing_template_decl;
2820 domain = compute_array_index_type (NULL_TREE, nelts,
2821 tf_warning_or_error);
2822 type = build_cplus_array_type (type, domain);
2823 --processing_template_decl;
2825 write_type (type);
2827 if (init && TREE_CODE (init) == TREE_LIST
2828 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2829 write_expression (TREE_VALUE (init));
2830 else
2832 if (init)
2833 write_string ("pi");
2834 if (init && init != void_node)
2835 for (t = init; t; t = TREE_CHAIN (t))
2836 write_expression (TREE_VALUE (t));
2837 write_char ('E');
2840 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2842 gcc_assert (code == DELETE_EXPR);
2843 if (DELETE_EXPR_USE_VEC (expr))
2844 code = VEC_DELETE_EXPR;
2846 if (DELETE_EXPR_USE_GLOBAL (expr))
2847 write_string ("gs");
2849 write_string (operator_name_info[(int) code].mangled_name);
2851 write_expression (TREE_OPERAND (expr, 0));
2853 else if (code == THROW_EXPR)
2855 tree op = TREE_OPERAND (expr, 0);
2856 if (op)
2858 write_string ("tw");
2859 write_expression (op);
2861 else
2862 write_string ("tr");
2864 else if (code == CONSTRUCTOR)
2866 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2867 unsigned i; tree val;
2869 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2870 write_string ("il");
2871 else
2873 write_string ("tl");
2874 write_type (TREE_TYPE (expr));
2876 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2877 write_expression (val);
2878 write_char ('E');
2880 else if (dependent_name (expr))
2882 write_unqualified_id (dependent_name (expr));
2884 else
2886 int i, len;
2887 const char *name;
2889 /* When we bind a variable or function to a non-type template
2890 argument with reference type, we create an ADDR_EXPR to show
2891 the fact that the entity's address has been taken. But, we
2892 don't actually want to output a mangling code for the `&'. */
2893 if (TREE_CODE (expr) == ADDR_EXPR
2894 && TREE_TYPE (expr)
2895 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2897 expr = TREE_OPERAND (expr, 0);
2898 if (DECL_P (expr))
2900 write_expression (expr);
2901 return;
2904 code = TREE_CODE (expr);
2907 if (code == COMPONENT_REF)
2909 tree ob = TREE_OPERAND (expr, 0);
2911 if (TREE_CODE (ob) == ARROW_EXPR)
2913 write_string (operator_name_info[(int)code].mangled_name);
2914 ob = TREE_OPERAND (ob, 0);
2915 write_expression (ob);
2917 else if (!is_dummy_object (ob))
2919 write_string ("dt");
2920 write_expression (ob);
2922 /* else, for a non-static data member with no associated object (in
2923 unevaluated context), use the unresolved-name mangling. */
2925 write_member_name (TREE_OPERAND (expr, 1));
2926 return;
2929 /* If it wasn't any of those, recursively expand the expression. */
2930 name = operator_name_info[(int) code].mangled_name;
2932 /* We used to mangle const_cast and static_cast like a C cast. */
2933 if (code == CONST_CAST_EXPR
2934 || code == STATIC_CAST_EXPR)
2936 if (abi_version_crosses (6))
2937 G.need_abi_warning = 1;
2938 if (!abi_version_at_least (6))
2939 name = operator_name_info[CAST_EXPR].mangled_name;
2942 if (name == NULL)
2944 switch (code)
2946 case TRAIT_EXPR:
2947 error ("use of built-in trait %qE in function signature; "
2948 "use library traits instead", expr);
2949 break;
2951 default:
2952 sorry ("mangling %C", code);
2953 break;
2955 return;
2957 else
2958 write_string (name);
2960 switch (code)
2962 case CALL_EXPR:
2964 tree fn = CALL_EXPR_FN (expr);
2966 if (TREE_CODE (fn) == ADDR_EXPR)
2967 fn = TREE_OPERAND (fn, 0);
2969 /* Mangle a dependent name as the name, not whatever happens to
2970 be the first function in the overload set. */
2971 if ((TREE_CODE (fn) == FUNCTION_DECL
2972 || TREE_CODE (fn) == OVERLOAD)
2973 && type_dependent_expression_p_push (expr))
2974 fn = DECL_NAME (get_first_fn (fn));
2976 write_expression (fn);
2979 for (i = 0; i < call_expr_nargs (expr); ++i)
2980 write_expression (CALL_EXPR_ARG (expr, i));
2981 write_char ('E');
2982 break;
2984 case CAST_EXPR:
2985 write_type (TREE_TYPE (expr));
2986 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2987 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2988 else
2990 tree args = TREE_OPERAND (expr, 0);
2991 write_char ('_');
2992 for (; args; args = TREE_CHAIN (args))
2993 write_expression (TREE_VALUE (args));
2994 write_char ('E');
2996 break;
2998 case DYNAMIC_CAST_EXPR:
2999 case REINTERPRET_CAST_EXPR:
3000 case STATIC_CAST_EXPR:
3001 case CONST_CAST_EXPR:
3002 write_type (TREE_TYPE (expr));
3003 write_expression (TREE_OPERAND (expr, 0));
3004 break;
3006 case PREINCREMENT_EXPR:
3007 case PREDECREMENT_EXPR:
3008 if (abi_version_at_least (6))
3009 write_char ('_');
3010 if (abi_version_crosses (6))
3011 G.need_abi_warning = 1;
3012 /* Fall through. */
3014 default:
3015 /* In the middle-end, some expressions have more operands than
3016 they do in templates (and mangling). */
3017 len = cp_tree_operand_length (expr);
3019 for (i = 0; i < len; ++i)
3021 tree operand = TREE_OPERAND (expr, i);
3022 /* As a GNU extension, the middle operand of a
3023 conditional may be omitted. Since expression
3024 manglings are supposed to represent the input token
3025 stream, there's no good way to mangle such an
3026 expression without extending the C++ ABI. */
3027 if (code == COND_EXPR && i == 1 && !operand)
3029 error ("omitted middle operand to %<?:%> operand "
3030 "cannot be mangled");
3031 continue;
3033 write_expression (operand);
3039 /* Literal subcase of non-terminal <template-arg>.
3041 "Literal arguments, e.g. "A<42L>", are encoded with their type
3042 and value. Negative integer values are preceded with "n"; for
3043 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3044 encoded as 0, true as 1." */
3046 static void
3047 write_template_arg_literal (const tree value)
3049 write_char ('L');
3050 write_type (TREE_TYPE (value));
3052 /* Write a null member pointer value as (type)0, regardless of its
3053 real representation. */
3054 if (null_member_pointer_value_p (value))
3055 write_integer_cst (integer_zero_node);
3056 else
3057 switch (TREE_CODE (value))
3059 case CONST_DECL:
3060 write_integer_cst (DECL_INITIAL (value));
3061 break;
3063 case INTEGER_CST:
3064 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3065 || integer_zerop (value) || integer_onep (value));
3066 write_integer_cst (value);
3067 break;
3069 case REAL_CST:
3070 write_real_cst (value);
3071 break;
3073 case COMPLEX_CST:
3074 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3075 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3077 write_integer_cst (TREE_REALPART (value));
3078 write_char ('_');
3079 write_integer_cst (TREE_IMAGPART (value));
3081 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3082 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3084 write_real_cst (TREE_REALPART (value));
3085 write_char ('_');
3086 write_real_cst (TREE_IMAGPART (value));
3088 else
3089 gcc_unreachable ();
3090 break;
3092 case STRING_CST:
3093 sorry ("string literal in function template signature");
3094 break;
3096 default:
3097 gcc_unreachable ();
3100 write_char ('E');
3103 /* Non-terminal <template-arg>.
3105 <template-arg> ::= <type> # type
3106 ::= L <type> </value/ number> E # literal
3107 ::= LZ <name> E # external name
3108 ::= X <expression> E # expression */
3110 static void
3111 write_template_arg (tree node)
3113 enum tree_code code = TREE_CODE (node);
3115 MANGLE_TRACE_TREE ("template-arg", node);
3117 /* A template template parameter's argument list contains TREE_LIST
3118 nodes of which the value field is the actual argument. */
3119 if (code == TREE_LIST)
3121 node = TREE_VALUE (node);
3122 /* If it's a decl, deal with its type instead. */
3123 if (DECL_P (node))
3125 node = TREE_TYPE (node);
3126 code = TREE_CODE (node);
3130 if (REFERENCE_REF_P (node))
3131 node = TREE_OPERAND (node, 0);
3132 if (TREE_CODE (node) == NOP_EXPR
3133 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3135 /* Template parameters can be of reference type. To maintain
3136 internal consistency, such arguments use a conversion from
3137 address of object to reference type. */
3138 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3139 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3142 if (TREE_CODE (node) == BASELINK
3143 && !type_unknown_p (node))
3145 if (abi_version_at_least (6))
3146 node = BASELINK_FUNCTIONS (node);
3147 if (abi_version_crosses (6))
3148 /* We wrongly wrapped a class-scope function in X/E. */
3149 G.need_abi_warning = 1;
3152 if (ARGUMENT_PACK_P (node))
3154 /* Expand the template argument pack. */
3155 tree args = ARGUMENT_PACK_ARGS (node);
3156 int i, length = TREE_VEC_LENGTH (args);
3157 if (abi_version_at_least (6))
3158 write_char ('J');
3159 else
3160 write_char ('I');
3161 if (abi_version_crosses (6))
3162 G.need_abi_warning = 1;
3163 for (i = 0; i < length; ++i)
3164 write_template_arg (TREE_VEC_ELT (args, i));
3165 write_char ('E');
3167 else if (TYPE_P (node))
3168 write_type (node);
3169 else if (code == TEMPLATE_DECL)
3170 /* A template appearing as a template arg is a template template arg. */
3171 write_template_template_arg (node);
3172 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3173 || code == CONST_DECL
3174 || null_member_pointer_value_p (node))
3175 write_template_arg_literal (node);
3176 else if (DECL_P (node))
3178 write_char ('L');
3179 /* Until ABI version 3, the underscore before the mangled name
3180 was incorrectly omitted. */
3181 if (!abi_version_at_least (3))
3182 write_char ('Z');
3183 else
3184 write_string ("_Z");
3185 if (abi_version_crosses (3))
3186 G.need_abi_warning = 1;
3187 write_encoding (node);
3188 write_char ('E');
3190 else
3192 /* Template arguments may be expressions. */
3193 write_char ('X');
3194 write_expression (node);
3195 write_char ('E');
3199 /* <template-template-arg>
3200 ::= <name>
3201 ::= <substitution> */
3203 static void
3204 write_template_template_arg (const tree decl)
3206 MANGLE_TRACE_TREE ("template-template-arg", decl);
3208 if (find_substitution (decl))
3209 return;
3210 write_name (decl, /*ignore_local_scope=*/0);
3211 add_substitution (decl);
3215 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3217 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3218 ::= A <expression> _ </element/ type>
3220 "Array types encode the dimension (number of elements) and the
3221 element type. For variable length arrays, the dimension (but not
3222 the '_' separator) is omitted." */
3224 static void
3225 write_array_type (const tree type)
3227 write_char ('A');
3228 if (TYPE_DOMAIN (type))
3230 tree index_type;
3231 tree max;
3233 index_type = TYPE_DOMAIN (type);
3234 /* The INDEX_TYPE gives the upper and lower bounds of the
3235 array. */
3236 max = TYPE_MAX_VALUE (index_type);
3237 if (TREE_CODE (max) == INTEGER_CST)
3239 /* The ABI specifies that we should mangle the number of
3240 elements in the array, not the largest allowed index. */
3241 offset_int wmax = wi::to_offset (max) + 1;
3242 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3243 number of elements as zero. */
3244 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3245 gcc_assert (wi::fits_uhwi_p (wmax));
3246 write_unsigned_number (wmax.to_uhwi ());
3248 else
3250 max = TREE_OPERAND (max, 0);
3251 write_expression (max);
3255 write_char ('_');
3256 write_type (TREE_TYPE (type));
3259 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3260 variables. TYPE is a pointer-to-member POINTER_TYPE.
3262 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3264 static void
3265 write_pointer_to_member_type (const tree type)
3267 write_char ('M');
3268 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3269 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3272 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3273 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3274 TEMPLATE_PARM_INDEX.
3276 <template-param> ::= T </parameter/ number> _ */
3278 static void
3279 write_template_param (const tree parm)
3281 int parm_index;
3283 MANGLE_TRACE_TREE ("template-parm", parm);
3285 switch (TREE_CODE (parm))
3287 case TEMPLATE_TYPE_PARM:
3288 case TEMPLATE_TEMPLATE_PARM:
3289 case BOUND_TEMPLATE_TEMPLATE_PARM:
3290 parm_index = TEMPLATE_TYPE_IDX (parm);
3291 break;
3293 case TEMPLATE_PARM_INDEX:
3294 parm_index = TEMPLATE_PARM_IDX (parm);
3295 break;
3297 default:
3298 gcc_unreachable ();
3301 write_char ('T');
3302 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3303 earliest template param denoted by `_'. */
3304 write_compact_number (parm_index);
3307 /* <template-template-param>
3308 ::= <template-param>
3309 ::= <substitution> */
3311 static void
3312 write_template_template_param (const tree parm)
3314 tree templ = NULL_TREE;
3316 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3317 template template parameter. The substitution candidate here is
3318 only the template. */
3319 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3321 templ
3322 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3323 if (find_substitution (templ))
3324 return;
3327 /* <template-param> encodes only the template parameter position,
3328 not its template arguments, which is fine here. */
3329 write_template_param (parm);
3330 if (templ)
3331 add_substitution (templ);
3334 /* Non-terminal <substitution>.
3336 <substitution> ::= S <seq-id> _
3337 ::= S_ */
3339 static void
3340 write_substitution (const int seq_id)
3342 MANGLE_TRACE ("substitution", "");
3344 write_char ('S');
3345 if (seq_id > 0)
3346 write_number (seq_id - 1, /*unsigned=*/1, 36);
3347 write_char ('_');
3350 /* Start mangling ENTITY. */
3352 static inline void
3353 start_mangling (const tree entity)
3355 G.entity = entity;
3356 G.need_abi_warning = false;
3357 obstack_free (&name_obstack, name_base);
3358 mangle_obstack = &name_obstack;
3359 name_base = obstack_alloc (&name_obstack, 0);
3362 /* Done with mangling. If WARN is true, and the name of G.entity will
3363 be mangled differently in a future version of the ABI, issue a
3364 warning. */
3366 static void
3367 finish_mangling_internal (void)
3369 /* Clear all the substitutions. */
3370 vec_safe_truncate (G.substitutions, 0);
3372 /* Null-terminate the string. */
3373 write_char ('\0');
3377 /* Like finish_mangling_internal, but return the mangled string. */
3379 static inline const char *
3380 finish_mangling (void)
3382 finish_mangling_internal ();
3383 return (const char *) obstack_finish (mangle_obstack);
3386 /* Like finish_mangling_internal, but return an identifier. */
3388 static tree
3389 finish_mangling_get_identifier (void)
3391 finish_mangling_internal ();
3392 /* Don't obstack_finish here, and the next start_mangling will
3393 remove the identifier. */
3394 return get_identifier ((const char *) obstack_base (mangle_obstack));
3397 /* Initialize data structures for mangling. */
3399 void
3400 init_mangle (void)
3402 gcc_obstack_init (&name_obstack);
3403 name_base = obstack_alloc (&name_obstack, 0);
3404 vec_alloc (G.substitutions, 0);
3406 /* Cache these identifiers for quick comparison when checking for
3407 standard substitutions. */
3408 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3409 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3410 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3411 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3412 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3413 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3416 /* Generate the mangled name of DECL. */
3418 static tree
3419 mangle_decl_string (const tree decl)
3421 tree result;
3422 location_t saved_loc = input_location;
3423 tree saved_fn = NULL_TREE;
3424 bool template_p = false;
3426 /* We shouldn't be trying to mangle an uninstantiated template. */
3427 gcc_assert (!type_dependent_expression_p (decl));
3429 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3431 struct tinst_level *tl = current_instantiation ();
3432 if ((!tl || tl->decl != decl)
3433 && push_tinst_level (decl))
3435 template_p = true;
3436 saved_fn = current_function_decl;
3437 current_function_decl = NULL_TREE;
3440 input_location = DECL_SOURCE_LOCATION (decl);
3442 start_mangling (decl);
3444 if (TREE_CODE (decl) == TYPE_DECL)
3445 write_type (TREE_TYPE (decl));
3446 else
3447 write_mangled_name (decl, true);
3449 result = finish_mangling_get_identifier ();
3450 if (DEBUG_MANGLE)
3451 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3452 IDENTIFIER_POINTER (result));
3454 if (template_p)
3456 pop_tinst_level ();
3457 current_function_decl = saved_fn;
3459 input_location = saved_loc;
3461 return result;
3464 /* Return an identifier for the external mangled name of DECL. */
3466 static tree
3467 get_mangled_id (tree decl)
3469 tree id = mangle_decl_string (decl);
3470 return targetm.mangle_decl_assembler_name (decl, id);
3473 /* If DECL is a mangling alias, remove it from the symbol table and return
3474 true; otherwise return false. */
3476 bool
3477 maybe_remove_implicit_alias (tree decl)
3479 if (DECL_P (decl) && DECL_ARTIFICIAL (decl)
3480 && DECL_IGNORED_P (decl)
3481 && (TREE_CODE (decl) == FUNCTION_DECL
3482 || (TREE_CODE (decl) == VAR_DECL
3483 && TREE_STATIC (decl))))
3485 symtab_node *n = symtab_node::get (decl);
3486 if (n && n->cpp_implicit_alias)
3488 n->remove();
3489 return true;
3492 return false;
3495 /* Create an identifier for the external mangled name of DECL. */
3497 void
3498 mangle_decl (const tree decl)
3500 tree id;
3501 bool dep;
3503 /* Don't bother mangling uninstantiated templates. */
3504 ++processing_template_decl;
3505 if (TREE_CODE (decl) == TYPE_DECL)
3506 dep = dependent_type_p (TREE_TYPE (decl));
3507 else
3508 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3509 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3510 --processing_template_decl;
3511 if (dep)
3512 return;
3514 id = get_mangled_id (decl);
3515 SET_DECL_ASSEMBLER_NAME (decl, id);
3517 if (G.need_abi_warning
3518 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3519 && TREE_CODE (decl) != TYPE_DECL
3520 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3521 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3523 /* If the mangling will change in the future, emit an alias with the
3524 future mangled name for forward-compatibility. */
3525 int save_ver;
3526 tree id2;
3528 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3529 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3530 inform (DECL_SOURCE_LOCATION (decl), "a later -fabi-version= (or =0) "
3531 "avoids this error with a change in mangling");
3533 save_ver = flag_abi_version;
3534 flag_abi_version = flag_abi_compat_version;
3535 id2 = mangle_decl_string (decl);
3536 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3537 flag_abi_version = save_ver;
3539 if (id2 == id)
3540 return;
3542 if (warn_abi)
3544 if (flag_abi_compat_version != 0
3545 && abi_version_at_least (flag_abi_compat_version))
3546 warning (OPT_Wabi, "the mangled name of %q+D changed between "
3547 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3548 G.entity, flag_abi_compat_version, id2,
3549 flag_abi_version, id);
3550 else
3551 warning (OPT_Wabi, "the mangled name of %q+D changes between "
3552 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3553 G.entity, flag_abi_version, id,
3554 flag_abi_compat_version, id2);
3557 #ifdef ASM_OUTPUT_DEF
3558 /* If there's a declaration already using this mangled name,
3559 don't create a compatibility alias that conflicts. */
3560 if (IDENTIFIER_GLOBAL_VALUE (id2))
3561 return;
3563 struct cgraph_node *n = NULL;
3564 if (TREE_CODE (decl) == FUNCTION_DECL
3565 && !(n = cgraph_node::get (decl)))
3566 /* Don't create an alias to an unreferenced function. */
3567 return;
3569 tree alias = make_alias_for (decl, id2);
3570 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
3571 DECL_IGNORED_P (alias) = 1;
3572 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3573 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3574 if (vague_linkage_p (decl))
3575 DECL_WEAK (alias) = 1;
3576 if (TREE_CODE (decl) == FUNCTION_DECL)
3577 n->create_same_body_alias (alias, decl);
3578 else
3579 varpool_node::create_extra_name_alias (alias, decl);
3580 #endif
3584 /* Generate the mangled representation of TYPE. */
3586 const char *
3587 mangle_type_string (const tree type)
3589 const char *result;
3591 start_mangling (type);
3592 write_type (type);
3593 result = finish_mangling ();
3594 if (DEBUG_MANGLE)
3595 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3596 return result;
3599 /* Create an identifier for the mangled name of a special component
3600 for belonging to TYPE. CODE is the ABI-specified code for this
3601 component. */
3603 static tree
3604 mangle_special_for_type (const tree type, const char *code)
3606 tree result;
3608 /* We don't have an actual decl here for the special component, so
3609 we can't just process the <encoded-name>. Instead, fake it. */
3610 start_mangling (type);
3612 /* Start the mangling. */
3613 write_string ("_Z");
3614 write_string (code);
3616 /* Add the type. */
3617 write_type (type);
3618 result = finish_mangling_get_identifier ();
3620 if (DEBUG_MANGLE)
3621 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3622 IDENTIFIER_POINTER (result));
3624 return result;
3627 /* Create an identifier for the mangled representation of the typeinfo
3628 structure for TYPE. */
3630 tree
3631 mangle_typeinfo_for_type (const tree type)
3633 return mangle_special_for_type (type, "TI");
3636 /* Create an identifier for the mangled name of the NTBS containing
3637 the mangled name of TYPE. */
3639 tree
3640 mangle_typeinfo_string_for_type (const tree type)
3642 return mangle_special_for_type (type, "TS");
3645 /* Create an identifier for the mangled name of the vtable for TYPE. */
3647 tree
3648 mangle_vtbl_for_type (const tree type)
3650 return mangle_special_for_type (type, "TV");
3653 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3655 tree
3656 mangle_vtt_for_type (const tree type)
3658 return mangle_special_for_type (type, "TT");
3661 /* Return an identifier for a construction vtable group. TYPE is
3662 the most derived class in the hierarchy; BINFO is the base
3663 subobject for which this construction vtable group will be used.
3665 This mangling isn't part of the ABI specification; in the ABI
3666 specification, the vtable group is dumped in the same COMDAT as the
3667 main vtable, and is referenced only from that vtable, so it doesn't
3668 need an external name. For binary formats without COMDAT sections,
3669 though, we need external names for the vtable groups.
3671 We use the production
3673 <special-name> ::= CT <type> <offset number> _ <base type> */
3675 tree
3676 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3678 tree result;
3680 start_mangling (type);
3682 write_string ("_Z");
3683 write_string ("TC");
3684 write_type (type);
3685 write_integer_cst (BINFO_OFFSET (binfo));
3686 write_char ('_');
3687 write_type (BINFO_TYPE (binfo));
3689 result = finish_mangling_get_identifier ();
3690 if (DEBUG_MANGLE)
3691 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3692 IDENTIFIER_POINTER (result));
3693 return result;
3696 /* Mangle a this pointer or result pointer adjustment.
3698 <call-offset> ::= h <fixed offset number> _
3699 ::= v <fixed offset number> _ <virtual offset number> _ */
3701 static void
3702 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3704 write_char (virtual_offset ? 'v' : 'h');
3706 /* For either flavor, write the fixed offset. */
3707 write_integer_cst (fixed_offset);
3708 write_char ('_');
3710 /* For a virtual thunk, add the virtual offset. */
3711 if (virtual_offset)
3713 write_integer_cst (virtual_offset);
3714 write_char ('_');
3718 /* Return an identifier for the mangled name of a this-adjusting or
3719 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3720 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3721 is a virtual thunk, and it is the vtbl offset in
3722 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3723 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3724 thunk itself. A covariant thunk name always includes the adjustment
3725 for the this pointer, even if there is none.
3727 <special-name> ::= T <call-offset> <base encoding>
3728 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3729 <base encoding> */
3731 tree
3732 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3733 tree virtual_offset)
3735 tree result;
3737 start_mangling (fn_decl);
3739 write_string ("_Z");
3740 write_char ('T');
3742 if (!this_adjusting)
3744 /* Covariant thunk with no this adjustment */
3745 write_char ('c');
3746 mangle_call_offset (integer_zero_node, NULL_TREE);
3747 mangle_call_offset (fixed_offset, virtual_offset);
3749 else if (!DECL_THUNK_P (fn_decl))
3750 /* Plain this adjusting thunk. */
3751 mangle_call_offset (fixed_offset, virtual_offset);
3752 else
3754 /* This adjusting thunk to covariant thunk. */
3755 write_char ('c');
3756 mangle_call_offset (fixed_offset, virtual_offset);
3757 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3758 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3759 if (virtual_offset)
3760 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3761 mangle_call_offset (fixed_offset, virtual_offset);
3762 fn_decl = THUNK_TARGET (fn_decl);
3765 /* Scoped name. */
3766 write_encoding (fn_decl);
3768 result = finish_mangling_get_identifier ();
3769 if (DEBUG_MANGLE)
3770 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3771 return result;
3774 struct conv_type_hasher : ggc_hasher<tree>
3776 static hashval_t hash (tree);
3777 static bool equal (tree, tree);
3780 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3781 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3782 TYPE. */
3784 static GTY (()) hash_table<conv_type_hasher> *conv_type_names;
3786 /* Hash a node (VAL1) in the table. */
3788 hashval_t
3789 conv_type_hasher::hash (tree val)
3791 return (hashval_t) TYPE_UID (TREE_TYPE (val));
3794 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3796 bool
3797 conv_type_hasher::equal (tree val1, tree val2)
3799 return TREE_TYPE (val1) == val2;
3802 /* Return an identifier for the mangled unqualified name for a
3803 conversion operator to TYPE. This mangling is not specified by the
3804 ABI spec; it is only used internally. */
3806 tree
3807 mangle_conv_op_name_for_type (const tree type)
3809 tree *slot;
3810 tree identifier;
3812 if (type == error_mark_node)
3813 return error_mark_node;
3815 if (conv_type_names == NULL)
3816 conv_type_names = hash_table<conv_type_hasher>::create_ggc (31);
3818 slot = conv_type_names->find_slot_with_hash (type,
3819 (hashval_t) TYPE_UID (type),
3820 INSERT);
3821 identifier = *slot;
3822 if (!identifier)
3824 char buffer[64];
3826 /* Create a unique name corresponding to TYPE. */
3827 sprintf (buffer, "operator %lu",
3828 (unsigned long) conv_type_names->elements ());
3829 identifier = get_identifier (buffer);
3830 *slot = identifier;
3832 /* Hang TYPE off the identifier so it can be found easily later
3833 when performing conversions. */
3834 TREE_TYPE (identifier) = type;
3836 /* Set bits on the identifier so we know later it's a conversion. */
3837 IDENTIFIER_OPNAME_P (identifier) = 1;
3838 IDENTIFIER_TYPENAME_P (identifier) = 1;
3841 return identifier;
3844 /* Write out the appropriate string for this variable when generating
3845 another mangled name based on this one. */
3847 static void
3848 write_guarded_var_name (const tree variable)
3850 if (DECL_NAME (variable)
3851 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3852 /* The name of a guard variable for a reference temporary should refer
3853 to the reference, not the temporary. */
3854 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3855 else
3856 write_name (variable, /*ignore_local_scope=*/0);
3859 /* Return an identifier for the name of an initialization guard
3860 variable for indicated VARIABLE. */
3862 tree
3863 mangle_guard_variable (const tree variable)
3865 start_mangling (variable);
3866 write_string ("_ZGV");
3867 write_guarded_var_name (variable);
3868 return finish_mangling_get_identifier ();
3871 /* Return an identifier for the name of a thread_local initialization
3872 function for VARIABLE. */
3874 tree
3875 mangle_tls_init_fn (const tree variable)
3877 start_mangling (variable);
3878 write_string ("_ZTH");
3879 write_guarded_var_name (variable);
3880 return finish_mangling_get_identifier ();
3883 /* Return an identifier for the name of a thread_local wrapper
3884 function for VARIABLE. */
3886 #define TLS_WRAPPER_PREFIX "_ZTW"
3888 tree
3889 mangle_tls_wrapper_fn (const tree variable)
3891 start_mangling (variable);
3892 write_string (TLS_WRAPPER_PREFIX);
3893 write_guarded_var_name (variable);
3894 return finish_mangling_get_identifier ();
3897 /* Return true iff FN is a thread_local wrapper function. */
3899 bool
3900 decl_tls_wrapper_p (const tree fn)
3902 if (TREE_CODE (fn) != FUNCTION_DECL)
3903 return false;
3904 tree name = DECL_NAME (fn);
3905 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3906 strlen (TLS_WRAPPER_PREFIX)) == 0;
3909 /* Return an identifier for the name of a temporary variable used to
3910 initialize a static reference. This isn't part of the ABI, but we might
3911 as well call them something readable. */
3913 static GTY(()) int temp_count;
3915 tree
3916 mangle_ref_init_variable (const tree variable)
3918 start_mangling (variable);
3919 write_string ("_ZGR");
3920 write_name (variable, /*ignore_local_scope=*/0);
3921 /* Avoid name clashes with aggregate initialization of multiple
3922 references at once. */
3923 write_unsigned_number (temp_count++);
3924 return finish_mangling_get_identifier ();
3928 /* Foreign language type mangling section. */
3930 /* How to write the type codes for the integer Java type. */
3932 static void
3933 write_java_integer_type_codes (const tree type)
3935 if (type == java_int_type_node)
3936 write_char ('i');
3937 else if (type == java_short_type_node)
3938 write_char ('s');
3939 else if (type == java_byte_type_node)
3940 write_char ('c');
3941 else if (type == java_char_type_node)
3942 write_char ('w');
3943 else if (type == java_long_type_node)
3944 write_char ('x');
3945 else if (type == java_boolean_type_node)
3946 write_char ('b');
3947 else
3948 gcc_unreachable ();
3951 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3952 function generates a mangled name for the vtable map variable of
3953 the class type. For example, if the class type is
3954 "std::bad_exception", the mangled name for the class is
3955 "St13bad_exception". This function would generate the name
3956 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3957 "_VTV<std::bad_exception>::__vtable_map". */
3960 char *
3961 get_mangled_vtable_map_var_name (tree class_type)
3963 char *var_name = NULL;
3964 const char *prefix = "_ZN4_VTVI";
3965 const char *postfix = "E12__vtable_mapE";
3967 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3969 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3970 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3971 strlen (prefix) +
3972 strlen (postfix) + 1;
3974 var_name = (char *) xmalloc (len);
3976 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3978 return var_name;
3981 #include "gt-cp-mangle.h"