2014-01-30 Alangi Derick <alangiderick@gmail.com>
[official-gcc.git] / gcc / cp / mangle.c
blob7bb6f4b0199c2254be60035e9d270c4550c02b7e
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file implements mangling of C++ names according to the IA64
22 C++ ABI specification. A mangled name encodes a function or
23 variable's name, scope, type, and/or template arguments into a text
24 identifier. This identifier is used as the function's or
25 variable's linkage name, to preserve compatibility between C++'s
26 language features (templates, scoping, and overloading) and C
27 linkers.
29 Additionally, g++ uses mangled names internally. To support this,
30 mangling of types is allowed, even though the mangled name of a
31 type should not appear by itself as an exported name. Ditto for
32 uninstantiated templates.
34 The primary entry point for this module is mangle_decl, which
35 returns an identifier containing the mangled name for a decl.
36 Additional entry points are provided to build mangled names of
37 particular constructs when the appropriate decl for that construct
38 is not available. These are:
40 mangle_typeinfo_for_type: typeinfo data
41 mangle_typeinfo_string_for_type: typeinfo type name
42 mangle_vtbl_for_type: virtual table data
43 mangle_vtt_for_type: VTT data
44 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
45 mangle_thunk: thunk function or entry */
47 #include "config.h"
48 #include "system.h"
49 #include "coretypes.h"
50 #include "tm.h"
51 #include "tree.h"
52 #include "stor-layout.h"
53 #include "stringpool.h"
54 #include "tm_p.h"
55 #include "cp-tree.h"
56 #include "obstack.h"
57 #include "flags.h"
58 #include "target.h"
59 #include "cgraph.h"
61 /* Debugging support. */
63 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
64 #ifndef DEBUG_MANGLE
65 #define DEBUG_MANGLE 0
66 #endif
68 /* Macros for tracing the write_* functions. */
69 #if DEBUG_MANGLE
70 # define MANGLE_TRACE(FN, INPUT) \
71 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
72 # define MANGLE_TRACE_TREE(FN, NODE) \
73 fprintf (stderr, " %-24s: %-24s (%p)\n", \
74 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
75 #else
76 # define MANGLE_TRACE(FN, INPUT)
77 # define MANGLE_TRACE_TREE(FN, NODE)
78 #endif
80 /* Nonzero if NODE is a class template-id. We can't rely on
81 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
82 that hard to distinguish A<T> from A, where A<T> is the type as
83 instantiated outside of the template, and A is the type used
84 without parameters inside the template. */
85 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
86 (TYPE_LANG_SPECIFIC (NODE) != NULL \
87 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
88 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
89 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
91 /* Things we only need one of. This module is not reentrant. */
92 typedef struct GTY(()) globals {
93 /* An array of the current substitution candidates, in the order
94 we've seen them. */
95 vec<tree, va_gc> *substitutions;
97 /* The entity that is being mangled. */
98 tree GTY ((skip)) entity;
100 /* How many parameter scopes we are inside. */
101 int parm_depth;
103 /* True if the mangling will be different in a future version of the
104 ABI. */
105 bool need_abi_warning;
106 } globals;
108 static GTY (()) globals G;
110 /* The obstack on which we build mangled names. */
111 static struct obstack *mangle_obstack;
113 /* The obstack on which we build mangled names that are not going to
114 be IDENTIFIER_NODEs. */
115 static struct obstack name_obstack;
117 /* The first object on the name_obstack; we use this to free memory
118 allocated on the name_obstack. */
119 static void *name_base;
121 /* Indices into subst_identifiers. These are identifiers used in
122 special substitution rules. */
123 typedef enum
125 SUBID_ALLOCATOR,
126 SUBID_BASIC_STRING,
127 SUBID_CHAR_TRAITS,
128 SUBID_BASIC_ISTREAM,
129 SUBID_BASIC_OSTREAM,
130 SUBID_BASIC_IOSTREAM,
131 SUBID_MAX
133 substitution_identifier_index_t;
135 /* For quick substitution checks, look up these common identifiers
136 once only. */
137 static GTY(()) tree subst_identifiers[SUBID_MAX];
139 /* Single-letter codes for builtin integer types, defined in
140 <builtin-type>. These are indexed by integer_type_kind values. */
141 static const char
142 integer_type_codes[itk_none] =
144 'c', /* itk_char */
145 'a', /* itk_signed_char */
146 'h', /* itk_unsigned_char */
147 's', /* itk_short */
148 't', /* itk_unsigned_short */
149 'i', /* itk_int */
150 'j', /* itk_unsigned_int */
151 'l', /* itk_long */
152 'm', /* itk_unsigned_long */
153 'x', /* itk_long_long */
154 'y', /* itk_unsigned_long_long */
155 'n', /* itk_int128 */
156 'o', /* itk_unsigned_int128 */
159 static int decl_is_template_id (const tree, tree* const);
161 /* Functions for handling substitutions. */
163 static inline tree canonicalize_for_substitution (tree);
164 static void add_substitution (tree);
165 static inline int is_std_substitution (const tree,
166 const substitution_identifier_index_t);
167 static inline int is_std_substitution_char (const tree,
168 const substitution_identifier_index_t);
169 static int find_substitution (tree);
170 static void mangle_call_offset (const tree, const tree);
172 /* Functions for emitting mangled representations of things. */
174 static void write_mangled_name (const tree, bool);
175 static void write_encoding (const tree);
176 static void write_name (tree, const int);
177 static void write_abi_tags (tree);
178 static void write_unscoped_name (const tree);
179 static void write_unscoped_template_name (const tree);
180 static void write_nested_name (const tree);
181 static void write_prefix (const tree);
182 static void write_template_prefix (const tree);
183 static void write_unqualified_name (const tree);
184 static void write_conversion_operator_name (const tree);
185 static void write_source_name (tree);
186 static void write_literal_operator_name (tree);
187 static void write_unnamed_type_name (const tree);
188 static void write_closure_type_name (const tree);
189 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
190 const unsigned int);
191 static void write_number (unsigned HOST_WIDE_INT, const int,
192 const unsigned int);
193 static void write_compact_number (int num);
194 static void write_integer_cst (const tree);
195 static void write_real_cst (const tree);
196 static void write_identifier (const char *);
197 static void write_special_name_constructor (const tree);
198 static void write_special_name_destructor (const tree);
199 static void write_type (tree);
200 static int write_CV_qualifiers_for_type (const tree);
201 static void write_builtin_type (tree);
202 static void write_function_type (const tree);
203 static void write_bare_function_type (const tree, const int, const tree);
204 static void write_method_parms (tree, const int, const tree);
205 static void write_class_enum_type (const tree);
206 static void write_template_args (tree);
207 static void write_expression (tree);
208 static void write_template_arg_literal (const tree);
209 static void write_template_arg (tree);
210 static void write_template_template_arg (const tree);
211 static void write_array_type (const tree);
212 static void write_pointer_to_member_type (const tree);
213 static void write_template_param (const tree);
214 static void write_template_template_param (const tree);
215 static void write_substitution (const int);
216 static int discriminator_for_local_entity (tree);
217 static int discriminator_for_string_literal (tree, tree);
218 static void write_discriminator (const int);
219 static void write_local_name (tree, const tree, const tree);
220 static void dump_substitution_candidates (void);
221 static tree mangle_decl_string (const tree);
222 static int local_class_index (tree);
224 /* Control functions. */
226 static inline void start_mangling (const tree);
227 static inline const char *finish_mangling (const bool);
228 static tree mangle_special_for_type (const tree, const char *);
230 /* Foreign language functions. */
232 static void write_java_integer_type_codes (const tree);
234 /* Append a single character to the end of the mangled
235 representation. */
236 #define write_char(CHAR) \
237 obstack_1grow (mangle_obstack, (CHAR))
239 /* Append a sized buffer to the end of the mangled representation. */
240 #define write_chars(CHAR, LEN) \
241 obstack_grow (mangle_obstack, (CHAR), (LEN))
243 /* Append a NUL-terminated string to the end of the mangled
244 representation. */
245 #define write_string(STRING) \
246 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
248 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
249 same purpose (context, which may be a type) and value (template
250 decl). See write_template_prefix for more information on what this
251 is used for. */
252 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
253 (TREE_CODE (NODE1) == TREE_LIST \
254 && TREE_CODE (NODE2) == TREE_LIST \
255 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
256 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
257 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
258 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
260 /* Write out an unsigned quantity in base 10. */
261 #define write_unsigned_number(NUMBER) \
262 write_number ((NUMBER), /*unsigned_p=*/1, 10)
264 /* If DECL is a template instance, return nonzero and, if
265 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
266 Otherwise return zero. */
268 static int
269 decl_is_template_id (const tree decl, tree* const template_info)
271 if (TREE_CODE (decl) == TYPE_DECL)
273 /* TYPE_DECLs are handled specially. Look at its type to decide
274 if this is a template instantiation. */
275 const tree type = TREE_TYPE (decl);
277 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
279 if (template_info != NULL)
280 /* For a templated TYPE_DECL, the template info is hanging
281 off the type. */
282 *template_info = TYPE_TEMPLATE_INFO (type);
283 return 1;
286 else
288 /* Check if this is a primary template. */
289 if (DECL_LANG_SPECIFIC (decl) != NULL
290 && DECL_USE_TEMPLATE (decl)
291 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
292 && TREE_CODE (decl) != TEMPLATE_DECL)
294 if (template_info != NULL)
295 /* For most templated decls, the template info is hanging
296 off the decl. */
297 *template_info = DECL_TEMPLATE_INFO (decl);
298 return 1;
302 /* It's not a template id. */
303 return 0;
306 /* Produce debugging output of current substitution candidates. */
308 static void
309 dump_substitution_candidates (void)
311 unsigned i;
312 tree el;
314 fprintf (stderr, " ++ substitutions ");
315 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
317 const char *name = "???";
319 if (i > 0)
320 fprintf (stderr, " ");
321 if (DECL_P (el))
322 name = IDENTIFIER_POINTER (DECL_NAME (el));
323 else if (TREE_CODE (el) == TREE_LIST)
324 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
325 else if (TYPE_NAME (el))
326 name = IDENTIFIER_POINTER (TYPE_IDENTIFIER (el));
327 fprintf (stderr, " S%d_ = ", i - 1);
328 if (TYPE_P (el) &&
329 (CP_TYPE_RESTRICT_P (el)
330 || CP_TYPE_VOLATILE_P (el)
331 || CP_TYPE_CONST_P (el)))
332 fprintf (stderr, "CV-");
333 fprintf (stderr, "%s (%s at %p)\n",
334 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
338 /* Both decls and types can be substitution candidates, but sometimes
339 they refer to the same thing. For instance, a TYPE_DECL and
340 RECORD_TYPE for the same class refer to the same thing, and should
341 be treated accordingly in substitutions. This function returns a
342 canonicalized tree node representing NODE that is used when adding
343 and substitution candidates and finding matches. */
345 static inline tree
346 canonicalize_for_substitution (tree node)
348 /* For a TYPE_DECL, use the type instead. */
349 if (TREE_CODE (node) == TYPE_DECL)
350 node = TREE_TYPE (node);
351 if (TYPE_P (node)
352 && TYPE_CANONICAL (node) != node
353 && TYPE_MAIN_VARIANT (node) != node)
355 tree orig = node;
356 /* Here we want to strip the topmost typedef only.
357 We need to do that so is_std_substitution can do proper
358 name matching. */
359 if (TREE_CODE (node) == FUNCTION_TYPE)
360 /* Use build_qualified_type and TYPE_QUALS here to preserve
361 the old buggy mangling of attribute noreturn with abi<5. */
362 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
363 TYPE_QUALS (node));
364 else
365 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
366 cp_type_quals (node));
367 if (TREE_CODE (node) == FUNCTION_TYPE
368 || TREE_CODE (node) == METHOD_TYPE)
369 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
371 return node;
374 /* Add NODE as a substitution candidate. NODE must not already be on
375 the list of candidates. */
377 static void
378 add_substitution (tree node)
380 tree c;
382 if (DEBUG_MANGLE)
383 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
384 get_tree_code_name (TREE_CODE (node)), (void *) node);
386 /* Get the canonicalized substitution candidate for NODE. */
387 c = canonicalize_for_substitution (node);
388 if (DEBUG_MANGLE && c != node)
389 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
390 get_tree_code_name (TREE_CODE (node)), (void *) node);
391 node = c;
393 #if ENABLE_CHECKING
394 /* Make sure NODE isn't already a candidate. */
396 int i;
397 tree candidate;
399 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
401 gcc_assert (!(DECL_P (node) && node == candidate));
402 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
403 && same_type_p (node, candidate)));
406 #endif /* ENABLE_CHECKING */
408 /* Put the decl onto the varray of substitution candidates. */
409 vec_safe_push (G.substitutions, node);
411 if (DEBUG_MANGLE)
412 dump_substitution_candidates ();
415 /* Helper function for find_substitution. Returns nonzero if NODE,
416 which may be a decl or a CLASS_TYPE, is a template-id with template
417 name of substitution_index[INDEX] in the ::std namespace. */
419 static inline int
420 is_std_substitution (const tree node,
421 const substitution_identifier_index_t index)
423 tree type = NULL;
424 tree decl = NULL;
426 if (DECL_P (node))
428 type = TREE_TYPE (node);
429 decl = node;
431 else if (CLASS_TYPE_P (node))
433 type = node;
434 decl = TYPE_NAME (node);
436 else
437 /* These are not the droids you're looking for. */
438 return 0;
440 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
441 && TYPE_LANG_SPECIFIC (type)
442 && TYPE_TEMPLATE_INFO (type)
443 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
444 == subst_identifiers[index]));
447 /* Helper function for find_substitution. Returns nonzero if NODE,
448 which may be a decl or a CLASS_TYPE, is the template-id
449 ::std::identifier<char>, where identifier is
450 substitution_index[INDEX]. */
452 static inline int
453 is_std_substitution_char (const tree node,
454 const substitution_identifier_index_t index)
456 tree args;
457 /* Check NODE's name is ::std::identifier. */
458 if (!is_std_substitution (node, index))
459 return 0;
460 /* Figure out its template args. */
461 if (DECL_P (node))
462 args = DECL_TI_ARGS (node);
463 else if (CLASS_TYPE_P (node))
464 args = CLASSTYPE_TI_ARGS (node);
465 else
466 /* Oops, not a template. */
467 return 0;
468 /* NODE's template arg list should be <char>. */
469 return
470 TREE_VEC_LENGTH (args) == 1
471 && TREE_VEC_ELT (args, 0) == char_type_node;
474 /* Check whether a substitution should be used to represent NODE in
475 the mangling.
477 First, check standard special-case substitutions.
479 <substitution> ::= St
480 # ::std
482 ::= Sa
483 # ::std::allocator
485 ::= Sb
486 # ::std::basic_string
488 ::= Ss
489 # ::std::basic_string<char,
490 ::std::char_traits<char>,
491 ::std::allocator<char> >
493 ::= Si
494 # ::std::basic_istream<char, ::std::char_traits<char> >
496 ::= So
497 # ::std::basic_ostream<char, ::std::char_traits<char> >
499 ::= Sd
500 # ::std::basic_iostream<char, ::std::char_traits<char> >
502 Then examine the stack of currently available substitution
503 candidates for entities appearing earlier in the same mangling
505 If a substitution is found, write its mangled representation and
506 return nonzero. If none is found, just return zero. */
508 static int
509 find_substitution (tree node)
511 int i;
512 const int size = vec_safe_length (G.substitutions);
513 tree decl;
514 tree type;
516 if (DEBUG_MANGLE)
517 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
518 get_tree_code_name (TREE_CODE (node)), (void *) node);
520 /* Obtain the canonicalized substitution representation for NODE.
521 This is what we'll compare against. */
522 node = canonicalize_for_substitution (node);
524 /* Check for builtin substitutions. */
526 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
527 type = TYPE_P (node) ? node : TREE_TYPE (node);
529 /* Check for std::allocator. */
530 if (decl
531 && is_std_substitution (decl, SUBID_ALLOCATOR)
532 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
534 write_string ("Sa");
535 return 1;
538 /* Check for std::basic_string. */
539 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
541 if (TYPE_P (node))
543 /* If this is a type (i.e. a fully-qualified template-id),
544 check for
545 std::basic_string <char,
546 std::char_traits<char>,
547 std::allocator<char> > . */
548 if (cp_type_quals (type) == TYPE_UNQUALIFIED
549 && CLASSTYPE_USE_TEMPLATE (type))
551 tree args = CLASSTYPE_TI_ARGS (type);
552 if (TREE_VEC_LENGTH (args) == 3
553 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
554 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
555 SUBID_CHAR_TRAITS)
556 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
557 SUBID_ALLOCATOR))
559 write_string ("Ss");
560 return 1;
564 else
565 /* Substitute for the template name only if this isn't a type. */
567 write_string ("Sb");
568 return 1;
572 /* Check for basic_{i,o,io}stream. */
573 if (TYPE_P (node)
574 && cp_type_quals (type) == TYPE_UNQUALIFIED
575 && CLASS_TYPE_P (type)
576 && CLASSTYPE_USE_TEMPLATE (type)
577 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
579 /* First, check for the template
580 args <char, std::char_traits<char> > . */
581 tree args = CLASSTYPE_TI_ARGS (type);
582 if (TREE_VEC_LENGTH (args) == 2
583 && TYPE_P (TREE_VEC_ELT (args, 0))
584 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
585 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
586 SUBID_CHAR_TRAITS))
588 /* Got them. Is this basic_istream? */
589 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
591 write_string ("Si");
592 return 1;
594 /* Or basic_ostream? */
595 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
597 write_string ("So");
598 return 1;
600 /* Or basic_iostream? */
601 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
603 write_string ("Sd");
604 return 1;
609 /* Check for namespace std. */
610 if (decl && DECL_NAMESPACE_STD_P (decl))
612 write_string ("St");
613 return 1;
616 /* Now check the list of available substitutions for this mangling
617 operation. */
618 for (i = 0; i < size; ++i)
620 tree candidate = (*G.substitutions)[i];
621 /* NODE is a matched to a candidate if it's the same decl node or
622 if it's the same type. */
623 if (decl == candidate
624 || (TYPE_P (candidate) && type && TYPE_P (node)
625 && same_type_p (type, candidate))
626 || NESTED_TEMPLATE_MATCH (node, candidate))
628 write_substitution (i);
629 return 1;
633 /* No substitution found. */
634 return 0;
638 /* TOP_LEVEL is true, if this is being called at outermost level of
639 mangling. It should be false when mangling a decl appearing in an
640 expression within some other mangling.
642 <mangled-name> ::= _Z <encoding> */
644 static void
645 write_mangled_name (const tree decl, bool top_level)
647 MANGLE_TRACE_TREE ("mangled-name", decl);
649 if (/* The names of `extern "C"' functions are not mangled. */
650 DECL_EXTERN_C_FUNCTION_P (decl)
651 /* But overloaded operator names *are* mangled. */
652 && !DECL_OVERLOADED_OPERATOR_P (decl))
654 unmangled_name:;
656 if (top_level)
657 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
658 else
660 /* The standard notes: "The <encoding> of an extern "C"
661 function is treated like global-scope data, i.e. as its
662 <source-name> without a type." We cannot write
663 overloaded operators that way though, because it contains
664 characters invalid in assembler. */
665 if (abi_version_at_least (2))
666 write_string ("_Z");
667 else
668 G.need_abi_warning = true;
669 write_source_name (DECL_NAME (decl));
672 else if (VAR_P (decl)
673 /* The names of non-static global variables aren't mangled. */
674 && DECL_EXTERNAL_LINKAGE_P (decl)
675 && (CP_DECL_CONTEXT (decl) == global_namespace
676 /* And neither are `extern "C"' variables. */
677 || DECL_EXTERN_C_P (decl)))
679 if (top_level || abi_version_at_least (2))
680 goto unmangled_name;
681 else
683 G.need_abi_warning = true;
684 goto mangled_name;
687 else
689 mangled_name:;
690 write_string ("_Z");
691 write_encoding (decl);
695 /* <encoding> ::= <function name> <bare-function-type>
696 ::= <data name> */
698 static void
699 write_encoding (const tree decl)
701 MANGLE_TRACE_TREE ("encoding", decl);
703 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
705 /* For overloaded operators write just the mangled name
706 without arguments. */
707 if (DECL_OVERLOADED_OPERATOR_P (decl))
708 write_name (decl, /*ignore_local_scope=*/0);
709 else
710 write_source_name (DECL_NAME (decl));
711 return;
714 write_name (decl, /*ignore_local_scope=*/0);
715 if (TREE_CODE (decl) == FUNCTION_DECL)
717 tree fn_type;
718 tree d;
720 if (decl_is_template_id (decl, NULL))
722 fn_type = get_mostly_instantiated_function_type (decl);
723 /* FN_TYPE will not have parameter types for in-charge or
724 VTT parameters. Therefore, we pass NULL_TREE to
725 write_bare_function_type -- otherwise, it will get
726 confused about which artificial parameters to skip. */
727 d = NULL_TREE;
729 else
731 fn_type = TREE_TYPE (decl);
732 d = decl;
735 write_bare_function_type (fn_type,
736 (!DECL_CONSTRUCTOR_P (decl)
737 && !DECL_DESTRUCTOR_P (decl)
738 && !DECL_CONV_FN_P (decl)
739 && decl_is_template_id (decl, NULL)),
744 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
745 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
747 static tree
748 decl_mangling_context (tree decl)
750 tree tcontext = targetm.cxx.decl_mangling_context (decl);
752 if (tcontext != NULL_TREE)
753 return tcontext;
755 if (TREE_CODE (decl) == TYPE_DECL
756 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
758 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
759 if (extra)
760 return extra;
762 else if (TREE_CODE (decl) == TYPE_DECL
763 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
764 /* template type parms have no mangling context. */
765 return NULL_TREE;
766 return CP_DECL_CONTEXT (decl);
769 /* <name> ::= <unscoped-name>
770 ::= <unscoped-template-name> <template-args>
771 ::= <nested-name>
772 ::= <local-name>
774 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
775 called from <local-name>, which mangles the enclosing scope
776 elsewhere and then uses this function to mangle just the part
777 underneath the function scope. So don't use the <local-name>
778 production, to avoid an infinite recursion. */
780 static void
781 write_name (tree decl, const int ignore_local_scope)
783 tree context;
785 MANGLE_TRACE_TREE ("name", decl);
787 if (TREE_CODE (decl) == TYPE_DECL)
789 /* In case this is a typedef, fish out the corresponding
790 TYPE_DECL for the main variant. */
791 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
794 context = decl_mangling_context (decl);
796 gcc_assert (context != NULL_TREE);
798 /* A decl in :: or ::std scope is treated specially. The former is
799 mangled using <unscoped-name> or <unscoped-template-name>, the
800 latter with a special substitution. Also, a name that is
801 directly in a local function scope is also mangled with
802 <unscoped-name> rather than a full <nested-name>. */
803 if (context == global_namespace
804 || DECL_NAMESPACE_STD_P (context)
805 || (ignore_local_scope
806 && (TREE_CODE (context) == FUNCTION_DECL
807 || (abi_version_at_least (7)
808 && TREE_CODE (context) == PARM_DECL))))
810 tree template_info;
811 /* Is this a template instance? */
812 if (decl_is_template_id (decl, &template_info))
814 /* Yes: use <unscoped-template-name>. */
815 write_unscoped_template_name (TI_TEMPLATE (template_info));
816 write_template_args (TI_ARGS (template_info));
818 else
819 /* Everything else gets an <unqualified-name>. */
820 write_unscoped_name (decl);
822 else
824 /* Handle local names, unless we asked not to (that is, invoked
825 under <local-name>, to handle only the part of the name under
826 the local scope). */
827 if (!ignore_local_scope)
829 /* Scan up the list of scope context, looking for a
830 function. If we find one, this entity is in local
831 function scope. local_entity tracks context one scope
832 level down, so it will contain the element that's
833 directly in that function's scope, either decl or one of
834 its enclosing scopes. */
835 tree local_entity = decl;
836 while (context != global_namespace)
838 /* Make sure we're always dealing with decls. */
839 if (TYPE_P (context))
840 context = TYPE_NAME (context);
841 /* Is this a function? */
842 if (TREE_CODE (context) == FUNCTION_DECL
843 || TREE_CODE (context) == PARM_DECL)
845 /* Yes, we have local scope. Use the <local-name>
846 production for the innermost function scope. */
847 write_local_name (context, local_entity, decl);
848 return;
850 /* Up one scope level. */
851 local_entity = context;
852 context = decl_mangling_context (context);
855 /* No local scope found? Fall through to <nested-name>. */
858 /* Other decls get a <nested-name> to encode their scope. */
859 write_nested_name (decl);
863 /* <unscoped-name> ::= <unqualified-name>
864 ::= St <unqualified-name> # ::std:: */
866 static void
867 write_unscoped_name (const tree decl)
869 tree context = decl_mangling_context (decl);
871 MANGLE_TRACE_TREE ("unscoped-name", decl);
873 /* Is DECL in ::std? */
874 if (DECL_NAMESPACE_STD_P (context))
876 write_string ("St");
877 write_unqualified_name (decl);
879 else
881 /* If not, it should be either in the global namespace, or directly
882 in a local function scope. A lambda can also be mangled in the
883 scope of a default argument. */
884 gcc_assert (context == global_namespace
885 || TREE_CODE (context) == PARM_DECL
886 || TREE_CODE (context) == FUNCTION_DECL);
888 write_unqualified_name (decl);
892 /* <unscoped-template-name> ::= <unscoped-name>
893 ::= <substitution> */
895 static void
896 write_unscoped_template_name (const tree decl)
898 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
900 if (find_substitution (decl))
901 return;
902 write_unscoped_name (decl);
903 add_substitution (decl);
906 /* Write the nested name, including CV-qualifiers, of DECL.
908 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
909 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
911 <ref-qualifier> ::= R # & ref-qualifier
912 ::= O # && ref-qualifier
913 <CV-qualifiers> ::= [r] [V] [K] */
915 static void
916 write_nested_name (const tree decl)
918 tree template_info;
920 MANGLE_TRACE_TREE ("nested-name", decl);
922 write_char ('N');
924 /* Write CV-qualifiers, if this is a member function. */
925 if (TREE_CODE (decl) == FUNCTION_DECL
926 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
928 if (DECL_VOLATILE_MEMFUNC_P (decl))
929 write_char ('V');
930 if (DECL_CONST_MEMFUNC_P (decl))
931 write_char ('K');
932 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
934 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
935 write_char ('O');
936 else
937 write_char ('R');
941 /* Is this a template instance? */
942 if (decl_is_template_id (decl, &template_info))
944 /* Yes, use <template-prefix>. */
945 write_template_prefix (decl);
946 write_template_args (TI_ARGS (template_info));
948 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
950 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
951 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
953 write_template_prefix (decl);
954 write_template_args (TREE_OPERAND (name, 1));
956 else
958 write_prefix (decl_mangling_context (decl));
959 write_unqualified_name (decl);
962 else
964 /* No, just use <prefix> */
965 write_prefix (decl_mangling_context (decl));
966 write_unqualified_name (decl);
968 write_char ('E');
971 /* <prefix> ::= <prefix> <unqualified-name>
972 ::= <template-param>
973 ::= <template-prefix> <template-args>
974 ::= <decltype>
975 ::= # empty
976 ::= <substitution> */
978 static void
979 write_prefix (const tree node)
981 tree decl;
982 /* Non-NULL if NODE represents a template-id. */
983 tree template_info = NULL;
985 if (node == NULL
986 || node == global_namespace)
987 return;
989 MANGLE_TRACE_TREE ("prefix", node);
991 if (TREE_CODE (node) == DECLTYPE_TYPE)
993 write_type (node);
994 return;
997 if (find_substitution (node))
998 return;
1000 if (DECL_P (node))
1002 /* If this is a function or parm decl, that means we've hit function
1003 scope, so this prefix must be for a local name. In this
1004 case, we're under the <local-name> production, which encodes
1005 the enclosing function scope elsewhere. So don't continue
1006 here. */
1007 if (TREE_CODE (node) == FUNCTION_DECL
1008 || TREE_CODE (node) == PARM_DECL)
1009 return;
1011 decl = node;
1012 decl_is_template_id (decl, &template_info);
1014 else
1016 /* Node is a type. */
1017 decl = TYPE_NAME (node);
1018 if (CLASSTYPE_TEMPLATE_ID_P (node))
1019 template_info = TYPE_TEMPLATE_INFO (node);
1022 /* In G++ 3.2, the name of the template parameter was used. */
1023 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1024 && !abi_version_at_least (2))
1025 G.need_abi_warning = true;
1027 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1028 && abi_version_at_least (2))
1029 write_template_param (node);
1030 else if (template_info != NULL)
1031 /* Templated. */
1033 write_template_prefix (decl);
1034 write_template_args (TI_ARGS (template_info));
1036 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1038 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1039 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1041 write_template_prefix (decl);
1042 write_template_args (TREE_OPERAND (name, 1));
1044 else
1046 write_prefix (decl_mangling_context (decl));
1047 write_unqualified_name (decl);
1050 else
1051 /* Not templated. */
1053 write_prefix (decl_mangling_context (decl));
1054 write_unqualified_name (decl);
1055 if (VAR_P (decl)
1056 || TREE_CODE (decl) == FIELD_DECL)
1058 /* <data-member-prefix> := <member source-name> M */
1059 write_char ('M');
1060 return;
1064 add_substitution (node);
1067 /* <template-prefix> ::= <prefix> <template component>
1068 ::= <template-param>
1069 ::= <substitution> */
1071 static void
1072 write_template_prefix (const tree node)
1074 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1075 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1076 tree context = decl_mangling_context (decl);
1077 tree template_info;
1078 tree templ;
1079 tree substitution;
1081 MANGLE_TRACE_TREE ("template-prefix", node);
1083 /* Find the template decl. */
1084 if (decl_is_template_id (decl, &template_info))
1085 templ = TI_TEMPLATE (template_info);
1086 else if (TREE_CODE (type) == TYPENAME_TYPE)
1087 /* For a typename type, all we have is the name. */
1088 templ = DECL_NAME (decl);
1089 else
1091 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1093 templ = TYPE_TI_TEMPLATE (type);
1096 /* For a member template, though, the template name for the
1097 innermost name must have all the outer template levels
1098 instantiated. For instance, consider
1100 template<typename T> struct Outer {
1101 template<typename U> struct Inner {};
1104 The template name for `Inner' in `Outer<int>::Inner<float>' is
1105 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1106 levels separately, so there's no TEMPLATE_DECL available for this
1107 (there's only `Outer<T>::Inner<U>').
1109 In order to get the substitutions right, we create a special
1110 TREE_LIST to represent the substitution candidate for a nested
1111 template. The TREE_PURPOSE is the template's context, fully
1112 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1113 template.
1115 So, for the example above, `Outer<int>::Inner' is represented as a
1116 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1117 and whose value is `Outer<T>::Inner<U>'. */
1118 if (TYPE_P (context))
1119 substitution = build_tree_list (context, templ);
1120 else
1121 substitution = templ;
1123 if (find_substitution (substitution))
1124 return;
1126 /* In G++ 3.2, the name of the template template parameter was used. */
1127 if (TREE_TYPE (templ)
1128 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1129 && !abi_version_at_least (2))
1130 G.need_abi_warning = true;
1132 if (TREE_TYPE (templ)
1133 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1134 && abi_version_at_least (2))
1135 write_template_param (TREE_TYPE (templ));
1136 else
1138 write_prefix (context);
1139 write_unqualified_name (decl);
1142 add_substitution (substitution);
1145 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1146 mangled through special entry points.
1148 <unqualified-name> ::= <operator-name>
1149 ::= <special-name>
1150 ::= <source-name>
1151 ::= <unnamed-type-name>
1152 ::= <local-source-name>
1154 <local-source-name> ::= L <source-name> <discriminator> */
1156 static void
1157 write_unqualified_id (tree identifier)
1159 if (IDENTIFIER_TYPENAME_P (identifier))
1160 write_conversion_operator_name (TREE_TYPE (identifier));
1161 else if (IDENTIFIER_OPNAME_P (identifier))
1163 int i;
1164 const char *mangled_name = NULL;
1166 /* Unfortunately, there is no easy way to go from the
1167 name of the operator back to the corresponding tree
1168 code. */
1169 for (i = 0; i < MAX_TREE_CODES; ++i)
1170 if (operator_name_info[i].identifier == identifier)
1172 /* The ABI says that we prefer binary operator
1173 names to unary operator names. */
1174 if (operator_name_info[i].arity == 2)
1176 mangled_name = operator_name_info[i].mangled_name;
1177 break;
1179 else if (!mangled_name)
1180 mangled_name = operator_name_info[i].mangled_name;
1182 else if (assignment_operator_name_info[i].identifier
1183 == identifier)
1185 mangled_name
1186 = assignment_operator_name_info[i].mangled_name;
1187 break;
1189 write_string (mangled_name);
1191 else if (UDLIT_OPER_P (identifier))
1192 write_literal_operator_name (identifier);
1193 else
1194 write_source_name (identifier);
1197 static void
1198 write_unqualified_name (const tree decl)
1200 MANGLE_TRACE_TREE ("unqualified-name", decl);
1202 if (identifier_p (decl))
1204 write_unqualified_id (decl);
1205 return;
1208 bool found = false;
1210 if (DECL_NAME (decl) == NULL_TREE)
1212 found = true;
1213 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1214 write_source_name (DECL_ASSEMBLER_NAME (decl));
1216 else if (DECL_DECLARES_FUNCTION_P (decl))
1218 found = true;
1219 if (DECL_CONSTRUCTOR_P (decl))
1220 write_special_name_constructor (decl);
1221 else if (DECL_DESTRUCTOR_P (decl))
1222 write_special_name_destructor (decl);
1223 else if (DECL_CONV_FN_P (decl))
1225 /* Conversion operator. Handle it right here.
1226 <operator> ::= cv <type> */
1227 tree type;
1228 if (decl_is_template_id (decl, NULL))
1230 tree fn_type;
1231 fn_type = get_mostly_instantiated_function_type (decl);
1232 type = TREE_TYPE (fn_type);
1234 else if (FNDECL_USED_AUTO (decl))
1235 type = (DECL_STRUCT_FUNCTION (decl)->language
1236 ->x_auto_return_pattern);
1237 else
1238 type = DECL_CONV_FN_TYPE (decl);
1239 write_conversion_operator_name (type);
1241 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1243 operator_name_info_t *oni;
1244 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1245 oni = assignment_operator_name_info;
1246 else
1247 oni = operator_name_info;
1249 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1251 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1252 write_literal_operator_name (DECL_NAME (decl));
1253 else
1254 found = false;
1257 if (found)
1258 /* OK */;
1259 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1260 && DECL_NAMESPACE_SCOPE_P (decl)
1261 && decl_linkage (decl) == lk_internal)
1263 MANGLE_TRACE_TREE ("local-source-name", decl);
1264 write_char ('L');
1265 write_source_name (DECL_NAME (decl));
1266 /* The default discriminator is 1, and that's all we ever use,
1267 so there's no code to output one here. */
1269 else
1271 tree type = TREE_TYPE (decl);
1273 if (TREE_CODE (decl) == TYPE_DECL
1274 && TYPE_ANONYMOUS_P (type))
1275 write_unnamed_type_name (type);
1276 else if (TREE_CODE (decl) == TYPE_DECL
1277 && LAMBDA_TYPE_P (type))
1278 write_closure_type_name (type);
1279 else
1280 write_source_name (DECL_NAME (decl));
1283 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1284 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1285 : DECL_ATTRIBUTES (decl));
1286 write_abi_tags (lookup_attribute ("abi_tag", attrs));
1289 /* Write the unqualified-name for a conversion operator to TYPE. */
1291 static void
1292 write_conversion_operator_name (const tree type)
1294 write_string ("cv");
1295 write_type (type);
1298 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1300 <source-name> ::= </length/ number> <identifier> */
1302 static void
1303 write_source_name (tree identifier)
1305 MANGLE_TRACE_TREE ("source-name", identifier);
1307 /* Never write the whole template-id name including the template
1308 arguments; we only want the template name. */
1309 if (IDENTIFIER_TEMPLATE (identifier))
1310 identifier = IDENTIFIER_TEMPLATE (identifier);
1312 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1313 write_identifier (IDENTIFIER_POINTER (identifier));
1316 /* Compare two TREE_STRINGs like strcmp. */
1319 tree_string_cmp (const void *p1, const void *p2)
1321 if (p1 == p2)
1322 return 0;
1323 tree s1 = *(const tree*)p1;
1324 tree s2 = *(const tree*)p2;
1325 return strcmp (TREE_STRING_POINTER (s1),
1326 TREE_STRING_POINTER (s2));
1329 /* ID is the name of a function or type with abi_tags attribute TAGS.
1330 Write out the name, suitably decorated. */
1332 static void
1333 write_abi_tags (tree tags)
1335 if (tags == NULL_TREE)
1336 return;
1338 tags = TREE_VALUE (tags);
1340 vec<tree, va_gc> * vec = make_tree_vector();
1342 for (tree t = tags; t; t = TREE_CHAIN (t))
1344 if (ABI_TAG_IMPLICIT (t))
1345 continue;
1346 tree str = TREE_VALUE (t);
1347 vec_safe_push (vec, str);
1350 vec->qsort (tree_string_cmp);
1352 unsigned i; tree str;
1353 FOR_EACH_VEC_ELT (*vec, i, str)
1355 write_string ("B");
1356 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1357 write_identifier (TREE_STRING_POINTER (str));
1360 release_tree_vector (vec);
1363 /* Write a user-defined literal operator.
1364 ::= li <source-name> # "" <source-name>
1365 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1367 static void
1368 write_literal_operator_name (tree identifier)
1370 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1371 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1372 write_unsigned_number (strlen (suffix));
1373 write_identifier (suffix);
1376 /* Encode 0 as _, and 1+ as n-1_. */
1378 static void
1379 write_compact_number (int num)
1381 if (num > 0)
1382 write_unsigned_number (num - 1);
1383 write_char ('_');
1386 /* Return how many unnamed types precede TYPE in its enclosing class. */
1388 static int
1389 nested_anon_class_index (tree type)
1391 int index = 0;
1392 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1393 for (; member; member = DECL_CHAIN (member))
1394 if (DECL_IMPLICIT_TYPEDEF_P (member))
1396 tree memtype = TREE_TYPE (member);
1397 if (memtype == type)
1398 return index;
1399 else if (TYPE_ANONYMOUS_P (memtype))
1400 ++index;
1403 gcc_unreachable ();
1406 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1408 static void
1409 write_unnamed_type_name (const tree type)
1411 int discriminator;
1412 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1414 if (TYPE_FUNCTION_SCOPE_P (type))
1415 discriminator = local_class_index (type);
1416 else if (TYPE_CLASS_SCOPE_P (type))
1417 discriminator = nested_anon_class_index (type);
1418 else
1420 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1421 /* Just use the old mangling at namespace scope. */
1422 write_source_name (TYPE_IDENTIFIER (type));
1423 return;
1426 write_string ("Ut");
1427 write_compact_number (discriminator);
1430 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1431 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1433 static void
1434 write_closure_type_name (const tree type)
1436 tree fn = lambda_function (type);
1437 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1438 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1440 MANGLE_TRACE_TREE ("closure-type-name", type);
1442 write_string ("Ul");
1443 write_method_parms (parms, /*method_p=*/1, fn);
1444 write_char ('E');
1445 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1448 /* Convert NUMBER to ascii using base BASE and generating at least
1449 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1450 into which to store the characters. Returns the number of
1451 characters generated (these will be laid out in advance of where
1452 BUFFER points). */
1454 static int
1455 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1456 char *buffer, const unsigned int min_digits)
1458 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1459 unsigned digits = 0;
1461 while (number)
1463 unsigned HOST_WIDE_INT d = number / base;
1465 *--buffer = base_digits[number - d * base];
1466 digits++;
1467 number = d;
1469 while (digits < min_digits)
1471 *--buffer = base_digits[0];
1472 digits++;
1474 return digits;
1477 /* Non-terminal <number>.
1479 <number> ::= [n] </decimal integer/> */
1481 static void
1482 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1483 const unsigned int base)
1485 char buffer[sizeof (HOST_WIDE_INT) * 8];
1486 unsigned count = 0;
1488 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1490 write_char ('n');
1491 number = -((HOST_WIDE_INT) number);
1493 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1494 write_chars (buffer + sizeof (buffer) - count, count);
1497 /* Write out an integral CST in decimal. Most numbers are small, and
1498 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1499 bigger than that, which we must deal with. */
1501 static inline void
1502 write_integer_cst (const tree cst)
1504 int sign = tree_int_cst_sgn (cst);
1506 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1508 /* A bignum. We do this in chunks, each of which fits in a
1509 HOST_WIDE_INT. */
1510 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1511 unsigned HOST_WIDE_INT chunk;
1512 unsigned chunk_digits;
1513 char *ptr = buffer + sizeof (buffer);
1514 unsigned count = 0;
1515 tree n, base, type;
1516 int done;
1518 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1519 representable. */
1520 chunk = 1000000000;
1521 chunk_digits = 9;
1523 if (sizeof (HOST_WIDE_INT) >= 8)
1525 /* It is at least 64 bits, so 10^18 is representable. */
1526 chunk_digits = 18;
1527 chunk *= chunk;
1530 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1531 base = build_int_cstu (type, chunk);
1532 n = build_int_cst_wide (type,
1533 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1535 if (sign < 0)
1537 write_char ('n');
1538 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1542 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1543 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1544 unsigned c;
1546 done = integer_zerop (d);
1547 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1548 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1549 done ? 1 : chunk_digits);
1550 ptr -= c;
1551 count += c;
1552 n = d;
1554 while (!done);
1555 write_chars (ptr, count);
1557 else
1559 /* A small num. */
1560 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1562 if (sign < 0)
1564 write_char ('n');
1565 low = -low;
1567 write_unsigned_number (low);
1571 /* Write out a floating-point literal.
1573 "Floating-point literals are encoded using the bit pattern of the
1574 target processor's internal representation of that number, as a
1575 fixed-length lowercase hexadecimal string, high-order bytes first
1576 (even if the target processor would store low-order bytes first).
1577 The "n" prefix is not used for floating-point literals; the sign
1578 bit is encoded with the rest of the number.
1580 Here are some examples, assuming the IEEE standard representation
1581 for floating point numbers. (Spaces are for readability, not
1582 part of the encoding.)
1584 1.0f Lf 3f80 0000 E
1585 -1.0f Lf bf80 0000 E
1586 1.17549435e-38f Lf 0080 0000 E
1587 1.40129846e-45f Lf 0000 0001 E
1588 0.0f Lf 0000 0000 E"
1590 Caller is responsible for the Lx and the E. */
1591 static void
1592 write_real_cst (const tree value)
1594 if (abi_version_at_least (2))
1596 long target_real[4]; /* largest supported float */
1597 char buffer[9]; /* eight hex digits in a 32-bit number */
1598 int i, limit, dir;
1600 tree type = TREE_TYPE (value);
1601 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1603 real_to_target (target_real, &TREE_REAL_CST (value),
1604 TYPE_MODE (type));
1606 /* The value in target_real is in the target word order,
1607 so we must write it out backward if that happens to be
1608 little-endian. write_number cannot be used, it will
1609 produce uppercase. */
1610 if (FLOAT_WORDS_BIG_ENDIAN)
1611 i = 0, limit = words, dir = 1;
1612 else
1613 i = words - 1, limit = -1, dir = -1;
1615 for (; i != limit; i += dir)
1617 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1618 write_chars (buffer, 8);
1621 else
1623 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1624 literally. Note that compatibility with 3.2 is impossible,
1625 because the old floating-point emulator used a different
1626 format for REAL_VALUE_TYPE. */
1627 size_t i;
1628 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1629 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1630 /*unsigned_p*/ 1,
1631 /*base*/ 16);
1632 G.need_abi_warning = 1;
1636 /* Non-terminal <identifier>.
1638 <identifier> ::= </unqualified source code identifier> */
1640 static void
1641 write_identifier (const char *identifier)
1643 MANGLE_TRACE ("identifier", identifier);
1644 write_string (identifier);
1647 /* Handle constructor productions of non-terminal <special-name>.
1648 CTOR is a constructor FUNCTION_DECL.
1650 <special-name> ::= C1 # complete object constructor
1651 ::= C2 # base object constructor
1652 ::= C3 # complete object allocating constructor
1654 Currently, allocating constructors are never used. */
1656 static void
1657 write_special_name_constructor (const tree ctor)
1659 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1660 write_string ("C2");
1661 /* This is the old-style "[unified]" constructor.
1662 In some cases, we may emit this function and call
1663 it from the clones in order to share code and save space. */
1664 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1665 write_string ("C4");
1666 else
1668 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1669 write_string ("C1");
1673 /* Handle destructor productions of non-terminal <special-name>.
1674 DTOR is a destructor FUNCTION_DECL.
1676 <special-name> ::= D0 # deleting (in-charge) destructor
1677 ::= D1 # complete object (in-charge) destructor
1678 ::= D2 # base object (not-in-charge) destructor */
1680 static void
1681 write_special_name_destructor (const tree dtor)
1683 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1684 write_string ("D0");
1685 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1686 write_string ("D2");
1687 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1688 /* This is the old-style "[unified]" destructor.
1689 In some cases, we may emit this function and call
1690 it from the clones in order to share code and save space. */
1691 write_string ("D4");
1692 else
1694 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1695 write_string ("D1");
1699 /* Scan the vector of local classes and return how many others with the
1700 same name (or same no name) and context precede ENTITY. */
1702 static int
1703 local_class_index (tree entity)
1705 int ix, discriminator = 0;
1706 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1707 : TYPE_IDENTIFIER (entity));
1708 tree ctx = TYPE_CONTEXT (entity);
1709 for (ix = 0; ; ix++)
1711 tree type = (*local_classes)[ix];
1712 if (type == entity)
1713 return discriminator;
1714 if (TYPE_CONTEXT (type) == ctx
1715 && (name ? TYPE_IDENTIFIER (type) == name
1716 : TYPE_ANONYMOUS_P (type)))
1717 ++discriminator;
1719 gcc_unreachable ();
1722 /* Return the discriminator for ENTITY appearing inside
1723 FUNCTION. The discriminator is the lexical ordinal of VAR among
1724 entities with the same name in the same FUNCTION. */
1726 static int
1727 discriminator_for_local_entity (tree entity)
1729 if (DECL_DISCRIMINATOR_P (entity))
1731 if (DECL_DISCRIMINATOR_SET_P (entity))
1732 return DECL_DISCRIMINATOR (entity);
1733 else
1734 /* The first entity with a particular name doesn't get
1735 DECL_DISCRIMINATOR set up. */
1736 return 0;
1738 else if (TREE_CODE (entity) == TYPE_DECL)
1740 /* Scan the list of local classes. */
1741 entity = TREE_TYPE (entity);
1743 /* Lambdas and unnamed types have their own discriminators. */
1744 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1745 return 0;
1747 return local_class_index (entity);
1749 else
1750 gcc_unreachable ();
1753 /* Return the discriminator for STRING, a string literal used inside
1754 FUNCTION. The discriminator is the lexical ordinal of STRING among
1755 string literals used in FUNCTION. */
1757 static int
1758 discriminator_for_string_literal (tree /*function*/,
1759 tree /*string*/)
1761 /* For now, we don't discriminate amongst string literals. */
1762 return 0;
1765 /* <discriminator> := _ <number>
1767 The discriminator is used only for the second and later occurrences
1768 of the same name within a single function. In this case <number> is
1769 n - 2, if this is the nth occurrence, in lexical order. */
1771 static void
1772 write_discriminator (const int discriminator)
1774 /* If discriminator is zero, don't write anything. Otherwise... */
1775 if (discriminator > 0)
1777 write_char ('_');
1778 write_unsigned_number (discriminator - 1);
1782 /* Mangle the name of a function-scope entity. FUNCTION is the
1783 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1784 default argument scope. ENTITY is the decl for the entity itself.
1785 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1786 either ENTITY itself or an enclosing scope of ENTITY.
1788 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1789 := Z <function encoding> E s [<discriminator>]
1790 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1792 static void
1793 write_local_name (tree function, const tree local_entity,
1794 const tree entity)
1796 tree parm = NULL_TREE;
1798 MANGLE_TRACE_TREE ("local-name", entity);
1800 if (TREE_CODE (function) == PARM_DECL)
1802 parm = function;
1803 function = DECL_CONTEXT (parm);
1806 write_char ('Z');
1807 write_encoding (function);
1808 write_char ('E');
1810 /* For this purpose, parameters are numbered from right-to-left. */
1811 if (parm)
1813 tree t;
1814 int i = 0;
1815 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1817 if (t == parm)
1818 i = 1;
1819 else if (i)
1820 ++i;
1822 write_char ('d');
1823 write_compact_number (i - 1);
1826 if (TREE_CODE (entity) == STRING_CST)
1828 write_char ('s');
1829 write_discriminator (discriminator_for_string_literal (function,
1830 entity));
1832 else
1834 /* Now the <entity name>. Let write_name know its being called
1835 from <local-name>, so it doesn't try to process the enclosing
1836 function scope again. */
1837 write_name (entity, /*ignore_local_scope=*/1);
1838 write_discriminator (discriminator_for_local_entity (local_entity));
1842 /* Non-terminals <type> and <CV-qualifier>.
1844 <type> ::= <builtin-type>
1845 ::= <function-type>
1846 ::= <class-enum-type>
1847 ::= <array-type>
1848 ::= <pointer-to-member-type>
1849 ::= <template-param>
1850 ::= <substitution>
1851 ::= <CV-qualifier>
1852 ::= P <type> # pointer-to
1853 ::= R <type> # reference-to
1854 ::= C <type> # complex pair (C 2000)
1855 ::= G <type> # imaginary (C 2000) [not supported]
1856 ::= U <source-name> <type> # vendor extended type qualifier
1858 C++0x extensions
1860 <type> ::= RR <type> # rvalue reference-to
1861 <type> ::= Dt <expression> # decltype of an id-expression or
1862 # class member access
1863 <type> ::= DT <expression> # decltype of an expression
1864 <type> ::= Dn # decltype of nullptr
1866 TYPE is a type node. */
1868 static void
1869 write_type (tree type)
1871 /* This gets set to nonzero if TYPE turns out to be a (possibly
1872 CV-qualified) builtin type. */
1873 int is_builtin_type = 0;
1875 MANGLE_TRACE_TREE ("type", type);
1877 if (type == error_mark_node)
1878 return;
1880 type = canonicalize_for_substitution (type);
1881 if (find_substitution (type))
1882 return;
1885 if (write_CV_qualifiers_for_type (type) > 0)
1886 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1887 mangle the unqualified type. The recursive call is needed here
1888 since both the qualified and unqualified types are substitution
1889 candidates. */
1891 tree t = TYPE_MAIN_VARIANT (type);
1892 if (TREE_CODE (t) == FUNCTION_TYPE
1893 || TREE_CODE (t) == METHOD_TYPE)
1895 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1896 if (abi_version_at_least (8))
1897 /* Avoid adding the unqualified function type as a substitution. */
1898 write_function_type (t);
1899 else
1900 write_type (t);
1902 else
1903 write_type (t);
1905 else if (TREE_CODE (type) == ARRAY_TYPE)
1906 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1907 so that the cv-qualification of the element type is available
1908 in write_array_type. */
1909 write_array_type (type);
1910 else
1912 tree type_orig = type;
1914 /* See through any typedefs. */
1915 type = TYPE_MAIN_VARIANT (type);
1916 if (TREE_CODE (type) == FUNCTION_TYPE
1917 || TREE_CODE (type) == METHOD_TYPE)
1918 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1920 /* According to the C++ ABI, some library classes are passed the
1921 same as the scalar type of their single member and use the same
1922 mangling. */
1923 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1924 type = TREE_TYPE (first_field (type));
1926 if (TYPE_PTRDATAMEM_P (type))
1927 write_pointer_to_member_type (type);
1928 else
1930 /* Handle any target-specific fundamental types. */
1931 const char *target_mangling
1932 = targetm.mangle_type (type_orig);
1934 if (target_mangling)
1936 write_string (target_mangling);
1937 /* Add substitutions for types other than fundamental
1938 types. */
1939 if (!VOID_TYPE_P (type)
1940 && TREE_CODE (type) != INTEGER_TYPE
1941 && TREE_CODE (type) != REAL_TYPE
1942 && TREE_CODE (type) != BOOLEAN_TYPE)
1943 add_substitution (type);
1944 return;
1947 switch (TREE_CODE (type))
1949 case VOID_TYPE:
1950 case BOOLEAN_TYPE:
1951 case INTEGER_TYPE: /* Includes wchar_t. */
1952 case REAL_TYPE:
1953 case FIXED_POINT_TYPE:
1955 /* If this is a typedef, TYPE may not be one of
1956 the standard builtin type nodes, but an alias of one. Use
1957 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1958 write_builtin_type (TYPE_MAIN_VARIANT (type));
1959 ++is_builtin_type;
1961 break;
1963 case COMPLEX_TYPE:
1964 write_char ('C');
1965 write_type (TREE_TYPE (type));
1966 break;
1968 case FUNCTION_TYPE:
1969 case METHOD_TYPE:
1970 write_function_type (type);
1971 break;
1973 case UNION_TYPE:
1974 case RECORD_TYPE:
1975 case ENUMERAL_TYPE:
1976 /* A pointer-to-member function is represented as a special
1977 RECORD_TYPE, so check for this first. */
1978 if (TYPE_PTRMEMFUNC_P (type))
1979 write_pointer_to_member_type (type);
1980 else
1981 write_class_enum_type (type);
1982 break;
1984 case TYPENAME_TYPE:
1985 case UNBOUND_CLASS_TEMPLATE:
1986 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1987 ordinary nested names. */
1988 write_nested_name (TYPE_STUB_DECL (type));
1989 break;
1991 case POINTER_TYPE:
1992 case REFERENCE_TYPE:
1993 if (TYPE_PTR_P (type))
1994 write_char ('P');
1995 else if (TYPE_REF_IS_RVALUE (type))
1996 write_char ('O');
1997 else
1998 write_char ('R');
2000 tree target = TREE_TYPE (type);
2001 /* Attribute const/noreturn are not reflected in mangling.
2002 We strip them here rather than at a lower level because
2003 a typedef or template argument can have function type
2004 with function-cv-quals (that use the same representation),
2005 but you can't have a pointer/reference to such a type. */
2006 if (abi_version_at_least (5)
2007 && TREE_CODE (target) == FUNCTION_TYPE)
2008 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2009 write_type (target);
2011 break;
2013 case TEMPLATE_TYPE_PARM:
2014 if (is_auto (type))
2016 if (AUTO_IS_DECLTYPE (type))
2017 write_identifier ("Dc");
2018 else
2019 write_identifier ("Da");
2020 ++is_builtin_type;
2021 break;
2023 /* else fall through. */
2024 case TEMPLATE_PARM_INDEX:
2025 write_template_param (type);
2026 break;
2028 case TEMPLATE_TEMPLATE_PARM:
2029 write_template_template_param (type);
2030 break;
2032 case BOUND_TEMPLATE_TEMPLATE_PARM:
2033 write_template_template_param (type);
2034 write_template_args
2035 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2036 break;
2038 case VECTOR_TYPE:
2039 if (abi_version_at_least (4))
2041 write_string ("Dv");
2042 /* Non-constant vector size would be encoded with
2043 _ expression, but we don't support that yet. */
2044 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2045 write_char ('_');
2047 else
2049 G.need_abi_warning = 1;
2050 write_string ("U8__vector");
2052 write_type (TREE_TYPE (type));
2053 break;
2055 case TYPE_PACK_EXPANSION:
2056 write_string ("Dp");
2057 write_type (PACK_EXPANSION_PATTERN (type));
2058 break;
2060 case DECLTYPE_TYPE:
2061 /* These shouldn't make it into mangling. */
2062 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2063 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2065 /* In ABI <5, we stripped decltype of a plain decl. */
2066 if (!abi_version_at_least (5)
2067 && DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2069 tree expr = DECLTYPE_TYPE_EXPR (type);
2070 tree etype = NULL_TREE;
2071 switch (TREE_CODE (expr))
2073 case VAR_DECL:
2074 case PARM_DECL:
2075 case RESULT_DECL:
2076 case FUNCTION_DECL:
2077 case CONST_DECL:
2078 case TEMPLATE_PARM_INDEX:
2079 etype = TREE_TYPE (expr);
2080 break;
2082 default:
2083 break;
2086 if (etype && !type_uses_auto (etype))
2088 G.need_abi_warning = 1;
2089 write_type (etype);
2090 return;
2094 write_char ('D');
2095 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2096 write_char ('t');
2097 else
2098 write_char ('T');
2099 ++cp_unevaluated_operand;
2100 write_expression (DECLTYPE_TYPE_EXPR (type));
2101 --cp_unevaluated_operand;
2102 write_char ('E');
2103 break;
2105 case NULLPTR_TYPE:
2106 write_string ("Dn");
2107 if (abi_version_at_least (7))
2108 ++is_builtin_type;
2109 break;
2111 case TYPEOF_TYPE:
2112 sorry ("mangling typeof, use decltype instead");
2113 break;
2115 case UNDERLYING_TYPE:
2116 sorry ("mangling __underlying_type");
2117 break;
2119 case LANG_TYPE:
2120 /* fall through. */
2122 default:
2123 gcc_unreachable ();
2128 /* Types other than builtin types are substitution candidates. */
2129 if (!is_builtin_type)
2130 add_substitution (type);
2133 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2134 CV-qualifiers written for TYPE.
2136 <CV-qualifiers> ::= [r] [V] [K] */
2138 static int
2139 write_CV_qualifiers_for_type (const tree type)
2141 int num_qualifiers = 0;
2143 /* The order is specified by:
2145 "In cases where multiple order-insensitive qualifiers are
2146 present, they should be ordered 'K' (closest to the base type),
2147 'V', 'r', and 'U' (farthest from the base type) ..."
2149 Note that we do not use cp_type_quals below; given "const
2150 int[3]", the "const" is emitted with the "int", not with the
2151 array. */
2152 cp_cv_quals quals = TYPE_QUALS (type);
2154 if (quals & TYPE_QUAL_RESTRICT)
2156 write_char ('r');
2157 ++num_qualifiers;
2159 if (quals & TYPE_QUAL_VOLATILE)
2161 write_char ('V');
2162 ++num_qualifiers;
2164 if (quals & TYPE_QUAL_CONST)
2166 write_char ('K');
2167 ++num_qualifiers;
2170 return num_qualifiers;
2173 /* Non-terminal <builtin-type>.
2175 <builtin-type> ::= v # void
2176 ::= b # bool
2177 ::= w # wchar_t
2178 ::= c # char
2179 ::= a # signed char
2180 ::= h # unsigned char
2181 ::= s # short
2182 ::= t # unsigned short
2183 ::= i # int
2184 ::= j # unsigned int
2185 ::= l # long
2186 ::= m # unsigned long
2187 ::= x # long long, __int64
2188 ::= y # unsigned long long, __int64
2189 ::= n # __int128
2190 ::= o # unsigned __int128
2191 ::= f # float
2192 ::= d # double
2193 ::= e # long double, __float80
2194 ::= g # __float128 [not supported]
2195 ::= u <source-name> # vendor extended type */
2197 static void
2198 write_builtin_type (tree type)
2200 if (TYPE_CANONICAL (type))
2201 type = TYPE_CANONICAL (type);
2203 switch (TREE_CODE (type))
2205 case VOID_TYPE:
2206 write_char ('v');
2207 break;
2209 case BOOLEAN_TYPE:
2210 write_char ('b');
2211 break;
2213 case INTEGER_TYPE:
2214 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2215 isn't in integer_type_nodes. */
2216 if (type == wchar_type_node)
2217 write_char ('w');
2218 else if (type == char16_type_node)
2219 write_string ("Ds");
2220 else if (type == char32_type_node)
2221 write_string ("Di");
2222 else if (TYPE_FOR_JAVA (type))
2223 write_java_integer_type_codes (type);
2224 else
2226 size_t itk;
2227 /* Assume TYPE is one of the shared integer type nodes. Find
2228 it in the array of these nodes. */
2229 iagain:
2230 for (itk = 0; itk < itk_none; ++itk)
2231 if (integer_types[itk] != NULL_TREE
2232 && type == integer_types[itk])
2234 /* Print the corresponding single-letter code. */
2235 write_char (integer_type_codes[itk]);
2236 break;
2239 if (itk == itk_none)
2241 tree t = c_common_type_for_mode (TYPE_MODE (type),
2242 TYPE_UNSIGNED (type));
2243 if (type != t)
2245 type = t;
2246 goto iagain;
2249 if (TYPE_PRECISION (type) == 128)
2250 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2251 else
2253 /* Allow for cases where TYPE is not one of the shared
2254 integer type nodes and write a "vendor extended builtin
2255 type" with a name the form intN or uintN, respectively.
2256 Situations like this can happen if you have an
2257 __attribute__((__mode__(__SI__))) type and use exotic
2258 switches like '-mint8' on AVR. Of course, this is
2259 undefined by the C++ ABI (and '-mint8' is not even
2260 Standard C conforming), but when using such special
2261 options you're pretty much in nowhere land anyway. */
2262 const char *prefix;
2263 char prec[11]; /* up to ten digits for an unsigned */
2265 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2266 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2267 write_char ('u'); /* "vendor extended builtin type" */
2268 write_unsigned_number (strlen (prefix) + strlen (prec));
2269 write_string (prefix);
2270 write_string (prec);
2274 break;
2276 case REAL_TYPE:
2277 if (type == float_type_node
2278 || type == java_float_type_node)
2279 write_char ('f');
2280 else if (type == double_type_node
2281 || type == java_double_type_node)
2282 write_char ('d');
2283 else if (type == long_double_type_node)
2284 write_char ('e');
2285 else if (type == dfloat32_type_node)
2286 write_string ("Df");
2287 else if (type == dfloat64_type_node)
2288 write_string ("Dd");
2289 else if (type == dfloat128_type_node)
2290 write_string ("De");
2291 else
2292 gcc_unreachable ();
2293 break;
2295 case FIXED_POINT_TYPE:
2296 write_string ("DF");
2297 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2298 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2299 if (type == fract_type_node
2300 || type == sat_fract_type_node
2301 || type == accum_type_node
2302 || type == sat_accum_type_node)
2303 write_char ('i');
2304 else if (type == unsigned_fract_type_node
2305 || type == sat_unsigned_fract_type_node
2306 || type == unsigned_accum_type_node
2307 || type == sat_unsigned_accum_type_node)
2308 write_char ('j');
2309 else if (type == short_fract_type_node
2310 || type == sat_short_fract_type_node
2311 || type == short_accum_type_node
2312 || type == sat_short_accum_type_node)
2313 write_char ('s');
2314 else if (type == unsigned_short_fract_type_node
2315 || type == sat_unsigned_short_fract_type_node
2316 || type == unsigned_short_accum_type_node
2317 || type == sat_unsigned_short_accum_type_node)
2318 write_char ('t');
2319 else if (type == long_fract_type_node
2320 || type == sat_long_fract_type_node
2321 || type == long_accum_type_node
2322 || type == sat_long_accum_type_node)
2323 write_char ('l');
2324 else if (type == unsigned_long_fract_type_node
2325 || type == sat_unsigned_long_fract_type_node
2326 || type == unsigned_long_accum_type_node
2327 || type == sat_unsigned_long_accum_type_node)
2328 write_char ('m');
2329 else if (type == long_long_fract_type_node
2330 || type == sat_long_long_fract_type_node
2331 || type == long_long_accum_type_node
2332 || type == sat_long_long_accum_type_node)
2333 write_char ('x');
2334 else if (type == unsigned_long_long_fract_type_node
2335 || type == sat_unsigned_long_long_fract_type_node
2336 || type == unsigned_long_long_accum_type_node
2337 || type == sat_unsigned_long_long_accum_type_node)
2338 write_char ('y');
2339 else
2340 sorry ("mangling unknown fixed point type");
2341 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2342 if (TYPE_SATURATING (type))
2343 write_char ('s');
2344 else
2345 write_char ('n');
2346 break;
2348 default:
2349 gcc_unreachable ();
2353 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2354 METHOD_TYPE. The return type is mangled before the parameter
2355 types.
2357 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2359 static void
2360 write_function_type (const tree type)
2362 MANGLE_TRACE_TREE ("function-type", type);
2364 /* For a pointer to member function, the function type may have
2365 cv-qualifiers, indicating the quals for the artificial 'this'
2366 parameter. */
2367 if (TREE_CODE (type) == METHOD_TYPE)
2369 /* The first parameter must be a POINTER_TYPE pointing to the
2370 `this' parameter. */
2371 tree this_type = class_of_this_parm (type);
2372 write_CV_qualifiers_for_type (this_type);
2375 write_char ('F');
2376 /* We don't track whether or not a type is `extern "C"'. Note that
2377 you can have an `extern "C"' function that does not have
2378 `extern "C"' type, and vice versa:
2380 extern "C" typedef void function_t();
2381 function_t f; // f has C++ linkage, but its type is
2382 // `extern "C"'
2384 typedef void function_t();
2385 extern "C" function_t f; // Vice versa.
2387 See [dcl.link]. */
2388 write_bare_function_type (type, /*include_return_type_p=*/1,
2389 /*decl=*/NULL);
2390 if (FUNCTION_REF_QUALIFIED (type))
2392 if (FUNCTION_RVALUE_QUALIFIED (type))
2393 write_char ('O');
2394 else
2395 write_char ('R');
2397 write_char ('E');
2400 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2401 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2402 is mangled before the parameter types. If non-NULL, DECL is
2403 FUNCTION_DECL for the function whose type is being emitted.
2405 If DECL is a member of a Java type, then a literal 'J'
2406 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2407 were nonzero.
2409 <bare-function-type> ::= [J]</signature/ type>+ */
2411 static void
2412 write_bare_function_type (const tree type, const int include_return_type_p,
2413 const tree decl)
2415 int java_method_p;
2417 MANGLE_TRACE_TREE ("bare-function-type", type);
2419 /* Detect Java methods and emit special encoding. */
2420 if (decl != NULL
2421 && DECL_FUNCTION_MEMBER_P (decl)
2422 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2423 && !DECL_CONSTRUCTOR_P (decl)
2424 && !DECL_DESTRUCTOR_P (decl)
2425 && !DECL_CONV_FN_P (decl))
2427 java_method_p = 1;
2428 write_char ('J');
2430 else
2432 java_method_p = 0;
2435 /* Mangle the return type, if requested. */
2436 if (include_return_type_p || java_method_p)
2437 write_type (TREE_TYPE (type));
2439 /* Now mangle the types of the arguments. */
2440 ++G.parm_depth;
2441 write_method_parms (TYPE_ARG_TYPES (type),
2442 TREE_CODE (type) == METHOD_TYPE,
2443 decl);
2444 --G.parm_depth;
2447 /* Write the mangled representation of a method parameter list of
2448 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2449 considered a non-static method, and the this parameter is omitted.
2450 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2451 parameters are being emitted. */
2453 static void
2454 write_method_parms (tree parm_types, const int method_p, const tree decl)
2456 tree first_parm_type;
2457 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2459 /* Assume this parameter type list is variable-length. If it ends
2460 with a void type, then it's not. */
2461 int varargs_p = 1;
2463 /* If this is a member function, skip the first arg, which is the
2464 this pointer.
2465 "Member functions do not encode the type of their implicit this
2466 parameter."
2468 Similarly, there's no need to mangle artificial parameters, like
2469 the VTT parameters for constructors and destructors. */
2470 if (method_p)
2472 parm_types = TREE_CHAIN (parm_types);
2473 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2475 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2477 parm_types = TREE_CHAIN (parm_types);
2478 parm_decl = DECL_CHAIN (parm_decl);
2482 for (first_parm_type = parm_types;
2483 parm_types;
2484 parm_types = TREE_CHAIN (parm_types))
2486 tree parm = TREE_VALUE (parm_types);
2487 if (parm == void_type_node)
2489 /* "Empty parameter lists, whether declared as () or
2490 conventionally as (void), are encoded with a void parameter
2491 (v)." */
2492 if (parm_types == first_parm_type)
2493 write_type (parm);
2494 /* If the parm list is terminated with a void type, it's
2495 fixed-length. */
2496 varargs_p = 0;
2497 /* A void type better be the last one. */
2498 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2500 else
2501 write_type (parm);
2504 if (varargs_p)
2505 /* <builtin-type> ::= z # ellipsis */
2506 write_char ('z');
2509 /* <class-enum-type> ::= <name> */
2511 static void
2512 write_class_enum_type (const tree type)
2514 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2517 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2518 arguments.
2520 <template-args> ::= I <template-arg>* E */
2522 static void
2523 write_template_args (tree args)
2525 int i;
2526 int length = 0;
2528 MANGLE_TRACE_TREE ("template-args", args);
2530 write_char ('I');
2532 if (args)
2533 length = TREE_VEC_LENGTH (args);
2535 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2537 /* We have nested template args. We want the innermost template
2538 argument list. */
2539 args = TREE_VEC_ELT (args, length - 1);
2540 length = TREE_VEC_LENGTH (args);
2542 for (i = 0; i < length; ++i)
2543 write_template_arg (TREE_VEC_ELT (args, i));
2545 write_char ('E');
2548 /* Write out the
2549 <unqualified-name>
2550 <unqualified-name> <template-args>
2551 part of SCOPE_REF or COMPONENT_REF mangling. */
2553 static void
2554 write_member_name (tree member)
2556 if (identifier_p (member))
2557 write_unqualified_id (member);
2558 else if (DECL_P (member))
2559 write_unqualified_name (member);
2560 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2562 tree name = TREE_OPERAND (member, 0);
2563 if (TREE_CODE (name) == OVERLOAD)
2564 name = OVL_FUNCTION (name);
2565 write_member_name (name);
2566 write_template_args (TREE_OPERAND (member, 1));
2568 else
2569 write_expression (member);
2572 /* <expression> ::= <unary operator-name> <expression>
2573 ::= <binary operator-name> <expression> <expression>
2574 ::= <expr-primary>
2576 <expr-primary> ::= <template-param>
2577 ::= L <type> <value number> E # literal
2578 ::= L <mangled-name> E # external name
2579 ::= st <type> # sizeof
2580 ::= sr <type> <unqualified-name> # dependent name
2581 ::= sr <type> <unqualified-name> <template-args> */
2583 static void
2584 write_expression (tree expr)
2586 enum tree_code code = TREE_CODE (expr);
2588 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2589 is converted (via qualification conversions) to another
2590 type. */
2591 while (TREE_CODE (expr) == NOP_EXPR
2592 /* Parentheses aren't mangled. */
2593 || code == PAREN_EXPR
2594 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2596 expr = TREE_OPERAND (expr, 0);
2597 code = TREE_CODE (expr);
2600 if (code == BASELINK
2601 && (!type_unknown_p (expr)
2602 || !BASELINK_QUALIFIED_P (expr)))
2604 expr = BASELINK_FUNCTIONS (expr);
2605 code = TREE_CODE (expr);
2608 /* Handle pointers-to-members by making them look like expression
2609 nodes. */
2610 if (code == PTRMEM_CST)
2612 expr = build_nt (ADDR_EXPR,
2613 build_qualified_name (/*type=*/NULL_TREE,
2614 PTRMEM_CST_CLASS (expr),
2615 PTRMEM_CST_MEMBER (expr),
2616 /*template_p=*/false));
2617 code = TREE_CODE (expr);
2620 /* Handle template parameters. */
2621 if (code == TEMPLATE_TYPE_PARM
2622 || code == TEMPLATE_TEMPLATE_PARM
2623 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2624 || code == TEMPLATE_PARM_INDEX)
2625 write_template_param (expr);
2626 /* Handle literals. */
2627 else if (TREE_CODE_CLASS (code) == tcc_constant
2628 || (abi_version_at_least (2) && code == CONST_DECL))
2629 write_template_arg_literal (expr);
2630 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2632 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2633 write_string ("fpT");
2635 else if (code == PARM_DECL)
2637 /* A function parameter used in a late-specified return type. */
2638 int index = DECL_PARM_INDEX (expr);
2639 int level = DECL_PARM_LEVEL (expr);
2640 int delta = G.parm_depth - level + 1;
2641 gcc_assert (index >= 1);
2642 write_char ('f');
2643 if (delta != 0)
2645 if (abi_version_at_least (5))
2647 /* Let L be the number of function prototype scopes from the
2648 innermost one (in which the parameter reference occurs) up
2649 to (and including) the one containing the declaration of
2650 the referenced parameter. If the parameter declaration
2651 clause of the innermost function prototype scope has been
2652 completely seen, it is not counted (in that case -- which
2653 is perhaps the most common -- L can be zero). */
2654 write_char ('L');
2655 write_unsigned_number (delta - 1);
2657 else
2658 G.need_abi_warning = true;
2660 write_char ('p');
2661 write_compact_number (index - 1);
2663 else if (DECL_P (expr))
2665 /* G++ 3.2 incorrectly mangled non-type template arguments of
2666 enumeration type using their names. */
2667 if (code == CONST_DECL)
2668 G.need_abi_warning = 1;
2669 write_char ('L');
2670 write_mangled_name (expr, false);
2671 write_char ('E');
2673 else if (TREE_CODE (expr) == SIZEOF_EXPR
2674 && SIZEOF_EXPR_TYPE_P (expr))
2676 write_string ("st");
2677 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2679 else if (TREE_CODE (expr) == SIZEOF_EXPR
2680 && TYPE_P (TREE_OPERAND (expr, 0)))
2682 write_string ("st");
2683 write_type (TREE_OPERAND (expr, 0));
2685 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2686 && TYPE_P (TREE_OPERAND (expr, 0)))
2688 write_string ("at");
2689 write_type (TREE_OPERAND (expr, 0));
2691 else if (code == SCOPE_REF
2692 || code == BASELINK)
2694 tree scope, member;
2695 if (code == SCOPE_REF)
2697 scope = TREE_OPERAND (expr, 0);
2698 member = TREE_OPERAND (expr, 1);
2700 else
2702 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2703 member = BASELINK_FUNCTIONS (expr);
2706 if (!abi_version_at_least (2) && DECL_P (member))
2708 write_string ("sr");
2709 write_type (scope);
2710 /* G++ 3.2 incorrectly put out both the "sr" code and
2711 the nested name of the qualified name. */
2712 G.need_abi_warning = 1;
2713 write_encoding (member);
2716 /* If the MEMBER is a real declaration, then the qualifying
2717 scope was not dependent. Ideally, we would not have a
2718 SCOPE_REF in those cases, but sometimes we do. If the second
2719 argument is a DECL, then the name must not have been
2720 dependent. */
2721 else if (DECL_P (member))
2722 write_expression (member);
2723 else
2725 write_string ("sr");
2726 write_type (scope);
2727 write_member_name (member);
2730 else if (INDIRECT_REF_P (expr)
2731 && TREE_TYPE (TREE_OPERAND (expr, 0))
2732 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2734 write_expression (TREE_OPERAND (expr, 0));
2736 else if (identifier_p (expr))
2738 /* An operator name appearing as a dependent name needs to be
2739 specially marked to disambiguate between a use of the operator
2740 name and a use of the operator in an expression. */
2741 if (IDENTIFIER_OPNAME_P (expr))
2742 write_string ("on");
2743 write_unqualified_id (expr);
2745 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2747 tree fn = TREE_OPERAND (expr, 0);
2748 if (is_overloaded_fn (fn))
2749 fn = DECL_NAME (get_first_fn (fn));
2750 if (IDENTIFIER_OPNAME_P (fn))
2751 write_string ("on");
2752 write_unqualified_id (fn);
2753 write_template_args (TREE_OPERAND (expr, 1));
2755 else if (TREE_CODE (expr) == MODOP_EXPR)
2757 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2758 const char *name = (assignment_operator_name_info[(int) subop]
2759 .mangled_name);
2760 write_string (name);
2761 write_expression (TREE_OPERAND (expr, 0));
2762 write_expression (TREE_OPERAND (expr, 2));
2764 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2766 /* ::= [gs] nw <expression>* _ <type> E
2767 ::= [gs] nw <expression>* _ <type> <initializer>
2768 ::= [gs] na <expression>* _ <type> E
2769 ::= [gs] na <expression>* _ <type> <initializer>
2770 <initializer> ::= pi <expression>* E */
2771 tree placement = TREE_OPERAND (expr, 0);
2772 tree type = TREE_OPERAND (expr, 1);
2773 tree nelts = TREE_OPERAND (expr, 2);
2774 tree init = TREE_OPERAND (expr, 3);
2775 tree t;
2777 gcc_assert (code == NEW_EXPR);
2778 if (TREE_OPERAND (expr, 2))
2779 code = VEC_NEW_EXPR;
2781 if (NEW_EXPR_USE_GLOBAL (expr))
2782 write_string ("gs");
2784 write_string (operator_name_info[(int) code].mangled_name);
2786 for (t = placement; t; t = TREE_CHAIN (t))
2787 write_expression (TREE_VALUE (t));
2789 write_char ('_');
2791 if (nelts)
2793 tree domain;
2794 ++processing_template_decl;
2795 domain = compute_array_index_type (NULL_TREE, nelts,
2796 tf_warning_or_error);
2797 type = build_cplus_array_type (type, domain);
2798 --processing_template_decl;
2800 write_type (type);
2802 if (init && TREE_CODE (init) == TREE_LIST
2803 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
2804 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
2805 write_expression (TREE_VALUE (init));
2806 else
2808 if (init)
2809 write_string ("pi");
2810 if (init && init != void_zero_node)
2811 for (t = init; t; t = TREE_CHAIN (t))
2812 write_expression (TREE_VALUE (t));
2813 write_char ('E');
2816 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2818 gcc_assert (code == DELETE_EXPR);
2819 if (DELETE_EXPR_USE_VEC (expr))
2820 code = VEC_DELETE_EXPR;
2822 if (DELETE_EXPR_USE_GLOBAL (expr))
2823 write_string ("gs");
2825 write_string (operator_name_info[(int) code].mangled_name);
2827 write_expression (TREE_OPERAND (expr, 0));
2829 else if (code == THROW_EXPR)
2831 tree op = TREE_OPERAND (expr, 0);
2832 if (op)
2834 write_string ("tw");
2835 write_expression (op);
2837 else
2838 write_string ("tr");
2840 else if (code == CONSTRUCTOR)
2842 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2843 unsigned i; tree val;
2845 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2846 write_string ("il");
2847 else
2849 write_string ("tl");
2850 write_type (TREE_TYPE (expr));
2852 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2853 write_expression (val);
2854 write_char ('E');
2856 else if (dependent_name (expr))
2858 write_unqualified_id (dependent_name (expr));
2860 else
2862 int i, len;
2863 const char *name;
2865 /* When we bind a variable or function to a non-type template
2866 argument with reference type, we create an ADDR_EXPR to show
2867 the fact that the entity's address has been taken. But, we
2868 don't actually want to output a mangling code for the `&'. */
2869 if (TREE_CODE (expr) == ADDR_EXPR
2870 && TREE_TYPE (expr)
2871 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2873 expr = TREE_OPERAND (expr, 0);
2874 if (DECL_P (expr))
2876 write_expression (expr);
2877 return;
2880 code = TREE_CODE (expr);
2883 if (code == COMPONENT_REF)
2885 tree ob = TREE_OPERAND (expr, 0);
2887 if (TREE_CODE (ob) == ARROW_EXPR)
2889 write_string (operator_name_info[(int)code].mangled_name);
2890 ob = TREE_OPERAND (ob, 0);
2892 else
2893 write_string ("dt");
2895 write_expression (ob);
2896 write_member_name (TREE_OPERAND (expr, 1));
2897 return;
2900 /* If it wasn't any of those, recursively expand the expression. */
2901 name = operator_name_info[(int) code].mangled_name;
2903 /* We used to mangle const_cast and static_cast like a C cast. */
2904 if (!abi_version_at_least (6)
2905 && (code == CONST_CAST_EXPR
2906 || code == STATIC_CAST_EXPR))
2908 name = operator_name_info[CAST_EXPR].mangled_name;
2909 G.need_abi_warning = 1;
2912 if (name == NULL)
2914 switch (code)
2916 case TRAIT_EXPR:
2917 error ("use of built-in trait %qE in function signature; "
2918 "use library traits instead", expr);
2919 break;
2921 default:
2922 sorry ("mangling %C", code);
2923 break;
2925 return;
2927 else
2928 write_string (name);
2930 switch (code)
2932 case CALL_EXPR:
2934 tree fn = CALL_EXPR_FN (expr);
2936 if (TREE_CODE (fn) == ADDR_EXPR)
2937 fn = TREE_OPERAND (fn, 0);
2939 /* Mangle a dependent name as the name, not whatever happens to
2940 be the first function in the overload set. */
2941 if ((TREE_CODE (fn) == FUNCTION_DECL
2942 || TREE_CODE (fn) == OVERLOAD)
2943 && type_dependent_expression_p_push (expr))
2944 fn = DECL_NAME (get_first_fn (fn));
2946 write_expression (fn);
2949 for (i = 0; i < call_expr_nargs (expr); ++i)
2950 write_expression (CALL_EXPR_ARG (expr, i));
2951 write_char ('E');
2952 break;
2954 case CAST_EXPR:
2955 write_type (TREE_TYPE (expr));
2956 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2957 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2958 else
2960 tree args = TREE_OPERAND (expr, 0);
2961 write_char ('_');
2962 for (; args; args = TREE_CHAIN (args))
2963 write_expression (TREE_VALUE (args));
2964 write_char ('E');
2966 break;
2968 case DYNAMIC_CAST_EXPR:
2969 case REINTERPRET_CAST_EXPR:
2970 case STATIC_CAST_EXPR:
2971 case CONST_CAST_EXPR:
2972 write_type (TREE_TYPE (expr));
2973 write_expression (TREE_OPERAND (expr, 0));
2974 break;
2976 case PREINCREMENT_EXPR:
2977 case PREDECREMENT_EXPR:
2978 if (abi_version_at_least (6))
2979 write_char ('_');
2980 else
2981 G.need_abi_warning = 1;
2982 /* Fall through. */
2984 default:
2985 /* In the middle-end, some expressions have more operands than
2986 they do in templates (and mangling). */
2987 len = cp_tree_operand_length (expr);
2989 for (i = 0; i < len; ++i)
2991 tree operand = TREE_OPERAND (expr, i);
2992 /* As a GNU extension, the middle operand of a
2993 conditional may be omitted. Since expression
2994 manglings are supposed to represent the input token
2995 stream, there's no good way to mangle such an
2996 expression without extending the C++ ABI. */
2997 if (code == COND_EXPR && i == 1 && !operand)
2999 error ("omitted middle operand to %<?:%> operand "
3000 "cannot be mangled");
3001 continue;
3003 write_expression (operand);
3009 /* Literal subcase of non-terminal <template-arg>.
3011 "Literal arguments, e.g. "A<42L>", are encoded with their type
3012 and value. Negative integer values are preceded with "n"; for
3013 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3014 encoded as 0, true as 1." */
3016 static void
3017 write_template_arg_literal (const tree value)
3019 write_char ('L');
3020 write_type (TREE_TYPE (value));
3022 /* Write a null member pointer value as (type)0, regardless of its
3023 real representation. */
3024 if (null_member_pointer_value_p (value))
3025 write_integer_cst (integer_zero_node);
3026 else
3027 switch (TREE_CODE (value))
3029 case CONST_DECL:
3030 write_integer_cst (DECL_INITIAL (value));
3031 break;
3033 case INTEGER_CST:
3034 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3035 || integer_zerop (value) || integer_onep (value));
3036 write_integer_cst (value);
3037 break;
3039 case REAL_CST:
3040 write_real_cst (value);
3041 break;
3043 case COMPLEX_CST:
3044 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3045 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3047 write_integer_cst (TREE_REALPART (value));
3048 write_char ('_');
3049 write_integer_cst (TREE_IMAGPART (value));
3051 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3052 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3054 write_real_cst (TREE_REALPART (value));
3055 write_char ('_');
3056 write_real_cst (TREE_IMAGPART (value));
3058 else
3059 gcc_unreachable ();
3060 break;
3062 case STRING_CST:
3063 sorry ("string literal in function template signature");
3064 break;
3066 default:
3067 gcc_unreachable ();
3070 write_char ('E');
3073 /* Non-terminal <template-arg>.
3075 <template-arg> ::= <type> # type
3076 ::= L <type> </value/ number> E # literal
3077 ::= LZ <name> E # external name
3078 ::= X <expression> E # expression */
3080 static void
3081 write_template_arg (tree node)
3083 enum tree_code code = TREE_CODE (node);
3085 MANGLE_TRACE_TREE ("template-arg", node);
3087 /* A template template parameter's argument list contains TREE_LIST
3088 nodes of which the value field is the actual argument. */
3089 if (code == TREE_LIST)
3091 node = TREE_VALUE (node);
3092 /* If it's a decl, deal with its type instead. */
3093 if (DECL_P (node))
3095 node = TREE_TYPE (node);
3096 code = TREE_CODE (node);
3100 if (TREE_CODE (node) == NOP_EXPR
3101 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3103 /* Template parameters can be of reference type. To maintain
3104 internal consistency, such arguments use a conversion from
3105 address of object to reference type. */
3106 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3107 if (abi_version_at_least (2))
3108 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3109 else
3110 G.need_abi_warning = 1;
3113 if (TREE_CODE (node) == BASELINK
3114 && !type_unknown_p (node))
3116 if (abi_version_at_least (6))
3117 node = BASELINK_FUNCTIONS (node);
3118 else
3119 /* We wrongly wrapped a class-scope function in X/E. */
3120 G.need_abi_warning = 1;
3123 if (ARGUMENT_PACK_P (node))
3125 /* Expand the template argument pack. */
3126 tree args = ARGUMENT_PACK_ARGS (node);
3127 int i, length = TREE_VEC_LENGTH (args);
3128 if (abi_version_at_least (6))
3129 write_char ('J');
3130 else
3132 write_char ('I');
3133 G.need_abi_warning = 1;
3135 for (i = 0; i < length; ++i)
3136 write_template_arg (TREE_VEC_ELT (args, i));
3137 write_char ('E');
3139 else if (TYPE_P (node))
3140 write_type (node);
3141 else if (code == TEMPLATE_DECL)
3142 /* A template appearing as a template arg is a template template arg. */
3143 write_template_template_arg (node);
3144 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3145 || (abi_version_at_least (2) && code == CONST_DECL)
3146 || null_member_pointer_value_p (node))
3147 write_template_arg_literal (node);
3148 else if (DECL_P (node))
3150 /* Until ABI version 2, non-type template arguments of
3151 enumeration type were mangled using their names. */
3152 if (code == CONST_DECL && !abi_version_at_least (2))
3153 G.need_abi_warning = 1;
3154 write_char ('L');
3155 /* Until ABI version 3, the underscore before the mangled name
3156 was incorrectly omitted. */
3157 if (!abi_version_at_least (3))
3159 G.need_abi_warning = 1;
3160 write_char ('Z');
3162 else
3163 write_string ("_Z");
3164 write_encoding (node);
3165 write_char ('E');
3167 else
3169 /* Template arguments may be expressions. */
3170 write_char ('X');
3171 write_expression (node);
3172 write_char ('E');
3176 /* <template-template-arg>
3177 ::= <name>
3178 ::= <substitution> */
3180 static void
3181 write_template_template_arg (const tree decl)
3183 MANGLE_TRACE_TREE ("template-template-arg", decl);
3185 if (find_substitution (decl))
3186 return;
3187 write_name (decl, /*ignore_local_scope=*/0);
3188 add_substitution (decl);
3192 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3194 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3195 ::= A <expression> _ </element/ type>
3197 "Array types encode the dimension (number of elements) and the
3198 element type. For variable length arrays, the dimension (but not
3199 the '_' separator) is omitted." */
3201 static void
3202 write_array_type (const tree type)
3204 write_char ('A');
3205 if (TYPE_DOMAIN (type))
3207 tree index_type;
3208 tree max;
3210 index_type = TYPE_DOMAIN (type);
3211 /* The INDEX_TYPE gives the upper and lower bounds of the
3212 array. */
3213 max = TYPE_MAX_VALUE (index_type);
3214 if (TREE_CODE (max) == INTEGER_CST)
3216 /* The ABI specifies that we should mangle the number of
3217 elements in the array, not the largest allowed index. */
3218 double_int dmax = tree_to_double_int (max) + double_int_one;
3219 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3220 number of elements as zero. */
3221 dmax = dmax.zext (TYPE_PRECISION (TREE_TYPE (max)));
3222 gcc_assert (dmax.fits_uhwi ());
3223 write_unsigned_number (dmax.low);
3225 else
3227 max = TREE_OPERAND (max, 0);
3228 if (!abi_version_at_least (2))
3230 /* value_dependent_expression_p presumes nothing is
3231 dependent when PROCESSING_TEMPLATE_DECL is zero. */
3232 ++processing_template_decl;
3233 if (!value_dependent_expression_p (max))
3234 G.need_abi_warning = 1;
3235 --processing_template_decl;
3237 write_expression (max);
3241 write_char ('_');
3242 write_type (TREE_TYPE (type));
3245 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3246 variables. TYPE is a pointer-to-member POINTER_TYPE.
3248 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3250 static void
3251 write_pointer_to_member_type (const tree type)
3253 write_char ('M');
3254 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3255 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3258 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3259 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3260 TEMPLATE_PARM_INDEX.
3262 <template-param> ::= T </parameter/ number> _ */
3264 static void
3265 write_template_param (const tree parm)
3267 int parm_index;
3269 MANGLE_TRACE_TREE ("template-parm", parm);
3271 switch (TREE_CODE (parm))
3273 case TEMPLATE_TYPE_PARM:
3274 case TEMPLATE_TEMPLATE_PARM:
3275 case BOUND_TEMPLATE_TEMPLATE_PARM:
3276 parm_index = TEMPLATE_TYPE_IDX (parm);
3277 break;
3279 case TEMPLATE_PARM_INDEX:
3280 parm_index = TEMPLATE_PARM_IDX (parm);
3281 break;
3283 default:
3284 gcc_unreachable ();
3287 write_char ('T');
3288 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3289 earliest template param denoted by `_'. */
3290 write_compact_number (parm_index);
3293 /* <template-template-param>
3294 ::= <template-param>
3295 ::= <substitution> */
3297 static void
3298 write_template_template_param (const tree parm)
3300 tree templ = NULL_TREE;
3302 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3303 template template parameter. The substitution candidate here is
3304 only the template. */
3305 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3307 templ
3308 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3309 if (find_substitution (templ))
3310 return;
3313 /* <template-param> encodes only the template parameter position,
3314 not its template arguments, which is fine here. */
3315 write_template_param (parm);
3316 if (templ)
3317 add_substitution (templ);
3320 /* Non-terminal <substitution>.
3322 <substitution> ::= S <seq-id> _
3323 ::= S_ */
3325 static void
3326 write_substitution (const int seq_id)
3328 MANGLE_TRACE ("substitution", "");
3330 write_char ('S');
3331 if (seq_id > 0)
3332 write_number (seq_id - 1, /*unsigned=*/1, 36);
3333 write_char ('_');
3336 /* Start mangling ENTITY. */
3338 static inline void
3339 start_mangling (const tree entity)
3341 G.entity = entity;
3342 G.need_abi_warning = false;
3343 obstack_free (&name_obstack, name_base);
3344 mangle_obstack = &name_obstack;
3345 name_base = obstack_alloc (&name_obstack, 0);
3348 /* Done with mangling. If WARN is true, and the name of G.entity will
3349 be mangled differently in a future version of the ABI, issue a
3350 warning. */
3352 static void
3353 finish_mangling_internal (const bool warn)
3355 if (warn_abi && warn && G.need_abi_warning)
3356 warning (OPT_Wabi, "the mangled name of %qD will change in a future "
3357 "version of GCC",
3358 G.entity);
3360 /* Clear all the substitutions. */
3361 vec_safe_truncate (G.substitutions, 0);
3363 /* Null-terminate the string. */
3364 write_char ('\0');
3368 /* Like finish_mangling_internal, but return the mangled string. */
3370 static inline const char *
3371 finish_mangling (const bool warn)
3373 finish_mangling_internal (warn);
3374 return (const char *) obstack_finish (mangle_obstack);
3377 /* Like finish_mangling_internal, but return an identifier. */
3379 static tree
3380 finish_mangling_get_identifier (const bool warn)
3382 finish_mangling_internal (warn);
3383 /* Don't obstack_finish here, and the next start_mangling will
3384 remove the identifier. */
3385 return get_identifier ((const char *) obstack_base (mangle_obstack));
3388 /* Initialize data structures for mangling. */
3390 void
3391 init_mangle (void)
3393 gcc_obstack_init (&name_obstack);
3394 name_base = obstack_alloc (&name_obstack, 0);
3395 vec_alloc (G.substitutions, 0);
3397 /* Cache these identifiers for quick comparison when checking for
3398 standard substitutions. */
3399 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3400 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3401 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3402 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3403 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3404 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3407 /* Generate the mangled name of DECL. */
3409 static tree
3410 mangle_decl_string (const tree decl)
3412 tree result;
3413 location_t saved_loc = input_location;
3414 tree saved_fn = NULL_TREE;
3415 bool template_p = false;
3417 /* We shouldn't be trying to mangle an uninstantiated template. */
3418 gcc_assert (!type_dependent_expression_p (decl));
3420 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3422 struct tinst_level *tl = current_instantiation ();
3423 if ((!tl || tl->decl != decl)
3424 && push_tinst_level (decl))
3426 template_p = true;
3427 saved_fn = current_function_decl;
3428 current_function_decl = NULL_TREE;
3431 input_location = DECL_SOURCE_LOCATION (decl);
3433 start_mangling (decl);
3435 if (TREE_CODE (decl) == TYPE_DECL)
3436 write_type (TREE_TYPE (decl));
3437 else
3438 write_mangled_name (decl, true);
3440 result = finish_mangling_get_identifier (/*warn=*/true);
3441 if (DEBUG_MANGLE)
3442 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3443 IDENTIFIER_POINTER (result));
3445 if (template_p)
3447 pop_tinst_level ();
3448 current_function_decl = saved_fn;
3450 input_location = saved_loc;
3452 return result;
3455 /* Return an identifier for the external mangled name of DECL. */
3457 static tree
3458 get_mangled_id (tree decl)
3460 tree id = mangle_decl_string (decl);
3461 return targetm.mangle_decl_assembler_name (decl, id);
3464 /* Create an identifier for the external mangled name of DECL. */
3466 void
3467 mangle_decl (const tree decl)
3469 tree id;
3470 bool dep;
3472 /* Don't bother mangling uninstantiated templates. */
3473 ++processing_template_decl;
3474 if (TREE_CODE (decl) == TYPE_DECL)
3475 dep = dependent_type_p (TREE_TYPE (decl));
3476 else
3477 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3478 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3479 --processing_template_decl;
3480 if (dep)
3481 return;
3483 id = get_mangled_id (decl);
3484 SET_DECL_ASSEMBLER_NAME (decl, id);
3486 if (G.need_abi_warning
3487 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3488 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3489 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3491 #ifdef ASM_OUTPUT_DEF
3492 /* If the mangling will change in the future, emit an alias with the
3493 future mangled name for forward-compatibility. */
3494 int save_ver;
3495 tree id2, alias;
3496 #endif
3498 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3499 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3500 inform (DECL_SOURCE_LOCATION (decl), "-fabi-version=6 (or =0) "
3501 "avoids this error with a change in mangling");
3503 #ifdef ASM_OUTPUT_DEF
3504 save_ver = flag_abi_version;
3505 flag_abi_version = 0;
3506 id2 = mangle_decl_string (decl);
3507 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3508 flag_abi_version = save_ver;
3510 alias = make_alias_for (decl, id2);
3511 DECL_IGNORED_P (alias) = 1;
3512 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3513 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3514 if (vague_linkage_p (decl))
3515 DECL_WEAK (alias) = 1;
3516 if (TREE_CODE (decl) == FUNCTION_DECL)
3517 cgraph_same_body_alias (cgraph_get_create_node (decl), alias, decl);
3518 else
3519 varpool_extra_name_alias (alias, decl);
3520 #endif
3524 /* Generate the mangled representation of TYPE. */
3526 const char *
3527 mangle_type_string (const tree type)
3529 const char *result;
3531 start_mangling (type);
3532 write_type (type);
3533 result = finish_mangling (/*warn=*/false);
3534 if (DEBUG_MANGLE)
3535 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3536 return result;
3539 /* Create an identifier for the mangled name of a special component
3540 for belonging to TYPE. CODE is the ABI-specified code for this
3541 component. */
3543 static tree
3544 mangle_special_for_type (const tree type, const char *code)
3546 tree result;
3548 /* We don't have an actual decl here for the special component, so
3549 we can't just process the <encoded-name>. Instead, fake it. */
3550 start_mangling (type);
3552 /* Start the mangling. */
3553 write_string ("_Z");
3554 write_string (code);
3556 /* Add the type. */
3557 write_type (type);
3558 result = finish_mangling_get_identifier (/*warn=*/false);
3560 if (DEBUG_MANGLE)
3561 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3562 IDENTIFIER_POINTER (result));
3564 return result;
3567 /* Create an identifier for the mangled representation of the typeinfo
3568 structure for TYPE. */
3570 tree
3571 mangle_typeinfo_for_type (const tree type)
3573 return mangle_special_for_type (type, "TI");
3576 /* Create an identifier for the mangled name of the NTBS containing
3577 the mangled name of TYPE. */
3579 tree
3580 mangle_typeinfo_string_for_type (const tree type)
3582 return mangle_special_for_type (type, "TS");
3585 /* Create an identifier for the mangled name of the vtable for TYPE. */
3587 tree
3588 mangle_vtbl_for_type (const tree type)
3590 return mangle_special_for_type (type, "TV");
3593 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3595 tree
3596 mangle_vtt_for_type (const tree type)
3598 return mangle_special_for_type (type, "TT");
3601 /* Return an identifier for a construction vtable group. TYPE is
3602 the most derived class in the hierarchy; BINFO is the base
3603 subobject for which this construction vtable group will be used.
3605 This mangling isn't part of the ABI specification; in the ABI
3606 specification, the vtable group is dumped in the same COMDAT as the
3607 main vtable, and is referenced only from that vtable, so it doesn't
3608 need an external name. For binary formats without COMDAT sections,
3609 though, we need external names for the vtable groups.
3611 We use the production
3613 <special-name> ::= CT <type> <offset number> _ <base type> */
3615 tree
3616 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3618 tree result;
3620 start_mangling (type);
3622 write_string ("_Z");
3623 write_string ("TC");
3624 write_type (type);
3625 write_integer_cst (BINFO_OFFSET (binfo));
3626 write_char ('_');
3627 write_type (BINFO_TYPE (binfo));
3629 result = finish_mangling_get_identifier (/*warn=*/false);
3630 if (DEBUG_MANGLE)
3631 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3632 IDENTIFIER_POINTER (result));
3633 return result;
3636 /* Mangle a this pointer or result pointer adjustment.
3638 <call-offset> ::= h <fixed offset number> _
3639 ::= v <fixed offset number> _ <virtual offset number> _ */
3641 static void
3642 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3644 write_char (virtual_offset ? 'v' : 'h');
3646 /* For either flavor, write the fixed offset. */
3647 write_integer_cst (fixed_offset);
3648 write_char ('_');
3650 /* For a virtual thunk, add the virtual offset. */
3651 if (virtual_offset)
3653 write_integer_cst (virtual_offset);
3654 write_char ('_');
3658 /* Return an identifier for the mangled name of a this-adjusting or
3659 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3660 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3661 is a virtual thunk, and it is the vtbl offset in
3662 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3663 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3664 thunk itself. A covariant thunk name always includes the adjustment
3665 for the this pointer, even if there is none.
3667 <special-name> ::= T <call-offset> <base encoding>
3668 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3669 <base encoding> */
3671 tree
3672 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3673 tree virtual_offset)
3675 tree result;
3677 start_mangling (fn_decl);
3679 write_string ("_Z");
3680 write_char ('T');
3682 if (!this_adjusting)
3684 /* Covariant thunk with no this adjustment */
3685 write_char ('c');
3686 mangle_call_offset (integer_zero_node, NULL_TREE);
3687 mangle_call_offset (fixed_offset, virtual_offset);
3689 else if (!DECL_THUNK_P (fn_decl))
3690 /* Plain this adjusting thunk. */
3691 mangle_call_offset (fixed_offset, virtual_offset);
3692 else
3694 /* This adjusting thunk to covariant thunk. */
3695 write_char ('c');
3696 mangle_call_offset (fixed_offset, virtual_offset);
3697 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3698 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3699 if (virtual_offset)
3700 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3701 mangle_call_offset (fixed_offset, virtual_offset);
3702 fn_decl = THUNK_TARGET (fn_decl);
3705 /* Scoped name. */
3706 write_encoding (fn_decl);
3708 result = finish_mangling_get_identifier (/*warn=*/false);
3709 if (DEBUG_MANGLE)
3710 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3711 return result;
3714 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3715 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3716 TYPE. */
3718 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
3720 /* Hash a node (VAL1) in the table. */
3722 static hashval_t
3723 hash_type (const void *val)
3725 return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
3728 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3730 static int
3731 compare_type (const void *val1, const void *val2)
3733 return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
3736 /* Return an identifier for the mangled unqualified name for a
3737 conversion operator to TYPE. This mangling is not specified by the
3738 ABI spec; it is only used internally. */
3740 tree
3741 mangle_conv_op_name_for_type (const tree type)
3743 void **slot;
3744 tree identifier;
3746 if (type == error_mark_node)
3747 return error_mark_node;
3749 if (conv_type_names == NULL)
3750 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
3752 slot = htab_find_slot_with_hash (conv_type_names, type,
3753 (hashval_t) TYPE_UID (type), INSERT);
3754 identifier = (tree)*slot;
3755 if (!identifier)
3757 char buffer[64];
3759 /* Create a unique name corresponding to TYPE. */
3760 sprintf (buffer, "operator %lu",
3761 (unsigned long) htab_elements (conv_type_names));
3762 identifier = get_identifier (buffer);
3763 *slot = identifier;
3765 /* Hang TYPE off the identifier so it can be found easily later
3766 when performing conversions. */
3767 TREE_TYPE (identifier) = type;
3769 /* Set bits on the identifier so we know later it's a conversion. */
3770 IDENTIFIER_OPNAME_P (identifier) = 1;
3771 IDENTIFIER_TYPENAME_P (identifier) = 1;
3774 return identifier;
3777 /* Write out the appropriate string for this variable when generating
3778 another mangled name based on this one. */
3780 static void
3781 write_guarded_var_name (const tree variable)
3783 if (DECL_NAME (variable)
3784 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3785 /* The name of a guard variable for a reference temporary should refer
3786 to the reference, not the temporary. */
3787 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3788 else
3789 write_name (variable, /*ignore_local_scope=*/0);
3792 /* Return an identifier for the name of an initialization guard
3793 variable for indicated VARIABLE. */
3795 tree
3796 mangle_guard_variable (const tree variable)
3798 start_mangling (variable);
3799 write_string ("_ZGV");
3800 write_guarded_var_name (variable);
3801 return finish_mangling_get_identifier (/*warn=*/false);
3804 /* Return an identifier for the name of a thread_local initialization
3805 function for VARIABLE. */
3807 tree
3808 mangle_tls_init_fn (const tree variable)
3810 start_mangling (variable);
3811 write_string ("_ZTH");
3812 write_guarded_var_name (variable);
3813 return finish_mangling_get_identifier (/*warn=*/false);
3816 /* Return an identifier for the name of a thread_local wrapper
3817 function for VARIABLE. */
3819 #define TLS_WRAPPER_PREFIX "_ZTW"
3821 tree
3822 mangle_tls_wrapper_fn (const tree variable)
3824 start_mangling (variable);
3825 write_string (TLS_WRAPPER_PREFIX);
3826 write_guarded_var_name (variable);
3827 return finish_mangling_get_identifier (/*warn=*/false);
3830 /* Return true iff FN is a thread_local wrapper function. */
3832 bool
3833 decl_tls_wrapper_p (const tree fn)
3835 if (TREE_CODE (fn) != FUNCTION_DECL)
3836 return false;
3837 tree name = DECL_NAME (fn);
3838 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3839 strlen (TLS_WRAPPER_PREFIX)) == 0;
3842 /* Return an identifier for the name of a temporary variable used to
3843 initialize a static reference. This isn't part of the ABI, but we might
3844 as well call them something readable. */
3846 static GTY(()) int temp_count;
3848 tree
3849 mangle_ref_init_variable (const tree variable)
3851 start_mangling (variable);
3852 write_string ("_ZGR");
3853 write_name (variable, /*ignore_local_scope=*/0);
3854 /* Avoid name clashes with aggregate initialization of multiple
3855 references at once. */
3856 write_unsigned_number (temp_count++);
3857 return finish_mangling_get_identifier (/*warn=*/false);
3861 /* Foreign language type mangling section. */
3863 /* How to write the type codes for the integer Java type. */
3865 static void
3866 write_java_integer_type_codes (const tree type)
3868 if (type == java_int_type_node)
3869 write_char ('i');
3870 else if (type == java_short_type_node)
3871 write_char ('s');
3872 else if (type == java_byte_type_node)
3873 write_char ('c');
3874 else if (type == java_char_type_node)
3875 write_char ('w');
3876 else if (type == java_long_type_node)
3877 write_char ('x');
3878 else if (type == java_boolean_type_node)
3879 write_char ('b');
3880 else
3881 gcc_unreachable ();
3884 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3885 function generates a mangled name for the vtable map variable of
3886 the class type. For example, if the class type is
3887 "std::bad_exception", the mangled name for the class is
3888 "St13bad_exception". This function would generate the name
3889 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3890 "_VTV<std::bad_exception>::__vtable_map". */
3893 char *
3894 get_mangled_vtable_map_var_name (tree class_type)
3896 char *var_name = NULL;
3897 const char *prefix = "_ZN4_VTVI";
3898 const char *postfix = "E12__vtable_mapE";
3900 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3902 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3903 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3904 strlen (prefix) +
3905 strlen (postfix) + 1;
3907 var_name = (char *) xmalloc (len);
3909 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3911 return var_name;
3914 #include "gt-cp-mangle.h"