Debian Codegraph @

gcc/cp/mangle.cc

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