]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/mangle.c
gcc/ada/ChangeLog:
[thirdparty/gcc.git] / gcc / cp / mangle.c
CommitLineData
a5b4465c 1/* Name mangling for the 3.0 -*- C++ -*- ABI.
fbd26352 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
de5ccbfb 3 Written by Alex Samuel <samuel@codesourcery.com>
98eaf693 4
6f0d25a6 5 This file is part of GCC.
98eaf693 6
6f0d25a6 7 GCC is free software; you can redistribute it and/or modify it
98eaf693 8 under the terms of the GNU General Public License as published by
aa139c3f 9 the Free Software Foundation; either version 3, or (at your option)
98eaf693 10 any later version.
11
6f0d25a6 12 GCC is distributed in the hope that it will be useful, but
98eaf693 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.
16
aa139c3f 17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
98eaf693 20
21/* This file implements mangling of C++ names according to the IA64
22 C++ ABI specification. A mangled name encodes a function or
23 variable's name, scope, type, and/or template arguments into a text
24 identifier. This identifier is used as the function's or
25 variable's linkage name, to preserve compatibility between C++'s
26 language features (templates, scoping, and overloading) and C
27 linkers.
28
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.
33
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:
39
653e5405 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
074ab442 45 mangle_thunk: thunk function or entry */
98eaf693 46
47#include "config.h"
48#include "system.h"
805e22b2 49#include "coretypes.h"
4cba6f60 50#include "target.h"
51#include "vtable-verify.h"
4cba6f60 52#include "cp-tree.h"
4cba6f60 53#include "stringpool.h"
54#include "cgraph.h"
9ed99284 55#include "stor-layout.h"
e100aadc 56#include "flags.h"
399fbdbd 57#include "attribs.h"
98eaf693 58
59/* Debugging support. */
60
61/* Define DEBUG_MANGLE to enable very verbose trace messages. */
62#ifndef DEBUG_MANGLE
63#define DEBUG_MANGLE 0
64#endif
65
66/* Macros for tracing the write_* functions. */
67#if DEBUG_MANGLE
68# define MANGLE_TRACE(FN, INPUT) \
0a8caa99 69 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
98eaf693 70# define MANGLE_TRACE_TREE(FN, NODE) \
71 fprintf (stderr, " %-24s: %-24s (%p)\n", \
f3d35d4d 72 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
98eaf693 73#else
74# define MANGLE_TRACE(FN, INPUT)
75# define MANGLE_TRACE_TREE(FN, NODE)
76#endif
77
3160db1d 78/* Nonzero if NODE is a class template-id. We can't rely on
8762fbe2 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
bbb9efab 82 without parameters inside the template. */
18f2b9d5 83#define CLASSTYPE_TEMPLATE_ID_P(NODE) \
49a669e2 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))))
98eaf693 88
229a58b1 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))
93
2b463b9a 94/* And sometimes we can simplify the code path if we don't need to worry about
95 previous ABIs. */
96#define abi_flag_at_least(flag,N) (flag == 0 || flag >= N)
97#define any_abi_below(N) \
98 (!abi_version_at_least (N) \
99 || !abi_flag_at_least (warn_abi_version, (N)) \
100 || !abi_flag_at_least (flag_abi_compat_version, (N)))
101
98eaf693 102/* Things we only need one of. This module is not reentrant. */
6dc50383 103struct GTY(()) globals {
98eaf693 104 /* An array of the current substitution candidates, in the order
105 we've seen them. */
f1f41a6c 106 vec<tree, va_gc> *substitutions;
16497dfb 107
18f2b9d5 108 /* The entity that is being mangled. */
f13b585c 109 tree GTY ((skip)) entity;
18f2b9d5 110
4d7aaf8e 111 /* How many parameter scopes we are inside. */
112 int parm_depth;
113
18f2b9d5 114 /* True if the mangling will be different in a future version of the
115 ABI. */
116 bool need_abi_warning;
2e9e9363 117
118 /* True if the mangling will be different in C++17 mode. */
40e2decb 119 bool need_cxx17_warning;
6dc50383 120};
f13b585c 121
122static GTY (()) globals G;
123
124/* The obstack on which we build mangled names. */
125static struct obstack *mangle_obstack;
126
127/* The obstack on which we build mangled names that are not going to
128 be IDENTIFIER_NODEs. */
129static struct obstack name_obstack;
130
1eff44ec 131/* The first object on the name_obstack; we use this to free memory
132 allocated on the name_obstack. */
f13b585c 133static void *name_base;
98eaf693 134
135/* Indices into subst_identifiers. These are identifiers used in
136 special substitution rules. */
137typedef enum
138{
139 SUBID_ALLOCATOR,
140 SUBID_BASIC_STRING,
141 SUBID_CHAR_TRAITS,
142 SUBID_BASIC_ISTREAM,
143 SUBID_BASIC_OSTREAM,
144 SUBID_BASIC_IOSTREAM,
145 SUBID_MAX
146}
147substitution_identifier_index_t;
148
149/* For quick substitution checks, look up these common identifiers
150 once only. */
652c3635 151static GTY(()) tree subst_identifiers[SUBID_MAX];
98eaf693 152
153/* Single-letter codes for builtin integer types, defined in
154 <builtin-type>. These are indexed by integer_type_kind values. */
b14b6cce 155static const char
98eaf693 156integer_type_codes[itk_none] =
157{
158 'c', /* itk_char */
159 'a', /* itk_signed_char */
160 'h', /* itk_unsigned_char */
161 's', /* itk_short */
162 't', /* itk_unsigned_short */
163 'i', /* itk_int */
164 'j', /* itk_unsigned_int */
165 'l', /* itk_long */
166 'm', /* itk_unsigned_long */
167 'x', /* itk_long_long */
6388cfe2 168 'y', /* itk_unsigned_long_long */
9f75f026 169 /* __intN types are handled separately */
170 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
98eaf693 171};
172
61e3d157 173static int decl_is_template_id (const tree, tree* const);
8762fbe2 174
98eaf693 175/* Functions for handling substitutions. */
176
61e3d157 177static inline tree canonicalize_for_substitution (tree);
178static void add_substitution (tree);
179static inline int is_std_substitution (const tree,
180 const substitution_identifier_index_t);
181static inline int is_std_substitution_char (const tree,
182 const substitution_identifier_index_t);
183static int find_substitution (tree);
184static void mangle_call_offset (const tree, const tree);
98eaf693 185
186/* Functions for emitting mangled representations of things. */
187
1da3f78a 188static void write_mangled_name (const tree, bool);
61e3d157 189static void write_encoding (const tree);
190static void write_name (tree, const int);
d4701f6c 191static void write_abi_tags (tree);
61e3d157 192static void write_unscoped_name (const tree);
193static void write_unscoped_template_name (const tree);
194static void write_nested_name (const tree);
195static void write_prefix (const tree);
196static void write_template_prefix (const tree);
42a2bdf5 197static void write_unqualified_name (tree);
61e3d157 198static void write_conversion_operator_name (const tree);
199static void write_source_name (tree);
244db24d 200static void write_literal_operator_name (tree);
a8b75081 201static void write_unnamed_type_name (const tree);
202static void write_closure_type_name (const tree);
61e3d157 203static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
204 const unsigned int);
205static void write_number (unsigned HOST_WIDE_INT, const int,
206 const unsigned int);
a8b75081 207static void write_compact_number (int num);
61e3d157 208static void write_integer_cst (const tree);
209static void write_real_cst (const tree);
210static void write_identifier (const char *);
211static void write_special_name_constructor (const tree);
212static void write_special_name_destructor (const tree);
213static void write_type (tree);
214static int write_CV_qualifiers_for_type (const tree);
215static void write_builtin_type (tree);
216static void write_function_type (const tree);
217static void write_bare_function_type (const tree, const int, const tree);
218static void write_method_parms (tree, const int, const tree);
219static void write_class_enum_type (const tree);
220static void write_template_args (tree);
221static void write_expression (tree);
222static void write_template_arg_literal (const tree);
223static void write_template_arg (tree);
224static void write_template_template_arg (const tree);
225static void write_array_type (const tree);
226static void write_pointer_to_member_type (const tree);
227static void write_template_param (const tree);
228static void write_template_template_param (const tree);
229static void write_substitution (const int);
230static int discriminator_for_local_entity (tree);
231static int discriminator_for_string_literal (tree, tree);
232static void write_discriminator (const int);
a8b75081 233static void write_local_name (tree, const tree, const tree);
61e3d157 234static void dump_substitution_candidates (void);
dfecde36 235static tree mangle_decl_string (const tree);
a4a311c3 236static void maybe_check_abi_tags (tree, tree = NULL_TREE, int = 10);
2b463b9a 237static bool equal_abi_tags (tree, tree);
98eaf693 238
239/* Control functions. */
240
dfecde36 241static inline void start_mangling (const tree);
61e3d157 242static tree mangle_special_for_type (const tree, const char *);
98eaf693 243
244/* Append a single character to the end of the mangled
245 representation. */
653e5405 246#define write_char(CHAR) \
f13b585c 247 obstack_1grow (mangle_obstack, (CHAR))
98eaf693 248
47cd6605 249/* Append a sized buffer to the end of the mangled representation. */
653e5405 250#define write_chars(CHAR, LEN) \
f13b585c 251 obstack_grow (mangle_obstack, (CHAR), (LEN))
2ec35239 252
98eaf693 253/* Append a NUL-terminated string to the end of the mangled
254 representation. */
653e5405 255#define write_string(STRING) \
f13b585c 256 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
98eaf693 257
3160db1d 258/* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
f753592a 259 same purpose (context, which may be a type) and value (template
98eaf693 260 decl). See write_template_prefix for more information on what this
261 is used for. */
653e5405 262#define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
263 (TREE_CODE (NODE1) == TREE_LIST \
264 && TREE_CODE (NODE2) == TREE_LIST \
265 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
266 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
267 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
98eaf693 268 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
269
e75e76ea 270/* Write out an unsigned quantity in base 10. */
653e5405 271#define write_unsigned_number(NUMBER) \
0a8caa99 272 write_number ((NUMBER), /*unsigned_p=*/1, 10)
e75e76ea 273
22b930cb 274/* If DECL is a template instance (including the uninstantiated template
275 itself), return nonzero and, if TEMPLATE_INFO is non-NULL, set
276 *TEMPLATE_INFO to its template info. Otherwise return zero. */
8762fbe2 277
278static int
61e3d157 279decl_is_template_id (const tree decl, tree* const template_info)
8762fbe2 280{
281 if (TREE_CODE (decl) == TYPE_DECL)
282 {
283 /* TYPE_DECLs are handled specially. Look at its type to decide
284 if this is a template instantiation. */
61e3d157 285 const tree type = TREE_TYPE (decl);
8762fbe2 286
287 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
288 {
289 if (template_info != NULL)
290 /* For a templated TYPE_DECL, the template info is hanging
291 off the type. */
18f2b9d5 292 *template_info = TYPE_TEMPLATE_INFO (type);
8762fbe2 293 return 1;
294 }
9031d10b 295 }
8762fbe2 296 else
297 {
298 /* Check if this is a primary template. */
299 if (DECL_LANG_SPECIFIC (decl) != NULL
22b930cb 300 && VAR_OR_FUNCTION_DECL_P (decl)
301 && DECL_TEMPLATE_INFO (decl)
8762fbe2 302 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
303 && TREE_CODE (decl) != TEMPLATE_DECL)
304 {
305 if (template_info != NULL)
306 /* For most templated decls, the template info is hanging
307 off the decl. */
308 *template_info = DECL_TEMPLATE_INFO (decl);
309 return 1;
310 }
311 }
312
313 /* It's not a template id. */
314 return 0;
315}
316
98eaf693 317/* Produce debugging output of current substitution candidates. */
318
319static void
eb32e911 320dump_substitution_candidates (void)
98eaf693 321{
322 unsigned i;
db5d9b05 323 tree el;
98eaf693 324
325 fprintf (stderr, " ++ substitutions ");
f1f41a6c 326 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
98eaf693 327 {
98eaf693 328 const char *name = "???";
329
330 if (i > 0)
331 fprintf (stderr, " ");
332 if (DECL_P (el))
333 name = IDENTIFIER_POINTER (DECL_NAME (el));
334 else if (TREE_CODE (el) == TREE_LIST)
335 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
336 else if (TYPE_NAME (el))
eb192f47 337 name = TYPE_NAME_STRING (el);
98eaf693 338 fprintf (stderr, " S%d_ = ", i - 1);
9031d10b 339 if (TYPE_P (el) &&
340 (CP_TYPE_RESTRICT_P (el)
341 || CP_TYPE_VOLATILE_P (el)
98eaf693 342 || CP_TYPE_CONST_P (el)))
343 fprintf (stderr, "CV-");
9031d10b 344 fprintf (stderr, "%s (%s at %p)\n",
f3d35d4d 345 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
98eaf693 346 }
347}
348
2e9e9363 349/* <exception-spec> ::=
350 Do -- non-throwing exception specification
351 DO <expression> E -- computed (instantiation-dependent) noexcept
352 Dw <type>* E -- throw (types) */
353
354static void
355write_exception_spec (tree spec)
356{
357
358 if (!spec || spec == noexcept_false_spec)
359 /* Nothing. */
360 return;
361
362 if (!flag_noexcept_type)
363 {
40e2decb 364 G.need_cxx17_warning = true;
2e9e9363 365 return;
366 }
367
4afe894b 368 if (spec == noexcept_true_spec || spec == empty_except_spec)
2e9e9363 369 write_string ("Do");
4afe894b 370 else if (tree expr = TREE_PURPOSE (spec))
2e9e9363 371 {
4afe894b 372 /* noexcept (expr) */
373 gcc_assert (uses_template_parms (expr));
2e9e9363 374 write_string ("DO");
4afe894b 375 write_expression (expr);
2e9e9363 376 write_char ('E');
377 }
378 else
379 {
4afe894b 380 /* throw (type-list) */
2e9e9363 381 write_string ("Dw");
382 for (tree t = spec; t; t = TREE_CHAIN (t))
383 write_type (TREE_VALUE (t));
384 write_char ('E');
385 }
386}
387
98eaf693 388/* Both decls and types can be substitution candidates, but sometimes
389 they refer to the same thing. For instance, a TYPE_DECL and
390 RECORD_TYPE for the same class refer to the same thing, and should
755edffd 391 be treated accordingly in substitutions. This function returns a
98eaf693 392 canonicalized tree node representing NODE that is used when adding
393 and substitution candidates and finding matches. */
394
395static inline tree
61e3d157 396canonicalize_for_substitution (tree node)
98eaf693 397{
398 /* For a TYPE_DECL, use the type instead. */
399 if (TREE_CODE (node) == TYPE_DECL)
400 node = TREE_TYPE (node);
2fb61329 401 if (TYPE_P (node)
402 && TYPE_CANONICAL (node) != node
403 && TYPE_MAIN_VARIANT (node) != node)
f962c310 404 {
2ba14c1f 405 tree orig = node;
2fb61329 406 /* Here we want to strip the topmost typedef only.
407 We need to do that so is_std_substitution can do proper
408 name matching. */
f962c310 409 if (TREE_CODE (node) == FUNCTION_TYPE)
410 /* Use build_qualified_type and TYPE_QUALS here to preserve
411 the old buggy mangling of attribute noreturn with abi<5. */
412 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
413 TYPE_QUALS (node));
414 else
415 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
416 cp_type_quals (node));
412b8947 417 if (FUNC_OR_METHOD_TYPE_P (node))
2e9e9363 418 {
419 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
420 tree r = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (orig));
421 if (flag_noexcept_type)
422 node = build_exception_variant (node, r);
423 else
424 /* Set the warning flag if appropriate. */
425 write_exception_spec (r);
426 }
f962c310 427 }
98eaf693 428 return node;
429}
430
431/* Add NODE as a substitution candidate. NODE must not already be on
432 the list of candidates. */
433
434static void
61e3d157 435add_substitution (tree node)
98eaf693 436{
437 tree c;
438
439 if (DEBUG_MANGLE)
9031d10b 440 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
f3d35d4d 441 get_tree_code_name (TREE_CODE (node)), (void *) node);
98eaf693 442
443 /* Get the canonicalized substitution candidate for NODE. */
444 c = canonicalize_for_substitution (node);
445 if (DEBUG_MANGLE && c != node)
446 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
f3d35d4d 447 get_tree_code_name (TREE_CODE (node)), (void *) node);
98eaf693 448 node = c;
449
98eaf693 450 /* Make sure NODE isn't already a candidate. */
5e8689fb 451 if (flag_checking)
452 {
453 int i;
454 tree candidate;
455
456 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
457 {
458 gcc_assert (!(DECL_P (node) && node == candidate));
459 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
092b1d6f 460 && same_type_p (node, candidate)));
5e8689fb 461 }
462 }
98eaf693 463
464 /* Put the decl onto the varray of substitution candidates. */
f1f41a6c 465 vec_safe_push (G.substitutions, node);
98eaf693 466
467 if (DEBUG_MANGLE)
468 dump_substitution_candidates ();
469}
470
3160db1d 471/* Helper function for find_substitution. Returns nonzero if NODE,
98eaf693 472 which may be a decl or a CLASS_TYPE, is a template-id with template
473 name of substitution_index[INDEX] in the ::std namespace. */
474
9031d10b 475static inline int
61e3d157 476is_std_substitution (const tree node,
477 const substitution_identifier_index_t index)
98eaf693 478{
479 tree type = NULL;
480 tree decl = NULL;
481
482 if (DECL_P (node))
483 {
484 type = TREE_TYPE (node);
485 decl = node;
486 }
487 else if (CLASS_TYPE_P (node))
488 {
489 type = node;
490 decl = TYPE_NAME (node);
491 }
9031d10b 492 else
98eaf693 493 /* These are not the droids you're looking for. */
494 return 0;
495
f753592a 496 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
9031d10b 497 && TYPE_LANG_SPECIFIC (type)
18f2b9d5 498 && TYPE_TEMPLATE_INFO (type)
9031d10b 499 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
f753592a 500 == subst_identifiers[index]));
98eaf693 501}
502
b795d0cb 503/* Return the ABI tags (the TREE_VALUE of the "abi_tag" attribute entry) for T,
504 which can be a decl or type. */
505
506static tree
507get_abi_tags (tree t)
508{
509 if (!t || TREE_CODE (t) == NAMESPACE_DECL)
510 return NULL_TREE;
511
512 if (DECL_P (t) && DECL_DECLARES_TYPE_P (t))
513 t = TREE_TYPE (t);
514
515 tree attrs;
516 if (TYPE_P (t))
517 attrs = TYPE_ATTRIBUTES (t);
518 else
519 attrs = DECL_ATTRIBUTES (t);
520
521 tree tags = lookup_attribute ("abi_tag", attrs);
522 if (tags)
523 tags = TREE_VALUE (tags);
524 return tags;
525}
526
3160db1d 527/* Helper function for find_substitution. Returns nonzero if NODE,
98eaf693 528 which may be a decl or a CLASS_TYPE, is the template-id
529 ::std::identifier<char>, where identifier is
530 substitution_index[INDEX]. */
531
532static inline int
61e3d157 533is_std_substitution_char (const tree node,
534 const substitution_identifier_index_t index)
98eaf693 535{
536 tree args;
537 /* Check NODE's name is ::std::identifier. */
538 if (!is_std_substitution (node, index))
539 return 0;
540 /* Figure out its template args. */
541 if (DECL_P (node))
9031d10b 542 args = DECL_TI_ARGS (node);
98eaf693 543 else if (CLASS_TYPE_P (node))
544 args = CLASSTYPE_TI_ARGS (node);
545 else
546 /* Oops, not a template. */
547 return 0;
548 /* NODE's template arg list should be <char>. */
9031d10b 549 return
98eaf693 550 TREE_VEC_LENGTH (args) == 1
551 && TREE_VEC_ELT (args, 0) == char_type_node;
552}
553
554/* Check whether a substitution should be used to represent NODE in
555 the mangling.
556
557 First, check standard special-case substitutions.
558
9031d10b 559 <substitution> ::= St
653e5405 560 # ::std
98eaf693 561
653e5405 562 ::= Sa
98eaf693 563 # ::std::allocator
564
653e5405 565 ::= Sb
566 # ::std::basic_string
98eaf693 567
653e5405 568 ::= Ss
569 # ::std::basic_string<char,
98eaf693 570 ::std::char_traits<char>,
571 ::std::allocator<char> >
572
653e5405 573 ::= Si
574 # ::std::basic_istream<char, ::std::char_traits<char> >
98eaf693 575
653e5405 576 ::= So
577 # ::std::basic_ostream<char, ::std::char_traits<char> >
98eaf693 578
653e5405 579 ::= Sd
580 # ::std::basic_iostream<char, ::std::char_traits<char> >
98eaf693 581
582 Then examine the stack of currently available substitution
583 candidates for entities appearing earlier in the same mangling
584
585 If a substitution is found, write its mangled representation and
3160db1d 586 return nonzero. If none is found, just return zero. */
98eaf693 587
588static int
61e3d157 589find_substitution (tree node)
98eaf693 590{
591 int i;
f1f41a6c 592 const int size = vec_safe_length (G.substitutions);
98eaf693 593 tree decl;
594 tree type;
7cd4e87d 595 const char *abbr = NULL;
98eaf693 596
597 if (DEBUG_MANGLE)
598 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
f3d35d4d 599 get_tree_code_name (TREE_CODE (node)), (void *) node);
98eaf693 600
601 /* Obtain the canonicalized substitution representation for NODE.
602 This is what we'll compare against. */
603 node = canonicalize_for_substitution (node);
604
605 /* Check for builtin substitutions. */
606
607 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
608 type = TYPE_P (node) ? node : TREE_TYPE (node);
609
610 /* Check for std::allocator. */
9031d10b 611 if (decl
f753592a 612 && is_std_substitution (decl, SUBID_ALLOCATOR)
613 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
7cd4e87d 614 abbr = "Sa";
98eaf693 615
616 /* Check for std::basic_string. */
7cd4e87d 617 else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
98eaf693 618 {
e75e76ea 619 if (TYPE_P (node))
98eaf693 620 {
9031d10b 621 /* If this is a type (i.e. a fully-qualified template-id),
622 check for
653e5405 623 std::basic_string <char,
624 std::char_traits<char>,
98eaf693 625 std::allocator<char> > . */
3119c950 626 if (cp_type_quals (type) == TYPE_UNQUALIFIED
98eaf693 627 && CLASSTYPE_USE_TEMPLATE (type))
628 {
629 tree args = CLASSTYPE_TI_ARGS (type);
630 if (TREE_VEC_LENGTH (args) == 3
b7f68aff 631 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
98eaf693 632 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
633 SUBID_CHAR_TRAITS)
634 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
635 SUBID_ALLOCATOR))
7cd4e87d 636 abbr = "Ss";
98eaf693 637 }
638 }
639 else
640 /* Substitute for the template name only if this isn't a type. */
7cd4e87d 641 abbr = "Sb";
98eaf693 642 }
643
644 /* Check for basic_{i,o,io}stream. */
7cd4e87d 645 else if (TYPE_P (node)
646 && cp_type_quals (type) == TYPE_UNQUALIFIED
647 && CLASS_TYPE_P (type)
648 && CLASSTYPE_USE_TEMPLATE (type)
649 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
98eaf693 650 {
9031d10b 651 /* First, check for the template
98eaf693 652 args <char, std::char_traits<char> > . */
653 tree args = CLASSTYPE_TI_ARGS (type);
654 if (TREE_VEC_LENGTH (args) == 2
e4f430b5 655 && TYPE_P (TREE_VEC_ELT (args, 0))
b7f68aff 656 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
98eaf693 657 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
658 SUBID_CHAR_TRAITS))
659 {
660 /* Got them. Is this basic_istream? */
290031a3 661 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
7cd4e87d 662 abbr = "Si";
98eaf693 663 /* Or basic_ostream? */
290031a3 664 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
7cd4e87d 665 abbr = "So";
98eaf693 666 /* Or basic_iostream? */
290031a3 667 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
7cd4e87d 668 abbr = "Sd";
98eaf693 669 }
670 }
671
672 /* Check for namespace std. */
7cd4e87d 673 else if (decl && DECL_NAMESPACE_STD_P (decl))
98eaf693 674 {
675 write_string ("St");
676 return 1;
677 }
678
7cd4e87d 679 tree tags = NULL_TREE;
776bb221 680 if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
b795d0cb 681 tags = get_abi_tags (type);
98eaf693 682 /* Now check the list of available substitutions for this mangling
47cd6605 683 operation. */
7cd4e87d 684 if (!abbr || tags) for (i = 0; i < size; ++i)
98eaf693 685 {
f1f41a6c 686 tree candidate = (*G.substitutions)[i];
98eaf693 687 /* NODE is a matched to a candidate if it's the same decl node or
688 if it's the same type. */
689 if (decl == candidate
272bba4d 690 || (TYPE_P (candidate) && type && TYPE_P (node)
98eaf693 691 && same_type_p (type, candidate))
692 || NESTED_TEMPLATE_MATCH (node, candidate))
693 {
694 write_substitution (i);
695 return 1;
696 }
697 }
698
7cd4e87d 699 if (!abbr)
700 /* No substitution found. */
701 return 0;
702
703 write_string (abbr);
704 if (tags)
705 {
706 /* If there are ABI tags on the abbreviation, it becomes
707 a substitution candidate. */
708 write_abi_tags (tags);
709 add_substitution (node);
710 }
711 return 1;
98eaf693 712}
713
527cb890 714/* Returns whether DECL's symbol name should be the plain unqualified-id
715 rather than a more complicated mangled name. */
716
717static bool
718unmangled_name_p (const tree decl)
719{
720 if (TREE_CODE (decl) == FUNCTION_DECL)
721 {
722 /* The names of `extern "C"' functions are not mangled. */
723 return (DECL_EXTERN_C_FUNCTION_P (decl)
724 /* But overloaded operator names *are* mangled. */
725 && !DECL_OVERLOADED_OPERATOR_P (decl));
726 }
727 else if (VAR_P (decl))
728 {
729 /* static variables are mangled. */
730 if (!DECL_EXTERNAL_LINKAGE_P (decl))
731 return false;
732
733 /* extern "C" declarations aren't mangled. */
734 if (DECL_EXTERN_C_P (decl))
735 return true;
736
737 /* Other variables at non-global scope are mangled. */
738 if (CP_DECL_CONTEXT (decl) != global_namespace)
739 return false;
740
741 /* Variable template instantiations are mangled. */
742 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
743 && variable_template_p (DECL_TI_TEMPLATE (decl)))
744 return false;
745
746 /* Declarations with ABI tags are mangled. */
b795d0cb 747 if (get_abi_tags (decl))
527cb890 748 return false;
749
750 /* The names of non-static global variables aren't mangled. */
751 return true;
752 }
753
754 return false;
755}
98eaf693 756
1da3f78a 757/* TOP_LEVEL is true, if this is being called at outermost level of
758 mangling. It should be false when mangling a decl appearing in an
759 expression within some other mangling.
9031d10b 760
1da3f78a 761 <mangled-name> ::= _Z <encoding> */
98eaf693 762
89df180d 763static void
1da3f78a 764write_mangled_name (const tree decl, bool top_level)
98eaf693 765{
766 MANGLE_TRACE_TREE ("mangled-name", decl);
767
527cb890 768 check_abi_tags (decl);
9031d10b 769
527cb890 770 if (unmangled_name_p (decl))
771 {
1da3f78a 772 if (top_level)
773 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
774 else
775 {
776 /* The standard notes: "The <encoding> of an extern "C"
653e5405 777 function is treated like global-scope data, i.e. as its
778 <source-name> without a type." We cannot write
779 overloaded operators that way though, because it contains
780 characters invalid in assembler. */
f591db9a 781 write_string ("_Z");
1da3f78a 782 write_source_name (DECL_NAME (decl));
783 }
784 }
98eaf693 785 else
98eaf693 786 {
787 write_string ("_Z");
788 write_encoding (decl);
789 }
790}
791
527cb890 792/* Returns true if the return type of DECL is part of its signature, and
793 therefore its mangling. */
794
795bool
796mangle_return_type_p (tree decl)
797{
798 return (!DECL_CONSTRUCTOR_P (decl)
799 && !DECL_DESTRUCTOR_P (decl)
800 && !DECL_CONV_FN_P (decl)
801 && decl_is_template_id (decl, NULL));
802}
803
98eaf693 804/* <encoding> ::= <function name> <bare-function-type>
b7f68aff 805 ::= <data name> */
98eaf693 806
807static void
61e3d157 808write_encoding (const tree decl)
98eaf693 809{
810 MANGLE_TRACE_TREE ("encoding", decl);
811
98eaf693 812 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
813 {
9d8c6ec8 814 /* For overloaded operators write just the mangled name
815 without arguments. */
816 if (DECL_OVERLOADED_OPERATOR_P (decl))
817 write_name (decl, /*ignore_local_scope=*/0);
818 else
819 write_source_name (DECL_NAME (decl));
98eaf693 820 return;
821 }
822
bbb9efab 823 write_name (decl, /*ignore_local_scope=*/0);
98eaf693 824 if (TREE_CODE (decl) == FUNCTION_DECL)
825 {
826 tree fn_type;
d5460724 827 tree d;
2e9e9363 828 bool tmpl = decl_is_template_id (decl, NULL);
98eaf693 829
2e9e9363 830 if (tmpl)
d5460724 831 {
832 fn_type = get_mostly_instantiated_function_type (decl);
833 /* FN_TYPE will not have parameter types for in-charge or
834 VTT parameters. Therefore, we pass NULL_TREE to
835 write_bare_function_type -- otherwise, it will get
836 confused about which artificial parameters to skip. */
837 d = NULL_TREE;
838 }
98eaf693 839 else
d5460724 840 {
841 fn_type = TREE_TYPE (decl);
842 d = decl;
843 }
98eaf693 844
9031d10b 845 write_bare_function_type (fn_type,
527cb890 846 mangle_return_type_p (decl),
d5460724 847 d);
98eaf693 848 }
98eaf693 849}
850
a8b75081 851/* Lambdas can have a bit more context for mangling, specifically VAR_DECL
852 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
853
854static tree
855decl_mangling_context (tree decl)
856{
16a1895e 857 tree tcontext = targetm.cxx.decl_mangling_context (decl);
858
859 if (tcontext != NULL_TREE)
860 return tcontext;
861
5e410273 862 if (TREE_CODE (decl) == TEMPLATE_DECL
863 && DECL_TEMPLATE_RESULT (decl))
864 decl = DECL_TEMPLATE_RESULT (decl);
865
a8b75081 866 if (TREE_CODE (decl) == TYPE_DECL
867 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
868 {
869 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
870 if (extra)
871 return extra;
872 }
e422db1c 873 else if (template_type_parameter_p (decl))
8a03d6ff 874 /* template type parms have no mangling context. */
875 return NULL_TREE;
a8b75081 876 return CP_DECL_CONTEXT (decl);
877}
878
98eaf693 879/* <name> ::= <unscoped-name>
653e5405 880 ::= <unscoped-template-name> <template-args>
98eaf693 881 ::= <nested-name>
9031d10b 882 ::= <local-name>
bbb9efab 883
3160db1d 884 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
bbb9efab 885 called from <local-name>, which mangles the enclosing scope
886 elsewhere and then uses this function to mangle just the part
887 underneath the function scope. So don't use the <local-name>
888 production, to avoid an infinite recursion. */
98eaf693 889
890static void
61e3d157 891write_name (tree decl, const int ignore_local_scope)
98eaf693 892{
e75e76ea 893 tree context;
894
98eaf693 895 MANGLE_TRACE_TREE ("name", decl);
896
8762fbe2 897 if (TREE_CODE (decl) == TYPE_DECL)
898 {
899 /* In case this is a typedef, fish out the corresponding
900 TYPE_DECL for the main variant. */
901 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
8762fbe2 902 }
a8b75081 903
904 context = decl_mangling_context (decl);
62164d4c 905
f7b5b4ed 906 gcc_assert (context != NULL_TREE);
907
229a58b1 908 if (abi_warn_or_compat_version_crosses (7)
74b777e5 909 && ignore_local_scope
910 && TREE_CODE (context) == PARM_DECL)
911 G.need_abi_warning = 1;
912
57b47eb0 913 /* A decl in :: or ::std scope is treated specially. The former is
914 mangled using <unscoped-name> or <unscoped-template-name>, the
915 latter with a special substitution. Also, a name that is
916 directly in a local function scope is also mangled with
917 <unscoped-name> rather than a full <nested-name>. */
f7b5b4ed 918 if (context == global_namespace
57b47eb0 919 || DECL_NAMESPACE_STD_P (context)
dc78d83a 920 || (ignore_local_scope
921 && (TREE_CODE (context) == FUNCTION_DECL
922 || (abi_version_at_least (7)
923 && TREE_CODE (context) == PARM_DECL))))
98eaf693 924 {
8762fbe2 925 tree template_info;
926 /* Is this a template instance? */
927 if (decl_is_template_id (decl, &template_info))
98eaf693 928 {
8762fbe2 929 /* Yes: use <unscoped-template-name>. */
930 write_unscoped_template_name (TI_TEMPLATE (template_info));
931 write_template_args (TI_ARGS (template_info));
98eaf693 932 }
933 else
934 /* Everything else gets an <unqualified-name>. */
935 write_unscoped_name (decl);
936 }
98eaf693 937 else
bbb9efab 938 {
939 /* Handle local names, unless we asked not to (that is, invoked
653e5405 940 under <local-name>, to handle only the part of the name under
941 the local scope). */
bbb9efab 942 if (!ignore_local_scope)
653e5405 943 {
bbb9efab 944 /* Scan up the list of scope context, looking for a
945 function. If we find one, this entity is in local
946 function scope. local_entity tracks context one scope
947 level down, so it will contain the element that's
948 directly in that function's scope, either decl or one of
949 its enclosing scopes. */
950 tree local_entity = decl;
f7b5b4ed 951 while (context != global_namespace)
bbb9efab 952 {
953 /* Make sure we're always dealing with decls. */
f7b5b4ed 954 if (TYPE_P (context))
bbb9efab 955 context = TYPE_NAME (context);
956 /* Is this a function? */
a8b75081 957 if (TREE_CODE (context) == FUNCTION_DECL
958 || TREE_CODE (context) == PARM_DECL)
bbb9efab 959 {
960 /* Yes, we have local scope. Use the <local-name>
961 production for the innermost function scope. */
962 write_local_name (context, local_entity, decl);
963 return;
964 }
965 /* Up one scope level. */
966 local_entity = context;
a8b75081 967 context = decl_mangling_context (context);
bbb9efab 968 }
969
970 /* No local scope found? Fall through to <nested-name>. */
971 }
972
973 /* Other decls get a <nested-name> to encode their scope. */
974 write_nested_name (decl);
975 }
98eaf693 976}
977
978/* <unscoped-name> ::= <unqualified-name>
653e5405 979 ::= St <unqualified-name> # ::std:: */
98eaf693 980
981static void
61e3d157 982write_unscoped_name (const tree decl)
98eaf693 983{
16a1895e 984 tree context = decl_mangling_context (decl);
98eaf693 985
986 MANGLE_TRACE_TREE ("unscoped-name", decl);
987
988 /* Is DECL in ::std? */
989 if (DECL_NAMESPACE_STD_P (context))
990 {
991 write_string ("St");
992 write_unqualified_name (decl);
993 }
092b1d6f 994 else
995 {
996 /* If not, it should be either in the global namespace, or directly
f7b5b4ed 997 in a local function scope. A lambda can also be mangled in the
998 scope of a default argument. */
9031d10b 999 gcc_assert (context == global_namespace
f7b5b4ed 1000 || TREE_CODE (context) == PARM_DECL
092b1d6f 1001 || TREE_CODE (context) == FUNCTION_DECL);
9031d10b 1002
092b1d6f 1003 write_unqualified_name (decl);
1004 }
98eaf693 1005}
1006
1007/* <unscoped-template-name> ::= <unscoped-name>
653e5405 1008 ::= <substitution> */
98eaf693 1009
1010static void
61e3d157 1011write_unscoped_template_name (const tree decl)
98eaf693 1012{
1013 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
1014
1015 if (find_substitution (decl))
1016 return;
1017 write_unscoped_name (decl);
1018 add_substitution (decl);
1019}
1020
1021/* Write the nested name, including CV-qualifiers, of DECL.
1022
2ba14c1f 1023 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1024 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
98eaf693 1025
2ba14c1f 1026 <ref-qualifier> ::= R # & ref-qualifier
1027 ::= O # && ref-qualifier
98eaf693 1028 <CV-qualifiers> ::= [r] [V] [K] */
1029
1030static void
61e3d157 1031write_nested_name (const tree decl)
98eaf693 1032{
8762fbe2 1033 tree template_info;
1034
98eaf693 1035 MANGLE_TRACE_TREE ("nested-name", decl);
1036
1037 write_char ('N');
9031d10b 1038
98eaf693 1039 /* Write CV-qualifiers, if this is a member function. */
9031d10b 1040 if (TREE_CODE (decl) == FUNCTION_DECL
98eaf693 1041 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1042 {
1043 if (DECL_VOLATILE_MEMFUNC_P (decl))
1044 write_char ('V');
1045 if (DECL_CONST_MEMFUNC_P (decl))
1046 write_char ('K');
2ba14c1f 1047 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
1048 {
1049 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
1050 write_char ('O');
1051 else
1052 write_char ('R');
1053 }
98eaf693 1054 }
1055
8762fbe2 1056 /* Is this a template instance? */
1057 if (decl_is_template_id (decl, &template_info))
98eaf693 1058 {
8762fbe2 1059 /* Yes, use <template-prefix>. */
98eaf693 1060 write_template_prefix (decl);
8762fbe2 1061 write_template_args (TI_ARGS (template_info));
98eaf693 1062 }
42af8c2a 1063 else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
fbfe1b55 1064 && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
4682b6fe 1065 {
1066 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1067 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1068 {
1069 write_template_prefix (decl);
1070 write_template_args (TREE_OPERAND (name, 1));
1071 }
1072 else
1073 {
7abd84e4 1074 write_prefix (decl_mangling_context (decl));
4682b6fe 1075 write_unqualified_name (decl);
1076 }
1077 }
98eaf693 1078 else
1079 {
8762fbe2 1080 /* No, just use <prefix> */
272bba4d 1081 write_prefix (decl_mangling_context (decl));
bbb9efab 1082 write_unqualified_name (decl);
98eaf693 1083 }
1084 write_char ('E');
1085}
1086
18f2b9d5 1087/* <prefix> ::= <prefix> <unqualified-name>
653e5405 1088 ::= <template-param>
1089 ::= <template-prefix> <template-args>
c5a7bf9e 1090 ::= <decltype>
98eaf693 1091 ::= # empty
1092 ::= <substitution> */
1093
1094static void
61e3d157 1095write_prefix (const tree node)
98eaf693 1096{
1097 tree decl;
8762fbe2 1098 /* Non-NULL if NODE represents a template-id. */
1099 tree template_info = NULL;
1100
98eaf693 1101 if (node == NULL
1102 || node == global_namespace)
1103 return;
1104
a8b75081 1105 MANGLE_TRACE_TREE ("prefix", node);
1106
c5a7bf9e 1107 if (TREE_CODE (node) == DECLTYPE_TYPE)
1108 {
1109 write_type (node);
1110 return;
1111 }
1112
98eaf693 1113 if (find_substitution (node))
1114 return;
1115
8762fbe2 1116 if (DECL_P (node))
98eaf693 1117 {
a8b75081 1118 /* If this is a function or parm decl, that means we've hit function
bbb9efab 1119 scope, so this prefix must be for a local name. In this
1120 case, we're under the <local-name> production, which encodes
1121 the enclosing function scope elsewhere. So don't continue
1122 here. */
a8b75081 1123 if (TREE_CODE (node) == FUNCTION_DECL
1124 || TREE_CODE (node) == PARM_DECL)
bbb9efab 1125 return;
1126
8762fbe2 1127 decl = node;
1128 decl_is_template_id (decl, &template_info);
98eaf693 1129 }
8762fbe2 1130 else
8762fbe2 1131 {
18f2b9d5 1132 /* Node is a type. */
8762fbe2 1133 decl = TYPE_NAME (node);
1134 if (CLASSTYPE_TEMPLATE_ID_P (node))
18f2b9d5 1135 template_info = TYPE_TEMPLATE_INFO (node);
8762fbe2 1136 }
1137
f591db9a 1138 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
18f2b9d5 1139 write_template_param (node);
1140 else if (template_info != NULL)
8762fbe2 1141 /* Templated. */
98eaf693 1142 {
8762fbe2 1143 write_template_prefix (decl);
1144 write_template_args (TI_ARGS (template_info));
98eaf693 1145 }
4682b6fe 1146 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1147 {
1148 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1149 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1150 {
1151 write_template_prefix (decl);
1152 write_template_args (TREE_OPERAND (name, 1));
1153 }
1154 else
1155 {
7abd84e4 1156 write_prefix (decl_mangling_context (decl));
4682b6fe 1157 write_unqualified_name (decl);
1158 }
1159 }
98eaf693 1160 else
1161 /* Not templated. */
1162 {
a8b75081 1163 write_prefix (decl_mangling_context (decl));
bbb9efab 1164 write_unqualified_name (decl);
80a58eb0 1165 if (VAR_P (decl)
a8b75081 1166 || TREE_CODE (decl) == FIELD_DECL)
1167 {
1168 /* <data-member-prefix> := <member source-name> M */
1169 write_char ('M');
1170 return;
1171 }
98eaf693 1172 }
1173
1174 add_substitution (node);
1175}
1176
1177/* <template-prefix> ::= <prefix> <template component>
653e5405 1178 ::= <template-param>
1179 ::= <substitution> */
98eaf693 1180
1181static void
61e3d157 1182write_template_prefix (const tree node)
98eaf693 1183{
1184 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1185 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
7abd84e4 1186 tree context = decl_mangling_context (decl);
8762fbe2 1187 tree template_info;
607a5d68 1188 tree templ;
98eaf693 1189 tree substitution;
1190
1191 MANGLE_TRACE_TREE ("template-prefix", node);
1192
1193 /* Find the template decl. */
8762fbe2 1194 if (decl_is_template_id (decl, &template_info))
607a5d68 1195 templ = TI_TEMPLATE (template_info);
4682b6fe 1196 else if (TREE_CODE (type) == TYPENAME_TYPE)
1197 /* For a typename type, all we have is the name. */
1198 templ = DECL_NAME (decl);
98eaf693 1199 else
092b1d6f 1200 {
1201 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
9031d10b 1202
607a5d68 1203 templ = TYPE_TI_TEMPLATE (type);
092b1d6f 1204 }
98eaf693 1205
f753592a 1206 /* For a member template, though, the template name for the
1207 innermost name must have all the outer template levels
1208 instantiated. For instance, consider
1209
1210 template<typename T> struct Outer {
1211 template<typename U> struct Inner {};
1212 };
1213
1214 The template name for `Inner' in `Outer<int>::Inner<float>' is
1215 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1216 levels separately, so there's no TEMPLATE_DECL available for this
1217 (there's only `Outer<T>::Inner<U>').
1218
1219 In order to get the substitutions right, we create a special
1220 TREE_LIST to represent the substitution candidate for a nested
1221 template. The TREE_PURPOSE is the template's context, fully
1222 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1223 template.
1224
1225 So, for the example above, `Outer<int>::Inner' is represented as a
1226 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1227 and whose value is `Outer<T>::Inner<U>'. */
c57ab0e8 1228 if (context && TYPE_P (context))
607a5d68 1229 substitution = build_tree_list (context, templ);
f753592a 1230 else
607a5d68 1231 substitution = templ;
98eaf693 1232
1233 if (find_substitution (substitution))
1234 return;
1235
4682b6fe 1236 if (TREE_TYPE (templ)
f591db9a 1237 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
607a5d68 1238 write_template_param (TREE_TYPE (templ));
18f2b9d5 1239 else
1240 {
1241 write_prefix (context);
1242 write_unqualified_name (decl);
1243 }
98eaf693 1244
1245 add_substitution (substitution);
1246}
1247
aa996a7a 1248/* As the list of identifiers for the structured binding declaration
1249 DECL is likely gone, try to recover the DC <source-name>+ E portion
1250 from its mangled name. Return pointer to the DC and set len to
1251 the length up to and including the terminating E. On failure
1252 return NULL. */
1253
1254static const char *
1255find_decomp_unqualified_name (tree decl, size_t *len)
1256{
1257 const char *p = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1258 const char *end = p + IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (decl));
1259 bool nested = false;
1260 if (strncmp (p, "_Z", 2))
1261 return NULL;
1262 p += 2;
1263 if (!strncmp (p, "St", 2))
1264 p += 2;
1265 else if (*p == 'N')
1266 {
1267 nested = true;
1268 ++p;
1269 while (ISDIGIT (p[0]))
1270 {
1271 char *e;
1272 long num = strtol (p, &e, 10);
1273 if (num >= 1 && num < end - e)
1274 p = e + num;
1275 else
1276 break;
1277 }
1278 }
1279 if (strncmp (p, "DC", 2))
1280 return NULL;
1281 if (nested)
1282 {
1283 if (end[-1] != 'E')
1284 return NULL;
1285 --end;
1286 }
1287 if (end[-1] != 'E')
1288 return NULL;
1289 *len = end - p;
1290 return p;
1291}
1292
98eaf693 1293/* We don't need to handle thunks, vtables, or VTTs here. Those are
9031d10b 1294 mangled through special entry points.
98eaf693 1295
1296 <unqualified-name> ::= <operator-name>
9031d10b 1297 ::= <special-name>
9751796f 1298 ::= <source-name>
a8b75081 1299 ::= <unnamed-type-name>
9751796f 1300 ::= <local-source-name>
1301
1302 <local-source-name> ::= L <source-name> <discriminator> */
98eaf693 1303
c44a7393 1304static void
1305write_unqualified_id (tree identifier)
1306{
991449b2 1307 if (IDENTIFIER_CONV_OP_P (identifier))
c44a7393 1308 write_conversion_operator_name (TREE_TYPE (identifier));
816659f9 1309 else if (IDENTIFIER_OVL_OP_P (identifier))
c44a7393 1310 {
a543234d 1311 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier);
1312 write_string (ovl_op->mangled_name);
c44a7393 1313 }
244db24d 1314 else if (UDLIT_OPER_P (identifier))
1315 write_literal_operator_name (identifier);
c44a7393 1316 else
1317 write_source_name (identifier);
1318}
1319
98eaf693 1320static void
42a2bdf5 1321write_unqualified_name (tree decl)
98eaf693 1322{
1323 MANGLE_TRACE_TREE ("unqualified-name", decl);
1324
694683bb 1325 if (identifier_p (decl))
c44a7393 1326 {
1327 write_unqualified_id (decl);
1328 return;
1329 }
1330
d4701f6c 1331 bool found = false;
1332
39e70cbf 1333 if (DECL_NAME (decl) == NULL_TREE)
2336da2a 1334 {
d4701f6c 1335 found = true;
2336da2a 1336 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
aa996a7a 1337 const char *decomp_str = NULL;
1338 size_t decomp_len = 0;
1339 if (VAR_P (decl)
1340 && DECL_DECOMPOSITION_P (decl)
1341 && DECL_NAME (decl) == NULL_TREE
1342 && DECL_NAMESPACE_SCOPE_P (decl))
1343 decomp_str = find_decomp_unqualified_name (decl, &decomp_len);
1344 if (decomp_str)
1345 write_chars (decomp_str, decomp_len);
1346 else
1347 write_source_name (DECL_ASSEMBLER_NAME (decl));
2336da2a 1348 }
39e70cbf 1349 else if (DECL_DECLARES_FUNCTION_P (decl))
98eaf693 1350 {
d4701f6c 1351 found = true;
39e70cbf 1352 if (DECL_CONSTRUCTOR_P (decl))
1353 write_special_name_constructor (decl);
1354 else if (DECL_DESTRUCTOR_P (decl))
1355 write_special_name_destructor (decl);
1356 else if (DECL_CONV_FN_P (decl))
33927c59 1357 {
39e70cbf 1358 /* Conversion operator. Handle it right here.
1359 <operator> ::= cv <type> */
1360 tree type;
1361 if (decl_is_template_id (decl, NULL))
1362 {
1363 tree fn_type;
1364 fn_type = get_mostly_instantiated_function_type (decl);
1365 type = TREE_TYPE (fn_type);
1366 }
18bb3b80 1367 else if (FNDECL_USED_AUTO (decl))
c40a8e3a 1368 type = DECL_SAVED_AUTO_RETURN_TYPE (decl);
39e70cbf 1369 else
1370 type = DECL_CONV_FN_TYPE (decl);
1371 write_conversion_operator_name (type);
1372 }
1373 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1374 {
ca16a224 1375 const char *mangled_name
2cc899e0 1376 = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (decl)]
1377 [DECL_OVERLOADED_OPERATOR_CODE_RAW (decl)].mangled_name);
ca16a224 1378 write_string (mangled_name);
33927c59 1379 }
244db24d 1380 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1381 write_literal_operator_name (DECL_NAME (decl));
33927c59 1382 else
39e70cbf 1383 found = false;
98eaf693 1384 }
39e70cbf 1385
d4701f6c 1386 if (found)
1387 /* OK */;
1388 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1389 && DECL_NAMESPACE_SCOPE_P (decl)
1390 && decl_linkage (decl) == lk_internal)
9751796f 1391 {
1392 MANGLE_TRACE_TREE ("local-source-name", decl);
1393 write_char ('L');
1394 write_source_name (DECL_NAME (decl));
1395 /* The default discriminator is 1, and that's all we ever use,
1396 so there's no code to output one here. */
1397 }
98eaf693 1398 else
a8b75081 1399 {
1400 tree type = TREE_TYPE (decl);
1401
1402 if (TREE_CODE (decl) == TYPE_DECL
4f86cbb0 1403 && TYPE_UNNAMED_P (type))
a8b75081 1404 write_unnamed_type_name (type);
1405 else if (TREE_CODE (decl) == TYPE_DECL
1406 && LAMBDA_TYPE_P (type))
1407 write_closure_type_name (type);
1408 else
1409 write_source_name (DECL_NAME (decl));
1410 }
d4701f6c 1411
2b463b9a 1412 /* We use the ABI tags from the primary class template, ignoring tags on any
42a2bdf5 1413 specializations. This is necessary because C++ doesn't require a
2b463b9a 1414 specialization to be declared before it is used unless the use requires a
1415 complete type, but we need to get the tags right on incomplete types as
1416 well. */
42a2bdf5 1417 if (tree tmpl = most_general_template (decl))
2b463b9a 1418 {
1419 tree res = DECL_TEMPLATE_RESULT (tmpl);
1420 if (res == NULL_TREE)
1421 /* UNBOUND_CLASS_TEMPLATE. */;
1422 else if (DECL_DECLARES_TYPE_P (decl))
1423 decl = res;
1424 else if (any_abi_below (11))
1425 {
1426 /* ABI v10 implicit tags on the template. */
1427 tree mtags = missing_abi_tags (res);
1428 /* Explicit tags on the template. */
1429 tree ttags = get_abi_tags (res);
1430 /* Tags on the instantiation. */
1431 tree dtags = get_abi_tags (decl);
1432
1433 if (mtags && abi_warn_or_compat_version_crosses (10))
1434 G.need_abi_warning = 1;
1435
1436 /* Add the v10 tags to the explicit tags now. */
1437 mtags = chainon (mtags, ttags);
1438
1439 if (!G.need_abi_warning
1440 && abi_warn_or_compat_version_crosses (11)
1441 && !equal_abi_tags (dtags, mtags))
1442 G.need_abi_warning = 1;
1443
1444 if (!abi_version_at_least (10))
1445 /* In abi <10, we only got the explicit tags. */
1446 decl = res;
1447 else if (flag_abi_version == 10)
1448 {
1449 /* In ABI 10, we want explict and implicit tags. */
1450 write_abi_tags (mtags);
1451 return;
1452 }
1453 }
1454 }
1455
1456 tree tags = get_abi_tags (decl);
b3b6f5b5 1457 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
1458 && any_abi_below (11))
1459 if (tree mtags = missing_abi_tags (decl))
1460 {
1461 if (abi_warn_or_compat_version_crosses (11))
1462 G.need_abi_warning = true;
1463 if (!abi_version_at_least (11))
1464 tags = chainon (mtags, tags);
1465 }
2b463b9a 1466 write_abi_tags (tags);
98eaf693 1467}
1468
94302392 1469/* Write the unqualified-name for a conversion operator to TYPE. */
1470
1471static void
61e3d157 1472write_conversion_operator_name (const tree type)
94302392 1473{
1474 write_string ("cv");
1475 write_type (type);
1476}
1477
9031d10b 1478/* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
98eaf693 1479
1480 <source-name> ::= </length/ number> <identifier> */
1481
1482static void
61e3d157 1483write_source_name (tree identifier)
98eaf693 1484{
1485 MANGLE_TRACE_TREE ("source-name", identifier);
1486
e75e76ea 1487 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
98eaf693 1488 write_identifier (IDENTIFIER_POINTER (identifier));
1489}
1490
d4701f6c 1491/* Compare two TREE_STRINGs like strcmp. */
1492
1493int
1494tree_string_cmp (const void *p1, const void *p2)
1495{
1496 if (p1 == p2)
1497 return 0;
1498 tree s1 = *(const tree*)p1;
1499 tree s2 = *(const tree*)p2;
1500 return strcmp (TREE_STRING_POINTER (s1),
1501 TREE_STRING_POINTER (s2));
1502}
1503
2b463b9a 1504/* Return the TREE_LIST of TAGS as a sorted VEC. */
d4701f6c 1505
2b463b9a 1506static vec<tree, va_gc> *
1507sorted_abi_tags (tree tags)
d4701f6c 1508{
f1f41a6c 1509 vec<tree, va_gc> * vec = make_tree_vector();
d4701f6c 1510
1511 for (tree t = tags; t; t = TREE_CHAIN (t))
1512 {
9d78c80e 1513 if (ABI_TAG_IMPLICIT (t))
1514 continue;
d4701f6c 1515 tree str = TREE_VALUE (t);
f1f41a6c 1516 vec_safe_push (vec, str);
d4701f6c 1517 }
1518
f1f41a6c 1519 vec->qsort (tree_string_cmp);
d4701f6c 1520
2b463b9a 1521 return vec;
1522}
1523
1524/* ID is the name of a function or type with abi_tags attribute TAGS.
1525 Write out the name, suitably decorated. */
1526
1527static void
1528write_abi_tags (tree tags)
1529{
1530 if (tags == NULL_TREE)
1531 return;
1532
1533 vec<tree, va_gc> * vec = sorted_abi_tags (tags);
1534
d4701f6c 1535 unsigned i; tree str;
f1f41a6c 1536 FOR_EACH_VEC_ELT (*vec, i, str)
d4701f6c 1537 {
1538 write_string ("B");
1539 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1540 write_identifier (TREE_STRING_POINTER (str));
1541 }
1542
1543 release_tree_vector (vec);
1544}
1545
2b463b9a 1546/* True iff the TREE_LISTS T1 and T2 of ABI tags are equivalent. */
1547
1548static bool
1549equal_abi_tags (tree t1, tree t2)
1550{
1551 releasing_vec v1 = sorted_abi_tags (t1);
1552 releasing_vec v2 = sorted_abi_tags (t2);
1553
1554 unsigned len1 = v1->length();
1555 if (len1 != v2->length())
1556 return false;
1557 for (unsigned i = 0; i < len1; ++i)
1558 if (tree_string_cmp (v1[i], v2[i]) != 0)
1559 return false;
1560 return true;
1561}
1562
244db24d 1563/* Write a user-defined literal operator.
b9eba7af 1564 ::= li <source-name> # "" <source-name>
244db24d 1565 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1566
1567static void
1568write_literal_operator_name (tree identifier)
1569{
1570 const char* suffix = UDLIT_OP_SUFFIX (identifier);
b9eba7af 1571 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1572 write_unsigned_number (strlen (suffix));
1573 write_identifier (suffix);
244db24d 1574}
1575
a8b75081 1576/* Encode 0 as _, and 1+ as n-1_. */
1577
1578static void
1579write_compact_number (int num)
1580{
1581 if (num > 0)
1582 write_unsigned_number (num - 1);
1583 write_char ('_');
1584}
1585
fb5d6a4f 1586/* Return how many unnamed types precede TYPE in its enclosing class. */
1587
1588static int
1589nested_anon_class_index (tree type)
1590{
1591 int index = 0;
1592 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1767a056 1593 for (; member; member = DECL_CHAIN (member))
fb5d6a4f 1594 if (DECL_IMPLICIT_TYPEDEF_P (member))
1595 {
1596 tree memtype = TREE_TYPE (member);
1597 if (memtype == type)
1598 return index;
4f86cbb0 1599 else if (TYPE_UNNAMED_P (memtype))
fb5d6a4f 1600 ++index;
1601 }
1602
70a397b0 1603 if (seen_error ())
1604 return -1;
1605
fb5d6a4f 1606 gcc_unreachable ();
1607}
1608
1609/* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1610
a8b75081 1611static void
a49c5913 1612write_unnamed_type_name (const tree type)
a8b75081 1613{
fb5d6a4f 1614 int discriminator;
a8b75081 1615 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1616
fb5d6a4f 1617 if (TYPE_FUNCTION_SCOPE_P (type))
772b23aa 1618 discriminator = discriminator_for_local_entity (TYPE_NAME (type));
fb5d6a4f 1619 else if (TYPE_CLASS_SCOPE_P (type))
1620 discriminator = nested_anon_class_index (type);
1621 else
1622 {
1623 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1624 /* Just use the old mangling at namespace scope. */
1625 write_source_name (TYPE_IDENTIFIER (type));
1626 return;
1627 }
1628
a8b75081 1629 write_string ("Ut");
fb5d6a4f 1630 write_compact_number (discriminator);
a8b75081 1631}
1632
1633/* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1634 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1635
1636static void
1637write_closure_type_name (const tree type)
1638{
1639 tree fn = lambda_function (type);
1640 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1641 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1642
1643 MANGLE_TRACE_TREE ("closure-type-name", type);
1644
1645 write_string ("Ul");
43936711 1646 write_method_parms (parms, /*method_p=*/1, fn);
a8b75081 1647 write_char ('E');
1648 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1649}
1650
2ec35239 1651/* Convert NUMBER to ascii using base BASE and generating at least
1652 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1653 into which to store the characters. Returns the number of
88aa6d3e 1654 characters generated (these will be laid out in advance of where
2ec35239 1655 BUFFER points). */
1656
1657static int
61e3d157 1658hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1659 char *buffer, const unsigned int min_digits)
2ec35239 1660{
1661 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1662 unsigned digits = 0;
9031d10b 1663
2ec35239 1664 while (number)
1665 {
1666 unsigned HOST_WIDE_INT d = number / base;
9031d10b 1667
2ec35239 1668 *--buffer = base_digits[number - d * base];
1669 digits++;
1670 number = d;
1671 }
1672 while (digits < min_digits)
1673 {
1674 *--buffer = base_digits[0];
1675 digits++;
1676 }
1677 return digits;
1678}
1679
98eaf693 1680/* Non-terminal <number>.
1681
1682 <number> ::= [n] </decimal integer/> */
1683
1684static void
61e3d157 1685write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1686 const unsigned int base)
98eaf693 1687{
2ec35239 1688 char buffer[sizeof (HOST_WIDE_INT) * 8];
1689 unsigned count = 0;
98eaf693 1690
e75e76ea 1691 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
98eaf693 1692 {
1693 write_char ('n');
e75e76ea 1694 number = -((HOST_WIDE_INT) number);
98eaf693 1695 }
2ec35239 1696 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1697 write_chars (buffer + sizeof (buffer) - count, count);
98eaf693 1698}
1699
2ec35239 1700/* Write out an integral CST in decimal. Most numbers are small, and
1701 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
47cd6605 1702 bigger than that, which we must deal with. */
98eaf693 1703
1704static inline void
61e3d157 1705write_integer_cst (const tree cst)
98eaf693 1706{
2ec35239 1707 int sign = tree_int_cst_sgn (cst);
e1d65c9f 1708 widest_int abs_value = wi::abs (wi::to_widest (cst));
1709 if (!wi::fits_uhwi_p (abs_value))
e75e76ea 1710 {
2ec35239 1711 /* A bignum. We do this in chunks, each of which fits in a
47cd6605 1712 HOST_WIDE_INT. */
2ec35239 1713 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1714 unsigned HOST_WIDE_INT chunk;
1715 unsigned chunk_digits;
1716 char *ptr = buffer + sizeof (buffer);
1717 unsigned count = 0;
1718 tree n, base, type;
1719 int done;
1720
1721 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
47cd6605 1722 representable. */
2ec35239 1723 chunk = 1000000000;
1724 chunk_digits = 9;
9031d10b 1725
2ec35239 1726 if (sizeof (HOST_WIDE_INT) >= 8)
1727 {
47cd6605 1728 /* It is at least 64 bits, so 10^18 is representable. */
2ec35239 1729 chunk_digits = 18;
1730 chunk *= chunk;
1731 }
9031d10b 1732
4070745f 1733 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
7016c612 1734 base = build_int_cstu (type, chunk);
e3d0f65c 1735 n = wide_int_to_tree (type, wi::to_wide (cst));
2ec35239 1736
1737 if (sign < 0)
1738 {
1739 write_char ('n');
389dd41b 1740 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
2ec35239 1741 }
1742 do
1743 {
389dd41b 1744 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1745 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
2ec35239 1746 unsigned c;
61e3d157 1747
2ec35239 1748 done = integer_zerop (d);
389dd41b 1749 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
f9ae6f95 1750 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
831d52a2 1751 done ? 1 : chunk_digits);
2ec35239 1752 ptr -= c;
1753 count += c;
1754 n = d;
1755 }
1756 while (!done);
1757 write_chars (ptr, count);
1758 }
9031d10b 1759 else
2ec35239 1760 {
1761 /* A small num. */
2ec35239 1762 if (sign < 0)
e1d65c9f 1763 write_char ('n');
1764 write_unsigned_number (abs_value.to_uhwi ());
e75e76ea 1765 }
98eaf693 1766}
1767
9031d10b 1768/* Write out a floating-point literal.
1769
cc116129 1770 "Floating-point literals are encoded using the bit pattern of the
1771 target processor's internal representation of that number, as a
1772 fixed-length lowercase hexadecimal string, high-order bytes first
1773 (even if the target processor would store low-order bytes first).
1774 The "n" prefix is not used for floating-point literals; the sign
1775 bit is encoded with the rest of the number.
1776
1777 Here are some examples, assuming the IEEE standard representation
1778 for floating point numbers. (Spaces are for readability, not
1779 part of the encoding.)
1780
653e5405 1781 1.0f Lf 3f80 0000 E
1782 -1.0f Lf bf80 0000 E
1783 1.17549435e-38f Lf 0080 0000 E
1784 1.40129846e-45f Lf 0000 0001 E
1785 0.0f Lf 0000 0000 E"
cc116129 1786
1787 Caller is responsible for the Lx and the E. */
1788static void
61e3d157 1789write_real_cst (const tree value)
cc116129 1790{
f591db9a 1791 long target_real[4]; /* largest supported float */
b18dea91 1792 /* Buffer for eight hex digits in a 32-bit number but big enough
1793 even for 64-bit long to avoid warnings. */
1794 char buffer[17];
f591db9a 1795 int i, limit, dir;
1796
1797 tree type = TREE_TYPE (value);
299dd9fa 1798 int words = GET_MODE_BITSIZE (SCALAR_FLOAT_TYPE_MODE (type)) / 32;
f591db9a 1799
1800 real_to_target (target_real, &TREE_REAL_CST (value),
1801 TYPE_MODE (type));
1802
1803 /* The value in target_real is in the target word order,
1804 so we must write it out backward if that happens to be
1805 little-endian. write_number cannot be used, it will
1806 produce uppercase. */
1807 if (FLOAT_WORDS_BIG_ENDIAN)
1808 i = 0, limit = words, dir = 1;
cc116129 1809 else
f591db9a 1810 i = words - 1, limit = -1, dir = -1;
1811
1812 for (; i != limit; i += dir)
cc116129 1813 {
f591db9a 1814 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1815 write_chars (buffer, 8);
cc116129 1816 }
1817}
1818
98eaf693 1819/* Non-terminal <identifier>.
1820
1821 <identifier> ::= </unqualified source code identifier> */
1822
1823static void
61e3d157 1824write_identifier (const char *identifier)
98eaf693 1825{
1826 MANGLE_TRACE ("identifier", identifier);
1827 write_string (identifier);
1828}
1829
1830/* Handle constructor productions of non-terminal <special-name>.
9031d10b 1831 CTOR is a constructor FUNCTION_DECL.
98eaf693 1832
1833 <special-name> ::= C1 # complete object constructor
653e5405 1834 ::= C2 # base object constructor
1835 ::= C3 # complete object allocating constructor
98eaf693 1836
468088ac 1837 Currently, allocating constructors are never used. */
98eaf693 1838
1839static void
61e3d157 1840write_special_name_constructor (const tree ctor)
98eaf693 1841{
7896267d 1842 write_char ('C');
1843 bool new_inh = (flag_new_inheriting_ctors
1844 && DECL_INHERITED_CTOR (ctor));
1845 if (new_inh)
1846 write_char ('I');
092b1d6f 1847 if (DECL_BASE_CONSTRUCTOR_P (ctor))
7896267d 1848 write_char ('2');
468088ac 1849 /* This is the old-style "[unified]" constructor.
1850 In some cases, we may emit this function and call
1851 it from the clones in order to share code and save space. */
1852 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
7896267d 1853 write_char ('4');
f2b8a47d 1854 else
092b1d6f 1855 {
468088ac 1856 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
7896267d 1857 write_char ('1');
092b1d6f 1858 }
7896267d 1859 if (new_inh)
1860 write_type (DECL_INHERITED_CTOR_BASE (ctor));
98eaf693 1861}
1862
1863/* Handle destructor productions of non-terminal <special-name>.
9031d10b 1864 DTOR is a destructor FUNCTION_DECL.
98eaf693 1865
1866 <special-name> ::= D0 # deleting (in-charge) destructor
653e5405 1867 ::= D1 # complete object (in-charge) destructor
468088ac 1868 ::= D2 # base object (not-in-charge) destructor */
98eaf693 1869
1870static void
61e3d157 1871write_special_name_destructor (const tree dtor)
98eaf693 1872{
1873 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1874 write_string ("D0");
98eaf693 1875 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1876 write_string ("D2");
468088ac 1877 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1878 /* This is the old-style "[unified]" destructor.
1879 In some cases, we may emit this function and call
1880 it from the clones in order to share code and save space. */
1881 write_string ("D4");
f2b8a47d 1882 else
092b1d6f 1883 {
468088ac 1884 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
092b1d6f 1885 write_string ("D1");
1886 }
98eaf693 1887}
1888
1889/* Return the discriminator for ENTITY appearing inside
772b23aa 1890 FUNCTION. The discriminator is the lexical ordinal of VAR or TYPE among
1891 entities with the same name and kind in the same FUNCTION. */
98eaf693 1892
1893static int
61e3d157 1894discriminator_for_local_entity (tree entity)
98eaf693 1895{
772b23aa 1896 if (!DECL_LANG_SPECIFIC (entity))
fb5d6a4f 1897 {
772b23aa 1898 /* Some decls, like __FUNCTION__, don't need a discriminator. */
1899 gcc_checking_assert (DECL_ARTIFICIAL (entity));
1900 return 0;
9031d10b 1901 }
772b23aa 1902 else if (tree disc = DECL_DISCRIMINATOR (entity))
1903 return TREE_INT_CST_LOW (disc);
fb5d6a4f 1904 else
772b23aa 1905 /* The first entity with a particular name doesn't get
1906 DECL_DISCRIMINATOR set up. */
1907 return 0;
98eaf693 1908}
1909
1910/* Return the discriminator for STRING, a string literal used inside
63eff20d 1911 FUNCTION. The discriminator is the lexical ordinal of STRING among
98eaf693 1912 string literals used in FUNCTION. */
1913
1914static int
a49c5913 1915discriminator_for_string_literal (tree /*function*/,
1916 tree /*string*/)
98eaf693 1917{
1918 /* For now, we don't discriminate amongst string literals. */
1919 return 0;
1920}
1921
a2929a3a 1922/* <discriminator> := _ <number> # when number < 10
1923 := __ <number> _ # when number >= 10
98eaf693 1924
1925 The discriminator is used only for the second and later occurrences
1926 of the same name within a single function. In this case <number> is
1927 n - 2, if this is the nth occurrence, in lexical order. */
1928
1929static void
61e3d157 1930write_discriminator (const int discriminator)
98eaf693 1931{
47cd6605 1932 /* If discriminator is zero, don't write anything. Otherwise... */
98eaf693 1933 if (discriminator > 0)
1934 {
1935 write_char ('_');
32d3ed1d 1936 if (discriminator - 1 >= 10)
a2929a3a 1937 {
a2929a3a 1938 if (abi_warn_or_compat_version_crosses (11))
1939 G.need_abi_warning = 1;
32d3ed1d 1940 if (abi_version_at_least (11))
1941 write_char ('_');
a2929a3a 1942 }
5a4fcef0 1943 write_unsigned_number (discriminator - 1);
a2929a3a 1944 if (abi_version_at_least (11) && discriminator - 1 >= 10)
1945 write_char ('_');
98eaf693 1946 }
1947}
1948
1949/* Mangle the name of a function-scope entity. FUNCTION is the
a8b75081 1950 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1951 default argument scope. ENTITY is the decl for the entity itself.
1952 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1953 either ENTITY itself or an enclosing scope of ENTITY.
98eaf693 1954
1955 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
a8b75081 1956 := Z <function encoding> E s [<discriminator>]
1957 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
98eaf693 1958
1959static void
a8b75081 1960write_local_name (tree function, const tree local_entity,
61e3d157 1961 const tree entity)
98eaf693 1962{
a8b75081 1963 tree parm = NULL_TREE;
1964
98eaf693 1965 MANGLE_TRACE_TREE ("local-name", entity);
1966
a8b75081 1967 if (TREE_CODE (function) == PARM_DECL)
1968 {
1969 parm = function;
1970 function = DECL_CONTEXT (parm);
1971 }
1972
98eaf693 1973 write_char ('Z');
1974 write_encoding (function);
1975 write_char ('E');
a8b75081 1976
1977 /* For this purpose, parameters are numbered from right-to-left. */
1978 if (parm)
1979 {
1980 tree t;
1981 int i = 0;
1767a056 1982 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
a8b75081 1983 {
1984 if (t == parm)
1985 i = 1;
1986 else if (i)
1987 ++i;
1988 }
1989 write_char ('d');
1990 write_compact_number (i - 1);
1991 }
1992
98eaf693 1993 if (TREE_CODE (entity) == STRING_CST)
1994 {
1995 write_char ('s');
9031d10b 1996 write_discriminator (discriminator_for_string_literal (function,
98eaf693 1997 entity));
1998 }
1999 else
2000 {
bbb9efab 2001 /* Now the <entity name>. Let write_name know its being called
2002 from <local-name>, so it doesn't try to process the enclosing
2003 function scope again. */
2004 write_name (entity, /*ignore_local_scope=*/1);
772b23aa 2005 if (DECL_DISCRIMINATOR_P (local_entity)
2006 && !(TREE_CODE (local_entity) == TYPE_DECL
2007 && (LAMBDA_TYPE_P (TREE_TYPE (local_entity))
2008 || TYPE_UNNAMED_P (TREE_TYPE (local_entity)))))
2009 write_discriminator (discriminator_for_local_entity (local_entity));
98eaf693 2010 }
2011}
2012
9031d10b 2013/* Non-terminals <type> and <CV-qualifier>.
98eaf693 2014
2015 <type> ::= <builtin-type>
653e5405 2016 ::= <function-type>
2017 ::= <class-enum-type>
2018 ::= <array-type>
2019 ::= <pointer-to-member-type>
2020 ::= <template-param>
2021 ::= <substitution>
2022 ::= <CV-qualifier>
2023 ::= P <type> # pointer-to
2024 ::= R <type> # reference-to
2025 ::= C <type> # complex pair (C 2000)
2026 ::= G <type> # imaginary (C 2000) [not supported]
2027 ::= U <source-name> <type> # vendor extended type qualifier
98eaf693 2028
63949b38 2029 C++0x extensions
2030
2031 <type> ::= RR <type> # rvalue reference-to
34da8800 2032 <type> ::= Dt <expression> # decltype of an id-expression or
2033 # class member access
2034 <type> ::= DT <expression> # decltype of an expression
6fe11077 2035 <type> ::= Dn # decltype of nullptr
63949b38 2036
98eaf693 2037 TYPE is a type node. */
2038
9031d10b 2039static void
61e3d157 2040write_type (tree type)
98eaf693 2041{
3160db1d 2042 /* This gets set to nonzero if TYPE turns out to be a (possibly
98eaf693 2043 CV-qualified) builtin type. */
2044 int is_builtin_type = 0;
2045
2046 MANGLE_TRACE_TREE ("type", type);
2047
0cd69b17 2048 if (type == error_mark_node)
2049 return;
2050
f962c310 2051 type = canonicalize_for_substitution (type);
98eaf693 2052 if (find_substitution (type))
2053 return;
9031d10b 2054
8df5a43d 2055
98eaf693 2056 if (write_CV_qualifiers_for_type (type) > 0)
2057 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
2058 mangle the unqualified type. The recursive call is needed here
755edffd 2059 since both the qualified and unqualified types are substitution
98eaf693 2060 candidates. */
2ba14c1f 2061 {
2062 tree t = TYPE_MAIN_VARIANT (type);
399fbdbd 2063 if (TYPE_ATTRIBUTES (t) && !OVERLOAD_TYPE_P (t))
6d02e6b2 2064 {
2065 tree attrs = NULL_TREE;
2066 if (tx_safe_fn_type_p (type))
2067 attrs = tree_cons (get_identifier ("transaction_safe"),
2068 NULL_TREE, attrs);
2069 t = cp_build_type_attribute_variant (t, attrs);
2070 }
399fbdbd 2071 gcc_assert (t != type);
412b8947 2072 if (FUNC_OR_METHOD_TYPE_P (t))
862e5b6d 2073 {
2074 t = build_ref_qualified_type (t, type_memfn_rqual (type));
c2ca6e19 2075 if (flag_noexcept_type)
2076 {
2077 tree r = TYPE_RAISES_EXCEPTIONS (type);
2078 t = build_exception_variant (t, r);
2079 }
399fbdbd 2080 if (abi_version_at_least (8)
2081 || type == TYPE_MAIN_VARIANT (type))
862e5b6d 2082 /* Avoid adding the unqualified function type as a substitution. */
2083 write_function_type (t);
2084 else
2085 write_type (t);
229a58b1 2086 if (abi_warn_or_compat_version_crosses (8))
74b777e5 2087 G.need_abi_warning = 1;
862e5b6d 2088 }
2089 else
2090 write_type (t);
2ba14c1f 2091 }
621eb26c 2092 else if (TREE_CODE (type) == ARRAY_TYPE)
2093 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
2094 so that the cv-qualification of the element type is available
2095 in write_array_type. */
2096 write_array_type (type);
98eaf693 2097 else
e75e76ea 2098 {
eddcdde1 2099 tree type_orig = type;
2100
e75e76ea 2101 /* See through any typedefs. */
2102 type = TYPE_MAIN_VARIANT (type);
412b8947 2103 if (FUNC_OR_METHOD_TYPE_P (type))
e5122931 2104 type = cxx_copy_lang_qualifiers (type, type_orig);
e75e76ea 2105
480e417f 2106 /* According to the C++ ABI, some library classes are passed the
2107 same as the scalar type of their single member and use the same
2108 mangling. */
2109 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2110 type = TREE_TYPE (first_field (type));
2111
05765a91 2112 if (TYPE_PTRDATAMEM_P (type))
120c0017 2113 write_pointer_to_member_type (type);
eddcdde1 2114 else
2115 {
333715c2 2116 /* Handle any target-specific fundamental types. */
2117 const char *target_mangling
eddcdde1 2118 = targetm.mangle_type (type_orig);
333715c2 2119
2120 if (target_mangling)
2121 {
2122 write_string (target_mangling);
43bf2fc5 2123 /* Add substitutions for types other than fundamental
2124 types. */
c21c015b 2125 if (!VOID_TYPE_P (type)
43bf2fc5 2126 && TREE_CODE (type) != INTEGER_TYPE
2127 && TREE_CODE (type) != REAL_TYPE
2128 && TREE_CODE (type) != BOOLEAN_TYPE)
2129 add_substitution (type);
333715c2 2130 return;
2131 }
2132
eddcdde1 2133 switch (TREE_CODE (type))
2134 {
2135 case VOID_TYPE:
2136 case BOOLEAN_TYPE:
2137 case INTEGER_TYPE: /* Includes wchar_t. */
2138 case REAL_TYPE:
e4583147 2139 case FIXED_POINT_TYPE:
eddcdde1 2140 {
2141 /* If this is a typedef, TYPE may not be one of
2142 the standard builtin type nodes, but an alias of one. Use
2143 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
2144 write_builtin_type (TYPE_MAIN_VARIANT (type));
2145 ++is_builtin_type;
2146 }
2147 break;
2148
2149 case COMPLEX_TYPE:
2150 write_char ('C');
2151 write_type (TREE_TYPE (type));
2152 break;
2153
2154 case FUNCTION_TYPE:
2155 case METHOD_TYPE:
2156 write_function_type (type);
2157 break;
2158
2159 case UNION_TYPE:
2160 case RECORD_TYPE:
2161 case ENUMERAL_TYPE:
2162 /* A pointer-to-member function is represented as a special
2163 RECORD_TYPE, so check for this first. */
2164 if (TYPE_PTRMEMFUNC_P (type))
2165 write_pointer_to_member_type (type);
2166 else
2167 write_class_enum_type (type);
2168 break;
2169
2170 case TYPENAME_TYPE:
2171 case UNBOUND_CLASS_TEMPLATE:
2172 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2173 ordinary nested names. */
2174 write_nested_name (TYPE_STUB_DECL (type));
2175 break;
2176
2177 case POINTER_TYPE:
eddcdde1 2178 case REFERENCE_TYPE:
c21c015b 2179 if (TYPE_PTR_P (type))
80e93d60 2180 write_char ('P');
2181 else if (TYPE_REF_IS_RVALUE (type))
2182 write_char ('O');
c4692e04 2183 else
2184 write_char ('R');
80e93d60 2185 {
2186 tree target = TREE_TYPE (type);
2187 /* Attribute const/noreturn are not reflected in mangling.
2188 We strip them here rather than at a lower level because
2189 a typedef or template argument can have function type
2190 with function-cv-quals (that use the same representation),
2191 but you can't have a pointer/reference to such a type. */
74b777e5 2192 if (TREE_CODE (target) == FUNCTION_TYPE)
2193 {
229a58b1 2194 if (abi_warn_or_compat_version_crosses (5)
74b777e5 2195 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2196 G.need_abi_warning = 1;
2197 if (abi_version_at_least (5))
2198 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2199 }
80e93d60 2200 write_type (target);
2201 }
eddcdde1 2202 break;
2203
2204 case TEMPLATE_TYPE_PARM:
1610993e 2205 if (is_auto (type))
2206 {
5357d7fd 2207 if (AUTO_IS_DECLTYPE (type))
2208 write_identifier ("Dc");
2209 else
2210 write_identifier ("Da");
1610993e 2211 ++is_builtin_type;
2212 break;
2213 }
e3533433 2214 /* fall through. */
eddcdde1 2215 case TEMPLATE_PARM_INDEX:
2216 write_template_param (type);
2217 break;
2218
2219 case TEMPLATE_TEMPLATE_PARM:
2220 write_template_template_param (type);
2221 break;
2222
2223 case BOUND_TEMPLATE_TEMPLATE_PARM:
2224 write_template_template_param (type);
2225 write_template_args
2226 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2227 break;
2228
2229 case VECTOR_TYPE:
29e2e80a 2230 if (abi_version_at_least (4))
2231 {
2232 write_string ("Dv");
2233 /* Non-constant vector size would be encoded with
2234 _ expression, but we don't support that yet. */
f08ee65f 2235 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type)
2236 .to_constant ());
29e2e80a 2237 write_char ('_');
2238 }
2239 else
74b777e5 2240 write_string ("U8__vector");
229a58b1 2241 if (abi_warn_or_compat_version_crosses (4))
74b777e5 2242 G.need_abi_warning = 1;
eddcdde1 2243 write_type (TREE_TYPE (type));
2244 break;
2245
2246 case TYPE_PACK_EXPANSION:
4682b6fe 2247 write_string ("Dp");
eddcdde1 2248 write_type (PACK_EXPANSION_PATTERN (type));
2249 break;
2250
34da8800 2251 case DECLTYPE_TYPE:
a8b75081 2252 /* These shouldn't make it into mangling. */
2253 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
a75d43a4 2254 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
a8b75081 2255
87d553db 2256 /* In ABI <5, we stripped decltype of a plain decl. */
74b777e5 2257 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
4d7aaf8e 2258 {
2259 tree expr = DECLTYPE_TYPE_EXPR (type);
2260 tree etype = NULL_TREE;
2261 switch (TREE_CODE (expr))
2262 {
2263 case VAR_DECL:
2264 case PARM_DECL:
2265 case RESULT_DECL:
2266 case FUNCTION_DECL:
2267 case CONST_DECL:
2268 case TEMPLATE_PARM_INDEX:
2269 etype = TREE_TYPE (expr);
2270 break;
2271
2272 default:
2273 break;
2274 }
2275
2276 if (etype && !type_uses_auto (etype))
2277 {
229a58b1 2278 if (abi_warn_or_compat_version_crosses (5))
74b777e5 2279 G.need_abi_warning = 1;
2280 if (!abi_version_at_least (5))
2281 {
2282 write_type (etype);
2283 return;
2284 }
4d7aaf8e 2285 }
2286 }
2287
34da8800 2288 write_char ('D');
2289 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2290 write_char ('t');
2291 else
2292 write_char ('T');
48d94ede 2293 ++cp_unevaluated_operand;
34da8800 2294 write_expression (DECLTYPE_TYPE_EXPR (type));
48d94ede 2295 --cp_unevaluated_operand;
34da8800 2296 write_char ('E');
2297 break;
2298
fa67d8e8 2299 case NULLPTR_TYPE:
2300 write_string ("Dn");
fbcdace8 2301 if (abi_version_at_least (7))
2302 ++is_builtin_type;
229a58b1 2303 if (abi_warn_or_compat_version_crosses (7))
74b777e5 2304 G.need_abi_warning = 1;
fa67d8e8 2305 break;
2306
e56ff3c4 2307 case TYPEOF_TYPE:
85b9be9b 2308 sorry ("mangling %<typeof%>, use %<decltype%> instead");
e56ff3c4 2309 break;
2310
8de5c43e 2311 case UNDERLYING_TYPE:
85b9be9b 2312 sorry ("mangling %<__underlying_type%>");
8de5c43e 2313 break;
2314
c7ca48ea 2315 case LANG_TYPE:
fa67d8e8 2316 /* fall through. */
c7ca48ea 2317
eddcdde1 2318 default:
2319 gcc_unreachable ();
2320 }
e75e76ea 2321 }
2322 }
98eaf693 2323
2324 /* Types other than builtin types are substitution candidates. */
2325 if (!is_builtin_type)
2326 add_substitution (type);
2327}
2328
399fbdbd 2329/* qsort callback for sorting a vector of attribute entries. */
2330
2331static int
2332attr_strcmp (const void *p1, const void *p2)
2333{
2334 tree a1 = *(const tree*)p1;
2335 tree a2 = *(const tree*)p2;
2336
2337 const attribute_spec *as1 = lookup_attribute_spec (get_attribute_name (a1));
2338 const attribute_spec *as2 = lookup_attribute_spec (get_attribute_name (a2));
2339
2340 return strcmp (as1->name, as2->name);
2341}
2342
98eaf693 2343/* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2344 CV-qualifiers written for TYPE.
2345
2346 <CV-qualifiers> ::= [r] [V] [K] */
2347
2348static int
61e3d157 2349write_CV_qualifiers_for_type (const tree type)
98eaf693 2350{
2351 int num_qualifiers = 0;
2352
2353 /* The order is specified by:
2354
2355 "In cases where multiple order-insensitive qualifiers are
2356 present, they should be ordered 'K' (closest to the base type),
399fbdbd 2357 'V', 'r', and 'U' (farthest from the base type) ..." */
2358
2359 /* Mangle attributes that affect type identity as extended qualifiers.
2360
399fbdbd 2361 We don't do this with classes and enums because their attributes
2362 are part of their definitions, not something added on. */
2363
e07f1d23 2364 if (!OVERLOAD_TYPE_P (type))
399fbdbd 2365 {
2366 auto_vec<tree> vec;
2367 for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
2368 {
2369 tree name = get_attribute_name (a);
2370 const attribute_spec *as = lookup_attribute_spec (name);
2371 if (as && as->affects_type_identity
6d02e6b2 2372 && !is_attribute_p ("transaction_safe", name)
399fbdbd 2373 && !is_attribute_p ("abi_tag", name))
2374 vec.safe_push (a);
2375 }
229a58b1 2376 if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
e07f1d23 2377 G.need_abi_warning = true;
2378 if (abi_version_at_least (10))
399fbdbd 2379 {
e07f1d23 2380 vec.qsort (attr_strcmp);
2381 while (!vec.is_empty())
399fbdbd 2382 {
e07f1d23 2383 tree a = vec.pop();
2384 const attribute_spec *as
2385 = lookup_attribute_spec (get_attribute_name (a));
2386
2387 write_char ('U');
2388 write_unsigned_number (strlen (as->name));
2389 write_string (as->name);
2390 if (TREE_VALUE (a))
399fbdbd 2391 {
e07f1d23 2392 write_char ('I');
2393 for (tree args = TREE_VALUE (a); args;
2394 args = TREE_CHAIN (args))
2395 {
2396 tree arg = TREE_VALUE (args);
2397 write_template_arg (arg);
2398 }
2399 write_char ('E');
399fbdbd 2400 }
399fbdbd 2401
e07f1d23 2402 ++num_qualifiers;
2403 }
399fbdbd 2404 }
2405 }
621eb26c 2406
399fbdbd 2407 /* Note that we do not use cp_type_quals below; given "const
621eb26c 2408 int[3]", the "const" is emitted with the "int", not with the
2409 array. */
ce494fcf 2410 cp_cv_quals quals = TYPE_QUALS (type);
98eaf693 2411
ce494fcf 2412 if (quals & TYPE_QUAL_RESTRICT)
98eaf693 2413 {
2414 write_char ('r');
2415 ++num_qualifiers;
2416 }
ce494fcf 2417 if (quals & TYPE_QUAL_VOLATILE)
98eaf693 2418 {
2419 write_char ('V');
2420 ++num_qualifiers;
2421 }
ce494fcf 2422 if (quals & TYPE_QUAL_CONST)
98eaf693 2423 {
2424 write_char ('K');
2425 ++num_qualifiers;
2426 }
2427
2428 return num_qualifiers;
2429}
2430
9031d10b 2431/* Non-terminal <builtin-type>.
98eaf693 2432
9031d10b 2433 <builtin-type> ::= v # void
653e5405 2434 ::= b # bool
2435 ::= w # wchar_t
2436 ::= c # char
2437 ::= a # signed char
2438 ::= h # unsigned char
2439 ::= s # short
2440 ::= t # unsigned short
2441 ::= i # int
2442 ::= j # unsigned int
2443 ::= l # long
2444 ::= m # unsigned long
2445 ::= x # long long, __int64
2446 ::= y # unsigned long long, __int64
2447 ::= n # __int128
2448 ::= o # unsigned __int128
2449 ::= f # float
2450 ::= d # double
2451 ::= e # long double, __float80
2452 ::= g # __float128 [not supported]
2453 ::= u <source-name> # vendor extended type */
98eaf693 2454
9031d10b 2455static void
61e3d157 2456write_builtin_type (tree type)
98eaf693 2457{
9a984729 2458 if (TYPE_CANONICAL (type))
2459 type = TYPE_CANONICAL (type);
2460
98eaf693 2461 switch (TREE_CODE (type))
2462 {
2463 case VOID_TYPE:
2464 write_char ('v');
2465 break;
2466
2467 case BOOLEAN_TYPE:
2468 write_char ('b');
2469 break;
2470
2471 case INTEGER_TYPE:
82362779 2472 /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
924bbf02 2473 isn't in integer_type_nodes. */
84ea6a1d 2474 if (type == wchar_type_node)
98eaf693 2475 write_char ('w');
82362779 2476 else if (type == char8_type_node)
2477 write_string ("Du");
924bbf02 2478 else if (type == char16_type_node)
4682b6fe 2479 write_string ("Ds");
924bbf02 2480 else if (type == char32_type_node)
4682b6fe 2481 write_string ("Di");
98eaf693 2482 else
2483 {
2484 size_t itk;
2485 /* Assume TYPE is one of the shared integer type nodes. Find
2486 it in the array of these nodes. */
90a83261 2487 iagain:
98eaf693 2488 for (itk = 0; itk < itk_none; ++itk)
890d0caa 2489 if (integer_types[itk] != NULL_TREE
9f75f026 2490 && integer_type_codes[itk] != '\0'
890d0caa 2491 && type == integer_types[itk])
98eaf693 2492 {
2493 /* Print the corresponding single-letter code. */
2494 write_char (integer_type_codes[itk]);
2495 break;
2496 }
52eb8ed3 2497
98eaf693 2498 if (itk == itk_none)
90a83261 2499 {
771d21fa 2500 tree t = c_common_type_for_mode (TYPE_MODE (type),
78a8ed03 2501 TYPE_UNSIGNED (type));
42a8ab57 2502 if (type != t)
52eb8ed3 2503 {
42a8ab57 2504 type = t;
2505 goto iagain;
52eb8ed3 2506 }
42a8ab57 2507
2508 if (TYPE_PRECISION (type) == 128)
2509 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
6526d3a8 2510 else
2511 {
42a8ab57 2512 /* Allow for cases where TYPE is not one of the shared
2513 integer type nodes and write a "vendor extended builtin
2514 type" with a name the form intN or uintN, respectively.
2515 Situations like this can happen if you have an
2516 __attribute__((__mode__(__SI__))) type and use exotic
9fb415cc 2517 switches like '-mint8' on AVR. Of course, this is
2518 undefined by the C++ ABI (and '-mint8' is not even
2519 Standard C conforming), but when using such special
2520 options you're pretty much in nowhere land anyway. */
42a8ab57 2521 const char *prefix;
2522 char prec[11]; /* up to ten digits for an unsigned */
2523
2524 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2525 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2526 write_char ('u'); /* "vendor extended builtin type" */
2527 write_unsigned_number (strlen (prefix) + strlen (prec));
2528 write_string (prefix);
2529 write_string (prec);
6526d3a8 2530 }
90a83261 2531 }
98eaf693 2532 }
2533 break;
2534
2535 case REAL_TYPE:
c93d719b 2536 if (type == float_type_node)
98eaf693 2537 write_char ('f');
c93d719b 2538 else if (type == double_type_node)
98eaf693 2539 write_char ('d');
84ea6a1d 2540 else if (type == long_double_type_node)
98eaf693 2541 write_char ('e');
dcd70a22 2542 else if (type == dfloat32_type_node)
2543 write_string ("Df");
2544 else if (type == dfloat64_type_node)
2545 write_string ("Dd");
2546 else if (type == dfloat128_type_node)
2547 write_string ("De");
98eaf693 2548 else
092b1d6f 2549 gcc_unreachable ();
98eaf693 2550 break;
2551
e4583147 2552 case FIXED_POINT_TYPE:
2553 write_string ("DF");
2554 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2555 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2556 if (type == fract_type_node
2557 || type == sat_fract_type_node
2558 || type == accum_type_node
2559 || type == sat_accum_type_node)
2560 write_char ('i');
2561 else if (type == unsigned_fract_type_node
2562 || type == sat_unsigned_fract_type_node
2563 || type == unsigned_accum_type_node
2564 || type == sat_unsigned_accum_type_node)
2565 write_char ('j');
2566 else if (type == short_fract_type_node
2567 || type == sat_short_fract_type_node
2568 || type == short_accum_type_node
2569 || type == sat_short_accum_type_node)
2570 write_char ('s');
2571 else if (type == unsigned_short_fract_type_node
2572 || type == sat_unsigned_short_fract_type_node
2573 || type == unsigned_short_accum_type_node
2574 || type == sat_unsigned_short_accum_type_node)
2575 write_char ('t');
2576 else if (type == long_fract_type_node
2577 || type == sat_long_fract_type_node
2578 || type == long_accum_type_node
2579 || type == sat_long_accum_type_node)
2580 write_char ('l');
2581 else if (type == unsigned_long_fract_type_node
2582 || type == sat_unsigned_long_fract_type_node
2583 || type == unsigned_long_accum_type_node
2584 || type == sat_unsigned_long_accum_type_node)
2585 write_char ('m');
2586 else if (type == long_long_fract_type_node
2587 || type == sat_long_long_fract_type_node
2588 || type == long_long_accum_type_node
2589 || type == sat_long_long_accum_type_node)
2590 write_char ('x');
2591 else if (type == unsigned_long_long_fract_type_node
2592 || type == sat_unsigned_long_long_fract_type_node
2593 || type == unsigned_long_long_accum_type_node
2594 || type == sat_unsigned_long_long_accum_type_node)
2595 write_char ('y');
2596 else
2597 sorry ("mangling unknown fixed point type");
2598 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2599 if (TYPE_SATURATING (type))
2600 write_char ('s');
2601 else
2602 write_char ('n');
2603 break;
2604
98eaf693 2605 default:
092b1d6f 2606 gcc_unreachable ();
98eaf693 2607 }
2608}
2609
2610/* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
aa6b4383 2611 METHOD_TYPE. The return type is mangled before the parameter
2612 types.
98eaf693 2613
2ba14c1f 2614 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
98eaf693 2615
2616static void
61e3d157 2617write_function_type (const tree type)
98eaf693 2618{
2619 MANGLE_TRACE_TREE ("function-type", type);
2620
58274fe8 2621 /* For a pointer to member function, the function type may have
2622 cv-qualifiers, indicating the quals for the artificial 'this'
2623 parameter. */
2624 if (TREE_CODE (type) == METHOD_TYPE)
2625 {
2626 /* The first parameter must be a POINTER_TYPE pointing to the
2627 `this' parameter. */
a8d966c1 2628 tree this_type = class_of_this_parm (type);
58274fe8 2629 write_CV_qualifiers_for_type (this_type);
2630 }
2631
2e9e9363 2632 write_exception_spec (TYPE_RAISES_EXCEPTIONS (type));
2633
6d02e6b2 2634 if (tx_safe_fn_type_p (type))
2635 write_string ("Dx");
2636
98eaf693 2637 write_char ('F');
2638 /* We don't track whether or not a type is `extern "C"'. Note that
2639 you can have an `extern "C"' function that does not have
2640 `extern "C"' type, and vice versa:
2641
2642 extern "C" typedef void function_t();
2643 function_t f; // f has C++ linkage, but its type is
653e5405 2644 // `extern "C"'
98eaf693 2645
2646 typedef void function_t();
2647 extern "C" function_t f; // Vice versa.
2648
2649 See [dcl.link]. */
9031d10b 2650 write_bare_function_type (type, /*include_return_type_p=*/1,
7c8696b2 2651 /*decl=*/NULL);
2ba14c1f 2652 if (FUNCTION_REF_QUALIFIED (type))
2653 {
2654 if (FUNCTION_RVALUE_QUALIFIED (type))
2655 write_char ('O');
2656 else
2657 write_char ('R');
2658 }
98eaf693 2659 write_char ('E');
2660}
2661
7c8696b2 2662/* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
3160db1d 2663 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
7c8696b2 2664 is mangled before the parameter types. If non-NULL, DECL is
c93d719b 2665 FUNCTION_DECL for the function whose type is being emitted. */
98eaf693 2666
2667static void
61e3d157 2668write_bare_function_type (const tree type, const int include_return_type_p,
2669 const tree decl)
98eaf693 2670{
2671 MANGLE_TRACE_TREE ("bare-function-type", type);
2672
2673 /* Mangle the return type, if requested. */
c93d719b 2674 if (include_return_type_p)
98eaf693 2675 write_type (TREE_TYPE (type));
2676
2677 /* Now mangle the types of the arguments. */
4d7aaf8e 2678 ++G.parm_depth;
9031d10b 2679 write_method_parms (TYPE_ARG_TYPES (type),
7c8696b2 2680 TREE_CODE (type) == METHOD_TYPE,
2681 decl);
4d7aaf8e 2682 --G.parm_depth;
98eaf693 2683}
2684
2685/* Write the mangled representation of a method parameter list of
3160db1d 2686 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
7c8696b2 2687 considered a non-static method, and the this parameter is omitted.
2688 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2689 parameters are being emitted. */
98eaf693 2690
2691static void
61e3d157 2692write_method_parms (tree parm_types, const int method_p, const tree decl)
98eaf693 2693{
7c8696b2 2694 tree first_parm_type;
2695 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2696
98eaf693 2697 /* Assume this parameter type list is variable-length. If it ends
2698 with a void type, then it's not. */
2699 int varargs_p = 1;
2700
2701 /* If this is a member function, skip the first arg, which is the
9031d10b 2702 this pointer.
98eaf693 2703 "Member functions do not encode the type of their implicit this
9031d10b 2704 parameter."
2705
7c8696b2 2706 Similarly, there's no need to mangle artificial parameters, like
2707 the VTT parameters for constructors and destructors. */
98eaf693 2708 if (method_p)
98eaf693 2709 {
7c8696b2 2710 parm_types = TREE_CHAIN (parm_types);
1767a056 2711 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
98eaf693 2712
7c8696b2 2713 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2714 {
2715 parm_types = TREE_CHAIN (parm_types);
1767a056 2716 parm_decl = DECL_CHAIN (parm_decl);
7c8696b2 2717 }
b4c5b883 2718
2719 if (decl && ctor_omit_inherited_parms (decl))
2720 /* Bring back parameters omitted from an inherited ctor. */
2721 parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl));
7c8696b2 2722 }
2723
9031d10b 2724 for (first_parm_type = parm_types;
2725 parm_types;
7c8696b2 2726 parm_types = TREE_CHAIN (parm_types))
2727 {
2728 tree parm = TREE_VALUE (parm_types);
e3cfe3ce 2729 if (parm == void_type_node)
98eaf693 2730 {
2731 /* "Empty parameter lists, whether declared as () or
2732 conventionally as (void), are encoded with a void parameter
2733 (v)." */
7c8696b2 2734 if (parm_types == first_parm_type)
98eaf693 2735 write_type (parm);
2736 /* If the parm list is terminated with a void type, it's
2737 fixed-length. */
2738 varargs_p = 0;
2739 /* A void type better be the last one. */
b4df430b 2740 gcc_assert (TREE_CHAIN (parm_types) == NULL);
98eaf693 2741 }
2742 else
2743 write_type (parm);
2744 }
2745
2746 if (varargs_p)
2747 /* <builtin-type> ::= z # ellipsis */
2748 write_char ('z');
2749}
2750
2751/* <class-enum-type> ::= <name> */
2752
9031d10b 2753static void
61e3d157 2754write_class_enum_type (const tree type)
98eaf693 2755{
bbb9efab 2756 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
98eaf693 2757}
2758
2759/* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2760 arguments.
2761
f59dc0de 2762 <template-args> ::= I <template-arg>* E */
98eaf693 2763
2764static void
61e3d157 2765write_template_args (tree args)
98eaf693 2766{
25f4fc1e 2767 int i;
f59dc0de 2768 int length = 0;
9031d10b 2769
98eaf693 2770 MANGLE_TRACE_TREE ("template-args", args);
2771
94302392 2772 write_char ('I');
98eaf693 2773
f59dc0de 2774 if (args)
2775 length = TREE_VEC_LENGTH (args);
94302392 2776
fbfe1b55 2777 if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
94302392 2778 {
25f4fc1e 2779 /* We have nested template args. We want the innermost template
2780 argument list. */
2781 args = TREE_VEC_ELT (args, length - 1);
2782 length = TREE_VEC_LENGTH (args);
98eaf693 2783 }
25f4fc1e 2784 for (i = 0; i < length; ++i)
2785 write_template_arg (TREE_VEC_ELT (args, i));
9031d10b 2786
98eaf693 2787 write_char ('E');
2788}
2789
4682b6fe 2790/* Write out the
2791 <unqualified-name>
2792 <unqualified-name> <template-args>
2793 part of SCOPE_REF or COMPONENT_REF mangling. */
2794
2795static void
2796write_member_name (tree member)
2797{
991449b2 2798 if (identifier_p (member))
322066d8 2799 {
991449b2 2800 if (abi_version_at_least (11) && IDENTIFIER_ANY_OP_P (member))
2801 {
2802 write_string ("on");
2803 if (abi_warn_or_compat_version_crosses (11))
2804 G.need_abi_warning = 1;
2805 }
2806 write_unqualified_id (member);
322066d8 2807 }
4682b6fe 2808 else if (DECL_P (member))
fb3127b9 2809 write_unqualified_name (member);
4682b6fe 2810 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2811 {
2812 tree name = TREE_OPERAND (member, 0);
6767ca9a 2813 name = OVL_FIRST (name);
4682b6fe 2814 write_member_name (name);
2815 write_template_args (TREE_OPERAND (member, 1));
2816 }
2817 else
2818 write_expression (member);
2819}
2820
98eaf693 2821/* <expression> ::= <unary operator-name> <expression>
2822 ::= <binary operator-name> <expression> <expression>
2823 ::= <expr-primary>
2824
2825 <expr-primary> ::= <template-param>
653e5405 2826 ::= L <type> <value number> E # literal
2827 ::= L <mangled-name> E # external name
34eac767 2828 ::= st <type> # sizeof
2829 ::= sr <type> <unqualified-name> # dependent name
653e5405 2830 ::= sr <type> <unqualified-name> <template-args> */
98eaf693 2831
2832static void
61e3d157 2833write_expression (tree expr)
98eaf693 2834{
3986b463 2835 enum tree_code code = TREE_CODE (expr);
98eaf693 2836
1ed9e78d 2837 if (TREE_CODE (expr) == TARGET_EXPR)
2838 {
2839 expr = TARGET_EXPR_INITIAL (expr);
2840 code = TREE_CODE (expr);
2841 }
2842
f0474a47 2843 /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer
2844 argument is converted (via qualification conversions) to another type. */
2845 while (CONVERT_EXPR_CODE_P (code)
d76863c8 2846 || location_wrapper_p (expr)
07850d16 2847 /* Parentheses aren't mangled. */
2848 || code == PAREN_EXPR
1ed9e78d 2849 || code == NON_LVALUE_EXPR
2850 || (code == VIEW_CONVERT_EXPR
2851 && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
b312a686 2852 {
2853 expr = TREE_OPERAND (expr, 0);
2854 code = TREE_CODE (expr);
2855 }
2856
8272c334 2857 if (code == BASELINK
2858 && (!type_unknown_p (expr)
2859 || !BASELINK_QUALIFIED_P (expr)))
0e5cde0c 2860 {
2861 expr = BASELINK_FUNCTIONS (expr);
2862 code = TREE_CODE (expr);
2863 }
2864
98eaf693 2865 /* Handle pointers-to-members by making them look like expression
2866 nodes. */
2867 if (code == PTRMEM_CST)
2868 {
2869 expr = build_nt (ADDR_EXPR,
fbb01da7 2870 build_qualified_name (/*type=*/NULL_TREE,
2871 PTRMEM_CST_CLASS (expr),
2872 PTRMEM_CST_MEMBER (expr),
2873 /*template_p=*/false));
98eaf693 2874 code = TREE_CODE (expr);
2875 }
2876
47cd6605 2877 /* Handle template parameters. */
9031d10b 2878 if (code == TEMPLATE_TYPE_PARM
98eaf693 2879 || code == TEMPLATE_TEMPLATE_PARM
1e93ca27 2880 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2881 || code == TEMPLATE_PARM_INDEX)
98eaf693 2882 write_template_param (expr);
2883 /* Handle literals. */
ce45a448 2884 else if (TREE_CODE_CLASS (code) == tcc_constant
f591db9a 2885 || code == CONST_DECL)
98eaf693 2886 write_template_arg_literal (expr);
56f83c40 2887 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2888 {
e34c848a 2889 gcc_assert (id_equal (DECL_NAME (expr), "this"));
56f83c40 2890 write_string ("fpT");
2891 }
34eac767 2892 else if (code == PARM_DECL)
2893 {
e92154af 2894 /* A function parameter used in a late-specified return type. */
32d008d9 2895 int index = DECL_PARM_INDEX (expr);
4d7aaf8e 2896 int level = DECL_PARM_LEVEL (expr);
2897 int delta = G.parm_depth - level + 1;
32d008d9 2898 gcc_assert (index >= 1);
4d7aaf8e 2899 write_char ('f');
2900 if (delta != 0)
2901 {
87d553db 2902 if (abi_version_at_least (5))
4d7aaf8e 2903 {
2904 /* Let L be the number of function prototype scopes from the
2905 innermost one (in which the parameter reference occurs) up
2906 to (and including) the one containing the declaration of
2907 the referenced parameter. If the parameter declaration
2908 clause of the innermost function prototype scope has been
2909 completely seen, it is not counted (in that case -- which
2910 is perhaps the most common -- L can be zero). */
2911 write_char ('L');
2912 write_unsigned_number (delta - 1);
2913 }
229a58b1 2914 if (abi_warn_or_compat_version_crosses (5))
4d7aaf8e 2915 G.need_abi_warning = true;
2916 }
2917 write_char ('p');
a8b75081 2918 write_compact_number (index - 1);
34eac767 2919 }
98eaf693 2920 else if (DECL_P (expr))
2921 {
2922 write_char ('L');
1da3f78a 2923 write_mangled_name (expr, false);
98eaf693 2924 write_char ('E');
2925 }
17fdf85c 2926 else if (TREE_CODE (expr) == SIZEOF_EXPR)
121296ee 2927 {
17fdf85c 2928 tree op = TREE_OPERAND (expr, 0);
2929
2930 if (PACK_EXPANSION_P (op))
2931 {
2932 if (abi_warn_or_compat_version_crosses (11))
2933 G.need_abi_warning = true;
2934 if (abi_version_at_least (11))
2935 {
2936 /* sZ rather than szDp. */
2937 write_string ("sZ");
2938 write_expression (PACK_EXPANSION_PATTERN (op));
2939 return;
2940 }
2941 }
2942
2943 if (SIZEOF_EXPR_TYPE_P (expr))
2944 {
2945 write_string ("st");
2946 write_type (TREE_TYPE (op));
2947 }
2948 else if (ARGUMENT_PACK_P (op))
2949 {
2950 tree args = ARGUMENT_PACK_ARGS (op);
2951 int length = TREE_VEC_LENGTH (args);
2952 if (abi_warn_or_compat_version_crosses (10))
2953 G.need_abi_warning = true;
2954 if (abi_version_at_least (10))
2955 {
2956 /* sP <template-arg>* E # sizeof...(T), size of a captured
2957 template parameter pack from an alias template */
2958 write_string ("sP");
2959 for (int i = 0; i < length; ++i)
2960 write_template_arg (TREE_VEC_ELT (args, i));
2961 write_char ('E');
2962 }
2963 else
2964 {
2965 /* In GCC 5 we represented this sizeof wrong, with the effect
2966 that we mangled it as the last element of the pack. */
2967 tree arg = TREE_VEC_ELT (args, length-1);
2968 if (TYPE_P (op))
2969 {
2970 write_string ("st");
2971 write_type (arg);
2972 }
2973 else
2974 {
2975 write_string ("sz");
2976 write_expression (arg);
2977 }
2978 }
2979 }
2980 else if (TYPE_P (TREE_OPERAND (expr, 0)))
2981 {
2982 write_string ("st");
2983 write_type (TREE_OPERAND (expr, 0));
2984 }
2985 else
2986 goto normal_expr;
74c75ba5 2987 }
e92154af 2988 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2989 && TYPE_P (TREE_OPERAND (expr, 0)))
2990 {
2991 write_string ("at");
2992 write_type (TREE_OPERAND (expr, 0));
2993 }
8272c334 2994 else if (code == SCOPE_REF
2995 || code == BASELINK)
94302392 2996 {
8272c334 2997 tree scope, member;
2998 if (code == SCOPE_REF)
2999 {
3000 scope = TREE_OPERAND (expr, 0);
3001 member = TREE_OPERAND (expr, 1);
af48cea2 3002 if (BASELINK_P (member))
3003 member = BASELINK_FUNCTIONS (member);
8272c334 3004 }
3005 else
3006 {
3007 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
3008 member = BASELINK_FUNCTIONS (expr);
3009 }
94302392 3010
3011 /* If the MEMBER is a real declaration, then the qualifying
3012 scope was not dependent. Ideally, we would not have a
3013 SCOPE_REF in those cases, but sometimes we do. If the second
3014 argument is a DECL, then the name must not have been
3015 dependent. */
f591db9a 3016 if (DECL_P (member))
94302392 3017 write_expression (member);
3018 else
3019 {
94302392 3020 write_string ("sr");
3021 write_type (scope);
c44a7393 3022 write_member_name (member);
94302392 3023 }
3024 }
86ccc124 3025 else if (INDIRECT_REF_P (expr)
e92154af 3026 && TREE_TYPE (TREE_OPERAND (expr, 0))
90ad495b 3027 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
e92154af 3028 {
3029 write_expression (TREE_OPERAND (expr, 0));
3030 }
694683bb 3031 else if (identifier_p (expr))
c44a7393 3032 {
3033 /* An operator name appearing as a dependent name needs to be
3034 specially marked to disambiguate between a use of the operator
3035 name and a use of the operator in an expression. */
991449b2 3036 if (IDENTIFIER_ANY_OP_P (expr))
c44a7393 3037 write_string ("on");
3038 write_unqualified_id (expr);
3039 }
3040 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
3041 {
3042 tree fn = TREE_OPERAND (expr, 0);
6767ca9a 3043 fn = OVL_NAME (fn);
991449b2 3044 if (IDENTIFIER_ANY_OP_P (fn))
c44a7393 3045 write_string ("on");
3046 write_unqualified_id (fn);
3047 write_template_args (TREE_OPERAND (expr, 1));
3048 }
cc5f60f5 3049 else if (TREE_CODE (expr) == MODOP_EXPR)
3050 {
3051 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
ca16a224 3052 const char *name = OVL_OP_INFO (true, subop)->mangled_name;
3053
cc5f60f5 3054 write_string (name);
3055 write_expression (TREE_OPERAND (expr, 0));
3056 write_expression (TREE_OPERAND (expr, 2));
3057 }
2d847c18 3058 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
3059 {
3060 /* ::= [gs] nw <expression>* _ <type> E
3061 ::= [gs] nw <expression>* _ <type> <initializer>
3062 ::= [gs] na <expression>* _ <type> E
3063 ::= [gs] na <expression>* _ <type> <initializer>
3064 <initializer> ::= pi <expression>* E */
3065 tree placement = TREE_OPERAND (expr, 0);
3066 tree type = TREE_OPERAND (expr, 1);
3067 tree nelts = TREE_OPERAND (expr, 2);
3068 tree init = TREE_OPERAND (expr, 3);
3069 tree t;
3070
3071 gcc_assert (code == NEW_EXPR);
3072 if (TREE_OPERAND (expr, 2))
3073 code = VEC_NEW_EXPR;
3074
3075 if (NEW_EXPR_USE_GLOBAL (expr))
3076 write_string ("gs");
3077
ca16a224 3078 write_string (OVL_OP_INFO (false, code)->mangled_name);
2d847c18 3079
3080 for (t = placement; t; t = TREE_CHAIN (t))
3081 write_expression (TREE_VALUE (t));
3082
3083 write_char ('_');
3084
3085 if (nelts)
3086 {
3087 tree domain;
3088 ++processing_template_decl;
3089 domain = compute_array_index_type (NULL_TREE, nelts,
3090 tf_warning_or_error);
3091 type = build_cplus_array_type (type, domain);
3092 --processing_template_decl;
3093 }
3094 write_type (type);
3095
3096 if (init && TREE_CODE (init) == TREE_LIST
8e8713cd 3097 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2d847c18 3098 write_expression (TREE_VALUE (init));
3099 else
3100 {
3101 if (init)
3102 write_string ("pi");
3ab4693e 3103 if (init && init != void_node)
2d847c18 3104 for (t = init; t; t = TREE_CHAIN (t))
3105 write_expression (TREE_VALUE (t));
3106 write_char ('E');
3107 }
3108 }
3109 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
3110 {
3111 gcc_assert (code == DELETE_EXPR);
3112 if (DELETE_EXPR_USE_VEC (expr))
3113 code = VEC_DELETE_EXPR;
3114
3115 if (DELETE_EXPR_USE_GLOBAL (expr))
3116 write_string ("gs");
3117
ca16a224 3118 write_string (OVL_OP_INFO (false, code)->mangled_name);
2d847c18 3119
3120 write_expression (TREE_OPERAND (expr, 0));
3121 }
3122 else if (code == THROW_EXPR)
3123 {
3124 tree op = TREE_OPERAND (expr, 0);
3125 if (op)
3126 {
3127 write_string ("tw");
3128 write_expression (op);
3129 }
3130 else
3131 write_string ("tr");
3132 }
3133 else if (code == CONSTRUCTOR)
3134 {
b11751dc 3135 bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr);
3136 tree etype = TREE_TYPE (expr);
2d847c18 3137
b11751dc 3138 if (braced_init)
2d847c18 3139 write_string ("il");
3140 else
3141 {
3142 write_string ("tl");
b11751dc 3143 write_type (etype);
3144 }
3145
3146 if (!initializer_zerop (expr) || !trivial_type_p (etype))
3147 {
3148 /* Convert braced initializer lists to STRING_CSTs so that
3149 A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
3150 still using the latter mangling for strings that
3151 originated as braced initializer lists. */
3152 expr = braced_lists_to_strings (etype, expr);
3153
3154 if (TREE_CODE (expr) == CONSTRUCTOR)
3155 {
3156 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
3157 unsigned last_nonzero = -1, i;
3158 tree val;
3159
3160 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
3161 if (!initializer_zerop (val))
3162 last_nonzero = i;
3163
3164 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
3165 {
3166 if (i > last_nonzero)
3167 break;
3168 write_expression (val);
3169 }
3170 }
3171 else
3172 {
3173 gcc_assert (TREE_CODE (expr) == STRING_CST);
3174 write_expression (expr);
3175 }
2d847c18 3176 }
2d847c18 3177 write_char ('E');
3178 }
9c093b14 3179 else if (code == LAMBDA_EXPR)
3180 {
3181 /* [temp.over.link] Two lambda-expressions are never considered
3182 equivalent.
3183
3184 So just use the closure type mangling. */
3185 write_string ("tl");
3186 write_type (LAMBDA_EXPR_CLOSURE (expr));
3187 write_char ('E');
3188 }
2d847c18 3189 else if (dependent_name (expr))
3190 {
3191 write_unqualified_id (dependent_name (expr));
3192 }
98eaf693 3193 else
3194 {
17fdf85c 3195 normal_expr:
e9b1be72 3196 int i, len;
e92154af 3197 const char *name;
98eaf693 3198
148e86d7 3199 /* When we bind a variable or function to a non-type template
3200 argument with reference type, we create an ADDR_EXPR to show
3201 the fact that the entity's address has been taken. But, we
3202 don't actually want to output a mangling code for the `&'. */
3203 if (TREE_CODE (expr) == ADDR_EXPR
3204 && TREE_TYPE (expr)
90ad495b 3205 && TYPE_REF_P (TREE_TYPE (expr)))
e75e76ea 3206 {
3207 expr = TREE_OPERAND (expr, 0);
3208 if (DECL_P (expr))
3209 {
3210 write_expression (expr);
3211 return;
3212 }
148e86d7 3213
e75e76ea 3214 code = TREE_CODE (expr);
3215 }
94302392 3216
59280ded 3217 if (code == COMPONENT_REF)
3218 {
3219 tree ob = TREE_OPERAND (expr, 0);
3220
3221 if (TREE_CODE (ob) == ARROW_EXPR)
3222 {
ca16a224 3223 write_string (OVL_OP_INFO (false, code)->mangled_name);
59280ded 3224 ob = TREE_OPERAND (ob, 0);
dc66dd88 3225 write_expression (ob);
59280ded 3226 }
dc66dd88 3227 else if (!is_dummy_object (ob))
3228 {
3229 write_string ("dt");
3230 write_expression (ob);
3231 }
3232 /* else, for a non-static data member with no associated object (in
3233 unevaluated context), use the unresolved-name mangling. */
59280ded 3234
59280ded 3235 write_member_name (TREE_OPERAND (expr, 1));
3236 return;
3237 }
3238
98eaf693 3239 /* If it wasn't any of those, recursively expand the expression. */
ca16a224 3240 name = OVL_OP_INFO (false, code)->mangled_name;
2d847c18 3241
3242 /* We used to mangle const_cast and static_cast like a C cast. */
74b777e5 3243 if (code == CONST_CAST_EXPR
3244 || code == STATIC_CAST_EXPR)
2d847c18 3245 {
229a58b1 3246 if (abi_warn_or_compat_version_crosses (6))
74b777e5 3247 G.need_abi_warning = 1;
3248 if (!abi_version_at_least (6))
ca16a224 3249 name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name;
2d847c18 3250 }
3251
e92154af 3252 if (name == NULL)
3253 {
c5d17c88 3254 switch (code)
3255 {
3256 case TRAIT_EXPR:
3257 error ("use of built-in trait %qE in function signature; "
3258 "use library traits instead", expr);
3259 break;
3260
3261 default:
3262 sorry ("mangling %C", code);
3263 break;
3264 }
e92154af 3265 return;
3266 }
3267 else
3268 write_string (name);
98eaf693 3269
ee95f490 3270 switch (code)
98eaf693 3271 {
653e5405 3272 case CALL_EXPR:
c6ac8f0e 3273 {
3274 tree fn = CALL_EXPR_FN (expr);
3275
3276 if (TREE_CODE (fn) == ADDR_EXPR)
3277 fn = TREE_OPERAND (fn, 0);
3278
3279 /* Mangle a dependent name as the name, not whatever happens to
3280 be the first function in the overload set. */
70a4616f 3281 if (OVL_P (fn)
c6ac8f0e 3282 && type_dependent_expression_p_push (expr))
6767ca9a 3283 fn = OVL_NAME (fn);
c6ac8f0e 3284
c44a7393 3285 write_expression (fn);
c6ac8f0e 3286 }
3287
34eac767 3288 for (i = 0; i < call_expr_nargs (expr); ++i)
3289 write_expression (CALL_EXPR_ARG (expr, i));
3290 write_char ('E');
653e5405 3291 break;
33df020e 3292
ee95f490 3293 case CAST_EXPR:
3294 write_type (TREE_TYPE (expr));
e92154af 3295 if (list_length (TREE_OPERAND (expr, 0)) == 1)
d6828f49 3296 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
e92154af 3297 else
3298 {
3299 tree args = TREE_OPERAND (expr, 0);
3300 write_char ('_');
3301 for (; args; args = TREE_CHAIN (args))
3302 write_expression (TREE_VALUE (args));
3303 write_char ('E');
3304 }
ee95f490 3305 break;
3306
2d847c18 3307 case DYNAMIC_CAST_EXPR:
3308 case REINTERPRET_CAST_EXPR:
ee95f490 3309 case STATIC_CAST_EXPR:
3310 case CONST_CAST_EXPR:
3311 write_type (TREE_TYPE (expr));
3312 write_expression (TREE_OPERAND (expr, 0));
3313 break;
3314
2d847c18 3315 case PREINCREMENT_EXPR:
3316 case PREDECREMENT_EXPR:
3317 if (abi_version_at_least (6))
3318 write_char ('_');
229a58b1 3319 if (abi_warn_or_compat_version_crosses (6))
2d847c18 3320 G.need_abi_warning = 1;
3321 /* Fall through. */
e92154af 3322
ee95f490 3323 default:
e9b1be72 3324 /* In the middle-end, some expressions have more operands than
3325 they do in templates (and mangling). */
d558fa9c 3326 len = cp_tree_operand_length (expr);
e9b1be72 3327
3328 for (i = 0; i < len; ++i)
716521b5 3329 {
3330 tree operand = TREE_OPERAND (expr, i);
fbb01da7 3331 /* As a GNU extension, the middle operand of a
716521b5 3332 conditional may be omitted. Since expression
3333 manglings are supposed to represent the input token
3334 stream, there's no good way to mangle such an
3335 expression without extending the C++ ABI. */
3336 if (code == COND_EXPR && i == 1 && !operand)
3337 {
1e5fcbe2 3338 error ("omitted middle operand to %<?:%> operand "
716521b5 3339 "cannot be mangled");
3340 continue;
3341 }
c09c7fa4 3342 else if (FOLD_EXPR_P (expr))
3343 {
3344 /* The first 'operand' of a fold-expression is the operator
3345 that it folds over. */
3346 if (i == 0)
3347 {
3348 int fcode = TREE_INT_CST_LOW (operand);
ca16a224 3349 write_string (OVL_OP_INFO (false, fcode)->mangled_name);
c09c7fa4 3350 continue;
3351 }
3352 else if (code == BINARY_LEFT_FOLD_EXPR)
3353 {
3354 /* The order of operands of the binary left and right
3355 folds is the same, but we want to mangle them in
3356 lexical order, i.e. non-pack first. */
3357 if (i == 1)
3358 operand = FOLD_EXPR_INIT (expr);
3359 else
3360 operand = FOLD_EXPR_PACK (expr);
3361 }
3362 if (PACK_EXPANSION_P (operand))
3363 operand = PACK_EXPANSION_PATTERN (operand);
3364 }
716521b5 3365 write_expression (operand);
3366 }
98eaf693 3367 }
98eaf693 3368 }
3369}
3370
9031d10b 3371/* Literal subcase of non-terminal <template-arg>.
98eaf693 3372
3373 "Literal arguments, e.g. "A<42L>", are encoded with their type
3374 and value. Negative integer values are preceded with "n"; for
3375 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
cc116129 3376 encoded as 0, true as 1." */
98eaf693 3377
3378static void
61e3d157 3379write_template_arg_literal (const tree value)
98eaf693 3380{
b11751dc 3381 if (TREE_CODE (value) == STRING_CST)
3382 /* Temporarily mangle strings as braced initializer lists. */
3383 write_string ("tl");
3384 else
3385 write_char ('L');
3386
3387 tree valtype = TREE_TYPE (value);
3388 write_type (valtype);
98eaf693 3389
b5cdaa0b 3390 /* Write a null member pointer value as (type)0, regardless of its
3391 real representation. */
3392 if (null_member_pointer_value_p (value))
3393 write_integer_cst (integer_zero_node);
3394 else
3395 switch (TREE_CODE (value))
3396 {
3397 case CONST_DECL:
3398 write_integer_cst (DECL_INITIAL (value));
3399 break;
9031d10b 3400
b5cdaa0b 3401 case INTEGER_CST:
3402 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3403 || integer_zerop (value) || integer_onep (value));
3404 write_integer_cst (value);
3405 break;
092b1d6f 3406
b5cdaa0b 3407 case REAL_CST:
3408 write_real_cst (value);
3409 break;
98eaf693 3410
193dc72f 3411 case COMPLEX_CST:
3412 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3413 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3414 {
3415 write_integer_cst (TREE_REALPART (value));
3416 write_char ('_');
3417 write_integer_cst (TREE_IMAGPART (value));
3418 }
3419 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3420 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3421 {
3422 write_real_cst (TREE_REALPART (value));
3423 write_char ('_');
3424 write_real_cst (TREE_IMAGPART (value));
3425 }
3426 else
3427 gcc_unreachable ();
3428 break;
3429
b5cdaa0b 3430 case STRING_CST:
b11751dc 3431 {
3432 /* Mangle strings the same as braced initializer lists. */
3433 unsigned n = TREE_STRING_LENGTH (value);
3434 const char *str = TREE_STRING_POINTER (value);
3435
3436 /* Count the number of trailing nuls and subtract them from
3437 STRSIZE because they don't need to be mangled. */
3438 for (const char *p = str + n - 1; ; --p)
3439 {
3440 if (*p || p == str)
3441 {
3442 n -= str + n - !!*p - p;
3443 break;
3444 }
3445 }
3446 tree eltype = TREE_TYPE (valtype);
3447 for (const char *p = str; n--; ++p)
3448 {
3449 write_char ('L');
3450 write_type (eltype);
3451 write_unsigned_number (*(const unsigned char*)p);
3452 write_string ("E");
3453 }
3454 break;
3455 }
99fddf6e 3456
b5cdaa0b 3457 default:
3458 gcc_unreachable ();
3459 }
9031d10b 3460
98eaf693 3461 write_char ('E');
3462}
3463
9031d10b 3464/* Non-terminal <template-arg>.
98eaf693 3465
653e5405 3466 <template-arg> ::= <type> # type
3467 ::= L <type> </value/ number> E # literal
3468 ::= LZ <name> E # external name
3469 ::= X <expression> E # expression */
98eaf693 3470
3471static void
61e3d157 3472write_template_arg (tree node)
98eaf693 3473{
3474 enum tree_code code = TREE_CODE (node);
3475
3476 MANGLE_TRACE_TREE ("template-arg", node);
3477
a5268b2f 3478 /* A template template parameter's argument list contains TREE_LIST
dfea972c 3479 nodes of which the value field is the actual argument. */
98eaf693 3480 if (code == TREE_LIST)
3481 {
3482 node = TREE_VALUE (node);
3483 /* If it's a decl, deal with its type instead. */
3484 if (DECL_P (node))
3485 {
3486 node = TREE_TYPE (node);
3487 code = TREE_CODE (node);
3488 }
3489 }
9031d10b 3490
95f798aa 3491 if (template_parm_object_p (node))
3492 /* We want to mangle the argument, not the var we stored it in. */
3493 node = DECL_INITIAL (node);
3494
44677b54 3495 /* Strip a conversion added by convert_nontype_argument. */
3496 if (TREE_CODE (node) == IMPLICIT_CONV_EXPR)
3497 node = TREE_OPERAND (node, 0);
80fe9b6a 3498 if (REFERENCE_REF_P (node))
3499 node = TREE_OPERAND (node, 0);
25f4fc1e 3500 if (TREE_CODE (node) == NOP_EXPR
90ad495b 3501 && TYPE_REF_P (TREE_TYPE (node)))
25f4fc1e 3502 {
3503 /* Template parameters can be of reference type. To maintain
3504 internal consistency, such arguments use a conversion from
3505 address of object to reference type. */
b4df430b 3506 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
f591db9a 3507 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
25f4fc1e 3508 }
98eaf693 3509
2d847c18 3510 if (TREE_CODE (node) == BASELINK
3511 && !type_unknown_p (node))
3512 {
3513 if (abi_version_at_least (6))
3514 node = BASELINK_FUNCTIONS (node);
229a58b1 3515 if (abi_warn_or_compat_version_crosses (6))
2d847c18 3516 /* We wrongly wrapped a class-scope function in X/E. */
3517 G.need_abi_warning = 1;
3518 }
3519
d95d815d 3520 if (ARGUMENT_PACK_P (node))
3521 {
3522 /* Expand the template argument pack. */
3523 tree args = ARGUMENT_PACK_ARGS (node);
3524 int i, length = TREE_VEC_LENGTH (args);
2d847c18 3525 if (abi_version_at_least (6))
3526 write_char ('J');
3527 else
74b777e5 3528 write_char ('I');
229a58b1 3529 if (abi_warn_or_compat_version_crosses (6))
74b777e5 3530 G.need_abi_warning = 1;
d95d815d 3531 for (i = 0; i < length; ++i)
3532 write_template_arg (TREE_VEC_ELT (args, i));
4682b6fe 3533 write_char ('E');
d95d815d 3534 }
3535 else if (TYPE_P (node))
98eaf693 3536 write_type (node);
3537 else if (code == TEMPLATE_DECL)
3538 /* A template appearing as a template arg is a template template arg. */
3539 write_template_template_arg (node);
ce45a448 3540 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
f591db9a 3541 || code == CONST_DECL
b5cdaa0b 3542 || null_member_pointer_value_p (node))
d5f9419c 3543 write_template_arg_literal (node);
98eaf693 3544 else if (DECL_P (node))
3545 {
3546 write_char ('L');
f10ff0e0 3547 /* Until ABI version 3, the underscore before the mangled name
3548 was incorrectly omitted. */
3549 if (!abi_version_at_least (3))
74b777e5 3550 write_char ('Z');
f10ff0e0 3551 else
3552 write_string ("_Z");
229a58b1 3553 if (abi_warn_or_compat_version_crosses (3))
74b777e5 3554 G.need_abi_warning = 1;
98eaf693 3555 write_encoding (node);
3556 write_char ('E');
3557 }
98eaf693 3558 else
3559 {
3560 /* Template arguments may be expressions. */
3561 write_char ('X');
3562 write_expression (node);
3563 write_char ('E');
3564 }
3565}
3566
3567/* <template-template-arg>
3568 ::= <name>
3569 ::= <substitution> */
3570
9140e457 3571static void
61e3d157 3572write_template_template_arg (const tree decl)
98eaf693 3573{
3574 MANGLE_TRACE_TREE ("template-template-arg", decl);
3575
3576 if (find_substitution (decl))
3577 return;
bbb9efab 3578 write_name (decl, /*ignore_local_scope=*/0);
98eaf693 3579 add_substitution (decl);
3580}
3581
3582
9031d10b 3583/* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
98eaf693 3584
9031d10b 3585 <array-type> ::= A [</dimension/ number>] _ </element/ type>
653e5405 3586 ::= A <expression> _ </element/ type>
98eaf693 3587
3588 "Array types encode the dimension (number of elements) and the
6b94e133 3589 element type. For variable length arrays, the dimension (but not
3590 the '_' separator) is omitted."
3591 Note that for flexible array members, like for other arrays of
3592 unspecified size, the dimension is also omitted. */
98eaf693 3593
61e3d157 3594static void
3595write_array_type (const tree type)
98eaf693 3596{
3597 write_char ('A');
3598 if (TYPE_DOMAIN (type))
3599 {
3600 tree index_type;
98eaf693 3601
3602 index_type = TYPE_DOMAIN (type);
6b94e133 3603 /* The INDEX_TYPE gives the upper and lower bounds of the array.
3604 It's null for flexible array members which have no upper bound
3605 (this is a change from GCC 5 and prior where such members were
3606 incorrectly mangled as zero-length arrays). */
3607 if (tree max = TYPE_MAX_VALUE (index_type))
f753592a 3608 {
6b94e133 3609 if (TREE_CODE (max) == INTEGER_CST)
3610 {
3611 /* The ABI specifies that we should mangle the number of
3612 elements in the array, not the largest allowed index. */
3613 offset_int wmax = wi::to_offset (max) + 1;
3614 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3615 number of elements as zero. */
3616 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3617 gcc_assert (wi::fits_uhwi_p (wmax));
3618 write_unsigned_number (wmax.to_uhwi ());
3619 }
3620 else
3621 {
3622 max = TREE_OPERAND (max, 0);
3623 write_expression (max);
3624 }
1cd6548d 3625 }
98eaf693 3626 }
3627 write_char ('_');
3628 write_type (TREE_TYPE (type));
3629}
3630
3631/* Non-terminal <pointer-to-member-type> for pointer-to-member
3632 variables. TYPE is a pointer-to-member POINTER_TYPE.
3633
3634 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3635
3636static void
61e3d157 3637write_pointer_to_member_type (const tree type)
98eaf693 3638{
3639 write_char ('M');
58274fe8 3640 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
98eaf693 3641 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3642}
3643
3644/* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
1e93ca27 3645 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3646 TEMPLATE_PARM_INDEX.
98eaf693 3647
ef4534a3 3648 <template-param> ::= T </parameter/ number> _ */
98eaf693 3649
3650static void
61e3d157 3651write_template_param (const tree parm)
98eaf693 3652{
3653 int parm_index;
3654
3655 MANGLE_TRACE_TREE ("template-parm", parm);
3656
3657 switch (TREE_CODE (parm))
3658 {
3659 case TEMPLATE_TYPE_PARM:
3660 case TEMPLATE_TEMPLATE_PARM:
1e93ca27 3661 case BOUND_TEMPLATE_TEMPLATE_PARM:
98eaf693 3662 parm_index = TEMPLATE_TYPE_IDX (parm);
3663 break;
3664
3665 case TEMPLATE_PARM_INDEX:
3666 parm_index = TEMPLATE_PARM_IDX (parm);
3667 break;
3668
3669 default:
092b1d6f 3670 gcc_unreachable ();
98eaf693 3671 }
3672
3673 write_char ('T');
3674 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3675 earliest template param denoted by `_'. */
a8b75081 3676 write_compact_number (parm_index);
98eaf693 3677}
3678
3679/* <template-template-param>
653e5405 3680 ::= <template-param>
98eaf693 3681 ::= <substitution> */
3682
3683static void
61e3d157 3684write_template_template_param (const tree parm)
98eaf693 3685{
607a5d68 3686 tree templ = NULL_TREE;
98eaf693 3687
3688 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3689 template template parameter. The substitution candidate here is
3690 only the template. */
1e93ca27 3691 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
98eaf693 3692 {
607a5d68 3693 templ
98eaf693 3694 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
607a5d68 3695 if (find_substitution (templ))
98eaf693 3696 return;
3697 }
3698
3699 /* <template-param> encodes only the template parameter position,
3700 not its template arguments, which is fine here. */
3701 write_template_param (parm);
607a5d68 3702 if (templ)
3703 add_substitution (templ);
98eaf693 3704}
3705
9031d10b 3706/* Non-terminal <substitution>.
98eaf693 3707
3708 <substitution> ::= S <seq-id> _
653e5405 3709 ::= S_ */
98eaf693 3710
3711static void
61e3d157 3712write_substitution (const int seq_id)
98eaf693 3713{
3714 MANGLE_TRACE ("substitution", "");
3715
3716 write_char ('S');
3717 if (seq_id > 0)
e75e76ea 3718 write_number (seq_id - 1, /*unsigned=*/1, 36);
98eaf693 3719 write_char ('_');
3720}
3721
18f2b9d5 3722/* Start mangling ENTITY. */
98eaf693 3723
3724static inline void
dfecde36 3725start_mangling (const tree entity)
98eaf693 3726{
18f2b9d5 3727 G.entity = entity;
3728 G.need_abi_warning = false;
40e2decb 3729 G.need_cxx17_warning = false;
dfecde36 3730 obstack_free (&name_obstack, name_base);
3731 mangle_obstack = &name_obstack;
3732 name_base = obstack_alloc (&name_obstack, 0);
98eaf693 3733}
3734
dfecde36 3735/* Done with mangling. If WARN is true, and the name of G.entity will
3736 be mangled differently in a future version of the ABI, issue a
3737 warning. */
98eaf693 3738
dfecde36 3739static void
74b777e5 3740finish_mangling_internal (void)
98eaf693 3741{
3742 /* Clear all the substitutions. */
f1f41a6c 3743 vec_safe_truncate (G.substitutions, 0);
98eaf693 3744
3745 /* Null-terminate the string. */
3746 write_char ('\0');
dfecde36 3747}
3748
98eaf693 3749
dfecde36 3750/* Like finish_mangling_internal, but return the mangled string. */
3751
3752static inline const char *
74b777e5 3753finish_mangling (void)
dfecde36 3754{
74b777e5 3755 finish_mangling_internal ();
f13b585c 3756 return (const char *) obstack_finish (mangle_obstack);
98eaf693 3757}
3758
dfecde36 3759/* Like finish_mangling_internal, but return an identifier. */
3760
3761static tree
74b777e5 3762finish_mangling_get_identifier (void)
dfecde36 3763{
74b777e5 3764 finish_mangling_internal ();
dfecde36 3765 /* Don't obstack_finish here, and the next start_mangling will
3766 remove the identifier. */
4cd09d28 3767 return get_identifier ((const char *) obstack_base (mangle_obstack));
dfecde36 3768}
3769
98eaf693 3770/* Initialize data structures for mangling. */
3771
3772void
eb32e911 3773init_mangle (void)
98eaf693 3774{
f13b585c 3775 gcc_obstack_init (&name_obstack);
3776 name_base = obstack_alloc (&name_obstack, 0);
f1f41a6c 3777 vec_alloc (G.substitutions, 0);
98eaf693 3778
3779 /* Cache these identifiers for quick comparison when checking for
3780 standard substitutions. */
3781 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3782 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3783 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3784 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3785 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3786 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3787}
3788
3789/* Generate the mangled name of DECL. */
3790
dfecde36 3791static tree
61e3d157 3792mangle_decl_string (const tree decl)
98eaf693 3793{
dfecde36 3794 tree result;
df63172d 3795 location_t saved_loc = input_location;
3796 tree saved_fn = NULL_TREE;
3797 bool template_p = false;
3798
97de3401 3799 /* We shouldn't be trying to mangle an uninstantiated template. */
3800 gcc_assert (!type_dependent_expression_p (decl));
3801
df63172d 3802 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3803 {
3804 struct tinst_level *tl = current_instantiation ();
f5cb81c6 3805 if ((!tl || tl->maybe_get_node () != decl)
6ba663d6 3806 && push_tinst_level (decl))
df63172d 3807 {
3808 template_p = true;
3809 saved_fn = current_function_decl;
df63172d 3810 current_function_decl = NULL_TREE;
3811 }
3812 }
3813 input_location = DECL_SOURCE_LOCATION (decl);
98eaf693 3814
dfecde36 3815 start_mangling (decl);
98eaf693 3816
3817 if (TREE_CODE (decl) == TYPE_DECL)
3818 write_type (TREE_TYPE (decl));
3819 else
1da3f78a 3820 write_mangled_name (decl, true);
9031d10b 3821
74b777e5 3822 result = finish_mangling_get_identifier ();
98eaf693 3823 if (DEBUG_MANGLE)
dfecde36 3824 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3825 IDENTIFIER_POINTER (result));
df63172d 3826
3827 if (template_p)
3828 {
3829 pop_tinst_level ();
3830 current_function_decl = saved_fn;
3831 }
3832 input_location = saved_loc;
3833
98eaf693 3834 return result;
3835}
3836
3e5c885b 3837/* Return an identifier for the external mangled name of DECL. */
3838
3839static tree
3840get_mangled_id (tree decl)
3841{
3842 tree id = mangle_decl_string (decl);
3843 return targetm.mangle_decl_assembler_name (decl, id);
3844}
3845
98eaf693 3846/* Create an identifier for the external mangled name of DECL. */
3847
7e64c604 3848void
61e3d157 3849mangle_decl (const tree decl)
98eaf693 3850{
71d3f205 3851 tree id;
3852 bool dep;
3853
3854 /* Don't bother mangling uninstantiated templates. */
3855 ++processing_template_decl;
3856 if (TREE_CODE (decl) == TYPE_DECL)
3857 dep = dependent_type_p (TREE_TYPE (decl));
3858 else
3859 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3860 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3861 --processing_template_decl;
3862 if (dep)
3863 return;
3864
960a5573 3865 /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
3866 It is not needed to assign names to anonymous namespace, but we use the
3867 "<anon>" marker to be able to tell if type is C++ ODR type or type
3868 produced by other language. */
3869 if (TREE_CODE (decl) == TYPE_DECL
3870 && TYPE_STUB_DECL (TREE_TYPE (decl))
3871 && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
3872 id = get_identifier ("<anon>");
3873 else
3874 {
3875 gcc_assert (TREE_CODE (decl) != TYPE_DECL
3876 || !no_linkage_check (TREE_TYPE (decl), true));
f67cece7 3877 if (abi_version_at_least (10))
3878 if (tree fn = decl_function_context (decl))
3879 maybe_check_abi_tags (fn, decl);
960a5573 3880 id = get_mangled_id (decl);
3881 }
97a424bc 3882 SET_DECL_ASSEMBLER_NAME (decl, id);
20009fe3 3883
40e2decb 3884 if (G.need_cxx17_warning
a08fc382 3885 && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl)))
957a727b 3886 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type,
2e9e9363 3887 "mangled name for %qD will change in C++17 because the "
3888 "exception specification is part of a function type",
3889 decl);
3890
6ad47359 3891 if (id != DECL_NAME (decl)
fb5491a0 3892 /* Don't do this for a fake symbol we aren't going to emit anyway. */
a1d8686f 3893 && TREE_CODE (decl) != TYPE_DECL
c6e04fca 3894 && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
20009fe3 3895 {
c6b4d6c1 3896 int save_ver = flag_abi_version;
3897 tree id2 = NULL_TREE;
6ad47359 3898
c6b4d6c1 3899 if (!DECL_REALLY_EXTERN (decl))
6ad47359 3900 {
e845448c 3901 record_mangling (decl, G.need_abi_warning);
6ad47359 3902
c6b4d6c1 3903 if (!G.need_abi_warning)
3904 return;
20009fe3 3905
c6b4d6c1 3906 flag_abi_version = flag_abi_compat_version;
3907 id2 = mangle_decl_string (decl);
3908 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3909 flag_abi_version = save_ver;
20009fe3 3910
c6b4d6c1 3911 if (id2 != id)
3912 note_mangling_alias (decl, id2);
3913 }
74b777e5 3914
3915 if (warn_abi)
3916 {
8c41abe8 3917 const char fabi_version[] = "-fabi-version";
3918
c6b4d6c1 3919 if (flag_abi_compat_version != warn_abi_version
3920 || id2 == NULL_TREE)
229a58b1 3921 {
3922 flag_abi_version = warn_abi_version;
3923 id2 = mangle_decl_string (decl);
3924 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3925 }
a5b4465c 3926 flag_abi_version = save_ver;
229a58b1 3927
3928 if (id2 == id)
3929 /* OK. */;
3930 else if (warn_abi_version != 0
3931 && abi_version_at_least (warn_abi_version))
66ed189d 3932 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
3933 "the mangled name of %qD changed between "
8c41abe8 3934 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
3935 G.entity, fabi_version, warn_abi_version, id2,
3936 fabi_version, save_ver, id);
74b777e5 3937 else
66ed189d 3938 warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
3939 "the mangled name of %qD changes between "
8c41abe8 3940 "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
3941 G.entity, fabi_version, save_ver, id,
3942 fabi_version, warn_abi_version, id2);
74b777e5 3943 }
3944
229a58b1 3945 flag_abi_version = save_ver;
a03f54f4 3946 }
98eaf693 3947}
3948
ec46af50 3949/* Generate the mangled representation of TYPE. */
98eaf693 3950
3951const char *
ec46af50 3952mangle_type_string (const tree type)
98eaf693 3953{
3954 const char *result;
3955
dfecde36 3956 start_mangling (type);
98eaf693 3957 write_type (type);
74b777e5 3958 result = finish_mangling ();
98eaf693 3959 if (DEBUG_MANGLE)
3960 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3961 return result;
3962}
3963
98eaf693 3964/* Create an identifier for the mangled name of a special component
3965 for belonging to TYPE. CODE is the ABI-specified code for this
3966 component. */
3967
3968static tree
61e3d157 3969mangle_special_for_type (const tree type, const char *code)
98eaf693 3970{
dfecde36 3971 tree result;
98eaf693 3972
3973 /* We don't have an actual decl here for the special component, so
3974 we can't just process the <encoded-name>. Instead, fake it. */
dfecde36 3975 start_mangling (type);
98eaf693 3976
3977 /* Start the mangling. */
3978 write_string ("_Z");
3979 write_string (code);
3980
3981 /* Add the type. */
3982 write_type (type);
74b777e5 3983 result = finish_mangling_get_identifier ();
98eaf693 3984
3985 if (DEBUG_MANGLE)
dfecde36 3986 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3987 IDENTIFIER_POINTER (result));
98eaf693 3988
dfecde36 3989 return result;
98eaf693 3990}
3991
3992/* Create an identifier for the mangled representation of the typeinfo
3993 structure for TYPE. */
3994
3995tree
61e3d157 3996mangle_typeinfo_for_type (const tree type)
98eaf693 3997{
3998 return mangle_special_for_type (type, "TI");
3999}
4000
4001/* Create an identifier for the mangled name of the NTBS containing
4002 the mangled name of TYPE. */
4003
4004tree
61e3d157 4005mangle_typeinfo_string_for_type (const tree type)
98eaf693 4006{
4007 return mangle_special_for_type (type, "TS");
4008}
4009
4010/* Create an identifier for the mangled name of the vtable for TYPE. */
4011
4012tree
61e3d157 4013mangle_vtbl_for_type (const tree type)
98eaf693 4014{
4015 return mangle_special_for_type (type, "TV");
4016}
4017
4018/* Returns an identifier for the mangled name of the VTT for TYPE. */
4019
4020tree
61e3d157 4021mangle_vtt_for_type (const tree type)
98eaf693 4022{
4023 return mangle_special_for_type (type, "TT");
4024}
4025
a6f4466c 4026/* Returns an identifier for the mangled name of the decomposition
4027 artificial variable DECL. DECLS is the vector of the VAR_DECLs
4028 for the identifier-list. */
4029
4030tree
4031mangle_decomp (const tree decl, vec<tree> &decls)
4032{
4033 gcc_assert (!type_dependent_expression_p (decl));
4034
4035 location_t saved_loc = input_location;
4036 input_location = DECL_SOURCE_LOCATION (decl);
4037
4038 start_mangling (decl);
4039 write_string ("_Z");
4040
4041 tree context = decl_mangling_context (decl);
4042 gcc_assert (context != NULL_TREE);
4043
4044 bool nested = false;
4045 if (DECL_NAMESPACE_STD_P (context))
4046 write_string ("St");
4047 else if (context != global_namespace)
4048 {
4049 nested = true;
4050 write_char ('N');
4051 write_prefix (decl_mangling_context (decl));
4052 }
4053
4054 write_string ("DC");
4055 unsigned int i;
4056 tree d;
4057 FOR_EACH_VEC_ELT (decls, i, d)
4058 write_unqualified_name (d);
4059 write_char ('E');
4060
4061 if (nested)
4062 write_char ('E');
4063
4064 tree id = finish_mangling_get_identifier ();
4065 if (DEBUG_MANGLE)
4066 fprintf (stderr, "mangle_decomp = '%s'\n\n",
4067 IDENTIFIER_POINTER (id));
4068
4069 input_location = saved_loc;
4070 return id;
4071}
4072
98eaf693 4073/* Return an identifier for a construction vtable group. TYPE is
4074 the most derived class in the hierarchy; BINFO is the base
9031d10b 4075 subobject for which this construction vtable group will be used.
98eaf693 4076
4077 This mangling isn't part of the ABI specification; in the ABI
4078 specification, the vtable group is dumped in the same COMDAT as the
4079 main vtable, and is referenced only from that vtable, so it doesn't
4080 need an external name. For binary formats without COMDAT sections,
9031d10b 4081 though, we need external names for the vtable groups.
98eaf693 4082
4083 We use the production
4084
4085 <special-name> ::= CT <type> <offset number> _ <base type> */
4086
4087tree
61e3d157 4088mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
98eaf693 4089{
dfecde36 4090 tree result;
98eaf693 4091
dfecde36 4092 start_mangling (type);
98eaf693 4093
4094 write_string ("_Z");
4095 write_string ("TC");
4096 write_type (type);
4097 write_integer_cst (BINFO_OFFSET (binfo));
4098 write_char ('_');
4099 write_type (BINFO_TYPE (binfo));
4100
74b777e5 4101 result = finish_mangling_get_identifier ();
98eaf693 4102 if (DEBUG_MANGLE)
dfecde36 4103 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
4104 IDENTIFIER_POINTER (result));
4105 return result;
98eaf693 4106}
4107
805e22b2 4108/* Mangle a this pointer or result pointer adjustment.
9031d10b 4109
805e22b2 4110 <call-offset> ::= h <fixed offset number> _
4111 ::= v <fixed offset number> _ <virtual offset number> _ */
9031d10b 4112
805e22b2 4113static void
61e3d157 4114mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
805e22b2 4115{
71b1859a 4116 write_char (virtual_offset ? 'v' : 'h');
98eaf693 4117
805e22b2 4118 /* For either flavor, write the fixed offset. */
4119 write_integer_cst (fixed_offset);
4120 write_char ('_');
4121
4122 /* For a virtual thunk, add the virtual offset. */
4123 if (virtual_offset)
4124 {
4125 write_integer_cst (virtual_offset);
4126 write_char ('_');
4127 }
4128}
4129
4130/* Return an identifier for the mangled name of a this-adjusting or
4131 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
4132 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
4133 is a virtual thunk, and it is the vtbl offset in
f1d555e3 4134 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
805e22b2 4135 zero for a covariant thunk. Note, that FN_DECL might be a covariant
4136 thunk itself. A covariant thunk name always includes the adjustment
4137 for the this pointer, even if there is none.
4138
4139 <special-name> ::= T <call-offset> <base encoding>
653e5405 4140 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
074ab442 4141 <base encoding> */
98eaf693 4142
4143tree
61e3d157 4144mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
a4a311c3 4145 tree virtual_offset, tree thunk)
98eaf693 4146{
dfecde36 4147 tree result;
9031d10b 4148
a4a311c3 4149 if (abi_version_at_least (11))
4150 maybe_check_abi_tags (fn_decl, thunk, 11);
4151
dfecde36 4152 start_mangling (fn_decl);
98eaf693 4153
4154 write_string ("_Z");
98eaf693 4155 write_char ('T');
9031d10b 4156
71b1859a 4157 if (!this_adjusting)
805e22b2 4158 {
4159 /* Covariant thunk with no this adjustment */
4160 write_char ('c');
4161 mangle_call_offset (integer_zero_node, NULL_TREE);
4162 mangle_call_offset (fixed_offset, virtual_offset);
4163 }
71b1859a 4164 else if (!DECL_THUNK_P (fn_decl))
4165 /* Plain this adjusting thunk. */
4166 mangle_call_offset (fixed_offset, virtual_offset);
98eaf693 4167 else
98eaf693 4168 {
805e22b2 4169 /* This adjusting thunk to covariant thunk. */
4170 write_char ('c');
4171 mangle_call_offset (fixed_offset, virtual_offset);
71b1859a 4172 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
4173 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
4174 if (virtual_offset)
4175 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
4176 mangle_call_offset (fixed_offset, virtual_offset);
4177 fn_decl = THUNK_TARGET (fn_decl);
98eaf693 4178 }
4179
4180 /* Scoped name. */
4181 write_encoding (fn_decl);
4182
74b777e5 4183 result = finish_mangling_get_identifier ();
98eaf693 4184 if (DEBUG_MANGLE)
dfecde36 4185 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
4186 return result;
98eaf693 4187}
4188
b2c95551 4189/* Handle ABI backwards compatibility for past bugs where we didn't call
4190 check_abi_tags in places where it's needed: call check_abi_tags and warn if
f67cece7 4191 it makes a difference. If FOR_DECL is non-null, it's the declaration
4192 that we're actually trying to mangle; if it's null, we're mangling the
4193 guard variable for T. */
b2c95551 4194
4195static void
a4a311c3 4196maybe_check_abi_tags (tree t, tree for_decl, int ver)
b2c95551 4197{
f67cece7 4198 if (DECL_ASSEMBLER_NAME_SET_P (t))
4199 return;
4200
b795d0cb 4201 tree oldtags = get_abi_tags (t);
b2c95551 4202
f67cece7 4203 mangle_decl (t);
b2c95551 4204
b795d0cb 4205 tree newtags = get_abi_tags (t);
4206 if (newtags && newtags != oldtags
a4a311c3 4207 && abi_version_crosses (ver))
f67cece7 4208 {
a4a311c3 4209 if (for_decl && DECL_THUNK_P (for_decl))
4210 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
4211 "the mangled name of a thunk for %qD changes between "
2f6d557f 4212 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
a4a311c3 4213 t, flag_abi_version, warn_abi_version);
4214 else if (for_decl)
f67cece7 4215 warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
4216 "the mangled name of %qD changes between "
2f6d557f 4217 "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
f67cece7 4218 for_decl, flag_abi_version, warn_abi_version);
4219 else
4220 warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
67af79be 4221 "the mangled name of the initialization guard variable "
2f6d557f 4222 "for %qD changes between %<-fabi-version=%d%> and "
4223 "%<-fabi-version=%d%>",
f67cece7 4224 t, flag_abi_version, warn_abi_version);
4225 }
b2c95551 4226}
4227
462819c8 4228/* Write out the appropriate string for this variable when generating
4229 another mangled name based on this one. */
98eaf693 4230
462819c8 4231static void
4232write_guarded_var_name (const tree variable)
98eaf693 4233{
580d697f 4234 if (DECL_NAME (variable)
4235 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
ca106ab1 4236 /* The name of a guard variable for a reference temporary should refer
4237 to the reference, not the temporary. */
4238 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
4239 else
f67cece7 4240 write_name (variable, /*ignore_local_scope=*/0);
462819c8 4241}
4242
4243/* Return an identifier for the name of an initialization guard
4244 variable for indicated VARIABLE. */
4245
4246tree
4247mangle_guard_variable (const tree variable)
4248{
f67cece7 4249 if (abi_version_at_least (10))
4250 maybe_check_abi_tags (variable);
462819c8 4251 start_mangling (variable);
4252 write_string ("_ZGV");
4253 write_guarded_var_name (variable);
74b777e5 4254 return finish_mangling_get_identifier ();
462819c8 4255}
4256
4257/* Return an identifier for the name of a thread_local initialization
4258 function for VARIABLE. */
4259
4260tree
4261mangle_tls_init_fn (const tree variable)
4262{
e1c52611 4263 check_abi_tags (variable);
462819c8 4264 start_mangling (variable);
4265 write_string ("_ZTH");
4266 write_guarded_var_name (variable);
74b777e5 4267 return finish_mangling_get_identifier ();
462819c8 4268}
4269
4270/* Return an identifier for the name of a thread_local wrapper
4271 function for VARIABLE. */
4272
4273#define TLS_WRAPPER_PREFIX "_ZTW"
4274
4275tree
4276mangle_tls_wrapper_fn (const tree variable)
4277{
e1c52611 4278 check_abi_tags (variable);
462819c8 4279 start_mangling (variable);
4280 write_string (TLS_WRAPPER_PREFIX);
4281 write_guarded_var_name (variable);
74b777e5 4282 return finish_mangling_get_identifier ();
98eaf693 4283}
47fc2356 4284
462819c8 4285/* Return true iff FN is a thread_local wrapper function. */
4286
4287bool
4288decl_tls_wrapper_p (const tree fn)
4289{
4290 if (TREE_CODE (fn) != FUNCTION_DECL)
4291 return false;
4292 tree name = DECL_NAME (fn);
4293 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
4294 strlen (TLS_WRAPPER_PREFIX)) == 0;
4295}
4296
ca106ab1 4297/* Return an identifier for the name of a temporary variable used to
8c89c5fc 4298 initialize a static reference. This is now part of the ABI. */
c7b89256 4299
ca106ab1 4300tree
61e3d157 4301mangle_ref_init_variable (const tree variable)
ca106ab1 4302{
dfecde36 4303 start_mangling (variable);
ca106ab1 4304 write_string ("_ZGR");
b2c95551 4305 check_abi_tags (variable);
ca106ab1 4306 write_name (variable, /*ignore_local_scope=*/0);
c7b89256 4307 /* Avoid name clashes with aggregate initialization of multiple
4308 references at once. */
8c89c5fc 4309 write_compact_number (current_ref_temp_count++);
74b777e5 4310 return finish_mangling_get_identifier ();
ca106ab1 4311}
95f798aa 4312
4313/* Return an identifier for the mangled name of a C++20 template parameter
4314 object for template argument EXPR. */
4315
4316tree
4317mangle_template_parm_object (tree expr)
4318{
4319 start_mangling (expr);
4320 write_string ("_ZTAX");
4321 write_expression (expr);
4322 write_char ('E');
4323 return finish_mangling_get_identifier ();
4324}
47fc2356 4325\f
b710ec85 4326/* Given a CLASS_TYPE, such as a record for std::bad_exception this
4327 function generates a mangled name for the vtable map variable of
4328 the class type. For example, if the class type is
4329 "std::bad_exception", the mangled name for the class is
4330 "St13bad_exception". This function would generate the name
4331 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
4332 "_VTV<std::bad_exception>::__vtable_map". */
4333
4334
4335char *
4336get_mangled_vtable_map_var_name (tree class_type)
4337{
4338 char *var_name = NULL;
4339 const char *prefix = "_ZN4_VTVI";
4340 const char *postfix = "E12__vtable_mapE";
4341
4342 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
4343
4344 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
68a98a70 4345
4346 if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
4347 {
4348 class_id = get_mangled_id (TYPE_NAME (class_type));
4349 vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
4350 }
4351
b710ec85 4352 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
4353 strlen (prefix) +
4354 strlen (postfix) + 1;
4355
4356 var_name = (char *) xmalloc (len);
4357
4358 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
4359
4360 return var_name;
4361}
4362
652c3635 4363#include "gt-cp-mangle.h"