ada: output.adb: fix newline being inserted when buffer is full
[official-gcc.git] / gcc / cp / mangle.cc
blob5789adcf680f5790b3acfc8c8543ed7c4e9b4049
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);
802 /* If this is the pre/post function for a guarded function, append
803 .pre/post, like something from create_virtual_clone. */
804 if (DECL_IS_PRE_FN_P (decl))
805 write_string (".pre");
806 else if (DECL_IS_POST_FN_P (decl))
807 write_string (".post");
809 /* If this is a coroutine helper, then append an appropriate string to
810 identify which. */
811 if (tree ramp = DECL_RAMP_FN (decl))
813 if (DECL_ACTOR_FN (ramp) == decl)
814 write_string (JOIN_STR "actor");
815 else if (DECL_DESTROY_FN (ramp) == decl)
816 write_string (JOIN_STR "destroy");
817 else
818 gcc_unreachable ();
822 /* Returns true if the return type of DECL is part of its signature, and
823 therefore its mangling. */
825 bool
826 mangle_return_type_p (tree decl)
828 return (!DECL_CONSTRUCTOR_P (decl)
829 && !DECL_DESTRUCTOR_P (decl)
830 && !DECL_CONV_FN_P (decl)
831 && maybe_template_info (decl));
834 /* <encoding> ::= <function name> <bare-function-type>
835 ::= <data name> */
837 static void
838 write_encoding (const tree decl)
840 MANGLE_TRACE_TREE ("encoding", decl);
842 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
844 /* For overloaded operators write just the mangled name
845 without arguments. */
846 if (DECL_OVERLOADED_OPERATOR_P (decl))
847 write_name (decl, /*ignore_local_scope=*/0);
848 else
849 write_source_name (DECL_NAME (decl));
850 return;
853 write_name (decl, /*ignore_local_scope=*/0);
854 if (TREE_CODE (decl) == FUNCTION_DECL)
856 tree fn_type;
857 tree d;
859 if (maybe_template_info (decl))
861 fn_type = get_mostly_instantiated_function_type (decl);
862 /* FN_TYPE will not have parameter types for in-charge or
863 VTT parameters. Therefore, we pass NULL_TREE to
864 write_bare_function_type -- otherwise, it will get
865 confused about which artificial parameters to skip. */
866 d = NULL_TREE;
868 else
870 fn_type = TREE_TYPE (decl);
871 d = decl;
874 write_bare_function_type (fn_type,
875 mangle_return_type_p (decl),
881 /* Interface to substitution and identifier mangling, used by the
882 module name mangler. */
884 void
885 mangle_module_substitution (int v)
887 write_substitution (v - 1);
891 mangle_module_component (tree comp, bool partition_p)
893 write_char ('W');
894 if (partition_p)
895 write_char ('P');
896 write_source_name (comp);
898 // Module substitutions use the same number-space as entity
899 // substitutions, but are orthogonal.
900 vec_safe_push (G.substitutions, NULL_TREE);
901 return G.substitutions->length ();
904 /* If the outermost non-namespace context (including DECL itself) is
905 a module-linkage decl, mangle the module information. For module
906 global initializers we need to include the partition part.
908 <module-name> ::= <module-sub>
909 || <subst>
910 || <module-name> <module-sub>
911 <module-sub> :: W [P] <unqualified-name>
914 static void
915 write_module (int m, bool include_partition)
917 G.mod = true;
918 mangle_module (m, include_partition);
921 static void
922 maybe_write_module (tree decl)
924 if (!DECL_NAMESPACE_SCOPE_P (decl))
925 return;
927 if (!TREE_PUBLIC (STRIP_TEMPLATE (decl)))
928 return;
930 if (TREE_CODE (decl) == NAMESPACE_DECL)
931 return;
933 int m = get_originating_module (decl, true);
934 if (m >= 0)
935 write_module (m, false);
938 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
939 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
941 static tree
942 decl_mangling_context (tree decl)
944 tree tcontext = targetm.cxx.decl_mangling_context (decl);
946 if (tcontext != NULL_TREE)
947 return tcontext;
949 if (TREE_CODE (decl) == TEMPLATE_DECL
950 && DECL_TEMPLATE_RESULT (decl))
951 decl = DECL_TEMPLATE_RESULT (decl);
953 if (TREE_CODE (decl) == TYPE_DECL
954 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
956 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
957 if (extra)
958 return extra;
960 else if (template_type_parameter_p (decl))
961 /* template type parms have no mangling context. */
962 return NULL_TREE;
964 tcontext = CP_DECL_CONTEXT (decl);
966 /* Ignore the artificial declare reduction functions. */
967 if (tcontext
968 && TREE_CODE (tcontext) == FUNCTION_DECL
969 && DECL_OMP_DECLARE_REDUCTION_P (tcontext))
970 return decl_mangling_context (tcontext);
972 return tcontext;
975 /* <name> ::= <unscoped-name>
976 ::= <unscoped-template-name> <template-args>
977 ::= <nested-name>
978 ::= <local-name>
980 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
981 called from <local-name>, which mangles the enclosing scope
982 elsewhere and then uses this function to mangle just the part
983 underneath the function scope. So don't use the <local-name>
984 production, to avoid an infinite recursion. */
986 static void
987 write_name (tree decl, const int ignore_local_scope)
989 tree context;
991 MANGLE_TRACE_TREE ("name", decl);
993 if (TREE_CODE (decl) == TYPE_DECL)
995 /* In case this is a typedef, fish out the corresponding
996 TYPE_DECL for the main variant. */
997 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
1000 context = decl_mangling_context (decl);
1002 gcc_assert (context != NULL_TREE);
1004 if (abi_warn_or_compat_version_crosses (7)
1005 && ignore_local_scope
1006 && TREE_CODE (context) == PARM_DECL)
1007 G.need_abi_warning = 1;
1009 /* A decl in :: or ::std scope is treated specially. The former is
1010 mangled using <unscoped-name> or <unscoped-template-name>, the
1011 latter with a special substitution. Also, a name that is
1012 directly in a local function scope is also mangled with
1013 <unscoped-name> rather than a full <nested-name>. */
1014 if (context == global_namespace
1015 || DECL_NAMESPACE_STD_P (context)
1016 || (ignore_local_scope
1017 && (TREE_CODE (context) == FUNCTION_DECL
1018 || (abi_version_at_least (7)
1019 && TREE_CODE (context) == PARM_DECL))))
1021 /* Is this a template instance? */
1022 if (tree info = maybe_template_info (decl))
1024 /* Yes: use <unscoped-template-name>. */
1025 write_unscoped_template_name (TI_TEMPLATE (info));
1026 write_template_args (TI_ARGS (info));
1028 else
1029 /* Everything else gets an <unqualified-name>. */
1030 write_unscoped_name (decl);
1032 else
1034 /* Handle local names, unless we asked not to (that is, invoked
1035 under <local-name>, to handle only the part of the name under
1036 the local scope). */
1037 if (!ignore_local_scope)
1039 /* Scan up the list of scope context, looking for a
1040 function. If we find one, this entity is in local
1041 function scope. local_entity tracks context one scope
1042 level down, so it will contain the element that's
1043 directly in that function's scope, either decl or one of
1044 its enclosing scopes. */
1045 tree local_entity = decl;
1046 while (context != global_namespace)
1048 /* Make sure we're always dealing with decls. */
1049 if (TYPE_P (context))
1050 context = TYPE_NAME (context);
1051 /* Is this a function? */
1052 if (TREE_CODE (context) == FUNCTION_DECL
1053 || TREE_CODE (context) == PARM_DECL)
1055 /* Yes, we have local scope. Use the <local-name>
1056 production for the innermost function scope. */
1057 write_local_name (context, local_entity, decl);
1058 return;
1060 /* Up one scope level. */
1061 local_entity = context;
1062 context = decl_mangling_context (context);
1065 /* No local scope found? Fall through to <nested-name>. */
1068 /* Other decls get a <nested-name> to encode their scope. */
1069 write_nested_name (decl);
1073 /* <unscoped-name> ::= <unqualified-name>
1074 ::= St <unqualified-name> # ::std:: */
1076 static void
1077 write_unscoped_name (const tree decl)
1079 tree context = decl_mangling_context (decl);
1081 MANGLE_TRACE_TREE ("unscoped-name", decl);
1083 /* Is DECL in ::std? */
1084 if (DECL_NAMESPACE_STD_P (context))
1086 write_string ("St");
1087 write_unqualified_name (decl);
1089 else
1091 /* If not, it should be either in the global namespace, or directly
1092 in a local function scope. A lambda can also be mangled in the
1093 scope of a default argument. */
1094 gcc_assert (context == global_namespace
1095 || TREE_CODE (context) == PARM_DECL
1096 || TREE_CODE (context) == FUNCTION_DECL);
1098 write_unqualified_name (decl);
1102 /* <unscoped-template-name> ::= <unscoped-name>
1103 ::= <substitution> */
1105 static void
1106 write_unscoped_template_name (const tree decl)
1108 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
1110 if (find_substitution (decl))
1111 return;
1112 write_unscoped_name (decl);
1113 add_substitution (decl);
1116 /* Write the nested name, including CV-qualifiers, of DECL.
1118 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1119 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1121 <ref-qualifier> ::= R # & ref-qualifier
1122 ::= O # && ref-qualifier
1123 <CV-qualifiers> ::= [r] [V] [K] */
1125 static void
1126 write_nested_name (const tree decl)
1128 MANGLE_TRACE_TREE ("nested-name", decl);
1130 write_char ('N');
1132 /* Write CV-qualifiers, if this is a member function. */
1133 if (TREE_CODE (decl) == FUNCTION_DECL
1134 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1136 if (DECL_VOLATILE_MEMFUNC_P (decl))
1137 write_char ('V');
1138 if (DECL_CONST_MEMFUNC_P (decl))
1139 write_char ('K');
1140 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
1142 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
1143 write_char ('O');
1144 else
1145 write_char ('R');
1149 /* Is this a template instance? */
1150 if (tree info = maybe_template_info (decl))
1152 /* Yes, use <template-prefix>. */
1153 write_template_prefix (decl);
1154 write_template_args (TI_ARGS (info));
1156 else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
1157 && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1159 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1160 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1162 write_template_prefix (decl);
1163 write_template_args (TREE_OPERAND (name, 1));
1165 else
1167 write_prefix (decl_mangling_context (decl));
1168 write_unqualified_name (decl);
1171 else
1173 /* No, just use <prefix> */
1174 write_prefix (decl_mangling_context (decl));
1175 write_unqualified_name (decl);
1177 write_char ('E');
1180 /* <prefix> ::= <prefix> <unqualified-name>
1181 ::= <template-param>
1182 ::= <template-prefix> <template-args>
1183 ::= <decltype>
1184 ::= # empty
1185 ::= <substitution> */
1187 static void
1188 write_prefix (const tree node)
1190 tree decl;
1192 if (node == NULL
1193 || node == global_namespace)
1194 return;
1196 MANGLE_TRACE_TREE ("prefix", node);
1198 if (TREE_CODE (node) == DECLTYPE_TYPE)
1200 write_type (node);
1201 return;
1204 if (find_substitution (node))
1205 return;
1207 tree template_info = NULL_TREE;
1208 if (DECL_P (node))
1210 /* If this is a function or parm decl, that means we've hit function
1211 scope, so this prefix must be for a local name. In this
1212 case, we're under the <local-name> production, which encodes
1213 the enclosing function scope elsewhere. So don't continue
1214 here. */
1215 if (TREE_CODE (node) == FUNCTION_DECL
1216 || TREE_CODE (node) == PARM_DECL)
1217 return;
1219 decl = node;
1220 template_info = maybe_template_info (decl);
1222 else
1224 /* Node is a type. */
1225 decl = TYPE_NAME (node);
1226 /* The DECL might not point at the node. */
1227 if (CLASSTYPE_TEMPLATE_ID_P (node))
1228 template_info = TYPE_TEMPLATE_INFO (node);
1231 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1232 write_template_param (node);
1233 else if (template_info)
1234 /* Templated. */
1236 write_template_prefix (decl);
1237 write_template_args (TI_ARGS (template_info));
1239 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1241 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1242 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1244 write_template_prefix (decl);
1245 write_template_args (TREE_OPERAND (name, 1));
1247 else
1249 write_prefix (decl_mangling_context (decl));
1250 write_unqualified_name (decl);
1253 else
1254 /* Not templated. */
1256 write_prefix (decl_mangling_context (decl));
1257 write_unqualified_name (decl);
1258 if (VAR_P (decl)
1259 || TREE_CODE (decl) == FIELD_DECL)
1261 /* <data-member-prefix> := <member source-name> M */
1262 write_char ('M');
1264 /* Before ABI 18, we did not count these as substitution
1265 candidates. This leads to incorrect demanglings (and
1266 ABI divergence to other compilers). */
1267 if (abi_warn_or_compat_version_crosses (18))
1268 G.need_abi_warning = true;
1269 if (!abi_version_at_least (18))
1270 return;
1274 add_substitution (node);
1277 /* <template-prefix> ::= <prefix> <template component>
1278 ::= <template-param>
1279 ::= <substitution> */
1281 static void
1282 write_template_prefix (const tree node)
1284 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1285 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1286 tree context = decl_mangling_context (decl);
1287 tree templ;
1288 tree substitution;
1290 MANGLE_TRACE_TREE ("template-prefix", node);
1292 /* Find the template decl. */
1293 if (tree info = maybe_template_info (decl))
1294 templ = TI_TEMPLATE (info);
1295 else if (TREE_CODE (type) == TYPENAME_TYPE)
1296 /* For a typename type, all we have is the name. */
1297 templ = DECL_NAME (decl);
1298 else
1300 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1302 templ = TYPE_TI_TEMPLATE (type);
1305 /* For a member template, though, the template name for the
1306 innermost name must have all the outer template levels
1307 instantiated. For instance, consider
1309 template<typename T> struct Outer {
1310 template<typename U> struct Inner {};
1313 The template name for `Inner' in `Outer<int>::Inner<float>' is
1314 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1315 levels separately, so there's no TEMPLATE_DECL available for this
1316 (there's only `Outer<T>::Inner<U>').
1318 In order to get the substitutions right, we create a special
1319 TREE_LIST to represent the substitution candidate for a nested
1320 template. The TREE_PURPOSE is the template's context, fully
1321 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1322 template.
1324 So, for the example above, `Outer<int>::Inner' is represented as a
1325 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1326 and whose value is `Outer<T>::Inner<U>'. */
1327 if (context && TYPE_P (context))
1328 substitution = build_tree_list (context, templ);
1329 else
1330 substitution = templ;
1332 if (find_substitution (substitution))
1333 return;
1335 if (TREE_TYPE (templ)
1336 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1337 write_template_param (TREE_TYPE (templ));
1338 else
1340 write_prefix (context);
1341 write_unqualified_name (decl);
1344 add_substitution (substitution);
1347 /* As the list of identifiers for the structured binding declaration
1348 DECL is likely gone, try to recover the DC <source-name>+ E portion
1349 from its mangled name. Return pointer to the DC and set len to
1350 the length up to and including the terminating E. On failure
1351 return NULL. */
1353 static const char *
1354 find_decomp_unqualified_name (tree decl, size_t *len)
1356 const char *p = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1357 const char *end = p + IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (decl));
1358 bool nested = false;
1359 if (!startswith (p, "_Z"))
1360 return NULL;
1361 p += 2;
1362 if (startswith (p, "St"))
1363 p += 2;
1364 else if (*p == 'N')
1366 nested = true;
1367 ++p;
1368 while (ISDIGIT (p[0]))
1370 char *e;
1371 long num = strtol (p, &e, 10);
1372 if (num >= 1 && num < end - e)
1373 p = e + num;
1374 else
1375 break;
1378 if (!startswith (p, "DC"))
1379 return NULL;
1380 if (nested)
1382 if (end[-1] != 'E')
1383 return NULL;
1384 --end;
1386 if (end[-1] != 'E')
1387 return NULL;
1388 *len = end - p;
1389 return p;
1392 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1393 mangled through special entry points.
1395 <unqualified-name> ::= [<module-name>] <operator-name>
1396 ::= <special-name>
1397 ::= [<module-name>] <source-name>
1398 ::= [<module-name>] <unnamed-type-name>
1399 ::= <local-source-name>
1401 <local-source-name> ::= L <source-name> <discriminator> */
1403 static void
1404 write_unqualified_id (tree identifier)
1406 if (IDENTIFIER_CONV_OP_P (identifier))
1407 write_conversion_operator_name (TREE_TYPE (identifier));
1408 else if (IDENTIFIER_OVL_OP_P (identifier))
1410 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier);
1411 write_string (ovl_op->mangled_name);
1413 else if (UDLIT_OPER_P (identifier))
1414 write_literal_operator_name (identifier);
1415 else
1416 write_source_name (identifier);
1419 static void
1420 write_unqualified_name (tree decl)
1422 MANGLE_TRACE_TREE ("unqualified-name", decl);
1424 if (modules_p ())
1425 maybe_write_module (decl);
1427 if (identifier_p (decl))
1429 write_unqualified_id (decl);
1430 return;
1433 bool found = false;
1435 if (DECL_NAME (decl) == NULL_TREE)
1437 found = true;
1438 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1439 const char *decomp_str = NULL;
1440 size_t decomp_len = 0;
1441 if (VAR_P (decl)
1442 && DECL_DECOMPOSITION_P (decl)
1443 && DECL_NAME (decl) == NULL_TREE
1444 && DECL_NAMESPACE_SCOPE_P (decl))
1445 decomp_str = find_decomp_unqualified_name (decl, &decomp_len);
1446 if (decomp_str)
1447 write_chars (decomp_str, decomp_len);
1448 else
1449 write_source_name (DECL_ASSEMBLER_NAME (decl));
1451 else if (DECL_DECLARES_FUNCTION_P (decl))
1453 found = true;
1454 if (DECL_CONSTRUCTOR_P (decl))
1455 write_special_name_constructor (decl);
1456 else if (DECL_DESTRUCTOR_P (decl))
1457 write_special_name_destructor (decl);
1458 else if (DECL_CONV_FN_P (decl))
1460 /* Conversion operator. Handle it right here.
1461 <operator> ::= cv <type> */
1462 tree type;
1463 if (maybe_template_info (decl))
1465 tree fn_type;
1466 fn_type = get_mostly_instantiated_function_type (decl);
1467 type = TREE_TYPE (fn_type);
1469 else if (FNDECL_USED_AUTO (decl))
1470 type = DECL_SAVED_AUTO_RETURN_TYPE (decl);
1471 else
1472 type = DECL_CONV_FN_TYPE (decl);
1473 write_conversion_operator_name (type);
1475 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1477 tree t;
1478 if (!(t = DECL_RAMP_FN (decl)))
1479 t = decl;
1480 const char *mangled_name
1481 = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (t)]
1482 [DECL_OVERLOADED_OPERATOR_CODE_RAW (t)].mangled_name);
1483 write_string (mangled_name);
1485 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1486 write_literal_operator_name (DECL_NAME (decl));
1487 else
1488 found = false;
1491 if (found)
1492 /* OK */;
1493 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1494 && DECL_NAMESPACE_SCOPE_P (decl)
1495 && decl_linkage (decl) == lk_internal)
1497 MANGLE_TRACE_TREE ("local-source-name", decl);
1498 write_char ('L');
1499 write_source_name (DECL_NAME (decl));
1500 /* The default discriminator is 1, and that's all we ever use,
1501 so there's no code to output one here. */
1503 else
1505 tree type = TREE_TYPE (decl);
1507 if (TREE_CODE (decl) == TYPE_DECL
1508 && TYPE_UNNAMED_P (type))
1509 write_unnamed_type_name (type);
1510 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type))
1511 write_closure_type_name (type);
1512 else
1513 write_source_name (DECL_NAME (decl));
1516 /* We use the ABI tags from the primary class template, ignoring tags on any
1517 specializations. This is necessary because C++ doesn't require a
1518 specialization to be declared before it is used unless the use requires a
1519 complete type, but we need to get the tags right on incomplete types as
1520 well. */
1521 if (tree tmpl = most_general_template (decl))
1523 tree res = DECL_TEMPLATE_RESULT (tmpl);
1524 if (res == NULL_TREE)
1525 /* UNBOUND_CLASS_TEMPLATE. */;
1526 else if (DECL_DECLARES_TYPE_P (decl))
1527 decl = res;
1528 else if (any_abi_below (11))
1530 /* ABI v10 implicit tags on the template. */
1531 tree mtags = missing_abi_tags (res);
1532 /* Explicit tags on the template. */
1533 tree ttags = get_abi_tags (res);
1534 /* Tags on the instantiation. */
1535 tree dtags = get_abi_tags (decl);
1537 if (mtags && abi_warn_or_compat_version_crosses (10))
1538 G.need_abi_warning = 1;
1540 /* Add the v10 tags to the explicit tags now. */
1541 mtags = chainon (mtags, ttags);
1543 if (!G.need_abi_warning
1544 && abi_warn_or_compat_version_crosses (11)
1545 && !equal_abi_tags (dtags, mtags))
1546 G.need_abi_warning = 1;
1548 if (!abi_version_at_least (10))
1549 /* In abi <10, we only got the explicit tags. */
1550 decl = res;
1551 else if (flag_abi_version == 10)
1553 /* In ABI 10, we want explict and implicit tags. */
1554 write_abi_tags (mtags);
1555 return;
1560 tree tags = get_abi_tags (decl);
1561 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
1562 && any_abi_below (11))
1563 if (tree mtags = missing_abi_tags (decl))
1565 if (abi_warn_or_compat_version_crosses (11))
1566 G.need_abi_warning = true;
1567 if (!abi_version_at_least (11))
1568 tags = chainon (mtags, tags);
1570 write_abi_tags (tags);
1573 /* Write the unqualified-name for a conversion operator to TYPE. */
1575 static void
1576 write_conversion_operator_name (const tree type)
1578 write_string ("cv");
1579 write_type (type);
1582 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1584 <source-name> ::= </length/ number> <identifier> */
1586 static void
1587 write_source_name (tree identifier)
1589 MANGLE_TRACE_TREE ("source-name", identifier);
1591 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1592 write_identifier (IDENTIFIER_POINTER (identifier));
1595 /* Compare two TREE_STRINGs like strcmp. */
1598 tree_string_cmp (const void *p1, const void *p2)
1600 if (p1 == p2)
1601 return 0;
1602 tree s1 = *(const tree*)p1;
1603 tree s2 = *(const tree*)p2;
1604 return strcmp (TREE_STRING_POINTER (s1),
1605 TREE_STRING_POINTER (s2));
1608 /* Return the TREE_LIST of TAGS as a sorted VEC. */
1610 static vec<tree, va_gc> *
1611 sorted_abi_tags (tree tags)
1613 vec<tree, va_gc> * vec = make_tree_vector();
1615 for (tree t = tags; t; t = TREE_CHAIN (t))
1617 if (ABI_TAG_IMPLICIT (t))
1618 continue;
1619 tree str = TREE_VALUE (t);
1620 vec_safe_push (vec, str);
1623 vec->qsort (tree_string_cmp);
1625 return vec;
1628 /* ID is the name of a function or type with abi_tags attribute TAGS.
1629 Write out the name, suitably decorated. */
1631 static void
1632 write_abi_tags (tree tags)
1634 if (tags == NULL_TREE)
1635 return;
1637 vec<tree, va_gc> * vec = sorted_abi_tags (tags);
1639 unsigned i; tree str;
1640 FOR_EACH_VEC_ELT (*vec, i, str)
1642 write_string ("B");
1643 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1644 write_identifier (TREE_STRING_POINTER (str));
1647 release_tree_vector (vec);
1650 /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */
1652 static bool
1653 equal_abi_tags (tree t1, tree t2)
1655 releasing_vec v1 = sorted_abi_tags (t1);
1656 releasing_vec v2 = sorted_abi_tags (t2);
1658 unsigned len1 = v1->length();
1659 if (len1 != v2->length())
1660 return false;
1661 for (unsigned i = 0; i < len1; ++i)
1662 if (tree_string_cmp (v1[i], v2[i]) != 0)
1663 return false;
1664 return true;
1667 /* Write a user-defined literal operator.
1668 ::= li <source-name> # "" <source-name>
1669 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1671 static void
1672 write_literal_operator_name (tree identifier)
1674 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1675 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1676 write_unsigned_number (strlen (suffix));
1677 write_identifier (suffix);
1680 /* Encode 0 as _, and 1+ as n-1_. */
1682 static void
1683 write_compact_number (int num)
1685 gcc_checking_assert (num >= 0);
1686 if (num > 0)
1687 write_unsigned_number (num - 1);
1688 write_char ('_');
1691 /* Return how many unnamed types precede TYPE in its enclosing class. */
1693 static int
1694 nested_anon_class_index (tree type)
1696 int index = 0;
1697 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1698 for (; member; member = DECL_CHAIN (member))
1699 if (DECL_IMPLICIT_TYPEDEF_P (member))
1701 tree memtype = TREE_TYPE (member);
1702 if (memtype == type)
1703 return index;
1704 else if (TYPE_UNNAMED_P (memtype))
1705 ++index;
1708 if (seen_error ())
1709 return -1;
1711 gcc_unreachable ();
1714 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1716 static void
1717 write_unnamed_type_name (const tree type)
1719 int discriminator;
1720 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1722 if (TYPE_FUNCTION_SCOPE_P (type))
1723 discriminator = discriminator_for_local_entity (TYPE_NAME (type));
1724 else if (TYPE_CLASS_SCOPE_P (type))
1725 discriminator = nested_anon_class_index (type);
1726 else
1728 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1729 /* Just use the old mangling at namespace scope. */
1730 write_source_name (TYPE_IDENTIFIER (type));
1731 return;
1734 write_string ("Ut");
1735 write_compact_number (discriminator);
1738 // A template head, for templated lambdas.
1739 // <template-head> ::= Tp* Ty
1740 // Tp* Tn <type>
1741 // Tp* Tt <template-head> E
1742 // New in ABI=18. Returns true iff we emitted anything -- used for ABI
1743 // version warning.
1745 static bool
1746 write_closure_template_head (tree tmpl)
1748 bool any = false;
1750 // We only need one level of template parms
1751 tree inner = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
1753 for (int ix = 0, len = TREE_VEC_LENGTH (inner); ix != len; ix++)
1755 tree parm = TREE_VEC_ELT (inner, ix);
1756 if (parm == error_mark_node)
1757 continue;
1758 parm = TREE_VALUE (parm);
1760 if (DECL_VIRTUAL_P (parm))
1761 // A synthetic parm, we're done.
1762 break;
1764 any = true;
1765 if (abi_version_at_least (18))
1767 if (TREE_CODE (parm) == PARM_DECL
1768 ? TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
1769 : TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
1770 write_string ("Tp");
1772 switch (TREE_CODE (parm))
1774 default:
1775 gcc_unreachable ();
1777 case TYPE_DECL:
1778 write_string ("Ty");
1779 break;
1781 case PARM_DECL:
1782 write_string ("Tn");
1783 write_type (TREE_TYPE (parm));
1784 break;
1786 case TEMPLATE_DECL:
1787 write_string ("Tt");
1788 write_closure_template_head (parm);
1789 write_string ("E");
1790 break;
1795 return any;
1798 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1799 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1801 static void
1802 write_closure_type_name (const tree type)
1804 tree fn = lambda_function (type);
1805 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1806 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1808 MANGLE_TRACE_TREE ("closure-type-name", type);
1810 write_string ("Ul");
1812 if (auto ti = maybe_template_info (fn))
1813 if (write_closure_template_head (TI_TEMPLATE (ti)))
1814 // If there were any explicit template parms, we may need to
1815 // issue a mangling diagnostic.
1816 if (abi_warn_or_compat_version_crosses (18))
1817 G.need_abi_warning = true;
1819 write_method_parms (parms, /*method_p=*/1, fn);
1820 write_char ('E');
1821 if ((LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
1822 != LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda))
1823 && abi_warn_or_compat_version_crosses (18))
1824 G.need_abi_warning = true;
1825 write_compact_number (abi_version_at_least (18)
1826 ? LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
1827 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda));
1830 /* Convert NUMBER to ascii using base BASE and generating at least
1831 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1832 into which to store the characters. Returns the number of
1833 characters generated (these will be laid out in advance of where
1834 BUFFER points). */
1836 static int
1837 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1838 char *buffer, const unsigned int min_digits)
1840 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1841 unsigned digits = 0;
1843 while (number)
1845 unsigned HOST_WIDE_INT d = number / base;
1847 *--buffer = base_digits[number - d * base];
1848 digits++;
1849 number = d;
1851 while (digits < min_digits)
1853 *--buffer = base_digits[0];
1854 digits++;
1856 return digits;
1859 /* Non-terminal <number>.
1861 <number> ::= [n] </decimal integer/> */
1863 static void
1864 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1865 const unsigned int base)
1867 char buffer[sizeof (HOST_WIDE_INT) * 8];
1868 unsigned count = 0;
1870 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1872 write_char ('n');
1873 number = -((HOST_WIDE_INT) number);
1875 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1876 write_chars (buffer + sizeof (buffer) - count, count);
1879 /* Write out an integral CST in decimal. Most numbers are small, and
1880 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1881 bigger than that, which we must deal with. */
1883 static inline void
1884 write_integer_cst (const tree cst)
1886 int sign = tree_int_cst_sgn (cst);
1887 widest_int abs_value = wi::abs (wi::to_widest (cst));
1888 if (!wi::fits_uhwi_p (abs_value))
1890 /* A bignum. We do this in chunks, each of which fits in a
1891 HOST_WIDE_INT. */
1892 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1893 unsigned HOST_WIDE_INT chunk;
1894 unsigned chunk_digits;
1895 char *ptr = buffer + sizeof (buffer);
1896 unsigned count = 0;
1897 tree n, base, type;
1898 int done;
1900 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1901 representable. */
1902 chunk = 1000000000;
1903 chunk_digits = 9;
1905 if (sizeof (HOST_WIDE_INT) >= 8)
1907 /* It is at least 64 bits, so 10^18 is representable. */
1908 chunk_digits = 18;
1909 chunk *= chunk;
1912 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1913 base = build_int_cstu (type, chunk);
1914 n = wide_int_to_tree (type, wi::to_wide (cst));
1916 if (sign < 0)
1918 write_char ('n');
1919 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1923 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1924 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1925 unsigned c;
1927 done = integer_zerop (d);
1928 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1929 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1930 done ? 1 : chunk_digits);
1931 ptr -= c;
1932 count += c;
1933 n = d;
1935 while (!done);
1936 write_chars (ptr, count);
1938 else
1940 /* A small num. */
1941 if (sign < 0)
1942 write_char ('n');
1943 write_unsigned_number (abs_value.to_uhwi ());
1947 /* Write out a floating-point literal.
1949 "Floating-point literals are encoded using the bit pattern of the
1950 target processor's internal representation of that number, as a
1951 fixed-length lowercase hexadecimal string, high-order bytes first
1952 (even if the target processor would store low-order bytes first).
1953 The "n" prefix is not used for floating-point literals; the sign
1954 bit is encoded with the rest of the number.
1956 Here are some examples, assuming the IEEE standard representation
1957 for floating point numbers. (Spaces are for readability, not
1958 part of the encoding.)
1960 1.0f Lf 3f80 0000 E
1961 -1.0f Lf bf80 0000 E
1962 1.17549435e-38f Lf 0080 0000 E
1963 1.40129846e-45f Lf 0000 0001 E
1964 0.0f Lf 0000 0000 E"
1966 Caller is responsible for the Lx and the E. */
1967 static void
1968 write_real_cst (const tree value)
1970 long target_real[4]; /* largest supported float */
1971 /* Buffer for eight hex digits in a 32-bit number but big enough
1972 even for 64-bit long to avoid warnings. */
1973 char buffer[17];
1974 int i, limit, dir;
1976 tree type = TREE_TYPE (value);
1977 int words = GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type)) / 32;
1979 real_to_target (target_real, &TREE_REAL_CST (value),
1980 TYPE_MODE (type));
1982 /* The value in target_real is in the target word order,
1983 so we must write it out backward if that happens to be
1984 little-endian. write_number cannot be used, it will
1985 produce uppercase. */
1986 if (FLOAT_WORDS_BIG_ENDIAN)
1987 i = 0, limit = words, dir = 1;
1988 else
1989 i = words - 1, limit = -1, dir = -1;
1991 for (; i != limit; i += dir)
1993 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1994 write_chars (buffer, 8);
1998 /* Non-terminal <identifier>.
2000 <identifier> ::= </unqualified source code identifier> */
2002 static void
2003 write_identifier (const char *identifier)
2005 MANGLE_TRACE ("identifier", identifier);
2006 write_string (identifier);
2009 /* Handle constructor productions of non-terminal <special-name>.
2010 CTOR is a constructor FUNCTION_DECL.
2012 <special-name> ::= C1 # complete object constructor
2013 ::= C2 # base object constructor
2014 ::= C3 # complete object allocating constructor
2016 Currently, allocating constructors are never used. */
2018 static void
2019 write_special_name_constructor (const tree ctor)
2021 write_char ('C');
2022 bool new_inh = (flag_new_inheriting_ctors
2023 && DECL_INHERITED_CTOR (ctor));
2024 if (new_inh)
2025 write_char ('I');
2026 if (DECL_BASE_CONSTRUCTOR_P (ctor))
2027 write_char ('2');
2028 /* This is the old-style "[unified]" constructor.
2029 In some cases, we may emit this function and call
2030 it from the clones in order to share code and save space. */
2031 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
2032 write_char ('4');
2033 else
2035 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
2036 write_char ('1');
2038 if (new_inh)
2039 write_type (DECL_INHERITED_CTOR_BASE (ctor));
2042 /* Handle destructor productions of non-terminal <special-name>.
2043 DTOR is a destructor FUNCTION_DECL.
2045 <special-name> ::= D0 # deleting (in-charge) destructor
2046 ::= D1 # complete object (in-charge) destructor
2047 ::= D2 # base object (not-in-charge) destructor */
2049 static void
2050 write_special_name_destructor (const tree dtor)
2052 if (DECL_DELETING_DESTRUCTOR_P (dtor))
2053 write_string ("D0");
2054 else if (DECL_BASE_DESTRUCTOR_P (dtor))
2055 write_string ("D2");
2056 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
2057 /* This is the old-style "[unified]" destructor.
2058 In some cases, we may emit this function and call
2059 it from the clones in order to share code and save space. */
2060 write_string ("D4");
2061 else
2063 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
2064 write_string ("D1");
2068 /* Return the discriminator for ENTITY appearing inside
2069 FUNCTION. The discriminator is the lexical ordinal of VAR or TYPE among
2070 entities with the same name and kind in the same FUNCTION. */
2072 static int
2073 discriminator_for_local_entity (tree entity)
2075 if (!DECL_LANG_SPECIFIC (entity))
2077 /* Some decls, like __FUNCTION__, don't need a discriminator. */
2078 gcc_checking_assert (DECL_ARTIFICIAL (entity));
2079 return 0;
2081 else if (tree disc = DECL_DISCRIMINATOR (entity))
2082 return TREE_INT_CST_LOW (disc);
2083 else
2084 /* The first entity with a particular name doesn't get
2085 DECL_DISCRIMINATOR set up. */
2086 return 0;
2089 /* Return the discriminator for STRING, a string literal used inside
2090 FUNCTION. The discriminator is the lexical ordinal of STRING among
2091 string literals used in FUNCTION. */
2093 static int
2094 discriminator_for_string_literal (tree /*function*/,
2095 tree /*string*/)
2097 /* For now, we don't discriminate amongst string literals. */
2098 return 0;
2101 /* <discriminator> := _ <number> # when number < 10
2102 := __ <number> _ # when number >= 10
2104 The discriminator is used only for the second and later occurrences
2105 of the same name within a single function. In this case <number> is
2106 n - 2, if this is the nth occurrence, in lexical order. */
2108 static void
2109 write_discriminator (const int discriminator)
2111 /* If discriminator is zero, don't write anything. Otherwise... */
2112 if (discriminator > 0)
2114 write_char ('_');
2115 if (discriminator - 1 >= 10)
2117 if (abi_warn_or_compat_version_crosses (11))
2118 G.need_abi_warning = 1;
2119 if (abi_version_at_least (11))
2120 write_char ('_');
2122 write_unsigned_number (discriminator - 1);
2123 if (abi_version_at_least (11) && discriminator - 1 >= 10)
2124 write_char ('_');
2128 /* Mangle the name of a function-scope entity. FUNCTION is the
2129 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
2130 default argument scope. ENTITY is the decl for the entity itself.
2131 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
2132 either ENTITY itself or an enclosing scope of ENTITY.
2134 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2135 := Z <function encoding> E s [<discriminator>]
2136 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
2138 static void
2139 write_local_name (tree function, const tree local_entity,
2140 const tree entity)
2142 tree parm = NULL_TREE;
2144 MANGLE_TRACE_TREE ("local-name", entity);
2146 if (TREE_CODE (function) == PARM_DECL)
2148 parm = function;
2149 function = DECL_CONTEXT (parm);
2152 write_char ('Z');
2153 write_encoding (function);
2154 write_char ('E');
2156 /* For this purpose, parameters are numbered from right-to-left. */
2157 if (parm)
2159 int i = list_length (parm);
2160 write_char ('d');
2161 write_compact_number (i - 1);
2164 if (TREE_CODE (entity) == STRING_CST)
2166 write_char ('s');
2167 write_discriminator (discriminator_for_string_literal (function,
2168 entity));
2170 else
2172 /* Now the <entity name>. Let write_name know its being called
2173 from <local-name>, so it doesn't try to process the enclosing
2174 function scope again. */
2175 write_name (entity, /*ignore_local_scope=*/1);
2176 if (DECL_DISCRIMINATOR_P (local_entity)
2177 && !(TREE_CODE (local_entity) == TYPE_DECL
2178 && TYPE_ANON_P (TREE_TYPE (local_entity))))
2179 write_discriminator (discriminator_for_local_entity (local_entity));
2183 /* Non-terminals <type> and <CV-qualifier>.
2185 <type> ::= <builtin-type>
2186 ::= <function-type>
2187 ::= <class-enum-type>
2188 ::= <array-type>
2189 ::= <pointer-to-member-type>
2190 ::= <template-param>
2191 ::= <substitution>
2192 ::= <CV-qualifier>
2193 ::= P <type> # pointer-to
2194 ::= R <type> # reference-to
2195 ::= C <type> # complex pair (C 2000)
2196 ::= G <type> # imaginary (C 2000) [not supported]
2197 ::= U <source-name> <type> # vendor extended type qualifier
2199 C++0x extensions
2201 <type> ::= RR <type> # rvalue reference-to
2202 <type> ::= Dt <expression> # decltype of an id-expression or
2203 # class member access
2204 <type> ::= DT <expression> # decltype of an expression
2205 <type> ::= Dn # decltype of nullptr
2207 TYPE is a type node. */
2209 static void
2210 write_type (tree type)
2212 /* This gets set to nonzero if TYPE turns out to be a (possibly
2213 CV-qualified) builtin type. */
2214 int is_builtin_type = 0;
2216 MANGLE_TRACE_TREE ("type", type);
2218 if (type == error_mark_node)
2219 return;
2221 type = canonicalize_for_substitution (type);
2222 if (find_substitution (type))
2223 return;
2226 if (write_CV_qualifiers_for_type (type) > 0)
2227 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
2228 mangle the unqualified type. The recursive call is needed here
2229 since both the qualified and unqualified types are substitution
2230 candidates. */
2232 tree t = TYPE_MAIN_VARIANT (type);
2233 if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t))
2235 tree attrs = NULL_TREE;
2236 if (tx_safe_fn_type_p (type))
2237 attrs = tree_cons (get_identifier ("transaction_safe"),
2238 NULL_TREE, attrs);
2239 t = cp_build_type_attribute_variant (t, attrs);
2241 gcc_assert (t != type);
2242 if (FUNC_OR_METHOD_TYPE_P (t))
2244 t = build_ref_qualified_type (t, type_memfn_rqual (type));
2245 if (flag_noexcept_type)
2247 tree r = TYPE_RAISES_EXCEPTIONS (type);
2248 t = build_exception_variant (t, r);
2250 if (abi_version_at_least (8)
2251 || type == TYPE_MAIN_VARIANT (type))
2252 /* Avoid adding the unqualified function type as a substitution. */
2253 write_function_type (t);
2254 else
2255 write_type (t);
2256 if (abi_warn_or_compat_version_crosses (8))
2257 G.need_abi_warning = 1;
2259 else
2260 write_type (t);
2262 else if (TREE_CODE (type) == ARRAY_TYPE)
2263 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
2264 so that the cv-qualification of the element type is available
2265 in write_array_type. */
2266 write_array_type (type);
2267 else
2269 tree type_orig = type;
2271 /* See through any typedefs. */
2272 type = TYPE_MAIN_VARIANT (type);
2273 if (FUNC_OR_METHOD_TYPE_P (type))
2274 type = cxx_copy_lang_qualifiers (type, type_orig);
2276 /* According to the C++ ABI, some library classes are passed the
2277 same as the scalar type of their single member and use the same
2278 mangling. */
2279 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2280 type = TREE_TYPE (first_field (type));
2282 if (TYPE_PTRDATAMEM_P (type))
2283 write_pointer_to_member_type (type);
2284 else
2286 /* Handle any target-specific fundamental types. */
2287 const char *target_mangling
2288 = targetm.mangle_type (type_orig);
2290 if (target_mangling)
2292 write_string (target_mangling);
2293 /* Add substitutions for types other than fundamental
2294 types. */
2295 if (!VOID_TYPE_P (type)
2296 && TREE_CODE (type) != INTEGER_TYPE
2297 && TREE_CODE (type) != REAL_TYPE
2298 && TREE_CODE (type) != BOOLEAN_TYPE)
2299 add_substitution (type);
2300 return;
2303 switch (TREE_CODE (type))
2305 case VOID_TYPE:
2306 case BOOLEAN_TYPE:
2307 case INTEGER_TYPE: /* Includes wchar_t. */
2308 case REAL_TYPE:
2309 case FIXED_POINT_TYPE:
2311 /* If this is a typedef, TYPE may not be one of
2312 the standard builtin type nodes, but an alias of one. Use
2313 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
2314 write_builtin_type (TYPE_MAIN_VARIANT (type));
2315 ++is_builtin_type;
2317 break;
2319 case COMPLEX_TYPE:
2320 write_char ('C');
2321 write_type (TREE_TYPE (type));
2322 break;
2324 case FUNCTION_TYPE:
2325 case METHOD_TYPE:
2326 write_function_type (type);
2327 break;
2329 case UNION_TYPE:
2330 case RECORD_TYPE:
2331 case ENUMERAL_TYPE:
2332 /* A pointer-to-member function is represented as a special
2333 RECORD_TYPE, so check for this first. */
2334 if (TYPE_PTRMEMFUNC_P (type))
2335 write_pointer_to_member_type (type);
2336 else
2337 write_class_enum_type (type);
2338 break;
2340 case TYPENAME_TYPE:
2341 case UNBOUND_CLASS_TEMPLATE:
2342 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2343 ordinary nested names. */
2344 write_nested_name (TYPE_STUB_DECL (type));
2345 break;
2347 case POINTER_TYPE:
2348 case REFERENCE_TYPE:
2349 if (TYPE_PTR_P (type))
2350 write_char ('P');
2351 else if (TYPE_REF_IS_RVALUE (type))
2352 write_char ('O');
2353 else
2354 write_char ('R');
2356 tree target = TREE_TYPE (type);
2357 /* Attribute const/noreturn are not reflected in mangling.
2358 We strip them here rather than at a lower level because
2359 a typedef or template argument can have function type
2360 with function-cv-quals (that use the same representation),
2361 but you can't have a pointer/reference to such a type. */
2362 if (TREE_CODE (target) == FUNCTION_TYPE)
2364 if (abi_warn_or_compat_version_crosses (5)
2365 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2366 G.need_abi_warning = 1;
2367 if (abi_version_at_least (5))
2368 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2370 write_type (target);
2372 break;
2374 case TEMPLATE_TYPE_PARM:
2375 if (is_auto (type))
2377 if (AUTO_IS_DECLTYPE (type))
2378 write_identifier ("Dc");
2379 else
2380 write_identifier ("Da");
2381 ++is_builtin_type;
2382 break;
2384 /* fall through. */
2385 case TEMPLATE_PARM_INDEX:
2386 write_template_param (type);
2387 break;
2389 case TEMPLATE_TEMPLATE_PARM:
2390 write_template_template_param (type);
2391 break;
2393 case BOUND_TEMPLATE_TEMPLATE_PARM:
2394 write_template_template_param (type);
2395 write_template_args
2396 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2397 break;
2399 case VECTOR_TYPE:
2400 if (abi_version_at_least (4))
2402 write_string ("Dv");
2403 /* Non-constant vector size would be encoded with
2404 _ expression, but we don't support that yet. */
2405 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type)
2406 .to_constant ());
2407 write_char ('_');
2409 else
2410 write_string ("U8__vector");
2411 if (abi_warn_or_compat_version_crosses (4))
2412 G.need_abi_warning = 1;
2413 write_type (TREE_TYPE (type));
2414 break;
2416 case TYPE_PACK_EXPANSION:
2417 write_string ("Dp");
2418 write_type (PACK_EXPANSION_PATTERN (type));
2419 break;
2421 case DECLTYPE_TYPE:
2422 /* These shouldn't make it into mangling. */
2423 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2424 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2426 /* In ABI <5, we stripped decltype of a plain decl. */
2427 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2429 tree expr = DECLTYPE_TYPE_EXPR (type);
2430 tree etype = NULL_TREE;
2431 switch (TREE_CODE (expr))
2433 case VAR_DECL:
2434 case PARM_DECL:
2435 case RESULT_DECL:
2436 case FUNCTION_DECL:
2437 case CONST_DECL:
2438 case TEMPLATE_PARM_INDEX:
2439 etype = TREE_TYPE (expr);
2440 break;
2442 default:
2443 break;
2446 if (etype && !type_uses_auto (etype))
2448 if (abi_warn_or_compat_version_crosses (5))
2449 G.need_abi_warning = 1;
2450 if (!abi_version_at_least (5))
2452 write_type (etype);
2453 return;
2458 write_char ('D');
2459 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2460 write_char ('t');
2461 else
2462 write_char ('T');
2463 ++cp_unevaluated_operand;
2464 write_expression (DECLTYPE_TYPE_EXPR (type));
2465 --cp_unevaluated_operand;
2466 write_char ('E');
2467 break;
2469 case NULLPTR_TYPE:
2470 write_string ("Dn");
2471 if (abi_version_at_least (7))
2472 ++is_builtin_type;
2473 if (abi_warn_or_compat_version_crosses (7))
2474 G.need_abi_warning = 1;
2475 break;
2477 case TYPEOF_TYPE:
2478 sorry ("mangling %<typeof%>, use %<decltype%> instead");
2479 break;
2481 case TRAIT_TYPE:
2482 error ("use of built-in trait %qT in function signature; "
2483 "use library traits instead", type);
2484 break;
2486 case LANG_TYPE:
2487 /* fall through. */
2489 default:
2490 gcc_unreachable ();
2495 /* Types other than builtin types are substitution candidates. */
2496 if (!is_builtin_type)
2497 add_substitution (type);
2500 /* qsort callback for sorting a vector of attribute entries. */
2502 static int
2503 attr_strcmp (const void *p1, const void *p2)
2505 tree a1 = *(const tree*)p1;
2506 tree a2 = *(const tree*)p2;
2508 const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1));
2509 const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2));
2511 return strcmp (as1->name, as2->name);
2514 /* Return true if we should mangle a type attribute with name NAME. */
2516 static bool
2517 mangle_type_attribute_p (tree name)
2519 const attribute_spec *as = lookup_attribute_spec (name);
2520 if (!as || !as->affects_type_identity)
2521 return false;
2523 /* Skip internal-only attributes, which are distinguished from others
2524 by having a space. At present, all internal-only attributes that
2525 affect type identity are target-specific and are handled by
2526 targetm.mangle_type instead.
2528 Another reason to do this is that a space isn't a valid identifier
2529 character for most file formats. */
2530 if (strchr (IDENTIFIER_POINTER (name), ' '))
2531 return false;
2533 /* The following attributes are mangled specially. */
2534 if (is_attribute_p ("transaction_safe", name))
2535 return false;
2536 if (is_attribute_p ("abi_tag", name))
2537 return false;
2539 return true;
2542 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2543 CV-qualifiers written for TYPE.
2545 <CV-qualifiers> ::= [r] [V] [K] */
2547 static int
2548 write_CV_qualifiers_for_type (const tree type)
2550 int num_qualifiers = 0;
2552 /* The order is specified by:
2554 "In cases where multiple order-insensitive qualifiers are
2555 present, they should be ordered 'K' (closest to the base type),
2556 'V', 'r', and 'U' (farthest from the base type) ..." */
2558 /* Mangle attributes that affect type identity as extended qualifiers.
2560 We don't do this with classes and enums because their attributes
2561 are part of their definitions, not something added on. */
2563 if (!OVERLOAD_TYPE_P (type))
2565 auto_vec<tree> vec;
2566 for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
2567 if (mangle_type_attribute_p (get_attribute_name (a)))
2568 vec.safe_push (a);
2569 if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
2570 G.need_abi_warning = true;
2571 if (abi_version_at_least (10))
2573 vec.qsort (attr_strcmp);
2574 while (!vec.is_empty())
2576 tree a = vec.pop();
2577 const attribute_spec *as
2578 = lookup_attribute_spec (get_attribute_name (a));
2580 write_char ('U');
2581 write_unsigned_number (strlen (as->name));
2582 write_string (as->name);
2583 if (TREE_VALUE (a))
2585 write_char ('I');
2586 for (tree args = TREE_VALUE (a); args;
2587 args = TREE_CHAIN (args))
2589 tree arg = TREE_VALUE (args);
2590 write_template_arg (arg);
2592 write_char ('E');
2595 ++num_qualifiers;
2600 /* Note that we do not use cp_type_quals below; given "const
2601 int[3]", the "const" is emitted with the "int", not with the
2602 array. */
2603 cp_cv_quals quals = TYPE_QUALS (type);
2605 if (quals & TYPE_QUAL_RESTRICT)
2607 write_char ('r');
2608 ++num_qualifiers;
2610 if (quals & TYPE_QUAL_VOLATILE)
2612 write_char ('V');
2613 ++num_qualifiers;
2615 if (quals & TYPE_QUAL_CONST)
2617 write_char ('K');
2618 ++num_qualifiers;
2621 return num_qualifiers;
2624 /* Non-terminal <builtin-type>.
2626 <builtin-type> ::= v # void
2627 ::= b # bool
2628 ::= w # wchar_t
2629 ::= c # char
2630 ::= a # signed char
2631 ::= h # unsigned char
2632 ::= s # short
2633 ::= t # unsigned short
2634 ::= i # int
2635 ::= j # unsigned int
2636 ::= l # long
2637 ::= m # unsigned long
2638 ::= x # long long, __int64
2639 ::= y # unsigned long long, __int64
2640 ::= n # __int128
2641 ::= o # unsigned __int128
2642 ::= f # float
2643 ::= d # double
2644 ::= e # long double, __float80
2645 ::= g # __float128 [not supported]
2646 ::= u <source-name> # vendor extended type */
2648 static void
2649 write_builtin_type (tree type)
2651 if (TYPE_CANONICAL (type))
2652 type = TYPE_CANONICAL (type);
2654 switch (TREE_CODE (type))
2656 case VOID_TYPE:
2657 write_char ('v');
2658 break;
2660 case BOOLEAN_TYPE:
2661 write_char ('b');
2662 break;
2664 case INTEGER_TYPE:
2665 /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
2666 isn't in integer_type_nodes. */
2667 if (type == wchar_type_node)
2668 write_char ('w');
2669 else if (type == char8_type_node)
2670 write_string ("Du");
2671 else if (type == char16_type_node)
2672 write_string ("Ds");
2673 else if (type == char32_type_node)
2674 write_string ("Di");
2675 else
2677 size_t itk;
2678 /* Assume TYPE is one of the shared integer type nodes. Find
2679 it in the array of these nodes. */
2680 iagain:
2681 for (itk = 0; itk < itk_none; ++itk)
2682 if (integer_types[itk] != NULL_TREE
2683 && integer_type_codes[itk] != '\0'
2684 && type == integer_types[itk])
2686 /* Print the corresponding single-letter code. */
2687 write_char (integer_type_codes[itk]);
2688 break;
2691 if (itk == itk_none)
2693 tree t = c_common_type_for_mode (TYPE_MODE (type),
2694 TYPE_UNSIGNED (type));
2695 if (type != t)
2697 type = t;
2698 goto iagain;
2701 if (TYPE_PRECISION (type) == 128)
2702 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2703 else
2705 /* Allow for cases where TYPE is not one of the shared
2706 integer type nodes and write a "vendor extended builtin
2707 type" with a name the form intN or uintN, respectively.
2708 Situations like this can happen if you have an
2709 __attribute__((__mode__(__SI__))) type and use exotic
2710 switches like '-mint8' on AVR. Of course, this is
2711 undefined by the C++ ABI (and '-mint8' is not even
2712 Standard C conforming), but when using such special
2713 options you're pretty much in nowhere land anyway. */
2714 const char *prefix;
2715 char prec[11]; /* up to ten digits for an unsigned */
2717 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2718 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2719 write_char ('u'); /* "vendor extended builtin type" */
2720 write_unsigned_number (strlen (prefix) + strlen (prec));
2721 write_string (prefix);
2722 write_string (prec);
2726 break;
2728 case REAL_TYPE:
2729 if (type == float_type_node)
2730 write_char ('f');
2731 else if (type == double_type_node)
2732 write_char ('d');
2733 else if (type == long_double_type_node)
2734 write_char ('e');
2735 else if (type == dfloat32_type_node || type == fallback_dfloat32_type)
2736 write_string ("Df");
2737 else if (type == dfloat64_type_node || type == fallback_dfloat64_type)
2738 write_string ("Dd");
2739 else if (type == dfloat128_type_node || type == fallback_dfloat128_type)
2740 write_string ("De");
2741 else if (type == float16_type_node)
2742 write_string ("DF16_");
2743 else if (type == float32_type_node)
2744 write_string ("DF32_");
2745 else if (type == float64_type_node)
2746 write_string ("DF64_");
2747 else if (type == float128_type_node)
2748 write_string ("DF128_");
2749 else if (type == float32x_type_node)
2750 write_string ("DF32x");
2751 else if (type == float64x_type_node)
2752 write_string ("DF64x");
2753 else if (type == float128x_type_node)
2754 write_string ("DF128x");
2755 else
2756 gcc_unreachable ();
2757 break;
2759 default:
2760 gcc_unreachable ();
2764 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2765 METHOD_TYPE. The return type is mangled before the parameter
2766 types.
2768 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2770 static void
2771 write_function_type (const tree type)
2773 MANGLE_TRACE_TREE ("function-type", type);
2775 /* For a pointer to member function, the function type may have
2776 cv-qualifiers, indicating the quals for the artificial 'this'
2777 parameter. */
2778 if (TREE_CODE (type) == METHOD_TYPE)
2780 /* The first parameter must be a POINTER_TYPE pointing to the
2781 `this' parameter. */
2782 tree this_type = class_of_this_parm (type);
2783 write_CV_qualifiers_for_type (this_type);
2786 write_exception_spec (TYPE_RAISES_EXCEPTIONS (type));
2788 if (tx_safe_fn_type_p (type))
2789 write_string ("Dx");
2791 write_char ('F');
2792 /* We don't track whether or not a type is `extern "C"'. Note that
2793 you can have an `extern "C"' function that does not have
2794 `extern "C"' type, and vice versa:
2796 extern "C" typedef void function_t();
2797 function_t f; // f has C++ linkage, but its type is
2798 // `extern "C"'
2800 typedef void function_t();
2801 extern "C" function_t f; // Vice versa.
2803 See [dcl.link]. */
2804 write_bare_function_type (type, /*include_return_type_p=*/1,
2805 /*decl=*/NULL);
2806 if (FUNCTION_REF_QUALIFIED (type))
2808 if (FUNCTION_RVALUE_QUALIFIED (type))
2809 write_char ('O');
2810 else
2811 write_char ('R');
2813 write_char ('E');
2816 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2817 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2818 is mangled before the parameter types. If non-NULL, DECL is
2819 FUNCTION_DECL for the function whose type is being emitted. */
2821 static void
2822 write_bare_function_type (const tree type, const int include_return_type_p,
2823 const tree decl)
2825 MANGLE_TRACE_TREE ("bare-function-type", type);
2827 /* Mangle the return type, if requested. */
2828 if (include_return_type_p)
2829 write_type (TREE_TYPE (type));
2831 /* Now mangle the types of the arguments. */
2832 ++G.parm_depth;
2833 write_method_parms (TYPE_ARG_TYPES (type),
2834 TREE_CODE (type) == METHOD_TYPE,
2835 decl);
2836 --G.parm_depth;
2839 /* Write the mangled representation of a method parameter list of
2840 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2841 considered a non-static method, and the this parameter is omitted.
2842 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2843 parameters are being emitted. */
2845 static void
2846 write_method_parms (tree parm_types, const int method_p, const tree decl)
2848 tree first_parm_type;
2849 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2851 /* Assume this parameter type list is variable-length. If it ends
2852 with a void type, then it's not. */
2853 int varargs_p = 1;
2855 /* If this is a member function, skip the first arg, which is the
2856 this pointer.
2857 "Member functions do not encode the type of their implicit this
2858 parameter."
2860 Similarly, there's no need to mangle artificial parameters, like
2861 the VTT parameters for constructors and destructors. */
2862 if (method_p)
2864 parm_types = TREE_CHAIN (parm_types);
2865 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2867 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2869 parm_types = TREE_CHAIN (parm_types);
2870 parm_decl = DECL_CHAIN (parm_decl);
2873 if (decl && ctor_omit_inherited_parms (decl))
2874 /* Bring back parameters omitted from an inherited ctor. */
2875 parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl));
2878 for (first_parm_type = parm_types;
2879 parm_types;
2880 parm_types = TREE_CHAIN (parm_types))
2882 tree parm = TREE_VALUE (parm_types);
2883 if (parm == void_type_node)
2885 /* "Empty parameter lists, whether declared as () or
2886 conventionally as (void), are encoded with a void parameter
2887 (v)." */
2888 if (parm_types == first_parm_type)
2889 write_type (parm);
2890 /* If the parm list is terminated with a void type, it's
2891 fixed-length. */
2892 varargs_p = 0;
2893 /* A void type better be the last one. */
2894 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2896 else
2897 write_type (parm);
2900 if (varargs_p)
2901 /* <builtin-type> ::= z # ellipsis */
2902 write_char ('z');
2905 /* <class-enum-type> ::= <name> */
2907 static void
2908 write_class_enum_type (const tree type)
2910 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2913 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2914 arguments.
2916 <template-args> ::= I <template-arg>* E */
2918 static void
2919 write_template_args (tree args)
2921 int i;
2922 int length = 0;
2924 MANGLE_TRACE_TREE ("template-args", args);
2926 write_char ('I');
2928 if (args)
2929 length = TREE_VEC_LENGTH (args);
2931 if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2933 /* We have nested template args. We want the innermost template
2934 argument list. */
2935 args = TREE_VEC_ELT (args, length - 1);
2936 length = TREE_VEC_LENGTH (args);
2938 for (i = 0; i < length; ++i)
2939 write_template_arg (TREE_VEC_ELT (args, i));
2941 write_char ('E');
2944 /* Write out the
2945 <unqualified-name>
2946 <unqualified-name> <template-args>
2947 part of SCOPE_REF or COMPONENT_REF mangling. */
2949 static void
2950 write_member_name (tree member)
2952 if (identifier_p (member))
2954 if (IDENTIFIER_ANY_OP_P (member))
2956 if (abi_version_at_least (11))
2957 write_string ("on");
2958 if (abi_warn_or_compat_version_crosses (11))
2959 G.need_abi_warning = 1;
2961 write_unqualified_id (member);
2963 else if (DECL_P (member))
2965 gcc_assert (!DECL_OVERLOADED_OPERATOR_P (member));
2966 write_unqualified_name (member);
2968 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2970 tree name = TREE_OPERAND (member, 0);
2971 name = OVL_FIRST (name);
2972 write_member_name (name);
2973 write_template_args (TREE_OPERAND (member, 1));
2975 else
2976 write_expression (member);
2979 /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for
2980 converting to BASE, or just the conversion of EXPR if BASE is null.
2982 "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base
2983 path Min(P) is defined as follows: let C_i be the last element for which the
2984 conversion to C_0 is unambiguous; if that element is C_n, the minimized path
2985 is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) ->
2986 C_0."
2988 We mangle the conversion to C_i if it's different from C_n. */
2990 static bool
2991 write_base_ref (tree expr, tree base = NULL_TREE)
2993 if (TREE_CODE (expr) != COMPONENT_REF)
2994 return false;
2996 tree field = TREE_OPERAND (expr, 1);
2998 if (TREE_CODE (field) != FIELD_DECL || !DECL_FIELD_IS_BASE (field))
2999 return false;
3001 tree object = TREE_OPERAND (expr, 0);
3003 tree binfo = NULL_TREE;
3004 if (base)
3006 tree cur = TREE_TYPE (object);
3007 binfo = lookup_base (cur, base, ba_unique, NULL, tf_none);
3009 else
3010 /* We're at the end of the base conversion chain, so it can't be
3011 ambiguous. */
3012 base = TREE_TYPE (field);
3014 if (binfo == error_mark_node)
3016 /* cur->base is ambiguous, so make the conversion to
3017 last explicit, expressed as a cast (last&)object. */
3018 tree last = TREE_TYPE (expr);
3019 write_string (OVL_OP_INFO (false, CAST_EXPR)->mangled_name);
3020 write_type (build_reference_type (last));
3021 write_expression (object);
3023 else if (write_base_ref (object, base))
3024 /* cur->base is unambiguous, but we had another base conversion
3025 underneath and wrote it out. */;
3026 else
3027 /* No more base conversions, just write out the object. */
3028 write_expression (object);
3030 return true;
3033 /* The number of elements spanned by a RANGE_EXPR. */
3035 unsigned HOST_WIDE_INT
3036 range_expr_nelts (tree expr)
3038 tree lo = TREE_OPERAND (expr, 0);
3039 tree hi = TREE_OPERAND (expr, 1);
3040 return tree_to_uhwi (hi) - tree_to_uhwi (lo) + 1;
3043 /* <expression> ::= <unary operator-name> <expression>
3044 ::= <binary operator-name> <expression> <expression>
3045 ::= <expr-primary>
3047 <expr-primary> ::= <template-param>
3048 ::= L <type> <value number> E # literal
3049 ::= L <mangled-name> E # external name
3050 ::= st <type> # sizeof
3051 ::= sr <type> <unqualified-name> # dependent name
3052 ::= sr <type> <unqualified-name> <template-args> */
3054 static void
3055 write_expression (tree expr)
3057 enum tree_code code = TREE_CODE (expr);
3059 if (TREE_CODE (expr) == TARGET_EXPR)
3061 expr = TARGET_EXPR_INITIAL (expr);
3062 code = TREE_CODE (expr);
3065 /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer
3066 argument is converted (via qualification conversions) to another type. */
3067 while (CONVERT_EXPR_CODE_P (code)
3068 || code == IMPLICIT_CONV_EXPR
3069 || location_wrapper_p (expr)
3070 /* Parentheses aren't mangled. */
3071 || code == PAREN_EXPR
3072 || code == NON_LVALUE_EXPR
3073 || (code == VIEW_CONVERT_EXPR
3074 && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
3076 expr = TREE_OPERAND (expr, 0);
3077 code = TREE_CODE (expr);
3080 if (code == BASELINK
3081 && (!type_unknown_p (expr)
3082 || !BASELINK_QUALIFIED_P (expr)))
3084 expr = BASELINK_FUNCTIONS (expr);
3085 code = TREE_CODE (expr);
3088 /* Handle pointers-to-members by making them look like expression
3089 nodes. */
3090 if (code == PTRMEM_CST)
3092 expr = build_nt (ADDR_EXPR,
3093 build_qualified_name (/*type=*/NULL_TREE,
3094 PTRMEM_CST_CLASS (expr),
3095 PTRMEM_CST_MEMBER (expr),
3096 /*template_p=*/false));
3097 code = TREE_CODE (expr);
3100 /* Handle template parameters. */
3101 if (code == TEMPLATE_TYPE_PARM
3102 || code == TEMPLATE_TEMPLATE_PARM
3103 || code == BOUND_TEMPLATE_TEMPLATE_PARM
3104 || code == TEMPLATE_PARM_INDEX)
3105 write_template_param (expr);
3106 /* Handle literals. */
3107 else if (TREE_CODE_CLASS (code) == tcc_constant
3108 || code == CONST_DECL)
3109 write_template_arg_literal (expr);
3110 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
3112 gcc_assert (id_equal (DECL_NAME (expr), "this"));
3113 write_string ("fpT");
3115 else if (code == PARM_DECL)
3117 /* A function parameter used in a late-specified return type. */
3118 int index = DECL_PARM_INDEX (expr);
3119 int level = DECL_PARM_LEVEL (expr);
3120 int delta = G.parm_depth - level + 1;
3121 gcc_assert (index >= 1);
3122 write_char ('f');
3123 if (delta != 0)
3125 if (abi_version_at_least (5))
3127 /* Let L be the number of function prototype scopes from the
3128 innermost one (in which the parameter reference occurs) up
3129 to (and including) the one containing the declaration of
3130 the referenced parameter. If the parameter declaration
3131 clause of the innermost function prototype scope has been
3132 completely seen, it is not counted (in that case -- which
3133 is perhaps the most common -- L can be zero). */
3134 write_char ('L');
3135 write_unsigned_number (delta - 1);
3137 if (abi_warn_or_compat_version_crosses (5))
3138 G.need_abi_warning = true;
3140 write_char ('p');
3141 write_compact_number (index - 1);
3143 else if (DECL_P (expr))
3145 write_char ('L');
3146 write_mangled_name (expr, false);
3147 write_char ('E');
3149 else if (TREE_CODE (expr) == SIZEOF_EXPR)
3151 tree op = TREE_OPERAND (expr, 0);
3153 if (PACK_EXPANSION_P (op))
3155 if (abi_warn_or_compat_version_crosses (11))
3156 G.need_abi_warning = true;
3157 if (abi_version_at_least (11))
3159 /* sZ rather than szDp. */
3160 write_string ("sZ");
3161 write_expression (PACK_EXPANSION_PATTERN (op));
3162 return;
3166 if (SIZEOF_EXPR_TYPE_P (expr))
3168 write_string ("st");
3169 write_type (TREE_TYPE (op));
3171 else if (ARGUMENT_PACK_P (op))
3173 tree args = ARGUMENT_PACK_ARGS (op);
3174 int length = TREE_VEC_LENGTH (args);
3175 if (abi_warn_or_compat_version_crosses (10))
3176 G.need_abi_warning = true;
3177 if (abi_version_at_least (10))
3179 /* sP <template-arg>* E # sizeof...(T), size of a captured
3180 template parameter pack from an alias template */
3181 write_string ("sP");
3182 for (int i = 0; i < length; ++i)
3183 write_template_arg (TREE_VEC_ELT (args, i));
3184 write_char ('E');
3186 else
3188 /* In GCC 5 we represented this sizeof wrong, with the effect
3189 that we mangled it as the last element of the pack. */
3190 tree arg = TREE_VEC_ELT (args, length-1);
3191 if (TYPE_P (op))
3193 write_string ("st");
3194 write_type (arg);
3196 else
3198 write_string ("sz");
3199 write_expression (arg);
3203 else if (TYPE_P (TREE_OPERAND (expr, 0)))
3205 write_string ("st");
3206 write_type (TREE_OPERAND (expr, 0));
3208 else
3209 goto normal_expr;
3211 else if (TREE_CODE (expr) == ALIGNOF_EXPR)
3213 if (!ALIGNOF_EXPR_STD_P (expr))
3215 if (abi_warn_or_compat_version_crosses (16))
3216 G.need_abi_warning = true;
3217 if (abi_version_at_least (16))
3219 /* We used to mangle __alignof__ like alignof. */
3220 write_string ("u11__alignof__");
3221 write_template_arg (TREE_OPERAND (expr, 0));
3222 write_char ('E');
3223 return;
3226 if (TYPE_P (TREE_OPERAND (expr, 0)))
3228 write_string ("at");
3229 write_type (TREE_OPERAND (expr, 0));
3231 else
3232 goto normal_expr;
3234 else if (code == SCOPE_REF
3235 || code == BASELINK)
3237 tree scope, member;
3238 if (code == SCOPE_REF)
3240 scope = TREE_OPERAND (expr, 0);
3241 member = TREE_OPERAND (expr, 1);
3242 if (BASELINK_P (member))
3243 member = BASELINK_FUNCTIONS (member);
3245 else
3247 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
3248 member = BASELINK_FUNCTIONS (expr);
3251 /* If the MEMBER is a real declaration, then the qualifying
3252 scope was not dependent. Ideally, we would not have a
3253 SCOPE_REF in those cases, but sometimes we do. If the second
3254 argument is a DECL, then the name must not have been
3255 dependent. */
3256 if (DECL_P (member))
3257 write_expression (member);
3258 else
3260 gcc_assert (code != BASELINK || BASELINK_QUALIFIED_P (expr));
3261 write_string ("sr");
3262 write_type (scope);
3263 write_member_name (member);
3266 else if (INDIRECT_REF_P (expr)
3267 && TREE_TYPE (TREE_OPERAND (expr, 0))
3268 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
3270 write_expression (TREE_OPERAND (expr, 0));
3272 else if (identifier_p (expr))
3274 /* An operator name appearing as a dependent name needs to be
3275 specially marked to disambiguate between a use of the operator
3276 name and a use of the operator in an expression. */
3277 if (IDENTIFIER_ANY_OP_P (expr))
3278 write_string ("on");
3279 write_unqualified_id (expr);
3281 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
3283 tree fn = TREE_OPERAND (expr, 0);
3284 fn = OVL_NAME (fn);
3285 if (IDENTIFIER_ANY_OP_P (fn))
3286 write_string ("on");
3287 write_unqualified_id (fn);
3288 write_template_args (TREE_OPERAND (expr, 1));
3290 else if (TREE_CODE (expr) == MODOP_EXPR)
3292 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
3293 const char *name = OVL_OP_INFO (true, subop)->mangled_name;
3295 write_string (name);
3296 write_expression (TREE_OPERAND (expr, 0));
3297 write_expression (TREE_OPERAND (expr, 2));
3299 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
3301 /* ::= [gs] nw <expression>* _ <type> E
3302 ::= [gs] nw <expression>* _ <type> <initializer>
3303 ::= [gs] na <expression>* _ <type> E
3304 ::= [gs] na <expression>* _ <type> <initializer>
3305 <initializer> ::= pi <expression>* E */
3306 tree placement = TREE_OPERAND (expr, 0);
3307 tree type = TREE_OPERAND (expr, 1);
3308 tree nelts = TREE_OPERAND (expr, 2);
3309 tree init = TREE_OPERAND (expr, 3);
3310 tree t;
3312 gcc_assert (code == NEW_EXPR);
3313 if (TREE_OPERAND (expr, 2))
3314 code = VEC_NEW_EXPR;
3316 if (NEW_EXPR_USE_GLOBAL (expr))
3317 write_string ("gs");
3319 write_string (OVL_OP_INFO (false, code)->mangled_name);
3321 for (t = placement; t; t = TREE_CHAIN (t))
3322 write_expression (TREE_VALUE (t));
3324 write_char ('_');
3326 if (nelts)
3328 tree domain;
3329 ++processing_template_decl;
3330 domain = compute_array_index_type (NULL_TREE, nelts,
3331 tf_warning_or_error);
3332 type = build_cplus_array_type (type, domain);
3333 --processing_template_decl;
3335 write_type (type);
3337 if (init && TREE_CODE (init) == TREE_LIST
3338 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
3339 write_expression (TREE_VALUE (init));
3340 else
3342 if (init)
3343 write_string ("pi");
3344 if (init && init != void_node)
3345 for (t = init; t; t = TREE_CHAIN (t))
3346 write_expression (TREE_VALUE (t));
3347 write_char ('E');
3350 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
3352 gcc_assert (code == DELETE_EXPR);
3353 if (DELETE_EXPR_USE_VEC (expr))
3354 code = VEC_DELETE_EXPR;
3356 if (DELETE_EXPR_USE_GLOBAL (expr))
3357 write_string ("gs");
3359 write_string (OVL_OP_INFO (false, code)->mangled_name);
3361 write_expression (TREE_OPERAND (expr, 0));
3363 else if (code == THROW_EXPR)
3365 tree op = TREE_OPERAND (expr, 0);
3366 if (op)
3368 write_string ("tw");
3369 write_expression (op);
3371 else
3372 write_string ("tr");
3374 else if (code == CONSTRUCTOR)
3376 bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr);
3377 tree etype = TREE_TYPE (expr);
3379 if (braced_init)
3380 write_string ("il");
3381 else
3383 write_string ("tl");
3384 write_type (etype);
3387 /* If this is an undigested initializer, mangle it as written.
3388 COMPOUND_LITERAL_P doesn't actually distinguish between digested and
3389 undigested braced casts, but it should work to use it to distinguish
3390 between braced casts in a template signature (undigested) and template
3391 parm object values (digested), and all CONSTRUCTORS that get here
3392 should be one of those two cases. */
3393 bool undigested = braced_init || COMPOUND_LITERAL_P (expr);
3394 if (undigested || !zero_init_expr_p (expr))
3396 /* Convert braced initializer lists to STRING_CSTs so that
3397 A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
3398 still using the latter mangling for strings that
3399 originated as braced initializer lists. */
3400 expr = braced_lists_to_strings (etype, expr);
3402 if (TREE_CODE (expr) == CONSTRUCTOR)
3404 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
3405 unsigned last_nonzero = UINT_MAX;
3406 constructor_elt *ce;
3408 if (!undigested)
3409 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3410 if ((TREE_CODE (etype) == UNION_TYPE
3411 && ce->index != first_field (etype))
3412 || !zero_init_expr_p (ce->value))
3413 last_nonzero = i;
3415 if (undigested || last_nonzero != UINT_MAX)
3416 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3418 if (i > last_nonzero)
3419 break;
3420 if (!undigested && TREE_CODE (etype) == UNION_TYPE)
3422 /* Express the active member as a designator. */
3423 write_string ("di");
3424 write_unqualified_name (ce->index);
3426 unsigned reps = 1;
3427 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3428 reps = range_expr_nelts (ce->index);
3429 for (unsigned j = 0; j < reps; ++j)
3430 write_expression (ce->value);
3433 else
3435 gcc_assert (TREE_CODE (expr) == STRING_CST);
3436 write_expression (expr);
3439 write_char ('E');
3441 else if (code == LAMBDA_EXPR)
3443 /* [temp.over.link] Two lambda-expressions are never considered
3444 equivalent.
3446 So just use the closure type mangling. */
3447 write_string ("tl");
3448 write_type (LAMBDA_EXPR_CLOSURE (expr));
3449 write_char ('E');
3451 else if (dependent_name (expr))
3453 tree name = dependent_name (expr);
3454 if (IDENTIFIER_ANY_OP_P (name))
3456 if (abi_version_at_least (16))
3457 write_string ("on");
3458 if (abi_warn_or_compat_version_crosses (16))
3459 G.need_abi_warning = 1;
3461 write_unqualified_id (name);
3463 else
3465 normal_expr:
3466 int i, len;
3467 const char *name;
3469 /* When we bind a variable or function to a non-type template
3470 argument with reference type, we create an ADDR_EXPR to show
3471 the fact that the entity's address has been taken. But, we
3472 don't actually want to output a mangling code for the `&'. */
3473 if (TREE_CODE (expr) == ADDR_EXPR
3474 && TREE_TYPE (expr)
3475 && TYPE_REF_P (TREE_TYPE (expr)))
3477 expr = TREE_OPERAND (expr, 0);
3478 if (DECL_P (expr))
3480 write_expression (expr);
3481 return;
3484 code = TREE_CODE (expr);
3487 if (code == COMPONENT_REF)
3489 tree ob = TREE_OPERAND (expr, 0);
3491 if (TREE_CODE (ob) == ARROW_EXPR)
3493 write_string (OVL_OP_INFO (false, code)->mangled_name);
3494 ob = TREE_OPERAND (ob, 0);
3495 write_expression (ob);
3497 else if (write_base_ref (expr))
3498 return;
3499 else if (!is_dummy_object (ob))
3501 write_string ("dt");
3502 write_expression (ob);
3504 /* else, for a non-static data member with no associated object (in
3505 unevaluated context), use the unresolved-name mangling. */
3507 write_member_name (TREE_OPERAND (expr, 1));
3508 return;
3511 /* If it wasn't any of those, recursively expand the expression. */
3512 name = OVL_OP_INFO (false, code)->mangled_name;
3514 /* We used to mangle const_cast and static_cast like a C cast. */
3515 if (code == CONST_CAST_EXPR
3516 || code == STATIC_CAST_EXPR)
3518 if (abi_warn_or_compat_version_crosses (6))
3519 G.need_abi_warning = 1;
3520 if (!abi_version_at_least (6))
3521 name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name;
3524 if (name == NULL)
3526 switch (code)
3528 case TRAIT_EXPR:
3529 error ("use of built-in trait %qE in function signature; "
3530 "use library traits instead", expr);
3531 break;
3533 default:
3534 sorry ("mangling %C", code);
3535 break;
3537 return;
3539 else
3540 write_string (name);
3542 switch (code)
3544 case CALL_EXPR:
3546 tree fn = CALL_EXPR_FN (expr);
3548 if (TREE_CODE (fn) == ADDR_EXPR)
3549 fn = TREE_OPERAND (fn, 0);
3551 /* Mangle a dependent name as the name, not whatever happens to
3552 be the first function in the overload set. */
3553 if (OVL_P (fn)
3554 && type_dependent_expression_p_push (expr))
3555 fn = OVL_NAME (fn);
3557 write_expression (fn);
3560 for (i = 0; i < call_expr_nargs (expr); ++i)
3561 write_expression (CALL_EXPR_ARG (expr, i));
3562 write_char ('E');
3563 break;
3565 case CAST_EXPR:
3566 write_type (TREE_TYPE (expr));
3567 if (list_length (TREE_OPERAND (expr, 0)) == 1)
3568 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
3569 else
3571 tree args = TREE_OPERAND (expr, 0);
3572 write_char ('_');
3573 for (; args; args = TREE_CHAIN (args))
3574 write_expression (TREE_VALUE (args));
3575 write_char ('E');
3577 break;
3579 case DYNAMIC_CAST_EXPR:
3580 case REINTERPRET_CAST_EXPR:
3581 case STATIC_CAST_EXPR:
3582 case CONST_CAST_EXPR:
3583 write_type (TREE_TYPE (expr));
3584 write_expression (TREE_OPERAND (expr, 0));
3585 break;
3587 case PREINCREMENT_EXPR:
3588 case PREDECREMENT_EXPR:
3589 if (abi_version_at_least (6))
3590 write_char ('_');
3591 if (abi_warn_or_compat_version_crosses (6))
3592 G.need_abi_warning = 1;
3593 /* Fall through. */
3595 default:
3596 /* In the middle-end, some expressions have more operands than
3597 they do in templates (and mangling). */
3598 len = cp_tree_operand_length (expr);
3600 for (i = 0; i < len; ++i)
3602 tree operand = TREE_OPERAND (expr, i);
3603 /* As a GNU extension, the middle operand of a
3604 conditional may be omitted. Since expression
3605 manglings are supposed to represent the input token
3606 stream, there's no good way to mangle such an
3607 expression without extending the C++ ABI. */
3608 if (code == COND_EXPR && i == 1 && !operand)
3610 error ("omitted middle operand to %<?:%> operand "
3611 "cannot be mangled");
3612 continue;
3614 else if (FOLD_EXPR_P (expr))
3616 /* The first 'operand' of a fold-expression is the operator
3617 that it folds over. */
3618 if (i == 0)
3620 int fcode = TREE_INT_CST_LOW (operand);
3621 write_string (OVL_OP_INFO (false, fcode)->mangled_name);
3622 continue;
3624 else if (code == BINARY_LEFT_FOLD_EXPR)
3626 /* The order of operands of the binary left and right
3627 folds is the same, but we want to mangle them in
3628 lexical order, i.e. non-pack first. */
3629 if (i == 1)
3630 operand = FOLD_EXPR_INIT (expr);
3631 else
3632 operand = FOLD_EXPR_PACK (expr);
3634 if (PACK_EXPANSION_P (operand))
3635 operand = PACK_EXPANSION_PATTERN (operand);
3637 write_expression (operand);
3643 /* Literal subcase of non-terminal <template-arg>.
3645 "Literal arguments, e.g. "A<42L>", are encoded with their type
3646 and value. Negative integer values are preceded with "n"; for
3647 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3648 encoded as 0, true as 1." */
3650 static void
3651 write_template_arg_literal (const tree value)
3653 if (TREE_CODE (value) == STRING_CST)
3654 /* Temporarily mangle strings as braced initializer lists. */
3655 write_string ("tl");
3656 else
3657 write_char ('L');
3659 tree valtype = TREE_TYPE (value);
3660 write_type (valtype);
3662 /* Write a null member pointer value as (type)0, regardless of its
3663 real representation. */
3664 if (null_member_pointer_value_p (value))
3665 write_integer_cst (integer_zero_node);
3666 else
3667 switch (TREE_CODE (value))
3669 case CONST_DECL:
3670 write_integer_cst (DECL_INITIAL (value));
3671 break;
3673 case INTEGER_CST:
3674 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3675 || integer_zerop (value) || integer_onep (value));
3676 if (!(abi_version_at_least (14)
3677 && NULLPTR_TYPE_P (TREE_TYPE (value))))
3678 write_integer_cst (value);
3679 break;
3681 case REAL_CST:
3682 write_real_cst (value);
3683 break;
3685 case COMPLEX_CST:
3686 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3687 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3689 write_integer_cst (TREE_REALPART (value));
3690 write_char ('_');
3691 write_integer_cst (TREE_IMAGPART (value));
3693 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3694 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3696 write_real_cst (TREE_REALPART (value));
3697 write_char ('_');
3698 write_real_cst (TREE_IMAGPART (value));
3700 else
3701 gcc_unreachable ();
3702 break;
3704 case STRING_CST:
3706 /* Mangle strings the same as braced initializer lists. */
3707 unsigned n = TREE_STRING_LENGTH (value);
3708 const char *str = TREE_STRING_POINTER (value);
3710 /* Count the number of trailing nuls and subtract them from
3711 STRSIZE because they don't need to be mangled. */
3712 for (const char *p = str + n - 1; ; --p)
3714 if (*p || p == str)
3716 n -= str + n - !!*p - p;
3717 break;
3720 tree eltype = TREE_TYPE (valtype);
3721 for (const char *p = str; n--; ++p)
3723 write_char ('L');
3724 write_type (eltype);
3725 write_unsigned_number (*(const unsigned char*)p);
3726 write_string ("E");
3728 break;
3731 default:
3732 gcc_unreachable ();
3735 write_char ('E');
3738 /* Non-terminal <template-arg>.
3740 <template-arg> ::= <type> # type
3741 ::= L <type> </value/ number> E # literal
3742 ::= LZ <name> E # external name
3743 ::= X <expression> E # expression */
3745 static void
3746 write_template_arg (tree node)
3748 enum tree_code code = TREE_CODE (node);
3750 MANGLE_TRACE_TREE ("template-arg", node);
3752 /* A template template parameter's argument list contains TREE_LIST
3753 nodes of which the value field is the actual argument. */
3754 if (code == TREE_LIST)
3756 node = TREE_VALUE (node);
3757 /* If it's a decl, deal with its type instead. */
3758 if (DECL_P (node))
3760 node = TREE_TYPE (node);
3761 code = TREE_CODE (node);
3765 if (TREE_CODE (node) == VAR_DECL && DECL_NTTP_OBJECT_P (node))
3766 /* We want to mangle the argument, not the var we stored it in. */
3767 node = tparm_object_argument (node);
3769 /* Strip a conversion added by convert_nontype_argument. */
3770 if (TREE_CODE (node) == IMPLICIT_CONV_EXPR)
3771 node = TREE_OPERAND (node, 0);
3772 if (REFERENCE_REF_P (node))
3773 node = TREE_OPERAND (node, 0);
3774 if (TREE_CODE (node) == NOP_EXPR
3775 && TYPE_REF_P (TREE_TYPE (node)))
3777 /* Template parameters can be of reference type. To maintain
3778 internal consistency, such arguments use a conversion from
3779 address of object to reference type. */
3780 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3781 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3784 if (TREE_CODE (node) == BASELINK
3785 && !type_unknown_p (node))
3787 if (abi_version_at_least (6))
3788 node = BASELINK_FUNCTIONS (node);
3789 if (abi_warn_or_compat_version_crosses (6))
3790 /* We wrongly wrapped a class-scope function in X/E. */
3791 G.need_abi_warning = 1;
3794 if (ARGUMENT_PACK_P (node))
3796 /* Expand the template argument pack. */
3797 tree args = ARGUMENT_PACK_ARGS (node);
3798 int i, length = TREE_VEC_LENGTH (args);
3799 if (abi_version_at_least (6))
3800 write_char ('J');
3801 else
3802 write_char ('I');
3803 if (abi_warn_or_compat_version_crosses (6))
3804 G.need_abi_warning = 1;
3805 for (i = 0; i < length; ++i)
3806 write_template_arg (TREE_VEC_ELT (args, i));
3807 write_char ('E');
3809 else if (TYPE_P (node))
3810 write_type (node);
3811 else if (code == TEMPLATE_DECL)
3812 /* A template appearing as a template arg is a template template arg. */
3813 write_template_template_arg (node);
3814 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3815 || code == CONST_DECL
3816 || null_member_pointer_value_p (node))
3817 write_template_arg_literal (node);
3818 else if (DECL_P (node))
3820 write_char ('L');
3821 /* Until ABI version 3, the underscore before the mangled name
3822 was incorrectly omitted. */
3823 if (!abi_version_at_least (3))
3824 write_char ('Z');
3825 else
3826 write_string ("_Z");
3827 if (abi_warn_or_compat_version_crosses (3))
3828 G.need_abi_warning = 1;
3829 write_encoding (node);
3830 write_char ('E');
3832 else
3834 /* Template arguments may be expressions. */
3835 write_char ('X');
3836 write_expression (node);
3837 write_char ('E');
3841 /* <template-template-arg>
3842 ::= <name>
3843 ::= <substitution> */
3845 static void
3846 write_template_template_arg (const tree decl)
3848 MANGLE_TRACE_TREE ("template-template-arg", decl);
3850 if (find_substitution (decl))
3851 return;
3852 write_name (decl, /*ignore_local_scope=*/0);
3853 add_substitution (decl);
3857 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3859 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3860 ::= A <expression> _ </element/ type>
3862 "Array types encode the dimension (number of elements) and the
3863 element type. For variable length arrays, the dimension (but not
3864 the '_' separator) is omitted."
3865 Note that for flexible array members, like for other arrays of
3866 unspecified size, the dimension is also omitted. */
3868 static void
3869 write_array_type (const tree type)
3871 write_char ('A');
3872 if (TYPE_DOMAIN (type))
3874 tree index_type;
3876 index_type = TYPE_DOMAIN (type);
3877 /* The INDEX_TYPE gives the upper and lower bounds of the array.
3878 It's null for flexible array members which have no upper bound
3879 (this is a change from GCC 5 and prior where such members were
3880 incorrectly mangled as zero-length arrays). */
3881 if (tree max = TYPE_MAX_VALUE (index_type))
3883 if (TREE_CODE (max) == INTEGER_CST)
3885 /* The ABI specifies that we should mangle the number of
3886 elements in the array, not the largest allowed index. */
3887 offset_int wmax = wi::to_offset (max) + 1;
3888 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3889 number of elements as zero. */
3890 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3891 gcc_assert (wi::fits_uhwi_p (wmax));
3892 write_unsigned_number (wmax.to_uhwi ());
3894 else
3896 max = TREE_OPERAND (max, 0);
3897 write_expression (max);
3901 write_char ('_');
3902 write_type (TREE_TYPE (type));
3905 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3906 variables. TYPE is a pointer-to-member POINTER_TYPE.
3908 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3910 static void
3911 write_pointer_to_member_type (const tree type)
3913 write_char ('M');
3914 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3915 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3918 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3919 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3920 TEMPLATE_PARM_INDEX.
3922 <template-param> ::= T </parameter/ number> _ */
3924 static void
3925 write_template_param (const tree parm)
3927 int parm_index;
3929 MANGLE_TRACE_TREE ("template-parm", parm);
3931 switch (TREE_CODE (parm))
3933 case TEMPLATE_TYPE_PARM:
3934 case TEMPLATE_TEMPLATE_PARM:
3935 case BOUND_TEMPLATE_TEMPLATE_PARM:
3936 parm_index = TEMPLATE_TYPE_IDX (parm);
3937 break;
3939 case TEMPLATE_PARM_INDEX:
3940 parm_index = TEMPLATE_PARM_IDX (parm);
3941 break;
3943 default:
3944 gcc_unreachable ();
3947 write_char ('T');
3948 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3949 earliest template param denoted by `_'. */
3950 write_compact_number (parm_index);
3953 /* <template-template-param>
3954 ::= <template-param>
3955 ::= <substitution> */
3957 static void
3958 write_template_template_param (const tree parm)
3960 tree templ = NULL_TREE;
3962 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3963 template template parameter. The substitution candidate here is
3964 only the template. */
3965 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3967 templ
3968 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3969 if (find_substitution (templ))
3970 return;
3973 /* <template-param> encodes only the template parameter position,
3974 not its template arguments, which is fine here. */
3975 write_template_param (parm);
3976 if (templ)
3977 add_substitution (templ);
3980 /* Non-terminal <substitution>.
3982 <substitution> ::= S <seq-id> _
3983 ::= S_ */
3985 static void
3986 write_substitution (const int seq_id)
3988 MANGLE_TRACE ("substitution", "");
3990 write_char ('S');
3991 if (seq_id > 0)
3992 write_number (seq_id - 1, /*unsigned=*/1, 36);
3993 write_char ('_');
3996 /* Start mangling ENTITY. */
3998 static inline void
3999 start_mangling (const tree entity)
4001 G.entity = entity;
4002 G.need_abi_warning = false;
4003 G.need_cxx17_warning = false;
4004 G.mod = false;
4005 obstack_free (&name_obstack, name_base);
4006 mangle_obstack = &name_obstack;
4007 name_base = obstack_alloc (&name_obstack, 0);
4010 /* Done with mangling. Release the data. */
4012 static void
4013 finish_mangling_internal (void)
4015 /* Clear all the substitutions. */
4016 vec_safe_truncate (G.substitutions, 0);
4018 if (G.mod)
4019 mangle_module_fini ();
4021 /* Null-terminate the string. */
4022 write_char ('\0');
4026 /* Like finish_mangling_internal, but return the mangled string. */
4028 static inline const char *
4029 finish_mangling (void)
4031 finish_mangling_internal ();
4032 return (const char *) obstack_finish (mangle_obstack);
4035 /* Like finish_mangling_internal, but return an identifier. */
4037 static tree
4038 finish_mangling_get_identifier (void)
4040 finish_mangling_internal ();
4041 /* Don't obstack_finish here, and the next start_mangling will
4042 remove the identifier. */
4043 return get_identifier ((const char *) obstack_base (mangle_obstack));
4046 /* Initialize data structures for mangling. */
4048 void
4049 init_mangle (void)
4051 gcc_obstack_init (&name_obstack);
4052 name_base = obstack_alloc (&name_obstack, 0);
4053 vec_alloc (G.substitutions, 0);
4055 /* Cache these identifiers for quick comparison when checking for
4056 standard substitutions. */
4057 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
4058 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
4059 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
4060 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
4061 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
4062 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
4065 /* Generate a mangling for MODULE's global initializer fn. */
4067 tree
4068 mangle_module_global_init (int module)
4070 start_mangling (NULL_TREE);
4072 write_string ("_ZGI");
4073 write_module (module, true);
4075 return finish_mangling_get_identifier ();
4078 /* Generate the mangled name of DECL. */
4080 static tree
4081 mangle_decl_string (const tree decl)
4083 tree result;
4084 tree saved_fn = NULL_TREE;
4085 bool template_p = false;
4087 /* We shouldn't be trying to mangle an uninstantiated template. */
4088 gcc_assert (!type_dependent_expression_p (decl));
4090 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4092 struct tinst_level *tl = current_instantiation ();
4093 if ((!tl || tl->maybe_get_node () != decl)
4094 && push_tinst_level (decl))
4096 template_p = true;
4097 saved_fn = current_function_decl;
4098 current_function_decl = NULL_TREE;
4101 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
4103 start_mangling (decl);
4105 if (TREE_CODE (decl) == TYPE_DECL)
4106 write_type (TREE_TYPE (decl));
4107 else
4108 write_mangled_name (decl, true);
4110 result = finish_mangling_get_identifier ();
4111 if (DEBUG_MANGLE)
4112 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
4113 IDENTIFIER_POINTER (result));
4115 if (template_p)
4117 pop_tinst_level ();
4118 current_function_decl = saved_fn;
4121 return result;
4124 /* Return an identifier for the external mangled name of DECL. */
4126 static tree
4127 get_mangled_id (tree decl)
4129 tree id = mangle_decl_string (decl);
4130 return targetm.mangle_decl_assembler_name (decl, id);
4133 /* Create an identifier for the external mangled name of DECL. */
4135 void
4136 mangle_decl (const tree decl)
4138 tree id;
4139 bool dep;
4141 /* Don't bother mangling uninstantiated templates. */
4142 ++processing_template_decl;
4143 if (TREE_CODE (decl) == TYPE_DECL)
4144 dep = dependent_type_p (TREE_TYPE (decl));
4145 else
4146 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4147 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
4148 --processing_template_decl;
4149 if (dep)
4150 return;
4152 /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
4153 It is not needed to assign names to anonymous namespace, but we use the
4154 "<anon>" marker to be able to tell if type is C++ ODR type or type
4155 produced by other language. */
4156 if (TREE_CODE (decl) == TYPE_DECL
4157 && TYPE_STUB_DECL (TREE_TYPE (decl))
4158 && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
4159 id = get_identifier ("<anon>");
4160 else
4162 gcc_assert (TREE_CODE (decl) != TYPE_DECL
4163 || !no_linkage_check (TREE_TYPE (decl), true));
4164 if (abi_version_at_least (10))
4165 if (tree fn = decl_function_context (decl))
4166 maybe_check_abi_tags (fn, decl);
4167 id = get_mangled_id (decl);
4169 SET_DECL_ASSEMBLER_NAME (decl, id);
4171 if (G.need_cxx17_warning
4172 && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl)))
4173 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type,
4174 "mangled name for %qD will change in C++17 because the "
4175 "exception specification is part of a function type",
4176 decl);
4178 if (id != DECL_NAME (decl)
4179 /* Don't do this for a fake symbol we aren't going to emit anyway. */
4180 && TREE_CODE (decl) != TYPE_DECL
4181 && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
4183 int save_ver = flag_abi_version;
4184 tree id2 = NULL_TREE;
4186 if (!DECL_REALLY_EXTERN (decl))
4188 record_mangling (decl, G.need_abi_warning);
4190 if (!G.need_abi_warning)
4191 return;
4193 flag_abi_version = flag_abi_compat_version;
4194 id2 = mangle_decl_string (decl);
4195 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4196 flag_abi_version = save_ver;
4198 if (id2 != id)
4199 note_mangling_alias (decl, id2);
4202 if (warn_abi)
4204 const char fabi_version[] = "-fabi-version";
4206 if (flag_abi_compat_version != warn_abi_version
4207 || id2 == NULL_TREE)
4209 flag_abi_version = warn_abi_version;
4210 id2 = mangle_decl_string (decl);
4211 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4213 flag_abi_version = save_ver;
4215 if (id2 == id)
4216 /* OK. */;
4217 else if (warn_abi_version != 0
4218 && abi_version_at_least (warn_abi_version))
4219 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4220 "the mangled name of %qD changed between "
4221 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4222 G.entity, fabi_version, warn_abi_version, id2,
4223 fabi_version, save_ver, id);
4224 else
4225 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4226 "the mangled name of %qD changes between "
4227 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4228 G.entity, fabi_version, save_ver, id,
4229 fabi_version, warn_abi_version, id2);
4232 flag_abi_version = save_ver;
4236 /* Generate the mangled representation of TYPE. */
4238 const char *
4239 mangle_type_string (const tree type)
4241 const char *result;
4243 start_mangling (type);
4244 write_type (type);
4245 result = finish_mangling ();
4246 if (DEBUG_MANGLE)
4247 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
4248 return result;
4251 /* Create an identifier for the mangled name of a special component
4252 for belonging to TYPE. CODE is the ABI-specified code for this
4253 component. */
4255 static tree
4256 mangle_special_for_type (const tree type, const char *code)
4258 tree result;
4260 /* We don't have an actual decl here for the special component, so
4261 we can't just process the <encoded-name>. Instead, fake it. */
4262 start_mangling (type);
4264 /* Start the mangling. */
4265 write_string ("_Z");
4266 write_string (code);
4268 /* Add the type. */
4269 write_type (type);
4270 result = finish_mangling_get_identifier ();
4272 if (DEBUG_MANGLE)
4273 fprintf (stderr, "mangle_special_for_type = %s\n\n",
4274 IDENTIFIER_POINTER (result));
4276 return result;
4279 /* Create an identifier for the mangled representation of the typeinfo
4280 structure for TYPE. */
4282 tree
4283 mangle_typeinfo_for_type (const tree type)
4285 return mangle_special_for_type (type, "TI");
4288 /* Create an identifier for the mangled name of the NTBS containing
4289 the mangled name of TYPE. */
4291 tree
4292 mangle_typeinfo_string_for_type (const tree type)
4294 return mangle_special_for_type (type, "TS");
4297 /* Create an identifier for the mangled name of the vtable for TYPE. */
4299 tree
4300 mangle_vtbl_for_type (const tree type)
4302 return mangle_special_for_type (type, "TV");
4305 /* Returns an identifier for the mangled name of the VTT for TYPE. */
4307 tree
4308 mangle_vtt_for_type (const tree type)
4310 return mangle_special_for_type (type, "TT");
4313 /* Returns an identifier for the mangled name of the decomposition
4314 artificial variable DECL. DECLS is the vector of the VAR_DECLs
4315 for the identifier-list. */
4317 tree
4318 mangle_decomp (const tree decl, vec<tree> &decls)
4320 gcc_assert (!type_dependent_expression_p (decl));
4322 location_t saved_loc = input_location;
4323 input_location = DECL_SOURCE_LOCATION (decl);
4325 start_mangling (decl);
4326 write_string ("_Z");
4328 tree context = decl_mangling_context (decl);
4329 gcc_assert (context != NULL_TREE);
4331 bool nested = false;
4332 if (DECL_NAMESPACE_STD_P (context))
4333 write_string ("St");
4334 else if (context != global_namespace)
4336 nested = true;
4337 write_char ('N');
4338 write_prefix (decl_mangling_context (decl));
4341 write_string ("DC");
4342 unsigned int i;
4343 tree d;
4344 FOR_EACH_VEC_ELT (decls, i, d)
4345 write_unqualified_name (d);
4346 write_char ('E');
4348 if (nested)
4349 write_char ('E');
4351 tree id = finish_mangling_get_identifier ();
4352 if (DEBUG_MANGLE)
4353 fprintf (stderr, "mangle_decomp = '%s'\n\n",
4354 IDENTIFIER_POINTER (id));
4356 input_location = saved_loc;
4357 return id;
4360 /* Return an identifier for a construction vtable group. TYPE is
4361 the most derived class in the hierarchy; BINFO is the base
4362 subobject for which this construction vtable group will be used.
4364 This mangling isn't part of the ABI specification; in the ABI
4365 specification, the vtable group is dumped in the same COMDAT as the
4366 main vtable, and is referenced only from that vtable, so it doesn't
4367 need an external name. For binary formats without COMDAT sections,
4368 though, we need external names for the vtable groups.
4370 We use the production
4372 <special-name> ::= CT <type> <offset number> _ <base type> */
4374 tree
4375 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
4377 tree result;
4379 start_mangling (type);
4381 write_string ("_Z");
4382 write_string ("TC");
4383 write_type (type);
4384 write_integer_cst (BINFO_OFFSET (binfo));
4385 write_char ('_');
4386 write_type (BINFO_TYPE (binfo));
4388 result = finish_mangling_get_identifier ();
4389 if (DEBUG_MANGLE)
4390 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
4391 IDENTIFIER_POINTER (result));
4392 return result;
4395 /* Mangle a this pointer or result pointer adjustment.
4397 <call-offset> ::= h <fixed offset number> _
4398 ::= v <fixed offset number> _ <virtual offset number> _ */
4400 static void
4401 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
4403 write_char (virtual_offset ? 'v' : 'h');
4405 /* For either flavor, write the fixed offset. */
4406 write_integer_cst (fixed_offset);
4407 write_char ('_');
4409 /* For a virtual thunk, add the virtual offset. */
4410 if (virtual_offset)
4412 write_integer_cst (virtual_offset);
4413 write_char ('_');
4417 /* Return an identifier for the mangled name of a this-adjusting or
4418 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
4419 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
4420 is a virtual thunk, and it is the vtbl offset in
4421 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
4422 zero for a covariant thunk. Note, that FN_DECL might be a covariant
4423 thunk itself. A covariant thunk name always includes the adjustment
4424 for the this pointer, even if there is none.
4426 <special-name> ::= T <call-offset> <base encoding>
4427 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
4428 <base encoding> */
4430 tree
4431 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
4432 tree virtual_offset, tree thunk)
4434 tree result;
4436 if (abi_version_at_least (11))
4437 maybe_check_abi_tags (fn_decl, thunk, 11);
4439 start_mangling (fn_decl);
4441 write_string ("_Z");
4442 write_char ('T');
4444 if (!this_adjusting)
4446 /* Covariant thunk with no this adjustment */
4447 write_char ('c');
4448 mangle_call_offset (integer_zero_node, NULL_TREE);
4449 mangle_call_offset (fixed_offset, virtual_offset);
4451 else if (!DECL_THUNK_P (fn_decl))
4452 /* Plain this adjusting thunk. */
4453 mangle_call_offset (fixed_offset, virtual_offset);
4454 else
4456 /* This adjusting thunk to covariant thunk. */
4457 write_char ('c');
4458 mangle_call_offset (fixed_offset, virtual_offset);
4459 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
4460 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
4461 if (virtual_offset)
4462 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
4463 mangle_call_offset (fixed_offset, virtual_offset);
4464 fn_decl = THUNK_TARGET (fn_decl);
4467 /* Scoped name. */
4468 write_encoding (fn_decl);
4470 result = finish_mangling_get_identifier ();
4471 if (DEBUG_MANGLE)
4472 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
4473 return result;
4476 /* Handle ABI backwards compatibility for past bugs where we didn't call
4477 check_abi_tags in places where it's needed: call check_abi_tags and warn if
4478 it makes a difference. If FOR_DECL is non-null, it's the declaration
4479 that we're actually trying to mangle; if it's null, we're mangling the
4480 guard variable for T. */
4482 static void
4483 maybe_check_abi_tags (tree t, tree for_decl, int ver)
4485 if (DECL_ASSEMBLER_NAME_SET_P (t))
4486 return;
4488 tree oldtags = get_abi_tags (t);
4490 mangle_decl (t);
4492 tree newtags = get_abi_tags (t);
4493 if (newtags && newtags != oldtags
4494 && abi_version_crosses (ver))
4496 if (for_decl && DECL_THUNK_P (for_decl))
4497 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4498 "the mangled name of a thunk for %qD changes between "
4499 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4500 t, flag_abi_version, warn_abi_version);
4501 else if (for_decl)
4502 warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
4503 "the mangled name of %qD changes between "
4504 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4505 for_decl, flag_abi_version, warn_abi_version);
4506 else
4507 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4508 "the mangled name of the initialization guard variable "
4509 "for %qD changes between %<-fabi-version=%d%> and "
4510 "%<-fabi-version=%d%>",
4511 t, flag_abi_version, warn_abi_version);
4515 /* Write out the appropriate string for this variable when generating
4516 another mangled name based on this one. */
4518 static void
4519 write_guarded_var_name (const tree variable)
4521 if (DECL_NAME (variable)
4522 && startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR"))
4523 /* The name of a guard variable for a reference temporary should refer
4524 to the reference, not the temporary. */
4525 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
4526 else
4527 write_name (variable, /*ignore_local_scope=*/0);
4530 /* Return an identifier for the name of an initialization guard
4531 variable for indicated VARIABLE. */
4533 tree
4534 mangle_guard_variable (const tree variable)
4536 if (abi_version_at_least (10))
4537 maybe_check_abi_tags (variable);
4538 start_mangling (variable);
4539 write_string ("_ZGV");
4540 write_guarded_var_name (variable);
4541 return finish_mangling_get_identifier ();
4544 /* Return an identifier for the name of a thread_local initialization
4545 function for VARIABLE. */
4547 tree
4548 mangle_tls_init_fn (const tree variable)
4550 check_abi_tags (variable);
4551 start_mangling (variable);
4552 write_string ("_ZTH");
4553 write_guarded_var_name (variable);
4554 return finish_mangling_get_identifier ();
4557 /* Return an identifier for the name of a thread_local wrapper
4558 function for VARIABLE. */
4560 #define TLS_WRAPPER_PREFIX "_ZTW"
4562 tree
4563 mangle_tls_wrapper_fn (const tree variable)
4565 check_abi_tags (variable);
4566 start_mangling (variable);
4567 write_string (TLS_WRAPPER_PREFIX);
4568 write_guarded_var_name (variable);
4569 return finish_mangling_get_identifier ();
4572 /* Return true iff FN is a thread_local wrapper function. */
4574 bool
4575 decl_tls_wrapper_p (const tree fn)
4577 if (TREE_CODE (fn) != FUNCTION_DECL)
4578 return false;
4579 tree name = DECL_NAME (fn);
4580 return startswith (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX);
4583 /* Return an identifier for the name of a temporary variable used to
4584 initialize a static reference. This is now part of the ABI. */
4586 tree
4587 mangle_ref_init_variable (const tree variable)
4589 start_mangling (variable);
4590 write_string ("_ZGR");
4591 check_abi_tags (variable);
4592 write_name (variable, /*ignore_local_scope=*/0);
4593 /* Avoid name clashes with aggregate initialization of multiple
4594 references at once. */
4595 write_compact_number (current_ref_temp_count++);
4596 return finish_mangling_get_identifier ();
4599 /* Return an identifier for the mangled name of a C++20 template parameter
4600 object for template argument EXPR. */
4602 tree
4603 mangle_template_parm_object (tree expr)
4605 start_mangling (expr);
4606 write_string ("_ZTAX");
4607 write_expression (expr);
4608 write_char ('E');
4609 return finish_mangling_get_identifier ();
4612 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
4613 function generates a mangled name for the vtable map variable of
4614 the class type. For example, if the class type is
4615 "std::bad_exception", the mangled name for the class is
4616 "St13bad_exception". This function would generate the name
4617 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
4618 "_VTV<std::bad_exception>::__vtable_map". */
4621 char *
4622 get_mangled_vtable_map_var_name (tree class_type)
4624 char *var_name = NULL;
4625 const char *prefix = "_ZN4_VTVI";
4626 const char *postfix = "E12__vtable_mapE";
4628 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
4630 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
4632 if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
4634 class_id = get_mangled_id (TYPE_NAME (class_type));
4635 vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
4638 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
4639 strlen (prefix) +
4640 strlen (postfix) + 1;
4642 var_name = (char *) xmalloc (len);
4644 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
4646 return var_name;
4649 #include "gt-cp-mangle.h"