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 bool = false);
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 14507123 : abi_check (int ver)
285 : {
286 72363164 : if (abi_warn_or_compat_version_crosses (ver))
287 91499 : G.need_abi_warning = true;
288 14507123 : 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 1391568108 : maybe_template_info (const tree decl)
296 : {
297 1391568108 : 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 530696545 : const tree type = TREE_TYPE (decl);
302 :
303 530696545 : if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
304 421816443 : return TYPE_TEMPLATE_INFO (type);
305 : }
306 : else
307 : {
308 : /* Check if the template is a primary template. */
309 860871563 : if (DECL_LANG_SPECIFIC (decl) != NULL
310 859553684 : && VAR_OR_FUNCTION_DECL_P (decl)
311 731194461 : && DECL_TEMPLATE_INFO (decl)
312 1432942154 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
313 89174285 : 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 3252517 : write_exception_spec (tree spec)
365 : {
366 :
367 3252517 : if (!spec || spec == noexcept_false_spec)
368 : /* Nothing. */
369 : return;
370 :
371 42547 : if (!flag_noexcept_type)
372 : {
373 17 : G.need_cxx17_warning = true;
374 17 : return;
375 : }
376 :
377 42530 : if (spec == noexcept_true_spec || spec == empty_except_spec)
378 42515 : 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 4876748745 : canonicalize_for_substitution (tree node)
406 : {
407 : /* For a TYPE_DECL, use the type instead. */
408 4876748745 : if (TREE_CODE (node) == TYPE_DECL)
409 5104 : node = TREE_TYPE (node);
410 4876748745 : if (TYPE_P (node)
411 3584013952 : && TYPE_CANONICAL (node) != node
412 5281869018 : && TYPE_MAIN_VARIANT (node) != node)
413 : {
414 114782537 : 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 114782537 : 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 28258 : node = build_qualified_type (TYPE_MAIN_VARIANT (node),
422 28258 : TYPE_QUALS (node));
423 : else
424 114754279 : node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
425 : cp_type_quals (node));
426 114782537 : if (FUNC_OR_METHOD_TYPE_P (node))
427 : {
428 28267 : node = build_ref_qualified_type (node, type_memfn_rqual (orig));
429 28267 : tree r = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (orig));
430 28267 : if (flag_noexcept_type)
431 28223 : node = build_exception_variant (node, r);
432 : else
433 : /* Set the warning flag if appropriate. */
434 44 : write_exception_spec (r);
435 : }
436 : }
437 4876748745 : 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 1264625497 : add_substitution (tree node)
445 : {
446 1264625497 : tree c;
447 :
448 1264625497 : 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 1264625497 : c = canonicalize_for_substitution (node);
454 1264625497 : 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 1264625497 : node = c;
458 :
459 : /* Make sure NODE isn't already a candidate. */
460 1264625497 : if (flag_checking)
461 : {
462 : int i;
463 : tree candidate;
464 :
465 6566012830 : FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
466 5301387449 : if (candidate)
467 : {
468 5301379929 : gcc_assert (!(DECL_P (node) && node == candidate));
469 5301379929 : 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 1264625497 : vec_safe_push (G.substitutions, node);
476 :
477 1264625497 : if (DEBUG_MANGLE)
478 : dump_substitution_candidates ();
479 1264625497 : }
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 4280013160 : is_std_substitution (const tree node,
488 : const substitution_identifier_index_t index)
489 : {
490 4280013160 : tree type = NULL_TREE;
491 4280013160 : tree decl = NULL_TREE;
492 :
493 8535196724 : auto std_substitution_p = [&] (tree decl, tree type)
494 : {
495 4255183564 : if (!DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl)))
496 : return false;
497 :
498 1643544273 : if (!(TYPE_LANG_SPECIFIC (type) && TYPE_TEMPLATE_INFO (type)))
499 : return false;
500 :
501 1220533053 : tree tmpl = TYPE_TI_TEMPLATE (type);
502 1220533053 : if (DECL_NAME (tmpl) != subst_identifiers[index])
503 : return false;
504 :
505 145009597 : if (modules_p () && get_originating_module (tmpl, true) >= 0)
506 27 : return false;
507 :
508 : return true;
509 4280013160 : };
510 :
511 4280013160 : if (TREE_CODE (node) == TYPE_DECL || DECL_CLASS_TEMPLATE_P (node))
512 : {
513 3379967927 : type = TREE_TYPE (node);
514 3379967927 : decl = node;
515 : }
516 900045233 : else if (CLASS_TYPE_P (node))
517 : {
518 4755826 : type = node;
519 4755826 : 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 895289407 : 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 3384723753 : 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 1876567162 : get_abi_tags (tree t)
540 : {
541 1876567162 : if (!t || TREE_CODE (t) == NAMESPACE_DECL)
542 : return NULL_TREE;
543 :
544 1749065648 : if (DECL_P (t) && DECL_DECLARES_TYPE_P (t))
545 470611743 : t = TREE_TYPE (t);
546 :
547 1749065648 : if (TREE_CODE (t) == TEMPLATE_DECL && DECL_TEMPLATE_RESULT (t))
548 : {
549 17402517 : tree tags = get_abi_tags (DECL_TEMPLATE_RESULT (t));
550 : /* We used to overlook abi_tag on function and variable templates. */
551 17402517 : if (tags && abi_check (19))
552 10 : return tags;
553 : else
554 : return NULL_TREE;
555 : }
556 :
557 1731663131 : tree attrs;
558 1731663131 : if (TYPE_P (t))
559 1505431172 : attrs = TYPE_ATTRIBUTES (t);
560 : else
561 226231959 : attrs = DECL_ATTRIBUTES (t);
562 :
563 1731663131 : tree tags = lookup_attribute ("abi_tag", attrs);
564 1731663131 : if (tags)
565 11787517 : 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 6830801 : is_std_substitution_char (const tree node,
576 : const substitution_identifier_index_t index)
577 : {
578 6830801 : tree args;
579 : /* Check NODE's name is ::std::identifier. */
580 6830801 : if (!is_std_substitution (node, index))
581 : return 0;
582 : /* Figure out its template args. */
583 4302416 : if (DECL_P (node))
584 0 : args = DECL_TI_ARGS (node);
585 4302416 : else if (CLASS_TYPE_P (node))
586 4302416 : 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 4302416 : return
592 4302416 : TREE_VEC_LENGTH (args) == 1
593 4302416 : && 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 2329371494 : find_substitution (tree node)
632 : {
633 2329371494 : int i;
634 2329371494 : const int size = vec_safe_length (G.substitutions);
635 2329371494 : tree decl;
636 2329371494 : tree type;
637 2329371494 : const char *abbr = NULL;
638 :
639 2329371494 : 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 2329371494 : node = canonicalize_for_substitution (node);
646 :
647 : /* Check for builtin substitutions. */
648 :
649 2329371494 : decl = TYPE_P (node) ? TYPE_NAME (node) : node;
650 2329371494 : type = TYPE_P (node) ? node : TREE_TYPE (node);
651 :
652 : /* Check for std::allocator. */
653 2329371494 : if (decl
654 2162677710 : && is_std_substitution (decl, SUBID_ALLOCATOR)
655 2468996321 : && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
656 : abbr = "Sa";
657 :
658 : /* Check for std::basic_string. */
659 2265525307 : 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 2265369430 : else if (TYPE_P (node)
688 1504712381 : && cp_type_quals (type) == TYPE_UNQUALIFIED
689 1418424232 : && CLASS_TYPE_P (type)
690 597649700 : && CLASSTYPE_USE_TEMPLATE (type)
691 2705300692 : && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
692 : {
693 : /* First, check for the template
694 : args <char, std::char_traits<char> > . */
695 439931262 : tree args = CLASSTYPE_TI_ARGS (type);
696 439931262 : if (TREE_VEC_LENGTH (args) == 2
697 138141372 : && template_args_equal (TREE_VEC_ELT (args, 0), char_type_node)
698 446683032 : && is_std_substitution_char (TREE_VEC_ELT (args, 1),
699 : SUBID_CHAR_TRAITS))
700 : {
701 : /* Got them. Is this basic_istream? */
702 4223391 : if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
703 : abbr = "Si";
704 : /* Or basic_ostream? */
705 3919296 : else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
706 : abbr = "So";
707 : /* Or basic_iostream? */
708 3530439 : else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
709 2102878863 : abbr = "Sd";
710 : }
711 : }
712 :
713 : /* Check for namespace std. */
714 1825438168 : else if (decl && DECL_NAMESPACE_STD_P (decl))
715 : {
716 226492631 : write_string ("St");
717 226492631 : return 1;
718 : }
719 :
720 2102878863 : tree tags = NULL_TREE;
721 2102878863 : if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
722 1034819429 : tags = get_abi_tags (type);
723 : /* Now check the list of available substitutions for this mangling
724 : operation. */
725 2102878863 : if (!abbr || tags)
726 9133496000 : for (i = 0; i < size; ++i)
727 7274709498 : 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 7274699377 : if (decl == candidate
732 7212209347 : || (TYPE_P (candidate) && type && TYPE_P (node)
733 3182639990 : && same_type_p (type, candidate))
734 14371228841 : || NESTED_TEMPLATE_MATCH (node, candidate))
735 : {
736 179233110 : write_substitution (i);
737 179233110 : return 1;
738 : }
739 : }
740 :
741 1858786502 : if (!abbr)
742 : /* No substitution found. */
743 : return 0;
744 :
745 64859257 : write_string (abbr);
746 64859257 : 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 221149706 : unmangled_name_p (const tree decl)
761 : {
762 221149706 : if (TREE_CODE (decl) == FUNCTION_DECL)
763 : {
764 : /* The names of `extern "C"' functions are not mangled. */
765 187518516 : return (DECL_EXTERN_C_FUNCTION_P (decl)
766 : /* But overloaded operator names *are* mangled. */
767 2252176 : && !DECL_OVERLOADED_OPERATOR_P (decl));
768 : }
769 33631190 : else if (VAR_P (decl))
770 : {
771 : /* extern "C" declarations aren't mangled. */
772 33627807 : if (DECL_NAMESPACE_SCOPE_P (decl) && DECL_EXTERN_C_P (decl))
773 : return true;
774 :
775 : /* static variables are mangled. */
776 33420819 : if (!DECL_EXTERNAL_LINKAGE_P (decl))
777 : return false;
778 :
779 : /* Other variables at non-global scope are mangled. */
780 33201344 : if (CP_DECL_CONTEXT (decl) != global_namespace)
781 : return false;
782 :
783 : /* Variable template instantiations are mangled. */
784 125516 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
785 124029 : && variable_template_p (DECL_TI_TEMPLATE (decl)))
786 : return false;
787 :
788 : /* Declarations with ABI tags are mangled. */
789 114585 : if (get_abi_tags (decl))
790 : return false;
791 :
792 : // Declarations attached to a named module are mangled
793 114233 : if (modules_p () && get_originating_module (decl, true) >= 0)
794 121 : 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 221149706 : write_mangled_name (const tree decl, bool top_level)
811 : {
812 221149706 : MANGLE_TRACE_TREE ("mangled-name", decl);
813 :
814 221149706 : check_abi_tags (decl);
815 :
816 221149706 : if (unmangled_name_p (decl))
817 : {
818 2573273 : if (top_level)
819 2572986 : 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 218576433 : write_string ("_Z");
834 218576433 : write_encoding (decl);
835 : }
836 :
837 : /* If this is a coroutine helper, then append an appropriate string to
838 : identify which. */
839 221149706 : if (tree ramp = DECL_RAMP_FN (decl))
840 : {
841 3082 : if (DECL_ACTOR_FN (ramp) == decl)
842 1541 : write_string (JOIN_STR "actor");
843 1541 : else if (DECL_DESTROY_FN (ramp) == decl)
844 1541 : write_string (JOIN_STR "destroy");
845 : else
846 0 : gcc_unreachable ();
847 : }
848 221149706 : }
849 :
850 : /* Returns true if the return type of DECL is part of its signature, and
851 : therefore its mangling. */
852 :
853 : bool
854 375685744 : mangle_return_type_p (tree decl)
855 : {
856 375685744 : return (!DECL_CONSTRUCTOR_P (decl)
857 315057834 : && !DECL_DESTRUCTOR_P (decl)
858 300842289 : && !DECL_CONV_FN_P (decl)
859 673800400 : && maybe_template_info (decl));
860 : }
861 :
862 : /* <constraint-expression> ::= <expression> */
863 :
864 : static void
865 3282640 : 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 417293721 : 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 417293721 : if (constraints && abi_check (19))
888 : {
889 : tree probe = constraints;
890 : while (probe
891 344012 : && !cp_expr_location (probe)
892 381556 : && 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 343640 : if (probe && cp_expr_location (probe))
899 : {
900 306468 : write_char ('Q');
901 306468 : write_constraint_expression (probe);
902 : }
903 : }
904 417293721 : }
905 :
906 : /* <type-constraint> ::= <name> */
907 :
908 : static void
909 145465 : write_type_constraint (tree cnst)
910 : {
911 145465 : if (!cnst)
912 : return;
913 :
914 145465 : gcc_checking_assert (TREE_CODE (cnst) == TEMPLATE_ID_EXPR);
915 :
916 145465 : tree concept_decl = get_concept_check_template (cnst);
917 145465 : write_name (concept_decl);
918 145465 : tree args = TREE_OPERAND (cnst, 1);
919 145465 : if (TREE_VEC_LENGTH (args) > 1)
920 : {
921 52517 : TEMPLATE_ARGS_TYPE_CONSTRAINT_P (args) = true;
922 52517 : write_template_args (args);
923 : }
924 : }
925 :
926 : /* <encoding> ::= <function name> <bare-function-type>
927 : ::= <data name> */
928 :
929 : static void
930 224127765 : write_encoding (const tree decl)
931 : {
932 224127765 : MANGLE_TRACE_TREE ("encoding", decl);
933 :
934 224127765 : 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 11219 : if (DECL_OVERLOADED_OPERATOR_P (decl))
939 3 : write_name (decl);
940 : else
941 11216 : write_source_name (DECL_NAME (decl));
942 : return;
943 : }
944 :
945 224116546 : write_name (decl);
946 224116546 : if (TREE_CODE (decl) == FUNCTION_DECL)
947 : {
948 190806253 : tree fn_type;
949 190806253 : tree d;
950 :
951 190806253 : if (maybe_template_info (decl))
952 : {
953 17348479 : 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 17348479 : d = NULL_TREE;
959 : }
960 : else
961 : {
962 173457774 : fn_type = TREE_TYPE (decl);
963 173457774 : d = decl;
964 : }
965 :
966 190806253 : write_bare_function_type (fn_type,
967 190806253 : mangle_return_type_p (decl),
968 : d);
969 :
970 190806253 : if (tree c = get_trailing_function_requirements (decl))
971 2996058 : if (abi_check (19))
972 : {
973 2976166 : ++G.parm_depth;
974 2976166 : write_char ('Q');
975 2976166 : write_constraint_expression (c);
976 2976166 : --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 8604 : mangle_module_component (tree comp, bool partition_p)
992 : {
993 8604 : write_char ('W');
994 8604 : if (partition_p)
995 226 : write_char ('P');
996 8604 : write_source_name (comp);
997 :
998 : // Module substitutions use the same number-space as entity
999 : // substitutions, but are orthogonal.
1000 8604 : vec_safe_push (G.substitutions, NULL_TREE);
1001 8604 : 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 8347 : write_module (int m, bool include_partition)
1016 : {
1017 8347 : G.mod = true;
1018 0 : mangle_module (m, include_partition);
1019 0 : }
1020 :
1021 : static void
1022 1858493 : maybe_write_module (tree decl)
1023 : {
1024 1858493 : if (!DECL_NAMESPACE_SCOPE_P (decl))
1025 : return;
1026 :
1027 1325479 : if (!TREE_PUBLIC (STRIP_TEMPLATE (decl)))
1028 : return;
1029 :
1030 1316233 : if (TREE_CODE (decl) == NAMESPACE_DECL)
1031 : return;
1032 :
1033 999955 : int m = get_originating_module (decl, true);
1034 999955 : if (m >= 0)
1035 6242 : 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 2077445566 : decl_mangling_context (tree decl)
1043 : {
1044 2077445714 : tree tcontext = targetm.cxx.decl_mangling_context (decl);
1045 :
1046 2077445714 : if (tcontext != NULL_TREE)
1047 : return tcontext;
1048 :
1049 2077445714 : if (TREE_CODE (decl) == TEMPLATE_DECL
1050 2077445714 : && DECL_TEMPLATE_RESULT (decl))
1051 : decl = DECL_TEMPLATE_RESULT (decl);
1052 :
1053 2077445714 : if (TREE_CODE (decl) == TYPE_DECL
1054 3055750930 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
1055 : {
1056 6055858 : tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
1057 6055858 : 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 2071389856 : else if (template_type_parameter_p (decl))
1067 : /* template type parms have no mangling context. */
1068 : return NULL_TREE;
1069 :
1070 2071394891 : tcontext = CP_DECL_CONTEXT (decl);
1071 :
1072 2071394891 : if (member_like_constrained_friend_p (decl))
1073 90136 : tcontext = DECL_FRIEND_CONTEXT (decl);
1074 :
1075 : /* Ignore the artificial declare reduction functions. */
1076 2071394891 : if (tcontext
1077 2071394891 : && TREE_CODE (tcontext) == FUNCTION_DECL
1078 2077206255 : && 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 true, 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 553157936 : write_name (tree decl, const bool ignore_local_scope/*=false*/)
1097 : {
1098 553157936 : tree context;
1099 :
1100 553157936 : MANGLE_TRACE_TREE ("name", decl);
1101 :
1102 553157936 : 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 324519765 : decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
1107 : }
1108 :
1109 553157936 : context = decl_mangling_context (decl);
1110 :
1111 553157936 : gcc_assert (context != NULL_TREE);
1112 :
1113 2765308739 : 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 553157936 : if (context == global_namespace
1124 541472275 : || DECL_NAMESPACE_STD_P (context)
1125 322798981 : || (ignore_local_scope
1126 5069355 : && (TREE_CODE (context) == FUNCTION_DECL
1127 3485775 : || (abi_version_at_least (7)
1128 3485775 : && TREE_CODE (context) == PARM_DECL))))
1129 : {
1130 : /* Is this a template instance? */
1131 231942565 : if (tree info = maybe_template_info (decl))
1132 : {
1133 : /* Yes: use <unscoped-template-name>. */
1134 193250766 : 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 193250766 : tree parms = (TREE_CODE (decl) == FUNCTION_DECL
1139 198172823 : ? DECL_TEMPLATE_PARMS (TI_TEMPLATE (info))
1140 4922057 : : NULL_TREE);
1141 193250766 : write_template_args (TI_ARGS (info), parms);
1142 : }
1143 : else
1144 : /* Everything else gets an <unqualified-name>. */
1145 38691799 : 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 321215371 : 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 1013462051 : while (context != global_namespace)
1162 : {
1163 : /* Make sure we're always dealing with decls. */
1164 700801780 : if (TYPE_P (context))
1165 222061972 : context = TYPE_NAME (context);
1166 : /* Is this a function? */
1167 700801780 : if (TREE_CODE (context) == FUNCTION_DECL
1168 695732791 : || TREE_CODE (context) == PARM_DECL)
1169 : {
1170 : /* Yes, we have local scope. Use the <local-name>
1171 : production for the innermost function scope. */
1172 5069355 : write_local_name (context, local_entity, decl);
1173 5069355 : return;
1174 : }
1175 : /* Up one scope level. */
1176 695732425 : local_entity = context;
1177 695732425 : 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 316146016 : write_nested_name (decl);
1185 : }
1186 : }
1187 :
1188 : /* <unscoped-name> ::= <unqualified-name>
1189 : ::= St <unqualified-name> # ::std:: */
1190 :
1191 : static void
1192 165501133 : write_unscoped_name (const tree decl)
1193 : {
1194 165501133 : tree context = decl_mangling_context (decl);
1195 :
1196 165501133 : MANGLE_TRACE_TREE ("unscoped-name", decl);
1197 :
1198 : /* Is DECL in ::std? */
1199 165501133 : if (DECL_NAMESPACE_STD_P (context))
1200 : {
1201 155185525 : write_string ("St");
1202 155185525 : 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 10315608 : gcc_assert (context == global_namespace
1210 : || TREE_CODE (context) == PARM_DECL
1211 : || TREE_CODE (context) == FUNCTION_DECL);
1212 :
1213 10315608 : write_unqualified_name (decl);
1214 : }
1215 165501133 : }
1216 :
1217 : /* <unscoped-template-name> ::= <unscoped-name>
1218 : ::= <substitution> */
1219 :
1220 : static void
1221 193250766 : write_unscoped_template_name (const tree decl)
1222 : {
1223 193250766 : MANGLE_TRACE_TREE ("unscoped-template-name", decl);
1224 :
1225 193250766 : if (find_substitution (decl))
1226 : return;
1227 126809334 : write_unscoped_name (decl);
1228 126809334 : 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 318848083 : write_nested_name (const tree decl)
1242 : {
1243 318848083 : MANGLE_TRACE_TREE ("nested-name", decl);
1244 :
1245 318848083 : write_char ('N');
1246 :
1247 : /* Write CV-qualifiers, if this is an iobj member function. */
1248 318848083 : if (TREE_CODE (decl) == FUNCTION_DECL
1249 318848083 : && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
1250 : {
1251 150500159 : if (DECL_VOLATILE_MEMFUNC_P (decl))
1252 8807270 : write_char ('V');
1253 150500159 : if (DECL_CONST_MEMFUNC_P (decl))
1254 45526208 : write_char ('K');
1255 150500159 : if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
1256 : {
1257 155622 : if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
1258 97331 : write_char ('O');
1259 : else
1260 58291 : write_char ('R');
1261 : }
1262 : }
1263 139801742 : else if (DECL_DECLARES_FUNCTION_P (decl)
1264 168347924 : && DECL_XOBJ_MEMBER_FUNCTION_P (decl))
1265 5471 : write_char ('H');
1266 :
1267 : /* Is this a template instance? */
1268 318848083 : if (tree info = maybe_template_info (decl))
1269 : {
1270 : /* Yes, use <template-prefix>. */
1271 55395815 : write_template_prefix (decl);
1272 55395815 : write_template_args (TI_ARGS (info));
1273 : }
1274 263452268 : else if ((!abi_version_at_least (10) || TREE_CODE (decl) == TYPE_DECL)
1275 341534821 : && TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1276 : {
1277 2702064 : tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1278 2702064 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1279 : {
1280 50785 : write_template_prefix (decl);
1281 50785 : write_template_args (TREE_OPERAND (name, 1));
1282 : }
1283 : else
1284 : {
1285 2651279 : write_prefix (decl_mangling_context (decl));
1286 2651279 : write_unqualified_name (decl);
1287 : }
1288 : }
1289 : else
1290 : {
1291 : /* No, just use <prefix> */
1292 260750204 : write_prefix (decl_mangling_context (decl));
1293 260750204 : write_unqualified_name (decl);
1294 : }
1295 318848083 : write_char ('E');
1296 318848083 : }
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 656948511 : write_prefix (const tree node)
1308 : {
1309 656948511 : tree decl;
1310 :
1311 656948511 : if (node == NULL
1312 656948511 : || node == global_namespace)
1313 : return;
1314 :
1315 633375500 : MANGLE_TRACE_TREE ("prefix", node);
1316 :
1317 633375500 : if (TREE_CODE (node) == DECLTYPE_TYPE
1318 633375392 : || TREE_CODE (node) == TRAIT_TYPE)
1319 : {
1320 111 : write_type (node);
1321 111 : return;
1322 : }
1323 :
1324 633375389 : if (TREE_CODE (node) == SPLICE_SCOPE)
1325 : {
1326 10 : write_splice (node);
1327 10 : return;
1328 : }
1329 :
1330 633375379 : if (find_substitution (node))
1331 : return;
1332 :
1333 347701907 : tree template_info = NULL_TREE;
1334 347701907 : 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 131055884 : if (TREE_CODE (node) == FUNCTION_DECL
1342 127571113 : || TREE_CODE (node) == PARM_DECL)
1343 : return;
1344 :
1345 127570777 : decl = node;
1346 127570777 : template_info = maybe_template_info (decl);
1347 : }
1348 : else
1349 : {
1350 : /* Node is a type. */
1351 216646023 : decl = TYPE_NAME (node);
1352 : /* The DECL might not point at the node. */
1353 216646023 : if (CLASSTYPE_TEMPLATE_ID_P (node))
1354 163596903 : template_info = TYPE_TEMPLATE_INFO (node);
1355 : }
1356 :
1357 344216800 : if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1358 12438 : write_template_param (node);
1359 344204362 : else if (template_info)
1360 : /* Templated. */
1361 : {
1362 163639234 : write_template_prefix (decl);
1363 163639234 : write_template_args (TI_ARGS (template_info));
1364 : }
1365 180565128 : else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1366 : {
1367 136688 : tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1368 136688 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1369 : {
1370 118093 : write_template_prefix (decl);
1371 118093 : write_template_args (TREE_OPERAND (name, 1));
1372 : }
1373 : else
1374 : {
1375 18595 : write_prefix (decl_mangling_context (decl));
1376 18595 : write_unqualified_name (decl);
1377 : }
1378 : }
1379 : else
1380 : /* Not templated. */
1381 : {
1382 180428440 : write_prefix (decl_mangling_context (decl));
1383 180428440 : write_unqualified_name (decl);
1384 180428440 : if (VAR_P (decl)
1385 180428440 : || TREE_CODE (decl) == FIELD_DECL)
1386 : {
1387 : /* <data-member-prefix> := <member source-name> M */
1388 26885 : 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 26885 : if (!abi_check (18))
1394 : return;
1395 : }
1396 : }
1397 :
1398 344216668 : add_substitution (node);
1399 : }
1400 :
1401 : /* <template-prefix> ::= <prefix> <template component>
1402 : ::= <template-param>
1403 : ::= <substitution> */
1404 :
1405 : static void
1406 219203927 : write_template_prefix (const tree node)
1407 : {
1408 219203927 : tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1409 219203927 : tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1410 219203927 : tree context = decl_mangling_context (decl);
1411 219203927 : tree templ;
1412 219203927 : tree substitution;
1413 :
1414 219203927 : MANGLE_TRACE_TREE ("template-prefix", node);
1415 :
1416 : /* Find the template decl. */
1417 219203927 : if (tree info = maybe_template_info (decl))
1418 219034063 : templ = TI_TEMPLATE (info);
1419 169864 : else if (TREE_CODE (type) == TYPENAME_TYPE)
1420 : /* For a typename type, all we have is the name. */
1421 168878 : 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 219203927 : if (context && TYPE_P (context))
1452 11385145 : substitution = build_tree_list (context, templ);
1453 : else
1454 : substitution = templ;
1455 :
1456 219203927 : if (find_substitution (substitution))
1457 : return;
1458 :
1459 213100157 : if (TREE_TYPE (templ)
1460 213100157 : && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1461 986 : write_template_param (TREE_TYPE (templ));
1462 : else
1463 : {
1464 213099171 : write_prefix (context);
1465 213099171 : write_unqualified_name (decl);
1466 : }
1467 :
1468 213100157 : 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 5008172 : write_unqualified_id (tree identifier)
1507 : {
1508 5008172 : if (IDENTIFIER_CONV_OP_P (identifier))
1509 15 : write_conversion_operator_name (TREE_TYPE (identifier));
1510 5008157 : else if (IDENTIFIER_OVL_OP_P (identifier))
1511 : {
1512 53483 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (identifier);
1513 53483 : write_string (ovl_op->mangled_name);
1514 53483 : }
1515 4954674 : else if (UDLIT_OPER_P (identifier))
1516 0 : write_literal_operator_name (identifier);
1517 : else
1518 4954674 : write_source_name (identifier);
1519 5008172 : }
1520 :
1521 : static void
1522 822452898 : write_unqualified_name (tree decl)
1523 : {
1524 822452898 : MANGLE_TRACE_TREE ("unqualified-name", decl);
1525 :
1526 822452898 : if (modules_p ())
1527 1858493 : maybe_write_module (decl);
1528 :
1529 822452898 : if (identifier_p (decl))
1530 : {
1531 0 : write_unqualified_id (decl);
1532 0 : return;
1533 : }
1534 :
1535 822452898 : bool found = false;
1536 :
1537 822452898 : if (DECL_NAME (decl) == NULL_TREE
1538 822452898 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1539 21 : decl = anon_aggr_naming_decl (TREE_TYPE (decl));
1540 822452877 : else if (DECL_NAME (decl) == NULL_TREE)
1541 : {
1542 11625 : found = true;
1543 11625 : gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1544 11625 : write_source_name (DECL_ASSEMBLER_NAME (decl));
1545 : }
1546 822441252 : else if (DECL_DECLARES_FUNCTION_P (decl))
1547 : {
1548 190805717 : found = true;
1549 :
1550 : /* A constrained hidden friend is mangled like a member function, with
1551 : the name prefixed by 'F'. */
1552 190805717 : if (member_like_constrained_friend_p (decl))
1553 22534 : write_char ('F');
1554 :
1555 381611434 : if (DECL_CONSTRUCTOR_P (decl))
1556 30428354 : write_special_name_constructor (decl);
1557 160377363 : else if (DECL_DESTRUCTOR_P (decl))
1558 7346737 : write_special_name_destructor (decl);
1559 153030626 : else if (DECL_CONV_FN_P (decl))
1560 : {
1561 : /* Conversion operator. Handle it right here.
1562 : <operator> ::= cv <type> */
1563 2722356 : tree type;
1564 2722356 : if (maybe_template_info (decl))
1565 : {
1566 588 : tree fn_type;
1567 588 : fn_type = get_mostly_instantiated_function_type (decl);
1568 588 : type = TREE_TYPE (fn_type);
1569 : }
1570 2721768 : else if (FNDECL_USED_AUTO (decl))
1571 60 : type = DECL_SAVED_AUTO_RETURN_TYPE (decl);
1572 : else
1573 2721708 : type = DECL_CONV_FN_TYPE (decl);
1574 2722356 : write_conversion_operator_name (type);
1575 : }
1576 150308270 : else if (DECL_OVERLOADED_OPERATOR_P (decl))
1577 : {
1578 34452444 : tree t;
1579 34452444 : if (!(t = DECL_RAMP_FN (decl)))
1580 34452052 : t = decl;
1581 34452444 : const char *mangled_name
1582 34452444 : = (ovl_op_info[DECL_ASSIGNMENT_OPERATOR_P (t)]
1583 34452444 : [DECL_OVERLOADED_OPERATOR_CODE_RAW (t)].mangled_name);
1584 34452444 : write_string (mangled_name);
1585 : }
1586 115855826 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
1587 302835 : write_literal_operator_name (DECL_NAME (decl));
1588 : else
1589 : found = false;
1590 : }
1591 :
1592 75264372 : if (found)
1593 : /* OK */;
1594 747188547 : else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1595 128642 : && DECL_NAMESPACE_SCOPE_P (decl)
1596 747262289 : && decl_linkage (decl) == lk_internal)
1597 : {
1598 62363 : MANGLE_TRACE_TREE ("local-source-name", decl);
1599 62363 : write_char ('L');
1600 62363 : 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 747126184 : tree type = TREE_TYPE (decl);
1607 :
1608 747126184 : if (TREE_CODE (decl) == TYPE_DECL
1609 747126184 : && enum_with_enumerator_for_linkage_p (type))
1610 20 : write_unnamed_enum_name (type);
1611 1109840165 : else if (TREE_CODE (decl) == TYPE_DECL && TYPE_UNNAMED_P (type))
1612 1553 : write_unnamed_type_name (type);
1613 1083207455 : else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (type))
1614 2359463 : write_closure_type_name (type);
1615 : else
1616 744765148 : 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 822452898 : if (tree tmpl = most_general_template (decl))
1625 : {
1626 483220949 : tree res = DECL_TEMPLATE_RESULT (tmpl);
1627 483220949 : if (res == NULL_TREE)
1628 : /* UNBOUND_CLASS_TEMPLATE. */;
1629 483220946 : else if (DECL_DECLARES_TYPE_P (decl))
1630 : decl = res;
1631 167435074 : 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 822452826 : tree tags = get_abi_tags (decl);
1664 185883902 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CONV_FN_P (decl)
1665 825175182 : && 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 822452826 : write_abi_tags (tags);
1672 : }
1673 :
1674 : /* Write the unqualified-name for a conversion operator to TYPE. */
1675 :
1676 : static void
1677 2722371 : write_conversion_operator_name (const tree type)
1678 : {
1679 2722371 : write_string ("cv");
1680 2722371 : write_type (type);
1681 2722371 : }
1682 :
1683 : /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1684 :
1685 : <source-name> ::= </length/ number> <identifier> */
1686 :
1687 : static void
1688 749814327 : write_source_name (tree identifier)
1689 : {
1690 749814327 : MANGLE_TRACE_TREE ("source-name", identifier);
1691 :
1692 749814327 : write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1693 749814327 : write_identifier (IDENTIFIER_POINTER (identifier));
1694 749814327 : }
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 501722 : sorted_abi_tags (tree tags, bool ignore_inherited_p)
1713 : {
1714 501722 : vec<tree, va_gc> * vec = make_tree_vector();
1715 :
1716 840542 : for (tree t = tags; t; t = TREE_CHAIN (t))
1717 : {
1718 338820 : if (ABI_TAG_NOT_MANGLED (t)
1719 338820 : || (ignore_inherited_p && ABI_TAG_INHERITED (t)))
1720 6 : continue;
1721 338814 : tree str = TREE_VALUE (t);
1722 338814 : vec_safe_push (vec, str);
1723 : }
1724 :
1725 501722 : vec->qsort (tree_string_cmp);
1726 :
1727 501722 : 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 822452934 : write_abi_tags (tree tags)
1735 : {
1736 822452934 : if (tags == NULL_TREE)
1737 822452934 : return;
1738 :
1739 338688 : vec<tree, va_gc> * vec = sorted_abi_tags (tags, /*ignore_inherited_p=*/false);
1740 :
1741 338688 : unsigned i; tree str;
1742 1016091 : FOR_EACH_VEC_ELT (*vec, i, str)
1743 : {
1744 338715 : write_string ("B");
1745 338715 : write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1746 338715 : write_identifier (TREE_STRING_POINTER (str));
1747 : }
1748 :
1749 338688 : 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 302835 : write_literal_operator_name (tree identifier)
1776 : {
1777 302835 : const char* suffix = UDLIT_OP_SUFFIX (identifier);
1778 302835 : write_identifier (UDLIT_OP_MANGLED_PREFIX);
1779 302835 : write_unsigned_number (strlen (suffix));
1780 302835 : write_identifier (suffix);
1781 302835 : }
1782 :
1783 : /* Encode 0 as _, and 1+ as n-1_. */
1784 :
1785 : static void
1786 31736529 : write_compact_number (int num)
1787 : {
1788 31736529 : gcc_checking_assert (num >= 0);
1789 31736529 : if (num > 0)
1790 12231428 : write_unsigned_number (num - 1);
1791 31736529 : write_char ('_');
1792 31736529 : }
1793 :
1794 : /* Return how many unnamed types precede TYPE in its enclosing class. */
1795 :
1796 : static int
1797 839 : nested_anon_class_index (tree type)
1798 : {
1799 839 : int index = 0;
1800 839 : tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1801 22181 : for (; member; member = DECL_CHAIN (member))
1802 22181 : if (DECL_IMPLICIT_TYPEDEF_P (member))
1803 : {
1804 1299 : tree memtype = TREE_TYPE (member);
1805 1299 : 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 1553 : write_unnamed_type_name (const tree type)
1821 : {
1822 1553 : int discriminator;
1823 1553 : MANGLE_TRACE_TREE ("unnamed-type-name", type);
1824 :
1825 1553 : if (TYPE_FUNCTION_SCOPE_P (type))
1826 358 : discriminator = discriminator_for_local_entity (TYPE_NAME (type));
1827 1195 : else if (TYPE_CLASS_SCOPE_P (type))
1828 839 : discriminator = nested_anon_class_index (type);
1829 : else
1830 : {
1831 356 : gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1832 : /* Just use the old mangling at namespace scope. */
1833 356 : write_source_name (TYPE_IDENTIFIER (type));
1834 356 : return;
1835 : }
1836 :
1837 1197 : write_string ("Ut");
1838 1197 : 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 8522276 : template_parm_natural_p (tree arg, tree parm)
1857 : {
1858 8522276 : tree decl = TREE_VALUE (parm);
1859 :
1860 : /* A template parameter is "natural" if: */
1861 :
1862 8522276 : if (template_parameter_pack_p (decl))
1863 : {
1864 833783 : tree args = ARGUMENT_PACK_ARGS (arg);
1865 833783 : 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 652656 : 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 8341149 : if (TREE_CODE (decl) == TEMPLATE_DECL)
1888 9214 : return template_heads_equivalent_p (arg, decl);
1889 :
1890 : /* the argument is a type and the parameter is unconstrained; or */
1891 8331935 : else if (TREE_CODE (decl) == TYPE_DECL)
1892 7308627 : 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 1023308 : 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 1023308 : 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 119745 : write_template_param_decl (tree parm)
1920 : {
1921 119745 : tree decl = TREE_VALUE (parm);
1922 :
1923 119745 : if (template_parameter_pack_p (decl))
1924 44629 : write_string ("Tp");
1925 :
1926 119745 : switch (TREE_CODE (decl))
1927 : {
1928 48253 : case PARM_DECL:
1929 48253 : {
1930 48253 : write_string ("Tn");
1931 :
1932 48253 : 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 48253 : if (tree c = (is_auto (type)
1936 52734 : ? TEMPLATE_PARM_CONSTRAINTS (parm)
1937 4481 : : 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 48231 : write_type (type);
1952 : }
1953 : break;
1954 :
1955 355 : case TEMPLATE_DECL:
1956 355 : {
1957 355 : write_string ("Tt");
1958 355 : tree parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
1959 824 : for (tree node : tree_vec_range (parms))
1960 469 : write_template_param_decl (node);
1961 355 : write_char ('E');
1962 : }
1963 355 : break;
1964 :
1965 71137 : case TYPE_DECL:
1966 71137 : if (tree c = TEMPLATE_PARM_CONSTRAINTS (parm))
1967 : {
1968 37519 : if (TREE_CODE (c) == UNARY_LEFT_FOLD_EXPR)
1969 : {
1970 84 : c = FOLD_EXPR_PACK (c);
1971 84 : c = PACK_EXPANSION_PATTERN (c);
1972 : }
1973 37519 : if (TREE_CODE (decl) == TYPE_DECL)
1974 : {
1975 37519 : write_string ("Tk");
1976 37519 : write_type_constraint (c);
1977 : }
1978 : }
1979 : else
1980 33618 : write_string ("Ty");
1981 : break;
1982 :
1983 0 : default:
1984 0 : gcc_unreachable ();
1985 : }
1986 119745 : }
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 524132 : write_closure_template_head (tree tmpl)
1994 : {
1995 524132 : bool any = false;
1996 :
1997 : // We only need one level of template parms
1998 524132 : tree parms = DECL_TEMPLATE_PARMS (tmpl);
1999 524132 : tree inner = INNERMOST_TEMPLATE_PARMS (parms);
2000 :
2001 601497 : for (int ix = 0, len = TREE_VEC_LENGTH (inner); ix != len; ix++)
2002 : {
2003 542520 : tree parm = TREE_VEC_ELT (inner, ix);
2004 542520 : if (parm == error_mark_node)
2005 0 : continue;
2006 :
2007 542520 : if (DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (parm)))
2008 : // A synthetic parm, we're done.
2009 : break;
2010 :
2011 77365 : any = true;
2012 77365 : if (abi_version_at_least (18))
2013 77125 : write_template_param_decl (parm);
2014 : }
2015 :
2016 524132 : write_tparms_constraints (TEMPLATE_PARMS_CONSTRAINTS (parms));
2017 :
2018 524132 : 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 2359463 : write_closure_type_name (const tree type)
2026 : {
2027 2359463 : tree fn = lambda_function (type);
2028 2359463 : tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
2029 2359463 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
2030 :
2031 2359463 : MANGLE_TRACE_TREE ("closure-type-name", type);
2032 :
2033 2359463 : write_string ("Ul");
2034 :
2035 2359463 : if (auto ti = maybe_template_info (fn))
2036 524132 : 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 380385 : if (abi_warn_or_compat_version_crosses (18))
2040 198 : G.need_abi_warning = true;
2041 :
2042 2359463 : write_method_parms (parms, TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE, fn);
2043 2359463 : write_char ('E');
2044 2359463 : if ((LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
2045 2359463 : != LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda))
2046 3209543 : && abi_warn_or_compat_version_crosses (18))
2047 288 : G.need_abi_warning = true;
2048 4718926 : write_compact_number (abi_version_at_least (18)
2049 2358941 : ? LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda)
2050 522 : : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda));
2051 2359463 : }
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 1014906177 : hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
2061 : char *buffer, const unsigned int min_digits)
2062 : {
2063 1014906177 : static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2064 1014906177 : unsigned digits = 0;
2065 :
2066 2525619578 : while (number)
2067 : {
2068 1510713401 : unsigned HOST_WIDE_INT d = number / base;
2069 :
2070 1510713401 : *--buffer = base_digits[number - d * base];
2071 1510713401 : digits++;
2072 1510713401 : number = d;
2073 : }
2074 1068912099 : while (digits < min_digits)
2075 : {
2076 54005922 : *--buffer = base_digits[0];
2077 54005922 : digits++;
2078 : }
2079 1014906177 : return digits;
2080 : }
2081 :
2082 : /* Non-terminal <number>.
2083 :
2084 : <number> ::= [n] </decimal integer/> */
2085 :
2086 : static void
2087 1014905877 : write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
2088 : const unsigned int base)
2089 : {
2090 1014905877 : char buffer[sizeof (HOST_WIDE_INT) * 8];
2091 1014905877 : unsigned count = 0;
2092 :
2093 1014905877 : if (!unsigned_p && (HOST_WIDE_INT) number < 0)
2094 : {
2095 0 : write_char ('n');
2096 0 : number = -((HOST_WIDE_INT) number);
2097 : }
2098 1014905877 : count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
2099 1014905877 : write_chars (buffer + sizeof (buffer) - count, count);
2100 1014905877 : }
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 95787010 : write_integer_cst (const tree cst)
2108 : {
2109 95787010 : int sign = tree_int_cst_sgn (cst);
2110 95787010 : widest_int abs_value = wi::abs (wi::to_widest (cst));
2111 95787010 : 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 95786910 : if (sign < 0)
2165 487083 : write_char ('n');
2166 95786910 : write_unsigned_number (abs_value.to_uhwi ());
2167 : }
2168 95787010 : }
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 751639603 : write_identifier (const char *identifier)
2263 : {
2264 751639603 : MANGLE_TRACE ("identifier", identifier);
2265 751639603 : write_string (identifier);
2266 751639603 : }
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 30428354 : write_special_name_constructor (const tree ctor)
2279 : {
2280 30428354 : write_char ('C');
2281 30428354 : bool new_inh = (flag_new_inheriting_ctors
2282 60840981 : && DECL_INHERITED_CTOR (ctor));
2283 161700 : if (new_inh)
2284 161700 : write_char ('I');
2285 30428354 : if (DECL_BASE_CONSTRUCTOR_P (ctor))
2286 6279248 : 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 24149106 : else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
2291 17872873 : write_char ('4');
2292 : else
2293 : {
2294 6276233 : gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
2295 6276233 : write_char ('1');
2296 : }
2297 30428354 : if (new_inh)
2298 323400 : write_type (DECL_INHERITED_CTOR_BASE (ctor));
2299 30428354 : }
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 7346737 : write_special_name_destructor (const tree dtor)
2310 : {
2311 7346737 : if (DECL_DELETING_DESTRUCTOR_P (dtor))
2312 618661 : write_string ("D0");
2313 6728076 : else if (DECL_BASE_DESTRUCTOR_P (dtor))
2314 1603965 : write_string ("D2");
2315 5124111 : 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 2925872 : write_string ("D4");
2320 : else
2321 : {
2322 2198239 : gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
2323 2198239 : write_string ("D1");
2324 : }
2325 7346737 : }
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 2782099 : discriminator_for_local_entity (tree entity)
2333 : {
2334 2782099 : if (!DECL_LANG_SPECIFIC (entity))
2335 : {
2336 : /* Some decls, like __FUNCTION__, don't need a discriminator. */
2337 33222 : gcc_checking_assert (DECL_ARTIFICIAL (entity));
2338 : return 0;
2339 : }
2340 5353119 : 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 2781741 : write_discriminator (const int discriminator)
2369 : {
2370 : /* If discriminator is zero, don't write anything. Otherwise... */
2371 2781741 : 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 2781741 : }
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 5069355 : write_local_name (tree function, const tree local_entity,
2397 : const tree entity)
2398 : {
2399 5069355 : tree parm = NULL_TREE;
2400 :
2401 5069355 : MANGLE_TRACE_TREE ("local-name", entity);
2402 :
2403 5069355 : if (TREE_CODE (function) == PARM_DECL)
2404 : {
2405 366 : parm = function;
2406 366 : function = DECL_CONTEXT (parm);
2407 : }
2408 :
2409 5069355 : write_char ('Z');
2410 5069355 : write_encoding (function);
2411 5069355 : write_char ('E');
2412 :
2413 : /* For this purpose, parameters are numbered from right-to-left. */
2414 5069355 : 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 5069355 : 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 5069355 : write_name (entity, /*ignore_local_scope=*/true);
2433 5069355 : if (DECL_DISCRIMINATOR_P (local_entity)
2434 15029535 : && !(TREE_CODE (local_entity) == TYPE_DECL
2435 14673888 : && TYPE_ANON_P (TREE_TYPE (local_entity))))
2436 2781472 : write_discriminator (discriminator_for_local_entity (local_entity));
2437 : }
2438 5069355 : }
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 1282751760 : 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 1282751769 : int is_builtin_type = 0;
2474 :
2475 1282751769 : MANGLE_TRACE_TREE ("type", type);
2476 :
2477 1282751769 : if (type == error_mark_node)
2478 : return;
2479 :
2480 1282751754 : type = canonicalize_for_substitution (type);
2481 1282751754 : if (find_substitution (type))
2482 : return;
2483 :
2484 :
2485 1170468244 : 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 82533288 : tree t = TYPE_MAIN_VARIANT (type);
2492 82533288 : 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 82533288 : gcc_assert (t != type);
2501 82533288 : 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 82526563 : write_type (t);
2520 : }
2521 1087934956 : 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 1568479 : write_array_type (type);
2526 : else
2527 : {
2528 1086366477 : tree type_orig = type;
2529 :
2530 : /* See through any typedefs. */
2531 1086366477 : type = TYPE_MAIN_VARIANT (type);
2532 1086366477 : if (FUNC_OR_METHOD_TYPE_P (type))
2533 3245772 : 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 1086366477 : if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
2539 16206 : type = TREE_TYPE (first_field (type));
2540 :
2541 1086366477 : if (TYPE_PTRDATAMEM_P (type))
2542 17088 : write_pointer_to_member_type (type);
2543 : else
2544 : {
2545 : /* Handle any target-specific fundamental types. */
2546 1086349389 : const char *target_mangling
2547 1086349389 : = targetm.mangle_type (type_orig);
2548 :
2549 1086349389 : if (target_mangling)
2550 : {
2551 9754772 : write_string (target_mangling);
2552 : /* Add substitutions for types other than fundamental
2553 : types. */
2554 9754772 : 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 1076594617 : switch (TREE_CODE (type))
2563 : {
2564 579014513 : case VOID_TYPE:
2565 579014513 : case BOOLEAN_TYPE:
2566 579014513 : case INTEGER_TYPE: /* Includes wchar_t. */
2567 579014513 : case REAL_TYPE:
2568 579014513 : case FIXED_POINT_TYPE:
2569 579014513 : {
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 579014513 : write_builtin_type (TYPE_MAIN_VARIANT (type));
2574 579014513 : ++is_builtin_type;
2575 : }
2576 579014513 : break;
2577 :
2578 687223 : case COMPLEX_TYPE:
2579 687223 : write_char ('C');
2580 687223 : write_type (TREE_TYPE (type));
2581 687223 : break;
2582 :
2583 3245772 : case FUNCTION_TYPE:
2584 3245772 : case METHOD_TYPE:
2585 3245772 : write_function_type (type);
2586 3245772 : break;
2587 :
2588 323381602 : case UNION_TYPE:
2589 323381602 : case RECORD_TYPE:
2590 323381602 : case ENUMERAL_TYPE:
2591 : /* A pointer-to-member function is represented as a special
2592 : RECORD_TYPE, so check for this first. */
2593 323381602 : if (TYPE_PTRMEMFUNC_P (type))
2594 268911 : write_pointer_to_member_type (type);
2595 : else
2596 323112691 : write_class_enum_type (type);
2597 : break;
2598 :
2599 2702067 : case TYPENAME_TYPE:
2600 2702067 : case UNBOUND_CLASS_TEMPLATE:
2601 : /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
2602 : ordinary nested names. */
2603 2702067 : write_nested_name (TYPE_STUB_DECL (type));
2604 2702067 : break;
2605 :
2606 133574738 : case POINTER_TYPE:
2607 133574738 : case REFERENCE_TYPE:
2608 133574738 : if (TYPE_PTR_P (type))
2609 57261262 : write_char ('P');
2610 76313476 : else if (TYPE_REF_IS_RVALUE (type))
2611 12849933 : write_char ('O');
2612 : else
2613 63463543 : write_char ('R');
2614 133574738 : {
2615 133574738 : 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 133574738 : if (TREE_CODE (target) == FUNCTION_TYPE)
2622 : {
2623 11077505 : if (abi_warn_or_compat_version_crosses (5)
2624 2215855 : && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
2625 3 : G.need_abi_warning = 1;
2626 2215849 : if (abi_version_at_least (5))
2627 2215420 : target = build_qualified_type (target, TYPE_UNQUALIFIED);
2628 : }
2629 133574738 : write_type (target);
2630 : }
2631 133574738 : break;
2632 :
2633 29065818 : case TEMPLATE_TYPE_PARM:
2634 29065818 : if (is_auto (type))
2635 : {
2636 880924 : if (template_placeholder_p (type)
2637 880924 : && 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);
2644 33 : break;
2645 : }
2646 880891 : if (AUTO_IS_DECLTYPE (type))
2647 21898 : write_identifier ("Dc");
2648 : else
2649 858993 : write_identifier ("Da");
2650 : ++is_builtin_type;
2651 : break;
2652 : }
2653 : /* fall through. */
2654 28184894 : case TEMPLATE_PARM_INDEX:
2655 28184894 : write_template_param (type);
2656 28184894 : 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 35277 : case VECTOR_TYPE:
2669 35277 : if (abi_version_at_least (4))
2670 : {
2671 35262 : write_string ("Dv");
2672 : /* Non-constant vector size would be encoded with
2673 : _ expression, but we don't support that yet. */
2674 35262 : write_unsigned_number (TYPE_VECTOR_SUBPARTS (type)
2675 : .to_constant ());
2676 35262 : write_char ('_');
2677 35262 : }
2678 : else
2679 15 : write_string ("U8__vector");
2680 176340 : if (abi_warn_or_compat_version_crosses (4))
2681 15 : G.need_abi_warning = 1;
2682 35277 : write_type (TREE_TYPE (type));
2683 35277 : break;
2684 :
2685 3410562 : case TYPE_PACK_EXPANSION:
2686 3410562 : write_string ("Dp");
2687 3410562 : write_type (PACK_EXPANSION_PATTERN (type));
2688 3410562 : break;
2689 :
2690 450761 : case DECLTYPE_TYPE:
2691 : /* These shouldn't make it into mangling. */
2692 450761 : 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 450761 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2697 : {
2698 555 : tree expr = DECLTYPE_TYPE_EXPR (type);
2699 555 : tree etype = NULL_TREE;
2700 555 : 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 450752 : write_char ('D');
2726 450752 : if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2727 546 : write_char ('t');
2728 : else
2729 450206 : write_char ('T');
2730 450752 : ++cp_unevaluated_operand;
2731 450752 : write_expression (DECLTYPE_TYPE_EXPR (type));
2732 450752 : --cp_unevaluated_operand;
2733 450752 : write_char ('E');
2734 450752 : break;
2735 :
2736 796600 : case NULLPTR_TYPE:
2737 796600 : write_string ("Dn");
2738 796600 : if (abi_check (7))
2739 : ++is_builtin_type;
2740 : break;
2741 :
2742 52 : case SPLICE_SCOPE:
2743 52 : write_splice (type);
2744 52 : 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 228950 : case OPAQUE_TYPE:
2774 228950 : if (REFLECTION_TYPE_P (type))
2775 : {
2776 228950 : write_string ("Dm");
2777 228950 : ++is_builtin_type;
2778 228950 : 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 1159910177 : if (!is_builtin_type)
2790 579792511 : 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 12094 : mangle_type_attribute_p (tree name)
2811 : {
2812 12094 : const attribute_spec *as = lookup_attribute_spec (name);
2813 12094 : 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 1170737155 : write_CV_qualifiers_for_type (const tree type)
2842 : {
2843 1170737155 : 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 1170737155 : if (!OVERLOAD_TYPE_P (type))
2857 : {
2858 812304011 : auto_vec<tree> vec;
2859 812316105 : for (tree a = TYPE_ATTRIBUTES (type); a; a = TREE_CHAIN (a))
2860 12094 : if (mangle_type_attribute_p (get_attribute_name (a)))
2861 33 : vec.safe_push (a);
2862 4060759832 : if (abi_warn_or_compat_version_crosses (10) && !vec.is_empty ())
2863 0 : G.need_abi_warning = true;
2864 812304011 : if (abi_version_at_least (10))
2865 : {
2866 812055354 : vec.qsort (attr_strcmp);
2867 812304077 : 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 812304011 : }
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 1170737155 : cv_qualifier quals;
2897 1170737155 : addr_space_t as;
2898 1170737155 : std::tie (quals, as) = TYPE_QUALS (type).split ();
2899 :
2900 : /* No address space support yet. */
2901 1170737155 : gcc_checking_assert (ADDR_SPACE_GENERIC_P (as));
2902 :
2903 1170737155 : if (quals & TYPE_QUAL_RESTRICT)
2904 : {
2905 43 : write_char ('r');
2906 43 : ++num_qualifiers;
2907 : }
2908 1170737155 : if (quals & TYPE_QUAL_VOLATILE)
2909 : {
2910 539444 : write_char ('V');
2911 539444 : ++num_qualifiers;
2912 : }
2913 1170737155 : if (quals & TYPE_QUAL_CONST)
2914 : {
2915 82464991 : write_char ('K');
2916 82464991 : ++num_qualifiers;
2917 : }
2918 :
2919 1170737155 : return num_qualifiers;
2920 : }
2921 :
2922 : /* Non-terminal <builtin-type>.
2923 :
2924 : <builtin-type> ::= v # void
2925 : ::= b # bool
2926 : ::= w # wchar_t
2927 : ::= c # char
2928 : ::= a # signed char
2929 : ::= h # unsigned char
2930 : ::= s # short
2931 : ::= t # unsigned short
2932 : ::= i # int
2933 : ::= j # unsigned int
2934 : ::= l # long
2935 : ::= m # unsigned long
2936 : ::= x # long long, __int64
2937 : ::= y # unsigned long long, __int64
2938 : ::= n # __int128
2939 : ::= o # unsigned __int128
2940 : ::= f # float
2941 : ::= d # double
2942 : ::= e # long double, __float80
2943 : ::= g # __float128 [not supported]
2944 : ::= u <source-name> # vendor extended type */
2945 :
2946 : static void
2947 579014513 : write_builtin_type (tree type)
2948 : {
2949 579014513 : if (TYPE_CANONICAL (type))
2950 579014513 : type = TYPE_CANONICAL (type);
2951 :
2952 579014513 : switch (TREE_CODE (type))
2953 : {
2954 78399317 : case VOID_TYPE:
2955 78399317 : write_char ('v');
2956 78399317 : break;
2957 :
2958 49857973 : case BOOLEAN_TYPE:
2959 49857973 : write_char ('b');
2960 49857973 : break;
2961 :
2962 432377109 : case INTEGER_TYPE:
2963 : /* TYPE may still be wchar_t, char8_t, char16_t, or char32_t, since that
2964 : isn't in integer_type_nodes. */
2965 432377109 : if (type == wchar_type_node)
2966 43271047 : write_char ('w');
2967 389106062 : else if (type == char8_type_node)
2968 22576123 : write_string ("Du");
2969 366529939 : else if (type == char16_type_node)
2970 22325970 : write_string ("Ds");
2971 344203969 : else if (type == char32_type_node)
2972 25002252 : write_string ("Di");
2973 : else
2974 : {
2975 319201731 : size_t itk;
2976 : /* Assume TYPE is one of the shared integer type nodes. Find
2977 : it in the array of these nodes. */
2978 319201717 : iagain:
2979 2030255794 : for (itk = 0; itk < itk_none; ++itk)
2980 2028664062 : if (integer_types[itk] != NULL_TREE
2981 2019113670 : && integer_type_codes[itk] != '\0'
2982 2015930206 : && type == integer_types[itk])
2983 : {
2984 : /* Print the corresponding single-letter code. */
2985 317609999 : write_char (integer_type_codes[itk]);
2986 317609999 : break;
2987 : }
2988 :
2989 319201731 : if (itk == itk_none)
2990 : {
2991 1591732 : tree t = c_common_type_for_mode (TYPE_MODE (type),
2992 1591732 : TYPE_UNSIGNED (type));
2993 1591732 : if (type != t)
2994 : {
2995 14 : type = t;
2996 14 : goto iagain;
2997 : }
2998 :
2999 1591718 : if (TYPE_PRECISION (type) == 128)
3000 2223560 : write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
3001 : else
3002 : {
3003 : /* Allow for cases where TYPE is not one of the shared
3004 : integer type nodes and write a "vendor extended builtin
3005 : type" with a name the form intN or uintN, respectively.
3006 : Situations like this can happen if you have an
3007 : __attribute__((__mode__(__SI__))) type and use exotic
3008 : switches like '-mint8' on AVR. Of course, this is
3009 : undefined by the C++ ABI (and '-mint8' is not even
3010 : Standard C conforming), but when using such special
3011 : options you're pretty much in nowhere land anyway. */
3012 0 : const char *prefix;
3013 0 : char prec[11]; /* up to ten digits for an unsigned */
3014 :
3015 0 : prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
3016 0 : sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
3017 0 : write_char ('u'); /* "vendor extended builtin type" */
3018 0 : write_unsigned_number (strlen (prefix) + strlen (prec));
3019 0 : write_string (prefix);
3020 0 : write_string (prec);
3021 : }
3022 : }
3023 : }
3024 : break;
3025 :
3026 18380114 : case REAL_TYPE:
3027 18380114 : if (type == float_type_node)
3028 5970635 : write_char ('f');
3029 12409479 : else if (type == double_type_node)
3030 9948071 : write_char ('d');
3031 2461408 : else if (type == long_double_type_node)
3032 0 : write_char ('e');
3033 2461408 : else if (type == dfloat32_type_node)
3034 6152 : write_string ("Df");
3035 2455256 : else if (type == dfloat64_type_node)
3036 6041 : write_string ("Dd");
3037 2449215 : else if (type == dfloat128_type_node)
3038 6008 : write_string ("De");
3039 2443207 : else if (type == float16_type_node)
3040 0 : write_string ("DF16_");
3041 2443207 : else if (type == float32_type_node)
3042 807635 : write_string ("DF32_");
3043 1635572 : else if (type == float64_type_node)
3044 809379 : write_string ("DF64_");
3045 826193 : else if (type == float128_type_node)
3046 767031 : write_string ("DF128_");
3047 59162 : else if (type == float32x_type_node)
3048 29581 : write_string ("DF32x");
3049 29581 : else if (type == float64x_type_node)
3050 29581 : write_string ("DF64x");
3051 0 : else if (type == float128x_type_node)
3052 0 : write_string ("DF128x");
3053 0 : else if (type == bfloat16_type_node)
3054 0 : write_string ("DF16b");
3055 : else
3056 0 : gcc_unreachable ();
3057 : break;
3058 :
3059 0 : default:
3060 0 : gcc_unreachable ();
3061 : }
3062 579014513 : }
3063 :
3064 : /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
3065 : METHOD_TYPE. The return type is mangled before the parameter
3066 : types.
3067 :
3068 : <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
3069 :
3070 : static void
3071 3252473 : write_function_type (const tree type)
3072 : {
3073 3252473 : MANGLE_TRACE_TREE ("function-type", type);
3074 :
3075 : /* For a pointer to member function, the function type may have
3076 : cv-qualifiers, indicating the quals for the artificial 'this'
3077 : parameter. */
3078 3252473 : if (TREE_CODE (type) == METHOD_TYPE)
3079 : {
3080 : /* The first parameter must be a POINTER_TYPE pointing to the
3081 : `this' parameter. */
3082 268911 : tree this_type = class_of_this_parm (type);
3083 268911 : write_CV_qualifiers_for_type (this_type);
3084 : }
3085 :
3086 3252473 : write_exception_spec (TYPE_RAISES_EXCEPTIONS (type));
3087 :
3088 3252473 : if (tx_safe_fn_type_p (type))
3089 24 : write_string ("Dx");
3090 :
3091 3252473 : write_char ('F');
3092 : /* We don't track whether or not a type is `extern "C"'. Note that
3093 : you can have an `extern "C"' function that does not have
3094 : `extern "C"' type, and vice versa:
3095 :
3096 : extern "C" typedef void function_t();
3097 : function_t f; // f has C++ linkage, but its type is
3098 : // `extern "C"'
3099 :
3100 : typedef void function_t();
3101 : extern "C" function_t f; // Vice versa.
3102 :
3103 : See [dcl.link]. */
3104 3252473 : write_bare_function_type (type, /*include_return_type_p=*/1,
3105 : /*decl=*/NULL);
3106 3252473 : if (FUNCTION_REF_QUALIFIED (type))
3107 : {
3108 3622 : if (FUNCTION_RVALUE_QUALIFIED (type))
3109 1589 : write_char ('O');
3110 : else
3111 2033 : write_char ('R');
3112 : }
3113 3252473 : write_char ('E');
3114 3252473 : }
3115 :
3116 : /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
3117 : METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
3118 : is mangled before the parameter types. If non-NULL, DECL is
3119 : FUNCTION_DECL for the function whose type is being emitted. */
3120 :
3121 : static void
3122 194058726 : write_bare_function_type (const tree type, const int include_return_type_p,
3123 : const tree decl)
3124 : {
3125 194058726 : MANGLE_TRACE_TREE ("bare-function-type", type);
3126 :
3127 : /* Mangle the return type, if requested. */
3128 194058726 : if (include_return_type_p)
3129 17639223 : write_type (TREE_TYPE (type));
3130 :
3131 : /* Now mangle the types of the arguments. */
3132 194058726 : ++G.parm_depth;
3133 194058726 : write_method_parms (TYPE_ARG_TYPES (type),
3134 194058726 : TREE_CODE (type) == METHOD_TYPE,
3135 : decl);
3136 194058726 : --G.parm_depth;
3137 194058726 : }
3138 :
3139 : /* Write the mangled representation of a method parameter list of
3140 : types given in PARM_TYPES. If METHOD_P is nonzero, the function is
3141 : considered a non-static method, and the this parameter is omitted.
3142 : If non-NULL, DECL is the FUNCTION_DECL for the function whose
3143 : parameters are being emitted. */
3144 :
3145 : static void
3146 196418189 : write_method_parms (tree parm_types, const int method_p, const tree decl)
3147 : {
3148 196418189 : tree first_parm_type;
3149 372235426 : tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
3150 :
3151 : /* Assume this parameter type list is variable-length. If it ends
3152 : with a void type, then it's not. */
3153 196418189 : int varargs_p = 1;
3154 :
3155 : /* If this is a member function, skip the first arg, which is the
3156 : this pointer.
3157 : "Member functions do not encode the type of their implicit this
3158 : parameter."
3159 :
3160 : Similarly, there's no need to mangle artificial parameters, like
3161 : the VTT parameters for constructors and destructors. */
3162 196418189 : if (method_p)
3163 : {
3164 153125877 : parm_types = TREE_CHAIN (parm_types);
3165 153125877 : parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
3166 :
3167 153166633 : while (parm_decl && DECL_ARTIFICIAL (parm_decl))
3168 : {
3169 40756 : parm_types = TREE_CHAIN (parm_types);
3170 40756 : parm_decl = DECL_CHAIN (parm_decl);
3171 : }
3172 :
3173 153125877 : if (decl && ctor_omit_inherited_parms (decl))
3174 : /* Bring back parameters omitted from an inherited ctor. */
3175 54 : parm_types = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (decl));
3176 : }
3177 :
3178 196418189 : for (first_parm_type = parm_types;
3179 613841868 : parm_types;
3180 417423679 : parm_types = TREE_CHAIN (parm_types))
3181 : {
3182 417423679 : tree parm = TREE_VALUE (parm_types);
3183 417423679 : if (parm == void_type_node)
3184 : {
3185 : /* "Empty parameter lists, whether declared as () or
3186 : conventionally as (void), are encoded with a void parameter
3187 : (v)." */
3188 196373956 : if (parm_types == first_parm_type)
3189 65851494 : write_type (parm);
3190 : /* If the parm list is terminated with a void type, it's
3191 : fixed-length. */
3192 196373956 : varargs_p = 0;
3193 : /* A void type better be the last one. */
3194 196373956 : gcc_assert (TREE_CHAIN (parm_types) == NULL);
3195 : }
3196 : else
3197 221049723 : write_type (parm);
3198 : }
3199 :
3200 196418189 : if (varargs_p)
3201 : /* <builtin-type> ::= z # ellipsis */
3202 44233 : write_char ('z');
3203 196418189 : }
3204 :
3205 : /* <class-enum-type> ::= <name> */
3206 :
3207 : static void
3208 323112691 : write_class_enum_type (const tree type)
3209 : {
3210 323112691 : write_name (TYPE_NAME (type));
3211 323112691 : }
3212 :
3213 : /* Mangle a requirement REQ in a requires-expression. */
3214 :
3215 : static void
3216 556324 : write_requirement (tree req)
3217 : {
3218 556324 : tree op = TREE_OPERAND (req, 0);
3219 :
3220 556324 : switch (tree_code code = TREE_CODE (req))
3221 : {
3222 : /* # simple-requirement or compound-requirement
3223 : <requirement> ::= X <expression> [ N ] [ R <type-constraint> ]
3224 : X <expression> C <expression>
3225 : [ R <type-constraint> ] */
3226 555891 : case SIMPLE_REQ:
3227 555891 : case COMPOUND_REQ:
3228 555891 : write_char ('X');
3229 555891 : write_expression (op);
3230 555891 : if (code == SIMPLE_REQ)
3231 : break;
3232 107939 : if (operand_equal_p (TREE_OPERAND (req, 2), boolean_true_node))
3233 15 : write_char ('N');
3234 107924 : else if (TREE_OPERAND (req, 2) != error_mark_node
3235 107924 : && !operand_equal_p (TREE_OPERAND (req, 2), boolean_false_node))
3236 : {
3237 6 : write_char ('C');
3238 6 : write_expression (TREE_OPERAND (req, 2));
3239 : }
3240 107939 : if (tree constr = TREE_OPERAND (req, 1))
3241 : {
3242 107924 : write_char ('R');
3243 107924 : write_type_constraint (PLACEHOLDER_TYPE_CONSTRAINTS (constr));
3244 : }
3245 : break;
3246 :
3247 : /* <requirement> ::= T <type> # type-requirement */
3248 427 : case TYPE_REQ:
3249 427 : write_char ('T');
3250 427 : write_type (op);
3251 427 : break;
3252 :
3253 : /* <requirement> ::= Q <constraint-expression> # nested-requirement */
3254 6 : case NESTED_REQ:
3255 6 : write_char ('Q');
3256 6 : write_constraint_expression (op);
3257 6 : break;
3258 :
3259 0 : default:
3260 0 : gcc_unreachable ();
3261 : }
3262 556324 : }
3263 :
3264 : /* # requires { ... }
3265 : <expression> ::= rq <requirement>+ E
3266 : # requires (...) { ... }
3267 : <expression> ::= rQ <bare-function-type> _ <requirement>+ E */
3268 :
3269 : static void
3270 528506 : write_requires_expr (tree expr)
3271 : {
3272 528506 : tree parms = REQUIRES_EXPR_PARMS (expr);
3273 528506 : if (parms)
3274 : {
3275 91198 : write_string ("rQ");
3276 91198 : ++G.parm_depth;
3277 182845 : for (; parms; parms = DECL_CHAIN (parms))
3278 91647 : write_type (cv_unqualified (TREE_TYPE (parms)));
3279 91198 : --G.parm_depth;
3280 91198 : write_char ('_');
3281 : }
3282 : else
3283 437308 : write_string ("rq");
3284 :
3285 1084830 : for (tree reqs = REQUIRES_EXPR_REQS (expr); reqs;
3286 556324 : reqs = TREE_CHAIN (reqs))
3287 556324 : write_requirement (TREE_VALUE (reqs));
3288 :
3289 528506 : write_char ('E');
3290 528506 : }
3291 :
3292 : /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
3293 : arguments.
3294 :
3295 : <template-args> ::= I <template-arg>* [Q <constraint-expr>] E */
3296 :
3297 : static void
3298 416769589 : write_template_args (tree args, tree parms /*= NULL_TREE*/)
3299 : {
3300 416769589 : int i;
3301 416769589 : int length = 0;
3302 :
3303 416769589 : MANGLE_TRACE_TREE ("template-args", args);
3304 :
3305 416769589 : write_char ('I');
3306 :
3307 416769589 : if (args)
3308 416769589 : length = TREE_VEC_LENGTH (args);
3309 :
3310 416769589 : tree constraints = NULL_TREE;
3311 416769589 : if (parms)
3312 : {
3313 4922057 : constraints = TEMPLATE_PARMS_CONSTRAINTS (parms);
3314 4922057 : parms = INNERMOST_TEMPLATE_PARMS (parms);
3315 : }
3316 :
3317 416769589 : if (args && length && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
3318 : {
3319 : /* We have nested template args. We want the innermost template
3320 : argument list. */
3321 6184739 : args = TREE_VEC_ELT (args, length - 1);
3322 6184739 : length = TREE_VEC_LENGTH (args);
3323 : }
3324 416769589 : if (TEMPLATE_ARGS_TYPE_CONSTRAINT_P (args))
3325 : /* Skip the constrained type. */
3326 : i = 1;
3327 : else
3328 416717072 : i = 0;
3329 416769589 : bool implicit_parm_scope = false;
3330 1158474189 : for (; i < length; ++i)
3331 : {
3332 741704600 : tree arg = TREE_VEC_ELT (args, i);
3333 741704600 : if (parms)
3334 : {
3335 8522276 : tree parm = TREE_VEC_ELT (parms, i);
3336 8522276 : tree decl = TREE_VALUE (parm);
3337 8522276 : if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl)
3338 8522276 : && !implicit_parm_scope)
3339 : {
3340 : /* The rest of the template parameters are based on generic
3341 : function parameters, so any expressions in their
3342 : type-constraints are in parameter scope. */
3343 9799 : implicit_parm_scope = true;
3344 9799 : ++G.parm_depth;
3345 : }
3346 8522276 : if (!template_parm_natural_p (arg, parm)
3347 8522276 : && abi_check (19))
3348 42151 : write_template_param_decl (parm);
3349 : }
3350 741704600 : write_template_arg (arg);
3351 : }
3352 416769589 : if (implicit_parm_scope)
3353 9799 : --G.parm_depth;
3354 :
3355 416769589 : write_tparms_constraints (constraints);
3356 :
3357 416769589 : write_char ('E');
3358 416769589 : }
3359 :
3360 : /* Write out the
3361 : <unqualified-name>
3362 : <unqualified-name> <template-args>
3363 : part of SCOPE_REF or COMPONENT_REF mangling. */
3364 :
3365 : static void
3366 509906 : write_member_name (tree member)
3367 : {
3368 509906 : if (identifier_p (member))
3369 : {
3370 371083 : if (IDENTIFIER_ANY_OP_P (member))
3371 : {
3372 53479 : if (abi_check (11))
3373 53455 : write_string ("on");
3374 : }
3375 371083 : write_unqualified_id (member);
3376 : }
3377 138823 : else if (DECL_P (member))
3378 : {
3379 1296 : if (ANON_AGGR_TYPE_P (TREE_TYPE (member)))
3380 : ;
3381 1290 : else if (DECL_OVERLOADED_OPERATOR_P (member))
3382 : {
3383 15 : if (abi_check (16))
3384 9 : write_string ("on");
3385 : }
3386 1296 : write_unqualified_name (member);
3387 : }
3388 137527 : else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
3389 : {
3390 271 : tree name = TREE_OPERAND (member, 0);
3391 271 : name = OVL_FIRST (name);
3392 271 : write_member_name (name);
3393 271 : write_template_args (TREE_OPERAND (member, 1));
3394 : }
3395 : else
3396 137256 : write_expression (member);
3397 509906 : }
3398 :
3399 : /* EXPR is a base COMPONENT_REF; write the minimized base conversion path for
3400 : converting to BASE, or just the conversion of EXPR if BASE is null.
3401 :
3402 : "Given a fully explicit base path P := C_n -> ... -> C_0, the minimized base
3403 : path Min(P) is defined as follows: let C_i be the last element for which the
3404 : conversion to C_0 is unambiguous; if that element is C_n, the minimized path
3405 : is C_n -> C_0; otherwise, the minimized path is Min(C_n -> ... -> C_i) ->
3406 : C_0."
3407 :
3408 : We mangle the conversion to C_i if it's different from C_n. */
3409 :
3410 : static bool
3411 199282 : write_base_ref (tree expr, tree base = NULL_TREE)
3412 : {
3413 199282 : if (TREE_CODE (expr) != COMPONENT_REF)
3414 : return false;
3415 :
3416 199276 : tree field = TREE_OPERAND (expr, 1);
3417 :
3418 199276 : if (TREE_CODE (field) != FIELD_DECL || !DECL_FIELD_IS_BASE (field))
3419 : return false;
3420 :
3421 18 : tree object = TREE_OPERAND (expr, 0);
3422 :
3423 18 : tree binfo = NULL_TREE;
3424 18 : if (base)
3425 : {
3426 9 : tree cur = TREE_TYPE (object);
3427 9 : binfo = lookup_base (cur, base, ba_unique, NULL, tf_none);
3428 : }
3429 : else
3430 : /* We're at the end of the base conversion chain, so it can't be
3431 : ambiguous. */
3432 9 : base = TREE_TYPE (field);
3433 :
3434 18 : if (binfo == error_mark_node)
3435 : {
3436 : /* cur->base is ambiguous, so make the conversion to
3437 : last explicit, expressed as a cast (last&)object. */
3438 3 : tree last = TREE_TYPE (expr);
3439 3 : write_string (OVL_OP_INFO (false, CAST_EXPR)->mangled_name);
3440 3 : write_type (build_reference_type (last));
3441 3 : write_expression (object);
3442 : }
3443 15 : else if (write_base_ref (object, base))
3444 : /* cur->base is unambiguous, but we had another base conversion
3445 : underneath and wrote it out. */;
3446 : else
3447 : /* No more base conversions, just write out the object. */
3448 6 : write_expression (object);
3449 :
3450 : return true;
3451 : }
3452 :
3453 : /* The number of elements spanned by a RANGE_EXPR. */
3454 :
3455 : unsigned HOST_WIDE_INT
3456 24 : range_expr_nelts (tree expr)
3457 : {
3458 24 : tree lo = TREE_OPERAND (expr, 0);
3459 24 : tree hi = TREE_OPERAND (expr, 1);
3460 24 : return tree_to_uhwi (hi) - tree_to_uhwi (lo) + 1;
3461 : }
3462 :
3463 : /* <expression> ::= <unary operator-name> <expression>
3464 : ::= <binary operator-name> <expression> <expression>
3465 : ::= <expr-primary>
3466 :
3467 : <expr-primary> ::= <template-param>
3468 : ::= L <type> <value number> E # literal
3469 : ::= L <mangled-name> E # external name
3470 : ::= st <type> # sizeof
3471 : ::= sr <type> <unqualified-name> # dependent name
3472 : ::= sr <type> <unqualified-name> <template-args>
3473 : ::= L Dm <value reflection> E # C++26 reflection
3474 : # value [proposed]
3475 : ::= <splice> # C++26 dependent splice [proposed] */
3476 :
3477 : static void
3478 12575569 : write_expression (tree expr)
3479 : {
3480 13006391 : enum tree_code code = TREE_CODE (expr);
3481 :
3482 13006391 : if (TREE_CODE (expr) == TARGET_EXPR)
3483 : {
3484 0 : expr = TARGET_EXPR_INITIAL (expr);
3485 0 : code = TREE_CODE (expr);
3486 : }
3487 :
3488 : /* Skip NOP_EXPR and CONVERT_EXPR. They can occur when (say) a pointer
3489 : argument is converted (via qualification conversions) to another type. */
3490 13570668 : while (CONVERT_EXPR_CODE_P (code)
3491 13447559 : || code == IMPLICIT_CONV_EXPR
3492 13447514 : || location_wrapper_p (expr)
3493 : /* Parentheses aren't mangled. */
3494 13006757 : || code == PAREN_EXPR
3495 13006757 : || code == NON_LVALUE_EXPR
3496 26577425 : || (code == VIEW_CONVERT_EXPR
3497 366 : && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
3498 : {
3499 564277 : expr = TREE_OPERAND (expr, 0);
3500 564277 : code = TREE_CODE (expr);
3501 : }
3502 :
3503 13006391 : if (code == BASELINK
3504 13006391 : && (!type_unknown_p (expr)
3505 766219 : || !BASELINK_QUALIFIED_P (expr)))
3506 : {
3507 783154 : expr = BASELINK_FUNCTIONS (expr);
3508 783154 : code = TREE_CODE (expr);
3509 : }
3510 :
3511 : /* Handle pointers-to-members by making them look like expression
3512 : nodes. */
3513 13006391 : if (code == PTRMEM_CST)
3514 : {
3515 7790 : expr = build_nt (ADDR_EXPR,
3516 : build_qualified_name (/*type=*/NULL_TREE,
3517 3895 : PTRMEM_CST_CLASS (expr),
3518 3895 : PTRMEM_CST_MEMBER (expr),
3519 : /*template_p=*/false));
3520 3895 : code = TREE_CODE (expr);
3521 : }
3522 :
3523 : /* Handle template parameters. */
3524 13006391 : if (code == TEMPLATE_TYPE_PARM
3525 13006391 : || code == TEMPLATE_TEMPLATE_PARM
3526 13006391 : || code == BOUND_TEMPLATE_TEMPLATE_PARM
3527 13002377 : || code == TEMPLATE_PARM_INDEX)
3528 786724 : write_template_param (expr);
3529 : /* Handle literals. */
3530 12219667 : else if (TREE_CODE_CLASS (code) == tcc_constant
3531 11840191 : || code == CONST_DECL
3532 11840191 : || code == REFLECT_EXPR)
3533 404056 : write_template_arg_literal (expr);
3534 11815611 : else if (code == EXCESS_PRECISION_EXPR
3535 11815611 : && TREE_CODE (TREE_OPERAND (expr, 0)) == REAL_CST)
3536 0 : write_template_arg_literal (fold_convert (TREE_TYPE (expr),
3537 : TREE_OPERAND (expr, 0)));
3538 11815611 : else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
3539 : {
3540 121743 : gcc_assert (id_equal (DECL_NAME (expr), "this"));
3541 121743 : write_string ("fpT");
3542 : }
3543 11693868 : else if (code == PARM_DECL)
3544 : {
3545 : /* A function parameter used in a late-specified return type. */
3546 359527 : int index = DECL_PARM_INDEX (expr);
3547 359527 : int level = DECL_PARM_LEVEL (expr);
3548 359527 : int delta = G.parm_depth - level + 1;
3549 359527 : gcc_assert (index >= 1);
3550 359527 : write_char ('f');
3551 359527 : if (delta != 0)
3552 : {
3553 172252 : gcc_checking_assert (delta > 0);
3554 172252 : if (abi_check (5))
3555 : {
3556 : /* Let L be the number of function prototype scopes from the
3557 : innermost one (in which the parameter reference occurs) up
3558 : to (and including) the one containing the declaration of
3559 : the referenced parameter. If the parameter declaration
3560 : clause of the innermost function prototype scope has been
3561 : completely seen, it is not counted (in that case -- which
3562 : is perhaps the most common -- L can be zero). */
3563 172249 : write_char ('L');
3564 172249 : write_unsigned_number (delta - 1);
3565 : }
3566 : }
3567 359527 : write_char ('p');
3568 359527 : write_compact_number (index - 1);
3569 : }
3570 11334341 : else if (DECL_P (expr))
3571 : {
3572 378473 : write_char ('L');
3573 378473 : write_mangled_name (expr, false);
3574 378473 : write_char ('E');
3575 : }
3576 10955868 : else if (TREE_CODE (expr) == SIZEOF_EXPR)
3577 : {
3578 6971 : tree op = TREE_OPERAND (expr, 0);
3579 :
3580 6971 : if (PACK_EXPANSION_P (op))
3581 : {
3582 5462 : sizeof_pack:
3583 5465 : if (abi_check (11))
3584 : {
3585 : /* sZ rather than szDp. */
3586 5456 : write_string ("sZ");
3587 5456 : write_expression (PACK_EXPANSION_PATTERN (op));
3588 : return;
3589 : }
3590 : }
3591 :
3592 1518 : if (SIZEOF_EXPR_TYPE_P (expr))
3593 : {
3594 0 : write_string ("st");
3595 0 : write_type (TREE_TYPE (op));
3596 : }
3597 1518 : else if (ARGUMENT_PACK_P (op))
3598 : {
3599 15 : tree args = ARGUMENT_PACK_ARGS (op);
3600 15 : int length = TREE_VEC_LENGTH (args);
3601 15 : if (abi_check (10))
3602 : {
3603 : /* Before v19 we wrongly mangled all single pack expansions with
3604 : sZ, but now only for expressions, as types ICEd (95298). */
3605 12 : if (length == 1)
3606 : {
3607 9 : tree arg = TREE_VEC_ELT (args, 0);
3608 9 : if (TREE_CODE (arg) == EXPR_PACK_EXPANSION
3609 9 : && !abi_check (19))
3610 : {
3611 3 : op = arg;
3612 3 : goto sizeof_pack;
3613 : }
3614 : }
3615 :
3616 : /* sP <template-arg>* E # sizeof...(T), size of a captured
3617 : template parameter pack from an alias template */
3618 9 : write_string ("sP");
3619 24 : for (int i = 0; i < length; ++i)
3620 15 : write_template_arg (TREE_VEC_ELT (args, i));
3621 9 : write_char ('E');
3622 : }
3623 : else
3624 : {
3625 : /* In GCC 5 we represented this sizeof wrong, with the effect
3626 : that we mangled it as the last element of the pack. */
3627 3 : tree arg = TREE_VEC_ELT (args, length-1);
3628 3 : if (TYPE_P (op))
3629 : {
3630 3 : write_string ("st");
3631 3 : write_type (arg);
3632 : }
3633 : else
3634 : {
3635 0 : write_string ("sz");
3636 0 : write_expression (arg);
3637 : }
3638 : }
3639 : }
3640 1503 : else if (TYPE_P (TREE_OPERAND (expr, 0)))
3641 : {
3642 1381 : write_string ("st");
3643 1381 : write_type (TREE_OPERAND (expr, 0));
3644 : }
3645 : else
3646 122 : goto normal_expr;
3647 : }
3648 10948897 : else if (code == PACK_INDEX_EXPR)
3649 : {
3650 : /* https://github.com/itanium-cxx-abi/cxx-abi/issues/175. */
3651 37 : write_string ("sy");
3652 37 : if (TREE_CODE (PACK_INDEX_PACK (expr)) == TREE_VEC)
3653 : {
3654 2 : write_char ('J');
3655 20 : for (int i = 0; i < TREE_VEC_LENGTH (PACK_INDEX_PACK (expr));
3656 : ++i)
3657 8 : write_template_arg (TREE_VEC_ELT (PACK_INDEX_PACK (expr), i));
3658 2 : write_char ('E');
3659 : }
3660 : else
3661 : /* sy rather than sysp. */
3662 35 : write_expression (PACK_EXPANSION_PATTERN (PACK_INDEX_PACK (expr)));
3663 37 : write_expression (PACK_INDEX_INDEX (expr));
3664 : }
3665 10948860 : else if (TREE_CODE (expr) == ALIGNOF_EXPR)
3666 : {
3667 36 : if (!ALIGNOF_EXPR_STD_P (expr))
3668 : {
3669 24 : if (abi_check (16))
3670 : {
3671 : /* We used to mangle __alignof__ like alignof. */
3672 18 : write_string ("u11__alignof__");
3673 18 : write_template_arg (TREE_OPERAND (expr, 0));
3674 18 : write_char ('E');
3675 18 : return;
3676 : }
3677 : }
3678 18 : if (TYPE_P (TREE_OPERAND (expr, 0)))
3679 : {
3680 9 : write_string ("at");
3681 9 : write_type (TREE_OPERAND (expr, 0));
3682 : }
3683 : else
3684 9 : goto normal_expr;
3685 : }
3686 10948824 : else if (code == SCOPE_REF
3687 10948824 : || code == BASELINK)
3688 : {
3689 314282 : tree scope, member;
3690 314282 : if (code == SCOPE_REF)
3691 : {
3692 314249 : scope = TREE_OPERAND (expr, 0);
3693 314249 : member = TREE_OPERAND (expr, 1);
3694 314249 : if (BASELINK_P (member))
3695 21 : member = BASELINK_FUNCTIONS (member);
3696 : }
3697 : else
3698 : {
3699 33 : scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
3700 33 : member = BASELINK_FUNCTIONS (expr);
3701 : }
3702 :
3703 : /* If the MEMBER is a real declaration, then the qualifying
3704 : scope was not dependent. Ideally, we would not have a
3705 : SCOPE_REF in those cases, but sometimes we do. If the second
3706 : argument is a DECL, then the name must not have been
3707 : dependent. */
3708 314282 : if (DECL_P (member))
3709 : write_expression (member);
3710 : else
3711 : {
3712 310084 : gcc_assert (code != BASELINK || BASELINK_QUALIFIED_P (expr));
3713 310051 : write_string ("sr");
3714 310051 : write_type (scope);
3715 310051 : write_member_name (member);
3716 : }
3717 : }
3718 10634542 : else if (INDIRECT_REF_P (expr)
3719 468631 : && TREE_TYPE (TREE_OPERAND (expr, 0))
3720 11020602 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
3721 : {
3722 263205 : write_expression (TREE_OPERAND (expr, 0));
3723 : }
3724 10371337 : else if (identifier_p (expr))
3725 : {
3726 : /* An operator name appearing as a dependent name needs to be
3727 : specially marked to disambiguate between a use of the operator
3728 : name and a use of the operator in an expression. */
3729 254820 : if (IDENTIFIER_ANY_OP_P (expr))
3730 7 : write_string ("on");
3731 254820 : write_unqualified_id (expr);
3732 : }
3733 10116517 : else if (dependent_splice_p (expr))
3734 44 : write_splice (expr);
3735 10116473 : else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
3736 : {
3737 4261560 : tree fn = TREE_OPERAND (expr, 0);
3738 5629418 : if (!identifier_p (fn))
3739 4261557 : fn = OVL_NAME (fn);
3740 4261560 : if (IDENTIFIER_ANY_OP_P (fn))
3741 3 : write_string ("on");
3742 4261560 : write_unqualified_id (fn);
3743 4261560 : write_template_args (TREE_OPERAND (expr, 1));
3744 : }
3745 5854913 : else if (TREE_CODE (expr) == MODOP_EXPR)
3746 : {
3747 87 : enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
3748 87 : const char *name = OVL_OP_INFO (true, subop)->mangled_name;
3749 :
3750 87 : write_string (name);
3751 87 : write_expression (TREE_OPERAND (expr, 0));
3752 87 : write_expression (TREE_OPERAND (expr, 2));
3753 : }
3754 5854826 : else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
3755 : {
3756 : /* ::= [gs] nw <expression>* _ <type> E
3757 : ::= [gs] nw <expression>* _ <type> <initializer>
3758 : ::= [gs] na <expression>* _ <type> E
3759 : ::= [gs] na <expression>* _ <type> <initializer>
3760 : <initializer> ::= pi <expression>* E */
3761 155827 : tree placement = TREE_OPERAND (expr, 0);
3762 155827 : tree type = TREE_OPERAND (expr, 1);
3763 155827 : tree nelts = TREE_OPERAND (expr, 2);
3764 155827 : tree init = TREE_OPERAND (expr, 3);
3765 155827 : tree t;
3766 :
3767 155827 : gcc_assert (code == NEW_EXPR);
3768 155827 : if (TREE_OPERAND (expr, 2))
3769 12 : code = VEC_NEW_EXPR;
3770 :
3771 155827 : if (NEW_EXPR_USE_GLOBAL (expr))
3772 155802 : write_string ("gs");
3773 :
3774 155827 : write_string (OVL_OP_INFO (false, code)->mangled_name);
3775 :
3776 311629 : for (t = placement; t; t = TREE_CHAIN (t))
3777 155802 : write_expression (TREE_VALUE (t));
3778 :
3779 155827 : write_char ('_');
3780 :
3781 155827 : if (nelts)
3782 : {
3783 12 : ++processing_template_decl;
3784 : /* Avoid compute_array_index_type complaints about
3785 : non-constant nelts. */
3786 12 : tree max = cp_build_binary_op (input_location, MINUS_EXPR,
3787 : fold_convert (sizetype, nelts),
3788 : size_one_node,
3789 : tf_warning_or_error);
3790 12 : max = maybe_constant_value (max);
3791 12 : tree domain = build_index_type (max);
3792 12 : type = build_cplus_array_type (type, domain);
3793 12 : --processing_template_decl;
3794 : }
3795 155827 : write_type (type);
3796 :
3797 155815 : if (init && TREE_CODE (init) == TREE_LIST
3798 311636 : && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
3799 : write_expression (TREE_VALUE (init));
3800 : else
3801 : {
3802 155824 : if (init)
3803 155812 : write_string ("pi");
3804 155812 : if (init && init != void_node)
3805 311612 : for (t = init; t; t = TREE_CHAIN (t))
3806 155806 : write_expression (TREE_VALUE (t));
3807 155824 : write_char ('E');
3808 : }
3809 : }
3810 : else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
3811 : {
3812 12 : gcc_assert (code == DELETE_EXPR);
3813 12 : if (DELETE_EXPR_USE_VEC (expr))
3814 6 : code = VEC_DELETE_EXPR;
3815 :
3816 12 : if (DELETE_EXPR_USE_GLOBAL (expr))
3817 6 : write_string ("gs");
3818 :
3819 12 : write_string (OVL_OP_INFO (false, code)->mangled_name);
3820 :
3821 12 : write_expression (TREE_OPERAND (expr, 0));
3822 : }
3823 : else if (code == THROW_EXPR)
3824 : {
3825 8 : tree op = TREE_OPERAND (expr, 0);
3826 8 : if (op)
3827 : {
3828 5 : write_string ("tw");
3829 5 : write_expression (op);
3830 : }
3831 : else
3832 3 : write_string ("tr");
3833 : }
3834 : else if (code == NOEXCEPT_EXPR)
3835 : {
3836 6 : write_string ("nx");
3837 6 : write_expression (TREE_OPERAND (expr, 0));
3838 : }
3839 : else if (code == CONSTRUCTOR)
3840 : {
3841 177095 : bool braced_init = BRACE_ENCLOSED_INITIALIZER_P (expr);
3842 177095 : tree etype = TREE_TYPE (expr);
3843 :
3844 177095 : if (braced_init)
3845 98 : write_string ("il");
3846 : else
3847 : {
3848 176997 : write_string ("tl");
3849 176997 : write_type (etype);
3850 : }
3851 :
3852 : /* If this is an undigested initializer, mangle it as written.
3853 : COMPOUND_LITERAL_P doesn't actually distinguish between digested and
3854 : undigested braced casts, but it should work to use it to distinguish
3855 : between braced casts in a template signature (undigested) and template
3856 : parm object values (digested), and all CONSTRUCTORS that get here
3857 : should be one of those two cases. */
3858 177095 : bool undigested = braced_init || COMPOUND_LITERAL_P (expr);
3859 176692 : if (undigested || !zero_init_expr_p (expr))
3860 : {
3861 : /* Convert braced initializer lists to STRING_CSTs so that
3862 : A<"Foo"> mangles the same as A<{'F', 'o', 'o', 0}> while
3863 : still using the latter mangling for strings that
3864 : originated as braced initializer lists. */
3865 156203 : expr = braced_lists_to_strings (etype, expr);
3866 :
3867 156203 : if (TREE_CODE (expr) == CONSTRUCTOR)
3868 : {
3869 156195 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
3870 156195 : unsigned last_nonzero = UINT_MAX;
3871 156195 : constructor_elt *ce;
3872 :
3873 156195 : if (!undigested)
3874 376035 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3875 220243 : if ((TREE_CODE (etype) == UNION_TYPE
3876 37 : && ce->index != first_field (etype))
3877 220269 : || !zero_init_expr_p (ce->value))
3878 : last_nonzero = i;
3879 :
3880 156195 : tree prev_field = NULL_TREE;
3881 156195 : if (undigested || last_nonzero != UINT_MAX)
3882 374093 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3883 : {
3884 220214 : if (i > last_nonzero)
3885 : break;
3886 217733 : if (!undigested && !CONSTRUCTOR_NO_CLEARING (expr)
3887 217898 : && (TREE_CODE (etype) == RECORD_TYPE
3888 215861 : || TREE_CODE (etype) == ARRAY_TYPE))
3889 : {
3890 : /* Write out any implicit non-trailing zeros
3891 : (which we neglected to do before v21). */
3892 215824 : if (TREE_CODE (etype) == RECORD_TYPE)
3893 : {
3894 133486 : tree field;
3895 133486 : if (i == 0)
3896 120193 : field = first_field (etype);
3897 : else
3898 13293 : field = DECL_CHAIN (prev_field);
3899 133898 : for (;;)
3900 : {
3901 133692 : field = next_subobject_field (field);
3902 133692 : if (field == ce->index)
3903 : break;
3904 206 : if (abi_check (21))
3905 : {
3906 196 : tree type = TREE_TYPE (field), expr;
3907 196 : if (REFLECTION_TYPE_P (type))
3908 0 : expr = get_null_reflection ();
3909 : else
3910 196 : expr = build_zero_cst (type);
3911 196 : write_expression (expr);
3912 : }
3913 206 : field = DECL_CHAIN (field);
3914 206 : }
3915 : }
3916 82338 : else if (TREE_CODE (etype) == ARRAY_TYPE)
3917 : {
3918 82338 : unsigned HOST_WIDE_INT j;
3919 82338 : if (i == 0)
3920 : j = 0;
3921 : else
3922 47400 : j = 1 + tree_to_uhwi (prev_field);
3923 82338 : unsigned HOST_WIDE_INT k;
3924 82338 : if (TREE_CODE (ce->index) == RANGE_EXPR)
3925 12 : k = tree_to_uhwi (TREE_OPERAND (ce->index, 0));
3926 : else
3927 82326 : k = tree_to_uhwi (ce->index);
3928 82338 : tree zero = NULL_TREE;
3929 84108 : for (; j < k; ++j)
3930 1770 : if (abi_check (21))
3931 : {
3932 1188 : if (!zero)
3933 30 : zero = build_zero_cst (TREE_TYPE (etype));
3934 1188 : write_expression (zero);
3935 : }
3936 : }
3937 : }
3938 :
3939 217898 : if (!undigested && TREE_CODE (etype) == UNION_TYPE)
3940 : {
3941 : /* Express the active member as a designator. */
3942 37 : write_string ("di");
3943 37 : write_unqualified_name (ce->index);
3944 : }
3945 217898 : unsigned reps = 1;
3946 217898 : if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3947 12 : reps = range_expr_nelts (ce->index);
3948 217898 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
3949 : {
3950 30 : gcc_assert (reps == 1);
3951 30 : unsigned int len = RAW_DATA_LENGTH (ce->value);
3952 : /* If this is the last non-zero element, skip
3953 : zeros at the end. */
3954 30 : if (i == last_nonzero)
3955 282 : while (len)
3956 : {
3957 282 : if (RAW_DATA_POINTER (ce->value)[len - 1])
3958 : break;
3959 : --len;
3960 : }
3961 30 : tree valtype = TREE_TYPE (ce->value);
3962 3906 : for (unsigned int i = 0; i < len; ++i)
3963 : {
3964 3876 : write_char ('L');
3965 3876 : write_type (valtype);
3966 3876 : unsigned HOST_WIDE_INT v;
3967 3876 : if (!TYPE_UNSIGNED (valtype)
3968 780 : && TYPE_PRECISION (valtype) == BITS_PER_UNIT
3969 4656 : && RAW_DATA_SCHAR_ELT (ce->value, i) < 0)
3970 : {
3971 0 : write_char ('n');
3972 0 : v = -RAW_DATA_SCHAR_ELT (ce->value, i);
3973 : }
3974 : else
3975 3876 : v = RAW_DATA_UCHAR_ELT (ce->value, i);
3976 3876 : write_unsigned_number (v);
3977 3876 : write_char ('E');
3978 : }
3979 : }
3980 : else
3981 435754 : for (unsigned j = 0; j < reps; ++j)
3982 217886 : write_expression (ce->value);
3983 217898 : prev_field = ce->index;
3984 217898 : if (prev_field && TREE_CODE (prev_field) == RANGE_EXPR)
3985 12 : prev_field = TREE_OPERAND (prev_field, 1);
3986 : }
3987 : }
3988 : else
3989 : {
3990 8 : gcc_assert (TREE_CODE (expr) == STRING_CST);
3991 8 : write_expression (expr);
3992 : }
3993 : }
3994 177095 : write_char ('E');
3995 : }
3996 : else if (code == LAMBDA_EXPR)
3997 : {
3998 : /* [temp.over.link] Two lambda-expressions are never considered
3999 : equivalent.
4000 :
4001 : So just use the closure type mangling. */
4002 1408 : write_char ('L');
4003 1408 : write_type (LAMBDA_EXPR_CLOSURE (expr));
4004 1408 : write_char ('E');
4005 : }
4006 : else if (code == REQUIRES_EXPR)
4007 528506 : write_requires_expr (expr);
4008 4991964 : else if (dependent_name (expr))
4009 : {
4010 120629 : tree name = dependent_name (expr);
4011 120629 : if (IDENTIFIER_ANY_OP_P (name))
4012 : {
4013 9 : if (abi_check (16))
4014 6 : write_string ("on");
4015 : }
4016 120629 : write_unqualified_id (name);
4017 : }
4018 : else
4019 : {
4020 4871335 : normal_expr:
4021 4871466 : int i, len;
4022 4871466 : const char *name;
4023 :
4024 : /* When we bind a variable or function to a non-type template
4025 : argument with reference type, we create an ADDR_EXPR to show
4026 : the fact that the entity's address has been taken. But, we
4027 : don't actually want to output a mangling code for the `&'. */
4028 4871466 : if (TREE_CODE (expr) == ADDR_EXPR
4029 7364 : && TREE_TYPE (expr)
4030 4874935 : && TYPE_REF_P (TREE_TYPE (expr)))
4031 : {
4032 0 : expr = TREE_OPERAND (expr, 0);
4033 0 : if (DECL_P (expr))
4034 : {
4035 : write_expression (expr);
4036 : return;
4037 : }
4038 :
4039 0 : code = TREE_CODE (expr);
4040 : }
4041 :
4042 4871466 : if (code == COMPONENT_REF)
4043 : {
4044 199593 : tree ob = TREE_OPERAND (expr, 0);
4045 :
4046 199593 : if (TREE_CODE (ob) == ARROW_EXPR)
4047 : {
4048 326 : write_string (OVL_OP_INFO (false, code)->mangled_name);
4049 326 : ob = TREE_OPERAND (ob, 0);
4050 326 : write_expression (ob);
4051 : }
4052 199267 : else if (write_base_ref (expr))
4053 : return;
4054 199258 : else if (!is_dummy_object (ob))
4055 : {
4056 199252 : write_string ("dt");
4057 199252 : write_expression (ob);
4058 : }
4059 : /* else, for a non-static data member with no associated object (in
4060 : unevaluated context), use the unresolved-name mangling. */
4061 :
4062 199584 : write_member_name (TREE_OPERAND (expr, 1));
4063 199584 : return;
4064 : }
4065 :
4066 : /* If it wasn't any of those, recursively expand the expression. */
4067 4671873 : name = OVL_OP_INFO (false, code)->mangled_name;
4068 :
4069 : /* We used to mangle const_cast and static_cast like a C cast. */
4070 4671873 : if (code == CONST_CAST_EXPR
4071 4671873 : || code == STATIC_CAST_EXPR)
4072 : {
4073 265 : if (!abi_check (6))
4074 15 : name = OVL_OP_INFO (false, CAST_EXPR)->mangled_name;
4075 : }
4076 :
4077 4671873 : if (name == NULL)
4078 : {
4079 3 : switch (code)
4080 : {
4081 3 : case TRAIT_EXPR:
4082 3 : error ("use of built-in trait %qE in function signature; "
4083 : "use library traits instead", expr);
4084 3 : break;
4085 :
4086 0 : case ERROR_MARK:
4087 0 : if (seen_error ())
4088 : break;
4089 : /* FALLTHROUGH */
4090 0 : default:
4091 0 : sorry ("mangling %C", code);
4092 0 : break;
4093 : }
4094 : return;
4095 : }
4096 : else
4097 4671870 : write_string (name);
4098 :
4099 4671870 : switch (code)
4100 : {
4101 2113527 : case CALL_EXPR:
4102 2113527 : if (tree fn = CALL_EXPR_FN (expr))
4103 : {
4104 2113521 : if (TREE_CODE (fn) == ADDR_EXPR)
4105 0 : fn = TREE_OPERAND (fn, 0);
4106 :
4107 : /* Mangle a dependent name as the name, not whatever happens to
4108 : be the first function in the overload set. */
4109 2112885 : if (OVL_P (fn)
4110 2368221 : && type_dependent_expression_p_push (expr))
4111 254717 : fn = OVL_NAME (fn);
4112 :
4113 2113521 : write_expression (fn);
4114 : }
4115 : else
4116 6 : switch (CALL_EXPR_IFN (expr))
4117 : {
4118 : /* Calls to __builtin_bswapg, __builtin_bitreverseg,
4119 : __builtin_clmulg, __builtin_bitcompressg or
4120 : __builtin_bitexpandg are parsed as IFN_BSWAP,
4121 : IFN_BITREVERSE, IFN_CLMUL, IFN_BITCOMPRESS or IFN_BITEXPAND
4122 : calls because we don't have those builtins but instead
4123 : keywords. Pretend they are builtin function template. */
4124 3 : case IFN_BSWAP:
4125 3 : write_source_name (get_identifier ("__builtin_bswapg"));
4126 3 : break;
4127 0 : case IFN_BITCOMPRESS:
4128 0 : write_source_name (get_identifier ("__builtin_bitcompressg"));
4129 0 : break;
4130 0 : case IFN_BITEXPAND:
4131 0 : write_source_name (get_identifier ("__builtin_bitexpandg"));
4132 0 : break;
4133 3 : case IFN_BITREVERSE:
4134 3 : write_source_name (get_identifier ("__builtin_bitreverseg"));
4135 3 : break;
4136 0 : case IFN_CLMUL:
4137 0 : write_source_name (get_identifier ("__builtin_clmulg"));
4138 0 : break;
4139 0 : default:
4140 0 : gcc_unreachable ();
4141 : }
4142 :
4143 2761743 : for (i = 0; i < call_expr_nargs (expr); ++i)
4144 648216 : write_expression (CALL_EXPR_ARG (expr, i));
4145 2113527 : write_char ('E');
4146 2113527 : break;
4147 :
4148 157687 : case CAST_EXPR:
4149 157687 : write_type (TREE_TYPE (expr));
4150 157687 : if (list_length (TREE_OPERAND (expr, 0)) == 1)
4151 157509 : write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
4152 : else
4153 : {
4154 178 : tree args = TREE_OPERAND (expr, 0);
4155 178 : write_char ('_');
4156 190 : for (; args; args = TREE_CHAIN (args))
4157 12 : write_expression (TREE_VALUE (args));
4158 178 : write_char ('E');
4159 : }
4160 : break;
4161 :
4162 271 : case DYNAMIC_CAST_EXPR:
4163 271 : case REINTERPRET_CAST_EXPR:
4164 271 : case STATIC_CAST_EXPR:
4165 271 : case CONST_CAST_EXPR:
4166 271 : write_type (TREE_TYPE (expr));
4167 271 : write_expression (TREE_OPERAND (expr, 0));
4168 271 : break;
4169 :
4170 7551 : case PREINCREMENT_EXPR:
4171 7551 : case PREDECREMENT_EXPR:
4172 7551 : if (abi_check (6))
4173 7542 : write_char ('_');
4174 : /* Fall through. */
4175 :
4176 2400385 : default:
4177 : /* In the middle-end, some expressions have more operands than
4178 : they do in templates (and mangling). */
4179 2400385 : len = cp_tree_operand_length (expr);
4180 :
4181 8675070 : for (i = 0; i < len; ++i)
4182 : {
4183 3874300 : tree operand = TREE_OPERAND (expr, i);
4184 : /* As a GNU extension, the middle operand of a
4185 : conditional may be omitted. Since expression
4186 : manglings are supposed to represent the input token
4187 : stream, there's no good way to mangle such an
4188 : expression without extending the C++ ABI. */
4189 3874300 : if (code == COND_EXPR && i == 1 && !operand)
4190 : {
4191 3 : error ("omitted middle operand to %<?:%> operand "
4192 : "cannot be mangled");
4193 3 : continue;
4194 : }
4195 3874297 : else if (FOLD_EXPR_P (expr))
4196 : {
4197 : /* The first 'operand' of a fold-expression is the operator
4198 : that it folds over. */
4199 510542 : if (i == 0)
4200 : {
4201 255134 : int fcode = TREE_INT_CST_LOW (operand);
4202 255134 : write_string (OVL_OP_INFO (false, fcode)->mangled_name);
4203 255134 : continue;
4204 255134 : }
4205 255408 : else if (code == BINARY_LEFT_FOLD_EXPR)
4206 : {
4207 : /* The order of operands of the binary left and right
4208 : folds is the same, but we want to mangle them in
4209 : lexical order, i.e. non-pack first. */
4210 542 : if (i == 1)
4211 271 : operand = FOLD_EXPR_INIT (expr);
4212 : else
4213 271 : operand = FOLD_EXPR_PACK (expr);
4214 : }
4215 255408 : if (PACK_EXPANSION_P (operand))
4216 255134 : operand = PACK_EXPANSION_PATTERN (operand);
4217 : }
4218 3619163 : write_expression (operand);
4219 : }
4220 : }
4221 : }
4222 : }
4223 :
4224 : /* Non-terminal <reflection>.
4225 :
4226 : <reflection> ::= nu # null reflection
4227 : ::= vl <expression> # value
4228 : ::= ob <expression> # object
4229 : ::= vr <variable name> # variable
4230 : ::= sb <sb name> # structured binding
4231 : ::= fn <function encoding> # function
4232 : ::= pa [ <nonnegative number> ] _ <encoding> # fn param
4233 : ::= en <prefix> <unqualified-name> # enumerator
4234 : ::= an [ <nonnegative number> ] _ # annotation
4235 : ::= ta <alias prefix> <alias unqualified-name>
4236 : [ <alias template-args> ] _ <type> # type alias
4237 : ::= ty <type> # type
4238 : ::= dm <prefix> <unqualified-name> # ns data member
4239 : ::= da <prefix> [ <nonnegative number> ] _ # empty anon union
4240 : # data member
4241 : ::= un <prefix> [ <nonnegative number> ] _ # unnamed bitfld
4242 : ::= ct [ <prefix> ] <unqualified-name> # class template
4243 : ::= ft [ <prefix> ] <unqualified-name> # function template
4244 : ::= vt [ <prefix> ] <unqualified-name> # variable template
4245 : ::= at [ <prefix> ] <unqualified-name> # alias template
4246 : ::= co [ <prefix> ] <unqualified-name> # concept
4247 : ::= na [ <prefix> ] <unqualified-name> # namespace alias
4248 : ::= ns [ <prefix> ] <unqualified-name> # namespace
4249 : ::= gs # ^^::
4250 : ::= tt <template-template-param> # templ templ param
4251 : ::= de <expression> # dependent expr
4252 : ::= ba [ <nonnegative number> ] _ <type> # dir. base cls rel
4253 : ::= ds <type> _ [ <unqualified-name> ] _
4254 : [ <alignment number> ] _ [ <bit-width number> ] _
4255 : [ n ] [ <template-arg>* ] # data member spec */
4256 :
4257 : static void
4258 1758 : write_reflection (tree refl)
4259 : {
4260 1758 : char prefix[3];
4261 1758 : tree arg = reflection_mangle_prefix (refl, prefix);
4262 1758 : if (strcmp (prefix, "dm") == 0
4263 234 : && DECL_NAME (arg) == NULL_TREE
4264 12 : && ANON_AGGR_TYPE_P (TREE_TYPE (arg))
4265 1770 : && anon_aggr_naming_decl (TREE_TYPE (arg)) == NULL_TREE)
4266 12 : strcpy (prefix, "da");
4267 1758 : write_string (prefix);
4268 : /* If there is no argument, nothing further needs to be mangled. */
4269 1758 : if (arg == NULL_TREE)
4270 62 : return;
4271 1696 : if (strcmp (prefix, "vl") == 0 || strcmp (prefix, "ob") == 0)
4272 22 : write_expression (arg);
4273 1674 : else if (strcmp (prefix, "vr") == 0 || strcmp (prefix, "sb") == 0)
4274 234 : write_name (arg);
4275 1440 : else if (strcmp (prefix, "fn") == 0)
4276 196 : write_encoding (arg);
4277 1244 : else if (strcmp (prefix, "pa") == 0)
4278 : {
4279 82 : tree fn = DECL_CONTEXT (arg);
4280 : /* DECL_PARM_INDEX is 1-based but here we want a 0-based index. */
4281 82 : const int idx = DECL_PARM_INDEX (arg) - 1;
4282 82 : write_compact_number (idx);
4283 82 : write_encoding (fn);
4284 : }
4285 1162 : else if (strcmp (prefix, "en") == 0)
4286 : {
4287 26 : write_prefix (decl_mangling_context (arg));
4288 26 : write_unqualified_name (arg);
4289 : }
4290 1136 : else if (strcmp (prefix, "an") == 0)
4291 10 : write_compact_number (tree_to_uhwi (arg));
4292 1126 : else if (strcmp (prefix, "ta") == 0)
4293 : {
4294 28 : arg = TYPE_NAME (arg);
4295 : /* Can't use write_prefix (arg) here instead of
4296 : write_prefix + write_unqualified_name + optional
4297 : write_template_args, it shouldn't be
4298 : remembered among substitutions. */
4299 28 : write_prefix (decl_mangling_context (arg));
4300 28 : if (modules_p ())
4301 0 : maybe_write_module (arg);
4302 28 : write_source_name (DECL_NAME (arg));
4303 28 : tree template_info = maybe_template_info (arg);
4304 28 : if (template_info)
4305 4 : write_template_args (TI_ARGS (template_info));
4306 28 : write_char ('_');
4307 28 : write_type (DECL_ORIGINAL_TYPE (arg));
4308 : }
4309 1098 : else if (strcmp (prefix, "ty") == 0)
4310 314 : write_type (arg);
4311 784 : else if (strcmp (prefix, "dm") == 0)
4312 : {
4313 222 : tree ctx = decl_mangling_context (arg);
4314 452 : while (ctx && ANON_UNION_TYPE_P (ctx))
4315 8 : ctx = decl_mangling_context (TYPE_NAME (ctx));
4316 222 : write_prefix (ctx);
4317 222 : write_unqualified_name (arg);
4318 : }
4319 562 : else if (strcmp (prefix, "da") == 0)
4320 : {
4321 12 : int idx = 0;
4322 12 : tree ctx = decl_mangling_context (arg);
4323 198 : for (tree f = TYPE_FIELDS (ctx); f; f = DECL_CHAIN (f))
4324 198 : if (f == arg)
4325 : break;
4326 186 : else if (TREE_CODE (f) == FIELD_DECL
4327 18 : && DECL_NAME (f) == NULL_TREE
4328 18 : && ANON_AGGR_TYPE_P (TREE_TYPE (f))
4329 204 : && anon_aggr_naming_decl (TREE_TYPE (f)) == NULL_TREE)
4330 18 : ++idx;
4331 12 : write_prefix (ctx);
4332 12 : write_compact_number (idx);
4333 : }
4334 550 : else if (strcmp (prefix, "un") == 0)
4335 : {
4336 8 : tree ctx = DECL_CONTEXT (arg);
4337 8 : int idx = 0;
4338 142 : for (tree f = TYPE_FIELDS (ctx); f; f = DECL_CHAIN (f))
4339 142 : if (f == arg)
4340 : break;
4341 134 : else if (TREE_CODE (f) == FIELD_DECL && DECL_UNNAMED_BIT_FIELD (f))
4342 6 : ++idx;
4343 8 : write_prefix (decl_mangling_context (arg));
4344 8 : write_compact_number (idx);
4345 : }
4346 542 : else if (strcmp (prefix, "ct") == 0
4347 484 : || strcmp (prefix, "ft") == 0
4348 434 : || strcmp (prefix, "vt") == 0
4349 406 : || strcmp (prefix, "at") == 0
4350 380 : || strcmp (prefix, "co") == 0
4351 358 : || strcmp (prefix, "na") == 0
4352 320 : || strcmp (prefix, "ns") == 0)
4353 : {
4354 426 : write_prefix (decl_mangling_context (arg));
4355 426 : write_unqualified_name (STRIP_TEMPLATE (arg));
4356 : }
4357 116 : else if (strcmp (prefix, "ba") == 0)
4358 : {
4359 26 : gcc_assert (TREE_CODE (arg) == TREE_BINFO);
4360 : tree c = arg, base_binfo;
4361 52 : while (BINFO_INHERITANCE_CHAIN (c))
4362 : c = BINFO_INHERITANCE_CHAIN (c);
4363 :
4364 : unsigned idx;
4365 30 : for (idx = 0; BINFO_BASE_ITERATE (c, idx, base_binfo); idx++)
4366 30 : if (base_binfo == arg)
4367 : break;
4368 26 : write_compact_number (idx);
4369 26 : write_type (BINFO_TYPE (c));
4370 : }
4371 90 : else if (strcmp (prefix, "ds") == 0)
4372 : {
4373 84 : gcc_assert (TREE_CODE (arg) == TREE_VEC);
4374 84 : write_type (TREE_VEC_ELT (arg, 0));
4375 84 : write_char ('_');
4376 84 : if (TREE_VEC_ELT (arg, 1))
4377 80 : write_unqualified_id (TREE_VEC_ELT (arg, 1));
4378 84 : write_char ('_');
4379 84 : if (TREE_VEC_ELT (arg, 2))
4380 2 : write_number (tree_to_shwi (TREE_VEC_ELT (arg, 2)), 0, 10);
4381 84 : write_char ('_');
4382 84 : if (TREE_VEC_ELT (arg, 3))
4383 6 : write_number (tree_to_shwi (TREE_VEC_ELT (arg, 3)), 0, 10);
4384 84 : write_char ('_');
4385 84 : if (integer_nonzerop (TREE_VEC_ELT (arg, 4)))
4386 2 : write_char ('n');
4387 90 : for (int i = 5; i < TREE_VEC_LENGTH (arg); ++i)
4388 6 : write_template_arg (REFLECT_EXPR_HANDLE (TREE_VEC_ELT (arg, i)));
4389 : }
4390 6 : else if (strcmp (prefix, "tt") == 0)
4391 : {
4392 2 : gcc_assert (DECL_TEMPLATE_TEMPLATE_PARM_P (arg));
4393 2 : write_template_template_param (TREE_TYPE (arg));
4394 : }
4395 4 : else if (strcmp (prefix, "de") == 0)
4396 4 : write_expression (arg);
4397 : else
4398 0 : gcc_unreachable ();
4399 : }
4400 :
4401 : /* Mangle a dependent splice.
4402 :
4403 : <splice> ::= DS <expression> [ <template-args> ] E
4404 :
4405 : TODO: This is only a proposed mangling.
4406 : See <https://github.com/itanium-cxx-abi/cxx-abi/issues/208>. */
4407 :
4408 : static void
4409 106 : write_splice (tree sp)
4410 : {
4411 106 : write_string ("DS");
4412 :
4413 106 : if (TREE_CODE (sp) == SPLICE_SCOPE)
4414 62 : sp = SPLICE_SCOPE_EXPR (sp);
4415 106 : gcc_assert (dependent_splice_p (sp));
4416 106 : if (TREE_CODE (sp) == TEMPLATE_ID_EXPR)
4417 : {
4418 8 : write_expression (TREE_OPERAND (TREE_OPERAND (sp, 0), 0));
4419 8 : write_template_args (TREE_OPERAND (sp, 1));
4420 : }
4421 : else
4422 98 : write_expression (TREE_OPERAND (sp, 0));
4423 :
4424 106 : write_char ('E');
4425 106 : }
4426 :
4427 : /* Literal subcase of non-terminal <template-arg>.
4428 :
4429 : "Literal arguments, e.g. "A<42L>", are encoded with their type
4430 : and value. Negative integer values are preceded with "n"; for
4431 : example, "A<-42L>" becomes "1AILln42EE". The bool value false is
4432 : encoded as 0, true as 1." */
4433 :
4434 : static void
4435 94698263 : write_template_arg_literal (const tree value)
4436 : {
4437 94698263 : if (TREE_CODE (value) == STRING_CST)
4438 : /* Temporarily mangle strings as braced initializer lists. */
4439 1667 : write_string ("tl");
4440 : else
4441 94696596 : write_char ('L');
4442 :
4443 94698263 : tree valtype = TREE_TYPE (value);
4444 94698263 : write_type (valtype);
4445 :
4446 : /* Write a null member pointer value as (type)0, regardless of its
4447 : real representation. */
4448 94698263 : if (null_member_pointer_value_p (value))
4449 264 : write_integer_cst (integer_zero_node);
4450 : else
4451 94697999 : switch (TREE_CODE (value))
4452 : {
4453 24320 : case CONST_DECL:
4454 24320 : write_integer_cst (DECL_INITIAL (value));
4455 24320 : break;
4456 :
4457 94667705 : case INTEGER_CST:
4458 94667705 : gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
4459 : || integer_zerop (value) || integer_onep (value));
4460 94667705 : if (!(abi_version_at_least (14)
4461 94660835 : && NULLPTR_TYPE_P (TREE_TYPE (value))))
4462 94667643 : write_integer_cst (value);
4463 : break;
4464 :
4465 2543 : case REAL_CST:
4466 2543 : write_real_cst (value);
4467 2543 : break;
4468 :
4469 6 : case COMPLEX_CST:
4470 6 : if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
4471 6 : && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
4472 : {
4473 3 : write_integer_cst (TREE_REALPART (value));
4474 3 : write_char ('_');
4475 3 : write_integer_cst (TREE_IMAGPART (value));
4476 : }
4477 3 : else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
4478 3 : && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
4479 : {
4480 3 : write_real_cst (TREE_REALPART (value));
4481 3 : write_char ('_');
4482 3 : write_real_cst (TREE_IMAGPART (value));
4483 : }
4484 : else
4485 0 : gcc_unreachable ();
4486 : break;
4487 :
4488 1667 : case STRING_CST:
4489 1667 : {
4490 : /* Mangle strings the same as braced initializer lists. */
4491 1667 : unsigned n = TREE_STRING_LENGTH (value);
4492 1667 : const char *str = TREE_STRING_POINTER (value);
4493 :
4494 : /* Count the number of trailing nuls and subtract them from
4495 : STRSIZE because they don't need to be mangled. */
4496 4638 : for (const char *p = str + n - 1; ; --p)
4497 : {
4498 4638 : if (*p || p == str)
4499 : {
4500 1667 : n -= str + n - !!*p - p;
4501 1667 : break;
4502 : }
4503 : }
4504 1667 : tree eltype = TREE_TYPE (valtype);
4505 11784 : for (const char *p = str; n--; ++p)
4506 : {
4507 10117 : write_char ('L');
4508 10117 : write_type (eltype);
4509 10117 : write_unsigned_number (*(const unsigned char*)p);
4510 10117 : write_string ("E");
4511 : }
4512 : break;
4513 : }
4514 :
4515 1758 : case REFLECT_EXPR:
4516 1758 : write_reflection (value);
4517 1758 : break;
4518 :
4519 0 : default:
4520 0 : gcc_unreachable ();
4521 : }
4522 :
4523 94698263 : write_char ('E');
4524 94698263 : }
4525 :
4526 : /* Non-terminal <template-arg>.
4527 :
4528 : <template-arg> ::= <type> # type
4529 : ::= L <type> </value/ number> E # literal
4530 : ::= LZ <name> E # external name
4531 : ::= X <expression> E # expression */
4532 :
4533 : static void
4534 756183434 : write_template_arg (tree node)
4535 : {
4536 756183434 : enum tree_code code = TREE_CODE (node);
4537 :
4538 756183434 : MANGLE_TRACE_TREE ("template-arg", node);
4539 :
4540 : /* A template template parameter's argument list contains TREE_LIST
4541 : nodes of which the value field is the actual argument. */
4542 756183434 : if (code == TREE_LIST)
4543 : {
4544 0 : node = TREE_VALUE (node);
4545 : /* If it's a decl, deal with its type instead. */
4546 0 : if (DECL_P (node))
4547 : {
4548 0 : node = TREE_TYPE (node);
4549 0 : code = TREE_CODE (node);
4550 : }
4551 : }
4552 :
4553 756183434 : if (VAR_P (node) && DECL_NTTP_OBJECT_P (node))
4554 : /* We want to mangle the argument, not the var we stored it in. */
4555 51669 : node = tparm_object_argument (node);
4556 :
4557 : /* Strip a conversion added by convert_nontype_argument. */
4558 756183434 : if (TREE_CODE (node) == IMPLICIT_CONV_EXPR)
4559 78 : node = TREE_OPERAND (node, 0);
4560 756183434 : if (REFERENCE_REF_P (node))
4561 282 : node = TREE_OPERAND (node, 0);
4562 756183434 : if (TREE_CODE (node) == NOP_EXPR
4563 756183434 : && TYPE_REF_P (TREE_TYPE (node)))
4564 : {
4565 : /* Template parameters can be of reference type. To maintain
4566 : internal consistency, such arguments use a conversion from
4567 : address of object to reference type. */
4568 276 : gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
4569 276 : node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
4570 : }
4571 :
4572 756183434 : if (TREE_CODE (node) == BASELINK
4573 756183434 : && !type_unknown_p (node))
4574 : {
4575 : /* Before v6 we wrongly wrapped a class-scope function in X/E. */
4576 18 : if (abi_check (6))
4577 12 : node = BASELINK_FUNCTIONS (node);
4578 : }
4579 :
4580 756183434 : if (ARGUMENT_PACK_P (node))
4581 : {
4582 : /* Expand the template argument pack. */
4583 10011455 : tree args = ARGUMENT_PACK_ARGS (node);
4584 10011455 : int i, length = TREE_VEC_LENGTH (args);
4585 10011455 : if (abi_check (6))
4586 10011437 : write_char ('J');
4587 : else
4588 18 : write_char ('I');
4589 24490225 : for (i = 0; i < length; ++i)
4590 14478770 : write_template_arg (TREE_VEC_ELT (args, i));
4591 10011455 : write_char ('E');
4592 : }
4593 746171979 : else if (TYPE_P (node))
4594 650281107 : write_type (node);
4595 95890872 : else if (code == TEMPLATE_DECL)
4596 : /* A template appearing as a template arg is a template template arg. */
4597 789099 : write_template_template_arg (node);
4598 94296295 : else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
4599 805478 : || code == CONST_DECL
4600 809212 : || null_member_pointer_value_p (node)
4601 95910837 : || code == REFLECT_EXPR)
4602 94294207 : write_template_arg_literal (node);
4603 807566 : else if (code == EXCESS_PRECISION_EXPR
4604 807566 : && TREE_CODE (TREE_OPERAND (node, 0)) == REAL_CST)
4605 0 : write_template_arg_literal (fold_convert (TREE_TYPE (node),
4606 : TREE_OPERAND (node, 0)));
4607 807566 : else if (DECL_P (node))
4608 : {
4609 295 : write_char ('L');
4610 : /* Until ABI version 3, the underscore before the mangled name
4611 : was incorrectly omitted. */
4612 295 : if (!abi_check (3))
4613 21 : write_char ('Z');
4614 : else
4615 274 : write_string ("_Z");
4616 295 : write_encoding (node);
4617 295 : write_char ('E');
4618 : }
4619 : else
4620 : {
4621 : /* Template arguments may be expressions. */
4622 807271 : write_char ('X');
4623 807271 : write_expression (node);
4624 807271 : write_char ('E');
4625 : }
4626 756183434 : }
4627 :
4628 : /* <template-template-arg>
4629 : ::= <name>
4630 : ::= <substitution> */
4631 :
4632 : static void
4633 789099 : write_template_template_arg (const tree decl)
4634 : {
4635 789099 : MANGLE_TRACE_TREE ("template-template-arg", decl);
4636 :
4637 789099 : if (find_substitution (decl))
4638 : return;
4639 706285 : write_name (decl);
4640 706285 : add_substitution (decl);
4641 : }
4642 :
4643 :
4644 : /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
4645 :
4646 : <array-type> ::= A [</dimension/ number>] _ </element/ type>
4647 : ::= A <expression> _ </element/ type>
4648 :
4649 : "Array types encode the dimension (number of elements) and the
4650 : element type. For variable length arrays, the dimension (but not
4651 : the '_' separator) is omitted."
4652 : Note that for flexible array members, like for other arrays of
4653 : unspecified size, the dimension is also omitted. */
4654 :
4655 : static void
4656 1568479 : write_array_type (const tree type)
4657 : {
4658 1568479 : write_char ('A');
4659 1568479 : if (TYPE_DOMAIN (type))
4660 : {
4661 400197 : tree index_type;
4662 :
4663 400197 : index_type = TYPE_DOMAIN (type);
4664 : /* The INDEX_TYPE gives the upper and lower bounds of the array.
4665 : It's null for flexible array members which have no upper bound
4666 : (this is a change from GCC 5 and prior where such members were
4667 : incorrectly mangled as zero-length arrays). */
4668 400197 : if (tree max = TYPE_MAX_VALUE (index_type))
4669 : {
4670 400197 : if (TREE_CODE (max) == INTEGER_CST)
4671 : {
4672 : /* The ABI specifies that we should mangle the number of
4673 : elements in the array, not the largest allowed index. */
4674 237957 : offset_int wmax = wi::to_offset (max) + 1;
4675 : /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
4676 : number of elements as zero. */
4677 237957 : wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
4678 237957 : gcc_assert (wi::fits_uhwi_p (wmax));
4679 237957 : write_unsigned_number (wmax.to_uhwi ());
4680 : }
4681 : else
4682 : {
4683 162240 : gcc_checking_assert (TREE_CODE (max) == MINUS_EXPR
4684 : && integer_onep (TREE_OPERAND (max, 1)));
4685 162240 : max = TREE_OPERAND (max, 0);
4686 162240 : write_expression (max);
4687 : }
4688 : }
4689 : }
4690 1568479 : write_char ('_');
4691 1568479 : write_type (TREE_TYPE (type));
4692 1568479 : }
4693 :
4694 : /* Non-terminal <pointer-to-member-type> for pointer-to-member
4695 : variables. TYPE is a pointer-to-member POINTER_TYPE.
4696 :
4697 : <pointer-to-member-type> ::= M </class/ type> </member/ type> */
4698 :
4699 : static void
4700 285999 : write_pointer_to_member_type (const tree type)
4701 : {
4702 285999 : write_char ('M');
4703 285999 : write_type (TYPE_PTRMEM_CLASS_TYPE (type));
4704 285999 : write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
4705 285999 : }
4706 :
4707 : /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
4708 : TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
4709 : TEMPLATE_PARM_INDEX.
4710 :
4711 : <template-param> ::= T </parameter/ number> _ */
4712 :
4713 : static void
4714 28985673 : write_template_param (const tree parm)
4715 : {
4716 28985673 : int parm_index;
4717 28985673 : int level;
4718 :
4719 28985673 : MANGLE_TRACE_TREE ("template-parm", parm);
4720 :
4721 28985673 : switch (TREE_CODE (parm))
4722 : {
4723 28202963 : case TEMPLATE_TYPE_PARM:
4724 28202963 : case TEMPLATE_TEMPLATE_PARM:
4725 28202963 : case BOUND_TEMPLATE_TEMPLATE_PARM:
4726 28202963 : parm_index = TEMPLATE_TYPE_IDX (parm);
4727 28202963 : level = TEMPLATE_TYPE_LEVEL (parm);
4728 28202963 : break;
4729 :
4730 782710 : case TEMPLATE_PARM_INDEX:
4731 782710 : parm_index = TEMPLATE_PARM_IDX (parm);
4732 782710 : level = TEMPLATE_PARM_LEVEL (parm);
4733 782710 : break;
4734 :
4735 0 : default:
4736 0 : gcc_unreachable ();
4737 : }
4738 :
4739 28985673 : write_char ('T');
4740 28985673 : if (level > 1)
4741 : {
4742 29348 : if (abi_check (19))
4743 : {
4744 29336 : write_char ('L');
4745 29336 : write_compact_number (level - 1);
4746 : }
4747 : }
4748 : /* NUMBER as it appears in the mangling is (-1)-indexed, with the
4749 : earliest template param denoted by `_'. */
4750 28985673 : write_compact_number (parm_index);
4751 28985673 : }
4752 :
4753 : /* <template-template-param>
4754 : ::= <template-param>
4755 : ::= <substitution> */
4756 :
4757 : static void
4758 631 : write_template_template_param (const tree parm)
4759 : {
4760 631 : tree templ = NULL_TREE;
4761 :
4762 : /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
4763 : template template parameter. The substitution candidate here is
4764 : only the template. */
4765 631 : if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
4766 : {
4767 536 : templ
4768 536 : = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
4769 536 : if (find_substitution (templ))
4770 : return;
4771 : }
4772 :
4773 : /* <template-param> encodes only the template parameter position,
4774 : not its template arguments, which is fine here. */
4775 631 : write_template_param (parm);
4776 631 : if (templ)
4777 536 : add_substitution (templ);
4778 : }
4779 :
4780 : /* Non-terminal <substitution>.
4781 :
4782 : <substitution> ::= S <seq-id> _
4783 : ::= S_ */
4784 :
4785 : static void
4786 179233535 : write_substitution (const int seq_id)
4787 : {
4788 179233535 : MANGLE_TRACE ("substitution", "");
4789 :
4790 179233535 : write_char ('S');
4791 179233535 : if (seq_id > 0)
4792 155971682 : write_number (seq_id - 1, /*unsigned=*/1, 36);
4793 179233535 : write_char ('_');
4794 179233535 : }
4795 :
4796 : /* Start mangling ENTITY. */
4797 :
4798 : static inline void
4799 228092537 : start_mangling (const tree entity)
4800 : {
4801 228092537 : G = {};
4802 228092537 : G.entity = entity;
4803 228092537 : obstack_free (&name_obstack, name_base);
4804 228092537 : mangle_obstack = &name_obstack;
4805 228092537 : name_base = obstack_alloc (&name_obstack, 0);
4806 228092537 : }
4807 :
4808 : /* Done with mangling. Release the data. */
4809 :
4810 : static void
4811 228092537 : finish_mangling_internal (void)
4812 : {
4813 : /* Clear all the substitutions. */
4814 228092537 : vec_safe_truncate (G.substitutions, 0);
4815 :
4816 228092537 : if (G.mod)
4817 7876 : mangle_module_fini ();
4818 :
4819 : /* Null-terminate the string. */
4820 228092537 : write_char ('\0');
4821 228092537 : }
4822 :
4823 :
4824 : /* Like finish_mangling_internal, but return the mangled string. */
4825 :
4826 : static inline const char *
4827 451349 : finish_mangling (void)
4828 : {
4829 451349 : finish_mangling_internal ();
4830 451349 : return (const char *) obstack_finish (mangle_obstack);
4831 : }
4832 :
4833 : /* Like finish_mangling_internal, but return an identifier. */
4834 :
4835 : static tree
4836 227641188 : finish_mangling_get_identifier (void)
4837 : {
4838 227641188 : finish_mangling_internal ();
4839 : /* Don't obstack_finish here, and the next start_mangling will
4840 : remove the identifier. */
4841 227641188 : return get_identifier ((const char *) obstack_base (mangle_obstack));
4842 : }
4843 :
4844 : /* Initialize data structures for mangling. */
4845 :
4846 : void
4847 102674 : init_mangle (void)
4848 : {
4849 102674 : gcc_obstack_init (&name_obstack);
4850 102674 : name_base = obstack_alloc (&name_obstack, 0);
4851 102674 : vec_alloc (G.substitutions, 0);
4852 :
4853 : /* Cache these identifiers for quick comparison when checking for
4854 : standard substitutions. */
4855 102674 : subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
4856 102674 : subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
4857 102674 : subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
4858 102674 : subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
4859 102674 : subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
4860 102674 : subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
4861 102674 : }
4862 :
4863 : /* Generate a mangling for MODULE's global initializer fn. */
4864 :
4865 : tree
4866 2105 : mangle_module_global_init (int module)
4867 : {
4868 2105 : start_mangling (NULL_TREE);
4869 :
4870 2105 : write_string ("_ZGI");
4871 2105 : write_module (module, true);
4872 :
4873 2105 : return finish_mangling_get_identifier ();
4874 : }
4875 :
4876 : /* Generate the mangled name of DECL. */
4877 :
4878 : static tree
4879 221100006 : mangle_decl_string (const tree decl)
4880 : {
4881 221100006 : tree result;
4882 221100006 : tree saved_fn = current_function_decl;
4883 :
4884 : /* We shouldn't be trying to mangle an uninstantiated template. */
4885 221100006 : gcc_assert (!type_dependent_expression_p (decl));
4886 :
4887 221100006 : current_function_decl = NULL_TREE;
4888 221100006 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
4889 :
4890 221100006 : start_mangling (decl);
4891 :
4892 221100006 : if (TREE_CODE (decl) == TYPE_DECL)
4893 328773 : write_type (TREE_TYPE (decl));
4894 : else
4895 220771233 : write_mangled_name (decl, true);
4896 :
4897 221100006 : result = finish_mangling_get_identifier ();
4898 221100006 : if (DEBUG_MANGLE)
4899 : fprintf (stderr, "mangle_decl_string = '%s'\n\n",
4900 : IDENTIFIER_POINTER (result));
4901 :
4902 221100006 : current_function_decl = saved_fn;
4903 221100006 : return result;
4904 221100006 : }
4905 :
4906 : /* Return an identifier for the external mangled name of DECL. */
4907 :
4908 : static tree
4909 165481476 : get_mangled_id (tree decl)
4910 : {
4911 165481476 : tree id = mangle_decl_string (decl);
4912 165481476 : return targetm.mangle_decl_assembler_name (decl, id);
4913 : }
4914 :
4915 : /* Create an identifier for the external mangled name of DECL. */
4916 :
4917 : void
4918 165490105 : mangle_decl (const tree decl)
4919 : {
4920 165490105 : tree id;
4921 165490105 : bool dep;
4922 :
4923 : /* Don't bother mangling uninstantiated templates. */
4924 165490105 : ++processing_template_decl;
4925 165490105 : if (TREE_CODE (decl) == TYPE_DECL)
4926 329260 : dep = dependent_type_p (TREE_TYPE (decl));
4927 : else
4928 328986431 : dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
4929 275370914 : && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
4930 165490105 : --processing_template_decl;
4931 165490105 : if (dep)
4932 : return;
4933 :
4934 : /* During LTO we keep mangled names of TYPE_DECLs for ODR type merging.
4935 : It is not needed to assign names to anonymous namespace, but we use the
4936 : "<anon>" marker to be able to tell if type is C++ ODR type or type
4937 : produced by other language. */
4938 165481963 : if (TREE_CODE (decl) == TYPE_DECL
4939 329260 : && TYPE_STUB_DECL (TREE_TYPE (decl))
4940 165800067 : && !TREE_PUBLIC (TYPE_STUB_DECL (TREE_TYPE (decl))))
4941 487 : id = get_identifier ("<anon>");
4942 : else
4943 : {
4944 165481476 : gcc_assert (TREE_CODE (decl) != TYPE_DECL
4945 : || !no_linkage_check (TREE_TYPE (decl), true));
4946 165481476 : if (abi_version_at_least (10))
4947 165363438 : if (tree fn = decl_function_context (decl))
4948 3596772 : maybe_check_abi_tags (fn, decl);
4949 165481476 : id = get_mangled_id (decl);
4950 : }
4951 165481963 : SET_DECL_ASSEMBLER_NAME (decl, id);
4952 :
4953 165481963 : if (G.need_cxx17_warning
4954 165481963 : && (TREE_PUBLIC (decl) || DECL_REALLY_EXTERN (decl)))
4955 10 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wnoexcept_type,
4956 : "mangled name for %qD will change in C++17 because the "
4957 : "exception specification is part of a function type",
4958 : decl);
4959 :
4960 165481963 : if (id != DECL_NAME (decl)
4961 : /* Don't do this for a fake symbol we aren't going to emit anyway. */
4962 162909005 : && TREE_CODE (decl) != TYPE_DECL
4963 328061708 : && !DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
4964 : {
4965 142017466 : int save_ver = flag_abi_version;
4966 142017466 : tree id2 = NULL_TREE;
4967 :
4968 142017466 : if (!DECL_REALLY_EXTERN (decl))
4969 : {
4970 85654753 : record_mangling (decl, G.need_abi_warning);
4971 :
4972 85654753 : if (!G.need_abi_warning)
4973 : return;
4974 :
4975 38777 : flag_abi_version = flag_abi_compat_version;
4976 38777 : id2 = mangle_decl_string (decl);
4977 38777 : id2 = targetm.mangle_decl_assembler_name (decl, id2);
4978 38777 : flag_abi_version = save_ver;
4979 :
4980 38777 : if (id2 != id)
4981 38661 : note_mangling_alias (decl, id2);
4982 : }
4983 :
4984 56401490 : if (warn_abi)
4985 : {
4986 55579948 : const char fabi_version[] = "-fabi-version";
4987 :
4988 55579948 : if (flag_abi_compat_version != warn_abi_version
4989 55579221 : || id2 == NULL_TREE)
4990 : {
4991 55579753 : flag_abi_version = warn_abi_version;
4992 55579753 : id2 = mangle_decl_string (decl);
4993 55579753 : id2 = targetm.mangle_decl_assembler_name (decl, id2);
4994 : }
4995 55579948 : flag_abi_version = save_ver;
4996 :
4997 55579948 : if (id2 == id)
4998 : /* OK. */;
4999 266 : else if (warn_abi_version != 0
5000 266 : && abi_version_at_least (warn_abi_version))
5001 222 : warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
5002 : "the mangled name of %qD changed between "
5003 : "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
5004 : G.entity, fabi_version, warn_abi_version, id2,
5005 : fabi_version, save_ver, id);
5006 : else
5007 44 : warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
5008 : "the mangled name of %qD changes between "
5009 : "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
5010 : G.entity, fabi_version, save_ver, id,
5011 : fabi_version, warn_abi_version, id2);
5012 : }
5013 :
5014 56401490 : flag_abi_version = save_ver;
5015 : }
5016 : }
5017 :
5018 : /* Generate the mangled representation of TYPE. */
5019 :
5020 : const char *
5021 451349 : mangle_type_string (const tree type)
5022 : {
5023 451349 : const char *result;
5024 :
5025 451349 : start_mangling (type);
5026 451349 : write_type (type);
5027 451349 : result = finish_mangling ();
5028 451349 : if (DEBUG_MANGLE)
5029 : fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
5030 451349 : return result;
5031 : }
5032 :
5033 : /* Create an identifier for the mangled name of a special component
5034 : for belonging to TYPE. CODE is the ABI-specified code for this
5035 : component. */
5036 :
5037 : static tree
5038 5728874 : mangle_special_for_type (const tree type, const char *code)
5039 : {
5040 5728874 : tree result;
5041 :
5042 : /* We don't have an actual decl here for the special component, so
5043 : we can't just process the <encoded-name>. Instead, fake it. */
5044 5728874 : start_mangling (type);
5045 :
5046 : /* Start the mangling. */
5047 5728874 : write_string ("_Z");
5048 5728874 : write_string (code);
5049 :
5050 : /* Add the type. */
5051 5728874 : write_type (type);
5052 5728874 : result = finish_mangling_get_identifier ();
5053 :
5054 5728874 : if (DEBUG_MANGLE)
5055 : fprintf (stderr, "mangle_special_for_type = %s\n\n",
5056 : IDENTIFIER_POINTER (result));
5057 :
5058 5728874 : return result;
5059 : }
5060 :
5061 : /* Create an identifier for the mangled representation of the typeinfo
5062 : structure for TYPE. */
5063 :
5064 : tree
5065 3280281 : mangle_typeinfo_for_type (const tree type)
5066 : {
5067 3280281 : return mangle_special_for_type (type, "TI");
5068 : }
5069 :
5070 : /* Create an identifier for the mangled name of the NTBS containing
5071 : the mangled name of TYPE. */
5072 :
5073 : tree
5074 442894 : mangle_typeinfo_string_for_type (const tree type)
5075 : {
5076 442894 : return mangle_special_for_type (type, "TS");
5077 : }
5078 :
5079 : /* Create an identifier for the mangled name of the vtable for TYPE. */
5080 :
5081 : tree
5082 1821651 : mangle_vtbl_for_type (const tree type)
5083 : {
5084 1821651 : return mangle_special_for_type (type, "TV");
5085 : }
5086 :
5087 : /* Returns an identifier for the mangled name of the VTT for TYPE. */
5088 :
5089 : tree
5090 184048 : mangle_vtt_for_type (const tree type)
5091 : {
5092 184048 : return mangle_special_for_type (type, "TT");
5093 : }
5094 :
5095 : /* Returns an identifier for the mangled name of the decomposition
5096 : artificial variable DECL. DECLS is the vector of the VAR_DECLs
5097 : for the identifier-list. */
5098 :
5099 : tree
5100 897 : mangle_decomp (const tree decl, vec<tree> &decls)
5101 : {
5102 897 : gcc_assert (!type_dependent_expression_p (decl));
5103 :
5104 897 : location_t saved_loc = input_location;
5105 897 : input_location = DECL_SOURCE_LOCATION (decl);
5106 :
5107 897 : check_abi_tags (decl);
5108 897 : start_mangling (decl);
5109 897 : write_string ("_Z");
5110 :
5111 897 : tree context = decl_mangling_context (decl);
5112 897 : gcc_assert (context != NULL_TREE);
5113 :
5114 897 : bool nested = false;
5115 897 : bool local = false;
5116 897 : if (DECL_NAMESPACE_STD_P (context))
5117 9 : write_string ("St");
5118 888 : else if (TREE_CODE (context) == FUNCTION_DECL)
5119 : {
5120 269 : local = true;
5121 269 : write_char ('Z');
5122 269 : write_encoding (context);
5123 269 : write_char ('E');
5124 : }
5125 619 : else if (context != global_namespace)
5126 : {
5127 100 : nested = true;
5128 100 : write_char ('N');
5129 100 : write_prefix (context);
5130 : }
5131 :
5132 897 : write_string ("DC");
5133 897 : unsigned int i;
5134 897 : tree d;
5135 2966 : FOR_EACH_VEC_ELT (decls, i, d)
5136 2069 : write_unqualified_name (d);
5137 897 : write_char ('E');
5138 :
5139 897 : if (tree tags = get_abi_tags (decl))
5140 : {
5141 : /* We didn't emit ABI tags for structured bindings before ABI 19. */
5142 30 : if (!G.need_abi_warning
5143 30 : && TREE_PUBLIC (decl)
5144 120 : && abi_warn_or_compat_version_crosses (19))
5145 30 : G.need_abi_warning = 1;
5146 :
5147 30 : if (abi_version_at_least (19))
5148 30 : write_abi_tags (tags);
5149 : }
5150 :
5151 897 : if (nested)
5152 100 : write_char ('E');
5153 797 : else if (local && DECL_DISCRIMINATOR_P (decl))
5154 269 : write_discriminator (discriminator_for_local_entity (decl));
5155 :
5156 897 : tree id = finish_mangling_get_identifier ();
5157 897 : if (DEBUG_MANGLE)
5158 : fprintf (stderr, "mangle_decomp = '%s'\n\n",
5159 : IDENTIFIER_POINTER (id));
5160 :
5161 897 : input_location = saved_loc;
5162 :
5163 897 : if (warn_abi && G.need_abi_warning)
5164 : {
5165 0 : const char fabi_version[] = "-fabi-version";
5166 0 : tree id2 = id;
5167 0 : int save_ver = flag_abi_version;
5168 :
5169 0 : if (flag_abi_version != warn_abi_version)
5170 : {
5171 0 : flag_abi_version = warn_abi_version;
5172 0 : id2 = mangle_decomp (decl, decls);
5173 0 : flag_abi_version = save_ver;
5174 : }
5175 :
5176 0 : if (id2 == id)
5177 : /* OK. */;
5178 0 : else if (warn_abi_version != 0
5179 0 : && abi_version_at_least (warn_abi_version))
5180 0 : warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
5181 : "the mangled name of %qD changed between "
5182 : "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
5183 : G.entity, fabi_version, warn_abi_version, id2,
5184 : fabi_version, save_ver, id);
5185 : else
5186 0 : warning_at (DECL_SOURCE_LOCATION (G.entity), OPT_Wabi,
5187 : "the mangled name of %qD changes between "
5188 : "%<%s=%d%> (%qD) and %<%s=%d%> (%qD)",
5189 : G.entity, fabi_version, save_ver, id,
5190 : fabi_version, warn_abi_version, id2);
5191 : }
5192 :
5193 897 : return id;
5194 : }
5195 :
5196 : /* Return an identifier for a construction vtable group. TYPE is
5197 : the most derived class in the hierarchy; BINFO is the base
5198 : subobject for which this construction vtable group will be used.
5199 :
5200 : This mangling isn't part of the ABI specification; in the ABI
5201 : specification, the vtable group is dumped in the same COMDAT as the
5202 : main vtable, and is referenced only from that vtable, so it doesn't
5203 : need an external name. For binary formats without COMDAT sections,
5204 : though, we need external names for the vtable groups.
5205 :
5206 : We use the production
5207 :
5208 : <special-name> ::= TC <type> <offset number> _ <base type> */
5209 :
5210 : tree
5211 252739 : mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
5212 : {
5213 252739 : tree result;
5214 :
5215 252739 : start_mangling (type);
5216 :
5217 252739 : write_string ("_Z");
5218 252739 : write_string ("TC");
5219 252739 : write_type (type);
5220 252739 : write_integer_cst (BINFO_OFFSET (binfo));
5221 252739 : write_char ('_');
5222 252739 : write_type (BINFO_TYPE (binfo));
5223 :
5224 252739 : result = finish_mangling_get_identifier ();
5225 252739 : if (DEBUG_MANGLE)
5226 : fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
5227 : IDENTIFIER_POINTER (result));
5228 252739 : return result;
5229 : }
5230 :
5231 : /* Mangle a this pointer or result pointer adjustment.
5232 :
5233 : <call-offset> ::= h <fixed offset number> _
5234 : ::= v <fixed offset number> _ <virtual offset number> _ */
5235 :
5236 : static void
5237 481522 : mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
5238 : {
5239 481522 : write_char (virtual_offset ? 'v' : 'h');
5240 :
5241 : /* For either flavor, write the fixed offset. */
5242 481522 : write_integer_cst (fixed_offset);
5243 481522 : write_char ('_');
5244 :
5245 : /* For a virtual thunk, add the virtual offset. */
5246 481522 : if (virtual_offset)
5247 : {
5248 360516 : write_integer_cst (virtual_offset);
5249 360516 : write_char ('_');
5250 : }
5251 481522 : }
5252 :
5253 : /* Return an identifier for the mangled name of a this-adjusting or
5254 : covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
5255 : to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
5256 : is a virtual thunk, and it is the vtbl offset in
5257 : bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
5258 : zero for a covariant thunk. Note, that FN_DECL might be a covariant
5259 : thunk itself. A covariant thunk name always includes the adjustment
5260 : for the this pointer, even if there is none.
5261 :
5262 : <special-name> ::= T <call-offset> <base encoding>
5263 : ::= Tc <this_adjust call-offset> <result_adjust call-offset>
5264 : <base encoding> */
5265 :
5266 : tree
5267 481135 : mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
5268 : tree virtual_offset, tree thunk)
5269 : {
5270 481135 : tree result;
5271 :
5272 481135 : if (abi_version_at_least (11))
5273 481132 : maybe_check_abi_tags (fn_decl, thunk, 11);
5274 :
5275 481135 : start_mangling (fn_decl);
5276 :
5277 481135 : write_string ("_Z");
5278 481135 : write_char ('T');
5279 :
5280 481135 : if (!this_adjusting)
5281 : {
5282 : /* Covariant thunk with no this adjustment */
5283 217 : write_char ('c');
5284 217 : mangle_call_offset (integer_zero_node, NULL_TREE);
5285 217 : mangle_call_offset (fixed_offset, virtual_offset);
5286 : }
5287 480918 : else if (!DECL_THUNK_P (fn_decl))
5288 : /* Plain this adjusting thunk. */
5289 480748 : mangle_call_offset (fixed_offset, virtual_offset);
5290 : else
5291 : {
5292 : /* This adjusting thunk to covariant thunk. */
5293 170 : write_char ('c');
5294 170 : mangle_call_offset (fixed_offset, virtual_offset);
5295 170 : fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
5296 170 : virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
5297 170 : if (virtual_offset)
5298 124 : virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
5299 170 : mangle_call_offset (fixed_offset, virtual_offset);
5300 170 : fn_decl = THUNK_TARGET (fn_decl);
5301 : }
5302 :
5303 : /* Scoped name. */
5304 481135 : write_encoding (fn_decl);
5305 :
5306 481135 : result = finish_mangling_get_identifier ();
5307 481135 : if (DEBUG_MANGLE)
5308 : fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
5309 481135 : return result;
5310 : }
5311 :
5312 : /* Handle ABI backwards compatibility for past bugs where we didn't call
5313 : check_abi_tags in places where it's needed: call check_abi_tags and warn if
5314 : it makes a difference. If FOR_DECL is non-null, it's the declaration
5315 : that we're actually trying to mangle; if it's null, we're mangling the
5316 : guard variable for T. */
5317 :
5318 : static void
5319 4082732 : maybe_check_abi_tags (tree t, tree for_decl, int ver)
5320 : {
5321 4082732 : if (DECL_ASSEMBLER_NAME_SET_P (t))
5322 : return;
5323 :
5324 806876 : tree oldtags = get_abi_tags (t);
5325 :
5326 806876 : mangle_decl (t);
5327 :
5328 806876 : tree newtags = get_abi_tags (t);
5329 806876 : if (newtags && newtags != oldtags
5330 48 : && abi_version_crosses (ver))
5331 : {
5332 12 : if (for_decl && DECL_THUNK_P (for_decl))
5333 3 : warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
5334 : "the mangled name of a thunk for %qD changes between "
5335 : "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
5336 : t, flag_abi_version, warn_abi_version);
5337 9 : else if (for_decl)
5338 6 : warning_at (DECL_SOURCE_LOCATION (for_decl), OPT_Wabi,
5339 : "the mangled name of %qD changes between "
5340 : "%<-fabi-version=%d%> and %<-fabi-version=%d%>",
5341 : for_decl, flag_abi_version, warn_abi_version);
5342 : else
5343 3 : warning_at (DECL_SOURCE_LOCATION (t), OPT_Wabi,
5344 : "the mangled name of the initialization guard variable "
5345 : "for %qD changes between %<-fabi-version=%d%> and "
5346 : "%<-fabi-version=%d%>",
5347 : t, flag_abi_version, warn_abi_version);
5348 : }
5349 : }
5350 :
5351 : /* Write out the appropriate string for this variable when generating
5352 : another mangled name based on this one. */
5353 :
5354 : static void
5355 7602 : write_guarded_var_name (const tree variable)
5356 : {
5357 7602 : if (DECL_NAME (variable)
5358 7602 : && startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR"))
5359 : /* The name of a guard variable for a reference temporary should refer
5360 : to the reference, not the temporary. */
5361 6 : write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
5362 7596 : else if (DECL_DECOMPOSITION_P (variable)
5363 751 : && DECL_NAME (variable) == NULL_TREE
5364 7868 : && startswith (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (variable)),
5365 : "_Z"))
5366 : /* The name of a guard variable for a structured binding needs special
5367 : casing. */
5368 272 : write_string (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (variable)) + 2);
5369 : else
5370 7324 : write_name (variable);
5371 7602 : }
5372 :
5373 : /* Return an identifier for the name of an initialization guard
5374 : variable for indicated VARIABLE. */
5375 :
5376 : tree
5377 4834 : mangle_guard_variable (const tree variable)
5378 : {
5379 4834 : if (abi_version_at_least (10))
5380 4828 : maybe_check_abi_tags (variable);
5381 4834 : start_mangling (variable);
5382 4834 : write_string ("_ZGV");
5383 4834 : write_guarded_var_name (variable);
5384 4834 : return finish_mangling_get_identifier ();
5385 : }
5386 :
5387 : /* Return an identifier for the name of a thread_local initialization
5388 : function for VARIABLE. */
5389 :
5390 : tree
5391 1190 : mangle_tls_init_fn (const tree variable)
5392 : {
5393 1190 : check_abi_tags (variable);
5394 1190 : start_mangling (variable);
5395 1190 : write_string ("_ZTH");
5396 1190 : write_guarded_var_name (variable);
5397 1190 : return finish_mangling_get_identifier ();
5398 : }
5399 :
5400 : /* Return an identifier for the name of a thread_local wrapper
5401 : function for VARIABLE. */
5402 :
5403 : #define TLS_WRAPPER_PREFIX "_ZTW"
5404 :
5405 : tree
5406 749 : mangle_tls_wrapper_fn (const tree variable)
5407 : {
5408 749 : check_abi_tags (variable);
5409 749 : start_mangling (variable);
5410 749 : write_string (TLS_WRAPPER_PREFIX);
5411 749 : write_guarded_var_name (variable);
5412 749 : return finish_mangling_get_identifier ();
5413 : }
5414 :
5415 : /* Return true iff FN is a thread_local wrapper function. */
5416 :
5417 : bool
5418 2515374 : decl_tls_wrapper_p (const tree fn)
5419 : {
5420 2515374 : if (TREE_CODE (fn) != FUNCTION_DECL)
5421 : return false;
5422 2142058 : tree name = DECL_NAME (fn);
5423 2142058 : return startswith (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX);
5424 : }
5425 :
5426 : /* Return an identifier for the name of a temporary variable used to
5427 : initialize a static reference. This is now part of the ABI. */
5428 :
5429 : tree
5430 829 : mangle_ref_init_variable (const tree variable)
5431 : {
5432 829 : start_mangling (variable);
5433 829 : write_string ("_ZGR");
5434 829 : check_abi_tags (variable);
5435 829 : write_guarded_var_name (variable);
5436 : /* Avoid name clashes with aggregate initialization of multiple
5437 : references at once. */
5438 829 : write_compact_number (current_ref_temp_count++);
5439 829 : return finish_mangling_get_identifier ();
5440 : }
5441 :
5442 : /* Return an identifier for the mangled name of a C++20 template parameter
5443 : object for template argument EXPR. */
5444 :
5445 : tree
5446 67830 : mangle_template_parm_object (tree expr)
5447 : {
5448 67830 : start_mangling (expr);
5449 67830 : write_string ("_ZTAX");
5450 67830 : write_expression (expr);
5451 67830 : write_char ('E');
5452 67830 : return finish_mangling_get_identifier ();
5453 : }
5454 :
5455 : /* Given a CLASS_TYPE, such as a record for std::bad_exception this
5456 : function generates a mangled name for the vtable map variable of
5457 : the class type. For example, if the class type is
5458 : "std::bad_exception", the mangled name for the class is
5459 : "St13bad_exception". This function would generate the name
5460 : "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
5461 : "_VTV<std::bad_exception>::__vtable_map". */
5462 :
5463 :
5464 : char *
5465 6 : get_mangled_vtable_map_var_name (tree class_type)
5466 : {
5467 6 : char *var_name = NULL;
5468 6 : const char *prefix = "_ZN4_VTVI";
5469 6 : const char *postfix = "E12__vtable_mapE";
5470 :
5471 6 : gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
5472 :
5473 6 : tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
5474 :
5475 6 : if (strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL)
5476 : {
5477 0 : class_id = get_mangled_id (TYPE_NAME (class_type));
5478 0 : vtbl_register_mangled_name (TYPE_NAME (class_type), class_id);
5479 : }
5480 :
5481 6 : unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
5482 : strlen (prefix) +
5483 6 : strlen (postfix) + 1;
5484 :
5485 6 : var_name = (char *) xmalloc (len);
5486 :
5487 6 : sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
5488 :
5489 6 : return var_name;
5490 : }
5491 :
5492 : #include "gt-cp-mangle.h"
|