aarch64: Use dup and zip1 for interleaving elements in vector initializer.
[official-gcc.git] / gcc / cp / mangle.cc
blobe363ef35b9f0221c320563f74e112a658a14e67c
1 /* Name mangling for the 3.0 -*- C++ -*- ABI.
2 Copyright (C) 2000-2022 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);
803 /* Returns true if the return type of DECL is part of its signature, and
804 therefore its mangling. */
806 bool
807 mangle_return_type_p (tree decl)
809 return (!DECL_CONSTRUCTOR_P (decl)
810 && !DECL_DESTRUCTOR_P (decl)
811 && !DECL_CONV_FN_P (decl)
812 && maybe_template_info (decl));
815 /* <encoding> ::= <function name> <bare-function-type>
816 ::= <data name> */
818 static void
819 write_encoding (const tree decl)
821 MANGLE_TRACE_TREE ("encoding", decl);
823 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
825 /* For overloaded operators write just the mangled name
826 without arguments. */
827 if (DECL_OVERLOADED_OPERATOR_P (decl))
828 write_name (decl, /*ignore_local_scope=*/0);
829 else
830 write_source_name (DECL_NAME (decl));
831 return;
834 write_name (decl, /*ignore_local_scope=*/0);
835 if (TREE_CODE (decl) == FUNCTION_DECL)
837 tree fn_type;
838 tree d;
840 if (maybe_template_info (decl))
842 fn_type = get_mostly_instantiated_function_type (decl);
843 /* FN_TYPE will not have parameter types for in-charge or
844 VTT parameters. Therefore, we pass NULL_TREE to
845 write_bare_function_type -- otherwise, it will get
846 confused about which artificial parameters to skip. */
847 d = NULL_TREE;
849 else
851 fn_type = TREE_TYPE (decl);
852 d = decl;
855 write_bare_function_type (fn_type,
856 mangle_return_type_p (decl),
859 /* If this is the pre/post function for a guarded function, append
860 .pre/post, like something from create_virtual_clone. */
861 if (DECL_IS_PRE_FN_P (decl))
862 write_string (".pre");
863 else if (DECL_IS_POST_FN_P (decl))
864 write_string (".post");
866 /* If this is a coroutine helper, then append an appropriate string to
867 identify which. */
868 if (tree ramp = DECL_RAMP_FN (decl))
870 if (DECL_ACTOR_FN (ramp) == decl)
871 write_string (JOIN_STR "actor");
872 else if (DECL_DESTROY_FN (ramp) == decl)
873 write_string (JOIN_STR "destroy");
874 else
875 gcc_unreachable ();
880 /* Interface to substitution and identifier mangling, used by the
881 module name mangler. */
883 void
884 mangle_module_substitution (int v)
886 write_substitution (v - 1);
890 mangle_module_component (tree comp, bool partition_p)
892 write_char ('W');
893 if (partition_p)
894 write_char ('P');
895 write_source_name (comp);
897 // Module substitutions use the same number-space as entity
898 // substitutions, but are orthogonal.
899 vec_safe_push (G.substitutions, NULL_TREE);
900 return G.substitutions->length ();
903 /* If the outermost non-namespace context (including DECL itself) is
904 a module-linkage decl, mangle the module information. For module
905 global initializers we need to include the partition part.
907 <module-name> ::= <module-sub>
908 || <subst>
909 || <module-name> <module-sub>
910 <module-sub> :: W [P] <unqualified-name>
913 static void
914 write_module (int m, bool include_partition)
916 G.mod = true;
917 mangle_module (m, include_partition);
920 static void
921 maybe_write_module (tree decl)
923 if (!DECL_NAMESPACE_SCOPE_P (decl))
924 return;
926 if (!TREE_PUBLIC (STRIP_TEMPLATE (decl)))
927 return;
929 if (TREE_CODE (decl) == NAMESPACE_DECL)
930 return;
932 int m = get_originating_module (decl, true);
933 if (m >= 0)
934 write_module (m, false);
937 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
938 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
940 static tree
941 decl_mangling_context (tree decl)
943 tree tcontext = targetm.cxx.decl_mangling_context (decl);
945 if (tcontext != NULL_TREE)
946 return tcontext;
948 if (TREE_CODE (decl) == TEMPLATE_DECL
949 && DECL_TEMPLATE_RESULT (decl))
950 decl = DECL_TEMPLATE_RESULT (decl);
952 if (TREE_CODE (decl) == TYPE_DECL
953 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
955 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
956 if (extra)
957 return extra;
959 else if (template_type_parameter_p (decl))
960 /* template type parms have no mangling context. */
961 return NULL_TREE;
963 tcontext = CP_DECL_CONTEXT (decl);
965 /* Ignore the artificial declare reduction functions. */
966 if (tcontext
967 && TREE_CODE (tcontext) == FUNCTION_DECL
968 && DECL_OMP_DECLARE_REDUCTION_P (tcontext))
969 return decl_mangling_context (tcontext);
971 return tcontext;
974 /* <name> ::= <unscoped-name>
975 ::= <unscoped-template-name> <template-args>
976 ::= <nested-name>
977 ::= <local-name>
979 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
980 called from <local-name>, which mangles the enclosing scope
981 elsewhere and then uses this function to mangle just the part
982 underneath the function scope. So don't use the <local-name>
983 production, to avoid an infinite recursion. */
985 static void
986 write_name (tree decl, const int ignore_local_scope)
988 tree context;
990 MANGLE_TRACE_TREE ("name", decl);
992 if (TREE_CODE (decl) == TYPE_DECL)
994 /* In case this is a typedef, fish out the corresponding
995 TYPE_DECL for the main variant. */
996 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
999 context = decl_mangling_context (decl);
1001 gcc_assert (context != NULL_TREE);
1003 if (abi_warn_or_compat_version_crosses (7)
1004 && ignore_local_scope
1005 && TREE_CODE (context) == PARM_DECL)
1006 G.need_abi_warning = 1;
1008 /* A decl in :: or ::std scope is treated specially. The former is
1009 mangled using <unscoped-name> or <unscoped-template-name>, the
1010 latter with a special substitution. Also, a name that is
1011 directly in a local function scope is also mangled with
1012 <unscoped-name> rather than a full <nested-name>. */
1013 if (context == global_namespace
1014 || DECL_NAMESPACE_STD_P (context)
1015 || (ignore_local_scope
1016 && (TREE_CODE (context) == FUNCTION_DECL
1017 || (abi_version_at_least (7)
1018 && TREE_CODE (context) == PARM_DECL))))
1020 /* Is this a template instance? */
1021 if (tree info = maybe_template_info (decl))
1023 /* Yes: use <unscoped-template-name>. */
1024 write_unscoped_template_name (TI_TEMPLATE (info));
1025 write_template_args (TI_ARGS (info));
1027 else
1028 /* Everything else gets an <unqualified-name>. */
1029 write_unscoped_name (decl);
1031 else
1033 /* Handle local names, unless we asked not to (that is, invoked
1034 under <local-name>, to handle only the part of the name under
1035 the local scope). */
1036 if (!ignore_local_scope)
1038 /* Scan up the list of scope context, looking for a
1039 function. If we find one, this entity is in local
1040 function scope. local_entity tracks context one scope
1041 level down, so it will contain the element that's
1042 directly in that function's scope, either decl or one of
1043 its enclosing scopes. */
1044 tree local_entity = decl;
1045 while (context != global_namespace)
1047 /* Make sure we're always dealing with decls. */
1048 if (TYPE_P (context))
1049 context = TYPE_NAME (context);
1050 /* Is this a function? */
1051 if (TREE_CODE (context) == FUNCTION_DECL
1052 || TREE_CODE (context) == PARM_DECL)
1054 /* Yes, we have local scope. Use the <local-name>
1055 production for the innermost function scope. */
1056 write_local_name (context, local_entity, decl);
1057 return;
1059 /* Up one scope level. */
1060 local_entity = context;
1061 context = decl_mangling_context (context);
1064 /* No local scope found? Fall through to <nested-name>. */
1067 /* Other decls get a <nested-name> to encode their scope. */
1068 write_nested_name (decl);
1072 /* <unscoped-name> ::= <unqualified-name>
1073 ::= St <unqualified-name> # ::std:: */
1075 static void
1076 write_unscoped_name (const tree decl)
1078 tree context = decl_mangling_context (decl);
1080 MANGLE_TRACE_TREE ("unscoped-name", decl);
1082 /* Is DECL in ::std? */
1083 if (DECL_NAMESPACE_STD_P (context))
1085 write_string ("St");
1086 write_unqualified_name (decl);
1088 else
1090 /* If not, it should be either in the global namespace, or directly
1091 in a local function scope. A lambda can also be mangled in the
1092 scope of a default argument. */
1093 gcc_assert (context == global_namespace
1094 || TREE_CODE (context) == PARM_DECL
1095 || TREE_CODE (context) == FUNCTION_DECL);
1097 write_unqualified_name (decl);
1101 /* <unscoped-template-name> ::= <unscoped-name>
1102 ::= <substitution> */
1104 static void
1105 write_unscoped_template_name (const tree decl)
1107 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
1109 if (find_substitution (decl))
1110 return;
1111 write_unscoped_name (decl);
1112 add_substitution (decl);
1115 /* Write the nested name, including CV-qualifiers, of DECL.
1117 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1118 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1120 <ref-qualifier> ::= R # & ref-qualifier
1121 ::= O # && ref-qualifier
1122 <CV-qualifiers> ::= [r] [V] [K] */
1124 static void
1125 write_nested_name (const tree decl)
1127 MANGLE_TRACE_TREE ("nested-name", decl);
1129 write_char ('N');
1131 /* Write CV-qualifiers, if this is a member function. */
1132 if (TREE_CODE (decl) == FUNCTION_DECL
1133 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1135 if (DECL_VOLATILE_MEMFUNC_P (decl))
1136 write_char ('V');
1137 if (DECL_CONST_MEMFUNC_P (decl))
1138 write_char ('K');
1139 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
1141 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
1142 write_char ('O');
1143 else
1144 write_char ('R');
1148 /* Is this a template instance? */
1149 if (tree info = maybe_template_info (decl))
1151 /* Yes, use <template-prefix>. */
1152 write_template_prefix (decl);
1153 write_template_args (TI_ARGS (info));
1155 else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
1156 && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1158 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1159 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1161 write_template_prefix (decl);
1162 write_template_args (TREE_OPERAND (name, 1));
1164 else
1166 write_prefix (decl_mangling_context (decl));
1167 write_unqualified_name (decl);
1170 else
1172 /* No, just use <prefix> */
1173 write_prefix (decl_mangling_context (decl));
1174 write_unqualified_name (decl);
1176 write_char ('E');
1179 /* <prefix> ::= <prefix> <unqualified-name>
1180 ::= <template-param>
1181 ::= <template-prefix> <template-args>
1182 ::= <decltype>
1183 ::= # empty
1184 ::= <substitution> */
1186 static void
1187 write_prefix (const tree node)
1189 tree decl;
1191 if (node == NULL
1192 || node == global_namespace)
1193 return;
1195 MANGLE_TRACE_TREE ("prefix", node);
1197 if (TREE_CODE (node) == DECLTYPE_TYPE)
1199 write_type (node);
1200 return;
1203 if (find_substitution (node))
1204 return;
1206 tree template_info = NULL_TREE;
1207 if (DECL_P (node))
1209 /* If this is a function or parm decl, that means we've hit function
1210 scope, so this prefix must be for a local name. In this
1211 case, we're under the <local-name> production, which encodes
1212 the enclosing function scope elsewhere. So don't continue
1213 here. */
1214 if (TREE_CODE (node) == FUNCTION_DECL
1215 || TREE_CODE (node) == PARM_DECL)
1216 return;
1218 decl = node;
1219 template_info = maybe_template_info (decl);
1221 else
1223 /* Node is a type. */
1224 decl = TYPE_NAME (node);
1225 /* The DECL might not point at the node. */
1226 if (CLASSTYPE_TEMPLATE_ID_P (node))
1227 template_info = TYPE_TEMPLATE_INFO (node);
1230 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1231 write_template_param (node);
1232 else if (template_info)
1233 /* Templated. */
1235 write_template_prefix (decl);
1236 write_template_args (TI_ARGS (template_info));
1238 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1240 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1241 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1243 write_template_prefix (decl);
1244 write_template_args (TREE_OPERAND (name, 1));
1246 else
1248 write_prefix (decl_mangling_context (decl));
1249 write_unqualified_name (decl);
1252 else
1253 /* Not templated. */
1255 write_prefix (decl_mangling_context (decl));
1256 write_unqualified_name (decl);
1257 if (VAR_P (decl)
1258 || TREE_CODE (decl) == FIELD_DECL)
1260 /* <data-member-prefix> := <member source-name> M */
1261 write_char ('M');
1263 /* Before ABI 18, we did not count these as substitution
1264 candidates. This leads to incorrect demanglings (and
1265 ABI divergence to other compilers). */
1266 if (abi_warn_or_compat_version_crosses (18))
1267 G.need_abi_warning = true;
1268 if (!abi_version_at_least (18))
1269 return;
1273 add_substitution (node);
1276 /* <template-prefix> ::= <prefix> <template component>
1277 ::= <template-param>
1278 ::= <substitution> */
1280 static void
1281 write_template_prefix (const tree node)
1283 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1284 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1285 tree context = decl_mangling_context (decl);
1286 tree templ;
1287 tree substitution;
1289 MANGLE_TRACE_TREE ("template-prefix", node);
1291 /* Find the template decl. */
1292 if (tree info = maybe_template_info (decl))
1293 templ = TI_TEMPLATE (info);
1294 else if (TREE_CODE (type) == TYPENAME_TYPE)
1295 /* For a typename type, all we have is the name. */
1296 templ = DECL_NAME (decl);
1297 else
1299 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1301 templ = TYPE_TI_TEMPLATE (type);
1304 /* For a member template, though, the template name for the
1305 innermost name must have all the outer template levels
1306 instantiated. For instance, consider
1308 template<typename T> struct Outer {
1309 template<typename U> struct Inner {};
1312 The template name for `Inner' in `Outer<int>::Inner<float>' is
1313 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1314 levels separately, so there's no TEMPLATE_DECL available for this
1315 (there's only `Outer<T>::Inner<U>').
1317 In order to get the substitutions right, we create a special
1318 TREE_LIST to represent the substitution candidate for a nested
1319 template. The TREE_PURPOSE is the template's context, fully
1320 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1321 template.
1323 So, for the example above, `Outer<int>::Inner' is represented as a
1324 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1325 and whose value is `Outer<T>::Inner<U>'. */
1326 if (context && TYPE_P (context))
1327 substitution = build_tree_list (context, templ);
1328 else
1329 substitution = templ;
1331 if (find_substitution (substitution))
1332 return;
1334 if (TREE_TYPE (templ)
1335 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1336 write_template_param (TREE_TYPE (templ));
1337 else
1339 write_prefix (context);
1340 write_unqualified_name (decl);
1343 add_substitution (substitution);
1346 /* As the list of identifiers for the structured binding declaration
1347 DECL is likely gone, try to recover the DC <source-name>+ E portion
1348 from its mangled name. Return pointer to the DC and set len to
1349 the length up to and including the terminating E. On failure
1350 return NULL. */
1352 static const char *
1353 find_decomp_unqualified_name (tree decl, size_t *len)
1355 const char *p = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1356 const char *end = p + IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (decl));
1357 bool nested = false;
1358 if (!startswith (p, "_Z"))
1359 return NULL;
1360 p += 2;
1361 if (startswith (p, "St"))
1362 p += 2;
1363 else if (*p == 'N')
1365 nested = true;
1366 ++p;
1367 while (ISDIGIT (p[0]))
1369 char *e;
1370 long num = strtol (p, &e, 10);
1371 if (num >= 1 && num < end - e)
1372 p = e + num;
1373 else
1374 break;
1377 if (!startswith (p, "DC"))
1378 return NULL;
1379 if (nested)
1381 if (end[-1] != 'E')
1382 return NULL;
1383 --end;
1385 if (end[-1] != 'E')
1386 return NULL;
1387 *len = end - p;
1388 return p;
1391 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1392 mangled through special entry points.
1394 <unqualified-name> ::= [<module-name>] <operator-name>
1395 ::= <special-name>
1396 ::= [<module-name>] <source-name>
1397 ::= [<module-name>] <unnamed-type-name>
1398 ::= <local-source-name>
1400 <local-source-name> ::= L <source-name> <discriminator> */
1402 static void
1403 write_unqualified_id (tree identifier)
1405 if (IDENTIFIER_CONV_OP_P (identifier))
1406 write_conversion_operator_name (TREE_TYPE (identifier));
1407 else if (IDENTIFIER_OVL_OP_P (identifier))
1409 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier);
1410 write_string (ovl_op->mangled_name);
1412 else if (UDLIT_OPER_P (identifier))
1413 write_literal_operator_name (identifier);
1414 else
1415 write_source_name (identifier);
1418 static void
1419 write_unqualified_name (tree decl)
1421 MANGLE_TRACE_TREE ("unqualified-name", decl);
1423 if (modules_p ())
1424 maybe_write_module (decl);
1426 if (identifier_p (decl))
1428 write_unqualified_id (decl);
1429 return;
1432 bool found = false;
1434 if (DECL_NAME (decl) == NULL_TREE)
1436 found = true;
1437 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1438 const char *decomp_str = NULL;
1439 size_t decomp_len = 0;
1440 if (VAR_P (decl)
1441 && DECL_DECOMPOSITION_P (decl)
1442 && DECL_NAME (decl) == NULL_TREE
1443 && DECL_NAMESPACE_SCOPE_P (decl))
1444 decomp_str = find_decomp_unqualified_name (decl, &decomp_len);
1445 if (decomp_str)
1446 write_chars (decomp_str, decomp_len);
1447 else
1448 write_source_name (DECL_ASSEMBLER_NAME (decl));
1450 else if (DECL_DECLARES_FUNCTION_P (decl))
1452 found = true;
1453 if (DECL_CONSTRUCTOR_P (decl))
1454 write_special_name_constructor (decl);
1455 else if (DECL_DESTRUCTOR_P (decl))
1456 write_special_name_destructor (decl);
1457 else if (DECL_CONV_FN_P (decl))
1459 /* Conversion operator. Handle it right here.
1460 <operator> ::= cv <type> */
1461 tree type;
1462 if (maybe_template_info (decl))
1464 tree fn_type;
1465 fn_type = get_mostly_instantiated_function_type (decl);
1466 type = TREE_TYPE (fn_type);
1468 else if (FNDECL_USED_AUTO (decl))
1469 type = DECL_SAVED_AUTO_RETURN_TYPE (decl);
1470 else
1471 type = DECL_CONV_FN_TYPE (decl);
1472 write_conversion_operator_name (type);
1474 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1476 tree t;
1477 if (!(t = DECL_RAMP_FN (decl)))
1478 t = decl;
1479 const char *mangled_name
1480 = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (t)]
1481 [DECL_OVERLOADED_OPERATOR_CODE_RAW (t)].mangled_name);
1482 write_string (mangled_name);
1484 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1485 write_literal_operator_name (DECL_NAME (decl));
1486 else
1487 found = false;
1490 if (found)
1491 /* OK */;
1492 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1493 && DECL_NAMESPACE_SCOPE_P (decl)
1494 && decl_linkage (decl) == lk_internal)
1496 MANGLE_TRACE_TREE ("local-source-name", decl);
1497 write_char ('L');
1498 write_source_name (DECL_NAME (decl));
1499 /* The default discriminator is 1, and that's all we ever use,
1500 so there's no code to output one here. */
1502 else
1504 tree type = TREE_TYPE (decl);
1506 if (TREE_CODE (decl) == TYPE_DECL
1507 && TYPE_UNNAMED_P (type))
1508 write_unnamed_type_name (type);
1509 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type))
1510 write_closure_type_name (type);
1511 else
1512 write_source_name (DECL_NAME (decl));
1515 /* We use the ABI tags from the primary class template, ignoring tags on any
1516 specializations. This is necessary because C++ doesn't require a
1517 specialization to be declared before it is used unless the use requires a
1518 complete type, but we need to get the tags right on incomplete types as
1519 well. */
1520 if (tree tmpl = most_general_template (decl))
1522 tree res = DECL_TEMPLATE_RESULT (tmpl);
1523 if (res == NULL_TREE)
1524 /* UNBOUND_CLASS_TEMPLATE. */;
1525 else if (DECL_DECLARES_TYPE_P (decl))
1526 decl = res;
1527 else if (any_abi_below (11))
1529 /* ABI v10 implicit tags on the template. */
1530 tree mtags = missing_abi_tags (res);
1531 /* Explicit tags on the template. */
1532 tree ttags = get_abi_tags (res);
1533 /* Tags on the instantiation. */
1534 tree dtags = get_abi_tags (decl);
1536 if (mtags && abi_warn_or_compat_version_crosses (10))
1537 G.need_abi_warning = 1;
1539 /* Add the v10 tags to the explicit tags now. */
1540 mtags = chainon (mtags, ttags);
1542 if (!G.need_abi_warning
1543 && abi_warn_or_compat_version_crosses (11)
1544 && !equal_abi_tags (dtags, mtags))
1545 G.need_abi_warning = 1;
1547 if (!abi_version_at_least (10))
1548 /* In abi <10, we only got the explicit tags. */
1549 decl = res;
1550 else if (flag_abi_version == 10)
1552 /* In ABI 10, we want explict and implicit tags. */
1553 write_abi_tags (mtags);
1554 return;
1559 tree tags = get_abi_tags (decl);
1560 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
1561 && any_abi_below (11))
1562 if (tree mtags = missing_abi_tags (decl))
1564 if (abi_warn_or_compat_version_crosses (11))
1565 G.need_abi_warning = true;
1566 if (!abi_version_at_least (11))
1567 tags = chainon (mtags, tags);
1569 write_abi_tags (tags);
1572 /* Write the unqualified-name for a conversion operator to TYPE. */
1574 static void
1575 write_conversion_operator_name (const tree type)
1577 write_string ("cv");
1578 write_type (type);
1581 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1583 <source-name> ::= </length/ number> <identifier> */
1585 static void
1586 write_source_name (tree identifier)
1588 MANGLE_TRACE_TREE ("source-name", identifier);
1590 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1591 write_identifier (IDENTIFIER_POINTER (identifier));
1594 /* Compare two TREE_STRINGs like strcmp. */
1597 tree_string_cmp (const void *p1, const void *p2)
1599 if (p1 == p2)
1600 return 0;
1601 tree s1 = *(const tree*)p1;
1602 tree s2 = *(const tree*)p2;
1603 return strcmp (TREE_STRING_POINTER (s1),
1604 TREE_STRING_POINTER (s2));
1607 /* Return the TREE_LIST of TAGS as a sorted VEC. */
1609 static vec<tree, va_gc> *
1610 sorted_abi_tags (tree tags)
1612 vec<tree, va_gc> * vec = make_tree_vector();
1614 for (tree t = tags; t; t = TREE_CHAIN (t))
1616 if (ABI_TAG_IMPLICIT (t))
1617 continue;
1618 tree str = TREE_VALUE (t);
1619 vec_safe_push (vec, str);
1622 vec->qsort (tree_string_cmp);
1624 return vec;
1627 /* ID is the name of a function or type with abi_tags attribute TAGS.
1628 Write out the name, suitably decorated. */
1630 static void
1631 write_abi_tags (tree tags)
1633 if (tags == NULL_TREE)
1634 return;
1636 vec<tree, va_gc> * vec = sorted_abi_tags (tags);
1638 unsigned i; tree str;
1639 FOR_EACH_VEC_ELT (*vec, i, str)
1641 write_string ("B");
1642 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1643 write_identifier (TREE_STRING_POINTER (str));
1646 release_tree_vector (vec);
1649 /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */
1651 static bool
1652 equal_abi_tags (tree t1, tree t2)
1654 releasing_vec v1 = sorted_abi_tags (t1);
1655 releasing_vec v2 = sorted_abi_tags (t2);
1657 unsigned len1 = v1->length();
1658 if (len1 != v2->length())
1659 return false;
1660 for (unsigned i = 0; i < len1; ++i)
1661 if (tree_string_cmp (v1[i], v2[i]) != 0)
1662 return false;
1663 return true;
1666 /* Write a user-defined literal operator.
1667 ::= li <source-name> # "" <source-name>
1668 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1670 static void
1671 write_literal_operator_name (tree identifier)
1673 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1674 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1675 write_unsigned_number (strlen (suffix));
1676 write_identifier (suffix);
1679 /* Encode 0 as _, and 1+ as n-1_. */
1681 static void
1682 write_compact_number (int num)
1684 gcc_checking_assert (num >= 0);
1685 if (num > 0)
1686 write_unsigned_number (num - 1);
1687 write_char ('_');
1690 /* Return how many unnamed types precede TYPE in its enclosing class. */
1692 static int
1693 nested_anon_class_index (tree type)
1695 int index = 0;
1696 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1697 for (; member; member = DECL_CHAIN (member))
1698 if (DECL_IMPLICIT_TYPEDEF_P (member))
1700 tree memtype = TREE_TYPE (member);
1701 if (memtype == type)
1702 return index;
1703 else if (TYPE_UNNAMED_P (memtype))
1704 ++index;
1707 if (seen_error ())
1708 return -1;
1710 gcc_unreachable ();
1713 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1715 static void
1716 write_unnamed_type_name (const tree type)
1718 int discriminator;
1719 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1721 if (TYPE_FUNCTION_SCOPE_P (type))
1722 discriminator = discriminator_for_local_entity (TYPE_NAME (type));
1723 else if (TYPE_CLASS_SCOPE_P (type))
1724 discriminator = nested_anon_class_index (type);
1725 else
1727 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1728 /* Just use the old mangling at namespace scope. */
1729 write_source_name (TYPE_IDENTIFIER (type));
1730 return;
1733 write_string ("Ut");
1734 write_compact_number (discriminator);
1737 // A template head, for templated lambdas.
1738 // <template-head> ::= Tp* Ty
1739 // Tp* Tn <type>
1740 // Tp* Tt <template-head> E
1741 // New in ABI=18. Returns true iff we emitted anything -- used for ABI
1742 // version warning.
1744 static bool
1745 write_closure_template_head (tree tmpl)
1747 bool any = false;
1749 // We only need one level of template parms
1750 tree inner = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
1752 for (int ix = 0, len = TREE_VEC_LENGTH (inner); ix != len; ix++)
1754 tree parm = TREE_VEC_ELT (inner, ix);
1755 if (parm == error_mark_node)
1756 continue;
1757 parm = TREE_VALUE (parm);
1759 if (DECL_VIRTUAL_P (parm))
1760 // A synthetic parm, we're done.
1761 break;
1763 any = true;
1764 if (abi_version_at_least (18))
1766 if (TREE_CODE (parm) == PARM_DECL
1767 ? TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
1768 : TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
1769 write_string ("Tp");
1771 switch (TREE_CODE (parm))
1773 default:
1774 gcc_unreachable ();
1776 case TYPE_DECL:
1777 write_string ("Ty");
1778 break;
1780 case PARM_DECL:
1781 write_string ("Tn");
1782 write_type (TREE_TYPE (parm));
1783 break;
1785 case TEMPLATE_DECL:
1786 write_string ("Tt");
1787 write_closure_template_head (parm);
1788 write_string ("E");
1789 break;
1794 return any;
1797 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1798 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1800 static void
1801 write_closure_type_name (const tree type)
1803 tree fn = lambda_function (type);
1804 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1805 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1807 MANGLE_TRACE_TREE ("closure-type-name", type);
1809 write_string ("Ul");
1811 if (auto ti = maybe_template_info (fn))
1812 if (write_closure_template_head (TI_TEMPLATE (ti)))
1813 // If there were any explicit template parms, we may need to
1814 // issue a mangling diagnostic.
1815 if (abi_warn_or_compat_version_crosses (18))
1816 G.need_abi_warning = true;
1818 write_method_parms (parms, /*method_p=*/1, fn);
1819 write_char ('E');
1820 if ((LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
1821 != LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda))
1822 && abi_warn_or_compat_version_crosses (18))
1823 G.need_abi_warning = true;
1824 write_compact_number (abi_version_at_least (18)
1825 ? LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
1826 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda));
1829 /* Convert NUMBER to ascii using base BASE and generating at least
1830 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1831 into which to store the characters. Returns the number of
1832 characters generated (these will be laid out in advance of where
1833 BUFFER points). */
1835 static int
1836 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1837 char *buffer, const unsigned int min_digits)
1839 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1840 unsigned digits = 0;
1842 while (number)
1844 unsigned HOST_WIDE_INT d = number / base;
1846 *--buffer = base_digits[number - d * base];
1847 digits++;
1848 number = d;
1850 while (digits < min_digits)
1852 *--buffer = base_digits[0];
1853 digits++;
1855 return digits;
1858 /* Non-terminal <number>.
1860 <number> ::= [n] </decimal integer/> */
1862 static void
1863 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1864 const unsigned int base)
1866 char buffer[sizeof (HOST_WIDE_INT) * 8];
1867 unsigned count = 0;
1869 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1871 write_char ('n');
1872 number = -((HOST_WIDE_INT) number);
1874 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1875 write_chars (buffer + sizeof (buffer) - count, count);
1878 /* Write out an integral CST in decimal. Most numbers are small, and
1879 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1880 bigger than that, which we must deal with. */
1882 static inline void
1883 write_integer_cst (const tree cst)
1885 int sign = tree_int_cst_sgn (cst);
1886 widest_int abs_value = wi::abs (wi::to_widest (cst));
1887 if (!wi::fits_uhwi_p (abs_value))
1889 /* A bignum. We do this in chunks, each of which fits in a
1890 HOST_WIDE_INT. */
1891 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1892 unsigned HOST_WIDE_INT chunk;
1893 unsigned chunk_digits;
1894 char *ptr = buffer + sizeof (buffer);
1895 unsigned count = 0;
1896 tree n, base, type;
1897 int done;
1899 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1900 representable. */
1901 chunk = 1000000000;
1902 chunk_digits = 9;
1904 if (sizeof (HOST_WIDE_INT) >= 8)
1906 /* It is at least 64 bits, so 10^18 is representable. */
1907 chunk_digits = 18;
1908 chunk *= chunk;
1911 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1912 base = build_int_cstu (type, chunk);
1913 n = wide_int_to_tree (type, wi::to_wide (cst));
1915 if (sign < 0)
1917 write_char ('n');
1918 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1922 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1923 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1924 unsigned c;
1926 done = integer_zerop (d);
1927 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1928 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1929 done ? 1 : chunk_digits);
1930 ptr -= c;
1931 count += c;
1932 n = d;
1934 while (!done);
1935 write_chars (ptr, count);
1937 else
1939 /* A small num. */
1940 if (sign < 0)
1941 write_char ('n');
1942 write_unsigned_number (abs_value.to_uhwi ());
1946 /* Write out a floating-point literal.
1948 "Floating-point literals are encoded using the bit pattern of the
1949 target processor's internal representation of that number, as a
1950 fixed-length lowercase hexadecimal string, high-order bytes first
1951 (even if the target processor would store low-order bytes first).
1952 The "n" prefix is not used for floating-point literals; the sign
1953 bit is encoded with the rest of the number.
1955 Here are some examples, assuming the IEEE standard representation
1956 for floating point numbers. (Spaces are for readability, not
1957 part of the encoding.)
1959 1.0f Lf 3f80 0000 E
1960 -1.0f Lf bf80 0000 E
1961 1.17549435e-38f Lf 0080 0000 E
1962 1.40129846e-45f Lf 0000 0001 E
1963 0.0f Lf 0000 0000 E"
1965 Caller is responsible for the Lx and the E. */
1966 static void
1967 write_real_cst (const tree value)
1969 long target_real[4]; /* largest supported float */
1970 /* Buffer for eight hex digits in a 32-bit number but big enough
1971 even for 64-bit long to avoid warnings. */
1972 char buffer[17];
1973 int i, limit, dir;
1975 tree type = TREE_TYPE (value);
1976 int words = GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type)) / 32;
1978 real_to_target (target_real, &TREE_REAL_CST (value),
1979 TYPE_MODE (type));
1981 /* The value in target_real is in the target word order,
1982 so we must write it out backward if that happens to be
1983 little-endian. write_number cannot be used, it will
1984 produce uppercase. */
1985 if (FLOAT_WORDS_BIG_ENDIAN)
1986 i = 0, limit = words, dir = 1;
1987 else
1988 i = words - 1, limit = -1, dir = -1;
1990 for (; i != limit; i += dir)
1992 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1993 write_chars (buffer, 8);
1997 /* Non-terminal <identifier>.
1999 <identifier> ::= </unqualified source code identifier> */
2001 static void
2002 write_identifier (const char *identifier)
2004 MANGLE_TRACE ("identifier", identifier);
2005 write_string (identifier);
2008 /* Handle constructor productions of non-terminal <special-name>.
2009 CTOR is a constructor FUNCTION_DECL.
2011 <special-name> ::= C1 # complete object constructor
2012 ::= C2 # base object constructor
2013 ::= C3 # complete object allocating constructor
2015 Currently, allocating constructors are never used. */
2017 static void
2018 write_special_name_constructor (const tree ctor)
2020 write_char ('C');
2021 bool new_inh = (flag_new_inheriting_ctors
2022 && DECL_INHERITED_CTOR (ctor));
2023 if (new_inh)
2024 write_char ('I');
2025 if (DECL_BASE_CONSTRUCTOR_P (ctor))
2026 write_char ('2');
2027 /* This is the old-style "[unified]" constructor.
2028 In some cases, we may emit this function and call
2029 it from the clones in order to share code and save space. */
2030 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
2031 write_char ('4');
2032 else
2034 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
2035 write_char ('1');
2037 if (new_inh)
2038 write_type (DECL_INHERITED_CTOR_BASE (ctor));
2041 /* Handle destructor productions of non-terminal <special-name>.
2042 DTOR is a destructor FUNCTION_DECL.
2044 <special-name> ::= D0 # deleting (in-charge) destructor
2045 ::= D1 # complete object (in-charge) destructor
2046 ::= D2 # base object (not-in-charge) destructor */
2048 static void
2049 write_special_name_destructor (const tree dtor)
2051 if (DECL_DELETING_DESTRUCTOR_P (dtor))
2052 write_string ("D0");
2053 else if (DECL_BASE_DESTRUCTOR_P (dtor))
2054 write_string ("D2");
2055 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
2056 /* This is the old-style "[unified]" destructor.
2057 In some cases, we may emit this function and call
2058 it from the clones in order to share code and save space. */
2059 write_string ("D4");
2060 else
2062 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
2063 write_string ("D1");
2067 /* Return the discriminator for ENTITY appearing inside
2068 FUNCTION. The discriminator is the lexical ordinal of VAR or TYPE among
2069 entities with the same name and kind in the same FUNCTION. */
2071 static int
2072 discriminator_for_local_entity (tree entity)
2074 if (!DECL_LANG_SPECIFIC (entity))
2076 /* Some decls, like __FUNCTION__, don't need a discriminator. */
2077 gcc_checking_assert (DECL_ARTIFICIAL (entity));
2078 return 0;
2080 else if (tree disc = DECL_DISCRIMINATOR (entity))
2081 return TREE_INT_CST_LOW (disc);
2082 else
2083 /* The first entity with a particular name doesn't get
2084 DECL_DISCRIMINATOR set up. */
2085 return 0;
2088 /* Return the discriminator for STRING, a string literal used inside
2089 FUNCTION. The discriminator is the lexical ordinal of STRING among
2090 string literals used in FUNCTION. */
2092 static int
2093 discriminator_for_string_literal (tree /*function*/,
2094 tree /*string*/)
2096 /* For now, we don't discriminate amongst string literals. */
2097 return 0;
2100 /* <discriminator> := _ <number> # when number < 10
2101 := __ <number> _ # when number >= 10
2103 The discriminator is used only for the second and later occurrences
2104 of the same name within a single function. In this case <number> is
2105 n - 2, if this is the nth occurrence, in lexical order. */
2107 static void
2108 write_discriminator (const int discriminator)
2110 /* If discriminator is zero, don't write anything. Otherwise... */
2111 if (discriminator > 0)
2113 write_char ('_');
2114 if (discriminator - 1 >= 10)
2116 if (abi_warn_or_compat_version_crosses (11))
2117 G.need_abi_warning = 1;
2118 if (abi_version_at_least (11))
2119 write_char ('_');
2121 write_unsigned_number (discriminator - 1);
2122 if (abi_version_at_least (11) && discriminator - 1 >= 10)
2123 write_char ('_');
2127 /* Mangle the name of a function-scope entity. FUNCTION is the
2128 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
2129 default argument scope. ENTITY is the decl for the entity itself.
2130 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
2131 either ENTITY itself or an enclosing scope of ENTITY.
2133 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2134 := Z <function encoding> E s [<discriminator>]
2135 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
2137 static void
2138 write_local_name (tree function, const tree local_entity,
2139 const tree entity)
2141 tree parm = NULL_TREE;
2143 MANGLE_TRACE_TREE ("local-name", entity);
2145 if (TREE_CODE (function) == PARM_DECL)
2147 parm = function;
2148 function = DECL_CONTEXT (parm);
2151 write_char ('Z');
2152 write_encoding (function);
2153 write_char ('E');
2155 /* For this purpose, parameters are numbered from right-to-left. */
2156 if (parm)
2158 int i = list_length (parm);
2159 write_char ('d');
2160 write_compact_number (i - 1);
2163 if (TREE_CODE (entity) == STRING_CST)
2165 write_char ('s');
2166 write_discriminator (discriminator_for_string_literal (function,
2167 entity));
2169 else
2171 /* Now the <entity name>. Let write_name know its being called
2172 from <local-name>, so it doesn't try to process the enclosing
2173 function scope again. */
2174 write_name (entity, /*ignore_local_scope=*/1);
2175 if (DECL_DISCRIMINATOR_P (local_entity)
2176 && !(TREE_CODE (local_entity) == TYPE_DECL
2177 && TYPE_ANON_P (TREE_TYPE (local_entity))))
2178 write_discriminator (discriminator_for_local_entity (local_entity));
2182 /* Non-terminals <type> and <CV-qualifier>.
2184 <type> ::= <builtin-type>
2185 ::= <function-type>
2186 ::= <class-enum-type>
2187 ::= <array-type>
2188 ::= <pointer-to-member-type>
2189 ::= <template-param>
2190 ::= <substitution>
2191 ::= <CV-qualifier>
2192 ::= P <type> # pointer-to
2193 ::= R <type> # reference-to
2194 ::= C <type> # complex pair (C 2000)
2195 ::= G <type> # imaginary (C 2000) [not supported]
2196 ::= U <source-name> <type> # vendor extended type qualifier
2198 C++0x extensions
2200 <type> ::= RR <type> # rvalue reference-to
2201 <type> ::= Dt <expression> # decltype of an id-expression or
2202 # class member access
2203 <type> ::= DT <expression> # decltype of an expression
2204 <type> ::= Dn # decltype of nullptr
2206 TYPE is a type node. */
2208 static void
2209 write_type (tree type)
2211 /* This gets set to nonzero if TYPE turns out to be a (possibly
2212 CV-qualified) builtin type. */
2213 int is_builtin_type = 0;
2215 MANGLE_TRACE_TREE ("type", type);
2217 if (type == error_mark_node)
2218 return;
2220 type = canonicalize_for_substitution (type);
2221 if (find_substitution (type))
2222 return;
2225 if (write_CV_qualifiers_for_type (type) > 0)
2226 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
2227 mangle the unqualified type. The recursive call is needed here
2228 since both the qualified and unqualified types are substitution
2229 candidates. */
2231 tree t = TYPE_MAIN_VARIANT (type);
2232 if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t))
2234 tree attrs = NULL_TREE;
2235 if (tx_safe_fn_type_p (type))
2236 attrs = tree_cons (get_identifier ("transaction_safe"),
2237 NULL_TREE, attrs);
2238 t = cp_build_type_attribute_variant (t, attrs);
2240 gcc_assert (t != type);
2241 if (FUNC_OR_METHOD_TYPE_P (t))
2243 t = build_ref_qualified_type (t, type_memfn_rqual (type));
2244 if (flag_noexcept_type)
2246 tree r = TYPE_RAISES_EXCEPTIONS (type);
2247 t = build_exception_variant (t, r);
2249 if (abi_version_at_least (8)
2250 || type == TYPE_MAIN_VARIANT (type))
2251 /* Avoid adding the unqualified function type as a substitution. */
2252 write_function_type (t);
2253 else
2254 write_type (t);
2255 if (abi_warn_or_compat_version_crosses (8))
2256 G.need_abi_warning = 1;
2258 else
2259 write_type (t);
2261 else if (TREE_CODE (type) == ARRAY_TYPE)
2262 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
2263 so that the cv-qualification of the element type is available
2264 in write_array_type. */
2265 write_array_type (type);
2266 else
2268 tree type_orig = type;
2270 /* See through any typedefs. */
2271 type = TYPE_MAIN_VARIANT (type);
2272 if (FUNC_OR_METHOD_TYPE_P (type))
2273 type = cxx_copy_lang_qualifiers (type, type_orig);
2275 /* According to the C++ ABI, some library classes are passed the
2276 same as the scalar type of their single member and use the same
2277 mangling. */
2278 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2279 type = TREE_TYPE (first_field (type));
2281 if (TYPE_PTRDATAMEM_P (type))
2282 write_pointer_to_member_type (type);
2283 else
2285 /* Handle any target-specific fundamental types. */
2286 const char *target_mangling
2287 = targetm.mangle_type (type_orig);
2289 if (target_mangling)
2291 write_string (target_mangling);
2292 /* Add substitutions for types other than fundamental
2293 types. */
2294 if (!VOID_TYPE_P (type)
2295 && TREE_CODE (type) != INTEGER_TYPE
2296 && TREE_CODE (type) != REAL_TYPE
2297 && TREE_CODE (type) != BOOLEAN_TYPE)
2298 add_substitution (type);
2299 return;
2302 switch (TREE_CODE (type))
2304 case VOID_TYPE:
2305 case BOOLEAN_TYPE:
2306 case INTEGER_TYPE: /* Includes wchar_t. */
2307 case REAL_TYPE:
2308 case FIXED_POINT_TYPE:
2310 /* If this is a typedef, TYPE may not be one of
2311 the standard builtin type nodes, but an alias of one. Use
2312 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
2313 write_builtin_type (TYPE_MAIN_VARIANT (type));
2314 ++is_builtin_type;
2316 break;
2318 case COMPLEX_TYPE:
2319 write_char ('C');
2320 write_type (TREE_TYPE (type));
2321 break;
2323 case FUNCTION_TYPE:
2324 case METHOD_TYPE:
2325 write_function_type (type);
2326 break;
2328 case UNION_TYPE:
2329 case RECORD_TYPE:
2330 case ENUMERAL_TYPE:
2331 /* A pointer-to-member function is represented as a special
2332 RECORD_TYPE, so check for this first. */
2333 if (TYPE_PTRMEMFUNC_P (type))
2334 write_pointer_to_member_type (type);
2335 else
2336 write_class_enum_type (type);
2337 break;
2339 case TYPENAME_TYPE:
2340 case UNBOUND_CLASS_TEMPLATE:
2341 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2342 ordinary nested names. */
2343 write_nested_name (TYPE_STUB_DECL (type));
2344 break;
2346 case POINTER_TYPE:
2347 case REFERENCE_TYPE:
2348 if (TYPE_PTR_P (type))
2349 write_char ('P');
2350 else if (TYPE_REF_IS_RVALUE (type))
2351 write_char ('O');
2352 else
2353 write_char ('R');
2355 tree target = TREE_TYPE (type);
2356 /* Attribute const/noreturn are not reflected in mangling.
2357 We strip them here rather than at a lower level because
2358 a typedef or template argument can have function type
2359 with function-cv-quals (that use the same representation),
2360 but you can't have a pointer/reference to such a type. */
2361 if (TREE_CODE (target) == FUNCTION_TYPE)
2363 if (abi_warn_or_compat_version_crosses (5)
2364 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2365 G.need_abi_warning = 1;
2366 if (abi_version_at_least (5))
2367 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2369 write_type (target);
2371 break;
2373 case TEMPLATE_TYPE_PARM:
2374 if (is_auto (type))
2376 if (AUTO_IS_DECLTYPE (type))
2377 write_identifier ("Dc");
2378 else
2379 write_identifier ("Da");
2380 ++is_builtin_type;
2381 break;
2383 /* fall through. */
2384 case TEMPLATE_PARM_INDEX:
2385 write_template_param (type);
2386 break;
2388 case TEMPLATE_TEMPLATE_PARM:
2389 write_template_template_param (type);
2390 break;
2392 case BOUND_TEMPLATE_TEMPLATE_PARM:
2393 write_template_template_param (type);
2394 write_template_args
2395 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2396 break;
2398 case VECTOR_TYPE:
2399 if (abi_version_at_least (4))
2401 write_string ("Dv");
2402 /* Non-constant vector size would be encoded with
2403 _ expression, but we don't support that yet. */
2404 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type)
2405 .to_constant ());
2406 write_char ('_');
2408 else
2409 write_string ("U8__vector");
2410 if (abi_warn_or_compat_version_crosses (4))
2411 G.need_abi_warning = 1;
2412 write_type (TREE_TYPE (type));
2413 break;
2415 case TYPE_PACK_EXPANSION:
2416 write_string ("Dp");
2417 write_type (PACK_EXPANSION_PATTERN (type));
2418 break;
2420 case DECLTYPE_TYPE:
2421 /* These shouldn't make it into mangling. */
2422 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2423 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2425 /* In ABI <5, we stripped decltype of a plain decl. */
2426 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2428 tree expr = DECLTYPE_TYPE_EXPR (type);
2429 tree etype = NULL_TREE;
2430 switch (TREE_CODE (expr))
2432 case VAR_DECL:
2433 case PARM_DECL:
2434 case RESULT_DECL:
2435 case FUNCTION_DECL:
2436 case CONST_DECL:
2437 case TEMPLATE_PARM_INDEX:
2438 etype = TREE_TYPE (expr);
2439 break;
2441 default:
2442 break;
2445 if (etype && !type_uses_auto (etype))
2447 if (abi_warn_or_compat_version_crosses (5))
2448 G.need_abi_warning = 1;
2449 if (!abi_version_at_least (5))
2451 write_type (etype);
2452 return;
2457 write_char ('D');
2458 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2459 write_char ('t');
2460 else
2461 write_char ('T');
2462 ++cp_unevaluated_operand;
2463 write_expression (DECLTYPE_TYPE_EXPR (type));
2464 --cp_unevaluated_operand;
2465 write_char ('E');
2466 break;
2468 case NULLPTR_TYPE:
2469 write_string ("Dn");
2470 if (abi_version_at_least (7))
2471 ++is_builtin_type;
2472 if (abi_warn_or_compat_version_crosses (7))
2473 G.need_abi_warning = 1;
2474 break;
2476 case TYPEOF_TYPE:
2477 sorry ("mangling %<typeof%>, use %<decltype%> instead");
2478 break;
2480 case TRAIT_TYPE:
2481 error ("use of built-in trait %qT in function signature; "
2482 "use library traits instead", type);
2483 break;
2485 case LANG_TYPE:
2486 /* fall through. */
2488 default:
2489 gcc_unreachable ();
2494 /* Types other than builtin types are substitution candidates. */
2495 if (!is_builtin_type)
2496 add_substitution (type);
2499 /* qsort callback for sorting a vector of attribute entries. */
2501 static int
2502 attr_strcmp (const void *p1, const void *p2)
2504 tree a1 = *(const tree*)p1;
2505 tree a2 = *(const tree*)p2;
2507 const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1));
2508 const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2));
2510 return strcmp (as1->name, as2->name);
2513 /* Return true if we should mangle a type attribute with name NAME. */
2515 static bool
2516 mangle_type_attribute_p (tree name)
2518 const attribute_spec *as = lookup_attribute_spec (name);
2519 if (!as || !as->affects_type_identity)
2520 return false;
2522 /* Skip internal-only attributes, which are distinguished from others
2523 by having a space. At present, all internal-only attributes that
2524 affect type identity are target-specific and are handled by
2525 targetm.mangle_type instead.
2527 Another reason to do this is that a space isn't a valid identifier
2528 character for most file formats. */
2529 if (strchr (IDENTIFIER_POINTER (name), ' '))
2530 return false;
2532 /* The following attributes are mangled specially. */
2533 if (is_attribute_p ("transaction_safe", name))
2534 return false;
2535 if (is_attribute_p ("abi_tag", name))
2536 return false;
2538 return true;
2541 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2542 CV-qualifiers written for TYPE.
2544 <CV-qualifiers> ::= [r] [V] [K] */
2546 static int
2547 write_CV_qualifiers_for_type (const tree type)
2549 int num_qualifiers = 0;
2551 /* The order is specified by:
2553 "In cases where multiple order-insensitive qualifiers are
2554 present, they should be ordered 'K' (closest to the base type),
2555 'V', 'r', and 'U' (farthest from the base type) ..." */
2557 /* Mangle attributes that affect type identity as extended qualifiers.
2559 We don't do this with classes and enums because their attributes
2560 are part of their definitions, not something added on. */
2562 if (!OVERLOAD_TYPE_P (type))
2564 auto_vec<tree> vec;
2565 for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
2566 if (mangle_type_attribute_p (get_attribute_name (a)))
2567 vec.safe_push (a);
2568 if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
2569 G.need_abi_warning = true;
2570 if (abi_version_at_least (10))
2572 vec.qsort (attr_strcmp);
2573 while (!vec.is_empty())
2575 tree a = vec.pop();
2576 const attribute_spec *as
2577 = lookup_attribute_spec (get_attribute_name (a));
2579 write_char ('U');
2580 write_unsigned_number (strlen (as->name));
2581 write_string (as->name);
2582 if (TREE_VALUE (a))
2584 write_char ('I');
2585 for (tree args = TREE_VALUE (a); args;
2586 args = TREE_CHAIN (args))
2588 tree arg = TREE_VALUE (args);
2589 write_template_arg (arg);
2591 write_char ('E');
2594 ++num_qualifiers;
2599 /* Note that we do not use cp_type_quals below; given "const
2600 int[3]", the "const" is emitted with the "int", not with the
2601 array. */
2602 cp_cv_quals quals = TYPE_QUALS (type);
2604 if (quals & TYPE_QUAL_RESTRICT)
2606 write_char ('r');
2607 ++num_qualifiers;
2609 if (quals & TYPE_QUAL_VOLATILE)
2611 write_char ('V');
2612 ++num_qualifiers;
2614 if (quals & TYPE_QUAL_CONST)
2616 write_char ('K');
2617 ++num_qualifiers;
2620 return num_qualifiers;
2623 /* Non-terminal <builtin-type>.
2625 <builtin-type> ::= v # void
2626 ::= b # bool
2627 ::= w # wchar_t
2628 ::= c # char
2629 ::= a # signed char
2630 ::= h # unsigned char
2631 ::= s # short
2632 ::= t # unsigned short
2633 ::= i # int
2634 ::= j # unsigned int
2635 ::= l # long
2636 ::= m # unsigned long
2637 ::= x # long long, __int64
2638 ::= y # unsigned long long, __int64
2639 ::= n # __int128
2640 ::= o # unsigned __int128
2641 ::= f # float
2642 ::= d # double
2643 ::= e # long double, __float80
2644 ::= g # __float128 [not supported]
2645 ::= u <source-name> # vendor extended type */
2647 static void
2648 write_builtin_type (tree type)
2650 if (TYPE_CANONICAL (type))
2651 type = TYPE_CANONICAL (type);
2653 switch (TREE_CODE (type))
2655 case VOID_TYPE:
2656 write_char ('v');
2657 break;
2659 case BOOLEAN_TYPE:
2660 write_char ('b');
2661 break;
2663 case INTEGER_TYPE:
2664 /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
2665 isn't in integer_type_nodes. */
2666 if (type == wchar_type_node)
2667 write_char ('w');
2668 else if (type == char8_type_node)
2669 write_string ("Du");
2670 else if (type == char16_type_node)
2671 write_string ("Ds");
2672 else if (type == char32_type_node)
2673 write_string ("Di");
2674 else
2676 size_t itk;
2677 /* Assume TYPE is one of the shared integer type nodes. Find
2678 it in the array of these nodes. */
2679 iagain:
2680 for (itk = 0; itk < itk_none; ++itk)
2681 if (integer_types[itk] != NULL_TREE
2682 && integer_type_codes[itk] != '\0'
2683 && type == integer_types[itk])
2685 /* Print the corresponding single-letter code. */
2686 write_char (integer_type_codes[itk]);
2687 break;
2690 if (itk == itk_none)
2692 tree t = c_common_type_for_mode (TYPE_MODE (type),
2693 TYPE_UNSIGNED (type));
2694 if (type != t)
2696 type = t;
2697 goto iagain;
2700 if (TYPE_PRECISION (type) == 128)
2701 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2702 else
2704 /* Allow for cases where TYPE is not one of the shared
2705 integer type nodes and write a "vendor extended builtin
2706 type" with a name the form intN or uintN, respectively.
2707 Situations like this can happen if you have an
2708 __attribute__((__mode__(__SI__))) type and use exotic
2709 switches like '-mint8' on AVR. Of course, this is
2710 undefined by the C++ ABI (and '-mint8' is not even
2711 Standard C conforming), but when using such special
2712 options you're pretty much in nowhere land anyway. */
2713 const char *prefix;
2714 char prec[11]; /* up to ten digits for an unsigned */
2716 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2717 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2718 write_char ('u'); /* "vendor extended builtin type" */
2719 write_unsigned_number (strlen (prefix) + strlen (prec));
2720 write_string (prefix);
2721 write_string (prec);
2725 break;
2727 case REAL_TYPE:
2728 if (type == float_type_node)
2729 write_char ('f');
2730 else if (type == double_type_node)
2731 write_char ('d');
2732 else if (type == long_double_type_node)
2733 write_char ('e');
2734 else if (type == dfloat32_type_node || type == fallback_dfloat32_type)
2735 write_string ("Df");
2736 else if (type == dfloat64_type_node || type == fallback_dfloat64_type)
2737 write_string ("Dd");
2738 else if (type == dfloat128_type_node || type == fallback_dfloat128_type)
2739 write_string ("De");
2740 else if (type == float16_type_node)
2741 write_string ("DF16_");
2742 else if (type == float32_type_node)
2743 write_string ("DF32_");
2744 else if (type == float64_type_node)
2745 write_string ("DF64_");
2746 else if (type == float128_type_node)
2747 write_string ("DF128_");
2748 else if (type == float32x_type_node)
2749 write_string ("DF32x");
2750 else if (type == float64x_type_node)
2751 write_string ("DF64x");
2752 else if (type == float128x_type_node)
2753 write_string ("DF128x");
2754 else
2755 gcc_unreachable ();
2756 break;
2758 default:
2759 gcc_unreachable ();
2763 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2764 METHOD_TYPE. The return type is mangled before the parameter
2765 types.
2767 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2769 static void
2770 write_function_type (const tree type)
2772 MANGLE_TRACE_TREE ("function-type", type);
2774 /* For a pointer to member function, the function type may have
2775 cv-qualifiers, indicating the quals for the artificial 'this'
2776 parameter. */
2777 if (TREE_CODE (type) == METHOD_TYPE)
2779 /* The first parameter must be a POINTER_TYPE pointing to the
2780 `this' parameter. */
2781 tree this_type = class_of_this_parm (type);
2782 write_CV_qualifiers_for_type (this_type);
2785 write_exception_spec (TYPE_RAISES_EXCEPTIONS (type));
2787 if (tx_safe_fn_type_p (type))
2788 write_string ("Dx");
2790 write_char ('F');
2791 /* We don't track whether or not a type is `extern "C"'. Note that
2792 you can have an `extern "C"' function that does not have
2793 `extern "C"' type, and vice versa:
2795 extern "C" typedef void function_t();
2796 function_t f; // f has C++ linkage, but its type is
2797 // `extern "C"'
2799 typedef void function_t();
2800 extern "C" function_t f; // Vice versa.
2802 See [dcl.link]. */
2803 write_bare_function_type (type, /*include_return_type_p=*/1,
2804 /*decl=*/NULL);
2805 if (FUNCTION_REF_QUALIFIED (type))
2807 if (FUNCTION_RVALUE_QUALIFIED (type))
2808 write_char ('O');
2809 else
2810 write_char ('R');
2812 write_char ('E');
2815 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2816 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2817 is mangled before the parameter types. If non-NULL, DECL is
2818 FUNCTION_DECL for the function whose type is being emitted. */
2820 static void
2821 write_bare_function_type (const tree type, const int include_return_type_p,
2822 const tree decl)
2824 MANGLE_TRACE_TREE ("bare-function-type", type);
2826 /* Mangle the return type, if requested. */
2827 if (include_return_type_p)
2828 write_type (TREE_TYPE (type));
2830 /* Now mangle the types of the arguments. */
2831 ++G.parm_depth;
2832 write_method_parms (TYPE_ARG_TYPES (type),
2833 TREE_CODE (type) == METHOD_TYPE,
2834 decl);
2835 --G.parm_depth;
2838 /* Write the mangled representation of a method parameter list of
2839 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2840 considered a non-static method, and the this parameter is omitted.
2841 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2842 parameters are being emitted. */
2844 static void
2845 write_method_parms (tree parm_types, const int method_p, const tree decl)
2847 tree first_parm_type;
2848 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2850 /* Assume this parameter type list is variable-length. If it ends
2851 with a void type, then it's not. */
2852 int varargs_p = 1;
2854 /* If this is a member function, skip the first arg, which is the
2855 this pointer.
2856 "Member functions do not encode the type of their implicit this
2857 parameter."
2859 Similarly, there's no need to mangle artificial parameters, like
2860 the VTT parameters for constructors and destructors. */
2861 if (method_p)
2863 parm_types = TREE_CHAIN (parm_types);
2864 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2866 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2868 parm_types = TREE_CHAIN (parm_types);
2869 parm_decl = DECL_CHAIN (parm_decl);
2872 if (decl && ctor_omit_inherited_parms (decl))
2873 /* Bring back parameters omitted from an inherited ctor. */
2874 parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl));
2877 for (first_parm_type = parm_types;
2878 parm_types;
2879 parm_types = TREE_CHAIN (parm_types))
2881 tree parm = TREE_VALUE (parm_types);
2882 if (parm == void_type_node)
2884 /* "Empty parameter lists, whether declared as () or
2885 conventionally as (void), are encoded with a void parameter
2886 (v)." */
2887 if (parm_types == first_parm_type)
2888 write_type (parm);
2889 /* If the parm list is terminated with a void type, it's
2890 fixed-length. */
2891 varargs_p = 0;
2892 /* A void type better be the last one. */
2893 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2895 else
2896 write_type (parm);
2899 if (varargs_p)
2900 /* <builtin-type> ::= z # ellipsis */
2901 write_char ('z');
2904 /* <class-enum-type> ::= <name> */
2906 static void
2907 write_class_enum_type (const tree type)
2909 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2912 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2913 arguments.
2915 <template-args> ::= I <template-arg>* E */
2917 static void
2918 write_template_args (tree args)
2920 int i;
2921 int length = 0;
2923 MANGLE_TRACE_TREE ("template-args", args);
2925 write_char ('I');
2927 if (args)
2928 length = TREE_VEC_LENGTH (args);
2930 if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2932 /* We have nested template args. We want the innermost template
2933 argument list. */
2934 args = TREE_VEC_ELT (args, length - 1);
2935 length = TREE_VEC_LENGTH (args);
2937 for (i = 0; i < length; ++i)
2938 write_template_arg (TREE_VEC_ELT (args, i));
2940 write_char ('E');
2943 /* Write out the
2944 <unqualified-name>
2945 <unqualified-name> <template-args>
2946 part of SCOPE_REF or COMPONENT_REF mangling. */
2948 static void
2949 write_member_name (tree member)
2951 if (identifier_p (member))
2953 if (IDENTIFIER_ANY_OP_P (member))
2955 if (abi_version_at_least (11))
2956 write_string ("on");
2957 if (abi_warn_or_compat_version_crosses (11))
2958 G.need_abi_warning = 1;
2960 write_unqualified_id (member);
2962 else if (DECL_P (member))
2964 gcc_assert (!DECL_OVERLOADED_OPERATOR_P (member));
2965 write_unqualified_name (member);
2967 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2969 tree name = TREE_OPERAND (member, 0);
2970 name = OVL_FIRST (name);
2971 write_member_name (name);
2972 write_template_args (TREE_OPERAND (member, 1));
2974 else
2975 write_expression (member);
2978 /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for
2979 converting to BASE, or just the conversion of EXPR if BASE is null.
2981 "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base
2982 path Min(P) is defined as follows: let C_i be the last element for which the
2983 conversion to C_0 is unambiguous; if that element is C_n, the minimized path
2984 is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) ->
2985 C_0."
2987 We mangle the conversion to C_i if it's different from C_n. */
2989 static bool
2990 write_base_ref (tree expr, tree base = NULL_TREE)
2992 if (TREE_CODE (expr) != COMPONENT_REF)
2993 return false;
2995 tree field = TREE_OPERAND (expr, 1);
2997 if (TREE_CODE (field) != FIELD_DECL || !DECL_FIELD_IS_BASE (field))
2998 return false;
3000 tree object = TREE_OPERAND (expr, 0);
3002 tree binfo = NULL_TREE;
3003 if (base)
3005 tree cur = TREE_TYPE (object);
3006 binfo = lookup_base (cur, base, ba_unique, NULL, tf_none);
3008 else
3009 /* We're at the end of the base conversion chain, so it can't be
3010 ambiguous. */
3011 base = TREE_TYPE (field);
3013 if (binfo == error_mark_node)
3015 /* cur->base is ambiguous, so make the conversion to
3016 last explicit, expressed as a cast (last&)object. */
3017 tree last = TREE_TYPE (expr);
3018 write_string (OVL_OP_INFO (false, CAST_EXPR)->mangled_name);
3019 write_type (build_reference_type (last));
3020 write_expression (object);
3022 else if (write_base_ref (object, base))
3023 /* cur->base is unambiguous, but we had another base conversion
3024 underneath and wrote it out. */;
3025 else
3026 /* No more base conversions, just write out the object. */
3027 write_expression (object);
3029 return true;
3032 /* The number of elements spanned by a RANGE_EXPR. */
3034 unsigned HOST_WIDE_INT
3035 range_expr_nelts (tree expr)
3037 tree lo = TREE_OPERAND (expr, 0);
3038 tree hi = TREE_OPERAND (expr, 1);
3039 return tree_to_uhwi (hi) - tree_to_uhwi (lo) + 1;
3042 /* <expression> ::= <unary operator-name> <expression>
3043 ::= <binary operator-name> <expression> <expression>
3044 ::= <expr-primary>
3046 <expr-primary> ::= <template-param>
3047 ::= L <type> <value number> E # literal
3048 ::= L <mangled-name> E # external name
3049 ::= st <type> # sizeof
3050 ::= sr <type> <unqualified-name> # dependent name
3051 ::= sr <type> <unqualified-name> <template-args> */
3053 static void
3054 write_expression (tree expr)
3056 enum tree_code code = TREE_CODE (expr);
3058 if (TREE_CODE (expr) == TARGET_EXPR)
3060 expr = TARGET_EXPR_INITIAL (expr);
3061 code = TREE_CODE (expr);
3064 /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer
3065 argument is converted (via qualification conversions) to another type. */
3066 while (CONVERT_EXPR_CODE_P (code)
3067 || code == IMPLICIT_CONV_EXPR
3068 || location_wrapper_p (expr)
3069 /* Parentheses aren't mangled. */
3070 || code == PAREN_EXPR
3071 || code == NON_LVALUE_EXPR
3072 || (code == VIEW_CONVERT_EXPR
3073 && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
3075 expr = TREE_OPERAND (expr, 0);
3076 code = TREE_CODE (expr);
3079 if (code == BASELINK
3080 && (!type_unknown_p (expr)
3081 || !BASELINK_QUALIFIED_P (expr)))
3083 expr = BASELINK_FUNCTIONS (expr);
3084 code = TREE_CODE (expr);
3087 /* Handle pointers-to-members by making them look like expression
3088 nodes. */
3089 if (code == PTRMEM_CST)
3091 expr = build_nt (ADDR_EXPR,
3092 build_qualified_name (/*type=*/NULL_TREE,
3093 PTRMEM_CST_CLASS (expr),
3094 PTRMEM_CST_MEMBER (expr),
3095 /*template_p=*/false));
3096 code = TREE_CODE (expr);
3099 /* Handle template parameters. */
3100 if (code == TEMPLATE_TYPE_PARM
3101 || code == TEMPLATE_TEMPLATE_PARM
3102 || code == BOUND_TEMPLATE_TEMPLATE_PARM
3103 || code == TEMPLATE_PARM_INDEX)
3104 write_template_param (expr);
3105 /* Handle literals. */
3106 else if (TREE_CODE_CLASS (code) == tcc_constant
3107 || code == CONST_DECL)
3108 write_template_arg_literal (expr);
3109 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
3111 gcc_assert (id_equal (DECL_NAME (expr), "this"));
3112 write_string ("fpT");
3114 else if (code == PARM_DECL)
3116 /* A function parameter used in a late-specified return type. */
3117 int index = DECL_PARM_INDEX (expr);
3118 int level = DECL_PARM_LEVEL (expr);
3119 int delta = G.parm_depth - level + 1;
3120 gcc_assert (index >= 1);
3121 write_char ('f');
3122 if (delta != 0)
3124 if (abi_version_at_least (5))
3126 /* Let L be the number of function prototype scopes from the
3127 innermost one (in which the parameter reference occurs) up
3128 to (and including) the one containing the declaration of
3129 the referenced parameter. If the parameter declaration
3130 clause of the innermost function prototype scope has been
3131 completely seen, it is not counted (in that case -- which
3132 is perhaps the most common -- L can be zero). */
3133 write_char ('L');
3134 write_unsigned_number (delta - 1);
3136 if (abi_warn_or_compat_version_crosses (5))
3137 G.need_abi_warning = true;
3139 write_char ('p');
3140 write_compact_number (index - 1);
3142 else if (DECL_P (expr))
3144 write_char ('L');
3145 write_mangled_name (expr, false);
3146 write_char ('E');
3148 else if (TREE_CODE (expr) == SIZEOF_EXPR)
3150 tree op = TREE_OPERAND (expr, 0);
3152 if (PACK_EXPANSION_P (op))
3154 if (abi_warn_or_compat_version_crosses (11))
3155 G.need_abi_warning = true;
3156 if (abi_version_at_least (11))
3158 /* sZ rather than szDp. */
3159 write_string ("sZ");
3160 write_expression (PACK_EXPANSION_PATTERN (op));
3161 return;
3165 if (SIZEOF_EXPR_TYPE_P (expr))
3167 write_string ("st");
3168 write_type (TREE_TYPE (op));
3170 else if (ARGUMENT_PACK_P (op))
3172 tree args = ARGUMENT_PACK_ARGS (op);
3173 int length = TREE_VEC_LENGTH (args);
3174 if (abi_warn_or_compat_version_crosses (10))
3175 G.need_abi_warning = true;
3176 if (abi_version_at_least (10))
3178 /* sP <template-arg>* E # sizeof...(T), size of a captured
3179 template parameter pack from an alias template */
3180 write_string ("sP");
3181 for (int i = 0; i < length; ++i)
3182 write_template_arg (TREE_VEC_ELT (args, i));
3183 write_char ('E');
3185 else
3187 /* In GCC 5 we represented this sizeof wrong, with the effect
3188 that we mangled it as the last element of the pack. */
3189 tree arg = TREE_VEC_ELT (args, length-1);
3190 if (TYPE_P (op))
3192 write_string ("st");
3193 write_type (arg);
3195 else
3197 write_string ("sz");
3198 write_expression (arg);
3202 else if (TYPE_P (TREE_OPERAND (expr, 0)))
3204 write_string ("st");
3205 write_type (TREE_OPERAND (expr, 0));
3207 else
3208 goto normal_expr;
3210 else if (TREE_CODE (expr) == ALIGNOF_EXPR)
3212 if (!ALIGNOF_EXPR_STD_P (expr))
3214 if (abi_warn_or_compat_version_crosses (16))
3215 G.need_abi_warning = true;
3216 if (abi_version_at_least (16))
3218 /* We used to mangle __alignof__ like alignof. */
3219 write_string ("u11__alignof__");
3220 write_template_arg (TREE_OPERAND (expr, 0));
3221 write_char ('E');
3222 return;
3225 if (TYPE_P (TREE_OPERAND (expr, 0)))
3227 write_string ("at");
3228 write_type (TREE_OPERAND (expr, 0));
3230 else
3231 goto normal_expr;
3233 else if (code == SCOPE_REF
3234 || code == BASELINK)
3236 tree scope, member;
3237 if (code == SCOPE_REF)
3239 scope = TREE_OPERAND (expr, 0);
3240 member = TREE_OPERAND (expr, 1);
3241 if (BASELINK_P (member))
3242 member = BASELINK_FUNCTIONS (member);
3244 else
3246 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
3247 member = BASELINK_FUNCTIONS (expr);
3250 /* If the MEMBER is a real declaration, then the qualifying
3251 scope was not dependent. Ideally, we would not have a
3252 SCOPE_REF in those cases, but sometimes we do. If the second
3253 argument is a DECL, then the name must not have been
3254 dependent. */
3255 if (DECL_P (member))
3256 write_expression (member);
3257 else
3259 gcc_assert (code != BASELINK || BASELINK_QUALIFIED_P (expr));
3260 write_string ("sr");
3261 write_type (scope);
3262 write_member_name (member);
3265 else if (INDIRECT_REF_P (expr)
3266 && TREE_TYPE (TREE_OPERAND (expr, 0))
3267 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
3269 write_expression (TREE_OPERAND (expr, 0));
3271 else if (identifier_p (expr))
3273 /* An operator name appearing as a dependent name needs to be
3274 specially marked to disambiguate between a use of the operator
3275 name and a use of the operator in an expression. */
3276 if (IDENTIFIER_ANY_OP_P (expr))
3277 write_string ("on");
3278 write_unqualified_id (expr);
3280 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
3282 tree fn = TREE_OPERAND (expr, 0);
3283 fn = OVL_NAME (fn);
3284 if (IDENTIFIER_ANY_OP_P (fn))
3285 write_string ("on");
3286 write_unqualified_id (fn);
3287 write_template_args (TREE_OPERAND (expr, 1));
3289 else if (TREE_CODE (expr) == MODOP_EXPR)
3291 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
3292 const char *name = OVL_OP_INFO (true, subop)->mangled_name;
3294 write_string (name);
3295 write_expression (TREE_OPERAND (expr, 0));
3296 write_expression (TREE_OPERAND (expr, 2));
3298 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
3300 /* ::= [gs] nw <expression>* _ <type> E
3301 ::= [gs] nw <expression>* _ <type> <initializer>
3302 ::= [gs] na <expression>* _ <type> E
3303 ::= [gs] na <expression>* _ <type> <initializer>
3304 <initializer> ::= pi <expression>* E */
3305 tree placement = TREE_OPERAND (expr, 0);
3306 tree type = TREE_OPERAND (expr, 1);
3307 tree nelts = TREE_OPERAND (expr, 2);
3308 tree init = TREE_OPERAND (expr, 3);
3309 tree t;
3311 gcc_assert (code == NEW_EXPR);
3312 if (TREE_OPERAND (expr, 2))
3313 code = VEC_NEW_EXPR;
3315 if (NEW_EXPR_USE_GLOBAL (expr))
3316 write_string ("gs");
3318 write_string (OVL_OP_INFO (false, code)->mangled_name);
3320 for (t = placement; t; t = TREE_CHAIN (t))
3321 write_expression (TREE_VALUE (t));
3323 write_char ('_');
3325 if (nelts)
3327 tree domain;
3328 ++processing_template_decl;
3329 domain = compute_array_index_type (NULL_TREE, nelts,
3330 tf_warning_or_error);
3331 type = build_cplus_array_type (type, domain);
3332 --processing_template_decl;
3334 write_type (type);
3336 if (init && TREE_CODE (init) == TREE_LIST
3337 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
3338 write_expression (TREE_VALUE (init));
3339 else
3341 if (init)
3342 write_string ("pi");
3343 if (init && init != void_node)
3344 for (t = init; t; t = TREE_CHAIN (t))
3345 write_expression (TREE_VALUE (t));
3346 write_char ('E');
3349 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
3351 gcc_assert (code == DELETE_EXPR);
3352 if (DELETE_EXPR_USE_VEC (expr))
3353 code = VEC_DELETE_EXPR;
3355 if (DELETE_EXPR_USE_GLOBAL (expr))
3356 write_string ("gs");
3358 write_string (OVL_OP_INFO (false, code)->mangled_name);
3360 write_expression (TREE_OPERAND (expr, 0));
3362 else if (code == THROW_EXPR)
3364 tree op = TREE_OPERAND (expr, 0);
3365 if (op)
3367 write_string ("tw");
3368 write_expression (op);
3370 else
3371 write_string ("tr");
3373 else if (code == CONSTRUCTOR)
3375 bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr);
3376 tree etype = TREE_TYPE (expr);
3378 if (braced_init)
3379 write_string ("il");
3380 else
3382 write_string ("tl");
3383 write_type (etype);
3386 /* If this is an undigested initializer, mangle it as written.
3387 COMPOUND_LITERAL_P doesn't actually distinguish between digested and
3388 undigested braced casts, but it should work to use it to distinguish
3389 between braced casts in a template signature (undigested) and template
3390 parm object values (digested), and all CONSTRUCTORS that get here
3391 should be one of those two cases. */
3392 bool undigested = braced_init || COMPOUND_LITERAL_P (expr);
3393 if (undigested || !zero_init_expr_p (expr))
3395 /* Convert braced initializer lists to STRING_CSTs so that
3396 A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
3397 still using the latter mangling for strings that
3398 originated as braced initializer lists. */
3399 expr = braced_lists_to_strings (etype, expr);
3401 if (TREE_CODE (expr) == CONSTRUCTOR)
3403 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
3404 unsigned last_nonzero = UINT_MAX;
3405 constructor_elt *ce;
3407 if (!undigested)
3408 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3409 if ((TREE_CODE (etype) == UNION_TYPE
3410 && ce->index != first_field (etype))
3411 || !zero_init_expr_p (ce->value))
3412 last_nonzero = i;
3414 if (undigested || last_nonzero != UINT_MAX)
3415 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3417 if (i > last_nonzero)
3418 break;
3419 if (!undigested && TREE_CODE (etype) == UNION_TYPE)
3421 /* Express the active member as a designator. */
3422 write_string ("di");
3423 write_unqualified_name (ce->index);
3425 unsigned reps = 1;
3426 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3427 reps = range_expr_nelts (ce->index);
3428 for (unsigned j = 0; j < reps; ++j)
3429 write_expression (ce->value);
3432 else
3434 gcc_assert (TREE_CODE (expr) == STRING_CST);
3435 write_expression (expr);
3438 write_char ('E');
3440 else if (code == LAMBDA_EXPR)
3442 /* [temp.over.link] Two lambda-expressions are never considered
3443 equivalent.
3445 So just use the closure type mangling. */
3446 write_string ("tl");
3447 write_type (LAMBDA_EXPR_CLOSURE (expr));
3448 write_char ('E');
3450 else if (dependent_name (expr))
3452 tree name = dependent_name (expr);
3453 if (IDENTIFIER_ANY_OP_P (name))
3455 if (abi_version_at_least (16))
3456 write_string ("on");
3457 if (abi_warn_or_compat_version_crosses (16))
3458 G.need_abi_warning = 1;
3460 write_unqualified_id (name);
3462 else
3464 normal_expr:
3465 int i, len;
3466 const char *name;
3468 /* When we bind a variable or function to a non-type template
3469 argument with reference type, we create an ADDR_EXPR to show
3470 the fact that the entity's address has been taken. But, we
3471 don't actually want to output a mangling code for the `&'. */
3472 if (TREE_CODE (expr) == ADDR_EXPR
3473 && TREE_TYPE (expr)
3474 && TYPE_REF_P (TREE_TYPE (expr)))
3476 expr = TREE_OPERAND (expr, 0);
3477 if (DECL_P (expr))
3479 write_expression (expr);
3480 return;
3483 code = TREE_CODE (expr);
3486 if (code == COMPONENT_REF)
3488 tree ob = TREE_OPERAND (expr, 0);
3490 if (TREE_CODE (ob) == ARROW_EXPR)
3492 write_string (OVL_OP_INFO (false, code)->mangled_name);
3493 ob = TREE_OPERAND (ob, 0);
3494 write_expression (ob);
3496 else if (write_base_ref (expr))
3497 return;
3498 else if (!is_dummy_object (ob))
3500 write_string ("dt");
3501 write_expression (ob);
3503 /* else, for a non-static data member with no associated object (in
3504 unevaluated context), use the unresolved-name mangling. */
3506 write_member_name (TREE_OPERAND (expr, 1));
3507 return;
3510 /* If it wasn't any of those, recursively expand the expression. */
3511 name = OVL_OP_INFO (false, code)->mangled_name;
3513 /* We used to mangle const_cast and static_cast like a C cast. */
3514 if (code == CONST_CAST_EXPR
3515 || code == STATIC_CAST_EXPR)
3517 if (abi_warn_or_compat_version_crosses (6))
3518 G.need_abi_warning = 1;
3519 if (!abi_version_at_least (6))
3520 name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name;
3523 if (name == NULL)
3525 switch (code)
3527 case TRAIT_EXPR:
3528 error ("use of built-in trait %qE in function signature; "
3529 "use library traits instead", expr);
3530 break;
3532 default:
3533 sorry ("mangling %C", code);
3534 break;
3536 return;
3538 else
3539 write_string (name);
3541 switch (code)
3543 case CALL_EXPR:
3545 tree fn = CALL_EXPR_FN (expr);
3547 if (TREE_CODE (fn) == ADDR_EXPR)
3548 fn = TREE_OPERAND (fn, 0);
3550 /* Mangle a dependent name as the name, not whatever happens to
3551 be the first function in the overload set. */
3552 if (OVL_P (fn)
3553 && type_dependent_expression_p_push (expr))
3554 fn = OVL_NAME (fn);
3556 write_expression (fn);
3559 for (i = 0; i < call_expr_nargs (expr); ++i)
3560 write_expression (CALL_EXPR_ARG (expr, i));
3561 write_char ('E');
3562 break;
3564 case CAST_EXPR:
3565 write_type (TREE_TYPE (expr));
3566 if (list_length (TREE_OPERAND (expr, 0)) == 1)
3567 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
3568 else
3570 tree args = TREE_OPERAND (expr, 0);
3571 write_char ('_');
3572 for (; args; args = TREE_CHAIN (args))
3573 write_expression (TREE_VALUE (args));
3574 write_char ('E');
3576 break;
3578 case DYNAMIC_CAST_EXPR:
3579 case REINTERPRET_CAST_EXPR:
3580 case STATIC_CAST_EXPR:
3581 case CONST_CAST_EXPR:
3582 write_type (TREE_TYPE (expr));
3583 write_expression (TREE_OPERAND (expr, 0));
3584 break;
3586 case PREINCREMENT_EXPR:
3587 case PREDECREMENT_EXPR:
3588 if (abi_version_at_least (6))
3589 write_char ('_');
3590 if (abi_warn_or_compat_version_crosses (6))
3591 G.need_abi_warning = 1;
3592 /* Fall through. */
3594 default:
3595 /* In the middle-end, some expressions have more operands than
3596 they do in templates (and mangling). */
3597 len = cp_tree_operand_length (expr);
3599 for (i = 0; i < len; ++i)
3601 tree operand = TREE_OPERAND (expr, i);
3602 /* As a GNU extension, the middle operand of a
3603 conditional may be omitted. Since expression
3604 manglings are supposed to represent the input token
3605 stream, there's no good way to mangle such an
3606 expression without extending the C++ ABI. */
3607 if (code == COND_EXPR && i == 1 && !operand)
3609 error ("omitted middle operand to %<?:%> operand "
3610 "cannot be mangled");
3611 continue;
3613 else if (FOLD_EXPR_P (expr))
3615 /* The first 'operand' of a fold-expression is the operator
3616 that it folds over. */
3617 if (i == 0)
3619 int fcode = TREE_INT_CST_LOW (operand);
3620 write_string (OVL_OP_INFO (false, fcode)->mangled_name);
3621 continue;
3623 else if (code == BINARY_LEFT_FOLD_EXPR)
3625 /* The order of operands of the binary left and right
3626 folds is the same, but we want to mangle them in
3627 lexical order, i.e. non-pack first. */
3628 if (i == 1)
3629 operand = FOLD_EXPR_INIT (expr);
3630 else
3631 operand = FOLD_EXPR_PACK (expr);
3633 if (PACK_EXPANSION_P (operand))
3634 operand = PACK_EXPANSION_PATTERN (operand);
3636 write_expression (operand);
3642 /* Literal subcase of non-terminal <template-arg>.
3644 "Literal arguments, e.g. "A<42L>", are encoded with their type
3645 and value. Negative integer values are preceded with "n"; for
3646 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3647 encoded as 0, true as 1." */
3649 static void
3650 write_template_arg_literal (const tree value)
3652 if (TREE_CODE (value) == STRING_CST)
3653 /* Temporarily mangle strings as braced initializer lists. */
3654 write_string ("tl");
3655 else
3656 write_char ('L');
3658 tree valtype = TREE_TYPE (value);
3659 write_type (valtype);
3661 /* Write a null member pointer value as (type)0, regardless of its
3662 real representation. */
3663 if (null_member_pointer_value_p (value))
3664 write_integer_cst (integer_zero_node);
3665 else
3666 switch (TREE_CODE (value))
3668 case CONST_DECL:
3669 write_integer_cst (DECL_INITIAL (value));
3670 break;
3672 case INTEGER_CST:
3673 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3674 || integer_zerop (value) || integer_onep (value));
3675 if (!(abi_version_at_least (14)
3676 && NULLPTR_TYPE_P (TREE_TYPE (value))))
3677 write_integer_cst (value);
3678 break;
3680 case REAL_CST:
3681 write_real_cst (value);
3682 break;
3684 case COMPLEX_CST:
3685 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3686 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3688 write_integer_cst (TREE_REALPART (value));
3689 write_char ('_');
3690 write_integer_cst (TREE_IMAGPART (value));
3692 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3693 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3695 write_real_cst (TREE_REALPART (value));
3696 write_char ('_');
3697 write_real_cst (TREE_IMAGPART (value));
3699 else
3700 gcc_unreachable ();
3701 break;
3703 case STRING_CST:
3705 /* Mangle strings the same as braced initializer lists. */
3706 unsigned n = TREE_STRING_LENGTH (value);
3707 const char *str = TREE_STRING_POINTER (value);
3709 /* Count the number of trailing nuls and subtract them from
3710 STRSIZE because they don't need to be mangled. */
3711 for (const char *p = str + n - 1; ; --p)
3713 if (*p || p == str)
3715 n -= str + n - !!*p - p;
3716 break;
3719 tree eltype = TREE_TYPE (valtype);
3720 for (const char *p = str; n--; ++p)
3722 write_char ('L');
3723 write_type (eltype);
3724 write_unsigned_number (*(const unsigned char*)p);
3725 write_string ("E");
3727 break;
3730 default:
3731 gcc_unreachable ();
3734 write_char ('E');
3737 /* Non-terminal <template-arg>.
3739 <template-arg> ::= <type> # type
3740 ::= L <type> </value/ number> E # literal
3741 ::= LZ <name> E # external name
3742 ::= X <expression> E # expression */
3744 static void
3745 write_template_arg (tree node)
3747 enum tree_code code = TREE_CODE (node);
3749 MANGLE_TRACE_TREE ("template-arg", node);
3751 /* A template template parameter's argument list contains TREE_LIST
3752 nodes of which the value field is the actual argument. */
3753 if (code == TREE_LIST)
3755 node = TREE_VALUE (node);
3756 /* If it's a decl, deal with its type instead. */
3757 if (DECL_P (node))
3759 node = TREE_TYPE (node);
3760 code = TREE_CODE (node);
3764 if (TREE_CODE (node) == VAR_DECL && DECL_NTTP_OBJECT_P (node))
3765 /* We want to mangle the argument, not the var we stored it in. */
3766 node = tparm_object_argument (node);
3768 /* Strip a conversion added by convert_nontype_argument. */
3769 if (TREE_CODE (node) == IMPLICIT_CONV_EXPR)
3770 node = TREE_OPERAND (node, 0);
3771 if (REFERENCE_REF_P (node))
3772 node = TREE_OPERAND (node, 0);
3773 if (TREE_CODE (node) == NOP_EXPR
3774 && TYPE_REF_P (TREE_TYPE (node)))
3776 /* Template parameters can be of reference type. To maintain
3777 internal consistency, such arguments use a conversion from
3778 address of object to reference type. */
3779 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3780 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3783 if (TREE_CODE (node) == BASELINK
3784 && !type_unknown_p (node))
3786 if (abi_version_at_least (6))
3787 node = BASELINK_FUNCTIONS (node);
3788 if (abi_warn_or_compat_version_crosses (6))
3789 /* We wrongly wrapped a class-scope function in X/E. */
3790 G.need_abi_warning = 1;
3793 if (ARGUMENT_PACK_P (node))
3795 /* Expand the template argument pack. */
3796 tree args = ARGUMENT_PACK_ARGS (node);
3797 int i, length = TREE_VEC_LENGTH (args);
3798 if (abi_version_at_least (6))
3799 write_char ('J');
3800 else
3801 write_char ('I');
3802 if (abi_warn_or_compat_version_crosses (6))
3803 G.need_abi_warning = 1;
3804 for (i = 0; i < length; ++i)
3805 write_template_arg (TREE_VEC_ELT (args, i));
3806 write_char ('E');
3808 else if (TYPE_P (node))
3809 write_type (node);
3810 else if (code == TEMPLATE_DECL)
3811 /* A template appearing as a template arg is a template template arg. */
3812 write_template_template_arg (node);
3813 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3814 || code == CONST_DECL
3815 || null_member_pointer_value_p (node))
3816 write_template_arg_literal (node);
3817 else if (DECL_P (node))
3819 write_char ('L');
3820 /* Until ABI version 3, the underscore before the mangled name
3821 was incorrectly omitted. */
3822 if (!abi_version_at_least (3))
3823 write_char ('Z');
3824 else
3825 write_string ("_Z");
3826 if (abi_warn_or_compat_version_crosses (3))
3827 G.need_abi_warning = 1;
3828 write_encoding (node);
3829 write_char ('E');
3831 else
3833 /* Template arguments may be expressions. */
3834 write_char ('X');
3835 write_expression (node);
3836 write_char ('E');
3840 /* <template-template-arg>
3841 ::= <name>
3842 ::= <substitution> */
3844 static void
3845 write_template_template_arg (const tree decl)
3847 MANGLE_TRACE_TREE ("template-template-arg", decl);
3849 if (find_substitution (decl))
3850 return;
3851 write_name (decl, /*ignore_local_scope=*/0);
3852 add_substitution (decl);
3856 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3858 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3859 ::= A <expression> _ </element/ type>
3861 "Array types encode the dimension (number of elements) and the
3862 element type. For variable length arrays, the dimension (but not
3863 the '_' separator) is omitted."
3864 Note that for flexible array members, like for other arrays of
3865 unspecified size, the dimension is also omitted. */
3867 static void
3868 write_array_type (const tree type)
3870 write_char ('A');
3871 if (TYPE_DOMAIN (type))
3873 tree index_type;
3875 index_type = TYPE_DOMAIN (type);
3876 /* The INDEX_TYPE gives the upper and lower bounds of the array.
3877 It's null for flexible array members which have no upper bound
3878 (this is a change from GCC 5 and prior where such members were
3879 incorrectly mangled as zero-length arrays). */
3880 if (tree max = TYPE_MAX_VALUE (index_type))
3882 if (TREE_CODE (max) == INTEGER_CST)
3884 /* The ABI specifies that we should mangle the number of
3885 elements in the array, not the largest allowed index. */
3886 offset_int wmax = wi::to_offset (max) + 1;
3887 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3888 number of elements as zero. */
3889 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3890 gcc_assert (wi::fits_uhwi_p (wmax));
3891 write_unsigned_number (wmax.to_uhwi ());
3893 else
3895 max = TREE_OPERAND (max, 0);
3896 write_expression (max);
3900 write_char ('_');
3901 write_type (TREE_TYPE (type));
3904 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3905 variables. TYPE is a pointer-to-member POINTER_TYPE.
3907 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3909 static void
3910 write_pointer_to_member_type (const tree type)
3912 write_char ('M');
3913 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3914 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3917 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3918 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3919 TEMPLATE_PARM_INDEX.
3921 <template-param> ::= T </parameter/ number> _ */
3923 static void
3924 write_template_param (const tree parm)
3926 int parm_index;
3928 MANGLE_TRACE_TREE ("template-parm", parm);
3930 switch (TREE_CODE (parm))
3932 case TEMPLATE_TYPE_PARM:
3933 case TEMPLATE_TEMPLATE_PARM:
3934 case BOUND_TEMPLATE_TEMPLATE_PARM:
3935 parm_index = TEMPLATE_TYPE_IDX (parm);
3936 break;
3938 case TEMPLATE_PARM_INDEX:
3939 parm_index = TEMPLATE_PARM_IDX (parm);
3940 break;
3942 default:
3943 gcc_unreachable ();
3946 write_char ('T');
3947 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3948 earliest template param denoted by `_'. */
3949 write_compact_number (parm_index);
3952 /* <template-template-param>
3953 ::= <template-param>
3954 ::= <substitution> */
3956 static void
3957 write_template_template_param (const tree parm)
3959 tree templ = NULL_TREE;
3961 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3962 template template parameter. The substitution candidate here is
3963 only the template. */
3964 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3966 templ
3967 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3968 if (find_substitution (templ))
3969 return;
3972 /* <template-param> encodes only the template parameter position,
3973 not its template arguments, which is fine here. */
3974 write_template_param (parm);
3975 if (templ)
3976 add_substitution (templ);
3979 /* Non-terminal <substitution>.
3981 <substitution> ::= S <seq-id> _
3982 ::= S_ */
3984 static void
3985 write_substitution (const int seq_id)
3987 MANGLE_TRACE ("substitution", "");
3989 write_char ('S');
3990 if (seq_id > 0)
3991 write_number (seq_id - 1, /*unsigned=*/1, 36);
3992 write_char ('_');
3995 /* Start mangling ENTITY. */
3997 static inline void
3998 start_mangling (const tree entity)
4000 G.entity = entity;
4001 G.need_abi_warning = false;
4002 G.need_cxx17_warning = false;
4003 G.mod = false;
4004 obstack_free (&name_obstack, name_base);
4005 mangle_obstack = &name_obstack;
4006 name_base = obstack_alloc (&name_obstack, 0);
4009 /* Done with mangling. Release the data. */
4011 static void
4012 finish_mangling_internal (void)
4014 /* Clear all the substitutions. */
4015 vec_safe_truncate (G.substitutions, 0);
4017 if (G.mod)
4018 mangle_module_fini ();
4020 /* Null-terminate the string. */
4021 write_char ('\0');
4025 /* Like finish_mangling_internal, but return the mangled string. */
4027 static inline const char *
4028 finish_mangling (void)
4030 finish_mangling_internal ();
4031 return (const char *) obstack_finish (mangle_obstack);
4034 /* Like finish_mangling_internal, but return an identifier. */
4036 static tree
4037 finish_mangling_get_identifier (void)
4039 finish_mangling_internal ();
4040 /* Don't obstack_finish here, and the next start_mangling will
4041 remove the identifier. */
4042 return get_identifier ((const char *) obstack_base (mangle_obstack));
4045 /* Initialize data structures for mangling. */
4047 void
4048 init_mangle (void)
4050 gcc_obstack_init (&name_obstack);
4051 name_base = obstack_alloc (&name_obstack, 0);
4052 vec_alloc (G.substitutions, 0);
4054 /* Cache these identifiers for quick comparison when checking for
4055 standard substitutions. */
4056 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
4057 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
4058 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
4059 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
4060 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
4061 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
4064 /* Generate a mangling for MODULE's global initializer fn. */
4066 tree
4067 mangle_module_global_init (int module)
4069 start_mangling (NULL_TREE);
4071 write_string ("_ZGI");
4072 write_module (module, true);
4074 return finish_mangling_get_identifier ();
4077 /* Generate the mangled name of DECL. */
4079 static tree
4080 mangle_decl_string (const tree decl)
4082 tree result;
4083 tree saved_fn = NULL_TREE;
4084 bool template_p = false;
4086 /* We shouldn't be trying to mangle an uninstantiated template. */
4087 gcc_assert (!type_dependent_expression_p (decl));
4089 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4091 struct tinst_level *tl = current_instantiation ();
4092 if ((!tl || tl->maybe_get_node () != decl)
4093 && push_tinst_level (decl))
4095 template_p = true;
4096 saved_fn = current_function_decl;
4097 current_function_decl = NULL_TREE;
4100 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
4102 start_mangling (decl);
4104 if (TREE_CODE (decl) == TYPE_DECL)
4105 write_type (TREE_TYPE (decl));
4106 else
4107 write_mangled_name (decl, true);
4109 result = finish_mangling_get_identifier ();
4110 if (DEBUG_MANGLE)
4111 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
4112 IDENTIFIER_POINTER (result));
4114 if (template_p)
4116 pop_tinst_level ();
4117 current_function_decl = saved_fn;
4120 return result;
4123 /* Return an identifier for the external mangled name of DECL. */
4125 static tree
4126 get_mangled_id (tree decl)
4128 tree id = mangle_decl_string (decl);
4129 return targetm.mangle_decl_assembler_name (decl, id);
4132 /* Create an identifier for the external mangled name of DECL. */
4134 void
4135 mangle_decl (const tree decl)
4137 tree id;
4138 bool dep;
4140 /* Don't bother mangling uninstantiated templates. */
4141 ++processing_template_decl;
4142 if (TREE_CODE (decl) == TYPE_DECL)
4143 dep = dependent_type_p (TREE_TYPE (decl));
4144 else
4145 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4146 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
4147 --processing_template_decl;
4148 if (dep)
4149 return;
4151 /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
4152 It is not needed to assign names to anonymous namespace, but we use the
4153 "<anon>" marker to be able to tell if type is C++ ODR type or type
4154 produced by other language. */
4155 if (TREE_CODE (decl) == TYPE_DECL
4156 && TYPE_STUB_DECL (TREE_TYPE (decl))
4157 && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
4158 id = get_identifier ("<anon>");
4159 else
4161 gcc_assert (TREE_CODE (decl) != TYPE_DECL
4162 || !no_linkage_check (TREE_TYPE (decl), true));
4163 if (abi_version_at_least (10))
4164 if (tree fn = decl_function_context (decl))
4165 maybe_check_abi_tags (fn, decl);
4166 id = get_mangled_id (decl);
4168 SET_DECL_ASSEMBLER_NAME (decl, id);
4170 if (G.need_cxx17_warning
4171 && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl)))
4172 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type,
4173 "mangled name for %qD will change in C++17 because the "
4174 "exception specification is part of a function type",
4175 decl);
4177 if (id != DECL_NAME (decl)
4178 /* Don't do this for a fake symbol we aren't going to emit anyway. */
4179 && TREE_CODE (decl) != TYPE_DECL
4180 && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
4182 int save_ver = flag_abi_version;
4183 tree id2 = NULL_TREE;
4185 if (!DECL_REALLY_EXTERN (decl))
4187 record_mangling (decl, G.need_abi_warning);
4189 if (!G.need_abi_warning)
4190 return;
4192 flag_abi_version = flag_abi_compat_version;
4193 id2 = mangle_decl_string (decl);
4194 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4195 flag_abi_version = save_ver;
4197 if (id2 != id)
4198 note_mangling_alias (decl, id2);
4201 if (warn_abi)
4203 const char fabi_version[] = "-fabi-version";
4205 if (flag_abi_compat_version != warn_abi_version
4206 || id2 == NULL_TREE)
4208 flag_abi_version = warn_abi_version;
4209 id2 = mangle_decl_string (decl);
4210 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4212 flag_abi_version = save_ver;
4214 if (id2 == id)
4215 /* OK. */;
4216 else if (warn_abi_version != 0
4217 && abi_version_at_least (warn_abi_version))
4218 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4219 "the mangled name of %qD changed between "
4220 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4221 G.entity, fabi_version, warn_abi_version, id2,
4222 fabi_version, save_ver, id);
4223 else
4224 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4225 "the mangled name of %qD changes between "
4226 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4227 G.entity, fabi_version, save_ver, id,
4228 fabi_version, warn_abi_version, id2);
4231 flag_abi_version = save_ver;
4235 /* Generate the mangled representation of TYPE. */
4237 const char *
4238 mangle_type_string (const tree type)
4240 const char *result;
4242 start_mangling (type);
4243 write_type (type);
4244 result = finish_mangling ();
4245 if (DEBUG_MANGLE)
4246 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
4247 return result;
4250 /* Create an identifier for the mangled name of a special component
4251 for belonging to TYPE. CODE is the ABI-specified code for this
4252 component. */
4254 static tree
4255 mangle_special_for_type (const tree type, const char *code)
4257 tree result;
4259 /* We don't have an actual decl here for the special component, so
4260 we can't just process the <encoded-name>. Instead, fake it. */
4261 start_mangling (type);
4263 /* Start the mangling. */
4264 write_string ("_Z");
4265 write_string (code);
4267 /* Add the type. */
4268 write_type (type);
4269 result = finish_mangling_get_identifier ();
4271 if (DEBUG_MANGLE)
4272 fprintf (stderr, "mangle_special_for_type = %s\n\n",
4273 IDENTIFIER_POINTER (result));
4275 return result;
4278 /* Create an identifier for the mangled representation of the typeinfo
4279 structure for TYPE. */
4281 tree
4282 mangle_typeinfo_for_type (const tree type)
4284 return mangle_special_for_type (type, "TI");
4287 /* Create an identifier for the mangled name of the NTBS containing
4288 the mangled name of TYPE. */
4290 tree
4291 mangle_typeinfo_string_for_type (const tree type)
4293 return mangle_special_for_type (type, "TS");
4296 /* Create an identifier for the mangled name of the vtable for TYPE. */
4298 tree
4299 mangle_vtbl_for_type (const tree type)
4301 return mangle_special_for_type (type, "TV");
4304 /* Returns an identifier for the mangled name of the VTT for TYPE. */
4306 tree
4307 mangle_vtt_for_type (const tree type)
4309 return mangle_special_for_type (type, "TT");
4312 /* Returns an identifier for the mangled name of the decomposition
4313 artificial variable DECL. DECLS is the vector of the VAR_DECLs
4314 for the identifier-list. */
4316 tree
4317 mangle_decomp (const tree decl, vec<tree> &decls)
4319 gcc_assert (!type_dependent_expression_p (decl));
4321 location_t saved_loc = input_location;
4322 input_location = DECL_SOURCE_LOCATION (decl);
4324 start_mangling (decl);
4325 write_string ("_Z");
4327 tree context = decl_mangling_context (decl);
4328 gcc_assert (context != NULL_TREE);
4330 bool nested = false;
4331 if (DECL_NAMESPACE_STD_P (context))
4332 write_string ("St");
4333 else if (context != global_namespace)
4335 nested = true;
4336 write_char ('N');
4337 write_prefix (decl_mangling_context (decl));
4340 write_string ("DC");
4341 unsigned int i;
4342 tree d;
4343 FOR_EACH_VEC_ELT (decls, i, d)
4344 write_unqualified_name (d);
4345 write_char ('E');
4347 if (nested)
4348 write_char ('E');
4350 tree id = finish_mangling_get_identifier ();
4351 if (DEBUG_MANGLE)
4352 fprintf (stderr, "mangle_decomp = '%s'\n\n",
4353 IDENTIFIER_POINTER (id));
4355 input_location = saved_loc;
4356 return id;
4359 /* Return an identifier for a construction vtable group. TYPE is
4360 the most derived class in the hierarchy; BINFO is the base
4361 subobject for which this construction vtable group will be used.
4363 This mangling isn't part of the ABI specification; in the ABI
4364 specification, the vtable group is dumped in the same COMDAT as the
4365 main vtable, and is referenced only from that vtable, so it doesn't
4366 need an external name. For binary formats without COMDAT sections,
4367 though, we need external names for the vtable groups.
4369 We use the production
4371 <special-name> ::= CT <type> <offset number> _ <base type> */
4373 tree
4374 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
4376 tree result;
4378 start_mangling (type);
4380 write_string ("_Z");
4381 write_string ("TC");
4382 write_type (type);
4383 write_integer_cst (BINFO_OFFSET (binfo));
4384 write_char ('_');
4385 write_type (BINFO_TYPE (binfo));
4387 result = finish_mangling_get_identifier ();
4388 if (DEBUG_MANGLE)
4389 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
4390 IDENTIFIER_POINTER (result));
4391 return result;
4394 /* Mangle a this pointer or result pointer adjustment.
4396 <call-offset> ::= h <fixed offset number> _
4397 ::= v <fixed offset number> _ <virtual offset number> _ */
4399 static void
4400 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
4402 write_char (virtual_offset ? 'v' : 'h');
4404 /* For either flavor, write the fixed offset. */
4405 write_integer_cst (fixed_offset);
4406 write_char ('_');
4408 /* For a virtual thunk, add the virtual offset. */
4409 if (virtual_offset)
4411 write_integer_cst (virtual_offset);
4412 write_char ('_');
4416 /* Return an identifier for the mangled name of a this-adjusting or
4417 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
4418 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
4419 is a virtual thunk, and it is the vtbl offset in
4420 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
4421 zero for a covariant thunk. Note, that FN_DECL might be a covariant
4422 thunk itself. A covariant thunk name always includes the adjustment
4423 for the this pointer, even if there is none.
4425 <special-name> ::= T <call-offset> <base encoding>
4426 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
4427 <base encoding> */
4429 tree
4430 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
4431 tree virtual_offset, tree thunk)
4433 tree result;
4435 if (abi_version_at_least (11))
4436 maybe_check_abi_tags (fn_decl, thunk, 11);
4438 start_mangling (fn_decl);
4440 write_string ("_Z");
4441 write_char ('T');
4443 if (!this_adjusting)
4445 /* Covariant thunk with no this adjustment */
4446 write_char ('c');
4447 mangle_call_offset (integer_zero_node, NULL_TREE);
4448 mangle_call_offset (fixed_offset, virtual_offset);
4450 else if (!DECL_THUNK_P (fn_decl))
4451 /* Plain this adjusting thunk. */
4452 mangle_call_offset (fixed_offset, virtual_offset);
4453 else
4455 /* This adjusting thunk to covariant thunk. */
4456 write_char ('c');
4457 mangle_call_offset (fixed_offset, virtual_offset);
4458 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
4459 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
4460 if (virtual_offset)
4461 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
4462 mangle_call_offset (fixed_offset, virtual_offset);
4463 fn_decl = THUNK_TARGET (fn_decl);
4466 /* Scoped name. */
4467 write_encoding (fn_decl);
4469 result = finish_mangling_get_identifier ();
4470 if (DEBUG_MANGLE)
4471 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
4472 return result;
4475 /* Handle ABI backwards compatibility for past bugs where we didn't call
4476 check_abi_tags in places where it's needed: call check_abi_tags and warn if
4477 it makes a difference. If FOR_DECL is non-null, it's the declaration
4478 that we're actually trying to mangle; if it's null, we're mangling the
4479 guard variable for T. */
4481 static void
4482 maybe_check_abi_tags (tree t, tree for_decl, int ver)
4484 if (DECL_ASSEMBLER_NAME_SET_P (t))
4485 return;
4487 tree oldtags = get_abi_tags (t);
4489 mangle_decl (t);
4491 tree newtags = get_abi_tags (t);
4492 if (newtags && newtags != oldtags
4493 && abi_version_crosses (ver))
4495 if (for_decl && DECL_THUNK_P (for_decl))
4496 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4497 "the mangled name of a thunk for %qD changes between "
4498 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4499 t, flag_abi_version, warn_abi_version);
4500 else if (for_decl)
4501 warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
4502 "the mangled name of %qD changes between "
4503 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4504 for_decl, flag_abi_version, warn_abi_version);
4505 else
4506 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4507 "the mangled name of the initialization guard variable "
4508 "for %qD changes between %<-fabi-version=%d%> and "
4509 "%<-fabi-version=%d%>",
4510 t, flag_abi_version, warn_abi_version);
4514 /* Write out the appropriate string for this variable when generating
4515 another mangled name based on this one. */
4517 static void
4518 write_guarded_var_name (const tree variable)
4520 if (DECL_NAME (variable)
4521 && startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR"))
4522 /* The name of a guard variable for a reference temporary should refer
4523 to the reference, not the temporary. */
4524 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
4525 else
4526 write_name (variable, /*ignore_local_scope=*/0);
4529 /* Return an identifier for the name of an initialization guard
4530 variable for indicated VARIABLE. */
4532 tree
4533 mangle_guard_variable (const tree variable)
4535 if (abi_version_at_least (10))
4536 maybe_check_abi_tags (variable);
4537 start_mangling (variable);
4538 write_string ("_ZGV");
4539 write_guarded_var_name (variable);
4540 return finish_mangling_get_identifier ();
4543 /* Return an identifier for the name of a thread_local initialization
4544 function for VARIABLE. */
4546 tree
4547 mangle_tls_init_fn (const tree variable)
4549 check_abi_tags (variable);
4550 start_mangling (variable);
4551 write_string ("_ZTH");
4552 write_guarded_var_name (variable);
4553 return finish_mangling_get_identifier ();
4556 /* Return an identifier for the name of a thread_local wrapper
4557 function for VARIABLE. */
4559 #define TLS_WRAPPER_PREFIX "_ZTW"
4561 tree
4562 mangle_tls_wrapper_fn (const tree variable)
4564 check_abi_tags (variable);
4565 start_mangling (variable);
4566 write_string (TLS_WRAPPER_PREFIX);
4567 write_guarded_var_name (variable);
4568 return finish_mangling_get_identifier ();
4571 /* Return true iff FN is a thread_local wrapper function. */
4573 bool
4574 decl_tls_wrapper_p (const tree fn)
4576 if (TREE_CODE (fn) != FUNCTION_DECL)
4577 return false;
4578 tree name = DECL_NAME (fn);
4579 return startswith (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX);
4582 /* Return an identifier for the name of a temporary variable used to
4583 initialize a static reference. This is now part of the ABI. */
4585 tree
4586 mangle_ref_init_variable (const tree variable)
4588 start_mangling (variable);
4589 write_string ("_ZGR");
4590 check_abi_tags (variable);
4591 write_name (variable, /*ignore_local_scope=*/0);
4592 /* Avoid name clashes with aggregate initialization of multiple
4593 references at once. */
4594 write_compact_number (current_ref_temp_count++);
4595 return finish_mangling_get_identifier ();
4598 /* Return an identifier for the mangled name of a C++20 template parameter
4599 object for template argument EXPR. */
4601 tree
4602 mangle_template_parm_object (tree expr)
4604 start_mangling (expr);
4605 write_string ("_ZTAX");
4606 write_expression (expr);
4607 write_char ('E');
4608 return finish_mangling_get_identifier ();
4611 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
4612 function generates a mangled name for the vtable map variable of
4613 the class type. For example, if the class type is
4614 "std::bad_exception", the mangled name for the class is
4615 "St13bad_exception". This function would generate the name
4616 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
4617 "_VTV<std::bad_exception>::__vtable_map". */
4620 char *
4621 get_mangled_vtable_map_var_name (tree class_type)
4623 char *var_name = NULL;
4624 const char *prefix = "_ZN4_VTVI";
4625 const char *postfix = "E12__vtable_mapE";
4627 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
4629 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
4631 if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
4633 class_id = get_mangled_id (TYPE_NAME (class_type));
4634 vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
4637 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
4638 strlen (prefix) +
4639 strlen (postfix) + 1;
4641 var_name = (char *) xmalloc (len);
4643 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
4645 return var_name;
4648 #include "gt-cp-mangle.h"