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