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