tree-optimization/112990 - unsupported VEC_PERM from match pattern
[official-gcc.git] / libiberty / cp-demangle.c
blob151f2ebfa5244ae2f66409842b36ed4522775dad
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003-2023 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
40 name.
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 int cplus_demangle_v3_callback(const char *mangled, int options,
46 demangle_callbackref callback)
47 int java_demangle_v3_callback(const char *mangled,
48 demangle_callbackref callback)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
57 demangle_callbackref
58 and these functions defined in this file:
59 cplus_demangle_fill_name
60 cplus_demangle_fill_extended_operator
61 cplus_demangle_fill_ctor
62 cplus_demangle_fill_dtor
63 cplus_demangle_print
64 cplus_demangle_print_callback
65 and other functions defined in the file cp-demint.c.
67 This file also defines some other functions and variables which are
68 only to be used by the file cp-demint.c.
70 Preprocessor macros you can define while compiling this file:
72 IN_LIBGCC2
73 If defined, this file defines the following functions, q.v.:
74 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
75 int *status)
76 int __gcclibcxx_demangle_callback (const char *,
77 void (*)
78 (const char *, size_t, void *),
79 void *)
80 instead of cplus_demangle_v3[_callback]() and
81 java_demangle_v3[_callback]().
83 IN_GLIBCPP_V3
84 If defined, this file defines only __cxa_demangle() and
85 __gcclibcxx_demangle_callback(), and no other publically visible
86 functions or variables.
88 STANDALONE_DEMANGLER
89 If defined, this file defines a main() function which demangles
90 any arguments, or, if none, demangles stdin.
92 CP_DEMANGLE_DEBUG
93 If defined, turns on debugging mode, which prints information on
94 stdout about the mangled string. This is not generally useful.
96 CHECK_DEMANGLER
97 If defined, additional sanity checks will be performed. It will
98 cause some slowdown, but will allow to catch out-of-bound access
99 errors earlier. This macro is intended for testing and debugging. */
101 #if defined (_AIX) && !defined (__GNUC__)
102 #pragma alloca
103 #endif
105 #ifdef HAVE_CONFIG_H
106 #include "config.h"
107 #endif
109 #include <stdio.h>
111 #ifdef HAVE_STDLIB_H
112 #include <stdlib.h>
113 #endif
114 #ifdef HAVE_STRING_H
115 #include <string.h>
116 #endif
118 #ifdef HAVE_ALLOCA_H
119 # include <alloca.h>
120 #else
121 # ifndef alloca
122 # ifdef __GNUC__
123 # define alloca __builtin_alloca
124 # else
125 extern char *alloca ();
126 # endif /* __GNUC__ */
127 # endif /* alloca */
128 #endif /* HAVE_ALLOCA_H */
130 #ifdef HAVE_LIMITS_H
131 #include <limits.h>
132 #endif
133 #ifndef INT_MAX
134 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
135 #endif
137 #include "ansidecl.h"
138 #include "libiberty.h"
139 #include "demangle.h"
140 #include "cp-demangle.h"
142 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
143 also rename them via #define to avoid compiler errors when the
144 static definition conflicts with the extern declaration in a header
145 file. */
146 #ifdef IN_GLIBCPP_V3
148 #define CP_STATIC_IF_GLIBCPP_V3 static
150 #define cplus_demangle_fill_name d_fill_name
151 static int d_fill_name (struct demangle_component *, const char *, int);
153 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
154 static int
155 d_fill_extended_operator (struct demangle_component *, int,
156 struct demangle_component *);
158 #define cplus_demangle_fill_ctor d_fill_ctor
159 static int
160 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
161 struct demangle_component *);
163 #define cplus_demangle_fill_dtor d_fill_dtor
164 static int
165 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
166 struct demangle_component *);
168 #define cplus_demangle_mangled_name d_mangled_name
169 static struct demangle_component *d_mangled_name (struct d_info *, int);
171 #define cplus_demangle_type d_type
172 static struct demangle_component *d_type (struct d_info *);
174 #define cplus_demangle_print d_print
175 static char *d_print (int, struct demangle_component *, int, size_t *);
177 #define cplus_demangle_print_callback d_print_callback
178 static int d_print_callback (int, struct demangle_component *,
179 demangle_callbackref, void *);
181 #define cplus_demangle_init_info d_init_info
182 static void d_init_info (const char *, int, size_t, struct d_info *);
184 #else /* ! defined(IN_GLIBCPP_V3) */
185 #define CP_STATIC_IF_GLIBCPP_V3
186 #endif /* ! defined(IN_GLIBCPP_V3) */
188 /* See if the compiler supports dynamic arrays. */
190 #ifdef __GNUC__
191 #define CP_DYNAMIC_ARRAYS
192 #else
193 #ifdef __STDC__
194 #ifdef __STDC_VERSION__
195 #if __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__
196 #define CP_DYNAMIC_ARRAYS
197 #endif /* __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__ */
198 #endif /* defined (__STDC_VERSION__) */
199 #endif /* defined (__STDC__) */
200 #endif /* ! defined (__GNUC__) */
202 /* We avoid pulling in the ctype tables, to prevent pulling in
203 additional unresolved symbols when this code is used in a library.
204 FIXME: Is this really a valid reason? This comes from the original
205 V3 demangler code.
207 As of this writing this file has the following undefined references
208 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
209 strcat, strlen. */
211 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
212 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
213 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
215 /* The prefix prepended by GCC to an identifier represnting the
216 anonymous namespace. */
217 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
218 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
219 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
221 /* Information we keep for the standard substitutions. */
223 struct d_standard_sub_info
225 /* The code for this substitution. */
226 char code;
227 /* The simple string it expands to. */
228 const char *simple_expansion;
229 /* The length of the simple expansion. */
230 int simple_len;
231 /* The results of a full, verbose, expansion. This is used when
232 qualifying a constructor/destructor, or when in verbose mode. */
233 const char *full_expansion;
234 /* The length of the full expansion. */
235 int full_len;
236 /* What to set the last_name field of d_info to; NULL if we should
237 not set it. This is only relevant when qualifying a
238 constructor/destructor. */
239 const char *set_last_name;
240 /* The length of set_last_name. */
241 int set_last_name_len;
244 /* Accessors for subtrees of struct demangle_component. */
246 #define d_left(dc) ((dc)->u.s_binary.left)
247 #define d_right(dc) ((dc)->u.s_binary.right)
249 /* A list of templates. This is used while printing. */
251 struct d_print_template
253 /* Next template on the list. */
254 struct d_print_template *next;
255 /* This template. */
256 const struct demangle_component *template_decl;
259 /* A list of type modifiers. This is used while printing. */
261 struct d_print_mod
263 /* Next modifier on the list. These are in the reverse of the order
264 in which they appeared in the mangled string. */
265 struct d_print_mod *next;
266 /* The modifier. */
267 struct demangle_component *mod;
268 /* Whether this modifier was printed. */
269 int printed;
270 /* The list of templates which applies to this modifier. */
271 struct d_print_template *templates;
274 /* We use these structures to hold information during printing. */
276 struct d_growable_string
278 /* Buffer holding the result. */
279 char *buf;
280 /* Current length of data in buffer. */
281 size_t len;
282 /* Allocated size of buffer. */
283 size_t alc;
284 /* Set to 1 if we had a memory allocation failure. */
285 int allocation_failure;
288 /* Stack of components, innermost first, used to avoid loops. */
290 struct d_component_stack
292 /* This component. */
293 const struct demangle_component *dc;
294 /* This component's parent. */
295 const struct d_component_stack *parent;
298 /* A demangle component and some scope captured when it was first
299 traversed. */
301 struct d_saved_scope
303 /* The component whose scope this is. */
304 const struct demangle_component *container;
305 /* The list of templates, if any, that was current when this
306 scope was captured. */
307 struct d_print_template *templates;
310 /* Checkpoint structure to allow backtracking. This holds copies
311 of the fields of struct d_info that need to be restored
312 if a trial parse needs to be backtracked over. */
314 struct d_info_checkpoint
316 const char *n;
317 int next_comp;
318 int next_sub;
319 int expansion;
322 /* Maximum number of times d_print_comp may be called recursively. */
323 #define MAX_RECURSION_COUNT 1024
325 enum { D_PRINT_BUFFER_LENGTH = 256 };
326 struct d_print_info
328 /* Fixed-length allocated buffer for demangled data, flushed to the
329 callback with a NUL termination once full. */
330 char buf[D_PRINT_BUFFER_LENGTH];
331 /* Current length of data in buffer. */
332 size_t len;
333 /* The last character printed, saved individually so that it survives
334 any buffer flush. */
335 char last_char;
336 /* Callback function to handle demangled buffer flush. */
337 demangle_callbackref callback;
338 /* Opaque callback argument. */
339 void *opaque;
340 /* The current list of templates, if any. */
341 struct d_print_template *templates;
342 /* The current list of modifiers (e.g., pointer, reference, etc.),
343 if any. */
344 struct d_print_mod *modifiers;
345 /* Set to 1 if we saw a demangling error. */
346 int demangle_failure;
347 /* Number of times d_print_comp was recursively called. Should not
348 be bigger than MAX_RECURSION_COUNT. */
349 int recursion;
350 /* 1 more than the number of explicit template parms of a lambda. Template
351 parm references >= are actually 'auto'. */
352 int lambda_tpl_parms;
353 /* The current index into any template argument packs we are using
354 for printing, or -1 to print the whole pack. */
355 int pack_index;
356 /* Number of d_print_flush calls so far. */
357 unsigned long int flush_count;
358 /* Stack of components, innermost first, used to avoid loops. */
359 const struct d_component_stack *component_stack;
360 /* Array of saved scopes for evaluating substitutions. */
361 struct d_saved_scope *saved_scopes;
362 /* Index of the next unused saved scope in the above array. */
363 int next_saved_scope;
364 /* Number of saved scopes in the above array. */
365 int num_saved_scopes;
366 /* Array of templates for saving into scopes. */
367 struct d_print_template *copy_templates;
368 /* Index of the next unused copy template in the above array. */
369 int next_copy_template;
370 /* Number of copy templates in the above array. */
371 int num_copy_templates;
372 /* The nearest enclosing template, if any. */
373 const struct demangle_component *current_template;
376 #ifdef CP_DEMANGLE_DEBUG
377 static void d_dump (struct demangle_component *, int);
378 #endif
380 static struct demangle_component *
381 d_make_empty (struct d_info *);
383 static struct demangle_component *
384 d_make_comp (struct d_info *, enum demangle_component_type,
385 struct demangle_component *,
386 struct demangle_component *);
388 static struct demangle_component *
389 d_make_name (struct d_info *, const char *, int);
391 static struct demangle_component *
392 d_make_demangle_mangled_name (struct d_info *, const char *);
394 static struct demangle_component *
395 d_make_builtin_type (struct d_info *,
396 const struct demangle_builtin_type_info *);
398 static struct demangle_component *
399 d_make_operator (struct d_info *,
400 const struct demangle_operator_info *);
402 static struct demangle_component *
403 d_make_extended_operator (struct d_info *, int,
404 struct demangle_component *);
406 static struct demangle_component *
407 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
408 struct demangle_component *);
410 static struct demangle_component *
411 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
412 struct demangle_component *);
414 static struct demangle_component *
415 d_make_template_param (struct d_info *, int);
417 static struct demangle_component *
418 d_make_sub (struct d_info *, const char *, int);
420 static int
421 has_return_type (struct demangle_component *);
423 static int
424 is_ctor_dtor_or_conversion (struct demangle_component *);
426 static struct demangle_component *d_encoding (struct d_info *, int);
428 static struct demangle_component *d_name (struct d_info *, int substable);
430 static struct demangle_component *d_nested_name (struct d_info *);
432 static int d_maybe_module_name (struct d_info *, struct demangle_component **);
434 static struct demangle_component *d_prefix (struct d_info *, int);
436 static struct demangle_component *d_unqualified_name (struct d_info *,
437 struct demangle_component *scope, struct demangle_component *module);
439 static struct demangle_component *d_source_name (struct d_info *);
441 static int d_number (struct d_info *);
443 static struct demangle_component *d_identifier (struct d_info *, int);
445 static struct demangle_component *d_operator_name (struct d_info *);
447 static struct demangle_component *d_special_name (struct d_info *);
449 static struct demangle_component *d_parmlist (struct d_info *);
451 static int d_call_offset (struct d_info *, int);
453 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
455 static struct demangle_component **
456 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
458 static struct demangle_component *
459 d_ref_qualifier (struct d_info *, struct demangle_component *);
461 static struct demangle_component *
462 d_function_type (struct d_info *);
464 static struct demangle_component *
465 d_bare_function_type (struct d_info *, int);
467 static struct demangle_component *
468 d_class_enum_type (struct d_info *, int);
470 static struct demangle_component *d_array_type (struct d_info *);
472 static struct demangle_component *d_vector_type (struct d_info *);
474 static struct demangle_component *
475 d_pointer_to_member_type (struct d_info *);
477 static struct demangle_component *
478 d_template_param (struct d_info *);
480 static struct demangle_component *d_template_args (struct d_info *);
481 static struct demangle_component *d_template_args_1 (struct d_info *);
483 static struct demangle_component *
484 d_template_arg (struct d_info *);
486 static struct demangle_component *d_expression (struct d_info *);
488 static struct demangle_component *d_expr_primary (struct d_info *);
490 static struct demangle_component *d_local_name (struct d_info *);
492 static int d_discriminator (struct d_info *);
494 static struct demangle_component *d_template_parm (struct d_info *, int *bad);
496 static struct demangle_component *d_template_head (struct d_info *, int *bad);
498 static struct demangle_component *d_lambda (struct d_info *);
500 static struct demangle_component *d_unnamed_type (struct d_info *);
502 static struct demangle_component *
503 d_clone_suffix (struct d_info *, struct demangle_component *);
505 static int
506 d_add_substitution (struct d_info *, struct demangle_component *);
508 static struct demangle_component *d_substitution (struct d_info *, int);
510 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
512 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
514 static void d_growable_string_init (struct d_growable_string *, size_t);
516 static inline void
517 d_growable_string_resize (struct d_growable_string *, size_t);
519 static inline void
520 d_growable_string_append_buffer (struct d_growable_string *,
521 const char *, size_t);
522 static void
523 d_growable_string_callback_adapter (const char *, size_t, void *);
525 static void
526 d_print_init (struct d_print_info *, demangle_callbackref, void *,
527 struct demangle_component *);
529 static inline void d_print_error (struct d_print_info *);
531 static inline int d_print_saw_error (struct d_print_info *);
533 static inline void d_print_flush (struct d_print_info *);
535 static inline void d_append_char (struct d_print_info *, char);
537 static inline void d_append_buffer (struct d_print_info *,
538 const char *, size_t);
540 static inline void d_append_string (struct d_print_info *, const char *);
542 static inline char d_last_char (struct d_print_info *);
544 static void
545 d_print_comp (struct d_print_info *, int, struct demangle_component *);
547 static void
548 d_print_java_identifier (struct d_print_info *, const char *, int);
550 static void
551 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
553 static void
554 d_print_mod (struct d_print_info *, int, struct demangle_component *);
556 static void
557 d_print_function_type (struct d_print_info *, int,
558 struct demangle_component *,
559 struct d_print_mod *);
561 static void
562 d_print_array_type (struct d_print_info *, int,
563 struct demangle_component *,
564 struct d_print_mod *);
566 static void
567 d_print_expr_op (struct d_print_info *, int, struct demangle_component *);
569 static void d_print_cast (struct d_print_info *, int,
570 struct demangle_component *);
571 static void d_print_conversion (struct d_print_info *, int,
572 struct demangle_component *);
574 static int d_demangle_callback (const char *, int,
575 demangle_callbackref, void *);
576 static char *d_demangle (const char *, int, size_t *);
578 #define FNQUAL_COMPONENT_CASE \
579 case DEMANGLE_COMPONENT_RESTRICT_THIS: \
580 case DEMANGLE_COMPONENT_VOLATILE_THIS: \
581 case DEMANGLE_COMPONENT_CONST_THIS: \
582 case DEMANGLE_COMPONENT_REFERENCE_THIS: \
583 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
584 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
585 case DEMANGLE_COMPONENT_NOEXCEPT: \
586 case DEMANGLE_COMPONENT_THROW_SPEC
588 /* True iff TYPE is a demangling component representing a
589 function-type-qualifier. */
591 static int
592 is_fnqual_component_type (enum demangle_component_type type)
594 switch (type)
596 FNQUAL_COMPONENT_CASE:
597 return 1;
598 default:
599 break;
601 return 0;
605 #ifdef CP_DEMANGLE_DEBUG
607 static void
608 d_dump (struct demangle_component *dc, int indent)
610 int i;
612 if (dc == NULL)
614 if (indent == 0)
615 printf ("failed demangling\n");
616 return;
619 for (i = 0; i < indent; ++i)
620 putchar (' ');
622 switch (dc->type)
624 case DEMANGLE_COMPONENT_NAME:
625 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
626 return;
627 case DEMANGLE_COMPONENT_TAGGED_NAME:
628 printf ("tagged name\n");
629 d_dump (dc->u.s_binary.left, indent + 2);
630 d_dump (dc->u.s_binary.right, indent + 2);
631 return;
632 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
633 printf ("template parameter %ld\n", dc->u.s_number.number);
634 return;
635 case DEMANGLE_COMPONENT_TPARM_OBJ:
636 printf ("template parameter object\n");
637 break;
638 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
639 printf ("function parameter %ld\n", dc->u.s_number.number);
640 return;
641 case DEMANGLE_COMPONENT_CTOR:
642 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
643 d_dump (dc->u.s_ctor.name, indent + 2);
644 return;
645 case DEMANGLE_COMPONENT_DTOR:
646 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
647 d_dump (dc->u.s_dtor.name, indent + 2);
648 return;
649 case DEMANGLE_COMPONENT_SUB_STD:
650 printf ("standard substitution %s\n", dc->u.s_string.string);
651 return;
652 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
653 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
654 return;
655 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
657 char suffix[2] = { dc->u.s_extended_builtin.type->suffix, 0 };
658 printf ("builtin type %s%d%s\n", dc->u.s_extended_builtin.type->name,
659 dc->u.s_extended_builtin.type->arg, suffix);
661 return;
662 case DEMANGLE_COMPONENT_OPERATOR:
663 printf ("operator %s\n", dc->u.s_operator.op->name);
664 return;
665 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
666 printf ("extended operator with %d args\n",
667 dc->u.s_extended_operator.args);
668 d_dump (dc->u.s_extended_operator.name, indent + 2);
669 return;
671 case DEMANGLE_COMPONENT_QUAL_NAME:
672 printf ("qualified name\n");
673 break;
674 case DEMANGLE_COMPONENT_LOCAL_NAME:
675 printf ("local name\n");
676 break;
677 case DEMANGLE_COMPONENT_TYPED_NAME:
678 printf ("typed name\n");
679 break;
680 case DEMANGLE_COMPONENT_TEMPLATE:
681 printf ("template\n");
682 break;
683 case DEMANGLE_COMPONENT_VTABLE:
684 printf ("vtable\n");
685 break;
686 case DEMANGLE_COMPONENT_VTT:
687 printf ("VTT\n");
688 break;
689 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
690 printf ("construction vtable\n");
691 break;
692 case DEMANGLE_COMPONENT_TYPEINFO:
693 printf ("typeinfo\n");
694 break;
695 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
696 printf ("typeinfo name\n");
697 break;
698 case DEMANGLE_COMPONENT_TYPEINFO_FN:
699 printf ("typeinfo function\n");
700 break;
701 case DEMANGLE_COMPONENT_THUNK:
702 printf ("thunk\n");
703 break;
704 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
705 printf ("virtual thunk\n");
706 break;
707 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
708 printf ("covariant thunk\n");
709 break;
710 case DEMANGLE_COMPONENT_JAVA_CLASS:
711 printf ("java class\n");
712 break;
713 case DEMANGLE_COMPONENT_GUARD:
714 printf ("guard\n");
715 break;
716 case DEMANGLE_COMPONENT_REFTEMP:
717 printf ("reference temporary\n");
718 break;
719 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
720 printf ("hidden alias\n");
721 break;
722 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
723 printf ("transaction clone\n");
724 break;
725 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
726 printf ("non-transaction clone\n");
727 break;
728 case DEMANGLE_COMPONENT_RESTRICT:
729 printf ("restrict\n");
730 break;
731 case DEMANGLE_COMPONENT_VOLATILE:
732 printf ("volatile\n");
733 break;
734 case DEMANGLE_COMPONENT_CONST:
735 printf ("const\n");
736 break;
737 case DEMANGLE_COMPONENT_RESTRICT_THIS:
738 printf ("restrict this\n");
739 break;
740 case DEMANGLE_COMPONENT_VOLATILE_THIS:
741 printf ("volatile this\n");
742 break;
743 case DEMANGLE_COMPONENT_CONST_THIS:
744 printf ("const this\n");
745 break;
746 case DEMANGLE_COMPONENT_REFERENCE_THIS:
747 printf ("reference this\n");
748 break;
749 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
750 printf ("rvalue reference this\n");
751 break;
752 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
753 printf ("transaction_safe this\n");
754 break;
755 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
756 printf ("vendor type qualifier\n");
757 break;
758 case DEMANGLE_COMPONENT_POINTER:
759 printf ("pointer\n");
760 break;
761 case DEMANGLE_COMPONENT_REFERENCE:
762 printf ("reference\n");
763 break;
764 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
765 printf ("rvalue reference\n");
766 break;
767 case DEMANGLE_COMPONENT_COMPLEX:
768 printf ("complex\n");
769 break;
770 case DEMANGLE_COMPONENT_IMAGINARY:
771 printf ("imaginary\n");
772 break;
773 case DEMANGLE_COMPONENT_VENDOR_TYPE:
774 printf ("vendor type\n");
775 break;
776 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
777 printf ("function type\n");
778 break;
779 case DEMANGLE_COMPONENT_ARRAY_TYPE:
780 printf ("array type\n");
781 break;
782 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
783 printf ("pointer to member type\n");
784 break;
785 case DEMANGLE_COMPONENT_ARGLIST:
786 printf ("argument list\n");
787 break;
788 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
789 printf ("template argument list\n");
790 break;
791 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
792 printf ("initializer list\n");
793 break;
794 case DEMANGLE_COMPONENT_CAST:
795 printf ("cast\n");
796 break;
797 case DEMANGLE_COMPONENT_CONVERSION:
798 printf ("conversion operator\n");
799 break;
800 case DEMANGLE_COMPONENT_NULLARY:
801 printf ("nullary operator\n");
802 break;
803 case DEMANGLE_COMPONENT_UNARY:
804 printf ("unary operator\n");
805 break;
806 case DEMANGLE_COMPONENT_BINARY:
807 printf ("binary operator\n");
808 break;
809 case DEMANGLE_COMPONENT_BINARY_ARGS:
810 printf ("binary operator arguments\n");
811 break;
812 case DEMANGLE_COMPONENT_TRINARY:
813 printf ("trinary operator\n");
814 break;
815 case DEMANGLE_COMPONENT_TRINARY_ARG1:
816 printf ("trinary operator arguments 1\n");
817 break;
818 case DEMANGLE_COMPONENT_TRINARY_ARG2:
819 printf ("trinary operator arguments 1\n");
820 break;
821 case DEMANGLE_COMPONENT_LITERAL:
822 printf ("literal\n");
823 break;
824 case DEMANGLE_COMPONENT_LITERAL_NEG:
825 printf ("negative literal\n");
826 break;
827 case DEMANGLE_COMPONENT_VENDOR_EXPR:
828 printf ("vendor expression\n");
829 break;
830 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
831 printf ("java resource\n");
832 break;
833 case DEMANGLE_COMPONENT_COMPOUND_NAME:
834 printf ("compound name\n");
835 break;
836 case DEMANGLE_COMPONENT_CHARACTER:
837 printf ("character '%c'\n", dc->u.s_character.character);
838 return;
839 case DEMANGLE_COMPONENT_NUMBER:
840 printf ("number %ld\n", dc->u.s_number.number);
841 return;
842 case DEMANGLE_COMPONENT_DECLTYPE:
843 printf ("decltype\n");
844 break;
845 case DEMANGLE_COMPONENT_PACK_EXPANSION:
846 printf ("pack expansion\n");
847 break;
848 case DEMANGLE_COMPONENT_TLS_INIT:
849 printf ("tls init function\n");
850 break;
851 case DEMANGLE_COMPONENT_TLS_WRAPPER:
852 printf ("tls wrapper function\n");
853 break;
854 case DEMANGLE_COMPONENT_DEFAULT_ARG:
855 printf ("default argument %d\n", dc->u.s_unary_num.num);
856 d_dump (dc->u.s_unary_num.sub, indent+2);
857 return;
858 case DEMANGLE_COMPONENT_LAMBDA:
859 printf ("lambda %d\n", dc->u.s_unary_num.num);
860 d_dump (dc->u.s_unary_num.sub, indent+2);
861 return;
864 d_dump (d_left (dc), indent + 2);
865 d_dump (d_right (dc), indent + 2);
868 #endif /* CP_DEMANGLE_DEBUG */
870 /* Fill in a DEMANGLE_COMPONENT_NAME. */
872 CP_STATIC_IF_GLIBCPP_V3
874 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
876 if (p == NULL || s == NULL || len <= 0)
877 return 0;
878 p->d_printing = 0;
879 p->d_counting = 0;
880 p->type = DEMANGLE_COMPONENT_NAME;
881 p->u.s_name.s = s;
882 p->u.s_name.len = len;
883 return 1;
886 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
888 CP_STATIC_IF_GLIBCPP_V3
890 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
891 struct demangle_component *name)
893 if (p == NULL || args < 0 || name == NULL)
894 return 0;
895 p->d_printing = 0;
896 p->d_counting = 0;
897 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
898 p->u.s_extended_operator.args = args;
899 p->u.s_extended_operator.name = name;
900 return 1;
903 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
905 CP_STATIC_IF_GLIBCPP_V3
907 cplus_demangle_fill_ctor (struct demangle_component *p,
908 enum gnu_v3_ctor_kinds kind,
909 struct demangle_component *name)
911 if (p == NULL
912 || name == NULL
913 || (int) kind < gnu_v3_complete_object_ctor
914 || (int) kind > gnu_v3_object_ctor_group)
915 return 0;
916 p->d_printing = 0;
917 p->d_counting = 0;
918 p->type = DEMANGLE_COMPONENT_CTOR;
919 p->u.s_ctor.kind = kind;
920 p->u.s_ctor.name = name;
921 return 1;
924 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
926 CP_STATIC_IF_GLIBCPP_V3
928 cplus_demangle_fill_dtor (struct demangle_component *p,
929 enum gnu_v3_dtor_kinds kind,
930 struct demangle_component *name)
932 if (p == NULL
933 || name == NULL
934 || (int) kind < gnu_v3_deleting_dtor
935 || (int) kind > gnu_v3_object_dtor_group)
936 return 0;
937 p->d_printing = 0;
938 p->d_counting = 0;
939 p->type = DEMANGLE_COMPONENT_DTOR;
940 p->u.s_dtor.kind = kind;
941 p->u.s_dtor.name = name;
942 return 1;
945 /* Add a new component. */
947 static struct demangle_component *
948 d_make_empty (struct d_info *di)
950 struct demangle_component *p;
952 if (di->next_comp >= di->num_comps)
953 return NULL;
954 p = &di->comps[di->next_comp];
955 p->d_printing = 0;
956 p->d_counting = 0;
957 ++di->next_comp;
958 return p;
961 /* Add a new generic component. */
963 static struct demangle_component *
964 d_make_comp (struct d_info *di, enum demangle_component_type type,
965 struct demangle_component *left,
966 struct demangle_component *right)
968 struct demangle_component *p;
970 /* We check for errors here. A typical error would be a NULL return
971 from a subroutine. We catch those here, and return NULL
972 upward. */
973 switch (type)
975 /* These types require two parameters. */
976 case DEMANGLE_COMPONENT_QUAL_NAME:
977 case DEMANGLE_COMPONENT_LOCAL_NAME:
978 case DEMANGLE_COMPONENT_TYPED_NAME:
979 case DEMANGLE_COMPONENT_TAGGED_NAME:
980 case DEMANGLE_COMPONENT_TEMPLATE:
981 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
982 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
983 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
984 case DEMANGLE_COMPONENT_UNARY:
985 case DEMANGLE_COMPONENT_BINARY:
986 case DEMANGLE_COMPONENT_BINARY_ARGS:
987 case DEMANGLE_COMPONENT_TRINARY:
988 case DEMANGLE_COMPONENT_TRINARY_ARG1:
989 case DEMANGLE_COMPONENT_LITERAL:
990 case DEMANGLE_COMPONENT_LITERAL_NEG:
991 case DEMANGLE_COMPONENT_VENDOR_EXPR:
992 case DEMANGLE_COMPONENT_COMPOUND_NAME:
993 case DEMANGLE_COMPONENT_VECTOR_TYPE:
994 case DEMANGLE_COMPONENT_CLONE:
995 case DEMANGLE_COMPONENT_MODULE_ENTITY:
996 case DEMANGLE_COMPONENT_CONSTRAINTS:
997 if (left == NULL || right == NULL)
998 return NULL;
999 break;
1001 /* These types only require one parameter. */
1002 case DEMANGLE_COMPONENT_VTABLE:
1003 case DEMANGLE_COMPONENT_VTT:
1004 case DEMANGLE_COMPONENT_TYPEINFO:
1005 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
1006 case DEMANGLE_COMPONENT_TYPEINFO_FN:
1007 case DEMANGLE_COMPONENT_THUNK:
1008 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
1009 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
1010 case DEMANGLE_COMPONENT_JAVA_CLASS:
1011 case DEMANGLE_COMPONENT_GUARD:
1012 case DEMANGLE_COMPONENT_TLS_INIT:
1013 case DEMANGLE_COMPONENT_TLS_WRAPPER:
1014 case DEMANGLE_COMPONENT_REFTEMP:
1015 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
1016 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
1017 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
1018 case DEMANGLE_COMPONENT_POINTER:
1019 case DEMANGLE_COMPONENT_REFERENCE:
1020 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
1021 case DEMANGLE_COMPONENT_COMPLEX:
1022 case DEMANGLE_COMPONENT_IMAGINARY:
1023 case DEMANGLE_COMPONENT_VENDOR_TYPE:
1024 case DEMANGLE_COMPONENT_CAST:
1025 case DEMANGLE_COMPONENT_CONVERSION:
1026 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
1027 case DEMANGLE_COMPONENT_DECLTYPE:
1028 case DEMANGLE_COMPONENT_PACK_EXPANSION:
1029 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
1030 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
1031 case DEMANGLE_COMPONENT_NULLARY:
1032 case DEMANGLE_COMPONENT_TRINARY_ARG2:
1033 case DEMANGLE_COMPONENT_TPARM_OBJ:
1034 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
1035 case DEMANGLE_COMPONENT_MODULE_INIT:
1036 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
1037 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
1038 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
1039 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
1040 case DEMANGLE_COMPONENT_FRIEND:
1041 if (left == NULL)
1042 return NULL;
1043 break;
1045 /* This needs a right parameter, but the left parameter can be
1046 empty. */
1047 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1048 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
1049 case DEMANGLE_COMPONENT_MODULE_NAME:
1050 case DEMANGLE_COMPONENT_MODULE_PARTITION:
1051 if (right == NULL)
1052 return NULL;
1053 break;
1055 /* These are allowed to have no parameters--in some cases they
1056 will be filled in later. */
1057 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1058 case DEMANGLE_COMPONENT_RESTRICT:
1059 case DEMANGLE_COMPONENT_VOLATILE:
1060 case DEMANGLE_COMPONENT_CONST:
1061 case DEMANGLE_COMPONENT_ARGLIST:
1062 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1063 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
1064 FNQUAL_COMPONENT_CASE:
1065 break;
1067 /* Other types should not be seen here. */
1068 default:
1069 return NULL;
1072 p = d_make_empty (di);
1073 if (p != NULL)
1075 p->type = type;
1076 p->u.s_binary.left = left;
1077 p->u.s_binary.right = right;
1079 return p;
1082 /* Add a new demangle mangled name component. */
1084 static struct demangle_component *
1085 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1087 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1088 return d_make_name (di, s, strlen (s));
1089 d_advance (di, 2);
1090 return d_encoding (di, 0);
1093 /* Add a new name component. */
1095 static struct demangle_component *
1096 d_make_name (struct d_info *di, const char *s, int len)
1098 struct demangle_component *p;
1100 p = d_make_empty (di);
1101 if (! cplus_demangle_fill_name (p, s, len))
1102 return NULL;
1103 return p;
1106 /* Add a new builtin type component. */
1108 static struct demangle_component *
1109 d_make_builtin_type (struct d_info *di,
1110 const struct demangle_builtin_type_info *type)
1112 struct demangle_component *p;
1114 if (type == NULL)
1115 return NULL;
1116 p = d_make_empty (di);
1117 if (p != NULL)
1119 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1120 p->u.s_builtin.type = type;
1122 return p;
1125 /* Add a new extended builtin type component. */
1127 static struct demangle_component *
1128 d_make_extended_builtin_type (struct d_info *di,
1129 const struct demangle_builtin_type_info *type,
1130 short arg, char suffix)
1132 struct demangle_component *p;
1134 if (type == NULL)
1135 return NULL;
1136 p = d_make_empty (di);
1137 if (p != NULL)
1139 p->type = DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE;
1140 p->u.s_extended_builtin.type = type;
1141 p->u.s_extended_builtin.arg = arg;
1142 p->u.s_extended_builtin.suffix = suffix;
1144 return p;
1147 /* Add a new operator component. */
1149 static struct demangle_component *
1150 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1152 struct demangle_component *p;
1154 p = d_make_empty (di);
1155 if (p != NULL)
1157 p->type = DEMANGLE_COMPONENT_OPERATOR;
1158 p->u.s_operator.op = op;
1160 return p;
1163 /* Add a new extended operator component. */
1165 static struct demangle_component *
1166 d_make_extended_operator (struct d_info *di, int args,
1167 struct demangle_component *name)
1169 struct demangle_component *p;
1171 p = d_make_empty (di);
1172 if (! cplus_demangle_fill_extended_operator (p, args, name))
1173 return NULL;
1174 return p;
1177 static struct demangle_component *
1178 d_make_default_arg (struct d_info *di, int num,
1179 struct demangle_component *sub)
1181 struct demangle_component *p = d_make_empty (di);
1182 if (p)
1184 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1185 p->u.s_unary_num.num = num;
1186 p->u.s_unary_num.sub = sub;
1188 return p;
1191 /* Add a new constructor component. */
1193 static struct demangle_component *
1194 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1195 struct demangle_component *name)
1197 struct demangle_component *p;
1199 p = d_make_empty (di);
1200 if (! cplus_demangle_fill_ctor (p, kind, name))
1201 return NULL;
1202 return p;
1205 /* Add a new destructor component. */
1207 static struct demangle_component *
1208 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1209 struct demangle_component *name)
1211 struct demangle_component *p;
1213 p = d_make_empty (di);
1214 if (! cplus_demangle_fill_dtor (p, kind, name))
1215 return NULL;
1216 return p;
1219 /* Add a new template parameter. */
1221 static struct demangle_component *
1222 d_make_template_param (struct d_info *di, int i)
1224 struct demangle_component *p;
1226 p = d_make_empty (di);
1227 if (p != NULL)
1229 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1230 p->u.s_number.number = i;
1232 return p;
1235 /* Add a new function parameter. */
1237 static struct demangle_component *
1238 d_make_function_param (struct d_info *di, int i)
1240 struct demangle_component *p;
1242 p = d_make_empty (di);
1243 if (p != NULL)
1245 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1246 p->u.s_number.number = i;
1248 return p;
1251 /* Add a new standard substitution component. */
1253 static struct demangle_component *
1254 d_make_sub (struct d_info *di, const char *name, int len)
1256 struct demangle_component *p;
1258 p = d_make_empty (di);
1259 if (p != NULL)
1261 p->type = DEMANGLE_COMPONENT_SUB_STD;
1262 p->u.s_string.string = name;
1263 p->u.s_string.len = len;
1265 return p;
1268 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1270 TOP_LEVEL is non-zero when called at the top level. */
1272 CP_STATIC_IF_GLIBCPP_V3
1273 struct demangle_component *
1274 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1276 struct demangle_component *p;
1278 if (! d_check_char (di, '_')
1279 /* Allow missing _ if not at toplevel to work around a
1280 bug in G++ abi-version=2 mangling; see the comment in
1281 write_template_arg. */
1282 && top_level)
1283 return NULL;
1284 if (! d_check_char (di, 'Z'))
1285 return NULL;
1286 p = d_encoding (di, top_level);
1288 /* If at top level and parsing parameters, check for a clone
1289 suffix. */
1290 if (top_level && (di->options & DMGL_PARAMS) != 0)
1291 while (d_peek_char (di) == '.'
1292 && (IS_LOWER (d_peek_next_char (di))
1293 || d_peek_next_char (di) == '_'
1294 || IS_DIGIT (d_peek_next_char (di))))
1295 p = d_clone_suffix (di, p);
1297 return p;
1300 /* Return whether a function should have a return type. The argument
1301 is the function name, which may be qualified in various ways. The
1302 rules are that template functions have return types with some
1303 exceptions, function types which are not part of a function name
1304 mangling have return types with some exceptions, and non-template
1305 function names do not have return types. The exceptions are that
1306 constructors, destructors, and conversion operators do not have
1307 return types. */
1309 static int
1310 has_return_type (struct demangle_component *dc)
1312 if (dc == NULL)
1313 return 0;
1314 switch (dc->type)
1316 default:
1317 return 0;
1318 case DEMANGLE_COMPONENT_LOCAL_NAME:
1319 return has_return_type (d_right (dc));
1320 case DEMANGLE_COMPONENT_TEMPLATE:
1321 return ! is_ctor_dtor_or_conversion (d_left (dc));
1322 FNQUAL_COMPONENT_CASE:
1323 return has_return_type (d_left (dc));
1327 /* Return whether a name is a constructor, a destructor, or a
1328 conversion operator. */
1330 static int
1331 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1333 if (dc == NULL)
1334 return 0;
1335 switch (dc->type)
1337 default:
1338 return 0;
1339 case DEMANGLE_COMPONENT_QUAL_NAME:
1340 case DEMANGLE_COMPONENT_LOCAL_NAME:
1341 return is_ctor_dtor_or_conversion (d_right (dc));
1342 case DEMANGLE_COMPONENT_CTOR:
1343 case DEMANGLE_COMPONENT_DTOR:
1344 case DEMANGLE_COMPONENT_CONVERSION:
1345 return 1;
1349 /* [ Q <constraint-expression> ] */
1351 static struct demangle_component *
1352 d_maybe_constraints (struct d_info *di, struct demangle_component *dc)
1354 if (d_peek_char (di) == 'Q')
1356 d_advance (di, 1);
1357 struct demangle_component *expr = d_expression (di);
1358 if (expr == NULL)
1359 return NULL;
1360 dc = d_make_comp (di, DEMANGLE_COMPONENT_CONSTRAINTS, dc, expr);
1362 return dc;
1365 /* <encoding> ::= <(function) name> <bare-function-type>
1366 ::= <(data) name>
1367 ::= <special-name>
1369 TOP_LEVEL is non-zero when called at the top level, in which case
1370 if DMGL_PARAMS is not set we do not demangle the function
1371 parameters. We only set this at the top level, because otherwise
1372 we would not correctly demangle names in local scopes. */
1374 static struct demangle_component *
1375 d_encoding (struct d_info *di, int top_level)
1377 char peek = d_peek_char (di);
1378 struct demangle_component *dc;
1380 if (peek == 'G' || peek == 'T')
1381 dc = d_special_name (di);
1382 else
1384 dc = d_name (di, 0);
1386 if (!dc)
1387 /* Failed already. */;
1388 else if (top_level && (di->options & DMGL_PARAMS) == 0)
1390 /* Strip off any initial CV-qualifiers, as they really apply
1391 to the `this' parameter, and they were not output by the
1392 v2 demangler without DMGL_PARAMS. */
1393 while (is_fnqual_component_type (dc->type))
1394 dc = d_left (dc);
1396 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1397 there may be function-qualifiers on its right argument which
1398 really apply here; this happens when parsing a class
1399 which is local to a function. */
1400 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1402 while (d_right (dc) != NULL
1403 && is_fnqual_component_type (d_right (dc)->type))
1404 d_right (dc) = d_left (d_right (dc));
1406 if (d_right (dc) == NULL)
1407 dc = NULL;
1410 else
1412 peek = d_peek_char (di);
1413 if (peek != '\0' && peek != 'E')
1415 struct demangle_component *ftype;
1417 ftype = d_bare_function_type (di, has_return_type (dc));
1418 if (!ftype)
1419 return NULL;
1421 /* If this is a non-top-level local-name, clear the
1422 return type, so it doesn't confuse the user by
1423 being confused with the return type of whaever
1424 this is nested within. */
1425 if (!top_level && dc->type == DEMANGLE_COMPONENT_LOCAL_NAME
1426 && ftype->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
1427 d_left (ftype) = NULL;
1429 ftype = d_maybe_constraints (di, ftype);
1431 dc = d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME,
1432 dc, ftype);
1437 return dc;
1440 /* <tagged-name> ::= <name> B <source-name> */
1442 static struct demangle_component *
1443 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1445 struct demangle_component *hold_last_name;
1446 char peek;
1448 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1449 hold_last_name = di->last_name;
1451 while (peek = d_peek_char (di),
1452 peek == 'B')
1454 struct demangle_component *tag;
1455 d_advance (di, 1);
1456 tag = d_source_name (di);
1457 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1460 di->last_name = hold_last_name;
1462 return dc;
1465 /* <name> ::= <nested-name>
1466 ::= <unscoped-name>
1467 ::= <unscoped-template-name> <template-args>
1468 ::= <local-name>
1470 <unscoped-name> ::= <unqualified-name>
1471 ::= St <unqualified-name>
1473 <unscoped-template-name> ::= <unscoped-name>
1474 ::= <substitution>
1477 static struct demangle_component *
1478 d_name (struct d_info *di, int substable)
1480 char peek = d_peek_char (di);
1481 struct demangle_component *dc = NULL;
1482 struct demangle_component *module = NULL;
1483 int subst = 0;
1485 switch (peek)
1487 case 'N':
1488 dc = d_nested_name (di);
1489 break;
1491 case 'Z':
1492 dc = d_local_name (di);
1493 break;
1495 case 'U':
1496 dc = d_unqualified_name (di, NULL, NULL);
1497 break;
1499 case 'S':
1501 if (d_peek_next_char (di) == 't')
1503 d_advance (di, 2);
1504 dc = d_make_name (di, "std", 3);
1505 di->expansion += 3;
1508 if (d_peek_char (di) == 'S')
1510 module = d_substitution (di, 0);
1511 if (!module)
1512 return NULL;
1513 if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1514 || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1516 if (dc)
1517 return NULL;
1518 subst = 1;
1519 dc = module;
1520 module = NULL;
1524 /* FALLTHROUGH */
1526 case 'L':
1527 default:
1528 if (!subst)
1529 dc = d_unqualified_name (di, dc, module);
1530 if (d_peek_char (di) == 'I')
1532 /* This is <template-args>, which means that we just saw
1533 <unscoped-template-name>, which is a substitution
1534 candidate. */
1535 if (!subst && !d_add_substitution (di, dc))
1536 return NULL;
1537 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1538 d_template_args (di));
1539 subst = 0;
1541 break;
1543 if (substable && !subst && !d_add_substitution (di, dc))
1544 return NULL;
1545 return dc;
1548 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1549 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1552 static struct demangle_component *
1553 d_nested_name (struct d_info *di)
1555 struct demangle_component *ret;
1556 struct demangle_component **pret;
1557 struct demangle_component *rqual;
1559 if (! d_check_char (di, 'N'))
1560 return NULL;
1562 pret = d_cv_qualifiers (di, &ret, 1);
1563 if (pret == NULL)
1564 return NULL;
1566 /* Parse the ref-qualifier now and then attach it
1567 once we have something to attach it to. */
1568 rqual = d_ref_qualifier (di, NULL);
1570 *pret = d_prefix (di, 1);
1571 if (*pret == NULL)
1572 return NULL;
1574 if (rqual)
1576 d_left (rqual) = ret;
1577 ret = rqual;
1580 if (! d_check_char (di, 'E'))
1581 return NULL;
1583 return ret;
1586 /* <prefix> ::= <prefix> <unqualified-name>
1587 ::= <template-prefix> <template-args>
1588 ::= <template-param>
1589 ::= <decltype>
1591 ::= <substitution>
1593 <template-prefix> ::= <prefix> <(template) unqualified-name>
1594 ::= <template-param>
1595 ::= <substitution>
1597 SUBST is true if we should add substitutions (as normal), false
1598 if not (in an unresolved-name). */
1600 static struct demangle_component *
1601 d_prefix (struct d_info *di, int substable)
1603 struct demangle_component *ret = NULL;
1605 for (;;)
1607 char peek = d_peek_char (di);
1609 /* The older code accepts a <local-name> here, but I don't see
1610 that in the grammar. The older code does not accept a
1611 <template-param> here. */
1613 if (peek == 'D'
1614 && (d_peek_next_char (di) == 'T'
1615 || d_peek_next_char (di) == 't'))
1617 /* Decltype. */
1618 if (ret)
1619 return NULL;
1620 ret = cplus_demangle_type (di);
1622 else if (peek == 'I')
1624 if (ret == NULL)
1625 return NULL;
1626 struct demangle_component *dc = d_template_args (di);
1627 if (!dc)
1628 return NULL;
1629 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret, dc);
1631 else if (peek == 'T')
1633 if (ret)
1634 return NULL;
1635 ret = d_template_param (di);
1637 else if (peek == 'M')
1639 /* Initializer scope for a lambda. We already added it as a
1640 substitution candidate, don't do that again. */
1641 d_advance (di, 1);
1642 continue;
1644 else
1646 struct demangle_component *module = NULL;
1647 if (peek == 'S')
1649 module = d_substitution (di, 1);
1650 if (!module)
1651 return NULL;
1652 if (!(module->type == DEMANGLE_COMPONENT_MODULE_NAME
1653 || module->type == DEMANGLE_COMPONENT_MODULE_PARTITION))
1655 if (ret)
1656 return NULL;
1657 ret = module;
1658 continue;
1661 ret = d_unqualified_name (di, ret, module);
1664 if (!ret)
1665 break;
1667 if (d_peek_char (di) == 'E')
1668 break;
1670 if (substable && !d_add_substitution (di, ret))
1671 return NULL;
1674 return ret;
1677 static int
1678 d_maybe_module_name (struct d_info *di, struct demangle_component **name)
1680 while (d_peek_char (di) == 'W')
1682 d_advance (di, 1);
1683 enum demangle_component_type code = DEMANGLE_COMPONENT_MODULE_NAME;
1684 if (d_peek_char (di) == 'P')
1686 code = DEMANGLE_COMPONENT_MODULE_PARTITION;
1687 d_advance (di, 1);
1690 *name = d_make_comp (di, code, *name, d_source_name (di));
1691 if (!*name)
1692 return 0;
1693 if (!d_add_substitution (di, *name))
1694 return 0;
1696 return 1;
1699 /* <unqualified-name> ::= [<module-name>] <operator-name> [<abi-tags>]
1700 ::= [<module-name>] <ctor-dtor-name> [<abi-tags>]
1701 ::= [<module-name>] <source-name> [<abi-tags>]
1702 ::= [<module-name>] F <source-name> [<abi-tags>]
1703 ::= [<module-name>] <local-source-name> [<abi-tags>]
1704 ::= [<module-name>] DC <source-name>+ E [<abi-tags>]
1705 <local-source-name> ::= L <source-name> <discriminator> [<abi-tags>]
1708 static struct demangle_component *
1709 d_unqualified_name (struct d_info *di, struct demangle_component *scope,
1710 struct demangle_component *module)
1712 struct demangle_component *ret;
1713 char peek;
1714 int member_like_friend = 0;
1716 if (!d_maybe_module_name (di, &module))
1717 return NULL;
1719 peek = d_peek_char (di);
1720 if (peek == 'F')
1722 member_like_friend = 1;
1723 d_advance (di, 1);
1724 peek = d_peek_char (di);
1726 if (IS_DIGIT (peek))
1727 ret = d_source_name (di);
1728 else if (IS_LOWER (peek))
1730 int was_expr = di->is_expression;
1731 if (peek == 'o' && d_peek_next_char (di) == 'n')
1733 d_advance (di, 2);
1734 /* Treat cv as naming a conversion operator. */
1735 di->is_expression = 0;
1737 ret = d_operator_name (di);
1738 di->is_expression = was_expr;
1739 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1741 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1742 if (!strcmp (ret->u.s_operator.op->code, "li"))
1743 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1744 d_source_name (di));
1747 else if (peek == 'D' && d_peek_next_char (di) == 'C')
1749 // structured binding
1750 d_advance (di, 2);
1751 struct demangle_component *prev = NULL;
1754 struct demangle_component *next =
1755 d_make_comp (di, DEMANGLE_COMPONENT_STRUCTURED_BINDING,
1756 d_source_name (di), NULL);
1757 if (prev)
1758 d_right (prev) = next;
1759 else
1760 ret = next;
1761 prev = next;
1763 while (prev && d_peek_char (di) != 'E');
1764 if (prev)
1765 d_advance (di, 1);
1766 else
1767 ret = NULL;
1769 else if (peek == 'C' || peek == 'D')
1770 ret = d_ctor_dtor_name (di);
1771 else if (peek == 'L')
1773 d_advance (di, 1);
1775 ret = d_source_name (di);
1776 if (ret == NULL)
1777 return NULL;
1778 if (! d_discriminator (di))
1779 return NULL;
1781 else if (peek == 'U')
1783 switch (d_peek_next_char (di))
1785 case 'l':
1786 ret = d_lambda (di);
1787 break;
1788 case 't':
1789 ret = d_unnamed_type (di);
1790 break;
1791 default:
1792 return NULL;
1795 else
1796 return NULL;
1798 if (module)
1799 ret = d_make_comp (di, DEMANGLE_COMPONENT_MODULE_ENTITY, ret, module);
1800 if (d_peek_char (di) == 'B')
1801 ret = d_abi_tags (di, ret);
1802 if (member_like_friend)
1803 ret = d_make_comp (di, DEMANGLE_COMPONENT_FRIEND, ret, NULL);
1804 if (scope)
1805 ret = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, scope, ret);
1807 return ret;
1810 /* <source-name> ::= <(positive length) number> <identifier> */
1812 static struct demangle_component *
1813 d_source_name (struct d_info *di)
1815 int len;
1816 struct demangle_component *ret;
1818 len = d_number (di);
1819 if (len <= 0)
1820 return NULL;
1821 ret = d_identifier (di, len);
1822 di->last_name = ret;
1823 return ret;
1826 /* number ::= [n] <(non-negative decimal integer)> */
1828 static int
1829 d_number (struct d_info *di)
1831 int negative;
1832 char peek;
1833 int ret;
1835 negative = 0;
1836 peek = d_peek_char (di);
1837 if (peek == 'n')
1839 negative = 1;
1840 d_advance (di, 1);
1841 peek = d_peek_char (di);
1844 ret = 0;
1845 while (1)
1847 if (! IS_DIGIT (peek))
1849 if (negative)
1850 ret = - ret;
1851 return ret;
1853 if (ret > ((INT_MAX - (peek - '0')) / 10))
1854 return -1;
1855 ret = ret * 10 + (peek - '0');
1856 d_advance (di, 1);
1857 peek = d_peek_char (di);
1861 /* Like d_number, but returns a demangle_component. */
1863 static struct demangle_component *
1864 d_number_component (struct d_info *di)
1866 struct demangle_component *ret = d_make_empty (di);
1867 if (ret)
1869 ret->type = DEMANGLE_COMPONENT_NUMBER;
1870 ret->u.s_number.number = d_number (di);
1872 return ret;
1875 /* identifier ::= <(unqualified source code identifier)> */
1877 static struct demangle_component *
1878 d_identifier (struct d_info *di, int len)
1880 const char *name;
1882 name = d_str (di);
1884 if (di->send - name < len)
1885 return NULL;
1887 d_advance (di, len);
1889 /* A Java mangled name may have a trailing '$' if it is a C++
1890 keyword. This '$' is not included in the length count. We just
1891 ignore the '$'. */
1892 if ((di->options & DMGL_JAVA) != 0
1893 && d_peek_char (di) == '$')
1894 d_advance (di, 1);
1896 /* Look for something which looks like a gcc encoding of an
1897 anonymous namespace, and replace it with a more user friendly
1898 name. */
1899 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1900 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1901 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1903 const char *s;
1905 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1906 if ((*s == '.' || *s == '_' || *s == '$')
1907 && s[1] == 'N')
1909 di->expansion -= len - sizeof "(anonymous namespace)";
1910 return d_make_name (di, "(anonymous namespace)",
1911 sizeof "(anonymous namespace)" - 1);
1915 return d_make_name (di, name, len);
1918 /* operator_name ::= many different two character encodings.
1919 ::= cv <type>
1920 ::= v <digit> <source-name>
1922 This list is sorted for binary search. */
1924 #define NL(s) s, (sizeof s) - 1
1926 CP_STATIC_IF_GLIBCPP_V3
1927 const struct demangle_operator_info cplus_demangle_operators[] =
1929 { "aN", NL ("&="), 2 },
1930 { "aS", NL ("="), 2 },
1931 { "aa", NL ("&&"), 2 },
1932 { "ad", NL ("&"), 1 },
1933 { "an", NL ("&"), 2 },
1934 { "at", NL ("alignof "), 1 },
1935 { "aw", NL ("co_await "), 1 },
1936 { "az", NL ("alignof "), 1 },
1937 { "cc", NL ("const_cast"), 2 },
1938 { "cl", NL ("()"), 2 },
1939 { "cm", NL (","), 2 },
1940 { "co", NL ("~"), 1 },
1941 { "dV", NL ("/="), 2 },
1942 { "dX", NL ("[...]="), 3 }, /* [expr...expr] = expr */
1943 { "da", NL ("delete[] "), 1 },
1944 { "dc", NL ("dynamic_cast"), 2 },
1945 { "de", NL ("*"), 1 },
1946 { "di", NL ("="), 2 }, /* .name = expr */
1947 { "dl", NL ("delete "), 1 },
1948 { "ds", NL (".*"), 2 },
1949 { "dt", NL ("."), 2 },
1950 { "dv", NL ("/"), 2 },
1951 { "dx", NL ("]="), 2 }, /* [expr] = expr */
1952 { "eO", NL ("^="), 2 },
1953 { "eo", NL ("^"), 2 },
1954 { "eq", NL ("=="), 2 },
1955 { "fL", NL ("..."), 3 },
1956 { "fR", NL ("..."), 3 },
1957 { "fl", NL ("..."), 2 },
1958 { "fr", NL ("..."), 2 },
1959 { "ge", NL (">="), 2 },
1960 { "gs", NL ("::"), 1 },
1961 { "gt", NL (">"), 2 },
1962 { "ix", NL ("[]"), 2 },
1963 { "lS", NL ("<<="), 2 },
1964 { "le", NL ("<="), 2 },
1965 { "li", NL ("operator\"\" "), 1 },
1966 { "ls", NL ("<<"), 2 },
1967 { "lt", NL ("<"), 2 },
1968 { "mI", NL ("-="), 2 },
1969 { "mL", NL ("*="), 2 },
1970 { "mi", NL ("-"), 2 },
1971 { "ml", NL ("*"), 2 },
1972 { "mm", NL ("--"), 1 },
1973 { "na", NL ("new[]"), 3 },
1974 { "ne", NL ("!="), 2 },
1975 { "ng", NL ("-"), 1 },
1976 { "nt", NL ("!"), 1 },
1977 { "nw", NL ("new"), 3 },
1978 { "nx", NL ("noexcept"), 1 },
1979 { "oR", NL ("|="), 2 },
1980 { "oo", NL ("||"), 2 },
1981 { "or", NL ("|"), 2 },
1982 { "pL", NL ("+="), 2 },
1983 { "pl", NL ("+"), 2 },
1984 { "pm", NL ("->*"), 2 },
1985 { "pp", NL ("++"), 1 },
1986 { "ps", NL ("+"), 1 },
1987 { "pt", NL ("->"), 2 },
1988 { "qu", NL ("?"), 3 },
1989 { "rM", NL ("%="), 2 },
1990 { "rS", NL (">>="), 2 },
1991 { "rc", NL ("reinterpret_cast"), 2 },
1992 { "rm", NL ("%"), 2 },
1993 { "rs", NL (">>"), 2 },
1994 { "sP", NL ("sizeof..."), 1 },
1995 { "sZ", NL ("sizeof..."), 1 },
1996 { "sc", NL ("static_cast"), 2 },
1997 { "ss", NL ("<=>"), 2 },
1998 { "st", NL ("sizeof "), 1 },
1999 { "sz", NL ("sizeof "), 1 },
2000 { "tr", NL ("throw"), 0 },
2001 { "tw", NL ("throw "), 1 },
2002 { NULL, NULL, 0, 0 }
2005 static struct demangle_component *
2006 d_operator_name (struct d_info *di)
2008 char c1;
2009 char c2;
2011 c1 = d_next_char (di);
2012 c2 = d_next_char (di);
2013 if (c1 == 'v' && IS_DIGIT (c2))
2014 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
2015 else if (c1 == 'c' && c2 == 'v')
2017 struct demangle_component *type;
2018 int was_conversion = di->is_conversion;
2019 struct demangle_component *res;
2021 di->is_conversion = ! di->is_expression;
2022 type = cplus_demangle_type (di);
2023 if (di->is_conversion)
2024 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
2025 else
2026 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
2027 di->is_conversion = was_conversion;
2028 return res;
2030 else
2032 /* LOW is the inclusive lower bound. */
2033 int low = 0;
2034 /* HIGH is the exclusive upper bound. We subtract one to ignore
2035 the sentinel at the end of the array. */
2036 int high = ((sizeof (cplus_demangle_operators)
2037 / sizeof (cplus_demangle_operators[0]))
2038 - 1);
2040 while (1)
2042 int i;
2043 const struct demangle_operator_info *p;
2045 i = low + (high - low) / 2;
2046 p = cplus_demangle_operators + i;
2048 if (c1 == p->code[0] && c2 == p->code[1])
2049 return d_make_operator (di, p);
2051 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
2052 high = i;
2053 else
2054 low = i + 1;
2055 if (low == high)
2056 return NULL;
2061 static struct demangle_component *
2062 d_make_character (struct d_info *di, int c)
2064 struct demangle_component *p;
2065 p = d_make_empty (di);
2066 if (p != NULL)
2068 p->type = DEMANGLE_COMPONENT_CHARACTER;
2069 p->u.s_character.character = c;
2071 return p;
2074 static struct demangle_component *
2075 d_java_resource (struct d_info *di)
2077 struct demangle_component *p = NULL;
2078 struct demangle_component *next = NULL;
2079 int len, i;
2080 char c;
2081 const char *str;
2083 len = d_number (di);
2084 if (len <= 1)
2085 return NULL;
2087 /* Eat the leading '_'. */
2088 if (d_next_char (di) != '_')
2089 return NULL;
2090 len--;
2092 str = d_str (di);
2093 i = 0;
2095 while (len > 0)
2097 c = str[i];
2098 if (!c)
2099 return NULL;
2101 /* Each chunk is either a '$' escape... */
2102 if (c == '$')
2104 i++;
2105 switch (str[i++])
2107 case 'S':
2108 c = '/';
2109 break;
2110 case '_':
2111 c = '.';
2112 break;
2113 case '$':
2114 c = '$';
2115 break;
2116 default:
2117 return NULL;
2119 next = d_make_character (di, c);
2120 d_advance (di, i);
2121 str = d_str (di);
2122 len -= i;
2123 i = 0;
2124 if (next == NULL)
2125 return NULL;
2127 /* ... or a sequence of characters. */
2128 else
2130 while (i < len && str[i] && str[i] != '$')
2131 i++;
2133 next = d_make_name (di, str, i);
2134 d_advance (di, i);
2135 str = d_str (di);
2136 len -= i;
2137 i = 0;
2138 if (next == NULL)
2139 return NULL;
2142 if (p == NULL)
2143 p = next;
2144 else
2146 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
2147 if (p == NULL)
2148 return NULL;
2152 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
2154 return p;
2157 /* <special-name> ::= TV <type>
2158 ::= TT <type>
2159 ::= TI <type>
2160 ::= TS <type>
2161 ::= TA <template-arg>
2162 ::= GV <(object) name>
2163 ::= T <call-offset> <(base) encoding>
2164 ::= Tc <call-offset> <call-offset> <(base) encoding>
2165 Also g++ extensions:
2166 ::= TC <type> <(offset) number> _ <(base) type>
2167 ::= TF <type>
2168 ::= TJ <type>
2169 ::= GR <name>
2170 ::= GA <encoding>
2171 ::= Gr <resource name>
2172 ::= GTt <encoding>
2173 ::= GTn <encoding>
2176 static struct demangle_component *
2177 d_special_name (struct d_info *di)
2179 di->expansion += 20;
2180 if (d_check_char (di, 'T'))
2182 switch (d_next_char (di))
2184 case 'V':
2185 di->expansion -= 5;
2186 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2187 cplus_demangle_type (di), NULL);
2188 case 'T':
2189 di->expansion -= 10;
2190 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2191 cplus_demangle_type (di), NULL);
2192 case 'I':
2193 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2194 cplus_demangle_type (di), NULL);
2195 case 'S':
2196 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2197 cplus_demangle_type (di), NULL);
2199 case 'h':
2200 if (! d_call_offset (di, 'h'))
2201 return NULL;
2202 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2203 d_encoding (di, 0), NULL);
2205 case 'v':
2206 if (! d_call_offset (di, 'v'))
2207 return NULL;
2208 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2209 d_encoding (di, 0), NULL);
2211 case 'c':
2212 if (! d_call_offset (di, '\0'))
2213 return NULL;
2214 if (! d_call_offset (di, '\0'))
2215 return NULL;
2216 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2217 d_encoding (di, 0), NULL);
2219 case 'C':
2221 struct demangle_component *derived_type;
2222 int offset;
2223 struct demangle_component *base_type;
2225 derived_type = cplus_demangle_type (di);
2226 offset = d_number (di);
2227 if (offset < 0)
2228 return NULL;
2229 if (! d_check_char (di, '_'))
2230 return NULL;
2231 base_type = cplus_demangle_type (di);
2232 /* We don't display the offset. FIXME: We should display
2233 it in verbose mode. */
2234 di->expansion += 5;
2235 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2236 base_type, derived_type);
2239 case 'F':
2240 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2241 cplus_demangle_type (di), NULL);
2242 case 'J':
2243 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2244 cplus_demangle_type (di), NULL);
2246 case 'H':
2247 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2248 d_name (di, 0), NULL);
2250 case 'W':
2251 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2252 d_name (di, 0), NULL);
2254 case 'A':
2255 return d_make_comp (di, DEMANGLE_COMPONENT_TPARM_OBJ,
2256 d_template_arg (di), NULL);
2258 default:
2259 return NULL;
2262 else if (d_check_char (di, 'G'))
2264 switch (d_next_char (di))
2266 case 'V':
2267 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD,
2268 d_name (di, 0), NULL);
2270 case 'R':
2272 struct demangle_component *name = d_name (di, 0);
2273 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2274 d_number_component (di));
2277 case 'A':
2278 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2279 d_encoding (di, 0), NULL);
2281 case 'I':
2283 struct demangle_component *module = NULL;
2284 if (!d_maybe_module_name (di, &module) || !module)
2285 return NULL;
2286 return d_make_comp (di, DEMANGLE_COMPONENT_MODULE_INIT,
2287 module, NULL);
2289 case 'T':
2290 switch (d_next_char (di))
2292 case 'n':
2293 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2294 d_encoding (di, 0), NULL);
2295 default:
2296 /* ??? The proposal is that other letters (such as 'h') stand
2297 for different variants of transaction cloning, such as
2298 compiling directly for hardware transaction support. But
2299 they still should all be transactional clones of some sort
2300 so go ahead and call them that. */
2301 case 't':
2302 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2303 d_encoding (di, 0), NULL);
2306 case 'r':
2307 return d_java_resource (di);
2309 default:
2310 return NULL;
2313 else
2314 return NULL;
2317 /* <call-offset> ::= h <nv-offset> _
2318 ::= v <v-offset> _
2320 <nv-offset> ::= <(offset) number>
2322 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2324 The C parameter, if not '\0', is a character we just read which is
2325 the start of the <call-offset>.
2327 We don't display the offset information anywhere. FIXME: We should
2328 display it in verbose mode. */
2330 static int
2331 d_call_offset (struct d_info *di, int c)
2333 if (c == '\0')
2334 c = d_next_char (di);
2336 if (c == 'h')
2337 d_number (di);
2338 else if (c == 'v')
2340 d_number (di);
2341 if (! d_check_char (di, '_'))
2342 return 0;
2343 d_number (di);
2345 else
2346 return 0;
2348 if (! d_check_char (di, '_'))
2349 return 0;
2351 return 1;
2354 /* <ctor-dtor-name> ::= C1
2355 ::= C2
2356 ::= C3
2357 ::= D0
2358 ::= D1
2359 ::= D2
2362 static struct demangle_component *
2363 d_ctor_dtor_name (struct d_info *di)
2365 if (di->last_name != NULL)
2367 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2368 di->expansion += di->last_name->u.s_name.len;
2369 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2370 di->expansion += di->last_name->u.s_string.len;
2372 switch (d_peek_char (di))
2374 case 'C':
2376 enum gnu_v3_ctor_kinds kind;
2377 int inheriting = 0;
2379 if (d_peek_next_char (di) == 'I')
2381 inheriting = 1;
2382 d_advance (di, 1);
2385 switch (d_peek_next_char (di))
2387 case '1':
2388 kind = gnu_v3_complete_object_ctor;
2389 break;
2390 case '2':
2391 kind = gnu_v3_base_object_ctor;
2392 break;
2393 case '3':
2394 kind = gnu_v3_complete_object_allocating_ctor;
2395 break;
2396 case '4':
2397 kind = gnu_v3_unified_ctor;
2398 break;
2399 case '5':
2400 kind = gnu_v3_object_ctor_group;
2401 break;
2402 default:
2403 return NULL;
2406 d_advance (di, 2);
2408 if (inheriting)
2409 cplus_demangle_type (di);
2411 return d_make_ctor (di, kind, di->last_name);
2414 case 'D':
2416 enum gnu_v3_dtor_kinds kind;
2418 switch (d_peek_next_char (di))
2420 case '0':
2421 kind = gnu_v3_deleting_dtor;
2422 break;
2423 case '1':
2424 kind = gnu_v3_complete_object_dtor;
2425 break;
2426 case '2':
2427 kind = gnu_v3_base_object_dtor;
2428 break;
2429 /* digit '3' is not used */
2430 case '4':
2431 kind = gnu_v3_unified_dtor;
2432 break;
2433 case '5':
2434 kind = gnu_v3_object_dtor_group;
2435 break;
2436 default:
2437 return NULL;
2439 d_advance (di, 2);
2440 return d_make_dtor (di, kind, di->last_name);
2443 default:
2444 return NULL;
2448 /* True iff we're looking at an order-insensitive type-qualifier, including
2449 function-type-qualifiers. */
2451 static int
2452 next_is_type_qual (struct d_info *di)
2454 char peek = d_peek_char (di);
2455 if (peek == 'r' || peek == 'V' || peek == 'K')
2456 return 1;
2457 if (peek == 'D')
2459 peek = d_peek_next_char (di);
2460 if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2461 return 1;
2463 return 0;
2466 /* <type> ::= <builtin-type>
2467 ::= <function-type>
2468 ::= <class-enum-type>
2469 ::= <array-type>
2470 ::= <pointer-to-member-type>
2471 ::= <template-param>
2472 ::= <template-template-param> <template-args>
2473 ::= <substitution>
2474 ::= <CV-qualifiers> <type>
2475 ::= P <type>
2476 ::= R <type>
2477 ::= O <type> (C++0x)
2478 ::= C <type>
2479 ::= G <type>
2480 ::= U <source-name> <type>
2482 <builtin-type> ::= various one letter codes
2483 ::= u <source-name>
2486 CP_STATIC_IF_GLIBCPP_V3
2487 const struct demangle_builtin_type_info
2488 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2490 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2491 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2492 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2493 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2494 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2495 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2496 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2497 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2498 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2499 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2500 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2501 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2502 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2503 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2504 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2505 D_PRINT_DEFAULT },
2506 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2507 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2508 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2509 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2510 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2511 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2512 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2513 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2514 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2515 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2516 D_PRINT_UNSIGNED_LONG_LONG },
2517 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2518 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2519 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2520 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2521 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2522 /* 30 */ { NL ("char8_t"), NL ("char8_t"), D_PRINT_DEFAULT },
2523 /* 31 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2524 /* 32 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2525 /* 33 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2526 D_PRINT_DEFAULT },
2527 /* 34 */ { NL ("_Float"), NL ("_Float"), D_PRINT_FLOAT },
2528 /* 35 */ { NL ("std::bfloat16_t"), NL ("std::bfloat16_t"), D_PRINT_FLOAT },
2531 CP_STATIC_IF_GLIBCPP_V3
2532 struct demangle_component *
2533 cplus_demangle_type (struct d_info *di)
2535 char peek;
2536 struct demangle_component *ret;
2537 int can_subst;
2539 /* The ABI specifies that when CV-qualifiers are used, the base type
2540 is substitutable, and the fully qualified type is substitutable,
2541 but the base type with a strict subset of the CV-qualifiers is
2542 not substitutable. The natural recursive implementation of the
2543 CV-qualifiers would cause subsets to be substitutable, so instead
2544 we pull them all off now.
2546 FIXME: The ABI says that order-insensitive vendor qualifiers
2547 should be handled in the same way, but we have no way to tell
2548 which vendor qualifiers are order-insensitive and which are
2549 order-sensitive. So we just assume that they are all
2550 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2551 __vector, and it treats it as order-sensitive when mangling
2552 names. */
2554 if (next_is_type_qual (di))
2556 struct demangle_component **pret;
2558 pret = d_cv_qualifiers (di, &ret, 0);
2559 if (pret == NULL)
2560 return NULL;
2561 if (d_peek_char (di) == 'F')
2563 /* cv-qualifiers before a function type apply to 'this',
2564 so avoid adding the unqualified function type to
2565 the substitution list. */
2566 *pret = d_function_type (di);
2568 else
2569 *pret = cplus_demangle_type (di);
2570 if (!*pret)
2571 return NULL;
2572 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2573 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2575 /* Move the ref-qualifier outside the cv-qualifiers so that
2576 they are printed in the right order. */
2577 struct demangle_component *fn = d_left (*pret);
2578 d_left (*pret) = ret;
2579 ret = *pret;
2580 *pret = fn;
2582 if (! d_add_substitution (di, ret))
2583 return NULL;
2584 return ret;
2587 can_subst = 1;
2589 peek = d_peek_char (di);
2590 switch (peek)
2592 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2593 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2594 case 'o': case 's': case 't':
2595 case 'v': case 'w': case 'x': case 'y': case 'z':
2596 ret = d_make_builtin_type (di,
2597 &cplus_demangle_builtin_types[peek - 'a']);
2598 di->expansion += ret->u.s_builtin.type->len;
2599 can_subst = 0;
2600 d_advance (di, 1);
2601 break;
2603 case 'u':
2604 d_advance (di, 1);
2605 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2606 d_source_name (di), NULL);
2607 break;
2609 case 'F':
2610 ret = d_function_type (di);
2611 break;
2613 case 'A':
2614 ret = d_array_type (di);
2615 break;
2617 case 'M':
2618 ret = d_pointer_to_member_type (di);
2619 break;
2621 case 'T':
2622 ret = d_template_param (di);
2623 if (d_peek_char (di) == 'I')
2625 /* This may be <template-template-param> <template-args>.
2626 If this is the type for a conversion operator, we can
2627 have a <template-template-param> here only by following
2628 a derivation like this:
2630 <nested-name>
2631 -> <template-prefix> <template-args>
2632 -> <prefix> <template-unqualified-name> <template-args>
2633 -> <unqualified-name> <template-unqualified-name> <template-args>
2634 -> <source-name> <template-unqualified-name> <template-args>
2635 -> <source-name> <operator-name> <template-args>
2636 -> <source-name> cv <type> <template-args>
2637 -> <source-name> cv <template-template-param> <template-args> <template-args>
2639 where the <template-args> is followed by another.
2640 Otherwise, we must have a derivation like this:
2642 <nested-name>
2643 -> <template-prefix> <template-args>
2644 -> <prefix> <template-unqualified-name> <template-args>
2645 -> <unqualified-name> <template-unqualified-name> <template-args>
2646 -> <source-name> <template-unqualified-name> <template-args>
2647 -> <source-name> <operator-name> <template-args>
2648 -> <source-name> cv <type> <template-args>
2649 -> <source-name> cv <template-param> <template-args>
2651 where we need to leave the <template-args> to be processed
2652 by d_prefix (following the <template-prefix>).
2654 The <template-template-param> part is a substitution
2655 candidate. */
2656 if (! di->is_conversion)
2658 if (! d_add_substitution (di, ret))
2659 return NULL;
2660 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2661 d_template_args (di));
2663 else
2665 struct demangle_component *args;
2666 struct d_info_checkpoint checkpoint;
2668 d_checkpoint (di, &checkpoint);
2669 args = d_template_args (di);
2670 if (d_peek_char (di) == 'I')
2672 if (! d_add_substitution (di, ret))
2673 return NULL;
2674 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2675 args);
2677 else
2678 d_backtrack (di, &checkpoint);
2681 break;
2683 case 'O':
2684 d_advance (di, 1);
2685 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2686 cplus_demangle_type (di), NULL);
2687 break;
2689 case 'P':
2690 d_advance (di, 1);
2691 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2692 cplus_demangle_type (di), NULL);
2693 break;
2695 case 'R':
2696 d_advance (di, 1);
2697 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2698 cplus_demangle_type (di), NULL);
2699 break;
2701 case 'C':
2702 d_advance (di, 1);
2703 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2704 cplus_demangle_type (di), NULL);
2705 break;
2707 case 'G':
2708 d_advance (di, 1);
2709 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2710 cplus_demangle_type (di), NULL);
2711 break;
2713 case 'U':
2714 d_advance (di, 1);
2715 ret = d_source_name (di);
2716 if (d_peek_char (di) == 'I')
2717 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2718 d_template_args (di));
2719 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2720 cplus_demangle_type (di), ret);
2721 break;
2723 case 'D':
2724 can_subst = 0;
2725 d_advance (di, 1);
2726 peek = d_next_char (di);
2727 switch (peek)
2729 case 'T':
2730 case 't':
2731 /* decltype (expression) */
2732 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2733 d_expression (di), NULL);
2734 if (ret && d_next_char (di) != 'E')
2735 ret = NULL;
2736 can_subst = 1;
2737 break;
2739 case 'p':
2740 /* Pack expansion. */
2741 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2742 cplus_demangle_type (di), NULL);
2743 can_subst = 1;
2744 break;
2746 case 'a':
2747 /* auto */
2748 ret = d_make_name (di, "auto", 4);
2749 break;
2750 case 'c':
2751 /* decltype(auto) */
2752 ret = d_make_name (di, "decltype(auto)", 14);
2753 break;
2755 case 'f':
2756 /* 32-bit decimal floating point */
2757 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2758 di->expansion += ret->u.s_builtin.type->len;
2759 break;
2760 case 'd':
2761 /* 64-bit DFP */
2762 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2763 di->expansion += ret->u.s_builtin.type->len;
2764 break;
2765 case 'e':
2766 /* 128-bit DFP */
2767 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2768 di->expansion += ret->u.s_builtin.type->len;
2769 break;
2770 case 'h':
2771 /* 16-bit half-precision FP */
2772 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2773 di->expansion += ret->u.s_builtin.type->len;
2774 break;
2775 case 'u':
2776 /* char8_t */
2777 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2778 di->expansion += ret->u.s_builtin.type->len;
2779 break;
2780 case 's':
2781 /* char16_t */
2782 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2783 di->expansion += ret->u.s_builtin.type->len;
2784 break;
2785 case 'i':
2786 /* char32_t */
2787 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2788 di->expansion += ret->u.s_builtin.type->len;
2789 break;
2791 case 'F':
2792 /* DF<number>_ - _Float<number>.
2793 DF<number>x - _Float<number>x
2794 DF16b - std::bfloat16_t. */
2796 int arg = d_number (di);
2797 char buf[12];
2798 char suffix = 0;
2799 if (d_peek_char (di) == 'b')
2801 if (arg != 16)
2802 return NULL;
2803 d_advance (di, 1);
2804 ret = d_make_builtin_type (di,
2805 &cplus_demangle_builtin_types[35]);
2806 di->expansion += ret->u.s_builtin.type->len;
2807 break;
2809 if (d_peek_char (di) == 'x')
2810 suffix = 'x';
2811 if (!suffix && d_peek_char (di) != '_')
2812 return NULL;
2814 = d_make_extended_builtin_type (di,
2815 &cplus_demangle_builtin_types[34],
2816 arg, suffix);
2817 d_advance (di, 1);
2818 sprintf (buf, "%d", arg);
2819 di->expansion += ret->u.s_extended_builtin.type->len
2820 + strlen (buf) + (suffix != 0);
2821 break;
2824 case 'v':
2825 ret = d_vector_type (di);
2826 can_subst = 1;
2827 break;
2829 case 'n':
2830 /* decltype(nullptr) */
2831 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[33]);
2832 di->expansion += ret->u.s_builtin.type->len;
2833 break;
2835 default:
2836 return NULL;
2838 break;
2840 default:
2841 return d_class_enum_type (di, 1);
2844 if (can_subst)
2846 if (! d_add_substitution (di, ret))
2847 return NULL;
2850 return ret;
2853 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2855 static struct demangle_component **
2856 d_cv_qualifiers (struct d_info *di,
2857 struct demangle_component **pret, int member_fn)
2859 struct demangle_component **pstart;
2860 char peek;
2862 pstart = pret;
2863 peek = d_peek_char (di);
2864 while (next_is_type_qual (di))
2866 enum demangle_component_type t;
2867 struct demangle_component *right = NULL;
2869 d_advance (di, 1);
2870 if (peek == 'r')
2872 t = (member_fn
2873 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2874 : DEMANGLE_COMPONENT_RESTRICT);
2875 di->expansion += sizeof "restrict";
2877 else if (peek == 'V')
2879 t = (member_fn
2880 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2881 : DEMANGLE_COMPONENT_VOLATILE);
2882 di->expansion += sizeof "volatile";
2884 else if (peek == 'K')
2886 t = (member_fn
2887 ? DEMANGLE_COMPONENT_CONST_THIS
2888 : DEMANGLE_COMPONENT_CONST);
2889 di->expansion += sizeof "const";
2891 else
2893 peek = d_next_char (di);
2894 if (peek == 'x')
2896 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2897 di->expansion += sizeof "transaction_safe";
2899 else if (peek == 'o'
2900 || peek == 'O')
2902 t = DEMANGLE_COMPONENT_NOEXCEPT;
2903 di->expansion += sizeof "noexcept";
2904 if (peek == 'O')
2906 right = d_expression (di);
2907 if (right == NULL)
2908 return NULL;
2909 if (! d_check_char (di, 'E'))
2910 return NULL;
2913 else if (peek == 'w')
2915 t = DEMANGLE_COMPONENT_THROW_SPEC;
2916 di->expansion += sizeof "throw";
2917 right = d_parmlist (di);
2918 if (right == NULL)
2919 return NULL;
2920 if (! d_check_char (di, 'E'))
2921 return NULL;
2923 else
2924 return NULL;
2927 *pret = d_make_comp (di, t, NULL, right);
2928 if (*pret == NULL)
2929 return NULL;
2930 pret = &d_left (*pret);
2932 peek = d_peek_char (di);
2935 if (!member_fn && peek == 'F')
2937 while (pstart != pret)
2939 switch ((*pstart)->type)
2941 case DEMANGLE_COMPONENT_RESTRICT:
2942 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2943 break;
2944 case DEMANGLE_COMPONENT_VOLATILE:
2945 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2946 break;
2947 case DEMANGLE_COMPONENT_CONST:
2948 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2949 break;
2950 default:
2951 break;
2953 pstart = &d_left (*pstart);
2957 return pret;
2960 /* <ref-qualifier> ::= R
2961 ::= O */
2963 static struct demangle_component *
2964 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2966 struct demangle_component *ret = sub;
2967 char peek;
2969 peek = d_peek_char (di);
2970 if (peek == 'R' || peek == 'O')
2972 enum demangle_component_type t;
2973 if (peek == 'R')
2975 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2976 di->expansion += sizeof "&";
2978 else
2980 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2981 di->expansion += sizeof "&&";
2983 d_advance (di, 1);
2985 ret = d_make_comp (di, t, ret, NULL);
2988 return ret;
2991 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2993 static struct demangle_component *
2994 d_function_type (struct d_info *di)
2996 struct demangle_component *ret = NULL;
2998 if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
3000 if (di->recursion_level > DEMANGLE_RECURSION_LIMIT)
3001 /* FIXME: There ought to be a way to report
3002 that the recursion limit has been reached. */
3003 return NULL;
3005 di->recursion_level ++;
3008 if (d_check_char (di, 'F'))
3010 if (d_peek_char (di) == 'Y')
3012 /* Function has C linkage. We don't print this information.
3013 FIXME: We should print it in verbose mode. */
3014 d_advance (di, 1);
3016 ret = d_bare_function_type (di, 1);
3017 ret = d_ref_qualifier (di, ret);
3019 if (! d_check_char (di, 'E'))
3020 ret = NULL;
3023 if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
3024 di->recursion_level --;
3025 return ret;
3028 /* <type>+ */
3030 static struct demangle_component *
3031 d_parmlist (struct d_info *di)
3033 struct demangle_component *tl;
3034 struct demangle_component **ptl;
3036 tl = NULL;
3037 ptl = &tl;
3038 while (1)
3040 struct demangle_component *type;
3042 char peek = d_peek_char (di);
3043 if (peek == '\0' || peek == 'E' || peek == '.' || peek == 'Q')
3044 break;
3045 if ((peek == 'R' || peek == 'O')
3046 && d_peek_next_char (di) == 'E')
3047 /* Function ref-qualifier, not a ref prefix for a parameter type. */
3048 break;
3049 type = cplus_demangle_type (di);
3050 if (type == NULL)
3051 return NULL;
3052 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
3053 if (*ptl == NULL)
3054 return NULL;
3055 ptl = &d_right (*ptl);
3058 /* There should be at least one parameter type besides the optional
3059 return type. A function which takes no arguments will have a
3060 single parameter type void. */
3061 if (tl == NULL)
3062 return NULL;
3064 /* If we have a single parameter type void, omit it. */
3065 if (d_right (tl) == NULL
3066 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3067 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
3069 di->expansion -= d_left (tl)->u.s_builtin.type->len;
3070 d_left (tl) = NULL;
3073 return tl;
3076 /* <bare-function-type> ::= [J]<type>+ */
3078 static struct demangle_component *
3079 d_bare_function_type (struct d_info *di, int has_return_type)
3081 struct demangle_component *return_type;
3082 struct demangle_component *tl;
3083 char peek;
3085 /* Detect special qualifier indicating that the first argument
3086 is the return type. */
3087 peek = d_peek_char (di);
3088 if (peek == 'J')
3090 d_advance (di, 1);
3091 has_return_type = 1;
3094 if (has_return_type)
3096 return_type = cplus_demangle_type (di);
3097 if (return_type == NULL)
3098 return NULL;
3100 else
3101 return_type = NULL;
3103 tl = d_parmlist (di);
3104 if (tl == NULL)
3105 return NULL;
3107 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
3108 return_type, tl);
3111 /* <class-enum-type> ::= <name> */
3113 static struct demangle_component *
3114 d_class_enum_type (struct d_info *di, int substable)
3116 return d_name (di, substable);
3119 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
3120 ::= A [<(dimension) expression>] _ <(element) type>
3123 static struct demangle_component *
3124 d_array_type (struct d_info *di)
3126 char peek;
3127 struct demangle_component *dim;
3129 if (! d_check_char (di, 'A'))
3130 return NULL;
3132 peek = d_peek_char (di);
3133 if (peek == '_')
3134 dim = NULL;
3135 else if (IS_DIGIT (peek))
3137 const char *s;
3139 s = d_str (di);
3142 d_advance (di, 1);
3143 peek = d_peek_char (di);
3145 while (IS_DIGIT (peek));
3146 dim = d_make_name (di, s, d_str (di) - s);
3147 if (dim == NULL)
3148 return NULL;
3150 else
3152 dim = d_expression (di);
3153 if (dim == NULL)
3154 return NULL;
3157 if (! d_check_char (di, '_'))
3158 return NULL;
3160 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
3161 cplus_demangle_type (di));
3164 /* <vector-type> ::= Dv <number> _ <type>
3165 ::= Dv _ <expression> _ <type> */
3167 static struct demangle_component *
3168 d_vector_type (struct d_info *di)
3170 char peek;
3171 struct demangle_component *dim;
3173 peek = d_peek_char (di);
3174 if (peek == '_')
3176 d_advance (di, 1);
3177 dim = d_expression (di);
3179 else
3180 dim = d_number_component (di);
3182 if (dim == NULL)
3183 return NULL;
3185 if (! d_check_char (di, '_'))
3186 return NULL;
3188 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
3189 cplus_demangle_type (di));
3192 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
3194 static struct demangle_component *
3195 d_pointer_to_member_type (struct d_info *di)
3197 struct demangle_component *cl;
3198 struct demangle_component *mem;
3200 if (! d_check_char (di, 'M'))
3201 return NULL;
3203 cl = cplus_demangle_type (di);
3204 if (cl == NULL)
3205 return NULL;
3207 /* The ABI says, "The type of a non-static member function is considered
3208 to be different, for the purposes of substitution, from the type of a
3209 namespace-scope or static member function whose type appears
3210 similar. The types of two non-static member functions are considered
3211 to be different, for the purposes of substitution, if the functions
3212 are members of different classes. In other words, for the purposes of
3213 substitution, the class of which the function is a member is
3214 considered part of the type of function."
3216 For a pointer to member function, this call to cplus_demangle_type
3217 will end up adding a (possibly qualified) non-member function type to
3218 the substitution table, which is not correct; however, the member
3219 function type will never be used in a substitution, so putting the
3220 wrong type in the substitution table is harmless. */
3222 mem = cplus_demangle_type (di);
3223 if (mem == NULL)
3224 return NULL;
3226 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
3229 /* <non-negative number> _ */
3231 static int
3232 d_compact_number (struct d_info *di)
3234 int num;
3235 if (d_peek_char (di) == '_')
3236 num = 0;
3237 else if (d_peek_char (di) == 'n')
3238 return -1;
3239 else
3240 num = d_number (di) + 1;
3242 if (num < 0 || ! d_check_char (di, '_'))
3243 return -1;
3244 return num;
3247 /* <template-param> ::= T_
3248 ::= T <(parameter-2 non-negative) number> _
3251 static struct demangle_component *
3252 d_template_param (struct d_info *di)
3254 int param;
3256 if (! d_check_char (di, 'T'))
3257 return NULL;
3259 param = d_compact_number (di);
3260 if (param < 0)
3261 return NULL;
3263 return d_make_template_param (di, param);
3266 /* <template-args> ::= I <template-arg>+ E */
3268 static struct demangle_component *
3269 d_template_args (struct d_info *di)
3271 if (d_peek_char (di) != 'I'
3272 && d_peek_char (di) != 'J')
3273 return NULL;
3274 d_advance (di, 1);
3276 return d_template_args_1 (di);
3279 /* <template-arg>* [Q <constraint-expression>] E */
3281 static struct demangle_component *
3282 d_template_args_1 (struct d_info *di)
3284 struct demangle_component *hold_last_name;
3285 struct demangle_component *al;
3286 struct demangle_component **pal;
3288 /* Preserve the last name we saw--don't let the template arguments
3289 clobber it, as that would give us the wrong name for a subsequent
3290 constructor or destructor. */
3291 hold_last_name = di->last_name;
3293 if (d_peek_char (di) == 'E')
3295 /* An argument pack can be empty. */
3296 d_advance (di, 1);
3297 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3300 al = NULL;
3301 pal = &al;
3302 while (1)
3304 struct demangle_component *a;
3306 a = d_template_arg (di);
3307 if (a == NULL)
3308 return NULL;
3310 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3311 if (*pal == NULL)
3312 return NULL;
3313 pal = &d_right (*pal);
3315 char peek = d_peek_char (di);
3316 if (peek == 'E' || peek == 'Q')
3317 break;
3320 al = d_maybe_constraints (di, al);
3322 if (d_peek_char (di) != 'E')
3323 return NULL;
3324 d_advance (di, 1);
3326 di->last_name = hold_last_name;
3328 return al;
3331 /* <template-arg> ::= <type>
3332 ::= X <expression> E
3333 ::= <expr-primary>
3336 static struct demangle_component *
3337 d_template_arg (struct d_info *di)
3339 struct demangle_component *ret;
3341 switch (d_peek_char (di))
3343 case 'X':
3344 d_advance (di, 1);
3345 ret = d_expression (di);
3346 if (! d_check_char (di, 'E'))
3347 return NULL;
3348 return ret;
3350 case 'L':
3351 return d_expr_primary (di);
3353 case 'I':
3354 case 'J':
3355 /* An argument pack. */
3356 return d_template_args (di);
3358 default:
3359 return cplus_demangle_type (di);
3363 /* Parse a sequence of expressions until we hit the terminator
3364 character. */
3366 static struct demangle_component *
3367 d_exprlist (struct d_info *di, char terminator)
3369 struct demangle_component *list = NULL;
3370 struct demangle_component **p = &list;
3372 if (d_peek_char (di) == terminator)
3374 d_advance (di, 1);
3375 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3378 while (1)
3380 struct demangle_component *arg = d_expression (di);
3381 if (arg == NULL)
3382 return NULL;
3384 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3385 if (*p == NULL)
3386 return NULL;
3387 p = &d_right (*p);
3389 if (d_peek_char (di) == terminator)
3391 d_advance (di, 1);
3392 break;
3396 return list;
3399 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3400 dynamic_cast, static_cast or reinterpret_cast. */
3402 static int
3403 op_is_new_cast (struct demangle_component *op)
3405 const char *code = op->u.s_operator.op->code;
3406 return (code[1] == 'c'
3407 && (code[0] == 's' || code[0] == 'd'
3408 || code[0] == 'c' || code[0] == 'r'));
3411 /* <unresolved-name> ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3412 ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3413 # T::N::x /decltype(p)::N::x
3414 ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3415 # A::x, N::y, A<T>::z; "gs" means leading "::"
3416 ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3418 "gs" is handled elsewhere, as a unary operator. */
3420 static struct demangle_component *
3421 d_unresolved_name (struct d_info *di)
3423 struct demangle_component *type;
3424 struct demangle_component *name;
3425 char peek;
3427 /* Consume the "sr". */
3428 d_advance (di, 2);
3430 peek = d_peek_char (di);
3431 if (di->unresolved_name_state
3432 && (IS_DIGIT (peek)
3433 || IS_LOWER (peek)
3434 || peek == 'C'
3435 || peek == 'U'
3436 || peek == 'L'))
3438 /* The third production is ambiguous with the old unresolved-name syntax
3439 of <type> <base-unresolved-name>; in the old mangling, A::x was mangled
3440 as sr1A1x, now sr1AE1x. So we first try to demangle using the new
3441 mangling, then with the old if that fails. */
3442 di->unresolved_name_state = -1;
3443 type = d_prefix (di, 0);
3444 if (d_peek_char (di) == 'E')
3445 d_advance (di, 1);
3447 else
3448 type = cplus_demangle_type (di);
3449 name = d_unqualified_name (di, type, NULL);
3450 if (d_peek_char (di) == 'I')
3451 name = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3452 d_template_args (di));
3453 return name;
3456 /* <expression> ::= <(unary) operator-name> <expression>
3457 ::= <(binary) operator-name> <expression> <expression>
3458 ::= <(trinary) operator-name> <expression> <expression> <expression>
3459 ::= cl <expression>+ E
3460 ::= st <type>
3461 ::= <template-param>
3462 ::= u <source-name> <template-arg>* E # vendor extended expression
3463 ::= <unresolved-name>
3464 ::= <expr-primary>
3466 <braced-expression> ::= <expression>
3467 ::= di <field source-name> <braced-expression> # .name = expr
3468 ::= dx <index expression> <braced-expression> # [expr] = expr
3469 ::= dX <range begin expression> <range end expression> <braced-expression>
3470 # [expr ... expr] = expr
3473 static struct demangle_component *
3474 d_expression_1 (struct d_info *di)
3476 char peek;
3478 peek = d_peek_char (di);
3479 if (peek == 'L')
3480 return d_expr_primary (di);
3481 else if (peek == 'T')
3482 return d_template_param (di);
3483 else if (peek == 's' && d_peek_next_char (di) == 'r')
3484 return d_unresolved_name (di);
3485 else if (peek == 's' && d_peek_next_char (di) == 'p')
3487 d_advance (di, 2);
3488 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3489 d_expression_1 (di), NULL);
3491 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3493 /* Function parameter used in a late-specified return type. */
3494 int index;
3495 d_advance (di, 2);
3496 if (d_peek_char (di) == 'T')
3498 /* 'this' parameter. */
3499 d_advance (di, 1);
3500 index = 0;
3502 else
3504 index = d_compact_number (di);
3505 if (index == INT_MAX || index == -1)
3506 return NULL;
3507 index++;
3509 return d_make_function_param (di, index);
3511 else if (IS_DIGIT (peek)
3512 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3514 /* We can get an unqualified name as an expression in the case of
3515 a dependent function call, i.e. decltype(f(t)). */
3516 struct demangle_component *name;
3518 if (peek == 'o')
3519 /* operator-function-id, i.e. operator+(t). */
3520 d_advance (di, 2);
3522 name = d_unqualified_name (di, NULL, NULL);
3523 if (name == NULL)
3524 return NULL;
3525 if (d_peek_char (di) == 'I')
3526 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3527 d_template_args (di));
3528 else
3529 return name;
3531 else if ((peek == 'i' || peek == 't')
3532 && d_peek_next_char (di) == 'l')
3534 /* Brace-enclosed initializer list, untyped or typed. */
3535 struct demangle_component *type = NULL;
3536 d_advance (di, 2);
3537 if (peek == 't')
3538 type = cplus_demangle_type (di);
3539 if (!d_peek_char (di) || !d_peek_next_char (di))
3540 return NULL;
3541 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3542 type, d_exprlist (di, 'E'));
3544 else if (peek == 'u')
3546 /* A vendor extended expression. */
3547 struct demangle_component *name, *args;
3548 d_advance (di, 1);
3549 name = d_source_name (di);
3550 args = d_template_args_1 (di);
3551 return d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_EXPR, name, args);
3553 else
3555 struct demangle_component *op;
3556 const char *code = NULL;
3557 int args;
3559 op = d_operator_name (di);
3560 if (op == NULL)
3561 return NULL;
3563 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3565 code = op->u.s_operator.op->code;
3566 di->expansion += op->u.s_operator.op->len - 2;
3567 if (strcmp (code, "st") == 0)
3568 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3569 cplus_demangle_type (di));
3572 switch (op->type)
3574 default:
3575 return NULL;
3576 case DEMANGLE_COMPONENT_OPERATOR:
3577 args = op->u.s_operator.op->args;
3578 break;
3579 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3580 args = op->u.s_extended_operator.args;
3581 break;
3582 case DEMANGLE_COMPONENT_CAST:
3583 args = 1;
3584 break;
3587 switch (args)
3589 case 0:
3590 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3592 case 1:
3594 struct demangle_component *operand;
3595 int suffix = 0;
3597 if (code && (code[0] == 'p' || code[0] == 'm')
3598 && code[1] == code[0])
3599 /* pp_ and mm_ are the prefix variants. */
3600 suffix = !d_check_char (di, '_');
3602 if (op->type == DEMANGLE_COMPONENT_CAST
3603 && d_check_char (di, '_'))
3604 operand = d_exprlist (di, 'E');
3605 else if (code && !strcmp (code, "sP"))
3606 operand = d_template_args_1 (di);
3607 else
3608 operand = d_expression_1 (di);
3610 if (suffix)
3611 /* Indicate the suffix variant for d_print_comp. */
3612 operand = d_make_comp (di, DEMANGLE_COMPONENT_BINARY_ARGS,
3613 operand, operand);
3615 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, operand);
3617 case 2:
3619 struct demangle_component *left;
3620 struct demangle_component *right;
3622 if (code == NULL)
3623 return NULL;
3624 if (op_is_new_cast (op))
3625 left = cplus_demangle_type (di);
3626 else if (code[0] == 'f')
3627 /* fold-expression. */
3628 left = d_operator_name (di);
3629 else if (!strcmp (code, "di"))
3630 left = d_unqualified_name (di, NULL, NULL);
3631 else
3632 left = d_expression_1 (di);
3633 if (!strcmp (code, "cl"))
3634 right = d_exprlist (di, 'E');
3635 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3637 peek = d_peek_char (di);
3638 /* These codes start a qualified name. */
3639 if ((peek == 'g' && d_peek_next_char (di) == 's')
3640 || (peek == 's' && d_peek_next_char (di) == 'r'))
3641 right = d_expression_1 (di);
3642 else
3644 /* Otherwise it's an unqualified name. We use
3645 d_unqualified_name rather than d_expression_1 here for
3646 old mangled names that didn't add 'on' before operator
3647 names. */
3648 right = d_unqualified_name (di, NULL, NULL);
3649 if (d_peek_char (di) == 'I')
3650 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3651 right, d_template_args (di));
3654 else
3655 right = d_expression_1 (di);
3657 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3658 d_make_comp (di,
3659 DEMANGLE_COMPONENT_BINARY_ARGS,
3660 left, right));
3662 case 3:
3664 struct demangle_component *first;
3665 struct demangle_component *second;
3666 struct demangle_component *third;
3668 if (code == NULL)
3669 return NULL;
3670 else if (!strcmp (code, "qu")
3671 || !strcmp (code, "dX"))
3673 /* ?: expression. */
3674 first = d_expression_1 (di);
3675 second = d_expression_1 (di);
3676 third = d_expression_1 (di);
3677 if (third == NULL)
3678 return NULL;
3680 else if (code[0] == 'f')
3682 /* fold-expression. */
3683 first = d_operator_name (di);
3684 second = d_expression_1 (di);
3685 third = d_expression_1 (di);
3686 if (third == NULL)
3687 return NULL;
3689 else if (code[0] == 'n')
3691 /* new-expression. */
3692 if (code[1] != 'w' && code[1] != 'a')
3693 return NULL;
3694 first = d_exprlist (di, '_');
3695 second = cplus_demangle_type (di);
3696 if (d_peek_char (di) == 'E')
3698 d_advance (di, 1);
3699 third = NULL;
3701 else if (d_peek_char (di) == 'p'
3702 && d_peek_next_char (di) == 'i')
3704 /* Parenthesized initializer. */
3705 d_advance (di, 2);
3706 third = d_exprlist (di, 'E');
3708 else if (d_peek_char (di) == 'i'
3709 && d_peek_next_char (di) == 'l')
3710 /* initializer-list. */
3711 third = d_expression_1 (di);
3712 else
3713 return NULL;
3715 else
3716 return NULL;
3717 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3718 d_make_comp (di,
3719 DEMANGLE_COMPONENT_TRINARY_ARG1,
3720 first,
3721 d_make_comp (di,
3722 DEMANGLE_COMPONENT_TRINARY_ARG2,
3723 second, third)));
3725 default:
3726 return NULL;
3731 static struct demangle_component *
3732 d_expression (struct d_info *di)
3734 struct demangle_component *ret;
3735 int was_expression = di->is_expression;
3737 di->is_expression = 1;
3738 ret = d_expression_1 (di);
3739 di->is_expression = was_expression;
3740 return ret;
3743 /* <expr-primary> ::= L <type> <(value) number> E
3744 ::= L <type> <(value) float> E
3745 ::= L <mangled-name> E
3748 static struct demangle_component *
3749 d_expr_primary (struct d_info *di)
3751 struct demangle_component *ret;
3753 if (! d_check_char (di, 'L'))
3754 return NULL;
3755 if (d_peek_char (di) == '_'
3756 /* Workaround for G++ bug; see comment in write_template_arg. */
3757 || d_peek_char (di) == 'Z')
3758 ret = cplus_demangle_mangled_name (di, 0);
3759 else
3761 struct demangle_component *type;
3762 enum demangle_component_type t;
3763 const char *s;
3765 type = cplus_demangle_type (di);
3766 if (type == NULL)
3767 return NULL;
3769 /* If we have a type we know how to print, we aren't going to
3770 print the type name itself. */
3771 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3772 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3773 di->expansion -= type->u.s_builtin.type->len;
3775 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3776 && strcmp (type->u.s_builtin.type->name,
3777 cplus_demangle_builtin_types[33].name) == 0)
3779 if (d_peek_char (di) == 'E')
3781 d_advance (di, 1);
3782 return type;
3786 /* Rather than try to interpret the literal value, we just
3787 collect it as a string. Note that it's possible to have a
3788 floating point literal here. The ABI specifies that the
3789 format of such literals is machine independent. That's fine,
3790 but what's not fine is that versions of g++ up to 3.2 with
3791 -fabi-version=1 used upper case letters in the hex constant,
3792 and dumped out gcc's internal representation. That makes it
3793 hard to tell where the constant ends, and hard to dump the
3794 constant in any readable form anyhow. We don't attempt to
3795 handle these cases. */
3797 t = DEMANGLE_COMPONENT_LITERAL;
3798 if (d_peek_char (di) == 'n')
3800 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3801 d_advance (di, 1);
3803 s = d_str (di);
3804 while (d_peek_char (di) != 'E')
3806 if (d_peek_char (di) == '\0')
3807 return NULL;
3808 d_advance (di, 1);
3810 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3812 if (! d_check_char (di, 'E'))
3813 return NULL;
3814 return ret;
3817 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3818 ::= Z <(function) encoding> E s [<discriminator>]
3819 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3822 static struct demangle_component *
3823 d_local_name (struct d_info *di)
3825 struct demangle_component *function;
3826 struct demangle_component *name;
3828 if (! d_check_char (di, 'Z'))
3829 return NULL;
3831 function = d_encoding (di, 0);
3832 if (!function)
3833 return NULL;
3835 if (! d_check_char (di, 'E'))
3836 return NULL;
3838 if (d_peek_char (di) == 's')
3840 d_advance (di, 1);
3841 if (! d_discriminator (di))
3842 return NULL;
3843 name = d_make_name (di, "string literal", sizeof "string literal" - 1);
3845 else
3847 int num = -1;
3849 if (d_peek_char (di) == 'd')
3851 /* Default argument scope: d <number> _. */
3852 d_advance (di, 1);
3853 num = d_compact_number (di);
3854 if (num < 0)
3855 return NULL;
3858 name = d_name (di, 0);
3860 if (name
3861 /* Lambdas and unnamed types have internal discriminators
3862 and are not functions. */
3863 && name->type != DEMANGLE_COMPONENT_LAMBDA
3864 && name->type != DEMANGLE_COMPONENT_UNNAMED_TYPE)
3866 /* Read and ignore an optional discriminator. */
3867 if (! d_discriminator (di))
3868 return NULL;
3871 if (num >= 0)
3872 name = d_make_default_arg (di, num, name);
3875 /* Elide the return type of the containing function so as to not
3876 confuse the user thinking it is the return type of whatever local
3877 function we might be containing. */
3878 if (function->type == DEMANGLE_COMPONENT_TYPED_NAME
3879 && d_right (function)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3880 d_left (d_right (function)) = NULL;
3882 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3885 /* <discriminator> ::= _ <number> # when number < 10
3886 ::= __ <number> _ # when number >= 10
3888 <discriminator> ::= _ <number> # when number >=10
3889 is also accepted to support gcc versions that wrongly mangled that way.
3891 We demangle the discriminator, but we don't print it out. FIXME:
3892 We should print it out in verbose mode. */
3894 static int
3895 d_discriminator (struct d_info *di)
3897 int discrim, num_underscores = 1;
3899 if (d_peek_char (di) != '_')
3900 return 1;
3901 d_advance (di, 1);
3902 if (d_peek_char (di) == '_')
3904 ++num_underscores;
3905 d_advance (di, 1);
3908 discrim = d_number (di);
3909 if (discrim < 0)
3910 return 0;
3911 if (num_underscores > 1 && discrim >= 10)
3913 if (d_peek_char (di) == '_')
3914 d_advance (di, 1);
3915 else
3916 return 0;
3919 return 1;
3922 /* <template-parm> ::= Ty
3923 ::= Tn <type>
3924 ::= Tt <template-head> E
3925 ::= Tp <template-parm> */
3927 static struct demangle_component *
3928 d_template_parm (struct d_info *di, int *bad)
3930 if (d_peek_char (di) != 'T')
3931 return NULL;
3933 struct demangle_component *op;
3934 enum demangle_component_type kind;
3935 switch (d_peek_next_char (di))
3937 default:
3938 return NULL;
3940 case 'p': /* Pack */
3941 d_advance (di, 2);
3942 op = d_template_parm (di, bad);
3943 kind = DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM;
3944 if (!op)
3946 *bad = 1;
3947 return NULL;
3949 break;
3951 case 'y': /* Typename */
3952 d_advance (di, 2);
3953 op = NULL;
3954 kind = DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM;
3955 break;
3957 case 'n': /* Non-Type */
3958 d_advance (di, 2);
3959 op = cplus_demangle_type (di);
3960 kind = DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM;
3961 if (!op)
3963 *bad = 1;
3964 return NULL;
3966 break;
3968 case 't': /* Template */
3969 d_advance (di, 2);
3970 op = d_template_head (di, bad);
3971 kind = DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM;
3972 if (!op || !d_check_char (di, 'E'))
3974 *bad = 1;
3975 return NULL;
3979 return d_make_comp (di, kind, op, NULL);
3982 /* <template-head> ::= <template-head>? <template-parm> */
3984 static struct demangle_component *
3985 d_template_head (struct d_info *di, int *bad)
3987 struct demangle_component *res = NULL, **slot = &res;
3988 struct demangle_component *op;
3990 while ((op = d_template_parm (di, bad)))
3992 *slot = op;
3993 slot = &d_right (op);
3996 /* Wrap it in a template head, to make concatenating with any parm list, and
3997 printing simpler. */
3998 if (res)
3999 res = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_HEAD, res, NULL);
4001 return res;
4004 /* <closure-type-name> ::= Ul <template-head>? <lambda-sig> E [ <nonnegative number> ] _ */
4006 static struct demangle_component *
4007 d_lambda (struct d_info *di)
4009 if (! d_check_char (di, 'U'))
4010 return NULL;
4011 if (! d_check_char (di, 'l'))
4012 return NULL;
4014 int bad = 0;
4015 struct demangle_component *head = d_template_head (di, &bad);
4016 if (bad)
4017 return NULL;
4019 struct demangle_component *tl = d_parmlist (di);
4020 if (tl == NULL)
4021 return NULL;
4022 if (head)
4024 d_right (head) = tl;
4025 tl = head;
4028 if (! d_check_char (di, 'E'))
4029 return NULL;
4031 int num = d_compact_number (di);
4032 if (num < 0)
4033 return NULL;
4035 struct demangle_component *ret = d_make_empty (di);
4036 if (ret)
4038 ret->type = DEMANGLE_COMPONENT_LAMBDA;
4039 ret->u.s_unary_num.sub = tl;
4040 ret->u.s_unary_num.num = num;
4043 return ret;
4046 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
4048 static struct demangle_component *
4049 d_unnamed_type (struct d_info *di)
4051 struct demangle_component *ret;
4052 int num;
4054 if (! d_check_char (di, 'U'))
4055 return NULL;
4056 if (! d_check_char (di, 't'))
4057 return NULL;
4059 num = d_compact_number (di);
4060 if (num < 0)
4061 return NULL;
4063 ret = d_make_empty (di);
4064 if (ret)
4066 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
4067 ret->u.s_number.number = num;
4070 if (! d_add_substitution (di, ret))
4071 return NULL;
4073 return ret;
4076 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
4079 static struct demangle_component *
4080 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
4082 const char *suffix = d_str (di);
4083 const char *pend = suffix;
4084 struct demangle_component *n;
4086 if (*pend == '.' && (IS_LOWER (pend[1]) || IS_DIGIT (pend[1])
4087 || pend[1] == '_'))
4089 pend += 2;
4090 while (IS_LOWER (*pend) || IS_DIGIT (*pend) || *pend == '_')
4091 ++pend;
4093 while (*pend == '.' && IS_DIGIT (pend[1]))
4095 pend += 2;
4096 while (IS_DIGIT (*pend))
4097 ++pend;
4099 d_advance (di, pend - suffix);
4100 n = d_make_name (di, suffix, pend - suffix);
4101 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
4104 /* Add a new substitution. */
4106 static int
4107 d_add_substitution (struct d_info *di, struct demangle_component *dc)
4109 if (dc == NULL)
4110 return 0;
4111 if (di->next_sub >= di->num_subs)
4112 return 0;
4113 di->subs[di->next_sub] = dc;
4114 ++di->next_sub;
4115 return 1;
4118 /* <substitution> ::= S <seq-id> _
4119 ::= S_
4120 ::= St
4121 ::= Sa
4122 ::= Sb
4123 ::= Ss
4124 ::= Si
4125 ::= So
4126 ::= Sd
4128 If PREFIX is non-zero, then this type is being used as a prefix in
4129 a qualified name. In this case, for the standard substitutions, we
4130 need to check whether we are being used as a prefix for a
4131 constructor or destructor, and return a full template name.
4132 Otherwise we will get something like std::iostream::~iostream()
4133 which does not correspond particularly well to any function which
4134 actually appears in the source.
4137 static const struct d_standard_sub_info standard_subs[] =
4139 { 't', NL ("std"),
4140 NL ("std"),
4141 NULL, 0 },
4142 { 'a', NL ("std::allocator"),
4143 NL ("std::allocator"),
4144 NL ("allocator") },
4145 { 'b', NL ("std::basic_string"),
4146 NL ("std::basic_string"),
4147 NL ("basic_string") },
4148 { 's', NL ("std::string"),
4149 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
4150 NL ("basic_string") },
4151 { 'i', NL ("std::istream"),
4152 NL ("std::basic_istream<char, std::char_traits<char> >"),
4153 NL ("basic_istream") },
4154 { 'o', NL ("std::ostream"),
4155 NL ("std::basic_ostream<char, std::char_traits<char> >"),
4156 NL ("basic_ostream") },
4157 { 'd', NL ("std::iostream"),
4158 NL ("std::basic_iostream<char, std::char_traits<char> >"),
4159 NL ("basic_iostream") }
4162 static struct demangle_component *
4163 d_substitution (struct d_info *di, int prefix)
4165 char c;
4167 if (! d_check_char (di, 'S'))
4168 return NULL;
4170 c = d_next_char (di);
4171 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
4173 unsigned int id;
4175 id = 0;
4176 if (c != '_')
4180 unsigned int new_id;
4182 if (IS_DIGIT (c))
4183 new_id = id * 36 + c - '0';
4184 else if (IS_UPPER (c))
4185 new_id = id * 36 + c - 'A' + 10;
4186 else
4187 return NULL;
4188 if (new_id < id)
4189 return NULL;
4190 id = new_id;
4191 c = d_next_char (di);
4193 while (c != '_');
4195 ++id;
4198 if (id >= (unsigned int) di->next_sub)
4199 return NULL;
4201 return di->subs[id];
4203 else
4205 int verbose;
4206 const struct d_standard_sub_info *p;
4207 const struct d_standard_sub_info *pend;
4209 verbose = (di->options & DMGL_VERBOSE) != 0;
4210 if (! verbose && prefix)
4212 char peek;
4214 peek = d_peek_char (di);
4215 if (peek == 'C' || peek == 'D')
4216 verbose = 1;
4219 pend = (&standard_subs[0]
4220 + sizeof standard_subs / sizeof standard_subs[0]);
4221 for (p = &standard_subs[0]; p < pend; ++p)
4223 if (c == p->code)
4225 const char *s;
4226 int len;
4227 struct demangle_component *dc;
4229 if (p->set_last_name != NULL)
4230 di->last_name = d_make_sub (di, p->set_last_name,
4231 p->set_last_name_len);
4232 if (verbose)
4234 s = p->full_expansion;
4235 len = p->full_len;
4237 else
4239 s = p->simple_expansion;
4240 len = p->simple_len;
4242 di->expansion += len;
4243 dc = d_make_sub (di, s, len);
4244 if (d_peek_char (di) == 'B')
4246 /* If there are ABI tags on the abbreviation, it becomes
4247 a substitution candidate. */
4248 dc = d_abi_tags (di, dc);
4249 if (! d_add_substitution (di, dc))
4250 return NULL;
4252 return dc;
4256 return NULL;
4260 static void
4261 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
4263 checkpoint->n = di->n;
4264 checkpoint->next_comp = di->next_comp;
4265 checkpoint->next_sub = di->next_sub;
4266 checkpoint->expansion = di->expansion;
4269 static void
4270 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
4272 di->n = checkpoint->n;
4273 di->next_comp = checkpoint->next_comp;
4274 di->next_sub = checkpoint->next_sub;
4275 di->expansion = checkpoint->expansion;
4278 /* Initialize a growable string. */
4280 static void
4281 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
4283 dgs->buf = NULL;
4284 dgs->len = 0;
4285 dgs->alc = 0;
4286 dgs->allocation_failure = 0;
4288 if (estimate > 0)
4289 d_growable_string_resize (dgs, estimate);
4292 /* Grow a growable string to a given size. */
4294 static inline void
4295 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
4297 size_t newalc;
4298 char *newbuf;
4300 if (dgs->allocation_failure)
4301 return;
4303 /* Start allocation at two bytes to avoid any possibility of confusion
4304 with the special value of 1 used as a return in *palc to indicate
4305 allocation failures. */
4306 newalc = dgs->alc > 0 ? dgs->alc : 2;
4307 while (newalc < need)
4308 newalc <<= 1;
4310 newbuf = (char *) realloc (dgs->buf, newalc);
4311 if (newbuf == NULL)
4313 free (dgs->buf);
4314 dgs->buf = NULL;
4315 dgs->len = 0;
4316 dgs->alc = 0;
4317 dgs->allocation_failure = 1;
4318 return;
4320 dgs->buf = newbuf;
4321 dgs->alc = newalc;
4324 /* Append a buffer to a growable string. */
4326 static inline void
4327 d_growable_string_append_buffer (struct d_growable_string *dgs,
4328 const char *s, size_t l)
4330 size_t need;
4332 need = dgs->len + l + 1;
4333 if (need > dgs->alc)
4334 d_growable_string_resize (dgs, need);
4336 if (dgs->allocation_failure)
4337 return;
4339 memcpy (dgs->buf + dgs->len, s, l);
4340 dgs->buf[dgs->len + l] = '\0';
4341 dgs->len += l;
4344 /* Bridge growable strings to the callback mechanism. */
4346 static void
4347 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
4349 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
4351 d_growable_string_append_buffer (dgs, s, l);
4354 /* Walk the tree, counting the number of templates encountered, and
4355 the number of times a scope might be saved. These counts will be
4356 used to allocate data structures for d_print_comp, so the logic
4357 here must mirror the logic d_print_comp will use. It is not
4358 important that the resulting numbers are exact, so long as they
4359 are larger than the actual numbers encountered. */
4361 static void
4362 d_count_templates_scopes (struct d_print_info *dpi,
4363 struct demangle_component *dc)
4365 if (dc == NULL || dc->d_counting > 1 || dpi->recursion > MAX_RECURSION_COUNT)
4366 return;
4368 ++ dc->d_counting;
4370 switch (dc->type)
4372 case DEMANGLE_COMPONENT_NAME:
4373 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4374 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4375 case DEMANGLE_COMPONENT_SUB_STD:
4376 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4377 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4378 case DEMANGLE_COMPONENT_OPERATOR:
4379 case DEMANGLE_COMPONENT_CHARACTER:
4380 case DEMANGLE_COMPONENT_NUMBER:
4381 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4382 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
4383 case DEMANGLE_COMPONENT_MODULE_NAME:
4384 case DEMANGLE_COMPONENT_MODULE_PARTITION:
4385 case DEMANGLE_COMPONENT_MODULE_INIT:
4386 case DEMANGLE_COMPONENT_FIXED_TYPE:
4387 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
4388 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
4389 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
4390 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
4391 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
4392 break;
4394 case DEMANGLE_COMPONENT_TEMPLATE:
4395 dpi->num_copy_templates++;
4396 goto recurse_left_right;
4398 case DEMANGLE_COMPONENT_REFERENCE:
4399 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4400 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4401 dpi->num_saved_scopes++;
4402 goto recurse_left_right;
4404 case DEMANGLE_COMPONENT_QUAL_NAME:
4405 case DEMANGLE_COMPONENT_LOCAL_NAME:
4406 case DEMANGLE_COMPONENT_TYPED_NAME:
4407 case DEMANGLE_COMPONENT_VTABLE:
4408 case DEMANGLE_COMPONENT_VTT:
4409 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4410 case DEMANGLE_COMPONENT_TYPEINFO:
4411 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4412 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4413 case DEMANGLE_COMPONENT_THUNK:
4414 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4415 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4416 case DEMANGLE_COMPONENT_JAVA_CLASS:
4417 case DEMANGLE_COMPONENT_GUARD:
4418 case DEMANGLE_COMPONENT_TLS_INIT:
4419 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4420 case DEMANGLE_COMPONENT_REFTEMP:
4421 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4422 case DEMANGLE_COMPONENT_RESTRICT:
4423 case DEMANGLE_COMPONENT_VOLATILE:
4424 case DEMANGLE_COMPONENT_CONST:
4425 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4426 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4427 case DEMANGLE_COMPONENT_CONST_THIS:
4428 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4429 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4430 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4431 case DEMANGLE_COMPONENT_NOEXCEPT:
4432 case DEMANGLE_COMPONENT_THROW_SPEC:
4433 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4434 case DEMANGLE_COMPONENT_POINTER:
4435 case DEMANGLE_COMPONENT_COMPLEX:
4436 case DEMANGLE_COMPONENT_IMAGINARY:
4437 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4438 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4439 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4440 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4441 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4442 case DEMANGLE_COMPONENT_ARGLIST:
4443 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4444 case DEMANGLE_COMPONENT_TPARM_OBJ:
4445 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4446 case DEMANGLE_COMPONENT_CAST:
4447 case DEMANGLE_COMPONENT_CONVERSION:
4448 case DEMANGLE_COMPONENT_NULLARY:
4449 case DEMANGLE_COMPONENT_UNARY:
4450 case DEMANGLE_COMPONENT_BINARY:
4451 case DEMANGLE_COMPONENT_BINARY_ARGS:
4452 case DEMANGLE_COMPONENT_TRINARY:
4453 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4454 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4455 case DEMANGLE_COMPONENT_LITERAL:
4456 case DEMANGLE_COMPONENT_LITERAL_NEG:
4457 case DEMANGLE_COMPONENT_VENDOR_EXPR:
4458 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4459 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4460 case DEMANGLE_COMPONENT_DECLTYPE:
4461 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4462 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4463 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4464 case DEMANGLE_COMPONENT_TAGGED_NAME:
4465 case DEMANGLE_COMPONENT_CLONE:
4466 case DEMANGLE_COMPONENT_CONSTRAINTS:
4467 recurse_left_right:
4468 /* PR 89394 - Check for too much recursion. */
4469 if (dpi->recursion > DEMANGLE_RECURSION_LIMIT)
4470 /* FIXME: There ought to be a way to report to the
4471 user that the recursion limit has been reached. */
4472 return;
4474 ++ dpi->recursion;
4475 d_count_templates_scopes (dpi, d_left (dc));
4476 d_count_templates_scopes (dpi, d_right (dc));
4477 -- dpi->recursion;
4478 break;
4480 case DEMANGLE_COMPONENT_CTOR:
4481 d_count_templates_scopes (dpi, dc->u.s_ctor.name);
4482 break;
4484 case DEMANGLE_COMPONENT_DTOR:
4485 d_count_templates_scopes (dpi, dc->u.s_dtor.name);
4486 break;
4488 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4489 d_count_templates_scopes (dpi, dc->u.s_extended_operator.name);
4490 break;
4492 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4493 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4494 case DEMANGLE_COMPONENT_MODULE_ENTITY:
4495 case DEMANGLE_COMPONENT_FRIEND:
4496 d_count_templates_scopes (dpi, d_left (dc));
4497 break;
4499 case DEMANGLE_COMPONENT_LAMBDA:
4500 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4501 d_count_templates_scopes (dpi, dc->u.s_unary_num.sub);
4502 break;
4506 /* Initialize a print information structure. */
4508 static void
4509 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4510 void *opaque, struct demangle_component *dc)
4512 dpi->len = 0;
4513 dpi->last_char = '\0';
4514 dpi->templates = NULL;
4515 dpi->modifiers = NULL;
4516 dpi->pack_index = 0;
4517 dpi->flush_count = 0;
4519 dpi->callback = callback;
4520 dpi->opaque = opaque;
4522 dpi->demangle_failure = 0;
4523 dpi->recursion = 0;
4524 dpi->lambda_tpl_parms = 0;
4526 dpi->component_stack = NULL;
4528 dpi->saved_scopes = NULL;
4529 dpi->next_saved_scope = 0;
4530 dpi->num_saved_scopes = 0;
4532 dpi->copy_templates = NULL;
4533 dpi->next_copy_template = 0;
4534 dpi->num_copy_templates = 0;
4536 d_count_templates_scopes (dpi, dc);
4537 /* If we did not reach the recursion limit, then reset the
4538 current recursion value back to 0, so that we can print
4539 the templates. */
4540 if (dpi->recursion < DEMANGLE_RECURSION_LIMIT)
4541 dpi->recursion = 0;
4542 dpi->num_copy_templates *= dpi->num_saved_scopes;
4544 dpi->current_template = NULL;
4547 /* Indicate that an error occurred during printing, and test for error. */
4549 static inline void
4550 d_print_error (struct d_print_info *dpi)
4552 dpi->demangle_failure = 1;
4555 static inline int
4556 d_print_saw_error (struct d_print_info *dpi)
4558 return dpi->demangle_failure != 0;
4561 /* Flush buffered characters to the callback. */
4563 static inline void
4564 d_print_flush (struct d_print_info *dpi)
4566 dpi->buf[dpi->len] = '\0';
4567 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4568 dpi->len = 0;
4569 dpi->flush_count++;
4572 /* Append characters and buffers for printing. */
4574 static inline void
4575 d_append_char (struct d_print_info *dpi, char c)
4577 if (dpi->len == sizeof (dpi->buf) - 1)
4578 d_print_flush (dpi);
4580 dpi->buf[dpi->len++] = c;
4581 dpi->last_char = c;
4584 static inline void
4585 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4587 size_t i;
4589 for (i = 0; i < l; i++)
4590 d_append_char (dpi, s[i]);
4593 static inline void
4594 d_append_string (struct d_print_info *dpi, const char *s)
4596 d_append_buffer (dpi, s, strlen (s));
4599 static inline void
4600 d_append_num (struct d_print_info *dpi, int l)
4602 char buf[25];
4603 sprintf (buf,"%d", l);
4604 d_append_string (dpi, buf);
4607 static inline char
4608 d_last_char (struct d_print_info *dpi)
4610 return dpi->last_char;
4613 /* Turn components into a human readable string. OPTIONS is the
4614 options bits passed to the demangler. DC is the tree to print.
4615 CALLBACK is a function to call to flush demangled string segments
4616 as they fill the intermediate buffer, and OPAQUE is a generalized
4617 callback argument. On success, this returns 1. On failure,
4618 it returns 0, indicating a bad parse. It does not use heap
4619 memory to build an output string, so cannot encounter memory
4620 allocation failure. */
4622 CP_STATIC_IF_GLIBCPP_V3
4624 cplus_demangle_print_callback (int options,
4625 struct demangle_component *dc,
4626 demangle_callbackref callback, void *opaque)
4628 struct d_print_info dpi;
4630 d_print_init (&dpi, callback, opaque, dc);
4633 #ifdef CP_DYNAMIC_ARRAYS
4634 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4635 and flagged as errors by Address Sanitizer. */
4636 __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
4637 ? dpi.num_saved_scopes : 1];
4638 __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
4639 ? dpi.num_copy_templates : 1];
4641 dpi.saved_scopes = scopes;
4642 dpi.copy_templates = temps;
4643 #else
4644 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4645 * sizeof (*dpi.saved_scopes));
4646 dpi.copy_templates = alloca (dpi.num_copy_templates
4647 * sizeof (*dpi.copy_templates));
4648 #endif
4650 d_print_comp (&dpi, options, dc);
4653 d_print_flush (&dpi);
4655 return ! d_print_saw_error (&dpi);
4658 /* Turn components into a human readable string. OPTIONS is the
4659 options bits passed to the demangler. DC is the tree to print.
4660 ESTIMATE is a guess at the length of the result. This returns a
4661 string allocated by malloc, or NULL on error. On success, this
4662 sets *PALC to the size of the allocated buffer. On failure, this
4663 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4664 failure. */
4666 CP_STATIC_IF_GLIBCPP_V3
4667 char *
4668 cplus_demangle_print (int options, struct demangle_component *dc,
4669 int estimate, size_t *palc)
4671 struct d_growable_string dgs;
4673 d_growable_string_init (&dgs, estimate);
4675 if (! cplus_demangle_print_callback (options, dc,
4676 d_growable_string_callback_adapter,
4677 &dgs))
4679 free (dgs.buf);
4680 *palc = 0;
4681 return NULL;
4684 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4685 return dgs.buf;
4688 /* Returns the I'th element of the template arglist ARGS, or NULL on
4689 failure. If I is negative, return the entire arglist. */
4691 static struct demangle_component *
4692 d_index_template_argument (struct demangle_component *args, int i)
4694 struct demangle_component *a;
4696 if (i < 0)
4697 /* Print the whole argument pack. */
4698 return args;
4700 for (a = args;
4701 a != NULL;
4702 a = d_right (a))
4704 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4705 return NULL;
4706 if (i <= 0)
4707 break;
4708 --i;
4710 if (i != 0 || a == NULL)
4711 return NULL;
4713 return d_left (a);
4716 /* Returns the template argument from the current context indicated by DC,
4717 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4719 static struct demangle_component *
4720 d_lookup_template_argument (struct d_print_info *dpi,
4721 const struct demangle_component *dc)
4723 if (dpi->templates == NULL)
4725 d_print_error (dpi);
4726 return NULL;
4729 return d_index_template_argument
4730 (d_right (dpi->templates->template_decl),
4731 dc->u.s_number.number);
4734 /* Returns a template argument pack used in DC (any will do), or NULL. */
4736 static struct demangle_component *
4737 d_find_pack (struct d_print_info *dpi,
4738 const struct demangle_component *dc)
4740 struct demangle_component *a;
4741 if (dc == NULL)
4742 return NULL;
4744 switch (dc->type)
4746 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4747 a = d_lookup_template_argument (dpi, dc);
4748 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4749 return a;
4750 return NULL;
4752 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4753 return NULL;
4755 case DEMANGLE_COMPONENT_LAMBDA:
4756 case DEMANGLE_COMPONENT_NAME:
4757 case DEMANGLE_COMPONENT_TAGGED_NAME:
4758 case DEMANGLE_COMPONENT_OPERATOR:
4759 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4760 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
4761 case DEMANGLE_COMPONENT_SUB_STD:
4762 case DEMANGLE_COMPONENT_CHARACTER:
4763 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4764 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4765 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4766 case DEMANGLE_COMPONENT_NUMBER:
4767 return NULL;
4769 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4770 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4771 case DEMANGLE_COMPONENT_CTOR:
4772 return d_find_pack (dpi, dc->u.s_ctor.name);
4773 case DEMANGLE_COMPONENT_DTOR:
4774 return d_find_pack (dpi, dc->u.s_dtor.name);
4776 default:
4777 a = d_find_pack (dpi, d_left (dc));
4778 if (a)
4779 return a;
4780 return d_find_pack (dpi, d_right (dc));
4784 /* Returns the length of the template argument pack DC. */
4786 static int
4787 d_pack_length (const struct demangle_component *dc)
4789 int count = 0;
4790 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4791 && d_left (dc) != NULL)
4793 ++count;
4794 dc = d_right (dc);
4796 return count;
4799 /* Returns the number of template args in DC, expanding any pack expansions
4800 found there. */
4802 static int
4803 d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
4805 int count = 0;
4806 for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4807 dc = d_right (dc))
4809 struct demangle_component *elt = d_left (dc);
4810 if (elt == NULL)
4811 break;
4812 if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
4814 struct demangle_component *a = d_find_pack (dpi, d_left (elt));
4815 count += d_pack_length (a);
4817 else
4818 ++count;
4820 return count;
4823 /* DC is a component of a mangled expression. Print it, wrapped in parens
4824 if needed. */
4826 static void
4827 d_print_subexpr (struct d_print_info *dpi, int options,
4828 struct demangle_component *dc)
4830 int simple = 0;
4831 if (dc->type == DEMANGLE_COMPONENT_NAME
4832 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4833 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4834 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4835 simple = 1;
4836 if (!simple)
4837 d_append_char (dpi, '(');
4838 d_print_comp (dpi, options, dc);
4839 if (!simple)
4840 d_append_char (dpi, ')');
4843 /* Save the current scope. */
4845 static void
4846 d_save_scope (struct d_print_info *dpi,
4847 const struct demangle_component *container)
4849 struct d_saved_scope *scope;
4850 struct d_print_template *src, **link;
4852 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4854 d_print_error (dpi);
4855 return;
4857 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4858 dpi->next_saved_scope++;
4860 scope->container = container;
4861 link = &scope->templates;
4863 for (src = dpi->templates; src != NULL; src = src->next)
4865 struct d_print_template *dst;
4867 if (dpi->next_copy_template >= dpi->num_copy_templates)
4869 d_print_error (dpi);
4870 return;
4872 dst = &dpi->copy_templates[dpi->next_copy_template];
4873 dpi->next_copy_template++;
4875 dst->template_decl = src->template_decl;
4876 *link = dst;
4877 link = &dst->next;
4880 *link = NULL;
4883 /* Attempt to locate a previously saved scope. Returns NULL if no
4884 corresponding saved scope was found. */
4886 static struct d_saved_scope *
4887 d_get_saved_scope (struct d_print_info *dpi,
4888 const struct demangle_component *container)
4890 int i;
4892 for (i = 0; i < dpi->next_saved_scope; i++)
4893 if (dpi->saved_scopes[i].container == container)
4894 return &dpi->saved_scopes[i];
4896 return NULL;
4899 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4900 return false. */
4902 static int
4903 d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
4904 struct demangle_component *dc)
4906 struct demangle_component *ops, *operator_, *op1, *op2;
4907 int save_idx;
4909 const char *fold_code = d_left (dc)->u.s_operator.op->code;
4910 if (fold_code[0] != 'f')
4911 return 0;
4913 ops = d_right (dc);
4914 operator_ = d_left (ops);
4915 op1 = d_right (ops);
4916 op2 = 0;
4917 if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
4919 op2 = d_right (op1);
4920 op1 = d_left (op1);
4923 /* Print the whole pack. */
4924 save_idx = dpi->pack_index;
4925 dpi->pack_index = -1;
4927 switch (fold_code[1])
4929 /* Unary left fold, (... + X). */
4930 case 'l':
4931 d_append_string (dpi, "(...");
4932 d_print_expr_op (dpi, options, operator_);
4933 d_print_subexpr (dpi, options, op1);
4934 d_append_char (dpi, ')');
4935 break;
4937 /* Unary right fold, (X + ...). */
4938 case 'r':
4939 d_append_char (dpi, '(');
4940 d_print_subexpr (dpi, options, op1);
4941 d_print_expr_op (dpi, options, operator_);
4942 d_append_string (dpi, "...)");
4943 break;
4945 /* Binary left fold, (42 + ... + X). */
4946 case 'L':
4947 /* Binary right fold, (X + ... + 42). */
4948 case 'R':
4949 d_append_char (dpi, '(');
4950 d_print_subexpr (dpi, options, op1);
4951 d_print_expr_op (dpi, options, operator_);
4952 d_append_string (dpi, "...");
4953 d_print_expr_op (dpi, options, operator_);
4954 d_print_subexpr (dpi, options, op2);
4955 d_append_char (dpi, ')');
4956 break;
4959 dpi->pack_index = save_idx;
4960 return 1;
4963 /* True iff DC represents a C99-style designated initializer. */
4965 static int
4966 is_designated_init (struct demangle_component *dc)
4968 if (dc->type != DEMANGLE_COMPONENT_BINARY
4969 && dc->type != DEMANGLE_COMPONENT_TRINARY)
4970 return 0;
4972 struct demangle_component *op = d_left (dc);
4973 const char *code = op->u.s_operator.op->code;
4974 return (code[0] == 'd'
4975 && (code[1] == 'i' || code[1] == 'x' || code[1] == 'X'));
4978 /* If DC represents a C99-style designated initializer, print it and return
4979 true; otherwise, return false. */
4981 static int
4982 d_maybe_print_designated_init (struct d_print_info *dpi, int options,
4983 struct demangle_component *dc)
4985 if (!is_designated_init (dc))
4986 return 0;
4988 const char *code = d_left (dc)->u.s_operator.op->code;
4990 struct demangle_component *operands = d_right (dc);
4991 struct demangle_component *op1 = d_left (operands);
4992 struct demangle_component *op2 = d_right (operands);
4994 if (code[1] == 'i')
4995 d_append_char (dpi, '.');
4996 else
4997 d_append_char (dpi, '[');
4999 d_print_comp (dpi, options, op1);
5000 if (code[1] == 'X')
5002 d_append_string (dpi, " ... ");
5003 d_print_comp (dpi, options, d_left (op2));
5004 op2 = d_right (op2);
5006 if (code[1] != 'i')
5007 d_append_char (dpi, ']');
5008 if (is_designated_init (op2))
5010 /* Don't put '=' or '(' between chained designators. */
5011 d_print_comp (dpi, options, op2);
5013 else
5015 d_append_char (dpi, '=');
5016 d_print_subexpr (dpi, options, op2);
5018 return 1;
5021 static void
5022 d_print_lambda_parm_name (struct d_print_info *dpi, int type, unsigned index)
5024 const char *str;
5025 switch (type)
5027 default:
5028 dpi->demangle_failure = 1;
5029 str = "";
5030 break;
5032 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
5033 str = "$T";
5034 break;
5036 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
5037 str = "$N";
5038 break;
5040 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
5041 str = "$TT";
5042 break;
5044 d_append_string (dpi, str);
5045 d_append_num (dpi, index);
5048 /* Subroutine to handle components. */
5050 static void
5051 d_print_comp_inner (struct d_print_info *dpi, int options,
5052 struct demangle_component *dc)
5054 /* Magic variable to let reference smashing skip over the next modifier
5055 without needing to modify *dc. */
5056 struct demangle_component *mod_inner = NULL;
5058 /* Variable used to store the current templates while a previously
5059 captured scope is used. */
5060 struct d_print_template *saved_templates;
5062 /* Nonzero if templates have been stored in the above variable. */
5063 int need_template_restore = 0;
5065 if (dc == NULL)
5067 d_print_error (dpi);
5068 return;
5070 if (d_print_saw_error (dpi))
5071 return;
5073 switch (dc->type)
5075 case DEMANGLE_COMPONENT_NAME:
5076 if ((options & DMGL_JAVA) == 0)
5077 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
5078 else
5079 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
5080 return;
5082 case DEMANGLE_COMPONENT_TAGGED_NAME:
5083 d_print_comp (dpi, options, d_left (dc));
5084 d_append_string (dpi, "[abi:");
5085 d_print_comp (dpi, options, d_right (dc));
5086 d_append_char (dpi, ']');
5087 return;
5089 case DEMANGLE_COMPONENT_STRUCTURED_BINDING:
5090 d_append_char (dpi, '[');
5091 for (;;)
5093 d_print_comp (dpi, options, d_left (dc));
5094 dc = d_right (dc);
5095 if (!dc)
5096 break;
5097 d_append_string (dpi, ", ");
5099 d_append_char (dpi, ']');
5100 return;
5102 case DEMANGLE_COMPONENT_MODULE_ENTITY:
5103 d_print_comp (dpi, options, d_left (dc));
5104 d_append_char (dpi, '@');
5105 d_print_comp (dpi, options, d_right (dc));
5106 return;
5108 case DEMANGLE_COMPONENT_MODULE_NAME:
5109 case DEMANGLE_COMPONENT_MODULE_PARTITION:
5111 if (d_left (dc))
5112 d_print_comp (dpi, options, d_left (dc));
5113 char c = dc->type == DEMANGLE_COMPONENT_MODULE_PARTITION
5114 ? ':' : d_left (dc) ? '.' : 0;
5115 if (c)
5116 d_append_char (dpi, c);
5117 d_print_comp (dpi, options, d_right (dc));
5119 return;
5121 case DEMANGLE_COMPONENT_QUAL_NAME:
5122 case DEMANGLE_COMPONENT_LOCAL_NAME:
5123 d_print_comp (dpi, options, d_left (dc));
5124 if ((options & DMGL_JAVA) == 0)
5125 d_append_string (dpi, "::");
5126 else
5127 d_append_char (dpi, '.');
5129 struct demangle_component *local_name = d_right (dc);
5130 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5132 d_append_string (dpi, "{default arg#");
5133 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
5134 d_append_string (dpi, "}::");
5135 local_name = local_name->u.s_unary_num.sub;
5137 d_print_comp (dpi, options, local_name);
5139 return;
5141 case DEMANGLE_COMPONENT_TYPED_NAME:
5143 struct d_print_mod *hold_modifiers;
5144 struct demangle_component *typed_name;
5145 struct d_print_mod adpm[4];
5146 unsigned int i;
5147 struct d_print_template dpt;
5149 /* Pass the name down to the type so that it can be printed in
5150 the right place for the type. We also have to pass down
5151 any CV-qualifiers, which apply to the this parameter. */
5152 hold_modifiers = dpi->modifiers;
5153 dpi->modifiers = 0;
5154 i = 0;
5155 typed_name = d_left (dc);
5156 while (typed_name != NULL)
5158 if (i >= sizeof adpm / sizeof adpm[0])
5160 d_print_error (dpi);
5161 return;
5164 adpm[i].next = dpi->modifiers;
5165 dpi->modifiers = &adpm[i];
5166 adpm[i].mod = typed_name;
5167 adpm[i].printed = 0;
5168 adpm[i].templates = dpi->templates;
5169 ++i;
5171 if (!is_fnqual_component_type (typed_name->type))
5172 break;
5174 typed_name = d_left (typed_name);
5177 if (typed_name == NULL)
5179 d_print_error (dpi);
5180 return;
5183 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
5184 there may be CV-qualifiers on its right argument which
5185 really apply here; this happens when parsing a class that
5186 is local to a function. */
5187 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5189 typed_name = d_right (typed_name);
5190 if (typed_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5191 typed_name = typed_name->u.s_unary_num.sub;
5192 while (typed_name != NULL
5193 && is_fnqual_component_type (typed_name->type))
5195 if (i >= sizeof adpm / sizeof adpm[0])
5197 d_print_error (dpi);
5198 return;
5201 adpm[i] = adpm[i - 1];
5202 adpm[i].next = &adpm[i - 1];
5203 dpi->modifiers = &adpm[i];
5205 adpm[i - 1].mod = typed_name;
5206 adpm[i - 1].printed = 0;
5207 adpm[i - 1].templates = dpi->templates;
5208 ++i;
5210 typed_name = d_left (typed_name);
5212 if (typed_name == NULL)
5214 d_print_error (dpi);
5215 return;
5219 /* If typed_name is a template, then it applies to the
5220 function type as well. */
5221 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5223 dpt.next = dpi->templates;
5224 dpi->templates = &dpt;
5225 dpt.template_decl = typed_name;
5227 /* Constraints are mangled as part of the template argument list,
5228 so they wrap the _TEMPLATE_ARGLIST. But
5229 d_lookup_template_argument expects the RHS of _TEMPLATE to be
5230 the _ARGLIST, and constraints need to refer to these args. So
5231 move the _CONSTRAINTS out of the _TEMPLATE and onto the type.
5232 This will result in them being printed after the () like a
5233 trailing requires-clause, but that seems like our best option
5234 given that we aren't printing a template-head. */
5235 struct demangle_component *tnr = d_right (typed_name);
5236 if (tnr->type == DEMANGLE_COMPONENT_CONSTRAINTS)
5238 d_right (typed_name) = d_left (tnr);
5239 d_left (tnr) = d_right (dc);
5240 d_right (dc) = tnr;
5244 d_print_comp (dpi, options, d_right (dc));
5246 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
5247 dpi->templates = dpt.next;
5249 /* If the modifiers didn't get printed by the type, print them
5250 now. */
5251 while (i > 0)
5253 --i;
5254 if (! adpm[i].printed)
5256 d_append_char (dpi, ' ');
5257 d_print_mod (dpi, options, adpm[i].mod);
5261 dpi->modifiers = hold_modifiers;
5263 return;
5266 case DEMANGLE_COMPONENT_TEMPLATE:
5268 struct d_print_mod *hold_dpm;
5269 struct demangle_component *dcl;
5270 const struct demangle_component *hold_current;
5272 /* This template may need to be referenced by a cast operator
5273 contained in its subtree. */
5274 hold_current = dpi->current_template;
5275 dpi->current_template = dc;
5277 /* Don't push modifiers into a template definition. Doing so
5278 could give the wrong definition for a template argument.
5279 Instead, treat the template essentially as a name. */
5281 hold_dpm = dpi->modifiers;
5282 dpi->modifiers = NULL;
5284 dcl = d_left (dc);
5286 if ((options & DMGL_JAVA) != 0
5287 && dcl->type == DEMANGLE_COMPONENT_NAME
5288 && dcl->u.s_name.len == 6
5289 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
5291 /* Special-case Java arrays, so that JArray<TYPE> appears
5292 instead as TYPE[]. */
5294 d_print_comp (dpi, options, d_right (dc));
5295 d_append_string (dpi, "[]");
5297 else
5299 d_print_comp (dpi, options, dcl);
5300 if (d_last_char (dpi) == '<')
5301 d_append_char (dpi, ' ');
5302 d_append_char (dpi, '<');
5303 d_print_comp (dpi, options, d_right (dc));
5304 /* Avoid generating two consecutive '>' characters, to avoid
5305 the C++ syntactic ambiguity. */
5306 if (d_last_char (dpi) == '>')
5307 d_append_char (dpi, ' ');
5308 d_append_char (dpi, '>');
5311 dpi->modifiers = hold_dpm;
5312 dpi->current_template = hold_current;
5314 return;
5317 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
5318 if (dpi->lambda_tpl_parms > dc->u.s_number.number + 1)
5320 const struct demangle_component *a
5321 = d_left (dpi->templates->template_decl);
5322 unsigned c;
5323 for (c = dc->u.s_number.number; a && c; c--)
5324 a = d_right (a);
5325 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
5326 a = d_left (a);
5327 if (!a)
5328 dpi->demangle_failure = 1;
5329 else
5330 d_print_lambda_parm_name (dpi, a->type, dc->u.s_number.number);
5332 else if (dpi->lambda_tpl_parms)
5334 /* Show the template parm index, as that's how g++ displays
5335 these, and future proofs us against potential
5336 '[]<typename T> (T *a, T *b) {...}'. */
5337 d_append_buffer (dpi, "auto:", 5);
5338 d_append_num (dpi, dc->u.s_number.number + 1);
5340 else
5342 struct d_print_template *hold_dpt;
5343 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
5345 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5346 a = d_index_template_argument (a, dpi->pack_index);
5348 if (a == NULL)
5350 d_print_error (dpi);
5351 return;
5354 /* While processing this parameter, we need to pop the list
5355 of templates. This is because the template parameter may
5356 itself be a reference to a parameter of an outer
5357 template. */
5359 hold_dpt = dpi->templates;
5360 dpi->templates = hold_dpt->next;
5362 d_print_comp (dpi, options, a);
5364 dpi->templates = hold_dpt;
5366 return;
5368 case DEMANGLE_COMPONENT_TPARM_OBJ:
5369 d_append_string (dpi, "template parameter object for ");
5370 d_print_comp (dpi, options, d_left (dc));
5371 return;
5373 case DEMANGLE_COMPONENT_CTOR:
5374 d_print_comp (dpi, options, dc->u.s_ctor.name);
5375 return;
5377 case DEMANGLE_COMPONENT_DTOR:
5378 d_append_char (dpi, '~');
5379 d_print_comp (dpi, options, dc->u.s_dtor.name);
5380 return;
5382 case DEMANGLE_COMPONENT_MODULE_INIT:
5383 d_append_string (dpi, "initializer for module ");
5384 d_print_comp (dpi, options, d_left (dc));
5385 return;
5387 case DEMANGLE_COMPONENT_VTABLE:
5388 d_append_string (dpi, "vtable for ");
5389 d_print_comp (dpi, options, d_left (dc));
5390 return;
5392 case DEMANGLE_COMPONENT_VTT:
5393 d_append_string (dpi, "VTT for ");
5394 d_print_comp (dpi, options, d_left (dc));
5395 return;
5397 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
5398 d_append_string (dpi, "construction vtable for ");
5399 d_print_comp (dpi, options, d_left (dc));
5400 d_append_string (dpi, "-in-");
5401 d_print_comp (dpi, options, d_right (dc));
5402 return;
5404 case DEMANGLE_COMPONENT_TYPEINFO:
5405 d_append_string (dpi, "typeinfo for ");
5406 d_print_comp (dpi, options, d_left (dc));
5407 return;
5409 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
5410 d_append_string (dpi, "typeinfo name for ");
5411 d_print_comp (dpi, options, d_left (dc));
5412 return;
5414 case DEMANGLE_COMPONENT_TYPEINFO_FN:
5415 d_append_string (dpi, "typeinfo fn for ");
5416 d_print_comp (dpi, options, d_left (dc));
5417 return;
5419 case DEMANGLE_COMPONENT_THUNK:
5420 d_append_string (dpi, "non-virtual thunk to ");
5421 d_print_comp (dpi, options, d_left (dc));
5422 return;
5424 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
5425 d_append_string (dpi, "virtual thunk to ");
5426 d_print_comp (dpi, options, d_left (dc));
5427 return;
5429 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
5430 d_append_string (dpi, "covariant return thunk to ");
5431 d_print_comp (dpi, options, d_left (dc));
5432 return;
5434 case DEMANGLE_COMPONENT_JAVA_CLASS:
5435 d_append_string (dpi, "java Class for ");
5436 d_print_comp (dpi, options, d_left (dc));
5437 return;
5439 case DEMANGLE_COMPONENT_GUARD:
5440 d_append_string (dpi, "guard variable for ");
5441 d_print_comp (dpi, options, d_left (dc));
5442 return;
5444 case DEMANGLE_COMPONENT_TLS_INIT:
5445 d_append_string (dpi, "TLS init function for ");
5446 d_print_comp (dpi, options, d_left (dc));
5447 return;
5449 case DEMANGLE_COMPONENT_TLS_WRAPPER:
5450 d_append_string (dpi, "TLS wrapper function for ");
5451 d_print_comp (dpi, options, d_left (dc));
5452 return;
5454 case DEMANGLE_COMPONENT_REFTEMP:
5455 d_append_string (dpi, "reference temporary #");
5456 d_print_comp (dpi, options, d_right (dc));
5457 d_append_string (dpi, " for ");
5458 d_print_comp (dpi, options, d_left (dc));
5459 return;
5461 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
5462 d_append_string (dpi, "hidden alias for ");
5463 d_print_comp (dpi, options, d_left (dc));
5464 return;
5466 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
5467 d_append_string (dpi, "transaction clone for ");
5468 d_print_comp (dpi, options, d_left (dc));
5469 return;
5471 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
5472 d_append_string (dpi, "non-transaction clone for ");
5473 d_print_comp (dpi, options, d_left (dc));
5474 return;
5476 case DEMANGLE_COMPONENT_SUB_STD:
5477 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
5478 return;
5480 case DEMANGLE_COMPONENT_RESTRICT:
5481 case DEMANGLE_COMPONENT_VOLATILE:
5482 case DEMANGLE_COMPONENT_CONST:
5484 struct d_print_mod *pdpm;
5486 /* When printing arrays, it's possible to have cases where the
5487 same CV-qualifier gets pushed on the stack multiple times.
5488 We only need to print it once. */
5490 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
5492 if (! pdpm->printed)
5494 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
5495 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
5496 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
5497 break;
5498 if (pdpm->mod->type == dc->type)
5500 d_print_comp (dpi, options, d_left (dc));
5501 return;
5506 goto modifier;
5508 case DEMANGLE_COMPONENT_REFERENCE:
5509 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5511 /* Handle reference smashing: & + && = &. */
5512 struct demangle_component *sub = d_left (dc);
5513 if (!dpi->lambda_tpl_parms
5514 && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
5516 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
5517 struct demangle_component *a;
5519 if (scope == NULL)
5521 /* This is the first time SUB has been traversed.
5522 We need to capture the current templates so
5523 they can be restored if SUB is reentered as a
5524 substitution. */
5525 d_save_scope (dpi, sub);
5526 if (d_print_saw_error (dpi))
5527 return;
5529 else
5531 const struct d_component_stack *dcse;
5532 int found_self_or_parent = 0;
5534 /* This traversal is reentering SUB as a substition.
5535 If we are not beneath SUB or DC in the tree then we
5536 need to restore SUB's template stack temporarily. */
5537 for (dcse = dpi->component_stack; dcse != NULL;
5538 dcse = dcse->parent)
5540 if (dcse->dc == sub
5541 || (dcse->dc == dc
5542 && dcse != dpi->component_stack))
5544 found_self_or_parent = 1;
5545 break;
5549 if (!found_self_or_parent)
5551 saved_templates = dpi->templates;
5552 dpi->templates = scope->templates;
5553 need_template_restore = 1;
5557 a = d_lookup_template_argument (dpi, sub);
5558 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5559 a = d_index_template_argument (a, dpi->pack_index);
5561 if (a == NULL)
5563 if (need_template_restore)
5564 dpi->templates = saved_templates;
5566 d_print_error (dpi);
5567 return;
5570 sub = a;
5573 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
5574 || sub->type == dc->type)
5575 dc = sub;
5576 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
5577 mod_inner = d_left (sub);
5579 /* Fall through. */
5581 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5582 case DEMANGLE_COMPONENT_POINTER:
5583 case DEMANGLE_COMPONENT_COMPLEX:
5584 case DEMANGLE_COMPONENT_IMAGINARY:
5585 FNQUAL_COMPONENT_CASE:
5586 modifier:
5588 /* We keep a list of modifiers on the stack. */
5589 struct d_print_mod dpm;
5591 dpm.next = dpi->modifiers;
5592 dpi->modifiers = &dpm;
5593 dpm.mod = dc;
5594 dpm.printed = 0;
5595 dpm.templates = dpi->templates;
5597 if (!mod_inner)
5598 mod_inner = d_left (dc);
5600 d_print_comp (dpi, options, mod_inner);
5602 /* If the modifier didn't get printed by the type, print it
5603 now. */
5604 if (! dpm.printed)
5605 d_print_mod (dpi, options, dc);
5607 dpi->modifiers = dpm.next;
5609 if (need_template_restore)
5610 dpi->templates = saved_templates;
5612 return;
5615 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5616 if ((options & DMGL_JAVA) == 0)
5617 d_append_buffer (dpi, dc->u.s_builtin.type->name,
5618 dc->u.s_builtin.type->len);
5619 else
5620 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
5621 dc->u.s_builtin.type->java_len);
5622 return;
5624 case DEMANGLE_COMPONENT_EXTENDED_BUILTIN_TYPE:
5625 d_append_buffer (dpi, dc->u.s_extended_builtin.type->name,
5626 dc->u.s_extended_builtin.type->len);
5627 d_append_num (dpi, dc->u.s_extended_builtin.arg);
5628 if (dc->u.s_extended_builtin.suffix)
5629 d_append_buffer (dpi, &dc->u.s_extended_builtin.suffix, 1);
5630 return;
5632 case DEMANGLE_COMPONENT_VENDOR_TYPE:
5633 d_print_comp (dpi, options, d_left (dc));
5634 return;
5636 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5638 if ((options & DMGL_RET_POSTFIX) != 0)
5639 d_print_function_type (dpi,
5640 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5641 dc, dpi->modifiers);
5643 /* Print return type if present */
5644 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
5645 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5646 d_left (dc));
5647 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
5649 struct d_print_mod dpm;
5651 /* We must pass this type down as a modifier in order to
5652 print it in the right location. */
5653 dpm.next = dpi->modifiers;
5654 dpi->modifiers = &dpm;
5655 dpm.mod = dc;
5656 dpm.printed = 0;
5657 dpm.templates = dpi->templates;
5659 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5660 d_left (dc));
5662 dpi->modifiers = dpm.next;
5664 if (dpm.printed)
5665 return;
5667 /* In standard prefix notation, there is a space between the
5668 return type and the function signature. */
5669 if ((options & DMGL_RET_POSTFIX) == 0)
5670 d_append_char (dpi, ' ');
5673 if ((options & DMGL_RET_POSTFIX) == 0)
5674 d_print_function_type (dpi,
5675 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5676 dc, dpi->modifiers);
5678 return;
5681 case DEMANGLE_COMPONENT_ARRAY_TYPE:
5683 struct d_print_mod *hold_modifiers;
5684 struct d_print_mod adpm[4];
5685 unsigned int i;
5686 struct d_print_mod *pdpm;
5688 /* We must pass this type down as a modifier in order to print
5689 multi-dimensional arrays correctly. If the array itself is
5690 CV-qualified, we act as though the element type were
5691 CV-qualified. We do this by copying the modifiers down
5692 rather than fiddling pointers, so that we don't wind up
5693 with a d_print_mod higher on the stack pointing into our
5694 stack frame after we return. */
5696 hold_modifiers = dpi->modifiers;
5698 adpm[0].next = hold_modifiers;
5699 dpi->modifiers = &adpm[0];
5700 adpm[0].mod = dc;
5701 adpm[0].printed = 0;
5702 adpm[0].templates = dpi->templates;
5704 i = 1;
5705 pdpm = hold_modifiers;
5706 while (pdpm != NULL
5707 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
5708 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
5709 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
5711 if (! pdpm->printed)
5713 if (i >= sizeof adpm / sizeof adpm[0])
5715 d_print_error (dpi);
5716 return;
5719 adpm[i] = *pdpm;
5720 adpm[i].next = dpi->modifiers;
5721 dpi->modifiers = &adpm[i];
5722 pdpm->printed = 1;
5723 ++i;
5726 pdpm = pdpm->next;
5729 d_print_comp (dpi, options, d_right (dc));
5731 dpi->modifiers = hold_modifiers;
5733 if (adpm[0].printed)
5734 return;
5736 while (i > 1)
5738 --i;
5739 d_print_mod (dpi, options, adpm[i].mod);
5742 d_print_array_type (dpi, options, dc, dpi->modifiers);
5744 return;
5747 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5748 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5750 struct d_print_mod dpm;
5752 dpm.next = dpi->modifiers;
5753 dpi->modifiers = &dpm;
5754 dpm.mod = dc;
5755 dpm.printed = 0;
5756 dpm.templates = dpi->templates;
5758 d_print_comp (dpi, options, d_right (dc));
5760 /* If the modifier didn't get printed by the type, print it
5761 now. */
5762 if (! dpm.printed)
5763 d_print_mod (dpi, options, dc);
5765 dpi->modifiers = dpm.next;
5767 return;
5770 case DEMANGLE_COMPONENT_ARGLIST:
5771 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5772 if (d_left (dc) != NULL)
5773 d_print_comp (dpi, options, d_left (dc));
5774 if (d_right (dc) != NULL)
5776 size_t len;
5777 unsigned long int flush_count;
5778 /* Make sure ", " isn't flushed by d_append_string, otherwise
5779 dpi->len -= 2 wouldn't work. */
5780 if (dpi->len >= sizeof (dpi->buf) - 2)
5781 d_print_flush (dpi);
5782 d_append_string (dpi, ", ");
5783 len = dpi->len;
5784 flush_count = dpi->flush_count;
5785 d_print_comp (dpi, options, d_right (dc));
5786 /* If that didn't print anything (which can happen with empty
5787 template argument packs), remove the comma and space. */
5788 if (dpi->flush_count == flush_count && dpi->len == len)
5789 dpi->len -= 2;
5791 return;
5793 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5795 struct demangle_component *type = d_left (dc);
5796 struct demangle_component *list = d_right (dc);
5798 if (type)
5799 d_print_comp (dpi, options, type);
5800 d_append_char (dpi, '{');
5801 d_print_comp (dpi, options, list);
5802 d_append_char (dpi, '}');
5804 return;
5806 case DEMANGLE_COMPONENT_OPERATOR:
5808 const struct demangle_operator_info *op = dc->u.s_operator.op;
5809 int len = op->len;
5811 d_append_string (dpi, "operator");
5812 /* Add a space before new/delete. */
5813 if (IS_LOWER (op->name[0]))
5814 d_append_char (dpi, ' ');
5815 /* Omit a trailing space. */
5816 if (op->name[len-1] == ' ')
5817 --len;
5818 d_append_buffer (dpi, op->name, len);
5819 return;
5822 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5823 d_append_string (dpi, "operator ");
5824 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5825 return;
5827 case DEMANGLE_COMPONENT_CONVERSION:
5828 d_append_string (dpi, "operator ");
5829 d_print_conversion (dpi, options, dc);
5830 return;
5832 case DEMANGLE_COMPONENT_NULLARY:
5833 d_print_expr_op (dpi, options, d_left (dc));
5834 return;
5836 case DEMANGLE_COMPONENT_UNARY:
5838 struct demangle_component *op = d_left (dc);
5839 struct demangle_component *operand = d_right (dc);
5840 const char *code = NULL;
5842 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5844 code = op->u.s_operator.op->code;
5845 if (!strcmp (code, "ad"))
5847 /* Don't print the argument list for the address of a
5848 function. */
5849 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5850 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5851 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5852 operand = d_left (operand);
5854 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5856 /* This indicates a suffix operator. */
5857 operand = d_left (operand);
5858 d_print_subexpr (dpi, options, operand);
5859 d_print_expr_op (dpi, options, op);
5860 return;
5864 /* For sizeof..., just print the pack length. */
5865 if (code && !strcmp (code, "sZ"))
5867 struct demangle_component *a = d_find_pack (dpi, operand);
5868 int len = d_pack_length (a);
5869 d_append_num (dpi, len);
5870 return;
5872 else if (code && !strcmp (code, "sP"))
5874 int len = d_args_length (dpi, operand);
5875 d_append_num (dpi, len);
5876 return;
5879 if (op->type != DEMANGLE_COMPONENT_CAST)
5880 d_print_expr_op (dpi, options, op);
5881 else
5883 d_append_char (dpi, '(');
5884 d_print_cast (dpi, options, op);
5885 d_append_char (dpi, ')');
5887 if (code && !strcmp (code, "gs"))
5888 /* Avoid parens after '::'. */
5889 d_print_comp (dpi, options, operand);
5890 else if (code && (!strcmp (code, "st") || !strcmp (code, "nx")))
5891 /* Always print parens for sizeof (type) and noexcept(expr). */
5893 d_append_char (dpi, '(');
5894 d_print_comp (dpi, options, operand);
5895 d_append_char (dpi, ')');
5897 else
5898 d_print_subexpr (dpi, options, operand);
5900 return;
5902 case DEMANGLE_COMPONENT_BINARY:
5903 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5905 d_print_error (dpi);
5906 return;
5909 if (op_is_new_cast (d_left (dc)))
5911 d_print_expr_op (dpi, options, d_left (dc));
5912 d_append_char (dpi, '<');
5913 d_print_comp (dpi, options, d_left (d_right (dc)));
5914 d_append_string (dpi, ">(");
5915 d_print_comp (dpi, options, d_right (d_right (dc)));
5916 d_append_char (dpi, ')');
5917 return;
5920 if (d_maybe_print_fold_expression (dpi, options, dc))
5921 return;
5923 if (d_maybe_print_designated_init (dpi, options, dc))
5924 return;
5926 /* We wrap an expression which uses the greater-than operator in
5927 an extra layer of parens so that it does not get confused
5928 with the '>' which ends the template parameters. */
5929 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5930 && d_left (dc)->u.s_operator.op->len == 1
5931 && d_left (dc)->u.s_operator.op->name[0] == '>')
5932 d_append_char (dpi, '(');
5934 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5935 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5937 /* Function call used in an expression should not have printed types
5938 of the function arguments. Values of the function arguments still
5939 get printed below. */
5941 const struct demangle_component *func = d_left (d_right (dc));
5943 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5944 d_print_error (dpi);
5945 d_print_subexpr (dpi, options, d_left (func));
5947 else
5948 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5949 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5951 d_append_char (dpi, '[');
5952 d_print_comp (dpi, options, d_right (d_right (dc)));
5953 d_append_char (dpi, ']');
5955 else
5957 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5958 d_print_expr_op (dpi, options, d_left (dc));
5959 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5962 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5963 && d_left (dc)->u.s_operator.op->len == 1
5964 && d_left (dc)->u.s_operator.op->name[0] == '>')
5965 d_append_char (dpi, ')');
5967 return;
5969 case DEMANGLE_COMPONENT_BINARY_ARGS:
5970 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5971 d_print_error (dpi);
5972 return;
5974 case DEMANGLE_COMPONENT_TRINARY:
5975 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5976 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5978 d_print_error (dpi);
5979 return;
5981 if (d_maybe_print_fold_expression (dpi, options, dc))
5982 return;
5983 if (d_maybe_print_designated_init (dpi, options, dc))
5984 return;
5986 struct demangle_component *op = d_left (dc);
5987 struct demangle_component *first = d_left (d_right (dc));
5988 struct demangle_component *second = d_left (d_right (d_right (dc)));
5989 struct demangle_component *third = d_right (d_right (d_right (dc)));
5991 if (!strcmp (op->u.s_operator.op->code, "qu"))
5993 d_print_subexpr (dpi, options, first);
5994 d_print_expr_op (dpi, options, op);
5995 d_print_subexpr (dpi, options, second);
5996 d_append_string (dpi, " : ");
5997 d_print_subexpr (dpi, options, third);
5999 else
6001 d_append_string (dpi, "new ");
6002 if (d_left (first) != NULL)
6004 d_print_subexpr (dpi, options, first);
6005 d_append_char (dpi, ' ');
6007 d_print_comp (dpi, options, second);
6008 if (third)
6009 d_print_subexpr (dpi, options, third);
6012 return;
6014 case DEMANGLE_COMPONENT_TRINARY_ARG1:
6015 case DEMANGLE_COMPONENT_TRINARY_ARG2:
6016 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
6017 d_print_error (dpi);
6018 return;
6020 case DEMANGLE_COMPONENT_LITERAL:
6021 case DEMANGLE_COMPONENT_LITERAL_NEG:
6023 enum d_builtin_type_print tp;
6025 /* For some builtin types, produce simpler output. */
6026 tp = D_PRINT_DEFAULT;
6027 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
6029 tp = d_left (dc)->u.s_builtin.type->print;
6030 switch (tp)
6032 case D_PRINT_INT:
6033 case D_PRINT_UNSIGNED:
6034 case D_PRINT_LONG:
6035 case D_PRINT_UNSIGNED_LONG:
6036 case D_PRINT_LONG_LONG:
6037 case D_PRINT_UNSIGNED_LONG_LONG:
6038 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
6040 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6041 d_append_char (dpi, '-');
6042 d_print_comp (dpi, options, d_right (dc));
6043 switch (tp)
6045 default:
6046 break;
6047 case D_PRINT_UNSIGNED:
6048 d_append_char (dpi, 'u');
6049 break;
6050 case D_PRINT_LONG:
6051 d_append_char (dpi, 'l');
6052 break;
6053 case D_PRINT_UNSIGNED_LONG:
6054 d_append_string (dpi, "ul");
6055 break;
6056 case D_PRINT_LONG_LONG:
6057 d_append_string (dpi, "ll");
6058 break;
6059 case D_PRINT_UNSIGNED_LONG_LONG:
6060 d_append_string (dpi, "ull");
6061 break;
6063 return;
6065 break;
6067 case D_PRINT_BOOL:
6068 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
6069 && d_right (dc)->u.s_name.len == 1
6070 && dc->type == DEMANGLE_COMPONENT_LITERAL)
6072 switch (d_right (dc)->u.s_name.s[0])
6074 case '0':
6075 d_append_string (dpi, "false");
6076 return;
6077 case '1':
6078 d_append_string (dpi, "true");
6079 return;
6080 default:
6081 break;
6084 break;
6086 default:
6087 break;
6091 d_append_char (dpi, '(');
6092 d_print_comp (dpi, options, d_left (dc));
6093 d_append_char (dpi, ')');
6094 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
6095 d_append_char (dpi, '-');
6096 if (tp == D_PRINT_FLOAT)
6097 d_append_char (dpi, '[');
6098 d_print_comp (dpi, options, d_right (dc));
6099 if (tp == D_PRINT_FLOAT)
6100 d_append_char (dpi, ']');
6102 return;
6104 case DEMANGLE_COMPONENT_VENDOR_EXPR:
6105 d_print_comp (dpi, options, d_left (dc));
6106 d_append_char (dpi, '(');
6107 d_print_comp (dpi, options, d_right (dc));
6108 d_append_char (dpi, ')');
6109 return;
6111 case DEMANGLE_COMPONENT_NUMBER:
6112 d_append_num (dpi, dc->u.s_number.number);
6113 return;
6115 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
6116 d_append_string (dpi, "java resource ");
6117 d_print_comp (dpi, options, d_left (dc));
6118 return;
6120 case DEMANGLE_COMPONENT_COMPOUND_NAME:
6121 d_print_comp (dpi, options, d_left (dc));
6122 d_print_comp (dpi, options, d_right (dc));
6123 return;
6125 case DEMANGLE_COMPONENT_CHARACTER:
6126 d_append_char (dpi, dc->u.s_character.character);
6127 return;
6129 case DEMANGLE_COMPONENT_DECLTYPE:
6130 d_append_string (dpi, "decltype (");
6131 d_print_comp (dpi, options, d_left (dc));
6132 d_append_char (dpi, ')');
6133 return;
6135 case DEMANGLE_COMPONENT_PACK_EXPANSION:
6137 struct demangle_component *a = NULL;
6139 if (!dpi->lambda_tpl_parms)
6140 a = d_find_pack (dpi, d_left (dc));
6141 if (a == NULL)
6143 /* d_find_pack won't find anything if the only packs involved
6144 in this expansion are function parameter packs; in that
6145 case, just print the pattern and "...". */
6146 d_print_subexpr (dpi, options, d_left (dc));
6147 d_append_string (dpi, "...");
6149 else
6151 int len = d_pack_length (a);
6152 int i;
6154 dc = d_left (dc);
6155 for (i = 0; i < len; ++i)
6157 if (i)
6158 d_append_string (dpi, ", ");
6159 dpi->pack_index = i;
6160 d_print_comp (dpi, options, dc);
6164 return;
6166 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
6168 long num = dc->u.s_number.number;
6169 if (num == 0)
6170 d_append_string (dpi, "this");
6171 else
6173 d_append_string (dpi, "{parm#");
6174 d_append_num (dpi, num);
6175 d_append_char (dpi, '}');
6178 return;
6180 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
6181 d_append_string (dpi, "global constructors keyed to ");
6182 d_print_comp (dpi, options, dc->u.s_binary.left);
6183 return;
6185 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
6186 d_append_string (dpi, "global destructors keyed to ");
6187 d_print_comp (dpi, options, dc->u.s_binary.left);
6188 return;
6190 case DEMANGLE_COMPONENT_LAMBDA:
6192 d_append_string (dpi, "{lambda");
6193 struct demangle_component *parms = dc->u.s_unary_num.sub;
6194 struct d_print_template dpt;
6195 /* Generic lambda auto parms are mangled as the (synthedic) template
6196 type parm they are. We need to tell the printer that (a) we're in
6197 a lambda, and (b) the number of synthetic parms. */
6198 int saved_tpl_parms = dpi->lambda_tpl_parms;
6199 dpi->lambda_tpl_parms = 0;
6200 /* Hang any lambda head as-if template args. */
6201 dpt.template_decl = NULL;
6202 dpt.next = dpi->templates;
6203 dpi->templates = &dpt;
6204 if (parms && parms->type == DEMANGLE_COMPONENT_TEMPLATE_HEAD)
6206 dpt.template_decl = parms;
6208 d_append_char (dpi, '<');
6209 struct demangle_component *parm;
6210 for (parm = d_left (parms); parm; parm = d_right (parm))
6212 if (dpi->lambda_tpl_parms++)
6213 d_append_string (dpi, ", ");
6214 d_print_comp (dpi, options, parm);
6215 d_append_char (dpi, ' ');
6216 if (parm->type == DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM)
6217 parm = d_left (parm);
6218 d_print_lambda_parm_name (dpi, parm->type,
6219 dpi->lambda_tpl_parms - 1);
6221 d_append_char (dpi, '>');
6223 parms = d_right (parms);
6225 dpi->lambda_tpl_parms++;
6227 d_append_char (dpi, '(');
6228 d_print_comp (dpi, options, parms);
6229 dpi->lambda_tpl_parms = saved_tpl_parms;
6230 dpi->templates = dpt.next;
6231 d_append_string (dpi, ")#");
6232 d_append_num (dpi, dc->u.s_unary_num.num + 1);
6233 d_append_char (dpi, '}');
6235 return;
6237 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
6238 d_append_string (dpi, "{unnamed type#");
6239 d_append_num (dpi, dc->u.s_number.number + 1);
6240 d_append_char (dpi, '}');
6241 return;
6243 case DEMANGLE_COMPONENT_CLONE:
6244 d_print_comp (dpi, options, d_left (dc));
6245 d_append_string (dpi, " [clone ");
6246 d_print_comp (dpi, options, d_right (dc));
6247 d_append_char (dpi, ']');
6248 return;
6250 case DEMANGLE_COMPONENT_FRIEND:
6251 d_print_comp (dpi, options, d_left (dc));
6252 d_append_string (dpi, "[friend]");
6253 return;
6255 case DEMANGLE_COMPONENT_TEMPLATE_HEAD:
6257 d_append_char (dpi, '<');
6258 int count = 0;
6259 struct demangle_component *parm;
6260 for (parm = d_left (dc); parm; parm = d_right (parm))
6262 if (count++)
6263 d_append_string (dpi, ", ");
6264 d_print_comp (dpi, options, parm);
6266 d_append_char (dpi, '>');
6268 return;
6270 case DEMANGLE_COMPONENT_TEMPLATE_TYPE_PARM:
6271 d_append_string (dpi, "typename");
6272 return;
6274 case DEMANGLE_COMPONENT_TEMPLATE_NON_TYPE_PARM:
6275 d_print_comp (dpi, options, d_left (dc));
6276 return;
6278 case DEMANGLE_COMPONENT_TEMPLATE_TEMPLATE_PARM:
6279 d_append_string (dpi, "template");
6280 d_print_comp (dpi, options, d_left (dc));
6281 d_append_string (dpi, " class");
6282 return;
6284 case DEMANGLE_COMPONENT_TEMPLATE_PACK_PARM:
6285 d_print_comp (dpi, options, d_left (dc));
6286 d_append_string (dpi, "...");
6287 return;
6289 case DEMANGLE_COMPONENT_CONSTRAINTS:
6290 d_print_comp (dpi, options, d_left (dc));
6291 d_append_string (dpi, " requires ");
6292 d_print_comp (dpi, options, d_right (dc));
6293 return;
6295 default:
6296 d_print_error (dpi);
6297 return;
6301 static void
6302 d_print_comp (struct d_print_info *dpi, int options,
6303 struct demangle_component *dc)
6305 struct d_component_stack self;
6306 if (dc == NULL || dc->d_printing > 1 || dpi->recursion > MAX_RECURSION_COUNT)
6308 d_print_error (dpi);
6309 return;
6312 dc->d_printing++;
6313 dpi->recursion++;
6315 self.dc = dc;
6316 self.parent = dpi->component_stack;
6317 dpi->component_stack = &self;
6319 d_print_comp_inner (dpi, options, dc);
6321 dpi->component_stack = self.parent;
6322 dc->d_printing--;
6323 dpi->recursion--;
6326 /* Print a Java dentifier. For Java we try to handle encoded extended
6327 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
6328 so we don't it for C++. Characters are encoded as
6329 __U<hex-char>+_. */
6331 static void
6332 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
6334 const char *p;
6335 const char *end;
6337 end = name + len;
6338 for (p = name; p < end; ++p)
6340 if (end - p > 3
6341 && p[0] == '_'
6342 && p[1] == '_'
6343 && p[2] == 'U')
6345 unsigned long c;
6346 const char *q;
6348 c = 0;
6349 for (q = p + 3; q < end; ++q)
6351 int dig;
6353 if (IS_DIGIT (*q))
6354 dig = *q - '0';
6355 else if (*q >= 'A' && *q <= 'F')
6356 dig = *q - 'A' + 10;
6357 else if (*q >= 'a' && *q <= 'f')
6358 dig = *q - 'a' + 10;
6359 else
6360 break;
6362 c = c * 16 + dig;
6364 /* If the Unicode character is larger than 256, we don't try
6365 to deal with it here. FIXME. */
6366 if (q < end && *q == '_' && c < 256)
6368 d_append_char (dpi, c);
6369 p = q;
6370 continue;
6374 d_append_char (dpi, *p);
6378 /* Print a list of modifiers. SUFFIX is 1 if we are printing
6379 qualifiers on this after printing a function. */
6381 static void
6382 d_print_mod_list (struct d_print_info *dpi, int options,
6383 struct d_print_mod *mods, int suffix)
6385 struct d_print_template *hold_dpt;
6387 if (mods == NULL || d_print_saw_error (dpi))
6388 return;
6390 if (mods->printed
6391 || (! suffix
6392 && (is_fnqual_component_type (mods->mod->type))))
6394 d_print_mod_list (dpi, options, mods->next, suffix);
6395 return;
6398 mods->printed = 1;
6400 hold_dpt = dpi->templates;
6401 dpi->templates = mods->templates;
6403 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
6405 d_print_function_type (dpi, options, mods->mod, mods->next);
6406 dpi->templates = hold_dpt;
6407 return;
6409 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6411 d_print_array_type (dpi, options, mods->mod, mods->next);
6412 dpi->templates = hold_dpt;
6413 return;
6415 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
6417 struct d_print_mod *hold_modifiers;
6418 struct demangle_component *dc;
6420 /* When this is on the modifier stack, we have pulled any
6421 qualifiers off the right argument already. Otherwise, we
6422 print it as usual, but don't let the left argument see any
6423 modifiers. */
6425 hold_modifiers = dpi->modifiers;
6426 dpi->modifiers = NULL;
6427 d_print_comp (dpi, options, d_left (mods->mod));
6428 dpi->modifiers = hold_modifiers;
6430 if ((options & DMGL_JAVA) == 0)
6431 d_append_string (dpi, "::");
6432 else
6433 d_append_char (dpi, '.');
6435 dc = d_right (mods->mod);
6437 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
6439 d_append_string (dpi, "{default arg#");
6440 d_append_num (dpi, dc->u.s_unary_num.num + 1);
6441 d_append_string (dpi, "}::");
6442 dc = dc->u.s_unary_num.sub;
6445 while (is_fnqual_component_type (dc->type))
6446 dc = d_left (dc);
6448 d_print_comp (dpi, options, dc);
6450 dpi->templates = hold_dpt;
6451 return;
6454 d_print_mod (dpi, options, mods->mod);
6456 dpi->templates = hold_dpt;
6458 d_print_mod_list (dpi, options, mods->next, suffix);
6461 /* Print a modifier. */
6463 static void
6464 d_print_mod (struct d_print_info *dpi, int options,
6465 struct demangle_component *mod)
6467 switch (mod->type)
6469 case DEMANGLE_COMPONENT_RESTRICT:
6470 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6471 d_append_string (dpi, " restrict");
6472 return;
6473 case DEMANGLE_COMPONENT_VOLATILE:
6474 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6475 d_append_string (dpi, " volatile");
6476 return;
6477 case DEMANGLE_COMPONENT_CONST:
6478 case DEMANGLE_COMPONENT_CONST_THIS:
6479 d_append_string (dpi, " const");
6480 return;
6481 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
6482 d_append_string (dpi, " transaction_safe");
6483 return;
6484 case DEMANGLE_COMPONENT_NOEXCEPT:
6485 d_append_string (dpi, " noexcept");
6486 if (d_right (mod))
6488 d_append_char (dpi, '(');
6489 d_print_comp (dpi, options, d_right (mod));
6490 d_append_char (dpi, ')');
6492 return;
6493 case DEMANGLE_COMPONENT_THROW_SPEC:
6494 d_append_string (dpi, " throw");
6495 if (d_right (mod))
6497 d_append_char (dpi, '(');
6498 d_print_comp (dpi, options, d_right (mod));
6499 d_append_char (dpi, ')');
6501 return;
6502 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6503 d_append_char (dpi, ' ');
6504 d_print_comp (dpi, options, d_right (mod));
6505 return;
6506 case DEMANGLE_COMPONENT_POINTER:
6507 /* There is no pointer symbol in Java. */
6508 if ((options & DMGL_JAVA) == 0)
6509 d_append_char (dpi, '*');
6510 return;
6511 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6512 /* For the ref-qualifier, put a space before the &. */
6513 d_append_char (dpi, ' ');
6514 /* FALLTHRU */
6515 case DEMANGLE_COMPONENT_REFERENCE:
6516 d_append_char (dpi, '&');
6517 return;
6518 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6519 d_append_char (dpi, ' ');
6520 /* FALLTHRU */
6521 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6522 d_append_string (dpi, "&&");
6523 return;
6524 case DEMANGLE_COMPONENT_COMPLEX:
6525 d_append_string (dpi, " _Complex");
6526 return;
6527 case DEMANGLE_COMPONENT_IMAGINARY:
6528 d_append_string (dpi, " _Imaginary");
6529 return;
6530 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6531 if (d_last_char (dpi) != '(')
6532 d_append_char (dpi, ' ');
6533 d_print_comp (dpi, options, d_left (mod));
6534 d_append_string (dpi, "::*");
6535 return;
6536 case DEMANGLE_COMPONENT_TYPED_NAME:
6537 d_print_comp (dpi, options, d_left (mod));
6538 return;
6539 case DEMANGLE_COMPONENT_VECTOR_TYPE:
6540 d_append_string (dpi, " __vector(");
6541 d_print_comp (dpi, options, d_left (mod));
6542 d_append_char (dpi, ')');
6543 return;
6545 default:
6546 /* Otherwise, we have something that won't go back on the
6547 modifier stack, so we can just print it. */
6548 d_print_comp (dpi, options, mod);
6549 return;
6553 /* Print a function type, except for the return type. */
6555 static void
6556 d_print_function_type (struct d_print_info *dpi, int options,
6557 struct demangle_component *dc,
6558 struct d_print_mod *mods)
6560 int need_paren;
6561 int need_space;
6562 struct d_print_mod *p;
6563 struct d_print_mod *hold_modifiers;
6565 need_paren = 0;
6566 need_space = 0;
6567 for (p = mods; p != NULL; p = p->next)
6569 if (p->printed)
6570 break;
6572 switch (p->mod->type)
6574 case DEMANGLE_COMPONENT_POINTER:
6575 case DEMANGLE_COMPONENT_REFERENCE:
6576 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
6577 need_paren = 1;
6578 break;
6579 case DEMANGLE_COMPONENT_RESTRICT:
6580 case DEMANGLE_COMPONENT_VOLATILE:
6581 case DEMANGLE_COMPONENT_CONST:
6582 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
6583 case DEMANGLE_COMPONENT_COMPLEX:
6584 case DEMANGLE_COMPONENT_IMAGINARY:
6585 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
6586 need_space = 1;
6587 need_paren = 1;
6588 break;
6589 FNQUAL_COMPONENT_CASE:
6590 break;
6591 default:
6592 break;
6594 if (need_paren)
6595 break;
6598 if (need_paren)
6600 if (! need_space)
6602 if (d_last_char (dpi) != '('
6603 && d_last_char (dpi) != '*')
6604 need_space = 1;
6606 if (need_space && d_last_char (dpi) != ' ')
6607 d_append_char (dpi, ' ');
6608 d_append_char (dpi, '(');
6611 hold_modifiers = dpi->modifiers;
6612 dpi->modifiers = NULL;
6614 d_print_mod_list (dpi, options, mods, 0);
6616 if (need_paren)
6617 d_append_char (dpi, ')');
6619 d_append_char (dpi, '(');
6621 if (d_right (dc) != NULL)
6622 d_print_comp (dpi, options, d_right (dc));
6624 d_append_char (dpi, ')');
6626 d_print_mod_list (dpi, options, mods, 1);
6628 dpi->modifiers = hold_modifiers;
6631 /* Print an array type, except for the element type. */
6633 static void
6634 d_print_array_type (struct d_print_info *dpi, int options,
6635 struct demangle_component *dc,
6636 struct d_print_mod *mods)
6638 int need_space;
6640 need_space = 1;
6641 if (mods != NULL)
6643 int need_paren;
6644 struct d_print_mod *p;
6646 need_paren = 0;
6647 for (p = mods; p != NULL; p = p->next)
6649 if (! p->printed)
6651 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6653 need_space = 0;
6654 break;
6656 else
6658 need_paren = 1;
6659 need_space = 1;
6660 break;
6665 if (need_paren)
6666 d_append_string (dpi, " (");
6668 d_print_mod_list (dpi, options, mods, 0);
6670 if (need_paren)
6671 d_append_char (dpi, ')');
6674 if (need_space)
6675 d_append_char (dpi, ' ');
6677 d_append_char (dpi, '[');
6679 if (d_left (dc) != NULL)
6680 d_print_comp (dpi, options, d_left (dc));
6682 d_append_char (dpi, ']');
6685 /* Print an operator in an expression. */
6687 static void
6688 d_print_expr_op (struct d_print_info *dpi, int options,
6689 struct demangle_component *dc)
6691 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
6692 d_append_buffer (dpi, dc->u.s_operator.op->name,
6693 dc->u.s_operator.op->len);
6694 else
6695 d_print_comp (dpi, options, dc);
6698 /* Print a cast. */
6700 static void
6701 d_print_cast (struct d_print_info *dpi, int options,
6702 struct demangle_component *dc)
6704 d_print_comp (dpi, options, d_left (dc));
6707 /* Print a conversion operator. */
6709 static void
6710 d_print_conversion (struct d_print_info *dpi, int options,
6711 struct demangle_component *dc)
6713 struct d_print_template dpt;
6715 /* For a conversion operator, we need the template parameters from
6716 the enclosing template in scope for processing the type. */
6717 if (dpi->current_template != NULL)
6719 dpt.next = dpi->templates;
6720 dpi->templates = &dpt;
6721 dpt.template_decl = dpi->current_template;
6724 d_print_comp (dpi, options, d_left (dc));
6726 if (dpi->current_template != NULL)
6727 dpi->templates = dpt.next;
6730 /* Initialize the information structure we use to pass around
6731 information. */
6733 CP_STATIC_IF_GLIBCPP_V3
6734 void
6735 cplus_demangle_init_info (const char *mangled, int options, size_t len,
6736 struct d_info *di)
6738 di->s = mangled;
6739 di->send = mangled + len;
6740 di->options = options;
6742 di->n = mangled;
6744 /* We cannot need more components than twice the number of chars in
6745 the mangled string. Most components correspond directly to
6746 chars, but the ARGLIST types are exceptions. */
6747 di->num_comps = 2 * len;
6748 di->next_comp = 0;
6750 /* Similarly, we cannot need more substitutions than there are
6751 chars in the mangled string. */
6752 di->num_subs = len;
6753 di->next_sub = 0;
6755 di->last_name = NULL;
6757 di->expansion = 0;
6758 di->is_expression = 0;
6759 di->is_conversion = 0;
6760 di->recursion_level = 0;
6763 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6764 mangled name, return strings in repeated callback giving the demangled
6765 name. OPTIONS is the usual libiberty demangler options. On success,
6766 this returns 1. On failure, returns 0. */
6768 static int
6769 d_demangle_callback (const char *mangled, int options,
6770 demangle_callbackref callback, void *opaque)
6772 enum
6774 DCT_TYPE,
6775 DCT_MANGLED,
6776 DCT_GLOBAL_CTORS,
6777 DCT_GLOBAL_DTORS
6779 type;
6780 struct d_info di;
6781 struct demangle_component *dc;
6782 int status;
6784 if (mangled[0] == '_' && mangled[1] == 'Z')
6785 type = DCT_MANGLED;
6786 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6787 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6788 && (mangled[9] == 'D' || mangled[9] == 'I')
6789 && mangled[10] == '_')
6790 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
6791 else
6793 if ((options & DMGL_TYPES) == 0)
6794 return 0;
6795 type = DCT_TYPE;
6798 di.unresolved_name_state = 1;
6800 again:
6801 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6803 /* PR 87675 - Check for a mangled string that is so long
6804 that we do not have enough stack space to demangle it. */
6805 if (((options & DMGL_NO_RECURSE_LIMIT) == 0)
6806 /* This check is a bit arbitrary, since what we really want to do is to
6807 compare the sizes of the di.comps and di.subs arrays against the
6808 amount of stack space remaining. But there is no portable way to do
6809 this, so instead we use the recursion limit as a guide to the maximum
6810 size of the arrays. */
6811 && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT)
6813 /* FIXME: We need a way to indicate that a stack limit has been reached. */
6814 return 0;
6818 #ifdef CP_DYNAMIC_ARRAYS
6819 __extension__ struct demangle_component comps[di.num_comps];
6820 __extension__ struct demangle_component *subs[di.num_subs];
6822 di.comps = comps;
6823 di.subs = subs;
6824 #else
6825 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6826 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6827 #endif
6829 switch (type)
6831 case DCT_TYPE:
6832 dc = cplus_demangle_type (&di);
6833 break;
6834 case DCT_MANGLED:
6835 dc = cplus_demangle_mangled_name (&di, 1);
6836 break;
6837 case DCT_GLOBAL_CTORS:
6838 case DCT_GLOBAL_DTORS:
6839 d_advance (&di, 11);
6840 dc = d_make_comp (&di,
6841 (type == DCT_GLOBAL_CTORS
6842 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6843 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
6844 d_make_demangle_mangled_name (&di, d_str (&di)),
6845 NULL);
6846 d_advance (&di, strlen (d_str (&di)));
6847 break;
6848 default:
6849 abort (); /* We have listed all the cases. */
6852 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6853 mangled string, then we didn't successfully demangle it. If
6854 DMGL_PARAMS is not set, we didn't look at the trailing
6855 parameters. */
6856 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
6857 dc = NULL;
6859 /* See discussion in d_unresolved_name. */
6860 if (dc == NULL && di.unresolved_name_state == -1)
6862 di.unresolved_name_state = 0;
6863 goto again;
6866 #ifdef CP_DEMANGLE_DEBUG
6867 d_dump (dc, 0);
6868 #endif
6870 status = (dc != NULL)
6871 ? cplus_demangle_print_callback (options, dc, callback, opaque)
6872 : 0;
6875 return status;
6878 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6879 name, return a buffer allocated with malloc holding the demangled
6880 name. OPTIONS is the usual libiberty demangler options. On
6881 success, this sets *PALC to the allocated size of the returned
6882 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6883 a memory allocation failure, and returns NULL. */
6885 static char *
6886 d_demangle (const char *mangled, int options, size_t *palc)
6888 struct d_growable_string dgs;
6889 int status;
6891 d_growable_string_init (&dgs, 0);
6893 status = d_demangle_callback (mangled, options,
6894 d_growable_string_callback_adapter, &dgs);
6895 if (status == 0)
6897 free (dgs.buf);
6898 *palc = 0;
6899 return NULL;
6902 *palc = dgs.allocation_failure ? 1 : dgs.alc;
6903 return dgs.buf;
6906 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6908 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6910 /* ia64 ABI-mandated entry point in the C++ runtime library for
6911 performing demangling. MANGLED_NAME is a NUL-terminated character
6912 string containing the name to be demangled.
6914 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6915 *LENGTH bytes, into which the demangled name is stored. If
6916 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6917 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6918 is placed in a region of memory allocated with malloc.
6920 If LENGTH is non-NULL, the length of the buffer containing the
6921 demangled name, is placed in *LENGTH.
6923 The return value is a pointer to the start of the NUL-terminated
6924 demangled name, or NULL if the demangling fails. The caller is
6925 responsible for deallocating this memory using free.
6927 *STATUS is set to one of the following values:
6928 0: The demangling operation succeeded.
6929 -1: A memory allocation failure occurred.
6930 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6931 -3: One of the arguments is invalid.
6933 The demangling is performed using the C++ ABI mangling rules, with
6934 GNU extensions. */
6936 char *
6937 __cxa_demangle (const char *mangled_name, char *output_buffer,
6938 size_t *length, int *status)
6940 char *demangled;
6941 size_t alc;
6943 if (mangled_name == NULL)
6945 if (status != NULL)
6946 *status = -3;
6947 return NULL;
6950 if (output_buffer != NULL && length == NULL)
6952 if (status != NULL)
6953 *status = -3;
6954 return NULL;
6957 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6959 if (demangled == NULL)
6961 if (status != NULL)
6963 if (alc == 1)
6964 *status = -1;
6965 else
6966 *status = -2;
6968 return NULL;
6971 if (output_buffer == NULL)
6973 if (length != NULL)
6974 *length = alc;
6976 else
6978 if (strlen (demangled) < *length)
6980 strcpy (output_buffer, demangled);
6981 free (demangled);
6982 demangled = output_buffer;
6984 else
6986 free (output_buffer);
6987 *length = alc;
6991 if (status != NULL)
6992 *status = 0;
6994 return demangled;
6997 extern int __gcclibcxx_demangle_callback (const char *,
6998 void (*)
6999 (const char *, size_t, void *),
7000 void *);
7002 /* Alternative, allocationless entry point in the C++ runtime library
7003 for performing demangling. MANGLED_NAME is a NUL-terminated character
7004 string containing the name to be demangled.
7006 CALLBACK is a callback function, called with demangled string
7007 segments as demangling progresses; it is called at least once,
7008 but may be called more than once. OPAQUE is a generalized pointer
7009 used as a callback argument.
7011 The return code is one of the following values, equivalent to
7012 the STATUS values of __cxa_demangle() (excluding -1, since this
7013 function performs no memory allocations):
7014 0: The demangling operation succeeded.
7015 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
7016 -3: One of the arguments is invalid.
7018 The demangling is performed using the C++ ABI mangling rules, with
7019 GNU extensions. */
7022 __gcclibcxx_demangle_callback (const char *mangled_name,
7023 void (*callback) (const char *, size_t, void *),
7024 void *opaque)
7026 int status;
7028 if (mangled_name == NULL || callback == NULL)
7029 return -3;
7031 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
7032 callback, opaque);
7033 if (status == 0)
7034 return -2;
7036 return 0;
7039 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
7041 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
7042 mangled name, return a buffer allocated with malloc holding the
7043 demangled name. Otherwise, return NULL. */
7045 char *
7046 cplus_demangle_v3 (const char *mangled, int options)
7048 size_t alc;
7050 return d_demangle (mangled, options, &alc);
7054 cplus_demangle_v3_callback (const char *mangled, int options,
7055 demangle_callbackref callback, void *opaque)
7057 return d_demangle_callback (mangled, options, callback, opaque);
7060 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
7061 conventions, but the output formatting is a little different.
7062 This instructs the C++ demangler not to emit pointer characters ("*"), to
7063 use Java's namespace separator symbol ("." instead of "::"), and to output
7064 JArray<TYPE> as TYPE[]. */
7066 char *
7067 java_demangle_v3 (const char *mangled)
7069 size_t alc;
7071 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
7075 java_demangle_v3_callback (const char *mangled,
7076 demangle_callbackref callback, void *opaque)
7078 return d_demangle_callback (mangled,
7079 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
7080 callback, opaque);
7083 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
7085 #ifndef IN_GLIBCPP_V3
7087 /* Demangle a string in order to find out whether it is a constructor
7088 or destructor. Return non-zero on success. Set *CTOR_KIND and
7089 *DTOR_KIND appropriately. */
7091 static int
7092 is_ctor_or_dtor (const char *mangled,
7093 enum gnu_v3_ctor_kinds *ctor_kind,
7094 enum gnu_v3_dtor_kinds *dtor_kind)
7096 struct d_info di;
7097 struct demangle_component *dc;
7098 int ret;
7100 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
7101 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
7103 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
7106 #ifdef CP_DYNAMIC_ARRAYS
7107 __extension__ struct demangle_component comps[di.num_comps];
7108 __extension__ struct demangle_component *subs[di.num_subs];
7110 di.comps = comps;
7111 di.subs = subs;
7112 #else
7113 di.comps = alloca (di.num_comps * sizeof (*di.comps));
7114 di.subs = alloca (di.num_subs * sizeof (*di.subs));
7115 #endif
7117 dc = cplus_demangle_mangled_name (&di, 1);
7119 /* Note that because we did not pass DMGL_PARAMS, we don't expect
7120 to demangle the entire string. */
7122 ret = 0;
7123 while (dc != NULL)
7125 switch (dc->type)
7127 /* These cannot appear on a constructor or destructor. */
7128 case DEMANGLE_COMPONENT_RESTRICT_THIS:
7129 case DEMANGLE_COMPONENT_VOLATILE_THIS:
7130 case DEMANGLE_COMPONENT_CONST_THIS:
7131 case DEMANGLE_COMPONENT_REFERENCE_THIS:
7132 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
7133 default:
7134 dc = NULL;
7135 break;
7136 case DEMANGLE_COMPONENT_TYPED_NAME:
7137 case DEMANGLE_COMPONENT_TEMPLATE:
7138 dc = d_left (dc);
7139 break;
7140 case DEMANGLE_COMPONENT_QUAL_NAME:
7141 case DEMANGLE_COMPONENT_LOCAL_NAME:
7142 dc = d_right (dc);
7143 break;
7144 case DEMANGLE_COMPONENT_CTOR:
7145 *ctor_kind = dc->u.s_ctor.kind;
7146 ret = 1;
7147 dc = NULL;
7148 break;
7149 case DEMANGLE_COMPONENT_DTOR:
7150 *dtor_kind = dc->u.s_dtor.kind;
7151 ret = 1;
7152 dc = NULL;
7153 break;
7158 return ret;
7161 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
7162 name. A non-zero return indicates the type of constructor. */
7164 enum gnu_v3_ctor_kinds
7165 is_gnu_v3_mangled_ctor (const char *name)
7167 enum gnu_v3_ctor_kinds ctor_kind;
7168 enum gnu_v3_dtor_kinds dtor_kind;
7170 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7171 return (enum gnu_v3_ctor_kinds) 0;
7172 return ctor_kind;
7176 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
7177 name. A non-zero return indicates the type of destructor. */
7179 enum gnu_v3_dtor_kinds
7180 is_gnu_v3_mangled_dtor (const char *name)
7182 enum gnu_v3_ctor_kinds ctor_kind;
7183 enum gnu_v3_dtor_kinds dtor_kind;
7185 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
7186 return (enum gnu_v3_dtor_kinds) 0;
7187 return dtor_kind;
7190 #endif /* IN_GLIBCPP_V3 */
7192 #ifdef STANDALONE_DEMANGLER
7194 #include "getopt.h"
7195 #include "dyn-string.h"
7197 static void print_usage (FILE* fp, int exit_value);
7199 #define IS_ALPHA(CHAR) \
7200 (((CHAR) >= 'a' && (CHAR) <= 'z') \
7201 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
7203 /* Non-zero if CHAR is a character than can occur in a mangled name. */
7204 #define is_mangled_char(CHAR) \
7205 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
7206 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
7208 /* The name of this program, as invoked. */
7209 const char* program_name;
7211 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
7213 static void
7214 print_usage (FILE* fp, int exit_value)
7216 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
7217 fprintf (fp, "Options:\n");
7218 fprintf (fp, " -h,--help Display this message.\n");
7219 fprintf (fp, " -p,--no-params Don't display function parameters\n");
7220 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
7221 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
7223 exit (exit_value);
7226 /* Option specification for getopt_long. */
7227 static const struct option long_options[] =
7229 { "help", no_argument, NULL, 'h' },
7230 { "no-params", no_argument, NULL, 'p' },
7231 { "verbose", no_argument, NULL, 'v' },
7232 { NULL, no_argument, NULL, 0 },
7235 /* Main entry for a demangling filter executable. It will demangle
7236 its command line arguments, if any. If none are provided, it will
7237 filter stdin to stdout, replacing any recognized mangled C++ names
7238 with their demangled equivalents. */
7241 main (int argc, char *argv[])
7243 int i;
7244 int opt_char;
7245 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
7247 /* Use the program name of this program, as invoked. */
7248 program_name = argv[0];
7250 /* Parse options. */
7253 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
7254 switch (opt_char)
7256 case '?': /* Unrecognized option. */
7257 print_usage (stderr, 1);
7258 break;
7260 case 'h':
7261 print_usage (stdout, 0);
7262 break;
7264 case 'p':
7265 options &= ~ DMGL_PARAMS;
7266 break;
7268 case 'v':
7269 options |= DMGL_VERBOSE;
7270 break;
7273 while (opt_char != -1);
7275 if (optind == argc)
7276 /* No command line arguments were provided. Filter stdin. */
7278 dyn_string_t mangled = dyn_string_new (3);
7279 char *s;
7281 /* Read all of input. */
7282 while (!feof (stdin))
7284 char c;
7286 /* Pile characters into mangled until we hit one that can't
7287 occur in a mangled name. */
7288 c = getchar ();
7289 while (!feof (stdin) && is_mangled_char (c))
7291 dyn_string_append_char (mangled, c);
7292 if (feof (stdin))
7293 break;
7294 c = getchar ();
7297 if (dyn_string_length (mangled) > 0)
7299 #ifdef IN_GLIBCPP_V3
7300 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
7301 #else
7302 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
7303 #endif
7305 if (s != NULL)
7307 fputs (s, stdout);
7308 free (s);
7310 else
7312 /* It might not have been a mangled name. Print the
7313 original text. */
7314 fputs (dyn_string_buf (mangled), stdout);
7317 dyn_string_clear (mangled);
7320 /* If we haven't hit EOF yet, we've read one character that
7321 can't occur in a mangled name, so print it out. */
7322 if (!feof (stdin))
7323 putchar (c);
7326 dyn_string_delete (mangled);
7328 else
7329 /* Demangle command line arguments. */
7331 /* Loop over command line arguments. */
7332 for (i = optind; i < argc; ++i)
7334 char *s;
7335 #ifdef IN_GLIBCPP_V3
7336 int status;
7337 #endif
7339 /* Attempt to demangle. */
7340 #ifdef IN_GLIBCPP_V3
7341 s = __cxa_demangle (argv[i], NULL, NULL, &status);
7342 #else
7343 s = cplus_demangle_v3 (argv[i], options);
7344 #endif
7346 /* If it worked, print the demangled name. */
7347 if (s != NULL)
7349 printf ("%s\n", s);
7350 free (s);
7352 else
7354 #ifdef IN_GLIBCPP_V3
7355 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
7356 #else
7357 fprintf (stderr, "Failed: %s\n", argv[i]);
7358 #endif
7363 return 0;
7366 #endif /* STANDALONE_DEMANGLER */