Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / cp / mangle.cc
blobbef0fda6d2291e4ca40b141b721012798a8aff6e
1 /* Name mangling for the 3.0 -*- C++ -*- ABI.
2 Copyright (C) 2000-2023 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 (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
85 || (CLASS_TYPE_P (NODE) \
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. Contains NULLS, which correspond to module
106 substitutions. */
107 vec<tree, va_gc> *substitutions;
109 /* The entity that is being mangled. */
110 tree GTY ((skip)) entity;
112 /* How many parameter scopes we are inside. */
113 int parm_depth;
115 /* True if the mangling will be different in a future version of the
116 ABI. */
117 bool need_abi_warning;
119 /* True if the mangling will be different in C++17 mode. */
120 bool need_cxx17_warning;
122 /* True if we mangled a module name. */
123 bool mod;
126 static GTY (()) globals G;
128 /* The obstack on which we build mangled names. */
129 static struct obstack *mangle_obstack;
131 /* The obstack on which we build mangled names that are not going to
132 be IDENTIFIER_NODEs. */
133 static struct obstack name_obstack;
135 /* The first object on the name_obstack; we use this to free memory
136 allocated on the name_obstack. */
137 static void *name_base;
139 /* Indices into subst_identifiers. These are identifiers used in
140 special substitution rules. */
141 typedef enum
143 SUBID_ALLOCATOR,
144 SUBID_BASIC_STRING,
145 SUBID_CHAR_TRAITS,
146 SUBID_BASIC_ISTREAM,
147 SUBID_BASIC_OSTREAM,
148 SUBID_BASIC_IOSTREAM,
149 SUBID_MAX
151 substitution_identifier_index_t;
153 /* For quick substitution checks, look up these common identifiers
154 once only. */
155 static GTY(()) tree subst_identifiers[SUBID_MAX];
157 /* Single-letter codes for builtin integer types, defined in
158 <builtin-type>. These are indexed by integer_type_kind values. */
159 static const char
160 integer_type_codes[itk_none] =
162 'c', /* itk_char */
163 'a', /* itk_signed_char */
164 'h', /* itk_unsigned_char */
165 's', /* itk_short */
166 't', /* itk_unsigned_short */
167 'i', /* itk_int */
168 'j', /* itk_unsigned_int */
169 'l', /* itk_long */
170 'm', /* itk_unsigned_long */
171 'x', /* itk_long_long */
172 'y', /* itk_unsigned_long_long */
173 /* __intN types are handled separately */
174 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
177 static tree maybe_template_info (const tree);
179 /* Functions for handling substitutions. */
181 static inline tree canonicalize_for_substitution (tree);
182 static void add_substitution (tree);
183 static inline bool is_std_substitution (const tree,
184 const substitution_identifier_index_t);
185 static inline bool is_std_substitution_char (const tree,
186 const substitution_identifier_index_t);
187 static int find_substitution (tree);
188 static void mangle_call_offset (const tree, const tree);
190 /* Functions for emitting mangled representations of things. */
192 static void write_mangled_name (const tree, bool);
193 static void write_encoding (const tree);
194 static void write_name (tree, const int);
195 static void write_abi_tags (tree);
196 static void write_unscoped_name (const tree);
197 static void write_unscoped_template_name (const tree);
198 static void write_nested_name (const tree);
199 static void write_prefix (const tree);
200 static void write_template_prefix (const tree);
201 static void write_unqualified_name (tree);
202 static void write_conversion_operator_name (const tree);
203 static void write_source_name (tree);
204 static void write_literal_operator_name (tree);
205 static void write_unnamed_type_name (const tree);
206 static void write_closure_type_name (const tree);
207 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
208 const unsigned int);
209 static void write_number (unsigned HOST_WIDE_INT, const int,
210 const unsigned int);
211 static void write_compact_number (int num);
212 static void write_integer_cst (const tree);
213 static void write_real_cst (const tree);
214 static void write_identifier (const char *);
215 static void write_special_name_constructor (const tree);
216 static void write_special_name_destructor (const tree);
217 static void write_type (tree);
218 static int write_CV_qualifiers_for_type (const tree);
219 static void write_builtin_type (tree);
220 static void write_function_type (const tree);
221 static void write_bare_function_type (const tree, const int, const tree);
222 static void write_method_parms (tree, const int, const tree);
223 static void write_class_enum_type (const tree);
224 static void write_template_args (tree);
225 static void write_expression (tree);
226 static void write_template_arg_literal (const tree);
227 static void write_template_arg (tree);
228 static void write_template_template_arg (const tree);
229 static void write_array_type (const tree);
230 static void write_pointer_to_member_type (const tree);
231 static void write_template_param (const tree);
232 static void write_template_template_param (const tree);
233 static void write_substitution (const int);
234 static int discriminator_for_local_entity (tree);
235 static int discriminator_for_string_literal (tree, tree);
236 static void write_discriminator (const int);
237 static void write_local_name (tree, const tree, const tree);
238 static void dump_substitution_candidates (void);
239 static tree mangle_decl_string (const tree);
240 static void maybe_check_abi_tags (tree, tree = NULL_TREE, int = 10);
241 static bool equal_abi_tags (tree, tree);
243 /* Control functions. */
245 static inline void start_mangling (const tree);
246 static tree mangle_special_for_type (const tree, const char *);
248 /* Append a single character to the end of the mangled
249 representation. */
250 #define write_char(CHAR) \
251 obstack_1grow (mangle_obstack, (CHAR))
253 /* Append a sized buffer to the end of the mangled representation. */
254 #define write_chars(CHAR, LEN) \
255 obstack_grow (mangle_obstack, (CHAR), (LEN))
257 /* Append a NUL-terminated string to the end of the mangled
258 representation. */
259 #define write_string(STRING) \
260 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
262 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
263 same purpose (context, which may be a type) and value (template
264 decl). See write_template_prefix for more information on what this
265 is used for. */
266 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
267 (TREE_CODE (NODE1) == TREE_LIST \
268 && TREE_CODE (NODE2) == TREE_LIST \
269 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
270 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
271 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
272 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
274 /* Write out an unsigned quantity in base 10. */
275 #define write_unsigned_number(NUMBER) \
276 write_number ((NUMBER), /*unsigned_p=*/1, 10)
278 /* If DECL is a template instance (including the uninstantiated template
279 itself), return its TEMPLATE_INFO. Otherwise return NULL. */
281 static tree
282 maybe_template_info (const tree decl)
284 if (TREE_CODE (decl) == TYPE_DECL)
286 /* TYPE_DECLs are handled specially. Look at its type to decide
287 if this is a template instantiation. */
288 const tree type = TREE_TYPE (decl);
290 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
291 return TYPE_TEMPLATE_INFO (type);
293 else
295 /* Check if the template is a primary template. */
296 if (DECL_LANG_SPECIFIC (decl) != NULL
297 && VAR_OR_FUNCTION_DECL_P (decl)
298 && DECL_TEMPLATE_INFO (decl)
299 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
300 return DECL_TEMPLATE_INFO (decl);
303 /* It's not a template id. */
304 return NULL_TREE;
307 /* Produce debugging output of current substitution candidates. */
309 static void
310 dump_substitution_candidates (void)
312 unsigned i;
313 tree el;
315 fprintf (stderr, " ++ substitutions ");
316 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
318 const char *name = "???";
320 if (i > 0)
321 fprintf (stderr, " ");
322 if (!el)
323 name = "module";
324 else if (DECL_P (el))
325 name = IDENTIFIER_POINTER (DECL_NAME (el));
326 else if (TREE_CODE (el) == TREE_LIST)
327 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
328 else if (TYPE_NAME (el))
329 name = TYPE_NAME_STRING (el);
330 fprintf (stderr, " S%d_ = ", i - 1);
331 if (el)
333 if (TYPE_P (el) &&
334 (CP_TYPE_RESTRICT_P (el)
335 || CP_TYPE_VOLATILE_P (el)
336 || CP_TYPE_CONST_P (el)))
337 fprintf (stderr, "CV-");
338 fprintf (stderr, "%s (%s at %p)",
339 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
341 fprintf (stderr, "\n");
345 /* <exception-spec> ::=
346 Do -- non-throwing exception specification
347 DO <expression> E -- computed (instantiation-dependent) noexcept
348 Dw <type>* E -- throw (types) */
350 static void
351 write_exception_spec (tree spec)
354 if (!spec || spec == noexcept_false_spec)
355 /* Nothing. */
356 return;
358 if (!flag_noexcept_type)
360 G.need_cxx17_warning = true;
361 return;
364 if (spec == noexcept_true_spec || spec == empty_except_spec)
365 write_string ("Do");
366 else if (tree expr = TREE_PURPOSE (spec))
368 /* noexcept (expr) */
369 gcc_assert (uses_template_parms (expr));
370 write_string ("DO");
371 write_expression (expr);
372 write_char ('E');
374 else
376 /* throw (type-list) */
377 write_string ("Dw");
378 for (tree t = spec; t; t = TREE_CHAIN (t))
379 write_type (TREE_VALUE (t));
380 write_char ('E');
384 /* Both decls and types can be substitution candidates, but sometimes
385 they refer to the same thing. For instance, a TYPE_DECL and
386 RECORD_TYPE for the same class refer to the same thing, and should
387 be treated accordingly in substitutions. This function returns a
388 canonicalized tree node representing NODE that is used when adding
389 and substitution candidates and finding matches. */
391 static inline tree
392 canonicalize_for_substitution (tree node)
394 /* For a TYPE_DECL, use the type instead. */
395 if (TREE_CODE (node) == TYPE_DECL)
396 node = TREE_TYPE (node);
397 if (TYPE_P (node)
398 && TYPE_CANONICAL (node) != node
399 && TYPE_MAIN_VARIANT (node) != node)
401 tree orig = node;
402 /* Here we want to strip the topmost typedef only.
403 We need to do that so is_std_substitution can do proper
404 name matching. */
405 if (TREE_CODE (node) == FUNCTION_TYPE)
406 /* Use build_qualified_type and TYPE_QUALS here to preserve
407 the old buggy mangling of attribute noreturn with abi<5. */
408 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
409 TYPE_QUALS (node));
410 else
411 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
412 cp_type_quals (node));
413 if (FUNC_OR_METHOD_TYPE_P (node))
415 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
416 tree r = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (orig));
417 if (flag_noexcept_type)
418 node = build_exception_variant (node, r);
419 else
420 /* Set the warning flag if appropriate. */
421 write_exception_spec (r);
424 return node;
427 /* Add NODE as a substitution candidate. NODE must not already be on
428 the list of candidates. */
430 static void
431 add_substitution (tree node)
433 tree c;
435 if (DEBUG_MANGLE)
436 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
437 get_tree_code_name (TREE_CODE (node)), (void *) node);
439 /* Get the canonicalized substitution candidate for NODE. */
440 c = canonicalize_for_substitution (node);
441 if (DEBUG_MANGLE && c != node)
442 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
443 get_tree_code_name (TREE_CODE (node)), (void *) node);
444 node = c;
446 /* Make sure NODE isn't already a candidate. */
447 if (flag_checking)
449 int i;
450 tree candidate;
452 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
453 if (candidate)
455 gcc_assert (!(DECL_P (node) && node == candidate));
456 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
457 && same_type_p (node, candidate)));
461 /* Put the decl onto the varray of substitution candidates. */
462 vec_safe_push (G.substitutions, node);
464 if (DEBUG_MANGLE)
465 dump_substitution_candidates ();
468 /* Helper function for find_substitution. Returns nonzero if NODE,
469 which may be a decl or a CLASS_TYPE, is a template-id with template
470 name of substitution_index[INDEX] in the ::std namespace, with
471 global module attachment. */
473 static bool
474 is_std_substitution (const tree node,
475 const substitution_identifier_index_t index)
477 tree type = NULL;
478 tree decl = NULL;
480 if (DECL_P (node))
482 type = TREE_TYPE (node);
483 decl = node;
485 else if (CLASS_TYPE_P (node))
487 type = node;
488 decl = TYPE_NAME (node);
490 else
491 /* These are not the droids you're looking for. */
492 return false;
494 if (!DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl)))
495 return false;
497 if (!(TYPE_LANG_SPECIFIC (type) && TYPE_TEMPLATE_INFO (type)))
498 return false;
500 tree tmpl = TYPE_TI_TEMPLATE (type);
501 if (DECL_NAME (tmpl) != subst_identifiers[index])
502 return false;
504 if (modules_p () && get_originating_module (tmpl, true) >= 0)
505 return false;
507 return true;
510 /* Return the ABI tags (the TREE_VALUE of the "abi_tag" attribute entry) for T,
511 which can be a decl or type. */
513 static tree
514 get_abi_tags (tree t)
516 if (!t || TREE_CODE (t) == NAMESPACE_DECL)
517 return NULL_TREE;
519 if (DECL_P (t) && DECL_DECLARES_TYPE_P (t))
520 t = TREE_TYPE (t);
522 tree attrs;
523 if (TYPE_P (t))
524 attrs = TYPE_ATTRIBUTES (t);
525 else
526 attrs = DECL_ATTRIBUTES (t);
528 tree tags = lookup_attribute ("abi_tag", attrs);
529 if (tags)
530 tags = TREE_VALUE (tags);
531 return tags;
534 /* Helper function for find_substitution. Returns nonzero if NODE,
535 which may be a decl or a CLASS_TYPE, is the template-id
536 ::std::identifier<char>, where identifier is
537 substitution_index[INDEX]. */
539 static bool
540 is_std_substitution_char (const tree node,
541 const substitution_identifier_index_t index)
543 tree args;
544 /* Check NODE's name is ::std::identifier. */
545 if (!is_std_substitution (node, index))
546 return 0;
547 /* Figure out its template args. */
548 if (DECL_P (node))
549 args = DECL_TI_ARGS (node);
550 else if (CLASS_TYPE_P (node))
551 args = CLASSTYPE_TI_ARGS (node);
552 else
553 /* Oops, not a template. */
554 return 0;
555 /* NODE's template arg list should be <char>. */
556 return
557 TREE_VEC_LENGTH (args) == 1
558 && TREE_VEC_ELT (args, 0) == char_type_node;
561 /* Check whether a substitution should be used to represent NODE in
562 the mangling.
564 First, check standard special-case substitutions.
566 <substitution> ::= St
567 # ::std
569 ::= Sa
570 # ::std::allocator
572 ::= Sb
573 # ::std::basic_string
575 ::= Ss
576 # ::std::basic_string<char,
577 ::std::char_traits<char>,
578 ::std::allocator<char> >
580 ::= Si
581 # ::std::basic_istream<char, ::std::char_traits<char> >
583 ::= So
584 # ::std::basic_ostream<char, ::std::char_traits<char> >
586 ::= Sd
587 # ::std::basic_iostream<char, ::std::char_traits<char> >
589 Then examine the stack of currently available substitution
590 candidates for entities appearing earlier in the same mangling
592 If a substitution is found, write its mangled representation and
593 return nonzero. If none is found, just return zero. */
595 static int
596 find_substitution (tree node)
598 int i;
599 const int size = vec_safe_length (G.substitutions);
600 tree decl;
601 tree type;
602 const char *abbr = NULL;
604 if (DEBUG_MANGLE)
605 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
606 get_tree_code_name (TREE_CODE (node)), (void *) node);
608 /* Obtain the canonicalized substitution representation for NODE.
609 This is what we'll compare against. */
610 node = canonicalize_for_substitution (node);
612 /* Check for builtin substitutions. */
614 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
615 type = TYPE_P (node) ? node : TREE_TYPE (node);
617 /* Check for std::allocator. */
618 if (decl
619 && is_std_substitution (decl, SUBID_ALLOCATOR)
620 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
621 abbr = "Sa";
623 /* Check for std::basic_string. */
624 else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
626 if (TYPE_P (node))
628 /* If this is a type (i.e. a fully-qualified template-id),
629 check for
630 std::basic_string <char,
631 std::char_traits<char>,
632 std::allocator<char> > . */
633 if (cp_type_quals (type) == TYPE_UNQUALIFIED
634 && CLASSTYPE_USE_TEMPLATE (type))
636 tree args = CLASSTYPE_TI_ARGS (type);
637 if (TREE_VEC_LENGTH (args) == 3
638 && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
639 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
640 SUBID_CHAR_TRAITS)
641 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
642 SUBID_ALLOCATOR))
643 abbr = "Ss";
646 else
647 /* Substitute for the template name only if this isn't a type. */
648 abbr = "Sb";
651 /* Check for basic_{i,o,io}stream. */
652 else if (TYPE_P (node)
653 && cp_type_quals (type) == TYPE_UNQUALIFIED
654 && CLASS_TYPE_P (type)
655 && CLASSTYPE_USE_TEMPLATE (type)
656 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
658 /* First, check for the template
659 args <char, std::char_traits<char> > . */
660 tree args = CLASSTYPE_TI_ARGS (type);
661 if (TREE_VEC_LENGTH (args) == 2
662 && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
663 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
664 SUBID_CHAR_TRAITS))
666 /* Got them. Is this basic_istream? */
667 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
668 abbr = "Si";
669 /* Or basic_ostream? */
670 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
671 abbr = "So";
672 /* Or basic_iostream? */
673 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
674 abbr = "Sd";
678 /* Check for namespace std. */
679 else if (decl && DECL_NAMESPACE_STD_P (decl))
681 write_string ("St");
682 return 1;
685 tree tags = NULL_TREE;
686 if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
687 tags = get_abi_tags (type);
688 /* Now check the list of available substitutions for this mangling
689 operation. */
690 if (!abbr || tags)
691 for (i = 0; i < size; ++i)
692 if (tree candidate = (*G.substitutions)[i])
694 /* NODE is a matched to a candidate if it's the same decl node or
695 if it's the same type. */
696 if (decl == candidate
697 || (TYPE_P (candidate) && type && TYPE_P (node)
698 && same_type_p (type, candidate))
699 || NESTED_TEMPLATE_MATCH (node, candidate))
701 write_substitution (i);
702 return 1;
706 if (!abbr)
707 /* No substitution found. */
708 return 0;
710 write_string (abbr);
711 if (tags)
713 /* If there are ABI tags on the abbreviation, it becomes
714 a substitution candidate. */
715 write_abi_tags (tags);
716 add_substitution (node);
718 return 1;
721 /* Returns whether DECL's symbol name should be the plain unqualified-id
722 rather than a more complicated mangled name. */
724 static bool
725 unmangled_name_p (const tree decl)
727 if (TREE_CODE (decl) == FUNCTION_DECL)
729 /* The names of `extern "C"' functions are not mangled. */
730 return (DECL_EXTERN_C_FUNCTION_P (decl)
731 /* But overloaded operator names *are* mangled. */
732 && !DECL_OVERLOADED_OPERATOR_P (decl));
734 else if (VAR_P (decl))
736 /* static variables are mangled. */
737 if (!DECL_EXTERNAL_LINKAGE_P (decl))
738 return false;
740 /* extern "C" declarations aren't mangled. */
741 if (DECL_EXTERN_C_P (decl))
742 return true;
744 /* Other variables at non-global scope are mangled. */
745 if (CP_DECL_CONTEXT (decl) != global_namespace)
746 return false;
748 /* Variable template instantiations are mangled. */
749 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
750 && variable_template_p (DECL_TI_TEMPLATE (decl)))
751 return false;
753 /* Declarations with ABI tags are mangled. */
754 if (get_abi_tags (decl))
755 return false;
757 // Declarations attached to a named module are mangled
758 if (modules_p () && get_originating_module (decl, true) >= 0)
759 return false;
761 /* The names of non-static global variables aren't mangled. */
762 return true;
765 return false;
768 /* TOP_LEVEL is true, if this is being called at outermost level of
769 mangling. It should be false when mangling a decl appearing in an
770 expression within some other mangling.
772 <mangled-name> ::= _Z <encoding> */
774 static void
775 write_mangled_name (const tree decl, bool top_level)
777 MANGLE_TRACE_TREE ("mangled-name", decl);
779 check_abi_tags (decl);
781 if (unmangled_name_p (decl))
783 if (top_level)
784 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
785 else
787 /* The standard notes: "The <encoding> of an extern "C"
788 function is treated like global-scope data, i.e. as its
789 <source-name> without a type." We cannot write
790 overloaded operators that way though, because it contains
791 characters invalid in assembler. */
792 write_string ("_Z");
793 write_source_name (DECL_NAME (decl));
796 else
798 write_string ("_Z");
799 write_encoding (decl);
802 /* If this is the pre/post function for a guarded function, append
803 .pre/post, like something from create_virtual_clone. */
804 if (DECL_IS_PRE_FN_P (decl))
805 write_string (".pre");
806 else if (DECL_IS_POST_FN_P (decl))
807 write_string (".post");
809 /* If this is a coroutine helper, then append an appropriate string to
810 identify which. */
811 if (tree ramp = DECL_RAMP_FN (decl))
813 if (DECL_ACTOR_FN (ramp) == decl)
814 write_string (JOIN_STR "actor");
815 else if (DECL_DESTROY_FN (ramp) == decl)
816 write_string (JOIN_STR "destroy");
817 else
818 gcc_unreachable ();
822 /* Returns true if the return type of DECL is part of its signature, and
823 therefore its mangling. */
825 bool
826 mangle_return_type_p (tree decl)
828 return (!DECL_CONSTRUCTOR_P (decl)
829 && !DECL_DESTRUCTOR_P (decl)
830 && !DECL_CONV_FN_P (decl)
831 && maybe_template_info (decl));
834 /* <encoding> ::= <function name> <bare-function-type>
835 ::= <data name> */
837 static void
838 write_encoding (const tree decl)
840 MANGLE_TRACE_TREE ("encoding", decl);
842 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
844 /* For overloaded operators write just the mangled name
845 without arguments. */
846 if (DECL_OVERLOADED_OPERATOR_P (decl))
847 write_name (decl, /*ignore_local_scope=*/0);
848 else
849 write_source_name (DECL_NAME (decl));
850 return;
853 write_name (decl, /*ignore_local_scope=*/0);
854 if (TREE_CODE (decl) == FUNCTION_DECL)
856 tree fn_type;
857 tree d;
859 if (maybe_template_info (decl))
861 fn_type = get_mostly_instantiated_function_type (decl);
862 /* FN_TYPE will not have parameter types for in-charge or
863 VTT parameters. Therefore, we pass NULL_TREE to
864 write_bare_function_type -- otherwise, it will get
865 confused about which artificial parameters to skip. */
866 d = NULL_TREE;
868 else
870 fn_type = TREE_TYPE (decl);
871 d = decl;
874 write_bare_function_type (fn_type,
875 mangle_return_type_p (decl),
881 /* Interface to substitution and identifier mangling, used by the
882 module name mangler. */
884 void
885 mangle_module_substitution (int v)
887 write_substitution (v - 1);
891 mangle_module_component (tree comp, bool partition_p)
893 write_char ('W');
894 if (partition_p)
895 write_char ('P');
896 write_source_name (comp);
898 // Module substitutions use the same number-space as entity
899 // substitutions, but are orthogonal.
900 vec_safe_push (G.substitutions, NULL_TREE);
901 return G.substitutions->length ();
904 /* If the outermost non-namespace context (including DECL itself) is
905 a module-linkage decl, mangle the module information. For module
906 global initializers we need to include the partition part.
908 <module-name> ::= <module-sub>
909 || <subst>
910 || <module-name> <module-sub>
911 <module-sub> :: W [P] <unqualified-name>
914 static void
915 write_module (int m, bool include_partition)
917 G.mod = true;
918 mangle_module (m, include_partition);
921 static void
922 maybe_write_module (tree decl)
924 if (!DECL_NAMESPACE_SCOPE_P (decl))
925 return;
927 if (!TREE_PUBLIC (STRIP_TEMPLATE (decl)))
928 return;
930 if (TREE_CODE (decl) == NAMESPACE_DECL)
931 return;
933 int m = get_originating_module (decl, true);
934 if (m >= 0)
935 write_module (m, false);
938 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
939 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
941 static tree
942 decl_mangling_context (tree decl)
944 tree tcontext = targetm.cxx.decl_mangling_context (decl);
946 if (tcontext != NULL_TREE)
947 return tcontext;
949 if (TREE_CODE (decl) == TEMPLATE_DECL
950 && DECL_TEMPLATE_RESULT (decl))
951 decl = DECL_TEMPLATE_RESULT (decl);
953 if (TREE_CODE (decl) == TYPE_DECL
954 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
956 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
957 if (extra)
958 return extra;
960 else if (template_type_parameter_p (decl))
961 /* template type parms have no mangling context. */
962 return NULL_TREE;
964 tcontext = CP_DECL_CONTEXT (decl);
966 /* Ignore the artificial declare reduction functions. */
967 if (tcontext
968 && TREE_CODE (tcontext) == FUNCTION_DECL
969 && DECL_OMP_DECLARE_REDUCTION_P (tcontext))
970 return decl_mangling_context (tcontext);
972 return tcontext;
975 /* <name> ::= <unscoped-name>
976 ::= <unscoped-template-name> <template-args>
977 ::= <nested-name>
978 ::= <local-name>
980 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
981 called from <local-name>, which mangles the enclosing scope
982 elsewhere and then uses this function to mangle just the part
983 underneath the function scope. So don't use the <local-name>
984 production, to avoid an infinite recursion. */
986 static void
987 write_name (tree decl, const int ignore_local_scope)
989 tree context;
991 MANGLE_TRACE_TREE ("name", decl);
993 if (TREE_CODE (decl) == TYPE_DECL)
995 /* In case this is a typedef, fish out the corresponding
996 TYPE_DECL for the main variant. */
997 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
1000 context = decl_mangling_context (decl);
1002 gcc_assert (context != NULL_TREE);
1004 if (abi_warn_or_compat_version_crosses (7)
1005 && ignore_local_scope
1006 && TREE_CODE (context) == PARM_DECL)
1007 G.need_abi_warning = 1;
1009 /* A decl in :: or ::std scope is treated specially. The former is
1010 mangled using <unscoped-name> or <unscoped-template-name>, the
1011 latter with a special substitution. Also, a name that is
1012 directly in a local function scope is also mangled with
1013 <unscoped-name> rather than a full <nested-name>. */
1014 if (context == global_namespace
1015 || DECL_NAMESPACE_STD_P (context)
1016 || (ignore_local_scope
1017 && (TREE_CODE (context) == FUNCTION_DECL
1018 || (abi_version_at_least (7)
1019 && TREE_CODE (context) == PARM_DECL))))
1021 /* Is this a template instance? */
1022 if (tree info = maybe_template_info (decl))
1024 /* Yes: use <unscoped-template-name>. */
1025 write_unscoped_template_name (TI_TEMPLATE (info));
1026 write_template_args (TI_ARGS (info));
1028 else
1029 /* Everything else gets an <unqualified-name>. */
1030 write_unscoped_name (decl);
1032 else
1034 /* Handle local names, unless we asked not to (that is, invoked
1035 under <local-name>, to handle only the part of the name under
1036 the local scope). */
1037 if (!ignore_local_scope)
1039 /* Scan up the list of scope context, looking for a
1040 function. If we find one, this entity is in local
1041 function scope. local_entity tracks context one scope
1042 level down, so it will contain the element that's
1043 directly in that function's scope, either decl or one of
1044 its enclosing scopes. */
1045 tree local_entity = decl;
1046 while (context != global_namespace)
1048 /* Make sure we're always dealing with decls. */
1049 if (TYPE_P (context))
1050 context = TYPE_NAME (context);
1051 /* Is this a function? */
1052 if (TREE_CODE (context) == FUNCTION_DECL
1053 || TREE_CODE (context) == PARM_DECL)
1055 /* Yes, we have local scope. Use the <local-name>
1056 production for the innermost function scope. */
1057 write_local_name (context, local_entity, decl);
1058 return;
1060 /* Up one scope level. */
1061 local_entity = context;
1062 context = decl_mangling_context (context);
1065 /* No local scope found? Fall through to <nested-name>. */
1068 /* Other decls get a <nested-name> to encode their scope. */
1069 write_nested_name (decl);
1073 /* <unscoped-name> ::= <unqualified-name>
1074 ::= St <unqualified-name> # ::std:: */
1076 static void
1077 write_unscoped_name (const tree decl)
1079 tree context = decl_mangling_context (decl);
1081 MANGLE_TRACE_TREE ("unscoped-name", decl);
1083 /* Is DECL in ::std? */
1084 if (DECL_NAMESPACE_STD_P (context))
1086 write_string ("St");
1087 write_unqualified_name (decl);
1089 else
1091 /* If not, it should be either in the global namespace, or directly
1092 in a local function scope. A lambda can also be mangled in the
1093 scope of a default argument. */
1094 gcc_assert (context == global_namespace
1095 || TREE_CODE (context) == PARM_DECL
1096 || TREE_CODE (context) == FUNCTION_DECL);
1098 write_unqualified_name (decl);
1102 /* <unscoped-template-name> ::= <unscoped-name>
1103 ::= <substitution> */
1105 static void
1106 write_unscoped_template_name (const tree decl)
1108 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
1110 if (find_substitution (decl))
1111 return;
1112 write_unscoped_name (decl);
1113 add_substitution (decl);
1116 /* Write the nested name, including CV-qualifiers, of DECL.
1118 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1119 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1121 <ref-qualifier> ::= R # & ref-qualifier
1122 ::= O # && ref-qualifier
1123 <CV-qualifiers> ::= [r] [V] [K] */
1125 static void
1126 write_nested_name (const tree decl)
1128 MANGLE_TRACE_TREE ("nested-name", decl);
1130 write_char ('N');
1132 /* Write CV-qualifiers, if this is a member function. */
1133 if (TREE_CODE (decl) == FUNCTION_DECL
1134 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1136 if (DECL_VOLATILE_MEMFUNC_P (decl))
1137 write_char ('V');
1138 if (DECL_CONST_MEMFUNC_P (decl))
1139 write_char ('K');
1140 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
1142 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
1143 write_char ('O');
1144 else
1145 write_char ('R');
1149 /* Is this a template instance? */
1150 if (tree info = maybe_template_info (decl))
1152 /* Yes, use <template-prefix>. */
1153 write_template_prefix (decl);
1154 write_template_args (TI_ARGS (info));
1156 else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
1157 && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1159 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1160 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1162 write_template_prefix (decl);
1163 write_template_args (TREE_OPERAND (name, 1));
1165 else
1167 write_prefix (decl_mangling_context (decl));
1168 write_unqualified_name (decl);
1171 else
1173 /* No, just use <prefix> */
1174 write_prefix (decl_mangling_context (decl));
1175 write_unqualified_name (decl);
1177 write_char ('E');
1180 /* <prefix> ::= <prefix> <unqualified-name>
1181 ::= <template-param>
1182 ::= <template-prefix> <template-args>
1183 ::= <decltype>
1184 ::= # empty
1185 ::= <substitution> */
1187 static void
1188 write_prefix (const tree node)
1190 tree decl;
1192 if (node == NULL
1193 || node == global_namespace)
1194 return;
1196 MANGLE_TRACE_TREE ("prefix", node);
1198 if (TREE_CODE (node) == DECLTYPE_TYPE)
1200 write_type (node);
1201 return;
1204 if (find_substitution (node))
1205 return;
1207 tree template_info = NULL_TREE;
1208 if (DECL_P (node))
1210 /* If this is a function or parm decl, that means we've hit function
1211 scope, so this prefix must be for a local name. In this
1212 case, we're under the <local-name> production, which encodes
1213 the enclosing function scope elsewhere. So don't continue
1214 here. */
1215 if (TREE_CODE (node) == FUNCTION_DECL
1216 || TREE_CODE (node) == PARM_DECL)
1217 return;
1219 decl = node;
1220 template_info = maybe_template_info (decl);
1222 else
1224 /* Node is a type. */
1225 decl = TYPE_NAME (node);
1226 /* The DECL might not point at the node. */
1227 if (CLASSTYPE_TEMPLATE_ID_P (node))
1228 template_info = TYPE_TEMPLATE_INFO (node);
1231 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1232 write_template_param (node);
1233 else if (template_info)
1234 /* Templated. */
1236 write_template_prefix (decl);
1237 write_template_args (TI_ARGS (template_info));
1239 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1241 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1242 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1244 write_template_prefix (decl);
1245 write_template_args (TREE_OPERAND (name, 1));
1247 else
1249 write_prefix (decl_mangling_context (decl));
1250 write_unqualified_name (decl);
1253 else
1254 /* Not templated. */
1256 write_prefix (decl_mangling_context (decl));
1257 write_unqualified_name (decl);
1258 if (VAR_P (decl)
1259 || TREE_CODE (decl) == FIELD_DECL)
1261 /* <data-member-prefix> := <member source-name> M */
1262 write_char ('M');
1264 /* Before ABI 18, we did not count these as substitution
1265 candidates. This leads to incorrect demanglings (and
1266 ABI divergence to other compilers). */
1267 if (abi_warn_or_compat_version_crosses (18))
1268 G.need_abi_warning = true;
1269 if (!abi_version_at_least (18))
1270 return;
1274 add_substitution (node);
1277 /* <template-prefix> ::= <prefix> <template component>
1278 ::= <template-param>
1279 ::= <substitution> */
1281 static void
1282 write_template_prefix (const tree node)
1284 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1285 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1286 tree context = decl_mangling_context (decl);
1287 tree templ;
1288 tree substitution;
1290 MANGLE_TRACE_TREE ("template-prefix", node);
1292 /* Find the template decl. */
1293 if (tree info = maybe_template_info (decl))
1294 templ = TI_TEMPLATE (info);
1295 else if (TREE_CODE (type) == TYPENAME_TYPE)
1296 /* For a typename type, all we have is the name. */
1297 templ = DECL_NAME (decl);
1298 else
1300 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1302 templ = TYPE_TI_TEMPLATE (type);
1305 /* For a member template, though, the template name for the
1306 innermost name must have all the outer template levels
1307 instantiated. For instance, consider
1309 template<typename T> struct Outer {
1310 template<typename U> struct Inner {};
1313 The template name for `Inner' in `Outer<int>::Inner<float>' is
1314 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1315 levels separately, so there's no TEMPLATE_DECL available for this
1316 (there's only `Outer<T>::Inner<U>').
1318 In order to get the substitutions right, we create a special
1319 TREE_LIST to represent the substitution candidate for a nested
1320 template. The TREE_PURPOSE is the template's context, fully
1321 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1322 template.
1324 So, for the example above, `Outer<int>::Inner' is represented as a
1325 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1326 and whose value is `Outer<T>::Inner<U>'. */
1327 if (context && TYPE_P (context))
1328 substitution = build_tree_list (context, templ);
1329 else
1330 substitution = templ;
1332 if (find_substitution (substitution))
1333 return;
1335 if (TREE_TYPE (templ)
1336 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1337 write_template_param (TREE_TYPE (templ));
1338 else
1340 write_prefix (context);
1341 write_unqualified_name (decl);
1344 add_substitution (substitution);
1347 /* As the list of identifiers for the structured binding declaration
1348 DECL is likely gone, try to recover the DC <source-name>+ E portion
1349 from its mangled name. Return pointer to the DC and set len to
1350 the length up to and including the terminating E. On failure
1351 return NULL. */
1353 static const char *
1354 find_decomp_unqualified_name (tree decl, size_t *len)
1356 const char *p = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1357 const char *end = p + IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (decl));
1358 bool nested = false;
1359 if (!startswith (p, "_Z"))
1360 return NULL;
1361 p += 2;
1362 if (startswith (p, "St"))
1363 p += 2;
1364 else if (*p == 'N')
1366 nested = true;
1367 ++p;
1368 while (ISDIGIT (p[0]))
1370 char *e;
1371 long num = strtol (p, &e, 10);
1372 if (num >= 1 && num < end - e)
1373 p = e + num;
1374 else
1375 break;
1378 if (!startswith (p, "DC"))
1379 return NULL;
1380 if (nested)
1382 if (end[-1] != 'E')
1383 return NULL;
1384 --end;
1386 if (end[-1] != 'E')
1387 return NULL;
1388 *len = end - p;
1389 return p;
1392 /* "For the purposes of mangling, the name of an anonymous union is considered
1393 to be the name of the first named data member found by a pre-order,
1394 depth-first, declaration-order walk of the data members of the anonymous
1395 union. If there is no such data member (i.e., if all of the data members in
1396 the union are unnamed), then there is no way for a program to refer to the
1397 anonymous union, and there is therefore no need to mangle its name." */
1399 static tree
1400 anon_aggr_naming_decl (tree type)
1402 tree field = next_aggregate_field (TYPE_FIELDS (type));
1403 for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1405 if (DECL_NAME (field))
1406 return field;
1407 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1408 if (tree sub = anon_aggr_naming_decl (TREE_TYPE (field)))
1409 return sub;
1411 return NULL_TREE;
1414 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1415 mangled through special entry points.
1417 <unqualified-name> ::= [<module-name>] <operator-name>
1418 ::= <special-name>
1419 ::= [<module-name>] <source-name>
1420 ::= [<module-name>] <unnamed-type-name>
1421 ::= <local-source-name>
1423 <local-source-name> ::= L <source-name> <discriminator> */
1425 static void
1426 write_unqualified_id (tree identifier)
1428 if (IDENTIFIER_CONV_OP_P (identifier))
1429 write_conversion_operator_name (TREE_TYPE (identifier));
1430 else if (IDENTIFIER_OVL_OP_P (identifier))
1432 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier);
1433 write_string (ovl_op->mangled_name);
1435 else if (UDLIT_OPER_P (identifier))
1436 write_literal_operator_name (identifier);
1437 else
1438 write_source_name (identifier);
1441 static void
1442 write_unqualified_name (tree decl)
1444 MANGLE_TRACE_TREE ("unqualified-name", decl);
1446 if (modules_p ())
1447 maybe_write_module (decl);
1449 if (identifier_p (decl))
1451 write_unqualified_id (decl);
1452 return;
1455 bool found = false;
1457 if (DECL_NAME (decl) == NULL_TREE
1458 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1459 decl = anon_aggr_naming_decl (TREE_TYPE (decl));
1460 else if (DECL_NAME (decl) == NULL_TREE)
1462 found = true;
1463 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1464 const char *decomp_str = NULL;
1465 size_t decomp_len = 0;
1466 if (VAR_P (decl)
1467 && DECL_DECOMPOSITION_P (decl)
1468 && DECL_NAME (decl) == NULL_TREE
1469 && DECL_NAMESPACE_SCOPE_P (decl))
1470 decomp_str = find_decomp_unqualified_name (decl, &decomp_len);
1471 if (decomp_str)
1472 write_chars (decomp_str, decomp_len);
1473 else
1474 write_source_name (DECL_ASSEMBLER_NAME (decl));
1476 else if (DECL_DECLARES_FUNCTION_P (decl))
1478 found = true;
1479 if (DECL_CONSTRUCTOR_P (decl))
1480 write_special_name_constructor (decl);
1481 else if (DECL_DESTRUCTOR_P (decl))
1482 write_special_name_destructor (decl);
1483 else if (DECL_CONV_FN_P (decl))
1485 /* Conversion operator. Handle it right here.
1486 <operator> ::= cv <type> */
1487 tree type;
1488 if (maybe_template_info (decl))
1490 tree fn_type;
1491 fn_type = get_mostly_instantiated_function_type (decl);
1492 type = TREE_TYPE (fn_type);
1494 else if (FNDECL_USED_AUTO (decl))
1495 type = DECL_SAVED_AUTO_RETURN_TYPE (decl);
1496 else
1497 type = DECL_CONV_FN_TYPE (decl);
1498 write_conversion_operator_name (type);
1500 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1502 tree t;
1503 if (!(t = DECL_RAMP_FN (decl)))
1504 t = decl;
1505 const char *mangled_name
1506 = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (t)]
1507 [DECL_OVERLOADED_OPERATOR_CODE_RAW (t)].mangled_name);
1508 write_string (mangled_name);
1510 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1511 write_literal_operator_name (DECL_NAME (decl));
1512 else
1513 found = false;
1516 if (found)
1517 /* OK */;
1518 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1519 && DECL_NAMESPACE_SCOPE_P (decl)
1520 && decl_linkage (decl) == lk_internal)
1522 MANGLE_TRACE_TREE ("local-source-name", decl);
1523 write_char ('L');
1524 write_source_name (DECL_NAME (decl));
1525 /* The default discriminator is 1, and that's all we ever use,
1526 so there's no code to output one here. */
1528 else
1530 tree type = TREE_TYPE (decl);
1532 if (TREE_CODE (decl) == TYPE_DECL
1533 && TYPE_UNNAMED_P (type))
1534 write_unnamed_type_name (type);
1535 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type))
1536 write_closure_type_name (type);
1537 else
1538 write_source_name (DECL_NAME (decl));
1541 /* We use the ABI tags from the primary class template, ignoring tags on any
1542 specializations. This is necessary because C++ doesn't require a
1543 specialization to be declared before it is used unless the use requires a
1544 complete type, but we need to get the tags right on incomplete types as
1545 well. */
1546 if (tree tmpl = most_general_template (decl))
1548 tree res = DECL_TEMPLATE_RESULT (tmpl);
1549 if (res == NULL_TREE)
1550 /* UNBOUND_CLASS_TEMPLATE. */;
1551 else if (DECL_DECLARES_TYPE_P (decl))
1552 decl = res;
1553 else if (any_abi_below (11))
1555 /* ABI v10 implicit tags on the template. */
1556 tree mtags = missing_abi_tags (res);
1557 /* Explicit tags on the template. */
1558 tree ttags = get_abi_tags (res);
1559 /* Tags on the instantiation. */
1560 tree dtags = get_abi_tags (decl);
1562 if (mtags && abi_warn_or_compat_version_crosses (10))
1563 G.need_abi_warning = 1;
1565 /* Add the v10 tags to the explicit tags now. */
1566 mtags = chainon (mtags, ttags);
1568 if (!G.need_abi_warning
1569 && abi_warn_or_compat_version_crosses (11)
1570 && !equal_abi_tags (dtags, mtags))
1571 G.need_abi_warning = 1;
1573 if (!abi_version_at_least (10))
1574 /* In abi <10, we only got the explicit tags. */
1575 decl = res;
1576 else if (flag_abi_version == 10)
1578 /* In ABI 10, we want explict and implicit tags. */
1579 write_abi_tags (mtags);
1580 return;
1585 tree tags = get_abi_tags (decl);
1586 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
1587 && any_abi_below (11))
1588 if (tree mtags = missing_abi_tags (decl))
1590 if (abi_warn_or_compat_version_crosses (11))
1591 G.need_abi_warning = true;
1592 if (!abi_version_at_least (11))
1593 tags = chainon (mtags, tags);
1595 write_abi_tags (tags);
1598 /* Write the unqualified-name for a conversion operator to TYPE. */
1600 static void
1601 write_conversion_operator_name (const tree type)
1603 write_string ("cv");
1604 write_type (type);
1607 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1609 <source-name> ::= </length/ number> <identifier> */
1611 static void
1612 write_source_name (tree identifier)
1614 MANGLE_TRACE_TREE ("source-name", identifier);
1616 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1617 write_identifier (IDENTIFIER_POINTER (identifier));
1620 /* Compare two TREE_STRINGs like strcmp. */
1623 tree_string_cmp (const void *p1, const void *p2)
1625 if (p1 == p2)
1626 return 0;
1627 tree s1 = *(const tree*)p1;
1628 tree s2 = *(const tree*)p2;
1629 return strcmp (TREE_STRING_POINTER (s1),
1630 TREE_STRING_POINTER (s2));
1633 /* Return the TREE_LIST of TAGS as a sorted VEC. */
1635 static vec<tree, va_gc> *
1636 sorted_abi_tags (tree tags)
1638 vec<tree, va_gc> * vec = make_tree_vector();
1640 for (tree t = tags; t; t = TREE_CHAIN (t))
1642 if (ABI_TAG_IMPLICIT (t))
1643 continue;
1644 tree str = TREE_VALUE (t);
1645 vec_safe_push (vec, str);
1648 vec->qsort (tree_string_cmp);
1650 return vec;
1653 /* ID is the name of a function or type with abi_tags attribute TAGS.
1654 Write out the name, suitably decorated. */
1656 static void
1657 write_abi_tags (tree tags)
1659 if (tags == NULL_TREE)
1660 return;
1662 vec<tree, va_gc> * vec = sorted_abi_tags (tags);
1664 unsigned i; tree str;
1665 FOR_EACH_VEC_ELT (*vec, i, str)
1667 write_string ("B");
1668 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1669 write_identifier (TREE_STRING_POINTER (str));
1672 release_tree_vector (vec);
1675 /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */
1677 static bool
1678 equal_abi_tags (tree t1, tree t2)
1680 releasing_vec v1 = sorted_abi_tags (t1);
1681 releasing_vec v2 = sorted_abi_tags (t2);
1683 unsigned len1 = v1->length();
1684 if (len1 != v2->length())
1685 return false;
1686 for (unsigned i = 0; i < len1; ++i)
1687 if (tree_string_cmp (v1[i], v2[i]) != 0)
1688 return false;
1689 return true;
1692 /* Write a user-defined literal operator.
1693 ::= li <source-name> # "" <source-name>
1694 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1696 static void
1697 write_literal_operator_name (tree identifier)
1699 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1700 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1701 write_unsigned_number (strlen (suffix));
1702 write_identifier (suffix);
1705 /* Encode 0 as _, and 1+ as n-1_. */
1707 static void
1708 write_compact_number (int num)
1710 gcc_checking_assert (num >= 0);
1711 if (num > 0)
1712 write_unsigned_number (num - 1);
1713 write_char ('_');
1716 /* Return how many unnamed types precede TYPE in its enclosing class. */
1718 static int
1719 nested_anon_class_index (tree type)
1721 int index = 0;
1722 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1723 for (; member; member = DECL_CHAIN (member))
1724 if (DECL_IMPLICIT_TYPEDEF_P (member))
1726 tree memtype = TREE_TYPE (member);
1727 if (memtype == type)
1728 return index;
1729 else if (TYPE_UNNAMED_P (memtype))
1730 ++index;
1733 if (seen_error ())
1734 return -1;
1736 gcc_unreachable ();
1739 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1741 static void
1742 write_unnamed_type_name (const tree type)
1744 int discriminator;
1745 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1747 if (TYPE_FUNCTION_SCOPE_P (type))
1748 discriminator = discriminator_for_local_entity (TYPE_NAME (type));
1749 else if (TYPE_CLASS_SCOPE_P (type))
1750 discriminator = nested_anon_class_index (type);
1751 else
1753 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1754 /* Just use the old mangling at namespace scope. */
1755 write_source_name (TYPE_IDENTIFIER (type));
1756 return;
1759 write_string ("Ut");
1760 write_compact_number (discriminator);
1763 // A template head, for templated lambdas.
1764 // <template-head> ::= Tp* Ty
1765 // Tp* Tn <type>
1766 // Tp* Tt <template-head> E
1767 // New in ABI=18. Returns true iff we emitted anything -- used for ABI
1768 // version warning.
1770 static bool
1771 write_closure_template_head (tree tmpl)
1773 bool any = false;
1775 // We only need one level of template parms
1776 tree inner = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
1778 for (int ix = 0, len = TREE_VEC_LENGTH (inner); ix != len; ix++)
1780 tree parm = TREE_VEC_ELT (inner, ix);
1781 if (parm == error_mark_node)
1782 continue;
1783 parm = TREE_VALUE (parm);
1785 if (DECL_VIRTUAL_P (parm))
1786 // A synthetic parm, we're done.
1787 break;
1789 any = true;
1790 if (abi_version_at_least (18))
1792 if (TREE_CODE (parm) == PARM_DECL
1793 ? TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
1794 : TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
1795 write_string ("Tp");
1797 switch (TREE_CODE (parm))
1799 default:
1800 gcc_unreachable ();
1802 case TYPE_DECL:
1803 write_string ("Ty");
1804 break;
1806 case PARM_DECL:
1807 write_string ("Tn");
1808 write_type (TREE_TYPE (parm));
1809 break;
1811 case TEMPLATE_DECL:
1812 write_string ("Tt");
1813 write_closure_template_head (parm);
1814 write_string ("E");
1815 break;
1820 return any;
1823 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1824 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1826 static void
1827 write_closure_type_name (const tree type)
1829 tree fn = lambda_function (type);
1830 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1831 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1833 MANGLE_TRACE_TREE ("closure-type-name", type);
1835 write_string ("Ul");
1837 if (auto ti = maybe_template_info (fn))
1838 if (write_closure_template_head (TI_TEMPLATE (ti)))
1839 // If there were any explicit template parms, we may need to
1840 // issue a mangling diagnostic.
1841 if (abi_warn_or_compat_version_crosses (18))
1842 G.need_abi_warning = true;
1844 write_method_parms (parms, TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE, fn);
1845 write_char ('E');
1846 if ((LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
1847 != LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda))
1848 && abi_warn_or_compat_version_crosses (18))
1849 G.need_abi_warning = true;
1850 write_compact_number (abi_version_at_least (18)
1851 ? LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
1852 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda));
1855 /* Convert NUMBER to ascii using base BASE and generating at least
1856 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1857 into which to store the characters. Returns the number of
1858 characters generated (these will be laid out in advance of where
1859 BUFFER points). */
1861 static int
1862 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1863 char *buffer, const unsigned int min_digits)
1865 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1866 unsigned digits = 0;
1868 while (number)
1870 unsigned HOST_WIDE_INT d = number / base;
1872 *--buffer = base_digits[number - d * base];
1873 digits++;
1874 number = d;
1876 while (digits < min_digits)
1878 *--buffer = base_digits[0];
1879 digits++;
1881 return digits;
1884 /* Non-terminal <number>.
1886 <number> ::= [n] </decimal integer/> */
1888 static void
1889 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1890 const unsigned int base)
1892 char buffer[sizeof (HOST_WIDE_INT) * 8];
1893 unsigned count = 0;
1895 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1897 write_char ('n');
1898 number = -((HOST_WIDE_INT) number);
1900 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1901 write_chars (buffer + sizeof (buffer) - count, count);
1904 /* Write out an integral CST in decimal. Most numbers are small, and
1905 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1906 bigger than that, which we must deal with. */
1908 static inline void
1909 write_integer_cst (const tree cst)
1911 int sign = tree_int_cst_sgn (cst);
1912 widest_int abs_value = wi::abs (wi::to_widest (cst));
1913 if (!wi::fits_uhwi_p (abs_value))
1915 /* A bignum. We do this in chunks, each of which fits in a
1916 HOST_WIDE_INT. */
1917 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1918 unsigned HOST_WIDE_INT chunk;
1919 unsigned chunk_digits;
1920 char *ptr = buffer + sizeof (buffer);
1921 unsigned count = 0;
1922 tree n, base, type;
1923 int done;
1925 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1926 representable. */
1927 chunk = 1000000000;
1928 chunk_digits = 9;
1930 if (sizeof (HOST_WIDE_INT) >= 8)
1932 /* It is at least 64 bits, so 10^18 is representable. */
1933 chunk_digits = 18;
1934 chunk *= chunk;
1937 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1938 base = build_int_cstu (type, chunk);
1939 n = wide_int_to_tree (type, wi::to_wide (cst));
1941 if (sign < 0)
1943 write_char ('n');
1944 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1948 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1949 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1950 unsigned c;
1952 done = integer_zerop (d);
1953 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1954 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1955 done ? 1 : chunk_digits);
1956 ptr -= c;
1957 count += c;
1958 n = d;
1960 while (!done);
1961 write_chars (ptr, count);
1963 else
1965 /* A small num. */
1966 if (sign < 0)
1967 write_char ('n');
1968 write_unsigned_number (abs_value.to_uhwi ());
1972 /* Write out a floating-point literal.
1974 "Floating-point literals are encoded using the bit pattern of the
1975 target processor's internal representation of that number, as a
1976 fixed-length lowercase hexadecimal string, high-order bytes first
1977 (even if the target processor would store low-order bytes first).
1978 The "n" prefix is not used for floating-point literals; the sign
1979 bit is encoded with the rest of the number.
1981 Here are some examples, assuming the IEEE standard representation
1982 for floating point numbers. (Spaces are for readability, not
1983 part of the encoding.)
1985 1.0f Lf 3f80 0000 E
1986 -1.0f Lf bf80 0000 E
1987 1.17549435e-38f Lf 0080 0000 E
1988 1.40129846e-45f Lf 0000 0001 E
1989 0.0f Lf 0000 0000 E"
1991 Caller is responsible for the Lx and the E. */
1992 static void
1993 write_real_cst (const tree value)
1995 long target_real[4]; /* largest supported float */
1996 /* Buffer for eight hex digits in a 32-bit number but big enough
1997 even for 64-bit long to avoid warnings. */
1998 char buffer[17];
1999 int i, limit, dir;
2001 tree type = TREE_TYPE (value);
2002 int words = GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type)) / 32;
2004 real_to_target (target_real, &TREE_REAL_CST (value),
2005 TYPE_MODE (type));
2007 /* The value in target_real is in the target word order,
2008 so we must write it out backward if that happens to be
2009 little-endian. write_number cannot be used, it will
2010 produce uppercase. */
2011 if (FLOAT_WORDS_BIG_ENDIAN)
2012 i = 0, limit = words, dir = 1;
2013 else
2014 i = words - 1, limit = -1, dir = -1;
2016 for (; i != limit; i += dir)
2018 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
2019 write_chars (buffer, 8);
2023 /* Non-terminal <identifier>.
2025 <identifier> ::= </unqualified source code identifier> */
2027 static void
2028 write_identifier (const char *identifier)
2030 MANGLE_TRACE ("identifier", identifier);
2031 write_string (identifier);
2034 /* Handle constructor productions of non-terminal <special-name>.
2035 CTOR is a constructor FUNCTION_DECL.
2037 <special-name> ::= C1 # complete object constructor
2038 ::= C2 # base object constructor
2039 ::= C3 # complete object allocating constructor
2041 Currently, allocating constructors are never used. */
2043 static void
2044 write_special_name_constructor (const tree ctor)
2046 write_char ('C');
2047 bool new_inh = (flag_new_inheriting_ctors
2048 && DECL_INHERITED_CTOR (ctor));
2049 if (new_inh)
2050 write_char ('I');
2051 if (DECL_BASE_CONSTRUCTOR_P (ctor))
2052 write_char ('2');
2053 /* This is the old-style "[unified]" constructor.
2054 In some cases, we may emit this function and call
2055 it from the clones in order to share code and save space. */
2056 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
2057 write_char ('4');
2058 else
2060 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
2061 write_char ('1');
2063 if (new_inh)
2064 write_type (DECL_INHERITED_CTOR_BASE (ctor));
2067 /* Handle destructor productions of non-terminal <special-name>.
2068 DTOR is a destructor FUNCTION_DECL.
2070 <special-name> ::= D0 # deleting (in-charge) destructor
2071 ::= D1 # complete object (in-charge) destructor
2072 ::= D2 # base object (not-in-charge) destructor */
2074 static void
2075 write_special_name_destructor (const tree dtor)
2077 if (DECL_DELETING_DESTRUCTOR_P (dtor))
2078 write_string ("D0");
2079 else if (DECL_BASE_DESTRUCTOR_P (dtor))
2080 write_string ("D2");
2081 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
2082 /* This is the old-style "[unified]" destructor.
2083 In some cases, we may emit this function and call
2084 it from the clones in order to share code and save space. */
2085 write_string ("D4");
2086 else
2088 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
2089 write_string ("D1");
2093 /* Return the discriminator for ENTITY appearing inside
2094 FUNCTION. The discriminator is the lexical ordinal of VAR or TYPE among
2095 entities with the same name and kind in the same FUNCTION. */
2097 static int
2098 discriminator_for_local_entity (tree entity)
2100 if (!DECL_LANG_SPECIFIC (entity))
2102 /* Some decls, like __FUNCTION__, don't need a discriminator. */
2103 gcc_checking_assert (DECL_ARTIFICIAL (entity));
2104 return 0;
2106 else if (tree disc = DECL_DISCRIMINATOR (entity))
2107 return TREE_INT_CST_LOW (disc);
2108 else
2109 /* The first entity with a particular name doesn't get
2110 DECL_DISCRIMINATOR set up. */
2111 return 0;
2114 /* Return the discriminator for STRING, a string literal used inside
2115 FUNCTION. The discriminator is the lexical ordinal of STRING among
2116 string literals used in FUNCTION. */
2118 static int
2119 discriminator_for_string_literal (tree /*function*/,
2120 tree /*string*/)
2122 /* For now, we don't discriminate amongst string literals. */
2123 return 0;
2126 /* <discriminator> := _ <number> # when number < 10
2127 := __ <number> _ # when number >= 10
2129 The discriminator is used only for the second and later occurrences
2130 of the same name within a single function. In this case <number> is
2131 n - 2, if this is the nth occurrence, in lexical order. */
2133 static void
2134 write_discriminator (const int discriminator)
2136 /* If discriminator is zero, don't write anything. Otherwise... */
2137 if (discriminator > 0)
2139 write_char ('_');
2140 if (discriminator - 1 >= 10)
2142 if (abi_warn_or_compat_version_crosses (11))
2143 G.need_abi_warning = 1;
2144 if (abi_version_at_least (11))
2145 write_char ('_');
2147 write_unsigned_number (discriminator - 1);
2148 if (abi_version_at_least (11) && discriminator - 1 >= 10)
2149 write_char ('_');
2153 /* Mangle the name of a function-scope entity. FUNCTION is the
2154 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
2155 default argument scope. ENTITY is the decl for the entity itself.
2156 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
2157 either ENTITY itself or an enclosing scope of ENTITY.
2159 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2160 := Z <function encoding> E s [<discriminator>]
2161 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
2163 static void
2164 write_local_name (tree function, const tree local_entity,
2165 const tree entity)
2167 tree parm = NULL_TREE;
2169 MANGLE_TRACE_TREE ("local-name", entity);
2171 if (TREE_CODE (function) == PARM_DECL)
2173 parm = function;
2174 function = DECL_CONTEXT (parm);
2177 write_char ('Z');
2178 write_encoding (function);
2179 write_char ('E');
2181 /* For this purpose, parameters are numbered from right-to-left. */
2182 if (parm)
2184 int i = list_length (parm);
2185 write_char ('d');
2186 write_compact_number (i - 1);
2189 if (TREE_CODE (entity) == STRING_CST)
2191 write_char ('s');
2192 write_discriminator (discriminator_for_string_literal (function,
2193 entity));
2195 else
2197 /* Now the <entity name>. Let write_name know its being called
2198 from <local-name>, so it doesn't try to process the enclosing
2199 function scope again. */
2200 write_name (entity, /*ignore_local_scope=*/1);
2201 if (DECL_DISCRIMINATOR_P (local_entity)
2202 && !(TREE_CODE (local_entity) == TYPE_DECL
2203 && TYPE_ANON_P (TREE_TYPE (local_entity))))
2204 write_discriminator (discriminator_for_local_entity (local_entity));
2208 /* Non-terminals <type> and <CV-qualifier>.
2210 <type> ::= <builtin-type>
2211 ::= <function-type>
2212 ::= <class-enum-type>
2213 ::= <array-type>
2214 ::= <pointer-to-member-type>
2215 ::= <template-param>
2216 ::= <substitution>
2217 ::= <CV-qualifier>
2218 ::= P <type> # pointer-to
2219 ::= R <type> # reference-to
2220 ::= C <type> # complex pair (C 2000)
2221 ::= G <type> # imaginary (C 2000) [not supported]
2222 ::= U <source-name> <type> # vendor extended type qualifier
2224 C++0x extensions
2226 <type> ::= RR <type> # rvalue reference-to
2227 <type> ::= Dt <expression> # decltype of an id-expression or
2228 # class member access
2229 <type> ::= DT <expression> # decltype of an expression
2230 <type> ::= Dn # decltype of nullptr
2232 TYPE is a type node. */
2234 static void
2235 write_type (tree type)
2237 /* This gets set to nonzero if TYPE turns out to be a (possibly
2238 CV-qualified) builtin type. */
2239 int is_builtin_type = 0;
2241 MANGLE_TRACE_TREE ("type", type);
2243 if (type == error_mark_node)
2244 return;
2246 type = canonicalize_for_substitution (type);
2247 if (find_substitution (type))
2248 return;
2251 if (write_CV_qualifiers_for_type (type) > 0)
2252 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
2253 mangle the unqualified type. The recursive call is needed here
2254 since both the qualified and unqualified types are substitution
2255 candidates. */
2257 tree t = TYPE_MAIN_VARIANT (type);
2258 if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t))
2260 tree attrs = NULL_TREE;
2261 if (tx_safe_fn_type_p (type))
2262 attrs = tree_cons (get_identifier ("transaction_safe"),
2263 NULL_TREE, attrs);
2264 t = cp_build_type_attribute_variant (t, attrs);
2266 gcc_assert (t != type);
2267 if (FUNC_OR_METHOD_TYPE_P (t))
2269 t = build_ref_qualified_type (t, type_memfn_rqual (type));
2270 if (flag_noexcept_type)
2272 tree r = TYPE_RAISES_EXCEPTIONS (type);
2273 t = build_exception_variant (t, r);
2275 if (abi_version_at_least (8)
2276 || type == TYPE_MAIN_VARIANT (type))
2277 /* Avoid adding the unqualified function type as a substitution. */
2278 write_function_type (t);
2279 else
2280 write_type (t);
2281 if (abi_warn_or_compat_version_crosses (8))
2282 G.need_abi_warning = 1;
2284 else
2285 write_type (t);
2287 else if (TREE_CODE (type) == ARRAY_TYPE)
2288 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
2289 so that the cv-qualification of the element type is available
2290 in write_array_type. */
2291 write_array_type (type);
2292 else
2294 tree type_orig = type;
2296 /* See through any typedefs. */
2297 type = TYPE_MAIN_VARIANT (type);
2298 if (FUNC_OR_METHOD_TYPE_P (type))
2299 type = cxx_copy_lang_qualifiers (type, type_orig);
2301 /* According to the C++ ABI, some library classes are passed the
2302 same as the scalar type of their single member and use the same
2303 mangling. */
2304 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2305 type = TREE_TYPE (first_field (type));
2307 if (TYPE_PTRDATAMEM_P (type))
2308 write_pointer_to_member_type (type);
2309 else
2311 /* Handle any target-specific fundamental types. */
2312 const char *target_mangling
2313 = targetm.mangle_type (type_orig);
2315 if (target_mangling)
2317 write_string (target_mangling);
2318 /* Add substitutions for types other than fundamental
2319 types. */
2320 if (!VOID_TYPE_P (type)
2321 && TREE_CODE (type) != INTEGER_TYPE
2322 && TREE_CODE (type) != REAL_TYPE
2323 && TREE_CODE (type) != BOOLEAN_TYPE)
2324 add_substitution (type);
2325 return;
2328 switch (TREE_CODE (type))
2330 case VOID_TYPE:
2331 case BOOLEAN_TYPE:
2332 case INTEGER_TYPE: /* Includes wchar_t. */
2333 case REAL_TYPE:
2334 case FIXED_POINT_TYPE:
2336 /* If this is a typedef, TYPE may not be one of
2337 the standard builtin type nodes, but an alias of one. Use
2338 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
2339 write_builtin_type (TYPE_MAIN_VARIANT (type));
2340 ++is_builtin_type;
2342 break;
2344 case COMPLEX_TYPE:
2345 write_char ('C');
2346 write_type (TREE_TYPE (type));
2347 break;
2349 case FUNCTION_TYPE:
2350 case METHOD_TYPE:
2351 write_function_type (type);
2352 break;
2354 case UNION_TYPE:
2355 case RECORD_TYPE:
2356 case ENUMERAL_TYPE:
2357 /* A pointer-to-member function is represented as a special
2358 RECORD_TYPE, so check for this first. */
2359 if (TYPE_PTRMEMFUNC_P (type))
2360 write_pointer_to_member_type (type);
2361 else
2362 write_class_enum_type (type);
2363 break;
2365 case TYPENAME_TYPE:
2366 case UNBOUND_CLASS_TEMPLATE:
2367 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2368 ordinary nested names. */
2369 write_nested_name (TYPE_STUB_DECL (type));
2370 break;
2372 case POINTER_TYPE:
2373 case REFERENCE_TYPE:
2374 if (TYPE_PTR_P (type))
2375 write_char ('P');
2376 else if (TYPE_REF_IS_RVALUE (type))
2377 write_char ('O');
2378 else
2379 write_char ('R');
2381 tree target = TREE_TYPE (type);
2382 /* Attribute const/noreturn are not reflected in mangling.
2383 We strip them here rather than at a lower level because
2384 a typedef or template argument can have function type
2385 with function-cv-quals (that use the same representation),
2386 but you can't have a pointer/reference to such a type. */
2387 if (TREE_CODE (target) == FUNCTION_TYPE)
2389 if (abi_warn_or_compat_version_crosses (5)
2390 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2391 G.need_abi_warning = 1;
2392 if (abi_version_at_least (5))
2393 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2395 write_type (target);
2397 break;
2399 case TEMPLATE_TYPE_PARM:
2400 if (is_auto (type))
2402 if (AUTO_IS_DECLTYPE (type))
2403 write_identifier ("Dc");
2404 else
2405 write_identifier ("Da");
2406 ++is_builtin_type;
2407 break;
2409 /* fall through. */
2410 case TEMPLATE_PARM_INDEX:
2411 write_template_param (type);
2412 break;
2414 case TEMPLATE_TEMPLATE_PARM:
2415 write_template_template_param (type);
2416 break;
2418 case BOUND_TEMPLATE_TEMPLATE_PARM:
2419 write_template_template_param (type);
2420 write_template_args
2421 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2422 break;
2424 case VECTOR_TYPE:
2425 if (abi_version_at_least (4))
2427 write_string ("Dv");
2428 /* Non-constant vector size would be encoded with
2429 _ expression, but we don't support that yet. */
2430 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type)
2431 .to_constant ());
2432 write_char ('_');
2434 else
2435 write_string ("U8__vector");
2436 if (abi_warn_or_compat_version_crosses (4))
2437 G.need_abi_warning = 1;
2438 write_type (TREE_TYPE (type));
2439 break;
2441 case TYPE_PACK_EXPANSION:
2442 write_string ("Dp");
2443 write_type (PACK_EXPANSION_PATTERN (type));
2444 break;
2446 case DECLTYPE_TYPE:
2447 /* These shouldn't make it into mangling. */
2448 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2449 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2451 /* In ABI <5, we stripped decltype of a plain decl. */
2452 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2454 tree expr = DECLTYPE_TYPE_EXPR (type);
2455 tree etype = NULL_TREE;
2456 switch (TREE_CODE (expr))
2458 case VAR_DECL:
2459 case PARM_DECL:
2460 case RESULT_DECL:
2461 case FUNCTION_DECL:
2462 case CONST_DECL:
2463 case TEMPLATE_PARM_INDEX:
2464 etype = TREE_TYPE (expr);
2465 break;
2467 default:
2468 break;
2471 if (etype && !type_uses_auto (etype))
2473 if (abi_warn_or_compat_version_crosses (5))
2474 G.need_abi_warning = 1;
2475 if (!abi_version_at_least (5))
2477 write_type (etype);
2478 return;
2483 write_char ('D');
2484 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2485 write_char ('t');
2486 else
2487 write_char ('T');
2488 ++cp_unevaluated_operand;
2489 write_expression (DECLTYPE_TYPE_EXPR (type));
2490 --cp_unevaluated_operand;
2491 write_char ('E');
2492 break;
2494 case NULLPTR_TYPE:
2495 write_string ("Dn");
2496 if (abi_version_at_least (7))
2497 ++is_builtin_type;
2498 if (abi_warn_or_compat_version_crosses (7))
2499 G.need_abi_warning = 1;
2500 break;
2502 case TYPEOF_TYPE:
2503 sorry ("mangling %<typeof%>, use %<decltype%> instead");
2504 break;
2506 case TRAIT_TYPE:
2507 error ("use of built-in trait %qT in function signature; "
2508 "use library traits instead", type);
2509 break;
2511 case LANG_TYPE:
2512 /* fall through. */
2514 default:
2515 gcc_unreachable ();
2520 /* Types other than builtin types are substitution candidates. */
2521 if (!is_builtin_type)
2522 add_substitution (type);
2525 /* qsort callback for sorting a vector of attribute entries. */
2527 static int
2528 attr_strcmp (const void *p1, const void *p2)
2530 tree a1 = *(const tree*)p1;
2531 tree a2 = *(const tree*)p2;
2533 const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1));
2534 const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2));
2536 return strcmp (as1->name, as2->name);
2539 /* Return true if we should mangle a type attribute with name NAME. */
2541 static bool
2542 mangle_type_attribute_p (tree name)
2544 const attribute_spec *as = lookup_attribute_spec (name);
2545 if (!as || !as->affects_type_identity)
2546 return false;
2548 /* Skip internal-only attributes, which are distinguished from others
2549 by having a space. At present, all internal-only attributes that
2550 affect type identity are target-specific and are handled by
2551 targetm.mangle_type instead.
2553 Another reason to do this is that a space isn't a valid identifier
2554 character for most file formats. */
2555 if (strchr (IDENTIFIER_POINTER (name), ' '))
2556 return false;
2558 /* The following attributes are mangled specially. */
2559 if (is_attribute_p ("transaction_safe", name))
2560 return false;
2561 if (is_attribute_p ("abi_tag", name))
2562 return false;
2564 return true;
2567 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2568 CV-qualifiers written for TYPE.
2570 <CV-qualifiers> ::= [r] [V] [K] */
2572 static int
2573 write_CV_qualifiers_for_type (const tree type)
2575 int num_qualifiers = 0;
2577 /* The order is specified by:
2579 "In cases where multiple order-insensitive qualifiers are
2580 present, they should be ordered 'K' (closest to the base type),
2581 'V', 'r', and 'U' (farthest from the base type) ..." */
2583 /* Mangle attributes that affect type identity as extended qualifiers.
2585 We don't do this with classes and enums because their attributes
2586 are part of their definitions, not something added on. */
2588 if (!OVERLOAD_TYPE_P (type))
2590 auto_vec<tree> vec;
2591 for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
2592 if (mangle_type_attribute_p (get_attribute_name (a)))
2593 vec.safe_push (a);
2594 if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
2595 G.need_abi_warning = true;
2596 if (abi_version_at_least (10))
2598 vec.qsort (attr_strcmp);
2599 while (!vec.is_empty())
2601 tree a = vec.pop();
2602 const attribute_spec *as
2603 = lookup_attribute_spec (get_attribute_name (a));
2605 write_char ('U');
2606 write_unsigned_number (strlen (as->name));
2607 write_string (as->name);
2608 if (TREE_VALUE (a))
2610 write_char ('I');
2611 for (tree args = TREE_VALUE (a); args;
2612 args = TREE_CHAIN (args))
2614 tree arg = TREE_VALUE (args);
2615 write_template_arg (arg);
2617 write_char ('E');
2620 ++num_qualifiers;
2625 /* Note that we do not use cp_type_quals below; given "const
2626 int[3]", the "const" is emitted with the "int", not with the
2627 array. */
2628 cp_cv_quals quals = TYPE_QUALS (type);
2630 if (quals & TYPE_QUAL_RESTRICT)
2632 write_char ('r');
2633 ++num_qualifiers;
2635 if (quals & TYPE_QUAL_VOLATILE)
2637 write_char ('V');
2638 ++num_qualifiers;
2640 if (quals & TYPE_QUAL_CONST)
2642 write_char ('K');
2643 ++num_qualifiers;
2646 return num_qualifiers;
2649 /* Non-terminal <builtin-type>.
2651 <builtin-type> ::= v # void
2652 ::= b # bool
2653 ::= w # wchar_t
2654 ::= c # char
2655 ::= a # signed char
2656 ::= h # unsigned char
2657 ::= s # short
2658 ::= t # unsigned short
2659 ::= i # int
2660 ::= j # unsigned int
2661 ::= l # long
2662 ::= m # unsigned long
2663 ::= x # long long, __int64
2664 ::= y # unsigned long long, __int64
2665 ::= n # __int128
2666 ::= o # unsigned __int128
2667 ::= f # float
2668 ::= d # double
2669 ::= e # long double, __float80
2670 ::= g # __float128 [not supported]
2671 ::= u <source-name> # vendor extended type */
2673 static void
2674 write_builtin_type (tree type)
2676 if (TYPE_CANONICAL (type))
2677 type = TYPE_CANONICAL (type);
2679 switch (TREE_CODE (type))
2681 case VOID_TYPE:
2682 write_char ('v');
2683 break;
2685 case BOOLEAN_TYPE:
2686 write_char ('b');
2687 break;
2689 case INTEGER_TYPE:
2690 /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
2691 isn't in integer_type_nodes. */
2692 if (type == wchar_type_node)
2693 write_char ('w');
2694 else if (type == char8_type_node)
2695 write_string ("Du");
2696 else if (type == char16_type_node)
2697 write_string ("Ds");
2698 else if (type == char32_type_node)
2699 write_string ("Di");
2700 else
2702 size_t itk;
2703 /* Assume TYPE is one of the shared integer type nodes. Find
2704 it in the array of these nodes. */
2705 iagain:
2706 for (itk = 0; itk < itk_none; ++itk)
2707 if (integer_types[itk] != NULL_TREE
2708 && integer_type_codes[itk] != '\0'
2709 && type == integer_types[itk])
2711 /* Print the corresponding single-letter code. */
2712 write_char (integer_type_codes[itk]);
2713 break;
2716 if (itk == itk_none)
2718 tree t = c_common_type_for_mode (TYPE_MODE (type),
2719 TYPE_UNSIGNED (type));
2720 if (type != t)
2722 type = t;
2723 goto iagain;
2726 if (TYPE_PRECISION (type) == 128)
2727 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2728 else
2730 /* Allow for cases where TYPE is not one of the shared
2731 integer type nodes and write a "vendor extended builtin
2732 type" with a name the form intN or uintN, respectively.
2733 Situations like this can happen if you have an
2734 __attribute__((__mode__(__SI__))) type and use exotic
2735 switches like '-mint8' on AVR. Of course, this is
2736 undefined by the C++ ABI (and '-mint8' is not even
2737 Standard C conforming), but when using such special
2738 options you're pretty much in nowhere land anyway. */
2739 const char *prefix;
2740 char prec[11]; /* up to ten digits for an unsigned */
2742 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2743 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2744 write_char ('u'); /* "vendor extended builtin type" */
2745 write_unsigned_number (strlen (prefix) + strlen (prec));
2746 write_string (prefix);
2747 write_string (prec);
2751 break;
2753 case REAL_TYPE:
2754 if (type == float_type_node)
2755 write_char ('f');
2756 else if (type == double_type_node)
2757 write_char ('d');
2758 else if (type == long_double_type_node)
2759 write_char ('e');
2760 else if (type == dfloat32_type_node)
2761 write_string ("Df");
2762 else if (type == dfloat64_type_node)
2763 write_string ("Dd");
2764 else if (type == dfloat128_type_node)
2765 write_string ("De");
2766 else if (type == float16_type_node)
2767 write_string ("DF16_");
2768 else if (type == float32_type_node)
2769 write_string ("DF32_");
2770 else if (type == float64_type_node)
2771 write_string ("DF64_");
2772 else if (type == float128_type_node)
2773 write_string ("DF128_");
2774 else if (type == float32x_type_node)
2775 write_string ("DF32x");
2776 else if (type == float64x_type_node)
2777 write_string ("DF64x");
2778 else if (type == float128x_type_node)
2779 write_string ("DF128x");
2780 else if (type == bfloat16_type_node)
2781 write_string ("DF16b");
2782 else
2783 gcc_unreachable ();
2784 break;
2786 default:
2787 gcc_unreachable ();
2791 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2792 METHOD_TYPE. The return type is mangled before the parameter
2793 types.
2795 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2797 static void
2798 write_function_type (const tree type)
2800 MANGLE_TRACE_TREE ("function-type", type);
2802 /* For a pointer to member function, the function type may have
2803 cv-qualifiers, indicating the quals for the artificial 'this'
2804 parameter. */
2805 if (TREE_CODE (type) == METHOD_TYPE)
2807 /* The first parameter must be a POINTER_TYPE pointing to the
2808 `this' parameter. */
2809 tree this_type = class_of_this_parm (type);
2810 write_CV_qualifiers_for_type (this_type);
2813 write_exception_spec (TYPE_RAISES_EXCEPTIONS (type));
2815 if (tx_safe_fn_type_p (type))
2816 write_string ("Dx");
2818 write_char ('F');
2819 /* We don't track whether or not a type is `extern "C"'. Note that
2820 you can have an `extern "C"' function that does not have
2821 `extern "C"' type, and vice versa:
2823 extern "C" typedef void function_t();
2824 function_t f; // f has C++ linkage, but its type is
2825 // `extern "C"'
2827 typedef void function_t();
2828 extern "C" function_t f; // Vice versa.
2830 See [dcl.link]. */
2831 write_bare_function_type (type, /*include_return_type_p=*/1,
2832 /*decl=*/NULL);
2833 if (FUNCTION_REF_QUALIFIED (type))
2835 if (FUNCTION_RVALUE_QUALIFIED (type))
2836 write_char ('O');
2837 else
2838 write_char ('R');
2840 write_char ('E');
2843 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2844 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2845 is mangled before the parameter types. If non-NULL, DECL is
2846 FUNCTION_DECL for the function whose type is being emitted. */
2848 static void
2849 write_bare_function_type (const tree type, const int include_return_type_p,
2850 const tree decl)
2852 MANGLE_TRACE_TREE ("bare-function-type", type);
2854 /* Mangle the return type, if requested. */
2855 if (include_return_type_p)
2856 write_type (TREE_TYPE (type));
2858 /* Now mangle the types of the arguments. */
2859 ++G.parm_depth;
2860 write_method_parms (TYPE_ARG_TYPES (type),
2861 TREE_CODE (type) == METHOD_TYPE,
2862 decl);
2863 --G.parm_depth;
2866 /* Write the mangled representation of a method parameter list of
2867 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2868 considered a non-static method, and the this parameter is omitted.
2869 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2870 parameters are being emitted. */
2872 static void
2873 write_method_parms (tree parm_types, const int method_p, const tree decl)
2875 tree first_parm_type;
2876 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2878 /* Assume this parameter type list is variable-length. If it ends
2879 with a void type, then it's not. */
2880 int varargs_p = 1;
2882 /* If this is a member function, skip the first arg, which is the
2883 this pointer.
2884 "Member functions do not encode the type of their implicit this
2885 parameter."
2887 Similarly, there's no need to mangle artificial parameters, like
2888 the VTT parameters for constructors and destructors. */
2889 if (method_p)
2891 parm_types = TREE_CHAIN (parm_types);
2892 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2894 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2896 parm_types = TREE_CHAIN (parm_types);
2897 parm_decl = DECL_CHAIN (parm_decl);
2900 if (decl && ctor_omit_inherited_parms (decl))
2901 /* Bring back parameters omitted from an inherited ctor. */
2902 parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl));
2905 for (first_parm_type = parm_types;
2906 parm_types;
2907 parm_types = TREE_CHAIN (parm_types))
2909 tree parm = TREE_VALUE (parm_types);
2910 if (parm == void_type_node)
2912 /* "Empty parameter lists, whether declared as () or
2913 conventionally as (void), are encoded with a void parameter
2914 (v)." */
2915 if (parm_types == first_parm_type)
2916 write_type (parm);
2917 /* If the parm list is terminated with a void type, it's
2918 fixed-length. */
2919 varargs_p = 0;
2920 /* A void type better be the last one. */
2921 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2923 else
2924 write_type (parm);
2927 if (varargs_p)
2928 /* <builtin-type> ::= z # ellipsis */
2929 write_char ('z');
2932 /* <class-enum-type> ::= <name> */
2934 static void
2935 write_class_enum_type (const tree type)
2937 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2940 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2941 arguments.
2943 <template-args> ::= I <template-arg>* E */
2945 static void
2946 write_template_args (tree args)
2948 int i;
2949 int length = 0;
2951 MANGLE_TRACE_TREE ("template-args", args);
2953 write_char ('I');
2955 if (args)
2956 length = TREE_VEC_LENGTH (args);
2958 if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2960 /* We have nested template args. We want the innermost template
2961 argument list. */
2962 args = TREE_VEC_ELT (args, length - 1);
2963 length = TREE_VEC_LENGTH (args);
2965 for (i = 0; i < length; ++i)
2966 write_template_arg (TREE_VEC_ELT (args, i));
2968 write_char ('E');
2971 /* Write out the
2972 <unqualified-name>
2973 <unqualified-name> <template-args>
2974 part of SCOPE_REF or COMPONENT_REF mangling. */
2976 static void
2977 write_member_name (tree member)
2979 if (identifier_p (member))
2981 if (IDENTIFIER_ANY_OP_P (member))
2983 if (abi_version_at_least (11))
2984 write_string ("on");
2985 if (abi_warn_or_compat_version_crosses (11))
2986 G.need_abi_warning = 1;
2988 write_unqualified_id (member);
2990 else if (DECL_P (member))
2992 gcc_assert (!DECL_OVERLOADED_OPERATOR_P (member));
2993 write_unqualified_name (member);
2995 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2997 tree name = TREE_OPERAND (member, 0);
2998 name = OVL_FIRST (name);
2999 write_member_name (name);
3000 write_template_args (TREE_OPERAND (member, 1));
3002 else
3003 write_expression (member);
3006 /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for
3007 converting to BASE, or just the conversion of EXPR if BASE is null.
3009 "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base
3010 path Min(P) is defined as follows: let C_i be the last element for which the
3011 conversion to C_0 is unambiguous; if that element is C_n, the minimized path
3012 is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) ->
3013 C_0."
3015 We mangle the conversion to C_i if it's different from C_n. */
3017 static bool
3018 write_base_ref (tree expr, tree base = NULL_TREE)
3020 if (TREE_CODE (expr) != COMPONENT_REF)
3021 return false;
3023 tree field = TREE_OPERAND (expr, 1);
3025 if (TREE_CODE (field) != FIELD_DECL || !DECL_FIELD_IS_BASE (field))
3026 return false;
3028 tree object = TREE_OPERAND (expr, 0);
3030 tree binfo = NULL_TREE;
3031 if (base)
3033 tree cur = TREE_TYPE (object);
3034 binfo = lookup_base (cur, base, ba_unique, NULL, tf_none);
3036 else
3037 /* We're at the end of the base conversion chain, so it can't be
3038 ambiguous. */
3039 base = TREE_TYPE (field);
3041 if (binfo == error_mark_node)
3043 /* cur->base is ambiguous, so make the conversion to
3044 last explicit, expressed as a cast (last&)object. */
3045 tree last = TREE_TYPE (expr);
3046 write_string (OVL_OP_INFO (false, CAST_EXPR)->mangled_name);
3047 write_type (build_reference_type (last));
3048 write_expression (object);
3050 else if (write_base_ref (object, base))
3051 /* cur->base is unambiguous, but we had another base conversion
3052 underneath and wrote it out. */;
3053 else
3054 /* No more base conversions, just write out the object. */
3055 write_expression (object);
3057 return true;
3060 /* The number of elements spanned by a RANGE_EXPR. */
3062 unsigned HOST_WIDE_INT
3063 range_expr_nelts (tree expr)
3065 tree lo = TREE_OPERAND (expr, 0);
3066 tree hi = TREE_OPERAND (expr, 1);
3067 return tree_to_uhwi (hi) - tree_to_uhwi (lo) + 1;
3070 /* <expression> ::= <unary operator-name> <expression>
3071 ::= <binary operator-name> <expression> <expression>
3072 ::= <expr-primary>
3074 <expr-primary> ::= <template-param>
3075 ::= L <type> <value number> E # literal
3076 ::= L <mangled-name> E # external name
3077 ::= st <type> # sizeof
3078 ::= sr <type> <unqualified-name> # dependent name
3079 ::= sr <type> <unqualified-name> <template-args> */
3081 static void
3082 write_expression (tree expr)
3084 enum tree_code code = TREE_CODE (expr);
3086 if (TREE_CODE (expr) == TARGET_EXPR)
3088 expr = TARGET_EXPR_INITIAL (expr);
3089 code = TREE_CODE (expr);
3092 /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer
3093 argument is converted (via qualification conversions) to another type. */
3094 while (CONVERT_EXPR_CODE_P (code)
3095 || code == IMPLICIT_CONV_EXPR
3096 || location_wrapper_p (expr)
3097 /* Parentheses aren't mangled. */
3098 || code == PAREN_EXPR
3099 || code == NON_LVALUE_EXPR
3100 || (code == VIEW_CONVERT_EXPR
3101 && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
3103 expr = TREE_OPERAND (expr, 0);
3104 code = TREE_CODE (expr);
3107 if (code == BASELINK
3108 && (!type_unknown_p (expr)
3109 || !BASELINK_QUALIFIED_P (expr)))
3111 expr = BASELINK_FUNCTIONS (expr);
3112 code = TREE_CODE (expr);
3115 /* Handle pointers-to-members by making them look like expression
3116 nodes. */
3117 if (code == PTRMEM_CST)
3119 expr = build_nt (ADDR_EXPR,
3120 build_qualified_name (/*type=*/NULL_TREE,
3121 PTRMEM_CST_CLASS (expr),
3122 PTRMEM_CST_MEMBER (expr),
3123 /*template_p=*/false));
3124 code = TREE_CODE (expr);
3127 /* Handle template parameters. */
3128 if (code == TEMPLATE_TYPE_PARM
3129 || code == TEMPLATE_TEMPLATE_PARM
3130 || code == BOUND_TEMPLATE_TEMPLATE_PARM
3131 || code == TEMPLATE_PARM_INDEX)
3132 write_template_param (expr);
3133 /* Handle literals. */
3134 else if (TREE_CODE_CLASS (code) == tcc_constant
3135 || code == CONST_DECL)
3136 write_template_arg_literal (expr);
3137 else if (code == EXCESS_PRECISION_EXPR
3138 && TREE_CODE (TREE_OPERAND (expr, 0)) == REAL_CST)
3139 write_template_arg_literal (fold_convert (TREE_TYPE (expr),
3140 TREE_OPERAND (expr, 0)));
3141 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
3143 gcc_assert (id_equal (DECL_NAME (expr), "this"));
3144 write_string ("fpT");
3146 else if (code == PARM_DECL)
3148 /* A function parameter used in a late-specified return type. */
3149 int index = DECL_PARM_INDEX (expr);
3150 int level = DECL_PARM_LEVEL (expr);
3151 int delta = G.parm_depth - level + 1;
3152 gcc_assert (index >= 1);
3153 write_char ('f');
3154 if (delta != 0)
3156 if (abi_version_at_least (5))
3158 /* Let L be the number of function prototype scopes from the
3159 innermost one (in which the parameter reference occurs) up
3160 to (and including) the one containing the declaration of
3161 the referenced parameter. If the parameter declaration
3162 clause of the innermost function prototype scope has been
3163 completely seen, it is not counted (in that case -- which
3164 is perhaps the most common -- L can be zero). */
3165 write_char ('L');
3166 write_unsigned_number (delta - 1);
3168 if (abi_warn_or_compat_version_crosses (5))
3169 G.need_abi_warning = true;
3171 write_char ('p');
3172 write_compact_number (index - 1);
3174 else if (DECL_P (expr))
3176 write_char ('L');
3177 write_mangled_name (expr, false);
3178 write_char ('E');
3180 else if (TREE_CODE (expr) == SIZEOF_EXPR)
3182 tree op = TREE_OPERAND (expr, 0);
3184 if (PACK_EXPANSION_P (op))
3186 if (abi_warn_or_compat_version_crosses (11))
3187 G.need_abi_warning = true;
3188 if (abi_version_at_least (11))
3190 /* sZ rather than szDp. */
3191 write_string ("sZ");
3192 write_expression (PACK_EXPANSION_PATTERN (op));
3193 return;
3197 if (SIZEOF_EXPR_TYPE_P (expr))
3199 write_string ("st");
3200 write_type (TREE_TYPE (op));
3202 else if (ARGUMENT_PACK_P (op))
3204 tree args = ARGUMENT_PACK_ARGS (op);
3205 int length = TREE_VEC_LENGTH (args);
3206 if (abi_warn_or_compat_version_crosses (10))
3207 G.need_abi_warning = true;
3208 if (abi_version_at_least (10))
3210 /* sP <template-arg>* E # sizeof...(T), size of a captured
3211 template parameter pack from an alias template */
3212 write_string ("sP");
3213 for (int i = 0; i < length; ++i)
3214 write_template_arg (TREE_VEC_ELT (args, i));
3215 write_char ('E');
3217 else
3219 /* In GCC 5 we represented this sizeof wrong, with the effect
3220 that we mangled it as the last element of the pack. */
3221 tree arg = TREE_VEC_ELT (args, length-1);
3222 if (TYPE_P (op))
3224 write_string ("st");
3225 write_type (arg);
3227 else
3229 write_string ("sz");
3230 write_expression (arg);
3234 else if (TYPE_P (TREE_OPERAND (expr, 0)))
3236 write_string ("st");
3237 write_type (TREE_OPERAND (expr, 0));
3239 else
3240 goto normal_expr;
3242 else if (TREE_CODE (expr) == ALIGNOF_EXPR)
3244 if (!ALIGNOF_EXPR_STD_P (expr))
3246 if (abi_warn_or_compat_version_crosses (16))
3247 G.need_abi_warning = true;
3248 if (abi_version_at_least (16))
3250 /* We used to mangle __alignof__ like alignof. */
3251 write_string ("u11__alignof__");
3252 write_template_arg (TREE_OPERAND (expr, 0));
3253 write_char ('E');
3254 return;
3257 if (TYPE_P (TREE_OPERAND (expr, 0)))
3259 write_string ("at");
3260 write_type (TREE_OPERAND (expr, 0));
3262 else
3263 goto normal_expr;
3265 else if (code == SCOPE_REF
3266 || code == BASELINK)
3268 tree scope, member;
3269 if (code == SCOPE_REF)
3271 scope = TREE_OPERAND (expr, 0);
3272 member = TREE_OPERAND (expr, 1);
3273 if (BASELINK_P (member))
3274 member = BASELINK_FUNCTIONS (member);
3276 else
3278 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
3279 member = BASELINK_FUNCTIONS (expr);
3282 /* If the MEMBER is a real declaration, then the qualifying
3283 scope was not dependent. Ideally, we would not have a
3284 SCOPE_REF in those cases, but sometimes we do. If the second
3285 argument is a DECL, then the name must not have been
3286 dependent. */
3287 if (DECL_P (member))
3288 write_expression (member);
3289 else
3291 gcc_assert (code != BASELINK || BASELINK_QUALIFIED_P (expr));
3292 write_string ("sr");
3293 write_type (scope);
3294 write_member_name (member);
3297 else if (INDIRECT_REF_P (expr)
3298 && TREE_TYPE (TREE_OPERAND (expr, 0))
3299 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
3301 write_expression (TREE_OPERAND (expr, 0));
3303 else if (identifier_p (expr))
3305 /* An operator name appearing as a dependent name needs to be
3306 specially marked to disambiguate between a use of the operator
3307 name and a use of the operator in an expression. */
3308 if (IDENTIFIER_ANY_OP_P (expr))
3309 write_string ("on");
3310 write_unqualified_id (expr);
3312 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
3314 tree fn = TREE_OPERAND (expr, 0);
3315 if (!identifier_p (fn))
3316 fn = OVL_NAME (fn);
3317 if (IDENTIFIER_ANY_OP_P (fn))
3318 write_string ("on");
3319 write_unqualified_id (fn);
3320 write_template_args (TREE_OPERAND (expr, 1));
3322 else if (TREE_CODE (expr) == MODOP_EXPR)
3324 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
3325 const char *name = OVL_OP_INFO (true, subop)->mangled_name;
3327 write_string (name);
3328 write_expression (TREE_OPERAND (expr, 0));
3329 write_expression (TREE_OPERAND (expr, 2));
3331 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
3333 /* ::= [gs] nw <expression>* _ <type> E
3334 ::= [gs] nw <expression>* _ <type> <initializer>
3335 ::= [gs] na <expression>* _ <type> E
3336 ::= [gs] na <expression>* _ <type> <initializer>
3337 <initializer> ::= pi <expression>* E */
3338 tree placement = TREE_OPERAND (expr, 0);
3339 tree type = TREE_OPERAND (expr, 1);
3340 tree nelts = TREE_OPERAND (expr, 2);
3341 tree init = TREE_OPERAND (expr, 3);
3342 tree t;
3344 gcc_assert (code == NEW_EXPR);
3345 if (TREE_OPERAND (expr, 2))
3346 code = VEC_NEW_EXPR;
3348 if (NEW_EXPR_USE_GLOBAL (expr))
3349 write_string ("gs");
3351 write_string (OVL_OP_INFO (false, code)->mangled_name);
3353 for (t = placement; t; t = TREE_CHAIN (t))
3354 write_expression (TREE_VALUE (t));
3356 write_char ('_');
3358 if (nelts)
3360 tree domain;
3361 ++processing_template_decl;
3362 domain = compute_array_index_type (NULL_TREE, nelts,
3363 tf_warning_or_error);
3364 type = build_cplus_array_type (type, domain);
3365 --processing_template_decl;
3367 write_type (type);
3369 if (init && TREE_CODE (init) == TREE_LIST
3370 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
3371 write_expression (TREE_VALUE (init));
3372 else
3374 if (init)
3375 write_string ("pi");
3376 if (init && init != void_node)
3377 for (t = init; t; t = TREE_CHAIN (t))
3378 write_expression (TREE_VALUE (t));
3379 write_char ('E');
3382 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
3384 gcc_assert (code == DELETE_EXPR);
3385 if (DELETE_EXPR_USE_VEC (expr))
3386 code = VEC_DELETE_EXPR;
3388 if (DELETE_EXPR_USE_GLOBAL (expr))
3389 write_string ("gs");
3391 write_string (OVL_OP_INFO (false, code)->mangled_name);
3393 write_expression (TREE_OPERAND (expr, 0));
3395 else if (code == THROW_EXPR)
3397 tree op = TREE_OPERAND (expr, 0);
3398 if (op)
3400 write_string ("tw");
3401 write_expression (op);
3403 else
3404 write_string ("tr");
3406 else if (code == NOEXCEPT_EXPR)
3408 write_string ("nx");
3409 write_expression (TREE_OPERAND (expr, 0));
3411 else if (code == CONSTRUCTOR)
3413 bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr);
3414 tree etype = TREE_TYPE (expr);
3416 if (braced_init)
3417 write_string ("il");
3418 else
3420 write_string ("tl");
3421 write_type (etype);
3424 /* If this is an undigested initializer, mangle it as written.
3425 COMPOUND_LITERAL_P doesn't actually distinguish between digested and
3426 undigested braced casts, but it should work to use it to distinguish
3427 between braced casts in a template signature (undigested) and template
3428 parm object values (digested), and all CONSTRUCTORS that get here
3429 should be one of those two cases. */
3430 bool undigested = braced_init || COMPOUND_LITERAL_P (expr);
3431 if (undigested || !zero_init_expr_p (expr))
3433 /* Convert braced initializer lists to STRING_CSTs so that
3434 A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
3435 still using the latter mangling for strings that
3436 originated as braced initializer lists. */
3437 expr = braced_lists_to_strings (etype, expr);
3439 if (TREE_CODE (expr) == CONSTRUCTOR)
3441 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
3442 unsigned last_nonzero = UINT_MAX;
3443 constructor_elt *ce;
3445 if (!undigested)
3446 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3447 if ((TREE_CODE (etype) == UNION_TYPE
3448 && ce->index != first_field (etype))
3449 || !zero_init_expr_p (ce->value))
3450 last_nonzero = i;
3452 if (undigested || last_nonzero != UINT_MAX)
3453 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3455 if (i > last_nonzero)
3456 break;
3457 if (!undigested && TREE_CODE (etype) == UNION_TYPE)
3459 /* Express the active member as a designator. */
3460 write_string ("di");
3461 write_unqualified_name (ce->index);
3463 unsigned reps = 1;
3464 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3465 reps = range_expr_nelts (ce->index);
3466 for (unsigned j = 0; j < reps; ++j)
3467 write_expression (ce->value);
3470 else
3472 gcc_assert (TREE_CODE (expr) == STRING_CST);
3473 write_expression (expr);
3476 write_char ('E');
3478 else if (code == LAMBDA_EXPR)
3480 /* [temp.over.link] Two lambda-expressions are never considered
3481 equivalent.
3483 So just use the closure type mangling. */
3484 write_string ("tl");
3485 write_type (LAMBDA_EXPR_CLOSURE (expr));
3486 write_char ('E');
3488 else if (dependent_name (expr))
3490 tree name = dependent_name (expr);
3491 if (IDENTIFIER_ANY_OP_P (name))
3493 if (abi_version_at_least (16))
3494 write_string ("on");
3495 if (abi_warn_or_compat_version_crosses (16))
3496 G.need_abi_warning = 1;
3498 write_unqualified_id (name);
3500 else
3502 normal_expr:
3503 int i, len;
3504 const char *name;
3506 /* When we bind a variable or function to a non-type template
3507 argument with reference type, we create an ADDR_EXPR to show
3508 the fact that the entity's address has been taken. But, we
3509 don't actually want to output a mangling code for the `&'. */
3510 if (TREE_CODE (expr) == ADDR_EXPR
3511 && TREE_TYPE (expr)
3512 && TYPE_REF_P (TREE_TYPE (expr)))
3514 expr = TREE_OPERAND (expr, 0);
3515 if (DECL_P (expr))
3517 write_expression (expr);
3518 return;
3521 code = TREE_CODE (expr);
3524 if (code == COMPONENT_REF)
3526 tree ob = TREE_OPERAND (expr, 0);
3528 if (TREE_CODE (ob) == ARROW_EXPR)
3530 write_string (OVL_OP_INFO (false, code)->mangled_name);
3531 ob = TREE_OPERAND (ob, 0);
3532 write_expression (ob);
3534 else if (write_base_ref (expr))
3535 return;
3536 else if (!is_dummy_object (ob))
3538 write_string ("dt");
3539 write_expression (ob);
3541 /* else, for a non-static data member with no associated object (in
3542 unevaluated context), use the unresolved-name mangling. */
3544 write_member_name (TREE_OPERAND (expr, 1));
3545 return;
3548 /* If it wasn't any of those, recursively expand the expression. */
3549 name = OVL_OP_INFO (false, code)->mangled_name;
3551 /* We used to mangle const_cast and static_cast like a C cast. */
3552 if (code == CONST_CAST_EXPR
3553 || code == STATIC_CAST_EXPR)
3555 if (abi_warn_or_compat_version_crosses (6))
3556 G.need_abi_warning = 1;
3557 if (!abi_version_at_least (6))
3558 name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name;
3561 if (name == NULL)
3563 switch (code)
3565 case TRAIT_EXPR:
3566 error ("use of built-in trait %qE in function signature; "
3567 "use library traits instead", expr);
3568 break;
3570 default:
3571 sorry ("mangling %C", code);
3572 break;
3574 return;
3576 else
3577 write_string (name);
3579 switch (code)
3581 case CALL_EXPR:
3583 tree fn = CALL_EXPR_FN (expr);
3585 if (TREE_CODE (fn) == ADDR_EXPR)
3586 fn = TREE_OPERAND (fn, 0);
3588 /* Mangle a dependent name as the name, not whatever happens to
3589 be the first function in the overload set. */
3590 if (OVL_P (fn)
3591 && type_dependent_expression_p_push (expr))
3592 fn = OVL_NAME (fn);
3594 write_expression (fn);
3597 for (i = 0; i < call_expr_nargs (expr); ++i)
3598 write_expression (CALL_EXPR_ARG (expr, i));
3599 write_char ('E');
3600 break;
3602 case CAST_EXPR:
3603 write_type (TREE_TYPE (expr));
3604 if (list_length (TREE_OPERAND (expr, 0)) == 1)
3605 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
3606 else
3608 tree args = TREE_OPERAND (expr, 0);
3609 write_char ('_');
3610 for (; args; args = TREE_CHAIN (args))
3611 write_expression (TREE_VALUE (args));
3612 write_char ('E');
3614 break;
3616 case DYNAMIC_CAST_EXPR:
3617 case REINTERPRET_CAST_EXPR:
3618 case STATIC_CAST_EXPR:
3619 case CONST_CAST_EXPR:
3620 write_type (TREE_TYPE (expr));
3621 write_expression (TREE_OPERAND (expr, 0));
3622 break;
3624 case PREINCREMENT_EXPR:
3625 case PREDECREMENT_EXPR:
3626 if (abi_version_at_least (6))
3627 write_char ('_');
3628 if (abi_warn_or_compat_version_crosses (6))
3629 G.need_abi_warning = 1;
3630 /* Fall through. */
3632 default:
3633 /* In the middle-end, some expressions have more operands than
3634 they do in templates (and mangling). */
3635 len = cp_tree_operand_length (expr);
3637 for (i = 0; i < len; ++i)
3639 tree operand = TREE_OPERAND (expr, i);
3640 /* As a GNU extension, the middle operand of a
3641 conditional may be omitted. Since expression
3642 manglings are supposed to represent the input token
3643 stream, there's no good way to mangle such an
3644 expression without extending the C++ ABI. */
3645 if (code == COND_EXPR && i == 1 && !operand)
3647 error ("omitted middle operand to %<?:%> operand "
3648 "cannot be mangled");
3649 continue;
3651 else if (FOLD_EXPR_P (expr))
3653 /* The first 'operand' of a fold-expression is the operator
3654 that it folds over. */
3655 if (i == 0)
3657 int fcode = TREE_INT_CST_LOW (operand);
3658 write_string (OVL_OP_INFO (false, fcode)->mangled_name);
3659 continue;
3661 else if (code == BINARY_LEFT_FOLD_EXPR)
3663 /* The order of operands of the binary left and right
3664 folds is the same, but we want to mangle them in
3665 lexical order, i.e. non-pack first. */
3666 if (i == 1)
3667 operand = FOLD_EXPR_INIT (expr);
3668 else
3669 operand = FOLD_EXPR_PACK (expr);
3671 if (PACK_EXPANSION_P (operand))
3672 operand = PACK_EXPANSION_PATTERN (operand);
3674 write_expression (operand);
3680 /* Literal subcase of non-terminal <template-arg>.
3682 "Literal arguments, e.g. "A<42L>", are encoded with their type
3683 and value. Negative integer values are preceded with "n"; for
3684 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3685 encoded as 0, true as 1." */
3687 static void
3688 write_template_arg_literal (const tree value)
3690 if (TREE_CODE (value) == STRING_CST)
3691 /* Temporarily mangle strings as braced initializer lists. */
3692 write_string ("tl");
3693 else
3694 write_char ('L');
3696 tree valtype = TREE_TYPE (value);
3697 write_type (valtype);
3699 /* Write a null member pointer value as (type)0, regardless of its
3700 real representation. */
3701 if (null_member_pointer_value_p (value))
3702 write_integer_cst (integer_zero_node);
3703 else
3704 switch (TREE_CODE (value))
3706 case CONST_DECL:
3707 write_integer_cst (DECL_INITIAL (value));
3708 break;
3710 case INTEGER_CST:
3711 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3712 || integer_zerop (value) || integer_onep (value));
3713 if (!(abi_version_at_least (14)
3714 && NULLPTR_TYPE_P (TREE_TYPE (value))))
3715 write_integer_cst (value);
3716 break;
3718 case REAL_CST:
3719 write_real_cst (value);
3720 break;
3722 case COMPLEX_CST:
3723 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3724 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3726 write_integer_cst (TREE_REALPART (value));
3727 write_char ('_');
3728 write_integer_cst (TREE_IMAGPART (value));
3730 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3731 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3733 write_real_cst (TREE_REALPART (value));
3734 write_char ('_');
3735 write_real_cst (TREE_IMAGPART (value));
3737 else
3738 gcc_unreachable ();
3739 break;
3741 case STRING_CST:
3743 /* Mangle strings the same as braced initializer lists. */
3744 unsigned n = TREE_STRING_LENGTH (value);
3745 const char *str = TREE_STRING_POINTER (value);
3747 /* Count the number of trailing nuls and subtract them from
3748 STRSIZE because they don't need to be mangled. */
3749 for (const char *p = str + n - 1; ; --p)
3751 if (*p || p == str)
3753 n -= str + n - !!*p - p;
3754 break;
3757 tree eltype = TREE_TYPE (valtype);
3758 for (const char *p = str; n--; ++p)
3760 write_char ('L');
3761 write_type (eltype);
3762 write_unsigned_number (*(const unsigned char*)p);
3763 write_string ("E");
3765 break;
3768 default:
3769 gcc_unreachable ();
3772 write_char ('E');
3775 /* Non-terminal <template-arg>.
3777 <template-arg> ::= <type> # type
3778 ::= L <type> </value/ number> E # literal
3779 ::= LZ <name> E # external name
3780 ::= X <expression> E # expression */
3782 static void
3783 write_template_arg (tree node)
3785 enum tree_code code = TREE_CODE (node);
3787 MANGLE_TRACE_TREE ("template-arg", node);
3789 /* A template template parameter's argument list contains TREE_LIST
3790 nodes of which the value field is the actual argument. */
3791 if (code == TREE_LIST)
3793 node = TREE_VALUE (node);
3794 /* If it's a decl, deal with its type instead. */
3795 if (DECL_P (node))
3797 node = TREE_TYPE (node);
3798 code = TREE_CODE (node);
3802 if (VAR_P (node) && DECL_NTTP_OBJECT_P (node))
3803 /* We want to mangle the argument, not the var we stored it in. */
3804 node = tparm_object_argument (node);
3806 /* Strip a conversion added by convert_nontype_argument. */
3807 if (TREE_CODE (node) == IMPLICIT_CONV_EXPR)
3808 node = TREE_OPERAND (node, 0);
3809 if (REFERENCE_REF_P (node))
3810 node = TREE_OPERAND (node, 0);
3811 if (TREE_CODE (node) == NOP_EXPR
3812 && TYPE_REF_P (TREE_TYPE (node)))
3814 /* Template parameters can be of reference type. To maintain
3815 internal consistency, such arguments use a conversion from
3816 address of object to reference type. */
3817 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3818 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3821 if (TREE_CODE (node) == BASELINK
3822 && !type_unknown_p (node))
3824 if (abi_version_at_least (6))
3825 node = BASELINK_FUNCTIONS (node);
3826 if (abi_warn_or_compat_version_crosses (6))
3827 /* We wrongly wrapped a class-scope function in X/E. */
3828 G.need_abi_warning = 1;
3831 if (ARGUMENT_PACK_P (node))
3833 /* Expand the template argument pack. */
3834 tree args = ARGUMENT_PACK_ARGS (node);
3835 int i, length = TREE_VEC_LENGTH (args);
3836 if (abi_version_at_least (6))
3837 write_char ('J');
3838 else
3839 write_char ('I');
3840 if (abi_warn_or_compat_version_crosses (6))
3841 G.need_abi_warning = 1;
3842 for (i = 0; i < length; ++i)
3843 write_template_arg (TREE_VEC_ELT (args, i));
3844 write_char ('E');
3846 else if (TYPE_P (node))
3847 write_type (node);
3848 else if (code == TEMPLATE_DECL)
3849 /* A template appearing as a template arg is a template template arg. */
3850 write_template_template_arg (node);
3851 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3852 || code == CONST_DECL
3853 || null_member_pointer_value_p (node))
3854 write_template_arg_literal (node);
3855 else if (code == EXCESS_PRECISION_EXPR
3856 && TREE_CODE (TREE_OPERAND (node, 0)) == REAL_CST)
3857 write_template_arg_literal (fold_convert (TREE_TYPE (node),
3858 TREE_OPERAND (node, 0)));
3859 else if (DECL_P (node))
3861 write_char ('L');
3862 /* Until ABI version 3, the underscore before the mangled name
3863 was incorrectly omitted. */
3864 if (!abi_version_at_least (3))
3865 write_char ('Z');
3866 else
3867 write_string ("_Z");
3868 if (abi_warn_or_compat_version_crosses (3))
3869 G.need_abi_warning = 1;
3870 write_encoding (node);
3871 write_char ('E');
3873 else
3875 /* Template arguments may be expressions. */
3876 write_char ('X');
3877 write_expression (node);
3878 write_char ('E');
3882 /* <template-template-arg>
3883 ::= <name>
3884 ::= <substitution> */
3886 static void
3887 write_template_template_arg (const tree decl)
3889 MANGLE_TRACE_TREE ("template-template-arg", decl);
3891 if (find_substitution (decl))
3892 return;
3893 write_name (decl, /*ignore_local_scope=*/0);
3894 add_substitution (decl);
3898 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3900 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3901 ::= A <expression> _ </element/ type>
3903 "Array types encode the dimension (number of elements) and the
3904 element type. For variable length arrays, the dimension (but not
3905 the '_' separator) is omitted."
3906 Note that for flexible array members, like for other arrays of
3907 unspecified size, the dimension is also omitted. */
3909 static void
3910 write_array_type (const tree type)
3912 write_char ('A');
3913 if (TYPE_DOMAIN (type))
3915 tree index_type;
3917 index_type = TYPE_DOMAIN (type);
3918 /* The INDEX_TYPE gives the upper and lower bounds of the array.
3919 It's null for flexible array members which have no upper bound
3920 (this is a change from GCC 5 and prior where such members were
3921 incorrectly mangled as zero-length arrays). */
3922 if (tree max = TYPE_MAX_VALUE (index_type))
3924 if (TREE_CODE (max) == INTEGER_CST)
3926 /* The ABI specifies that we should mangle the number of
3927 elements in the array, not the largest allowed index. */
3928 offset_int wmax = wi::to_offset (max) + 1;
3929 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3930 number of elements as zero. */
3931 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3932 gcc_assert (wi::fits_uhwi_p (wmax));
3933 write_unsigned_number (wmax.to_uhwi ());
3935 else
3937 max = TREE_OPERAND (max, 0);
3938 write_expression (max);
3942 write_char ('_');
3943 write_type (TREE_TYPE (type));
3946 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3947 variables. TYPE is a pointer-to-member POINTER_TYPE.
3949 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3951 static void
3952 write_pointer_to_member_type (const tree type)
3954 write_char ('M');
3955 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3956 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3959 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3960 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3961 TEMPLATE_PARM_INDEX.
3963 <template-param> ::= T </parameter/ number> _ */
3965 static void
3966 write_template_param (const tree parm)
3968 int parm_index;
3970 MANGLE_TRACE_TREE ("template-parm", parm);
3972 switch (TREE_CODE (parm))
3974 case TEMPLATE_TYPE_PARM:
3975 case TEMPLATE_TEMPLATE_PARM:
3976 case BOUND_TEMPLATE_TEMPLATE_PARM:
3977 parm_index = TEMPLATE_TYPE_IDX (parm);
3978 break;
3980 case TEMPLATE_PARM_INDEX:
3981 parm_index = TEMPLATE_PARM_IDX (parm);
3982 break;
3984 default:
3985 gcc_unreachable ();
3988 write_char ('T');
3989 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3990 earliest template param denoted by `_'. */
3991 write_compact_number (parm_index);
3994 /* <template-template-param>
3995 ::= <template-param>
3996 ::= <substitution> */
3998 static void
3999 write_template_template_param (const tree parm)
4001 tree templ = NULL_TREE;
4003 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
4004 template template parameter. The substitution candidate here is
4005 only the template. */
4006 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
4008 templ
4009 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
4010 if (find_substitution (templ))
4011 return;
4014 /* <template-param> encodes only the template parameter position,
4015 not its template arguments, which is fine here. */
4016 write_template_param (parm);
4017 if (templ)
4018 add_substitution (templ);
4021 /* Non-terminal <substitution>.
4023 <substitution> ::= S <seq-id> _
4024 ::= S_ */
4026 static void
4027 write_substitution (const int seq_id)
4029 MANGLE_TRACE ("substitution", "");
4031 write_char ('S');
4032 if (seq_id > 0)
4033 write_number (seq_id - 1, /*unsigned=*/1, 36);
4034 write_char ('_');
4037 /* Start mangling ENTITY. */
4039 static inline void
4040 start_mangling (const tree entity)
4042 G.entity = entity;
4043 G.need_abi_warning = false;
4044 G.need_cxx17_warning = false;
4045 G.mod = false;
4046 obstack_free (&name_obstack, name_base);
4047 mangle_obstack = &name_obstack;
4048 name_base = obstack_alloc (&name_obstack, 0);
4051 /* Done with mangling. Release the data. */
4053 static void
4054 finish_mangling_internal (void)
4056 /* Clear all the substitutions. */
4057 vec_safe_truncate (G.substitutions, 0);
4059 if (G.mod)
4060 mangle_module_fini ();
4062 /* Null-terminate the string. */
4063 write_char ('\0');
4067 /* Like finish_mangling_internal, but return the mangled string. */
4069 static inline const char *
4070 finish_mangling (void)
4072 finish_mangling_internal ();
4073 return (const char *) obstack_finish (mangle_obstack);
4076 /* Like finish_mangling_internal, but return an identifier. */
4078 static tree
4079 finish_mangling_get_identifier (void)
4081 finish_mangling_internal ();
4082 /* Don't obstack_finish here, and the next start_mangling will
4083 remove the identifier. */
4084 return get_identifier ((const char *) obstack_base (mangle_obstack));
4087 /* Initialize data structures for mangling. */
4089 void
4090 init_mangle (void)
4092 gcc_obstack_init (&name_obstack);
4093 name_base = obstack_alloc (&name_obstack, 0);
4094 vec_alloc (G.substitutions, 0);
4096 /* Cache these identifiers for quick comparison when checking for
4097 standard substitutions. */
4098 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
4099 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
4100 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
4101 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
4102 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
4103 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
4106 /* Generate a mangling for MODULE's global initializer fn. */
4108 tree
4109 mangle_module_global_init (int module)
4111 start_mangling (NULL_TREE);
4113 write_string ("_ZGI");
4114 write_module (module, true);
4116 return finish_mangling_get_identifier ();
4119 /* Generate the mangled name of DECL. */
4121 static tree
4122 mangle_decl_string (const tree decl)
4124 tree result;
4125 tree saved_fn = NULL_TREE;
4126 bool template_p = false;
4128 /* We shouldn't be trying to mangle an uninstantiated template. */
4129 gcc_assert (!type_dependent_expression_p (decl));
4131 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4133 struct tinst_level *tl = current_instantiation ();
4134 if ((!tl || tl->maybe_get_node () != decl)
4135 && push_tinst_level (decl))
4137 template_p = true;
4138 saved_fn = current_function_decl;
4139 current_function_decl = NULL_TREE;
4142 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
4144 start_mangling (decl);
4146 if (TREE_CODE (decl) == TYPE_DECL)
4147 write_type (TREE_TYPE (decl));
4148 else
4149 write_mangled_name (decl, true);
4151 result = finish_mangling_get_identifier ();
4152 if (DEBUG_MANGLE)
4153 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
4154 IDENTIFIER_POINTER (result));
4156 if (template_p)
4158 pop_tinst_level ();
4159 current_function_decl = saved_fn;
4162 return result;
4165 /* Return an identifier for the external mangled name of DECL. */
4167 static tree
4168 get_mangled_id (tree decl)
4170 tree id = mangle_decl_string (decl);
4171 return targetm.mangle_decl_assembler_name (decl, id);
4174 /* Create an identifier for the external mangled name of DECL. */
4176 void
4177 mangle_decl (const tree decl)
4179 tree id;
4180 bool dep;
4182 /* Don't bother mangling uninstantiated templates. */
4183 ++processing_template_decl;
4184 if (TREE_CODE (decl) == TYPE_DECL)
4185 dep = dependent_type_p (TREE_TYPE (decl));
4186 else
4187 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4188 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
4189 --processing_template_decl;
4190 if (dep)
4191 return;
4193 /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
4194 It is not needed to assign names to anonymous namespace, but we use the
4195 "<anon>" marker to be able to tell if type is C++ ODR type or type
4196 produced by other language. */
4197 if (TREE_CODE (decl) == TYPE_DECL
4198 && TYPE_STUB_DECL (TREE_TYPE (decl))
4199 && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
4200 id = get_identifier ("<anon>");
4201 else
4203 gcc_assert (TREE_CODE (decl) != TYPE_DECL
4204 || !no_linkage_check (TREE_TYPE (decl), true));
4205 if (abi_version_at_least (10))
4206 if (tree fn = decl_function_context (decl))
4207 maybe_check_abi_tags (fn, decl);
4208 id = get_mangled_id (decl);
4210 SET_DECL_ASSEMBLER_NAME (decl, id);
4212 if (G.need_cxx17_warning
4213 && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl)))
4214 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type,
4215 "mangled name for %qD will change in C++17 because the "
4216 "exception specification is part of a function type",
4217 decl);
4219 if (id != DECL_NAME (decl)
4220 /* Don't do this for a fake symbol we aren't going to emit anyway. */
4221 && TREE_CODE (decl) != TYPE_DECL
4222 && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
4224 int save_ver = flag_abi_version;
4225 tree id2 = NULL_TREE;
4227 if (!DECL_REALLY_EXTERN (decl))
4229 record_mangling (decl, G.need_abi_warning);
4231 if (!G.need_abi_warning)
4232 return;
4234 flag_abi_version = flag_abi_compat_version;
4235 id2 = mangle_decl_string (decl);
4236 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4237 flag_abi_version = save_ver;
4239 if (id2 != id)
4240 note_mangling_alias (decl, id2);
4243 if (warn_abi)
4245 const char fabi_version[] = "-fabi-version";
4247 if (flag_abi_compat_version != warn_abi_version
4248 || id2 == NULL_TREE)
4250 flag_abi_version = warn_abi_version;
4251 id2 = mangle_decl_string (decl);
4252 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4254 flag_abi_version = save_ver;
4256 if (id2 == id)
4257 /* OK. */;
4258 else if (warn_abi_version != 0
4259 && abi_version_at_least (warn_abi_version))
4260 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4261 "the mangled name of %qD changed between "
4262 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4263 G.entity, fabi_version, warn_abi_version, id2,
4264 fabi_version, save_ver, id);
4265 else
4266 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4267 "the mangled name of %qD changes between "
4268 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4269 G.entity, fabi_version, save_ver, id,
4270 fabi_version, warn_abi_version, id2);
4273 flag_abi_version = save_ver;
4277 /* Generate the mangled representation of TYPE. */
4279 const char *
4280 mangle_type_string (const tree type)
4282 const char *result;
4284 start_mangling (type);
4285 write_type (type);
4286 result = finish_mangling ();
4287 if (DEBUG_MANGLE)
4288 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
4289 return result;
4292 /* Create an identifier for the mangled name of a special component
4293 for belonging to TYPE. CODE is the ABI-specified code for this
4294 component. */
4296 static tree
4297 mangle_special_for_type (const tree type, const char *code)
4299 tree result;
4301 /* We don't have an actual decl here for the special component, so
4302 we can't just process the <encoded-name>. Instead, fake it. */
4303 start_mangling (type);
4305 /* Start the mangling. */
4306 write_string ("_Z");
4307 write_string (code);
4309 /* Add the type. */
4310 write_type (type);
4311 result = finish_mangling_get_identifier ();
4313 if (DEBUG_MANGLE)
4314 fprintf (stderr, "mangle_special_for_type = %s\n\n",
4315 IDENTIFIER_POINTER (result));
4317 return result;
4320 /* Create an identifier for the mangled representation of the typeinfo
4321 structure for TYPE. */
4323 tree
4324 mangle_typeinfo_for_type (const tree type)
4326 return mangle_special_for_type (type, "TI");
4329 /* Create an identifier for the mangled name of the NTBS containing
4330 the mangled name of TYPE. */
4332 tree
4333 mangle_typeinfo_string_for_type (const tree type)
4335 return mangle_special_for_type (type, "TS");
4338 /* Create an identifier for the mangled name of the vtable for TYPE. */
4340 tree
4341 mangle_vtbl_for_type (const tree type)
4343 return mangle_special_for_type (type, "TV");
4346 /* Returns an identifier for the mangled name of the VTT for TYPE. */
4348 tree
4349 mangle_vtt_for_type (const tree type)
4351 return mangle_special_for_type (type, "TT");
4354 /* Returns an identifier for the mangled name of the decomposition
4355 artificial variable DECL. DECLS is the vector of the VAR_DECLs
4356 for the identifier-list. */
4358 tree
4359 mangle_decomp (const tree decl, vec<tree> &decls)
4361 gcc_assert (!type_dependent_expression_p (decl));
4363 location_t saved_loc = input_location;
4364 input_location = DECL_SOURCE_LOCATION (decl);
4366 start_mangling (decl);
4367 write_string ("_Z");
4369 tree context = decl_mangling_context (decl);
4370 gcc_assert (context != NULL_TREE);
4372 bool nested = false;
4373 if (DECL_NAMESPACE_STD_P (context))
4374 write_string ("St");
4375 else if (context != global_namespace)
4377 nested = true;
4378 write_char ('N');
4379 write_prefix (decl_mangling_context (decl));
4382 write_string ("DC");
4383 unsigned int i;
4384 tree d;
4385 FOR_EACH_VEC_ELT (decls, i, d)
4386 write_unqualified_name (d);
4387 write_char ('E');
4389 if (nested)
4390 write_char ('E');
4392 tree id = finish_mangling_get_identifier ();
4393 if (DEBUG_MANGLE)
4394 fprintf (stderr, "mangle_decomp = '%s'\n\n",
4395 IDENTIFIER_POINTER (id));
4397 input_location = saved_loc;
4398 return id;
4401 /* Return an identifier for a construction vtable group. TYPE is
4402 the most derived class in the hierarchy; BINFO is the base
4403 subobject for which this construction vtable group will be used.
4405 This mangling isn't part of the ABI specification; in the ABI
4406 specification, the vtable group is dumped in the same COMDAT as the
4407 main vtable, and is referenced only from that vtable, so it doesn't
4408 need an external name. For binary formats without COMDAT sections,
4409 though, we need external names for the vtable groups.
4411 We use the production
4413 <special-name> ::= CT <type> <offset number> _ <base type> */
4415 tree
4416 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
4418 tree result;
4420 start_mangling (type);
4422 write_string ("_Z");
4423 write_string ("TC");
4424 write_type (type);
4425 write_integer_cst (BINFO_OFFSET (binfo));
4426 write_char ('_');
4427 write_type (BINFO_TYPE (binfo));
4429 result = finish_mangling_get_identifier ();
4430 if (DEBUG_MANGLE)
4431 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
4432 IDENTIFIER_POINTER (result));
4433 return result;
4436 /* Mangle a this pointer or result pointer adjustment.
4438 <call-offset> ::= h <fixed offset number> _
4439 ::= v <fixed offset number> _ <virtual offset number> _ */
4441 static void
4442 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
4444 write_char (virtual_offset ? 'v' : 'h');
4446 /* For either flavor, write the fixed offset. */
4447 write_integer_cst (fixed_offset);
4448 write_char ('_');
4450 /* For a virtual thunk, add the virtual offset. */
4451 if (virtual_offset)
4453 write_integer_cst (virtual_offset);
4454 write_char ('_');
4458 /* Return an identifier for the mangled name of a this-adjusting or
4459 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
4460 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
4461 is a virtual thunk, and it is the vtbl offset in
4462 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
4463 zero for a covariant thunk. Note, that FN_DECL might be a covariant
4464 thunk itself. A covariant thunk name always includes the adjustment
4465 for the this pointer, even if there is none.
4467 <special-name> ::= T <call-offset> <base encoding>
4468 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
4469 <base encoding> */
4471 tree
4472 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
4473 tree virtual_offset, tree thunk)
4475 tree result;
4477 if (abi_version_at_least (11))
4478 maybe_check_abi_tags (fn_decl, thunk, 11);
4480 start_mangling (fn_decl);
4482 write_string ("_Z");
4483 write_char ('T');
4485 if (!this_adjusting)
4487 /* Covariant thunk with no this adjustment */
4488 write_char ('c');
4489 mangle_call_offset (integer_zero_node, NULL_TREE);
4490 mangle_call_offset (fixed_offset, virtual_offset);
4492 else if (!DECL_THUNK_P (fn_decl))
4493 /* Plain this adjusting thunk. */
4494 mangle_call_offset (fixed_offset, virtual_offset);
4495 else
4497 /* This adjusting thunk to covariant thunk. */
4498 write_char ('c');
4499 mangle_call_offset (fixed_offset, virtual_offset);
4500 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
4501 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
4502 if (virtual_offset)
4503 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
4504 mangle_call_offset (fixed_offset, virtual_offset);
4505 fn_decl = THUNK_TARGET (fn_decl);
4508 /* Scoped name. */
4509 write_encoding (fn_decl);
4511 result = finish_mangling_get_identifier ();
4512 if (DEBUG_MANGLE)
4513 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
4514 return result;
4517 /* Handle ABI backwards compatibility for past bugs where we didn't call
4518 check_abi_tags in places where it's needed: call check_abi_tags and warn if
4519 it makes a difference. If FOR_DECL is non-null, it's the declaration
4520 that we're actually trying to mangle; if it's null, we're mangling the
4521 guard variable for T. */
4523 static void
4524 maybe_check_abi_tags (tree t, tree for_decl, int ver)
4526 if (DECL_ASSEMBLER_NAME_SET_P (t))
4527 return;
4529 tree oldtags = get_abi_tags (t);
4531 mangle_decl (t);
4533 tree newtags = get_abi_tags (t);
4534 if (newtags && newtags != oldtags
4535 && abi_version_crosses (ver))
4537 if (for_decl && DECL_THUNK_P (for_decl))
4538 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4539 "the mangled name of a thunk for %qD changes between "
4540 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4541 t, flag_abi_version, warn_abi_version);
4542 else if (for_decl)
4543 warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
4544 "the mangled name of %qD changes between "
4545 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4546 for_decl, flag_abi_version, warn_abi_version);
4547 else
4548 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4549 "the mangled name of the initialization guard variable "
4550 "for %qD changes between %<-fabi-version=%d%> and "
4551 "%<-fabi-version=%d%>",
4552 t, flag_abi_version, warn_abi_version);
4556 /* Write out the appropriate string for this variable when generating
4557 another mangled name based on this one. */
4559 static void
4560 write_guarded_var_name (const tree variable)
4562 if (DECL_NAME (variable)
4563 && startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR"))
4564 /* The name of a guard variable for a reference temporary should refer
4565 to the reference, not the temporary. */
4566 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
4567 else
4568 write_name (variable, /*ignore_local_scope=*/0);
4571 /* Return an identifier for the name of an initialization guard
4572 variable for indicated VARIABLE. */
4574 tree
4575 mangle_guard_variable (const tree variable)
4577 if (abi_version_at_least (10))
4578 maybe_check_abi_tags (variable);
4579 start_mangling (variable);
4580 write_string ("_ZGV");
4581 write_guarded_var_name (variable);
4582 return finish_mangling_get_identifier ();
4585 /* Return an identifier for the name of a thread_local initialization
4586 function for VARIABLE. */
4588 tree
4589 mangle_tls_init_fn (const tree variable)
4591 check_abi_tags (variable);
4592 start_mangling (variable);
4593 write_string ("_ZTH");
4594 write_guarded_var_name (variable);
4595 return finish_mangling_get_identifier ();
4598 /* Return an identifier for the name of a thread_local wrapper
4599 function for VARIABLE. */
4601 #define TLS_WRAPPER_PREFIX "_ZTW"
4603 tree
4604 mangle_tls_wrapper_fn (const tree variable)
4606 check_abi_tags (variable);
4607 start_mangling (variable);
4608 write_string (TLS_WRAPPER_PREFIX);
4609 write_guarded_var_name (variable);
4610 return finish_mangling_get_identifier ();
4613 /* Return true iff FN is a thread_local wrapper function. */
4615 bool
4616 decl_tls_wrapper_p (const tree fn)
4618 if (TREE_CODE (fn) != FUNCTION_DECL)
4619 return false;
4620 tree name = DECL_NAME (fn);
4621 return startswith (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX);
4624 /* Return an identifier for the name of a temporary variable used to
4625 initialize a static reference. This is now part of the ABI. */
4627 tree
4628 mangle_ref_init_variable (const tree variable)
4630 start_mangling (variable);
4631 write_string ("_ZGR");
4632 check_abi_tags (variable);
4633 write_name (variable, /*ignore_local_scope=*/0);
4634 /* Avoid name clashes with aggregate initialization of multiple
4635 references at once. */
4636 write_compact_number (current_ref_temp_count++);
4637 return finish_mangling_get_identifier ();
4640 /* Return an identifier for the mangled name of a C++20 template parameter
4641 object for template argument EXPR. */
4643 tree
4644 mangle_template_parm_object (tree expr)
4646 start_mangling (expr);
4647 write_string ("_ZTAX");
4648 write_expression (expr);
4649 write_char ('E');
4650 return finish_mangling_get_identifier ();
4653 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
4654 function generates a mangled name for the vtable map variable of
4655 the class type. For example, if the class type is
4656 "std::bad_exception", the mangled name for the class is
4657 "St13bad_exception". This function would generate the name
4658 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
4659 "_VTV<std::bad_exception>::__vtable_map". */
4662 char *
4663 get_mangled_vtable_map_var_name (tree class_type)
4665 char *var_name = NULL;
4666 const char *prefix = "_ZN4_VTVI";
4667 const char *postfix = "E12__vtable_mapE";
4669 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
4671 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
4673 if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
4675 class_id = get_mangled_id (TYPE_NAME (class_type));
4676 vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
4679 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
4680 strlen (prefix) +
4681 strlen (postfix) + 1;
4683 var_name = (char *) xmalloc (len);
4685 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
4687 return var_name;
4690 #include "gt-cp-mangle.h"