]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/mangle.c
Update FSF address.
[gcc.git] / gcc / cp / mangle.c
CommitLineData
3461fba7 1/* Name mangling for the 3.0 C++ ABI.
b39309c8
KH
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
1f6e1acc
AS
4 Written by Alex Samuel <sameul@codesourcery.com>
5
f5adbb8d 6 This file is part of GCC.
1f6e1acc 7
f5adbb8d 8 GCC is free software; you can redistribute it and/or modify it
1f6e1acc
AS
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.
12
f5adbb8d 13 GCC is distributed in the hope that it will be useful, but
1f6e1acc
AS
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.
17
18 You should have received a copy of the GNU General Public License
f5adbb8d 19 along with GCC; see the file COPYING. If not, write to the Free
1788952f
KC
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
1f6e1acc
AS
22
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.
30
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.
35
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:
41
0cbd7506
MS
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
1f6e1acc
AS
48
49*/
50
51#include "config.h"
52#include "system.h"
4977bab6
ZW
53#include "coretypes.h"
54#include "tm.h"
1f6e1acc 55#include "tree.h"
24386c5e 56#include "tm_p.h"
1f6e1acc 57#include "cp-tree.h"
11ad4784 58#include "real.h"
1f6e1acc
AS
59#include "obstack.h"
60#include "toplev.h"
61#include "varray.h"
57782ad8 62#include "flags.h"
f18eca82 63#include "target.h"
1f6e1acc
AS
64
65/* Debugging support. */
66
67/* Define DEBUG_MANGLE to enable very verbose trace messages. */
68#ifndef DEBUG_MANGLE
69#define DEBUG_MANGLE 0
70#endif
71
72/* Macros for tracing the write_* functions. */
73#if DEBUG_MANGLE
74# define MANGLE_TRACE(FN, INPUT) \
ff0b0e4c 75 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
1f6e1acc
AS
76# define MANGLE_TRACE_TREE(FN, NODE) \
77 fprintf (stderr, " %-24s: %-24s (%p)\n", \
0cbd7506 78 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
1f6e1acc
AS
79#else
80# define MANGLE_TRACE(FN, INPUT)
81# define MANGLE_TRACE_TREE(FN, NODE)
82#endif
83
838dfd8a 84/* Nonzero if NODE is a class template-id. We can't rely on
722bed0f
AS
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
f0f33848 88 without parameters inside the template. */
6397d80b
MM
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))))))
1f6e1acc
AS
94
95/* Things we only need one of. This module is not reentrant. */
2b8fe4a0 96typedef struct globals GTY(())
1f6e1acc 97{
1f6e1acc
AS
98 /* An array of the current substitution candidates, in the order
99 we've seen them. */
6e1141d7 100 VEC(tree,gc) *substitutions;
8c152bad 101
6397d80b 102 /* The entity that is being mangled. */
2b8fe4a0 103 tree GTY ((skip)) entity;
6397d80b 104
6397d80b
MM
105 /* True if the mangling will be different in a future version of the
106 ABI. */
107 bool need_abi_warning;
2b8fe4a0
MM
108} globals;
109
110static GTY (()) globals G;
111
112/* The obstack on which we build mangled names. */
113static struct obstack *mangle_obstack;
114
115/* The obstack on which we build mangled names that are not going to
116 be IDENTIFIER_NODEs. */
117static struct obstack name_obstack;
118
bb5ecf29
MM
119/* The first object on the name_obstack; we use this to free memory
120 allocated on the name_obstack. */
2b8fe4a0 121static void *name_base;
1f6e1acc 122
bb5ecf29
MM
123/* An incomplete mangled name. There will be no NUL terminator. If
124 there is no incomplete mangled name, this variable is NULL. */
125static char *partially_mangled_name;
126
127/* The number of characters in the PARTIALLY_MANGLED_NAME. */
128static size_t partially_mangled_name_len;
129
1f6e1acc
AS
130/* Indices into subst_identifiers. These are identifiers used in
131 special substitution rules. */
132typedef enum
133{
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
141}
142substitution_identifier_index_t;
143
144/* For quick substitution checks, look up these common identifiers
145 once only. */
0de298af 146static GTY(()) tree subst_identifiers[SUBID_MAX];
1f6e1acc
AS
147
148/* Single-letter codes for builtin integer types, defined in
149 <builtin-type>. These are indexed by integer_type_kind values. */
eb6b51b9 150static const char
1f6e1acc
AS
151integer_type_codes[itk_none] =
152{
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 */
164};
165
89aec4b1 166static int decl_is_template_id (const tree, tree* const);
722bed0f 167
1f6e1acc
AS
168/* Functions for handling substitutions. */
169
89aec4b1
NN
170static inline tree canonicalize_for_substitution (tree);
171static void add_substitution (tree);
172static inline int is_std_substitution (const tree,
173 const substitution_identifier_index_t);
174static inline int is_std_substitution_char (const tree,
175 const substitution_identifier_index_t);
176static int find_substitution (tree);
177static void mangle_call_offset (const tree, const tree);
1f6e1acc
AS
178
179/* Functions for emitting mangled representations of things. */
180
5fd80fbc 181static void write_mangled_name (const tree, bool);
89aec4b1
NN
182static void write_encoding (const tree);
183static void write_name (tree, const int);
184static void write_unscoped_name (const tree);
185static void write_unscoped_template_name (const tree);
186static void write_nested_name (const tree);
187static void write_prefix (const tree);
188static void write_template_prefix (const tree);
189static void write_unqualified_name (const tree);
190static void write_conversion_operator_name (const tree);
191static void write_source_name (tree);
192static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
193 const unsigned int);
194static void write_number (unsigned HOST_WIDE_INT, const int,
195 const unsigned int);
196static void write_integer_cst (const tree);
197static void write_real_cst (const tree);
198static void write_identifier (const char *);
199static void write_special_name_constructor (const tree);
200static void write_special_name_destructor (const tree);
201static void write_type (tree);
202static int write_CV_qualifiers_for_type (const tree);
203static void write_builtin_type (tree);
204static void write_function_type (const tree);
205static void write_bare_function_type (const tree, const int, const tree);
206static void write_method_parms (tree, const int, const tree);
207static void write_class_enum_type (const tree);
208static void write_template_args (tree);
209static void write_expression (tree);
210static void write_template_arg_literal (const tree);
211static void write_template_arg (tree);
212static void write_template_template_arg (const tree);
213static void write_array_type (const tree);
214static void write_pointer_to_member_type (const tree);
215static void write_template_param (const tree);
216static void write_template_template_param (const tree);
217static void write_substitution (const int);
218static int discriminator_for_local_entity (tree);
219static int discriminator_for_string_literal (tree, tree);
220static void write_discriminator (const int);
221static void write_local_name (const tree, const tree, const tree);
222static void dump_substitution_candidates (void);
223static const char *mangle_decl_string (const tree);
1f6e1acc
AS
224
225/* Control functions. */
226
2b8fe4a0 227static inline void start_mangling (const tree, bool);
89aec4b1
NN
228static inline const char *finish_mangling (const bool);
229static tree mangle_special_for_type (const tree, const char *);
1f6e1acc 230
c6002625 231/* Foreign language functions. */
23d4e4cc 232
89aec4b1 233static void write_java_integer_type_codes (const tree);
23d4e4cc 234
1f6e1acc
AS
235/* Append a single character to the end of the mangled
236 representation. */
0cbd7506 237#define write_char(CHAR) \
2b8fe4a0 238 obstack_1grow (mangle_obstack, (CHAR))
1f6e1acc 239
c6002625 240/* Append a sized buffer to the end of the mangled representation. */
0cbd7506 241#define write_chars(CHAR, LEN) \
2b8fe4a0 242 obstack_grow (mangle_obstack, (CHAR), (LEN))
0576ec13 243
1f6e1acc
AS
244/* Append a NUL-terminated string to the end of the mangled
245 representation. */
0cbd7506 246#define write_string(STRING) \
2b8fe4a0 247 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
1f6e1acc 248
838dfd8a 249/* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
459c43ad 250 same purpose (context, which may be a type) and value (template
1f6e1acc
AS
251 decl). See write_template_prefix for more information on what this
252 is used for. */
0cbd7506
MS
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)) \
1f6e1acc
AS
259 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
260
82a362d0 261/* Write out an unsigned quantity in base 10. */
0cbd7506 262#define write_unsigned_number(NUMBER) \
ff0b0e4c 263 write_number ((NUMBER), /*unsigned_p=*/1, 10)
82a362d0 264
bb5ecf29
MM
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. */
271
272static void
273save_partially_mangled_name (void)
274{
275 if (mangle_obstack == &ident_hash->stack)
276 {
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));
283 }
284}
285
286/* Restore the incomplete mangled name saved with
287 save_partially_mangled_name. */
288
289static void
290restore_partially_mangled_name (void)
291{
292 if (partially_mangled_name)
293 {
294 obstack_grow (mangle_obstack, partially_mangled_name,
295 partially_mangled_name_len);
296 free (partially_mangled_name);
297 partially_mangled_name = NULL;
298 }
299}
300
838dfd8a 301/* If DECL is a template instance, return nonzero and, if
722bed0f
AS
302 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
303 Otherwise return zero. */
304
305static int
89aec4b1 306decl_is_template_id (const tree decl, tree* const template_info)
722bed0f
AS
307{
308 if (TREE_CODE (decl) == TYPE_DECL)
309 {
310 /* TYPE_DECLs are handled specially. Look at its type to decide
311 if this is a template instantiation. */
89aec4b1 312 const tree type = TREE_TYPE (decl);
722bed0f
AS
313
314 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
315 {
316 if (template_info != NULL)
317 /* For a templated TYPE_DECL, the template info is hanging
318 off the type. */
6397d80b 319 *template_info = TYPE_TEMPLATE_INFO (type);
722bed0f
AS
320 return 1;
321 }
c8094d83 322 }
722bed0f
AS
323 else
324 {
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)
330 {
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;
336 }
337 }
338
339 /* It's not a template id. */
340 return 0;
341}
342
1f6e1acc
AS
343/* Produce debugging output of current substitution candidates. */
344
345static void
edaf3e03 346dump_substitution_candidates (void)
1f6e1acc
AS
347{
348 unsigned i;
6e1141d7 349 tree el;
1f6e1acc
AS
350
351 fprintf (stderr, " ++ substitutions ");
6e1141d7 352 for (i = 0; VEC_iterate (tree, G.substitutions, i, el); ++i)
1f6e1acc 353 {
1f6e1acc
AS
354 const char *name = "???";
355
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);
c8094d83
MS
365 if (TYPE_P (el) &&
366 (CP_TYPE_RESTRICT_P (el)
367 || CP_TYPE_VOLATILE_P (el)
1f6e1acc
AS
368 || CP_TYPE_CONST_P (el)))
369 fprintf (stderr, "CV-");
c8094d83 370 fprintf (stderr, "%s (%s at %p)\n",
1f6e1acc
AS
371 name, tree_code_name[TREE_CODE (el)], (void *) el);
372 }
373}
374
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
34cd5ae7 378 be treated accordingly in substitutions. This function returns a
1f6e1acc
AS
379 canonicalized tree node representing NODE that is used when adding
380 and substitution candidates and finding matches. */
381
382static inline tree
89aec4b1 383canonicalize_for_substitution (tree node)
1f6e1acc
AS
384{
385 /* For a TYPE_DECL, use the type instead. */
386 if (TREE_CODE (node) == TYPE_DECL)
387 node = TREE_TYPE (node);
bc6e9a0a
MM
388 if (TYPE_P (node))
389 node = canonical_type_variant (node);
1f6e1acc
AS
390
391 return node;
392}
393
394/* Add NODE as a substitution candidate. NODE must not already be on
395 the list of candidates. */
396
397static void
89aec4b1 398add_substitution (tree node)
1f6e1acc
AS
399{
400 tree c;
401
402 if (DEBUG_MANGLE)
c8094d83 403 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
1f6e1acc
AS
404 tree_code_name[TREE_CODE (node)], (void *) node);
405
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;
412
413#if ENABLE_CHECKING
414 /* Make sure NODE isn't already a candidate. */
415 {
416 int i;
6e1141d7
KH
417 tree candidate;
418
419 for (i = 0; VEC_iterate (tree, G.substitutions, i, candidate); i++)
1f6e1acc 420 {
8dc2b103 421 gcc_assert (!(DECL_P (node) && node == candidate));
c8094d83 422 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
8dc2b103 423 && same_type_p (node, candidate)));
1f6e1acc
AS
424 }
425 }
426#endif /* ENABLE_CHECKING */
427
428 /* Put the decl onto the varray of substitution candidates. */
6e1141d7 429 VEC_safe_push (tree, gc, G.substitutions, node);
1f6e1acc
AS
430
431 if (DEBUG_MANGLE)
432 dump_substitution_candidates ();
433}
434
838dfd8a 435/* Helper function for find_substitution. Returns nonzero if NODE,
1f6e1acc
AS
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. */
438
c8094d83 439static inline int
89aec4b1
NN
440is_std_substitution (const tree node,
441 const substitution_identifier_index_t index)
1f6e1acc
AS
442{
443 tree type = NULL;
444 tree decl = NULL;
445
446 if (DECL_P (node))
447 {
448 type = TREE_TYPE (node);
449 decl = node;
450 }
451 else if (CLASS_TYPE_P (node))
452 {
453 type = node;
454 decl = TYPE_NAME (node);
455 }
c8094d83 456 else
1f6e1acc
AS
457 /* These are not the droids you're looking for. */
458 return 0;
459
459c43ad 460 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
c8094d83 461 && TYPE_LANG_SPECIFIC (type)
6397d80b 462 && TYPE_TEMPLATE_INFO (type)
c8094d83 463 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
459c43ad 464 == subst_identifiers[index]));
1f6e1acc
AS
465}
466
838dfd8a 467/* Helper function for find_substitution. Returns nonzero if NODE,
1f6e1acc
AS
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]. */
471
472static inline int
89aec4b1
NN
473is_std_substitution_char (const tree node,
474 const substitution_identifier_index_t index)
1f6e1acc
AS
475{
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))
c8094d83 482 args = DECL_TI_ARGS (node);
1f6e1acc
AS
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>. */
c8094d83 489 return
1f6e1acc
AS
490 TREE_VEC_LENGTH (args) == 1
491 && TREE_VEC_ELT (args, 0) == char_type_node;
492}
493
494/* Check whether a substitution should be used to represent NODE in
495 the mangling.
496
497 First, check standard special-case substitutions.
498
c8094d83 499 <substitution> ::= St
0cbd7506 500 # ::std
1f6e1acc 501
0cbd7506 502 ::= Sa
1f6e1acc
AS
503 # ::std::allocator
504
0cbd7506
MS
505 ::= Sb
506 # ::std::basic_string
1f6e1acc 507
0cbd7506
MS
508 ::= Ss
509 # ::std::basic_string<char,
1f6e1acc
AS
510 ::std::char_traits<char>,
511 ::std::allocator<char> >
512
0cbd7506
MS
513 ::= Si
514 # ::std::basic_istream<char, ::std::char_traits<char> >
1f6e1acc 515
0cbd7506
MS
516 ::= So
517 # ::std::basic_ostream<char, ::std::char_traits<char> >
1f6e1acc 518
0cbd7506
MS
519 ::= Sd
520 # ::std::basic_iostream<char, ::std::char_traits<char> >
1f6e1acc
AS
521
522 Then examine the stack of currently available substitution
523 candidates for entities appearing earlier in the same mangling
524
525 If a substitution is found, write its mangled representation and
838dfd8a 526 return nonzero. If none is found, just return zero. */
1f6e1acc
AS
527
528static int
89aec4b1 529find_substitution (tree node)
1f6e1acc
AS
530{
531 int i;
6e1141d7 532 const int size = VEC_length (tree, G.substitutions);
1f6e1acc
AS
533 tree decl;
534 tree type;
535
536 if (DEBUG_MANGLE)
537 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
538 tree_code_name[TREE_CODE (node)], (void *) node);
539
540 /* Obtain the canonicalized substitution representation for NODE.
541 This is what we'll compare against. */
542 node = canonicalize_for_substitution (node);
543
544 /* Check for builtin substitutions. */
545
546 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
547 type = TYPE_P (node) ? node : TREE_TYPE (node);
548
549 /* Check for std::allocator. */
c8094d83 550 if (decl
459c43ad
MM
551 && is_std_substitution (decl, SUBID_ALLOCATOR)
552 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
1f6e1acc
AS
553 {
554 write_string ("Sa");
555 return 1;
556 }
557
558 /* Check for std::basic_string. */
559 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
560 {
82a362d0 561 if (TYPE_P (node))
1f6e1acc 562 {
c8094d83
MS
563 /* If this is a type (i.e. a fully-qualified template-id),
564 check for
0cbd7506
MS
565 std::basic_string <char,
566 std::char_traits<char>,
1f6e1acc 567 std::allocator<char> > . */
89d684bb 568 if (cp_type_quals (type) == TYPE_UNQUALIFIED
1f6e1acc
AS
569 && CLASSTYPE_USE_TEMPLATE (type))
570 {
571 tree args = CLASSTYPE_TI_ARGS (type);
572 if (TREE_VEC_LENGTH (args) == 3
bece74bd 573 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
1f6e1acc
AS
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))
578 {
579 write_string ("Ss");
580 return 1;
581 }
582 }
583 }
584 else
585 /* Substitute for the template name only if this isn't a type. */
586 {
587 write_string ("Sb");
588 return 1;
589 }
590 }
591
592 /* Check for basic_{i,o,io}stream. */
82a362d0 593 if (TYPE_P (node)
89d684bb 594 && cp_type_quals (type) == TYPE_UNQUALIFIED
1f6e1acc
AS
595 && CLASS_TYPE_P (type)
596 && CLASSTYPE_USE_TEMPLATE (type)
597 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
598 {
c8094d83 599 /* First, check for the template
1f6e1acc
AS
600 args <char, std::char_traits<char> > . */
601 tree args = CLASSTYPE_TI_ARGS (type);
602 if (TREE_VEC_LENGTH (args) == 2
6df5158a 603 && TYPE_P (TREE_VEC_ELT (args, 0))
bece74bd 604 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
1f6e1acc
AS
605 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
606 SUBID_CHAR_TRAITS))
607 {
608 /* Got them. Is this basic_istream? */
609 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
610 if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
611 {
612 write_string ("Si");
613 return 1;
614 }
615 /* Or basic_ostream? */
616 else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
617 {
618 write_string ("So");
619 return 1;
620 }
621 /* Or basic_iostream? */
622 else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
623 {
624 write_string ("Sd");
625 return 1;
626 }
627 }
628 }
629
630 /* Check for namespace std. */
82a362d0 631 if (decl && DECL_NAMESPACE_STD_P (decl))
1f6e1acc
AS
632 {
633 write_string ("St");
634 return 1;
635 }
636
637 /* Now check the list of available substitutions for this mangling
c6002625 638 operation. */
1f6e1acc
AS
639 for (i = 0; i < size; ++i)
640 {
6e1141d7 641 tree candidate = VEC_index (tree, G.substitutions, i);
1f6e1acc
AS
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))
648 {
649 write_substitution (i);
650 return 1;
651 }
652 }
653
654 /* No substitution found. */
655 return 0;
656}
657
658
5fd80fbc
NS
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.
c8094d83 662
5fd80fbc 663 <mangled-name> ::= _Z <encoding> */
1f6e1acc 664
965514bd 665static void
5fd80fbc 666write_mangled_name (const tree decl, bool top_level)
1f6e1acc
AS
667{
668 MANGLE_TRACE_TREE ("mangled-name", decl);
669
5fd80fbc
NS
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))
674 {
675 unmangled_name:;
c8094d83 676
5fd80fbc
NS
677 if (top_level)
678 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
679 else
680 {
681 /* The standard notes: "The <encoding> of an extern "C"
0cbd7506
MS
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. */
5fd80fbc
NS
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));
691 }
692 }
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)))
698 {
699 if (top_level || abi_version_at_least (2))
700 goto unmangled_name;
701 else
702 {
703 G.need_abi_warning = true;
704 goto mangled_name;
705 }
706 }
1f6e1acc 707 else
1f6e1acc 708 {
5fd80fbc 709 mangled_name:;
1f6e1acc
AS
710 write_string ("_Z");
711 write_encoding (decl);
5fd80fbc
NS
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* ");
1f6e1acc
AS
719 }
720}
721
722/* <encoding> ::= <function name> <bare-function-type>
bece74bd 723 ::= <data name> */
1f6e1acc
AS
724
725static void
89aec4b1 726write_encoding (const tree decl)
1f6e1acc
AS
727{
728 MANGLE_TRACE_TREE ("encoding", decl);
729
1f6e1acc
AS
730 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
731 {
324ffb8c
JJ
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));
1f6e1acc
AS
738 return;
739 }
740
f0f33848 741 write_name (decl, /*ignore_local_scope=*/0);
1f6e1acc
AS
742 if (TREE_CODE (decl) == FUNCTION_DECL)
743 {
744 tree fn_type;
4a5e0ed9 745 tree d;
1f6e1acc 746
722bed0f 747 if (decl_is_template_id (decl, NULL))
4a5e0ed9 748 {
bb5ecf29 749 save_partially_mangled_name ();
4a5e0ed9 750 fn_type = get_mostly_instantiated_function_type (decl);
bb5ecf29 751 restore_partially_mangled_name ();
4a5e0ed9
MM
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;
757 }
1f6e1acc 758 else
4a5e0ed9
MM
759 {
760 fn_type = TREE_TYPE (decl);
761 d = decl;
762 }
1f6e1acc 763
c8094d83 764 write_bare_function_type (fn_type,
94350948
MM
765 (!DECL_CONSTRUCTOR_P (decl)
766 && !DECL_DESTRUCTOR_P (decl)
f22967f3 767 && !DECL_CONV_FN_P (decl)
f58c04ac 768 && decl_is_template_id (decl, NULL)),
4a5e0ed9 769 d);
1f6e1acc 770 }
1f6e1acc
AS
771}
772
773/* <name> ::= <unscoped-name>
0cbd7506 774 ::= <unscoped-template-name> <template-args>
1f6e1acc 775 ::= <nested-name>
c8094d83 776 ::= <local-name>
f0f33848 777
838dfd8a 778 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
f0f33848
AS
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. */
1f6e1acc
AS
783
784static void
89aec4b1 785write_name (tree decl, const int ignore_local_scope)
1f6e1acc 786{
82a362d0
CS
787 tree context;
788
1f6e1acc
AS
789 MANGLE_TRACE_TREE ("name", decl);
790
722bed0f
AS
791 if (TREE_CODE (decl) == TYPE_DECL)
792 {
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)));
797 }
798 else
799 context = (DECL_CONTEXT (decl) == NULL) ? NULL : CP_DECL_CONTEXT (decl);
800
beac9b93
AS
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>. */
c8094d83
MS
806 if (context == NULL
807 || context == global_namespace
beac9b93
AS
808 || DECL_NAMESPACE_STD_P (context)
809 || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
1f6e1acc 810 {
722bed0f
AS
811 tree template_info;
812 /* Is this a template instance? */
813 if (decl_is_template_id (decl, &template_info))
1f6e1acc 814 {
722bed0f
AS
815 /* Yes: use <unscoped-template-name>. */
816 write_unscoped_template_name (TI_TEMPLATE (template_info));
817 write_template_args (TI_ARGS (template_info));
1f6e1acc
AS
818 }
819 else
820 /* Everything else gets an <unqualified-name>. */
821 write_unscoped_name (decl);
822 }
1f6e1acc 823 else
f0f33848
AS
824 {
825 /* Handle local names, unless we asked not to (that is, invoked
0cbd7506
MS
826 under <local-name>, to handle only the part of the name under
827 the local scope). */
f0f33848 828 if (!ignore_local_scope)
0cbd7506 829 {
f0f33848
AS
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)
838 {
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)
844 {
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;
849 }
850 /* Up one scope level. */
851 local_entity = context;
852 context = CP_DECL_CONTEXT (context);
853 }
854
855 /* No local scope found? Fall through to <nested-name>. */
856 }
857
858 /* Other decls get a <nested-name> to encode their scope. */
859 write_nested_name (decl);
860 }
1f6e1acc
AS
861}
862
863/* <unscoped-name> ::= <unqualified-name>
0cbd7506 864 ::= St <unqualified-name> # ::std:: */
1f6e1acc
AS
865
866static void
89aec4b1 867write_unscoped_name (const tree decl)
1f6e1acc
AS
868{
869 tree context = CP_DECL_CONTEXT (decl);
870
871 MANGLE_TRACE_TREE ("unscoped-name", decl);
872
873 /* Is DECL in ::std? */
874 if (DECL_NAMESPACE_STD_P (context))
875 {
876 write_string ("St");
877 write_unqualified_name (decl);
878 }
8dc2b103
NS
879 else
880 {
881 /* If not, it should be either in the global namespace, or directly
0cbd7506 882 in a local function scope. */
c8094d83 883 gcc_assert (context == global_namespace
8dc2b103
NS
884 || context == NULL
885 || TREE_CODE (context) == FUNCTION_DECL);
c8094d83 886
8dc2b103
NS
887 write_unqualified_name (decl);
888 }
1f6e1acc
AS
889}
890
891/* <unscoped-template-name> ::= <unscoped-name>
0cbd7506 892 ::= <substitution> */
1f6e1acc
AS
893
894static void
89aec4b1 895write_unscoped_template_name (const tree decl)
1f6e1acc
AS
896{
897 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
898
899 if (find_substitution (decl))
900 return;
901 write_unscoped_name (decl);
902 add_substitution (decl);
903}
904
905/* Write the nested name, including CV-qualifiers, of DECL.
906
c8094d83 907 <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
0cbd7506 908 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1f6e1acc
AS
909
910 <CV-qualifiers> ::= [r] [V] [K] */
911
912static void
89aec4b1 913write_nested_name (const tree decl)
1f6e1acc 914{
722bed0f
AS
915 tree template_info;
916
1f6e1acc
AS
917 MANGLE_TRACE_TREE ("nested-name", decl);
918
919 write_char ('N');
c8094d83 920
1f6e1acc 921 /* Write CV-qualifiers, if this is a member function. */
c8094d83 922 if (TREE_CODE (decl) == FUNCTION_DECL
1f6e1acc
AS
923 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
924 {
925 if (DECL_VOLATILE_MEMFUNC_P (decl))
926 write_char ('V');
927 if (DECL_CONST_MEMFUNC_P (decl))
928 write_char ('K');
929 }
930
722bed0f
AS
931 /* Is this a template instance? */
932 if (decl_is_template_id (decl, &template_info))
1f6e1acc 933 {
722bed0f 934 /* Yes, use <template-prefix>. */
1f6e1acc 935 write_template_prefix (decl);
722bed0f 936 write_template_args (TI_ARGS (template_info));
1f6e1acc
AS
937 }
938 else
939 {
722bed0f 940 /* No, just use <prefix> */
1f6e1acc 941 write_prefix (DECL_CONTEXT (decl));
f0f33848 942 write_unqualified_name (decl);
1f6e1acc
AS
943 }
944 write_char ('E');
945}
946
6397d80b 947/* <prefix> ::= <prefix> <unqualified-name>
0cbd7506
MS
948 ::= <template-param>
949 ::= <template-prefix> <template-args>
1f6e1acc
AS
950 ::= # empty
951 ::= <substitution> */
952
953static void
89aec4b1 954write_prefix (const tree node)
1f6e1acc
AS
955{
956 tree decl;
722bed0f
AS
957 /* Non-NULL if NODE represents a template-id. */
958 tree template_info = NULL;
959
960 MANGLE_TRACE_TREE ("prefix", node);
1f6e1acc
AS
961
962 if (node == NULL
963 || node == global_namespace)
964 return;
965
1f6e1acc
AS
966 if (find_substitution (node))
967 return;
968
722bed0f 969 if (DECL_P (node))
1f6e1acc 970 {
f0f33848
AS
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;
978
722bed0f
AS
979 decl = node;
980 decl_is_template_id (decl, &template_info);
1f6e1acc 981 }
722bed0f 982 else
722bed0f 983 {
6397d80b 984 /* Node is a type. */
722bed0f
AS
985 decl = TYPE_NAME (node);
986 if (CLASSTYPE_TEMPLATE_ID_P (node))
6397d80b 987 template_info = TYPE_TEMPLATE_INFO (node);
722bed0f
AS
988 }
989
6397d80b 990 /* In G++ 3.2, the name of the template parameter was used. */
c8094d83 991 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
6397d80b
MM
992 && !abi_version_at_least (2))
993 G.need_abi_warning = true;
994
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)
722bed0f 999 /* Templated. */
1f6e1acc 1000 {
722bed0f
AS
1001 write_template_prefix (decl);
1002 write_template_args (TI_ARGS (template_info));
1f6e1acc
AS
1003 }
1004 else
1005 /* Not templated. */
1006 {
722bed0f 1007 write_prefix (CP_DECL_CONTEXT (decl));
f0f33848 1008 write_unqualified_name (decl);
1f6e1acc
AS
1009 }
1010
1011 add_substitution (node);
1012}
1013
1014/* <template-prefix> ::= <prefix> <template component>
0cbd7506
MS
1015 ::= <template-param>
1016 ::= <substitution> */
1f6e1acc
AS
1017
1018static void
89aec4b1 1019write_template_prefix (const tree node)
1f6e1acc
AS
1020{
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);
722bed0f 1024 tree template_info;
1f6e1acc
AS
1025 tree template;
1026 tree substitution;
1027
1028 MANGLE_TRACE_TREE ("template-prefix", node);
1029
1030 /* Find the template decl. */
722bed0f
AS
1031 if (decl_is_template_id (decl, &template_info))
1032 template = TI_TEMPLATE (template_info);
1f6e1acc 1033 else
8dc2b103
NS
1034 {
1035 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
c8094d83 1036
8dc2b103
NS
1037 template = TYPE_TI_TEMPLATE (type);
1038 }
1f6e1acc 1039
459c43ad
MM
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
1043
1044 template<typename T> struct Outer {
1045 template<typename U> struct Inner {};
1046 };
1047
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>').
1052
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.
1058
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;
1f6e1acc
AS
1066
1067 if (find_substitution (substitution))
1068 return;
1069
6397d80b
MM
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;
1074
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
1079 {
1080 write_prefix (context);
1081 write_unqualified_name (decl);
1082 }
1f6e1acc
AS
1083
1084 add_substitution (substitution);
1085}
1086
1f6e1acc 1087/* We don't need to handle thunks, vtables, or VTTs here. Those are
c8094d83 1088 mangled through special entry points.
1f6e1acc
AS
1089
1090 <unqualified-name> ::= <operator-name>
c8094d83 1091 ::= <special-name>
1f6e1acc
AS
1092 ::= <source-name> */
1093
1094static void
89aec4b1 1095write_unqualified_name (const tree decl)
1f6e1acc
AS
1096{
1097 MANGLE_TRACE_TREE ("unqualified-name", decl);
1098
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);
ed36980c
JM
1103 else if (DECL_NAME (decl) == NULL_TREE)
1104 write_source_name (DECL_ASSEMBLER_NAME (decl));
c8094d83 1105 else if (DECL_CONV_FN_P (decl))
1f6e1acc 1106 {
c8094d83 1107 /* Conversion operator. Handle it right here.
0cbd7506 1108 <operator> ::= cv <type> */
83b14b88
JM
1109 tree type;
1110 if (decl_is_template_id (decl, NULL))
1111 {
bb5ecf29
MM
1112 tree fn_type;
1113 save_partially_mangled_name ();
1114 fn_type = get_mostly_instantiated_function_type (decl);
1115 restore_partially_mangled_name ();
83b14b88
JM
1116 type = TREE_TYPE (fn_type);
1117 }
1118 else
ca90f3e1 1119 type = DECL_CONV_FN_TYPE (decl);
3fa3c4bd 1120 write_conversion_operator_name (type);
1f6e1acc
AS
1121 }
1122 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1123 {
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;
c8094d83 1129
1f6e1acc
AS
1130 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1131 }
1132 else
1133 write_source_name (DECL_NAME (decl));
1134}
1135
3fa3c4bd
MM
1136/* Write the unqualified-name for a conversion operator to TYPE. */
1137
1138static void
89aec4b1 1139write_conversion_operator_name (const tree type)
3fa3c4bd
MM
1140{
1141 write_string ("cv");
1142 write_type (type);
1143}
1144
c8094d83 1145/* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1f6e1acc
AS
1146
1147 <source-name> ::= </length/ number> <identifier> */
1148
1149static void
89aec4b1 1150write_source_name (tree identifier)
1f6e1acc
AS
1151{
1152 MANGLE_TRACE_TREE ("source-name", identifier);
1153
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);
1158
82a362d0 1159 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1f6e1acc
AS
1160 write_identifier (IDENTIFIER_POINTER (identifier));
1161}
1162
0576ec13
NS
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). */
1168
1169static int
89aec4b1
NN
1170hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1171 char *buffer, const unsigned int min_digits)
0576ec13
NS
1172{
1173 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1174 unsigned digits = 0;
c8094d83 1175
0576ec13
NS
1176 while (number)
1177 {
1178 unsigned HOST_WIDE_INT d = number / base;
c8094d83 1179
0576ec13
NS
1180 *--buffer = base_digits[number - d * base];
1181 digits++;
1182 number = d;
1183 }
1184 while (digits < min_digits)
1185 {
1186 *--buffer = base_digits[0];
1187 digits++;
1188 }
1189 return digits;
1190}
1191
1f6e1acc
AS
1192/* Non-terminal <number>.
1193
1194 <number> ::= [n] </decimal integer/> */
1195
1196static void
89aec4b1
NN
1197write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1198 const unsigned int base)
1f6e1acc 1199{
0576ec13
NS
1200 char buffer[sizeof (HOST_WIDE_INT) * 8];
1201 unsigned count = 0;
1f6e1acc 1202
82a362d0 1203 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1f6e1acc
AS
1204 {
1205 write_char ('n');
82a362d0 1206 number = -((HOST_WIDE_INT) number);
1f6e1acc 1207 }
0576ec13
NS
1208 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1209 write_chars (buffer + sizeof (buffer) - count, count);
1f6e1acc
AS
1210}
1211
0576ec13
NS
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
c6002625 1214 bigger than that, which we must deal with. */
1f6e1acc
AS
1215
1216static inline void
89aec4b1 1217write_integer_cst (const tree cst)
1f6e1acc 1218{
0576ec13
NS
1219 int sign = tree_int_cst_sgn (cst);
1220
1221 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
82a362d0 1222 {
0576ec13 1223 /* A bignum. We do this in chunks, each of which fits in a
c6002625 1224 HOST_WIDE_INT. */
0576ec13
NS
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;
1232
1233 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
c6002625 1234 representable. */
0576ec13
NS
1235 chunk = 1000000000;
1236 chunk_digits = 9;
c8094d83 1237
0576ec13
NS
1238 if (sizeof (HOST_WIDE_INT) >= 8)
1239 {
c6002625 1240 /* It is at least 64 bits, so 10^18 is representable. */
0576ec13
NS
1241 chunk_digits = 18;
1242 chunk *= chunk;
1243 }
c8094d83 1244
ceef8ce4 1245 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
7d60be94
NS
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));
0576ec13
NS
1249
1250 if (sign < 0)
1251 {
1252 write_char ('n');
7866705a 1253 n = fold_build1 (NEGATE_EXPR, type, n);
0576ec13
NS
1254 }
1255 do
1256 {
7866705a
SB
1257 tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
1258 tree tmp = fold_build2 (MULT_EXPR, type, d, base);
0576ec13 1259 unsigned c;
89aec4b1 1260
0576ec13 1261 done = integer_zerop (d);
7866705a 1262 tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
0576ec13 1263 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
f293ce4b 1264 done ? 1 : chunk_digits);
0576ec13
NS
1265 ptr -= c;
1266 count += c;
1267 n = d;
1268 }
1269 while (!done);
1270 write_chars (ptr, count);
1271 }
c8094d83 1272 else
0576ec13
NS
1273 {
1274 /* A small num. */
1275 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
c8094d83 1276
0576ec13
NS
1277 if (sign < 0)
1278 {
1279 write_char ('n');
1280 low = -low;
1281 }
1282 write_unsigned_number (low);
82a362d0 1283 }
1f6e1acc
AS
1284}
1285
c8094d83
MS
1286/* Write out a floating-point literal.
1287
94d3ea58
ZW
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.
1294
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.)
1298
0cbd7506
MS
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"
94d3ea58
ZW
1304
1305 Caller is responsible for the Lx and the E. */
1306static void
89aec4b1 1307write_real_cst (const tree value)
94d3ea58
ZW
1308{
1309 if (abi_version_at_least (2))
1310 {
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;
1314
1315 tree type = TREE_TYPE (value);
1316 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1317
1318 real_to_target (target_real, &TREE_REAL_CST (value),
1319 TYPE_MODE (type));
1320
1321 /* The value in target_real is in the target word order,
0cbd7506 1322 so we must write it out backward if that happens to be
94d3ea58
ZW
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;
1329
1330 for (; i != limit; i += dir)
1331 {
1332 sprintf (buffer, "%08lx", target_real[i]);
1333 write_chars (buffer, 8);
1334 }
1335 }
1336 else
1337 {
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)
c8094d83 1344 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
94d3ea58
ZW
1345 /*unsigned_p*/ 1,
1346 /*base*/ 16);
1347 G.need_abi_warning = 1;
1348 }
1349}
1350
1f6e1acc
AS
1351/* Non-terminal <identifier>.
1352
1353 <identifier> ::= </unqualified source code identifier> */
1354
1355static void
89aec4b1 1356write_identifier (const char *identifier)
1f6e1acc
AS
1357{
1358 MANGLE_TRACE ("identifier", identifier);
1359 write_string (identifier);
1360}
1361
1362/* Handle constructor productions of non-terminal <special-name>.
c8094d83 1363 CTOR is a constructor FUNCTION_DECL.
1f6e1acc
AS
1364
1365 <special-name> ::= C1 # complete object constructor
0cbd7506
MS
1366 ::= C2 # base object constructor
1367 ::= C3 # complete object allocating constructor
1f6e1acc 1368
c8094d83 1369 Currently, allocating constructors are never used.
1f6e1acc 1370
3461fba7
NS
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. */
1f6e1acc
AS
1374
1375static void
89aec4b1 1376write_special_name_constructor (const tree ctor)
1f6e1acc 1377{
8dc2b103 1378 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1f6e1acc 1379 write_string ("C2");
657c130a 1380 else
8dc2b103
NS
1381 {
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");
1389 }
1f6e1acc
AS
1390}
1391
1392/* Handle destructor productions of non-terminal <special-name>.
c8094d83 1393 DTOR is a destructor FUNCTION_DECL.
1f6e1acc
AS
1394
1395 <special-name> ::= D0 # deleting (in-charge) destructor
0cbd7506
MS
1396 ::= D1 # complete object (in-charge) destructor
1397 ::= D2 # base object (not-in-charge) destructor
1f6e1acc 1398
3461fba7
NS
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. */
1f6e1acc
AS
1402
1403static void
89aec4b1 1404write_special_name_destructor (const tree dtor)
1f6e1acc
AS
1405{
1406 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1407 write_string ("D0");
1f6e1acc
AS
1408 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1409 write_string ("D2");
657c130a 1410 else
8dc2b103
NS
1411 {
1412 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1413 /* Even though we don't ever emit a definition of
0cbd7506
MS
1414 the old-style destructor, we still have to
1415 consider entities (like static variables) nested
1416 inside it. */
8dc2b103
NS
1417 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1418 write_string ("D1");
1419 }
1f6e1acc
AS
1420}
1421
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. */
1425
1426static int
89aec4b1 1427discriminator_for_local_entity (tree entity)
1f6e1acc 1428{
1f6e1acc 1429 /* Assume this is the only local entity with this name. */
89aec4b1 1430 int discriminator = 0;
1f6e1acc 1431
8e4ce833
JJ
1432 if (DECL_DISCRIMINATOR_P (entity) && DECL_LANG_SPECIFIC (entity))
1433 discriminator = DECL_DISCRIMINATOR (entity);
1434 else if (TREE_CODE (entity) == TYPE_DECL)
1435 {
9267ee62 1436 int ix;
c8094d83 1437
8e4ce833
JJ
1438 /* Scan the list of local classes. */
1439 entity = TREE_TYPE (entity);
9267ee62
NS
1440 for (ix = 0; ; ix++)
1441 {
1442 tree type = VEC_index (tree, local_classes, ix);
1443 if (type == entity)
1444 break;
1445 if (TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (entity)
1446 && TYPE_CONTEXT (type) == TYPE_CONTEXT (entity))
1447 ++discriminator;
1448 }
c8094d83 1449 }
1f6e1acc
AS
1450
1451 return discriminator;
1452}
1453
1454/* Return the discriminator for STRING, a string literal used inside
cd0be382 1455 FUNCTION. The discriminator is the lexical ordinal of STRING among
1f6e1acc
AS
1456 string literals used in FUNCTION. */
1457
1458static int
5671bf27
AJ
1459discriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
1460 tree string ATTRIBUTE_UNUSED)
1f6e1acc
AS
1461{
1462 /* For now, we don't discriminate amongst string literals. */
1463 return 0;
1464}
1465
c8094d83 1466/* <discriminator> := _ <number>
1f6e1acc
AS
1467
1468 The discriminator is used only for the second and later occurrences
1469 of the same name within a single function. In this case <number> is
1470 n - 2, if this is the nth occurrence, in lexical order. */
1471
1472static void
89aec4b1 1473write_discriminator (const int discriminator)
1f6e1acc 1474{
c6002625 1475 /* If discriminator is zero, don't write anything. Otherwise... */
1f6e1acc
AS
1476 if (discriminator > 0)
1477 {
1478 write_char ('_');
f1a0345c 1479 write_unsigned_number (discriminator - 1);
1f6e1acc
AS
1480 }
1481}
1482
1483/* Mangle the name of a function-scope entity. FUNCTION is the
1484 FUNCTION_DECL for the enclosing function. ENTITY is the decl for
f0f33848
AS
1485 the entity itself. LOCAL_ENTITY is the entity that's directly
1486 scoped in FUNCTION_DECL, either ENTITY itself or an enclosing scope
1487 of ENTITY.
1f6e1acc
AS
1488
1489 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
0cbd7506 1490 := Z <function encoding> E s [<discriminator>] */
1f6e1acc
AS
1491
1492static void
89aec4b1
NN
1493write_local_name (const tree function, const tree local_entity,
1494 const tree entity)
1f6e1acc
AS
1495{
1496 MANGLE_TRACE_TREE ("local-name", entity);
1497
1498 write_char ('Z');
1499 write_encoding (function);
1500 write_char ('E');
1501 if (TREE_CODE (entity) == STRING_CST)
1502 {
1503 write_char ('s');
c8094d83 1504 write_discriminator (discriminator_for_string_literal (function,
1f6e1acc
AS
1505 entity));
1506 }
1507 else
1508 {
f0f33848
AS
1509 /* Now the <entity name>. Let write_name know its being called
1510 from <local-name>, so it doesn't try to process the enclosing
1511 function scope again. */
1512 write_name (entity, /*ignore_local_scope=*/1);
1513 write_discriminator (discriminator_for_local_entity (local_entity));
1f6e1acc
AS
1514 }
1515}
1516
c8094d83 1517/* Non-terminals <type> and <CV-qualifier>.
1f6e1acc
AS
1518
1519 <type> ::= <builtin-type>
0cbd7506
MS
1520 ::= <function-type>
1521 ::= <class-enum-type>
1522 ::= <array-type>
1523 ::= <pointer-to-member-type>
1524 ::= <template-param>
1525 ::= <substitution>
1526 ::= <CV-qualifier>
1527 ::= P <type> # pointer-to
1528 ::= R <type> # reference-to
1529 ::= C <type> # complex pair (C 2000)
1530 ::= G <type> # imaginary (C 2000) [not supported]
1531 ::= U <source-name> <type> # vendor extended type qualifier
1f6e1acc
AS
1532
1533 TYPE is a type node. */
1534
c8094d83 1535static void
89aec4b1 1536write_type (tree type)
1f6e1acc 1537{
838dfd8a 1538 /* This gets set to nonzero if TYPE turns out to be a (possibly
1f6e1acc
AS
1539 CV-qualified) builtin type. */
1540 int is_builtin_type = 0;
1541
1542 MANGLE_TRACE_TREE ("type", type);
1543
04f3dc2b
MM
1544 if (type == error_mark_node)
1545 return;
1546
1f6e1acc
AS
1547 if (find_substitution (type))
1548 return;
c8094d83 1549
1f6e1acc
AS
1550 if (write_CV_qualifiers_for_type (type) > 0)
1551 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1552 mangle the unqualified type. The recursive call is needed here
34cd5ae7 1553 since both the qualified and unqualified types are substitution
1f6e1acc
AS
1554 candidates. */
1555 write_type (TYPE_MAIN_VARIANT (type));
b970a21d
MM
1556 else if (TREE_CODE (type) == ARRAY_TYPE)
1557 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1558 so that the cv-qualification of the element type is available
1559 in write_array_type. */
1560 write_array_type (type);
1f6e1acc 1561 else
82a362d0
CS
1562 {
1563 /* See through any typedefs. */
1564 type = TYPE_MAIN_VARIANT (type);
1565
8d245821
MM
1566 if (TYPE_PTRMEM_P (type))
1567 write_pointer_to_member_type (type);
1568 else switch (TREE_CODE (type))
82a362d0
CS
1569 {
1570 case VOID_TYPE:
1571 case BOOLEAN_TYPE:
1572 case INTEGER_TYPE: /* Includes wchar_t. */
1573 case REAL_TYPE:
f18eca82
ZL
1574 {
1575 /* Handle any target-specific fundamental types. */
1576 const char *target_mangling
1577 = targetm.mangle_fundamental_type (type);
1578
1579 if (target_mangling)
1580 {
1581 write_string (target_mangling);
1582 return;
1583 }
1584
82a362d0
CS
1585 /* If this is a typedef, TYPE may not be one of
1586 the standard builtin type nodes, but an alias of one. Use
1587 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1588 write_builtin_type (TYPE_MAIN_VARIANT (type));
1589 ++is_builtin_type;
1590 break;
f18eca82 1591 }
82a362d0
CS
1592
1593 case COMPLEX_TYPE:
1594 write_char ('C');
1595 write_type (TREE_TYPE (type));
1596 break;
1597
1598 case FUNCTION_TYPE:
1599 case METHOD_TYPE:
94350948 1600 write_function_type (type);
82a362d0
CS
1601 break;
1602
1603 case UNION_TYPE:
1604 case RECORD_TYPE:
1605 case ENUMERAL_TYPE:
1606 /* A pointer-to-member function is represented as a special
1607 RECORD_TYPE, so check for this first. */
1608 if (TYPE_PTRMEMFUNC_P (type))
1609 write_pointer_to_member_type (type);
1610 else
1611 write_class_enum_type (type);
1612 break;
1613
1614 case TYPENAME_TYPE:
b8c6534b
KL
1615 case UNBOUND_CLASS_TEMPLATE:
1616 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1617 ordinary nested names. */
82a362d0
CS
1618 write_nested_name (TYPE_STUB_DECL (type));
1619 break;
1620
82a362d0 1621 case POINTER_TYPE:
8d245821
MM
1622 write_char ('P');
1623 write_type (TREE_TYPE (type));
82a362d0
CS
1624 break;
1625
1626 case REFERENCE_TYPE:
1627 write_char ('R');
1628 write_type (TREE_TYPE (type));
1629 break;
1630
1631 case TEMPLATE_TYPE_PARM:
1632 case TEMPLATE_PARM_INDEX:
1633 write_template_param (type);
1634 break;
1635
1636 case TEMPLATE_TEMPLATE_PARM:
1637 write_template_template_param (type);
a1281f45
KL
1638 break;
1639
1640 case BOUND_TEMPLATE_TEMPLATE_PARM:
1641 write_template_template_param (type);
c8094d83 1642 write_template_args
a1281f45 1643 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
82a362d0
CS
1644 break;
1645
c00996a3
JM
1646 case VECTOR_TYPE:
1647 write_string ("U8__vector");
1648 write_type (TREE_TYPE (type));
1649 break;
1650
82a362d0 1651 default:
8dc2b103 1652 gcc_unreachable ();
82a362d0
CS
1653 }
1654 }
1f6e1acc
AS
1655
1656 /* Types other than builtin types are substitution candidates. */
1657 if (!is_builtin_type)
1658 add_substitution (type);
1659}
1660
1661/* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
1662 CV-qualifiers written for TYPE.
1663
1664 <CV-qualifiers> ::= [r] [V] [K] */
1665
1666static int
89aec4b1 1667write_CV_qualifiers_for_type (const tree type)
1f6e1acc
AS
1668{
1669 int num_qualifiers = 0;
1670
1671 /* The order is specified by:
1672
1673 "In cases where multiple order-insensitive qualifiers are
1674 present, they should be ordered 'K' (closest to the base type),
c8094d83 1675 'V', 'r', and 'U' (farthest from the base type) ..."
b970a21d
MM
1676
1677 Note that we do not use cp_type_quals below; given "const
1678 int[3]", the "const" is emitted with the "int", not with the
1679 array. */
1f6e1acc 1680
b970a21d 1681 if (TYPE_QUALS (type) & TYPE_QUAL_RESTRICT)
1f6e1acc
AS
1682 {
1683 write_char ('r');
1684 ++num_qualifiers;
1685 }
b970a21d 1686 if (TYPE_QUALS (type) & TYPE_QUAL_VOLATILE)
1f6e1acc
AS
1687 {
1688 write_char ('V');
1689 ++num_qualifiers;
1690 }
b970a21d 1691 if (TYPE_QUALS (type) & TYPE_QUAL_CONST)
1f6e1acc
AS
1692 {
1693 write_char ('K');
1694 ++num_qualifiers;
1695 }
1696
1697 return num_qualifiers;
1698}
1699
c8094d83 1700/* Non-terminal <builtin-type>.
1f6e1acc 1701
c8094d83 1702 <builtin-type> ::= v # void
0cbd7506
MS
1703 ::= b # bool
1704 ::= w # wchar_t
1705 ::= c # char
1706 ::= a # signed char
1707 ::= h # unsigned char
1708 ::= s # short
1709 ::= t # unsigned short
1710 ::= i # int
1711 ::= j # unsigned int
1712 ::= l # long
1713 ::= m # unsigned long
1714 ::= x # long long, __int64
1715 ::= y # unsigned long long, __int64
1716 ::= n # __int128
1717 ::= o # unsigned __int128
1718 ::= f # float
1719 ::= d # double
1720 ::= e # long double, __float80
1721 ::= g # __float128 [not supported]
1722 ::= u <source-name> # vendor extended type */
1f6e1acc 1723
c8094d83 1724static void
89aec4b1 1725write_builtin_type (tree type)
1f6e1acc
AS
1726{
1727 switch (TREE_CODE (type))
1728 {
1729 case VOID_TYPE:
1730 write_char ('v');
1731 break;
1732
1733 case BOOLEAN_TYPE:
1734 write_char ('b');
1735 break;
1736
1737 case INTEGER_TYPE:
1738 /* If this is size_t, get the underlying int type. */
1739 if (TYPE_IS_SIZETYPE (type))
1740 type = TYPE_DOMAIN (type);
1741
1742 /* TYPE may still be wchar_t, since that isn't in
1743 integer_type_nodes. */
4d8a1dd6 1744 if (type == wchar_type_node)
1f6e1acc 1745 write_char ('w');
792508b5 1746 else if (TYPE_FOR_JAVA (type))
23d4e4cc 1747 write_java_integer_type_codes (type);
1f6e1acc
AS
1748 else
1749 {
1750 size_t itk;
1751 /* Assume TYPE is one of the shared integer type nodes. Find
1752 it in the array of these nodes. */
c00996a3 1753 iagain:
1f6e1acc 1754 for (itk = 0; itk < itk_none; ++itk)
4d8a1dd6 1755 if (type == integer_types[itk])
1f6e1acc
AS
1756 {
1757 /* Print the corresponding single-letter code. */
1758 write_char (integer_type_codes[itk]);
1759 break;
1760 }
049e6879 1761
1f6e1acc 1762 if (itk == itk_none)
c00996a3 1763 {
b0c48229 1764 tree t = c_common_type_for_mode (TYPE_MODE (type),
8df83eae 1765 TYPE_UNSIGNED (type));
4484e9b6 1766 if (type != t)
049e6879 1767 {
4484e9b6
MO
1768 type = t;
1769 goto iagain;
049e6879 1770 }
4484e9b6
MO
1771
1772 if (TYPE_PRECISION (type) == 128)
1773 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
56668fb0
RS
1774 else
1775 {
4484e9b6
MO
1776 /* Allow for cases where TYPE is not one of the shared
1777 integer type nodes and write a "vendor extended builtin
1778 type" with a name the form intN or uintN, respectively.
1779 Situations like this can happen if you have an
1780 __attribute__((__mode__(__SI__))) type and use exotic
62d9f2e9
RS
1781 switches like '-mint8' on AVR. Of course, this is
1782 undefined by the C++ ABI (and '-mint8' is not even
1783 Standard C conforming), but when using such special
1784 options you're pretty much in nowhere land anyway. */
4484e9b6
MO
1785 const char *prefix;
1786 char prec[11]; /* up to ten digits for an unsigned */
1787
1788 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
1789 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
1790 write_char ('u'); /* "vendor extended builtin type" */
1791 write_unsigned_number (strlen (prefix) + strlen (prec));
1792 write_string (prefix);
1793 write_string (prec);
56668fb0 1794 }
c00996a3 1795 }
1f6e1acc
AS
1796 }
1797 break;
1798
1799 case REAL_TYPE:
23d4e4cc
APB
1800 if (type == float_type_node
1801 || type == java_float_type_node)
1f6e1acc 1802 write_char ('f');
23d4e4cc
APB
1803 else if (type == double_type_node
1804 || type == java_double_type_node)
1f6e1acc 1805 write_char ('d');
4d8a1dd6 1806 else if (type == long_double_type_node)
1f6e1acc
AS
1807 write_char ('e');
1808 else
8dc2b103 1809 gcc_unreachable ();
1f6e1acc
AS
1810 break;
1811
1812 default:
8dc2b103 1813 gcc_unreachable ();
1f6e1acc
AS
1814 }
1815}
1816
1817/* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
94350948
MM
1818 METHOD_TYPE. The return type is mangled before the parameter
1819 types.
1f6e1acc
AS
1820
1821 <function-type> ::= F [Y] <bare-function-type> E */
1822
1823static void
89aec4b1 1824write_function_type (const tree type)
1f6e1acc
AS
1825{
1826 MANGLE_TRACE_TREE ("function-type", type);
1827
6ea868b7
JM
1828 /* For a pointer to member function, the function type may have
1829 cv-qualifiers, indicating the quals for the artificial 'this'
1830 parameter. */
1831 if (TREE_CODE (type) == METHOD_TYPE)
1832 {
1833 /* The first parameter must be a POINTER_TYPE pointing to the
1834 `this' parameter. */
1835 tree this_type = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
1836 write_CV_qualifiers_for_type (this_type);
1837 }
1838
1f6e1acc
AS
1839 write_char ('F');
1840 /* We don't track whether or not a type is `extern "C"'. Note that
1841 you can have an `extern "C"' function that does not have
1842 `extern "C"' type, and vice versa:
1843
1844 extern "C" typedef void function_t();
1845 function_t f; // f has C++ linkage, but its type is
0cbd7506 1846 // `extern "C"'
1f6e1acc
AS
1847
1848 typedef void function_t();
1849 extern "C" function_t f; // Vice versa.
1850
1851 See [dcl.link]. */
c8094d83 1852 write_bare_function_type (type, /*include_return_type_p=*/1,
f58c04ac 1853 /*decl=*/NULL);
1f6e1acc
AS
1854 write_char ('E');
1855}
1856
f58c04ac 1857/* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
838dfd8a 1858 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
f58c04ac
MM
1859 is mangled before the parameter types. If non-NULL, DECL is
1860 FUNCTION_DECL for the function whose type is being emitted.
1f6e1acc
AS
1861
1862 <bare-function-type> ::= </signature/ type>+ */
1863
1864static void
89aec4b1
NN
1865write_bare_function_type (const tree type, const int include_return_type_p,
1866 const tree decl)
1f6e1acc
AS
1867{
1868 MANGLE_TRACE_TREE ("bare-function-type", type);
1869
1870 /* Mangle the return type, if requested. */
1871 if (include_return_type_p)
1872 write_type (TREE_TYPE (type));
1873
1874 /* Now mangle the types of the arguments. */
c8094d83 1875 write_method_parms (TYPE_ARG_TYPES (type),
f58c04ac
MM
1876 TREE_CODE (type) == METHOD_TYPE,
1877 decl);
1f6e1acc
AS
1878}
1879
1880/* Write the mangled representation of a method parameter list of
838dfd8a 1881 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
f58c04ac
MM
1882 considered a non-static method, and the this parameter is omitted.
1883 If non-NULL, DECL is the FUNCTION_DECL for the function whose
1884 parameters are being emitted. */
1f6e1acc
AS
1885
1886static void
89aec4b1 1887write_method_parms (tree parm_types, const int method_p, const tree decl)
1f6e1acc 1888{
f58c04ac
MM
1889 tree first_parm_type;
1890 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
1891
1f6e1acc
AS
1892 /* Assume this parameter type list is variable-length. If it ends
1893 with a void type, then it's not. */
1894 int varargs_p = 1;
1895
1896 /* If this is a member function, skip the first arg, which is the
c8094d83 1897 this pointer.
1f6e1acc 1898 "Member functions do not encode the type of their implicit this
c8094d83
MS
1899 parameter."
1900
f58c04ac
MM
1901 Similarly, there's no need to mangle artificial parameters, like
1902 the VTT parameters for constructors and destructors. */
1f6e1acc 1903 if (method_p)
1f6e1acc 1904 {
f58c04ac
MM
1905 parm_types = TREE_CHAIN (parm_types);
1906 parm_decl = parm_decl ? TREE_CHAIN (parm_decl) : NULL_TREE;
1f6e1acc 1907
f58c04ac
MM
1908 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
1909 {
1910 parm_types = TREE_CHAIN (parm_types);
1911 parm_decl = TREE_CHAIN (parm_decl);
1912 }
1913 }
1914
c8094d83
MS
1915 for (first_parm_type = parm_types;
1916 parm_types;
f58c04ac
MM
1917 parm_types = TREE_CHAIN (parm_types))
1918 {
1919 tree parm = TREE_VALUE (parm_types);
b72801e2 1920 if (parm == void_type_node)
1f6e1acc
AS
1921 {
1922 /* "Empty parameter lists, whether declared as () or
1923 conventionally as (void), are encoded with a void parameter
1924 (v)." */
f58c04ac 1925 if (parm_types == first_parm_type)
1f6e1acc
AS
1926 write_type (parm);
1927 /* If the parm list is terminated with a void type, it's
1928 fixed-length. */
1929 varargs_p = 0;
1930 /* A void type better be the last one. */
50bc768d 1931 gcc_assert (TREE_CHAIN (parm_types) == NULL);
1f6e1acc
AS
1932 }
1933 else
1934 write_type (parm);
1935 }
1936
1937 if (varargs_p)
1938 /* <builtin-type> ::= z # ellipsis */
1939 write_char ('z');
1940}
1941
1942/* <class-enum-type> ::= <name> */
1943
c8094d83 1944static void
89aec4b1 1945write_class_enum_type (const tree type)
1f6e1acc 1946{
f0f33848 1947 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
1f6e1acc
AS
1948}
1949
1950/* Non-terminal <template-args>. ARGS is a TREE_VEC of template
1951 arguments.
1952
1953 <template-args> ::= I <template-arg>+ E */
1954
1955static void
89aec4b1 1956write_template_args (tree args)
1f6e1acc 1957{
0f30f285
NS
1958 int i;
1959 int length = TREE_VEC_LENGTH (args);
c8094d83 1960
1f6e1acc
AS
1961 MANGLE_TRACE_TREE ("template-args", args);
1962
3fa3c4bd 1963 write_char ('I');
1f6e1acc 1964
50bc768d 1965 gcc_assert (length > 0);
3fa3c4bd 1966
0f30f285 1967 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
3fa3c4bd 1968 {
0f30f285
NS
1969 /* We have nested template args. We want the innermost template
1970 argument list. */
1971 args = TREE_VEC_ELT (args, length - 1);
1972 length = TREE_VEC_LENGTH (args);
1f6e1acc 1973 }
0f30f285
NS
1974 for (i = 0; i < length; ++i)
1975 write_template_arg (TREE_VEC_ELT (args, i));
c8094d83 1976
1f6e1acc
AS
1977 write_char ('E');
1978}
1979
1980/* <expression> ::= <unary operator-name> <expression>
1981 ::= <binary operator-name> <expression> <expression>
1982 ::= <expr-primary>
1983
1984 <expr-primary> ::= <template-param>
0cbd7506
MS
1985 ::= L <type> <value number> E # literal
1986 ::= L <mangled-name> E # external name
1987 ::= sr <type> <unqualified-name>
1988 ::= sr <type> <unqualified-name> <template-args> */
1f6e1acc
AS
1989
1990static void
89aec4b1 1991write_expression (tree expr)
1f6e1acc
AS
1992{
1993 enum tree_code code;
1994
1995 code = TREE_CODE (expr);
1996
b6ab6892
GB
1997 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
1998 is converted (via qualification conversions) to another
1999 type. */
2000 while (TREE_CODE (expr) == NOP_EXPR
2001 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2002 {
2003 expr = TREE_OPERAND (expr, 0);
2004 code = TREE_CODE (expr);
2005 }
2006
1f6e1acc
AS
2007 /* Handle pointers-to-members by making them look like expression
2008 nodes. */
2009 if (code == PTRMEM_CST)
2010 {
2011 expr = build_nt (ADDR_EXPR,
2012 build_nt (SCOPE_REF,
2013 PTRMEM_CST_CLASS (expr),
2014 PTRMEM_CST_MEMBER (expr)));
2015 code = TREE_CODE (expr);
2016 }
2017
c6002625 2018 /* Handle template parameters. */
c8094d83 2019 if (code == TEMPLATE_TYPE_PARM
1f6e1acc 2020 || code == TEMPLATE_TEMPLATE_PARM
a1281f45
KL
2021 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2022 || code == TEMPLATE_PARM_INDEX)
1f6e1acc
AS
2023 write_template_param (expr);
2024 /* Handle literals. */
6615c446 2025 else if (TREE_CODE_CLASS (code) == tcc_constant
d3133e68 2026 || (abi_version_at_least (2) && code == CONST_DECL))
1f6e1acc
AS
2027 write_template_arg_literal (expr);
2028 else if (DECL_P (expr))
2029 {
d3133e68
MM
2030 /* G++ 3.2 incorrectly mangled non-type template arguments of
2031 enumeration type using their names. */
2032 if (code == CONST_DECL)
2033 G.need_abi_warning = 1;
1f6e1acc 2034 write_char ('L');
5fd80fbc 2035 write_mangled_name (expr, false);
1f6e1acc
AS
2036 write_char ('E');
2037 }
c8094d83 2038 else if (TREE_CODE (expr) == SIZEOF_EXPR
5d69ba1f
MM
2039 && TYPE_P (TREE_OPERAND (expr, 0)))
2040 {
2041 write_string ("st");
2042 write_type (TREE_OPERAND (expr, 0));
2043 }
3fa3c4bd
MM
2044 else if (abi_version_at_least (2) && TREE_CODE (expr) == SCOPE_REF)
2045 {
2046 tree scope = TREE_OPERAND (expr, 0);
2047 tree member = TREE_OPERAND (expr, 1);
2048
2049 /* If the MEMBER is a real declaration, then the qualifying
2050 scope was not dependent. Ideally, we would not have a
2051 SCOPE_REF in those cases, but sometimes we do. If the second
2052 argument is a DECL, then the name must not have been
2053 dependent. */
2054 if (DECL_P (member))
2055 write_expression (member);
2056 else
2057 {
2058 tree template_args;
2059
2060 write_string ("sr");
2061 write_type (scope);
2062 /* If MEMBER is a template-id, separate the template
2063 from the arguments. */
2064 if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2065 {
2066 template_args = TREE_OPERAND (member, 1);
2067 member = TREE_OPERAND (member, 0);
3fa3c4bd
MM
2068 }
2069 else
2070 template_args = NULL_TREE;
2071 /* Write out the name of the MEMBER. */
2072 if (IDENTIFIER_TYPENAME_P (member))
2073 write_conversion_operator_name (TREE_TYPE (member));
2074 else if (IDENTIFIER_OPNAME_P (member))
2075 {
2076 int i;
2077 const char *mangled_name = NULL;
2078
2079 /* Unfortunately, there is no easy way to go from the
2080 name of the operator back to the corresponding tree
2081 code. */
2082 for (i = 0; i < LAST_CPLUS_TREE_CODE; ++i)
2083 if (operator_name_info[i].identifier == member)
2084 {
2085 /* The ABI says that we prefer binary operator
2086 names to unary operator names. */
2087 if (operator_name_info[i].arity == 2)
2088 {
2089 mangled_name = operator_name_info[i].mangled_name;
2090 break;
2091 }
2092 else if (!mangled_name)
2093 mangled_name = operator_name_info[i].mangled_name;
2094 }
2095 else if (assignment_operator_name_info[i].identifier
2096 == member)
2097 {
c8094d83 2098 mangled_name
3fa3c4bd
MM
2099 = assignment_operator_name_info[i].mangled_name;
2100 break;
2101 }
2102 write_string (mangled_name);
2103 }
2104 else
2105 write_source_name (member);
2106 /* Write out the template arguments. */
2107 if (template_args)
2108 write_template_args (template_args);
2109 }
2110 }
1f6e1acc
AS
2111 else
2112 {
2113 int i;
2114
0dc09a61
MM
2115 /* When we bind a variable or function to a non-type template
2116 argument with reference type, we create an ADDR_EXPR to show
2117 the fact that the entity's address has been taken. But, we
2118 don't actually want to output a mangling code for the `&'. */
2119 if (TREE_CODE (expr) == ADDR_EXPR
2120 && TREE_TYPE (expr)
2121 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
82a362d0
CS
2122 {
2123 expr = TREE_OPERAND (expr, 0);
2124 if (DECL_P (expr))
2125 {
2126 write_expression (expr);
2127 return;
2128 }
0dc09a61 2129
82a362d0
CS
2130 code = TREE_CODE (expr);
2131 }
3fa3c4bd 2132
1f6e1acc
AS
2133 /* If it wasn't any of those, recursively expand the expression. */
2134 write_string (operator_name_info[(int) code].mangled_name);
2135
873ff987 2136 switch (code)
1f6e1acc 2137 {
0cbd7506
MS
2138 case CALL_EXPR:
2139 sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
2140 break;
f8bee204 2141
873ff987
KL
2142 case CAST_EXPR:
2143 write_type (TREE_TYPE (expr));
383c9d5d
MM
2144 /* There is no way to mangle a zero-operand cast like
2145 "T()". */
2146 if (!TREE_OPERAND (expr, 0))
2147 sorry ("zero-operand casts cannot be mangled due to a defect "
2148 "in the C++ ABI");
2149 else
2150 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
873ff987
KL
2151 break;
2152
2153 case STATIC_CAST_EXPR:
2154 case CONST_CAST_EXPR:
2155 write_type (TREE_TYPE (expr));
2156 write_expression (TREE_OPERAND (expr, 0));
2157 break;
2158
c8094d83 2159
873ff987
KL
2160 /* Handle pointers-to-members specially. */
2161 case SCOPE_REF:
1f6e1acc
AS
2162 write_type (TREE_OPERAND (expr, 0));
2163 if (TREE_CODE (TREE_OPERAND (expr, 1)) == IDENTIFIER_NODE)
2164 write_source_name (TREE_OPERAND (expr, 1));
d17811fd
MM
2165 else if (TREE_CODE (TREE_OPERAND (expr, 1)) == TEMPLATE_ID_EXPR)
2166 {
2167 tree template_id;
2168 tree name;
2169
2170 template_id = TREE_OPERAND (expr, 1);
2171 name = TREE_OPERAND (template_id, 0);
2172 /* FIXME: What about operators? */
50bc768d 2173 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
d17811fd
MM
2174 write_source_name (TREE_OPERAND (template_id, 0));
2175 write_template_args (TREE_OPERAND (template_id, 1));
2176 }
1f6e1acc 2177 else
3fa3c4bd
MM
2178 {
2179 /* G++ 3.2 incorrectly put out both the "sr" code and
2180 the nested name of the qualified name. */
2181 G.need_abi_warning = 1;
2182 write_encoding (TREE_OPERAND (expr, 1));
2183 }
873ff987
KL
2184 break;
2185
2186 default:
2187 for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
18fd68a8
MM
2188 {
2189 tree operand = TREE_OPERAND (expr, i);
2190 /* As a GNU expression, the middle operand of a
2191 conditional may be omitted. Since expression
2192 manglings are supposed to represent the input token
2193 stream, there's no good way to mangle such an
2194 expression without extending the C++ ABI. */
2195 if (code == COND_EXPR && i == 1 && !operand)
2196 {
9e637a26 2197 error ("omitted middle operand to %<?:%> operand "
18fd68a8
MM
2198 "cannot be mangled");
2199 continue;
2200 }
2201 write_expression (operand);
2202 }
1f6e1acc 2203 }
1f6e1acc
AS
2204 }
2205}
2206
c8094d83 2207/* Literal subcase of non-terminal <template-arg>.
1f6e1acc
AS
2208
2209 "Literal arguments, e.g. "A<42L>", are encoded with their type
2210 and value. Negative integer values are preceded with "n"; for
2211 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
94d3ea58 2212 encoded as 0, true as 1." */
1f6e1acc
AS
2213
2214static void
89aec4b1 2215write_template_arg_literal (const tree value)
1f6e1acc 2216{
1f6e1acc 2217 write_char ('L');
8dc2b103 2218 write_type (TREE_TYPE (value));
1f6e1acc 2219
8dc2b103 2220 switch (TREE_CODE (value))
1f6e1acc 2221 {
8dc2b103
NS
2222 case CONST_DECL:
2223 write_integer_cst (DECL_INITIAL (value));
2224 break;
c8094d83 2225
8dc2b103
NS
2226 case INTEGER_CST:
2227 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
2228 || integer_zerop (value) || integer_onep (value));
2229 write_integer_cst (value);
2230 break;
2231
2232 case REAL_CST:
2233 write_real_cst (value);
2234 break;
1f6e1acc 2235
8dc2b103
NS
2236 default:
2237 gcc_unreachable ();
2238 }
c8094d83 2239
1f6e1acc
AS
2240 write_char ('E');
2241}
2242
c8094d83 2243/* Non-terminal <template-arg>.
1f6e1acc 2244
0cbd7506
MS
2245 <template-arg> ::= <type> # type
2246 ::= L <type> </value/ number> E # literal
2247 ::= LZ <name> E # external name
2248 ::= X <expression> E # expression */
1f6e1acc
AS
2249
2250static void
89aec4b1 2251write_template_arg (tree node)
1f6e1acc
AS
2252{
2253 enum tree_code code = TREE_CODE (node);
2254
2255 MANGLE_TRACE_TREE ("template-arg", node);
2256
da1d7781 2257 /* A template template parameter's argument list contains TREE_LIST
78dcd41a 2258 nodes of which the value field is the actual argument. */
1f6e1acc
AS
2259 if (code == TREE_LIST)
2260 {
2261 node = TREE_VALUE (node);
2262 /* If it's a decl, deal with its type instead. */
2263 if (DECL_P (node))
2264 {
2265 node = TREE_TYPE (node);
2266 code = TREE_CODE (node);
2267 }
2268 }
c8094d83 2269
0f30f285
NS
2270 if (TREE_CODE (node) == NOP_EXPR
2271 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
2272 {
2273 /* Template parameters can be of reference type. To maintain
2274 internal consistency, such arguments use a conversion from
2275 address of object to reference type. */
50bc768d 2276 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
0f30f285
NS
2277 if (abi_version_at_least (2))
2278 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
2279 else
2280 G.need_abi_warning = 1;
2281 }
1f6e1acc
AS
2282
2283 if (TYPE_P (node))
2284 write_type (node);
2285 else if (code == TEMPLATE_DECL)
2286 /* A template appearing as a template arg is a template template arg. */
2287 write_template_template_arg (node);
6615c446 2288 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
d3133e68
MM
2289 || (abi_version_at_least (2) && code == CONST_DECL))
2290 write_template_arg_literal (node);
1f6e1acc
AS
2291 else if (DECL_P (node))
2292 {
d9634d53 2293 /* Until ABI version 2, non-type template arguments of
c8094d83 2294 enumeration type were mangled using their names. */
d9634d53 2295 if (code == CONST_DECL && !abi_version_at_least (2))
d3133e68 2296 G.need_abi_warning = 1;
1f6e1acc 2297 write_char ('L');
d9634d53
MM
2298 /* Until ABI version 3, the underscore before the mangled name
2299 was incorrectly omitted. */
2300 if (!abi_version_at_least (3))
2301 {
2302 G.need_abi_warning = 1;
2303 write_char ('Z');
2304 }
2305 else
2306 write_string ("_Z");
1f6e1acc
AS
2307 write_encoding (node);
2308 write_char ('E');
2309 }
1f6e1acc
AS
2310 else
2311 {
2312 /* Template arguments may be expressions. */
2313 write_char ('X');
2314 write_expression (node);
2315 write_char ('E');
2316 }
2317}
2318
2319/* <template-template-arg>
2320 ::= <name>
2321 ::= <substitution> */
2322
8ce33230 2323static void
89aec4b1 2324write_template_template_arg (const tree decl)
1f6e1acc
AS
2325{
2326 MANGLE_TRACE_TREE ("template-template-arg", decl);
2327
2328 if (find_substitution (decl))
2329 return;
f0f33848 2330 write_name (decl, /*ignore_local_scope=*/0);
1f6e1acc
AS
2331 add_substitution (decl);
2332}
2333
2334
c8094d83 2335/* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
1f6e1acc 2336
c8094d83 2337 <array-type> ::= A [</dimension/ number>] _ </element/ type>
0cbd7506 2338 ::= A <expression> _ </element/ type>
1f6e1acc
AS
2339
2340 "Array types encode the dimension (number of elements) and the
2341 element type. For variable length arrays, the dimension (but not
2342 the '_' separator) is omitted." */
2343
89aec4b1
NN
2344static void
2345write_array_type (const tree type)
1f6e1acc
AS
2346{
2347 write_char ('A');
2348 if (TYPE_DOMAIN (type))
2349 {
2350 tree index_type;
2351 tree max;
2352
2353 index_type = TYPE_DOMAIN (type);
2354 /* The INDEX_TYPE gives the upper and lower bounds of the
2355 array. */
2356 max = TYPE_MAX_VALUE (index_type);
2357 if (TREE_CODE (max) == INTEGER_CST)
459c43ad
MM
2358 {
2359 /* The ABI specifies that we should mangle the number of
2360 elements in the array, not the largest allowed index. */
2361 max = size_binop (PLUS_EXPR, max, size_one_node);
2362 write_unsigned_number (tree_low_cst (max, 1));
2363 }
1f6e1acc 2364 else
d63b5e9f
NS
2365 {
2366 max = TREE_OPERAND (max, 0);
2367 if (!abi_version_at_least (2))
2368 {
2369 /* value_dependent_expression_p presumes nothing is
0cbd7506 2370 dependent when PROCESSING_TEMPLATE_DECL is zero. */
d63b5e9f
NS
2371 ++processing_template_decl;
2372 if (!value_dependent_expression_p (max))
2373 G.need_abi_warning = 1;
2374 --processing_template_decl;
2375 }
2376 write_expression (max);
2377 }
c8094d83 2378
1f6e1acc
AS
2379 }
2380 write_char ('_');
2381 write_type (TREE_TYPE (type));
2382}
2383
2384/* Non-terminal <pointer-to-member-type> for pointer-to-member
2385 variables. TYPE is a pointer-to-member POINTER_TYPE.
2386
2387 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
2388
2389static void
89aec4b1 2390write_pointer_to_member_type (const tree type)
1f6e1acc
AS
2391{
2392 write_char ('M');
6ea868b7 2393 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
1f6e1acc
AS
2394 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
2395}
2396
2397/* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
a1281f45
KL
2398 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
2399 TEMPLATE_PARM_INDEX.
1f6e1acc 2400
ee76b931 2401 <template-param> ::= T </parameter/ number> _ */
1f6e1acc
AS
2402
2403static void
89aec4b1 2404write_template_param (const tree parm)
1f6e1acc
AS
2405{
2406 int parm_index;
8c152bad
NS
2407 int parm_level;
2408 tree parm_type = NULL_TREE;
1f6e1acc
AS
2409
2410 MANGLE_TRACE_TREE ("template-parm", parm);
2411
2412 switch (TREE_CODE (parm))
2413 {
2414 case TEMPLATE_TYPE_PARM:
2415 case TEMPLATE_TEMPLATE_PARM:
a1281f45 2416 case BOUND_TEMPLATE_TEMPLATE_PARM:
1f6e1acc 2417 parm_index = TEMPLATE_TYPE_IDX (parm);
8c152bad 2418 parm_level = TEMPLATE_TYPE_LEVEL (parm);
1f6e1acc
AS
2419 break;
2420
2421 case TEMPLATE_PARM_INDEX:
2422 parm_index = TEMPLATE_PARM_IDX (parm);
8c152bad
NS
2423 parm_level = TEMPLATE_PARM_LEVEL (parm);
2424 parm_type = TREE_TYPE (TEMPLATE_PARM_DECL (parm));
1f6e1acc
AS
2425 break;
2426
2427 default:
8dc2b103 2428 gcc_unreachable ();
1f6e1acc
AS
2429 }
2430
2431 write_char ('T');
2432 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
2433 earliest template param denoted by `_'. */
2434 if (parm_index > 0)
82a362d0 2435 write_unsigned_number (parm_index - 1);
1f6e1acc
AS
2436 write_char ('_');
2437}
2438
2439/* <template-template-param>
0cbd7506 2440 ::= <template-param>
1f6e1acc
AS
2441 ::= <substitution> */
2442
2443static void
89aec4b1 2444write_template_template_param (const tree parm)
1f6e1acc
AS
2445{
2446 tree template = NULL_TREE;
2447
2448 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
2449 template template parameter. The substitution candidate here is
2450 only the template. */
a1281f45 2451 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
1f6e1acc 2452 {
c8094d83 2453 template
1f6e1acc
AS
2454 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
2455 if (find_substitution (template))
2456 return;
2457 }
2458
2459 /* <template-param> encodes only the template parameter position,
2460 not its template arguments, which is fine here. */
2461 write_template_param (parm);
2462 if (template)
2463 add_substitution (template);
2464}
2465
c8094d83 2466/* Non-terminal <substitution>.
1f6e1acc
AS
2467
2468 <substitution> ::= S <seq-id> _
0cbd7506 2469 ::= S_ */
1f6e1acc
AS
2470
2471static void
89aec4b1 2472write_substitution (const int seq_id)
1f6e1acc
AS
2473{
2474 MANGLE_TRACE ("substitution", "");
2475
2476 write_char ('S');
2477 if (seq_id > 0)
82a362d0 2478 write_number (seq_id - 1, /*unsigned=*/1, 36);
1f6e1acc
AS
2479 write_char ('_');
2480}
2481
6397d80b 2482/* Start mangling ENTITY. */
1f6e1acc
AS
2483
2484static inline void
2b8fe4a0 2485start_mangling (const tree entity, const bool ident_p)
1f6e1acc 2486{
6397d80b
MM
2487 G.entity = entity;
2488 G.need_abi_warning = false;
c8094d83 2489 if (!ident_p)
2b8fe4a0
MM
2490 {
2491 obstack_free (&name_obstack, name_base);
2492 mangle_obstack = &name_obstack;
2493 name_base = obstack_alloc (&name_obstack, 0);
2494 }
2495 else
2496 mangle_obstack = &ident_hash->stack;
1f6e1acc
AS
2497}
2498
6397d80b
MM
2499/* Done with mangling. Return the generated mangled name. If WARN is
2500 true, and the name of G.entity will be mangled differently in a
2501 future version of the ABI, issue a warning. */
1f6e1acc
AS
2502
2503static inline const char *
89aec4b1 2504finish_mangling (const bool warn)
1f6e1acc 2505{
6397d80b 2506 if (warn_abi && warn && G.need_abi_warning)
d4ee4d25 2507 warning (0, "the mangled name of %qD will change in a future "
6397d80b
MM
2508 "version of GCC",
2509 G.entity);
2510
1f6e1acc 2511 /* Clear all the substitutions. */
6e1141d7 2512 VEC_truncate (tree, G.substitutions, 0);
1f6e1acc
AS
2513
2514 /* Null-terminate the string. */
2515 write_char ('\0');
2516
2b8fe4a0 2517 return (const char *) obstack_finish (mangle_obstack);
1f6e1acc
AS
2518}
2519
2520/* Initialize data structures for mangling. */
2521
2522void
edaf3e03 2523init_mangle (void)
1f6e1acc 2524{
2b8fe4a0
MM
2525 gcc_obstack_init (&name_obstack);
2526 name_base = obstack_alloc (&name_obstack, 0);
6e1141d7 2527 G.substitutions = NULL;
1f6e1acc
AS
2528
2529 /* Cache these identifiers for quick comparison when checking for
2530 standard substitutions. */
2531 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
2532 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
2533 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
2534 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
2535 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
2536 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
2537}
2538
2539/* Generate the mangled name of DECL. */
2540
2541static const char *
89aec4b1 2542mangle_decl_string (const tree decl)
1f6e1acc
AS
2543{
2544 const char *result;
2545
2b8fe4a0 2546 start_mangling (decl, /*ident_p=*/true);
1f6e1acc
AS
2547
2548 if (TREE_CODE (decl) == TYPE_DECL)
2549 write_type (TREE_TYPE (decl));
2550 else
5fd80fbc 2551 write_mangled_name (decl, true);
c8094d83 2552
6397d80b 2553 result = finish_mangling (/*warn=*/true);
1f6e1acc
AS
2554 if (DEBUG_MANGLE)
2555 fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
2556 return result;
2557}
2558
2b8fe4a0
MM
2559/* Like get_identifier, except that NAME is assumed to have been
2560 allocated on the obstack used by the identifier hash table. */
2561
2562static inline tree
2563get_identifier_nocopy (const char *name)
2564{
c8094d83 2565 hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name,
2b8fe4a0
MM
2566 strlen (name), HT_ALLOCED);
2567 return HT_IDENT_TO_GCC_IDENT (ht_node);
2568}
2569
1f6e1acc
AS
2570/* Create an identifier for the external mangled name of DECL. */
2571
92643fea 2572void
89aec4b1 2573mangle_decl (const tree decl)
1f6e1acc 2574{
c8094d83 2575 SET_DECL_ASSEMBLER_NAME (decl,
2b8fe4a0 2576 get_identifier_nocopy (mangle_decl_string (decl)));
1f6e1acc
AS
2577}
2578
2579/* Generate the mangled representation of TYPE. */
2580
2581const char *
89aec4b1 2582mangle_type_string (const tree type)
1f6e1acc
AS
2583{
2584 const char *result;
2585
2b8fe4a0 2586 start_mangling (type, /*ident_p=*/false);
1f6e1acc 2587 write_type (type);
6397d80b 2588 result = finish_mangling (/*warn=*/false);
1f6e1acc
AS
2589 if (DEBUG_MANGLE)
2590 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
2591 return result;
2592}
2593
1f6e1acc
AS
2594/* Create an identifier for the mangled name of a special component
2595 for belonging to TYPE. CODE is the ABI-specified code for this
2596 component. */
2597
2598static tree
89aec4b1 2599mangle_special_for_type (const tree type, const char *code)
1f6e1acc
AS
2600{
2601 const char *result;
2602
2603 /* We don't have an actual decl here for the special component, so
2604 we can't just process the <encoded-name>. Instead, fake it. */
2b8fe4a0 2605 start_mangling (type, /*ident_p=*/true);
1f6e1acc
AS
2606
2607 /* Start the mangling. */
2608 write_string ("_Z");
2609 write_string (code);
2610
2611 /* Add the type. */
2612 write_type (type);
6397d80b 2613 result = finish_mangling (/*warn=*/false);
1f6e1acc
AS
2614
2615 if (DEBUG_MANGLE)
2616 fprintf (stderr, "mangle_special_for_type = %s\n\n", result);
2617
2b8fe4a0 2618 return get_identifier_nocopy (result);
1f6e1acc
AS
2619}
2620
2621/* Create an identifier for the mangled representation of the typeinfo
2622 structure for TYPE. */
2623
2624tree
89aec4b1 2625mangle_typeinfo_for_type (const tree type)
1f6e1acc
AS
2626{
2627 return mangle_special_for_type (type, "TI");
2628}
2629
2630/* Create an identifier for the mangled name of the NTBS containing
2631 the mangled name of TYPE. */
2632
2633tree
89aec4b1 2634mangle_typeinfo_string_for_type (const tree type)
1f6e1acc
AS
2635{
2636 return mangle_special_for_type (type, "TS");
2637}
2638
2639/* Create an identifier for the mangled name of the vtable for TYPE. */
2640
2641tree
89aec4b1 2642mangle_vtbl_for_type (const tree type)
1f6e1acc
AS
2643{
2644 return mangle_special_for_type (type, "TV");
2645}
2646
2647/* Returns an identifier for the mangled name of the VTT for TYPE. */
2648
2649tree
89aec4b1 2650mangle_vtt_for_type (const tree type)
1f6e1acc
AS
2651{
2652 return mangle_special_for_type (type, "TT");
2653}
2654
2655/* Return an identifier for a construction vtable group. TYPE is
2656 the most derived class in the hierarchy; BINFO is the base
c8094d83 2657 subobject for which this construction vtable group will be used.
1f6e1acc
AS
2658
2659 This mangling isn't part of the ABI specification; in the ABI
2660 specification, the vtable group is dumped in the same COMDAT as the
2661 main vtable, and is referenced only from that vtable, so it doesn't
2662 need an external name. For binary formats without COMDAT sections,
c8094d83 2663 though, we need external names for the vtable groups.
1f6e1acc
AS
2664
2665 We use the production
2666
2667 <special-name> ::= CT <type> <offset number> _ <base type> */
2668
2669tree
89aec4b1 2670mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
1f6e1acc
AS
2671{
2672 const char *result;
2673
2b8fe4a0 2674 start_mangling (type, /*ident_p=*/true);
1f6e1acc
AS
2675
2676 write_string ("_Z");
2677 write_string ("TC");
2678 write_type (type);
2679 write_integer_cst (BINFO_OFFSET (binfo));
2680 write_char ('_');
2681 write_type (BINFO_TYPE (binfo));
2682
6397d80b 2683 result = finish_mangling (/*warn=*/false);
1f6e1acc
AS
2684 if (DEBUG_MANGLE)
2685 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n", result);
2b8fe4a0 2686 return get_identifier_nocopy (result);
1f6e1acc
AS
2687}
2688
4977bab6 2689/* Mangle a this pointer or result pointer adjustment.
c8094d83 2690
4977bab6
ZW
2691 <call-offset> ::= h <fixed offset number> _
2692 ::= v <fixed offset number> _ <virtual offset number> _ */
c8094d83 2693
4977bab6 2694static void
89aec4b1 2695mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
4977bab6 2696{
07fa4878 2697 write_char (virtual_offset ? 'v' : 'h');
1f6e1acc 2698
4977bab6
ZW
2699 /* For either flavor, write the fixed offset. */
2700 write_integer_cst (fixed_offset);
2701 write_char ('_');
2702
2703 /* For a virtual thunk, add the virtual offset. */
2704 if (virtual_offset)
2705 {
2706 write_integer_cst (virtual_offset);
2707 write_char ('_');
2708 }
2709}
2710
2711/* Return an identifier for the mangled name of a this-adjusting or
2712 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
2713 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
2714 is a virtual thunk, and it is the vtbl offset in
4de8668e 2715 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
4977bab6
ZW
2716 zero for a covariant thunk. Note, that FN_DECL might be a covariant
2717 thunk itself. A covariant thunk name always includes the adjustment
2718 for the this pointer, even if there is none.
2719
2720 <special-name> ::= T <call-offset> <base encoding>
0cbd7506
MS
2721 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
2722 <base encoding>
1f6e1acc
AS
2723*/
2724
2725tree
89aec4b1
NN
2726mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
2727 tree virtual_offset)
1f6e1acc
AS
2728{
2729 const char *result;
c8094d83 2730
2b8fe4a0 2731 start_mangling (fn_decl, /*ident_p=*/true);
1f6e1acc
AS
2732
2733 write_string ("_Z");
1f6e1acc 2734 write_char ('T');
c8094d83 2735
07fa4878 2736 if (!this_adjusting)
4977bab6
ZW
2737 {
2738 /* Covariant thunk with no this adjustment */
2739 write_char ('c');
2740 mangle_call_offset (integer_zero_node, NULL_TREE);
2741 mangle_call_offset (fixed_offset, virtual_offset);
2742 }
07fa4878
NS
2743 else if (!DECL_THUNK_P (fn_decl))
2744 /* Plain this adjusting thunk. */
2745 mangle_call_offset (fixed_offset, virtual_offset);
1f6e1acc 2746 else
1f6e1acc 2747 {
4977bab6
ZW
2748 /* This adjusting thunk to covariant thunk. */
2749 write_char ('c');
2750 mangle_call_offset (fixed_offset, virtual_offset);
07fa4878
NS
2751 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
2752 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
2753 if (virtual_offset)
2754 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
2755 mangle_call_offset (fixed_offset, virtual_offset);
2756 fn_decl = THUNK_TARGET (fn_decl);
1f6e1acc
AS
2757 }
2758
2759 /* Scoped name. */
2760 write_encoding (fn_decl);
2761
6397d80b 2762 result = finish_mangling (/*warn=*/false);
1f6e1acc
AS
2763 if (DEBUG_MANGLE)
2764 fprintf (stderr, "mangle_thunk = %s\n\n", result);
2b8fe4a0 2765 return get_identifier_nocopy (result);
1f6e1acc
AS
2766}
2767
ca90f3e1 2768/* This hash table maps TYPEs to the IDENTIFIER for a conversion
c1eb7f5c
NS
2769 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
2770 TYPE. */
ca90f3e1
MM
2771
2772static GTY ((param_is (union tree_node))) htab_t conv_type_names;
2773
2774/* Hash a node (VAL1) in the table. */
2775
2776static hashval_t
2777hash_type (const void *val)
2778{
c1eb7f5c 2779 return (hashval_t) TYPE_UID (TREE_TYPE ((tree) val));
ca90f3e1
MM
2780}
2781
2782/* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
2783
2784static int
2785compare_type (const void *val1, const void *val2)
2786{
c1eb7f5c 2787 return TREE_TYPE ((tree) val1) == (tree) val2;
ca90f3e1
MM
2788}
2789
1f6e1acc
AS
2790/* Return an identifier for the mangled unqualified name for a
2791 conversion operator to TYPE. This mangling is not specified by the
6e1b44bc 2792 ABI spec; it is only used internally. */
1f6e1acc
AS
2793
2794tree
89aec4b1 2795mangle_conv_op_name_for_type (const tree type)
1f6e1acc 2796{
ca90f3e1 2797 void **slot;
1f6e1acc
AS
2798 tree identifier;
2799
c8094d83 2800 if (conv_type_names == NULL)
ca90f3e1
MM
2801 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
2802
c8094d83 2803 slot = htab_find_slot_with_hash (conv_type_names, type,
c1eb7f5c
NS
2804 (hashval_t) TYPE_UID (type), INSERT);
2805 identifier = (tree)*slot;
2806 if (!identifier)
2807 {
2808 char buffer[64];
c8094d83 2809
c1eb7f5c
NS
2810 /* Create a unique name corresponding to TYPE. */
2811 sprintf (buffer, "operator %lu",
2812 (unsigned long) htab_elements (conv_type_names));
2813 identifier = get_identifier (buffer);
2814 *slot = identifier;
2815
2816 /* Hang TYPE off the identifier so it can be found easily later
2817 when performing conversions. */
2818 TREE_TYPE (identifier) = type;
2819
2820 /* Set bits on the identifier so we know later it's a conversion. */
2821 IDENTIFIER_OPNAME_P (identifier) = 1;
2822 IDENTIFIER_TYPENAME_P (identifier) = 1;
2823 }
c8094d83 2824
1f6e1acc
AS
2825 return identifier;
2826}
2827
2828/* Return an identifier for the name of an initialization guard
2829 variable for indicated VARIABLE. */
2830
2831tree
89aec4b1 2832mangle_guard_variable (const tree variable)
1f6e1acc 2833{
2b8fe4a0 2834 start_mangling (variable, /*ident_p=*/true);
1f6e1acc 2835 write_string ("_ZGV");
08ac397c
JM
2836 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
2837 /* The name of a guard variable for a reference temporary should refer
2838 to the reference, not the temporary. */
2839 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
2840 else
2841 write_name (variable, /*ignore_local_scope=*/0);
2b8fe4a0 2842 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
1f6e1acc 2843}
23d4e4cc 2844
08ac397c
JM
2845/* Return an identifier for the name of a temporary variable used to
2846 initialize a static reference. This isn't part of the ABI, but we might
2847 as well call them something readable. */
2848
2849tree
89aec4b1 2850mangle_ref_init_variable (const tree variable)
08ac397c 2851{
2b8fe4a0 2852 start_mangling (variable, /*ident_p=*/true);
08ac397c
JM
2853 write_string ("_ZGR");
2854 write_name (variable, /*ignore_local_scope=*/0);
2b8fe4a0 2855 return get_identifier_nocopy (finish_mangling (/*warn=*/false));
08ac397c 2856}
23d4e4cc
APB
2857\f
2858
2859/* Foreign language type mangling section. */
2860
2861/* How to write the type codes for the integer Java type. */
2862
2863static void
89aec4b1 2864write_java_integer_type_codes (const tree type)
23d4e4cc
APB
2865{
2866 if (type == java_int_type_node)
2867 write_char ('i');
2868 else if (type == java_short_type_node)
2869 write_char ('s');
2870 else if (type == java_byte_type_node)
2871 write_char ('c');
2872 else if (type == java_char_type_node)
2873 write_char ('w');
2874 else if (type == java_long_type_node)
2875 write_char ('x');
2876 else if (type == java_boolean_type_node)
2877 write_char ('b');
2878 else
8dc2b103 2879 gcc_unreachable ();
23d4e4cc
APB
2880}
2881
0de298af 2882#include "gt-cp-mangle.h"
This page took 1.609188 seconds and 5 git commands to generate.