2014-07-30 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / cp / mangle.c
blob40508ab0cc34bcb29ba2bb2526ad92af5655b18d
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"
60 #include "wide-int.h"
62 /* Debugging support. */
64 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
65 #ifndef DEBUG_MANGLE
66 #define DEBUG_MANGLE 0
67 #endif
69 /* Macros for tracing the write_* functions. */
70 #if DEBUG_MANGLE
71 # define MANGLE_TRACE(FN, INPUT) \
72 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
73 # define MANGLE_TRACE_TREE(FN, NODE) \
74 fprintf (stderr, " %-24s: %-24s (%p)\n", \
75 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
76 #else
77 # define MANGLE_TRACE(FN, INPUT)
78 # define MANGLE_TRACE_TREE(FN, NODE)
79 #endif
81 /* Nonzero if NODE is a class template-id. We can't rely on
82 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
83 that hard to distinguish A<T> from A, where A<T> is the type as
84 instantiated outside of the template, and A is the type used
85 without parameters inside the template. */
86 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
87 (TYPE_LANG_SPECIFIC (NODE) != NULL \
88 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
89 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
90 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
92 /* Things we only need one of. This module is not reentrant. */
93 typedef struct GTY(()) globals {
94 /* An array of the current substitution candidates, in the order
95 we've seen them. */
96 vec<tree, va_gc> *substitutions;
98 /* The entity that is being mangled. */
99 tree GTY ((skip)) entity;
101 /* How many parameter scopes we are inside. */
102 int parm_depth;
104 /* True if the mangling will be different in a future version of the
105 ABI. */
106 bool need_abi_warning;
107 } globals;
109 static GTY (()) globals G;
111 /* The obstack on which we build mangled names. */
112 static struct obstack *mangle_obstack;
114 /* The obstack on which we build mangled names that are not going to
115 be IDENTIFIER_NODEs. */
116 static struct obstack name_obstack;
118 /* The first object on the name_obstack; we use this to free memory
119 allocated on the name_obstack. */
120 static void *name_base;
122 /* Indices into subst_identifiers. These are identifiers used in
123 special substitution rules. */
124 typedef enum
126 SUBID_ALLOCATOR,
127 SUBID_BASIC_STRING,
128 SUBID_CHAR_TRAITS,
129 SUBID_BASIC_ISTREAM,
130 SUBID_BASIC_OSTREAM,
131 SUBID_BASIC_IOSTREAM,
132 SUBID_MAX
134 substitution_identifier_index_t;
136 /* For quick substitution checks, look up these common identifiers
137 once only. */
138 static GTY(()) tree subst_identifiers[SUBID_MAX];
140 /* Single-letter codes for builtin integer types, defined in
141 <builtin-type>. These are indexed by integer_type_kind values. */
142 static const char
143 integer_type_codes[itk_none] =
145 'c', /* itk_char */
146 'a', /* itk_signed_char */
147 'h', /* itk_unsigned_char */
148 's', /* itk_short */
149 't', /* itk_unsigned_short */
150 'i', /* itk_int */
151 'j', /* itk_unsigned_int */
152 'l', /* itk_long */
153 'm', /* itk_unsigned_long */
154 'x', /* itk_long_long */
155 'y', /* itk_unsigned_long_long */
156 'n', /* itk_int128 */
157 'o', /* itk_unsigned_int128 */
160 static int decl_is_template_id (const tree, tree* const);
162 /* Functions for handling substitutions. */
164 static inline tree canonicalize_for_substitution (tree);
165 static void add_substitution (tree);
166 static inline int is_std_substitution (const tree,
167 const substitution_identifier_index_t);
168 static inline int is_std_substitution_char (const tree,
169 const substitution_identifier_index_t);
170 static int find_substitution (tree);
171 static void mangle_call_offset (const tree, const tree);
173 /* Functions for emitting mangled representations of things. */
175 static void write_mangled_name (const tree, bool);
176 static void write_encoding (const tree);
177 static void write_name (tree, const int);
178 static void write_abi_tags (tree);
179 static void write_unscoped_name (const tree);
180 static void write_unscoped_template_name (const tree);
181 static void write_nested_name (const tree);
182 static void write_prefix (const tree);
183 static void write_template_prefix (const tree);
184 static void write_unqualified_name (tree);
185 static void write_conversion_operator_name (const tree);
186 static void write_source_name (tree);
187 static void write_literal_operator_name (tree);
188 static void write_unnamed_type_name (const tree);
189 static void write_closure_type_name (const tree);
190 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
191 const unsigned int);
192 static void write_number (unsigned HOST_WIDE_INT, const int,
193 const unsigned int);
194 static void write_compact_number (int num);
195 static void write_integer_cst (const tree);
196 static void write_real_cst (const tree);
197 static void write_identifier (const char *);
198 static void write_special_name_constructor (const tree);
199 static void write_special_name_destructor (const tree);
200 static void write_type (tree);
201 static int write_CV_qualifiers_for_type (const tree);
202 static void write_builtin_type (tree);
203 static void write_function_type (const tree);
204 static void write_bare_function_type (const tree, const int, const tree);
205 static void write_method_parms (tree, const int, const tree);
206 static void write_class_enum_type (const tree);
207 static void write_template_args (tree);
208 static void write_expression (tree);
209 static void write_template_arg_literal (const tree);
210 static void write_template_arg (tree);
211 static void write_template_template_arg (const tree);
212 static void write_array_type (const tree);
213 static void write_pointer_to_member_type (const tree);
214 static void write_template_param (const tree);
215 static void write_template_template_param (const tree);
216 static void write_substitution (const int);
217 static int discriminator_for_local_entity (tree);
218 static int discriminator_for_string_literal (tree, tree);
219 static void write_discriminator (const int);
220 static void write_local_name (tree, const tree, const tree);
221 static void dump_substitution_candidates (void);
222 static tree mangle_decl_string (const tree);
223 static int local_class_index (tree);
225 /* Control functions. */
227 static inline void start_mangling (const tree);
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 = TYPE_NAME_STRING (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 write_string ("_Z");
666 write_source_name (DECL_NAME (decl));
669 else if (VAR_P (decl)
670 /* The names of non-static global variables aren't mangled. */
671 && DECL_EXTERNAL_LINKAGE_P (decl)
672 && (CP_DECL_CONTEXT (decl) == global_namespace
673 /* And neither are `extern "C"' variables. */
674 || DECL_EXTERN_C_P (decl)))
676 goto unmangled_name;
678 else
680 write_string ("_Z");
681 write_encoding (decl);
685 /* <encoding> ::= <function name> <bare-function-type>
686 ::= <data name> */
688 static void
689 write_encoding (const tree decl)
691 MANGLE_TRACE_TREE ("encoding", decl);
693 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
695 /* For overloaded operators write just the mangled name
696 without arguments. */
697 if (DECL_OVERLOADED_OPERATOR_P (decl))
698 write_name (decl, /*ignore_local_scope=*/0);
699 else
700 write_source_name (DECL_NAME (decl));
701 return;
704 write_name (decl, /*ignore_local_scope=*/0);
705 if (TREE_CODE (decl) == FUNCTION_DECL)
707 tree fn_type;
708 tree d;
710 if (decl_is_template_id (decl, NULL))
712 fn_type = get_mostly_instantiated_function_type (decl);
713 /* FN_TYPE will not have parameter types for in-charge or
714 VTT parameters. Therefore, we pass NULL_TREE to
715 write_bare_function_type -- otherwise, it will get
716 confused about which artificial parameters to skip. */
717 d = NULL_TREE;
719 else
721 fn_type = TREE_TYPE (decl);
722 d = decl;
725 write_bare_function_type (fn_type,
726 (!DECL_CONSTRUCTOR_P (decl)
727 && !DECL_DESTRUCTOR_P (decl)
728 && !DECL_CONV_FN_P (decl)
729 && decl_is_template_id (decl, NULL)),
734 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
735 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
737 static tree
738 decl_mangling_context (tree decl)
740 tree tcontext = targetm.cxx.decl_mangling_context (decl);
742 if (tcontext != NULL_TREE)
743 return tcontext;
745 if (TREE_CODE (decl) == TEMPLATE_DECL
746 && DECL_TEMPLATE_RESULT (decl))
747 decl = DECL_TEMPLATE_RESULT (decl);
749 if (TREE_CODE (decl) == TYPE_DECL
750 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
752 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
753 if (extra)
754 return extra;
756 else if (TREE_CODE (decl) == TYPE_DECL
757 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
758 /* template type parms have no mangling context. */
759 return NULL_TREE;
760 return CP_DECL_CONTEXT (decl);
763 /* <name> ::= <unscoped-name>
764 ::= <unscoped-template-name> <template-args>
765 ::= <nested-name>
766 ::= <local-name>
768 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
769 called from <local-name>, which mangles the enclosing scope
770 elsewhere and then uses this function to mangle just the part
771 underneath the function scope. So don't use the <local-name>
772 production, to avoid an infinite recursion. */
774 static void
775 write_name (tree decl, const int ignore_local_scope)
777 tree context;
779 MANGLE_TRACE_TREE ("name", decl);
781 if (TREE_CODE (decl) == TYPE_DECL)
783 /* In case this is a typedef, fish out the corresponding
784 TYPE_DECL for the main variant. */
785 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
788 context = decl_mangling_context (decl);
790 gcc_assert (context != NULL_TREE);
792 if (abi_version_crosses (7)
793 && ignore_local_scope
794 && TREE_CODE (context) == PARM_DECL)
795 G.need_abi_warning = 1;
797 /* A decl in :: or ::std scope is treated specially. The former is
798 mangled using <unscoped-name> or <unscoped-template-name>, the
799 latter with a special substitution. Also, a name that is
800 directly in a local function scope is also mangled with
801 <unscoped-name> rather than a full <nested-name>. */
802 if (context == global_namespace
803 || DECL_NAMESPACE_STD_P (context)
804 || (ignore_local_scope
805 && (TREE_CODE (context) == FUNCTION_DECL
806 || (abi_version_at_least (7)
807 && TREE_CODE (context) == PARM_DECL))))
809 tree template_info;
810 /* Is this a template instance? */
811 if (decl_is_template_id (decl, &template_info))
813 /* Yes: use <unscoped-template-name>. */
814 write_unscoped_template_name (TI_TEMPLATE (template_info));
815 write_template_args (TI_ARGS (template_info));
817 else
818 /* Everything else gets an <unqualified-name>. */
819 write_unscoped_name (decl);
821 else
823 /* Handle local names, unless we asked not to (that is, invoked
824 under <local-name>, to handle only the part of the name under
825 the local scope). */
826 if (!ignore_local_scope)
828 /* Scan up the list of scope context, looking for a
829 function. If we find one, this entity is in local
830 function scope. local_entity tracks context one scope
831 level down, so it will contain the element that's
832 directly in that function's scope, either decl or one of
833 its enclosing scopes. */
834 tree local_entity = decl;
835 while (context != global_namespace)
837 /* Make sure we're always dealing with decls. */
838 if (TYPE_P (context))
839 context = TYPE_NAME (context);
840 /* Is this a function? */
841 if (TREE_CODE (context) == FUNCTION_DECL
842 || TREE_CODE (context) == PARM_DECL)
844 /* Yes, we have local scope. Use the <local-name>
845 production for the innermost function scope. */
846 write_local_name (context, local_entity, decl);
847 return;
849 /* Up one scope level. */
850 local_entity = context;
851 context = decl_mangling_context (context);
854 /* No local scope found? Fall through to <nested-name>. */
857 /* Other decls get a <nested-name> to encode their scope. */
858 write_nested_name (decl);
862 /* <unscoped-name> ::= <unqualified-name>
863 ::= St <unqualified-name> # ::std:: */
865 static void
866 write_unscoped_name (const tree decl)
868 tree context = decl_mangling_context (decl);
870 MANGLE_TRACE_TREE ("unscoped-name", decl);
872 /* Is DECL in ::std? */
873 if (DECL_NAMESPACE_STD_P (context))
875 write_string ("St");
876 write_unqualified_name (decl);
878 else
880 /* If not, it should be either in the global namespace, or directly
881 in a local function scope. A lambda can also be mangled in the
882 scope of a default argument. */
883 gcc_assert (context == global_namespace
884 || TREE_CODE (context) == PARM_DECL
885 || TREE_CODE (context) == FUNCTION_DECL);
887 write_unqualified_name (decl);
891 /* <unscoped-template-name> ::= <unscoped-name>
892 ::= <substitution> */
894 static void
895 write_unscoped_template_name (const tree decl)
897 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
899 if (find_substitution (decl))
900 return;
901 write_unscoped_name (decl);
902 add_substitution (decl);
905 /* Write the nested name, including CV-qualifiers, of DECL.
907 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
908 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
910 <ref-qualifier> ::= R # & ref-qualifier
911 ::= O # && ref-qualifier
912 <CV-qualifiers> ::= [r] [V] [K] */
914 static void
915 write_nested_name (const tree decl)
917 tree template_info;
919 MANGLE_TRACE_TREE ("nested-name", decl);
921 write_char ('N');
923 /* Write CV-qualifiers, if this is a member function. */
924 if (TREE_CODE (decl) == FUNCTION_DECL
925 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
927 if (DECL_VOLATILE_MEMFUNC_P (decl))
928 write_char ('V');
929 if (DECL_CONST_MEMFUNC_P (decl))
930 write_char ('K');
931 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
933 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
934 write_char ('O');
935 else
936 write_char ('R');
940 /* Is this a template instance? */
941 if (decl_is_template_id (decl, &template_info))
943 /* Yes, use <template-prefix>. */
944 write_template_prefix (decl);
945 write_template_args (TI_ARGS (template_info));
947 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
949 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
950 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
952 write_template_prefix (decl);
953 write_template_args (TREE_OPERAND (name, 1));
955 else
957 write_prefix (decl_mangling_context (decl));
958 write_unqualified_name (decl);
961 else
963 /* No, just use <prefix> */
964 write_prefix (decl_mangling_context (decl));
965 write_unqualified_name (decl);
967 write_char ('E');
970 /* <prefix> ::= <prefix> <unqualified-name>
971 ::= <template-param>
972 ::= <template-prefix> <template-args>
973 ::= <decltype>
974 ::= # empty
975 ::= <substitution> */
977 static void
978 write_prefix (const tree node)
980 tree decl;
981 /* Non-NULL if NODE represents a template-id. */
982 tree template_info = NULL;
984 if (node == NULL
985 || node == global_namespace)
986 return;
988 MANGLE_TRACE_TREE ("prefix", node);
990 if (TREE_CODE (node) == DECLTYPE_TYPE)
992 write_type (node);
993 return;
996 if (find_substitution (node))
997 return;
999 if (DECL_P (node))
1001 /* If this is a function or parm decl, that means we've hit function
1002 scope, so this prefix must be for a local name. In this
1003 case, we're under the <local-name> production, which encodes
1004 the enclosing function scope elsewhere. So don't continue
1005 here. */
1006 if (TREE_CODE (node) == FUNCTION_DECL
1007 || TREE_CODE (node) == PARM_DECL)
1008 return;
1010 decl = node;
1011 decl_is_template_id (decl, &template_info);
1013 else
1015 /* Node is a type. */
1016 decl = TYPE_NAME (node);
1017 if (CLASSTYPE_TEMPLATE_ID_P (node))
1018 template_info = TYPE_TEMPLATE_INFO (node);
1021 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1022 write_template_param (node);
1023 else if (template_info != NULL)
1024 /* Templated. */
1026 write_template_prefix (decl);
1027 write_template_args (TI_ARGS (template_info));
1029 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1031 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1032 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1034 write_template_prefix (decl);
1035 write_template_args (TREE_OPERAND (name, 1));
1037 else
1039 write_prefix (decl_mangling_context (decl));
1040 write_unqualified_name (decl);
1043 else
1044 /* Not templated. */
1046 write_prefix (decl_mangling_context (decl));
1047 write_unqualified_name (decl);
1048 if (VAR_P (decl)
1049 || TREE_CODE (decl) == FIELD_DECL)
1051 /* <data-member-prefix> := <member source-name> M */
1052 write_char ('M');
1053 return;
1057 add_substitution (node);
1060 /* <template-prefix> ::= <prefix> <template component>
1061 ::= <template-param>
1062 ::= <substitution> */
1064 static void
1065 write_template_prefix (const tree node)
1067 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1068 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1069 tree context = decl_mangling_context (decl);
1070 tree template_info;
1071 tree templ;
1072 tree substitution;
1074 MANGLE_TRACE_TREE ("template-prefix", node);
1076 /* Find the template decl. */
1077 if (decl_is_template_id (decl, &template_info))
1078 templ = TI_TEMPLATE (template_info);
1079 else if (TREE_CODE (type) == TYPENAME_TYPE)
1080 /* For a typename type, all we have is the name. */
1081 templ = DECL_NAME (decl);
1082 else
1084 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1086 templ = TYPE_TI_TEMPLATE (type);
1089 /* For a member template, though, the template name for the
1090 innermost name must have all the outer template levels
1091 instantiated. For instance, consider
1093 template<typename T> struct Outer {
1094 template<typename U> struct Inner {};
1097 The template name for `Inner' in `Outer<int>::Inner<float>' is
1098 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1099 levels separately, so there's no TEMPLATE_DECL available for this
1100 (there's only `Outer<T>::Inner<U>').
1102 In order to get the substitutions right, we create a special
1103 TREE_LIST to represent the substitution candidate for a nested
1104 template. The TREE_PURPOSE is the template's context, fully
1105 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1106 template.
1108 So, for the example above, `Outer<int>::Inner' is represented as a
1109 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1110 and whose value is `Outer<T>::Inner<U>'. */
1111 if (TYPE_P (context))
1112 substitution = build_tree_list (context, templ);
1113 else
1114 substitution = templ;
1116 if (find_substitution (substitution))
1117 return;
1119 if (TREE_TYPE (templ)
1120 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1121 write_template_param (TREE_TYPE (templ));
1122 else
1124 write_prefix (context);
1125 write_unqualified_name (decl);
1128 add_substitution (substitution);
1131 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1132 mangled through special entry points.
1134 <unqualified-name> ::= <operator-name>
1135 ::= <special-name>
1136 ::= <source-name>
1137 ::= <unnamed-type-name>
1138 ::= <local-source-name>
1140 <local-source-name> ::= L <source-name> <discriminator> */
1142 static void
1143 write_unqualified_id (tree identifier)
1145 if (IDENTIFIER_TYPENAME_P (identifier))
1146 write_conversion_operator_name (TREE_TYPE (identifier));
1147 else if (IDENTIFIER_OPNAME_P (identifier))
1149 int i;
1150 const char *mangled_name = NULL;
1152 /* Unfortunately, there is no easy way to go from the
1153 name of the operator back to the corresponding tree
1154 code. */
1155 for (i = 0; i < MAX_TREE_CODES; ++i)
1156 if (operator_name_info[i].identifier == identifier)
1158 /* The ABI says that we prefer binary operator
1159 names to unary operator names. */
1160 if (operator_name_info[i].arity == 2)
1162 mangled_name = operator_name_info[i].mangled_name;
1163 break;
1165 else if (!mangled_name)
1166 mangled_name = operator_name_info[i].mangled_name;
1168 else if (assignment_operator_name_info[i].identifier
1169 == identifier)
1171 mangled_name
1172 = assignment_operator_name_info[i].mangled_name;
1173 break;
1175 write_string (mangled_name);
1177 else if (UDLIT_OPER_P (identifier))
1178 write_literal_operator_name (identifier);
1179 else
1180 write_source_name (identifier);
1183 static void
1184 write_unqualified_name (tree decl)
1186 MANGLE_TRACE_TREE ("unqualified-name", decl);
1188 if (identifier_p (decl))
1190 write_unqualified_id (decl);
1191 return;
1194 bool found = false;
1196 if (DECL_NAME (decl) == NULL_TREE)
1198 found = true;
1199 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1200 write_source_name (DECL_ASSEMBLER_NAME (decl));
1202 else if (DECL_DECLARES_FUNCTION_P (decl))
1204 found = true;
1205 if (DECL_CONSTRUCTOR_P (decl))
1206 write_special_name_constructor (decl);
1207 else if (DECL_DESTRUCTOR_P (decl))
1208 write_special_name_destructor (decl);
1209 else if (DECL_CONV_FN_P (decl))
1211 /* Conversion operator. Handle it right here.
1212 <operator> ::= cv <type> */
1213 tree type;
1214 if (decl_is_template_id (decl, NULL))
1216 tree fn_type;
1217 fn_type = get_mostly_instantiated_function_type (decl);
1218 type = TREE_TYPE (fn_type);
1220 else if (FNDECL_USED_AUTO (decl))
1221 type = (DECL_STRUCT_FUNCTION (decl)->language
1222 ->x_auto_return_pattern);
1223 else
1224 type = DECL_CONV_FN_TYPE (decl);
1225 write_conversion_operator_name (type);
1227 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1229 operator_name_info_t *oni;
1230 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1231 oni = assignment_operator_name_info;
1232 else
1233 oni = operator_name_info;
1235 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1237 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1238 write_literal_operator_name (DECL_NAME (decl));
1239 else
1240 found = false;
1243 if (found)
1244 /* OK */;
1245 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1246 && DECL_NAMESPACE_SCOPE_P (decl)
1247 && decl_linkage (decl) == lk_internal)
1249 MANGLE_TRACE_TREE ("local-source-name", decl);
1250 write_char ('L');
1251 write_source_name (DECL_NAME (decl));
1252 /* The default discriminator is 1, and that's all we ever use,
1253 so there's no code to output one here. */
1255 else
1257 tree type = TREE_TYPE (decl);
1259 if (TREE_CODE (decl) == TYPE_DECL
1260 && TYPE_ANONYMOUS_P (type))
1261 write_unnamed_type_name (type);
1262 else if (TREE_CODE (decl) == TYPE_DECL
1263 && LAMBDA_TYPE_P (type))
1264 write_closure_type_name (type);
1265 else
1266 write_source_name (DECL_NAME (decl));
1269 /* We use the ABI tags from the primary template, ignoring tags on any
1270 specializations. This is necessary because C++ doesn't require a
1271 specialization to be declared before it is used unless the use
1272 requires a complete type, but we need to get the tags right on
1273 incomplete types as well. */
1274 if (tree tmpl = most_general_template (decl))
1275 decl = DECL_TEMPLATE_RESULT (tmpl);
1276 /* Don't crash on an unbound class template. */
1277 if (decl)
1279 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1280 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1281 : DECL_ATTRIBUTES (decl));
1282 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);
1502 widest_int abs_value = wi::abs (wi::to_widest (cst));
1503 if (!wi::fits_uhwi_p (abs_value))
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 = wide_int_to_tree (type, cst);
1531 if (sign < 0)
1533 write_char ('n');
1534 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1538 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1539 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1540 unsigned c;
1542 done = integer_zerop (d);
1543 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1544 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1545 done ? 1 : chunk_digits);
1546 ptr -= c;
1547 count += c;
1548 n = d;
1550 while (!done);
1551 write_chars (ptr, count);
1553 else
1555 /* A small num. */
1556 if (sign < 0)
1557 write_char ('n');
1558 write_unsigned_number (abs_value.to_uhwi ());
1562 /* Write out a floating-point literal.
1564 "Floating-point literals are encoded using the bit pattern of the
1565 target processor's internal representation of that number, as a
1566 fixed-length lowercase hexadecimal string, high-order bytes first
1567 (even if the target processor would store low-order bytes first).
1568 The "n" prefix is not used for floating-point literals; the sign
1569 bit is encoded with the rest of the number.
1571 Here are some examples, assuming the IEEE standard representation
1572 for floating point numbers. (Spaces are for readability, not
1573 part of the encoding.)
1575 1.0f Lf 3f80 0000 E
1576 -1.0f Lf bf80 0000 E
1577 1.17549435e-38f Lf 0080 0000 E
1578 1.40129846e-45f Lf 0000 0001 E
1579 0.0f Lf 0000 0000 E"
1581 Caller is responsible for the Lx and the E. */
1582 static void
1583 write_real_cst (const tree value)
1585 long target_real[4]; /* largest supported float */
1586 char buffer[9]; /* eight hex digits in a 32-bit number */
1587 int i, limit, dir;
1589 tree type = TREE_TYPE (value);
1590 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1592 real_to_target (target_real, &TREE_REAL_CST (value),
1593 TYPE_MODE (type));
1595 /* The value in target_real is in the target word order,
1596 so we must write it out backward if that happens to be
1597 little-endian. write_number cannot be used, it will
1598 produce uppercase. */
1599 if (FLOAT_WORDS_BIG_ENDIAN)
1600 i = 0, limit = words, dir = 1;
1601 else
1602 i = words - 1, limit = -1, dir = -1;
1604 for (; i != limit; i += dir)
1606 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1607 write_chars (buffer, 8);
1611 /* Non-terminal <identifier>.
1613 <identifier> ::= </unqualified source code identifier> */
1615 static void
1616 write_identifier (const char *identifier)
1618 MANGLE_TRACE ("identifier", identifier);
1619 write_string (identifier);
1622 /* Handle constructor productions of non-terminal <special-name>.
1623 CTOR is a constructor FUNCTION_DECL.
1625 <special-name> ::= C1 # complete object constructor
1626 ::= C2 # base object constructor
1627 ::= C3 # complete object allocating constructor
1629 Currently, allocating constructors are never used. */
1631 static void
1632 write_special_name_constructor (const tree ctor)
1634 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1635 write_string ("C2");
1636 /* This is the old-style "[unified]" constructor.
1637 In some cases, we may emit this function and call
1638 it from the clones in order to share code and save space. */
1639 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1640 write_string ("C4");
1641 else
1643 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1644 write_string ("C1");
1648 /* Handle destructor productions of non-terminal <special-name>.
1649 DTOR is a destructor FUNCTION_DECL.
1651 <special-name> ::= D0 # deleting (in-charge) destructor
1652 ::= D1 # complete object (in-charge) destructor
1653 ::= D2 # base object (not-in-charge) destructor */
1655 static void
1656 write_special_name_destructor (const tree dtor)
1658 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1659 write_string ("D0");
1660 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1661 write_string ("D2");
1662 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1663 /* This is the old-style "[unified]" destructor.
1664 In some cases, we may emit this function and call
1665 it from the clones in order to share code and save space. */
1666 write_string ("D4");
1667 else
1669 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1670 write_string ("D1");
1674 /* Scan the vector of local classes and return how many others with the
1675 same name (or same no name) and context precede ENTITY. */
1677 static int
1678 local_class_index (tree entity)
1680 int ix, discriminator = 0;
1681 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1682 : TYPE_IDENTIFIER (entity));
1683 tree ctx = TYPE_CONTEXT (entity);
1684 for (ix = 0; ; ix++)
1686 tree type = (*local_classes)[ix];
1687 if (type == entity)
1688 return discriminator;
1689 if (TYPE_CONTEXT (type) == ctx
1690 && (name ? TYPE_IDENTIFIER (type) == name
1691 : TYPE_ANONYMOUS_P (type)))
1692 ++discriminator;
1694 gcc_unreachable ();
1697 /* Return the discriminator for ENTITY appearing inside
1698 FUNCTION. The discriminator is the lexical ordinal of VAR among
1699 entities with the same name in the same FUNCTION. */
1701 static int
1702 discriminator_for_local_entity (tree entity)
1704 if (DECL_DISCRIMINATOR_P (entity))
1706 if (DECL_DISCRIMINATOR_SET_P (entity))
1707 return DECL_DISCRIMINATOR (entity);
1708 else
1709 /* The first entity with a particular name doesn't get
1710 DECL_DISCRIMINATOR set up. */
1711 return 0;
1713 else if (TREE_CODE (entity) == TYPE_DECL)
1715 /* Scan the list of local classes. */
1716 entity = TREE_TYPE (entity);
1718 /* Lambdas and unnamed types have their own discriminators. */
1719 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1720 return 0;
1722 return local_class_index (entity);
1724 else
1725 gcc_unreachable ();
1728 /* Return the discriminator for STRING, a string literal used inside
1729 FUNCTION. The discriminator is the lexical ordinal of STRING among
1730 string literals used in FUNCTION. */
1732 static int
1733 discriminator_for_string_literal (tree /*function*/,
1734 tree /*string*/)
1736 /* For now, we don't discriminate amongst string literals. */
1737 return 0;
1740 /* <discriminator> := _ <number>
1742 The discriminator is used only for the second and later occurrences
1743 of the same name within a single function. In this case <number> is
1744 n - 2, if this is the nth occurrence, in lexical order. */
1746 static void
1747 write_discriminator (const int discriminator)
1749 /* If discriminator is zero, don't write anything. Otherwise... */
1750 if (discriminator > 0)
1752 write_char ('_');
1753 write_unsigned_number (discriminator - 1);
1757 /* Mangle the name of a function-scope entity. FUNCTION is the
1758 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1759 default argument scope. ENTITY is the decl for the entity itself.
1760 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1761 either ENTITY itself or an enclosing scope of ENTITY.
1763 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1764 := Z <function encoding> E s [<discriminator>]
1765 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1767 static void
1768 write_local_name (tree function, const tree local_entity,
1769 const tree entity)
1771 tree parm = NULL_TREE;
1773 MANGLE_TRACE_TREE ("local-name", entity);
1775 if (TREE_CODE (function) == PARM_DECL)
1777 parm = function;
1778 function = DECL_CONTEXT (parm);
1781 write_char ('Z');
1782 write_encoding (function);
1783 write_char ('E');
1785 /* For this purpose, parameters are numbered from right-to-left. */
1786 if (parm)
1788 tree t;
1789 int i = 0;
1790 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1792 if (t == parm)
1793 i = 1;
1794 else if (i)
1795 ++i;
1797 write_char ('d');
1798 write_compact_number (i - 1);
1801 if (TREE_CODE (entity) == STRING_CST)
1803 write_char ('s');
1804 write_discriminator (discriminator_for_string_literal (function,
1805 entity));
1807 else
1809 /* Now the <entity name>. Let write_name know its being called
1810 from <local-name>, so it doesn't try to process the enclosing
1811 function scope again. */
1812 write_name (entity, /*ignore_local_scope=*/1);
1813 write_discriminator (discriminator_for_local_entity (local_entity));
1817 /* Non-terminals <type> and <CV-qualifier>.
1819 <type> ::= <builtin-type>
1820 ::= <function-type>
1821 ::= <class-enum-type>
1822 ::= <array-type>
1823 ::= <pointer-to-member-type>
1824 ::= <template-param>
1825 ::= <substitution>
1826 ::= <CV-qualifier>
1827 ::= P <type> # pointer-to
1828 ::= R <type> # reference-to
1829 ::= C <type> # complex pair (C 2000)
1830 ::= G <type> # imaginary (C 2000) [not supported]
1831 ::= U <source-name> <type> # vendor extended type qualifier
1833 C++0x extensions
1835 <type> ::= RR <type> # rvalue reference-to
1836 <type> ::= Dt <expression> # decltype of an id-expression or
1837 # class member access
1838 <type> ::= DT <expression> # decltype of an expression
1839 <type> ::= Dn # decltype of nullptr
1841 TYPE is a type node. */
1843 static void
1844 write_type (tree type)
1846 /* This gets set to nonzero if TYPE turns out to be a (possibly
1847 CV-qualified) builtin type. */
1848 int is_builtin_type = 0;
1850 MANGLE_TRACE_TREE ("type", type);
1852 if (type == error_mark_node)
1853 return;
1855 type = canonicalize_for_substitution (type);
1856 if (find_substitution (type))
1857 return;
1860 if (write_CV_qualifiers_for_type (type) > 0)
1861 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1862 mangle the unqualified type. The recursive call is needed here
1863 since both the qualified and unqualified types are substitution
1864 candidates. */
1866 tree t = TYPE_MAIN_VARIANT (type);
1867 if (TREE_CODE (t) == FUNCTION_TYPE
1868 || TREE_CODE (t) == METHOD_TYPE)
1870 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1871 if (abi_version_at_least (8))
1872 /* Avoid adding the unqualified function type as a substitution. */
1873 write_function_type (t);
1874 else
1875 write_type (t);
1876 if (abi_version_crosses (8))
1877 G.need_abi_warning = 1;
1879 else
1880 write_type (t);
1882 else if (TREE_CODE (type) == ARRAY_TYPE)
1883 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1884 so that the cv-qualification of the element type is available
1885 in write_array_type. */
1886 write_array_type (type);
1887 else
1889 tree type_orig = type;
1891 /* See through any typedefs. */
1892 type = TYPE_MAIN_VARIANT (type);
1893 if (TREE_CODE (type) == FUNCTION_TYPE
1894 || TREE_CODE (type) == METHOD_TYPE)
1895 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1897 /* According to the C++ ABI, some library classes are passed the
1898 same as the scalar type of their single member and use the same
1899 mangling. */
1900 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1901 type = TREE_TYPE (first_field (type));
1903 if (TYPE_PTRDATAMEM_P (type))
1904 write_pointer_to_member_type (type);
1905 else
1907 /* Handle any target-specific fundamental types. */
1908 const char *target_mangling
1909 = targetm.mangle_type (type_orig);
1911 if (target_mangling)
1913 write_string (target_mangling);
1914 /* Add substitutions for types other than fundamental
1915 types. */
1916 if (!VOID_TYPE_P (type)
1917 && TREE_CODE (type) != INTEGER_TYPE
1918 && TREE_CODE (type) != REAL_TYPE
1919 && TREE_CODE (type) != BOOLEAN_TYPE)
1920 add_substitution (type);
1921 return;
1924 switch (TREE_CODE (type))
1926 case VOID_TYPE:
1927 case BOOLEAN_TYPE:
1928 case INTEGER_TYPE: /* Includes wchar_t. */
1929 case REAL_TYPE:
1930 case FIXED_POINT_TYPE:
1932 /* If this is a typedef, TYPE may not be one of
1933 the standard builtin type nodes, but an alias of one. Use
1934 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1935 write_builtin_type (TYPE_MAIN_VARIANT (type));
1936 ++is_builtin_type;
1938 break;
1940 case COMPLEX_TYPE:
1941 write_char ('C');
1942 write_type (TREE_TYPE (type));
1943 break;
1945 case FUNCTION_TYPE:
1946 case METHOD_TYPE:
1947 write_function_type (type);
1948 break;
1950 case UNION_TYPE:
1951 case RECORD_TYPE:
1952 case ENUMERAL_TYPE:
1953 /* A pointer-to-member function is represented as a special
1954 RECORD_TYPE, so check for this first. */
1955 if (TYPE_PTRMEMFUNC_P (type))
1956 write_pointer_to_member_type (type);
1957 else
1958 write_class_enum_type (type);
1959 break;
1961 case TYPENAME_TYPE:
1962 case UNBOUND_CLASS_TEMPLATE:
1963 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1964 ordinary nested names. */
1965 write_nested_name (TYPE_STUB_DECL (type));
1966 break;
1968 case POINTER_TYPE:
1969 case REFERENCE_TYPE:
1970 if (TYPE_PTR_P (type))
1971 write_char ('P');
1972 else if (TYPE_REF_IS_RVALUE (type))
1973 write_char ('O');
1974 else
1975 write_char ('R');
1977 tree target = TREE_TYPE (type);
1978 /* Attribute const/noreturn are not reflected in mangling.
1979 We strip them here rather than at a lower level because
1980 a typedef or template argument can have function type
1981 with function-cv-quals (that use the same representation),
1982 but you can't have a pointer/reference to such a type. */
1983 if (TREE_CODE (target) == FUNCTION_TYPE)
1985 if (abi_version_crosses (5)
1986 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
1987 G.need_abi_warning = 1;
1988 if (abi_version_at_least (5))
1989 target = build_qualified_type (target, TYPE_UNQUALIFIED);
1991 write_type (target);
1993 break;
1995 case TEMPLATE_TYPE_PARM:
1996 if (is_auto (type))
1998 if (AUTO_IS_DECLTYPE (type))
1999 write_identifier ("Dc");
2000 else
2001 write_identifier ("Da");
2002 ++is_builtin_type;
2003 break;
2005 /* else fall through. */
2006 case TEMPLATE_PARM_INDEX:
2007 write_template_param (type);
2008 break;
2010 case TEMPLATE_TEMPLATE_PARM:
2011 write_template_template_param (type);
2012 break;
2014 case BOUND_TEMPLATE_TEMPLATE_PARM:
2015 write_template_template_param (type);
2016 write_template_args
2017 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2018 break;
2020 case VECTOR_TYPE:
2021 if (abi_version_at_least (4))
2023 write_string ("Dv");
2024 /* Non-constant vector size would be encoded with
2025 _ expression, but we don't support that yet. */
2026 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2027 write_char ('_');
2029 else
2030 write_string ("U8__vector");
2031 if (abi_version_crosses (4))
2032 G.need_abi_warning = 1;
2033 write_type (TREE_TYPE (type));
2034 break;
2036 case TYPE_PACK_EXPANSION:
2037 write_string ("Dp");
2038 write_type (PACK_EXPANSION_PATTERN (type));
2039 break;
2041 case DECLTYPE_TYPE:
2042 /* These shouldn't make it into mangling. */
2043 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2044 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2046 /* In ABI <5, we stripped decltype of a plain decl. */
2047 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2049 tree expr = DECLTYPE_TYPE_EXPR (type);
2050 tree etype = NULL_TREE;
2051 switch (TREE_CODE (expr))
2053 case VAR_DECL:
2054 case PARM_DECL:
2055 case RESULT_DECL:
2056 case FUNCTION_DECL:
2057 case CONST_DECL:
2058 case TEMPLATE_PARM_INDEX:
2059 etype = TREE_TYPE (expr);
2060 break;
2062 default:
2063 break;
2066 if (etype && !type_uses_auto (etype))
2068 if (abi_version_crosses (5))
2069 G.need_abi_warning = 1;
2070 if (!abi_version_at_least (5))
2072 write_type (etype);
2073 return;
2078 write_char ('D');
2079 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2080 write_char ('t');
2081 else
2082 write_char ('T');
2083 ++cp_unevaluated_operand;
2084 write_expression (DECLTYPE_TYPE_EXPR (type));
2085 --cp_unevaluated_operand;
2086 write_char ('E');
2087 break;
2089 case NULLPTR_TYPE:
2090 write_string ("Dn");
2091 if (abi_version_at_least (7))
2092 ++is_builtin_type;
2093 if (abi_version_crosses (7))
2094 G.need_abi_warning = 1;
2095 break;
2097 case TYPEOF_TYPE:
2098 sorry ("mangling typeof, use decltype instead");
2099 break;
2101 case UNDERLYING_TYPE:
2102 sorry ("mangling __underlying_type");
2103 break;
2105 case LANG_TYPE:
2106 /* fall through. */
2108 default:
2109 gcc_unreachable ();
2114 /* Types other than builtin types are substitution candidates. */
2115 if (!is_builtin_type)
2116 add_substitution (type);
2119 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2120 CV-qualifiers written for TYPE.
2122 <CV-qualifiers> ::= [r] [V] [K] */
2124 static int
2125 write_CV_qualifiers_for_type (const tree type)
2127 int num_qualifiers = 0;
2129 /* The order is specified by:
2131 "In cases where multiple order-insensitive qualifiers are
2132 present, they should be ordered 'K' (closest to the base type),
2133 'V', 'r', and 'U' (farthest from the base type) ..."
2135 Note that we do not use cp_type_quals below; given "const
2136 int[3]", the "const" is emitted with the "int", not with the
2137 array. */
2138 cp_cv_quals quals = TYPE_QUALS (type);
2140 if (quals & TYPE_QUAL_RESTRICT)
2142 write_char ('r');
2143 ++num_qualifiers;
2145 if (quals & TYPE_QUAL_VOLATILE)
2147 write_char ('V');
2148 ++num_qualifiers;
2150 if (quals & TYPE_QUAL_CONST)
2152 write_char ('K');
2153 ++num_qualifiers;
2156 return num_qualifiers;
2159 /* Non-terminal <builtin-type>.
2161 <builtin-type> ::= v # void
2162 ::= b # bool
2163 ::= w # wchar_t
2164 ::= c # char
2165 ::= a # signed char
2166 ::= h # unsigned char
2167 ::= s # short
2168 ::= t # unsigned short
2169 ::= i # int
2170 ::= j # unsigned int
2171 ::= l # long
2172 ::= m # unsigned long
2173 ::= x # long long, __int64
2174 ::= y # unsigned long long, __int64
2175 ::= n # __int128
2176 ::= o # unsigned __int128
2177 ::= f # float
2178 ::= d # double
2179 ::= e # long double, __float80
2180 ::= g # __float128 [not supported]
2181 ::= u <source-name> # vendor extended type */
2183 static void
2184 write_builtin_type (tree type)
2186 if (TYPE_CANONICAL (type))
2187 type = TYPE_CANONICAL (type);
2189 switch (TREE_CODE (type))
2191 case VOID_TYPE:
2192 write_char ('v');
2193 break;
2195 case BOOLEAN_TYPE:
2196 write_char ('b');
2197 break;
2199 case INTEGER_TYPE:
2200 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2201 isn't in integer_type_nodes. */
2202 if (type == wchar_type_node)
2203 write_char ('w');
2204 else if (type == char16_type_node)
2205 write_string ("Ds");
2206 else if (type == char32_type_node)
2207 write_string ("Di");
2208 else if (TYPE_FOR_JAVA (type))
2209 write_java_integer_type_codes (type);
2210 else
2212 size_t itk;
2213 /* Assume TYPE is one of the shared integer type nodes. Find
2214 it in the array of these nodes. */
2215 iagain:
2216 for (itk = 0; itk < itk_none; ++itk)
2217 if (integer_types[itk] != NULL_TREE
2218 && type == integer_types[itk])
2220 /* Print the corresponding single-letter code. */
2221 write_char (integer_type_codes[itk]);
2222 break;
2225 if (itk == itk_none)
2227 tree t = c_common_type_for_mode (TYPE_MODE (type),
2228 TYPE_UNSIGNED (type));
2229 if (type != t)
2231 type = t;
2232 goto iagain;
2235 if (TYPE_PRECISION (type) == 128)
2236 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2237 else
2239 /* Allow for cases where TYPE is not one of the shared
2240 integer type nodes and write a "vendor extended builtin
2241 type" with a name the form intN or uintN, respectively.
2242 Situations like this can happen if you have an
2243 __attribute__((__mode__(__SI__))) type and use exotic
2244 switches like '-mint8' on AVR. Of course, this is
2245 undefined by the C++ ABI (and '-mint8' is not even
2246 Standard C conforming), but when using such special
2247 options you're pretty much in nowhere land anyway. */
2248 const char *prefix;
2249 char prec[11]; /* up to ten digits for an unsigned */
2251 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2252 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2253 write_char ('u'); /* "vendor extended builtin type" */
2254 write_unsigned_number (strlen (prefix) + strlen (prec));
2255 write_string (prefix);
2256 write_string (prec);
2260 break;
2262 case REAL_TYPE:
2263 if (type == float_type_node
2264 || type == java_float_type_node)
2265 write_char ('f');
2266 else if (type == double_type_node
2267 || type == java_double_type_node)
2268 write_char ('d');
2269 else if (type == long_double_type_node)
2270 write_char ('e');
2271 else if (type == dfloat32_type_node)
2272 write_string ("Df");
2273 else if (type == dfloat64_type_node)
2274 write_string ("Dd");
2275 else if (type == dfloat128_type_node)
2276 write_string ("De");
2277 else
2278 gcc_unreachable ();
2279 break;
2281 case FIXED_POINT_TYPE:
2282 write_string ("DF");
2283 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2284 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2285 if (type == fract_type_node
2286 || type == sat_fract_type_node
2287 || type == accum_type_node
2288 || type == sat_accum_type_node)
2289 write_char ('i');
2290 else if (type == unsigned_fract_type_node
2291 || type == sat_unsigned_fract_type_node
2292 || type == unsigned_accum_type_node
2293 || type == sat_unsigned_accum_type_node)
2294 write_char ('j');
2295 else if (type == short_fract_type_node
2296 || type == sat_short_fract_type_node
2297 || type == short_accum_type_node
2298 || type == sat_short_accum_type_node)
2299 write_char ('s');
2300 else if (type == unsigned_short_fract_type_node
2301 || type == sat_unsigned_short_fract_type_node
2302 || type == unsigned_short_accum_type_node
2303 || type == sat_unsigned_short_accum_type_node)
2304 write_char ('t');
2305 else if (type == long_fract_type_node
2306 || type == sat_long_fract_type_node
2307 || type == long_accum_type_node
2308 || type == sat_long_accum_type_node)
2309 write_char ('l');
2310 else if (type == unsigned_long_fract_type_node
2311 || type == sat_unsigned_long_fract_type_node
2312 || type == unsigned_long_accum_type_node
2313 || type == sat_unsigned_long_accum_type_node)
2314 write_char ('m');
2315 else if (type == long_long_fract_type_node
2316 || type == sat_long_long_fract_type_node
2317 || type == long_long_accum_type_node
2318 || type == sat_long_long_accum_type_node)
2319 write_char ('x');
2320 else if (type == unsigned_long_long_fract_type_node
2321 || type == sat_unsigned_long_long_fract_type_node
2322 || type == unsigned_long_long_accum_type_node
2323 || type == sat_unsigned_long_long_accum_type_node)
2324 write_char ('y');
2325 else
2326 sorry ("mangling unknown fixed point type");
2327 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2328 if (TYPE_SATURATING (type))
2329 write_char ('s');
2330 else
2331 write_char ('n');
2332 break;
2334 default:
2335 gcc_unreachable ();
2339 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2340 METHOD_TYPE. The return type is mangled before the parameter
2341 types.
2343 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2345 static void
2346 write_function_type (const tree type)
2348 MANGLE_TRACE_TREE ("function-type", type);
2350 /* For a pointer to member function, the function type may have
2351 cv-qualifiers, indicating the quals for the artificial 'this'
2352 parameter. */
2353 if (TREE_CODE (type) == METHOD_TYPE)
2355 /* The first parameter must be a POINTER_TYPE pointing to the
2356 `this' parameter. */
2357 tree this_type = class_of_this_parm (type);
2358 write_CV_qualifiers_for_type (this_type);
2361 write_char ('F');
2362 /* We don't track whether or not a type is `extern "C"'. Note that
2363 you can have an `extern "C"' function that does not have
2364 `extern "C"' type, and vice versa:
2366 extern "C" typedef void function_t();
2367 function_t f; // f has C++ linkage, but its type is
2368 // `extern "C"'
2370 typedef void function_t();
2371 extern "C" function_t f; // Vice versa.
2373 See [dcl.link]. */
2374 write_bare_function_type (type, /*include_return_type_p=*/1,
2375 /*decl=*/NULL);
2376 if (FUNCTION_REF_QUALIFIED (type))
2378 if (FUNCTION_RVALUE_QUALIFIED (type))
2379 write_char ('O');
2380 else
2381 write_char ('R');
2383 write_char ('E');
2386 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2387 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2388 is mangled before the parameter types. If non-NULL, DECL is
2389 FUNCTION_DECL for the function whose type is being emitted.
2391 If DECL is a member of a Java type, then a literal 'J'
2392 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2393 were nonzero.
2395 <bare-function-type> ::= [J]</signature/ type>+ */
2397 static void
2398 write_bare_function_type (const tree type, const int include_return_type_p,
2399 const tree decl)
2401 int java_method_p;
2403 MANGLE_TRACE_TREE ("bare-function-type", type);
2405 /* Detect Java methods and emit special encoding. */
2406 if (decl != NULL
2407 && DECL_FUNCTION_MEMBER_P (decl)
2408 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2409 && !DECL_CONSTRUCTOR_P (decl)
2410 && !DECL_DESTRUCTOR_P (decl)
2411 && !DECL_CONV_FN_P (decl))
2413 java_method_p = 1;
2414 write_char ('J');
2416 else
2418 java_method_p = 0;
2421 /* Mangle the return type, if requested. */
2422 if (include_return_type_p || java_method_p)
2423 write_type (TREE_TYPE (type));
2425 /* Now mangle the types of the arguments. */
2426 ++G.parm_depth;
2427 write_method_parms (TYPE_ARG_TYPES (type),
2428 TREE_CODE (type) == METHOD_TYPE,
2429 decl);
2430 --G.parm_depth;
2433 /* Write the mangled representation of a method parameter list of
2434 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2435 considered a non-static method, and the this parameter is omitted.
2436 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2437 parameters are being emitted. */
2439 static void
2440 write_method_parms (tree parm_types, const int method_p, const tree decl)
2442 tree first_parm_type;
2443 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2445 /* Assume this parameter type list is variable-length. If it ends
2446 with a void type, then it's not. */
2447 int varargs_p = 1;
2449 /* If this is a member function, skip the first arg, which is the
2450 this pointer.
2451 "Member functions do not encode the type of their implicit this
2452 parameter."
2454 Similarly, there's no need to mangle artificial parameters, like
2455 the VTT parameters for constructors and destructors. */
2456 if (method_p)
2458 parm_types = TREE_CHAIN (parm_types);
2459 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2461 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2463 parm_types = TREE_CHAIN (parm_types);
2464 parm_decl = DECL_CHAIN (parm_decl);
2468 for (first_parm_type = parm_types;
2469 parm_types;
2470 parm_types = TREE_CHAIN (parm_types))
2472 tree parm = TREE_VALUE (parm_types);
2473 if (parm == void_type_node)
2475 /* "Empty parameter lists, whether declared as () or
2476 conventionally as (void), are encoded with a void parameter
2477 (v)." */
2478 if (parm_types == first_parm_type)
2479 write_type (parm);
2480 /* If the parm list is terminated with a void type, it's
2481 fixed-length. */
2482 varargs_p = 0;
2483 /* A void type better be the last one. */
2484 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2486 else
2487 write_type (parm);
2490 if (varargs_p)
2491 /* <builtin-type> ::= z # ellipsis */
2492 write_char ('z');
2495 /* <class-enum-type> ::= <name> */
2497 static void
2498 write_class_enum_type (const tree type)
2500 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2503 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2504 arguments.
2506 <template-args> ::= I <template-arg>* E */
2508 static void
2509 write_template_args (tree args)
2511 int i;
2512 int length = 0;
2514 MANGLE_TRACE_TREE ("template-args", args);
2516 write_char ('I');
2518 if (args)
2519 length = TREE_VEC_LENGTH (args);
2521 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2523 /* We have nested template args. We want the innermost template
2524 argument list. */
2525 args = TREE_VEC_ELT (args, length - 1);
2526 length = TREE_VEC_LENGTH (args);
2528 for (i = 0; i < length; ++i)
2529 write_template_arg (TREE_VEC_ELT (args, i));
2531 write_char ('E');
2534 /* Write out the
2535 <unqualified-name>
2536 <unqualified-name> <template-args>
2537 part of SCOPE_REF or COMPONENT_REF mangling. */
2539 static void
2540 write_member_name (tree member)
2542 if (identifier_p (member))
2543 write_unqualified_id (member);
2544 else if (DECL_P (member))
2545 write_unqualified_name (member);
2546 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2548 tree name = TREE_OPERAND (member, 0);
2549 if (TREE_CODE (name) == OVERLOAD)
2550 name = OVL_FUNCTION (name);
2551 write_member_name (name);
2552 write_template_args (TREE_OPERAND (member, 1));
2554 else
2555 write_expression (member);
2558 /* <expression> ::= <unary operator-name> <expression>
2559 ::= <binary operator-name> <expression> <expression>
2560 ::= <expr-primary>
2562 <expr-primary> ::= <template-param>
2563 ::= L <type> <value number> E # literal
2564 ::= L <mangled-name> E # external name
2565 ::= st <type> # sizeof
2566 ::= sr <type> <unqualified-name> # dependent name
2567 ::= sr <type> <unqualified-name> <template-args> */
2569 static void
2570 write_expression (tree expr)
2572 enum tree_code code = TREE_CODE (expr);
2574 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2575 is converted (via qualification conversions) to another
2576 type. */
2577 while (TREE_CODE (expr) == NOP_EXPR
2578 /* Parentheses aren't mangled. */
2579 || code == PAREN_EXPR
2580 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2582 expr = TREE_OPERAND (expr, 0);
2583 code = TREE_CODE (expr);
2586 if (code == BASELINK
2587 && (!type_unknown_p (expr)
2588 || !BASELINK_QUALIFIED_P (expr)))
2590 expr = BASELINK_FUNCTIONS (expr);
2591 code = TREE_CODE (expr);
2594 /* Handle pointers-to-members by making them look like expression
2595 nodes. */
2596 if (code == PTRMEM_CST)
2598 expr = build_nt (ADDR_EXPR,
2599 build_qualified_name (/*type=*/NULL_TREE,
2600 PTRMEM_CST_CLASS (expr),
2601 PTRMEM_CST_MEMBER (expr),
2602 /*template_p=*/false));
2603 code = TREE_CODE (expr);
2606 /* Handle template parameters. */
2607 if (code == TEMPLATE_TYPE_PARM
2608 || code == TEMPLATE_TEMPLATE_PARM
2609 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2610 || code == TEMPLATE_PARM_INDEX)
2611 write_template_param (expr);
2612 /* Handle literals. */
2613 else if (TREE_CODE_CLASS (code) == tcc_constant
2614 || code == CONST_DECL)
2615 write_template_arg_literal (expr);
2616 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2618 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2619 write_string ("fpT");
2621 else if (code == PARM_DECL)
2623 /* A function parameter used in a late-specified return type. */
2624 int index = DECL_PARM_INDEX (expr);
2625 int level = DECL_PARM_LEVEL (expr);
2626 int delta = G.parm_depth - level + 1;
2627 gcc_assert (index >= 1);
2628 write_char ('f');
2629 if (delta != 0)
2631 if (abi_version_at_least (5))
2633 /* Let L be the number of function prototype scopes from the
2634 innermost one (in which the parameter reference occurs) up
2635 to (and including) the one containing the declaration of
2636 the referenced parameter. If the parameter declaration
2637 clause of the innermost function prototype scope has been
2638 completely seen, it is not counted (in that case -- which
2639 is perhaps the most common -- L can be zero). */
2640 write_char ('L');
2641 write_unsigned_number (delta - 1);
2643 if (abi_version_crosses (5))
2644 G.need_abi_warning = true;
2646 write_char ('p');
2647 write_compact_number (index - 1);
2649 else if (DECL_P (expr))
2651 write_char ('L');
2652 write_mangled_name (expr, false);
2653 write_char ('E');
2655 else if (TREE_CODE (expr) == SIZEOF_EXPR
2656 && SIZEOF_EXPR_TYPE_P (expr))
2658 write_string ("st");
2659 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2661 else if (TREE_CODE (expr) == SIZEOF_EXPR
2662 && TYPE_P (TREE_OPERAND (expr, 0)))
2664 write_string ("st");
2665 write_type (TREE_OPERAND (expr, 0));
2667 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2668 && TYPE_P (TREE_OPERAND (expr, 0)))
2670 write_string ("at");
2671 write_type (TREE_OPERAND (expr, 0));
2673 else if (code == SCOPE_REF
2674 || code == BASELINK)
2676 tree scope, member;
2677 if (code == SCOPE_REF)
2679 scope = TREE_OPERAND (expr, 0);
2680 member = TREE_OPERAND (expr, 1);
2682 else
2684 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2685 member = BASELINK_FUNCTIONS (expr);
2688 /* If the MEMBER is a real declaration, then the qualifying
2689 scope was not dependent. Ideally, we would not have a
2690 SCOPE_REF in those cases, but sometimes we do. If the second
2691 argument is a DECL, then the name must not have been
2692 dependent. */
2693 if (DECL_P (member))
2694 write_expression (member);
2695 else
2697 write_string ("sr");
2698 write_type (scope);
2699 write_member_name (member);
2702 else if (INDIRECT_REF_P (expr)
2703 && TREE_TYPE (TREE_OPERAND (expr, 0))
2704 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2706 write_expression (TREE_OPERAND (expr, 0));
2708 else if (identifier_p (expr))
2710 /* An operator name appearing as a dependent name needs to be
2711 specially marked to disambiguate between a use of the operator
2712 name and a use of the operator in an expression. */
2713 if (IDENTIFIER_OPNAME_P (expr))
2714 write_string ("on");
2715 write_unqualified_id (expr);
2717 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2719 tree fn = TREE_OPERAND (expr, 0);
2720 if (is_overloaded_fn (fn))
2721 fn = DECL_NAME (get_first_fn (fn));
2722 if (IDENTIFIER_OPNAME_P (fn))
2723 write_string ("on");
2724 write_unqualified_id (fn);
2725 write_template_args (TREE_OPERAND (expr, 1));
2727 else if (TREE_CODE (expr) == MODOP_EXPR)
2729 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2730 const char *name = (assignment_operator_name_info[(int) subop]
2731 .mangled_name);
2732 write_string (name);
2733 write_expression (TREE_OPERAND (expr, 0));
2734 write_expression (TREE_OPERAND (expr, 2));
2736 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2738 /* ::= [gs] nw <expression>* _ <type> E
2739 ::= [gs] nw <expression>* _ <type> <initializer>
2740 ::= [gs] na <expression>* _ <type> E
2741 ::= [gs] na <expression>* _ <type> <initializer>
2742 <initializer> ::= pi <expression>* E */
2743 tree placement = TREE_OPERAND (expr, 0);
2744 tree type = TREE_OPERAND (expr, 1);
2745 tree nelts = TREE_OPERAND (expr, 2);
2746 tree init = TREE_OPERAND (expr, 3);
2747 tree t;
2749 gcc_assert (code == NEW_EXPR);
2750 if (TREE_OPERAND (expr, 2))
2751 code = VEC_NEW_EXPR;
2753 if (NEW_EXPR_USE_GLOBAL (expr))
2754 write_string ("gs");
2756 write_string (operator_name_info[(int) code].mangled_name);
2758 for (t = placement; t; t = TREE_CHAIN (t))
2759 write_expression (TREE_VALUE (t));
2761 write_char ('_');
2763 if (nelts)
2765 tree domain;
2766 ++processing_template_decl;
2767 domain = compute_array_index_type (NULL_TREE, nelts,
2768 tf_warning_or_error);
2769 type = build_cplus_array_type (type, domain);
2770 --processing_template_decl;
2772 write_type (type);
2774 if (init && TREE_CODE (init) == TREE_LIST
2775 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2776 write_expression (TREE_VALUE (init));
2777 else
2779 if (init)
2780 write_string ("pi");
2781 if (init && init != void_node)
2782 for (t = init; t; t = TREE_CHAIN (t))
2783 write_expression (TREE_VALUE (t));
2784 write_char ('E');
2787 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2789 gcc_assert (code == DELETE_EXPR);
2790 if (DELETE_EXPR_USE_VEC (expr))
2791 code = VEC_DELETE_EXPR;
2793 if (DELETE_EXPR_USE_GLOBAL (expr))
2794 write_string ("gs");
2796 write_string (operator_name_info[(int) code].mangled_name);
2798 write_expression (TREE_OPERAND (expr, 0));
2800 else if (code == THROW_EXPR)
2802 tree op = TREE_OPERAND (expr, 0);
2803 if (op)
2805 write_string ("tw");
2806 write_expression (op);
2808 else
2809 write_string ("tr");
2811 else if (code == CONSTRUCTOR)
2813 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2814 unsigned i; tree val;
2816 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2817 write_string ("il");
2818 else
2820 write_string ("tl");
2821 write_type (TREE_TYPE (expr));
2823 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2824 write_expression (val);
2825 write_char ('E');
2827 else if (dependent_name (expr))
2829 write_unqualified_id (dependent_name (expr));
2831 else
2833 int i, len;
2834 const char *name;
2836 /* When we bind a variable or function to a non-type template
2837 argument with reference type, we create an ADDR_EXPR to show
2838 the fact that the entity's address has been taken. But, we
2839 don't actually want to output a mangling code for the `&'. */
2840 if (TREE_CODE (expr) == ADDR_EXPR
2841 && TREE_TYPE (expr)
2842 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2844 expr = TREE_OPERAND (expr, 0);
2845 if (DECL_P (expr))
2847 write_expression (expr);
2848 return;
2851 code = TREE_CODE (expr);
2854 if (code == COMPONENT_REF)
2856 tree ob = TREE_OPERAND (expr, 0);
2858 if (TREE_CODE (ob) == ARROW_EXPR)
2860 write_string (operator_name_info[(int)code].mangled_name);
2861 ob = TREE_OPERAND (ob, 0);
2863 else
2864 write_string ("dt");
2866 write_expression (ob);
2867 write_member_name (TREE_OPERAND (expr, 1));
2868 return;
2871 /* If it wasn't any of those, recursively expand the expression. */
2872 name = operator_name_info[(int) code].mangled_name;
2874 /* We used to mangle const_cast and static_cast like a C cast. */
2875 if (code == CONST_CAST_EXPR
2876 || code == STATIC_CAST_EXPR)
2878 if (abi_version_crosses (6))
2879 G.need_abi_warning = 1;
2880 if (!abi_version_at_least (6))
2881 name = operator_name_info[CAST_EXPR].mangled_name;
2884 if (name == NULL)
2886 switch (code)
2888 case TRAIT_EXPR:
2889 error ("use of built-in trait %qE in function signature; "
2890 "use library traits instead", expr);
2891 break;
2893 default:
2894 sorry ("mangling %C", code);
2895 break;
2897 return;
2899 else
2900 write_string (name);
2902 switch (code)
2904 case CALL_EXPR:
2906 tree fn = CALL_EXPR_FN (expr);
2908 if (TREE_CODE (fn) == ADDR_EXPR)
2909 fn = TREE_OPERAND (fn, 0);
2911 /* Mangle a dependent name as the name, not whatever happens to
2912 be the first function in the overload set. */
2913 if ((TREE_CODE (fn) == FUNCTION_DECL
2914 || TREE_CODE (fn) == OVERLOAD)
2915 && type_dependent_expression_p_push (expr))
2916 fn = DECL_NAME (get_first_fn (fn));
2918 write_expression (fn);
2921 for (i = 0; i < call_expr_nargs (expr); ++i)
2922 write_expression (CALL_EXPR_ARG (expr, i));
2923 write_char ('E');
2924 break;
2926 case CAST_EXPR:
2927 write_type (TREE_TYPE (expr));
2928 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2929 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2930 else
2932 tree args = TREE_OPERAND (expr, 0);
2933 write_char ('_');
2934 for (; args; args = TREE_CHAIN (args))
2935 write_expression (TREE_VALUE (args));
2936 write_char ('E');
2938 break;
2940 case DYNAMIC_CAST_EXPR:
2941 case REINTERPRET_CAST_EXPR:
2942 case STATIC_CAST_EXPR:
2943 case CONST_CAST_EXPR:
2944 write_type (TREE_TYPE (expr));
2945 write_expression (TREE_OPERAND (expr, 0));
2946 break;
2948 case PREINCREMENT_EXPR:
2949 case PREDECREMENT_EXPR:
2950 if (abi_version_at_least (6))
2951 write_char ('_');
2952 if (abi_version_crosses (6))
2953 G.need_abi_warning = 1;
2954 /* Fall through. */
2956 default:
2957 /* In the middle-end, some expressions have more operands than
2958 they do in templates (and mangling). */
2959 len = cp_tree_operand_length (expr);
2961 for (i = 0; i < len; ++i)
2963 tree operand = TREE_OPERAND (expr, i);
2964 /* As a GNU extension, the middle operand of a
2965 conditional may be omitted. Since expression
2966 manglings are supposed to represent the input token
2967 stream, there's no good way to mangle such an
2968 expression without extending the C++ ABI. */
2969 if (code == COND_EXPR && i == 1 && !operand)
2971 error ("omitted middle operand to %<?:%> operand "
2972 "cannot be mangled");
2973 continue;
2975 write_expression (operand);
2981 /* Literal subcase of non-terminal <template-arg>.
2983 "Literal arguments, e.g. "A<42L>", are encoded with their type
2984 and value. Negative integer values are preceded with "n"; for
2985 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2986 encoded as 0, true as 1." */
2988 static void
2989 write_template_arg_literal (const tree value)
2991 write_char ('L');
2992 write_type (TREE_TYPE (value));
2994 /* Write a null member pointer value as (type)0, regardless of its
2995 real representation. */
2996 if (null_member_pointer_value_p (value))
2997 write_integer_cst (integer_zero_node);
2998 else
2999 switch (TREE_CODE (value))
3001 case CONST_DECL:
3002 write_integer_cst (DECL_INITIAL (value));
3003 break;
3005 case INTEGER_CST:
3006 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3007 || integer_zerop (value) || integer_onep (value));
3008 write_integer_cst (value);
3009 break;
3011 case REAL_CST:
3012 write_real_cst (value);
3013 break;
3015 case COMPLEX_CST:
3016 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3017 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3019 write_integer_cst (TREE_REALPART (value));
3020 write_char ('_');
3021 write_integer_cst (TREE_IMAGPART (value));
3023 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3024 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3026 write_real_cst (TREE_REALPART (value));
3027 write_char ('_');
3028 write_real_cst (TREE_IMAGPART (value));
3030 else
3031 gcc_unreachable ();
3032 break;
3034 case STRING_CST:
3035 sorry ("string literal in function template signature");
3036 break;
3038 default:
3039 gcc_unreachable ();
3042 write_char ('E');
3045 /* Non-terminal <template-arg>.
3047 <template-arg> ::= <type> # type
3048 ::= L <type> </value/ number> E # literal
3049 ::= LZ <name> E # external name
3050 ::= X <expression> E # expression */
3052 static void
3053 write_template_arg (tree node)
3055 enum tree_code code = TREE_CODE (node);
3057 MANGLE_TRACE_TREE ("template-arg", node);
3059 /* A template template parameter's argument list contains TREE_LIST
3060 nodes of which the value field is the actual argument. */
3061 if (code == TREE_LIST)
3063 node = TREE_VALUE (node);
3064 /* If it's a decl, deal with its type instead. */
3065 if (DECL_P (node))
3067 node = TREE_TYPE (node);
3068 code = TREE_CODE (node);
3072 if (TREE_CODE (node) == NOP_EXPR
3073 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3075 /* Template parameters can be of reference type. To maintain
3076 internal consistency, such arguments use a conversion from
3077 address of object to reference type. */
3078 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3079 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3082 if (TREE_CODE (node) == BASELINK
3083 && !type_unknown_p (node))
3085 if (abi_version_at_least (6))
3086 node = BASELINK_FUNCTIONS (node);
3087 if (abi_version_crosses (6))
3088 /* We wrongly wrapped a class-scope function in X/E. */
3089 G.need_abi_warning = 1;
3092 if (ARGUMENT_PACK_P (node))
3094 /* Expand the template argument pack. */
3095 tree args = ARGUMENT_PACK_ARGS (node);
3096 int i, length = TREE_VEC_LENGTH (args);
3097 if (abi_version_at_least (6))
3098 write_char ('J');
3099 else
3100 write_char ('I');
3101 if (abi_version_crosses (6))
3102 G.need_abi_warning = 1;
3103 for (i = 0; i < length; ++i)
3104 write_template_arg (TREE_VEC_ELT (args, i));
3105 write_char ('E');
3107 else if (TYPE_P (node))
3108 write_type (node);
3109 else if (code == TEMPLATE_DECL)
3110 /* A template appearing as a template arg is a template template arg. */
3111 write_template_template_arg (node);
3112 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3113 || code == CONST_DECL
3114 || null_member_pointer_value_p (node))
3115 write_template_arg_literal (node);
3116 else if (DECL_P (node))
3118 write_char ('L');
3119 /* Until ABI version 3, the underscore before the mangled name
3120 was incorrectly omitted. */
3121 if (!abi_version_at_least (3))
3122 write_char ('Z');
3123 else
3124 write_string ("_Z");
3125 if (abi_version_crosses (3))
3126 G.need_abi_warning = 1;
3127 write_encoding (node);
3128 write_char ('E');
3130 else
3132 /* Template arguments may be expressions. */
3133 write_char ('X');
3134 write_expression (node);
3135 write_char ('E');
3139 /* <template-template-arg>
3140 ::= <name>
3141 ::= <substitution> */
3143 static void
3144 write_template_template_arg (const tree decl)
3146 MANGLE_TRACE_TREE ("template-template-arg", decl);
3148 if (find_substitution (decl))
3149 return;
3150 write_name (decl, /*ignore_local_scope=*/0);
3151 add_substitution (decl);
3155 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3157 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3158 ::= A <expression> _ </element/ type>
3160 "Array types encode the dimension (number of elements) and the
3161 element type. For variable length arrays, the dimension (but not
3162 the '_' separator) is omitted." */
3164 static void
3165 write_array_type (const tree type)
3167 write_char ('A');
3168 if (TYPE_DOMAIN (type))
3170 tree index_type;
3171 tree max;
3173 index_type = TYPE_DOMAIN (type);
3174 /* The INDEX_TYPE gives the upper and lower bounds of the
3175 array. */
3176 max = TYPE_MAX_VALUE (index_type);
3177 if (TREE_CODE (max) == INTEGER_CST)
3179 /* The ABI specifies that we should mangle the number of
3180 elements in the array, not the largest allowed index. */
3181 offset_int wmax = wi::to_offset (max) + 1;
3182 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3183 number of elements as zero. */
3184 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3185 gcc_assert (wi::fits_uhwi_p (wmax));
3186 write_unsigned_number (wmax.to_uhwi ());
3188 else
3190 max = TREE_OPERAND (max, 0);
3191 write_expression (max);
3195 write_char ('_');
3196 write_type (TREE_TYPE (type));
3199 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3200 variables. TYPE is a pointer-to-member POINTER_TYPE.
3202 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3204 static void
3205 write_pointer_to_member_type (const tree type)
3207 write_char ('M');
3208 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3209 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3212 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3213 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3214 TEMPLATE_PARM_INDEX.
3216 <template-param> ::= T </parameter/ number> _ */
3218 static void
3219 write_template_param (const tree parm)
3221 int parm_index;
3223 MANGLE_TRACE_TREE ("template-parm", parm);
3225 switch (TREE_CODE (parm))
3227 case TEMPLATE_TYPE_PARM:
3228 case TEMPLATE_TEMPLATE_PARM:
3229 case BOUND_TEMPLATE_TEMPLATE_PARM:
3230 parm_index = TEMPLATE_TYPE_IDX (parm);
3231 break;
3233 case TEMPLATE_PARM_INDEX:
3234 parm_index = TEMPLATE_PARM_IDX (parm);
3235 break;
3237 default:
3238 gcc_unreachable ();
3241 write_char ('T');
3242 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3243 earliest template param denoted by `_'. */
3244 write_compact_number (parm_index);
3247 /* <template-template-param>
3248 ::= <template-param>
3249 ::= <substitution> */
3251 static void
3252 write_template_template_param (const tree parm)
3254 tree templ = NULL_TREE;
3256 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3257 template template parameter. The substitution candidate here is
3258 only the template. */
3259 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3261 templ
3262 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3263 if (find_substitution (templ))
3264 return;
3267 /* <template-param> encodes only the template parameter position,
3268 not its template arguments, which is fine here. */
3269 write_template_param (parm);
3270 if (templ)
3271 add_substitution (templ);
3274 /* Non-terminal <substitution>.
3276 <substitution> ::= S <seq-id> _
3277 ::= S_ */
3279 static void
3280 write_substitution (const int seq_id)
3282 MANGLE_TRACE ("substitution", "");
3284 write_char ('S');
3285 if (seq_id > 0)
3286 write_number (seq_id - 1, /*unsigned=*/1, 36);
3287 write_char ('_');
3290 /* Start mangling ENTITY. */
3292 static inline void
3293 start_mangling (const tree entity)
3295 G.entity = entity;
3296 G.need_abi_warning = false;
3297 obstack_free (&name_obstack, name_base);
3298 mangle_obstack = &name_obstack;
3299 name_base = obstack_alloc (&name_obstack, 0);
3302 /* Done with mangling. If WARN is true, and the name of G.entity will
3303 be mangled differently in a future version of the ABI, issue a
3304 warning. */
3306 static void
3307 finish_mangling_internal (void)
3309 /* Clear all the substitutions. */
3310 vec_safe_truncate (G.substitutions, 0);
3312 /* Null-terminate the string. */
3313 write_char ('\0');
3317 /* Like finish_mangling_internal, but return the mangled string. */
3319 static inline const char *
3320 finish_mangling (void)
3322 finish_mangling_internal ();
3323 return (const char *) obstack_finish (mangle_obstack);
3326 /* Like finish_mangling_internal, but return an identifier. */
3328 static tree
3329 finish_mangling_get_identifier (void)
3331 finish_mangling_internal ();
3332 /* Don't obstack_finish here, and the next start_mangling will
3333 remove the identifier. */
3334 return get_identifier ((const char *) obstack_base (mangle_obstack));
3337 /* Initialize data structures for mangling. */
3339 void
3340 init_mangle (void)
3342 gcc_obstack_init (&name_obstack);
3343 name_base = obstack_alloc (&name_obstack, 0);
3344 vec_alloc (G.substitutions, 0);
3346 /* Cache these identifiers for quick comparison when checking for
3347 standard substitutions. */
3348 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3349 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3350 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3351 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3352 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3353 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3356 /* Generate the mangled name of DECL. */
3358 static tree
3359 mangle_decl_string (const tree decl)
3361 tree result;
3362 location_t saved_loc = input_location;
3363 tree saved_fn = NULL_TREE;
3364 bool template_p = false;
3366 /* We shouldn't be trying to mangle an uninstantiated template. */
3367 gcc_assert (!type_dependent_expression_p (decl));
3369 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3371 struct tinst_level *tl = current_instantiation ();
3372 if ((!tl || tl->decl != decl)
3373 && push_tinst_level (decl))
3375 template_p = true;
3376 saved_fn = current_function_decl;
3377 current_function_decl = NULL_TREE;
3380 input_location = DECL_SOURCE_LOCATION (decl);
3382 start_mangling (decl);
3384 if (TREE_CODE (decl) == TYPE_DECL)
3385 write_type (TREE_TYPE (decl));
3386 else
3387 write_mangled_name (decl, true);
3389 result = finish_mangling_get_identifier ();
3390 if (DEBUG_MANGLE)
3391 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3392 IDENTIFIER_POINTER (result));
3394 if (template_p)
3396 pop_tinst_level ();
3397 current_function_decl = saved_fn;
3399 input_location = saved_loc;
3401 return result;
3404 /* Return an identifier for the external mangled name of DECL. */
3406 static tree
3407 get_mangled_id (tree decl)
3409 tree id = mangle_decl_string (decl);
3410 return targetm.mangle_decl_assembler_name (decl, id);
3413 /* Create an identifier for the external mangled name of DECL. */
3415 void
3416 mangle_decl (const tree decl)
3418 tree id;
3419 bool dep;
3421 /* Don't bother mangling uninstantiated templates. */
3422 ++processing_template_decl;
3423 if (TREE_CODE (decl) == TYPE_DECL)
3424 dep = dependent_type_p (TREE_TYPE (decl));
3425 else
3426 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3427 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3428 --processing_template_decl;
3429 if (dep)
3430 return;
3432 id = get_mangled_id (decl);
3433 SET_DECL_ASSEMBLER_NAME (decl, id);
3435 if (G.need_abi_warning
3436 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3437 && TREE_CODE (decl) != TYPE_DECL
3438 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3439 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3441 /* If the mangling will change in the future, emit an alias with the
3442 future mangled name for forward-compatibility. */
3443 int save_ver;
3444 tree id2;
3446 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3447 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3448 inform (DECL_SOURCE_LOCATION (decl), "a later -fabi-version= (or =0) "
3449 "avoids this error with a change in mangling");
3451 save_ver = flag_abi_version;
3452 flag_abi_version = flag_abi_compat_version;
3453 id2 = mangle_decl_string (decl);
3454 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3455 flag_abi_version = save_ver;
3457 if (id2 == id)
3458 return;
3460 if (warn_abi)
3462 if (flag_abi_compat_version != 0
3463 && abi_version_at_least (flag_abi_compat_version))
3464 warning (OPT_Wabi, "the mangled name of %q+D changed between "
3465 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3466 G.entity, flag_abi_compat_version, id2,
3467 flag_abi_version, id);
3468 else
3469 warning (OPT_Wabi, "the mangled name of %q+D changes between "
3470 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3471 G.entity, flag_abi_version, id,
3472 flag_abi_compat_version, id2);
3475 #ifdef ASM_OUTPUT_DEF
3476 if (flag_abi_compat_version != 0
3477 && IDENTIFIER_GLOBAL_VALUE (id2))
3478 return;
3480 tree alias = make_alias_for (decl, id2);
3481 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
3482 DECL_IGNORED_P (alias) = 1;
3483 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3484 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3485 if (vague_linkage_p (decl))
3486 DECL_WEAK (alias) = 1;
3487 if (TREE_CODE (decl) == FUNCTION_DECL)
3489 /* Don't create an alias to an unreferenced function. */
3490 if (struct cgraph_node *n = cgraph_node::get (decl))
3491 n->create_same_body_alias (alias, decl);
3493 else
3494 varpool_node::create_extra_name_alias (alias, decl);
3495 #endif
3499 /* Generate the mangled representation of TYPE. */
3501 const char *
3502 mangle_type_string (const tree type)
3504 const char *result;
3506 start_mangling (type);
3507 write_type (type);
3508 result = finish_mangling ();
3509 if (DEBUG_MANGLE)
3510 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3511 return result;
3514 /* Create an identifier for the mangled name of a special component
3515 for belonging to TYPE. CODE is the ABI-specified code for this
3516 component. */
3518 static tree
3519 mangle_special_for_type (const tree type, const char *code)
3521 tree result;
3523 /* We don't have an actual decl here for the special component, so
3524 we can't just process the <encoded-name>. Instead, fake it. */
3525 start_mangling (type);
3527 /* Start the mangling. */
3528 write_string ("_Z");
3529 write_string (code);
3531 /* Add the type. */
3532 write_type (type);
3533 result = finish_mangling_get_identifier ();
3535 if (DEBUG_MANGLE)
3536 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3537 IDENTIFIER_POINTER (result));
3539 return result;
3542 /* Create an identifier for the mangled representation of the typeinfo
3543 structure for TYPE. */
3545 tree
3546 mangle_typeinfo_for_type (const tree type)
3548 return mangle_special_for_type (type, "TI");
3551 /* Create an identifier for the mangled name of the NTBS containing
3552 the mangled name of TYPE. */
3554 tree
3555 mangle_typeinfo_string_for_type (const tree type)
3557 return mangle_special_for_type (type, "TS");
3560 /* Create an identifier for the mangled name of the vtable for TYPE. */
3562 tree
3563 mangle_vtbl_for_type (const tree type)
3565 return mangle_special_for_type (type, "TV");
3568 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3570 tree
3571 mangle_vtt_for_type (const tree type)
3573 return mangle_special_for_type (type, "TT");
3576 /* Return an identifier for a construction vtable group. TYPE is
3577 the most derived class in the hierarchy; BINFO is the base
3578 subobject for which this construction vtable group will be used.
3580 This mangling isn't part of the ABI specification; in the ABI
3581 specification, the vtable group is dumped in the same COMDAT as the
3582 main vtable, and is referenced only from that vtable, so it doesn't
3583 need an external name. For binary formats without COMDAT sections,
3584 though, we need external names for the vtable groups.
3586 We use the production
3588 <special-name> ::= CT <type> <offset number> _ <base type> */
3590 tree
3591 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3593 tree result;
3595 start_mangling (type);
3597 write_string ("_Z");
3598 write_string ("TC");
3599 write_type (type);
3600 write_integer_cst (BINFO_OFFSET (binfo));
3601 write_char ('_');
3602 write_type (BINFO_TYPE (binfo));
3604 result = finish_mangling_get_identifier ();
3605 if (DEBUG_MANGLE)
3606 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3607 IDENTIFIER_POINTER (result));
3608 return result;
3611 /* Mangle a this pointer or result pointer adjustment.
3613 <call-offset> ::= h <fixed offset number> _
3614 ::= v <fixed offset number> _ <virtual offset number> _ */
3616 static void
3617 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3619 write_char (virtual_offset ? 'v' : 'h');
3621 /* For either flavor, write the fixed offset. */
3622 write_integer_cst (fixed_offset);
3623 write_char ('_');
3625 /* For a virtual thunk, add the virtual offset. */
3626 if (virtual_offset)
3628 write_integer_cst (virtual_offset);
3629 write_char ('_');
3633 /* Return an identifier for the mangled name of a this-adjusting or
3634 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3635 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3636 is a virtual thunk, and it is the vtbl offset in
3637 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3638 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3639 thunk itself. A covariant thunk name always includes the adjustment
3640 for the this pointer, even if there is none.
3642 <special-name> ::= T <call-offset> <base encoding>
3643 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3644 <base encoding> */
3646 tree
3647 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3648 tree virtual_offset)
3650 tree result;
3652 start_mangling (fn_decl);
3654 write_string ("_Z");
3655 write_char ('T');
3657 if (!this_adjusting)
3659 /* Covariant thunk with no this adjustment */
3660 write_char ('c');
3661 mangle_call_offset (integer_zero_node, NULL_TREE);
3662 mangle_call_offset (fixed_offset, virtual_offset);
3664 else if (!DECL_THUNK_P (fn_decl))
3665 /* Plain this adjusting thunk. */
3666 mangle_call_offset (fixed_offset, virtual_offset);
3667 else
3669 /* This adjusting thunk to covariant thunk. */
3670 write_char ('c');
3671 mangle_call_offset (fixed_offset, virtual_offset);
3672 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3673 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3674 if (virtual_offset)
3675 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3676 mangle_call_offset (fixed_offset, virtual_offset);
3677 fn_decl = THUNK_TARGET (fn_decl);
3680 /* Scoped name. */
3681 write_encoding (fn_decl);
3683 result = finish_mangling_get_identifier ();
3684 if (DEBUG_MANGLE)
3685 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3686 return result;
3689 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3690 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3691 TYPE. */
3693 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
3695 /* Hash a node (VAL1) in the table. */
3697 static hashval_t
3698 hash_type (const void *val)
3700 return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
3703 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3705 static int
3706 compare_type (const void *val1, const void *val2)
3708 return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
3711 /* Return an identifier for the mangled unqualified name for a
3712 conversion operator to TYPE. This mangling is not specified by the
3713 ABI spec; it is only used internally. */
3715 tree
3716 mangle_conv_op_name_for_type (const tree type)
3718 void **slot;
3719 tree identifier;
3721 if (type == error_mark_node)
3722 return error_mark_node;
3724 if (conv_type_names == NULL)
3725 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
3727 slot = htab_find_slot_with_hash (conv_type_names, type,
3728 (hashval_t) TYPE_UID (type), INSERT);
3729 identifier = (tree)*slot;
3730 if (!identifier)
3732 char buffer[64];
3734 /* Create a unique name corresponding to TYPE. */
3735 sprintf (buffer, "operator %lu",
3736 (unsigned long) htab_elements (conv_type_names));
3737 identifier = get_identifier (buffer);
3738 *slot = identifier;
3740 /* Hang TYPE off the identifier so it can be found easily later
3741 when performing conversions. */
3742 TREE_TYPE (identifier) = type;
3744 /* Set bits on the identifier so we know later it's a conversion. */
3745 IDENTIFIER_OPNAME_P (identifier) = 1;
3746 IDENTIFIER_TYPENAME_P (identifier) = 1;
3749 return identifier;
3752 /* Write out the appropriate string for this variable when generating
3753 another mangled name based on this one. */
3755 static void
3756 write_guarded_var_name (const tree variable)
3758 if (DECL_NAME (variable)
3759 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3760 /* The name of a guard variable for a reference temporary should refer
3761 to the reference, not the temporary. */
3762 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3763 else
3764 write_name (variable, /*ignore_local_scope=*/0);
3767 /* Return an identifier for the name of an initialization guard
3768 variable for indicated VARIABLE. */
3770 tree
3771 mangle_guard_variable (const tree variable)
3773 start_mangling (variable);
3774 write_string ("_ZGV");
3775 write_guarded_var_name (variable);
3776 return finish_mangling_get_identifier ();
3779 /* Return an identifier for the name of a thread_local initialization
3780 function for VARIABLE. */
3782 tree
3783 mangle_tls_init_fn (const tree variable)
3785 start_mangling (variable);
3786 write_string ("_ZTH");
3787 write_guarded_var_name (variable);
3788 return finish_mangling_get_identifier ();
3791 /* Return an identifier for the name of a thread_local wrapper
3792 function for VARIABLE. */
3794 #define TLS_WRAPPER_PREFIX "_ZTW"
3796 tree
3797 mangle_tls_wrapper_fn (const tree variable)
3799 start_mangling (variable);
3800 write_string (TLS_WRAPPER_PREFIX);
3801 write_guarded_var_name (variable);
3802 return finish_mangling_get_identifier ();
3805 /* Return true iff FN is a thread_local wrapper function. */
3807 bool
3808 decl_tls_wrapper_p (const tree fn)
3810 if (TREE_CODE (fn) != FUNCTION_DECL)
3811 return false;
3812 tree name = DECL_NAME (fn);
3813 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3814 strlen (TLS_WRAPPER_PREFIX)) == 0;
3817 /* Return an identifier for the name of a temporary variable used to
3818 initialize a static reference. This isn't part of the ABI, but we might
3819 as well call them something readable. */
3821 static GTY(()) int temp_count;
3823 tree
3824 mangle_ref_init_variable (const tree variable)
3826 start_mangling (variable);
3827 write_string ("_ZGR");
3828 write_name (variable, /*ignore_local_scope=*/0);
3829 /* Avoid name clashes with aggregate initialization of multiple
3830 references at once. */
3831 write_unsigned_number (temp_count++);
3832 return finish_mangling_get_identifier ();
3836 /* Foreign language type mangling section. */
3838 /* How to write the type codes for the integer Java type. */
3840 static void
3841 write_java_integer_type_codes (const tree type)
3843 if (type == java_int_type_node)
3844 write_char ('i');
3845 else if (type == java_short_type_node)
3846 write_char ('s');
3847 else if (type == java_byte_type_node)
3848 write_char ('c');
3849 else if (type == java_char_type_node)
3850 write_char ('w');
3851 else if (type == java_long_type_node)
3852 write_char ('x');
3853 else if (type == java_boolean_type_node)
3854 write_char ('b');
3855 else
3856 gcc_unreachable ();
3859 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3860 function generates a mangled name for the vtable map variable of
3861 the class type. For example, if the class type is
3862 "std::bad_exception", the mangled name for the class is
3863 "St13bad_exception". This function would generate the name
3864 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3865 "_VTV<std::bad_exception>::__vtable_map". */
3868 char *
3869 get_mangled_vtable_map_var_name (tree class_type)
3871 char *var_name = NULL;
3872 const char *prefix = "_ZN4_VTVI";
3873 const char *postfix = "E12__vtable_mapE";
3875 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3877 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3878 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3879 strlen (prefix) +
3880 strlen (postfix) + 1;
3882 var_name = (char *) xmalloc (len);
3884 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3886 return var_name;
3889 #include "gt-cp-mangle.h"