PR go/67101
[official-gcc.git] / libiberty / cp-demangle.c
blob82541004bf7762a022eea7d884bdf321631b4f95
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
20 executable.)
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
41 name.
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
58 demangle_callbackref
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
64 cplus_demangle_print
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
73 IN_LIBGCC2
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int *status)
77 int __gcclibcxx_demangle_callback (const char *,
78 void (*)
79 (const char *, size_t, void *),
80 void *)
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
84 IN_GLIBCPP_V3
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
89 STANDALONE_DEMANGLER
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
93 CP_DEMANGLE_DEBUG
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
97 CHECK_DEMANGLER
98 If defined, additional sanity checks will be performed. It will
99 cause some slowdown, but will allow to catch out-of-bound access
100 errors earlier. This macro is intended for testing and debugging. */
102 #if defined (_AIX) && !defined (__GNUC__)
103 #pragma alloca
104 #endif
106 #ifdef HAVE_CONFIG_H
107 #include "config.h"
108 #endif
110 #include <stdio.h>
112 #ifdef HAVE_STDLIB_H
113 #include <stdlib.h>
114 #endif
115 #ifdef HAVE_STRING_H
116 #include <string.h>
117 #endif
119 #ifdef HAVE_ALLOCA_H
120 # include <alloca.h>
121 #else
122 # ifndef alloca
123 # ifdef __GNUC__
124 # define alloca __builtin_alloca
125 # else
126 extern char *alloca ();
127 # endif /* __GNUC__ */
128 # endif /* alloca */
129 #endif /* HAVE_ALLOCA_H */
131 #include "ansidecl.h"
132 #include "libiberty.h"
133 #include "demangle.h"
134 #include "cp-demangle.h"
136 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
137 also rename them via #define to avoid compiler errors when the
138 static definition conflicts with the extern declaration in a header
139 file. */
140 #ifdef IN_GLIBCPP_V3
142 #define CP_STATIC_IF_GLIBCPP_V3 static
144 #define cplus_demangle_fill_name d_fill_name
145 static int d_fill_name (struct demangle_component *, const char *, int);
147 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
148 static int
149 d_fill_extended_operator (struct demangle_component *, int,
150 struct demangle_component *);
152 #define cplus_demangle_fill_ctor d_fill_ctor
153 static int
154 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
155 struct demangle_component *);
157 #define cplus_demangle_fill_dtor d_fill_dtor
158 static int
159 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
160 struct demangle_component *);
162 #define cplus_demangle_mangled_name d_mangled_name
163 static struct demangle_component *d_mangled_name (struct d_info *, int);
165 #define cplus_demangle_type d_type
166 static struct demangle_component *d_type (struct d_info *);
168 #define cplus_demangle_print d_print
169 static char *d_print (int, const struct demangle_component *, int, size_t *);
171 #define cplus_demangle_print_callback d_print_callback
172 static int d_print_callback (int, const struct demangle_component *,
173 demangle_callbackref, void *);
175 #define cplus_demangle_init_info d_init_info
176 static void d_init_info (const char *, int, size_t, struct d_info *);
178 #else /* ! defined(IN_GLIBCPP_V3) */
179 #define CP_STATIC_IF_GLIBCPP_V3
180 #endif /* ! defined(IN_GLIBCPP_V3) */
182 /* See if the compiler supports dynamic arrays. */
184 #ifdef __GNUC__
185 #define CP_DYNAMIC_ARRAYS
186 #else
187 #ifdef __STDC__
188 #ifdef __STDC_VERSION__
189 #if __STDC_VERSION__ >= 199901L
190 #define CP_DYNAMIC_ARRAYS
191 #endif /* __STDC__VERSION >= 199901L */
192 #endif /* defined (__STDC_VERSION__) */
193 #endif /* defined (__STDC__) */
194 #endif /* ! defined (__GNUC__) */
196 /* We avoid pulling in the ctype tables, to prevent pulling in
197 additional unresolved symbols when this code is used in a library.
198 FIXME: Is this really a valid reason? This comes from the original
199 V3 demangler code.
201 As of this writing this file has the following undefined references
202 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
203 strcat, strlen. */
205 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
206 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
207 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
209 /* The prefix prepended by GCC to an identifier represnting the
210 anonymous namespace. */
211 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
212 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
213 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
215 /* Information we keep for the standard substitutions. */
217 struct d_standard_sub_info
219 /* The code for this substitution. */
220 char code;
221 /* The simple string it expands to. */
222 const char *simple_expansion;
223 /* The length of the simple expansion. */
224 int simple_len;
225 /* The results of a full, verbose, expansion. This is used when
226 qualifying a constructor/destructor, or when in verbose mode. */
227 const char *full_expansion;
228 /* The length of the full expansion. */
229 int full_len;
230 /* What to set the last_name field of d_info to; NULL if we should
231 not set it. This is only relevant when qualifying a
232 constructor/destructor. */
233 const char *set_last_name;
234 /* The length of set_last_name. */
235 int set_last_name_len;
238 /* Accessors for subtrees of struct demangle_component. */
240 #define d_left(dc) ((dc)->u.s_binary.left)
241 #define d_right(dc) ((dc)->u.s_binary.right)
243 /* A list of templates. This is used while printing. */
245 struct d_print_template
247 /* Next template on the list. */
248 struct d_print_template *next;
249 /* This template. */
250 const struct demangle_component *template_decl;
253 /* A list of type modifiers. This is used while printing. */
255 struct d_print_mod
257 /* Next modifier on the list. These are in the reverse of the order
258 in which they appeared in the mangled string. */
259 struct d_print_mod *next;
260 /* The modifier. */
261 const struct demangle_component *mod;
262 /* Whether this modifier was printed. */
263 int printed;
264 /* The list of templates which applies to this modifier. */
265 struct d_print_template *templates;
268 /* We use these structures to hold information during printing. */
270 struct d_growable_string
272 /* Buffer holding the result. */
273 char *buf;
274 /* Current length of data in buffer. */
275 size_t len;
276 /* Allocated size of buffer. */
277 size_t alc;
278 /* Set to 1 if we had a memory allocation failure. */
279 int allocation_failure;
282 /* Stack of components, innermost first, used to avoid loops. */
284 struct d_component_stack
286 /* This component. */
287 const struct demangle_component *dc;
288 /* This component's parent. */
289 const struct d_component_stack *parent;
292 /* A demangle component and some scope captured when it was first
293 traversed. */
295 struct d_saved_scope
297 /* The component whose scope this is. */
298 const struct demangle_component *container;
299 /* The list of templates, if any, that was current when this
300 scope was captured. */
301 struct d_print_template *templates;
304 /* Checkpoint structure to allow backtracking. This holds copies
305 of the fields of struct d_info that need to be restored
306 if a trial parse needs to be backtracked over. */
308 struct d_info_checkpoint
310 const char *n;
311 int next_comp;
312 int next_sub;
313 int did_subs;
314 int expansion;
317 enum { D_PRINT_BUFFER_LENGTH = 256 };
318 struct d_print_info
320 /* Fixed-length allocated buffer for demangled data, flushed to the
321 callback with a NUL termination once full. */
322 char buf[D_PRINT_BUFFER_LENGTH];
323 /* Current length of data in buffer. */
324 size_t len;
325 /* The last character printed, saved individually so that it survives
326 any buffer flush. */
327 char last_char;
328 /* Callback function to handle demangled buffer flush. */
329 demangle_callbackref callback;
330 /* Opaque callback argument. */
331 void *opaque;
332 /* The current list of templates, if any. */
333 struct d_print_template *templates;
334 /* The current list of modifiers (e.g., pointer, reference, etc.),
335 if any. */
336 struct d_print_mod *modifiers;
337 /* Set to 1 if we saw a demangling error. */
338 int demangle_failure;
339 /* The current index into any template argument packs we are using
340 for printing. */
341 int pack_index;
342 /* Number of d_print_flush calls so far. */
343 unsigned long int flush_count;
344 /* Stack of components, innermost first, used to avoid loops. */
345 const struct d_component_stack *component_stack;
346 /* Array of saved scopes for evaluating substitutions. */
347 struct d_saved_scope *saved_scopes;
348 /* Index of the next unused saved scope in the above array. */
349 int next_saved_scope;
350 /* Number of saved scopes in the above array. */
351 int num_saved_scopes;
352 /* Array of templates for saving into scopes. */
353 struct d_print_template *copy_templates;
354 /* Index of the next unused copy template in the above array. */
355 int next_copy_template;
356 /* Number of copy templates in the above array. */
357 int num_copy_templates;
358 /* The nearest enclosing template, if any. */
359 const struct demangle_component *current_template;
362 #ifdef CP_DEMANGLE_DEBUG
363 static void d_dump (struct demangle_component *, int);
364 #endif
366 static struct demangle_component *
367 d_make_empty (struct d_info *);
369 static struct demangle_component *
370 d_make_comp (struct d_info *, enum demangle_component_type,
371 struct demangle_component *,
372 struct demangle_component *);
374 static struct demangle_component *
375 d_make_name (struct d_info *, const char *, int);
377 static struct demangle_component *
378 d_make_demangle_mangled_name (struct d_info *, const char *);
380 static struct demangle_component *
381 d_make_builtin_type (struct d_info *,
382 const struct demangle_builtin_type_info *);
384 static struct demangle_component *
385 d_make_operator (struct d_info *,
386 const struct demangle_operator_info *);
388 static struct demangle_component *
389 d_make_extended_operator (struct d_info *, int,
390 struct demangle_component *);
392 static struct demangle_component *
393 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
394 struct demangle_component *);
396 static struct demangle_component *
397 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
398 struct demangle_component *);
400 static struct demangle_component *
401 d_make_template_param (struct d_info *, long);
403 static struct demangle_component *
404 d_make_sub (struct d_info *, const char *, int);
406 static int
407 has_return_type (struct demangle_component *);
409 static int
410 is_ctor_dtor_or_conversion (struct demangle_component *);
412 static struct demangle_component *d_encoding (struct d_info *, int);
414 static struct demangle_component *d_name (struct d_info *);
416 static struct demangle_component *d_nested_name (struct d_info *);
418 static struct demangle_component *d_prefix (struct d_info *);
420 static struct demangle_component *d_unqualified_name (struct d_info *);
422 static struct demangle_component *d_source_name (struct d_info *);
424 static long d_number (struct d_info *);
426 static struct demangle_component *d_identifier (struct d_info *, long);
428 static struct demangle_component *d_operator_name (struct d_info *);
430 static struct demangle_component *d_special_name (struct d_info *);
432 static int d_call_offset (struct d_info *, int);
434 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
436 static struct demangle_component **
437 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
439 static struct demangle_component *
440 d_ref_qualifier (struct d_info *, struct demangle_component *);
442 static struct demangle_component *
443 d_function_type (struct d_info *);
445 static struct demangle_component *
446 d_bare_function_type (struct d_info *, int);
448 static struct demangle_component *
449 d_class_enum_type (struct d_info *);
451 static struct demangle_component *d_array_type (struct d_info *);
453 static struct demangle_component *d_vector_type (struct d_info *);
455 static struct demangle_component *
456 d_pointer_to_member_type (struct d_info *);
458 static struct demangle_component *
459 d_template_param (struct d_info *);
461 static struct demangle_component *d_template_args (struct d_info *);
463 static struct demangle_component *
464 d_template_arg (struct d_info *);
466 static struct demangle_component *d_expression (struct d_info *);
468 static struct demangle_component *d_expr_primary (struct d_info *);
470 static struct demangle_component *d_local_name (struct d_info *);
472 static int d_discriminator (struct d_info *);
474 static struct demangle_component *d_lambda (struct d_info *);
476 static struct demangle_component *d_unnamed_type (struct d_info *);
478 static struct demangle_component *
479 d_clone_suffix (struct d_info *, struct demangle_component *);
481 static int
482 d_add_substitution (struct d_info *, struct demangle_component *);
484 static struct demangle_component *d_substitution (struct d_info *, int);
486 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
488 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
490 static void d_growable_string_init (struct d_growable_string *, size_t);
492 static inline void
493 d_growable_string_resize (struct d_growable_string *, size_t);
495 static inline void
496 d_growable_string_append_buffer (struct d_growable_string *,
497 const char *, size_t);
498 static void
499 d_growable_string_callback_adapter (const char *, size_t, void *);
501 static void
502 d_print_init (struct d_print_info *, demangle_callbackref, void *,
503 const struct demangle_component *);
505 static inline void d_print_error (struct d_print_info *);
507 static inline int d_print_saw_error (struct d_print_info *);
509 static inline void d_print_flush (struct d_print_info *);
511 static inline void d_append_char (struct d_print_info *, char);
513 static inline void d_append_buffer (struct d_print_info *,
514 const char *, size_t);
516 static inline void d_append_string (struct d_print_info *, const char *);
518 static inline char d_last_char (struct d_print_info *);
520 static void
521 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
523 static void
524 d_print_java_identifier (struct d_print_info *, const char *, int);
526 static void
527 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
529 static void
530 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
532 static void
533 d_print_function_type (struct d_print_info *, int,
534 const struct demangle_component *,
535 struct d_print_mod *);
537 static void
538 d_print_array_type (struct d_print_info *, int,
539 const struct demangle_component *,
540 struct d_print_mod *);
542 static void
543 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
545 static void
546 d_print_cast (struct d_print_info *, int, const struct demangle_component *);
548 static int d_demangle_callback (const char *, int,
549 demangle_callbackref, void *);
550 static char *d_demangle (const char *, int, size_t *);
552 #ifdef CP_DEMANGLE_DEBUG
554 static void
555 d_dump (struct demangle_component *dc, int indent)
557 int i;
559 if (dc == NULL)
561 if (indent == 0)
562 printf ("failed demangling\n");
563 return;
566 for (i = 0; i < indent; ++i)
567 putchar (' ');
569 switch (dc->type)
571 case DEMANGLE_COMPONENT_NAME:
572 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
573 return;
574 case DEMANGLE_COMPONENT_TAGGED_NAME:
575 printf ("tagged name\n");
576 d_dump (dc->u.s_binary.left, indent + 2);
577 d_dump (dc->u.s_binary.right, indent + 2);
578 return;
579 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
580 printf ("template parameter %ld\n", dc->u.s_number.number);
581 return;
582 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
583 printf ("function parameter %ld\n", dc->u.s_number.number);
584 return;
585 case DEMANGLE_COMPONENT_CTOR:
586 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
587 d_dump (dc->u.s_ctor.name, indent + 2);
588 return;
589 case DEMANGLE_COMPONENT_DTOR:
590 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
591 d_dump (dc->u.s_dtor.name, indent + 2);
592 return;
593 case DEMANGLE_COMPONENT_SUB_STD:
594 printf ("standard substitution %s\n", dc->u.s_string.string);
595 return;
596 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
597 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
598 return;
599 case DEMANGLE_COMPONENT_OPERATOR:
600 printf ("operator %s\n", dc->u.s_operator.op->name);
601 return;
602 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
603 printf ("extended operator with %d args\n",
604 dc->u.s_extended_operator.args);
605 d_dump (dc->u.s_extended_operator.name, indent + 2);
606 return;
608 case DEMANGLE_COMPONENT_QUAL_NAME:
609 printf ("qualified name\n");
610 break;
611 case DEMANGLE_COMPONENT_LOCAL_NAME:
612 printf ("local name\n");
613 break;
614 case DEMANGLE_COMPONENT_TYPED_NAME:
615 printf ("typed name\n");
616 break;
617 case DEMANGLE_COMPONENT_TEMPLATE:
618 printf ("template\n");
619 break;
620 case DEMANGLE_COMPONENT_VTABLE:
621 printf ("vtable\n");
622 break;
623 case DEMANGLE_COMPONENT_VTT:
624 printf ("VTT\n");
625 break;
626 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
627 printf ("construction vtable\n");
628 break;
629 case DEMANGLE_COMPONENT_TYPEINFO:
630 printf ("typeinfo\n");
631 break;
632 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
633 printf ("typeinfo name\n");
634 break;
635 case DEMANGLE_COMPONENT_TYPEINFO_FN:
636 printf ("typeinfo function\n");
637 break;
638 case DEMANGLE_COMPONENT_THUNK:
639 printf ("thunk\n");
640 break;
641 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
642 printf ("virtual thunk\n");
643 break;
644 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
645 printf ("covariant thunk\n");
646 break;
647 case DEMANGLE_COMPONENT_JAVA_CLASS:
648 printf ("java class\n");
649 break;
650 case DEMANGLE_COMPONENT_GUARD:
651 printf ("guard\n");
652 break;
653 case DEMANGLE_COMPONENT_REFTEMP:
654 printf ("reference temporary\n");
655 break;
656 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
657 printf ("hidden alias\n");
658 break;
659 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
660 printf ("transaction clone\n");
661 break;
662 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
663 printf ("non-transaction clone\n");
664 break;
665 case DEMANGLE_COMPONENT_RESTRICT:
666 printf ("restrict\n");
667 break;
668 case DEMANGLE_COMPONENT_VOLATILE:
669 printf ("volatile\n");
670 break;
671 case DEMANGLE_COMPONENT_CONST:
672 printf ("const\n");
673 break;
674 case DEMANGLE_COMPONENT_RESTRICT_THIS:
675 printf ("restrict this\n");
676 break;
677 case DEMANGLE_COMPONENT_VOLATILE_THIS:
678 printf ("volatile this\n");
679 break;
680 case DEMANGLE_COMPONENT_CONST_THIS:
681 printf ("const this\n");
682 break;
683 case DEMANGLE_COMPONENT_REFERENCE_THIS:
684 printf ("reference this\n");
685 break;
686 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
687 printf ("rvalue reference this\n");
688 break;
689 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
690 printf ("vendor type qualifier\n");
691 break;
692 case DEMANGLE_COMPONENT_POINTER:
693 printf ("pointer\n");
694 break;
695 case DEMANGLE_COMPONENT_REFERENCE:
696 printf ("reference\n");
697 break;
698 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
699 printf ("rvalue reference\n");
700 break;
701 case DEMANGLE_COMPONENT_COMPLEX:
702 printf ("complex\n");
703 break;
704 case DEMANGLE_COMPONENT_IMAGINARY:
705 printf ("imaginary\n");
706 break;
707 case DEMANGLE_COMPONENT_VENDOR_TYPE:
708 printf ("vendor type\n");
709 break;
710 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
711 printf ("function type\n");
712 break;
713 case DEMANGLE_COMPONENT_ARRAY_TYPE:
714 printf ("array type\n");
715 break;
716 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
717 printf ("pointer to member type\n");
718 break;
719 case DEMANGLE_COMPONENT_FIXED_TYPE:
720 printf ("fixed-point type, accum? %d, sat? %d\n",
721 dc->u.s_fixed.accum, dc->u.s_fixed.sat);
722 d_dump (dc->u.s_fixed.length, indent + 2);
723 break;
724 case DEMANGLE_COMPONENT_ARGLIST:
725 printf ("argument list\n");
726 break;
727 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
728 printf ("template argument list\n");
729 break;
730 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
731 printf ("initializer list\n");
732 break;
733 case DEMANGLE_COMPONENT_CAST:
734 printf ("cast\n");
735 break;
736 case DEMANGLE_COMPONENT_NULLARY:
737 printf ("nullary operator\n");
738 break;
739 case DEMANGLE_COMPONENT_UNARY:
740 printf ("unary operator\n");
741 break;
742 case DEMANGLE_COMPONENT_BINARY:
743 printf ("binary operator\n");
744 break;
745 case DEMANGLE_COMPONENT_BINARY_ARGS:
746 printf ("binary operator arguments\n");
747 break;
748 case DEMANGLE_COMPONENT_TRINARY:
749 printf ("trinary operator\n");
750 break;
751 case DEMANGLE_COMPONENT_TRINARY_ARG1:
752 printf ("trinary operator arguments 1\n");
753 break;
754 case DEMANGLE_COMPONENT_TRINARY_ARG2:
755 printf ("trinary operator arguments 1\n");
756 break;
757 case DEMANGLE_COMPONENT_LITERAL:
758 printf ("literal\n");
759 break;
760 case DEMANGLE_COMPONENT_LITERAL_NEG:
761 printf ("negative literal\n");
762 break;
763 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
764 printf ("java resource\n");
765 break;
766 case DEMANGLE_COMPONENT_COMPOUND_NAME:
767 printf ("compound name\n");
768 break;
769 case DEMANGLE_COMPONENT_CHARACTER:
770 printf ("character '%c'\n", dc->u.s_character.character);
771 return;
772 case DEMANGLE_COMPONENT_NUMBER:
773 printf ("number %ld\n", dc->u.s_number.number);
774 return;
775 case DEMANGLE_COMPONENT_DECLTYPE:
776 printf ("decltype\n");
777 break;
778 case DEMANGLE_COMPONENT_PACK_EXPANSION:
779 printf ("pack expansion\n");
780 break;
781 case DEMANGLE_COMPONENT_TLS_INIT:
782 printf ("tls init function\n");
783 break;
784 case DEMANGLE_COMPONENT_TLS_WRAPPER:
785 printf ("tls wrapper function\n");
786 break;
787 case DEMANGLE_COMPONENT_DEFAULT_ARG:
788 printf ("default argument %d\n", dc->u.s_unary_num.num);
789 d_dump (dc->u.s_unary_num.sub, indent+2);
790 return;
791 case DEMANGLE_COMPONENT_LAMBDA:
792 printf ("lambda %d\n", dc->u.s_unary_num.num);
793 d_dump (dc->u.s_unary_num.sub, indent+2);
794 return;
797 d_dump (d_left (dc), indent + 2);
798 d_dump (d_right (dc), indent + 2);
801 #endif /* CP_DEMANGLE_DEBUG */
803 /* Fill in a DEMANGLE_COMPONENT_NAME. */
805 CP_STATIC_IF_GLIBCPP_V3
807 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
809 if (p == NULL || s == NULL || len == 0)
810 return 0;
811 p->type = DEMANGLE_COMPONENT_NAME;
812 p->u.s_name.s = s;
813 p->u.s_name.len = len;
814 return 1;
817 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
819 CP_STATIC_IF_GLIBCPP_V3
821 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
822 struct demangle_component *name)
824 if (p == NULL || args < 0 || name == NULL)
825 return 0;
826 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
827 p->u.s_extended_operator.args = args;
828 p->u.s_extended_operator.name = name;
829 return 1;
832 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
834 CP_STATIC_IF_GLIBCPP_V3
836 cplus_demangle_fill_ctor (struct demangle_component *p,
837 enum gnu_v3_ctor_kinds kind,
838 struct demangle_component *name)
840 if (p == NULL
841 || name == NULL
842 || (int) kind < gnu_v3_complete_object_ctor
843 || (int) kind > gnu_v3_object_ctor_group)
844 return 0;
845 p->type = DEMANGLE_COMPONENT_CTOR;
846 p->u.s_ctor.kind = kind;
847 p->u.s_ctor.name = name;
848 return 1;
851 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
853 CP_STATIC_IF_GLIBCPP_V3
855 cplus_demangle_fill_dtor (struct demangle_component *p,
856 enum gnu_v3_dtor_kinds kind,
857 struct demangle_component *name)
859 if (p == NULL
860 || name == NULL
861 || (int) kind < gnu_v3_deleting_dtor
862 || (int) kind > gnu_v3_object_dtor_group)
863 return 0;
864 p->type = DEMANGLE_COMPONENT_DTOR;
865 p->u.s_dtor.kind = kind;
866 p->u.s_dtor.name = name;
867 return 1;
870 /* Add a new component. */
872 static struct demangle_component *
873 d_make_empty (struct d_info *di)
875 struct demangle_component *p;
877 if (di->next_comp >= di->num_comps)
878 return NULL;
879 p = &di->comps[di->next_comp];
880 ++di->next_comp;
881 return p;
884 /* Add a new generic component. */
886 static struct demangle_component *
887 d_make_comp (struct d_info *di, enum demangle_component_type type,
888 struct demangle_component *left,
889 struct demangle_component *right)
891 struct demangle_component *p;
893 /* We check for errors here. A typical error would be a NULL return
894 from a subroutine. We catch those here, and return NULL
895 upward. */
896 switch (type)
898 /* These types require two parameters. */
899 case DEMANGLE_COMPONENT_QUAL_NAME:
900 case DEMANGLE_COMPONENT_LOCAL_NAME:
901 case DEMANGLE_COMPONENT_TYPED_NAME:
902 case DEMANGLE_COMPONENT_TAGGED_NAME:
903 case DEMANGLE_COMPONENT_TEMPLATE:
904 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
905 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
906 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
907 case DEMANGLE_COMPONENT_UNARY:
908 case DEMANGLE_COMPONENT_BINARY:
909 case DEMANGLE_COMPONENT_BINARY_ARGS:
910 case DEMANGLE_COMPONENT_TRINARY:
911 case DEMANGLE_COMPONENT_TRINARY_ARG1:
912 case DEMANGLE_COMPONENT_LITERAL:
913 case DEMANGLE_COMPONENT_LITERAL_NEG:
914 case DEMANGLE_COMPONENT_COMPOUND_NAME:
915 case DEMANGLE_COMPONENT_VECTOR_TYPE:
916 case DEMANGLE_COMPONENT_CLONE:
917 if (left == NULL || right == NULL)
918 return NULL;
919 break;
921 /* These types only require one parameter. */
922 case DEMANGLE_COMPONENT_VTABLE:
923 case DEMANGLE_COMPONENT_VTT:
924 case DEMANGLE_COMPONENT_TYPEINFO:
925 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
926 case DEMANGLE_COMPONENT_TYPEINFO_FN:
927 case DEMANGLE_COMPONENT_THUNK:
928 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
929 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
930 case DEMANGLE_COMPONENT_JAVA_CLASS:
931 case DEMANGLE_COMPONENT_GUARD:
932 case DEMANGLE_COMPONENT_TLS_INIT:
933 case DEMANGLE_COMPONENT_TLS_WRAPPER:
934 case DEMANGLE_COMPONENT_REFTEMP:
935 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
936 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
937 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
938 case DEMANGLE_COMPONENT_POINTER:
939 case DEMANGLE_COMPONENT_REFERENCE:
940 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
941 case DEMANGLE_COMPONENT_COMPLEX:
942 case DEMANGLE_COMPONENT_IMAGINARY:
943 case DEMANGLE_COMPONENT_VENDOR_TYPE:
944 case DEMANGLE_COMPONENT_CAST:
945 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
946 case DEMANGLE_COMPONENT_DECLTYPE:
947 case DEMANGLE_COMPONENT_PACK_EXPANSION:
948 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
949 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
950 case DEMANGLE_COMPONENT_NULLARY:
951 case DEMANGLE_COMPONENT_TRINARY_ARG2:
952 if (left == NULL)
953 return NULL;
954 break;
956 /* This needs a right parameter, but the left parameter can be
957 empty. */
958 case DEMANGLE_COMPONENT_ARRAY_TYPE:
959 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
960 if (right == NULL)
961 return NULL;
962 break;
964 /* These are allowed to have no parameters--in some cases they
965 will be filled in later. */
966 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
967 case DEMANGLE_COMPONENT_RESTRICT:
968 case DEMANGLE_COMPONENT_VOLATILE:
969 case DEMANGLE_COMPONENT_CONST:
970 case DEMANGLE_COMPONENT_RESTRICT_THIS:
971 case DEMANGLE_COMPONENT_VOLATILE_THIS:
972 case DEMANGLE_COMPONENT_CONST_THIS:
973 case DEMANGLE_COMPONENT_REFERENCE_THIS:
974 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
975 case DEMANGLE_COMPONENT_ARGLIST:
976 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
977 break;
979 /* Other types should not be seen here. */
980 default:
981 return NULL;
984 p = d_make_empty (di);
985 if (p != NULL)
987 p->type = type;
988 p->u.s_binary.left = left;
989 p->u.s_binary.right = right;
991 return p;
994 /* Add a new demangle mangled name component. */
996 static struct demangle_component *
997 d_make_demangle_mangled_name (struct d_info *di, const char *s)
999 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1000 return d_make_name (di, s, strlen (s));
1001 d_advance (di, 2);
1002 return d_encoding (di, 0);
1005 /* Add a new name component. */
1007 static struct demangle_component *
1008 d_make_name (struct d_info *di, const char *s, int len)
1010 struct demangle_component *p;
1012 p = d_make_empty (di);
1013 if (! cplus_demangle_fill_name (p, s, len))
1014 return NULL;
1015 return p;
1018 /* Add a new builtin type component. */
1020 static struct demangle_component *
1021 d_make_builtin_type (struct d_info *di,
1022 const struct demangle_builtin_type_info *type)
1024 struct demangle_component *p;
1026 if (type == NULL)
1027 return NULL;
1028 p = d_make_empty (di);
1029 if (p != NULL)
1031 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1032 p->u.s_builtin.type = type;
1034 return p;
1037 /* Add a new operator component. */
1039 static struct demangle_component *
1040 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1042 struct demangle_component *p;
1044 p = d_make_empty (di);
1045 if (p != NULL)
1047 p->type = DEMANGLE_COMPONENT_OPERATOR;
1048 p->u.s_operator.op = op;
1050 return p;
1053 /* Add a new extended operator component. */
1055 static struct demangle_component *
1056 d_make_extended_operator (struct d_info *di, int args,
1057 struct demangle_component *name)
1059 struct demangle_component *p;
1061 p = d_make_empty (di);
1062 if (! cplus_demangle_fill_extended_operator (p, args, name))
1063 return NULL;
1064 return p;
1067 static struct demangle_component *
1068 d_make_default_arg (struct d_info *di, int num,
1069 struct demangle_component *sub)
1071 struct demangle_component *p = d_make_empty (di);
1072 if (p)
1074 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1075 p->u.s_unary_num.num = num;
1076 p->u.s_unary_num.sub = sub;
1078 return p;
1081 /* Add a new constructor component. */
1083 static struct demangle_component *
1084 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1085 struct demangle_component *name)
1087 struct demangle_component *p;
1089 p = d_make_empty (di);
1090 if (! cplus_demangle_fill_ctor (p, kind, name))
1091 return NULL;
1092 return p;
1095 /* Add a new destructor component. */
1097 static struct demangle_component *
1098 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1099 struct demangle_component *name)
1101 struct demangle_component *p;
1103 p = d_make_empty (di);
1104 if (! cplus_demangle_fill_dtor (p, kind, name))
1105 return NULL;
1106 return p;
1109 /* Add a new template parameter. */
1111 static struct demangle_component *
1112 d_make_template_param (struct d_info *di, long i)
1114 struct demangle_component *p;
1116 p = d_make_empty (di);
1117 if (p != NULL)
1119 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1120 p->u.s_number.number = i;
1122 return p;
1125 /* Add a new function parameter. */
1127 static struct demangle_component *
1128 d_make_function_param (struct d_info *di, long i)
1130 struct demangle_component *p;
1132 p = d_make_empty (di);
1133 if (p != NULL)
1135 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1136 p->u.s_number.number = i;
1138 return p;
1141 /* Add a new standard substitution component. */
1143 static struct demangle_component *
1144 d_make_sub (struct d_info *di, const char *name, int len)
1146 struct demangle_component *p;
1148 p = d_make_empty (di);
1149 if (p != NULL)
1151 p->type = DEMANGLE_COMPONENT_SUB_STD;
1152 p->u.s_string.string = name;
1153 p->u.s_string.len = len;
1155 return p;
1158 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1160 TOP_LEVEL is non-zero when called at the top level. */
1162 CP_STATIC_IF_GLIBCPP_V3
1163 struct demangle_component *
1164 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1166 struct demangle_component *p;
1168 if (! d_check_char (di, '_')
1169 /* Allow missing _ if not at toplevel to work around a
1170 bug in G++ abi-version=2 mangling; see the comment in
1171 write_template_arg. */
1172 && top_level)
1173 return NULL;
1174 if (! d_check_char (di, 'Z'))
1175 return NULL;
1176 p = d_encoding (di, top_level);
1178 /* If at top level and parsing parameters, check for a clone
1179 suffix. */
1180 if (top_level && (di->options & DMGL_PARAMS) != 0)
1181 while (d_peek_char (di) == '.'
1182 && (IS_LOWER (d_peek_next_char (di))
1183 || d_peek_next_char (di) == '_'
1184 || IS_DIGIT (d_peek_next_char (di))))
1185 p = d_clone_suffix (di, p);
1187 return p;
1190 /* Return whether a function should have a return type. The argument
1191 is the function name, which may be qualified in various ways. The
1192 rules are that template functions have return types with some
1193 exceptions, function types which are not part of a function name
1194 mangling have return types with some exceptions, and non-template
1195 function names do not have return types. The exceptions are that
1196 constructors, destructors, and conversion operators do not have
1197 return types. */
1199 static int
1200 has_return_type (struct demangle_component *dc)
1202 if (dc == NULL)
1203 return 0;
1204 switch (dc->type)
1206 default:
1207 return 0;
1208 case DEMANGLE_COMPONENT_TEMPLATE:
1209 return ! is_ctor_dtor_or_conversion (d_left (dc));
1210 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1211 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1212 case DEMANGLE_COMPONENT_CONST_THIS:
1213 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1214 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1215 return has_return_type (d_left (dc));
1219 /* Return whether a name is a constructor, a destructor, or a
1220 conversion operator. */
1222 static int
1223 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1225 if (dc == NULL)
1226 return 0;
1227 switch (dc->type)
1229 default:
1230 return 0;
1231 case DEMANGLE_COMPONENT_QUAL_NAME:
1232 case DEMANGLE_COMPONENT_LOCAL_NAME:
1233 return is_ctor_dtor_or_conversion (d_right (dc));
1234 case DEMANGLE_COMPONENT_CTOR:
1235 case DEMANGLE_COMPONENT_DTOR:
1236 case DEMANGLE_COMPONENT_CAST:
1237 return 1;
1241 /* <encoding> ::= <(function) name> <bare-function-type>
1242 ::= <(data) name>
1243 ::= <special-name>
1245 TOP_LEVEL is non-zero when called at the top level, in which case
1246 if DMGL_PARAMS is not set we do not demangle the function
1247 parameters. We only set this at the top level, because otherwise
1248 we would not correctly demangle names in local scopes. */
1250 static struct demangle_component *
1251 d_encoding (struct d_info *di, int top_level)
1253 char peek = d_peek_char (di);
1255 if (peek == 'G' || peek == 'T')
1256 return d_special_name (di);
1257 else
1259 struct demangle_component *dc;
1261 dc = d_name (di);
1263 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1265 /* Strip off any initial CV-qualifiers, as they really apply
1266 to the `this' parameter, and they were not output by the
1267 v2 demangler without DMGL_PARAMS. */
1268 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1269 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1270 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1271 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1272 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1273 dc = d_left (dc);
1275 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1276 there may be CV-qualifiers on its right argument which
1277 really apply here; this happens when parsing a class
1278 which is local to a function. */
1279 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1281 struct demangle_component *dcr;
1283 dcr = d_right (dc);
1284 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1285 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1286 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1287 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1288 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1289 dcr = d_left (dcr);
1290 dc->u.s_binary.right = dcr;
1293 return dc;
1296 peek = d_peek_char (di);
1297 if (dc == NULL || peek == '\0' || peek == 'E')
1298 return dc;
1299 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1300 d_bare_function_type (di, has_return_type (dc)));
1304 /* <tagged-name> ::= <name> B <source-name> */
1306 static struct demangle_component *
1307 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1309 char peek;
1310 while (peek = d_peek_char (di),
1311 peek == 'B')
1313 struct demangle_component *tag;
1314 d_advance (di, 1);
1315 tag = d_source_name (di);
1316 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1318 return dc;
1321 /* <name> ::= <nested-name>
1322 ::= <unscoped-name>
1323 ::= <unscoped-template-name> <template-args>
1324 ::= <local-name>
1326 <unscoped-name> ::= <unqualified-name>
1327 ::= St <unqualified-name>
1329 <unscoped-template-name> ::= <unscoped-name>
1330 ::= <substitution>
1333 static struct demangle_component *
1334 d_name (struct d_info *di)
1336 char peek = d_peek_char (di);
1337 struct demangle_component *dc;
1339 switch (peek)
1341 case 'N':
1342 return d_nested_name (di);
1344 case 'Z':
1345 return d_local_name (di);
1347 case 'U':
1348 return d_unqualified_name (di);
1350 case 'S':
1352 int subst;
1354 if (d_peek_next_char (di) != 't')
1356 dc = d_substitution (di, 0);
1357 subst = 1;
1359 else
1361 d_advance (di, 2);
1362 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1363 d_make_name (di, "std", 3),
1364 d_unqualified_name (di));
1365 di->expansion += 3;
1366 subst = 0;
1369 if (d_peek_char (di) != 'I')
1371 /* The grammar does not permit this case to occur if we
1372 called d_substitution() above (i.e., subst == 1). We
1373 don't bother to check. */
1375 else
1377 /* This is <template-args>, which means that we just saw
1378 <unscoped-template-name>, which is a substitution
1379 candidate if we didn't just get it from a
1380 substitution. */
1381 if (! subst)
1383 if (! d_add_substitution (di, dc))
1384 return NULL;
1386 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1387 d_template_args (di));
1390 return dc;
1393 case 'L':
1394 default:
1395 dc = d_unqualified_name (di);
1396 if (d_peek_char (di) == 'I')
1398 /* This is <template-args>, which means that we just saw
1399 <unscoped-template-name>, which is a substitution
1400 candidate. */
1401 if (! d_add_substitution (di, dc))
1402 return NULL;
1403 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1404 d_template_args (di));
1406 return dc;
1410 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1411 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1414 static struct demangle_component *
1415 d_nested_name (struct d_info *di)
1417 struct demangle_component *ret;
1418 struct demangle_component **pret;
1419 struct demangle_component *rqual;
1421 if (! d_check_char (di, 'N'))
1422 return NULL;
1424 pret = d_cv_qualifiers (di, &ret, 1);
1425 if (pret == NULL)
1426 return NULL;
1428 /* Parse the ref-qualifier now and then attach it
1429 once we have something to attach it to. */
1430 rqual = d_ref_qualifier (di, NULL);
1432 *pret = d_prefix (di);
1433 if (*pret == NULL)
1434 return NULL;
1436 if (rqual)
1438 d_left (rqual) = ret;
1439 ret = rqual;
1442 if (! d_check_char (di, 'E'))
1443 return NULL;
1445 return ret;
1448 /* <prefix> ::= <prefix> <unqualified-name>
1449 ::= <template-prefix> <template-args>
1450 ::= <template-param>
1451 ::= <decltype>
1453 ::= <substitution>
1455 <template-prefix> ::= <prefix> <(template) unqualified-name>
1456 ::= <template-param>
1457 ::= <substitution>
1460 static struct demangle_component *
1461 d_prefix (struct d_info *di)
1463 struct demangle_component *ret = NULL;
1465 while (1)
1467 char peek;
1468 enum demangle_component_type comb_type;
1469 struct demangle_component *dc;
1471 peek = d_peek_char (di);
1472 if (peek == '\0')
1473 return NULL;
1475 /* The older code accepts a <local-name> here, but I don't see
1476 that in the grammar. The older code does not accept a
1477 <template-param> here. */
1479 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1480 if (peek == 'D')
1482 char peek2 = d_peek_next_char (di);
1483 if (peek2 == 'T' || peek2 == 't')
1484 /* Decltype. */
1485 dc = cplus_demangle_type (di);
1486 else
1487 /* Destructor name. */
1488 dc = d_unqualified_name (di);
1490 else if (IS_DIGIT (peek)
1491 || IS_LOWER (peek)
1492 || peek == 'C'
1493 || peek == 'U'
1494 || peek == 'L')
1495 dc = d_unqualified_name (di);
1496 else if (peek == 'S')
1497 dc = d_substitution (di, 1);
1498 else if (peek == 'I')
1500 if (ret == NULL)
1501 return NULL;
1502 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1503 dc = d_template_args (di);
1505 else if (peek == 'T')
1506 dc = d_template_param (di);
1507 else if (peek == 'E')
1508 return ret;
1509 else if (peek == 'M')
1511 /* Initializer scope for a lambda. We don't need to represent
1512 this; the normal code will just treat the variable as a type
1513 scope, which gives appropriate output. */
1514 if (ret == NULL)
1515 return NULL;
1516 d_advance (di, 1);
1517 continue;
1519 else
1520 return NULL;
1522 if (ret == NULL)
1523 ret = dc;
1524 else
1525 ret = d_make_comp (di, comb_type, ret, dc);
1527 if (peek != 'S' && d_peek_char (di) != 'E')
1529 if (! d_add_substitution (di, ret))
1530 return NULL;
1535 /* <unqualified-name> ::= <operator-name>
1536 ::= <ctor-dtor-name>
1537 ::= <source-name>
1538 ::= <local-source-name>
1540 <local-source-name> ::= L <source-name> <discriminator>
1543 static struct demangle_component *
1544 d_unqualified_name (struct d_info *di)
1546 struct demangle_component *ret;
1547 char peek;
1549 peek = d_peek_char (di);
1550 if (IS_DIGIT (peek))
1551 ret = d_source_name (di);
1552 else if (IS_LOWER (peek))
1554 ret = d_operator_name (di);
1555 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1557 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1558 if (!strcmp (ret->u.s_operator.op->code, "li"))
1559 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1560 d_source_name (di));
1563 else if (peek == 'C' || peek == 'D')
1564 ret = d_ctor_dtor_name (di);
1565 else if (peek == 'L')
1567 d_advance (di, 1);
1569 ret = d_source_name (di);
1570 if (ret == NULL)
1571 return NULL;
1572 if (! d_discriminator (di))
1573 return NULL;
1575 else if (peek == 'U')
1577 switch (d_peek_next_char (di))
1579 case 'l':
1580 ret = d_lambda (di);
1581 break;
1582 case 't':
1583 ret = d_unnamed_type (di);
1584 break;
1585 default:
1586 return NULL;
1589 else
1590 return NULL;
1592 if (d_peek_char (di) == 'B')
1593 ret = d_abi_tags (di, ret);
1594 return ret;
1597 /* <source-name> ::= <(positive length) number> <identifier> */
1599 static struct demangle_component *
1600 d_source_name (struct d_info *di)
1602 long len;
1603 struct demangle_component *ret;
1605 len = d_number (di);
1606 if (len <= 0)
1607 return NULL;
1608 ret = d_identifier (di, len);
1609 di->last_name = ret;
1610 return ret;
1613 /* number ::= [n] <(non-negative decimal integer)> */
1615 static long
1616 d_number (struct d_info *di)
1618 int negative;
1619 char peek;
1620 long ret;
1622 negative = 0;
1623 peek = d_peek_char (di);
1624 if (peek == 'n')
1626 negative = 1;
1627 d_advance (di, 1);
1628 peek = d_peek_char (di);
1631 ret = 0;
1632 while (1)
1634 if (! IS_DIGIT (peek))
1636 if (negative)
1637 ret = - ret;
1638 return ret;
1640 ret = ret * 10 + peek - '0';
1641 d_advance (di, 1);
1642 peek = d_peek_char (di);
1646 /* Like d_number, but returns a demangle_component. */
1648 static struct demangle_component *
1649 d_number_component (struct d_info *di)
1651 struct demangle_component *ret = d_make_empty (di);
1652 if (ret)
1654 ret->type = DEMANGLE_COMPONENT_NUMBER;
1655 ret->u.s_number.number = d_number (di);
1657 return ret;
1660 /* identifier ::= <(unqualified source code identifier)> */
1662 static struct demangle_component *
1663 d_identifier (struct d_info *di, long len)
1665 const char *name;
1667 name = d_str (di);
1669 if (di->send - name < len)
1670 return NULL;
1672 d_advance (di, len);
1674 /* A Java mangled name may have a trailing '$' if it is a C++
1675 keyword. This '$' is not included in the length count. We just
1676 ignore the '$'. */
1677 if ((di->options & DMGL_JAVA) != 0
1678 && d_peek_char (di) == '$')
1679 d_advance (di, 1);
1681 /* Look for something which looks like a gcc encoding of an
1682 anonymous namespace, and replace it with a more user friendly
1683 name. */
1684 if (len >= (long) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1685 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1686 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1688 const char *s;
1690 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1691 if ((*s == '.' || *s == '_' || *s == '$')
1692 && s[1] == 'N')
1694 di->expansion -= len - sizeof "(anonymous namespace)";
1695 return d_make_name (di, "(anonymous namespace)",
1696 sizeof "(anonymous namespace)" - 1);
1700 return d_make_name (di, name, len);
1703 /* operator_name ::= many different two character encodings.
1704 ::= cv <type>
1705 ::= v <digit> <source-name>
1707 This list is sorted for binary search. */
1709 #define NL(s) s, (sizeof s) - 1
1711 CP_STATIC_IF_GLIBCPP_V3
1712 const struct demangle_operator_info cplus_demangle_operators[] =
1714 { "aN", NL ("&="), 2 },
1715 { "aS", NL ("="), 2 },
1716 { "aa", NL ("&&"), 2 },
1717 { "ad", NL ("&"), 1 },
1718 { "an", NL ("&"), 2 },
1719 { "at", NL ("alignof "), 1 },
1720 { "az", NL ("alignof "), 1 },
1721 { "cc", NL ("const_cast"), 2 },
1722 { "cl", NL ("()"), 2 },
1723 { "cm", NL (","), 2 },
1724 { "co", NL ("~"), 1 },
1725 { "dV", NL ("/="), 2 },
1726 { "da", NL ("delete[] "), 1 },
1727 { "dc", NL ("dynamic_cast"), 2 },
1728 { "de", NL ("*"), 1 },
1729 { "dl", NL ("delete "), 1 },
1730 { "ds", NL (".*"), 2 },
1731 { "dt", NL ("."), 2 },
1732 { "dv", NL ("/"), 2 },
1733 { "eO", NL ("^="), 2 },
1734 { "eo", NL ("^"), 2 },
1735 { "eq", NL ("=="), 2 },
1736 { "ge", NL (">="), 2 },
1737 { "gs", NL ("::"), 1 },
1738 { "gt", NL (">"), 2 },
1739 { "ix", NL ("[]"), 2 },
1740 { "lS", NL ("<<="), 2 },
1741 { "le", NL ("<="), 2 },
1742 { "li", NL ("operator\"\" "), 1 },
1743 { "ls", NL ("<<"), 2 },
1744 { "lt", NL ("<"), 2 },
1745 { "mI", NL ("-="), 2 },
1746 { "mL", NL ("*="), 2 },
1747 { "mi", NL ("-"), 2 },
1748 { "ml", NL ("*"), 2 },
1749 { "mm", NL ("--"), 1 },
1750 { "na", NL ("new[]"), 3 },
1751 { "ne", NL ("!="), 2 },
1752 { "ng", NL ("-"), 1 },
1753 { "nt", NL ("!"), 1 },
1754 { "nw", NL ("new"), 3 },
1755 { "oR", NL ("|="), 2 },
1756 { "oo", NL ("||"), 2 },
1757 { "or", NL ("|"), 2 },
1758 { "pL", NL ("+="), 2 },
1759 { "pl", NL ("+"), 2 },
1760 { "pm", NL ("->*"), 2 },
1761 { "pp", NL ("++"), 1 },
1762 { "ps", NL ("+"), 1 },
1763 { "pt", NL ("->"), 2 },
1764 { "qu", NL ("?"), 3 },
1765 { "rM", NL ("%="), 2 },
1766 { "rS", NL (">>="), 2 },
1767 { "rc", NL ("reinterpret_cast"), 2 },
1768 { "rm", NL ("%"), 2 },
1769 { "rs", NL (">>"), 2 },
1770 { "sc", NL ("static_cast"), 2 },
1771 { "st", NL ("sizeof "), 1 },
1772 { "sz", NL ("sizeof "), 1 },
1773 { "tr", NL ("throw"), 0 },
1774 { "tw", NL ("throw "), 1 },
1775 { NULL, NULL, 0, 0 }
1778 static struct demangle_component *
1779 d_operator_name (struct d_info *di)
1781 char c1;
1782 char c2;
1784 c1 = d_next_char (di);
1785 c2 = d_next_char (di);
1786 if (c1 == 'v' && IS_DIGIT (c2))
1787 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1788 else if (c1 == 'c' && c2 == 'v')
1790 struct demangle_component *type;
1791 int was_conversion = di->is_conversion;
1793 di->is_conversion = ! di->is_expression;
1794 type = cplus_demangle_type (di);
1795 di->is_conversion = was_conversion;
1796 return d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1798 else
1800 /* LOW is the inclusive lower bound. */
1801 int low = 0;
1802 /* HIGH is the exclusive upper bound. We subtract one to ignore
1803 the sentinel at the end of the array. */
1804 int high = ((sizeof (cplus_demangle_operators)
1805 / sizeof (cplus_demangle_operators[0]))
1806 - 1);
1808 while (1)
1810 int i;
1811 const struct demangle_operator_info *p;
1813 i = low + (high - low) / 2;
1814 p = cplus_demangle_operators + i;
1816 if (c1 == p->code[0] && c2 == p->code[1])
1817 return d_make_operator (di, p);
1819 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1820 high = i;
1821 else
1822 low = i + 1;
1823 if (low == high)
1824 return NULL;
1829 static struct demangle_component *
1830 d_make_character (struct d_info *di, int c)
1832 struct demangle_component *p;
1833 p = d_make_empty (di);
1834 if (p != NULL)
1836 p->type = DEMANGLE_COMPONENT_CHARACTER;
1837 p->u.s_character.character = c;
1839 return p;
1842 static struct demangle_component *
1843 d_java_resource (struct d_info *di)
1845 struct demangle_component *p = NULL;
1846 struct demangle_component *next = NULL;
1847 long len, i;
1848 char c;
1849 const char *str;
1851 len = d_number (di);
1852 if (len <= 1)
1853 return NULL;
1855 /* Eat the leading '_'. */
1856 if (d_next_char (di) != '_')
1857 return NULL;
1858 len--;
1860 str = d_str (di);
1861 i = 0;
1863 while (len > 0)
1865 c = str[i];
1866 if (!c)
1867 return NULL;
1869 /* Each chunk is either a '$' escape... */
1870 if (c == '$')
1872 i++;
1873 switch (str[i++])
1875 case 'S':
1876 c = '/';
1877 break;
1878 case '_':
1879 c = '.';
1880 break;
1881 case '$':
1882 c = '$';
1883 break;
1884 default:
1885 return NULL;
1887 next = d_make_character (di, c);
1888 d_advance (di, i);
1889 str = d_str (di);
1890 len -= i;
1891 i = 0;
1892 if (next == NULL)
1893 return NULL;
1895 /* ... or a sequence of characters. */
1896 else
1898 while (i < len && str[i] && str[i] != '$')
1899 i++;
1901 next = d_make_name (di, str, i);
1902 d_advance (di, i);
1903 str = d_str (di);
1904 len -= i;
1905 i = 0;
1906 if (next == NULL)
1907 return NULL;
1910 if (p == NULL)
1911 p = next;
1912 else
1914 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1915 if (p == NULL)
1916 return NULL;
1920 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1922 return p;
1925 /* <special-name> ::= TV <type>
1926 ::= TT <type>
1927 ::= TI <type>
1928 ::= TS <type>
1929 ::= GV <(object) name>
1930 ::= T <call-offset> <(base) encoding>
1931 ::= Tc <call-offset> <call-offset> <(base) encoding>
1932 Also g++ extensions:
1933 ::= TC <type> <(offset) number> _ <(base) type>
1934 ::= TF <type>
1935 ::= TJ <type>
1936 ::= GR <name>
1937 ::= GA <encoding>
1938 ::= Gr <resource name>
1939 ::= GTt <encoding>
1940 ::= GTn <encoding>
1943 static struct demangle_component *
1944 d_special_name (struct d_info *di)
1946 di->expansion += 20;
1947 if (d_check_char (di, 'T'))
1949 switch (d_next_char (di))
1951 case 'V':
1952 di->expansion -= 5;
1953 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1954 cplus_demangle_type (di), NULL);
1955 case 'T':
1956 di->expansion -= 10;
1957 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1958 cplus_demangle_type (di), NULL);
1959 case 'I':
1960 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1961 cplus_demangle_type (di), NULL);
1962 case 'S':
1963 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1964 cplus_demangle_type (di), NULL);
1966 case 'h':
1967 if (! d_call_offset (di, 'h'))
1968 return NULL;
1969 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1970 d_encoding (di, 0), NULL);
1972 case 'v':
1973 if (! d_call_offset (di, 'v'))
1974 return NULL;
1975 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1976 d_encoding (di, 0), NULL);
1978 case 'c':
1979 if (! d_call_offset (di, '\0'))
1980 return NULL;
1981 if (! d_call_offset (di, '\0'))
1982 return NULL;
1983 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1984 d_encoding (di, 0), NULL);
1986 case 'C':
1988 struct demangle_component *derived_type;
1989 long offset;
1990 struct demangle_component *base_type;
1992 derived_type = cplus_demangle_type (di);
1993 offset = d_number (di);
1994 if (offset < 0)
1995 return NULL;
1996 if (! d_check_char (di, '_'))
1997 return NULL;
1998 base_type = cplus_demangle_type (di);
1999 /* We don't display the offset. FIXME: We should display
2000 it in verbose mode. */
2001 di->expansion += 5;
2002 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2003 base_type, derived_type);
2006 case 'F':
2007 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2008 cplus_demangle_type (di), NULL);
2009 case 'J':
2010 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2011 cplus_demangle_type (di), NULL);
2013 case 'H':
2014 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2015 d_name (di), NULL);
2017 case 'W':
2018 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2019 d_name (di), NULL);
2021 default:
2022 return NULL;
2025 else if (d_check_char (di, 'G'))
2027 switch (d_next_char (di))
2029 case 'V':
2030 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2032 case 'R':
2034 struct demangle_component *name = d_name (di);
2035 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2036 d_number_component (di));
2039 case 'A':
2040 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2041 d_encoding (di, 0), NULL);
2043 case 'T':
2044 switch (d_next_char (di))
2046 case 'n':
2047 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2048 d_encoding (di, 0), NULL);
2049 default:
2050 /* ??? The proposal is that other letters (such as 'h') stand
2051 for different variants of transaction cloning, such as
2052 compiling directly for hardware transaction support. But
2053 they still should all be transactional clones of some sort
2054 so go ahead and call them that. */
2055 case 't':
2056 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2057 d_encoding (di, 0), NULL);
2060 case 'r':
2061 return d_java_resource (di);
2063 default:
2064 return NULL;
2067 else
2068 return NULL;
2071 /* <call-offset> ::= h <nv-offset> _
2072 ::= v <v-offset> _
2074 <nv-offset> ::= <(offset) number>
2076 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2078 The C parameter, if not '\0', is a character we just read which is
2079 the start of the <call-offset>.
2081 We don't display the offset information anywhere. FIXME: We should
2082 display it in verbose mode. */
2084 static int
2085 d_call_offset (struct d_info *di, int c)
2087 if (c == '\0')
2088 c = d_next_char (di);
2090 if (c == 'h')
2091 d_number (di);
2092 else if (c == 'v')
2094 d_number (di);
2095 if (! d_check_char (di, '_'))
2096 return 0;
2097 d_number (di);
2099 else
2100 return 0;
2102 if (! d_check_char (di, '_'))
2103 return 0;
2105 return 1;
2108 /* <ctor-dtor-name> ::= C1
2109 ::= C2
2110 ::= C3
2111 ::= D0
2112 ::= D1
2113 ::= D2
2116 static struct demangle_component *
2117 d_ctor_dtor_name (struct d_info *di)
2119 if (di->last_name != NULL)
2121 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2122 di->expansion += di->last_name->u.s_name.len;
2123 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2124 di->expansion += di->last_name->u.s_string.len;
2126 switch (d_peek_char (di))
2128 case 'C':
2130 enum gnu_v3_ctor_kinds kind;
2132 switch (d_peek_next_char (di))
2134 case '1':
2135 kind = gnu_v3_complete_object_ctor;
2136 break;
2137 case '2':
2138 kind = gnu_v3_base_object_ctor;
2139 break;
2140 case '3':
2141 kind = gnu_v3_complete_object_allocating_ctor;
2142 break;
2143 case '4':
2144 kind = gnu_v3_unified_ctor;
2145 break;
2146 case '5':
2147 kind = gnu_v3_object_ctor_group;
2148 break;
2149 default:
2150 return NULL;
2152 d_advance (di, 2);
2153 return d_make_ctor (di, kind, di->last_name);
2156 case 'D':
2158 enum gnu_v3_dtor_kinds kind;
2160 switch (d_peek_next_char (di))
2162 case '0':
2163 kind = gnu_v3_deleting_dtor;
2164 break;
2165 case '1':
2166 kind = gnu_v3_complete_object_dtor;
2167 break;
2168 case '2':
2169 kind = gnu_v3_base_object_dtor;
2170 break;
2171 /* digit '3' is not used */
2172 case '4':
2173 kind = gnu_v3_unified_dtor;
2174 break;
2175 case '5':
2176 kind = gnu_v3_object_dtor_group;
2177 break;
2178 default:
2179 return NULL;
2181 d_advance (di, 2);
2182 return d_make_dtor (di, kind, di->last_name);
2185 default:
2186 return NULL;
2190 /* <type> ::= <builtin-type>
2191 ::= <function-type>
2192 ::= <class-enum-type>
2193 ::= <array-type>
2194 ::= <pointer-to-member-type>
2195 ::= <template-param>
2196 ::= <template-template-param> <template-args>
2197 ::= <substitution>
2198 ::= <CV-qualifiers> <type>
2199 ::= P <type>
2200 ::= R <type>
2201 ::= O <type> (C++0x)
2202 ::= C <type>
2203 ::= G <type>
2204 ::= U <source-name> <type>
2206 <builtin-type> ::= various one letter codes
2207 ::= u <source-name>
2210 CP_STATIC_IF_GLIBCPP_V3
2211 const struct demangle_builtin_type_info
2212 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2214 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2215 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2216 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2217 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2218 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2219 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2220 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2221 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2222 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2223 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2224 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2225 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2226 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2227 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2228 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2229 D_PRINT_DEFAULT },
2230 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2231 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2232 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2233 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2234 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2235 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2236 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2237 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2238 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2239 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2240 D_PRINT_UNSIGNED_LONG_LONG },
2241 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2242 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2243 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2244 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2245 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2246 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2247 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2248 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2249 D_PRINT_DEFAULT },
2252 CP_STATIC_IF_GLIBCPP_V3
2253 struct demangle_component *
2254 cplus_demangle_type (struct d_info *di)
2256 char peek;
2257 struct demangle_component *ret;
2258 int can_subst;
2260 /* The ABI specifies that when CV-qualifiers are used, the base type
2261 is substitutable, and the fully qualified type is substitutable,
2262 but the base type with a strict subset of the CV-qualifiers is
2263 not substitutable. The natural recursive implementation of the
2264 CV-qualifiers would cause subsets to be substitutable, so instead
2265 we pull them all off now.
2267 FIXME: The ABI says that order-insensitive vendor qualifiers
2268 should be handled in the same way, but we have no way to tell
2269 which vendor qualifiers are order-insensitive and which are
2270 order-sensitive. So we just assume that they are all
2271 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2272 __vector, and it treats it as order-sensitive when mangling
2273 names. */
2275 peek = d_peek_char (di);
2276 if (peek == 'r' || peek == 'V' || peek == 'K')
2278 struct demangle_component **pret;
2280 pret = d_cv_qualifiers (di, &ret, 0);
2281 if (pret == NULL)
2282 return NULL;
2283 if (d_peek_char (di) == 'F')
2285 /* cv-qualifiers before a function type apply to 'this',
2286 so avoid adding the unqualified function type to
2287 the substitution list. */
2288 *pret = d_function_type (di);
2290 else
2291 *pret = cplus_demangle_type (di);
2292 if (!*pret)
2293 return NULL;
2294 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2295 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2297 /* Move the ref-qualifier outside the cv-qualifiers so that
2298 they are printed in the right order. */
2299 struct demangle_component *fn = d_left (*pret);
2300 d_left (*pret) = ret;
2301 ret = *pret;
2302 *pret = fn;
2304 if (! d_add_substitution (di, ret))
2305 return NULL;
2306 return ret;
2309 can_subst = 1;
2311 switch (peek)
2313 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2314 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2315 case 'o': case 's': case 't':
2316 case 'v': case 'w': case 'x': case 'y': case 'z':
2317 ret = d_make_builtin_type (di,
2318 &cplus_demangle_builtin_types[peek - 'a']);
2319 di->expansion += ret->u.s_builtin.type->len;
2320 can_subst = 0;
2321 d_advance (di, 1);
2322 break;
2324 case 'u':
2325 d_advance (di, 1);
2326 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2327 d_source_name (di), NULL);
2328 break;
2330 case 'F':
2331 ret = d_function_type (di);
2332 break;
2334 case '0': case '1': case '2': case '3': case '4':
2335 case '5': case '6': case '7': case '8': case '9':
2336 case 'N':
2337 case 'Z':
2338 ret = d_class_enum_type (di);
2339 break;
2341 case 'A':
2342 ret = d_array_type (di);
2343 break;
2345 case 'M':
2346 ret = d_pointer_to_member_type (di);
2347 break;
2349 case 'T':
2350 ret = d_template_param (di);
2351 if (d_peek_char (di) == 'I')
2353 /* This may be <template-template-param> <template-args>.
2354 If this is the type for a conversion operator, we can
2355 have a <template-template-param> here only by following
2356 a derivation like this:
2358 <nested-name>
2359 -> <template-prefix> <template-args>
2360 -> <prefix> <template-unqualified-name> <template-args>
2361 -> <unqualified-name> <template-unqualified-name> <template-args>
2362 -> <source-name> <template-unqualified-name> <template-args>
2363 -> <source-name> <operator-name> <template-args>
2364 -> <source-name> cv <type> <template-args>
2365 -> <source-name> cv <template-template-param> <template-args> <template-args>
2367 where the <template-args> is followed by another.
2368 Otherwise, we must have a derivation like this:
2370 <nested-name>
2371 -> <template-prefix> <template-args>
2372 -> <prefix> <template-unqualified-name> <template-args>
2373 -> <unqualified-name> <template-unqualified-name> <template-args>
2374 -> <source-name> <template-unqualified-name> <template-args>
2375 -> <source-name> <operator-name> <template-args>
2376 -> <source-name> cv <type> <template-args>
2377 -> <source-name> cv <template-param> <template-args>
2379 where we need to leave the <template-args> to be processed
2380 by d_prefix (following the <template-prefix>).
2382 The <template-template-param> part is a substitution
2383 candidate. */
2384 if (! di->is_conversion)
2386 if (! d_add_substitution (di, ret))
2387 return NULL;
2388 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2389 d_template_args (di));
2391 else
2393 struct demangle_component *args;
2394 struct d_info_checkpoint checkpoint;
2396 d_checkpoint (di, &checkpoint);
2397 args = d_template_args (di);
2398 if (d_peek_char (di) == 'I')
2400 if (! d_add_substitution (di, ret))
2401 return NULL;
2402 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2403 args);
2405 else
2406 d_backtrack (di, &checkpoint);
2409 break;
2411 case 'S':
2412 /* If this is a special substitution, then it is the start of
2413 <class-enum-type>. */
2415 char peek_next;
2417 peek_next = d_peek_next_char (di);
2418 if (IS_DIGIT (peek_next)
2419 || peek_next == '_'
2420 || IS_UPPER (peek_next))
2422 ret = d_substitution (di, 0);
2423 /* The substituted name may have been a template name and
2424 may be followed by tepmlate args. */
2425 if (d_peek_char (di) == 'I')
2426 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2427 d_template_args (di));
2428 else
2429 can_subst = 0;
2431 else
2433 ret = d_class_enum_type (di);
2434 /* If the substitution was a complete type, then it is not
2435 a new substitution candidate. However, if the
2436 substitution was followed by template arguments, then
2437 the whole thing is a substitution candidate. */
2438 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2439 can_subst = 0;
2442 break;
2444 case 'O':
2445 d_advance (di, 1);
2446 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2447 cplus_demangle_type (di), NULL);
2448 break;
2450 case 'P':
2451 d_advance (di, 1);
2452 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2453 cplus_demangle_type (di), NULL);
2454 break;
2456 case 'R':
2457 d_advance (di, 1);
2458 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2459 cplus_demangle_type (di), NULL);
2460 break;
2462 case 'C':
2463 d_advance (di, 1);
2464 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2465 cplus_demangle_type (di), NULL);
2466 break;
2468 case 'G':
2469 d_advance (di, 1);
2470 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2471 cplus_demangle_type (di), NULL);
2472 break;
2474 case 'U':
2475 d_advance (di, 1);
2476 ret = d_source_name (di);
2477 if (d_peek_char (di) == 'I')
2478 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2479 d_template_args (di));
2480 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2481 cplus_demangle_type (di), ret);
2482 break;
2484 case 'D':
2485 can_subst = 0;
2486 d_advance (di, 1);
2487 peek = d_next_char (di);
2488 switch (peek)
2490 case 'T':
2491 case 't':
2492 /* decltype (expression) */
2493 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2494 d_expression (di), NULL);
2495 if (ret && d_next_char (di) != 'E')
2496 ret = NULL;
2497 can_subst = 1;
2498 break;
2500 case 'p':
2501 /* Pack expansion. */
2502 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2503 cplus_demangle_type (di), NULL);
2504 can_subst = 1;
2505 break;
2507 case 'a':
2508 /* auto */
2509 ret = d_make_name (di, "auto", 4);
2510 break;
2512 case 'f':
2513 /* 32-bit decimal floating point */
2514 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2515 di->expansion += ret->u.s_builtin.type->len;
2516 break;
2517 case 'd':
2518 /* 64-bit DFP */
2519 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2520 di->expansion += ret->u.s_builtin.type->len;
2521 break;
2522 case 'e':
2523 /* 128-bit DFP */
2524 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2525 di->expansion += ret->u.s_builtin.type->len;
2526 break;
2527 case 'h':
2528 /* 16-bit half-precision FP */
2529 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2530 di->expansion += ret->u.s_builtin.type->len;
2531 break;
2532 case 's':
2533 /* char16_t */
2534 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2535 di->expansion += ret->u.s_builtin.type->len;
2536 break;
2537 case 'i':
2538 /* char32_t */
2539 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2540 di->expansion += ret->u.s_builtin.type->len;
2541 break;
2543 case 'F':
2544 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2545 ret = d_make_empty (di);
2546 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2547 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2548 /* For demangling we don't care about the bits. */
2549 d_number (di);
2550 ret->u.s_fixed.length = cplus_demangle_type (di);
2551 if (ret->u.s_fixed.length == NULL)
2552 return NULL;
2553 d_number (di);
2554 peek = d_next_char (di);
2555 ret->u.s_fixed.sat = (peek == 's');
2556 break;
2558 case 'v':
2559 ret = d_vector_type (di);
2560 can_subst = 1;
2561 break;
2563 case 'n':
2564 /* decltype(nullptr) */
2565 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2566 di->expansion += ret->u.s_builtin.type->len;
2567 break;
2569 default:
2570 return NULL;
2572 break;
2574 default:
2575 return NULL;
2578 if (can_subst)
2580 if (! d_add_substitution (di, ret))
2581 return NULL;
2584 return ret;
2587 /* <CV-qualifiers> ::= [r] [V] [K] */
2589 static struct demangle_component **
2590 d_cv_qualifiers (struct d_info *di,
2591 struct demangle_component **pret, int member_fn)
2593 struct demangle_component **pstart;
2594 char peek;
2596 pstart = pret;
2597 peek = d_peek_char (di);
2598 while (peek == 'r' || peek == 'V' || peek == 'K')
2600 enum demangle_component_type t;
2602 d_advance (di, 1);
2603 if (peek == 'r')
2605 t = (member_fn
2606 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2607 : DEMANGLE_COMPONENT_RESTRICT);
2608 di->expansion += sizeof "restrict";
2610 else if (peek == 'V')
2612 t = (member_fn
2613 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2614 : DEMANGLE_COMPONENT_VOLATILE);
2615 di->expansion += sizeof "volatile";
2617 else
2619 t = (member_fn
2620 ? DEMANGLE_COMPONENT_CONST_THIS
2621 : DEMANGLE_COMPONENT_CONST);
2622 di->expansion += sizeof "const";
2625 *pret = d_make_comp (di, t, NULL, NULL);
2626 if (*pret == NULL)
2627 return NULL;
2628 pret = &d_left (*pret);
2630 peek = d_peek_char (di);
2633 if (!member_fn && peek == 'F')
2635 while (pstart != pret)
2637 switch ((*pstart)->type)
2639 case DEMANGLE_COMPONENT_RESTRICT:
2640 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2641 break;
2642 case DEMANGLE_COMPONENT_VOLATILE:
2643 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2644 break;
2645 case DEMANGLE_COMPONENT_CONST:
2646 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2647 break;
2648 default:
2649 break;
2651 pstart = &d_left (*pstart);
2655 return pret;
2658 /* <ref-qualifier> ::= R
2659 ::= O */
2661 static struct demangle_component *
2662 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2664 struct demangle_component *ret = sub;
2665 char peek;
2667 peek = d_peek_char (di);
2668 if (peek == 'R' || peek == 'O')
2670 enum demangle_component_type t;
2671 if (peek == 'R')
2673 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2674 di->expansion += sizeof "&";
2676 else
2678 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2679 di->expansion += sizeof "&&";
2681 d_advance (di, 1);
2683 ret = d_make_comp (di, t, ret, NULL);
2686 return ret;
2689 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2691 static struct demangle_component *
2692 d_function_type (struct d_info *di)
2694 struct demangle_component *ret;
2696 if (! d_check_char (di, 'F'))
2697 return NULL;
2698 if (d_peek_char (di) == 'Y')
2700 /* Function has C linkage. We don't print this information.
2701 FIXME: We should print it in verbose mode. */
2702 d_advance (di, 1);
2704 ret = d_bare_function_type (di, 1);
2705 ret = d_ref_qualifier (di, ret);
2707 if (! d_check_char (di, 'E'))
2708 return NULL;
2709 return ret;
2712 /* <type>+ */
2714 static struct demangle_component *
2715 d_parmlist (struct d_info *di)
2717 struct demangle_component *tl;
2718 struct demangle_component **ptl;
2720 tl = NULL;
2721 ptl = &tl;
2722 while (1)
2724 struct demangle_component *type;
2726 char peek = d_peek_char (di);
2727 if (peek == '\0' || peek == 'E' || peek == '.')
2728 break;
2729 if ((peek == 'R' || peek == 'O')
2730 && d_peek_next_char (di) == 'E')
2731 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2732 break;
2733 type = cplus_demangle_type (di);
2734 if (type == NULL)
2735 return NULL;
2736 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2737 if (*ptl == NULL)
2738 return NULL;
2739 ptl = &d_right (*ptl);
2742 /* There should be at least one parameter type besides the optional
2743 return type. A function which takes no arguments will have a
2744 single parameter type void. */
2745 if (tl == NULL)
2746 return NULL;
2748 /* If we have a single parameter type void, omit it. */
2749 if (d_right (tl) == NULL
2750 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2751 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2753 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2754 d_left (tl) = NULL;
2757 return tl;
2760 /* <bare-function-type> ::= [J]<type>+ */
2762 static struct demangle_component *
2763 d_bare_function_type (struct d_info *di, int has_return_type)
2765 struct demangle_component *return_type;
2766 struct demangle_component *tl;
2767 char peek;
2769 /* Detect special qualifier indicating that the first argument
2770 is the return type. */
2771 peek = d_peek_char (di);
2772 if (peek == 'J')
2774 d_advance (di, 1);
2775 has_return_type = 1;
2778 if (has_return_type)
2780 return_type = cplus_demangle_type (di);
2781 if (return_type == NULL)
2782 return NULL;
2784 else
2785 return_type = NULL;
2787 tl = d_parmlist (di);
2788 if (tl == NULL)
2789 return NULL;
2791 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2792 return_type, tl);
2795 /* <class-enum-type> ::= <name> */
2797 static struct demangle_component *
2798 d_class_enum_type (struct d_info *di)
2800 return d_name (di);
2803 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2804 ::= A [<(dimension) expression>] _ <(element) type>
2807 static struct demangle_component *
2808 d_array_type (struct d_info *di)
2810 char peek;
2811 struct demangle_component *dim;
2813 if (! d_check_char (di, 'A'))
2814 return NULL;
2816 peek = d_peek_char (di);
2817 if (peek == '_')
2818 dim = NULL;
2819 else if (IS_DIGIT (peek))
2821 const char *s;
2823 s = d_str (di);
2826 d_advance (di, 1);
2827 peek = d_peek_char (di);
2829 while (IS_DIGIT (peek));
2830 dim = d_make_name (di, s, d_str (di) - s);
2831 if (dim == NULL)
2832 return NULL;
2834 else
2836 dim = d_expression (di);
2837 if (dim == NULL)
2838 return NULL;
2841 if (! d_check_char (di, '_'))
2842 return NULL;
2844 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2845 cplus_demangle_type (di));
2848 /* <vector-type> ::= Dv <number> _ <type>
2849 ::= Dv _ <expression> _ <type> */
2851 static struct demangle_component *
2852 d_vector_type (struct d_info *di)
2854 char peek;
2855 struct demangle_component *dim;
2857 peek = d_peek_char (di);
2858 if (peek == '_')
2860 d_advance (di, 1);
2861 dim = d_expression (di);
2863 else
2864 dim = d_number_component (di);
2866 if (dim == NULL)
2867 return NULL;
2869 if (! d_check_char (di, '_'))
2870 return NULL;
2872 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2873 cplus_demangle_type (di));
2876 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2878 static struct demangle_component *
2879 d_pointer_to_member_type (struct d_info *di)
2881 struct demangle_component *cl;
2882 struct demangle_component *mem;
2884 if (! d_check_char (di, 'M'))
2885 return NULL;
2887 cl = cplus_demangle_type (di);
2888 if (cl == NULL)
2889 return NULL;
2891 /* The ABI says, "The type of a non-static member function is considered
2892 to be different, for the purposes of substitution, from the type of a
2893 namespace-scope or static member function whose type appears
2894 similar. The types of two non-static member functions are considered
2895 to be different, for the purposes of substitution, if the functions
2896 are members of different classes. In other words, for the purposes of
2897 substitution, the class of which the function is a member is
2898 considered part of the type of function."
2900 For a pointer to member function, this call to cplus_demangle_type
2901 will end up adding a (possibly qualified) non-member function type to
2902 the substitution table, which is not correct; however, the member
2903 function type will never be used in a substitution, so putting the
2904 wrong type in the substitution table is harmless. */
2906 mem = cplus_demangle_type (di);
2907 if (mem == NULL)
2908 return NULL;
2910 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2913 /* <non-negative number> _ */
2915 static long
2916 d_compact_number (struct d_info *di)
2918 long num;
2919 if (d_peek_char (di) == '_')
2920 num = 0;
2921 else if (d_peek_char (di) == 'n')
2922 return -1;
2923 else
2924 num = d_number (di) + 1;
2926 if (! d_check_char (di, '_'))
2927 return -1;
2928 return num;
2931 /* <template-param> ::= T_
2932 ::= T <(parameter-2 non-negative) number> _
2935 static struct demangle_component *
2936 d_template_param (struct d_info *di)
2938 long param;
2940 if (! d_check_char (di, 'T'))
2941 return NULL;
2943 param = d_compact_number (di);
2944 if (param < 0)
2945 return NULL;
2947 ++di->did_subs;
2949 return d_make_template_param (di, param);
2952 /* <template-args> ::= I <template-arg>+ E */
2954 static struct demangle_component *
2955 d_template_args (struct d_info *di)
2957 struct demangle_component *hold_last_name;
2958 struct demangle_component *al;
2959 struct demangle_component **pal;
2961 /* Preserve the last name we saw--don't let the template arguments
2962 clobber it, as that would give us the wrong name for a subsequent
2963 constructor or destructor. */
2964 hold_last_name = di->last_name;
2966 if (d_peek_char (di) != 'I'
2967 && d_peek_char (di) != 'J')
2968 return NULL;
2969 d_advance (di, 1);
2971 if (d_peek_char (di) == 'E')
2973 /* An argument pack can be empty. */
2974 d_advance (di, 1);
2975 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2978 al = NULL;
2979 pal = &al;
2980 while (1)
2982 struct demangle_component *a;
2984 a = d_template_arg (di);
2985 if (a == NULL)
2986 return NULL;
2988 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2989 if (*pal == NULL)
2990 return NULL;
2991 pal = &d_right (*pal);
2993 if (d_peek_char (di) == 'E')
2995 d_advance (di, 1);
2996 break;
3000 di->last_name = hold_last_name;
3002 return al;
3005 /* <template-arg> ::= <type>
3006 ::= X <expression> E
3007 ::= <expr-primary>
3010 static struct demangle_component *
3011 d_template_arg (struct d_info *di)
3013 struct demangle_component *ret;
3015 switch (d_peek_char (di))
3017 case 'X':
3018 d_advance (di, 1);
3019 ret = d_expression (di);
3020 if (! d_check_char (di, 'E'))
3021 return NULL;
3022 return ret;
3024 case 'L':
3025 return d_expr_primary (di);
3027 case 'I':
3028 case 'J':
3029 /* An argument pack. */
3030 return d_template_args (di);
3032 default:
3033 return cplus_demangle_type (di);
3037 /* Parse a sequence of expressions until we hit the terminator
3038 character. */
3040 static struct demangle_component *
3041 d_exprlist (struct d_info *di, char terminator)
3043 struct demangle_component *list = NULL;
3044 struct demangle_component **p = &list;
3046 if (d_peek_char (di) == terminator)
3048 d_advance (di, 1);
3049 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3052 while (1)
3054 struct demangle_component *arg = d_expression (di);
3055 if (arg == NULL)
3056 return NULL;
3058 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3059 if (*p == NULL)
3060 return NULL;
3061 p = &d_right (*p);
3063 if (d_peek_char (di) == terminator)
3065 d_advance (di, 1);
3066 break;
3070 return list;
3073 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3074 dynamic_cast, static_cast or reinterpret_cast. */
3076 static int
3077 op_is_new_cast (struct demangle_component *op)
3079 const char *code = op->u.s_operator.op->code;
3080 return (code[1] == 'c'
3081 && (code[0] == 's' || code[0] == 'd'
3082 || code[0] == 'c' || code[0] == 'r'));
3085 /* <expression> ::= <(unary) operator-name> <expression>
3086 ::= <(binary) operator-name> <expression> <expression>
3087 ::= <(trinary) operator-name> <expression> <expression> <expression>
3088 ::= cl <expression>+ E
3089 ::= st <type>
3090 ::= <template-param>
3091 ::= sr <type> <unqualified-name>
3092 ::= sr <type> <unqualified-name> <template-args>
3093 ::= <expr-primary>
3096 static inline struct demangle_component *
3097 d_expression_1 (struct d_info *di)
3099 char peek;
3101 peek = d_peek_char (di);
3102 if (peek == 'L')
3103 return d_expr_primary (di);
3104 else if (peek == 'T')
3105 return d_template_param (di);
3106 else if (peek == 's' && d_peek_next_char (di) == 'r')
3108 struct demangle_component *type;
3109 struct demangle_component *name;
3111 d_advance (di, 2);
3112 type = cplus_demangle_type (di);
3113 name = d_unqualified_name (di);
3114 if (d_peek_char (di) != 'I')
3115 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3116 else
3117 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3118 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3119 d_template_args (di)));
3121 else if (peek == 's' && d_peek_next_char (di) == 'p')
3123 d_advance (di, 2);
3124 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3125 d_expression_1 (di), NULL);
3127 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3129 /* Function parameter used in a late-specified return type. */
3130 int index;
3131 d_advance (di, 2);
3132 if (d_peek_char (di) == 'T')
3134 /* 'this' parameter. */
3135 d_advance (di, 1);
3136 index = 0;
3138 else
3140 index = d_compact_number (di) + 1;
3141 if (index == 0)
3142 return NULL;
3144 return d_make_function_param (di, index);
3146 else if (IS_DIGIT (peek)
3147 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3149 /* We can get an unqualified name as an expression in the case of
3150 a dependent function call, i.e. decltype(f(t)). */
3151 struct demangle_component *name;
3153 if (peek == 'o')
3154 /* operator-function-id, i.e. operator+(t). */
3155 d_advance (di, 2);
3157 name = d_unqualified_name (di);
3158 if (name == NULL)
3159 return NULL;
3160 if (d_peek_char (di) == 'I')
3161 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3162 d_template_args (di));
3163 else
3164 return name;
3166 else if ((peek == 'i' || peek == 't')
3167 && d_peek_next_char (di) == 'l')
3169 /* Brace-enclosed initializer list, untyped or typed. */
3170 struct demangle_component *type = NULL;
3171 if (peek == 't')
3172 type = cplus_demangle_type (di);
3173 if (!d_peek_next_char (di))
3174 return NULL;
3175 d_advance (di, 2);
3176 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3177 type, d_exprlist (di, 'E'));
3179 else
3181 struct demangle_component *op;
3182 const char *code = NULL;
3183 int args;
3185 op = d_operator_name (di);
3186 if (op == NULL)
3187 return NULL;
3189 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3191 code = op->u.s_operator.op->code;
3192 di->expansion += op->u.s_operator.op->len - 2;
3193 if (strcmp (code, "st") == 0)
3194 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3195 cplus_demangle_type (di));
3198 switch (op->type)
3200 default:
3201 return NULL;
3202 case DEMANGLE_COMPONENT_OPERATOR:
3203 args = op->u.s_operator.op->args;
3204 break;
3205 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3206 args = op->u.s_extended_operator.args;
3207 break;
3208 case DEMANGLE_COMPONENT_CAST:
3209 args = 1;
3210 break;
3213 switch (args)
3215 case 0:
3216 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3218 case 1:
3220 struct demangle_component *operand;
3221 int suffix = 0;
3223 if (code && (code[0] == 'p' || code[0] == 'm')
3224 && code[1] == code[0])
3225 /* pp_ and mm_ are the prefix variants. */
3226 suffix = !d_check_char (di, '_');
3228 if (op->type == DEMANGLE_COMPONENT_CAST
3229 && d_check_char (di, '_'))
3230 operand = d_exprlist (di, 'E');
3231 else
3232 operand = d_expression_1 (di);
3234 if (suffix)
3235 /* Indicate the suffix variant for d_print_comp. */
3236 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3237 d_make_comp (di,
3238 DEMANGLE_COMPONENT_BINARY_ARGS,
3239 operand, operand));
3240 else
3241 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3242 operand);
3244 case 2:
3246 struct demangle_component *left;
3247 struct demangle_component *right;
3249 if (code == NULL)
3250 return NULL;
3251 if (op_is_new_cast (op))
3252 left = cplus_demangle_type (di);
3253 else
3254 left = d_expression_1 (di);
3255 if (!strcmp (code, "cl"))
3256 right = d_exprlist (di, 'E');
3257 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3259 right = d_unqualified_name (di);
3260 if (d_peek_char (di) == 'I')
3261 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3262 right, d_template_args (di));
3264 else
3265 right = d_expression_1 (di);
3267 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3268 d_make_comp (di,
3269 DEMANGLE_COMPONENT_BINARY_ARGS,
3270 left, right));
3272 case 3:
3274 struct demangle_component *first;
3275 struct demangle_component *second;
3276 struct demangle_component *third;
3278 if (code == NULL)
3279 return NULL;
3280 else if (!strcmp (code, "qu"))
3282 /* ?: expression. */
3283 first = d_expression_1 (di);
3284 second = d_expression_1 (di);
3285 third = d_expression_1 (di);
3287 else if (code[0] == 'n')
3289 /* new-expression. */
3290 if (code[1] != 'w' && code[1] != 'a')
3291 return NULL;
3292 first = d_exprlist (di, '_');
3293 second = cplus_demangle_type (di);
3294 if (d_peek_char (di) == 'E')
3296 d_advance (di, 1);
3297 third = NULL;
3299 else if (d_peek_char (di) == 'p'
3300 && d_peek_next_char (di) == 'i')
3302 /* Parenthesized initializer. */
3303 d_advance (di, 2);
3304 third = d_exprlist (di, 'E');
3306 else if (d_peek_char (di) == 'i'
3307 && d_peek_next_char (di) == 'l')
3308 /* initializer-list. */
3309 third = d_expression_1 (di);
3310 else
3311 return NULL;
3313 else
3314 return NULL;
3315 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3316 d_make_comp (di,
3317 DEMANGLE_COMPONENT_TRINARY_ARG1,
3318 first,
3319 d_make_comp (di,
3320 DEMANGLE_COMPONENT_TRINARY_ARG2,
3321 second, third)));
3323 default:
3324 return NULL;
3329 static struct demangle_component *
3330 d_expression (struct d_info *di)
3332 struct demangle_component *ret;
3333 int was_expression = di->is_expression;
3335 di->is_expression = 1;
3336 ret = d_expression_1 (di);
3337 di->is_expression = was_expression;
3338 return ret;
3341 /* <expr-primary> ::= L <type> <(value) number> E
3342 ::= L <type> <(value) float> E
3343 ::= L <mangled-name> E
3346 static struct demangle_component *
3347 d_expr_primary (struct d_info *di)
3349 struct demangle_component *ret;
3351 if (! d_check_char (di, 'L'))
3352 return NULL;
3353 if (d_peek_char (di) == '_'
3354 /* Workaround for G++ bug; see comment in write_template_arg. */
3355 || d_peek_char (di) == 'Z')
3356 ret = cplus_demangle_mangled_name (di, 0);
3357 else
3359 struct demangle_component *type;
3360 enum demangle_component_type t;
3361 const char *s;
3363 type = cplus_demangle_type (di);
3364 if (type == NULL)
3365 return NULL;
3367 /* If we have a type we know how to print, we aren't going to
3368 print the type name itself. */
3369 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3370 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3371 di->expansion -= type->u.s_builtin.type->len;
3373 /* Rather than try to interpret the literal value, we just
3374 collect it as a string. Note that it's possible to have a
3375 floating point literal here. The ABI specifies that the
3376 format of such literals is machine independent. That's fine,
3377 but what's not fine is that versions of g++ up to 3.2 with
3378 -fabi-version=1 used upper case letters in the hex constant,
3379 and dumped out gcc's internal representation. That makes it
3380 hard to tell where the constant ends, and hard to dump the
3381 constant in any readable form anyhow. We don't attempt to
3382 handle these cases. */
3384 t = DEMANGLE_COMPONENT_LITERAL;
3385 if (d_peek_char (di) == 'n')
3387 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3388 d_advance (di, 1);
3390 s = d_str (di);
3391 while (d_peek_char (di) != 'E')
3393 if (d_peek_char (di) == '\0')
3394 return NULL;
3395 d_advance (di, 1);
3397 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3399 if (! d_check_char (di, 'E'))
3400 return NULL;
3401 return ret;
3404 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3405 ::= Z <(function) encoding> E s [<discriminator>]
3406 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3409 static struct demangle_component *
3410 d_local_name (struct d_info *di)
3412 struct demangle_component *function;
3414 if (! d_check_char (di, 'Z'))
3415 return NULL;
3417 function = d_encoding (di, 0);
3419 if (! d_check_char (di, 'E'))
3420 return NULL;
3422 if (d_peek_char (di) == 's')
3424 d_advance (di, 1);
3425 if (! d_discriminator (di))
3426 return NULL;
3427 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3428 d_make_name (di, "string literal",
3429 sizeof "string literal" - 1));
3431 else
3433 struct demangle_component *name;
3434 int num = -1;
3436 if (d_peek_char (di) == 'd')
3438 /* Default argument scope: d <number> _. */
3439 d_advance (di, 1);
3440 num = d_compact_number (di);
3441 if (num < 0)
3442 return NULL;
3445 name = d_name (di);
3446 if (name)
3447 switch (name->type)
3449 /* Lambdas and unnamed types have internal discriminators. */
3450 case DEMANGLE_COMPONENT_LAMBDA:
3451 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3452 break;
3453 default:
3454 if (! d_discriminator (di))
3455 return NULL;
3457 if (num >= 0)
3458 name = d_make_default_arg (di, num, name);
3459 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3463 /* <discriminator> ::= _ <(non-negative) number>
3465 We demangle the discriminator, but we don't print it out. FIXME:
3466 We should print it out in verbose mode. */
3468 static int
3469 d_discriminator (struct d_info *di)
3471 long discrim;
3473 if (d_peek_char (di) != '_')
3474 return 1;
3475 d_advance (di, 1);
3476 discrim = d_number (di);
3477 if (discrim < 0)
3478 return 0;
3479 return 1;
3482 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3484 static struct demangle_component *
3485 d_lambda (struct d_info *di)
3487 struct demangle_component *tl;
3488 struct demangle_component *ret;
3489 int num;
3491 if (! d_check_char (di, 'U'))
3492 return NULL;
3493 if (! d_check_char (di, 'l'))
3494 return NULL;
3496 tl = d_parmlist (di);
3497 if (tl == NULL)
3498 return NULL;
3500 if (! d_check_char (di, 'E'))
3501 return NULL;
3503 num = d_compact_number (di);
3504 if (num < 0)
3505 return NULL;
3507 ret = d_make_empty (di);
3508 if (ret)
3510 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3511 ret->u.s_unary_num.sub = tl;
3512 ret->u.s_unary_num.num = num;
3515 if (! d_add_substitution (di, ret))
3516 return NULL;
3518 return ret;
3521 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3523 static struct demangle_component *
3524 d_unnamed_type (struct d_info *di)
3526 struct demangle_component *ret;
3527 long num;
3529 if (! d_check_char (di, 'U'))
3530 return NULL;
3531 if (! d_check_char (di, 't'))
3532 return NULL;
3534 num = d_compact_number (di);
3535 if (num < 0)
3536 return NULL;
3538 ret = d_make_empty (di);
3539 if (ret)
3541 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3542 ret->u.s_number.number = num;
3545 if (! d_add_substitution (di, ret))
3546 return NULL;
3548 return ret;
3551 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3554 static struct demangle_component *
3555 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3557 const char *suffix = d_str (di);
3558 const char *pend = suffix;
3559 struct demangle_component *n;
3561 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3563 pend += 2;
3564 while (IS_LOWER (*pend) || *pend == '_')
3565 ++pend;
3567 while (*pend == '.' && IS_DIGIT (pend[1]))
3569 pend += 2;
3570 while (IS_DIGIT (*pend))
3571 ++pend;
3573 d_advance (di, pend - suffix);
3574 n = d_make_name (di, suffix, pend - suffix);
3575 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3578 /* Add a new substitution. */
3580 static int
3581 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3583 if (dc == NULL)
3584 return 0;
3585 if (di->next_sub >= di->num_subs)
3586 return 0;
3587 di->subs[di->next_sub] = dc;
3588 ++di->next_sub;
3589 return 1;
3592 /* <substitution> ::= S <seq-id> _
3593 ::= S_
3594 ::= St
3595 ::= Sa
3596 ::= Sb
3597 ::= Ss
3598 ::= Si
3599 ::= So
3600 ::= Sd
3602 If PREFIX is non-zero, then this type is being used as a prefix in
3603 a qualified name. In this case, for the standard substitutions, we
3604 need to check whether we are being used as a prefix for a
3605 constructor or destructor, and return a full template name.
3606 Otherwise we will get something like std::iostream::~iostream()
3607 which does not correspond particularly well to any function which
3608 actually appears in the source.
3611 static const struct d_standard_sub_info standard_subs[] =
3613 { 't', NL ("std"),
3614 NL ("std"),
3615 NULL, 0 },
3616 { 'a', NL ("std::allocator"),
3617 NL ("std::allocator"),
3618 NL ("allocator") },
3619 { 'b', NL ("std::basic_string"),
3620 NL ("std::basic_string"),
3621 NL ("basic_string") },
3622 { 's', NL ("std::string"),
3623 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3624 NL ("basic_string") },
3625 { 'i', NL ("std::istream"),
3626 NL ("std::basic_istream<char, std::char_traits<char> >"),
3627 NL ("basic_istream") },
3628 { 'o', NL ("std::ostream"),
3629 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3630 NL ("basic_ostream") },
3631 { 'd', NL ("std::iostream"),
3632 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3633 NL ("basic_iostream") }
3636 static struct demangle_component *
3637 d_substitution (struct d_info *di, int prefix)
3639 char c;
3641 if (! d_check_char (di, 'S'))
3642 return NULL;
3644 c = d_next_char (di);
3645 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3647 unsigned int id;
3649 id = 0;
3650 if (c != '_')
3654 unsigned int new_id;
3656 if (IS_DIGIT (c))
3657 new_id = id * 36 + c - '0';
3658 else if (IS_UPPER (c))
3659 new_id = id * 36 + c - 'A' + 10;
3660 else
3661 return NULL;
3662 if (new_id < id)
3663 return NULL;
3664 id = new_id;
3665 c = d_next_char (di);
3667 while (c != '_');
3669 ++id;
3672 if (id >= (unsigned int) di->next_sub)
3673 return NULL;
3675 ++di->did_subs;
3677 return di->subs[id];
3679 else
3681 int verbose;
3682 const struct d_standard_sub_info *p;
3683 const struct d_standard_sub_info *pend;
3685 verbose = (di->options & DMGL_VERBOSE) != 0;
3686 if (! verbose && prefix)
3688 char peek;
3690 peek = d_peek_char (di);
3691 if (peek == 'C' || peek == 'D')
3692 verbose = 1;
3695 pend = (&standard_subs[0]
3696 + sizeof standard_subs / sizeof standard_subs[0]);
3697 for (p = &standard_subs[0]; p < pend; ++p)
3699 if (c == p->code)
3701 const char *s;
3702 int len;
3703 struct demangle_component *c;
3705 if (p->set_last_name != NULL)
3706 di->last_name = d_make_sub (di, p->set_last_name,
3707 p->set_last_name_len);
3708 if (verbose)
3710 s = p->full_expansion;
3711 len = p->full_len;
3713 else
3715 s = p->simple_expansion;
3716 len = p->simple_len;
3718 di->expansion += len;
3719 c = d_make_sub (di, s, len);
3720 if (d_peek_char (di) == 'B')
3722 /* If there are ABI tags on the abbreviation, it becomes
3723 a substitution candidate. */
3724 c = d_abi_tags (di, c);
3725 d_add_substitution (di, c);
3727 return c;
3731 return NULL;
3735 static void
3736 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3738 checkpoint->n = di->n;
3739 checkpoint->next_comp = di->next_comp;
3740 checkpoint->next_sub = di->next_sub;
3741 checkpoint->did_subs = di->did_subs;
3742 checkpoint->expansion = di->expansion;
3745 static void
3746 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3748 di->n = checkpoint->n;
3749 di->next_comp = checkpoint->next_comp;
3750 di->next_sub = checkpoint->next_sub;
3751 di->did_subs = checkpoint->did_subs;
3752 di->expansion = checkpoint->expansion;
3755 /* Initialize a growable string. */
3757 static void
3758 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3760 dgs->buf = NULL;
3761 dgs->len = 0;
3762 dgs->alc = 0;
3763 dgs->allocation_failure = 0;
3765 if (estimate > 0)
3766 d_growable_string_resize (dgs, estimate);
3769 /* Grow a growable string to a given size. */
3771 static inline void
3772 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3774 size_t newalc;
3775 char *newbuf;
3777 if (dgs->allocation_failure)
3778 return;
3780 /* Start allocation at two bytes to avoid any possibility of confusion
3781 with the special value of 1 used as a return in *palc to indicate
3782 allocation failures. */
3783 newalc = dgs->alc > 0 ? dgs->alc : 2;
3784 while (newalc < need)
3785 newalc <<= 1;
3787 newbuf = (char *) realloc (dgs->buf, newalc);
3788 if (newbuf == NULL)
3790 free (dgs->buf);
3791 dgs->buf = NULL;
3792 dgs->len = 0;
3793 dgs->alc = 0;
3794 dgs->allocation_failure = 1;
3795 return;
3797 dgs->buf = newbuf;
3798 dgs->alc = newalc;
3801 /* Append a buffer to a growable string. */
3803 static inline void
3804 d_growable_string_append_buffer (struct d_growable_string *dgs,
3805 const char *s, size_t l)
3807 size_t need;
3809 need = dgs->len + l + 1;
3810 if (need > dgs->alc)
3811 d_growable_string_resize (dgs, need);
3813 if (dgs->allocation_failure)
3814 return;
3816 memcpy (dgs->buf + dgs->len, s, l);
3817 dgs->buf[dgs->len + l] = '\0';
3818 dgs->len += l;
3821 /* Bridge growable strings to the callback mechanism. */
3823 static void
3824 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3826 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3828 d_growable_string_append_buffer (dgs, s, l);
3831 /* Walk the tree, counting the number of templates encountered, and
3832 the number of times a scope might be saved. These counts will be
3833 used to allocate data structures for d_print_comp, so the logic
3834 here must mirror the logic d_print_comp will use. It is not
3835 important that the resulting numbers are exact, so long as they
3836 are larger than the actual numbers encountered. */
3838 static void
3839 d_count_templates_scopes (int *num_templates, int *num_scopes,
3840 const struct demangle_component *dc)
3842 if (dc == NULL)
3843 return;
3845 switch (dc->type)
3847 case DEMANGLE_COMPONENT_NAME:
3848 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3849 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3850 case DEMANGLE_COMPONENT_SUB_STD:
3851 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3852 case DEMANGLE_COMPONENT_OPERATOR:
3853 case DEMANGLE_COMPONENT_CHARACTER:
3854 case DEMANGLE_COMPONENT_NUMBER:
3855 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3856 break;
3858 case DEMANGLE_COMPONENT_TEMPLATE:
3859 (*num_templates)++;
3860 goto recurse_left_right;
3862 case DEMANGLE_COMPONENT_REFERENCE:
3863 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3864 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
3865 (*num_scopes)++;
3866 goto recurse_left_right;
3868 case DEMANGLE_COMPONENT_QUAL_NAME:
3869 case DEMANGLE_COMPONENT_LOCAL_NAME:
3870 case DEMANGLE_COMPONENT_TYPED_NAME:
3871 case DEMANGLE_COMPONENT_VTABLE:
3872 case DEMANGLE_COMPONENT_VTT:
3873 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3874 case DEMANGLE_COMPONENT_TYPEINFO:
3875 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3876 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3877 case DEMANGLE_COMPONENT_THUNK:
3878 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3879 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3880 case DEMANGLE_COMPONENT_JAVA_CLASS:
3881 case DEMANGLE_COMPONENT_GUARD:
3882 case DEMANGLE_COMPONENT_TLS_INIT:
3883 case DEMANGLE_COMPONENT_TLS_WRAPPER:
3884 case DEMANGLE_COMPONENT_REFTEMP:
3885 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3886 case DEMANGLE_COMPONENT_RESTRICT:
3887 case DEMANGLE_COMPONENT_VOLATILE:
3888 case DEMANGLE_COMPONENT_CONST:
3889 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3890 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3891 case DEMANGLE_COMPONENT_CONST_THIS:
3892 case DEMANGLE_COMPONENT_REFERENCE_THIS:
3893 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
3894 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3895 case DEMANGLE_COMPONENT_POINTER:
3896 case DEMANGLE_COMPONENT_COMPLEX:
3897 case DEMANGLE_COMPONENT_IMAGINARY:
3898 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3899 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3900 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3901 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3902 case DEMANGLE_COMPONENT_VECTOR_TYPE:
3903 case DEMANGLE_COMPONENT_ARGLIST:
3904 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3905 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
3906 case DEMANGLE_COMPONENT_CAST:
3907 case DEMANGLE_COMPONENT_NULLARY:
3908 case DEMANGLE_COMPONENT_UNARY:
3909 case DEMANGLE_COMPONENT_BINARY:
3910 case DEMANGLE_COMPONENT_BINARY_ARGS:
3911 case DEMANGLE_COMPONENT_TRINARY:
3912 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3913 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3914 case DEMANGLE_COMPONENT_LITERAL:
3915 case DEMANGLE_COMPONENT_LITERAL_NEG:
3916 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3917 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3918 case DEMANGLE_COMPONENT_DECLTYPE:
3919 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
3920 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
3921 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3922 case DEMANGLE_COMPONENT_TAGGED_NAME:
3923 case DEMANGLE_COMPONENT_CLONE:
3924 recurse_left_right:
3925 d_count_templates_scopes (num_templates, num_scopes,
3926 d_left (dc));
3927 d_count_templates_scopes (num_templates, num_scopes,
3928 d_right (dc));
3929 break;
3931 case DEMANGLE_COMPONENT_CTOR:
3932 d_count_templates_scopes (num_templates, num_scopes,
3933 dc->u.s_ctor.name);
3934 break;
3936 case DEMANGLE_COMPONENT_DTOR:
3937 d_count_templates_scopes (num_templates, num_scopes,
3938 dc->u.s_dtor.name);
3939 break;
3941 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3942 d_count_templates_scopes (num_templates, num_scopes,
3943 dc->u.s_extended_operator.name);
3944 break;
3946 case DEMANGLE_COMPONENT_FIXED_TYPE:
3947 d_count_templates_scopes (num_templates, num_scopes,
3948 dc->u.s_fixed.length);
3949 break;
3951 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
3952 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
3953 d_count_templates_scopes (num_templates, num_scopes,
3954 d_left (dc));
3955 break;
3957 case DEMANGLE_COMPONENT_LAMBDA:
3958 case DEMANGLE_COMPONENT_DEFAULT_ARG:
3959 d_count_templates_scopes (num_templates, num_scopes,
3960 dc->u.s_unary_num.sub);
3961 break;
3965 /* Initialize a print information structure. */
3967 static void
3968 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3969 void *opaque, const struct demangle_component *dc)
3971 dpi->len = 0;
3972 dpi->last_char = '\0';
3973 dpi->templates = NULL;
3974 dpi->modifiers = NULL;
3975 dpi->pack_index = 0;
3976 dpi->flush_count = 0;
3978 dpi->callback = callback;
3979 dpi->opaque = opaque;
3981 dpi->demangle_failure = 0;
3983 dpi->component_stack = NULL;
3985 dpi->saved_scopes = NULL;
3986 dpi->next_saved_scope = 0;
3987 dpi->num_saved_scopes = 0;
3989 dpi->copy_templates = NULL;
3990 dpi->next_copy_template = 0;
3991 dpi->num_copy_templates = 0;
3993 d_count_templates_scopes (&dpi->num_copy_templates,
3994 &dpi->num_saved_scopes, dc);
3995 dpi->num_copy_templates *= dpi->num_saved_scopes;
3997 dpi->current_template = NULL;
4000 /* Indicate that an error occurred during printing, and test for error. */
4002 static inline void
4003 d_print_error (struct d_print_info *dpi)
4005 dpi->demangle_failure = 1;
4008 static inline int
4009 d_print_saw_error (struct d_print_info *dpi)
4011 return dpi->demangle_failure != 0;
4014 /* Flush buffered characters to the callback. */
4016 static inline void
4017 d_print_flush (struct d_print_info *dpi)
4019 dpi->buf[dpi->len] = '\0';
4020 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4021 dpi->len = 0;
4022 dpi->flush_count++;
4025 /* Append characters and buffers for printing. */
4027 static inline void
4028 d_append_char (struct d_print_info *dpi, char c)
4030 if (dpi->len == sizeof (dpi->buf) - 1)
4031 d_print_flush (dpi);
4033 dpi->buf[dpi->len++] = c;
4034 dpi->last_char = c;
4037 static inline void
4038 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4040 size_t i;
4042 for (i = 0; i < l; i++)
4043 d_append_char (dpi, s[i]);
4046 static inline void
4047 d_append_string (struct d_print_info *dpi, const char *s)
4049 d_append_buffer (dpi, s, strlen (s));
4052 static inline void
4053 d_append_num (struct d_print_info *dpi, long l)
4055 char buf[25];
4056 sprintf (buf,"%ld", l);
4057 d_append_string (dpi, buf);
4060 static inline char
4061 d_last_char (struct d_print_info *dpi)
4063 return dpi->last_char;
4066 /* Turn components into a human readable string. OPTIONS is the
4067 options bits passed to the demangler. DC is the tree to print.
4068 CALLBACK is a function to call to flush demangled string segments
4069 as they fill the intermediate buffer, and OPAQUE is a generalized
4070 callback argument. On success, this returns 1. On failure,
4071 it returns 0, indicating a bad parse. It does not use heap
4072 memory to build an output string, so cannot encounter memory
4073 allocation failure. */
4075 CP_STATIC_IF_GLIBCPP_V3
4077 cplus_demangle_print_callback (int options,
4078 const struct demangle_component *dc,
4079 demangle_callbackref callback, void *opaque)
4081 struct d_print_info dpi;
4083 d_print_init (&dpi, callback, opaque, dc);
4086 #ifdef CP_DYNAMIC_ARRAYS
4087 __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
4088 __extension__ struct d_print_template temps[dpi.num_copy_templates];
4090 dpi.saved_scopes = scopes;
4091 dpi.copy_templates = temps;
4092 #else
4093 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4094 * sizeof (*dpi.saved_scopes));
4095 dpi.copy_templates = alloca (dpi.num_copy_templates
4096 * sizeof (*dpi.copy_templates));
4097 #endif
4099 d_print_comp (&dpi, options, dc);
4102 d_print_flush (&dpi);
4104 return ! d_print_saw_error (&dpi);
4107 /* Turn components into a human readable string. OPTIONS is the
4108 options bits passed to the demangler. DC is the tree to print.
4109 ESTIMATE is a guess at the length of the result. This returns a
4110 string allocated by malloc, or NULL on error. On success, this
4111 sets *PALC to the size of the allocated buffer. On failure, this
4112 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4113 failure. */
4115 CP_STATIC_IF_GLIBCPP_V3
4116 char *
4117 cplus_demangle_print (int options, const struct demangle_component *dc,
4118 int estimate, size_t *palc)
4120 struct d_growable_string dgs;
4122 d_growable_string_init (&dgs, estimate);
4124 if (! cplus_demangle_print_callback (options, dc,
4125 d_growable_string_callback_adapter,
4126 &dgs))
4128 free (dgs.buf);
4129 *palc = 0;
4130 return NULL;
4133 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4134 return dgs.buf;
4137 /* Returns the I'th element of the template arglist ARGS, or NULL on
4138 failure. */
4140 static struct demangle_component *
4141 d_index_template_argument (struct demangle_component *args, int i)
4143 struct demangle_component *a;
4145 for (a = args;
4146 a != NULL;
4147 a = d_right (a))
4149 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4150 return NULL;
4151 if (i <= 0)
4152 break;
4153 --i;
4155 if (i != 0 || a == NULL)
4156 return NULL;
4158 return d_left (a);
4161 /* Returns the template argument from the current context indicated by DC,
4162 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4164 static struct demangle_component *
4165 d_lookup_template_argument (struct d_print_info *dpi,
4166 const struct demangle_component *dc)
4168 if (dpi->templates == NULL)
4170 d_print_error (dpi);
4171 return NULL;
4174 return d_index_template_argument
4175 (d_right (dpi->templates->template_decl),
4176 dc->u.s_number.number);
4179 /* Returns a template argument pack used in DC (any will do), or NULL. */
4181 static struct demangle_component *
4182 d_find_pack (struct d_print_info *dpi,
4183 const struct demangle_component *dc)
4185 struct demangle_component *a;
4186 if (dc == NULL)
4187 return NULL;
4189 switch (dc->type)
4191 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4192 a = d_lookup_template_argument (dpi, dc);
4193 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4194 return a;
4195 return NULL;
4197 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4198 return NULL;
4200 case DEMANGLE_COMPONENT_LAMBDA:
4201 case DEMANGLE_COMPONENT_NAME:
4202 case DEMANGLE_COMPONENT_TAGGED_NAME:
4203 case DEMANGLE_COMPONENT_OPERATOR:
4204 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4205 case DEMANGLE_COMPONENT_SUB_STD:
4206 case DEMANGLE_COMPONENT_CHARACTER:
4207 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4208 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4209 case DEMANGLE_COMPONENT_FIXED_TYPE:
4210 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4211 case DEMANGLE_COMPONENT_NUMBER:
4212 return NULL;
4214 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4215 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4216 case DEMANGLE_COMPONENT_CTOR:
4217 return d_find_pack (dpi, dc->u.s_ctor.name);
4218 case DEMANGLE_COMPONENT_DTOR:
4219 return d_find_pack (dpi, dc->u.s_dtor.name);
4221 default:
4222 a = d_find_pack (dpi, d_left (dc));
4223 if (a)
4224 return a;
4225 return d_find_pack (dpi, d_right (dc));
4229 /* Returns the length of the template argument pack DC. */
4231 static int
4232 d_pack_length (const struct demangle_component *dc)
4234 int count = 0;
4235 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4236 && d_left (dc) != NULL)
4238 ++count;
4239 dc = d_right (dc);
4241 return count;
4244 /* DC is a component of a mangled expression. Print it, wrapped in parens
4245 if needed. */
4247 static void
4248 d_print_subexpr (struct d_print_info *dpi, int options,
4249 const struct demangle_component *dc)
4251 int simple = 0;
4252 if (dc->type == DEMANGLE_COMPONENT_NAME
4253 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4254 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4255 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4256 simple = 1;
4257 if (!simple)
4258 d_append_char (dpi, '(');
4259 d_print_comp (dpi, options, dc);
4260 if (!simple)
4261 d_append_char (dpi, ')');
4264 /* Save the current scope. */
4266 static void
4267 d_save_scope (struct d_print_info *dpi,
4268 const struct demangle_component *container)
4270 struct d_saved_scope *scope;
4271 struct d_print_template *src, **link;
4273 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4275 d_print_error (dpi);
4276 return;
4278 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4279 dpi->next_saved_scope++;
4281 scope->container = container;
4282 link = &scope->templates;
4284 for (src = dpi->templates; src != NULL; src = src->next)
4286 struct d_print_template *dst;
4288 if (dpi->next_copy_template >= dpi->num_copy_templates)
4290 d_print_error (dpi);
4291 return;
4293 dst = &dpi->copy_templates[dpi->next_copy_template];
4294 dpi->next_copy_template++;
4296 dst->template_decl = src->template_decl;
4297 *link = dst;
4298 link = &dst->next;
4301 *link = NULL;
4304 /* Attempt to locate a previously saved scope. Returns NULL if no
4305 corresponding saved scope was found. */
4307 static struct d_saved_scope *
4308 d_get_saved_scope (struct d_print_info *dpi,
4309 const struct demangle_component *container)
4311 int i;
4313 for (i = 0; i < dpi->next_saved_scope; i++)
4314 if (dpi->saved_scopes[i].container == container)
4315 return &dpi->saved_scopes[i];
4317 return NULL;
4320 /* Subroutine to handle components. */
4322 static void
4323 d_print_comp_inner (struct d_print_info *dpi, int options,
4324 const struct demangle_component *dc)
4326 /* Magic variable to let reference smashing skip over the next modifier
4327 without needing to modify *dc. */
4328 const struct demangle_component *mod_inner = NULL;
4330 /* Variable used to store the current templates while a previously
4331 captured scope is used. */
4332 struct d_print_template *saved_templates;
4334 /* Nonzero if templates have been stored in the above variable. */
4335 int need_template_restore = 0;
4337 if (dc == NULL)
4339 d_print_error (dpi);
4340 return;
4342 if (d_print_saw_error (dpi))
4343 return;
4345 switch (dc->type)
4347 case DEMANGLE_COMPONENT_NAME:
4348 if ((options & DMGL_JAVA) == 0)
4349 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4350 else
4351 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4352 return;
4354 case DEMANGLE_COMPONENT_TAGGED_NAME:
4355 d_print_comp (dpi, options, d_left (dc));
4356 d_append_string (dpi, "[abi:");
4357 d_print_comp (dpi, options, d_right (dc));
4358 d_append_char (dpi, ']');
4359 return;
4361 case DEMANGLE_COMPONENT_QUAL_NAME:
4362 case DEMANGLE_COMPONENT_LOCAL_NAME:
4363 d_print_comp (dpi, options, d_left (dc));
4364 if ((options & DMGL_JAVA) == 0)
4365 d_append_string (dpi, "::");
4366 else
4367 d_append_char (dpi, '.');
4369 struct demangle_component *local_name = d_right (dc);
4370 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4372 d_append_string (dpi, "{default arg#");
4373 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4374 d_append_string (dpi, "}::");
4375 local_name = local_name->u.s_unary_num.sub;
4377 d_print_comp (dpi, options, local_name);
4379 return;
4381 case DEMANGLE_COMPONENT_TYPED_NAME:
4383 struct d_print_mod *hold_modifiers;
4384 struct demangle_component *typed_name;
4385 struct d_print_mod adpm[4];
4386 unsigned int i;
4387 struct d_print_template dpt;
4389 /* Pass the name down to the type so that it can be printed in
4390 the right place for the type. We also have to pass down
4391 any CV-qualifiers, which apply to the this parameter. */
4392 hold_modifiers = dpi->modifiers;
4393 dpi->modifiers = 0;
4394 i = 0;
4395 typed_name = d_left (dc);
4396 while (typed_name != NULL)
4398 if (i >= sizeof adpm / sizeof adpm[0])
4400 d_print_error (dpi);
4401 return;
4404 adpm[i].next = dpi->modifiers;
4405 dpi->modifiers = &adpm[i];
4406 adpm[i].mod = typed_name;
4407 adpm[i].printed = 0;
4408 adpm[i].templates = dpi->templates;
4409 ++i;
4411 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4412 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4413 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4414 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4415 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4416 break;
4418 typed_name = d_left (typed_name);
4421 if (typed_name == NULL)
4423 d_print_error (dpi);
4424 return;
4427 /* If typed_name is a template, then it applies to the
4428 function type as well. */
4429 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4431 dpt.next = dpi->templates;
4432 dpi->templates = &dpt;
4433 dpt.template_decl = typed_name;
4436 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4437 there may be CV-qualifiers on its right argument which
4438 really apply here; this happens when parsing a class which
4439 is local to a function. */
4440 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4442 struct demangle_component *local_name;
4444 local_name = d_right (typed_name);
4445 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4446 local_name = local_name->u.s_unary_num.sub;
4447 if (local_name == NULL)
4449 d_print_error (dpi);
4450 return;
4452 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4453 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4454 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4455 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4456 || (local_name->type
4457 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4459 if (i >= sizeof adpm / sizeof adpm[0])
4461 d_print_error (dpi);
4462 return;
4465 adpm[i] = adpm[i - 1];
4466 adpm[i].next = &adpm[i - 1];
4467 dpi->modifiers = &adpm[i];
4469 adpm[i - 1].mod = local_name;
4470 adpm[i - 1].printed = 0;
4471 adpm[i - 1].templates = dpi->templates;
4472 ++i;
4474 local_name = d_left (local_name);
4478 d_print_comp (dpi, options, d_right (dc));
4480 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4481 dpi->templates = dpt.next;
4483 /* If the modifiers didn't get printed by the type, print them
4484 now. */
4485 while (i > 0)
4487 --i;
4488 if (! adpm[i].printed)
4490 d_append_char (dpi, ' ');
4491 d_print_mod (dpi, options, adpm[i].mod);
4495 dpi->modifiers = hold_modifiers;
4497 return;
4500 case DEMANGLE_COMPONENT_TEMPLATE:
4502 struct d_print_mod *hold_dpm;
4503 struct demangle_component *dcl;
4504 const struct demangle_component *hold_current;
4506 /* This template may need to be referenced by a cast operator
4507 contained in its subtree. */
4508 hold_current = dpi->current_template;
4509 dpi->current_template = dc;
4511 /* Don't push modifiers into a template definition. Doing so
4512 could give the wrong definition for a template argument.
4513 Instead, treat the template essentially as a name. */
4515 hold_dpm = dpi->modifiers;
4516 dpi->modifiers = NULL;
4518 dcl = d_left (dc);
4520 if ((options & DMGL_JAVA) != 0
4521 && dcl->type == DEMANGLE_COMPONENT_NAME
4522 && dcl->u.s_name.len == 6
4523 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4525 /* Special-case Java arrays, so that JArray<TYPE> appears
4526 instead as TYPE[]. */
4528 d_print_comp (dpi, options, d_right (dc));
4529 d_append_string (dpi, "[]");
4531 else
4533 d_print_comp (dpi, options, dcl);
4534 if (d_last_char (dpi) == '<')
4535 d_append_char (dpi, ' ');
4536 d_append_char (dpi, '<');
4537 d_print_comp (dpi, options, d_right (dc));
4538 /* Avoid generating two consecutive '>' characters, to avoid
4539 the C++ syntactic ambiguity. */
4540 if (d_last_char (dpi) == '>')
4541 d_append_char (dpi, ' ');
4542 d_append_char (dpi, '>');
4545 dpi->modifiers = hold_dpm;
4546 dpi->current_template = hold_current;
4548 return;
4551 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4553 struct d_print_template *hold_dpt;
4554 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4556 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4557 a = d_index_template_argument (a, dpi->pack_index);
4559 if (a == NULL)
4561 d_print_error (dpi);
4562 return;
4565 /* While processing this parameter, we need to pop the list of
4566 templates. This is because the template parameter may
4567 itself be a reference to a parameter of an outer
4568 template. */
4570 hold_dpt = dpi->templates;
4571 dpi->templates = hold_dpt->next;
4573 d_print_comp (dpi, options, a);
4575 dpi->templates = hold_dpt;
4577 return;
4580 case DEMANGLE_COMPONENT_CTOR:
4581 d_print_comp (dpi, options, dc->u.s_ctor.name);
4582 return;
4584 case DEMANGLE_COMPONENT_DTOR:
4585 d_append_char (dpi, '~');
4586 d_print_comp (dpi, options, dc->u.s_dtor.name);
4587 return;
4589 case DEMANGLE_COMPONENT_VTABLE:
4590 d_append_string (dpi, "vtable for ");
4591 d_print_comp (dpi, options, d_left (dc));
4592 return;
4594 case DEMANGLE_COMPONENT_VTT:
4595 d_append_string (dpi, "VTT for ");
4596 d_print_comp (dpi, options, d_left (dc));
4597 return;
4599 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4600 d_append_string (dpi, "construction vtable for ");
4601 d_print_comp (dpi, options, d_left (dc));
4602 d_append_string (dpi, "-in-");
4603 d_print_comp (dpi, options, d_right (dc));
4604 return;
4606 case DEMANGLE_COMPONENT_TYPEINFO:
4607 d_append_string (dpi, "typeinfo for ");
4608 d_print_comp (dpi, options, d_left (dc));
4609 return;
4611 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4612 d_append_string (dpi, "typeinfo name for ");
4613 d_print_comp (dpi, options, d_left (dc));
4614 return;
4616 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4617 d_append_string (dpi, "typeinfo fn for ");
4618 d_print_comp (dpi, options, d_left (dc));
4619 return;
4621 case DEMANGLE_COMPONENT_THUNK:
4622 d_append_string (dpi, "non-virtual thunk to ");
4623 d_print_comp (dpi, options, d_left (dc));
4624 return;
4626 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4627 d_append_string (dpi, "virtual thunk to ");
4628 d_print_comp (dpi, options, d_left (dc));
4629 return;
4631 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4632 d_append_string (dpi, "covariant return thunk to ");
4633 d_print_comp (dpi, options, d_left (dc));
4634 return;
4636 case DEMANGLE_COMPONENT_JAVA_CLASS:
4637 d_append_string (dpi, "java Class for ");
4638 d_print_comp (dpi, options, d_left (dc));
4639 return;
4641 case DEMANGLE_COMPONENT_GUARD:
4642 d_append_string (dpi, "guard variable for ");
4643 d_print_comp (dpi, options, d_left (dc));
4644 return;
4646 case DEMANGLE_COMPONENT_TLS_INIT:
4647 d_append_string (dpi, "TLS init function for ");
4648 d_print_comp (dpi, options, d_left (dc));
4649 return;
4651 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4652 d_append_string (dpi, "TLS wrapper function for ");
4653 d_print_comp (dpi, options, d_left (dc));
4654 return;
4656 case DEMANGLE_COMPONENT_REFTEMP:
4657 d_append_string (dpi, "reference temporary #");
4658 d_print_comp (dpi, options, d_right (dc));
4659 d_append_string (dpi, " for ");
4660 d_print_comp (dpi, options, d_left (dc));
4661 return;
4663 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4664 d_append_string (dpi, "hidden alias for ");
4665 d_print_comp (dpi, options, d_left (dc));
4666 return;
4668 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4669 d_append_string (dpi, "transaction clone for ");
4670 d_print_comp (dpi, options, d_left (dc));
4671 return;
4673 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4674 d_append_string (dpi, "non-transaction clone for ");
4675 d_print_comp (dpi, options, d_left (dc));
4676 return;
4678 case DEMANGLE_COMPONENT_SUB_STD:
4679 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4680 return;
4682 case DEMANGLE_COMPONENT_RESTRICT:
4683 case DEMANGLE_COMPONENT_VOLATILE:
4684 case DEMANGLE_COMPONENT_CONST:
4686 struct d_print_mod *pdpm;
4688 /* When printing arrays, it's possible to have cases where the
4689 same CV-qualifier gets pushed on the stack multiple times.
4690 We only need to print it once. */
4692 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4694 if (! pdpm->printed)
4696 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4697 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4698 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4699 break;
4700 if (pdpm->mod->type == dc->type)
4702 d_print_comp (dpi, options, d_left (dc));
4703 return;
4708 goto modifier;
4710 case DEMANGLE_COMPONENT_REFERENCE:
4711 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4713 /* Handle reference smashing: & + && = &. */
4714 const struct demangle_component *sub = d_left (dc);
4715 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4717 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4718 struct demangle_component *a;
4720 if (scope == NULL)
4722 /* This is the first time SUB has been traversed.
4723 We need to capture the current templates so
4724 they can be restored if SUB is reentered as a
4725 substitution. */
4726 d_save_scope (dpi, sub);
4727 if (d_print_saw_error (dpi))
4728 return;
4730 else
4732 const struct d_component_stack *dcse;
4733 int found_self_or_parent = 0;
4735 /* This traversal is reentering SUB as a substition.
4736 If we are not beneath SUB or DC in the tree then we
4737 need to restore SUB's template stack temporarily. */
4738 for (dcse = dpi->component_stack; dcse != NULL;
4739 dcse = dcse->parent)
4741 if (dcse->dc == sub
4742 || (dcse->dc == dc
4743 && dcse != dpi->component_stack))
4745 found_self_or_parent = 1;
4746 break;
4750 if (!found_self_or_parent)
4752 saved_templates = dpi->templates;
4753 dpi->templates = scope->templates;
4754 need_template_restore = 1;
4758 a = d_lookup_template_argument (dpi, sub);
4759 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4760 a = d_index_template_argument (a, dpi->pack_index);
4762 if (a == NULL)
4764 if (need_template_restore)
4765 dpi->templates = saved_templates;
4767 d_print_error (dpi);
4768 return;
4771 sub = a;
4774 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4775 || sub->type == dc->type)
4776 dc = sub;
4777 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4778 mod_inner = d_left (sub);
4780 /* Fall through. */
4782 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4783 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4784 case DEMANGLE_COMPONENT_CONST_THIS:
4785 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4786 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4787 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4788 case DEMANGLE_COMPONENT_POINTER:
4789 case DEMANGLE_COMPONENT_COMPLEX:
4790 case DEMANGLE_COMPONENT_IMAGINARY:
4791 modifier:
4793 /* We keep a list of modifiers on the stack. */
4794 struct d_print_mod dpm;
4796 dpm.next = dpi->modifiers;
4797 dpi->modifiers = &dpm;
4798 dpm.mod = dc;
4799 dpm.printed = 0;
4800 dpm.templates = dpi->templates;
4802 if (!mod_inner)
4803 mod_inner = d_left (dc);
4805 d_print_comp (dpi, options, mod_inner);
4807 /* If the modifier didn't get printed by the type, print it
4808 now. */
4809 if (! dpm.printed)
4810 d_print_mod (dpi, options, dc);
4812 dpi->modifiers = dpm.next;
4814 if (need_template_restore)
4815 dpi->templates = saved_templates;
4817 return;
4820 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4821 if ((options & DMGL_JAVA) == 0)
4822 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4823 dc->u.s_builtin.type->len);
4824 else
4825 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4826 dc->u.s_builtin.type->java_len);
4827 return;
4829 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4830 d_print_comp (dpi, options, d_left (dc));
4831 return;
4833 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4835 if ((options & DMGL_RET_POSTFIX) != 0)
4836 d_print_function_type (dpi,
4837 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4838 dc, dpi->modifiers);
4840 /* Print return type if present */
4841 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4842 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4843 d_left (dc));
4844 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4846 struct d_print_mod dpm;
4848 /* We must pass this type down as a modifier in order to
4849 print it in the right location. */
4850 dpm.next = dpi->modifiers;
4851 dpi->modifiers = &dpm;
4852 dpm.mod = dc;
4853 dpm.printed = 0;
4854 dpm.templates = dpi->templates;
4856 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4857 d_left (dc));
4859 dpi->modifiers = dpm.next;
4861 if (dpm.printed)
4862 return;
4864 /* In standard prefix notation, there is a space between the
4865 return type and the function signature. */
4866 if ((options & DMGL_RET_POSTFIX) == 0)
4867 d_append_char (dpi, ' ');
4870 if ((options & DMGL_RET_POSTFIX) == 0)
4871 d_print_function_type (dpi,
4872 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4873 dc, dpi->modifiers);
4875 return;
4878 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4880 struct d_print_mod *hold_modifiers;
4881 struct d_print_mod adpm[4];
4882 unsigned int i;
4883 struct d_print_mod *pdpm;
4885 /* We must pass this type down as a modifier in order to print
4886 multi-dimensional arrays correctly. If the array itself is
4887 CV-qualified, we act as though the element type were
4888 CV-qualified. We do this by copying the modifiers down
4889 rather than fiddling pointers, so that we don't wind up
4890 with a d_print_mod higher on the stack pointing into our
4891 stack frame after we return. */
4893 hold_modifiers = dpi->modifiers;
4895 adpm[0].next = hold_modifiers;
4896 dpi->modifiers = &adpm[0];
4897 adpm[0].mod = dc;
4898 adpm[0].printed = 0;
4899 adpm[0].templates = dpi->templates;
4901 i = 1;
4902 pdpm = hold_modifiers;
4903 while (pdpm != NULL
4904 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4905 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4906 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4908 if (! pdpm->printed)
4910 if (i >= sizeof adpm / sizeof adpm[0])
4912 d_print_error (dpi);
4913 return;
4916 adpm[i] = *pdpm;
4917 adpm[i].next = dpi->modifiers;
4918 dpi->modifiers = &adpm[i];
4919 pdpm->printed = 1;
4920 ++i;
4923 pdpm = pdpm->next;
4926 d_print_comp (dpi, options, d_right (dc));
4928 dpi->modifiers = hold_modifiers;
4930 if (adpm[0].printed)
4931 return;
4933 while (i > 1)
4935 --i;
4936 d_print_mod (dpi, options, adpm[i].mod);
4939 d_print_array_type (dpi, options, dc, dpi->modifiers);
4941 return;
4944 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4945 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4947 struct d_print_mod dpm;
4949 dpm.next = dpi->modifiers;
4950 dpi->modifiers = &dpm;
4951 dpm.mod = dc;
4952 dpm.printed = 0;
4953 dpm.templates = dpi->templates;
4955 d_print_comp (dpi, options, d_right (dc));
4957 /* If the modifier didn't get printed by the type, print it
4958 now. */
4959 if (! dpm.printed)
4960 d_print_mod (dpi, options, dc);
4962 dpi->modifiers = dpm.next;
4964 return;
4967 case DEMANGLE_COMPONENT_FIXED_TYPE:
4968 if (dc->u.s_fixed.sat)
4969 d_append_string (dpi, "_Sat ");
4970 /* Don't print "int _Accum". */
4971 if (dc->u.s_fixed.length->u.s_builtin.type
4972 != &cplus_demangle_builtin_types['i'-'a'])
4974 d_print_comp (dpi, options, dc->u.s_fixed.length);
4975 d_append_char (dpi, ' ');
4977 if (dc->u.s_fixed.accum)
4978 d_append_string (dpi, "_Accum");
4979 else
4980 d_append_string (dpi, "_Fract");
4981 return;
4983 case DEMANGLE_COMPONENT_ARGLIST:
4984 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4985 if (d_left (dc) != NULL)
4986 d_print_comp (dpi, options, d_left (dc));
4987 if (d_right (dc) != NULL)
4989 size_t len;
4990 unsigned long int flush_count;
4991 /* Make sure ", " isn't flushed by d_append_string, otherwise
4992 dpi->len -= 2 wouldn't work. */
4993 if (dpi->len >= sizeof (dpi->buf) - 2)
4994 d_print_flush (dpi);
4995 d_append_string (dpi, ", ");
4996 len = dpi->len;
4997 flush_count = dpi->flush_count;
4998 d_print_comp (dpi, options, d_right (dc));
4999 /* If that didn't print anything (which can happen with empty
5000 template argument packs), remove the comma and space. */
5001 if (dpi->flush_count == flush_count && dpi->len == len)
5002 dpi->len -= 2;
5004 return;
5006 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5008 struct demangle_component *type = d_left (dc);
5009 struct demangle_component *list = d_right (dc);
5011 if (type)
5012 d_print_comp (dpi, options, type);
5013 d_append_char (dpi, '{');
5014 d_print_comp (dpi, options, list);
5015 d_append_char (dpi, '}');
5017 return;
5019 case DEMANGLE_COMPONENT_OPERATOR:
5021 const struct demangle_operator_info *op = dc->u.s_operator.op;
5022 int len = op->len;
5024 d_append_string (dpi, "operator");
5025 /* Add a space before new/delete. */
5026 if (IS_LOWER (op->name[0]))
5027 d_append_char (dpi, ' ');
5028 /* Omit a trailing space. */
5029 if (op->name[len-1] == ' ')
5030 --len;
5031 d_append_buffer (dpi, op->name, len);
5032 return;
5035 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5036 d_append_string (dpi, "operator ");
5037 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5038 return;
5040 case DEMANGLE_COMPONENT_CAST:
5041 d_append_string (dpi, "operator ");
5042 d_print_cast (dpi, options, dc);
5043 return;
5045 case DEMANGLE_COMPONENT_NULLARY:
5046 d_print_expr_op (dpi, options, d_left (dc));
5047 return;
5049 case DEMANGLE_COMPONENT_UNARY:
5051 struct demangle_component *op = d_left (dc);
5052 struct demangle_component *operand = d_right (dc);
5053 const char *code = NULL;
5055 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5057 code = op->u.s_operator.op->code;
5058 if (!strcmp (code, "ad"))
5060 /* Don't print the argument list for the address of a
5061 function. */
5062 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5063 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5064 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5065 operand = d_left (operand);
5067 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5069 /* This indicates a suffix operator. */
5070 operand = d_left (operand);
5071 d_print_subexpr (dpi, options, operand);
5072 d_print_expr_op (dpi, options, op);
5073 return;
5077 if (op->type != DEMANGLE_COMPONENT_CAST)
5078 d_print_expr_op (dpi, options, op);
5079 else
5081 d_append_char (dpi, '(');
5082 d_print_cast (dpi, options, op);
5083 d_append_char (dpi, ')');
5085 if (code && !strcmp (code, "gs"))
5086 /* Avoid parens after '::'. */
5087 d_print_comp (dpi, options, operand);
5088 else if (code && !strcmp (code, "st"))
5089 /* Always print parens for sizeof (type). */
5091 d_append_char (dpi, '(');
5092 d_print_comp (dpi, options, operand);
5093 d_append_char (dpi, ')');
5095 else
5096 d_print_subexpr (dpi, options, operand);
5098 return;
5100 case DEMANGLE_COMPONENT_BINARY:
5101 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5103 d_print_error (dpi);
5104 return;
5107 if (op_is_new_cast (d_left (dc)))
5109 d_print_expr_op (dpi, options, d_left (dc));
5110 d_append_char (dpi, '<');
5111 d_print_comp (dpi, options, d_left (d_right (dc)));
5112 d_append_string (dpi, ">(");
5113 d_print_comp (dpi, options, d_right (d_right (dc)));
5114 d_append_char (dpi, ')');
5115 return;
5118 /* We wrap an expression which uses the greater-than operator in
5119 an extra layer of parens so that it does not get confused
5120 with the '>' which ends the template parameters. */
5121 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5122 && d_left (dc)->u.s_operator.op->len == 1
5123 && d_left (dc)->u.s_operator.op->name[0] == '>')
5124 d_append_char (dpi, '(');
5126 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5127 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5129 /* Function call used in an expression should not have printed types
5130 of the function arguments. Values of the function arguments still
5131 get printed below. */
5133 const struct demangle_component *func = d_left (d_right (dc));
5135 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5136 d_print_error (dpi);
5137 d_print_subexpr (dpi, options, d_left (func));
5139 else
5140 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5141 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5143 d_append_char (dpi, '[');
5144 d_print_comp (dpi, options, d_right (d_right (dc)));
5145 d_append_char (dpi, ']');
5147 else
5149 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5150 d_print_expr_op (dpi, options, d_left (dc));
5151 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5154 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5155 && d_left (dc)->u.s_operator.op->len == 1
5156 && d_left (dc)->u.s_operator.op->name[0] == '>')
5157 d_append_char (dpi, ')');
5159 return;
5161 case DEMANGLE_COMPONENT_BINARY_ARGS:
5162 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5163 d_print_error (dpi);
5164 return;
5166 case DEMANGLE_COMPONENT_TRINARY:
5167 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5168 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5170 d_print_error (dpi);
5171 return;
5174 struct demangle_component *op = d_left (dc);
5175 struct demangle_component *first = d_left (d_right (dc));
5176 struct demangle_component *second = d_left (d_right (d_right (dc)));
5177 struct demangle_component *third = d_right (d_right (d_right (dc)));
5179 if (!strcmp (op->u.s_operator.op->code, "qu"))
5181 d_print_subexpr (dpi, options, first);
5182 d_print_expr_op (dpi, options, op);
5183 d_print_subexpr (dpi, options, second);
5184 d_append_string (dpi, " : ");
5185 d_print_subexpr (dpi, options, third);
5187 else
5189 d_append_string (dpi, "new ");
5190 if (d_left (first) != NULL)
5192 d_print_subexpr (dpi, options, first);
5193 d_append_char (dpi, ' ');
5195 d_print_comp (dpi, options, second);
5196 if (third)
5197 d_print_subexpr (dpi, options, third);
5200 return;
5202 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5203 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5204 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5205 d_print_error (dpi);
5206 return;
5208 case DEMANGLE_COMPONENT_LITERAL:
5209 case DEMANGLE_COMPONENT_LITERAL_NEG:
5211 enum d_builtin_type_print tp;
5213 /* For some builtin types, produce simpler output. */
5214 tp = D_PRINT_DEFAULT;
5215 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5217 tp = d_left (dc)->u.s_builtin.type->print;
5218 switch (tp)
5220 case D_PRINT_INT:
5221 case D_PRINT_UNSIGNED:
5222 case D_PRINT_LONG:
5223 case D_PRINT_UNSIGNED_LONG:
5224 case D_PRINT_LONG_LONG:
5225 case D_PRINT_UNSIGNED_LONG_LONG:
5226 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5228 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5229 d_append_char (dpi, '-');
5230 d_print_comp (dpi, options, d_right (dc));
5231 switch (tp)
5233 default:
5234 break;
5235 case D_PRINT_UNSIGNED:
5236 d_append_char (dpi, 'u');
5237 break;
5238 case D_PRINT_LONG:
5239 d_append_char (dpi, 'l');
5240 break;
5241 case D_PRINT_UNSIGNED_LONG:
5242 d_append_string (dpi, "ul");
5243 break;
5244 case D_PRINT_LONG_LONG:
5245 d_append_string (dpi, "ll");
5246 break;
5247 case D_PRINT_UNSIGNED_LONG_LONG:
5248 d_append_string (dpi, "ull");
5249 break;
5251 return;
5253 break;
5255 case D_PRINT_BOOL:
5256 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5257 && d_right (dc)->u.s_name.len == 1
5258 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5260 switch (d_right (dc)->u.s_name.s[0])
5262 case '0':
5263 d_append_string (dpi, "false");
5264 return;
5265 case '1':
5266 d_append_string (dpi, "true");
5267 return;
5268 default:
5269 break;
5272 break;
5274 default:
5275 break;
5279 d_append_char (dpi, '(');
5280 d_print_comp (dpi, options, d_left (dc));
5281 d_append_char (dpi, ')');
5282 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5283 d_append_char (dpi, '-');
5284 if (tp == D_PRINT_FLOAT)
5285 d_append_char (dpi, '[');
5286 d_print_comp (dpi, options, d_right (dc));
5287 if (tp == D_PRINT_FLOAT)
5288 d_append_char (dpi, ']');
5290 return;
5292 case DEMANGLE_COMPONENT_NUMBER:
5293 d_append_num (dpi, dc->u.s_number.number);
5294 return;
5296 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5297 d_append_string (dpi, "java resource ");
5298 d_print_comp (dpi, options, d_left (dc));
5299 return;
5301 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5302 d_print_comp (dpi, options, d_left (dc));
5303 d_print_comp (dpi, options, d_right (dc));
5304 return;
5306 case DEMANGLE_COMPONENT_CHARACTER:
5307 d_append_char (dpi, dc->u.s_character.character);
5308 return;
5310 case DEMANGLE_COMPONENT_DECLTYPE:
5311 d_append_string (dpi, "decltype (");
5312 d_print_comp (dpi, options, d_left (dc));
5313 d_append_char (dpi, ')');
5314 return;
5316 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5318 int len;
5319 int i;
5320 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5321 if (a == NULL)
5323 /* d_find_pack won't find anything if the only packs involved
5324 in this expansion are function parameter packs; in that
5325 case, just print the pattern and "...". */
5326 d_print_subexpr (dpi, options, d_left (dc));
5327 d_append_string (dpi, "...");
5328 return;
5331 len = d_pack_length (a);
5332 dc = d_left (dc);
5333 for (i = 0; i < len; ++i)
5335 dpi->pack_index = i;
5336 d_print_comp (dpi, options, dc);
5337 if (i < len-1)
5338 d_append_string (dpi, ", ");
5341 return;
5343 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5345 long num = dc->u.s_number.number;
5346 if (num == 0)
5347 d_append_string (dpi, "this");
5348 else
5350 d_append_string (dpi, "{parm#");
5351 d_append_num (dpi, num);
5352 d_append_char (dpi, '}');
5355 return;
5357 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5358 d_append_string (dpi, "global constructors keyed to ");
5359 d_print_comp (dpi, options, dc->u.s_binary.left);
5360 return;
5362 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5363 d_append_string (dpi, "global destructors keyed to ");
5364 d_print_comp (dpi, options, dc->u.s_binary.left);
5365 return;
5367 case DEMANGLE_COMPONENT_LAMBDA:
5368 d_append_string (dpi, "{lambda(");
5369 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5370 d_append_string (dpi, ")#");
5371 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5372 d_append_char (dpi, '}');
5373 return;
5375 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5376 d_append_string (dpi, "{unnamed type#");
5377 d_append_num (dpi, dc->u.s_number.number + 1);
5378 d_append_char (dpi, '}');
5379 return;
5381 case DEMANGLE_COMPONENT_CLONE:
5382 d_print_comp (dpi, options, d_left (dc));
5383 d_append_string (dpi, " [clone ");
5384 d_print_comp (dpi, options, d_right (dc));
5385 d_append_char (dpi, ']');
5386 return;
5388 default:
5389 d_print_error (dpi);
5390 return;
5394 static void
5395 d_print_comp (struct d_print_info *dpi, int options,
5396 const struct demangle_component *dc)
5398 struct d_component_stack self;
5400 self.dc = dc;
5401 self.parent = dpi->component_stack;
5402 dpi->component_stack = &self;
5404 d_print_comp_inner (dpi, options, dc);
5406 dpi->component_stack = self.parent;
5409 /* Print a Java dentifier. For Java we try to handle encoded extended
5410 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5411 so we don't it for C++. Characters are encoded as
5412 __U<hex-char>+_. */
5414 static void
5415 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5417 const char *p;
5418 const char *end;
5420 end = name + len;
5421 for (p = name; p < end; ++p)
5423 if (end - p > 3
5424 && p[0] == '_'
5425 && p[1] == '_'
5426 && p[2] == 'U')
5428 unsigned long c;
5429 const char *q;
5431 c = 0;
5432 for (q = p + 3; q < end; ++q)
5434 int dig;
5436 if (IS_DIGIT (*q))
5437 dig = *q - '0';
5438 else if (*q >= 'A' && *q <= 'F')
5439 dig = *q - 'A' + 10;
5440 else if (*q >= 'a' && *q <= 'f')
5441 dig = *q - 'a' + 10;
5442 else
5443 break;
5445 c = c * 16 + dig;
5447 /* If the Unicode character is larger than 256, we don't try
5448 to deal with it here. FIXME. */
5449 if (q < end && *q == '_' && c < 256)
5451 d_append_char (dpi, c);
5452 p = q;
5453 continue;
5457 d_append_char (dpi, *p);
5461 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5462 qualifiers on this after printing a function. */
5464 static void
5465 d_print_mod_list (struct d_print_info *dpi, int options,
5466 struct d_print_mod *mods, int suffix)
5468 struct d_print_template *hold_dpt;
5470 if (mods == NULL || d_print_saw_error (dpi))
5471 return;
5473 if (mods->printed
5474 || (! suffix
5475 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5476 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5477 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5478 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5479 || (mods->mod->type
5480 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5482 d_print_mod_list (dpi, options, mods->next, suffix);
5483 return;
5486 mods->printed = 1;
5488 hold_dpt = dpi->templates;
5489 dpi->templates = mods->templates;
5491 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5493 d_print_function_type (dpi, options, mods->mod, mods->next);
5494 dpi->templates = hold_dpt;
5495 return;
5497 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5499 d_print_array_type (dpi, options, mods->mod, mods->next);
5500 dpi->templates = hold_dpt;
5501 return;
5503 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5505 struct d_print_mod *hold_modifiers;
5506 struct demangle_component *dc;
5508 /* When this is on the modifier stack, we have pulled any
5509 qualifiers off the right argument already. Otherwise, we
5510 print it as usual, but don't let the left argument see any
5511 modifiers. */
5513 hold_modifiers = dpi->modifiers;
5514 dpi->modifiers = NULL;
5515 d_print_comp (dpi, options, d_left (mods->mod));
5516 dpi->modifiers = hold_modifiers;
5518 if ((options & DMGL_JAVA) == 0)
5519 d_append_string (dpi, "::");
5520 else
5521 d_append_char (dpi, '.');
5523 dc = d_right (mods->mod);
5525 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5527 d_append_string (dpi, "{default arg#");
5528 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5529 d_append_string (dpi, "}::");
5530 dc = dc->u.s_unary_num.sub;
5533 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5534 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5535 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5536 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5537 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5538 dc = d_left (dc);
5540 d_print_comp (dpi, options, dc);
5542 dpi->templates = hold_dpt;
5543 return;
5546 d_print_mod (dpi, options, mods->mod);
5548 dpi->templates = hold_dpt;
5550 d_print_mod_list (dpi, options, mods->next, suffix);
5553 /* Print a modifier. */
5555 static void
5556 d_print_mod (struct d_print_info *dpi, int options,
5557 const struct demangle_component *mod)
5559 switch (mod->type)
5561 case DEMANGLE_COMPONENT_RESTRICT:
5562 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5563 d_append_string (dpi, " restrict");
5564 return;
5565 case DEMANGLE_COMPONENT_VOLATILE:
5566 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5567 d_append_string (dpi, " volatile");
5568 return;
5569 case DEMANGLE_COMPONENT_CONST:
5570 case DEMANGLE_COMPONENT_CONST_THIS:
5571 d_append_string (dpi, " const");
5572 return;
5573 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5574 d_append_char (dpi, ' ');
5575 d_print_comp (dpi, options, d_right (mod));
5576 return;
5577 case DEMANGLE_COMPONENT_POINTER:
5578 /* There is no pointer symbol in Java. */
5579 if ((options & DMGL_JAVA) == 0)
5580 d_append_char (dpi, '*');
5581 return;
5582 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5583 /* For the ref-qualifier, put a space before the &. */
5584 d_append_char (dpi, ' ');
5585 case DEMANGLE_COMPONENT_REFERENCE:
5586 d_append_char (dpi, '&');
5587 return;
5588 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5589 d_append_char (dpi, ' ');
5590 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5591 d_append_string (dpi, "&&");
5592 return;
5593 case DEMANGLE_COMPONENT_COMPLEX:
5594 d_append_string (dpi, "complex ");
5595 return;
5596 case DEMANGLE_COMPONENT_IMAGINARY:
5597 d_append_string (dpi, "imaginary ");
5598 return;
5599 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5600 if (d_last_char (dpi) != '(')
5601 d_append_char (dpi, ' ');
5602 d_print_comp (dpi, options, d_left (mod));
5603 d_append_string (dpi, "::*");
5604 return;
5605 case DEMANGLE_COMPONENT_TYPED_NAME:
5606 d_print_comp (dpi, options, d_left (mod));
5607 return;
5608 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5609 d_append_string (dpi, " __vector(");
5610 d_print_comp (dpi, options, d_left (mod));
5611 d_append_char (dpi, ')');
5612 return;
5614 default:
5615 /* Otherwise, we have something that won't go back on the
5616 modifier stack, so we can just print it. */
5617 d_print_comp (dpi, options, mod);
5618 return;
5622 /* Print a function type, except for the return type. */
5624 static void
5625 d_print_function_type (struct d_print_info *dpi, int options,
5626 const struct demangle_component *dc,
5627 struct d_print_mod *mods)
5629 int need_paren;
5630 int need_space;
5631 struct d_print_mod *p;
5632 struct d_print_mod *hold_modifiers;
5634 need_paren = 0;
5635 need_space = 0;
5636 for (p = mods; p != NULL; p = p->next)
5638 if (p->printed)
5639 break;
5641 switch (p->mod->type)
5643 case DEMANGLE_COMPONENT_POINTER:
5644 case DEMANGLE_COMPONENT_REFERENCE:
5645 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5646 need_paren = 1;
5647 break;
5648 case DEMANGLE_COMPONENT_RESTRICT:
5649 case DEMANGLE_COMPONENT_VOLATILE:
5650 case DEMANGLE_COMPONENT_CONST:
5651 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5652 case DEMANGLE_COMPONENT_COMPLEX:
5653 case DEMANGLE_COMPONENT_IMAGINARY:
5654 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5655 need_space = 1;
5656 need_paren = 1;
5657 break;
5658 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5659 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5660 case DEMANGLE_COMPONENT_CONST_THIS:
5661 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5662 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5663 break;
5664 default:
5665 break;
5667 if (need_paren)
5668 break;
5671 if (need_paren)
5673 if (! need_space)
5675 if (d_last_char (dpi) != '('
5676 && d_last_char (dpi) != '*')
5677 need_space = 1;
5679 if (need_space && d_last_char (dpi) != ' ')
5680 d_append_char (dpi, ' ');
5681 d_append_char (dpi, '(');
5684 hold_modifiers = dpi->modifiers;
5685 dpi->modifiers = NULL;
5687 d_print_mod_list (dpi, options, mods, 0);
5689 if (need_paren)
5690 d_append_char (dpi, ')');
5692 d_append_char (dpi, '(');
5694 if (d_right (dc) != NULL)
5695 d_print_comp (dpi, options, d_right (dc));
5697 d_append_char (dpi, ')');
5699 d_print_mod_list (dpi, options, mods, 1);
5701 dpi->modifiers = hold_modifiers;
5704 /* Print an array type, except for the element type. */
5706 static void
5707 d_print_array_type (struct d_print_info *dpi, int options,
5708 const struct demangle_component *dc,
5709 struct d_print_mod *mods)
5711 int need_space;
5713 need_space = 1;
5714 if (mods != NULL)
5716 int need_paren;
5717 struct d_print_mod *p;
5719 need_paren = 0;
5720 for (p = mods; p != NULL; p = p->next)
5722 if (! p->printed)
5724 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5726 need_space = 0;
5727 break;
5729 else
5731 need_paren = 1;
5732 need_space = 1;
5733 break;
5738 if (need_paren)
5739 d_append_string (dpi, " (");
5741 d_print_mod_list (dpi, options, mods, 0);
5743 if (need_paren)
5744 d_append_char (dpi, ')');
5747 if (need_space)
5748 d_append_char (dpi, ' ');
5750 d_append_char (dpi, '[');
5752 if (d_left (dc) != NULL)
5753 d_print_comp (dpi, options, d_left (dc));
5755 d_append_char (dpi, ']');
5758 /* Print an operator in an expression. */
5760 static void
5761 d_print_expr_op (struct d_print_info *dpi, int options,
5762 const struct demangle_component *dc)
5764 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5765 d_append_buffer (dpi, dc->u.s_operator.op->name,
5766 dc->u.s_operator.op->len);
5767 else
5768 d_print_comp (dpi, options, dc);
5771 /* Print a cast. */
5773 static void
5774 d_print_cast (struct d_print_info *dpi, int options,
5775 const struct demangle_component *dc)
5777 struct d_print_template dpt;
5779 /* For a cast operator, we need the template parameters from
5780 the enclosing template in scope for processing the type. */
5781 if (dpi->current_template != NULL)
5783 dpt.next = dpi->templates;
5784 dpi->templates = &dpt;
5785 dpt.template_decl = dpi->current_template;
5788 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5790 d_print_comp (dpi, options, d_left (dc));
5791 if (dpi->current_template != NULL)
5792 dpi->templates = dpt.next;
5794 else
5796 d_print_comp (dpi, options, d_left (d_left (dc)));
5798 /* For a templated cast operator, we need to remove the template
5799 parameters from scope after printing the operator name,
5800 so we need to handle the template printing here. */
5801 if (dpi->current_template != NULL)
5802 dpi->templates = dpt.next;
5804 if (d_last_char (dpi) == '<')
5805 d_append_char (dpi, ' ');
5806 d_append_char (dpi, '<');
5807 d_print_comp (dpi, options, d_right (d_left (dc)));
5808 /* Avoid generating two consecutive '>' characters, to avoid
5809 the C++ syntactic ambiguity. */
5810 if (d_last_char (dpi) == '>')
5811 d_append_char (dpi, ' ');
5812 d_append_char (dpi, '>');
5816 /* Initialize the information structure we use to pass around
5817 information. */
5819 CP_STATIC_IF_GLIBCPP_V3
5820 void
5821 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5822 struct d_info *di)
5824 di->s = mangled;
5825 di->send = mangled + len;
5826 di->options = options;
5828 di->n = mangled;
5830 /* We can not need more components than twice the number of chars in
5831 the mangled string. Most components correspond directly to
5832 chars, but the ARGLIST types are exceptions. */
5833 di->num_comps = 2 * len;
5834 di->next_comp = 0;
5836 /* Similarly, we can not need more substitutions than there are
5837 chars in the mangled string. */
5838 di->num_subs = len;
5839 di->next_sub = 0;
5840 di->did_subs = 0;
5842 di->last_name = NULL;
5844 di->expansion = 0;
5845 di->is_expression = 0;
5846 di->is_conversion = 0;
5849 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5850 mangled name, return strings in repeated callback giving the demangled
5851 name. OPTIONS is the usual libiberty demangler options. On success,
5852 this returns 1. On failure, returns 0. */
5854 static int
5855 d_demangle_callback (const char *mangled, int options,
5856 demangle_callbackref callback, void *opaque)
5858 enum
5860 DCT_TYPE,
5861 DCT_MANGLED,
5862 DCT_GLOBAL_CTORS,
5863 DCT_GLOBAL_DTORS
5865 type;
5866 struct d_info di;
5867 struct demangle_component *dc;
5868 int status;
5870 if (mangled[0] == '_' && mangled[1] == 'Z')
5871 type = DCT_MANGLED;
5872 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5873 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5874 && (mangled[9] == 'D' || mangled[9] == 'I')
5875 && mangled[10] == '_')
5876 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5877 else
5879 if ((options & DMGL_TYPES) == 0)
5880 return 0;
5881 type = DCT_TYPE;
5884 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5887 #ifdef CP_DYNAMIC_ARRAYS
5888 __extension__ struct demangle_component comps[di.num_comps];
5889 __extension__ struct demangle_component *subs[di.num_subs];
5891 di.comps = comps;
5892 di.subs = subs;
5893 #else
5894 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5895 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5896 #endif
5898 switch (type)
5900 case DCT_TYPE:
5901 dc = cplus_demangle_type (&di);
5902 break;
5903 case DCT_MANGLED:
5904 dc = cplus_demangle_mangled_name (&di, 1);
5905 break;
5906 case DCT_GLOBAL_CTORS:
5907 case DCT_GLOBAL_DTORS:
5908 d_advance (&di, 11);
5909 dc = d_make_comp (&di,
5910 (type == DCT_GLOBAL_CTORS
5911 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5912 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5913 d_make_demangle_mangled_name (&di, d_str (&di)),
5914 NULL);
5915 d_advance (&di, strlen (d_str (&di)));
5916 break;
5917 default:
5918 abort (); /* We have listed all the cases. */
5921 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5922 mangled string, then we didn't successfully demangle it. If
5923 DMGL_PARAMS is not set, we didn't look at the trailing
5924 parameters. */
5925 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5926 dc = NULL;
5928 #ifdef CP_DEMANGLE_DEBUG
5929 d_dump (dc, 0);
5930 #endif
5932 status = (dc != NULL)
5933 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5934 : 0;
5937 return status;
5940 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5941 name, return a buffer allocated with malloc holding the demangled
5942 name. OPTIONS is the usual libiberty demangler options. On
5943 success, this sets *PALC to the allocated size of the returned
5944 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5945 a memory allocation failure, and returns NULL. */
5947 static char *
5948 d_demangle (const char *mangled, int options, size_t *palc)
5950 struct d_growable_string dgs;
5951 int status;
5953 d_growable_string_init (&dgs, 0);
5955 status = d_demangle_callback (mangled, options,
5956 d_growable_string_callback_adapter, &dgs);
5957 if (status == 0)
5959 free (dgs.buf);
5960 *palc = 0;
5961 return NULL;
5964 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5965 return dgs.buf;
5968 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5970 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5972 /* ia64 ABI-mandated entry point in the C++ runtime library for
5973 performing demangling. MANGLED_NAME is a NUL-terminated character
5974 string containing the name to be demangled.
5976 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5977 *LENGTH bytes, into which the demangled name is stored. If
5978 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5979 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5980 is placed in a region of memory allocated with malloc.
5982 If LENGTH is non-NULL, the length of the buffer containing the
5983 demangled name, is placed in *LENGTH.
5985 The return value is a pointer to the start of the NUL-terminated
5986 demangled name, or NULL if the demangling fails. The caller is
5987 responsible for deallocating this memory using free.
5989 *STATUS is set to one of the following values:
5990 0: The demangling operation succeeded.
5991 -1: A memory allocation failure occurred.
5992 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5993 -3: One of the arguments is invalid.
5995 The demangling is performed using the C++ ABI mangling rules, with
5996 GNU extensions. */
5998 char *
5999 __cxa_demangle (const char *mangled_name, char *output_buffer,
6000 size_t *length, int *status)
6002 char *demangled;
6003 size_t alc;
6005 if (mangled_name == NULL)
6007 if (status != NULL)
6008 *status = -3;
6009 return NULL;
6012 if (output_buffer != NULL && length == NULL)
6014 if (status != NULL)
6015 *status = -3;
6016 return NULL;
6019 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6021 if (demangled == NULL)
6023 if (status != NULL)
6025 if (alc == 1)
6026 *status = -1;
6027 else
6028 *status = -2;
6030 return NULL;
6033 if (output_buffer == NULL)
6035 if (length != NULL)
6036 *length = alc;
6038 else
6040 if (strlen (demangled) < *length)
6042 strcpy (output_buffer, demangled);
6043 free (demangled);
6044 demangled = output_buffer;
6046 else
6048 free (output_buffer);
6049 *length = alc;
6053 if (status != NULL)
6054 *status = 0;
6056 return demangled;
6059 extern int __gcclibcxx_demangle_callback (const char *,
6060 void (*)
6061 (const char *, size_t, void *),
6062 void *);
6064 /* Alternative, allocationless entry point in the C++ runtime library
6065 for performing demangling. MANGLED_NAME is a NUL-terminated character
6066 string containing the name to be demangled.
6068 CALLBACK is a callback function, called with demangled string
6069 segments as demangling progresses; it is called at least once,
6070 but may be called more than once. OPAQUE is a generalized pointer
6071 used as a callback argument.
6073 The return code is one of the following values, equivalent to
6074 the STATUS values of __cxa_demangle() (excluding -1, since this
6075 function performs no memory allocations):
6076 0: The demangling operation succeeded.
6077 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6078 -3: One of the arguments is invalid.
6080 The demangling is performed using the C++ ABI mangling rules, with
6081 GNU extensions. */
6084 __gcclibcxx_demangle_callback (const char *mangled_name,
6085 void (*callback) (const char *, size_t, void *),
6086 void *opaque)
6088 int status;
6090 if (mangled_name == NULL || callback == NULL)
6091 return -3;
6093 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6094 callback, opaque);
6095 if (status == 0)
6096 return -2;
6098 return 0;
6101 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6103 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6104 mangled name, return a buffer allocated with malloc holding the
6105 demangled name. Otherwise, return NULL. */
6107 char *
6108 cplus_demangle_v3 (const char *mangled, int options)
6110 size_t alc;
6112 return d_demangle (mangled, options, &alc);
6116 cplus_demangle_v3_callback (const char *mangled, int options,
6117 demangle_callbackref callback, void *opaque)
6119 return d_demangle_callback (mangled, options, callback, opaque);
6122 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6123 conventions, but the output formatting is a little different.
6124 This instructs the C++ demangler not to emit pointer characters ("*"), to
6125 use Java's namespace separator symbol ("." instead of "::"), and to output
6126 JArray<TYPE> as TYPE[]. */
6128 char *
6129 java_demangle_v3 (const char *mangled)
6131 size_t alc;
6133 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6137 java_demangle_v3_callback (const char *mangled,
6138 demangle_callbackref callback, void *opaque)
6140 return d_demangle_callback (mangled,
6141 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6142 callback, opaque);
6145 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6147 #ifndef IN_GLIBCPP_V3
6149 /* Demangle a string in order to find out whether it is a constructor
6150 or destructor. Return non-zero on success. Set *CTOR_KIND and
6151 *DTOR_KIND appropriately. */
6153 static int
6154 is_ctor_or_dtor (const char *mangled,
6155 enum gnu_v3_ctor_kinds *ctor_kind,
6156 enum gnu_v3_dtor_kinds *dtor_kind)
6158 struct d_info di;
6159 struct demangle_component *dc;
6160 int ret;
6162 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6163 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6165 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6168 #ifdef CP_DYNAMIC_ARRAYS
6169 __extension__ struct demangle_component comps[di.num_comps];
6170 __extension__ struct demangle_component *subs[di.num_subs];
6172 di.comps = comps;
6173 di.subs = subs;
6174 #else
6175 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6176 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6177 #endif
6179 dc = cplus_demangle_mangled_name (&di, 1);
6181 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6182 to demangle the entire string. */
6184 ret = 0;
6185 while (dc != NULL)
6187 switch (dc->type)
6189 /* These cannot appear on a constructor or destructor. */
6190 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6191 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6192 case DEMANGLE_COMPONENT_CONST_THIS:
6193 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6194 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6195 default:
6196 dc = NULL;
6197 break;
6198 case DEMANGLE_COMPONENT_TYPED_NAME:
6199 case DEMANGLE_COMPONENT_TEMPLATE:
6200 dc = d_left (dc);
6201 break;
6202 case DEMANGLE_COMPONENT_QUAL_NAME:
6203 case DEMANGLE_COMPONENT_LOCAL_NAME:
6204 dc = d_right (dc);
6205 break;
6206 case DEMANGLE_COMPONENT_CTOR:
6207 *ctor_kind = dc->u.s_ctor.kind;
6208 ret = 1;
6209 dc = NULL;
6210 break;
6211 case DEMANGLE_COMPONENT_DTOR:
6212 *dtor_kind = dc->u.s_dtor.kind;
6213 ret = 1;
6214 dc = NULL;
6215 break;
6220 return ret;
6223 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6224 name. A non-zero return indicates the type of constructor. */
6226 enum gnu_v3_ctor_kinds
6227 is_gnu_v3_mangled_ctor (const char *name)
6229 enum gnu_v3_ctor_kinds ctor_kind;
6230 enum gnu_v3_dtor_kinds dtor_kind;
6232 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6233 return (enum gnu_v3_ctor_kinds) 0;
6234 return ctor_kind;
6238 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6239 name. A non-zero return indicates the type of destructor. */
6241 enum gnu_v3_dtor_kinds
6242 is_gnu_v3_mangled_dtor (const char *name)
6244 enum gnu_v3_ctor_kinds ctor_kind;
6245 enum gnu_v3_dtor_kinds dtor_kind;
6247 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6248 return (enum gnu_v3_dtor_kinds) 0;
6249 return dtor_kind;
6252 #endif /* IN_GLIBCPP_V3 */
6254 #ifdef STANDALONE_DEMANGLER
6256 #include "getopt.h"
6257 #include "dyn-string.h"
6259 static void print_usage (FILE* fp, int exit_value);
6261 #define IS_ALPHA(CHAR) \
6262 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6263 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6265 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6266 #define is_mangled_char(CHAR) \
6267 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6268 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6270 /* The name of this program, as invoked. */
6271 const char* program_name;
6273 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6275 static void
6276 print_usage (FILE* fp, int exit_value)
6278 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6279 fprintf (fp, "Options:\n");
6280 fprintf (fp, " -h,--help Display this message.\n");
6281 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6282 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6283 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6285 exit (exit_value);
6288 /* Option specification for getopt_long. */
6289 static const struct option long_options[] =
6291 { "help", no_argument, NULL, 'h' },
6292 { "no-params", no_argument, NULL, 'p' },
6293 { "verbose", no_argument, NULL, 'v' },
6294 { NULL, no_argument, NULL, 0 },
6297 /* Main entry for a demangling filter executable. It will demangle
6298 its command line arguments, if any. If none are provided, it will
6299 filter stdin to stdout, replacing any recognized mangled C++ names
6300 with their demangled equivalents. */
6303 main (int argc, char *argv[])
6305 int i;
6306 int opt_char;
6307 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6309 /* Use the program name of this program, as invoked. */
6310 program_name = argv[0];
6312 /* Parse options. */
6315 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6316 switch (opt_char)
6318 case '?': /* Unrecognized option. */
6319 print_usage (stderr, 1);
6320 break;
6322 case 'h':
6323 print_usage (stdout, 0);
6324 break;
6326 case 'p':
6327 options &= ~ DMGL_PARAMS;
6328 break;
6330 case 'v':
6331 options |= DMGL_VERBOSE;
6332 break;
6335 while (opt_char != -1);
6337 if (optind == argc)
6338 /* No command line arguments were provided. Filter stdin. */
6340 dyn_string_t mangled = dyn_string_new (3);
6341 char *s;
6343 /* Read all of input. */
6344 while (!feof (stdin))
6346 char c;
6348 /* Pile characters into mangled until we hit one that can't
6349 occur in a mangled name. */
6350 c = getchar ();
6351 while (!feof (stdin) && is_mangled_char (c))
6353 dyn_string_append_char (mangled, c);
6354 if (feof (stdin))
6355 break;
6356 c = getchar ();
6359 if (dyn_string_length (mangled) > 0)
6361 #ifdef IN_GLIBCPP_V3
6362 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6363 #else
6364 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6365 #endif
6367 if (s != NULL)
6369 fputs (s, stdout);
6370 free (s);
6372 else
6374 /* It might not have been a mangled name. Print the
6375 original text. */
6376 fputs (dyn_string_buf (mangled), stdout);
6379 dyn_string_clear (mangled);
6382 /* If we haven't hit EOF yet, we've read one character that
6383 can't occur in a mangled name, so print it out. */
6384 if (!feof (stdin))
6385 putchar (c);
6388 dyn_string_delete (mangled);
6390 else
6391 /* Demangle command line arguments. */
6393 /* Loop over command line arguments. */
6394 for (i = optind; i < argc; ++i)
6396 char *s;
6397 #ifdef IN_GLIBCPP_V3
6398 int status;
6399 #endif
6401 /* Attempt to demangle. */
6402 #ifdef IN_GLIBCPP_V3
6403 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6404 #else
6405 s = cplus_demangle_v3 (argv[i], options);
6406 #endif
6408 /* If it worked, print the demangled name. */
6409 if (s != NULL)
6411 printf ("%s\n", s);
6412 free (s);
6414 else
6416 #ifdef IN_GLIBCPP_V3
6417 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6418 #else
6419 fprintf (stderr, "Failed: %s\n", argv[i]);
6420 #endif
6425 return 0;
6428 #endif /* STANDALONE_DEMANGLER */