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