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