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