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