* config/sparc/sparc.opt (msubxc): New option.
[official-gcc.git] / gcc / cp / mangle.c
blob9f86e914afedce346a6c55e7cd53c1ff505584a1
1 /* Name mangling for the 3.0 -*- C++ -*- ABI.
2 Copyright (C) 2000-2016 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 "target.h"
51 #include "vtable-verify.h"
52 #include "cp-tree.h"
53 #include "stringpool.h"
54 #include "cgraph.h"
55 #include "stor-layout.h"
56 #include "flags.h"
57 #include "attribs.h"
59 /* Debugging support. */
61 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
62 #ifndef DEBUG_MANGLE
63 #define DEBUG_MANGLE 0
64 #endif
66 /* Macros for tracing the write_* functions. */
67 #if DEBUG_MANGLE
68 # define MANGLE_TRACE(FN, INPUT) \
69 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
70 # define MANGLE_TRACE_TREE(FN, NODE) \
71 fprintf (stderr, " %-24s: %-24s (%p)\n", \
72 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
73 #else
74 # define MANGLE_TRACE(FN, INPUT)
75 # define MANGLE_TRACE_TREE(FN, NODE)
76 #endif
78 /* Nonzero if NODE is a class template-id. We can't rely on
79 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
80 that hard to distinguish A<T> from A, where A<T> is the type as
81 instantiated outside of the template, and A is the type used
82 without parameters inside the template. */
83 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
84 (TYPE_LANG_SPECIFIC (NODE) != NULL \
85 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
86 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
87 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
89 /* For deciding whether to set G.need_abi_warning, we need to consider both
90 warn_abi_version and flag_abi_compat_version. */
91 #define abi_warn_or_compat_version_crosses(N) \
92 (abi_version_crosses (N) || abi_compat_version_crosses (N))
94 /* And sometimes we can simplify the code path if we don't need to worry about
95 previous ABIs. */
96 #define abi_flag_at_least(flag,N) (flag == 0 || flag >= N)
97 #define any_abi_below(N) \
98 (!abi_version_at_least (N) \
99 || !abi_flag_at_least (warn_abi_version, (N)) \
100 || !abi_flag_at_least (flag_abi_compat_version, (N)))
102 /* Things we only need one of. This module is not reentrant. */
103 struct GTY(()) globals {
104 /* An array of the current substitution candidates, in the order
105 we've seen them. */
106 vec<tree, va_gc> *substitutions;
108 /* The entity that is being mangled. */
109 tree GTY ((skip)) entity;
111 /* How many parameter scopes we are inside. */
112 int parm_depth;
114 /* True if the mangling will be different in a future version of the
115 ABI. */
116 bool need_abi_warning;
119 static GTY (()) globals G;
121 /* The obstack on which we build mangled names. */
122 static struct obstack *mangle_obstack;
124 /* The obstack on which we build mangled names that are not going to
125 be IDENTIFIER_NODEs. */
126 static struct obstack name_obstack;
128 /* The first object on the name_obstack; we use this to free memory
129 allocated on the name_obstack. */
130 static void *name_base;
132 /* Indices into subst_identifiers. These are identifiers used in
133 special substitution rules. */
134 typedef enum
136 SUBID_ALLOCATOR,
137 SUBID_BASIC_STRING,
138 SUBID_CHAR_TRAITS,
139 SUBID_BASIC_ISTREAM,
140 SUBID_BASIC_OSTREAM,
141 SUBID_BASIC_IOSTREAM,
142 SUBID_MAX
144 substitution_identifier_index_t;
146 /* For quick substitution checks, look up these common identifiers
147 once only. */
148 static GTY(()) tree subst_identifiers[SUBID_MAX];
150 /* Single-letter codes for builtin integer types, defined in
151 <builtin-type>. These are indexed by integer_type_kind values. */
152 static const char
153 integer_type_codes[itk_none] =
155 'c', /* itk_char */
156 'a', /* itk_signed_char */
157 'h', /* itk_unsigned_char */
158 's', /* itk_short */
159 't', /* itk_unsigned_short */
160 'i', /* itk_int */
161 'j', /* itk_unsigned_int */
162 'l', /* itk_long */
163 'm', /* itk_unsigned_long */
164 'x', /* itk_long_long */
165 'y', /* itk_unsigned_long_long */
166 /* __intN types are handled separately */
167 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
170 static int decl_is_template_id (const tree, tree* const);
172 /* Functions for handling substitutions. */
174 static inline tree canonicalize_for_substitution (tree);
175 static void add_substitution (tree);
176 static inline int is_std_substitution (const tree,
177 const substitution_identifier_index_t);
178 static inline int is_std_substitution_char (const tree,
179 const substitution_identifier_index_t);
180 static int find_substitution (tree);
181 static void mangle_call_offset (const tree, const tree);
183 /* Functions for emitting mangled representations of things. */
185 static void write_mangled_name (const tree, bool);
186 static void write_encoding (const tree);
187 static void write_name (tree, const int);
188 static void write_abi_tags (tree);
189 static void write_unscoped_name (const tree);
190 static void write_unscoped_template_name (const tree);
191 static void write_nested_name (const tree);
192 static void write_prefix (const tree);
193 static void write_template_prefix (const tree);
194 static void write_unqualified_name (tree);
195 static void write_conversion_operator_name (const tree);
196 static void write_source_name (tree);
197 static void write_literal_operator_name (tree);
198 static void write_unnamed_type_name (const tree);
199 static void write_closure_type_name (const tree);
200 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
201 const unsigned int);
202 static void write_number (unsigned HOST_WIDE_INT, const int,
203 const unsigned int);
204 static void write_compact_number (int num);
205 static void write_integer_cst (const tree);
206 static void write_real_cst (const tree);
207 static void write_identifier (const char *);
208 static void write_special_name_constructor (const tree);
209 static void write_special_name_destructor (const tree);
210 static void write_type (tree);
211 static int write_CV_qualifiers_for_type (const tree);
212 static void write_builtin_type (tree);
213 static void write_function_type (const tree);
214 static void write_bare_function_type (const tree, const int, const tree);
215 static void write_method_parms (tree, const int, const tree);
216 static void write_class_enum_type (const tree);
217 static void write_template_args (tree);
218 static void write_expression (tree);
219 static void write_template_arg_literal (const tree);
220 static void write_template_arg (tree);
221 static void write_template_template_arg (const tree);
222 static void write_array_type (const tree);
223 static void write_pointer_to_member_type (const tree);
224 static void write_template_param (const tree);
225 static void write_template_template_param (const tree);
226 static void write_substitution (const int);
227 static int discriminator_for_local_entity (tree);
228 static int discriminator_for_string_literal (tree, tree);
229 static void write_discriminator (const int);
230 static void write_local_name (tree, const tree, const tree);
231 static void dump_substitution_candidates (void);
232 static tree mangle_decl_string (const tree);
233 static int local_class_index (tree);
234 static void maybe_check_abi_tags (tree, tree = NULL_TREE, int = 10);
235 static bool equal_abi_tags (tree, tree);
237 /* Control functions. */
239 static inline void start_mangling (const tree);
240 static tree mangle_special_for_type (const tree, const char *);
242 /* Append a single character to the end of the mangled
243 representation. */
244 #define write_char(CHAR) \
245 obstack_1grow (mangle_obstack, (CHAR))
247 /* Append a sized buffer to the end of the mangled representation. */
248 #define write_chars(CHAR, LEN) \
249 obstack_grow (mangle_obstack, (CHAR), (LEN))
251 /* Append a NUL-terminated string to the end of the mangled
252 representation. */
253 #define write_string(STRING) \
254 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
256 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
257 same purpose (context, which may be a type) and value (template
258 decl). See write_template_prefix for more information on what this
259 is used for. */
260 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
261 (TREE_CODE (NODE1) == TREE_LIST \
262 && TREE_CODE (NODE2) == TREE_LIST \
263 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
264 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
265 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
266 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
268 /* Write out an unsigned quantity in base 10. */
269 #define write_unsigned_number(NUMBER) \
270 write_number ((NUMBER), /*unsigned_p=*/1, 10)
272 /* If DECL is a template instance (including the uninstantiated template
273 itself), return nonzero and, if TEMPLATE_INFO is non-NULL, set
274 *TEMPLATE_INFO to its template info. Otherwise return zero. */
276 static int
277 decl_is_template_id (const tree decl, tree* const template_info)
279 if (TREE_CODE (decl) == TYPE_DECL)
281 /* TYPE_DECLs are handled specially. Look at its type to decide
282 if this is a template instantiation. */
283 const tree type = TREE_TYPE (decl);
285 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
287 if (template_info != NULL)
288 /* For a templated TYPE_DECL, the template info is hanging
289 off the type. */
290 *template_info = TYPE_TEMPLATE_INFO (type);
291 return 1;
294 else
296 /* Check if this is a primary template. */
297 if (DECL_LANG_SPECIFIC (decl) != NULL
298 && VAR_OR_FUNCTION_DECL_P (decl)
299 && DECL_TEMPLATE_INFO (decl)
300 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
301 && TREE_CODE (decl) != TEMPLATE_DECL)
303 if (template_info != NULL)
304 /* For most templated decls, the template info is hanging
305 off the decl. */
306 *template_info = DECL_TEMPLATE_INFO (decl);
307 return 1;
311 /* It's not a template id. */
312 return 0;
315 /* Produce debugging output of current substitution candidates. */
317 static void
318 dump_substitution_candidates (void)
320 unsigned i;
321 tree el;
323 fprintf (stderr, " ++ substitutions ");
324 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
326 const char *name = "???";
328 if (i > 0)
329 fprintf (stderr, " ");
330 if (DECL_P (el))
331 name = IDENTIFIER_POINTER (DECL_NAME (el));
332 else if (TREE_CODE (el) == TREE_LIST)
333 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
334 else if (TYPE_NAME (el))
335 name = TYPE_NAME_STRING (el);
336 fprintf (stderr, " S%d_ = ", i - 1);
337 if (TYPE_P (el) &&
338 (CP_TYPE_RESTRICT_P (el)
339 || CP_TYPE_VOLATILE_P (el)
340 || CP_TYPE_CONST_P (el)))
341 fprintf (stderr, "CV-");
342 fprintf (stderr, "%s (%s at %p)\n",
343 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
347 /* Both decls and types can be substitution candidates, but sometimes
348 they refer to the same thing. For instance, a TYPE_DECL and
349 RECORD_TYPE for the same class refer to the same thing, and should
350 be treated accordingly in substitutions. This function returns a
351 canonicalized tree node representing NODE that is used when adding
352 and substitution candidates and finding matches. */
354 static inline tree
355 canonicalize_for_substitution (tree node)
357 /* For a TYPE_DECL, use the type instead. */
358 if (TREE_CODE (node) == TYPE_DECL)
359 node = TREE_TYPE (node);
360 if (TYPE_P (node)
361 && TYPE_CANONICAL (node) != node
362 && TYPE_MAIN_VARIANT (node) != node)
364 tree orig = node;
365 /* Here we want to strip the topmost typedef only.
366 We need to do that so is_std_substitution can do proper
367 name matching. */
368 if (TREE_CODE (node) == FUNCTION_TYPE)
369 /* Use build_qualified_type and TYPE_QUALS here to preserve
370 the old buggy mangling of attribute noreturn with abi<5. */
371 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
372 TYPE_QUALS (node));
373 else
374 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
375 cp_type_quals (node));
376 if (TREE_CODE (node) == FUNCTION_TYPE
377 || TREE_CODE (node) == METHOD_TYPE)
378 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
380 return node;
383 /* Add NODE as a substitution candidate. NODE must not already be on
384 the list of candidates. */
386 static void
387 add_substitution (tree node)
389 tree c;
391 if (DEBUG_MANGLE)
392 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
393 get_tree_code_name (TREE_CODE (node)), (void *) node);
395 /* Get the canonicalized substitution candidate for NODE. */
396 c = canonicalize_for_substitution (node);
397 if (DEBUG_MANGLE && c != node)
398 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
399 get_tree_code_name (TREE_CODE (node)), (void *) node);
400 node = c;
402 /* Make sure NODE isn't already a candidate. */
403 if (flag_checking)
405 int i;
406 tree candidate;
408 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
410 gcc_assert (!(DECL_P (node) && node == candidate));
411 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
412 && same_type_p (node, candidate)));
416 /* Put the decl onto the varray of substitution candidates. */
417 vec_safe_push (G.substitutions, node);
419 if (DEBUG_MANGLE)
420 dump_substitution_candidates ();
423 /* Helper function for find_substitution. Returns nonzero if NODE,
424 which may be a decl or a CLASS_TYPE, is a template-id with template
425 name of substitution_index[INDEX] in the ::std namespace. */
427 static inline int
428 is_std_substitution (const tree node,
429 const substitution_identifier_index_t index)
431 tree type = NULL;
432 tree decl = NULL;
434 if (DECL_P (node))
436 type = TREE_TYPE (node);
437 decl = node;
439 else if (CLASS_TYPE_P (node))
441 type = node;
442 decl = TYPE_NAME (node);
444 else
445 /* These are not the droids you're looking for. */
446 return 0;
448 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
449 && TYPE_LANG_SPECIFIC (type)
450 && TYPE_TEMPLATE_INFO (type)
451 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
452 == subst_identifiers[index]));
455 /* Return the ABI tags (the TREE_VALUE of the "abi_tag" attribute entry) for T,
456 which can be a decl or type. */
458 static tree
459 get_abi_tags (tree t)
461 if (!t || TREE_CODE (t) == NAMESPACE_DECL)
462 return NULL_TREE;
464 if (DECL_P (t) && DECL_DECLARES_TYPE_P (t))
465 t = TREE_TYPE (t);
467 tree attrs;
468 if (TYPE_P (t))
469 attrs = TYPE_ATTRIBUTES (t);
470 else
471 attrs = DECL_ATTRIBUTES (t);
473 tree tags = lookup_attribute ("abi_tag", attrs);
474 if (tags)
475 tags = TREE_VALUE (tags);
476 return tags;
479 /* Helper function for find_substitution. Returns nonzero if NODE,
480 which may be a decl or a CLASS_TYPE, is the template-id
481 ::std::identifier<char>, where identifier is
482 substitution_index[INDEX]. */
484 static inline int
485 is_std_substitution_char (const tree node,
486 const substitution_identifier_index_t index)
488 tree args;
489 /* Check NODE's name is ::std::identifier. */
490 if (!is_std_substitution (node, index))
491 return 0;
492 /* Figure out its template args. */
493 if (DECL_P (node))
494 args = DECL_TI_ARGS (node);
495 else if (CLASS_TYPE_P (node))
496 args = CLASSTYPE_TI_ARGS (node);
497 else
498 /* Oops, not a template. */
499 return 0;
500 /* NODE's template arg list should be <char>. */
501 return
502 TREE_VEC_LENGTH (args) == 1
503 && TREE_VEC_ELT (args, 0) == char_type_node;
506 /* Check whether a substitution should be used to represent NODE in
507 the mangling.
509 First, check standard special-case substitutions.
511 <substitution> ::= St
512 # ::std
514 ::= Sa
515 # ::std::allocator
517 ::= Sb
518 # ::std::basic_string
520 ::= Ss
521 # ::std::basic_string<char,
522 ::std::char_traits<char>,
523 ::std::allocator<char> >
525 ::= Si
526 # ::std::basic_istream<char, ::std::char_traits<char> >
528 ::= So
529 # ::std::basic_ostream<char, ::std::char_traits<char> >
531 ::= Sd
532 # ::std::basic_iostream<char, ::std::char_traits<char> >
534 Then examine the stack of currently available substitution
535 candidates for entities appearing earlier in the same mangling
537 If a substitution is found, write its mangled representation and
538 return nonzero. If none is found, just return zero. */
540 static int
541 find_substitution (tree node)
543 int i;
544 const int size = vec_safe_length (G.substitutions);
545 tree decl;
546 tree type;
547 const char *abbr = NULL;
549 if (DEBUG_MANGLE)
550 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
551 get_tree_code_name (TREE_CODE (node)), (void *) node);
553 /* Obtain the canonicalized substitution representation for NODE.
554 This is what we'll compare against. */
555 node = canonicalize_for_substitution (node);
557 /* Check for builtin substitutions. */
559 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
560 type = TYPE_P (node) ? node : TREE_TYPE (node);
562 /* Check for std::allocator. */
563 if (decl
564 && is_std_substitution (decl, SUBID_ALLOCATOR)
565 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
566 abbr = "Sa";
568 /* Check for std::basic_string. */
569 else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
571 if (TYPE_P (node))
573 /* If this is a type (i.e. a fully-qualified template-id),
574 check for
575 std::basic_string <char,
576 std::char_traits<char>,
577 std::allocator<char> > . */
578 if (cp_type_quals (type) == TYPE_UNQUALIFIED
579 && CLASSTYPE_USE_TEMPLATE (type))
581 tree args = CLASSTYPE_TI_ARGS (type);
582 if (TREE_VEC_LENGTH (args) == 3
583 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
584 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
585 SUBID_CHAR_TRAITS)
586 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
587 SUBID_ALLOCATOR))
588 abbr = "Ss";
591 else
592 /* Substitute for the template name only if this isn't a type. */
593 abbr = "Sb";
596 /* Check for basic_{i,o,io}stream. */
597 else if (TYPE_P (node)
598 && cp_type_quals (type) == TYPE_UNQUALIFIED
599 && CLASS_TYPE_P (type)
600 && CLASSTYPE_USE_TEMPLATE (type)
601 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
603 /* First, check for the template
604 args <char, std::char_traits<char> > . */
605 tree args = CLASSTYPE_TI_ARGS (type);
606 if (TREE_VEC_LENGTH (args) == 2
607 && TYPE_P (TREE_VEC_ELT (args, 0))
608 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
609 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
610 SUBID_CHAR_TRAITS))
612 /* Got them. Is this basic_istream? */
613 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
614 abbr = "Si";
615 /* Or basic_ostream? */
616 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
617 abbr = "So";
618 /* Or basic_iostream? */
619 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
620 abbr = "Sd";
624 /* Check for namespace std. */
625 else if (decl && DECL_NAMESPACE_STD_P (decl))
627 write_string ("St");
628 return 1;
631 tree tags = NULL_TREE;
632 if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
633 tags = get_abi_tags (type);
634 /* Now check the list of available substitutions for this mangling
635 operation. */
636 if (!abbr || tags) for (i = 0; i < size; ++i)
638 tree candidate = (*G.substitutions)[i];
639 /* NODE is a matched to a candidate if it's the same decl node or
640 if it's the same type. */
641 if (decl == candidate
642 || (TYPE_P (candidate) && type && TYPE_P (node)
643 && same_type_p (type, candidate))
644 || NESTED_TEMPLATE_MATCH (node, candidate))
646 write_substitution (i);
647 return 1;
651 if (!abbr)
652 /* No substitution found. */
653 return 0;
655 write_string (abbr);
656 if (tags)
658 /* If there are ABI tags on the abbreviation, it becomes
659 a substitution candidate. */
660 write_abi_tags (tags);
661 add_substitution (node);
663 return 1;
666 /* Returns whether DECL's symbol name should be the plain unqualified-id
667 rather than a more complicated mangled name. */
669 static bool
670 unmangled_name_p (const tree decl)
672 if (TREE_CODE (decl) == FUNCTION_DECL)
674 /* The names of `extern "C"' functions are not mangled. */
675 return (DECL_EXTERN_C_FUNCTION_P (decl)
676 /* But overloaded operator names *are* mangled. */
677 && !DECL_OVERLOADED_OPERATOR_P (decl));
679 else if (VAR_P (decl))
681 /* static variables are mangled. */
682 if (!DECL_EXTERNAL_LINKAGE_P (decl))
683 return false;
685 /* extern "C" declarations aren't mangled. */
686 if (DECL_EXTERN_C_P (decl))
687 return true;
689 /* Other variables at non-global scope are mangled. */
690 if (CP_DECL_CONTEXT (decl) != global_namespace)
691 return false;
693 /* Variable template instantiations are mangled. */
694 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
695 && variable_template_p (DECL_TI_TEMPLATE (decl)))
696 return false;
698 /* Declarations with ABI tags are mangled. */
699 if (get_abi_tags (decl))
700 return false;
702 /* The names of non-static global variables aren't mangled. */
703 return true;
706 return false;
709 /* TOP_LEVEL is true, if this is being called at outermost level of
710 mangling. It should be false when mangling a decl appearing in an
711 expression within some other mangling.
713 <mangled-name> ::= _Z <encoding> */
715 static void
716 write_mangled_name (const tree decl, bool top_level)
718 MANGLE_TRACE_TREE ("mangled-name", decl);
720 check_abi_tags (decl);
722 if (unmangled_name_p (decl))
724 if (top_level)
725 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
726 else
728 /* The standard notes: "The <encoding> of an extern "C"
729 function is treated like global-scope data, i.e. as its
730 <source-name> without a type." We cannot write
731 overloaded operators that way though, because it contains
732 characters invalid in assembler. */
733 write_string ("_Z");
734 write_source_name (DECL_NAME (decl));
737 else
739 write_string ("_Z");
740 write_encoding (decl);
744 /* Returns true if the return type of DECL is part of its signature, and
745 therefore its mangling. */
747 bool
748 mangle_return_type_p (tree decl)
750 return (!DECL_CONSTRUCTOR_P (decl)
751 && !DECL_DESTRUCTOR_P (decl)
752 && !DECL_CONV_FN_P (decl)
753 && decl_is_template_id (decl, NULL));
756 /* <encoding> ::= <function name> <bare-function-type>
757 ::= <data name> */
759 static void
760 write_encoding (const tree decl)
762 MANGLE_TRACE_TREE ("encoding", decl);
764 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
766 /* For overloaded operators write just the mangled name
767 without arguments. */
768 if (DECL_OVERLOADED_OPERATOR_P (decl))
769 write_name (decl, /*ignore_local_scope=*/0);
770 else
771 write_source_name (DECL_NAME (decl));
772 return;
775 write_name (decl, /*ignore_local_scope=*/0);
776 if (TREE_CODE (decl) == FUNCTION_DECL)
778 tree fn_type;
779 tree d;
781 if (decl_is_template_id (decl, NULL))
783 fn_type = get_mostly_instantiated_function_type (decl);
784 /* FN_TYPE will not have parameter types for in-charge or
785 VTT parameters. Therefore, we pass NULL_TREE to
786 write_bare_function_type -- otherwise, it will get
787 confused about which artificial parameters to skip. */
788 d = NULL_TREE;
790 else
792 fn_type = TREE_TYPE (decl);
793 d = decl;
796 write_bare_function_type (fn_type,
797 mangle_return_type_p (decl),
802 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
803 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
805 static tree
806 decl_mangling_context (tree decl)
808 tree tcontext = targetm.cxx.decl_mangling_context (decl);
810 if (tcontext != NULL_TREE)
811 return tcontext;
813 if (TREE_CODE (decl) == TEMPLATE_DECL
814 && DECL_TEMPLATE_RESULT (decl))
815 decl = DECL_TEMPLATE_RESULT (decl);
817 if (TREE_CODE (decl) == TYPE_DECL
818 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
820 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
821 if (extra)
822 return extra;
824 else if (template_type_parameter_p (decl))
825 /* template type parms have no mangling context. */
826 return NULL_TREE;
827 return CP_DECL_CONTEXT (decl);
830 /* <name> ::= <unscoped-name>
831 ::= <unscoped-template-name> <template-args>
832 ::= <nested-name>
833 ::= <local-name>
835 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
836 called from <local-name>, which mangles the enclosing scope
837 elsewhere and then uses this function to mangle just the part
838 underneath the function scope. So don't use the <local-name>
839 production, to avoid an infinite recursion. */
841 static void
842 write_name (tree decl, const int ignore_local_scope)
844 tree context;
846 MANGLE_TRACE_TREE ("name", decl);
848 if (TREE_CODE (decl) == TYPE_DECL)
850 /* In case this is a typedef, fish out the corresponding
851 TYPE_DECL for the main variant. */
852 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
855 context = decl_mangling_context (decl);
857 gcc_assert (context != NULL_TREE);
859 if (abi_warn_or_compat_version_crosses (7)
860 && ignore_local_scope
861 && TREE_CODE (context) == PARM_DECL)
862 G.need_abi_warning = 1;
864 /* A decl in :: or ::std scope is treated specially. The former is
865 mangled using <unscoped-name> or <unscoped-template-name>, the
866 latter with a special substitution. Also, a name that is
867 directly in a local function scope is also mangled with
868 <unscoped-name> rather than a full <nested-name>. */
869 if (context == global_namespace
870 || DECL_NAMESPACE_STD_P (context)
871 || (ignore_local_scope
872 && (TREE_CODE (context) == FUNCTION_DECL
873 || (abi_version_at_least (7)
874 && TREE_CODE (context) == PARM_DECL))))
876 tree template_info;
877 /* Is this a template instance? */
878 if (decl_is_template_id (decl, &template_info))
880 /* Yes: use <unscoped-template-name>. */
881 write_unscoped_template_name (TI_TEMPLATE (template_info));
882 write_template_args (TI_ARGS (template_info));
884 else
885 /* Everything else gets an <unqualified-name>. */
886 write_unscoped_name (decl);
888 else
890 /* Handle local names, unless we asked not to (that is, invoked
891 under <local-name>, to handle only the part of the name under
892 the local scope). */
893 if (!ignore_local_scope)
895 /* Scan up the list of scope context, looking for a
896 function. If we find one, this entity is in local
897 function scope. local_entity tracks context one scope
898 level down, so it will contain the element that's
899 directly in that function's scope, either decl or one of
900 its enclosing scopes. */
901 tree local_entity = decl;
902 while (context != global_namespace)
904 /* Make sure we're always dealing with decls. */
905 if (TYPE_P (context))
906 context = TYPE_NAME (context);
907 /* Is this a function? */
908 if (TREE_CODE (context) == FUNCTION_DECL
909 || TREE_CODE (context) == PARM_DECL)
911 /* Yes, we have local scope. Use the <local-name>
912 production for the innermost function scope. */
913 write_local_name (context, local_entity, decl);
914 return;
916 /* Up one scope level. */
917 local_entity = context;
918 context = decl_mangling_context (context);
921 /* No local scope found? Fall through to <nested-name>. */
924 /* Other decls get a <nested-name> to encode their scope. */
925 write_nested_name (decl);
929 /* <unscoped-name> ::= <unqualified-name>
930 ::= St <unqualified-name> # ::std:: */
932 static void
933 write_unscoped_name (const tree decl)
935 tree context = decl_mangling_context (decl);
937 MANGLE_TRACE_TREE ("unscoped-name", decl);
939 /* Is DECL in ::std? */
940 if (DECL_NAMESPACE_STD_P (context))
942 write_string ("St");
943 write_unqualified_name (decl);
945 else
947 /* If not, it should be either in the global namespace, or directly
948 in a local function scope. A lambda can also be mangled in the
949 scope of a default argument. */
950 gcc_assert (context == global_namespace
951 || TREE_CODE (context) == PARM_DECL
952 || TREE_CODE (context) == FUNCTION_DECL);
954 write_unqualified_name (decl);
958 /* <unscoped-template-name> ::= <unscoped-name>
959 ::= <substitution> */
961 static void
962 write_unscoped_template_name (const tree decl)
964 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
966 if (find_substitution (decl))
967 return;
968 write_unscoped_name (decl);
969 add_substitution (decl);
972 /* Write the nested name, including CV-qualifiers, of DECL.
974 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
975 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
977 <ref-qualifier> ::= R # & ref-qualifier
978 ::= O # && ref-qualifier
979 <CV-qualifiers> ::= [r] [V] [K] */
981 static void
982 write_nested_name (const tree decl)
984 tree template_info;
986 MANGLE_TRACE_TREE ("nested-name", decl);
988 write_char ('N');
990 /* Write CV-qualifiers, if this is a member function. */
991 if (TREE_CODE (decl) == FUNCTION_DECL
992 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
994 if (DECL_VOLATILE_MEMFUNC_P (decl))
995 write_char ('V');
996 if (DECL_CONST_MEMFUNC_P (decl))
997 write_char ('K');
998 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
1000 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
1001 write_char ('O');
1002 else
1003 write_char ('R');
1007 /* Is this a template instance? */
1008 if (decl_is_template_id (decl, &template_info))
1010 /* Yes, use <template-prefix>. */
1011 write_template_prefix (decl);
1012 write_template_args (TI_ARGS (template_info));
1014 else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
1015 && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1017 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1018 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1020 write_template_prefix (decl);
1021 write_template_args (TREE_OPERAND (name, 1));
1023 else
1025 write_prefix (decl_mangling_context (decl));
1026 write_unqualified_name (decl);
1029 else
1031 /* No, just use <prefix> */
1032 write_prefix (decl_mangling_context (decl));
1033 write_unqualified_name (decl);
1035 write_char ('E');
1038 /* <prefix> ::= <prefix> <unqualified-name>
1039 ::= <template-param>
1040 ::= <template-prefix> <template-args>
1041 ::= <decltype>
1042 ::= # empty
1043 ::= <substitution> */
1045 static void
1046 write_prefix (const tree node)
1048 tree decl;
1049 /* Non-NULL if NODE represents a template-id. */
1050 tree template_info = NULL;
1052 if (node == NULL
1053 || node == global_namespace)
1054 return;
1056 MANGLE_TRACE_TREE ("prefix", node);
1058 if (TREE_CODE (node) == DECLTYPE_TYPE)
1060 write_type (node);
1061 return;
1064 if (find_substitution (node))
1065 return;
1067 if (DECL_P (node))
1069 /* If this is a function or parm decl, that means we've hit function
1070 scope, so this prefix must be for a local name. In this
1071 case, we're under the <local-name> production, which encodes
1072 the enclosing function scope elsewhere. So don't continue
1073 here. */
1074 if (TREE_CODE (node) == FUNCTION_DECL
1075 || TREE_CODE (node) == PARM_DECL)
1076 return;
1078 decl = node;
1079 decl_is_template_id (decl, &template_info);
1081 else
1083 /* Node is a type. */
1084 decl = TYPE_NAME (node);
1085 if (CLASSTYPE_TEMPLATE_ID_P (node))
1086 template_info = TYPE_TEMPLATE_INFO (node);
1089 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1090 write_template_param (node);
1091 else if (template_info != NULL)
1092 /* Templated. */
1094 write_template_prefix (decl);
1095 write_template_args (TI_ARGS (template_info));
1097 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1099 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1100 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1102 write_template_prefix (decl);
1103 write_template_args (TREE_OPERAND (name, 1));
1105 else
1107 write_prefix (decl_mangling_context (decl));
1108 write_unqualified_name (decl);
1111 else
1112 /* Not templated. */
1114 write_prefix (decl_mangling_context (decl));
1115 write_unqualified_name (decl);
1116 if (VAR_P (decl)
1117 || TREE_CODE (decl) == FIELD_DECL)
1119 /* <data-member-prefix> := <member source-name> M */
1120 write_char ('M');
1121 return;
1125 add_substitution (node);
1128 /* <template-prefix> ::= <prefix> <template component>
1129 ::= <template-param>
1130 ::= <substitution> */
1132 static void
1133 write_template_prefix (const tree node)
1135 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1136 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1137 tree context = decl_mangling_context (decl);
1138 tree template_info;
1139 tree templ;
1140 tree substitution;
1142 MANGLE_TRACE_TREE ("template-prefix", node);
1144 /* Find the template decl. */
1145 if (decl_is_template_id (decl, &template_info))
1146 templ = TI_TEMPLATE (template_info);
1147 else if (TREE_CODE (type) == TYPENAME_TYPE)
1148 /* For a typename type, all we have is the name. */
1149 templ = DECL_NAME (decl);
1150 else
1152 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1154 templ = TYPE_TI_TEMPLATE (type);
1157 /* For a member template, though, the template name for the
1158 innermost name must have all the outer template levels
1159 instantiated. For instance, consider
1161 template<typename T> struct Outer {
1162 template<typename U> struct Inner {};
1165 The template name for `Inner' in `Outer<int>::Inner<float>' is
1166 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1167 levels separately, so there's no TEMPLATE_DECL available for this
1168 (there's only `Outer<T>::Inner<U>').
1170 In order to get the substitutions right, we create a special
1171 TREE_LIST to represent the substitution candidate for a nested
1172 template. The TREE_PURPOSE is the template's context, fully
1173 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1174 template.
1176 So, for the example above, `Outer<int>::Inner' is represented as a
1177 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1178 and whose value is `Outer<T>::Inner<U>'. */
1179 if (context && TYPE_P (context))
1180 substitution = build_tree_list (context, templ);
1181 else
1182 substitution = templ;
1184 if (find_substitution (substitution))
1185 return;
1187 if (TREE_TYPE (templ)
1188 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1189 write_template_param (TREE_TYPE (templ));
1190 else
1192 write_prefix (context);
1193 write_unqualified_name (decl);
1196 add_substitution (substitution);
1199 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1200 mangled through special entry points.
1202 <unqualified-name> ::= <operator-name>
1203 ::= <special-name>
1204 ::= <source-name>
1205 ::= <unnamed-type-name>
1206 ::= <local-source-name>
1208 <local-source-name> ::= L <source-name> <discriminator> */
1210 static void
1211 write_unqualified_id (tree identifier)
1213 if (IDENTIFIER_TYPENAME_P (identifier))
1214 write_conversion_operator_name (TREE_TYPE (identifier));
1215 else if (IDENTIFIER_OPNAME_P (identifier))
1217 int i;
1218 const char *mangled_name = NULL;
1220 /* Unfortunately, there is no easy way to go from the
1221 name of the operator back to the corresponding tree
1222 code. */
1223 for (i = 0; i < MAX_TREE_CODES; ++i)
1224 if (operator_name_info[i].identifier == identifier)
1226 /* The ABI says that we prefer binary operator
1227 names to unary operator names. */
1228 if (operator_name_info[i].arity == 2)
1230 mangled_name = operator_name_info[i].mangled_name;
1231 break;
1233 else if (!mangled_name)
1234 mangled_name = operator_name_info[i].mangled_name;
1236 else if (assignment_operator_name_info[i].identifier
1237 == identifier)
1239 mangled_name
1240 = assignment_operator_name_info[i].mangled_name;
1241 break;
1243 write_string (mangled_name);
1245 else if (UDLIT_OPER_P (identifier))
1246 write_literal_operator_name (identifier);
1247 else
1248 write_source_name (identifier);
1251 static void
1252 write_unqualified_name (tree decl)
1254 MANGLE_TRACE_TREE ("unqualified-name", decl);
1256 if (identifier_p (decl))
1258 write_unqualified_id (decl);
1259 return;
1262 bool found = false;
1264 if (DECL_NAME (decl) == NULL_TREE)
1266 found = true;
1267 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1268 write_source_name (DECL_ASSEMBLER_NAME (decl));
1270 else if (DECL_DECLARES_FUNCTION_P (decl))
1272 found = true;
1273 if (DECL_CONSTRUCTOR_P (decl))
1274 write_special_name_constructor (decl);
1275 else if (DECL_DESTRUCTOR_P (decl))
1276 write_special_name_destructor (decl);
1277 else if (DECL_CONV_FN_P (decl))
1279 /* Conversion operator. Handle it right here.
1280 <operator> ::= cv <type> */
1281 tree type;
1282 if (decl_is_template_id (decl, NULL))
1284 tree fn_type;
1285 fn_type = get_mostly_instantiated_function_type (decl);
1286 type = TREE_TYPE (fn_type);
1288 else if (FNDECL_USED_AUTO (decl))
1289 type = (DECL_STRUCT_FUNCTION (decl)->language
1290 ->x_auto_return_pattern);
1291 else
1292 type = DECL_CONV_FN_TYPE (decl);
1293 write_conversion_operator_name (type);
1295 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1297 operator_name_info_t *oni;
1298 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1299 oni = assignment_operator_name_info;
1300 else
1301 oni = operator_name_info;
1303 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1305 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1306 write_literal_operator_name (DECL_NAME (decl));
1307 else
1308 found = false;
1311 if (found)
1312 /* OK */;
1313 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1314 && DECL_NAMESPACE_SCOPE_P (decl)
1315 && decl_linkage (decl) == lk_internal)
1317 MANGLE_TRACE_TREE ("local-source-name", decl);
1318 write_char ('L');
1319 write_source_name (DECL_NAME (decl));
1320 /* The default discriminator is 1, and that's all we ever use,
1321 so there's no code to output one here. */
1323 else
1325 tree type = TREE_TYPE (decl);
1327 if (TREE_CODE (decl) == TYPE_DECL
1328 && TYPE_UNNAMED_P (type))
1329 write_unnamed_type_name (type);
1330 else if (TREE_CODE (decl) == TYPE_DECL
1331 && LAMBDA_TYPE_P (type))
1332 write_closure_type_name (type);
1333 else
1334 write_source_name (DECL_NAME (decl));
1337 /* We use the ABI tags from the primary class template, ignoring tags on any
1338 specializations. This is necessary because C++ doesn't require a
1339 specialization to be declared before it is used unless the use requires a
1340 complete type, but we need to get the tags right on incomplete types as
1341 well. */
1342 if (tree tmpl = most_general_template (decl))
1344 tree res = DECL_TEMPLATE_RESULT (tmpl);
1345 if (res == NULL_TREE)
1346 /* UNBOUND_CLASS_TEMPLATE. */;
1347 else if (DECL_DECLARES_TYPE_P (decl))
1348 decl = res;
1349 else if (any_abi_below (11))
1351 /* ABI v10 implicit tags on the template. */
1352 tree mtags = missing_abi_tags (res);
1353 /* Explicit tags on the template. */
1354 tree ttags = get_abi_tags (res);
1355 /* Tags on the instantiation. */
1356 tree dtags = get_abi_tags (decl);
1358 if (mtags && abi_warn_or_compat_version_crosses (10))
1359 G.need_abi_warning = 1;
1361 /* Add the v10 tags to the explicit tags now. */
1362 mtags = chainon (mtags, ttags);
1364 if (!G.need_abi_warning
1365 && abi_warn_or_compat_version_crosses (11)
1366 && !equal_abi_tags (dtags, mtags))
1367 G.need_abi_warning = 1;
1369 if (!abi_version_at_least (10))
1370 /* In abi <10, we only got the explicit tags. */
1371 decl = res;
1372 else if (flag_abi_version == 10)
1374 /* In ABI 10, we want explict and implicit tags. */
1375 write_abi_tags (mtags);
1376 return;
1381 tree tags = get_abi_tags (decl);
1382 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
1383 && any_abi_below (11))
1384 if (tree mtags = missing_abi_tags (decl))
1386 if (abi_warn_or_compat_version_crosses (11))
1387 G.need_abi_warning = true;
1388 if (!abi_version_at_least (11))
1389 tags = chainon (mtags, tags);
1391 write_abi_tags (tags);
1394 /* Write the unqualified-name for a conversion operator to TYPE. */
1396 static void
1397 write_conversion_operator_name (const tree type)
1399 write_string ("cv");
1400 write_type (type);
1403 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1405 <source-name> ::= </length/ number> <identifier> */
1407 static void
1408 write_source_name (tree identifier)
1410 MANGLE_TRACE_TREE ("source-name", identifier);
1412 /* Never write the whole template-id name including the template
1413 arguments; we only want the template name. */
1414 if (IDENTIFIER_TEMPLATE (identifier))
1415 identifier = IDENTIFIER_TEMPLATE (identifier);
1417 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1418 write_identifier (IDENTIFIER_POINTER (identifier));
1421 /* Compare two TREE_STRINGs like strcmp. */
1424 tree_string_cmp (const void *p1, const void *p2)
1426 if (p1 == p2)
1427 return 0;
1428 tree s1 = *(const tree*)p1;
1429 tree s2 = *(const tree*)p2;
1430 return strcmp (TREE_STRING_POINTER (s1),
1431 TREE_STRING_POINTER (s2));
1434 /* Return the TREE_LIST of TAGS as a sorted VEC. */
1436 static vec<tree, va_gc> *
1437 sorted_abi_tags (tree tags)
1439 vec<tree, va_gc> * vec = make_tree_vector();
1441 for (tree t = tags; t; t = TREE_CHAIN (t))
1443 if (ABI_TAG_IMPLICIT (t))
1444 continue;
1445 tree str = TREE_VALUE (t);
1446 vec_safe_push (vec, str);
1449 vec->qsort (tree_string_cmp);
1451 return vec;
1454 /* ID is the name of a function or type with abi_tags attribute TAGS.
1455 Write out the name, suitably decorated. */
1457 static void
1458 write_abi_tags (tree tags)
1460 if (tags == NULL_TREE)
1461 return;
1463 vec<tree, va_gc> * vec = sorted_abi_tags (tags);
1465 unsigned i; tree str;
1466 FOR_EACH_VEC_ELT (*vec, i, str)
1468 write_string ("B");
1469 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1470 write_identifier (TREE_STRING_POINTER (str));
1473 release_tree_vector (vec);
1476 /* Simplified unique_ptr clone to release a tree vec on exit. */
1478 struct releasing_vec
1480 typedef vec<tree, va_gc> vec_t;
1482 releasing_vec (vec_t *v): v(v) { }
1483 releasing_vec (): v(make_tree_vector ()) { }
1485 vec_t &operator* () const { return *v; }
1486 vec_t *operator-> () const { return v; }
1487 vec_t *get () const { return v; }
1488 operator vec_t *() const { return v; }
1489 tree& operator[] (unsigned i) const { return (*v)[i]; }
1491 ~releasing_vec() { release_tree_vector (v); }
1492 private:
1493 vec_t *v;
1496 /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */
1498 static bool
1499 equal_abi_tags (tree t1, tree t2)
1501 releasing_vec v1 = sorted_abi_tags (t1);
1502 releasing_vec v2 = sorted_abi_tags (t2);
1504 unsigned len1 = v1->length();
1505 if (len1 != v2->length())
1506 return false;
1507 for (unsigned i = 0; i < len1; ++i)
1508 if (tree_string_cmp (v1[i], v2[i]) != 0)
1509 return false;
1510 return true;
1513 /* Write a user-defined literal operator.
1514 ::= li <source-name> # "" <source-name>
1515 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1517 static void
1518 write_literal_operator_name (tree identifier)
1520 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1521 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1522 write_unsigned_number (strlen (suffix));
1523 write_identifier (suffix);
1526 /* Encode 0 as _, and 1+ as n-1_. */
1528 static void
1529 write_compact_number (int num)
1531 if (num > 0)
1532 write_unsigned_number (num - 1);
1533 write_char ('_');
1536 /* Return how many unnamed types precede TYPE in its enclosing class. */
1538 static int
1539 nested_anon_class_index (tree type)
1541 int index = 0;
1542 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1543 for (; member; member = DECL_CHAIN (member))
1544 if (DECL_IMPLICIT_TYPEDEF_P (member))
1546 tree memtype = TREE_TYPE (member);
1547 if (memtype == type)
1548 return index;
1549 else if (TYPE_UNNAMED_P (memtype))
1550 ++index;
1553 gcc_unreachable ();
1556 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1558 static void
1559 write_unnamed_type_name (const tree type)
1561 int discriminator;
1562 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1564 if (TYPE_FUNCTION_SCOPE_P (type))
1565 discriminator = local_class_index (type);
1566 else if (TYPE_CLASS_SCOPE_P (type))
1567 discriminator = nested_anon_class_index (type);
1568 else
1570 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1571 /* Just use the old mangling at namespace scope. */
1572 write_source_name (TYPE_IDENTIFIER (type));
1573 return;
1576 write_string ("Ut");
1577 write_compact_number (discriminator);
1580 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1581 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1583 static void
1584 write_closure_type_name (const tree type)
1586 tree fn = lambda_function (type);
1587 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1588 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1590 MANGLE_TRACE_TREE ("closure-type-name", type);
1592 write_string ("Ul");
1593 write_method_parms (parms, /*method_p=*/1, fn);
1594 write_char ('E');
1595 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1598 /* Convert NUMBER to ascii using base BASE and generating at least
1599 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1600 into which to store the characters. Returns the number of
1601 characters generated (these will be laid out in advance of where
1602 BUFFER points). */
1604 static int
1605 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1606 char *buffer, const unsigned int min_digits)
1608 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1609 unsigned digits = 0;
1611 while (number)
1613 unsigned HOST_WIDE_INT d = number / base;
1615 *--buffer = base_digits[number - d * base];
1616 digits++;
1617 number = d;
1619 while (digits < min_digits)
1621 *--buffer = base_digits[0];
1622 digits++;
1624 return digits;
1627 /* Non-terminal <number>.
1629 <number> ::= [n] </decimal integer/> */
1631 static void
1632 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1633 const unsigned int base)
1635 char buffer[sizeof (HOST_WIDE_INT) * 8];
1636 unsigned count = 0;
1638 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1640 write_char ('n');
1641 number = -((HOST_WIDE_INT) number);
1643 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1644 write_chars (buffer + sizeof (buffer) - count, count);
1647 /* Write out an integral CST in decimal. Most numbers are small, and
1648 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1649 bigger than that, which we must deal with. */
1651 static inline void
1652 write_integer_cst (const tree cst)
1654 int sign = tree_int_cst_sgn (cst);
1655 widest_int abs_value = wi::abs (wi::to_widest (cst));
1656 if (!wi::fits_uhwi_p (abs_value))
1658 /* A bignum. We do this in chunks, each of which fits in a
1659 HOST_WIDE_INT. */
1660 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1661 unsigned HOST_WIDE_INT chunk;
1662 unsigned chunk_digits;
1663 char *ptr = buffer + sizeof (buffer);
1664 unsigned count = 0;
1665 tree n, base, type;
1666 int done;
1668 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1669 representable. */
1670 chunk = 1000000000;
1671 chunk_digits = 9;
1673 if (sizeof (HOST_WIDE_INT) >= 8)
1675 /* It is at least 64 bits, so 10^18 is representable. */
1676 chunk_digits = 18;
1677 chunk *= chunk;
1680 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1681 base = build_int_cstu (type, chunk);
1682 n = wide_int_to_tree (type, cst);
1684 if (sign < 0)
1686 write_char ('n');
1687 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1691 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1692 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1693 unsigned c;
1695 done = integer_zerop (d);
1696 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1697 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1698 done ? 1 : chunk_digits);
1699 ptr -= c;
1700 count += c;
1701 n = d;
1703 while (!done);
1704 write_chars (ptr, count);
1706 else
1708 /* A small num. */
1709 if (sign < 0)
1710 write_char ('n');
1711 write_unsigned_number (abs_value.to_uhwi ());
1715 /* Write out a floating-point literal.
1717 "Floating-point literals are encoded using the bit pattern of the
1718 target processor's internal representation of that number, as a
1719 fixed-length lowercase hexadecimal string, high-order bytes first
1720 (even if the target processor would store low-order bytes first).
1721 The "n" prefix is not used for floating-point literals; the sign
1722 bit is encoded with the rest of the number.
1724 Here are some examples, assuming the IEEE standard representation
1725 for floating point numbers. (Spaces are for readability, not
1726 part of the encoding.)
1728 1.0f Lf 3f80 0000 E
1729 -1.0f Lf bf80 0000 E
1730 1.17549435e-38f Lf 0080 0000 E
1731 1.40129846e-45f Lf 0000 0001 E
1732 0.0f Lf 0000 0000 E"
1734 Caller is responsible for the Lx and the E. */
1735 static void
1736 write_real_cst (const tree value)
1738 long target_real[4]; /* largest supported float */
1739 /* Buffer for eight hex digits in a 32-bit number but big enough
1740 even for 64-bit long to avoid warnings. */
1741 char buffer[17];
1742 int i, limit, dir;
1744 tree type = TREE_TYPE (value);
1745 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1747 real_to_target (target_real, &TREE_REAL_CST (value),
1748 TYPE_MODE (type));
1750 /* The value in target_real is in the target word order,
1751 so we must write it out backward if that happens to be
1752 little-endian. write_number cannot be used, it will
1753 produce uppercase. */
1754 if (FLOAT_WORDS_BIG_ENDIAN)
1755 i = 0, limit = words, dir = 1;
1756 else
1757 i = words - 1, limit = -1, dir = -1;
1759 for (; i != limit; i += dir)
1761 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1762 write_chars (buffer, 8);
1766 /* Non-terminal <identifier>.
1768 <identifier> ::= </unqualified source code identifier> */
1770 static void
1771 write_identifier (const char *identifier)
1773 MANGLE_TRACE ("identifier", identifier);
1774 write_string (identifier);
1777 /* Handle constructor productions of non-terminal <special-name>.
1778 CTOR is a constructor FUNCTION_DECL.
1780 <special-name> ::= C1 # complete object constructor
1781 ::= C2 # base object constructor
1782 ::= C3 # complete object allocating constructor
1784 Currently, allocating constructors are never used. */
1786 static void
1787 write_special_name_constructor (const tree ctor)
1789 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1790 write_string ("C2");
1791 /* This is the old-style "[unified]" constructor.
1792 In some cases, we may emit this function and call
1793 it from the clones in order to share code and save space. */
1794 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1795 write_string ("C4");
1796 else
1798 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1799 write_string ("C1");
1803 /* Handle destructor productions of non-terminal <special-name>.
1804 DTOR is a destructor FUNCTION_DECL.
1806 <special-name> ::= D0 # deleting (in-charge) destructor
1807 ::= D1 # complete object (in-charge) destructor
1808 ::= D2 # base object (not-in-charge) destructor */
1810 static void
1811 write_special_name_destructor (const tree dtor)
1813 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1814 write_string ("D0");
1815 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1816 write_string ("D2");
1817 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1818 /* This is the old-style "[unified]" destructor.
1819 In some cases, we may emit this function and call
1820 it from the clones in order to share code and save space. */
1821 write_string ("D4");
1822 else
1824 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1825 write_string ("D1");
1829 /* Scan the vector of local classes and return how many others with the
1830 same name (or same no name) and context precede ENTITY. */
1832 static int
1833 local_class_index (tree entity)
1835 int ix, discriminator = 0;
1836 tree name = (TYPE_UNNAMED_P (entity) ? NULL_TREE
1837 : TYPE_IDENTIFIER (entity));
1838 tree ctx = TYPE_CONTEXT (entity);
1839 for (ix = 0; ; ix++)
1841 tree type = (*local_classes)[ix];
1842 if (type == entity)
1843 return discriminator;
1844 if (TYPE_CONTEXT (type) == ctx
1845 && (name ? TYPE_IDENTIFIER (type) == name
1846 : TYPE_UNNAMED_P (type)))
1847 ++discriminator;
1849 gcc_unreachable ();
1852 /* Return the discriminator for ENTITY appearing inside
1853 FUNCTION. The discriminator is the lexical ordinal of VAR among
1854 entities with the same name in the same FUNCTION. */
1856 static int
1857 discriminator_for_local_entity (tree entity)
1859 if (DECL_DISCRIMINATOR_P (entity))
1861 if (DECL_DISCRIMINATOR_SET_P (entity))
1862 return DECL_DISCRIMINATOR (entity);
1863 else
1864 /* The first entity with a particular name doesn't get
1865 DECL_DISCRIMINATOR set up. */
1866 return 0;
1868 else if (TREE_CODE (entity) == TYPE_DECL)
1870 /* Scan the list of local classes. */
1871 entity = TREE_TYPE (entity);
1873 /* Lambdas and unnamed types have their own discriminators. */
1874 if (LAMBDA_TYPE_P (entity) || TYPE_UNNAMED_P (entity))
1875 return 0;
1877 return local_class_index (entity);
1879 else
1880 gcc_unreachable ();
1883 /* Return the discriminator for STRING, a string literal used inside
1884 FUNCTION. The discriminator is the lexical ordinal of STRING among
1885 string literals used in FUNCTION. */
1887 static int
1888 discriminator_for_string_literal (tree /*function*/,
1889 tree /*string*/)
1891 /* For now, we don't discriminate amongst string literals. */
1892 return 0;
1895 /* <discriminator> := _ <number>
1897 The discriminator is used only for the second and later occurrences
1898 of the same name within a single function. In this case <number> is
1899 n - 2, if this is the nth occurrence, in lexical order. */
1901 static void
1902 write_discriminator (const int discriminator)
1904 /* If discriminator is zero, don't write anything. Otherwise... */
1905 if (discriminator > 0)
1907 write_char ('_');
1908 write_unsigned_number (discriminator - 1);
1912 /* Mangle the name of a function-scope entity. FUNCTION is the
1913 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1914 default argument scope. ENTITY is the decl for the entity itself.
1915 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1916 either ENTITY itself or an enclosing scope of ENTITY.
1918 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1919 := Z <function encoding> E s [<discriminator>]
1920 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1922 static void
1923 write_local_name (tree function, const tree local_entity,
1924 const tree entity)
1926 tree parm = NULL_TREE;
1928 MANGLE_TRACE_TREE ("local-name", entity);
1930 if (TREE_CODE (function) == PARM_DECL)
1932 parm = function;
1933 function = DECL_CONTEXT (parm);
1936 write_char ('Z');
1937 write_encoding (function);
1938 write_char ('E');
1940 /* For this purpose, parameters are numbered from right-to-left. */
1941 if (parm)
1943 tree t;
1944 int i = 0;
1945 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1947 if (t == parm)
1948 i = 1;
1949 else if (i)
1950 ++i;
1952 write_char ('d');
1953 write_compact_number (i - 1);
1956 if (TREE_CODE (entity) == STRING_CST)
1958 write_char ('s');
1959 write_discriminator (discriminator_for_string_literal (function,
1960 entity));
1962 else
1964 /* Now the <entity name>. Let write_name know its being called
1965 from <local-name>, so it doesn't try to process the enclosing
1966 function scope again. */
1967 write_name (entity, /*ignore_local_scope=*/1);
1968 write_discriminator (discriminator_for_local_entity (local_entity));
1972 /* Non-terminals <type> and <CV-qualifier>.
1974 <type> ::= <builtin-type>
1975 ::= <function-type>
1976 ::= <class-enum-type>
1977 ::= <array-type>
1978 ::= <pointer-to-member-type>
1979 ::= <template-param>
1980 ::= <substitution>
1981 ::= <CV-qualifier>
1982 ::= P <type> # pointer-to
1983 ::= R <type> # reference-to
1984 ::= C <type> # complex pair (C 2000)
1985 ::= G <type> # imaginary (C 2000) [not supported]
1986 ::= U <source-name> <type> # vendor extended type qualifier
1988 C++0x extensions
1990 <type> ::= RR <type> # rvalue reference-to
1991 <type> ::= Dt <expression> # decltype of an id-expression or
1992 # class member access
1993 <type> ::= DT <expression> # decltype of an expression
1994 <type> ::= Dn # decltype of nullptr
1996 TYPE is a type node. */
1998 static void
1999 write_type (tree type)
2001 /* This gets set to nonzero if TYPE turns out to be a (possibly
2002 CV-qualified) builtin type. */
2003 int is_builtin_type = 0;
2005 MANGLE_TRACE_TREE ("type", type);
2007 if (type == error_mark_node)
2008 return;
2010 type = canonicalize_for_substitution (type);
2011 if (find_substitution (type))
2012 return;
2015 if (write_CV_qualifiers_for_type (type) > 0)
2016 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
2017 mangle the unqualified type. The recursive call is needed here
2018 since both the qualified and unqualified types are substitution
2019 candidates. */
2021 tree t = TYPE_MAIN_VARIANT (type);
2022 if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t))
2024 tree attrs = NULL_TREE;
2025 if (tx_safe_fn_type_p (type))
2026 attrs = tree_cons (get_identifier ("transaction_safe"),
2027 NULL_TREE, attrs);
2028 t = cp_build_type_attribute_variant (t, attrs);
2030 gcc_assert (t != type);
2031 if (TREE_CODE (t) == FUNCTION_TYPE
2032 || TREE_CODE (t) == METHOD_TYPE)
2034 t = build_ref_qualified_type (t, type_memfn_rqual (type));
2035 if (abi_version_at_least (8)
2036 || type == TYPE_MAIN_VARIANT (type))
2037 /* Avoid adding the unqualified function type as a substitution. */
2038 write_function_type (t);
2039 else
2040 write_type (t);
2041 if (abi_warn_or_compat_version_crosses (8))
2042 G.need_abi_warning = 1;
2044 else
2045 write_type (t);
2047 else if (TREE_CODE (type) == ARRAY_TYPE)
2048 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
2049 so that the cv-qualification of the element type is available
2050 in write_array_type. */
2051 write_array_type (type);
2052 else
2054 tree type_orig = type;
2056 /* See through any typedefs. */
2057 type = TYPE_MAIN_VARIANT (type);
2058 if (TREE_CODE (type) == FUNCTION_TYPE
2059 || TREE_CODE (type) == METHOD_TYPE)
2060 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
2062 /* According to the C++ ABI, some library classes are passed the
2063 same as the scalar type of their single member and use the same
2064 mangling. */
2065 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2066 type = TREE_TYPE (first_field (type));
2068 if (TYPE_PTRDATAMEM_P (type))
2069 write_pointer_to_member_type (type);
2070 else
2072 /* Handle any target-specific fundamental types. */
2073 const char *target_mangling
2074 = targetm.mangle_type (type_orig);
2076 if (target_mangling)
2078 write_string (target_mangling);
2079 /* Add substitutions for types other than fundamental
2080 types. */
2081 if (!VOID_TYPE_P (type)
2082 && TREE_CODE (type) != INTEGER_TYPE
2083 && TREE_CODE (type) != REAL_TYPE
2084 && TREE_CODE (type) != BOOLEAN_TYPE)
2085 add_substitution (type);
2086 return;
2089 switch (TREE_CODE (type))
2091 case VOID_TYPE:
2092 case BOOLEAN_TYPE:
2093 case INTEGER_TYPE: /* Includes wchar_t. */
2094 case REAL_TYPE:
2095 case FIXED_POINT_TYPE:
2097 /* If this is a typedef, TYPE may not be one of
2098 the standard builtin type nodes, but an alias of one. Use
2099 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
2100 write_builtin_type (TYPE_MAIN_VARIANT (type));
2101 ++is_builtin_type;
2103 break;
2105 case COMPLEX_TYPE:
2106 write_char ('C');
2107 write_type (TREE_TYPE (type));
2108 break;
2110 case FUNCTION_TYPE:
2111 case METHOD_TYPE:
2112 write_function_type (type);
2113 break;
2115 case UNION_TYPE:
2116 case RECORD_TYPE:
2117 case ENUMERAL_TYPE:
2118 /* A pointer-to-member function is represented as a special
2119 RECORD_TYPE, so check for this first. */
2120 if (TYPE_PTRMEMFUNC_P (type))
2121 write_pointer_to_member_type (type);
2122 else
2123 write_class_enum_type (type);
2124 break;
2126 case TYPENAME_TYPE:
2127 case UNBOUND_CLASS_TEMPLATE:
2128 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2129 ordinary nested names. */
2130 write_nested_name (TYPE_STUB_DECL (type));
2131 break;
2133 case POINTER_TYPE:
2134 case REFERENCE_TYPE:
2135 if (TYPE_PTR_P (type))
2136 write_char ('P');
2137 else if (TYPE_REF_IS_RVALUE (type))
2138 write_char ('O');
2139 else
2140 write_char ('R');
2142 tree target = TREE_TYPE (type);
2143 /* Attribute const/noreturn are not reflected in mangling.
2144 We strip them here rather than at a lower level because
2145 a typedef or template argument can have function type
2146 with function-cv-quals (that use the same representation),
2147 but you can't have a pointer/reference to such a type. */
2148 if (TREE_CODE (target) == FUNCTION_TYPE)
2150 if (abi_warn_or_compat_version_crosses (5)
2151 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2152 G.need_abi_warning = 1;
2153 if (abi_version_at_least (5))
2154 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2156 write_type (target);
2158 break;
2160 case TEMPLATE_TYPE_PARM:
2161 if (is_auto (type))
2163 if (AUTO_IS_DECLTYPE (type))
2164 write_identifier ("Dc");
2165 else
2166 write_identifier ("Da");
2167 ++is_builtin_type;
2168 break;
2170 /* fall through. */
2171 case TEMPLATE_PARM_INDEX:
2172 write_template_param (type);
2173 break;
2175 case TEMPLATE_TEMPLATE_PARM:
2176 write_template_template_param (type);
2177 break;
2179 case BOUND_TEMPLATE_TEMPLATE_PARM:
2180 write_template_template_param (type);
2181 write_template_args
2182 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2183 break;
2185 case VECTOR_TYPE:
2186 if (abi_version_at_least (4))
2188 write_string ("Dv");
2189 /* Non-constant vector size would be encoded with
2190 _ expression, but we don't support that yet. */
2191 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2192 write_char ('_');
2194 else
2195 write_string ("U8__vector");
2196 if (abi_warn_or_compat_version_crosses (4))
2197 G.need_abi_warning = 1;
2198 write_type (TREE_TYPE (type));
2199 break;
2201 case TYPE_PACK_EXPANSION:
2202 write_string ("Dp");
2203 write_type (PACK_EXPANSION_PATTERN (type));
2204 break;
2206 case DECLTYPE_TYPE:
2207 /* These shouldn't make it into mangling. */
2208 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2209 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2211 /* In ABI <5, we stripped decltype of a plain decl. */
2212 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2214 tree expr = DECLTYPE_TYPE_EXPR (type);
2215 tree etype = NULL_TREE;
2216 switch (TREE_CODE (expr))
2218 case VAR_DECL:
2219 case PARM_DECL:
2220 case RESULT_DECL:
2221 case FUNCTION_DECL:
2222 case CONST_DECL:
2223 case TEMPLATE_PARM_INDEX:
2224 etype = TREE_TYPE (expr);
2225 break;
2227 default:
2228 break;
2231 if (etype && !type_uses_auto (etype))
2233 if (abi_warn_or_compat_version_crosses (5))
2234 G.need_abi_warning = 1;
2235 if (!abi_version_at_least (5))
2237 write_type (etype);
2238 return;
2243 write_char ('D');
2244 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2245 write_char ('t');
2246 else
2247 write_char ('T');
2248 ++cp_unevaluated_operand;
2249 write_expression (DECLTYPE_TYPE_EXPR (type));
2250 --cp_unevaluated_operand;
2251 write_char ('E');
2252 break;
2254 case NULLPTR_TYPE:
2255 write_string ("Dn");
2256 if (abi_version_at_least (7))
2257 ++is_builtin_type;
2258 if (abi_warn_or_compat_version_crosses (7))
2259 G.need_abi_warning = 1;
2260 break;
2262 case TYPEOF_TYPE:
2263 sorry ("mangling typeof, use decltype instead");
2264 break;
2266 case UNDERLYING_TYPE:
2267 sorry ("mangling __underlying_type");
2268 break;
2270 case LANG_TYPE:
2271 /* fall through. */
2273 default:
2274 gcc_unreachable ();
2279 /* Types other than builtin types are substitution candidates. */
2280 if (!is_builtin_type)
2281 add_substitution (type);
2284 /* qsort callback for sorting a vector of attribute entries. */
2286 static int
2287 attr_strcmp (const void *p1, const void *p2)
2289 tree a1 = *(const tree*)p1;
2290 tree a2 = *(const tree*)p2;
2292 const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1));
2293 const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2));
2295 return strcmp (as1->name, as2->name);
2298 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2299 CV-qualifiers written for TYPE.
2301 <CV-qualifiers> ::= [r] [V] [K] */
2303 static int
2304 write_CV_qualifiers_for_type (const tree type)
2306 int num_qualifiers = 0;
2308 /* The order is specified by:
2310 "In cases where multiple order-insensitive qualifiers are
2311 present, they should be ordered 'K' (closest to the base type),
2312 'V', 'r', and 'U' (farthest from the base type) ..." */
2314 /* Mangle attributes that affect type identity as extended qualifiers.
2316 We don't do this with classes and enums because their attributes
2317 are part of their definitions, not something added on. */
2319 if (!OVERLOAD_TYPE_P (type))
2321 auto_vec<tree> vec;
2322 for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
2324 tree name = get_attribute_name (a);
2325 const attribute_spec *as = lookup_attribute_spec (name);
2326 if (as && as->affects_type_identity
2327 && !is_attribute_p ("transaction_safe", name)
2328 && !is_attribute_p ("abi_tag", name))
2329 vec.safe_push (a);
2331 if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
2332 G.need_abi_warning = true;
2333 if (abi_version_at_least (10))
2335 vec.qsort (attr_strcmp);
2336 while (!vec.is_empty())
2338 tree a = vec.pop();
2339 const attribute_spec *as
2340 = lookup_attribute_spec (get_attribute_name (a));
2342 write_char ('U');
2343 write_unsigned_number (strlen (as->name));
2344 write_string (as->name);
2345 if (TREE_VALUE (a))
2347 write_char ('I');
2348 for (tree args = TREE_VALUE (a); args;
2349 args = TREE_CHAIN (args))
2351 tree arg = TREE_VALUE (args);
2352 write_template_arg (arg);
2354 write_char ('E');
2357 ++num_qualifiers;
2362 /* Note that we do not use cp_type_quals below; given "const
2363 int[3]", the "const" is emitted with the "int", not with the
2364 array. */
2365 cp_cv_quals quals = TYPE_QUALS (type);
2367 if (quals & TYPE_QUAL_RESTRICT)
2369 write_char ('r');
2370 ++num_qualifiers;
2372 if (quals & TYPE_QUAL_VOLATILE)
2374 write_char ('V');
2375 ++num_qualifiers;
2377 if (quals & TYPE_QUAL_CONST)
2379 write_char ('K');
2380 ++num_qualifiers;
2383 return num_qualifiers;
2386 /* Non-terminal <builtin-type>.
2388 <builtin-type> ::= v # void
2389 ::= b # bool
2390 ::= w # wchar_t
2391 ::= c # char
2392 ::= a # signed char
2393 ::= h # unsigned char
2394 ::= s # short
2395 ::= t # unsigned short
2396 ::= i # int
2397 ::= j # unsigned int
2398 ::= l # long
2399 ::= m # unsigned long
2400 ::= x # long long, __int64
2401 ::= y # unsigned long long, __int64
2402 ::= n # __int128
2403 ::= o # unsigned __int128
2404 ::= f # float
2405 ::= d # double
2406 ::= e # long double, __float80
2407 ::= g # __float128 [not supported]
2408 ::= u <source-name> # vendor extended type */
2410 static void
2411 write_builtin_type (tree type)
2413 if (TYPE_CANONICAL (type))
2414 type = TYPE_CANONICAL (type);
2416 switch (TREE_CODE (type))
2418 case VOID_TYPE:
2419 write_char ('v');
2420 break;
2422 case BOOLEAN_TYPE:
2423 write_char ('b');
2424 break;
2426 case INTEGER_TYPE:
2427 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2428 isn't in integer_type_nodes. */
2429 if (type == wchar_type_node)
2430 write_char ('w');
2431 else if (type == char16_type_node)
2432 write_string ("Ds");
2433 else if (type == char32_type_node)
2434 write_string ("Di");
2435 else
2437 size_t itk;
2438 /* Assume TYPE is one of the shared integer type nodes. Find
2439 it in the array of these nodes. */
2440 iagain:
2441 for (itk = 0; itk < itk_none; ++itk)
2442 if (integer_types[itk] != NULL_TREE
2443 && integer_type_codes[itk] != '\0'
2444 && type == integer_types[itk])
2446 /* Print the corresponding single-letter code. */
2447 write_char (integer_type_codes[itk]);
2448 break;
2451 if (itk == itk_none)
2453 tree t = c_common_type_for_mode (TYPE_MODE (type),
2454 TYPE_UNSIGNED (type));
2455 if (type != t)
2457 type = t;
2458 goto iagain;
2461 if (TYPE_PRECISION (type) == 128)
2462 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2463 else
2465 /* Allow for cases where TYPE is not one of the shared
2466 integer type nodes and write a "vendor extended builtin
2467 type" with a name the form intN or uintN, respectively.
2468 Situations like this can happen if you have an
2469 __attribute__((__mode__(__SI__))) type and use exotic
2470 switches like '-mint8' on AVR. Of course, this is
2471 undefined by the C++ ABI (and '-mint8' is not even
2472 Standard C conforming), but when using such special
2473 options you're pretty much in nowhere land anyway. */
2474 const char *prefix;
2475 char prec[11]; /* up to ten digits for an unsigned */
2477 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2478 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2479 write_char ('u'); /* "vendor extended builtin type" */
2480 write_unsigned_number (strlen (prefix) + strlen (prec));
2481 write_string (prefix);
2482 write_string (prec);
2486 break;
2488 case REAL_TYPE:
2489 if (type == float_type_node)
2490 write_char ('f');
2491 else if (type == double_type_node)
2492 write_char ('d');
2493 else if (type == long_double_type_node)
2494 write_char ('e');
2495 else if (type == dfloat32_type_node)
2496 write_string ("Df");
2497 else if (type == dfloat64_type_node)
2498 write_string ("Dd");
2499 else if (type == dfloat128_type_node)
2500 write_string ("De");
2501 else
2502 gcc_unreachable ();
2503 break;
2505 case FIXED_POINT_TYPE:
2506 write_string ("DF");
2507 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2508 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2509 if (type == fract_type_node
2510 || type == sat_fract_type_node
2511 || type == accum_type_node
2512 || type == sat_accum_type_node)
2513 write_char ('i');
2514 else if (type == unsigned_fract_type_node
2515 || type == sat_unsigned_fract_type_node
2516 || type == unsigned_accum_type_node
2517 || type == sat_unsigned_accum_type_node)
2518 write_char ('j');
2519 else if (type == short_fract_type_node
2520 || type == sat_short_fract_type_node
2521 || type == short_accum_type_node
2522 || type == sat_short_accum_type_node)
2523 write_char ('s');
2524 else if (type == unsigned_short_fract_type_node
2525 || type == sat_unsigned_short_fract_type_node
2526 || type == unsigned_short_accum_type_node
2527 || type == sat_unsigned_short_accum_type_node)
2528 write_char ('t');
2529 else if (type == long_fract_type_node
2530 || type == sat_long_fract_type_node
2531 || type == long_accum_type_node
2532 || type == sat_long_accum_type_node)
2533 write_char ('l');
2534 else if (type == unsigned_long_fract_type_node
2535 || type == sat_unsigned_long_fract_type_node
2536 || type == unsigned_long_accum_type_node
2537 || type == sat_unsigned_long_accum_type_node)
2538 write_char ('m');
2539 else if (type == long_long_fract_type_node
2540 || type == sat_long_long_fract_type_node
2541 || type == long_long_accum_type_node
2542 || type == sat_long_long_accum_type_node)
2543 write_char ('x');
2544 else if (type == unsigned_long_long_fract_type_node
2545 || type == sat_unsigned_long_long_fract_type_node
2546 || type == unsigned_long_long_accum_type_node
2547 || type == sat_unsigned_long_long_accum_type_node)
2548 write_char ('y');
2549 else
2550 sorry ("mangling unknown fixed point type");
2551 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2552 if (TYPE_SATURATING (type))
2553 write_char ('s');
2554 else
2555 write_char ('n');
2556 break;
2558 default:
2559 gcc_unreachable ();
2563 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2564 METHOD_TYPE. The return type is mangled before the parameter
2565 types.
2567 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2569 static void
2570 write_function_type (const tree type)
2572 MANGLE_TRACE_TREE ("function-type", type);
2574 /* For a pointer to member function, the function type may have
2575 cv-qualifiers, indicating the quals for the artificial 'this'
2576 parameter. */
2577 if (TREE_CODE (type) == METHOD_TYPE)
2579 /* The first parameter must be a POINTER_TYPE pointing to the
2580 `this' parameter. */
2581 tree this_type = class_of_this_parm (type);
2582 write_CV_qualifiers_for_type (this_type);
2585 if (tx_safe_fn_type_p (type))
2586 write_string ("Dx");
2588 write_char ('F');
2589 /* We don't track whether or not a type is `extern "C"'. Note that
2590 you can have an `extern "C"' function that does not have
2591 `extern "C"' type, and vice versa:
2593 extern "C" typedef void function_t();
2594 function_t f; // f has C++ linkage, but its type is
2595 // `extern "C"'
2597 typedef void function_t();
2598 extern "C" function_t f; // Vice versa.
2600 See [dcl.link]. */
2601 write_bare_function_type (type, /*include_return_type_p=*/1,
2602 /*decl=*/NULL);
2603 if (FUNCTION_REF_QUALIFIED (type))
2605 if (FUNCTION_RVALUE_QUALIFIED (type))
2606 write_char ('O');
2607 else
2608 write_char ('R');
2610 write_char ('E');
2613 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2614 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2615 is mangled before the parameter types. If non-NULL, DECL is
2616 FUNCTION_DECL for the function whose type is being emitted. */
2618 static void
2619 write_bare_function_type (const tree type, const int include_return_type_p,
2620 const tree decl)
2622 MANGLE_TRACE_TREE ("bare-function-type", type);
2624 /* Mangle the return type, if requested. */
2625 if (include_return_type_p)
2626 write_type (TREE_TYPE (type));
2628 /* Now mangle the types of the arguments. */
2629 ++G.parm_depth;
2630 write_method_parms (TYPE_ARG_TYPES (type),
2631 TREE_CODE (type) == METHOD_TYPE,
2632 decl);
2633 --G.parm_depth;
2636 /* Write the mangled representation of a method parameter list of
2637 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2638 considered a non-static method, and the this parameter is omitted.
2639 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2640 parameters are being emitted. */
2642 static void
2643 write_method_parms (tree parm_types, const int method_p, const tree decl)
2645 tree first_parm_type;
2646 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2648 /* Assume this parameter type list is variable-length. If it ends
2649 with a void type, then it's not. */
2650 int varargs_p = 1;
2652 /* If this is a member function, skip the first arg, which is the
2653 this pointer.
2654 "Member functions do not encode the type of their implicit this
2655 parameter."
2657 Similarly, there's no need to mangle artificial parameters, like
2658 the VTT parameters for constructors and destructors. */
2659 if (method_p)
2661 parm_types = TREE_CHAIN (parm_types);
2662 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2664 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2666 parm_types = TREE_CHAIN (parm_types);
2667 parm_decl = DECL_CHAIN (parm_decl);
2671 for (first_parm_type = parm_types;
2672 parm_types;
2673 parm_types = TREE_CHAIN (parm_types))
2675 tree parm = TREE_VALUE (parm_types);
2676 if (parm == void_type_node)
2678 /* "Empty parameter lists, whether declared as () or
2679 conventionally as (void), are encoded with a void parameter
2680 (v)." */
2681 if (parm_types == first_parm_type)
2682 write_type (parm);
2683 /* If the parm list is terminated with a void type, it's
2684 fixed-length. */
2685 varargs_p = 0;
2686 /* A void type better be the last one. */
2687 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2689 else
2690 write_type (parm);
2693 if (varargs_p)
2694 /* <builtin-type> ::= z # ellipsis */
2695 write_char ('z');
2698 /* <class-enum-type> ::= <name> */
2700 static void
2701 write_class_enum_type (const tree type)
2703 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2706 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2707 arguments.
2709 <template-args> ::= I <template-arg>* E */
2711 static void
2712 write_template_args (tree args)
2714 int i;
2715 int length = 0;
2717 MANGLE_TRACE_TREE ("template-args", args);
2719 write_char ('I');
2721 if (args)
2722 length = TREE_VEC_LENGTH (args);
2724 if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2726 /* We have nested template args. We want the innermost template
2727 argument list. */
2728 args = TREE_VEC_ELT (args, length - 1);
2729 length = TREE_VEC_LENGTH (args);
2731 for (i = 0; i < length; ++i)
2732 write_template_arg (TREE_VEC_ELT (args, i));
2734 write_char ('E');
2737 /* Write out the
2738 <unqualified-name>
2739 <unqualified-name> <template-args>
2740 part of SCOPE_REF or COMPONENT_REF mangling. */
2742 static void
2743 write_member_name (tree member)
2745 if (identifier_p (member))
2746 write_unqualified_id (member);
2747 else if (DECL_P (member))
2748 write_unqualified_name (member);
2749 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2751 tree name = TREE_OPERAND (member, 0);
2752 if (TREE_CODE (name) == OVERLOAD)
2753 name = OVL_FUNCTION (name);
2754 write_member_name (name);
2755 write_template_args (TREE_OPERAND (member, 1));
2757 else
2758 write_expression (member);
2761 /* <expression> ::= <unary operator-name> <expression>
2762 ::= <binary operator-name> <expression> <expression>
2763 ::= <expr-primary>
2765 <expr-primary> ::= <template-param>
2766 ::= L <type> <value number> E # literal
2767 ::= L <mangled-name> E # external name
2768 ::= st <type> # sizeof
2769 ::= sr <type> <unqualified-name> # dependent name
2770 ::= sr <type> <unqualified-name> <template-args> */
2772 static void
2773 write_expression (tree expr)
2775 enum tree_code code = TREE_CODE (expr);
2777 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2778 is converted (via qualification conversions) to another
2779 type. */
2780 while (TREE_CODE (expr) == NOP_EXPR
2781 /* Parentheses aren't mangled. */
2782 || code == PAREN_EXPR
2783 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2785 expr = TREE_OPERAND (expr, 0);
2786 code = TREE_CODE (expr);
2789 if (code == BASELINK
2790 && (!type_unknown_p (expr)
2791 || !BASELINK_QUALIFIED_P (expr)))
2793 expr = BASELINK_FUNCTIONS (expr);
2794 code = TREE_CODE (expr);
2797 /* Handle pointers-to-members by making them look like expression
2798 nodes. */
2799 if (code == PTRMEM_CST)
2801 expr = build_nt (ADDR_EXPR,
2802 build_qualified_name (/*type=*/NULL_TREE,
2803 PTRMEM_CST_CLASS (expr),
2804 PTRMEM_CST_MEMBER (expr),
2805 /*template_p=*/false));
2806 code = TREE_CODE (expr);
2809 /* Handle template parameters. */
2810 if (code == TEMPLATE_TYPE_PARM
2811 || code == TEMPLATE_TEMPLATE_PARM
2812 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2813 || code == TEMPLATE_PARM_INDEX)
2814 write_template_param (expr);
2815 /* Handle literals. */
2816 else if (TREE_CODE_CLASS (code) == tcc_constant
2817 || code == CONST_DECL)
2818 write_template_arg_literal (expr);
2819 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2821 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2822 write_string ("fpT");
2824 else if (code == PARM_DECL)
2826 /* A function parameter used in a late-specified return type. */
2827 int index = DECL_PARM_INDEX (expr);
2828 int level = DECL_PARM_LEVEL (expr);
2829 int delta = G.parm_depth - level + 1;
2830 gcc_assert (index >= 1);
2831 write_char ('f');
2832 if (delta != 0)
2834 if (abi_version_at_least (5))
2836 /* Let L be the number of function prototype scopes from the
2837 innermost one (in which the parameter reference occurs) up
2838 to (and including) the one containing the declaration of
2839 the referenced parameter. If the parameter declaration
2840 clause of the innermost function prototype scope has been
2841 completely seen, it is not counted (in that case -- which
2842 is perhaps the most common -- L can be zero). */
2843 write_char ('L');
2844 write_unsigned_number (delta - 1);
2846 if (abi_warn_or_compat_version_crosses (5))
2847 G.need_abi_warning = true;
2849 write_char ('p');
2850 write_compact_number (index - 1);
2852 else if (DECL_P (expr))
2854 write_char ('L');
2855 write_mangled_name (expr, false);
2856 write_char ('E');
2858 else if (TREE_CODE (expr) == SIZEOF_EXPR)
2860 tree op = TREE_OPERAND (expr, 0);
2862 if (PACK_EXPANSION_P (op))
2864 if (abi_warn_or_compat_version_crosses (11))
2865 G.need_abi_warning = true;
2866 if (abi_version_at_least (11))
2868 /* sZ rather than szDp. */
2869 write_string ("sZ");
2870 write_expression (PACK_EXPANSION_PATTERN (op));
2871 return;
2875 if (SIZEOF_EXPR_TYPE_P (expr))
2877 write_string ("st");
2878 write_type (TREE_TYPE (op));
2880 else if (ARGUMENT_PACK_P (op))
2882 tree args = ARGUMENT_PACK_ARGS (op);
2883 int length = TREE_VEC_LENGTH (args);
2884 if (abi_warn_or_compat_version_crosses (10))
2885 G.need_abi_warning = true;
2886 if (abi_version_at_least (10))
2888 /* sP <template-arg>* E # sizeof...(T), size of a captured
2889 template parameter pack from an alias template */
2890 write_string ("sP");
2891 for (int i = 0; i < length; ++i)
2892 write_template_arg (TREE_VEC_ELT (args, i));
2893 write_char ('E');
2895 else
2897 /* In GCC 5 we represented this sizeof wrong, with the effect
2898 that we mangled it as the last element of the pack. */
2899 tree arg = TREE_VEC_ELT (args, length-1);
2900 if (TYPE_P (op))
2902 write_string ("st");
2903 write_type (arg);
2905 else
2907 write_string ("sz");
2908 write_expression (arg);
2912 else if (TYPE_P (TREE_OPERAND (expr, 0)))
2914 write_string ("st");
2915 write_type (TREE_OPERAND (expr, 0));
2917 else
2918 goto normal_expr;
2920 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2921 && TYPE_P (TREE_OPERAND (expr, 0)))
2923 write_string ("at");
2924 write_type (TREE_OPERAND (expr, 0));
2926 else if (code == SCOPE_REF
2927 || code == BASELINK)
2929 tree scope, member;
2930 if (code == SCOPE_REF)
2932 scope = TREE_OPERAND (expr, 0);
2933 member = TREE_OPERAND (expr, 1);
2935 else
2937 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2938 member = BASELINK_FUNCTIONS (expr);
2941 /* If the MEMBER is a real declaration, then the qualifying
2942 scope was not dependent. Ideally, we would not have a
2943 SCOPE_REF in those cases, but sometimes we do. If the second
2944 argument is a DECL, then the name must not have been
2945 dependent. */
2946 if (DECL_P (member))
2947 write_expression (member);
2948 else
2950 write_string ("sr");
2951 write_type (scope);
2952 write_member_name (member);
2955 else if (INDIRECT_REF_P (expr)
2956 && TREE_TYPE (TREE_OPERAND (expr, 0))
2957 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2959 write_expression (TREE_OPERAND (expr, 0));
2961 else if (identifier_p (expr))
2963 /* An operator name appearing as a dependent name needs to be
2964 specially marked to disambiguate between a use of the operator
2965 name and a use of the operator in an expression. */
2966 if (IDENTIFIER_OPNAME_P (expr))
2967 write_string ("on");
2968 write_unqualified_id (expr);
2970 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2972 tree fn = TREE_OPERAND (expr, 0);
2973 if (is_overloaded_fn (fn))
2974 fn = get_first_fn (fn);
2975 if (DECL_P (fn))
2976 fn = DECL_NAME (fn);
2977 if (IDENTIFIER_OPNAME_P (fn))
2978 write_string ("on");
2979 write_unqualified_id (fn);
2980 write_template_args (TREE_OPERAND (expr, 1));
2982 else if (TREE_CODE (expr) == MODOP_EXPR)
2984 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2985 const char *name = (assignment_operator_name_info[(int) subop]
2986 .mangled_name);
2987 write_string (name);
2988 write_expression (TREE_OPERAND (expr, 0));
2989 write_expression (TREE_OPERAND (expr, 2));
2991 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2993 /* ::= [gs] nw <expression>* _ <type> E
2994 ::= [gs] nw <expression>* _ <type> <initializer>
2995 ::= [gs] na <expression>* _ <type> E
2996 ::= [gs] na <expression>* _ <type> <initializer>
2997 <initializer> ::= pi <expression>* E */
2998 tree placement = TREE_OPERAND (expr, 0);
2999 tree type = TREE_OPERAND (expr, 1);
3000 tree nelts = TREE_OPERAND (expr, 2);
3001 tree init = TREE_OPERAND (expr, 3);
3002 tree t;
3004 gcc_assert (code == NEW_EXPR);
3005 if (TREE_OPERAND (expr, 2))
3006 code = VEC_NEW_EXPR;
3008 if (NEW_EXPR_USE_GLOBAL (expr))
3009 write_string ("gs");
3011 write_string (operator_name_info[(int) code].mangled_name);
3013 for (t = placement; t; t = TREE_CHAIN (t))
3014 write_expression (TREE_VALUE (t));
3016 write_char ('_');
3018 if (nelts)
3020 tree domain;
3021 ++processing_template_decl;
3022 domain = compute_array_index_type (NULL_TREE, nelts,
3023 tf_warning_or_error);
3024 type = build_cplus_array_type (type, domain);
3025 --processing_template_decl;
3027 write_type (type);
3029 if (init && TREE_CODE (init) == TREE_LIST
3030 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
3031 write_expression (TREE_VALUE (init));
3032 else
3034 if (init)
3035 write_string ("pi");
3036 if (init && init != void_node)
3037 for (t = init; t; t = TREE_CHAIN (t))
3038 write_expression (TREE_VALUE (t));
3039 write_char ('E');
3042 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
3044 gcc_assert (code == DELETE_EXPR);
3045 if (DELETE_EXPR_USE_VEC (expr))
3046 code = VEC_DELETE_EXPR;
3048 if (DELETE_EXPR_USE_GLOBAL (expr))
3049 write_string ("gs");
3051 write_string (operator_name_info[(int) code].mangled_name);
3053 write_expression (TREE_OPERAND (expr, 0));
3055 else if (code == THROW_EXPR)
3057 tree op = TREE_OPERAND (expr, 0);
3058 if (op)
3060 write_string ("tw");
3061 write_expression (op);
3063 else
3064 write_string ("tr");
3066 else if (code == CONSTRUCTOR)
3068 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
3069 unsigned i; tree val;
3071 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3072 write_string ("il");
3073 else
3075 write_string ("tl");
3076 write_type (TREE_TYPE (expr));
3078 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
3079 write_expression (val);
3080 write_char ('E');
3082 else if (dependent_name (expr))
3084 write_unqualified_id (dependent_name (expr));
3086 else
3088 normal_expr:
3089 int i, len;
3090 const char *name;
3092 /* When we bind a variable or function to a non-type template
3093 argument with reference type, we create an ADDR_EXPR to show
3094 the fact that the entity's address has been taken. But, we
3095 don't actually want to output a mangling code for the `&'. */
3096 if (TREE_CODE (expr) == ADDR_EXPR
3097 && TREE_TYPE (expr)
3098 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
3100 expr = TREE_OPERAND (expr, 0);
3101 if (DECL_P (expr))
3103 write_expression (expr);
3104 return;
3107 code = TREE_CODE (expr);
3110 if (code == COMPONENT_REF)
3112 tree ob = TREE_OPERAND (expr, 0);
3114 if (TREE_CODE (ob) == ARROW_EXPR)
3116 write_string (operator_name_info[(int)code].mangled_name);
3117 ob = TREE_OPERAND (ob, 0);
3118 write_expression (ob);
3120 else if (!is_dummy_object (ob))
3122 write_string ("dt");
3123 write_expression (ob);
3125 /* else, for a non-static data member with no associated object (in
3126 unevaluated context), use the unresolved-name mangling. */
3128 write_member_name (TREE_OPERAND (expr, 1));
3129 return;
3132 /* If it wasn't any of those, recursively expand the expression. */
3133 name = operator_name_info[(int) code].mangled_name;
3135 /* We used to mangle const_cast and static_cast like a C cast. */
3136 if (code == CONST_CAST_EXPR
3137 || code == STATIC_CAST_EXPR)
3139 if (abi_warn_or_compat_version_crosses (6))
3140 G.need_abi_warning = 1;
3141 if (!abi_version_at_least (6))
3142 name = operator_name_info[CAST_EXPR].mangled_name;
3145 if (name == NULL)
3147 switch (code)
3149 case TRAIT_EXPR:
3150 error ("use of built-in trait %qE in function signature; "
3151 "use library traits instead", expr);
3152 break;
3154 default:
3155 sorry ("mangling %C", code);
3156 break;
3158 return;
3160 else
3161 write_string (name);
3163 switch (code)
3165 case CALL_EXPR:
3167 tree fn = CALL_EXPR_FN (expr);
3169 if (TREE_CODE (fn) == ADDR_EXPR)
3170 fn = TREE_OPERAND (fn, 0);
3172 /* Mangle a dependent name as the name, not whatever happens to
3173 be the first function in the overload set. */
3174 if ((TREE_CODE (fn) == FUNCTION_DECL
3175 || TREE_CODE (fn) == OVERLOAD)
3176 && type_dependent_expression_p_push (expr))
3177 fn = DECL_NAME (get_first_fn (fn));
3179 write_expression (fn);
3182 for (i = 0; i < call_expr_nargs (expr); ++i)
3183 write_expression (CALL_EXPR_ARG (expr, i));
3184 write_char ('E');
3185 break;
3187 case CAST_EXPR:
3188 write_type (TREE_TYPE (expr));
3189 if (list_length (TREE_OPERAND (expr, 0)) == 1)
3190 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
3191 else
3193 tree args = TREE_OPERAND (expr, 0);
3194 write_char ('_');
3195 for (; args; args = TREE_CHAIN (args))
3196 write_expression (TREE_VALUE (args));
3197 write_char ('E');
3199 break;
3201 case DYNAMIC_CAST_EXPR:
3202 case REINTERPRET_CAST_EXPR:
3203 case STATIC_CAST_EXPR:
3204 case CONST_CAST_EXPR:
3205 write_type (TREE_TYPE (expr));
3206 write_expression (TREE_OPERAND (expr, 0));
3207 break;
3209 case PREINCREMENT_EXPR:
3210 case PREDECREMENT_EXPR:
3211 if (abi_version_at_least (6))
3212 write_char ('_');
3213 if (abi_warn_or_compat_version_crosses (6))
3214 G.need_abi_warning = 1;
3215 /* Fall through. */
3217 default:
3218 /* In the middle-end, some expressions have more operands than
3219 they do in templates (and mangling). */
3220 len = cp_tree_operand_length (expr);
3222 for (i = 0; i < len; ++i)
3224 tree operand = TREE_OPERAND (expr, i);
3225 /* As a GNU extension, the middle operand of a
3226 conditional may be omitted. Since expression
3227 manglings are supposed to represent the input token
3228 stream, there's no good way to mangle such an
3229 expression without extending the C++ ABI. */
3230 if (code == COND_EXPR && i == 1 && !operand)
3232 error ("omitted middle operand to %<?:%> operand "
3233 "cannot be mangled");
3234 continue;
3236 else if (FOLD_EXPR_P (expr))
3238 /* The first 'operand' of a fold-expression is the operator
3239 that it folds over. */
3240 if (i == 0)
3242 int fcode = TREE_INT_CST_LOW (operand);
3243 write_string (operator_name_info[fcode].mangled_name);
3244 continue;
3246 else if (code == BINARY_LEFT_FOLD_EXPR)
3248 /* The order of operands of the binary left and right
3249 folds is the same, but we want to mangle them in
3250 lexical order, i.e. non-pack first. */
3251 if (i == 1)
3252 operand = FOLD_EXPR_INIT (expr);
3253 else
3254 operand = FOLD_EXPR_PACK (expr);
3256 if (PACK_EXPANSION_P (operand))
3257 operand = PACK_EXPANSION_PATTERN (operand);
3259 write_expression (operand);
3265 /* Literal subcase of non-terminal <template-arg>.
3267 "Literal arguments, e.g. "A<42L>", are encoded with their type
3268 and value. Negative integer values are preceded with "n"; for
3269 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3270 encoded as 0, true as 1." */
3272 static void
3273 write_template_arg_literal (const tree value)
3275 write_char ('L');
3276 write_type (TREE_TYPE (value));
3278 /* Write a null member pointer value as (type)0, regardless of its
3279 real representation. */
3280 if (null_member_pointer_value_p (value))
3281 write_integer_cst (integer_zero_node);
3282 else
3283 switch (TREE_CODE (value))
3285 case CONST_DECL:
3286 write_integer_cst (DECL_INITIAL (value));
3287 break;
3289 case INTEGER_CST:
3290 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3291 || integer_zerop (value) || integer_onep (value));
3292 write_integer_cst (value);
3293 break;
3295 case REAL_CST:
3296 write_real_cst (value);
3297 break;
3299 case COMPLEX_CST:
3300 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3301 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3303 write_integer_cst (TREE_REALPART (value));
3304 write_char ('_');
3305 write_integer_cst (TREE_IMAGPART (value));
3307 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3308 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3310 write_real_cst (TREE_REALPART (value));
3311 write_char ('_');
3312 write_real_cst (TREE_IMAGPART (value));
3314 else
3315 gcc_unreachable ();
3316 break;
3318 case STRING_CST:
3319 sorry ("string literal in function template signature");
3320 break;
3322 default:
3323 gcc_unreachable ();
3326 write_char ('E');
3329 /* Non-terminal <template-arg>.
3331 <template-arg> ::= <type> # type
3332 ::= L <type> </value/ number> E # literal
3333 ::= LZ <name> E # external name
3334 ::= X <expression> E # expression */
3336 static void
3337 write_template_arg (tree node)
3339 enum tree_code code = TREE_CODE (node);
3341 MANGLE_TRACE_TREE ("template-arg", node);
3343 /* A template template parameter's argument list contains TREE_LIST
3344 nodes of which the value field is the actual argument. */
3345 if (code == TREE_LIST)
3347 node = TREE_VALUE (node);
3348 /* If it's a decl, deal with its type instead. */
3349 if (DECL_P (node))
3351 node = TREE_TYPE (node);
3352 code = TREE_CODE (node);
3356 if (REFERENCE_REF_P (node))
3357 node = TREE_OPERAND (node, 0);
3358 if (TREE_CODE (node) == NOP_EXPR
3359 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3361 /* Template parameters can be of reference type. To maintain
3362 internal consistency, such arguments use a conversion from
3363 address of object to reference type. */
3364 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3365 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3368 if (TREE_CODE (node) == BASELINK
3369 && !type_unknown_p (node))
3371 if (abi_version_at_least (6))
3372 node = BASELINK_FUNCTIONS (node);
3373 if (abi_warn_or_compat_version_crosses (6))
3374 /* We wrongly wrapped a class-scope function in X/E. */
3375 G.need_abi_warning = 1;
3378 if (ARGUMENT_PACK_P (node))
3380 /* Expand the template argument pack. */
3381 tree args = ARGUMENT_PACK_ARGS (node);
3382 int i, length = TREE_VEC_LENGTH (args);
3383 if (abi_version_at_least (6))
3384 write_char ('J');
3385 else
3386 write_char ('I');
3387 if (abi_warn_or_compat_version_crosses (6))
3388 G.need_abi_warning = 1;
3389 for (i = 0; i < length; ++i)
3390 write_template_arg (TREE_VEC_ELT (args, i));
3391 write_char ('E');
3393 else if (TYPE_P (node))
3394 write_type (node);
3395 else if (code == TEMPLATE_DECL)
3396 /* A template appearing as a template arg is a template template arg. */
3397 write_template_template_arg (node);
3398 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3399 || code == CONST_DECL
3400 || null_member_pointer_value_p (node))
3401 write_template_arg_literal (node);
3402 else if (DECL_P (node))
3404 write_char ('L');
3405 /* Until ABI version 3, the underscore before the mangled name
3406 was incorrectly omitted. */
3407 if (!abi_version_at_least (3))
3408 write_char ('Z');
3409 else
3410 write_string ("_Z");
3411 if (abi_warn_or_compat_version_crosses (3))
3412 G.need_abi_warning = 1;
3413 write_encoding (node);
3414 write_char ('E');
3416 else
3418 /* Template arguments may be expressions. */
3419 write_char ('X');
3420 write_expression (node);
3421 write_char ('E');
3425 /* <template-template-arg>
3426 ::= <name>
3427 ::= <substitution> */
3429 static void
3430 write_template_template_arg (const tree decl)
3432 MANGLE_TRACE_TREE ("template-template-arg", decl);
3434 if (find_substitution (decl))
3435 return;
3436 write_name (decl, /*ignore_local_scope=*/0);
3437 add_substitution (decl);
3441 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3443 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3444 ::= A <expression> _ </element/ type>
3446 "Array types encode the dimension (number of elements) and the
3447 element type. For variable length arrays, the dimension (but not
3448 the '_' separator) is omitted."
3449 Note that for flexible array members, like for other arrays of
3450 unspecified size, the dimension is also omitted. */
3452 static void
3453 write_array_type (const tree type)
3455 write_char ('A');
3456 if (TYPE_DOMAIN (type))
3458 tree index_type;
3460 index_type = TYPE_DOMAIN (type);
3461 /* The INDEX_TYPE gives the upper and lower bounds of the array.
3462 It's null for flexible array members which have no upper bound
3463 (this is a change from GCC 5 and prior where such members were
3464 incorrectly mangled as zero-length arrays). */
3465 if (tree max = TYPE_MAX_VALUE (index_type))
3467 if (TREE_CODE (max) == INTEGER_CST)
3469 /* The ABI specifies that we should mangle the number of
3470 elements in the array, not the largest allowed index. */
3471 offset_int wmax = wi::to_offset (max) + 1;
3472 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3473 number of elements as zero. */
3474 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3475 gcc_assert (wi::fits_uhwi_p (wmax));
3476 write_unsigned_number (wmax.to_uhwi ());
3478 else
3480 max = TREE_OPERAND (max, 0);
3481 write_expression (max);
3485 write_char ('_');
3486 write_type (TREE_TYPE (type));
3489 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3490 variables. TYPE is a pointer-to-member POINTER_TYPE.
3492 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3494 static void
3495 write_pointer_to_member_type (const tree type)
3497 write_char ('M');
3498 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3499 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3502 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3503 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3504 TEMPLATE_PARM_INDEX.
3506 <template-param> ::= T </parameter/ number> _ */
3508 static void
3509 write_template_param (const tree parm)
3511 int parm_index;
3513 MANGLE_TRACE_TREE ("template-parm", parm);
3515 switch (TREE_CODE (parm))
3517 case TEMPLATE_TYPE_PARM:
3518 case TEMPLATE_TEMPLATE_PARM:
3519 case BOUND_TEMPLATE_TEMPLATE_PARM:
3520 parm_index = TEMPLATE_TYPE_IDX (parm);
3521 break;
3523 case TEMPLATE_PARM_INDEX:
3524 parm_index = TEMPLATE_PARM_IDX (parm);
3525 break;
3527 default:
3528 gcc_unreachable ();
3531 write_char ('T');
3532 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3533 earliest template param denoted by `_'. */
3534 write_compact_number (parm_index);
3537 /* <template-template-param>
3538 ::= <template-param>
3539 ::= <substitution> */
3541 static void
3542 write_template_template_param (const tree parm)
3544 tree templ = NULL_TREE;
3546 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3547 template template parameter. The substitution candidate here is
3548 only the template. */
3549 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3551 templ
3552 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3553 if (find_substitution (templ))
3554 return;
3557 /* <template-param> encodes only the template parameter position,
3558 not its template arguments, which is fine here. */
3559 write_template_param (parm);
3560 if (templ)
3561 add_substitution (templ);
3564 /* Non-terminal <substitution>.
3566 <substitution> ::= S <seq-id> _
3567 ::= S_ */
3569 static void
3570 write_substitution (const int seq_id)
3572 MANGLE_TRACE ("substitution", "");
3574 write_char ('S');
3575 if (seq_id > 0)
3576 write_number (seq_id - 1, /*unsigned=*/1, 36);
3577 write_char ('_');
3580 /* Start mangling ENTITY. */
3582 static inline void
3583 start_mangling (const tree entity)
3585 G.entity = entity;
3586 G.need_abi_warning = false;
3587 obstack_free (&name_obstack, name_base);
3588 mangle_obstack = &name_obstack;
3589 name_base = obstack_alloc (&name_obstack, 0);
3592 /* Done with mangling. If WARN is true, and the name of G.entity will
3593 be mangled differently in a future version of the ABI, issue a
3594 warning. */
3596 static void
3597 finish_mangling_internal (void)
3599 /* Clear all the substitutions. */
3600 vec_safe_truncate (G.substitutions, 0);
3602 /* Null-terminate the string. */
3603 write_char ('\0');
3607 /* Like finish_mangling_internal, but return the mangled string. */
3609 static inline const char *
3610 finish_mangling (void)
3612 finish_mangling_internal ();
3613 return (const char *) obstack_finish (mangle_obstack);
3616 /* Like finish_mangling_internal, but return an identifier. */
3618 static tree
3619 finish_mangling_get_identifier (void)
3621 finish_mangling_internal ();
3622 /* Don't obstack_finish here, and the next start_mangling will
3623 remove the identifier. */
3624 return get_identifier ((const char *) obstack_base (mangle_obstack));
3627 /* Initialize data structures for mangling. */
3629 void
3630 init_mangle (void)
3632 gcc_obstack_init (&name_obstack);
3633 name_base = obstack_alloc (&name_obstack, 0);
3634 vec_alloc (G.substitutions, 0);
3636 /* Cache these identifiers for quick comparison when checking for
3637 standard substitutions. */
3638 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3639 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3640 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3641 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3642 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3643 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3646 /* Generate the mangled name of DECL. */
3648 static tree
3649 mangle_decl_string (const tree decl)
3651 tree result;
3652 location_t saved_loc = input_location;
3653 tree saved_fn = NULL_TREE;
3654 bool template_p = false;
3656 /* We shouldn't be trying to mangle an uninstantiated template. */
3657 gcc_assert (!type_dependent_expression_p (decl));
3659 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3661 struct tinst_level *tl = current_instantiation ();
3662 if ((!tl || tl->decl != decl)
3663 && push_tinst_level (decl))
3665 template_p = true;
3666 saved_fn = current_function_decl;
3667 current_function_decl = NULL_TREE;
3670 input_location = DECL_SOURCE_LOCATION (decl);
3672 start_mangling (decl);
3674 if (TREE_CODE (decl) == TYPE_DECL)
3675 write_type (TREE_TYPE (decl));
3676 else
3677 write_mangled_name (decl, true);
3679 result = finish_mangling_get_identifier ();
3680 if (DEBUG_MANGLE)
3681 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3682 IDENTIFIER_POINTER (result));
3684 if (template_p)
3686 pop_tinst_level ();
3687 current_function_decl = saved_fn;
3689 input_location = saved_loc;
3691 return result;
3694 /* Return an identifier for the external mangled name of DECL. */
3696 static tree
3697 get_mangled_id (tree decl)
3699 tree id = mangle_decl_string (decl);
3700 return targetm.mangle_decl_assembler_name (decl, id);
3703 /* If DECL is an implicit mangling alias, return its symtab node; otherwise
3704 return NULL. */
3706 static symtab_node *
3707 decl_implicit_alias_p (tree decl)
3709 if (DECL_P (decl) && DECL_ARTIFICIAL (decl)
3710 && DECL_IGNORED_P (decl)
3711 && (TREE_CODE (decl) == FUNCTION_DECL
3712 || (VAR_P (decl) && TREE_STATIC (decl))))
3714 symtab_node *n = symtab_node::get (decl);
3715 if (n && n->cpp_implicit_alias)
3716 return n;
3718 return NULL;
3721 /* If DECL is a mangling alias, remove it from the symbol table and return
3722 true; otherwise return false. */
3724 bool
3725 maybe_remove_implicit_alias (tree decl)
3727 if (symtab_node *n = decl_implicit_alias_p (decl))
3729 n->remove();
3730 return true;
3732 return false;
3735 /* Create an identifier for the external mangled name of DECL. */
3737 void
3738 mangle_decl (const tree decl)
3740 tree id;
3741 bool dep;
3743 /* Don't bother mangling uninstantiated templates. */
3744 ++processing_template_decl;
3745 if (TREE_CODE (decl) == TYPE_DECL)
3746 dep = dependent_type_p (TREE_TYPE (decl));
3747 else
3748 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3749 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3750 --processing_template_decl;
3751 if (dep)
3752 return;
3754 /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
3755 It is not needed to assign names to anonymous namespace, but we use the
3756 "<anon>" marker to be able to tell if type is C++ ODR type or type
3757 produced by other language. */
3758 if (TREE_CODE (decl) == TYPE_DECL
3759 && TYPE_STUB_DECL (TREE_TYPE (decl))
3760 && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
3761 id = get_identifier ("<anon>");
3762 else
3764 gcc_assert (TREE_CODE (decl) != TYPE_DECL
3765 || !no_linkage_check (TREE_TYPE (decl), true));
3766 if (abi_version_at_least (10))
3767 if (tree fn = decl_function_context (decl))
3768 maybe_check_abi_tags (fn, decl);
3769 id = get_mangled_id (decl);
3771 SET_DECL_ASSEMBLER_NAME (decl, id);
3773 if (id != DECL_NAME (decl)
3774 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3775 && TREE_CODE (decl) != TYPE_DECL
3776 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3777 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3779 int save_ver = flag_abi_version;
3780 tree id2 = NULL_TREE;
3782 if (!DECL_REALLY_EXTERN (decl))
3784 bool set = false;
3786 /* Check IDENTIFIER_GLOBAL_VALUE before setting to avoid redundant
3787 errors from multiple definitions. */
3788 tree d = IDENTIFIER_GLOBAL_VALUE (id);
3789 if (!d || decl_implicit_alias_p (d))
3791 set = true;
3792 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3795 if (!G.need_abi_warning)
3796 return;
3798 /* If the mangling will change in the future, emit an alias with the
3799 future mangled name for forward-compatibility. */
3800 if (!set)
3802 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3803 inform (DECL_SOURCE_LOCATION (decl), "a later -fabi-version= (or "
3804 "=0) avoids this error with a change in mangling");
3807 flag_abi_version = flag_abi_compat_version;
3808 id2 = mangle_decl_string (decl);
3809 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3810 flag_abi_version = save_ver;
3812 if (id2 != id)
3813 note_mangling_alias (decl, id2);
3816 if (warn_abi)
3818 if (flag_abi_compat_version != warn_abi_version
3819 || id2 == NULL_TREE)
3821 flag_abi_version = warn_abi_version;
3822 id2 = mangle_decl_string (decl);
3823 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3825 flag_abi_version = save_ver;
3827 if (id2 == id)
3828 /* OK. */;
3829 else if (warn_abi_version != 0
3830 && abi_version_at_least (warn_abi_version))
3831 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
3832 "the mangled name of %qD changed between "
3833 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3834 G.entity, warn_abi_version, id2,
3835 save_ver, id);
3836 else
3837 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
3838 "the mangled name of %qD changes between "
3839 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3840 G.entity, save_ver, id,
3841 warn_abi_version, id2);
3844 flag_abi_version = save_ver;
3848 /* Generate the mangled representation of TYPE. */
3850 const char *
3851 mangle_type_string (const tree type)
3853 const char *result;
3855 start_mangling (type);
3856 write_type (type);
3857 result = finish_mangling ();
3858 if (DEBUG_MANGLE)
3859 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3860 return result;
3863 /* Create an identifier for the mangled name of a special component
3864 for belonging to TYPE. CODE is the ABI-specified code for this
3865 component. */
3867 static tree
3868 mangle_special_for_type (const tree type, const char *code)
3870 tree result;
3872 /* We don't have an actual decl here for the special component, so
3873 we can't just process the <encoded-name>. Instead, fake it. */
3874 start_mangling (type);
3876 /* Start the mangling. */
3877 write_string ("_Z");
3878 write_string (code);
3880 /* Add the type. */
3881 write_type (type);
3882 result = finish_mangling_get_identifier ();
3884 if (DEBUG_MANGLE)
3885 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3886 IDENTIFIER_POINTER (result));
3888 return result;
3891 /* Create an identifier for the mangled representation of the typeinfo
3892 structure for TYPE. */
3894 tree
3895 mangle_typeinfo_for_type (const tree type)
3897 return mangle_special_for_type (type, "TI");
3900 /* Create an identifier for the mangled name of the NTBS containing
3901 the mangled name of TYPE. */
3903 tree
3904 mangle_typeinfo_string_for_type (const tree type)
3906 return mangle_special_for_type (type, "TS");
3909 /* Create an identifier for the mangled name of the vtable for TYPE. */
3911 tree
3912 mangle_vtbl_for_type (const tree type)
3914 return mangle_special_for_type (type, "TV");
3917 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3919 tree
3920 mangle_vtt_for_type (const tree type)
3922 return mangle_special_for_type (type, "TT");
3925 /* Return an identifier for a construction vtable group. TYPE is
3926 the most derived class in the hierarchy; BINFO is the base
3927 subobject for which this construction vtable group will be used.
3929 This mangling isn't part of the ABI specification; in the ABI
3930 specification, the vtable group is dumped in the same COMDAT as the
3931 main vtable, and is referenced only from that vtable, so it doesn't
3932 need an external name. For binary formats without COMDAT sections,
3933 though, we need external names for the vtable groups.
3935 We use the production
3937 <special-name> ::= CT <type> <offset number> _ <base type> */
3939 tree
3940 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3942 tree result;
3944 start_mangling (type);
3946 write_string ("_Z");
3947 write_string ("TC");
3948 write_type (type);
3949 write_integer_cst (BINFO_OFFSET (binfo));
3950 write_char ('_');
3951 write_type (BINFO_TYPE (binfo));
3953 result = finish_mangling_get_identifier ();
3954 if (DEBUG_MANGLE)
3955 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3956 IDENTIFIER_POINTER (result));
3957 return result;
3960 /* Mangle a this pointer or result pointer adjustment.
3962 <call-offset> ::= h <fixed offset number> _
3963 ::= v <fixed offset number> _ <virtual offset number> _ */
3965 static void
3966 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3968 write_char (virtual_offset ? 'v' : 'h');
3970 /* For either flavor, write the fixed offset. */
3971 write_integer_cst (fixed_offset);
3972 write_char ('_');
3974 /* For a virtual thunk, add the virtual offset. */
3975 if (virtual_offset)
3977 write_integer_cst (virtual_offset);
3978 write_char ('_');
3982 /* Return an identifier for the mangled name of a this-adjusting or
3983 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3984 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3985 is a virtual thunk, and it is the vtbl offset in
3986 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3987 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3988 thunk itself. A covariant thunk name always includes the adjustment
3989 for the this pointer, even if there is none.
3991 <special-name> ::= T <call-offset> <base encoding>
3992 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3993 <base encoding> */
3995 tree
3996 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3997 tree virtual_offset, tree thunk)
3999 tree result;
4001 if (abi_version_at_least (11))
4002 maybe_check_abi_tags (fn_decl, thunk, 11);
4004 start_mangling (fn_decl);
4006 write_string ("_Z");
4007 write_char ('T');
4009 if (!this_adjusting)
4011 /* Covariant thunk with no this adjustment */
4012 write_char ('c');
4013 mangle_call_offset (integer_zero_node, NULL_TREE);
4014 mangle_call_offset (fixed_offset, virtual_offset);
4016 else if (!DECL_THUNK_P (fn_decl))
4017 /* Plain this adjusting thunk. */
4018 mangle_call_offset (fixed_offset, virtual_offset);
4019 else
4021 /* This adjusting thunk to covariant thunk. */
4022 write_char ('c');
4023 mangle_call_offset (fixed_offset, virtual_offset);
4024 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
4025 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
4026 if (virtual_offset)
4027 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
4028 mangle_call_offset (fixed_offset, virtual_offset);
4029 fn_decl = THUNK_TARGET (fn_decl);
4032 /* Scoped name. */
4033 write_encoding (fn_decl);
4035 result = finish_mangling_get_identifier ();
4036 if (DEBUG_MANGLE)
4037 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
4038 return result;
4041 struct conv_type_hasher : ggc_ptr_hash<tree_node>
4043 static hashval_t hash (tree);
4044 static bool equal (tree, tree);
4047 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
4048 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
4049 TYPE. */
4051 static GTY (()) hash_table<conv_type_hasher> *conv_type_names;
4053 /* Hash a node (VAL1) in the table. */
4055 hashval_t
4056 conv_type_hasher::hash (tree val)
4058 return (hashval_t) TYPE_UID (TREE_TYPE (val));
4061 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
4063 bool
4064 conv_type_hasher::equal (tree val1, tree val2)
4066 return TREE_TYPE (val1) == val2;
4069 /* Return an identifier for the mangled unqualified name for a
4070 conversion operator to TYPE. This mangling is not specified by the
4071 ABI spec; it is only used internally. */
4073 tree
4074 mangle_conv_op_name_for_type (const tree type)
4076 tree *slot;
4077 tree identifier;
4079 if (type == error_mark_node)
4080 return error_mark_node;
4082 if (conv_type_names == NULL)
4083 conv_type_names = hash_table<conv_type_hasher>::create_ggc (31);
4085 slot = conv_type_names->find_slot_with_hash (type,
4086 (hashval_t) TYPE_UID (type),
4087 INSERT);
4088 identifier = *slot;
4089 if (!identifier)
4091 char buffer[64];
4093 /* Create a unique name corresponding to TYPE. */
4094 sprintf (buffer, "operator %lu",
4095 (unsigned long) conv_type_names->elements ());
4096 identifier = get_identifier (buffer);
4097 *slot = identifier;
4099 /* Hang TYPE off the identifier so it can be found easily later
4100 when performing conversions. */
4101 TREE_TYPE (identifier) = type;
4103 /* Set bits on the identifier so we know later it's a conversion. */
4104 IDENTIFIER_OPNAME_P (identifier) = 1;
4105 IDENTIFIER_TYPENAME_P (identifier) = 1;
4108 return identifier;
4111 /* Handle ABI backwards compatibility for past bugs where we didn't call
4112 check_abi_tags in places where it's needed: call check_abi_tags and warn if
4113 it makes a difference. If FOR_DECL is non-null, it's the declaration
4114 that we're actually trying to mangle; if it's null, we're mangling the
4115 guard variable for T. */
4117 static void
4118 maybe_check_abi_tags (tree t, tree for_decl, int ver)
4120 if (DECL_ASSEMBLER_NAME_SET_P (t))
4121 return;
4123 tree oldtags = get_abi_tags (t);
4125 mangle_decl (t);
4127 tree newtags = get_abi_tags (t);
4128 if (newtags && newtags != oldtags
4129 && abi_version_crosses (ver))
4131 if (for_decl && DECL_THUNK_P (for_decl))
4132 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4133 "the mangled name of a thunk for %qD changes between "
4134 "-fabi-version=%d and -fabi-version=%d",
4135 t, flag_abi_version, warn_abi_version);
4136 else if (for_decl)
4137 warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
4138 "the mangled name of %qD changes between "
4139 "-fabi-version=%d and -fabi-version=%d",
4140 for_decl, flag_abi_version, warn_abi_version);
4141 else
4142 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4143 "the mangled name of the initialization guard variable for"
4144 "%qD changes between -fabi-version=%d and -fabi-version=%d",
4145 t, flag_abi_version, warn_abi_version);
4149 /* Write out the appropriate string for this variable when generating
4150 another mangled name based on this one. */
4152 static void
4153 write_guarded_var_name (const tree variable)
4155 if (DECL_NAME (variable)
4156 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
4157 /* The name of a guard variable for a reference temporary should refer
4158 to the reference, not the temporary. */
4159 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
4160 else
4161 write_name (variable, /*ignore_local_scope=*/0);
4164 /* Return an identifier for the name of an initialization guard
4165 variable for indicated VARIABLE. */
4167 tree
4168 mangle_guard_variable (const tree variable)
4170 if (abi_version_at_least (10))
4171 maybe_check_abi_tags (variable);
4172 start_mangling (variable);
4173 write_string ("_ZGV");
4174 write_guarded_var_name (variable);
4175 return finish_mangling_get_identifier ();
4178 /* Return an identifier for the name of a thread_local initialization
4179 function for VARIABLE. */
4181 tree
4182 mangle_tls_init_fn (const tree variable)
4184 start_mangling (variable);
4185 write_string ("_ZTH");
4186 write_guarded_var_name (variable);
4187 return finish_mangling_get_identifier ();
4190 /* Return an identifier for the name of a thread_local wrapper
4191 function for VARIABLE. */
4193 #define TLS_WRAPPER_PREFIX "_ZTW"
4195 tree
4196 mangle_tls_wrapper_fn (const tree variable)
4198 start_mangling (variable);
4199 write_string (TLS_WRAPPER_PREFIX);
4200 write_guarded_var_name (variable);
4201 return finish_mangling_get_identifier ();
4204 /* Return true iff FN is a thread_local wrapper function. */
4206 bool
4207 decl_tls_wrapper_p (const tree fn)
4209 if (TREE_CODE (fn) != FUNCTION_DECL)
4210 return false;
4211 tree name = DECL_NAME (fn);
4212 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
4213 strlen (TLS_WRAPPER_PREFIX)) == 0;
4216 /* Return an identifier for the name of a temporary variable used to
4217 initialize a static reference. This isn't part of the ABI, but we might
4218 as well call them something readable. */
4220 static GTY(()) int temp_count;
4222 tree
4223 mangle_ref_init_variable (const tree variable)
4225 start_mangling (variable);
4226 write_string ("_ZGR");
4227 check_abi_tags (variable);
4228 write_name (variable, /*ignore_local_scope=*/0);
4229 /* Avoid name clashes with aggregate initialization of multiple
4230 references at once. */
4231 write_unsigned_number (temp_count++);
4232 return finish_mangling_get_identifier ();
4235 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
4236 function generates a mangled name for the vtable map variable of
4237 the class type. For example, if the class type is
4238 "std::bad_exception", the mangled name for the class is
4239 "St13bad_exception". This function would generate the name
4240 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
4241 "_VTV<std::bad_exception>::__vtable_map". */
4244 char *
4245 get_mangled_vtable_map_var_name (tree class_type)
4247 char *var_name = NULL;
4248 const char *prefix = "_ZN4_VTVI";
4249 const char *postfix = "E12__vtable_mapE";
4251 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
4253 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
4255 if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
4257 class_id = get_mangled_id (TYPE_NAME (class_type));
4258 vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
4261 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
4262 strlen (prefix) +
4263 strlen (postfix) + 1;
4265 var_name = (char *) xmalloc (len);
4267 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
4269 return var_name;
4272 #include "gt-cp-mangle.h"