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