* xvasprintf.c: New file.
[official-gcc.git] / gcc / cp / mangle.c
blob71a6e3bf6efbb77681bc1356ce46a7d7f8d68be5
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 "tree-hasher.h"
53 #include "stor-layout.h"
54 #include "stringpool.h"
55 #include "tm_p.h"
56 #include "cp-tree.h"
57 #include "obstack.h"
58 #include "flags.h"
59 #include "target.h"
60 #include "hash-map.h"
61 #include "is-a.h"
62 #include "plugin-api.h"
63 #include "vec.h"
64 #include "hashtab.h"
65 #include "hash-set.h"
66 #include "machmode.h"
67 #include "hard-reg-set.h"
68 #include "input.h"
69 #include "function.h"
70 #include "ipa-ref.h"
71 #include "cgraph.h"
72 #include "wide-int.h"
74 /* Debugging support. */
76 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
77 #ifndef DEBUG_MANGLE
78 #define DEBUG_MANGLE 0
79 #endif
81 /* Macros for tracing the write_* functions. */
82 #if DEBUG_MANGLE
83 # define MANGLE_TRACE(FN, INPUT) \
84 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
85 # define MANGLE_TRACE_TREE(FN, NODE) \
86 fprintf (stderr, " %-24s: %-24s (%p)\n", \
87 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
88 #else
89 # define MANGLE_TRACE(FN, INPUT)
90 # define MANGLE_TRACE_TREE(FN, NODE)
91 #endif
93 /* Nonzero if NODE is a class template-id. We can't rely on
94 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
95 that hard to distinguish A<T> from A, where A<T> is the type as
96 instantiated outside of the template, and A is the type used
97 without parameters inside the template. */
98 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
99 (TYPE_LANG_SPECIFIC (NODE) != NULL \
100 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
101 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
102 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
104 /* Things we only need one of. This module is not reentrant. */
105 typedef struct GTY(()) globals {
106 /* An array of the current substitution candidates, in the order
107 we've seen them. */
108 vec<tree, va_gc> *substitutions;
110 /* The entity that is being mangled. */
111 tree GTY ((skip)) entity;
113 /* How many parameter scopes we are inside. */
114 int parm_depth;
116 /* True if the mangling will be different in a future version of the
117 ABI. */
118 bool need_abi_warning;
119 } globals;
121 static GTY (()) globals G;
123 /* The obstack on which we build mangled names. */
124 static struct obstack *mangle_obstack;
126 /* The obstack on which we build mangled names that are not going to
127 be IDENTIFIER_NODEs. */
128 static struct obstack name_obstack;
130 /* The first object on the name_obstack; we use this to free memory
131 allocated on the name_obstack. */
132 static void *name_base;
134 /* Indices into subst_identifiers. These are identifiers used in
135 special substitution rules. */
136 typedef enum
138 SUBID_ALLOCATOR,
139 SUBID_BASIC_STRING,
140 SUBID_CHAR_TRAITS,
141 SUBID_BASIC_ISTREAM,
142 SUBID_BASIC_OSTREAM,
143 SUBID_BASIC_IOSTREAM,
144 SUBID_MAX
146 substitution_identifier_index_t;
148 /* For quick substitution checks, look up these common identifiers
149 once only. */
150 static GTY(()) tree subst_identifiers[SUBID_MAX];
152 /* Single-letter codes for builtin integer types, defined in
153 <builtin-type>. These are indexed by integer_type_kind values. */
154 static const char
155 integer_type_codes[itk_none] =
157 'c', /* itk_char */
158 'a', /* itk_signed_char */
159 'h', /* itk_unsigned_char */
160 's', /* itk_short */
161 't', /* itk_unsigned_short */
162 'i', /* itk_int */
163 'j', /* itk_unsigned_int */
164 'l', /* itk_long */
165 'm', /* itk_unsigned_long */
166 'x', /* itk_long_long */
167 'y', /* itk_unsigned_long_long */
168 /* __intN types are handled separately */
169 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
172 static int decl_is_template_id (const tree, tree* const);
174 /* Functions for handling substitutions. */
176 static inline tree canonicalize_for_substitution (tree);
177 static void add_substitution (tree);
178 static inline int is_std_substitution (const tree,
179 const substitution_identifier_index_t);
180 static inline int is_std_substitution_char (const tree,
181 const substitution_identifier_index_t);
182 static int find_substitution (tree);
183 static void mangle_call_offset (const tree, const tree);
185 /* Functions for emitting mangled representations of things. */
187 static void write_mangled_name (const tree, bool);
188 static void write_encoding (const tree);
189 static void write_name (tree, const int);
190 static void write_abi_tags (tree);
191 static void write_unscoped_name (const tree);
192 static void write_unscoped_template_name (const tree);
193 static void write_nested_name (const tree);
194 static void write_prefix (const tree);
195 static void write_template_prefix (const tree);
196 static void write_unqualified_name (tree);
197 static void write_conversion_operator_name (const tree);
198 static void write_source_name (tree);
199 static void write_literal_operator_name (tree);
200 static void write_unnamed_type_name (const tree);
201 static void write_closure_type_name (const tree);
202 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
203 const unsigned int);
204 static void write_number (unsigned HOST_WIDE_INT, const int,
205 const unsigned int);
206 static void write_compact_number (int num);
207 static void write_integer_cst (const tree);
208 static void write_real_cst (const tree);
209 static void write_identifier (const char *);
210 static void write_special_name_constructor (const tree);
211 static void write_special_name_destructor (const tree);
212 static void write_type (tree);
213 static int write_CV_qualifiers_for_type (const tree);
214 static void write_builtin_type (tree);
215 static void write_function_type (const tree);
216 static void write_bare_function_type (const tree, const int, const tree);
217 static void write_method_parms (tree, const int, const tree);
218 static void write_class_enum_type (const tree);
219 static void write_template_args (tree);
220 static void write_expression (tree);
221 static void write_template_arg_literal (const tree);
222 static void write_template_arg (tree);
223 static void write_template_template_arg (const tree);
224 static void write_array_type (const tree);
225 static void write_pointer_to_member_type (const tree);
226 static void write_template_param (const tree);
227 static void write_template_template_param (const tree);
228 static void write_substitution (const int);
229 static int discriminator_for_local_entity (tree);
230 static int discriminator_for_string_literal (tree, tree);
231 static void write_discriminator (const int);
232 static void write_local_name (tree, const tree, const tree);
233 static void dump_substitution_candidates (void);
234 static tree mangle_decl_string (const tree);
235 static int local_class_index (tree);
237 /* Control functions. */
239 static inline void start_mangling (const tree);
240 static tree mangle_special_for_type (const tree, const char *);
242 /* Foreign language functions. */
244 static void write_java_integer_type_codes (const tree);
246 /* Append a single character to the end of the mangled
247 representation. */
248 #define write_char(CHAR) \
249 obstack_1grow (mangle_obstack, (CHAR))
251 /* Append a sized buffer to the end of the mangled representation. */
252 #define write_chars(CHAR, LEN) \
253 obstack_grow (mangle_obstack, (CHAR), (LEN))
255 /* Append a NUL-terminated string to the end of the mangled
256 representation. */
257 #define write_string(STRING) \
258 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
260 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
261 same purpose (context, which may be a type) and value (template
262 decl). See write_template_prefix for more information on what this
263 is used for. */
264 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
265 (TREE_CODE (NODE1) == TREE_LIST \
266 && TREE_CODE (NODE2) == TREE_LIST \
267 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
268 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
269 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
270 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
272 /* Write out an unsigned quantity in base 10. */
273 #define write_unsigned_number(NUMBER) \
274 write_number ((NUMBER), /*unsigned_p=*/1, 10)
276 /* If DECL is a template instance, return nonzero and, if
277 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
278 Otherwise return zero. */
280 static int
281 decl_is_template_id (const tree decl, tree* const template_info)
283 if (TREE_CODE (decl) == TYPE_DECL)
285 /* TYPE_DECLs are handled specially. Look at its type to decide
286 if this is a template instantiation. */
287 const tree type = TREE_TYPE (decl);
289 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
291 if (template_info != NULL)
292 /* For a templated TYPE_DECL, the template info is hanging
293 off the type. */
294 *template_info = TYPE_TEMPLATE_INFO (type);
295 return 1;
298 else
300 /* Check if this is a primary template. */
301 if (DECL_LANG_SPECIFIC (decl) != NULL
302 && DECL_USE_TEMPLATE (decl)
303 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
304 && TREE_CODE (decl) != TEMPLATE_DECL)
306 if (template_info != NULL)
307 /* For most templated decls, the template info is hanging
308 off the decl. */
309 *template_info = DECL_TEMPLATE_INFO (decl);
310 return 1;
314 /* It's not a template id. */
315 return 0;
318 /* Produce debugging output of current substitution candidates. */
320 static void
321 dump_substitution_candidates (void)
323 unsigned i;
324 tree el;
326 fprintf (stderr, " ++ substitutions ");
327 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
329 const char *name = "???";
331 if (i > 0)
332 fprintf (stderr, " ");
333 if (DECL_P (el))
334 name = IDENTIFIER_POINTER (DECL_NAME (el));
335 else if (TREE_CODE (el) == TREE_LIST)
336 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
337 else if (TYPE_NAME (el))
338 name = TYPE_NAME_STRING (el);
339 fprintf (stderr, " S%d_ = ", i - 1);
340 if (TYPE_P (el) &&
341 (CP_TYPE_RESTRICT_P (el)
342 || CP_TYPE_VOLATILE_P (el)
343 || CP_TYPE_CONST_P (el)))
344 fprintf (stderr, "CV-");
345 fprintf (stderr, "%s (%s at %p)\n",
346 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
350 /* Both decls and types can be substitution candidates, but sometimes
351 they refer to the same thing. For instance, a TYPE_DECL and
352 RECORD_TYPE for the same class refer to the same thing, and should
353 be treated accordingly in substitutions. This function returns a
354 canonicalized tree node representing NODE that is used when adding
355 and substitution candidates and finding matches. */
357 static inline tree
358 canonicalize_for_substitution (tree node)
360 /* For a TYPE_DECL, use the type instead. */
361 if (TREE_CODE (node) == TYPE_DECL)
362 node = TREE_TYPE (node);
363 if (TYPE_P (node)
364 && TYPE_CANONICAL (node) != node
365 && TYPE_MAIN_VARIANT (node) != node)
367 tree orig = node;
368 /* Here we want to strip the topmost typedef only.
369 We need to do that so is_std_substitution can do proper
370 name matching. */
371 if (TREE_CODE (node) == FUNCTION_TYPE)
372 /* Use build_qualified_type and TYPE_QUALS here to preserve
373 the old buggy mangling of attribute noreturn with abi<5. */
374 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
375 TYPE_QUALS (node));
376 else
377 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
378 cp_type_quals (node));
379 if (TREE_CODE (node) == FUNCTION_TYPE
380 || TREE_CODE (node) == METHOD_TYPE)
381 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
383 return node;
386 /* Add NODE as a substitution candidate. NODE must not already be on
387 the list of candidates. */
389 static void
390 add_substitution (tree node)
392 tree c;
394 if (DEBUG_MANGLE)
395 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
396 get_tree_code_name (TREE_CODE (node)), (void *) node);
398 /* Get the canonicalized substitution candidate for NODE. */
399 c = canonicalize_for_substitution (node);
400 if (DEBUG_MANGLE && c != node)
401 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
402 get_tree_code_name (TREE_CODE (node)), (void *) node);
403 node = c;
405 #if ENABLE_CHECKING
406 /* Make sure NODE isn't already a candidate. */
408 int i;
409 tree candidate;
411 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
413 gcc_assert (!(DECL_P (node) && node == candidate));
414 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
415 && same_type_p (node, candidate)));
418 #endif /* ENABLE_CHECKING */
420 /* Put the decl onto the varray of substitution candidates. */
421 vec_safe_push (G.substitutions, node);
423 if (DEBUG_MANGLE)
424 dump_substitution_candidates ();
427 /* Helper function for find_substitution. Returns nonzero if NODE,
428 which may be a decl or a CLASS_TYPE, is a template-id with template
429 name of substitution_index[INDEX] in the ::std namespace. */
431 static inline int
432 is_std_substitution (const tree node,
433 const substitution_identifier_index_t index)
435 tree type = NULL;
436 tree decl = NULL;
438 if (DECL_P (node))
440 type = TREE_TYPE (node);
441 decl = node;
443 else if (CLASS_TYPE_P (node))
445 type = node;
446 decl = TYPE_NAME (node);
448 else
449 /* These are not the droids you're looking for. */
450 return 0;
452 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
453 && TYPE_LANG_SPECIFIC (type)
454 && TYPE_TEMPLATE_INFO (type)
455 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
456 == subst_identifiers[index]));
459 /* Helper function for find_substitution. Returns nonzero if NODE,
460 which may be a decl or a CLASS_TYPE, is the template-id
461 ::std::identifier<char>, where identifier is
462 substitution_index[INDEX]. */
464 static inline int
465 is_std_substitution_char (const tree node,
466 const substitution_identifier_index_t index)
468 tree args;
469 /* Check NODE's name is ::std::identifier. */
470 if (!is_std_substitution (node, index))
471 return 0;
472 /* Figure out its template args. */
473 if (DECL_P (node))
474 args = DECL_TI_ARGS (node);
475 else if (CLASS_TYPE_P (node))
476 args = CLASSTYPE_TI_ARGS (node);
477 else
478 /* Oops, not a template. */
479 return 0;
480 /* NODE's template arg list should be <char>. */
481 return
482 TREE_VEC_LENGTH (args) == 1
483 && TREE_VEC_ELT (args, 0) == char_type_node;
486 /* Check whether a substitution should be used to represent NODE in
487 the mangling.
489 First, check standard special-case substitutions.
491 <substitution> ::= St
492 # ::std
494 ::= Sa
495 # ::std::allocator
497 ::= Sb
498 # ::std::basic_string
500 ::= Ss
501 # ::std::basic_string<char,
502 ::std::char_traits<char>,
503 ::std::allocator<char> >
505 ::= Si
506 # ::std::basic_istream<char, ::std::char_traits<char> >
508 ::= So
509 # ::std::basic_ostream<char, ::std::char_traits<char> >
511 ::= Sd
512 # ::std::basic_iostream<char, ::std::char_traits<char> >
514 Then examine the stack of currently available substitution
515 candidates for entities appearing earlier in the same mangling
517 If a substitution is found, write its mangled representation and
518 return nonzero. If none is found, just return zero. */
520 static int
521 find_substitution (tree node)
523 int i;
524 const int size = vec_safe_length (G.substitutions);
525 tree decl;
526 tree type;
527 const char *abbr = NULL;
529 if (DEBUG_MANGLE)
530 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
531 get_tree_code_name (TREE_CODE (node)), (void *) node);
533 /* Obtain the canonicalized substitution representation for NODE.
534 This is what we'll compare against. */
535 node = canonicalize_for_substitution (node);
537 /* Check for builtin substitutions. */
539 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
540 type = TYPE_P (node) ? node : TREE_TYPE (node);
542 /* Check for std::allocator. */
543 if (decl
544 && is_std_substitution (decl, SUBID_ALLOCATOR)
545 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
546 abbr = "Sa";
548 /* Check for std::basic_string. */
549 else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
551 if (TYPE_P (node))
553 /* If this is a type (i.e. a fully-qualified template-id),
554 check for
555 std::basic_string <char,
556 std::char_traits<char>,
557 std::allocator<char> > . */
558 if (cp_type_quals (type) == TYPE_UNQUALIFIED
559 && CLASSTYPE_USE_TEMPLATE (type))
561 tree args = CLASSTYPE_TI_ARGS (type);
562 if (TREE_VEC_LENGTH (args) == 3
563 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
564 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
565 SUBID_CHAR_TRAITS)
566 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
567 SUBID_ALLOCATOR))
568 abbr = "Ss";
571 else
572 /* Substitute for the template name only if this isn't a type. */
573 abbr = "Sb";
576 /* Check for basic_{i,o,io}stream. */
577 else if (TYPE_P (node)
578 && cp_type_quals (type) == TYPE_UNQUALIFIED
579 && CLASS_TYPE_P (type)
580 && CLASSTYPE_USE_TEMPLATE (type)
581 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
583 /* First, check for the template
584 args <char, std::char_traits<char> > . */
585 tree args = CLASSTYPE_TI_ARGS (type);
586 if (TREE_VEC_LENGTH (args) == 2
587 && TYPE_P (TREE_VEC_ELT (args, 0))
588 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
589 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
590 SUBID_CHAR_TRAITS))
592 /* Got them. Is this basic_istream? */
593 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
594 abbr = "Si";
595 /* Or basic_ostream? */
596 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
597 abbr = "So";
598 /* Or basic_iostream? */
599 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
600 abbr = "Sd";
604 /* Check for namespace std. */
605 else if (decl && DECL_NAMESPACE_STD_P (decl))
607 write_string ("St");
608 return 1;
611 tree tags = NULL_TREE;
612 if (OVERLOAD_TYPE_P (node) || DECL_CLASS_TEMPLATE_P (node))
613 tags = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (type));
614 /* Now check the list of available substitutions for this mangling
615 operation. */
616 if (!abbr || tags) for (i = 0; i < size; ++i)
618 tree candidate = (*G.substitutions)[i];
619 /* NODE is a matched to a candidate if it's the same decl node or
620 if it's the same type. */
621 if (decl == candidate
622 || (TYPE_P (candidate) && type && TYPE_P (node)
623 && same_type_p (type, candidate))
624 || NESTED_TEMPLATE_MATCH (node, candidate))
626 write_substitution (i);
627 return 1;
631 if (!abbr)
632 /* No substitution found. */
633 return 0;
635 write_string (abbr);
636 if (tags)
638 /* If there are ABI tags on the abbreviation, it becomes
639 a substitution candidate. */
640 write_abi_tags (tags);
641 add_substitution (node);
643 return 1;
647 /* TOP_LEVEL is true, if this is being called at outermost level of
648 mangling. It should be false when mangling a decl appearing in an
649 expression within some other mangling.
651 <mangled-name> ::= _Z <encoding> */
653 static void
654 write_mangled_name (const tree decl, bool top_level)
656 MANGLE_TRACE_TREE ("mangled-name", decl);
658 if (/* The names of `extern "C"' functions are not mangled. */
659 DECL_EXTERN_C_FUNCTION_P (decl)
660 /* But overloaded operator names *are* mangled. */
661 && !DECL_OVERLOADED_OPERATOR_P (decl))
663 unmangled_name:;
665 if (top_level)
666 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
667 else
669 /* The standard notes: "The <encoding> of an extern "C"
670 function is treated like global-scope data, i.e. as its
671 <source-name> without a type." We cannot write
672 overloaded operators that way though, because it contains
673 characters invalid in assembler. */
674 write_string ("_Z");
675 write_source_name (DECL_NAME (decl));
678 else if (VAR_P (decl)
679 /* Variable template instantiations are mangled. */
680 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
681 /* The names of non-static global variables aren't mangled. */
682 && DECL_EXTERNAL_LINKAGE_P (decl)
683 && (CP_DECL_CONTEXT (decl) == global_namespace
684 /* And neither are `extern "C"' variables. */
685 || DECL_EXTERN_C_P (decl)))
687 goto unmangled_name;
689 else
691 write_string ("_Z");
692 write_encoding (decl);
696 /* <encoding> ::= <function name> <bare-function-type>
697 ::= <data name> */
699 static void
700 write_encoding (const tree decl)
702 MANGLE_TRACE_TREE ("encoding", decl);
704 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
706 /* For overloaded operators write just the mangled name
707 without arguments. */
708 if (DECL_OVERLOADED_OPERATOR_P (decl))
709 write_name (decl, /*ignore_local_scope=*/0);
710 else
711 write_source_name (DECL_NAME (decl));
712 return;
715 write_name (decl, /*ignore_local_scope=*/0);
716 if (TREE_CODE (decl) == FUNCTION_DECL)
718 tree fn_type;
719 tree d;
721 if (decl_is_template_id (decl, NULL))
723 fn_type = get_mostly_instantiated_function_type (decl);
724 /* FN_TYPE will not have parameter types for in-charge or
725 VTT parameters. Therefore, we pass NULL_TREE to
726 write_bare_function_type -- otherwise, it will get
727 confused about which artificial parameters to skip. */
728 d = NULL_TREE;
730 else
732 fn_type = TREE_TYPE (decl);
733 d = decl;
736 write_bare_function_type (fn_type,
737 (!DECL_CONSTRUCTOR_P (decl)
738 && !DECL_DESTRUCTOR_P (decl)
739 && !DECL_CONV_FN_P (decl)
740 && decl_is_template_id (decl, NULL)),
745 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
746 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
748 static tree
749 decl_mangling_context (tree decl)
751 tree tcontext = targetm.cxx.decl_mangling_context (decl);
753 if (tcontext != NULL_TREE)
754 return tcontext;
756 if (TREE_CODE (decl) == TEMPLATE_DECL
757 && DECL_TEMPLATE_RESULT (decl))
758 decl = DECL_TEMPLATE_RESULT (decl);
760 if (TREE_CODE (decl) == TYPE_DECL
761 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
763 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
764 if (extra)
765 return extra;
767 else if (template_type_parameter_p (decl))
768 /* template type parms have no mangling context. */
769 return NULL_TREE;
770 return CP_DECL_CONTEXT (decl);
773 /* <name> ::= <unscoped-name>
774 ::= <unscoped-template-name> <template-args>
775 ::= <nested-name>
776 ::= <local-name>
778 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
779 called from <local-name>, which mangles the enclosing scope
780 elsewhere and then uses this function to mangle just the part
781 underneath the function scope. So don't use the <local-name>
782 production, to avoid an infinite recursion. */
784 static void
785 write_name (tree decl, const int ignore_local_scope)
787 tree context;
789 MANGLE_TRACE_TREE ("name", decl);
791 if (TREE_CODE (decl) == TYPE_DECL)
793 /* In case this is a typedef, fish out the corresponding
794 TYPE_DECL for the main variant. */
795 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
798 context = decl_mangling_context (decl);
800 gcc_assert (context != NULL_TREE);
802 if (abi_version_crosses (7)
803 && ignore_local_scope
804 && TREE_CODE (context) == PARM_DECL)
805 G.need_abi_warning = 1;
807 /* A decl in :: or ::std scope is treated specially. The former is
808 mangled using <unscoped-name> or <unscoped-template-name>, the
809 latter with a special substitution. Also, a name that is
810 directly in a local function scope is also mangled with
811 <unscoped-name> rather than a full <nested-name>. */
812 if (context == global_namespace
813 || DECL_NAMESPACE_STD_P (context)
814 || (ignore_local_scope
815 && (TREE_CODE (context) == FUNCTION_DECL
816 || (abi_version_at_least (7)
817 && TREE_CODE (context) == PARM_DECL))))
819 tree template_info;
820 /* Is this a template instance? */
821 if (decl_is_template_id (decl, &template_info))
823 /* Yes: use <unscoped-template-name>. */
824 write_unscoped_template_name (TI_TEMPLATE (template_info));
825 write_template_args (TI_ARGS (template_info));
827 else
828 /* Everything else gets an <unqualified-name>. */
829 write_unscoped_name (decl);
831 else
833 /* Handle local names, unless we asked not to (that is, invoked
834 under <local-name>, to handle only the part of the name under
835 the local scope). */
836 if (!ignore_local_scope)
838 /* Scan up the list of scope context, looking for a
839 function. If we find one, this entity is in local
840 function scope. local_entity tracks context one scope
841 level down, so it will contain the element that's
842 directly in that function's scope, either decl or one of
843 its enclosing scopes. */
844 tree local_entity = decl;
845 while (context != global_namespace)
847 /* Make sure we're always dealing with decls. */
848 if (TYPE_P (context))
849 context = TYPE_NAME (context);
850 /* Is this a function? */
851 if (TREE_CODE (context) == FUNCTION_DECL
852 || TREE_CODE (context) == PARM_DECL)
854 /* Yes, we have local scope. Use the <local-name>
855 production for the innermost function scope. */
856 write_local_name (context, local_entity, decl);
857 return;
859 /* Up one scope level. */
860 local_entity = context;
861 context = decl_mangling_context (context);
864 /* No local scope found? Fall through to <nested-name>. */
867 /* Other decls get a <nested-name> to encode their scope. */
868 write_nested_name (decl);
872 /* <unscoped-name> ::= <unqualified-name>
873 ::= St <unqualified-name> # ::std:: */
875 static void
876 write_unscoped_name (const tree decl)
878 tree context = decl_mangling_context (decl);
880 MANGLE_TRACE_TREE ("unscoped-name", decl);
882 /* Is DECL in ::std? */
883 if (DECL_NAMESPACE_STD_P (context))
885 write_string ("St");
886 write_unqualified_name (decl);
888 else
890 /* If not, it should be either in the global namespace, or directly
891 in a local function scope. A lambda can also be mangled in the
892 scope of a default argument. */
893 gcc_assert (context == global_namespace
894 || TREE_CODE (context) == PARM_DECL
895 || TREE_CODE (context) == FUNCTION_DECL);
897 write_unqualified_name (decl);
901 /* <unscoped-template-name> ::= <unscoped-name>
902 ::= <substitution> */
904 static void
905 write_unscoped_template_name (const tree decl)
907 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
909 if (find_substitution (decl))
910 return;
911 write_unscoped_name (decl);
912 add_substitution (decl);
915 /* Write the nested name, including CV-qualifiers, of DECL.
917 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
918 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
920 <ref-qualifier> ::= R # & ref-qualifier
921 ::= O # && ref-qualifier
922 <CV-qualifiers> ::= [r] [V] [K] */
924 static void
925 write_nested_name (const tree decl)
927 tree template_info;
929 MANGLE_TRACE_TREE ("nested-name", decl);
931 write_char ('N');
933 /* Write CV-qualifiers, if this is a member function. */
934 if (TREE_CODE (decl) == FUNCTION_DECL
935 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
937 if (DECL_VOLATILE_MEMFUNC_P (decl))
938 write_char ('V');
939 if (DECL_CONST_MEMFUNC_P (decl))
940 write_char ('K');
941 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
943 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
944 write_char ('O');
945 else
946 write_char ('R');
950 /* Is this a template instance? */
951 if (decl_is_template_id (decl, &template_info))
953 /* Yes, use <template-prefix>. */
954 write_template_prefix (decl);
955 write_template_args (TI_ARGS (template_info));
957 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
959 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
960 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
962 write_template_prefix (decl);
963 write_template_args (TREE_OPERAND (name, 1));
965 else
967 write_prefix (decl_mangling_context (decl));
968 write_unqualified_name (decl);
971 else
973 /* No, just use <prefix> */
974 write_prefix (decl_mangling_context (decl));
975 write_unqualified_name (decl);
977 write_char ('E');
980 /* <prefix> ::= <prefix> <unqualified-name>
981 ::= <template-param>
982 ::= <template-prefix> <template-args>
983 ::= <decltype>
984 ::= # empty
985 ::= <substitution> */
987 static void
988 write_prefix (const tree node)
990 tree decl;
991 /* Non-NULL if NODE represents a template-id. */
992 tree template_info = NULL;
994 if (node == NULL
995 || node == global_namespace)
996 return;
998 MANGLE_TRACE_TREE ("prefix", node);
1000 if (TREE_CODE (node) == DECLTYPE_TYPE)
1002 write_type (node);
1003 return;
1006 if (find_substitution (node))
1007 return;
1009 if (DECL_P (node))
1011 /* If this is a function or parm decl, that means we've hit function
1012 scope, so this prefix must be for a local name. In this
1013 case, we're under the <local-name> production, which encodes
1014 the enclosing function scope elsewhere. So don't continue
1015 here. */
1016 if (TREE_CODE (node) == FUNCTION_DECL
1017 || TREE_CODE (node) == PARM_DECL)
1018 return;
1020 decl = node;
1021 decl_is_template_id (decl, &template_info);
1023 else
1025 /* Node is a type. */
1026 decl = TYPE_NAME (node);
1027 if (CLASSTYPE_TEMPLATE_ID_P (node))
1028 template_info = TYPE_TEMPLATE_INFO (node);
1031 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1032 write_template_param (node);
1033 else if (template_info != NULL)
1034 /* Templated. */
1036 write_template_prefix (decl);
1037 write_template_args (TI_ARGS (template_info));
1039 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1041 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1042 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1044 write_template_prefix (decl);
1045 write_template_args (TREE_OPERAND (name, 1));
1047 else
1049 write_prefix (decl_mangling_context (decl));
1050 write_unqualified_name (decl);
1053 else
1054 /* Not templated. */
1056 write_prefix (decl_mangling_context (decl));
1057 write_unqualified_name (decl);
1058 if (VAR_P (decl)
1059 || TREE_CODE (decl) == FIELD_DECL)
1061 /* <data-member-prefix> := <member source-name> M */
1062 write_char ('M');
1063 return;
1067 add_substitution (node);
1070 /* <template-prefix> ::= <prefix> <template component>
1071 ::= <template-param>
1072 ::= <substitution> */
1074 static void
1075 write_template_prefix (const tree node)
1077 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1078 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1079 tree context = decl_mangling_context (decl);
1080 tree template_info;
1081 tree templ;
1082 tree substitution;
1084 MANGLE_TRACE_TREE ("template-prefix", node);
1086 /* Find the template decl. */
1087 if (decl_is_template_id (decl, &template_info))
1088 templ = TI_TEMPLATE (template_info);
1089 else if (TREE_CODE (type) == TYPENAME_TYPE)
1090 /* For a typename type, all we have is the name. */
1091 templ = DECL_NAME (decl);
1092 else
1094 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1096 templ = TYPE_TI_TEMPLATE (type);
1099 /* For a member template, though, the template name for the
1100 innermost name must have all the outer template levels
1101 instantiated. For instance, consider
1103 template<typename T> struct Outer {
1104 template<typename U> struct Inner {};
1107 The template name for `Inner' in `Outer<int>::Inner<float>' is
1108 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1109 levels separately, so there's no TEMPLATE_DECL available for this
1110 (there's only `Outer<T>::Inner<U>').
1112 In order to get the substitutions right, we create a special
1113 TREE_LIST to represent the substitution candidate for a nested
1114 template. The TREE_PURPOSE is the template's context, fully
1115 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1116 template.
1118 So, for the example above, `Outer<int>::Inner' is represented as a
1119 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1120 and whose value is `Outer<T>::Inner<U>'. */
1121 if (TYPE_P (context))
1122 substitution = build_tree_list (context, templ);
1123 else
1124 substitution = templ;
1126 if (find_substitution (substitution))
1127 return;
1129 if (TREE_TYPE (templ)
1130 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1131 write_template_param (TREE_TYPE (templ));
1132 else
1134 write_prefix (context);
1135 write_unqualified_name (decl);
1138 add_substitution (substitution);
1141 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1142 mangled through special entry points.
1144 <unqualified-name> ::= <operator-name>
1145 ::= <special-name>
1146 ::= <source-name>
1147 ::= <unnamed-type-name>
1148 ::= <local-source-name>
1150 <local-source-name> ::= L <source-name> <discriminator> */
1152 static void
1153 write_unqualified_id (tree identifier)
1155 if (IDENTIFIER_TYPENAME_P (identifier))
1156 write_conversion_operator_name (TREE_TYPE (identifier));
1157 else if (IDENTIFIER_OPNAME_P (identifier))
1159 int i;
1160 const char *mangled_name = NULL;
1162 /* Unfortunately, there is no easy way to go from the
1163 name of the operator back to the corresponding tree
1164 code. */
1165 for (i = 0; i < MAX_TREE_CODES; ++i)
1166 if (operator_name_info[i].identifier == identifier)
1168 /* The ABI says that we prefer binary operator
1169 names to unary operator names. */
1170 if (operator_name_info[i].arity == 2)
1172 mangled_name = operator_name_info[i].mangled_name;
1173 break;
1175 else if (!mangled_name)
1176 mangled_name = operator_name_info[i].mangled_name;
1178 else if (assignment_operator_name_info[i].identifier
1179 == identifier)
1181 mangled_name
1182 = assignment_operator_name_info[i].mangled_name;
1183 break;
1185 write_string (mangled_name);
1187 else if (UDLIT_OPER_P (identifier))
1188 write_literal_operator_name (identifier);
1189 else
1190 write_source_name (identifier);
1193 static void
1194 write_unqualified_name (tree decl)
1196 MANGLE_TRACE_TREE ("unqualified-name", decl);
1198 if (identifier_p (decl))
1200 write_unqualified_id (decl);
1201 return;
1204 bool found = false;
1206 if (DECL_NAME (decl) == NULL_TREE)
1208 found = true;
1209 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1210 write_source_name (DECL_ASSEMBLER_NAME (decl));
1212 else if (DECL_DECLARES_FUNCTION_P (decl))
1214 found = true;
1215 if (DECL_CONSTRUCTOR_P (decl))
1216 write_special_name_constructor (decl);
1217 else if (DECL_DESTRUCTOR_P (decl))
1218 write_special_name_destructor (decl);
1219 else if (DECL_CONV_FN_P (decl))
1221 /* Conversion operator. Handle it right here.
1222 <operator> ::= cv <type> */
1223 tree type;
1224 if (decl_is_template_id (decl, NULL))
1226 tree fn_type;
1227 fn_type = get_mostly_instantiated_function_type (decl);
1228 type = TREE_TYPE (fn_type);
1230 else if (FNDECL_USED_AUTO (decl))
1231 type = (DECL_STRUCT_FUNCTION (decl)->language
1232 ->x_auto_return_pattern);
1233 else
1234 type = DECL_CONV_FN_TYPE (decl);
1235 write_conversion_operator_name (type);
1237 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1239 operator_name_info_t *oni;
1240 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1241 oni = assignment_operator_name_info;
1242 else
1243 oni = operator_name_info;
1245 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1247 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1248 write_literal_operator_name (DECL_NAME (decl));
1249 else
1250 found = false;
1253 if (found)
1254 /* OK */;
1255 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1256 && DECL_NAMESPACE_SCOPE_P (decl)
1257 && decl_linkage (decl) == lk_internal)
1259 MANGLE_TRACE_TREE ("local-source-name", decl);
1260 write_char ('L');
1261 write_source_name (DECL_NAME (decl));
1262 /* The default discriminator is 1, and that's all we ever use,
1263 so there's no code to output one here. */
1265 else
1267 tree type = TREE_TYPE (decl);
1269 if (TREE_CODE (decl) == TYPE_DECL
1270 && TYPE_ANONYMOUS_P (type))
1271 write_unnamed_type_name (type);
1272 else if (TREE_CODE (decl) == TYPE_DECL
1273 && LAMBDA_TYPE_P (type))
1274 write_closure_type_name (type);
1275 else
1276 write_source_name (DECL_NAME (decl));
1279 /* We use the ABI tags from the primary template, ignoring tags on any
1280 specializations. This is necessary because C++ doesn't require a
1281 specialization to be declared before it is used unless the use
1282 requires a complete type, but we need to get the tags right on
1283 incomplete types as well. */
1284 if (tree tmpl = most_general_template (decl))
1285 decl = DECL_TEMPLATE_RESULT (tmpl);
1286 /* Don't crash on an unbound class template. */
1287 if (decl)
1289 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1290 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1291 : DECL_ATTRIBUTES (decl));
1292 write_abi_tags (lookup_attribute ("abi_tag", attrs));
1296 /* Write the unqualified-name for a conversion operator to TYPE. */
1298 static void
1299 write_conversion_operator_name (const tree type)
1301 write_string ("cv");
1302 write_type (type);
1305 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1307 <source-name> ::= </length/ number> <identifier> */
1309 static void
1310 write_source_name (tree identifier)
1312 MANGLE_TRACE_TREE ("source-name", identifier);
1314 /* Never write the whole template-id name including the template
1315 arguments; we only want the template name. */
1316 if (IDENTIFIER_TEMPLATE (identifier))
1317 identifier = IDENTIFIER_TEMPLATE (identifier);
1319 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1320 write_identifier (IDENTIFIER_POINTER (identifier));
1323 /* Compare two TREE_STRINGs like strcmp. */
1326 tree_string_cmp (const void *p1, const void *p2)
1328 if (p1 == p2)
1329 return 0;
1330 tree s1 = *(const tree*)p1;
1331 tree s2 = *(const tree*)p2;
1332 return strcmp (TREE_STRING_POINTER (s1),
1333 TREE_STRING_POINTER (s2));
1336 /* ID is the name of a function or type with abi_tags attribute TAGS.
1337 Write out the name, suitably decorated. */
1339 static void
1340 write_abi_tags (tree tags)
1342 if (tags == NULL_TREE)
1343 return;
1345 tags = TREE_VALUE (tags);
1347 vec<tree, va_gc> * vec = make_tree_vector();
1349 for (tree t = tags; t; t = TREE_CHAIN (t))
1351 if (ABI_TAG_IMPLICIT (t))
1352 continue;
1353 tree str = TREE_VALUE (t);
1354 vec_safe_push (vec, str);
1357 vec->qsort (tree_string_cmp);
1359 unsigned i; tree str;
1360 FOR_EACH_VEC_ELT (*vec, i, str)
1362 write_string ("B");
1363 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1364 write_identifier (TREE_STRING_POINTER (str));
1367 release_tree_vector (vec);
1370 /* Write a user-defined literal operator.
1371 ::= li <source-name> # "" <source-name>
1372 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1374 static void
1375 write_literal_operator_name (tree identifier)
1377 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1378 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1379 write_unsigned_number (strlen (suffix));
1380 write_identifier (suffix);
1383 /* Encode 0 as _, and 1+ as n-1_. */
1385 static void
1386 write_compact_number (int num)
1388 if (num > 0)
1389 write_unsigned_number (num - 1);
1390 write_char ('_');
1393 /* Return how many unnamed types precede TYPE in its enclosing class. */
1395 static int
1396 nested_anon_class_index (tree type)
1398 int index = 0;
1399 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1400 for (; member; member = DECL_CHAIN (member))
1401 if (DECL_IMPLICIT_TYPEDEF_P (member))
1403 tree memtype = TREE_TYPE (member);
1404 if (memtype == type)
1405 return index;
1406 else if (TYPE_ANONYMOUS_P (memtype))
1407 ++index;
1410 gcc_unreachable ();
1413 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1415 static void
1416 write_unnamed_type_name (const tree type)
1418 int discriminator;
1419 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1421 if (TYPE_FUNCTION_SCOPE_P (type))
1422 discriminator = local_class_index (type);
1423 else if (TYPE_CLASS_SCOPE_P (type))
1424 discriminator = nested_anon_class_index (type);
1425 else
1427 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1428 /* Just use the old mangling at namespace scope. */
1429 write_source_name (TYPE_IDENTIFIER (type));
1430 return;
1433 write_string ("Ut");
1434 write_compact_number (discriminator);
1437 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1438 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1440 static void
1441 write_closure_type_name (const tree type)
1443 tree fn = lambda_function (type);
1444 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1445 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1447 MANGLE_TRACE_TREE ("closure-type-name", type);
1449 write_string ("Ul");
1450 write_method_parms (parms, /*method_p=*/1, fn);
1451 write_char ('E');
1452 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1455 /* Convert NUMBER to ascii using base BASE and generating at least
1456 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1457 into which to store the characters. Returns the number of
1458 characters generated (these will be laid out in advance of where
1459 BUFFER points). */
1461 static int
1462 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1463 char *buffer, const unsigned int min_digits)
1465 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1466 unsigned digits = 0;
1468 while (number)
1470 unsigned HOST_WIDE_INT d = number / base;
1472 *--buffer = base_digits[number - d * base];
1473 digits++;
1474 number = d;
1476 while (digits < min_digits)
1478 *--buffer = base_digits[0];
1479 digits++;
1481 return digits;
1484 /* Non-terminal <number>.
1486 <number> ::= [n] </decimal integer/> */
1488 static void
1489 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1490 const unsigned int base)
1492 char buffer[sizeof (HOST_WIDE_INT) * 8];
1493 unsigned count = 0;
1495 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1497 write_char ('n');
1498 number = -((HOST_WIDE_INT) number);
1500 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1501 write_chars (buffer + sizeof (buffer) - count, count);
1504 /* Write out an integral CST in decimal. Most numbers are small, and
1505 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1506 bigger than that, which we must deal with. */
1508 static inline void
1509 write_integer_cst (const tree cst)
1511 int sign = tree_int_cst_sgn (cst);
1512 widest_int abs_value = wi::abs (wi::to_widest (cst));
1513 if (!wi::fits_uhwi_p (abs_value))
1515 /* A bignum. We do this in chunks, each of which fits in a
1516 HOST_WIDE_INT. */
1517 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1518 unsigned HOST_WIDE_INT chunk;
1519 unsigned chunk_digits;
1520 char *ptr = buffer + sizeof (buffer);
1521 unsigned count = 0;
1522 tree n, base, type;
1523 int done;
1525 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1526 representable. */
1527 chunk = 1000000000;
1528 chunk_digits = 9;
1530 if (sizeof (HOST_WIDE_INT) >= 8)
1532 /* It is at least 64 bits, so 10^18 is representable. */
1533 chunk_digits = 18;
1534 chunk *= chunk;
1537 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1538 base = build_int_cstu (type, chunk);
1539 n = wide_int_to_tree (type, cst);
1541 if (sign < 0)
1543 write_char ('n');
1544 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1548 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1549 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1550 unsigned c;
1552 done = integer_zerop (d);
1553 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1554 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1555 done ? 1 : chunk_digits);
1556 ptr -= c;
1557 count += c;
1558 n = d;
1560 while (!done);
1561 write_chars (ptr, count);
1563 else
1565 /* A small num. */
1566 if (sign < 0)
1567 write_char ('n');
1568 write_unsigned_number (abs_value.to_uhwi ());
1572 /* Write out a floating-point literal.
1574 "Floating-point literals are encoded using the bit pattern of the
1575 target processor's internal representation of that number, as a
1576 fixed-length lowercase hexadecimal string, high-order bytes first
1577 (even if the target processor would store low-order bytes first).
1578 The "n" prefix is not used for floating-point literals; the sign
1579 bit is encoded with the rest of the number.
1581 Here are some examples, assuming the IEEE standard representation
1582 for floating point numbers. (Spaces are for readability, not
1583 part of the encoding.)
1585 1.0f Lf 3f80 0000 E
1586 -1.0f Lf bf80 0000 E
1587 1.17549435e-38f Lf 0080 0000 E
1588 1.40129846e-45f Lf 0000 0001 E
1589 0.0f Lf 0000 0000 E"
1591 Caller is responsible for the Lx and the E. */
1592 static void
1593 write_real_cst (const tree value)
1595 long target_real[4]; /* largest supported float */
1596 char buffer[9]; /* eight hex digits in a 32-bit number */
1597 int i, limit, dir;
1599 tree type = TREE_TYPE (value);
1600 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1602 real_to_target (target_real, &TREE_REAL_CST (value),
1603 TYPE_MODE (type));
1605 /* The value in target_real is in the target word order,
1606 so we must write it out backward if that happens to be
1607 little-endian. write_number cannot be used, it will
1608 produce uppercase. */
1609 if (FLOAT_WORDS_BIG_ENDIAN)
1610 i = 0, limit = words, dir = 1;
1611 else
1612 i = words - 1, limit = -1, dir = -1;
1614 for (; i != limit; i += dir)
1616 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1617 write_chars (buffer, 8);
1621 /* Non-terminal <identifier>.
1623 <identifier> ::= </unqualified source code identifier> */
1625 static void
1626 write_identifier (const char *identifier)
1628 MANGLE_TRACE ("identifier", identifier);
1629 write_string (identifier);
1632 /* Handle constructor productions of non-terminal <special-name>.
1633 CTOR is a constructor FUNCTION_DECL.
1635 <special-name> ::= C1 # complete object constructor
1636 ::= C2 # base object constructor
1637 ::= C3 # complete object allocating constructor
1639 Currently, allocating constructors are never used. */
1641 static void
1642 write_special_name_constructor (const tree ctor)
1644 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1645 write_string ("C2");
1646 /* This is the old-style "[unified]" constructor.
1647 In some cases, we may emit this function and call
1648 it from the clones in order to share code and save space. */
1649 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1650 write_string ("C4");
1651 else
1653 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1654 write_string ("C1");
1658 /* Handle destructor productions of non-terminal <special-name>.
1659 DTOR is a destructor FUNCTION_DECL.
1661 <special-name> ::= D0 # deleting (in-charge) destructor
1662 ::= D1 # complete object (in-charge) destructor
1663 ::= D2 # base object (not-in-charge) destructor */
1665 static void
1666 write_special_name_destructor (const tree dtor)
1668 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1669 write_string ("D0");
1670 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1671 write_string ("D2");
1672 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1673 /* This is the old-style "[unified]" destructor.
1674 In some cases, we may emit this function and call
1675 it from the clones in order to share code and save space. */
1676 write_string ("D4");
1677 else
1679 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1680 write_string ("D1");
1684 /* Scan the vector of local classes and return how many others with the
1685 same name (or same no name) and context precede ENTITY. */
1687 static int
1688 local_class_index (tree entity)
1690 int ix, discriminator = 0;
1691 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1692 : TYPE_IDENTIFIER (entity));
1693 tree ctx = TYPE_CONTEXT (entity);
1694 for (ix = 0; ; ix++)
1696 tree type = (*local_classes)[ix];
1697 if (type == entity)
1698 return discriminator;
1699 if (TYPE_CONTEXT (type) == ctx
1700 && (name ? TYPE_IDENTIFIER (type) == name
1701 : TYPE_ANONYMOUS_P (type)))
1702 ++discriminator;
1704 gcc_unreachable ();
1707 /* Return the discriminator for ENTITY appearing inside
1708 FUNCTION. The discriminator is the lexical ordinal of VAR among
1709 entities with the same name in the same FUNCTION. */
1711 static int
1712 discriminator_for_local_entity (tree entity)
1714 if (DECL_DISCRIMINATOR_P (entity))
1716 if (DECL_DISCRIMINATOR_SET_P (entity))
1717 return DECL_DISCRIMINATOR (entity);
1718 else
1719 /* The first entity with a particular name doesn't get
1720 DECL_DISCRIMINATOR set up. */
1721 return 0;
1723 else if (TREE_CODE (entity) == TYPE_DECL)
1725 /* Scan the list of local classes. */
1726 entity = TREE_TYPE (entity);
1728 /* Lambdas and unnamed types have their own discriminators. */
1729 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1730 return 0;
1732 return local_class_index (entity);
1734 else
1735 gcc_unreachable ();
1738 /* Return the discriminator for STRING, a string literal used inside
1739 FUNCTION. The discriminator is the lexical ordinal of STRING among
1740 string literals used in FUNCTION. */
1742 static int
1743 discriminator_for_string_literal (tree /*function*/,
1744 tree /*string*/)
1746 /* For now, we don't discriminate amongst string literals. */
1747 return 0;
1750 /* <discriminator> := _ <number>
1752 The discriminator is used only for the second and later occurrences
1753 of the same name within a single function. In this case <number> is
1754 n - 2, if this is the nth occurrence, in lexical order. */
1756 static void
1757 write_discriminator (const int discriminator)
1759 /* If discriminator is zero, don't write anything. Otherwise... */
1760 if (discriminator > 0)
1762 write_char ('_');
1763 write_unsigned_number (discriminator - 1);
1767 /* Mangle the name of a function-scope entity. FUNCTION is the
1768 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1769 default argument scope. ENTITY is the decl for the entity itself.
1770 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1771 either ENTITY itself or an enclosing scope of ENTITY.
1773 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1774 := Z <function encoding> E s [<discriminator>]
1775 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1777 static void
1778 write_local_name (tree function, const tree local_entity,
1779 const tree entity)
1781 tree parm = NULL_TREE;
1783 MANGLE_TRACE_TREE ("local-name", entity);
1785 if (TREE_CODE (function) == PARM_DECL)
1787 parm = function;
1788 function = DECL_CONTEXT (parm);
1791 write_char ('Z');
1792 write_encoding (function);
1793 write_char ('E');
1795 /* For this purpose, parameters are numbered from right-to-left. */
1796 if (parm)
1798 tree t;
1799 int i = 0;
1800 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1802 if (t == parm)
1803 i = 1;
1804 else if (i)
1805 ++i;
1807 write_char ('d');
1808 write_compact_number (i - 1);
1811 if (TREE_CODE (entity) == STRING_CST)
1813 write_char ('s');
1814 write_discriminator (discriminator_for_string_literal (function,
1815 entity));
1817 else
1819 /* Now the <entity name>. Let write_name know its being called
1820 from <local-name>, so it doesn't try to process the enclosing
1821 function scope again. */
1822 write_name (entity, /*ignore_local_scope=*/1);
1823 write_discriminator (discriminator_for_local_entity (local_entity));
1827 /* Non-terminals <type> and <CV-qualifier>.
1829 <type> ::= <builtin-type>
1830 ::= <function-type>
1831 ::= <class-enum-type>
1832 ::= <array-type>
1833 ::= <pointer-to-member-type>
1834 ::= <template-param>
1835 ::= <substitution>
1836 ::= <CV-qualifier>
1837 ::= P <type> # pointer-to
1838 ::= R <type> # reference-to
1839 ::= C <type> # complex pair (C 2000)
1840 ::= G <type> # imaginary (C 2000) [not supported]
1841 ::= U <source-name> <type> # vendor extended type qualifier
1843 C++0x extensions
1845 <type> ::= RR <type> # rvalue reference-to
1846 <type> ::= Dt <expression> # decltype of an id-expression or
1847 # class member access
1848 <type> ::= DT <expression> # decltype of an expression
1849 <type> ::= Dn # decltype of nullptr
1851 TYPE is a type node. */
1853 static void
1854 write_type (tree type)
1856 /* This gets set to nonzero if TYPE turns out to be a (possibly
1857 CV-qualified) builtin type. */
1858 int is_builtin_type = 0;
1860 MANGLE_TRACE_TREE ("type", type);
1862 if (type == error_mark_node)
1863 return;
1865 type = canonicalize_for_substitution (type);
1866 if (find_substitution (type))
1867 return;
1870 if (write_CV_qualifiers_for_type (type) > 0)
1871 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1872 mangle the unqualified type. The recursive call is needed here
1873 since both the qualified and unqualified types are substitution
1874 candidates. */
1876 tree t = TYPE_MAIN_VARIANT (type);
1877 if (TREE_CODE (t) == FUNCTION_TYPE
1878 || TREE_CODE (t) == METHOD_TYPE)
1880 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1881 if (abi_version_at_least (8))
1882 /* Avoid adding the unqualified function type as a substitution. */
1883 write_function_type (t);
1884 else
1885 write_type (t);
1886 if (abi_version_crosses (8))
1887 G.need_abi_warning = 1;
1889 else
1890 write_type (t);
1892 else if (TREE_CODE (type) == ARRAY_TYPE)
1893 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1894 so that the cv-qualification of the element type is available
1895 in write_array_type. */
1896 write_array_type (type);
1897 else
1899 tree type_orig = type;
1901 /* See through any typedefs. */
1902 type = TYPE_MAIN_VARIANT (type);
1903 if (TREE_CODE (type) == FUNCTION_TYPE
1904 || TREE_CODE (type) == METHOD_TYPE)
1905 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1907 /* According to the C++ ABI, some library classes are passed the
1908 same as the scalar type of their single member and use the same
1909 mangling. */
1910 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1911 type = TREE_TYPE (first_field (type));
1913 if (TYPE_PTRDATAMEM_P (type))
1914 write_pointer_to_member_type (type);
1915 else
1917 /* Handle any target-specific fundamental types. */
1918 const char *target_mangling
1919 = targetm.mangle_type (type_orig);
1921 if (target_mangling)
1923 write_string (target_mangling);
1924 /* Add substitutions for types other than fundamental
1925 types. */
1926 if (!VOID_TYPE_P (type)
1927 && TREE_CODE (type) != INTEGER_TYPE
1928 && TREE_CODE (type) != REAL_TYPE
1929 && TREE_CODE (type) != BOOLEAN_TYPE)
1930 add_substitution (type);
1931 return;
1934 switch (TREE_CODE (type))
1936 case VOID_TYPE:
1937 case BOOLEAN_TYPE:
1938 case INTEGER_TYPE: /* Includes wchar_t. */
1939 case REAL_TYPE:
1940 case FIXED_POINT_TYPE:
1942 /* If this is a typedef, TYPE may not be one of
1943 the standard builtin type nodes, but an alias of one. Use
1944 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1945 write_builtin_type (TYPE_MAIN_VARIANT (type));
1946 ++is_builtin_type;
1948 break;
1950 case COMPLEX_TYPE:
1951 write_char ('C');
1952 write_type (TREE_TYPE (type));
1953 break;
1955 case FUNCTION_TYPE:
1956 case METHOD_TYPE:
1957 write_function_type (type);
1958 break;
1960 case UNION_TYPE:
1961 case RECORD_TYPE:
1962 case ENUMERAL_TYPE:
1963 /* A pointer-to-member function is represented as a special
1964 RECORD_TYPE, so check for this first. */
1965 if (TYPE_PTRMEMFUNC_P (type))
1966 write_pointer_to_member_type (type);
1967 else
1968 write_class_enum_type (type);
1969 break;
1971 case TYPENAME_TYPE:
1972 case UNBOUND_CLASS_TEMPLATE:
1973 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1974 ordinary nested names. */
1975 write_nested_name (TYPE_STUB_DECL (type));
1976 break;
1978 case POINTER_TYPE:
1979 case REFERENCE_TYPE:
1980 if (TYPE_PTR_P (type))
1981 write_char ('P');
1982 else if (TYPE_REF_IS_RVALUE (type))
1983 write_char ('O');
1984 else
1985 write_char ('R');
1987 tree target = TREE_TYPE (type);
1988 /* Attribute const/noreturn are not reflected in mangling.
1989 We strip them here rather than at a lower level because
1990 a typedef or template argument can have function type
1991 with function-cv-quals (that use the same representation),
1992 but you can't have a pointer/reference to such a type. */
1993 if (TREE_CODE (target) == FUNCTION_TYPE)
1995 if (abi_version_crosses (5)
1996 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
1997 G.need_abi_warning = 1;
1998 if (abi_version_at_least (5))
1999 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2001 write_type (target);
2003 break;
2005 case TEMPLATE_TYPE_PARM:
2006 if (is_auto (type))
2008 if (AUTO_IS_DECLTYPE (type))
2009 write_identifier ("Dc");
2010 else
2011 write_identifier ("Da");
2012 ++is_builtin_type;
2013 break;
2015 /* else fall through. */
2016 case TEMPLATE_PARM_INDEX:
2017 write_template_param (type);
2018 break;
2020 case TEMPLATE_TEMPLATE_PARM:
2021 write_template_template_param (type);
2022 break;
2024 case BOUND_TEMPLATE_TEMPLATE_PARM:
2025 write_template_template_param (type);
2026 write_template_args
2027 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2028 break;
2030 case VECTOR_TYPE:
2031 if (abi_version_at_least (4))
2033 write_string ("Dv");
2034 /* Non-constant vector size would be encoded with
2035 _ expression, but we don't support that yet. */
2036 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2037 write_char ('_');
2039 else
2040 write_string ("U8__vector");
2041 if (abi_version_crosses (4))
2042 G.need_abi_warning = 1;
2043 write_type (TREE_TYPE (type));
2044 break;
2046 case TYPE_PACK_EXPANSION:
2047 write_string ("Dp");
2048 write_type (PACK_EXPANSION_PATTERN (type));
2049 break;
2051 case DECLTYPE_TYPE:
2052 /* These shouldn't make it into mangling. */
2053 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2054 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2056 /* In ABI <5, we stripped decltype of a plain decl. */
2057 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2059 tree expr = DECLTYPE_TYPE_EXPR (type);
2060 tree etype = NULL_TREE;
2061 switch (TREE_CODE (expr))
2063 case VAR_DECL:
2064 case PARM_DECL:
2065 case RESULT_DECL:
2066 case FUNCTION_DECL:
2067 case CONST_DECL:
2068 case TEMPLATE_PARM_INDEX:
2069 etype = TREE_TYPE (expr);
2070 break;
2072 default:
2073 break;
2076 if (etype && !type_uses_auto (etype))
2078 if (abi_version_crosses (5))
2079 G.need_abi_warning = 1;
2080 if (!abi_version_at_least (5))
2082 write_type (etype);
2083 return;
2088 write_char ('D');
2089 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2090 write_char ('t');
2091 else
2092 write_char ('T');
2093 ++cp_unevaluated_operand;
2094 write_expression (DECLTYPE_TYPE_EXPR (type));
2095 --cp_unevaluated_operand;
2096 write_char ('E');
2097 break;
2099 case NULLPTR_TYPE:
2100 write_string ("Dn");
2101 if (abi_version_at_least (7))
2102 ++is_builtin_type;
2103 if (abi_version_crosses (7))
2104 G.need_abi_warning = 1;
2105 break;
2107 case TYPEOF_TYPE:
2108 sorry ("mangling typeof, use decltype instead");
2109 break;
2111 case UNDERLYING_TYPE:
2112 sorry ("mangling __underlying_type");
2113 break;
2115 case LANG_TYPE:
2116 /* fall through. */
2118 default:
2119 gcc_unreachable ();
2124 /* Types other than builtin types are substitution candidates. */
2125 if (!is_builtin_type)
2126 add_substitution (type);
2129 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2130 CV-qualifiers written for TYPE.
2132 <CV-qualifiers> ::= [r] [V] [K] */
2134 static int
2135 write_CV_qualifiers_for_type (const tree type)
2137 int num_qualifiers = 0;
2139 /* The order is specified by:
2141 "In cases where multiple order-insensitive qualifiers are
2142 present, they should be ordered 'K' (closest to the base type),
2143 'V', 'r', and 'U' (farthest from the base type) ..."
2145 Note that we do not use cp_type_quals below; given "const
2146 int[3]", the "const" is emitted with the "int", not with the
2147 array. */
2148 cp_cv_quals quals = TYPE_QUALS (type);
2150 if (quals & TYPE_QUAL_RESTRICT)
2152 write_char ('r');
2153 ++num_qualifiers;
2155 if (quals & TYPE_QUAL_VOLATILE)
2157 write_char ('V');
2158 ++num_qualifiers;
2160 if (quals & TYPE_QUAL_CONST)
2162 write_char ('K');
2163 ++num_qualifiers;
2166 return num_qualifiers;
2169 /* Non-terminal <builtin-type>.
2171 <builtin-type> ::= v # void
2172 ::= b # bool
2173 ::= w # wchar_t
2174 ::= c # char
2175 ::= a # signed char
2176 ::= h # unsigned char
2177 ::= s # short
2178 ::= t # unsigned short
2179 ::= i # int
2180 ::= j # unsigned int
2181 ::= l # long
2182 ::= m # unsigned long
2183 ::= x # long long, __int64
2184 ::= y # unsigned long long, __int64
2185 ::= n # __int128
2186 ::= o # unsigned __int128
2187 ::= f # float
2188 ::= d # double
2189 ::= e # long double, __float80
2190 ::= g # __float128 [not supported]
2191 ::= u <source-name> # vendor extended type */
2193 static void
2194 write_builtin_type (tree type)
2196 if (TYPE_CANONICAL (type))
2197 type = TYPE_CANONICAL (type);
2199 switch (TREE_CODE (type))
2201 case VOID_TYPE:
2202 write_char ('v');
2203 break;
2205 case BOOLEAN_TYPE:
2206 write_char ('b');
2207 break;
2209 case INTEGER_TYPE:
2210 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2211 isn't in integer_type_nodes. */
2212 if (type == wchar_type_node)
2213 write_char ('w');
2214 else if (type == char16_type_node)
2215 write_string ("Ds");
2216 else if (type == char32_type_node)
2217 write_string ("Di");
2218 else if (TYPE_FOR_JAVA (type))
2219 write_java_integer_type_codes (type);
2220 else
2222 size_t itk;
2223 /* Assume TYPE is one of the shared integer type nodes. Find
2224 it in the array of these nodes. */
2225 iagain:
2226 for (itk = 0; itk < itk_none; ++itk)
2227 if (integer_types[itk] != NULL_TREE
2228 && integer_type_codes[itk] != '\0'
2229 && type == integer_types[itk])
2231 /* Print the corresponding single-letter code. */
2232 write_char (integer_type_codes[itk]);
2233 break;
2236 if (itk == itk_none)
2238 tree t = c_common_type_for_mode (TYPE_MODE (type),
2239 TYPE_UNSIGNED (type));
2240 if (type != t)
2242 type = t;
2243 goto iagain;
2246 if (TYPE_PRECISION (type) == 128)
2247 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2248 else
2250 /* Allow for cases where TYPE is not one of the shared
2251 integer type nodes and write a "vendor extended builtin
2252 type" with a name the form intN or uintN, respectively.
2253 Situations like this can happen if you have an
2254 __attribute__((__mode__(__SI__))) type and use exotic
2255 switches like '-mint8' on AVR. Of course, this is
2256 undefined by the C++ ABI (and '-mint8' is not even
2257 Standard C conforming), but when using such special
2258 options you're pretty much in nowhere land anyway. */
2259 const char *prefix;
2260 char prec[11]; /* up to ten digits for an unsigned */
2262 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2263 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2264 write_char ('u'); /* "vendor extended builtin type" */
2265 write_unsigned_number (strlen (prefix) + strlen (prec));
2266 write_string (prefix);
2267 write_string (prec);
2271 break;
2273 case REAL_TYPE:
2274 if (type == float_type_node
2275 || type == java_float_type_node)
2276 write_char ('f');
2277 else if (type == double_type_node
2278 || type == java_double_type_node)
2279 write_char ('d');
2280 else if (type == long_double_type_node)
2281 write_char ('e');
2282 else if (type == dfloat32_type_node)
2283 write_string ("Df");
2284 else if (type == dfloat64_type_node)
2285 write_string ("Dd");
2286 else if (type == dfloat128_type_node)
2287 write_string ("De");
2288 else
2289 gcc_unreachable ();
2290 break;
2292 case FIXED_POINT_TYPE:
2293 write_string ("DF");
2294 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2295 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2296 if (type == fract_type_node
2297 || type == sat_fract_type_node
2298 || type == accum_type_node
2299 || type == sat_accum_type_node)
2300 write_char ('i');
2301 else if (type == unsigned_fract_type_node
2302 || type == sat_unsigned_fract_type_node
2303 || type == unsigned_accum_type_node
2304 || type == sat_unsigned_accum_type_node)
2305 write_char ('j');
2306 else if (type == short_fract_type_node
2307 || type == sat_short_fract_type_node
2308 || type == short_accum_type_node
2309 || type == sat_short_accum_type_node)
2310 write_char ('s');
2311 else if (type == unsigned_short_fract_type_node
2312 || type == sat_unsigned_short_fract_type_node
2313 || type == unsigned_short_accum_type_node
2314 || type == sat_unsigned_short_accum_type_node)
2315 write_char ('t');
2316 else if (type == long_fract_type_node
2317 || type == sat_long_fract_type_node
2318 || type == long_accum_type_node
2319 || type == sat_long_accum_type_node)
2320 write_char ('l');
2321 else if (type == unsigned_long_fract_type_node
2322 || type == sat_unsigned_long_fract_type_node
2323 || type == unsigned_long_accum_type_node
2324 || type == sat_unsigned_long_accum_type_node)
2325 write_char ('m');
2326 else if (type == long_long_fract_type_node
2327 || type == sat_long_long_fract_type_node
2328 || type == long_long_accum_type_node
2329 || type == sat_long_long_accum_type_node)
2330 write_char ('x');
2331 else if (type == unsigned_long_long_fract_type_node
2332 || type == sat_unsigned_long_long_fract_type_node
2333 || type == unsigned_long_long_accum_type_node
2334 || type == sat_unsigned_long_long_accum_type_node)
2335 write_char ('y');
2336 else
2337 sorry ("mangling unknown fixed point type");
2338 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2339 if (TYPE_SATURATING (type))
2340 write_char ('s');
2341 else
2342 write_char ('n');
2343 break;
2345 default:
2346 gcc_unreachable ();
2350 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2351 METHOD_TYPE. The return type is mangled before the parameter
2352 types.
2354 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2356 static void
2357 write_function_type (const tree type)
2359 MANGLE_TRACE_TREE ("function-type", type);
2361 /* For a pointer to member function, the function type may have
2362 cv-qualifiers, indicating the quals for the artificial 'this'
2363 parameter. */
2364 if (TREE_CODE (type) == METHOD_TYPE)
2366 /* The first parameter must be a POINTER_TYPE pointing to the
2367 `this' parameter. */
2368 tree this_type = class_of_this_parm (type);
2369 write_CV_qualifiers_for_type (this_type);
2372 write_char ('F');
2373 /* We don't track whether or not a type is `extern "C"'. Note that
2374 you can have an `extern "C"' function that does not have
2375 `extern "C"' type, and vice versa:
2377 extern "C" typedef void function_t();
2378 function_t f; // f has C++ linkage, but its type is
2379 // `extern "C"'
2381 typedef void function_t();
2382 extern "C" function_t f; // Vice versa.
2384 See [dcl.link]. */
2385 write_bare_function_type (type, /*include_return_type_p=*/1,
2386 /*decl=*/NULL);
2387 if (FUNCTION_REF_QUALIFIED (type))
2389 if (FUNCTION_RVALUE_QUALIFIED (type))
2390 write_char ('O');
2391 else
2392 write_char ('R');
2394 write_char ('E');
2397 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2398 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2399 is mangled before the parameter types. If non-NULL, DECL is
2400 FUNCTION_DECL for the function whose type is being emitted.
2402 If DECL is a member of a Java type, then a literal 'J'
2403 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2404 were nonzero.
2406 <bare-function-type> ::= [J]</signature/ type>+ */
2408 static void
2409 write_bare_function_type (const tree type, const int include_return_type_p,
2410 const tree decl)
2412 int java_method_p;
2414 MANGLE_TRACE_TREE ("bare-function-type", type);
2416 /* Detect Java methods and emit special encoding. */
2417 if (decl != NULL
2418 && DECL_FUNCTION_MEMBER_P (decl)
2419 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2420 && !DECL_CONSTRUCTOR_P (decl)
2421 && !DECL_DESTRUCTOR_P (decl)
2422 && !DECL_CONV_FN_P (decl))
2424 java_method_p = 1;
2425 write_char ('J');
2427 else
2429 java_method_p = 0;
2432 /* Mangle the return type, if requested. */
2433 if (include_return_type_p || java_method_p)
2434 write_type (TREE_TYPE (type));
2436 /* Now mangle the types of the arguments. */
2437 ++G.parm_depth;
2438 write_method_parms (TYPE_ARG_TYPES (type),
2439 TREE_CODE (type) == METHOD_TYPE,
2440 decl);
2441 --G.parm_depth;
2444 /* Write the mangled representation of a method parameter list of
2445 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2446 considered a non-static method, and the this parameter is omitted.
2447 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2448 parameters are being emitted. */
2450 static void
2451 write_method_parms (tree parm_types, const int method_p, const tree decl)
2453 tree first_parm_type;
2454 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2456 /* Assume this parameter type list is variable-length. If it ends
2457 with a void type, then it's not. */
2458 int varargs_p = 1;
2460 /* If this is a member function, skip the first arg, which is the
2461 this pointer.
2462 "Member functions do not encode the type of their implicit this
2463 parameter."
2465 Similarly, there's no need to mangle artificial parameters, like
2466 the VTT parameters for constructors and destructors. */
2467 if (method_p)
2469 parm_types = TREE_CHAIN (parm_types);
2470 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2472 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2474 parm_types = TREE_CHAIN (parm_types);
2475 parm_decl = DECL_CHAIN (parm_decl);
2479 for (first_parm_type = parm_types;
2480 parm_types;
2481 parm_types = TREE_CHAIN (parm_types))
2483 tree parm = TREE_VALUE (parm_types);
2484 if (parm == void_type_node)
2486 /* "Empty parameter lists, whether declared as () or
2487 conventionally as (void), are encoded with a void parameter
2488 (v)." */
2489 if (parm_types == first_parm_type)
2490 write_type (parm);
2491 /* If the parm list is terminated with a void type, it's
2492 fixed-length. */
2493 varargs_p = 0;
2494 /* A void type better be the last one. */
2495 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2497 else
2498 write_type (parm);
2501 if (varargs_p)
2502 /* <builtin-type> ::= z # ellipsis */
2503 write_char ('z');
2506 /* <class-enum-type> ::= <name> */
2508 static void
2509 write_class_enum_type (const tree type)
2511 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2514 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2515 arguments.
2517 <template-args> ::= I <template-arg>* E */
2519 static void
2520 write_template_args (tree args)
2522 int i;
2523 int length = 0;
2525 MANGLE_TRACE_TREE ("template-args", args);
2527 write_char ('I');
2529 if (args)
2530 length = TREE_VEC_LENGTH (args);
2532 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2534 /* We have nested template args. We want the innermost template
2535 argument list. */
2536 args = TREE_VEC_ELT (args, length - 1);
2537 length = TREE_VEC_LENGTH (args);
2539 for (i = 0; i < length; ++i)
2540 write_template_arg (TREE_VEC_ELT (args, i));
2542 write_char ('E');
2545 /* Write out the
2546 <unqualified-name>
2547 <unqualified-name> <template-args>
2548 part of SCOPE_REF or COMPONENT_REF mangling. */
2550 static void
2551 write_member_name (tree member)
2553 if (identifier_p (member))
2554 write_unqualified_id (member);
2555 else if (DECL_P (member))
2556 write_unqualified_name (member);
2557 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2559 tree name = TREE_OPERAND (member, 0);
2560 if (TREE_CODE (name) == OVERLOAD)
2561 name = OVL_FUNCTION (name);
2562 write_member_name (name);
2563 write_template_args (TREE_OPERAND (member, 1));
2565 else
2566 write_expression (member);
2569 /* <expression> ::= <unary operator-name> <expression>
2570 ::= <binary operator-name> <expression> <expression>
2571 ::= <expr-primary>
2573 <expr-primary> ::= <template-param>
2574 ::= L <type> <value number> E # literal
2575 ::= L <mangled-name> E # external name
2576 ::= st <type> # sizeof
2577 ::= sr <type> <unqualified-name> # dependent name
2578 ::= sr <type> <unqualified-name> <template-args> */
2580 static void
2581 write_expression (tree expr)
2583 enum tree_code code = TREE_CODE (expr);
2585 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2586 is converted (via qualification conversions) to another
2587 type. */
2588 while (TREE_CODE (expr) == NOP_EXPR
2589 /* Parentheses aren't mangled. */
2590 || code == PAREN_EXPR
2591 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2593 expr = TREE_OPERAND (expr, 0);
2594 code = TREE_CODE (expr);
2597 if (code == BASELINK
2598 && (!type_unknown_p (expr)
2599 || !BASELINK_QUALIFIED_P (expr)))
2601 expr = BASELINK_FUNCTIONS (expr);
2602 code = TREE_CODE (expr);
2605 /* Handle pointers-to-members by making them look like expression
2606 nodes. */
2607 if (code == PTRMEM_CST)
2609 expr = build_nt (ADDR_EXPR,
2610 build_qualified_name (/*type=*/NULL_TREE,
2611 PTRMEM_CST_CLASS (expr),
2612 PTRMEM_CST_MEMBER (expr),
2613 /*template_p=*/false));
2614 code = TREE_CODE (expr);
2617 /* Handle template parameters. */
2618 if (code == TEMPLATE_TYPE_PARM
2619 || code == TEMPLATE_TEMPLATE_PARM
2620 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2621 || code == TEMPLATE_PARM_INDEX)
2622 write_template_param (expr);
2623 /* Handle literals. */
2624 else if (TREE_CODE_CLASS (code) == tcc_constant
2625 || code == CONST_DECL)
2626 write_template_arg_literal (expr);
2627 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2629 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2630 write_string ("fpT");
2632 else if (code == PARM_DECL)
2634 /* A function parameter used in a late-specified return type. */
2635 int index = DECL_PARM_INDEX (expr);
2636 int level = DECL_PARM_LEVEL (expr);
2637 int delta = G.parm_depth - level + 1;
2638 gcc_assert (index >= 1);
2639 write_char ('f');
2640 if (delta != 0)
2642 if (abi_version_at_least (5))
2644 /* Let L be the number of function prototype scopes from the
2645 innermost one (in which the parameter reference occurs) up
2646 to (and including) the one containing the declaration of
2647 the referenced parameter. If the parameter declaration
2648 clause of the innermost function prototype scope has been
2649 completely seen, it is not counted (in that case -- which
2650 is perhaps the most common -- L can be zero). */
2651 write_char ('L');
2652 write_unsigned_number (delta - 1);
2654 if (abi_version_crosses (5))
2655 G.need_abi_warning = true;
2657 write_char ('p');
2658 write_compact_number (index - 1);
2660 else if (DECL_P (expr))
2662 write_char ('L');
2663 write_mangled_name (expr, false);
2664 write_char ('E');
2666 else if (TREE_CODE (expr) == SIZEOF_EXPR
2667 && SIZEOF_EXPR_TYPE_P (expr))
2669 write_string ("st");
2670 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2672 else if (TREE_CODE (expr) == SIZEOF_EXPR
2673 && TYPE_P (TREE_OPERAND (expr, 0)))
2675 write_string ("st");
2676 write_type (TREE_OPERAND (expr, 0));
2678 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2679 && TYPE_P (TREE_OPERAND (expr, 0)))
2681 write_string ("at");
2682 write_type (TREE_OPERAND (expr, 0));
2684 else if (code == SCOPE_REF
2685 || code == BASELINK)
2687 tree scope, member;
2688 if (code == SCOPE_REF)
2690 scope = TREE_OPERAND (expr, 0);
2691 member = TREE_OPERAND (expr, 1);
2693 else
2695 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2696 member = BASELINK_FUNCTIONS (expr);
2699 /* If the MEMBER is a real declaration, then the qualifying
2700 scope was not dependent. Ideally, we would not have a
2701 SCOPE_REF in those cases, but sometimes we do. If the second
2702 argument is a DECL, then the name must not have been
2703 dependent. */
2704 if (DECL_P (member))
2705 write_expression (member);
2706 else
2708 write_string ("sr");
2709 write_type (scope);
2710 write_member_name (member);
2713 else if (INDIRECT_REF_P (expr)
2714 && TREE_TYPE (TREE_OPERAND (expr, 0))
2715 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2717 write_expression (TREE_OPERAND (expr, 0));
2719 else if (identifier_p (expr))
2721 /* An operator name appearing as a dependent name needs to be
2722 specially marked to disambiguate between a use of the operator
2723 name and a use of the operator in an expression. */
2724 if (IDENTIFIER_OPNAME_P (expr))
2725 write_string ("on");
2726 write_unqualified_id (expr);
2728 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2730 tree fn = TREE_OPERAND (expr, 0);
2731 if (is_overloaded_fn (fn))
2732 fn = DECL_NAME (get_first_fn (fn));
2733 if (IDENTIFIER_OPNAME_P (fn))
2734 write_string ("on");
2735 write_unqualified_id (fn);
2736 write_template_args (TREE_OPERAND (expr, 1));
2738 else if (TREE_CODE (expr) == MODOP_EXPR)
2740 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2741 const char *name = (assignment_operator_name_info[(int) subop]
2742 .mangled_name);
2743 write_string (name);
2744 write_expression (TREE_OPERAND (expr, 0));
2745 write_expression (TREE_OPERAND (expr, 2));
2747 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2749 /* ::= [gs] nw <expression>* _ <type> E
2750 ::= [gs] nw <expression>* _ <type> <initializer>
2751 ::= [gs] na <expression>* _ <type> E
2752 ::= [gs] na <expression>* _ <type> <initializer>
2753 <initializer> ::= pi <expression>* E */
2754 tree placement = TREE_OPERAND (expr, 0);
2755 tree type = TREE_OPERAND (expr, 1);
2756 tree nelts = TREE_OPERAND (expr, 2);
2757 tree init = TREE_OPERAND (expr, 3);
2758 tree t;
2760 gcc_assert (code == NEW_EXPR);
2761 if (TREE_OPERAND (expr, 2))
2762 code = VEC_NEW_EXPR;
2764 if (NEW_EXPR_USE_GLOBAL (expr))
2765 write_string ("gs");
2767 write_string (operator_name_info[(int) code].mangled_name);
2769 for (t = placement; t; t = TREE_CHAIN (t))
2770 write_expression (TREE_VALUE (t));
2772 write_char ('_');
2774 if (nelts)
2776 tree domain;
2777 ++processing_template_decl;
2778 domain = compute_array_index_type (NULL_TREE, nelts,
2779 tf_warning_or_error);
2780 type = build_cplus_array_type (type, domain);
2781 --processing_template_decl;
2783 write_type (type);
2785 if (init && TREE_CODE (init) == TREE_LIST
2786 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2787 write_expression (TREE_VALUE (init));
2788 else
2790 if (init)
2791 write_string ("pi");
2792 if (init && init != void_node)
2793 for (t = init; t; t = TREE_CHAIN (t))
2794 write_expression (TREE_VALUE (t));
2795 write_char ('E');
2798 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2800 gcc_assert (code == DELETE_EXPR);
2801 if (DELETE_EXPR_USE_VEC (expr))
2802 code = VEC_DELETE_EXPR;
2804 if (DELETE_EXPR_USE_GLOBAL (expr))
2805 write_string ("gs");
2807 write_string (operator_name_info[(int) code].mangled_name);
2809 write_expression (TREE_OPERAND (expr, 0));
2811 else if (code == THROW_EXPR)
2813 tree op = TREE_OPERAND (expr, 0);
2814 if (op)
2816 write_string ("tw");
2817 write_expression (op);
2819 else
2820 write_string ("tr");
2822 else if (code == CONSTRUCTOR)
2824 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2825 unsigned i; tree val;
2827 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2828 write_string ("il");
2829 else
2831 write_string ("tl");
2832 write_type (TREE_TYPE (expr));
2834 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2835 write_expression (val);
2836 write_char ('E');
2838 else if (dependent_name (expr))
2840 write_unqualified_id (dependent_name (expr));
2842 else
2844 int i, len;
2845 const char *name;
2847 /* When we bind a variable or function to a non-type template
2848 argument with reference type, we create an ADDR_EXPR to show
2849 the fact that the entity's address has been taken. But, we
2850 don't actually want to output a mangling code for the `&'. */
2851 if (TREE_CODE (expr) == ADDR_EXPR
2852 && TREE_TYPE (expr)
2853 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2855 expr = TREE_OPERAND (expr, 0);
2856 if (DECL_P (expr))
2858 write_expression (expr);
2859 return;
2862 code = TREE_CODE (expr);
2865 if (code == COMPONENT_REF)
2867 tree ob = TREE_OPERAND (expr, 0);
2869 if (TREE_CODE (ob) == ARROW_EXPR)
2871 write_string (operator_name_info[(int)code].mangled_name);
2872 ob = TREE_OPERAND (ob, 0);
2873 write_expression (ob);
2875 else if (!is_dummy_object (ob))
2877 write_string ("dt");
2878 write_expression (ob);
2880 /* else, for a non-static data member with no associated object (in
2881 unevaluated context), use the unresolved-name mangling. */
2883 write_member_name (TREE_OPERAND (expr, 1));
2884 return;
2887 /* If it wasn't any of those, recursively expand the expression. */
2888 name = operator_name_info[(int) code].mangled_name;
2890 /* We used to mangle const_cast and static_cast like a C cast. */
2891 if (code == CONST_CAST_EXPR
2892 || code == STATIC_CAST_EXPR)
2894 if (abi_version_crosses (6))
2895 G.need_abi_warning = 1;
2896 if (!abi_version_at_least (6))
2897 name = operator_name_info[CAST_EXPR].mangled_name;
2900 if (name == NULL)
2902 switch (code)
2904 case TRAIT_EXPR:
2905 error ("use of built-in trait %qE in function signature; "
2906 "use library traits instead", expr);
2907 break;
2909 default:
2910 sorry ("mangling %C", code);
2911 break;
2913 return;
2915 else
2916 write_string (name);
2918 switch (code)
2920 case CALL_EXPR:
2922 tree fn = CALL_EXPR_FN (expr);
2924 if (TREE_CODE (fn) == ADDR_EXPR)
2925 fn = TREE_OPERAND (fn, 0);
2927 /* Mangle a dependent name as the name, not whatever happens to
2928 be the first function in the overload set. */
2929 if ((TREE_CODE (fn) == FUNCTION_DECL
2930 || TREE_CODE (fn) == OVERLOAD)
2931 && type_dependent_expression_p_push (expr))
2932 fn = DECL_NAME (get_first_fn (fn));
2934 write_expression (fn);
2937 for (i = 0; i < call_expr_nargs (expr); ++i)
2938 write_expression (CALL_EXPR_ARG (expr, i));
2939 write_char ('E');
2940 break;
2942 case CAST_EXPR:
2943 write_type (TREE_TYPE (expr));
2944 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2945 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2946 else
2948 tree args = TREE_OPERAND (expr, 0);
2949 write_char ('_');
2950 for (; args; args = TREE_CHAIN (args))
2951 write_expression (TREE_VALUE (args));
2952 write_char ('E');
2954 break;
2956 case DYNAMIC_CAST_EXPR:
2957 case REINTERPRET_CAST_EXPR:
2958 case STATIC_CAST_EXPR:
2959 case CONST_CAST_EXPR:
2960 write_type (TREE_TYPE (expr));
2961 write_expression (TREE_OPERAND (expr, 0));
2962 break;
2964 case PREINCREMENT_EXPR:
2965 case PREDECREMENT_EXPR:
2966 if (abi_version_at_least (6))
2967 write_char ('_');
2968 if (abi_version_crosses (6))
2969 G.need_abi_warning = 1;
2970 /* Fall through. */
2972 default:
2973 /* In the middle-end, some expressions have more operands than
2974 they do in templates (and mangling). */
2975 len = cp_tree_operand_length (expr);
2977 for (i = 0; i < len; ++i)
2979 tree operand = TREE_OPERAND (expr, i);
2980 /* As a GNU extension, the middle operand of a
2981 conditional may be omitted. Since expression
2982 manglings are supposed to represent the input token
2983 stream, there's no good way to mangle such an
2984 expression without extending the C++ ABI. */
2985 if (code == COND_EXPR && i == 1 && !operand)
2987 error ("omitted middle operand to %<?:%> operand "
2988 "cannot be mangled");
2989 continue;
2991 write_expression (operand);
2997 /* Literal subcase of non-terminal <template-arg>.
2999 "Literal arguments, e.g. "A<42L>", are encoded with their type
3000 and value. Negative integer values are preceded with "n"; for
3001 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3002 encoded as 0, true as 1." */
3004 static void
3005 write_template_arg_literal (const tree value)
3007 write_char ('L');
3008 write_type (TREE_TYPE (value));
3010 /* Write a null member pointer value as (type)0, regardless of its
3011 real representation. */
3012 if (null_member_pointer_value_p (value))
3013 write_integer_cst (integer_zero_node);
3014 else
3015 switch (TREE_CODE (value))
3017 case CONST_DECL:
3018 write_integer_cst (DECL_INITIAL (value));
3019 break;
3021 case INTEGER_CST:
3022 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3023 || integer_zerop (value) || integer_onep (value));
3024 write_integer_cst (value);
3025 break;
3027 case REAL_CST:
3028 write_real_cst (value);
3029 break;
3031 case COMPLEX_CST:
3032 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3033 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3035 write_integer_cst (TREE_REALPART (value));
3036 write_char ('_');
3037 write_integer_cst (TREE_IMAGPART (value));
3039 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3040 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3042 write_real_cst (TREE_REALPART (value));
3043 write_char ('_');
3044 write_real_cst (TREE_IMAGPART (value));
3046 else
3047 gcc_unreachable ();
3048 break;
3050 case STRING_CST:
3051 sorry ("string literal in function template signature");
3052 break;
3054 default:
3055 gcc_unreachable ();
3058 write_char ('E');
3061 /* Non-terminal <template-arg>.
3063 <template-arg> ::= <type> # type
3064 ::= L <type> </value/ number> E # literal
3065 ::= LZ <name> E # external name
3066 ::= X <expression> E # expression */
3068 static void
3069 write_template_arg (tree node)
3071 enum tree_code code = TREE_CODE (node);
3073 MANGLE_TRACE_TREE ("template-arg", node);
3075 /* A template template parameter's argument list contains TREE_LIST
3076 nodes of which the value field is the actual argument. */
3077 if (code == TREE_LIST)
3079 node = TREE_VALUE (node);
3080 /* If it's a decl, deal with its type instead. */
3081 if (DECL_P (node))
3083 node = TREE_TYPE (node);
3084 code = TREE_CODE (node);
3088 if (REFERENCE_REF_P (node))
3089 node = TREE_OPERAND (node, 0);
3090 if (TREE_CODE (node) == NOP_EXPR
3091 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3093 /* Template parameters can be of reference type. To maintain
3094 internal consistency, such arguments use a conversion from
3095 address of object to reference type. */
3096 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3097 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3100 if (TREE_CODE (node) == BASELINK
3101 && !type_unknown_p (node))
3103 if (abi_version_at_least (6))
3104 node = BASELINK_FUNCTIONS (node);
3105 if (abi_version_crosses (6))
3106 /* We wrongly wrapped a class-scope function in X/E. */
3107 G.need_abi_warning = 1;
3110 if (ARGUMENT_PACK_P (node))
3112 /* Expand the template argument pack. */
3113 tree args = ARGUMENT_PACK_ARGS (node);
3114 int i, length = TREE_VEC_LENGTH (args);
3115 if (abi_version_at_least (6))
3116 write_char ('J');
3117 else
3118 write_char ('I');
3119 if (abi_version_crosses (6))
3120 G.need_abi_warning = 1;
3121 for (i = 0; i < length; ++i)
3122 write_template_arg (TREE_VEC_ELT (args, i));
3123 write_char ('E');
3125 else if (TYPE_P (node))
3126 write_type (node);
3127 else if (code == TEMPLATE_DECL)
3128 /* A template appearing as a template arg is a template template arg. */
3129 write_template_template_arg (node);
3130 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3131 || code == CONST_DECL
3132 || null_member_pointer_value_p (node))
3133 write_template_arg_literal (node);
3134 else if (DECL_P (node))
3136 write_char ('L');
3137 /* Until ABI version 3, the underscore before the mangled name
3138 was incorrectly omitted. */
3139 if (!abi_version_at_least (3))
3140 write_char ('Z');
3141 else
3142 write_string ("_Z");
3143 if (abi_version_crosses (3))
3144 G.need_abi_warning = 1;
3145 write_encoding (node);
3146 write_char ('E');
3148 else
3150 /* Template arguments may be expressions. */
3151 write_char ('X');
3152 write_expression (node);
3153 write_char ('E');
3157 /* <template-template-arg>
3158 ::= <name>
3159 ::= <substitution> */
3161 static void
3162 write_template_template_arg (const tree decl)
3164 MANGLE_TRACE_TREE ("template-template-arg", decl);
3166 if (find_substitution (decl))
3167 return;
3168 write_name (decl, /*ignore_local_scope=*/0);
3169 add_substitution (decl);
3173 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3175 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3176 ::= A <expression> _ </element/ type>
3178 "Array types encode the dimension (number of elements) and the
3179 element type. For variable length arrays, the dimension (but not
3180 the '_' separator) is omitted." */
3182 static void
3183 write_array_type (const tree type)
3185 write_char ('A');
3186 if (TYPE_DOMAIN (type))
3188 tree index_type;
3189 tree max;
3191 index_type = TYPE_DOMAIN (type);
3192 /* The INDEX_TYPE gives the upper and lower bounds of the
3193 array. */
3194 max = TYPE_MAX_VALUE (index_type);
3195 if (TREE_CODE (max) == INTEGER_CST)
3197 /* The ABI specifies that we should mangle the number of
3198 elements in the array, not the largest allowed index. */
3199 offset_int wmax = wi::to_offset (max) + 1;
3200 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3201 number of elements as zero. */
3202 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3203 gcc_assert (wi::fits_uhwi_p (wmax));
3204 write_unsigned_number (wmax.to_uhwi ());
3206 else
3208 max = TREE_OPERAND (max, 0);
3209 write_expression (max);
3213 write_char ('_');
3214 write_type (TREE_TYPE (type));
3217 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3218 variables. TYPE is a pointer-to-member POINTER_TYPE.
3220 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3222 static void
3223 write_pointer_to_member_type (const tree type)
3225 write_char ('M');
3226 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3227 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3230 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3231 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3232 TEMPLATE_PARM_INDEX.
3234 <template-param> ::= T </parameter/ number> _ */
3236 static void
3237 write_template_param (const tree parm)
3239 int parm_index;
3241 MANGLE_TRACE_TREE ("template-parm", parm);
3243 switch (TREE_CODE (parm))
3245 case TEMPLATE_TYPE_PARM:
3246 case TEMPLATE_TEMPLATE_PARM:
3247 case BOUND_TEMPLATE_TEMPLATE_PARM:
3248 parm_index = TEMPLATE_TYPE_IDX (parm);
3249 break;
3251 case TEMPLATE_PARM_INDEX:
3252 parm_index = TEMPLATE_PARM_IDX (parm);
3253 break;
3255 default:
3256 gcc_unreachable ();
3259 write_char ('T');
3260 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3261 earliest template param denoted by `_'. */
3262 write_compact_number (parm_index);
3265 /* <template-template-param>
3266 ::= <template-param>
3267 ::= <substitution> */
3269 static void
3270 write_template_template_param (const tree parm)
3272 tree templ = NULL_TREE;
3274 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3275 template template parameter. The substitution candidate here is
3276 only the template. */
3277 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3279 templ
3280 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3281 if (find_substitution (templ))
3282 return;
3285 /* <template-param> encodes only the template parameter position,
3286 not its template arguments, which is fine here. */
3287 write_template_param (parm);
3288 if (templ)
3289 add_substitution (templ);
3292 /* Non-terminal <substitution>.
3294 <substitution> ::= S <seq-id> _
3295 ::= S_ */
3297 static void
3298 write_substitution (const int seq_id)
3300 MANGLE_TRACE ("substitution", "");
3302 write_char ('S');
3303 if (seq_id > 0)
3304 write_number (seq_id - 1, /*unsigned=*/1, 36);
3305 write_char ('_');
3308 /* Start mangling ENTITY. */
3310 static inline void
3311 start_mangling (const tree entity)
3313 G.entity = entity;
3314 G.need_abi_warning = false;
3315 obstack_free (&name_obstack, name_base);
3316 mangle_obstack = &name_obstack;
3317 name_base = obstack_alloc (&name_obstack, 0);
3320 /* Done with mangling. If WARN is true, and the name of G.entity will
3321 be mangled differently in a future version of the ABI, issue a
3322 warning. */
3324 static void
3325 finish_mangling_internal (void)
3327 /* Clear all the substitutions. */
3328 vec_safe_truncate (G.substitutions, 0);
3330 /* Null-terminate the string. */
3331 write_char ('\0');
3335 /* Like finish_mangling_internal, but return the mangled string. */
3337 static inline const char *
3338 finish_mangling (void)
3340 finish_mangling_internal ();
3341 return (const char *) obstack_finish (mangle_obstack);
3344 /* Like finish_mangling_internal, but return an identifier. */
3346 static tree
3347 finish_mangling_get_identifier (void)
3349 finish_mangling_internal ();
3350 /* Don't obstack_finish here, and the next start_mangling will
3351 remove the identifier. */
3352 return get_identifier ((const char *) obstack_base (mangle_obstack));
3355 /* Initialize data structures for mangling. */
3357 void
3358 init_mangle (void)
3360 gcc_obstack_init (&name_obstack);
3361 name_base = obstack_alloc (&name_obstack, 0);
3362 vec_alloc (G.substitutions, 0);
3364 /* Cache these identifiers for quick comparison when checking for
3365 standard substitutions. */
3366 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3367 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3368 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3369 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3370 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3371 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3374 /* Generate the mangled name of DECL. */
3376 static tree
3377 mangle_decl_string (const tree decl)
3379 tree result;
3380 location_t saved_loc = input_location;
3381 tree saved_fn = NULL_TREE;
3382 bool template_p = false;
3384 /* We shouldn't be trying to mangle an uninstantiated template. */
3385 gcc_assert (!type_dependent_expression_p (decl));
3387 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3389 struct tinst_level *tl = current_instantiation ();
3390 if ((!tl || tl->decl != decl)
3391 && push_tinst_level (decl))
3393 template_p = true;
3394 saved_fn = current_function_decl;
3395 current_function_decl = NULL_TREE;
3398 input_location = DECL_SOURCE_LOCATION (decl);
3400 start_mangling (decl);
3402 if (TREE_CODE (decl) == TYPE_DECL)
3403 write_type (TREE_TYPE (decl));
3404 else
3405 write_mangled_name (decl, true);
3407 result = finish_mangling_get_identifier ();
3408 if (DEBUG_MANGLE)
3409 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3410 IDENTIFIER_POINTER (result));
3412 if (template_p)
3414 pop_tinst_level ();
3415 current_function_decl = saved_fn;
3417 input_location = saved_loc;
3419 return result;
3422 /* Return an identifier for the external mangled name of DECL. */
3424 static tree
3425 get_mangled_id (tree decl)
3427 tree id = mangle_decl_string (decl);
3428 return targetm.mangle_decl_assembler_name (decl, id);
3431 /* If DECL is a mangling alias, remove it from the symbol table and return
3432 true; otherwise return false. */
3434 bool
3435 maybe_remove_implicit_alias (tree decl)
3437 if (DECL_P (decl) && DECL_ARTIFICIAL (decl)
3438 && DECL_IGNORED_P (decl)
3439 && (TREE_CODE (decl) == FUNCTION_DECL
3440 || (TREE_CODE (decl) == VAR_DECL
3441 && TREE_STATIC (decl))))
3443 symtab_node *n = symtab_node::get (decl);
3444 if (n && n->cpp_implicit_alias)
3446 n->remove();
3447 return true;
3450 return false;
3453 /* Create an identifier for the external mangled name of DECL. */
3455 void
3456 mangle_decl (const tree decl)
3458 tree id;
3459 bool dep;
3461 /* Don't bother mangling uninstantiated templates. */
3462 ++processing_template_decl;
3463 if (TREE_CODE (decl) == TYPE_DECL)
3464 dep = dependent_type_p (TREE_TYPE (decl));
3465 else
3466 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3467 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3468 --processing_template_decl;
3469 if (dep)
3470 return;
3472 id = get_mangled_id (decl);
3473 SET_DECL_ASSEMBLER_NAME (decl, id);
3475 if (G.need_abi_warning
3476 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3477 && TREE_CODE (decl) != TYPE_DECL
3478 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3479 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3481 /* If the mangling will change in the future, emit an alias with the
3482 future mangled name for forward-compatibility. */
3483 int save_ver;
3484 tree id2;
3486 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3487 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3488 inform (DECL_SOURCE_LOCATION (decl), "a later -fabi-version= (or =0) "
3489 "avoids this error with a change in mangling");
3491 save_ver = flag_abi_version;
3492 flag_abi_version = flag_abi_compat_version;
3493 id2 = mangle_decl_string (decl);
3494 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3495 flag_abi_version = save_ver;
3497 if (id2 == id)
3498 return;
3500 if (warn_abi)
3502 if (flag_abi_compat_version != 0
3503 && abi_version_at_least (flag_abi_compat_version))
3504 warning (OPT_Wabi, "the mangled name of %q+D changed between "
3505 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3506 G.entity, flag_abi_compat_version, id2,
3507 flag_abi_version, id);
3508 else
3509 warning (OPT_Wabi, "the mangled name of %q+D changes between "
3510 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3511 G.entity, flag_abi_version, id,
3512 flag_abi_compat_version, id2);
3515 #ifdef ASM_OUTPUT_DEF
3516 /* If there's a declaration already using this mangled name,
3517 don't create a compatibility alias that conflicts. */
3518 if (IDENTIFIER_GLOBAL_VALUE (id2))
3519 return;
3521 struct cgraph_node *n = NULL;
3522 if (TREE_CODE (decl) == FUNCTION_DECL
3523 && !(n = cgraph_node::get (decl)))
3524 /* Don't create an alias to an unreferenced function. */
3525 return;
3527 tree alias = make_alias_for (decl, id2);
3528 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
3529 DECL_IGNORED_P (alias) = 1;
3530 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3531 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3532 if (vague_linkage_p (decl))
3533 DECL_WEAK (alias) = 1;
3534 if (TREE_CODE (decl) == FUNCTION_DECL)
3535 n->create_same_body_alias (alias, decl);
3536 else
3537 varpool_node::create_extra_name_alias (alias, decl);
3538 #endif
3542 /* Generate the mangled representation of TYPE. */
3544 const char *
3545 mangle_type_string (const tree type)
3547 const char *result;
3549 start_mangling (type);
3550 write_type (type);
3551 result = finish_mangling ();
3552 if (DEBUG_MANGLE)
3553 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3554 return result;
3557 /* Create an identifier for the mangled name of a special component
3558 for belonging to TYPE. CODE is the ABI-specified code for this
3559 component. */
3561 static tree
3562 mangle_special_for_type (const tree type, const char *code)
3564 tree result;
3566 /* We don't have an actual decl here for the special component, so
3567 we can't just process the <encoded-name>. Instead, fake it. */
3568 start_mangling (type);
3570 /* Start the mangling. */
3571 write_string ("_Z");
3572 write_string (code);
3574 /* Add the type. */
3575 write_type (type);
3576 result = finish_mangling_get_identifier ();
3578 if (DEBUG_MANGLE)
3579 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3580 IDENTIFIER_POINTER (result));
3582 return result;
3585 /* Create an identifier for the mangled representation of the typeinfo
3586 structure for TYPE. */
3588 tree
3589 mangle_typeinfo_for_type (const tree type)
3591 return mangle_special_for_type (type, "TI");
3594 /* Create an identifier for the mangled name of the NTBS containing
3595 the mangled name of TYPE. */
3597 tree
3598 mangle_typeinfo_string_for_type (const tree type)
3600 return mangle_special_for_type (type, "TS");
3603 /* Create an identifier for the mangled name of the vtable for TYPE. */
3605 tree
3606 mangle_vtbl_for_type (const tree type)
3608 return mangle_special_for_type (type, "TV");
3611 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3613 tree
3614 mangle_vtt_for_type (const tree type)
3616 return mangle_special_for_type (type, "TT");
3619 /* Return an identifier for a construction vtable group. TYPE is
3620 the most derived class in the hierarchy; BINFO is the base
3621 subobject for which this construction vtable group will be used.
3623 This mangling isn't part of the ABI specification; in the ABI
3624 specification, the vtable group is dumped in the same COMDAT as the
3625 main vtable, and is referenced only from that vtable, so it doesn't
3626 need an external name. For binary formats without COMDAT sections,
3627 though, we need external names for the vtable groups.
3629 We use the production
3631 <special-name> ::= CT <type> <offset number> _ <base type> */
3633 tree
3634 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3636 tree result;
3638 start_mangling (type);
3640 write_string ("_Z");
3641 write_string ("TC");
3642 write_type (type);
3643 write_integer_cst (BINFO_OFFSET (binfo));
3644 write_char ('_');
3645 write_type (BINFO_TYPE (binfo));
3647 result = finish_mangling_get_identifier ();
3648 if (DEBUG_MANGLE)
3649 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3650 IDENTIFIER_POINTER (result));
3651 return result;
3654 /* Mangle a this pointer or result pointer adjustment.
3656 <call-offset> ::= h <fixed offset number> _
3657 ::= v <fixed offset number> _ <virtual offset number> _ */
3659 static void
3660 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3662 write_char (virtual_offset ? 'v' : 'h');
3664 /* For either flavor, write the fixed offset. */
3665 write_integer_cst (fixed_offset);
3666 write_char ('_');
3668 /* For a virtual thunk, add the virtual offset. */
3669 if (virtual_offset)
3671 write_integer_cst (virtual_offset);
3672 write_char ('_');
3676 /* Return an identifier for the mangled name of a this-adjusting or
3677 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3678 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3679 is a virtual thunk, and it is the vtbl offset in
3680 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3681 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3682 thunk itself. A covariant thunk name always includes the adjustment
3683 for the this pointer, even if there is none.
3685 <special-name> ::= T <call-offset> <base encoding>
3686 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3687 <base encoding> */
3689 tree
3690 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3691 tree virtual_offset)
3693 tree result;
3695 start_mangling (fn_decl);
3697 write_string ("_Z");
3698 write_char ('T');
3700 if (!this_adjusting)
3702 /* Covariant thunk with no this adjustment */
3703 write_char ('c');
3704 mangle_call_offset (integer_zero_node, NULL_TREE);
3705 mangle_call_offset (fixed_offset, virtual_offset);
3707 else if (!DECL_THUNK_P (fn_decl))
3708 /* Plain this adjusting thunk. */
3709 mangle_call_offset (fixed_offset, virtual_offset);
3710 else
3712 /* This adjusting thunk to covariant thunk. */
3713 write_char ('c');
3714 mangle_call_offset (fixed_offset, virtual_offset);
3715 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3716 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3717 if (virtual_offset)
3718 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3719 mangle_call_offset (fixed_offset, virtual_offset);
3720 fn_decl = THUNK_TARGET (fn_decl);
3723 /* Scoped name. */
3724 write_encoding (fn_decl);
3726 result = finish_mangling_get_identifier ();
3727 if (DEBUG_MANGLE)
3728 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3729 return result;
3732 struct conv_type_hasher : ggc_hasher<tree>
3734 static hashval_t hash (tree);
3735 static bool equal (tree, tree);
3738 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3739 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3740 TYPE. */
3742 static GTY (()) hash_table<conv_type_hasher> *conv_type_names;
3744 /* Hash a node (VAL1) in the table. */
3746 hashval_t
3747 conv_type_hasher::hash (tree val)
3749 return (hashval_t) TYPE_UID (TREE_TYPE (val));
3752 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3754 bool
3755 conv_type_hasher::equal (tree val1, tree val2)
3757 return TREE_TYPE (val1) == val2;
3760 /* Return an identifier for the mangled unqualified name for a
3761 conversion operator to TYPE. This mangling is not specified by the
3762 ABI spec; it is only used internally. */
3764 tree
3765 mangle_conv_op_name_for_type (const tree type)
3767 tree *slot;
3768 tree identifier;
3770 if (type == error_mark_node)
3771 return error_mark_node;
3773 if (conv_type_names == NULL)
3774 conv_type_names = hash_table<conv_type_hasher>::create_ggc (31);
3776 slot = conv_type_names->find_slot_with_hash (type,
3777 (hashval_t) TYPE_UID (type),
3778 INSERT);
3779 identifier = *slot;
3780 if (!identifier)
3782 char buffer[64];
3784 /* Create a unique name corresponding to TYPE. */
3785 sprintf (buffer, "operator %lu",
3786 (unsigned long) conv_type_names->elements ());
3787 identifier = get_identifier (buffer);
3788 *slot = identifier;
3790 /* Hang TYPE off the identifier so it can be found easily later
3791 when performing conversions. */
3792 TREE_TYPE (identifier) = type;
3794 /* Set bits on the identifier so we know later it's a conversion. */
3795 IDENTIFIER_OPNAME_P (identifier) = 1;
3796 IDENTIFIER_TYPENAME_P (identifier) = 1;
3799 return identifier;
3802 /* Write out the appropriate string for this variable when generating
3803 another mangled name based on this one. */
3805 static void
3806 write_guarded_var_name (const tree variable)
3808 if (DECL_NAME (variable)
3809 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3810 /* The name of a guard variable for a reference temporary should refer
3811 to the reference, not the temporary. */
3812 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3813 else
3814 write_name (variable, /*ignore_local_scope=*/0);
3817 /* Return an identifier for the name of an initialization guard
3818 variable for indicated VARIABLE. */
3820 tree
3821 mangle_guard_variable (const tree variable)
3823 start_mangling (variable);
3824 write_string ("_ZGV");
3825 write_guarded_var_name (variable);
3826 return finish_mangling_get_identifier ();
3829 /* Return an identifier for the name of a thread_local initialization
3830 function for VARIABLE. */
3832 tree
3833 mangle_tls_init_fn (const tree variable)
3835 start_mangling (variable);
3836 write_string ("_ZTH");
3837 write_guarded_var_name (variable);
3838 return finish_mangling_get_identifier ();
3841 /* Return an identifier for the name of a thread_local wrapper
3842 function for VARIABLE. */
3844 #define TLS_WRAPPER_PREFIX "_ZTW"
3846 tree
3847 mangle_tls_wrapper_fn (const tree variable)
3849 start_mangling (variable);
3850 write_string (TLS_WRAPPER_PREFIX);
3851 write_guarded_var_name (variable);
3852 return finish_mangling_get_identifier ();
3855 /* Return true iff FN is a thread_local wrapper function. */
3857 bool
3858 decl_tls_wrapper_p (const tree fn)
3860 if (TREE_CODE (fn) != FUNCTION_DECL)
3861 return false;
3862 tree name = DECL_NAME (fn);
3863 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3864 strlen (TLS_WRAPPER_PREFIX)) == 0;
3867 /* Return an identifier for the name of a temporary variable used to
3868 initialize a static reference. This isn't part of the ABI, but we might
3869 as well call them something readable. */
3871 static GTY(()) int temp_count;
3873 tree
3874 mangle_ref_init_variable (const tree variable)
3876 start_mangling (variable);
3877 write_string ("_ZGR");
3878 write_name (variable, /*ignore_local_scope=*/0);
3879 /* Avoid name clashes with aggregate initialization of multiple
3880 references at once. */
3881 write_unsigned_number (temp_count++);
3882 return finish_mangling_get_identifier ();
3886 /* Foreign language type mangling section. */
3888 /* How to write the type codes for the integer Java type. */
3890 static void
3891 write_java_integer_type_codes (const tree type)
3893 if (type == java_int_type_node)
3894 write_char ('i');
3895 else if (type == java_short_type_node)
3896 write_char ('s');
3897 else if (type == java_byte_type_node)
3898 write_char ('c');
3899 else if (type == java_char_type_node)
3900 write_char ('w');
3901 else if (type == java_long_type_node)
3902 write_char ('x');
3903 else if (type == java_boolean_type_node)
3904 write_char ('b');
3905 else
3906 gcc_unreachable ();
3909 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3910 function generates a mangled name for the vtable map variable of
3911 the class type. For example, if the class type is
3912 "std::bad_exception", the mangled name for the class is
3913 "St13bad_exception". This function would generate the name
3914 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3915 "_VTV<std::bad_exception>::__vtable_map". */
3918 char *
3919 get_mangled_vtable_map_var_name (tree class_type)
3921 char *var_name = NULL;
3922 const char *prefix = "_ZN4_VTVI";
3923 const char *postfix = "E12__vtable_mapE";
3925 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3927 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3928 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3929 strlen (prefix) +
3930 strlen (postfix) + 1;
3932 var_name = (char *) xmalloc (len);
3934 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3936 return var_name;
3939 #include "gt-cp-mangle.h"