1 /* Name mangling for the 3.0 -*- C++ -*- ABI.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Written by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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
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 */
49 #include "coretypes.h"
51 #include "vtable-verify.h"
53 #include "stringpool.h"
55 #include "stor-layout.h"
59 /* Debugging support. */
61 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
63 #define DEBUG_MANGLE 0
66 /* Macros for tracing the write_* functions. */
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))
74 # define MANGLE_TRACE(FN, INPUT)
75 # define MANGLE_TRACE_TREE(FN, NODE)
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
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
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. */
115 /* True if the mangling will be different in a future version of the
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. */
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. */
148 SUBID_BASIC_IOSTREAM
,
151 substitution_identifier_index_t
;
153 /* For quick substitution checks, look up these common identifiers
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. */
160 integer_type_codes
[itk_none
] =
163 'a', /* itk_signed_char */
164 'h', /* itk_unsigned_char */
166 't', /* itk_unsigned_short */
168 'j', /* itk_unsigned_int */
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 *,
209 static void write_number (unsigned HOST_WIDE_INT
, const int,
211 static void write_compact_number (int num
);
212 static void write_integer_cst (const tree
);
213 static void write_real_cst (const tree
);
214 static void write_identifier (const char *);
215 static void write_special_name_constructor (const tree
);
216 static void write_special_name_destructor (const tree
);
217 static void write_type (tree
);
218 static int write_CV_qualifiers_for_type (const tree
);
219 static void write_builtin_type (tree
);
220 static void write_function_type (const tree
);
221 static void write_bare_function_type (const tree
, const int, const tree
);
222 static void write_method_parms (tree
, const int, const tree
);
223 static void write_class_enum_type (const tree
);
224 static void write_template_args (tree
);
225 static void write_expression (tree
);
226 static void write_template_arg_literal (const tree
);
227 static void write_template_arg (tree
);
228 static void write_template_template_arg (const tree
);
229 static void write_array_type (const tree
);
230 static void write_pointer_to_member_type (const tree
);
231 static void write_template_param (const tree
);
232 static void write_template_template_param (const tree
);
233 static void write_substitution (const int);
234 static int discriminator_for_local_entity (tree
);
235 static int discriminator_for_string_literal (tree
, tree
);
236 static void write_discriminator (const int);
237 static void write_local_name (tree
, const tree
, const tree
);
238 static void dump_substitution_candidates (void);
239 static tree
mangle_decl_string (const tree
);
240 static void maybe_check_abi_tags (tree
, tree
= NULL_TREE
, int = 10);
241 static bool equal_abi_tags (tree
, tree
);
243 /* Control functions. */
245 static inline void start_mangling (const tree
);
246 static tree
mangle_special_for_type (const tree
, const char *);
248 /* Append a single character to the end of the mangled
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
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
266 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
267 (TREE_CODE (NODE1) == TREE_LIST \
268 && TREE_CODE (NODE2) == TREE_LIST \
269 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
270 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
271 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
272 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
274 /* Write out an unsigned quantity in base 10. */
275 #define write_unsigned_number(NUMBER) \
276 write_number ((NUMBER), /*unsigned_p=*/1, 10)
278 /* If DECL is a template instance (including the uninstantiated template
279 itself), return its TEMPLATE_INFO. Otherwise return NULL. */
282 maybe_template_info (const tree decl
)
284 if (TREE_CODE (decl
) == TYPE_DECL
)
286 /* TYPE_DECLs are handled specially. Look at its type to decide
287 if this is a template instantiation. */
288 const tree type
= TREE_TYPE (decl
);
290 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_ID_P (type
))
291 return TYPE_TEMPLATE_INFO (type
);
295 /* Check if the template is a primary template. */
296 if (DECL_LANG_SPECIFIC (decl
) != NULL
297 && VAR_OR_FUNCTION_DECL_P (decl
)
298 && DECL_TEMPLATE_INFO (decl
)
299 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
300 return DECL_TEMPLATE_INFO (decl
);
303 /* It's not a template id. */
307 /* Produce debugging output of current substitution candidates. */
310 dump_substitution_candidates (void)
315 fprintf (stderr
, " ++ substitutions ");
316 FOR_EACH_VEC_ELT (*G
.substitutions
, i
, el
)
318 const char *name
= "???";
321 fprintf (stderr
, " ");
324 else if (DECL_P (el
))
325 name
= IDENTIFIER_POINTER (DECL_NAME (el
));
326 else if (TREE_CODE (el
) == TREE_LIST
)
327 name
= IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el
)));
328 else if (TYPE_NAME (el
))
329 name
= TYPE_NAME_STRING (el
);
330 fprintf (stderr
, " S%d_ = ", i
- 1);
334 (CP_TYPE_RESTRICT_P (el
)
335 || CP_TYPE_VOLATILE_P (el
)
336 || CP_TYPE_CONST_P (el
)))
337 fprintf (stderr
, "CV-");
338 fprintf (stderr
, "%s (%s at %p)",
339 name
, get_tree_code_name (TREE_CODE (el
)), (void *) el
);
341 fprintf (stderr
, "\n");
345 /* <exception-spec> ::=
346 Do -- non-throwing exception specification
347 DO <expression> E -- computed (instantiation-dependent) noexcept
348 Dw <type>* E -- throw (types) */
351 write_exception_spec (tree spec
)
354 if (!spec
|| spec
== noexcept_false_spec
)
358 if (!flag_noexcept_type
)
360 G
.need_cxx17_warning
= true;
364 if (spec
== noexcept_true_spec
|| spec
== empty_except_spec
)
366 else if (tree expr
= TREE_PURPOSE (spec
))
368 /* noexcept (expr) */
369 gcc_assert (uses_template_parms (expr
));
371 write_expression (expr
);
376 /* throw (type-list) */
378 for (tree t
= spec
; t
; t
= TREE_CHAIN (t
))
379 write_type (TREE_VALUE (t
));
384 /* Both decls and types can be substitution candidates, but sometimes
385 they refer to the same thing. For instance, a TYPE_DECL and
386 RECORD_TYPE for the same class refer to the same thing, and should
387 be treated accordingly in substitutions. This function returns a
388 canonicalized tree node representing NODE that is used when adding
389 and substitution candidates and finding matches. */
392 canonicalize_for_substitution (tree node
)
394 /* For a TYPE_DECL, use the type instead. */
395 if (TREE_CODE (node
) == TYPE_DECL
)
396 node
= TREE_TYPE (node
);
398 && TYPE_CANONICAL (node
) != node
399 && TYPE_MAIN_VARIANT (node
) != node
)
402 /* Here we want to strip the topmost typedef only.
403 We need to do that so is_std_substitution can do proper
405 if (TREE_CODE (node
) == FUNCTION_TYPE
)
406 /* Use build_qualified_type and TYPE_QUALS here to preserve
407 the old buggy mangling of attribute noreturn with abi<5. */
408 node
= build_qualified_type (TYPE_MAIN_VARIANT (node
),
411 node
= cp_build_qualified_type (TYPE_MAIN_VARIANT (node
),
412 cp_type_quals (node
));
413 if (FUNC_OR_METHOD_TYPE_P (node
))
415 node
= build_ref_qualified_type (node
, type_memfn_rqual (orig
));
416 tree r
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (orig
));
417 if (flag_noexcept_type
)
418 node
= build_exception_variant (node
, r
);
420 /* Set the warning flag if appropriate. */
421 write_exception_spec (r
);
427 /* Add NODE as a substitution candidate. NODE must not already be on
428 the list of candidates. */
431 add_substitution (tree node
)
436 fprintf (stderr
, " ++ add_substitution (%s at %10p)\n",
437 get_tree_code_name (TREE_CODE (node
)), (void *) node
);
439 /* Get the canonicalized substitution candidate for NODE. */
440 c
= canonicalize_for_substitution (node
);
441 if (DEBUG_MANGLE
&& c
!= node
)
442 fprintf (stderr
, " ++ using candidate (%s at %10p)\n",
443 get_tree_code_name (TREE_CODE (node
)), (void *) node
);
446 /* Make sure NODE isn't already a candidate. */
452 FOR_EACH_VEC_SAFE_ELT (G
.substitutions
, i
, candidate
)
455 gcc_assert (!(DECL_P (node
) && node
== candidate
));
456 gcc_assert (!(TYPE_P (node
) && TYPE_P (candidate
)
457 && same_type_p (node
, candidate
)));
461 /* Put the decl onto the varray of substitution candidates. */
462 vec_safe_push (G
.substitutions
, node
);
465 dump_substitution_candidates ();
468 /* Helper function for find_substitution. Returns nonzero if NODE,
469 which may be a decl or a CLASS_TYPE, is a template-id with template
470 name of substitution_index[INDEX] in the ::std namespace, with
471 global module attachment. */
474 is_std_substitution (const tree node
,
475 const substitution_identifier_index_t index
)
482 type
= TREE_TYPE (node
);
485 else if (CLASS_TYPE_P (node
))
488 decl
= TYPE_NAME (node
);
491 /* These are not the droids you're looking for. */
494 if (!DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl
)))
497 if (!(TYPE_LANG_SPECIFIC (type
) && TYPE_TEMPLATE_INFO (type
)))
500 tree tmpl
= TYPE_TI_TEMPLATE (type
);
501 if (DECL_NAME (tmpl
) != subst_identifiers
[index
])
504 if (modules_p () && get_originating_module (tmpl
, true) >= 0)
510 /* Return the ABI tags (the TREE_VALUE of the "abi_tag" attribute entry) for T,
511 which can be a decl or type. */
514 get_abi_tags (tree t
)
516 if (!t
|| TREE_CODE (t
) == NAMESPACE_DECL
)
519 if (DECL_P (t
) && DECL_DECLARES_TYPE_P (t
))
524 attrs
= TYPE_ATTRIBUTES (t
);
526 attrs
= DECL_ATTRIBUTES (t
);
528 tree tags
= lookup_attribute ("abi_tag", attrs
);
530 tags
= TREE_VALUE (tags
);
534 /* Helper function for find_substitution. Returns nonzero if NODE,
535 which may be a decl or a CLASS_TYPE, is the template-id
536 ::std::identifier<char>, where identifier is
537 substitution_index[INDEX]. */
540 is_std_substitution_char (const tree node
,
541 const substitution_identifier_index_t index
)
544 /* Check NODE's name is ::std::identifier. */
545 if (!is_std_substitution (node
, index
))
547 /* Figure out its template args. */
549 args
= DECL_TI_ARGS (node
);
550 else if (CLASS_TYPE_P (node
))
551 args
= CLASSTYPE_TI_ARGS (node
);
553 /* Oops, not a template. */
555 /* NODE's template arg list should be <char>. */
557 TREE_VEC_LENGTH (args
) == 1
558 && TREE_VEC_ELT (args
, 0) == char_type_node
;
561 /* Check whether a substitution should be used to represent NODE in
564 First, check standard special-case substitutions.
566 <substitution> ::= St
573 # ::std::basic_string
576 # ::std::basic_string<char,
577 ::std::char_traits<char>,
578 ::std::allocator<char> >
581 # ::std::basic_istream<char, ::std::char_traits<char> >
584 # ::std::basic_ostream<char, ::std::char_traits<char> >
587 # ::std::basic_iostream<char, ::std::char_traits<char> >
589 Then examine the stack of currently available substitution
590 candidates for entities appearing earlier in the same mangling
592 If a substitution is found, write its mangled representation and
593 return nonzero. If none is found, just return zero. */
596 find_substitution (tree node
)
599 const int size
= vec_safe_length (G
.substitutions
);
602 const char *abbr
= NULL
;
605 fprintf (stderr
, " ++ find_substitution (%s at %p)\n",
606 get_tree_code_name (TREE_CODE (node
)), (void *) node
);
608 /* Obtain the canonicalized substitution representation for NODE.
609 This is what we'll compare against. */
610 node
= canonicalize_for_substitution (node
);
612 /* Check for builtin substitutions. */
614 decl
= TYPE_P (node
) ? TYPE_NAME (node
) : node
;
615 type
= TYPE_P (node
) ? node
: TREE_TYPE (node
);
617 /* Check for std::allocator. */
619 && is_std_substitution (decl
, SUBID_ALLOCATOR
)
620 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl
)))
623 /* Check for std::basic_string. */
624 else if (decl
&& is_std_substitution (decl
, SUBID_BASIC_STRING
))
628 /* If this is a type (i.e. a fully-qualified template-id),
630 std::basic_string <char,
631 std::char_traits<char>,
632 std::allocator<char> > . */
633 if (cp_type_quals (type
) == TYPE_UNQUALIFIED
634 && CLASSTYPE_USE_TEMPLATE (type
))
636 tree args
= CLASSTYPE_TI_ARGS (type
);
637 if (TREE_VEC_LENGTH (args
) == 3
638 && template_args_equal (TREE_VEC_ELT (args
, 0), char_type_node
)
639 && is_std_substitution_char (TREE_VEC_ELT (args
, 1),
641 && is_std_substitution_char (TREE_VEC_ELT (args
, 2),
647 /* Substitute for the template name only if this isn't a type. */
651 /* Check for basic_{i,o,io}stream. */
652 else if (TYPE_P (node
)
653 && cp_type_quals (type
) == TYPE_UNQUALIFIED
654 && CLASS_TYPE_P (type
)
655 && CLASSTYPE_USE_TEMPLATE (type
)
656 && CLASSTYPE_TEMPLATE_INFO (type
) != NULL
)
658 /* First, check for the template
659 args <char, std::char_traits<char> > . */
660 tree args
= CLASSTYPE_TI_ARGS (type
);
661 if (TREE_VEC_LENGTH (args
) == 2
662 && template_args_equal (TREE_VEC_ELT (args
, 0), char_type_node
)
663 && is_std_substitution_char (TREE_VEC_ELT (args
, 1),
666 /* Got them. Is this basic_istream? */
667 if (is_std_substitution (decl
, SUBID_BASIC_ISTREAM
))
669 /* Or basic_ostream? */
670 else if (is_std_substitution (decl
, SUBID_BASIC_OSTREAM
))
672 /* Or basic_iostream? */
673 else if (is_std_substitution (decl
, SUBID_BASIC_IOSTREAM
))
678 /* Check for namespace std. */
679 else if (decl
&& DECL_NAMESPACE_STD_P (decl
))
685 tree tags
= NULL_TREE
;
686 if (OVERLOAD_TYPE_P (node
) || DECL_CLASS_TEMPLATE_P (node
))
687 tags
= get_abi_tags (type
);
688 /* Now check the list of available substitutions for this mangling
691 for (i
= 0; i
< size
; ++i
)
692 if (tree candidate
= (*G
.substitutions
)[i
])
694 /* NODE is a matched to a candidate if it's the same decl node or
695 if it's the same type. */
696 if (decl
== candidate
697 || (TYPE_P (candidate
) && type
&& TYPE_P (node
)
698 && same_type_p (type
, candidate
))
699 || NESTED_TEMPLATE_MATCH (node
, candidate
))
701 write_substitution (i
);
707 /* No substitution found. */
713 /* If there are ABI tags on the abbreviation, it becomes
714 a substitution candidate. */
715 write_abi_tags (tags
);
716 add_substitution (node
);
721 /* Returns whether DECL's symbol name should be the plain unqualified-id
722 rather than a more complicated mangled name. */
725 unmangled_name_p (const tree decl
)
727 if (TREE_CODE (decl
) == FUNCTION_DECL
)
729 /* The names of `extern "C"' functions are not mangled. */
730 return (DECL_EXTERN_C_FUNCTION_P (decl
)
731 /* But overloaded operator names *are* mangled. */
732 && !DECL_OVERLOADED_OPERATOR_P (decl
));
734 else if (VAR_P (decl
))
736 /* static variables are mangled. */
737 if (!DECL_EXTERNAL_LINKAGE_P (decl
))
740 /* extern "C" declarations aren't mangled. */
741 if (DECL_EXTERN_C_P (decl
))
744 /* Other variables at non-global scope are mangled. */
745 if (CP_DECL_CONTEXT (decl
) != global_namespace
)
748 /* Variable template instantiations are mangled. */
749 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
750 && variable_template_p (DECL_TI_TEMPLATE (decl
)))
753 /* Declarations with ABI tags are mangled. */
754 if (get_abi_tags (decl
))
757 // Declarations attached to a named module are mangled
758 if (modules_p () && get_originating_module (decl
, true) >= 0)
761 /* The names of non-static global variables aren't mangled. */
768 /* TOP_LEVEL is true, if this is being called at outermost level of
769 mangling. It should be false when mangling a decl appearing in an
770 expression within some other mangling.
772 <mangled-name> ::= _Z <encoding> */
775 write_mangled_name (const tree decl
, bool top_level
)
777 MANGLE_TRACE_TREE ("mangled-name", decl
);
779 check_abi_tags (decl
);
781 if (unmangled_name_p (decl
))
784 write_string (IDENTIFIER_POINTER (DECL_NAME (decl
)));
787 /* The standard notes: "The <encoding> of an extern "C"
788 function is treated like global-scope data, i.e. as its
789 <source-name> without a type." We cannot write
790 overloaded operators that way though, because it contains
791 characters invalid in assembler. */
793 write_source_name (DECL_NAME (decl
));
799 write_encoding (decl
);
803 /* Returns true if the return type of DECL is part of its signature, and
804 therefore its mangling. */
807 mangle_return_type_p (tree decl
)
809 return (!DECL_CONSTRUCTOR_P (decl
)
810 && !DECL_DESTRUCTOR_P (decl
)
811 && !DECL_CONV_FN_P (decl
)
812 && maybe_template_info (decl
));
815 /* <encoding> ::= <function name> <bare-function-type>
819 write_encoding (const tree decl
)
821 MANGLE_TRACE_TREE ("encoding", decl
);
823 if (DECL_LANG_SPECIFIC (decl
) && DECL_EXTERN_C_FUNCTION_P (decl
))
825 /* For overloaded operators write just the mangled name
826 without arguments. */
827 if (DECL_OVERLOADED_OPERATOR_P (decl
))
828 write_name (decl
, /*ignore_local_scope=*/0);
830 write_source_name (DECL_NAME (decl
));
834 write_name (decl
, /*ignore_local_scope=*/0);
835 if (TREE_CODE (decl
) == FUNCTION_DECL
)
840 if (maybe_template_info (decl
))
842 fn_type
= get_mostly_instantiated_function_type (decl
);
843 /* FN_TYPE will not have parameter types for in-charge or
844 VTT parameters. Therefore, we pass NULL_TREE to
845 write_bare_function_type -- otherwise, it will get
846 confused about which artificial parameters to skip. */
851 fn_type
= TREE_TYPE (decl
);
855 write_bare_function_type (fn_type
,
856 mangle_return_type_p (decl
),
859 /* If this is the pre/post function for a guarded function, append
860 .pre/post, like something from create_virtual_clone. */
861 if (DECL_IS_PRE_FN_P (decl
))
862 write_string (".pre");
863 else if (DECL_IS_POST_FN_P (decl
))
864 write_string (".post");
866 /* If this is a coroutine helper, then append an appropriate string to
868 if (tree ramp
= DECL_RAMP_FN (decl
))
870 if (DECL_ACTOR_FN (ramp
) == decl
)
871 write_string (JOIN_STR
"actor");
872 else if (DECL_DESTROY_FN (ramp
) == decl
)
873 write_string (JOIN_STR
"destroy");
880 /* Interface to substitution and identifier mangling, used by the
881 module name mangler. */
884 mangle_module_substitution (int v
)
886 write_substitution (v
- 1);
890 mangle_module_component (tree comp
, bool partition_p
)
895 write_source_name (comp
);
897 // Module substitutions use the same number-space as entity
898 // substitutions, but are orthogonal.
899 vec_safe_push (G
.substitutions
, NULL_TREE
);
900 return G
.substitutions
->length ();
903 /* If the outermost non-namespace context (including DECL itself) is
904 a module-linkage decl, mangle the module information. For module
905 global initializers we need to include the partition part.
907 <module-name> ::= <module-sub>
909 || <module-name> <module-sub>
910 <module-sub> :: W [P] <unqualified-name>
914 write_module (int m
, bool include_partition
)
917 mangle_module (m
, include_partition
);
921 maybe_write_module (tree decl
)
923 if (!DECL_NAMESPACE_SCOPE_P (decl
))
926 if (!TREE_PUBLIC (STRIP_TEMPLATE (decl
)))
929 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
932 int m
= get_originating_module (decl
, true);
934 write_module (m
, false);
937 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
938 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
941 decl_mangling_context (tree decl
)
943 tree tcontext
= targetm
.cxx
.decl_mangling_context (decl
);
945 if (tcontext
!= NULL_TREE
)
948 if (TREE_CODE (decl
) == TEMPLATE_DECL
949 && DECL_TEMPLATE_RESULT (decl
))
950 decl
= DECL_TEMPLATE_RESULT (decl
);
952 if (TREE_CODE (decl
) == TYPE_DECL
953 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
955 tree extra
= LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl
));
959 else if (template_type_parameter_p (decl
))
960 /* template type parms have no mangling context. */
963 tcontext
= CP_DECL_CONTEXT (decl
);
965 /* Ignore the artificial declare reduction functions. */
967 && TREE_CODE (tcontext
) == FUNCTION_DECL
968 && DECL_OMP_DECLARE_REDUCTION_P (tcontext
))
969 return decl_mangling_context (tcontext
);
974 /* <name> ::= <unscoped-name>
975 ::= <unscoped-template-name> <template-args>
979 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
980 called from <local-name>, which mangles the enclosing scope
981 elsewhere and then uses this function to mangle just the part
982 underneath the function scope. So don't use the <local-name>
983 production, to avoid an infinite recursion. */
986 write_name (tree decl
, const int ignore_local_scope
)
990 MANGLE_TRACE_TREE ("name", decl
);
992 if (TREE_CODE (decl
) == TYPE_DECL
)
994 /* In case this is a typedef, fish out the corresponding
995 TYPE_DECL for the main variant. */
996 decl
= TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl
)));
999 context
= decl_mangling_context (decl
);
1001 gcc_assert (context
!= NULL_TREE
);
1003 if (abi_warn_or_compat_version_crosses (7)
1004 && ignore_local_scope
1005 && TREE_CODE (context
) == PARM_DECL
)
1006 G
.need_abi_warning
= 1;
1008 /* A decl in :: or ::std scope is treated specially. The former is
1009 mangled using <unscoped-name> or <unscoped-template-name>, the
1010 latter with a special substitution. Also, a name that is
1011 directly in a local function scope is also mangled with
1012 <unscoped-name> rather than a full <nested-name>. */
1013 if (context
== global_namespace
1014 || DECL_NAMESPACE_STD_P (context
)
1015 || (ignore_local_scope
1016 && (TREE_CODE (context
) == FUNCTION_DECL
1017 || (abi_version_at_least (7)
1018 && TREE_CODE (context
) == PARM_DECL
))))
1020 /* Is this a template instance? */
1021 if (tree info
= maybe_template_info (decl
))
1023 /* Yes: use <unscoped-template-name>. */
1024 write_unscoped_template_name (TI_TEMPLATE (info
));
1025 write_template_args (TI_ARGS (info
));
1028 /* Everything else gets an <unqualified-name>. */
1029 write_unscoped_name (decl
);
1033 /* Handle local names, unless we asked not to (that is, invoked
1034 under <local-name>, to handle only the part of the name under
1035 the local scope). */
1036 if (!ignore_local_scope
)
1038 /* Scan up the list of scope context, looking for a
1039 function. If we find one, this entity is in local
1040 function scope. local_entity tracks context one scope
1041 level down, so it will contain the element that's
1042 directly in that function's scope, either decl or one of
1043 its enclosing scopes. */
1044 tree local_entity
= decl
;
1045 while (context
!= global_namespace
)
1047 /* Make sure we're always dealing with decls. */
1048 if (TYPE_P (context
))
1049 context
= TYPE_NAME (context
);
1050 /* Is this a function? */
1051 if (TREE_CODE (context
) == FUNCTION_DECL
1052 || TREE_CODE (context
) == PARM_DECL
)
1054 /* Yes, we have local scope. Use the <local-name>
1055 production for the innermost function scope. */
1056 write_local_name (context
, local_entity
, decl
);
1059 /* Up one scope level. */
1060 local_entity
= context
;
1061 context
= decl_mangling_context (context
);
1064 /* No local scope found? Fall through to <nested-name>. */
1067 /* Other decls get a <nested-name> to encode their scope. */
1068 write_nested_name (decl
);
1072 /* <unscoped-name> ::= <unqualified-name>
1073 ::= St <unqualified-name> # ::std:: */
1076 write_unscoped_name (const tree decl
)
1078 tree context
= decl_mangling_context (decl
);
1080 MANGLE_TRACE_TREE ("unscoped-name", decl
);
1082 /* Is DECL in ::std? */
1083 if (DECL_NAMESPACE_STD_P (context
))
1085 write_string ("St");
1086 write_unqualified_name (decl
);
1090 /* If not, it should be either in the global namespace, or directly
1091 in a local function scope. A lambda can also be mangled in the
1092 scope of a default argument. */
1093 gcc_assert (context
== global_namespace
1094 || TREE_CODE (context
) == PARM_DECL
1095 || TREE_CODE (context
) == FUNCTION_DECL
);
1097 write_unqualified_name (decl
);
1101 /* <unscoped-template-name> ::= <unscoped-name>
1102 ::= <substitution> */
1105 write_unscoped_template_name (const tree decl
)
1107 MANGLE_TRACE_TREE ("unscoped-template-name", decl
);
1109 if (find_substitution (decl
))
1111 write_unscoped_name (decl
);
1112 add_substitution (decl
);
1115 /* Write the nested name, including CV-qualifiers, of DECL.
1117 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1118 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1120 <ref-qualifier> ::= R # & ref-qualifier
1121 ::= O # && ref-qualifier
1122 <CV-qualifiers> ::= [r] [V] [K] */
1125 write_nested_name (const tree decl
)
1127 MANGLE_TRACE_TREE ("nested-name", decl
);
1131 /* Write CV-qualifiers, if this is a member function. */
1132 if (TREE_CODE (decl
) == FUNCTION_DECL
1133 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1135 if (DECL_VOLATILE_MEMFUNC_P (decl
))
1137 if (DECL_CONST_MEMFUNC_P (decl
))
1139 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl
)))
1141 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl
)))
1148 /* Is this a template instance? */
1149 if (tree info
= maybe_template_info (decl
))
1151 /* Yes, use <template-prefix>. */
1152 write_template_prefix (decl
);
1153 write_template_args (TI_ARGS (info
));
1155 else if ((!abi_version_at_least (10) || TREE_CODE (decl
) == TYPE_DECL
)
1156 && TREE_CODE (TREE_TYPE (decl
)) == TYPENAME_TYPE
)
1158 tree name
= TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl
));
1159 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1161 write_template_prefix (decl
);
1162 write_template_args (TREE_OPERAND (name
, 1));
1166 write_prefix (decl_mangling_context (decl
));
1167 write_unqualified_name (decl
);
1172 /* No, just use <prefix> */
1173 write_prefix (decl_mangling_context (decl
));
1174 write_unqualified_name (decl
);
1179 /* <prefix> ::= <prefix> <unqualified-name>
1180 ::= <template-param>
1181 ::= <template-prefix> <template-args>
1184 ::= <substitution> */
1187 write_prefix (const tree node
)
1192 || node
== global_namespace
)
1195 MANGLE_TRACE_TREE ("prefix", node
);
1197 if (TREE_CODE (node
) == DECLTYPE_TYPE
)
1203 if (find_substitution (node
))
1206 tree template_info
= NULL_TREE
;
1209 /* If this is a function or parm decl, that means we've hit function
1210 scope, so this prefix must be for a local name. In this
1211 case, we're under the <local-name> production, which encodes
1212 the enclosing function scope elsewhere. So don't continue
1214 if (TREE_CODE (node
) == FUNCTION_DECL
1215 || TREE_CODE (node
) == PARM_DECL
)
1219 template_info
= maybe_template_info (decl
);
1223 /* Node is a type. */
1224 decl
= TYPE_NAME (node
);
1225 /* The DECL might not point at the node. */
1226 if (CLASSTYPE_TEMPLATE_ID_P (node
))
1227 template_info
= TYPE_TEMPLATE_INFO (node
);
1230 if (TREE_CODE (node
) == TEMPLATE_TYPE_PARM
)
1231 write_template_param (node
);
1232 else if (template_info
)
1235 write_template_prefix (decl
);
1236 write_template_args (TI_ARGS (template_info
));
1238 else if (TREE_CODE (TREE_TYPE (decl
)) == TYPENAME_TYPE
)
1240 tree name
= TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl
));
1241 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
1243 write_template_prefix (decl
);
1244 write_template_args (TREE_OPERAND (name
, 1));
1248 write_prefix (decl_mangling_context (decl
));
1249 write_unqualified_name (decl
);
1253 /* Not templated. */
1255 write_prefix (decl_mangling_context (decl
));
1256 write_unqualified_name (decl
);
1258 || TREE_CODE (decl
) == FIELD_DECL
)
1260 /* <data-member-prefix> := <member source-name> M */
1263 /* Before ABI 18, we did not count these as substitution
1264 candidates. This leads to incorrect demanglings (and
1265 ABI divergence to other compilers). */
1266 if (abi_warn_or_compat_version_crosses (18))
1267 G
.need_abi_warning
= true;
1268 if (!abi_version_at_least (18))
1273 add_substitution (node
);
1276 /* <template-prefix> ::= <prefix> <template component>
1277 ::= <template-param>
1278 ::= <substitution> */
1281 write_template_prefix (const tree node
)
1283 tree decl
= DECL_P (node
) ? node
: TYPE_NAME (node
);
1284 tree type
= DECL_P (node
) ? TREE_TYPE (node
) : node
;
1285 tree context
= decl_mangling_context (decl
);
1289 MANGLE_TRACE_TREE ("template-prefix", node
);
1291 /* Find the template decl. */
1292 if (tree info
= maybe_template_info (decl
))
1293 templ
= TI_TEMPLATE (info
);
1294 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
1295 /* For a typename type, all we have is the name. */
1296 templ
= DECL_NAME (decl
);
1299 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type
));
1301 templ
= TYPE_TI_TEMPLATE (type
);
1304 /* For a member template, though, the template name for the
1305 innermost name must have all the outer template levels
1306 instantiated. For instance, consider
1308 template<typename T> struct Outer {
1309 template<typename U> struct Inner {};
1312 The template name for `Inner' in `Outer<int>::Inner<float>' is
1313 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1314 levels separately, so there's no TEMPLATE_DECL available for this
1315 (there's only `Outer<T>::Inner<U>').
1317 In order to get the substitutions right, we create a special
1318 TREE_LIST to represent the substitution candidate for a nested
1319 template. The TREE_PURPOSE is the template's context, fully
1320 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1323 So, for the example above, `Outer<int>::Inner' is represented as a
1324 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1325 and whose value is `Outer<T>::Inner<U>'. */
1326 if (context
&& TYPE_P (context
))
1327 substitution
= build_tree_list (context
, templ
);
1329 substitution
= templ
;
1331 if (find_substitution (substitution
))
1334 if (TREE_TYPE (templ
)
1335 && TREE_CODE (TREE_TYPE (templ
)) == TEMPLATE_TEMPLATE_PARM
)
1336 write_template_param (TREE_TYPE (templ
));
1339 write_prefix (context
);
1340 write_unqualified_name (decl
);
1343 add_substitution (substitution
);
1346 /* As the list of identifiers for the structured binding declaration
1347 DECL is likely gone, try to recover the DC <source-name>+ E portion
1348 from its mangled name. Return pointer to the DC and set len to
1349 the length up to and including the terminating E. On failure
1353 find_decomp_unqualified_name (tree decl
, size_t *len
)
1355 const char *p
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
1356 const char *end
= p
+ IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (decl
));
1357 bool nested
= false;
1358 if (!startswith (p
, "_Z"))
1361 if (startswith (p
, "St"))
1367 while (ISDIGIT (p
[0]))
1370 long num
= strtol (p
, &e
, 10);
1371 if (num
>= 1 && num
< end
- e
)
1377 if (!startswith (p
, "DC"))
1391 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1392 mangled through special entry points.
1394 <unqualified-name> ::= [<module-name>] <operator-name>
1396 ::= [<module-name>] <source-name>
1397 ::= [<module-name>] <unnamed-type-name>
1398 ::= <local-source-name>
1400 <local-source-name> ::= L <source-name> <discriminator> */
1403 write_unqualified_id (tree identifier
)
1405 if (IDENTIFIER_CONV_OP_P (identifier
))
1406 write_conversion_operator_name (TREE_TYPE (identifier
));
1407 else if (IDENTIFIER_OVL_OP_P (identifier
))
1409 const ovl_op_info_t
*ovl_op
= IDENTIFIER_OVL_OP_INFO (identifier
);
1410 write_string (ovl_op
->mangled_name
);
1412 else if (UDLIT_OPER_P (identifier
))
1413 write_literal_operator_name (identifier
);
1415 write_source_name (identifier
);
1419 write_unqualified_name (tree decl
)
1421 MANGLE_TRACE_TREE ("unqualified-name", decl
);
1424 maybe_write_module (decl
);
1426 if (identifier_p (decl
))
1428 write_unqualified_id (decl
);
1434 if (DECL_NAME (decl
) == NULL_TREE
)
1437 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
1438 const char *decomp_str
= NULL
;
1439 size_t decomp_len
= 0;
1441 && DECL_DECOMPOSITION_P (decl
)
1442 && DECL_NAME (decl
) == NULL_TREE
1443 && DECL_NAMESPACE_SCOPE_P (decl
))
1444 decomp_str
= find_decomp_unqualified_name (decl
, &decomp_len
);
1446 write_chars (decomp_str
, decomp_len
);
1448 write_source_name (DECL_ASSEMBLER_NAME (decl
));
1450 else if (DECL_DECLARES_FUNCTION_P (decl
))
1453 if (DECL_CONSTRUCTOR_P (decl
))
1454 write_special_name_constructor (decl
);
1455 else if (DECL_DESTRUCTOR_P (decl
))
1456 write_special_name_destructor (decl
);
1457 else if (DECL_CONV_FN_P (decl
))
1459 /* Conversion operator. Handle it right here.
1460 <operator> ::= cv <type> */
1462 if (maybe_template_info (decl
))
1465 fn_type
= get_mostly_instantiated_function_type (decl
);
1466 type
= TREE_TYPE (fn_type
);
1468 else if (FNDECL_USED_AUTO (decl
))
1469 type
= DECL_SAVED_AUTO_RETURN_TYPE (decl
);
1471 type
= DECL_CONV_FN_TYPE (decl
);
1472 write_conversion_operator_name (type
);
1474 else if (DECL_OVERLOADED_OPERATOR_P (decl
))
1477 if (!(t
= DECL_RAMP_FN (decl
)))
1479 const char *mangled_name
1480 = (ovl_op_info
[DECL_ASSIGNMENT_OPERATOR_P (t
)]
1481 [DECL_OVERLOADED_OPERATOR_CODE_RAW (t
)].mangled_name
);
1482 write_string (mangled_name
);
1484 else if (UDLIT_OPER_P (DECL_NAME (decl
)))
1485 write_literal_operator_name (DECL_NAME (decl
));
1492 else if (VAR_OR_FUNCTION_DECL_P (decl
) && ! TREE_PUBLIC (decl
)
1493 && DECL_NAMESPACE_SCOPE_P (decl
)
1494 && decl_linkage (decl
) == lk_internal
)
1496 MANGLE_TRACE_TREE ("local-source-name", decl
);
1498 write_source_name (DECL_NAME (decl
));
1499 /* The default discriminator is 1, and that's all we ever use,
1500 so there's no code to output one here. */
1504 tree type
= TREE_TYPE (decl
);
1506 if (TREE_CODE (decl
) == TYPE_DECL
1507 && TYPE_UNNAMED_P (type
))
1508 write_unnamed_type_name (type
);
1509 else if (TREE_CODE (decl
) == TYPE_DECL
&& LAMBDA_TYPE_P (type
))
1510 write_closure_type_name (type
);
1512 write_source_name (DECL_NAME (decl
));
1515 /* We use the ABI tags from the primary class template, ignoring tags on any
1516 specializations. This is necessary because C++ doesn't require a
1517 specialization to be declared before it is used unless the use requires a
1518 complete type, but we need to get the tags right on incomplete types as
1520 if (tree tmpl
= most_general_template (decl
))
1522 tree res
= DECL_TEMPLATE_RESULT (tmpl
);
1523 if (res
== NULL_TREE
)
1524 /* UNBOUND_CLASS_TEMPLATE. */;
1525 else if (DECL_DECLARES_TYPE_P (decl
))
1527 else if (any_abi_below (11))
1529 /* ABI v10 implicit tags on the template. */
1530 tree mtags
= missing_abi_tags (res
);
1531 /* Explicit tags on the template. */
1532 tree ttags
= get_abi_tags (res
);
1533 /* Tags on the instantiation. */
1534 tree dtags
= get_abi_tags (decl
);
1536 if (mtags
&& abi_warn_or_compat_version_crosses (10))
1537 G
.need_abi_warning
= 1;
1539 /* Add the v10 tags to the explicit tags now. */
1540 mtags
= chainon (mtags
, ttags
);
1542 if (!G
.need_abi_warning
1543 && abi_warn_or_compat_version_crosses (11)
1544 && !equal_abi_tags (dtags
, mtags
))
1545 G
.need_abi_warning
= 1;
1547 if (!abi_version_at_least (10))
1548 /* In abi <10, we only got the explicit tags. */
1550 else if (flag_abi_version
== 10)
1552 /* In ABI 10, we want explict and implicit tags. */
1553 write_abi_tags (mtags
);
1559 tree tags
= get_abi_tags (decl
);
1560 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_CONV_FN_P (decl
)
1561 && any_abi_below (11))
1562 if (tree mtags
= missing_abi_tags (decl
))
1564 if (abi_warn_or_compat_version_crosses (11))
1565 G
.need_abi_warning
= true;
1566 if (!abi_version_at_least (11))
1567 tags
= chainon (mtags
, tags
);
1569 write_abi_tags (tags
);
1572 /* Write the unqualified-name for a conversion operator to TYPE. */
1575 write_conversion_operator_name (const tree type
)
1577 write_string ("cv");
1581 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1583 <source-name> ::= </length/ number> <identifier> */
1586 write_source_name (tree identifier
)
1588 MANGLE_TRACE_TREE ("source-name", identifier
);
1590 write_unsigned_number (IDENTIFIER_LENGTH (identifier
));
1591 write_identifier (IDENTIFIER_POINTER (identifier
));
1594 /* Compare two TREE_STRINGs like strcmp. */
1597 tree_string_cmp (const void *p1
, const void *p2
)
1601 tree s1
= *(const tree
*)p1
;
1602 tree s2
= *(const tree
*)p2
;
1603 return strcmp (TREE_STRING_POINTER (s1
),
1604 TREE_STRING_POINTER (s2
));
1607 /* Return the TREE_LIST of TAGS as a sorted VEC. */
1609 static vec
<tree
, va_gc
> *
1610 sorted_abi_tags (tree tags
)
1612 vec
<tree
, va_gc
> * vec
= make_tree_vector();
1614 for (tree t
= tags
; t
; t
= TREE_CHAIN (t
))
1616 if (ABI_TAG_IMPLICIT (t
))
1618 tree str
= TREE_VALUE (t
);
1619 vec_safe_push (vec
, str
);
1622 vec
->qsort (tree_string_cmp
);
1627 /* ID is the name of a function or type with abi_tags attribute TAGS.
1628 Write out the name, suitably decorated. */
1631 write_abi_tags (tree tags
)
1633 if (tags
== NULL_TREE
)
1636 vec
<tree
, va_gc
> * vec
= sorted_abi_tags (tags
);
1638 unsigned i
; tree str
;
1639 FOR_EACH_VEC_ELT (*vec
, i
, str
)
1642 write_unsigned_number (TREE_STRING_LENGTH (str
) - 1);
1643 write_identifier (TREE_STRING_POINTER (str
));
1646 release_tree_vector (vec
);
1649 /* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */
1652 equal_abi_tags (tree t1
, tree t2
)
1654 releasing_vec v1
= sorted_abi_tags (t1
);
1655 releasing_vec v2
= sorted_abi_tags (t2
);
1657 unsigned len1
= v1
->length();
1658 if (len1
!= v2
->length())
1660 for (unsigned i
= 0; i
< len1
; ++i
)
1661 if (tree_string_cmp (v1
[i
], v2
[i
]) != 0)
1666 /* Write a user-defined literal operator.
1667 ::= li <source-name> # "" <source-name>
1668 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1671 write_literal_operator_name (tree identifier
)
1673 const char* suffix
= UDLIT_OP_SUFFIX (identifier
);
1674 write_identifier (UDLIT_OP_MANGLED_PREFIX
);
1675 write_unsigned_number (strlen (suffix
));
1676 write_identifier (suffix
);
1679 /* Encode 0 as _, and 1+ as n-1_. */
1682 write_compact_number (int num
)
1684 gcc_checking_assert (num
>= 0);
1686 write_unsigned_number (num
- 1);
1690 /* Return how many unnamed types precede TYPE in its enclosing class. */
1693 nested_anon_class_index (tree type
)
1696 tree member
= TYPE_FIELDS (TYPE_CONTEXT (type
));
1697 for (; member
; member
= DECL_CHAIN (member
))
1698 if (DECL_IMPLICIT_TYPEDEF_P (member
))
1700 tree memtype
= TREE_TYPE (member
);
1701 if (memtype
== type
)
1703 else if (TYPE_UNNAMED_P (memtype
))
1713 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1716 write_unnamed_type_name (const tree type
)
1719 MANGLE_TRACE_TREE ("unnamed-type-name", type
);
1721 if (TYPE_FUNCTION_SCOPE_P (type
))
1722 discriminator
= discriminator_for_local_entity (TYPE_NAME (type
));
1723 else if (TYPE_CLASS_SCOPE_P (type
))
1724 discriminator
= nested_anon_class_index (type
);
1727 gcc_assert (no_linkage_check (type
, /*relaxed_p=*/true));
1728 /* Just use the old mangling at namespace scope. */
1729 write_source_name (TYPE_IDENTIFIER (type
));
1733 write_string ("Ut");
1734 write_compact_number (discriminator
);
1737 // A template head, for templated lambdas.
1738 // <template-head> ::= Tp* Ty
1740 // Tp* Tt <template-head> E
1741 // New in ABI=18. Returns true iff we emitted anything -- used for ABI
1745 write_closure_template_head (tree tmpl
)
1749 // We only need one level of template parms
1750 tree inner
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
1752 for (int ix
= 0, len
= TREE_VEC_LENGTH (inner
); ix
!= len
; ix
++)
1754 tree parm
= TREE_VEC_ELT (inner
, ix
);
1755 if (parm
== error_mark_node
)
1757 parm
= TREE_VALUE (parm
);
1759 if (DECL_VIRTUAL_P (parm
))
1760 // A synthetic parm, we're done.
1764 if (abi_version_at_least (18))
1766 if (TREE_CODE (parm
) == PARM_DECL
1767 ? TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
1768 : TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
1769 write_string ("Tp");
1771 switch (TREE_CODE (parm
))
1777 write_string ("Ty");
1781 write_string ("Tn");
1782 write_type (TREE_TYPE (parm
));
1786 write_string ("Tt");
1787 write_closure_template_head (parm
);
1797 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1798 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1801 write_closure_type_name (const tree type
)
1803 tree fn
= lambda_function (type
);
1804 tree lambda
= CLASSTYPE_LAMBDA_EXPR (type
);
1805 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1807 MANGLE_TRACE_TREE ("closure-type-name", type
);
1809 write_string ("Ul");
1811 if (auto ti
= maybe_template_info (fn
))
1812 if (write_closure_template_head (TI_TEMPLATE (ti
)))
1813 // If there were any explicit template parms, we may need to
1814 // issue a mangling diagnostic.
1815 if (abi_warn_or_compat_version_crosses (18))
1816 G
.need_abi_warning
= true;
1818 write_method_parms (parms
, /*method_p=*/1, fn
);
1820 if ((LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda
)
1821 != LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda
))
1822 && abi_warn_or_compat_version_crosses (18))
1823 G
.need_abi_warning
= true;
1824 write_compact_number (abi_version_at_least (18)
1825 ? LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda
)
1826 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda
));
1829 /* Convert NUMBER to ascii using base BASE and generating at least
1830 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1831 into which to store the characters. Returns the number of
1832 characters generated (these will be laid out in advance of where
1836 hwint_to_ascii (unsigned HOST_WIDE_INT number
, const unsigned int base
,
1837 char *buffer
, const unsigned int min_digits
)
1839 static const char base_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1840 unsigned digits
= 0;
1844 unsigned HOST_WIDE_INT d
= number
/ base
;
1846 *--buffer
= base_digits
[number
- d
* base
];
1850 while (digits
< min_digits
)
1852 *--buffer
= base_digits
[0];
1858 /* Non-terminal <number>.
1860 <number> ::= [n] </decimal integer/> */
1863 write_number (unsigned HOST_WIDE_INT number
, const int unsigned_p
,
1864 const unsigned int base
)
1866 char buffer
[sizeof (HOST_WIDE_INT
) * 8];
1869 if (!unsigned_p
&& (HOST_WIDE_INT
) number
< 0)
1872 number
= -((HOST_WIDE_INT
) number
);
1874 count
= hwint_to_ascii (number
, base
, buffer
+ sizeof (buffer
), 1);
1875 write_chars (buffer
+ sizeof (buffer
) - count
, count
);
1878 /* Write out an integral CST in decimal. Most numbers are small, and
1879 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1880 bigger than that, which we must deal with. */
1883 write_integer_cst (const tree cst
)
1885 int sign
= tree_int_cst_sgn (cst
);
1886 widest_int abs_value
= wi::abs (wi::to_widest (cst
));
1887 if (!wi::fits_uhwi_p (abs_value
))
1889 /* A bignum. We do this in chunks, each of which fits in a
1891 char buffer
[sizeof (HOST_WIDE_INT
) * 8 * 2];
1892 unsigned HOST_WIDE_INT chunk
;
1893 unsigned chunk_digits
;
1894 char *ptr
= buffer
+ sizeof (buffer
);
1899 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1904 if (sizeof (HOST_WIDE_INT
) >= 8)
1906 /* It is at least 64 bits, so 10^18 is representable. */
1911 type
= c_common_signed_or_unsigned_type (1, TREE_TYPE (cst
));
1912 base
= build_int_cstu (type
, chunk
);
1913 n
= wide_int_to_tree (type
, wi::to_wide (cst
));
1918 n
= fold_build1_loc (input_location
, NEGATE_EXPR
, type
, n
);
1922 tree d
= fold_build2_loc (input_location
, FLOOR_DIV_EXPR
, type
, n
, base
);
1923 tree tmp
= fold_build2_loc (input_location
, MULT_EXPR
, type
, d
, base
);
1926 done
= integer_zerop (d
);
1927 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
, type
, n
, tmp
);
1928 c
= hwint_to_ascii (TREE_INT_CST_LOW (tmp
), 10, ptr
,
1929 done
? 1 : chunk_digits
);
1935 write_chars (ptr
, count
);
1942 write_unsigned_number (abs_value
.to_uhwi ());
1946 /* Write out a floating-point literal.
1948 "Floating-point literals are encoded using the bit pattern of the
1949 target processor's internal representation of that number, as a
1950 fixed-length lowercase hexadecimal string, high-order bytes first
1951 (even if the target processor would store low-order bytes first).
1952 The "n" prefix is not used for floating-point literals; the sign
1953 bit is encoded with the rest of the number.
1955 Here are some examples, assuming the IEEE standard representation
1956 for floating point numbers. (Spaces are for readability, not
1957 part of the encoding.)
1960 -1.0f Lf bf80 0000 E
1961 1.17549435e-38f Lf 0080 0000 E
1962 1.40129846e-45f Lf 0000 0001 E
1963 0.0f Lf 0000 0000 E"
1965 Caller is responsible for the Lx and the E. */
1967 write_real_cst (const tree value
)
1969 long target_real
[4]; /* largest supported float */
1970 /* Buffer for eight hex digits in a 32-bit number but big enough
1971 even for 64-bit long to avoid warnings. */
1975 tree type
= TREE_TYPE (value
);
1976 int words
= GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type
)) / 32;
1978 real_to_target (target_real
, &TREE_REAL_CST (value
),
1981 /* The value in target_real is in the target word order,
1982 so we must write it out backward if that happens to be
1983 little-endian. write_number cannot be used, it will
1984 produce uppercase. */
1985 if (FLOAT_WORDS_BIG_ENDIAN
)
1986 i
= 0, limit
= words
, dir
= 1;
1988 i
= words
- 1, limit
= -1, dir
= -1;
1990 for (; i
!= limit
; i
+= dir
)
1992 sprintf (buffer
, "%08lx", (unsigned long) target_real
[i
]);
1993 write_chars (buffer
, 8);
1997 /* Non-terminal <identifier>.
1999 <identifier> ::= </unqualified source code identifier> */
2002 write_identifier (const char *identifier
)
2004 MANGLE_TRACE ("identifier", identifier
);
2005 write_string (identifier
);
2008 /* Handle constructor productions of non-terminal <special-name>.
2009 CTOR is a constructor FUNCTION_DECL.
2011 <special-name> ::= C1 # complete object constructor
2012 ::= C2 # base object constructor
2013 ::= C3 # complete object allocating constructor
2015 Currently, allocating constructors are never used. */
2018 write_special_name_constructor (const tree ctor
)
2021 bool new_inh
= (flag_new_inheriting_ctors
2022 && DECL_INHERITED_CTOR (ctor
));
2025 if (DECL_BASE_CONSTRUCTOR_P (ctor
))
2027 /* This is the old-style "[unified]" constructor.
2028 In some cases, we may emit this function and call
2029 it from the clones in order to share code and save space. */
2030 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor
))
2034 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor
));
2038 write_type (DECL_INHERITED_CTOR_BASE (ctor
));
2041 /* Handle destructor productions of non-terminal <special-name>.
2042 DTOR is a destructor FUNCTION_DECL.
2044 <special-name> ::= D0 # deleting (in-charge) destructor
2045 ::= D1 # complete object (in-charge) destructor
2046 ::= D2 # base object (not-in-charge) destructor */
2049 write_special_name_destructor (const tree dtor
)
2051 if (DECL_DELETING_DESTRUCTOR_P (dtor
))
2052 write_string ("D0");
2053 else if (DECL_BASE_DESTRUCTOR_P (dtor
))
2054 write_string ("D2");
2055 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor
))
2056 /* This is the old-style "[unified]" destructor.
2057 In some cases, we may emit this function and call
2058 it from the clones in order to share code and save space. */
2059 write_string ("D4");
2062 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor
));
2063 write_string ("D1");
2067 /* Return the discriminator for ENTITY appearing inside
2068 FUNCTION. The discriminator is the lexical ordinal of VAR or TYPE among
2069 entities with the same name and kind in the same FUNCTION. */
2072 discriminator_for_local_entity (tree entity
)
2074 if (!DECL_LANG_SPECIFIC (entity
))
2076 /* Some decls, like __FUNCTION__, don't need a discriminator. */
2077 gcc_checking_assert (DECL_ARTIFICIAL (entity
));
2080 else if (tree disc
= DECL_DISCRIMINATOR (entity
))
2081 return TREE_INT_CST_LOW (disc
);
2083 /* The first entity with a particular name doesn't get
2084 DECL_DISCRIMINATOR set up. */
2088 /* Return the discriminator for STRING, a string literal used inside
2089 FUNCTION. The discriminator is the lexical ordinal of STRING among
2090 string literals used in FUNCTION. */
2093 discriminator_for_string_literal (tree
/*function*/,
2096 /* For now, we don't discriminate amongst string literals. */
2100 /* <discriminator> := _ <number> # when number < 10
2101 := __ <number> _ # when number >= 10
2103 The discriminator is used only for the second and later occurrences
2104 of the same name within a single function. In this case <number> is
2105 n - 2, if this is the nth occurrence, in lexical order. */
2108 write_discriminator (const int discriminator
)
2110 /* If discriminator is zero, don't write anything. Otherwise... */
2111 if (discriminator
> 0)
2114 if (discriminator
- 1 >= 10)
2116 if (abi_warn_or_compat_version_crosses (11))
2117 G
.need_abi_warning
= 1;
2118 if (abi_version_at_least (11))
2121 write_unsigned_number (discriminator
- 1);
2122 if (abi_version_at_least (11) && discriminator
- 1 >= 10)
2127 /* Mangle the name of a function-scope entity. FUNCTION is the
2128 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
2129 default argument scope. ENTITY is the decl for the entity itself.
2130 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
2131 either ENTITY itself or an enclosing scope of ENTITY.
2133 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2134 := Z <function encoding> E s [<discriminator>]
2135 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
2138 write_local_name (tree function
, const tree local_entity
,
2141 tree parm
= NULL_TREE
;
2143 MANGLE_TRACE_TREE ("local-name", entity
);
2145 if (TREE_CODE (function
) == PARM_DECL
)
2148 function
= DECL_CONTEXT (parm
);
2152 write_encoding (function
);
2155 /* For this purpose, parameters are numbered from right-to-left. */
2158 int i
= list_length (parm
);
2160 write_compact_number (i
- 1);
2163 if (TREE_CODE (entity
) == STRING_CST
)
2166 write_discriminator (discriminator_for_string_literal (function
,
2171 /* Now the <entity name>. Let write_name know its being called
2172 from <local-name>, so it doesn't try to process the enclosing
2173 function scope again. */
2174 write_name (entity
, /*ignore_local_scope=*/1);
2175 if (DECL_DISCRIMINATOR_P (local_entity
)
2176 && !(TREE_CODE (local_entity
) == TYPE_DECL
2177 && TYPE_ANON_P (TREE_TYPE (local_entity
))))
2178 write_discriminator (discriminator_for_local_entity (local_entity
));
2182 /* Non-terminals <type> and <CV-qualifier>.
2184 <type> ::= <builtin-type>
2186 ::= <class-enum-type>
2188 ::= <pointer-to-member-type>
2189 ::= <template-param>
2192 ::= P <type> # pointer-to
2193 ::= R <type> # reference-to
2194 ::= C <type> # complex pair (C 2000)
2195 ::= G <type> # imaginary (C 2000) [not supported]
2196 ::= U <source-name> <type> # vendor extended type qualifier
2200 <type> ::= RR <type> # rvalue reference-to
2201 <type> ::= Dt <expression> # decltype of an id-expression or
2202 # class member access
2203 <type> ::= DT <expression> # decltype of an expression
2204 <type> ::= Dn # decltype of nullptr
2206 TYPE is a type node. */
2209 write_type (tree type
)
2211 /* This gets set to nonzero if TYPE turns out to be a (possibly
2212 CV-qualified) builtin type. */
2213 int is_builtin_type
= 0;
2215 MANGLE_TRACE_TREE ("type", type
);
2217 if (type
== error_mark_node
)
2220 type
= canonicalize_for_substitution (type
);
2221 if (find_substitution (type
))
2225 if (write_CV_qualifiers_for_type (type
) > 0)
2226 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
2227 mangle the unqualified type. The recursive call is needed here
2228 since both the qualified and unqualified types are substitution
2231 tree t
= TYPE_MAIN_VARIANT (type
);
2232 if (TYPE_ATTRIBUTES (t
) && !OVERLOAD_TYPE_P (t
))
2234 tree attrs
= NULL_TREE
;
2235 if (tx_safe_fn_type_p (type
))
2236 attrs
= tree_cons (get_identifier ("transaction_safe"),
2238 t
= cp_build_type_attribute_variant (t
, attrs
);
2240 gcc_assert (t
!= type
);
2241 if (FUNC_OR_METHOD_TYPE_P (t
))
2243 t
= build_ref_qualified_type (t
, type_memfn_rqual (type
));
2244 if (flag_noexcept_type
)
2246 tree r
= TYPE_RAISES_EXCEPTIONS (type
);
2247 t
= build_exception_variant (t
, r
);
2249 if (abi_version_at_least (8)
2250 || type
== TYPE_MAIN_VARIANT (type
))
2251 /* Avoid adding the unqualified function type as a substitution. */
2252 write_function_type (t
);
2255 if (abi_warn_or_compat_version_crosses (8))
2256 G
.need_abi_warning
= 1;
2261 else if (TREE_CODE (type
) == ARRAY_TYPE
)
2262 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
2263 so that the cv-qualification of the element type is available
2264 in write_array_type. */
2265 write_array_type (type
);
2268 tree type_orig
= type
;
2270 /* See through any typedefs. */
2271 type
= TYPE_MAIN_VARIANT (type
);
2272 if (FUNC_OR_METHOD_TYPE_P (type
))
2273 type
= cxx_copy_lang_qualifiers (type
, type_orig
);
2275 /* According to the C++ ABI, some library classes are passed the
2276 same as the scalar type of their single member and use the same
2278 if (TREE_CODE (type
) == RECORD_TYPE
&& TYPE_TRANSPARENT_AGGR (type
))
2279 type
= TREE_TYPE (first_field (type
));
2281 if (TYPE_PTRDATAMEM_P (type
))
2282 write_pointer_to_member_type (type
);
2285 /* Handle any target-specific fundamental types. */
2286 const char *target_mangling
2287 = targetm
.mangle_type (type_orig
);
2289 if (target_mangling
)
2291 write_string (target_mangling
);
2292 /* Add substitutions for types other than fundamental
2294 if (!VOID_TYPE_P (type
)
2295 && TREE_CODE (type
) != INTEGER_TYPE
2296 && TREE_CODE (type
) != REAL_TYPE
2297 && TREE_CODE (type
) != BOOLEAN_TYPE
)
2298 add_substitution (type
);
2302 switch (TREE_CODE (type
))
2306 case INTEGER_TYPE
: /* Includes wchar_t. */
2308 case FIXED_POINT_TYPE
:
2310 /* If this is a typedef, TYPE may not be one of
2311 the standard builtin type nodes, but an alias of one. Use
2312 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
2313 write_builtin_type (TYPE_MAIN_VARIANT (type
));
2320 write_type (TREE_TYPE (type
));
2325 write_function_type (type
);
2331 /* A pointer-to-member function is represented as a special
2332 RECORD_TYPE, so check for this first. */
2333 if (TYPE_PTRMEMFUNC_P (type
))
2334 write_pointer_to_member_type (type
);
2336 write_class_enum_type (type
);
2340 case UNBOUND_CLASS_TEMPLATE
:
2341 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2342 ordinary nested names. */
2343 write_nested_name (TYPE_STUB_DECL (type
));
2347 case REFERENCE_TYPE
:
2348 if (TYPE_PTR_P (type
))
2350 else if (TYPE_REF_IS_RVALUE (type
))
2355 tree target
= TREE_TYPE (type
);
2356 /* Attribute const/noreturn are not reflected in mangling.
2357 We strip them here rather than at a lower level because
2358 a typedef or template argument can have function type
2359 with function-cv-quals (that use the same representation),
2360 but you can't have a pointer/reference to such a type. */
2361 if (TREE_CODE (target
) == FUNCTION_TYPE
)
2363 if (abi_warn_or_compat_version_crosses (5)
2364 && TYPE_QUALS (target
) != TYPE_UNQUALIFIED
)
2365 G
.need_abi_warning
= 1;
2366 if (abi_version_at_least (5))
2367 target
= build_qualified_type (target
, TYPE_UNQUALIFIED
);
2369 write_type (target
);
2373 case TEMPLATE_TYPE_PARM
:
2376 if (AUTO_IS_DECLTYPE (type
))
2377 write_identifier ("Dc");
2379 write_identifier ("Da");
2384 case TEMPLATE_PARM_INDEX
:
2385 write_template_param (type
);
2388 case TEMPLATE_TEMPLATE_PARM
:
2389 write_template_template_param (type
);
2392 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2393 write_template_template_param (type
);
2395 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type
)));
2399 if (abi_version_at_least (4))
2401 write_string ("Dv");
2402 /* Non-constant vector size would be encoded with
2403 _ expression, but we don't support that yet. */
2404 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type
)
2409 write_string ("U8__vector");
2410 if (abi_warn_or_compat_version_crosses (4))
2411 G
.need_abi_warning
= 1;
2412 write_type (TREE_TYPE (type
));
2415 case TYPE_PACK_EXPANSION
:
2416 write_string ("Dp");
2417 write_type (PACK_EXPANSION_PATTERN (type
));
2421 /* These shouldn't make it into mangling. */
2422 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type
)
2423 && !DECLTYPE_FOR_LAMBDA_PROXY (type
));
2425 /* In ABI <5, we stripped decltype of a plain decl. */
2426 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type
))
2428 tree expr
= DECLTYPE_TYPE_EXPR (type
);
2429 tree etype
= NULL_TREE
;
2430 switch (TREE_CODE (expr
))
2437 case TEMPLATE_PARM_INDEX
:
2438 etype
= TREE_TYPE (expr
);
2445 if (etype
&& !type_uses_auto (etype
))
2447 if (abi_warn_or_compat_version_crosses (5))
2448 G
.need_abi_warning
= 1;
2449 if (!abi_version_at_least (5))
2458 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type
))
2462 ++cp_unevaluated_operand
;
2463 write_expression (DECLTYPE_TYPE_EXPR (type
));
2464 --cp_unevaluated_operand
;
2469 write_string ("Dn");
2470 if (abi_version_at_least (7))
2472 if (abi_warn_or_compat_version_crosses (7))
2473 G
.need_abi_warning
= 1;
2477 sorry ("mangling %<typeof%>, use %<decltype%> instead");
2481 error ("use of built-in trait %qT in function signature; "
2482 "use library traits instead", type
);
2494 /* Types other than builtin types are substitution candidates. */
2495 if (!is_builtin_type
)
2496 add_substitution (type
);
2499 /* qsort callback for sorting a vector of attribute entries. */
2502 attr_strcmp (const void *p1
, const void *p2
)
2504 tree a1
= *(const tree
*)p1
;
2505 tree a2
= *(const tree
*)p2
;
2507 const attribute_spec
*as1
= lookup_attribute_spec (get_attribute_name (a1
));
2508 const attribute_spec
*as2
= lookup_attribute_spec (get_attribute_name (a2
));
2510 return strcmp (as1
->name
, as2
->name
);
2513 /* Return true if we should mangle a type attribute with name NAME. */
2516 mangle_type_attribute_p (tree name
)
2518 const attribute_spec
*as
= lookup_attribute_spec (name
);
2519 if (!as
|| !as
->affects_type_identity
)
2522 /* Skip internal-only attributes, which are distinguished from others
2523 by having a space. At present, all internal-only attributes that
2524 affect type identity are target-specific and are handled by
2525 targetm.mangle_type instead.
2527 Another reason to do this is that a space isn't a valid identifier
2528 character for most file formats. */
2529 if (strchr (IDENTIFIER_POINTER (name
), ' '))
2532 /* The following attributes are mangled specially. */
2533 if (is_attribute_p ("transaction_safe", name
))
2535 if (is_attribute_p ("abi_tag", name
))
2541 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2542 CV-qualifiers written for TYPE.
2544 <CV-qualifiers> ::= [r] [V] [K] */
2547 write_CV_qualifiers_for_type (const tree type
)
2549 int num_qualifiers
= 0;
2551 /* The order is specified by:
2553 "In cases where multiple order-insensitive qualifiers are
2554 present, they should be ordered 'K' (closest to the base type),
2555 'V', 'r', and 'U' (farthest from the base type) ..." */
2557 /* Mangle attributes that affect type identity as extended qualifiers.
2559 We don't do this with classes and enums because their attributes
2560 are part of their definitions, not something added on. */
2562 if (!OVERLOAD_TYPE_P (type
))
2565 for (tree a
= TYPE_ATTRIBUTES (type
); a
; a
= TREE_CHAIN (a
))
2566 if (mangle_type_attribute_p (get_attribute_name (a
)))
2568 if (abi_warn_or_compat_version_crosses (10) && !vec
.is_empty ())
2569 G
.need_abi_warning
= true;
2570 if (abi_version_at_least (10))
2572 vec
.qsort (attr_strcmp
);
2573 while (!vec
.is_empty())
2576 const attribute_spec
*as
2577 = lookup_attribute_spec (get_attribute_name (a
));
2580 write_unsigned_number (strlen (as
->name
));
2581 write_string (as
->name
);
2585 for (tree args
= TREE_VALUE (a
); args
;
2586 args
= TREE_CHAIN (args
))
2588 tree arg
= TREE_VALUE (args
);
2589 write_template_arg (arg
);
2599 /* Note that we do not use cp_type_quals below; given "const
2600 int[3]", the "const" is emitted with the "int", not with the
2602 cp_cv_quals quals
= TYPE_QUALS (type
);
2604 if (quals
& TYPE_QUAL_RESTRICT
)
2609 if (quals
& TYPE_QUAL_VOLATILE
)
2614 if (quals
& TYPE_QUAL_CONST
)
2620 return num_qualifiers
;
2623 /* Non-terminal <builtin-type>.
2625 <builtin-type> ::= v # void
2630 ::= h # unsigned char
2632 ::= t # unsigned short
2634 ::= j # unsigned int
2636 ::= m # unsigned long
2637 ::= x # long long, __int64
2638 ::= y # unsigned long long, __int64
2640 ::= o # unsigned __int128
2643 ::= e # long double, __float80
2644 ::= g # __float128 [not supported]
2645 ::= u <source-name> # vendor extended type */
2648 write_builtin_type (tree type
)
2650 if (TYPE_CANONICAL (type
))
2651 type
= TYPE_CANONICAL (type
);
2653 switch (TREE_CODE (type
))
2664 /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
2665 isn't in integer_type_nodes. */
2666 if (type
== wchar_type_node
)
2668 else if (type
== char8_type_node
)
2669 write_string ("Du");
2670 else if (type
== char16_type_node
)
2671 write_string ("Ds");
2672 else if (type
== char32_type_node
)
2673 write_string ("Di");
2677 /* Assume TYPE is one of the shared integer type nodes. Find
2678 it in the array of these nodes. */
2680 for (itk
= 0; itk
< itk_none
; ++itk
)
2681 if (integer_types
[itk
] != NULL_TREE
2682 && integer_type_codes
[itk
] != '\0'
2683 && type
== integer_types
[itk
])
2685 /* Print the corresponding single-letter code. */
2686 write_char (integer_type_codes
[itk
]);
2690 if (itk
== itk_none
)
2692 tree t
= c_common_type_for_mode (TYPE_MODE (type
),
2693 TYPE_UNSIGNED (type
));
2700 if (TYPE_PRECISION (type
) == 128)
2701 write_char (TYPE_UNSIGNED (type
) ? 'o' : 'n');
2704 /* Allow for cases where TYPE is not one of the shared
2705 integer type nodes and write a "vendor extended builtin
2706 type" with a name the form intN or uintN, respectively.
2707 Situations like this can happen if you have an
2708 __attribute__((__mode__(__SI__))) type and use exotic
2709 switches like '-mint8' on AVR. Of course, this is
2710 undefined by the C++ ABI (and '-mint8' is not even
2711 Standard C conforming), but when using such special
2712 options you're pretty much in nowhere land anyway. */
2714 char prec
[11]; /* up to ten digits for an unsigned */
2716 prefix
= TYPE_UNSIGNED (type
) ? "uint" : "int";
2717 sprintf (prec
, "%u", (unsigned) TYPE_PRECISION (type
));
2718 write_char ('u'); /* "vendor extended builtin type" */
2719 write_unsigned_number (strlen (prefix
) + strlen (prec
));
2720 write_string (prefix
);
2721 write_string (prec
);
2728 if (type
== float_type_node
)
2730 else if (type
== double_type_node
)
2732 else if (type
== long_double_type_node
)
2734 else if (type
== dfloat32_type_node
|| type
== fallback_dfloat32_type
)
2735 write_string ("Df");
2736 else if (type
== dfloat64_type_node
|| type
== fallback_dfloat64_type
)
2737 write_string ("Dd");
2738 else if (type
== dfloat128_type_node
|| type
== fallback_dfloat128_type
)
2739 write_string ("De");
2740 else if (type
== float16_type_node
)
2741 write_string ("DF16_");
2742 else if (type
== float32_type_node
)
2743 write_string ("DF32_");
2744 else if (type
== float64_type_node
)
2745 write_string ("DF64_");
2746 else if (type
== float128_type_node
)
2747 write_string ("DF128_");
2748 else if (type
== float32x_type_node
)
2749 write_string ("DF32x");
2750 else if (type
== float64x_type_node
)
2751 write_string ("DF64x");
2752 else if (type
== float128x_type_node
)
2753 write_string ("DF128x");
2763 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2764 METHOD_TYPE. The return type is mangled before the parameter
2767 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2770 write_function_type (const tree type
)
2772 MANGLE_TRACE_TREE ("function-type", type
);
2774 /* For a pointer to member function, the function type may have
2775 cv-qualifiers, indicating the quals for the artificial 'this'
2777 if (TREE_CODE (type
) == METHOD_TYPE
)
2779 /* The first parameter must be a POINTER_TYPE pointing to the
2780 `this' parameter. */
2781 tree this_type
= class_of_this_parm (type
);
2782 write_CV_qualifiers_for_type (this_type
);
2785 write_exception_spec (TYPE_RAISES_EXCEPTIONS (type
));
2787 if (tx_safe_fn_type_p (type
))
2788 write_string ("Dx");
2791 /* We don't track whether or not a type is `extern "C"'. Note that
2792 you can have an `extern "C"' function that does not have
2793 `extern "C"' type, and vice versa:
2795 extern "C" typedef void function_t();
2796 function_t f; // f has C++ linkage, but its type is
2799 typedef void function_t();
2800 extern "C" function_t f; // Vice versa.
2803 write_bare_function_type (type
, /*include_return_type_p=*/1,
2805 if (FUNCTION_REF_QUALIFIED (type
))
2807 if (FUNCTION_RVALUE_QUALIFIED (type
))
2815 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2816 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2817 is mangled before the parameter types. If non-NULL, DECL is
2818 FUNCTION_DECL for the function whose type is being emitted. */
2821 write_bare_function_type (const tree type
, const int include_return_type_p
,
2824 MANGLE_TRACE_TREE ("bare-function-type", type
);
2826 /* Mangle the return type, if requested. */
2827 if (include_return_type_p
)
2828 write_type (TREE_TYPE (type
));
2830 /* Now mangle the types of the arguments. */
2832 write_method_parms (TYPE_ARG_TYPES (type
),
2833 TREE_CODE (type
) == METHOD_TYPE
,
2838 /* Write the mangled representation of a method parameter list of
2839 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2840 considered a non-static method, and the this parameter is omitted.
2841 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2842 parameters are being emitted. */
2845 write_method_parms (tree parm_types
, const int method_p
, const tree decl
)
2847 tree first_parm_type
;
2848 tree parm_decl
= decl
? DECL_ARGUMENTS (decl
) : NULL_TREE
;
2850 /* Assume this parameter type list is variable-length. If it ends
2851 with a void type, then it's not. */
2854 /* If this is a member function, skip the first arg, which is the
2856 "Member functions do not encode the type of their implicit this
2859 Similarly, there's no need to mangle artificial parameters, like
2860 the VTT parameters for constructors and destructors. */
2863 parm_types
= TREE_CHAIN (parm_types
);
2864 parm_decl
= parm_decl
? DECL_CHAIN (parm_decl
) : NULL_TREE
;
2866 while (parm_decl
&& DECL_ARTIFICIAL (parm_decl
))
2868 parm_types
= TREE_CHAIN (parm_types
);
2869 parm_decl
= DECL_CHAIN (parm_decl
);
2872 if (decl
&& ctor_omit_inherited_parms (decl
))
2873 /* Bring back parameters omitted from an inherited ctor. */
2874 parm_types
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl
));
2877 for (first_parm_type
= parm_types
;
2879 parm_types
= TREE_CHAIN (parm_types
))
2881 tree parm
= TREE_VALUE (parm_types
);
2882 if (parm
== void_type_node
)
2884 /* "Empty parameter lists, whether declared as () or
2885 conventionally as (void), are encoded with a void parameter
2887 if (parm_types
== first_parm_type
)
2889 /* If the parm list is terminated with a void type, it's
2892 /* A void type better be the last one. */
2893 gcc_assert (TREE_CHAIN (parm_types
) == NULL
);
2900 /* <builtin-type> ::= z # ellipsis */
2904 /* <class-enum-type> ::= <name> */
2907 write_class_enum_type (const tree type
)
2909 write_name (TYPE_NAME (type
), /*ignore_local_scope=*/0);
2912 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2915 <template-args> ::= I <template-arg>* E */
2918 write_template_args (tree args
)
2923 MANGLE_TRACE_TREE ("template-args", args
);
2928 length
= TREE_VEC_LENGTH (args
);
2930 if (args
&& length
&& TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
2932 /* We have nested template args. We want the innermost template
2934 args
= TREE_VEC_ELT (args
, length
- 1);
2935 length
= TREE_VEC_LENGTH (args
);
2937 for (i
= 0; i
< length
; ++i
)
2938 write_template_arg (TREE_VEC_ELT (args
, i
));
2945 <unqualified-name> <template-args>
2946 part of SCOPE_REF or COMPONENT_REF mangling. */
2949 write_member_name (tree member
)
2951 if (identifier_p (member
))
2953 if (IDENTIFIER_ANY_OP_P (member
))
2955 if (abi_version_at_least (11))
2956 write_string ("on");
2957 if (abi_warn_or_compat_version_crosses (11))
2958 G
.need_abi_warning
= 1;
2960 write_unqualified_id (member
);
2962 else if (DECL_P (member
))
2964 gcc_assert (!DECL_OVERLOADED_OPERATOR_P (member
));
2965 write_unqualified_name (member
);
2967 else if (TREE_CODE (member
) == TEMPLATE_ID_EXPR
)
2969 tree name
= TREE_OPERAND (member
, 0);
2970 name
= OVL_FIRST (name
);
2971 write_member_name (name
);
2972 write_template_args (TREE_OPERAND (member
, 1));
2975 write_expression (member
);
2978 /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for
2979 converting to BASE, or just the conversion of EXPR if BASE is null.
2981 "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base
2982 path Min(P) is defined as follows: let C_i be the last element for which the
2983 conversion to C_0 is unambiguous; if that element is C_n, the minimized path
2984 is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) ->
2987 We mangle the conversion to C_i if it's different from C_n. */
2990 write_base_ref (tree expr
, tree base
= NULL_TREE
)
2992 if (TREE_CODE (expr
) != COMPONENT_REF
)
2995 tree field
= TREE_OPERAND (expr
, 1);
2997 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (field
))
3000 tree object
= TREE_OPERAND (expr
, 0);
3002 tree binfo
= NULL_TREE
;
3005 tree cur
= TREE_TYPE (object
);
3006 binfo
= lookup_base (cur
, base
, ba_unique
, NULL
, tf_none
);
3009 /* We're at the end of the base conversion chain, so it can't be
3011 base
= TREE_TYPE (field
);
3013 if (binfo
== error_mark_node
)
3015 /* cur->base is ambiguous, so make the conversion to
3016 last explicit, expressed as a cast (last&)object. */
3017 tree last
= TREE_TYPE (expr
);
3018 write_string (OVL_OP_INFO (false, CAST_EXPR
)->mangled_name
);
3019 write_type (build_reference_type (last
));
3020 write_expression (object
);
3022 else if (write_base_ref (object
, base
))
3023 /* cur->base is unambiguous, but we had another base conversion
3024 underneath and wrote it out. */;
3026 /* No more base conversions, just write out the object. */
3027 write_expression (object
);
3032 /* The number of elements spanned by a RANGE_EXPR. */
3034 unsigned HOST_WIDE_INT
3035 range_expr_nelts (tree expr
)
3037 tree lo
= TREE_OPERAND (expr
, 0);
3038 tree hi
= TREE_OPERAND (expr
, 1);
3039 return tree_to_uhwi (hi
) - tree_to_uhwi (lo
) + 1;
3042 /* <expression> ::= <unary operator-name> <expression>
3043 ::= <binary operator-name> <expression> <expression>
3046 <expr-primary> ::= <template-param>
3047 ::= L <type> <value number> E # literal
3048 ::= L <mangled-name> E # external name
3049 ::= st <type> # sizeof
3050 ::= sr <type> <unqualified-name> # dependent name
3051 ::= sr <type> <unqualified-name> <template-args> */
3054 write_expression (tree expr
)
3056 enum tree_code code
= TREE_CODE (expr
);
3058 if (TREE_CODE (expr
) == TARGET_EXPR
)
3060 expr
= TARGET_EXPR_INITIAL (expr
);
3061 code
= TREE_CODE (expr
);
3064 /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer
3065 argument is converted (via qualification conversions) to another type. */
3066 while (CONVERT_EXPR_CODE_P (code
)
3067 || code
== IMPLICIT_CONV_EXPR
3068 || location_wrapper_p (expr
)
3069 /* Parentheses aren't mangled. */
3070 || code
== PAREN_EXPR
3071 || code
== NON_LVALUE_EXPR
3072 || (code
== VIEW_CONVERT_EXPR
3073 && TREE_CODE (TREE_OPERAND (expr
, 0)) == TEMPLATE_PARM_INDEX
))
3075 expr
= TREE_OPERAND (expr
, 0);
3076 code
= TREE_CODE (expr
);
3079 if (code
== BASELINK
3080 && (!type_unknown_p (expr
)
3081 || !BASELINK_QUALIFIED_P (expr
)))
3083 expr
= BASELINK_FUNCTIONS (expr
);
3084 code
= TREE_CODE (expr
);
3087 /* Handle pointers-to-members by making them look like expression
3089 if (code
== PTRMEM_CST
)
3091 expr
= build_nt (ADDR_EXPR
,
3092 build_qualified_name (/*type=*/NULL_TREE
,
3093 PTRMEM_CST_CLASS (expr
),
3094 PTRMEM_CST_MEMBER (expr
),
3095 /*template_p=*/false));
3096 code
= TREE_CODE (expr
);
3099 /* Handle template parameters. */
3100 if (code
== TEMPLATE_TYPE_PARM
3101 || code
== TEMPLATE_TEMPLATE_PARM
3102 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
3103 || code
== TEMPLATE_PARM_INDEX
)
3104 write_template_param (expr
);
3105 /* Handle literals. */
3106 else if (TREE_CODE_CLASS (code
) == tcc_constant
3107 || code
== CONST_DECL
)
3108 write_template_arg_literal (expr
);
3109 else if (code
== PARM_DECL
&& DECL_ARTIFICIAL (expr
))
3111 gcc_assert (id_equal (DECL_NAME (expr
), "this"));
3112 write_string ("fpT");
3114 else if (code
== PARM_DECL
)
3116 /* A function parameter used in a late-specified return type. */
3117 int index
= DECL_PARM_INDEX (expr
);
3118 int level
= DECL_PARM_LEVEL (expr
);
3119 int delta
= G
.parm_depth
- level
+ 1;
3120 gcc_assert (index
>= 1);
3124 if (abi_version_at_least (5))
3126 /* Let L be the number of function prototype scopes from the
3127 innermost one (in which the parameter reference occurs) up
3128 to (and including) the one containing the declaration of
3129 the referenced parameter. If the parameter declaration
3130 clause of the innermost function prototype scope has been
3131 completely seen, it is not counted (in that case -- which
3132 is perhaps the most common -- L can be zero). */
3134 write_unsigned_number (delta
- 1);
3136 if (abi_warn_or_compat_version_crosses (5))
3137 G
.need_abi_warning
= true;
3140 write_compact_number (index
- 1);
3142 else if (DECL_P (expr
))
3145 write_mangled_name (expr
, false);
3148 else if (TREE_CODE (expr
) == SIZEOF_EXPR
)
3150 tree op
= TREE_OPERAND (expr
, 0);
3152 if (PACK_EXPANSION_P (op
))
3154 if (abi_warn_or_compat_version_crosses (11))
3155 G
.need_abi_warning
= true;
3156 if (abi_version_at_least (11))
3158 /* sZ rather than szDp. */
3159 write_string ("sZ");
3160 write_expression (PACK_EXPANSION_PATTERN (op
));
3165 if (SIZEOF_EXPR_TYPE_P (expr
))
3167 write_string ("st");
3168 write_type (TREE_TYPE (op
));
3170 else if (ARGUMENT_PACK_P (op
))
3172 tree args
= ARGUMENT_PACK_ARGS (op
);
3173 int length
= TREE_VEC_LENGTH (args
);
3174 if (abi_warn_or_compat_version_crosses (10))
3175 G
.need_abi_warning
= true;
3176 if (abi_version_at_least (10))
3178 /* sP <template-arg>* E # sizeof...(T), size of a captured
3179 template parameter pack from an alias template */
3180 write_string ("sP");
3181 for (int i
= 0; i
< length
; ++i
)
3182 write_template_arg (TREE_VEC_ELT (args
, i
));
3187 /* In GCC 5 we represented this sizeof wrong, with the effect
3188 that we mangled it as the last element of the pack. */
3189 tree arg
= TREE_VEC_ELT (args
, length
-1);
3192 write_string ("st");
3197 write_string ("sz");
3198 write_expression (arg
);
3202 else if (TYPE_P (TREE_OPERAND (expr
, 0)))
3204 write_string ("st");
3205 write_type (TREE_OPERAND (expr
, 0));
3210 else if (TREE_CODE (expr
) == ALIGNOF_EXPR
)
3212 if (!ALIGNOF_EXPR_STD_P (expr
))
3214 if (abi_warn_or_compat_version_crosses (16))
3215 G
.need_abi_warning
= true;
3216 if (abi_version_at_least (16))
3218 /* We used to mangle __alignof__ like alignof. */
3219 write_string ("u11__alignof__");
3220 write_template_arg (TREE_OPERAND (expr
, 0));
3225 if (TYPE_P (TREE_OPERAND (expr
, 0)))
3227 write_string ("at");
3228 write_type (TREE_OPERAND (expr
, 0));
3233 else if (code
== SCOPE_REF
3234 || code
== BASELINK
)
3237 if (code
== SCOPE_REF
)
3239 scope
= TREE_OPERAND (expr
, 0);
3240 member
= TREE_OPERAND (expr
, 1);
3241 if (BASELINK_P (member
))
3242 member
= BASELINK_FUNCTIONS (member
);
3246 scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (expr
));
3247 member
= BASELINK_FUNCTIONS (expr
);
3250 /* If the MEMBER is a real declaration, then the qualifying
3251 scope was not dependent. Ideally, we would not have a
3252 SCOPE_REF in those cases, but sometimes we do. If the second
3253 argument is a DECL, then the name must not have been
3255 if (DECL_P (member
))
3256 write_expression (member
);
3259 gcc_assert (code
!= BASELINK
|| BASELINK_QUALIFIED_P (expr
));
3260 write_string ("sr");
3262 write_member_name (member
);
3265 else if (INDIRECT_REF_P (expr
)
3266 && TREE_TYPE (TREE_OPERAND (expr
, 0))
3267 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
3269 write_expression (TREE_OPERAND (expr
, 0));
3271 else if (identifier_p (expr
))
3273 /* An operator name appearing as a dependent name needs to be
3274 specially marked to disambiguate between a use of the operator
3275 name and a use of the operator in an expression. */
3276 if (IDENTIFIER_ANY_OP_P (expr
))
3277 write_string ("on");
3278 write_unqualified_id (expr
);
3280 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
3282 tree fn
= TREE_OPERAND (expr
, 0);
3284 if (IDENTIFIER_ANY_OP_P (fn
))
3285 write_string ("on");
3286 write_unqualified_id (fn
);
3287 write_template_args (TREE_OPERAND (expr
, 1));
3289 else if (TREE_CODE (expr
) == MODOP_EXPR
)
3291 enum tree_code subop
= TREE_CODE (TREE_OPERAND (expr
, 1));
3292 const char *name
= OVL_OP_INFO (true, subop
)->mangled_name
;
3294 write_string (name
);
3295 write_expression (TREE_OPERAND (expr
, 0));
3296 write_expression (TREE_OPERAND (expr
, 2));
3298 else if (code
== NEW_EXPR
|| code
== VEC_NEW_EXPR
)
3300 /* ::= [gs] nw <expression>* _ <type> E
3301 ::= [gs] nw <expression>* _ <type> <initializer>
3302 ::= [gs] na <expression>* _ <type> E
3303 ::= [gs] na <expression>* _ <type> <initializer>
3304 <initializer> ::= pi <expression>* E */
3305 tree placement
= TREE_OPERAND (expr
, 0);
3306 tree type
= TREE_OPERAND (expr
, 1);
3307 tree nelts
= TREE_OPERAND (expr
, 2);
3308 tree init
= TREE_OPERAND (expr
, 3);
3311 gcc_assert (code
== NEW_EXPR
);
3312 if (TREE_OPERAND (expr
, 2))
3313 code
= VEC_NEW_EXPR
;
3315 if (NEW_EXPR_USE_GLOBAL (expr
))
3316 write_string ("gs");
3318 write_string (OVL_OP_INFO (false, code
)->mangled_name
);
3320 for (t
= placement
; t
; t
= TREE_CHAIN (t
))
3321 write_expression (TREE_VALUE (t
));
3328 ++processing_template_decl
;
3329 domain
= compute_array_index_type (NULL_TREE
, nelts
,
3330 tf_warning_or_error
);
3331 type
= build_cplus_array_type (type
, domain
);
3332 --processing_template_decl
;
3336 if (init
&& TREE_CODE (init
) == TREE_LIST
3337 && DIRECT_LIST_INIT_P (TREE_VALUE (init
)))
3338 write_expression (TREE_VALUE (init
));
3342 write_string ("pi");
3343 if (init
&& init
!= void_node
)
3344 for (t
= init
; t
; t
= TREE_CHAIN (t
))
3345 write_expression (TREE_VALUE (t
));
3349 else if (code
== DELETE_EXPR
|| code
== VEC_DELETE_EXPR
)
3351 gcc_assert (code
== DELETE_EXPR
);
3352 if (DELETE_EXPR_USE_VEC (expr
))
3353 code
= VEC_DELETE_EXPR
;
3355 if (DELETE_EXPR_USE_GLOBAL (expr
))
3356 write_string ("gs");
3358 write_string (OVL_OP_INFO (false, code
)->mangled_name
);
3360 write_expression (TREE_OPERAND (expr
, 0));
3362 else if (code
== THROW_EXPR
)
3364 tree op
= TREE_OPERAND (expr
, 0);
3367 write_string ("tw");
3368 write_expression (op
);
3371 write_string ("tr");
3373 else if (code
== CONSTRUCTOR
)
3375 bool braced_init
= BRACE_ENCLOSED_INITIALIZER_P (expr
);
3376 tree etype
= TREE_TYPE (expr
);
3379 write_string ("il");
3382 write_string ("tl");
3386 /* If this is an undigested initializer, mangle it as written.
3387 COMPOUND_LITERAL_P doesn't actually distinguish between digested and
3388 undigested braced casts, but it should work to use it to distinguish
3389 between braced casts in a template signature (undigested) and template
3390 parm object values (digested), and all CONSTRUCTORS that get here
3391 should be one of those two cases. */
3392 bool undigested
= braced_init
|| COMPOUND_LITERAL_P (expr
);
3393 if (undigested
|| !zero_init_expr_p (expr
))
3395 /* Convert braced initializer lists to STRING_CSTs so that
3396 A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
3397 still using the latter mangling for strings that
3398 originated as braced initializer lists. */
3399 expr
= braced_lists_to_strings (etype
, expr
);
3401 if (TREE_CODE (expr
) == CONSTRUCTOR
)
3403 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (expr
);
3404 unsigned last_nonzero
= UINT_MAX
;
3405 constructor_elt
*ce
;
3408 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
3409 if ((TREE_CODE (etype
) == UNION_TYPE
3410 && ce
->index
!= first_field (etype
))
3411 || !zero_init_expr_p (ce
->value
))
3414 if (undigested
|| last_nonzero
!= UINT_MAX
)
3415 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
3417 if (i
> last_nonzero
)
3419 if (!undigested
&& TREE_CODE (etype
) == UNION_TYPE
)
3421 /* Express the active member as a designator. */
3422 write_string ("di");
3423 write_unqualified_name (ce
->index
);
3426 if (ce
->index
&& TREE_CODE (ce
->index
) == RANGE_EXPR
)
3427 reps
= range_expr_nelts (ce
->index
);
3428 for (unsigned j
= 0; j
< reps
; ++j
)
3429 write_expression (ce
->value
);
3434 gcc_assert (TREE_CODE (expr
) == STRING_CST
);
3435 write_expression (expr
);
3440 else if (code
== LAMBDA_EXPR
)
3442 /* [temp.over.link] Two lambda-expressions are never considered
3445 So just use the closure type mangling. */
3446 write_string ("tl");
3447 write_type (LAMBDA_EXPR_CLOSURE (expr
));
3450 else if (dependent_name (expr
))
3452 tree name
= dependent_name (expr
);
3453 if (IDENTIFIER_ANY_OP_P (name
))
3455 if (abi_version_at_least (16))
3456 write_string ("on");
3457 if (abi_warn_or_compat_version_crosses (16))
3458 G
.need_abi_warning
= 1;
3460 write_unqualified_id (name
);
3468 /* When we bind a variable or function to a non-type template
3469 argument with reference type, we create an ADDR_EXPR to show
3470 the fact that the entity's address has been taken. But, we
3471 don't actually want to output a mangling code for the `&'. */
3472 if (TREE_CODE (expr
) == ADDR_EXPR
3474 && TYPE_REF_P (TREE_TYPE (expr
)))
3476 expr
= TREE_OPERAND (expr
, 0);
3479 write_expression (expr
);
3483 code
= TREE_CODE (expr
);
3486 if (code
== COMPONENT_REF
)
3488 tree ob
= TREE_OPERAND (expr
, 0);
3490 if (TREE_CODE (ob
) == ARROW_EXPR
)
3492 write_string (OVL_OP_INFO (false, code
)->mangled_name
);
3493 ob
= TREE_OPERAND (ob
, 0);
3494 write_expression (ob
);
3496 else if (write_base_ref (expr
))
3498 else if (!is_dummy_object (ob
))
3500 write_string ("dt");
3501 write_expression (ob
);
3503 /* else, for a non-static data member with no associated object (in
3504 unevaluated context), use the unresolved-name mangling. */
3506 write_member_name (TREE_OPERAND (expr
, 1));
3510 /* If it wasn't any of those, recursively expand the expression. */
3511 name
= OVL_OP_INFO (false, code
)->mangled_name
;
3513 /* We used to mangle const_cast and static_cast like a C cast. */
3514 if (code
== CONST_CAST_EXPR
3515 || code
== STATIC_CAST_EXPR
)
3517 if (abi_warn_or_compat_version_crosses (6))
3518 G
.need_abi_warning
= 1;
3519 if (!abi_version_at_least (6))
3520 name
= OVL_OP_INFO (false, CAST_EXPR
)->mangled_name
;
3528 error ("use of built-in trait %qE in function signature; "
3529 "use library traits instead", expr
);
3533 sorry ("mangling %C", code
);
3539 write_string (name
);
3545 tree fn
= CALL_EXPR_FN (expr
);
3547 if (TREE_CODE (fn
) == ADDR_EXPR
)
3548 fn
= TREE_OPERAND (fn
, 0);
3550 /* Mangle a dependent name as the name, not whatever happens to
3551 be the first function in the overload set. */
3553 && type_dependent_expression_p_push (expr
))
3556 write_expression (fn
);
3559 for (i
= 0; i
< call_expr_nargs (expr
); ++i
)
3560 write_expression (CALL_EXPR_ARG (expr
, i
));
3565 write_type (TREE_TYPE (expr
));
3566 if (list_length (TREE_OPERAND (expr
, 0)) == 1)
3567 write_expression (TREE_VALUE (TREE_OPERAND (expr
, 0)));
3570 tree args
= TREE_OPERAND (expr
, 0);
3572 for (; args
; args
= TREE_CHAIN (args
))
3573 write_expression (TREE_VALUE (args
));
3578 case DYNAMIC_CAST_EXPR
:
3579 case REINTERPRET_CAST_EXPR
:
3580 case STATIC_CAST_EXPR
:
3581 case CONST_CAST_EXPR
:
3582 write_type (TREE_TYPE (expr
));
3583 write_expression (TREE_OPERAND (expr
, 0));
3586 case PREINCREMENT_EXPR
:
3587 case PREDECREMENT_EXPR
:
3588 if (abi_version_at_least (6))
3590 if (abi_warn_or_compat_version_crosses (6))
3591 G
.need_abi_warning
= 1;
3595 /* In the middle-end, some expressions have more operands than
3596 they do in templates (and mangling). */
3597 len
= cp_tree_operand_length (expr
);
3599 for (i
= 0; i
< len
; ++i
)
3601 tree operand
= TREE_OPERAND (expr
, i
);
3602 /* As a GNU extension, the middle operand of a
3603 conditional may be omitted. Since expression
3604 manglings are supposed to represent the input token
3605 stream, there's no good way to mangle such an
3606 expression without extending the C++ ABI. */
3607 if (code
== COND_EXPR
&& i
== 1 && !operand
)
3609 error ("omitted middle operand to %<?:%> operand "
3610 "cannot be mangled");
3613 else if (FOLD_EXPR_P (expr
))
3615 /* The first 'operand' of a fold-expression is the operator
3616 that it folds over. */
3619 int fcode
= TREE_INT_CST_LOW (operand
);
3620 write_string (OVL_OP_INFO (false, fcode
)->mangled_name
);
3623 else if (code
== BINARY_LEFT_FOLD_EXPR
)
3625 /* The order of operands of the binary left and right
3626 folds is the same, but we want to mangle them in
3627 lexical order, i.e. non-pack first. */
3629 operand
= FOLD_EXPR_INIT (expr
);
3631 operand
= FOLD_EXPR_PACK (expr
);
3633 if (PACK_EXPANSION_P (operand
))
3634 operand
= PACK_EXPANSION_PATTERN (operand
);
3636 write_expression (operand
);
3642 /* Literal subcase of non-terminal <template-arg>.
3644 "Literal arguments, e.g. "A<42L>", are encoded with their type
3645 and value. Negative integer values are preceded with "n"; for
3646 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3647 encoded as 0, true as 1." */
3650 write_template_arg_literal (const tree value
)
3652 if (TREE_CODE (value
) == STRING_CST
)
3653 /* Temporarily mangle strings as braced initializer lists. */
3654 write_string ("tl");
3658 tree valtype
= TREE_TYPE (value
);
3659 write_type (valtype
);
3661 /* Write a null member pointer value as (type)0, regardless of its
3662 real representation. */
3663 if (null_member_pointer_value_p (value
))
3664 write_integer_cst (integer_zero_node
);
3666 switch (TREE_CODE (value
))
3669 write_integer_cst (DECL_INITIAL (value
));
3673 gcc_assert (!same_type_p (TREE_TYPE (value
), boolean_type_node
)
3674 || integer_zerop (value
) || integer_onep (value
));
3675 if (!(abi_version_at_least (14)
3676 && NULLPTR_TYPE_P (TREE_TYPE (value
))))
3677 write_integer_cst (value
);
3681 write_real_cst (value
);
3685 if (TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
3686 && TREE_CODE (TREE_IMAGPART (value
)) == INTEGER_CST
)
3688 write_integer_cst (TREE_REALPART (value
));
3690 write_integer_cst (TREE_IMAGPART (value
));
3692 else if (TREE_CODE (TREE_REALPART (value
)) == REAL_CST
3693 && TREE_CODE (TREE_IMAGPART (value
)) == REAL_CST
)
3695 write_real_cst (TREE_REALPART (value
));
3697 write_real_cst (TREE_IMAGPART (value
));
3705 /* Mangle strings the same as braced initializer lists. */
3706 unsigned n
= TREE_STRING_LENGTH (value
);
3707 const char *str
= TREE_STRING_POINTER (value
);
3709 /* Count the number of trailing nuls and subtract them from
3710 STRSIZE because they don't need to be mangled. */
3711 for (const char *p
= str
+ n
- 1; ; --p
)
3715 n
-= str
+ n
- !!*p
- p
;
3719 tree eltype
= TREE_TYPE (valtype
);
3720 for (const char *p
= str
; n
--; ++p
)
3723 write_type (eltype
);
3724 write_unsigned_number (*(const unsigned char*)p
);
3737 /* Non-terminal <template-arg>.
3739 <template-arg> ::= <type> # type
3740 ::= L <type> </value/ number> E # literal
3741 ::= LZ <name> E # external name
3742 ::= X <expression> E # expression */
3745 write_template_arg (tree node
)
3747 enum tree_code code
= TREE_CODE (node
);
3749 MANGLE_TRACE_TREE ("template-arg", node
);
3751 /* A template template parameter's argument list contains TREE_LIST
3752 nodes of which the value field is the actual argument. */
3753 if (code
== TREE_LIST
)
3755 node
= TREE_VALUE (node
);
3756 /* If it's a decl, deal with its type instead. */
3759 node
= TREE_TYPE (node
);
3760 code
= TREE_CODE (node
);
3764 if (TREE_CODE (node
) == VAR_DECL
&& DECL_NTTP_OBJECT_P (node
))
3765 /* We want to mangle the argument, not the var we stored it in. */
3766 node
= tparm_object_argument (node
);
3768 /* Strip a conversion added by convert_nontype_argument. */
3769 if (TREE_CODE (node
) == IMPLICIT_CONV_EXPR
)
3770 node
= TREE_OPERAND (node
, 0);
3771 if (REFERENCE_REF_P (node
))
3772 node
= TREE_OPERAND (node
, 0);
3773 if (TREE_CODE (node
) == NOP_EXPR
3774 && TYPE_REF_P (TREE_TYPE (node
)))
3776 /* Template parameters can be of reference type. To maintain
3777 internal consistency, such arguments use a conversion from
3778 address of object to reference type. */
3779 gcc_assert (TREE_CODE (TREE_OPERAND (node
, 0)) == ADDR_EXPR
);
3780 node
= TREE_OPERAND (TREE_OPERAND (node
, 0), 0);
3783 if (TREE_CODE (node
) == BASELINK
3784 && !type_unknown_p (node
))
3786 if (abi_version_at_least (6))
3787 node
= BASELINK_FUNCTIONS (node
);
3788 if (abi_warn_or_compat_version_crosses (6))
3789 /* We wrongly wrapped a class-scope function in X/E. */
3790 G
.need_abi_warning
= 1;
3793 if (ARGUMENT_PACK_P (node
))
3795 /* Expand the template argument pack. */
3796 tree args
= ARGUMENT_PACK_ARGS (node
);
3797 int i
, length
= TREE_VEC_LENGTH (args
);
3798 if (abi_version_at_least (6))
3802 if (abi_warn_or_compat_version_crosses (6))
3803 G
.need_abi_warning
= 1;
3804 for (i
= 0; i
< length
; ++i
)
3805 write_template_arg (TREE_VEC_ELT (args
, i
));
3808 else if (TYPE_P (node
))
3810 else if (code
== TEMPLATE_DECL
)
3811 /* A template appearing as a template arg is a template template arg. */
3812 write_template_template_arg (node
);
3813 else if ((TREE_CODE_CLASS (code
) == tcc_constant
&& code
!= PTRMEM_CST
)
3814 || code
== CONST_DECL
3815 || null_member_pointer_value_p (node
))
3816 write_template_arg_literal (node
);
3817 else if (DECL_P (node
))
3820 /* Until ABI version 3, the underscore before the mangled name
3821 was incorrectly omitted. */
3822 if (!abi_version_at_least (3))
3825 write_string ("_Z");
3826 if (abi_warn_or_compat_version_crosses (3))
3827 G
.need_abi_warning
= 1;
3828 write_encoding (node
);
3833 /* Template arguments may be expressions. */
3835 write_expression (node
);
3840 /* <template-template-arg>
3842 ::= <substitution> */
3845 write_template_template_arg (const tree decl
)
3847 MANGLE_TRACE_TREE ("template-template-arg", decl
);
3849 if (find_substitution (decl
))
3851 write_name (decl
, /*ignore_local_scope=*/0);
3852 add_substitution (decl
);
3856 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3858 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3859 ::= A <expression> _ </element/ type>
3861 "Array types encode the dimension (number of elements) and the
3862 element type. For variable length arrays, the dimension (but not
3863 the '_' separator) is omitted."
3864 Note that for flexible array members, like for other arrays of
3865 unspecified size, the dimension is also omitted. */
3868 write_array_type (const tree type
)
3871 if (TYPE_DOMAIN (type
))
3875 index_type
= TYPE_DOMAIN (type
);
3876 /* The INDEX_TYPE gives the upper and lower bounds of the array.
3877 It's null for flexible array members which have no upper bound
3878 (this is a change from GCC 5 and prior where such members were
3879 incorrectly mangled as zero-length arrays). */
3880 if (tree max
= TYPE_MAX_VALUE (index_type
))
3882 if (TREE_CODE (max
) == INTEGER_CST
)
3884 /* The ABI specifies that we should mangle the number of
3885 elements in the array, not the largest allowed index. */
3886 offset_int wmax
= wi::to_offset (max
) + 1;
3887 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3888 number of elements as zero. */
3889 wmax
= wi::zext (wmax
, TYPE_PRECISION (TREE_TYPE (max
)));
3890 gcc_assert (wi::fits_uhwi_p (wmax
));
3891 write_unsigned_number (wmax
.to_uhwi ());
3895 max
= TREE_OPERAND (max
, 0);
3896 write_expression (max
);
3901 write_type (TREE_TYPE (type
));
3904 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3905 variables. TYPE is a pointer-to-member POINTER_TYPE.
3907 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3910 write_pointer_to_member_type (const tree type
)
3913 write_type (TYPE_PTRMEM_CLASS_TYPE (type
));
3914 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
3917 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3918 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3919 TEMPLATE_PARM_INDEX.
3921 <template-param> ::= T </parameter/ number> _ */
3924 write_template_param (const tree parm
)
3928 MANGLE_TRACE_TREE ("template-parm", parm
);
3930 switch (TREE_CODE (parm
))
3932 case TEMPLATE_TYPE_PARM
:
3933 case TEMPLATE_TEMPLATE_PARM
:
3934 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3935 parm_index
= TEMPLATE_TYPE_IDX (parm
);
3938 case TEMPLATE_PARM_INDEX
:
3939 parm_index
= TEMPLATE_PARM_IDX (parm
);
3947 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3948 earliest template param denoted by `_'. */
3949 write_compact_number (parm_index
);
3952 /* <template-template-param>
3953 ::= <template-param>
3954 ::= <substitution> */
3957 write_template_template_param (const tree parm
)
3959 tree templ
= NULL_TREE
;
3961 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3962 template template parameter. The substitution candidate here is
3963 only the template. */
3964 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
3967 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
));
3968 if (find_substitution (templ
))
3972 /* <template-param> encodes only the template parameter position,
3973 not its template arguments, which is fine here. */
3974 write_template_param (parm
);
3976 add_substitution (templ
);
3979 /* Non-terminal <substitution>.
3981 <substitution> ::= S <seq-id> _
3985 write_substitution (const int seq_id
)
3987 MANGLE_TRACE ("substitution", "");
3991 write_number (seq_id
- 1, /*unsigned=*/1, 36);
3995 /* Start mangling ENTITY. */
3998 start_mangling (const tree entity
)
4001 G
.need_abi_warning
= false;
4002 G
.need_cxx17_warning
= false;
4004 obstack_free (&name_obstack
, name_base
);
4005 mangle_obstack
= &name_obstack
;
4006 name_base
= obstack_alloc (&name_obstack
, 0);
4009 /* Done with mangling. Release the data. */
4012 finish_mangling_internal (void)
4014 /* Clear all the substitutions. */
4015 vec_safe_truncate (G
.substitutions
, 0);
4018 mangle_module_fini ();
4020 /* Null-terminate the string. */
4025 /* Like finish_mangling_internal, but return the mangled string. */
4027 static inline const char *
4028 finish_mangling (void)
4030 finish_mangling_internal ();
4031 return (const char *) obstack_finish (mangle_obstack
);
4034 /* Like finish_mangling_internal, but return an identifier. */
4037 finish_mangling_get_identifier (void)
4039 finish_mangling_internal ();
4040 /* Don't obstack_finish here, and the next start_mangling will
4041 remove the identifier. */
4042 return get_identifier ((const char *) obstack_base (mangle_obstack
));
4045 /* Initialize data structures for mangling. */
4050 gcc_obstack_init (&name_obstack
);
4051 name_base
= obstack_alloc (&name_obstack
, 0);
4052 vec_alloc (G
.substitutions
, 0);
4054 /* Cache these identifiers for quick comparison when checking for
4055 standard substitutions. */
4056 subst_identifiers
[SUBID_ALLOCATOR
] = get_identifier ("allocator");
4057 subst_identifiers
[SUBID_BASIC_STRING
] = get_identifier ("basic_string");
4058 subst_identifiers
[SUBID_CHAR_TRAITS
] = get_identifier ("char_traits");
4059 subst_identifiers
[SUBID_BASIC_ISTREAM
] = get_identifier ("basic_istream");
4060 subst_identifiers
[SUBID_BASIC_OSTREAM
] = get_identifier ("basic_ostream");
4061 subst_identifiers
[SUBID_BASIC_IOSTREAM
] = get_identifier ("basic_iostream");
4064 /* Generate a mangling for MODULE's global initializer fn. */
4067 mangle_module_global_init (int module
)
4069 start_mangling (NULL_TREE
);
4071 write_string ("_ZGI");
4072 write_module (module
, true);
4074 return finish_mangling_get_identifier ();
4077 /* Generate the mangled name of DECL. */
4080 mangle_decl_string (const tree decl
)
4083 tree saved_fn
= NULL_TREE
;
4084 bool template_p
= false;
4086 /* We shouldn't be trying to mangle an uninstantiated template. */
4087 gcc_assert (!type_dependent_expression_p (decl
));
4089 if (DECL_LANG_SPECIFIC (decl
) && DECL_USE_TEMPLATE (decl
))
4091 struct tinst_level
*tl
= current_instantiation ();
4092 if ((!tl
|| tl
->maybe_get_node () != decl
)
4093 && push_tinst_level (decl
))
4096 saved_fn
= current_function_decl
;
4097 current_function_decl
= NULL_TREE
;
4100 iloc_sentinel
ils (DECL_SOURCE_LOCATION (decl
));
4102 start_mangling (decl
);
4104 if (TREE_CODE (decl
) == TYPE_DECL
)
4105 write_type (TREE_TYPE (decl
));
4107 write_mangled_name (decl
, true);
4109 result
= finish_mangling_get_identifier ();
4111 fprintf (stderr
, "mangle_decl_string = '%s'\n\n",
4112 IDENTIFIER_POINTER (result
));
4117 current_function_decl
= saved_fn
;
4123 /* Return an identifier for the external mangled name of DECL. */
4126 get_mangled_id (tree decl
)
4128 tree id
= mangle_decl_string (decl
);
4129 return targetm
.mangle_decl_assembler_name (decl
, id
);
4132 /* Create an identifier for the external mangled name of DECL. */
4135 mangle_decl (const tree decl
)
4140 /* Don't bother mangling uninstantiated templates. */
4141 ++processing_template_decl
;
4142 if (TREE_CODE (decl
) == TYPE_DECL
)
4143 dep
= dependent_type_p (TREE_TYPE (decl
));
4145 dep
= (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
4146 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl
)));
4147 --processing_template_decl
;
4151 /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
4152 It is not needed to assign names to anonymous namespace, but we use the
4153 "<anon>" marker to be able to tell if type is C++ ODR type or type
4154 produced by other language. */
4155 if (TREE_CODE (decl
) == TYPE_DECL
4156 && TYPE_STUB_DECL (TREE_TYPE (decl
))
4157 && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl
))))
4158 id
= get_identifier ("<anon>");
4161 gcc_assert (TREE_CODE (decl
) != TYPE_DECL
4162 || !no_linkage_check (TREE_TYPE (decl
), true));
4163 if (abi_version_at_least (10))
4164 if (tree fn
= decl_function_context (decl
))
4165 maybe_check_abi_tags (fn
, decl
);
4166 id
= get_mangled_id (decl
);
4168 SET_DECL_ASSEMBLER_NAME (decl
, id
);
4170 if (G
.need_cxx17_warning
4171 && (TREE_PUBLIC (decl
) || DECL_REALLY_EXTERN (decl
)))
4172 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wnoexcept_type
,
4173 "mangled name for %qD will change in C++17 because the "
4174 "exception specification is part of a function type",
4177 if (id
!= DECL_NAME (decl
)
4178 /* Don't do this for a fake symbol we aren't going to emit anyway. */
4179 && TREE_CODE (decl
) != TYPE_DECL
4180 && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl
))
4182 int save_ver
= flag_abi_version
;
4183 tree id2
= NULL_TREE
;
4185 if (!DECL_REALLY_EXTERN (decl
))
4187 record_mangling (decl
, G
.need_abi_warning
);
4189 if (!G
.need_abi_warning
)
4192 flag_abi_version
= flag_abi_compat_version
;
4193 id2
= mangle_decl_string (decl
);
4194 id2
= targetm
.mangle_decl_assembler_name (decl
, id2
);
4195 flag_abi_version
= save_ver
;
4198 note_mangling_alias (decl
, id2
);
4203 const char fabi_version
[] = "-fabi-version";
4205 if (flag_abi_compat_version
!= warn_abi_version
4206 || id2
== NULL_TREE
)
4208 flag_abi_version
= warn_abi_version
;
4209 id2
= mangle_decl_string (decl
);
4210 id2
= targetm
.mangle_decl_assembler_name (decl
, id2
);
4212 flag_abi_version
= save_ver
;
4216 else if (warn_abi_version
!= 0
4217 && abi_version_at_least (warn_abi_version
))
4218 warning_at (DECL_SOURCE_LOCATION (G
.entity
), OPT_Wabi
,
4219 "the mangled name of %qD changed between "
4220 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4221 G
.entity
, fabi_version
, warn_abi_version
, id2
,
4222 fabi_version
, save_ver
, id
);
4224 warning_at (DECL_SOURCE_LOCATION (G
.entity
), OPT_Wabi
,
4225 "the mangled name of %qD changes between "
4226 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
4227 G
.entity
, fabi_version
, save_ver
, id
,
4228 fabi_version
, warn_abi_version
, id2
);
4231 flag_abi_version
= save_ver
;
4235 /* Generate the mangled representation of TYPE. */
4238 mangle_type_string (const tree type
)
4242 start_mangling (type
);
4244 result
= finish_mangling ();
4246 fprintf (stderr
, "mangle_type_string = '%s'\n\n", result
);
4250 /* Create an identifier for the mangled name of a special component
4251 for belonging to TYPE. CODE is the ABI-specified code for this
4255 mangle_special_for_type (const tree type
, const char *code
)
4259 /* We don't have an actual decl here for the special component, so
4260 we can't just process the <encoded-name>. Instead, fake it. */
4261 start_mangling (type
);
4263 /* Start the mangling. */
4264 write_string ("_Z");
4265 write_string (code
);
4269 result
= finish_mangling_get_identifier ();
4272 fprintf (stderr
, "mangle_special_for_type = %s\n\n",
4273 IDENTIFIER_POINTER (result
));
4278 /* Create an identifier for the mangled representation of the typeinfo
4279 structure for TYPE. */
4282 mangle_typeinfo_for_type (const tree type
)
4284 return mangle_special_for_type (type
, "TI");
4287 /* Create an identifier for the mangled name of the NTBS containing
4288 the mangled name of TYPE. */
4291 mangle_typeinfo_string_for_type (const tree type
)
4293 return mangle_special_for_type (type
, "TS");
4296 /* Create an identifier for the mangled name of the vtable for TYPE. */
4299 mangle_vtbl_for_type (const tree type
)
4301 return mangle_special_for_type (type
, "TV");
4304 /* Returns an identifier for the mangled name of the VTT for TYPE. */
4307 mangle_vtt_for_type (const tree type
)
4309 return mangle_special_for_type (type
, "TT");
4312 /* Returns an identifier for the mangled name of the decomposition
4313 artificial variable DECL. DECLS is the vector of the VAR_DECLs
4314 for the identifier-list. */
4317 mangle_decomp (const tree decl
, vec
<tree
> &decls
)
4319 gcc_assert (!type_dependent_expression_p (decl
));
4321 location_t saved_loc
= input_location
;
4322 input_location
= DECL_SOURCE_LOCATION (decl
);
4324 start_mangling (decl
);
4325 write_string ("_Z");
4327 tree context
= decl_mangling_context (decl
);
4328 gcc_assert (context
!= NULL_TREE
);
4330 bool nested
= false;
4331 if (DECL_NAMESPACE_STD_P (context
))
4332 write_string ("St");
4333 else if (context
!= global_namespace
)
4337 write_prefix (decl_mangling_context (decl
));
4340 write_string ("DC");
4343 FOR_EACH_VEC_ELT (decls
, i
, d
)
4344 write_unqualified_name (d
);
4350 tree id
= finish_mangling_get_identifier ();
4352 fprintf (stderr
, "mangle_decomp = '%s'\n\n",
4353 IDENTIFIER_POINTER (id
));
4355 input_location
= saved_loc
;
4359 /* Return an identifier for a construction vtable group. TYPE is
4360 the most derived class in the hierarchy; BINFO is the base
4361 subobject for which this construction vtable group will be used.
4363 This mangling isn't part of the ABI specification; in the ABI
4364 specification, the vtable group is dumped in the same COMDAT as the
4365 main vtable, and is referenced only from that vtable, so it doesn't
4366 need an external name. For binary formats without COMDAT sections,
4367 though, we need external names for the vtable groups.
4369 We use the production
4371 <special-name> ::= CT <type> <offset number> _ <base type> */
4374 mangle_ctor_vtbl_for_type (const tree type
, const tree binfo
)
4378 start_mangling (type
);
4380 write_string ("_Z");
4381 write_string ("TC");
4383 write_integer_cst (BINFO_OFFSET (binfo
));
4385 write_type (BINFO_TYPE (binfo
));
4387 result
= finish_mangling_get_identifier ();
4389 fprintf (stderr
, "mangle_ctor_vtbl_for_type = %s\n\n",
4390 IDENTIFIER_POINTER (result
));
4394 /* Mangle a this pointer or result pointer adjustment.
4396 <call-offset> ::= h <fixed offset number> _
4397 ::= v <fixed offset number> _ <virtual offset number> _ */
4400 mangle_call_offset (const tree fixed_offset
, const tree virtual_offset
)
4402 write_char (virtual_offset
? 'v' : 'h');
4404 /* For either flavor, write the fixed offset. */
4405 write_integer_cst (fixed_offset
);
4408 /* For a virtual thunk, add the virtual offset. */
4411 write_integer_cst (virtual_offset
);
4416 /* Return an identifier for the mangled name of a this-adjusting or
4417 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
4418 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
4419 is a virtual thunk, and it is the vtbl offset in
4420 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
4421 zero for a covariant thunk. Note, that FN_DECL might be a covariant
4422 thunk itself. A covariant thunk name always includes the adjustment
4423 for the this pointer, even if there is none.
4425 <special-name> ::= T <call-offset> <base encoding>
4426 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
4430 mangle_thunk (tree fn_decl
, const int this_adjusting
, tree fixed_offset
,
4431 tree virtual_offset
, tree thunk
)
4435 if (abi_version_at_least (11))
4436 maybe_check_abi_tags (fn_decl
, thunk
, 11);
4438 start_mangling (fn_decl
);
4440 write_string ("_Z");
4443 if (!this_adjusting
)
4445 /* Covariant thunk with no this adjustment */
4447 mangle_call_offset (integer_zero_node
, NULL_TREE
);
4448 mangle_call_offset (fixed_offset
, virtual_offset
);
4450 else if (!DECL_THUNK_P (fn_decl
))
4451 /* Plain this adjusting thunk. */
4452 mangle_call_offset (fixed_offset
, virtual_offset
);
4455 /* This adjusting thunk to covariant thunk. */
4457 mangle_call_offset (fixed_offset
, virtual_offset
);
4458 fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (fn_decl
));
4459 virtual_offset
= THUNK_VIRTUAL_OFFSET (fn_decl
);
4461 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
4462 mangle_call_offset (fixed_offset
, virtual_offset
);
4463 fn_decl
= THUNK_TARGET (fn_decl
);
4467 write_encoding (fn_decl
);
4469 result
= finish_mangling_get_identifier ();
4471 fprintf (stderr
, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result
));
4475 /* Handle ABI backwards compatibility for past bugs where we didn't call
4476 check_abi_tags in places where it's needed: call check_abi_tags and warn if
4477 it makes a difference. If FOR_DECL is non-null, it's the declaration
4478 that we're actually trying to mangle; if it's null, we're mangling the
4479 guard variable for T. */
4482 maybe_check_abi_tags (tree t
, tree for_decl
, int ver
)
4484 if (DECL_ASSEMBLER_NAME_SET_P (t
))
4487 tree oldtags
= get_abi_tags (t
);
4491 tree newtags
= get_abi_tags (t
);
4492 if (newtags
&& newtags
!= oldtags
4493 && abi_version_crosses (ver
))
4495 if (for_decl
&& DECL_THUNK_P (for_decl
))
4496 warning_at (DECL_SOURCE_LOCATION (t
), OPT_Wabi
,
4497 "the mangled name of a thunk for %qD changes between "
4498 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4499 t
, flag_abi_version
, warn_abi_version
);
4501 warning_at (DECL_SOURCE_LOCATION (for_decl
), OPT_Wabi
,
4502 "the mangled name of %qD changes between "
4503 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
4504 for_decl
, flag_abi_version
, warn_abi_version
);
4506 warning_at (DECL_SOURCE_LOCATION (t
), OPT_Wabi
,
4507 "the mangled name of the initialization guard variable "
4508 "for %qD changes between %<-fabi-version=%d%> and "
4509 "%<-fabi-version=%d%>",
4510 t
, flag_abi_version
, warn_abi_version
);
4514 /* Write out the appropriate string for this variable when generating
4515 another mangled name based on this one. */
4518 write_guarded_var_name (const tree variable
)
4520 if (DECL_NAME (variable
)
4521 && startswith (IDENTIFIER_POINTER (DECL_NAME (variable
)), "_ZGR"))
4522 /* The name of a guard variable for a reference temporary should refer
4523 to the reference, not the temporary. */
4524 write_string (IDENTIFIER_POINTER (DECL_NAME (variable
)) + 4);
4526 write_name (variable
, /*ignore_local_scope=*/0);
4529 /* Return an identifier for the name of an initialization guard
4530 variable for indicated VARIABLE. */
4533 mangle_guard_variable (const tree variable
)
4535 if (abi_version_at_least (10))
4536 maybe_check_abi_tags (variable
);
4537 start_mangling (variable
);
4538 write_string ("_ZGV");
4539 write_guarded_var_name (variable
);
4540 return finish_mangling_get_identifier ();
4543 /* Return an identifier for the name of a thread_local initialization
4544 function for VARIABLE. */
4547 mangle_tls_init_fn (const tree variable
)
4549 check_abi_tags (variable
);
4550 start_mangling (variable
);
4551 write_string ("_ZTH");
4552 write_guarded_var_name (variable
);
4553 return finish_mangling_get_identifier ();
4556 /* Return an identifier for the name of a thread_local wrapper
4557 function for VARIABLE. */
4559 #define TLS_WRAPPER_PREFIX "_ZTW"
4562 mangle_tls_wrapper_fn (const tree variable
)
4564 check_abi_tags (variable
);
4565 start_mangling (variable
);
4566 write_string (TLS_WRAPPER_PREFIX
);
4567 write_guarded_var_name (variable
);
4568 return finish_mangling_get_identifier ();
4571 /* Return true iff FN is a thread_local wrapper function. */
4574 decl_tls_wrapper_p (const tree fn
)
4576 if (TREE_CODE (fn
) != FUNCTION_DECL
)
4578 tree name
= DECL_NAME (fn
);
4579 return startswith (IDENTIFIER_POINTER (name
), TLS_WRAPPER_PREFIX
);
4582 /* Return an identifier for the name of a temporary variable used to
4583 initialize a static reference. This is now part of the ABI. */
4586 mangle_ref_init_variable (const tree variable
)
4588 start_mangling (variable
);
4589 write_string ("_ZGR");
4590 check_abi_tags (variable
);
4591 write_name (variable
, /*ignore_local_scope=*/0);
4592 /* Avoid name clashes with aggregate initialization of multiple
4593 references at once. */
4594 write_compact_number (current_ref_temp_count
++);
4595 return finish_mangling_get_identifier ();
4598 /* Return an identifier for the mangled name of a C++20 template parameter
4599 object for template argument EXPR. */
4602 mangle_template_parm_object (tree expr
)
4604 start_mangling (expr
);
4605 write_string ("_ZTAX");
4606 write_expression (expr
);
4608 return finish_mangling_get_identifier ();
4611 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
4612 function generates a mangled name for the vtable map variable of
4613 the class type. For example, if the class type is
4614 "std::bad_exception", the mangled name for the class is
4615 "St13bad_exception". This function would generate the name
4616 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
4617 "_VTV<std::bad_exception>::__vtable_map". */
4621 get_mangled_vtable_map_var_name (tree class_type
)
4623 char *var_name
= NULL
;
4624 const char *prefix
= "_ZN4_VTVI";
4625 const char *postfix
= "E12__vtable_mapE";
4627 gcc_assert (TREE_CODE (class_type
) == RECORD_TYPE
);
4629 tree class_id
= DECL_ASSEMBLER_NAME (TYPE_NAME (class_type
));
4631 if (strstr (IDENTIFIER_POINTER (class_id
), "<anon>") != NULL
)
4633 class_id
= get_mangled_id (TYPE_NAME (class_type
));
4634 vtbl_register_mangled_name (TYPE_NAME (class_type
), class_id
);
4637 unsigned int len
= strlen (IDENTIFIER_POINTER (class_id
)) +
4639 strlen (postfix
) + 1;
4641 var_name
= (char *) xmalloc (len
);
4643 sprintf (var_name
, "%s%s%s", prefix
, IDENTIFIER_POINTER (class_id
), postfix
);
4648 #include "gt-cp-mangle.h"