Commit the vtable verification feature.
[gcc.git] / gcc / cp / mangle.c
1 /* Name mangling for the 3.0 C++ ABI.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Alex Samuel <samuel@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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* This file implements mangling of C++ names according to the IA64
22 C++ ABI specification. A mangled name encodes a function or
23 variable's name, scope, type, and/or template arguments into a text
24 identifier. This identifier is used as the function's or
25 variable's linkage name, to preserve compatibility between C++'s
26 language features (templates, scoping, and overloading) and C
27 linkers.
28
29 Additionally, g++ uses mangled names internally. To support this,
30 mangling of types is allowed, even though the mangled name of a
31 type should not appear by itself as an exported name. Ditto for
32 uninstantiated templates.
33
34 The primary entry point for this module is mangle_decl, which
35 returns an identifier containing the mangled name for a decl.
36 Additional entry points are provided to build mangled names of
37 particular constructs when the appropriate decl for that construct
38 is not available. These are:
39
40 mangle_typeinfo_for_type: typeinfo data
41 mangle_typeinfo_string_for_type: typeinfo type name
42 mangle_vtbl_for_type: virtual table data
43 mangle_vtt_for_type: VTT data
44 mangle_ctor_vtbl_for_type: `C-in-B' constructor virtual table data
45 mangle_thunk: thunk function or entry */
46
47 #include "config.h"
48 #include "system.h"
49 #include "coretypes.h"
50 #include "tm.h"
51 #include "tree.h"
52 #include "tm_p.h"
53 #include "cp-tree.h"
54 #include "obstack.h"
55 #include "flags.h"
56 #include "target.h"
57 #include "cgraph.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) \
69 fprintf (stderr, " %-24s: %-24s\n", (FN), (INPUT))
70 # define MANGLE_TRACE_TREE(FN, NODE) \
71 fprintf (stderr, " %-24s: %-24s (%p)\n", \
72 (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
73 #else
74 # define MANGLE_TRACE(FN, INPUT)
75 # define MANGLE_TRACE_TREE(FN, NODE)
76 #endif
77
78 /* Nonzero 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
82 without parameters inside the template. */
83 #define CLASSTYPE_TEMPLATE_ID_P(NODE) \
84 (TYPE_LANG_SPECIFIC (NODE) != NULL \
85 && (TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM \
86 || (CLASSTYPE_TEMPLATE_INFO (NODE) != NULL \
87 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (NODE))))))
88
89 /* Things we only need one of. This module is not reentrant. */
90 typedef struct GTY(()) globals {
91 /* An array of the current substitution candidates, in the order
92 we've seen them. */
93 vec<tree, va_gc> *substitutions;
94
95 /* The entity that is being mangled. */
96 tree GTY ((skip)) entity;
97
98 /* How many parameter scopes we are inside. */
99 int parm_depth;
100
101 /* True if the mangling will be different in a future version of the
102 ABI. */
103 bool need_abi_warning;
104 } globals;
105
106 static GTY (()) globals G;
107
108 /* The obstack on which we build mangled names. */
109 static struct obstack *mangle_obstack;
110
111 /* The obstack on which we build mangled names that are not going to
112 be IDENTIFIER_NODEs. */
113 static struct obstack name_obstack;
114
115 /* The first object on the name_obstack; we use this to free memory
116 allocated on the name_obstack. */
117 static void *name_base;
118
119 /* Indices into subst_identifiers. These are identifiers used in
120 special substitution rules. */
121 typedef enum
122 {
123 SUBID_ALLOCATOR,
124 SUBID_BASIC_STRING,
125 SUBID_CHAR_TRAITS,
126 SUBID_BASIC_ISTREAM,
127 SUBID_BASIC_OSTREAM,
128 SUBID_BASIC_IOSTREAM,
129 SUBID_MAX
130 }
131 substitution_identifier_index_t;
132
133 /* For quick substitution checks, look up these common identifiers
134 once only. */
135 static GTY(()) tree subst_identifiers[SUBID_MAX];
136
137 /* Single-letter codes for builtin integer types, defined in
138 <builtin-type>. These are indexed by integer_type_kind values. */
139 static const char
140 integer_type_codes[itk_none] =
141 {
142 'c', /* itk_char */
143 'a', /* itk_signed_char */
144 'h', /* itk_unsigned_char */
145 's', /* itk_short */
146 't', /* itk_unsigned_short */
147 'i', /* itk_int */
148 'j', /* itk_unsigned_int */
149 'l', /* itk_long */
150 'm', /* itk_unsigned_long */
151 'x', /* itk_long_long */
152 'y', /* itk_unsigned_long_long */
153 'n', /* itk_int128 */
154 'o', /* itk_unsigned_int128 */
155 };
156
157 static int decl_is_template_id (const tree, tree* const);
158
159 /* Functions for handling substitutions. */
160
161 static inline tree canonicalize_for_substitution (tree);
162 static void add_substitution (tree);
163 static inline int is_std_substitution (const tree,
164 const substitution_identifier_index_t);
165 static inline int is_std_substitution_char (const tree,
166 const substitution_identifier_index_t);
167 static int find_substitution (tree);
168 static void mangle_call_offset (const tree, const tree);
169
170 /* Functions for emitting mangled representations of things. */
171
172 static void write_mangled_name (const tree, bool);
173 static void write_encoding (const tree);
174 static void write_name (tree, const int);
175 static void write_abi_tags (tree);
176 static void write_unscoped_name (const tree);
177 static void write_unscoped_template_name (const tree);
178 static void write_nested_name (const tree);
179 static void write_prefix (const tree);
180 static void write_template_prefix (const tree);
181 static void write_unqualified_name (const tree);
182 static void write_conversion_operator_name (const tree);
183 static void write_source_name (tree);
184 static void write_literal_operator_name (tree);
185 static void write_unnamed_type_name (const tree);
186 static void write_closure_type_name (const tree);
187 static int hwint_to_ascii (unsigned HOST_WIDE_INT, const unsigned int, char *,
188 const unsigned int);
189 static void write_number (unsigned HOST_WIDE_INT, const int,
190 const unsigned int);
191 static void write_compact_number (int num);
192 static void write_integer_cst (const tree);
193 static void write_real_cst (const tree);
194 static void write_identifier (const char *);
195 static void write_special_name_constructor (const tree);
196 static void write_special_name_destructor (const tree);
197 static void write_type (tree);
198 static int write_CV_qualifiers_for_type (const tree);
199 static void write_builtin_type (tree);
200 static void write_function_type (const tree);
201 static void write_bare_function_type (const tree, const int, const tree);
202 static void write_method_parms (tree, const int, const tree);
203 static void write_class_enum_type (const tree);
204 static void write_template_args (tree);
205 static void write_expression (tree);
206 static void write_template_arg_literal (const tree);
207 static void write_template_arg (tree);
208 static void write_template_template_arg (const tree);
209 static void write_array_type (const tree);
210 static void write_pointer_to_member_type (const tree);
211 static void write_template_param (const tree);
212 static void write_template_template_param (const tree);
213 static void write_substitution (const int);
214 static int discriminator_for_local_entity (tree);
215 static int discriminator_for_string_literal (tree, tree);
216 static void write_discriminator (const int);
217 static void write_local_name (tree, const tree, const tree);
218 static void dump_substitution_candidates (void);
219 static tree mangle_decl_string (const tree);
220 static int local_class_index (tree);
221
222 /* Control functions. */
223
224 static inline void start_mangling (const tree);
225 static inline const char *finish_mangling (const bool);
226 static tree mangle_special_for_type (const tree, const char *);
227
228 /* Foreign language functions. */
229
230 static void write_java_integer_type_codes (const tree);
231
232 /* Append a single character to the end of the mangled
233 representation. */
234 #define write_char(CHAR) \
235 obstack_1grow (mangle_obstack, (CHAR))
236
237 /* Append a sized buffer to the end of the mangled representation. */
238 #define write_chars(CHAR, LEN) \
239 obstack_grow (mangle_obstack, (CHAR), (LEN))
240
241 /* Append a NUL-terminated string to the end of the mangled
242 representation. */
243 #define write_string(STRING) \
244 obstack_grow (mangle_obstack, (STRING), strlen (STRING))
245
246 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
247 same purpose (context, which may be a type) and value (template
248 decl). See write_template_prefix for more information on what this
249 is used for. */
250 #define NESTED_TEMPLATE_MATCH(NODE1, NODE2) \
251 (TREE_CODE (NODE1) == TREE_LIST \
252 && TREE_CODE (NODE2) == TREE_LIST \
253 && ((TYPE_P (TREE_PURPOSE (NODE1)) \
254 && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2))) \
255 || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2)) \
256 && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
257
258 /* Write out an unsigned quantity in base 10. */
259 #define write_unsigned_number(NUMBER) \
260 write_number ((NUMBER), /*unsigned_p=*/1, 10)
261
262 /* If DECL is a template instance, return nonzero and, if
263 TEMPLATE_INFO is non-NULL, set *TEMPLATE_INFO to its template info.
264 Otherwise return zero. */
265
266 static int
267 decl_is_template_id (const tree decl, tree* const template_info)
268 {
269 if (TREE_CODE (decl) == TYPE_DECL)
270 {
271 /* TYPE_DECLs are handled specially. Look at its type to decide
272 if this is a template instantiation. */
273 const tree type = TREE_TYPE (decl);
274
275 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_ID_P (type))
276 {
277 if (template_info != NULL)
278 /* For a templated TYPE_DECL, the template info is hanging
279 off the type. */
280 *template_info = TYPE_TEMPLATE_INFO (type);
281 return 1;
282 }
283 }
284 else
285 {
286 /* Check if this is a primary template. */
287 if (DECL_LANG_SPECIFIC (decl) != NULL
288 && DECL_USE_TEMPLATE (decl)
289 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
290 && TREE_CODE (decl) != TEMPLATE_DECL)
291 {
292 if (template_info != NULL)
293 /* For most templated decls, the template info is hanging
294 off the decl. */
295 *template_info = DECL_TEMPLATE_INFO (decl);
296 return 1;
297 }
298 }
299
300 /* It's not a template id. */
301 return 0;
302 }
303
304 /* Produce debugging output of current substitution candidates. */
305
306 static void
307 dump_substitution_candidates (void)
308 {
309 unsigned i;
310 tree el;
311
312 fprintf (stderr, " ++ substitutions ");
313 FOR_EACH_VEC_ELT (*G.substitutions, i, el)
314 {
315 const char *name = "???";
316
317 if (i > 0)
318 fprintf (stderr, " ");
319 if (DECL_P (el))
320 name = IDENTIFIER_POINTER (DECL_NAME (el));
321 else if (TREE_CODE (el) == TREE_LIST)
322 name = IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (el)));
323 else if (TYPE_NAME (el))
324 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
325 fprintf (stderr, " S%d_ = ", i - 1);
326 if (TYPE_P (el) &&
327 (CP_TYPE_RESTRICT_P (el)
328 || CP_TYPE_VOLATILE_P (el)
329 || CP_TYPE_CONST_P (el)))
330 fprintf (stderr, "CV-");
331 fprintf (stderr, "%s (%s at %p)\n",
332 name, tree_code_name[TREE_CODE (el)], (void *) el);
333 }
334 }
335
336 /* Both decls and types can be substitution candidates, but sometimes
337 they refer to the same thing. For instance, a TYPE_DECL and
338 RECORD_TYPE for the same class refer to the same thing, and should
339 be treated accordingly in substitutions. This function returns a
340 canonicalized tree node representing NODE that is used when adding
341 and substitution candidates and finding matches. */
342
343 static inline tree
344 canonicalize_for_substitution (tree node)
345 {
346 /* For a TYPE_DECL, use the type instead. */
347 if (TREE_CODE (node) == TYPE_DECL)
348 node = TREE_TYPE (node);
349 if (TYPE_P (node)
350 && TYPE_CANONICAL (node) != node
351 && TYPE_MAIN_VARIANT (node) != node)
352 {
353 tree orig = node;
354 /* Here we want to strip the topmost typedef only.
355 We need to do that so is_std_substitution can do proper
356 name matching. */
357 if (TREE_CODE (node) == FUNCTION_TYPE)
358 /* Use build_qualified_type and TYPE_QUALS here to preserve
359 the old buggy mangling of attribute noreturn with abi<5. */
360 node = build_qualified_type (TYPE_MAIN_VARIANT (node),
361 TYPE_QUALS (node));
362 else
363 node = cp_build_qualified_type (TYPE_MAIN_VARIANT (node),
364 cp_type_quals (node));
365 if (TREE_CODE (node) == FUNCTION_TYPE
366 || TREE_CODE (node) == METHOD_TYPE)
367 node = build_ref_qualified_type (node, type_memfn_rqual (orig));
368 }
369 return node;
370 }
371
372 /* Add NODE as a substitution candidate. NODE must not already be on
373 the list of candidates. */
374
375 static void
376 add_substitution (tree node)
377 {
378 tree c;
379
380 if (DEBUG_MANGLE)
381 fprintf (stderr, " ++ add_substitution (%s at %10p)\n",
382 tree_code_name[TREE_CODE (node)], (void *) node);
383
384 /* Get the canonicalized substitution candidate for NODE. */
385 c = canonicalize_for_substitution (node);
386 if (DEBUG_MANGLE && c != node)
387 fprintf (stderr, " ++ using candidate (%s at %10p)\n",
388 tree_code_name[TREE_CODE (node)], (void *) node);
389 node = c;
390
391 #if ENABLE_CHECKING
392 /* Make sure NODE isn't already a candidate. */
393 {
394 int i;
395 tree candidate;
396
397 FOR_EACH_VEC_SAFE_ELT (G.substitutions, i, candidate)
398 {
399 gcc_assert (!(DECL_P (node) && node == candidate));
400 gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
401 && same_type_p (node, candidate)));
402 }
403 }
404 #endif /* ENABLE_CHECKING */
405
406 /* Put the decl onto the varray of substitution candidates. */
407 vec_safe_push (G.substitutions, node);
408
409 if (DEBUG_MANGLE)
410 dump_substitution_candidates ();
411 }
412
413 /* Helper function for find_substitution. Returns nonzero if NODE,
414 which may be a decl or a CLASS_TYPE, is a template-id with template
415 name of substitution_index[INDEX] in the ::std namespace. */
416
417 static inline int
418 is_std_substitution (const tree node,
419 const substitution_identifier_index_t index)
420 {
421 tree type = NULL;
422 tree decl = NULL;
423
424 if (DECL_P (node))
425 {
426 type = TREE_TYPE (node);
427 decl = node;
428 }
429 else if (CLASS_TYPE_P (node))
430 {
431 type = node;
432 decl = TYPE_NAME (node);
433 }
434 else
435 /* These are not the droids you're looking for. */
436 return 0;
437
438 return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
439 && TYPE_LANG_SPECIFIC (type)
440 && TYPE_TEMPLATE_INFO (type)
441 && (DECL_NAME (TYPE_TI_TEMPLATE (type))
442 == subst_identifiers[index]));
443 }
444
445 /* Helper function for find_substitution. Returns nonzero if NODE,
446 which may be a decl or a CLASS_TYPE, is the template-id
447 ::std::identifier<char>, where identifier is
448 substitution_index[INDEX]. */
449
450 static inline int
451 is_std_substitution_char (const tree node,
452 const substitution_identifier_index_t index)
453 {
454 tree args;
455 /* Check NODE's name is ::std::identifier. */
456 if (!is_std_substitution (node, index))
457 return 0;
458 /* Figure out its template args. */
459 if (DECL_P (node))
460 args = DECL_TI_ARGS (node);
461 else if (CLASS_TYPE_P (node))
462 args = CLASSTYPE_TI_ARGS (node);
463 else
464 /* Oops, not a template. */
465 return 0;
466 /* NODE's template arg list should be <char>. */
467 return
468 TREE_VEC_LENGTH (args) == 1
469 && TREE_VEC_ELT (args, 0) == char_type_node;
470 }
471
472 /* Check whether a substitution should be used to represent NODE in
473 the mangling.
474
475 First, check standard special-case substitutions.
476
477 <substitution> ::= St
478 # ::std
479
480 ::= Sa
481 # ::std::allocator
482
483 ::= Sb
484 # ::std::basic_string
485
486 ::= Ss
487 # ::std::basic_string<char,
488 ::std::char_traits<char>,
489 ::std::allocator<char> >
490
491 ::= Si
492 # ::std::basic_istream<char, ::std::char_traits<char> >
493
494 ::= So
495 # ::std::basic_ostream<char, ::std::char_traits<char> >
496
497 ::= Sd
498 # ::std::basic_iostream<char, ::std::char_traits<char> >
499
500 Then examine the stack of currently available substitution
501 candidates for entities appearing earlier in the same mangling
502
503 If a substitution is found, write its mangled representation and
504 return nonzero. If none is found, just return zero. */
505
506 static int
507 find_substitution (tree node)
508 {
509 int i;
510 const int size = vec_safe_length (G.substitutions);
511 tree decl;
512 tree type;
513
514 if (DEBUG_MANGLE)
515 fprintf (stderr, " ++ find_substitution (%s at %p)\n",
516 tree_code_name[TREE_CODE (node)], (void *) node);
517
518 /* Obtain the canonicalized substitution representation for NODE.
519 This is what we'll compare against. */
520 node = canonicalize_for_substitution (node);
521
522 /* Check for builtin substitutions. */
523
524 decl = TYPE_P (node) ? TYPE_NAME (node) : node;
525 type = TYPE_P (node) ? node : TREE_TYPE (node);
526
527 /* Check for std::allocator. */
528 if (decl
529 && is_std_substitution (decl, SUBID_ALLOCATOR)
530 && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
531 {
532 write_string ("Sa");
533 return 1;
534 }
535
536 /* Check for std::basic_string. */
537 if (decl && is_std_substitution (decl, SUBID_BASIC_STRING))
538 {
539 if (TYPE_P (node))
540 {
541 /* If this is a type (i.e. a fully-qualified template-id),
542 check for
543 std::basic_string <char,
544 std::char_traits<char>,
545 std::allocator<char> > . */
546 if (cp_type_quals (type) == TYPE_UNQUALIFIED
547 && CLASSTYPE_USE_TEMPLATE (type))
548 {
549 tree args = CLASSTYPE_TI_ARGS (type);
550 if (TREE_VEC_LENGTH (args) == 3
551 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
552 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
553 SUBID_CHAR_TRAITS)
554 && is_std_substitution_char (TREE_VEC_ELT (args, 2),
555 SUBID_ALLOCATOR))
556 {
557 write_string ("Ss");
558 return 1;
559 }
560 }
561 }
562 else
563 /* Substitute for the template name only if this isn't a type. */
564 {
565 write_string ("Sb");
566 return 1;
567 }
568 }
569
570 /* Check for basic_{i,o,io}stream. */
571 if (TYPE_P (node)
572 && cp_type_quals (type) == TYPE_UNQUALIFIED
573 && CLASS_TYPE_P (type)
574 && CLASSTYPE_USE_TEMPLATE (type)
575 && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
576 {
577 /* First, check for the template
578 args <char, std::char_traits<char> > . */
579 tree args = CLASSTYPE_TI_ARGS (type);
580 if (TREE_VEC_LENGTH (args) == 2
581 && TYPE_P (TREE_VEC_ELT (args, 0))
582 && same_type_p (TREE_VEC_ELT (args, 0), char_type_node)
583 && is_std_substitution_char (TREE_VEC_ELT (args, 1),
584 SUBID_CHAR_TRAITS))
585 {
586 /* Got them. Is this basic_istream? */
587 if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
588 {
589 write_string ("Si");
590 return 1;
591 }
592 /* Or basic_ostream? */
593 else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
594 {
595 write_string ("So");
596 return 1;
597 }
598 /* Or basic_iostream? */
599 else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
600 {
601 write_string ("Sd");
602 return 1;
603 }
604 }
605 }
606
607 /* Check for namespace std. */
608 if (decl && DECL_NAMESPACE_STD_P (decl))
609 {
610 write_string ("St");
611 return 1;
612 }
613
614 /* Now check the list of available substitutions for this mangling
615 operation. */
616 for (i = 0; i < size; ++i)
617 {
618 tree candidate = (*G.substitutions)[i];
619 /* NODE is a matched to a candidate if it's the same decl node or
620 if it's the same type. */
621 if (decl == candidate
622 || (TYPE_P (candidate) && type && TYPE_P (node)
623 && same_type_p (type, candidate))
624 || NESTED_TEMPLATE_MATCH (node, candidate))
625 {
626 write_substitution (i);
627 return 1;
628 }
629 }
630
631 /* No substitution found. */
632 return 0;
633 }
634
635
636 /* TOP_LEVEL is true, if this is being called at outermost level of
637 mangling. It should be false when mangling a decl appearing in an
638 expression within some other mangling.
639
640 <mangled-name> ::= _Z <encoding> */
641
642 static void
643 write_mangled_name (const tree decl, bool top_level)
644 {
645 MANGLE_TRACE_TREE ("mangled-name", decl);
646
647 if (/* The names of `extern "C"' functions are not mangled. */
648 DECL_EXTERN_C_FUNCTION_P (decl)
649 /* But overloaded operator names *are* mangled. */
650 && !DECL_OVERLOADED_OPERATOR_P (decl))
651 {
652 unmangled_name:;
653
654 if (top_level)
655 write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
656 else
657 {
658 /* The standard notes: "The <encoding> of an extern "C"
659 function is treated like global-scope data, i.e. as its
660 <source-name> without a type." We cannot write
661 overloaded operators that way though, because it contains
662 characters invalid in assembler. */
663 if (abi_version_at_least (2))
664 write_string ("_Z");
665 else
666 G.need_abi_warning = true;
667 write_source_name (DECL_NAME (decl));
668 }
669 }
670 else if (VAR_P (decl)
671 /* The names of non-static global variables aren't mangled. */
672 && DECL_EXTERNAL_LINKAGE_P (decl)
673 && (CP_DECL_CONTEXT (decl) == global_namespace
674 /* And neither are `extern "C"' variables. */
675 || DECL_EXTERN_C_P (decl)))
676 {
677 if (top_level || abi_version_at_least (2))
678 goto unmangled_name;
679 else
680 {
681 G.need_abi_warning = true;
682 goto mangled_name;
683 }
684 }
685 else
686 {
687 mangled_name:;
688 write_string ("_Z");
689 write_encoding (decl);
690 if (DECL_LANG_SPECIFIC (decl)
691 && (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
692 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)))
693 /* We need a distinct mangled name for these entities, but
694 we should never actually output it. So, we append some
695 characters the assembler won't like. */
696 write_string (" *INTERNAL* ");
697 }
698 }
699
700 /* <encoding> ::= <function name> <bare-function-type>
701 ::= <data name> */
702
703 static void
704 write_encoding (const tree decl)
705 {
706 MANGLE_TRACE_TREE ("encoding", decl);
707
708 if (DECL_LANG_SPECIFIC (decl) && DECL_EXTERN_C_FUNCTION_P (decl))
709 {
710 /* For overloaded operators write just the mangled name
711 without arguments. */
712 if (DECL_OVERLOADED_OPERATOR_P (decl))
713 write_name (decl, /*ignore_local_scope=*/0);
714 else
715 write_source_name (DECL_NAME (decl));
716 return;
717 }
718
719 write_name (decl, /*ignore_local_scope=*/0);
720 if (TREE_CODE (decl) == FUNCTION_DECL)
721 {
722 tree fn_type;
723 tree d;
724
725 if (decl_is_template_id (decl, NULL))
726 {
727 fn_type = get_mostly_instantiated_function_type (decl);
728 /* FN_TYPE will not have parameter types for in-charge or
729 VTT parameters. Therefore, we pass NULL_TREE to
730 write_bare_function_type -- otherwise, it will get
731 confused about which artificial parameters to skip. */
732 d = NULL_TREE;
733 }
734 else
735 {
736 fn_type = TREE_TYPE (decl);
737 d = decl;
738 }
739
740 write_bare_function_type (fn_type,
741 (!DECL_CONSTRUCTOR_P (decl)
742 && !DECL_DESTRUCTOR_P (decl)
743 && !DECL_CONV_FN_P (decl)
744 && decl_is_template_id (decl, NULL)),
745 d);
746 }
747 }
748
749 /* Lambdas can have a bit more context for mangling, specifically VAR_DECL
750 or PARM_DECL context, which doesn't belong in DECL_CONTEXT. */
751
752 static tree
753 decl_mangling_context (tree decl)
754 {
755 tree tcontext = targetm.cxx.decl_mangling_context (decl);
756
757 if (tcontext != NULL_TREE)
758 return tcontext;
759
760 if (TREE_CODE (decl) == TYPE_DECL
761 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
762 {
763 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
764 if (extra)
765 return extra;
766 }
767 else if (TREE_CODE (decl) == TYPE_DECL
768 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
769 /* template type parms have no mangling context. */
770 return NULL_TREE;
771 return CP_DECL_CONTEXT (decl);
772 }
773
774 /* <name> ::= <unscoped-name>
775 ::= <unscoped-template-name> <template-args>
776 ::= <nested-name>
777 ::= <local-name>
778
779 If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
780 called from <local-name>, which mangles the enclosing scope
781 elsewhere and then uses this function to mangle just the part
782 underneath the function scope. So don't use the <local-name>
783 production, to avoid an infinite recursion. */
784
785 static void
786 write_name (tree decl, const int ignore_local_scope)
787 {
788 tree context;
789
790 MANGLE_TRACE_TREE ("name", decl);
791
792 if (TREE_CODE (decl) == TYPE_DECL)
793 {
794 /* In case this is a typedef, fish out the corresponding
795 TYPE_DECL for the main variant. */
796 decl = TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));
797 }
798
799 context = decl_mangling_context (decl);
800
801 gcc_assert (context != NULL_TREE);
802
803 /* A decl in :: or ::std scope is treated specially. The former is
804 mangled using <unscoped-name> or <unscoped-template-name>, the
805 latter with a special substitution. Also, a name that is
806 directly in a local function scope is also mangled with
807 <unscoped-name> rather than a full <nested-name>. */
808 if (context == global_namespace
809 || DECL_NAMESPACE_STD_P (context)
810 || (ignore_local_scope
811 && (TREE_CODE (context) == FUNCTION_DECL
812 || (abi_version_at_least (7)
813 && TREE_CODE (context) == PARM_DECL))))
814 {
815 tree template_info;
816 /* Is this a template instance? */
817 if (decl_is_template_id (decl, &template_info))
818 {
819 /* Yes: use <unscoped-template-name>. */
820 write_unscoped_template_name (TI_TEMPLATE (template_info));
821 write_template_args (TI_ARGS (template_info));
822 }
823 else
824 /* Everything else gets an <unqualified-name>. */
825 write_unscoped_name (decl);
826 }
827 else
828 {
829 /* Handle local names, unless we asked not to (that is, invoked
830 under <local-name>, to handle only the part of the name under
831 the local scope). */
832 if (!ignore_local_scope)
833 {
834 /* Scan up the list of scope context, looking for a
835 function. If we find one, this entity is in local
836 function scope. local_entity tracks context one scope
837 level down, so it will contain the element that's
838 directly in that function's scope, either decl or one of
839 its enclosing scopes. */
840 tree local_entity = decl;
841 while (context != global_namespace)
842 {
843 /* Make sure we're always dealing with decls. */
844 if (TYPE_P (context))
845 context = TYPE_NAME (context);
846 /* Is this a function? */
847 if (TREE_CODE (context) == FUNCTION_DECL
848 || TREE_CODE (context) == PARM_DECL)
849 {
850 /* Yes, we have local scope. Use the <local-name>
851 production for the innermost function scope. */
852 write_local_name (context, local_entity, decl);
853 return;
854 }
855 /* Up one scope level. */
856 local_entity = context;
857 context = decl_mangling_context (context);
858 }
859
860 /* No local scope found? Fall through to <nested-name>. */
861 }
862
863 /* Other decls get a <nested-name> to encode their scope. */
864 write_nested_name (decl);
865 }
866 }
867
868 /* <unscoped-name> ::= <unqualified-name>
869 ::= St <unqualified-name> # ::std:: */
870
871 static void
872 write_unscoped_name (const tree decl)
873 {
874 tree context = decl_mangling_context (decl);
875
876 MANGLE_TRACE_TREE ("unscoped-name", decl);
877
878 /* Is DECL in ::std? */
879 if (DECL_NAMESPACE_STD_P (context))
880 {
881 write_string ("St");
882 write_unqualified_name (decl);
883 }
884 else
885 {
886 /* If not, it should be either in the global namespace, or directly
887 in a local function scope. A lambda can also be mangled in the
888 scope of a default argument. */
889 gcc_assert (context == global_namespace
890 || TREE_CODE (context) == PARM_DECL
891 || TREE_CODE (context) == FUNCTION_DECL);
892
893 write_unqualified_name (decl);
894 }
895 }
896
897 /* <unscoped-template-name> ::= <unscoped-name>
898 ::= <substitution> */
899
900 static void
901 write_unscoped_template_name (const tree decl)
902 {
903 MANGLE_TRACE_TREE ("unscoped-template-name", decl);
904
905 if (find_substitution (decl))
906 return;
907 write_unscoped_name (decl);
908 add_substitution (decl);
909 }
910
911 /* Write the nested name, including CV-qualifiers, of DECL.
912
913 <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
914 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
915
916 <ref-qualifier> ::= R # & ref-qualifier
917 ::= O # && ref-qualifier
918 <CV-qualifiers> ::= [r] [V] [K] */
919
920 static void
921 write_nested_name (const tree decl)
922 {
923 tree template_info;
924
925 MANGLE_TRACE_TREE ("nested-name", decl);
926
927 write_char ('N');
928
929 /* Write CV-qualifiers, if this is a member function. */
930 if (TREE_CODE (decl) == FUNCTION_DECL
931 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
932 {
933 if (DECL_VOLATILE_MEMFUNC_P (decl))
934 write_char ('V');
935 if (DECL_CONST_MEMFUNC_P (decl))
936 write_char ('K');
937 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)))
938 {
939 if (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
940 write_char ('O');
941 else
942 write_char ('R');
943 }
944 }
945
946 /* Is this a template instance? */
947 if (decl_is_template_id (decl, &template_info))
948 {
949 /* Yes, use <template-prefix>. */
950 write_template_prefix (decl);
951 write_template_args (TI_ARGS (template_info));
952 }
953 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
954 {
955 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
956 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
957 {
958 write_template_prefix (decl);
959 write_template_args (TREE_OPERAND (name, 1));
960 }
961 else
962 {
963 write_prefix (decl_mangling_context (decl));
964 write_unqualified_name (decl);
965 }
966 }
967 else
968 {
969 /* No, just use <prefix> */
970 write_prefix (decl_mangling_context (decl));
971 write_unqualified_name (decl);
972 }
973 write_char ('E');
974 }
975
976 /* <prefix> ::= <prefix> <unqualified-name>
977 ::= <template-param>
978 ::= <template-prefix> <template-args>
979 ::= <decltype>
980 ::= # empty
981 ::= <substitution> */
982
983 static void
984 write_prefix (const tree node)
985 {
986 tree decl;
987 /* Non-NULL if NODE represents a template-id. */
988 tree template_info = NULL;
989
990 if (node == NULL
991 || node == global_namespace)
992 return;
993
994 MANGLE_TRACE_TREE ("prefix", node);
995
996 if (TREE_CODE (node) == DECLTYPE_TYPE)
997 {
998 write_type (node);
999 return;
1000 }
1001
1002 if (find_substitution (node))
1003 return;
1004
1005 if (DECL_P (node))
1006 {
1007 /* If this is a function or parm decl, that means we've hit function
1008 scope, so this prefix must be for a local name. In this
1009 case, we're under the <local-name> production, which encodes
1010 the enclosing function scope elsewhere. So don't continue
1011 here. */
1012 if (TREE_CODE (node) == FUNCTION_DECL
1013 || TREE_CODE (node) == PARM_DECL)
1014 return;
1015
1016 decl = node;
1017 decl_is_template_id (decl, &template_info);
1018 }
1019 else
1020 {
1021 /* Node is a type. */
1022 decl = TYPE_NAME (node);
1023 if (CLASSTYPE_TEMPLATE_ID_P (node))
1024 template_info = TYPE_TEMPLATE_INFO (node);
1025 }
1026
1027 /* In G++ 3.2, the name of the template parameter was used. */
1028 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1029 && !abi_version_at_least (2))
1030 G.need_abi_warning = true;
1031
1032 if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
1033 && abi_version_at_least (2))
1034 write_template_param (node);
1035 else if (template_info != NULL)
1036 /* Templated. */
1037 {
1038 write_template_prefix (decl);
1039 write_template_args (TI_ARGS (template_info));
1040 }
1041 else if (TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
1042 {
1043 tree name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (decl));
1044 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1045 {
1046 write_template_prefix (decl);
1047 write_template_args (TREE_OPERAND (name, 1));
1048 }
1049 else
1050 {
1051 write_prefix (decl_mangling_context (decl));
1052 write_unqualified_name (decl);
1053 }
1054 }
1055 else
1056 /* Not templated. */
1057 {
1058 write_prefix (decl_mangling_context (decl));
1059 write_unqualified_name (decl);
1060 if (VAR_P (decl)
1061 || TREE_CODE (decl) == FIELD_DECL)
1062 {
1063 /* <data-member-prefix> := <member source-name> M */
1064 write_char ('M');
1065 return;
1066 }
1067 }
1068
1069 add_substitution (node);
1070 }
1071
1072 /* <template-prefix> ::= <prefix> <template component>
1073 ::= <template-param>
1074 ::= <substitution> */
1075
1076 static void
1077 write_template_prefix (const tree node)
1078 {
1079 tree decl = DECL_P (node) ? node : TYPE_NAME (node);
1080 tree type = DECL_P (node) ? TREE_TYPE (node) : node;
1081 tree context = decl_mangling_context (decl);
1082 tree template_info;
1083 tree templ;
1084 tree substitution;
1085
1086 MANGLE_TRACE_TREE ("template-prefix", node);
1087
1088 /* Find the template decl. */
1089 if (decl_is_template_id (decl, &template_info))
1090 templ = TI_TEMPLATE (template_info);
1091 else if (TREE_CODE (type) == TYPENAME_TYPE)
1092 /* For a typename type, all we have is the name. */
1093 templ = DECL_NAME (decl);
1094 else
1095 {
1096 gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
1097
1098 templ = TYPE_TI_TEMPLATE (type);
1099 }
1100
1101 /* For a member template, though, the template name for the
1102 innermost name must have all the outer template levels
1103 instantiated. For instance, consider
1104
1105 template<typename T> struct Outer {
1106 template<typename U> struct Inner {};
1107 };
1108
1109 The template name for `Inner' in `Outer<int>::Inner<float>' is
1110 `Outer<int>::Inner<U>'. In g++, we don't instantiate the template
1111 levels separately, so there's no TEMPLATE_DECL available for this
1112 (there's only `Outer<T>::Inner<U>').
1113
1114 In order to get the substitutions right, we create a special
1115 TREE_LIST to represent the substitution candidate for a nested
1116 template. The TREE_PURPOSE is the template's context, fully
1117 instantiated, and the TREE_VALUE is the TEMPLATE_DECL for the inner
1118 template.
1119
1120 So, for the example above, `Outer<int>::Inner' is represented as a
1121 substitution candidate by a TREE_LIST whose purpose is `Outer<int>'
1122 and whose value is `Outer<T>::Inner<U>'. */
1123 if (TYPE_P (context))
1124 substitution = build_tree_list (context, templ);
1125 else
1126 substitution = templ;
1127
1128 if (find_substitution (substitution))
1129 return;
1130
1131 /* In G++ 3.2, the name of the template template parameter was used. */
1132 if (TREE_TYPE (templ)
1133 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1134 && !abi_version_at_least (2))
1135 G.need_abi_warning = true;
1136
1137 if (TREE_TYPE (templ)
1138 && TREE_CODE (TREE_TYPE (templ)) == TEMPLATE_TEMPLATE_PARM
1139 && abi_version_at_least (2))
1140 write_template_param (TREE_TYPE (templ));
1141 else
1142 {
1143 write_prefix (context);
1144 write_unqualified_name (decl);
1145 }
1146
1147 add_substitution (substitution);
1148 }
1149
1150 /* We don't need to handle thunks, vtables, or VTTs here. Those are
1151 mangled through special entry points.
1152
1153 <unqualified-name> ::= <operator-name>
1154 ::= <special-name>
1155 ::= <source-name>
1156 ::= <unnamed-type-name>
1157 ::= <local-source-name>
1158
1159 <local-source-name> ::= L <source-name> <discriminator> */
1160
1161 static void
1162 write_unqualified_id (tree identifier)
1163 {
1164 if (IDENTIFIER_TYPENAME_P (identifier))
1165 write_conversion_operator_name (TREE_TYPE (identifier));
1166 else if (IDENTIFIER_OPNAME_P (identifier))
1167 {
1168 int i;
1169 const char *mangled_name = NULL;
1170
1171 /* Unfortunately, there is no easy way to go from the
1172 name of the operator back to the corresponding tree
1173 code. */
1174 for (i = 0; i < MAX_TREE_CODES; ++i)
1175 if (operator_name_info[i].identifier == identifier)
1176 {
1177 /* The ABI says that we prefer binary operator
1178 names to unary operator names. */
1179 if (operator_name_info[i].arity == 2)
1180 {
1181 mangled_name = operator_name_info[i].mangled_name;
1182 break;
1183 }
1184 else if (!mangled_name)
1185 mangled_name = operator_name_info[i].mangled_name;
1186 }
1187 else if (assignment_operator_name_info[i].identifier
1188 == identifier)
1189 {
1190 mangled_name
1191 = assignment_operator_name_info[i].mangled_name;
1192 break;
1193 }
1194 write_string (mangled_name);
1195 }
1196 else if (UDLIT_OPER_P (identifier))
1197 write_literal_operator_name (identifier);
1198 else
1199 write_source_name (identifier);
1200 }
1201
1202 static void
1203 write_unqualified_name (const tree decl)
1204 {
1205 MANGLE_TRACE_TREE ("unqualified-name", decl);
1206
1207 if (identifier_p (decl))
1208 {
1209 write_unqualified_id (decl);
1210 return;
1211 }
1212
1213 bool found = false;
1214
1215 if (DECL_NAME (decl) == NULL_TREE)
1216 {
1217 found = true;
1218 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1219 write_source_name (DECL_ASSEMBLER_NAME (decl));
1220 }
1221 else if (DECL_DECLARES_FUNCTION_P (decl))
1222 {
1223 found = true;
1224 if (DECL_CONSTRUCTOR_P (decl))
1225 write_special_name_constructor (decl);
1226 else if (DECL_DESTRUCTOR_P (decl))
1227 write_special_name_destructor (decl);
1228 else if (DECL_CONV_FN_P (decl))
1229 {
1230 /* Conversion operator. Handle it right here.
1231 <operator> ::= cv <type> */
1232 tree type;
1233 if (decl_is_template_id (decl, NULL))
1234 {
1235 tree fn_type;
1236 fn_type = get_mostly_instantiated_function_type (decl);
1237 type = TREE_TYPE (fn_type);
1238 }
1239 else
1240 type = DECL_CONV_FN_TYPE (decl);
1241 write_conversion_operator_name (type);
1242 }
1243 else if (DECL_OVERLOADED_OPERATOR_P (decl))
1244 {
1245 operator_name_info_t *oni;
1246 if (DECL_ASSIGNMENT_OPERATOR_P (decl))
1247 oni = assignment_operator_name_info;
1248 else
1249 oni = operator_name_info;
1250
1251 write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
1252 }
1253 else if (UDLIT_OPER_P (DECL_NAME (decl)))
1254 write_literal_operator_name (DECL_NAME (decl));
1255 else
1256 found = false;
1257 }
1258
1259 if (found)
1260 /* OK */;
1261 else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
1262 && DECL_NAMESPACE_SCOPE_P (decl)
1263 && decl_linkage (decl) == lk_internal)
1264 {
1265 MANGLE_TRACE_TREE ("local-source-name", decl);
1266 write_char ('L');
1267 write_source_name (DECL_NAME (decl));
1268 /* The default discriminator is 1, and that's all we ever use,
1269 so there's no code to output one here. */
1270 }
1271 else
1272 {
1273 tree type = TREE_TYPE (decl);
1274
1275 if (TREE_CODE (decl) == TYPE_DECL
1276 && TYPE_ANONYMOUS_P (type))
1277 write_unnamed_type_name (type);
1278 else if (TREE_CODE (decl) == TYPE_DECL
1279 && LAMBDA_TYPE_P (type))
1280 write_closure_type_name (type);
1281 else
1282 write_source_name (DECL_NAME (decl));
1283 }
1284
1285 tree attrs = (TREE_CODE (decl) == TYPE_DECL
1286 ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
1287 : DECL_ATTRIBUTES (decl));
1288 write_abi_tags (lookup_attribute ("abi_tag", attrs));
1289 }
1290
1291 /* Write the unqualified-name for a conversion operator to TYPE. */
1292
1293 static void
1294 write_conversion_operator_name (const tree type)
1295 {
1296 write_string ("cv");
1297 write_type (type);
1298 }
1299
1300 /* Non-terminal <source-name>. IDENTIFIER is an IDENTIFIER_NODE.
1301
1302 <source-name> ::= </length/ number> <identifier> */
1303
1304 static void
1305 write_source_name (tree identifier)
1306 {
1307 MANGLE_TRACE_TREE ("source-name", identifier);
1308
1309 /* Never write the whole template-id name including the template
1310 arguments; we only want the template name. */
1311 if (IDENTIFIER_TEMPLATE (identifier))
1312 identifier = IDENTIFIER_TEMPLATE (identifier);
1313
1314 write_unsigned_number (IDENTIFIER_LENGTH (identifier));
1315 write_identifier (IDENTIFIER_POINTER (identifier));
1316 }
1317
1318 /* Compare two TREE_STRINGs like strcmp. */
1319
1320 int
1321 tree_string_cmp (const void *p1, const void *p2)
1322 {
1323 if (p1 == p2)
1324 return 0;
1325 tree s1 = *(const tree*)p1;
1326 tree s2 = *(const tree*)p2;
1327 return strcmp (TREE_STRING_POINTER (s1),
1328 TREE_STRING_POINTER (s2));
1329 }
1330
1331 /* ID is the name of a function or type with abi_tags attribute TAGS.
1332 Write out the name, suitably decorated. */
1333
1334 static void
1335 write_abi_tags (tree tags)
1336 {
1337 if (tags == NULL_TREE)
1338 return;
1339
1340 tags = TREE_VALUE (tags);
1341
1342 vec<tree, va_gc> * vec = make_tree_vector();
1343
1344 for (tree t = tags; t; t = TREE_CHAIN (t))
1345 {
1346 tree str = TREE_VALUE (t);
1347 vec_safe_push (vec, str);
1348 }
1349
1350 vec->qsort (tree_string_cmp);
1351
1352 unsigned i; tree str;
1353 FOR_EACH_VEC_ELT (*vec, i, str)
1354 {
1355 write_string ("B");
1356 write_unsigned_number (TREE_STRING_LENGTH (str) - 1);
1357 write_identifier (TREE_STRING_POINTER (str));
1358 }
1359
1360 release_tree_vector (vec);
1361 }
1362
1363 /* Write a user-defined literal operator.
1364 ::= li <source-name> # "" <source-name>
1365 IDENTIFIER is an LITERAL_IDENTIFIER_NODE. */
1366
1367 static void
1368 write_literal_operator_name (tree identifier)
1369 {
1370 const char* suffix = UDLIT_OP_SUFFIX (identifier);
1371 write_identifier (UDLIT_OP_MANGLED_PREFIX);
1372 write_unsigned_number (strlen (suffix));
1373 write_identifier (suffix);
1374 }
1375
1376 /* Encode 0 as _, and 1+ as n-1_. */
1377
1378 static void
1379 write_compact_number (int num)
1380 {
1381 if (num > 0)
1382 write_unsigned_number (num - 1);
1383 write_char ('_');
1384 }
1385
1386 /* Return how many unnamed types precede TYPE in its enclosing class. */
1387
1388 static int
1389 nested_anon_class_index (tree type)
1390 {
1391 int index = 0;
1392 tree member = TYPE_FIELDS (TYPE_CONTEXT (type));
1393 for (; member; member = DECL_CHAIN (member))
1394 if (DECL_IMPLICIT_TYPEDEF_P (member))
1395 {
1396 tree memtype = TREE_TYPE (member);
1397 if (memtype == type)
1398 return index;
1399 else if (TYPE_ANONYMOUS_P (memtype))
1400 ++index;
1401 }
1402
1403 gcc_unreachable ();
1404 }
1405
1406 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
1407
1408 static void
1409 write_unnamed_type_name (const tree type)
1410 {
1411 int discriminator;
1412 MANGLE_TRACE_TREE ("unnamed-type-name", type);
1413
1414 if (TYPE_FUNCTION_SCOPE_P (type))
1415 discriminator = local_class_index (type);
1416 else if (TYPE_CLASS_SCOPE_P (type))
1417 discriminator = nested_anon_class_index (type);
1418 else
1419 {
1420 gcc_assert (no_linkage_check (type, /*relaxed_p=*/true));
1421 /* Just use the old mangling at namespace scope. */
1422 write_source_name (TYPE_IDENTIFIER (type));
1423 return;
1424 }
1425
1426 write_string ("Ut");
1427 write_compact_number (discriminator);
1428 }
1429
1430 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1431 <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters */
1432
1433 static void
1434 write_closure_type_name (const tree type)
1435 {
1436 tree fn = lambda_function (type);
1437 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
1438 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1439
1440 MANGLE_TRACE_TREE ("closure-type-name", type);
1441
1442 write_string ("Ul");
1443 write_method_parms (parms, /*method_p=*/1, fn);
1444 write_char ('E');
1445 write_compact_number (LAMBDA_EXPR_DISCRIMINATOR (lambda));
1446 }
1447
1448 /* Convert NUMBER to ascii using base BASE and generating at least
1449 MIN_DIGITS characters. BUFFER points to the _end_ of the buffer
1450 into which to store the characters. Returns the number of
1451 characters generated (these will be laid out in advance of where
1452 BUFFER points). */
1453
1454 static int
1455 hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
1456 char *buffer, const unsigned int min_digits)
1457 {
1458 static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1459 unsigned digits = 0;
1460
1461 while (number)
1462 {
1463 unsigned HOST_WIDE_INT d = number / base;
1464
1465 *--buffer = base_digits[number - d * base];
1466 digits++;
1467 number = d;
1468 }
1469 while (digits < min_digits)
1470 {
1471 *--buffer = base_digits[0];
1472 digits++;
1473 }
1474 return digits;
1475 }
1476
1477 /* Non-terminal <number>.
1478
1479 <number> ::= [n] </decimal integer/> */
1480
1481 static void
1482 write_number (unsigned HOST_WIDE_INT number, const int unsigned_p,
1483 const unsigned int base)
1484 {
1485 char buffer[sizeof (HOST_WIDE_INT) * 8];
1486 unsigned count = 0;
1487
1488 if (!unsigned_p && (HOST_WIDE_INT) number < 0)
1489 {
1490 write_char ('n');
1491 number = -((HOST_WIDE_INT) number);
1492 }
1493 count = hwint_to_ascii (number, base, buffer + sizeof (buffer), 1);
1494 write_chars (buffer + sizeof (buffer) - count, count);
1495 }
1496
1497 /* Write out an integral CST in decimal. Most numbers are small, and
1498 representable in a HOST_WIDE_INT. Occasionally we'll have numbers
1499 bigger than that, which we must deal with. */
1500
1501 static inline void
1502 write_integer_cst (const tree cst)
1503 {
1504 int sign = tree_int_cst_sgn (cst);
1505
1506 if (TREE_INT_CST_HIGH (cst) + (sign < 0))
1507 {
1508 /* A bignum. We do this in chunks, each of which fits in a
1509 HOST_WIDE_INT. */
1510 char buffer[sizeof (HOST_WIDE_INT) * 8 * 2];
1511 unsigned HOST_WIDE_INT chunk;
1512 unsigned chunk_digits;
1513 char *ptr = buffer + sizeof (buffer);
1514 unsigned count = 0;
1515 tree n, base, type;
1516 int done;
1517
1518 /* HOST_WIDE_INT must be at least 32 bits, so 10^9 is
1519 representable. */
1520 chunk = 1000000000;
1521 chunk_digits = 9;
1522
1523 if (sizeof (HOST_WIDE_INT) >= 8)
1524 {
1525 /* It is at least 64 bits, so 10^18 is representable. */
1526 chunk_digits = 18;
1527 chunk *= chunk;
1528 }
1529
1530 type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
1531 base = build_int_cstu (type, chunk);
1532 n = build_int_cst_wide (type,
1533 TREE_INT_CST_LOW (cst), TREE_INT_CST_HIGH (cst));
1534
1535 if (sign < 0)
1536 {
1537 write_char ('n');
1538 n = fold_build1_loc (input_location, NEGATE_EXPR, type, n);
1539 }
1540 do
1541 {
1542 tree d = fold_build2_loc (input_location, FLOOR_DIV_EXPR, type, n, base);
1543 tree tmp = fold_build2_loc (input_location, MULT_EXPR, type, d, base);
1544 unsigned c;
1545
1546 done = integer_zerop (d);
1547 tmp = fold_build2_loc (input_location, MINUS_EXPR, type, n, tmp);
1548 c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
1549 done ? 1 : chunk_digits);
1550 ptr -= c;
1551 count += c;
1552 n = d;
1553 }
1554 while (!done);
1555 write_chars (ptr, count);
1556 }
1557 else
1558 {
1559 /* A small num. */
1560 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
1561
1562 if (sign < 0)
1563 {
1564 write_char ('n');
1565 low = -low;
1566 }
1567 write_unsigned_number (low);
1568 }
1569 }
1570
1571 /* Write out a floating-point literal.
1572
1573 "Floating-point literals are encoded using the bit pattern of the
1574 target processor's internal representation of that number, as a
1575 fixed-length lowercase hexadecimal string, high-order bytes first
1576 (even if the target processor would store low-order bytes first).
1577 The "n" prefix is not used for floating-point literals; the sign
1578 bit is encoded with the rest of the number.
1579
1580 Here are some examples, assuming the IEEE standard representation
1581 for floating point numbers. (Spaces are for readability, not
1582 part of the encoding.)
1583
1584 1.0f Lf 3f80 0000 E
1585 -1.0f Lf bf80 0000 E
1586 1.17549435e-38f Lf 0080 0000 E
1587 1.40129846e-45f Lf 0000 0001 E
1588 0.0f Lf 0000 0000 E"
1589
1590 Caller is responsible for the Lx and the E. */
1591 static void
1592 write_real_cst (const tree value)
1593 {
1594 if (abi_version_at_least (2))
1595 {
1596 long target_real[4]; /* largest supported float */
1597 char buffer[9]; /* eight hex digits in a 32-bit number */
1598 int i, limit, dir;
1599
1600 tree type = TREE_TYPE (value);
1601 int words = GET_MODE_BITSIZE (TYPE_MODE (type)) / 32;
1602
1603 real_to_target (target_real, &TREE_REAL_CST (value),
1604 TYPE_MODE (type));
1605
1606 /* The value in target_real is in the target word order,
1607 so we must write it out backward if that happens to be
1608 little-endian. write_number cannot be used, it will
1609 produce uppercase. */
1610 if (FLOAT_WORDS_BIG_ENDIAN)
1611 i = 0, limit = words, dir = 1;
1612 else
1613 i = words - 1, limit = -1, dir = -1;
1614
1615 for (; i != limit; i += dir)
1616 {
1617 sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
1618 write_chars (buffer, 8);
1619 }
1620 }
1621 else
1622 {
1623 /* In G++ 3.3 and before the REAL_VALUE_TYPE was written out
1624 literally. Note that compatibility with 3.2 is impossible,
1625 because the old floating-point emulator used a different
1626 format for REAL_VALUE_TYPE. */
1627 size_t i;
1628 for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
1629 write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
1630 /*unsigned_p*/ 1,
1631 /*base*/ 16);
1632 G.need_abi_warning = 1;
1633 }
1634 }
1635
1636 /* Non-terminal <identifier>.
1637
1638 <identifier> ::= </unqualified source code identifier> */
1639
1640 static void
1641 write_identifier (const char *identifier)
1642 {
1643 MANGLE_TRACE ("identifier", identifier);
1644 write_string (identifier);
1645 }
1646
1647 /* Handle constructor productions of non-terminal <special-name>.
1648 CTOR is a constructor FUNCTION_DECL.
1649
1650 <special-name> ::= C1 # complete object constructor
1651 ::= C2 # base object constructor
1652 ::= C3 # complete object allocating constructor
1653
1654 Currently, allocating constructors are never used.
1655
1656 We also need to provide mangled names for the maybe-in-charge
1657 constructor, so we treat it here too. mangle_decl_string will
1658 append *INTERNAL* to that, to make sure we never emit it. */
1659
1660 static void
1661 write_special_name_constructor (const tree ctor)
1662 {
1663 if (DECL_BASE_CONSTRUCTOR_P (ctor))
1664 write_string ("C2");
1665 else
1666 {
1667 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (ctor)
1668 /* Even though we don't ever emit a definition of
1669 the old-style destructor, we still have to
1670 consider entities (like static variables) nested
1671 inside it. */
1672 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (ctor));
1673 write_string ("C1");
1674 }
1675 }
1676
1677 /* Handle destructor productions of non-terminal <special-name>.
1678 DTOR is a destructor FUNCTION_DECL.
1679
1680 <special-name> ::= D0 # deleting (in-charge) destructor
1681 ::= D1 # complete object (in-charge) destructor
1682 ::= D2 # base object (not-in-charge) destructor
1683
1684 We also need to provide mangled names for the maybe-incharge
1685 destructor, so we treat it here too. mangle_decl_string will
1686 append *INTERNAL* to that, to make sure we never emit it. */
1687
1688 static void
1689 write_special_name_destructor (const tree dtor)
1690 {
1691 if (DECL_DELETING_DESTRUCTOR_P (dtor))
1692 write_string ("D0");
1693 else if (DECL_BASE_DESTRUCTOR_P (dtor))
1694 write_string ("D2");
1695 else
1696 {
1697 gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
1698 /* Even though we don't ever emit a definition of
1699 the old-style destructor, we still have to
1700 consider entities (like static variables) nested
1701 inside it. */
1702 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
1703 write_string ("D1");
1704 }
1705 }
1706
1707 /* Scan the vector of local classes and return how many others with the
1708 same name (or same no name) and context precede ENTITY. */
1709
1710 static int
1711 local_class_index (tree entity)
1712 {
1713 int ix, discriminator = 0;
1714 tree name = (TYPE_ANONYMOUS_P (entity) ? NULL_TREE
1715 : TYPE_IDENTIFIER (entity));
1716 tree ctx = TYPE_CONTEXT (entity);
1717 for (ix = 0; ; ix++)
1718 {
1719 tree type = (*local_classes)[ix];
1720 if (type == entity)
1721 return discriminator;
1722 if (TYPE_CONTEXT (type) == ctx
1723 && (name ? TYPE_IDENTIFIER (type) == name
1724 : TYPE_ANONYMOUS_P (type)))
1725 ++discriminator;
1726 }
1727 gcc_unreachable ();
1728 }
1729
1730 /* Return the discriminator for ENTITY appearing inside
1731 FUNCTION. The discriminator is the lexical ordinal of VAR among
1732 entities with the same name in the same FUNCTION. */
1733
1734 static int
1735 discriminator_for_local_entity (tree entity)
1736 {
1737 if (DECL_DISCRIMINATOR_P (entity))
1738 {
1739 if (DECL_DISCRIMINATOR_SET_P (entity))
1740 return DECL_DISCRIMINATOR (entity);
1741 else
1742 /* The first entity with a particular name doesn't get
1743 DECL_DISCRIMINATOR set up. */
1744 return 0;
1745 }
1746 else if (TREE_CODE (entity) == TYPE_DECL)
1747 {
1748 /* Scan the list of local classes. */
1749 entity = TREE_TYPE (entity);
1750
1751 /* Lambdas and unnamed types have their own discriminators. */
1752 if (LAMBDA_TYPE_P (entity) || TYPE_ANONYMOUS_P (entity))
1753 return 0;
1754
1755 return local_class_index (entity);
1756 }
1757 else
1758 gcc_unreachable ();
1759 }
1760
1761 /* Return the discriminator for STRING, a string literal used inside
1762 FUNCTION. The discriminator is the lexical ordinal of STRING among
1763 string literals used in FUNCTION. */
1764
1765 static int
1766 discriminator_for_string_literal (tree /*function*/,
1767 tree /*string*/)
1768 {
1769 /* For now, we don't discriminate amongst string literals. */
1770 return 0;
1771 }
1772
1773 /* <discriminator> := _ <number>
1774
1775 The discriminator is used only for the second and later occurrences
1776 of the same name within a single function. In this case <number> is
1777 n - 2, if this is the nth occurrence, in lexical order. */
1778
1779 static void
1780 write_discriminator (const int discriminator)
1781 {
1782 /* If discriminator is zero, don't write anything. Otherwise... */
1783 if (discriminator > 0)
1784 {
1785 write_char ('_');
1786 write_unsigned_number (discriminator - 1);
1787 }
1788 }
1789
1790 /* Mangle the name of a function-scope entity. FUNCTION is the
1791 FUNCTION_DECL for the enclosing function, or a PARM_DECL for lambdas in
1792 default argument scope. ENTITY is the decl for the entity itself.
1793 LOCAL_ENTITY is the entity that's directly scoped in FUNCTION_DECL,
1794 either ENTITY itself or an enclosing scope of ENTITY.
1795
1796 <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1797 := Z <function encoding> E s [<discriminator>]
1798 := Z <function encoding> Ed [ <parameter number> ] _ <entity name> */
1799
1800 static void
1801 write_local_name (tree function, const tree local_entity,
1802 const tree entity)
1803 {
1804 tree parm = NULL_TREE;
1805
1806 MANGLE_TRACE_TREE ("local-name", entity);
1807
1808 if (TREE_CODE (function) == PARM_DECL)
1809 {
1810 parm = function;
1811 function = DECL_CONTEXT (parm);
1812 }
1813
1814 write_char ('Z');
1815 write_encoding (function);
1816 write_char ('E');
1817
1818 /* For this purpose, parameters are numbered from right-to-left. */
1819 if (parm)
1820 {
1821 tree t;
1822 int i = 0;
1823 for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
1824 {
1825 if (t == parm)
1826 i = 1;
1827 else if (i)
1828 ++i;
1829 }
1830 write_char ('d');
1831 write_compact_number (i - 1);
1832 }
1833
1834 if (TREE_CODE (entity) == STRING_CST)
1835 {
1836 write_char ('s');
1837 write_discriminator (discriminator_for_string_literal (function,
1838 entity));
1839 }
1840 else
1841 {
1842 /* Now the <entity name>. Let write_name know its being called
1843 from <local-name>, so it doesn't try to process the enclosing
1844 function scope again. */
1845 write_name (entity, /*ignore_local_scope=*/1);
1846 write_discriminator (discriminator_for_local_entity (local_entity));
1847 }
1848 }
1849
1850 /* Non-terminals <type> and <CV-qualifier>.
1851
1852 <type> ::= <builtin-type>
1853 ::= <function-type>
1854 ::= <class-enum-type>
1855 ::= <array-type>
1856 ::= <pointer-to-member-type>
1857 ::= <template-param>
1858 ::= <substitution>
1859 ::= <CV-qualifier>
1860 ::= P <type> # pointer-to
1861 ::= R <type> # reference-to
1862 ::= C <type> # complex pair (C 2000)
1863 ::= G <type> # imaginary (C 2000) [not supported]
1864 ::= U <source-name> <type> # vendor extended type qualifier
1865
1866 C++0x extensions
1867
1868 <type> ::= RR <type> # rvalue reference-to
1869 <type> ::= Dt <expression> # decltype of an id-expression or
1870 # class member access
1871 <type> ::= DT <expression> # decltype of an expression
1872 <type> ::= Dn # decltype of nullptr
1873
1874 TYPE is a type node. */
1875
1876 static void
1877 write_type (tree type)
1878 {
1879 /* This gets set to nonzero if TYPE turns out to be a (possibly
1880 CV-qualified) builtin type. */
1881 int is_builtin_type = 0;
1882
1883 MANGLE_TRACE_TREE ("type", type);
1884
1885 if (type == error_mark_node)
1886 return;
1887
1888 type = canonicalize_for_substitution (type);
1889 if (find_substitution (type))
1890 return;
1891
1892
1893 if (write_CV_qualifiers_for_type (type) > 0)
1894 /* If TYPE was CV-qualified, we just wrote the qualifiers; now
1895 mangle the unqualified type. The recursive call is needed here
1896 since both the qualified and unqualified types are substitution
1897 candidates. */
1898 {
1899 tree t = TYPE_MAIN_VARIANT (type);
1900 if (TREE_CODE (t) == FUNCTION_TYPE
1901 || TREE_CODE (t) == METHOD_TYPE)
1902 {
1903 t = build_ref_qualified_type (t, type_memfn_rqual (type));
1904 if (abi_version_at_least (8))
1905 /* Avoid adding the unqualified function type as a substitution. */
1906 write_function_type (t);
1907 else
1908 write_type (t);
1909 }
1910 else
1911 write_type (t);
1912 }
1913 else if (TREE_CODE (type) == ARRAY_TYPE)
1914 /* It is important not to use the TYPE_MAIN_VARIANT of TYPE here
1915 so that the cv-qualification of the element type is available
1916 in write_array_type. */
1917 write_array_type (type);
1918 else
1919 {
1920 tree type_orig = type;
1921
1922 /* See through any typedefs. */
1923 type = TYPE_MAIN_VARIANT (type);
1924 if (TREE_CODE (type) == FUNCTION_TYPE
1925 || TREE_CODE (type) == METHOD_TYPE)
1926 type = build_ref_qualified_type (type, type_memfn_rqual (type_orig));
1927
1928 /* According to the C++ ABI, some library classes are passed the
1929 same as the scalar type of their single member and use the same
1930 mangling. */
1931 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
1932 type = TREE_TYPE (first_field (type));
1933
1934 if (TYPE_PTRDATAMEM_P (type))
1935 write_pointer_to_member_type (type);
1936 else
1937 {
1938 /* Handle any target-specific fundamental types. */
1939 const char *target_mangling
1940 = targetm.mangle_type (type_orig);
1941
1942 if (target_mangling)
1943 {
1944 write_string (target_mangling);
1945 /* Add substitutions for types other than fundamental
1946 types. */
1947 if (!VOID_TYPE_P (type)
1948 && TREE_CODE (type) != INTEGER_TYPE
1949 && TREE_CODE (type) != REAL_TYPE
1950 && TREE_CODE (type) != BOOLEAN_TYPE)
1951 add_substitution (type);
1952 return;
1953 }
1954
1955 switch (TREE_CODE (type))
1956 {
1957 case VOID_TYPE:
1958 case BOOLEAN_TYPE:
1959 case INTEGER_TYPE: /* Includes wchar_t. */
1960 case REAL_TYPE:
1961 case FIXED_POINT_TYPE:
1962 {
1963 /* If this is a typedef, TYPE may not be one of
1964 the standard builtin type nodes, but an alias of one. Use
1965 TYPE_MAIN_VARIANT to get to the underlying builtin type. */
1966 write_builtin_type (TYPE_MAIN_VARIANT (type));
1967 ++is_builtin_type;
1968 }
1969 break;
1970
1971 case COMPLEX_TYPE:
1972 write_char ('C');
1973 write_type (TREE_TYPE (type));
1974 break;
1975
1976 case FUNCTION_TYPE:
1977 case METHOD_TYPE:
1978 write_function_type (type);
1979 break;
1980
1981 case UNION_TYPE:
1982 case RECORD_TYPE:
1983 case ENUMERAL_TYPE:
1984 /* A pointer-to-member function is represented as a special
1985 RECORD_TYPE, so check for this first. */
1986 if (TYPE_PTRMEMFUNC_P (type))
1987 write_pointer_to_member_type (type);
1988 else
1989 write_class_enum_type (type);
1990 break;
1991
1992 case TYPENAME_TYPE:
1993 case UNBOUND_CLASS_TEMPLATE:
1994 /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
1995 ordinary nested names. */
1996 write_nested_name (TYPE_STUB_DECL (type));
1997 break;
1998
1999 case POINTER_TYPE:
2000 case REFERENCE_TYPE:
2001 if (TYPE_PTR_P (type))
2002 write_char ('P');
2003 else if (TYPE_REF_IS_RVALUE (type))
2004 write_char ('O');
2005 else
2006 write_char ('R');
2007 {
2008 tree target = TREE_TYPE (type);
2009 /* Attribute const/noreturn are not reflected in mangling.
2010 We strip them here rather than at a lower level because
2011 a typedef or template argument can have function type
2012 with function-cv-quals (that use the same representation),
2013 but you can't have a pointer/reference to such a type. */
2014 if (abi_version_at_least (5)
2015 && TREE_CODE (target) == FUNCTION_TYPE)
2016 target = build_qualified_type (target, TYPE_UNQUALIFIED);
2017 write_type (target);
2018 }
2019 break;
2020
2021 case TEMPLATE_TYPE_PARM:
2022 if (is_auto (type))
2023 {
2024 if (AUTO_IS_DECLTYPE (type))
2025 write_identifier ("Dc");
2026 else
2027 write_identifier ("Da");
2028 ++is_builtin_type;
2029 break;
2030 }
2031 /* else fall through. */
2032 case TEMPLATE_PARM_INDEX:
2033 write_template_param (type);
2034 break;
2035
2036 case TEMPLATE_TEMPLATE_PARM:
2037 write_template_template_param (type);
2038 break;
2039
2040 case BOUND_TEMPLATE_TEMPLATE_PARM:
2041 write_template_template_param (type);
2042 write_template_args
2043 (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
2044 break;
2045
2046 case VECTOR_TYPE:
2047 if (abi_version_at_least (4))
2048 {
2049 write_string ("Dv");
2050 /* Non-constant vector size would be encoded with
2051 _ expression, but we don't support that yet. */
2052 write_unsigned_number (TYPE_VECTOR_SUBPARTS (type));
2053 write_char ('_');
2054 }
2055 else
2056 {
2057 G.need_abi_warning = 1;
2058 write_string ("U8__vector");
2059 }
2060 write_type (TREE_TYPE (type));
2061 break;
2062
2063 case TYPE_PACK_EXPANSION:
2064 write_string ("Dp");
2065 write_type (PACK_EXPANSION_PATTERN (type));
2066 break;
2067
2068 case DECLTYPE_TYPE:
2069 /* These shouldn't make it into mangling. */
2070 gcc_assert (!DECLTYPE_FOR_LAMBDA_CAPTURE (type)
2071 && !DECLTYPE_FOR_LAMBDA_PROXY (type));
2072
2073 /* In ABI <5, we stripped decltype of a plain decl. */
2074 if (!abi_version_at_least (5)
2075 && DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2076 {
2077 tree expr = DECLTYPE_TYPE_EXPR (type);
2078 tree etype = NULL_TREE;
2079 switch (TREE_CODE (expr))
2080 {
2081 case VAR_DECL:
2082 case PARM_DECL:
2083 case RESULT_DECL:
2084 case FUNCTION_DECL:
2085 case CONST_DECL:
2086 case TEMPLATE_PARM_INDEX:
2087 etype = TREE_TYPE (expr);
2088 break;
2089
2090 default:
2091 break;
2092 }
2093
2094 if (etype && !type_uses_auto (etype))
2095 {
2096 G.need_abi_warning = 1;
2097 write_type (etype);
2098 return;
2099 }
2100 }
2101
2102 write_char ('D');
2103 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
2104 write_char ('t');
2105 else
2106 write_char ('T');
2107 ++cp_unevaluated_operand;
2108 write_expression (DECLTYPE_TYPE_EXPR (type));
2109 --cp_unevaluated_operand;
2110 write_char ('E');
2111 break;
2112
2113 case NULLPTR_TYPE:
2114 write_string ("Dn");
2115 if (abi_version_at_least (7))
2116 ++is_builtin_type;
2117 break;
2118
2119 case TYPEOF_TYPE:
2120 sorry ("mangling typeof, use decltype instead");
2121 break;
2122
2123 case UNDERLYING_TYPE:
2124 sorry ("mangling __underlying_type");
2125 break;
2126
2127 case LANG_TYPE:
2128 /* fall through. */
2129
2130 default:
2131 gcc_unreachable ();
2132 }
2133 }
2134 }
2135
2136 /* Types other than builtin types are substitution candidates. */
2137 if (!is_builtin_type)
2138 add_substitution (type);
2139 }
2140
2141 /* Non-terminal <CV-qualifiers> for type nodes. Returns the number of
2142 CV-qualifiers written for TYPE.
2143
2144 <CV-qualifiers> ::= [r] [V] [K] */
2145
2146 static int
2147 write_CV_qualifiers_for_type (const tree type)
2148 {
2149 int num_qualifiers = 0;
2150
2151 /* The order is specified by:
2152
2153 "In cases where multiple order-insensitive qualifiers are
2154 present, they should be ordered 'K' (closest to the base type),
2155 'V', 'r', and 'U' (farthest from the base type) ..."
2156
2157 Note that we do not use cp_type_quals below; given "const
2158 int[3]", the "const" is emitted with the "int", not with the
2159 array. */
2160 cp_cv_quals quals = TYPE_QUALS (type);
2161
2162 if (quals & TYPE_QUAL_RESTRICT)
2163 {
2164 write_char ('r');
2165 ++num_qualifiers;
2166 }
2167 if (quals & TYPE_QUAL_VOLATILE)
2168 {
2169 write_char ('V');
2170 ++num_qualifiers;
2171 }
2172 if (quals & TYPE_QUAL_CONST)
2173 {
2174 write_char ('K');
2175 ++num_qualifiers;
2176 }
2177
2178 return num_qualifiers;
2179 }
2180
2181 /* Non-terminal <builtin-type>.
2182
2183 <builtin-type> ::= v # void
2184 ::= b # bool
2185 ::= w # wchar_t
2186 ::= c # char
2187 ::= a # signed char
2188 ::= h # unsigned char
2189 ::= s # short
2190 ::= t # unsigned short
2191 ::= i # int
2192 ::= j # unsigned int
2193 ::= l # long
2194 ::= m # unsigned long
2195 ::= x # long long, __int64
2196 ::= y # unsigned long long, __int64
2197 ::= n # __int128
2198 ::= o # unsigned __int128
2199 ::= f # float
2200 ::= d # double
2201 ::= e # long double, __float80
2202 ::= g # __float128 [not supported]
2203 ::= u <source-name> # vendor extended type */
2204
2205 static void
2206 write_builtin_type (tree type)
2207 {
2208 if (TYPE_CANONICAL (type))
2209 type = TYPE_CANONICAL (type);
2210
2211 switch (TREE_CODE (type))
2212 {
2213 case VOID_TYPE:
2214 write_char ('v');
2215 break;
2216
2217 case BOOLEAN_TYPE:
2218 write_char ('b');
2219 break;
2220
2221 case INTEGER_TYPE:
2222 /* TYPE may still be wchar_t, char16_t, or char32_t, since that
2223 isn't in integer_type_nodes. */
2224 if (type == wchar_type_node)
2225 write_char ('w');
2226 else if (type == char16_type_node)
2227 write_string ("Ds");
2228 else if (type == char32_type_node)
2229 write_string ("Di");
2230 else if (TYPE_FOR_JAVA (type))
2231 write_java_integer_type_codes (type);
2232 else
2233 {
2234 size_t itk;
2235 /* Assume TYPE is one of the shared integer type nodes. Find
2236 it in the array of these nodes. */
2237 iagain:
2238 for (itk = 0; itk < itk_none; ++itk)
2239 if (integer_types[itk] != NULL_TREE
2240 && type == integer_types[itk])
2241 {
2242 /* Print the corresponding single-letter code. */
2243 write_char (integer_type_codes[itk]);
2244 break;
2245 }
2246
2247 if (itk == itk_none)
2248 {
2249 tree t = c_common_type_for_mode (TYPE_MODE (type),
2250 TYPE_UNSIGNED (type));
2251 if (type != t)
2252 {
2253 type = t;
2254 goto iagain;
2255 }
2256
2257 if (TYPE_PRECISION (type) == 128)
2258 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
2259 else
2260 {
2261 /* Allow for cases where TYPE is not one of the shared
2262 integer type nodes and write a "vendor extended builtin
2263 type" with a name the form intN or uintN, respectively.
2264 Situations like this can happen if you have an
2265 __attribute__((__mode__(__SI__))) type and use exotic
2266 switches like '-mint8' on AVR. Of course, this is
2267 undefined by the C++ ABI (and '-mint8' is not even
2268 Standard C conforming), but when using such special
2269 options you're pretty much in nowhere land anyway. */
2270 const char *prefix;
2271 char prec[11]; /* up to ten digits for an unsigned */
2272
2273 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
2274 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
2275 write_char ('u'); /* "vendor extended builtin type" */
2276 write_unsigned_number (strlen (prefix) + strlen (prec));
2277 write_string (prefix);
2278 write_string (prec);
2279 }
2280 }
2281 }
2282 break;
2283
2284 case REAL_TYPE:
2285 if (type == float_type_node
2286 || type == java_float_type_node)
2287 write_char ('f');
2288 else if (type == double_type_node
2289 || type == java_double_type_node)
2290 write_char ('d');
2291 else if (type == long_double_type_node)
2292 write_char ('e');
2293 else if (type == dfloat32_type_node)
2294 write_string ("Df");
2295 else if (type == dfloat64_type_node)
2296 write_string ("Dd");
2297 else if (type == dfloat128_type_node)
2298 write_string ("De");
2299 else
2300 gcc_unreachable ();
2301 break;
2302
2303 case FIXED_POINT_TYPE:
2304 write_string ("DF");
2305 if (GET_MODE_IBIT (TYPE_MODE (type)) > 0)
2306 write_unsigned_number (GET_MODE_IBIT (TYPE_MODE (type)));
2307 if (type == fract_type_node
2308 || type == sat_fract_type_node
2309 || type == accum_type_node
2310 || type == sat_accum_type_node)
2311 write_char ('i');
2312 else if (type == unsigned_fract_type_node
2313 || type == sat_unsigned_fract_type_node
2314 || type == unsigned_accum_type_node
2315 || type == sat_unsigned_accum_type_node)
2316 write_char ('j');
2317 else if (type == short_fract_type_node
2318 || type == sat_short_fract_type_node
2319 || type == short_accum_type_node
2320 || type == sat_short_accum_type_node)
2321 write_char ('s');
2322 else if (type == unsigned_short_fract_type_node
2323 || type == sat_unsigned_short_fract_type_node
2324 || type == unsigned_short_accum_type_node
2325 || type == sat_unsigned_short_accum_type_node)
2326 write_char ('t');
2327 else if (type == long_fract_type_node
2328 || type == sat_long_fract_type_node
2329 || type == long_accum_type_node
2330 || type == sat_long_accum_type_node)
2331 write_char ('l');
2332 else if (type == unsigned_long_fract_type_node
2333 || type == sat_unsigned_long_fract_type_node
2334 || type == unsigned_long_accum_type_node
2335 || type == sat_unsigned_long_accum_type_node)
2336 write_char ('m');
2337 else if (type == long_long_fract_type_node
2338 || type == sat_long_long_fract_type_node
2339 || type == long_long_accum_type_node
2340 || type == sat_long_long_accum_type_node)
2341 write_char ('x');
2342 else if (type == unsigned_long_long_fract_type_node
2343 || type == sat_unsigned_long_long_fract_type_node
2344 || type == unsigned_long_long_accum_type_node
2345 || type == sat_unsigned_long_long_accum_type_node)
2346 write_char ('y');
2347 else
2348 sorry ("mangling unknown fixed point type");
2349 write_unsigned_number (GET_MODE_FBIT (TYPE_MODE (type)));
2350 if (TYPE_SATURATING (type))
2351 write_char ('s');
2352 else
2353 write_char ('n');
2354 break;
2355
2356 default:
2357 gcc_unreachable ();
2358 }
2359 }
2360
2361 /* Non-terminal <function-type>. NODE is a FUNCTION_TYPE or
2362 METHOD_TYPE. The return type is mangled before the parameter
2363 types.
2364
2365 <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2366
2367 static void
2368 write_function_type (const tree type)
2369 {
2370 MANGLE_TRACE_TREE ("function-type", type);
2371
2372 /* For a pointer to member function, the function type may have
2373 cv-qualifiers, indicating the quals for the artificial 'this'
2374 parameter. */
2375 if (TREE_CODE (type) == METHOD_TYPE)
2376 {
2377 /* The first parameter must be a POINTER_TYPE pointing to the
2378 `this' parameter. */
2379 tree this_type = class_of_this_parm (type);
2380 write_CV_qualifiers_for_type (this_type);
2381 }
2382
2383 write_char ('F');
2384 /* We don't track whether or not a type is `extern "C"'. Note that
2385 you can have an `extern "C"' function that does not have
2386 `extern "C"' type, and vice versa:
2387
2388 extern "C" typedef void function_t();
2389 function_t f; // f has C++ linkage, but its type is
2390 // `extern "C"'
2391
2392 typedef void function_t();
2393 extern "C" function_t f; // Vice versa.
2394
2395 See [dcl.link]. */
2396 write_bare_function_type (type, /*include_return_type_p=*/1,
2397 /*decl=*/NULL);
2398 if (FUNCTION_REF_QUALIFIED (type))
2399 {
2400 if (FUNCTION_RVALUE_QUALIFIED (type))
2401 write_char ('O');
2402 else
2403 write_char ('R');
2404 }
2405 write_char ('E');
2406 }
2407
2408 /* Non-terminal <bare-function-type>. TYPE is a FUNCTION_TYPE or
2409 METHOD_TYPE. If INCLUDE_RETURN_TYPE is nonzero, the return value
2410 is mangled before the parameter types. If non-NULL, DECL is
2411 FUNCTION_DECL for the function whose type is being emitted.
2412
2413 If DECL is a member of a Java type, then a literal 'J'
2414 is output and the return type is mangled as if INCLUDE_RETURN_TYPE
2415 were nonzero.
2416
2417 <bare-function-type> ::= [J]</signature/ type>+ */
2418
2419 static void
2420 write_bare_function_type (const tree type, const int include_return_type_p,
2421 const tree decl)
2422 {
2423 int java_method_p;
2424
2425 MANGLE_TRACE_TREE ("bare-function-type", type);
2426
2427 /* Detect Java methods and emit special encoding. */
2428 if (decl != NULL
2429 && DECL_FUNCTION_MEMBER_P (decl)
2430 && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
2431 && !DECL_CONSTRUCTOR_P (decl)
2432 && !DECL_DESTRUCTOR_P (decl)
2433 && !DECL_CONV_FN_P (decl))
2434 {
2435 java_method_p = 1;
2436 write_char ('J');
2437 }
2438 else
2439 {
2440 java_method_p = 0;
2441 }
2442
2443 /* Mangle the return type, if requested. */
2444 if (include_return_type_p || java_method_p)
2445 write_type (TREE_TYPE (type));
2446
2447 /* Now mangle the types of the arguments. */
2448 ++G.parm_depth;
2449 write_method_parms (TYPE_ARG_TYPES (type),
2450 TREE_CODE (type) == METHOD_TYPE,
2451 decl);
2452 --G.parm_depth;
2453 }
2454
2455 /* Write the mangled representation of a method parameter list of
2456 types given in PARM_TYPES. If METHOD_P is nonzero, the function is
2457 considered a non-static method, and the this parameter is omitted.
2458 If non-NULL, DECL is the FUNCTION_DECL for the function whose
2459 parameters are being emitted. */
2460
2461 static void
2462 write_method_parms (tree parm_types, const int method_p, const tree decl)
2463 {
2464 tree first_parm_type;
2465 tree parm_decl = decl ? DECL_ARGUMENTS (decl) : NULL_TREE;
2466
2467 /* Assume this parameter type list is variable-length. If it ends
2468 with a void type, then it's not. */
2469 int varargs_p = 1;
2470
2471 /* If this is a member function, skip the first arg, which is the
2472 this pointer.
2473 "Member functions do not encode the type of their implicit this
2474 parameter."
2475
2476 Similarly, there's no need to mangle artificial parameters, like
2477 the VTT parameters for constructors and destructors. */
2478 if (method_p)
2479 {
2480 parm_types = TREE_CHAIN (parm_types);
2481 parm_decl = parm_decl ? DECL_CHAIN (parm_decl) : NULL_TREE;
2482
2483 while (parm_decl && DECL_ARTIFICIAL (parm_decl))
2484 {
2485 parm_types = TREE_CHAIN (parm_types);
2486 parm_decl = DECL_CHAIN (parm_decl);
2487 }
2488 }
2489
2490 for (first_parm_type = parm_types;
2491 parm_types;
2492 parm_types = TREE_CHAIN (parm_types))
2493 {
2494 tree parm = TREE_VALUE (parm_types);
2495 if (parm == void_type_node)
2496 {
2497 /* "Empty parameter lists, whether declared as () or
2498 conventionally as (void), are encoded with a void parameter
2499 (v)." */
2500 if (parm_types == first_parm_type)
2501 write_type (parm);
2502 /* If the parm list is terminated with a void type, it's
2503 fixed-length. */
2504 varargs_p = 0;
2505 /* A void type better be the last one. */
2506 gcc_assert (TREE_CHAIN (parm_types) == NULL);
2507 }
2508 else
2509 write_type (parm);
2510 }
2511
2512 if (varargs_p)
2513 /* <builtin-type> ::= z # ellipsis */
2514 write_char ('z');
2515 }
2516
2517 /* <class-enum-type> ::= <name> */
2518
2519 static void
2520 write_class_enum_type (const tree type)
2521 {
2522 write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
2523 }
2524
2525 /* Non-terminal <template-args>. ARGS is a TREE_VEC of template
2526 arguments.
2527
2528 <template-args> ::= I <template-arg>* E */
2529
2530 static void
2531 write_template_args (tree args)
2532 {
2533 int i;
2534 int length = 0;
2535
2536 MANGLE_TRACE_TREE ("template-args", args);
2537
2538 write_char ('I');
2539
2540 if (args)
2541 length = TREE_VEC_LENGTH (args);
2542
2543 if (args && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2544 {
2545 /* We have nested template args. We want the innermost template
2546 argument list. */
2547 args = TREE_VEC_ELT (args, length - 1);
2548 length = TREE_VEC_LENGTH (args);
2549 }
2550 for (i = 0; i < length; ++i)
2551 write_template_arg (TREE_VEC_ELT (args, i));
2552
2553 write_char ('E');
2554 }
2555
2556 /* Write out the
2557 <unqualified-name>
2558 <unqualified-name> <template-args>
2559 part of SCOPE_REF or COMPONENT_REF mangling. */
2560
2561 static void
2562 write_member_name (tree member)
2563 {
2564 if (identifier_p (member))
2565 write_unqualified_id (member);
2566 else if (DECL_P (member))
2567 write_unqualified_name (member);
2568 else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
2569 {
2570 tree name = TREE_OPERAND (member, 0);
2571 if (TREE_CODE (name) == OVERLOAD)
2572 name = OVL_FUNCTION (name);
2573 write_member_name (name);
2574 write_template_args (TREE_OPERAND (member, 1));
2575 }
2576 else
2577 write_expression (member);
2578 }
2579
2580 /* <expression> ::= <unary operator-name> <expression>
2581 ::= <binary operator-name> <expression> <expression>
2582 ::= <expr-primary>
2583
2584 <expr-primary> ::= <template-param>
2585 ::= L <type> <value number> E # literal
2586 ::= L <mangled-name> E # external name
2587 ::= st <type> # sizeof
2588 ::= sr <type> <unqualified-name> # dependent name
2589 ::= sr <type> <unqualified-name> <template-args> */
2590
2591 static void
2592 write_expression (tree expr)
2593 {
2594 enum tree_code code = TREE_CODE (expr);
2595
2596 /* Skip NOP_EXPRs. They can occur when (say) a pointer argument
2597 is converted (via qualification conversions) to another
2598 type. */
2599 while (TREE_CODE (expr) == NOP_EXPR
2600 /* Parentheses aren't mangled. */
2601 || code == PAREN_EXPR
2602 || TREE_CODE (expr) == NON_LVALUE_EXPR)
2603 {
2604 expr = TREE_OPERAND (expr, 0);
2605 code = TREE_CODE (expr);
2606 }
2607
2608 if (code == BASELINK
2609 && (!type_unknown_p (expr)
2610 || !BASELINK_QUALIFIED_P (expr)))
2611 {
2612 expr = BASELINK_FUNCTIONS (expr);
2613 code = TREE_CODE (expr);
2614 }
2615
2616 /* Handle pointers-to-members by making them look like expression
2617 nodes. */
2618 if (code == PTRMEM_CST)
2619 {
2620 expr = build_nt (ADDR_EXPR,
2621 build_qualified_name (/*type=*/NULL_TREE,
2622 PTRMEM_CST_CLASS (expr),
2623 PTRMEM_CST_MEMBER (expr),
2624 /*template_p=*/false));
2625 code = TREE_CODE (expr);
2626 }
2627
2628 /* Handle template parameters. */
2629 if (code == TEMPLATE_TYPE_PARM
2630 || code == TEMPLATE_TEMPLATE_PARM
2631 || code == BOUND_TEMPLATE_TEMPLATE_PARM
2632 || code == TEMPLATE_PARM_INDEX)
2633 write_template_param (expr);
2634 /* Handle literals. */
2635 else if (TREE_CODE_CLASS (code) == tcc_constant
2636 || (abi_version_at_least (2) && code == CONST_DECL))
2637 write_template_arg_literal (expr);
2638 else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
2639 {
2640 gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
2641 write_string ("fpT");
2642 }
2643 else if (code == PARM_DECL)
2644 {
2645 /* A function parameter used in a late-specified return type. */
2646 int index = DECL_PARM_INDEX (expr);
2647 int level = DECL_PARM_LEVEL (expr);
2648 int delta = G.parm_depth - level + 1;
2649 gcc_assert (index >= 1);
2650 write_char ('f');
2651 if (delta != 0)
2652 {
2653 if (abi_version_at_least (5))
2654 {
2655 /* Let L be the number of function prototype scopes from the
2656 innermost one (in which the parameter reference occurs) up
2657 to (and including) the one containing the declaration of
2658 the referenced parameter. If the parameter declaration
2659 clause of the innermost function prototype scope has been
2660 completely seen, it is not counted (in that case -- which
2661 is perhaps the most common -- L can be zero). */
2662 write_char ('L');
2663 write_unsigned_number (delta - 1);
2664 }
2665 else
2666 G.need_abi_warning = true;
2667 }
2668 write_char ('p');
2669 write_compact_number (index - 1);
2670 }
2671 else if (DECL_P (expr))
2672 {
2673 /* G++ 3.2 incorrectly mangled non-type template arguments of
2674 enumeration type using their names. */
2675 if (code == CONST_DECL)
2676 G.need_abi_warning = 1;
2677 write_char ('L');
2678 write_mangled_name (expr, false);
2679 write_char ('E');
2680 }
2681 else if (TREE_CODE (expr) == SIZEOF_EXPR
2682 && SIZEOF_EXPR_TYPE_P (expr))
2683 {
2684 write_string ("st");
2685 write_type (TREE_TYPE (TREE_OPERAND (expr, 0)));
2686 }
2687 else if (TREE_CODE (expr) == SIZEOF_EXPR
2688 && TYPE_P (TREE_OPERAND (expr, 0)))
2689 {
2690 write_string ("st");
2691 write_type (TREE_OPERAND (expr, 0));
2692 }
2693 else if (TREE_CODE (expr) == ALIGNOF_EXPR
2694 && TYPE_P (TREE_OPERAND (expr, 0)))
2695 {
2696 write_string ("at");
2697 write_type (TREE_OPERAND (expr, 0));
2698 }
2699 else if (code == SCOPE_REF
2700 || code == BASELINK)
2701 {
2702 tree scope, member;
2703 if (code == SCOPE_REF)
2704 {
2705 scope = TREE_OPERAND (expr, 0);
2706 member = TREE_OPERAND (expr, 1);
2707 }
2708 else
2709 {
2710 scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
2711 member = BASELINK_FUNCTIONS (expr);
2712 }
2713
2714 if (!abi_version_at_least (2) && DECL_P (member))
2715 {
2716 write_string ("sr");
2717 write_type (scope);
2718 /* G++ 3.2 incorrectly put out both the "sr" code and
2719 the nested name of the qualified name. */
2720 G.need_abi_warning = 1;
2721 write_encoding (member);
2722 }
2723
2724 /* If the MEMBER is a real declaration, then the qualifying
2725 scope was not dependent. Ideally, we would not have a
2726 SCOPE_REF in those cases, but sometimes we do. If the second
2727 argument is a DECL, then the name must not have been
2728 dependent. */
2729 else if (DECL_P (member))
2730 write_expression (member);
2731 else
2732 {
2733 write_string ("sr");
2734 write_type (scope);
2735 write_member_name (member);
2736 }
2737 }
2738 else if (INDIRECT_REF_P (expr)
2739 && TREE_TYPE (TREE_OPERAND (expr, 0))
2740 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2741 {
2742 write_expression (TREE_OPERAND (expr, 0));
2743 }
2744 else if (identifier_p (expr))
2745 {
2746 /* An operator name appearing as a dependent name needs to be
2747 specially marked to disambiguate between a use of the operator
2748 name and a use of the operator in an expression. */
2749 if (IDENTIFIER_OPNAME_P (expr))
2750 write_string ("on");
2751 write_unqualified_id (expr);
2752 }
2753 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
2754 {
2755 tree fn = TREE_OPERAND (expr, 0);
2756 if (is_overloaded_fn (fn))
2757 fn = DECL_NAME (get_first_fn (fn));
2758 if (IDENTIFIER_OPNAME_P (fn))
2759 write_string ("on");
2760 write_unqualified_id (fn);
2761 write_template_args (TREE_OPERAND (expr, 1));
2762 }
2763 else if (TREE_CODE (expr) == MODOP_EXPR)
2764 {
2765 enum tree_code subop = TREE_CODE (TREE_OPERAND (expr, 1));
2766 const char *name = (assignment_operator_name_info[(int) subop]
2767 .mangled_name);
2768 write_string (name);
2769 write_expression (TREE_OPERAND (expr, 0));
2770 write_expression (TREE_OPERAND (expr, 2));
2771 }
2772 else if (code == NEW_EXPR || code == VEC_NEW_EXPR)
2773 {
2774 /* ::= [gs] nw <expression>* _ <type> E
2775 ::= [gs] nw <expression>* _ <type> <initializer>
2776 ::= [gs] na <expression>* _ <type> E
2777 ::= [gs] na <expression>* _ <type> <initializer>
2778 <initializer> ::= pi <expression>* E */
2779 tree placement = TREE_OPERAND (expr, 0);
2780 tree type = TREE_OPERAND (expr, 1);
2781 tree nelts = TREE_OPERAND (expr, 2);
2782 tree init = TREE_OPERAND (expr, 3);
2783 tree t;
2784
2785 gcc_assert (code == NEW_EXPR);
2786 if (TREE_OPERAND (expr, 2))
2787 code = VEC_NEW_EXPR;
2788
2789 if (NEW_EXPR_USE_GLOBAL (expr))
2790 write_string ("gs");
2791
2792 write_string (operator_name_info[(int) code].mangled_name);
2793
2794 for (t = placement; t; t = TREE_CHAIN (t))
2795 write_expression (TREE_VALUE (t));
2796
2797 write_char ('_');
2798
2799 if (nelts)
2800 {
2801 tree domain;
2802 ++processing_template_decl;
2803 domain = compute_array_index_type (NULL_TREE, nelts,
2804 tf_warning_or_error);
2805 type = build_cplus_array_type (type, domain);
2806 --processing_template_decl;
2807 }
2808 write_type (type);
2809
2810 if (init && TREE_CODE (init) == TREE_LIST
2811 && TREE_CODE (TREE_VALUE (init)) == CONSTRUCTOR
2812 && CONSTRUCTOR_IS_DIRECT_INIT (TREE_VALUE (init)))
2813 write_expression (TREE_VALUE (init));
2814 else
2815 {
2816 if (init)
2817 write_string ("pi");
2818 if (init && init != void_zero_node)
2819 for (t = init; t; t = TREE_CHAIN (t))
2820 write_expression (TREE_VALUE (t));
2821 write_char ('E');
2822 }
2823 }
2824 else if (code == DELETE_EXPR || code == VEC_DELETE_EXPR)
2825 {
2826 gcc_assert (code == DELETE_EXPR);
2827 if (DELETE_EXPR_USE_VEC (expr))
2828 code = VEC_DELETE_EXPR;
2829
2830 if (DELETE_EXPR_USE_GLOBAL (expr))
2831 write_string ("gs");
2832
2833 write_string (operator_name_info[(int) code].mangled_name);
2834
2835 write_expression (TREE_OPERAND (expr, 0));
2836 }
2837 else if (code == THROW_EXPR)
2838 {
2839 tree op = TREE_OPERAND (expr, 0);
2840 if (op)
2841 {
2842 write_string ("tw");
2843 write_expression (op);
2844 }
2845 else
2846 write_string ("tr");
2847 }
2848 else if (code == CONSTRUCTOR)
2849 {
2850 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (expr);
2851 unsigned i; tree val;
2852
2853 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
2854 write_string ("il");
2855 else
2856 {
2857 write_string ("tl");
2858 write_type (TREE_TYPE (expr));
2859 }
2860 FOR_EACH_CONSTRUCTOR_VALUE (elts, i, val)
2861 write_expression (val);
2862 write_char ('E');
2863 }
2864 else if (dependent_name (expr))
2865 {
2866 write_unqualified_id (dependent_name (expr));
2867 }
2868 else
2869 {
2870 int i, len;
2871 const char *name;
2872
2873 /* When we bind a variable or function to a non-type template
2874 argument with reference type, we create an ADDR_EXPR to show
2875 the fact that the entity's address has been taken. But, we
2876 don't actually want to output a mangling code for the `&'. */
2877 if (TREE_CODE (expr) == ADDR_EXPR
2878 && TREE_TYPE (expr)
2879 && TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE)
2880 {
2881 expr = TREE_OPERAND (expr, 0);
2882 if (DECL_P (expr))
2883 {
2884 write_expression (expr);
2885 return;
2886 }
2887
2888 code = TREE_CODE (expr);
2889 }
2890
2891 if (code == COMPONENT_REF)
2892 {
2893 tree ob = TREE_OPERAND (expr, 0);
2894
2895 if (TREE_CODE (ob) == ARROW_EXPR)
2896 {
2897 write_string (operator_name_info[(int)code].mangled_name);
2898 ob = TREE_OPERAND (ob, 0);
2899 }
2900 else
2901 write_string ("dt");
2902
2903 write_expression (ob);
2904 write_member_name (TREE_OPERAND (expr, 1));
2905 return;
2906 }
2907
2908 /* If it wasn't any of those, recursively expand the expression. */
2909 name = operator_name_info[(int) code].mangled_name;
2910
2911 /* We used to mangle const_cast and static_cast like a C cast. */
2912 if (!abi_version_at_least (6)
2913 && (code == CONST_CAST_EXPR
2914 || code == STATIC_CAST_EXPR))
2915 {
2916 name = operator_name_info[CAST_EXPR].mangled_name;
2917 G.need_abi_warning = 1;
2918 }
2919
2920 if (name == NULL)
2921 {
2922 switch (code)
2923 {
2924 case TRAIT_EXPR:
2925 error ("use of built-in trait %qE in function signature; "
2926 "use library traits instead", expr);
2927 break;
2928
2929 default:
2930 sorry ("mangling %C", code);
2931 break;
2932 }
2933 return;
2934 }
2935 else
2936 write_string (name);
2937
2938 switch (code)
2939 {
2940 case CALL_EXPR:
2941 {
2942 tree fn = CALL_EXPR_FN (expr);
2943
2944 if (TREE_CODE (fn) == ADDR_EXPR)
2945 fn = TREE_OPERAND (fn, 0);
2946
2947 /* Mangle a dependent name as the name, not whatever happens to
2948 be the first function in the overload set. */
2949 if ((TREE_CODE (fn) == FUNCTION_DECL
2950 || TREE_CODE (fn) == OVERLOAD)
2951 && type_dependent_expression_p_push (expr))
2952 fn = DECL_NAME (get_first_fn (fn));
2953
2954 write_expression (fn);
2955 }
2956
2957 for (i = 0; i < call_expr_nargs (expr); ++i)
2958 write_expression (CALL_EXPR_ARG (expr, i));
2959 write_char ('E');
2960 break;
2961
2962 case CAST_EXPR:
2963 write_type (TREE_TYPE (expr));
2964 if (list_length (TREE_OPERAND (expr, 0)) == 1)
2965 write_expression (TREE_VALUE (TREE_OPERAND (expr, 0)));
2966 else
2967 {
2968 tree args = TREE_OPERAND (expr, 0);
2969 write_char ('_');
2970 for (; args; args = TREE_CHAIN (args))
2971 write_expression (TREE_VALUE (args));
2972 write_char ('E');
2973 }
2974 break;
2975
2976 case DYNAMIC_CAST_EXPR:
2977 case REINTERPRET_CAST_EXPR:
2978 case STATIC_CAST_EXPR:
2979 case CONST_CAST_EXPR:
2980 write_type (TREE_TYPE (expr));
2981 write_expression (TREE_OPERAND (expr, 0));
2982 break;
2983
2984 case PREINCREMENT_EXPR:
2985 case PREDECREMENT_EXPR:
2986 if (abi_version_at_least (6))
2987 write_char ('_');
2988 else
2989 G.need_abi_warning = 1;
2990 /* Fall through. */
2991
2992 default:
2993 /* In the middle-end, some expressions have more operands than
2994 they do in templates (and mangling). */
2995 len = cp_tree_operand_length (expr);
2996
2997 for (i = 0; i < len; ++i)
2998 {
2999 tree operand = TREE_OPERAND (expr, i);
3000 /* As a GNU extension, the middle operand of a
3001 conditional may be omitted. Since expression
3002 manglings are supposed to represent the input token
3003 stream, there's no good way to mangle such an
3004 expression without extending the C++ ABI. */
3005 if (code == COND_EXPR && i == 1 && !operand)
3006 {
3007 error ("omitted middle operand to %<?:%> operand "
3008 "cannot be mangled");
3009 continue;
3010 }
3011 write_expression (operand);
3012 }
3013 }
3014 }
3015 }
3016
3017 /* Literal subcase of non-terminal <template-arg>.
3018
3019 "Literal arguments, e.g. "A<42L>", are encoded with their type
3020 and value. Negative integer values are preceded with "n"; for
3021 example, "A<-42L>" becomes "1AILln42EE". The bool value false is
3022 encoded as 0, true as 1." */
3023
3024 static void
3025 write_template_arg_literal (const tree value)
3026 {
3027 write_char ('L');
3028 write_type (TREE_TYPE (value));
3029
3030 /* Write a null member pointer value as (type)0, regardless of its
3031 real representation. */
3032 if (null_member_pointer_value_p (value))
3033 write_integer_cst (integer_zero_node);
3034 else
3035 switch (TREE_CODE (value))
3036 {
3037 case CONST_DECL:
3038 write_integer_cst (DECL_INITIAL (value));
3039 break;
3040
3041 case INTEGER_CST:
3042 gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
3043 || integer_zerop (value) || integer_onep (value));
3044 write_integer_cst (value);
3045 break;
3046
3047 case REAL_CST:
3048 write_real_cst (value);
3049 break;
3050
3051 case COMPLEX_CST:
3052 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST
3053 && TREE_CODE (TREE_IMAGPART (value)) == INTEGER_CST)
3054 {
3055 write_integer_cst (TREE_REALPART (value));
3056 write_char ('_');
3057 write_integer_cst (TREE_IMAGPART (value));
3058 }
3059 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST
3060 && TREE_CODE (TREE_IMAGPART (value)) == REAL_CST)
3061 {
3062 write_real_cst (TREE_REALPART (value));
3063 write_char ('_');
3064 write_real_cst (TREE_IMAGPART (value));
3065 }
3066 else
3067 gcc_unreachable ();
3068 break;
3069
3070 case STRING_CST:
3071 sorry ("string literal in function template signature");
3072 break;
3073
3074 default:
3075 gcc_unreachable ();
3076 }
3077
3078 write_char ('E');
3079 }
3080
3081 /* Non-terminal <template-arg>.
3082
3083 <template-arg> ::= <type> # type
3084 ::= L <type> </value/ number> E # literal
3085 ::= LZ <name> E # external name
3086 ::= X <expression> E # expression */
3087
3088 static void
3089 write_template_arg (tree node)
3090 {
3091 enum tree_code code = TREE_CODE (node);
3092
3093 MANGLE_TRACE_TREE ("template-arg", node);
3094
3095 /* A template template parameter's argument list contains TREE_LIST
3096 nodes of which the value field is the actual argument. */
3097 if (code == TREE_LIST)
3098 {
3099 node = TREE_VALUE (node);
3100 /* If it's a decl, deal with its type instead. */
3101 if (DECL_P (node))
3102 {
3103 node = TREE_TYPE (node);
3104 code = TREE_CODE (node);
3105 }
3106 }
3107
3108 if (TREE_CODE (node) == NOP_EXPR
3109 && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
3110 {
3111 /* Template parameters can be of reference type. To maintain
3112 internal consistency, such arguments use a conversion from
3113 address of object to reference type. */
3114 gcc_assert (TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR);
3115 if (abi_version_at_least (2))
3116 node = TREE_OPERAND (TREE_OPERAND (node, 0), 0);
3117 else
3118 G.need_abi_warning = 1;
3119 }
3120
3121 if (TREE_CODE (node) == BASELINK
3122 && !type_unknown_p (node))
3123 {
3124 if (abi_version_at_least (6))
3125 node = BASELINK_FUNCTIONS (node);
3126 else
3127 /* We wrongly wrapped a class-scope function in X/E. */
3128 G.need_abi_warning = 1;
3129 }
3130
3131 if (ARGUMENT_PACK_P (node))
3132 {
3133 /* Expand the template argument pack. */
3134 tree args = ARGUMENT_PACK_ARGS (node);
3135 int i, length = TREE_VEC_LENGTH (args);
3136 if (abi_version_at_least (6))
3137 write_char ('J');
3138 else
3139 {
3140 write_char ('I');
3141 G.need_abi_warning = 1;
3142 }
3143 for (i = 0; i < length; ++i)
3144 write_template_arg (TREE_VEC_ELT (args, i));
3145 write_char ('E');
3146 }
3147 else if (TYPE_P (node))
3148 write_type (node);
3149 else if (code == TEMPLATE_DECL)
3150 /* A template appearing as a template arg is a template template arg. */
3151 write_template_template_arg (node);
3152 else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
3153 || (abi_version_at_least (2) && code == CONST_DECL)
3154 || null_member_pointer_value_p (node))
3155 write_template_arg_literal (node);
3156 else if (DECL_P (node))
3157 {
3158 /* Until ABI version 2, non-type template arguments of
3159 enumeration type were mangled using their names. */
3160 if (code == CONST_DECL && !abi_version_at_least (2))
3161 G.need_abi_warning = 1;
3162 write_char ('L');
3163 /* Until ABI version 3, the underscore before the mangled name
3164 was incorrectly omitted. */
3165 if (!abi_version_at_least (3))
3166 {
3167 G.need_abi_warning = 1;
3168 write_char ('Z');
3169 }
3170 else
3171 write_string ("_Z");
3172 write_encoding (node);
3173 write_char ('E');
3174 }
3175 else
3176 {
3177 /* Template arguments may be expressions. */
3178 write_char ('X');
3179 write_expression (node);
3180 write_char ('E');
3181 }
3182 }
3183
3184 /* <template-template-arg>
3185 ::= <name>
3186 ::= <substitution> */
3187
3188 static void
3189 write_template_template_arg (const tree decl)
3190 {
3191 MANGLE_TRACE_TREE ("template-template-arg", decl);
3192
3193 if (find_substitution (decl))
3194 return;
3195 write_name (decl, /*ignore_local_scope=*/0);
3196 add_substitution (decl);
3197 }
3198
3199
3200 /* Non-terminal <array-type>. TYPE is an ARRAY_TYPE.
3201
3202 <array-type> ::= A [</dimension/ number>] _ </element/ type>
3203 ::= A <expression> _ </element/ type>
3204
3205 "Array types encode the dimension (number of elements) and the
3206 element type. For variable length arrays, the dimension (but not
3207 the '_' separator) is omitted." */
3208
3209 static void
3210 write_array_type (const tree type)
3211 {
3212 write_char ('A');
3213 if (TYPE_DOMAIN (type))
3214 {
3215 tree index_type;
3216 tree max;
3217
3218 index_type = TYPE_DOMAIN (type);
3219 /* The INDEX_TYPE gives the upper and lower bounds of the
3220 array. */
3221 max = TYPE_MAX_VALUE (index_type);
3222 if (TREE_CODE (max) == INTEGER_CST)
3223 {
3224 /* The ABI specifies that we should mangle the number of
3225 elements in the array, not the largest allowed index. */
3226 double_int dmax = tree_to_double_int (max) + double_int_one;
3227 /* Truncate the result - this will mangle [0, SIZE_INT_MAX]
3228 number of elements as zero. */
3229 dmax = dmax.zext (TYPE_PRECISION (TREE_TYPE (max)));
3230 gcc_assert (dmax.fits_uhwi ());
3231 write_unsigned_number (dmax.low);
3232 }
3233 else
3234 {
3235 max = TREE_OPERAND (max, 0);
3236 if (!abi_version_at_least (2))
3237 {
3238 /* value_dependent_expression_p presumes nothing is
3239 dependent when PROCESSING_TEMPLATE_DECL is zero. */
3240 ++processing_template_decl;
3241 if (!value_dependent_expression_p (max))
3242 G.need_abi_warning = 1;
3243 --processing_template_decl;
3244 }
3245 write_expression (max);
3246 }
3247
3248 }
3249 write_char ('_');
3250 write_type (TREE_TYPE (type));
3251 }
3252
3253 /* Non-terminal <pointer-to-member-type> for pointer-to-member
3254 variables. TYPE is a pointer-to-member POINTER_TYPE.
3255
3256 <pointer-to-member-type> ::= M </class/ type> </member/ type> */
3257
3258 static void
3259 write_pointer_to_member_type (const tree type)
3260 {
3261 write_char ('M');
3262 write_type (TYPE_PTRMEM_CLASS_TYPE (type));
3263 write_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
3264 }
3265
3266 /* Non-terminal <template-param>. PARM is a TEMPLATE_TYPE_PARM,
3267 TEMPLATE_TEMPLATE_PARM, BOUND_TEMPLATE_TEMPLATE_PARM or a
3268 TEMPLATE_PARM_INDEX.
3269
3270 <template-param> ::= T </parameter/ number> _ */
3271
3272 static void
3273 write_template_param (const tree parm)
3274 {
3275 int parm_index;
3276
3277 MANGLE_TRACE_TREE ("template-parm", parm);
3278
3279 switch (TREE_CODE (parm))
3280 {
3281 case TEMPLATE_TYPE_PARM:
3282 case TEMPLATE_TEMPLATE_PARM:
3283 case BOUND_TEMPLATE_TEMPLATE_PARM:
3284 parm_index = TEMPLATE_TYPE_IDX (parm);
3285 break;
3286
3287 case TEMPLATE_PARM_INDEX:
3288 parm_index = TEMPLATE_PARM_IDX (parm);
3289 break;
3290
3291 default:
3292 gcc_unreachable ();
3293 }
3294
3295 write_char ('T');
3296 /* NUMBER as it appears in the mangling is (-1)-indexed, with the
3297 earliest template param denoted by `_'. */
3298 write_compact_number (parm_index);
3299 }
3300
3301 /* <template-template-param>
3302 ::= <template-param>
3303 ::= <substitution> */
3304
3305 static void
3306 write_template_template_param (const tree parm)
3307 {
3308 tree templ = NULL_TREE;
3309
3310 /* PARM, a TEMPLATE_TEMPLATE_PARM, is an instantiation of the
3311 template template parameter. The substitution candidate here is
3312 only the template. */
3313 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
3314 {
3315 templ
3316 = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
3317 if (find_substitution (templ))
3318 return;
3319 }
3320
3321 /* <template-param> encodes only the template parameter position,
3322 not its template arguments, which is fine here. */
3323 write_template_param (parm);
3324 if (templ)
3325 add_substitution (templ);
3326 }
3327
3328 /* Non-terminal <substitution>.
3329
3330 <substitution> ::= S <seq-id> _
3331 ::= S_ */
3332
3333 static void
3334 write_substitution (const int seq_id)
3335 {
3336 MANGLE_TRACE ("substitution", "");
3337
3338 write_char ('S');
3339 if (seq_id > 0)
3340 write_number (seq_id - 1, /*unsigned=*/1, 36);
3341 write_char ('_');
3342 }
3343
3344 /* Start mangling ENTITY. */
3345
3346 static inline void
3347 start_mangling (const tree entity)
3348 {
3349 G.entity = entity;
3350 G.need_abi_warning = false;
3351 obstack_free (&name_obstack, name_base);
3352 mangle_obstack = &name_obstack;
3353 name_base = obstack_alloc (&name_obstack, 0);
3354 }
3355
3356 /* Done with mangling. If WARN is true, and the name of G.entity will
3357 be mangled differently in a future version of the ABI, issue a
3358 warning. */
3359
3360 static void
3361 finish_mangling_internal (const bool warn)
3362 {
3363 if (warn_abi && warn && G.need_abi_warning)
3364 warning (OPT_Wabi, "the mangled name of %qD will change in a future "
3365 "version of GCC",
3366 G.entity);
3367
3368 /* Clear all the substitutions. */
3369 vec_safe_truncate (G.substitutions, 0);
3370
3371 /* Null-terminate the string. */
3372 write_char ('\0');
3373 }
3374
3375
3376 /* Like finish_mangling_internal, but return the mangled string. */
3377
3378 static inline const char *
3379 finish_mangling (const bool warn)
3380 {
3381 finish_mangling_internal (warn);
3382 return (const char *) obstack_finish (mangle_obstack);
3383 }
3384
3385 /* Like finish_mangling_internal, but return an identifier. */
3386
3387 static tree
3388 finish_mangling_get_identifier (const bool warn)
3389 {
3390 finish_mangling_internal (warn);
3391 /* Don't obstack_finish here, and the next start_mangling will
3392 remove the identifier. */
3393 return get_identifier ((const char *) obstack_base (mangle_obstack));
3394 }
3395
3396 /* Initialize data structures for mangling. */
3397
3398 void
3399 init_mangle (void)
3400 {
3401 gcc_obstack_init (&name_obstack);
3402 name_base = obstack_alloc (&name_obstack, 0);
3403 vec_alloc (G.substitutions, 0);
3404
3405 /* Cache these identifiers for quick comparison when checking for
3406 standard substitutions. */
3407 subst_identifiers[SUBID_ALLOCATOR] = get_identifier ("allocator");
3408 subst_identifiers[SUBID_BASIC_STRING] = get_identifier ("basic_string");
3409 subst_identifiers[SUBID_CHAR_TRAITS] = get_identifier ("char_traits");
3410 subst_identifiers[SUBID_BASIC_ISTREAM] = get_identifier ("basic_istream");
3411 subst_identifiers[SUBID_BASIC_OSTREAM] = get_identifier ("basic_ostream");
3412 subst_identifiers[SUBID_BASIC_IOSTREAM] = get_identifier ("basic_iostream");
3413 }
3414
3415 /* Generate the mangled name of DECL. */
3416
3417 static tree
3418 mangle_decl_string (const tree decl)
3419 {
3420 tree result;
3421 location_t saved_loc = input_location;
3422 tree saved_fn = NULL_TREE;
3423 bool template_p = false;
3424
3425 /* We shouldn't be trying to mangle an uninstantiated template. */
3426 gcc_assert (!type_dependent_expression_p (decl));
3427
3428 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3429 {
3430 struct tinst_level *tl = current_instantiation ();
3431 if ((!tl || tl->decl != decl)
3432 && push_tinst_level (decl))
3433 {
3434 template_p = true;
3435 saved_fn = current_function_decl;
3436 current_function_decl = NULL_TREE;
3437 }
3438 }
3439 input_location = DECL_SOURCE_LOCATION (decl);
3440
3441 start_mangling (decl);
3442
3443 if (TREE_CODE (decl) == TYPE_DECL)
3444 write_type (TREE_TYPE (decl));
3445 else
3446 write_mangled_name (decl, true);
3447
3448 result = finish_mangling_get_identifier (/*warn=*/true);
3449 if (DEBUG_MANGLE)
3450 fprintf (stderr, "mangle_decl_string = '%s'\n\n",
3451 IDENTIFIER_POINTER (result));
3452
3453 if (template_p)
3454 {
3455 pop_tinst_level ();
3456 current_function_decl = saved_fn;
3457 }
3458 input_location = saved_loc;
3459
3460 return result;
3461 }
3462
3463 /* Return an identifier for the external mangled name of DECL. */
3464
3465 static tree
3466 get_mangled_id (tree decl)
3467 {
3468 tree id = mangle_decl_string (decl);
3469 return targetm.mangle_decl_assembler_name (decl, id);
3470 }
3471
3472 /* Create an identifier for the external mangled name of DECL. */
3473
3474 void
3475 mangle_decl (const tree decl)
3476 {
3477 tree id;
3478 bool dep;
3479
3480 /* Don't bother mangling uninstantiated templates. */
3481 ++processing_template_decl;
3482 if (TREE_CODE (decl) == TYPE_DECL)
3483 dep = dependent_type_p (TREE_TYPE (decl));
3484 else
3485 dep = (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
3486 && any_dependent_template_arguments_p (DECL_TI_ARGS (decl)));
3487 --processing_template_decl;
3488 if (dep)
3489 return;
3490
3491 id = get_mangled_id (decl);
3492 SET_DECL_ASSEMBLER_NAME (decl, id);
3493
3494 if (G.need_abi_warning
3495 /* Don't do this for a fake symbol we aren't going to emit anyway. */
3496 && !DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
3497 && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3498 {
3499 #ifdef ASM_OUTPUT_DEF
3500 /* If the mangling will change in the future, emit an alias with the
3501 future mangled name for forward-compatibility. */
3502 int save_ver;
3503 tree id2, alias;
3504 #endif
3505
3506 SET_IDENTIFIER_GLOBAL_VALUE (id, decl);
3507 if (IDENTIFIER_GLOBAL_VALUE (id) != decl)
3508 inform (DECL_SOURCE_LOCATION (decl), "-fabi-version=6 (or =0) "
3509 "avoids this error with a change in mangling");
3510
3511 #ifdef ASM_OUTPUT_DEF
3512 save_ver = flag_abi_version;
3513 flag_abi_version = 0;
3514 id2 = mangle_decl_string (decl);
3515 id2 = targetm.mangle_decl_assembler_name (decl, id2);
3516 flag_abi_version = save_ver;
3517
3518 alias = make_alias_for (decl, id2);
3519 DECL_IGNORED_P (alias) = 1;
3520 TREE_PUBLIC (alias) = TREE_PUBLIC (decl);
3521 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl);
3522 if (vague_linkage_p (decl))
3523 DECL_WEAK (alias) = 1;
3524 if (TREE_CODE (decl) == FUNCTION_DECL)
3525 cgraph_same_body_alias (cgraph_get_create_node (decl), alias, decl);
3526 else
3527 varpool_extra_name_alias (alias, decl);
3528 #endif
3529 }
3530 }
3531
3532 /* Generate the mangled representation of TYPE. */
3533
3534 const char *
3535 mangle_type_string (const tree type)
3536 {
3537 const char *result;
3538
3539 start_mangling (type);
3540 write_type (type);
3541 result = finish_mangling (/*warn=*/false);
3542 if (DEBUG_MANGLE)
3543 fprintf (stderr, "mangle_type_string = '%s'\n\n", result);
3544 return result;
3545 }
3546
3547 /* Create an identifier for the mangled name of a special component
3548 for belonging to TYPE. CODE is the ABI-specified code for this
3549 component. */
3550
3551 static tree
3552 mangle_special_for_type (const tree type, const char *code)
3553 {
3554 tree result;
3555
3556 /* We don't have an actual decl here for the special component, so
3557 we can't just process the <encoded-name>. Instead, fake it. */
3558 start_mangling (type);
3559
3560 /* Start the mangling. */
3561 write_string ("_Z");
3562 write_string (code);
3563
3564 /* Add the type. */
3565 write_type (type);
3566 result = finish_mangling_get_identifier (/*warn=*/false);
3567
3568 if (DEBUG_MANGLE)
3569 fprintf (stderr, "mangle_special_for_type = %s\n\n",
3570 IDENTIFIER_POINTER (result));
3571
3572 return result;
3573 }
3574
3575 /* Create an identifier for the mangled representation of the typeinfo
3576 structure for TYPE. */
3577
3578 tree
3579 mangle_typeinfo_for_type (const tree type)
3580 {
3581 return mangle_special_for_type (type, "TI");
3582 }
3583
3584 /* Create an identifier for the mangled name of the NTBS containing
3585 the mangled name of TYPE. */
3586
3587 tree
3588 mangle_typeinfo_string_for_type (const tree type)
3589 {
3590 return mangle_special_for_type (type, "TS");
3591 }
3592
3593 /* Create an identifier for the mangled name of the vtable for TYPE. */
3594
3595 tree
3596 mangle_vtbl_for_type (const tree type)
3597 {
3598 return mangle_special_for_type (type, "TV");
3599 }
3600
3601 /* Returns an identifier for the mangled name of the VTT for TYPE. */
3602
3603 tree
3604 mangle_vtt_for_type (const tree type)
3605 {
3606 return mangle_special_for_type (type, "TT");
3607 }
3608
3609 /* Return an identifier for a construction vtable group. TYPE is
3610 the most derived class in the hierarchy; BINFO is the base
3611 subobject for which this construction vtable group will be used.
3612
3613 This mangling isn't part of the ABI specification; in the ABI
3614 specification, the vtable group is dumped in the same COMDAT as the
3615 main vtable, and is referenced only from that vtable, so it doesn't
3616 need an external name. For binary formats without COMDAT sections,
3617 though, we need external names for the vtable groups.
3618
3619 We use the production
3620
3621 <special-name> ::= CT <type> <offset number> _ <base type> */
3622
3623 tree
3624 mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
3625 {
3626 tree result;
3627
3628 start_mangling (type);
3629
3630 write_string ("_Z");
3631 write_string ("TC");
3632 write_type (type);
3633 write_integer_cst (BINFO_OFFSET (binfo));
3634 write_char ('_');
3635 write_type (BINFO_TYPE (binfo));
3636
3637 result = finish_mangling_get_identifier (/*warn=*/false);
3638 if (DEBUG_MANGLE)
3639 fprintf (stderr, "mangle_ctor_vtbl_for_type = %s\n\n",
3640 IDENTIFIER_POINTER (result));
3641 return result;
3642 }
3643
3644 /* Mangle a this pointer or result pointer adjustment.
3645
3646 <call-offset> ::= h <fixed offset number> _
3647 ::= v <fixed offset number> _ <virtual offset number> _ */
3648
3649 static void
3650 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
3651 {
3652 write_char (virtual_offset ? 'v' : 'h');
3653
3654 /* For either flavor, write the fixed offset. */
3655 write_integer_cst (fixed_offset);
3656 write_char ('_');
3657
3658 /* For a virtual thunk, add the virtual offset. */
3659 if (virtual_offset)
3660 {
3661 write_integer_cst (virtual_offset);
3662 write_char ('_');
3663 }
3664 }
3665
3666 /* Return an identifier for the mangled name of a this-adjusting or
3667 covariant thunk to FN_DECL. FIXED_OFFSET is the initial adjustment
3668 to this used to find the vptr. If VIRTUAL_OFFSET is non-NULL, this
3669 is a virtual thunk, and it is the vtbl offset in
3670 bytes. THIS_ADJUSTING is nonzero for a this adjusting thunk and
3671 zero for a covariant thunk. Note, that FN_DECL might be a covariant
3672 thunk itself. A covariant thunk name always includes the adjustment
3673 for the this pointer, even if there is none.
3674
3675 <special-name> ::= T <call-offset> <base encoding>
3676 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
3677 <base encoding> */
3678
3679 tree
3680 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
3681 tree virtual_offset)
3682 {
3683 tree result;
3684
3685 start_mangling (fn_decl);
3686
3687 write_string ("_Z");
3688 write_char ('T');
3689
3690 if (!this_adjusting)
3691 {
3692 /* Covariant thunk with no this adjustment */
3693 write_char ('c');
3694 mangle_call_offset (integer_zero_node, NULL_TREE);
3695 mangle_call_offset (fixed_offset, virtual_offset);
3696 }
3697 else if (!DECL_THUNK_P (fn_decl))
3698 /* Plain this adjusting thunk. */
3699 mangle_call_offset (fixed_offset, virtual_offset);
3700 else
3701 {
3702 /* This adjusting thunk to covariant thunk. */
3703 write_char ('c');
3704 mangle_call_offset (fixed_offset, virtual_offset);
3705 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn_decl));
3706 virtual_offset = THUNK_VIRTUAL_OFFSET (fn_decl);
3707 if (virtual_offset)
3708 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
3709 mangle_call_offset (fixed_offset, virtual_offset);
3710 fn_decl = THUNK_TARGET (fn_decl);
3711 }
3712
3713 /* Scoped name. */
3714 write_encoding (fn_decl);
3715
3716 result = finish_mangling_get_identifier (/*warn=*/false);
3717 if (DEBUG_MANGLE)
3718 fprintf (stderr, "mangle_thunk = %s\n\n", IDENTIFIER_POINTER (result));
3719 return result;
3720 }
3721
3722 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
3723 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
3724 TYPE. */
3725
3726 static GTY ((param_is (union tree_node))) htab_t conv_type_names;
3727
3728 /* Hash a node (VAL1) in the table. */
3729
3730 static hashval_t
3731 hash_type (const void *val)
3732 {
3733 return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
3734 }
3735
3736 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE). */
3737
3738 static int
3739 compare_type (const void *val1, const void *val2)
3740 {
3741 return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
3742 }
3743
3744 /* Return an identifier for the mangled unqualified name for a
3745 conversion operator to TYPE. This mangling is not specified by the
3746 ABI spec; it is only used internally. */
3747
3748 tree
3749 mangle_conv_op_name_for_type (const tree type)
3750 {
3751 void **slot;
3752 tree identifier;
3753
3754 if (type == error_mark_node)
3755 return error_mark_node;
3756
3757 if (conv_type_names == NULL)
3758 conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
3759
3760 slot = htab_find_slot_with_hash (conv_type_names, type,
3761 (hashval_t) TYPE_UID (type), INSERT);
3762 identifier = (tree)*slot;
3763 if (!identifier)
3764 {
3765 char buffer[64];
3766
3767 /* Create a unique name corresponding to TYPE. */
3768 sprintf (buffer, "operator %lu",
3769 (unsigned long) htab_elements (conv_type_names));
3770 identifier = get_identifier (buffer);
3771 *slot = identifier;
3772
3773 /* Hang TYPE off the identifier so it can be found easily later
3774 when performing conversions. */
3775 TREE_TYPE (identifier) = type;
3776
3777 /* Set bits on the identifier so we know later it's a conversion. */
3778 IDENTIFIER_OPNAME_P (identifier) = 1;
3779 IDENTIFIER_TYPENAME_P (identifier) = 1;
3780 }
3781
3782 return identifier;
3783 }
3784
3785 /* Write out the appropriate string for this variable when generating
3786 another mangled name based on this one. */
3787
3788 static void
3789 write_guarded_var_name (const tree variable)
3790 {
3791 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR", 4) == 0)
3792 /* The name of a guard variable for a reference temporary should refer
3793 to the reference, not the temporary. */
3794 write_string (IDENTIFIER_POINTER (DECL_NAME (variable)) + 4);
3795 else
3796 write_name (variable, /*ignore_local_scope=*/0);
3797 }
3798
3799 /* Return an identifier for the name of an initialization guard
3800 variable for indicated VARIABLE. */
3801
3802 tree
3803 mangle_guard_variable (const tree variable)
3804 {
3805 start_mangling (variable);
3806 write_string ("_ZGV");
3807 write_guarded_var_name (variable);
3808 return finish_mangling_get_identifier (/*warn=*/false);
3809 }
3810
3811 /* Return an identifier for the name of a thread_local initialization
3812 function for VARIABLE. */
3813
3814 tree
3815 mangle_tls_init_fn (const tree variable)
3816 {
3817 start_mangling (variable);
3818 write_string ("_ZTH");
3819 write_guarded_var_name (variable);
3820 return finish_mangling_get_identifier (/*warn=*/false);
3821 }
3822
3823 /* Return an identifier for the name of a thread_local wrapper
3824 function for VARIABLE. */
3825
3826 #define TLS_WRAPPER_PREFIX "_ZTW"
3827
3828 tree
3829 mangle_tls_wrapper_fn (const tree variable)
3830 {
3831 start_mangling (variable);
3832 write_string (TLS_WRAPPER_PREFIX);
3833 write_guarded_var_name (variable);
3834 return finish_mangling_get_identifier (/*warn=*/false);
3835 }
3836
3837 /* Return true iff FN is a thread_local wrapper function. */
3838
3839 bool
3840 decl_tls_wrapper_p (const tree fn)
3841 {
3842 if (TREE_CODE (fn) != FUNCTION_DECL)
3843 return false;
3844 tree name = DECL_NAME (fn);
3845 return strncmp (IDENTIFIER_POINTER (name), TLS_WRAPPER_PREFIX,
3846 strlen (TLS_WRAPPER_PREFIX)) == 0;
3847 }
3848
3849 /* Return an identifier for the name of a temporary variable used to
3850 initialize a static reference. This isn't part of the ABI, but we might
3851 as well call them something readable. */
3852
3853 static GTY(()) int temp_count;
3854
3855 tree
3856 mangle_ref_init_variable (const tree variable)
3857 {
3858 start_mangling (variable);
3859 write_string ("_ZGR");
3860 write_name (variable, /*ignore_local_scope=*/0);
3861 /* Avoid name clashes with aggregate initialization of multiple
3862 references at once. */
3863 write_unsigned_number (temp_count++);
3864 return finish_mangling_get_identifier (/*warn=*/false);
3865 }
3866 \f
3867
3868 /* Foreign language type mangling section. */
3869
3870 /* How to write the type codes for the integer Java type. */
3871
3872 static void
3873 write_java_integer_type_codes (const tree type)
3874 {
3875 if (type == java_int_type_node)
3876 write_char ('i');
3877 else if (type == java_short_type_node)
3878 write_char ('s');
3879 else if (type == java_byte_type_node)
3880 write_char ('c');
3881 else if (type == java_char_type_node)
3882 write_char ('w');
3883 else if (type == java_long_type_node)
3884 write_char ('x');
3885 else if (type == java_boolean_type_node)
3886 write_char ('b');
3887 else
3888 gcc_unreachable ();
3889 }
3890
3891 /* Given a CLASS_TYPE, such as a record for std::bad_exception this
3892 function generates a mangled name for the vtable map variable of
3893 the class type. For example, if the class type is
3894 "std::bad_exception", the mangled name for the class is
3895 "St13bad_exception". This function would generate the name
3896 "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
3897 "_VTV<std::bad_exception>::__vtable_map". */
3898
3899
3900 char *
3901 get_mangled_vtable_map_var_name (tree class_type)
3902 {
3903 char *var_name = NULL;
3904 const char *prefix = "_ZN4_VTVI";
3905 const char *postfix = "E12__vtable_mapE";
3906
3907 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
3908
3909 tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
3910 unsigned int len = strlen (IDENTIFIER_POINTER (class_id)) +
3911 strlen (prefix) +
3912 strlen (postfix) + 1;
3913
3914 var_name = (char *) xmalloc (len);
3915
3916 sprintf (var_name, "%s%s%s", prefix, IDENTIFIER_POINTER (class_id), postfix);
3917
3918 return var_name;
3919 }
3920
3921 #include "gt-cp-mangle.h"