[Patch AArch64] Use 128-bit vectors when autovectorizing 16-bit float types
[official-gcc.git] / libiberty / cp-demangle.c
blobf0dbf9381c6becf02c89fc3df0dbd24bb71d7e9b
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
40 name.
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 int cplus_demangle_v3_callback(const char *mangled, int options,
46 demangle_callbackref callback)
47 int java_demangle_v3_callback(const char *mangled,
48 demangle_callbackref callback)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
57 demangle_callbackref
58 and these functions defined in this file:
59 cplus_demangle_fill_name
60 cplus_demangle_fill_extended_operator
61 cplus_demangle_fill_ctor
62 cplus_demangle_fill_dtor
63 cplus_demangle_print
64 cplus_demangle_print_callback
65 and other functions defined in the file cp-demint.c.
67 This file also defines some other functions and variables which are
68 only to be used by the file cp-demint.c.
70 Preprocessor macros you can define while compiling this file:
72 IN_LIBGCC2
73 If defined, this file defines the following functions, q.v.:
74 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
75 int *status)
76 int __gcclibcxx_demangle_callback (const char *,
77 void (*)
78 (const char *, size_t, void *),
79 void *)
80 instead of cplus_demangle_v3[_callback]() and
81 java_demangle_v3[_callback]().
83 IN_GLIBCPP_V3
84 If defined, this file defines only __cxa_demangle() and
85 __gcclibcxx_demangle_callback(), and no other publically visible
86 functions or variables.
88 STANDALONE_DEMANGLER
89 If defined, this file defines a main() function which demangles
90 any arguments, or, if none, demangles stdin.
92 CP_DEMANGLE_DEBUG
93 If defined, turns on debugging mode, which prints information on
94 stdout about the mangled string. This is not generally useful.
96 CHECK_DEMANGLER
97 If defined, additional sanity checks will be performed. It will
98 cause some slowdown, but will allow to catch out-of-bound access
99 errors earlier. This macro is intended for testing and debugging. */
101 #if defined (_AIX) && !defined (__GNUC__)
102 #pragma alloca
103 #endif
105 #ifdef HAVE_CONFIG_H
106 #include "config.h"
107 #endif
109 #include <stdio.h>
111 #ifdef HAVE_STDLIB_H
112 #include <stdlib.h>
113 #endif
114 #ifdef HAVE_STRING_H
115 #include <string.h>
116 #endif
118 #ifdef HAVE_ALLOCA_H
119 # include <alloca.h>
120 #else
121 # ifndef alloca
122 # ifdef __GNUC__
123 # define alloca __builtin_alloca
124 # else
125 extern char *alloca ();
126 # endif /* __GNUC__ */
127 # endif /* alloca */
128 #endif /* HAVE_ALLOCA_H */
130 #ifdef HAVE_LIMITS_H
131 #include <limits.h>
132 #endif
133 #ifndef INT_MAX
134 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
135 #endif
137 #include "ansidecl.h"
138 #include "libiberty.h"
139 #include "demangle.h"
140 #include "cp-demangle.h"
142 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
143 also rename them via #define to avoid compiler errors when the
144 static definition conflicts with the extern declaration in a header
145 file. */
146 #ifdef IN_GLIBCPP_V3
148 #define CP_STATIC_IF_GLIBCPP_V3 static
150 #define cplus_demangle_fill_name d_fill_name
151 static int d_fill_name (struct demangle_component *, const char *, int);
153 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
154 static int
155 d_fill_extended_operator (struct demangle_component *, int,
156 struct demangle_component *);
158 #define cplus_demangle_fill_ctor d_fill_ctor
159 static int
160 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
161 struct demangle_component *);
163 #define cplus_demangle_fill_dtor d_fill_dtor
164 static int
165 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
166 struct demangle_component *);
168 #define cplus_demangle_mangled_name d_mangled_name
169 static struct demangle_component *d_mangled_name (struct d_info *, int);
171 #define cplus_demangle_type d_type
172 static struct demangle_component *d_type (struct d_info *);
174 #define cplus_demangle_print d_print
175 static char *d_print (int, const struct demangle_component *, int, size_t *);
177 #define cplus_demangle_print_callback d_print_callback
178 static int d_print_callback (int, const struct demangle_component *,
179 demangle_callbackref, void *);
181 #define cplus_demangle_init_info d_init_info
182 static void d_init_info (const char *, int, size_t, struct d_info *);
184 #else /* ! defined(IN_GLIBCPP_V3) */
185 #define CP_STATIC_IF_GLIBCPP_V3
186 #endif /* ! defined(IN_GLIBCPP_V3) */
188 /* See if the compiler supports dynamic arrays. */
190 #ifdef __GNUC__
191 #define CP_DYNAMIC_ARRAYS
192 #else
193 #ifdef __STDC__
194 #ifdef __STDC_VERSION__
195 #if __STDC_VERSION__ >= 199901L
196 #define CP_DYNAMIC_ARRAYS
197 #endif /* __STDC__VERSION >= 199901L */
198 #endif /* defined (__STDC_VERSION__) */
199 #endif /* defined (__STDC__) */
200 #endif /* ! defined (__GNUC__) */
202 /* We avoid pulling in the ctype tables, to prevent pulling in
203 additional unresolved symbols when this code is used in a library.
204 FIXME: Is this really a valid reason? This comes from the original
205 V3 demangler code.
207 As of this writing this file has the following undefined references
208 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
209 strcat, strlen. */
211 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
212 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
213 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
215 /* The prefix prepended by GCC to an identifier represnting the
216 anonymous namespace. */
217 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
218 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
219 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
221 /* Information we keep for the standard substitutions. */
223 struct d_standard_sub_info
225 /* The code for this substitution. */
226 char code;
227 /* The simple string it expands to. */
228 const char *simple_expansion;
229 /* The length of the simple expansion. */
230 int simple_len;
231 /* The results of a full, verbose, expansion. This is used when
232 qualifying a constructor/destructor, or when in verbose mode. */
233 const char *full_expansion;
234 /* The length of the full expansion. */
235 int full_len;
236 /* What to set the last_name field of d_info to; NULL if we should
237 not set it. This is only relevant when qualifying a
238 constructor/destructor. */
239 const char *set_last_name;
240 /* The length of set_last_name. */
241 int set_last_name_len;
244 /* Accessors for subtrees of struct demangle_component. */
246 #define d_left(dc) ((dc)->u.s_binary.left)
247 #define d_right(dc) ((dc)->u.s_binary.right)
249 /* A list of templates. This is used while printing. */
251 struct d_print_template
253 /* Next template on the list. */
254 struct d_print_template *next;
255 /* This template. */
256 const struct demangle_component *template_decl;
259 /* A list of type modifiers. This is used while printing. */
261 struct d_print_mod
263 /* Next modifier on the list. These are in the reverse of the order
264 in which they appeared in the mangled string. */
265 struct d_print_mod *next;
266 /* The modifier. */
267 const struct demangle_component *mod;
268 /* Whether this modifier was printed. */
269 int printed;
270 /* The list of templates which applies to this modifier. */
271 struct d_print_template *templates;
274 /* We use these structures to hold information during printing. */
276 struct d_growable_string
278 /* Buffer holding the result. */
279 char *buf;
280 /* Current length of data in buffer. */
281 size_t len;
282 /* Allocated size of buffer. */
283 size_t alc;
284 /* Set to 1 if we had a memory allocation failure. */
285 int allocation_failure;
288 /* Stack of components, innermost first, used to avoid loops. */
290 struct d_component_stack
292 /* This component. */
293 const struct demangle_component *dc;
294 /* This component's parent. */
295 const struct d_component_stack *parent;
298 /* A demangle component and some scope captured when it was first
299 traversed. */
301 struct d_saved_scope
303 /* The component whose scope this is. */
304 const struct demangle_component *container;
305 /* The list of templates, if any, that was current when this
306 scope was captured. */
307 struct d_print_template *templates;
310 /* Checkpoint structure to allow backtracking. This holds copies
311 of the fields of struct d_info that need to be restored
312 if a trial parse needs to be backtracked over. */
314 struct d_info_checkpoint
316 const char *n;
317 int next_comp;
318 int next_sub;
319 int did_subs;
320 int expansion;
323 enum { D_PRINT_BUFFER_LENGTH = 256 };
324 struct d_print_info
326 /* Fixed-length allocated buffer for demangled data, flushed to the
327 callback with a NUL termination once full. */
328 char buf[D_PRINT_BUFFER_LENGTH];
329 /* Current length of data in buffer. */
330 size_t len;
331 /* The last character printed, saved individually so that it survives
332 any buffer flush. */
333 char last_char;
334 /* Callback function to handle demangled buffer flush. */
335 demangle_callbackref callback;
336 /* Opaque callback argument. */
337 void *opaque;
338 /* The current list of templates, if any. */
339 struct d_print_template *templates;
340 /* The current list of modifiers (e.g., pointer, reference, etc.),
341 if any. */
342 struct d_print_mod *modifiers;
343 /* Set to 1 if we saw a demangling error. */
344 int demangle_failure;
345 /* Non-zero if we're printing a lambda argument. A template
346 parameter reference actually means 'auto'. */
347 int is_lambda_arg;
348 /* The current index into any template argument packs we are using
349 for printing, or -1 to print the whole pack. */
350 int pack_index;
351 /* Number of d_print_flush calls so far. */
352 unsigned long int flush_count;
353 /* Stack of components, innermost first, used to avoid loops. */
354 const struct d_component_stack *component_stack;
355 /* Array of saved scopes for evaluating substitutions. */
356 struct d_saved_scope *saved_scopes;
357 /* Index of the next unused saved scope in the above array. */
358 int next_saved_scope;
359 /* Number of saved scopes in the above array. */
360 int num_saved_scopes;
361 /* Array of templates for saving into scopes. */
362 struct d_print_template *copy_templates;
363 /* Index of the next unused copy template in the above array. */
364 int next_copy_template;
365 /* Number of copy templates in the above array. */
366 int num_copy_templates;
367 /* The nearest enclosing template, if any. */
368 const struct demangle_component *current_template;
371 #ifdef CP_DEMANGLE_DEBUG
372 static void d_dump (struct demangle_component *, int);
373 #endif
375 static struct demangle_component *
376 d_make_empty (struct d_info *);
378 static struct demangle_component *
379 d_make_comp (struct d_info *, enum demangle_component_type,
380 struct demangle_component *,
381 struct demangle_component *);
383 static struct demangle_component *
384 d_make_name (struct d_info *, const char *, int);
386 static struct demangle_component *
387 d_make_demangle_mangled_name (struct d_info *, const char *);
389 static struct demangle_component *
390 d_make_builtin_type (struct d_info *,
391 const struct demangle_builtin_type_info *);
393 static struct demangle_component *
394 d_make_operator (struct d_info *,
395 const struct demangle_operator_info *);
397 static struct demangle_component *
398 d_make_extended_operator (struct d_info *, int,
399 struct demangle_component *);
401 static struct demangle_component *
402 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
403 struct demangle_component *);
405 static struct demangle_component *
406 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
407 struct demangle_component *);
409 static struct demangle_component *
410 d_make_template_param (struct d_info *, int);
412 static struct demangle_component *
413 d_make_sub (struct d_info *, const char *, int);
415 static int
416 has_return_type (struct demangle_component *);
418 static int
419 is_ctor_dtor_or_conversion (struct demangle_component *);
421 static struct demangle_component *d_encoding (struct d_info *, int);
423 static struct demangle_component *d_name (struct d_info *);
425 static struct demangle_component *d_nested_name (struct d_info *);
427 static struct demangle_component *d_prefix (struct d_info *);
429 static struct demangle_component *d_unqualified_name (struct d_info *);
431 static struct demangle_component *d_source_name (struct d_info *);
433 static int d_number (struct d_info *);
435 static struct demangle_component *d_identifier (struct d_info *, int);
437 static struct demangle_component *d_operator_name (struct d_info *);
439 static struct demangle_component *d_special_name (struct d_info *);
441 static struct demangle_component *d_parmlist (struct d_info *);
443 static int d_call_offset (struct d_info *, int);
445 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
447 static struct demangle_component **
448 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
450 static struct demangle_component *
451 d_ref_qualifier (struct d_info *, struct demangle_component *);
453 static struct demangle_component *
454 d_function_type (struct d_info *);
456 static struct demangle_component *
457 d_bare_function_type (struct d_info *, int);
459 static struct demangle_component *
460 d_class_enum_type (struct d_info *);
462 static struct demangle_component *d_array_type (struct d_info *);
464 static struct demangle_component *d_vector_type (struct d_info *);
466 static struct demangle_component *
467 d_pointer_to_member_type (struct d_info *);
469 static struct demangle_component *
470 d_template_param (struct d_info *);
472 static struct demangle_component *d_template_args (struct d_info *);
473 static struct demangle_component *d_template_args_1 (struct d_info *);
475 static struct demangle_component *
476 d_template_arg (struct d_info *);
478 static struct demangle_component *d_expression (struct d_info *);
480 static struct demangle_component *d_expr_primary (struct d_info *);
482 static struct demangle_component *d_local_name (struct d_info *);
484 static int d_discriminator (struct d_info *);
486 static struct demangle_component *d_lambda (struct d_info *);
488 static struct demangle_component *d_unnamed_type (struct d_info *);
490 static struct demangle_component *
491 d_clone_suffix (struct d_info *, struct demangle_component *);
493 static int
494 d_add_substitution (struct d_info *, struct demangle_component *);
496 static struct demangle_component *d_substitution (struct d_info *, int);
498 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
500 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
502 static void d_growable_string_init (struct d_growable_string *, size_t);
504 static inline void
505 d_growable_string_resize (struct d_growable_string *, size_t);
507 static inline void
508 d_growable_string_append_buffer (struct d_growable_string *,
509 const char *, size_t);
510 static void
511 d_growable_string_callback_adapter (const char *, size_t, void *);
513 static void
514 d_print_init (struct d_print_info *, demangle_callbackref, void *,
515 const struct demangle_component *);
517 static inline void d_print_error (struct d_print_info *);
519 static inline int d_print_saw_error (struct d_print_info *);
521 static inline void d_print_flush (struct d_print_info *);
523 static inline void d_append_char (struct d_print_info *, char);
525 static inline void d_append_buffer (struct d_print_info *,
526 const char *, size_t);
528 static inline void d_append_string (struct d_print_info *, const char *);
530 static inline char d_last_char (struct d_print_info *);
532 static void
533 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
535 static void
536 d_print_java_identifier (struct d_print_info *, const char *, int);
538 static void
539 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
541 static void
542 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
544 static void
545 d_print_function_type (struct d_print_info *, int,
546 const struct demangle_component *,
547 struct d_print_mod *);
549 static void
550 d_print_array_type (struct d_print_info *, int,
551 const struct demangle_component *,
552 struct d_print_mod *);
554 static void
555 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
557 static void d_print_cast (struct d_print_info *, int,
558 const struct demangle_component *);
559 static void d_print_conversion (struct d_print_info *, int,
560 const struct demangle_component *);
562 static int d_demangle_callback (const char *, int,
563 demangle_callbackref, void *);
564 static char *d_demangle (const char *, int, size_t *);
566 /* True iff TYPE is a demangling component representing a
567 function-type-qualifier. */
569 static int
570 is_fnqual_component_type (enum demangle_component_type type)
572 return (type == DEMANGLE_COMPONENT_RESTRICT_THIS
573 || type == DEMANGLE_COMPONENT_VOLATILE_THIS
574 || type == DEMANGLE_COMPONENT_CONST_THIS
575 || type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
576 || type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
577 || type == DEMANGLE_COMPONENT_NOEXCEPT
578 || type == DEMANGLE_COMPONENT_THROW_SPEC
579 || type == DEMANGLE_COMPONENT_REFERENCE_THIS);
582 #define FNQUAL_COMPONENT_CASE \
583 case DEMANGLE_COMPONENT_RESTRICT_THIS: \
584 case DEMANGLE_COMPONENT_VOLATILE_THIS: \
585 case DEMANGLE_COMPONENT_CONST_THIS: \
586 case DEMANGLE_COMPONENT_REFERENCE_THIS: \
587 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
588 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
589 case DEMANGLE_COMPONENT_NOEXCEPT: \
590 case DEMANGLE_COMPONENT_THROW_SPEC
592 #ifdef CP_DEMANGLE_DEBUG
594 static void
595 d_dump (struct demangle_component *dc, int indent)
597 int i;
599 if (dc == NULL)
601 if (indent == 0)
602 printf ("failed demangling\n");
603 return;
606 for (i = 0; i < indent; ++i)
607 putchar (' ');
609 switch (dc->type)
611 case DEMANGLE_COMPONENT_NAME:
612 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
613 return;
614 case DEMANGLE_COMPONENT_TAGGED_NAME:
615 printf ("tagged name\n");
616 d_dump (dc->u.s_binary.left, indent + 2);
617 d_dump (dc->u.s_binary.right, indent + 2);
618 return;
619 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
620 printf ("template parameter %ld\n", dc->u.s_number.number);
621 return;
622 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
623 printf ("function parameter %ld\n", dc->u.s_number.number);
624 return;
625 case DEMANGLE_COMPONENT_CTOR:
626 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
627 d_dump (dc->u.s_ctor.name, indent + 2);
628 return;
629 case DEMANGLE_COMPONENT_DTOR:
630 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
631 d_dump (dc->u.s_dtor.name, indent + 2);
632 return;
633 case DEMANGLE_COMPONENT_SUB_STD:
634 printf ("standard substitution %s\n", dc->u.s_string.string);
635 return;
636 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
637 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
638 return;
639 case DEMANGLE_COMPONENT_OPERATOR:
640 printf ("operator %s\n", dc->u.s_operator.op->name);
641 return;
642 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
643 printf ("extended operator with %d args\n",
644 dc->u.s_extended_operator.args);
645 d_dump (dc->u.s_extended_operator.name, indent + 2);
646 return;
648 case DEMANGLE_COMPONENT_QUAL_NAME:
649 printf ("qualified name\n");
650 break;
651 case DEMANGLE_COMPONENT_LOCAL_NAME:
652 printf ("local name\n");
653 break;
654 case DEMANGLE_COMPONENT_TYPED_NAME:
655 printf ("typed name\n");
656 break;
657 case DEMANGLE_COMPONENT_TEMPLATE:
658 printf ("template\n");
659 break;
660 case DEMANGLE_COMPONENT_VTABLE:
661 printf ("vtable\n");
662 break;
663 case DEMANGLE_COMPONENT_VTT:
664 printf ("VTT\n");
665 break;
666 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
667 printf ("construction vtable\n");
668 break;
669 case DEMANGLE_COMPONENT_TYPEINFO:
670 printf ("typeinfo\n");
671 break;
672 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
673 printf ("typeinfo name\n");
674 break;
675 case DEMANGLE_COMPONENT_TYPEINFO_FN:
676 printf ("typeinfo function\n");
677 break;
678 case DEMANGLE_COMPONENT_THUNK:
679 printf ("thunk\n");
680 break;
681 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
682 printf ("virtual thunk\n");
683 break;
684 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
685 printf ("covariant thunk\n");
686 break;
687 case DEMANGLE_COMPONENT_JAVA_CLASS:
688 printf ("java class\n");
689 break;
690 case DEMANGLE_COMPONENT_GUARD:
691 printf ("guard\n");
692 break;
693 case DEMANGLE_COMPONENT_REFTEMP:
694 printf ("reference temporary\n");
695 break;
696 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
697 printf ("hidden alias\n");
698 break;
699 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
700 printf ("transaction clone\n");
701 break;
702 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
703 printf ("non-transaction clone\n");
704 break;
705 case DEMANGLE_COMPONENT_RESTRICT:
706 printf ("restrict\n");
707 break;
708 case DEMANGLE_COMPONENT_VOLATILE:
709 printf ("volatile\n");
710 break;
711 case DEMANGLE_COMPONENT_CONST:
712 printf ("const\n");
713 break;
714 case DEMANGLE_COMPONENT_RESTRICT_THIS:
715 printf ("restrict this\n");
716 break;
717 case DEMANGLE_COMPONENT_VOLATILE_THIS:
718 printf ("volatile this\n");
719 break;
720 case DEMANGLE_COMPONENT_CONST_THIS:
721 printf ("const this\n");
722 break;
723 case DEMANGLE_COMPONENT_REFERENCE_THIS:
724 printf ("reference this\n");
725 break;
726 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
727 printf ("rvalue reference this\n");
728 break;
729 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
730 printf ("transaction_safe this\n");
731 break;
732 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
733 printf ("vendor type qualifier\n");
734 break;
735 case DEMANGLE_COMPONENT_POINTER:
736 printf ("pointer\n");
737 break;
738 case DEMANGLE_COMPONENT_REFERENCE:
739 printf ("reference\n");
740 break;
741 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
742 printf ("rvalue reference\n");
743 break;
744 case DEMANGLE_COMPONENT_COMPLEX:
745 printf ("complex\n");
746 break;
747 case DEMANGLE_COMPONENT_IMAGINARY:
748 printf ("imaginary\n");
749 break;
750 case DEMANGLE_COMPONENT_VENDOR_TYPE:
751 printf ("vendor type\n");
752 break;
753 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
754 printf ("function type\n");
755 break;
756 case DEMANGLE_COMPONENT_ARRAY_TYPE:
757 printf ("array type\n");
758 break;
759 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
760 printf ("pointer to member type\n");
761 break;
762 case DEMANGLE_COMPONENT_FIXED_TYPE:
763 printf ("fixed-point type, accum? %d, sat? %d\n",
764 dc->u.s_fixed.accum, dc->u.s_fixed.sat);
765 d_dump (dc->u.s_fixed.length, indent + 2);
766 break;
767 case DEMANGLE_COMPONENT_ARGLIST:
768 printf ("argument list\n");
769 break;
770 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
771 printf ("template argument list\n");
772 break;
773 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
774 printf ("initializer list\n");
775 break;
776 case DEMANGLE_COMPONENT_CAST:
777 printf ("cast\n");
778 break;
779 case DEMANGLE_COMPONENT_CONVERSION:
780 printf ("conversion operator\n");
781 break;
782 case DEMANGLE_COMPONENT_NULLARY:
783 printf ("nullary operator\n");
784 break;
785 case DEMANGLE_COMPONENT_UNARY:
786 printf ("unary operator\n");
787 break;
788 case DEMANGLE_COMPONENT_BINARY:
789 printf ("binary operator\n");
790 break;
791 case DEMANGLE_COMPONENT_BINARY_ARGS:
792 printf ("binary operator arguments\n");
793 break;
794 case DEMANGLE_COMPONENT_TRINARY:
795 printf ("trinary operator\n");
796 break;
797 case DEMANGLE_COMPONENT_TRINARY_ARG1:
798 printf ("trinary operator arguments 1\n");
799 break;
800 case DEMANGLE_COMPONENT_TRINARY_ARG2:
801 printf ("trinary operator arguments 1\n");
802 break;
803 case DEMANGLE_COMPONENT_LITERAL:
804 printf ("literal\n");
805 break;
806 case DEMANGLE_COMPONENT_LITERAL_NEG:
807 printf ("negative literal\n");
808 break;
809 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
810 printf ("java resource\n");
811 break;
812 case DEMANGLE_COMPONENT_COMPOUND_NAME:
813 printf ("compound name\n");
814 break;
815 case DEMANGLE_COMPONENT_CHARACTER:
816 printf ("character '%c'\n", dc->u.s_character.character);
817 return;
818 case DEMANGLE_COMPONENT_NUMBER:
819 printf ("number %ld\n", dc->u.s_number.number);
820 return;
821 case DEMANGLE_COMPONENT_DECLTYPE:
822 printf ("decltype\n");
823 break;
824 case DEMANGLE_COMPONENT_PACK_EXPANSION:
825 printf ("pack expansion\n");
826 break;
827 case DEMANGLE_COMPONENT_TLS_INIT:
828 printf ("tls init function\n");
829 break;
830 case DEMANGLE_COMPONENT_TLS_WRAPPER:
831 printf ("tls wrapper function\n");
832 break;
833 case DEMANGLE_COMPONENT_DEFAULT_ARG:
834 printf ("default argument %d\n", dc->u.s_unary_num.num);
835 d_dump (dc->u.s_unary_num.sub, indent+2);
836 return;
837 case DEMANGLE_COMPONENT_LAMBDA:
838 printf ("lambda %d\n", dc->u.s_unary_num.num);
839 d_dump (dc->u.s_unary_num.sub, indent+2);
840 return;
843 d_dump (d_left (dc), indent + 2);
844 d_dump (d_right (dc), indent + 2);
847 #endif /* CP_DEMANGLE_DEBUG */
849 /* Fill in a DEMANGLE_COMPONENT_NAME. */
851 CP_STATIC_IF_GLIBCPP_V3
853 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
855 if (p == NULL || s == NULL || len == 0)
856 return 0;
857 p->type = DEMANGLE_COMPONENT_NAME;
858 p->u.s_name.s = s;
859 p->u.s_name.len = len;
860 return 1;
863 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
865 CP_STATIC_IF_GLIBCPP_V3
867 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
868 struct demangle_component *name)
870 if (p == NULL || args < 0 || name == NULL)
871 return 0;
872 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
873 p->u.s_extended_operator.args = args;
874 p->u.s_extended_operator.name = name;
875 return 1;
878 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
880 CP_STATIC_IF_GLIBCPP_V3
882 cplus_demangle_fill_ctor (struct demangle_component *p,
883 enum gnu_v3_ctor_kinds kind,
884 struct demangle_component *name)
886 if (p == NULL
887 || name == NULL
888 || (int) kind < gnu_v3_complete_object_ctor
889 || (int) kind > gnu_v3_object_ctor_group)
890 return 0;
891 p->type = DEMANGLE_COMPONENT_CTOR;
892 p->u.s_ctor.kind = kind;
893 p->u.s_ctor.name = name;
894 return 1;
897 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
899 CP_STATIC_IF_GLIBCPP_V3
901 cplus_demangle_fill_dtor (struct demangle_component *p,
902 enum gnu_v3_dtor_kinds kind,
903 struct demangle_component *name)
905 if (p == NULL
906 || name == NULL
907 || (int) kind < gnu_v3_deleting_dtor
908 || (int) kind > gnu_v3_object_dtor_group)
909 return 0;
910 p->type = DEMANGLE_COMPONENT_DTOR;
911 p->u.s_dtor.kind = kind;
912 p->u.s_dtor.name = name;
913 return 1;
916 /* Add a new component. */
918 static struct demangle_component *
919 d_make_empty (struct d_info *di)
921 struct demangle_component *p;
923 if (di->next_comp >= di->num_comps)
924 return NULL;
925 p = &di->comps[di->next_comp];
926 ++di->next_comp;
927 return p;
930 /* Add a new generic component. */
932 static struct demangle_component *
933 d_make_comp (struct d_info *di, enum demangle_component_type type,
934 struct demangle_component *left,
935 struct demangle_component *right)
937 struct demangle_component *p;
939 /* We check for errors here. A typical error would be a NULL return
940 from a subroutine. We catch those here, and return NULL
941 upward. */
942 switch (type)
944 /* These types require two parameters. */
945 case DEMANGLE_COMPONENT_QUAL_NAME:
946 case DEMANGLE_COMPONENT_LOCAL_NAME:
947 case DEMANGLE_COMPONENT_TYPED_NAME:
948 case DEMANGLE_COMPONENT_TAGGED_NAME:
949 case DEMANGLE_COMPONENT_TEMPLATE:
950 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
951 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
952 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
953 case DEMANGLE_COMPONENT_UNARY:
954 case DEMANGLE_COMPONENT_BINARY:
955 case DEMANGLE_COMPONENT_BINARY_ARGS:
956 case DEMANGLE_COMPONENT_TRINARY:
957 case DEMANGLE_COMPONENT_TRINARY_ARG1:
958 case DEMANGLE_COMPONENT_LITERAL:
959 case DEMANGLE_COMPONENT_LITERAL_NEG:
960 case DEMANGLE_COMPONENT_COMPOUND_NAME:
961 case DEMANGLE_COMPONENT_VECTOR_TYPE:
962 case DEMANGLE_COMPONENT_CLONE:
963 if (left == NULL || right == NULL)
964 return NULL;
965 break;
967 /* These types only require one parameter. */
968 case DEMANGLE_COMPONENT_VTABLE:
969 case DEMANGLE_COMPONENT_VTT:
970 case DEMANGLE_COMPONENT_TYPEINFO:
971 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
972 case DEMANGLE_COMPONENT_TYPEINFO_FN:
973 case DEMANGLE_COMPONENT_THUNK:
974 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
975 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
976 case DEMANGLE_COMPONENT_JAVA_CLASS:
977 case DEMANGLE_COMPONENT_GUARD:
978 case DEMANGLE_COMPONENT_TLS_INIT:
979 case DEMANGLE_COMPONENT_TLS_WRAPPER:
980 case DEMANGLE_COMPONENT_REFTEMP:
981 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
982 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
983 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
984 case DEMANGLE_COMPONENT_POINTER:
985 case DEMANGLE_COMPONENT_REFERENCE:
986 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
987 case DEMANGLE_COMPONENT_COMPLEX:
988 case DEMANGLE_COMPONENT_IMAGINARY:
989 case DEMANGLE_COMPONENT_VENDOR_TYPE:
990 case DEMANGLE_COMPONENT_CAST:
991 case DEMANGLE_COMPONENT_CONVERSION:
992 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
993 case DEMANGLE_COMPONENT_DECLTYPE:
994 case DEMANGLE_COMPONENT_PACK_EXPANSION:
995 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
996 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
997 case DEMANGLE_COMPONENT_NULLARY:
998 case DEMANGLE_COMPONENT_TRINARY_ARG2:
999 if (left == NULL)
1000 return NULL;
1001 break;
1003 /* This needs a right parameter, but the left parameter can be
1004 empty. */
1005 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1006 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
1007 if (right == NULL)
1008 return NULL;
1009 break;
1011 /* These are allowed to have no parameters--in some cases they
1012 will be filled in later. */
1013 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1014 case DEMANGLE_COMPONENT_RESTRICT:
1015 case DEMANGLE_COMPONENT_VOLATILE:
1016 case DEMANGLE_COMPONENT_CONST:
1017 case DEMANGLE_COMPONENT_ARGLIST:
1018 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1019 FNQUAL_COMPONENT_CASE:
1020 break;
1022 /* Other types should not be seen here. */
1023 default:
1024 return NULL;
1027 p = d_make_empty (di);
1028 if (p != NULL)
1030 p->type = type;
1031 p->u.s_binary.left = left;
1032 p->u.s_binary.right = right;
1034 return p;
1037 /* Add a new demangle mangled name component. */
1039 static struct demangle_component *
1040 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1042 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1043 return d_make_name (di, s, strlen (s));
1044 d_advance (di, 2);
1045 return d_encoding (di, 0);
1048 /* Add a new name component. */
1050 static struct demangle_component *
1051 d_make_name (struct d_info *di, const char *s, int len)
1053 struct demangle_component *p;
1055 p = d_make_empty (di);
1056 if (! cplus_demangle_fill_name (p, s, len))
1057 return NULL;
1058 return p;
1061 /* Add a new builtin type component. */
1063 static struct demangle_component *
1064 d_make_builtin_type (struct d_info *di,
1065 const struct demangle_builtin_type_info *type)
1067 struct demangle_component *p;
1069 if (type == NULL)
1070 return NULL;
1071 p = d_make_empty (di);
1072 if (p != NULL)
1074 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1075 p->u.s_builtin.type = type;
1077 return p;
1080 /* Add a new operator component. */
1082 static struct demangle_component *
1083 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1085 struct demangle_component *p;
1087 p = d_make_empty (di);
1088 if (p != NULL)
1090 p->type = DEMANGLE_COMPONENT_OPERATOR;
1091 p->u.s_operator.op = op;
1093 return p;
1096 /* Add a new extended operator component. */
1098 static struct demangle_component *
1099 d_make_extended_operator (struct d_info *di, int args,
1100 struct demangle_component *name)
1102 struct demangle_component *p;
1104 p = d_make_empty (di);
1105 if (! cplus_demangle_fill_extended_operator (p, args, name))
1106 return NULL;
1107 return p;
1110 static struct demangle_component *
1111 d_make_default_arg (struct d_info *di, int num,
1112 struct demangle_component *sub)
1114 struct demangle_component *p = d_make_empty (di);
1115 if (p)
1117 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1118 p->u.s_unary_num.num = num;
1119 p->u.s_unary_num.sub = sub;
1121 return p;
1124 /* Add a new constructor component. */
1126 static struct demangle_component *
1127 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1128 struct demangle_component *name)
1130 struct demangle_component *p;
1132 p = d_make_empty (di);
1133 if (! cplus_demangle_fill_ctor (p, kind, name))
1134 return NULL;
1135 return p;
1138 /* Add a new destructor component. */
1140 static struct demangle_component *
1141 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1142 struct demangle_component *name)
1144 struct demangle_component *p;
1146 p = d_make_empty (di);
1147 if (! cplus_demangle_fill_dtor (p, kind, name))
1148 return NULL;
1149 return p;
1152 /* Add a new template parameter. */
1154 static struct demangle_component *
1155 d_make_template_param (struct d_info *di, int i)
1157 struct demangle_component *p;
1159 p = d_make_empty (di);
1160 if (p != NULL)
1162 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1163 p->u.s_number.number = i;
1165 return p;
1168 /* Add a new function parameter. */
1170 static struct demangle_component *
1171 d_make_function_param (struct d_info *di, int i)
1173 struct demangle_component *p;
1175 p = d_make_empty (di);
1176 if (p != NULL)
1178 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1179 p->u.s_number.number = i;
1181 return p;
1184 /* Add a new standard substitution component. */
1186 static struct demangle_component *
1187 d_make_sub (struct d_info *di, const char *name, int len)
1189 struct demangle_component *p;
1191 p = d_make_empty (di);
1192 if (p != NULL)
1194 p->type = DEMANGLE_COMPONENT_SUB_STD;
1195 p->u.s_string.string = name;
1196 p->u.s_string.len = len;
1198 return p;
1201 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1203 TOP_LEVEL is non-zero when called at the top level. */
1205 CP_STATIC_IF_GLIBCPP_V3
1206 struct demangle_component *
1207 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1209 struct demangle_component *p;
1211 if (! d_check_char (di, '_')
1212 /* Allow missing _ if not at toplevel to work around a
1213 bug in G++ abi-version=2 mangling; see the comment in
1214 write_template_arg. */
1215 && top_level)
1216 return NULL;
1217 if (! d_check_char (di, 'Z'))
1218 return NULL;
1219 p = d_encoding (di, top_level);
1221 /* If at top level and parsing parameters, check for a clone
1222 suffix. */
1223 if (top_level && (di->options & DMGL_PARAMS) != 0)
1224 while (d_peek_char (di) == '.'
1225 && (IS_LOWER (d_peek_next_char (di))
1226 || d_peek_next_char (di) == '_'
1227 || IS_DIGIT (d_peek_next_char (di))))
1228 p = d_clone_suffix (di, p);
1230 return p;
1233 /* Return whether a function should have a return type. The argument
1234 is the function name, which may be qualified in various ways. The
1235 rules are that template functions have return types with some
1236 exceptions, function types which are not part of a function name
1237 mangling have return types with some exceptions, and non-template
1238 function names do not have return types. The exceptions are that
1239 constructors, destructors, and conversion operators do not have
1240 return types. */
1242 static int
1243 has_return_type (struct demangle_component *dc)
1245 if (dc == NULL)
1246 return 0;
1247 switch (dc->type)
1249 default:
1250 return 0;
1251 case DEMANGLE_COMPONENT_TEMPLATE:
1252 return ! is_ctor_dtor_or_conversion (d_left (dc));
1253 FNQUAL_COMPONENT_CASE:
1254 return has_return_type (d_left (dc));
1258 /* Return whether a name is a constructor, a destructor, or a
1259 conversion operator. */
1261 static int
1262 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1264 if (dc == NULL)
1265 return 0;
1266 switch (dc->type)
1268 default:
1269 return 0;
1270 case DEMANGLE_COMPONENT_QUAL_NAME:
1271 case DEMANGLE_COMPONENT_LOCAL_NAME:
1272 return is_ctor_dtor_or_conversion (d_right (dc));
1273 case DEMANGLE_COMPONENT_CTOR:
1274 case DEMANGLE_COMPONENT_DTOR:
1275 case DEMANGLE_COMPONENT_CONVERSION:
1276 return 1;
1280 /* <encoding> ::= <(function) name> <bare-function-type>
1281 ::= <(data) name>
1282 ::= <special-name>
1284 TOP_LEVEL is non-zero when called at the top level, in which case
1285 if DMGL_PARAMS is not set we do not demangle the function
1286 parameters. We only set this at the top level, because otherwise
1287 we would not correctly demangle names in local scopes. */
1289 static struct demangle_component *
1290 d_encoding (struct d_info *di, int top_level)
1292 char peek = d_peek_char (di);
1294 if (peek == 'G' || peek == 'T')
1295 return d_special_name (di);
1296 else
1298 struct demangle_component *dc;
1300 dc = d_name (di);
1302 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1304 /* Strip off any initial CV-qualifiers, as they really apply
1305 to the `this' parameter, and they were not output by the
1306 v2 demangler without DMGL_PARAMS. */
1307 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1308 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1309 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1310 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1311 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1312 dc = d_left (dc);
1314 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1315 there may be function-qualifiers on its right argument which
1316 really apply here; this happens when parsing a class
1317 which is local to a function. */
1318 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1320 struct demangle_component *dcr;
1322 dcr = d_right (dc);
1323 while (is_fnqual_component_type (dcr->type))
1324 dcr = d_left (dcr);
1325 dc->u.s_binary.right = dcr;
1328 return dc;
1331 peek = d_peek_char (di);
1332 if (dc == NULL || peek == '\0' || peek == 'E')
1333 return dc;
1334 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1335 d_bare_function_type (di, has_return_type (dc)));
1339 /* <tagged-name> ::= <name> B <source-name> */
1341 static struct demangle_component *
1342 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1344 struct demangle_component *hold_last_name;
1345 char peek;
1347 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1348 hold_last_name = di->last_name;
1350 while (peek = d_peek_char (di),
1351 peek == 'B')
1353 struct demangle_component *tag;
1354 d_advance (di, 1);
1355 tag = d_source_name (di);
1356 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1359 di->last_name = hold_last_name;
1361 return dc;
1364 /* <name> ::= <nested-name>
1365 ::= <unscoped-name>
1366 ::= <unscoped-template-name> <template-args>
1367 ::= <local-name>
1369 <unscoped-name> ::= <unqualified-name>
1370 ::= St <unqualified-name>
1372 <unscoped-template-name> ::= <unscoped-name>
1373 ::= <substitution>
1376 static struct demangle_component *
1377 d_name (struct d_info *di)
1379 char peek = d_peek_char (di);
1380 struct demangle_component *dc;
1382 switch (peek)
1384 case 'N':
1385 return d_nested_name (di);
1387 case 'Z':
1388 return d_local_name (di);
1390 case 'U':
1391 return d_unqualified_name (di);
1393 case 'S':
1395 int subst;
1397 if (d_peek_next_char (di) != 't')
1399 dc = d_substitution (di, 0);
1400 subst = 1;
1402 else
1404 d_advance (di, 2);
1405 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1406 d_make_name (di, "std", 3),
1407 d_unqualified_name (di));
1408 di->expansion += 3;
1409 subst = 0;
1412 if (d_peek_char (di) != 'I')
1414 /* The grammar does not permit this case to occur if we
1415 called d_substitution() above (i.e., subst == 1). We
1416 don't bother to check. */
1418 else
1420 /* This is <template-args>, which means that we just saw
1421 <unscoped-template-name>, which is a substitution
1422 candidate if we didn't just get it from a
1423 substitution. */
1424 if (! subst)
1426 if (! d_add_substitution (di, dc))
1427 return NULL;
1429 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1430 d_template_args (di));
1433 return dc;
1436 case 'L':
1437 default:
1438 dc = d_unqualified_name (di);
1439 if (d_peek_char (di) == 'I')
1441 /* This is <template-args>, which means that we just saw
1442 <unscoped-template-name>, which is a substitution
1443 candidate. */
1444 if (! d_add_substitution (di, dc))
1445 return NULL;
1446 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1447 d_template_args (di));
1449 return dc;
1453 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1454 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1457 static struct demangle_component *
1458 d_nested_name (struct d_info *di)
1460 struct demangle_component *ret;
1461 struct demangle_component **pret;
1462 struct demangle_component *rqual;
1464 if (! d_check_char (di, 'N'))
1465 return NULL;
1467 pret = d_cv_qualifiers (di, &ret, 1);
1468 if (pret == NULL)
1469 return NULL;
1471 /* Parse the ref-qualifier now and then attach it
1472 once we have something to attach it to. */
1473 rqual = d_ref_qualifier (di, NULL);
1475 *pret = d_prefix (di);
1476 if (*pret == NULL)
1477 return NULL;
1479 if (rqual)
1481 d_left (rqual) = ret;
1482 ret = rqual;
1485 if (! d_check_char (di, 'E'))
1486 return NULL;
1488 return ret;
1491 /* <prefix> ::= <prefix> <unqualified-name>
1492 ::= <template-prefix> <template-args>
1493 ::= <template-param>
1494 ::= <decltype>
1496 ::= <substitution>
1498 <template-prefix> ::= <prefix> <(template) unqualified-name>
1499 ::= <template-param>
1500 ::= <substitution>
1503 static struct demangle_component *
1504 d_prefix (struct d_info *di)
1506 struct demangle_component *ret = NULL;
1508 while (1)
1510 char peek;
1511 enum demangle_component_type comb_type;
1512 struct demangle_component *dc;
1514 peek = d_peek_char (di);
1515 if (peek == '\0')
1516 return NULL;
1518 /* The older code accepts a <local-name> here, but I don't see
1519 that in the grammar. The older code does not accept a
1520 <template-param> here. */
1522 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1523 if (peek == 'D')
1525 char peek2 = d_peek_next_char (di);
1526 if (peek2 == 'T' || peek2 == 't')
1527 /* Decltype. */
1528 dc = cplus_demangle_type (di);
1529 else
1530 /* Destructor name. */
1531 dc = d_unqualified_name (di);
1533 else if (IS_DIGIT (peek)
1534 || IS_LOWER (peek)
1535 || peek == 'C'
1536 || peek == 'U'
1537 || peek == 'L')
1538 dc = d_unqualified_name (di);
1539 else if (peek == 'S')
1540 dc = d_substitution (di, 1);
1541 else if (peek == 'I')
1543 if (ret == NULL)
1544 return NULL;
1545 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1546 dc = d_template_args (di);
1548 else if (peek == 'T')
1549 dc = d_template_param (di);
1550 else if (peek == 'E')
1551 return ret;
1552 else if (peek == 'M')
1554 /* Initializer scope for a lambda. We don't need to represent
1555 this; the normal code will just treat the variable as a type
1556 scope, which gives appropriate output. */
1557 if (ret == NULL)
1558 return NULL;
1559 d_advance (di, 1);
1560 continue;
1562 else
1563 return NULL;
1565 if (ret == NULL)
1566 ret = dc;
1567 else
1568 ret = d_make_comp (di, comb_type, ret, dc);
1570 if (peek != 'S' && d_peek_char (di) != 'E')
1572 if (! d_add_substitution (di, ret))
1573 return NULL;
1578 /* <unqualified-name> ::= <operator-name>
1579 ::= <ctor-dtor-name>
1580 ::= <source-name>
1581 ::= <local-source-name>
1583 <local-source-name> ::= L <source-name> <discriminator>
1586 static struct demangle_component *
1587 d_unqualified_name (struct d_info *di)
1589 struct demangle_component *ret;
1590 char peek;
1592 peek = d_peek_char (di);
1593 if (IS_DIGIT (peek))
1594 ret = d_source_name (di);
1595 else if (IS_LOWER (peek))
1597 if (peek == 'o' && d_peek_next_char (di) == 'n')
1598 d_advance (di, 2);
1599 ret = d_operator_name (di);
1600 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1602 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1603 if (!strcmp (ret->u.s_operator.op->code, "li"))
1604 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1605 d_source_name (di));
1608 else if (peek == 'C' || peek == 'D')
1609 ret = d_ctor_dtor_name (di);
1610 else if (peek == 'L')
1612 d_advance (di, 1);
1614 ret = d_source_name (di);
1615 if (ret == NULL)
1616 return NULL;
1617 if (! d_discriminator (di))
1618 return NULL;
1620 else if (peek == 'U')
1622 switch (d_peek_next_char (di))
1624 case 'l':
1625 ret = d_lambda (di);
1626 break;
1627 case 't':
1628 ret = d_unnamed_type (di);
1629 break;
1630 default:
1631 return NULL;
1634 else
1635 return NULL;
1637 if (d_peek_char (di) == 'B')
1638 ret = d_abi_tags (di, ret);
1639 return ret;
1642 /* <source-name> ::= <(positive length) number> <identifier> */
1644 static struct demangle_component *
1645 d_source_name (struct d_info *di)
1647 int len;
1648 struct demangle_component *ret;
1650 len = d_number (di);
1651 if (len <= 0)
1652 return NULL;
1653 ret = d_identifier (di, len);
1654 di->last_name = ret;
1655 return ret;
1658 /* number ::= [n] <(non-negative decimal integer)> */
1660 static int
1661 d_number (struct d_info *di)
1663 int negative;
1664 char peek;
1665 int ret;
1667 negative = 0;
1668 peek = d_peek_char (di);
1669 if (peek == 'n')
1671 negative = 1;
1672 d_advance (di, 1);
1673 peek = d_peek_char (di);
1676 ret = 0;
1677 while (1)
1679 if (! IS_DIGIT (peek))
1681 if (negative)
1682 ret = - ret;
1683 return ret;
1685 ret = ret * 10 + peek - '0';
1686 d_advance (di, 1);
1687 peek = d_peek_char (di);
1691 /* Like d_number, but returns a demangle_component. */
1693 static struct demangle_component *
1694 d_number_component (struct d_info *di)
1696 struct demangle_component *ret = d_make_empty (di);
1697 if (ret)
1699 ret->type = DEMANGLE_COMPONENT_NUMBER;
1700 ret->u.s_number.number = d_number (di);
1702 return ret;
1705 /* identifier ::= <(unqualified source code identifier)> */
1707 static struct demangle_component *
1708 d_identifier (struct d_info *di, int len)
1710 const char *name;
1712 name = d_str (di);
1714 if (di->send - name < len)
1715 return NULL;
1717 d_advance (di, len);
1719 /* A Java mangled name may have a trailing '$' if it is a C++
1720 keyword. This '$' is not included in the length count. We just
1721 ignore the '$'. */
1722 if ((di->options & DMGL_JAVA) != 0
1723 && d_peek_char (di) == '$')
1724 d_advance (di, 1);
1726 /* Look for something which looks like a gcc encoding of an
1727 anonymous namespace, and replace it with a more user friendly
1728 name. */
1729 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1730 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1731 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1733 const char *s;
1735 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1736 if ((*s == '.' || *s == '_' || *s == '$')
1737 && s[1] == 'N')
1739 di->expansion -= len - sizeof "(anonymous namespace)";
1740 return d_make_name (di, "(anonymous namespace)",
1741 sizeof "(anonymous namespace)" - 1);
1745 return d_make_name (di, name, len);
1748 /* operator_name ::= many different two character encodings.
1749 ::= cv <type>
1750 ::= v <digit> <source-name>
1752 This list is sorted for binary search. */
1754 #define NL(s) s, (sizeof s) - 1
1756 CP_STATIC_IF_GLIBCPP_V3
1757 const struct demangle_operator_info cplus_demangle_operators[] =
1759 { "aN", NL ("&="), 2 },
1760 { "aS", NL ("="), 2 },
1761 { "aa", NL ("&&"), 2 },
1762 { "ad", NL ("&"), 1 },
1763 { "an", NL ("&"), 2 },
1764 { "at", NL ("alignof "), 1 },
1765 { "az", NL ("alignof "), 1 },
1766 { "cc", NL ("const_cast"), 2 },
1767 { "cl", NL ("()"), 2 },
1768 { "cm", NL (","), 2 },
1769 { "co", NL ("~"), 1 },
1770 { "dV", NL ("/="), 2 },
1771 { "da", NL ("delete[] "), 1 },
1772 { "dc", NL ("dynamic_cast"), 2 },
1773 { "de", NL ("*"), 1 },
1774 { "dl", NL ("delete "), 1 },
1775 { "ds", NL (".*"), 2 },
1776 { "dt", NL ("."), 2 },
1777 { "dv", NL ("/"), 2 },
1778 { "eO", NL ("^="), 2 },
1779 { "eo", NL ("^"), 2 },
1780 { "eq", NL ("=="), 2 },
1781 { "fL", NL ("..."), 3 },
1782 { "fR", NL ("..."), 3 },
1783 { "fl", NL ("..."), 2 },
1784 { "fr", NL ("..."), 2 },
1785 { "ge", NL (">="), 2 },
1786 { "gs", NL ("::"), 1 },
1787 { "gt", NL (">"), 2 },
1788 { "ix", NL ("[]"), 2 },
1789 { "lS", NL ("<<="), 2 },
1790 { "le", NL ("<="), 2 },
1791 { "li", NL ("operator\"\" "), 1 },
1792 { "ls", NL ("<<"), 2 },
1793 { "lt", NL ("<"), 2 },
1794 { "mI", NL ("-="), 2 },
1795 { "mL", NL ("*="), 2 },
1796 { "mi", NL ("-"), 2 },
1797 { "ml", NL ("*"), 2 },
1798 { "mm", NL ("--"), 1 },
1799 { "na", NL ("new[]"), 3 },
1800 { "ne", NL ("!="), 2 },
1801 { "ng", NL ("-"), 1 },
1802 { "nt", NL ("!"), 1 },
1803 { "nw", NL ("new"), 3 },
1804 { "oR", NL ("|="), 2 },
1805 { "oo", NL ("||"), 2 },
1806 { "or", NL ("|"), 2 },
1807 { "pL", NL ("+="), 2 },
1808 { "pl", NL ("+"), 2 },
1809 { "pm", NL ("->*"), 2 },
1810 { "pp", NL ("++"), 1 },
1811 { "ps", NL ("+"), 1 },
1812 { "pt", NL ("->"), 2 },
1813 { "qu", NL ("?"), 3 },
1814 { "rM", NL ("%="), 2 },
1815 { "rS", NL (">>="), 2 },
1816 { "rc", NL ("reinterpret_cast"), 2 },
1817 { "rm", NL ("%"), 2 },
1818 { "rs", NL (">>"), 2 },
1819 { "sP", NL ("sizeof..."), 1 },
1820 { "sZ", NL ("sizeof..."), 1 },
1821 { "sc", NL ("static_cast"), 2 },
1822 { "st", NL ("sizeof "), 1 },
1823 { "sz", NL ("sizeof "), 1 },
1824 { "tr", NL ("throw"), 0 },
1825 { "tw", NL ("throw "), 1 },
1826 { NULL, NULL, 0, 0 }
1829 static struct demangle_component *
1830 d_operator_name (struct d_info *di)
1832 char c1;
1833 char c2;
1835 c1 = d_next_char (di);
1836 c2 = d_next_char (di);
1837 if (c1 == 'v' && IS_DIGIT (c2))
1838 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1839 else if (c1 == 'c' && c2 == 'v')
1841 struct demangle_component *type;
1842 int was_conversion = di->is_conversion;
1843 struct demangle_component *res;
1845 di->is_conversion = ! di->is_expression;
1846 type = cplus_demangle_type (di);
1847 if (di->is_conversion)
1848 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
1849 else
1850 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1851 di->is_conversion = was_conversion;
1852 return res;
1854 else
1856 /* LOW is the inclusive lower bound. */
1857 int low = 0;
1858 /* HIGH is the exclusive upper bound. We subtract one to ignore
1859 the sentinel at the end of the array. */
1860 int high = ((sizeof (cplus_demangle_operators)
1861 / sizeof (cplus_demangle_operators[0]))
1862 - 1);
1864 while (1)
1866 int i;
1867 const struct demangle_operator_info *p;
1869 i = low + (high - low) / 2;
1870 p = cplus_demangle_operators + i;
1872 if (c1 == p->code[0] && c2 == p->code[1])
1873 return d_make_operator (di, p);
1875 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1876 high = i;
1877 else
1878 low = i + 1;
1879 if (low == high)
1880 return NULL;
1885 static struct demangle_component *
1886 d_make_character (struct d_info *di, int c)
1888 struct demangle_component *p;
1889 p = d_make_empty (di);
1890 if (p != NULL)
1892 p->type = DEMANGLE_COMPONENT_CHARACTER;
1893 p->u.s_character.character = c;
1895 return p;
1898 static struct demangle_component *
1899 d_java_resource (struct d_info *di)
1901 struct demangle_component *p = NULL;
1902 struct demangle_component *next = NULL;
1903 int len, i;
1904 char c;
1905 const char *str;
1907 len = d_number (di);
1908 if (len <= 1)
1909 return NULL;
1911 /* Eat the leading '_'. */
1912 if (d_next_char (di) != '_')
1913 return NULL;
1914 len--;
1916 str = d_str (di);
1917 i = 0;
1919 while (len > 0)
1921 c = str[i];
1922 if (!c)
1923 return NULL;
1925 /* Each chunk is either a '$' escape... */
1926 if (c == '$')
1928 i++;
1929 switch (str[i++])
1931 case 'S':
1932 c = '/';
1933 break;
1934 case '_':
1935 c = '.';
1936 break;
1937 case '$':
1938 c = '$';
1939 break;
1940 default:
1941 return NULL;
1943 next = d_make_character (di, c);
1944 d_advance (di, i);
1945 str = d_str (di);
1946 len -= i;
1947 i = 0;
1948 if (next == NULL)
1949 return NULL;
1951 /* ... or a sequence of characters. */
1952 else
1954 while (i < len && str[i] && str[i] != '$')
1955 i++;
1957 next = d_make_name (di, str, i);
1958 d_advance (di, i);
1959 str = d_str (di);
1960 len -= i;
1961 i = 0;
1962 if (next == NULL)
1963 return NULL;
1966 if (p == NULL)
1967 p = next;
1968 else
1970 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1971 if (p == NULL)
1972 return NULL;
1976 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1978 return p;
1981 /* <special-name> ::= TV <type>
1982 ::= TT <type>
1983 ::= TI <type>
1984 ::= TS <type>
1985 ::= GV <(object) name>
1986 ::= T <call-offset> <(base) encoding>
1987 ::= Tc <call-offset> <call-offset> <(base) encoding>
1988 Also g++ extensions:
1989 ::= TC <type> <(offset) number> _ <(base) type>
1990 ::= TF <type>
1991 ::= TJ <type>
1992 ::= GR <name>
1993 ::= GA <encoding>
1994 ::= Gr <resource name>
1995 ::= GTt <encoding>
1996 ::= GTn <encoding>
1999 static struct demangle_component *
2000 d_special_name (struct d_info *di)
2002 di->expansion += 20;
2003 if (d_check_char (di, 'T'))
2005 switch (d_next_char (di))
2007 case 'V':
2008 di->expansion -= 5;
2009 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2010 cplus_demangle_type (di), NULL);
2011 case 'T':
2012 di->expansion -= 10;
2013 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2014 cplus_demangle_type (di), NULL);
2015 case 'I':
2016 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2017 cplus_demangle_type (di), NULL);
2018 case 'S':
2019 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2020 cplus_demangle_type (di), NULL);
2022 case 'h':
2023 if (! d_call_offset (di, 'h'))
2024 return NULL;
2025 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2026 d_encoding (di, 0), NULL);
2028 case 'v':
2029 if (! d_call_offset (di, 'v'))
2030 return NULL;
2031 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2032 d_encoding (di, 0), NULL);
2034 case 'c':
2035 if (! d_call_offset (di, '\0'))
2036 return NULL;
2037 if (! d_call_offset (di, '\0'))
2038 return NULL;
2039 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2040 d_encoding (di, 0), NULL);
2042 case 'C':
2044 struct demangle_component *derived_type;
2045 int offset;
2046 struct demangle_component *base_type;
2048 derived_type = cplus_demangle_type (di);
2049 offset = d_number (di);
2050 if (offset < 0)
2051 return NULL;
2052 if (! d_check_char (di, '_'))
2053 return NULL;
2054 base_type = cplus_demangle_type (di);
2055 /* We don't display the offset. FIXME: We should display
2056 it in verbose mode. */
2057 di->expansion += 5;
2058 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2059 base_type, derived_type);
2062 case 'F':
2063 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2064 cplus_demangle_type (di), NULL);
2065 case 'J':
2066 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2067 cplus_demangle_type (di), NULL);
2069 case 'H':
2070 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2071 d_name (di), NULL);
2073 case 'W':
2074 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2075 d_name (di), NULL);
2077 default:
2078 return NULL;
2081 else if (d_check_char (di, 'G'))
2083 switch (d_next_char (di))
2085 case 'V':
2086 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2088 case 'R':
2090 struct demangle_component *name = d_name (di);
2091 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2092 d_number_component (di));
2095 case 'A':
2096 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2097 d_encoding (di, 0), NULL);
2099 case 'T':
2100 switch (d_next_char (di))
2102 case 'n':
2103 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2104 d_encoding (di, 0), NULL);
2105 default:
2106 /* ??? The proposal is that other letters (such as 'h') stand
2107 for different variants of transaction cloning, such as
2108 compiling directly for hardware transaction support. But
2109 they still should all be transactional clones of some sort
2110 so go ahead and call them that. */
2111 case 't':
2112 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2113 d_encoding (di, 0), NULL);
2116 case 'r':
2117 return d_java_resource (di);
2119 default:
2120 return NULL;
2123 else
2124 return NULL;
2127 /* <call-offset> ::= h <nv-offset> _
2128 ::= v <v-offset> _
2130 <nv-offset> ::= <(offset) number>
2132 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2134 The C parameter, if not '\0', is a character we just read which is
2135 the start of the <call-offset>.
2137 We don't display the offset information anywhere. FIXME: We should
2138 display it in verbose mode. */
2140 static int
2141 d_call_offset (struct d_info *di, int c)
2143 if (c == '\0')
2144 c = d_next_char (di);
2146 if (c == 'h')
2147 d_number (di);
2148 else if (c == 'v')
2150 d_number (di);
2151 if (! d_check_char (di, '_'))
2152 return 0;
2153 d_number (di);
2155 else
2156 return 0;
2158 if (! d_check_char (di, '_'))
2159 return 0;
2161 return 1;
2164 /* <ctor-dtor-name> ::= C1
2165 ::= C2
2166 ::= C3
2167 ::= D0
2168 ::= D1
2169 ::= D2
2172 static struct demangle_component *
2173 d_ctor_dtor_name (struct d_info *di)
2175 if (di->last_name != NULL)
2177 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2178 di->expansion += di->last_name->u.s_name.len;
2179 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2180 di->expansion += di->last_name->u.s_string.len;
2182 switch (d_peek_char (di))
2184 case 'C':
2186 enum gnu_v3_ctor_kinds kind;
2187 int inheriting = 0;
2189 if (d_peek_next_char (di) == 'I')
2191 inheriting = 1;
2192 d_advance (di, 1);
2195 switch (d_peek_next_char (di))
2197 case '1':
2198 kind = gnu_v3_complete_object_ctor;
2199 break;
2200 case '2':
2201 kind = gnu_v3_base_object_ctor;
2202 break;
2203 case '3':
2204 kind = gnu_v3_complete_object_allocating_ctor;
2205 break;
2206 case '4':
2207 kind = gnu_v3_unified_ctor;
2208 break;
2209 case '5':
2210 kind = gnu_v3_object_ctor_group;
2211 break;
2212 default:
2213 return NULL;
2216 d_advance (di, 2);
2218 if (inheriting)
2219 cplus_demangle_type (di);
2221 return d_make_ctor (di, kind, di->last_name);
2224 case 'D':
2226 enum gnu_v3_dtor_kinds kind;
2228 switch (d_peek_next_char (di))
2230 case '0':
2231 kind = gnu_v3_deleting_dtor;
2232 break;
2233 case '1':
2234 kind = gnu_v3_complete_object_dtor;
2235 break;
2236 case '2':
2237 kind = gnu_v3_base_object_dtor;
2238 break;
2239 /* digit '3' is not used */
2240 case '4':
2241 kind = gnu_v3_unified_dtor;
2242 break;
2243 case '5':
2244 kind = gnu_v3_object_dtor_group;
2245 break;
2246 default:
2247 return NULL;
2249 d_advance (di, 2);
2250 return d_make_dtor (di, kind, di->last_name);
2253 default:
2254 return NULL;
2258 /* True iff we're looking at an order-insensitive type-qualifier, including
2259 function-type-qualifiers. */
2261 static int
2262 next_is_type_qual (struct d_info *di)
2264 char peek = d_peek_char (di);
2265 if (peek == 'r' || peek == 'V' || peek == 'K')
2266 return 1;
2267 if (peek == 'D')
2269 peek = d_peek_next_char (di);
2270 if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2271 return 1;
2273 return 0;
2276 /* <type> ::= <builtin-type>
2277 ::= <function-type>
2278 ::= <class-enum-type>
2279 ::= <array-type>
2280 ::= <pointer-to-member-type>
2281 ::= <template-param>
2282 ::= <template-template-param> <template-args>
2283 ::= <substitution>
2284 ::= <CV-qualifiers> <type>
2285 ::= P <type>
2286 ::= R <type>
2287 ::= O <type> (C++0x)
2288 ::= C <type>
2289 ::= G <type>
2290 ::= U <source-name> <type>
2292 <builtin-type> ::= various one letter codes
2293 ::= u <source-name>
2296 CP_STATIC_IF_GLIBCPP_V3
2297 const struct demangle_builtin_type_info
2298 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2300 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2301 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2302 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2303 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2304 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2305 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2306 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2307 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2308 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2309 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2310 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2311 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2312 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2313 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2314 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2315 D_PRINT_DEFAULT },
2316 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2317 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2318 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2319 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2320 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2321 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2322 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2323 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2324 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2325 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2326 D_PRINT_UNSIGNED_LONG_LONG },
2327 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2328 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2329 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2330 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2331 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2332 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2333 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2334 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2335 D_PRINT_DEFAULT },
2338 CP_STATIC_IF_GLIBCPP_V3
2339 struct demangle_component *
2340 cplus_demangle_type (struct d_info *di)
2342 char peek;
2343 struct demangle_component *ret;
2344 int can_subst;
2346 /* The ABI specifies that when CV-qualifiers are used, the base type
2347 is substitutable, and the fully qualified type is substitutable,
2348 but the base type with a strict subset of the CV-qualifiers is
2349 not substitutable. The natural recursive implementation of the
2350 CV-qualifiers would cause subsets to be substitutable, so instead
2351 we pull them all off now.
2353 FIXME: The ABI says that order-insensitive vendor qualifiers
2354 should be handled in the same way, but we have no way to tell
2355 which vendor qualifiers are order-insensitive and which are
2356 order-sensitive. So we just assume that they are all
2357 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2358 __vector, and it treats it as order-sensitive when mangling
2359 names. */
2361 if (next_is_type_qual (di))
2363 struct demangle_component **pret;
2365 pret = d_cv_qualifiers (di, &ret, 0);
2366 if (pret == NULL)
2367 return NULL;
2368 if (d_peek_char (di) == 'F')
2370 /* cv-qualifiers before a function type apply to 'this',
2371 so avoid adding the unqualified function type to
2372 the substitution list. */
2373 *pret = d_function_type (di);
2375 else
2376 *pret = cplus_demangle_type (di);
2377 if (!*pret)
2378 return NULL;
2379 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2380 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2382 /* Move the ref-qualifier outside the cv-qualifiers so that
2383 they are printed in the right order. */
2384 struct demangle_component *fn = d_left (*pret);
2385 d_left (*pret) = ret;
2386 ret = *pret;
2387 *pret = fn;
2389 if (! d_add_substitution (di, ret))
2390 return NULL;
2391 return ret;
2394 can_subst = 1;
2396 peek = d_peek_char (di);
2397 switch (peek)
2399 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2400 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2401 case 'o': case 's': case 't':
2402 case 'v': case 'w': case 'x': case 'y': case 'z':
2403 ret = d_make_builtin_type (di,
2404 &cplus_demangle_builtin_types[peek - 'a']);
2405 di->expansion += ret->u.s_builtin.type->len;
2406 can_subst = 0;
2407 d_advance (di, 1);
2408 break;
2410 case 'u':
2411 d_advance (di, 1);
2412 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2413 d_source_name (di), NULL);
2414 break;
2416 case 'F':
2417 ret = d_function_type (di);
2418 break;
2420 case '0': case '1': case '2': case '3': case '4':
2421 case '5': case '6': case '7': case '8': case '9':
2422 case 'N':
2423 case 'Z':
2424 ret = d_class_enum_type (di);
2425 break;
2427 case 'A':
2428 ret = d_array_type (di);
2429 break;
2431 case 'M':
2432 ret = d_pointer_to_member_type (di);
2433 break;
2435 case 'T':
2436 ret = d_template_param (di);
2437 if (d_peek_char (di) == 'I')
2439 /* This may be <template-template-param> <template-args>.
2440 If this is the type for a conversion operator, we can
2441 have a <template-template-param> here only by following
2442 a derivation like this:
2444 <nested-name>
2445 -> <template-prefix> <template-args>
2446 -> <prefix> <template-unqualified-name> <template-args>
2447 -> <unqualified-name> <template-unqualified-name> <template-args>
2448 -> <source-name> <template-unqualified-name> <template-args>
2449 -> <source-name> <operator-name> <template-args>
2450 -> <source-name> cv <type> <template-args>
2451 -> <source-name> cv <template-template-param> <template-args> <template-args>
2453 where the <template-args> is followed by another.
2454 Otherwise, we must have a derivation like this:
2456 <nested-name>
2457 -> <template-prefix> <template-args>
2458 -> <prefix> <template-unqualified-name> <template-args>
2459 -> <unqualified-name> <template-unqualified-name> <template-args>
2460 -> <source-name> <template-unqualified-name> <template-args>
2461 -> <source-name> <operator-name> <template-args>
2462 -> <source-name> cv <type> <template-args>
2463 -> <source-name> cv <template-param> <template-args>
2465 where we need to leave the <template-args> to be processed
2466 by d_prefix (following the <template-prefix>).
2468 The <template-template-param> part is a substitution
2469 candidate. */
2470 if (! di->is_conversion)
2472 if (! d_add_substitution (di, ret))
2473 return NULL;
2474 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2475 d_template_args (di));
2477 else
2479 struct demangle_component *args;
2480 struct d_info_checkpoint checkpoint;
2482 d_checkpoint (di, &checkpoint);
2483 args = d_template_args (di);
2484 if (d_peek_char (di) == 'I')
2486 if (! d_add_substitution (di, ret))
2487 return NULL;
2488 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2489 args);
2491 else
2492 d_backtrack (di, &checkpoint);
2495 break;
2497 case 'S':
2498 /* If this is a special substitution, then it is the start of
2499 <class-enum-type>. */
2501 char peek_next;
2503 peek_next = d_peek_next_char (di);
2504 if (IS_DIGIT (peek_next)
2505 || peek_next == '_'
2506 || IS_UPPER (peek_next))
2508 ret = d_substitution (di, 0);
2509 /* The substituted name may have been a template name and
2510 may be followed by tepmlate args. */
2511 if (d_peek_char (di) == 'I')
2512 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2513 d_template_args (di));
2514 else
2515 can_subst = 0;
2517 else
2519 ret = d_class_enum_type (di);
2520 /* If the substitution was a complete type, then it is not
2521 a new substitution candidate. However, if the
2522 substitution was followed by template arguments, then
2523 the whole thing is a substitution candidate. */
2524 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2525 can_subst = 0;
2528 break;
2530 case 'O':
2531 d_advance (di, 1);
2532 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2533 cplus_demangle_type (di), NULL);
2534 break;
2536 case 'P':
2537 d_advance (di, 1);
2538 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2539 cplus_demangle_type (di), NULL);
2540 break;
2542 case 'R':
2543 d_advance (di, 1);
2544 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2545 cplus_demangle_type (di), NULL);
2546 break;
2548 case 'C':
2549 d_advance (di, 1);
2550 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2551 cplus_demangle_type (di), NULL);
2552 break;
2554 case 'G':
2555 d_advance (di, 1);
2556 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2557 cplus_demangle_type (di), NULL);
2558 break;
2560 case 'U':
2561 d_advance (di, 1);
2562 ret = d_source_name (di);
2563 if (d_peek_char (di) == 'I')
2564 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2565 d_template_args (di));
2566 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2567 cplus_demangle_type (di), ret);
2568 break;
2570 case 'D':
2571 can_subst = 0;
2572 d_advance (di, 1);
2573 peek = d_next_char (di);
2574 switch (peek)
2576 case 'T':
2577 case 't':
2578 /* decltype (expression) */
2579 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2580 d_expression (di), NULL);
2581 if (ret && d_next_char (di) != 'E')
2582 ret = NULL;
2583 can_subst = 1;
2584 break;
2586 case 'p':
2587 /* Pack expansion. */
2588 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2589 cplus_demangle_type (di), NULL);
2590 can_subst = 1;
2591 break;
2593 case 'a':
2594 /* auto */
2595 ret = d_make_name (di, "auto", 4);
2596 break;
2597 case 'c':
2598 /* decltype(auto) */
2599 ret = d_make_name (di, "decltype(auto)", 14);
2600 break;
2602 case 'f':
2603 /* 32-bit decimal floating point */
2604 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2605 di->expansion += ret->u.s_builtin.type->len;
2606 break;
2607 case 'd':
2608 /* 64-bit DFP */
2609 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2610 di->expansion += ret->u.s_builtin.type->len;
2611 break;
2612 case 'e':
2613 /* 128-bit DFP */
2614 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2615 di->expansion += ret->u.s_builtin.type->len;
2616 break;
2617 case 'h':
2618 /* 16-bit half-precision FP */
2619 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2620 di->expansion += ret->u.s_builtin.type->len;
2621 break;
2622 case 's':
2623 /* char16_t */
2624 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2625 di->expansion += ret->u.s_builtin.type->len;
2626 break;
2627 case 'i':
2628 /* char32_t */
2629 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2630 di->expansion += ret->u.s_builtin.type->len;
2631 break;
2633 case 'F':
2634 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2635 ret = d_make_empty (di);
2636 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2637 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2638 /* For demangling we don't care about the bits. */
2639 d_number (di);
2640 ret->u.s_fixed.length = cplus_demangle_type (di);
2641 if (ret->u.s_fixed.length == NULL)
2642 return NULL;
2643 d_number (di);
2644 peek = d_next_char (di);
2645 ret->u.s_fixed.sat = (peek == 's');
2646 break;
2648 case 'v':
2649 ret = d_vector_type (di);
2650 can_subst = 1;
2651 break;
2653 case 'n':
2654 /* decltype(nullptr) */
2655 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2656 di->expansion += ret->u.s_builtin.type->len;
2657 break;
2659 default:
2660 return NULL;
2662 break;
2664 default:
2665 return NULL;
2668 if (can_subst)
2670 if (! d_add_substitution (di, ret))
2671 return NULL;
2674 return ret;
2677 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2679 static struct demangle_component **
2680 d_cv_qualifiers (struct d_info *di,
2681 struct demangle_component **pret, int member_fn)
2683 struct demangle_component **pstart;
2684 char peek;
2686 pstart = pret;
2687 peek = d_peek_char (di);
2688 while (next_is_type_qual (di))
2690 enum demangle_component_type t;
2691 struct demangle_component *right = NULL;
2693 d_advance (di, 1);
2694 if (peek == 'r')
2696 t = (member_fn
2697 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2698 : DEMANGLE_COMPONENT_RESTRICT);
2699 di->expansion += sizeof "restrict";
2701 else if (peek == 'V')
2703 t = (member_fn
2704 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2705 : DEMANGLE_COMPONENT_VOLATILE);
2706 di->expansion += sizeof "volatile";
2708 else if (peek == 'K')
2710 t = (member_fn
2711 ? DEMANGLE_COMPONENT_CONST_THIS
2712 : DEMANGLE_COMPONENT_CONST);
2713 di->expansion += sizeof "const";
2715 else
2717 peek = d_next_char (di);
2718 if (peek == 'x')
2720 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2721 di->expansion += sizeof "transaction_safe";
2723 else if (peek == 'o'
2724 || peek == 'O')
2726 t = DEMANGLE_COMPONENT_NOEXCEPT;
2727 di->expansion += sizeof "noexcept";
2728 if (peek == 'O')
2730 right = d_expression (di);
2731 if (right == NULL)
2732 return NULL;
2733 if (! d_check_char (di, 'E'))
2734 return NULL;
2737 else if (peek == 'w')
2739 t = DEMANGLE_COMPONENT_THROW_SPEC;
2740 di->expansion += sizeof "throw";
2741 right = d_parmlist (di);
2742 if (right == NULL)
2743 return NULL;
2744 if (! d_check_char (di, 'E'))
2745 return NULL;
2747 else
2748 return NULL;
2751 *pret = d_make_comp (di, t, NULL, right);
2752 if (*pret == NULL)
2753 return NULL;
2754 pret = &d_left (*pret);
2756 peek = d_peek_char (di);
2759 if (!member_fn && peek == 'F')
2761 while (pstart != pret)
2763 switch ((*pstart)->type)
2765 case DEMANGLE_COMPONENT_RESTRICT:
2766 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2767 break;
2768 case DEMANGLE_COMPONENT_VOLATILE:
2769 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2770 break;
2771 case DEMANGLE_COMPONENT_CONST:
2772 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2773 break;
2774 default:
2775 break;
2777 pstart = &d_left (*pstart);
2781 return pret;
2784 /* <ref-qualifier> ::= R
2785 ::= O */
2787 static struct demangle_component *
2788 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2790 struct demangle_component *ret = sub;
2791 char peek;
2793 peek = d_peek_char (di);
2794 if (peek == 'R' || peek == 'O')
2796 enum demangle_component_type t;
2797 if (peek == 'R')
2799 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2800 di->expansion += sizeof "&";
2802 else
2804 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2805 di->expansion += sizeof "&&";
2807 d_advance (di, 1);
2809 ret = d_make_comp (di, t, ret, NULL);
2812 return ret;
2815 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2817 static struct demangle_component *
2818 d_function_type (struct d_info *di)
2820 struct demangle_component *ret;
2822 if (! d_check_char (di, 'F'))
2823 return NULL;
2824 if (d_peek_char (di) == 'Y')
2826 /* Function has C linkage. We don't print this information.
2827 FIXME: We should print it in verbose mode. */
2828 d_advance (di, 1);
2830 ret = d_bare_function_type (di, 1);
2831 ret = d_ref_qualifier (di, ret);
2833 if (! d_check_char (di, 'E'))
2834 return NULL;
2835 return ret;
2838 /* <type>+ */
2840 static struct demangle_component *
2841 d_parmlist (struct d_info *di)
2843 struct demangle_component *tl;
2844 struct demangle_component **ptl;
2846 tl = NULL;
2847 ptl = &tl;
2848 while (1)
2850 struct demangle_component *type;
2852 char peek = d_peek_char (di);
2853 if (peek == '\0' || peek == 'E' || peek == '.')
2854 break;
2855 if ((peek == 'R' || peek == 'O')
2856 && d_peek_next_char (di) == 'E')
2857 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2858 break;
2859 type = cplus_demangle_type (di);
2860 if (type == NULL)
2861 return NULL;
2862 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2863 if (*ptl == NULL)
2864 return NULL;
2865 ptl = &d_right (*ptl);
2868 /* There should be at least one parameter type besides the optional
2869 return type. A function which takes no arguments will have a
2870 single parameter type void. */
2871 if (tl == NULL)
2872 return NULL;
2874 /* If we have a single parameter type void, omit it. */
2875 if (d_right (tl) == NULL
2876 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2877 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2879 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2880 d_left (tl) = NULL;
2883 return tl;
2886 /* <bare-function-type> ::= [J]<type>+ */
2888 static struct demangle_component *
2889 d_bare_function_type (struct d_info *di, int has_return_type)
2891 struct demangle_component *return_type;
2892 struct demangle_component *tl;
2893 char peek;
2895 /* Detect special qualifier indicating that the first argument
2896 is the return type. */
2897 peek = d_peek_char (di);
2898 if (peek == 'J')
2900 d_advance (di, 1);
2901 has_return_type = 1;
2904 if (has_return_type)
2906 return_type = cplus_demangle_type (di);
2907 if (return_type == NULL)
2908 return NULL;
2910 else
2911 return_type = NULL;
2913 tl = d_parmlist (di);
2914 if (tl == NULL)
2915 return NULL;
2917 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2918 return_type, tl);
2921 /* <class-enum-type> ::= <name> */
2923 static struct demangle_component *
2924 d_class_enum_type (struct d_info *di)
2926 return d_name (di);
2929 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2930 ::= A [<(dimension) expression>] _ <(element) type>
2933 static struct demangle_component *
2934 d_array_type (struct d_info *di)
2936 char peek;
2937 struct demangle_component *dim;
2939 if (! d_check_char (di, 'A'))
2940 return NULL;
2942 peek = d_peek_char (di);
2943 if (peek == '_')
2944 dim = NULL;
2945 else if (IS_DIGIT (peek))
2947 const char *s;
2949 s = d_str (di);
2952 d_advance (di, 1);
2953 peek = d_peek_char (di);
2955 while (IS_DIGIT (peek));
2956 dim = d_make_name (di, s, d_str (di) - s);
2957 if (dim == NULL)
2958 return NULL;
2960 else
2962 dim = d_expression (di);
2963 if (dim == NULL)
2964 return NULL;
2967 if (! d_check_char (di, '_'))
2968 return NULL;
2970 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2971 cplus_demangle_type (di));
2974 /* <vector-type> ::= Dv <number> _ <type>
2975 ::= Dv _ <expression> _ <type> */
2977 static struct demangle_component *
2978 d_vector_type (struct d_info *di)
2980 char peek;
2981 struct demangle_component *dim;
2983 peek = d_peek_char (di);
2984 if (peek == '_')
2986 d_advance (di, 1);
2987 dim = d_expression (di);
2989 else
2990 dim = d_number_component (di);
2992 if (dim == NULL)
2993 return NULL;
2995 if (! d_check_char (di, '_'))
2996 return NULL;
2998 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2999 cplus_demangle_type (di));
3002 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
3004 static struct demangle_component *
3005 d_pointer_to_member_type (struct d_info *di)
3007 struct demangle_component *cl;
3008 struct demangle_component *mem;
3010 if (! d_check_char (di, 'M'))
3011 return NULL;
3013 cl = cplus_demangle_type (di);
3014 if (cl == NULL)
3015 return NULL;
3017 /* The ABI says, "The type of a non-static member function is considered
3018 to be different, for the purposes of substitution, from the type of a
3019 namespace-scope or static member function whose type appears
3020 similar. The types of two non-static member functions are considered
3021 to be different, for the purposes of substitution, if the functions
3022 are members of different classes. In other words, for the purposes of
3023 substitution, the class of which the function is a member is
3024 considered part of the type of function."
3026 For a pointer to member function, this call to cplus_demangle_type
3027 will end up adding a (possibly qualified) non-member function type to
3028 the substitution table, which is not correct; however, the member
3029 function type will never be used in a substitution, so putting the
3030 wrong type in the substitution table is harmless. */
3032 mem = cplus_demangle_type (di);
3033 if (mem == NULL)
3034 return NULL;
3036 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
3039 /* <non-negative number> _ */
3041 static int
3042 d_compact_number (struct d_info *di)
3044 int num;
3045 if (d_peek_char (di) == '_')
3046 num = 0;
3047 else if (d_peek_char (di) == 'n')
3048 return -1;
3049 else
3050 num = d_number (di) + 1;
3052 if (num < 0 || ! d_check_char (di, '_'))
3053 return -1;
3054 return num;
3057 /* <template-param> ::= T_
3058 ::= T <(parameter-2 non-negative) number> _
3061 static struct demangle_component *
3062 d_template_param (struct d_info *di)
3064 int param;
3066 if (! d_check_char (di, 'T'))
3067 return NULL;
3069 param = d_compact_number (di);
3070 if (param < 0)
3071 return NULL;
3073 ++di->did_subs;
3075 return d_make_template_param (di, param);
3078 /* <template-args> ::= I <template-arg>+ E */
3080 static struct demangle_component *
3081 d_template_args (struct d_info *di)
3083 if (d_peek_char (di) != 'I'
3084 && d_peek_char (di) != 'J')
3085 return NULL;
3086 d_advance (di, 1);
3088 return d_template_args_1 (di);
3091 /* <template-arg>* E */
3093 static struct demangle_component *
3094 d_template_args_1 (struct d_info *di)
3096 struct demangle_component *hold_last_name;
3097 struct demangle_component *al;
3098 struct demangle_component **pal;
3100 /* Preserve the last name we saw--don't let the template arguments
3101 clobber it, as that would give us the wrong name for a subsequent
3102 constructor or destructor. */
3103 hold_last_name = di->last_name;
3105 if (d_peek_char (di) == 'E')
3107 /* An argument pack can be empty. */
3108 d_advance (di, 1);
3109 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3112 al = NULL;
3113 pal = &al;
3114 while (1)
3116 struct demangle_component *a;
3118 a = d_template_arg (di);
3119 if (a == NULL)
3120 return NULL;
3122 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3123 if (*pal == NULL)
3124 return NULL;
3125 pal = &d_right (*pal);
3127 if (d_peek_char (di) == 'E')
3129 d_advance (di, 1);
3130 break;
3134 di->last_name = hold_last_name;
3136 return al;
3139 /* <template-arg> ::= <type>
3140 ::= X <expression> E
3141 ::= <expr-primary>
3144 static struct demangle_component *
3145 d_template_arg (struct d_info *di)
3147 struct demangle_component *ret;
3149 switch (d_peek_char (di))
3151 case 'X':
3152 d_advance (di, 1);
3153 ret = d_expression (di);
3154 if (! d_check_char (di, 'E'))
3155 return NULL;
3156 return ret;
3158 case 'L':
3159 return d_expr_primary (di);
3161 case 'I':
3162 case 'J':
3163 /* An argument pack. */
3164 return d_template_args (di);
3166 default:
3167 return cplus_demangle_type (di);
3171 /* Parse a sequence of expressions until we hit the terminator
3172 character. */
3174 static struct demangle_component *
3175 d_exprlist (struct d_info *di, char terminator)
3177 struct demangle_component *list = NULL;
3178 struct demangle_component **p = &list;
3180 if (d_peek_char (di) == terminator)
3182 d_advance (di, 1);
3183 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3186 while (1)
3188 struct demangle_component *arg = d_expression (di);
3189 if (arg == NULL)
3190 return NULL;
3192 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3193 if (*p == NULL)
3194 return NULL;
3195 p = &d_right (*p);
3197 if (d_peek_char (di) == terminator)
3199 d_advance (di, 1);
3200 break;
3204 return list;
3207 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3208 dynamic_cast, static_cast or reinterpret_cast. */
3210 static int
3211 op_is_new_cast (struct demangle_component *op)
3213 const char *code = op->u.s_operator.op->code;
3214 return (code[1] == 'c'
3215 && (code[0] == 's' || code[0] == 'd'
3216 || code[0] == 'c' || code[0] == 'r'));
3219 /* <expression> ::= <(unary) operator-name> <expression>
3220 ::= <(binary) operator-name> <expression> <expression>
3221 ::= <(trinary) operator-name> <expression> <expression> <expression>
3222 ::= cl <expression>+ E
3223 ::= st <type>
3224 ::= <template-param>
3225 ::= sr <type> <unqualified-name>
3226 ::= sr <type> <unqualified-name> <template-args>
3227 ::= <expr-primary>
3230 static inline struct demangle_component *
3231 d_expression_1 (struct d_info *di)
3233 char peek;
3235 peek = d_peek_char (di);
3236 if (peek == 'L')
3237 return d_expr_primary (di);
3238 else if (peek == 'T')
3239 return d_template_param (di);
3240 else if (peek == 's' && d_peek_next_char (di) == 'r')
3242 struct demangle_component *type;
3243 struct demangle_component *name;
3245 d_advance (di, 2);
3246 type = cplus_demangle_type (di);
3247 name = d_unqualified_name (di);
3248 if (d_peek_char (di) != 'I')
3249 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3250 else
3251 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3252 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3253 d_template_args (di)));
3255 else if (peek == 's' && d_peek_next_char (di) == 'p')
3257 d_advance (di, 2);
3258 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3259 d_expression_1 (di), NULL);
3261 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3263 /* Function parameter used in a late-specified return type. */
3264 int index;
3265 d_advance (di, 2);
3266 if (d_peek_char (di) == 'T')
3268 /* 'this' parameter. */
3269 d_advance (di, 1);
3270 index = 0;
3272 else
3274 index = d_compact_number (di);
3275 if (index == INT_MAX || index == -1)
3276 return NULL;
3277 index++;
3279 return d_make_function_param (di, index);
3281 else if (IS_DIGIT (peek)
3282 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3284 /* We can get an unqualified name as an expression in the case of
3285 a dependent function call, i.e. decltype(f(t)). */
3286 struct demangle_component *name;
3288 if (peek == 'o')
3289 /* operator-function-id, i.e. operator+(t). */
3290 d_advance (di, 2);
3292 name = d_unqualified_name (di);
3293 if (name == NULL)
3294 return NULL;
3295 if (d_peek_char (di) == 'I')
3296 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3297 d_template_args (di));
3298 else
3299 return name;
3301 else if ((peek == 'i' || peek == 't')
3302 && d_peek_next_char (di) == 'l')
3304 /* Brace-enclosed initializer list, untyped or typed. */
3305 struct demangle_component *type = NULL;
3306 if (peek == 't')
3307 type = cplus_demangle_type (di);
3308 if (!d_peek_next_char (di))
3309 return NULL;
3310 d_advance (di, 2);
3311 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3312 type, d_exprlist (di, 'E'));
3314 else
3316 struct demangle_component *op;
3317 const char *code = NULL;
3318 int args;
3320 op = d_operator_name (di);
3321 if (op == NULL)
3322 return NULL;
3324 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3326 code = op->u.s_operator.op->code;
3327 di->expansion += op->u.s_operator.op->len - 2;
3328 if (strcmp (code, "st") == 0)
3329 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3330 cplus_demangle_type (di));
3333 switch (op->type)
3335 default:
3336 return NULL;
3337 case DEMANGLE_COMPONENT_OPERATOR:
3338 args = op->u.s_operator.op->args;
3339 break;
3340 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3341 args = op->u.s_extended_operator.args;
3342 break;
3343 case DEMANGLE_COMPONENT_CAST:
3344 args = 1;
3345 break;
3348 switch (args)
3350 case 0:
3351 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3353 case 1:
3355 struct demangle_component *operand;
3356 int suffix = 0;
3358 if (code && (code[0] == 'p' || code[0] == 'm')
3359 && code[1] == code[0])
3360 /* pp_ and mm_ are the prefix variants. */
3361 suffix = !d_check_char (di, '_');
3363 if (op->type == DEMANGLE_COMPONENT_CAST
3364 && d_check_char (di, '_'))
3365 operand = d_exprlist (di, 'E');
3366 else if (code && !strcmp (code, "sP"))
3367 operand = d_template_args_1 (di);
3368 else
3369 operand = d_expression_1 (di);
3371 if (suffix)
3372 /* Indicate the suffix variant for d_print_comp. */
3373 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3374 d_make_comp (di,
3375 DEMANGLE_COMPONENT_BINARY_ARGS,
3376 operand, operand));
3377 else
3378 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3379 operand);
3381 case 2:
3383 struct demangle_component *left;
3384 struct demangle_component *right;
3386 if (code == NULL)
3387 return NULL;
3388 if (op_is_new_cast (op))
3389 left = cplus_demangle_type (di);
3390 else if (code[0] == 'f')
3391 /* fold-expression. */
3392 left = d_operator_name (di);
3393 else
3394 left = d_expression_1 (di);
3395 if (!strcmp (code, "cl"))
3396 right = d_exprlist (di, 'E');
3397 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3399 right = d_unqualified_name (di);
3400 if (d_peek_char (di) == 'I')
3401 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3402 right, d_template_args (di));
3404 else
3405 right = d_expression_1 (di);
3407 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3408 d_make_comp (di,
3409 DEMANGLE_COMPONENT_BINARY_ARGS,
3410 left, right));
3412 case 3:
3414 struct demangle_component *first;
3415 struct demangle_component *second;
3416 struct demangle_component *third;
3418 if (code == NULL)
3419 return NULL;
3420 else if (!strcmp (code, "qu"))
3422 /* ?: expression. */
3423 first = d_expression_1 (di);
3424 second = d_expression_1 (di);
3425 third = d_expression_1 (di);
3426 if (third == NULL)
3427 return NULL;
3429 else if (code[0] == 'f')
3431 /* fold-expression. */
3432 first = d_operator_name (di);
3433 second = d_expression_1 (di);
3434 third = d_expression_1 (di);
3435 if (third == NULL)
3436 return NULL;
3438 else if (code[0] == 'n')
3440 /* new-expression. */
3441 if (code[1] != 'w' && code[1] != 'a')
3442 return NULL;
3443 first = d_exprlist (di, '_');
3444 second = cplus_demangle_type (di);
3445 if (d_peek_char (di) == 'E')
3447 d_advance (di, 1);
3448 third = NULL;
3450 else if (d_peek_char (di) == 'p'
3451 && d_peek_next_char (di) == 'i')
3453 /* Parenthesized initializer. */
3454 d_advance (di, 2);
3455 third = d_exprlist (di, 'E');
3457 else if (d_peek_char (di) == 'i'
3458 && d_peek_next_char (di) == 'l')
3459 /* initializer-list. */
3460 third = d_expression_1 (di);
3461 else
3462 return NULL;
3464 else
3465 return NULL;
3466 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3467 d_make_comp (di,
3468 DEMANGLE_COMPONENT_TRINARY_ARG1,
3469 first,
3470 d_make_comp (di,
3471 DEMANGLE_COMPONENT_TRINARY_ARG2,
3472 second, third)));
3474 default:
3475 return NULL;
3480 static struct demangle_component *
3481 d_expression (struct d_info *di)
3483 struct demangle_component *ret;
3484 int was_expression = di->is_expression;
3486 di->is_expression = 1;
3487 ret = d_expression_1 (di);
3488 di->is_expression = was_expression;
3489 return ret;
3492 /* <expr-primary> ::= L <type> <(value) number> E
3493 ::= L <type> <(value) float> E
3494 ::= L <mangled-name> E
3497 static struct demangle_component *
3498 d_expr_primary (struct d_info *di)
3500 struct demangle_component *ret;
3502 if (! d_check_char (di, 'L'))
3503 return NULL;
3504 if (d_peek_char (di) == '_'
3505 /* Workaround for G++ bug; see comment in write_template_arg. */
3506 || d_peek_char (di) == 'Z')
3507 ret = cplus_demangle_mangled_name (di, 0);
3508 else
3510 struct demangle_component *type;
3511 enum demangle_component_type t;
3512 const char *s;
3514 type = cplus_demangle_type (di);
3515 if (type == NULL)
3516 return NULL;
3518 /* If we have a type we know how to print, we aren't going to
3519 print the type name itself. */
3520 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3521 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3522 di->expansion -= type->u.s_builtin.type->len;
3524 /* Rather than try to interpret the literal value, we just
3525 collect it as a string. Note that it's possible to have a
3526 floating point literal here. The ABI specifies that the
3527 format of such literals is machine independent. That's fine,
3528 but what's not fine is that versions of g++ up to 3.2 with
3529 -fabi-version=1 used upper case letters in the hex constant,
3530 and dumped out gcc's internal representation. That makes it
3531 hard to tell where the constant ends, and hard to dump the
3532 constant in any readable form anyhow. We don't attempt to
3533 handle these cases. */
3535 t = DEMANGLE_COMPONENT_LITERAL;
3536 if (d_peek_char (di) == 'n')
3538 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3539 d_advance (di, 1);
3541 s = d_str (di);
3542 while (d_peek_char (di) != 'E')
3544 if (d_peek_char (di) == '\0')
3545 return NULL;
3546 d_advance (di, 1);
3548 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3550 if (! d_check_char (di, 'E'))
3551 return NULL;
3552 return ret;
3555 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3556 ::= Z <(function) encoding> E s [<discriminator>]
3557 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3560 static struct demangle_component *
3561 d_local_name (struct d_info *di)
3563 struct demangle_component *function;
3565 if (! d_check_char (di, 'Z'))
3566 return NULL;
3568 function = d_encoding (di, 0);
3570 if (! d_check_char (di, 'E'))
3571 return NULL;
3573 if (d_peek_char (di) == 's')
3575 d_advance (di, 1);
3576 if (! d_discriminator (di))
3577 return NULL;
3578 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3579 d_make_name (di, "string literal",
3580 sizeof "string literal" - 1));
3582 else
3584 struct demangle_component *name;
3585 int num = -1;
3587 if (d_peek_char (di) == 'd')
3589 /* Default argument scope: d <number> _. */
3590 d_advance (di, 1);
3591 num = d_compact_number (di);
3592 if (num < 0)
3593 return NULL;
3596 name = d_name (di);
3597 if (name)
3598 switch (name->type)
3600 /* Lambdas and unnamed types have internal discriminators. */
3601 case DEMANGLE_COMPONENT_LAMBDA:
3602 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3603 break;
3604 default:
3605 if (! d_discriminator (di))
3606 return NULL;
3608 if (num >= 0)
3609 name = d_make_default_arg (di, num, name);
3610 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3614 /* <discriminator> ::= _ <number> # when number < 10
3615 ::= __ <number> _ # when number >= 10
3617 <discriminator> ::= _ <number> # when number >=10
3618 is also accepted to support gcc versions that wrongly mangled that way.
3620 We demangle the discriminator, but we don't print it out. FIXME:
3621 We should print it out in verbose mode. */
3623 static int
3624 d_discriminator (struct d_info *di)
3626 int discrim, num_underscores = 1;
3628 if (d_peek_char (di) != '_')
3629 return 1;
3630 d_advance (di, 1);
3631 if (d_peek_char (di) == '_')
3633 ++num_underscores;
3634 d_advance (di, 1);
3637 discrim = d_number (di);
3638 if (discrim < 0)
3639 return 0;
3640 if (num_underscores > 1 && discrim >= 10)
3642 if (d_peek_char (di) == '_')
3643 d_advance (di, 1);
3644 else
3645 return 0;
3648 return 1;
3651 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3653 static struct demangle_component *
3654 d_lambda (struct d_info *di)
3656 struct demangle_component *tl;
3657 struct demangle_component *ret;
3658 int num;
3660 if (! d_check_char (di, 'U'))
3661 return NULL;
3662 if (! d_check_char (di, 'l'))
3663 return NULL;
3665 tl = d_parmlist (di);
3666 if (tl == NULL)
3667 return NULL;
3669 if (! d_check_char (di, 'E'))
3670 return NULL;
3672 num = d_compact_number (di);
3673 if (num < 0)
3674 return NULL;
3676 ret = d_make_empty (di);
3677 if (ret)
3679 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3680 ret->u.s_unary_num.sub = tl;
3681 ret->u.s_unary_num.num = num;
3684 if (! d_add_substitution (di, ret))
3685 return NULL;
3687 return ret;
3690 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3692 static struct demangle_component *
3693 d_unnamed_type (struct d_info *di)
3695 struct demangle_component *ret;
3696 int num;
3698 if (! d_check_char (di, 'U'))
3699 return NULL;
3700 if (! d_check_char (di, 't'))
3701 return NULL;
3703 num = d_compact_number (di);
3704 if (num < 0)
3705 return NULL;
3707 ret = d_make_empty (di);
3708 if (ret)
3710 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3711 ret->u.s_number.number = num;
3714 if (! d_add_substitution (di, ret))
3715 return NULL;
3717 return ret;
3720 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3723 static struct demangle_component *
3724 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3726 const char *suffix = d_str (di);
3727 const char *pend = suffix;
3728 struct demangle_component *n;
3730 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3732 pend += 2;
3733 while (IS_LOWER (*pend) || *pend == '_')
3734 ++pend;
3736 while (*pend == '.' && IS_DIGIT (pend[1]))
3738 pend += 2;
3739 while (IS_DIGIT (*pend))
3740 ++pend;
3742 d_advance (di, pend - suffix);
3743 n = d_make_name (di, suffix, pend - suffix);
3744 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3747 /* Add a new substitution. */
3749 static int
3750 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3752 if (dc == NULL)
3753 return 0;
3754 if (di->next_sub >= di->num_subs)
3755 return 0;
3756 di->subs[di->next_sub] = dc;
3757 ++di->next_sub;
3758 return 1;
3761 /* <substitution> ::= S <seq-id> _
3762 ::= S_
3763 ::= St
3764 ::= Sa
3765 ::= Sb
3766 ::= Ss
3767 ::= Si
3768 ::= So
3769 ::= Sd
3771 If PREFIX is non-zero, then this type is being used as a prefix in
3772 a qualified name. In this case, for the standard substitutions, we
3773 need to check whether we are being used as a prefix for a
3774 constructor or destructor, and return a full template name.
3775 Otherwise we will get something like std::iostream::~iostream()
3776 which does not correspond particularly well to any function which
3777 actually appears in the source.
3780 static const struct d_standard_sub_info standard_subs[] =
3782 { 't', NL ("std"),
3783 NL ("std"),
3784 NULL, 0 },
3785 { 'a', NL ("std::allocator"),
3786 NL ("std::allocator"),
3787 NL ("allocator") },
3788 { 'b', NL ("std::basic_string"),
3789 NL ("std::basic_string"),
3790 NL ("basic_string") },
3791 { 's', NL ("std::string"),
3792 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3793 NL ("basic_string") },
3794 { 'i', NL ("std::istream"),
3795 NL ("std::basic_istream<char, std::char_traits<char> >"),
3796 NL ("basic_istream") },
3797 { 'o', NL ("std::ostream"),
3798 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3799 NL ("basic_ostream") },
3800 { 'd', NL ("std::iostream"),
3801 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3802 NL ("basic_iostream") }
3805 static struct demangle_component *
3806 d_substitution (struct d_info *di, int prefix)
3808 char c;
3810 if (! d_check_char (di, 'S'))
3811 return NULL;
3813 c = d_next_char (di);
3814 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3816 unsigned int id;
3818 id = 0;
3819 if (c != '_')
3823 unsigned int new_id;
3825 if (IS_DIGIT (c))
3826 new_id = id * 36 + c - '0';
3827 else if (IS_UPPER (c))
3828 new_id = id * 36 + c - 'A' + 10;
3829 else
3830 return NULL;
3831 if (new_id < id)
3832 return NULL;
3833 id = new_id;
3834 c = d_next_char (di);
3836 while (c != '_');
3838 ++id;
3841 if (id >= (unsigned int) di->next_sub)
3842 return NULL;
3844 ++di->did_subs;
3846 return di->subs[id];
3848 else
3850 int verbose;
3851 const struct d_standard_sub_info *p;
3852 const struct d_standard_sub_info *pend;
3854 verbose = (di->options & DMGL_VERBOSE) != 0;
3855 if (! verbose && prefix)
3857 char peek;
3859 peek = d_peek_char (di);
3860 if (peek == 'C' || peek == 'D')
3861 verbose = 1;
3864 pend = (&standard_subs[0]
3865 + sizeof standard_subs / sizeof standard_subs[0]);
3866 for (p = &standard_subs[0]; p < pend; ++p)
3868 if (c == p->code)
3870 const char *s;
3871 int len;
3872 struct demangle_component *dc;
3874 if (p->set_last_name != NULL)
3875 di->last_name = d_make_sub (di, p->set_last_name,
3876 p->set_last_name_len);
3877 if (verbose)
3879 s = p->full_expansion;
3880 len = p->full_len;
3882 else
3884 s = p->simple_expansion;
3885 len = p->simple_len;
3887 di->expansion += len;
3888 dc = d_make_sub (di, s, len);
3889 if (d_peek_char (di) == 'B')
3891 /* If there are ABI tags on the abbreviation, it becomes
3892 a substitution candidate. */
3893 dc = d_abi_tags (di, dc);
3894 d_add_substitution (di, dc);
3896 return dc;
3900 return NULL;
3904 static void
3905 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3907 checkpoint->n = di->n;
3908 checkpoint->next_comp = di->next_comp;
3909 checkpoint->next_sub = di->next_sub;
3910 checkpoint->did_subs = di->did_subs;
3911 checkpoint->expansion = di->expansion;
3914 static void
3915 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3917 di->n = checkpoint->n;
3918 di->next_comp = checkpoint->next_comp;
3919 di->next_sub = checkpoint->next_sub;
3920 di->did_subs = checkpoint->did_subs;
3921 di->expansion = checkpoint->expansion;
3924 /* Initialize a growable string. */
3926 static void
3927 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3929 dgs->buf = NULL;
3930 dgs->len = 0;
3931 dgs->alc = 0;
3932 dgs->allocation_failure = 0;
3934 if (estimate > 0)
3935 d_growable_string_resize (dgs, estimate);
3938 /* Grow a growable string to a given size. */
3940 static inline void
3941 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3943 size_t newalc;
3944 char *newbuf;
3946 if (dgs->allocation_failure)
3947 return;
3949 /* Start allocation at two bytes to avoid any possibility of confusion
3950 with the special value of 1 used as a return in *palc to indicate
3951 allocation failures. */
3952 newalc = dgs->alc > 0 ? dgs->alc : 2;
3953 while (newalc < need)
3954 newalc <<= 1;
3956 newbuf = (char *) realloc (dgs->buf, newalc);
3957 if (newbuf == NULL)
3959 free (dgs->buf);
3960 dgs->buf = NULL;
3961 dgs->len = 0;
3962 dgs->alc = 0;
3963 dgs->allocation_failure = 1;
3964 return;
3966 dgs->buf = newbuf;
3967 dgs->alc = newalc;
3970 /* Append a buffer to a growable string. */
3972 static inline void
3973 d_growable_string_append_buffer (struct d_growable_string *dgs,
3974 const char *s, size_t l)
3976 size_t need;
3978 need = dgs->len + l + 1;
3979 if (need > dgs->alc)
3980 d_growable_string_resize (dgs, need);
3982 if (dgs->allocation_failure)
3983 return;
3985 memcpy (dgs->buf + dgs->len, s, l);
3986 dgs->buf[dgs->len + l] = '\0';
3987 dgs->len += l;
3990 /* Bridge growable strings to the callback mechanism. */
3992 static void
3993 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3995 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3997 d_growable_string_append_buffer (dgs, s, l);
4000 /* Walk the tree, counting the number of templates encountered, and
4001 the number of times a scope might be saved. These counts will be
4002 used to allocate data structures for d_print_comp, so the logic
4003 here must mirror the logic d_print_comp will use. It is not
4004 important that the resulting numbers are exact, so long as they
4005 are larger than the actual numbers encountered. */
4007 static void
4008 d_count_templates_scopes (int *num_templates, int *num_scopes,
4009 const struct demangle_component *dc)
4011 if (dc == NULL)
4012 return;
4014 switch (dc->type)
4016 case DEMANGLE_COMPONENT_NAME:
4017 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4018 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4019 case DEMANGLE_COMPONENT_SUB_STD:
4020 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4021 case DEMANGLE_COMPONENT_OPERATOR:
4022 case DEMANGLE_COMPONENT_CHARACTER:
4023 case DEMANGLE_COMPONENT_NUMBER:
4024 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4025 break;
4027 case DEMANGLE_COMPONENT_TEMPLATE:
4028 (*num_templates)++;
4029 goto recurse_left_right;
4031 case DEMANGLE_COMPONENT_REFERENCE:
4032 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4033 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4034 (*num_scopes)++;
4035 goto recurse_left_right;
4037 case DEMANGLE_COMPONENT_QUAL_NAME:
4038 case DEMANGLE_COMPONENT_LOCAL_NAME:
4039 case DEMANGLE_COMPONENT_TYPED_NAME:
4040 case DEMANGLE_COMPONENT_VTABLE:
4041 case DEMANGLE_COMPONENT_VTT:
4042 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4043 case DEMANGLE_COMPONENT_TYPEINFO:
4044 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4045 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4046 case DEMANGLE_COMPONENT_THUNK:
4047 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4048 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4049 case DEMANGLE_COMPONENT_JAVA_CLASS:
4050 case DEMANGLE_COMPONENT_GUARD:
4051 case DEMANGLE_COMPONENT_TLS_INIT:
4052 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4053 case DEMANGLE_COMPONENT_REFTEMP:
4054 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4055 case DEMANGLE_COMPONENT_RESTRICT:
4056 case DEMANGLE_COMPONENT_VOLATILE:
4057 case DEMANGLE_COMPONENT_CONST:
4058 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4059 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4060 case DEMANGLE_COMPONENT_CONST_THIS:
4061 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4062 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4063 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4064 case DEMANGLE_COMPONENT_NOEXCEPT:
4065 case DEMANGLE_COMPONENT_THROW_SPEC:
4066 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4067 case DEMANGLE_COMPONENT_POINTER:
4068 case DEMANGLE_COMPONENT_COMPLEX:
4069 case DEMANGLE_COMPONENT_IMAGINARY:
4070 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4071 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4072 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4073 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4074 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4075 case DEMANGLE_COMPONENT_ARGLIST:
4076 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4077 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4078 case DEMANGLE_COMPONENT_CAST:
4079 case DEMANGLE_COMPONENT_CONVERSION:
4080 case DEMANGLE_COMPONENT_NULLARY:
4081 case DEMANGLE_COMPONENT_UNARY:
4082 case DEMANGLE_COMPONENT_BINARY:
4083 case DEMANGLE_COMPONENT_BINARY_ARGS:
4084 case DEMANGLE_COMPONENT_TRINARY:
4085 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4086 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4087 case DEMANGLE_COMPONENT_LITERAL:
4088 case DEMANGLE_COMPONENT_LITERAL_NEG:
4089 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4090 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4091 case DEMANGLE_COMPONENT_DECLTYPE:
4092 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4093 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4094 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4095 case DEMANGLE_COMPONENT_TAGGED_NAME:
4096 case DEMANGLE_COMPONENT_CLONE:
4097 recurse_left_right:
4098 d_count_templates_scopes (num_templates, num_scopes,
4099 d_left (dc));
4100 d_count_templates_scopes (num_templates, num_scopes,
4101 d_right (dc));
4102 break;
4104 case DEMANGLE_COMPONENT_CTOR:
4105 d_count_templates_scopes (num_templates, num_scopes,
4106 dc->u.s_ctor.name);
4107 break;
4109 case DEMANGLE_COMPONENT_DTOR:
4110 d_count_templates_scopes (num_templates, num_scopes,
4111 dc->u.s_dtor.name);
4112 break;
4114 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4115 d_count_templates_scopes (num_templates, num_scopes,
4116 dc->u.s_extended_operator.name);
4117 break;
4119 case DEMANGLE_COMPONENT_FIXED_TYPE:
4120 d_count_templates_scopes (num_templates, num_scopes,
4121 dc->u.s_fixed.length);
4122 break;
4124 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4125 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4126 d_count_templates_scopes (num_templates, num_scopes,
4127 d_left (dc));
4128 break;
4130 case DEMANGLE_COMPONENT_LAMBDA:
4131 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4132 d_count_templates_scopes (num_templates, num_scopes,
4133 dc->u.s_unary_num.sub);
4134 break;
4138 /* Initialize a print information structure. */
4140 static void
4141 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4142 void *opaque, const struct demangle_component *dc)
4144 dpi->len = 0;
4145 dpi->last_char = '\0';
4146 dpi->templates = NULL;
4147 dpi->modifiers = NULL;
4148 dpi->pack_index = 0;
4149 dpi->flush_count = 0;
4151 dpi->callback = callback;
4152 dpi->opaque = opaque;
4154 dpi->demangle_failure = 0;
4155 dpi->is_lambda_arg = 0;
4157 dpi->component_stack = NULL;
4159 dpi->saved_scopes = NULL;
4160 dpi->next_saved_scope = 0;
4161 dpi->num_saved_scopes = 0;
4163 dpi->copy_templates = NULL;
4164 dpi->next_copy_template = 0;
4165 dpi->num_copy_templates = 0;
4167 d_count_templates_scopes (&dpi->num_copy_templates,
4168 &dpi->num_saved_scopes, dc);
4169 dpi->num_copy_templates *= dpi->num_saved_scopes;
4171 dpi->current_template = NULL;
4174 /* Indicate that an error occurred during printing, and test for error. */
4176 static inline void
4177 d_print_error (struct d_print_info *dpi)
4179 dpi->demangle_failure = 1;
4182 static inline int
4183 d_print_saw_error (struct d_print_info *dpi)
4185 return dpi->demangle_failure != 0;
4188 /* Flush buffered characters to the callback. */
4190 static inline void
4191 d_print_flush (struct d_print_info *dpi)
4193 dpi->buf[dpi->len] = '\0';
4194 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4195 dpi->len = 0;
4196 dpi->flush_count++;
4199 /* Append characters and buffers for printing. */
4201 static inline void
4202 d_append_char (struct d_print_info *dpi, char c)
4204 if (dpi->len == sizeof (dpi->buf) - 1)
4205 d_print_flush (dpi);
4207 dpi->buf[dpi->len++] = c;
4208 dpi->last_char = c;
4211 static inline void
4212 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4214 size_t i;
4216 for (i = 0; i < l; i++)
4217 d_append_char (dpi, s[i]);
4220 static inline void
4221 d_append_string (struct d_print_info *dpi, const char *s)
4223 d_append_buffer (dpi, s, strlen (s));
4226 static inline void
4227 d_append_num (struct d_print_info *dpi, int l)
4229 char buf[25];
4230 sprintf (buf,"%d", l);
4231 d_append_string (dpi, buf);
4234 static inline char
4235 d_last_char (struct d_print_info *dpi)
4237 return dpi->last_char;
4240 /* Turn components into a human readable string. OPTIONS is the
4241 options bits passed to the demangler. DC is the tree to print.
4242 CALLBACK is a function to call to flush demangled string segments
4243 as they fill the intermediate buffer, and OPAQUE is a generalized
4244 callback argument. On success, this returns 1. On failure,
4245 it returns 0, indicating a bad parse. It does not use heap
4246 memory to build an output string, so cannot encounter memory
4247 allocation failure. */
4249 CP_STATIC_IF_GLIBCPP_V3
4251 cplus_demangle_print_callback (int options,
4252 const struct demangle_component *dc,
4253 demangle_callbackref callback, void *opaque)
4255 struct d_print_info dpi;
4257 d_print_init (&dpi, callback, opaque, dc);
4260 #ifdef CP_DYNAMIC_ARRAYS
4261 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4262 and flagged as errors by Address Sanitizer. */
4263 __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
4264 ? dpi.num_saved_scopes : 1];
4265 __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
4266 ? dpi.num_copy_templates : 1];
4268 dpi.saved_scopes = scopes;
4269 dpi.copy_templates = temps;
4270 #else
4271 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4272 * sizeof (*dpi.saved_scopes));
4273 dpi.copy_templates = alloca (dpi.num_copy_templates
4274 * sizeof (*dpi.copy_templates));
4275 #endif
4277 d_print_comp (&dpi, options, dc);
4280 d_print_flush (&dpi);
4282 return ! d_print_saw_error (&dpi);
4285 /* Turn components into a human readable string. OPTIONS is the
4286 options bits passed to the demangler. DC is the tree to print.
4287 ESTIMATE is a guess at the length of the result. This returns a
4288 string allocated by malloc, or NULL on error. On success, this
4289 sets *PALC to the size of the allocated buffer. On failure, this
4290 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4291 failure. */
4293 CP_STATIC_IF_GLIBCPP_V3
4294 char *
4295 cplus_demangle_print (int options, const struct demangle_component *dc,
4296 int estimate, size_t *palc)
4298 struct d_growable_string dgs;
4300 d_growable_string_init (&dgs, estimate);
4302 if (! cplus_demangle_print_callback (options, dc,
4303 d_growable_string_callback_adapter,
4304 &dgs))
4306 free (dgs.buf);
4307 *palc = 0;
4308 return NULL;
4311 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4312 return dgs.buf;
4315 /* Returns the I'th element of the template arglist ARGS, or NULL on
4316 failure. If I is negative, return the entire arglist. */
4318 static struct demangle_component *
4319 d_index_template_argument (struct demangle_component *args, int i)
4321 struct demangle_component *a;
4323 if (i < 0)
4324 /* Print the whole argument pack. */
4325 return args;
4327 for (a = args;
4328 a != NULL;
4329 a = d_right (a))
4331 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4332 return NULL;
4333 if (i <= 0)
4334 break;
4335 --i;
4337 if (i != 0 || a == NULL)
4338 return NULL;
4340 return d_left (a);
4343 /* Returns the template argument from the current context indicated by DC,
4344 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4346 static struct demangle_component *
4347 d_lookup_template_argument (struct d_print_info *dpi,
4348 const struct demangle_component *dc)
4350 if (dpi->templates == NULL)
4352 d_print_error (dpi);
4353 return NULL;
4356 return d_index_template_argument
4357 (d_right (dpi->templates->template_decl),
4358 dc->u.s_number.number);
4361 /* Returns a template argument pack used in DC (any will do), or NULL. */
4363 static struct demangle_component *
4364 d_find_pack (struct d_print_info *dpi,
4365 const struct demangle_component *dc)
4367 struct demangle_component *a;
4368 if (dc == NULL)
4369 return NULL;
4371 switch (dc->type)
4373 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4374 a = d_lookup_template_argument (dpi, dc);
4375 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4376 return a;
4377 return NULL;
4379 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4380 return NULL;
4382 case DEMANGLE_COMPONENT_LAMBDA:
4383 case DEMANGLE_COMPONENT_NAME:
4384 case DEMANGLE_COMPONENT_TAGGED_NAME:
4385 case DEMANGLE_COMPONENT_OPERATOR:
4386 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4387 case DEMANGLE_COMPONENT_SUB_STD:
4388 case DEMANGLE_COMPONENT_CHARACTER:
4389 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4390 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4391 case DEMANGLE_COMPONENT_FIXED_TYPE:
4392 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4393 case DEMANGLE_COMPONENT_NUMBER:
4394 return NULL;
4396 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4397 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4398 case DEMANGLE_COMPONENT_CTOR:
4399 return d_find_pack (dpi, dc->u.s_ctor.name);
4400 case DEMANGLE_COMPONENT_DTOR:
4401 return d_find_pack (dpi, dc->u.s_dtor.name);
4403 default:
4404 a = d_find_pack (dpi, d_left (dc));
4405 if (a)
4406 return a;
4407 return d_find_pack (dpi, d_right (dc));
4411 /* Returns the length of the template argument pack DC. */
4413 static int
4414 d_pack_length (const struct demangle_component *dc)
4416 int count = 0;
4417 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4418 && d_left (dc) != NULL)
4420 ++count;
4421 dc = d_right (dc);
4423 return count;
4426 /* Returns the number of template args in DC, expanding any pack expansions
4427 found there. */
4429 static int
4430 d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
4432 int count = 0;
4433 for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4434 dc = d_right (dc))
4436 struct demangle_component *elt = d_left (dc);
4437 if (elt == NULL)
4438 break;
4439 if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
4441 struct demangle_component *a = d_find_pack (dpi, d_left (elt));
4442 count += d_pack_length (a);
4444 else
4445 ++count;
4447 return count;
4450 /* DC is a component of a mangled expression. Print it, wrapped in parens
4451 if needed. */
4453 static void
4454 d_print_subexpr (struct d_print_info *dpi, int options,
4455 const struct demangle_component *dc)
4457 int simple = 0;
4458 if (dc->type == DEMANGLE_COMPONENT_NAME
4459 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4460 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4461 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4462 simple = 1;
4463 if (!simple)
4464 d_append_char (dpi, '(');
4465 d_print_comp (dpi, options, dc);
4466 if (!simple)
4467 d_append_char (dpi, ')');
4470 /* Save the current scope. */
4472 static void
4473 d_save_scope (struct d_print_info *dpi,
4474 const struct demangle_component *container)
4476 struct d_saved_scope *scope;
4477 struct d_print_template *src, **link;
4479 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4481 d_print_error (dpi);
4482 return;
4484 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4485 dpi->next_saved_scope++;
4487 scope->container = container;
4488 link = &scope->templates;
4490 for (src = dpi->templates; src != NULL; src = src->next)
4492 struct d_print_template *dst;
4494 if (dpi->next_copy_template >= dpi->num_copy_templates)
4496 d_print_error (dpi);
4497 return;
4499 dst = &dpi->copy_templates[dpi->next_copy_template];
4500 dpi->next_copy_template++;
4502 dst->template_decl = src->template_decl;
4503 *link = dst;
4504 link = &dst->next;
4507 *link = NULL;
4510 /* Attempt to locate a previously saved scope. Returns NULL if no
4511 corresponding saved scope was found. */
4513 static struct d_saved_scope *
4514 d_get_saved_scope (struct d_print_info *dpi,
4515 const struct demangle_component *container)
4517 int i;
4519 for (i = 0; i < dpi->next_saved_scope; i++)
4520 if (dpi->saved_scopes[i].container == container)
4521 return &dpi->saved_scopes[i];
4523 return NULL;
4526 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4527 return false. */
4529 static int
4530 d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
4531 const struct demangle_component *dc)
4533 const struct demangle_component *ops, *operator_, *op1, *op2;
4534 int save_idx;
4536 const char *fold_code = d_left (dc)->u.s_operator.op->code;
4537 if (fold_code[0] != 'f')
4538 return 0;
4540 ops = d_right (dc);
4541 operator_ = d_left (ops);
4542 op1 = d_right (ops);
4543 op2 = 0;
4544 if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
4546 op2 = d_right (op1);
4547 op1 = d_left (op1);
4550 /* Print the whole pack. */
4551 save_idx = dpi->pack_index;
4552 dpi->pack_index = -1;
4554 switch (fold_code[1])
4556 /* Unary left fold, (... + X). */
4557 case 'l':
4558 d_append_string (dpi, "(...");
4559 d_print_expr_op (dpi, options, operator_);
4560 d_print_subexpr (dpi, options, op1);
4561 d_append_char (dpi, ')');
4562 break;
4564 /* Unary right fold, (X + ...). */
4565 case 'r':
4566 d_append_char (dpi, '(');
4567 d_print_subexpr (dpi, options, op1);
4568 d_print_expr_op (dpi, options, operator_);
4569 d_append_string (dpi, "...)");
4570 break;
4572 /* Binary left fold, (42 + ... + X). */
4573 case 'L':
4574 /* Binary right fold, (X + ... + 42). */
4575 case 'R':
4576 d_append_char (dpi, '(');
4577 d_print_subexpr (dpi, options, op1);
4578 d_print_expr_op (dpi, options, operator_);
4579 d_append_string (dpi, "...");
4580 d_print_expr_op (dpi, options, operator_);
4581 d_print_subexpr (dpi, options, op2);
4582 d_append_char (dpi, ')');
4583 break;
4586 dpi->pack_index = save_idx;
4587 return 1;
4590 /* Subroutine to handle components. */
4592 static void
4593 d_print_comp_inner (struct d_print_info *dpi, int options,
4594 const struct demangle_component *dc)
4596 /* Magic variable to let reference smashing skip over the next modifier
4597 without needing to modify *dc. */
4598 const struct demangle_component *mod_inner = NULL;
4600 /* Variable used to store the current templates while a previously
4601 captured scope is used. */
4602 struct d_print_template *saved_templates;
4604 /* Nonzero if templates have been stored in the above variable. */
4605 int need_template_restore = 0;
4607 if (dc == NULL)
4609 d_print_error (dpi);
4610 return;
4612 if (d_print_saw_error (dpi))
4613 return;
4615 switch (dc->type)
4617 case DEMANGLE_COMPONENT_NAME:
4618 if ((options & DMGL_JAVA) == 0)
4619 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4620 else
4621 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4622 return;
4624 case DEMANGLE_COMPONENT_TAGGED_NAME:
4625 d_print_comp (dpi, options, d_left (dc));
4626 d_append_string (dpi, "[abi:");
4627 d_print_comp (dpi, options, d_right (dc));
4628 d_append_char (dpi, ']');
4629 return;
4631 case DEMANGLE_COMPONENT_QUAL_NAME:
4632 case DEMANGLE_COMPONENT_LOCAL_NAME:
4633 d_print_comp (dpi, options, d_left (dc));
4634 if ((options & DMGL_JAVA) == 0)
4635 d_append_string (dpi, "::");
4636 else
4637 d_append_char (dpi, '.');
4639 struct demangle_component *local_name = d_right (dc);
4640 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4642 d_append_string (dpi, "{default arg#");
4643 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4644 d_append_string (dpi, "}::");
4645 local_name = local_name->u.s_unary_num.sub;
4647 d_print_comp (dpi, options, local_name);
4649 return;
4651 case DEMANGLE_COMPONENT_TYPED_NAME:
4653 struct d_print_mod *hold_modifiers;
4654 struct demangle_component *typed_name;
4655 struct d_print_mod adpm[4];
4656 unsigned int i;
4657 struct d_print_template dpt;
4659 /* Pass the name down to the type so that it can be printed in
4660 the right place for the type. We also have to pass down
4661 any CV-qualifiers, which apply to the this parameter. */
4662 hold_modifiers = dpi->modifiers;
4663 dpi->modifiers = 0;
4664 i = 0;
4665 typed_name = d_left (dc);
4666 while (typed_name != NULL)
4668 if (i >= sizeof adpm / sizeof adpm[0])
4670 d_print_error (dpi);
4671 return;
4674 adpm[i].next = dpi->modifiers;
4675 dpi->modifiers = &adpm[i];
4676 adpm[i].mod = typed_name;
4677 adpm[i].printed = 0;
4678 adpm[i].templates = dpi->templates;
4679 ++i;
4681 if (!is_fnqual_component_type (typed_name->type))
4682 break;
4684 typed_name = d_left (typed_name);
4687 if (typed_name == NULL)
4689 d_print_error (dpi);
4690 return;
4693 /* If typed_name is a template, then it applies to the
4694 function type as well. */
4695 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4697 dpt.next = dpi->templates;
4698 dpi->templates = &dpt;
4699 dpt.template_decl = typed_name;
4702 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4703 there may be CV-qualifiers on its right argument which
4704 really apply here; this happens when parsing a class which
4705 is local to a function. */
4706 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4708 struct demangle_component *local_name;
4710 local_name = d_right (typed_name);
4711 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4712 local_name = local_name->u.s_unary_num.sub;
4713 if (local_name == NULL)
4715 d_print_error (dpi);
4716 return;
4718 while (is_fnqual_component_type (local_name->type))
4720 if (i >= sizeof adpm / sizeof adpm[0])
4722 d_print_error (dpi);
4723 return;
4726 adpm[i] = adpm[i - 1];
4727 adpm[i].next = &adpm[i - 1];
4728 dpi->modifiers = &adpm[i];
4730 adpm[i - 1].mod = local_name;
4731 adpm[i - 1].printed = 0;
4732 adpm[i - 1].templates = dpi->templates;
4733 ++i;
4735 local_name = d_left (local_name);
4739 d_print_comp (dpi, options, d_right (dc));
4741 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4742 dpi->templates = dpt.next;
4744 /* If the modifiers didn't get printed by the type, print them
4745 now. */
4746 while (i > 0)
4748 --i;
4749 if (! adpm[i].printed)
4751 d_append_char (dpi, ' ');
4752 d_print_mod (dpi, options, adpm[i].mod);
4756 dpi->modifiers = hold_modifiers;
4758 return;
4761 case DEMANGLE_COMPONENT_TEMPLATE:
4763 struct d_print_mod *hold_dpm;
4764 struct demangle_component *dcl;
4765 const struct demangle_component *hold_current;
4767 /* This template may need to be referenced by a cast operator
4768 contained in its subtree. */
4769 hold_current = dpi->current_template;
4770 dpi->current_template = dc;
4772 /* Don't push modifiers into a template definition. Doing so
4773 could give the wrong definition for a template argument.
4774 Instead, treat the template essentially as a name. */
4776 hold_dpm = dpi->modifiers;
4777 dpi->modifiers = NULL;
4779 dcl = d_left (dc);
4781 if ((options & DMGL_JAVA) != 0
4782 && dcl->type == DEMANGLE_COMPONENT_NAME
4783 && dcl->u.s_name.len == 6
4784 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4786 /* Special-case Java arrays, so that JArray<TYPE> appears
4787 instead as TYPE[]. */
4789 d_print_comp (dpi, options, d_right (dc));
4790 d_append_string (dpi, "[]");
4792 else
4794 d_print_comp (dpi, options, dcl);
4795 if (d_last_char (dpi) == '<')
4796 d_append_char (dpi, ' ');
4797 d_append_char (dpi, '<');
4798 d_print_comp (dpi, options, d_right (dc));
4799 /* Avoid generating two consecutive '>' characters, to avoid
4800 the C++ syntactic ambiguity. */
4801 if (d_last_char (dpi) == '>')
4802 d_append_char (dpi, ' ');
4803 d_append_char (dpi, '>');
4806 dpi->modifiers = hold_dpm;
4807 dpi->current_template = hold_current;
4809 return;
4812 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4813 if (dpi->is_lambda_arg)
4815 /* Show the template parm index, as that's how g++ displays
4816 these, and future proofs us against potential
4817 '[]<typename T> (T *a, T *b) {...}'. */
4818 d_append_buffer (dpi, "auto:", 5);
4819 d_append_num (dpi, dc->u.s_number.number + 1);
4821 else
4823 struct d_print_template *hold_dpt;
4824 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4826 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4827 a = d_index_template_argument (a, dpi->pack_index);
4829 if (a == NULL)
4831 d_print_error (dpi);
4832 return;
4835 /* While processing this parameter, we need to pop the list
4836 of templates. This is because the template parameter may
4837 itself be a reference to a parameter of an outer
4838 template. */
4840 hold_dpt = dpi->templates;
4841 dpi->templates = hold_dpt->next;
4843 d_print_comp (dpi, options, a);
4845 dpi->templates = hold_dpt;
4847 return;
4849 case DEMANGLE_COMPONENT_CTOR:
4850 d_print_comp (dpi, options, dc->u.s_ctor.name);
4851 return;
4853 case DEMANGLE_COMPONENT_DTOR:
4854 d_append_char (dpi, '~');
4855 d_print_comp (dpi, options, dc->u.s_dtor.name);
4856 return;
4858 case DEMANGLE_COMPONENT_VTABLE:
4859 d_append_string (dpi, "vtable for ");
4860 d_print_comp (dpi, options, d_left (dc));
4861 return;
4863 case DEMANGLE_COMPONENT_VTT:
4864 d_append_string (dpi, "VTT for ");
4865 d_print_comp (dpi, options, d_left (dc));
4866 return;
4868 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4869 d_append_string (dpi, "construction vtable for ");
4870 d_print_comp (dpi, options, d_left (dc));
4871 d_append_string (dpi, "-in-");
4872 d_print_comp (dpi, options, d_right (dc));
4873 return;
4875 case DEMANGLE_COMPONENT_TYPEINFO:
4876 d_append_string (dpi, "typeinfo for ");
4877 d_print_comp (dpi, options, d_left (dc));
4878 return;
4880 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4881 d_append_string (dpi, "typeinfo name for ");
4882 d_print_comp (dpi, options, d_left (dc));
4883 return;
4885 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4886 d_append_string (dpi, "typeinfo fn for ");
4887 d_print_comp (dpi, options, d_left (dc));
4888 return;
4890 case DEMANGLE_COMPONENT_THUNK:
4891 d_append_string (dpi, "non-virtual thunk to ");
4892 d_print_comp (dpi, options, d_left (dc));
4893 return;
4895 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4896 d_append_string (dpi, "virtual thunk to ");
4897 d_print_comp (dpi, options, d_left (dc));
4898 return;
4900 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4901 d_append_string (dpi, "covariant return thunk to ");
4902 d_print_comp (dpi, options, d_left (dc));
4903 return;
4905 case DEMANGLE_COMPONENT_JAVA_CLASS:
4906 d_append_string (dpi, "java Class for ");
4907 d_print_comp (dpi, options, d_left (dc));
4908 return;
4910 case DEMANGLE_COMPONENT_GUARD:
4911 d_append_string (dpi, "guard variable for ");
4912 d_print_comp (dpi, options, d_left (dc));
4913 return;
4915 case DEMANGLE_COMPONENT_TLS_INIT:
4916 d_append_string (dpi, "TLS init function for ");
4917 d_print_comp (dpi, options, d_left (dc));
4918 return;
4920 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4921 d_append_string (dpi, "TLS wrapper function for ");
4922 d_print_comp (dpi, options, d_left (dc));
4923 return;
4925 case DEMANGLE_COMPONENT_REFTEMP:
4926 d_append_string (dpi, "reference temporary #");
4927 d_print_comp (dpi, options, d_right (dc));
4928 d_append_string (dpi, " for ");
4929 d_print_comp (dpi, options, d_left (dc));
4930 return;
4932 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4933 d_append_string (dpi, "hidden alias for ");
4934 d_print_comp (dpi, options, d_left (dc));
4935 return;
4937 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4938 d_append_string (dpi, "transaction clone for ");
4939 d_print_comp (dpi, options, d_left (dc));
4940 return;
4942 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4943 d_append_string (dpi, "non-transaction clone for ");
4944 d_print_comp (dpi, options, d_left (dc));
4945 return;
4947 case DEMANGLE_COMPONENT_SUB_STD:
4948 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4949 return;
4951 case DEMANGLE_COMPONENT_RESTRICT:
4952 case DEMANGLE_COMPONENT_VOLATILE:
4953 case DEMANGLE_COMPONENT_CONST:
4955 struct d_print_mod *pdpm;
4957 /* When printing arrays, it's possible to have cases where the
4958 same CV-qualifier gets pushed on the stack multiple times.
4959 We only need to print it once. */
4961 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4963 if (! pdpm->printed)
4965 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4966 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4967 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4968 break;
4969 if (pdpm->mod->type == dc->type)
4971 d_print_comp (dpi, options, d_left (dc));
4972 return;
4977 goto modifier;
4979 case DEMANGLE_COMPONENT_REFERENCE:
4980 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4982 /* Handle reference smashing: & + && = &. */
4983 const struct demangle_component *sub = d_left (dc);
4984 if (!dpi->is_lambda_arg
4985 && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4987 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4988 struct demangle_component *a;
4990 if (scope == NULL)
4992 /* This is the first time SUB has been traversed.
4993 We need to capture the current templates so
4994 they can be restored if SUB is reentered as a
4995 substitution. */
4996 d_save_scope (dpi, sub);
4997 if (d_print_saw_error (dpi))
4998 return;
5000 else
5002 const struct d_component_stack *dcse;
5003 int found_self_or_parent = 0;
5005 /* This traversal is reentering SUB as a substition.
5006 If we are not beneath SUB or DC in the tree then we
5007 need to restore SUB's template stack temporarily. */
5008 for (dcse = dpi->component_stack; dcse != NULL;
5009 dcse = dcse->parent)
5011 if (dcse->dc == sub
5012 || (dcse->dc == dc
5013 && dcse != dpi->component_stack))
5015 found_self_or_parent = 1;
5016 break;
5020 if (!found_self_or_parent)
5022 saved_templates = dpi->templates;
5023 dpi->templates = scope->templates;
5024 need_template_restore = 1;
5028 a = d_lookup_template_argument (dpi, sub);
5029 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5030 a = d_index_template_argument (a, dpi->pack_index);
5032 if (a == NULL)
5034 if (need_template_restore)
5035 dpi->templates = saved_templates;
5037 d_print_error (dpi);
5038 return;
5041 sub = a;
5044 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
5045 || sub->type == dc->type)
5046 dc = sub;
5047 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
5048 mod_inner = d_left (sub);
5050 /* Fall through. */
5052 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5053 case DEMANGLE_COMPONENT_POINTER:
5054 case DEMANGLE_COMPONENT_COMPLEX:
5055 case DEMANGLE_COMPONENT_IMAGINARY:
5056 FNQUAL_COMPONENT_CASE:
5057 modifier:
5059 /* We keep a list of modifiers on the stack. */
5060 struct d_print_mod dpm;
5062 dpm.next = dpi->modifiers;
5063 dpi->modifiers = &dpm;
5064 dpm.mod = dc;
5065 dpm.printed = 0;
5066 dpm.templates = dpi->templates;
5068 if (!mod_inner)
5069 mod_inner = d_left (dc);
5071 d_print_comp (dpi, options, mod_inner);
5073 /* If the modifier didn't get printed by the type, print it
5074 now. */
5075 if (! dpm.printed)
5076 d_print_mod (dpi, options, dc);
5078 dpi->modifiers = dpm.next;
5080 if (need_template_restore)
5081 dpi->templates = saved_templates;
5083 return;
5086 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5087 if ((options & DMGL_JAVA) == 0)
5088 d_append_buffer (dpi, dc->u.s_builtin.type->name,
5089 dc->u.s_builtin.type->len);
5090 else
5091 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
5092 dc->u.s_builtin.type->java_len);
5093 return;
5095 case DEMANGLE_COMPONENT_VENDOR_TYPE:
5096 d_print_comp (dpi, options, d_left (dc));
5097 return;
5099 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5101 if ((options & DMGL_RET_POSTFIX) != 0)
5102 d_print_function_type (dpi,
5103 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5104 dc, dpi->modifiers);
5106 /* Print return type if present */
5107 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
5108 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5109 d_left (dc));
5110 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
5112 struct d_print_mod dpm;
5114 /* We must pass this type down as a modifier in order to
5115 print it in the right location. */
5116 dpm.next = dpi->modifiers;
5117 dpi->modifiers = &dpm;
5118 dpm.mod = dc;
5119 dpm.printed = 0;
5120 dpm.templates = dpi->templates;
5122 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5123 d_left (dc));
5125 dpi->modifiers = dpm.next;
5127 if (dpm.printed)
5128 return;
5130 /* In standard prefix notation, there is a space between the
5131 return type and the function signature. */
5132 if ((options & DMGL_RET_POSTFIX) == 0)
5133 d_append_char (dpi, ' ');
5136 if ((options & DMGL_RET_POSTFIX) == 0)
5137 d_print_function_type (dpi,
5138 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5139 dc, dpi->modifiers);
5141 return;
5144 case DEMANGLE_COMPONENT_ARRAY_TYPE:
5146 struct d_print_mod *hold_modifiers;
5147 struct d_print_mod adpm[4];
5148 unsigned int i;
5149 struct d_print_mod *pdpm;
5151 /* We must pass this type down as a modifier in order to print
5152 multi-dimensional arrays correctly. If the array itself is
5153 CV-qualified, we act as though the element type were
5154 CV-qualified. We do this by copying the modifiers down
5155 rather than fiddling pointers, so that we don't wind up
5156 with a d_print_mod higher on the stack pointing into our
5157 stack frame after we return. */
5159 hold_modifiers = dpi->modifiers;
5161 adpm[0].next = hold_modifiers;
5162 dpi->modifiers = &adpm[0];
5163 adpm[0].mod = dc;
5164 adpm[0].printed = 0;
5165 adpm[0].templates = dpi->templates;
5167 i = 1;
5168 pdpm = hold_modifiers;
5169 while (pdpm != NULL
5170 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
5171 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
5172 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
5174 if (! pdpm->printed)
5176 if (i >= sizeof adpm / sizeof adpm[0])
5178 d_print_error (dpi);
5179 return;
5182 adpm[i] = *pdpm;
5183 adpm[i].next = dpi->modifiers;
5184 dpi->modifiers = &adpm[i];
5185 pdpm->printed = 1;
5186 ++i;
5189 pdpm = pdpm->next;
5192 d_print_comp (dpi, options, d_right (dc));
5194 dpi->modifiers = hold_modifiers;
5196 if (adpm[0].printed)
5197 return;
5199 while (i > 1)
5201 --i;
5202 d_print_mod (dpi, options, adpm[i].mod);
5205 d_print_array_type (dpi, options, dc, dpi->modifiers);
5207 return;
5210 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5211 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5213 struct d_print_mod dpm;
5215 dpm.next = dpi->modifiers;
5216 dpi->modifiers = &dpm;
5217 dpm.mod = dc;
5218 dpm.printed = 0;
5219 dpm.templates = dpi->templates;
5221 d_print_comp (dpi, options, d_right (dc));
5223 /* If the modifier didn't get printed by the type, print it
5224 now. */
5225 if (! dpm.printed)
5226 d_print_mod (dpi, options, dc);
5228 dpi->modifiers = dpm.next;
5230 return;
5233 case DEMANGLE_COMPONENT_FIXED_TYPE:
5234 if (dc->u.s_fixed.sat)
5235 d_append_string (dpi, "_Sat ");
5236 /* Don't print "int _Accum". */
5237 if (dc->u.s_fixed.length->u.s_builtin.type
5238 != &cplus_demangle_builtin_types['i'-'a'])
5240 d_print_comp (dpi, options, dc->u.s_fixed.length);
5241 d_append_char (dpi, ' ');
5243 if (dc->u.s_fixed.accum)
5244 d_append_string (dpi, "_Accum");
5245 else
5246 d_append_string (dpi, "_Fract");
5247 return;
5249 case DEMANGLE_COMPONENT_ARGLIST:
5250 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5251 if (d_left (dc) != NULL)
5252 d_print_comp (dpi, options, d_left (dc));
5253 if (d_right (dc) != NULL)
5255 size_t len;
5256 unsigned long int flush_count;
5257 /* Make sure ", " isn't flushed by d_append_string, otherwise
5258 dpi->len -= 2 wouldn't work. */
5259 if (dpi->len >= sizeof (dpi->buf) - 2)
5260 d_print_flush (dpi);
5261 d_append_string (dpi, ", ");
5262 len = dpi->len;
5263 flush_count = dpi->flush_count;
5264 d_print_comp (dpi, options, d_right (dc));
5265 /* If that didn't print anything (which can happen with empty
5266 template argument packs), remove the comma and space. */
5267 if (dpi->flush_count == flush_count && dpi->len == len)
5268 dpi->len -= 2;
5270 return;
5272 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5274 struct demangle_component *type = d_left (dc);
5275 struct demangle_component *list = d_right (dc);
5277 if (type)
5278 d_print_comp (dpi, options, type);
5279 d_append_char (dpi, '{');
5280 d_print_comp (dpi, options, list);
5281 d_append_char (dpi, '}');
5283 return;
5285 case DEMANGLE_COMPONENT_OPERATOR:
5287 const struct demangle_operator_info *op = dc->u.s_operator.op;
5288 int len = op->len;
5290 d_append_string (dpi, "operator");
5291 /* Add a space before new/delete. */
5292 if (IS_LOWER (op->name[0]))
5293 d_append_char (dpi, ' ');
5294 /* Omit a trailing space. */
5295 if (op->name[len-1] == ' ')
5296 --len;
5297 d_append_buffer (dpi, op->name, len);
5298 return;
5301 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5302 d_append_string (dpi, "operator ");
5303 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5304 return;
5306 case DEMANGLE_COMPONENT_CONVERSION:
5307 d_append_string (dpi, "operator ");
5308 d_print_conversion (dpi, options, dc);
5309 return;
5311 case DEMANGLE_COMPONENT_NULLARY:
5312 d_print_expr_op (dpi, options, d_left (dc));
5313 return;
5315 case DEMANGLE_COMPONENT_UNARY:
5317 struct demangle_component *op = d_left (dc);
5318 struct demangle_component *operand = d_right (dc);
5319 const char *code = NULL;
5321 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5323 code = op->u.s_operator.op->code;
5324 if (!strcmp (code, "ad"))
5326 /* Don't print the argument list for the address of a
5327 function. */
5328 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5329 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5330 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5331 operand = d_left (operand);
5333 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5335 /* This indicates a suffix operator. */
5336 operand = d_left (operand);
5337 d_print_subexpr (dpi, options, operand);
5338 d_print_expr_op (dpi, options, op);
5339 return;
5343 /* For sizeof..., just print the pack length. */
5344 if (code && !strcmp (code, "sZ"))
5346 struct demangle_component *a = d_find_pack (dpi, operand);
5347 int len = d_pack_length (a);
5348 d_append_num (dpi, len);
5349 return;
5351 else if (code && !strcmp (code, "sP"))
5353 int len = d_args_length (dpi, operand);
5354 d_append_num (dpi, len);
5355 return;
5358 if (op->type != DEMANGLE_COMPONENT_CAST)
5359 d_print_expr_op (dpi, options, op);
5360 else
5362 d_append_char (dpi, '(');
5363 d_print_cast (dpi, options, op);
5364 d_append_char (dpi, ')');
5366 if (code && !strcmp (code, "gs"))
5367 /* Avoid parens after '::'. */
5368 d_print_comp (dpi, options, operand);
5369 else if (code && !strcmp (code, "st"))
5370 /* Always print parens for sizeof (type). */
5372 d_append_char (dpi, '(');
5373 d_print_comp (dpi, options, operand);
5374 d_append_char (dpi, ')');
5376 else
5377 d_print_subexpr (dpi, options, operand);
5379 return;
5381 case DEMANGLE_COMPONENT_BINARY:
5382 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5384 d_print_error (dpi);
5385 return;
5388 if (op_is_new_cast (d_left (dc)))
5390 d_print_expr_op (dpi, options, d_left (dc));
5391 d_append_char (dpi, '<');
5392 d_print_comp (dpi, options, d_left (d_right (dc)));
5393 d_append_string (dpi, ">(");
5394 d_print_comp (dpi, options, d_right (d_right (dc)));
5395 d_append_char (dpi, ')');
5396 return;
5399 if (d_maybe_print_fold_expression (dpi, options, dc))
5400 return;
5402 /* We wrap an expression which uses the greater-than operator in
5403 an extra layer of parens so that it does not get confused
5404 with the '>' which ends the template parameters. */
5405 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5406 && d_left (dc)->u.s_operator.op->len == 1
5407 && d_left (dc)->u.s_operator.op->name[0] == '>')
5408 d_append_char (dpi, '(');
5410 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5411 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5413 /* Function call used in an expression should not have printed types
5414 of the function arguments. Values of the function arguments still
5415 get printed below. */
5417 const struct demangle_component *func = d_left (d_right (dc));
5419 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5420 d_print_error (dpi);
5421 d_print_subexpr (dpi, options, d_left (func));
5423 else
5424 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5425 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5427 d_append_char (dpi, '[');
5428 d_print_comp (dpi, options, d_right (d_right (dc)));
5429 d_append_char (dpi, ']');
5431 else
5433 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5434 d_print_expr_op (dpi, options, d_left (dc));
5435 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5438 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5439 && d_left (dc)->u.s_operator.op->len == 1
5440 && d_left (dc)->u.s_operator.op->name[0] == '>')
5441 d_append_char (dpi, ')');
5443 return;
5445 case DEMANGLE_COMPONENT_BINARY_ARGS:
5446 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5447 d_print_error (dpi);
5448 return;
5450 case DEMANGLE_COMPONENT_TRINARY:
5451 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5452 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5454 d_print_error (dpi);
5455 return;
5457 if (d_maybe_print_fold_expression (dpi, options, dc))
5458 return;
5460 struct demangle_component *op = d_left (dc);
5461 struct demangle_component *first = d_left (d_right (dc));
5462 struct demangle_component *second = d_left (d_right (d_right (dc)));
5463 struct demangle_component *third = d_right (d_right (d_right (dc)));
5465 if (!strcmp (op->u.s_operator.op->code, "qu"))
5467 d_print_subexpr (dpi, options, first);
5468 d_print_expr_op (dpi, options, op);
5469 d_print_subexpr (dpi, options, second);
5470 d_append_string (dpi, " : ");
5471 d_print_subexpr (dpi, options, third);
5473 else
5475 d_append_string (dpi, "new ");
5476 if (d_left (first) != NULL)
5478 d_print_subexpr (dpi, options, first);
5479 d_append_char (dpi, ' ');
5481 d_print_comp (dpi, options, second);
5482 if (third)
5483 d_print_subexpr (dpi, options, third);
5486 return;
5488 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5489 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5490 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5491 d_print_error (dpi);
5492 return;
5494 case DEMANGLE_COMPONENT_LITERAL:
5495 case DEMANGLE_COMPONENT_LITERAL_NEG:
5497 enum d_builtin_type_print tp;
5499 /* For some builtin types, produce simpler output. */
5500 tp = D_PRINT_DEFAULT;
5501 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5503 tp = d_left (dc)->u.s_builtin.type->print;
5504 switch (tp)
5506 case D_PRINT_INT:
5507 case D_PRINT_UNSIGNED:
5508 case D_PRINT_LONG:
5509 case D_PRINT_UNSIGNED_LONG:
5510 case D_PRINT_LONG_LONG:
5511 case D_PRINT_UNSIGNED_LONG_LONG:
5512 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5514 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5515 d_append_char (dpi, '-');
5516 d_print_comp (dpi, options, d_right (dc));
5517 switch (tp)
5519 default:
5520 break;
5521 case D_PRINT_UNSIGNED:
5522 d_append_char (dpi, 'u');
5523 break;
5524 case D_PRINT_LONG:
5525 d_append_char (dpi, 'l');
5526 break;
5527 case D_PRINT_UNSIGNED_LONG:
5528 d_append_string (dpi, "ul");
5529 break;
5530 case D_PRINT_LONG_LONG:
5531 d_append_string (dpi, "ll");
5532 break;
5533 case D_PRINT_UNSIGNED_LONG_LONG:
5534 d_append_string (dpi, "ull");
5535 break;
5537 return;
5539 break;
5541 case D_PRINT_BOOL:
5542 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5543 && d_right (dc)->u.s_name.len == 1
5544 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5546 switch (d_right (dc)->u.s_name.s[0])
5548 case '0':
5549 d_append_string (dpi, "false");
5550 return;
5551 case '1':
5552 d_append_string (dpi, "true");
5553 return;
5554 default:
5555 break;
5558 break;
5560 default:
5561 break;
5565 d_append_char (dpi, '(');
5566 d_print_comp (dpi, options, d_left (dc));
5567 d_append_char (dpi, ')');
5568 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5569 d_append_char (dpi, '-');
5570 if (tp == D_PRINT_FLOAT)
5571 d_append_char (dpi, '[');
5572 d_print_comp (dpi, options, d_right (dc));
5573 if (tp == D_PRINT_FLOAT)
5574 d_append_char (dpi, ']');
5576 return;
5578 case DEMANGLE_COMPONENT_NUMBER:
5579 d_append_num (dpi, dc->u.s_number.number);
5580 return;
5582 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5583 d_append_string (dpi, "java resource ");
5584 d_print_comp (dpi, options, d_left (dc));
5585 return;
5587 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5588 d_print_comp (dpi, options, d_left (dc));
5589 d_print_comp (dpi, options, d_right (dc));
5590 return;
5592 case DEMANGLE_COMPONENT_CHARACTER:
5593 d_append_char (dpi, dc->u.s_character.character);
5594 return;
5596 case DEMANGLE_COMPONENT_DECLTYPE:
5597 d_append_string (dpi, "decltype (");
5598 d_print_comp (dpi, options, d_left (dc));
5599 d_append_char (dpi, ')');
5600 return;
5602 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5604 int len;
5605 int i;
5606 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5607 if (a == NULL)
5609 /* d_find_pack won't find anything if the only packs involved
5610 in this expansion are function parameter packs; in that
5611 case, just print the pattern and "...". */
5612 d_print_subexpr (dpi, options, d_left (dc));
5613 d_append_string (dpi, "...");
5614 return;
5617 len = d_pack_length (a);
5618 dc = d_left (dc);
5619 for (i = 0; i < len; ++i)
5621 dpi->pack_index = i;
5622 d_print_comp (dpi, options, dc);
5623 if (i < len-1)
5624 d_append_string (dpi, ", ");
5627 return;
5629 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5631 long num = dc->u.s_number.number;
5632 if (num == 0)
5633 d_append_string (dpi, "this");
5634 else
5636 d_append_string (dpi, "{parm#");
5637 d_append_num (dpi, num);
5638 d_append_char (dpi, '}');
5641 return;
5643 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5644 d_append_string (dpi, "global constructors keyed to ");
5645 d_print_comp (dpi, options, dc->u.s_binary.left);
5646 return;
5648 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5649 d_append_string (dpi, "global destructors keyed to ");
5650 d_print_comp (dpi, options, dc->u.s_binary.left);
5651 return;
5653 case DEMANGLE_COMPONENT_LAMBDA:
5654 d_append_string (dpi, "{lambda(");
5655 /* Generic lambda auto parms are mangled as the template type
5656 parm they are. */
5657 dpi->is_lambda_arg++;
5658 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5659 dpi->is_lambda_arg--;
5660 d_append_string (dpi, ")#");
5661 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5662 d_append_char (dpi, '}');
5663 return;
5665 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5666 d_append_string (dpi, "{unnamed type#");
5667 d_append_num (dpi, dc->u.s_number.number + 1);
5668 d_append_char (dpi, '}');
5669 return;
5671 case DEMANGLE_COMPONENT_CLONE:
5672 d_print_comp (dpi, options, d_left (dc));
5673 d_append_string (dpi, " [clone ");
5674 d_print_comp (dpi, options, d_right (dc));
5675 d_append_char (dpi, ']');
5676 return;
5678 default:
5679 d_print_error (dpi);
5680 return;
5684 static void
5685 d_print_comp (struct d_print_info *dpi, int options,
5686 const struct demangle_component *dc)
5688 struct d_component_stack self;
5690 self.dc = dc;
5691 self.parent = dpi->component_stack;
5692 dpi->component_stack = &self;
5694 d_print_comp_inner (dpi, options, dc);
5696 dpi->component_stack = self.parent;
5699 /* Print a Java dentifier. For Java we try to handle encoded extended
5700 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5701 so we don't it for C++. Characters are encoded as
5702 __U<hex-char>+_. */
5704 static void
5705 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5707 const char *p;
5708 const char *end;
5710 end = name + len;
5711 for (p = name; p < end; ++p)
5713 if (end - p > 3
5714 && p[0] == '_'
5715 && p[1] == '_'
5716 && p[2] == 'U')
5718 unsigned long c;
5719 const char *q;
5721 c = 0;
5722 for (q = p + 3; q < end; ++q)
5724 int dig;
5726 if (IS_DIGIT (*q))
5727 dig = *q - '0';
5728 else if (*q >= 'A' && *q <= 'F')
5729 dig = *q - 'A' + 10;
5730 else if (*q >= 'a' && *q <= 'f')
5731 dig = *q - 'a' + 10;
5732 else
5733 break;
5735 c = c * 16 + dig;
5737 /* If the Unicode character is larger than 256, we don't try
5738 to deal with it here. FIXME. */
5739 if (q < end && *q == '_' && c < 256)
5741 d_append_char (dpi, c);
5742 p = q;
5743 continue;
5747 d_append_char (dpi, *p);
5751 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5752 qualifiers on this after printing a function. */
5754 static void
5755 d_print_mod_list (struct d_print_info *dpi, int options,
5756 struct d_print_mod *mods, int suffix)
5758 struct d_print_template *hold_dpt;
5760 if (mods == NULL || d_print_saw_error (dpi))
5761 return;
5763 if (mods->printed
5764 || (! suffix
5765 && (is_fnqual_component_type (mods->mod->type))))
5767 d_print_mod_list (dpi, options, mods->next, suffix);
5768 return;
5771 mods->printed = 1;
5773 hold_dpt = dpi->templates;
5774 dpi->templates = mods->templates;
5776 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5778 d_print_function_type (dpi, options, mods->mod, mods->next);
5779 dpi->templates = hold_dpt;
5780 return;
5782 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5784 d_print_array_type (dpi, options, mods->mod, mods->next);
5785 dpi->templates = hold_dpt;
5786 return;
5788 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5790 struct d_print_mod *hold_modifiers;
5791 struct demangle_component *dc;
5793 /* When this is on the modifier stack, we have pulled any
5794 qualifiers off the right argument already. Otherwise, we
5795 print it as usual, but don't let the left argument see any
5796 modifiers. */
5798 hold_modifiers = dpi->modifiers;
5799 dpi->modifiers = NULL;
5800 d_print_comp (dpi, options, d_left (mods->mod));
5801 dpi->modifiers = hold_modifiers;
5803 if ((options & DMGL_JAVA) == 0)
5804 d_append_string (dpi, "::");
5805 else
5806 d_append_char (dpi, '.');
5808 dc = d_right (mods->mod);
5810 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5812 d_append_string (dpi, "{default arg#");
5813 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5814 d_append_string (dpi, "}::");
5815 dc = dc->u.s_unary_num.sub;
5818 while (is_fnqual_component_type (dc->type))
5819 dc = d_left (dc);
5821 d_print_comp (dpi, options, dc);
5823 dpi->templates = hold_dpt;
5824 return;
5827 d_print_mod (dpi, options, mods->mod);
5829 dpi->templates = hold_dpt;
5831 d_print_mod_list (dpi, options, mods->next, suffix);
5834 /* Print a modifier. */
5836 static void
5837 d_print_mod (struct d_print_info *dpi, int options,
5838 const struct demangle_component *mod)
5840 switch (mod->type)
5842 case DEMANGLE_COMPONENT_RESTRICT:
5843 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5844 d_append_string (dpi, " restrict");
5845 return;
5846 case DEMANGLE_COMPONENT_VOLATILE:
5847 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5848 d_append_string (dpi, " volatile");
5849 return;
5850 case DEMANGLE_COMPONENT_CONST:
5851 case DEMANGLE_COMPONENT_CONST_THIS:
5852 d_append_string (dpi, " const");
5853 return;
5854 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
5855 d_append_string (dpi, " transaction_safe");
5856 return;
5857 case DEMANGLE_COMPONENT_NOEXCEPT:
5858 d_append_string (dpi, " noexcept");
5859 if (d_right (mod))
5861 d_append_char (dpi, '(');
5862 d_print_comp (dpi, options, d_right (mod));
5863 d_append_char (dpi, ')');
5865 return;
5866 case DEMANGLE_COMPONENT_THROW_SPEC:
5867 d_append_string (dpi, " throw");
5868 if (d_right (mod))
5870 d_append_char (dpi, '(');
5871 d_print_comp (dpi, options, d_right (mod));
5872 d_append_char (dpi, ')');
5874 return;
5875 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5876 d_append_char (dpi, ' ');
5877 d_print_comp (dpi, options, d_right (mod));
5878 return;
5879 case DEMANGLE_COMPONENT_POINTER:
5880 /* There is no pointer symbol in Java. */
5881 if ((options & DMGL_JAVA) == 0)
5882 d_append_char (dpi, '*');
5883 return;
5884 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5885 /* For the ref-qualifier, put a space before the &. */
5886 d_append_char (dpi, ' ');
5887 /* FALLTHRU */
5888 case DEMANGLE_COMPONENT_REFERENCE:
5889 d_append_char (dpi, '&');
5890 return;
5891 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5892 d_append_char (dpi, ' ');
5893 /* FALLTHRU */
5894 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5895 d_append_string (dpi, "&&");
5896 return;
5897 case DEMANGLE_COMPONENT_COMPLEX:
5898 d_append_string (dpi, "complex ");
5899 return;
5900 case DEMANGLE_COMPONENT_IMAGINARY:
5901 d_append_string (dpi, "imaginary ");
5902 return;
5903 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5904 if (d_last_char (dpi) != '(')
5905 d_append_char (dpi, ' ');
5906 d_print_comp (dpi, options, d_left (mod));
5907 d_append_string (dpi, "::*");
5908 return;
5909 case DEMANGLE_COMPONENT_TYPED_NAME:
5910 d_print_comp (dpi, options, d_left (mod));
5911 return;
5912 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5913 d_append_string (dpi, " __vector(");
5914 d_print_comp (dpi, options, d_left (mod));
5915 d_append_char (dpi, ')');
5916 return;
5918 default:
5919 /* Otherwise, we have something that won't go back on the
5920 modifier stack, so we can just print it. */
5921 d_print_comp (dpi, options, mod);
5922 return;
5926 /* Print a function type, except for the return type. */
5928 static void
5929 d_print_function_type (struct d_print_info *dpi, int options,
5930 const struct demangle_component *dc,
5931 struct d_print_mod *mods)
5933 int need_paren;
5934 int need_space;
5935 struct d_print_mod *p;
5936 struct d_print_mod *hold_modifiers;
5938 need_paren = 0;
5939 need_space = 0;
5940 for (p = mods; p != NULL; p = p->next)
5942 if (p->printed)
5943 break;
5945 switch (p->mod->type)
5947 case DEMANGLE_COMPONENT_POINTER:
5948 case DEMANGLE_COMPONENT_REFERENCE:
5949 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5950 need_paren = 1;
5951 break;
5952 case DEMANGLE_COMPONENT_RESTRICT:
5953 case DEMANGLE_COMPONENT_VOLATILE:
5954 case DEMANGLE_COMPONENT_CONST:
5955 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5956 case DEMANGLE_COMPONENT_COMPLEX:
5957 case DEMANGLE_COMPONENT_IMAGINARY:
5958 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5959 need_space = 1;
5960 need_paren = 1;
5961 break;
5962 FNQUAL_COMPONENT_CASE:
5963 break;
5964 default:
5965 break;
5967 if (need_paren)
5968 break;
5971 if (need_paren)
5973 if (! need_space)
5975 if (d_last_char (dpi) != '('
5976 && d_last_char (dpi) != '*')
5977 need_space = 1;
5979 if (need_space && d_last_char (dpi) != ' ')
5980 d_append_char (dpi, ' ');
5981 d_append_char (dpi, '(');
5984 hold_modifiers = dpi->modifiers;
5985 dpi->modifiers = NULL;
5987 d_print_mod_list (dpi, options, mods, 0);
5989 if (need_paren)
5990 d_append_char (dpi, ')');
5992 d_append_char (dpi, '(');
5994 if (d_right (dc) != NULL)
5995 d_print_comp (dpi, options, d_right (dc));
5997 d_append_char (dpi, ')');
5999 d_print_mod_list (dpi, options, mods, 1);
6001 dpi->modifiers = hold_modifiers;
6004 /* Print an array type, except for the element type. */
6006 static void
6007 d_print_array_type (struct d_print_info *dpi, int options,
6008 const struct demangle_component *dc,
6009 struct d_print_mod *mods)
6011 int need_space;
6013 need_space = 1;
6014 if (mods != NULL)
6016 int need_paren;
6017 struct d_print_mod *p;
6019 need_paren = 0;
6020 for (p = mods; p != NULL; p = p->next)
6022 if (! p->printed)
6024 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6026 need_space = 0;
6027 break;
6029 else
6031 need_paren = 1;
6032 need_space = 1;
6033 break;
6038 if (need_paren)
6039 d_append_string (dpi, " (");
6041 d_print_mod_list (dpi, options, mods, 0);
6043 if (need_paren)
6044 d_append_char (dpi, ')');
6047 if (need_space)
6048 d_append_char (dpi, ' ');
6050 d_append_char (dpi, '[');
6052 if (d_left (dc) != NULL)
6053 d_print_comp (dpi, options, d_left (dc));
6055 d_append_char (dpi, ']');
6058 /* Print an operator in an expression. */
6060 static void
6061 d_print_expr_op (struct d_print_info *dpi, int options,
6062 const struct demangle_component *dc)
6064 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
6065 d_append_buffer (dpi, dc->u.s_operator.op->name,
6066 dc->u.s_operator.op->len);
6067 else
6068 d_print_comp (dpi, options, dc);
6071 /* Print a cast. */
6073 static void
6074 d_print_cast (struct d_print_info *dpi, int options,
6075 const struct demangle_component *dc)
6077 d_print_comp (dpi, options, d_left (dc));
6080 /* Print a conversion operator. */
6082 static void
6083 d_print_conversion (struct d_print_info *dpi, int options,
6084 const struct demangle_component *dc)
6086 struct d_print_template dpt;
6088 /* For a conversion operator, we need the template parameters from
6089 the enclosing template in scope for processing the type. */
6090 if (dpi->current_template != NULL)
6092 dpt.next = dpi->templates;
6093 dpi->templates = &dpt;
6094 dpt.template_decl = dpi->current_template;
6097 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
6099 d_print_comp (dpi, options, d_left (dc));
6100 if (dpi->current_template != NULL)
6101 dpi->templates = dpt.next;
6103 else
6105 d_print_comp (dpi, options, d_left (d_left (dc)));
6107 /* For a templated cast operator, we need to remove the template
6108 parameters from scope after printing the operator name,
6109 so we need to handle the template printing here. */
6110 if (dpi->current_template != NULL)
6111 dpi->templates = dpt.next;
6113 if (d_last_char (dpi) == '<')
6114 d_append_char (dpi, ' ');
6115 d_append_char (dpi, '<');
6116 d_print_comp (dpi, options, d_right (d_left (dc)));
6117 /* Avoid generating two consecutive '>' characters, to avoid
6118 the C++ syntactic ambiguity. */
6119 if (d_last_char (dpi) == '>')
6120 d_append_char (dpi, ' ');
6121 d_append_char (dpi, '>');
6125 /* Initialize the information structure we use to pass around
6126 information. */
6128 CP_STATIC_IF_GLIBCPP_V3
6129 void
6130 cplus_demangle_init_info (const char *mangled, int options, size_t len,
6131 struct d_info *di)
6133 di->s = mangled;
6134 di->send = mangled + len;
6135 di->options = options;
6137 di->n = mangled;
6139 /* We can not need more components than twice the number of chars in
6140 the mangled string. Most components correspond directly to
6141 chars, but the ARGLIST types are exceptions. */
6142 di->num_comps = 2 * len;
6143 di->next_comp = 0;
6145 /* Similarly, we can not need more substitutions than there are
6146 chars in the mangled string. */
6147 di->num_subs = len;
6148 di->next_sub = 0;
6149 di->did_subs = 0;
6151 di->last_name = NULL;
6153 di->expansion = 0;
6154 di->is_expression = 0;
6155 di->is_conversion = 0;
6158 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6159 mangled name, return strings in repeated callback giving the demangled
6160 name. OPTIONS is the usual libiberty demangler options. On success,
6161 this returns 1. On failure, returns 0. */
6163 static int
6164 d_demangle_callback (const char *mangled, int options,
6165 demangle_callbackref callback, void *opaque)
6167 enum
6169 DCT_TYPE,
6170 DCT_MANGLED,
6171 DCT_GLOBAL_CTORS,
6172 DCT_GLOBAL_DTORS
6174 type;
6175 struct d_info di;
6176 struct demangle_component *dc;
6177 int status;
6179 if (mangled[0] == '_' && mangled[1] == 'Z')
6180 type = DCT_MANGLED;
6181 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6182 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6183 && (mangled[9] == 'D' || mangled[9] == 'I')
6184 && mangled[10] == '_')
6185 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
6186 else
6188 if ((options & DMGL_TYPES) == 0)
6189 return 0;
6190 type = DCT_TYPE;
6193 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6196 #ifdef CP_DYNAMIC_ARRAYS
6197 __extension__ struct demangle_component comps[di.num_comps];
6198 __extension__ struct demangle_component *subs[di.num_subs];
6200 di.comps = comps;
6201 di.subs = subs;
6202 #else
6203 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6204 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6205 #endif
6207 switch (type)
6209 case DCT_TYPE:
6210 dc = cplus_demangle_type (&di);
6211 break;
6212 case DCT_MANGLED:
6213 dc = cplus_demangle_mangled_name (&di, 1);
6214 break;
6215 case DCT_GLOBAL_CTORS:
6216 case DCT_GLOBAL_DTORS:
6217 d_advance (&di, 11);
6218 dc = d_make_comp (&di,
6219 (type == DCT_GLOBAL_CTORS
6220 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6221 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
6222 d_make_demangle_mangled_name (&di, d_str (&di)),
6223 NULL);
6224 d_advance (&di, strlen (d_str (&di)));
6225 break;
6226 default:
6227 abort (); /* We have listed all the cases. */
6230 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6231 mangled string, then we didn't successfully demangle it. If
6232 DMGL_PARAMS is not set, we didn't look at the trailing
6233 parameters. */
6234 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
6235 dc = NULL;
6237 #ifdef CP_DEMANGLE_DEBUG
6238 d_dump (dc, 0);
6239 #endif
6241 status = (dc != NULL)
6242 ? cplus_demangle_print_callback (options, dc, callback, opaque)
6243 : 0;
6246 return status;
6249 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6250 name, return a buffer allocated with malloc holding the demangled
6251 name. OPTIONS is the usual libiberty demangler options. On
6252 success, this sets *PALC to the allocated size of the returned
6253 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6254 a memory allocation failure, and returns NULL. */
6256 static char *
6257 d_demangle (const char *mangled, int options, size_t *palc)
6259 struct d_growable_string dgs;
6260 int status;
6262 d_growable_string_init (&dgs, 0);
6264 status = d_demangle_callback (mangled, options,
6265 d_growable_string_callback_adapter, &dgs);
6266 if (status == 0)
6268 free (dgs.buf);
6269 *palc = 0;
6270 return NULL;
6273 *palc = dgs.allocation_failure ? 1 : dgs.alc;
6274 return dgs.buf;
6277 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6279 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6281 /* ia64 ABI-mandated entry point in the C++ runtime library for
6282 performing demangling. MANGLED_NAME is a NUL-terminated character
6283 string containing the name to be demangled.
6285 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6286 *LENGTH bytes, into which the demangled name is stored. If
6287 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6288 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6289 is placed in a region of memory allocated with malloc.
6291 If LENGTH is non-NULL, the length of the buffer containing the
6292 demangled name, is placed in *LENGTH.
6294 The return value is a pointer to the start of the NUL-terminated
6295 demangled name, or NULL if the demangling fails. The caller is
6296 responsible for deallocating this memory using free.
6298 *STATUS is set to one of the following values:
6299 0: The demangling operation succeeded.
6300 -1: A memory allocation failure occurred.
6301 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6302 -3: One of the arguments is invalid.
6304 The demangling is performed using the C++ ABI mangling rules, with
6305 GNU extensions. */
6307 char *
6308 __cxa_demangle (const char *mangled_name, char *output_buffer,
6309 size_t *length, int *status)
6311 char *demangled;
6312 size_t alc;
6314 if (mangled_name == NULL)
6316 if (status != NULL)
6317 *status = -3;
6318 return NULL;
6321 if (output_buffer != NULL && length == NULL)
6323 if (status != NULL)
6324 *status = -3;
6325 return NULL;
6328 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6330 if (demangled == NULL)
6332 if (status != NULL)
6334 if (alc == 1)
6335 *status = -1;
6336 else
6337 *status = -2;
6339 return NULL;
6342 if (output_buffer == NULL)
6344 if (length != NULL)
6345 *length = alc;
6347 else
6349 if (strlen (demangled) < *length)
6351 strcpy (output_buffer, demangled);
6352 free (demangled);
6353 demangled = output_buffer;
6355 else
6357 free (output_buffer);
6358 *length = alc;
6362 if (status != NULL)
6363 *status = 0;
6365 return demangled;
6368 extern int __gcclibcxx_demangle_callback (const char *,
6369 void (*)
6370 (const char *, size_t, void *),
6371 void *);
6373 /* Alternative, allocationless entry point in the C++ runtime library
6374 for performing demangling. MANGLED_NAME is a NUL-terminated character
6375 string containing the name to be demangled.
6377 CALLBACK is a callback function, called with demangled string
6378 segments as demangling progresses; it is called at least once,
6379 but may be called more than once. OPAQUE is a generalized pointer
6380 used as a callback argument.
6382 The return code is one of the following values, equivalent to
6383 the STATUS values of __cxa_demangle() (excluding -1, since this
6384 function performs no memory allocations):
6385 0: The demangling operation succeeded.
6386 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6387 -3: One of the arguments is invalid.
6389 The demangling is performed using the C++ ABI mangling rules, with
6390 GNU extensions. */
6393 __gcclibcxx_demangle_callback (const char *mangled_name,
6394 void (*callback) (const char *, size_t, void *),
6395 void *opaque)
6397 int status;
6399 if (mangled_name == NULL || callback == NULL)
6400 return -3;
6402 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6403 callback, opaque);
6404 if (status == 0)
6405 return -2;
6407 return 0;
6410 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6412 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6413 mangled name, return a buffer allocated with malloc holding the
6414 demangled name. Otherwise, return NULL. */
6416 char *
6417 cplus_demangle_v3 (const char *mangled, int options)
6419 size_t alc;
6421 return d_demangle (mangled, options, &alc);
6425 cplus_demangle_v3_callback (const char *mangled, int options,
6426 demangle_callbackref callback, void *opaque)
6428 return d_demangle_callback (mangled, options, callback, opaque);
6431 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6432 conventions, but the output formatting is a little different.
6433 This instructs the C++ demangler not to emit pointer characters ("*"), to
6434 use Java's namespace separator symbol ("." instead of "::"), and to output
6435 JArray<TYPE> as TYPE[]. */
6437 char *
6438 java_demangle_v3 (const char *mangled)
6440 size_t alc;
6442 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6446 java_demangle_v3_callback (const char *mangled,
6447 demangle_callbackref callback, void *opaque)
6449 return d_demangle_callback (mangled,
6450 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6451 callback, opaque);
6454 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6456 #ifndef IN_GLIBCPP_V3
6458 /* Demangle a string in order to find out whether it is a constructor
6459 or destructor. Return non-zero on success. Set *CTOR_KIND and
6460 *DTOR_KIND appropriately. */
6462 static int
6463 is_ctor_or_dtor (const char *mangled,
6464 enum gnu_v3_ctor_kinds *ctor_kind,
6465 enum gnu_v3_dtor_kinds *dtor_kind)
6467 struct d_info di;
6468 struct demangle_component *dc;
6469 int ret;
6471 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6472 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6474 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6477 #ifdef CP_DYNAMIC_ARRAYS
6478 __extension__ struct demangle_component comps[di.num_comps];
6479 __extension__ struct demangle_component *subs[di.num_subs];
6481 di.comps = comps;
6482 di.subs = subs;
6483 #else
6484 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6485 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6486 #endif
6488 dc = cplus_demangle_mangled_name (&di, 1);
6490 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6491 to demangle the entire string. */
6493 ret = 0;
6494 while (dc != NULL)
6496 switch (dc->type)
6498 /* These cannot appear on a constructor or destructor. */
6499 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6500 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6501 case DEMANGLE_COMPONENT_CONST_THIS:
6502 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6503 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6504 default:
6505 dc = NULL;
6506 break;
6507 case DEMANGLE_COMPONENT_TYPED_NAME:
6508 case DEMANGLE_COMPONENT_TEMPLATE:
6509 dc = d_left (dc);
6510 break;
6511 case DEMANGLE_COMPONENT_QUAL_NAME:
6512 case DEMANGLE_COMPONENT_LOCAL_NAME:
6513 dc = d_right (dc);
6514 break;
6515 case DEMANGLE_COMPONENT_CTOR:
6516 *ctor_kind = dc->u.s_ctor.kind;
6517 ret = 1;
6518 dc = NULL;
6519 break;
6520 case DEMANGLE_COMPONENT_DTOR:
6521 *dtor_kind = dc->u.s_dtor.kind;
6522 ret = 1;
6523 dc = NULL;
6524 break;
6529 return ret;
6532 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6533 name. A non-zero return indicates the type of constructor. */
6535 enum gnu_v3_ctor_kinds
6536 is_gnu_v3_mangled_ctor (const char *name)
6538 enum gnu_v3_ctor_kinds ctor_kind;
6539 enum gnu_v3_dtor_kinds dtor_kind;
6541 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6542 return (enum gnu_v3_ctor_kinds) 0;
6543 return ctor_kind;
6547 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6548 name. A non-zero return indicates the type of destructor. */
6550 enum gnu_v3_dtor_kinds
6551 is_gnu_v3_mangled_dtor (const char *name)
6553 enum gnu_v3_ctor_kinds ctor_kind;
6554 enum gnu_v3_dtor_kinds dtor_kind;
6556 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6557 return (enum gnu_v3_dtor_kinds) 0;
6558 return dtor_kind;
6561 #endif /* IN_GLIBCPP_V3 */
6563 #ifdef STANDALONE_DEMANGLER
6565 #include "getopt.h"
6566 #include "dyn-string.h"
6568 static void print_usage (FILE* fp, int exit_value);
6570 #define IS_ALPHA(CHAR) \
6571 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6572 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6574 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6575 #define is_mangled_char(CHAR) \
6576 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6577 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6579 /* The name of this program, as invoked. */
6580 const char* program_name;
6582 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6584 static void
6585 print_usage (FILE* fp, int exit_value)
6587 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6588 fprintf (fp, "Options:\n");
6589 fprintf (fp, " -h,--help Display this message.\n");
6590 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6591 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6592 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6594 exit (exit_value);
6597 /* Option specification for getopt_long. */
6598 static const struct option long_options[] =
6600 { "help", no_argument, NULL, 'h' },
6601 { "no-params", no_argument, NULL, 'p' },
6602 { "verbose", no_argument, NULL, 'v' },
6603 { NULL, no_argument, NULL, 0 },
6606 /* Main entry for a demangling filter executable. It will demangle
6607 its command line arguments, if any. If none are provided, it will
6608 filter stdin to stdout, replacing any recognized mangled C++ names
6609 with their demangled equivalents. */
6612 main (int argc, char *argv[])
6614 int i;
6615 int opt_char;
6616 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6618 /* Use the program name of this program, as invoked. */
6619 program_name = argv[0];
6621 /* Parse options. */
6624 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6625 switch (opt_char)
6627 case '?': /* Unrecognized option. */
6628 print_usage (stderr, 1);
6629 break;
6631 case 'h':
6632 print_usage (stdout, 0);
6633 break;
6635 case 'p':
6636 options &= ~ DMGL_PARAMS;
6637 break;
6639 case 'v':
6640 options |= DMGL_VERBOSE;
6641 break;
6644 while (opt_char != -1);
6646 if (optind == argc)
6647 /* No command line arguments were provided. Filter stdin. */
6649 dyn_string_t mangled = dyn_string_new (3);
6650 char *s;
6652 /* Read all of input. */
6653 while (!feof (stdin))
6655 char c;
6657 /* Pile characters into mangled until we hit one that can't
6658 occur in a mangled name. */
6659 c = getchar ();
6660 while (!feof (stdin) && is_mangled_char (c))
6662 dyn_string_append_char (mangled, c);
6663 if (feof (stdin))
6664 break;
6665 c = getchar ();
6668 if (dyn_string_length (mangled) > 0)
6670 #ifdef IN_GLIBCPP_V3
6671 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6672 #else
6673 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6674 #endif
6676 if (s != NULL)
6678 fputs (s, stdout);
6679 free (s);
6681 else
6683 /* It might not have been a mangled name. Print the
6684 original text. */
6685 fputs (dyn_string_buf (mangled), stdout);
6688 dyn_string_clear (mangled);
6691 /* If we haven't hit EOF yet, we've read one character that
6692 can't occur in a mangled name, so print it out. */
6693 if (!feof (stdin))
6694 putchar (c);
6697 dyn_string_delete (mangled);
6699 else
6700 /* Demangle command line arguments. */
6702 /* Loop over command line arguments. */
6703 for (i = optind; i < argc; ++i)
6705 char *s;
6706 #ifdef IN_GLIBCPP_V3
6707 int status;
6708 #endif
6710 /* Attempt to demangle. */
6711 #ifdef IN_GLIBCPP_V3
6712 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6713 #else
6714 s = cplus_demangle_v3 (argv[i], options);
6715 #endif
6717 /* If it worked, print the demangled name. */
6718 if (s != NULL)
6720 printf ("%s\n", s);
6721 free (s);
6723 else
6725 #ifdef IN_GLIBCPP_V3
6726 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6727 #else
6728 fprintf (stderr, "Failed: %s\n", argv[i]);
6729 #endif
6734 return 0;
6737 #endif /* STANDALONE_DEMANGLER */