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