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

source code of gcc/cp/mangle.cc