* friend.c, lex.c, mangle.c, repo.c: Update copyright.
[official-gcc.git] / gcc / cp / mangle.c
blob2dd3e93bfc325f1cfac79f6798066abb6c25ca33
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Written by Alex Samuel <sameul@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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* This file implements mangling of C++ names according to the IA64
24 C++ ABI specification. A mangled name encodes a function or
25 variable's name, scope, type, and/or template arguments into a text
26 identifier. This identifier is used as the function's or
27 variable's linkage name, to preserve compatibility between C++'s
28 language features (templates, scoping, and overloading) and C
29 linkers.
31 Additionally, g++ uses mangled names internally. To support this,
32 mangling of types is allowed, even though the mangled name of a
33 type should not appear by itself as an exported name. Ditto for
34 uninstantiated templates.
36 The primary entry point for this module is mangle_decl, which
37 returns an identifier containing the mangled name for a decl.
38 Additional entry points are provided to build mangled names of
39 particular constructs when the appropriate decl for that construct
40 is not available. These are:
42 mangle_typeinfo_for_type: typeinfo data
43 mangle_typeinfo_string_for_type: typeinfo type name
44 mangle_vtbl_for_type: virtual table data
45 mangle_vtt_for_type: VTT data
46 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
47 mangle_thunk: thunk function or entry
51 #include "config.h"
52 #include "system.h"
53 #include "coretypes.h"
54 #include "tm.h"
55 #include "tree.h"
56 #include "tm_p.h"
57 #include "cp-tree.h"
58 #include "real.h"
59 #include "obstack.h"
60 #include "toplev.h"
61 #include "varray.h"
62 #include "flags.h"
63 #include "target.h"
65 /* Debugging support. */
67 /* Define DEBUG_MANGLE to enable very verbose trace messages. */
68 #ifndef DEBUG_MANGLE
69 #define DEBUG_MANGLE 0
70 #endif
72 /* Macros for tracing the write_* functions. */
73 #if DEBUG_MANGLE
74 # define MANGLE_TRACE(FN, INPUT) \
75 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
76 # define MANGLE_TRACE_TREE(FN, NODE) \
77 fprintf (stderr, " %-24s: %-24s (%p)\n", \
78 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
79 #else
80 # define MANGLE_TRACE(FN, INPUT)
81 # define MANGLE_TRACE_TREE(FN, NODE)
82 #endif
84 /* Nonzero if NODE is a class template-id. We can't rely on
85 CLASSTYPE_USE_TEMPLATE here because of tricky bugs in the parser
86 that hard to distinguish A<T> from A, where A<T> is the type as
87 instantiated outside of the template, and A is the type used
88 without parameters inside the template. */
89 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
90 (TYPE_LANG_SPECIFIC (NODE) != NULL \
91 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
92 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
93 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
95 /* Things we only need one of. This module is not reentrant. */
96 typedef struct globals GTY(())
98 /* An array of the current substitution candidates, in the order
99 we've seen them. */
100 varray_type substitutions;
102 /* The entity that is being mangled. */
103 tree GTY ((skip)) entity;
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 /* An incomplete mangled name. There will be no NUL terminator. If
124 there is no incomplete mangled name, this variable is NULL. */
125 static char *partially_mangled_name;
127 /* The number of characters in the PARTIALLY_MANGLED_NAME. */
128 static size_t partially_mangled_name_len;
130 /* Indices into subst_identifiers. These are identifiers used in
131 special substitution rules. */
132 typedef enum
134 SUBID_ALLOCATOR,
135 SUBID_BASIC_STRING,
136 SUBID_CHAR_TRAITS,
137 SUBID_BASIC_ISTREAM,
138 SUBID_BASIC_OSTREAM,
139 SUBID_BASIC_IOSTREAM,
140 SUBID_MAX
142 substitution_identifier_index_t;
144 /* For quick substitution checks, look up these common identifiers
145 once only. */
146 static GTY(()) tree subst_identifiers[SUBID_MAX];
148 /* Single-letter codes for builtin integer types, defined in
149 <builtin-type>. These are indexed by integer_type_kind values. */
150 static const char
151 integer_type_codes[itk_none] =
153 'c', /* itk_char */
154 'a', /* itk_signed_char */
155 'h', /* itk_unsigned_char */
156 's', /* itk_short */
157 't', /* itk_unsigned_short */
158 'i', /* itk_int */
159 'j', /* itk_unsigned_int */
160 'l', /* itk_long */
161 'm', /* itk_unsigned_long */
162 'x', /* itk_long_long */
163 'y' /* itk_unsigned_long_long */
166 static int decl_is_template_id (const tree, tree* const);
168 /* Functions for handling substitutions. */
170 static inline tree canonicalize_for_substitution (tree);
171 static void add_substitution (tree);
172 static inline int is_std_substitution (const tree,
173 const substitution_identifier_index_t);
174 static inline int is_std_substitution_char (const tree,
175 const substitution_identifier_index_t);
176 static int find_substitution (tree);
177 static void mangle_call_offset (const tree, const tree);
179 /* Functions for emitting mangled representations of things. */
181 static void write_mangled_name (const tree, bool);
182 static void write_encoding (const tree);
183 static void write_name (tree, const int);
184 static void write_unscoped_name (const tree);
185 static void write_unscoped_template_name (const tree);
186 static void write_nested_name (const tree);
187 static void write_prefix (const tree);
188 static void write_template_prefix (const tree);
189 static void write_unqualified_name (const tree);
190 static void write_conversion_operator_name (const tree);
191 static void write_source_name (tree);
192 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
193 const unsigned int);
194 static void write_number (unsigned HOST_WIDE_INT, const int,
195 const unsigned int);
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 (const tree, const tree, const tree);
222 static void dump_substitution_candidates (void);
223 static const char *mangle_decl_string (const tree);
225 /* Control functions. */
227 static inline void start_mangling (const tree, bool);
228 static inline const char *finish_mangling (const bool);
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 /* Save the current (incomplete) mangled name and release the obstack
266 storage holding it. This function should be used during mangling
267 when making a call that could result in a call to get_identifier,
268 as such a call will clobber the same obstack being used for
269 mangling. This function may not be called twice without an
270 intervening call to restore_partially_mangled_name. */
272 static void
273 save_partially_mangled_name (void)
275 if (mangle_obstack == &ident_hash->stack)
277 gcc_assert (!partially_mangled_name);
278 partially_mangled_name_len = obstack_object_size (mangle_obstack);
279 partially_mangled_name = xmalloc (partially_mangled_name_len);
280 memcpy (partially_mangled_name, obstack_base (mangle_obstack),
281 partially_mangled_name_len);
282 obstack_free (mangle_obstack, obstack_finish (mangle_obstack));
286 /* Restore the incomplete mangled name saved with
287 save_partially_mangled_name. */
289 static void
290 restore_partially_mangled_name (void)
292 if (partially_mangled_name)
294 obstack_grow (mangle_obstack, partially_mangled_name,
295 partially_mangled_name_len);
296 free (partially_mangled_name);
297 partially_mangled_name = NULL;
301 /* If DECL is a template instance, return nonzero and, if
302 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
303 Otherwise return zero. */
305 static int
306 decl_is_template_id (const tree decl, tree* const template_info)
308 if (TREE_CODE (decl) == TYPE_DECL)
310 /* TYPE_DECLs are handled specially. Look at its type to decide
311 if this is a template instantiation. */
312 const tree type = TREE_TYPE (decl);
314 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
316 if (template_info != NULL)
317 /* For a templated TYPE_DECL, the template info is hanging
318 off the type. */
319 *template_info = TYPE_TEMPLATE_INFO (type);
320 return 1;
323 else
325 /* Check if this is a primary template. */
326 if (DECL_LANG_SPECIFIC (decl) != NULL
327 && DECL_USE_TEMPLATE (decl)
328 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
329 && TREE_CODE (decl) != TEMPLATE_DECL)
331 if (template_info != NULL)
332 /* For most templated decls, the template info is hanging
333 off the decl. */
334 *template_info = DECL_TEMPLATE_INFO (decl);
335 return 1;
339 /* It's not a template id. */
340 return 0;
343 /* Produce debugging output of current substitution candidates. */
345 static void
346 dump_substitution_candidates (void)
348 unsigned i;
350 fprintf (stderr, " ++ substitutions ");
351 for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
353 tree el = VARRAY_TREE (G.substitutions, i);
354 const char *name = "???";
356 if (i > 0)
357 fprintf (stderr, " ");
358 if (DECL_P (el))
359 name = IDENTIFIER_POINTER (DECL_NAME (el));
360 else if (TREE_CODE (el) == TREE_LIST)
361 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
362 else if (TYPE_NAME (el))
363 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
364 fprintf (stderr, " S%d_ = ", i - 1);
365 if (TYPE_P (el) &&
366 (CP_TYPE_RESTRICT_P (el)
367 || CP_TYPE_VOLATILE_P (el)
368 || CP_TYPE_CONST_P (el)))
369 fprintf (stderr, "CV-");
370 fprintf (stderr, "%s (%s at %p)\n",
371 name, tree_code_name[TREE_CODE (el)], (void *) el);
375 /* Both decls and types can be substitution candidates, but sometimes
376 they refer to the same thing. For instance, a TYPE_DECL and
377 RECORD_TYPE for the same class refer to the same thing, and should
378 be treated accordingly in substitutions. This function returns a
379 canonicalized tree node representing NODE that is used when adding
380 and substitution candidates and finding matches. */
382 static inline tree
383 canonicalize_for_substitution (tree node)
385 /* For a TYPE_DECL, use the type instead. */
386 if (TREE_CODE (node) == TYPE_DECL)
387 node = TREE_TYPE (node);
388 if (TYPE_P (node))
389 node = canonical_type_variant (node);
391 return node;
394 /* Add NODE as a substitution candidate. NODE must not already be on
395 the list of candidates. */
397 static void
398 add_substitution (tree node)
400 tree c;
402 if (DEBUG_MANGLE)
403 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
404 tree_code_name[TREE_CODE (node)], (void *) node);
406 /* Get the canonicalized substitution candidate for NODE. */
407 c = canonicalize_for_substitution (node);
408 if (DEBUG_MANGLE && c != node)
409 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
410 tree_code_name[TREE_CODE (node)], (void *) node);
411 node = c;
413 #if ENABLE_CHECKING
414 /* Make sure NODE isn't already a candidate. */
416 int i;
417 for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
419 const tree candidate = VARRAY_TREE (G.substitutions, i);
421 gcc_assert (!(DECL_P (node) && node == candidate));
422 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
423 && same_type_p (node, candidate)));
426 #endif /* ENABLE_CHECKING */
428 /* Put the decl onto the varray of substitution candidates. */
429 VARRAY_PUSH_TREE (G.substitutions, node);
431 if (DEBUG_MANGLE)
432 dump_substitution_candidates ();
435 /* Helper function for find_substitution. Returns nonzero if NODE,
436 which may be a decl or a CLASS_TYPE, is a template-id with template
437 name of substitution_index[INDEX] in the ::std namespace. */
439 static inline int
440 is_std_substitution (const tree node,
441 const substitution_identifier_index_t index)
443 tree type = NULL;
444 tree decl = NULL;
446 if (DECL_P (node))
448 type = TREE_TYPE (node);
449 decl = node;
451 else if (CLASS_TYPE_P (node))
453 type = node;
454 decl = TYPE_NAME (node);
456 else
457 /* These are not the droids you're looking for. */
458 return 0;
460 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
461 && TYPE_LANG_SPECIFIC (type)
462 && TYPE_TEMPLATE_INFO (type)
463 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
464 == subst_identifiers[index]));
467 /* Helper function for find_substitution. Returns nonzero if NODE,
468 which may be a decl or a CLASS_TYPE, is the template-id
469 ::std::identifier<char>, where identifier is
470 substitution_index[INDEX]. */
472 static inline int
473 is_std_substitution_char (const tree node,
474 const substitution_identifier_index_t index)
476 tree args;
477 /* Check NODE's name is ::std::identifier. */
478 if (!is_std_substitution (node, index))
479 return 0;
480 /* Figure out its template args. */
481 if (DECL_P (node))
482 args = DECL_TI_ARGS (node);
483 else if (CLASS_TYPE_P (node))
484 args = CLASSTYPE_TI_ARGS (node);
485 else
486 /* Oops, not a template. */
487 return 0;
488 /* NODE's template arg list should be <char>. */
489 return
490 TREE_VEC_LENGTH (args) == 1
491 && TREE_VEC_ELT (args, 0) == char_type_node;
494 /* Check whether a substitution should be used to represent NODE in
495 the mangling.
497 First, check standard special-case substitutions.
499 <substitution> ::= St
500 # ::std
502 ::= Sa
503 # ::std::allocator
505 ::= Sb
506 # ::std::basic_string
508 ::= Ss
509 # ::std::basic_string<char,
510 ::std::char_traits<char>,
511 ::std::allocator<char> >
513 ::= Si
514 # ::std::basic_istream<char, ::std::char_traits<char> >
516 ::= So
517 # ::std::basic_ostream<char, ::std::char_traits<char> >
519 ::= Sd
520 # ::std::basic_iostream<char, ::std::char_traits<char> >
522 Then examine the stack of currently available substitution
523 candidates for entities appearing earlier in the same mangling
525 If a substitution is found, write its mangled representation and
526 return nonzero. If none is found, just return zero. */
528 static int
529 find_substitution (tree node)
531 int i;
532 const int size = VARRAY_ACTIVE_SIZE (G.substitutions);
533 tree decl;
534 tree type;
536 if (DEBUG_MANGLE)
537 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
538 tree_code_name[TREE_CODE (node)], (void *) node);
540 /* Obtain the canonicalized substitution representation for NODE.
541 This is what we'll compare against. */
542 node = canonicalize_for_substitution (node);
544 /* Check for builtin substitutions. */
546 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
547 type = TYPE_P (node) ? node : TREE_TYPE (node);
549 /* Check for std::allocator. */
550 if (decl
551 && is_std_substitution (decl, SUBID_ALLOCATOR)
552 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
554 write_string ("Sa");
555 return 1;
558 /* Check for std::basic_string. */
559 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
561 if (TYPE_P (node))
563 /* If this is a type (i.e. a fully-qualified template-id),
564 check for
565 std::basic_string <char,
566 std::char_traits<char>,
567 std::allocator<char> > . */
568 if (cp_type_quals (type) == TYPE_UNQUALIFIED
569 && CLASSTYPE_USE_TEMPLATE (type))
571 tree args = CLASSTYPE_TI_ARGS (type);
572 if (TREE_VEC_LENGTH (args) == 3
573 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
574 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
575 SUBID_CHAR_TRAITS)
576 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
577 SUBID_ALLOCATOR))
579 write_string ("Ss");
580 return 1;
584 else
585 /* Substitute for the template name only if this isn't a type. */
587 write_string ("Sb");
588 return 1;
592 /* Check for basic_{i,o,io}stream. */
593 if (TYPE_P (node)
594 && cp_type_quals (type) == TYPE_UNQUALIFIED
595 && CLASS_TYPE_P (type)
596 && CLASSTYPE_USE_TEMPLATE (type)
597 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
599 /* First, check for the template
600 args <char, std::char_traits<char> > . */
601 tree args = CLASSTYPE_TI_ARGS (type);
602 if (TREE_VEC_LENGTH (args) == 2
603 && TYPE_P (TREE_VEC_ELT (args, 0))
604 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
605 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
606 SUBID_CHAR_TRAITS))
608 /* Got them. Is this basic_istream? */
609 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
610 if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
612 write_string ("Si");
613 return 1;
615 /* Or basic_ostream? */
616 else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
618 write_string ("So");
619 return 1;
621 /* Or basic_iostream? */
622 else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
624 write_string ("Sd");
625 return 1;
630 /* Check for namespace std. */
631 if (decl && DECL_NAMESPACE_STD_P (decl))
633 write_string ("St");
634 return 1;
637 /* Now check the list of available substitutions for this mangling
638 operation. */
639 for (i = 0; i < size; ++i)
641 tree candidate = VARRAY_TREE (G.substitutions, i);
642 /* NODE is a matched to a candidate if it's the same decl node or
643 if it's the same type. */
644 if (decl == candidate
645 || (TYPE_P (candidate) && type && TYPE_P (type)
646 && same_type_p (type, candidate))
647 || NESTED_TEMPLATE_MATCH (node, candidate))
649 write_substitution (i);
650 return 1;
654 /* No substitution found. */
655 return 0;
659 /* TOP_LEVEL is true, if this is being called at outermost level of
660 mangling. It should be false when mangling a decl appearing in an
661 expression within some other mangling.
663 <mangled-name> ::= _Z <encoding> */
665 static void
666 write_mangled_name (const tree decl, bool top_level)
668 MANGLE_TRACE_TREE ("mangled-name", decl);
670 if (/* The names of `extern "C"' functions are not mangled. */
671 DECL_EXTERN_C_FUNCTION_P (decl)
672 /* But overloaded operator names *are* mangled. */
673 && !DECL_OVERLOADED_OPERATOR_P (decl))
675 unmangled_name:;
677 if (top_level)
678 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
679 else
681 /* The standard notes: "The <encoding> of an extern "C"
682 function is treated like global-scope data, i.e. as its
683 <source-name> without a type." We cannot write
684 overloaded operators that way though, because it contains
685 characters invalid in assembler. */
686 if (abi_version_at_least (2))
687 write_string ("_Z");
688 else
689 G.need_abi_warning = true;
690 write_source_name (DECL_NAME (decl));
693 else if (TREE_CODE (decl) == VAR_DECL
694 /* The names of global variables aren't mangled. */
695 && (CP_DECL_CONTEXT (decl) == global_namespace
696 /* And neither are `extern "C"' variables. */
697 || DECL_EXTERN_C_P (decl)))
699 if (top_level || abi_version_at_least (2))
700 goto unmangled_name;
701 else
703 G.need_abi_warning = true;
704 goto mangled_name;
707 else
709 mangled_name:;
710 write_string ("_Z");
711 write_encoding (decl);
712 if (DECL_LANG_SPECIFIC (decl)
713 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
714 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
715 /* We need a distinct mangled name for these entities, but
716 we should never actually output it. So, we append some
717 characters the assembler won't like. */
718 write_string (" *INTERNAL* ");
722 /* <encoding> ::= <function name> <bare-function-type>
723 ::= <data name> */
725 static void
726 write_encoding (const tree decl)
728 MANGLE_TRACE_TREE ("encoding", decl);
730 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
732 /* For overloaded operators write just the mangled name
733 without arguments. */
734 if (DECL_OVERLOADED_OPERATOR_P (decl))
735 write_name (decl, /*ignore_local_scope=*/0);
736 else
737 write_source_name (DECL_NAME (decl));
738 return;
741 write_name (decl, /*ignore_local_scope=*/0);
742 if (TREE_CODE (decl) == FUNCTION_DECL)
744 tree fn_type;
745 tree d;
747 if (decl_is_template_id (decl, NULL))
749 save_partially_mangled_name ();
750 fn_type = get_mostly_instantiated_function_type (decl);
751 restore_partially_mangled_name ();
752 /* FN_TYPE will not have parameter types for in-charge or
753 VTT parameters. Therefore, we pass NULL_TREE to
754 write_bare_function_type -- otherwise, it will get
755 confused about which artificial parameters to skip. */
756 d = NULL_TREE;
758 else
760 fn_type = TREE_TYPE (decl);
761 d = decl;
764 write_bare_function_type (fn_type,
765 (!DECL_CONSTRUCTOR_P (decl)
766 && !DECL_DESTRUCTOR_P (decl)
767 && !DECL_CONV_FN_P (decl)
768 && decl_is_template_id (decl, NULL)),
773 /* <name> ::= <unscoped-name>
774 ::= <unscoped-template-name> <template-args>
775 ::= <nested-name>
776 ::= <local-name>
778 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
779 called from <local-name>, which mangles the enclosing scope
780 elsewhere and then uses this function to mangle just the part
781 underneath the function scope. So don't use the <local-name>
782 production, to avoid an infinite recursion. */
784 static void
785 write_name (tree decl, const int ignore_local_scope)
787 tree context;
789 MANGLE_TRACE_TREE ("name", decl);
791 if (TREE_CODE (decl) == TYPE_DECL)
793 /* In case this is a typedef, fish out the corresponding
794 TYPE_DECL for the main variant. */
795 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
796 context = TYPE_CONTEXT (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
798 else
799 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
801 /* A decl in :: or ::std scope is treated specially. The former is
802 mangled using <unscoped-name> or <unscoped-template-name>, the
803 latter with a special substitution. Also, a name that is
804 directly in a local function scope is also mangled with
805 <unscoped-name> rather than a full <nested-name>. */
806 if (context == NULL
807 || context == global_namespace
808 || DECL_NAMESPACE_STD_P (context)
809 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
811 tree template_info;
812 /* Is this a template instance? */
813 if (decl_is_template_id (decl, &template_info))
815 /* Yes: use <unscoped-template-name>. */
816 write_unscoped_template_name (TI_TEMPLATE (template_info));
817 write_template_args (TI_ARGS (template_info));
819 else
820 /* Everything else gets an <unqualified-name>. */
821 write_unscoped_name (decl);
823 else
825 /* Handle local names, unless we asked not to (that is, invoked
826 under <local-name>, to handle only the part of the name under
827 the local scope). */
828 if (!ignore_local_scope)
830 /* Scan up the list of scope context, looking for a
831 function. If we find one, this entity is in local
832 function scope. local_entity tracks context one scope
833 level down, so it will contain the element that's
834 directly in that function's scope, either decl or one of
835 its enclosing scopes. */
836 tree local_entity = decl;
837 while (context != NULL && context != global_namespace)
839 /* Make sure we're always dealing with decls. */
840 if (context != NULL && TYPE_P (context))
841 context = TYPE_NAME (context);
842 /* Is this a function? */
843 if (TREE_CODE (context) == FUNCTION_DECL)
845 /* Yes, we have local scope. Use the <local-name>
846 production for the innermost function scope. */
847 write_local_name (context, local_entity, decl);
848 return;
850 /* Up one scope level. */
851 local_entity = context;
852 context = CP_DECL_CONTEXT (context);
855 /* No local scope found? Fall through to <nested-name>. */
858 /* Other decls get a <nested-name> to encode their scope. */
859 write_nested_name (decl);
863 /* <unscoped-name> ::= <unqualified-name>
864 ::= St <unqualified-name> # ::std:: */
866 static void
867 write_unscoped_name (const tree decl)
869 tree context = CP_DECL_CONTEXT (decl);
871 MANGLE_TRACE_TREE ("unscoped-name", decl);
873 /* Is DECL in ::std? */
874 if (DECL_NAMESPACE_STD_P (context))
876 write_string ("St");
877 write_unqualified_name (decl);
879 else
881 /* If not, it should be either in the global namespace, or directly
882 in a local function scope. */
883 gcc_assert (context == global_namespace
884 || context == NULL
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>] <prefix> <unqualified-name> E
908 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
910 <CV-qualifiers> ::= [r] [V] [K] */
912 static void
913 write_nested_name (const tree decl)
915 tree template_info;
917 MANGLE_TRACE_TREE ("nested-name", decl);
919 write_char ('N');
921 /* Write CV-qualifiers, if this is a member function. */
922 if (TREE_CODE (decl) == FUNCTION_DECL
923 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
925 if (DECL_VOLATILE_MEMFUNC_P (decl))
926 write_char ('V');
927 if (DECL_CONST_MEMFUNC_P (decl))
928 write_char ('K');
931 /* Is this a template instance? */
932 if (decl_is_template_id (decl, &template_info))
934 /* Yes, use <template-prefix>. */
935 write_template_prefix (decl);
936 write_template_args (TI_ARGS (template_info));
938 else
940 /* No, just use <prefix> */
941 write_prefix (DECL_CONTEXT (decl));
942 write_unqualified_name (decl);
944 write_char ('E');
947 /* <prefix> ::= <prefix> <unqualified-name>
948 ::= <template-param>
949 ::= <template-prefix> <template-args>
950 ::= # empty
951 ::= <substitution> */
953 static void
954 write_prefix (const tree node)
956 tree decl;
957 /* Non-NULL if NODE represents a template-id. */
958 tree template_info = NULL;
960 MANGLE_TRACE_TREE ("prefix", node);
962 if (node == NULL
963 || node == global_namespace)
964 return;
966 if (find_substitution (node))
967 return;
969 if (DECL_P (node))
971 /* If this is a function decl, that means we've hit function
972 scope, so this prefix must be for a local name. In this
973 case, we're under the <local-name> production, which encodes
974 the enclosing function scope elsewhere. So don't continue
975 here. */
976 if (TREE_CODE (node) == FUNCTION_DECL)
977 return;
979 decl = node;
980 decl_is_template_id (decl, &template_info);
982 else
984 /* Node is a type. */
985 decl = TYPE_NAME (node);
986 if (CLASSTYPE_TEMPLATE_ID_P (node))
987 template_info = TYPE_TEMPLATE_INFO (node);
990 /* In G++ 3.2, the name of the template parameter was used. */
991 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
992 && !abi_version_at_least (2))
993 G.need_abi_warning = true;
995 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
996 && abi_version_at_least (2))
997 write_template_param (node);
998 else if (template_info != NULL)
999 /* Templated. */
1001 write_template_prefix (decl);
1002 write_template_args (TI_ARGS (template_info));
1004 else
1005 /* Not templated. */
1007 write_prefix (CP_DECL_CONTEXT (decl));
1008 write_unqualified_name (decl);
1011 add_substitution (node);
1014 /* <template-prefix> ::= <prefix> <template component>
1015 ::= <template-param>
1016 ::= <substitution> */
1018 static void
1019 write_template_prefix (const tree node)
1021 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1022 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1023 tree context = CP_DECL_CONTEXT (decl);
1024 tree template_info;
1025 tree template;
1026 tree substitution;
1028 MANGLE_TRACE_TREE ("template-prefix", node);
1030 /* Find the template decl. */
1031 if (decl_is_template_id (decl, &template_info))
1032 template = TI_TEMPLATE (template_info);
1033 else
1035 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1037 template = TYPE_TI_TEMPLATE (type);
1040 /* For a member template, though, the template name for the
1041 innermost name must have all the outer template levels
1042 instantiated. For instance, consider
1044 template<typename T> struct Outer {
1045 template<typename U> struct Inner {};
1048 The template name for `Inner' in `Outer<int>::Inner<float>' is
1049 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1050 levels separately, so there's no TEMPLATE_DECL available for this
1051 (there's only `Outer<T>::Inner<U>').
1053 In order to get the substitutions right, we create a special
1054 TREE_LIST to represent the substitution candidate for a nested
1055 template. The TREE_PURPOSE is the template's context, fully
1056 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1057 template.
1059 So, for the example above, `Outer<int>::Inner' is represented as a
1060 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1061 and whose value is `Outer<T>::Inner<U>'. */
1062 if (TYPE_P (context))
1063 substitution = build_tree_list (context, template);
1064 else
1065 substitution = template;
1067 if (find_substitution (substitution))
1068 return;
1070 /* In G++ 3.2, the name of the template template parameter was used. */
1071 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1072 && !abi_version_at_least (2))
1073 G.need_abi_warning = true;
1075 if (TREE_CODE (TREE_TYPE (template)) == TEMPLATE_TEMPLATE_PARM
1076 && abi_version_at_least (2))
1077 write_template_param (TREE_TYPE (template));
1078 else
1080 write_prefix (context);
1081 write_unqualified_name (decl);
1084 add_substitution (substitution);
1087 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1088 mangled through special entry points.
1090 <unqualified-name> ::= <operator-name>
1091 ::= <special-name>
1092 ::= <source-name> */
1094 static void
1095 write_unqualified_name (const tree decl)
1097 MANGLE_TRACE_TREE ("unqualified-name", decl);
1099 if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_CONSTRUCTOR_P (decl))
1100 write_special_name_constructor (decl);
1101 else if (DECL_LANG_SPECIFIC (decl) != NULL && DECL_DESTRUCTOR_P (decl))
1102 write_special_name_destructor (decl);
1103 else if (DECL_NAME (decl) == NULL_TREE)
1104 write_source_name (DECL_ASSEMBLER_NAME (decl));
1105 else if (DECL_CONV_FN_P (decl))
1107 /* Conversion operator. Handle it right here.
1108 <operator> ::= cv <type> */
1109 tree type;
1110 if (decl_is_template_id (decl, NULL))
1112 tree fn_type;
1113 save_partially_mangled_name ();
1114 fn_type = get_mostly_instantiated_function_type (decl);
1115 restore_partially_mangled_name ();
1116 type = TREE_TYPE (fn_type);
1118 else
1119 type = DECL_CONV_FN_TYPE (decl);
1120 write_conversion_operator_name (type);
1122 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1124 operator_name_info_t *oni;
1125 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1126 oni = assignment_operator_name_info;
1127 else
1128 oni = operator_name_info;
1130 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1132 else
1133 write_source_name (DECL_NAME (decl));
1136 /* Write the unqualified-name for a conversion operator to TYPE. */
1138 static void
1139 write_conversion_operator_name (const tree type)
1141 write_string ("cv");
1142 write_type (type);
1145 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1147 <source-name> ::= </length/ number> <identifier> */
1149 static void
1150 write_source_name (tree identifier)
1152 MANGLE_TRACE_TREE ("source-name", identifier);
1154 /* Never write the whole template-id name including the template
1155 arguments; we only want the template name. */
1156 if (IDENTIFIER_TEMPLATE (identifier))
1157 identifier = IDENTIFIER_TEMPLATE (identifier);
1159 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1160 write_identifier (IDENTIFIER_POINTER (identifier));
1163 /* Convert NUMBER to ascii using base BASE and generating at least
1164 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1165 into which to store the characters. Returns the number of
1166 characters generated (these will be layed out in advance of where
1167 BUFFER points). */
1169 static int
1170 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1171 char *buffer, const unsigned int min_digits)
1173 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1174 unsigned digits = 0;
1176 while (number)
1178 unsigned HOST_WIDE_INT d = number / base;
1180 *--buffer = base_digits[number - d * base];
1181 digits++;
1182 number = d;
1184 while (digits < min_digits)
1186 *--buffer = base_digits[0];
1187 digits++;
1189 return digits;
1192 /* Non-terminal <number>.
1194 <number> ::= [n] </decimal integer/> */
1196 static void
1197 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1198 const unsigned int base)
1200 char buffer[sizeof (HOST_WIDE_INT) * 8];
1201 unsigned count = 0;
1203 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1205 write_char ('n');
1206 number = -((HOST_WIDE_INT) number);
1208 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1209 write_chars (buffer + sizeof (buffer) - count, count);
1212 /* Write out an integral CST in decimal. Most numbers are small, and
1213 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1214 bigger than that, which we must deal with. */
1216 static inline void
1217 write_integer_cst (const tree cst)
1219 int sign = tree_int_cst_sgn (cst);
1221 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1223 /* A bignum. We do this in chunks, each of which fits in a
1224 HOST_WIDE_INT. */
1225 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1226 unsigned HOST_WIDE_INT chunk;
1227 unsigned chunk_digits;
1228 char *ptr = buffer + sizeof (buffer);
1229 unsigned count = 0;
1230 tree n, base, type;
1231 int done;
1233 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1234 representable. */
1235 chunk = 1000000000;
1236 chunk_digits = 9;
1238 if (sizeof (HOST_WIDE_INT) >= 8)
1240 /* It is at least 64 bits, so 10^18 is representable. */
1241 chunk_digits = 18;
1242 chunk *= chunk;
1245 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1246 base = build_int_cstu (type, chunk);
1247 n = build_int_cst_wide (type,
1248 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1250 if (sign < 0)
1252 write_char ('n');
1253 n = fold_build1 (NEGATE_EXPR, type, n);
1257 tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
1258 tree tmp = fold_build2 (MULT_EXPR, type, d, base);
1259 unsigned c;
1261 done = integer_zerop (d);
1262 tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
1263 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1264 done ? 1 : chunk_digits);
1265 ptr -= c;
1266 count += c;
1267 n = d;
1269 while (!done);
1270 write_chars (ptr, count);
1272 else
1274 /* A small num. */
1275 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1277 if (sign < 0)
1279 write_char ('n');
1280 low = -low;
1282 write_unsigned_number (low);
1286 /* Write out a floating-point literal.
1288 "Floating-point literals are encoded using the bit pattern of the
1289 target processor's internal representation of that number, as a
1290 fixed-length lowercase hexadecimal string, high-order bytes first
1291 (even if the target processor would store low-order bytes first).
1292 The "n" prefix is not used for floating-point literals; the sign
1293 bit is encoded with the rest of the number.
1295 Here are some examples, assuming the IEEE standard representation
1296 for floating point numbers. (Spaces are for readability, not
1297 part of the encoding.)
1299 1.0f Lf 3f80 0000 E
1300 -1.0f Lf bf80 0000 E
1301 1.17549435e-38f Lf 0080 0000 E
1302 1.40129846e-45f Lf 0000 0001 E
1303 0.0f Lf 0000 0000 E"
1305 Caller is responsible for the Lx and the E. */
1306 static void
1307 write_real_cst (const tree value)
1309 if (abi_version_at_least (2))
1311 long target_real[4]; /* largest supported float */
1312 char buffer[9]; /* eight hex digits in a 32-bit number */
1313 int i, limit, dir;
1315 tree type = TREE_TYPE (value);
1316 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1318 real_to_target (target_real, &TREE_REAL_CST (value),
1319 TYPE_MODE (type));
1321 /* The value in target_real is in the target word order,
1322 so we must write it out backward if that happens to be
1323 little-endian. write_number cannot be used, it will
1324 produce uppercase. */
1325 if (FLOAT_WORDS_BIG_ENDIAN)
1326 i = 0, limit = words, dir = 1;
1327 else
1328 i = words - 1, limit = -1, dir = -1;
1330 for (; i != limit; i += dir)
1332 sprintf (buffer, "%08lx", target_real[i]);
1333 write_chars (buffer, 8);
1336 else
1338 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1339 literally. Note that compatibility with 3.2 is impossible,
1340 because the old floating-point emulator used a different
1341 format for REAL_VALUE_TYPE. */
1342 size_t i;
1343 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1344 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1345 /*unsigned_p*/ 1,
1346 /*base*/ 16);
1347 G.need_abi_warning = 1;
1351 /* Non-terminal <identifier>.
1353 <identifier> ::= </unqualified source code identifier> */
1355 static void
1356 write_identifier (const char *identifier)
1358 MANGLE_TRACE ("identifier", identifier);
1359 write_string (identifier);
1362 /* Handle constructor productions of non-terminal <special-name>.
1363 CTOR is a constructor FUNCTION_DECL.
1365 <special-name> ::= C1 # complete object constructor
1366 ::= C2 # base object constructor
1367 ::= C3 # complete object allocating constructor
1369 Currently, allocating constructors are never used.
1371 We also need to provide mangled names for the maybe-in-charge
1372 constructor, so we treat it here too. mangle_decl_string will
1373 append *INTERNAL* to that, to make sure we never emit it. */
1375 static void
1376 write_special_name_constructor (const tree ctor)
1378 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1379 write_string ("C2");
1380 else
1382 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1383 /* Even though we don't ever emit a definition of
1384 the old-style destructor, we still have to
1385 consider entities (like static variables) nested
1386 inside it. */
1387 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor));
1388 write_string ("C1");
1392 /* Handle destructor productions of non-terminal <special-name>.
1393 DTOR is a destructor FUNCTION_DECL.
1395 <special-name> ::= D0 # deleting (in-charge) destructor
1396 ::= D1 # complete object (in-charge) destructor
1397 ::= D2 # base object (not-in-charge) destructor
1399 We also need to provide mangled names for the maybe-incharge
1400 destructor, so we treat it here too. mangle_decl_string will
1401 append *INTERNAL* to that, to make sure we never emit it. */
1403 static void
1404 write_special_name_destructor (const tree dtor)
1406 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1407 write_string ("D0");
1408 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1409 write_string ("D2");
1410 else
1412 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1413 /* Even though we don't ever emit a definition of
1414 the old-style destructor, we still have to
1415 consider entities (like static variables) nested
1416 inside it. */
1417 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1418 write_string ("D1");
1422 /* Return the discriminator for ENTITY appearing inside
1423 FUNCTION. The discriminator is the lexical ordinal of VAR among
1424 entities with the same name in the same FUNCTION. */
1426 static int
1427 discriminator_for_local_entity (tree entity)
1429 tree *type;
1431 /* Assume this is the only local entity with this name. */
1432 int discriminator = 0;
1434 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1435 discriminator = DECL_DISCRIMINATOR (entity);
1436 else if (TREE_CODE (entity) == TYPE_DECL)
1438 /* Scan the list of local classes. */
1439 entity = TREE_TYPE (entity);
1440 for (type = VEC_address (tree, local_classes); *type != entity; ++type)
1441 if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
1442 && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
1443 ++discriminator;
1446 return discriminator;
1449 /* Return the discriminator for STRING, a string literal used inside
1450 FUNCTION. The discriminator is the lexical ordinal of STRING among
1451 string literals used in FUNCTION. */
1453 static int
1454 discriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
1455 tree string ATTRIBUTE_UNUSED)
1457 /* For now, we don't discriminate amongst string literals. */
1458 return 0;
1461 /* <discriminator> := _ <number>
1463 The discriminator is used only for the second and later occurrences
1464 of the same name within a single function. In this case <number> is
1465 n - 2, if this is the nth occurrence, in lexical order. */
1467 static void
1468 write_discriminator (const int discriminator)
1470 /* If discriminator is zero, don't write anything. Otherwise... */
1471 if (discriminator > 0)
1473 write_char ('_');
1474 write_unsigned_number (discriminator - 1);
1478 /* Mangle the name of a function-scope entity. FUNCTION is the
1479 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
1480 the entity itself. LOCAL_ENTITY is the entity that's directly
1481 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1482 of ENTITY.
1484 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1485 := Z <function encoding> E s [<discriminator>] */
1487 static void
1488 write_local_name (const tree function, const tree local_entity,
1489 const tree entity)
1491 MANGLE_TRACE_TREE ("local-name", entity);
1493 write_char ('Z');
1494 write_encoding (function);
1495 write_char ('E');
1496 if (TREE_CODE (entity) == STRING_CST)
1498 write_char ('s');
1499 write_discriminator (discriminator_for_string_literal (function,
1500 entity));
1502 else
1504 /* Now the <entity name>. Let write_name know its being called
1505 from <local-name>, so it doesn't try to process the enclosing
1506 function scope again. */
1507 write_name (entity, /*ignore_local_scope=*/1);
1508 write_discriminator (discriminator_for_local_entity (local_entity));
1512 /* Non-terminals <type> and <CV-qualifier>.
1514 <type> ::= <builtin-type>
1515 ::= <function-type>
1516 ::= <class-enum-type>
1517 ::= <array-type>
1518 ::= <pointer-to-member-type>
1519 ::= <template-param>
1520 ::= <substitution>
1521 ::= <CV-qualifier>
1522 ::= P <type> # pointer-to
1523 ::= R <type> # reference-to
1524 ::= C <type> # complex pair (C 2000)
1525 ::= G <type> # imaginary (C 2000) [not supported]
1526 ::= U <source-name> <type> # vendor extended type qualifier
1528 TYPE is a type node. */
1530 static void
1531 write_type (tree type)
1533 /* This gets set to nonzero if TYPE turns out to be a (possibly
1534 CV-qualified) builtin type. */
1535 int is_builtin_type = 0;
1537 MANGLE_TRACE_TREE ("type", type);
1539 if (type == error_mark_node)
1540 return;
1542 if (find_substitution (type))
1543 return;
1545 if (write_CV_qualifiers_for_type (type) > 0)
1546 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1547 mangle the unqualified type. The recursive call is needed here
1548 since both the qualified and unqualified types are substitution
1549 candidates. */
1550 write_type (TYPE_MAIN_VARIANT (type));
1551 else if (TREE_CODE (type) == ARRAY_TYPE)
1552 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1553 so that the cv-qualification of the element type is available
1554 in write_array_type. */
1555 write_array_type (type);
1556 else
1558 /* See through any typedefs. */
1559 type = TYPE_MAIN_VARIANT (type);
1561 if (TYPE_PTRMEM_P (type))
1562 write_pointer_to_member_type (type);
1563 else switch (TREE_CODE (type))
1565 case VOID_TYPE:
1566 case BOOLEAN_TYPE:
1567 case INTEGER_TYPE: /* Includes wchar_t. */
1568 case REAL_TYPE:
1570 /* Handle any target-specific fundamental types. */
1571 const char *target_mangling
1572 = targetm.mangle_fundamental_type (type);
1574 if (target_mangling)
1576 write_string (target_mangling);
1577 return;
1580 /* If this is a typedef, TYPE may not be one of
1581 the standard builtin type nodes, but an alias of one. Use
1582 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1583 write_builtin_type (TYPE_MAIN_VARIANT (type));
1584 ++is_builtin_type;
1585 break;
1588 case COMPLEX_TYPE:
1589 write_char ('C');
1590 write_type (TREE_TYPE (type));
1591 break;
1593 case FUNCTION_TYPE:
1594 case METHOD_TYPE:
1595 write_function_type (type);
1596 break;
1598 case UNION_TYPE:
1599 case RECORD_TYPE:
1600 case ENUMERAL_TYPE:
1601 /* A pointer-to-member function is represented as a special
1602 RECORD_TYPE, so check for this first. */
1603 if (TYPE_PTRMEMFUNC_P (type))
1604 write_pointer_to_member_type (type);
1605 else
1606 write_class_enum_type (type);
1607 break;
1609 case TYPENAME_TYPE:
1610 case UNBOUND_CLASS_TEMPLATE:
1611 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1612 ordinary nested names. */
1613 write_nested_name (TYPE_STUB_DECL (type));
1614 break;
1616 case POINTER_TYPE:
1617 write_char ('P');
1618 write_type (TREE_TYPE (type));
1619 break;
1621 case REFERENCE_TYPE:
1622 write_char ('R');
1623 write_type (TREE_TYPE (type));
1624 break;
1626 case TEMPLATE_TYPE_PARM:
1627 case TEMPLATE_PARM_INDEX:
1628 write_template_param (type);
1629 break;
1631 case TEMPLATE_TEMPLATE_PARM:
1632 write_template_template_param (type);
1633 break;
1635 case BOUND_TEMPLATE_TEMPLATE_PARM:
1636 write_template_template_param (type);
1637 write_template_args
1638 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
1639 break;
1641 case VECTOR_TYPE:
1642 write_string ("U8__vector");
1643 write_type (TREE_TYPE (type));
1644 break;
1646 default:
1647 gcc_unreachable ();
1651 /* Types other than builtin types are substitution candidates. */
1652 if (!is_builtin_type)
1653 add_substitution (type);
1656 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1657 CV-qualifiers written for TYPE.
1659 <CV-qualifiers> ::= [r] [V] [K] */
1661 static int
1662 write_CV_qualifiers_for_type (const tree type)
1664 int num_qualifiers = 0;
1666 /* The order is specified by:
1668 "In cases where multiple order-insensitive qualifiers are
1669 present, they should be ordered 'K' (closest to the base type),
1670 'V', 'r', and 'U' (farthest from the base type) ..."
1672 Note that we do not use cp_type_quals below; given "const
1673 int[3]", the "const" is emitted with the "int", not with the
1674 array. */
1676 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1678 write_char ('r');
1679 ++num_qualifiers;
1681 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1683 write_char ('V');
1684 ++num_qualifiers;
1686 if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1688 write_char ('K');
1689 ++num_qualifiers;
1692 return num_qualifiers;
1695 /* Non-terminal <builtin-type>.
1697 <builtin-type> ::= v # void
1698 ::= b # bool
1699 ::= w # wchar_t
1700 ::= c # char
1701 ::= a # signed char
1702 ::= h # unsigned char
1703 ::= s # short
1704 ::= t # unsigned short
1705 ::= i # int
1706 ::= j # unsigned int
1707 ::= l # long
1708 ::= m # unsigned long
1709 ::= x # long long, __int64
1710 ::= y # unsigned long long, __int64
1711 ::= n # __int128
1712 ::= o # unsigned __int128
1713 ::= f # float
1714 ::= d # double
1715 ::= e # long double, __float80
1716 ::= g # __float128 [not supported]
1717 ::= u <source-name> # vendor extended type */
1719 static void
1720 write_builtin_type (tree type)
1722 switch (TREE_CODE (type))
1724 case VOID_TYPE:
1725 write_char ('v');
1726 break;
1728 case BOOLEAN_TYPE:
1729 write_char ('b');
1730 break;
1732 case INTEGER_TYPE:
1733 /* If this is size_t, get the underlying int type. */
1734 if (TYPE_IS_SIZETYPE (type))
1735 type = TYPE_DOMAIN (type);
1737 /* TYPE may still be wchar_t, since that isn't in
1738 integer_type_nodes. */
1739 if (type == wchar_type_node)
1740 write_char ('w');
1741 else if (TYPE_FOR_JAVA (type))
1742 write_java_integer_type_codes (type);
1743 else
1745 size_t itk;
1746 /* Assume TYPE is one of the shared integer type nodes. Find
1747 it in the array of these nodes. */
1748 iagain:
1749 for (itk = 0; itk < itk_none; ++itk)
1750 if (type == integer_types[itk])
1752 /* Print the corresponding single-letter code. */
1753 write_char (integer_type_codes[itk]);
1754 break;
1757 if (itk == itk_none)
1759 tree t = c_common_type_for_mode (TYPE_MODE (type),
1760 TYPE_UNSIGNED (type));
1761 if (type != t)
1763 type = t;
1764 goto iagain;
1767 if (TYPE_PRECISION (type) == 128)
1768 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
1769 else
1771 /* Allow for cases where TYPE is not one of the shared
1772 integer type nodes and write a "vendor extended builtin
1773 type" with a name the form intN or uintN, respectively.
1774 Situations like this can happen if you have an
1775 __attribute__((__mode__(__SI__))) type and use exotic
1776 switches like '-mint64' on MIPS or '-mint8' on AVR.
1777 Of course, this is undefined by the C++ ABI (and
1778 '-mint8' is not even Standard C conforming), but when
1779 using such special options you're pretty much in nowhere
1780 land anyway. */
1781 const char *prefix;
1782 char prec[11]; /* up to ten digits for an unsigned */
1784 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
1785 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
1786 write_char ('u'); /* "vendor extended builtin type" */
1787 write_unsigned_number (strlen (prefix) + strlen (prec));
1788 write_string (prefix);
1789 write_string (prec);
1793 break;
1795 case REAL_TYPE:
1796 if (type == float_type_node
1797 || type == java_float_type_node)
1798 write_char ('f');
1799 else if (type == double_type_node
1800 || type == java_double_type_node)
1801 write_char ('d');
1802 else if (type == long_double_type_node)
1803 write_char ('e');
1804 else
1805 gcc_unreachable ();
1806 break;
1808 default:
1809 gcc_unreachable ();
1813 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
1814 METHOD_TYPE. The return type is mangled before the parameter
1815 types.
1817 <function-type> ::= F [Y] <bare-function-type> E */
1819 static void
1820 write_function_type (const tree type)
1822 MANGLE_TRACE_TREE ("function-type", type);
1824 /* For a pointer to member function, the function type may have
1825 cv-qualifiers, indicating the quals for the artificial 'this'
1826 parameter. */
1827 if (TREE_CODE (type) == METHOD_TYPE)
1829 /* The first parameter must be a POINTER_TYPE pointing to the
1830 `this' parameter. */
1831 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1832 write_CV_qualifiers_for_type (this_type);
1835 write_char ('F');
1836 /* We don't track whether or not a type is `extern "C"'. Note that
1837 you can have an `extern "C"' function that does not have
1838 `extern "C"' type, and vice versa:
1840 extern "C" typedef void function_t();
1841 function_t f; // f has C++ linkage, but its type is
1842 // `extern "C"'
1844 typedef void function_t();
1845 extern "C" function_t f; // Vice versa.
1847 See [dcl.link]. */
1848 write_bare_function_type (type, /*include_return_type_p=*/1,
1849 /*decl=*/NULL);
1850 write_char ('E');
1853 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
1854 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
1855 is mangled before the parameter types. If non-NULL, DECL is
1856 FUNCTION_DECL for the function whose type is being emitted.
1858 <bare-function-type> ::= </signature/ type>+ */
1860 static void
1861 write_bare_function_type (const tree type, const int include_return_type_p,
1862 const tree decl)
1864 MANGLE_TRACE_TREE ("bare-function-type", type);
1866 /* Mangle the return type, if requested. */
1867 if (include_return_type_p)
1868 write_type (TREE_TYPE (type));
1870 /* Now mangle the types of the arguments. */
1871 write_method_parms (TYPE_ARG_TYPES (type),
1872 TREE_CODE (type) == METHOD_TYPE,
1873 decl);
1876 /* Write the mangled representation of a method parameter list of
1877 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
1878 considered a non-static method, and the this parameter is omitted.
1879 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1880 parameters are being emitted. */
1882 static void
1883 write_method_parms (tree parm_types, const int method_p, const tree decl)
1885 tree first_parm_type;
1886 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1888 /* Assume this parameter type list is variable-length. If it ends
1889 with a void type, then it's not. */
1890 int varargs_p = 1;
1892 /* If this is a member function, skip the first arg, which is the
1893 this pointer.
1894 "Member functions do not encode the type of their implicit this
1895 parameter."
1897 Similarly, there's no need to mangle artificial parameters, like
1898 the VTT parameters for constructors and destructors. */
1899 if (method_p)
1901 parm_types = TREE_CHAIN (parm_types);
1902 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1904 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1906 parm_types = TREE_CHAIN (parm_types);
1907 parm_decl = TREE_CHAIN (parm_decl);
1911 for (first_parm_type = parm_types;
1912 parm_types;
1913 parm_types = TREE_CHAIN (parm_types))
1915 tree parm = TREE_VALUE (parm_types);
1916 if (parm == void_type_node)
1918 /* "Empty parameter lists, whether declared as () or
1919 conventionally as (void), are encoded with a void parameter
1920 (v)." */
1921 if (parm_types == first_parm_type)
1922 write_type (parm);
1923 /* If the parm list is terminated with a void type, it's
1924 fixed-length. */
1925 varargs_p = 0;
1926 /* A void type better be the last one. */
1927 gcc_assert (TREE_CHAIN (parm_types) == NULL);
1929 else
1930 write_type (parm);
1933 if (varargs_p)
1934 /* <builtin-type> ::= z # ellipsis */
1935 write_char ('z');
1938 /* <class-enum-type> ::= <name> */
1940 static void
1941 write_class_enum_type (const tree type)
1943 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1946 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1947 arguments.
1949 <template-args> ::= I <template-arg>+ E */
1951 static void
1952 write_template_args (tree args)
1954 int i;
1955 int length = TREE_VEC_LENGTH (args);
1957 MANGLE_TRACE_TREE ("template-args", args);
1959 write_char ('I');
1961 gcc_assert (length > 0);
1963 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1965 /* We have nested template args. We want the innermost template
1966 argument list. */
1967 args = TREE_VEC_ELT (args, length - 1);
1968 length = TREE_VEC_LENGTH (args);
1970 for (i = 0; i < length; ++i)
1971 write_template_arg (TREE_VEC_ELT (args, i));
1973 write_char ('E');
1976 /* <expression> ::= <unary operator-name> <expression>
1977 ::= <binary operator-name> <expression> <expression>
1978 ::= <expr-primary>
1980 <expr-primary> ::= <template-param>
1981 ::= L <type> <value number> E # literal
1982 ::= L <mangled-name> E # external name
1983 ::= sr <type> <unqualified-name>
1984 ::= sr <type> <unqualified-name> <template-args> */
1986 static void
1987 write_expression (tree expr)
1989 enum tree_code code;
1991 code = TREE_CODE (expr);
1993 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1994 is converted (via qualification conversions) to another
1995 type. */
1996 while (TREE_CODE (expr) == NOP_EXPR
1997 || TREE_CODE (expr) == NON_LVALUE_EXPR)
1999 expr = TREE_OPERAND (expr, 0);
2000 code = TREE_CODE (expr);
2003 /* Handle pointers-to-members by making them look like expression
2004 nodes. */
2005 if (code == PTRMEM_CST)
2007 expr = build_nt (ADDR_EXPR,
2008 build_nt (SCOPE_REF,
2009 PTRMEM_CST_CLASS (expr),
2010 PTRMEM_CST_MEMBER (expr)));
2011 code = TREE_CODE (expr);
2014 /* Handle template parameters. */
2015 if (code == TEMPLATE_TYPE_PARM
2016 || code == TEMPLATE_TEMPLATE_PARM
2017 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2018 || code == TEMPLATE_PARM_INDEX)
2019 write_template_param (expr);
2020 /* Handle literals. */
2021 else if (TREE_CODE_CLASS (code) == tcc_constant
2022 || (abi_version_at_least (2) && code == CONST_DECL))
2023 write_template_arg_literal (expr);
2024 else if (DECL_P (expr))
2026 /* G++ 3.2 incorrectly mangled non-type template arguments of
2027 enumeration type using their names. */
2028 if (code == CONST_DECL)
2029 G.need_abi_warning = 1;
2030 write_char ('L');
2031 write_mangled_name (expr, false);
2032 write_char ('E');
2034 else if (TREE_CODE (expr) == SIZEOF_EXPR
2035 && TYPE_P (TREE_OPERAND (expr, 0)))
2037 write_string ("st");
2038 write_type (TREE_OPERAND (expr, 0));
2040 else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
2042 tree scope = TREE_OPERAND (expr, 0);
2043 tree member = TREE_OPERAND (expr, 1);
2045 /* If the MEMBER is a real declaration, then the qualifying
2046 scope was not dependent. Ideally, we would not have a
2047 SCOPE_REF in those cases, but sometimes we do. If the second
2048 argument is a DECL, then the name must not have been
2049 dependent. */
2050 if (DECL_P (member))
2051 write_expression (member);
2052 else
2054 tree template_args;
2056 write_string ("sr");
2057 write_type (scope);
2058 /* If MEMBER is a template-id, separate the template
2059 from the arguments. */
2060 if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2062 template_args = TREE_OPERAND (member, 1);
2063 member = TREE_OPERAND (member, 0);
2065 else
2066 template_args = NULL_TREE;
2067 /* Write out the name of the MEMBER. */
2068 if (IDENTIFIER_TYPENAME_P (member))
2069 write_conversion_operator_name (TREE_TYPE (member));
2070 else if (IDENTIFIER_OPNAME_P (member))
2072 int i;
2073 const char *mangled_name = NULL;
2075 /* Unfortunately, there is no easy way to go from the
2076 name of the operator back to the corresponding tree
2077 code. */
2078 for (i = 0; i < LAST_CPLUS_TREE_CODE; ++i)
2079 if (operator_name_info[i].identifier == member)
2081 /* The ABI says that we prefer binary operator
2082 names to unary operator names. */
2083 if (operator_name_info[i].arity == 2)
2085 mangled_name = operator_name_info[i].mangled_name;
2086 break;
2088 else if (!mangled_name)
2089 mangled_name = operator_name_info[i].mangled_name;
2091 else if (assignment_operator_name_info[i].identifier
2092 == member)
2094 mangled_name
2095 = assignment_operator_name_info[i].mangled_name;
2096 break;
2098 write_string (mangled_name);
2100 else
2101 write_source_name (member);
2102 /* Write out the template arguments. */
2103 if (template_args)
2104 write_template_args (template_args);
2107 else
2109 int i;
2111 /* When we bind a variable or function to a non-type template
2112 argument with reference type, we create an ADDR_EXPR to show
2113 the fact that the entity's address has been taken. But, we
2114 don't actually want to output a mangling code for the `&'. */
2115 if (TREE_CODE (expr) == ADDR_EXPR
2116 && TREE_TYPE (expr)
2117 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2119 expr = TREE_OPERAND (expr, 0);
2120 if (DECL_P (expr))
2122 write_expression (expr);
2123 return;
2126 code = TREE_CODE (expr);
2129 /* If it wasn't any of those, recursively expand the expression. */
2130 write_string (operator_name_info[(int) code].mangled_name);
2132 switch (code)
2134 case CALL_EXPR:
2135 sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
2136 break;
2138 case CAST_EXPR:
2139 write_type (TREE_TYPE (expr));
2140 /* There is no way to mangle a zero-operand cast like
2141 "T()". */
2142 if (!TREE_OPERAND (expr, 0))
2143 sorry ("zero-operand casts cannot be mangled due to a defect "
2144 "in the C++ ABI");
2145 else
2146 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2147 break;
2149 case STATIC_CAST_EXPR:
2150 case CONST_CAST_EXPR:
2151 write_type (TREE_TYPE (expr));
2152 write_expression (TREE_OPERAND (expr, 0));
2153 break;
2156 /* Handle pointers-to-members specially. */
2157 case SCOPE_REF:
2158 write_type (TREE_OPERAND (expr, 0));
2159 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
2160 write_source_name (TREE_OPERAND (expr, 1));
2161 else if (TREE_CODE (TREE_OPERAND (expr, 1)) == TEMPLATE_ID_EXPR)
2163 tree template_id;
2164 tree name;
2166 template_id = TREE_OPERAND (expr, 1);
2167 name = TREE_OPERAND (template_id, 0);
2168 /* FIXME: What about operators? */
2169 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2170 write_source_name (TREE_OPERAND (template_id, 0));
2171 write_template_args (TREE_OPERAND (template_id, 1));
2173 else
2175 /* G++ 3.2 incorrectly put out both the "sr" code and
2176 the nested name of the qualified name. */
2177 G.need_abi_warning = 1;
2178 write_encoding (TREE_OPERAND (expr, 1));
2180 break;
2182 default:
2183 for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
2185 tree operand = TREE_OPERAND (expr, i);
2186 /* As a GNU expression, the middle operand of a
2187 conditional may be omitted. Since expression
2188 manglings are supposed to represent the input token
2189 stream, there's no good way to mangle such an
2190 expression without extending the C++ ABI. */
2191 if (code == COND_EXPR && i == 1 && !operand)
2193 error ("omitted middle operand to %<?:%> operand "
2194 "cannot be mangled");
2195 continue;
2197 write_expression (operand);
2203 /* Literal subcase of non-terminal <template-arg>.
2205 "Literal arguments, e.g. "A<42L>", are encoded with their type
2206 and value. Negative integer values are preceded with "n"; for
2207 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
2208 encoded as 0, true as 1." */
2210 static void
2211 write_template_arg_literal (const tree value)
2213 write_char ('L');
2214 write_type (TREE_TYPE (value));
2216 switch (TREE_CODE (value))
2218 case CONST_DECL:
2219 write_integer_cst (DECL_INITIAL (value));
2220 break;
2222 case INTEGER_CST:
2223 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
2224 || integer_zerop (value) || integer_onep (value));
2225 write_integer_cst (value);
2226 break;
2228 case REAL_CST:
2229 write_real_cst (value);
2230 break;
2232 default:
2233 gcc_unreachable ();
2236 write_char ('E');
2239 /* Non-terminal <template-arg>.
2241 <template-arg> ::= <type> # type
2242 ::= L <type> </value/ number> E # literal
2243 ::= LZ <name> E # external name
2244 ::= X <expression> E # expression */
2246 static void
2247 write_template_arg (tree node)
2249 enum tree_code code = TREE_CODE (node);
2251 MANGLE_TRACE_TREE ("template-arg", node);
2253 /* A template template parameter's argument list contains TREE_LIST
2254 nodes of which the value field is the actual argument. */
2255 if (code == TREE_LIST)
2257 node = TREE_VALUE (node);
2258 /* If it's a decl, deal with its type instead. */
2259 if (DECL_P (node))
2261 node = TREE_TYPE (node);
2262 code = TREE_CODE (node);
2266 if (TREE_CODE (node) == NOP_EXPR
2267 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
2269 /* Template parameters can be of reference type. To maintain
2270 internal consistency, such arguments use a conversion from
2271 address of object to reference type. */
2272 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
2273 if (abi_version_at_least (2))
2274 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
2275 else
2276 G.need_abi_warning = 1;
2279 if (TYPE_P (node))
2280 write_type (node);
2281 else if (code == TEMPLATE_DECL)
2282 /* A template appearing as a template arg is a template template arg. */
2283 write_template_template_arg (node);
2284 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
2285 || (abi_version_at_least (2) && code == CONST_DECL))
2286 write_template_arg_literal (node);
2287 else if (DECL_P (node))
2289 /* Until ABI version 2, non-type template arguments of
2290 enumeration type were mangled using their names. */
2291 if (code == CONST_DECL && !abi_version_at_least (2))
2292 G.need_abi_warning = 1;
2293 write_char ('L');
2294 /* Until ABI version 3, the underscore before the mangled name
2295 was incorrectly omitted. */
2296 if (!abi_version_at_least (3))
2298 G.need_abi_warning = 1;
2299 write_char ('Z');
2301 else
2302 write_string ("_Z");
2303 write_encoding (node);
2304 write_char ('E');
2306 else
2308 /* Template arguments may be expressions. */
2309 write_char ('X');
2310 write_expression (node);
2311 write_char ('E');
2315 /* <template-template-arg>
2316 ::= <name>
2317 ::= <substitution> */
2319 static void
2320 write_template_template_arg (const tree decl)
2322 MANGLE_TRACE_TREE ("template-template-arg", decl);
2324 if (find_substitution (decl))
2325 return;
2326 write_name (decl, /*ignore_local_scope=*/0);
2327 add_substitution (decl);
2331 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
2333 <array-type> ::= A [</dimension/ number>] _ </element/ type>
2334 ::= A <expression> _ </element/ type>
2336 "Array types encode the dimension (number of elements) and the
2337 element type. For variable length arrays, the dimension (but not
2338 the '_' separator) is omitted." */
2340 static void
2341 write_array_type (const tree type)
2343 write_char ('A');
2344 if (TYPE_DOMAIN (type))
2346 tree index_type;
2347 tree max;
2349 index_type = TYPE_DOMAIN (type);
2350 /* The INDEX_TYPE gives the upper and lower bounds of the
2351 array. */
2352 max = TYPE_MAX_VALUE (index_type);
2353 if (TREE_CODE (max) == INTEGER_CST)
2355 /* The ABI specifies that we should mangle the number of
2356 elements in the array, not the largest allowed index. */
2357 max = size_binop (PLUS_EXPR, max, size_one_node);
2358 write_unsigned_number (tree_low_cst (max, 1));
2360 else
2362 max = TREE_OPERAND (max, 0);
2363 if (!abi_version_at_least (2))
2365 /* value_dependent_expression_p presumes nothing is
2366 dependent when PROCESSING_TEMPLATE_DECL is zero. */
2367 ++processing_template_decl;
2368 if (!value_dependent_expression_p (max))
2369 G.need_abi_warning = 1;
2370 --processing_template_decl;
2372 write_expression (max);
2376 write_char ('_');
2377 write_type (TREE_TYPE (type));
2380 /* Non-terminal <pointer-to-member-type> for pointer-to-member
2381 variables. TYPE is a pointer-to-member POINTER_TYPE.
2383 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2385 static void
2386 write_pointer_to_member_type (const tree type)
2388 write_char ('M');
2389 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
2390 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2393 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
2394 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2395 TEMPLATE_PARM_INDEX.
2397 <template-param> ::= T </parameter/ number> _ */
2399 static void
2400 write_template_param (const tree parm)
2402 int parm_index;
2403 int parm_level;
2404 tree parm_type = NULL_TREE;
2406 MANGLE_TRACE_TREE ("template-parm", parm);
2408 switch (TREE_CODE (parm))
2410 case TEMPLATE_TYPE_PARM:
2411 case TEMPLATE_TEMPLATE_PARM:
2412 case BOUND_TEMPLATE_TEMPLATE_PARM:
2413 parm_index = TEMPLATE_TYPE_IDX (parm);
2414 parm_level = TEMPLATE_TYPE_LEVEL (parm);
2415 break;
2417 case TEMPLATE_PARM_INDEX:
2418 parm_index = TEMPLATE_PARM_IDX (parm);
2419 parm_level = TEMPLATE_PARM_LEVEL (parm);
2420 parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
2421 break;
2423 default:
2424 gcc_unreachable ();
2427 write_char ('T');
2428 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2429 earliest template param denoted by `_'. */
2430 if (parm_index > 0)
2431 write_unsigned_number (parm_index - 1);
2432 write_char ('_');
2435 /* <template-template-param>
2436 ::= <template-param>
2437 ::= <substitution> */
2439 static void
2440 write_template_template_param (const tree parm)
2442 tree template = NULL_TREE;
2444 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2445 template template parameter. The substitution candidate here is
2446 only the template. */
2447 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
2449 template
2450 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2451 if (find_substitution (template))
2452 return;
2455 /* <template-param> encodes only the template parameter position,
2456 not its template arguments, which is fine here. */
2457 write_template_param (parm);
2458 if (template)
2459 add_substitution (template);
2462 /* Non-terminal <substitution>.
2464 <substitution> ::= S <seq-id> _
2465 ::= S_ */
2467 static void
2468 write_substitution (const int seq_id)
2470 MANGLE_TRACE ("substitution", "");
2472 write_char ('S');
2473 if (seq_id > 0)
2474 write_number (seq_id - 1, /*unsigned=*/1, 36);
2475 write_char ('_');
2478 /* Start mangling ENTITY. */
2480 static inline void
2481 start_mangling (const tree entity, const bool ident_p)
2483 G.entity = entity;
2484 G.need_abi_warning = false;
2485 if (!ident_p)
2487 obstack_free (&name_obstack, name_base);
2488 mangle_obstack = &name_obstack;
2489 name_base = obstack_alloc (&name_obstack, 0);
2491 else
2492 mangle_obstack = &ident_hash->stack;
2495 /* Done with mangling. Return the generated mangled name. If WARN is
2496 true, and the name of G.entity will be mangled differently in a
2497 future version of the ABI, issue a warning. */
2499 static inline const char *
2500 finish_mangling (const bool warn)
2502 if (warn_abi && warn && G.need_abi_warning)
2503 warning (0, "the mangled name of %qD will change in a future "
2504 "version of GCC",
2505 G.entity);
2507 /* Clear all the substitutions. */
2508 VARRAY_CLEAR (G.substitutions);
2510 /* Null-terminate the string. */
2511 write_char ('\0');
2513 return (const char *) obstack_finish (mangle_obstack);
2516 /* Initialize data structures for mangling. */
2518 void
2519 init_mangle (void)
2521 gcc_obstack_init (&name_obstack);
2522 name_base = obstack_alloc (&name_obstack, 0);
2523 VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
2525 /* Cache these identifiers for quick comparison when checking for
2526 standard substitutions. */
2527 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2528 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2529 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2530 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2531 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2532 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2535 /* Generate the mangled name of DECL. */
2537 static const char *
2538 mangle_decl_string (const tree decl)
2540 const char *result;
2542 start_mangling (decl, /*ident_p=*/true);
2544 if (TREE_CODE (decl) == TYPE_DECL)
2545 write_type (TREE_TYPE (decl));
2546 else
2547 write_mangled_name (decl, true);
2549 result = finish_mangling (/*warn=*/true);
2550 if (DEBUG_MANGLE)
2551 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2552 return result;
2555 /* Like get_identifier, except that NAME is assumed to have been
2556 allocated on the obstack used by the identifier hash table. */
2558 static inline tree
2559 get_identifier_nocopy (const char *name)
2561 hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name,
2562 strlen (name), HT_ALLOCED);
2563 return HT_IDENT_TO_GCC_IDENT (ht_node);
2566 /* Create an identifier for the external mangled name of DECL. */
2568 void
2569 mangle_decl (const tree decl)
2571 SET_DECL_ASSEMBLER_NAME (decl,
2572 get_identifier_nocopy (mangle_decl_string (decl)));
2575 /* Generate the mangled representation of TYPE. */
2577 const char *
2578 mangle_type_string (const tree type)
2580 const char *result;
2582 start_mangling (type, /*ident_p=*/false);
2583 write_type (type);
2584 result = finish_mangling (/*warn=*/false);
2585 if (DEBUG_MANGLE)
2586 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2587 return result;
2590 /* Create an identifier for the mangled name of a special component
2591 for belonging to TYPE. CODE is the ABI-specified code for this
2592 component. */
2594 static tree
2595 mangle_special_for_type (const tree type, const char *code)
2597 const char *result;
2599 /* We don't have an actual decl here for the special component, so
2600 we can't just process the <encoded-name>. Instead, fake it. */
2601 start_mangling (type, /*ident_p=*/true);
2603 /* Start the mangling. */
2604 write_string ("_Z");
2605 write_string (code);
2607 /* Add the type. */
2608 write_type (type);
2609 result = finish_mangling (/*warn=*/false);
2611 if (DEBUG_MANGLE)
2612 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2614 return get_identifier_nocopy (result);
2617 /* Create an identifier for the mangled representation of the typeinfo
2618 structure for TYPE. */
2620 tree
2621 mangle_typeinfo_for_type (const tree type)
2623 return mangle_special_for_type (type, "TI");
2626 /* Create an identifier for the mangled name of the NTBS containing
2627 the mangled name of TYPE. */
2629 tree
2630 mangle_typeinfo_string_for_type (const tree type)
2632 return mangle_special_for_type (type, "TS");
2635 /* Create an identifier for the mangled name of the vtable for TYPE. */
2637 tree
2638 mangle_vtbl_for_type (const tree type)
2640 return mangle_special_for_type (type, "TV");
2643 /* Returns an identifier for the mangled name of the VTT for TYPE. */
2645 tree
2646 mangle_vtt_for_type (const tree type)
2648 return mangle_special_for_type (type, "TT");
2651 /* Return an identifier for a construction vtable group. TYPE is
2652 the most derived class in the hierarchy; BINFO is the base
2653 subobject for which this construction vtable group will be used.
2655 This mangling isn't part of the ABI specification; in the ABI
2656 specification, the vtable group is dumped in the same COMDAT as the
2657 main vtable, and is referenced only from that vtable, so it doesn't
2658 need an external name. For binary formats without COMDAT sections,
2659 though, we need external names for the vtable groups.
2661 We use the production
2663 <special-name> ::= CT <type> <offset number> _ <base type> */
2665 tree
2666 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
2668 const char *result;
2670 start_mangling (type, /*ident_p=*/true);
2672 write_string ("_Z");
2673 write_string ("TC");
2674 write_type (type);
2675 write_integer_cst (BINFO_OFFSET (binfo));
2676 write_char ('_');
2677 write_type (BINFO_TYPE (binfo));
2679 result = finish_mangling (/*warn=*/false);
2680 if (DEBUG_MANGLE)
2681 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2682 return get_identifier_nocopy (result);
2685 /* Mangle a this pointer or result pointer adjustment.
2687 <call-offset> ::= h <fixed offset number> _
2688 ::= v <fixed offset number> _ <virtual offset number> _ */
2690 static void
2691 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
2693 write_char (virtual_offset ? 'v' : 'h');
2695 /* For either flavor, write the fixed offset. */
2696 write_integer_cst (fixed_offset);
2697 write_char ('_');
2699 /* For a virtual thunk, add the virtual offset. */
2700 if (virtual_offset)
2702 write_integer_cst (virtual_offset);
2703 write_char ('_');
2707 /* Return an identifier for the mangled name of a this-adjusting or
2708 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
2709 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
2710 is a virtual thunk, and it is the vtbl offset in
2711 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
2712 zero for a covariant thunk. Note, that FN_DECL might be a covariant
2713 thunk itself. A covariant thunk name always includes the adjustment
2714 for the this pointer, even if there is none.
2716 <special-name> ::= T <call-offset> <base encoding>
2717 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2718 <base encoding>
2721 tree
2722 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
2723 tree virtual_offset)
2725 const char *result;
2727 start_mangling (fn_decl, /*ident_p=*/true);
2729 write_string ("_Z");
2730 write_char ('T');
2732 if (!this_adjusting)
2734 /* Covariant thunk with no this adjustment */
2735 write_char ('c');
2736 mangle_call_offset (integer_zero_node, NULL_TREE);
2737 mangle_call_offset (fixed_offset, virtual_offset);
2739 else if (!DECL_THUNK_P (fn_decl))
2740 /* Plain this adjusting thunk. */
2741 mangle_call_offset (fixed_offset, virtual_offset);
2742 else
2744 /* This adjusting thunk to covariant thunk. */
2745 write_char ('c');
2746 mangle_call_offset (fixed_offset, virtual_offset);
2747 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2748 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2749 if (virtual_offset)
2750 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2751 mangle_call_offset (fixed_offset, virtual_offset);
2752 fn_decl = THUNK_TARGET (fn_decl);
2755 /* Scoped name. */
2756 write_encoding (fn_decl);
2758 result = finish_mangling (/*warn=*/false);
2759 if (DEBUG_MANGLE)
2760 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2761 return get_identifier_nocopy (result);
2764 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
2765 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
2766 TYPE. */
2768 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
2770 /* Hash a node (VAL1) in the table. */
2772 static hashval_t
2773 hash_type (const void *val)
2775 return (hashval_t) TYPE_UID (TREE_TYPE ((tree) val));
2778 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
2780 static int
2781 compare_type (const void *val1, const void *val2)
2783 return TREE_TYPE ((tree) val1) == (tree) val2;
2786 /* Return an identifier for the mangled unqualified name for a
2787 conversion operator to TYPE. This mangling is not specified by the
2788 ABI spec; it is only used internally. */
2790 tree
2791 mangle_conv_op_name_for_type (const tree type)
2793 void **slot;
2794 tree identifier;
2796 if (conv_type_names == NULL)
2797 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
2799 slot = htab_find_slot_with_hash (conv_type_names, type,
2800 (hashval_t) TYPE_UID (type), INSERT);
2801 identifier = (tree)*slot;
2802 if (!identifier)
2804 char buffer[64];
2806 /* Create a unique name corresponding to TYPE. */
2807 sprintf (buffer, "operator %lu",
2808 (unsigned long) htab_elements (conv_type_names));
2809 identifier = get_identifier (buffer);
2810 *slot = identifier;
2812 /* Hang TYPE off the identifier so it can be found easily later
2813 when performing conversions. */
2814 TREE_TYPE (identifier) = type;
2816 /* Set bits on the identifier so we know later it's a conversion. */
2817 IDENTIFIER_OPNAME_P (identifier) = 1;
2818 IDENTIFIER_TYPENAME_P (identifier) = 1;
2821 return identifier;
2824 /* Return an identifier for the name of an initialization guard
2825 variable for indicated VARIABLE. */
2827 tree
2828 mangle_guard_variable (const tree variable)
2830 start_mangling (variable, /*ident_p=*/true);
2831 write_string ("_ZGV");
2832 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
2833 /* The name of a guard variable for a reference temporary should refer
2834 to the reference, not the temporary. */
2835 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
2836 else
2837 write_name (variable, /*ignore_local_scope=*/0);
2838 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2841 /* Return an identifier for the name of a temporary variable used to
2842 initialize a static reference. This isn't part of the ABI, but we might
2843 as well call them something readable. */
2845 tree
2846 mangle_ref_init_variable (const tree variable)
2848 start_mangling (variable, /*ident_p=*/true);
2849 write_string ("_ZGR");
2850 write_name (variable, /*ignore_local_scope=*/0);
2851 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
2855 /* Foreign language type mangling section. */
2857 /* How to write the type codes for the integer Java type. */
2859 static void
2860 write_java_integer_type_codes (const tree type)
2862 if (type == java_int_type_node)
2863 write_char ('i');
2864 else if (type == java_short_type_node)
2865 write_char ('s');
2866 else if (type == java_byte_type_node)
2867 write_char ('c');
2868 else if (type == java_char_type_node)
2869 write_char ('w');
2870 else if (type == java_long_type_node)
2871 write_char ('x');
2872 else if (type == java_boolean_type_node)
2873 write_char ('b');
2874 else
2875 gcc_unreachable ();
2878 #include "gt-cp-mangle.h"