PR target/59685
[official-gcc.git] / gcc / cp / mangle.c
blobbe3c698f4b619d0da8dec667db339510fbc9f3e7
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
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.
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.
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/>. */
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.
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.
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:
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 */
47 #include "config.h"
48 #include "system.h"
49 #include "coretypes.h"
50 #include "tm.h"
51 #include "tree.h"
52 #include "stor-layout.h"
53 #include "stringpool.h"
54 #include "tm_p.h"
55 #include "cp-tree.h"
56 #include "obstack.h"
57 #include "flags.h"
58 #include "target.h"
59 #include "cgraph.h"
61 /* Debugging support. */
63 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
64 #ifndef DEBUG_MANGLE
65 #define DEBUG_MANGLE 0
66 #endif
68 /* Macros for tracing the write_* functions. */
69 #if DEBUG_MANGLE
70 # define MANGLE_TRACE(FN, INPUT) \
71 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
72 # define MANGLE_TRACE_TREE(FN, NODE) \
73 fprintf (stderr, " %-24s: %-24s (%p)\n", \
74 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
75 #else
76 # define MANGLE_TRACE(FN, INPUT)
77 # define MANGLE_TRACE_TREE(FN, NODE)
78 #endif
80 /* Nonzero if NODE is a class template-id. We can't rely on
81 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
82 that hard to distinguish A<T> from A, where A<T> is the type as
83 instantiated outside of the template, and A is the type used
84 without parameters inside the template. */
85 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
86 (TYPE_LANG_SPECIFIC (NODE) != NULL \
87 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
88 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
89 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
91 /* Things we only need one of. This module is not reentrant. */
92 typedef struct GTY(()) globals {
93 /* An array of the current substitution candidates, in the order
94 we've seen them. */
95 vec<tree, va_gc> *substitutions;
97 /* The entity that is being mangled. */
98 tree GTY ((skip)) entity;
100 /* How many parameter scopes we are inside. */
101 int parm_depth;
103 /* True if the mangling will be different in a future version of the
104 ABI. */
105 bool need_abi_warning;
106 } globals;
108 static GTY (()) globals G;
110 /* The obstack on which we build mangled names. */
111 static struct obstack *mangle_obstack;
113 /* The obstack on which we build mangled names that are not going to
114 be IDENTIFIER_NODEs. */
115 static struct obstack name_obstack;
117 /* The first object on the name_obstack; we use this to free memory
118 allocated on the name_obstack. */
119 static void *name_base;
121 /* Indices into subst_identifiers. These are identifiers used in
122 special substitution rules. */
123 typedef enum
125 SUBID_ALLOCATOR,
126 SUBID_BASIC_STRING,
127 SUBID_CHAR_TRAITS,
128 SUBID_BASIC_ISTREAM,
129 SUBID_BASIC_OSTREAM,
130 SUBID_BASIC_IOSTREAM,
131 SUBID_MAX
133 substitution_identifier_index_t;
135 /* For quick substitution checks, look up these common identifiers
136 once only. */
137 static GTY(()) tree subst_identifiers[SUBID_MAX];
139 /* Single-letter codes for builtin integer types, defined in
140 <builtin-type>. These are indexed by integer_type_kind values. */
141 static const char
142 integer_type_codes[itk_none] =
144 'c', /* itk_char */
145 'a', /* itk_signed_char */
146 'h', /* itk_unsigned_char */
147 's', /* itk_short */
148 't', /* itk_unsigned_short */
149 'i', /* itk_int */
150 'j', /* itk_unsigned_int */
151 'l', /* itk_long */
152 'm', /* itk_unsigned_long */
153 'x', /* itk_long_long */
154 'y', /* itk_unsigned_long_long */
155 'n', /* itk_int128 */
156 'o', /* itk_unsigned_int128 */
159 static int decl_is_template_id (const tree, tree* const);
161 /* Functions for handling substitutions. */
163 static inline tree canonicalize_for_substitution (tree);
164 static void add_substitution (tree);
165 static inline int is_std_substitution (const tree,
166 const substitution_identifier_index_t);
167 static inline int is_std_substitution_char (const tree,
168 const substitution_identifier_index_t);
169 static int find_substitution (tree);
170 static void mangle_call_offset (const tree, const tree);
172 /* Functions for emitting mangled representations of things. */
174 static void write_mangled_name (const tree, bool);
175 static void write_encoding (const tree);
176 static void write_name (tree, const int);
177 static void write_abi_tags (tree);
178 static void write_unscoped_name (const tree);
179 static void write_unscoped_template_name (const tree);
180 static void write_nested_name (const tree);
181 static void write_prefix (const tree);
182 static void write_template_prefix (const tree);
183 static void write_unqualified_name (const tree);
184 static void write_conversion_operator_name (const tree);
185 static void write_source_name (tree);
186 static void write_literal_operator_name (tree);
187 static void write_unnamed_type_name (const tree);
188 static void write_closure_type_name (const tree);
189 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
190 const unsigned int);
191 static void write_number (unsigned HOST_WIDE_INT, const int,
192 const unsigned int);
193 static void write_compact_number (int num);
194 static void write_integer_cst (const tree);
195 static void write_real_cst (const tree);
196 static void write_identifier (const char *);
197 static void write_special_name_constructor (const tree);
198 static void write_special_name_destructor (const tree);
199 static void write_type (tree);
200 static int write_CV_qualifiers_for_type (const tree);
201 static void write_builtin_type (tree);
202 static void write_function_type (const tree);
203 static void write_bare_function_type (const tree, const int, const tree);
204 static void write_method_parms (tree, const int, const tree);
205 static void write_class_enum_type (const tree);
206 static void write_template_args (tree);
207 static void write_expression (tree);
208 static void write_template_arg_literal (const tree);
209 static void write_template_arg (tree);
210 static void write_template_template_arg (const tree);
211 static void write_array_type (const tree);
212 static void write_pointer_to_member_type (const tree);
213 static void write_template_param (const tree);
214 static void write_template_template_param (const tree);
215 static void write_substitution (const int);
216 static int discriminator_for_local_entity (tree);
217 static int discriminator_for_string_literal (tree, tree);
218 static void write_discriminator (const int);
219 static void write_local_name (tree, const tree, const tree);
220 static void dump_substitution_candidates (void);
221 static tree mangle_decl_string (const tree);
222 static int local_class_index (tree);
224 /* Control functions. */
226 static inline void start_mangling (const tree);
227 static inline const char *finish_mangling (const bool);
228 static tree mangle_special_for_type (const tree, const char *);
230 /* Foreign language functions. */
232 static void write_java_integer_type_codes (const tree);
234 /* Append a single character to the end of the mangled
235 representation. */
236 #define write_char(CHAR) \
237 obstack_1grow (mangle_obstack, (CHAR))
239 /* Append a sized buffer to the end of the mangled representation. */
240 #define write_chars(CHAR, LEN) \
241 obstack_grow (mangle_obstack, (CHAR), (LEN))
243 /* Append a NUL-terminated string to the end of the mangled
244 representation. */
245 #define write_string(STRING) \
246 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
248 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
249 same purpose (context, which may be a type) and value (template
250 decl). See write_template_prefix for more information on what this
251 is used for. */
252 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
253 (TREE_CODE (NODE1) == TREE_LIST \
254 && TREE_CODE (NODE2) == TREE_LIST \
255 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
256 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
257 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
258 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
260 /* Write out an unsigned quantity in base 10. */
261 #define write_unsigned_number(NUMBER) \
262 write_number ((NUMBER), /*unsigned_p=*/1, 10)
264 /* If DECL is a template instance, return nonzero and, if
265 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
266 Otherwise return zero. */
268 static int
269 decl_is_template_id (const tree decl, tree* const template_info)
271 if (TREE_CODE (decl) == TYPE_DECL)
273 /* TYPE_DECLs are handled specially. Look at its type to decide
274 if this is a template instantiation. */
275 const tree type = TREE_TYPE (decl);
277 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
279 if (template_info != NULL)
280 /* For a templated TYPE_DECL, the template info is hanging
281 off the type. */
282 *template_info = TYPE_TEMPLATE_INFO (type);
283 return 1;
286 else
288 /* Check if this is a primary template. */
289 if (DECL_LANG_SPECIFIC (decl) != NULL
290 && DECL_USE_TEMPLATE (decl)
291 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
292 && TREE_CODE (decl) != TEMPLATE_DECL)
294 if (template_info != NULL)
295 /* For most templated decls, the template info is hanging
296 off the decl. */
297 *template_info = DECL_TEMPLATE_INFO (decl);
298 return 1;
302 /* It's not a template id. */
303 return 0;
306 /* Produce debugging output of current substitution candidates. */
308 static void
309 dump_substitution_candidates (void)
311 unsigned i;
312 tree el;
314 fprintf (stderr, " ++ substitutions ");
315 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
317 const char *name = "???";
319 if (i > 0)
320 fprintf (stderr, " ");
321 if (DECL_P (el))
322 name = IDENTIFIER_POINTER (DECL_NAME (el));
323 else if (TREE_CODE (el) == TREE_LIST)
324 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
325 else if (TYPE_NAME (el))
326 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
327 fprintf (stderr, " S%d_ = ", i - 1);
328 if (TYPE_P (el) &&
329 (CP_TYPE_RESTRICT_P (el)
330 || CP_TYPE_VOLATILE_P (el)
331 || CP_TYPE_CONST_P (el)))
332 fprintf (stderr, "CV-");
333 fprintf (stderr, "%s (%s at %p)\n",
334 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
338 /* Both decls and types can be substitution candidates, but sometimes
339 they refer to the same thing. For instance, a TYPE_DECL and
340 RECORD_TYPE for the same class refer to the same thing, and should
341 be treated accordingly in substitutions. This function returns a
342 canonicalized tree node representing NODE that is used when adding
343 and substitution candidates and finding matches. */
345 static inline tree
346 canonicalize_for_substitution (tree node)
348 /* For a TYPE_DECL, use the type instead. */
349 if (TREE_CODE (node) == TYPE_DECL)
350 node = TREE_TYPE (node);
351 if (TYPE_P (node)
352 && TYPE_CANONICAL (node) != node
353 && TYPE_MAIN_VARIANT (node) != node)
355 tree orig = node;
356 /* Here we want to strip the topmost typedef only.
357 We need to do that so is_std_substitution can do proper
358 name matching. */
359 if (TREE_CODE (node) == FUNCTION_TYPE)
360 /* Use build_qualified_type and TYPE_QUALS here to preserve
361 the old buggy mangling of attribute noreturn with abi<5. */
362 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
363 TYPE_QUALS (node));
364 else
365 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
366 cp_type_quals (node));
367 if (TREE_CODE (node) == FUNCTION_TYPE
368 || TREE_CODE (node) == METHOD_TYPE)
369 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
371 return node;
374 /* Add NODE as a substitution candidate. NODE must not already be on
375 the list of candidates. */
377 static void
378 add_substitution (tree node)
380 tree c;
382 if (DEBUG_MANGLE)
383 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
384 get_tree_code_name (TREE_CODE (node)), (void *) node);
386 /* Get the canonicalized substitution candidate for NODE. */
387 c = canonicalize_for_substitution (node);
388 if (DEBUG_MANGLE && c != node)
389 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
390 get_tree_code_name (TREE_CODE (node)), (void *) node);
391 node = c;
393 #if ENABLE_CHECKING
394 /* Make sure NODE isn't already a candidate. */
396 int i;
397 tree candidate;
399 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
401 gcc_assert (!(DECL_P (node) && node == candidate));
402 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
403 && same_type_p (node, candidate)));
406 #endif /* ENABLE_CHECKING */
408 /* Put the decl onto the varray of substitution candidates. */
409 vec_safe_push (G.substitutions, node);
411 if (DEBUG_MANGLE)
412 dump_substitution_candidates ();
415 /* Helper function for find_substitution. Returns nonzero if NODE,
416 which may be a decl or a CLASS_TYPE, is a template-id with template
417 name of substitution_index[INDEX] in the ::std namespace. */
419 static inline int
420 is_std_substitution (const tree node,
421 const substitution_identifier_index_t index)
423 tree type = NULL;
424 tree decl = NULL;
426 if (DECL_P (node))
428 type = TREE_TYPE (node);
429 decl = node;
431 else if (CLASS_TYPE_P (node))
433 type = node;
434 decl = TYPE_NAME (node);
436 else
437 /* These are not the droids you're looking for. */
438 return 0;
440 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
441 && TYPE_LANG_SPECIFIC (type)
442 && TYPE_TEMPLATE_INFO (type)
443 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
444 == subst_identifiers[index]));
447 /* Helper function for find_substitution. Returns nonzero if NODE,
448 which may be a decl or a CLASS_TYPE, is the template-id
449 ::std::identifier<char>, where identifier is
450 substitution_index[INDEX]. */
452 static inline int
453 is_std_substitution_char (const tree node,
454 const substitution_identifier_index_t index)
456 tree args;
457 /* Check NODE's name is ::std::identifier. */
458 if (!is_std_substitution (node, index))
459 return 0;
460 /* Figure out its template args. */
461 if (DECL_P (node))
462 args = DECL_TI_ARGS (node);
463 else if (CLASS_TYPE_P (node))
464 args = CLASSTYPE_TI_ARGS (node);
465 else
466 /* Oops, not a template. */
467 return 0;
468 /* NODE's template arg list should be <char>. */
469 return
470 TREE_VEC_LENGTH (args) == 1
471 && TREE_VEC_ELT (args, 0) == char_type_node;
474 /* Check whether a substitution should be used to represent NODE in
475 the mangling.
477 First, check standard special-case substitutions.
479 <substitution> ::= St
480 # ::std
482 ::= Sa
483 # ::std::allocator
485 ::= Sb
486 # ::std::basic_string
488 ::= Ss
489 # ::std::basic_string<char,
490 ::std::char_traits<char>,
491 ::std::allocator<char> >
493 ::= Si
494 # ::std::basic_istream<char, ::std::char_traits<char> >
496 ::= So
497 # ::std::basic_ostream<char, ::std::char_traits<char> >
499 ::= Sd
500 # ::std::basic_iostream<char, ::std::char_traits<char> >
502 Then examine the stack of currently available substitution
503 candidates for entities appearing earlier in the same mangling
505 If a substitution is found, write its mangled representation and
506 return nonzero. If none is found, just return zero. */
508 static int
509 find_substitution (tree node)
511 int i;
512 const int size = vec_safe_length (G.substitutions);
513 tree decl;
514 tree type;
516 if (DEBUG_MANGLE)
517 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
518 get_tree_code_name (TREE_CODE (node)), (void *) node);
520 /* Obtain the canonicalized substitution representation for NODE.
521 This is what we'll compare against. */
522 node = canonicalize_for_substitution (node);
524 /* Check for builtin substitutions. */
526 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
527 type = TYPE_P (node) ? node : TREE_TYPE (node);
529 /* Check for std::allocator. */
530 if (decl
531 && is_std_substitution (decl, SUBID_ALLOCATOR)
532 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
534 write_string ("Sa");
535 return 1;
538 /* Check for std::basic_string. */
539 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
541 if (TYPE_P (node))
543 /* If this is a type (i.e. a fully-qualified template-id),
544 check for
545 std::basic_string <char,
546 std::char_traits<char>,
547 std::allocator<char> > . */
548 if (cp_type_quals (type) == TYPE_UNQUALIFIED
549 && CLASSTYPE_USE_TEMPLATE (type))
551 tree args = CLASSTYPE_TI_ARGS (type);
552 if (TREE_VEC_LENGTH (args) == 3
553 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
554 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
555 SUBID_CHAR_TRAITS)
556 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
557 SUBID_ALLOCATOR))
559 write_string ("Ss");
560 return 1;
564 else
565 /* Substitute for the template name only if this isn't a type. */
567 write_string ("Sb");
568 return 1;
572 /* Check for basic_{i,o,io}stream. */
573 if (TYPE_P (node)
574 && cp_type_quals (type) == TYPE_UNQUALIFIED
575 && CLASS_TYPE_P (type)
576 && CLASSTYPE_USE_TEMPLATE (type)
577 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
579 /* First, check for the template
580 args <char, std::char_traits<char> > . */
581 tree args = CLASSTYPE_TI_ARGS (type);
582 if (TREE_VEC_LENGTH (args) == 2
583 && TYPE_P (TREE_VEC_ELT (args, 0))
584 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
585 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
586 SUBID_CHAR_TRAITS))
588 /* Got them. Is this basic_istream? */
589 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
591 write_string ("Si");
592 return 1;
594 /* Or basic_ostream? */
595 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
597 write_string ("So");
598 return 1;
600 /* Or basic_iostream? */
601 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
603 write_string ("Sd");
604 return 1;
609 /* Check for namespace std. */
610 if (decl && DECL_NAMESPACE_STD_P (decl))
612 write_string ("St");
613 return 1;
616 /* Now check the list of available substitutions for this mangling
617 operation. */
618 for (i = 0; i < size; ++i)
620 tree candidate = (*G.substitutions)[i];
621 /* NODE is a matched to a candidate if it's the same decl node or
622 if it's the same type. */
623 if (decl == candidate
624 || (TYPE_P (candidate) && type && TYPE_P (node)
625 && same_type_p (type, candidate))
626 || NESTED_TEMPLATE_MATCH (node, candidate))
628 write_substitution (i);
629 return 1;
633 /* No substitution found. */
634 return 0;
638 /* TOP_LEVEL is true, if this is being called at outermost level of
639 mangling. It should be false when mangling a decl appearing in an
640 expression within some other mangling.
642 <mangled-name> ::= _Z <encoding> */
644 static void
645 write_mangled_name (const tree decl, bool top_level)
647 MANGLE_TRACE_TREE ("mangled-name", decl);
649 if (/* The names of `extern "C"' functions are not mangled. */
650 DECL_EXTERN_C_FUNCTION_P (decl)
651 /* But overloaded operator names *are* mangled. */
652 && !DECL_OVERLOADED_OPERATOR_P (decl))
654 unmangled_name:;
656 if (top_level)
657 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
658 else
660 /* The standard notes: "The <encoding> of an extern "C"
661 function is treated like global-scope data, i.e. as its
662 <source-name> without a type." We cannot write
663 overloaded operators that way though, because it contains
664 characters invalid in assembler. */
665 if (abi_version_at_least (2))
666 write_string ("_Z");
667 else
668 G.need_abi_warning = true;
669 write_source_name (DECL_NAME (decl));
672 else if (VAR_P (decl)
673 /* The names of non-static global variables aren't mangled. */
674 && DECL_EXTERNAL_LINKAGE_P (decl)
675 && (CP_DECL_CONTEXT (decl) == global_namespace
676 /* And neither are `extern "C"' variables. */
677 || DECL_EXTERN_C_P (decl)))
679 if (top_level || abi_version_at_least (2))
680 goto unmangled_name;
681 else
683 G.need_abi_warning = true;
684 goto mangled_name;
687 else
689 mangled_name:;
690 write_string ("_Z");
691 write_encoding (decl);
695 /* <encoding> ::= <function name> <bare-function-type>
696 ::= <data name> */
698 static void
699 write_encoding (const tree decl)
701 MANGLE_TRACE_TREE ("encoding", decl);
703 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
705 /* For overloaded operators write just the mangled name
706 without arguments. */
707 if (DECL_OVERLOADED_OPERATOR_P (decl))
708 write_name (decl, /*ignore_local_scope=*/0);
709 else
710 write_source_name (DECL_NAME (decl));
711 return;
714 write_name (decl, /*ignore_local_scope=*/0);
715 if (TREE_CODE (decl) == FUNCTION_DECL)
717 tree fn_type;
718 tree d;
720 if (decl_is_template_id (decl, NULL))
722 fn_type = get_mostly_instantiated_function_type (decl);
723 /* FN_TYPE will not have parameter types for in-charge or
724 VTT parameters. Therefore, we pass NULL_TREE to
725 write_bare_function_type -- otherwise, it will get
726 confused about which artificial parameters to skip. */
727 d = NULL_TREE;
729 else
731 fn_type = TREE_TYPE (decl);
732 d = decl;
735 write_bare_function_type (fn_type,
736 (!DECL_CONSTRUCTOR_P (decl)
737 && !DECL_DESTRUCTOR_P (decl)
738 && !DECL_CONV_FN_P (decl)
739 && decl_is_template_id (decl, NULL)),
744 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
745 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
747 static tree
748 decl_mangling_context (tree decl)
750 tree tcontext = targetm.cxx.decl_mangling_context (decl);
752 if (tcontext != NULL_TREE)
753 return tcontext;
755 if (TREE_CODE (decl) == TYPE_DECL
756 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
758 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
759 if (extra)
760 return extra;
762 else if (TREE_CODE (decl) == TYPE_DECL
763 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
764 /* template type parms have no mangling context. */
765 return NULL_TREE;
766 return CP_DECL_CONTEXT (decl);
769 /* <name> ::= <unscoped-name>
770 ::= <unscoped-template-name> <template-args>
771 ::= <nested-name>
772 ::= <local-name>
774 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
775 called from <local-name>, which mangles the enclosing scope
776 elsewhere and then uses this function to mangle just the part
777 underneath the function scope. So don't use the <local-name>
778 production, to avoid an infinite recursion. */
780 static void
781 write_name (tree decl, const int ignore_local_scope)
783 tree context;
785 MANGLE_TRACE_TREE ("name", decl);
787 if (TREE_CODE (decl) == TYPE_DECL)
789 /* In case this is a typedef, fish out the corresponding
790 TYPE_DECL for the main variant. */
791 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
794 context = decl_mangling_context (decl);
796 gcc_assert (context != NULL_TREE);
798 /* A decl in :: or ::std scope is treated specially. The former is
799 mangled using <unscoped-name> or <unscoped-template-name>, the
800 latter with a special substitution. Also, a name that is
801 directly in a local function scope is also mangled with
802 <unscoped-name> rather than a full <nested-name>. */
803 if (context == global_namespace
804 || DECL_NAMESPACE_STD_P (context)
805 || (ignore_local_scope
806 && (TREE_CODE (context) == FUNCTION_DECL
807 || (abi_version_at_least (7)
808 && TREE_CODE (context) == PARM_DECL))))
810 tree template_info;
811 /* Is this a template instance? */
812 if (decl_is_template_id (decl, &template_info))
814 /* Yes: use <unscoped-template-name>. */
815 write_unscoped_template_name (TI_TEMPLATE (template_info));
816 write_template_args (TI_ARGS (template_info));
818 else
819 /* Everything else gets an <unqualified-name>. */
820 write_unscoped_name (decl);
822 else
824 /* Handle local names, unless we asked not to (that is, invoked
825 under <local-name>, to handle only the part of the name under
826 the local scope). */
827 if (!ignore_local_scope)
829 /* Scan up the list of scope context, looking for a
830 function. If we find one, this entity is in local
831 function scope. local_entity tracks context one scope
832 level down, so it will contain the element that's
833 directly in that function's scope, either decl or one of
834 its enclosing scopes. */
835 tree local_entity = decl;
836 while (context != global_namespace)
838 /* Make sure we're always dealing with decls. */
839 if (TYPE_P (context))
840 context = TYPE_NAME (context);
841 /* Is this a function? */
842 if (TREE_CODE (context) == FUNCTION_DECL
843 || TREE_CODE (context) == PARM_DECL)
845 /* Yes, we have local scope. Use the <local-name>
846 production for the innermost function scope. */
847 write_local_name (context, local_entity, decl);
848 return;
850 /* Up one scope level. */
851 local_entity = context;
852 context = decl_mangling_context (context);
855 /* No local scope found? Fall through to <nested-name>. */
858 /* Other decls get a <nested-name> to encode their scope. */
859 write_nested_name (decl);
863 /* <unscoped-name> ::= <unqualified-name>
864 ::= St <unqualified-name> # ::std:: */
866 static void
867 write_unscoped_name (const tree decl)
869 tree context = decl_mangling_context (decl);
871 MANGLE_TRACE_TREE ("unscoped-name", decl);
873 /* Is DECL in ::std? */
874 if (DECL_NAMESPACE_STD_P (context))
876 write_string ("St");
877 write_unqualified_name (decl);
879 else
881 /* If not, it should be either in the global namespace, or directly
882 in a local function scope. A lambda can also be mangled in the
883 scope of a default argument. */
884 gcc_assert (context == global_namespace
885 || TREE_CODE (context) == PARM_DECL
886 || TREE_CODE (context) == FUNCTION_DECL);
888 write_unqualified_name (decl);
892 /* <unscoped-template-name> ::= <unscoped-name>
893 ::= <substitution> */
895 static void
896 write_unscoped_template_name (const tree decl)
898 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
900 if (find_substitution (decl))
901 return;
902 write_unscoped_name (decl);
903 add_substitution (decl);
906 /* Write the nested name, including CV-qualifiers, of DECL.
908 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
909 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
911 <ref-qualifier> ::= R # & ref-qualifier
912 ::= O # && ref-qualifier
913 <CV-qualifiers> ::= [r] [V] [K] */
915 static void
916 write_nested_name (const tree decl)
918 tree template_info;
920 MANGLE_TRACE_TREE ("nested-name", decl);
922 write_char ('N');
924 /* Write CV-qualifiers, if this is a member function. */
925 if (TREE_CODE (decl) == FUNCTION_DECL
926 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
928 if (DECL_VOLATILE_MEMFUNC_P (decl))
929 write_char ('V');
930 if (DECL_CONST_MEMFUNC_P (decl))
931 write_char ('K');
932 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
934 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
935 write_char ('O');
936 else
937 write_char ('R');
941 /* Is this a template instance? */
942 if (decl_is_template_id (decl, &template_info))
944 /* Yes, use <template-prefix>. */
945 write_template_prefix (decl);
946 write_template_args (TI_ARGS (template_info));
948 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
950 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
951 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
953 write_template_prefix (decl);
954 write_template_args (TREE_OPERAND (name, 1));
956 else
958 write_prefix (decl_mangling_context (decl));
959 write_unqualified_name (decl);
962 else
964 /* No, just use <prefix> */
965 write_prefix (decl_mangling_context (decl));
966 write_unqualified_name (decl);
968 write_char ('E');
971 /* <prefix> ::= <prefix> <unqualified-name>
972 ::= <template-param>
973 ::= <template-prefix> <template-args>
974 ::= <decltype>
975 ::= # empty
976 ::= <substitution> */
978 static void
979 write_prefix (const tree node)
981 tree decl;
982 /* Non-NULL if NODE represents a template-id. */
983 tree template_info = NULL;
985 if (node == NULL
986 || node == global_namespace)
987 return;
989 MANGLE_TRACE_TREE ("prefix", node);
991 if (TREE_CODE (node) == DECLTYPE_TYPE)
993 write_type (node);
994 return;
997 if (find_substitution (node))
998 return;
1000 if (DECL_P (node))
1002 /* If this is a function or parm decl, that means we've hit function
1003 scope, so this prefix must be for a local name. In this
1004 case, we're under the <local-name> production, which encodes
1005 the enclosing function scope elsewhere. So don't continue
1006 here. */
1007 if (TREE_CODE (node) == FUNCTION_DECL
1008 || TREE_CODE (node) == PARM_DECL)
1009 return;
1011 decl = node;
1012 decl_is_template_id (decl, &template_info);
1014 else
1016 /* Node is a type. */
1017 decl = TYPE_NAME (node);
1018 if (CLASSTYPE_TEMPLATE_ID_P (node))
1019 template_info = TYPE_TEMPLATE_INFO (node);
1022 /* In G++ 3.2, the name of the template parameter was used. */
1023 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1024 && !abi_version_at_least (2))
1025 G.need_abi_warning = true;
1027 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1028 && abi_version_at_least (2))
1029 write_template_param (node);
1030 else if (template_info != NULL)
1031 /* Templated. */
1033 write_template_prefix (decl);
1034 write_template_args (TI_ARGS (template_info));
1036 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1038 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1039 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1041 write_template_prefix (decl);
1042 write_template_args (TREE_OPERAND (name, 1));
1044 else
1046 write_prefix (decl_mangling_context (decl));
1047 write_unqualified_name (decl);
1050 else
1051 /* Not templated. */
1053 write_prefix (decl_mangling_context (decl));
1054 write_unqualified_name (decl);
1055 if (VAR_P (decl)
1056 || TREE_CODE (decl) == FIELD_DECL)
1058 /* <data-member-prefix> := <member source-name> M */
1059 write_char ('M');
1060 return;
1064 add_substitution (node);
1067 /* <template-prefix> ::= <prefix> <template component>
1068 ::= <template-param>
1069 ::= <substitution> */
1071 static void
1072 write_template_prefix (const tree node)
1074 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1075 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1076 tree context = decl_mangling_context (decl);
1077 tree template_info;
1078 tree templ;
1079 tree substitution;
1081 MANGLE_TRACE_TREE ("template-prefix", node);
1083 /* Find the template decl. */
1084 if (decl_is_template_id (decl, &template_info))
1085 templ = TI_TEMPLATE (template_info);
1086 else if (TREE_CODE (type) == TYPENAME_TYPE)
1087 /* For a typename type, all we have is the name. */
1088 templ = DECL_NAME (decl);
1089 else
1091 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1093 templ = TYPE_TI_TEMPLATE (type);
1096 /* For a member template, though, the template name for the
1097 innermost name must have all the outer template levels
1098 instantiated. For instance, consider
1100 template<typename T> struct Outer {
1101 template<typename U> struct Inner {};
1104 The template name for `Inner' in `Outer<int>::Inner<float>' is
1105 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1106 levels separately, so there's no TEMPLATE_DECL available for this
1107 (there's only `Outer<T>::Inner<U>').
1109 In order to get the substitutions right, we create a special
1110 TREE_LIST to represent the substitution candidate for a nested
1111 template. The TREE_PURPOSE is the template's context, fully
1112 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1113 template.
1115 So, for the example above, `Outer<int>::Inner' is represented as a
1116 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1117 and whose value is `Outer<T>::Inner<U>'. */
1118 if (TYPE_P (context))
1119 substitution = build_tree_list (context, templ);
1120 else
1121 substitution = templ;
1123 if (find_substitution (substitution))
1124 return;
1126 /* In G++ 3.2, the name of the template template parameter was used. */
1127 if (TREE_TYPE (templ)
1128 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1129 && !abi_version_at_least (2))
1130 G.need_abi_warning = true;
1132 if (TREE_TYPE (templ)
1133 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1134 && abi_version_at_least (2))
1135 write_template_param (TREE_TYPE (templ));
1136 else
1138 write_prefix (context);
1139 write_unqualified_name (decl);
1142 add_substitution (substitution);
1145 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1146 mangled through special entry points.
1148 <unqualified-name> ::= <operator-name>
1149 ::= <special-name>
1150 ::= <source-name>
1151 ::= <unnamed-type-name>
1152 ::= <local-source-name>
1154 <local-source-name> ::= L <source-name> <discriminator> */
1156 static void
1157 write_unqualified_id (tree identifier)
1159 if (IDENTIFIER_TYPENAME_P (identifier))
1160 write_conversion_operator_name (TREE_TYPE (identifier));
1161 else if (IDENTIFIER_OPNAME_P (identifier))
1163 int i;
1164 const char *mangled_name = NULL;
1166 /* Unfortunately, there is no easy way to go from the
1167 name of the operator back to the corresponding tree
1168 code. */
1169 for (i = 0; i < MAX_TREE_CODES; ++i)
1170 if (operator_name_info[i].identifier == identifier)
1172 /* The ABI says that we prefer binary operator
1173 names to unary operator names. */
1174 if (operator_name_info[i].arity == 2)
1176 mangled_name = operator_name_info[i].mangled_name;
1177 break;
1179 else if (!mangled_name)
1180 mangled_name = operator_name_info[i].mangled_name;
1182 else if (assignment_operator_name_info[i].identifier
1183 == identifier)
1185 mangled_name
1186 = assignment_operator_name_info[i].mangled_name;
1187 break;
1189 write_string (mangled_name);
1191 else if (UDLIT_OPER_P (identifier))
1192 write_literal_operator_name (identifier);
1193 else
1194 write_source_name (identifier);
1197 static void
1198 write_unqualified_name (const tree decl)
1200 MANGLE_TRACE_TREE ("unqualified-name", decl);
1202 if (identifier_p (decl))
1204 write_unqualified_id (decl);
1205 return;
1208 bool found = false;
1210 if (DECL_NAME (decl) == NULL_TREE)
1212 found = true;
1213 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1214 write_source_name (DECL_ASSEMBLER_NAME (decl));
1216 else if (DECL_DECLARES_FUNCTION_P (decl))
1218 found = true;
1219 if (DECL_CONSTRUCTOR_P (decl))
1220 write_special_name_constructor (decl);
1221 else if (DECL_DESTRUCTOR_P (decl))
1222 write_special_name_destructor (decl);
1223 else if (DECL_CONV_FN_P (decl))
1225 /* Conversion operator. Handle it right here.
1226 <operator> ::= cv <type> */
1227 tree type;
1228 if (decl_is_template_id (decl, NULL))
1230 tree fn_type;
1231 fn_type = get_mostly_instantiated_function_type (decl);
1232 type = TREE_TYPE (fn_type);
1234 else
1235 type = DECL_CONV_FN_TYPE (decl);
1236 write_conversion_operator_name (type);
1238 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1240 operator_name_info_t *oni;
1241 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1242 oni = assignment_operator_name_info;
1243 else
1244 oni = operator_name_info;
1246 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1248 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1249 write_literal_operator_name (DECL_NAME (decl));
1250 else
1251 found = false;
1254 if (found)
1255 /* OK */;
1256 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1257 && DECL_NAMESPACE_SCOPE_P (decl)
1258 && decl_linkage (decl) == lk_internal)
1260 MANGLE_TRACE_TREE ("local-source-name", decl);
1261 write_char ('L');
1262 write_source_name (DECL_NAME (decl));
1263 /* The default discriminator is 1, and that's all we ever use,
1264 so there's no code to output one here. */
1266 else
1268 tree type = TREE_TYPE (decl);
1270 if (TREE_CODE (decl) == TYPE_DECL
1271 && TYPE_ANONYMOUS_P (type))
1272 write_unnamed_type_name (type);
1273 else if (TREE_CODE (decl) == TYPE_DECL
1274 && LAMBDA_TYPE_P (type))
1275 write_closure_type_name (type);
1276 else
1277 write_source_name (DECL_NAME (decl));
1280 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1281 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1282 : DECL_ATTRIBUTES (decl));
1283 write_abi_tags (lookup_attribute ("abi_tag", attrs));
1286 /* Write the unqualified-name for a conversion operator to TYPE. */
1288 static void
1289 write_conversion_operator_name (const tree type)
1291 write_string ("cv");
1292 write_type (type);
1295 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1297 <source-name> ::= </length/ number> <identifier> */
1299 static void
1300 write_source_name (tree identifier)
1302 MANGLE_TRACE_TREE ("source-name", identifier);
1304 /* Never write the whole template-id name including the template
1305 arguments; we only want the template name. */
1306 if (IDENTIFIER_TEMPLATE (identifier))
1307 identifier = IDENTIFIER_TEMPLATE (identifier);
1309 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1310 write_identifier (IDENTIFIER_POINTER (identifier));
1313 /* Compare two TREE_STRINGs like strcmp. */
1316 tree_string_cmp (const void *p1, const void *p2)
1318 if (p1 == p2)
1319 return 0;
1320 tree s1 = *(const tree*)p1;
1321 tree s2 = *(const tree*)p2;
1322 return strcmp (TREE_STRING_POINTER (s1),
1323 TREE_STRING_POINTER (s2));
1326 /* ID is the name of a function or type with abi_tags attribute TAGS.
1327 Write out the name, suitably decorated. */
1329 static void
1330 write_abi_tags (tree tags)
1332 if (tags == NULL_TREE)
1333 return;
1335 tags = TREE_VALUE (tags);
1337 vec<tree, va_gc> * vec = make_tree_vector();
1339 for (tree t = tags; t; t = TREE_CHAIN (t))
1341 if (ABI_TAG_IMPLICIT (t))
1342 continue;
1343 tree str = TREE_VALUE (t);
1344 vec_safe_push (vec, str);
1347 vec->qsort (tree_string_cmp);
1349 unsigned i; tree str;
1350 FOR_EACH_VEC_ELT (*vec, i, str)
1352 write_string ("B");
1353 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1354 write_identifier (TREE_STRING_POINTER (str));
1357 release_tree_vector (vec);
1360 /* Write a user-defined literal operator.
1361 ::= li <source-name> # "" <source-name>
1362 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1364 static void
1365 write_literal_operator_name (tree identifier)
1367 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1368 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1369 write_unsigned_number (strlen (suffix));
1370 write_identifier (suffix);
1373 /* Encode 0 as _, and 1+ as n-1_. */
1375 static void
1376 write_compact_number (int num)
1378 if (num > 0)
1379 write_unsigned_number (num - 1);
1380 write_char ('_');
1383 /* Return how many unnamed types precede TYPE in its enclosing class. */
1385 static int
1386 nested_anon_class_index (tree type)
1388 int index = 0;
1389 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1390 for (; member; member = DECL_CHAIN (member))
1391 if (DECL_IMPLICIT_TYPEDEF_P (member))
1393 tree memtype = TREE_TYPE (member);
1394 if (memtype == type)
1395 return index;
1396 else if (TYPE_ANONYMOUS_P (memtype))
1397 ++index;
1400 gcc_unreachable ();
1403 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1405 static void
1406 write_unnamed_type_name (const tree type)
1408 int discriminator;
1409 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1411 if (TYPE_FUNCTION_SCOPE_P (type))
1412 discriminator = local_class_index (type);
1413 else if (TYPE_CLASS_SCOPE_P (type))
1414 discriminator = nested_anon_class_index (type);
1415 else
1417 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1418 /* Just use the old mangling at namespace scope. */
1419 write_source_name (TYPE_IDENTIFIER (type));
1420 return;
1423 write_string ("Ut");
1424 write_compact_number (discriminator);
1427 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1428 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1430 static void
1431 write_closure_type_name (const tree type)
1433 tree fn = lambda_function (type);
1434 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1435 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1437 MANGLE_TRACE_TREE ("closure-type-name", type);
1439 write_string ("Ul");
1440 write_method_parms (parms, /*method_p=*/1, fn);
1441 write_char ('E');
1442 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1445 /* Convert NUMBER to ascii using base BASE and generating at least
1446 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1447 into which to store the characters. Returns the number of
1448 characters generated (these will be laid out in advance of where
1449 BUFFER points). */
1451 static int
1452 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1453 char *buffer, const unsigned int min_digits)
1455 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1456 unsigned digits = 0;
1458 while (number)
1460 unsigned HOST_WIDE_INT d = number / base;
1462 *--buffer = base_digits[number - d * base];
1463 digits++;
1464 number = d;
1466 while (digits < min_digits)
1468 *--buffer = base_digits[0];
1469 digits++;
1471 return digits;
1474 /* Non-terminal <number>.
1476 <number> ::= [n] </decimal integer/> */
1478 static void
1479 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1480 const unsigned int base)
1482 char buffer[sizeof (HOST_WIDE_INT) * 8];
1483 unsigned count = 0;
1485 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1487 write_char ('n');
1488 number = -((HOST_WIDE_INT) number);
1490 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1491 write_chars (buffer + sizeof (buffer) - count, count);
1494 /* Write out an integral CST in decimal. Most numbers are small, and
1495 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1496 bigger than that, which we must deal with. */
1498 static inline void
1499 write_integer_cst (const tree cst)
1501 int sign = tree_int_cst_sgn (cst);
1503 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1505 /* A bignum. We do this in chunks, each of which fits in a
1506 HOST_WIDE_INT. */
1507 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1508 unsigned HOST_WIDE_INT chunk;
1509 unsigned chunk_digits;
1510 char *ptr = buffer + sizeof (buffer);
1511 unsigned count = 0;
1512 tree n, base, type;
1513 int done;
1515 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1516 representable. */
1517 chunk = 1000000000;
1518 chunk_digits = 9;
1520 if (sizeof (HOST_WIDE_INT) >= 8)
1522 /* It is at least 64 bits, so 10^18 is representable. */
1523 chunk_digits = 18;
1524 chunk *= chunk;
1527 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1528 base = build_int_cstu (type, chunk);
1529 n = build_int_cst_wide (type,
1530 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1532 if (sign < 0)
1534 write_char ('n');
1535 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1539 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1540 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1541 unsigned c;
1543 done = integer_zerop (d);
1544 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1545 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1546 done ? 1 : chunk_digits);
1547 ptr -= c;
1548 count += c;
1549 n = d;
1551 while (!done);
1552 write_chars (ptr, count);
1554 else
1556 /* A small num. */
1557 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1559 if (sign < 0)
1561 write_char ('n');
1562 low = -low;
1564 write_unsigned_number (low);
1568 /* Write out a floating-point literal.
1570 "Floating-point literals are encoded using the bit pattern of the
1571 target processor's internal representation of that number, as a
1572 fixed-length lowercase hexadecimal string, high-order bytes first
1573 (even if the target processor would store low-order bytes first).
1574 The "n" prefix is not used for floating-point literals; the sign
1575 bit is encoded with the rest of the number.
1577 Here are some examples, assuming the IEEE standard representation
1578 for floating point numbers. (Spaces are for readability, not
1579 part of the encoding.)
1581 1.0f Lf 3f80 0000 E
1582 -1.0f Lf bf80 0000 E
1583 1.17549435e-38f Lf 0080 0000 E
1584 1.40129846e-45f Lf 0000 0001 E
1585 0.0f Lf 0000 0000 E"
1587 Caller is responsible for the Lx and the E. */
1588 static void
1589 write_real_cst (const tree value)
1591 if (abi_version_at_least (2))
1593 long target_real[4]; /* largest supported float */
1594 char buffer[9]; /* eight hex digits in a 32-bit number */
1595 int i, limit, dir;
1597 tree type = TREE_TYPE (value);
1598 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1600 real_to_target (target_real, &TREE_REAL_CST (value),
1601 TYPE_MODE (type));
1603 /* The value in target_real is in the target word order,
1604 so we must write it out backward if that happens to be
1605 little-endian. write_number cannot be used, it will
1606 produce uppercase. */
1607 if (FLOAT_WORDS_BIG_ENDIAN)
1608 i = 0, limit = words, dir = 1;
1609 else
1610 i = words - 1, limit = -1, dir = -1;
1612 for (; i != limit; i += dir)
1614 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1615 write_chars (buffer, 8);
1618 else
1620 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1621 literally. Note that compatibility with 3.2 is impossible,
1622 because the old floating-point emulator used a different
1623 format for REAL_VALUE_TYPE. */
1624 size_t i;
1625 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1626 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1627 /*unsigned_p*/ 1,
1628 /*base*/ 16);
1629 G.need_abi_warning = 1;
1633 /* Non-terminal <identifier>.
1635 <identifier> ::= </unqualified source code identifier> */
1637 static void
1638 write_identifier (const char *identifier)
1640 MANGLE_TRACE ("identifier", identifier);
1641 write_string (identifier);
1644 /* Handle constructor productions of non-terminal <special-name>.
1645 CTOR is a constructor FUNCTION_DECL.
1647 <special-name> ::= C1 # complete object constructor
1648 ::= C2 # base object constructor
1649 ::= C3 # complete object allocating constructor
1651 Currently, allocating constructors are never used. */
1653 static void
1654 write_special_name_constructor (const tree ctor)
1656 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1657 write_string ("C2");
1658 /* This is the old-style "[unified]" constructor.
1659 In some cases, we may emit this function and call
1660 it from the clones in order to share code and save space. */
1661 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1662 write_string ("C4");
1663 else
1665 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1666 write_string ("C1");
1670 /* Handle destructor productions of non-terminal <special-name>.
1671 DTOR is a destructor FUNCTION_DECL.
1673 <special-name> ::= D0 # deleting (in-charge) destructor
1674 ::= D1 # complete object (in-charge) destructor
1675 ::= D2 # base object (not-in-charge) destructor */
1677 static void
1678 write_special_name_destructor (const tree dtor)
1680 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1681 write_string ("D0");
1682 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1683 write_string ("D2");
1684 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1685 /* This is the old-style "[unified]" destructor.
1686 In some cases, we may emit this function and call
1687 it from the clones in order to share code and save space. */
1688 write_string ("D4");
1689 else
1691 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1692 write_string ("D1");
1696 /* Scan the vector of local classes and return how many others with the
1697 same name (or same no name) and context precede ENTITY. */
1699 static int
1700 local_class_index (tree entity)
1702 int ix, discriminator = 0;
1703 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1704 : TYPE_IDENTIFIER (entity));
1705 tree ctx = TYPE_CONTEXT (entity);
1706 for (ix = 0; ; ix++)
1708 tree type = (*local_classes)[ix];
1709 if (type == entity)
1710 return discriminator;
1711 if (TYPE_CONTEXT (type) == ctx
1712 && (name ? TYPE_IDENTIFIER (type) == name
1713 : TYPE_ANONYMOUS_P (type)))
1714 ++discriminator;
1716 gcc_unreachable ();
1719 /* Return the discriminator for ENTITY appearing inside
1720 FUNCTION. The discriminator is the lexical ordinal of VAR among
1721 entities with the same name in the same FUNCTION. */
1723 static int
1724 discriminator_for_local_entity (tree entity)
1726 if (DECL_DISCRIMINATOR_P (entity))
1728 if (DECL_DISCRIMINATOR_SET_P (entity))
1729 return DECL_DISCRIMINATOR (entity);
1730 else
1731 /* The first entity with a particular name doesn't get
1732 DECL_DISCRIMINATOR set up. */
1733 return 0;
1735 else if (TREE_CODE (entity) == TYPE_DECL)
1737 /* Scan the list of local classes. */
1738 entity = TREE_TYPE (entity);
1740 /* Lambdas and unnamed types have their own discriminators. */
1741 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1742 return 0;
1744 return local_class_index (entity);
1746 else
1747 gcc_unreachable ();
1750 /* Return the discriminator for STRING, a string literal used inside
1751 FUNCTION. The discriminator is the lexical ordinal of STRING among
1752 string literals used in FUNCTION. */
1754 static int
1755 discriminator_for_string_literal (tree /*function*/,
1756 tree /*string*/)
1758 /* For now, we don't discriminate amongst string literals. */
1759 return 0;
1762 /* <discriminator> := _ <number>
1764 The discriminator is used only for the second and later occurrences
1765 of the same name within a single function. In this case <number> is
1766 n - 2, if this is the nth occurrence, in lexical order. */
1768 static void
1769 write_discriminator (const int discriminator)
1771 /* If discriminator is zero, don't write anything. Otherwise... */
1772 if (discriminator > 0)
1774 write_char ('_');
1775 write_unsigned_number (discriminator - 1);
1779 /* Mangle the name of a function-scope entity. FUNCTION is the
1780 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1781 default argument scope. ENTITY is the decl for the entity itself.
1782 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1783 either ENTITY itself or an enclosing scope of ENTITY.
1785 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1786 := Z <function encoding> E s [<discriminator>]
1787 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1789 static void
1790 write_local_name (tree function, const tree local_entity,
1791 const tree entity)
1793 tree parm = NULL_TREE;
1795 MANGLE_TRACE_TREE ("local-name", entity);
1797 if (TREE_CODE (function) == PARM_DECL)
1799 parm = function;
1800 function = DECL_CONTEXT (parm);
1803 write_char ('Z');
1804 write_encoding (function);
1805 write_char ('E');
1807 /* For this purpose, parameters are numbered from right-to-left. */
1808 if (parm)
1810 tree t;
1811 int i = 0;
1812 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1814 if (t == parm)
1815 i = 1;
1816 else if (i)
1817 ++i;
1819 write_char ('d');
1820 write_compact_number (i - 1);
1823 if (TREE_CODE (entity) == STRING_CST)
1825 write_char ('s');
1826 write_discriminator (discriminator_for_string_literal (function,
1827 entity));
1829 else
1831 /* Now the <entity name>. Let write_name know its being called
1832 from <local-name>, so it doesn't try to process the enclosing
1833 function scope again. */
1834 write_name (entity, /*ignore_local_scope=*/1);
1835 write_discriminator (discriminator_for_local_entity (local_entity));
1839 /* Non-terminals <type> and <CV-qualifier>.
1841 <type> ::= <builtin-type>
1842 ::= <function-type>
1843 ::= <class-enum-type>
1844 ::= <array-type>
1845 ::= <pointer-to-member-type>
1846 ::= <template-param>
1847 ::= <substitution>
1848 ::= <CV-qualifier>
1849 ::= P <type> # pointer-to
1850 ::= R <type> # reference-to
1851 ::= C <type> # complex pair (C 2000)
1852 ::= G <type> # imaginary (C 2000) [not supported]
1853 ::= U <source-name> <type> # vendor extended type qualifier
1855 C++0x extensions
1857 <type> ::= RR <type> # rvalue reference-to
1858 <type> ::= Dt <expression> # decltype of an id-expression or
1859 # class member access
1860 <type> ::= DT <expression> # decltype of an expression
1861 <type> ::= Dn # decltype of nullptr
1863 TYPE is a type node. */
1865 static void
1866 write_type (tree type)
1868 /* This gets set to nonzero if TYPE turns out to be a (possibly
1869 CV-qualified) builtin type. */
1870 int is_builtin_type = 0;
1872 MANGLE_TRACE_TREE ("type", type);
1874 if (type == error_mark_node)
1875 return;
1877 type = canonicalize_for_substitution (type);
1878 if (find_substitution (type))
1879 return;
1882 if (write_CV_qualifiers_for_type (type) > 0)
1883 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1884 mangle the unqualified type. The recursive call is needed here
1885 since both the qualified and unqualified types are substitution
1886 candidates. */
1888 tree t = TYPE_MAIN_VARIANT (type);
1889 if (TREE_CODE (t) == FUNCTION_TYPE
1890 || TREE_CODE (t) == METHOD_TYPE)
1892 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1893 if (abi_version_at_least (8))
1894 /* Avoid adding the unqualified function type as a substitution. */
1895 write_function_type (t);
1896 else
1897 write_type (t);
1899 else
1900 write_type (t);
1902 else if (TREE_CODE (type) == ARRAY_TYPE)
1903 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1904 so that the cv-qualification of the element type is available
1905 in write_array_type. */
1906 write_array_type (type);
1907 else
1909 tree type_orig = type;
1911 /* See through any typedefs. */
1912 type = TYPE_MAIN_VARIANT (type);
1913 if (TREE_CODE (type) == FUNCTION_TYPE
1914 || TREE_CODE (type) == METHOD_TYPE)
1915 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1917 /* According to the C++ ABI, some library classes are passed the
1918 same as the scalar type of their single member and use the same
1919 mangling. */
1920 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1921 type = TREE_TYPE (first_field (type));
1923 if (TYPE_PTRDATAMEM_P (type))
1924 write_pointer_to_member_type (type);
1925 else
1927 /* Handle any target-specific fundamental types. */
1928 const char *target_mangling
1929 = targetm.mangle_type (type_orig);
1931 if (target_mangling)
1933 write_string (target_mangling);
1934 /* Add substitutions for types other than fundamental
1935 types. */
1936 if (!VOID_TYPE_P (type)
1937 && TREE_CODE (type) != INTEGER_TYPE
1938 && TREE_CODE (type) != REAL_TYPE
1939 && TREE_CODE (type) != BOOLEAN_TYPE)
1940 add_substitution (type);
1941 return;
1944 switch (TREE_CODE (type))
1946 case VOID_TYPE:
1947 case BOOLEAN_TYPE:
1948 case INTEGER_TYPE: /* Includes wchar_t. */
1949 case REAL_TYPE:
1950 case FIXED_POINT_TYPE:
1952 /* If this is a typedef, TYPE may not be one of
1953 the standard builtin type nodes, but an alias of one. Use
1954 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1955 write_builtin_type (TYPE_MAIN_VARIANT (type));
1956 ++is_builtin_type;
1958 break;
1960 case COMPLEX_TYPE:
1961 write_char ('C');
1962 write_type (TREE_TYPE (type));
1963 break;
1965 case FUNCTION_TYPE:
1966 case METHOD_TYPE:
1967 write_function_type (type);
1968 break;
1970 case UNION_TYPE:
1971 case RECORD_TYPE:
1972 case ENUMERAL_TYPE:
1973 /* A pointer-to-member function is represented as a special
1974 RECORD_TYPE, so check for this first. */
1975 if (TYPE_PTRMEMFUNC_P (type))
1976 write_pointer_to_member_type (type);
1977 else
1978 write_class_enum_type (type);
1979 break;
1981 case TYPENAME_TYPE:
1982 case UNBOUND_CLASS_TEMPLATE:
1983 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1984 ordinary nested names. */
1985 write_nested_name (TYPE_STUB_DECL (type));
1986 break;
1988 case POINTER_TYPE:
1989 case REFERENCE_TYPE:
1990 if (TYPE_PTR_P (type))
1991 write_char ('P');
1992 else if (TYPE_REF_IS_RVALUE (type))
1993 write_char ('O');
1994 else
1995 write_char ('R');
1997 tree target = TREE_TYPE (type);
1998 /* Attribute const/noreturn are not reflected in mangling.
1999 We strip them here rather than at a lower level because
2000 a typedef or template argument can have function type
2001 with function-cv-quals (that use the same representation),
2002 but you can't have a pointer/reference to such a type. */
2003 if (abi_version_at_least (5)
2004 && TREE_CODE (target) == FUNCTION_TYPE)
2005 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2006 write_type (target);
2008 break;
2010 case TEMPLATE_TYPE_PARM:
2011 if (is_auto (type))
2013 if (AUTO_IS_DECLTYPE (type))
2014 write_identifier ("Dc");
2015 else
2016 write_identifier ("Da");
2017 ++is_builtin_type;
2018 break;
2020 /* else fall through. */
2021 case TEMPLATE_PARM_INDEX:
2022 write_template_param (type);
2023 break;
2025 case TEMPLATE_TEMPLATE_PARM:
2026 write_template_template_param (type);
2027 break;
2029 case BOUND_TEMPLATE_TEMPLATE_PARM:
2030 write_template_template_param (type);
2031 write_template_args
2032 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2033 break;
2035 case VECTOR_TYPE:
2036 if (abi_version_at_least (4))
2038 write_string ("Dv");
2039 /* Non-constant vector size would be encoded with
2040 _ expression, but we don't support that yet. */
2041 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2042 write_char ('_');
2044 else
2046 G.need_abi_warning = 1;
2047 write_string ("U8__vector");
2049 write_type (TREE_TYPE (type));
2050 break;
2052 case TYPE_PACK_EXPANSION:
2053 write_string ("Dp");
2054 write_type (PACK_EXPANSION_PATTERN (type));
2055 break;
2057 case DECLTYPE_TYPE:
2058 /* These shouldn't make it into mangling. */
2059 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2060 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2062 /* In ABI <5, we stripped decltype of a plain decl. */
2063 if (!abi_version_at_least (5)
2064 && DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2066 tree expr = DECLTYPE_TYPE_EXPR (type);
2067 tree etype = NULL_TREE;
2068 switch (TREE_CODE (expr))
2070 case VAR_DECL:
2071 case PARM_DECL:
2072 case RESULT_DECL:
2073 case FUNCTION_DECL:
2074 case CONST_DECL:
2075 case TEMPLATE_PARM_INDEX:
2076 etype = TREE_TYPE (expr);
2077 break;
2079 default:
2080 break;
2083 if (etype && !type_uses_auto (etype))
2085 G.need_abi_warning = 1;
2086 write_type (etype);
2087 return;
2091 write_char ('D');
2092 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2093 write_char ('t');
2094 else
2095 write_char ('T');
2096 ++cp_unevaluated_operand;
2097 write_expression (DECLTYPE_TYPE_EXPR (type));
2098 --cp_unevaluated_operand;
2099 write_char ('E');
2100 break;
2102 case NULLPTR_TYPE:
2103 write_string ("Dn");
2104 if (abi_version_at_least (7))
2105 ++is_builtin_type;
2106 break;
2108 case TYPEOF_TYPE:
2109 sorry ("mangling typeof, use decltype instead");
2110 break;
2112 case UNDERLYING_TYPE:
2113 sorry ("mangling __underlying_type");
2114 break;
2116 case LANG_TYPE:
2117 /* fall through. */
2119 default:
2120 gcc_unreachable ();
2125 /* Types other than builtin types are substitution candidates. */
2126 if (!is_builtin_type)
2127 add_substitution (type);
2130 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2131 CV-qualifiers written for TYPE.
2133 <CV-qualifiers> ::= [r] [V] [K] */
2135 static int
2136 write_CV_qualifiers_for_type (const tree type)
2138 int num_qualifiers = 0;
2140 /* The order is specified by:
2142 "In cases where multiple order-insensitive qualifiers are
2143 present, they should be ordered 'K' (closest to the base type),
2144 'V', 'r', and 'U' (farthest from the base type) ..."
2146 Note that we do not use cp_type_quals below; given "const
2147 int[3]", the "const" is emitted with the "int", not with the
2148 array. */
2149 cp_cv_quals quals = TYPE_QUALS (type);
2151 if (quals & TYPE_QUAL_RESTRICT)
2153 write_char ('r');
2154 ++num_qualifiers;
2156 if (quals & TYPE_QUAL_VOLATILE)
2158 write_char ('V');
2159 ++num_qualifiers;
2161 if (quals & TYPE_QUAL_CONST)
2163 write_char ('K');
2164 ++num_qualifiers;
2167 return num_qualifiers;
2170 /* Non-terminal <builtin-type>.
2172 <builtin-type> ::= v # void
2173 ::= b # bool
2174 ::= w # wchar_t
2175 ::= c # char
2176 ::= a # signed char
2177 ::= h # unsigned char
2178 ::= s # short
2179 ::= t # unsigned short
2180 ::= i # int
2181 ::= j # unsigned int
2182 ::= l # long
2183 ::= m # unsigned long
2184 ::= x # long long, __int64
2185 ::= y # unsigned long long, __int64
2186 ::= n # __int128
2187 ::= o # unsigned __int128
2188 ::= f # float
2189 ::= d # double
2190 ::= e # long double, __float80
2191 ::= g # __float128 [not supported]
2192 ::= u <source-name> # vendor extended type */
2194 static void
2195 write_builtin_type (tree type)
2197 if (TYPE_CANONICAL (type))
2198 type = TYPE_CANONICAL (type);
2200 switch (TREE_CODE (type))
2202 case VOID_TYPE:
2203 write_char ('v');
2204 break;
2206 case BOOLEAN_TYPE:
2207 write_char ('b');
2208 break;
2210 case INTEGER_TYPE:
2211 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2212 isn't in integer_type_nodes. */
2213 if (type == wchar_type_node)
2214 write_char ('w');
2215 else if (type == char16_type_node)
2216 write_string ("Ds");
2217 else if (type == char32_type_node)
2218 write_string ("Di");
2219 else if (TYPE_FOR_JAVA (type))
2220 write_java_integer_type_codes (type);
2221 else
2223 size_t itk;
2224 /* Assume TYPE is one of the shared integer type nodes. Find
2225 it in the array of these nodes. */
2226 iagain:
2227 for (itk = 0; itk < itk_none; ++itk)
2228 if (integer_types[itk] != NULL_TREE
2229 && type == integer_types[itk])
2231 /* Print the corresponding single-letter code. */
2232 write_char (integer_type_codes[itk]);
2233 break;
2236 if (itk == itk_none)
2238 tree t = c_common_type_for_mode (TYPE_MODE (type),
2239 TYPE_UNSIGNED (type));
2240 if (type != t)
2242 type = t;
2243 goto iagain;
2246 if (TYPE_PRECISION (type) == 128)
2247 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2248 else
2250 /* Allow for cases where TYPE is not one of the shared
2251 integer type nodes and write a "vendor extended builtin
2252 type" with a name the form intN or uintN, respectively.
2253 Situations like this can happen if you have an
2254 __attribute__((__mode__(__SI__))) type and use exotic
2255 switches like '-mint8' on AVR. Of course, this is
2256 undefined by the C++ ABI (and '-mint8' is not even
2257 Standard C conforming), but when using such special
2258 options you're pretty much in nowhere land anyway. */
2259 const char *prefix;
2260 char prec[11]; /* up to ten digits for an unsigned */
2262 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2263 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2264 write_char ('u'); /* "vendor extended builtin type" */
2265 write_unsigned_number (strlen (prefix) + strlen (prec));
2266 write_string (prefix);
2267 write_string (prec);
2271 break;
2273 case REAL_TYPE:
2274 if (type == float_type_node
2275 || type == java_float_type_node)
2276 write_char ('f');
2277 else if (type == double_type_node
2278 || type == java_double_type_node)
2279 write_char ('d');
2280 else if (type == long_double_type_node)
2281 write_char ('e');
2282 else if (type == dfloat32_type_node)
2283 write_string ("Df");
2284 else if (type == dfloat64_type_node)
2285 write_string ("Dd");
2286 else if (type == dfloat128_type_node)
2287 write_string ("De");
2288 else
2289 gcc_unreachable ();
2290 break;
2292 case FIXED_POINT_TYPE:
2293 write_string ("DF");
2294 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2295 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2296 if (type == fract_type_node
2297 || type == sat_fract_type_node
2298 || type == accum_type_node
2299 || type == sat_accum_type_node)
2300 write_char ('i');
2301 else if (type == unsigned_fract_type_node
2302 || type == sat_unsigned_fract_type_node
2303 || type == unsigned_accum_type_node
2304 || type == sat_unsigned_accum_type_node)
2305 write_char ('j');
2306 else if (type == short_fract_type_node
2307 || type == sat_short_fract_type_node
2308 || type == short_accum_type_node
2309 || type == sat_short_accum_type_node)
2310 write_char ('s');
2311 else if (type == unsigned_short_fract_type_node
2312 || type == sat_unsigned_short_fract_type_node
2313 || type == unsigned_short_accum_type_node
2314 || type == sat_unsigned_short_accum_type_node)
2315 write_char ('t');
2316 else if (type == long_fract_type_node
2317 || type == sat_long_fract_type_node
2318 || type == long_accum_type_node
2319 || type == sat_long_accum_type_node)
2320 write_char ('l');
2321 else if (type == unsigned_long_fract_type_node
2322 || type == sat_unsigned_long_fract_type_node
2323 || type == unsigned_long_accum_type_node
2324 || type == sat_unsigned_long_accum_type_node)
2325 write_char ('m');
2326 else if (type == long_long_fract_type_node
2327 || type == sat_long_long_fract_type_node
2328 || type == long_long_accum_type_node
2329 || type == sat_long_long_accum_type_node)
2330 write_char ('x');
2331 else if (type == unsigned_long_long_fract_type_node
2332 || type == sat_unsigned_long_long_fract_type_node
2333 || type == unsigned_long_long_accum_type_node
2334 || type == sat_unsigned_long_long_accum_type_node)
2335 write_char ('y');
2336 else
2337 sorry ("mangling unknown fixed point type");
2338 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2339 if (TYPE_SATURATING (type))
2340 write_char ('s');
2341 else
2342 write_char ('n');
2343 break;
2345 default:
2346 gcc_unreachable ();
2350 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2351 METHOD_TYPE. The return type is mangled before the parameter
2352 types.
2354 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2356 static void
2357 write_function_type (const tree type)
2359 MANGLE_TRACE_TREE ("function-type", type);
2361 /* For a pointer to member function, the function type may have
2362 cv-qualifiers, indicating the quals for the artificial 'this'
2363 parameter. */
2364 if (TREE_CODE (type) == METHOD_TYPE)
2366 /* The first parameter must be a POINTER_TYPE pointing to the
2367 `this' parameter. */
2368 tree this_type = class_of_this_parm (type);
2369 write_CV_qualifiers_for_type (this_type);
2372 write_char ('F');
2373 /* We don't track whether or not a type is `extern "C"'. Note that
2374 you can have an `extern "C"' function that does not have
2375 `extern "C"' type, and vice versa:
2377 extern "C" typedef void function_t();
2378 function_t f; // f has C++ linkage, but its type is
2379 // `extern "C"'
2381 typedef void function_t();
2382 extern "C" function_t f; // Vice versa.
2384 See [dcl.link]. */
2385 write_bare_function_type (type, /*include_return_type_p=*/1,
2386 /*decl=*/NULL);
2387 if (FUNCTION_REF_QUALIFIED (type))
2389 if (FUNCTION_RVALUE_QUALIFIED (type))
2390 write_char ('O');
2391 else
2392 write_char ('R');
2394 write_char ('E');
2397 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2398 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2399 is mangled before the parameter types. If non-NULL, DECL is
2400 FUNCTION_DECL for the function whose type is being emitted.
2402 If DECL is a member of a Java type, then a literal 'J'
2403 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2404 were nonzero.
2406 <bare-function-type> ::= [J]</signature/ type>+ */
2408 static void
2409 write_bare_function_type (const tree type, const int include_return_type_p,
2410 const tree decl)
2412 int java_method_p;
2414 MANGLE_TRACE_TREE ("bare-function-type", type);
2416 /* Detect Java methods and emit special encoding. */
2417 if (decl != NULL
2418 && DECL_FUNCTION_MEMBER_P (decl)
2419 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2420 && !DECL_CONSTRUCTOR_P (decl)
2421 && !DECL_DESTRUCTOR_P (decl)
2422 && !DECL_CONV_FN_P (decl))
2424 java_method_p = 1;
2425 write_char ('J');
2427 else
2429 java_method_p = 0;
2432 /* Mangle the return type, if requested. */
2433 if (include_return_type_p || java_method_p)
2434 write_type (TREE_TYPE (type));
2436 /* Now mangle the types of the arguments. */
2437 ++G.parm_depth;
2438 write_method_parms (TYPE_ARG_TYPES (type),
2439 TREE_CODE (type) == METHOD_TYPE,
2440 decl);
2441 --G.parm_depth;
2444 /* Write the mangled representation of a method parameter list of
2445 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2446 considered a non-static method, and the this parameter is omitted.
2447 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2448 parameters are being emitted. */
2450 static void
2451 write_method_parms (tree parm_types, const int method_p, const tree decl)
2453 tree first_parm_type;
2454 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2456 /* Assume this parameter type list is variable-length. If it ends
2457 with a void type, then it's not. */
2458 int varargs_p = 1;
2460 /* If this is a member function, skip the first arg, which is the
2461 this pointer.
2462 "Member functions do not encode the type of their implicit this
2463 parameter."
2465 Similarly, there's no need to mangle artificial parameters, like
2466 the VTT parameters for constructors and destructors. */
2467 if (method_p)
2469 parm_types = TREE_CHAIN (parm_types);
2470 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2472 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2474 parm_types = TREE_CHAIN (parm_types);
2475 parm_decl = DECL_CHAIN (parm_decl);
2479 for (first_parm_type = parm_types;
2480 parm_types;
2481 parm_types = TREE_CHAIN (parm_types))
2483 tree parm = TREE_VALUE (parm_types);
2484 if (parm == void_type_node)
2486 /* "Empty parameter lists, whether declared as () or
2487 conventionally as (void), are encoded with a void parameter
2488 (v)." */
2489 if (parm_types == first_parm_type)
2490 write_type (parm);
2491 /* If the parm list is terminated with a void type, it's
2492 fixed-length. */
2493 varargs_p = 0;
2494 /* A void type better be the last one. */
2495 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2497 else
2498 write_type (parm);
2501 if (varargs_p)
2502 /* <builtin-type> ::= z # ellipsis */
2503 write_char ('z');
2506 /* <class-enum-type> ::= <name> */
2508 static void
2509 write_class_enum_type (const tree type)
2511 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2514 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2515 arguments.
2517 <template-args> ::= I <template-arg>* E */
2519 static void
2520 write_template_args (tree args)
2522 int i;
2523 int length = 0;
2525 MANGLE_TRACE_TREE ("template-args", args);
2527 write_char ('I');
2529 if (args)
2530 length = TREE_VEC_LENGTH (args);
2532 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2534 /* We have nested template args. We want the innermost template
2535 argument list. */
2536 args = TREE_VEC_ELT (args, length - 1);
2537 length = TREE_VEC_LENGTH (args);
2539 for (i = 0; i < length; ++i)
2540 write_template_arg (TREE_VEC_ELT (args, i));
2542 write_char ('E');
2545 /* Write out the
2546 <unqualified-name>
2547 <unqualified-name> <template-args>
2548 part of SCOPE_REF or COMPONENT_REF mangling. */
2550 static void
2551 write_member_name (tree member)
2553 if (identifier_p (member))
2554 write_unqualified_id (member);
2555 else if (DECL_P (member))
2556 write_unqualified_name (member);
2557 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2559 tree name = TREE_OPERAND (member, 0);
2560 if (TREE_CODE (name) == OVERLOAD)
2561 name = OVL_FUNCTION (name);
2562 write_member_name (name);
2563 write_template_args (TREE_OPERAND (member, 1));
2565 else
2566 write_expression (member);
2569 /* <expression> ::= <unary operator-name> <expression>
2570 ::= <binary operator-name> <expression> <expression>
2571 ::= <expr-primary>
2573 <expr-primary> ::= <template-param>
2574 ::= L <type> <value number> E # literal
2575 ::= L <mangled-name> E # external name
2576 ::= st <type> # sizeof
2577 ::= sr <type> <unqualified-name> # dependent name
2578 ::= sr <type> <unqualified-name> <template-args> */
2580 static void
2581 write_expression (tree expr)
2583 enum tree_code code = TREE_CODE (expr);
2585 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2586 is converted (via qualification conversions) to another
2587 type. */
2588 while (TREE_CODE (expr) == NOP_EXPR
2589 /* Parentheses aren't mangled. */
2590 || code == PAREN_EXPR
2591 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2593 expr = TREE_OPERAND (expr, 0);
2594 code = TREE_CODE (expr);
2597 if (code == BASELINK
2598 && (!type_unknown_p (expr)
2599 || !BASELINK_QUALIFIED_P (expr)))
2601 expr = BASELINK_FUNCTIONS (expr);
2602 code = TREE_CODE (expr);
2605 /* Handle pointers-to-members by making them look like expression
2606 nodes. */
2607 if (code == PTRMEM_CST)
2609 expr = build_nt (ADDR_EXPR,
2610 build_qualified_name (/*type=*/NULL_TREE,
2611 PTRMEM_CST_CLASS (expr),
2612 PTRMEM_CST_MEMBER (expr),
2613 /*template_p=*/false));
2614 code = TREE_CODE (expr);
2617 /* Handle template parameters. */
2618 if (code == TEMPLATE_TYPE_PARM
2619 || code == TEMPLATE_TEMPLATE_PARM
2620 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2621 || code == TEMPLATE_PARM_INDEX)
2622 write_template_param (expr);
2623 /* Handle literals. */
2624 else if (TREE_CODE_CLASS (code) == tcc_constant
2625 || (abi_version_at_least (2) && code == CONST_DECL))
2626 write_template_arg_literal (expr);
2627 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2629 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2630 write_string ("fpT");
2632 else if (code == PARM_DECL)
2634 /* A function parameter used in a late-specified return type. */
2635 int index = DECL_PARM_INDEX (expr);
2636 int level = DECL_PARM_LEVEL (expr);
2637 int delta = G.parm_depth - level + 1;
2638 gcc_assert (index >= 1);
2639 write_char ('f');
2640 if (delta != 0)
2642 if (abi_version_at_least (5))
2644 /* Let L be the number of function prototype scopes from the
2645 innermost one (in which the parameter reference occurs) up
2646 to (and including) the one containing the declaration of
2647 the referenced parameter. If the parameter declaration
2648 clause of the innermost function prototype scope has been
2649 completely seen, it is not counted (in that case -- which
2650 is perhaps the most common -- L can be zero). */
2651 write_char ('L');
2652 write_unsigned_number (delta - 1);
2654 else
2655 G.need_abi_warning = true;
2657 write_char ('p');
2658 write_compact_number (index - 1);
2660 else if (DECL_P (expr))
2662 /* G++ 3.2 incorrectly mangled non-type template arguments of
2663 enumeration type using their names. */
2664 if (code == CONST_DECL)
2665 G.need_abi_warning = 1;
2666 write_char ('L');
2667 write_mangled_name (expr, false);
2668 write_char ('E');
2670 else if (TREE_CODE (expr) == SIZEOF_EXPR
2671 && SIZEOF_EXPR_TYPE_P (expr))
2673 write_string ("st");
2674 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2676 else if (TREE_CODE (expr) == SIZEOF_EXPR
2677 && TYPE_P (TREE_OPERAND (expr, 0)))
2679 write_string ("st");
2680 write_type (TREE_OPERAND (expr, 0));
2682 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2683 && TYPE_P (TREE_OPERAND (expr, 0)))
2685 write_string ("at");
2686 write_type (TREE_OPERAND (expr, 0));
2688 else if (code == SCOPE_REF
2689 || code == BASELINK)
2691 tree scope, member;
2692 if (code == SCOPE_REF)
2694 scope = TREE_OPERAND (expr, 0);
2695 member = TREE_OPERAND (expr, 1);
2697 else
2699 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2700 member = BASELINK_FUNCTIONS (expr);
2703 if (!abi_version_at_least (2) && DECL_P (member))
2705 write_string ("sr");
2706 write_type (scope);
2707 /* G++ 3.2 incorrectly put out both the "sr" code and
2708 the nested name of the qualified name. */
2709 G.need_abi_warning = 1;
2710 write_encoding (member);
2713 /* If the MEMBER is a real declaration, then the qualifying
2714 scope was not dependent. Ideally, we would not have a
2715 SCOPE_REF in those cases, but sometimes we do. If the second
2716 argument is a DECL, then the name must not have been
2717 dependent. */
2718 else if (DECL_P (member))
2719 write_expression (member);
2720 else
2722 write_string ("sr");
2723 write_type (scope);
2724 write_member_name (member);
2727 else if (INDIRECT_REF_P (expr)
2728 && TREE_TYPE (TREE_OPERAND (expr, 0))
2729 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2731 write_expression (TREE_OPERAND (expr, 0));
2733 else if (identifier_p (expr))
2735 /* An operator name appearing as a dependent name needs to be
2736 specially marked to disambiguate between a use of the operator
2737 name and a use of the operator in an expression. */
2738 if (IDENTIFIER_OPNAME_P (expr))
2739 write_string ("on");
2740 write_unqualified_id (expr);
2742 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2744 tree fn = TREE_OPERAND (expr, 0);
2745 if (is_overloaded_fn (fn))
2746 fn = DECL_NAME (get_first_fn (fn));
2747 if (IDENTIFIER_OPNAME_P (fn))
2748 write_string ("on");
2749 write_unqualified_id (fn);
2750 write_template_args (TREE_OPERAND (expr, 1));
2752 else if (TREE_CODE (expr) == MODOP_EXPR)
2754 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2755 const char *name = (assignment_operator_name_info[(int) subop]
2756 .mangled_name);
2757 write_string (name);
2758 write_expression (TREE_OPERAND (expr, 0));
2759 write_expression (TREE_OPERAND (expr, 2));
2761 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2763 /* ::= [gs] nw <expression>* _ <type> E
2764 ::= [gs] nw <expression>* _ <type> <initializer>
2765 ::= [gs] na <expression>* _ <type> E
2766 ::= [gs] na <expression>* _ <type> <initializer>
2767 <initializer> ::= pi <expression>* E */
2768 tree placement = TREE_OPERAND (expr, 0);
2769 tree type = TREE_OPERAND (expr, 1);
2770 tree nelts = TREE_OPERAND (expr, 2);
2771 tree init = TREE_OPERAND (expr, 3);
2772 tree t;
2774 gcc_assert (code == NEW_EXPR);
2775 if (TREE_OPERAND (expr, 2))
2776 code = VEC_NEW_EXPR;
2778 if (NEW_EXPR_USE_GLOBAL (expr))
2779 write_string ("gs");
2781 write_string (operator_name_info[(int) code].mangled_name);
2783 for (t = placement; t; t = TREE_CHAIN (t))
2784 write_expression (TREE_VALUE (t));
2786 write_char ('_');
2788 if (nelts)
2790 tree domain;
2791 ++processing_template_decl;
2792 domain = compute_array_index_type (NULL_TREE, nelts,
2793 tf_warning_or_error);
2794 type = build_cplus_array_type (type, domain);
2795 --processing_template_decl;
2797 write_type (type);
2799 if (init && TREE_CODE (init) == TREE_LIST
2800 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
2801 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
2802 write_expression (TREE_VALUE (init));
2803 else
2805 if (init)
2806 write_string ("pi");
2807 if (init && init != void_zero_node)
2808 for (t = init; t; t = TREE_CHAIN (t))
2809 write_expression (TREE_VALUE (t));
2810 write_char ('E');
2813 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2815 gcc_assert (code == DELETE_EXPR);
2816 if (DELETE_EXPR_USE_VEC (expr))
2817 code = VEC_DELETE_EXPR;
2819 if (DELETE_EXPR_USE_GLOBAL (expr))
2820 write_string ("gs");
2822 write_string (operator_name_info[(int) code].mangled_name);
2824 write_expression (TREE_OPERAND (expr, 0));
2826 else if (code == THROW_EXPR)
2828 tree op = TREE_OPERAND (expr, 0);
2829 if (op)
2831 write_string ("tw");
2832 write_expression (op);
2834 else
2835 write_string ("tr");
2837 else if (code == CONSTRUCTOR)
2839 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2840 unsigned i; tree val;
2842 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2843 write_string ("il");
2844 else
2846 write_string ("tl");
2847 write_type (TREE_TYPE (expr));
2849 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2850 write_expression (val);
2851 write_char ('E');
2853 else if (dependent_name (expr))
2855 write_unqualified_id (dependent_name (expr));
2857 else
2859 int i, len;
2860 const char *name;
2862 /* When we bind a variable or function to a non-type template
2863 argument with reference type, we create an ADDR_EXPR to show
2864 the fact that the entity's address has been taken. But, we
2865 don't actually want to output a mangling code for the `&'. */
2866 if (TREE_CODE (expr) == ADDR_EXPR
2867 && TREE_TYPE (expr)
2868 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2870 expr = TREE_OPERAND (expr, 0);
2871 if (DECL_P (expr))
2873 write_expression (expr);
2874 return;
2877 code = TREE_CODE (expr);
2880 if (code == COMPONENT_REF)
2882 tree ob = TREE_OPERAND (expr, 0);
2884 if (TREE_CODE (ob) == ARROW_EXPR)
2886 write_string (operator_name_info[(int)code].mangled_name);
2887 ob = TREE_OPERAND (ob, 0);
2889 else
2890 write_string ("dt");
2892 write_expression (ob);
2893 write_member_name (TREE_OPERAND (expr, 1));
2894 return;
2897 /* If it wasn't any of those, recursively expand the expression. */
2898 name = operator_name_info[(int) code].mangled_name;
2900 /* We used to mangle const_cast and static_cast like a C cast. */
2901 if (!abi_version_at_least (6)
2902 && (code == CONST_CAST_EXPR
2903 || code == STATIC_CAST_EXPR))
2905 name = operator_name_info[CAST_EXPR].mangled_name;
2906 G.need_abi_warning = 1;
2909 if (name == NULL)
2911 switch (code)
2913 case TRAIT_EXPR:
2914 error ("use of built-in trait %qE in function signature; "
2915 "use library traits instead", expr);
2916 break;
2918 default:
2919 sorry ("mangling %C", code);
2920 break;
2922 return;
2924 else
2925 write_string (name);
2927 switch (code)
2929 case CALL_EXPR:
2931 tree fn = CALL_EXPR_FN (expr);
2933 if (TREE_CODE (fn) == ADDR_EXPR)
2934 fn = TREE_OPERAND (fn, 0);
2936 /* Mangle a dependent name as the name, not whatever happens to
2937 be the first function in the overload set. */
2938 if ((TREE_CODE (fn) == FUNCTION_DECL
2939 || TREE_CODE (fn) == OVERLOAD)
2940 && type_dependent_expression_p_push (expr))
2941 fn = DECL_NAME (get_first_fn (fn));
2943 write_expression (fn);
2946 for (i = 0; i < call_expr_nargs (expr); ++i)
2947 write_expression (CALL_EXPR_ARG (expr, i));
2948 write_char ('E');
2949 break;
2951 case CAST_EXPR:
2952 write_type (TREE_TYPE (expr));
2953 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2954 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2955 else
2957 tree args = TREE_OPERAND (expr, 0);
2958 write_char ('_');
2959 for (; args; args = TREE_CHAIN (args))
2960 write_expression (TREE_VALUE (args));
2961 write_char ('E');
2963 break;
2965 case DYNAMIC_CAST_EXPR:
2966 case REINTERPRET_CAST_EXPR:
2967 case STATIC_CAST_EXPR:
2968 case CONST_CAST_EXPR:
2969 write_type (TREE_TYPE (expr));
2970 write_expression (TREE_OPERAND (expr, 0));
2971 break;
2973 case PREINCREMENT_EXPR:
2974 case PREDECREMENT_EXPR:
2975 if (abi_version_at_least (6))
2976 write_char ('_');
2977 else
2978 G.need_abi_warning = 1;
2979 /* Fall through. */
2981 default:
2982 /* In the middle-end, some expressions have more operands than
2983 they do in templates (and mangling). */
2984 len = cp_tree_operand_length (expr);
2986 for (i = 0; i < len; ++i)
2988 tree operand = TREE_OPERAND (expr, i);
2989 /* As a GNU extension, the middle operand of a
2990 conditional may be omitted. Since expression
2991 manglings are supposed to represent the input token
2992 stream, there's no good way to mangle such an
2993 expression without extending the C++ ABI. */
2994 if (code == COND_EXPR && i == 1 && !operand)
2996 error ("omitted middle operand to %<?:%> operand "
2997 "cannot be mangled");
2998 continue;
3000 write_expression (operand);
3006 /* Literal subcase of non-terminal <template-arg>.
3008 "Literal arguments, e.g. "A<42L>", are encoded with their type
3009 and value. Negative integer values are preceded with "n"; for
3010 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3011 encoded as 0, true as 1." */
3013 static void
3014 write_template_arg_literal (const tree value)
3016 write_char ('L');
3017 write_type (TREE_TYPE (value));
3019 /* Write a null member pointer value as (type)0, regardless of its
3020 real representation. */
3021 if (null_member_pointer_value_p (value))
3022 write_integer_cst (integer_zero_node);
3023 else
3024 switch (TREE_CODE (value))
3026 case CONST_DECL:
3027 write_integer_cst (DECL_INITIAL (value));
3028 break;
3030 case INTEGER_CST:
3031 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3032 || integer_zerop (value) || integer_onep (value));
3033 write_integer_cst (value);
3034 break;
3036 case REAL_CST:
3037 write_real_cst (value);
3038 break;
3040 case COMPLEX_CST:
3041 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3042 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3044 write_integer_cst (TREE_REALPART (value));
3045 write_char ('_');
3046 write_integer_cst (TREE_IMAGPART (value));
3048 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3049 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3051 write_real_cst (TREE_REALPART (value));
3052 write_char ('_');
3053 write_real_cst (TREE_IMAGPART (value));
3055 else
3056 gcc_unreachable ();
3057 break;
3059 case STRING_CST:
3060 sorry ("string literal in function template signature");
3061 break;
3063 default:
3064 gcc_unreachable ();
3067 write_char ('E');
3070 /* Non-terminal <template-arg>.
3072 <template-arg> ::= <type> # type
3073 ::= L <type> </value/ number> E # literal
3074 ::= LZ <name> E # external name
3075 ::= X <expression> E # expression */
3077 static void
3078 write_template_arg (tree node)
3080 enum tree_code code = TREE_CODE (node);
3082 MANGLE_TRACE_TREE ("template-arg", node);
3084 /* A template template parameter's argument list contains TREE_LIST
3085 nodes of which the value field is the actual argument. */
3086 if (code == TREE_LIST)
3088 node = TREE_VALUE (node);
3089 /* If it's a decl, deal with its type instead. */
3090 if (DECL_P (node))
3092 node = TREE_TYPE (node);
3093 code = TREE_CODE (node);
3097 if (TREE_CODE (node) == NOP_EXPR
3098 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3100 /* Template parameters can be of reference type. To maintain
3101 internal consistency, such arguments use a conversion from
3102 address of object to reference type. */
3103 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3104 if (abi_version_at_least (2))
3105 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3106 else
3107 G.need_abi_warning = 1;
3110 if (TREE_CODE (node) == BASELINK
3111 && !type_unknown_p (node))
3113 if (abi_version_at_least (6))
3114 node = BASELINK_FUNCTIONS (node);
3115 else
3116 /* We wrongly wrapped a class-scope function in X/E. */
3117 G.need_abi_warning = 1;
3120 if (ARGUMENT_PACK_P (node))
3122 /* Expand the template argument pack. */
3123 tree args = ARGUMENT_PACK_ARGS (node);
3124 int i, length = TREE_VEC_LENGTH (args);
3125 if (abi_version_at_least (6))
3126 write_char ('J');
3127 else
3129 write_char ('I');
3130 G.need_abi_warning = 1;
3132 for (i = 0; i < length; ++i)
3133 write_template_arg (TREE_VEC_ELT (args, i));
3134 write_char ('E');
3136 else if (TYPE_P (node))
3137 write_type (node);
3138 else if (code == TEMPLATE_DECL)
3139 /* A template appearing as a template arg is a template template arg. */
3140 write_template_template_arg (node);
3141 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3142 || (abi_version_at_least (2) && code == CONST_DECL)
3143 || null_member_pointer_value_p (node))
3144 write_template_arg_literal (node);
3145 else if (DECL_P (node))
3147 /* Until ABI version 2, non-type template arguments of
3148 enumeration type were mangled using their names. */
3149 if (code == CONST_DECL && !abi_version_at_least (2))
3150 G.need_abi_warning = 1;
3151 write_char ('L');
3152 /* Until ABI version 3, the underscore before the mangled name
3153 was incorrectly omitted. */
3154 if (!abi_version_at_least (3))
3156 G.need_abi_warning = 1;
3157 write_char ('Z');
3159 else
3160 write_string ("_Z");
3161 write_encoding (node);
3162 write_char ('E');
3164 else
3166 /* Template arguments may be expressions. */
3167 write_char ('X');
3168 write_expression (node);
3169 write_char ('E');
3173 /* <template-template-arg>
3174 ::= <name>
3175 ::= <substitution> */
3177 static void
3178 write_template_template_arg (const tree decl)
3180 MANGLE_TRACE_TREE ("template-template-arg", decl);
3182 if (find_substitution (decl))
3183 return;
3184 write_name (decl, /*ignore_local_scope=*/0);
3185 add_substitution (decl);
3189 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3191 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3192 ::= A <expression> _ </element/ type>
3194 "Array types encode the dimension (number of elements) and the
3195 element type. For variable length arrays, the dimension (but not
3196 the '_' separator) is omitted." */
3198 static void
3199 write_array_type (const tree type)
3201 write_char ('A');
3202 if (TYPE_DOMAIN (type))
3204 tree index_type;
3205 tree max;
3207 index_type = TYPE_DOMAIN (type);
3208 /* The INDEX_TYPE gives the upper and lower bounds of the
3209 array. */
3210 max = TYPE_MAX_VALUE (index_type);
3211 if (TREE_CODE (max) == INTEGER_CST)
3213 /* The ABI specifies that we should mangle the number of
3214 elements in the array, not the largest allowed index. */
3215 double_int dmax = tree_to_double_int (max) + double_int_one;
3216 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3217 number of elements as zero. */
3218 dmax = dmax.zext (TYPE_PRECISION (TREE_TYPE (max)));
3219 gcc_assert (dmax.fits_uhwi ());
3220 write_unsigned_number (dmax.low);
3222 else
3224 max = TREE_OPERAND (max, 0);
3225 if (!abi_version_at_least (2))
3227 /* value_dependent_expression_p presumes nothing is
3228 dependent when PROCESSING_TEMPLATE_DECL is zero. */
3229 ++processing_template_decl;
3230 if (!value_dependent_expression_p (max))
3231 G.need_abi_warning = 1;
3232 --processing_template_decl;
3234 write_expression (max);
3238 write_char ('_');
3239 write_type (TREE_TYPE (type));
3242 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3243 variables. TYPE is a pointer-to-member POINTER_TYPE.
3245 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3247 static void
3248 write_pointer_to_member_type (const tree type)
3250 write_char ('M');
3251 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3252 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3255 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3256 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3257 TEMPLATE_PARM_INDEX.
3259 <template-param> ::= T </parameter/ number> _ */
3261 static void
3262 write_template_param (const tree parm)
3264 int parm_index;
3266 MANGLE_TRACE_TREE ("template-parm", parm);
3268 switch (TREE_CODE (parm))
3270 case TEMPLATE_TYPE_PARM:
3271 case TEMPLATE_TEMPLATE_PARM:
3272 case BOUND_TEMPLATE_TEMPLATE_PARM:
3273 parm_index = TEMPLATE_TYPE_IDX (parm);
3274 break;
3276 case TEMPLATE_PARM_INDEX:
3277 parm_index = TEMPLATE_PARM_IDX (parm);
3278 break;
3280 default:
3281 gcc_unreachable ();
3284 write_char ('T');
3285 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3286 earliest template param denoted by `_'. */
3287 write_compact_number (parm_index);
3290 /* <template-template-param>
3291 ::= <template-param>
3292 ::= <substitution> */
3294 static void
3295 write_template_template_param (const tree parm)
3297 tree templ = NULL_TREE;
3299 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3300 template template parameter. The substitution candidate here is
3301 only the template. */
3302 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3304 templ
3305 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3306 if (find_substitution (templ))
3307 return;
3310 /* <template-param> encodes only the template parameter position,
3311 not its template arguments, which is fine here. */
3312 write_template_param (parm);
3313 if (templ)
3314 add_substitution (templ);
3317 /* Non-terminal <substitution>.
3319 <substitution> ::= S <seq-id> _
3320 ::= S_ */
3322 static void
3323 write_substitution (const int seq_id)
3325 MANGLE_TRACE ("substitution", "");
3327 write_char ('S');
3328 if (seq_id > 0)
3329 write_number (seq_id - 1, /*unsigned=*/1, 36);
3330 write_char ('_');
3333 /* Start mangling ENTITY. */
3335 static inline void
3336 start_mangling (const tree entity)
3338 G.entity = entity;
3339 G.need_abi_warning = false;
3340 obstack_free (&name_obstack, name_base);
3341 mangle_obstack = &name_obstack;
3342 name_base = obstack_alloc (&name_obstack, 0);
3345 /* Done with mangling. If WARN is true, and the name of G.entity will
3346 be mangled differently in a future version of the ABI, issue a
3347 warning. */
3349 static void
3350 finish_mangling_internal (const bool warn)
3352 if (warn_abi && warn && G.need_abi_warning)
3353 warning (OPT_Wabi, "the mangled name of %qD will change in a future "
3354 "version of GCC",
3355 G.entity);
3357 /* Clear all the substitutions. */
3358 vec_safe_truncate (G.substitutions, 0);
3360 /* Null-terminate the string. */
3361 write_char ('\0');
3365 /* Like finish_mangling_internal, but return the mangled string. */
3367 static inline const char *
3368 finish_mangling (const bool warn)
3370 finish_mangling_internal (warn);
3371 return (const char *) obstack_finish (mangle_obstack);
3374 /* Like finish_mangling_internal, but return an identifier. */
3376 static tree
3377 finish_mangling_get_identifier (const bool warn)
3379 finish_mangling_internal (warn);
3380 /* Don't obstack_finish here, and the next start_mangling will
3381 remove the identifier. */
3382 return get_identifier ((const char *) obstack_base (mangle_obstack));
3385 /* Initialize data structures for mangling. */
3387 void
3388 init_mangle (void)
3390 gcc_obstack_init (&name_obstack);
3391 name_base = obstack_alloc (&name_obstack, 0);
3392 vec_alloc (G.substitutions, 0);
3394 /* Cache these identifiers for quick comparison when checking for
3395 standard substitutions. */
3396 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3397 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3398 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3399 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3400 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3401 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3404 /* Generate the mangled name of DECL. */
3406 static tree
3407 mangle_decl_string (const tree decl)
3409 tree result;
3410 location_t saved_loc = input_location;
3411 tree saved_fn = NULL_TREE;
3412 bool template_p = false;
3414 /* We shouldn't be trying to mangle an uninstantiated template. */
3415 gcc_assert (!type_dependent_expression_p (decl));
3417 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3419 struct tinst_level *tl = current_instantiation ();
3420 if ((!tl || tl->decl != decl)
3421 && push_tinst_level (decl))
3423 template_p = true;
3424 saved_fn = current_function_decl;
3425 current_function_decl = NULL_TREE;
3428 input_location = DECL_SOURCE_LOCATION (decl);
3430 start_mangling (decl);
3432 if (TREE_CODE (decl) == TYPE_DECL)
3433 write_type (TREE_TYPE (decl));
3434 else
3435 write_mangled_name (decl, true);
3437 result = finish_mangling_get_identifier (/*warn=*/true);
3438 if (DEBUG_MANGLE)
3439 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3440 IDENTIFIER_POINTER (result));
3442 if (template_p)
3444 pop_tinst_level ();
3445 current_function_decl = saved_fn;
3447 input_location = saved_loc;
3449 return result;
3452 /* Return an identifier for the external mangled name of DECL. */
3454 static tree
3455 get_mangled_id (tree decl)
3457 tree id = mangle_decl_string (decl);
3458 return targetm.mangle_decl_assembler_name (decl, id);
3461 /* Create an identifier for the external mangled name of DECL. */
3463 void
3464 mangle_decl (const tree decl)
3466 tree id;
3467 bool dep;
3469 /* Don't bother mangling uninstantiated templates. */
3470 ++processing_template_decl;
3471 if (TREE_CODE (decl) == TYPE_DECL)
3472 dep = dependent_type_p (TREE_TYPE (decl));
3473 else
3474 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3475 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3476 --processing_template_decl;
3477 if (dep)
3478 return;
3480 id = get_mangled_id (decl);
3481 SET_DECL_ASSEMBLER_NAME (decl, id);
3483 if (G.need_abi_warning
3484 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3485 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3486 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3488 #ifdef ASM_OUTPUT_DEF
3489 /* If the mangling will change in the future, emit an alias with the
3490 future mangled name for forward-compatibility. */
3491 int save_ver;
3492 tree id2, alias;
3493 #endif
3495 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3496 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3497 inform (DECL_SOURCE_LOCATION (decl), "-fabi-version=6 (or =0) "
3498 "avoids this error with a change in mangling");
3500 #ifdef ASM_OUTPUT_DEF
3501 save_ver = flag_abi_version;
3502 flag_abi_version = 0;
3503 id2 = mangle_decl_string (decl);
3504 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3505 flag_abi_version = save_ver;
3507 alias = make_alias_for (decl, id2);
3508 DECL_IGNORED_P (alias) = 1;
3509 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3510 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3511 if (vague_linkage_p (decl))
3512 DECL_WEAK (alias) = 1;
3513 if (TREE_CODE (decl) == FUNCTION_DECL)
3514 cgraph_same_body_alias (cgraph_get_create_node (decl), alias, decl);
3515 else
3516 varpool_extra_name_alias (alias, decl);
3517 #endif
3521 /* Generate the mangled representation of TYPE. */
3523 const char *
3524 mangle_type_string (const tree type)
3526 const char *result;
3528 start_mangling (type);
3529 write_type (type);
3530 result = finish_mangling (/*warn=*/false);
3531 if (DEBUG_MANGLE)
3532 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3533 return result;
3536 /* Create an identifier for the mangled name of a special component
3537 for belonging to TYPE. CODE is the ABI-specified code for this
3538 component. */
3540 static tree
3541 mangle_special_for_type (const tree type, const char *code)
3543 tree result;
3545 /* We don't have an actual decl here for the special component, so
3546 we can't just process the <encoded-name>. Instead, fake it. */
3547 start_mangling (type);
3549 /* Start the mangling. */
3550 write_string ("_Z");
3551 write_string (code);
3553 /* Add the type. */
3554 write_type (type);
3555 result = finish_mangling_get_identifier (/*warn=*/false);
3557 if (DEBUG_MANGLE)
3558 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3559 IDENTIFIER_POINTER (result));
3561 return result;
3564 /* Create an identifier for the mangled representation of the typeinfo
3565 structure for TYPE. */
3567 tree
3568 mangle_typeinfo_for_type (const tree type)
3570 return mangle_special_for_type (type, "TI");
3573 /* Create an identifier for the mangled name of the NTBS containing
3574 the mangled name of TYPE. */
3576 tree
3577 mangle_typeinfo_string_for_type (const tree type)
3579 return mangle_special_for_type (type, "TS");
3582 /* Create an identifier for the mangled name of the vtable for TYPE. */
3584 tree
3585 mangle_vtbl_for_type (const tree type)
3587 return mangle_special_for_type (type, "TV");
3590 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3592 tree
3593 mangle_vtt_for_type (const tree type)
3595 return mangle_special_for_type (type, "TT");
3598 /* Return an identifier for a construction vtable group. TYPE is
3599 the most derived class in the hierarchy; BINFO is the base
3600 subobject for which this construction vtable group will be used.
3602 This mangling isn't part of the ABI specification; in the ABI
3603 specification, the vtable group is dumped in the same COMDAT as the
3604 main vtable, and is referenced only from that vtable, so it doesn't
3605 need an external name. For binary formats without COMDAT sections,
3606 though, we need external names for the vtable groups.
3608 We use the production
3610 <special-name> ::= CT <type> <offset number> _ <base type> */
3612 tree
3613 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3615 tree result;
3617 start_mangling (type);
3619 write_string ("_Z");
3620 write_string ("TC");
3621 write_type (type);
3622 write_integer_cst (BINFO_OFFSET (binfo));
3623 write_char ('_');
3624 write_type (BINFO_TYPE (binfo));
3626 result = finish_mangling_get_identifier (/*warn=*/false);
3627 if (DEBUG_MANGLE)
3628 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3629 IDENTIFIER_POINTER (result));
3630 return result;
3633 /* Mangle a this pointer or result pointer adjustment.
3635 <call-offset> ::= h <fixed offset number> _
3636 ::= v <fixed offset number> _ <virtual offset number> _ */
3638 static void
3639 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3641 write_char (virtual_offset ? 'v' : 'h');
3643 /* For either flavor, write the fixed offset. */
3644 write_integer_cst (fixed_offset);
3645 write_char ('_');
3647 /* For a virtual thunk, add the virtual offset. */
3648 if (virtual_offset)
3650 write_integer_cst (virtual_offset);
3651 write_char ('_');
3655 /* Return an identifier for the mangled name of a this-adjusting or
3656 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3657 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3658 is a virtual thunk, and it is the vtbl offset in
3659 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3660 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3661 thunk itself. A covariant thunk name always includes the adjustment
3662 for the this pointer, even if there is none.
3664 <special-name> ::= T <call-offset> <base encoding>
3665 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3666 <base encoding> */
3668 tree
3669 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3670 tree virtual_offset)
3672 tree result;
3674 start_mangling (fn_decl);
3676 write_string ("_Z");
3677 write_char ('T');
3679 if (!this_adjusting)
3681 /* Covariant thunk with no this adjustment */
3682 write_char ('c');
3683 mangle_call_offset (integer_zero_node, NULL_TREE);
3684 mangle_call_offset (fixed_offset, virtual_offset);
3686 else if (!DECL_THUNK_P (fn_decl))
3687 /* Plain this adjusting thunk. */
3688 mangle_call_offset (fixed_offset, virtual_offset);
3689 else
3691 /* This adjusting thunk to covariant thunk. */
3692 write_char ('c');
3693 mangle_call_offset (fixed_offset, virtual_offset);
3694 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3695 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3696 if (virtual_offset)
3697 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3698 mangle_call_offset (fixed_offset, virtual_offset);
3699 fn_decl = THUNK_TARGET (fn_decl);
3702 /* Scoped name. */
3703 write_encoding (fn_decl);
3705 result = finish_mangling_get_identifier (/*warn=*/false);
3706 if (DEBUG_MANGLE)
3707 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3708 return result;
3711 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3712 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3713 TYPE. */
3715 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
3717 /* Hash a node (VAL1) in the table. */
3719 static hashval_t
3720 hash_type (const void *val)
3722 return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
3725 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3727 static int
3728 compare_type (const void *val1, const void *val2)
3730 return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
3733 /* Return an identifier for the mangled unqualified name for a
3734 conversion operator to TYPE. This mangling is not specified by the
3735 ABI spec; it is only used internally. */
3737 tree
3738 mangle_conv_op_name_for_type (const tree type)
3740 void **slot;
3741 tree identifier;
3743 if (type == error_mark_node)
3744 return error_mark_node;
3746 if (conv_type_names == NULL)
3747 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
3749 slot = htab_find_slot_with_hash (conv_type_names, type,
3750 (hashval_t) TYPE_UID (type), INSERT);
3751 identifier = (tree)*slot;
3752 if (!identifier)
3754 char buffer[64];
3756 /* Create a unique name corresponding to TYPE. */
3757 sprintf (buffer, "operator %lu",
3758 (unsigned long) htab_elements (conv_type_names));
3759 identifier = get_identifier (buffer);
3760 *slot = identifier;
3762 /* Hang TYPE off the identifier so it can be found easily later
3763 when performing conversions. */
3764 TREE_TYPE (identifier) = type;
3766 /* Set bits on the identifier so we know later it's a conversion. */
3767 IDENTIFIER_OPNAME_P (identifier) = 1;
3768 IDENTIFIER_TYPENAME_P (identifier) = 1;
3771 return identifier;
3774 /* Write out the appropriate string for this variable when generating
3775 another mangled name based on this one. */
3777 static void
3778 write_guarded_var_name (const tree variable)
3780 if (DECL_NAME (variable)
3781 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3782 /* The name of a guard variable for a reference temporary should refer
3783 to the reference, not the temporary. */
3784 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3785 else
3786 write_name (variable, /*ignore_local_scope=*/0);
3789 /* Return an identifier for the name of an initialization guard
3790 variable for indicated VARIABLE. */
3792 tree
3793 mangle_guard_variable (const tree variable)
3795 start_mangling (variable);
3796 write_string ("_ZGV");
3797 write_guarded_var_name (variable);
3798 return finish_mangling_get_identifier (/*warn=*/false);
3801 /* Return an identifier for the name of a thread_local initialization
3802 function for VARIABLE. */
3804 tree
3805 mangle_tls_init_fn (const tree variable)
3807 start_mangling (variable);
3808 write_string ("_ZTH");
3809 write_guarded_var_name (variable);
3810 return finish_mangling_get_identifier (/*warn=*/false);
3813 /* Return an identifier for the name of a thread_local wrapper
3814 function for VARIABLE. */
3816 #define TLS_WRAPPER_PREFIX "_ZTW"
3818 tree
3819 mangle_tls_wrapper_fn (const tree variable)
3821 start_mangling (variable);
3822 write_string (TLS_WRAPPER_PREFIX);
3823 write_guarded_var_name (variable);
3824 return finish_mangling_get_identifier (/*warn=*/false);
3827 /* Return true iff FN is a thread_local wrapper function. */
3829 bool
3830 decl_tls_wrapper_p (const tree fn)
3832 if (TREE_CODE (fn) != FUNCTION_DECL)
3833 return false;
3834 tree name = DECL_NAME (fn);
3835 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3836 strlen (TLS_WRAPPER_PREFIX)) == 0;
3839 /* Return an identifier for the name of a temporary variable used to
3840 initialize a static reference. This isn't part of the ABI, but we might
3841 as well call them something readable. */
3843 static GTY(()) int temp_count;
3845 tree
3846 mangle_ref_init_variable (const tree variable)
3848 start_mangling (variable);
3849 write_string ("_ZGR");
3850 write_name (variable, /*ignore_local_scope=*/0);
3851 /* Avoid name clashes with aggregate initialization of multiple
3852 references at once. */
3853 write_unsigned_number (temp_count++);
3854 return finish_mangling_get_identifier (/*warn=*/false);
3858 /* Foreign language type mangling section. */
3860 /* How to write the type codes for the integer Java type. */
3862 static void
3863 write_java_integer_type_codes (const tree type)
3865 if (type == java_int_type_node)
3866 write_char ('i');
3867 else if (type == java_short_type_node)
3868 write_char ('s');
3869 else if (type == java_byte_type_node)
3870 write_char ('c');
3871 else if (type == java_char_type_node)
3872 write_char ('w');
3873 else if (type == java_long_type_node)
3874 write_char ('x');
3875 else if (type == java_boolean_type_node)
3876 write_char ('b');
3877 else
3878 gcc_unreachable ();
3881 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3882 function generates a mangled name for the vtable map variable of
3883 the class type. For example, if the class type is
3884 "std::bad_exception", the mangled name for the class is
3885 "St13bad_exception". This function would generate the name
3886 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3887 "_VTV<std::bad_exception>::__vtable_map". */
3890 char *
3891 get_mangled_vtable_map_var_name (tree class_type)
3893 char *var_name = NULL;
3894 const char *prefix = "_ZN4_VTVI";
3895 const char *postfix = "E12__vtable_mapE";
3897 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3899 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3900 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3901 strlen (prefix) +
3902 strlen (postfix) + 1;
3904 var_name = (char *) xmalloc (len);
3906 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3908 return var_name;
3911 #include "gt-cp-mangle.h"