2009-04-21 Taras Glek <tglek@mozilla.com>
[official-gcc.git] / gcc / cp / mangle.c
blobff77981c264d7e6e93860e679853b0cef3674e4d
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Written by Alex Samuel <samuel@codesourcery.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file implements mangling of C++ names according to the IA64
23 C++ ABI specification. A mangled name encodes a function or
24 variable's name, scope, type, and/or template arguments into a text
25 identifier. This identifier is used as the function's or
26 variable's linkage name, to preserve compatibility between C++'s
27 language features (templates, scoping, and overloading) and C
28 linkers.
30 Additionally, g++ uses mangled names internally. To support this,
31 mangling of types is allowed, even though the mangled name of a
32 type should not appear by itself as an exported name. Ditto for
33 uninstantiated templates.
35 The primary entry point for this module is mangle_decl, which
36 returns an identifier containing the mangled name for a decl.
37 Additional entry points are provided to build mangled names of
38 particular constructs when the appropriate decl for that construct
39 is not available. These are:
41 mangle_typeinfo_for_type: typeinfo data
42 mangle_typeinfo_string_for_type: typeinfo type name
43 mangle_vtbl_for_type: virtual table data
44 mangle_vtt_for_type: VTT data
45 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
46 mangle_thunk: thunk function or entry */
48 #include "config.h"
49 #include "system.h"
50 #include "coretypes.h"
51 #include "tm.h"
52 #include "tree.h"
53 #include "tm_p.h"
54 #include "cp-tree.h"
55 #include "real.h"
56 #include "obstack.h"
57 #include "toplev.h"
58 #include "varray.h"
59 #include "flags.h"
60 #include "target.h"
62 /* Debugging support. */
64 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
65 #ifndef DEBUG_MANGLE
66 #define DEBUG_MANGLE 0
67 #endif
69 /* Macros for tracing the write_* functions. */
70 #if DEBUG_MANGLE
71 # define MANGLE_TRACE(FN, INPUT) \
72 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
73 # define MANGLE_TRACE_TREE(FN, NODE) \
74 fprintf (stderr, " %-24s: %-24s (%p)\n", \
75 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
76 #else
77 # define MANGLE_TRACE(FN, INPUT)
78 # define MANGLE_TRACE_TREE(FN, NODE)
79 #endif
81 /* Nonzero if NODE is a class template-id. We can't rely on
82 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
83 that hard to distinguish A<T> from A, where A<T> is the type as
84 instantiated outside of the template, and A is the type used
85 without parameters inside the template. */
86 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
87 (TYPE_LANG_SPECIFIC (NODE) != NULL \
88 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
89 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
90 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
92 /* Things we only need one of. This module is not reentrant. */
93 typedef struct GTY(()) globals {
94 /* An array of the current substitution candidates, in the order
95 we've seen them. */
96 VEC(tree,gc) *substitutions;
98 /* The entity that is being mangled. */
99 tree GTY ((skip)) entity;
101 /* True if the mangling will be different in a future version of the
102 ABI. */
103 bool need_abi_warning;
104 } globals;
106 static GTY (()) globals G;
108 /* The obstack on which we build mangled names. */
109 static struct obstack *mangle_obstack;
111 /* The obstack on which we build mangled names that are not going to
112 be IDENTIFIER_NODEs. */
113 static struct obstack name_obstack;
115 /* The first object on the name_obstack; we use this to free memory
116 allocated on the name_obstack. */
117 static void *name_base;
119 /* Indices into subst_identifiers. These are identifiers used in
120 special substitution rules. */
121 typedef enum
123 SUBID_ALLOCATOR,
124 SUBID_BASIC_STRING,
125 SUBID_CHAR_TRAITS,
126 SUBID_BASIC_ISTREAM,
127 SUBID_BASIC_OSTREAM,
128 SUBID_BASIC_IOSTREAM,
129 SUBID_MAX
131 substitution_identifier_index_t;
133 /* For quick substitution checks, look up these common identifiers
134 once only. */
135 static GTY(()) tree subst_identifiers[SUBID_MAX];
137 /* Single-letter codes for builtin integer types, defined in
138 <builtin-type>. These are indexed by integer_type_kind values. */
139 static const char
140 integer_type_codes[itk_none] =
142 'c', /* itk_char */
143 'a', /* itk_signed_char */
144 'h', /* itk_unsigned_char */
145 's', /* itk_short */
146 't', /* itk_unsigned_short */
147 'i', /* itk_int */
148 'j', /* itk_unsigned_int */
149 'l', /* itk_long */
150 'm', /* itk_unsigned_long */
151 'x', /* itk_long_long */
152 'y' /* itk_unsigned_long_long */
155 static int decl_is_template_id (const tree, tree* const);
157 /* Functions for handling substitutions. */
159 static inline tree canonicalize_for_substitution (tree);
160 static void add_substitution (tree);
161 static inline int is_std_substitution (const tree,
162 const substitution_identifier_index_t);
163 static inline int is_std_substitution_char (const tree,
164 const substitution_identifier_index_t);
165 static int find_substitution (tree);
166 static void mangle_call_offset (const tree, const tree);
168 /* Functions for emitting mangled representations of things. */
170 static void write_mangled_name (const tree, bool);
171 static void write_encoding (const tree);
172 static void write_name (tree, const int);
173 static void write_unscoped_name (const tree);
174 static void write_unscoped_template_name (const tree);
175 static void write_nested_name (const tree);
176 static void write_prefix (const tree);
177 static void write_template_prefix (const tree);
178 static void write_unqualified_name (const tree);
179 static void write_conversion_operator_name (const tree);
180 static void write_source_name (tree);
181 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
182 const unsigned int);
183 static void write_number (unsigned HOST_WIDE_INT, const int,
184 const unsigned int);
185 static void write_integer_cst (const tree);
186 static void write_real_cst (const tree);
187 static void write_identifier (const char *);
188 static void write_special_name_constructor (const tree);
189 static void write_special_name_destructor (const tree);
190 static void write_type (tree);
191 static int write_CV_qualifiers_for_type (const tree);
192 static void write_builtin_type (tree);
193 static void write_function_type (const tree);
194 static void write_bare_function_type (const tree, const int, const tree);
195 static void write_method_parms (tree, const int, const tree);
196 static void write_class_enum_type (const tree);
197 static void write_template_args (tree);
198 static void write_expression (tree);
199 static void write_template_arg_literal (const tree);
200 static void write_template_arg (tree);
201 static void write_template_template_arg (const tree);
202 static void write_array_type (const tree);
203 static void write_pointer_to_member_type (const tree);
204 static void write_template_param (const tree);
205 static void write_template_template_param (const tree);
206 static void write_substitution (const int);
207 static int discriminator_for_local_entity (tree);
208 static int discriminator_for_string_literal (tree, tree);
209 static void write_discriminator (const int);
210 static void write_local_name (const tree, const tree, const tree);
211 static void dump_substitution_candidates (void);
212 static tree mangle_decl_string (const tree);
214 /* Control functions. */
216 static inline void start_mangling (const tree);
217 static inline const char *finish_mangling (const bool);
218 static tree mangle_special_for_type (const tree, const char *);
220 /* Foreign language functions. */
222 static void write_java_integer_type_codes (const tree);
224 /* Append a single character to the end of the mangled
225 representation. */
226 #define write_char(CHAR) \
227 obstack_1grow (mangle_obstack, (CHAR))
229 /* Append a sized buffer to the end of the mangled representation. */
230 #define write_chars(CHAR, LEN) \
231 obstack_grow (mangle_obstack, (CHAR), (LEN))
233 /* Append a NUL-terminated string to the end of the mangled
234 representation. */
235 #define write_string(STRING) \
236 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
238 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
239 same purpose (context, which may be a type) and value (template
240 decl). See write_template_prefix for more information on what this
241 is used for. */
242 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
243 (TREE_CODE (NODE1) == TREE_LIST \
244 && TREE_CODE (NODE2) == TREE_LIST \
245 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
246 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
247 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
248 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
250 /* Write out an unsigned quantity in base 10. */
251 #define write_unsigned_number(NUMBER) \
252 write_number ((NUMBER), /*unsigned_p=*/1, 10)
254 /* If DECL is a template instance, return nonzero and, if
255 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
256 Otherwise return zero. */
258 static int
259 decl_is_template_id (const tree decl, tree* const template_info)
261 if (TREE_CODE (decl) == TYPE_DECL)
263 /* TYPE_DECLs are handled specially. Look at its type to decide
264 if this is a template instantiation. */
265 const tree type = TREE_TYPE (decl);
267 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
269 if (template_info != NULL)
270 /* For a templated TYPE_DECL, the template info is hanging
271 off the type. */
272 *template_info = TYPE_TEMPLATE_INFO (type);
273 return 1;
276 else
278 /* Check if this is a primary template. */
279 if (DECL_LANG_SPECIFIC (decl) != NULL
280 && DECL_USE_TEMPLATE (decl)
281 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
282 && TREE_CODE (decl) != TEMPLATE_DECL)
284 if (template_info != NULL)
285 /* For most templated decls, the template info is hanging
286 off the decl. */
287 *template_info = DECL_TEMPLATE_INFO (decl);
288 return 1;
292 /* It's not a template id. */
293 return 0;
296 /* Produce debugging output of current substitution candidates. */
298 static void
299 dump_substitution_candidates (void)
301 unsigned i;
302 tree el;
304 fprintf (stderr, " ++ substitutions ");
305 for (i = 0; VEC_iterate (tree, G.substitutions, i, el); ++i)
307 const char *name = "???";
309 if (i > 0)
310 fprintf (stderr, " ");
311 if (DECL_P (el))
312 name = IDENTIFIER_POINTER (DECL_NAME (el));
313 else if (TREE_CODE (el) == TREE_LIST)
314 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
315 else if (TYPE_NAME (el))
316 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
317 fprintf (stderr, " S%d_ = ", i - 1);
318 if (TYPE_P (el) &&
319 (CP_TYPE_RESTRICT_P (el)
320 || CP_TYPE_VOLATILE_P (el)
321 || CP_TYPE_CONST_P (el)))
322 fprintf (stderr, "CV-");
323 fprintf (stderr, "%s (%s at %p)\n",
324 name, tree_code_name[TREE_CODE (el)], (void *) el);
328 /* Both decls and types can be substitution candidates, but sometimes
329 they refer to the same thing. For instance, a TYPE_DECL and
330 RECORD_TYPE for the same class refer to the same thing, and should
331 be treated accordingly in substitutions. This function returns a
332 canonicalized tree node representing NODE that is used when adding
333 and substitution candidates and finding matches. */
335 static inline tree
336 canonicalize_for_substitution (tree node)
338 /* For a TYPE_DECL, use the type instead. */
339 if (TREE_CODE (node) == TYPE_DECL)
340 node = TREE_TYPE (node);
341 if (TYPE_P (node))
342 node = canonical_type_variant (node);
344 return node;
347 /* Add NODE as a substitution candidate. NODE must not already be on
348 the list of candidates. */
350 static void
351 add_substitution (tree node)
353 tree c;
355 if (DEBUG_MANGLE)
356 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
357 tree_code_name[TREE_CODE (node)], (void *) node);
359 /* Get the canonicalized substitution candidate for NODE. */
360 c = canonicalize_for_substitution (node);
361 if (DEBUG_MANGLE && c != node)
362 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
363 tree_code_name[TREE_CODE (node)], (void *) node);
364 node = c;
366 #if ENABLE_CHECKING
367 /* Make sure NODE isn't already a candidate. */
369 int i;
370 tree candidate;
372 for (i = 0; VEC_iterate (tree, G.substitutions, i, candidate); i++)
374 gcc_assert (!(DECL_P (node) && node == candidate));
375 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
376 && same_type_p (node, candidate)));
379 #endif /* ENABLE_CHECKING */
381 /* Put the decl onto the varray of substitution candidates. */
382 VEC_safe_push (tree, gc, G.substitutions, node);
384 if (DEBUG_MANGLE)
385 dump_substitution_candidates ();
388 /* Helper function for find_substitution. Returns nonzero if NODE,
389 which may be a decl or a CLASS_TYPE, is a template-id with template
390 name of substitution_index[INDEX] in the ::std namespace. */
392 static inline int
393 is_std_substitution (const tree node,
394 const substitution_identifier_index_t index)
396 tree type = NULL;
397 tree decl = NULL;
399 if (DECL_P (node))
401 type = TREE_TYPE (node);
402 decl = node;
404 else if (CLASS_TYPE_P (node))
406 type = node;
407 decl = TYPE_NAME (node);
409 else
410 /* These are not the droids you're looking for. */
411 return 0;
413 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
414 && TYPE_LANG_SPECIFIC (type)
415 && TYPE_TEMPLATE_INFO (type)
416 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
417 == subst_identifiers[index]));
420 /* Helper function for find_substitution. Returns nonzero if NODE,
421 which may be a decl or a CLASS_TYPE, is the template-id
422 ::std::identifier<char>, where identifier is
423 substitution_index[INDEX]. */
425 static inline int
426 is_std_substitution_char (const tree node,
427 const substitution_identifier_index_t index)
429 tree args;
430 /* Check NODE's name is ::std::identifier. */
431 if (!is_std_substitution (node, index))
432 return 0;
433 /* Figure out its template args. */
434 if (DECL_P (node))
435 args = DECL_TI_ARGS (node);
436 else if (CLASS_TYPE_P (node))
437 args = CLASSTYPE_TI_ARGS (node);
438 else
439 /* Oops, not a template. */
440 return 0;
441 /* NODE's template arg list should be <char>. */
442 return
443 TREE_VEC_LENGTH (args) == 1
444 && TREE_VEC_ELT (args, 0) == char_type_node;
447 /* Check whether a substitution should be used to represent NODE in
448 the mangling.
450 First, check standard special-case substitutions.
452 <substitution> ::= St
453 # ::std
455 ::= Sa
456 # ::std::allocator
458 ::= Sb
459 # ::std::basic_string
461 ::= Ss
462 # ::std::basic_string<char,
463 ::std::char_traits<char>,
464 ::std::allocator<char> >
466 ::= Si
467 # ::std::basic_istream<char, ::std::char_traits<char> >
469 ::= So
470 # ::std::basic_ostream<char, ::std::char_traits<char> >
472 ::= Sd
473 # ::std::basic_iostream<char, ::std::char_traits<char> >
475 Then examine the stack of currently available substitution
476 candidates for entities appearing earlier in the same mangling
478 If a substitution is found, write its mangled representation and
479 return nonzero. If none is found, just return zero. */
481 static int
482 find_substitution (tree node)
484 int i;
485 const int size = VEC_length (tree, G.substitutions);
486 tree decl;
487 tree type;
489 if (DEBUG_MANGLE)
490 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
491 tree_code_name[TREE_CODE (node)], (void *) node);
493 /* Obtain the canonicalized substitution representation for NODE.
494 This is what we'll compare against. */
495 node = canonicalize_for_substitution (node);
497 /* Check for builtin substitutions. */
499 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
500 type = TYPE_P (node) ? node : TREE_TYPE (node);
502 /* Check for std::allocator. */
503 if (decl
504 && is_std_substitution (decl, SUBID_ALLOCATOR)
505 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
507 write_string ("Sa");
508 return 1;
511 /* Check for std::basic_string. */
512 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
514 if (TYPE_P (node))
516 /* If this is a type (i.e. a fully-qualified template-id),
517 check for
518 std::basic_string <char,
519 std::char_traits<char>,
520 std::allocator<char> > . */
521 if (cp_type_quals (type) == TYPE_UNQUALIFIED
522 && CLASSTYPE_USE_TEMPLATE (type))
524 tree args = CLASSTYPE_TI_ARGS (type);
525 if (TREE_VEC_LENGTH (args) == 3
526 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
527 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
528 SUBID_CHAR_TRAITS)
529 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
530 SUBID_ALLOCATOR))
532 write_string ("Ss");
533 return 1;
537 else
538 /* Substitute for the template name only if this isn't a type. */
540 write_string ("Sb");
541 return 1;
545 /* Check for basic_{i,o,io}stream. */
546 if (TYPE_P (node)
547 && cp_type_quals (type) == TYPE_UNQUALIFIED
548 && CLASS_TYPE_P (type)
549 && CLASSTYPE_USE_TEMPLATE (type)
550 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
552 /* First, check for the template
553 args <char, std::char_traits<char> > . */
554 tree args = CLASSTYPE_TI_ARGS (type);
555 if (TREE_VEC_LENGTH (args) == 2
556 && TYPE_P (TREE_VEC_ELT (args, 0))
557 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
558 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
559 SUBID_CHAR_TRAITS))
561 /* Got them. Is this basic_istream? */
562 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
564 write_string ("Si");
565 return 1;
567 /* Or basic_ostream? */
568 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
570 write_string ("So");
571 return 1;
573 /* Or basic_iostream? */
574 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
576 write_string ("Sd");
577 return 1;
582 /* Check for namespace std. */
583 if (decl && DECL_NAMESPACE_STD_P (decl))
585 write_string ("St");
586 return 1;
589 /* Now check the list of available substitutions for this mangling
590 operation. */
591 for (i = 0; i < size; ++i)
593 tree candidate = VEC_index (tree, G.substitutions, i);
594 /* NODE is a matched to a candidate if it's the same decl node or
595 if it's the same type. */
596 if (decl == candidate
597 || (TYPE_P (candidate) && type && TYPE_P (type)
598 && same_type_p (type, candidate))
599 || NESTED_TEMPLATE_MATCH (node, candidate))
601 write_substitution (i);
602 return 1;
606 /* No substitution found. */
607 return 0;
611 /* TOP_LEVEL is true, if this is being called at outermost level of
612 mangling. It should be false when mangling a decl appearing in an
613 expression within some other mangling.
615 <mangled-name> ::= _Z <encoding> */
617 static void
618 write_mangled_name (const tree decl, bool top_level)
620 MANGLE_TRACE_TREE ("mangled-name", decl);
622 if (/* The names of `extern "C"' functions are not mangled. */
623 DECL_EXTERN_C_FUNCTION_P (decl)
624 /* But overloaded operator names *are* mangled. */
625 && !DECL_OVERLOADED_OPERATOR_P (decl))
627 unmangled_name:;
629 if (top_level)
630 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
631 else
633 /* The standard notes: "The <encoding> of an extern "C"
634 function is treated like global-scope data, i.e. as its
635 <source-name> without a type." We cannot write
636 overloaded operators that way though, because it contains
637 characters invalid in assembler. */
638 if (abi_version_at_least (2))
639 write_string ("_Z");
640 else
641 G.need_abi_warning = true;
642 write_source_name (DECL_NAME (decl));
645 else if (TREE_CODE (decl) == VAR_DECL
646 /* The names of non-static global variables aren't mangled. */
647 && DECL_EXTERNAL_LINKAGE_P (decl)
648 && (CP_DECL_CONTEXT (decl) == global_namespace
649 /* And neither are `extern "C"' variables. */
650 || DECL_EXTERN_C_P (decl)))
652 if (top_level || abi_version_at_least (2))
653 goto unmangled_name;
654 else
656 G.need_abi_warning = true;
657 goto mangled_name;
660 else
662 mangled_name:;
663 write_string ("_Z");
664 write_encoding (decl);
665 if (DECL_LANG_SPECIFIC (decl)
666 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
667 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
668 /* We need a distinct mangled name for these entities, but
669 we should never actually output it. So, we append some
670 characters the assembler won't like. */
671 write_string (" *INTERNAL* ");
675 /* <encoding> ::= <function name> <bare-function-type>
676 ::= <data name> */
678 static void
679 write_encoding (const tree decl)
681 MANGLE_TRACE_TREE ("encoding", decl);
683 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
685 /* For overloaded operators write just the mangled name
686 without arguments. */
687 if (DECL_OVERLOADED_OPERATOR_P (decl))
688 write_name (decl, /*ignore_local_scope=*/0);
689 else
690 write_source_name (DECL_NAME (decl));
691 return;
694 write_name (decl, /*ignore_local_scope=*/0);
695 if (TREE_CODE (decl) == FUNCTION_DECL)
697 tree fn_type;
698 tree d;
700 if (decl_is_template_id (decl, NULL))
702 fn_type = get_mostly_instantiated_function_type (decl);
703 /* FN_TYPE will not have parameter types for in-charge or
704 VTT parameters. Therefore, we pass NULL_TREE to
705 write_bare_function_type -- otherwise, it will get
706 confused about which artificial parameters to skip. */
707 d = NULL_TREE;
709 else
711 fn_type = TREE_TYPE (decl);
712 d = decl;
715 write_bare_function_type (fn_type,
716 (!DECL_CONSTRUCTOR_P (decl)
717 && !DECL_DESTRUCTOR_P (decl)
718 && !DECL_CONV_FN_P (decl)
719 && decl_is_template_id (decl, NULL)),
724 /* <name> ::= <unscoped-name>
725 ::= <unscoped-template-name> <template-args>
726 ::= <nested-name>
727 ::= <local-name>
729 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
730 called from <local-name>, which mangles the enclosing scope
731 elsewhere and then uses this function to mangle just the part
732 underneath the function scope. So don't use the <local-name>
733 production, to avoid an infinite recursion. */
735 static void
736 write_name (tree decl, const int ignore_local_scope)
738 tree context;
740 MANGLE_TRACE_TREE ("name", decl);
742 if (TREE_CODE (decl) == TYPE_DECL)
744 /* In case this is a typedef, fish out the corresponding
745 TYPE_DECL for the main variant. */
746 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
747 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
749 else
750 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
752 /* A decl in :: or ::std scope is treated specially. The former is
753 mangled using <unscoped-name> or <unscoped-template-name>, the
754 latter with a special substitution. Also, a name that is
755 directly in a local function scope is also mangled with
756 <unscoped-name> rather than a full <nested-name>. */
757 if (context == NULL
758 || context == global_namespace
759 || DECL_NAMESPACE_STD_P (context)
760 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
762 tree template_info;
763 /* Is this a template instance? */
764 if (decl_is_template_id (decl, &template_info))
766 /* Yes: use <unscoped-template-name>. */
767 write_unscoped_template_name (TI_TEMPLATE (template_info));
768 write_template_args (TI_ARGS (template_info));
770 else
771 /* Everything else gets an <unqualified-name>. */
772 write_unscoped_name (decl);
774 else
776 /* Handle local names, unless we asked not to (that is, invoked
777 under <local-name>, to handle only the part of the name under
778 the local scope). */
779 if (!ignore_local_scope)
781 /* Scan up the list of scope context, looking for a
782 function. If we find one, this entity is in local
783 function scope. local_entity tracks context one scope
784 level down, so it will contain the element that's
785 directly in that function's scope, either decl or one of
786 its enclosing scopes. */
787 tree local_entity = decl;
788 while (context != NULL && context != global_namespace)
790 /* Make sure we're always dealing with decls. */
791 if (context != NULL && TYPE_P (context))
792 context = TYPE_NAME (context);
793 /* Is this a function? */
794 if (TREE_CODE (context) == FUNCTION_DECL)
796 /* Yes, we have local scope. Use the <local-name>
797 production for the innermost function scope. */
798 write_local_name (context, local_entity, decl);
799 return;
801 /* Up one scope level. */
802 local_entity = context;
803 context = CP_DECL_CONTEXT (context);
806 /* No local scope found? Fall through to <nested-name>. */
809 /* Other decls get a <nested-name> to encode their scope. */
810 write_nested_name (decl);
814 /* <unscoped-name> ::= <unqualified-name>
815 ::= St <unqualified-name> # ::std:: */
817 static void
818 write_unscoped_name (const tree decl)
820 tree context = CP_DECL_CONTEXT (decl);
822 MANGLE_TRACE_TREE ("unscoped-name", decl);
824 /* Is DECL in ::std? */
825 if (DECL_NAMESPACE_STD_P (context))
827 write_string ("St");
828 write_unqualified_name (decl);
830 else
832 /* If not, it should be either in the global namespace, or directly
833 in a local function scope. */
834 gcc_assert (context == global_namespace
835 || context == NULL
836 || TREE_CODE (context) == FUNCTION_DECL);
838 write_unqualified_name (decl);
842 /* <unscoped-template-name> ::= <unscoped-name>
843 ::= <substitution> */
845 static void
846 write_unscoped_template_name (const tree decl)
848 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
850 if (find_substitution (decl))
851 return;
852 write_unscoped_name (decl);
853 add_substitution (decl);
856 /* Write the nested name, including CV-qualifiers, of DECL.
858 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
859 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
861 <CV-qualifiers> ::= [r] [V] [K] */
863 static void
864 write_nested_name (const tree decl)
866 tree template_info;
868 MANGLE_TRACE_TREE ("nested-name", decl);
870 write_char ('N');
872 /* Write CV-qualifiers, if this is a member function. */
873 if (TREE_CODE (decl) == FUNCTION_DECL
874 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
876 if (DECL_VOLATILE_MEMFUNC_P (decl))
877 write_char ('V');
878 if (DECL_CONST_MEMFUNC_P (decl))
879 write_char ('K');
882 /* Is this a template instance? */
883 if (decl_is_template_id (decl, &template_info))
885 /* Yes, use <template-prefix>. */
886 write_template_prefix (decl);
887 write_template_args (TI_ARGS (template_info));
889 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
891 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
892 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
894 write_template_prefix (decl);
895 write_template_args (TREE_OPERAND (name, 1));
897 else
899 write_prefix (CP_DECL_CONTEXT (decl));
900 write_unqualified_name (decl);
903 else
905 /* No, just use <prefix> */
906 write_prefix (DECL_CONTEXT (decl));
907 write_unqualified_name (decl);
909 write_char ('E');
912 /* <prefix> ::= <prefix> <unqualified-name>
913 ::= <template-param>
914 ::= <template-prefix> <template-args>
915 ::= # empty
916 ::= <substitution> */
918 static void
919 write_prefix (const tree node)
921 tree decl;
922 /* Non-NULL if NODE represents a template-id. */
923 tree template_info = NULL;
925 MANGLE_TRACE_TREE ("prefix", node);
927 if (node == NULL
928 || node == global_namespace)
929 return;
931 if (find_substitution (node))
932 return;
934 if (DECL_P (node))
936 /* If this is a function decl, that means we've hit function
937 scope, so this prefix must be for a local name. In this
938 case, we're under the <local-name> production, which encodes
939 the enclosing function scope elsewhere. So don't continue
940 here. */
941 if (TREE_CODE (node) == FUNCTION_DECL)
942 return;
944 decl = node;
945 decl_is_template_id (decl, &template_info);
947 else
949 /* Node is a type. */
950 decl = TYPE_NAME (node);
951 if (CLASSTYPE_TEMPLATE_ID_P (node))
952 template_info = TYPE_TEMPLATE_INFO (node);
955 /* In G++ 3.2, the name of the template parameter was used. */
956 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
957 && !abi_version_at_least (2))
958 G.need_abi_warning = true;
960 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
961 && abi_version_at_least (2))
962 write_template_param (node);
963 else if (template_info != NULL)
964 /* Templated. */
966 write_template_prefix (decl);
967 write_template_args (TI_ARGS (template_info));
969 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
971 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
972 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
974 write_template_prefix (decl);
975 write_template_args (TREE_OPERAND (name, 1));
977 else
979 write_prefix (CP_DECL_CONTEXT (decl));
980 write_unqualified_name (decl);
983 else
984 /* Not templated. */
986 write_prefix (CP_DECL_CONTEXT (decl));
987 write_unqualified_name (decl);
990 add_substitution (node);
993 /* <template-prefix> ::= <prefix> <template component>
994 ::= <template-param>
995 ::= <substitution> */
997 static void
998 write_template_prefix (const tree node)
1000 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1001 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1002 tree context = CP_DECL_CONTEXT (decl);
1003 tree template_info;
1004 tree templ;
1005 tree substitution;
1007 MANGLE_TRACE_TREE ("template-prefix", node);
1009 /* Find the template decl. */
1010 if (decl_is_template_id (decl, &template_info))
1011 templ = TI_TEMPLATE (template_info);
1012 else if (TREE_CODE (type) == TYPENAME_TYPE)
1013 /* For a typename type, all we have is the name. */
1014 templ = DECL_NAME (decl);
1015 else
1017 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1019 templ = TYPE_TI_TEMPLATE (type);
1022 /* For a member template, though, the template name for the
1023 innermost name must have all the outer template levels
1024 instantiated. For instance, consider
1026 template<typename T> struct Outer {
1027 template<typename U> struct Inner {};
1030 The template name for `Inner' in `Outer<int>::Inner<float>' is
1031 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1032 levels separately, so there's no TEMPLATE_DECL available for this
1033 (there's only `Outer<T>::Inner<U>').
1035 In order to get the substitutions right, we create a special
1036 TREE_LIST to represent the substitution candidate for a nested
1037 template. The TREE_PURPOSE is the template's context, fully
1038 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1039 template.
1041 So, for the example above, `Outer<int>::Inner' is represented as a
1042 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1043 and whose value is `Outer<T>::Inner<U>'. */
1044 if (TYPE_P (context))
1045 substitution = build_tree_list (context, templ);
1046 else
1047 substitution = templ;
1049 if (find_substitution (substitution))
1050 return;
1052 /* In G++ 3.2, the name of the template template parameter was used. */
1053 if (TREE_TYPE (templ)
1054 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1055 && !abi_version_at_least (2))
1056 G.need_abi_warning = true;
1058 if (TREE_TYPE (templ)
1059 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1060 && abi_version_at_least (2))
1061 write_template_param (TREE_TYPE (templ));
1062 else
1064 write_prefix (context);
1065 write_unqualified_name (decl);
1068 add_substitution (substitution);
1071 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1072 mangled through special entry points.
1074 <unqualified-name> ::= <operator-name>
1075 ::= <special-name>
1076 ::= <source-name>
1077 ::= <local-source-name>
1079 <local-source-name> ::= L <source-name> <discriminator> */
1081 static void
1082 write_unqualified_name (const tree decl)
1084 MANGLE_TRACE_TREE ("unqualified-name", decl);
1086 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
1087 write_special_name_constructor (decl);
1088 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
1089 write_special_name_destructor (decl);
1090 else if (DECL_NAME (decl) == NULL_TREE)
1092 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1093 write_source_name (DECL_ASSEMBLER_NAME (decl));
1095 else if (DECL_CONV_FN_P (decl))
1097 /* Conversion operator. Handle it right here.
1098 <operator> ::= cv <type> */
1099 tree type;
1100 if (decl_is_template_id (decl, NULL))
1102 tree fn_type;
1103 fn_type = get_mostly_instantiated_function_type (decl);
1104 type = TREE_TYPE (fn_type);
1106 else
1107 type = DECL_CONV_FN_TYPE (decl);
1108 write_conversion_operator_name (type);
1110 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1112 operator_name_info_t *oni;
1113 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1114 oni = assignment_operator_name_info;
1115 else
1116 oni = operator_name_info;
1118 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1120 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1121 && DECL_NAMESPACE_SCOPE_P (decl)
1122 && decl_linkage (decl) == lk_internal)
1124 MANGLE_TRACE_TREE ("local-source-name", decl);
1125 write_char ('L');
1126 write_source_name (DECL_NAME (decl));
1127 /* The default discriminator is 1, and that's all we ever use,
1128 so there's no code to output one here. */
1130 else
1131 write_source_name (DECL_NAME (decl));
1134 /* Write the unqualified-name for a conversion operator to TYPE. */
1136 static void
1137 write_conversion_operator_name (const tree type)
1139 write_string ("cv");
1140 write_type (type);
1143 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1145 <source-name> ::= </length/ number> <identifier> */
1147 static void
1148 write_source_name (tree identifier)
1150 MANGLE_TRACE_TREE ("source-name", identifier);
1152 /* Never write the whole template-id name including the template
1153 arguments; we only want the template name. */
1154 if (IDENTIFIER_TEMPLATE (identifier))
1155 identifier = IDENTIFIER_TEMPLATE (identifier);
1157 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1158 write_identifier (IDENTIFIER_POINTER (identifier));
1161 /* Convert NUMBER to ascii using base BASE and generating at least
1162 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1163 into which to store the characters. Returns the number of
1164 characters generated (these will be layed out in advance of where
1165 BUFFER points). */
1167 static int
1168 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1169 char *buffer, const unsigned int min_digits)
1171 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1172 unsigned digits = 0;
1174 while (number)
1176 unsigned HOST_WIDE_INT d = number / base;
1178 *--buffer = base_digits[number - d * base];
1179 digits++;
1180 number = d;
1182 while (digits < min_digits)
1184 *--buffer = base_digits[0];
1185 digits++;
1187 return digits;
1190 /* Non-terminal <number>.
1192 <number> ::= [n] </decimal integer/> */
1194 static void
1195 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1196 const unsigned int base)
1198 char buffer[sizeof (HOST_WIDE_INT) * 8];
1199 unsigned count = 0;
1201 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1203 write_char ('n');
1204 number = -((HOST_WIDE_INT) number);
1206 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1207 write_chars (buffer + sizeof (buffer) - count, count);
1210 /* Write out an integral CST in decimal. Most numbers are small, and
1211 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1212 bigger than that, which we must deal with. */
1214 static inline void
1215 write_integer_cst (const tree cst)
1217 int sign = tree_int_cst_sgn (cst);
1219 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1221 /* A bignum. We do this in chunks, each of which fits in a
1222 HOST_WIDE_INT. */
1223 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1224 unsigned HOST_WIDE_INT chunk;
1225 unsigned chunk_digits;
1226 char *ptr = buffer + sizeof (buffer);
1227 unsigned count = 0;
1228 tree n, base, type;
1229 int done;
1231 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1232 representable. */
1233 chunk = 1000000000;
1234 chunk_digits = 9;
1236 if (sizeof (HOST_WIDE_INT) >= 8)
1238 /* It is at least 64 bits, so 10^18 is representable. */
1239 chunk_digits = 18;
1240 chunk *= chunk;
1243 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1244 base = build_int_cstu (type, chunk);
1245 n = build_int_cst_wide (type,
1246 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1248 if (sign < 0)
1250 write_char ('n');
1251 n = fold_build1 (NEGATE_EXPR, type, n);
1255 tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
1256 tree tmp = fold_build2 (MULT_EXPR, type, d, base);
1257 unsigned c;
1259 done = integer_zerop (d);
1260 tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
1261 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1262 done ? 1 : chunk_digits);
1263 ptr -= c;
1264 count += c;
1265 n = d;
1267 while (!done);
1268 write_chars (ptr, count);
1270 else
1272 /* A small num. */
1273 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1275 if (sign < 0)
1277 write_char ('n');
1278 low = -low;
1280 write_unsigned_number (low);
1284 /* Write out a floating-point literal.
1286 "Floating-point literals are encoded using the bit pattern of the
1287 target processor's internal representation of that number, as a
1288 fixed-length lowercase hexadecimal string, high-order bytes first
1289 (even if the target processor would store low-order bytes first).
1290 The "n" prefix is not used for floating-point literals; the sign
1291 bit is encoded with the rest of the number.
1293 Here are some examples, assuming the IEEE standard representation
1294 for floating point numbers. (Spaces are for readability, not
1295 part of the encoding.)
1297 1.0f Lf 3f80 0000 E
1298 -1.0f Lf bf80 0000 E
1299 1.17549435e-38f Lf 0080 0000 E
1300 1.40129846e-45f Lf 0000 0001 E
1301 0.0f Lf 0000 0000 E"
1303 Caller is responsible for the Lx and the E. */
1304 static void
1305 write_real_cst (const tree value)
1307 if (abi_version_at_least (2))
1309 long target_real[4]; /* largest supported float */
1310 char buffer[9]; /* eight hex digits in a 32-bit number */
1311 int i, limit, dir;
1313 tree type = TREE_TYPE (value);
1314 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1316 real_to_target (target_real, &TREE_REAL_CST (value),
1317 TYPE_MODE (type));
1319 /* The value in target_real is in the target word order,
1320 so we must write it out backward if that happens to be
1321 little-endian. write_number cannot be used, it will
1322 produce uppercase. */
1323 if (FLOAT_WORDS_BIG_ENDIAN)
1324 i = 0, limit = words, dir = 1;
1325 else
1326 i = words - 1, limit = -1, dir = -1;
1328 for (; i != limit; i += dir)
1330 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1331 write_chars (buffer, 8);
1334 else
1336 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1337 literally. Note that compatibility with 3.2 is impossible,
1338 because the old floating-point emulator used a different
1339 format for REAL_VALUE_TYPE. */
1340 size_t i;
1341 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1342 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1343 /*unsigned_p*/ 1,
1344 /*base*/ 16);
1345 G.need_abi_warning = 1;
1349 /* Non-terminal <identifier>.
1351 <identifier> ::= </unqualified source code identifier> */
1353 static void
1354 write_identifier (const char *identifier)
1356 MANGLE_TRACE ("identifier", identifier);
1357 write_string (identifier);
1360 /* Handle constructor productions of non-terminal <special-name>.
1361 CTOR is a constructor FUNCTION_DECL.
1363 <special-name> ::= C1 # complete object constructor
1364 ::= C2 # base object constructor
1365 ::= C3 # complete object allocating constructor
1367 Currently, allocating constructors are never used.
1369 We also need to provide mangled names for the maybe-in-charge
1370 constructor, so we treat it here too. mangle_decl_string will
1371 append *INTERNAL* to that, to make sure we never emit it. */
1373 static void
1374 write_special_name_constructor (const tree ctor)
1376 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1377 write_string ("C2");
1378 else
1380 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1381 /* Even though we don't ever emit a definition of
1382 the old-style destructor, we still have to
1383 consider entities (like static variables) nested
1384 inside it. */
1385 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor));
1386 write_string ("C1");
1390 /* Handle destructor productions of non-terminal <special-name>.
1391 DTOR is a destructor FUNCTION_DECL.
1393 <special-name> ::= D0 # deleting (in-charge) destructor
1394 ::= D1 # complete object (in-charge) destructor
1395 ::= D2 # base object (not-in-charge) destructor
1397 We also need to provide mangled names for the maybe-incharge
1398 destructor, so we treat it here too. mangle_decl_string will
1399 append *INTERNAL* to that, to make sure we never emit it. */
1401 static void
1402 write_special_name_destructor (const tree dtor)
1404 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1405 write_string ("D0");
1406 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1407 write_string ("D2");
1408 else
1410 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1411 /* Even though we don't ever emit a definition of
1412 the old-style destructor, we still have to
1413 consider entities (like static variables) nested
1414 inside it. */
1415 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1416 write_string ("D1");
1420 /* Return the discriminator for ENTITY appearing inside
1421 FUNCTION. The discriminator is the lexical ordinal of VAR among
1422 entities with the same name in the same FUNCTION. */
1424 static int
1425 discriminator_for_local_entity (tree entity)
1427 /* Assume this is the only local entity with this name. */
1428 int discriminator = 0;
1430 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1431 discriminator = DECL_DISCRIMINATOR (entity);
1432 else if (TREE_CODE (entity) == TYPE_DECL)
1434 int ix;
1436 /* Scan the list of local classes. */
1437 entity = TREE_TYPE (entity);
1438 for (ix = 0; ; ix++)
1440 tree type = VEC_index (tree, local_classes, ix);
1441 if (type == entity)
1442 break;
1443 if (TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (entity)
1444 && TYPE_CONTEXT (type) == TYPE_CONTEXT (entity))
1445 ++discriminator;
1449 return discriminator;
1452 /* Return the discriminator for STRING, a string literal used inside
1453 FUNCTION. The discriminator is the lexical ordinal of STRING among
1454 string literals used in FUNCTION. */
1456 static int
1457 discriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
1458 tree string ATTRIBUTE_UNUSED)
1460 /* For now, we don't discriminate amongst string literals. */
1461 return 0;
1464 /* <discriminator> := _ <number>
1466 The discriminator is used only for the second and later occurrences
1467 of the same name within a single function. In this case <number> is
1468 n - 2, if this is the nth occurrence, in lexical order. */
1470 static void
1471 write_discriminator (const int discriminator)
1473 /* If discriminator is zero, don't write anything. Otherwise... */
1474 if (discriminator > 0)
1476 write_char ('_');
1477 write_unsigned_number (discriminator - 1);
1481 /* Mangle the name of a function-scope entity. FUNCTION is the
1482 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1483 the entity itself. LOCAL_ENTITY is the entity that's directly
1484 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1485 of ENTITY.
1487 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1488 := Z <function encoding> E s [<discriminator>] */
1490 static void
1491 write_local_name (const tree function, const tree local_entity,
1492 const tree entity)
1494 MANGLE_TRACE_TREE ("local-name", entity);
1496 write_char ('Z');
1497 write_encoding (function);
1498 write_char ('E');
1499 if (TREE_CODE (entity) == STRING_CST)
1501 write_char ('s');
1502 write_discriminator (discriminator_for_string_literal (function,
1503 entity));
1505 else
1507 /* Now the <entity name>. Let write_name know its being called
1508 from <local-name>, so it doesn't try to process the enclosing
1509 function scope again. */
1510 write_name (entity, /*ignore_local_scope=*/1);
1511 write_discriminator (discriminator_for_local_entity (local_entity));
1515 /* Non-terminals <type> and <CV-qualifier>.
1517 <type> ::= <builtin-type>
1518 ::= <function-type>
1519 ::= <class-enum-type>
1520 ::= <array-type>
1521 ::= <pointer-to-member-type>
1522 ::= <template-param>
1523 ::= <substitution>
1524 ::= <CV-qualifier>
1525 ::= P <type> # pointer-to
1526 ::= R <type> # reference-to
1527 ::= C <type> # complex pair (C 2000)
1528 ::= G <type> # imaginary (C 2000) [not supported]
1529 ::= U <source-name> <type> # vendor extended type qualifier
1531 C++0x extensions
1533 <type> ::= RR <type> # rvalue reference-to
1534 <type> ::= Dt <expression> # decltype of an id-expression or
1535 # class member access
1536 <type> ::= DT <expression> # decltype of an expression
1538 TYPE is a type node. */
1540 static void
1541 write_type (tree type)
1543 /* This gets set to nonzero if TYPE turns out to be a (possibly
1544 CV-qualified) builtin type. */
1545 int is_builtin_type = 0;
1547 MANGLE_TRACE_TREE ("type", type);
1549 if (type == error_mark_node)
1550 return;
1552 if (find_substitution (type))
1553 return;
1555 if (write_CV_qualifiers_for_type (type) > 0)
1556 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1557 mangle the unqualified type. The recursive call is needed here
1558 since both the qualified and unqualified types are substitution
1559 candidates. */
1560 write_type (TYPE_MAIN_VARIANT (type));
1561 else if (TREE_CODE (type) == ARRAY_TYPE)
1562 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1563 so that the cv-qualification of the element type is available
1564 in write_array_type. */
1565 write_array_type (type);
1566 else
1568 tree type_orig = type;
1570 /* See through any typedefs. */
1571 type = TYPE_MAIN_VARIANT (type);
1573 if (TYPE_PTRMEM_P (type))
1574 write_pointer_to_member_type (type);
1575 else
1577 /* Handle any target-specific fundamental types. */
1578 const char *target_mangling
1579 = targetm.mangle_type (type_orig);
1581 if (target_mangling)
1583 write_string (target_mangling);
1584 /* Add substitutions for types other than fundamental
1585 types. */
1586 if (TREE_CODE (type) != VOID_TYPE
1587 && TREE_CODE (type) != INTEGER_TYPE
1588 && TREE_CODE (type) != REAL_TYPE
1589 && TREE_CODE (type) != BOOLEAN_TYPE)
1590 add_substitution (type);
1591 return;
1594 switch (TREE_CODE (type))
1596 case VOID_TYPE:
1597 case BOOLEAN_TYPE:
1598 case INTEGER_TYPE: /* Includes wchar_t. */
1599 case REAL_TYPE:
1600 case FIXED_POINT_TYPE:
1602 /* If this is a typedef, TYPE may not be one of
1603 the standard builtin type nodes, but an alias of one. Use
1604 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1605 write_builtin_type (TYPE_MAIN_VARIANT (type));
1606 ++is_builtin_type;
1608 break;
1610 case COMPLEX_TYPE:
1611 write_char ('C');
1612 write_type (TREE_TYPE (type));
1613 break;
1615 case FUNCTION_TYPE:
1616 case METHOD_TYPE:
1617 write_function_type (type);
1618 break;
1620 case UNION_TYPE:
1621 case RECORD_TYPE:
1622 case ENUMERAL_TYPE:
1623 /* A pointer-to-member function is represented as a special
1624 RECORD_TYPE, so check for this first. */
1625 if (TYPE_PTRMEMFUNC_P (type))
1626 write_pointer_to_member_type (type);
1627 else
1628 write_class_enum_type (type);
1629 break;
1631 case TYPENAME_TYPE:
1632 case UNBOUND_CLASS_TEMPLATE:
1633 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1634 ordinary nested names. */
1635 write_nested_name (TYPE_STUB_DECL (type));
1636 break;
1638 case POINTER_TYPE:
1639 write_char ('P');
1640 write_type (TREE_TYPE (type));
1641 break;
1643 case REFERENCE_TYPE:
1644 if (TYPE_REF_IS_RVALUE (type))
1645 write_char('O');
1646 else
1647 write_char ('R');
1648 write_type (TREE_TYPE (type));
1649 break;
1651 case TEMPLATE_TYPE_PARM:
1652 case TEMPLATE_PARM_INDEX:
1653 write_template_param (type);
1654 break;
1656 case TEMPLATE_TEMPLATE_PARM:
1657 write_template_template_param (type);
1658 break;
1660 case BOUND_TEMPLATE_TEMPLATE_PARM:
1661 write_template_template_param (type);
1662 write_template_args
1663 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1664 break;
1666 case VECTOR_TYPE:
1667 write_string ("U8__vector");
1668 write_type (TREE_TYPE (type));
1669 break;
1671 case TYPE_PACK_EXPANSION:
1672 write_string ("Dp");
1673 write_type (PACK_EXPANSION_PATTERN (type));
1674 break;
1676 case DECLTYPE_TYPE:
1677 write_char ('D');
1678 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
1679 write_char ('t');
1680 else
1681 write_char ('T');
1682 ++skip_evaluation;
1683 write_expression (DECLTYPE_TYPE_EXPR (type));
1684 --skip_evaluation;
1685 write_char ('E');
1686 break;
1688 case TYPEOF_TYPE:
1689 sorry ("mangling typeof, use decltype instead");
1690 break;
1692 default:
1693 gcc_unreachable ();
1698 /* Types other than builtin types are substitution candidates. */
1699 if (!is_builtin_type)
1700 add_substitution (type);
1703 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1704 CV-qualifiers written for TYPE.
1706 <CV-qualifiers> ::= [r] [V] [K] */
1708 static int
1709 write_CV_qualifiers_for_type (const tree type)
1711 int num_qualifiers = 0;
1713 /* The order is specified by:
1715 "In cases where multiple order-insensitive qualifiers are
1716 present, they should be ordered 'K' (closest to the base type),
1717 'V', 'r', and 'U' (farthest from the base type) ..."
1719 Note that we do not use cp_type_quals below; given "const
1720 int[3]", the "const" is emitted with the "int", not with the
1721 array. */
1723 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1725 write_char ('r');
1726 ++num_qualifiers;
1728 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1730 write_char ('V');
1731 ++num_qualifiers;
1733 if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1735 write_char ('K');
1736 ++num_qualifiers;
1739 return num_qualifiers;
1742 /* Non-terminal <builtin-type>.
1744 <builtin-type> ::= v # void
1745 ::= b # bool
1746 ::= w # wchar_t
1747 ::= c # char
1748 ::= a # signed char
1749 ::= h # unsigned char
1750 ::= s # short
1751 ::= t # unsigned short
1752 ::= i # int
1753 ::= j # unsigned int
1754 ::= l # long
1755 ::= m # unsigned long
1756 ::= x # long long, __int64
1757 ::= y # unsigned long long, __int64
1758 ::= n # __int128
1759 ::= o # unsigned __int128
1760 ::= f # float
1761 ::= d # double
1762 ::= e # long double, __float80
1763 ::= g # __float128 [not supported]
1764 ::= u <source-name> # vendor extended type */
1766 static void
1767 write_builtin_type (tree type)
1769 if (TYPE_CANONICAL (type))
1770 type = TYPE_CANONICAL (type);
1772 switch (TREE_CODE (type))
1774 case VOID_TYPE:
1775 write_char ('v');
1776 break;
1778 case BOOLEAN_TYPE:
1779 write_char ('b');
1780 break;
1782 case INTEGER_TYPE:
1783 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
1784 isn't in integer_type_nodes. */
1785 if (type == wchar_type_node)
1786 write_char ('w');
1787 else if (type == char16_type_node)
1788 write_string ("Ds");
1789 else if (type == char32_type_node)
1790 write_string ("Di");
1791 else if (TYPE_FOR_JAVA (type))
1792 write_java_integer_type_codes (type);
1793 else
1795 size_t itk;
1796 /* Assume TYPE is one of the shared integer type nodes. Find
1797 it in the array of these nodes. */
1798 iagain:
1799 for (itk = 0; itk < itk_none; ++itk)
1800 if (type == integer_types[itk])
1802 /* Print the corresponding single-letter code. */
1803 write_char (integer_type_codes[itk]);
1804 break;
1807 if (itk == itk_none)
1809 tree t = c_common_type_for_mode (TYPE_MODE (type),
1810 TYPE_UNSIGNED (type));
1811 if (type != t)
1813 type = t;
1814 goto iagain;
1817 if (TYPE_PRECISION (type) == 128)
1818 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
1819 else
1821 /* Allow for cases where TYPE is not one of the shared
1822 integer type nodes and write a "vendor extended builtin
1823 type" with a name the form intN or uintN, respectively.
1824 Situations like this can happen if you have an
1825 __attribute__((__mode__(__SI__))) type and use exotic
1826 switches like '-mint8' on AVR. Of course, this is
1827 undefined by the C++ ABI (and '-mint8' is not even
1828 Standard C conforming), but when using such special
1829 options you're pretty much in nowhere land anyway. */
1830 const char *prefix;
1831 char prec[11]; /* up to ten digits for an unsigned */
1833 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
1834 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
1835 write_char ('u'); /* "vendor extended builtin type" */
1836 write_unsigned_number (strlen (prefix) + strlen (prec));
1837 write_string (prefix);
1838 write_string (prec);
1842 break;
1844 case REAL_TYPE:
1845 if (type == float_type_node
1846 || type == java_float_type_node)
1847 write_char ('f');
1848 else if (type == double_type_node
1849 || type == java_double_type_node)
1850 write_char ('d');
1851 else if (type == long_double_type_node)
1852 write_char ('e');
1853 else
1854 gcc_unreachable ();
1855 break;
1857 case FIXED_POINT_TYPE:
1858 write_string ("DF");
1859 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
1860 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
1861 if (type == fract_type_node
1862 || type == sat_fract_type_node
1863 || type == accum_type_node
1864 || type == sat_accum_type_node)
1865 write_char ('i');
1866 else if (type == unsigned_fract_type_node
1867 || type == sat_unsigned_fract_type_node
1868 || type == unsigned_accum_type_node
1869 || type == sat_unsigned_accum_type_node)
1870 write_char ('j');
1871 else if (type == short_fract_type_node
1872 || type == sat_short_fract_type_node
1873 || type == short_accum_type_node
1874 || type == sat_short_accum_type_node)
1875 write_char ('s');
1876 else if (type == unsigned_short_fract_type_node
1877 || type == sat_unsigned_short_fract_type_node
1878 || type == unsigned_short_accum_type_node
1879 || type == sat_unsigned_short_accum_type_node)
1880 write_char ('t');
1881 else if (type == long_fract_type_node
1882 || type == sat_long_fract_type_node
1883 || type == long_accum_type_node
1884 || type == sat_long_accum_type_node)
1885 write_char ('l');
1886 else if (type == unsigned_long_fract_type_node
1887 || type == sat_unsigned_long_fract_type_node
1888 || type == unsigned_long_accum_type_node
1889 || type == sat_unsigned_long_accum_type_node)
1890 write_char ('m');
1891 else if (type == long_long_fract_type_node
1892 || type == sat_long_long_fract_type_node
1893 || type == long_long_accum_type_node
1894 || type == sat_long_long_accum_type_node)
1895 write_char ('x');
1896 else if (type == unsigned_long_long_fract_type_node
1897 || type == sat_unsigned_long_long_fract_type_node
1898 || type == unsigned_long_long_accum_type_node
1899 || type == sat_unsigned_long_long_accum_type_node)
1900 write_char ('y');
1901 else
1902 sorry ("mangling unknown fixed point type");
1903 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
1904 if (TYPE_SATURATING (type))
1905 write_char ('s');
1906 else
1907 write_char ('n');
1908 break;
1910 default:
1911 gcc_unreachable ();
1915 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1916 METHOD_TYPE. The return type is mangled before the parameter
1917 types.
1919 <function-type> ::= F [Y] <bare-function-type> E */
1921 static void
1922 write_function_type (const tree type)
1924 MANGLE_TRACE_TREE ("function-type", type);
1926 /* For a pointer to member function, the function type may have
1927 cv-qualifiers, indicating the quals for the artificial 'this'
1928 parameter. */
1929 if (TREE_CODE (type) == METHOD_TYPE)
1931 /* The first parameter must be a POINTER_TYPE pointing to the
1932 `this' parameter. */
1933 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1934 write_CV_qualifiers_for_type (this_type);
1937 write_char ('F');
1938 /* We don't track whether or not a type is `extern "C"'. Note that
1939 you can have an `extern "C"' function that does not have
1940 `extern "C"' type, and vice versa:
1942 extern "C" typedef void function_t();
1943 function_t f; // f has C++ linkage, but its type is
1944 // `extern "C"'
1946 typedef void function_t();
1947 extern "C" function_t f; // Vice versa.
1949 See [dcl.link]. */
1950 write_bare_function_type (type, /*include_return_type_p=*/1,
1951 /*decl=*/NULL);
1952 write_char ('E');
1955 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1956 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
1957 is mangled before the parameter types. If non-NULL, DECL is
1958 FUNCTION_DECL for the function whose type is being emitted.
1960 If DECL is a member of a Java type, then a literal 'J'
1961 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
1962 were nonzero.
1964 <bare-function-type> ::= [J]</signature/ type>+ */
1966 static void
1967 write_bare_function_type (const tree type, const int include_return_type_p,
1968 const tree decl)
1970 int java_method_p;
1972 MANGLE_TRACE_TREE ("bare-function-type", type);
1974 /* Detect Java methods and emit special encoding. */
1975 if (decl != NULL
1976 && DECL_FUNCTION_MEMBER_P (decl)
1977 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
1978 && !DECL_CONSTRUCTOR_P (decl)
1979 && !DECL_DESTRUCTOR_P (decl)
1980 && !DECL_CONV_FN_P (decl))
1982 java_method_p = 1;
1983 write_char ('J');
1985 else
1987 java_method_p = 0;
1990 /* Mangle the return type, if requested. */
1991 if (include_return_type_p || java_method_p)
1992 write_type (TREE_TYPE (type));
1994 /* Now mangle the types of the arguments. */
1995 write_method_parms (TYPE_ARG_TYPES (type),
1996 TREE_CODE (type) == METHOD_TYPE,
1997 decl);
2000 /* Write the mangled representation of a method parameter list of
2001 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2002 considered a non-static method, and the this parameter is omitted.
2003 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2004 parameters are being emitted. */
2006 static void
2007 write_method_parms (tree parm_types, const int method_p, const tree decl)
2009 tree first_parm_type;
2010 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2012 /* Assume this parameter type list is variable-length. If it ends
2013 with a void type, then it's not. */
2014 int varargs_p = 1;
2016 /* If this is a member function, skip the first arg, which is the
2017 this pointer.
2018 "Member functions do not encode the type of their implicit this
2019 parameter."
2021 Similarly, there's no need to mangle artificial parameters, like
2022 the VTT parameters for constructors and destructors. */
2023 if (method_p)
2025 parm_types = TREE_CHAIN (parm_types);
2026 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
2028 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2030 parm_types = TREE_CHAIN (parm_types);
2031 parm_decl = TREE_CHAIN (parm_decl);
2035 for (first_parm_type = parm_types;
2036 parm_types;
2037 parm_types = TREE_CHAIN (parm_types))
2039 tree parm = TREE_VALUE (parm_types);
2040 if (parm == void_type_node)
2042 /* "Empty parameter lists, whether declared as () or
2043 conventionally as (void), are encoded with a void parameter
2044 (v)." */
2045 if (parm_types == first_parm_type)
2046 write_type (parm);
2047 /* If the parm list is terminated with a void type, it's
2048 fixed-length. */
2049 varargs_p = 0;
2050 /* A void type better be the last one. */
2051 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2053 else
2054 write_type (parm);
2057 if (varargs_p)
2058 /* <builtin-type> ::= z # ellipsis */
2059 write_char ('z');
2062 /* <class-enum-type> ::= <name> */
2064 static void
2065 write_class_enum_type (const tree type)
2067 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2070 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2071 arguments.
2073 <template-args> ::= I <template-arg>+ E */
2075 static void
2076 write_template_args (tree args)
2078 int i;
2079 int length = TREE_VEC_LENGTH (args);
2081 MANGLE_TRACE_TREE ("template-args", args);
2083 write_char ('I');
2085 gcc_assert (length > 0);
2087 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2089 /* We have nested template args. We want the innermost template
2090 argument list. */
2091 args = TREE_VEC_ELT (args, length - 1);
2092 length = TREE_VEC_LENGTH (args);
2094 for (i = 0; i < length; ++i)
2095 write_template_arg (TREE_VEC_ELT (args, i));
2097 write_char ('E');
2100 /* Write out the
2101 <unqualified-name>
2102 <unqualified-name> <template-args>
2103 part of SCOPE_REF or COMPONENT_REF mangling. */
2105 static void
2106 write_member_name (tree member)
2108 if (TREE_CODE (member) == IDENTIFIER_NODE)
2109 write_source_name (member);
2110 else if (DECL_P (member))
2112 /* G++ 3.2 incorrectly put out both the "sr" code and
2113 the nested name of the qualified name. */
2114 G.need_abi_warning = 1;
2115 write_unqualified_name (member);
2117 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2119 tree name = TREE_OPERAND (member, 0);
2120 if (TREE_CODE (name) == OVERLOAD)
2121 name = OVL_FUNCTION (name);
2122 write_member_name (name);
2123 write_template_args (TREE_OPERAND (member, 1));
2125 else
2126 write_expression (member);
2129 /* <expression> ::= <unary operator-name> <expression>
2130 ::= <binary operator-name> <expression> <expression>
2131 ::= <expr-primary>
2133 <expr-primary> ::= <template-param>
2134 ::= L <type> <value number> E # literal
2135 ::= L <mangled-name> E # external name
2136 ::= st <type> # sizeof
2137 ::= sr <type> <unqualified-name> # dependent name
2138 ::= sr <type> <unqualified-name> <template-args> */
2140 static void
2141 write_expression (tree expr)
2143 enum tree_code code = TREE_CODE (expr);
2145 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2146 is converted (via qualification conversions) to another
2147 type. */
2148 while (TREE_CODE (expr) == NOP_EXPR
2149 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2151 expr = TREE_OPERAND (expr, 0);
2152 code = TREE_CODE (expr);
2155 if (code == BASELINK)
2157 expr = BASELINK_FUNCTIONS (expr);
2158 code = TREE_CODE (expr);
2161 /* Handle pointers-to-members by making them look like expression
2162 nodes. */
2163 if (code == PTRMEM_CST)
2165 expr = build_nt (ADDR_EXPR,
2166 build_qualified_name (/*type=*/NULL_TREE,
2167 PTRMEM_CST_CLASS (expr),
2168 PTRMEM_CST_MEMBER (expr),
2169 /*template_p=*/false));
2170 code = TREE_CODE (expr);
2173 /* Handle template parameters. */
2174 if (code == TEMPLATE_TYPE_PARM
2175 || code == TEMPLATE_TEMPLATE_PARM
2176 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2177 || code == TEMPLATE_PARM_INDEX)
2178 write_template_param (expr);
2179 /* Handle literals. */
2180 else if (TREE_CODE_CLASS (code) == tcc_constant
2181 || (abi_version_at_least (2) && code == CONST_DECL))
2182 write_template_arg_literal (expr);
2183 else if (code == PARM_DECL)
2185 /* A function parameter used in a late-specified return type. */
2186 int index = parm_index (expr);
2187 write_string ("fp");
2188 if (index > 1)
2189 write_unsigned_number (index - 2);
2190 write_char ('_');
2192 else if (DECL_P (expr))
2194 /* G++ 3.2 incorrectly mangled non-type template arguments of
2195 enumeration type using their names. */
2196 if (code == CONST_DECL)
2197 G.need_abi_warning = 1;
2198 write_char ('L');
2199 write_mangled_name (expr, false);
2200 write_char ('E');
2202 else if (TREE_CODE (expr) == SIZEOF_EXPR
2203 && TYPE_P (TREE_OPERAND (expr, 0)))
2205 write_string ("st");
2206 write_type (TREE_OPERAND (expr, 0));
2208 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2209 && TYPE_P (TREE_OPERAND (expr, 0)))
2211 write_string ("at");
2212 write_type (TREE_OPERAND (expr, 0));
2214 else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
2216 tree scope = TREE_OPERAND (expr, 0);
2217 tree member = TREE_OPERAND (expr, 1);
2219 /* If the MEMBER is a real declaration, then the qualifying
2220 scope was not dependent. Ideally, we would not have a
2221 SCOPE_REF in those cases, but sometimes we do. If the second
2222 argument is a DECL, then the name must not have been
2223 dependent. */
2224 if (DECL_P (member))
2225 write_expression (member);
2226 else
2228 tree template_args;
2230 write_string ("sr");
2231 write_type (scope);
2232 /* If MEMBER is a template-id, separate the template
2233 from the arguments. */
2234 if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2236 template_args = TREE_OPERAND (member, 1);
2237 member = TREE_OPERAND (member, 0);
2239 else
2240 template_args = NULL_TREE;
2241 /* Write out the name of the MEMBER. */
2242 if (IDENTIFIER_TYPENAME_P (member))
2243 write_conversion_operator_name (TREE_TYPE (member));
2244 else if (IDENTIFIER_OPNAME_P (member))
2246 int i;
2247 const char *mangled_name = NULL;
2249 /* Unfortunately, there is no easy way to go from the
2250 name of the operator back to the corresponding tree
2251 code. */
2252 for (i = 0; i < MAX_TREE_CODES; ++i)
2253 if (operator_name_info[i].identifier == member)
2255 /* The ABI says that we prefer binary operator
2256 names to unary operator names. */
2257 if (operator_name_info[i].arity == 2)
2259 mangled_name = operator_name_info[i].mangled_name;
2260 break;
2262 else if (!mangled_name)
2263 mangled_name = operator_name_info[i].mangled_name;
2265 else if (assignment_operator_name_info[i].identifier
2266 == member)
2268 mangled_name
2269 = assignment_operator_name_info[i].mangled_name;
2270 break;
2272 write_string (mangled_name);
2274 else
2275 write_source_name (member);
2276 /* Write out the template arguments. */
2277 if (template_args)
2278 write_template_args (template_args);
2281 else if (TREE_CODE (expr) == INDIRECT_REF
2282 && TREE_TYPE (TREE_OPERAND (expr, 0))
2283 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2285 write_expression (TREE_OPERAND (expr, 0));
2287 else
2289 int i;
2290 const char *name;
2292 /* When we bind a variable or function to a non-type template
2293 argument with reference type, we create an ADDR_EXPR to show
2294 the fact that the entity's address has been taken. But, we
2295 don't actually want to output a mangling code for the `&'. */
2296 if (TREE_CODE (expr) == ADDR_EXPR
2297 && TREE_TYPE (expr)
2298 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2300 expr = TREE_OPERAND (expr, 0);
2301 if (DECL_P (expr))
2303 write_expression (expr);
2304 return;
2307 code = TREE_CODE (expr);
2310 if (code == COMPONENT_REF)
2312 tree ob = TREE_OPERAND (expr, 0);
2314 if (TREE_CODE (ob) == ARROW_EXPR)
2316 write_string (operator_name_info[(int)code].mangled_name);
2317 ob = TREE_OPERAND (ob, 0);
2319 else
2320 write_string ("dt");
2322 write_expression (ob);
2323 write_member_name (TREE_OPERAND (expr, 1));
2324 return;
2327 /* If it wasn't any of those, recursively expand the expression. */
2328 name = operator_name_info[(int) code].mangled_name;
2329 if (name == NULL)
2331 sorry ("mangling %C", code);
2332 return;
2334 else
2335 write_string (name);
2337 switch (code)
2339 case CALL_EXPR:
2341 tree fn = CALL_EXPR_FN (expr);
2343 if (TREE_CODE (fn) == ADDR_EXPR)
2344 fn = TREE_OPERAND (fn, 0);
2346 /* Mangle a dependent name as the name, not whatever happens to
2347 be the first function in the overload set. */
2348 if ((TREE_CODE (fn) == FUNCTION_DECL
2349 || TREE_CODE (fn) == OVERLOAD)
2350 && type_dependent_expression_p_push (expr))
2351 fn = DECL_NAME (get_first_fn (fn));
2353 if (TREE_CODE (fn) == IDENTIFIER_NODE)
2354 write_source_name (fn);
2355 else
2356 write_expression (fn);
2359 for (i = 0; i < call_expr_nargs (expr); ++i)
2360 write_expression (CALL_EXPR_ARG (expr, i));
2361 write_char ('E');
2362 break;
2364 case CAST_EXPR:
2365 write_type (TREE_TYPE (expr));
2366 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2367 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2368 else
2370 tree args = TREE_OPERAND (expr, 0);
2371 write_char ('_');
2372 for (; args; args = TREE_CHAIN (args))
2373 write_expression (TREE_VALUE (args));
2374 write_char ('E');
2376 break;
2378 /* FIXME these should have a distinct mangling. */
2379 case STATIC_CAST_EXPR:
2380 case CONST_CAST_EXPR:
2381 write_type (TREE_TYPE (expr));
2382 write_expression (TREE_OPERAND (expr, 0));
2383 break;
2385 case NEW_EXPR:
2386 sorry ("mangling new-expression");
2387 break;
2389 /* Handle pointers-to-members specially. */
2390 case SCOPE_REF:
2391 write_type (TREE_OPERAND (expr, 0));
2392 write_member_name (TREE_OPERAND (expr, 1));
2393 break;
2395 default:
2396 for (i = 0; i < TREE_OPERAND_LENGTH (expr); ++i)
2398 tree operand = TREE_OPERAND (expr, i);
2399 /* As a GNU extension, the middle operand of a
2400 conditional may be omitted. Since expression
2401 manglings are supposed to represent the input token
2402 stream, there's no good way to mangle such an
2403 expression without extending the C++ ABI. */
2404 if (code == COND_EXPR && i == 1 && !operand)
2406 error ("omitted middle operand to %<?:%> operand "
2407 "cannot be mangled");
2408 continue;
2410 write_expression (operand);
2416 /* Literal subcase of non-terminal <template-arg>.
2418 "Literal arguments, e.g. "A<42L>", are encoded with their type
2419 and value. Negative integer values are preceded with "n"; for
2420 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2421 encoded as 0, true as 1." */
2423 static void
2424 write_template_arg_literal (const tree value)
2426 write_char ('L');
2427 write_type (TREE_TYPE (value));
2429 switch (TREE_CODE (value))
2431 case CONST_DECL:
2432 write_integer_cst (DECL_INITIAL (value));
2433 break;
2435 case INTEGER_CST:
2436 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
2437 || integer_zerop (value) || integer_onep (value));
2438 write_integer_cst (value);
2439 break;
2441 case REAL_CST:
2442 write_real_cst (value);
2443 break;
2445 default:
2446 gcc_unreachable ();
2449 write_char ('E');
2452 /* Non-terminal <template-arg>.
2454 <template-arg> ::= <type> # type
2455 ::= L <type> </value/ number> E # literal
2456 ::= LZ <name> E # external name
2457 ::= X <expression> E # expression */
2459 static void
2460 write_template_arg (tree node)
2462 enum tree_code code = TREE_CODE (node);
2464 MANGLE_TRACE_TREE ("template-arg", node);
2466 /* A template template parameter's argument list contains TREE_LIST
2467 nodes of which the value field is the actual argument. */
2468 if (code == TREE_LIST)
2470 node = TREE_VALUE (node);
2471 /* If it's a decl, deal with its type instead. */
2472 if (DECL_P (node))
2474 node = TREE_TYPE (node);
2475 code = TREE_CODE (node);
2479 if (TREE_CODE (node) == NOP_EXPR
2480 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
2482 /* Template parameters can be of reference type. To maintain
2483 internal consistency, such arguments use a conversion from
2484 address of object to reference type. */
2485 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
2486 if (abi_version_at_least (2))
2487 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
2488 else
2489 G.need_abi_warning = 1;
2492 if (ARGUMENT_PACK_P (node))
2494 /* Expand the template argument pack. */
2495 tree args = ARGUMENT_PACK_ARGS (node);
2496 int i, length = TREE_VEC_LENGTH (args);
2497 write_char ('I');
2498 for (i = 0; i < length; ++i)
2499 write_template_arg (TREE_VEC_ELT (args, i));
2500 write_char ('E');
2502 else if (TYPE_P (node))
2503 write_type (node);
2504 else if (code == TEMPLATE_DECL)
2505 /* A template appearing as a template arg is a template template arg. */
2506 write_template_template_arg (node);
2507 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
2508 || (abi_version_at_least (2) && code == CONST_DECL))
2509 write_template_arg_literal (node);
2510 else if (DECL_P (node))
2512 /* Until ABI version 2, non-type template arguments of
2513 enumeration type were mangled using their names. */
2514 if (code == CONST_DECL && !abi_version_at_least (2))
2515 G.need_abi_warning = 1;
2516 write_char ('L');
2517 /* Until ABI version 3, the underscore before the mangled name
2518 was incorrectly omitted. */
2519 if (!abi_version_at_least (3))
2521 G.need_abi_warning = 1;
2522 write_char ('Z');
2524 else
2525 write_string ("_Z");
2526 write_encoding (node);
2527 write_char ('E');
2529 else
2531 /* Template arguments may be expressions. */
2532 write_char ('X');
2533 write_expression (node);
2534 write_char ('E');
2538 /* <template-template-arg>
2539 ::= <name>
2540 ::= <substitution> */
2542 static void
2543 write_template_template_arg (const tree decl)
2545 MANGLE_TRACE_TREE ("template-template-arg", decl);
2547 if (find_substitution (decl))
2548 return;
2549 write_name (decl, /*ignore_local_scope=*/0);
2550 add_substitution (decl);
2554 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
2556 <array-type> ::= A [</dimension/ number>] _ </element/ type>
2557 ::= A <expression> _ </element/ type>
2559 "Array types encode the dimension (number of elements) and the
2560 element type. For variable length arrays, the dimension (but not
2561 the '_' separator) is omitted." */
2563 static void
2564 write_array_type (const tree type)
2566 write_char ('A');
2567 if (TYPE_DOMAIN (type))
2569 tree index_type;
2570 tree max;
2572 index_type = TYPE_DOMAIN (type);
2573 /* The INDEX_TYPE gives the upper and lower bounds of the
2574 array. */
2575 max = TYPE_MAX_VALUE (index_type);
2576 if (TREE_CODE (max) == INTEGER_CST)
2578 /* The ABI specifies that we should mangle the number of
2579 elements in the array, not the largest allowed index. */
2580 max = size_binop (PLUS_EXPR, max, size_one_node);
2581 write_unsigned_number (tree_low_cst (max, 1));
2583 else
2585 max = TREE_OPERAND (max, 0);
2586 if (!abi_version_at_least (2))
2588 /* value_dependent_expression_p presumes nothing is
2589 dependent when PROCESSING_TEMPLATE_DECL is zero. */
2590 ++processing_template_decl;
2591 if (!value_dependent_expression_p (max))
2592 G.need_abi_warning = 1;
2593 --processing_template_decl;
2595 write_expression (max);
2599 write_char ('_');
2600 write_type (TREE_TYPE (type));
2603 /* Non-terminal <pointer-to-member-type> for pointer-to-member
2604 variables. TYPE is a pointer-to-member POINTER_TYPE.
2606 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2608 static void
2609 write_pointer_to_member_type (const tree type)
2611 write_char ('M');
2612 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
2613 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2616 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
2617 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2618 TEMPLATE_PARM_INDEX.
2620 <template-param> ::= T </parameter/ number> _ */
2622 static void
2623 write_template_param (const tree parm)
2625 int parm_index;
2626 int parm_level;
2627 tree parm_type = NULL_TREE;
2629 MANGLE_TRACE_TREE ("template-parm", parm);
2631 switch (TREE_CODE (parm))
2633 case TEMPLATE_TYPE_PARM:
2634 case TEMPLATE_TEMPLATE_PARM:
2635 case BOUND_TEMPLATE_TEMPLATE_PARM:
2636 parm_index = TEMPLATE_TYPE_IDX (parm);
2637 parm_level = TEMPLATE_TYPE_LEVEL (parm);
2638 break;
2640 case TEMPLATE_PARM_INDEX:
2641 parm_index = TEMPLATE_PARM_IDX (parm);
2642 parm_level = TEMPLATE_PARM_LEVEL (parm);
2643 parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
2644 break;
2646 default:
2647 gcc_unreachable ();
2650 write_char ('T');
2651 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2652 earliest template param denoted by `_'. */
2653 if (parm_index > 0)
2654 write_unsigned_number (parm_index - 1);
2655 write_char ('_');
2658 /* <template-template-param>
2659 ::= <template-param>
2660 ::= <substitution> */
2662 static void
2663 write_template_template_param (const tree parm)
2665 tree templ = NULL_TREE;
2667 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2668 template template parameter. The substitution candidate here is
2669 only the template. */
2670 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2672 templ
2673 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2674 if (find_substitution (templ))
2675 return;
2678 /* <template-param> encodes only the template parameter position,
2679 not its template arguments, which is fine here. */
2680 write_template_param (parm);
2681 if (templ)
2682 add_substitution (templ);
2685 /* Non-terminal <substitution>.
2687 <substitution> ::= S <seq-id> _
2688 ::= S_ */
2690 static void
2691 write_substitution (const int seq_id)
2693 MANGLE_TRACE ("substitution", "");
2695 write_char ('S');
2696 if (seq_id > 0)
2697 write_number (seq_id - 1, /*unsigned=*/1, 36);
2698 write_char ('_');
2701 /* Start mangling ENTITY. */
2703 static inline void
2704 start_mangling (const tree entity)
2706 G.entity = entity;
2707 G.need_abi_warning = false;
2708 obstack_free (&name_obstack, name_base);
2709 mangle_obstack = &name_obstack;
2710 name_base = obstack_alloc (&name_obstack, 0);
2713 /* Done with mangling. If WARN is true, and the name of G.entity will
2714 be mangled differently in a future version of the ABI, issue a
2715 warning. */
2717 static void
2718 finish_mangling_internal (const bool warn)
2720 if (warn_abi && warn && G.need_abi_warning)
2721 warning (OPT_Wabi, "the mangled name of %qD will change in a future "
2722 "version of GCC",
2723 G.entity);
2725 /* Clear all the substitutions. */
2726 VEC_truncate (tree, G.substitutions, 0);
2728 /* Null-terminate the string. */
2729 write_char ('\0');
2733 /* Like finish_mangling_internal, but return the mangled string. */
2735 static inline const char *
2736 finish_mangling (const bool warn)
2738 finish_mangling_internal (warn);
2739 return (const char *) obstack_finish (mangle_obstack);
2742 /* Like finish_mangling_internal, but return an identifier. */
2744 static tree
2745 finish_mangling_get_identifier (const bool warn)
2747 finish_mangling_internal (warn);
2748 /* Don't obstack_finish here, and the next start_mangling will
2749 remove the identifier. */
2750 return get_identifier ((const char *) name_base);
2753 /* Initialize data structures for mangling. */
2755 void
2756 init_mangle (void)
2758 gcc_obstack_init (&name_obstack);
2759 name_base = obstack_alloc (&name_obstack, 0);
2760 G.substitutions = NULL;
2762 /* Cache these identifiers for quick comparison when checking for
2763 standard substitutions. */
2764 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2765 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2766 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2767 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2768 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2769 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2772 /* Generate the mangled name of DECL. */
2774 static tree
2775 mangle_decl_string (const tree decl)
2777 tree result;
2779 start_mangling (decl);
2781 if (TREE_CODE (decl) == TYPE_DECL)
2782 write_type (TREE_TYPE (decl));
2783 else
2784 write_mangled_name (decl, true);
2786 result = finish_mangling_get_identifier (/*warn=*/true);
2787 if (DEBUG_MANGLE)
2788 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
2789 IDENTIFIER_POINTER (result));
2790 return result;
2793 /* Create an identifier for the external mangled name of DECL. */
2795 void
2796 mangle_decl (const tree decl)
2798 tree id = mangle_decl_string (decl);
2799 id = targetm.mangle_decl_assembler_name (decl, id);
2800 SET_DECL_ASSEMBLER_NAME (decl, id);
2803 /* Generate the mangled representation of TYPE. */
2805 const char *
2806 mangle_type_string (const tree type)
2808 const char *result;
2810 start_mangling (type);
2811 write_type (type);
2812 result = finish_mangling (/*warn=*/false);
2813 if (DEBUG_MANGLE)
2814 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2815 return result;
2818 /* Create an identifier for the mangled name of a special component
2819 for belonging to TYPE. CODE is the ABI-specified code for this
2820 component. */
2822 static tree
2823 mangle_special_for_type (const tree type, const char *code)
2825 tree result;
2827 /* We don't have an actual decl here for the special component, so
2828 we can't just process the <encoded-name>. Instead, fake it. */
2829 start_mangling (type);
2831 /* Start the mangling. */
2832 write_string ("_Z");
2833 write_string (code);
2835 /* Add the type. */
2836 write_type (type);
2837 result = finish_mangling_get_identifier (/*warn=*/false);
2839 if (DEBUG_MANGLE)
2840 fprintf (stderr, "mangle_special_for_type = %s\n\n",
2841 IDENTIFIER_POINTER (result));
2843 return result;
2846 /* Create an identifier for the mangled representation of the typeinfo
2847 structure for TYPE. */
2849 tree
2850 mangle_typeinfo_for_type (const tree type)
2852 return mangle_special_for_type (type, "TI");
2855 /* Create an identifier for the mangled name of the NTBS containing
2856 the mangled name of TYPE. */
2858 tree
2859 mangle_typeinfo_string_for_type (const tree type)
2861 return mangle_special_for_type (type, "TS");
2864 /* Create an identifier for the mangled name of the vtable for TYPE. */
2866 tree
2867 mangle_vtbl_for_type (const tree type)
2869 return mangle_special_for_type (type, "TV");
2872 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2874 tree
2875 mangle_vtt_for_type (const tree type)
2877 return mangle_special_for_type (type, "TT");
2880 /* Return an identifier for a construction vtable group. TYPE is
2881 the most derived class in the hierarchy; BINFO is the base
2882 subobject for which this construction vtable group will be used.
2884 This mangling isn't part of the ABI specification; in the ABI
2885 specification, the vtable group is dumped in the same COMDAT as the
2886 main vtable, and is referenced only from that vtable, so it doesn't
2887 need an external name. For binary formats without COMDAT sections,
2888 though, we need external names for the vtable groups.
2890 We use the production
2892 <special-name> ::= CT <type> <offset number> _ <base type> */
2894 tree
2895 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
2897 tree result;
2899 start_mangling (type);
2901 write_string ("_Z");
2902 write_string ("TC");
2903 write_type (type);
2904 write_integer_cst (BINFO_OFFSET (binfo));
2905 write_char ('_');
2906 write_type (BINFO_TYPE (binfo));
2908 result = finish_mangling_get_identifier (/*warn=*/false);
2909 if (DEBUG_MANGLE)
2910 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
2911 IDENTIFIER_POINTER (result));
2912 return result;
2915 /* Mangle a this pointer or result pointer adjustment.
2917 <call-offset> ::= h <fixed offset number> _
2918 ::= v <fixed offset number> _ <virtual offset number> _ */
2920 static void
2921 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
2923 write_char (virtual_offset ? 'v' : 'h');
2925 /* For either flavor, write the fixed offset. */
2926 write_integer_cst (fixed_offset);
2927 write_char ('_');
2929 /* For a virtual thunk, add the virtual offset. */
2930 if (virtual_offset)
2932 write_integer_cst (virtual_offset);
2933 write_char ('_');
2937 /* Return an identifier for the mangled name of a this-adjusting or
2938 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
2939 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
2940 is a virtual thunk, and it is the vtbl offset in
2941 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
2942 zero for a covariant thunk. Note, that FN_DECL might be a covariant
2943 thunk itself. A covariant thunk name always includes the adjustment
2944 for the this pointer, even if there is none.
2946 <special-name> ::= T <call-offset> <base encoding>
2947 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2948 <base encoding> */
2950 tree
2951 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
2952 tree virtual_offset)
2954 tree result;
2956 start_mangling (fn_decl);
2958 write_string ("_Z");
2959 write_char ('T');
2961 if (!this_adjusting)
2963 /* Covariant thunk with no this adjustment */
2964 write_char ('c');
2965 mangle_call_offset (integer_zero_node, NULL_TREE);
2966 mangle_call_offset (fixed_offset, virtual_offset);
2968 else if (!DECL_THUNK_P (fn_decl))
2969 /* Plain this adjusting thunk. */
2970 mangle_call_offset (fixed_offset, virtual_offset);
2971 else
2973 /* This adjusting thunk to covariant thunk. */
2974 write_char ('c');
2975 mangle_call_offset (fixed_offset, virtual_offset);
2976 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2977 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2978 if (virtual_offset)
2979 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2980 mangle_call_offset (fixed_offset, virtual_offset);
2981 fn_decl = THUNK_TARGET (fn_decl);
2984 /* Scoped name. */
2985 write_encoding (fn_decl);
2987 result = finish_mangling_get_identifier (/*warn=*/false);
2988 if (DEBUG_MANGLE)
2989 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
2990 return result;
2993 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
2994 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
2995 TYPE. */
2997 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
2999 /* Hash a node (VAL1) in the table. */
3001 static hashval_t
3002 hash_type (const void *val)
3004 return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
3007 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3009 static int
3010 compare_type (const void *val1, const void *val2)
3012 return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
3015 /* Return an identifier for the mangled unqualified name for a
3016 conversion operator to TYPE. This mangling is not specified by the
3017 ABI spec; it is only used internally. */
3019 tree
3020 mangle_conv_op_name_for_type (const tree type)
3022 void **slot;
3023 tree identifier;
3025 if (type == error_mark_node)
3026 return error_mark_node;
3028 if (conv_type_names == NULL)
3029 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
3031 slot = htab_find_slot_with_hash (conv_type_names, type,
3032 (hashval_t) TYPE_UID (type), INSERT);
3033 identifier = (tree)*slot;
3034 if (!identifier)
3036 char buffer[64];
3038 /* Create a unique name corresponding to TYPE. */
3039 sprintf (buffer, "operator %lu",
3040 (unsigned long) htab_elements (conv_type_names));
3041 identifier = get_identifier (buffer);
3042 *slot = identifier;
3044 /* Hang TYPE off the identifier so it can be found easily later
3045 when performing conversions. */
3046 TREE_TYPE (identifier) = type;
3048 /* Set bits on the identifier so we know later it's a conversion. */
3049 IDENTIFIER_OPNAME_P (identifier) = 1;
3050 IDENTIFIER_TYPENAME_P (identifier) = 1;
3053 return identifier;
3056 /* Return an identifier for the name of an initialization guard
3057 variable for indicated VARIABLE. */
3059 tree
3060 mangle_guard_variable (const tree variable)
3062 start_mangling (variable);
3063 write_string ("_ZGV");
3064 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3065 /* The name of a guard variable for a reference temporary should refer
3066 to the reference, not the temporary. */
3067 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3068 else
3069 write_name (variable, /*ignore_local_scope=*/0);
3070 return finish_mangling_get_identifier (/*warn=*/false);
3073 /* Return an identifier for the name of a temporary variable used to
3074 initialize a static reference. This isn't part of the ABI, but we might
3075 as well call them something readable. */
3077 tree
3078 mangle_ref_init_variable (const tree variable)
3080 start_mangling (variable);
3081 write_string ("_ZGR");
3082 write_name (variable, /*ignore_local_scope=*/0);
3083 return finish_mangling_get_identifier (/*warn=*/false);
3087 /* Foreign language type mangling section. */
3089 /* How to write the type codes for the integer Java type. */
3091 static void
3092 write_java_integer_type_codes (const tree type)
3094 if (type == java_int_type_node)
3095 write_char ('i');
3096 else if (type == java_short_type_node)
3097 write_char ('s');
3098 else if (type == java_byte_type_node)
3099 write_char ('c');
3100 else if (type == java_char_type_node)
3101 write_char ('w');
3102 else if (type == java_long_type_node)
3103 write_char ('x');
3104 else if (type == java_boolean_type_node)
3105 write_char ('b');
3106 else
3107 gcc_unreachable ();
3110 #include "gt-cp-mangle.h"