Daily bump.
[official-gcc.git] / gcc / cp / mangle.cc
bloba04bc584586f28cb80d21b5c6d647416aa8843df
1 /* Name mangling for the 3.0 -*- C++ -*- ABI.
2 Copyright (C) 2000-2024 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, tree = NULL_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 /* Check for -fabi-version dependent mangling and also set the need_abi_warning
279 flag as appropriate. */
281 static bool
282 abi_check (int ver)
284 if (abi_warn_or_compat_version_crosses (ver))
285 G.need_abi_warning = true;
286 return abi_version_at_least (ver);
289 /* If DECL is a template instance (including the uninstantiated template
290 itself), return its TEMPLATE_INFO. Otherwise return NULL. */
292 static tree
293 maybe_template_info (const tree decl)
295 if (TREE_CODE (decl) == TYPE_DECL)
297 /* TYPE_DECLs are handled specially. Look at its type to decide
298 if this is a template instantiation. */
299 const tree type = TREE_TYPE (decl);
301 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
302 return TYPE_TEMPLATE_INFO (type);
304 else
306 /* Check if the template is a primary template. */
307 if (DECL_LANG_SPECIFIC (decl) != NULL
308 && VAR_OR_FUNCTION_DECL_P (decl)
309 && DECL_TEMPLATE_INFO (decl)
310 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
311 return DECL_TEMPLATE_INFO (decl);
314 /* It's not a template id. */
315 return NULL_TREE;
318 /* Produce debugging output of current substitution candidates. */
320 static void
321 dump_substitution_candidates (void)
323 unsigned i;
324 tree el;
326 fprintf (stderr, " ++ substitutions ");
327 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
329 const char *name = "???";
331 if (i > 0)
332 fprintf (stderr, " ");
333 if (!el)
334 name = "module";
335 else if (DECL_P (el))
336 name = IDENTIFIER_POINTER (DECL_NAME (el));
337 else if (TREE_CODE (el) == TREE_LIST)
338 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
339 else if (TYPE_NAME (el))
340 name = TYPE_NAME_STRING (el);
341 fprintf (stderr, " S%d_ = ", i - 1);
342 if (el)
344 if (TYPE_P (el) &&
345 (CP_TYPE_RESTRICT_P (el)
346 || CP_TYPE_VOLATILE_P (el)
347 || CP_TYPE_CONST_P (el)))
348 fprintf (stderr, "CV-");
349 fprintf (stderr, "%s (%s at %p)",
350 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
352 fprintf (stderr, "\n");
356 /* <exception-spec> ::=
357 Do -- non-throwing exception specification
358 DO <expression> E -- computed (instantiation-dependent) noexcept
359 Dw <type>* E -- throw (types) */
361 static void
362 write_exception_spec (tree spec)
365 if (!spec || spec == noexcept_false_spec)
366 /* Nothing. */
367 return;
369 if (!flag_noexcept_type)
371 G.need_cxx17_warning = true;
372 return;
375 if (spec == noexcept_true_spec || spec == empty_except_spec)
376 write_string ("Do");
377 else if (tree expr = TREE_PURPOSE (spec))
379 /* noexcept (expr) */
380 gcc_assert (uses_template_parms (expr));
381 write_string ("DO");
382 write_expression (expr);
383 write_char ('E');
385 else
387 /* throw (type-list) */
388 write_string ("Dw");
389 for (tree t = spec; t; t = TREE_CHAIN (t))
390 write_type (TREE_VALUE (t));
391 write_char ('E');
395 /* Both decls and types can be substitution candidates, but sometimes
396 they refer to the same thing. For instance, a TYPE_DECL and
397 RECORD_TYPE for the same class refer to the same thing, and should
398 be treated accordingly in substitutions. This function returns a
399 canonicalized tree node representing NODE that is used when adding
400 and substitution candidates and finding matches. */
402 static inline tree
403 canonicalize_for_substitution (tree node)
405 /* For a TYPE_DECL, use the type instead. */
406 if (TREE_CODE (node) == TYPE_DECL)
407 node = TREE_TYPE (node);
408 if (TYPE_P (node)
409 && TYPE_CANONICAL (node) != node
410 && TYPE_MAIN_VARIANT (node) != node)
412 tree orig = node;
413 /* Here we want to strip the topmost typedef only.
414 We need to do that so is_std_substitution can do proper
415 name matching. */
416 if (TREE_CODE (node) == FUNCTION_TYPE)
417 /* Use build_qualified_type and TYPE_QUALS here to preserve
418 the old buggy mangling of attribute noreturn with abi<5. */
419 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
420 TYPE_QUALS (node));
421 else
422 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
423 cp_type_quals (node));
424 if (FUNC_OR_METHOD_TYPE_P (node))
426 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
427 tree r = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (orig));
428 if (flag_noexcept_type)
429 node = build_exception_variant (node, r);
430 else
431 /* Set the warning flag if appropriate. */
432 write_exception_spec (r);
435 return node;
438 /* Add NODE as a substitution candidate. NODE must not already be on
439 the list of candidates. */
441 static void
442 add_substitution (tree node)
444 tree c;
446 if (DEBUG_MANGLE)
447 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
448 get_tree_code_name (TREE_CODE (node)), (void *) node);
450 /* Get the canonicalized substitution candidate for NODE. */
451 c = canonicalize_for_substitution (node);
452 if (DEBUG_MANGLE && c != node)
453 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
454 get_tree_code_name (TREE_CODE (node)), (void *) node);
455 node = c;
457 /* Make sure NODE isn't already a candidate. */
458 if (flag_checking)
460 int i;
461 tree candidate;
463 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
464 if (candidate)
466 gcc_assert (!(DECL_P (node) && node == candidate));
467 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
468 && same_type_p (node, candidate)));
472 /* Put the decl onto the varray of substitution candidates. */
473 vec_safe_push (G.substitutions, node);
475 if (DEBUG_MANGLE)
476 dump_substitution_candidates ();
479 /* Helper function for find_substitution. Returns nonzero if NODE,
480 which may be a decl or a CLASS_TYPE, is a template-id with template
481 name of substitution_index[INDEX] in the ::std namespace, with
482 global module attachment. */
484 static bool
485 is_std_substitution (const tree node,
486 const substitution_identifier_index_t index)
488 tree type = NULL;
489 tree decl = NULL;
491 if (DECL_P (node))
493 type = TREE_TYPE (node);
494 decl = node;
496 else if (CLASS_TYPE_P (node))
498 type = node;
499 decl = TYPE_NAME (node);
501 else
502 /* These are not the droids you're looking for. */
503 return false;
505 if (!DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl)))
506 return false;
508 if (!(TYPE_LANG_SPECIFIC (type) && TYPE_TEMPLATE_INFO (type)))
509 return false;
511 tree tmpl = TYPE_TI_TEMPLATE (type);
512 if (DECL_NAME (tmpl) != subst_identifiers[index])
513 return false;
515 if (modules_p () && get_originating_module (tmpl, true) >= 0)
516 return false;
518 return true;
521 /* Return the ABI tags (the TREE_VALUE of the "abi_tag" attribute entry) for T,
522 which can be a decl or type. */
524 static tree
525 get_abi_tags (tree t)
527 if (!t || TREE_CODE (t) == NAMESPACE_DECL)
528 return NULL_TREE;
530 if (DECL_P (t) && DECL_DECLARES_TYPE_P (t))
531 t = TREE_TYPE (t);
533 if (TREE_CODE (t) == TEMPLATE_DECL && DECL_TEMPLATE_RESULT (t))
535 tree tags = get_abi_tags (DECL_TEMPLATE_RESULT (t));
536 /* We used to overlook abi_tag on function and variable templates. */
537 if (tags && abi_check (19))
538 return tags;
539 else
540 return NULL_TREE;
543 tree attrs;
544 if (TYPE_P (t))
545 attrs = TYPE_ATTRIBUTES (t);
546 else
547 attrs = DECL_ATTRIBUTES (t);
549 tree tags = lookup_attribute ("abi_tag", attrs);
550 if (tags)
551 tags = TREE_VALUE (tags);
552 return tags;
555 /* Helper function for find_substitution. Returns nonzero if NODE,
556 which may be a decl or a CLASS_TYPE, is the template-id
557 ::std::identifier<char>, where identifier is
558 substitution_index[INDEX]. */
560 static bool
561 is_std_substitution_char (const tree node,
562 const substitution_identifier_index_t index)
564 tree args;
565 /* Check NODE's name is ::std::identifier. */
566 if (!is_std_substitution (node, index))
567 return 0;
568 /* Figure out its template args. */
569 if (DECL_P (node))
570 args = DECL_TI_ARGS (node);
571 else if (CLASS_TYPE_P (node))
572 args = CLASSTYPE_TI_ARGS (node);
573 else
574 /* Oops, not a template. */
575 return 0;
576 /* NODE's template arg list should be <char>. */
577 return
578 TREE_VEC_LENGTH (args) == 1
579 && TREE_VEC_ELT (args, 0) == char_type_node;
582 /* Check whether a substitution should be used to represent NODE in
583 the mangling.
585 First, check standard special-case substitutions.
587 <substitution> ::= St
588 # ::std
590 ::= Sa
591 # ::std::allocator
593 ::= Sb
594 # ::std::basic_string
596 ::= Ss
597 # ::std::basic_string<char,
598 ::std::char_traits<char>,
599 ::std::allocator<char> >
601 ::= Si
602 # ::std::basic_istream<char, ::std::char_traits<char> >
604 ::= So
605 # ::std::basic_ostream<char, ::std::char_traits<char> >
607 ::= Sd
608 # ::std::basic_iostream<char, ::std::char_traits<char> >
610 Then examine the stack of currently available substitution
611 candidates for entities appearing earlier in the same mangling
613 If a substitution is found, write its mangled representation and
614 return nonzero. If none is found, just return zero. */
616 static int
617 find_substitution (tree node)
619 int i;
620 const int size = vec_safe_length (G.substitutions);
621 tree decl;
622 tree type;
623 const char *abbr = NULL;
625 if (DEBUG_MANGLE)
626 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
627 get_tree_code_name (TREE_CODE (node)), (void *) node);
629 /* Obtain the canonicalized substitution representation for NODE.
630 This is what we'll compare against. */
631 node = canonicalize_for_substitution (node);
633 /* Check for builtin substitutions. */
635 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
636 type = TYPE_P (node) ? node : TREE_TYPE (node);
638 /* Check for std::allocator. */
639 if (decl
640 && is_std_substitution (decl, SUBID_ALLOCATOR)
641 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
642 abbr = "Sa";
644 /* Check for std::basic_string. */
645 else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
647 if (TYPE_P (node))
649 /* If this is a type (i.e. a fully-qualified template-id),
650 check for
651 std::basic_string <char,
652 std::char_traits<char>,
653 std::allocator<char> > . */
654 if (cp_type_quals (type) == TYPE_UNQUALIFIED
655 && CLASSTYPE_USE_TEMPLATE (type))
657 tree args = CLASSTYPE_TI_ARGS (type);
658 if (TREE_VEC_LENGTH (args) == 3
659 && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
660 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
661 SUBID_CHAR_TRAITS)
662 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
663 SUBID_ALLOCATOR))
664 abbr = "Ss";
667 else
668 /* Substitute for the template name only if this isn't a type. */
669 abbr = "Sb";
672 /* Check for basic_{i,o,io}stream. */
673 else if (TYPE_P (node)
674 && cp_type_quals (type) == TYPE_UNQUALIFIED
675 && CLASS_TYPE_P (type)
676 && CLASSTYPE_USE_TEMPLATE (type)
677 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
679 /* First, check for the template
680 args <char, std::char_traits<char> > . */
681 tree args = CLASSTYPE_TI_ARGS (type);
682 if (TREE_VEC_LENGTH (args) == 2
683 && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
684 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
685 SUBID_CHAR_TRAITS))
687 /* Got them. Is this basic_istream? */
688 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
689 abbr = "Si";
690 /* Or basic_ostream? */
691 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
692 abbr = "So";
693 /* Or basic_iostream? */
694 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
695 abbr = "Sd";
699 /* Check for namespace std. */
700 else if (decl && DECL_NAMESPACE_STD_P (decl))
702 write_string ("St");
703 return 1;
706 tree tags = NULL_TREE;
707 if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
708 tags = get_abi_tags (type);
709 /* Now check the list of available substitutions for this mangling
710 operation. */
711 if (!abbr || tags)
712 for (i = 0; i < size; ++i)
713 if (tree candidate = (*G.substitutions)[i])
715 /* NODE is a matched to a candidate if it's the same decl node or
716 if it's the same type. */
717 if (decl == candidate
718 || (TYPE_P (candidate) && type && TYPE_P (node)
719 && same_type_p (type, candidate))
720 || NESTED_TEMPLATE_MATCH (node, candidate))
722 write_substitution (i);
723 return 1;
727 if (!abbr)
728 /* No substitution found. */
729 return 0;
731 write_string (abbr);
732 if (tags)
734 /* If there are ABI tags on the abbreviation, it becomes
735 a substitution candidate. */
736 write_abi_tags (tags);
737 add_substitution (node);
739 return 1;
742 /* Returns whether DECL's symbol name should be the plain unqualified-id
743 rather than a more complicated mangled name. */
745 static bool
746 unmangled_name_p (const tree decl)
748 if (TREE_CODE (decl) == FUNCTION_DECL)
750 /* The names of `extern "C"' functions are not mangled. */
751 return (DECL_EXTERN_C_FUNCTION_P (decl)
752 /* But overloaded operator names *are* mangled. */
753 && !DECL_OVERLOADED_OPERATOR_P (decl));
755 else if (VAR_P (decl))
757 /* static variables are mangled. */
758 if (!DECL_EXTERNAL_LINKAGE_P (decl))
759 return false;
761 /* extern "C" declarations aren't mangled. */
762 if (DECL_EXTERN_C_P (decl))
763 return true;
765 /* Other variables at non-global scope are mangled. */
766 if (CP_DECL_CONTEXT (decl) != global_namespace)
767 return false;
769 /* Variable template instantiations are mangled. */
770 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
771 && variable_template_p (DECL_TI_TEMPLATE (decl)))
772 return false;
774 /* Declarations with ABI tags are mangled. */
775 if (get_abi_tags (decl))
776 return false;
778 // Declarations attached to a named module are mangled
779 if (modules_p () && get_originating_module (decl, true) >= 0)
780 return false;
782 /* The names of non-static global variables aren't mangled. */
783 return true;
786 return false;
789 /* TOP_LEVEL is true, if this is being called at outermost level of
790 mangling. It should be false when mangling a decl appearing in an
791 expression within some other mangling.
793 <mangled-name> ::= _Z <encoding> */
795 static void
796 write_mangled_name (const tree decl, bool top_level)
798 MANGLE_TRACE_TREE ("mangled-name", decl);
800 check_abi_tags (decl);
802 if (unmangled_name_p (decl))
804 if (top_level)
805 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
806 else
808 /* The standard notes: "The <encoding> of an extern "C"
809 function is treated like global-scope data, i.e. as its
810 <source-name> without a type." We cannot write
811 overloaded operators that way though, because it contains
812 characters invalid in assembler. */
813 write_string ("_Z");
814 write_source_name (DECL_NAME (decl));
817 else
819 write_string ("_Z");
820 write_encoding (decl);
823 /* If this is the pre/post function for a guarded function, append
824 .pre/post, like something from create_virtual_clone. */
825 if (DECL_IS_PRE_FN_P (decl))
826 write_string (".pre");
827 else if (DECL_IS_POST_FN_P (decl))
828 write_string (".post");
830 /* If this is a coroutine helper, then append an appropriate string to
831 identify which. */
832 if (tree ramp = DECL_RAMP_FN (decl))
834 if (DECL_ACTOR_FN (ramp) == decl)
835 write_string (JOIN_STR "actor");
836 else if (DECL_DESTROY_FN (ramp) == decl)
837 write_string (JOIN_STR "destroy");
838 else
839 gcc_unreachable ();
843 /* Returns true if the return type of DECL is part of its signature, and
844 therefore its mangling. */
846 bool
847 mangle_return_type_p (tree decl)
849 return (!DECL_CONSTRUCTOR_P (decl)
850 && !DECL_DESTRUCTOR_P (decl)
851 && !DECL_CONV_FN_P (decl)
852 && maybe_template_info (decl));
855 /* <constraint-expression> ::= <expression> */
857 static void
858 write_constraint_expression (tree expr)
860 write_expression (expr);
863 /* Mangle a requires-clause following a template-head, if any.
865 Q <constraint_expression> E */
867 static void
868 write_tparms_constraints (tree constraints)
870 /* In a declaration with shorthand constraints in the template-head, followed
871 by a requires-clause, followed by shorthand constraints in the
872 function-parameter-list, the full constraints will be some && with the
873 parameter constraints on the RHS, around an && with the requires-clause on
874 the RHS. Find the requires-clause, if any.
876 This logic relies on the && and ... from combine_constraint_expressions,
877 finish_shorthand_constraint, and convert_generic_types_to_packs having
878 UNKNOWN_LOCATION. If they need to have an actual location, we could move
879 to using a TREE_LANG_FLAG. */
880 if (constraints && abi_check (19))
882 tree probe = constraints;
883 while (probe
884 && !EXPR_LOCATION (probe)
885 && TREE_CODE (probe) == TRUTH_ANDIF_EXPR)
887 tree op1 = TREE_OPERAND (probe, 1);
888 probe = (EXPR_LOCATION (op1) ? op1
889 : TREE_OPERAND (probe, 0));
891 if (probe && EXPR_LOCATION (probe))
893 write_char ('Q');
894 write_constraint_expression (probe);
899 /* <type-constraint> ::= <name> */
901 static void
902 write_type_constraint (tree cnst)
904 if (!cnst) return;
906 cnst = unpack_concept_check (cnst);
907 gcc_checking_assert (TREE_CODE (cnst) == TEMPLATE_ID_EXPR);
909 tree concept_decl = get_concept_check_template (cnst);
910 write_name (concept_decl, 0);
911 tree args = TREE_OPERAND (cnst, 1);
912 if (TREE_VEC_LENGTH (args) > 1)
914 TEMPLATE_ARGS_TYPE_CONSTRAINT_P (args) = true;
915 write_template_args (args);
919 /* <encoding> ::= <function name> <bare-function-type>
920 ::= <data name> */
922 static void
923 write_encoding (const tree decl)
925 MANGLE_TRACE_TREE ("encoding", decl);
927 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
929 /* For overloaded operators write just the mangled name
930 without arguments. */
931 if (DECL_OVERLOADED_OPERATOR_P (decl))
932 write_name (decl, /*ignore_local_scope=*/0);
933 else
934 write_source_name (DECL_NAME (decl));
935 return;
938 write_name (decl, /*ignore_local_scope=*/0);
939 if (TREE_CODE (decl) == FUNCTION_DECL)
941 tree fn_type;
942 tree d;
944 if (maybe_template_info (decl))
946 fn_type = get_mostly_instantiated_function_type (decl);
947 /* FN_TYPE will not have parameter types for in-charge or
948 VTT parameters. Therefore, we pass NULL_TREE to
949 write_bare_function_type -- otherwise, it will get
950 confused about which artificial parameters to skip. */
951 d = NULL_TREE;
953 else
955 fn_type = TREE_TYPE (decl);
956 d = decl;
959 write_bare_function_type (fn_type,
960 mangle_return_type_p (decl),
963 if (tree c = get_trailing_function_requirements (decl))
964 if (abi_check (19))
966 ++G.parm_depth;
967 write_char ('Q');
968 write_constraint_expression (c);
969 --G.parm_depth;
974 /* Interface to substitution and identifier mangling, used by the
975 module name mangler. */
977 void
978 mangle_module_substitution (int v)
980 write_substitution (v - 1);
984 mangle_module_component (tree comp, bool partition_p)
986 write_char ('W');
987 if (partition_p)
988 write_char ('P');
989 write_source_name (comp);
991 // Module substitutions use the same number-space as entity
992 // substitutions, but are orthogonal.
993 vec_safe_push (G.substitutions, NULL_TREE);
994 return G.substitutions->length ();
997 /* If the outermost non-namespace context (including DECL itself) is
998 a module-linkage decl, mangle the module information. For module
999 global initializers we need to include the partition part.
1001 <module-name> ::= <module-sub>
1002 || <subst>
1003 || <module-name> <module-sub>
1004 <module-sub> :: W [P] <unqualified-name>
1007 static void
1008 write_module (int m, bool include_partition)
1010 G.mod = true;
1011 mangle_module (m, include_partition);
1014 static void
1015 maybe_write_module (tree decl)
1017 if (!DECL_NAMESPACE_SCOPE_P (decl))
1018 return;
1020 if (!TREE_PUBLIC (STRIP_TEMPLATE (decl)))
1021 return;
1023 if (TREE_CODE (decl) == NAMESPACE_DECL)
1024 return;
1026 int m = get_originating_module (decl, true);
1027 if (m >= 0)
1028 write_module (m, false);
1031 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
1032 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
1034 static tree
1035 decl_mangling_context (tree decl)
1037 tree tcontext = targetm.cxx.decl_mangling_context (decl);
1039 if (tcontext != NULL_TREE)
1040 return tcontext;
1042 if (TREE_CODE (decl) == TEMPLATE_DECL
1043 && DECL_TEMPLATE_RESULT (decl))
1044 decl = DECL_TEMPLATE_RESULT (decl);
1046 if (TREE_CODE (decl) == TYPE_DECL
1047 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
1049 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
1050 if (extra)
1051 return extra;
1053 else if (template_type_parameter_p (decl))
1054 /* template type parms have no mangling context. */
1055 return NULL_TREE;
1057 tcontext = CP_DECL_CONTEXT (decl);
1059 if (member_like_constrained_friend_p (decl))
1060 tcontext = DECL_FRIEND_CONTEXT (decl);
1062 /* Ignore the artificial declare reduction functions. */
1063 if (tcontext
1064 && TREE_CODE (tcontext) == FUNCTION_DECL
1065 && DECL_OMP_DECLARE_REDUCTION_P (tcontext))
1066 return decl_mangling_context (tcontext);
1068 return tcontext;
1071 /* <name> ::= <unscoped-name>
1072 ::= <unscoped-template-name> <template-args>
1073 ::= <nested-name>
1074 ::= <local-name>
1076 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
1077 called from <local-name>, which mangles the enclosing scope
1078 elsewhere and then uses this function to mangle just the part
1079 underneath the function scope. So don't use the <local-name>
1080 production, to avoid an infinite recursion. */
1082 static void
1083 write_name (tree decl, const int ignore_local_scope)
1085 tree context;
1087 MANGLE_TRACE_TREE ("name", decl);
1089 if (TREE_CODE (decl) == TYPE_DECL)
1091 /* In case this is a typedef, fish out the corresponding
1092 TYPE_DECL for the main variant. */
1093 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
1096 context = decl_mangling_context (decl);
1098 gcc_assert (context != NULL_TREE);
1100 if (abi_warn_or_compat_version_crosses (7)
1101 && ignore_local_scope
1102 && TREE_CODE (context) == PARM_DECL)
1103 G.need_abi_warning = 1;
1105 /* A decl in :: or ::std scope is treated specially. The former is
1106 mangled using <unscoped-name> or <unscoped-template-name>, the
1107 latter with a special substitution. Also, a name that is
1108 directly in a local function scope is also mangled with
1109 <unscoped-name> rather than a full <nested-name>. */
1110 if (context == global_namespace
1111 || DECL_NAMESPACE_STD_P (context)
1112 || (ignore_local_scope
1113 && (TREE_CODE (context) == FUNCTION_DECL
1114 || (abi_version_at_least (7)
1115 && TREE_CODE (context) == PARM_DECL))))
1117 /* Is this a template instance? */
1118 if (tree info = maybe_template_info (decl))
1120 /* Yes: use <unscoped-template-name>. */
1121 write_unscoped_template_name (TI_TEMPLATE (info));
1122 /* Pass down the parms of a function template in case we need to
1123 mangle them; we don't mangle the parms of a non-overloadable
1124 template. */
1125 tree parms = (TREE_CODE (decl) == FUNCTION_DECL
1126 ? DECL_TEMPLATE_PARMS (TI_TEMPLATE (info))
1127 : NULL_TREE);
1128 write_template_args (TI_ARGS (info), parms);
1130 else
1131 /* Everything else gets an <unqualified-name>. */
1132 write_unscoped_name (decl);
1134 else
1136 /* Handle local names, unless we asked not to (that is, invoked
1137 under <local-name>, to handle only the part of the name under
1138 the local scope). */
1139 if (!ignore_local_scope)
1141 /* Scan up the list of scope context, looking for a
1142 function. If we find one, this entity is in local
1143 function scope. local_entity tracks context one scope
1144 level down, so it will contain the element that's
1145 directly in that function's scope, either decl or one of
1146 its enclosing scopes. */
1147 tree local_entity = decl;
1148 while (context != global_namespace)
1150 /* Make sure we're always dealing with decls. */
1151 if (TYPE_P (context))
1152 context = TYPE_NAME (context);
1153 /* Is this a function? */
1154 if (TREE_CODE (context) == FUNCTION_DECL
1155 || TREE_CODE (context) == PARM_DECL)
1157 /* Yes, we have local scope. Use the <local-name>
1158 production for the innermost function scope. */
1159 write_local_name (context, local_entity, decl);
1160 return;
1162 /* Up one scope level. */
1163 local_entity = context;
1164 context = decl_mangling_context (context);
1167 /* No local scope found? Fall through to <nested-name>. */
1170 /* Other decls get a <nested-name> to encode their scope. */
1171 write_nested_name (decl);
1175 /* <unscoped-name> ::= <unqualified-name>
1176 ::= St <unqualified-name> # ::std:: */
1178 static void
1179 write_unscoped_name (const tree decl)
1181 tree context = decl_mangling_context (decl);
1183 MANGLE_TRACE_TREE ("unscoped-name", decl);
1185 /* Is DECL in ::std? */
1186 if (DECL_NAMESPACE_STD_P (context))
1188 write_string ("St");
1189 write_unqualified_name (decl);
1191 else
1193 /* If not, it should be either in the global namespace, or directly
1194 in a local function scope. A lambda can also be mangled in the
1195 scope of a default argument. */
1196 gcc_assert (context == global_namespace
1197 || TREE_CODE (context) == PARM_DECL
1198 || TREE_CODE (context) == FUNCTION_DECL);
1200 write_unqualified_name (decl);
1204 /* <unscoped-template-name> ::= <unscoped-name>
1205 ::= <substitution> */
1207 static void
1208 write_unscoped_template_name (const tree decl)
1210 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
1212 if (find_substitution (decl))
1213 return;
1214 write_unscoped_name (decl);
1215 add_substitution (decl);
1218 /* Write the nested name, including CV-qualifiers, of DECL.
1220 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1221 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1223 <ref-qualifier> ::= R # & ref-qualifier
1224 ::= O # && ref-qualifier
1225 <CV-qualifiers> ::= [r] [V] [K] */
1227 static void
1228 write_nested_name (const tree decl)
1230 MANGLE_TRACE_TREE ("nested-name", decl);
1232 write_char ('N');
1234 /* Write CV-qualifiers, if this is an iobj member function. */
1235 if (TREE_CODE (decl) == FUNCTION_DECL
1236 && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
1238 if (DECL_VOLATILE_MEMFUNC_P (decl))
1239 write_char ('V');
1240 if (DECL_CONST_MEMFUNC_P (decl))
1241 write_char ('K');
1242 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
1244 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
1245 write_char ('O');
1246 else
1247 write_char ('R');
1250 else if (DECL_DECLARES_FUNCTION_P (decl)
1251 && DECL_XOBJ_MEMBER_FUNCTION_P (decl))
1252 write_char ('H');
1254 /* Is this a template instance? */
1255 if (tree info = maybe_template_info (decl))
1257 /* Yes, use <template-prefix>. */
1258 write_template_prefix (decl);
1259 write_template_args (TI_ARGS (info));
1261 else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
1262 && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1264 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1265 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1267 write_template_prefix (decl);
1268 write_template_args (TREE_OPERAND (name, 1));
1270 else
1272 write_prefix (decl_mangling_context (decl));
1273 write_unqualified_name (decl);
1276 else
1278 /* No, just use <prefix> */
1279 write_prefix (decl_mangling_context (decl));
1280 write_unqualified_name (decl);
1282 write_char ('E');
1285 /* <prefix> ::= <prefix> <unqualified-name>
1286 ::= <template-param>
1287 ::= <template-prefix> <template-args>
1288 ::= <decltype>
1289 ::= # empty
1290 ::= <substitution> */
1292 static void
1293 write_prefix (const tree node)
1295 tree decl;
1297 if (node == NULL
1298 || node == global_namespace)
1299 return;
1301 MANGLE_TRACE_TREE ("prefix", node);
1303 if (TREE_CODE (node) == DECLTYPE_TYPE)
1305 write_type (node);
1306 return;
1309 if (find_substitution (node))
1310 return;
1312 tree template_info = NULL_TREE;
1313 if (DECL_P (node))
1315 /* If this is a function or parm decl, that means we've hit function
1316 scope, so this prefix must be for a local name. In this
1317 case, we're under the <local-name> production, which encodes
1318 the enclosing function scope elsewhere. So don't continue
1319 here. */
1320 if (TREE_CODE (node) == FUNCTION_DECL
1321 || TREE_CODE (node) == PARM_DECL)
1322 return;
1324 decl = node;
1325 template_info = maybe_template_info (decl);
1327 else
1329 /* Node is a type. */
1330 decl = TYPE_NAME (node);
1331 /* The DECL might not point at the node. */
1332 if (CLASSTYPE_TEMPLATE_ID_P (node))
1333 template_info = TYPE_TEMPLATE_INFO (node);
1336 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1337 write_template_param (node);
1338 else if (template_info)
1339 /* Templated. */
1341 write_template_prefix (decl);
1342 write_template_args (TI_ARGS (template_info));
1344 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1346 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1347 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1349 write_template_prefix (decl);
1350 write_template_args (TREE_OPERAND (name, 1));
1352 else
1354 write_prefix (decl_mangling_context (decl));
1355 write_unqualified_name (decl);
1358 else
1359 /* Not templated. */
1361 write_prefix (decl_mangling_context (decl));
1362 write_unqualified_name (decl);
1363 if (VAR_P (decl)
1364 || TREE_CODE (decl) == FIELD_DECL)
1366 /* <data-member-prefix> := <member source-name> M */
1367 write_char ('M');
1369 /* Before ABI 18, we did not count these as substitution
1370 candidates. This leads to incorrect demanglings (and
1371 ABI divergence to other compilers). */
1372 if (!abi_check (18))
1373 return;
1377 add_substitution (node);
1380 /* <template-prefix> ::= <prefix> <template component>
1381 ::= <template-param>
1382 ::= <substitution> */
1384 static void
1385 write_template_prefix (const tree node)
1387 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1388 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1389 tree context = decl_mangling_context (decl);
1390 tree templ;
1391 tree substitution;
1393 MANGLE_TRACE_TREE ("template-prefix", node);
1395 /* Find the template decl. */
1396 if (tree info = maybe_template_info (decl))
1397 templ = TI_TEMPLATE (info);
1398 else if (TREE_CODE (type) == TYPENAME_TYPE)
1399 /* For a typename type, all we have is the name. */
1400 templ = DECL_NAME (decl);
1401 else
1403 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1405 templ = TYPE_TI_TEMPLATE (type);
1408 /* For a member template, though, the template name for the
1409 innermost name must have all the outer template levels
1410 instantiated. For instance, consider
1412 template<typename T> struct Outer {
1413 template<typename U> struct Inner {};
1416 The template name for `Inner' in `Outer<int>::Inner<float>' is
1417 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1418 levels separately, so there's no TEMPLATE_DECL available for this
1419 (there's only `Outer<T>::Inner<U>').
1421 In order to get the substitutions right, we create a special
1422 TREE_LIST to represent the substitution candidate for a nested
1423 template. The TREE_PURPOSE is the template's context, fully
1424 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1425 template.
1427 So, for the example above, `Outer<int>::Inner' is represented as a
1428 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1429 and whose value is `Outer<T>::Inner<U>'. */
1430 if (context && TYPE_P (context))
1431 substitution = build_tree_list (context, templ);
1432 else
1433 substitution = templ;
1435 if (find_substitution (substitution))
1436 return;
1438 if (TREE_TYPE (templ)
1439 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1440 write_template_param (TREE_TYPE (templ));
1441 else
1443 write_prefix (context);
1444 write_unqualified_name (decl);
1447 add_substitution (substitution);
1450 /* "For the purposes of mangling, the name of an anonymous union is considered
1451 to be the name of the first named data member found by a pre-order,
1452 depth-first, declaration-order walk of the data members of the anonymous
1453 union. If there is no such data member (i.e., if all of the data members in
1454 the union are unnamed), then there is no way for a program to refer to the
1455 anonymous union, and there is therefore no need to mangle its name." */
1457 static tree
1458 anon_aggr_naming_decl (tree type)
1460 tree field = next_aggregate_field (TYPE_FIELDS (type));
1461 for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1463 if (DECL_NAME (field))
1464 return field;
1465 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1466 if (tree sub = anon_aggr_naming_decl (TREE_TYPE (field)))
1467 return sub;
1469 return NULL_TREE;
1472 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1473 mangled through special entry points.
1475 <unqualified-name> ::= [<module-name>] <operator-name>
1476 ::= <special-name>
1477 ::= [<module-name>] <source-name>
1478 ::= [<module-name>] <unnamed-type-name>
1479 ::= <local-source-name>
1480 ::= F <source-name> # member-like constrained friend
1482 <local-source-name> ::= L <source-name> <discriminator> */
1484 static void
1485 write_unqualified_id (tree identifier)
1487 if (IDENTIFIER_CONV_OP_P (identifier))
1488 write_conversion_operator_name (TREE_TYPE (identifier));
1489 else if (IDENTIFIER_OVL_OP_P (identifier))
1491 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier);
1492 write_string (ovl_op->mangled_name);
1494 else if (UDLIT_OPER_P (identifier))
1495 write_literal_operator_name (identifier);
1496 else
1497 write_source_name (identifier);
1500 static void
1501 write_unqualified_name (tree decl)
1503 MANGLE_TRACE_TREE ("unqualified-name", decl);
1505 if (modules_p ())
1506 maybe_write_module (decl);
1508 if (identifier_p (decl))
1510 write_unqualified_id (decl);
1511 return;
1514 bool found = false;
1516 if (DECL_NAME (decl) == NULL_TREE
1517 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1518 decl = anon_aggr_naming_decl (TREE_TYPE (decl));
1519 else if (DECL_NAME (decl) == NULL_TREE)
1521 found = true;
1522 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1523 write_source_name (DECL_ASSEMBLER_NAME (decl));
1525 else if (DECL_DECLARES_FUNCTION_P (decl))
1527 found = true;
1529 /* A constrained hidden friend is mangled like a member function, with
1530 the name prefixed by 'F'. */
1531 if (member_like_constrained_friend_p (decl))
1532 write_char ('F');
1534 if (DECL_CONSTRUCTOR_P (decl))
1535 write_special_name_constructor (decl);
1536 else if (DECL_DESTRUCTOR_P (decl))
1537 write_special_name_destructor (decl);
1538 else if (DECL_CONV_FN_P (decl))
1540 /* Conversion operator. Handle it right here.
1541 <operator> ::= cv <type> */
1542 tree type;
1543 if (maybe_template_info (decl))
1545 tree fn_type;
1546 fn_type = get_mostly_instantiated_function_type (decl);
1547 type = TREE_TYPE (fn_type);
1549 else if (FNDECL_USED_AUTO (decl))
1550 type = DECL_SAVED_AUTO_RETURN_TYPE (decl);
1551 else
1552 type = DECL_CONV_FN_TYPE (decl);
1553 write_conversion_operator_name (type);
1555 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1557 tree t;
1558 if (!(t = DECL_RAMP_FN (decl)))
1559 t = decl;
1560 const char *mangled_name
1561 = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (t)]
1562 [DECL_OVERLOADED_OPERATOR_CODE_RAW (t)].mangled_name);
1563 write_string (mangled_name);
1565 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1566 write_literal_operator_name (DECL_NAME (decl));
1567 else
1568 found = false;
1571 if (found)
1572 /* OK */;
1573 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1574 && DECL_NAMESPACE_SCOPE_P (decl)
1575 && decl_linkage (decl) == lk_internal)
1577 MANGLE_TRACE_TREE ("local-source-name", decl);
1578 write_char ('L');
1579 write_source_name (DECL_NAME (decl));
1580 /* The default discriminator is 1, and that's all we ever use,
1581 so there's no code to output one here. */
1583 else
1585 tree type = TREE_TYPE (decl);
1587 if (TREE_CODE (decl) == TYPE_DECL
1588 && TYPE_UNNAMED_P (type))
1589 write_unnamed_type_name (type);
1590 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type))
1591 write_closure_type_name (type);
1592 else
1593 write_source_name (DECL_NAME (decl));
1596 /* We use the ABI tags from the primary class template, ignoring tags on any
1597 specializations. This is necessary because C++ doesn't require a
1598 specialization to be declared before it is used unless the use requires a
1599 complete type, but we need to get the tags right on incomplete types as
1600 well. */
1601 if (tree tmpl = most_general_template (decl))
1603 tree res = DECL_TEMPLATE_RESULT (tmpl);
1604 if (res == NULL_TREE)
1605 /* UNBOUND_CLASS_TEMPLATE. */;
1606 else if (DECL_DECLARES_TYPE_P (decl))
1607 decl = res;
1608 else if (any_abi_below (11))
1610 /* ABI v10 implicit tags on the template. */
1611 tree mtags = missing_abi_tags (res);
1612 /* Explicit tags on the template. */
1613 tree ttags = get_abi_tags (res);
1614 /* Tags on the instantiation. */
1615 tree dtags = get_abi_tags (decl);
1617 if (mtags && abi_warn_or_compat_version_crosses (10))
1618 G.need_abi_warning = 1;
1620 /* Add the v10 tags to the explicit tags now. */
1621 mtags = chainon (mtags, ttags);
1623 if (!G.need_abi_warning
1624 && abi_warn_or_compat_version_crosses (11)
1625 && !equal_abi_tags (dtags, mtags))
1626 G.need_abi_warning = 1;
1628 if (!abi_version_at_least (10))
1629 /* In abi <10, we only got the explicit tags. */
1630 decl = res;
1631 else if (flag_abi_version == 10)
1633 /* In ABI 10, we want explict and implicit tags. */
1634 write_abi_tags (mtags);
1635 return;
1640 tree tags = get_abi_tags (decl);
1641 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
1642 && any_abi_below (11))
1643 if (tree mtags = missing_abi_tags (decl))
1645 if (!abi_check (11))
1646 tags = chainon (mtags, tags);
1648 write_abi_tags (tags);
1651 /* Write the unqualified-name for a conversion operator to TYPE. */
1653 static void
1654 write_conversion_operator_name (const tree type)
1656 write_string ("cv");
1657 write_type (type);
1660 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1662 <source-name> ::= </length/ number> <identifier> */
1664 static void
1665 write_source_name (tree identifier)
1667 MANGLE_TRACE_TREE ("source-name", identifier);
1669 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1670 write_identifier (IDENTIFIER_POINTER (identifier));
1673 /* Compare two TREE_STRINGs like strcmp. */
1676 tree_string_cmp (const void *p1, const void *p2)
1678 if (p1 == p2)
1679 return 0;
1680 tree s1 = *(const tree*)p1;
1681 tree s2 = *(const tree*)p2;
1682 return strcmp (TREE_STRING_POINTER (s1),
1683 TREE_STRING_POINTER (s2));
1686 /* Return the TREE_LIST of TAGS as a sorted VEC. */
1688 static vec<tree, va_gc> *
1689 sorted_abi_tags (tree tags)
1691 vec<tree, va_gc> * vec = make_tree_vector();
1693 for (tree t = tags; t; t = TREE_CHAIN (t))
1695 if (ABI_TAG_IMPLICIT (t))
1696 continue;
1697 tree str = TREE_VALUE (t);
1698 vec_safe_push (vec, str);
1701 vec->qsort (tree_string_cmp);
1703 return vec;
1706 /* ID is the name of a function or type with abi_tags attribute TAGS.
1707 Write out the name, suitably decorated. */
1709 static void
1710 write_abi_tags (tree tags)
1712 if (tags == NULL_TREE)
1713 return;
1715 vec<tree, va_gc> * vec = sorted_abi_tags (tags);
1717 unsigned i; tree str;
1718 FOR_EACH_VEC_ELT (*vec, i, str)
1720 write_string ("B");
1721 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1722 write_identifier (TREE_STRING_POINTER (str));
1725 release_tree_vector (vec);
1728 /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */
1730 static bool
1731 equal_abi_tags (tree t1, tree t2)
1733 releasing_vec v1 = sorted_abi_tags (t1);
1734 releasing_vec v2 = sorted_abi_tags (t2);
1736 unsigned len1 = v1->length();
1737 if (len1 != v2->length())
1738 return false;
1739 for (unsigned i = 0; i < len1; ++i)
1740 if (tree_string_cmp (v1[i], v2[i]) != 0)
1741 return false;
1742 return true;
1745 /* Write a user-defined literal operator.
1746 ::= li <source-name> # "" <source-name>
1747 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1749 static void
1750 write_literal_operator_name (tree identifier)
1752 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1753 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1754 write_unsigned_number (strlen (suffix));
1755 write_identifier (suffix);
1758 /* Encode 0 as _, and 1+ as n-1_. */
1760 static void
1761 write_compact_number (int num)
1763 gcc_checking_assert (num >= 0);
1764 if (num > 0)
1765 write_unsigned_number (num - 1);
1766 write_char ('_');
1769 /* Return how many unnamed types precede TYPE in its enclosing class. */
1771 static int
1772 nested_anon_class_index (tree type)
1774 int index = 0;
1775 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1776 for (; member; member = DECL_CHAIN (member))
1777 if (DECL_IMPLICIT_TYPEDEF_P (member))
1779 tree memtype = TREE_TYPE (member);
1780 if (memtype == type)
1781 return index;
1782 else if (TYPE_UNNAMED_P (memtype))
1783 ++index;
1786 if (seen_error ())
1787 return -1;
1789 gcc_unreachable ();
1792 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1794 static void
1795 write_unnamed_type_name (const tree type)
1797 int discriminator;
1798 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1800 if (TYPE_FUNCTION_SCOPE_P (type))
1801 discriminator = discriminator_for_local_entity (TYPE_NAME (type));
1802 else if (TYPE_CLASS_SCOPE_P (type))
1803 discriminator = nested_anon_class_index (type);
1804 else
1806 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1807 /* Just use the old mangling at namespace scope. */
1808 write_source_name (TYPE_IDENTIFIER (type));
1809 return;
1812 write_string ("Ut");
1813 write_compact_number (discriminator);
1816 /* ABI issue #47: if a function template parameter is not "natural" for its
1817 argument we must mangle the parameter. */
1819 static bool
1820 template_parm_natural_p (tree arg, tree parm)
1822 tree decl = TREE_VALUE (parm);
1824 /* A template parameter is "natural" if: */
1826 if (template_parameter_pack_p (decl))
1828 tree args = ARGUMENT_PACK_ARGS (arg);
1829 if (TREE_VEC_LENGTH (args) == 0)
1831 #if 0
1832 /* the argument is an empty pack and the parameter is an
1833 unconstrained template type parameter pack; */
1834 if (TREE_CODE (decl) != TYPE_DECL)
1835 return false;
1836 #else
1837 /* Defer changing the mangling of C++11 code like
1838 template <int i> int max();
1839 template <int i, int j, int... rest> int max(); */
1840 return true;
1841 #endif
1843 else
1844 /* the argument is a non-empty pack and a non-pack variant of the
1845 parameter would be natural for the first element of the pack; */
1846 arg = TREE_VEC_ELT (args, 0);
1849 /* the argument is a template and the parameter has the exact
1850 same template head; */
1851 if (TREE_CODE (decl) == TEMPLATE_DECL)
1852 return template_heads_equivalent_p (arg, decl);
1854 /* the argument is a type and the parameter is unconstrained; or */
1855 else if (TREE_CODE (decl) == TYPE_DECL)
1856 return !TEMPLATE_PARM_CONSTRAINTS (parm);
1858 /* the argument is a non-type template argument and the declared parameter
1859 type neither is instantiation dependent nor contains deduced types. */
1860 else if (TREE_CODE (decl) == PARM_DECL)
1862 #if 0
1863 return !uses_template_parms (TREE_TYPE (decl));
1864 #else
1865 /* Defer changing the mangling of C++98 code like
1866 template <class T, T V> .... */
1867 return !type_uses_auto (TREE_TYPE (decl));
1868 #endif
1871 gcc_unreachable ();
1874 /* Used for lambda template head and non-natural function template parameters.
1876 <template-param-decl> ::= Ty # template type parameter
1877 ::= Tk <type-constraint> # constrained type parameter
1878 ::= Tn <type> # template non-type parameter
1879 ::= Tt <template-param-decl>* [Q <constraint-expression] E # ttp
1880 ::= Tp <non-pack template-param-decl> # template parameter pack */
1882 static void
1883 write_template_param_decl (tree parm)
1885 tree decl = TREE_VALUE (parm);
1887 if (template_parameter_pack_p (decl))
1888 write_string ("Tp");
1890 switch (TREE_CODE (decl))
1892 case PARM_DECL:
1894 write_string ("Tn");
1896 tree type = TREE_TYPE (decl);
1897 if (tree c = (is_auto (type)
1898 ? PLACEHOLDER_TYPE_CONSTRAINTS (type)
1899 : NULL_TREE))
1901 if (AUTO_IS_DECLTYPE (type))
1902 write_string ("DK");
1903 else
1904 write_string ("Dk");
1905 write_type_constraint (c);
1907 else
1908 write_type (type);
1910 break;
1912 case TEMPLATE_DECL:
1914 write_string ("Tt");
1915 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
1916 for (tree node : tree_vec_range (parms))
1917 write_template_param_decl (node);
1918 write_char ('E');
1920 break;
1922 case TYPE_DECL:
1923 if (tree c = TEMPLATE_PARM_CONSTRAINTS (parm))
1925 if (TREE_CODE (c) == UNARY_LEFT_FOLD_EXPR)
1927 c = FOLD_EXPR_PACK (c);
1928 c = PACK_EXPANSION_PATTERN (c);
1930 if (TREE_CODE (decl) == TYPE_DECL)
1932 write_string ("Tk");
1933 write_type_constraint (c);
1936 else
1937 write_string ("Ty");
1938 break;
1940 default:
1941 gcc_unreachable ();
1945 // A template head, for templated lambdas.
1946 // New in ABI=18. Returns true iff we emitted anything -- used for ABI
1947 // version warning.
1949 static bool
1950 write_closure_template_head (tree tmpl)
1952 bool any = false;
1954 // We only need one level of template parms
1955 tree parms = DECL_TEMPLATE_PARMS (tmpl);
1956 tree inner = INNERMOST_TEMPLATE_PARMS (parms);
1958 for (int ix = 0, len = TREE_VEC_LENGTH (inner); ix != len; ix++)
1960 tree parm = TREE_VEC_ELT (inner, ix);
1961 if (parm == error_mark_node)
1962 continue;
1964 if (DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (parm)))
1965 // A synthetic parm, we're done.
1966 break;
1968 any = true;
1969 if (abi_version_at_least (18))
1970 write_template_param_decl (parm);
1973 write_tparms_constraints (TEMPLATE_PARMS_CONSTRAINTS (parms));
1975 return any;
1978 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1979 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1981 static void
1982 write_closure_type_name (const tree type)
1984 tree fn = lambda_function (type);
1985 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1986 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1988 MANGLE_TRACE_TREE ("closure-type-name", type);
1990 write_string ("Ul");
1992 if (auto ti = maybe_template_info (fn))
1993 if (write_closure_template_head (TI_TEMPLATE (ti)))
1994 // If there were any explicit template parms, we may need to
1995 // issue a mangling diagnostic.
1996 if (abi_warn_or_compat_version_crosses (18))
1997 G.need_abi_warning = true;
1999 write_method_parms (parms, TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE, fn);
2000 write_char ('E');
2001 if ((LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
2002 != LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda))
2003 && abi_warn_or_compat_version_crosses (18))
2004 G.need_abi_warning = true;
2005 write_compact_number (abi_version_at_least (18)
2006 ? LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
2007 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda));
2010 /* Convert NUMBER to ascii using base BASE and generating at least
2011 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
2012 into which to store the characters. Returns the number of
2013 characters generated (these will be laid out in advance of where
2014 BUFFER points). */
2016 static int
2017 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
2018 char *buffer, const unsigned int min_digits)
2020 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2021 unsigned digits = 0;
2023 while (number)
2025 unsigned HOST_WIDE_INT d = number / base;
2027 *--buffer = base_digits[number - d * base];
2028 digits++;
2029 number = d;
2031 while (digits < min_digits)
2033 *--buffer = base_digits[0];
2034 digits++;
2036 return digits;
2039 /* Non-terminal <number>.
2041 <number> ::= [n] </decimal integer/> */
2043 static void
2044 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
2045 const unsigned int base)
2047 char buffer[sizeof (HOST_WIDE_INT) * 8];
2048 unsigned count = 0;
2050 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
2052 write_char ('n');
2053 number = -((HOST_WIDE_INT) number);
2055 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
2056 write_chars (buffer + sizeof (buffer) - count, count);
2059 /* Write out an integral CST in decimal. Most numbers are small, and
2060 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
2061 bigger than that, which we must deal with. */
2063 static inline void
2064 write_integer_cst (const tree cst)
2066 int sign = tree_int_cst_sgn (cst);
2067 widest_int abs_value = wi::abs (wi::to_widest (cst));
2068 if (!wi::fits_uhwi_p (abs_value))
2070 /* A bignum. We do this in chunks, each of which fits in a
2071 HOST_WIDE_INT. */
2072 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
2073 unsigned HOST_WIDE_INT chunk;
2074 unsigned chunk_digits;
2075 char *ptr = buffer + sizeof (buffer);
2076 unsigned count = 0;
2077 tree n, base, type;
2078 int done;
2080 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
2081 representable. */
2082 chunk = 1000000000;
2083 chunk_digits = 9;
2085 if (sizeof (HOST_WIDE_INT) >= 8)
2087 /* It is at least 64 bits, so 10^18 is representable. */
2088 chunk_digits = 18;
2089 chunk *= chunk;
2092 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
2093 base = build_int_cstu (type, chunk);
2094 n = wide_int_to_tree (type, wi::to_wide (cst));
2096 if (sign < 0)
2098 write_char ('n');
2099 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
2103 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
2104 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
2105 unsigned c;
2107 done = integer_zerop (d);
2108 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
2109 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
2110 done ? 1 : chunk_digits);
2111 ptr -= c;
2112 count += c;
2113 n = d;
2115 while (!done);
2116 write_chars (ptr, count);
2118 else
2120 /* A small num. */
2121 if (sign < 0)
2122 write_char ('n');
2123 write_unsigned_number (abs_value.to_uhwi ());
2127 /* Write out a floating-point literal.
2129 "Floating-point literals are encoded using the bit pattern of the
2130 target processor's internal representation of that number, as a
2131 fixed-length lowercase hexadecimal string, high-order bytes first
2132 (even if the target processor would store low-order bytes first).
2133 The "n" prefix is not used for floating-point literals; the sign
2134 bit is encoded with the rest of the number.
2136 Here are some examples, assuming the IEEE standard representation
2137 for floating point numbers. (Spaces are for readability, not
2138 part of the encoding.)
2140 1.0f Lf 3f80 0000 E
2141 -1.0f Lf bf80 0000 E
2142 1.17549435e-38f Lf 0080 0000 E
2143 1.40129846e-45f Lf 0000 0001 E
2144 0.0f Lf 0000 0000 E"
2146 Caller is responsible for the Lx and the E. */
2147 static void
2148 write_real_cst (const tree value)
2150 long target_real[4]; /* largest supported float */
2151 /* Buffer for eight hex digits in a 32-bit number but big enough
2152 even for 64-bit long to avoid warnings. */
2153 char buffer[17];
2154 int i, limit, dir;
2156 tree type = TREE_TYPE (value);
2157 int words = GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type)) / 32;
2159 real_to_target (target_real, &TREE_REAL_CST (value),
2160 TYPE_MODE (type));
2162 /* The value in target_real is in the target word order,
2163 so we must write it out backward if that happens to be
2164 little-endian. write_number cannot be used, it will
2165 produce uppercase. */
2166 if (FLOAT_WORDS_BIG_ENDIAN)
2167 i = 0, limit = words, dir = 1;
2168 else
2169 i = words - 1, limit = -1, dir = -1;
2171 for (; i != limit; i += dir)
2173 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
2174 write_chars (buffer, 8);
2178 /* Non-terminal <identifier>.
2180 <identifier> ::= </unqualified source code identifier> */
2182 static void
2183 write_identifier (const char *identifier)
2185 MANGLE_TRACE ("identifier", identifier);
2186 write_string (identifier);
2189 /* Handle constructor productions of non-terminal <special-name>.
2190 CTOR is a constructor FUNCTION_DECL.
2192 <special-name> ::= C1 # complete object constructor
2193 ::= C2 # base object constructor
2194 ::= C3 # complete object allocating constructor
2196 Currently, allocating constructors are never used. */
2198 static void
2199 write_special_name_constructor (const tree ctor)
2201 write_char ('C');
2202 bool new_inh = (flag_new_inheriting_ctors
2203 && DECL_INHERITED_CTOR (ctor));
2204 if (new_inh)
2205 write_char ('I');
2206 if (DECL_BASE_CONSTRUCTOR_P (ctor))
2207 write_char ('2');
2208 /* This is the old-style "[unified]" constructor.
2209 In some cases, we may emit this function and call
2210 it from the clones in order to share code and save space. */
2211 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
2212 write_char ('4');
2213 else
2215 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
2216 write_char ('1');
2218 if (new_inh)
2219 write_type (DECL_INHERITED_CTOR_BASE (ctor));
2222 /* Handle destructor productions of non-terminal <special-name>.
2223 DTOR is a destructor FUNCTION_DECL.
2225 <special-name> ::= D0 # deleting (in-charge) destructor
2226 ::= D1 # complete object (in-charge) destructor
2227 ::= D2 # base object (not-in-charge) destructor */
2229 static void
2230 write_special_name_destructor (const tree dtor)
2232 if (DECL_DELETING_DESTRUCTOR_P (dtor))
2233 write_string ("D0");
2234 else if (DECL_BASE_DESTRUCTOR_P (dtor))
2235 write_string ("D2");
2236 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
2237 /* This is the old-style "[unified]" destructor.
2238 In some cases, we may emit this function and call
2239 it from the clones in order to share code and save space. */
2240 write_string ("D4");
2241 else
2243 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
2244 write_string ("D1");
2248 /* Return the discriminator for ENTITY appearing inside
2249 FUNCTION. The discriminator is the lexical ordinal of VAR or TYPE among
2250 entities with the same name and kind in the same FUNCTION. */
2252 static int
2253 discriminator_for_local_entity (tree entity)
2255 if (!DECL_LANG_SPECIFIC (entity))
2257 /* Some decls, like __FUNCTION__, don't need a discriminator. */
2258 gcc_checking_assert (DECL_ARTIFICIAL (entity));
2259 return 0;
2261 else if (tree disc = DECL_DISCRIMINATOR (entity))
2262 return TREE_INT_CST_LOW (disc);
2263 else
2264 /* The first entity with a particular name doesn't get
2265 DECL_DISCRIMINATOR set up. */
2266 return 0;
2269 /* Return the discriminator for STRING, a string literal used inside
2270 FUNCTION. The discriminator is the lexical ordinal of STRING among
2271 string literals used in FUNCTION. */
2273 static int
2274 discriminator_for_string_literal (tree /*function*/,
2275 tree /*string*/)
2277 /* For now, we don't discriminate amongst string literals. */
2278 return 0;
2281 /* <discriminator> := _ <number> # when number < 10
2282 := __ <number> _ # when number >= 10
2284 The discriminator is used only for the second and later occurrences
2285 of the same name within a single function. In this case <number> is
2286 n - 2, if this is the nth occurrence, in lexical order. */
2288 static void
2289 write_discriminator (const int discriminator)
2291 /* If discriminator is zero, don't write anything. Otherwise... */
2292 if (discriminator > 0)
2294 write_char ('_');
2295 if (discriminator - 1 >= 10)
2297 if (abi_check (11))
2298 write_char ('_');
2300 write_unsigned_number (discriminator - 1);
2301 if (abi_version_at_least (11) && discriminator - 1 >= 10)
2302 write_char ('_');
2306 /* Mangle the name of a function-scope entity. FUNCTION is the
2307 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
2308 default argument scope. ENTITY is the decl for the entity itself.
2309 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
2310 either ENTITY itself or an enclosing scope of ENTITY.
2312 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2313 := Z <function encoding> E s [<discriminator>]
2314 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
2316 static void
2317 write_local_name (tree function, const tree local_entity,
2318 const tree entity)
2320 tree parm = NULL_TREE;
2322 MANGLE_TRACE_TREE ("local-name", entity);
2324 if (TREE_CODE (function) == PARM_DECL)
2326 parm = function;
2327 function = DECL_CONTEXT (parm);
2330 write_char ('Z');
2331 write_encoding (function);
2332 write_char ('E');
2334 /* For this purpose, parameters are numbered from right-to-left. */
2335 if (parm)
2337 int i = list_length (parm);
2338 write_char ('d');
2339 write_compact_number (i - 1);
2342 if (TREE_CODE (entity) == STRING_CST)
2344 write_char ('s');
2345 write_discriminator (discriminator_for_string_literal (function,
2346 entity));
2348 else
2350 /* Now the <entity name>. Let write_name know its being called
2351 from <local-name>, so it doesn't try to process the enclosing
2352 function scope again. */
2353 write_name (entity, /*ignore_local_scope=*/1);
2354 if (DECL_DISCRIMINATOR_P (local_entity)
2355 && !(TREE_CODE (local_entity) == TYPE_DECL
2356 && TYPE_ANON_P (TREE_TYPE (local_entity))))
2357 write_discriminator (discriminator_for_local_entity (local_entity));
2361 /* Non-terminals <type> and <CV-qualifier>.
2363 <type> ::= <builtin-type>
2364 ::= <function-type>
2365 ::= <class-enum-type>
2366 ::= <array-type>
2367 ::= <pointer-to-member-type>
2368 ::= <template-param>
2369 ::= <substitution>
2370 ::= <CV-qualifier>
2371 ::= P <type> # pointer-to
2372 ::= R <type> # reference-to
2373 ::= C <type> # complex pair (C 2000)
2374 ::= G <type> # imaginary (C 2000) [not supported]
2375 ::= U <source-name> <type> # vendor extended type qualifier
2377 C++0x extensions
2379 <type> ::= RR <type> # rvalue reference-to
2380 <type> ::= Dt <expression> # decltype of an id-expression or
2381 # class member access
2382 <type> ::= DT <expression> # decltype of an expression
2383 <type> ::= Dn # decltype of nullptr
2385 TYPE is a type node. */
2387 static void
2388 write_type (tree type)
2390 /* This gets set to nonzero if TYPE turns out to be a (possibly
2391 CV-qualified) builtin type. */
2392 int is_builtin_type = 0;
2394 MANGLE_TRACE_TREE ("type", type);
2396 if (type == error_mark_node)
2397 return;
2399 type = canonicalize_for_substitution (type);
2400 if (find_substitution (type))
2401 return;
2404 if (write_CV_qualifiers_for_type (type) > 0)
2405 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
2406 mangle the unqualified type. The recursive call is needed here
2407 since both the qualified and unqualified types are substitution
2408 candidates. */
2410 tree t = TYPE_MAIN_VARIANT (type);
2411 if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t))
2413 tree attrs = NULL_TREE;
2414 if (tx_safe_fn_type_p (type))
2415 attrs = tree_cons (get_identifier ("transaction_safe"),
2416 NULL_TREE, attrs);
2417 t = cp_build_type_attribute_variant (t, attrs);
2419 gcc_assert (t != type);
2420 if (FUNC_OR_METHOD_TYPE_P (t))
2422 t = build_ref_qualified_type (t, type_memfn_rqual (type));
2423 if (flag_noexcept_type)
2425 tree r = TYPE_RAISES_EXCEPTIONS (type);
2426 t = build_exception_variant (t, r);
2428 if (abi_version_at_least (8)
2429 || type == TYPE_MAIN_VARIANT (type))
2430 /* Avoid adding the unqualified function type as a substitution. */
2431 write_function_type (t);
2432 else
2433 write_type (t);
2434 if (abi_warn_or_compat_version_crosses (8))
2435 G.need_abi_warning = 1;
2437 else
2438 write_type (t);
2440 else if (TREE_CODE (type) == ARRAY_TYPE)
2441 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
2442 so that the cv-qualification of the element type is available
2443 in write_array_type. */
2444 write_array_type (type);
2445 else
2447 tree type_orig = type;
2449 /* See through any typedefs. */
2450 type = TYPE_MAIN_VARIANT (type);
2451 if (FUNC_OR_METHOD_TYPE_P (type))
2452 type = cxx_copy_lang_qualifiers (type, type_orig);
2454 /* According to the C++ ABI, some library classes are passed the
2455 same as the scalar type of their single member and use the same
2456 mangling. */
2457 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2458 type = TREE_TYPE (first_field (type));
2460 if (TYPE_PTRDATAMEM_P (type))
2461 write_pointer_to_member_type (type);
2462 else
2464 /* Handle any target-specific fundamental types. */
2465 const char *target_mangling
2466 = targetm.mangle_type (type_orig);
2468 if (target_mangling)
2470 write_string (target_mangling);
2471 /* Add substitutions for types other than fundamental
2472 types. */
2473 if (!VOID_TYPE_P (type)
2474 && TREE_CODE (type) != INTEGER_TYPE
2475 && TREE_CODE (type) != REAL_TYPE
2476 && TREE_CODE (type) != BOOLEAN_TYPE)
2477 add_substitution (type);
2478 return;
2481 switch (TREE_CODE (type))
2483 case VOID_TYPE:
2484 case BOOLEAN_TYPE:
2485 case INTEGER_TYPE: /* Includes wchar_t. */
2486 case REAL_TYPE:
2487 case FIXED_POINT_TYPE:
2489 /* If this is a typedef, TYPE may not be one of
2490 the standard builtin type nodes, but an alias of one. Use
2491 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
2492 write_builtin_type (TYPE_MAIN_VARIANT (type));
2493 ++is_builtin_type;
2495 break;
2497 case COMPLEX_TYPE:
2498 write_char ('C');
2499 write_type (TREE_TYPE (type));
2500 break;
2502 case FUNCTION_TYPE:
2503 case METHOD_TYPE:
2504 write_function_type (type);
2505 break;
2507 case UNION_TYPE:
2508 case RECORD_TYPE:
2509 case ENUMERAL_TYPE:
2510 /* A pointer-to-member function is represented as a special
2511 RECORD_TYPE, so check for this first. */
2512 if (TYPE_PTRMEMFUNC_P (type))
2513 write_pointer_to_member_type (type);
2514 else
2515 write_class_enum_type (type);
2516 break;
2518 case TYPENAME_TYPE:
2519 case UNBOUND_CLASS_TEMPLATE:
2520 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2521 ordinary nested names. */
2522 write_nested_name (TYPE_STUB_DECL (type));
2523 break;
2525 case POINTER_TYPE:
2526 case REFERENCE_TYPE:
2527 if (TYPE_PTR_P (type))
2528 write_char ('P');
2529 else if (TYPE_REF_IS_RVALUE (type))
2530 write_char ('O');
2531 else
2532 write_char ('R');
2534 tree target = TREE_TYPE (type);
2535 /* Attribute const/noreturn are not reflected in mangling.
2536 We strip them here rather than at a lower level because
2537 a typedef or template argument can have function type
2538 with function-cv-quals (that use the same representation),
2539 but you can't have a pointer/reference to such a type. */
2540 if (TREE_CODE (target) == FUNCTION_TYPE)
2542 if (abi_warn_or_compat_version_crosses (5)
2543 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2544 G.need_abi_warning = 1;
2545 if (abi_version_at_least (5))
2546 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2548 write_type (target);
2550 break;
2552 case TEMPLATE_TYPE_PARM:
2553 if (is_auto (type))
2555 if (template_placeholder_p (type)
2556 && abi_check (19))
2558 /* ABI #109: placeholder is mangled as its template. */
2559 type = CLASS_PLACEHOLDER_TEMPLATE (type);
2560 if (find_substitution (type))
2561 return;
2562 write_name (type, 0);
2563 break;
2565 if (AUTO_IS_DECLTYPE (type))
2566 write_identifier ("Dc");
2567 else
2568 write_identifier ("Da");
2569 ++is_builtin_type;
2570 break;
2572 /* fall through. */
2573 case TEMPLATE_PARM_INDEX:
2574 write_template_param (type);
2575 break;
2577 case TEMPLATE_TEMPLATE_PARM:
2578 write_template_template_param (type);
2579 break;
2581 case BOUND_TEMPLATE_TEMPLATE_PARM:
2582 write_template_template_param (type);
2583 write_template_args
2584 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2585 break;
2587 case VECTOR_TYPE:
2588 if (abi_version_at_least (4))
2590 write_string ("Dv");
2591 /* Non-constant vector size would be encoded with
2592 _ expression, but we don't support that yet. */
2593 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type)
2594 .to_constant ());
2595 write_char ('_');
2597 else
2598 write_string ("U8__vector");
2599 if (abi_warn_or_compat_version_crosses (4))
2600 G.need_abi_warning = 1;
2601 write_type (TREE_TYPE (type));
2602 break;
2604 case TYPE_PACK_EXPANSION:
2605 write_string ("Dp");
2606 write_type (PACK_EXPANSION_PATTERN (type));
2607 break;
2609 case DECLTYPE_TYPE:
2610 /* These shouldn't make it into mangling. */
2611 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2612 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2614 /* In ABI <5, we stripped decltype of a plain decl. */
2615 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2617 tree expr = DECLTYPE_TYPE_EXPR (type);
2618 tree etype = NULL_TREE;
2619 switch (TREE_CODE (expr))
2621 case VAR_DECL:
2622 case PARM_DECL:
2623 case RESULT_DECL:
2624 case FUNCTION_DECL:
2625 case CONST_DECL:
2626 case TEMPLATE_PARM_INDEX:
2627 etype = TREE_TYPE (expr);
2628 break;
2630 default:
2631 break;
2634 if (etype && !type_uses_auto (etype))
2636 if (!abi_check (5))
2638 write_type (etype);
2639 return;
2644 write_char ('D');
2645 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2646 write_char ('t');
2647 else
2648 write_char ('T');
2649 ++cp_unevaluated_operand;
2650 write_expression (DECLTYPE_TYPE_EXPR (type));
2651 --cp_unevaluated_operand;
2652 write_char ('E');
2653 break;
2655 case NULLPTR_TYPE:
2656 write_string ("Dn");
2657 if (abi_check (7))
2658 ++is_builtin_type;
2659 break;
2661 case TYPEOF_TYPE:
2662 sorry ("mangling %<typeof%>, use %<decltype%> instead");
2663 break;
2665 case TRAIT_TYPE:
2666 error ("use of built-in trait %qT in function signature; "
2667 "use library traits instead", type);
2668 break;
2670 case LANG_TYPE:
2671 /* fall through. */
2673 default:
2674 gcc_unreachable ();
2679 /* Types other than builtin types are substitution candidates. */
2680 if (!is_builtin_type)
2681 add_substitution (type);
2684 /* qsort callback for sorting a vector of attribute entries. */
2686 static int
2687 attr_strcmp (const void *p1, const void *p2)
2689 tree a1 = *(const tree*)p1;
2690 tree a2 = *(const tree*)p2;
2692 const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1));
2693 const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2));
2695 return strcmp (as1->name, as2->name);
2698 /* Return true if we should mangle a type attribute with name NAME. */
2700 static bool
2701 mangle_type_attribute_p (tree name)
2703 const attribute_spec *as = lookup_attribute_spec (name);
2704 if (!as || !as->affects_type_identity)
2705 return false;
2707 /* Skip internal-only attributes, which are distinguished from others
2708 by having a space. At present, all internal-only attributes that
2709 affect type identity are target-specific and are handled by
2710 targetm.mangle_type instead.
2712 Another reason to do this is that a space isn't a valid identifier
2713 character for most file formats. */
2714 if (strchr (IDENTIFIER_POINTER (name), ' '))
2715 return false;
2717 /* The following attributes are mangled specially. */
2718 if (is_attribute_p ("transaction_safe", name))
2719 return false;
2720 if (is_attribute_p ("abi_tag", name))
2721 return false;
2723 return true;
2726 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2727 CV-qualifiers written for TYPE.
2729 <CV-qualifiers> ::= [r] [V] [K] */
2731 static int
2732 write_CV_qualifiers_for_type (const tree type)
2734 int num_qualifiers = 0;
2736 /* The order is specified by:
2738 "In cases where multiple order-insensitive qualifiers are
2739 present, they should be ordered 'K' (closest to the base type),
2740 'V', 'r', and 'U' (farthest from the base type) ..." */
2742 /* Mangle attributes that affect type identity as extended qualifiers.
2744 We don't do this with classes and enums because their attributes
2745 are part of their definitions, not something added on. */
2747 if (!OVERLOAD_TYPE_P (type))
2749 auto_vec<tree> vec;
2750 for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
2751 if (mangle_type_attribute_p (get_attribute_name (a)))
2752 vec.safe_push (a);
2753 if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
2754 G.need_abi_warning = true;
2755 if (abi_version_at_least (10))
2757 vec.qsort (attr_strcmp);
2758 while (!vec.is_empty())
2760 tree a = vec.pop();
2761 const attribute_spec *as
2762 = lookup_attribute_spec (get_attribute_name (a));
2764 write_char ('U');
2765 write_unsigned_number (strlen (as->name));
2766 write_string (as->name);
2767 if (TREE_VALUE (a))
2769 write_char ('I');
2770 for (tree args = TREE_VALUE (a); args;
2771 args = TREE_CHAIN (args))
2773 tree arg = TREE_VALUE (args);
2774 write_template_arg (arg);
2776 write_char ('E');
2779 ++num_qualifiers;
2784 /* Note that we do not use cp_type_quals below; given "const
2785 int[3]", the "const" is emitted with the "int", not with the
2786 array. */
2787 cp_cv_quals quals = TYPE_QUALS (type);
2789 if (quals & TYPE_QUAL_RESTRICT)
2791 write_char ('r');
2792 ++num_qualifiers;
2794 if (quals & TYPE_QUAL_VOLATILE)
2796 write_char ('V');
2797 ++num_qualifiers;
2799 if (quals & TYPE_QUAL_CONST)
2801 write_char ('K');
2802 ++num_qualifiers;
2805 return num_qualifiers;
2808 /* Non-terminal <builtin-type>.
2810 <builtin-type> ::= v # void
2811 ::= b # bool
2812 ::= w # wchar_t
2813 ::= c # char
2814 ::= a # signed char
2815 ::= h # unsigned char
2816 ::= s # short
2817 ::= t # unsigned short
2818 ::= i # int
2819 ::= j # unsigned int
2820 ::= l # long
2821 ::= m # unsigned long
2822 ::= x # long long, __int64
2823 ::= y # unsigned long long, __int64
2824 ::= n # __int128
2825 ::= o # unsigned __int128
2826 ::= f # float
2827 ::= d # double
2828 ::= e # long double, __float80
2829 ::= g # __float128 [not supported]
2830 ::= u <source-name> # vendor extended type */
2832 static void
2833 write_builtin_type (tree type)
2835 if (TYPE_CANONICAL (type))
2836 type = TYPE_CANONICAL (type);
2838 switch (TREE_CODE (type))
2840 case VOID_TYPE:
2841 write_char ('v');
2842 break;
2844 case BOOLEAN_TYPE:
2845 write_char ('b');
2846 break;
2848 case INTEGER_TYPE:
2849 /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
2850 isn't in integer_type_nodes. */
2851 if (type == wchar_type_node)
2852 write_char ('w');
2853 else if (type == char8_type_node)
2854 write_string ("Du");
2855 else if (type == char16_type_node)
2856 write_string ("Ds");
2857 else if (type == char32_type_node)
2858 write_string ("Di");
2859 else
2861 size_t itk;
2862 /* Assume TYPE is one of the shared integer type nodes. Find
2863 it in the array of these nodes. */
2864 iagain:
2865 for (itk = 0; itk < itk_none; ++itk)
2866 if (integer_types[itk] != NULL_TREE
2867 && integer_type_codes[itk] != '\0'
2868 && type == integer_types[itk])
2870 /* Print the corresponding single-letter code. */
2871 write_char (integer_type_codes[itk]);
2872 break;
2875 if (itk == itk_none)
2877 tree t = c_common_type_for_mode (TYPE_MODE (type),
2878 TYPE_UNSIGNED (type));
2879 if (type != t)
2881 type = t;
2882 goto iagain;
2885 if (TYPE_PRECISION (type) == 128)
2886 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2887 else
2889 /* Allow for cases where TYPE is not one of the shared
2890 integer type nodes and write a "vendor extended builtin
2891 type" with a name the form intN or uintN, respectively.
2892 Situations like this can happen if you have an
2893 __attribute__((__mode__(__SI__))) type and use exotic
2894 switches like '-mint8' on AVR. Of course, this is
2895 undefined by the C++ ABI (and '-mint8' is not even
2896 Standard C conforming), but when using such special
2897 options you're pretty much in nowhere land anyway. */
2898 const char *prefix;
2899 char prec[11]; /* up to ten digits for an unsigned */
2901 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2902 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2903 write_char ('u'); /* "vendor extended builtin type" */
2904 write_unsigned_number (strlen (prefix) + strlen (prec));
2905 write_string (prefix);
2906 write_string (prec);
2910 break;
2912 case REAL_TYPE:
2913 if (type == float_type_node)
2914 write_char ('f');
2915 else if (type == double_type_node)
2916 write_char ('d');
2917 else if (type == long_double_type_node)
2918 write_char ('e');
2919 else if (type == dfloat32_type_node)
2920 write_string ("Df");
2921 else if (type == dfloat64_type_node)
2922 write_string ("Dd");
2923 else if (type == dfloat128_type_node)
2924 write_string ("De");
2925 else if (type == float16_type_node)
2926 write_string ("DF16_");
2927 else if (type == float32_type_node)
2928 write_string ("DF32_");
2929 else if (type == float64_type_node)
2930 write_string ("DF64_");
2931 else if (type == float128_type_node)
2932 write_string ("DF128_");
2933 else if (type == float32x_type_node)
2934 write_string ("DF32x");
2935 else if (type == float64x_type_node)
2936 write_string ("DF64x");
2937 else if (type == float128x_type_node)
2938 write_string ("DF128x");
2939 else if (type == bfloat16_type_node)
2940 write_string ("DF16b");
2941 else
2942 gcc_unreachable ();
2943 break;
2945 default:
2946 gcc_unreachable ();
2950 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2951 METHOD_TYPE. The return type is mangled before the parameter
2952 types.
2954 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2956 static void
2957 write_function_type (const tree type)
2959 MANGLE_TRACE_TREE ("function-type", type);
2961 /* For a pointer to member function, the function type may have
2962 cv-qualifiers, indicating the quals for the artificial 'this'
2963 parameter. */
2964 if (TREE_CODE (type) == METHOD_TYPE)
2966 /* The first parameter must be a POINTER_TYPE pointing to the
2967 `this' parameter. */
2968 tree this_type = class_of_this_parm (type);
2969 write_CV_qualifiers_for_type (this_type);
2972 write_exception_spec (TYPE_RAISES_EXCEPTIONS (type));
2974 if (tx_safe_fn_type_p (type))
2975 write_string ("Dx");
2977 write_char ('F');
2978 /* We don't track whether or not a type is `extern "C"'. Note that
2979 you can have an `extern "C"' function that does not have
2980 `extern "C"' type, and vice versa:
2982 extern "C" typedef void function_t();
2983 function_t f; // f has C++ linkage, but its type is
2984 // `extern "C"'
2986 typedef void function_t();
2987 extern "C" function_t f; // Vice versa.
2989 See [dcl.link]. */
2990 write_bare_function_type (type, /*include_return_type_p=*/1,
2991 /*decl=*/NULL);
2992 if (FUNCTION_REF_QUALIFIED (type))
2994 if (FUNCTION_RVALUE_QUALIFIED (type))
2995 write_char ('O');
2996 else
2997 write_char ('R');
2999 write_char ('E');
3002 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
3003 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
3004 is mangled before the parameter types. If non-NULL, DECL is
3005 FUNCTION_DECL for the function whose type is being emitted. */
3007 static void
3008 write_bare_function_type (const tree type, const int include_return_type_p,
3009 const tree decl)
3011 MANGLE_TRACE_TREE ("bare-function-type", type);
3013 /* Mangle the return type, if requested. */
3014 if (include_return_type_p)
3015 write_type (TREE_TYPE (type));
3017 /* Now mangle the types of the arguments. */
3018 ++G.parm_depth;
3019 write_method_parms (TYPE_ARG_TYPES (type),
3020 TREE_CODE (type) == METHOD_TYPE,
3021 decl);
3022 --G.parm_depth;
3025 /* Write the mangled representation of a method parameter list of
3026 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
3027 considered a non-static method, and the this parameter is omitted.
3028 If non-NULL, DECL is the FUNCTION_DECL for the function whose
3029 parameters are being emitted. */
3031 static void
3032 write_method_parms (tree parm_types, const int method_p, const tree decl)
3034 tree first_parm_type;
3035 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
3037 /* Assume this parameter type list is variable-length. If it ends
3038 with a void type, then it's not. */
3039 int varargs_p = 1;
3041 /* If this is a member function, skip the first arg, which is the
3042 this pointer.
3043 "Member functions do not encode the type of their implicit this
3044 parameter."
3046 Similarly, there's no need to mangle artificial parameters, like
3047 the VTT parameters for constructors and destructors. */
3048 if (method_p)
3050 parm_types = TREE_CHAIN (parm_types);
3051 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
3053 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
3055 parm_types = TREE_CHAIN (parm_types);
3056 parm_decl = DECL_CHAIN (parm_decl);
3059 if (decl && ctor_omit_inherited_parms (decl))
3060 /* Bring back parameters omitted from an inherited ctor. */
3061 parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl));
3064 for (first_parm_type = parm_types;
3065 parm_types;
3066 parm_types = TREE_CHAIN (parm_types))
3068 tree parm = TREE_VALUE (parm_types);
3069 if (parm == void_type_node)
3071 /* "Empty parameter lists, whether declared as () or
3072 conventionally as (void), are encoded with a void parameter
3073 (v)." */
3074 if (parm_types == first_parm_type)
3075 write_type (parm);
3076 /* If the parm list is terminated with a void type, it's
3077 fixed-length. */
3078 varargs_p = 0;
3079 /* A void type better be the last one. */
3080 gcc_assert (TREE_CHAIN (parm_types) == NULL);
3082 else
3083 write_type (parm);
3086 if (varargs_p)
3087 /* <builtin-type> ::= z # ellipsis */
3088 write_char ('z');
3091 /* <class-enum-type> ::= <name> */
3093 static void
3094 write_class_enum_type (const tree type)
3096 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
3099 /* Mangle a requirement REQ in a requires-expression. */
3101 static void
3102 write_requirement (tree req)
3104 tree op = TREE_OPERAND (req, 0);
3106 switch (tree_code code = TREE_CODE (req))
3108 /* # simple-requirement or compound-requirement
3109 <requirement> ::= X <expression> [ N ] [ R <type-constraint> ] */
3110 case SIMPLE_REQ:
3111 case COMPOUND_REQ:
3112 write_char ('X');
3113 write_expression (op);
3114 if (code == SIMPLE_REQ)
3115 break;
3116 if (COMPOUND_REQ_NOEXCEPT_P (req))
3117 write_char ('N');
3118 if (tree constr = TREE_OPERAND (req, 1))
3120 write_char ('R');
3121 write_type_constraint (PLACEHOLDER_TYPE_CONSTRAINTS (constr));
3123 break;
3125 /* <requirement> ::= T <type> # type-requirement */
3126 case TYPE_REQ:
3127 write_char ('T');
3128 write_type (op);
3129 break;
3131 /* <requirement> ::= Q <constraint-expression> # nested-requirement */
3132 case NESTED_REQ:
3133 write_char ('Q');
3134 write_constraint_expression (op);
3135 break;
3137 default:
3138 gcc_unreachable ();
3142 /* # requires { ... }
3143 <expression> ::= rq <requirement>+ E
3144 # requires (...) { ... }
3145 <expression> ::= rQ <bare-function-type> _ <requirement>+ E */
3147 static void
3148 write_requires_expr (tree expr)
3150 tree parms = REQUIRES_EXPR_PARMS (expr);
3151 if (parms)
3153 write_string ("rQ");
3154 ++G.parm_depth;
3155 for (; parms; parms = DECL_CHAIN (parms))
3156 write_type (cv_unqualified (TREE_TYPE (parms)));
3157 --G.parm_depth;
3158 write_char ('_');
3160 else
3161 write_string ("rq");
3163 for (tree reqs = REQUIRES_EXPR_REQS (expr); reqs;
3164 reqs = TREE_CHAIN (reqs))
3165 write_requirement (TREE_VALUE (reqs));
3167 write_char ('E');
3170 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
3171 arguments.
3173 <template-args> ::= I <template-arg>* [Q <constraint-expr>] E */
3175 static void
3176 write_template_args (tree args, tree parms /*= NULL_TREE*/)
3178 int i;
3179 int length = 0;
3181 MANGLE_TRACE_TREE ("template-args", args);
3183 write_char ('I');
3185 if (args)
3186 length = TREE_VEC_LENGTH (args);
3188 tree constraints = NULL_TREE;
3189 if (parms)
3191 constraints = TEMPLATE_PARMS_CONSTRAINTS (parms);
3192 parms = INNERMOST_TEMPLATE_PARMS (parms);
3195 if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
3197 /* We have nested template args. We want the innermost template
3198 argument list. */
3199 args = TREE_VEC_ELT (args, length - 1);
3200 length = TREE_VEC_LENGTH (args);
3202 if (TEMPLATE_ARGS_TYPE_CONSTRAINT_P (args))
3203 /* Skip the constrained type. */
3204 i = 1;
3205 else
3206 i = 0;
3207 bool implicit_parm_scope = false;
3208 for (; i < length; ++i)
3210 tree arg = TREE_VEC_ELT (args, i);
3211 if (parms)
3213 tree parm = TREE_VEC_ELT (parms, i);
3214 tree decl = TREE_VALUE (parm);
3215 if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl)
3216 && !implicit_parm_scope)
3218 /* The rest of the template parameters are based on generic
3219 function parameters, so any expressions in their
3220 type-constraints are in parameter scope. */
3221 implicit_parm_scope = true;
3222 ++G.parm_depth;
3224 if (!template_parm_natural_p (arg, parm)
3225 && abi_check (19))
3226 write_template_param_decl (parm);
3228 write_template_arg (arg);
3230 if (implicit_parm_scope)
3231 --G.parm_depth;
3233 write_tparms_constraints (constraints);
3235 write_char ('E');
3238 /* Write out the
3239 <unqualified-name>
3240 <unqualified-name> <template-args>
3241 part of SCOPE_REF or COMPONENT_REF mangling. */
3243 static void
3244 write_member_name (tree member)
3246 if (identifier_p (member))
3248 if (IDENTIFIER_ANY_OP_P (member))
3250 if (abi_check (11))
3251 write_string ("on");
3253 write_unqualified_id (member);
3255 else if (DECL_P (member))
3257 gcc_assert (!DECL_OVERLOADED_OPERATOR_P (member));
3258 write_unqualified_name (member);
3260 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
3262 tree name = TREE_OPERAND (member, 0);
3263 name = OVL_FIRST (name);
3264 write_member_name (name);
3265 write_template_args (TREE_OPERAND (member, 1));
3267 else
3268 write_expression (member);
3271 /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for
3272 converting to BASE, or just the conversion of EXPR if BASE is null.
3274 "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base
3275 path Min(P) is defined as follows: let C_i be the last element for which the
3276 conversion to C_0 is unambiguous; if that element is C_n, the minimized path
3277 is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) ->
3278 C_0."
3280 We mangle the conversion to C_i if it's different from C_n. */
3282 static bool
3283 write_base_ref (tree expr, tree base = NULL_TREE)
3285 if (TREE_CODE (expr) != COMPONENT_REF)
3286 return false;
3288 tree field = TREE_OPERAND (expr, 1);
3290 if (TREE_CODE (field) != FIELD_DECL || !DECL_FIELD_IS_BASE (field))
3291 return false;
3293 tree object = TREE_OPERAND (expr, 0);
3295 tree binfo = NULL_TREE;
3296 if (base)
3298 tree cur = TREE_TYPE (object);
3299 binfo = lookup_base (cur, base, ba_unique, NULL, tf_none);
3301 else
3302 /* We're at the end of the base conversion chain, so it can't be
3303 ambiguous. */
3304 base = TREE_TYPE (field);
3306 if (binfo == error_mark_node)
3308 /* cur->base is ambiguous, so make the conversion to
3309 last explicit, expressed as a cast (last&)object. */
3310 tree last = TREE_TYPE (expr);
3311 write_string (OVL_OP_INFO (false, CAST_EXPR)->mangled_name);
3312 write_type (build_reference_type (last));
3313 write_expression (object);
3315 else if (write_base_ref (object, base))
3316 /* cur->base is unambiguous, but we had another base conversion
3317 underneath and wrote it out. */;
3318 else
3319 /* No more base conversions, just write out the object. */
3320 write_expression (object);
3322 return true;
3325 /* The number of elements spanned by a RANGE_EXPR. */
3327 unsigned HOST_WIDE_INT
3328 range_expr_nelts (tree expr)
3330 tree lo = TREE_OPERAND (expr, 0);
3331 tree hi = TREE_OPERAND (expr, 1);
3332 return tree_to_uhwi (hi) - tree_to_uhwi (lo) + 1;
3335 /* <expression> ::= <unary operator-name> <expression>
3336 ::= <binary operator-name> <expression> <expression>
3337 ::= <expr-primary>
3339 <expr-primary> ::= <template-param>
3340 ::= L <type> <value number> E # literal
3341 ::= L <mangled-name> E # external name
3342 ::= st <type> # sizeof
3343 ::= sr <type> <unqualified-name> # dependent name
3344 ::= sr <type> <unqualified-name> <template-args> */
3346 static void
3347 write_expression (tree expr)
3349 enum tree_code code = TREE_CODE (expr);
3351 if (TREE_CODE (expr) == TARGET_EXPR)
3353 expr = TARGET_EXPR_INITIAL (expr);
3354 code = TREE_CODE (expr);
3357 /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer
3358 argument is converted (via qualification conversions) to another type. */
3359 while (CONVERT_EXPR_CODE_P (code)
3360 || code == IMPLICIT_CONV_EXPR
3361 || location_wrapper_p (expr)
3362 /* Parentheses aren't mangled. */
3363 || code == PAREN_EXPR
3364 || code == NON_LVALUE_EXPR
3365 || (code == VIEW_CONVERT_EXPR
3366 && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
3368 expr = TREE_OPERAND (expr, 0);
3369 code = TREE_CODE (expr);
3372 if (code == BASELINK
3373 && (!type_unknown_p (expr)
3374 || !BASELINK_QUALIFIED_P (expr)))
3376 expr = BASELINK_FUNCTIONS (expr);
3377 code = TREE_CODE (expr);
3380 /* Handle pointers-to-members by making them look like expression
3381 nodes. */
3382 if (code == PTRMEM_CST)
3384 expr = build_nt (ADDR_EXPR,
3385 build_qualified_name (/*type=*/NULL_TREE,
3386 PTRMEM_CST_CLASS (expr),
3387 PTRMEM_CST_MEMBER (expr),
3388 /*template_p=*/false));
3389 code = TREE_CODE (expr);
3392 /* Handle template parameters. */
3393 if (code == TEMPLATE_TYPE_PARM
3394 || code == TEMPLATE_TEMPLATE_PARM
3395 || code == BOUND_TEMPLATE_TEMPLATE_PARM
3396 || code == TEMPLATE_PARM_INDEX)
3397 write_template_param (expr);
3398 /* Handle literals. */
3399 else if (TREE_CODE_CLASS (code) == tcc_constant
3400 || code == CONST_DECL)
3401 write_template_arg_literal (expr);
3402 else if (code == EXCESS_PRECISION_EXPR
3403 && TREE_CODE (TREE_OPERAND (expr, 0)) == REAL_CST)
3404 write_template_arg_literal (fold_convert (TREE_TYPE (expr),
3405 TREE_OPERAND (expr, 0)));
3406 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
3408 gcc_assert (id_equal (DECL_NAME (expr), "this"));
3409 write_string ("fpT");
3411 else if (code == PARM_DECL)
3413 /* A function parameter used in a late-specified return type. */
3414 int index = DECL_PARM_INDEX (expr);
3415 int level = DECL_PARM_LEVEL (expr);
3416 int delta = G.parm_depth - level + 1;
3417 gcc_assert (index >= 1);
3418 write_char ('f');
3419 if (delta != 0)
3421 gcc_checking_assert (delta > 0);
3422 if (abi_check (5))
3424 /* Let L be the number of function prototype scopes from the
3425 innermost one (in which the parameter reference occurs) up
3426 to (and including) the one containing the declaration of
3427 the referenced parameter. If the parameter declaration
3428 clause of the innermost function prototype scope has been
3429 completely seen, it is not counted (in that case -- which
3430 is perhaps the most common -- L can be zero). */
3431 write_char ('L');
3432 write_unsigned_number (delta - 1);
3435 write_char ('p');
3436 write_compact_number (index - 1);
3438 else if (DECL_P (expr))
3440 write_char ('L');
3441 write_mangled_name (expr, false);
3442 write_char ('E');
3444 else if (TREE_CODE (expr) == SIZEOF_EXPR)
3446 tree op = TREE_OPERAND (expr, 0);
3448 if (PACK_EXPANSION_P (op))
3450 sizeof_pack:
3451 if (abi_check (11))
3453 /* sZ rather than szDp. */
3454 write_string ("sZ");
3455 write_expression (PACK_EXPANSION_PATTERN (op));
3456 return;
3460 if (SIZEOF_EXPR_TYPE_P (expr))
3462 write_string ("st");
3463 write_type (TREE_TYPE (op));
3465 else if (ARGUMENT_PACK_P (op))
3467 tree args = ARGUMENT_PACK_ARGS (op);
3468 int length = TREE_VEC_LENGTH (args);
3469 if (abi_check (10))
3471 /* Before v19 we wrongly mangled all single pack expansions with
3472 sZ, but now only for expressions, as types ICEd (95298). */
3473 if (length == 1)
3475 tree arg = TREE_VEC_ELT (args, 0);
3476 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION
3477 && !abi_check (19))
3479 op = arg;
3480 goto sizeof_pack;
3484 /* sP <template-arg>* E # sizeof...(T), size of a captured
3485 template parameter pack from an alias template */
3486 write_string ("sP");
3487 for (int i = 0; i < length; ++i)
3488 write_template_arg (TREE_VEC_ELT (args, i));
3489 write_char ('E');
3491 else
3493 /* In GCC 5 we represented this sizeof wrong, with the effect
3494 that we mangled it as the last element of the pack. */
3495 tree arg = TREE_VEC_ELT (args, length-1);
3496 if (TYPE_P (op))
3498 write_string ("st");
3499 write_type (arg);
3501 else
3503 write_string ("sz");
3504 write_expression (arg);
3508 else if (TYPE_P (TREE_OPERAND (expr, 0)))
3510 write_string ("st");
3511 write_type (TREE_OPERAND (expr, 0));
3513 else
3514 goto normal_expr;
3516 else if (TREE_CODE (expr) == ALIGNOF_EXPR)
3518 if (!ALIGNOF_EXPR_STD_P (expr))
3520 if (abi_check (16))
3522 /* We used to mangle __alignof__ like alignof. */
3523 write_string ("u11__alignof__");
3524 write_template_arg (TREE_OPERAND (expr, 0));
3525 write_char ('E');
3526 return;
3529 if (TYPE_P (TREE_OPERAND (expr, 0)))
3531 write_string ("at");
3532 write_type (TREE_OPERAND (expr, 0));
3534 else
3535 goto normal_expr;
3537 else if (code == SCOPE_REF
3538 || code == BASELINK)
3540 tree scope, member;
3541 if (code == SCOPE_REF)
3543 scope = TREE_OPERAND (expr, 0);
3544 member = TREE_OPERAND (expr, 1);
3545 if (BASELINK_P (member))
3546 member = BASELINK_FUNCTIONS (member);
3548 else
3550 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
3551 member = BASELINK_FUNCTIONS (expr);
3554 /* If the MEMBER is a real declaration, then the qualifying
3555 scope was not dependent. Ideally, we would not have a
3556 SCOPE_REF in those cases, but sometimes we do. If the second
3557 argument is a DECL, then the name must not have been
3558 dependent. */
3559 if (DECL_P (member))
3560 write_expression (member);
3561 else
3563 gcc_assert (code != BASELINK || BASELINK_QUALIFIED_P (expr));
3564 write_string ("sr");
3565 write_type (scope);
3566 write_member_name (member);
3569 else if (INDIRECT_REF_P (expr)
3570 && TREE_TYPE (TREE_OPERAND (expr, 0))
3571 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
3573 write_expression (TREE_OPERAND (expr, 0));
3575 else if (identifier_p (expr))
3577 /* An operator name appearing as a dependent name needs to be
3578 specially marked to disambiguate between a use of the operator
3579 name and a use of the operator in an expression. */
3580 if (IDENTIFIER_ANY_OP_P (expr))
3581 write_string ("on");
3582 write_unqualified_id (expr);
3584 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
3586 tree fn = TREE_OPERAND (expr, 0);
3587 if (!identifier_p (fn))
3588 fn = OVL_NAME (fn);
3589 if (IDENTIFIER_ANY_OP_P (fn))
3590 write_string ("on");
3591 write_unqualified_id (fn);
3592 write_template_args (TREE_OPERAND (expr, 1));
3594 else if (TREE_CODE (expr) == MODOP_EXPR)
3596 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
3597 const char *name = OVL_OP_INFO (true, subop)->mangled_name;
3599 write_string (name);
3600 write_expression (TREE_OPERAND (expr, 0));
3601 write_expression (TREE_OPERAND (expr, 2));
3603 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
3605 /* ::= [gs] nw <expression>* _ <type> E
3606 ::= [gs] nw <expression>* _ <type> <initializer>
3607 ::= [gs] na <expression>* _ <type> E
3608 ::= [gs] na <expression>* _ <type> <initializer>
3609 <initializer> ::= pi <expression>* E */
3610 tree placement = TREE_OPERAND (expr, 0);
3611 tree type = TREE_OPERAND (expr, 1);
3612 tree nelts = TREE_OPERAND (expr, 2);
3613 tree init = TREE_OPERAND (expr, 3);
3614 tree t;
3616 gcc_assert (code == NEW_EXPR);
3617 if (TREE_OPERAND (expr, 2))
3618 code = VEC_NEW_EXPR;
3620 if (NEW_EXPR_USE_GLOBAL (expr))
3621 write_string ("gs");
3623 write_string (OVL_OP_INFO (false, code)->mangled_name);
3625 for (t = placement; t; t = TREE_CHAIN (t))
3626 write_expression (TREE_VALUE (t));
3628 write_char ('_');
3630 if (nelts)
3632 tree domain;
3633 ++processing_template_decl;
3634 domain = compute_array_index_type (NULL_TREE, nelts,
3635 tf_warning_or_error);
3636 type = build_cplus_array_type (type, domain);
3637 --processing_template_decl;
3639 write_type (type);
3641 if (init && TREE_CODE (init) == TREE_LIST
3642 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
3643 write_expression (TREE_VALUE (init));
3644 else
3646 if (init)
3647 write_string ("pi");
3648 if (init && init != void_node)
3649 for (t = init; t; t = TREE_CHAIN (t))
3650 write_expression (TREE_VALUE (t));
3651 write_char ('E');
3654 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
3656 gcc_assert (code == DELETE_EXPR);
3657 if (DELETE_EXPR_USE_VEC (expr))
3658 code = VEC_DELETE_EXPR;
3660 if (DELETE_EXPR_USE_GLOBAL (expr))
3661 write_string ("gs");
3663 write_string (OVL_OP_INFO (false, code)->mangled_name);
3665 write_expression (TREE_OPERAND (expr, 0));
3667 else if (code == THROW_EXPR)
3669 tree op = TREE_OPERAND (expr, 0);
3670 if (op)
3672 write_string ("tw");
3673 write_expression (op);
3675 else
3676 write_string ("tr");
3678 else if (code == NOEXCEPT_EXPR)
3680 write_string ("nx");
3681 write_expression (TREE_OPERAND (expr, 0));
3683 else if (code == CONSTRUCTOR)
3685 bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr);
3686 tree etype = TREE_TYPE (expr);
3688 if (braced_init)
3689 write_string ("il");
3690 else
3692 write_string ("tl");
3693 write_type (etype);
3696 /* If this is an undigested initializer, mangle it as written.
3697 COMPOUND_LITERAL_P doesn't actually distinguish between digested and
3698 undigested braced casts, but it should work to use it to distinguish
3699 between braced casts in a template signature (undigested) and template
3700 parm object values (digested), and all CONSTRUCTORS that get here
3701 should be one of those two cases. */
3702 bool undigested = braced_init || COMPOUND_LITERAL_P (expr);
3703 if (undigested || !zero_init_expr_p (expr))
3705 /* Convert braced initializer lists to STRING_CSTs so that
3706 A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
3707 still using the latter mangling for strings that
3708 originated as braced initializer lists. */
3709 expr = braced_lists_to_strings (etype, expr);
3711 if (TREE_CODE (expr) == CONSTRUCTOR)
3713 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
3714 unsigned last_nonzero = UINT_MAX;
3715 constructor_elt *ce;
3717 if (!undigested)
3718 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3719 if ((TREE_CODE (etype) == UNION_TYPE
3720 && ce->index != first_field (etype))
3721 || !zero_init_expr_p (ce->value))
3722 last_nonzero = i;
3724 if (undigested || last_nonzero != UINT_MAX)
3725 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3727 if (i > last_nonzero)
3728 break;
3729 if (!undigested && TREE_CODE (etype) == UNION_TYPE)
3731 /* Express the active member as a designator. */
3732 write_string ("di");
3733 write_unqualified_name (ce->index);
3735 unsigned reps = 1;
3736 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3737 reps = range_expr_nelts (ce->index);
3738 for (unsigned j = 0; j < reps; ++j)
3739 write_expression (ce->value);
3742 else
3744 gcc_assert (TREE_CODE (expr) == STRING_CST);
3745 write_expression (expr);
3748 write_char ('E');
3750 else if (code == LAMBDA_EXPR)
3752 /* [temp.over.link] Two lambda-expressions are never considered
3753 equivalent.
3755 So just use the closure type mangling. */
3756 write_string ("tl");
3757 write_type (LAMBDA_EXPR_CLOSURE (expr));
3758 write_char ('E');
3760 else if (code == REQUIRES_EXPR)
3761 write_requires_expr (expr);
3762 else if (dependent_name (expr))
3764 tree name = dependent_name (expr);
3765 if (IDENTIFIER_ANY_OP_P (name))
3767 if (abi_check (16))
3768 write_string ("on");
3770 write_unqualified_id (name);
3772 else
3774 normal_expr:
3775 int i, len;
3776 const char *name;
3778 /* When we bind a variable or function to a non-type template
3779 argument with reference type, we create an ADDR_EXPR to show
3780 the fact that the entity's address has been taken. But, we
3781 don't actually want to output a mangling code for the `&'. */
3782 if (TREE_CODE (expr) == ADDR_EXPR
3783 && TREE_TYPE (expr)
3784 && TYPE_REF_P (TREE_TYPE (expr)))
3786 expr = TREE_OPERAND (expr, 0);
3787 if (DECL_P (expr))
3789 write_expression (expr);
3790 return;
3793 code = TREE_CODE (expr);
3796 if (code == COMPONENT_REF)
3798 tree ob = TREE_OPERAND (expr, 0);
3800 if (TREE_CODE (ob) == ARROW_EXPR)
3802 write_string (OVL_OP_INFO (false, code)->mangled_name);
3803 ob = TREE_OPERAND (ob, 0);
3804 write_expression (ob);
3806 else if (write_base_ref (expr))
3807 return;
3808 else if (!is_dummy_object (ob))
3810 write_string ("dt");
3811 write_expression (ob);
3813 /* else, for a non-static data member with no associated object (in
3814 unevaluated context), use the unresolved-name mangling. */
3816 write_member_name (TREE_OPERAND (expr, 1));
3817 return;
3820 /* If it wasn't any of those, recursively expand the expression. */
3821 name = OVL_OP_INFO (false, code)->mangled_name;
3823 /* We used to mangle const_cast and static_cast like a C cast. */
3824 if (code == CONST_CAST_EXPR
3825 || code == STATIC_CAST_EXPR)
3827 if (!abi_check (6))
3828 name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name;
3831 if (name == NULL)
3833 switch (code)
3835 case TRAIT_EXPR:
3836 error ("use of built-in trait %qE in function signature; "
3837 "use library traits instead", expr);
3838 break;
3840 default:
3841 sorry ("mangling %C", code);
3842 break;
3844 return;
3846 else
3847 write_string (name);
3849 switch (code)
3851 case CALL_EXPR:
3853 tree fn = CALL_EXPR_FN (expr);
3855 if (TREE_CODE (fn) == ADDR_EXPR)
3856 fn = TREE_OPERAND (fn, 0);
3858 /* Mangle a dependent name as the name, not whatever happens to
3859 be the first function in the overload set. */
3860 if (OVL_P (fn)
3861 && type_dependent_expression_p_push (expr))
3862 fn = OVL_NAME (fn);
3864 write_expression (fn);
3867 for (i = 0; i < call_expr_nargs (expr); ++i)
3868 write_expression (CALL_EXPR_ARG (expr, i));
3869 write_char ('E');
3870 break;
3872 case CAST_EXPR:
3873 write_type (TREE_TYPE (expr));
3874 if (list_length (TREE_OPERAND (expr, 0)) == 1)
3875 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
3876 else
3878 tree args = TREE_OPERAND (expr, 0);
3879 write_char ('_');
3880 for (; args; args = TREE_CHAIN (args))
3881 write_expression (TREE_VALUE (args));
3882 write_char ('E');
3884 break;
3886 case DYNAMIC_CAST_EXPR:
3887 case REINTERPRET_CAST_EXPR:
3888 case STATIC_CAST_EXPR:
3889 case CONST_CAST_EXPR:
3890 write_type (TREE_TYPE (expr));
3891 write_expression (TREE_OPERAND (expr, 0));
3892 break;
3894 case PREINCREMENT_EXPR:
3895 case PREDECREMENT_EXPR:
3896 if (abi_check (6))
3897 write_char ('_');
3898 /* Fall through. */
3900 default:
3901 /* In the middle-end, some expressions have more operands than
3902 they do in templates (and mangling). */
3903 len = cp_tree_operand_length (expr);
3905 for (i = 0; i < len; ++i)
3907 tree operand = TREE_OPERAND (expr, i);
3908 /* As a GNU extension, the middle operand of a
3909 conditional may be omitted. Since expression
3910 manglings are supposed to represent the input token
3911 stream, there's no good way to mangle such an
3912 expression without extending the C++ ABI. */
3913 if (code == COND_EXPR && i == 1 && !operand)
3915 error ("omitted middle operand to %<?:%> operand "
3916 "cannot be mangled");
3917 continue;
3919 else if (FOLD_EXPR_P (expr))
3921 /* The first 'operand' of a fold-expression is the operator
3922 that it folds over. */
3923 if (i == 0)
3925 int fcode = TREE_INT_CST_LOW (operand);
3926 write_string (OVL_OP_INFO (false, fcode)->mangled_name);
3927 continue;
3929 else if (code == BINARY_LEFT_FOLD_EXPR)
3931 /* The order of operands of the binary left and right
3932 folds is the same, but we want to mangle them in
3933 lexical order, i.e. non-pack first. */
3934 if (i == 1)
3935 operand = FOLD_EXPR_INIT (expr);
3936 else
3937 operand = FOLD_EXPR_PACK (expr);
3939 if (PACK_EXPANSION_P (operand))
3940 operand = PACK_EXPANSION_PATTERN (operand);
3942 write_expression (operand);
3948 /* Literal subcase of non-terminal <template-arg>.
3950 "Literal arguments, e.g. "A<42L>", are encoded with their type
3951 and value. Negative integer values are preceded with "n"; for
3952 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3953 encoded as 0, true as 1." */
3955 static void
3956 write_template_arg_literal (const tree value)
3958 if (TREE_CODE (value) == STRING_CST)
3959 /* Temporarily mangle strings as braced initializer lists. */
3960 write_string ("tl");
3961 else
3962 write_char ('L');
3964 tree valtype = TREE_TYPE (value);
3965 write_type (valtype);
3967 /* Write a null member pointer value as (type)0, regardless of its
3968 real representation. */
3969 if (null_member_pointer_value_p (value))
3970 write_integer_cst (integer_zero_node);
3971 else
3972 switch (TREE_CODE (value))
3974 case CONST_DECL:
3975 write_integer_cst (DECL_INITIAL (value));
3976 break;
3978 case INTEGER_CST:
3979 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3980 || integer_zerop (value) || integer_onep (value));
3981 if (!(abi_version_at_least (14)
3982 && NULLPTR_TYPE_P (TREE_TYPE (value))))
3983 write_integer_cst (value);
3984 break;
3986 case REAL_CST:
3987 write_real_cst (value);
3988 break;
3990 case COMPLEX_CST:
3991 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3992 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3994 write_integer_cst (TREE_REALPART (value));
3995 write_char ('_');
3996 write_integer_cst (TREE_IMAGPART (value));
3998 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3999 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
4001 write_real_cst (TREE_REALPART (value));
4002 write_char ('_');
4003 write_real_cst (TREE_IMAGPART (value));
4005 else
4006 gcc_unreachable ();
4007 break;
4009 case STRING_CST:
4011 /* Mangle strings the same as braced initializer lists. */
4012 unsigned n = TREE_STRING_LENGTH (value);
4013 const char *str = TREE_STRING_POINTER (value);
4015 /* Count the number of trailing nuls and subtract them from
4016 STRSIZE because they don't need to be mangled. */
4017 for (const char *p = str + n - 1; ; --p)
4019 if (*p || p == str)
4021 n -= str + n - !!*p - p;
4022 break;
4025 tree eltype = TREE_TYPE (valtype);
4026 for (const char *p = str; n--; ++p)
4028 write_char ('L');
4029 write_type (eltype);
4030 write_unsigned_number (*(const unsigned char*)p);
4031 write_string ("E");
4033 break;
4036 default:
4037 gcc_unreachable ();
4040 write_char ('E');
4043 /* Non-terminal <template-arg>.
4045 <template-arg> ::= <type> # type
4046 ::= L <type> </value/ number> E # literal
4047 ::= LZ <name> E # external name
4048 ::= X <expression> E # expression */
4050 static void
4051 write_template_arg (tree node)
4053 enum tree_code code = TREE_CODE (node);
4055 MANGLE_TRACE_TREE ("template-arg", node);
4057 /* A template template parameter's argument list contains TREE_LIST
4058 nodes of which the value field is the actual argument. */
4059 if (code == TREE_LIST)
4061 node = TREE_VALUE (node);
4062 /* If it's a decl, deal with its type instead. */
4063 if (DECL_P (node))
4065 node = TREE_TYPE (node);
4066 code = TREE_CODE (node);
4070 if (VAR_P (node) && DECL_NTTP_OBJECT_P (node))
4071 /* We want to mangle the argument, not the var we stored it in. */
4072 node = tparm_object_argument (node);
4074 /* Strip a conversion added by convert_nontype_argument. */
4075 if (TREE_CODE (node) == IMPLICIT_CONV_EXPR)
4076 node = TREE_OPERAND (node, 0);
4077 if (REFERENCE_REF_P (node))
4078 node = TREE_OPERAND (node, 0);
4079 if (TREE_CODE (node) == NOP_EXPR
4080 && TYPE_REF_P (TREE_TYPE (node)))
4082 /* Template parameters can be of reference type. To maintain
4083 internal consistency, such arguments use a conversion from
4084 address of object to reference type. */
4085 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
4086 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
4089 if (TREE_CODE (node) == BASELINK
4090 && !type_unknown_p (node))
4092 /* Before v6 we wrongly wrapped a class-scope function in X/E. */
4093 if (abi_check (6))
4094 node = BASELINK_FUNCTIONS (node);
4097 if (ARGUMENT_PACK_P (node))
4099 /* Expand the template argument pack. */
4100 tree args = ARGUMENT_PACK_ARGS (node);
4101 int i, length = TREE_VEC_LENGTH (args);
4102 if (abi_check (6))
4103 write_char ('J');
4104 else
4105 write_char ('I');
4106 for (i = 0; i < length; ++i)
4107 write_template_arg (TREE_VEC_ELT (args, i));
4108 write_char ('E');
4110 else if (TYPE_P (node))
4111 write_type (node);
4112 else if (code == TEMPLATE_DECL)
4113 /* A template appearing as a template arg is a template template arg. */
4114 write_template_template_arg (node);
4115 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
4116 || code == CONST_DECL
4117 || null_member_pointer_value_p (node))
4118 write_template_arg_literal (node);
4119 else if (code == EXCESS_PRECISION_EXPR
4120 && TREE_CODE (TREE_OPERAND (node, 0)) == REAL_CST)
4121 write_template_arg_literal (fold_convert (TREE_TYPE (node),
4122 TREE_OPERAND (node, 0)));
4123 else if (DECL_P (node))
4125 write_char ('L');
4126 /* Until ABI version 3, the underscore before the mangled name
4127 was incorrectly omitted. */
4128 if (!abi_check (3))
4129 write_char ('Z');
4130 else
4131 write_string ("_Z");
4132 write_encoding (node);
4133 write_char ('E');
4135 else
4137 /* Template arguments may be expressions. */
4138 write_char ('X');
4139 write_expression (node);
4140 write_char ('E');
4144 /* <template-template-arg>
4145 ::= <name>
4146 ::= <substitution> */
4148 static void
4149 write_template_template_arg (const tree decl)
4151 MANGLE_TRACE_TREE ("template-template-arg", decl);
4153 if (find_substitution (decl))
4154 return;
4155 write_name (decl, /*ignore_local_scope=*/0);
4156 add_substitution (decl);
4160 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
4162 <array-type> ::= A [</dimension/ number>] _ </element/ type>
4163 ::= A <expression> _ </element/ type>
4165 "Array types encode the dimension (number of elements) and the
4166 element type. For variable length arrays, the dimension (but not
4167 the '_' separator) is omitted."
4168 Note that for flexible array members, like for other arrays of
4169 unspecified size, the dimension is also omitted. */
4171 static void
4172 write_array_type (const tree type)
4174 write_char ('A');
4175 if (TYPE_DOMAIN (type))
4177 tree index_type;
4179 index_type = TYPE_DOMAIN (type);
4180 /* The INDEX_TYPE gives the upper and lower bounds of the array.
4181 It's null for flexible array members which have no upper bound
4182 (this is a change from GCC 5 and prior where such members were
4183 incorrectly mangled as zero-length arrays). */
4184 if (tree max = TYPE_MAX_VALUE (index_type))
4186 if (TREE_CODE (max) == INTEGER_CST)
4188 /* The ABI specifies that we should mangle the number of
4189 elements in the array, not the largest allowed index. */
4190 offset_int wmax = wi::to_offset (max) + 1;
4191 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
4192 number of elements as zero. */
4193 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
4194 gcc_assert (wi::fits_uhwi_p (wmax));
4195 write_unsigned_number (wmax.to_uhwi ());
4197 else
4199 max = TREE_OPERAND (max, 0);
4200 write_expression (max);
4204 write_char ('_');
4205 write_type (TREE_TYPE (type));
4208 /* Non-terminal <pointer-to-member-type> for pointer-to-member
4209 variables. TYPE is a pointer-to-member POINTER_TYPE.
4211 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
4213 static void
4214 write_pointer_to_member_type (const tree type)
4216 write_char ('M');
4217 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
4218 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
4221 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
4222 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
4223 TEMPLATE_PARM_INDEX.
4225 <template-param> ::= T </parameter/ number> _ */
4227 static void
4228 write_template_param (const tree parm)
4230 int parm_index;
4231 int level;
4233 MANGLE_TRACE_TREE ("template-parm", parm);
4235 switch (TREE_CODE (parm))
4237 case TEMPLATE_TYPE_PARM:
4238 case TEMPLATE_TEMPLATE_PARM:
4239 case BOUND_TEMPLATE_TEMPLATE_PARM:
4240 parm_index = TEMPLATE_TYPE_IDX (parm);
4241 level = TEMPLATE_TYPE_LEVEL (parm);
4242 break;
4244 case TEMPLATE_PARM_INDEX:
4245 parm_index = TEMPLATE_PARM_IDX (parm);
4246 level = TEMPLATE_PARM_LEVEL (parm);
4247 break;
4249 default:
4250 gcc_unreachable ();
4253 write_char ('T');
4254 if (level > 1)
4256 if (abi_check (19))
4258 write_char ('L');
4259 write_compact_number (level - 1);
4262 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
4263 earliest template param denoted by `_'. */
4264 write_compact_number (parm_index);
4267 /* <template-template-param>
4268 ::= <template-param>
4269 ::= <substitution> */
4271 static void
4272 write_template_template_param (const tree parm)
4274 tree templ = NULL_TREE;
4276 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
4277 template template parameter. The substitution candidate here is
4278 only the template. */
4279 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
4281 templ
4282 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
4283 if (find_substitution (templ))
4284 return;
4287 /* <template-param> encodes only the template parameter position,
4288 not its template arguments, which is fine here. */
4289 write_template_param (parm);
4290 if (templ)
4291 add_substitution (templ);
4294 /* Non-terminal <substitution>.
4296 <substitution> ::= S <seq-id> _
4297 ::= S_ */
4299 static void
4300 write_substitution (const int seq_id)
4302 MANGLE_TRACE ("substitution", "");
4304 write_char ('S');
4305 if (seq_id > 0)
4306 write_number (seq_id - 1, /*unsigned=*/1, 36);
4307 write_char ('_');
4310 /* Start mangling ENTITY. */
4312 static inline void
4313 start_mangling (const tree entity)
4315 G = {};
4316 G.entity = entity;
4317 obstack_free (&name_obstack, name_base);
4318 mangle_obstack = &name_obstack;
4319 name_base = obstack_alloc (&name_obstack, 0);
4322 /* Done with mangling. Release the data. */
4324 static void
4325 finish_mangling_internal (void)
4327 /* Clear all the substitutions. */
4328 vec_safe_truncate (G.substitutions, 0);
4330 if (G.mod)
4331 mangle_module_fini ();
4333 /* Null-terminate the string. */
4334 write_char ('\0');
4338 /* Like finish_mangling_internal, but return the mangled string. */
4340 static inline const char *
4341 finish_mangling (void)
4343 finish_mangling_internal ();
4344 return (const char *) obstack_finish (mangle_obstack);
4347 /* Like finish_mangling_internal, but return an identifier. */
4349 static tree
4350 finish_mangling_get_identifier (void)
4352 finish_mangling_internal ();
4353 /* Don't obstack_finish here, and the next start_mangling will
4354 remove the identifier. */
4355 return get_identifier ((const char *) obstack_base (mangle_obstack));
4358 /* Initialize data structures for mangling. */
4360 void
4361 init_mangle (void)
4363 gcc_obstack_init (&name_obstack);
4364 name_base = obstack_alloc (&name_obstack, 0);
4365 vec_alloc (G.substitutions, 0);
4367 /* Cache these identifiers for quick comparison when checking for
4368 standard substitutions. */
4369 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
4370 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
4371 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
4372 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
4373 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
4374 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
4377 /* Generate a mangling for MODULE's global initializer fn. */
4379 tree
4380 mangle_module_global_init (int module)
4382 start_mangling (NULL_TREE);
4384 write_string ("_ZGI");
4385 write_module (module, true);
4387 return finish_mangling_get_identifier ();
4390 /* Generate the mangled name of DECL. */
4392 static tree
4393 mangle_decl_string (const tree decl)
4395 tree result;
4396 tree saved_fn = NULL_TREE;
4397 bool template_p = false;
4399 /* We shouldn't be trying to mangle an uninstantiated template. */
4400 gcc_assert (!type_dependent_expression_p (decl));
4402 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4404 struct tinst_level *tl = current_instantiation ();
4405 if ((!tl || tl->maybe_get_node () != decl)
4406 && push_tinst_level (decl))
4408 template_p = true;
4409 saved_fn = current_function_decl;
4410 current_function_decl = NULL_TREE;
4413 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
4415 start_mangling (decl);
4417 if (TREE_CODE (decl) == TYPE_DECL)
4418 write_type (TREE_TYPE (decl));
4419 else
4420 write_mangled_name (decl, true);
4422 result = finish_mangling_get_identifier ();
4423 if (DEBUG_MANGLE)
4424 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
4425 IDENTIFIER_POINTER (result));
4427 if (template_p)
4429 pop_tinst_level ();
4430 current_function_decl = saved_fn;
4433 return result;
4436 /* Return an identifier for the external mangled name of DECL. */
4438 static tree
4439 get_mangled_id (tree decl)
4441 tree id = mangle_decl_string (decl);
4442 return targetm.mangle_decl_assembler_name (decl, id);
4445 /* Create an identifier for the external mangled name of DECL. */
4447 void
4448 mangle_decl (const tree decl)
4450 tree id;
4451 bool dep;
4453 /* Don't bother mangling uninstantiated templates. */
4454 ++processing_template_decl;
4455 if (TREE_CODE (decl) == TYPE_DECL)
4456 dep = dependent_type_p (TREE_TYPE (decl));
4457 else
4458 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4459 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
4460 --processing_template_decl;
4461 if (dep)
4462 return;
4464 /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
4465 It is not needed to assign names to anonymous namespace, but we use the
4466 "<anon>" marker to be able to tell if type is C++ ODR type or type
4467 produced by other language. */
4468 if (TREE_CODE (decl) == TYPE_DECL
4469 && TYPE_STUB_DECL (TREE_TYPE (decl))
4470 && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
4471 id = get_identifier ("<anon>");
4472 else
4474 gcc_assert (TREE_CODE (decl) != TYPE_DECL
4475 || !no_linkage_check (TREE_TYPE (decl), true));
4476 if (abi_version_at_least (10))
4477 if (tree fn = decl_function_context (decl))
4478 maybe_check_abi_tags (fn, decl);
4479 id = get_mangled_id (decl);
4481 SET_DECL_ASSEMBLER_NAME (decl, id);
4483 if (G.need_cxx17_warning
4484 && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl)))
4485 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type,
4486 "mangled name for %qD will change in C++17 because the "
4487 "exception specification is part of a function type",
4488 decl);
4490 if (id != DECL_NAME (decl)
4491 /* Don't do this for a fake symbol we aren't going to emit anyway. */
4492 && TREE_CODE (decl) != TYPE_DECL
4493 && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
4495 int save_ver = flag_abi_version;
4496 tree id2 = NULL_TREE;
4498 if (!DECL_REALLY_EXTERN (decl))
4500 record_mangling (decl, G.need_abi_warning);
4502 if (!G.need_abi_warning)
4503 return;
4505 flag_abi_version = flag_abi_compat_version;
4506 id2 = mangle_decl_string (decl);
4507 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4508 flag_abi_version = save_ver;
4510 if (id2 != id)
4511 note_mangling_alias (decl, id2);
4514 if (warn_abi)
4516 const char fabi_version[] = "-fabi-version";
4518 if (flag_abi_compat_version != warn_abi_version
4519 || id2 == NULL_TREE)
4521 flag_abi_version = warn_abi_version;
4522 id2 = mangle_decl_string (decl);
4523 id2 = targetm.mangle_decl_assembler_name (decl, id2);
4525 flag_abi_version = save_ver;
4527 if (id2 == id)
4528 /* OK. */;
4529 else if (warn_abi_version != 0
4530 && abi_version_at_least (warn_abi_version))
4531 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4532 "the mangled name of %qD changed between "
4533 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4534 G.entity, fabi_version, warn_abi_version, id2,
4535 fabi_version, save_ver, id);
4536 else
4537 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4538 "the mangled name of %qD changes between "
4539 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4540 G.entity, fabi_version, save_ver, id,
4541 fabi_version, warn_abi_version, id2);
4544 flag_abi_version = save_ver;
4548 /* Generate the mangled representation of TYPE. */
4550 const char *
4551 mangle_type_string (const tree type)
4553 const char *result;
4555 start_mangling (type);
4556 write_type (type);
4557 result = finish_mangling ();
4558 if (DEBUG_MANGLE)
4559 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
4560 return result;
4563 /* Create an identifier for the mangled name of a special component
4564 for belonging to TYPE. CODE is the ABI-specified code for this
4565 component. */
4567 static tree
4568 mangle_special_for_type (const tree type, const char *code)
4570 tree result;
4572 /* We don't have an actual decl here for the special component, so
4573 we can't just process the <encoded-name>. Instead, fake it. */
4574 start_mangling (type);
4576 /* Start the mangling. */
4577 write_string ("_Z");
4578 write_string (code);
4580 /* Add the type. */
4581 write_type (type);
4582 result = finish_mangling_get_identifier ();
4584 if (DEBUG_MANGLE)
4585 fprintf (stderr, "mangle_special_for_type = %s\n\n",
4586 IDENTIFIER_POINTER (result));
4588 return result;
4591 /* Create an identifier for the mangled representation of the typeinfo
4592 structure for TYPE. */
4594 tree
4595 mangle_typeinfo_for_type (const tree type)
4597 return mangle_special_for_type (type, "TI");
4600 /* Create an identifier for the mangled name of the NTBS containing
4601 the mangled name of TYPE. */
4603 tree
4604 mangle_typeinfo_string_for_type (const tree type)
4606 return mangle_special_for_type (type, "TS");
4609 /* Create an identifier for the mangled name of the vtable for TYPE. */
4611 tree
4612 mangle_vtbl_for_type (const tree type)
4614 return mangle_special_for_type (type, "TV");
4617 /* Returns an identifier for the mangled name of the VTT for TYPE. */
4619 tree
4620 mangle_vtt_for_type (const tree type)
4622 return mangle_special_for_type (type, "TT");
4625 /* Returns an identifier for the mangled name of the decomposition
4626 artificial variable DECL. DECLS is the vector of the VAR_DECLs
4627 for the identifier-list. */
4629 tree
4630 mangle_decomp (const tree decl, vec<tree> &decls)
4632 gcc_assert (!type_dependent_expression_p (decl));
4634 location_t saved_loc = input_location;
4635 input_location = DECL_SOURCE_LOCATION (decl);
4637 check_abi_tags (decl);
4638 start_mangling (decl);
4639 write_string ("_Z");
4641 tree context = decl_mangling_context (decl);
4642 gcc_assert (context != NULL_TREE);
4644 bool nested = false;
4645 bool local = false;
4646 if (DECL_NAMESPACE_STD_P (context))
4647 write_string ("St");
4648 else if (TREE_CODE (context) == FUNCTION_DECL)
4650 local = true;
4651 write_char ('Z');
4652 write_encoding (context);
4653 write_char ('E');
4655 else if (context != global_namespace)
4657 nested = true;
4658 write_char ('N');
4659 write_prefix (context);
4662 write_string ("DC");
4663 unsigned int i;
4664 tree d;
4665 FOR_EACH_VEC_ELT (decls, i, d)
4666 write_unqualified_name (d);
4667 write_char ('E');
4669 if (tree tags = get_abi_tags (decl))
4671 /* We didn't emit ABI tags for structured bindings before ABI 19. */
4672 if (!G.need_abi_warning
4673 && TREE_PUBLIC (decl)
4674 && abi_warn_or_compat_version_crosses (19))
4675 G.need_abi_warning = 1;
4677 if (abi_version_at_least (19))
4678 write_abi_tags (tags);
4681 if (nested)
4682 write_char ('E');
4683 else if (local && DECL_DISCRIMINATOR_P (decl))
4684 write_discriminator (discriminator_for_local_entity (decl));
4686 tree id = finish_mangling_get_identifier ();
4687 if (DEBUG_MANGLE)
4688 fprintf (stderr, "mangle_decomp = '%s'\n\n",
4689 IDENTIFIER_POINTER (id));
4691 input_location = saved_loc;
4693 if (warn_abi && G.need_abi_warning)
4695 const char fabi_version[] = "-fabi-version";
4696 tree id2 = id;
4697 int save_ver = flag_abi_version;
4699 if (flag_abi_version != warn_abi_version)
4701 flag_abi_version = warn_abi_version;
4702 id2 = mangle_decomp (decl, decls);
4703 flag_abi_version = save_ver;
4706 if (id2 == id)
4707 /* OK. */;
4708 else if (warn_abi_version != 0
4709 && abi_version_at_least (warn_abi_version))
4710 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4711 "the mangled name of %qD changed between "
4712 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4713 G.entity, fabi_version, warn_abi_version, id2,
4714 fabi_version, save_ver, id);
4715 else
4716 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
4717 "the mangled name of %qD changes between "
4718 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4719 G.entity, fabi_version, save_ver, id,
4720 fabi_version, warn_abi_version, id2);
4723 return id;
4726 /* Return an identifier for a construction vtable group. TYPE is
4727 the most derived class in the hierarchy; BINFO is the base
4728 subobject for which this construction vtable group will be used.
4730 This mangling isn't part of the ABI specification; in the ABI
4731 specification, the vtable group is dumped in the same COMDAT as the
4732 main vtable, and is referenced only from that vtable, so it doesn't
4733 need an external name. For binary formats without COMDAT sections,
4734 though, we need external names for the vtable groups.
4736 We use the production
4738 <special-name> ::= CT <type> <offset number> _ <base type> */
4740 tree
4741 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
4743 tree result;
4745 start_mangling (type);
4747 write_string ("_Z");
4748 write_string ("TC");
4749 write_type (type);
4750 write_integer_cst (BINFO_OFFSET (binfo));
4751 write_char ('_');
4752 write_type (BINFO_TYPE (binfo));
4754 result = finish_mangling_get_identifier ();
4755 if (DEBUG_MANGLE)
4756 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
4757 IDENTIFIER_POINTER (result));
4758 return result;
4761 /* Mangle a this pointer or result pointer adjustment.
4763 <call-offset> ::= h <fixed offset number> _
4764 ::= v <fixed offset number> _ <virtual offset number> _ */
4766 static void
4767 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
4769 write_char (virtual_offset ? 'v' : 'h');
4771 /* For either flavor, write the fixed offset. */
4772 write_integer_cst (fixed_offset);
4773 write_char ('_');
4775 /* For a virtual thunk, add the virtual offset. */
4776 if (virtual_offset)
4778 write_integer_cst (virtual_offset);
4779 write_char ('_');
4783 /* Return an identifier for the mangled name of a this-adjusting or
4784 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
4785 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
4786 is a virtual thunk, and it is the vtbl offset in
4787 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
4788 zero for a covariant thunk. Note, that FN_DECL might be a covariant
4789 thunk itself. A covariant thunk name always includes the adjustment
4790 for the this pointer, even if there is none.
4792 <special-name> ::= T <call-offset> <base encoding>
4793 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
4794 <base encoding> */
4796 tree
4797 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
4798 tree virtual_offset, tree thunk)
4800 tree result;
4802 if (abi_version_at_least (11))
4803 maybe_check_abi_tags (fn_decl, thunk, 11);
4805 start_mangling (fn_decl);
4807 write_string ("_Z");
4808 write_char ('T');
4810 if (!this_adjusting)
4812 /* Covariant thunk with no this adjustment */
4813 write_char ('c');
4814 mangle_call_offset (integer_zero_node, NULL_TREE);
4815 mangle_call_offset (fixed_offset, virtual_offset);
4817 else if (!DECL_THUNK_P (fn_decl))
4818 /* Plain this adjusting thunk. */
4819 mangle_call_offset (fixed_offset, virtual_offset);
4820 else
4822 /* This adjusting thunk to covariant thunk. */
4823 write_char ('c');
4824 mangle_call_offset (fixed_offset, virtual_offset);
4825 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
4826 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
4827 if (virtual_offset)
4828 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
4829 mangle_call_offset (fixed_offset, virtual_offset);
4830 fn_decl = THUNK_TARGET (fn_decl);
4833 /* Scoped name. */
4834 write_encoding (fn_decl);
4836 result = finish_mangling_get_identifier ();
4837 if (DEBUG_MANGLE)
4838 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
4839 return result;
4842 /* Handle ABI backwards compatibility for past bugs where we didn't call
4843 check_abi_tags in places where it's needed: call check_abi_tags and warn if
4844 it makes a difference. If FOR_DECL is non-null, it's the declaration
4845 that we're actually trying to mangle; if it's null, we're mangling the
4846 guard variable for T. */
4848 static void
4849 maybe_check_abi_tags (tree t, tree for_decl, int ver)
4851 if (DECL_ASSEMBLER_NAME_SET_P (t))
4852 return;
4854 tree oldtags = get_abi_tags (t);
4856 mangle_decl (t);
4858 tree newtags = get_abi_tags (t);
4859 if (newtags && newtags != oldtags
4860 && abi_version_crosses (ver))
4862 if (for_decl && DECL_THUNK_P (for_decl))
4863 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4864 "the mangled name of a thunk for %qD changes between "
4865 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4866 t, flag_abi_version, warn_abi_version);
4867 else if (for_decl)
4868 warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
4869 "the mangled name of %qD changes between "
4870 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4871 for_decl, flag_abi_version, warn_abi_version);
4872 else
4873 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4874 "the mangled name of the initialization guard variable "
4875 "for %qD changes between %<-fabi-version=%d%> and "
4876 "%<-fabi-version=%d%>",
4877 t, flag_abi_version, warn_abi_version);
4881 /* Write out the appropriate string for this variable when generating
4882 another mangled name based on this one. */
4884 static void
4885 write_guarded_var_name (const tree variable)
4887 if (DECL_NAME (variable)
4888 && startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR"))
4889 /* The name of a guard variable for a reference temporary should refer
4890 to the reference, not the temporary. */
4891 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
4892 else if (DECL_DECOMPOSITION_P (variable)
4893 && DECL_NAME (variable) == NULL_TREE
4894 && startswith (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (variable)),
4895 "_Z"))
4896 /* The name of a guard variable for a structured binding needs special
4897 casing. */
4898 write_string (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (variable)) + 2);
4899 else
4900 write_name (variable, /*ignore_local_scope=*/0);
4903 /* Return an identifier for the name of an initialization guard
4904 variable for indicated VARIABLE. */
4906 tree
4907 mangle_guard_variable (const tree variable)
4909 if (abi_version_at_least (10))
4910 maybe_check_abi_tags (variable);
4911 start_mangling (variable);
4912 write_string ("_ZGV");
4913 write_guarded_var_name (variable);
4914 return finish_mangling_get_identifier ();
4917 /* Return an identifier for the name of a thread_local initialization
4918 function for VARIABLE. */
4920 tree
4921 mangle_tls_init_fn (const tree variable)
4923 check_abi_tags (variable);
4924 start_mangling (variable);
4925 write_string ("_ZTH");
4926 write_guarded_var_name (variable);
4927 return finish_mangling_get_identifier ();
4930 /* Return an identifier for the name of a thread_local wrapper
4931 function for VARIABLE. */
4933 #define TLS_WRAPPER_PREFIX "_ZTW"
4935 tree
4936 mangle_tls_wrapper_fn (const tree variable)
4938 check_abi_tags (variable);
4939 start_mangling (variable);
4940 write_string (TLS_WRAPPER_PREFIX);
4941 write_guarded_var_name (variable);
4942 return finish_mangling_get_identifier ();
4945 /* Return true iff FN is a thread_local wrapper function. */
4947 bool
4948 decl_tls_wrapper_p (const tree fn)
4950 if (TREE_CODE (fn) != FUNCTION_DECL)
4951 return false;
4952 tree name = DECL_NAME (fn);
4953 return startswith (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX);
4956 /* Return an identifier for the name of a temporary variable used to
4957 initialize a static reference. This is now part of the ABI. */
4959 tree
4960 mangle_ref_init_variable (const tree variable)
4962 start_mangling (variable);
4963 write_string ("_ZGR");
4964 check_abi_tags (variable);
4965 write_guarded_var_name (variable);
4966 /* Avoid name clashes with aggregate initialization of multiple
4967 references at once. */
4968 write_compact_number (current_ref_temp_count++);
4969 return finish_mangling_get_identifier ();
4972 /* Return an identifier for the mangled name of a C++20 template parameter
4973 object for template argument EXPR. */
4975 tree
4976 mangle_template_parm_object (tree expr)
4978 start_mangling (expr);
4979 write_string ("_ZTAX");
4980 write_expression (expr);
4981 write_char ('E');
4982 return finish_mangling_get_identifier ();
4985 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
4986 function generates a mangled name for the vtable map variable of
4987 the class type. For example, if the class type is
4988 "std::bad_exception", the mangled name for the class is
4989 "St13bad_exception". This function would generate the name
4990 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
4991 "_VTV<std::bad_exception>::__vtable_map". */
4994 char *
4995 get_mangled_vtable_map_var_name (tree class_type)
4997 char *var_name = NULL;
4998 const char *prefix = "_ZN4_VTVI";
4999 const char *postfix = "E12__vtable_mapE";
5001 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
5003 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
5005 if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
5007 class_id = get_mangled_id (TYPE_NAME (class_type));
5008 vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
5011 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
5012 strlen (prefix) +
5013 strlen (postfix) + 1;
5015 var_name = (char *) xmalloc (len);
5017 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
5019 return var_name;
5022 #include "gt-cp-mangle.h"