2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / cp / mangle.c
blobda82dd6acda90ee07a93bdcd48a6b26b5edc8487
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file implements mangling of C++ names according to the IA64
22 C++ ABI specification. A mangled name encodes a function or
23 variable's name, scope, type, and/or template arguments into a text
24 identifier. This identifier is used as the function's or
25 variable's linkage name, to preserve compatibility between C++'s
26 language features (templates, scoping, and overloading) and C
27 linkers.
29 Additionally, g++ uses mangled names internally. To support this,
30 mangling of types is allowed, even though the mangled name of a
31 type should not appear by itself as an exported name. Ditto for
32 uninstantiated templates.
34 The primary entry point for this module is mangle_decl, which
35 returns an identifier containing the mangled name for a decl.
36 Additional entry points are provided to build mangled names of
37 particular constructs when the appropriate decl for that construct
38 is not available. These are:
40 mangle_typeinfo_for_type: typeinfo data
41 mangle_typeinfo_string_for_type: typeinfo type name
42 mangle_vtbl_for_type: virtual table data
43 mangle_vtt_for_type: VTT data
44 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
45 mangle_thunk: thunk function or entry */
47 #include "config.h"
48 #include "system.h"
49 #include "coretypes.h"
50 #include "tm.h"
51 #include "tree.h"
52 #include "stor-layout.h"
53 #include "stringpool.h"
54 #include "tm_p.h"
55 #include "cp-tree.h"
56 #include "obstack.h"
57 #include "flags.h"
58 #include "target.h"
59 #include "cgraph.h"
61 /* Debugging support. */
63 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
64 #ifndef DEBUG_MANGLE
65 #define DEBUG_MANGLE 0
66 #endif
68 /* Macros for tracing the write_* functions. */
69 #if DEBUG_MANGLE
70 # define MANGLE_TRACE(FN, INPUT) \
71 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
72 # define MANGLE_TRACE_TREE(FN, NODE) \
73 fprintf (stderr, " %-24s: %-24s (%p)\n", \
74 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
75 #else
76 # define MANGLE_TRACE(FN, INPUT)
77 # define MANGLE_TRACE_TREE(FN, NODE)
78 #endif
80 /* Nonzero if NODE is a class template-id. We can't rely on
81 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
82 that hard to distinguish A<T> from A, where A<T> is the type as
83 instantiated outside of the template, and A is the type used
84 without parameters inside the template. */
85 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
86 (TYPE_LANG_SPECIFIC (NODE) != NULL \
87 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
88 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
89 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
91 /* Things we only need one of. This module is not reentrant. */
92 typedef struct GTY(()) globals {
93 /* An array of the current substitution candidates, in the order
94 we've seen them. */
95 vec<tree, va_gc> *substitutions;
97 /* The entity that is being mangled. */
98 tree GTY ((skip)) entity;
100 /* How many parameter scopes we are inside. */
101 int parm_depth;
103 /* True if the mangling will be different in a future version of the
104 ABI. */
105 bool need_abi_warning;
106 } globals;
108 static GTY (()) globals G;
110 /* The obstack on which we build mangled names. */
111 static struct obstack *mangle_obstack;
113 /* The obstack on which we build mangled names that are not going to
114 be IDENTIFIER_NODEs. */
115 static struct obstack name_obstack;
117 /* The first object on the name_obstack; we use this to free memory
118 allocated on the name_obstack. */
119 static void *name_base;
121 /* Indices into subst_identifiers. These are identifiers used in
122 special substitution rules. */
123 typedef enum
125 SUBID_ALLOCATOR,
126 SUBID_BASIC_STRING,
127 SUBID_CHAR_TRAITS,
128 SUBID_BASIC_ISTREAM,
129 SUBID_BASIC_OSTREAM,
130 SUBID_BASIC_IOSTREAM,
131 SUBID_MAX
133 substitution_identifier_index_t;
135 /* For quick substitution checks, look up these common identifiers
136 once only. */
137 static GTY(()) tree subst_identifiers[SUBID_MAX];
139 /* Single-letter codes for builtin integer types, defined in
140 <builtin-type>. These are indexed by integer_type_kind values. */
141 static const char
142 integer_type_codes[itk_none] =
144 'c', /* itk_char */
145 'a', /* itk_signed_char */
146 'h', /* itk_unsigned_char */
147 's', /* itk_short */
148 't', /* itk_unsigned_short */
149 'i', /* itk_int */
150 'j', /* itk_unsigned_int */
151 'l', /* itk_long */
152 'm', /* itk_unsigned_long */
153 'x', /* itk_long_long */
154 'y', /* itk_unsigned_long_long */
155 'n', /* itk_int128 */
156 'o', /* itk_unsigned_int128 */
159 static int decl_is_template_id (const tree, tree* const);
161 /* Functions for handling substitutions. */
163 static inline tree canonicalize_for_substitution (tree);
164 static void add_substitution (tree);
165 static inline int is_std_substitution (const tree,
166 const substitution_identifier_index_t);
167 static inline int is_std_substitution_char (const tree,
168 const substitution_identifier_index_t);
169 static int find_substitution (tree);
170 static void mangle_call_offset (const tree, const tree);
172 /* Functions for emitting mangled representations of things. */
174 static void write_mangled_name (const tree, bool);
175 static void write_encoding (const tree);
176 static void write_name (tree, const int);
177 static void write_abi_tags (tree);
178 static void write_unscoped_name (const tree);
179 static void write_unscoped_template_name (const tree);
180 static void write_nested_name (const tree);
181 static void write_prefix (const tree);
182 static void write_template_prefix (const tree);
183 static void write_unqualified_name (tree);
184 static void write_conversion_operator_name (const tree);
185 static void write_source_name (tree);
186 static void write_literal_operator_name (tree);
187 static void write_unnamed_type_name (const tree);
188 static void write_closure_type_name (const tree);
189 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
190 const unsigned int);
191 static void write_number (unsigned HOST_WIDE_INT, const int,
192 const unsigned int);
193 static void write_compact_number (int num);
194 static void write_integer_cst (const tree);
195 static void write_real_cst (const tree);
196 static void write_identifier (const char *);
197 static void write_special_name_constructor (const tree);
198 static void write_special_name_destructor (const tree);
199 static void write_type (tree);
200 static int write_CV_qualifiers_for_type (const tree);
201 static void write_builtin_type (tree);
202 static void write_function_type (const tree);
203 static void write_bare_function_type (const tree, const int, const tree);
204 static void write_method_parms (tree, const int, const tree);
205 static void write_class_enum_type (const tree);
206 static void write_template_args (tree);
207 static void write_expression (tree);
208 static void write_template_arg_literal (const tree);
209 static void write_template_arg (tree);
210 static void write_template_template_arg (const tree);
211 static void write_array_type (const tree);
212 static void write_pointer_to_member_type (const tree);
213 static void write_template_param (const tree);
214 static void write_template_template_param (const tree);
215 static void write_substitution (const int);
216 static int discriminator_for_local_entity (tree);
217 static int discriminator_for_string_literal (tree, tree);
218 static void write_discriminator (const int);
219 static void write_local_name (tree, const tree, const tree);
220 static void dump_substitution_candidates (void);
221 static tree mangle_decl_string (const tree);
222 static int local_class_index (tree);
224 /* Control functions. */
226 static inline void start_mangling (const tree);
227 static inline const char *finish_mangling (const bool);
228 static tree mangle_special_for_type (const tree, const char *);
230 /* Foreign language functions. */
232 static void write_java_integer_type_codes (const tree);
234 /* Append a single character to the end of the mangled
235 representation. */
236 #define write_char(CHAR) \
237 obstack_1grow (mangle_obstack, (CHAR))
239 /* Append a sized buffer to the end of the mangled representation. */
240 #define write_chars(CHAR, LEN) \
241 obstack_grow (mangle_obstack, (CHAR), (LEN))
243 /* Append a NUL-terminated string to the end of the mangled
244 representation. */
245 #define write_string(STRING) \
246 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
248 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
249 same purpose (context, which may be a type) and value (template
250 decl). See write_template_prefix for more information on what this
251 is used for. */
252 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
253 (TREE_CODE (NODE1) == TREE_LIST \
254 && TREE_CODE (NODE2) == TREE_LIST \
255 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
256 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
257 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
258 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
260 /* Write out an unsigned quantity in base 10. */
261 #define write_unsigned_number(NUMBER) \
262 write_number ((NUMBER), /*unsigned_p=*/1, 10)
264 /* If DECL is a template instance, return nonzero and, if
265 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
266 Otherwise return zero. */
268 static int
269 decl_is_template_id (const tree decl, tree* const template_info)
271 if (TREE_CODE (decl) == TYPE_DECL)
273 /* TYPE_DECLs are handled specially. Look at its type to decide
274 if this is a template instantiation. */
275 const tree type = TREE_TYPE (decl);
277 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
279 if (template_info != NULL)
280 /* For a templated TYPE_DECL, the template info is hanging
281 off the type. */
282 *template_info = TYPE_TEMPLATE_INFO (type);
283 return 1;
286 else
288 /* Check if this is a primary template. */
289 if (DECL_LANG_SPECIFIC (decl) != NULL
290 && DECL_USE_TEMPLATE (decl)
291 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
292 && TREE_CODE (decl) != TEMPLATE_DECL)
294 if (template_info != NULL)
295 /* For most templated decls, the template info is hanging
296 off the decl. */
297 *template_info = DECL_TEMPLATE_INFO (decl);
298 return 1;
302 /* It's not a template id. */
303 return 0;
306 /* Produce debugging output of current substitution candidates. */
308 static void
309 dump_substitution_candidates (void)
311 unsigned i;
312 tree el;
314 fprintf (stderr, " ++ substitutions ");
315 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
317 const char *name = "???";
319 if (i > 0)
320 fprintf (stderr, " ");
321 if (DECL_P (el))
322 name = IDENTIFIER_POINTER (DECL_NAME (el));
323 else if (TREE_CODE (el) == TREE_LIST)
324 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
325 else if (TYPE_NAME (el))
326 name = IDENTIFIER_POINTER (TYPE_IDENTIFIER (el));
327 fprintf (stderr, " S%d_ = ", i - 1);
328 if (TYPE_P (el) &&
329 (CP_TYPE_RESTRICT_P (el)
330 || CP_TYPE_VOLATILE_P (el)
331 || CP_TYPE_CONST_P (el)))
332 fprintf (stderr, "CV-");
333 fprintf (stderr, "%s (%s at %p)\n",
334 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
338 /* Both decls and types can be substitution candidates, but sometimes
339 they refer to the same thing. For instance, a TYPE_DECL and
340 RECORD_TYPE for the same class refer to the same thing, and should
341 be treated accordingly in substitutions. This function returns a
342 canonicalized tree node representing NODE that is used when adding
343 and substitution candidates and finding matches. */
345 static inline tree
346 canonicalize_for_substitution (tree node)
348 /* For a TYPE_DECL, use the type instead. */
349 if (TREE_CODE (node) == TYPE_DECL)
350 node = TREE_TYPE (node);
351 if (TYPE_P (node)
352 && TYPE_CANONICAL (node) != node
353 && TYPE_MAIN_VARIANT (node) != node)
355 tree orig = node;
356 /* Here we want to strip the topmost typedef only.
357 We need to do that so is_std_substitution can do proper
358 name matching. */
359 if (TREE_CODE (node) == FUNCTION_TYPE)
360 /* Use build_qualified_type and TYPE_QUALS here to preserve
361 the old buggy mangling of attribute noreturn with abi<5. */
362 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
363 TYPE_QUALS (node));
364 else
365 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
366 cp_type_quals (node));
367 if (TREE_CODE (node) == FUNCTION_TYPE
368 || TREE_CODE (node) == METHOD_TYPE)
369 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
371 return node;
374 /* Add NODE as a substitution candidate. NODE must not already be on
375 the list of candidates. */
377 static void
378 add_substitution (tree node)
380 tree c;
382 if (DEBUG_MANGLE)
383 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
384 get_tree_code_name (TREE_CODE (node)), (void *) node);
386 /* Get the canonicalized substitution candidate for NODE. */
387 c = canonicalize_for_substitution (node);
388 if (DEBUG_MANGLE && c != node)
389 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
390 get_tree_code_name (TREE_CODE (node)), (void *) node);
391 node = c;
393 #if ENABLE_CHECKING
394 /* Make sure NODE isn't already a candidate. */
396 int i;
397 tree candidate;
399 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
401 gcc_assert (!(DECL_P (node) && node == candidate));
402 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
403 && same_type_p (node, candidate)));
406 #endif /* ENABLE_CHECKING */
408 /* Put the decl onto the varray of substitution candidates. */
409 vec_safe_push (G.substitutions, node);
411 if (DEBUG_MANGLE)
412 dump_substitution_candidates ();
415 /* Helper function for find_substitution. Returns nonzero if NODE,
416 which may be a decl or a CLASS_TYPE, is a template-id with template
417 name of substitution_index[INDEX] in the ::std namespace. */
419 static inline int
420 is_std_substitution (const tree node,
421 const substitution_identifier_index_t index)
423 tree type = NULL;
424 tree decl = NULL;
426 if (DECL_P (node))
428 type = TREE_TYPE (node);
429 decl = node;
431 else if (CLASS_TYPE_P (node))
433 type = node;
434 decl = TYPE_NAME (node);
436 else
437 /* These are not the droids you're looking for. */
438 return 0;
440 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
441 && TYPE_LANG_SPECIFIC (type)
442 && TYPE_TEMPLATE_INFO (type)
443 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
444 == subst_identifiers[index]));
447 /* Helper function for find_substitution. Returns nonzero if NODE,
448 which may be a decl or a CLASS_TYPE, is the template-id
449 ::std::identifier<char>, where identifier is
450 substitution_index[INDEX]. */
452 static inline int
453 is_std_substitution_char (const tree node,
454 const substitution_identifier_index_t index)
456 tree args;
457 /* Check NODE's name is ::std::identifier. */
458 if (!is_std_substitution (node, index))
459 return 0;
460 /* Figure out its template args. */
461 if (DECL_P (node))
462 args = DECL_TI_ARGS (node);
463 else if (CLASS_TYPE_P (node))
464 args = CLASSTYPE_TI_ARGS (node);
465 else
466 /* Oops, not a template. */
467 return 0;
468 /* NODE's template arg list should be <char>. */
469 return
470 TREE_VEC_LENGTH (args) == 1
471 && TREE_VEC_ELT (args, 0) == char_type_node;
474 /* Check whether a substitution should be used to represent NODE in
475 the mangling.
477 First, check standard special-case substitutions.
479 <substitution> ::= St
480 # ::std
482 ::= Sa
483 # ::std::allocator
485 ::= Sb
486 # ::std::basic_string
488 ::= Ss
489 # ::std::basic_string<char,
490 ::std::char_traits<char>,
491 ::std::allocator<char> >
493 ::= Si
494 # ::std::basic_istream<char, ::std::char_traits<char> >
496 ::= So
497 # ::std::basic_ostream<char, ::std::char_traits<char> >
499 ::= Sd
500 # ::std::basic_iostream<char, ::std::char_traits<char> >
502 Then examine the stack of currently available substitution
503 candidates for entities appearing earlier in the same mangling
505 If a substitution is found, write its mangled representation and
506 return nonzero. If none is found, just return zero. */
508 static int
509 find_substitution (tree node)
511 int i;
512 const int size = vec_safe_length (G.substitutions);
513 tree decl;
514 tree type;
516 if (DEBUG_MANGLE)
517 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
518 get_tree_code_name (TREE_CODE (node)), (void *) node);
520 /* Obtain the canonicalized substitution representation for NODE.
521 This is what we'll compare against. */
522 node = canonicalize_for_substitution (node);
524 /* Check for builtin substitutions. */
526 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
527 type = TYPE_P (node) ? node : TREE_TYPE (node);
529 /* Check for std::allocator. */
530 if (decl
531 && is_std_substitution (decl, SUBID_ALLOCATOR)
532 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
534 write_string ("Sa");
535 return 1;
538 /* Check for std::basic_string. */
539 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
541 if (TYPE_P (node))
543 /* If this is a type (i.e. a fully-qualified template-id),
544 check for
545 std::basic_string <char,
546 std::char_traits<char>,
547 std::allocator<char> > . */
548 if (cp_type_quals (type) == TYPE_UNQUALIFIED
549 && CLASSTYPE_USE_TEMPLATE (type))
551 tree args = CLASSTYPE_TI_ARGS (type);
552 if (TREE_VEC_LENGTH (args) == 3
553 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
554 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
555 SUBID_CHAR_TRAITS)
556 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
557 SUBID_ALLOCATOR))
559 write_string ("Ss");
560 return 1;
564 else
565 /* Substitute for the template name only if this isn't a type. */
567 write_string ("Sb");
568 return 1;
572 /* Check for basic_{i,o,io}stream. */
573 if (TYPE_P (node)
574 && cp_type_quals (type) == TYPE_UNQUALIFIED
575 && CLASS_TYPE_P (type)
576 && CLASSTYPE_USE_TEMPLATE (type)
577 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
579 /* First, check for the template
580 args <char, std::char_traits<char> > . */
581 tree args = CLASSTYPE_TI_ARGS (type);
582 if (TREE_VEC_LENGTH (args) == 2
583 && TYPE_P (TREE_VEC_ELT (args, 0))
584 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
585 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
586 SUBID_CHAR_TRAITS))
588 /* Got them. Is this basic_istream? */
589 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
591 write_string ("Si");
592 return 1;
594 /* Or basic_ostream? */
595 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
597 write_string ("So");
598 return 1;
600 /* Or basic_iostream? */
601 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
603 write_string ("Sd");
604 return 1;
609 /* Check for namespace std. */
610 if (decl && DECL_NAMESPACE_STD_P (decl))
612 write_string ("St");
613 return 1;
616 /* Now check the list of available substitutions for this mangling
617 operation. */
618 for (i = 0; i < size; ++i)
620 tree candidate = (*G.substitutions)[i];
621 /* NODE is a matched to a candidate if it's the same decl node or
622 if it's the same type. */
623 if (decl == candidate
624 || (TYPE_P (candidate) && type && TYPE_P (node)
625 && same_type_p (type, candidate))
626 || NESTED_TEMPLATE_MATCH (node, candidate))
628 write_substitution (i);
629 return 1;
633 /* No substitution found. */
634 return 0;
638 /* TOP_LEVEL is true, if this is being called at outermost level of
639 mangling. It should be false when mangling a decl appearing in an
640 expression within some other mangling.
642 <mangled-name> ::= _Z <encoding> */
644 static void
645 write_mangled_name (const tree decl, bool top_level)
647 MANGLE_TRACE_TREE ("mangled-name", decl);
649 if (/* The names of `extern "C"' functions are not mangled. */
650 DECL_EXTERN_C_FUNCTION_P (decl)
651 /* But overloaded operator names *are* mangled. */
652 && !DECL_OVERLOADED_OPERATOR_P (decl))
654 unmangled_name:;
656 if (top_level)
657 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
658 else
660 /* The standard notes: "The <encoding> of an extern "C"
661 function is treated like global-scope data, i.e. as its
662 <source-name> without a type." We cannot write
663 overloaded operators that way though, because it contains
664 characters invalid in assembler. */
665 if (abi_version_at_least (2))
666 write_string ("_Z");
667 else
668 G.need_abi_warning = true;
669 write_source_name (DECL_NAME (decl));
672 else if (VAR_P (decl)
673 /* The names of non-static global variables aren't mangled. */
674 && DECL_EXTERNAL_LINKAGE_P (decl)
675 && (CP_DECL_CONTEXT (decl) == global_namespace
676 /* And neither are `extern "C"' variables. */
677 || DECL_EXTERN_C_P (decl)))
679 if (top_level || abi_version_at_least (2))
680 goto unmangled_name;
681 else
683 G.need_abi_warning = true;
684 goto mangled_name;
687 else
689 mangled_name:;
690 write_string ("_Z");
691 write_encoding (decl);
695 /* <encoding> ::= <function name> <bare-function-type>
696 ::= <data name> */
698 static void
699 write_encoding (const tree decl)
701 MANGLE_TRACE_TREE ("encoding", decl);
703 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
705 /* For overloaded operators write just the mangled name
706 without arguments. */
707 if (DECL_OVERLOADED_OPERATOR_P (decl))
708 write_name (decl, /*ignore_local_scope=*/0);
709 else
710 write_source_name (DECL_NAME (decl));
711 return;
714 write_name (decl, /*ignore_local_scope=*/0);
715 if (TREE_CODE (decl) == FUNCTION_DECL)
717 tree fn_type;
718 tree d;
720 if (decl_is_template_id (decl, NULL))
722 fn_type = get_mostly_instantiated_function_type (decl);
723 /* FN_TYPE will not have parameter types for in-charge or
724 VTT parameters. Therefore, we pass NULL_TREE to
725 write_bare_function_type -- otherwise, it will get
726 confused about which artificial parameters to skip. */
727 d = NULL_TREE;
729 else
731 fn_type = TREE_TYPE (decl);
732 d = decl;
735 write_bare_function_type (fn_type,
736 (!DECL_CONSTRUCTOR_P (decl)
737 && !DECL_DESTRUCTOR_P (decl)
738 && !DECL_CONV_FN_P (decl)
739 && decl_is_template_id (decl, NULL)),
744 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
745 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
747 static tree
748 decl_mangling_context (tree decl)
750 tree tcontext = targetm.cxx.decl_mangling_context (decl);
752 if (tcontext != NULL_TREE)
753 return tcontext;
755 if (TREE_CODE (decl) == TYPE_DECL
756 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
758 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
759 if (extra)
760 return extra;
762 else if (TREE_CODE (decl) == TYPE_DECL
763 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
764 /* template type parms have no mangling context. */
765 return NULL_TREE;
766 return CP_DECL_CONTEXT (decl);
769 /* <name> ::= <unscoped-name>
770 ::= <unscoped-template-name> <template-args>
771 ::= <nested-name>
772 ::= <local-name>
774 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
775 called from <local-name>, which mangles the enclosing scope
776 elsewhere and then uses this function to mangle just the part
777 underneath the function scope. So don't use the <local-name>
778 production, to avoid an infinite recursion. */
780 static void
781 write_name (tree decl, const int ignore_local_scope)
783 tree context;
785 MANGLE_TRACE_TREE ("name", decl);
787 if (TREE_CODE (decl) == TYPE_DECL)
789 /* In case this is a typedef, fish out the corresponding
790 TYPE_DECL for the main variant. */
791 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
794 context = decl_mangling_context (decl);
796 gcc_assert (context != NULL_TREE);
798 /* A decl in :: or ::std scope is treated specially. The former is
799 mangled using <unscoped-name> or <unscoped-template-name>, the
800 latter with a special substitution. Also, a name that is
801 directly in a local function scope is also mangled with
802 <unscoped-name> rather than a full <nested-name>. */
803 if (context == global_namespace
804 || DECL_NAMESPACE_STD_P (context)
805 || (ignore_local_scope
806 && (TREE_CODE (context) == FUNCTION_DECL
807 || (abi_version_at_least (7)
808 && TREE_CODE (context) == PARM_DECL))))
810 tree template_info;
811 /* Is this a template instance? */
812 if (decl_is_template_id (decl, &template_info))
814 /* Yes: use <unscoped-template-name>. */
815 write_unscoped_template_name (TI_TEMPLATE (template_info));
816 write_template_args (TI_ARGS (template_info));
818 else
819 /* Everything else gets an <unqualified-name>. */
820 write_unscoped_name (decl);
822 else
824 /* Handle local names, unless we asked not to (that is, invoked
825 under <local-name>, to handle only the part of the name under
826 the local scope). */
827 if (!ignore_local_scope)
829 /* Scan up the list of scope context, looking for a
830 function. If we find one, this entity is in local
831 function scope. local_entity tracks context one scope
832 level down, so it will contain the element that's
833 directly in that function's scope, either decl or one of
834 its enclosing scopes. */
835 tree local_entity = decl;
836 while (context != global_namespace)
838 /* Make sure we're always dealing with decls. */
839 if (TYPE_P (context))
840 context = TYPE_NAME (context);
841 /* Is this a function? */
842 if (TREE_CODE (context) == FUNCTION_DECL
843 || TREE_CODE (context) == PARM_DECL)
845 /* Yes, we have local scope. Use the <local-name>
846 production for the innermost function scope. */
847 write_local_name (context, local_entity, decl);
848 return;
850 /* Up one scope level. */
851 local_entity = context;
852 context = decl_mangling_context (context);
855 /* No local scope found? Fall through to <nested-name>. */
858 /* Other decls get a <nested-name> to encode their scope. */
859 write_nested_name (decl);
863 /* <unscoped-name> ::= <unqualified-name>
864 ::= St <unqualified-name> # ::std:: */
866 static void
867 write_unscoped_name (const tree decl)
869 tree context = decl_mangling_context (decl);
871 MANGLE_TRACE_TREE ("unscoped-name", decl);
873 /* Is DECL in ::std? */
874 if (DECL_NAMESPACE_STD_P (context))
876 write_string ("St");
877 write_unqualified_name (decl);
879 else
881 /* If not, it should be either in the global namespace, or directly
882 in a local function scope. A lambda can also be mangled in the
883 scope of a default argument. */
884 gcc_assert (context == global_namespace
885 || TREE_CODE (context) == PARM_DECL
886 || TREE_CODE (context) == FUNCTION_DECL);
888 write_unqualified_name (decl);
892 /* <unscoped-template-name> ::= <unscoped-name>
893 ::= <substitution> */
895 static void
896 write_unscoped_template_name (const tree decl)
898 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
900 if (find_substitution (decl))
901 return;
902 write_unscoped_name (decl);
903 add_substitution (decl);
906 /* Write the nested name, including CV-qualifiers, of DECL.
908 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
909 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
911 <ref-qualifier> ::= R # & ref-qualifier
912 ::= O # && ref-qualifier
913 <CV-qualifiers> ::= [r] [V] [K] */
915 static void
916 write_nested_name (const tree decl)
918 tree template_info;
920 MANGLE_TRACE_TREE ("nested-name", decl);
922 write_char ('N');
924 /* Write CV-qualifiers, if this is a member function. */
925 if (TREE_CODE (decl) == FUNCTION_DECL
926 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
928 if (DECL_VOLATILE_MEMFUNC_P (decl))
929 write_char ('V');
930 if (DECL_CONST_MEMFUNC_P (decl))
931 write_char ('K');
932 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
934 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
935 write_char ('O');
936 else
937 write_char ('R');
941 /* Is this a template instance? */
942 if (decl_is_template_id (decl, &template_info))
944 /* Yes, use <template-prefix>. */
945 write_template_prefix (decl);
946 write_template_args (TI_ARGS (template_info));
948 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
950 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
951 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
953 write_template_prefix (decl);
954 write_template_args (TREE_OPERAND (name, 1));
956 else
958 write_prefix (decl_mangling_context (decl));
959 write_unqualified_name (decl);
962 else
964 /* No, just use <prefix> */
965 write_prefix (decl_mangling_context (decl));
966 write_unqualified_name (decl);
968 write_char ('E');
971 /* <prefix> ::= <prefix> <unqualified-name>
972 ::= <template-param>
973 ::= <template-prefix> <template-args>
974 ::= <decltype>
975 ::= # empty
976 ::= <substitution> */
978 static void
979 write_prefix (const tree node)
981 tree decl;
982 /* Non-NULL if NODE represents a template-id. */
983 tree template_info = NULL;
985 if (node == NULL
986 || node == global_namespace)
987 return;
989 MANGLE_TRACE_TREE ("prefix", node);
991 if (TREE_CODE (node) == DECLTYPE_TYPE)
993 write_type (node);
994 return;
997 if (find_substitution (node))
998 return;
1000 if (DECL_P (node))
1002 /* If this is a function or parm decl, that means we've hit function
1003 scope, so this prefix must be for a local name. In this
1004 case, we're under the <local-name> production, which encodes
1005 the enclosing function scope elsewhere. So don't continue
1006 here. */
1007 if (TREE_CODE (node) == FUNCTION_DECL
1008 || TREE_CODE (node) == PARM_DECL)
1009 return;
1011 decl = node;
1012 decl_is_template_id (decl, &template_info);
1014 else
1016 /* Node is a type. */
1017 decl = TYPE_NAME (node);
1018 if (CLASSTYPE_TEMPLATE_ID_P (node))
1019 template_info = TYPE_TEMPLATE_INFO (node);
1022 /* In G++ 3.2, the name of the template parameter was used. */
1023 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1024 && !abi_version_at_least (2))
1025 G.need_abi_warning = true;
1027 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1028 && abi_version_at_least (2))
1029 write_template_param (node);
1030 else if (template_info != NULL)
1031 /* Templated. */
1033 write_template_prefix (decl);
1034 write_template_args (TI_ARGS (template_info));
1036 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1038 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1039 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1041 write_template_prefix (decl);
1042 write_template_args (TREE_OPERAND (name, 1));
1044 else
1046 write_prefix (decl_mangling_context (decl));
1047 write_unqualified_name (decl);
1050 else
1051 /* Not templated. */
1053 write_prefix (decl_mangling_context (decl));
1054 write_unqualified_name (decl);
1055 if (VAR_P (decl)
1056 || TREE_CODE (decl) == FIELD_DECL)
1058 /* <data-member-prefix> := <member source-name> M */
1059 write_char ('M');
1060 return;
1064 add_substitution (node);
1067 /* <template-prefix> ::= <prefix> <template component>
1068 ::= <template-param>
1069 ::= <substitution> */
1071 static void
1072 write_template_prefix (const tree node)
1074 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1075 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1076 tree context = decl_mangling_context (decl);
1077 tree template_info;
1078 tree templ;
1079 tree substitution;
1081 MANGLE_TRACE_TREE ("template-prefix", node);
1083 /* Find the template decl. */
1084 if (decl_is_template_id (decl, &template_info))
1085 templ = TI_TEMPLATE (template_info);
1086 else if (TREE_CODE (type) == TYPENAME_TYPE)
1087 /* For a typename type, all we have is the name. */
1088 templ = DECL_NAME (decl);
1089 else
1091 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1093 templ = TYPE_TI_TEMPLATE (type);
1096 /* For a member template, though, the template name for the
1097 innermost name must have all the outer template levels
1098 instantiated. For instance, consider
1100 template<typename T> struct Outer {
1101 template<typename U> struct Inner {};
1104 The template name for `Inner' in `Outer<int>::Inner<float>' is
1105 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1106 levels separately, so there's no TEMPLATE_DECL available for this
1107 (there's only `Outer<T>::Inner<U>').
1109 In order to get the substitutions right, we create a special
1110 TREE_LIST to represent the substitution candidate for a nested
1111 template. The TREE_PURPOSE is the template's context, fully
1112 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1113 template.
1115 So, for the example above, `Outer<int>::Inner' is represented as a
1116 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1117 and whose value is `Outer<T>::Inner<U>'. */
1118 if (TYPE_P (context))
1119 substitution = build_tree_list (context, templ);
1120 else
1121 substitution = templ;
1123 if (find_substitution (substitution))
1124 return;
1126 /* In G++ 3.2, the name of the template template parameter was used. */
1127 if (TREE_TYPE (templ)
1128 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1129 && !abi_version_at_least (2))
1130 G.need_abi_warning = true;
1132 if (TREE_TYPE (templ)
1133 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1134 && abi_version_at_least (2))
1135 write_template_param (TREE_TYPE (templ));
1136 else
1138 write_prefix (context);
1139 write_unqualified_name (decl);
1142 add_substitution (substitution);
1145 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1146 mangled through special entry points.
1148 <unqualified-name> ::= <operator-name>
1149 ::= <special-name>
1150 ::= <source-name>
1151 ::= <unnamed-type-name>
1152 ::= <local-source-name>
1154 <local-source-name> ::= L <source-name> <discriminator> */
1156 static void
1157 write_unqualified_id (tree identifier)
1159 if (IDENTIFIER_TYPENAME_P (identifier))
1160 write_conversion_operator_name (TREE_TYPE (identifier));
1161 else if (IDENTIFIER_OPNAME_P (identifier))
1163 int i;
1164 const char *mangled_name = NULL;
1166 /* Unfortunately, there is no easy way to go from the
1167 name of the operator back to the corresponding tree
1168 code. */
1169 for (i = 0; i < MAX_TREE_CODES; ++i)
1170 if (operator_name_info[i].identifier == identifier)
1172 /* The ABI says that we prefer binary operator
1173 names to unary operator names. */
1174 if (operator_name_info[i].arity == 2)
1176 mangled_name = operator_name_info[i].mangled_name;
1177 break;
1179 else if (!mangled_name)
1180 mangled_name = operator_name_info[i].mangled_name;
1182 else if (assignment_operator_name_info[i].identifier
1183 == identifier)
1185 mangled_name
1186 = assignment_operator_name_info[i].mangled_name;
1187 break;
1189 write_string (mangled_name);
1191 else if (UDLIT_OPER_P (identifier))
1192 write_literal_operator_name (identifier);
1193 else
1194 write_source_name (identifier);
1197 static void
1198 write_unqualified_name (tree decl)
1200 MANGLE_TRACE_TREE ("unqualified-name", decl);
1202 if (identifier_p (decl))
1204 write_unqualified_id (decl);
1205 return;
1208 bool found = false;
1210 if (DECL_NAME (decl) == NULL_TREE)
1212 found = true;
1213 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1214 write_source_name (DECL_ASSEMBLER_NAME (decl));
1216 else if (DECL_DECLARES_FUNCTION_P (decl))
1218 found = true;
1219 if (DECL_CONSTRUCTOR_P (decl))
1220 write_special_name_constructor (decl);
1221 else if (DECL_DESTRUCTOR_P (decl))
1222 write_special_name_destructor (decl);
1223 else if (DECL_CONV_FN_P (decl))
1225 /* Conversion operator. Handle it right here.
1226 <operator> ::= cv <type> */
1227 tree type;
1228 if (decl_is_template_id (decl, NULL))
1230 tree fn_type;
1231 fn_type = get_mostly_instantiated_function_type (decl);
1232 type = TREE_TYPE (fn_type);
1234 else if (FNDECL_USED_AUTO (decl))
1235 type = (DECL_STRUCT_FUNCTION (decl)->language
1236 ->x_auto_return_pattern);
1237 else
1238 type = DECL_CONV_FN_TYPE (decl);
1239 write_conversion_operator_name (type);
1241 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1243 operator_name_info_t *oni;
1244 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1245 oni = assignment_operator_name_info;
1246 else
1247 oni = operator_name_info;
1249 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1251 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1252 write_literal_operator_name (DECL_NAME (decl));
1253 else
1254 found = false;
1257 if (found)
1258 /* OK */;
1259 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1260 && DECL_NAMESPACE_SCOPE_P (decl)
1261 && decl_linkage (decl) == lk_internal)
1263 MANGLE_TRACE_TREE ("local-source-name", decl);
1264 write_char ('L');
1265 write_source_name (DECL_NAME (decl));
1266 /* The default discriminator is 1, and that's all we ever use,
1267 so there's no code to output one here. */
1269 else
1271 tree type = TREE_TYPE (decl);
1273 if (TREE_CODE (decl) == TYPE_DECL
1274 && TYPE_ANONYMOUS_P (type))
1275 write_unnamed_type_name (type);
1276 else if (TREE_CODE (decl) == TYPE_DECL
1277 && LAMBDA_TYPE_P (type))
1278 write_closure_type_name (type);
1279 else
1280 write_source_name (DECL_NAME (decl));
1283 /* We use the ABI tags from the primary template, ignoring tags on any
1284 specializations. This is necessary because C++ doesn't require a
1285 specialization to be declared before it is used unless the use
1286 requires a complete type, but we need to get the tags right on
1287 incomplete types as well. */
1288 if (tree tmpl = most_general_template (decl))
1289 decl = DECL_TEMPLATE_RESULT (tmpl);
1290 /* Don't crash on an unbound class template. */
1291 if (decl)
1293 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1294 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1295 : DECL_ATTRIBUTES (decl));
1296 write_abi_tags (lookup_attribute ("abi_tag", attrs));
1300 /* Write the unqualified-name for a conversion operator to TYPE. */
1302 static void
1303 write_conversion_operator_name (const tree type)
1305 write_string ("cv");
1306 write_type (type);
1309 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1311 <source-name> ::= </length/ number> <identifier> */
1313 static void
1314 write_source_name (tree identifier)
1316 MANGLE_TRACE_TREE ("source-name", identifier);
1318 /* Never write the whole template-id name including the template
1319 arguments; we only want the template name. */
1320 if (IDENTIFIER_TEMPLATE (identifier))
1321 identifier = IDENTIFIER_TEMPLATE (identifier);
1323 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1324 write_identifier (IDENTIFIER_POINTER (identifier));
1327 /* Compare two TREE_STRINGs like strcmp. */
1330 tree_string_cmp (const void *p1, const void *p2)
1332 if (p1 == p2)
1333 return 0;
1334 tree s1 = *(const tree*)p1;
1335 tree s2 = *(const tree*)p2;
1336 return strcmp (TREE_STRING_POINTER (s1),
1337 TREE_STRING_POINTER (s2));
1340 /* ID is the name of a function or type with abi_tags attribute TAGS.
1341 Write out the name, suitably decorated. */
1343 static void
1344 write_abi_tags (tree tags)
1346 if (tags == NULL_TREE)
1347 return;
1349 tags = TREE_VALUE (tags);
1351 vec<tree, va_gc> * vec = make_tree_vector();
1353 for (tree t = tags; t; t = TREE_CHAIN (t))
1355 if (ABI_TAG_IMPLICIT (t))
1356 continue;
1357 tree str = TREE_VALUE (t);
1358 vec_safe_push (vec, str);
1361 vec->qsort (tree_string_cmp);
1363 unsigned i; tree str;
1364 FOR_EACH_VEC_ELT (*vec, i, str)
1366 write_string ("B");
1367 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1368 write_identifier (TREE_STRING_POINTER (str));
1371 release_tree_vector (vec);
1374 /* Write a user-defined literal operator.
1375 ::= li <source-name> # "" <source-name>
1376 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1378 static void
1379 write_literal_operator_name (tree identifier)
1381 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1382 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1383 write_unsigned_number (strlen (suffix));
1384 write_identifier (suffix);
1387 /* Encode 0 as _, and 1+ as n-1_. */
1389 static void
1390 write_compact_number (int num)
1392 if (num > 0)
1393 write_unsigned_number (num - 1);
1394 write_char ('_');
1397 /* Return how many unnamed types precede TYPE in its enclosing class. */
1399 static int
1400 nested_anon_class_index (tree type)
1402 int index = 0;
1403 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1404 for (; member; member = DECL_CHAIN (member))
1405 if (DECL_IMPLICIT_TYPEDEF_P (member))
1407 tree memtype = TREE_TYPE (member);
1408 if (memtype == type)
1409 return index;
1410 else if (TYPE_ANONYMOUS_P (memtype))
1411 ++index;
1414 gcc_unreachable ();
1417 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1419 static void
1420 write_unnamed_type_name (const tree type)
1422 int discriminator;
1423 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1425 if (TYPE_FUNCTION_SCOPE_P (type))
1426 discriminator = local_class_index (type);
1427 else if (TYPE_CLASS_SCOPE_P (type))
1428 discriminator = nested_anon_class_index (type);
1429 else
1431 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1432 /* Just use the old mangling at namespace scope. */
1433 write_source_name (TYPE_IDENTIFIER (type));
1434 return;
1437 write_string ("Ut");
1438 write_compact_number (discriminator);
1441 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1442 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1444 static void
1445 write_closure_type_name (const tree type)
1447 tree fn = lambda_function (type);
1448 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1449 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1451 MANGLE_TRACE_TREE ("closure-type-name", type);
1453 write_string ("Ul");
1454 write_method_parms (parms, /*method_p=*/1, fn);
1455 write_char ('E');
1456 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1459 /* Convert NUMBER to ascii using base BASE and generating at least
1460 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1461 into which to store the characters. Returns the number of
1462 characters generated (these will be laid out in advance of where
1463 BUFFER points). */
1465 static int
1466 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1467 char *buffer, const unsigned int min_digits)
1469 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1470 unsigned digits = 0;
1472 while (number)
1474 unsigned HOST_WIDE_INT d = number / base;
1476 *--buffer = base_digits[number - d * base];
1477 digits++;
1478 number = d;
1480 while (digits < min_digits)
1482 *--buffer = base_digits[0];
1483 digits++;
1485 return digits;
1488 /* Non-terminal <number>.
1490 <number> ::= [n] </decimal integer/> */
1492 static void
1493 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1494 const unsigned int base)
1496 char buffer[sizeof (HOST_WIDE_INT) * 8];
1497 unsigned count = 0;
1499 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1501 write_char ('n');
1502 number = -((HOST_WIDE_INT) number);
1504 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1505 write_chars (buffer + sizeof (buffer) - count, count);
1508 /* Write out an integral CST in decimal. Most numbers are small, and
1509 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1510 bigger than that, which we must deal with. */
1512 static inline void
1513 write_integer_cst (const tree cst)
1515 int sign = tree_int_cst_sgn (cst);
1517 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1519 /* A bignum. We do this in chunks, each of which fits in a
1520 HOST_WIDE_INT. */
1521 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1522 unsigned HOST_WIDE_INT chunk;
1523 unsigned chunk_digits;
1524 char *ptr = buffer + sizeof (buffer);
1525 unsigned count = 0;
1526 tree n, base, type;
1527 int done;
1529 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1530 representable. */
1531 chunk = 1000000000;
1532 chunk_digits = 9;
1534 if (sizeof (HOST_WIDE_INT) >= 8)
1536 /* It is at least 64 bits, so 10^18 is representable. */
1537 chunk_digits = 18;
1538 chunk *= chunk;
1541 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1542 base = build_int_cstu (type, chunk);
1543 n = build_int_cst_wide (type,
1544 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1546 if (sign < 0)
1548 write_char ('n');
1549 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1553 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1554 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1555 unsigned c;
1557 done = integer_zerop (d);
1558 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1559 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1560 done ? 1 : chunk_digits);
1561 ptr -= c;
1562 count += c;
1563 n = d;
1565 while (!done);
1566 write_chars (ptr, count);
1568 else
1570 /* A small num. */
1571 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1573 if (sign < 0)
1575 write_char ('n');
1576 low = -low;
1578 write_unsigned_number (low);
1582 /* Write out a floating-point literal.
1584 "Floating-point literals are encoded using the bit pattern of the
1585 target processor's internal representation of that number, as a
1586 fixed-length lowercase hexadecimal string, high-order bytes first
1587 (even if the target processor would store low-order bytes first).
1588 The "n" prefix is not used for floating-point literals; the sign
1589 bit is encoded with the rest of the number.
1591 Here are some examples, assuming the IEEE standard representation
1592 for floating point numbers. (Spaces are for readability, not
1593 part of the encoding.)
1595 1.0f Lf 3f80 0000 E
1596 -1.0f Lf bf80 0000 E
1597 1.17549435e-38f Lf 0080 0000 E
1598 1.40129846e-45f Lf 0000 0001 E
1599 0.0f Lf 0000 0000 E"
1601 Caller is responsible for the Lx and the E. */
1602 static void
1603 write_real_cst (const tree value)
1605 if (abi_version_at_least (2))
1607 long target_real[4]; /* largest supported float */
1608 char buffer[9]; /* eight hex digits in a 32-bit number */
1609 int i, limit, dir;
1611 tree type = TREE_TYPE (value);
1612 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1614 real_to_target (target_real, &TREE_REAL_CST (value),
1615 TYPE_MODE (type));
1617 /* The value in target_real is in the target word order,
1618 so we must write it out backward if that happens to be
1619 little-endian. write_number cannot be used, it will
1620 produce uppercase. */
1621 if (FLOAT_WORDS_BIG_ENDIAN)
1622 i = 0, limit = words, dir = 1;
1623 else
1624 i = words - 1, limit = -1, dir = -1;
1626 for (; i != limit; i += dir)
1628 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1629 write_chars (buffer, 8);
1632 else
1634 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1635 literally. Note that compatibility with 3.2 is impossible,
1636 because the old floating-point emulator used a different
1637 format for REAL_VALUE_TYPE. */
1638 size_t i;
1639 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1640 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1641 /*unsigned_p*/ 1,
1642 /*base*/ 16);
1643 G.need_abi_warning = 1;
1647 /* Non-terminal <identifier>.
1649 <identifier> ::= </unqualified source code identifier> */
1651 static void
1652 write_identifier (const char *identifier)
1654 MANGLE_TRACE ("identifier", identifier);
1655 write_string (identifier);
1658 /* Handle constructor productions of non-terminal <special-name>.
1659 CTOR is a constructor FUNCTION_DECL.
1661 <special-name> ::= C1 # complete object constructor
1662 ::= C2 # base object constructor
1663 ::= C3 # complete object allocating constructor
1665 Currently, allocating constructors are never used. */
1667 static void
1668 write_special_name_constructor (const tree ctor)
1670 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1671 write_string ("C2");
1672 /* This is the old-style "[unified]" constructor.
1673 In some cases, we may emit this function and call
1674 it from the clones in order to share code and save space. */
1675 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1676 write_string ("C4");
1677 else
1679 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1680 write_string ("C1");
1684 /* Handle destructor productions of non-terminal <special-name>.
1685 DTOR is a destructor FUNCTION_DECL.
1687 <special-name> ::= D0 # deleting (in-charge) destructor
1688 ::= D1 # complete object (in-charge) destructor
1689 ::= D2 # base object (not-in-charge) destructor */
1691 static void
1692 write_special_name_destructor (const tree dtor)
1694 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1695 write_string ("D0");
1696 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1697 write_string ("D2");
1698 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1699 /* This is the old-style "[unified]" destructor.
1700 In some cases, we may emit this function and call
1701 it from the clones in order to share code and save space. */
1702 write_string ("D4");
1703 else
1705 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1706 write_string ("D1");
1710 /* Scan the vector of local classes and return how many others with the
1711 same name (or same no name) and context precede ENTITY. */
1713 static int
1714 local_class_index (tree entity)
1716 int ix, discriminator = 0;
1717 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1718 : TYPE_IDENTIFIER (entity));
1719 tree ctx = TYPE_CONTEXT (entity);
1720 for (ix = 0; ; ix++)
1722 tree type = (*local_classes)[ix];
1723 if (type == entity)
1724 return discriminator;
1725 if (TYPE_CONTEXT (type) == ctx
1726 && (name ? TYPE_IDENTIFIER (type) == name
1727 : TYPE_ANONYMOUS_P (type)))
1728 ++discriminator;
1730 gcc_unreachable ();
1733 /* Return the discriminator for ENTITY appearing inside
1734 FUNCTION. The discriminator is the lexical ordinal of VAR among
1735 entities with the same name in the same FUNCTION. */
1737 static int
1738 discriminator_for_local_entity (tree entity)
1740 if (DECL_DISCRIMINATOR_P (entity))
1742 if (DECL_DISCRIMINATOR_SET_P (entity))
1743 return DECL_DISCRIMINATOR (entity);
1744 else
1745 /* The first entity with a particular name doesn't get
1746 DECL_DISCRIMINATOR set up. */
1747 return 0;
1749 else if (TREE_CODE (entity) == TYPE_DECL)
1751 /* Scan the list of local classes. */
1752 entity = TREE_TYPE (entity);
1754 /* Lambdas and unnamed types have their own discriminators. */
1755 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1756 return 0;
1758 return local_class_index (entity);
1760 else
1761 gcc_unreachable ();
1764 /* Return the discriminator for STRING, a string literal used inside
1765 FUNCTION. The discriminator is the lexical ordinal of STRING among
1766 string literals used in FUNCTION. */
1768 static int
1769 discriminator_for_string_literal (tree /*function*/,
1770 tree /*string*/)
1772 /* For now, we don't discriminate amongst string literals. */
1773 return 0;
1776 /* <discriminator> := _ <number>
1778 The discriminator is used only for the second and later occurrences
1779 of the same name within a single function. In this case <number> is
1780 n - 2, if this is the nth occurrence, in lexical order. */
1782 static void
1783 write_discriminator (const int discriminator)
1785 /* If discriminator is zero, don't write anything. Otherwise... */
1786 if (discriminator > 0)
1788 write_char ('_');
1789 write_unsigned_number (discriminator - 1);
1793 /* Mangle the name of a function-scope entity. FUNCTION is the
1794 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1795 default argument scope. ENTITY is the decl for the entity itself.
1796 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1797 either ENTITY itself or an enclosing scope of ENTITY.
1799 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1800 := Z <function encoding> E s [<discriminator>]
1801 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1803 static void
1804 write_local_name (tree function, const tree local_entity,
1805 const tree entity)
1807 tree parm = NULL_TREE;
1809 MANGLE_TRACE_TREE ("local-name", entity);
1811 if (TREE_CODE (function) == PARM_DECL)
1813 parm = function;
1814 function = DECL_CONTEXT (parm);
1817 write_char ('Z');
1818 write_encoding (function);
1819 write_char ('E');
1821 /* For this purpose, parameters are numbered from right-to-left. */
1822 if (parm)
1824 tree t;
1825 int i = 0;
1826 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1828 if (t == parm)
1829 i = 1;
1830 else if (i)
1831 ++i;
1833 write_char ('d');
1834 write_compact_number (i - 1);
1837 if (TREE_CODE (entity) == STRING_CST)
1839 write_char ('s');
1840 write_discriminator (discriminator_for_string_literal (function,
1841 entity));
1843 else
1845 /* Now the <entity name>. Let write_name know its being called
1846 from <local-name>, so it doesn't try to process the enclosing
1847 function scope again. */
1848 write_name (entity, /*ignore_local_scope=*/1);
1849 write_discriminator (discriminator_for_local_entity (local_entity));
1853 /* Non-terminals <type> and <CV-qualifier>.
1855 <type> ::= <builtin-type>
1856 ::= <function-type>
1857 ::= <class-enum-type>
1858 ::= <array-type>
1859 ::= <pointer-to-member-type>
1860 ::= <template-param>
1861 ::= <substitution>
1862 ::= <CV-qualifier>
1863 ::= P <type> # pointer-to
1864 ::= R <type> # reference-to
1865 ::= C <type> # complex pair (C 2000)
1866 ::= G <type> # imaginary (C 2000) [not supported]
1867 ::= U <source-name> <type> # vendor extended type qualifier
1869 C++0x extensions
1871 <type> ::= RR <type> # rvalue reference-to
1872 <type> ::= Dt <expression> # decltype of an id-expression or
1873 # class member access
1874 <type> ::= DT <expression> # decltype of an expression
1875 <type> ::= Dn # decltype of nullptr
1877 TYPE is a type node. */
1879 static void
1880 write_type (tree type)
1882 /* This gets set to nonzero if TYPE turns out to be a (possibly
1883 CV-qualified) builtin type. */
1884 int is_builtin_type = 0;
1886 MANGLE_TRACE_TREE ("type", type);
1888 if (type == error_mark_node)
1889 return;
1891 type = canonicalize_for_substitution (type);
1892 if (find_substitution (type))
1893 return;
1896 if (write_CV_qualifiers_for_type (type) > 0)
1897 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1898 mangle the unqualified type. The recursive call is needed here
1899 since both the qualified and unqualified types are substitution
1900 candidates. */
1902 tree t = TYPE_MAIN_VARIANT (type);
1903 if (TREE_CODE (t) == FUNCTION_TYPE
1904 || TREE_CODE (t) == METHOD_TYPE)
1906 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1907 if (abi_version_at_least (8))
1908 /* Avoid adding the unqualified function type as a substitution. */
1909 write_function_type (t);
1910 else
1911 write_type (t);
1913 else
1914 write_type (t);
1916 else if (TREE_CODE (type) == ARRAY_TYPE)
1917 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1918 so that the cv-qualification of the element type is available
1919 in write_array_type. */
1920 write_array_type (type);
1921 else
1923 tree type_orig = type;
1925 /* See through any typedefs. */
1926 type = TYPE_MAIN_VARIANT (type);
1927 if (TREE_CODE (type) == FUNCTION_TYPE
1928 || TREE_CODE (type) == METHOD_TYPE)
1929 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1931 /* According to the C++ ABI, some library classes are passed the
1932 same as the scalar type of their single member and use the same
1933 mangling. */
1934 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1935 type = TREE_TYPE (first_field (type));
1937 if (TYPE_PTRDATAMEM_P (type))
1938 write_pointer_to_member_type (type);
1939 else
1941 /* Handle any target-specific fundamental types. */
1942 const char *target_mangling
1943 = targetm.mangle_type (type_orig);
1945 if (target_mangling)
1947 write_string (target_mangling);
1948 /* Add substitutions for types other than fundamental
1949 types. */
1950 if (!VOID_TYPE_P (type)
1951 && TREE_CODE (type) != INTEGER_TYPE
1952 && TREE_CODE (type) != REAL_TYPE
1953 && TREE_CODE (type) != BOOLEAN_TYPE)
1954 add_substitution (type);
1955 return;
1958 switch (TREE_CODE (type))
1960 case VOID_TYPE:
1961 case BOOLEAN_TYPE:
1962 case INTEGER_TYPE: /* Includes wchar_t. */
1963 case REAL_TYPE:
1964 case FIXED_POINT_TYPE:
1966 /* If this is a typedef, TYPE may not be one of
1967 the standard builtin type nodes, but an alias of one. Use
1968 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1969 write_builtin_type (TYPE_MAIN_VARIANT (type));
1970 ++is_builtin_type;
1972 break;
1974 case COMPLEX_TYPE:
1975 write_char ('C');
1976 write_type (TREE_TYPE (type));
1977 break;
1979 case FUNCTION_TYPE:
1980 case METHOD_TYPE:
1981 write_function_type (type);
1982 break;
1984 case UNION_TYPE:
1985 case RECORD_TYPE:
1986 case ENUMERAL_TYPE:
1987 /* A pointer-to-member function is represented as a special
1988 RECORD_TYPE, so check for this first. */
1989 if (TYPE_PTRMEMFUNC_P (type))
1990 write_pointer_to_member_type (type);
1991 else
1992 write_class_enum_type (type);
1993 break;
1995 case TYPENAME_TYPE:
1996 case UNBOUND_CLASS_TEMPLATE:
1997 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1998 ordinary nested names. */
1999 write_nested_name (TYPE_STUB_DECL (type));
2000 break;
2002 case POINTER_TYPE:
2003 case REFERENCE_TYPE:
2004 if (TYPE_PTR_P (type))
2005 write_char ('P');
2006 else if (TYPE_REF_IS_RVALUE (type))
2007 write_char ('O');
2008 else
2009 write_char ('R');
2011 tree target = TREE_TYPE (type);
2012 /* Attribute const/noreturn are not reflected in mangling.
2013 We strip them here rather than at a lower level because
2014 a typedef or template argument can have function type
2015 with function-cv-quals (that use the same representation),
2016 but you can't have a pointer/reference to such a type. */
2017 if (abi_version_at_least (5)
2018 && TREE_CODE (target) == FUNCTION_TYPE)
2019 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2020 write_type (target);
2022 break;
2024 case TEMPLATE_TYPE_PARM:
2025 if (is_auto (type))
2027 if (AUTO_IS_DECLTYPE (type))
2028 write_identifier ("Dc");
2029 else
2030 write_identifier ("Da");
2031 ++is_builtin_type;
2032 break;
2034 /* else fall through. */
2035 case TEMPLATE_PARM_INDEX:
2036 write_template_param (type);
2037 break;
2039 case TEMPLATE_TEMPLATE_PARM:
2040 write_template_template_param (type);
2041 break;
2043 case BOUND_TEMPLATE_TEMPLATE_PARM:
2044 write_template_template_param (type);
2045 write_template_args
2046 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2047 break;
2049 case VECTOR_TYPE:
2050 if (abi_version_at_least (4))
2052 write_string ("Dv");
2053 /* Non-constant vector size would be encoded with
2054 _ expression, but we don't support that yet. */
2055 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2056 write_char ('_');
2058 else
2060 G.need_abi_warning = 1;
2061 write_string ("U8__vector");
2063 write_type (TREE_TYPE (type));
2064 break;
2066 case TYPE_PACK_EXPANSION:
2067 write_string ("Dp");
2068 write_type (PACK_EXPANSION_PATTERN (type));
2069 break;
2071 case DECLTYPE_TYPE:
2072 /* These shouldn't make it into mangling. */
2073 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2074 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2076 /* In ABI <5, we stripped decltype of a plain decl. */
2077 if (!abi_version_at_least (5)
2078 && DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2080 tree expr = DECLTYPE_TYPE_EXPR (type);
2081 tree etype = NULL_TREE;
2082 switch (TREE_CODE (expr))
2084 case VAR_DECL:
2085 case PARM_DECL:
2086 case RESULT_DECL:
2087 case FUNCTION_DECL:
2088 case CONST_DECL:
2089 case TEMPLATE_PARM_INDEX:
2090 etype = TREE_TYPE (expr);
2091 break;
2093 default:
2094 break;
2097 if (etype && !type_uses_auto (etype))
2099 G.need_abi_warning = 1;
2100 write_type (etype);
2101 return;
2105 write_char ('D');
2106 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2107 write_char ('t');
2108 else
2109 write_char ('T');
2110 ++cp_unevaluated_operand;
2111 write_expression (DECLTYPE_TYPE_EXPR (type));
2112 --cp_unevaluated_operand;
2113 write_char ('E');
2114 break;
2116 case NULLPTR_TYPE:
2117 write_string ("Dn");
2118 if (abi_version_at_least (7))
2119 ++is_builtin_type;
2120 break;
2122 case TYPEOF_TYPE:
2123 sorry ("mangling typeof, use decltype instead");
2124 break;
2126 case UNDERLYING_TYPE:
2127 sorry ("mangling __underlying_type");
2128 break;
2130 case LANG_TYPE:
2131 /* fall through. */
2133 default:
2134 gcc_unreachable ();
2139 /* Types other than builtin types are substitution candidates. */
2140 if (!is_builtin_type)
2141 add_substitution (type);
2144 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2145 CV-qualifiers written for TYPE.
2147 <CV-qualifiers> ::= [r] [V] [K] */
2149 static int
2150 write_CV_qualifiers_for_type (const tree type)
2152 int num_qualifiers = 0;
2154 /* The order is specified by:
2156 "In cases where multiple order-insensitive qualifiers are
2157 present, they should be ordered 'K' (closest to the base type),
2158 'V', 'r', and 'U' (farthest from the base type) ..."
2160 Note that we do not use cp_type_quals below; given "const
2161 int[3]", the "const" is emitted with the "int", not with the
2162 array. */
2163 cp_cv_quals quals = TYPE_QUALS (type);
2165 if (quals & TYPE_QUAL_RESTRICT)
2167 write_char ('r');
2168 ++num_qualifiers;
2170 if (quals & TYPE_QUAL_VOLATILE)
2172 write_char ('V');
2173 ++num_qualifiers;
2175 if (quals & TYPE_QUAL_CONST)
2177 write_char ('K');
2178 ++num_qualifiers;
2181 return num_qualifiers;
2184 /* Non-terminal <builtin-type>.
2186 <builtin-type> ::= v # void
2187 ::= b # bool
2188 ::= w # wchar_t
2189 ::= c # char
2190 ::= a # signed char
2191 ::= h # unsigned char
2192 ::= s # short
2193 ::= t # unsigned short
2194 ::= i # int
2195 ::= j # unsigned int
2196 ::= l # long
2197 ::= m # unsigned long
2198 ::= x # long long, __int64
2199 ::= y # unsigned long long, __int64
2200 ::= n # __int128
2201 ::= o # unsigned __int128
2202 ::= f # float
2203 ::= d # double
2204 ::= e # long double, __float80
2205 ::= g # __float128 [not supported]
2206 ::= u <source-name> # vendor extended type */
2208 static void
2209 write_builtin_type (tree type)
2211 if (TYPE_CANONICAL (type))
2212 type = TYPE_CANONICAL (type);
2214 switch (TREE_CODE (type))
2216 case VOID_TYPE:
2217 write_char ('v');
2218 break;
2220 case BOOLEAN_TYPE:
2221 write_char ('b');
2222 break;
2224 case INTEGER_TYPE:
2225 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2226 isn't in integer_type_nodes. */
2227 if (type == wchar_type_node)
2228 write_char ('w');
2229 else if (type == char16_type_node)
2230 write_string ("Ds");
2231 else if (type == char32_type_node)
2232 write_string ("Di");
2233 else if (TYPE_FOR_JAVA (type))
2234 write_java_integer_type_codes (type);
2235 else
2237 size_t itk;
2238 /* Assume TYPE is one of the shared integer type nodes. Find
2239 it in the array of these nodes. */
2240 iagain:
2241 for (itk = 0; itk < itk_none; ++itk)
2242 if (integer_types[itk] != NULL_TREE
2243 && type == integer_types[itk])
2245 /* Print the corresponding single-letter code. */
2246 write_char (integer_type_codes[itk]);
2247 break;
2250 if (itk == itk_none)
2252 tree t = c_common_type_for_mode (TYPE_MODE (type),
2253 TYPE_UNSIGNED (type));
2254 if (type != t)
2256 type = t;
2257 goto iagain;
2260 if (TYPE_PRECISION (type) == 128)
2261 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2262 else
2264 /* Allow for cases where TYPE is not one of the shared
2265 integer type nodes and write a "vendor extended builtin
2266 type" with a name the form intN or uintN, respectively.
2267 Situations like this can happen if you have an
2268 __attribute__((__mode__(__SI__))) type and use exotic
2269 switches like '-mint8' on AVR. Of course, this is
2270 undefined by the C++ ABI (and '-mint8' is not even
2271 Standard C conforming), but when using such special
2272 options you're pretty much in nowhere land anyway. */
2273 const char *prefix;
2274 char prec[11]; /* up to ten digits for an unsigned */
2276 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2277 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2278 write_char ('u'); /* "vendor extended builtin type" */
2279 write_unsigned_number (strlen (prefix) + strlen (prec));
2280 write_string (prefix);
2281 write_string (prec);
2285 break;
2287 case REAL_TYPE:
2288 if (type == float_type_node
2289 || type == java_float_type_node)
2290 write_char ('f');
2291 else if (type == double_type_node
2292 || type == java_double_type_node)
2293 write_char ('d');
2294 else if (type == long_double_type_node)
2295 write_char ('e');
2296 else if (type == dfloat32_type_node)
2297 write_string ("Df");
2298 else if (type == dfloat64_type_node)
2299 write_string ("Dd");
2300 else if (type == dfloat128_type_node)
2301 write_string ("De");
2302 else
2303 gcc_unreachable ();
2304 break;
2306 case FIXED_POINT_TYPE:
2307 write_string ("DF");
2308 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2309 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2310 if (type == fract_type_node
2311 || type == sat_fract_type_node
2312 || type == accum_type_node
2313 || type == sat_accum_type_node)
2314 write_char ('i');
2315 else if (type == unsigned_fract_type_node
2316 || type == sat_unsigned_fract_type_node
2317 || type == unsigned_accum_type_node
2318 || type == sat_unsigned_accum_type_node)
2319 write_char ('j');
2320 else if (type == short_fract_type_node
2321 || type == sat_short_fract_type_node
2322 || type == short_accum_type_node
2323 || type == sat_short_accum_type_node)
2324 write_char ('s');
2325 else if (type == unsigned_short_fract_type_node
2326 || type == sat_unsigned_short_fract_type_node
2327 || type == unsigned_short_accum_type_node
2328 || type == sat_unsigned_short_accum_type_node)
2329 write_char ('t');
2330 else if (type == long_fract_type_node
2331 || type == sat_long_fract_type_node
2332 || type == long_accum_type_node
2333 || type == sat_long_accum_type_node)
2334 write_char ('l');
2335 else if (type == unsigned_long_fract_type_node
2336 || type == sat_unsigned_long_fract_type_node
2337 || type == unsigned_long_accum_type_node
2338 || type == sat_unsigned_long_accum_type_node)
2339 write_char ('m');
2340 else if (type == long_long_fract_type_node
2341 || type == sat_long_long_fract_type_node
2342 || type == long_long_accum_type_node
2343 || type == sat_long_long_accum_type_node)
2344 write_char ('x');
2345 else if (type == unsigned_long_long_fract_type_node
2346 || type == sat_unsigned_long_long_fract_type_node
2347 || type == unsigned_long_long_accum_type_node
2348 || type == sat_unsigned_long_long_accum_type_node)
2349 write_char ('y');
2350 else
2351 sorry ("mangling unknown fixed point type");
2352 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2353 if (TYPE_SATURATING (type))
2354 write_char ('s');
2355 else
2356 write_char ('n');
2357 break;
2359 default:
2360 gcc_unreachable ();
2364 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2365 METHOD_TYPE. The return type is mangled before the parameter
2366 types.
2368 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2370 static void
2371 write_function_type (const tree type)
2373 MANGLE_TRACE_TREE ("function-type", type);
2375 /* For a pointer to member function, the function type may have
2376 cv-qualifiers, indicating the quals for the artificial 'this'
2377 parameter. */
2378 if (TREE_CODE (type) == METHOD_TYPE)
2380 /* The first parameter must be a POINTER_TYPE pointing to the
2381 `this' parameter. */
2382 tree this_type = class_of_this_parm (type);
2383 write_CV_qualifiers_for_type (this_type);
2386 write_char ('F');
2387 /* We don't track whether or not a type is `extern "C"'. Note that
2388 you can have an `extern "C"' function that does not have
2389 `extern "C"' type, and vice versa:
2391 extern "C" typedef void function_t();
2392 function_t f; // f has C++ linkage, but its type is
2393 // `extern "C"'
2395 typedef void function_t();
2396 extern "C" function_t f; // Vice versa.
2398 See [dcl.link]. */
2399 write_bare_function_type (type, /*include_return_type_p=*/1,
2400 /*decl=*/NULL);
2401 if (FUNCTION_REF_QUALIFIED (type))
2403 if (FUNCTION_RVALUE_QUALIFIED (type))
2404 write_char ('O');
2405 else
2406 write_char ('R');
2408 write_char ('E');
2411 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2412 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2413 is mangled before the parameter types. If non-NULL, DECL is
2414 FUNCTION_DECL for the function whose type is being emitted.
2416 If DECL is a member of a Java type, then a literal 'J'
2417 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2418 were nonzero.
2420 <bare-function-type> ::= [J]</signature/ type>+ */
2422 static void
2423 write_bare_function_type (const tree type, const int include_return_type_p,
2424 const tree decl)
2426 int java_method_p;
2428 MANGLE_TRACE_TREE ("bare-function-type", type);
2430 /* Detect Java methods and emit special encoding. */
2431 if (decl != NULL
2432 && DECL_FUNCTION_MEMBER_P (decl)
2433 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2434 && !DECL_CONSTRUCTOR_P (decl)
2435 && !DECL_DESTRUCTOR_P (decl)
2436 && !DECL_CONV_FN_P (decl))
2438 java_method_p = 1;
2439 write_char ('J');
2441 else
2443 java_method_p = 0;
2446 /* Mangle the return type, if requested. */
2447 if (include_return_type_p || java_method_p)
2448 write_type (TREE_TYPE (type));
2450 /* Now mangle the types of the arguments. */
2451 ++G.parm_depth;
2452 write_method_parms (TYPE_ARG_TYPES (type),
2453 TREE_CODE (type) == METHOD_TYPE,
2454 decl);
2455 --G.parm_depth;
2458 /* Write the mangled representation of a method parameter list of
2459 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2460 considered a non-static method, and the this parameter is omitted.
2461 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2462 parameters are being emitted. */
2464 static void
2465 write_method_parms (tree parm_types, const int method_p, const tree decl)
2467 tree first_parm_type;
2468 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2470 /* Assume this parameter type list is variable-length. If it ends
2471 with a void type, then it's not. */
2472 int varargs_p = 1;
2474 /* If this is a member function, skip the first arg, which is the
2475 this pointer.
2476 "Member functions do not encode the type of their implicit this
2477 parameter."
2479 Similarly, there's no need to mangle artificial parameters, like
2480 the VTT parameters for constructors and destructors. */
2481 if (method_p)
2483 parm_types = TREE_CHAIN (parm_types);
2484 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2486 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2488 parm_types = TREE_CHAIN (parm_types);
2489 parm_decl = DECL_CHAIN (parm_decl);
2493 for (first_parm_type = parm_types;
2494 parm_types;
2495 parm_types = TREE_CHAIN (parm_types))
2497 tree parm = TREE_VALUE (parm_types);
2498 if (parm == void_type_node)
2500 /* "Empty parameter lists, whether declared as () or
2501 conventionally as (void), are encoded with a void parameter
2502 (v)." */
2503 if (parm_types == first_parm_type)
2504 write_type (parm);
2505 /* If the parm list is terminated with a void type, it's
2506 fixed-length. */
2507 varargs_p = 0;
2508 /* A void type better be the last one. */
2509 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2511 else
2512 write_type (parm);
2515 if (varargs_p)
2516 /* <builtin-type> ::= z # ellipsis */
2517 write_char ('z');
2520 /* <class-enum-type> ::= <name> */
2522 static void
2523 write_class_enum_type (const tree type)
2525 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2528 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2529 arguments.
2531 <template-args> ::= I <template-arg>* E */
2533 static void
2534 write_template_args (tree args)
2536 int i;
2537 int length = 0;
2539 MANGLE_TRACE_TREE ("template-args", args);
2541 write_char ('I');
2543 if (args)
2544 length = TREE_VEC_LENGTH (args);
2546 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2548 /* We have nested template args. We want the innermost template
2549 argument list. */
2550 args = TREE_VEC_ELT (args, length - 1);
2551 length = TREE_VEC_LENGTH (args);
2553 for (i = 0; i < length; ++i)
2554 write_template_arg (TREE_VEC_ELT (args, i));
2556 write_char ('E');
2559 /* Write out the
2560 <unqualified-name>
2561 <unqualified-name> <template-args>
2562 part of SCOPE_REF or COMPONENT_REF mangling. */
2564 static void
2565 write_member_name (tree member)
2567 if (identifier_p (member))
2568 write_unqualified_id (member);
2569 else if (DECL_P (member))
2570 write_unqualified_name (member);
2571 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2573 tree name = TREE_OPERAND (member, 0);
2574 if (TREE_CODE (name) == OVERLOAD)
2575 name = OVL_FUNCTION (name);
2576 write_member_name (name);
2577 write_template_args (TREE_OPERAND (member, 1));
2579 else
2580 write_expression (member);
2583 /* <expression> ::= <unary operator-name> <expression>
2584 ::= <binary operator-name> <expression> <expression>
2585 ::= <expr-primary>
2587 <expr-primary> ::= <template-param>
2588 ::= L <type> <value number> E # literal
2589 ::= L <mangled-name> E # external name
2590 ::= st <type> # sizeof
2591 ::= sr <type> <unqualified-name> # dependent name
2592 ::= sr <type> <unqualified-name> <template-args> */
2594 static void
2595 write_expression (tree expr)
2597 enum tree_code code = TREE_CODE (expr);
2599 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2600 is converted (via qualification conversions) to another
2601 type. */
2602 while (TREE_CODE (expr) == NOP_EXPR
2603 /* Parentheses aren't mangled. */
2604 || code == PAREN_EXPR
2605 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2607 expr = TREE_OPERAND (expr, 0);
2608 code = TREE_CODE (expr);
2611 if (code == BASELINK
2612 && (!type_unknown_p (expr)
2613 || !BASELINK_QUALIFIED_P (expr)))
2615 expr = BASELINK_FUNCTIONS (expr);
2616 code = TREE_CODE (expr);
2619 /* Handle pointers-to-members by making them look like expression
2620 nodes. */
2621 if (code == PTRMEM_CST)
2623 expr = build_nt (ADDR_EXPR,
2624 build_qualified_name (/*type=*/NULL_TREE,
2625 PTRMEM_CST_CLASS (expr),
2626 PTRMEM_CST_MEMBER (expr),
2627 /*template_p=*/false));
2628 code = TREE_CODE (expr);
2631 /* Handle template parameters. */
2632 if (code == TEMPLATE_TYPE_PARM
2633 || code == TEMPLATE_TEMPLATE_PARM
2634 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2635 || code == TEMPLATE_PARM_INDEX)
2636 write_template_param (expr);
2637 /* Handle literals. */
2638 else if (TREE_CODE_CLASS (code) == tcc_constant
2639 || (abi_version_at_least (2) && code == CONST_DECL))
2640 write_template_arg_literal (expr);
2641 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2643 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2644 write_string ("fpT");
2646 else if (code == PARM_DECL)
2648 /* A function parameter used in a late-specified return type. */
2649 int index = DECL_PARM_INDEX (expr);
2650 int level = DECL_PARM_LEVEL (expr);
2651 int delta = G.parm_depth - level + 1;
2652 gcc_assert (index >= 1);
2653 write_char ('f');
2654 if (delta != 0)
2656 if (abi_version_at_least (5))
2658 /* Let L be the number of function prototype scopes from the
2659 innermost one (in which the parameter reference occurs) up
2660 to (and including) the one containing the declaration of
2661 the referenced parameter. If the parameter declaration
2662 clause of the innermost function prototype scope has been
2663 completely seen, it is not counted (in that case -- which
2664 is perhaps the most common -- L can be zero). */
2665 write_char ('L');
2666 write_unsigned_number (delta - 1);
2668 else
2669 G.need_abi_warning = true;
2671 write_char ('p');
2672 write_compact_number (index - 1);
2674 else if (DECL_P (expr))
2676 /* G++ 3.2 incorrectly mangled non-type template arguments of
2677 enumeration type using their names. */
2678 if (code == CONST_DECL)
2679 G.need_abi_warning = 1;
2680 write_char ('L');
2681 write_mangled_name (expr, false);
2682 write_char ('E');
2684 else if (TREE_CODE (expr) == SIZEOF_EXPR
2685 && SIZEOF_EXPR_TYPE_P (expr))
2687 write_string ("st");
2688 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2690 else if (TREE_CODE (expr) == SIZEOF_EXPR
2691 && TYPE_P (TREE_OPERAND (expr, 0)))
2693 write_string ("st");
2694 write_type (TREE_OPERAND (expr, 0));
2696 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2697 && TYPE_P (TREE_OPERAND (expr, 0)))
2699 write_string ("at");
2700 write_type (TREE_OPERAND (expr, 0));
2702 else if (code == SCOPE_REF
2703 || code == BASELINK)
2705 tree scope, member;
2706 if (code == SCOPE_REF)
2708 scope = TREE_OPERAND (expr, 0);
2709 member = TREE_OPERAND (expr, 1);
2711 else
2713 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2714 member = BASELINK_FUNCTIONS (expr);
2717 if (!abi_version_at_least (2) && DECL_P (member))
2719 write_string ("sr");
2720 write_type (scope);
2721 /* G++ 3.2 incorrectly put out both the "sr" code and
2722 the nested name of the qualified name. */
2723 G.need_abi_warning = 1;
2724 write_encoding (member);
2727 /* If the MEMBER is a real declaration, then the qualifying
2728 scope was not dependent. Ideally, we would not have a
2729 SCOPE_REF in those cases, but sometimes we do. If the second
2730 argument is a DECL, then the name must not have been
2731 dependent. */
2732 else if (DECL_P (member))
2733 write_expression (member);
2734 else
2736 write_string ("sr");
2737 write_type (scope);
2738 write_member_name (member);
2741 else if (INDIRECT_REF_P (expr)
2742 && TREE_TYPE (TREE_OPERAND (expr, 0))
2743 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2745 write_expression (TREE_OPERAND (expr, 0));
2747 else if (identifier_p (expr))
2749 /* An operator name appearing as a dependent name needs to be
2750 specially marked to disambiguate between a use of the operator
2751 name and a use of the operator in an expression. */
2752 if (IDENTIFIER_OPNAME_P (expr))
2753 write_string ("on");
2754 write_unqualified_id (expr);
2756 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2758 tree fn = TREE_OPERAND (expr, 0);
2759 if (is_overloaded_fn (fn))
2760 fn = DECL_NAME (get_first_fn (fn));
2761 if (IDENTIFIER_OPNAME_P (fn))
2762 write_string ("on");
2763 write_unqualified_id (fn);
2764 write_template_args (TREE_OPERAND (expr, 1));
2766 else if (TREE_CODE (expr) == MODOP_EXPR)
2768 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2769 const char *name = (assignment_operator_name_info[(int) subop]
2770 .mangled_name);
2771 write_string (name);
2772 write_expression (TREE_OPERAND (expr, 0));
2773 write_expression (TREE_OPERAND (expr, 2));
2775 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2777 /* ::= [gs] nw <expression>* _ <type> E
2778 ::= [gs] nw <expression>* _ <type> <initializer>
2779 ::= [gs] na <expression>* _ <type> E
2780 ::= [gs] na <expression>* _ <type> <initializer>
2781 <initializer> ::= pi <expression>* E */
2782 tree placement = TREE_OPERAND (expr, 0);
2783 tree type = TREE_OPERAND (expr, 1);
2784 tree nelts = TREE_OPERAND (expr, 2);
2785 tree init = TREE_OPERAND (expr, 3);
2786 tree t;
2788 gcc_assert (code == NEW_EXPR);
2789 if (TREE_OPERAND (expr, 2))
2790 code = VEC_NEW_EXPR;
2792 if (NEW_EXPR_USE_GLOBAL (expr))
2793 write_string ("gs");
2795 write_string (operator_name_info[(int) code].mangled_name);
2797 for (t = placement; t; t = TREE_CHAIN (t))
2798 write_expression (TREE_VALUE (t));
2800 write_char ('_');
2802 if (nelts)
2804 tree domain;
2805 ++processing_template_decl;
2806 domain = compute_array_index_type (NULL_TREE, nelts,
2807 tf_warning_or_error);
2808 type = build_cplus_array_type (type, domain);
2809 --processing_template_decl;
2811 write_type (type);
2813 if (init && TREE_CODE (init) == TREE_LIST
2814 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
2815 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
2816 write_expression (TREE_VALUE (init));
2817 else
2819 if (init)
2820 write_string ("pi");
2821 if (init && init != void_zero_node)
2822 for (t = init; t; t = TREE_CHAIN (t))
2823 write_expression (TREE_VALUE (t));
2824 write_char ('E');
2827 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2829 gcc_assert (code == DELETE_EXPR);
2830 if (DELETE_EXPR_USE_VEC (expr))
2831 code = VEC_DELETE_EXPR;
2833 if (DELETE_EXPR_USE_GLOBAL (expr))
2834 write_string ("gs");
2836 write_string (operator_name_info[(int) code].mangled_name);
2838 write_expression (TREE_OPERAND (expr, 0));
2840 else if (code == THROW_EXPR)
2842 tree op = TREE_OPERAND (expr, 0);
2843 if (op)
2845 write_string ("tw");
2846 write_expression (op);
2848 else
2849 write_string ("tr");
2851 else if (code == CONSTRUCTOR)
2853 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2854 unsigned i; tree val;
2856 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2857 write_string ("il");
2858 else
2860 write_string ("tl");
2861 write_type (TREE_TYPE (expr));
2863 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2864 write_expression (val);
2865 write_char ('E');
2867 else if (dependent_name (expr))
2869 write_unqualified_id (dependent_name (expr));
2871 else
2873 int i, len;
2874 const char *name;
2876 /* When we bind a variable or function to a non-type template
2877 argument with reference type, we create an ADDR_EXPR to show
2878 the fact that the entity's address has been taken. But, we
2879 don't actually want to output a mangling code for the `&'. */
2880 if (TREE_CODE (expr) == ADDR_EXPR
2881 && TREE_TYPE (expr)
2882 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2884 expr = TREE_OPERAND (expr, 0);
2885 if (DECL_P (expr))
2887 write_expression (expr);
2888 return;
2891 code = TREE_CODE (expr);
2894 if (code == COMPONENT_REF)
2896 tree ob = TREE_OPERAND (expr, 0);
2898 if (TREE_CODE (ob) == ARROW_EXPR)
2900 write_string (operator_name_info[(int)code].mangled_name);
2901 ob = TREE_OPERAND (ob, 0);
2903 else
2904 write_string ("dt");
2906 write_expression (ob);
2907 write_member_name (TREE_OPERAND (expr, 1));
2908 return;
2911 /* If it wasn't any of those, recursively expand the expression. */
2912 name = operator_name_info[(int) code].mangled_name;
2914 /* We used to mangle const_cast and static_cast like a C cast. */
2915 if (!abi_version_at_least (6)
2916 && (code == CONST_CAST_EXPR
2917 || code == STATIC_CAST_EXPR))
2919 name = operator_name_info[CAST_EXPR].mangled_name;
2920 G.need_abi_warning = 1;
2923 if (name == NULL)
2925 switch (code)
2927 case TRAIT_EXPR:
2928 error ("use of built-in trait %qE in function signature; "
2929 "use library traits instead", expr);
2930 break;
2932 default:
2933 sorry ("mangling %C", code);
2934 break;
2936 return;
2938 else
2939 write_string (name);
2941 switch (code)
2943 case CALL_EXPR:
2945 tree fn = CALL_EXPR_FN (expr);
2947 if (TREE_CODE (fn) == ADDR_EXPR)
2948 fn = TREE_OPERAND (fn, 0);
2950 /* Mangle a dependent name as the name, not whatever happens to
2951 be the first function in the overload set. */
2952 if ((TREE_CODE (fn) == FUNCTION_DECL
2953 || TREE_CODE (fn) == OVERLOAD)
2954 && type_dependent_expression_p_push (expr))
2955 fn = DECL_NAME (get_first_fn (fn));
2957 write_expression (fn);
2960 for (i = 0; i < call_expr_nargs (expr); ++i)
2961 write_expression (CALL_EXPR_ARG (expr, i));
2962 write_char ('E');
2963 break;
2965 case CAST_EXPR:
2966 write_type (TREE_TYPE (expr));
2967 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2968 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2969 else
2971 tree args = TREE_OPERAND (expr, 0);
2972 write_char ('_');
2973 for (; args; args = TREE_CHAIN (args))
2974 write_expression (TREE_VALUE (args));
2975 write_char ('E');
2977 break;
2979 case DYNAMIC_CAST_EXPR:
2980 case REINTERPRET_CAST_EXPR:
2981 case STATIC_CAST_EXPR:
2982 case CONST_CAST_EXPR:
2983 write_type (TREE_TYPE (expr));
2984 write_expression (TREE_OPERAND (expr, 0));
2985 break;
2987 case PREINCREMENT_EXPR:
2988 case PREDECREMENT_EXPR:
2989 if (abi_version_at_least (6))
2990 write_char ('_');
2991 else
2992 G.need_abi_warning = 1;
2993 /* Fall through. */
2995 default:
2996 /* In the middle-end, some expressions have more operands than
2997 they do in templates (and mangling). */
2998 len = cp_tree_operand_length (expr);
3000 for (i = 0; i < len; ++i)
3002 tree operand = TREE_OPERAND (expr, i);
3003 /* As a GNU extension, the middle operand of a
3004 conditional may be omitted. Since expression
3005 manglings are supposed to represent the input token
3006 stream, there's no good way to mangle such an
3007 expression without extending the C++ ABI. */
3008 if (code == COND_EXPR && i == 1 && !operand)
3010 error ("omitted middle operand to %<?:%> operand "
3011 "cannot be mangled");
3012 continue;
3014 write_expression (operand);
3020 /* Literal subcase of non-terminal <template-arg>.
3022 "Literal arguments, e.g. "A<42L>", are encoded with their type
3023 and value. Negative integer values are preceded with "n"; for
3024 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3025 encoded as 0, true as 1." */
3027 static void
3028 write_template_arg_literal (const tree value)
3030 write_char ('L');
3031 write_type (TREE_TYPE (value));
3033 /* Write a null member pointer value as (type)0, regardless of its
3034 real representation. */
3035 if (null_member_pointer_value_p (value))
3036 write_integer_cst (integer_zero_node);
3037 else
3038 switch (TREE_CODE (value))
3040 case CONST_DECL:
3041 write_integer_cst (DECL_INITIAL (value));
3042 break;
3044 case INTEGER_CST:
3045 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3046 || integer_zerop (value) || integer_onep (value));
3047 write_integer_cst (value);
3048 break;
3050 case REAL_CST:
3051 write_real_cst (value);
3052 break;
3054 case COMPLEX_CST:
3055 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3056 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3058 write_integer_cst (TREE_REALPART (value));
3059 write_char ('_');
3060 write_integer_cst (TREE_IMAGPART (value));
3062 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3063 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3065 write_real_cst (TREE_REALPART (value));
3066 write_char ('_');
3067 write_real_cst (TREE_IMAGPART (value));
3069 else
3070 gcc_unreachable ();
3071 break;
3073 case STRING_CST:
3074 sorry ("string literal in function template signature");
3075 break;
3077 default:
3078 gcc_unreachable ();
3081 write_char ('E');
3084 /* Non-terminal <template-arg>.
3086 <template-arg> ::= <type> # type
3087 ::= L <type> </value/ number> E # literal
3088 ::= LZ <name> E # external name
3089 ::= X <expression> E # expression */
3091 static void
3092 write_template_arg (tree node)
3094 enum tree_code code = TREE_CODE (node);
3096 MANGLE_TRACE_TREE ("template-arg", node);
3098 /* A template template parameter's argument list contains TREE_LIST
3099 nodes of which the value field is the actual argument. */
3100 if (code == TREE_LIST)
3102 node = TREE_VALUE (node);
3103 /* If it's a decl, deal with its type instead. */
3104 if (DECL_P (node))
3106 node = TREE_TYPE (node);
3107 code = TREE_CODE (node);
3111 if (TREE_CODE (node) == NOP_EXPR
3112 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3114 /* Template parameters can be of reference type. To maintain
3115 internal consistency, such arguments use a conversion from
3116 address of object to reference type. */
3117 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3118 if (abi_version_at_least (2))
3119 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3120 else
3121 G.need_abi_warning = 1;
3124 if (TREE_CODE (node) == BASELINK
3125 && !type_unknown_p (node))
3127 if (abi_version_at_least (6))
3128 node = BASELINK_FUNCTIONS (node);
3129 else
3130 /* We wrongly wrapped a class-scope function in X/E. */
3131 G.need_abi_warning = 1;
3134 if (ARGUMENT_PACK_P (node))
3136 /* Expand the template argument pack. */
3137 tree args = ARGUMENT_PACK_ARGS (node);
3138 int i, length = TREE_VEC_LENGTH (args);
3139 if (abi_version_at_least (6))
3140 write_char ('J');
3141 else
3143 write_char ('I');
3144 G.need_abi_warning = 1;
3146 for (i = 0; i < length; ++i)
3147 write_template_arg (TREE_VEC_ELT (args, i));
3148 write_char ('E');
3150 else if (TYPE_P (node))
3151 write_type (node);
3152 else if (code == TEMPLATE_DECL)
3153 /* A template appearing as a template arg is a template template arg. */
3154 write_template_template_arg (node);
3155 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3156 || (abi_version_at_least (2) && code == CONST_DECL)
3157 || null_member_pointer_value_p (node))
3158 write_template_arg_literal (node);
3159 else if (DECL_P (node))
3161 /* Until ABI version 2, non-type template arguments of
3162 enumeration type were mangled using their names. */
3163 if (code == CONST_DECL && !abi_version_at_least (2))
3164 G.need_abi_warning = 1;
3165 write_char ('L');
3166 /* Until ABI version 3, the underscore before the mangled name
3167 was incorrectly omitted. */
3168 if (!abi_version_at_least (3))
3170 G.need_abi_warning = 1;
3171 write_char ('Z');
3173 else
3174 write_string ("_Z");
3175 write_encoding (node);
3176 write_char ('E');
3178 else
3180 /* Template arguments may be expressions. */
3181 write_char ('X');
3182 write_expression (node);
3183 write_char ('E');
3187 /* <template-template-arg>
3188 ::= <name>
3189 ::= <substitution> */
3191 static void
3192 write_template_template_arg (const tree decl)
3194 MANGLE_TRACE_TREE ("template-template-arg", decl);
3196 if (find_substitution (decl))
3197 return;
3198 write_name (decl, /*ignore_local_scope=*/0);
3199 add_substitution (decl);
3203 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3205 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3206 ::= A <expression> _ </element/ type>
3208 "Array types encode the dimension (number of elements) and the
3209 element type. For variable length arrays, the dimension (but not
3210 the '_' separator) is omitted." */
3212 static void
3213 write_array_type (const tree type)
3215 write_char ('A');
3216 if (TYPE_DOMAIN (type))
3218 tree index_type;
3219 tree max;
3221 index_type = TYPE_DOMAIN (type);
3222 /* The INDEX_TYPE gives the upper and lower bounds of the
3223 array. */
3224 max = TYPE_MAX_VALUE (index_type);
3225 if (TREE_CODE (max) == INTEGER_CST)
3227 /* The ABI specifies that we should mangle the number of
3228 elements in the array, not the largest allowed index. */
3229 double_int dmax = tree_to_double_int (max) + double_int_one;
3230 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3231 number of elements as zero. */
3232 dmax = dmax.zext (TYPE_PRECISION (TREE_TYPE (max)));
3233 gcc_assert (dmax.fits_uhwi ());
3234 write_unsigned_number (dmax.low);
3236 else
3238 max = TREE_OPERAND (max, 0);
3239 if (!abi_version_at_least (2))
3241 /* value_dependent_expression_p presumes nothing is
3242 dependent when PROCESSING_TEMPLATE_DECL is zero. */
3243 ++processing_template_decl;
3244 if (!value_dependent_expression_p (max))
3245 G.need_abi_warning = 1;
3246 --processing_template_decl;
3248 write_expression (max);
3252 write_char ('_');
3253 write_type (TREE_TYPE (type));
3256 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3257 variables. TYPE is a pointer-to-member POINTER_TYPE.
3259 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3261 static void
3262 write_pointer_to_member_type (const tree type)
3264 write_char ('M');
3265 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3266 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3269 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3270 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3271 TEMPLATE_PARM_INDEX.
3273 <template-param> ::= T </parameter/ number> _ */
3275 static void
3276 write_template_param (const tree parm)
3278 int parm_index;
3280 MANGLE_TRACE_TREE ("template-parm", parm);
3282 switch (TREE_CODE (parm))
3284 case TEMPLATE_TYPE_PARM:
3285 case TEMPLATE_TEMPLATE_PARM:
3286 case BOUND_TEMPLATE_TEMPLATE_PARM:
3287 parm_index = TEMPLATE_TYPE_IDX (parm);
3288 break;
3290 case TEMPLATE_PARM_INDEX:
3291 parm_index = TEMPLATE_PARM_IDX (parm);
3292 break;
3294 default:
3295 gcc_unreachable ();
3298 write_char ('T');
3299 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3300 earliest template param denoted by `_'. */
3301 write_compact_number (parm_index);
3304 /* <template-template-param>
3305 ::= <template-param>
3306 ::= <substitution> */
3308 static void
3309 write_template_template_param (const tree parm)
3311 tree templ = NULL_TREE;
3313 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3314 template template parameter. The substitution candidate here is
3315 only the template. */
3316 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3318 templ
3319 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3320 if (find_substitution (templ))
3321 return;
3324 /* <template-param> encodes only the template parameter position,
3325 not its template arguments, which is fine here. */
3326 write_template_param (parm);
3327 if (templ)
3328 add_substitution (templ);
3331 /* Non-terminal <substitution>.
3333 <substitution> ::= S <seq-id> _
3334 ::= S_ */
3336 static void
3337 write_substitution (const int seq_id)
3339 MANGLE_TRACE ("substitution", "");
3341 write_char ('S');
3342 if (seq_id > 0)
3343 write_number (seq_id - 1, /*unsigned=*/1, 36);
3344 write_char ('_');
3347 /* Start mangling ENTITY. */
3349 static inline void
3350 start_mangling (const tree entity)
3352 G.entity = entity;
3353 G.need_abi_warning = false;
3354 obstack_free (&name_obstack, name_base);
3355 mangle_obstack = &name_obstack;
3356 name_base = obstack_alloc (&name_obstack, 0);
3359 /* Done with mangling. If WARN is true, and the name of G.entity will
3360 be mangled differently in a future version of the ABI, issue a
3361 warning. */
3363 static void
3364 finish_mangling_internal (const bool warn)
3366 if (warn_abi && warn && G.need_abi_warning)
3367 warning (OPT_Wabi, "the mangled name of %qD will change in a future "
3368 "version of GCC",
3369 G.entity);
3371 /* Clear all the substitutions. */
3372 vec_safe_truncate (G.substitutions, 0);
3374 /* Null-terminate the string. */
3375 write_char ('\0');
3379 /* Like finish_mangling_internal, but return the mangled string. */
3381 static inline const char *
3382 finish_mangling (const bool warn)
3384 finish_mangling_internal (warn);
3385 return (const char *) obstack_finish (mangle_obstack);
3388 /* Like finish_mangling_internal, but return an identifier. */
3390 static tree
3391 finish_mangling_get_identifier (const bool warn)
3393 finish_mangling_internal (warn);
3394 /* Don't obstack_finish here, and the next start_mangling will
3395 remove the identifier. */
3396 return get_identifier ((const char *) obstack_base (mangle_obstack));
3399 /* Initialize data structures for mangling. */
3401 void
3402 init_mangle (void)
3404 gcc_obstack_init (&name_obstack);
3405 name_base = obstack_alloc (&name_obstack, 0);
3406 vec_alloc (G.substitutions, 0);
3408 /* Cache these identifiers for quick comparison when checking for
3409 standard substitutions. */
3410 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3411 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3412 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3413 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3414 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3415 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3418 /* Generate the mangled name of DECL. */
3420 static tree
3421 mangle_decl_string (const tree decl)
3423 tree result;
3424 location_t saved_loc = input_location;
3425 tree saved_fn = NULL_TREE;
3426 bool template_p = false;
3428 /* We shouldn't be trying to mangle an uninstantiated template. */
3429 gcc_assert (!type_dependent_expression_p (decl));
3431 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3433 struct tinst_level *tl = current_instantiation ();
3434 if ((!tl || tl->decl != decl)
3435 && push_tinst_level (decl))
3437 template_p = true;
3438 saved_fn = current_function_decl;
3439 current_function_decl = NULL_TREE;
3442 input_location = DECL_SOURCE_LOCATION (decl);
3444 start_mangling (decl);
3446 if (TREE_CODE (decl) == TYPE_DECL)
3447 write_type (TREE_TYPE (decl));
3448 else
3449 write_mangled_name (decl, true);
3451 result = finish_mangling_get_identifier (/*warn=*/true);
3452 if (DEBUG_MANGLE)
3453 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3454 IDENTIFIER_POINTER (result));
3456 if (template_p)
3458 pop_tinst_level ();
3459 current_function_decl = saved_fn;
3461 input_location = saved_loc;
3463 return result;
3466 /* Return an identifier for the external mangled name of DECL. */
3468 static tree
3469 get_mangled_id (tree decl)
3471 tree id = mangle_decl_string (decl);
3472 return targetm.mangle_decl_assembler_name (decl, id);
3475 /* Create an identifier for the external mangled name of DECL. */
3477 void
3478 mangle_decl (const tree decl)
3480 tree id;
3481 bool dep;
3483 /* Don't bother mangling uninstantiated templates. */
3484 ++processing_template_decl;
3485 if (TREE_CODE (decl) == TYPE_DECL)
3486 dep = dependent_type_p (TREE_TYPE (decl));
3487 else
3488 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3489 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3490 --processing_template_decl;
3491 if (dep)
3492 return;
3494 id = get_mangled_id (decl);
3495 SET_DECL_ASSEMBLER_NAME (decl, id);
3497 if (G.need_abi_warning
3498 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3499 && TREE_CODE (decl) != TYPE_DECL
3500 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3501 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3503 #ifdef ASM_OUTPUT_DEF
3504 /* If the mangling will change in the future, emit an alias with the
3505 future mangled name for forward-compatibility. */
3506 int save_ver;
3507 tree id2, alias;
3508 #endif
3510 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3511 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3512 inform (DECL_SOURCE_LOCATION (decl), "-fabi-version=6 (or =0) "
3513 "avoids this error with a change in mangling");
3515 #ifdef ASM_OUTPUT_DEF
3516 save_ver = flag_abi_version;
3517 flag_abi_version = 0;
3518 id2 = mangle_decl_string (decl);
3519 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3520 flag_abi_version = save_ver;
3522 alias = make_alias_for (decl, id2);
3523 DECL_IGNORED_P (alias) = 1;
3524 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3525 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3526 if (vague_linkage_p (decl))
3527 DECL_WEAK (alias) = 1;
3528 if (TREE_CODE (decl) == FUNCTION_DECL)
3529 cgraph_same_body_alias (cgraph_get_create_node (decl), alias, decl);
3530 else
3531 varpool_extra_name_alias (alias, decl);
3532 #endif
3536 /* Generate the mangled representation of TYPE. */
3538 const char *
3539 mangle_type_string (const tree type)
3541 const char *result;
3543 start_mangling (type);
3544 write_type (type);
3545 result = finish_mangling (/*warn=*/false);
3546 if (DEBUG_MANGLE)
3547 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3548 return result;
3551 /* Create an identifier for the mangled name of a special component
3552 for belonging to TYPE. CODE is the ABI-specified code for this
3553 component. */
3555 static tree
3556 mangle_special_for_type (const tree type, const char *code)
3558 tree result;
3560 /* We don't have an actual decl here for the special component, so
3561 we can't just process the <encoded-name>. Instead, fake it. */
3562 start_mangling (type);
3564 /* Start the mangling. */
3565 write_string ("_Z");
3566 write_string (code);
3568 /* Add the type. */
3569 write_type (type);
3570 result = finish_mangling_get_identifier (/*warn=*/false);
3572 if (DEBUG_MANGLE)
3573 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3574 IDENTIFIER_POINTER (result));
3576 return result;
3579 /* Create an identifier for the mangled representation of the typeinfo
3580 structure for TYPE. */
3582 tree
3583 mangle_typeinfo_for_type (const tree type)
3585 return mangle_special_for_type (type, "TI");
3588 /* Create an identifier for the mangled name of the NTBS containing
3589 the mangled name of TYPE. */
3591 tree
3592 mangle_typeinfo_string_for_type (const tree type)
3594 return mangle_special_for_type (type, "TS");
3597 /* Create an identifier for the mangled name of the vtable for TYPE. */
3599 tree
3600 mangle_vtbl_for_type (const tree type)
3602 return mangle_special_for_type (type, "TV");
3605 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3607 tree
3608 mangle_vtt_for_type (const tree type)
3610 return mangle_special_for_type (type, "TT");
3613 /* Return an identifier for a construction vtable group. TYPE is
3614 the most derived class in the hierarchy; BINFO is the base
3615 subobject for which this construction vtable group will be used.
3617 This mangling isn't part of the ABI specification; in the ABI
3618 specification, the vtable group is dumped in the same COMDAT as the
3619 main vtable, and is referenced only from that vtable, so it doesn't
3620 need an external name. For binary formats without COMDAT sections,
3621 though, we need external names for the vtable groups.
3623 We use the production
3625 <special-name> ::= CT <type> <offset number> _ <base type> */
3627 tree
3628 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3630 tree result;
3632 start_mangling (type);
3634 write_string ("_Z");
3635 write_string ("TC");
3636 write_type (type);
3637 write_integer_cst (BINFO_OFFSET (binfo));
3638 write_char ('_');
3639 write_type (BINFO_TYPE (binfo));
3641 result = finish_mangling_get_identifier (/*warn=*/false);
3642 if (DEBUG_MANGLE)
3643 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3644 IDENTIFIER_POINTER (result));
3645 return result;
3648 /* Mangle a this pointer or result pointer adjustment.
3650 <call-offset> ::= h <fixed offset number> _
3651 ::= v <fixed offset number> _ <virtual offset number> _ */
3653 static void
3654 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3656 write_char (virtual_offset ? 'v' : 'h');
3658 /* For either flavor, write the fixed offset. */
3659 write_integer_cst (fixed_offset);
3660 write_char ('_');
3662 /* For a virtual thunk, add the virtual offset. */
3663 if (virtual_offset)
3665 write_integer_cst (virtual_offset);
3666 write_char ('_');
3670 /* Return an identifier for the mangled name of a this-adjusting or
3671 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3672 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3673 is a virtual thunk, and it is the vtbl offset in
3674 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3675 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3676 thunk itself. A covariant thunk name always includes the adjustment
3677 for the this pointer, even if there is none.
3679 <special-name> ::= T <call-offset> <base encoding>
3680 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3681 <base encoding> */
3683 tree
3684 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3685 tree virtual_offset)
3687 tree result;
3689 start_mangling (fn_decl);
3691 write_string ("_Z");
3692 write_char ('T');
3694 if (!this_adjusting)
3696 /* Covariant thunk with no this adjustment */
3697 write_char ('c');
3698 mangle_call_offset (integer_zero_node, NULL_TREE);
3699 mangle_call_offset (fixed_offset, virtual_offset);
3701 else if (!DECL_THUNK_P (fn_decl))
3702 /* Plain this adjusting thunk. */
3703 mangle_call_offset (fixed_offset, virtual_offset);
3704 else
3706 /* This adjusting thunk to covariant thunk. */
3707 write_char ('c');
3708 mangle_call_offset (fixed_offset, virtual_offset);
3709 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3710 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3711 if (virtual_offset)
3712 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3713 mangle_call_offset (fixed_offset, virtual_offset);
3714 fn_decl = THUNK_TARGET (fn_decl);
3717 /* Scoped name. */
3718 write_encoding (fn_decl);
3720 result = finish_mangling_get_identifier (/*warn=*/false);
3721 if (DEBUG_MANGLE)
3722 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3723 return result;
3726 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3727 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3728 TYPE. */
3730 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
3732 /* Hash a node (VAL1) in the table. */
3734 static hashval_t
3735 hash_type (const void *val)
3737 return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
3740 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3742 static int
3743 compare_type (const void *val1, const void *val2)
3745 return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
3748 /* Return an identifier for the mangled unqualified name for a
3749 conversion operator to TYPE. This mangling is not specified by the
3750 ABI spec; it is only used internally. */
3752 tree
3753 mangle_conv_op_name_for_type (const tree type)
3755 void **slot;
3756 tree identifier;
3758 if (type == error_mark_node)
3759 return error_mark_node;
3761 if (conv_type_names == NULL)
3762 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
3764 slot = htab_find_slot_with_hash (conv_type_names, type,
3765 (hashval_t) TYPE_UID (type), INSERT);
3766 identifier = (tree)*slot;
3767 if (!identifier)
3769 char buffer[64];
3771 /* Create a unique name corresponding to TYPE. */
3772 sprintf (buffer, "operator %lu",
3773 (unsigned long) htab_elements (conv_type_names));
3774 identifier = get_identifier (buffer);
3775 *slot = identifier;
3777 /* Hang TYPE off the identifier so it can be found easily later
3778 when performing conversions. */
3779 TREE_TYPE (identifier) = type;
3781 /* Set bits on the identifier so we know later it's a conversion. */
3782 IDENTIFIER_OPNAME_P (identifier) = 1;
3783 IDENTIFIER_TYPENAME_P (identifier) = 1;
3786 return identifier;
3789 /* Write out the appropriate string for this variable when generating
3790 another mangled name based on this one. */
3792 static void
3793 write_guarded_var_name (const tree variable)
3795 if (DECL_NAME (variable)
3796 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3797 /* The name of a guard variable for a reference temporary should refer
3798 to the reference, not the temporary. */
3799 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3800 else
3801 write_name (variable, /*ignore_local_scope=*/0);
3804 /* Return an identifier for the name of an initialization guard
3805 variable for indicated VARIABLE. */
3807 tree
3808 mangle_guard_variable (const tree variable)
3810 start_mangling (variable);
3811 write_string ("_ZGV");
3812 write_guarded_var_name (variable);
3813 return finish_mangling_get_identifier (/*warn=*/false);
3816 /* Return an identifier for the name of a thread_local initialization
3817 function for VARIABLE. */
3819 tree
3820 mangle_tls_init_fn (const tree variable)
3822 start_mangling (variable);
3823 write_string ("_ZTH");
3824 write_guarded_var_name (variable);
3825 return finish_mangling_get_identifier (/*warn=*/false);
3828 /* Return an identifier for the name of a thread_local wrapper
3829 function for VARIABLE. */
3831 #define TLS_WRAPPER_PREFIX "_ZTW"
3833 tree
3834 mangle_tls_wrapper_fn (const tree variable)
3836 start_mangling (variable);
3837 write_string (TLS_WRAPPER_PREFIX);
3838 write_guarded_var_name (variable);
3839 return finish_mangling_get_identifier (/*warn=*/false);
3842 /* Return true iff FN is a thread_local wrapper function. */
3844 bool
3845 decl_tls_wrapper_p (const tree fn)
3847 if (TREE_CODE (fn) != FUNCTION_DECL)
3848 return false;
3849 tree name = DECL_NAME (fn);
3850 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3851 strlen (TLS_WRAPPER_PREFIX)) == 0;
3854 /* Return an identifier for the name of a temporary variable used to
3855 initialize a static reference. This isn't part of the ABI, but we might
3856 as well call them something readable. */
3858 static GTY(()) int temp_count;
3860 tree
3861 mangle_ref_init_variable (const tree variable)
3863 start_mangling (variable);
3864 write_string ("_ZGR");
3865 write_name (variable, /*ignore_local_scope=*/0);
3866 /* Avoid name clashes with aggregate initialization of multiple
3867 references at once. */
3868 write_unsigned_number (temp_count++);
3869 return finish_mangling_get_identifier (/*warn=*/false);
3873 /* Foreign language type mangling section. */
3875 /* How to write the type codes for the integer Java type. */
3877 static void
3878 write_java_integer_type_codes (const tree type)
3880 if (type == java_int_type_node)
3881 write_char ('i');
3882 else if (type == java_short_type_node)
3883 write_char ('s');
3884 else if (type == java_byte_type_node)
3885 write_char ('c');
3886 else if (type == java_char_type_node)
3887 write_char ('w');
3888 else if (type == java_long_type_node)
3889 write_char ('x');
3890 else if (type == java_boolean_type_node)
3891 write_char ('b');
3892 else
3893 gcc_unreachable ();
3896 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3897 function generates a mangled name for the vtable map variable of
3898 the class type. For example, if the class type is
3899 "std::bad_exception", the mangled name for the class is
3900 "St13bad_exception". This function would generate the name
3901 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3902 "_VTV<std::bad_exception>::__vtable_map". */
3905 char *
3906 get_mangled_vtable_map_var_name (tree class_type)
3908 char *var_name = NULL;
3909 const char *prefix = "_ZN4_VTVI";
3910 const char *postfix = "E12__vtable_mapE";
3912 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3914 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3915 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3916 strlen (prefix) +
3917 strlen (postfix) + 1;
3919 var_name = (char *) xmalloc (len);
3921 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3923 return var_name;
3926 #include "gt-cp-mangle.h"