compiler: Remove obsolete hidden_fields_are_ok code.
[official-gcc.git] / gcc / cp / mangle.c
blob55f508a272a0c09d4ef2adffd1594d8a440d671e
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file implements mangling of C++ names according to the IA64
22 C++ ABI specification. A mangled name encodes a function or
23 variable's name, scope, type, and/or template arguments into a text
24 identifier. This identifier is used as the function's or
25 variable's linkage name, to preserve compatibility between C++'s
26 language features (templates, scoping, and overloading) and C
27 linkers.
29 Additionally, g++ uses mangled names internally. To support this,
30 mangling of types is allowed, even though the mangled name of a
31 type should not appear by itself as an exported name. Ditto for
32 uninstantiated templates.
34 The primary entry point for this module is mangle_decl, which
35 returns an identifier containing the mangled name for a decl.
36 Additional entry points are provided to build mangled names of
37 particular constructs when the appropriate decl for that construct
38 is not available. These are:
40 mangle_typeinfo_for_type: typeinfo data
41 mangle_typeinfo_string_for_type: typeinfo type name
42 mangle_vtbl_for_type: virtual table data
43 mangle_vtt_for_type: VTT data
44 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
45 mangle_thunk: thunk function or entry */
47 #include "config.h"
48 #include "system.h"
49 #include "coretypes.h"
50 #include "tm.h"
51 #include "tree.h"
52 #include "tree-hasher.h"
53 #include "stor-layout.h"
54 #include "stringpool.h"
55 #include "tm_p.h"
56 #include "cp-tree.h"
57 #include "obstack.h"
58 #include "flags.h"
59 #include "target.h"
60 #include "cgraph.h"
61 #include "wide-int.h"
63 /* Debugging support. */
65 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
66 #ifndef DEBUG_MANGLE
67 #define DEBUG_MANGLE 0
68 #endif
70 /* Macros for tracing the write_* functions. */
71 #if DEBUG_MANGLE
72 # define MANGLE_TRACE(FN, INPUT) \
73 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
74 # define MANGLE_TRACE_TREE(FN, NODE) \
75 fprintf (stderr, " %-24s: %-24s (%p)\n", \
76 (FN), get_tree_code_name (TREE_CODE (NODE)), (void *) (NODE))
77 #else
78 # define MANGLE_TRACE(FN, INPUT)
79 # define MANGLE_TRACE_TREE(FN, NODE)
80 #endif
82 /* Nonzero if NODE is a class template-id. We can't rely on
83 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
84 that hard to distinguish A<T> from A, where A<T> is the type as
85 instantiated outside of the template, and A is the type used
86 without parameters inside the template. */
87 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
88 (TYPE_LANG_SPECIFIC (NODE) != NULL \
89 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
90 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
91 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
93 /* Things we only need one of. This module is not reentrant. */
94 typedef struct GTY(()) globals {
95 /* An array of the current substitution candidates, in the order
96 we've seen them. */
97 vec<tree, va_gc> *substitutions;
99 /* The entity that is being mangled. */
100 tree GTY ((skip)) entity;
102 /* How many parameter scopes we are inside. */
103 int parm_depth;
105 /* True if the mangling will be different in a future version of the
106 ABI. */
107 bool need_abi_warning;
108 } globals;
110 static GTY (()) globals G;
112 /* The obstack on which we build mangled names. */
113 static struct obstack *mangle_obstack;
115 /* The obstack on which we build mangled names that are not going to
116 be IDENTIFIER_NODEs. */
117 static struct obstack name_obstack;
119 /* The first object on the name_obstack; we use this to free memory
120 allocated on the name_obstack. */
121 static void *name_base;
123 /* Indices into subst_identifiers. These are identifiers used in
124 special substitution rules. */
125 typedef enum
127 SUBID_ALLOCATOR,
128 SUBID_BASIC_STRING,
129 SUBID_CHAR_TRAITS,
130 SUBID_BASIC_ISTREAM,
131 SUBID_BASIC_OSTREAM,
132 SUBID_BASIC_IOSTREAM,
133 SUBID_MAX
135 substitution_identifier_index_t;
137 /* For quick substitution checks, look up these common identifiers
138 once only. */
139 static GTY(()) tree subst_identifiers[SUBID_MAX];
141 /* Single-letter codes for builtin integer types, defined in
142 <builtin-type>. These are indexed by integer_type_kind values. */
143 static const char
144 integer_type_codes[itk_none] =
146 'c', /* itk_char */
147 'a', /* itk_signed_char */
148 'h', /* itk_unsigned_char */
149 's', /* itk_short */
150 't', /* itk_unsigned_short */
151 'i', /* itk_int */
152 'j', /* itk_unsigned_int */
153 'l', /* itk_long */
154 'm', /* itk_unsigned_long */
155 'x', /* itk_long_long */
156 'y', /* itk_unsigned_long_long */
157 /* __intN types are handled separately */
158 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
161 static int decl_is_template_id (const tree, tree* const);
163 /* Functions for handling substitutions. */
165 static inline tree canonicalize_for_substitution (tree);
166 static void add_substitution (tree);
167 static inline int is_std_substitution (const tree,
168 const substitution_identifier_index_t);
169 static inline int is_std_substitution_char (const tree,
170 const substitution_identifier_index_t);
171 static int find_substitution (tree);
172 static void mangle_call_offset (const tree, const tree);
174 /* Functions for emitting mangled representations of things. */
176 static void write_mangled_name (const tree, bool);
177 static void write_encoding (const tree);
178 static void write_name (tree, const int);
179 static void write_abi_tags (tree);
180 static void write_unscoped_name (const tree);
181 static void write_unscoped_template_name (const tree);
182 static void write_nested_name (const tree);
183 static void write_prefix (const tree);
184 static void write_template_prefix (const tree);
185 static void write_unqualified_name (tree);
186 static void write_conversion_operator_name (const tree);
187 static void write_source_name (tree);
188 static void write_literal_operator_name (tree);
189 static void write_unnamed_type_name (const tree);
190 static void write_closure_type_name (const tree);
191 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
192 const unsigned int);
193 static void write_number (unsigned HOST_WIDE_INT, const int,
194 const unsigned int);
195 static void write_compact_number (int num);
196 static void write_integer_cst (const tree);
197 static void write_real_cst (const tree);
198 static void write_identifier (const char *);
199 static void write_special_name_constructor (const tree);
200 static void write_special_name_destructor (const tree);
201 static void write_type (tree);
202 static int write_CV_qualifiers_for_type (const tree);
203 static void write_builtin_type (tree);
204 static void write_function_type (const tree);
205 static void write_bare_function_type (const tree, const int, const tree);
206 static void write_method_parms (tree, const int, const tree);
207 static void write_class_enum_type (const tree);
208 static void write_template_args (tree);
209 static void write_expression (tree);
210 static void write_template_arg_literal (const tree);
211 static void write_template_arg (tree);
212 static void write_template_template_arg (const tree);
213 static void write_array_type (const tree);
214 static void write_pointer_to_member_type (const tree);
215 static void write_template_param (const tree);
216 static void write_template_template_param (const tree);
217 static void write_substitution (const int);
218 static int discriminator_for_local_entity (tree);
219 static int discriminator_for_string_literal (tree, tree);
220 static void write_discriminator (const int);
221 static void write_local_name (tree, const tree, const tree);
222 static void dump_substitution_candidates (void);
223 static tree mangle_decl_string (const tree);
224 static int local_class_index (tree);
226 /* Control functions. */
228 static inline void start_mangling (const tree);
229 static tree mangle_special_for_type (const tree, const char *);
231 /* Foreign language functions. */
233 static void write_java_integer_type_codes (const tree);
235 /* Append a single character to the end of the mangled
236 representation. */
237 #define write_char(CHAR) \
238 obstack_1grow (mangle_obstack, (CHAR))
240 /* Append a sized buffer to the end of the mangled representation. */
241 #define write_chars(CHAR, LEN) \
242 obstack_grow (mangle_obstack, (CHAR), (LEN))
244 /* Append a NUL-terminated string to the end of the mangled
245 representation. */
246 #define write_string(STRING) \
247 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
249 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
250 same purpose (context, which may be a type) and value (template
251 decl). See write_template_prefix for more information on what this
252 is used for. */
253 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
254 (TREE_CODE (NODE1) == TREE_LIST \
255 && TREE_CODE (NODE2) == TREE_LIST \
256 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
257 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
258 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
259 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
261 /* Write out an unsigned quantity in base 10. */
262 #define write_unsigned_number(NUMBER) \
263 write_number ((NUMBER), /*unsigned_p=*/1, 10)
265 /* If DECL is a template instance, return nonzero and, if
266 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
267 Otherwise return zero. */
269 static int
270 decl_is_template_id (const tree decl, tree* const template_info)
272 if (TREE_CODE (decl) == TYPE_DECL)
274 /* TYPE_DECLs are handled specially. Look at its type to decide
275 if this is a template instantiation. */
276 const tree type = TREE_TYPE (decl);
278 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
280 if (template_info != NULL)
281 /* For a templated TYPE_DECL, the template info is hanging
282 off the type. */
283 *template_info = TYPE_TEMPLATE_INFO (type);
284 return 1;
287 else
289 /* Check if this is a primary template. */
290 if (DECL_LANG_SPECIFIC (decl) != NULL
291 && DECL_USE_TEMPLATE (decl)
292 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
293 && TREE_CODE (decl) != TEMPLATE_DECL)
295 if (template_info != NULL)
296 /* For most templated decls, the template info is hanging
297 off the decl. */
298 *template_info = DECL_TEMPLATE_INFO (decl);
299 return 1;
303 /* It's not a template id. */
304 return 0;
307 /* Produce debugging output of current substitution candidates. */
309 static void
310 dump_substitution_candidates (void)
312 unsigned i;
313 tree el;
315 fprintf (stderr, " ++ substitutions ");
316 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
318 const char *name = "???";
320 if (i > 0)
321 fprintf (stderr, " ");
322 if (DECL_P (el))
323 name = IDENTIFIER_POINTER (DECL_NAME (el));
324 else if (TREE_CODE (el) == TREE_LIST)
325 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
326 else if (TYPE_NAME (el))
327 name = TYPE_NAME_STRING (el);
328 fprintf (stderr, " S%d_ = ", i - 1);
329 if (TYPE_P (el) &&
330 (CP_TYPE_RESTRICT_P (el)
331 || CP_TYPE_VOLATILE_P (el)
332 || CP_TYPE_CONST_P (el)))
333 fprintf (stderr, "CV-");
334 fprintf (stderr, "%s (%s at %p)\n",
335 name, get_tree_code_name (TREE_CODE (el)), (void *) el);
339 /* Both decls and types can be substitution candidates, but sometimes
340 they refer to the same thing. For instance, a TYPE_DECL and
341 RECORD_TYPE for the same class refer to the same thing, and should
342 be treated accordingly in substitutions. This function returns a
343 canonicalized tree node representing NODE that is used when adding
344 and substitution candidates and finding matches. */
346 static inline tree
347 canonicalize_for_substitution (tree node)
349 /* For a TYPE_DECL, use the type instead. */
350 if (TREE_CODE (node) == TYPE_DECL)
351 node = TREE_TYPE (node);
352 if (TYPE_P (node)
353 && TYPE_CANONICAL (node) != node
354 && TYPE_MAIN_VARIANT (node) != node)
356 tree orig = node;
357 /* Here we want to strip the topmost typedef only.
358 We need to do that so is_std_substitution can do proper
359 name matching. */
360 if (TREE_CODE (node) == FUNCTION_TYPE)
361 /* Use build_qualified_type and TYPE_QUALS here to preserve
362 the old buggy mangling of attribute noreturn with abi<5. */
363 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
364 TYPE_QUALS (node));
365 else
366 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
367 cp_type_quals (node));
368 if (TREE_CODE (node) == FUNCTION_TYPE
369 || TREE_CODE (node) == METHOD_TYPE)
370 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
372 return node;
375 /* Add NODE as a substitution candidate. NODE must not already be on
376 the list of candidates. */
378 static void
379 add_substitution (tree node)
381 tree c;
383 if (DEBUG_MANGLE)
384 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
385 get_tree_code_name (TREE_CODE (node)), (void *) node);
387 /* Get the canonicalized substitution candidate for NODE. */
388 c = canonicalize_for_substitution (node);
389 if (DEBUG_MANGLE && c != node)
390 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
391 get_tree_code_name (TREE_CODE (node)), (void *) node);
392 node = c;
394 #if ENABLE_CHECKING
395 /* Make sure NODE isn't already a candidate. */
397 int i;
398 tree candidate;
400 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
402 gcc_assert (!(DECL_P (node) && node == candidate));
403 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
404 && same_type_p (node, candidate)));
407 #endif /* ENABLE_CHECKING */
409 /* Put the decl onto the varray of substitution candidates. */
410 vec_safe_push (G.substitutions, node);
412 if (DEBUG_MANGLE)
413 dump_substitution_candidates ();
416 /* Helper function for find_substitution. Returns nonzero if NODE,
417 which may be a decl or a CLASS_TYPE, is a template-id with template
418 name of substitution_index[INDEX] in the ::std namespace. */
420 static inline int
421 is_std_substitution (const tree node,
422 const substitution_identifier_index_t index)
424 tree type = NULL;
425 tree decl = NULL;
427 if (DECL_P (node))
429 type = TREE_TYPE (node);
430 decl = node;
432 else if (CLASS_TYPE_P (node))
434 type = node;
435 decl = TYPE_NAME (node);
437 else
438 /* These are not the droids you're looking for. */
439 return 0;
441 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
442 && TYPE_LANG_SPECIFIC (type)
443 && TYPE_TEMPLATE_INFO (type)
444 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
445 == subst_identifiers[index]));
448 /* Helper function for find_substitution. Returns nonzero if NODE,
449 which may be a decl or a CLASS_TYPE, is the template-id
450 ::std::identifier<char>, where identifier is
451 substitution_index[INDEX]. */
453 static inline int
454 is_std_substitution_char (const tree node,
455 const substitution_identifier_index_t index)
457 tree args;
458 /* Check NODE's name is ::std::identifier. */
459 if (!is_std_substitution (node, index))
460 return 0;
461 /* Figure out its template args. */
462 if (DECL_P (node))
463 args = DECL_TI_ARGS (node);
464 else if (CLASS_TYPE_P (node))
465 args = CLASSTYPE_TI_ARGS (node);
466 else
467 /* Oops, not a template. */
468 return 0;
469 /* NODE's template arg list should be <char>. */
470 return
471 TREE_VEC_LENGTH (args) == 1
472 && TREE_VEC_ELT (args, 0) == char_type_node;
475 /* Check whether a substitution should be used to represent NODE in
476 the mangling.
478 First, check standard special-case substitutions.
480 <substitution> ::= St
481 # ::std
483 ::= Sa
484 # ::std::allocator
486 ::= Sb
487 # ::std::basic_string
489 ::= Ss
490 # ::std::basic_string<char,
491 ::std::char_traits<char>,
492 ::std::allocator<char> >
494 ::= Si
495 # ::std::basic_istream<char, ::std::char_traits<char> >
497 ::= So
498 # ::std::basic_ostream<char, ::std::char_traits<char> >
500 ::= Sd
501 # ::std::basic_iostream<char, ::std::char_traits<char> >
503 Then examine the stack of currently available substitution
504 candidates for entities appearing earlier in the same mangling
506 If a substitution is found, write its mangled representation and
507 return nonzero. If none is found, just return zero. */
509 static int
510 find_substitution (tree node)
512 int i;
513 const int size = vec_safe_length (G.substitutions);
514 tree decl;
515 tree type;
516 const char *abbr = NULL;
518 if (DEBUG_MANGLE)
519 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
520 get_tree_code_name (TREE_CODE (node)), (void *) node);
522 /* Obtain the canonicalized substitution representation for NODE.
523 This is what we'll compare against. */
524 node = canonicalize_for_substitution (node);
526 /* Check for builtin substitutions. */
528 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
529 type = TYPE_P (node) ? node : TREE_TYPE (node);
531 /* Check for std::allocator. */
532 if (decl
533 && is_std_substitution (decl, SUBID_ALLOCATOR)
534 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
535 abbr = "Sa";
537 /* Check for std::basic_string. */
538 else if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
540 if (TYPE_P (node))
542 /* If this is a type (i.e. a fully-qualified template-id),
543 check for
544 std::basic_string <char,
545 std::char_traits<char>,
546 std::allocator<char> > . */
547 if (cp_type_quals (type) == TYPE_UNQUALIFIED
548 && CLASSTYPE_USE_TEMPLATE (type))
550 tree args = CLASSTYPE_TI_ARGS (type);
551 if (TREE_VEC_LENGTH (args) == 3
552 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
553 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
554 SUBID_CHAR_TRAITS)
555 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
556 SUBID_ALLOCATOR))
557 abbr = "Ss";
560 else
561 /* Substitute for the template name only if this isn't a type. */
562 abbr = "Sb";
565 /* Check for basic_{i,o,io}stream. */
566 else if (TYPE_P (node)
567 && cp_type_quals (type) == TYPE_UNQUALIFIED
568 && CLASS_TYPE_P (type)
569 && CLASSTYPE_USE_TEMPLATE (type)
570 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
572 /* First, check for the template
573 args <char, std::char_traits<char> > . */
574 tree args = CLASSTYPE_TI_ARGS (type);
575 if (TREE_VEC_LENGTH (args) == 2
576 && TYPE_P (TREE_VEC_ELT (args, 0))
577 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
578 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
579 SUBID_CHAR_TRAITS))
581 /* Got them. Is this basic_istream? */
582 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
583 abbr = "Si";
584 /* Or basic_ostream? */
585 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
586 abbr = "So";
587 /* Or basic_iostream? */
588 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
589 abbr = "Sd";
593 /* Check for namespace std. */
594 else if (decl && DECL_NAMESPACE_STD_P (decl))
596 write_string ("St");
597 return 1;
600 tree tags = NULL_TREE;
601 if (OVERLOAD_TYPE_P (node))
602 tags = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (type));
603 /* Now check the list of available substitutions for this mangling
604 operation. */
605 if (!abbr || tags) for (i = 0; i < size; ++i)
607 tree candidate = (*G.substitutions)[i];
608 /* NODE is a matched to a candidate if it's the same decl node or
609 if it's the same type. */
610 if (decl == candidate
611 || (TYPE_P (candidate) && type && TYPE_P (node)
612 && same_type_p (type, candidate))
613 || NESTED_TEMPLATE_MATCH (node, candidate))
615 write_substitution (i);
616 return 1;
620 if (!abbr)
621 /* No substitution found. */
622 return 0;
624 write_string (abbr);
625 if (tags)
627 /* If there are ABI tags on the abbreviation, it becomes
628 a substitution candidate. */
629 write_abi_tags (tags);
630 add_substitution (node);
632 return 1;
636 /* TOP_LEVEL is true, if this is being called at outermost level of
637 mangling. It should be false when mangling a decl appearing in an
638 expression within some other mangling.
640 <mangled-name> ::= _Z <encoding> */
642 static void
643 write_mangled_name (const tree decl, bool top_level)
645 MANGLE_TRACE_TREE ("mangled-name", decl);
647 if (/* The names of `extern "C"' functions are not mangled. */
648 DECL_EXTERN_C_FUNCTION_P (decl)
649 /* But overloaded operator names *are* mangled. */
650 && !DECL_OVERLOADED_OPERATOR_P (decl))
652 unmangled_name:;
654 if (top_level)
655 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
656 else
658 /* The standard notes: "The <encoding> of an extern "C"
659 function is treated like global-scope data, i.e. as its
660 <source-name> without a type." We cannot write
661 overloaded operators that way though, because it contains
662 characters invalid in assembler. */
663 write_string ("_Z");
664 write_source_name (DECL_NAME (decl));
667 else if (VAR_P (decl)
668 /* Variable template instantiations are mangled. */
669 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
670 /* The names of non-static global variables aren't mangled. */
671 && DECL_EXTERNAL_LINKAGE_P (decl)
672 && (CP_DECL_CONTEXT (decl) == global_namespace
673 /* And neither are `extern "C"' variables. */
674 || DECL_EXTERN_C_P (decl)))
676 goto unmangled_name;
678 else
680 write_string ("_Z");
681 write_encoding (decl);
685 /* <encoding> ::= <function name> <bare-function-type>
686 ::= <data name> */
688 static void
689 write_encoding (const tree decl)
691 MANGLE_TRACE_TREE ("encoding", decl);
693 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
695 /* For overloaded operators write just the mangled name
696 without arguments. */
697 if (DECL_OVERLOADED_OPERATOR_P (decl))
698 write_name (decl, /*ignore_local_scope=*/0);
699 else
700 write_source_name (DECL_NAME (decl));
701 return;
704 write_name (decl, /*ignore_local_scope=*/0);
705 if (TREE_CODE (decl) == FUNCTION_DECL)
707 tree fn_type;
708 tree d;
710 if (decl_is_template_id (decl, NULL))
712 fn_type = get_mostly_instantiated_function_type (decl);
713 /* FN_TYPE will not have parameter types for in-charge or
714 VTT parameters. Therefore, we pass NULL_TREE to
715 write_bare_function_type -- otherwise, it will get
716 confused about which artificial parameters to skip. */
717 d = NULL_TREE;
719 else
721 fn_type = TREE_TYPE (decl);
722 d = decl;
725 write_bare_function_type (fn_type,
726 (!DECL_CONSTRUCTOR_P (decl)
727 && !DECL_DESTRUCTOR_P (decl)
728 && !DECL_CONV_FN_P (decl)
729 && decl_is_template_id (decl, NULL)),
734 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
735 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
737 static tree
738 decl_mangling_context (tree decl)
740 tree tcontext = targetm.cxx.decl_mangling_context (decl);
742 if (tcontext != NULL_TREE)
743 return tcontext;
745 if (TREE_CODE (decl) == TEMPLATE_DECL
746 && DECL_TEMPLATE_RESULT (decl))
747 decl = DECL_TEMPLATE_RESULT (decl);
749 if (TREE_CODE (decl) == TYPE_DECL
750 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
752 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
753 if (extra)
754 return extra;
756 else if (TREE_CODE (decl) == TYPE_DECL
757 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
758 /* template type parms have no mangling context. */
759 return NULL_TREE;
760 return CP_DECL_CONTEXT (decl);
763 /* <name> ::= <unscoped-name>
764 ::= <unscoped-template-name> <template-args>
765 ::= <nested-name>
766 ::= <local-name>
768 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
769 called from <local-name>, which mangles the enclosing scope
770 elsewhere and then uses this function to mangle just the part
771 underneath the function scope. So don't use the <local-name>
772 production, to avoid an infinite recursion. */
774 static void
775 write_name (tree decl, const int ignore_local_scope)
777 tree context;
779 MANGLE_TRACE_TREE ("name", decl);
781 if (TREE_CODE (decl) == TYPE_DECL)
783 /* In case this is a typedef, fish out the corresponding
784 TYPE_DECL for the main variant. */
785 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
788 context = decl_mangling_context (decl);
790 gcc_assert (context != NULL_TREE);
792 if (abi_version_crosses (7)
793 && ignore_local_scope
794 && TREE_CODE (context) == PARM_DECL)
795 G.need_abi_warning = 1;
797 /* A decl in :: or ::std scope is treated specially. The former is
798 mangled using <unscoped-name> or <unscoped-template-name>, the
799 latter with a special substitution. Also, a name that is
800 directly in a local function scope is also mangled with
801 <unscoped-name> rather than a full <nested-name>. */
802 if (context == global_namespace
803 || DECL_NAMESPACE_STD_P (context)
804 || (ignore_local_scope
805 && (TREE_CODE (context) == FUNCTION_DECL
806 || (abi_version_at_least (7)
807 && TREE_CODE (context) == PARM_DECL))))
809 tree template_info;
810 /* Is this a template instance? */
811 if (decl_is_template_id (decl, &template_info))
813 /* Yes: use <unscoped-template-name>. */
814 write_unscoped_template_name (TI_TEMPLATE (template_info));
815 write_template_args (TI_ARGS (template_info));
817 else
818 /* Everything else gets an <unqualified-name>. */
819 write_unscoped_name (decl);
821 else
823 /* Handle local names, unless we asked not to (that is, invoked
824 under <local-name>, to handle only the part of the name under
825 the local scope). */
826 if (!ignore_local_scope)
828 /* Scan up the list of scope context, looking for a
829 function. If we find one, this entity is in local
830 function scope. local_entity tracks context one scope
831 level down, so it will contain the element that's
832 directly in that function's scope, either decl or one of
833 its enclosing scopes. */
834 tree local_entity = decl;
835 while (context != global_namespace)
837 /* Make sure we're always dealing with decls. */
838 if (TYPE_P (context))
839 context = TYPE_NAME (context);
840 /* Is this a function? */
841 if (TREE_CODE (context) == FUNCTION_DECL
842 || TREE_CODE (context) == PARM_DECL)
844 /* Yes, we have local scope. Use the <local-name>
845 production for the innermost function scope. */
846 write_local_name (context, local_entity, decl);
847 return;
849 /* Up one scope level. */
850 local_entity = context;
851 context = decl_mangling_context (context);
854 /* No local scope found? Fall through to <nested-name>. */
857 /* Other decls get a <nested-name> to encode their scope. */
858 write_nested_name (decl);
862 /* <unscoped-name> ::= <unqualified-name>
863 ::= St <unqualified-name> # ::std:: */
865 static void
866 write_unscoped_name (const tree decl)
868 tree context = decl_mangling_context (decl);
870 MANGLE_TRACE_TREE ("unscoped-name", decl);
872 /* Is DECL in ::std? */
873 if (DECL_NAMESPACE_STD_P (context))
875 write_string ("St");
876 write_unqualified_name (decl);
878 else
880 /* If not, it should be either in the global namespace, or directly
881 in a local function scope. A lambda can also be mangled in the
882 scope of a default argument. */
883 gcc_assert (context == global_namespace
884 || TREE_CODE (context) == PARM_DECL
885 || TREE_CODE (context) == FUNCTION_DECL);
887 write_unqualified_name (decl);
891 /* <unscoped-template-name> ::= <unscoped-name>
892 ::= <substitution> */
894 static void
895 write_unscoped_template_name (const tree decl)
897 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
899 if (find_substitution (decl))
900 return;
901 write_unscoped_name (decl);
902 add_substitution (decl);
905 /* Write the nested name, including CV-qualifiers, of DECL.
907 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
908 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
910 <ref-qualifier> ::= R # & ref-qualifier
911 ::= O # && ref-qualifier
912 <CV-qualifiers> ::= [r] [V] [K] */
914 static void
915 write_nested_name (const tree decl)
917 tree template_info;
919 MANGLE_TRACE_TREE ("nested-name", decl);
921 write_char ('N');
923 /* Write CV-qualifiers, if this is a member function. */
924 if (TREE_CODE (decl) == FUNCTION_DECL
925 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
927 if (DECL_VOLATILE_MEMFUNC_P (decl))
928 write_char ('V');
929 if (DECL_CONST_MEMFUNC_P (decl))
930 write_char ('K');
931 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
933 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
934 write_char ('O');
935 else
936 write_char ('R');
940 /* Is this a template instance? */
941 if (decl_is_template_id (decl, &template_info))
943 /* Yes, use <template-prefix>. */
944 write_template_prefix (decl);
945 write_template_args (TI_ARGS (template_info));
947 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
949 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
950 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
952 write_template_prefix (decl);
953 write_template_args (TREE_OPERAND (name, 1));
955 else
957 write_prefix (decl_mangling_context (decl));
958 write_unqualified_name (decl);
961 else
963 /* No, just use <prefix> */
964 write_prefix (decl_mangling_context (decl));
965 write_unqualified_name (decl);
967 write_char ('E');
970 /* <prefix> ::= <prefix> <unqualified-name>
971 ::= <template-param>
972 ::= <template-prefix> <template-args>
973 ::= <decltype>
974 ::= # empty
975 ::= <substitution> */
977 static void
978 write_prefix (const tree node)
980 tree decl;
981 /* Non-NULL if NODE represents a template-id. */
982 tree template_info = NULL;
984 if (node == NULL
985 || node == global_namespace)
986 return;
988 MANGLE_TRACE_TREE ("prefix", node);
990 if (TREE_CODE (node) == DECLTYPE_TYPE)
992 write_type (node);
993 return;
996 if (find_substitution (node))
997 return;
999 if (DECL_P (node))
1001 /* If this is a function or parm decl, that means we've hit function
1002 scope, so this prefix must be for a local name. In this
1003 case, we're under the <local-name> production, which encodes
1004 the enclosing function scope elsewhere. So don't continue
1005 here. */
1006 if (TREE_CODE (node) == FUNCTION_DECL
1007 || TREE_CODE (node) == PARM_DECL)
1008 return;
1010 decl = node;
1011 decl_is_template_id (decl, &template_info);
1013 else
1015 /* Node is a type. */
1016 decl = TYPE_NAME (node);
1017 if (CLASSTYPE_TEMPLATE_ID_P (node))
1018 template_info = TYPE_TEMPLATE_INFO (node);
1021 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM)
1022 write_template_param (node);
1023 else if (template_info != NULL)
1024 /* Templated. */
1026 write_template_prefix (decl);
1027 write_template_args (TI_ARGS (template_info));
1029 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1031 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1032 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1034 write_template_prefix (decl);
1035 write_template_args (TREE_OPERAND (name, 1));
1037 else
1039 write_prefix (decl_mangling_context (decl));
1040 write_unqualified_name (decl);
1043 else
1044 /* Not templated. */
1046 write_prefix (decl_mangling_context (decl));
1047 write_unqualified_name (decl);
1048 if (VAR_P (decl)
1049 || TREE_CODE (decl) == FIELD_DECL)
1051 /* <data-member-prefix> := <member source-name> M */
1052 write_char ('M');
1053 return;
1057 add_substitution (node);
1060 /* <template-prefix> ::= <prefix> <template component>
1061 ::= <template-param>
1062 ::= <substitution> */
1064 static void
1065 write_template_prefix (const tree node)
1067 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1068 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1069 tree context = decl_mangling_context (decl);
1070 tree template_info;
1071 tree templ;
1072 tree substitution;
1074 MANGLE_TRACE_TREE ("template-prefix", node);
1076 /* Find the template decl. */
1077 if (decl_is_template_id (decl, &template_info))
1078 templ = TI_TEMPLATE (template_info);
1079 else if (TREE_CODE (type) == TYPENAME_TYPE)
1080 /* For a typename type, all we have is the name. */
1081 templ = DECL_NAME (decl);
1082 else
1084 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1086 templ = TYPE_TI_TEMPLATE (type);
1089 /* For a member template, though, the template name for the
1090 innermost name must have all the outer template levels
1091 instantiated. For instance, consider
1093 template<typename T> struct Outer {
1094 template<typename U> struct Inner {};
1097 The template name for `Inner' in `Outer<int>::Inner<float>' is
1098 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1099 levels separately, so there's no TEMPLATE_DECL available for this
1100 (there's only `Outer<T>::Inner<U>').
1102 In order to get the substitutions right, we create a special
1103 TREE_LIST to represent the substitution candidate for a nested
1104 template. The TREE_PURPOSE is the template's context, fully
1105 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1106 template.
1108 So, for the example above, `Outer<int>::Inner' is represented as a
1109 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1110 and whose value is `Outer<T>::Inner<U>'. */
1111 if (TYPE_P (context))
1112 substitution = build_tree_list (context, templ);
1113 else
1114 substitution = templ;
1116 if (find_substitution (substitution))
1117 return;
1119 if (TREE_TYPE (templ)
1120 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM)
1121 write_template_param (TREE_TYPE (templ));
1122 else
1124 write_prefix (context);
1125 write_unqualified_name (decl);
1128 add_substitution (substitution);
1131 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1132 mangled through special entry points.
1134 <unqualified-name> ::= <operator-name>
1135 ::= <special-name>
1136 ::= <source-name>
1137 ::= <unnamed-type-name>
1138 ::= <local-source-name>
1140 <local-source-name> ::= L <source-name> <discriminator> */
1142 static void
1143 write_unqualified_id (tree identifier)
1145 if (IDENTIFIER_TYPENAME_P (identifier))
1146 write_conversion_operator_name (TREE_TYPE (identifier));
1147 else if (IDENTIFIER_OPNAME_P (identifier))
1149 int i;
1150 const char *mangled_name = NULL;
1152 /* Unfortunately, there is no easy way to go from the
1153 name of the operator back to the corresponding tree
1154 code. */
1155 for (i = 0; i < MAX_TREE_CODES; ++i)
1156 if (operator_name_info[i].identifier == identifier)
1158 /* The ABI says that we prefer binary operator
1159 names to unary operator names. */
1160 if (operator_name_info[i].arity == 2)
1162 mangled_name = operator_name_info[i].mangled_name;
1163 break;
1165 else if (!mangled_name)
1166 mangled_name = operator_name_info[i].mangled_name;
1168 else if (assignment_operator_name_info[i].identifier
1169 == identifier)
1171 mangled_name
1172 = assignment_operator_name_info[i].mangled_name;
1173 break;
1175 write_string (mangled_name);
1177 else if (UDLIT_OPER_P (identifier))
1178 write_literal_operator_name (identifier);
1179 else
1180 write_source_name (identifier);
1183 static void
1184 write_unqualified_name (tree decl)
1186 MANGLE_TRACE_TREE ("unqualified-name", decl);
1188 if (identifier_p (decl))
1190 write_unqualified_id (decl);
1191 return;
1194 bool found = false;
1196 if (DECL_NAME (decl) == NULL_TREE)
1198 found = true;
1199 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1200 write_source_name (DECL_ASSEMBLER_NAME (decl));
1202 else if (DECL_DECLARES_FUNCTION_P (decl))
1204 found = true;
1205 if (DECL_CONSTRUCTOR_P (decl))
1206 write_special_name_constructor (decl);
1207 else if (DECL_DESTRUCTOR_P (decl))
1208 write_special_name_destructor (decl);
1209 else if (DECL_CONV_FN_P (decl))
1211 /* Conversion operator. Handle it right here.
1212 <operator> ::= cv <type> */
1213 tree type;
1214 if (decl_is_template_id (decl, NULL))
1216 tree fn_type;
1217 fn_type = get_mostly_instantiated_function_type (decl);
1218 type = TREE_TYPE (fn_type);
1220 else if (FNDECL_USED_AUTO (decl))
1221 type = (DECL_STRUCT_FUNCTION (decl)->language
1222 ->x_auto_return_pattern);
1223 else
1224 type = DECL_CONV_FN_TYPE (decl);
1225 write_conversion_operator_name (type);
1227 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1229 operator_name_info_t *oni;
1230 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1231 oni = assignment_operator_name_info;
1232 else
1233 oni = operator_name_info;
1235 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1237 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1238 write_literal_operator_name (DECL_NAME (decl));
1239 else
1240 found = false;
1243 if (found)
1244 /* OK */;
1245 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1246 && DECL_NAMESPACE_SCOPE_P (decl)
1247 && decl_linkage (decl) == lk_internal)
1249 MANGLE_TRACE_TREE ("local-source-name", decl);
1250 write_char ('L');
1251 write_source_name (DECL_NAME (decl));
1252 /* The default discriminator is 1, and that's all we ever use,
1253 so there's no code to output one here. */
1255 else
1257 tree type = TREE_TYPE (decl);
1259 if (TREE_CODE (decl) == TYPE_DECL
1260 && TYPE_ANONYMOUS_P (type))
1261 write_unnamed_type_name (type);
1262 else if (TREE_CODE (decl) == TYPE_DECL
1263 && LAMBDA_TYPE_P (type))
1264 write_closure_type_name (type);
1265 else
1266 write_source_name (DECL_NAME (decl));
1269 /* We use the ABI tags from the primary template, ignoring tags on any
1270 specializations. This is necessary because C++ doesn't require a
1271 specialization to be declared before it is used unless the use
1272 requires a complete type, but we need to get the tags right on
1273 incomplete types as well. */
1274 if (tree tmpl = most_general_template (decl))
1275 decl = DECL_TEMPLATE_RESULT (tmpl);
1276 /* Don't crash on an unbound class template. */
1277 if (decl)
1279 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1280 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1281 : DECL_ATTRIBUTES (decl));
1282 write_abi_tags (lookup_attribute ("abi_tag", attrs));
1286 /* Write the unqualified-name for a conversion operator to TYPE. */
1288 static void
1289 write_conversion_operator_name (const tree type)
1291 write_string ("cv");
1292 write_type (type);
1295 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1297 <source-name> ::= </length/ number> <identifier> */
1299 static void
1300 write_source_name (tree identifier)
1302 MANGLE_TRACE_TREE ("source-name", identifier);
1304 /* Never write the whole template-id name including the template
1305 arguments; we only want the template name. */
1306 if (IDENTIFIER_TEMPLATE (identifier))
1307 identifier = IDENTIFIER_TEMPLATE (identifier);
1309 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1310 write_identifier (IDENTIFIER_POINTER (identifier));
1313 /* Compare two TREE_STRINGs like strcmp. */
1316 tree_string_cmp (const void *p1, const void *p2)
1318 if (p1 == p2)
1319 return 0;
1320 tree s1 = *(const tree*)p1;
1321 tree s2 = *(const tree*)p2;
1322 return strcmp (TREE_STRING_POINTER (s1),
1323 TREE_STRING_POINTER (s2));
1326 /* ID is the name of a function or type with abi_tags attribute TAGS.
1327 Write out the name, suitably decorated. */
1329 static void
1330 write_abi_tags (tree tags)
1332 if (tags == NULL_TREE)
1333 return;
1335 tags = TREE_VALUE (tags);
1337 vec<tree, va_gc> * vec = make_tree_vector();
1339 for (tree t = tags; t; t = TREE_CHAIN (t))
1341 if (ABI_TAG_IMPLICIT (t))
1342 continue;
1343 tree str = TREE_VALUE (t);
1344 vec_safe_push (vec, str);
1347 vec->qsort (tree_string_cmp);
1349 unsigned i; tree str;
1350 FOR_EACH_VEC_ELT (*vec, i, str)
1352 write_string ("B");
1353 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1354 write_identifier (TREE_STRING_POINTER (str));
1357 release_tree_vector (vec);
1360 /* Write a user-defined literal operator.
1361 ::= li <source-name> # "" <source-name>
1362 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1364 static void
1365 write_literal_operator_name (tree identifier)
1367 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1368 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1369 write_unsigned_number (strlen (suffix));
1370 write_identifier (suffix);
1373 /* Encode 0 as _, and 1+ as n-1_. */
1375 static void
1376 write_compact_number (int num)
1378 if (num > 0)
1379 write_unsigned_number (num - 1);
1380 write_char ('_');
1383 /* Return how many unnamed types precede TYPE in its enclosing class. */
1385 static int
1386 nested_anon_class_index (tree type)
1388 int index = 0;
1389 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1390 for (; member; member = DECL_CHAIN (member))
1391 if (DECL_IMPLICIT_TYPEDEF_P (member))
1393 tree memtype = TREE_TYPE (member);
1394 if (memtype == type)
1395 return index;
1396 else if (TYPE_ANONYMOUS_P (memtype))
1397 ++index;
1400 gcc_unreachable ();
1403 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1405 static void
1406 write_unnamed_type_name (const tree type)
1408 int discriminator;
1409 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1411 if (TYPE_FUNCTION_SCOPE_P (type))
1412 discriminator = local_class_index (type);
1413 else if (TYPE_CLASS_SCOPE_P (type))
1414 discriminator = nested_anon_class_index (type);
1415 else
1417 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1418 /* Just use the old mangling at namespace scope. */
1419 write_source_name (TYPE_IDENTIFIER (type));
1420 return;
1423 write_string ("Ut");
1424 write_compact_number (discriminator);
1427 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1428 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1430 static void
1431 write_closure_type_name (const tree type)
1433 tree fn = lambda_function (type);
1434 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1435 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1437 MANGLE_TRACE_TREE ("closure-type-name", type);
1439 write_string ("Ul");
1440 write_method_parms (parms, /*method_p=*/1, fn);
1441 write_char ('E');
1442 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1445 /* Convert NUMBER to ascii using base BASE and generating at least
1446 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1447 into which to store the characters. Returns the number of
1448 characters generated (these will be laid out in advance of where
1449 BUFFER points). */
1451 static int
1452 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1453 char *buffer, const unsigned int min_digits)
1455 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1456 unsigned digits = 0;
1458 while (number)
1460 unsigned HOST_WIDE_INT d = number / base;
1462 *--buffer = base_digits[number - d * base];
1463 digits++;
1464 number = d;
1466 while (digits < min_digits)
1468 *--buffer = base_digits[0];
1469 digits++;
1471 return digits;
1474 /* Non-terminal <number>.
1476 <number> ::= [n] </decimal integer/> */
1478 static void
1479 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1480 const unsigned int base)
1482 char buffer[sizeof (HOST_WIDE_INT) * 8];
1483 unsigned count = 0;
1485 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1487 write_char ('n');
1488 number = -((HOST_WIDE_INT) number);
1490 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1491 write_chars (buffer + sizeof (buffer) - count, count);
1494 /* Write out an integral CST in decimal. Most numbers are small, and
1495 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1496 bigger than that, which we must deal with. */
1498 static inline void
1499 write_integer_cst (const tree cst)
1501 int sign = tree_int_cst_sgn (cst);
1502 widest_int abs_value = wi::abs (wi::to_widest (cst));
1503 if (!wi::fits_uhwi_p (abs_value))
1505 /* A bignum. We do this in chunks, each of which fits in a
1506 HOST_WIDE_INT. */
1507 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1508 unsigned HOST_WIDE_INT chunk;
1509 unsigned chunk_digits;
1510 char *ptr = buffer + sizeof (buffer);
1511 unsigned count = 0;
1512 tree n, base, type;
1513 int done;
1515 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1516 representable. */
1517 chunk = 1000000000;
1518 chunk_digits = 9;
1520 if (sizeof (HOST_WIDE_INT) >= 8)
1522 /* It is at least 64 bits, so 10^18 is representable. */
1523 chunk_digits = 18;
1524 chunk *= chunk;
1527 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1528 base = build_int_cstu (type, chunk);
1529 n = wide_int_to_tree (type, cst);
1531 if (sign < 0)
1533 write_char ('n');
1534 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1538 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1539 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1540 unsigned c;
1542 done = integer_zerop (d);
1543 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1544 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1545 done ? 1 : chunk_digits);
1546 ptr -= c;
1547 count += c;
1548 n = d;
1550 while (!done);
1551 write_chars (ptr, count);
1553 else
1555 /* A small num. */
1556 if (sign < 0)
1557 write_char ('n');
1558 write_unsigned_number (abs_value.to_uhwi ());
1562 /* Write out a floating-point literal.
1564 "Floating-point literals are encoded using the bit pattern of the
1565 target processor's internal representation of that number, as a
1566 fixed-length lowercase hexadecimal string, high-order bytes first
1567 (even if the target processor would store low-order bytes first).
1568 The "n" prefix is not used for floating-point literals; the sign
1569 bit is encoded with the rest of the number.
1571 Here are some examples, assuming the IEEE standard representation
1572 for floating point numbers. (Spaces are for readability, not
1573 part of the encoding.)
1575 1.0f Lf 3f80 0000 E
1576 -1.0f Lf bf80 0000 E
1577 1.17549435e-38f Lf 0080 0000 E
1578 1.40129846e-45f Lf 0000 0001 E
1579 0.0f Lf 0000 0000 E"
1581 Caller is responsible for the Lx and the E. */
1582 static void
1583 write_real_cst (const tree value)
1585 long target_real[4]; /* largest supported float */
1586 char buffer[9]; /* eight hex digits in a 32-bit number */
1587 int i, limit, dir;
1589 tree type = TREE_TYPE (value);
1590 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1592 real_to_target (target_real, &TREE_REAL_CST (value),
1593 TYPE_MODE (type));
1595 /* The value in target_real is in the target word order,
1596 so we must write it out backward if that happens to be
1597 little-endian. write_number cannot be used, it will
1598 produce uppercase. */
1599 if (FLOAT_WORDS_BIG_ENDIAN)
1600 i = 0, limit = words, dir = 1;
1601 else
1602 i = words - 1, limit = -1, dir = -1;
1604 for (; i != limit; i += dir)
1606 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1607 write_chars (buffer, 8);
1611 /* Non-terminal <identifier>.
1613 <identifier> ::= </unqualified source code identifier> */
1615 static void
1616 write_identifier (const char *identifier)
1618 MANGLE_TRACE ("identifier", identifier);
1619 write_string (identifier);
1622 /* Handle constructor productions of non-terminal <special-name>.
1623 CTOR is a constructor FUNCTION_DECL.
1625 <special-name> ::= C1 # complete object constructor
1626 ::= C2 # base object constructor
1627 ::= C3 # complete object allocating constructor
1629 Currently, allocating constructors are never used. */
1631 static void
1632 write_special_name_constructor (const tree ctor)
1634 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1635 write_string ("C2");
1636 /* This is the old-style "[unified]" constructor.
1637 In some cases, we may emit this function and call
1638 it from the clones in order to share code and save space. */
1639 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor))
1640 write_string ("C4");
1641 else
1643 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor));
1644 write_string ("C1");
1648 /* Handle destructor productions of non-terminal <special-name>.
1649 DTOR is a destructor FUNCTION_DECL.
1651 <special-name> ::= D0 # deleting (in-charge) destructor
1652 ::= D1 # complete object (in-charge) destructor
1653 ::= D2 # base object (not-in-charge) destructor */
1655 static void
1656 write_special_name_destructor (const tree dtor)
1658 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1659 write_string ("D0");
1660 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1661 write_string ("D2");
1662 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor))
1663 /* This is the old-style "[unified]" destructor.
1664 In some cases, we may emit this function and call
1665 it from the clones in order to share code and save space. */
1666 write_string ("D4");
1667 else
1669 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor));
1670 write_string ("D1");
1674 /* Scan the vector of local classes and return how many others with the
1675 same name (or same no name) and context precede ENTITY. */
1677 static int
1678 local_class_index (tree entity)
1680 int ix, discriminator = 0;
1681 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1682 : TYPE_IDENTIFIER (entity));
1683 tree ctx = TYPE_CONTEXT (entity);
1684 for (ix = 0; ; ix++)
1686 tree type = (*local_classes)[ix];
1687 if (type == entity)
1688 return discriminator;
1689 if (TYPE_CONTEXT (type) == ctx
1690 && (name ? TYPE_IDENTIFIER (type) == name
1691 : TYPE_ANONYMOUS_P (type)))
1692 ++discriminator;
1694 gcc_unreachable ();
1697 /* Return the discriminator for ENTITY appearing inside
1698 FUNCTION. The discriminator is the lexical ordinal of VAR among
1699 entities with the same name in the same FUNCTION. */
1701 static int
1702 discriminator_for_local_entity (tree entity)
1704 if (DECL_DISCRIMINATOR_P (entity))
1706 if (DECL_DISCRIMINATOR_SET_P (entity))
1707 return DECL_DISCRIMINATOR (entity);
1708 else
1709 /* The first entity with a particular name doesn't get
1710 DECL_DISCRIMINATOR set up. */
1711 return 0;
1713 else if (TREE_CODE (entity) == TYPE_DECL)
1715 /* Scan the list of local classes. */
1716 entity = TREE_TYPE (entity);
1718 /* Lambdas and unnamed types have their own discriminators. */
1719 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1720 return 0;
1722 return local_class_index (entity);
1724 else
1725 gcc_unreachable ();
1728 /* Return the discriminator for STRING, a string literal used inside
1729 FUNCTION. The discriminator is the lexical ordinal of STRING among
1730 string literals used in FUNCTION. */
1732 static int
1733 discriminator_for_string_literal (tree /*function*/,
1734 tree /*string*/)
1736 /* For now, we don't discriminate amongst string literals. */
1737 return 0;
1740 /* <discriminator> := _ <number>
1742 The discriminator is used only for the second and later occurrences
1743 of the same name within a single function. In this case <number> is
1744 n - 2, if this is the nth occurrence, in lexical order. */
1746 static void
1747 write_discriminator (const int discriminator)
1749 /* If discriminator is zero, don't write anything. Otherwise... */
1750 if (discriminator > 0)
1752 write_char ('_');
1753 write_unsigned_number (discriminator - 1);
1757 /* Mangle the name of a function-scope entity. FUNCTION is the
1758 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1759 default argument scope. ENTITY is the decl for the entity itself.
1760 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1761 either ENTITY itself or an enclosing scope of ENTITY.
1763 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1764 := Z <function encoding> E s [<discriminator>]
1765 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1767 static void
1768 write_local_name (tree function, const tree local_entity,
1769 const tree entity)
1771 tree parm = NULL_TREE;
1773 MANGLE_TRACE_TREE ("local-name", entity);
1775 if (TREE_CODE (function) == PARM_DECL)
1777 parm = function;
1778 function = DECL_CONTEXT (parm);
1781 write_char ('Z');
1782 write_encoding (function);
1783 write_char ('E');
1785 /* For this purpose, parameters are numbered from right-to-left. */
1786 if (parm)
1788 tree t;
1789 int i = 0;
1790 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1792 if (t == parm)
1793 i = 1;
1794 else if (i)
1795 ++i;
1797 write_char ('d');
1798 write_compact_number (i - 1);
1801 if (TREE_CODE (entity) == STRING_CST)
1803 write_char ('s');
1804 write_discriminator (discriminator_for_string_literal (function,
1805 entity));
1807 else
1809 /* Now the <entity name>. Let write_name know its being called
1810 from <local-name>, so it doesn't try to process the enclosing
1811 function scope again. */
1812 write_name (entity, /*ignore_local_scope=*/1);
1813 write_discriminator (discriminator_for_local_entity (local_entity));
1817 /* Non-terminals <type> and <CV-qualifier>.
1819 <type> ::= <builtin-type>
1820 ::= <function-type>
1821 ::= <class-enum-type>
1822 ::= <array-type>
1823 ::= <pointer-to-member-type>
1824 ::= <template-param>
1825 ::= <substitution>
1826 ::= <CV-qualifier>
1827 ::= P <type> # pointer-to
1828 ::= R <type> # reference-to
1829 ::= C <type> # complex pair (C 2000)
1830 ::= G <type> # imaginary (C 2000) [not supported]
1831 ::= U <source-name> <type> # vendor extended type qualifier
1833 C++0x extensions
1835 <type> ::= RR <type> # rvalue reference-to
1836 <type> ::= Dt <expression> # decltype of an id-expression or
1837 # class member access
1838 <type> ::= DT <expression> # decltype of an expression
1839 <type> ::= Dn # decltype of nullptr
1841 TYPE is a type node. */
1843 static void
1844 write_type (tree type)
1846 /* This gets set to nonzero if TYPE turns out to be a (possibly
1847 CV-qualified) builtin type. */
1848 int is_builtin_type = 0;
1850 MANGLE_TRACE_TREE ("type", type);
1852 if (type == error_mark_node)
1853 return;
1855 type = canonicalize_for_substitution (type);
1856 if (find_substitution (type))
1857 return;
1860 if (write_CV_qualifiers_for_type (type) > 0)
1861 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1862 mangle the unqualified type. The recursive call is needed here
1863 since both the qualified and unqualified types are substitution
1864 candidates. */
1866 tree t = TYPE_MAIN_VARIANT (type);
1867 if (TREE_CODE (t) == FUNCTION_TYPE
1868 || TREE_CODE (t) == METHOD_TYPE)
1870 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1871 if (abi_version_at_least (8))
1872 /* Avoid adding the unqualified function type as a substitution. */
1873 write_function_type (t);
1874 else
1875 write_type (t);
1876 if (abi_version_crosses (8))
1877 G.need_abi_warning = 1;
1879 else
1880 write_type (t);
1882 else if (TREE_CODE (type) == ARRAY_TYPE)
1883 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1884 so that the cv-qualification of the element type is available
1885 in write_array_type. */
1886 write_array_type (type);
1887 else
1889 tree type_orig = type;
1891 /* See through any typedefs. */
1892 type = TYPE_MAIN_VARIANT (type);
1893 if (TREE_CODE (type) == FUNCTION_TYPE
1894 || TREE_CODE (type) == METHOD_TYPE)
1895 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1897 /* According to the C++ ABI, some library classes are passed the
1898 same as the scalar type of their single member and use the same
1899 mangling. */
1900 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1901 type = TREE_TYPE (first_field (type));
1903 if (TYPE_PTRDATAMEM_P (type))
1904 write_pointer_to_member_type (type);
1905 else
1907 /* Handle any target-specific fundamental types. */
1908 const char *target_mangling
1909 = targetm.mangle_type (type_orig);
1911 if (target_mangling)
1913 write_string (target_mangling);
1914 /* Add substitutions for types other than fundamental
1915 types. */
1916 if (!VOID_TYPE_P (type)
1917 && TREE_CODE (type) != INTEGER_TYPE
1918 && TREE_CODE (type) != REAL_TYPE
1919 && TREE_CODE (type) != BOOLEAN_TYPE)
1920 add_substitution (type);
1921 return;
1924 switch (TREE_CODE (type))
1926 case VOID_TYPE:
1927 case BOOLEAN_TYPE:
1928 case INTEGER_TYPE: /* Includes wchar_t. */
1929 case REAL_TYPE:
1930 case FIXED_POINT_TYPE:
1932 /* If this is a typedef, TYPE may not be one of
1933 the standard builtin type nodes, but an alias of one. Use
1934 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1935 write_builtin_type (TYPE_MAIN_VARIANT (type));
1936 ++is_builtin_type;
1938 break;
1940 case COMPLEX_TYPE:
1941 write_char ('C');
1942 write_type (TREE_TYPE (type));
1943 break;
1945 case FUNCTION_TYPE:
1946 case METHOD_TYPE:
1947 write_function_type (type);
1948 break;
1950 case UNION_TYPE:
1951 case RECORD_TYPE:
1952 case ENUMERAL_TYPE:
1953 /* A pointer-to-member function is represented as a special
1954 RECORD_TYPE, so check for this first. */
1955 if (TYPE_PTRMEMFUNC_P (type))
1956 write_pointer_to_member_type (type);
1957 else
1958 write_class_enum_type (type);
1959 break;
1961 case TYPENAME_TYPE:
1962 case UNBOUND_CLASS_TEMPLATE:
1963 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1964 ordinary nested names. */
1965 write_nested_name (TYPE_STUB_DECL (type));
1966 break;
1968 case POINTER_TYPE:
1969 case REFERENCE_TYPE:
1970 if (TYPE_PTR_P (type))
1971 write_char ('P');
1972 else if (TYPE_REF_IS_RVALUE (type))
1973 write_char ('O');
1974 else
1975 write_char ('R');
1977 tree target = TREE_TYPE (type);
1978 /* Attribute const/noreturn are not reflected in mangling.
1979 We strip them here rather than at a lower level because
1980 a typedef or template argument can have function type
1981 with function-cv-quals (that use the same representation),
1982 but you can't have a pointer/reference to such a type. */
1983 if (TREE_CODE (target) == FUNCTION_TYPE)
1985 if (abi_version_crosses (5)
1986 && TYPE_QUALS (target) != TYPE_UNQUALIFIED)
1987 G.need_abi_warning = 1;
1988 if (abi_version_at_least (5))
1989 target = build_qualified_type (target, TYPE_UNQUALIFIED);
1991 write_type (target);
1993 break;
1995 case TEMPLATE_TYPE_PARM:
1996 if (is_auto (type))
1998 if (AUTO_IS_DECLTYPE (type))
1999 write_identifier ("Dc");
2000 else
2001 write_identifier ("Da");
2002 ++is_builtin_type;
2003 break;
2005 /* else fall through. */
2006 case TEMPLATE_PARM_INDEX:
2007 write_template_param (type);
2008 break;
2010 case TEMPLATE_TEMPLATE_PARM:
2011 write_template_template_param (type);
2012 break;
2014 case BOUND_TEMPLATE_TEMPLATE_PARM:
2015 write_template_template_param (type);
2016 write_template_args
2017 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2018 break;
2020 case VECTOR_TYPE:
2021 if (abi_version_at_least (4))
2023 write_string ("Dv");
2024 /* Non-constant vector size would be encoded with
2025 _ expression, but we don't support that yet. */
2026 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2027 write_char ('_');
2029 else
2030 write_string ("U8__vector");
2031 if (abi_version_crosses (4))
2032 G.need_abi_warning = 1;
2033 write_type (TREE_TYPE (type));
2034 break;
2036 case TYPE_PACK_EXPANSION:
2037 write_string ("Dp");
2038 write_type (PACK_EXPANSION_PATTERN (type));
2039 break;
2041 case DECLTYPE_TYPE:
2042 /* These shouldn't make it into mangling. */
2043 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2044 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2046 /* In ABI <5, we stripped decltype of a plain decl. */
2047 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2049 tree expr = DECLTYPE_TYPE_EXPR (type);
2050 tree etype = NULL_TREE;
2051 switch (TREE_CODE (expr))
2053 case VAR_DECL:
2054 case PARM_DECL:
2055 case RESULT_DECL:
2056 case FUNCTION_DECL:
2057 case CONST_DECL:
2058 case TEMPLATE_PARM_INDEX:
2059 etype = TREE_TYPE (expr);
2060 break;
2062 default:
2063 break;
2066 if (etype && !type_uses_auto (etype))
2068 if (abi_version_crosses (5))
2069 G.need_abi_warning = 1;
2070 if (!abi_version_at_least (5))
2072 write_type (etype);
2073 return;
2078 write_char ('D');
2079 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2080 write_char ('t');
2081 else
2082 write_char ('T');
2083 ++cp_unevaluated_operand;
2084 write_expression (DECLTYPE_TYPE_EXPR (type));
2085 --cp_unevaluated_operand;
2086 write_char ('E');
2087 break;
2089 case NULLPTR_TYPE:
2090 write_string ("Dn");
2091 if (abi_version_at_least (7))
2092 ++is_builtin_type;
2093 if (abi_version_crosses (7))
2094 G.need_abi_warning = 1;
2095 break;
2097 case TYPEOF_TYPE:
2098 sorry ("mangling typeof, use decltype instead");
2099 break;
2101 case UNDERLYING_TYPE:
2102 sorry ("mangling __underlying_type");
2103 break;
2105 case LANG_TYPE:
2106 /* fall through. */
2108 default:
2109 gcc_unreachable ();
2114 /* Types other than builtin types are substitution candidates. */
2115 if (!is_builtin_type)
2116 add_substitution (type);
2119 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2120 CV-qualifiers written for TYPE.
2122 <CV-qualifiers> ::= [r] [V] [K] */
2124 static int
2125 write_CV_qualifiers_for_type (const tree type)
2127 int num_qualifiers = 0;
2129 /* The order is specified by:
2131 "In cases where multiple order-insensitive qualifiers are
2132 present, they should be ordered 'K' (closest to the base type),
2133 'V', 'r', and 'U' (farthest from the base type) ..."
2135 Note that we do not use cp_type_quals below; given "const
2136 int[3]", the "const" is emitted with the "int", not with the
2137 array. */
2138 cp_cv_quals quals = TYPE_QUALS (type);
2140 if (quals & TYPE_QUAL_RESTRICT)
2142 write_char ('r');
2143 ++num_qualifiers;
2145 if (quals & TYPE_QUAL_VOLATILE)
2147 write_char ('V');
2148 ++num_qualifiers;
2150 if (quals & TYPE_QUAL_CONST)
2152 write_char ('K');
2153 ++num_qualifiers;
2156 return num_qualifiers;
2159 /* Non-terminal <builtin-type>.
2161 <builtin-type> ::= v # void
2162 ::= b # bool
2163 ::= w # wchar_t
2164 ::= c # char
2165 ::= a # signed char
2166 ::= h # unsigned char
2167 ::= s # short
2168 ::= t # unsigned short
2169 ::= i # int
2170 ::= j # unsigned int
2171 ::= l # long
2172 ::= m # unsigned long
2173 ::= x # long long, __int64
2174 ::= y # unsigned long long, __int64
2175 ::= n # __int128
2176 ::= o # unsigned __int128
2177 ::= f # float
2178 ::= d # double
2179 ::= e # long double, __float80
2180 ::= g # __float128 [not supported]
2181 ::= u <source-name> # vendor extended type */
2183 static void
2184 write_builtin_type (tree type)
2186 if (TYPE_CANONICAL (type))
2187 type = TYPE_CANONICAL (type);
2189 switch (TREE_CODE (type))
2191 case VOID_TYPE:
2192 write_char ('v');
2193 break;
2195 case BOOLEAN_TYPE:
2196 write_char ('b');
2197 break;
2199 case INTEGER_TYPE:
2200 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2201 isn't in integer_type_nodes. */
2202 if (type == wchar_type_node)
2203 write_char ('w');
2204 else if (type == char16_type_node)
2205 write_string ("Ds");
2206 else if (type == char32_type_node)
2207 write_string ("Di");
2208 else if (TYPE_FOR_JAVA (type))
2209 write_java_integer_type_codes (type);
2210 else
2212 size_t itk;
2213 /* Assume TYPE is one of the shared integer type nodes. Find
2214 it in the array of these nodes. */
2215 iagain:
2216 for (itk = 0; itk < itk_none; ++itk)
2217 if (integer_types[itk] != NULL_TREE
2218 && integer_type_codes[itk] != '\0'
2219 && type == integer_types[itk])
2221 /* Print the corresponding single-letter code. */
2222 write_char (integer_type_codes[itk]);
2223 break;
2226 if (itk == itk_none)
2228 tree t = c_common_type_for_mode (TYPE_MODE (type),
2229 TYPE_UNSIGNED (type));
2230 if (type != t)
2232 type = t;
2233 goto iagain;
2236 if (TYPE_PRECISION (type) == 128)
2237 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2238 else
2240 /* Allow for cases where TYPE is not one of the shared
2241 integer type nodes and write a "vendor extended builtin
2242 type" with a name the form intN or uintN, respectively.
2243 Situations like this can happen if you have an
2244 __attribute__((__mode__(__SI__))) type and use exotic
2245 switches like '-mint8' on AVR. Of course, this is
2246 undefined by the C++ ABI (and '-mint8' is not even
2247 Standard C conforming), but when using such special
2248 options you're pretty much in nowhere land anyway. */
2249 const char *prefix;
2250 char prec[11]; /* up to ten digits for an unsigned */
2252 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2253 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2254 write_char ('u'); /* "vendor extended builtin type" */
2255 write_unsigned_number (strlen (prefix) + strlen (prec));
2256 write_string (prefix);
2257 write_string (prec);
2261 break;
2263 case REAL_TYPE:
2264 if (type == float_type_node
2265 || type == java_float_type_node)
2266 write_char ('f');
2267 else if (type == double_type_node
2268 || type == java_double_type_node)
2269 write_char ('d');
2270 else if (type == long_double_type_node)
2271 write_char ('e');
2272 else if (type == dfloat32_type_node)
2273 write_string ("Df");
2274 else if (type == dfloat64_type_node)
2275 write_string ("Dd");
2276 else if (type == dfloat128_type_node)
2277 write_string ("De");
2278 else
2279 gcc_unreachable ();
2280 break;
2282 case FIXED_POINT_TYPE:
2283 write_string ("DF");
2284 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2285 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2286 if (type == fract_type_node
2287 || type == sat_fract_type_node
2288 || type == accum_type_node
2289 || type == sat_accum_type_node)
2290 write_char ('i');
2291 else if (type == unsigned_fract_type_node
2292 || type == sat_unsigned_fract_type_node
2293 || type == unsigned_accum_type_node
2294 || type == sat_unsigned_accum_type_node)
2295 write_char ('j');
2296 else if (type == short_fract_type_node
2297 || type == sat_short_fract_type_node
2298 || type == short_accum_type_node
2299 || type == sat_short_accum_type_node)
2300 write_char ('s');
2301 else if (type == unsigned_short_fract_type_node
2302 || type == sat_unsigned_short_fract_type_node
2303 || type == unsigned_short_accum_type_node
2304 || type == sat_unsigned_short_accum_type_node)
2305 write_char ('t');
2306 else if (type == long_fract_type_node
2307 || type == sat_long_fract_type_node
2308 || type == long_accum_type_node
2309 || type == sat_long_accum_type_node)
2310 write_char ('l');
2311 else if (type == unsigned_long_fract_type_node
2312 || type == sat_unsigned_long_fract_type_node
2313 || type == unsigned_long_accum_type_node
2314 || type == sat_unsigned_long_accum_type_node)
2315 write_char ('m');
2316 else if (type == long_long_fract_type_node
2317 || type == sat_long_long_fract_type_node
2318 || type == long_long_accum_type_node
2319 || type == sat_long_long_accum_type_node)
2320 write_char ('x');
2321 else if (type == unsigned_long_long_fract_type_node
2322 || type == sat_unsigned_long_long_fract_type_node
2323 || type == unsigned_long_long_accum_type_node
2324 || type == sat_unsigned_long_long_accum_type_node)
2325 write_char ('y');
2326 else
2327 sorry ("mangling unknown fixed point type");
2328 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2329 if (TYPE_SATURATING (type))
2330 write_char ('s');
2331 else
2332 write_char ('n');
2333 break;
2335 default:
2336 gcc_unreachable ();
2340 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2341 METHOD_TYPE. The return type is mangled before the parameter
2342 types.
2344 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2346 static void
2347 write_function_type (const tree type)
2349 MANGLE_TRACE_TREE ("function-type", type);
2351 /* For a pointer to member function, the function type may have
2352 cv-qualifiers, indicating the quals for the artificial 'this'
2353 parameter. */
2354 if (TREE_CODE (type) == METHOD_TYPE)
2356 /* The first parameter must be a POINTER_TYPE pointing to the
2357 `this' parameter. */
2358 tree this_type = class_of_this_parm (type);
2359 write_CV_qualifiers_for_type (this_type);
2362 write_char ('F');
2363 /* We don't track whether or not a type is `extern "C"'. Note that
2364 you can have an `extern "C"' function that does not have
2365 `extern "C"' type, and vice versa:
2367 extern "C" typedef void function_t();
2368 function_t f; // f has C++ linkage, but its type is
2369 // `extern "C"'
2371 typedef void function_t();
2372 extern "C" function_t f; // Vice versa.
2374 See [dcl.link]. */
2375 write_bare_function_type (type, /*include_return_type_p=*/1,
2376 /*decl=*/NULL);
2377 if (FUNCTION_REF_QUALIFIED (type))
2379 if (FUNCTION_RVALUE_QUALIFIED (type))
2380 write_char ('O');
2381 else
2382 write_char ('R');
2384 write_char ('E');
2387 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2388 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2389 is mangled before the parameter types. If non-NULL, DECL is
2390 FUNCTION_DECL for the function whose type is being emitted.
2392 If DECL is a member of a Java type, then a literal 'J'
2393 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2394 were nonzero.
2396 <bare-function-type> ::= [J]</signature/ type>+ */
2398 static void
2399 write_bare_function_type (const tree type, const int include_return_type_p,
2400 const tree decl)
2402 int java_method_p;
2404 MANGLE_TRACE_TREE ("bare-function-type", type);
2406 /* Detect Java methods and emit special encoding. */
2407 if (decl != NULL
2408 && DECL_FUNCTION_MEMBER_P (decl)
2409 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2410 && !DECL_CONSTRUCTOR_P (decl)
2411 && !DECL_DESTRUCTOR_P (decl)
2412 && !DECL_CONV_FN_P (decl))
2414 java_method_p = 1;
2415 write_char ('J');
2417 else
2419 java_method_p = 0;
2422 /* Mangle the return type, if requested. */
2423 if (include_return_type_p || java_method_p)
2424 write_type (TREE_TYPE (type));
2426 /* Now mangle the types of the arguments. */
2427 ++G.parm_depth;
2428 write_method_parms (TYPE_ARG_TYPES (type),
2429 TREE_CODE (type) == METHOD_TYPE,
2430 decl);
2431 --G.parm_depth;
2434 /* Write the mangled representation of a method parameter list of
2435 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2436 considered a non-static method, and the this parameter is omitted.
2437 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2438 parameters are being emitted. */
2440 static void
2441 write_method_parms (tree parm_types, const int method_p, const tree decl)
2443 tree first_parm_type;
2444 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2446 /* Assume this parameter type list is variable-length. If it ends
2447 with a void type, then it's not. */
2448 int varargs_p = 1;
2450 /* If this is a member function, skip the first arg, which is the
2451 this pointer.
2452 "Member functions do not encode the type of their implicit this
2453 parameter."
2455 Similarly, there's no need to mangle artificial parameters, like
2456 the VTT parameters for constructors and destructors. */
2457 if (method_p)
2459 parm_types = TREE_CHAIN (parm_types);
2460 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2462 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2464 parm_types = TREE_CHAIN (parm_types);
2465 parm_decl = DECL_CHAIN (parm_decl);
2469 for (first_parm_type = parm_types;
2470 parm_types;
2471 parm_types = TREE_CHAIN (parm_types))
2473 tree parm = TREE_VALUE (parm_types);
2474 if (parm == void_type_node)
2476 /* "Empty parameter lists, whether declared as () or
2477 conventionally as (void), are encoded with a void parameter
2478 (v)." */
2479 if (parm_types == first_parm_type)
2480 write_type (parm);
2481 /* If the parm list is terminated with a void type, it's
2482 fixed-length. */
2483 varargs_p = 0;
2484 /* A void type better be the last one. */
2485 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2487 else
2488 write_type (parm);
2491 if (varargs_p)
2492 /* <builtin-type> ::= z # ellipsis */
2493 write_char ('z');
2496 /* <class-enum-type> ::= <name> */
2498 static void
2499 write_class_enum_type (const tree type)
2501 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2504 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2505 arguments.
2507 <template-args> ::= I <template-arg>* E */
2509 static void
2510 write_template_args (tree args)
2512 int i;
2513 int length = 0;
2515 MANGLE_TRACE_TREE ("template-args", args);
2517 write_char ('I');
2519 if (args)
2520 length = TREE_VEC_LENGTH (args);
2522 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2524 /* We have nested template args. We want the innermost template
2525 argument list. */
2526 args = TREE_VEC_ELT (args, length - 1);
2527 length = TREE_VEC_LENGTH (args);
2529 for (i = 0; i < length; ++i)
2530 write_template_arg (TREE_VEC_ELT (args, i));
2532 write_char ('E');
2535 /* Write out the
2536 <unqualified-name>
2537 <unqualified-name> <template-args>
2538 part of SCOPE_REF or COMPONENT_REF mangling. */
2540 static void
2541 write_member_name (tree member)
2543 if (identifier_p (member))
2544 write_unqualified_id (member);
2545 else if (DECL_P (member))
2546 write_unqualified_name (member);
2547 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2549 tree name = TREE_OPERAND (member, 0);
2550 if (TREE_CODE (name) == OVERLOAD)
2551 name = OVL_FUNCTION (name);
2552 write_member_name (name);
2553 write_template_args (TREE_OPERAND (member, 1));
2555 else
2556 write_expression (member);
2559 /* <expression> ::= <unary operator-name> <expression>
2560 ::= <binary operator-name> <expression> <expression>
2561 ::= <expr-primary>
2563 <expr-primary> ::= <template-param>
2564 ::= L <type> <value number> E # literal
2565 ::= L <mangled-name> E # external name
2566 ::= st <type> # sizeof
2567 ::= sr <type> <unqualified-name> # dependent name
2568 ::= sr <type> <unqualified-name> <template-args> */
2570 static void
2571 write_expression (tree expr)
2573 enum tree_code code = TREE_CODE (expr);
2575 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2576 is converted (via qualification conversions) to another
2577 type. */
2578 while (TREE_CODE (expr) == NOP_EXPR
2579 /* Parentheses aren't mangled. */
2580 || code == PAREN_EXPR
2581 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2583 expr = TREE_OPERAND (expr, 0);
2584 code = TREE_CODE (expr);
2587 if (code == BASELINK
2588 && (!type_unknown_p (expr)
2589 || !BASELINK_QUALIFIED_P (expr)))
2591 expr = BASELINK_FUNCTIONS (expr);
2592 code = TREE_CODE (expr);
2595 /* Handle pointers-to-members by making them look like expression
2596 nodes. */
2597 if (code == PTRMEM_CST)
2599 expr = build_nt (ADDR_EXPR,
2600 build_qualified_name (/*type=*/NULL_TREE,
2601 PTRMEM_CST_CLASS (expr),
2602 PTRMEM_CST_MEMBER (expr),
2603 /*template_p=*/false));
2604 code = TREE_CODE (expr);
2607 /* Handle template parameters. */
2608 if (code == TEMPLATE_TYPE_PARM
2609 || code == TEMPLATE_TEMPLATE_PARM
2610 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2611 || code == TEMPLATE_PARM_INDEX)
2612 write_template_param (expr);
2613 /* Handle literals. */
2614 else if (TREE_CODE_CLASS (code) == tcc_constant
2615 || code == CONST_DECL)
2616 write_template_arg_literal (expr);
2617 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2619 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2620 write_string ("fpT");
2622 else if (code == PARM_DECL)
2624 /* A function parameter used in a late-specified return type. */
2625 int index = DECL_PARM_INDEX (expr);
2626 int level = DECL_PARM_LEVEL (expr);
2627 int delta = G.parm_depth - level + 1;
2628 gcc_assert (index >= 1);
2629 write_char ('f');
2630 if (delta != 0)
2632 if (abi_version_at_least (5))
2634 /* Let L be the number of function prototype scopes from the
2635 innermost one (in which the parameter reference occurs) up
2636 to (and including) the one containing the declaration of
2637 the referenced parameter. If the parameter declaration
2638 clause of the innermost function prototype scope has been
2639 completely seen, it is not counted (in that case -- which
2640 is perhaps the most common -- L can be zero). */
2641 write_char ('L');
2642 write_unsigned_number (delta - 1);
2644 if (abi_version_crosses (5))
2645 G.need_abi_warning = true;
2647 write_char ('p');
2648 write_compact_number (index - 1);
2650 else if (DECL_P (expr))
2652 write_char ('L');
2653 write_mangled_name (expr, false);
2654 write_char ('E');
2656 else if (TREE_CODE (expr) == SIZEOF_EXPR
2657 && SIZEOF_EXPR_TYPE_P (expr))
2659 write_string ("st");
2660 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2662 else if (TREE_CODE (expr) == SIZEOF_EXPR
2663 && TYPE_P (TREE_OPERAND (expr, 0)))
2665 write_string ("st");
2666 write_type (TREE_OPERAND (expr, 0));
2668 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2669 && TYPE_P (TREE_OPERAND (expr, 0)))
2671 write_string ("at");
2672 write_type (TREE_OPERAND (expr, 0));
2674 else if (code == SCOPE_REF
2675 || code == BASELINK)
2677 tree scope, member;
2678 if (code == SCOPE_REF)
2680 scope = TREE_OPERAND (expr, 0);
2681 member = TREE_OPERAND (expr, 1);
2683 else
2685 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2686 member = BASELINK_FUNCTIONS (expr);
2689 /* If the MEMBER is a real declaration, then the qualifying
2690 scope was not dependent. Ideally, we would not have a
2691 SCOPE_REF in those cases, but sometimes we do. If the second
2692 argument is a DECL, then the name must not have been
2693 dependent. */
2694 if (DECL_P (member))
2695 write_expression (member);
2696 else
2698 write_string ("sr");
2699 write_type (scope);
2700 write_member_name (member);
2703 else if (INDIRECT_REF_P (expr)
2704 && TREE_TYPE (TREE_OPERAND (expr, 0))
2705 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2707 write_expression (TREE_OPERAND (expr, 0));
2709 else if (identifier_p (expr))
2711 /* An operator name appearing as a dependent name needs to be
2712 specially marked to disambiguate between a use of the operator
2713 name and a use of the operator in an expression. */
2714 if (IDENTIFIER_OPNAME_P (expr))
2715 write_string ("on");
2716 write_unqualified_id (expr);
2718 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2720 tree fn = TREE_OPERAND (expr, 0);
2721 if (is_overloaded_fn (fn))
2722 fn = DECL_NAME (get_first_fn (fn));
2723 if (IDENTIFIER_OPNAME_P (fn))
2724 write_string ("on");
2725 write_unqualified_id (fn);
2726 write_template_args (TREE_OPERAND (expr, 1));
2728 else if (TREE_CODE (expr) == MODOP_EXPR)
2730 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2731 const char *name = (assignment_operator_name_info[(int) subop]
2732 .mangled_name);
2733 write_string (name);
2734 write_expression (TREE_OPERAND (expr, 0));
2735 write_expression (TREE_OPERAND (expr, 2));
2737 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2739 /* ::= [gs] nw <expression>* _ <type> E
2740 ::= [gs] nw <expression>* _ <type> <initializer>
2741 ::= [gs] na <expression>* _ <type> E
2742 ::= [gs] na <expression>* _ <type> <initializer>
2743 <initializer> ::= pi <expression>* E */
2744 tree placement = TREE_OPERAND (expr, 0);
2745 tree type = TREE_OPERAND (expr, 1);
2746 tree nelts = TREE_OPERAND (expr, 2);
2747 tree init = TREE_OPERAND (expr, 3);
2748 tree t;
2750 gcc_assert (code == NEW_EXPR);
2751 if (TREE_OPERAND (expr, 2))
2752 code = VEC_NEW_EXPR;
2754 if (NEW_EXPR_USE_GLOBAL (expr))
2755 write_string ("gs");
2757 write_string (operator_name_info[(int) code].mangled_name);
2759 for (t = placement; t; t = TREE_CHAIN (t))
2760 write_expression (TREE_VALUE (t));
2762 write_char ('_');
2764 if (nelts)
2766 tree domain;
2767 ++processing_template_decl;
2768 domain = compute_array_index_type (NULL_TREE, nelts,
2769 tf_warning_or_error);
2770 type = build_cplus_array_type (type, domain);
2771 --processing_template_decl;
2773 write_type (type);
2775 if (init && TREE_CODE (init) == TREE_LIST
2776 && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
2777 write_expression (TREE_VALUE (init));
2778 else
2780 if (init)
2781 write_string ("pi");
2782 if (init && init != void_node)
2783 for (t = init; t; t = TREE_CHAIN (t))
2784 write_expression (TREE_VALUE (t));
2785 write_char ('E');
2788 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2790 gcc_assert (code == DELETE_EXPR);
2791 if (DELETE_EXPR_USE_VEC (expr))
2792 code = VEC_DELETE_EXPR;
2794 if (DELETE_EXPR_USE_GLOBAL (expr))
2795 write_string ("gs");
2797 write_string (operator_name_info[(int) code].mangled_name);
2799 write_expression (TREE_OPERAND (expr, 0));
2801 else if (code == THROW_EXPR)
2803 tree op = TREE_OPERAND (expr, 0);
2804 if (op)
2806 write_string ("tw");
2807 write_expression (op);
2809 else
2810 write_string ("tr");
2812 else if (code == CONSTRUCTOR)
2814 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2815 unsigned i; tree val;
2817 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2818 write_string ("il");
2819 else
2821 write_string ("tl");
2822 write_type (TREE_TYPE (expr));
2824 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2825 write_expression (val);
2826 write_char ('E');
2828 else if (dependent_name (expr))
2830 write_unqualified_id (dependent_name (expr));
2832 else
2834 int i, len;
2835 const char *name;
2837 /* When we bind a variable or function to a non-type template
2838 argument with reference type, we create an ADDR_EXPR to show
2839 the fact that the entity's address has been taken. But, we
2840 don't actually want to output a mangling code for the `&'. */
2841 if (TREE_CODE (expr) == ADDR_EXPR
2842 && TREE_TYPE (expr)
2843 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2845 expr = TREE_OPERAND (expr, 0);
2846 if (DECL_P (expr))
2848 write_expression (expr);
2849 return;
2852 code = TREE_CODE (expr);
2855 if (code == COMPONENT_REF)
2857 tree ob = TREE_OPERAND (expr, 0);
2859 if (TREE_CODE (ob) == ARROW_EXPR)
2861 write_string (operator_name_info[(int)code].mangled_name);
2862 ob = TREE_OPERAND (ob, 0);
2863 write_expression (ob);
2865 else if (!is_dummy_object (ob))
2867 write_string ("dt");
2868 write_expression (ob);
2870 /* else, for a non-static data member with no associated object (in
2871 unevaluated context), use the unresolved-name mangling. */
2873 write_member_name (TREE_OPERAND (expr, 1));
2874 return;
2877 /* If it wasn't any of those, recursively expand the expression. */
2878 name = operator_name_info[(int) code].mangled_name;
2880 /* We used to mangle const_cast and static_cast like a C cast. */
2881 if (code == CONST_CAST_EXPR
2882 || code == STATIC_CAST_EXPR)
2884 if (abi_version_crosses (6))
2885 G.need_abi_warning = 1;
2886 if (!abi_version_at_least (6))
2887 name = operator_name_info[CAST_EXPR].mangled_name;
2890 if (name == NULL)
2892 switch (code)
2894 case TRAIT_EXPR:
2895 error ("use of built-in trait %qE in function signature; "
2896 "use library traits instead", expr);
2897 break;
2899 default:
2900 sorry ("mangling %C", code);
2901 break;
2903 return;
2905 else
2906 write_string (name);
2908 switch (code)
2910 case CALL_EXPR:
2912 tree fn = CALL_EXPR_FN (expr);
2914 if (TREE_CODE (fn) == ADDR_EXPR)
2915 fn = TREE_OPERAND (fn, 0);
2917 /* Mangle a dependent name as the name, not whatever happens to
2918 be the first function in the overload set. */
2919 if ((TREE_CODE (fn) == FUNCTION_DECL
2920 || TREE_CODE (fn) == OVERLOAD)
2921 && type_dependent_expression_p_push (expr))
2922 fn = DECL_NAME (get_first_fn (fn));
2924 write_expression (fn);
2927 for (i = 0; i < call_expr_nargs (expr); ++i)
2928 write_expression (CALL_EXPR_ARG (expr, i));
2929 write_char ('E');
2930 break;
2932 case CAST_EXPR:
2933 write_type (TREE_TYPE (expr));
2934 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2935 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2936 else
2938 tree args = TREE_OPERAND (expr, 0);
2939 write_char ('_');
2940 for (; args; args = TREE_CHAIN (args))
2941 write_expression (TREE_VALUE (args));
2942 write_char ('E');
2944 break;
2946 case DYNAMIC_CAST_EXPR:
2947 case REINTERPRET_CAST_EXPR:
2948 case STATIC_CAST_EXPR:
2949 case CONST_CAST_EXPR:
2950 write_type (TREE_TYPE (expr));
2951 write_expression (TREE_OPERAND (expr, 0));
2952 break;
2954 case PREINCREMENT_EXPR:
2955 case PREDECREMENT_EXPR:
2956 if (abi_version_at_least (6))
2957 write_char ('_');
2958 if (abi_version_crosses (6))
2959 G.need_abi_warning = 1;
2960 /* Fall through. */
2962 default:
2963 /* In the middle-end, some expressions have more operands than
2964 they do in templates (and mangling). */
2965 len = cp_tree_operand_length (expr);
2967 for (i = 0; i < len; ++i)
2969 tree operand = TREE_OPERAND (expr, i);
2970 /* As a GNU extension, the middle operand of a
2971 conditional may be omitted. Since expression
2972 manglings are supposed to represent the input token
2973 stream, there's no good way to mangle such an
2974 expression without extending the C++ ABI. */
2975 if (code == COND_EXPR && i == 1 && !operand)
2977 error ("omitted middle operand to %<?:%> operand "
2978 "cannot be mangled");
2979 continue;
2981 write_expression (operand);
2987 /* Literal subcase of non-terminal <template-arg>.
2989 "Literal arguments, e.g. "A<42L>", are encoded with their type
2990 and value. Negative integer values are preceded with "n"; for
2991 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2992 encoded as 0, true as 1." */
2994 static void
2995 write_template_arg_literal (const tree value)
2997 write_char ('L');
2998 write_type (TREE_TYPE (value));
3000 /* Write a null member pointer value as (type)0, regardless of its
3001 real representation. */
3002 if (null_member_pointer_value_p (value))
3003 write_integer_cst (integer_zero_node);
3004 else
3005 switch (TREE_CODE (value))
3007 case CONST_DECL:
3008 write_integer_cst (DECL_INITIAL (value));
3009 break;
3011 case INTEGER_CST:
3012 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3013 || integer_zerop (value) || integer_onep (value));
3014 write_integer_cst (value);
3015 break;
3017 case REAL_CST:
3018 write_real_cst (value);
3019 break;
3021 case COMPLEX_CST:
3022 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3023 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3025 write_integer_cst (TREE_REALPART (value));
3026 write_char ('_');
3027 write_integer_cst (TREE_IMAGPART (value));
3029 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3030 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3032 write_real_cst (TREE_REALPART (value));
3033 write_char ('_');
3034 write_real_cst (TREE_IMAGPART (value));
3036 else
3037 gcc_unreachable ();
3038 break;
3040 case STRING_CST:
3041 sorry ("string literal in function template signature");
3042 break;
3044 default:
3045 gcc_unreachable ();
3048 write_char ('E');
3051 /* Non-terminal <template-arg>.
3053 <template-arg> ::= <type> # type
3054 ::= L <type> </value/ number> E # literal
3055 ::= LZ <name> E # external name
3056 ::= X <expression> E # expression */
3058 static void
3059 write_template_arg (tree node)
3061 enum tree_code code = TREE_CODE (node);
3063 MANGLE_TRACE_TREE ("template-arg", node);
3065 /* A template template parameter's argument list contains TREE_LIST
3066 nodes of which the value field is the actual argument. */
3067 if (code == TREE_LIST)
3069 node = TREE_VALUE (node);
3070 /* If it's a decl, deal with its type instead. */
3071 if (DECL_P (node))
3073 node = TREE_TYPE (node);
3074 code = TREE_CODE (node);
3078 if (TREE_CODE (node) == NOP_EXPR
3079 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3081 /* Template parameters can be of reference type. To maintain
3082 internal consistency, such arguments use a conversion from
3083 address of object to reference type. */
3084 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3085 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3088 if (TREE_CODE (node) == BASELINK
3089 && !type_unknown_p (node))
3091 if (abi_version_at_least (6))
3092 node = BASELINK_FUNCTIONS (node);
3093 if (abi_version_crosses (6))
3094 /* We wrongly wrapped a class-scope function in X/E. */
3095 G.need_abi_warning = 1;
3098 if (ARGUMENT_PACK_P (node))
3100 /* Expand the template argument pack. */
3101 tree args = ARGUMENT_PACK_ARGS (node);
3102 int i, length = TREE_VEC_LENGTH (args);
3103 if (abi_version_at_least (6))
3104 write_char ('J');
3105 else
3106 write_char ('I');
3107 if (abi_version_crosses (6))
3108 G.need_abi_warning = 1;
3109 for (i = 0; i < length; ++i)
3110 write_template_arg (TREE_VEC_ELT (args, i));
3111 write_char ('E');
3113 else if (TYPE_P (node))
3114 write_type (node);
3115 else if (code == TEMPLATE_DECL)
3116 /* A template appearing as a template arg is a template template arg. */
3117 write_template_template_arg (node);
3118 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3119 || code == CONST_DECL
3120 || null_member_pointer_value_p (node))
3121 write_template_arg_literal (node);
3122 else if (DECL_P (node))
3124 write_char ('L');
3125 /* Until ABI version 3, the underscore before the mangled name
3126 was incorrectly omitted. */
3127 if (!abi_version_at_least (3))
3128 write_char ('Z');
3129 else
3130 write_string ("_Z");
3131 if (abi_version_crosses (3))
3132 G.need_abi_warning = 1;
3133 write_encoding (node);
3134 write_char ('E');
3136 else
3138 /* Template arguments may be expressions. */
3139 write_char ('X');
3140 write_expression (node);
3141 write_char ('E');
3145 /* <template-template-arg>
3146 ::= <name>
3147 ::= <substitution> */
3149 static void
3150 write_template_template_arg (const tree decl)
3152 MANGLE_TRACE_TREE ("template-template-arg", decl);
3154 if (find_substitution (decl))
3155 return;
3156 write_name (decl, /*ignore_local_scope=*/0);
3157 add_substitution (decl);
3161 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3163 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3164 ::= A <expression> _ </element/ type>
3166 "Array types encode the dimension (number of elements) and the
3167 element type. For variable length arrays, the dimension (but not
3168 the '_' separator) is omitted." */
3170 static void
3171 write_array_type (const tree type)
3173 write_char ('A');
3174 if (TYPE_DOMAIN (type))
3176 tree index_type;
3177 tree max;
3179 index_type = TYPE_DOMAIN (type);
3180 /* The INDEX_TYPE gives the upper and lower bounds of the
3181 array. */
3182 max = TYPE_MAX_VALUE (index_type);
3183 if (TREE_CODE (max) == INTEGER_CST)
3185 /* The ABI specifies that we should mangle the number of
3186 elements in the array, not the largest allowed index. */
3187 offset_int wmax = wi::to_offset (max) + 1;
3188 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3189 number of elements as zero. */
3190 wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max)));
3191 gcc_assert (wi::fits_uhwi_p (wmax));
3192 write_unsigned_number (wmax.to_uhwi ());
3194 else
3196 max = TREE_OPERAND (max, 0);
3197 write_expression (max);
3201 write_char ('_');
3202 write_type (TREE_TYPE (type));
3205 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3206 variables. TYPE is a pointer-to-member POINTER_TYPE.
3208 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3210 static void
3211 write_pointer_to_member_type (const tree type)
3213 write_char ('M');
3214 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3215 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3218 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3219 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3220 TEMPLATE_PARM_INDEX.
3222 <template-param> ::= T </parameter/ number> _ */
3224 static void
3225 write_template_param (const tree parm)
3227 int parm_index;
3229 MANGLE_TRACE_TREE ("template-parm", parm);
3231 switch (TREE_CODE (parm))
3233 case TEMPLATE_TYPE_PARM:
3234 case TEMPLATE_TEMPLATE_PARM:
3235 case BOUND_TEMPLATE_TEMPLATE_PARM:
3236 parm_index = TEMPLATE_TYPE_IDX (parm);
3237 break;
3239 case TEMPLATE_PARM_INDEX:
3240 parm_index = TEMPLATE_PARM_IDX (parm);
3241 break;
3243 default:
3244 gcc_unreachable ();
3247 write_char ('T');
3248 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3249 earliest template param denoted by `_'. */
3250 write_compact_number (parm_index);
3253 /* <template-template-param>
3254 ::= <template-param>
3255 ::= <substitution> */
3257 static void
3258 write_template_template_param (const tree parm)
3260 tree templ = NULL_TREE;
3262 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3263 template template parameter. The substitution candidate here is
3264 only the template. */
3265 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3267 templ
3268 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3269 if (find_substitution (templ))
3270 return;
3273 /* <template-param> encodes only the template parameter position,
3274 not its template arguments, which is fine here. */
3275 write_template_param (parm);
3276 if (templ)
3277 add_substitution (templ);
3280 /* Non-terminal <substitution>.
3282 <substitution> ::= S <seq-id> _
3283 ::= S_ */
3285 static void
3286 write_substitution (const int seq_id)
3288 MANGLE_TRACE ("substitution", "");
3290 write_char ('S');
3291 if (seq_id > 0)
3292 write_number (seq_id - 1, /*unsigned=*/1, 36);
3293 write_char ('_');
3296 /* Start mangling ENTITY. */
3298 static inline void
3299 start_mangling (const tree entity)
3301 G.entity = entity;
3302 G.need_abi_warning = false;
3303 obstack_free (&name_obstack, name_base);
3304 mangle_obstack = &name_obstack;
3305 name_base = obstack_alloc (&name_obstack, 0);
3308 /* Done with mangling. If WARN is true, and the name of G.entity will
3309 be mangled differently in a future version of the ABI, issue a
3310 warning. */
3312 static void
3313 finish_mangling_internal (void)
3315 /* Clear all the substitutions. */
3316 vec_safe_truncate (G.substitutions, 0);
3318 /* Null-terminate the string. */
3319 write_char ('\0');
3323 /* Like finish_mangling_internal, but return the mangled string. */
3325 static inline const char *
3326 finish_mangling (void)
3328 finish_mangling_internal ();
3329 return (const char *) obstack_finish (mangle_obstack);
3332 /* Like finish_mangling_internal, but return an identifier. */
3334 static tree
3335 finish_mangling_get_identifier (void)
3337 finish_mangling_internal ();
3338 /* Don't obstack_finish here, and the next start_mangling will
3339 remove the identifier. */
3340 return get_identifier ((const char *) obstack_base (mangle_obstack));
3343 /* Initialize data structures for mangling. */
3345 void
3346 init_mangle (void)
3348 gcc_obstack_init (&name_obstack);
3349 name_base = obstack_alloc (&name_obstack, 0);
3350 vec_alloc (G.substitutions, 0);
3352 /* Cache these identifiers for quick comparison when checking for
3353 standard substitutions. */
3354 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3355 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3356 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3357 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3358 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3359 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3362 /* Generate the mangled name of DECL. */
3364 static tree
3365 mangle_decl_string (const tree decl)
3367 tree result;
3368 location_t saved_loc = input_location;
3369 tree saved_fn = NULL_TREE;
3370 bool template_p = false;
3372 /* We shouldn't be trying to mangle an uninstantiated template. */
3373 gcc_assert (!type_dependent_expression_p (decl));
3375 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3377 struct tinst_level *tl = current_instantiation ();
3378 if ((!tl || tl->decl != decl)
3379 && push_tinst_level (decl))
3381 template_p = true;
3382 saved_fn = current_function_decl;
3383 current_function_decl = NULL_TREE;
3386 input_location = DECL_SOURCE_LOCATION (decl);
3388 start_mangling (decl);
3390 if (TREE_CODE (decl) == TYPE_DECL)
3391 write_type (TREE_TYPE (decl));
3392 else
3393 write_mangled_name (decl, true);
3395 result = finish_mangling_get_identifier ();
3396 if (DEBUG_MANGLE)
3397 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3398 IDENTIFIER_POINTER (result));
3400 if (template_p)
3402 pop_tinst_level ();
3403 current_function_decl = saved_fn;
3405 input_location = saved_loc;
3407 return result;
3410 /* Return an identifier for the external mangled name of DECL. */
3412 static tree
3413 get_mangled_id (tree decl)
3415 tree id = mangle_decl_string (decl);
3416 return targetm.mangle_decl_assembler_name (decl, id);
3419 /* Create an identifier for the external mangled name of DECL. */
3421 void
3422 mangle_decl (const tree decl)
3424 tree id;
3425 bool dep;
3427 /* Don't bother mangling uninstantiated templates. */
3428 ++processing_template_decl;
3429 if (TREE_CODE (decl) == TYPE_DECL)
3430 dep = dependent_type_p (TREE_TYPE (decl));
3431 else
3432 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3433 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3434 --processing_template_decl;
3435 if (dep)
3436 return;
3438 id = get_mangled_id (decl);
3439 SET_DECL_ASSEMBLER_NAME (decl, id);
3441 if (G.need_abi_warning
3442 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3443 && TREE_CODE (decl) != TYPE_DECL
3444 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3445 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3447 /* If the mangling will change in the future, emit an alias with the
3448 future mangled name for forward-compatibility. */
3449 int save_ver;
3450 tree id2;
3452 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3453 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3454 inform (DECL_SOURCE_LOCATION (decl), "a later -fabi-version= (or =0) "
3455 "avoids this error with a change in mangling");
3457 save_ver = flag_abi_version;
3458 flag_abi_version = flag_abi_compat_version;
3459 id2 = mangle_decl_string (decl);
3460 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3461 flag_abi_version = save_ver;
3463 if (id2 == id)
3464 return;
3466 if (warn_abi)
3468 if (flag_abi_compat_version != 0
3469 && abi_version_at_least (flag_abi_compat_version))
3470 warning (OPT_Wabi, "the mangled name of %q+D changed between "
3471 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3472 G.entity, flag_abi_compat_version, id2,
3473 flag_abi_version, id);
3474 else
3475 warning (OPT_Wabi, "the mangled name of %q+D changes between "
3476 "-fabi-version=%d (%D) and -fabi-version=%d (%D)",
3477 G.entity, flag_abi_version, id,
3478 flag_abi_compat_version, id2);
3481 #ifdef ASM_OUTPUT_DEF
3482 if (flag_abi_compat_version != 0
3483 && IDENTIFIER_GLOBAL_VALUE (id2))
3484 return;
3486 tree alias = make_alias_for (decl, id2);
3487 SET_IDENTIFIER_GLOBAL_VALUE (id2, alias);
3488 DECL_IGNORED_P (alias) = 1;
3489 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3490 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3491 if (vague_linkage_p (decl))
3492 DECL_WEAK (alias) = 1;
3493 if (TREE_CODE (decl) == FUNCTION_DECL)
3495 /* Don't create an alias to an unreferenced function. */
3496 if (struct cgraph_node *n = cgraph_node::get (decl))
3497 n->create_same_body_alias (alias, decl);
3499 else
3500 varpool_node::create_extra_name_alias (alias, decl);
3501 #endif
3505 /* Generate the mangled representation of TYPE. */
3507 const char *
3508 mangle_type_string (const tree type)
3510 const char *result;
3512 start_mangling (type);
3513 write_type (type);
3514 result = finish_mangling ();
3515 if (DEBUG_MANGLE)
3516 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3517 return result;
3520 /* Create an identifier for the mangled name of a special component
3521 for belonging to TYPE. CODE is the ABI-specified code for this
3522 component. */
3524 static tree
3525 mangle_special_for_type (const tree type, const char *code)
3527 tree result;
3529 /* We don't have an actual decl here for the special component, so
3530 we can't just process the <encoded-name>. Instead, fake it. */
3531 start_mangling (type);
3533 /* Start the mangling. */
3534 write_string ("_Z");
3535 write_string (code);
3537 /* Add the type. */
3538 write_type (type);
3539 result = finish_mangling_get_identifier ();
3541 if (DEBUG_MANGLE)
3542 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3543 IDENTIFIER_POINTER (result));
3545 return result;
3548 /* Create an identifier for the mangled representation of the typeinfo
3549 structure for TYPE. */
3551 tree
3552 mangle_typeinfo_for_type (const tree type)
3554 return mangle_special_for_type (type, "TI");
3557 /* Create an identifier for the mangled name of the NTBS containing
3558 the mangled name of TYPE. */
3560 tree
3561 mangle_typeinfo_string_for_type (const tree type)
3563 return mangle_special_for_type (type, "TS");
3566 /* Create an identifier for the mangled name of the vtable for TYPE. */
3568 tree
3569 mangle_vtbl_for_type (const tree type)
3571 return mangle_special_for_type (type, "TV");
3574 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3576 tree
3577 mangle_vtt_for_type (const tree type)
3579 return mangle_special_for_type (type, "TT");
3582 /* Return an identifier for a construction vtable group. TYPE is
3583 the most derived class in the hierarchy; BINFO is the base
3584 subobject for which this construction vtable group will be used.
3586 This mangling isn't part of the ABI specification; in the ABI
3587 specification, the vtable group is dumped in the same COMDAT as the
3588 main vtable, and is referenced only from that vtable, so it doesn't
3589 need an external name. For binary formats without COMDAT sections,
3590 though, we need external names for the vtable groups.
3592 We use the production
3594 <special-name> ::= CT <type> <offset number> _ <base type> */
3596 tree
3597 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3599 tree result;
3601 start_mangling (type);
3603 write_string ("_Z");
3604 write_string ("TC");
3605 write_type (type);
3606 write_integer_cst (BINFO_OFFSET (binfo));
3607 write_char ('_');
3608 write_type (BINFO_TYPE (binfo));
3610 result = finish_mangling_get_identifier ();
3611 if (DEBUG_MANGLE)
3612 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3613 IDENTIFIER_POINTER (result));
3614 return result;
3617 /* Mangle a this pointer or result pointer adjustment.
3619 <call-offset> ::= h <fixed offset number> _
3620 ::= v <fixed offset number> _ <virtual offset number> _ */
3622 static void
3623 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3625 write_char (virtual_offset ? 'v' : 'h');
3627 /* For either flavor, write the fixed offset. */
3628 write_integer_cst (fixed_offset);
3629 write_char ('_');
3631 /* For a virtual thunk, add the virtual offset. */
3632 if (virtual_offset)
3634 write_integer_cst (virtual_offset);
3635 write_char ('_');
3639 /* Return an identifier for the mangled name of a this-adjusting or
3640 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3641 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3642 is a virtual thunk, and it is the vtbl offset in
3643 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3644 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3645 thunk itself. A covariant thunk name always includes the adjustment
3646 for the this pointer, even if there is none.
3648 <special-name> ::= T <call-offset> <base encoding>
3649 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3650 <base encoding> */
3652 tree
3653 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3654 tree virtual_offset)
3656 tree result;
3658 start_mangling (fn_decl);
3660 write_string ("_Z");
3661 write_char ('T');
3663 if (!this_adjusting)
3665 /* Covariant thunk with no this adjustment */
3666 write_char ('c');
3667 mangle_call_offset (integer_zero_node, NULL_TREE);
3668 mangle_call_offset (fixed_offset, virtual_offset);
3670 else if (!DECL_THUNK_P (fn_decl))
3671 /* Plain this adjusting thunk. */
3672 mangle_call_offset (fixed_offset, virtual_offset);
3673 else
3675 /* This adjusting thunk to covariant thunk. */
3676 write_char ('c');
3677 mangle_call_offset (fixed_offset, virtual_offset);
3678 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3679 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3680 if (virtual_offset)
3681 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3682 mangle_call_offset (fixed_offset, virtual_offset);
3683 fn_decl = THUNK_TARGET (fn_decl);
3686 /* Scoped name. */
3687 write_encoding (fn_decl);
3689 result = finish_mangling_get_identifier ();
3690 if (DEBUG_MANGLE)
3691 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3692 return result;
3695 struct conv_type_hasher : ggc_hasher<tree>
3697 static hashval_t hash (tree);
3698 static bool equal (tree, tree);
3701 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3702 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3703 TYPE. */
3705 static GTY (()) hash_table<conv_type_hasher> *conv_type_names;
3707 /* Hash a node (VAL1) in the table. */
3709 hashval_t
3710 conv_type_hasher::hash (tree val)
3712 return (hashval_t) TYPE_UID (TREE_TYPE (val));
3715 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3717 bool
3718 conv_type_hasher::equal (tree val1, tree val2)
3720 return TREE_TYPE (val1) == val2;
3723 /* Return an identifier for the mangled unqualified name for a
3724 conversion operator to TYPE. This mangling is not specified by the
3725 ABI spec; it is only used internally. */
3727 tree
3728 mangle_conv_op_name_for_type (const tree type)
3730 tree *slot;
3731 tree identifier;
3733 if (type == error_mark_node)
3734 return error_mark_node;
3736 if (conv_type_names == NULL)
3737 conv_type_names = hash_table<conv_type_hasher>::create_ggc (31);
3739 slot = conv_type_names->find_slot_with_hash (type,
3740 (hashval_t) TYPE_UID (type),
3741 INSERT);
3742 identifier = *slot;
3743 if (!identifier)
3745 char buffer[64];
3747 /* Create a unique name corresponding to TYPE. */
3748 sprintf (buffer, "operator %lu",
3749 (unsigned long) conv_type_names->elements ());
3750 identifier = get_identifier (buffer);
3751 *slot = identifier;
3753 /* Hang TYPE off the identifier so it can be found easily later
3754 when performing conversions. */
3755 TREE_TYPE (identifier) = type;
3757 /* Set bits on the identifier so we know later it's a conversion. */
3758 IDENTIFIER_OPNAME_P (identifier) = 1;
3759 IDENTIFIER_TYPENAME_P (identifier) = 1;
3762 return identifier;
3765 /* Write out the appropriate string for this variable when generating
3766 another mangled name based on this one. */
3768 static void
3769 write_guarded_var_name (const tree variable)
3771 if (DECL_NAME (variable)
3772 && strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3773 /* The name of a guard variable for a reference temporary should refer
3774 to the reference, not the temporary. */
3775 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3776 else
3777 write_name (variable, /*ignore_local_scope=*/0);
3780 /* Return an identifier for the name of an initialization guard
3781 variable for indicated VARIABLE. */
3783 tree
3784 mangle_guard_variable (const tree variable)
3786 start_mangling (variable);
3787 write_string ("_ZGV");
3788 write_guarded_var_name (variable);
3789 return finish_mangling_get_identifier ();
3792 /* Return an identifier for the name of a thread_local initialization
3793 function for VARIABLE. */
3795 tree
3796 mangle_tls_init_fn (const tree variable)
3798 start_mangling (variable);
3799 write_string ("_ZTH");
3800 write_guarded_var_name (variable);
3801 return finish_mangling_get_identifier ();
3804 /* Return an identifier for the name of a thread_local wrapper
3805 function for VARIABLE. */
3807 #define TLS_WRAPPER_PREFIX "_ZTW"
3809 tree
3810 mangle_tls_wrapper_fn (const tree variable)
3812 start_mangling (variable);
3813 write_string (TLS_WRAPPER_PREFIX);
3814 write_guarded_var_name (variable);
3815 return finish_mangling_get_identifier ();
3818 /* Return true iff FN is a thread_local wrapper function. */
3820 bool
3821 decl_tls_wrapper_p (const tree fn)
3823 if (TREE_CODE (fn) != FUNCTION_DECL)
3824 return false;
3825 tree name = DECL_NAME (fn);
3826 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3827 strlen (TLS_WRAPPER_PREFIX)) == 0;
3830 /* Return an identifier for the name of a temporary variable used to
3831 initialize a static reference. This isn't part of the ABI, but we might
3832 as well call them something readable. */
3834 static GTY(()) int temp_count;
3836 tree
3837 mangle_ref_init_variable (const tree variable)
3839 start_mangling (variable);
3840 write_string ("_ZGR");
3841 write_name (variable, /*ignore_local_scope=*/0);
3842 /* Avoid name clashes with aggregate initialization of multiple
3843 references at once. */
3844 write_unsigned_number (temp_count++);
3845 return finish_mangling_get_identifier ();
3849 /* Foreign language type mangling section. */
3851 /* How to write the type codes for the integer Java type. */
3853 static void
3854 write_java_integer_type_codes (const tree type)
3856 if (type == java_int_type_node)
3857 write_char ('i');
3858 else if (type == java_short_type_node)
3859 write_char ('s');
3860 else if (type == java_byte_type_node)
3861 write_char ('c');
3862 else if (type == java_char_type_node)
3863 write_char ('w');
3864 else if (type == java_long_type_node)
3865 write_char ('x');
3866 else if (type == java_boolean_type_node)
3867 write_char ('b');
3868 else
3869 gcc_unreachable ();
3872 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3873 function generates a mangled name for the vtable map variable of
3874 the class type. For example, if the class type is
3875 "std::bad_exception", the mangled name for the class is
3876 "St13bad_exception". This function would generate the name
3877 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3878 "_VTV<std::bad_exception>::__vtable_map". */
3881 char *
3882 get_mangled_vtable_map_var_name (tree class_type)
3884 char *var_name = NULL;
3885 const char *prefix = "_ZN4_VTVI";
3886 const char *postfix = "E12__vtable_mapE";
3888 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3890 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3891 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3892 strlen (prefix) +
3893 strlen (postfix) + 1;
3895 var_name = (char *) xmalloc (len);
3897 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3899 return var_name;
3902 #include "gt-cp-mangle.h"