FreeBSD: update README.freebsd
[valgrind.git] / coregrind / m_demangle / cp-demangle.c
blob870f27bb84d3e987a90f02bd6b733b3e41bbff04
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003-2023 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 https://itanium-cxx-abi.github.io/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 0 /* in valgrind */
102 #if defined (_AIX) && !defined (__GNUC__)
103 #pragma alloca
104 #endif
105 #endif /* ! in valgrind */
107 #if 0 /* in valgrind */
108 #ifdef HAVE_CONFIG_H
109 #include "config.h"
110 #endif
111 #endif /* ! in valgrind */
113 #if 0 /* in valgrind */
114 #include <stdio.h>
115 #endif /* ! in valgrind */
117 #if 0 /* in valgrind */
118 #ifdef HAVE_STDLIB_H
119 #include <stdlib.h>
120 #endif
121 #ifdef HAVE_STRING_H
122 #include <string.h>
123 #endif
124 #endif /* ! in valgrind */
126 #if 0 /* in valgrind */
127 #ifdef HAVE_ALLOCA_H
128 # include <alloca.h>
129 #else
130 # ifndef alloca
131 # ifdef __GNUC__
132 # define alloca __builtin_alloca
133 # else
134 extern char *alloca ();
135 # endif /* __GNUC__ */
136 # endif /* alloca */
137 #endif /* HAVE_ALLOCA_H */
138 #endif /* ! in valgrind */
140 #if 0 /* in valgrind */
141 #ifdef HAVE_LIMITS_H
142 #include <limits.h>
143 #endif
144 #endif /* ! in valgrind */
145 #ifndef INT_MAX
146 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
147 #endif
149 #if 0 /* in valgrind */
150 #include "ansidecl.h"
151 #include "libiberty.h"
152 #endif /* ! in valgrind */
154 #include "vg_libciface.h"
156 #include "demangle.h"
157 #include "cp-demangle.h"
159 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
160 also rename them via #define to avoid compiler errors when the
161 static definition conflicts with the extern declaration in a header
162 file. */
163 #ifdef IN_GLIBCPP_V3
165 #define CP_STATIC_IF_GLIBCPP_V3 static
167 #define cplus_demangle_fill_name d_fill_name
168 static int d_fill_name (struct demangle_component *, const char *, int);
170 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
171 static int
172 d_fill_extended_operator (struct demangle_component *, int,
173 struct demangle_component *);
175 #define cplus_demangle_fill_ctor d_fill_ctor
176 static int
177 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
178 struct demangle_component *);
180 #define cplus_demangle_fill_dtor d_fill_dtor
181 static int
182 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
183 struct demangle_component *);
185 #define cplus_demangle_mangled_name d_mangled_name
186 static struct demangle_component *d_mangled_name (struct d_info *, int);
188 #define cplus_demangle_type d_type
189 static struct demangle_component *d_type (struct d_info *);
191 #define cplus_demangle_print d_print
192 static char *d_print (int, struct demangle_component *, int, size_t *);
194 #define cplus_demangle_print_callback d_print_callback
195 static int d_print_callback (int, struct demangle_component *,
196 demangle_callbackref, void *);
198 #define cplus_demangle_init_info d_init_info
199 static void d_init_info (const char *, int, size_t, struct d_info *);
201 #else /* ! defined(IN_GLIBCPP_V3) */
202 #define CP_STATIC_IF_GLIBCPP_V3
203 #endif /* ! defined(IN_GLIBCPP_V3) */
205 /* See if the compiler supports dynamic arrays. */
207 #ifdef __GNUC__
208 #define CP_DYNAMIC_ARRAYS
209 #else
210 #ifdef __STDC__
211 #ifdef __STDC_VERSION__
212 #if __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__
213 #define CP_DYNAMIC_ARRAYS
214 #endif /* __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__ */
215 #endif /* defined (__STDC_VERSION__) */
216 #endif /* defined (__STDC__) */
217 #endif /* ! defined (__GNUC__) */
219 /* We avoid pulling in the ctype tables, to prevent pulling in
220 additional unresolved symbols when this code is used in a library.
221 FIXME: Is this really a valid reason? This comes from the original
222 V3 demangler code.
224 As of this writing this file has the following undefined references
225 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
226 strcat, strlen. */
228 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
229 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
230 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
232 /* The prefix prepended by GCC to an identifier represnting the
233 anonymous namespace. */
234 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
235 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
236 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
238 /* Information we keep for the standard substitutions. */
240 struct d_standard_sub_info
242 /* The code for this substitution. */
243 char code;
244 /* The simple string it expands to. */
245 const char *simple_expansion;
246 /* The length of the simple expansion. */
247 int simple_len;
248 /* The results of a full, verbose, expansion. This is used when
249 qualifying a constructor/destructor, or when in verbose mode. */
250 const char *full_expansion;
251 /* The length of the full expansion. */
252 int full_len;
253 /* What to set the last_name field of d_info to; NULL if we should
254 not set it. This is only relevant when qualifying a
255 constructor/destructor. */
256 const char *set_last_name;
257 /* The length of set_last_name. */
258 int set_last_name_len;
261 /* Accessors for subtrees of struct demangle_component. */
263 #define d_left(dc) ((dc)->u.s_binary.left)
264 #define d_right(dc) ((dc)->u.s_binary.right)
266 /* A list of templates. This is used while printing. */
268 struct d_print_template
270 /* Next template on the list. */
271 struct d_print_template *next;
272 /* This template. */
273 const struct demangle_component *template_decl;
276 /* A list of type modifiers. This is used while printing. */
278 struct d_print_mod
280 /* Next modifier on the list. These are in the reverse of the order
281 in which they appeared in the mangled string. */
282 struct d_print_mod *next;
283 /* The modifier. */
284 struct demangle_component *mod;
285 /* Whether this modifier was printed. */
286 int printed;
287 /* The list of templates which applies to this modifier. */
288 struct d_print_template *templates;
291 /* We use these structures to hold information during printing. */
293 struct d_growable_string
295 /* Buffer holding the result. */
296 char *buf;
297 /* Current length of data in buffer. */
298 size_t len;
299 /* Allocated size of buffer. */
300 size_t alc;
301 /* Set to 1 if we had a memory allocation failure. */
302 int allocation_failure;
305 /* Stack of components, innermost first, used to avoid loops. */
307 struct d_component_stack
309 /* This component. */
310 const struct demangle_component *dc;
311 /* This component's parent. */
312 const struct d_component_stack *parent;
315 /* A demangle component and some scope captured when it was first
316 traversed. */
318 struct d_saved_scope
320 /* The component whose scope this is. */
321 const struct demangle_component *container;
322 /* The list of templates, if any, that was current when this
323 scope was captured. */
324 struct d_print_template *templates;
327 /* Checkpoint structure to allow backtracking. This holds copies
328 of the fields of struct d_info that need to be restored
329 if a trial parse needs to be backtracked over. */
331 struct d_info_checkpoint
333 const char *n;
334 int next_comp;
335 int next_sub;
336 int expansion;
339 /* Maximum number of times d_print_comp may be called recursively. */
340 #define MAX_RECURSION_COUNT 1024
342 enum { D_PRINT_BUFFER_LENGTH = 256 };
343 struct d_print_info
345 /* Fixed-length allocated buffer for demangled data, flushed to the
346 callback with a NUL termination once full. */
347 char buf[D_PRINT_BUFFER_LENGTH];
348 /* Current length of data in buffer. */
349 size_t len;
350 /* The last character printed, saved individually so that it survives
351 any buffer flush. */
352 char last_char;
353 /* Callback function to handle demangled buffer flush. */
354 demangle_callbackref callback;
355 /* Opaque callback argument. */
356 void *opaque;
357 /* The current list of templates, if any. */
358 struct d_print_template *templates;
359 /* The current list of modifiers (e.g., pointer, reference, etc.),
360 if any. */
361 struct d_print_mod *modifiers;
362 /* Set to 1 if we saw a demangling error. */
363 int demangle_failure;
364 /* Number of times d_print_comp was recursively called. Should not
365 be bigger than MAX_RECURSION_COUNT. */
366 int recursion;
367 /* 1 more than the number of explicit template parms of a lambda. Template
368 parm references >= are actually 'auto'. */
369 int lambda_tpl_parms;
370 /* The current index into any template argument packs we are using
371 for printing, or -1 to print the whole pack. */
372 int pack_index;
373 /* Number of d_print_flush calls so far. */
374 unsigned long int flush_count;
375 /* Stack of components, innermost first, used to avoid loops. */
376 const struct d_component_stack *component_stack;
377 /* Array of saved scopes for evaluating substitutions. */
378 struct d_saved_scope *saved_scopes;
379 /* Index of the next unused saved scope in the above array. */
380 int next_saved_scope;
381 /* Number of saved scopes in the above array. */
382 int num_saved_scopes;
383 /* Array of templates for saving into scopes. */
384 struct d_print_template *copy_templates;
385 /* Index of the next unused copy template in the above array. */
386 int next_copy_template;
387 /* Number of copy templates in the above array. */
388 int num_copy_templates;
389 /* The nearest enclosing template, if any. */
390 const struct demangle_component *current_template;
393 #ifdef CP_DEMANGLE_DEBUG
394 static void d_dump (struct demangle_component *, int);
395 #endif
397 static struct demangle_component *
398 d_make_empty (struct d_info *);
400 static struct demangle_component *
401 d_make_comp (struct d_info *, enum demangle_component_type,
402 struct demangle_component *,
403 struct demangle_component *);
405 static struct demangle_component *
406 d_make_name (struct d_info *, const char *, int);
408 static struct demangle_component *
409 d_make_demangle_mangled_name (struct d_info *, const char *);
411 static struct demangle_component *
412 d_make_builtin_type (struct d_info *,
413 const struct demangle_builtin_type_info *);
415 static struct demangle_component *
416 d_make_operator (struct d_info *,
417 const struct demangle_operator_info *);
419 static struct demangle_component *
420 d_make_extended_operator (struct d_info *, int,
421 struct demangle_component *);
423 static struct demangle_component *
424 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
425 struct demangle_component *);
427 static struct demangle_component *
428 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
429 struct demangle_component *);
431 static struct demangle_component *
432 d_make_template_param (struct d_info *, int);
434 static struct demangle_component *
435 d_make_sub (struct d_info *, const char *, int);
437 static int
438 has_return_type (struct demangle_component *);
440 static int
441 is_ctor_dtor_or_conversion (struct demangle_component *);
443 static struct demangle_component *d_encoding (struct d_info *, int);
445 static struct demangle_component *d_name (struct d_info *, int substable);
447 static struct demangle_component *d_nested_name (struct d_info *);
449 static int d_maybe_module_name (struct d_info *, struct demangle_component **);
451 static struct demangle_component *d_prefix (struct d_info *, int);
453 static struct demangle_component *d_unqualified_name (struct d_info *,
454 struct demangle_component *scope, struct demangle_component *module);
456 static struct demangle_component *d_source_name (struct d_info *);
458 static int d_number (struct d_info *);
460 static struct demangle_component *d_identifier (struct d_info *, int);
462 static struct demangle_component *d_operator_name (struct d_info *);
464 static struct demangle_component *d_special_name (struct d_info *);
466 static struct demangle_component *d_parmlist (struct d_info *);
468 static int d_call_offset (struct d_info *, int);
470 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
472 static struct demangle_component **
473 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
475 static struct demangle_component *
476 d_ref_qualifier (struct d_info *, struct demangle_component *);
478 static struct demangle_component *
479 d_function_type (struct d_info *);
481 static struct demangle_component *
482 d_bare_function_type (struct d_info *, int);
484 static struct demangle_component *
485 d_class_enum_type (struct d_info *, int);
487 static struct demangle_component *d_array_type (struct d_info *);
489 static struct demangle_component *d_vector_type (struct d_info *);
491 static struct demangle_component *
492 d_pointer_to_member_type (struct d_info *);
494 static struct demangle_component *
495 d_template_param (struct d_info *);
497 static struct demangle_component *d_template_args (struct d_info *);
498 static struct demangle_component *d_template_args_1 (struct d_info *);
500 static struct demangle_component *
501 d_template_arg (struct d_info *);
503 static struct demangle_component *d_expression (struct d_info *);
505 static struct demangle_component *d_expr_primary (struct d_info *);
507 static struct demangle_component *d_local_name (struct d_info *);
509 static int d_discriminator (struct d_info *);
511 static struct demangle_component *d_template_parm (struct d_info *, int *bad);
513 static struct demangle_component *d_template_head (struct d_info *, int *bad);
515 static struct demangle_component *d_lambda (struct d_info *);
517 static struct demangle_component *d_unnamed_type (struct d_info *);
519 static struct demangle_component *
520 d_clone_suffix (struct d_info *, struct demangle_component *);
522 static int
523 d_add_substitution (struct d_info *, struct demangle_component *);
525 static struct demangle_component *d_substitution (struct d_info *, int);
527 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
529 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
531 static void d_growable_string_init (struct d_growable_string *, size_t);
533 static inline void
534 d_growable_string_resize (struct d_growable_string *, size_t);
536 static inline void
537 d_growable_string_append_buffer (struct d_growable_string *,
538 const char *, size_t);
539 static void
540 d_growable_string_callback_adapter (const char *, size_t, void *);
542 static void
543 d_print_init (struct d_print_info *, demangle_callbackref, void *,
544 struct demangle_component *);
546 static inline void d_print_error (struct d_print_info *);
548 static inline int d_print_saw_error (struct d_print_info *);
550 static inline void d_print_flush (struct d_print_info *);
552 static inline void d_append_char (struct d_print_info *, char);
554 static inline void d_append_buffer (struct d_print_info *,
555 const char *, size_t);
557 static inline void d_append_string (struct d_print_info *, const char *);
559 static inline char d_last_char (struct d_print_info *);
561 static void
562 d_print_comp (struct d_print_info *, int, struct demangle_component *);
564 static void
565 d_print_java_identifier (struct d_print_info *, const char *, int);
567 static void
568 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
570 static void
571 d_print_mod (struct d_print_info *, int, struct demangle_component *);
573 static void
574 d_print_function_type (struct d_print_info *, int,
575 struct demangle_component *,
576 struct d_print_mod *);
578 static void
579 d_print_array_type (struct d_print_info *, int,
580 struct demangle_component *,
581 struct d_print_mod *);
583 static void
584 d_print_expr_op (struct d_print_info *, int, struct demangle_component *);
586 static void d_print_cast (struct d_print_info *, int,
587 struct demangle_component *);
588 static void d_print_conversion (struct d_print_info *, int,
589 struct demangle_component *);
591 static int d_demangle_callback (const char *, int,
592 demangle_callbackref, void *);
593 static char *d_demangle (const char *, int, size_t *);
595 #define FNQUAL_COMPONENT_CASE \
596 case DEMANGLE_COMPONENT_RESTRICT_THIS: \
597 case DEMANGLE_COMPONENT_VOLATILE_THIS: \
598 case DEMANGLE_COMPONENT_CONST_THIS: \
599 case DEMANGLE_COMPONENT_REFERENCE_THIS: \
600 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
601 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
602 case DEMANGLE_COMPONENT_NOEXCEPT: \
603 case DEMANGLE_COMPONENT_THROW_SPEC
605 /* True iff TYPE is a demangling component representing a
606 function-type-qualifier. */
608 static int
609 is_fnqual_component_type (enum demangle_component_type type)
611 switch (type)
613 FNQUAL_COMPONENT_CASE:
614 return 1;
615 default:
616 break;
618 return 0;
622 #ifdef CP_DEMANGLE_DEBUG
624 static void
625 d_dump (struct demangle_component *dc, int indent)
627 int i;
629 if (dc == NULL)
631 if (indent == 0)
632 printf ("failed demangling\n");
633 return;
636 for (i = 0; i < indent; ++i)
637 putchar (' ');
639 switch (dc->type)
641 case DEMANGLE_COMPONENT_NAME:
642 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
643 return;
644 case DEMANGLE_COMPONENT_TAGGED_NAME:
645 printf ("tagged name\n");
646 d_dump (dc->u.s_binary.left, indent + 2);
647 d_dump (dc->u.s_binary.right, indent + 2);
648 return;
649 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
650 printf ("template parameter %ld\n", dc->u.s_number.number);
651 return;
652 case DEMANGLE_COMPONENT_TPARM_OBJ:
653 printf ("template parameter object\n");
654 break;
655 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
656 printf ("function parameter %ld\n", dc->u.s_number.number);
657 return;
658 case DEMANGLE_COMPONENT_CTOR:
659 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
660 d_dump (dc->u.s_ctor.name, indent + 2);
661 return;
662 case DEMANGLE_COMPONENT_DTOR:
663 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
664 d_dump (dc->u.s_dtor.name, indent + 2);
665 return;
666 case DEMANGLE_COMPONENT_SUB_STD:
667 printf ("standard substitution %s\n", dc->u.s_string.string);
668 return;
669 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
670 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
671 return;
672 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
674 char suffix[2] = { dc->u.s_extended_builtin.type->suffix, 0 };
675 printf ("builtin type %s%d%s\n", dc->u.s_extended_builtin.type->name,
676 dc->u.s_extended_builtin.type->arg, suffix);
678 return;
679 case DEMANGLE_COMPONENT_OPERATOR:
680 printf ("operator %s\n", dc->u.s_operator.op->name);
681 return;
682 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
683 printf ("extended operator with %d args\n",
684 dc->u.s_extended_operator.args);
685 d_dump (dc->u.s_extended_operator.name, indent + 2);
686 return;
688 case DEMANGLE_COMPONENT_QUAL_NAME:
689 printf ("qualified name\n");
690 break;
691 case DEMANGLE_COMPONENT_LOCAL_NAME:
692 printf ("local name\n");
693 break;
694 case DEMANGLE_COMPONENT_TYPED_NAME:
695 printf ("typed name\n");
696 break;
697 case DEMANGLE_COMPONENT_TEMPLATE:
698 printf ("template\n");
699 break;
700 case DEMANGLE_COMPONENT_VTABLE:
701 printf ("vtable\n");
702 break;
703 case DEMANGLE_COMPONENT_VTT:
704 printf ("VTT\n");
705 break;
706 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
707 printf ("construction vtable\n");
708 break;
709 case DEMANGLE_COMPONENT_TYPEINFO:
710 printf ("typeinfo\n");
711 break;
712 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
713 printf ("typeinfo name\n");
714 break;
715 case DEMANGLE_COMPONENT_TYPEINFO_FN:
716 printf ("typeinfo function\n");
717 break;
718 case DEMANGLE_COMPONENT_THUNK:
719 printf ("thunk\n");
720 break;
721 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
722 printf ("virtual thunk\n");
723 break;
724 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
725 printf ("covariant thunk\n");
726 break;
727 case DEMANGLE_COMPONENT_JAVA_CLASS:
728 printf ("java class\n");
729 break;
730 case DEMANGLE_COMPONENT_GUARD:
731 printf ("guard\n");
732 break;
733 case DEMANGLE_COMPONENT_REFTEMP:
734 printf ("reference temporary\n");
735 break;
736 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
737 printf ("hidden alias\n");
738 break;
739 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
740 printf ("transaction clone\n");
741 break;
742 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
743 printf ("non-transaction clone\n");
744 break;
745 case DEMANGLE_COMPONENT_RESTRICT:
746 printf ("restrict\n");
747 break;
748 case DEMANGLE_COMPONENT_VOLATILE:
749 printf ("volatile\n");
750 break;
751 case DEMANGLE_COMPONENT_CONST:
752 printf ("const\n");
753 break;
754 case DEMANGLE_COMPONENT_RESTRICT_THIS:
755 printf ("restrict this\n");
756 break;
757 case DEMANGLE_COMPONENT_VOLATILE_THIS:
758 printf ("volatile this\n");
759 break;
760 case DEMANGLE_COMPONENT_CONST_THIS:
761 printf ("const this\n");
762 break;
763 case DEMANGLE_COMPONENT_REFERENCE_THIS:
764 printf ("reference this\n");
765 break;
766 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
767 printf ("rvalue reference this\n");
768 break;
769 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
770 printf ("transaction_safe this\n");
771 break;
772 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
773 printf ("vendor type qualifier\n");
774 break;
775 case DEMANGLE_COMPONENT_POINTER:
776 printf ("pointer\n");
777 break;
778 case DEMANGLE_COMPONENT_REFERENCE:
779 printf ("reference\n");
780 break;
781 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
782 printf ("rvalue reference\n");
783 break;
784 case DEMANGLE_COMPONENT_COMPLEX:
785 printf ("complex\n");
786 break;
787 case DEMANGLE_COMPONENT_IMAGINARY:
788 printf ("imaginary\n");
789 break;
790 case DEMANGLE_COMPONENT_VENDOR_TYPE:
791 printf ("vendor type\n");
792 break;
793 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
794 printf ("function type\n");
795 break;
796 case DEMANGLE_COMPONENT_ARRAY_TYPE:
797 printf ("array type\n");
798 break;
799 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
800 printf ("pointer to member type\n");
801 break;
802 case DEMANGLE_COMPONENT_ARGLIST:
803 printf ("argument list\n");
804 break;
805 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
806 printf ("template argument list\n");
807 break;
808 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
809 printf ("initializer list\n");
810 break;
811 case DEMANGLE_COMPONENT_CAST:
812 printf ("cast\n");
813 break;
814 case DEMANGLE_COMPONENT_CONVERSION:
815 printf ("conversion operator\n");
816 break;
817 case DEMANGLE_COMPONENT_NULLARY:
818 printf ("nullary operator\n");
819 break;
820 case DEMANGLE_COMPONENT_UNARY:
821 printf ("unary operator\n");
822 break;
823 case DEMANGLE_COMPONENT_BINARY:
824 printf ("binary operator\n");
825 break;
826 case DEMANGLE_COMPONENT_BINARY_ARGS:
827 printf ("binary operator arguments\n");
828 break;
829 case DEMANGLE_COMPONENT_TRINARY:
830 printf ("trinary operator\n");
831 break;
832 case DEMANGLE_COMPONENT_TRINARY_ARG1:
833 printf ("trinary operator arguments 1\n");
834 break;
835 case DEMANGLE_COMPONENT_TRINARY_ARG2:
836 printf ("trinary operator arguments 1\n");
837 break;
838 case DEMANGLE_COMPONENT_LITERAL:
839 printf ("literal\n");
840 break;
841 case DEMANGLE_COMPONENT_LITERAL_NEG:
842 printf ("negative literal\n");
843 break;
844 case DEMANGLE_COMPONENT_VENDOR_EXPR:
845 printf ("vendor expression\n");
846 break;
847 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
848 printf ("java resource\n");
849 break;
850 case DEMANGLE_COMPONENT_COMPOUND_NAME:
851 printf ("compound name\n");
852 break;
853 case DEMANGLE_COMPONENT_CHARACTER:
854 printf ("character '%c'\n", dc->u.s_character.character);
855 return;
856 case DEMANGLE_COMPONENT_NUMBER:
857 printf ("number %ld\n", dc->u.s_number.number);
858 return;
859 case DEMANGLE_COMPONENT_DECLTYPE:
860 printf ("decltype\n");
861 break;
862 case DEMANGLE_COMPONENT_PACK_EXPANSION:
863 printf ("pack expansion\n");
864 break;
865 case DEMANGLE_COMPONENT_TLS_INIT:
866 printf ("tls init function\n");
867 break;
868 case DEMANGLE_COMPONENT_TLS_WRAPPER:
869 printf ("tls wrapper function\n");
870 break;
871 case DEMANGLE_COMPONENT_DEFAULT_ARG:
872 printf ("default argument %d\n", dc->u.s_unary_num.num);
873 d_dump (dc->u.s_unary_num.sub, indent+2);
874 return;
875 case DEMANGLE_COMPONENT_LAMBDA:
876 printf ("lambda %d\n", dc->u.s_unary_num.num);
877 d_dump (dc->u.s_unary_num.sub, indent+2);
878 return;
881 d_dump (d_left (dc), indent + 2);
882 d_dump (d_right (dc), indent + 2);
885 #endif /* CP_DEMANGLE_DEBUG */
887 /* Fill in a DEMANGLE_COMPONENT_NAME. */
889 CP_STATIC_IF_GLIBCPP_V3
891 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
893 if (p == NULL || s == NULL || len <= 0)
894 return 0;
895 p->d_printing = 0;
896 p->d_counting = 0;
897 p->type = DEMANGLE_COMPONENT_NAME;
898 p->u.s_name.s = s;
899 p->u.s_name.len = len;
900 return 1;
903 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
905 CP_STATIC_IF_GLIBCPP_V3
907 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
908 struct demangle_component *name)
910 if (p == NULL || args < 0 || name == NULL)
911 return 0;
912 p->d_printing = 0;
913 p->d_counting = 0;
914 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
915 p->u.s_extended_operator.args = args;
916 p->u.s_extended_operator.name = name;
917 return 1;
920 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
922 CP_STATIC_IF_GLIBCPP_V3
924 cplus_demangle_fill_ctor (struct demangle_component *p,
925 enum gnu_v3_ctor_kinds kind,
926 struct demangle_component *name)
928 if (p == NULL
929 || name == NULL
930 || (int) kind < gnu_v3_complete_object_ctor
931 || (int) kind > gnu_v3_object_ctor_group)
932 return 0;
933 p->d_printing = 0;
934 p->d_counting = 0;
935 p->type = DEMANGLE_COMPONENT_CTOR;
936 p->u.s_ctor.kind = kind;
937 p->u.s_ctor.name = name;
938 return 1;
941 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
943 CP_STATIC_IF_GLIBCPP_V3
945 cplus_demangle_fill_dtor (struct demangle_component *p,
946 enum gnu_v3_dtor_kinds kind,
947 struct demangle_component *name)
949 if (p == NULL
950 || name == NULL
951 || (int) kind < gnu_v3_deleting_dtor
952 || (int) kind > gnu_v3_object_dtor_group)
953 return 0;
954 p->d_printing = 0;
955 p->d_counting = 0;
956 p->type = DEMANGLE_COMPONENT_DTOR;
957 p->u.s_dtor.kind = kind;
958 p->u.s_dtor.name = name;
959 return 1;
962 /* Add a new component. */
964 static struct demangle_component *
965 d_make_empty (struct d_info *di)
967 struct demangle_component *p;
969 if (di->next_comp >= di->num_comps)
970 return NULL;
971 p = &di->comps[di->next_comp];
972 p->d_printing = 0;
973 p->d_counting = 0;
974 ++di->next_comp;
975 return p;
978 /* Add a new generic component. */
980 static struct demangle_component *
981 d_make_comp (struct d_info *di, enum demangle_component_type type,
982 struct demangle_component *left,
983 struct demangle_component *right)
985 struct demangle_component *p;
987 /* We check for errors here. A typical error would be a NULL return
988 from a subroutine. We catch those here, and return NULL
989 upward. */
990 switch (type)
992 /* These types require two parameters. */
993 case DEMANGLE_COMPONENT_QUAL_NAME:
994 case DEMANGLE_COMPONENT_LOCAL_NAME:
995 case DEMANGLE_COMPONENT_TYPED_NAME:
996 case DEMANGLE_COMPONENT_TAGGED_NAME:
997 case DEMANGLE_COMPONENT_TEMPLATE:
998 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
999 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
1000 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
1001 case DEMANGLE_COMPONENT_UNARY:
1002 case DEMANGLE_COMPONENT_BINARY:
1003 case DEMANGLE_COMPONENT_BINARY_ARGS:
1004 case DEMANGLE_COMPONENT_TRINARY:
1005 case DEMANGLE_COMPONENT_TRINARY_ARG1:
1006 case DEMANGLE_COMPONENT_LITERAL:
1007 case DEMANGLE_COMPONENT_LITERAL_NEG:
1008 case DEMANGLE_COMPONENT_VENDOR_EXPR:
1009 case DEMANGLE_COMPONENT_COMPOUND_NAME:
1010 case DEMANGLE_COMPONENT_VECTOR_TYPE:
1011 case DEMANGLE_COMPONENT_CLONE:
1012 case DEMANGLE_COMPONENT_MODULE_ENTITY:
1013 if (left == NULL || right == NULL)
1014 return NULL;
1015 break;
1017 /* These types only require one parameter. */
1018 case DEMANGLE_COMPONENT_VTABLE:
1019 case DEMANGLE_COMPONENT_VTT:
1020 case DEMANGLE_COMPONENT_TYPEINFO:
1021 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
1022 case DEMANGLE_COMPONENT_TYPEINFO_FN:
1023 case DEMANGLE_COMPONENT_THUNK:
1024 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
1025 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
1026 case DEMANGLE_COMPONENT_JAVA_CLASS:
1027 case DEMANGLE_COMPONENT_GUARD:
1028 case DEMANGLE_COMPONENT_TLS_INIT:
1029 case DEMANGLE_COMPONENT_TLS_WRAPPER:
1030 case DEMANGLE_COMPONENT_REFTEMP:
1031 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
1032 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
1033 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
1034 case DEMANGLE_COMPONENT_POINTER:
1035 case DEMANGLE_COMPONENT_REFERENCE:
1036 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
1037 case DEMANGLE_COMPONENT_COMPLEX:
1038 case DEMANGLE_COMPONENT_IMAGINARY:
1039 case DEMANGLE_COMPONENT_VENDOR_TYPE:
1040 case DEMANGLE_COMPONENT_CAST:
1041 case DEMANGLE_COMPONENT_CONVERSION:
1042 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
1043 case DEMANGLE_COMPONENT_DECLTYPE:
1044 case DEMANGLE_COMPONENT_PACK_EXPANSION:
1045 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
1046 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
1047 case DEMANGLE_COMPONENT_NULLARY:
1048 case DEMANGLE_COMPONENT_TRINARY_ARG2:
1049 case DEMANGLE_COMPONENT_TPARM_OBJ:
1050 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
1051 case DEMANGLE_COMPONENT_MODULE_INIT:
1052 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
1053 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
1054 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
1055 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
1056 if (left == NULL)
1057 return NULL;
1058 break;
1060 /* This needs a right parameter, but the left parameter can be
1061 empty. */
1062 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1063 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
1064 case DEMANGLE_COMPONENT_MODULE_NAME:
1065 case DEMANGLE_COMPONENT_MODULE_PARTITION:
1066 if (right == NULL)
1067 return NULL;
1068 break;
1070 /* These are allowed to have no parameters--in some cases they
1071 will be filled in later. */
1072 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1073 case DEMANGLE_COMPONENT_RESTRICT:
1074 case DEMANGLE_COMPONENT_VOLATILE:
1075 case DEMANGLE_COMPONENT_CONST:
1076 case DEMANGLE_COMPONENT_ARGLIST:
1077 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1078 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
1079 FNQUAL_COMPONENT_CASE:
1080 break;
1082 /* Other types should not be seen here. */
1083 default:
1084 return NULL;
1087 p = d_make_empty (di);
1088 if (p != NULL)
1090 p->type = type;
1091 p->u.s_binary.left = left;
1092 p->u.s_binary.right = right;
1094 return p;
1097 /* Add a new demangle mangled name component. */
1099 static struct demangle_component *
1100 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1102 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1103 return d_make_name (di, s, strlen (s));
1104 d_advance (di, 2);
1105 return d_encoding (di, 0);
1108 /* Add a new name component. */
1110 static struct demangle_component *
1111 d_make_name (struct d_info *di, const char *s, int len)
1113 struct demangle_component *p;
1115 p = d_make_empty (di);
1116 if (! cplus_demangle_fill_name (p, s, len))
1117 return NULL;
1118 return p;
1121 /* Add a new builtin type component. */
1123 static struct demangle_component *
1124 d_make_builtin_type (struct d_info *di,
1125 const struct demangle_builtin_type_info *type)
1127 struct demangle_component *p;
1129 if (type == NULL)
1130 return NULL;
1131 p = d_make_empty (di);
1132 if (p != NULL)
1134 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1135 p->u.s_builtin.type = type;
1137 return p;
1140 /* Add a new extended builtin type component. */
1142 static struct demangle_component *
1143 d_make_extended_builtin_type (struct d_info *di,
1144 const struct demangle_builtin_type_info *type,
1145 short arg, char suffix)
1147 struct demangle_component *p;
1149 if (type == NULL)
1150 return NULL;
1151 p = d_make_empty (di);
1152 if (p != NULL)
1154 p->type = DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE;
1155 p->u.s_extended_builtin.type = type;
1156 p->u.s_extended_builtin.arg = arg;
1157 p->u.s_extended_builtin.suffix = suffix;
1159 return p;
1162 /* Add a new operator component. */
1164 static struct demangle_component *
1165 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1167 struct demangle_component *p;
1169 p = d_make_empty (di);
1170 if (p != NULL)
1172 p->type = DEMANGLE_COMPONENT_OPERATOR;
1173 p->u.s_operator.op = op;
1175 return p;
1178 /* Add a new extended operator component. */
1180 static struct demangle_component *
1181 d_make_extended_operator (struct d_info *di, int args,
1182 struct demangle_component *name)
1184 struct demangle_component *p;
1186 p = d_make_empty (di);
1187 if (! cplus_demangle_fill_extended_operator (p, args, name))
1188 return NULL;
1189 return p;
1192 static struct demangle_component *
1193 d_make_default_arg (struct d_info *di, int num,
1194 struct demangle_component *sub)
1196 struct demangle_component *p = d_make_empty (di);
1197 if (p)
1199 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1200 p->u.s_unary_num.num = num;
1201 p->u.s_unary_num.sub = sub;
1203 return p;
1206 /* Add a new constructor component. */
1208 static struct demangle_component *
1209 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1210 struct demangle_component *name)
1212 struct demangle_component *p;
1214 p = d_make_empty (di);
1215 if (! cplus_demangle_fill_ctor (p, kind, name))
1216 return NULL;
1217 return p;
1220 /* Add a new destructor component. */
1222 static struct demangle_component *
1223 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1224 struct demangle_component *name)
1226 struct demangle_component *p;
1228 p = d_make_empty (di);
1229 if (! cplus_demangle_fill_dtor (p, kind, name))
1230 return NULL;
1231 return p;
1234 /* Add a new template parameter. */
1236 static struct demangle_component *
1237 d_make_template_param (struct d_info *di, int i)
1239 struct demangle_component *p;
1241 p = d_make_empty (di);
1242 if (p != NULL)
1244 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1245 p->u.s_number.number = i;
1247 return p;
1250 /* Add a new function parameter. */
1252 static struct demangle_component *
1253 d_make_function_param (struct d_info *di, int i)
1255 struct demangle_component *p;
1257 p = d_make_empty (di);
1258 if (p != NULL)
1260 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1261 p->u.s_number.number = i;
1263 return p;
1266 /* Add a new standard substitution component. */
1268 static struct demangle_component *
1269 d_make_sub (struct d_info *di, const char *name, int len)
1271 struct demangle_component *p;
1273 p = d_make_empty (di);
1274 if (p != NULL)
1276 p->type = DEMANGLE_COMPONENT_SUB_STD;
1277 p->u.s_string.string = name;
1278 p->u.s_string.len = len;
1280 return p;
1283 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1285 TOP_LEVEL is non-zero when called at the top level. */
1287 CP_STATIC_IF_GLIBCPP_V3
1288 struct demangle_component *
1289 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1291 struct demangle_component *p;
1293 if (! d_check_char (di, '_')
1294 /* Allow missing _ if not at toplevel to work around a
1295 bug in G++ abi-version=2 mangling; see the comment in
1296 write_template_arg. */
1297 && top_level)
1298 return NULL;
1299 if (! d_check_char (di, 'Z'))
1300 return NULL;
1301 p = d_encoding (di, top_level);
1303 /* If at top level and parsing parameters, check for a clone
1304 suffix. */
1305 if (top_level && (di->options & DMGL_PARAMS) != 0)
1306 while (d_peek_char (di) == '.'
1307 && (IS_LOWER (d_peek_next_char (di))
1308 || d_peek_next_char (di) == '_'
1309 || IS_DIGIT (d_peek_next_char (di))))
1310 p = d_clone_suffix (di, p);
1312 return p;
1315 /* Return whether a function should have a return type. The argument
1316 is the function name, which may be qualified in various ways. The
1317 rules are that template functions have return types with some
1318 exceptions, function types which are not part of a function name
1319 mangling have return types with some exceptions, and non-template
1320 function names do not have return types. The exceptions are that
1321 constructors, destructors, and conversion operators do not have
1322 return types. */
1324 static int
1325 has_return_type (struct demangle_component *dc)
1327 if (dc == NULL)
1328 return 0;
1329 switch (dc->type)
1331 default:
1332 return 0;
1333 case DEMANGLE_COMPONENT_LOCAL_NAME:
1334 return has_return_type (d_right (dc));
1335 case DEMANGLE_COMPONENT_TEMPLATE:
1336 return ! is_ctor_dtor_or_conversion (d_left (dc));
1337 FNQUAL_COMPONENT_CASE:
1338 return has_return_type (d_left (dc));
1342 /* Return whether a name is a constructor, a destructor, or a
1343 conversion operator. */
1345 static int
1346 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1348 if (dc == NULL)
1349 return 0;
1350 switch (dc->type)
1352 default:
1353 return 0;
1354 case DEMANGLE_COMPONENT_QUAL_NAME:
1355 case DEMANGLE_COMPONENT_LOCAL_NAME:
1356 return is_ctor_dtor_or_conversion (d_right (dc));
1357 case DEMANGLE_COMPONENT_CTOR:
1358 case DEMANGLE_COMPONENT_DTOR:
1359 case DEMANGLE_COMPONENT_CONVERSION:
1360 return 1;
1364 /* <encoding> ::= <(function) name> <bare-function-type>
1365 ::= <(data) name>
1366 ::= <special-name>
1368 TOP_LEVEL is non-zero when called at the top level, in which case
1369 if DMGL_PARAMS is not set we do not demangle the function
1370 parameters. We only set this at the top level, because otherwise
1371 we would not correctly demangle names in local scopes. */
1373 static struct demangle_component *
1374 d_encoding (struct d_info *di, int top_level)
1376 char peek = d_peek_char (di);
1377 struct demangle_component *dc;
1379 if (peek == 'G' || peek == 'T')
1380 dc = d_special_name (di);
1381 else
1383 dc = d_name (di, 0);
1385 if (!dc)
1386 /* Failed already. */;
1387 else if (top_level && (di->options & DMGL_PARAMS) == 0)
1389 /* Strip off any initial CV-qualifiers, as they really apply
1390 to the `this' parameter, and they were not output by the
1391 v2 demangler without DMGL_PARAMS. */
1392 while (is_fnqual_component_type (dc->type))
1393 dc = d_left (dc);
1395 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1396 there may be function-qualifiers on its right argument which
1397 really apply here; this happens when parsing a class
1398 which is local to a function. */
1399 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1401 while (d_right (dc) != NULL
1402 && is_fnqual_component_type (d_right (dc)->type))
1403 d_right (dc) = d_left (d_right (dc));
1405 if (d_right (dc) == NULL)
1406 dc = NULL;
1409 else
1411 peek = d_peek_char (di);
1412 if (peek != '\0' && peek != 'E')
1414 struct demangle_component *ftype;
1416 ftype = d_bare_function_type (di, has_return_type (dc));
1417 if (ftype)
1419 /* If this is a non-top-level local-name, clear the
1420 return type, so it doesn't confuse the user by
1421 being confused with the return type of whaever
1422 this is nested within. */
1423 if (!top_level && dc->type == DEMANGLE_COMPONENT_LOCAL_NAME
1424 && ftype->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
1425 d_left (ftype) = NULL;
1427 dc = d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME,
1428 dc, ftype);
1430 else
1431 dc = NULL;
1436 return dc;
1439 /* <tagged-name> ::= <name> B <source-name> */
1441 static struct demangle_component *
1442 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1444 struct demangle_component *hold_last_name;
1445 char peek;
1447 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1448 hold_last_name = di->last_name;
1450 while (peek = d_peek_char (di),
1451 peek == 'B')
1453 struct demangle_component *tag;
1454 d_advance (di, 1);
1455 tag = d_source_name (di);
1456 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1459 di->last_name = hold_last_name;
1461 return dc;
1464 /* <name> ::= <nested-name>
1465 ::= <unscoped-name>
1466 ::= <unscoped-template-name> <template-args>
1467 ::= <local-name>
1469 <unscoped-name> ::= <unqualified-name>
1470 ::= St <unqualified-name>
1472 <unscoped-template-name> ::= <unscoped-name>
1473 ::= <substitution>
1476 static struct demangle_component *
1477 d_name (struct d_info *di, int substable)
1479 char peek = d_peek_char (di);
1480 struct demangle_component *dc = NULL;
1481 struct demangle_component *module = NULL;
1482 int subst = 0;
1484 switch (peek)
1486 case 'N':
1487 dc = d_nested_name (di);
1488 break;
1490 case 'Z':
1491 dc = d_local_name (di);
1492 break;
1494 case 'U':
1495 dc = d_unqualified_name (di, NULL, NULL);
1496 break;
1498 case 'S':
1500 if (d_peek_next_char (di) == 't')
1502 d_advance (di, 2);
1503 dc = d_make_name (di, "std", 3);
1504 di->expansion += 3;
1507 if (d_peek_char (di) == 'S')
1509 module = d_substitution (di, 0);
1510 if (!module)
1511 return NULL;
1512 if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1513 || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1515 if (dc)
1516 return NULL;
1517 subst = 1;
1518 dc = module;
1519 module = NULL;
1523 /* FALLTHROUGH */
1525 case 'L':
1526 default:
1527 if (!subst)
1528 dc = d_unqualified_name (di, dc, module);
1529 if (d_peek_char (di) == 'I')
1531 /* This is <template-args>, which means that we just saw
1532 <unscoped-template-name>, which is a substitution
1533 candidate. */
1534 if (!subst && !d_add_substitution (di, dc))
1535 return NULL;
1536 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1537 d_template_args (di));
1538 subst = 0;
1540 break;
1542 if (substable && !subst && !d_add_substitution (di, dc))
1543 return NULL;
1544 return dc;
1547 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1548 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1551 static struct demangle_component *
1552 d_nested_name (struct d_info *di)
1554 struct demangle_component *ret;
1555 struct demangle_component **pret;
1556 struct demangle_component *rqual;
1558 if (! d_check_char (di, 'N'))
1559 return NULL;
1561 pret = d_cv_qualifiers (di, &ret, 1);
1562 if (pret == NULL)
1563 return NULL;
1565 /* Parse the ref-qualifier now and then attach it
1566 once we have something to attach it to. */
1567 rqual = d_ref_qualifier (di, NULL);
1569 *pret = d_prefix (di, 1);
1570 if (*pret == NULL)
1571 return NULL;
1573 if (rqual)
1575 d_left (rqual) = ret;
1576 ret = rqual;
1579 if (! d_check_char (di, 'E'))
1580 return NULL;
1582 return ret;
1585 /* <prefix> ::= <prefix> <unqualified-name>
1586 ::= <template-prefix> <template-args>
1587 ::= <template-param>
1588 ::= <decltype>
1590 ::= <substitution>
1592 <template-prefix> ::= <prefix> <(template) unqualified-name>
1593 ::= <template-param>
1594 ::= <substitution>
1596 SUBST is true if we should add substitutions (as normal), false
1597 if not (in an unresolved-name). */
1599 static struct demangle_component *
1600 d_prefix (struct d_info *di, int substable)
1602 struct demangle_component *ret = NULL;
1604 for (;;)
1606 char peek = d_peek_char (di);
1608 /* The older code accepts a <local-name> here, but I don't see
1609 that in the grammar. The older code does not accept a
1610 <template-param> here. */
1612 if (peek == 'D'
1613 && (d_peek_next_char (di) == 'T'
1614 || d_peek_next_char (di) == 't'))
1616 /* Decltype. */
1617 if (ret)
1618 return NULL;
1619 ret = cplus_demangle_type (di);
1621 else if (peek == 'I')
1623 if (ret == NULL)
1624 return NULL;
1625 struct demangle_component *dc = d_template_args (di);
1626 if (!dc)
1627 return NULL;
1628 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, dc);
1630 else if (peek == 'T')
1632 if (ret)
1633 return NULL;
1634 ret = d_template_param (di);
1636 else if (peek == 'M')
1638 /* Initializer scope for a lambda. We already added it as a
1639 substitution candidate, don't do that again. */
1640 d_advance (di, 1);
1641 continue;
1643 else
1645 struct demangle_component *module = NULL;
1646 if (peek == 'S')
1648 module = d_substitution (di, 1);
1649 if (!module)
1650 return NULL;
1651 if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1652 || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1654 if (ret)
1655 return NULL;
1656 ret = module;
1657 continue;
1660 ret = d_unqualified_name (di, ret, module);
1663 if (!ret)
1664 break;
1666 if (d_peek_char (di) == 'E')
1667 break;
1669 if (substable && !d_add_substitution (di, ret))
1670 return NULL;
1673 return ret;
1676 static int
1677 d_maybe_module_name (struct d_info *di, struct demangle_component **name)
1679 while (d_peek_char (di) == 'W')
1681 d_advance (di, 1);
1682 enum demangle_component_type code = DEMANGLE_COMPONENT_MODULE_NAME;
1683 if (d_peek_char (di) == 'P')
1685 code = DEMANGLE_COMPONENT_MODULE_PARTITION;
1686 d_advance (di, 1);
1689 *name = d_make_comp (di, code, *name, d_source_name (di));
1690 if (!*name)
1691 return 0;
1692 if (!d_add_substitution (di, *name))
1693 return 0;
1695 return 1;
1698 /* <unqualified-name> ::= [<module-name>] <operator-name> [<abi-tags>]
1699 ::= [<module-name>] <ctor-dtor-name> [<abi-tags>]
1700 ::= [<module-name>] <source-name> [<abi-tags>]
1701 ::= [<module-name>] <local-source-name> [<abi-tags>]
1702 ::= [<module-name>] DC <source-name>+ E [<abi-tags>]
1703 <local-source-name> ::= L <source-name> <discriminator> [<abi-tags>]
1706 static struct demangle_component *
1707 d_unqualified_name (struct d_info *di, struct demangle_component *scope,
1708 struct demangle_component *module)
1710 struct demangle_component *ret;
1711 char peek;
1713 if (!d_maybe_module_name (di, &module))
1714 return NULL;
1716 peek = d_peek_char (di);
1717 if (IS_DIGIT (peek))
1718 ret = d_source_name (di);
1719 else if (IS_LOWER (peek))
1721 int was_expr = di->is_expression;
1722 if (peek == 'o' && d_peek_next_char (di) == 'n')
1724 d_advance (di, 2);
1725 /* Treat cv as naming a conversion operator. */
1726 di->is_expression = 0;
1728 ret = d_operator_name (di);
1729 di->is_expression = was_expr;
1730 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1732 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1733 if (!strcmp (ret->u.s_operator.op->code, "li"))
1734 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1735 d_source_name (di));
1738 else if (peek == 'D' && d_peek_next_char (di) == 'C')
1740 // structured binding
1741 d_advance (di, 2);
1742 struct demangle_component *prev = NULL;
1745 struct demangle_component *next =
1746 d_make_comp (di, DEMANGLE_COMPONENT_STRUCTURED_BINDING,
1747 d_source_name (di), NULL);
1748 if (prev)
1749 d_right (prev) = next;
1750 else
1751 ret = next;
1752 prev = next;
1754 while (prev && d_peek_char (di) != 'E');
1755 if (prev)
1756 d_advance (di, 1);
1757 else
1758 ret = NULL;
1760 else if (peek == 'C' || peek == 'D')
1761 ret = d_ctor_dtor_name (di);
1762 else if (peek == 'L')
1764 d_advance (di, 1);
1766 ret = d_source_name (di);
1767 if (ret == NULL)
1768 return NULL;
1769 if (! d_discriminator (di))
1770 return NULL;
1772 else if (peek == 'U')
1774 switch (d_peek_next_char (di))
1776 case 'l':
1777 ret = d_lambda (di);
1778 break;
1779 case 't':
1780 ret = d_unnamed_type (di);
1781 break;
1782 default:
1783 return NULL;
1786 else
1787 return NULL;
1789 if (module)
1790 ret = d_make_comp (di, DEMANGLE_COMPONENT_MODULE_ENTITY, ret, module);
1791 if (d_peek_char (di) == 'B')
1792 ret = d_abi_tags (di, ret);
1793 if (scope)
1794 ret = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, scope, ret);
1796 return ret;
1799 /* <source-name> ::= <(positive length) number> <identifier> */
1801 static struct demangle_component *
1802 d_source_name (struct d_info *di)
1804 int len;
1805 struct demangle_component *ret;
1807 len = d_number (di);
1808 if (len <= 0)
1809 return NULL;
1810 ret = d_identifier (di, len);
1811 di->last_name = ret;
1812 return ret;
1815 /* number ::= [n] <(non-negative decimal integer)> */
1817 static int
1818 d_number (struct d_info *di)
1820 int negative;
1821 char peek;
1822 int ret;
1824 negative = 0;
1825 peek = d_peek_char (di);
1826 if (peek == 'n')
1828 negative = 1;
1829 d_advance (di, 1);
1830 peek = d_peek_char (di);
1833 ret = 0;
1834 while (1)
1836 if (! IS_DIGIT (peek))
1838 if (negative)
1839 ret = - ret;
1840 return ret;
1842 if (ret > ((INT_MAX - (peek - '0')) / 10))
1843 return -1;
1844 ret = ret * 10 + (peek - '0');
1845 d_advance (di, 1);
1846 peek = d_peek_char (di);
1850 /* Like d_number, but returns a demangle_component. */
1852 static struct demangle_component *
1853 d_number_component (struct d_info *di)
1855 struct demangle_component *ret = d_make_empty (di);
1856 if (ret)
1858 ret->type = DEMANGLE_COMPONENT_NUMBER;
1859 ret->u.s_number.number = d_number (di);
1861 return ret;
1864 /* identifier ::= <(unqualified source code identifier)> */
1866 static struct demangle_component *
1867 d_identifier (struct d_info *di, int len)
1869 const char *name;
1871 name = d_str (di);
1873 if (di->send - name < len)
1874 return NULL;
1876 d_advance (di, len);
1878 /* A Java mangled name may have a trailing '$' if it is a C++
1879 keyword. This '$' is not included in the length count. We just
1880 ignore the '$'. */
1881 if ((di->options & DMGL_JAVA) != 0
1882 && d_peek_char (di) == '$')
1883 d_advance (di, 1);
1885 /* Look for something which looks like a gcc encoding of an
1886 anonymous namespace, and replace it with a more user friendly
1887 name. */
1888 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1889 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1890 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1892 const char *s;
1894 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1895 if ((*s == '.' || *s == '_' || *s == '$')
1896 && s[1] == 'N')
1898 di->expansion -= len - sizeof "(anonymous namespace)";
1899 return d_make_name (di, "(anonymous namespace)",
1900 sizeof "(anonymous namespace)" - 1);
1904 return d_make_name (di, name, len);
1907 /* operator_name ::= many different two character encodings.
1908 ::= cv <type>
1909 ::= v <digit> <source-name>
1911 This list is sorted for binary search. */
1913 #define NL(s) s, (sizeof s) - 1
1915 CP_STATIC_IF_GLIBCPP_V3
1916 const struct demangle_operator_info cplus_demangle_operators[] =
1918 { "aN", NL ("&="), 2 },
1919 { "aS", NL ("="), 2 },
1920 { "aa", NL ("&&"), 2 },
1921 { "ad", NL ("&"), 1 },
1922 { "an", NL ("&"), 2 },
1923 { "at", NL ("alignof "), 1 },
1924 { "aw", NL ("co_await "), 1 },
1925 { "az", NL ("alignof "), 1 },
1926 { "cc", NL ("const_cast"), 2 },
1927 { "cl", NL ("()"), 2 },
1928 { "cm", NL (","), 2 },
1929 { "co", NL ("~"), 1 },
1930 { "dV", NL ("/="), 2 },
1931 { "dX", NL ("[...]="), 3 }, /* [expr...expr] = expr */
1932 { "da", NL ("delete[] "), 1 },
1933 { "dc", NL ("dynamic_cast"), 2 },
1934 { "de", NL ("*"), 1 },
1935 { "di", NL ("="), 2 }, /* .name = expr */
1936 { "dl", NL ("delete "), 1 },
1937 { "ds", NL (".*"), 2 },
1938 { "dt", NL ("."), 2 },
1939 { "dv", NL ("/"), 2 },
1940 { "dx", NL ("]="), 2 }, /* [expr] = expr */
1941 { "eO", NL ("^="), 2 },
1942 { "eo", NL ("^"), 2 },
1943 { "eq", NL ("=="), 2 },
1944 { "fL", NL ("..."), 3 },
1945 { "fR", NL ("..."), 3 },
1946 { "fl", NL ("..."), 2 },
1947 { "fr", NL ("..."), 2 },
1948 { "ge", NL (">="), 2 },
1949 { "gs", NL ("::"), 1 },
1950 { "gt", NL (">"), 2 },
1951 { "ix", NL ("[]"), 2 },
1952 { "lS", NL ("<<="), 2 },
1953 { "le", NL ("<="), 2 },
1954 { "li", NL ("operator\"\" "), 1 },
1955 { "ls", NL ("<<"), 2 },
1956 { "lt", NL ("<"), 2 },
1957 { "mI", NL ("-="), 2 },
1958 { "mL", NL ("*="), 2 },
1959 { "mi", NL ("-"), 2 },
1960 { "ml", NL ("*"), 2 },
1961 { "mm", NL ("--"), 1 },
1962 { "na", NL ("new[]"), 3 },
1963 { "ne", NL ("!="), 2 },
1964 { "ng", NL ("-"), 1 },
1965 { "nt", NL ("!"), 1 },
1966 { "nw", NL ("new"), 3 },
1967 { "oR", NL ("|="), 2 },
1968 { "oo", NL ("||"), 2 },
1969 { "or", NL ("|"), 2 },
1970 { "pL", NL ("+="), 2 },
1971 { "pl", NL ("+"), 2 },
1972 { "pm", NL ("->*"), 2 },
1973 { "pp", NL ("++"), 1 },
1974 { "ps", NL ("+"), 1 },
1975 { "pt", NL ("->"), 2 },
1976 { "qu", NL ("?"), 3 },
1977 { "rM", NL ("%="), 2 },
1978 { "rS", NL (">>="), 2 },
1979 { "rc", NL ("reinterpret_cast"), 2 },
1980 { "rm", NL ("%"), 2 },
1981 { "rs", NL (">>"), 2 },
1982 { "sP", NL ("sizeof..."), 1 },
1983 { "sZ", NL ("sizeof..."), 1 },
1984 { "sc", NL ("static_cast"), 2 },
1985 { "ss", NL ("<=>"), 2 },
1986 { "st", NL ("sizeof "), 1 },
1987 { "sz", NL ("sizeof "), 1 },
1988 { "tr", NL ("throw"), 0 },
1989 { "tw", NL ("throw "), 1 },
1990 { NULL, NULL, 0, 0 }
1993 static struct demangle_component *
1994 d_operator_name (struct d_info *di)
1996 char c1;
1997 char c2;
1999 c1 = d_next_char (di);
2000 c2 = d_next_char (di);
2001 if (c1 == 'v' && IS_DIGIT (c2))
2002 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
2003 else if (c1 == 'c' && c2 == 'v')
2005 struct demangle_component *type;
2006 int was_conversion = di->is_conversion;
2007 struct demangle_component *res;
2009 di->is_conversion = ! di->is_expression;
2010 type = cplus_demangle_type (di);
2011 if (di->is_conversion)
2012 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
2013 else
2014 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
2015 di->is_conversion = was_conversion;
2016 return res;
2018 else
2020 /* LOW is the inclusive lower bound. */
2021 int low = 0;
2022 /* HIGH is the exclusive upper bound. We subtract one to ignore
2023 the sentinel at the end of the array. */
2024 int high = ((sizeof (cplus_demangle_operators)
2025 / sizeof (cplus_demangle_operators[0]))
2026 - 1);
2028 while (1)
2030 int i;
2031 const struct demangle_operator_info *p;
2033 i = low + (high - low) / 2;
2034 p = cplus_demangle_operators + i;
2036 if (c1 == p->code[0] && c2 == p->code[1])
2037 return d_make_operator (di, p);
2039 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
2040 high = i;
2041 else
2042 low = i + 1;
2043 if (low == high)
2044 return NULL;
2049 static struct demangle_component *
2050 d_make_character (struct d_info *di, int c)
2052 struct demangle_component *p;
2053 p = d_make_empty (di);
2054 if (p != NULL)
2056 p->type = DEMANGLE_COMPONENT_CHARACTER;
2057 p->u.s_character.character = c;
2059 return p;
2062 static struct demangle_component *
2063 d_java_resource (struct d_info *di)
2065 struct demangle_component *p = NULL;
2066 struct demangle_component *next = NULL;
2067 int len, i;
2068 char c;
2069 const char *str;
2071 len = d_number (di);
2072 if (len <= 1)
2073 return NULL;
2075 /* Eat the leading '_'. */
2076 if (d_next_char (di) != '_')
2077 return NULL;
2078 len--;
2080 str = d_str (di);
2081 i = 0;
2083 while (len > 0)
2085 c = str[i];
2086 if (!c)
2087 return NULL;
2089 /* Each chunk is either a '$' escape... */
2090 if (c == '$')
2092 i++;
2093 switch (str[i++])
2095 case 'S':
2096 c = '/';
2097 break;
2098 case '_':
2099 c = '.';
2100 break;
2101 case '$':
2102 c = '$';
2103 break;
2104 default:
2105 return NULL;
2107 next = d_make_character (di, c);
2108 d_advance (di, i);
2109 str = d_str (di);
2110 len -= i;
2111 i = 0;
2112 if (next == NULL)
2113 return NULL;
2115 /* ... or a sequence of characters. */
2116 else
2118 while (i < len && str[i] && str[i] != '$')
2119 i++;
2121 next = d_make_name (di, str, i);
2122 d_advance (di, i);
2123 str = d_str (di);
2124 len -= i;
2125 i = 0;
2126 if (next == NULL)
2127 return NULL;
2130 if (p == NULL)
2131 p = next;
2132 else
2134 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
2135 if (p == NULL)
2136 return NULL;
2140 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
2142 return p;
2145 /* <special-name> ::= TV <type>
2146 ::= TT <type>
2147 ::= TI <type>
2148 ::= TS <type>
2149 ::= TA <template-arg>
2150 ::= GV <(object) name>
2151 ::= T <call-offset> <(base) encoding>
2152 ::= Tc <call-offset> <call-offset> <(base) encoding>
2153 Also g++ extensions:
2154 ::= TC <type> <(offset) number> _ <(base) type>
2155 ::= TF <type>
2156 ::= TJ <type>
2157 ::= GR <name>
2158 ::= GA <encoding>
2159 ::= Gr <resource name>
2160 ::= GTt <encoding>
2161 ::= GTn <encoding>
2164 static struct demangle_component *
2165 d_special_name (struct d_info *di)
2167 di->expansion += 20;
2168 if (d_check_char (di, 'T'))
2170 switch (d_next_char (di))
2172 case 'V':
2173 di->expansion -= 5;
2174 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2175 cplus_demangle_type (di), NULL);
2176 case 'T':
2177 di->expansion -= 10;
2178 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2179 cplus_demangle_type (di), NULL);
2180 case 'I':
2181 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2182 cplus_demangle_type (di), NULL);
2183 case 'S':
2184 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2185 cplus_demangle_type (di), NULL);
2187 case 'h':
2188 if (! d_call_offset (di, 'h'))
2189 return NULL;
2190 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2191 d_encoding (di, 0), NULL);
2193 case 'v':
2194 if (! d_call_offset (di, 'v'))
2195 return NULL;
2196 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2197 d_encoding (di, 0), NULL);
2199 case 'c':
2200 if (! d_call_offset (di, '\0'))
2201 return NULL;
2202 if (! d_call_offset (di, '\0'))
2203 return NULL;
2204 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2205 d_encoding (di, 0), NULL);
2207 case 'C':
2209 struct demangle_component *derived_type;
2210 int offset;
2211 struct demangle_component *base_type;
2213 derived_type = cplus_demangle_type (di);
2214 offset = d_number (di);
2215 if (offset < 0)
2216 return NULL;
2217 if (! d_check_char (di, '_'))
2218 return NULL;
2219 base_type = cplus_demangle_type (di);
2220 /* We don't display the offset. FIXME: We should display
2221 it in verbose mode. */
2222 di->expansion += 5;
2223 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2224 base_type, derived_type);
2227 case 'F':
2228 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2229 cplus_demangle_type (di), NULL);
2230 case 'J':
2231 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2232 cplus_demangle_type (di), NULL);
2234 case 'H':
2235 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2236 d_name (di, 0), NULL);
2238 case 'W':
2239 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2240 d_name (di, 0), NULL);
2242 case 'A':
2243 return d_make_comp (di, DEMANGLE_COMPONENT_TPARM_OBJ,
2244 d_template_arg (di), NULL);
2246 default:
2247 return NULL;
2250 else if (d_check_char (di, 'G'))
2252 switch (d_next_char (di))
2254 case 'V':
2255 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD,
2256 d_name (di, 0), NULL);
2258 case 'R':
2260 struct demangle_component *name = d_name (di, 0);
2261 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2262 d_number_component (di));
2265 case 'A':
2266 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2267 d_encoding (di, 0), NULL);
2269 case 'I':
2271 struct demangle_component *module = NULL;
2272 if (!d_maybe_module_name (di, &module) || !module)
2273 return NULL;
2274 return d_make_comp (di, DEMANGLE_COMPONENT_MODULE_INIT,
2275 module, NULL);
2277 case 'T':
2278 switch (d_next_char (di))
2280 case 'n':
2281 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2282 d_encoding (di, 0), NULL);
2283 default:
2284 /* ??? The proposal is that other letters (such as 'h') stand
2285 for different variants of transaction cloning, such as
2286 compiling directly for hardware transaction support. But
2287 they still should all be transactional clones of some sort
2288 so go ahead and call them that. */
2289 case 't':
2290 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2291 d_encoding (di, 0), NULL);
2294 case 'r':
2295 return d_java_resource (di);
2297 default:
2298 return NULL;
2301 else
2302 return NULL;
2305 /* <call-offset> ::= h <nv-offset> _
2306 ::= v <v-offset> _
2308 <nv-offset> ::= <(offset) number>
2310 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2312 The C parameter, if not '\0', is a character we just read which is
2313 the start of the <call-offset>.
2315 We don't display the offset information anywhere. FIXME: We should
2316 display it in verbose mode. */
2318 static int
2319 d_call_offset (struct d_info *di, int c)
2321 if (c == '\0')
2322 c = d_next_char (di);
2324 if (c == 'h')
2325 d_number (di);
2326 else if (c == 'v')
2328 d_number (di);
2329 if (! d_check_char (di, '_'))
2330 return 0;
2331 d_number (di);
2333 else
2334 return 0;
2336 if (! d_check_char (di, '_'))
2337 return 0;
2339 return 1;
2342 /* <ctor-dtor-name> ::= C1
2343 ::= C2
2344 ::= C3
2345 ::= D0
2346 ::= D1
2347 ::= D2
2350 static struct demangle_component *
2351 d_ctor_dtor_name (struct d_info *di)
2353 if (di->last_name != NULL)
2355 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2356 di->expansion += di->last_name->u.s_name.len;
2357 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2358 di->expansion += di->last_name->u.s_string.len;
2360 switch (d_peek_char (di))
2362 case 'C':
2364 enum gnu_v3_ctor_kinds kind;
2365 int inheriting = 0;
2367 if (d_peek_next_char (di) == 'I')
2369 inheriting = 1;
2370 d_advance (di, 1);
2373 switch (d_peek_next_char (di))
2375 case '1':
2376 kind = gnu_v3_complete_object_ctor;
2377 break;
2378 case '2':
2379 kind = gnu_v3_base_object_ctor;
2380 break;
2381 case '3':
2382 kind = gnu_v3_complete_object_allocating_ctor;
2383 break;
2384 case '4':
2385 kind = gnu_v3_unified_ctor;
2386 break;
2387 case '5':
2388 kind = gnu_v3_object_ctor_group;
2389 break;
2390 default:
2391 return NULL;
2394 d_advance (di, 2);
2396 if (inheriting)
2397 cplus_demangle_type (di);
2399 return d_make_ctor (di, kind, di->last_name);
2402 case 'D':
2404 enum gnu_v3_dtor_kinds kind;
2406 switch (d_peek_next_char (di))
2408 case '0':
2409 kind = gnu_v3_deleting_dtor;
2410 break;
2411 case '1':
2412 kind = gnu_v3_complete_object_dtor;
2413 break;
2414 case '2':
2415 kind = gnu_v3_base_object_dtor;
2416 break;
2417 /* digit '3' is not used */
2418 case '4':
2419 kind = gnu_v3_unified_dtor;
2420 break;
2421 case '5':
2422 kind = gnu_v3_object_dtor_group;
2423 break;
2424 default:
2425 return NULL;
2427 d_advance (di, 2);
2428 return d_make_dtor (di, kind, di->last_name);
2431 default:
2432 return NULL;
2436 /* True iff we're looking at an order-insensitive type-qualifier, including
2437 function-type-qualifiers. */
2439 static int
2440 next_is_type_qual (struct d_info *di)
2442 char peek = d_peek_char (di);
2443 if (peek == 'r' || peek == 'V' || peek == 'K')
2444 return 1;
2445 if (peek == 'D')
2447 peek = d_peek_next_char (di);
2448 if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2449 return 1;
2451 return 0;
2454 /* <type> ::= <builtin-type>
2455 ::= <function-type>
2456 ::= <class-enum-type>
2457 ::= <array-type>
2458 ::= <pointer-to-member-type>
2459 ::= <template-param>
2460 ::= <template-template-param> <template-args>
2461 ::= <substitution>
2462 ::= <CV-qualifiers> <type>
2463 ::= P <type>
2464 ::= R <type>
2465 ::= O <type> (C++0x)
2466 ::= C <type>
2467 ::= G <type>
2468 ::= U <source-name> <type>
2470 <builtin-type> ::= various one letter codes
2471 ::= u <source-name>
2474 CP_STATIC_IF_GLIBCPP_V3
2475 const struct demangle_builtin_type_info
2476 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2478 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2479 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2480 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2481 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2482 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2483 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2484 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2485 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2486 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2487 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2488 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2489 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2490 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2491 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2492 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2493 D_PRINT_DEFAULT },
2494 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2495 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2496 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2497 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2498 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2499 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2500 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2501 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2502 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2503 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2504 D_PRINT_UNSIGNED_LONG_LONG },
2505 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2506 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2507 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2508 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2509 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2510 /* 30 */ { NL ("char8_t"), NL ("char8_t"), D_PRINT_DEFAULT },
2511 /* 31 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2512 /* 32 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2513 /* 33 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2514 D_PRINT_DEFAULT },
2515 /* 34 */ { NL ("_Float"), NL ("_Float"), D_PRINT_FLOAT },
2516 /* 35 */ { NL ("std::bfloat16_t"), NL ("std::bfloat16_t"), D_PRINT_FLOAT },
2519 CP_STATIC_IF_GLIBCPP_V3
2520 struct demangle_component *
2521 cplus_demangle_type (struct d_info *di)
2523 char peek;
2524 struct demangle_component *ret = NULL;
2525 int can_subst;
2527 /* The ABI specifies that when CV-qualifiers are used, the base type
2528 is substitutable, and the fully qualified type is substitutable,
2529 but the base type with a strict subset of the CV-qualifiers is
2530 not substitutable. The natural recursive implementation of the
2531 CV-qualifiers would cause subsets to be substitutable, so instead
2532 we pull them all off now.
2534 FIXME: The ABI says that order-insensitive vendor qualifiers
2535 should be handled in the same way, but we have no way to tell
2536 which vendor qualifiers are order-insensitive and which are
2537 order-sensitive. So we just assume that they are all
2538 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2539 __vector, and it treats it as order-sensitive when mangling
2540 names. */
2542 if (next_is_type_qual (di))
2544 struct demangle_component **pret;
2546 pret = d_cv_qualifiers (di, &ret, 0);
2547 if (pret == NULL)
2548 return NULL;
2549 if (d_peek_char (di) == 'F')
2551 /* cv-qualifiers before a function type apply to 'this',
2552 so avoid adding the unqualified function type to
2553 the substitution list. */
2554 *pret = d_function_type (di);
2556 else
2557 *pret = cplus_demangle_type (di);
2558 if (!*pret)
2559 return NULL;
2560 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2561 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2563 /* Move the ref-qualifier outside the cv-qualifiers so that
2564 they are printed in the right order. */
2565 struct demangle_component *fn = d_left (*pret);
2566 d_left (*pret) = ret;
2567 ret = *pret;
2568 *pret = fn;
2570 if (! d_add_substitution (di, ret))
2571 return NULL;
2572 return ret;
2575 can_subst = 1;
2577 peek = d_peek_char (di);
2578 switch (peek)
2580 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2581 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2582 case 'o': case 's': case 't':
2583 case 'v': case 'w': case 'x': case 'y': case 'z':
2584 ret = d_make_builtin_type (di,
2585 &cplus_demangle_builtin_types[peek - 'a']);
2586 di->expansion += ret->u.s_builtin.type->len;
2587 can_subst = 0;
2588 d_advance (di, 1);
2589 break;
2591 case 'u':
2592 d_advance (di, 1);
2593 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2594 d_source_name (di), NULL);
2595 break;
2597 case 'F':
2598 ret = d_function_type (di);
2599 break;
2601 case 'A':
2602 ret = d_array_type (di);
2603 break;
2605 case 'M':
2606 ret = d_pointer_to_member_type (di);
2607 break;
2609 case 'T':
2610 ret = d_template_param (di);
2611 if (d_peek_char (di) == 'I')
2613 /* This may be <template-template-param> <template-args>.
2614 If this is the type for a conversion operator, we can
2615 have a <template-template-param> here only by following
2616 a derivation like this:
2618 <nested-name>
2619 -> <template-prefix> <template-args>
2620 -> <prefix> <template-unqualified-name> <template-args>
2621 -> <unqualified-name> <template-unqualified-name> <template-args>
2622 -> <source-name> <template-unqualified-name> <template-args>
2623 -> <source-name> <operator-name> <template-args>
2624 -> <source-name> cv <type> <template-args>
2625 -> <source-name> cv <template-template-param> <template-args> <template-args>
2627 where the <template-args> is followed by another.
2628 Otherwise, we must have a derivation like this:
2630 <nested-name>
2631 -> <template-prefix> <template-args>
2632 -> <prefix> <template-unqualified-name> <template-args>
2633 -> <unqualified-name> <template-unqualified-name> <template-args>
2634 -> <source-name> <template-unqualified-name> <template-args>
2635 -> <source-name> <operator-name> <template-args>
2636 -> <source-name> cv <type> <template-args>
2637 -> <source-name> cv <template-param> <template-args>
2639 where we need to leave the <template-args> to be processed
2640 by d_prefix (following the <template-prefix>).
2642 The <template-template-param> part is a substitution
2643 candidate. */
2644 if (! di->is_conversion)
2646 if (! d_add_substitution (di, ret))
2647 return NULL;
2648 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2649 d_template_args (di));
2651 else
2653 struct demangle_component *args;
2654 struct d_info_checkpoint checkpoint;
2656 d_checkpoint (di, &checkpoint);
2657 args = d_template_args (di);
2658 if (d_peek_char (di) == 'I')
2660 if (! d_add_substitution (di, ret))
2661 return NULL;
2662 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2663 args);
2665 else
2666 d_backtrack (di, &checkpoint);
2669 break;
2671 case 'O':
2672 d_advance (di, 1);
2673 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2674 cplus_demangle_type (di), NULL);
2675 break;
2677 case 'P':
2678 d_advance (di, 1);
2679 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2680 cplus_demangle_type (di), NULL);
2681 break;
2683 case 'R':
2684 d_advance (di, 1);
2685 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2686 cplus_demangle_type (di), NULL);
2687 break;
2689 case 'C':
2690 d_advance (di, 1);
2691 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2692 cplus_demangle_type (di), NULL);
2693 break;
2695 case 'G':
2696 d_advance (di, 1);
2697 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2698 cplus_demangle_type (di), NULL);
2699 break;
2701 case 'U':
2702 d_advance (di, 1);
2703 ret = d_source_name (di);
2704 if (d_peek_char (di) == 'I')
2705 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2706 d_template_args (di));
2707 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2708 cplus_demangle_type (di), ret);
2709 break;
2711 case 'D':
2712 can_subst = 0;
2713 d_advance (di, 1);
2714 peek = d_next_char (di);
2715 switch (peek)
2717 case 'T':
2718 case 't':
2719 /* decltype (expression) */
2720 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2721 d_expression (di), NULL);
2722 if (ret && d_next_char (di) != 'E')
2723 ret = NULL;
2724 can_subst = 1;
2725 break;
2727 case 'p':
2728 /* Pack expansion. */
2729 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2730 cplus_demangle_type (di), NULL);
2731 can_subst = 1;
2732 break;
2734 case 'a':
2735 /* auto */
2736 ret = d_make_name (di, "auto", 4);
2737 break;
2738 case 'c':
2739 /* decltype(auto) */
2740 ret = d_make_name (di, "decltype(auto)", 14);
2741 break;
2743 case 'f':
2744 /* 32-bit decimal floating point */
2745 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2746 di->expansion += ret->u.s_builtin.type->len;
2747 break;
2748 case 'd':
2749 /* 64-bit DFP */
2750 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2751 di->expansion += ret->u.s_builtin.type->len;
2752 break;
2753 case 'e':
2754 /* 128-bit DFP */
2755 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2756 di->expansion += ret->u.s_builtin.type->len;
2757 break;
2758 case 'h':
2759 /* 16-bit half-precision FP */
2760 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2761 di->expansion += ret->u.s_builtin.type->len;
2762 break;
2763 case 'u':
2764 /* char8_t */
2765 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2766 di->expansion += ret->u.s_builtin.type->len;
2767 break;
2768 case 's':
2769 /* char16_t */
2770 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2771 di->expansion += ret->u.s_builtin.type->len;
2772 break;
2773 case 'i':
2774 /* char32_t */
2775 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2776 di->expansion += ret->u.s_builtin.type->len;
2777 break;
2779 case 'F':
2780 /* DF<number>_ - _Float<number>.
2781 DF<number>x - _Float<number>x
2782 DF16b - std::bfloat16_t. */
2784 int arg = d_number (di);
2785 char buf[12];
2786 char suffix = 0;
2787 if (d_peek_char (di) == 'b')
2789 if (arg != 16)
2790 return NULL;
2791 d_advance (di, 1);
2792 ret = d_make_builtin_type (di,
2793 &cplus_demangle_builtin_types[35]);
2794 di->expansion += ret->u.s_builtin.type->len;
2795 break;
2797 if (d_peek_char (di) == 'x')
2798 suffix = 'x';
2799 if (!suffix && d_peek_char (di) != '_')
2800 return NULL;
2802 = d_make_extended_builtin_type (di,
2803 &cplus_demangle_builtin_types[34],
2804 arg, suffix);
2805 d_advance (di, 1);
2806 sprintf (buf, "%d", arg);
2807 di->expansion += ret->u.s_extended_builtin.type->len
2808 + strlen (buf) + (suffix != 0);
2809 break;
2812 case 'v':
2813 ret = d_vector_type (di);
2814 can_subst = 1;
2815 break;
2817 case 'n':
2818 /* decltype(nullptr) */
2819 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[33]);
2820 di->expansion += ret->u.s_builtin.type->len;
2821 break;
2823 default:
2824 return NULL;
2826 break;
2828 default:
2829 return d_class_enum_type (di, 1);
2832 if (can_subst)
2834 if (! d_add_substitution (di, ret))
2835 return NULL;
2838 return ret;
2841 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2843 static struct demangle_component **
2844 d_cv_qualifiers (struct d_info *di,
2845 struct demangle_component **pret, int member_fn)
2847 struct demangle_component **pstart;
2848 char peek;
2850 pstart = pret;
2851 peek = d_peek_char (di);
2852 while (next_is_type_qual (di))
2854 enum demangle_component_type t;
2855 struct demangle_component *right = NULL;
2857 d_advance (di, 1);
2858 if (peek == 'r')
2860 t = (member_fn
2861 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2862 : DEMANGLE_COMPONENT_RESTRICT);
2863 di->expansion += sizeof "restrict";
2865 else if (peek == 'V')
2867 t = (member_fn
2868 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2869 : DEMANGLE_COMPONENT_VOLATILE);
2870 di->expansion += sizeof "volatile";
2872 else if (peek == 'K')
2874 t = (member_fn
2875 ? DEMANGLE_COMPONENT_CONST_THIS
2876 : DEMANGLE_COMPONENT_CONST);
2877 di->expansion += sizeof "const";
2879 else
2881 peek = d_next_char (di);
2882 if (peek == 'x')
2884 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2885 di->expansion += sizeof "transaction_safe";
2887 else if (peek == 'o'
2888 || peek == 'O')
2890 t = DEMANGLE_COMPONENT_NOEXCEPT;
2891 di->expansion += sizeof "noexcept";
2892 if (peek == 'O')
2894 right = d_expression (di);
2895 if (right == NULL)
2896 return NULL;
2897 if (! d_check_char (di, 'E'))
2898 return NULL;
2901 else if (peek == 'w')
2903 t = DEMANGLE_COMPONENT_THROW_SPEC;
2904 di->expansion += sizeof "throw";
2905 right = d_parmlist (di);
2906 if (right == NULL)
2907 return NULL;
2908 if (! d_check_char (di, 'E'))
2909 return NULL;
2911 else
2912 return NULL;
2915 *pret = d_make_comp (di, t, NULL, right);
2916 if (*pret == NULL)
2917 return NULL;
2918 pret = &d_left (*pret);
2920 peek = d_peek_char (di);
2923 if (!member_fn && peek == 'F')
2925 while (pstart != pret)
2927 switch ((*pstart)->type)
2929 case DEMANGLE_COMPONENT_RESTRICT:
2930 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2931 break;
2932 case DEMANGLE_COMPONENT_VOLATILE:
2933 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2934 break;
2935 case DEMANGLE_COMPONENT_CONST:
2936 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2937 break;
2938 default:
2939 break;
2941 pstart = &d_left (*pstart);
2945 return pret;
2948 /* <ref-qualifier> ::= R
2949 ::= O */
2951 static struct demangle_component *
2952 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2954 struct demangle_component *ret = sub;
2955 char peek;
2957 peek = d_peek_char (di);
2958 if (peek == 'R' || peek == 'O')
2960 enum demangle_component_type t;
2961 if (peek == 'R')
2963 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2964 di->expansion += sizeof "&";
2966 else
2968 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2969 di->expansion += sizeof "&&";
2971 d_advance (di, 1);
2973 ret = d_make_comp (di, t, ret, NULL);
2976 return ret;
2979 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2981 static struct demangle_component *
2982 d_function_type (struct d_info *di)
2984 struct demangle_component *ret = NULL;
2986 if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
2988 if (di->recursion_level > DEMANGLE_RECURSION_LIMIT)
2989 /* FIXME: There ought to be a way to report
2990 that the recursion limit has been reached. */
2991 return NULL;
2993 di->recursion_level ++;
2996 if (d_check_char (di, 'F'))
2998 if (d_peek_char (di) == 'Y')
3000 /* Function has C linkage. We don't print this information.
3001 FIXME: We should print it in verbose mode. */
3002 d_advance (di, 1);
3004 ret = d_bare_function_type (di, 1);
3005 ret = d_ref_qualifier (di, ret);
3007 if (! d_check_char (di, 'E'))
3008 ret = NULL;
3011 if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
3012 di->recursion_level --;
3013 return ret;
3016 /* <type>+ */
3018 static struct demangle_component *
3019 d_parmlist (struct d_info *di)
3021 struct demangle_component *tl;
3022 struct demangle_component **ptl;
3024 tl = NULL;
3025 ptl = &tl;
3026 while (1)
3028 struct demangle_component *type;
3030 char peek = d_peek_char (di);
3031 if (peek == '\0' || peek == 'E' || peek == '.')
3032 break;
3033 if ((peek == 'R' || peek == 'O')
3034 && d_peek_next_char (di) == 'E')
3035 /* Function ref-qualifier, not a ref prefix for a parameter type. */
3036 break;
3037 type = cplus_demangle_type (di);
3038 if (type == NULL)
3039 return NULL;
3040 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
3041 if (*ptl == NULL)
3042 return NULL;
3043 ptl = &d_right (*ptl);
3046 /* There should be at least one parameter type besides the optional
3047 return type. A function which takes no arguments will have a
3048 single parameter type void. */
3049 if (tl == NULL)
3050 return NULL;
3052 /* If we have a single parameter type void, omit it. */
3053 if (d_right (tl) == NULL
3054 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3055 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
3057 di->expansion -= d_left (tl)->u.s_builtin.type->len;
3058 d_left (tl) = NULL;
3061 return tl;
3064 /* <bare-function-type> ::= [J]<type>+ */
3066 static struct demangle_component *
3067 d_bare_function_type (struct d_info *di, int has_return_type)
3069 struct demangle_component *return_type;
3070 struct demangle_component *tl;
3071 char peek;
3073 /* Detect special qualifier indicating that the first argument
3074 is the return type. */
3075 peek = d_peek_char (di);
3076 if (peek == 'J')
3078 d_advance (di, 1);
3079 has_return_type = 1;
3082 if (has_return_type)
3084 return_type = cplus_demangle_type (di);
3085 if (return_type == NULL)
3086 return NULL;
3088 else
3089 return_type = NULL;
3091 tl = d_parmlist (di);
3092 if (tl == NULL)
3093 return NULL;
3095 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
3096 return_type, tl);
3099 /* <class-enum-type> ::= <name> */
3101 static struct demangle_component *
3102 d_class_enum_type (struct d_info *di, int substable)
3104 return d_name (di, substable);
3107 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
3108 ::= A [<(dimension) expression>] _ <(element) type>
3111 static struct demangle_component *
3112 d_array_type (struct d_info *di)
3114 char peek;
3115 struct demangle_component *dim;
3117 if (! d_check_char (di, 'A'))
3118 return NULL;
3120 peek = d_peek_char (di);
3121 if (peek == '_')
3122 dim = NULL;
3123 else if (IS_DIGIT (peek))
3125 const char *s;
3127 s = d_str (di);
3130 d_advance (di, 1);
3131 peek = d_peek_char (di);
3133 while (IS_DIGIT (peek));
3134 dim = d_make_name (di, s, d_str (di) - s);
3135 if (dim == NULL)
3136 return NULL;
3138 else
3140 dim = d_expression (di);
3141 if (dim == NULL)
3142 return NULL;
3145 if (! d_check_char (di, '_'))
3146 return NULL;
3148 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
3149 cplus_demangle_type (di));
3152 /* <vector-type> ::= Dv <number> _ <type>
3153 ::= Dv _ <expression> _ <type> */
3155 static struct demangle_component *
3156 d_vector_type (struct d_info *di)
3158 char peek;
3159 struct demangle_component *dim;
3161 peek = d_peek_char (di);
3162 if (peek == '_')
3164 d_advance (di, 1);
3165 dim = d_expression (di);
3167 else
3168 dim = d_number_component (di);
3170 if (dim == NULL)
3171 return NULL;
3173 if (! d_check_char (di, '_'))
3174 return NULL;
3176 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
3177 cplus_demangle_type (di));
3180 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
3182 static struct demangle_component *
3183 d_pointer_to_member_type (struct d_info *di)
3185 struct demangle_component *cl;
3186 struct demangle_component *mem;
3188 if (! d_check_char (di, 'M'))
3189 return NULL;
3191 cl = cplus_demangle_type (di);
3192 if (cl == NULL)
3193 return NULL;
3195 /* The ABI says, "The type of a non-static member function is considered
3196 to be different, for the purposes of substitution, from the type of a
3197 namespace-scope or static member function whose type appears
3198 similar. The types of two non-static member functions are considered
3199 to be different, for the purposes of substitution, if the functions
3200 are members of different classes. In other words, for the purposes of
3201 substitution, the class of which the function is a member is
3202 considered part of the type of function."
3204 For a pointer to member function, this call to cplus_demangle_type
3205 will end up adding a (possibly qualified) non-member function type to
3206 the substitution table, which is not correct; however, the member
3207 function type will never be used in a substitution, so putting the
3208 wrong type in the substitution table is harmless. */
3210 mem = cplus_demangle_type (di);
3211 if (mem == NULL)
3212 return NULL;
3214 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
3217 /* <non-negative number> _ */
3219 static int
3220 d_compact_number (struct d_info *di)
3222 int num;
3223 if (d_peek_char (di) == '_')
3224 num = 0;
3225 else if (d_peek_char (di) == 'n')
3226 return -1;
3227 else
3228 num = d_number (di) + 1;
3230 if (num < 0 || ! d_check_char (di, '_'))
3231 return -1;
3232 return num;
3235 /* <template-param> ::= T_
3236 ::= T <(parameter-2 non-negative) number> _
3239 static struct demangle_component *
3240 d_template_param (struct d_info *di)
3242 int param;
3244 if (! d_check_char (di, 'T'))
3245 return NULL;
3247 param = d_compact_number (di);
3248 if (param < 0)
3249 return NULL;
3251 return d_make_template_param (di, param);
3254 /* <template-args> ::= I <template-arg>+ E */
3256 static struct demangle_component *
3257 d_template_args (struct d_info *di)
3259 if (d_peek_char (di) != 'I'
3260 && d_peek_char (di) != 'J')
3261 return NULL;
3262 d_advance (di, 1);
3264 return d_template_args_1 (di);
3267 /* <template-arg>* E */
3269 static struct demangle_component *
3270 d_template_args_1 (struct d_info *di)
3272 struct demangle_component *hold_last_name;
3273 struct demangle_component *al;
3274 struct demangle_component **pal;
3276 /* Preserve the last name we saw--don't let the template arguments
3277 clobber it, as that would give us the wrong name for a subsequent
3278 constructor or destructor. */
3279 hold_last_name = di->last_name;
3281 if (d_peek_char (di) == 'E')
3283 /* An argument pack can be empty. */
3284 d_advance (di, 1);
3285 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3288 al = NULL;
3289 pal = &al;
3290 while (1)
3292 struct demangle_component *a;
3294 a = d_template_arg (di);
3295 if (a == NULL)
3296 return NULL;
3298 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3299 if (*pal == NULL)
3300 return NULL;
3301 pal = &d_right (*pal);
3303 if (d_peek_char (di) == 'E')
3305 d_advance (di, 1);
3306 break;
3310 di->last_name = hold_last_name;
3312 return al;
3315 /* <template-arg> ::= <type>
3316 ::= X <expression> E
3317 ::= <expr-primary>
3320 static struct demangle_component *
3321 d_template_arg (struct d_info *di)
3323 struct demangle_component *ret;
3325 switch (d_peek_char (di))
3327 case 'X':
3328 d_advance (di, 1);
3329 ret = d_expression (di);
3330 if (! d_check_char (di, 'E'))
3331 return NULL;
3332 return ret;
3334 case 'L':
3335 return d_expr_primary (di);
3337 case 'I':
3338 case 'J':
3339 /* An argument pack. */
3340 return d_template_args (di);
3342 default:
3343 return cplus_demangle_type (di);
3347 /* Parse a sequence of expressions until we hit the terminator
3348 character. */
3350 static struct demangle_component *
3351 d_exprlist (struct d_info *di, char terminator)
3353 struct demangle_component *list = NULL;
3354 struct demangle_component **p = &list;
3356 if (d_peek_char (di) == terminator)
3358 d_advance (di, 1);
3359 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3362 while (1)
3364 struct demangle_component *arg = d_expression (di);
3365 if (arg == NULL)
3366 return NULL;
3368 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3369 if (*p == NULL)
3370 return NULL;
3371 p = &d_right (*p);
3373 if (d_peek_char (di) == terminator)
3375 d_advance (di, 1);
3376 break;
3380 return list;
3383 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3384 dynamic_cast, static_cast or reinterpret_cast. */
3386 static int
3387 op_is_new_cast (struct demangle_component *op)
3389 const char *code = op->u.s_operator.op->code;
3390 return (code[1] == 'c'
3391 && (code[0] == 's' || code[0] == 'd'
3392 || code[0] == 'c' || code[0] == 'r'));
3395 /* <unresolved-name> ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3396 ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3397 # T::N::x /decltype(p)::N::x
3398 ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3399 # A::x, N::y, A<T>::z; "gs" means leading "::"
3400 ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3402 "gs" is handled elsewhere, as a unary operator. */
3404 static struct demangle_component *
3405 d_unresolved_name (struct d_info *di)
3407 struct demangle_component *type;
3408 struct demangle_component *name;
3409 char peek;
3411 /* Consume the "sr". */
3412 d_advance (di, 2);
3414 peek = d_peek_char (di);
3415 if (di->unresolved_name_state
3416 && (IS_DIGIT (peek)
3417 || IS_LOWER (peek)
3418 || peek == 'C'
3419 || peek == 'U'
3420 || peek == 'L'))
3422 /* The third production is ambiguous with the old unresolved-name syntax
3423 of <type> <base-unresolved-name>; in the old mangling, A::x was mangled
3424 as sr1A1x, now sr1AE1x. So we first try to demangle using the new
3425 mangling, then with the old if that fails. */
3426 di->unresolved_name_state = -1;
3427 type = d_prefix (di, 0);
3428 if (d_peek_char (di) == 'E')
3429 d_advance (di, 1);
3431 else
3432 type = cplus_demangle_type (di);
3433 name = d_unqualified_name (di, type, NULL);
3434 if (d_peek_char (di) == 'I')
3435 name = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3436 d_template_args (di));
3437 return name;
3440 /* <expression> ::= <(unary) operator-name> <expression>
3441 ::= <(binary) operator-name> <expression> <expression>
3442 ::= <(trinary) operator-name> <expression> <expression> <expression>
3443 ::= cl <expression>+ E
3444 ::= st <type>
3445 ::= <template-param>
3446 ::= u <source-name> <template-arg>* E # vendor extended expression
3447 ::= <unresolved-name>
3448 ::= <expr-primary>
3450 <braced-expression> ::= <expression>
3451 ::= di <field source-name> <braced-expression> # .name = expr
3452 ::= dx <index expression> <braced-expression> # [expr] = expr
3453 ::= dX <range begin expression> <range end expression> <braced-expression>
3454 # [expr ... expr] = expr
3457 static struct demangle_component *
3458 d_expression_1 (struct d_info *di)
3460 char peek;
3462 peek = d_peek_char (di);
3463 if (peek == 'L')
3464 return d_expr_primary (di);
3465 else if (peek == 'T')
3466 return d_template_param (di);
3467 else if (peek == 's' && d_peek_next_char (di) == 'r')
3468 return d_unresolved_name (di);
3469 else if (peek == 's' && d_peek_next_char (di) == 'p')
3471 d_advance (di, 2);
3472 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3473 d_expression_1 (di), NULL);
3475 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3477 /* Function parameter used in a late-specified return type. */
3478 int index;
3479 d_advance (di, 2);
3480 if (d_peek_char (di) == 'T')
3482 /* 'this' parameter. */
3483 d_advance (di, 1);
3484 index = 0;
3486 else
3488 index = d_compact_number (di);
3489 if (index == INT_MAX || index == -1)
3490 return NULL;
3491 index++;
3493 return d_make_function_param (di, index);
3495 else if (IS_DIGIT (peek)
3496 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3498 /* We can get an unqualified name as an expression in the case of
3499 a dependent function call, i.e. decltype(f(t)). */
3500 struct demangle_component *name;
3502 if (peek == 'o')
3503 /* operator-function-id, i.e. operator+(t). */
3504 d_advance (di, 2);
3506 name = d_unqualified_name (di, NULL, NULL);
3507 if (name == NULL)
3508 return NULL;
3509 if (d_peek_char (di) == 'I')
3510 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3511 d_template_args (di));
3512 else
3513 return name;
3515 else if ((peek == 'i' || peek == 't')
3516 && d_peek_next_char (di) == 'l')
3518 /* Brace-enclosed initializer list, untyped or typed. */
3519 struct demangle_component *type = NULL;
3520 d_advance (di, 2);
3521 if (peek == 't')
3522 type = cplus_demangle_type (di);
3523 if (!d_peek_char (di) || !d_peek_next_char (di))
3524 return NULL;
3525 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3526 type, d_exprlist (di, 'E'));
3528 else if (peek == 'u')
3530 /* A vendor extended expression. */
3531 struct demangle_component *name, *args;
3532 d_advance (di, 1);
3533 name = d_source_name (di);
3534 args = d_template_args_1 (di);
3535 return d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_EXPR, name, args);
3537 else
3539 struct demangle_component *op;
3540 const char *code = NULL;
3541 int args;
3543 op = d_operator_name (di);
3544 if (op == NULL)
3545 return NULL;
3547 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3549 code = op->u.s_operator.op->code;
3550 di->expansion += op->u.s_operator.op->len - 2;
3551 if (strcmp (code, "st") == 0)
3552 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3553 cplus_demangle_type (di));
3556 switch (op->type)
3558 default:
3559 return NULL;
3560 case DEMANGLE_COMPONENT_OPERATOR:
3561 args = op->u.s_operator.op->args;
3562 break;
3563 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3564 args = op->u.s_extended_operator.args;
3565 break;
3566 case DEMANGLE_COMPONENT_CAST:
3567 args = 1;
3568 break;
3571 switch (args)
3573 case 0:
3574 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3576 case 1:
3578 struct demangle_component *operand;
3579 int suffix = 0;
3581 if (code && (code[0] == 'p' || code[0] == 'm')
3582 && code[1] == code[0])
3583 /* pp_ and mm_ are the prefix variants. */
3584 suffix = !d_check_char (di, '_');
3586 if (op->type == DEMANGLE_COMPONENT_CAST
3587 && d_check_char (di, '_'))
3588 operand = d_exprlist (di, 'E');
3589 else if (code && !strcmp (code, "sP"))
3590 operand = d_template_args_1 (di);
3591 else
3592 operand = d_expression_1 (di);
3594 if (suffix)
3595 /* Indicate the suffix variant for d_print_comp. */
3596 operand = d_make_comp (di, DEMANGLE_COMPONENT_BINARY_ARGS,
3597 operand, operand);
3599 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, operand);
3601 case 2:
3603 struct demangle_component *left;
3604 struct demangle_component *right;
3606 if (code == NULL)
3607 return NULL;
3608 if (op_is_new_cast (op))
3609 left = cplus_demangle_type (di);
3610 else if (code[0] == 'f')
3611 /* fold-expression. */
3612 left = d_operator_name (di);
3613 else if (!strcmp (code, "di"))
3614 left = d_unqualified_name (di, NULL, NULL);
3615 else
3616 left = d_expression_1 (di);
3617 if (!strcmp (code, "cl"))
3618 right = d_exprlist (di, 'E');
3619 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3621 peek = d_peek_char (di);
3622 /* These codes start a qualified name. */
3623 if ((peek == 'g' && d_peek_next_char (di) == 's')
3624 || (peek == 's' && d_peek_next_char (di) == 'r'))
3625 right = d_expression_1 (di);
3626 else
3628 /* Otherwise it's an unqualified name. We use
3629 d_unqualified_name rather than d_expression_1 here for
3630 old mangled names that didn't add 'on' before operator
3631 names. */
3632 right = d_unqualified_name (di, NULL, NULL);
3633 if (d_peek_char (di) == 'I')
3634 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3635 right, d_template_args (di));
3638 else
3639 right = d_expression_1 (di);
3641 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3642 d_make_comp (di,
3643 DEMANGLE_COMPONENT_BINARY_ARGS,
3644 left, right));
3646 case 3:
3648 struct demangle_component *first;
3649 struct demangle_component *second;
3650 struct demangle_component *third;
3652 if (code == NULL)
3653 return NULL;
3654 else if (!strcmp (code, "qu")
3655 || !strcmp (code, "dX"))
3657 /* ?: expression. */
3658 first = d_expression_1 (di);
3659 second = d_expression_1 (di);
3660 third = d_expression_1 (di);
3661 if (third == NULL)
3662 return NULL;
3664 else if (code[0] == 'f')
3666 /* fold-expression. */
3667 first = d_operator_name (di);
3668 second = d_expression_1 (di);
3669 third = d_expression_1 (di);
3670 if (third == NULL)
3671 return NULL;
3673 else if (code[0] == 'n')
3675 /* new-expression. */
3676 if (code[1] != 'w' && code[1] != 'a')
3677 return NULL;
3678 first = d_exprlist (di, '_');
3679 second = cplus_demangle_type (di);
3680 if (d_peek_char (di) == 'E')
3682 d_advance (di, 1);
3683 third = NULL;
3685 else if (d_peek_char (di) == 'p'
3686 && d_peek_next_char (di) == 'i')
3688 /* Parenthesized initializer. */
3689 d_advance (di, 2);
3690 third = d_exprlist (di, 'E');
3692 else if (d_peek_char (di) == 'i'
3693 && d_peek_next_char (di) == 'l')
3694 /* initializer-list. */
3695 third = d_expression_1 (di);
3696 else
3697 return NULL;
3699 else
3700 return NULL;
3701 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3702 d_make_comp (di,
3703 DEMANGLE_COMPONENT_TRINARY_ARG1,
3704 first,
3705 d_make_comp (di,
3706 DEMANGLE_COMPONENT_TRINARY_ARG2,
3707 second, third)));
3709 default:
3710 return NULL;
3715 static struct demangle_component *
3716 d_expression (struct d_info *di)
3718 struct demangle_component *ret;
3719 int was_expression = di->is_expression;
3721 di->is_expression = 1;
3722 ret = d_expression_1 (di);
3723 di->is_expression = was_expression;
3724 return ret;
3727 /* <expr-primary> ::= L <type> <(value) number> E
3728 ::= L <type> <(value) float> E
3729 ::= L <mangled-name> E
3732 static struct demangle_component *
3733 d_expr_primary (struct d_info *di)
3735 struct demangle_component *ret;
3737 if (! d_check_char (di, 'L'))
3738 return NULL;
3739 if (d_peek_char (di) == '_'
3740 /* Workaround for G++ bug; see comment in write_template_arg. */
3741 || d_peek_char (di) == 'Z')
3742 ret = cplus_demangle_mangled_name (di, 0);
3743 else
3745 struct demangle_component *type;
3746 enum demangle_component_type t;
3747 const char *s;
3749 type = cplus_demangle_type (di);
3750 if (type == NULL)
3751 return NULL;
3753 /* If we have a type we know how to print, we aren't going to
3754 print the type name itself. */
3755 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3756 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3757 di->expansion -= type->u.s_builtin.type->len;
3759 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3760 && strcmp (type->u.s_builtin.type->name,
3761 cplus_demangle_builtin_types[33].name) == 0)
3763 if (d_peek_char (di) == 'E')
3765 d_advance (di, 1);
3766 return type;
3770 /* Rather than try to interpret the literal value, we just
3771 collect it as a string. Note that it's possible to have a
3772 floating point literal here. The ABI specifies that the
3773 format of such literals is machine independent. That's fine,
3774 but what's not fine is that versions of g++ up to 3.2 with
3775 -fabi-version=1 used upper case letters in the hex constant,
3776 and dumped out gcc's internal representation. That makes it
3777 hard to tell where the constant ends, and hard to dump the
3778 constant in any readable form anyhow. We don't attempt to
3779 handle these cases. */
3781 t = DEMANGLE_COMPONENT_LITERAL;
3782 if (d_peek_char (di) == 'n')
3784 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3785 d_advance (di, 1);
3787 s = d_str (di);
3788 while (d_peek_char (di) != 'E')
3790 if (d_peek_char (di) == '\0')
3791 return NULL;
3792 d_advance (di, 1);
3794 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3796 if (! d_check_char (di, 'E'))
3797 return NULL;
3798 return ret;
3801 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3802 ::= Z <(function) encoding> E s [<discriminator>]
3803 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3806 static struct demangle_component *
3807 d_local_name (struct d_info *di)
3809 struct demangle_component *function;
3810 struct demangle_component *name;
3812 if (! d_check_char (di, 'Z'))
3813 return NULL;
3815 function = d_encoding (di, 0);
3816 if (!function)
3817 return NULL;
3819 if (! d_check_char (di, 'E'))
3820 return NULL;
3822 if (d_peek_char (di) == 's')
3824 d_advance (di, 1);
3825 if (! d_discriminator (di))
3826 return NULL;
3827 name = d_make_name (di, "string literal", sizeof "string literal" - 1);
3829 else
3831 int num = -1;
3833 if (d_peek_char (di) == 'd')
3835 /* Default argument scope: d <number> _. */
3836 d_advance (di, 1);
3837 num = d_compact_number (di);
3838 if (num < 0)
3839 return NULL;
3842 name = d_name (di, 0);
3844 if (name
3845 /* Lambdas and unnamed types have internal discriminators
3846 and are not functions. */
3847 && name->type != DEMANGLE_COMPONENT_LAMBDA
3848 && name->type != DEMANGLE_COMPONENT_UNNAMED_TYPE)
3850 /* Read and ignore an optional discriminator. */
3851 if (! d_discriminator (di))
3852 return NULL;
3855 if (num >= 0)
3856 name = d_make_default_arg (di, num, name);
3859 /* Elide the return type of the containing function so as to not
3860 confuse the user thinking it is the return type of whatever local
3861 function we might be containing. */
3862 if (function->type == DEMANGLE_COMPONENT_TYPED_NAME
3863 && d_right (function)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3864 d_left (d_right (function)) = NULL;
3866 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3869 /* <discriminator> ::= _ <number> # when number < 10
3870 ::= __ <number> _ # when number >= 10
3872 <discriminator> ::= _ <number> # when number >=10
3873 is also accepted to support gcc versions that wrongly mangled that way.
3875 We demangle the discriminator, but we don't print it out. FIXME:
3876 We should print it out in verbose mode. */
3878 static int
3879 d_discriminator (struct d_info *di)
3881 int discrim, num_underscores = 1;
3883 if (d_peek_char (di) != '_')
3884 return 1;
3885 d_advance (di, 1);
3886 if (d_peek_char (di) == '_')
3888 ++num_underscores;
3889 d_advance (di, 1);
3892 discrim = d_number (di);
3893 if (discrim < 0)
3894 return 0;
3895 if (num_underscores > 1 && discrim >= 10)
3897 if (d_peek_char (di) == '_')
3898 d_advance (di, 1);
3899 else
3900 return 0;
3903 return 1;
3906 /* <template-parm> ::= Ty
3907 ::= Tn <type>
3908 ::= Tt <template-head> E
3909 ::= Tp <template-parm> */
3911 static struct demangle_component *
3912 d_template_parm (struct d_info *di, int *bad)
3914 if (d_peek_char (di) != 'T')
3915 return NULL;
3917 struct demangle_component *op;
3918 enum demangle_component_type kind;
3919 switch (d_peek_next_char (di))
3921 default:
3922 return NULL;
3924 case 'p': /* Pack */
3925 d_advance (di, 2);
3926 op = d_template_parm (di, bad);
3927 kind = DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM;
3928 if (!op)
3930 *bad = 1;
3931 return NULL;
3933 break;
3935 case 'y': /* Typename */
3936 d_advance (di, 2);
3937 op = NULL;
3938 kind = DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM;
3939 break;
3941 case 'n': /* Non-Type */
3942 d_advance (di, 2);
3943 op = cplus_demangle_type (di);
3944 kind = DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM;
3945 if (!op)
3947 *bad = 1;
3948 return NULL;
3950 break;
3952 case 't': /* Template */
3953 d_advance (di, 2);
3954 op = d_template_head (di, bad);
3955 kind = DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM;
3956 if (!op || !d_check_char (di, 'E'))
3958 *bad = 1;
3959 return NULL;
3963 return d_make_comp (di, kind, op, NULL);
3966 /* <template-head> ::= <template-head>? <template-parm> */
3968 static struct demangle_component *
3969 d_template_head (struct d_info *di, int *bad)
3971 struct demangle_component *res = NULL, **slot = &res;
3972 struct demangle_component *op;
3974 while ((op = d_template_parm (di, bad)))
3976 *slot = op;
3977 slot = &d_right (op);
3980 /* Wrap it in a template head, to make concatenating with any parm list, and
3981 printing simpler. */
3982 if (res)
3983 res = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_HEAD, res, NULL);
3985 return res;
3988 /* <closure-type-name> ::= Ul <template-head>? <lambda-sig> E [ <nonnegative number> ] _ */
3990 static struct demangle_component *
3991 d_lambda (struct d_info *di)
3993 if (! d_check_char (di, 'U'))
3994 return NULL;
3995 if (! d_check_char (di, 'l'))
3996 return NULL;
3998 int bad = 0;
3999 struct demangle_component *head = d_template_head (di, &bad);
4000 if (bad)
4001 return NULL;
4003 struct demangle_component *tl = d_parmlist (di);
4004 if (tl == NULL)
4005 return NULL;
4006 if (head)
4008 d_right (head) = tl;
4009 tl = head;
4012 if (! d_check_char (di, 'E'))
4013 return NULL;
4015 int num = d_compact_number (di);
4016 if (num < 0)
4017 return NULL;
4019 struct demangle_component *ret = d_make_empty (di);
4020 if (ret)
4022 ret->type = DEMANGLE_COMPONENT_LAMBDA;
4023 ret->u.s_unary_num.sub = tl;
4024 ret->u.s_unary_num.num = num;
4027 return ret;
4030 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
4032 static struct demangle_component *
4033 d_unnamed_type (struct d_info *di)
4035 struct demangle_component *ret;
4036 int num;
4038 if (! d_check_char (di, 'U'))
4039 return NULL;
4040 if (! d_check_char (di, 't'))
4041 return NULL;
4043 num = d_compact_number (di);
4044 if (num < 0)
4045 return NULL;
4047 ret = d_make_empty (di);
4048 if (ret)
4050 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
4051 ret->u.s_number.number = num;
4054 if (! d_add_substitution (di, ret))
4055 return NULL;
4057 return ret;
4060 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
4063 static struct demangle_component *
4064 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
4066 const char *suffix = d_str (di);
4067 const char *pend = suffix;
4068 struct demangle_component *n;
4070 if (*pend == '.' && (IS_LOWER (pend[1]) || IS_DIGIT (pend[1])
4071 || pend[1] == '_'))
4073 pend += 2;
4074 while (IS_LOWER (*pend) || IS_DIGIT (*pend) || *pend == '_')
4075 ++pend;
4077 while (*pend == '.' && IS_DIGIT (pend[1]))
4079 pend += 2;
4080 while (IS_DIGIT (*pend))
4081 ++pend;
4083 d_advance (di, pend - suffix);
4084 n = d_make_name (di, suffix, pend - suffix);
4085 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
4088 /* Add a new substitution. */
4090 static int
4091 d_add_substitution (struct d_info *di, struct demangle_component *dc)
4093 if (dc == NULL)
4094 return 0;
4095 if (di->next_sub >= di->num_subs)
4096 return 0;
4097 di->subs[di->next_sub] = dc;
4098 ++di->next_sub;
4099 return 1;
4102 /* <substitution> ::= S <seq-id> _
4103 ::= S_
4104 ::= St
4105 ::= Sa
4106 ::= Sb
4107 ::= Ss
4108 ::= Si
4109 ::= So
4110 ::= Sd
4112 If PREFIX is non-zero, then this type is being used as a prefix in
4113 a qualified name. In this case, for the standard substitutions, we
4114 need to check whether we are being used as a prefix for a
4115 constructor or destructor, and return a full template name.
4116 Otherwise we will get something like std::iostream::~iostream()
4117 which does not correspond particularly well to any function which
4118 actually appears in the source.
4121 static const struct d_standard_sub_info standard_subs[] =
4123 { 't', NL ("std"),
4124 NL ("std"),
4125 NULL, 0 },
4126 { 'a', NL ("std::allocator"),
4127 NL ("std::allocator"),
4128 NL ("allocator") },
4129 { 'b', NL ("std::basic_string"),
4130 NL ("std::basic_string"),
4131 NL ("basic_string") },
4132 { 's', NL ("std::string"),
4133 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
4134 NL ("basic_string") },
4135 { 'i', NL ("std::istream"),
4136 NL ("std::basic_istream<char, std::char_traits<char> >"),
4137 NL ("basic_istream") },
4138 { 'o', NL ("std::ostream"),
4139 NL ("std::basic_ostream<char, std::char_traits<char> >"),
4140 NL ("basic_ostream") },
4141 { 'd', NL ("std::iostream"),
4142 NL ("std::basic_iostream<char, std::char_traits<char> >"),
4143 NL ("basic_iostream") }
4146 static struct demangle_component *
4147 d_substitution (struct d_info *di, int prefix)
4149 char c;
4151 if (! d_check_char (di, 'S'))
4152 return NULL;
4154 c = d_next_char (di);
4155 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
4157 unsigned int id;
4159 id = 0;
4160 if (c != '_')
4164 unsigned int new_id;
4166 if (IS_DIGIT (c))
4167 new_id = id * 36 + c - '0';
4168 else if (IS_UPPER (c))
4169 new_id = id * 36 + c - 'A' + 10;
4170 else
4171 return NULL;
4172 if (new_id < id)
4173 return NULL;
4174 id = new_id;
4175 c = d_next_char (di);
4177 while (c != '_');
4179 ++id;
4182 if (id >= (unsigned int) di->next_sub)
4183 return NULL;
4185 return di->subs[id];
4187 else
4189 int verbose;
4190 const struct d_standard_sub_info *p;
4191 const struct d_standard_sub_info *pend;
4193 verbose = (di->options & DMGL_VERBOSE) != 0;
4194 if (! verbose && prefix)
4196 char peek;
4198 peek = d_peek_char (di);
4199 if (peek == 'C' || peek == 'D')
4200 verbose = 1;
4203 pend = (&standard_subs[0]
4204 + sizeof standard_subs / sizeof standard_subs[0]);
4205 for (p = &standard_subs[0]; p < pend; ++p)
4207 if (c == p->code)
4209 const char *s;
4210 int len;
4211 struct demangle_component *dc;
4213 if (p->set_last_name != NULL)
4214 di->last_name = d_make_sub (di, p->set_last_name,
4215 p->set_last_name_len);
4216 if (verbose)
4218 s = p->full_expansion;
4219 len = p->full_len;
4221 else
4223 s = p->simple_expansion;
4224 len = p->simple_len;
4226 di->expansion += len;
4227 dc = d_make_sub (di, s, len);
4228 if (d_peek_char (di) == 'B')
4230 /* If there are ABI tags on the abbreviation, it becomes
4231 a substitution candidate. */
4232 dc = d_abi_tags (di, dc);
4233 if (! d_add_substitution (di, dc))
4234 return NULL;
4236 return dc;
4240 return NULL;
4244 static void
4245 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
4247 checkpoint->n = di->n;
4248 checkpoint->next_comp = di->next_comp;
4249 checkpoint->next_sub = di->next_sub;
4250 checkpoint->expansion = di->expansion;
4253 static void
4254 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
4256 di->n = checkpoint->n;
4257 di->next_comp = checkpoint->next_comp;
4258 di->next_sub = checkpoint->next_sub;
4259 di->expansion = checkpoint->expansion;
4262 /* Initialize a growable string. */
4264 static void
4265 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
4267 dgs->buf = NULL;
4268 dgs->len = 0;
4269 dgs->alc = 0;
4270 dgs->allocation_failure = 0;
4272 if (estimate > 0)
4273 d_growable_string_resize (dgs, estimate);
4276 /* Grow a growable string to a given size. */
4278 static inline void
4279 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
4281 size_t newalc;
4282 char *newbuf;
4284 if (dgs->allocation_failure)
4285 return;
4287 /* Start allocation at two bytes to avoid any possibility of confusion
4288 with the special value of 1 used as a return in *palc to indicate
4289 allocation failures. */
4290 newalc = dgs->alc > 0 ? dgs->alc : 2;
4291 while (newalc < need)
4292 newalc <<= 1;
4294 newbuf = (char *) realloc ("demangle.dgsr.1", dgs->buf, newalc);
4295 if (newbuf == NULL)
4297 free (dgs->buf);
4298 dgs->buf = NULL;
4299 dgs->len = 0;
4300 dgs->alc = 0;
4301 dgs->allocation_failure = 1;
4302 return;
4304 dgs->buf = newbuf;
4305 dgs->alc = newalc;
4308 /* Append a buffer to a growable string. */
4310 static inline void
4311 d_growable_string_append_buffer (struct d_growable_string *dgs,
4312 const char *s, size_t l)
4314 size_t need;
4316 need = dgs->len + l + 1;
4317 if (need > dgs->alc)
4318 d_growable_string_resize (dgs, need);
4320 if (dgs->allocation_failure)
4321 return;
4323 memcpy (dgs->buf + dgs->len, s, l);
4324 dgs->buf[dgs->len + l] = '\0';
4325 dgs->len += l;
4328 /* Bridge growable strings to the callback mechanism. */
4330 static void
4331 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
4333 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
4335 d_growable_string_append_buffer (dgs, s, l);
4338 /* Walk the tree, counting the number of templates encountered, and
4339 the number of times a scope might be saved. These counts will be
4340 used to allocate data structures for d_print_comp, so the logic
4341 here must mirror the logic d_print_comp will use. It is not
4342 important that the resulting numbers are exact, so long as they
4343 are larger than the actual numbers encountered. */
4345 static void
4346 d_count_templates_scopes (struct d_print_info *dpi,
4347 struct demangle_component *dc)
4349 if (dc == NULL || dc->d_counting > 1 || dpi->recursion > MAX_RECURSION_COUNT)
4350 return;
4352 ++ dc->d_counting;
4354 switch (dc->type)
4356 case DEMANGLE_COMPONENT_NAME:
4357 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4358 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4359 case DEMANGLE_COMPONENT_SUB_STD:
4360 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4361 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4362 case DEMANGLE_COMPONENT_OPERATOR:
4363 case DEMANGLE_COMPONENT_CHARACTER:
4364 case DEMANGLE_COMPONENT_NUMBER:
4365 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4366 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
4367 case DEMANGLE_COMPONENT_MODULE_NAME:
4368 case DEMANGLE_COMPONENT_MODULE_PARTITION:
4369 case DEMANGLE_COMPONENT_MODULE_INIT:
4370 case DEMANGLE_COMPONENT_FIXED_TYPE:
4371 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
4372 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
4373 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
4374 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
4375 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
4376 break;
4378 case DEMANGLE_COMPONENT_TEMPLATE:
4379 dpi->num_copy_templates++;
4380 goto recurse_left_right;
4382 case DEMANGLE_COMPONENT_REFERENCE:
4383 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4384 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4385 dpi->num_saved_scopes++;
4386 goto recurse_left_right;
4388 case DEMANGLE_COMPONENT_QUAL_NAME:
4389 case DEMANGLE_COMPONENT_LOCAL_NAME:
4390 case DEMANGLE_COMPONENT_TYPED_NAME:
4391 case DEMANGLE_COMPONENT_VTABLE:
4392 case DEMANGLE_COMPONENT_VTT:
4393 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4394 case DEMANGLE_COMPONENT_TYPEINFO:
4395 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4396 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4397 case DEMANGLE_COMPONENT_THUNK:
4398 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4399 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4400 case DEMANGLE_COMPONENT_JAVA_CLASS:
4401 case DEMANGLE_COMPONENT_GUARD:
4402 case DEMANGLE_COMPONENT_TLS_INIT:
4403 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4404 case DEMANGLE_COMPONENT_REFTEMP:
4405 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4406 case DEMANGLE_COMPONENT_RESTRICT:
4407 case DEMANGLE_COMPONENT_VOLATILE:
4408 case DEMANGLE_COMPONENT_CONST:
4409 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4410 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4411 case DEMANGLE_COMPONENT_CONST_THIS:
4412 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4413 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4414 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4415 case DEMANGLE_COMPONENT_NOEXCEPT:
4416 case DEMANGLE_COMPONENT_THROW_SPEC:
4417 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4418 case DEMANGLE_COMPONENT_POINTER:
4419 case DEMANGLE_COMPONENT_COMPLEX:
4420 case DEMANGLE_COMPONENT_IMAGINARY:
4421 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4422 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4423 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4424 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4425 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4426 case DEMANGLE_COMPONENT_ARGLIST:
4427 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4428 case DEMANGLE_COMPONENT_TPARM_OBJ:
4429 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4430 case DEMANGLE_COMPONENT_CAST:
4431 case DEMANGLE_COMPONENT_CONVERSION:
4432 case DEMANGLE_COMPONENT_NULLARY:
4433 case DEMANGLE_COMPONENT_UNARY:
4434 case DEMANGLE_COMPONENT_BINARY:
4435 case DEMANGLE_COMPONENT_BINARY_ARGS:
4436 case DEMANGLE_COMPONENT_TRINARY:
4437 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4438 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4439 case DEMANGLE_COMPONENT_LITERAL:
4440 case DEMANGLE_COMPONENT_LITERAL_NEG:
4441 case DEMANGLE_COMPONENT_VENDOR_EXPR:
4442 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4443 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4444 case DEMANGLE_COMPONENT_DECLTYPE:
4445 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4446 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4447 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4448 case DEMANGLE_COMPONENT_TAGGED_NAME:
4449 case DEMANGLE_COMPONENT_CLONE:
4450 recurse_left_right:
4451 /* PR 89394 - Check for too much recursion. */
4452 if (dpi->recursion > DEMANGLE_RECURSION_LIMIT)
4453 /* FIXME: There ought to be a way to report to the
4454 user that the recursion limit has been reached. */
4455 return;
4457 ++ dpi->recursion;
4458 d_count_templates_scopes (dpi, d_left (dc));
4459 d_count_templates_scopes (dpi, d_right (dc));
4460 -- dpi->recursion;
4461 break;
4463 case DEMANGLE_COMPONENT_CTOR:
4464 d_count_templates_scopes (dpi, dc->u.s_ctor.name);
4465 break;
4467 case DEMANGLE_COMPONENT_DTOR:
4468 d_count_templates_scopes (dpi, dc->u.s_dtor.name);
4469 break;
4471 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4472 d_count_templates_scopes (dpi, dc->u.s_extended_operator.name);
4473 break;
4475 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4476 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4477 case DEMANGLE_COMPONENT_MODULE_ENTITY:
4478 d_count_templates_scopes (dpi, d_left (dc));
4479 break;
4481 case DEMANGLE_COMPONENT_LAMBDA:
4482 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4483 d_count_templates_scopes (dpi, dc->u.s_unary_num.sub);
4484 break;
4488 /* Initialize a print information structure. */
4490 static void
4491 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4492 void *opaque, struct demangle_component *dc)
4494 dpi->len = 0;
4495 dpi->last_char = '\0';
4496 dpi->templates = NULL;
4497 dpi->modifiers = NULL;
4498 dpi->pack_index = 0;
4499 dpi->flush_count = 0;
4501 dpi->callback = callback;
4502 dpi->opaque = opaque;
4504 dpi->demangle_failure = 0;
4505 dpi->recursion = 0;
4506 dpi->lambda_tpl_parms = 0;
4508 dpi->component_stack = NULL;
4510 dpi->saved_scopes = NULL;
4511 dpi->next_saved_scope = 0;
4512 dpi->num_saved_scopes = 0;
4514 dpi->copy_templates = NULL;
4515 dpi->next_copy_template = 0;
4516 dpi->num_copy_templates = 0;
4518 d_count_templates_scopes (dpi, dc);
4519 /* If we did not reach the recursion limit, then reset the
4520 current recursion value back to 0, so that we can print
4521 the templates. */
4522 if (dpi->recursion < DEMANGLE_RECURSION_LIMIT)
4523 dpi->recursion = 0;
4524 dpi->num_copy_templates *= dpi->num_saved_scopes;
4526 dpi->current_template = NULL;
4529 /* Indicate that an error occurred during printing, and test for error. */
4531 static inline void
4532 d_print_error (struct d_print_info *dpi)
4534 dpi->demangle_failure = 1;
4537 static inline int
4538 d_print_saw_error (struct d_print_info *dpi)
4540 return dpi->demangle_failure != 0;
4543 /* Flush buffered characters to the callback. */
4545 static inline void
4546 d_print_flush (struct d_print_info *dpi)
4548 dpi->buf[dpi->len] = '\0';
4549 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4550 dpi->len = 0;
4551 dpi->flush_count++;
4554 /* Append characters and buffers for printing. */
4556 static inline void
4557 d_append_char (struct d_print_info *dpi, char c)
4559 if (dpi->len == sizeof (dpi->buf) - 1)
4560 d_print_flush (dpi);
4562 dpi->buf[dpi->len++] = c;
4563 dpi->last_char = c;
4566 static inline void
4567 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4569 size_t i;
4571 for (i = 0; i < l; i++)
4572 d_append_char (dpi, s[i]);
4575 static inline void
4576 d_append_string (struct d_print_info *dpi, const char *s)
4578 d_append_buffer (dpi, s, strlen (s));
4581 static inline void
4582 d_append_num (struct d_print_info *dpi, int l)
4584 char buf[25];
4585 sprintf (buf,"%d", l);
4586 d_append_string (dpi, buf);
4589 static inline char
4590 d_last_char (struct d_print_info *dpi)
4592 return dpi->last_char;
4595 /* Turn components into a human readable string. OPTIONS is the
4596 options bits passed to the demangler. DC is the tree to print.
4597 CALLBACK is a function to call to flush demangled string segments
4598 as they fill the intermediate buffer, and OPAQUE is a generalized
4599 callback argument. On success, this returns 1. On failure,
4600 it returns 0, indicating a bad parse. It does not use heap
4601 memory to build an output string, so cannot encounter memory
4602 allocation failure. */
4604 CP_STATIC_IF_GLIBCPP_V3
4606 cplus_demangle_print_callback (int options,
4607 struct demangle_component *dc,
4608 demangle_callbackref callback, void *opaque)
4610 struct d_print_info dpi;
4612 d_print_init (&dpi, callback, opaque, dc);
4615 #if 0 /* in valgrind */
4616 #ifdef CP_DYNAMIC_ARRAYS
4617 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4618 and flagged as errors by Address Sanitizer. */
4619 __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
4620 ? dpi.num_saved_scopes : 1];
4621 __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
4622 ? dpi.num_copy_templates : 1];
4624 dpi.saved_scopes = scopes;
4625 dpi.copy_templates = temps;
4626 #else
4627 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4628 * sizeof (*dpi.saved_scopes));
4629 dpi.copy_templates = alloca (dpi.num_copy_templates
4630 * sizeof (*dpi.copy_templates));
4631 #endif
4632 #else
4633 /* Allocate memory dynamically to avoid VLAs as valgrind stack
4634 is a scarce resource */
4635 dpi.saved_scopes = xmalloc(dpi.num_saved_scopes
4636 * sizeof (*dpi.saved_scopes));
4637 dpi.copy_templates = xmalloc (dpi.num_copy_templates
4638 * sizeof (*dpi.copy_templates));
4639 #endif /* ! in valgrind */
4640 d_print_comp (&dpi, options, dc);
4643 d_print_flush (&dpi);
4645 int status = ! d_print_saw_error (&dpi);
4647 #if 0 /* in valgrind */
4648 #else
4649 free (dpi.saved_scopes);
4650 free (dpi.copy_templates);
4651 #endif /* in valgrind */
4653 return status;
4656 /* Turn components into a human readable string. OPTIONS is the
4657 options bits passed to the demangler. DC is the tree to print.
4658 ESTIMATE is a guess at the length of the result. This returns a
4659 string allocated by malloc, or NULL on error. On success, this
4660 sets *PALC to the size of the allocated buffer. On failure, this
4661 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4662 failure. */
4664 CP_STATIC_IF_GLIBCPP_V3
4665 char *
4666 cplus_demangle_print (int options, struct demangle_component *dc,
4667 int estimate, size_t *palc)
4669 struct d_growable_string dgs;
4671 d_growable_string_init (&dgs, estimate);
4673 if (! cplus_demangle_print_callback (options, dc,
4674 d_growable_string_callback_adapter,
4675 &dgs))
4677 free (dgs.buf);
4678 *palc = 0;
4679 return NULL;
4682 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4683 return dgs.buf;
4686 /* Returns the I'th element of the template arglist ARGS, or NULL on
4687 failure. If I is negative, return the entire arglist. */
4689 static struct demangle_component *
4690 d_index_template_argument (struct demangle_component *args, int i)
4692 struct demangle_component *a;
4694 if (i < 0)
4695 /* Print the whole argument pack. */
4696 return args;
4698 for (a = args;
4699 a != NULL;
4700 a = d_right (a))
4702 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4703 return NULL;
4704 if (i <= 0)
4705 break;
4706 --i;
4708 if (i != 0 || a == NULL)
4709 return NULL;
4711 return d_left (a);
4714 /* Returns the template argument from the current context indicated by DC,
4715 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4717 static struct demangle_component *
4718 d_lookup_template_argument (struct d_print_info *dpi,
4719 const struct demangle_component *dc)
4721 if (dpi->templates == NULL)
4723 d_print_error (dpi);
4724 return NULL;
4727 return d_index_template_argument
4728 (d_right (dpi->templates->template_decl),
4729 dc->u.s_number.number);
4732 /* Returns a template argument pack used in DC (any will do), or NULL. */
4734 static struct demangle_component *
4735 d_find_pack (struct d_print_info *dpi,
4736 const struct demangle_component *dc)
4738 struct demangle_component *a;
4739 if (dc == NULL)
4740 return NULL;
4742 switch (dc->type)
4744 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4745 a = d_lookup_template_argument (dpi, dc);
4746 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4747 return a;
4748 return NULL;
4750 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4751 return NULL;
4753 case DEMANGLE_COMPONENT_LAMBDA:
4754 case DEMANGLE_COMPONENT_NAME:
4755 case DEMANGLE_COMPONENT_TAGGED_NAME:
4756 case DEMANGLE_COMPONENT_OPERATOR:
4757 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4758 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4759 case DEMANGLE_COMPONENT_SUB_STD:
4760 case DEMANGLE_COMPONENT_CHARACTER:
4761 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4762 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4763 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4764 case DEMANGLE_COMPONENT_NUMBER:
4765 return NULL;
4767 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4768 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4769 case DEMANGLE_COMPONENT_CTOR:
4770 return d_find_pack (dpi, dc->u.s_ctor.name);
4771 case DEMANGLE_COMPONENT_DTOR:
4772 return d_find_pack (dpi, dc->u.s_dtor.name);
4774 default:
4775 a = d_find_pack (dpi, d_left (dc));
4776 if (a)
4777 return a;
4778 return d_find_pack (dpi, d_right (dc));
4782 /* Returns the length of the template argument pack DC. */
4784 static int
4785 d_pack_length (const struct demangle_component *dc)
4787 int count = 0;
4788 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4789 && d_left (dc) != NULL)
4791 ++count;
4792 dc = d_right (dc);
4794 return count;
4797 /* Returns the number of template args in DC, expanding any pack expansions
4798 found there. */
4800 static int
4801 d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
4803 int count = 0;
4804 for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4805 dc = d_right (dc))
4807 struct demangle_component *elt = d_left (dc);
4808 if (elt == NULL)
4809 break;
4810 if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
4812 struct demangle_component *a = d_find_pack (dpi, d_left (elt));
4813 count += d_pack_length (a);
4815 else
4816 ++count;
4818 return count;
4821 /* DC is a component of a mangled expression. Print it, wrapped in parens
4822 if needed. */
4824 static void
4825 d_print_subexpr (struct d_print_info *dpi, int options,
4826 struct demangle_component *dc)
4828 int simple = 0;
4829 if (dc->type == DEMANGLE_COMPONENT_NAME
4830 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4831 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4832 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4833 simple = 1;
4834 if (!simple)
4835 d_append_char (dpi, '(');
4836 d_print_comp (dpi, options, dc);
4837 if (!simple)
4838 d_append_char (dpi, ')');
4841 /* Save the current scope. */
4843 static void
4844 d_save_scope (struct d_print_info *dpi,
4845 const struct demangle_component *container)
4847 struct d_saved_scope *scope;
4848 struct d_print_template *src, **link;
4850 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4852 d_print_error (dpi);
4853 return;
4855 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4856 dpi->next_saved_scope++;
4858 scope->container = container;
4859 link = &scope->templates;
4861 for (src = dpi->templates; src != NULL; src = src->next)
4863 struct d_print_template *dst;
4865 if (dpi->next_copy_template >= dpi->num_copy_templates)
4867 d_print_error (dpi);
4868 return;
4870 dst = &dpi->copy_templates[dpi->next_copy_template];
4871 dpi->next_copy_template++;
4873 dst->template_decl = src->template_decl;
4874 *link = dst;
4875 link = &dst->next;
4878 *link = NULL;
4881 /* Attempt to locate a previously saved scope. Returns NULL if no
4882 corresponding saved scope was found. */
4884 static struct d_saved_scope *
4885 d_get_saved_scope (struct d_print_info *dpi,
4886 const struct demangle_component *container)
4888 int i;
4890 for (i = 0; i < dpi->next_saved_scope; i++)
4891 if (dpi->saved_scopes[i].container == container)
4892 return &dpi->saved_scopes[i];
4894 return NULL;
4897 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4898 return false. */
4900 static int
4901 d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
4902 struct demangle_component *dc)
4904 struct demangle_component *ops, *operator_, *op1, *op2;
4905 int save_idx;
4907 const char *fold_code = d_left (dc)->u.s_operator.op->code;
4908 if (fold_code[0] != 'f')
4909 return 0;
4911 ops = d_right (dc);
4912 operator_ = d_left (ops);
4913 op1 = d_right (ops);
4914 op2 = 0;
4915 if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
4917 op2 = d_right (op1);
4918 op1 = d_left (op1);
4921 /* Print the whole pack. */
4922 save_idx = dpi->pack_index;
4923 dpi->pack_index = -1;
4925 switch (fold_code[1])
4927 /* Unary left fold, (... + X). */
4928 case 'l':
4929 d_append_string (dpi, "(...");
4930 d_print_expr_op (dpi, options, operator_);
4931 d_print_subexpr (dpi, options, op1);
4932 d_append_char (dpi, ')');
4933 break;
4935 /* Unary right fold, (X + ...). */
4936 case 'r':
4937 d_append_char (dpi, '(');
4938 d_print_subexpr (dpi, options, op1);
4939 d_print_expr_op (dpi, options, operator_);
4940 d_append_string (dpi, "...)");
4941 break;
4943 /* Binary left fold, (42 + ... + X). */
4944 case 'L':
4945 /* Binary right fold, (X + ... + 42). */
4946 case 'R':
4947 d_append_char (dpi, '(');
4948 d_print_subexpr (dpi, options, op1);
4949 d_print_expr_op (dpi, options, operator_);
4950 d_append_string (dpi, "...");
4951 d_print_expr_op (dpi, options, operator_);
4952 d_print_subexpr (dpi, options, op2);
4953 d_append_char (dpi, ')');
4954 break;
4957 dpi->pack_index = save_idx;
4958 return 1;
4961 /* True iff DC represents a C99-style designated initializer. */
4963 static int
4964 is_designated_init (struct demangle_component *dc)
4966 if (dc->type != DEMANGLE_COMPONENT_BINARY
4967 && dc->type != DEMANGLE_COMPONENT_TRINARY)
4968 return 0;
4970 struct demangle_component *op = d_left (dc);
4971 const char *code = op->u.s_operator.op->code;
4972 return (code[0] == 'd'
4973 && (code[1] == 'i' || code[1] == 'x' || code[1] == 'X'));
4976 /* If DC represents a C99-style designated initializer, print it and return
4977 true; otherwise, return false. */
4979 static int
4980 d_maybe_print_designated_init (struct d_print_info *dpi, int options,
4981 struct demangle_component *dc)
4983 if (!is_designated_init (dc))
4984 return 0;
4986 const char *code = d_left (dc)->u.s_operator.op->code;
4988 struct demangle_component *operands = d_right (dc);
4989 struct demangle_component *op1 = d_left (operands);
4990 struct demangle_component *op2 = d_right (operands);
4992 if (code[1] == 'i')
4993 d_append_char (dpi, '.');
4994 else
4995 d_append_char (dpi, '[');
4997 d_print_comp (dpi, options, op1);
4998 if (code[1] == 'X')
5000 d_append_string (dpi, " ... ");
5001 d_print_comp (dpi, options, d_left (op2));
5002 op2 = d_right (op2);
5004 if (code[1] != 'i')
5005 d_append_char (dpi, ']');
5006 if (is_designated_init (op2))
5008 /* Don't put '=' or '(' between chained designators. */
5009 d_print_comp (dpi, options, op2);
5011 else
5013 d_append_char (dpi, '=');
5014 d_print_subexpr (dpi, options, op2);
5016 return 1;
5019 static void
5020 d_print_lambda_parm_name (struct d_print_info *dpi, int type, unsigned index)
5022 const char *str;
5023 switch (type)
5025 default:
5026 dpi->demangle_failure = 1;
5027 str = "";
5028 break;
5030 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
5031 str = "$T";
5032 break;
5034 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
5035 str = "$N";
5036 break;
5038 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
5039 str = "$TT";
5040 break;
5042 d_append_string (dpi, str);
5043 d_append_num (dpi, index);
5046 /* Subroutine to handle components. */
5048 static void
5049 d_print_comp_inner (struct d_print_info *dpi, int options,
5050 struct demangle_component *dc)
5052 /* Magic variable to let reference smashing skip over the next modifier
5053 without needing to modify *dc. */
5054 struct demangle_component *mod_inner = NULL;
5056 /* Variable used to store the current templates while a previously
5057 captured scope is used. */
5058 struct d_print_template *saved_templates = NULL; /* silence GCC */
5060 /* Nonzero if templates have been stored in the above variable. */
5061 int need_template_restore = 0;
5063 if (dc == NULL)
5065 d_print_error (dpi);
5066 return;
5068 if (d_print_saw_error (dpi))
5069 return;
5071 switch (dc->type)
5073 case DEMANGLE_COMPONENT_NAME:
5074 if ((options & DMGL_JAVA) == 0)
5075 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
5076 else
5077 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
5078 return;
5080 case DEMANGLE_COMPONENT_TAGGED_NAME:
5081 d_print_comp (dpi, options, d_left (dc));
5082 d_append_string (dpi, "[abi:");
5083 d_print_comp (dpi, options, d_right (dc));
5084 d_append_char (dpi, ']');
5085 return;
5087 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
5088 d_append_char (dpi, '[');
5089 for (;;)
5091 d_print_comp (dpi, options, d_left (dc));
5092 dc = d_right (dc);
5093 if (!dc)
5094 break;
5095 d_append_string (dpi, ", ");
5097 d_append_char (dpi, ']');
5098 return;
5100 case DEMANGLE_COMPONENT_MODULE_ENTITY:
5101 d_print_comp (dpi, options, d_left (dc));
5102 d_append_char (dpi, '@');
5103 d_print_comp (dpi, options, d_right (dc));
5104 return;
5106 case DEMANGLE_COMPONENT_MODULE_NAME:
5107 case DEMANGLE_COMPONENT_MODULE_PARTITION:
5109 if (d_left (dc))
5110 d_print_comp (dpi, options, d_left (dc));
5111 char c = dc->type == DEMANGLE_COMPONENT_MODULE_PARTITION
5112 ? ':' : d_left (dc) ? '.' : 0;
5113 if (c)
5114 d_append_char (dpi, c);
5115 d_print_comp (dpi, options, d_right (dc));
5117 return;
5119 case DEMANGLE_COMPONENT_QUAL_NAME:
5120 case DEMANGLE_COMPONENT_LOCAL_NAME:
5121 d_print_comp (dpi, options, d_left (dc));
5122 if ((options & DMGL_JAVA) == 0)
5123 d_append_string (dpi, "::");
5124 else
5125 d_append_char (dpi, '.');
5127 struct demangle_component *local_name = d_right (dc);
5128 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5130 d_append_string (dpi, "{default arg#");
5131 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
5132 d_append_string (dpi, "}::");
5133 local_name = local_name->u.s_unary_num.sub;
5135 d_print_comp (dpi, options, local_name);
5137 return;
5139 case DEMANGLE_COMPONENT_TYPED_NAME:
5141 struct d_print_mod *hold_modifiers;
5142 struct demangle_component *typed_name;
5143 struct d_print_mod adpm[4];
5144 unsigned int i;
5145 struct d_print_template dpt;
5147 /* Pass the name down to the type so that it can be printed in
5148 the right place for the type. We also have to pass down
5149 any CV-qualifiers, which apply to the this parameter. */
5150 hold_modifiers = dpi->modifiers;
5151 dpi->modifiers = 0;
5152 i = 0;
5153 typed_name = d_left (dc);
5154 while (typed_name != NULL)
5156 if (i >= sizeof adpm / sizeof adpm[0])
5158 d_print_error (dpi);
5159 return;
5162 adpm[i].next = dpi->modifiers;
5163 dpi->modifiers = &adpm[i];
5164 adpm[i].mod = typed_name;
5165 adpm[i].printed = 0;
5166 adpm[i].templates = dpi->templates;
5167 ++i;
5169 if (!is_fnqual_component_type (typed_name->type))
5170 break;
5172 typed_name = d_left (typed_name);
5175 if (typed_name == NULL)
5177 d_print_error (dpi);
5178 return;
5181 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
5182 there may be CV-qualifiers on its right argument which
5183 really apply here; this happens when parsing a class that
5184 is local to a function. */
5185 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5187 typed_name = d_right (typed_name);
5188 if (typed_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5189 typed_name = typed_name->u.s_unary_num.sub;
5190 while (typed_name != NULL
5191 && is_fnqual_component_type (typed_name->type))
5193 if (i >= sizeof adpm / sizeof adpm[0])
5195 d_print_error (dpi);
5196 return;
5199 adpm[i] = adpm[i - 1];
5200 adpm[i].next = &adpm[i - 1];
5201 dpi->modifiers = &adpm[i];
5203 adpm[i - 1].mod = typed_name;
5204 adpm[i - 1].printed = 0;
5205 adpm[i - 1].templates = dpi->templates;
5206 ++i;
5208 typed_name = d_left (typed_name);
5210 if (typed_name == NULL)
5212 d_print_error (dpi);
5213 return;
5217 /* If typed_name is a template, then it applies to the
5218 function type as well. */
5219 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5221 dpt.next = dpi->templates;
5222 dpi->templates = &dpt;
5223 dpt.template_decl = typed_name;
5226 d_print_comp (dpi, options, d_right (dc));
5228 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5229 dpi->templates = dpt.next;
5231 /* If the modifiers didn't get printed by the type, print them
5232 now. */
5233 while (i > 0)
5235 --i;
5236 if (! adpm[i].printed)
5238 d_append_char (dpi, ' ');
5239 d_print_mod (dpi, options, adpm[i].mod);
5243 dpi->modifiers = hold_modifiers;
5245 return;
5248 case DEMANGLE_COMPONENT_TEMPLATE:
5250 struct d_print_mod *hold_dpm;
5251 struct demangle_component *dcl;
5252 const struct demangle_component *hold_current;
5254 /* This template may need to be referenced by a cast operator
5255 contained in its subtree. */
5256 hold_current = dpi->current_template;
5257 dpi->current_template = dc;
5259 /* Don't push modifiers into a template definition. Doing so
5260 could give the wrong definition for a template argument.
5261 Instead, treat the template essentially as a name. */
5263 hold_dpm = dpi->modifiers;
5264 dpi->modifiers = NULL;
5266 dcl = d_left (dc);
5268 if ((options & DMGL_JAVA) != 0
5269 && dcl->type == DEMANGLE_COMPONENT_NAME
5270 && dcl->u.s_name.len == 6
5271 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
5273 /* Special-case Java arrays, so that JArray<TYPE> appears
5274 instead as TYPE[]. */
5276 d_print_comp (dpi, options, d_right (dc));
5277 d_append_string (dpi, "[]");
5279 else
5281 d_print_comp (dpi, options, dcl);
5282 if (d_last_char (dpi) == '<')
5283 d_append_char (dpi, ' ');
5284 d_append_char (dpi, '<');
5285 d_print_comp (dpi, options, d_right (dc));
5286 /* Avoid generating two consecutive '>' characters, to avoid
5287 the C++ syntactic ambiguity. */
5288 if (d_last_char (dpi) == '>')
5289 d_append_char (dpi, ' ');
5290 d_append_char (dpi, '>');
5293 dpi->modifiers = hold_dpm;
5294 dpi->current_template = hold_current;
5296 return;
5299 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
5300 if (dpi->lambda_tpl_parms > dc->u.s_number.number + 1)
5302 const struct demangle_component *a
5303 = d_left (dpi->templates->template_decl);
5304 unsigned c;
5305 for (c = dc->u.s_number.number; a && c; c--)
5306 a = d_right (a);
5307 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
5308 a = d_left (a);
5309 if (!a)
5310 dpi->demangle_failure = 1;
5311 else
5312 d_print_lambda_parm_name (dpi, a->type, dc->u.s_number.number);
5314 else if (dpi->lambda_tpl_parms)
5316 /* Show the template parm index, as that's how g++ displays
5317 these, and future proofs us against potential
5318 '[]<typename T> (T *a, T *b) {...}'. */
5319 d_append_buffer (dpi, "auto:", 5);
5320 d_append_num (dpi, dc->u.s_number.number + 1);
5322 else
5324 struct d_print_template *hold_dpt;
5325 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
5327 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5328 a = d_index_template_argument (a, dpi->pack_index);
5330 if (a == NULL)
5332 d_print_error (dpi);
5333 return;
5336 /* While processing this parameter, we need to pop the list
5337 of templates. This is because the template parameter may
5338 itself be a reference to a parameter of an outer
5339 template. */
5341 hold_dpt = dpi->templates;
5342 dpi->templates = hold_dpt->next;
5344 d_print_comp (dpi, options, a);
5346 dpi->templates = hold_dpt;
5348 return;
5350 case DEMANGLE_COMPONENT_TPARM_OBJ:
5351 d_append_string (dpi, "template parameter object for ");
5352 d_print_comp (dpi, options, d_left (dc));
5353 return;
5355 case DEMANGLE_COMPONENT_CTOR:
5356 d_print_comp (dpi, options, dc->u.s_ctor.name);
5357 return;
5359 case DEMANGLE_COMPONENT_DTOR:
5360 d_append_char (dpi, '~');
5361 d_print_comp (dpi, options, dc->u.s_dtor.name);
5362 return;
5364 case DEMANGLE_COMPONENT_MODULE_INIT:
5365 d_append_string (dpi, "initializer for module ");
5366 d_print_comp (dpi, options, d_left (dc));
5367 return;
5369 case DEMANGLE_COMPONENT_VTABLE:
5370 d_append_string (dpi, "vtable for ");
5371 d_print_comp (dpi, options, d_left (dc));
5372 return;
5374 case DEMANGLE_COMPONENT_VTT:
5375 d_append_string (dpi, "VTT for ");
5376 d_print_comp (dpi, options, d_left (dc));
5377 return;
5379 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
5380 d_append_string (dpi, "construction vtable for ");
5381 d_print_comp (dpi, options, d_left (dc));
5382 d_append_string (dpi, "-in-");
5383 d_print_comp (dpi, options, d_right (dc));
5384 return;
5386 case DEMANGLE_COMPONENT_TYPEINFO:
5387 d_append_string (dpi, "typeinfo for ");
5388 d_print_comp (dpi, options, d_left (dc));
5389 return;
5391 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
5392 d_append_string (dpi, "typeinfo name for ");
5393 d_print_comp (dpi, options, d_left (dc));
5394 return;
5396 case DEMANGLE_COMPONENT_TYPEINFO_FN:
5397 d_append_string (dpi, "typeinfo fn for ");
5398 d_print_comp (dpi, options, d_left (dc));
5399 return;
5401 case DEMANGLE_COMPONENT_THUNK:
5402 d_append_string (dpi, "non-virtual thunk to ");
5403 d_print_comp (dpi, options, d_left (dc));
5404 return;
5406 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
5407 d_append_string (dpi, "virtual thunk to ");
5408 d_print_comp (dpi, options, d_left (dc));
5409 return;
5411 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
5412 d_append_string (dpi, "covariant return thunk to ");
5413 d_print_comp (dpi, options, d_left (dc));
5414 return;
5416 case DEMANGLE_COMPONENT_JAVA_CLASS:
5417 d_append_string (dpi, "java Class for ");
5418 d_print_comp (dpi, options, d_left (dc));
5419 return;
5421 case DEMANGLE_COMPONENT_GUARD:
5422 d_append_string (dpi, "guard variable for ");
5423 d_print_comp (dpi, options, d_left (dc));
5424 return;
5426 case DEMANGLE_COMPONENT_TLS_INIT:
5427 d_append_string (dpi, "TLS init function for ");
5428 d_print_comp (dpi, options, d_left (dc));
5429 return;
5431 case DEMANGLE_COMPONENT_TLS_WRAPPER:
5432 d_append_string (dpi, "TLS wrapper function for ");
5433 d_print_comp (dpi, options, d_left (dc));
5434 return;
5436 case DEMANGLE_COMPONENT_REFTEMP:
5437 d_append_string (dpi, "reference temporary #");
5438 d_print_comp (dpi, options, d_right (dc));
5439 d_append_string (dpi, " for ");
5440 d_print_comp (dpi, options, d_left (dc));
5441 return;
5443 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
5444 d_append_string (dpi, "hidden alias for ");
5445 d_print_comp (dpi, options, d_left (dc));
5446 return;
5448 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
5449 d_append_string (dpi, "transaction clone for ");
5450 d_print_comp (dpi, options, d_left (dc));
5451 return;
5453 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
5454 d_append_string (dpi, "non-transaction clone for ");
5455 d_print_comp (dpi, options, d_left (dc));
5456 return;
5458 case DEMANGLE_COMPONENT_SUB_STD:
5459 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
5460 return;
5462 case DEMANGLE_COMPONENT_RESTRICT:
5463 case DEMANGLE_COMPONENT_VOLATILE:
5464 case DEMANGLE_COMPONENT_CONST:
5466 struct d_print_mod *pdpm;
5468 /* When printing arrays, it's possible to have cases where the
5469 same CV-qualifier gets pushed on the stack multiple times.
5470 We only need to print it once. */
5472 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
5474 if (! pdpm->printed)
5476 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
5477 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
5478 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
5479 break;
5480 if (pdpm->mod->type == dc->type)
5482 d_print_comp (dpi, options, d_left (dc));
5483 return;
5488 goto modifier;
5490 case DEMANGLE_COMPONENT_REFERENCE:
5491 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5493 /* Handle reference smashing: & + && = &. */
5494 struct demangle_component *sub = d_left (dc);
5495 if (!dpi->lambda_tpl_parms
5496 && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
5498 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
5499 struct demangle_component *a;
5501 if (scope == NULL)
5503 /* This is the first time SUB has been traversed.
5504 We need to capture the current templates so
5505 they can be restored if SUB is reentered as a
5506 substitution. */
5507 d_save_scope (dpi, sub);
5508 if (d_print_saw_error (dpi))
5509 return;
5511 else
5513 const struct d_component_stack *dcse;
5514 int found_self_or_parent = 0;
5516 /* This traversal is reentering SUB as a substition.
5517 If we are not beneath SUB or DC in the tree then we
5518 need to restore SUB's template stack temporarily. */
5519 for (dcse = dpi->component_stack; dcse != NULL;
5520 dcse = dcse->parent)
5522 if (dcse->dc == sub
5523 || (dcse->dc == dc
5524 && dcse != dpi->component_stack))
5526 found_self_or_parent = 1;
5527 break;
5531 if (!found_self_or_parent)
5533 saved_templates = dpi->templates;
5534 dpi->templates = scope->templates;
5535 need_template_restore = 1;
5539 a = d_lookup_template_argument (dpi, sub);
5540 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5541 a = d_index_template_argument (a, dpi->pack_index);
5543 if (a == NULL)
5545 if (need_template_restore)
5546 dpi->templates = saved_templates;
5548 d_print_error (dpi);
5549 return;
5552 sub = a;
5555 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
5556 || sub->type == dc->type)
5557 dc = sub;
5558 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
5559 mod_inner = d_left (sub);
5561 /* Fall through. */
5563 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5564 case DEMANGLE_COMPONENT_POINTER:
5565 case DEMANGLE_COMPONENT_COMPLEX:
5566 case DEMANGLE_COMPONENT_IMAGINARY:
5567 FNQUAL_COMPONENT_CASE:
5568 modifier:
5570 /* We keep a list of modifiers on the stack. */
5571 struct d_print_mod dpm;
5573 dpm.next = dpi->modifiers;
5574 dpi->modifiers = &dpm;
5575 dpm.mod = dc;
5576 dpm.printed = 0;
5577 dpm.templates = dpi->templates;
5579 if (!mod_inner)
5580 mod_inner = d_left (dc);
5582 d_print_comp (dpi, options, mod_inner);
5584 /* If the modifier didn't get printed by the type, print it
5585 now. */
5586 if (! dpm.printed)
5587 d_print_mod (dpi, options, dc);
5589 dpi->modifiers = dpm.next;
5591 if (need_template_restore)
5592 dpi->templates = saved_templates;
5594 return;
5597 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5598 if ((options & DMGL_JAVA) == 0)
5599 d_append_buffer (dpi, dc->u.s_builtin.type->name,
5600 dc->u.s_builtin.type->len);
5601 else
5602 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
5603 dc->u.s_builtin.type->java_len);
5604 return;
5606 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
5607 d_append_buffer (dpi, dc->u.s_extended_builtin.type->name,
5608 dc->u.s_extended_builtin.type->len);
5609 d_append_num (dpi, dc->u.s_extended_builtin.arg);
5610 if (dc->u.s_extended_builtin.suffix)
5611 d_append_buffer (dpi, &dc->u.s_extended_builtin.suffix, 1);
5612 return;
5614 case DEMANGLE_COMPONENT_VENDOR_TYPE:
5615 d_print_comp (dpi, options, d_left (dc));
5616 return;
5618 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5620 if ((options & DMGL_RET_POSTFIX) != 0)
5621 d_print_function_type (dpi,
5622 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5623 dc, dpi->modifiers);
5625 /* Print return type if present */
5626 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
5627 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5628 d_left (dc));
5629 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
5631 struct d_print_mod dpm;
5633 /* We must pass this type down as a modifier in order to
5634 print it in the right location. */
5635 dpm.next = dpi->modifiers;
5636 dpi->modifiers = &dpm;
5637 dpm.mod = dc;
5638 dpm.printed = 0;
5639 dpm.templates = dpi->templates;
5641 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5642 d_left (dc));
5644 dpi->modifiers = dpm.next;
5646 if (dpm.printed)
5647 return;
5649 /* In standard prefix notation, there is a space between the
5650 return type and the function signature. */
5651 if ((options & DMGL_RET_POSTFIX) == 0)
5652 d_append_char (dpi, ' ');
5655 if ((options & DMGL_RET_POSTFIX) == 0)
5656 d_print_function_type (dpi,
5657 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5658 dc, dpi->modifiers);
5660 return;
5663 case DEMANGLE_COMPONENT_ARRAY_TYPE:
5665 struct d_print_mod *hold_modifiers;
5666 struct d_print_mod adpm[4];
5667 unsigned int i;
5668 struct d_print_mod *pdpm;
5670 /* We must pass this type down as a modifier in order to print
5671 multi-dimensional arrays correctly. If the array itself is
5672 CV-qualified, we act as though the element type were
5673 CV-qualified. We do this by copying the modifiers down
5674 rather than fiddling pointers, so that we don't wind up
5675 with a d_print_mod higher on the stack pointing into our
5676 stack frame after we return. */
5678 hold_modifiers = dpi->modifiers;
5680 adpm[0].next = hold_modifiers;
5681 dpi->modifiers = &adpm[0];
5682 adpm[0].mod = dc;
5683 adpm[0].printed = 0;
5684 adpm[0].templates = dpi->templates;
5686 i = 1;
5687 pdpm = hold_modifiers;
5688 while (pdpm != NULL
5689 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
5690 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
5691 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
5693 if (! pdpm->printed)
5695 if (i >= sizeof adpm / sizeof adpm[0])
5697 d_print_error (dpi);
5698 return;
5701 adpm[i] = *pdpm;
5702 adpm[i].next = dpi->modifiers;
5703 dpi->modifiers = &adpm[i];
5704 pdpm->printed = 1;
5705 ++i;
5708 pdpm = pdpm->next;
5711 d_print_comp (dpi, options, d_right (dc));
5713 dpi->modifiers = hold_modifiers;
5715 if (adpm[0].printed)
5716 return;
5718 while (i > 1)
5720 --i;
5721 d_print_mod (dpi, options, adpm[i].mod);
5724 d_print_array_type (dpi, options, dc, dpi->modifiers);
5726 return;
5729 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5730 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5732 struct d_print_mod dpm;
5734 dpm.next = dpi->modifiers;
5735 dpi->modifiers = &dpm;
5736 dpm.mod = dc;
5737 dpm.printed = 0;
5738 dpm.templates = dpi->templates;
5740 d_print_comp (dpi, options, d_right (dc));
5742 /* If the modifier didn't get printed by the type, print it
5743 now. */
5744 if (! dpm.printed)
5745 d_print_mod (dpi, options, dc);
5747 dpi->modifiers = dpm.next;
5749 return;
5752 case DEMANGLE_COMPONENT_ARGLIST:
5753 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5754 if (d_left (dc) != NULL)
5755 d_print_comp (dpi, options, d_left (dc));
5756 if (d_right (dc) != NULL)
5758 size_t len;
5759 unsigned long int flush_count;
5760 /* Make sure ", " isn't flushed by d_append_string, otherwise
5761 dpi->len -= 2 wouldn't work. */
5762 if (dpi->len >= sizeof (dpi->buf) - 2)
5763 d_print_flush (dpi);
5764 d_append_string (dpi, ", ");
5765 len = dpi->len;
5766 flush_count = dpi->flush_count;
5767 d_print_comp (dpi, options, d_right (dc));
5768 /* If that didn't print anything (which can happen with empty
5769 template argument packs), remove the comma and space. */
5770 if (dpi->flush_count == flush_count && dpi->len == len)
5771 dpi->len -= 2;
5773 return;
5775 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5777 struct demangle_component *type = d_left (dc);
5778 struct demangle_component *list = d_right (dc);
5780 if (type)
5781 d_print_comp (dpi, options, type);
5782 d_append_char (dpi, '{');
5783 d_print_comp (dpi, options, list);
5784 d_append_char (dpi, '}');
5786 return;
5788 case DEMANGLE_COMPONENT_OPERATOR:
5790 const struct demangle_operator_info *op = dc->u.s_operator.op;
5791 int len = op->len;
5793 d_append_string (dpi, "operator");
5794 /* Add a space before new/delete. */
5795 if (IS_LOWER (op->name[0]))
5796 d_append_char (dpi, ' ');
5797 /* Omit a trailing space. */
5798 if (op->name[len-1] == ' ')
5799 --len;
5800 d_append_buffer (dpi, op->name, len);
5801 return;
5804 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5805 d_append_string (dpi, "operator ");
5806 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5807 return;
5809 case DEMANGLE_COMPONENT_CONVERSION:
5810 d_append_string (dpi, "operator ");
5811 d_print_conversion (dpi, options, dc);
5812 return;
5814 case DEMANGLE_COMPONENT_NULLARY:
5815 d_print_expr_op (dpi, options, d_left (dc));
5816 return;
5818 case DEMANGLE_COMPONENT_UNARY:
5820 struct demangle_component *op = d_left (dc);
5821 struct demangle_component *operand = d_right (dc);
5822 const char *code = NULL;
5824 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5826 code = op->u.s_operator.op->code;
5827 if (!strcmp (code, "ad"))
5829 /* Don't print the argument list for the address of a
5830 function. */
5831 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5832 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5833 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5834 operand = d_left (operand);
5836 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5838 /* This indicates a suffix operator. */
5839 operand = d_left (operand);
5840 d_print_subexpr (dpi, options, operand);
5841 d_print_expr_op (dpi, options, op);
5842 return;
5846 /* For sizeof..., just print the pack length. */
5847 if (code && !strcmp (code, "sZ"))
5849 struct demangle_component *a = d_find_pack (dpi, operand);
5850 int len = d_pack_length (a);
5851 d_append_num (dpi, len);
5852 return;
5854 else if (code && !strcmp (code, "sP"))
5856 int len = d_args_length (dpi, operand);
5857 d_append_num (dpi, len);
5858 return;
5861 if (op->type != DEMANGLE_COMPONENT_CAST)
5862 d_print_expr_op (dpi, options, op);
5863 else
5865 d_append_char (dpi, '(');
5866 d_print_cast (dpi, options, op);
5867 d_append_char (dpi, ')');
5869 if (code && !strcmp (code, "gs"))
5870 /* Avoid parens after '::'. */
5871 d_print_comp (dpi, options, operand);
5872 else if (code && !strcmp (code, "st"))
5873 /* Always print parens for sizeof (type). */
5875 d_append_char (dpi, '(');
5876 d_print_comp (dpi, options, operand);
5877 d_append_char (dpi, ')');
5879 else
5880 d_print_subexpr (dpi, options, operand);
5882 return;
5884 case DEMANGLE_COMPONENT_BINARY:
5885 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5887 d_print_error (dpi);
5888 return;
5891 if (op_is_new_cast (d_left (dc)))
5893 d_print_expr_op (dpi, options, d_left (dc));
5894 d_append_char (dpi, '<');
5895 d_print_comp (dpi, options, d_left (d_right (dc)));
5896 d_append_string (dpi, ">(");
5897 d_print_comp (dpi, options, d_right (d_right (dc)));
5898 d_append_char (dpi, ')');
5899 return;
5902 if (d_maybe_print_fold_expression (dpi, options, dc))
5903 return;
5905 if (d_maybe_print_designated_init (dpi, options, dc))
5906 return;
5908 /* We wrap an expression which uses the greater-than operator in
5909 an extra layer of parens so that it does not get confused
5910 with the '>' which ends the template parameters. */
5911 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5912 && d_left (dc)->u.s_operator.op->len == 1
5913 && d_left (dc)->u.s_operator.op->name[0] == '>')
5914 d_append_char (dpi, '(');
5916 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5917 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5919 /* Function call used in an expression should not have printed types
5920 of the function arguments. Values of the function arguments still
5921 get printed below. */
5923 const struct demangle_component *func = d_left (d_right (dc));
5925 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5926 d_print_error (dpi);
5927 d_print_subexpr (dpi, options, d_left (func));
5929 else
5930 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5931 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5933 d_append_char (dpi, '[');
5934 d_print_comp (dpi, options, d_right (d_right (dc)));
5935 d_append_char (dpi, ']');
5937 else
5939 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5940 d_print_expr_op (dpi, options, d_left (dc));
5941 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5944 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5945 && d_left (dc)->u.s_operator.op->len == 1
5946 && d_left (dc)->u.s_operator.op->name[0] == '>')
5947 d_append_char (dpi, ')');
5949 return;
5951 case DEMANGLE_COMPONENT_BINARY_ARGS:
5952 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5953 d_print_error (dpi);
5954 return;
5956 case DEMANGLE_COMPONENT_TRINARY:
5957 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5958 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5960 d_print_error (dpi);
5961 return;
5963 if (d_maybe_print_fold_expression (dpi, options, dc))
5964 return;
5965 if (d_maybe_print_designated_init (dpi, options, dc))
5966 return;
5968 struct demangle_component *op = d_left (dc);
5969 struct demangle_component *first = d_left (d_right (dc));
5970 struct demangle_component *second = d_left (d_right (d_right (dc)));
5971 struct demangle_component *third = d_right (d_right (d_right (dc)));
5973 if (!strcmp (op->u.s_operator.op->code, "qu"))
5975 d_print_subexpr (dpi, options, first);
5976 d_print_expr_op (dpi, options, op);
5977 d_print_subexpr (dpi, options, second);
5978 d_append_string (dpi, " : ");
5979 d_print_subexpr (dpi, options, third);
5981 else
5983 d_append_string (dpi, "new ");
5984 if (d_left (first) != NULL)
5986 d_print_subexpr (dpi, options, first);
5987 d_append_char (dpi, ' ');
5989 d_print_comp (dpi, options, second);
5990 if (third)
5991 d_print_subexpr (dpi, options, third);
5994 return;
5996 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5997 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5998 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5999 d_print_error (dpi);
6000 return;
6002 case DEMANGLE_COMPONENT_LITERAL:
6003 case DEMANGLE_COMPONENT_LITERAL_NEG:
6005 enum d_builtin_type_print tp;
6007 /* For some builtin types, produce simpler output. */
6008 tp = D_PRINT_DEFAULT;
6009 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
6011 tp = d_left (dc)->u.s_builtin.type->print;
6012 switch (tp)
6014 case D_PRINT_INT:
6015 case D_PRINT_UNSIGNED:
6016 case D_PRINT_LONG:
6017 case D_PRINT_UNSIGNED_LONG:
6018 case D_PRINT_LONG_LONG:
6019 case D_PRINT_UNSIGNED_LONG_LONG:
6020 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
6022 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6023 d_append_char (dpi, '-');
6024 d_print_comp (dpi, options, d_right (dc));
6025 switch (tp)
6027 default:
6028 break;
6029 case D_PRINT_UNSIGNED:
6030 d_append_char (dpi, 'u');
6031 break;
6032 case D_PRINT_LONG:
6033 d_append_char (dpi, 'l');
6034 break;
6035 case D_PRINT_UNSIGNED_LONG:
6036 d_append_string (dpi, "ul");
6037 break;
6038 case D_PRINT_LONG_LONG:
6039 d_append_string (dpi, "ll");
6040 break;
6041 case D_PRINT_UNSIGNED_LONG_LONG:
6042 d_append_string (dpi, "ull");
6043 break;
6045 return;
6047 break;
6049 case D_PRINT_BOOL:
6050 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
6051 && d_right (dc)->u.s_name.len == 1
6052 && dc->type == DEMANGLE_COMPONENT_LITERAL)
6054 switch (d_right (dc)->u.s_name.s[0])
6056 case '0':
6057 d_append_string (dpi, "false");
6058 return;
6059 case '1':
6060 d_append_string (dpi, "true");
6061 return;
6062 default:
6063 break;
6066 break;
6068 default:
6069 break;
6073 d_append_char (dpi, '(');
6074 d_print_comp (dpi, options, d_left (dc));
6075 d_append_char (dpi, ')');
6076 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6077 d_append_char (dpi, '-');
6078 if (tp == D_PRINT_FLOAT)
6079 d_append_char (dpi, '[');
6080 d_print_comp (dpi, options, d_right (dc));
6081 if (tp == D_PRINT_FLOAT)
6082 d_append_char (dpi, ']');
6084 return;
6086 case DEMANGLE_COMPONENT_VENDOR_EXPR:
6087 d_print_comp (dpi, options, d_left (dc));
6088 d_append_char (dpi, '(');
6089 d_print_comp (dpi, options, d_right (dc));
6090 d_append_char (dpi, ')');
6091 return;
6093 case DEMANGLE_COMPONENT_NUMBER:
6094 d_append_num (dpi, dc->u.s_number.number);
6095 return;
6097 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
6098 d_append_string (dpi, "java resource ");
6099 d_print_comp (dpi, options, d_left (dc));
6100 return;
6102 case DEMANGLE_COMPONENT_COMPOUND_NAME:
6103 d_print_comp (dpi, options, d_left (dc));
6104 d_print_comp (dpi, options, d_right (dc));
6105 return;
6107 case DEMANGLE_COMPONENT_CHARACTER:
6108 d_append_char (dpi, dc->u.s_character.character);
6109 return;
6111 case DEMANGLE_COMPONENT_DECLTYPE:
6112 d_append_string (dpi, "decltype (");
6113 d_print_comp (dpi, options, d_left (dc));
6114 d_append_char (dpi, ')');
6115 return;
6117 case DEMANGLE_COMPONENT_PACK_EXPANSION:
6119 struct demangle_component *a = NULL;
6121 if (!dpi->lambda_tpl_parms)
6122 a = d_find_pack (dpi, d_left (dc));
6123 if (a == NULL)
6125 /* d_find_pack won't find anything if the only packs involved
6126 in this expansion are function parameter packs; in that
6127 case, just print the pattern and "...". */
6128 d_print_subexpr (dpi, options, d_left (dc));
6129 d_append_string (dpi, "...");
6131 else
6133 int len = d_pack_length (a);
6134 int i;
6136 dc = d_left (dc);
6137 for (i = 0; i < len; ++i)
6139 if (i)
6140 d_append_string (dpi, ", ");
6141 dpi->pack_index = i;
6142 d_print_comp (dpi, options, dc);
6146 return;
6148 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
6150 long num = dc->u.s_number.number;
6151 if (num == 0)
6152 d_append_string (dpi, "this");
6153 else
6155 d_append_string (dpi, "{parm#");
6156 d_append_num (dpi, num);
6157 d_append_char (dpi, '}');
6160 return;
6162 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
6163 d_append_string (dpi, "global constructors keyed to ");
6164 d_print_comp (dpi, options, dc->u.s_binary.left);
6165 return;
6167 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
6168 d_append_string (dpi, "global destructors keyed to ");
6169 d_print_comp (dpi, options, dc->u.s_binary.left);
6170 return;
6172 case DEMANGLE_COMPONENT_LAMBDA:
6174 d_append_string (dpi, "{lambda");
6175 struct demangle_component *parms = dc->u.s_unary_num.sub;
6176 struct d_print_template dpt;
6177 /* Generic lambda auto parms are mangled as the (synthedic) template
6178 type parm they are. We need to tell the printer that (a) we're in
6179 a lambda, and (b) the number of synthetic parms. */
6180 int saved_tpl_parms = dpi->lambda_tpl_parms;
6181 dpi->lambda_tpl_parms = 0;
6182 /* Hang any lambda head as-if template args. */
6183 dpt.template_decl = NULL;
6184 dpt.next = dpi->templates;
6185 dpi->templates = &dpt;
6186 if (parms && parms->type == DEMANGLE_COMPONENT_TEMPLATE_HEAD)
6188 dpt.template_decl = parms;
6190 d_append_char (dpi, '<');
6191 struct demangle_component *parm;
6192 for (parm = d_left (parms); parm; parm = d_right (parm))
6194 if (dpi->lambda_tpl_parms++)
6195 d_append_string (dpi, ", ");
6196 d_print_comp (dpi, options, parm);
6197 d_append_char (dpi, ' ');
6198 if (parm->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
6199 parm = d_left (parm);
6200 d_print_lambda_parm_name (dpi, parm->type,
6201 dpi->lambda_tpl_parms - 1);
6203 d_append_char (dpi, '>');
6205 parms = d_right (parms);
6207 dpi->lambda_tpl_parms++;
6209 d_append_char (dpi, '(');
6210 d_print_comp (dpi, options, parms);
6211 dpi->lambda_tpl_parms = saved_tpl_parms;
6212 dpi->templates = dpt.next;
6213 d_append_string (dpi, ")#");
6214 d_append_num (dpi, dc->u.s_unary_num.num + 1);
6215 d_append_char (dpi, '}');
6217 return;
6219 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
6220 d_append_string (dpi, "{unnamed type#");
6221 d_append_num (dpi, dc->u.s_number.number + 1);
6222 d_append_char (dpi, '}');
6223 return;
6225 case DEMANGLE_COMPONENT_CLONE:
6226 d_print_comp (dpi, options, d_left (dc));
6227 d_append_string (dpi, " [clone ");
6228 d_print_comp (dpi, options, d_right (dc));
6229 d_append_char (dpi, ']');
6230 return;
6232 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
6234 d_append_char (dpi, '<');
6235 int count = 0;
6236 struct demangle_component *parm;
6237 for (parm = d_left (dc); parm; parm = d_right (parm))
6239 if (count++)
6240 d_append_string (dpi, ", ");
6241 d_print_comp (dpi, options, parm);
6243 d_append_char (dpi, '>');
6245 return;
6247 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
6248 d_append_string (dpi, "typename");
6249 return;
6251 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
6252 d_print_comp (dpi, options, d_left (dc));
6253 return;
6255 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
6256 d_append_string (dpi, "template");
6257 d_print_comp (dpi, options, d_left (dc));
6258 d_append_string (dpi, " class");
6259 return;
6261 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
6262 d_print_comp (dpi, options, d_left (dc));
6263 d_append_string (dpi, "...");
6264 return;
6266 default:
6267 d_print_error (dpi);
6268 return;
6272 static void
6273 d_print_comp (struct d_print_info *dpi, int options,
6274 struct demangle_component *dc)
6276 struct d_component_stack self;
6277 if (dc == NULL || dc->d_printing > 1 || dpi->recursion > MAX_RECURSION_COUNT)
6279 d_print_error (dpi);
6280 return;
6283 dc->d_printing++;
6284 dpi->recursion++;
6286 self.dc = dc;
6287 self.parent = dpi->component_stack;
6288 dpi->component_stack = &self;
6290 d_print_comp_inner (dpi, options, dc);
6292 dpi->component_stack = self.parent;
6293 dc->d_printing--;
6294 dpi->recursion--;
6297 /* Print a Java dentifier. For Java we try to handle encoded extended
6298 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
6299 so we don't it for C++. Characters are encoded as
6300 __U<hex-char>+_. */
6302 static void
6303 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
6305 const char *p;
6306 const char *end;
6308 end = name + len;
6309 for (p = name; p < end; ++p)
6311 if (end - p > 3
6312 && p[0] == '_'
6313 && p[1] == '_'
6314 && p[2] == 'U')
6316 unsigned long c;
6317 const char *q;
6319 c = 0;
6320 for (q = p + 3; q < end; ++q)
6322 int dig;
6324 if (IS_DIGIT (*q))
6325 dig = *q - '0';
6326 else if (*q >= 'A' && *q <= 'F')
6327 dig = *q - 'A' + 10;
6328 else if (*q >= 'a' && *q <= 'f')
6329 dig = *q - 'a' + 10;
6330 else
6331 break;
6333 c = c * 16 + dig;
6335 /* If the Unicode character is larger than 256, we don't try
6336 to deal with it here. FIXME. */
6337 if (q < end && *q == '_' && c < 256)
6339 d_append_char (dpi, c);
6340 p = q;
6341 continue;
6345 d_append_char (dpi, *p);
6349 /* Print a list of modifiers. SUFFIX is 1 if we are printing
6350 qualifiers on this after printing a function. */
6352 static void
6353 d_print_mod_list (struct d_print_info *dpi, int options,
6354 struct d_print_mod *mods, int suffix)
6356 struct d_print_template *hold_dpt;
6358 if (mods == NULL || d_print_saw_error (dpi))
6359 return;
6361 if (mods->printed
6362 || (! suffix
6363 && (is_fnqual_component_type (mods->mod->type))))
6365 d_print_mod_list (dpi, options, mods->next, suffix);
6366 return;
6369 mods->printed = 1;
6371 hold_dpt = dpi->templates;
6372 dpi->templates = mods->templates;
6374 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
6376 d_print_function_type (dpi, options, mods->mod, mods->next);
6377 dpi->templates = hold_dpt;
6378 return;
6380 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6382 d_print_array_type (dpi, options, mods->mod, mods->next);
6383 dpi->templates = hold_dpt;
6384 return;
6386 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
6388 struct d_print_mod *hold_modifiers;
6389 struct demangle_component *dc;
6391 /* When this is on the modifier stack, we have pulled any
6392 qualifiers off the right argument already. Otherwise, we
6393 print it as usual, but don't let the left argument see any
6394 modifiers. */
6396 hold_modifiers = dpi->modifiers;
6397 dpi->modifiers = NULL;
6398 d_print_comp (dpi, options, d_left (mods->mod));
6399 dpi->modifiers = hold_modifiers;
6401 if ((options & DMGL_JAVA) == 0)
6402 d_append_string (dpi, "::");
6403 else
6404 d_append_char (dpi, '.');
6406 dc = d_right (mods->mod);
6408 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
6410 d_append_string (dpi, "{default arg#");
6411 d_append_num (dpi, dc->u.s_unary_num.num + 1);
6412 d_append_string (dpi, "}::");
6413 dc = dc->u.s_unary_num.sub;
6416 while (is_fnqual_component_type (dc->type))
6417 dc = d_left (dc);
6419 d_print_comp (dpi, options, dc);
6421 dpi->templates = hold_dpt;
6422 return;
6425 d_print_mod (dpi, options, mods->mod);
6427 dpi->templates = hold_dpt;
6429 d_print_mod_list (dpi, options, mods->next, suffix);
6432 /* Print a modifier. */
6434 static void
6435 d_print_mod (struct d_print_info *dpi, int options,
6436 struct demangle_component *mod)
6438 switch (mod->type)
6440 case DEMANGLE_COMPONENT_RESTRICT:
6441 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6442 d_append_string (dpi, " restrict");
6443 return;
6444 case DEMANGLE_COMPONENT_VOLATILE:
6445 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6446 d_append_string (dpi, " volatile");
6447 return;
6448 case DEMANGLE_COMPONENT_CONST:
6449 case DEMANGLE_COMPONENT_CONST_THIS:
6450 d_append_string (dpi, " const");
6451 return;
6452 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
6453 d_append_string (dpi, " transaction_safe");
6454 return;
6455 case DEMANGLE_COMPONENT_NOEXCEPT:
6456 d_append_string (dpi, " noexcept");
6457 if (d_right (mod))
6459 d_append_char (dpi, '(');
6460 d_print_comp (dpi, options, d_right (mod));
6461 d_append_char (dpi, ')');
6463 return;
6464 case DEMANGLE_COMPONENT_THROW_SPEC:
6465 d_append_string (dpi, " throw");
6466 if (d_right (mod))
6468 d_append_char (dpi, '(');
6469 d_print_comp (dpi, options, d_right (mod));
6470 d_append_char (dpi, ')');
6472 return;
6473 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6474 d_append_char (dpi, ' ');
6475 d_print_comp (dpi, options, d_right (mod));
6476 return;
6477 case DEMANGLE_COMPONENT_POINTER:
6478 /* There is no pointer symbol in Java. */
6479 if ((options & DMGL_JAVA) == 0)
6480 d_append_char (dpi, '*');
6481 return;
6482 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6483 /* For the ref-qualifier, put a space before the &. */
6484 d_append_char (dpi, ' ');
6485 /* FALLTHRU */
6486 case DEMANGLE_COMPONENT_REFERENCE:
6487 d_append_char (dpi, '&');
6488 return;
6489 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6490 d_append_char (dpi, ' ');
6491 /* FALLTHRU */
6492 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6493 d_append_string (dpi, "&&");
6494 return;
6495 case DEMANGLE_COMPONENT_COMPLEX:
6496 d_append_string (dpi, " _Complex");
6497 return;
6498 case DEMANGLE_COMPONENT_IMAGINARY:
6499 d_append_string (dpi, " _Imaginary");
6500 return;
6501 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6502 if (d_last_char (dpi) != '(')
6503 d_append_char (dpi, ' ');
6504 d_print_comp (dpi, options, d_left (mod));
6505 d_append_string (dpi, "::*");
6506 return;
6507 case DEMANGLE_COMPONENT_TYPED_NAME:
6508 d_print_comp (dpi, options, d_left (mod));
6509 return;
6510 case DEMANGLE_COMPONENT_VECTOR_TYPE:
6511 d_append_string (dpi, " __vector(");
6512 d_print_comp (dpi, options, d_left (mod));
6513 d_append_char (dpi, ')');
6514 return;
6516 default:
6517 /* Otherwise, we have something that won't go back on the
6518 modifier stack, so we can just print it. */
6519 d_print_comp (dpi, options, mod);
6520 return;
6524 /* Print a function type, except for the return type. */
6526 static void
6527 d_print_function_type (struct d_print_info *dpi, int options,
6528 struct demangle_component *dc,
6529 struct d_print_mod *mods)
6531 int need_paren;
6532 int need_space;
6533 struct d_print_mod *p;
6534 struct d_print_mod *hold_modifiers;
6536 need_paren = 0;
6537 need_space = 0;
6538 for (p = mods; p != NULL; p = p->next)
6540 if (p->printed)
6541 break;
6543 switch (p->mod->type)
6545 case DEMANGLE_COMPONENT_POINTER:
6546 case DEMANGLE_COMPONENT_REFERENCE:
6547 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6548 need_paren = 1;
6549 break;
6550 case DEMANGLE_COMPONENT_RESTRICT:
6551 case DEMANGLE_COMPONENT_VOLATILE:
6552 case DEMANGLE_COMPONENT_CONST:
6553 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6554 case DEMANGLE_COMPONENT_COMPLEX:
6555 case DEMANGLE_COMPONENT_IMAGINARY:
6556 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6557 need_space = 1;
6558 need_paren = 1;
6559 break;
6560 FNQUAL_COMPONENT_CASE:
6561 break;
6562 default:
6563 break;
6565 if (need_paren)
6566 break;
6569 if (need_paren)
6571 if (! need_space)
6573 if (d_last_char (dpi) != '('
6574 && d_last_char (dpi) != '*')
6575 need_space = 1;
6577 if (need_space && d_last_char (dpi) != ' ')
6578 d_append_char (dpi, ' ');
6579 d_append_char (dpi, '(');
6582 hold_modifiers = dpi->modifiers;
6583 dpi->modifiers = NULL;
6585 d_print_mod_list (dpi, options, mods, 0);
6587 if (need_paren)
6588 d_append_char (dpi, ')');
6590 d_append_char (dpi, '(');
6592 if (d_right (dc) != NULL)
6593 d_print_comp (dpi, options, d_right (dc));
6595 d_append_char (dpi, ')');
6597 d_print_mod_list (dpi, options, mods, 1);
6599 dpi->modifiers = hold_modifiers;
6602 /* Print an array type, except for the element type. */
6604 static void
6605 d_print_array_type (struct d_print_info *dpi, int options,
6606 struct demangle_component *dc,
6607 struct d_print_mod *mods)
6609 int need_space;
6611 need_space = 1;
6612 if (mods != NULL)
6614 int need_paren;
6615 struct d_print_mod *p;
6617 need_paren = 0;
6618 for (p = mods; p != NULL; p = p->next)
6620 if (! p->printed)
6622 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6624 need_space = 0;
6625 break;
6627 else
6629 need_paren = 1;
6630 need_space = 1;
6631 break;
6636 if (need_paren)
6637 d_append_string (dpi, " (");
6639 d_print_mod_list (dpi, options, mods, 0);
6641 if (need_paren)
6642 d_append_char (dpi, ')');
6645 if (need_space)
6646 d_append_char (dpi, ' ');
6648 d_append_char (dpi, '[');
6650 if (d_left (dc) != NULL)
6651 d_print_comp (dpi, options, d_left (dc));
6653 d_append_char (dpi, ']');
6656 /* Print an operator in an expression. */
6658 static void
6659 d_print_expr_op (struct d_print_info *dpi, int options,
6660 struct demangle_component *dc)
6662 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
6663 d_append_buffer (dpi, dc->u.s_operator.op->name,
6664 dc->u.s_operator.op->len);
6665 else
6666 d_print_comp (dpi, options, dc);
6669 /* Print a cast. */
6671 static void
6672 d_print_cast (struct d_print_info *dpi, int options,
6673 struct demangle_component *dc)
6675 d_print_comp (dpi, options, d_left (dc));
6678 /* Print a conversion operator. */
6680 static void
6681 d_print_conversion (struct d_print_info *dpi, int options,
6682 struct demangle_component *dc)
6684 struct d_print_template dpt;
6686 /* For a conversion operator, we need the template parameters from
6687 the enclosing template in scope for processing the type. */
6688 if (dpi->current_template != NULL)
6690 dpt.next = dpi->templates;
6691 dpi->templates = &dpt;
6692 dpt.template_decl = dpi->current_template;
6695 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
6697 d_print_comp (dpi, options, d_left (dc));
6698 if (dpi->current_template != NULL)
6699 dpi->templates = dpt.next;
6701 else
6703 d_print_comp (dpi, options, d_left (d_left (dc)));
6705 /* For a templated cast operator, we need to remove the template
6706 parameters from scope after printing the operator name,
6707 so we need to handle the template printing here. */
6708 if (dpi->current_template != NULL)
6709 dpi->templates = dpt.next;
6711 if (d_last_char (dpi) == '<')
6712 d_append_char (dpi, ' ');
6713 d_append_char (dpi, '<');
6714 d_print_comp (dpi, options, d_right (d_left (dc)));
6715 /* Avoid generating two consecutive '>' characters, to avoid
6716 the C++ syntactic ambiguity. */
6717 if (d_last_char (dpi) == '>')
6718 d_append_char (dpi, ' ');
6719 d_append_char (dpi, '>');
6723 /* Initialize the information structure we use to pass around
6724 information. */
6726 CP_STATIC_IF_GLIBCPP_V3
6727 void
6728 cplus_demangle_init_info (const char *mangled, int options, size_t len,
6729 struct d_info *di)
6731 di->s = mangled;
6732 di->send = mangled + len;
6733 di->options = options;
6735 di->n = mangled;
6737 /* We cannot need more components than twice the number of chars in
6738 the mangled string. Most components correspond directly to
6739 chars, but the ARGLIST types are exceptions. */
6740 di->num_comps = 2 * len;
6741 di->next_comp = 0;
6743 /* Similarly, we cannot need more substitutions than there are
6744 chars in the mangled string. */
6745 di->num_subs = len;
6746 di->next_sub = 0;
6748 di->last_name = NULL;
6750 di->expansion = 0;
6751 di->is_expression = 0;
6752 di->is_conversion = 0;
6753 di->recursion_level = 0;
6756 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6757 mangled name, return strings in repeated callback giving the demangled
6758 name. OPTIONS is the usual libiberty demangler options. On success,
6759 this returns 1. On failure, returns 0. */
6761 static int
6762 d_demangle_callback (const char *mangled, int options,
6763 demangle_callbackref callback, void *opaque)
6765 enum
6767 DCT_TYPE,
6768 DCT_MANGLED,
6769 DCT_GLOBAL_CTORS,
6770 DCT_GLOBAL_DTORS
6772 type;
6773 struct d_info di;
6774 struct demangle_component *dc;
6775 int status;
6777 if (mangled[0] == '_' && mangled[1] == 'Z')
6778 type = DCT_MANGLED;
6779 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6780 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6781 && (mangled[9] == 'D' || mangled[9] == 'I')
6782 && mangled[10] == '_')
6783 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
6784 else
6786 if ((options & DMGL_TYPES) == 0)
6787 return 0;
6788 type = DCT_TYPE;
6791 di.unresolved_name_state = 1;
6793 again:
6794 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6796 /* PR 87675 - Check for a mangled string that is so long
6797 that we do not have enough stack space to demangle it. */
6798 if (((options & DMGL_NO_RECURSE_LIMIT) == 0)
6799 /* This check is a bit arbitrary, since what we really want to do is to
6800 compare the sizes of the di.comps and di.subs arrays against the
6801 amount of stack space remaining. But there is no portable way to do
6802 this, so instead we use the recursion limit as a guide to the maximum
6803 size of the arrays. */
6804 && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT)
6806 /* FIXME: We need a way to indicate that a stack limit has been reached. */
6807 return 0;
6811 #if 0 /* in valgrind */
6812 #ifdef CP_DYNAMIC_ARRAYS
6813 __extension__ struct demangle_component comps[di.num_comps];
6814 __extension__ struct demangle_component *subs[di.num_subs];
6816 di.comps = comps;
6817 di.subs = subs;
6818 #else
6819 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6820 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6821 #endif
6822 #else
6823 /* Allocate memory dynamically to avoid VLAs as valgrind stack
6824 is a scarce resource */
6825 di.comps = xmalloc (di.num_comps * sizeof (*di.comps));
6826 di.subs = xmalloc (di.num_subs * sizeof (*di.subs));
6827 #endif /* ! in valgrind */
6829 switch (type)
6831 case DCT_TYPE:
6832 dc = cplus_demangle_type (&di);
6833 break;
6834 case DCT_MANGLED:
6835 dc = cplus_demangle_mangled_name (&di, 1);
6836 break;
6837 case DCT_GLOBAL_CTORS:
6838 case DCT_GLOBAL_DTORS:
6839 d_advance (&di, 11);
6840 dc = d_make_comp (&di,
6841 (type == DCT_GLOBAL_CTORS
6842 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6843 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
6844 d_make_demangle_mangled_name (&di, d_str (&di)),
6845 NULL);
6846 d_advance (&di, strlen (d_str (&di)));
6847 break;
6848 default:
6849 abort (); /* We have listed all the cases. */
6852 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6853 mangled string, then we didn't successfully demangle it. If
6854 DMGL_PARAMS is not set, we didn't look at the trailing
6855 parameters. */
6856 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
6857 dc = NULL;
6859 /* See discussion in d_unresolved_name. */
6860 if (dc == NULL && di.unresolved_name_state == -1)
6862 di.unresolved_name_state = 0;
6863 goto again;
6866 #ifdef CP_DEMANGLE_DEBUG
6867 d_dump (dc, 0);
6868 #endif
6870 status = (dc != NULL)
6871 ? cplus_demangle_print_callback (options, dc, callback, opaque)
6872 : 0;
6875 #if 0 /* in valgrind */
6876 #else
6877 free (di.comps);
6878 free (di.subs);
6879 #endif /* in valgrind */
6881 return status;
6884 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6885 name, return a buffer allocated with malloc holding the demangled
6886 name. OPTIONS is the usual libiberty demangler options. On
6887 success, this sets *PALC to the allocated size of the returned
6888 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6889 a memory allocation failure, and returns NULL. */
6891 static char *
6892 d_demangle (const char *mangled, int options, size_t *palc)
6894 struct d_growable_string dgs;
6895 int status;
6897 d_growable_string_init (&dgs, 0);
6899 status = d_demangle_callback (mangled, options,
6900 d_growable_string_callback_adapter, &dgs);
6901 if (status == 0)
6903 free (dgs.buf);
6904 *palc = 0;
6905 return NULL;
6908 *palc = dgs.allocation_failure ? 1 : dgs.alc;
6909 return dgs.buf;
6912 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6914 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6916 /* ia64 ABI-mandated entry point in the C++ runtime library for
6917 performing demangling. MANGLED_NAME is a NUL-terminated character
6918 string containing the name to be demangled.
6920 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6921 *LENGTH bytes, into which the demangled name is stored. If
6922 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6923 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6924 is placed in a region of memory allocated with malloc.
6926 If LENGTH is non-NULL, the length of the buffer containing the
6927 demangled name, is placed in *LENGTH.
6929 The return value is a pointer to the start of the NUL-terminated
6930 demangled name, or NULL if the demangling fails. The caller is
6931 responsible for deallocating this memory using free.
6933 *STATUS is set to one of the following values:
6934 0: The demangling operation succeeded.
6935 -1: A memory allocation failure occurred.
6936 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6937 -3: One of the arguments is invalid.
6939 The demangling is performed using the C++ ABI mangling rules, with
6940 GNU extensions. */
6942 char *
6943 __cxa_demangle (const char *mangled_name, char *output_buffer,
6944 size_t *length, int *status)
6946 char *demangled;
6947 size_t alc;
6949 if (mangled_name == NULL)
6951 if (status != NULL)
6952 *status = -3;
6953 return NULL;
6956 if (output_buffer != NULL && length == NULL)
6958 if (status != NULL)
6959 *status = -3;
6960 return NULL;
6963 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6965 if (demangled == NULL)
6967 if (status != NULL)
6969 if (alc == 1)
6970 *status = -1;
6971 else
6972 *status = -2;
6974 return NULL;
6977 if (output_buffer == NULL)
6979 if (length != NULL)
6980 *length = alc;
6982 else
6984 if (strlen (demangled) < *length)
6986 strcpy (output_buffer, demangled);
6987 free (demangled);
6988 demangled = output_buffer;
6990 else
6992 free (output_buffer);
6993 *length = alc;
6997 if (status != NULL)
6998 *status = 0;
7000 return demangled;
7003 extern int __gcclibcxx_demangle_callback (const char *,
7004 void (*)
7005 (const char *, size_t, void *),
7006 void *);
7008 /* Alternative, allocationless entry point in the C++ runtime library
7009 for performing demangling. MANGLED_NAME is a NUL-terminated character
7010 string containing the name to be demangled.
7012 CALLBACK is a callback function, called with demangled string
7013 segments as demangling progresses; it is called at least once,
7014 but may be called more than once. OPAQUE is a generalized pointer
7015 used as a callback argument.
7017 The return code is one of the following values, equivalent to
7018 the STATUS values of __cxa_demangle() (excluding -1, since this
7019 function performs no memory allocations):
7020 0: The demangling operation succeeded.
7021 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
7022 -3: One of the arguments is invalid.
7024 The demangling is performed using the C++ ABI mangling rules, with
7025 GNU extensions. */
7028 __gcclibcxx_demangle_callback (const char *mangled_name,
7029 void (*callback) (const char *, size_t, void *),
7030 void *opaque)
7032 int status;
7034 if (mangled_name == NULL || callback == NULL)
7035 return -3;
7037 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
7038 callback, opaque);
7039 if (status == 0)
7040 return -2;
7042 return 0;
7045 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
7047 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
7048 mangled name, return a buffer allocated with malloc holding the
7049 demangled name. Otherwise, return NULL. */
7051 char *
7052 cplus_demangle_v3 (const char *mangled, int options)
7054 size_t alc;
7056 return d_demangle (mangled, options, &alc);
7060 cplus_demangle_v3_callback (const char *mangled, int options,
7061 demangle_callbackref callback, void *opaque)
7063 return d_demangle_callback (mangled, options, callback, opaque);
7066 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
7067 conventions, but the output formatting is a little different.
7068 This instructs the C++ demangler not to emit pointer characters ("*"), to
7069 use Java's namespace separator symbol ("." instead of "::"), and to output
7070 JArray<TYPE> as TYPE[]. */
7072 char *
7073 java_demangle_v3 (const char *mangled)
7075 size_t alc;
7077 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
7081 java_demangle_v3_callback (const char *mangled,
7082 demangle_callbackref callback, void *opaque)
7084 return d_demangle_callback (mangled,
7085 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
7086 callback, opaque);
7089 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
7091 #ifndef IN_GLIBCPP_V3
7093 /* Demangle a string in order to find out whether it is a constructor
7094 or destructor. Return non-zero on success. Set *CTOR_KIND and
7095 *DTOR_KIND appropriately. */
7097 static int
7098 is_ctor_or_dtor (const char *mangled,
7099 enum gnu_v3_ctor_kinds *ctor_kind,
7100 enum gnu_v3_dtor_kinds *dtor_kind)
7102 struct d_info di;
7103 struct demangle_component *dc;
7104 int ret;
7106 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
7107 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
7109 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
7112 #if 0 /* in valgrind */
7113 #ifdef CP_DYNAMIC_ARRAYS
7114 __extension__ struct demangle_component comps[di.num_comps];
7115 __extension__ struct demangle_component *subs[di.num_subs];
7117 di.comps = comps;
7118 di.subs = subs;
7119 #else
7120 di.comps = alloca (di.num_comps * sizeof (*di.comps));
7121 di.subs = alloca (di.num_subs * sizeof (*di.subs));
7122 #endif
7123 #else
7124 /* Allocate memory dynamically to avoid VLAs as valgrind stack
7125 is a scarce resource */
7126 di.comps = xmalloc (di.num_comps * sizeof (*di.comps));
7127 di.subs = xmalloc (di.num_subs * sizeof (*di.subs));
7128 #endif /* ! in valgrind */
7129 dc = cplus_demangle_mangled_name (&di, 1);
7131 /* Note that because we did not pass DMGL_PARAMS, we don't expect
7132 to demangle the entire string. */
7134 ret = 0;
7135 while (dc != NULL)
7137 switch (dc->type)
7139 /* These cannot appear on a constructor or destructor. */
7140 case DEMANGLE_COMPONENT_RESTRICT_THIS:
7141 case DEMANGLE_COMPONENT_VOLATILE_THIS:
7142 case DEMANGLE_COMPONENT_CONST_THIS:
7143 case DEMANGLE_COMPONENT_REFERENCE_THIS:
7144 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
7145 default:
7146 dc = NULL;
7147 break;
7148 case DEMANGLE_COMPONENT_TYPED_NAME:
7149 case DEMANGLE_COMPONENT_TEMPLATE:
7150 dc = d_left (dc);
7151 break;
7152 case DEMANGLE_COMPONENT_QUAL_NAME:
7153 case DEMANGLE_COMPONENT_LOCAL_NAME:
7154 dc = d_right (dc);
7155 break;
7156 case DEMANGLE_COMPONENT_CTOR:
7157 *ctor_kind = dc->u.s_ctor.kind;
7158 ret = 1;
7159 dc = NULL;
7160 break;
7161 case DEMANGLE_COMPONENT_DTOR:
7162 *dtor_kind = dc->u.s_dtor.kind;
7163 ret = 1;
7164 dc = NULL;
7165 break;
7170 #if 0 /* in valgrind */
7171 #else
7172 free (di.comps);
7173 free (di.subs);
7174 #endif /* in valgrind */
7176 return ret;
7179 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
7180 name. A non-zero return indicates the type of constructor. */
7182 enum gnu_v3_ctor_kinds
7183 is_gnu_v3_mangled_ctor (const char *name)
7185 enum gnu_v3_ctor_kinds ctor_kind;
7186 enum gnu_v3_dtor_kinds dtor_kind;
7188 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7189 return (enum gnu_v3_ctor_kinds) 0;
7190 return ctor_kind;
7194 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
7195 name. A non-zero return indicates the type of destructor. */
7197 enum gnu_v3_dtor_kinds
7198 is_gnu_v3_mangled_dtor (const char *name)
7200 enum gnu_v3_ctor_kinds ctor_kind;
7201 enum gnu_v3_dtor_kinds dtor_kind;
7203 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7204 return (enum gnu_v3_dtor_kinds) 0;
7205 return dtor_kind;
7208 #endif /* IN_GLIBCPP_V3 */
7210 #ifdef STANDALONE_DEMANGLER
7212 #if 0 /* in valgrind */
7213 #include "getopt.h"
7214 #include "dyn-string.h"
7215 #endif /* ! in valgrind */
7217 static void print_usage (FILE* fp, int exit_value);
7219 #define IS_ALPHA(CHAR) \
7220 (((CHAR) >= 'a' && (CHAR) <= 'z') \
7221 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
7223 /* Non-zero if CHAR is a character than can occur in a mangled name. */
7224 #define is_mangled_char(CHAR) \
7225 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
7226 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
7228 /* The name of this program, as invoked. */
7229 const char* program_name;
7231 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
7233 static void
7234 print_usage (FILE* fp, int exit_value)
7236 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
7237 fprintf (fp, "Options:\n");
7238 fprintf (fp, " -h,--help Display this message.\n");
7239 fprintf (fp, " -p,--no-params Don't display function parameters\n");
7240 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
7241 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
7243 exit (exit_value);
7246 /* Option specification for getopt_long. */
7247 static const struct option long_options[] =
7249 { "help", no_argument, NULL, 'h' },
7250 { "no-params", no_argument, NULL, 'p' },
7251 { "verbose", no_argument, NULL, 'v' },
7252 { NULL, no_argument, NULL, 0 },
7255 /* Main entry for a demangling filter executable. It will demangle
7256 its command line arguments, if any. If none are provided, it will
7257 filter stdin to stdout, replacing any recognized mangled C++ names
7258 with their demangled equivalents. */
7261 main (int argc, char *argv[])
7263 int i;
7264 int opt_char;
7265 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
7267 /* Use the program name of this program, as invoked. */
7268 program_name = argv[0];
7270 /* Parse options. */
7273 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
7274 switch (opt_char)
7276 case '?': /* Unrecognized option. */
7277 print_usage (stderr, 1);
7278 break;
7280 case 'h':
7281 print_usage (stdout, 0);
7282 break;
7284 case 'p':
7285 options &= ~ DMGL_PARAMS;
7286 break;
7288 case 'v':
7289 options |= DMGL_VERBOSE;
7290 break;
7293 while (opt_char != -1);
7295 if (optind == argc)
7296 /* No command line arguments were provided. Filter stdin. */
7298 dyn_string_t mangled = dyn_string_new (3);
7299 char *s;
7301 /* Read all of input. */
7302 while (!feof (stdin))
7304 char c;
7306 /* Pile characters into mangled until we hit one that can't
7307 occur in a mangled name. */
7308 c = getchar ();
7309 while (!feof (stdin) && is_mangled_char (c))
7311 dyn_string_append_char (mangled, c);
7312 if (feof (stdin))
7313 break;
7314 c = getchar ();
7317 if (dyn_string_length (mangled) > 0)
7319 #ifdef IN_GLIBCPP_V3
7320 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
7321 #else
7322 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
7323 #endif
7325 if (s != NULL)
7327 fputs (s, stdout);
7328 free (s);
7330 else
7332 /* It might not have been a mangled name. Print the
7333 original text. */
7334 fputs (dyn_string_buf (mangled), stdout);
7337 dyn_string_clear (mangled);
7340 /* If we haven't hit EOF yet, we've read one character that
7341 can't occur in a mangled name, so print it out. */
7342 if (!feof (stdin))
7343 putchar (c);
7346 dyn_string_delete (mangled);
7348 else
7349 /* Demangle command line arguments. */
7351 /* Loop over command line arguments. */
7352 for (i = optind; i < argc; ++i)
7354 char *s;
7355 #ifdef IN_GLIBCPP_V3
7356 int status;
7357 #endif
7359 /* Attempt to demangle. */
7360 #ifdef IN_GLIBCPP_V3
7361 s = __cxa_demangle (argv[i], NULL, NULL, &status);
7362 #else
7363 s = cplus_demangle_v3 (argv[i], options);
7364 #endif
7366 /* If it worked, print the demangled name. */
7367 if (s != NULL)
7369 printf ("%s\n", s);
7370 free (s);
7372 else
7374 #ifdef IN_GLIBCPP_V3
7375 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
7376 #else
7377 fprintf (stderr, "Failed: %s\n", argv[i]);
7378 #endif
7383 return 0;
7386 #endif /* STANDALONE_DEMANGLER */