Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / coregrind / m_demangle / cp-demangle.c
blobb1a8d87e5934f75987dc31904e109ef3acc6b866
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003-2024 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_XOBJ_MEMBER_FUNCTION: \
602 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
603 case DEMANGLE_COMPONENT_NOEXCEPT: \
604 case DEMANGLE_COMPONENT_THROW_SPEC
606 /* True iff TYPE is a demangling component representing a
607 function-type-qualifier. */
609 static int
610 is_fnqual_component_type (enum demangle_component_type type)
612 switch (type)
614 FNQUAL_COMPONENT_CASE:
615 return 1;
616 default:
617 break;
619 return 0;
623 #ifdef CP_DEMANGLE_DEBUG
625 static void
626 d_dump (struct demangle_component *dc, int indent)
628 int i;
630 if (dc == NULL)
632 if (indent == 0)
633 printf ("failed demangling\n");
634 return;
637 for (i = 0; i < indent; ++i)
638 putchar (' ');
640 switch (dc->type)
642 case DEMANGLE_COMPONENT_NAME:
643 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
644 return;
645 case DEMANGLE_COMPONENT_TAGGED_NAME:
646 printf ("tagged name\n");
647 d_dump (dc->u.s_binary.left, indent + 2);
648 d_dump (dc->u.s_binary.right, indent + 2);
649 return;
650 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
651 printf ("template parameter %ld\n", dc->u.s_number.number);
652 return;
653 case DEMANGLE_COMPONENT_TPARM_OBJ:
654 printf ("template parameter object\n");
655 break;
656 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
657 printf ("function parameter %ld\n", dc->u.s_number.number);
658 return;
659 case DEMANGLE_COMPONENT_CTOR:
660 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
661 d_dump (dc->u.s_ctor.name, indent + 2);
662 return;
663 case DEMANGLE_COMPONENT_DTOR:
664 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
665 d_dump (dc->u.s_dtor.name, indent + 2);
666 return;
667 case DEMANGLE_COMPONENT_SUB_STD:
668 printf ("standard substitution %s\n", dc->u.s_string.string);
669 return;
670 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
671 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
672 return;
673 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
675 char suffix[2] = { dc->u.s_extended_builtin.type->suffix, 0 };
676 printf ("builtin type %s%d%s\n", dc->u.s_extended_builtin.type->name,
677 dc->u.s_extended_builtin.type->arg, suffix);
679 return;
680 case DEMANGLE_COMPONENT_OPERATOR:
681 printf ("operator %s\n", dc->u.s_operator.op->name);
682 return;
683 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
684 printf ("extended operator with %d args\n",
685 dc->u.s_extended_operator.args);
686 d_dump (dc->u.s_extended_operator.name, indent + 2);
687 return;
689 case DEMANGLE_COMPONENT_QUAL_NAME:
690 printf ("qualified name\n");
691 break;
692 case DEMANGLE_COMPONENT_LOCAL_NAME:
693 printf ("local name\n");
694 break;
695 case DEMANGLE_COMPONENT_TYPED_NAME:
696 printf ("typed name\n");
697 break;
698 case DEMANGLE_COMPONENT_TEMPLATE:
699 printf ("template\n");
700 break;
701 case DEMANGLE_COMPONENT_VTABLE:
702 printf ("vtable\n");
703 break;
704 case DEMANGLE_COMPONENT_VTT:
705 printf ("VTT\n");
706 break;
707 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
708 printf ("construction vtable\n");
709 break;
710 case DEMANGLE_COMPONENT_TYPEINFO:
711 printf ("typeinfo\n");
712 break;
713 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
714 printf ("typeinfo name\n");
715 break;
716 case DEMANGLE_COMPONENT_TYPEINFO_FN:
717 printf ("typeinfo function\n");
718 break;
719 case DEMANGLE_COMPONENT_THUNK:
720 printf ("thunk\n");
721 break;
722 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
723 printf ("virtual thunk\n");
724 break;
725 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
726 printf ("covariant thunk\n");
727 break;
728 case DEMANGLE_COMPONENT_JAVA_CLASS:
729 printf ("java class\n");
730 break;
731 case DEMANGLE_COMPONENT_GUARD:
732 printf ("guard\n");
733 break;
734 case DEMANGLE_COMPONENT_REFTEMP:
735 printf ("reference temporary\n");
736 break;
737 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
738 printf ("hidden alias\n");
739 break;
740 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
741 printf ("transaction clone\n");
742 break;
743 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
744 printf ("non-transaction clone\n");
745 break;
746 case DEMANGLE_COMPONENT_RESTRICT:
747 printf ("restrict\n");
748 break;
749 case DEMANGLE_COMPONENT_VOLATILE:
750 printf ("volatile\n");
751 break;
752 case DEMANGLE_COMPONENT_CONST:
753 printf ("const\n");
754 break;
755 case DEMANGLE_COMPONENT_RESTRICT_THIS:
756 printf ("restrict this\n");
757 break;
758 case DEMANGLE_COMPONENT_VOLATILE_THIS:
759 printf ("volatile this\n");
760 break;
761 case DEMANGLE_COMPONENT_CONST_THIS:
762 printf ("const this\n");
763 break;
764 case DEMANGLE_COMPONENT_REFERENCE_THIS:
765 printf ("reference this\n");
766 break;
767 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
768 printf ("rvalue reference this\n");
769 break;
770 case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
771 printf ("explicit object parameter\n");
772 break;
773 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
774 printf ("transaction_safe this\n");
775 break;
776 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
777 printf ("vendor type qualifier\n");
778 break;
779 case DEMANGLE_COMPONENT_POINTER:
780 printf ("pointer\n");
781 break;
782 case DEMANGLE_COMPONENT_REFERENCE:
783 printf ("reference\n");
784 break;
785 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
786 printf ("rvalue reference\n");
787 break;
788 case DEMANGLE_COMPONENT_COMPLEX:
789 printf ("complex\n");
790 break;
791 case DEMANGLE_COMPONENT_IMAGINARY:
792 printf ("imaginary\n");
793 break;
794 case DEMANGLE_COMPONENT_VENDOR_TYPE:
795 printf ("vendor type\n");
796 break;
797 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
798 printf ("function type\n");
799 break;
800 case DEMANGLE_COMPONENT_ARRAY_TYPE:
801 printf ("array type\n");
802 break;
803 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
804 printf ("pointer to member type\n");
805 break;
806 case DEMANGLE_COMPONENT_ARGLIST:
807 printf ("argument list\n");
808 break;
809 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
810 printf ("template argument list\n");
811 break;
812 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
813 printf ("initializer list\n");
814 break;
815 case DEMANGLE_COMPONENT_CAST:
816 printf ("cast\n");
817 break;
818 case DEMANGLE_COMPONENT_CONVERSION:
819 printf ("conversion operator\n");
820 break;
821 case DEMANGLE_COMPONENT_NULLARY:
822 printf ("nullary operator\n");
823 break;
824 case DEMANGLE_COMPONENT_UNARY:
825 printf ("unary operator\n");
826 break;
827 case DEMANGLE_COMPONENT_BINARY:
828 printf ("binary operator\n");
829 break;
830 case DEMANGLE_COMPONENT_BINARY_ARGS:
831 printf ("binary operator arguments\n");
832 break;
833 case DEMANGLE_COMPONENT_TRINARY:
834 printf ("trinary operator\n");
835 break;
836 case DEMANGLE_COMPONENT_TRINARY_ARG1:
837 printf ("trinary operator arguments 1\n");
838 break;
839 case DEMANGLE_COMPONENT_TRINARY_ARG2:
840 printf ("trinary operator arguments 1\n");
841 break;
842 case DEMANGLE_COMPONENT_LITERAL:
843 printf ("literal\n");
844 break;
845 case DEMANGLE_COMPONENT_LITERAL_NEG:
846 printf ("negative literal\n");
847 break;
848 case DEMANGLE_COMPONENT_VENDOR_EXPR:
849 printf ("vendor expression\n");
850 break;
851 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
852 printf ("java resource\n");
853 break;
854 case DEMANGLE_COMPONENT_COMPOUND_NAME:
855 printf ("compound name\n");
856 break;
857 case DEMANGLE_COMPONENT_CHARACTER:
858 printf ("character '%c'\n", dc->u.s_character.character);
859 return;
860 case DEMANGLE_COMPONENT_NUMBER:
861 printf ("number %ld\n", dc->u.s_number.number);
862 return;
863 case DEMANGLE_COMPONENT_DECLTYPE:
864 printf ("decltype\n");
865 break;
866 case DEMANGLE_COMPONENT_PACK_EXPANSION:
867 printf ("pack expansion\n");
868 break;
869 case DEMANGLE_COMPONENT_TLS_INIT:
870 printf ("tls init function\n");
871 break;
872 case DEMANGLE_COMPONENT_TLS_WRAPPER:
873 printf ("tls wrapper function\n");
874 break;
875 case DEMANGLE_COMPONENT_DEFAULT_ARG:
876 printf ("default argument %d\n", dc->u.s_unary_num.num);
877 d_dump (dc->u.s_unary_num.sub, indent+2);
878 return;
879 case DEMANGLE_COMPONENT_LAMBDA:
880 printf ("lambda %d\n", dc->u.s_unary_num.num);
881 d_dump (dc->u.s_unary_num.sub, indent+2);
882 return;
885 d_dump (d_left (dc), indent + 2);
886 d_dump (d_right (dc), indent + 2);
889 #endif /* CP_DEMANGLE_DEBUG */
891 /* Fill in a DEMANGLE_COMPONENT_NAME. */
893 CP_STATIC_IF_GLIBCPP_V3
895 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
897 if (p == NULL || s == NULL || len <= 0)
898 return 0;
899 p->d_printing = 0;
900 p->d_counting = 0;
901 p->type = DEMANGLE_COMPONENT_NAME;
902 p->u.s_name.s = s;
903 p->u.s_name.len = len;
904 return 1;
907 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
909 CP_STATIC_IF_GLIBCPP_V3
911 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
912 struct demangle_component *name)
914 if (p == NULL || args < 0 || name == NULL)
915 return 0;
916 p->d_printing = 0;
917 p->d_counting = 0;
918 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
919 p->u.s_extended_operator.args = args;
920 p->u.s_extended_operator.name = name;
921 return 1;
924 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
926 CP_STATIC_IF_GLIBCPP_V3
928 cplus_demangle_fill_ctor (struct demangle_component *p,
929 enum gnu_v3_ctor_kinds kind,
930 struct demangle_component *name)
932 if (p == NULL
933 || name == NULL
934 || (int) kind < gnu_v3_complete_object_ctor
935 || (int) kind > gnu_v3_object_ctor_group)
936 return 0;
937 p->d_printing = 0;
938 p->d_counting = 0;
939 p->type = DEMANGLE_COMPONENT_CTOR;
940 p->u.s_ctor.kind = kind;
941 p->u.s_ctor.name = name;
942 return 1;
945 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
947 CP_STATIC_IF_GLIBCPP_V3
949 cplus_demangle_fill_dtor (struct demangle_component *p,
950 enum gnu_v3_dtor_kinds kind,
951 struct demangle_component *name)
953 if (p == NULL
954 || name == NULL
955 || (int) kind < gnu_v3_deleting_dtor
956 || (int) kind > gnu_v3_object_dtor_group)
957 return 0;
958 p->d_printing = 0;
959 p->d_counting = 0;
960 p->type = DEMANGLE_COMPONENT_DTOR;
961 p->u.s_dtor.kind = kind;
962 p->u.s_dtor.name = name;
963 return 1;
966 /* Add a new component. */
968 static struct demangle_component *
969 d_make_empty (struct d_info *di)
971 struct demangle_component *p;
973 if (di->next_comp >= di->num_comps)
974 return NULL;
975 p = &di->comps[di->next_comp];
976 p->d_printing = 0;
977 p->d_counting = 0;
978 ++di->next_comp;
979 return p;
982 /* Add a new generic component. */
984 static struct demangle_component *
985 d_make_comp (struct d_info *di, enum demangle_component_type type,
986 struct demangle_component *left,
987 struct demangle_component *right)
989 struct demangle_component *p;
991 /* We check for errors here. A typical error would be a NULL return
992 from a subroutine. We catch those here, and return NULL
993 upward. */
994 switch (type)
996 /* These types require two parameters. */
997 case DEMANGLE_COMPONENT_QUAL_NAME:
998 case DEMANGLE_COMPONENT_LOCAL_NAME:
999 case DEMANGLE_COMPONENT_TYPED_NAME:
1000 case DEMANGLE_COMPONENT_TAGGED_NAME:
1001 case DEMANGLE_COMPONENT_TEMPLATE:
1002 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
1003 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
1004 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
1005 case DEMANGLE_COMPONENT_UNARY:
1006 case DEMANGLE_COMPONENT_BINARY:
1007 case DEMANGLE_COMPONENT_BINARY_ARGS:
1008 case DEMANGLE_COMPONENT_TRINARY:
1009 case DEMANGLE_COMPONENT_TRINARY_ARG1:
1010 case DEMANGLE_COMPONENT_LITERAL:
1011 case DEMANGLE_COMPONENT_LITERAL_NEG:
1012 case DEMANGLE_COMPONENT_VENDOR_EXPR:
1013 case DEMANGLE_COMPONENT_COMPOUND_NAME:
1014 case DEMANGLE_COMPONENT_VECTOR_TYPE:
1015 case DEMANGLE_COMPONENT_CLONE:
1016 case DEMANGLE_COMPONENT_MODULE_ENTITY:
1017 case DEMANGLE_COMPONENT_CONSTRAINTS:
1018 if (left == NULL || right == NULL)
1019 return NULL;
1020 break;
1022 /* These types only require one parameter. */
1023 case DEMANGLE_COMPONENT_VTABLE:
1024 case DEMANGLE_COMPONENT_VTT:
1025 case DEMANGLE_COMPONENT_TYPEINFO:
1026 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
1027 case DEMANGLE_COMPONENT_TYPEINFO_FN:
1028 case DEMANGLE_COMPONENT_THUNK:
1029 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
1030 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
1031 case DEMANGLE_COMPONENT_JAVA_CLASS:
1032 case DEMANGLE_COMPONENT_GUARD:
1033 case DEMANGLE_COMPONENT_TLS_INIT:
1034 case DEMANGLE_COMPONENT_TLS_WRAPPER:
1035 case DEMANGLE_COMPONENT_REFTEMP:
1036 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
1037 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
1038 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
1039 case DEMANGLE_COMPONENT_POINTER:
1040 case DEMANGLE_COMPONENT_REFERENCE:
1041 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
1042 case DEMANGLE_COMPONENT_COMPLEX:
1043 case DEMANGLE_COMPONENT_IMAGINARY:
1044 case DEMANGLE_COMPONENT_VENDOR_TYPE:
1045 case DEMANGLE_COMPONENT_CAST:
1046 case DEMANGLE_COMPONENT_CONVERSION:
1047 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
1048 case DEMANGLE_COMPONENT_DECLTYPE:
1049 case DEMANGLE_COMPONENT_PACK_EXPANSION:
1050 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
1051 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
1052 case DEMANGLE_COMPONENT_NULLARY:
1053 case DEMANGLE_COMPONENT_TRINARY_ARG2:
1054 case DEMANGLE_COMPONENT_TPARM_OBJ:
1055 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
1056 case DEMANGLE_COMPONENT_MODULE_INIT:
1057 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
1058 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
1059 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
1060 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
1061 case DEMANGLE_COMPONENT_FRIEND:
1062 if (left == NULL)
1063 return NULL;
1064 break;
1066 /* This needs a right parameter, but the left parameter can be
1067 empty. */
1068 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1069 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
1070 case DEMANGLE_COMPONENT_MODULE_NAME:
1071 case DEMANGLE_COMPONENT_MODULE_PARTITION:
1072 if (right == NULL)
1073 return NULL;
1074 break;
1076 /* These are allowed to have no parameters--in some cases they
1077 will be filled in later. */
1078 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1079 case DEMANGLE_COMPONENT_RESTRICT:
1080 case DEMANGLE_COMPONENT_VOLATILE:
1081 case DEMANGLE_COMPONENT_CONST:
1082 case DEMANGLE_COMPONENT_ARGLIST:
1083 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1084 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
1085 FNQUAL_COMPONENT_CASE:
1086 break;
1088 /* Other types should not be seen here. */
1089 default:
1090 return NULL;
1093 p = d_make_empty (di);
1094 if (p != NULL)
1096 p->type = type;
1097 p->u.s_binary.left = left;
1098 p->u.s_binary.right = right;
1100 return p;
1103 /* Add a new demangle mangled name component. */
1105 static struct demangle_component *
1106 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1108 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1109 return d_make_name (di, s, strlen (s));
1110 d_advance (di, 2);
1111 return d_encoding (di, 0);
1114 /* Add a new name component. */
1116 static struct demangle_component *
1117 d_make_name (struct d_info *di, const char *s, int len)
1119 struct demangle_component *p;
1121 p = d_make_empty (di);
1122 if (! cplus_demangle_fill_name (p, s, len))
1123 return NULL;
1124 return p;
1127 /* Add a new builtin type component. */
1129 static struct demangle_component *
1130 d_make_builtin_type (struct d_info *di,
1131 const struct demangle_builtin_type_info *type)
1133 struct demangle_component *p;
1135 if (type == NULL)
1136 return NULL;
1137 p = d_make_empty (di);
1138 if (p != NULL)
1140 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1141 p->u.s_builtin.type = type;
1143 return p;
1146 /* Add a new extended builtin type component. */
1148 static struct demangle_component *
1149 d_make_extended_builtin_type (struct d_info *di,
1150 const struct demangle_builtin_type_info *type,
1151 short arg, char suffix)
1153 struct demangle_component *p;
1155 if (type == NULL)
1156 return NULL;
1157 p = d_make_empty (di);
1158 if (p != NULL)
1160 p->type = DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE;
1161 p->u.s_extended_builtin.type = type;
1162 p->u.s_extended_builtin.arg = arg;
1163 p->u.s_extended_builtin.suffix = suffix;
1165 return p;
1168 /* Add a new operator component. */
1170 static struct demangle_component *
1171 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1173 struct demangle_component *p;
1175 p = d_make_empty (di);
1176 if (p != NULL)
1178 p->type = DEMANGLE_COMPONENT_OPERATOR;
1179 p->u.s_operator.op = op;
1181 return p;
1184 /* Add a new extended operator component. */
1186 static struct demangle_component *
1187 d_make_extended_operator (struct d_info *di, int args,
1188 struct demangle_component *name)
1190 struct demangle_component *p;
1192 p = d_make_empty (di);
1193 if (! cplus_demangle_fill_extended_operator (p, args, name))
1194 return NULL;
1195 return p;
1198 static struct demangle_component *
1199 d_make_default_arg (struct d_info *di, int num,
1200 struct demangle_component *sub)
1202 struct demangle_component *p = d_make_empty (di);
1203 if (p)
1205 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1206 p->u.s_unary_num.num = num;
1207 p->u.s_unary_num.sub = sub;
1209 return p;
1212 /* Add a new constructor component. */
1214 static struct demangle_component *
1215 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1216 struct demangle_component *name)
1218 struct demangle_component *p;
1220 p = d_make_empty (di);
1221 if (! cplus_demangle_fill_ctor (p, kind, name))
1222 return NULL;
1223 return p;
1226 /* Add a new destructor component. */
1228 static struct demangle_component *
1229 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1230 struct demangle_component *name)
1232 struct demangle_component *p;
1234 p = d_make_empty (di);
1235 if (! cplus_demangle_fill_dtor (p, kind, name))
1236 return NULL;
1237 return p;
1240 /* Add a new template parameter. */
1242 static struct demangle_component *
1243 d_make_template_param (struct d_info *di, int i)
1245 struct demangle_component *p;
1247 p = d_make_empty (di);
1248 if (p != NULL)
1250 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1251 p->u.s_number.number = i;
1253 return p;
1256 /* Add a new function parameter. */
1258 static struct demangle_component *
1259 d_make_function_param (struct d_info *di, int i)
1261 struct demangle_component *p;
1263 p = d_make_empty (di);
1264 if (p != NULL)
1266 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1267 p->u.s_number.number = i;
1269 return p;
1272 /* Add a new standard substitution component. */
1274 static struct demangle_component *
1275 d_make_sub (struct d_info *di, const char *name, int len)
1277 struct demangle_component *p;
1279 p = d_make_empty (di);
1280 if (p != NULL)
1282 p->type = DEMANGLE_COMPONENT_SUB_STD;
1283 p->u.s_string.string = name;
1284 p->u.s_string.len = len;
1286 return p;
1289 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1291 TOP_LEVEL is non-zero when called at the top level. */
1293 CP_STATIC_IF_GLIBCPP_V3
1294 struct demangle_component *
1295 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1297 struct demangle_component *p;
1299 if (! d_check_char (di, '_')
1300 /* Allow missing _ if not at toplevel to work around a
1301 bug in G++ abi-version=2 mangling; see the comment in
1302 write_template_arg. */
1303 && top_level)
1304 return NULL;
1305 if (! d_check_char (di, 'Z'))
1306 return NULL;
1307 p = d_encoding (di, top_level);
1309 /* If at top level and parsing parameters, check for a clone
1310 suffix. */
1311 if (top_level && (di->options & DMGL_PARAMS) != 0)
1312 while (d_peek_char (di) == '.'
1313 && (IS_LOWER (d_peek_next_char (di))
1314 || d_peek_next_char (di) == '_'
1315 || IS_DIGIT (d_peek_next_char (di))))
1316 p = d_clone_suffix (di, p);
1318 return p;
1321 /* Return whether a function should have a return type. The argument
1322 is the function name, which may be qualified in various ways. The
1323 rules are that template functions have return types with some
1324 exceptions, function types which are not part of a function name
1325 mangling have return types with some exceptions, and non-template
1326 function names do not have return types. The exceptions are that
1327 constructors, destructors, and conversion operators do not have
1328 return types. */
1330 static int
1331 has_return_type (struct demangle_component *dc)
1333 if (dc == NULL)
1334 return 0;
1335 switch (dc->type)
1337 default:
1338 return 0;
1339 case DEMANGLE_COMPONENT_LOCAL_NAME:
1340 return has_return_type (d_right (dc));
1341 case DEMANGLE_COMPONENT_TEMPLATE:
1342 return ! is_ctor_dtor_or_conversion (d_left (dc));
1343 FNQUAL_COMPONENT_CASE:
1344 return has_return_type (d_left (dc));
1348 /* Return whether a name is a constructor, a destructor, or a
1349 conversion operator. */
1351 static int
1352 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1354 if (dc == NULL)
1355 return 0;
1356 switch (dc->type)
1358 default:
1359 return 0;
1360 case DEMANGLE_COMPONENT_QUAL_NAME:
1361 case DEMANGLE_COMPONENT_LOCAL_NAME:
1362 return is_ctor_dtor_or_conversion (d_right (dc));
1363 case DEMANGLE_COMPONENT_CTOR:
1364 case DEMANGLE_COMPONENT_DTOR:
1365 case DEMANGLE_COMPONENT_CONVERSION:
1366 return 1;
1370 /* [ Q <constraint-expression> ] */
1372 static struct demangle_component *
1373 d_maybe_constraints (struct d_info *di, struct demangle_component *dc)
1375 if (d_peek_char (di) == 'Q')
1377 d_advance (di, 1);
1378 struct demangle_component *expr = d_expression (di);
1379 if (expr == NULL)
1380 return NULL;
1381 dc = d_make_comp (di, DEMANGLE_COMPONENT_CONSTRAINTS, dc, expr);
1383 return dc;
1386 /* <encoding> ::= <(function) name> <bare-function-type>
1387 ::= <(data) name>
1388 ::= <special-name>
1390 TOP_LEVEL is non-zero when called at the top level, in which case
1391 if DMGL_PARAMS is not set we do not demangle the function
1392 parameters. We only set this at the top level, because otherwise
1393 we would not correctly demangle names in local scopes. */
1395 static struct demangle_component *
1396 d_encoding (struct d_info *di, int top_level)
1398 char peek = d_peek_char (di);
1399 struct demangle_component *dc;
1401 if (peek == 'G' || peek == 'T')
1402 dc = d_special_name (di);
1403 else
1405 dc = d_name (di, 0);
1407 if (!dc)
1408 /* Failed already. */;
1409 else if (top_level && (di->options & DMGL_PARAMS) == 0)
1411 /* Strip off any initial CV-qualifiers, as they really apply
1412 to the `this' parameter, and they were not output by the
1413 v2 demangler without DMGL_PARAMS. */
1414 while (is_fnqual_component_type (dc->type))
1415 dc = d_left (dc);
1417 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1418 there may be function-qualifiers on its right argument which
1419 really apply here; this happens when parsing a class
1420 which is local to a function. */
1421 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1423 while (d_right (dc) != NULL
1424 && is_fnqual_component_type (d_right (dc)->type))
1425 d_right (dc) = d_left (d_right (dc));
1427 if (d_right (dc) == NULL)
1428 dc = NULL;
1431 else
1433 peek = d_peek_char (di);
1434 if (peek != '\0' && peek != 'E')
1436 struct demangle_component *ftype;
1438 ftype = d_bare_function_type (di, has_return_type (dc));
1439 if (!ftype)
1440 return NULL;
1442 /* If this is a non-top-level local-name, clear the
1443 return type, so it doesn't confuse the user by
1444 being confused with the return type of whaever
1445 this is nested within. */
1446 if (!top_level && dc->type == DEMANGLE_COMPONENT_LOCAL_NAME
1447 && ftype->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
1448 d_left (ftype) = NULL;
1450 ftype = d_maybe_constraints (di, ftype);
1452 dc = d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME,
1453 dc, ftype);
1458 return dc;
1461 /* <tagged-name> ::= <name> B <source-name> */
1463 static struct demangle_component *
1464 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1466 struct demangle_component *hold_last_name;
1467 char peek;
1469 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1470 hold_last_name = di->last_name;
1472 while (peek = d_peek_char (di),
1473 peek == 'B')
1475 struct demangle_component *tag;
1476 d_advance (di, 1);
1477 tag = d_source_name (di);
1478 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1481 di->last_name = hold_last_name;
1483 return dc;
1486 /* <name> ::= <nested-name>
1487 ::= <unscoped-name>
1488 ::= <unscoped-template-name> <template-args>
1489 ::= <local-name>
1491 <unscoped-name> ::= <unqualified-name>
1492 ::= St <unqualified-name>
1494 <unscoped-template-name> ::= <unscoped-name>
1495 ::= <substitution>
1498 static struct demangle_component *
1499 d_name (struct d_info *di, int substable)
1501 char peek = d_peek_char (di);
1502 struct demangle_component *dc = NULL;
1503 struct demangle_component *module = NULL;
1504 int subst = 0;
1506 switch (peek)
1508 case 'N':
1509 dc = d_nested_name (di);
1510 break;
1512 case 'Z':
1513 dc = d_local_name (di);
1514 break;
1516 case 'U':
1517 dc = d_unqualified_name (di, NULL, NULL);
1518 break;
1520 case 'S':
1522 if (d_peek_next_char (di) == 't')
1524 d_advance (di, 2);
1525 dc = d_make_name (di, "std", 3);
1526 di->expansion += 3;
1529 if (d_peek_char (di) == 'S')
1531 module = d_substitution (di, 0);
1532 if (!module)
1533 return NULL;
1534 if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1535 || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1537 if (dc)
1538 return NULL;
1539 subst = 1;
1540 dc = module;
1541 module = NULL;
1545 /* FALLTHROUGH */
1547 case 'L':
1548 default:
1549 if (!subst)
1550 dc = d_unqualified_name (di, dc, module);
1551 if (d_peek_char (di) == 'I')
1553 /* This is <template-args>, which means that we just saw
1554 <unscoped-template-name>, which is a substitution
1555 candidate. */
1556 if (!subst && !d_add_substitution (di, dc))
1557 return NULL;
1558 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1559 d_template_args (di));
1560 subst = 0;
1562 break;
1564 if (substable && !subst && !d_add_substitution (di, dc))
1565 return NULL;
1566 return dc;
1569 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1570 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1571 ::= N H <prefix> <unqualified-name> E
1572 ::= N H <template-prefix> <template-args> E
1575 static struct demangle_component *
1576 d_nested_name (struct d_info *di)
1578 struct demangle_component *ret;
1579 struct demangle_component **pret;
1580 struct demangle_component *rqual;
1582 if (! d_check_char (di, 'N'))
1583 return NULL;
1585 if (d_peek_char (di) == 'H')
1587 d_advance (di, 1);
1588 di->expansion += sizeof "this";
1589 pret = &ret;
1590 rqual = d_make_comp (di, DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION,
1591 NULL, NULL);
1593 else
1595 pret = d_cv_qualifiers (di, &ret, 1);
1596 if (pret == NULL)
1597 return NULL;
1599 /* Parse the ref-qualifier now and then attach it
1600 once we have something to attach it to. */
1601 rqual = d_ref_qualifier (di, NULL);
1604 *pret = d_prefix (di, 1);
1605 if (*pret == NULL)
1606 return NULL;
1608 if (rqual)
1610 d_left (rqual) = ret;
1611 ret = rqual;
1614 if (! d_check_char (di, 'E'))
1615 return NULL;
1617 return ret;
1620 /* <prefix> ::= <prefix> <unqualified-name>
1621 ::= <template-prefix> <template-args>
1622 ::= <template-param>
1623 ::= <decltype>
1625 ::= <substitution>
1627 <template-prefix> ::= <prefix> <(template) unqualified-name>
1628 ::= <template-param>
1629 ::= <substitution>
1631 SUBST is true if we should add substitutions (as normal), false
1632 if not (in an unresolved-name). */
1634 static struct demangle_component *
1635 d_prefix (struct d_info *di, int substable)
1637 struct demangle_component *ret = NULL;
1639 for (;;)
1641 char peek = d_peek_char (di);
1643 /* The older code accepts a <local-name> here, but I don't see
1644 that in the grammar. The older code does not accept a
1645 <template-param> here. */
1647 if (peek == 'D'
1648 && (d_peek_next_char (di) == 'T'
1649 || d_peek_next_char (di) == 't'))
1651 /* Decltype. */
1652 if (ret)
1653 return NULL;
1654 ret = cplus_demangle_type (di);
1656 else if (peek == 'I')
1658 if (ret == NULL)
1659 return NULL;
1660 struct demangle_component *dc = d_template_args (di);
1661 if (!dc)
1662 return NULL;
1663 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, dc);
1665 else if (peek == 'T')
1667 if (ret)
1668 return NULL;
1669 ret = d_template_param (di);
1671 else if (peek == 'M')
1673 /* Initializer scope for a lambda. We already added it as a
1674 substitution candidate, don't do that again. */
1675 d_advance (di, 1);
1676 continue;
1678 else
1680 struct demangle_component *module = NULL;
1681 if (peek == 'S')
1683 module = d_substitution (di, 1);
1684 if (!module)
1685 return NULL;
1686 if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1687 || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1689 if (ret)
1690 return NULL;
1691 ret = module;
1692 continue;
1695 ret = d_unqualified_name (di, ret, module);
1698 if (!ret)
1699 break;
1701 if (d_peek_char (di) == 'E')
1702 break;
1704 if (substable && !d_add_substitution (di, ret))
1705 return NULL;
1708 return ret;
1711 static int
1712 d_maybe_module_name (struct d_info *di, struct demangle_component **name)
1714 while (d_peek_char (di) == 'W')
1716 d_advance (di, 1);
1717 enum demangle_component_type code = DEMANGLE_COMPONENT_MODULE_NAME;
1718 if (d_peek_char (di) == 'P')
1720 code = DEMANGLE_COMPONENT_MODULE_PARTITION;
1721 d_advance (di, 1);
1724 *name = d_make_comp (di, code, *name, d_source_name (di));
1725 if (!*name)
1726 return 0;
1727 if (!d_add_substitution (di, *name))
1728 return 0;
1730 return 1;
1733 /* <unqualified-name> ::= [<module-name>] <operator-name> [<abi-tags>]
1734 ::= [<module-name>] <ctor-dtor-name> [<abi-tags>]
1735 ::= [<module-name>] <source-name> [<abi-tags>]
1736 ::= [<module-name>] F <source-name> [<abi-tags>]
1737 ::= [<module-name>] <local-source-name> [<abi-tags>]
1738 ::= [<module-name>] DC <source-name>+ E [<abi-tags>]
1739 <local-source-name> ::= L <source-name> <discriminator> [<abi-tags>]
1742 static struct demangle_component *
1743 d_unqualified_name (struct d_info *di, struct demangle_component *scope,
1744 struct demangle_component *module)
1746 struct demangle_component *ret;
1747 char peek;
1748 int member_like_friend = 0;
1750 if (!d_maybe_module_name (di, &module))
1751 return NULL;
1753 peek = d_peek_char (di);
1754 if (peek == 'F')
1756 member_like_friend = 1;
1757 d_advance (di, 1);
1758 peek = d_peek_char (di);
1760 if (IS_DIGIT (peek))
1761 ret = d_source_name (di);
1762 else if (IS_LOWER (peek))
1764 int was_expr = di->is_expression;
1765 if (peek == 'o' && d_peek_next_char (di) == 'n')
1767 d_advance (di, 2);
1768 /* Treat cv as naming a conversion operator. */
1769 di->is_expression = 0;
1771 ret = d_operator_name (di);
1772 di->is_expression = was_expr;
1773 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1775 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1776 if (!strcmp (ret->u.s_operator.op->code, "li"))
1777 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1778 d_source_name (di));
1781 else if (peek == 'D' && d_peek_next_char (di) == 'C')
1783 // structured binding
1784 d_advance (di, 2);
1785 struct demangle_component *prev = NULL;
1788 struct demangle_component *next =
1789 d_make_comp (di, DEMANGLE_COMPONENT_STRUCTURED_BINDING,
1790 d_source_name (di), NULL);
1791 if (prev)
1792 d_right (prev) = next;
1793 else
1794 ret = next;
1795 prev = next;
1797 while (prev && d_peek_char (di) != 'E');
1798 if (prev)
1799 d_advance (di, 1);
1800 else
1801 ret = NULL;
1803 else if (peek == 'C' || peek == 'D')
1804 ret = d_ctor_dtor_name (di);
1805 else if (peek == 'L')
1807 d_advance (di, 1);
1809 ret = d_source_name (di);
1810 if (ret == NULL)
1811 return NULL;
1812 if (! d_discriminator (di))
1813 return NULL;
1815 else if (peek == 'U')
1817 switch (d_peek_next_char (di))
1819 case 'l':
1820 ret = d_lambda (di);
1821 break;
1822 case 't':
1823 ret = d_unnamed_type (di);
1824 break;
1825 default:
1826 return NULL;
1829 else
1830 return NULL;
1832 if (module)
1833 ret = d_make_comp (di, DEMANGLE_COMPONENT_MODULE_ENTITY, ret, module);
1834 if (d_peek_char (di) == 'B')
1835 ret = d_abi_tags (di, ret);
1836 if (member_like_friend)
1837 ret = d_make_comp (di, DEMANGLE_COMPONENT_FRIEND, ret, NULL);
1838 if (scope)
1839 ret = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, scope, ret);
1841 return ret;
1844 /* <source-name> ::= <(positive length) number> <identifier> */
1846 static struct demangle_component *
1847 d_source_name (struct d_info *di)
1849 int len;
1850 struct demangle_component *ret;
1852 len = d_number (di);
1853 if (len <= 0)
1854 return NULL;
1855 ret = d_identifier (di, len);
1856 di->last_name = ret;
1857 return ret;
1860 /* number ::= [n] <(non-negative decimal integer)> */
1862 static int
1863 d_number (struct d_info *di)
1865 int negative;
1866 char peek;
1867 int ret;
1869 negative = 0;
1870 peek = d_peek_char (di);
1871 if (peek == 'n')
1873 negative = 1;
1874 d_advance (di, 1);
1875 peek = d_peek_char (di);
1878 ret = 0;
1879 while (1)
1881 if (! IS_DIGIT (peek))
1883 if (negative)
1884 ret = - ret;
1885 return ret;
1887 if (ret > ((INT_MAX - (peek - '0')) / 10))
1888 return -1;
1889 ret = ret * 10 + (peek - '0');
1890 d_advance (di, 1);
1891 peek = d_peek_char (di);
1895 /* Like d_number, but returns a demangle_component. */
1897 static struct demangle_component *
1898 d_number_component (struct d_info *di)
1900 struct demangle_component *ret = d_make_empty (di);
1901 if (ret)
1903 ret->type = DEMANGLE_COMPONENT_NUMBER;
1904 ret->u.s_number.number = d_number (di);
1906 return ret;
1909 /* identifier ::= <(unqualified source code identifier)> */
1911 static struct demangle_component *
1912 d_identifier (struct d_info *di, int len)
1914 const char *name;
1916 name = d_str (di);
1918 if (di->send - name < len)
1919 return NULL;
1921 d_advance (di, len);
1923 /* A Java mangled name may have a trailing '$' if it is a C++
1924 keyword. This '$' is not included in the length count. We just
1925 ignore the '$'. */
1926 if ((di->options & DMGL_JAVA) != 0
1927 && d_peek_char (di) == '$')
1928 d_advance (di, 1);
1930 /* Look for something which looks like a gcc encoding of an
1931 anonymous namespace, and replace it with a more user friendly
1932 name. */
1933 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1934 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1935 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1937 const char *s;
1939 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1940 if ((*s == '.' || *s == '_' || *s == '$')
1941 && s[1] == 'N')
1943 di->expansion -= len - sizeof "(anonymous namespace)";
1944 return d_make_name (di, "(anonymous namespace)",
1945 sizeof "(anonymous namespace)" - 1);
1949 return d_make_name (di, name, len);
1952 /* operator_name ::= many different two character encodings.
1953 ::= cv <type>
1954 ::= v <digit> <source-name>
1956 This list is sorted for binary search. */
1958 #define NL(s) s, (sizeof s) - 1
1960 CP_STATIC_IF_GLIBCPP_V3
1961 const struct demangle_operator_info cplus_demangle_operators[] =
1963 { "aN", NL ("&="), 2 },
1964 { "aS", NL ("="), 2 },
1965 { "aa", NL ("&&"), 2 },
1966 { "ad", NL ("&"), 1 },
1967 { "an", NL ("&"), 2 },
1968 { "at", NL ("alignof "), 1 },
1969 { "aw", NL ("co_await "), 1 },
1970 { "az", NL ("alignof "), 1 },
1971 { "cc", NL ("const_cast"), 2 },
1972 { "cl", NL ("()"), 2 },
1973 { "cm", NL (","), 2 },
1974 { "co", NL ("~"), 1 },
1975 { "dV", NL ("/="), 2 },
1976 { "dX", NL ("[...]="), 3 }, /* [expr...expr] = expr */
1977 { "da", NL ("delete[] "), 1 },
1978 { "dc", NL ("dynamic_cast"), 2 },
1979 { "de", NL ("*"), 1 },
1980 { "di", NL ("="), 2 }, /* .name = expr */
1981 { "dl", NL ("delete "), 1 },
1982 { "ds", NL (".*"), 2 },
1983 { "dt", NL ("."), 2 },
1984 { "dv", NL ("/"), 2 },
1985 { "dx", NL ("]="), 2 }, /* [expr] = expr */
1986 { "eO", NL ("^="), 2 },
1987 { "eo", NL ("^"), 2 },
1988 { "eq", NL ("=="), 2 },
1989 { "fL", NL ("..."), 3 },
1990 { "fR", NL ("..."), 3 },
1991 { "fl", NL ("..."), 2 },
1992 { "fr", NL ("..."), 2 },
1993 { "ge", NL (">="), 2 },
1994 { "gs", NL ("::"), 1 },
1995 { "gt", NL (">"), 2 },
1996 { "ix", NL ("[]"), 2 },
1997 { "lS", NL ("<<="), 2 },
1998 { "le", NL ("<="), 2 },
1999 { "li", NL ("operator\"\" "), 1 },
2000 { "ls", NL ("<<"), 2 },
2001 { "lt", NL ("<"), 2 },
2002 { "mI", NL ("-="), 2 },
2003 { "mL", NL ("*="), 2 },
2004 { "mi", NL ("-"), 2 },
2005 { "ml", NL ("*"), 2 },
2006 { "mm", NL ("--"), 1 },
2007 { "na", NL ("new[]"), 3 },
2008 { "ne", NL ("!="), 2 },
2009 { "ng", NL ("-"), 1 },
2010 { "nt", NL ("!"), 1 },
2011 { "nw", NL ("new"), 3 },
2012 { "nx", NL ("noexcept"), 1 },
2013 { "oR", NL ("|="), 2 },
2014 { "oo", NL ("||"), 2 },
2015 { "or", NL ("|"), 2 },
2016 { "pL", NL ("+="), 2 },
2017 { "pl", NL ("+"), 2 },
2018 { "pm", NL ("->*"), 2 },
2019 { "pp", NL ("++"), 1 },
2020 { "ps", NL ("+"), 1 },
2021 { "pt", NL ("->"), 2 },
2022 { "qu", NL ("?"), 3 },
2023 { "rM", NL ("%="), 2 },
2024 { "rS", NL (">>="), 2 },
2025 { "rc", NL ("reinterpret_cast"), 2 },
2026 { "rm", NL ("%"), 2 },
2027 { "rs", NL (">>"), 2 },
2028 { "sP", NL ("sizeof..."), 1 },
2029 { "sZ", NL ("sizeof..."), 1 },
2030 { "sc", NL ("static_cast"), 2 },
2031 { "ss", NL ("<=>"), 2 },
2032 { "st", NL ("sizeof "), 1 },
2033 { "sz", NL ("sizeof "), 1 },
2034 { "tr", NL ("throw"), 0 },
2035 { "tw", NL ("throw "), 1 },
2036 { NULL, NULL, 0, 0 }
2039 static struct demangle_component *
2040 d_operator_name (struct d_info *di)
2042 char c1;
2043 char c2;
2045 c1 = d_next_char (di);
2046 c2 = d_next_char (di);
2047 if (c1 == 'v' && IS_DIGIT (c2))
2048 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
2049 else if (c1 == 'c' && c2 == 'v')
2051 struct demangle_component *type;
2052 int was_conversion = di->is_conversion;
2053 struct demangle_component *res;
2055 di->is_conversion = ! di->is_expression;
2056 type = cplus_demangle_type (di);
2057 if (di->is_conversion)
2058 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
2059 else
2060 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
2061 di->is_conversion = was_conversion;
2062 return res;
2064 else
2066 /* LOW is the inclusive lower bound. */
2067 int low = 0;
2068 /* HIGH is the exclusive upper bound. We subtract one to ignore
2069 the sentinel at the end of the array. */
2070 int high = ((sizeof (cplus_demangle_operators)
2071 / sizeof (cplus_demangle_operators[0]))
2072 - 1);
2074 while (1)
2076 int i;
2077 const struct demangle_operator_info *p;
2079 i = low + (high - low) / 2;
2080 p = cplus_demangle_operators + i;
2082 if (c1 == p->code[0] && c2 == p->code[1])
2083 return d_make_operator (di, p);
2085 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
2086 high = i;
2087 else
2088 low = i + 1;
2089 if (low == high)
2090 return NULL;
2095 static struct demangle_component *
2096 d_make_character (struct d_info *di, int c)
2098 struct demangle_component *p;
2099 p = d_make_empty (di);
2100 if (p != NULL)
2102 p->type = DEMANGLE_COMPONENT_CHARACTER;
2103 p->u.s_character.character = c;
2105 return p;
2108 static struct demangle_component *
2109 d_java_resource (struct d_info *di)
2111 struct demangle_component *p = NULL;
2112 struct demangle_component *next = NULL;
2113 int len, i;
2114 char c;
2115 const char *str;
2117 len = d_number (di);
2118 if (len <= 1)
2119 return NULL;
2121 /* Eat the leading '_'. */
2122 if (d_next_char (di) != '_')
2123 return NULL;
2124 len--;
2126 str = d_str (di);
2127 i = 0;
2129 while (len > 0)
2131 c = str[i];
2132 if (!c)
2133 return NULL;
2135 /* Each chunk is either a '$' escape... */
2136 if (c == '$')
2138 i++;
2139 switch (str[i++])
2141 case 'S':
2142 c = '/';
2143 break;
2144 case '_':
2145 c = '.';
2146 break;
2147 case '$':
2148 c = '$';
2149 break;
2150 default:
2151 return NULL;
2153 next = d_make_character (di, c);
2154 d_advance (di, i);
2155 str = d_str (di);
2156 len -= i;
2157 i = 0;
2158 if (next == NULL)
2159 return NULL;
2161 /* ... or a sequence of characters. */
2162 else
2164 while (i < len && str[i] && str[i] != '$')
2165 i++;
2167 next = d_make_name (di, str, i);
2168 d_advance (di, i);
2169 str = d_str (di);
2170 len -= i;
2171 i = 0;
2172 if (next == NULL)
2173 return NULL;
2176 if (p == NULL)
2177 p = next;
2178 else
2180 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
2181 if (p == NULL)
2182 return NULL;
2186 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
2188 return p;
2191 /* <special-name> ::= TV <type>
2192 ::= TT <type>
2193 ::= TI <type>
2194 ::= TS <type>
2195 ::= TA <template-arg>
2196 ::= GV <(object) name>
2197 ::= T <call-offset> <(base) encoding>
2198 ::= Tc <call-offset> <call-offset> <(base) encoding>
2199 Also g++ extensions:
2200 ::= TC <type> <(offset) number> _ <(base) type>
2201 ::= TF <type>
2202 ::= TJ <type>
2203 ::= GR <name>
2204 ::= GA <encoding>
2205 ::= Gr <resource name>
2206 ::= GTt <encoding>
2207 ::= GTn <encoding>
2210 static struct demangle_component *
2211 d_special_name (struct d_info *di)
2213 di->expansion += 20;
2214 if (d_check_char (di, 'T'))
2216 switch (d_next_char (di))
2218 case 'V':
2219 di->expansion -= 5;
2220 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2221 cplus_demangle_type (di), NULL);
2222 case 'T':
2223 di->expansion -= 10;
2224 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2225 cplus_demangle_type (di), NULL);
2226 case 'I':
2227 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2228 cplus_demangle_type (di), NULL);
2229 case 'S':
2230 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2231 cplus_demangle_type (di), NULL);
2233 case 'h':
2234 if (! d_call_offset (di, 'h'))
2235 return NULL;
2236 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2237 d_encoding (di, 0), NULL);
2239 case 'v':
2240 if (! d_call_offset (di, 'v'))
2241 return NULL;
2242 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2243 d_encoding (di, 0), NULL);
2245 case 'c':
2246 if (! d_call_offset (di, '\0'))
2247 return NULL;
2248 if (! d_call_offset (di, '\0'))
2249 return NULL;
2250 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2251 d_encoding (di, 0), NULL);
2253 case 'C':
2255 struct demangle_component *derived_type;
2256 int offset;
2257 struct demangle_component *base_type;
2259 derived_type = cplus_demangle_type (di);
2260 offset = d_number (di);
2261 if (offset < 0)
2262 return NULL;
2263 if (! d_check_char (di, '_'))
2264 return NULL;
2265 base_type = cplus_demangle_type (di);
2266 /* We don't display the offset. FIXME: We should display
2267 it in verbose mode. */
2268 di->expansion += 5;
2269 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2270 base_type, derived_type);
2273 case 'F':
2274 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2275 cplus_demangle_type (di), NULL);
2276 case 'J':
2277 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2278 cplus_demangle_type (di), NULL);
2280 case 'H':
2281 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2282 d_name (di, 0), NULL);
2284 case 'W':
2285 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2286 d_name (di, 0), NULL);
2288 case 'A':
2289 return d_make_comp (di, DEMANGLE_COMPONENT_TPARM_OBJ,
2290 d_template_arg (di), NULL);
2292 default:
2293 return NULL;
2296 else if (d_check_char (di, 'G'))
2298 switch (d_next_char (di))
2300 case 'V':
2301 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD,
2302 d_name (di, 0), NULL);
2304 case 'R':
2306 struct demangle_component *name = d_name (di, 0);
2307 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2308 d_number_component (di));
2311 case 'A':
2312 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2313 d_encoding (di, 0), NULL);
2315 case 'I':
2317 struct demangle_component *module = NULL;
2318 if (!d_maybe_module_name (di, &module) || !module)
2319 return NULL;
2320 return d_make_comp (di, DEMANGLE_COMPONENT_MODULE_INIT,
2321 module, NULL);
2323 case 'T':
2324 switch (d_next_char (di))
2326 case 'n':
2327 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2328 d_encoding (di, 0), NULL);
2329 default:
2330 /* ??? The proposal is that other letters (such as 'h') stand
2331 for different variants of transaction cloning, such as
2332 compiling directly for hardware transaction support. But
2333 they still should all be transactional clones of some sort
2334 so go ahead and call them that. */
2335 case 't':
2336 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2337 d_encoding (di, 0), NULL);
2340 case 'r':
2341 return d_java_resource (di);
2343 default:
2344 return NULL;
2347 else
2348 return NULL;
2351 /* <call-offset> ::= h <nv-offset> _
2352 ::= v <v-offset> _
2354 <nv-offset> ::= <(offset) number>
2356 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2358 The C parameter, if not '\0', is a character we just read which is
2359 the start of the <call-offset>.
2361 We don't display the offset information anywhere. FIXME: We should
2362 display it in verbose mode. */
2364 static int
2365 d_call_offset (struct d_info *di, int c)
2367 if (c == '\0')
2368 c = d_next_char (di);
2370 if (c == 'h')
2371 d_number (di);
2372 else if (c == 'v')
2374 d_number (di);
2375 if (! d_check_char (di, '_'))
2376 return 0;
2377 d_number (di);
2379 else
2380 return 0;
2382 if (! d_check_char (di, '_'))
2383 return 0;
2385 return 1;
2388 /* <ctor-dtor-name> ::= C1
2389 ::= C2
2390 ::= C3
2391 ::= D0
2392 ::= D1
2393 ::= D2
2396 static struct demangle_component *
2397 d_ctor_dtor_name (struct d_info *di)
2399 if (di->last_name != NULL)
2401 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2402 di->expansion += di->last_name->u.s_name.len;
2403 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2404 di->expansion += di->last_name->u.s_string.len;
2406 switch (d_peek_char (di))
2408 case 'C':
2410 enum gnu_v3_ctor_kinds kind;
2411 int inheriting = 0;
2413 if (d_peek_next_char (di) == 'I')
2415 inheriting = 1;
2416 d_advance (di, 1);
2419 switch (d_peek_next_char (di))
2421 case '1':
2422 kind = gnu_v3_complete_object_ctor;
2423 break;
2424 case '2':
2425 kind = gnu_v3_base_object_ctor;
2426 break;
2427 case '3':
2428 kind = gnu_v3_complete_object_allocating_ctor;
2429 break;
2430 case '4':
2431 kind = gnu_v3_unified_ctor;
2432 break;
2433 case '5':
2434 kind = gnu_v3_object_ctor_group;
2435 break;
2436 default:
2437 return NULL;
2440 d_advance (di, 2);
2442 if (inheriting)
2443 cplus_demangle_type (di);
2445 return d_make_ctor (di, kind, di->last_name);
2448 case 'D':
2450 enum gnu_v3_dtor_kinds kind;
2452 switch (d_peek_next_char (di))
2454 case '0':
2455 kind = gnu_v3_deleting_dtor;
2456 break;
2457 case '1':
2458 kind = gnu_v3_complete_object_dtor;
2459 break;
2460 case '2':
2461 kind = gnu_v3_base_object_dtor;
2462 break;
2463 /* digit '3' is not used */
2464 case '4':
2465 kind = gnu_v3_unified_dtor;
2466 break;
2467 case '5':
2468 kind = gnu_v3_object_dtor_group;
2469 break;
2470 default:
2471 return NULL;
2473 d_advance (di, 2);
2474 return d_make_dtor (di, kind, di->last_name);
2477 default:
2478 return NULL;
2482 /* True iff we're looking at an order-insensitive type-qualifier, including
2483 function-type-qualifiers. */
2485 static int
2486 next_is_type_qual (struct d_info *di)
2488 char peek = d_peek_char (di);
2489 if (peek == 'r' || peek == 'V' || peek == 'K')
2490 return 1;
2491 if (peek == 'D')
2493 peek = d_peek_next_char (di);
2494 if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2495 return 1;
2497 return 0;
2500 /* <type> ::= <builtin-type>
2501 ::= <function-type>
2502 ::= <class-enum-type>
2503 ::= <array-type>
2504 ::= <pointer-to-member-type>
2505 ::= <template-param>
2506 ::= <template-template-param> <template-args>
2507 ::= <substitution>
2508 ::= <CV-qualifiers> <type>
2509 ::= P <type>
2510 ::= R <type>
2511 ::= O <type> (C++0x)
2512 ::= C <type>
2513 ::= G <type>
2514 ::= U <source-name> <type>
2516 <builtin-type> ::= various one letter codes
2517 ::= u <source-name>
2520 CP_STATIC_IF_GLIBCPP_V3
2521 const struct demangle_builtin_type_info
2522 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2524 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2525 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2526 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2527 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2528 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2529 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2530 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2531 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2532 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2533 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2534 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2535 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2536 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2537 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2538 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2539 D_PRINT_DEFAULT },
2540 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2541 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2542 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2543 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2544 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2545 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2546 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2547 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2548 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2549 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2550 D_PRINT_UNSIGNED_LONG_LONG },
2551 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2552 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2553 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2554 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2555 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2556 /* 30 */ { NL ("char8_t"), NL ("char8_t"), D_PRINT_DEFAULT },
2557 /* 31 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2558 /* 32 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2559 /* 33 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2560 D_PRINT_DEFAULT },
2561 /* 34 */ { NL ("_Float"), NL ("_Float"), D_PRINT_FLOAT },
2562 /* 35 */ { NL ("std::bfloat16_t"), NL ("std::bfloat16_t"), D_PRINT_FLOAT },
2565 CP_STATIC_IF_GLIBCPP_V3
2566 struct demangle_component *
2567 cplus_demangle_type (struct d_info *di)
2569 char peek;
2570 struct demangle_component *ret = NULL;
2571 int can_subst;
2573 /* The ABI specifies that when CV-qualifiers are used, the base type
2574 is substitutable, and the fully qualified type is substitutable,
2575 but the base type with a strict subset of the CV-qualifiers is
2576 not substitutable. The natural recursive implementation of the
2577 CV-qualifiers would cause subsets to be substitutable, so instead
2578 we pull them all off now.
2580 FIXME: The ABI says that order-insensitive vendor qualifiers
2581 should be handled in the same way, but we have no way to tell
2582 which vendor qualifiers are order-insensitive and which are
2583 order-sensitive. So we just assume that they are all
2584 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2585 __vector, and it treats it as order-sensitive when mangling
2586 names. */
2588 if (next_is_type_qual (di))
2590 struct demangle_component **pret;
2592 pret = d_cv_qualifiers (di, &ret, 0);
2593 if (pret == NULL)
2594 return NULL;
2595 if (d_peek_char (di) == 'F')
2597 /* cv-qualifiers before a function type apply to 'this',
2598 so avoid adding the unqualified function type to
2599 the substitution list. */
2600 *pret = d_function_type (di);
2602 else
2603 *pret = cplus_demangle_type (di);
2604 if (!*pret)
2605 return NULL;
2606 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2607 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2609 /* Move the ref-qualifier outside the cv-qualifiers so that
2610 they are printed in the right order. */
2611 struct demangle_component *fn = d_left (*pret);
2612 d_left (*pret) = ret;
2613 ret = *pret;
2614 *pret = fn;
2616 if (! d_add_substitution (di, ret))
2617 return NULL;
2618 return ret;
2621 can_subst = 1;
2623 peek = d_peek_char (di);
2624 switch (peek)
2626 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2627 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2628 case 'o': case 's': case 't':
2629 case 'v': case 'w': case 'x': case 'y': case 'z':
2630 ret = d_make_builtin_type (di,
2631 &cplus_demangle_builtin_types[peek - 'a']);
2632 di->expansion += ret->u.s_builtin.type->len;
2633 can_subst = 0;
2634 d_advance (di, 1);
2635 break;
2637 case 'u':
2638 d_advance (di, 1);
2639 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2640 d_source_name (di), NULL);
2641 break;
2643 case 'F':
2644 ret = d_function_type (di);
2645 break;
2647 case 'A':
2648 ret = d_array_type (di);
2649 break;
2651 case 'M':
2652 ret = d_pointer_to_member_type (di);
2653 break;
2655 case 'T':
2656 ret = d_template_param (di);
2657 if (d_peek_char (di) == 'I')
2659 /* This may be <template-template-param> <template-args>.
2660 If this is the type for a conversion operator, we can
2661 have a <template-template-param> here only by following
2662 a derivation like this:
2664 <nested-name>
2665 -> <template-prefix> <template-args>
2666 -> <prefix> <template-unqualified-name> <template-args>
2667 -> <unqualified-name> <template-unqualified-name> <template-args>
2668 -> <source-name> <template-unqualified-name> <template-args>
2669 -> <source-name> <operator-name> <template-args>
2670 -> <source-name> cv <type> <template-args>
2671 -> <source-name> cv <template-template-param> <template-args> <template-args>
2673 where the <template-args> is followed by another.
2674 Otherwise, we must have a derivation like this:
2676 <nested-name>
2677 -> <template-prefix> <template-args>
2678 -> <prefix> <template-unqualified-name> <template-args>
2679 -> <unqualified-name> <template-unqualified-name> <template-args>
2680 -> <source-name> <template-unqualified-name> <template-args>
2681 -> <source-name> <operator-name> <template-args>
2682 -> <source-name> cv <type> <template-args>
2683 -> <source-name> cv <template-param> <template-args>
2685 where we need to leave the <template-args> to be processed
2686 by d_prefix (following the <template-prefix>).
2688 The <template-template-param> part is a substitution
2689 candidate. */
2690 if (! di->is_conversion)
2692 if (! d_add_substitution (di, ret))
2693 return NULL;
2694 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2695 d_template_args (di));
2697 else
2699 struct demangle_component *args;
2700 struct d_info_checkpoint checkpoint;
2702 d_checkpoint (di, &checkpoint);
2703 args = d_template_args (di);
2704 if (d_peek_char (di) == 'I')
2706 if (! d_add_substitution (di, ret))
2707 return NULL;
2708 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2709 args);
2711 else
2712 d_backtrack (di, &checkpoint);
2715 break;
2717 case 'O':
2718 d_advance (di, 1);
2719 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2720 cplus_demangle_type (di), NULL);
2721 break;
2723 case 'P':
2724 d_advance (di, 1);
2725 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2726 cplus_demangle_type (di), NULL);
2727 break;
2729 case 'R':
2730 d_advance (di, 1);
2731 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2732 cplus_demangle_type (di), NULL);
2733 break;
2735 case 'C':
2736 d_advance (di, 1);
2737 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2738 cplus_demangle_type (di), NULL);
2739 break;
2741 case 'G':
2742 d_advance (di, 1);
2743 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2744 cplus_demangle_type (di), NULL);
2745 break;
2747 case 'U':
2748 d_advance (di, 1);
2749 ret = d_source_name (di);
2750 if (d_peek_char (di) == 'I')
2751 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2752 d_template_args (di));
2753 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2754 cplus_demangle_type (di), ret);
2755 break;
2757 case 'D':
2758 can_subst = 0;
2759 d_advance (di, 1);
2760 peek = d_next_char (di);
2761 switch (peek)
2763 case 'T':
2764 case 't':
2765 /* decltype (expression) */
2766 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2767 d_expression (di), NULL);
2768 if (ret && d_next_char (di) != 'E')
2769 ret = NULL;
2770 can_subst = 1;
2771 break;
2773 case 'p':
2774 /* Pack expansion. */
2775 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2776 cplus_demangle_type (di), NULL);
2777 can_subst = 1;
2778 break;
2780 case 'a':
2781 /* auto */
2782 ret = d_make_name (di, "auto", 4);
2783 break;
2784 case 'c':
2785 /* decltype(auto) */
2786 ret = d_make_name (di, "decltype(auto)", 14);
2787 break;
2789 case 'f':
2790 /* 32-bit decimal floating point */
2791 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2792 di->expansion += ret->u.s_builtin.type->len;
2793 break;
2794 case 'd':
2795 /* 64-bit DFP */
2796 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2797 di->expansion += ret->u.s_builtin.type->len;
2798 break;
2799 case 'e':
2800 /* 128-bit DFP */
2801 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2802 di->expansion += ret->u.s_builtin.type->len;
2803 break;
2804 case 'h':
2805 /* 16-bit half-precision FP */
2806 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2807 di->expansion += ret->u.s_builtin.type->len;
2808 break;
2809 case 'u':
2810 /* char8_t */
2811 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2812 di->expansion += ret->u.s_builtin.type->len;
2813 break;
2814 case 's':
2815 /* char16_t */
2816 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2817 di->expansion += ret->u.s_builtin.type->len;
2818 break;
2819 case 'i':
2820 /* char32_t */
2821 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2822 di->expansion += ret->u.s_builtin.type->len;
2823 break;
2825 case 'F':
2826 /* DF<number>_ - _Float<number>.
2827 DF<number>x - _Float<number>x
2828 DF16b - std::bfloat16_t. */
2830 int arg = d_number (di);
2831 char buf[12];
2832 char suffix = 0;
2833 if (d_peek_char (di) == 'b')
2835 if (arg != 16)
2836 return NULL;
2837 d_advance (di, 1);
2838 ret = d_make_builtin_type (di,
2839 &cplus_demangle_builtin_types[35]);
2840 di->expansion += ret->u.s_builtin.type->len;
2841 break;
2843 if (d_peek_char (di) == 'x')
2844 suffix = 'x';
2845 if (!suffix && d_peek_char (di) != '_')
2846 return NULL;
2848 = d_make_extended_builtin_type (di,
2849 &cplus_demangle_builtin_types[34],
2850 arg, suffix);
2851 d_advance (di, 1);
2852 sprintf (buf, "%d", arg);
2853 di->expansion += ret->u.s_extended_builtin.type->len
2854 + strlen (buf) + (suffix != 0);
2855 break;
2858 case 'v':
2859 ret = d_vector_type (di);
2860 can_subst = 1;
2861 break;
2863 case 'n':
2864 /* decltype(nullptr) */
2865 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[33]);
2866 di->expansion += ret->u.s_builtin.type->len;
2867 break;
2869 default:
2870 return NULL;
2872 break;
2874 default:
2875 return d_class_enum_type (di, 1);
2878 if (can_subst)
2880 if (! d_add_substitution (di, ret))
2881 return NULL;
2884 return ret;
2887 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2889 static struct demangle_component **
2890 d_cv_qualifiers (struct d_info *di,
2891 struct demangle_component **pret, int member_fn)
2893 struct demangle_component **pstart;
2894 char peek;
2896 pstart = pret;
2897 peek = d_peek_char (di);
2898 while (next_is_type_qual (di))
2900 enum demangle_component_type t;
2901 struct demangle_component *right = NULL;
2903 d_advance (di, 1);
2904 if (peek == 'r')
2906 t = (member_fn
2907 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2908 : DEMANGLE_COMPONENT_RESTRICT);
2909 di->expansion += sizeof "restrict";
2911 else if (peek == 'V')
2913 t = (member_fn
2914 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2915 : DEMANGLE_COMPONENT_VOLATILE);
2916 di->expansion += sizeof "volatile";
2918 else if (peek == 'K')
2920 t = (member_fn
2921 ? DEMANGLE_COMPONENT_CONST_THIS
2922 : DEMANGLE_COMPONENT_CONST);
2923 di->expansion += sizeof "const";
2925 else
2927 peek = d_next_char (di);
2928 if (peek == 'x')
2930 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2931 di->expansion += sizeof "transaction_safe";
2933 else if (peek == 'o'
2934 || peek == 'O')
2936 t = DEMANGLE_COMPONENT_NOEXCEPT;
2937 di->expansion += sizeof "noexcept";
2938 if (peek == 'O')
2940 right = d_expression (di);
2941 if (right == NULL)
2942 return NULL;
2943 if (! d_check_char (di, 'E'))
2944 return NULL;
2947 else if (peek == 'w')
2949 t = DEMANGLE_COMPONENT_THROW_SPEC;
2950 di->expansion += sizeof "throw";
2951 right = d_parmlist (di);
2952 if (right == NULL)
2953 return NULL;
2954 if (! d_check_char (di, 'E'))
2955 return NULL;
2957 else
2958 return NULL;
2961 *pret = d_make_comp (di, t, NULL, right);
2962 if (*pret == NULL)
2963 return NULL;
2964 pret = &d_left (*pret);
2966 peek = d_peek_char (di);
2969 if (!member_fn && peek == 'F')
2971 while (pstart != pret)
2973 switch ((*pstart)->type)
2975 case DEMANGLE_COMPONENT_RESTRICT:
2976 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2977 break;
2978 case DEMANGLE_COMPONENT_VOLATILE:
2979 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2980 break;
2981 case DEMANGLE_COMPONENT_CONST:
2982 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2983 break;
2984 default:
2985 break;
2987 pstart = &d_left (*pstart);
2991 return pret;
2994 /* <ref-qualifier> ::= R
2995 ::= O */
2997 static struct demangle_component *
2998 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
3000 struct demangle_component *ret = sub;
3001 char peek;
3003 peek = d_peek_char (di);
3004 if (peek == 'R' || peek == 'O')
3006 enum demangle_component_type t;
3007 if (peek == 'R')
3009 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
3010 di->expansion += sizeof "&";
3012 else
3014 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
3015 di->expansion += sizeof "&&";
3017 d_advance (di, 1);
3019 ret = d_make_comp (di, t, ret, NULL);
3022 return ret;
3025 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
3027 static struct demangle_component *
3028 d_function_type (struct d_info *di)
3030 struct demangle_component *ret = NULL;
3032 if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
3034 if (di->recursion_level > DEMANGLE_RECURSION_LIMIT)
3035 /* FIXME: There ought to be a way to report
3036 that the recursion limit has been reached. */
3037 return NULL;
3039 di->recursion_level ++;
3042 if (d_check_char (di, 'F'))
3044 if (d_peek_char (di) == 'Y')
3046 /* Function has C linkage. We don't print this information.
3047 FIXME: We should print it in verbose mode. */
3048 d_advance (di, 1);
3050 ret = d_bare_function_type (di, 1);
3051 ret = d_ref_qualifier (di, ret);
3053 if (! d_check_char (di, 'E'))
3054 ret = NULL;
3057 if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
3058 di->recursion_level --;
3059 return ret;
3062 /* <type>+ */
3064 static struct demangle_component *
3065 d_parmlist (struct d_info *di)
3067 struct demangle_component *tl;
3068 struct demangle_component **ptl;
3070 tl = NULL;
3071 ptl = &tl;
3072 while (1)
3074 struct demangle_component *type;
3076 char peek = d_peek_char (di);
3077 if (peek == '\0' || peek == 'E' || peek == '.' || peek == 'Q')
3078 break;
3079 if ((peek == 'R' || peek == 'O')
3080 && d_peek_next_char (di) == 'E')
3081 /* Function ref-qualifier, not a ref prefix for a parameter type. */
3082 break;
3083 type = cplus_demangle_type (di);
3084 if (type == NULL)
3085 return NULL;
3086 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
3087 if (*ptl == NULL)
3088 return NULL;
3089 ptl = &d_right (*ptl);
3092 /* There should be at least one parameter type besides the optional
3093 return type. A function which takes no arguments will have a
3094 single parameter type void. */
3095 if (tl == NULL)
3096 return NULL;
3098 /* If we have a single parameter type void, omit it. */
3099 if (d_right (tl) == NULL
3100 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3101 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
3103 di->expansion -= d_left (tl)->u.s_builtin.type->len;
3104 d_left (tl) = NULL;
3107 return tl;
3110 /* <bare-function-type> ::= [J]<type>+ */
3112 static struct demangle_component *
3113 d_bare_function_type (struct d_info *di, int has_return_type)
3115 struct demangle_component *return_type;
3116 struct demangle_component *tl;
3117 char peek;
3119 /* Detect special qualifier indicating that the first argument
3120 is the return type. */
3121 peek = d_peek_char (di);
3122 if (peek == 'J')
3124 d_advance (di, 1);
3125 has_return_type = 1;
3128 if (has_return_type)
3130 return_type = cplus_demangle_type (di);
3131 if (return_type == NULL)
3132 return NULL;
3134 else
3135 return_type = NULL;
3137 tl = d_parmlist (di);
3138 if (tl == NULL)
3139 return NULL;
3141 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
3142 return_type, tl);
3145 /* <class-enum-type> ::= <name> */
3147 static struct demangle_component *
3148 d_class_enum_type (struct d_info *di, int substable)
3150 return d_name (di, substable);
3153 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
3154 ::= A [<(dimension) expression>] _ <(element) type>
3157 static struct demangle_component *
3158 d_array_type (struct d_info *di)
3160 char peek;
3161 struct demangle_component *dim;
3163 if (! d_check_char (di, 'A'))
3164 return NULL;
3166 peek = d_peek_char (di);
3167 if (peek == '_')
3168 dim = NULL;
3169 else if (IS_DIGIT (peek))
3171 const char *s;
3173 s = d_str (di);
3176 d_advance (di, 1);
3177 peek = d_peek_char (di);
3179 while (IS_DIGIT (peek));
3180 dim = d_make_name (di, s, d_str (di) - s);
3181 if (dim == NULL)
3182 return NULL;
3184 else
3186 dim = d_expression (di);
3187 if (dim == NULL)
3188 return NULL;
3191 if (! d_check_char (di, '_'))
3192 return NULL;
3194 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
3195 cplus_demangle_type (di));
3198 /* <vector-type> ::= Dv <number> _ <type>
3199 ::= Dv _ <expression> _ <type> */
3201 static struct demangle_component *
3202 d_vector_type (struct d_info *di)
3204 char peek;
3205 struct demangle_component *dim;
3207 peek = d_peek_char (di);
3208 if (peek == '_')
3210 d_advance (di, 1);
3211 dim = d_expression (di);
3213 else
3214 dim = d_number_component (di);
3216 if (dim == NULL)
3217 return NULL;
3219 if (! d_check_char (di, '_'))
3220 return NULL;
3222 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
3223 cplus_demangle_type (di));
3226 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
3228 static struct demangle_component *
3229 d_pointer_to_member_type (struct d_info *di)
3231 struct demangle_component *cl;
3232 struct demangle_component *mem;
3234 if (! d_check_char (di, 'M'))
3235 return NULL;
3237 cl = cplus_demangle_type (di);
3238 if (cl == NULL)
3239 return NULL;
3241 /* The ABI says, "The type of a non-static member function is considered
3242 to be different, for the purposes of substitution, from the type of a
3243 namespace-scope or static member function whose type appears
3244 similar. The types of two non-static member functions are considered
3245 to be different, for the purposes of substitution, if the functions
3246 are members of different classes. In other words, for the purposes of
3247 substitution, the class of which the function is a member is
3248 considered part of the type of function."
3250 For a pointer to member function, this call to cplus_demangle_type
3251 will end up adding a (possibly qualified) non-member function type to
3252 the substitution table, which is not correct; however, the member
3253 function type will never be used in a substitution, so putting the
3254 wrong type in the substitution table is harmless. */
3256 mem = cplus_demangle_type (di);
3257 if (mem == NULL)
3258 return NULL;
3260 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
3263 /* <non-negative number> _ */
3265 static int
3266 d_compact_number (struct d_info *di)
3268 int num;
3269 if (d_peek_char (di) == '_')
3270 num = 0;
3271 else if (d_peek_char (di) == 'n')
3272 return -1;
3273 else
3274 num = d_number (di) + 1;
3276 if (num < 0 || ! d_check_char (di, '_'))
3277 return -1;
3278 return num;
3281 /* <template-param> ::= T_
3282 ::= T <(parameter-2 non-negative) number> _
3285 static struct demangle_component *
3286 d_template_param (struct d_info *di)
3288 int param;
3290 if (! d_check_char (di, 'T'))
3291 return NULL;
3293 param = d_compact_number (di);
3294 if (param < 0)
3295 return NULL;
3297 return d_make_template_param (di, param);
3300 /* <template-args> ::= I <template-arg>+ E */
3302 static struct demangle_component *
3303 d_template_args (struct d_info *di)
3305 if (d_peek_char (di) != 'I'
3306 && d_peek_char (di) != 'J')
3307 return NULL;
3308 d_advance (di, 1);
3310 return d_template_args_1 (di);
3313 /* <template-arg>* [Q <constraint-expression>] E */
3315 static struct demangle_component *
3316 d_template_args_1 (struct d_info *di)
3318 struct demangle_component *hold_last_name;
3319 struct demangle_component *al;
3320 struct demangle_component **pal;
3322 /* Preserve the last name we saw--don't let the template arguments
3323 clobber it, as that would give us the wrong name for a subsequent
3324 constructor or destructor. */
3325 hold_last_name = di->last_name;
3327 if (d_peek_char (di) == 'E')
3329 /* An argument pack can be empty. */
3330 d_advance (di, 1);
3331 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3334 al = NULL;
3335 pal = &al;
3336 while (1)
3338 struct demangle_component *a;
3340 a = d_template_arg (di);
3341 if (a == NULL)
3342 return NULL;
3344 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3345 if (*pal == NULL)
3346 return NULL;
3347 pal = &d_right (*pal);
3349 char peek = d_peek_char (di);
3350 if (peek == 'E' || peek == 'Q')
3351 break;
3354 al = d_maybe_constraints (di, al);
3356 if (d_peek_char (di) != 'E')
3357 return NULL;
3358 d_advance (di, 1);
3360 di->last_name = hold_last_name;
3362 return al;
3365 /* <template-arg> ::= <type>
3366 ::= X <expression> E
3367 ::= <expr-primary>
3370 static struct demangle_component *
3371 d_template_arg (struct d_info *di)
3373 struct demangle_component *ret;
3375 switch (d_peek_char (di))
3377 case 'X':
3378 d_advance (di, 1);
3379 ret = d_expression (di);
3380 if (! d_check_char (di, 'E'))
3381 return NULL;
3382 return ret;
3384 case 'L':
3385 return d_expr_primary (di);
3387 case 'I':
3388 case 'J':
3389 /* An argument pack. */
3390 return d_template_args (di);
3392 default:
3393 return cplus_demangle_type (di);
3397 /* Parse a sequence of expressions until we hit the terminator
3398 character. */
3400 static struct demangle_component *
3401 d_exprlist (struct d_info *di, char terminator)
3403 struct demangle_component *list = NULL;
3404 struct demangle_component **p = &list;
3406 if (d_peek_char (di) == terminator)
3408 d_advance (di, 1);
3409 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3412 while (1)
3414 struct demangle_component *arg = d_expression (di);
3415 if (arg == NULL)
3416 return NULL;
3418 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3419 if (*p == NULL)
3420 return NULL;
3421 p = &d_right (*p);
3423 if (d_peek_char (di) == terminator)
3425 d_advance (di, 1);
3426 break;
3430 return list;
3433 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3434 dynamic_cast, static_cast or reinterpret_cast. */
3436 static int
3437 op_is_new_cast (struct demangle_component *op)
3439 const char *code = op->u.s_operator.op->code;
3440 return (code[1] == 'c'
3441 && (code[0] == 's' || code[0] == 'd'
3442 || code[0] == 'c' || code[0] == 'r'));
3445 /* <unresolved-name> ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3446 ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3447 # T::N::x /decltype(p)::N::x
3448 ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3449 # A::x, N::y, A<T>::z; "gs" means leading "::"
3450 ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3452 "gs" is handled elsewhere, as a unary operator. */
3454 static struct demangle_component *
3455 d_unresolved_name (struct d_info *di)
3457 struct demangle_component *type;
3458 struct demangle_component *name;
3459 char peek;
3461 /* Consume the "sr". */
3462 d_advance (di, 2);
3464 peek = d_peek_char (di);
3465 if (di->unresolved_name_state
3466 && (IS_DIGIT (peek)
3467 || IS_LOWER (peek)
3468 || peek == 'C'
3469 || peek == 'U'
3470 || peek == 'L'))
3472 /* The third production is ambiguous with the old unresolved-name syntax
3473 of <type> <base-unresolved-name>; in the old mangling, A::x was mangled
3474 as sr1A1x, now sr1AE1x. So we first try to demangle using the new
3475 mangling, then with the old if that fails. */
3476 di->unresolved_name_state = -1;
3477 type = d_prefix (di, 0);
3478 if (d_peek_char (di) == 'E')
3479 d_advance (di, 1);
3481 else
3482 type = cplus_demangle_type (di);
3483 name = d_unqualified_name (di, type, NULL);
3484 if (d_peek_char (di) == 'I')
3485 name = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3486 d_template_args (di));
3487 return name;
3490 /* <expression> ::= <(unary) operator-name> <expression>
3491 ::= <(binary) operator-name> <expression> <expression>
3492 ::= <(trinary) operator-name> <expression> <expression> <expression>
3493 ::= cl <expression>+ E
3494 ::= st <type>
3495 ::= <template-param>
3496 ::= u <source-name> <template-arg>* E # vendor extended expression
3497 ::= <unresolved-name>
3498 ::= <expr-primary>
3500 <braced-expression> ::= <expression>
3501 ::= di <field source-name> <braced-expression> # .name = expr
3502 ::= dx <index expression> <braced-expression> # [expr] = expr
3503 ::= dX <range begin expression> <range end expression> <braced-expression>
3504 # [expr ... expr] = expr
3507 static struct demangle_component *
3508 d_expression_1 (struct d_info *di)
3510 char peek;
3512 peek = d_peek_char (di);
3513 if (peek == 'L')
3514 return d_expr_primary (di);
3515 else if (peek == 'T')
3516 return d_template_param (di);
3517 else if (peek == 's' && d_peek_next_char (di) == 'r')
3518 return d_unresolved_name (di);
3519 else if (peek == 's' && d_peek_next_char (di) == 'p')
3521 d_advance (di, 2);
3522 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3523 d_expression_1 (di), NULL);
3525 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3527 /* Function parameter used in a late-specified return type. */
3528 int index;
3529 d_advance (di, 2);
3530 if (d_peek_char (di) == 'T')
3532 /* 'this' parameter. */
3533 d_advance (di, 1);
3534 index = 0;
3536 else
3538 index = d_compact_number (di);
3539 if (index == INT_MAX || index == -1)
3540 return NULL;
3541 index++;
3543 return d_make_function_param (di, index);
3545 else if (IS_DIGIT (peek)
3546 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3548 /* We can get an unqualified name as an expression in the case of
3549 a dependent function call, i.e. decltype(f(t)). */
3550 struct demangle_component *name;
3552 if (peek == 'o')
3553 /* operator-function-id, i.e. operator+(t). */
3554 d_advance (di, 2);
3556 name = d_unqualified_name (di, NULL, NULL);
3557 if (name == NULL)
3558 return NULL;
3559 if (d_peek_char (di) == 'I')
3560 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3561 d_template_args (di));
3562 else
3563 return name;
3565 else if ((peek == 'i' || peek == 't')
3566 && d_peek_next_char (di) == 'l')
3568 /* Brace-enclosed initializer list, untyped or typed. */
3569 struct demangle_component *type = NULL;
3570 d_advance (di, 2);
3571 if (peek == 't')
3572 type = cplus_demangle_type (di);
3573 if (!d_peek_char (di) || !d_peek_next_char (di))
3574 return NULL;
3575 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3576 type, d_exprlist (di, 'E'));
3578 else if (peek == 'u')
3580 /* A vendor extended expression. */
3581 struct demangle_component *name, *args;
3582 d_advance (di, 1);
3583 name = d_source_name (di);
3584 args = d_template_args_1 (di);
3585 return d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_EXPR, name, args);
3587 else
3589 struct demangle_component *op;
3590 const char *code = NULL;
3591 int args;
3593 op = d_operator_name (di);
3594 if (op == NULL)
3595 return NULL;
3597 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3599 code = op->u.s_operator.op->code;
3600 di->expansion += op->u.s_operator.op->len - 2;
3601 if (strcmp (code, "st") == 0)
3602 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3603 cplus_demangle_type (di));
3606 switch (op->type)
3608 default:
3609 return NULL;
3610 case DEMANGLE_COMPONENT_OPERATOR:
3611 args = op->u.s_operator.op->args;
3612 break;
3613 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3614 args = op->u.s_extended_operator.args;
3615 break;
3616 case DEMANGLE_COMPONENT_CAST:
3617 args = 1;
3618 break;
3621 switch (args)
3623 case 0:
3624 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3626 case 1:
3628 struct demangle_component *operand;
3629 int suffix = 0;
3631 if (code && (code[0] == 'p' || code[0] == 'm')
3632 && code[1] == code[0])
3633 /* pp_ and mm_ are the prefix variants. */
3634 suffix = !d_check_char (di, '_');
3636 if (op->type == DEMANGLE_COMPONENT_CAST
3637 && d_check_char (di, '_'))
3638 operand = d_exprlist (di, 'E');
3639 else if (code && !strcmp (code, "sP"))
3640 operand = d_template_args_1 (di);
3641 else
3642 operand = d_expression_1 (di);
3644 if (suffix)
3645 /* Indicate the suffix variant for d_print_comp. */
3646 operand = d_make_comp (di, DEMANGLE_COMPONENT_BINARY_ARGS,
3647 operand, operand);
3649 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, operand);
3651 case 2:
3653 struct demangle_component *left;
3654 struct demangle_component *right;
3656 if (code == NULL)
3657 return NULL;
3658 if (op_is_new_cast (op))
3659 left = cplus_demangle_type (di);
3660 else if (code[0] == 'f')
3661 /* fold-expression. */
3662 left = d_operator_name (di);
3663 else if (!strcmp (code, "di"))
3664 left = d_unqualified_name (di, NULL, NULL);
3665 else
3666 left = d_expression_1 (di);
3667 if (!strcmp (code, "cl"))
3668 right = d_exprlist (di, 'E');
3669 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3671 peek = d_peek_char (di);
3672 /* These codes start a qualified name. */
3673 if ((peek == 'g' && d_peek_next_char (di) == 's')
3674 || (peek == 's' && d_peek_next_char (di) == 'r'))
3675 right = d_expression_1 (di);
3676 else
3678 /* Otherwise it's an unqualified name. We use
3679 d_unqualified_name rather than d_expression_1 here for
3680 old mangled names that didn't add 'on' before operator
3681 names. */
3682 right = d_unqualified_name (di, NULL, NULL);
3683 if (d_peek_char (di) == 'I')
3684 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3685 right, d_template_args (di));
3688 else
3689 right = d_expression_1 (di);
3691 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3692 d_make_comp (di,
3693 DEMANGLE_COMPONENT_BINARY_ARGS,
3694 left, right));
3696 case 3:
3698 struct demangle_component *first;
3699 struct demangle_component *second;
3700 struct demangle_component *third;
3702 if (code == NULL)
3703 return NULL;
3704 else if (!strcmp (code, "qu")
3705 || !strcmp (code, "dX"))
3707 /* ?: expression. */
3708 first = d_expression_1 (di);
3709 second = d_expression_1 (di);
3710 third = d_expression_1 (di);
3711 if (third == NULL)
3712 return NULL;
3714 else if (code[0] == 'f')
3716 /* fold-expression. */
3717 first = d_operator_name (di);
3718 second = d_expression_1 (di);
3719 third = d_expression_1 (di);
3720 if (third == NULL)
3721 return NULL;
3723 else if (code[0] == 'n')
3725 /* new-expression. */
3726 if (code[1] != 'w' && code[1] != 'a')
3727 return NULL;
3728 first = d_exprlist (di, '_');
3729 second = cplus_demangle_type (di);
3730 if (d_peek_char (di) == 'E')
3732 d_advance (di, 1);
3733 third = NULL;
3735 else if (d_peek_char (di) == 'p'
3736 && d_peek_next_char (di) == 'i')
3738 /* Parenthesized initializer. */
3739 d_advance (di, 2);
3740 third = d_exprlist (di, 'E');
3742 else if (d_peek_char (di) == 'i'
3743 && d_peek_next_char (di) == 'l')
3744 /* initializer-list. */
3745 third = d_expression_1 (di);
3746 else
3747 return NULL;
3749 else
3750 return NULL;
3751 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3752 d_make_comp (di,
3753 DEMANGLE_COMPONENT_TRINARY_ARG1,
3754 first,
3755 d_make_comp (di,
3756 DEMANGLE_COMPONENT_TRINARY_ARG2,
3757 second, third)));
3759 default:
3760 return NULL;
3765 static struct demangle_component *
3766 d_expression (struct d_info *di)
3768 struct demangle_component *ret;
3769 int was_expression = di->is_expression;
3771 di->is_expression = 1;
3772 ret = d_expression_1 (di);
3773 di->is_expression = was_expression;
3774 return ret;
3777 /* <expr-primary> ::= L <type> <(value) number> E
3778 ::= L <type> <(value) float> E
3779 ::= L <mangled-name> E
3782 static struct demangle_component *
3783 d_expr_primary (struct d_info *di)
3785 struct demangle_component *ret;
3787 if (! d_check_char (di, 'L'))
3788 return NULL;
3789 if (d_peek_char (di) == '_'
3790 /* Workaround for G++ bug; see comment in write_template_arg. */
3791 || d_peek_char (di) == 'Z')
3792 ret = cplus_demangle_mangled_name (di, 0);
3793 else
3795 struct demangle_component *type;
3796 enum demangle_component_type t;
3797 const char *s;
3799 type = cplus_demangle_type (di);
3800 if (type == NULL)
3801 return NULL;
3803 /* If we have a type we know how to print, we aren't going to
3804 print the type name itself. */
3805 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3806 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3807 di->expansion -= type->u.s_builtin.type->len;
3809 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3810 && strcmp (type->u.s_builtin.type->name,
3811 cplus_demangle_builtin_types[33].name) == 0)
3813 if (d_peek_char (di) == 'E')
3815 d_advance (di, 1);
3816 return type;
3820 /* Rather than try to interpret the literal value, we just
3821 collect it as a string. Note that it's possible to have a
3822 floating point literal here. The ABI specifies that the
3823 format of such literals is machine independent. That's fine,
3824 but what's not fine is that versions of g++ up to 3.2 with
3825 -fabi-version=1 used upper case letters in the hex constant,
3826 and dumped out gcc's internal representation. That makes it
3827 hard to tell where the constant ends, and hard to dump the
3828 constant in any readable form anyhow. We don't attempt to
3829 handle these cases. */
3831 t = DEMANGLE_COMPONENT_LITERAL;
3832 if (d_peek_char (di) == 'n')
3834 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3835 d_advance (di, 1);
3837 s = d_str (di);
3838 while (d_peek_char (di) != 'E')
3840 if (d_peek_char (di) == '\0')
3841 return NULL;
3842 d_advance (di, 1);
3844 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3846 if (! d_check_char (di, 'E'))
3847 return NULL;
3848 return ret;
3851 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3852 ::= Z <(function) encoding> E s [<discriminator>]
3853 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3856 static struct demangle_component *
3857 d_local_name (struct d_info *di)
3859 struct demangle_component *function;
3860 struct demangle_component *name;
3862 if (! d_check_char (di, 'Z'))
3863 return NULL;
3865 function = d_encoding (di, 0);
3866 if (!function)
3867 return NULL;
3869 if (! d_check_char (di, 'E'))
3870 return NULL;
3872 if (d_peek_char (di) == 's')
3874 d_advance (di, 1);
3875 if (! d_discriminator (di))
3876 return NULL;
3877 name = d_make_name (di, "string literal", sizeof "string literal" - 1);
3879 else
3881 int num = -1;
3883 if (d_peek_char (di) == 'd')
3885 /* Default argument scope: d <number> _. */
3886 d_advance (di, 1);
3887 num = d_compact_number (di);
3888 if (num < 0)
3889 return NULL;
3892 name = d_name (di, 0);
3894 if (name
3895 /* Lambdas and unnamed types have internal discriminators
3896 and are not functions. */
3897 && name->type != DEMANGLE_COMPONENT_LAMBDA
3898 && name->type != DEMANGLE_COMPONENT_UNNAMED_TYPE)
3900 /* Read and ignore an optional discriminator. */
3901 if (! d_discriminator (di))
3902 return NULL;
3905 if (num >= 0)
3906 name = d_make_default_arg (di, num, name);
3909 /* Elide the return type of the containing function so as to not
3910 confuse the user thinking it is the return type of whatever local
3911 function we might be containing. */
3912 if (function->type == DEMANGLE_COMPONENT_TYPED_NAME
3913 && d_right (function)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3914 d_left (d_right (function)) = NULL;
3916 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3919 /* <discriminator> ::= _ <number> # when number < 10
3920 ::= __ <number> _ # when number >= 10
3922 <discriminator> ::= _ <number> # when number >=10
3923 is also accepted to support gcc versions that wrongly mangled that way.
3925 We demangle the discriminator, but we don't print it out. FIXME:
3926 We should print it out in verbose mode. */
3928 static int
3929 d_discriminator (struct d_info *di)
3931 int discrim, num_underscores = 1;
3933 if (d_peek_char (di) != '_')
3934 return 1;
3935 d_advance (di, 1);
3936 if (d_peek_char (di) == '_')
3938 ++num_underscores;
3939 d_advance (di, 1);
3942 discrim = d_number (di);
3943 if (discrim < 0)
3944 return 0;
3945 if (num_underscores > 1 && discrim >= 10)
3947 if (d_peek_char (di) == '_')
3948 d_advance (di, 1);
3949 else
3950 return 0;
3953 return 1;
3956 /* <template-parm> ::= Ty
3957 ::= Tn <type>
3958 ::= Tt <template-head> E
3959 ::= Tp <template-parm> */
3961 static struct demangle_component *
3962 d_template_parm (struct d_info *di, int *bad)
3964 if (d_peek_char (di) != 'T')
3965 return NULL;
3967 struct demangle_component *op;
3968 enum demangle_component_type kind;
3969 switch (d_peek_next_char (di))
3971 default:
3972 return NULL;
3974 case 'p': /* Pack */
3975 d_advance (di, 2);
3976 op = d_template_parm (di, bad);
3977 kind = DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM;
3978 if (!op)
3980 *bad = 1;
3981 return NULL;
3983 break;
3985 case 'y': /* Typename */
3986 d_advance (di, 2);
3987 op = NULL;
3988 kind = DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM;
3989 break;
3991 case 'n': /* Non-Type */
3992 d_advance (di, 2);
3993 op = cplus_demangle_type (di);
3994 kind = DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM;
3995 if (!op)
3997 *bad = 1;
3998 return NULL;
4000 break;
4002 case 't': /* Template */
4003 d_advance (di, 2);
4004 op = d_template_head (di, bad);
4005 kind = DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM;
4006 if (!op || !d_check_char (di, 'E'))
4008 *bad = 1;
4009 return NULL;
4013 return d_make_comp (di, kind, op, NULL);
4016 /* <template-head> ::= <template-head>? <template-parm> */
4018 static struct demangle_component *
4019 d_template_head (struct d_info *di, int *bad)
4021 struct demangle_component *res = NULL, **slot = &res;
4022 struct demangle_component *op;
4024 while ((op = d_template_parm (di, bad)))
4026 *slot = op;
4027 slot = &d_right (op);
4030 /* Wrap it in a template head, to make concatenating with any parm list, and
4031 printing simpler. */
4032 if (res)
4033 res = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_HEAD, res, NULL);
4035 return res;
4038 /* <closure-type-name> ::= Ul <template-head>? <lambda-sig> E [ <nonnegative number> ] _ */
4040 static struct demangle_component *
4041 d_lambda (struct d_info *di)
4043 if (! d_check_char (di, 'U'))
4044 return NULL;
4045 if (! d_check_char (di, 'l'))
4046 return NULL;
4048 int bad = 0;
4049 struct demangle_component *head = d_template_head (di, &bad);
4050 if (bad)
4051 return NULL;
4053 struct demangle_component *tl = d_parmlist (di);
4054 if (tl == NULL)
4055 return NULL;
4056 if (head)
4058 d_right (head) = tl;
4059 tl = head;
4062 if (! d_check_char (di, 'E'))
4063 return NULL;
4065 int num = d_compact_number (di);
4066 if (num < 0)
4067 return NULL;
4069 struct demangle_component *ret = d_make_empty (di);
4070 if (ret)
4072 ret->type = DEMANGLE_COMPONENT_LAMBDA;
4073 ret->u.s_unary_num.sub = tl;
4074 ret->u.s_unary_num.num = num;
4077 return ret;
4080 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
4082 static struct demangle_component *
4083 d_unnamed_type (struct d_info *di)
4085 struct demangle_component *ret;
4086 int num;
4088 if (! d_check_char (di, 'U'))
4089 return NULL;
4090 if (! d_check_char (di, 't'))
4091 return NULL;
4093 num = d_compact_number (di);
4094 if (num < 0)
4095 return NULL;
4097 ret = d_make_empty (di);
4098 if (ret)
4100 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
4101 ret->u.s_number.number = num;
4104 if (! d_add_substitution (di, ret))
4105 return NULL;
4107 return ret;
4110 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
4113 static struct demangle_component *
4114 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
4116 const char *suffix = d_str (di);
4117 const char *pend = suffix;
4118 struct demangle_component *n;
4120 if (*pend == '.' && (IS_LOWER (pend[1]) || IS_DIGIT (pend[1])
4121 || pend[1] == '_'))
4123 pend += 2;
4124 while (IS_LOWER (*pend) || IS_DIGIT (*pend) || *pend == '_')
4125 ++pend;
4127 while (*pend == '.' && IS_DIGIT (pend[1]))
4129 pend += 2;
4130 while (IS_DIGIT (*pend))
4131 ++pend;
4133 d_advance (di, pend - suffix);
4134 n = d_make_name (di, suffix, pend - suffix);
4135 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
4138 /* Add a new substitution. */
4140 static int
4141 d_add_substitution (struct d_info *di, struct demangle_component *dc)
4143 if (dc == NULL)
4144 return 0;
4145 if (di->next_sub >= di->num_subs)
4146 return 0;
4147 di->subs[di->next_sub] = dc;
4148 ++di->next_sub;
4149 return 1;
4152 /* <substitution> ::= S <seq-id> _
4153 ::= S_
4154 ::= St
4155 ::= Sa
4156 ::= Sb
4157 ::= Ss
4158 ::= Si
4159 ::= So
4160 ::= Sd
4162 If PREFIX is non-zero, then this type is being used as a prefix in
4163 a qualified name. In this case, for the standard substitutions, we
4164 need to check whether we are being used as a prefix for a
4165 constructor or destructor, and return a full template name.
4166 Otherwise we will get something like std::iostream::~iostream()
4167 which does not correspond particularly well to any function which
4168 actually appears in the source.
4171 static const struct d_standard_sub_info standard_subs[] =
4173 { 't', NL ("std"),
4174 NL ("std"),
4175 NULL, 0 },
4176 { 'a', NL ("std::allocator"),
4177 NL ("std::allocator"),
4178 NL ("allocator") },
4179 { 'b', NL ("std::basic_string"),
4180 NL ("std::basic_string"),
4181 NL ("basic_string") },
4182 { 's', NL ("std::string"),
4183 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
4184 NL ("basic_string") },
4185 { 'i', NL ("std::istream"),
4186 NL ("std::basic_istream<char, std::char_traits<char> >"),
4187 NL ("basic_istream") },
4188 { 'o', NL ("std::ostream"),
4189 NL ("std::basic_ostream<char, std::char_traits<char> >"),
4190 NL ("basic_ostream") },
4191 { 'd', NL ("std::iostream"),
4192 NL ("std::basic_iostream<char, std::char_traits<char> >"),
4193 NL ("basic_iostream") }
4196 static struct demangle_component *
4197 d_substitution (struct d_info *di, int prefix)
4199 char c;
4201 if (! d_check_char (di, 'S'))
4202 return NULL;
4204 c = d_next_char (di);
4205 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
4207 unsigned int id;
4209 id = 0;
4210 if (c != '_')
4214 unsigned int new_id;
4216 if (IS_DIGIT (c))
4217 new_id = id * 36 + c - '0';
4218 else if (IS_UPPER (c))
4219 new_id = id * 36 + c - 'A' + 10;
4220 else
4221 return NULL;
4222 if (new_id < id)
4223 return NULL;
4224 id = new_id;
4225 c = d_next_char (di);
4227 while (c != '_');
4229 ++id;
4232 if (id >= (unsigned int) di->next_sub)
4233 return NULL;
4235 return di->subs[id];
4237 else
4239 int verbose;
4240 const struct d_standard_sub_info *p;
4241 const struct d_standard_sub_info *pend;
4243 verbose = (di->options & DMGL_VERBOSE) != 0;
4244 if (! verbose && prefix)
4246 char peek;
4248 peek = d_peek_char (di);
4249 if (peek == 'C' || peek == 'D')
4250 verbose = 1;
4253 pend = (&standard_subs[0]
4254 + sizeof standard_subs / sizeof standard_subs[0]);
4255 for (p = &standard_subs[0]; p < pend; ++p)
4257 if (c == p->code)
4259 const char *s;
4260 int len;
4261 struct demangle_component *dc;
4263 if (p->set_last_name != NULL)
4264 di->last_name = d_make_sub (di, p->set_last_name,
4265 p->set_last_name_len);
4266 if (verbose)
4268 s = p->full_expansion;
4269 len = p->full_len;
4271 else
4273 s = p->simple_expansion;
4274 len = p->simple_len;
4276 di->expansion += len;
4277 dc = d_make_sub (di, s, len);
4278 if (d_peek_char (di) == 'B')
4280 /* If there are ABI tags on the abbreviation, it becomes
4281 a substitution candidate. */
4282 dc = d_abi_tags (di, dc);
4283 if (! d_add_substitution (di, dc))
4284 return NULL;
4286 return dc;
4290 return NULL;
4294 static void
4295 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
4297 checkpoint->n = di->n;
4298 checkpoint->next_comp = di->next_comp;
4299 checkpoint->next_sub = di->next_sub;
4300 checkpoint->expansion = di->expansion;
4303 static void
4304 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
4306 di->n = checkpoint->n;
4307 di->next_comp = checkpoint->next_comp;
4308 di->next_sub = checkpoint->next_sub;
4309 di->expansion = checkpoint->expansion;
4312 /* Initialize a growable string. */
4314 static void
4315 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
4317 dgs->buf = NULL;
4318 dgs->len = 0;
4319 dgs->alc = 0;
4320 dgs->allocation_failure = 0;
4322 if (estimate > 0)
4323 d_growable_string_resize (dgs, estimate);
4326 /* Grow a growable string to a given size. */
4328 static inline void
4329 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
4331 size_t newalc;
4332 char *newbuf;
4334 if (dgs->allocation_failure)
4335 return;
4337 /* Start allocation at two bytes to avoid any possibility of confusion
4338 with the special value of 1 used as a return in *palc to indicate
4339 allocation failures. */
4340 newalc = dgs->alc > 0 ? dgs->alc : 2;
4341 while (newalc < need)
4342 newalc <<= 1;
4344 newbuf = (char *) realloc ("demangle.dgsr.1", dgs->buf, newalc);
4345 if (newbuf == NULL)
4347 free (dgs->buf);
4348 dgs->buf = NULL;
4349 dgs->len = 0;
4350 dgs->alc = 0;
4351 dgs->allocation_failure = 1;
4352 return;
4354 dgs->buf = newbuf;
4355 dgs->alc = newalc;
4358 /* Append a buffer to a growable string. */
4360 static inline void
4361 d_growable_string_append_buffer (struct d_growable_string *dgs,
4362 const char *s, size_t l)
4364 size_t need;
4366 need = dgs->len + l + 1;
4367 if (need > dgs->alc)
4368 d_growable_string_resize (dgs, need);
4370 if (dgs->allocation_failure)
4371 return;
4373 memcpy (dgs->buf + dgs->len, s, l);
4374 dgs->buf[dgs->len + l] = '\0';
4375 dgs->len += l;
4378 /* Bridge growable strings to the callback mechanism. */
4380 static void
4381 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
4383 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
4385 d_growable_string_append_buffer (dgs, s, l);
4388 /* Walk the tree, counting the number of templates encountered, and
4389 the number of times a scope might be saved. These counts will be
4390 used to allocate data structures for d_print_comp, so the logic
4391 here must mirror the logic d_print_comp will use. It is not
4392 important that the resulting numbers are exact, so long as they
4393 are larger than the actual numbers encountered. */
4395 static void
4396 d_count_templates_scopes (struct d_print_info *dpi,
4397 struct demangle_component *dc)
4399 if (dc == NULL || dc->d_counting > 1 || dpi->recursion > MAX_RECURSION_COUNT)
4400 return;
4402 ++ dc->d_counting;
4404 switch (dc->type)
4406 case DEMANGLE_COMPONENT_NAME:
4407 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4408 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4409 case DEMANGLE_COMPONENT_SUB_STD:
4410 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4411 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4412 case DEMANGLE_COMPONENT_OPERATOR:
4413 case DEMANGLE_COMPONENT_CHARACTER:
4414 case DEMANGLE_COMPONENT_NUMBER:
4415 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4416 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
4417 case DEMANGLE_COMPONENT_MODULE_NAME:
4418 case DEMANGLE_COMPONENT_MODULE_PARTITION:
4419 case DEMANGLE_COMPONENT_MODULE_INIT:
4420 case DEMANGLE_COMPONENT_FIXED_TYPE:
4421 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
4422 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
4423 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
4424 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
4425 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
4426 break;
4428 case DEMANGLE_COMPONENT_TEMPLATE:
4429 dpi->num_copy_templates++;
4430 goto recurse_left_right;
4432 case DEMANGLE_COMPONENT_REFERENCE:
4433 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4434 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4435 dpi->num_saved_scopes++;
4436 goto recurse_left_right;
4438 case DEMANGLE_COMPONENT_QUAL_NAME:
4439 case DEMANGLE_COMPONENT_LOCAL_NAME:
4440 case DEMANGLE_COMPONENT_TYPED_NAME:
4441 case DEMANGLE_COMPONENT_VTABLE:
4442 case DEMANGLE_COMPONENT_VTT:
4443 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4444 case DEMANGLE_COMPONENT_TYPEINFO:
4445 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4446 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4447 case DEMANGLE_COMPONENT_THUNK:
4448 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4449 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4450 case DEMANGLE_COMPONENT_JAVA_CLASS:
4451 case DEMANGLE_COMPONENT_GUARD:
4452 case DEMANGLE_COMPONENT_TLS_INIT:
4453 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4454 case DEMANGLE_COMPONENT_REFTEMP:
4455 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4456 case DEMANGLE_COMPONENT_RESTRICT:
4457 case DEMANGLE_COMPONENT_VOLATILE:
4458 case DEMANGLE_COMPONENT_CONST:
4459 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4460 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4461 case DEMANGLE_COMPONENT_CONST_THIS:
4462 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4463 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4464 case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
4465 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4466 case DEMANGLE_COMPONENT_NOEXCEPT:
4467 case DEMANGLE_COMPONENT_THROW_SPEC:
4468 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4469 case DEMANGLE_COMPONENT_POINTER:
4470 case DEMANGLE_COMPONENT_COMPLEX:
4471 case DEMANGLE_COMPONENT_IMAGINARY:
4472 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4473 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4474 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4475 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4476 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4477 case DEMANGLE_COMPONENT_ARGLIST:
4478 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4479 case DEMANGLE_COMPONENT_TPARM_OBJ:
4480 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4481 case DEMANGLE_COMPONENT_CAST:
4482 case DEMANGLE_COMPONENT_CONVERSION:
4483 case DEMANGLE_COMPONENT_NULLARY:
4484 case DEMANGLE_COMPONENT_UNARY:
4485 case DEMANGLE_COMPONENT_BINARY:
4486 case DEMANGLE_COMPONENT_BINARY_ARGS:
4487 case DEMANGLE_COMPONENT_TRINARY:
4488 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4489 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4490 case DEMANGLE_COMPONENT_LITERAL:
4491 case DEMANGLE_COMPONENT_LITERAL_NEG:
4492 case DEMANGLE_COMPONENT_VENDOR_EXPR:
4493 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4494 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4495 case DEMANGLE_COMPONENT_DECLTYPE:
4496 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4497 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4498 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4499 case DEMANGLE_COMPONENT_TAGGED_NAME:
4500 case DEMANGLE_COMPONENT_CLONE:
4501 case DEMANGLE_COMPONENT_CONSTRAINTS:
4502 recurse_left_right:
4503 /* PR 89394 - Check for too much recursion. */
4504 if (dpi->recursion > DEMANGLE_RECURSION_LIMIT)
4505 /* FIXME: There ought to be a way to report to the
4506 user that the recursion limit has been reached. */
4507 return;
4509 ++ dpi->recursion;
4510 d_count_templates_scopes (dpi, d_left (dc));
4511 d_count_templates_scopes (dpi, d_right (dc));
4512 -- dpi->recursion;
4513 break;
4515 case DEMANGLE_COMPONENT_CTOR:
4516 d_count_templates_scopes (dpi, dc->u.s_ctor.name);
4517 break;
4519 case DEMANGLE_COMPONENT_DTOR:
4520 d_count_templates_scopes (dpi, dc->u.s_dtor.name);
4521 break;
4523 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4524 d_count_templates_scopes (dpi, dc->u.s_extended_operator.name);
4525 break;
4527 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4528 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4529 case DEMANGLE_COMPONENT_MODULE_ENTITY:
4530 case DEMANGLE_COMPONENT_FRIEND:
4531 d_count_templates_scopes (dpi, d_left (dc));
4532 break;
4534 case DEMANGLE_COMPONENT_LAMBDA:
4535 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4536 d_count_templates_scopes (dpi, dc->u.s_unary_num.sub);
4537 break;
4541 /* Initialize a print information structure. */
4543 static void
4544 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4545 void *opaque, struct demangle_component *dc)
4547 dpi->len = 0;
4548 dpi->last_char = '\0';
4549 dpi->templates = NULL;
4550 dpi->modifiers = NULL;
4551 dpi->pack_index = 0;
4552 dpi->flush_count = 0;
4554 dpi->callback = callback;
4555 dpi->opaque = opaque;
4557 dpi->demangle_failure = 0;
4558 dpi->recursion = 0;
4559 dpi->lambda_tpl_parms = 0;
4561 dpi->component_stack = NULL;
4563 dpi->saved_scopes = NULL;
4564 dpi->next_saved_scope = 0;
4565 dpi->num_saved_scopes = 0;
4567 dpi->copy_templates = NULL;
4568 dpi->next_copy_template = 0;
4569 dpi->num_copy_templates = 0;
4571 d_count_templates_scopes (dpi, dc);
4572 /* If we did not reach the recursion limit, then reset the
4573 current recursion value back to 0, so that we can print
4574 the templates. */
4575 if (dpi->recursion < DEMANGLE_RECURSION_LIMIT)
4576 dpi->recursion = 0;
4577 dpi->num_copy_templates *= dpi->num_saved_scopes;
4579 dpi->current_template = NULL;
4582 /* Indicate that an error occurred during printing, and test for error. */
4584 static inline void
4585 d_print_error (struct d_print_info *dpi)
4587 dpi->demangle_failure = 1;
4590 static inline int
4591 d_print_saw_error (struct d_print_info *dpi)
4593 return dpi->demangle_failure != 0;
4596 /* Flush buffered characters to the callback. */
4598 static inline void
4599 d_print_flush (struct d_print_info *dpi)
4601 dpi->buf[dpi->len] = '\0';
4602 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4603 dpi->len = 0;
4604 dpi->flush_count++;
4607 /* Append characters and buffers for printing. */
4609 static inline void
4610 d_append_char (struct d_print_info *dpi, char c)
4612 if (dpi->len == sizeof (dpi->buf) - 1)
4613 d_print_flush (dpi);
4615 dpi->buf[dpi->len++] = c;
4616 dpi->last_char = c;
4619 static inline void
4620 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4622 size_t i;
4624 for (i = 0; i < l; i++)
4625 d_append_char (dpi, s[i]);
4628 static inline void
4629 d_append_string (struct d_print_info *dpi, const char *s)
4631 d_append_buffer (dpi, s, strlen (s));
4634 static inline void
4635 d_append_num (struct d_print_info *dpi, int l)
4637 char buf[25];
4638 sprintf (buf,"%d", l);
4639 d_append_string (dpi, buf);
4642 static inline char
4643 d_last_char (struct d_print_info *dpi)
4645 return dpi->last_char;
4648 /* Turn components into a human readable string. OPTIONS is the
4649 options bits passed to the demangler. DC is the tree to print.
4650 CALLBACK is a function to call to flush demangled string segments
4651 as they fill the intermediate buffer, and OPAQUE is a generalized
4652 callback argument. On success, this returns 1. On failure,
4653 it returns 0, indicating a bad parse. It does not use heap
4654 memory to build an output string, so cannot encounter memory
4655 allocation failure. */
4657 CP_STATIC_IF_GLIBCPP_V3
4659 cplus_demangle_print_callback (int options,
4660 struct demangle_component *dc,
4661 demangle_callbackref callback, void *opaque)
4663 struct d_print_info dpi;
4665 d_print_init (&dpi, callback, opaque, dc);
4668 #if 0 /* in valgrind */
4669 #ifdef CP_DYNAMIC_ARRAYS
4670 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4671 and flagged as errors by Address Sanitizer. */
4672 __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
4673 ? dpi.num_saved_scopes : 1];
4674 __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
4675 ? dpi.num_copy_templates : 1];
4677 dpi.saved_scopes = scopes;
4678 dpi.copy_templates = temps;
4679 #else
4680 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4681 * sizeof (*dpi.saved_scopes));
4682 dpi.copy_templates = alloca (dpi.num_copy_templates
4683 * sizeof (*dpi.copy_templates));
4684 #endif
4685 #else
4686 /* Allocate memory dynamically to avoid VLAs as valgrind stack
4687 is a scarce resource */
4688 dpi.saved_scopes = xmalloc(dpi.num_saved_scopes
4689 * sizeof (*dpi.saved_scopes));
4690 dpi.copy_templates = xmalloc (dpi.num_copy_templates
4691 * sizeof (*dpi.copy_templates));
4692 #endif /* ! in valgrind */
4693 d_print_comp (&dpi, options, dc);
4696 d_print_flush (&dpi);
4698 int status = ! d_print_saw_error (&dpi);
4700 #if 0 /* in valgrind */
4701 #else
4702 free (dpi.saved_scopes);
4703 free (dpi.copy_templates);
4704 #endif /* in valgrind */
4706 return status;
4709 /* Turn components into a human readable string. OPTIONS is the
4710 options bits passed to the demangler. DC is the tree to print.
4711 ESTIMATE is a guess at the length of the result. This returns a
4712 string allocated by malloc, or NULL on error. On success, this
4713 sets *PALC to the size of the allocated buffer. On failure, this
4714 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4715 failure. */
4717 CP_STATIC_IF_GLIBCPP_V3
4718 char *
4719 cplus_demangle_print (int options, struct demangle_component *dc,
4720 int estimate, size_t *palc)
4722 struct d_growable_string dgs;
4724 d_growable_string_init (&dgs, estimate);
4726 if (! cplus_demangle_print_callback (options, dc,
4727 d_growable_string_callback_adapter,
4728 &dgs))
4730 free (dgs.buf);
4731 *palc = 0;
4732 return NULL;
4735 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4736 return dgs.buf;
4739 /* Returns the I'th element of the template arglist ARGS, or NULL on
4740 failure. If I is negative, return the entire arglist. */
4742 static struct demangle_component *
4743 d_index_template_argument (struct demangle_component *args, int i)
4745 struct demangle_component *a;
4747 if (i < 0)
4748 /* Print the whole argument pack. */
4749 return args;
4751 for (a = args;
4752 a != NULL;
4753 a = d_right (a))
4755 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4756 return NULL;
4757 if (i <= 0)
4758 break;
4759 --i;
4761 if (i != 0 || a == NULL)
4762 return NULL;
4764 return d_left (a);
4767 /* Returns the template argument from the current context indicated by DC,
4768 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4770 static struct demangle_component *
4771 d_lookup_template_argument (struct d_print_info *dpi,
4772 const struct demangle_component *dc)
4774 if (dpi->templates == NULL)
4776 d_print_error (dpi);
4777 return NULL;
4780 return d_index_template_argument
4781 (d_right (dpi->templates->template_decl),
4782 dc->u.s_number.number);
4785 /* Returns a template argument pack used in DC (any will do), or NULL. */
4787 static struct demangle_component *
4788 d_find_pack (struct d_print_info *dpi,
4789 const struct demangle_component *dc)
4791 struct demangle_component *a;
4792 if (dc == NULL)
4793 return NULL;
4795 switch (dc->type)
4797 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4798 a = d_lookup_template_argument (dpi, dc);
4799 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4800 return a;
4801 return NULL;
4803 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4804 return NULL;
4806 case DEMANGLE_COMPONENT_LAMBDA:
4807 case DEMANGLE_COMPONENT_NAME:
4808 case DEMANGLE_COMPONENT_TAGGED_NAME:
4809 case DEMANGLE_COMPONENT_OPERATOR:
4810 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4811 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4812 case DEMANGLE_COMPONENT_SUB_STD:
4813 case DEMANGLE_COMPONENT_CHARACTER:
4814 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4815 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4816 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4817 case DEMANGLE_COMPONENT_NUMBER:
4818 return NULL;
4820 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4821 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4822 case DEMANGLE_COMPONENT_CTOR:
4823 return d_find_pack (dpi, dc->u.s_ctor.name);
4824 case DEMANGLE_COMPONENT_DTOR:
4825 return d_find_pack (dpi, dc->u.s_dtor.name);
4827 default:
4828 a = d_find_pack (dpi, d_left (dc));
4829 if (a)
4830 return a;
4831 return d_find_pack (dpi, d_right (dc));
4835 /* Returns the length of the template argument pack DC. */
4837 static int
4838 d_pack_length (const struct demangle_component *dc)
4840 int count = 0;
4841 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4842 && d_left (dc) != NULL)
4844 ++count;
4845 dc = d_right (dc);
4847 return count;
4850 /* Returns the number of template args in DC, expanding any pack expansions
4851 found there. */
4853 static int
4854 d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
4856 int count = 0;
4857 for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4858 dc = d_right (dc))
4860 struct demangle_component *elt = d_left (dc);
4861 if (elt == NULL)
4862 break;
4863 if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
4865 struct demangle_component *a = d_find_pack (dpi, d_left (elt));
4866 count += d_pack_length (a);
4868 else
4869 ++count;
4871 return count;
4874 /* DC is a component of a mangled expression. Print it, wrapped in parens
4875 if needed. */
4877 static void
4878 d_print_subexpr (struct d_print_info *dpi, int options,
4879 struct demangle_component *dc)
4881 int simple = 0;
4882 if (dc->type == DEMANGLE_COMPONENT_NAME
4883 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4884 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4885 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4886 simple = 1;
4887 if (!simple)
4888 d_append_char (dpi, '(');
4889 d_print_comp (dpi, options, dc);
4890 if (!simple)
4891 d_append_char (dpi, ')');
4894 /* Save the current scope. */
4896 static void
4897 d_save_scope (struct d_print_info *dpi,
4898 const struct demangle_component *container)
4900 struct d_saved_scope *scope;
4901 struct d_print_template *src, **link;
4903 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4905 d_print_error (dpi);
4906 return;
4908 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4909 dpi->next_saved_scope++;
4911 scope->container = container;
4912 link = &scope->templates;
4914 for (src = dpi->templates; src != NULL; src = src->next)
4916 struct d_print_template *dst;
4918 if (dpi->next_copy_template >= dpi->num_copy_templates)
4920 d_print_error (dpi);
4921 return;
4923 dst = &dpi->copy_templates[dpi->next_copy_template];
4924 dpi->next_copy_template++;
4926 dst->template_decl = src->template_decl;
4927 *link = dst;
4928 link = &dst->next;
4931 *link = NULL;
4934 /* Attempt to locate a previously saved scope. Returns NULL if no
4935 corresponding saved scope was found. */
4937 static struct d_saved_scope *
4938 d_get_saved_scope (struct d_print_info *dpi,
4939 const struct demangle_component *container)
4941 int i;
4943 for (i = 0; i < dpi->next_saved_scope; i++)
4944 if (dpi->saved_scopes[i].container == container)
4945 return &dpi->saved_scopes[i];
4947 return NULL;
4950 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4951 return false. */
4953 static int
4954 d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
4955 struct demangle_component *dc)
4957 struct demangle_component *ops, *operator_, *op1, *op2;
4958 int save_idx;
4960 const char *fold_code = d_left (dc)->u.s_operator.op->code;
4961 if (fold_code[0] != 'f')
4962 return 0;
4964 ops = d_right (dc);
4965 operator_ = d_left (ops);
4966 op1 = d_right (ops);
4967 op2 = 0;
4968 if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
4970 op2 = d_right (op1);
4971 op1 = d_left (op1);
4974 /* Print the whole pack. */
4975 save_idx = dpi->pack_index;
4976 dpi->pack_index = -1;
4978 switch (fold_code[1])
4980 /* Unary left fold, (... + X). */
4981 case 'l':
4982 d_append_string (dpi, "(...");
4983 d_print_expr_op (dpi, options, operator_);
4984 d_print_subexpr (dpi, options, op1);
4985 d_append_char (dpi, ')');
4986 break;
4988 /* Unary right fold, (X + ...). */
4989 case 'r':
4990 d_append_char (dpi, '(');
4991 d_print_subexpr (dpi, options, op1);
4992 d_print_expr_op (dpi, options, operator_);
4993 d_append_string (dpi, "...)");
4994 break;
4996 /* Binary left fold, (42 + ... + X). */
4997 case 'L':
4998 /* Binary right fold, (X + ... + 42). */
4999 case 'R':
5000 d_append_char (dpi, '(');
5001 d_print_subexpr (dpi, options, op1);
5002 d_print_expr_op (dpi, options, operator_);
5003 d_append_string (dpi, "...");
5004 d_print_expr_op (dpi, options, operator_);
5005 d_print_subexpr (dpi, options, op2);
5006 d_append_char (dpi, ')');
5007 break;
5010 dpi->pack_index = save_idx;
5011 return 1;
5014 /* True iff DC represents a C99-style designated initializer. */
5016 static int
5017 is_designated_init (struct demangle_component *dc)
5019 if (dc->type != DEMANGLE_COMPONENT_BINARY
5020 && dc->type != DEMANGLE_COMPONENT_TRINARY)
5021 return 0;
5023 struct demangle_component *op = d_left (dc);
5024 const char *code = op->u.s_operator.op->code;
5025 return (code[0] == 'd'
5026 && (code[1] == 'i' || code[1] == 'x' || code[1] == 'X'));
5029 /* If DC represents a C99-style designated initializer, print it and return
5030 true; otherwise, return false. */
5032 static int
5033 d_maybe_print_designated_init (struct d_print_info *dpi, int options,
5034 struct demangle_component *dc)
5036 if (!is_designated_init (dc))
5037 return 0;
5039 const char *code = d_left (dc)->u.s_operator.op->code;
5041 struct demangle_component *operands = d_right (dc);
5042 struct demangle_component *op1 = d_left (operands);
5043 struct demangle_component *op2 = d_right (operands);
5045 if (code[1] == 'i')
5046 d_append_char (dpi, '.');
5047 else
5048 d_append_char (dpi, '[');
5050 d_print_comp (dpi, options, op1);
5051 if (code[1] == 'X')
5053 d_append_string (dpi, " ... ");
5054 d_print_comp (dpi, options, d_left (op2));
5055 op2 = d_right (op2);
5057 if (code[1] != 'i')
5058 d_append_char (dpi, ']');
5059 if (is_designated_init (op2))
5061 /* Don't put '=' or '(' between chained designators. */
5062 d_print_comp (dpi, options, op2);
5064 else
5066 d_append_char (dpi, '=');
5067 d_print_subexpr (dpi, options, op2);
5069 return 1;
5072 static void
5073 d_print_lambda_parm_name (struct d_print_info *dpi, int type, unsigned index)
5075 const char *str;
5076 switch (type)
5078 default:
5079 dpi->demangle_failure = 1;
5080 str = "";
5081 break;
5083 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
5084 str = "$T";
5085 break;
5087 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
5088 str = "$N";
5089 break;
5091 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
5092 str = "$TT";
5093 break;
5095 d_append_string (dpi, str);
5096 d_append_num (dpi, index);
5099 /* Subroutine to handle components. */
5101 static void
5102 d_print_comp_inner (struct d_print_info *dpi, int options,
5103 struct demangle_component *dc)
5105 /* Magic variable to let reference smashing skip over the next modifier
5106 without needing to modify *dc. */
5107 struct demangle_component *mod_inner = NULL;
5109 /* Variable used to store the current templates while a previously
5110 captured scope is used. */
5111 struct d_print_template *saved_templates = NULL; /* silence GCC */
5113 /* Nonzero if templates have been stored in the above variable. */
5114 int need_template_restore = 0;
5116 if (dc == NULL)
5118 d_print_error (dpi);
5119 return;
5121 if (d_print_saw_error (dpi))
5122 return;
5124 switch (dc->type)
5126 case DEMANGLE_COMPONENT_NAME:
5127 if ((options & DMGL_JAVA) == 0)
5128 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
5129 else
5130 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
5131 return;
5133 case DEMANGLE_COMPONENT_TAGGED_NAME:
5134 d_print_comp (dpi, options, d_left (dc));
5135 d_append_string (dpi, "[abi:");
5136 d_print_comp (dpi, options, d_right (dc));
5137 d_append_char (dpi, ']');
5138 return;
5140 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
5141 d_append_char (dpi, '[');
5142 for (;;)
5144 d_print_comp (dpi, options, d_left (dc));
5145 dc = d_right (dc);
5146 if (!dc)
5147 break;
5148 d_append_string (dpi, ", ");
5150 d_append_char (dpi, ']');
5151 return;
5153 case DEMANGLE_COMPONENT_MODULE_ENTITY:
5154 d_print_comp (dpi, options, d_left (dc));
5155 d_append_char (dpi, '@');
5156 d_print_comp (dpi, options, d_right (dc));
5157 return;
5159 case DEMANGLE_COMPONENT_MODULE_NAME:
5160 case DEMANGLE_COMPONENT_MODULE_PARTITION:
5162 if (d_left (dc))
5163 d_print_comp (dpi, options, d_left (dc));
5164 char c = dc->type == DEMANGLE_COMPONENT_MODULE_PARTITION
5165 ? ':' : d_left (dc) ? '.' : 0;
5166 if (c)
5167 d_append_char (dpi, c);
5168 d_print_comp (dpi, options, d_right (dc));
5170 return;
5172 case DEMANGLE_COMPONENT_QUAL_NAME:
5173 case DEMANGLE_COMPONENT_LOCAL_NAME:
5174 d_print_comp (dpi, options, d_left (dc));
5175 if ((options & DMGL_JAVA) == 0)
5176 d_append_string (dpi, "::");
5177 else
5178 d_append_char (dpi, '.');
5180 struct demangle_component *local_name = d_right (dc);
5181 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5183 d_append_string (dpi, "{default arg#");
5184 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
5185 d_append_string (dpi, "}::");
5186 local_name = local_name->u.s_unary_num.sub;
5188 d_print_comp (dpi, options, local_name);
5190 return;
5192 case DEMANGLE_COMPONENT_TYPED_NAME:
5194 struct d_print_mod *hold_modifiers;
5195 struct demangle_component *typed_name;
5196 struct d_print_mod adpm[4];
5197 unsigned int i;
5198 struct d_print_template dpt;
5200 /* Pass the name down to the type so that it can be printed in
5201 the right place for the type. We also have to pass down
5202 any CV-qualifiers, which apply to the this parameter. */
5203 hold_modifiers = dpi->modifiers;
5204 dpi->modifiers = 0;
5205 i = 0;
5206 typed_name = d_left (dc);
5207 while (typed_name != NULL)
5209 if (i >= sizeof adpm / sizeof adpm[0])
5211 d_print_error (dpi);
5212 return;
5215 adpm[i].next = dpi->modifiers;
5216 dpi->modifiers = &adpm[i];
5217 adpm[i].mod = typed_name;
5218 adpm[i].printed = 0;
5219 adpm[i].templates = dpi->templates;
5220 ++i;
5222 if (!is_fnqual_component_type (typed_name->type))
5223 break;
5225 typed_name = d_left (typed_name);
5228 if (typed_name == NULL)
5230 d_print_error (dpi);
5231 return;
5234 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
5235 there may be CV-qualifiers on its right argument which
5236 really apply here; this happens when parsing a class that
5237 is local to a function. */
5238 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5240 typed_name = d_right (typed_name);
5241 if (typed_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5242 typed_name = typed_name->u.s_unary_num.sub;
5243 while (typed_name != NULL
5244 && is_fnqual_component_type (typed_name->type))
5246 if (i >= sizeof adpm / sizeof adpm[0])
5248 d_print_error (dpi);
5249 return;
5252 adpm[i] = adpm[i - 1];
5253 adpm[i].next = &adpm[i - 1];
5254 dpi->modifiers = &adpm[i];
5256 adpm[i - 1].mod = typed_name;
5257 adpm[i - 1].printed = 0;
5258 adpm[i - 1].templates = dpi->templates;
5259 ++i;
5261 typed_name = d_left (typed_name);
5263 if (typed_name == NULL)
5265 d_print_error (dpi);
5266 return;
5270 /* If typed_name is a template, then it applies to the
5271 function type as well. */
5272 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5274 dpt.next = dpi->templates;
5275 dpi->templates = &dpt;
5276 dpt.template_decl = typed_name;
5278 /* Constraints are mangled as part of the template argument list,
5279 so they wrap the _TEMPLATE_ARGLIST. But
5280 d_lookup_template_argument expects the RHS of _TEMPLATE to be
5281 the _ARGLIST, and constraints need to refer to these args. So
5282 move the _CONSTRAINTS out of the _TEMPLATE and onto the type.
5283 This will result in them being printed after the () like a
5284 trailing requires-clause, but that seems like our best option
5285 given that we aren't printing a template-head. */
5286 struct demangle_component *tnr = d_right (typed_name);
5287 if (tnr->type == DEMANGLE_COMPONENT_CONSTRAINTS)
5289 d_right (typed_name) = d_left (tnr);
5290 d_left (tnr) = d_right (dc);
5291 d_right (dc) = tnr;
5295 d_print_comp (dpi, options, d_right (dc));
5297 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5298 dpi->templates = dpt.next;
5300 /* If the modifiers didn't get printed by the type, print them
5301 now. */
5302 while (i > 0)
5304 --i;
5305 if (! adpm[i].printed)
5307 d_append_char (dpi, ' ');
5308 d_print_mod (dpi, options, adpm[i].mod);
5312 dpi->modifiers = hold_modifiers;
5314 return;
5317 case DEMANGLE_COMPONENT_TEMPLATE:
5319 struct d_print_mod *hold_dpm;
5320 struct demangle_component *dcl;
5321 const struct demangle_component *hold_current;
5323 /* This template may need to be referenced by a cast operator
5324 contained in its subtree. */
5325 hold_current = dpi->current_template;
5326 dpi->current_template = dc;
5328 /* Don't push modifiers into a template definition. Doing so
5329 could give the wrong definition for a template argument.
5330 Instead, treat the template essentially as a name. */
5332 hold_dpm = dpi->modifiers;
5333 dpi->modifiers = NULL;
5335 dcl = d_left (dc);
5337 if ((options & DMGL_JAVA) != 0
5338 && dcl->type == DEMANGLE_COMPONENT_NAME
5339 && dcl->u.s_name.len == 6
5340 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
5342 /* Special-case Java arrays, so that JArray<TYPE> appears
5343 instead as TYPE[]. */
5345 d_print_comp (dpi, options, d_right (dc));
5346 d_append_string (dpi, "[]");
5348 else
5350 d_print_comp (dpi, options, dcl);
5351 if (d_last_char (dpi) == '<')
5352 d_append_char (dpi, ' ');
5353 d_append_char (dpi, '<');
5354 d_print_comp (dpi, options, d_right (dc));
5355 /* Avoid generating two consecutive '>' characters, to avoid
5356 the C++ syntactic ambiguity. */
5357 if (d_last_char (dpi) == '>')
5358 d_append_char (dpi, ' ');
5359 d_append_char (dpi, '>');
5362 dpi->modifiers = hold_dpm;
5363 dpi->current_template = hold_current;
5365 return;
5368 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
5369 if (dpi->lambda_tpl_parms > dc->u.s_number.number + 1)
5371 const struct demangle_component *a
5372 = d_left (dpi->templates->template_decl);
5373 unsigned c;
5374 for (c = dc->u.s_number.number; a && c; c--)
5375 a = d_right (a);
5376 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
5377 a = d_left (a);
5378 if (!a)
5379 dpi->demangle_failure = 1;
5380 else
5381 d_print_lambda_parm_name (dpi, a->type, dc->u.s_number.number);
5383 else if (dpi->lambda_tpl_parms)
5385 /* Show the template parm index, as that's how g++ displays
5386 these, and future proofs us against potential
5387 '[]<typename T> (T *a, T *b) {...}'. */
5388 d_append_buffer (dpi, "auto:", 5);
5389 d_append_num (dpi, dc->u.s_number.number + 1);
5391 else
5393 struct d_print_template *hold_dpt;
5394 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
5396 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5397 a = d_index_template_argument (a, dpi->pack_index);
5399 if (a == NULL)
5401 d_print_error (dpi);
5402 return;
5405 /* While processing this parameter, we need to pop the list
5406 of templates. This is because the template parameter may
5407 itself be a reference to a parameter of an outer
5408 template. */
5410 hold_dpt = dpi->templates;
5411 dpi->templates = hold_dpt->next;
5413 d_print_comp (dpi, options, a);
5415 dpi->templates = hold_dpt;
5417 return;
5419 case DEMANGLE_COMPONENT_TPARM_OBJ:
5420 d_append_string (dpi, "template parameter object for ");
5421 d_print_comp (dpi, options, d_left (dc));
5422 return;
5424 case DEMANGLE_COMPONENT_CTOR:
5425 d_print_comp (dpi, options, dc->u.s_ctor.name);
5426 return;
5428 case DEMANGLE_COMPONENT_DTOR:
5429 d_append_char (dpi, '~');
5430 d_print_comp (dpi, options, dc->u.s_dtor.name);
5431 return;
5433 case DEMANGLE_COMPONENT_MODULE_INIT:
5434 d_append_string (dpi, "initializer for module ");
5435 d_print_comp (dpi, options, d_left (dc));
5436 return;
5438 case DEMANGLE_COMPONENT_VTABLE:
5439 d_append_string (dpi, "vtable for ");
5440 d_print_comp (dpi, options, d_left (dc));
5441 return;
5443 case DEMANGLE_COMPONENT_VTT:
5444 d_append_string (dpi, "VTT for ");
5445 d_print_comp (dpi, options, d_left (dc));
5446 return;
5448 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
5449 d_append_string (dpi, "construction vtable for ");
5450 d_print_comp (dpi, options, d_left (dc));
5451 d_append_string (dpi, "-in-");
5452 d_print_comp (dpi, options, d_right (dc));
5453 return;
5455 case DEMANGLE_COMPONENT_TYPEINFO:
5456 d_append_string (dpi, "typeinfo for ");
5457 d_print_comp (dpi, options, d_left (dc));
5458 return;
5460 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
5461 d_append_string (dpi, "typeinfo name for ");
5462 d_print_comp (dpi, options, d_left (dc));
5463 return;
5465 case DEMANGLE_COMPONENT_TYPEINFO_FN:
5466 d_append_string (dpi, "typeinfo fn for ");
5467 d_print_comp (dpi, options, d_left (dc));
5468 return;
5470 case DEMANGLE_COMPONENT_THUNK:
5471 d_append_string (dpi, "non-virtual thunk to ");
5472 d_print_comp (dpi, options, d_left (dc));
5473 return;
5475 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
5476 d_append_string (dpi, "virtual thunk to ");
5477 d_print_comp (dpi, options, d_left (dc));
5478 return;
5480 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
5481 d_append_string (dpi, "covariant return thunk to ");
5482 d_print_comp (dpi, options, d_left (dc));
5483 return;
5485 case DEMANGLE_COMPONENT_JAVA_CLASS:
5486 d_append_string (dpi, "java Class for ");
5487 d_print_comp (dpi, options, d_left (dc));
5488 return;
5490 case DEMANGLE_COMPONENT_GUARD:
5491 d_append_string (dpi, "guard variable for ");
5492 d_print_comp (dpi, options, d_left (dc));
5493 return;
5495 case DEMANGLE_COMPONENT_TLS_INIT:
5496 d_append_string (dpi, "TLS init function for ");
5497 d_print_comp (dpi, options, d_left (dc));
5498 return;
5500 case DEMANGLE_COMPONENT_TLS_WRAPPER:
5501 d_append_string (dpi, "TLS wrapper function for ");
5502 d_print_comp (dpi, options, d_left (dc));
5503 return;
5505 case DEMANGLE_COMPONENT_REFTEMP:
5506 d_append_string (dpi, "reference temporary #");
5507 d_print_comp (dpi, options, d_right (dc));
5508 d_append_string (dpi, " for ");
5509 d_print_comp (dpi, options, d_left (dc));
5510 return;
5512 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
5513 d_append_string (dpi, "hidden alias for ");
5514 d_print_comp (dpi, options, d_left (dc));
5515 return;
5517 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
5518 d_append_string (dpi, "transaction clone for ");
5519 d_print_comp (dpi, options, d_left (dc));
5520 return;
5522 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
5523 d_append_string (dpi, "non-transaction clone for ");
5524 d_print_comp (dpi, options, d_left (dc));
5525 return;
5527 case DEMANGLE_COMPONENT_SUB_STD:
5528 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
5529 return;
5531 case DEMANGLE_COMPONENT_RESTRICT:
5532 case DEMANGLE_COMPONENT_VOLATILE:
5533 case DEMANGLE_COMPONENT_CONST:
5535 struct d_print_mod *pdpm;
5537 /* When printing arrays, it's possible to have cases where the
5538 same CV-qualifier gets pushed on the stack multiple times.
5539 We only need to print it once. */
5541 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
5543 if (! pdpm->printed)
5545 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
5546 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
5547 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
5548 break;
5549 if (pdpm->mod->type == dc->type)
5551 d_print_comp (dpi, options, d_left (dc));
5552 return;
5557 goto modifier;
5559 case DEMANGLE_COMPONENT_REFERENCE:
5560 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5562 /* Handle reference smashing: & + && = &. */
5563 struct demangle_component *sub = d_left (dc);
5564 if (!dpi->lambda_tpl_parms
5565 && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
5567 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
5568 struct demangle_component *a;
5570 if (scope == NULL)
5572 /* This is the first time SUB has been traversed.
5573 We need to capture the current templates so
5574 they can be restored if SUB is reentered as a
5575 substitution. */
5576 d_save_scope (dpi, sub);
5577 if (d_print_saw_error (dpi))
5578 return;
5580 else
5582 const struct d_component_stack *dcse;
5583 int found_self_or_parent = 0;
5585 /* This traversal is reentering SUB as a substition.
5586 If we are not beneath SUB or DC in the tree then we
5587 need to restore SUB's template stack temporarily. */
5588 for (dcse = dpi->component_stack; dcse != NULL;
5589 dcse = dcse->parent)
5591 if (dcse->dc == sub
5592 || (dcse->dc == dc
5593 && dcse != dpi->component_stack))
5595 found_self_or_parent = 1;
5596 break;
5600 if (!found_self_or_parent)
5602 saved_templates = dpi->templates;
5603 dpi->templates = scope->templates;
5604 need_template_restore = 1;
5608 a = d_lookup_template_argument (dpi, sub);
5609 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5610 a = d_index_template_argument (a, dpi->pack_index);
5612 if (a == NULL)
5614 if (need_template_restore)
5615 dpi->templates = saved_templates;
5617 d_print_error (dpi);
5618 return;
5621 sub = a;
5624 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
5625 || sub->type == dc->type)
5626 dc = sub;
5627 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
5628 mod_inner = d_left (sub);
5630 /* Fall through. */
5632 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5633 case DEMANGLE_COMPONENT_POINTER:
5634 case DEMANGLE_COMPONENT_COMPLEX:
5635 case DEMANGLE_COMPONENT_IMAGINARY:
5636 FNQUAL_COMPONENT_CASE:
5637 modifier:
5639 /* We keep a list of modifiers on the stack. */
5640 struct d_print_mod dpm;
5642 dpm.next = dpi->modifiers;
5643 dpi->modifiers = &dpm;
5644 dpm.mod = dc;
5645 dpm.printed = 0;
5646 dpm.templates = dpi->templates;
5648 if (!mod_inner)
5649 mod_inner = d_left (dc);
5651 d_print_comp (dpi, options, mod_inner);
5653 /* If the modifier didn't get printed by the type, print it
5654 now. */
5655 if (! dpm.printed)
5656 d_print_mod (dpi, options, dc);
5658 dpi->modifiers = dpm.next;
5660 if (need_template_restore)
5661 dpi->templates = saved_templates;
5663 return;
5666 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5667 if ((options & DMGL_JAVA) == 0)
5668 d_append_buffer (dpi, dc->u.s_builtin.type->name,
5669 dc->u.s_builtin.type->len);
5670 else
5671 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
5672 dc->u.s_builtin.type->java_len);
5673 return;
5675 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
5676 d_append_buffer (dpi, dc->u.s_extended_builtin.type->name,
5677 dc->u.s_extended_builtin.type->len);
5678 d_append_num (dpi, dc->u.s_extended_builtin.arg);
5679 if (dc->u.s_extended_builtin.suffix)
5680 d_append_buffer (dpi, &dc->u.s_extended_builtin.suffix, 1);
5681 return;
5683 case DEMANGLE_COMPONENT_VENDOR_TYPE:
5684 d_print_comp (dpi, options, d_left (dc));
5685 return;
5687 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5689 if ((options & DMGL_RET_POSTFIX) != 0)
5690 d_print_function_type (dpi,
5691 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5692 dc, dpi->modifiers);
5694 /* Print return type if present */
5695 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
5696 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5697 d_left (dc));
5698 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
5700 struct d_print_mod dpm;
5702 /* We must pass this type down as a modifier in order to
5703 print it in the right location. */
5704 dpm.next = dpi->modifiers;
5705 dpi->modifiers = &dpm;
5706 dpm.mod = dc;
5707 dpm.printed = 0;
5708 dpm.templates = dpi->templates;
5710 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5711 d_left (dc));
5713 dpi->modifiers = dpm.next;
5715 if (dpm.printed)
5716 return;
5718 /* In standard prefix notation, there is a space between the
5719 return type and the function signature. */
5720 if ((options & DMGL_RET_POSTFIX) == 0)
5721 d_append_char (dpi, ' ');
5724 if ((options & DMGL_RET_POSTFIX) == 0)
5725 d_print_function_type (dpi,
5726 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5727 dc, dpi->modifiers);
5729 return;
5732 case DEMANGLE_COMPONENT_ARRAY_TYPE:
5734 struct d_print_mod *hold_modifiers;
5735 struct d_print_mod adpm[4];
5736 unsigned int i;
5737 struct d_print_mod *pdpm;
5739 /* We must pass this type down as a modifier in order to print
5740 multi-dimensional arrays correctly. If the array itself is
5741 CV-qualified, we act as though the element type were
5742 CV-qualified. We do this by copying the modifiers down
5743 rather than fiddling pointers, so that we don't wind up
5744 with a d_print_mod higher on the stack pointing into our
5745 stack frame after we return. */
5747 hold_modifiers = dpi->modifiers;
5749 adpm[0].next = hold_modifiers;
5750 dpi->modifiers = &adpm[0];
5751 adpm[0].mod = dc;
5752 adpm[0].printed = 0;
5753 adpm[0].templates = dpi->templates;
5755 i = 1;
5756 pdpm = hold_modifiers;
5757 while (pdpm != NULL
5758 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
5759 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
5760 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
5762 if (! pdpm->printed)
5764 if (i >= sizeof adpm / sizeof adpm[0])
5766 d_print_error (dpi);
5767 return;
5770 adpm[i] = *pdpm;
5771 adpm[i].next = dpi->modifiers;
5772 dpi->modifiers = &adpm[i];
5773 pdpm->printed = 1;
5774 ++i;
5777 pdpm = pdpm->next;
5780 d_print_comp (dpi, options, d_right (dc));
5782 dpi->modifiers = hold_modifiers;
5784 if (adpm[0].printed)
5785 return;
5787 while (i > 1)
5789 --i;
5790 d_print_mod (dpi, options, adpm[i].mod);
5793 d_print_array_type (dpi, options, dc, dpi->modifiers);
5795 return;
5798 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5799 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5801 struct d_print_mod dpm;
5803 dpm.next = dpi->modifiers;
5804 dpi->modifiers = &dpm;
5805 dpm.mod = dc;
5806 dpm.printed = 0;
5807 dpm.templates = dpi->templates;
5809 d_print_comp (dpi, options, d_right (dc));
5811 /* If the modifier didn't get printed by the type, print it
5812 now. */
5813 if (! dpm.printed)
5814 d_print_mod (dpi, options, dc);
5816 dpi->modifiers = dpm.next;
5818 return;
5821 case DEMANGLE_COMPONENT_ARGLIST:
5822 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5823 if (d_left (dc) != NULL)
5824 d_print_comp (dpi, options, d_left (dc));
5825 if (d_right (dc) != NULL)
5827 size_t len;
5828 unsigned long int flush_count;
5829 /* Make sure ", " isn't flushed by d_append_string, otherwise
5830 dpi->len -= 2 wouldn't work. */
5831 if (dpi->len >= sizeof (dpi->buf) - 2)
5832 d_print_flush (dpi);
5833 d_append_string (dpi, ", ");
5834 len = dpi->len;
5835 flush_count = dpi->flush_count;
5836 d_print_comp (dpi, options, d_right (dc));
5837 /* If that didn't print anything (which can happen with empty
5838 template argument packs), remove the comma and space. */
5839 if (dpi->flush_count == flush_count && dpi->len == len)
5840 dpi->len -= 2;
5842 return;
5844 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5846 struct demangle_component *type = d_left (dc);
5847 struct demangle_component *list = d_right (dc);
5849 if (type)
5850 d_print_comp (dpi, options, type);
5851 d_append_char (dpi, '{');
5852 d_print_comp (dpi, options, list);
5853 d_append_char (dpi, '}');
5855 return;
5857 case DEMANGLE_COMPONENT_OPERATOR:
5859 const struct demangle_operator_info *op = dc->u.s_operator.op;
5860 int len = op->len;
5862 d_append_string (dpi, "operator");
5863 /* Add a space before new/delete. */
5864 if (IS_LOWER (op->name[0]))
5865 d_append_char (dpi, ' ');
5866 /* Omit a trailing space. */
5867 if (op->name[len-1] == ' ')
5868 --len;
5869 d_append_buffer (dpi, op->name, len);
5870 return;
5873 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5874 d_append_string (dpi, "operator ");
5875 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5876 return;
5878 case DEMANGLE_COMPONENT_CONVERSION:
5879 d_append_string (dpi, "operator ");
5880 d_print_conversion (dpi, options, dc);
5881 return;
5883 case DEMANGLE_COMPONENT_NULLARY:
5884 d_print_expr_op (dpi, options, d_left (dc));
5885 return;
5887 case DEMANGLE_COMPONENT_UNARY:
5889 struct demangle_component *op = d_left (dc);
5890 struct demangle_component *operand = d_right (dc);
5891 const char *code = NULL;
5893 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5895 code = op->u.s_operator.op->code;
5896 if (!strcmp (code, "ad"))
5898 /* Don't print the argument list for the address of a
5899 function. */
5900 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5901 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5902 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5903 operand = d_left (operand);
5905 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5907 /* This indicates a suffix operator. */
5908 operand = d_left (operand);
5909 d_print_subexpr (dpi, options, operand);
5910 d_print_expr_op (dpi, options, op);
5911 return;
5915 /* For sizeof..., just print the pack length. */
5916 if (code && !strcmp (code, "sZ"))
5918 struct demangle_component *a = d_find_pack (dpi, operand);
5919 int len = d_pack_length (a);
5920 d_append_num (dpi, len);
5921 return;
5923 else if (code && !strcmp (code, "sP"))
5925 int len = d_args_length (dpi, operand);
5926 d_append_num (dpi, len);
5927 return;
5930 if (op->type != DEMANGLE_COMPONENT_CAST)
5931 d_print_expr_op (dpi, options, op);
5932 else
5934 d_append_char (dpi, '(');
5935 d_print_cast (dpi, options, op);
5936 d_append_char (dpi, ')');
5938 if (code && !strcmp (code, "gs"))
5939 /* Avoid parens after '::'. */
5940 d_print_comp (dpi, options, operand);
5941 else if (code && (!strcmp (code, "st") || !strcmp (code, "nx")))
5942 /* Always print parens for sizeof (type) and noexcept(expr). */
5944 d_append_char (dpi, '(');
5945 d_print_comp (dpi, options, operand);
5946 d_append_char (dpi, ')');
5948 else
5949 d_print_subexpr (dpi, options, operand);
5951 return;
5953 case DEMANGLE_COMPONENT_BINARY:
5954 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5956 d_print_error (dpi);
5957 return;
5960 if (op_is_new_cast (d_left (dc)))
5962 d_print_expr_op (dpi, options, d_left (dc));
5963 d_append_char (dpi, '<');
5964 d_print_comp (dpi, options, d_left (d_right (dc)));
5965 d_append_string (dpi, ">(");
5966 d_print_comp (dpi, options, d_right (d_right (dc)));
5967 d_append_char (dpi, ')');
5968 return;
5971 if (d_maybe_print_fold_expression (dpi, options, dc))
5972 return;
5974 if (d_maybe_print_designated_init (dpi, options, dc))
5975 return;
5977 /* We wrap an expression which uses the greater-than operator in
5978 an extra layer of parens so that it does not get confused
5979 with the '>' which ends the template parameters. */
5980 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5981 && d_left (dc)->u.s_operator.op->len == 1
5982 && d_left (dc)->u.s_operator.op->name[0] == '>')
5983 d_append_char (dpi, '(');
5985 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5986 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5988 /* Function call used in an expression should not have printed types
5989 of the function arguments. Values of the function arguments still
5990 get printed below. */
5992 const struct demangle_component *func = d_left (d_right (dc));
5994 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5995 d_print_error (dpi);
5996 d_print_subexpr (dpi, options, d_left (func));
5998 else
5999 d_print_subexpr (dpi, options, d_left (d_right (dc)));
6000 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
6002 d_append_char (dpi, '[');
6003 d_print_comp (dpi, options, d_right (d_right (dc)));
6004 d_append_char (dpi, ']');
6006 else
6008 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
6009 d_print_expr_op (dpi, options, d_left (dc));
6010 d_print_subexpr (dpi, options, d_right (d_right (dc)));
6013 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
6014 && d_left (dc)->u.s_operator.op->len == 1
6015 && d_left (dc)->u.s_operator.op->name[0] == '>')
6016 d_append_char (dpi, ')');
6018 return;
6020 case DEMANGLE_COMPONENT_BINARY_ARGS:
6021 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
6022 d_print_error (dpi);
6023 return;
6025 case DEMANGLE_COMPONENT_TRINARY:
6026 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
6027 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
6029 d_print_error (dpi);
6030 return;
6032 if (d_maybe_print_fold_expression (dpi, options, dc))
6033 return;
6034 if (d_maybe_print_designated_init (dpi, options, dc))
6035 return;
6037 struct demangle_component *op = d_left (dc);
6038 struct demangle_component *first = d_left (d_right (dc));
6039 struct demangle_component *second = d_left (d_right (d_right (dc)));
6040 struct demangle_component *third = d_right (d_right (d_right (dc)));
6042 if (!strcmp (op->u.s_operator.op->code, "qu"))
6044 d_print_subexpr (dpi, options, first);
6045 d_print_expr_op (dpi, options, op);
6046 d_print_subexpr (dpi, options, second);
6047 d_append_string (dpi, " : ");
6048 d_print_subexpr (dpi, options, third);
6050 else
6052 d_append_string (dpi, "new ");
6053 if (d_left (first) != NULL)
6055 d_print_subexpr (dpi, options, first);
6056 d_append_char (dpi, ' ');
6058 d_print_comp (dpi, options, second);
6059 if (third)
6060 d_print_subexpr (dpi, options, third);
6063 return;
6065 case DEMANGLE_COMPONENT_TRINARY_ARG1:
6066 case DEMANGLE_COMPONENT_TRINARY_ARG2:
6067 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
6068 d_print_error (dpi);
6069 return;
6071 case DEMANGLE_COMPONENT_LITERAL:
6072 case DEMANGLE_COMPONENT_LITERAL_NEG:
6074 enum d_builtin_type_print tp;
6076 /* For some builtin types, produce simpler output. */
6077 tp = D_PRINT_DEFAULT;
6078 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
6080 tp = d_left (dc)->u.s_builtin.type->print;
6081 switch (tp)
6083 case D_PRINT_INT:
6084 case D_PRINT_UNSIGNED:
6085 case D_PRINT_LONG:
6086 case D_PRINT_UNSIGNED_LONG:
6087 case D_PRINT_LONG_LONG:
6088 case D_PRINT_UNSIGNED_LONG_LONG:
6089 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
6091 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6092 d_append_char (dpi, '-');
6093 d_print_comp (dpi, options, d_right (dc));
6094 switch (tp)
6096 default:
6097 break;
6098 case D_PRINT_UNSIGNED:
6099 d_append_char (dpi, 'u');
6100 break;
6101 case D_PRINT_LONG:
6102 d_append_char (dpi, 'l');
6103 break;
6104 case D_PRINT_UNSIGNED_LONG:
6105 d_append_string (dpi, "ul");
6106 break;
6107 case D_PRINT_LONG_LONG:
6108 d_append_string (dpi, "ll");
6109 break;
6110 case D_PRINT_UNSIGNED_LONG_LONG:
6111 d_append_string (dpi, "ull");
6112 break;
6114 return;
6116 break;
6118 case D_PRINT_BOOL:
6119 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
6120 && d_right (dc)->u.s_name.len == 1
6121 && dc->type == DEMANGLE_COMPONENT_LITERAL)
6123 switch (d_right (dc)->u.s_name.s[0])
6125 case '0':
6126 d_append_string (dpi, "false");
6127 return;
6128 case '1':
6129 d_append_string (dpi, "true");
6130 return;
6131 default:
6132 break;
6135 break;
6137 default:
6138 break;
6142 d_append_char (dpi, '(');
6143 d_print_comp (dpi, options, d_left (dc));
6144 d_append_char (dpi, ')');
6145 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6146 d_append_char (dpi, '-');
6147 if (tp == D_PRINT_FLOAT)
6148 d_append_char (dpi, '[');
6149 d_print_comp (dpi, options, d_right (dc));
6150 if (tp == D_PRINT_FLOAT)
6151 d_append_char (dpi, ']');
6153 return;
6155 case DEMANGLE_COMPONENT_VENDOR_EXPR:
6156 d_print_comp (dpi, options, d_left (dc));
6157 d_append_char (dpi, '(');
6158 d_print_comp (dpi, options, d_right (dc));
6159 d_append_char (dpi, ')');
6160 return;
6162 case DEMANGLE_COMPONENT_NUMBER:
6163 d_append_num (dpi, dc->u.s_number.number);
6164 return;
6166 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
6167 d_append_string (dpi, "java resource ");
6168 d_print_comp (dpi, options, d_left (dc));
6169 return;
6171 case DEMANGLE_COMPONENT_COMPOUND_NAME:
6172 d_print_comp (dpi, options, d_left (dc));
6173 d_print_comp (dpi, options, d_right (dc));
6174 return;
6176 case DEMANGLE_COMPONENT_CHARACTER:
6177 d_append_char (dpi, dc->u.s_character.character);
6178 return;
6180 case DEMANGLE_COMPONENT_DECLTYPE:
6181 d_append_string (dpi, "decltype (");
6182 d_print_comp (dpi, options, d_left (dc));
6183 d_append_char (dpi, ')');
6184 return;
6186 case DEMANGLE_COMPONENT_PACK_EXPANSION:
6188 struct demangle_component *a = NULL;
6190 if (!dpi->lambda_tpl_parms)
6191 a = d_find_pack (dpi, d_left (dc));
6192 if (a == NULL)
6194 /* d_find_pack won't find anything if the only packs involved
6195 in this expansion are function parameter packs; in that
6196 case, just print the pattern and "...". */
6197 d_print_subexpr (dpi, options, d_left (dc));
6198 d_append_string (dpi, "...");
6200 else
6202 int len = d_pack_length (a);
6203 int i;
6205 dc = d_left (dc);
6206 for (i = 0; i < len; ++i)
6208 if (i)
6209 d_append_string (dpi, ", ");
6210 dpi->pack_index = i;
6211 d_print_comp (dpi, options, dc);
6215 return;
6217 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
6219 long num = dc->u.s_number.number;
6220 if (num == 0)
6221 d_append_string (dpi, "this");
6222 else
6224 d_append_string (dpi, "{parm#");
6225 d_append_num (dpi, num);
6226 d_append_char (dpi, '}');
6229 return;
6231 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
6232 d_append_string (dpi, "global constructors keyed to ");
6233 d_print_comp (dpi, options, dc->u.s_binary.left);
6234 return;
6236 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
6237 d_append_string (dpi, "global destructors keyed to ");
6238 d_print_comp (dpi, options, dc->u.s_binary.left);
6239 return;
6241 case DEMANGLE_COMPONENT_LAMBDA:
6243 d_append_string (dpi, "{lambda");
6244 struct demangle_component *parms = dc->u.s_unary_num.sub;
6245 struct d_print_template dpt;
6246 /* Generic lambda auto parms are mangled as the (synthedic) template
6247 type parm they are. We need to tell the printer that (a) we're in
6248 a lambda, and (b) the number of synthetic parms. */
6249 int saved_tpl_parms = dpi->lambda_tpl_parms;
6250 dpi->lambda_tpl_parms = 0;
6251 /* Hang any lambda head as-if template args. */
6252 dpt.template_decl = NULL;
6253 dpt.next = dpi->templates;
6254 dpi->templates = &dpt;
6255 if (parms && parms->type == DEMANGLE_COMPONENT_TEMPLATE_HEAD)
6257 dpt.template_decl = parms;
6259 d_append_char (dpi, '<');
6260 struct demangle_component *parm;
6261 for (parm = d_left (parms); parm; parm = d_right (parm))
6263 if (dpi->lambda_tpl_parms++)
6264 d_append_string (dpi, ", ");
6265 d_print_comp (dpi, options, parm);
6266 d_append_char (dpi, ' ');
6267 if (parm->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
6268 parm = d_left (parm);
6269 d_print_lambda_parm_name (dpi, parm->type,
6270 dpi->lambda_tpl_parms - 1);
6272 d_append_char (dpi, '>');
6274 parms = d_right (parms);
6276 dpi->lambda_tpl_parms++;
6278 d_append_char (dpi, '(');
6279 d_print_comp (dpi, options, parms);
6280 dpi->lambda_tpl_parms = saved_tpl_parms;
6281 dpi->templates = dpt.next;
6282 d_append_string (dpi, ")#");
6283 d_append_num (dpi, dc->u.s_unary_num.num + 1);
6284 d_append_char (dpi, '}');
6286 return;
6288 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
6289 d_append_string (dpi, "{unnamed type#");
6290 d_append_num (dpi, dc->u.s_number.number + 1);
6291 d_append_char (dpi, '}');
6292 return;
6294 case DEMANGLE_COMPONENT_CLONE:
6295 d_print_comp (dpi, options, d_left (dc));
6296 d_append_string (dpi, " [clone ");
6297 d_print_comp (dpi, options, d_right (dc));
6298 d_append_char (dpi, ']');
6299 return;
6301 case DEMANGLE_COMPONENT_FRIEND:
6302 d_print_comp (dpi, options, d_left (dc));
6303 d_append_string (dpi, "[friend]");
6304 return;
6306 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
6308 d_append_char (dpi, '<');
6309 int count = 0;
6310 struct demangle_component *parm;
6311 for (parm = d_left (dc); parm; parm = d_right (parm))
6313 if (count++)
6314 d_append_string (dpi, ", ");
6315 d_print_comp (dpi, options, parm);
6317 d_append_char (dpi, '>');
6319 return;
6321 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
6322 d_append_string (dpi, "typename");
6323 return;
6325 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
6326 d_print_comp (dpi, options, d_left (dc));
6327 return;
6329 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
6330 d_append_string (dpi, "template");
6331 d_print_comp (dpi, options, d_left (dc));
6332 d_append_string (dpi, " class");
6333 return;
6335 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
6336 d_print_comp (dpi, options, d_left (dc));
6337 d_append_string (dpi, "...");
6338 return;
6340 case DEMANGLE_COMPONENT_CONSTRAINTS:
6341 d_print_comp (dpi, options, d_left (dc));
6342 d_append_string (dpi, " requires ");
6343 d_print_comp (dpi, options, d_right (dc));
6344 return;
6346 default:
6347 d_print_error (dpi);
6348 return;
6352 static void
6353 d_print_comp (struct d_print_info *dpi, int options,
6354 struct demangle_component *dc)
6356 struct d_component_stack self;
6357 if (dc == NULL || dc->d_printing > 1 || dpi->recursion > MAX_RECURSION_COUNT)
6359 d_print_error (dpi);
6360 return;
6363 dc->d_printing++;
6364 dpi->recursion++;
6366 self.dc = dc;
6367 self.parent = dpi->component_stack;
6368 dpi->component_stack = &self;
6370 d_print_comp_inner (dpi, options, dc);
6372 dpi->component_stack = self.parent;
6373 dc->d_printing--;
6374 dpi->recursion--;
6377 /* Print a Java dentifier. For Java we try to handle encoded extended
6378 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
6379 so we don't it for C++. Characters are encoded as
6380 __U<hex-char>+_. */
6382 static void
6383 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
6385 const char *p;
6386 const char *end;
6388 end = name + len;
6389 for (p = name; p < end; ++p)
6391 if (end - p > 3
6392 && p[0] == '_'
6393 && p[1] == '_'
6394 && p[2] == 'U')
6396 unsigned long c;
6397 const char *q;
6399 c = 0;
6400 for (q = p + 3; q < end; ++q)
6402 int dig;
6404 if (IS_DIGIT (*q))
6405 dig = *q - '0';
6406 else if (*q >= 'A' && *q <= 'F')
6407 dig = *q - 'A' + 10;
6408 else if (*q >= 'a' && *q <= 'f')
6409 dig = *q - 'a' + 10;
6410 else
6411 break;
6413 c = c * 16 + dig;
6415 /* If the Unicode character is larger than 256, we don't try
6416 to deal with it here. FIXME. */
6417 if (q < end && *q == '_' && c < 256)
6419 d_append_char (dpi, c);
6420 p = q;
6421 continue;
6425 d_append_char (dpi, *p);
6429 /* Print a list of modifiers. SUFFIX is 1 if we are printing
6430 qualifiers on this after printing a function. */
6432 static void
6433 d_print_mod_list (struct d_print_info *dpi, int options,
6434 struct d_print_mod *mods, int suffix)
6436 struct d_print_template *hold_dpt;
6438 if (mods == NULL || d_print_saw_error (dpi))
6439 return;
6441 if (mods->printed
6442 || (! suffix
6443 && (is_fnqual_component_type (mods->mod->type))))
6445 d_print_mod_list (dpi, options, mods->next, suffix);
6446 return;
6449 mods->printed = 1;
6451 hold_dpt = dpi->templates;
6452 dpi->templates = mods->templates;
6454 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
6456 d_print_function_type (dpi, options, mods->mod, mods->next);
6457 dpi->templates = hold_dpt;
6458 return;
6460 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6462 d_print_array_type (dpi, options, mods->mod, mods->next);
6463 dpi->templates = hold_dpt;
6464 return;
6466 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
6468 struct d_print_mod *hold_modifiers;
6469 struct demangle_component *dc;
6471 /* When this is on the modifier stack, we have pulled any
6472 qualifiers off the right argument already. Otherwise, we
6473 print it as usual, but don't let the left argument see any
6474 modifiers. */
6476 hold_modifiers = dpi->modifiers;
6477 dpi->modifiers = NULL;
6478 d_print_comp (dpi, options, d_left (mods->mod));
6479 dpi->modifiers = hold_modifiers;
6481 if ((options & DMGL_JAVA) == 0)
6482 d_append_string (dpi, "::");
6483 else
6484 d_append_char (dpi, '.');
6486 dc = d_right (mods->mod);
6488 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
6490 d_append_string (dpi, "{default arg#");
6491 d_append_num (dpi, dc->u.s_unary_num.num + 1);
6492 d_append_string (dpi, "}::");
6493 dc = dc->u.s_unary_num.sub;
6496 while (is_fnqual_component_type (dc->type))
6497 dc = d_left (dc);
6499 d_print_comp (dpi, options, dc);
6501 dpi->templates = hold_dpt;
6502 return;
6505 d_print_mod (dpi, options, mods->mod);
6507 dpi->templates = hold_dpt;
6509 d_print_mod_list (dpi, options, mods->next, suffix);
6512 /* Print a modifier. */
6514 static void
6515 d_print_mod (struct d_print_info *dpi, int options,
6516 struct demangle_component *mod)
6518 switch (mod->type)
6520 case DEMANGLE_COMPONENT_RESTRICT:
6521 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6522 d_append_string (dpi, " restrict");
6523 return;
6524 case DEMANGLE_COMPONENT_VOLATILE:
6525 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6526 d_append_string (dpi, " volatile");
6527 return;
6528 case DEMANGLE_COMPONENT_CONST:
6529 case DEMANGLE_COMPONENT_CONST_THIS:
6530 d_append_string (dpi, " const");
6531 return;
6532 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
6533 d_append_string (dpi, " transaction_safe");
6534 return;
6535 case DEMANGLE_COMPONENT_NOEXCEPT:
6536 d_append_string (dpi, " noexcept");
6537 if (d_right (mod))
6539 d_append_char (dpi, '(');
6540 d_print_comp (dpi, options, d_right (mod));
6541 d_append_char (dpi, ')');
6543 return;
6544 case DEMANGLE_COMPONENT_THROW_SPEC:
6545 d_append_string (dpi, " throw");
6546 if (d_right (mod))
6548 d_append_char (dpi, '(');
6549 d_print_comp (dpi, options, d_right (mod));
6550 d_append_char (dpi, ')');
6552 return;
6553 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6554 d_append_char (dpi, ' ');
6555 d_print_comp (dpi, options, d_right (mod));
6556 return;
6557 case DEMANGLE_COMPONENT_POINTER:
6558 /* There is no pointer symbol in Java. */
6559 if ((options & DMGL_JAVA) == 0)
6560 d_append_char (dpi, '*');
6561 return;
6562 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6563 /* For the ref-qualifier, put a space before the &. */
6564 d_append_char (dpi, ' ');
6565 /* FALLTHRU */
6566 case DEMANGLE_COMPONENT_REFERENCE:
6567 d_append_char (dpi, '&');
6568 return;
6569 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6570 d_append_char (dpi, ' ');
6571 /* FALLTHRU */
6572 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6573 d_append_string (dpi, "&&");
6574 return;
6575 case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
6576 return;
6577 case DEMANGLE_COMPONENT_COMPLEX:
6578 d_append_string (dpi, " _Complex");
6579 return;
6580 case DEMANGLE_COMPONENT_IMAGINARY:
6581 d_append_string (dpi, " _Imaginary");
6582 return;
6583 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6584 if (d_last_char (dpi) != '(')
6585 d_append_char (dpi, ' ');
6586 d_print_comp (dpi, options, d_left (mod));
6587 d_append_string (dpi, "::*");
6588 return;
6589 case DEMANGLE_COMPONENT_TYPED_NAME:
6590 d_print_comp (dpi, options, d_left (mod));
6591 return;
6592 case DEMANGLE_COMPONENT_VECTOR_TYPE:
6593 d_append_string (dpi, " __vector(");
6594 d_print_comp (dpi, options, d_left (mod));
6595 d_append_char (dpi, ')');
6596 return;
6598 default:
6599 /* Otherwise, we have something that won't go back on the
6600 modifier stack, so we can just print it. */
6601 d_print_comp (dpi, options, mod);
6602 return;
6606 /* Print a function type, except for the return type. */
6608 static void
6609 d_print_function_type (struct d_print_info *dpi, int options,
6610 struct demangle_component *dc,
6611 struct d_print_mod *mods)
6613 int need_paren;
6614 int need_space;
6615 int xobj_memfn;
6616 struct d_print_mod *p;
6617 struct d_print_mod *hold_modifiers;
6619 need_paren = 0;
6620 need_space = 0;
6621 xobj_memfn = 0;
6622 for (p = mods; p != NULL; p = p->next)
6624 if (p->printed)
6625 break;
6627 switch (p->mod->type)
6629 case DEMANGLE_COMPONENT_POINTER:
6630 case DEMANGLE_COMPONENT_REFERENCE:
6631 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6632 need_paren = 1;
6633 break;
6634 case DEMANGLE_COMPONENT_RESTRICT:
6635 case DEMANGLE_COMPONENT_VOLATILE:
6636 case DEMANGLE_COMPONENT_CONST:
6637 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6638 case DEMANGLE_COMPONENT_COMPLEX:
6639 case DEMANGLE_COMPONENT_IMAGINARY:
6640 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6641 need_space = 1;
6642 need_paren = 1;
6643 break;
6644 case DEMANGLE_COMPONENT_XOBJ_MEMBER_FUNCTION:
6645 xobj_memfn = 1;
6646 break;
6647 default:
6648 break;
6650 if (need_paren)
6651 break;
6654 if (need_paren)
6656 if (! need_space)
6658 if (d_last_char (dpi) != '('
6659 && d_last_char (dpi) != '*')
6660 need_space = 1;
6662 if (need_space && d_last_char (dpi) != ' ')
6663 d_append_char (dpi, ' ');
6664 d_append_char (dpi, '(');
6667 hold_modifiers = dpi->modifiers;
6668 dpi->modifiers = NULL;
6670 d_print_mod_list (dpi, options, mods, 0);
6672 if (need_paren)
6673 d_append_char (dpi, ')');
6675 d_append_char (dpi, '(');
6676 if (xobj_memfn)
6677 d_append_string (dpi, "this ");
6679 if (d_right (dc) != NULL)
6680 d_print_comp (dpi, options, d_right (dc));
6682 d_append_char (dpi, ')');
6684 d_print_mod_list (dpi, options, mods, 1);
6686 dpi->modifiers = hold_modifiers;
6689 /* Print an array type, except for the element type. */
6691 static void
6692 d_print_array_type (struct d_print_info *dpi, int options,
6693 struct demangle_component *dc,
6694 struct d_print_mod *mods)
6696 int need_space;
6698 need_space = 1;
6699 if (mods != NULL)
6701 int need_paren;
6702 struct d_print_mod *p;
6704 need_paren = 0;
6705 for (p = mods; p != NULL; p = p->next)
6707 if (! p->printed)
6709 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6711 need_space = 0;
6712 break;
6714 else
6716 need_paren = 1;
6717 need_space = 1;
6718 break;
6723 if (need_paren)
6724 d_append_string (dpi, " (");
6726 d_print_mod_list (dpi, options, mods, 0);
6728 if (need_paren)
6729 d_append_char (dpi, ')');
6732 if (need_space)
6733 d_append_char (dpi, ' ');
6735 d_append_char (dpi, '[');
6737 if (d_left (dc) != NULL)
6738 d_print_comp (dpi, options, d_left (dc));
6740 d_append_char (dpi, ']');
6743 /* Print an operator in an expression. */
6745 static void
6746 d_print_expr_op (struct d_print_info *dpi, int options,
6747 struct demangle_component *dc)
6749 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
6750 d_append_buffer (dpi, dc->u.s_operator.op->name,
6751 dc->u.s_operator.op->len);
6752 else
6753 d_print_comp (dpi, options, dc);
6756 /* Print a cast. */
6758 static void
6759 d_print_cast (struct d_print_info *dpi, int options,
6760 struct demangle_component *dc)
6762 d_print_comp (dpi, options, d_left (dc));
6765 /* Print a conversion operator. */
6767 static void
6768 d_print_conversion (struct d_print_info *dpi, int options,
6769 struct demangle_component *dc)
6771 struct d_print_template dpt;
6773 /* For a conversion operator, we need the template parameters from
6774 the enclosing template in scope for processing the type. */
6775 if (dpi->current_template != NULL)
6777 dpt.next = dpi->templates;
6778 dpi->templates = &dpt;
6779 dpt.template_decl = dpi->current_template;
6782 d_print_comp (dpi, options, d_left (dc));
6784 if (dpi->current_template != NULL)
6785 dpi->templates = dpt.next;
6788 /* Initialize the information structure we use to pass around
6789 information. */
6791 CP_STATIC_IF_GLIBCPP_V3
6792 void
6793 cplus_demangle_init_info (const char *mangled, int options, size_t len,
6794 struct d_info *di)
6796 di->s = mangled;
6797 di->send = mangled + len;
6798 di->options = options;
6800 di->n = mangled;
6802 /* We cannot need more components than twice the number of chars in
6803 the mangled string. Most components correspond directly to
6804 chars, but the ARGLIST types are exceptions. */
6805 di->num_comps = 2 * len;
6806 di->next_comp = 0;
6808 /* Similarly, we cannot need more substitutions than there are
6809 chars in the mangled string. */
6810 di->num_subs = len;
6811 di->next_sub = 0;
6813 di->last_name = NULL;
6815 di->expansion = 0;
6816 di->is_expression = 0;
6817 di->is_conversion = 0;
6818 di->recursion_level = 0;
6821 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6822 mangled name, return strings in repeated callback giving the demangled
6823 name. OPTIONS is the usual libiberty demangler options. On success,
6824 this returns 1. On failure, returns 0. */
6826 static int
6827 d_demangle_callback (const char *mangled, int options,
6828 demangle_callbackref callback, void *opaque)
6830 enum
6832 DCT_TYPE,
6833 DCT_MANGLED,
6834 DCT_GLOBAL_CTORS,
6835 DCT_GLOBAL_DTORS
6837 type;
6838 struct d_info di;
6839 struct demangle_component *dc;
6840 int status;
6842 if (mangled[0] == '_' && mangled[1] == 'Z')
6843 type = DCT_MANGLED;
6844 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6845 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6846 && (mangled[9] == 'D' || mangled[9] == 'I')
6847 && mangled[10] == '_')
6848 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
6849 else
6851 if ((options & DMGL_TYPES) == 0)
6852 return 0;
6853 type = DCT_TYPE;
6856 di.unresolved_name_state = 1;
6858 again:
6859 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6861 /* PR 87675 - Check for a mangled string that is so long
6862 that we do not have enough stack space to demangle it. */
6863 if (((options & DMGL_NO_RECURSE_LIMIT) == 0)
6864 /* This check is a bit arbitrary, since what we really want to do is to
6865 compare the sizes of the di.comps and di.subs arrays against the
6866 amount of stack space remaining. But there is no portable way to do
6867 this, so instead we use the recursion limit as a guide to the maximum
6868 size of the arrays. */
6869 && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT)
6871 /* FIXME: We need a way to indicate that a stack limit has been reached. */
6872 return 0;
6876 #if 0 /* in valgrind */
6877 #ifdef CP_DYNAMIC_ARRAYS
6878 __extension__ struct demangle_component comps[di.num_comps];
6879 __extension__ struct demangle_component *subs[di.num_subs];
6881 di.comps = comps;
6882 di.subs = subs;
6883 #else
6884 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6885 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6886 #endif
6887 #else
6888 /* Allocate memory dynamically to avoid VLAs as valgrind stack
6889 is a scarce resource */
6890 di.comps = xmalloc (di.num_comps * sizeof (*di.comps));
6891 di.subs = xmalloc (di.num_subs * sizeof (*di.subs));
6892 #endif /* ! in valgrind */
6894 switch (type)
6896 case DCT_TYPE:
6897 dc = cplus_demangle_type (&di);
6898 break;
6899 case DCT_MANGLED:
6900 dc = cplus_demangle_mangled_name (&di, 1);
6901 break;
6902 case DCT_GLOBAL_CTORS:
6903 case DCT_GLOBAL_DTORS:
6904 d_advance (&di, 11);
6905 dc = d_make_comp (&di,
6906 (type == DCT_GLOBAL_CTORS
6907 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6908 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
6909 d_make_demangle_mangled_name (&di, d_str (&di)),
6910 NULL);
6911 d_advance (&di, strlen (d_str (&di)));
6912 break;
6913 default:
6914 abort (); /* We have listed all the cases. */
6917 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6918 mangled string, then we didn't successfully demangle it. If
6919 DMGL_PARAMS is not set, we didn't look at the trailing
6920 parameters. */
6921 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
6922 dc = NULL;
6924 /* See discussion in d_unresolved_name. */
6925 if (dc == NULL && di.unresolved_name_state == -1)
6927 di.unresolved_name_state = 0;
6928 goto again;
6931 #ifdef CP_DEMANGLE_DEBUG
6932 d_dump (dc, 0);
6933 #endif
6935 status = (dc != NULL)
6936 ? cplus_demangle_print_callback (options, dc, callback, opaque)
6937 : 0;
6940 #if 0 /* in valgrind */
6941 #else
6942 free (di.comps);
6943 free (di.subs);
6944 #endif /* in valgrind */
6946 return status;
6949 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6950 name, return a buffer allocated with malloc holding the demangled
6951 name. OPTIONS is the usual libiberty demangler options. On
6952 success, this sets *PALC to the allocated size of the returned
6953 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6954 a memory allocation failure, and returns NULL. */
6956 static char *
6957 d_demangle (const char *mangled, int options, size_t *palc)
6959 struct d_growable_string dgs;
6960 int status;
6962 d_growable_string_init (&dgs, 0);
6964 status = d_demangle_callback (mangled, options,
6965 d_growable_string_callback_adapter, &dgs);
6966 if (status == 0)
6968 free (dgs.buf);
6969 *palc = 0;
6970 return NULL;
6973 *palc = dgs.allocation_failure ? 1 : dgs.alc;
6974 return dgs.buf;
6977 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6979 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6981 /* ia64 ABI-mandated entry point in the C++ runtime library for
6982 performing demangling. MANGLED_NAME is a NUL-terminated character
6983 string containing the name to be demangled.
6985 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6986 *LENGTH bytes, into which the demangled name is stored. If
6987 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6988 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6989 is placed in a region of memory allocated with malloc.
6991 If LENGTH is non-NULL, the length of the buffer containing the
6992 demangled name, is placed in *LENGTH.
6994 The return value is a pointer to the start of the NUL-terminated
6995 demangled name, or NULL if the demangling fails. The caller is
6996 responsible for deallocating this memory using free.
6998 *STATUS is set to one of the following values:
6999 0: The demangling operation succeeded.
7000 -1: A memory allocation failure occurred.
7001 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
7002 -3: One of the arguments is invalid.
7004 The demangling is performed using the C++ ABI mangling rules, with
7005 GNU extensions. */
7007 char *
7008 __cxa_demangle (const char *mangled_name, char *output_buffer,
7009 size_t *length, int *status)
7011 char *demangled;
7012 size_t alc;
7014 if (mangled_name == NULL)
7016 if (status != NULL)
7017 *status = -3;
7018 return NULL;
7021 if (output_buffer != NULL && length == NULL)
7023 if (status != NULL)
7024 *status = -3;
7025 return NULL;
7028 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
7030 if (demangled == NULL)
7032 if (status != NULL)
7034 if (alc == 1)
7035 *status = -1;
7036 else
7037 *status = -2;
7039 return NULL;
7042 if (output_buffer == NULL)
7044 if (length != NULL)
7045 *length = alc;
7047 else
7049 if (strlen (demangled) < *length)
7051 strcpy (output_buffer, demangled);
7052 free (demangled);
7053 demangled = output_buffer;
7055 else
7057 free (output_buffer);
7058 *length = alc;
7062 if (status != NULL)
7063 *status = 0;
7065 return demangled;
7068 extern int __gcclibcxx_demangle_callback (const char *,
7069 void (*)
7070 (const char *, size_t, void *),
7071 void *);
7073 /* Alternative, allocationless entry point in the C++ runtime library
7074 for performing demangling. MANGLED_NAME is a NUL-terminated character
7075 string containing the name to be demangled.
7077 CALLBACK is a callback function, called with demangled string
7078 segments as demangling progresses; it is called at least once,
7079 but may be called more than once. OPAQUE is a generalized pointer
7080 used as a callback argument.
7082 The return code is one of the following values, equivalent to
7083 the STATUS values of __cxa_demangle() (excluding -1, since this
7084 function performs no memory allocations):
7085 0: The demangling operation succeeded.
7086 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
7087 -3: One of the arguments is invalid.
7089 The demangling is performed using the C++ ABI mangling rules, with
7090 GNU extensions. */
7093 __gcclibcxx_demangle_callback (const char *mangled_name,
7094 void (*callback) (const char *, size_t, void *),
7095 void *opaque)
7097 int status;
7099 if (mangled_name == NULL || callback == NULL)
7100 return -3;
7102 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
7103 callback, opaque);
7104 if (status == 0)
7105 return -2;
7107 return 0;
7110 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
7112 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
7113 mangled name, return a buffer allocated with malloc holding the
7114 demangled name. Otherwise, return NULL. */
7116 char *
7117 cplus_demangle_v3 (const char *mangled, int options)
7119 size_t alc;
7121 return d_demangle (mangled, options, &alc);
7125 cplus_demangle_v3_callback (const char *mangled, int options,
7126 demangle_callbackref callback, void *opaque)
7128 return d_demangle_callback (mangled, options, callback, opaque);
7131 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
7132 conventions, but the output formatting is a little different.
7133 This instructs the C++ demangler not to emit pointer characters ("*"), to
7134 use Java's namespace separator symbol ("." instead of "::"), and to output
7135 JArray<TYPE> as TYPE[]. */
7137 char *
7138 java_demangle_v3 (const char *mangled)
7140 size_t alc;
7142 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
7146 java_demangle_v3_callback (const char *mangled,
7147 demangle_callbackref callback, void *opaque)
7149 return d_demangle_callback (mangled,
7150 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
7151 callback, opaque);
7154 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
7156 #ifndef IN_GLIBCPP_V3
7158 /* Demangle a string in order to find out whether it is a constructor
7159 or destructor. Return non-zero on success. Set *CTOR_KIND and
7160 *DTOR_KIND appropriately. */
7162 static int
7163 is_ctor_or_dtor (const char *mangled,
7164 enum gnu_v3_ctor_kinds *ctor_kind,
7165 enum gnu_v3_dtor_kinds *dtor_kind)
7167 struct d_info di;
7168 struct demangle_component *dc;
7169 int ret;
7171 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
7172 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
7174 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
7177 #if 0 /* in valgrind */
7178 #ifdef CP_DYNAMIC_ARRAYS
7179 __extension__ struct demangle_component comps[di.num_comps];
7180 __extension__ struct demangle_component *subs[di.num_subs];
7182 di.comps = comps;
7183 di.subs = subs;
7184 #else
7185 di.comps = alloca (di.num_comps * sizeof (*di.comps));
7186 di.subs = alloca (di.num_subs * sizeof (*di.subs));
7187 #endif
7188 #else
7189 /* Allocate memory dynamically to avoid VLAs as valgrind stack
7190 is a scarce resource */
7191 di.comps = xmalloc (di.num_comps * sizeof (*di.comps));
7192 di.subs = xmalloc (di.num_subs * sizeof (*di.subs));
7193 #endif /* ! in valgrind */
7194 dc = cplus_demangle_mangled_name (&di, 1);
7196 /* Note that because we did not pass DMGL_PARAMS, we don't expect
7197 to demangle the entire string. */
7199 ret = 0;
7200 while (dc != NULL)
7202 switch (dc->type)
7204 /* These cannot appear on a constructor or destructor. */
7205 case DEMANGLE_COMPONENT_RESTRICT_THIS:
7206 case DEMANGLE_COMPONENT_VOLATILE_THIS:
7207 case DEMANGLE_COMPONENT_CONST_THIS:
7208 case DEMANGLE_COMPONENT_REFERENCE_THIS:
7209 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
7210 default:
7211 dc = NULL;
7212 break;
7213 case DEMANGLE_COMPONENT_TYPED_NAME:
7214 case DEMANGLE_COMPONENT_TEMPLATE:
7215 dc = d_left (dc);
7216 break;
7217 case DEMANGLE_COMPONENT_QUAL_NAME:
7218 case DEMANGLE_COMPONENT_LOCAL_NAME:
7219 dc = d_right (dc);
7220 break;
7221 case DEMANGLE_COMPONENT_CTOR:
7222 *ctor_kind = dc->u.s_ctor.kind;
7223 ret = 1;
7224 dc = NULL;
7225 break;
7226 case DEMANGLE_COMPONENT_DTOR:
7227 *dtor_kind = dc->u.s_dtor.kind;
7228 ret = 1;
7229 dc = NULL;
7230 break;
7235 #if 0 /* in valgrind */
7236 #else
7237 free (di.comps);
7238 free (di.subs);
7239 #endif /* in valgrind */
7241 return ret;
7244 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
7245 name. A non-zero return indicates the type of constructor. */
7247 enum gnu_v3_ctor_kinds
7248 is_gnu_v3_mangled_ctor (const char *name)
7250 enum gnu_v3_ctor_kinds ctor_kind;
7251 enum gnu_v3_dtor_kinds dtor_kind;
7253 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7254 return (enum gnu_v3_ctor_kinds) 0;
7255 return ctor_kind;
7259 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
7260 name. A non-zero return indicates the type of destructor. */
7262 enum gnu_v3_dtor_kinds
7263 is_gnu_v3_mangled_dtor (const char *name)
7265 enum gnu_v3_ctor_kinds ctor_kind;
7266 enum gnu_v3_dtor_kinds dtor_kind;
7268 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7269 return (enum gnu_v3_dtor_kinds) 0;
7270 return dtor_kind;
7273 #endif /* IN_GLIBCPP_V3 */
7275 #ifdef STANDALONE_DEMANGLER
7277 #if 0 /* in valgrind */
7278 #include "getopt.h"
7279 #include "dyn-string.h"
7280 #endif /* ! in valgrind */
7282 static void print_usage (FILE* fp, int exit_value);
7284 #define IS_ALPHA(CHAR) \
7285 (((CHAR) >= 'a' && (CHAR) <= 'z') \
7286 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
7288 /* Non-zero if CHAR is a character than can occur in a mangled name. */
7289 #define is_mangled_char(CHAR) \
7290 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
7291 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
7293 /* The name of this program, as invoked. */
7294 const char* program_name;
7296 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
7298 static void
7299 print_usage (FILE* fp, int exit_value)
7301 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
7302 fprintf (fp, "Options:\n");
7303 fprintf (fp, " -h,--help Display this message.\n");
7304 fprintf (fp, " -p,--no-params Don't display function parameters\n");
7305 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
7306 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
7308 exit (exit_value);
7311 /* Option specification for getopt_long. */
7312 static const struct option long_options[] =
7314 { "help", no_argument, NULL, 'h' },
7315 { "no-params", no_argument, NULL, 'p' },
7316 { "verbose", no_argument, NULL, 'v' },
7317 { NULL, no_argument, NULL, 0 },
7320 /* Main entry for a demangling filter executable. It will demangle
7321 its command line arguments, if any. If none are provided, it will
7322 filter stdin to stdout, replacing any recognized mangled C++ names
7323 with their demangled equivalents. */
7326 main (int argc, char *argv[])
7328 int i;
7329 int opt_char;
7330 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
7332 /* Use the program name of this program, as invoked. */
7333 program_name = argv[0];
7335 /* Parse options. */
7338 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
7339 switch (opt_char)
7341 case '?': /* Unrecognized option. */
7342 print_usage (stderr, 1);
7343 break;
7345 case 'h':
7346 print_usage (stdout, 0);
7347 break;
7349 case 'p':
7350 options &= ~ DMGL_PARAMS;
7351 break;
7353 case 'v':
7354 options |= DMGL_VERBOSE;
7355 break;
7358 while (opt_char != -1);
7360 if (optind == argc)
7361 /* No command line arguments were provided. Filter stdin. */
7363 dyn_string_t mangled = dyn_string_new (3);
7364 char *s;
7366 /* Read all of input. */
7367 while (!feof (stdin))
7369 char c;
7371 /* Pile characters into mangled until we hit one that can't
7372 occur in a mangled name. */
7373 c = getchar ();
7374 while (!feof (stdin) && is_mangled_char (c))
7376 dyn_string_append_char (mangled, c);
7377 if (feof (stdin))
7378 break;
7379 c = getchar ();
7382 if (dyn_string_length (mangled) > 0)
7384 #ifdef IN_GLIBCPP_V3
7385 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
7386 #else
7387 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
7388 #endif
7390 if (s != NULL)
7392 fputs (s, stdout);
7393 free (s);
7395 else
7397 /* It might not have been a mangled name. Print the
7398 original text. */
7399 fputs (dyn_string_buf (mangled), stdout);
7402 dyn_string_clear (mangled);
7405 /* If we haven't hit EOF yet, we've read one character that
7406 can't occur in a mangled name, so print it out. */
7407 if (!feof (stdin))
7408 putchar (c);
7411 dyn_string_delete (mangled);
7413 else
7414 /* Demangle command line arguments. */
7416 /* Loop over command line arguments. */
7417 for (i = optind; i < argc; ++i)
7419 char *s;
7420 #ifdef IN_GLIBCPP_V3
7421 int status;
7422 #endif
7424 /* Attempt to demangle. */
7425 #ifdef IN_GLIBCPP_V3
7426 s = __cxa_demangle (argv[i], NULL, NULL, &status);
7427 #else
7428 s = cplus_demangle_v3 (argv[i], options);
7429 #endif
7431 /* If it worked, print the demangled name. */
7432 if (s != NULL)
7434 printf ("%s\n", s);
7435 free (s);
7437 else
7439 #ifdef IN_GLIBCPP_V3
7440 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
7441 #else
7442 fprintf (stderr, "Failed: %s\n", argv[i]);
7443 #endif
7448 return 0;
7451 #endif /* STANDALONE_DEMANGLER */