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