* config/rs6000/t-spe (MULTILIB_EXCEPTIONS): Allow isel without SPE.
[official-gcc.git] / libiberty / cp-demangle.c
blob4ecdb1ee439e83cc8bfddaa47619f650398b4937
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.
98 #if defined (_AIX) && !defined (__GNUC__)
99 #pragma alloca
100 #endif
102 #ifdef HAVE_CONFIG_H
103 #include "config.h"
104 #endif
106 #include <stdio.h>
108 #ifdef HAVE_STDLIB_H
109 #include <stdlib.h>
110 #endif
111 #ifdef HAVE_STRING_H
112 #include <string.h>
113 #endif
115 #ifdef HAVE_ALLOCA_H
116 # include <alloca.h>
117 #else
118 # ifndef alloca
119 # ifdef __GNUC__
120 # define alloca __builtin_alloca
121 # else
122 extern char *alloca ();
123 # endif /* __GNUC__ */
124 # endif /* alloca */
125 #endif /* HAVE_ALLOCA_H */
127 #include "ansidecl.h"
128 #include "libiberty.h"
129 #include "demangle.h"
130 #include "cp-demangle.h"
132 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
133 also rename them via #define to avoid compiler errors when the
134 static definition conflicts with the extern declaration in a header
135 file. */
136 #ifdef IN_GLIBCPP_V3
138 #define CP_STATIC_IF_GLIBCPP_V3 static
140 #define cplus_demangle_fill_name d_fill_name
141 static int d_fill_name (struct demangle_component *, const char *, int);
143 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
144 static int
145 d_fill_extended_operator (struct demangle_component *, int,
146 struct demangle_component *);
148 #define cplus_demangle_fill_ctor d_fill_ctor
149 static int
150 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
151 struct demangle_component *);
153 #define cplus_demangle_fill_dtor d_fill_dtor
154 static int
155 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
156 struct demangle_component *);
158 #define cplus_demangle_mangled_name d_mangled_name
159 static struct demangle_component *d_mangled_name (struct d_info *, int);
161 #define cplus_demangle_type d_type
162 static struct demangle_component *d_type (struct d_info *);
164 #define cplus_demangle_print d_print
165 static char *d_print (int, const struct demangle_component *, int, size_t *);
167 #define cplus_demangle_print_callback d_print_callback
168 static int d_print_callback (int, const struct demangle_component *,
169 demangle_callbackref, void *);
171 #define cplus_demangle_init_info d_init_info
172 static void d_init_info (const char *, int, size_t, struct d_info *);
174 #else /* ! defined(IN_GLIBCPP_V3) */
175 #define CP_STATIC_IF_GLIBCPP_V3
176 #endif /* ! defined(IN_GLIBCPP_V3) */
178 /* See if the compiler supports dynamic arrays. */
180 #ifdef __GNUC__
181 #define CP_DYNAMIC_ARRAYS
182 #else
183 #ifdef __STDC__
184 #ifdef __STDC_VERSION__
185 #if __STDC_VERSION__ >= 199901L
186 #define CP_DYNAMIC_ARRAYS
187 #endif /* __STDC__VERSION >= 199901L */
188 #endif /* defined (__STDC_VERSION__) */
189 #endif /* defined (__STDC__) */
190 #endif /* ! defined (__GNUC__) */
192 /* We avoid pulling in the ctype tables, to prevent pulling in
193 additional unresolved symbols when this code is used in a library.
194 FIXME: Is this really a valid reason? This comes from the original
195 V3 demangler code.
197 As of this writing this file has the following undefined references
198 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
199 strcat, strlen. */
201 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
205 /* The prefix prepended by GCC to an identifier represnting the
206 anonymous namespace. */
207 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
211 /* Information we keep for the standard substitutions. */
213 struct d_standard_sub_info
215 /* The code for this substitution. */
216 char code;
217 /* The simple string it expands to. */
218 const char *simple_expansion;
219 /* The length of the simple expansion. */
220 int simple_len;
221 /* The results of a full, verbose, expansion. This is used when
222 qualifying a constructor/destructor, or when in verbose mode. */
223 const char *full_expansion;
224 /* The length of the full expansion. */
225 int full_len;
226 /* What to set the last_name field of d_info to; NULL if we should
227 not set it. This is only relevant when qualifying a
228 constructor/destructor. */
229 const char *set_last_name;
230 /* The length of set_last_name. */
231 int set_last_name_len;
234 /* Accessors for subtrees of struct demangle_component. */
236 #define d_left(dc) ((dc)->u.s_binary.left)
237 #define d_right(dc) ((dc)->u.s_binary.right)
239 /* A list of templates. This is used while printing. */
241 struct d_print_template
243 /* Next template on the list. */
244 struct d_print_template *next;
245 /* This template. */
246 const struct demangle_component *template_decl;
249 /* A list of type modifiers. This is used while printing. */
251 struct d_print_mod
253 /* Next modifier on the list. These are in the reverse of the order
254 in which they appeared in the mangled string. */
255 struct d_print_mod *next;
256 /* The modifier. */
257 const struct demangle_component *mod;
258 /* Whether this modifier was printed. */
259 int printed;
260 /* The list of templates which applies to this modifier. */
261 struct d_print_template *templates;
264 /* We use these structures to hold information during printing. */
266 struct d_growable_string
268 /* Buffer holding the result. */
269 char *buf;
270 /* Current length of data in buffer. */
271 size_t len;
272 /* Allocated size of buffer. */
273 size_t alc;
274 /* Set to 1 if we had a memory allocation failure. */
275 int allocation_failure;
278 /* Stack of components, innermost first, used to avoid loops. */
280 struct d_component_stack
282 /* This component. */
283 const struct demangle_component *dc;
284 /* This component's parent. */
285 const struct d_component_stack *parent;
288 /* A demangle component and some scope captured when it was first
289 traversed. */
291 struct d_saved_scope
293 /* The component whose scope this is. */
294 const struct demangle_component *container;
295 /* The list of templates, if any, that was current when this
296 scope was captured. */
297 struct d_print_template *templates;
300 /* Checkpoint structure to allow backtracking. This holds copies
301 of the fields of struct d_info that need to be restored
302 if a trial parse needs to be backtracked over. */
304 struct d_info_checkpoint
306 const char *n;
307 int next_comp;
308 int next_sub;
309 int did_subs;
310 int expansion;
313 enum { D_PRINT_BUFFER_LENGTH = 256 };
314 struct d_print_info
316 /* Fixed-length allocated buffer for demangled data, flushed to the
317 callback with a NUL termination once full. */
318 char buf[D_PRINT_BUFFER_LENGTH];
319 /* Current length of data in buffer. */
320 size_t len;
321 /* The last character printed, saved individually so that it survives
322 any buffer flush. */
323 char last_char;
324 /* Callback function to handle demangled buffer flush. */
325 demangle_callbackref callback;
326 /* Opaque callback argument. */
327 void *opaque;
328 /* The current list of templates, if any. */
329 struct d_print_template *templates;
330 /* The current list of modifiers (e.g., pointer, reference, etc.),
331 if any. */
332 struct d_print_mod *modifiers;
333 /* Set to 1 if we saw a demangling error. */
334 int demangle_failure;
335 /* The current index into any template argument packs we are using
336 for printing. */
337 int pack_index;
338 /* Number of d_print_flush calls so far. */
339 unsigned long int flush_count;
340 /* Stack of components, innermost first, used to avoid loops. */
341 const struct d_component_stack *component_stack;
342 /* Array of saved scopes for evaluating substitutions. */
343 struct d_saved_scope *saved_scopes;
344 /* Index of the next unused saved scope in the above array. */
345 int next_saved_scope;
346 /* Number of saved scopes in the above array. */
347 int num_saved_scopes;
348 /* Array of templates for saving into scopes. */
349 struct d_print_template *copy_templates;
350 /* Index of the next unused copy template in the above array. */
351 int next_copy_template;
352 /* Number of copy templates in the above array. */
353 int num_copy_templates;
354 /* The nearest enclosing template, if any. */
355 const struct demangle_component *current_template;
358 #ifdef CP_DEMANGLE_DEBUG
359 static void d_dump (struct demangle_component *, int);
360 #endif
362 static struct demangle_component *
363 d_make_empty (struct d_info *);
365 static struct demangle_component *
366 d_make_comp (struct d_info *, enum demangle_component_type,
367 struct demangle_component *,
368 struct demangle_component *);
370 static struct demangle_component *
371 d_make_name (struct d_info *, const char *, int);
373 static struct demangle_component *
374 d_make_demangle_mangled_name (struct d_info *, const char *);
376 static struct demangle_component *
377 d_make_builtin_type (struct d_info *,
378 const struct demangle_builtin_type_info *);
380 static struct demangle_component *
381 d_make_operator (struct d_info *,
382 const struct demangle_operator_info *);
384 static struct demangle_component *
385 d_make_extended_operator (struct d_info *, int,
386 struct demangle_component *);
388 static struct demangle_component *
389 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
390 struct demangle_component *);
392 static struct demangle_component *
393 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
394 struct demangle_component *);
396 static struct demangle_component *
397 d_make_template_param (struct d_info *, long);
399 static struct demangle_component *
400 d_make_sub (struct d_info *, const char *, int);
402 static int
403 has_return_type (struct demangle_component *);
405 static int
406 is_ctor_dtor_or_conversion (struct demangle_component *);
408 static struct demangle_component *d_encoding (struct d_info *, int);
410 static struct demangle_component *d_name (struct d_info *);
412 static struct demangle_component *d_nested_name (struct d_info *);
414 static struct demangle_component *d_prefix (struct d_info *);
416 static struct demangle_component *d_unqualified_name (struct d_info *);
418 static struct demangle_component *d_source_name (struct d_info *);
420 static long d_number (struct d_info *);
422 static struct demangle_component *d_identifier (struct d_info *, int);
424 static struct demangle_component *d_operator_name (struct d_info *);
426 static struct demangle_component *d_special_name (struct d_info *);
428 static int d_call_offset (struct d_info *, int);
430 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
432 static struct demangle_component **
433 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
435 static struct demangle_component *
436 d_ref_qualifier (struct d_info *, struct demangle_component *);
438 static struct demangle_component *
439 d_function_type (struct d_info *);
441 static struct demangle_component *
442 d_bare_function_type (struct d_info *, int);
444 static struct demangle_component *
445 d_class_enum_type (struct d_info *);
447 static struct demangle_component *d_array_type (struct d_info *);
449 static struct demangle_component *d_vector_type (struct d_info *);
451 static struct demangle_component *
452 d_pointer_to_member_type (struct d_info *);
454 static struct demangle_component *
455 d_template_param (struct d_info *);
457 static struct demangle_component *d_template_args (struct d_info *);
459 static struct demangle_component *
460 d_template_arg (struct d_info *);
462 static struct demangle_component *d_expression (struct d_info *);
464 static struct demangle_component *d_expr_primary (struct d_info *);
466 static struct demangle_component *d_local_name (struct d_info *);
468 static int d_discriminator (struct d_info *);
470 static struct demangle_component *d_lambda (struct d_info *);
472 static struct demangle_component *d_unnamed_type (struct d_info *);
474 static struct demangle_component *
475 d_clone_suffix (struct d_info *, struct demangle_component *);
477 static int
478 d_add_substitution (struct d_info *, struct demangle_component *);
480 static struct demangle_component *d_substitution (struct d_info *, int);
482 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
484 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
486 static void d_growable_string_init (struct d_growable_string *, size_t);
488 static inline void
489 d_growable_string_resize (struct d_growable_string *, size_t);
491 static inline void
492 d_growable_string_append_buffer (struct d_growable_string *,
493 const char *, size_t);
494 static void
495 d_growable_string_callback_adapter (const char *, size_t, void *);
497 static void
498 d_print_init (struct d_print_info *, demangle_callbackref, void *,
499 const struct demangle_component *);
501 static inline void d_print_error (struct d_print_info *);
503 static inline int d_print_saw_error (struct d_print_info *);
505 static inline void d_print_flush (struct d_print_info *);
507 static inline void d_append_char (struct d_print_info *, char);
509 static inline void d_append_buffer (struct d_print_info *,
510 const char *, size_t);
512 static inline void d_append_string (struct d_print_info *, const char *);
514 static inline char d_last_char (struct d_print_info *);
516 static void
517 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
519 static void
520 d_print_java_identifier (struct d_print_info *, const char *, int);
522 static void
523 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
525 static void
526 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
528 static void
529 d_print_function_type (struct d_print_info *, int,
530 const struct demangle_component *,
531 struct d_print_mod *);
533 static void
534 d_print_array_type (struct d_print_info *, int,
535 const struct demangle_component *,
536 struct d_print_mod *);
538 static void
539 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
541 static void
542 d_print_cast (struct d_print_info *, int, const struct demangle_component *);
544 static int d_demangle_callback (const char *, int,
545 demangle_callbackref, void *);
546 static char *d_demangle (const char *, int, size_t *);
548 #ifdef CP_DEMANGLE_DEBUG
550 static void
551 d_dump (struct demangle_component *dc, int indent)
553 int i;
555 if (dc == NULL)
557 if (indent == 0)
558 printf ("failed demangling\n");
559 return;
562 for (i = 0; i < indent; ++i)
563 putchar (' ');
565 switch (dc->type)
567 case DEMANGLE_COMPONENT_NAME:
568 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
569 return;
570 case DEMANGLE_COMPONENT_TAGGED_NAME:
571 printf ("tagged name\n");
572 d_dump (dc->u.s_binary.left, indent + 2);
573 d_dump (dc->u.s_binary.right, indent + 2);
574 return;
575 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
576 printf ("template parameter %ld\n", dc->u.s_number.number);
577 return;
578 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
579 printf ("function parameter %ld\n", dc->u.s_number.number);
580 return;
581 case DEMANGLE_COMPONENT_CTOR:
582 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
583 d_dump (dc->u.s_ctor.name, indent + 2);
584 return;
585 case DEMANGLE_COMPONENT_DTOR:
586 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
587 d_dump (dc->u.s_dtor.name, indent + 2);
588 return;
589 case DEMANGLE_COMPONENT_SUB_STD:
590 printf ("standard substitution %s\n", dc->u.s_string.string);
591 return;
592 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
593 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
594 return;
595 case DEMANGLE_COMPONENT_OPERATOR:
596 printf ("operator %s\n", dc->u.s_operator.op->name);
597 return;
598 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
599 printf ("extended operator with %d args\n",
600 dc->u.s_extended_operator.args);
601 d_dump (dc->u.s_extended_operator.name, indent + 2);
602 return;
604 case DEMANGLE_COMPONENT_QUAL_NAME:
605 printf ("qualified name\n");
606 break;
607 case DEMANGLE_COMPONENT_LOCAL_NAME:
608 printf ("local name\n");
609 break;
610 case DEMANGLE_COMPONENT_TYPED_NAME:
611 printf ("typed name\n");
612 break;
613 case DEMANGLE_COMPONENT_TEMPLATE:
614 printf ("template\n");
615 break;
616 case DEMANGLE_COMPONENT_VTABLE:
617 printf ("vtable\n");
618 break;
619 case DEMANGLE_COMPONENT_VTT:
620 printf ("VTT\n");
621 break;
622 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
623 printf ("construction vtable\n");
624 break;
625 case DEMANGLE_COMPONENT_TYPEINFO:
626 printf ("typeinfo\n");
627 break;
628 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
629 printf ("typeinfo name\n");
630 break;
631 case DEMANGLE_COMPONENT_TYPEINFO_FN:
632 printf ("typeinfo function\n");
633 break;
634 case DEMANGLE_COMPONENT_THUNK:
635 printf ("thunk\n");
636 break;
637 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
638 printf ("virtual thunk\n");
639 break;
640 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
641 printf ("covariant thunk\n");
642 break;
643 case DEMANGLE_COMPONENT_JAVA_CLASS:
644 printf ("java class\n");
645 break;
646 case DEMANGLE_COMPONENT_GUARD:
647 printf ("guard\n");
648 break;
649 case DEMANGLE_COMPONENT_REFTEMP:
650 printf ("reference temporary\n");
651 break;
652 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
653 printf ("hidden alias\n");
654 break;
655 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
656 printf ("transaction clone\n");
657 break;
658 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
659 printf ("non-transaction clone\n");
660 break;
661 case DEMANGLE_COMPONENT_RESTRICT:
662 printf ("restrict\n");
663 break;
664 case DEMANGLE_COMPONENT_VOLATILE:
665 printf ("volatile\n");
666 break;
667 case DEMANGLE_COMPONENT_CONST:
668 printf ("const\n");
669 break;
670 case DEMANGLE_COMPONENT_RESTRICT_THIS:
671 printf ("restrict this\n");
672 break;
673 case DEMANGLE_COMPONENT_VOLATILE_THIS:
674 printf ("volatile this\n");
675 break;
676 case DEMANGLE_COMPONENT_CONST_THIS:
677 printf ("const this\n");
678 break;
679 case DEMANGLE_COMPONENT_REFERENCE_THIS:
680 printf ("reference this\n");
681 break;
682 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
683 printf ("rvalue reference this\n");
684 break;
685 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
686 printf ("vendor type qualifier\n");
687 break;
688 case DEMANGLE_COMPONENT_POINTER:
689 printf ("pointer\n");
690 break;
691 case DEMANGLE_COMPONENT_REFERENCE:
692 printf ("reference\n");
693 break;
694 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
695 printf ("rvalue reference\n");
696 break;
697 case DEMANGLE_COMPONENT_COMPLEX:
698 printf ("complex\n");
699 break;
700 case DEMANGLE_COMPONENT_IMAGINARY:
701 printf ("imaginary\n");
702 break;
703 case DEMANGLE_COMPONENT_VENDOR_TYPE:
704 printf ("vendor type\n");
705 break;
706 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
707 printf ("function type\n");
708 break;
709 case DEMANGLE_COMPONENT_ARRAY_TYPE:
710 printf ("array type\n");
711 break;
712 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
713 printf ("pointer to member type\n");
714 break;
715 case DEMANGLE_COMPONENT_FIXED_TYPE:
716 printf ("fixed-point type, accum? %d, sat? %d\n",
717 dc->u.s_fixed.accum, dc->u.s_fixed.sat);
718 d_dump (dc->u.s_fixed.length, indent + 2)
719 break;
720 case DEMANGLE_COMPONENT_ARGLIST:
721 printf ("argument list\n");
722 break;
723 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
724 printf ("template argument list\n");
725 break;
726 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
727 printf ("initializer list\n");
728 break;
729 case DEMANGLE_COMPONENT_CAST:
730 printf ("cast\n");
731 break;
732 case DEMANGLE_COMPONENT_NULLARY:
733 printf ("nullary operator\n");
734 break;
735 case DEMANGLE_COMPONENT_UNARY:
736 printf ("unary operator\n");
737 break;
738 case DEMANGLE_COMPONENT_BINARY:
739 printf ("binary operator\n");
740 break;
741 case DEMANGLE_COMPONENT_BINARY_ARGS:
742 printf ("binary operator arguments\n");
743 break;
744 case DEMANGLE_COMPONENT_TRINARY:
745 printf ("trinary operator\n");
746 break;
747 case DEMANGLE_COMPONENT_TRINARY_ARG1:
748 printf ("trinary operator arguments 1\n");
749 break;
750 case DEMANGLE_COMPONENT_TRINARY_ARG2:
751 printf ("trinary operator arguments 1\n");
752 break;
753 case DEMANGLE_COMPONENT_LITERAL:
754 printf ("literal\n");
755 break;
756 case DEMANGLE_COMPONENT_LITERAL_NEG:
757 printf ("negative literal\n");
758 break;
759 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
760 printf ("java resource\n");
761 break;
762 case DEMANGLE_COMPONENT_COMPOUND_NAME:
763 printf ("compound name\n");
764 break;
765 case DEMANGLE_COMPONENT_CHARACTER:
766 printf ("character '%c'\n", dc->u.s_character.character);
767 return;
768 case DEMANGLE_COMPONENT_NUMBER:
769 printf ("number %ld\n", dc->u.s_number.number);
770 return;
771 case DEMANGLE_COMPONENT_DECLTYPE:
772 printf ("decltype\n");
773 break;
774 case DEMANGLE_COMPONENT_PACK_EXPANSION:
775 printf ("pack expansion\n");
776 break;
777 case DEMANGLE_COMPONENT_TLS_INIT:
778 printf ("tls init function\n");
779 break;
780 case DEMANGLE_COMPONENT_TLS_WRAPPER:
781 printf ("tls wrapper function\n");
782 break;
783 case DEMANGLE_COMPONENT_DEFAULT_ARG:
784 printf ("default argument %d\n", dc->u.s_unary_num.num);
785 d_dump (dc->u.s_unary_num.sub, indent+2);
786 return;
787 case DEMANGLE_COMPONENT_LAMBDA:
788 printf ("lambda %d\n", dc->u.s_unary_num.num);
789 d_dump (dc->u.s_unary_num.sub, indent+2);
790 return;
793 d_dump (d_left (dc), indent + 2);
794 d_dump (d_right (dc), indent + 2);
797 #endif /* CP_DEMANGLE_DEBUG */
799 /* Fill in a DEMANGLE_COMPONENT_NAME. */
801 CP_STATIC_IF_GLIBCPP_V3
803 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
805 if (p == NULL || s == NULL || len == 0)
806 return 0;
807 p->type = DEMANGLE_COMPONENT_NAME;
808 p->u.s_name.s = s;
809 p->u.s_name.len = len;
810 return 1;
813 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
815 CP_STATIC_IF_GLIBCPP_V3
817 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
818 struct demangle_component *name)
820 if (p == NULL || args < 0 || name == NULL)
821 return 0;
822 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
823 p->u.s_extended_operator.args = args;
824 p->u.s_extended_operator.name = name;
825 return 1;
828 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
830 CP_STATIC_IF_GLIBCPP_V3
832 cplus_demangle_fill_ctor (struct demangle_component *p,
833 enum gnu_v3_ctor_kinds kind,
834 struct demangle_component *name)
836 if (p == NULL
837 || name == NULL
838 || (int) kind < gnu_v3_complete_object_ctor
839 || (int) kind > gnu_v3_object_ctor_group)
840 return 0;
841 p->type = DEMANGLE_COMPONENT_CTOR;
842 p->u.s_ctor.kind = kind;
843 p->u.s_ctor.name = name;
844 return 1;
847 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
849 CP_STATIC_IF_GLIBCPP_V3
851 cplus_demangle_fill_dtor (struct demangle_component *p,
852 enum gnu_v3_dtor_kinds kind,
853 struct demangle_component *name)
855 if (p == NULL
856 || name == NULL
857 || (int) kind < gnu_v3_deleting_dtor
858 || (int) kind > gnu_v3_object_dtor_group)
859 return 0;
860 p->type = DEMANGLE_COMPONENT_DTOR;
861 p->u.s_dtor.kind = kind;
862 p->u.s_dtor.name = name;
863 return 1;
866 /* Add a new component. */
868 static struct demangle_component *
869 d_make_empty (struct d_info *di)
871 struct demangle_component *p;
873 if (di->next_comp >= di->num_comps)
874 return NULL;
875 p = &di->comps[di->next_comp];
876 ++di->next_comp;
877 return p;
880 /* Add a new generic component. */
882 static struct demangle_component *
883 d_make_comp (struct d_info *di, enum demangle_component_type type,
884 struct demangle_component *left,
885 struct demangle_component *right)
887 struct demangle_component *p;
889 /* We check for errors here. A typical error would be a NULL return
890 from a subroutine. We catch those here, and return NULL
891 upward. */
892 switch (type)
894 /* These types require two parameters. */
895 case DEMANGLE_COMPONENT_QUAL_NAME:
896 case DEMANGLE_COMPONENT_LOCAL_NAME:
897 case DEMANGLE_COMPONENT_TYPED_NAME:
898 case DEMANGLE_COMPONENT_TAGGED_NAME:
899 case DEMANGLE_COMPONENT_TEMPLATE:
900 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
901 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
902 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
903 case DEMANGLE_COMPONENT_UNARY:
904 case DEMANGLE_COMPONENT_BINARY:
905 case DEMANGLE_COMPONENT_BINARY_ARGS:
906 case DEMANGLE_COMPONENT_TRINARY:
907 case DEMANGLE_COMPONENT_TRINARY_ARG1:
908 case DEMANGLE_COMPONENT_LITERAL:
909 case DEMANGLE_COMPONENT_LITERAL_NEG:
910 case DEMANGLE_COMPONENT_COMPOUND_NAME:
911 case DEMANGLE_COMPONENT_VECTOR_TYPE:
912 case DEMANGLE_COMPONENT_CLONE:
913 if (left == NULL || right == NULL)
914 return NULL;
915 break;
917 /* These types only require one parameter. */
918 case DEMANGLE_COMPONENT_VTABLE:
919 case DEMANGLE_COMPONENT_VTT:
920 case DEMANGLE_COMPONENT_TYPEINFO:
921 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
922 case DEMANGLE_COMPONENT_TYPEINFO_FN:
923 case DEMANGLE_COMPONENT_THUNK:
924 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
925 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
926 case DEMANGLE_COMPONENT_JAVA_CLASS:
927 case DEMANGLE_COMPONENT_GUARD:
928 case DEMANGLE_COMPONENT_TLS_INIT:
929 case DEMANGLE_COMPONENT_TLS_WRAPPER:
930 case DEMANGLE_COMPONENT_REFTEMP:
931 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
932 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
933 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
934 case DEMANGLE_COMPONENT_POINTER:
935 case DEMANGLE_COMPONENT_REFERENCE:
936 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
937 case DEMANGLE_COMPONENT_COMPLEX:
938 case DEMANGLE_COMPONENT_IMAGINARY:
939 case DEMANGLE_COMPONENT_VENDOR_TYPE:
940 case DEMANGLE_COMPONENT_CAST:
941 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
942 case DEMANGLE_COMPONENT_DECLTYPE:
943 case DEMANGLE_COMPONENT_PACK_EXPANSION:
944 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
945 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
946 case DEMANGLE_COMPONENT_NULLARY:
947 case DEMANGLE_COMPONENT_TRINARY_ARG2:
948 if (left == NULL)
949 return NULL;
950 break;
952 /* This needs a right parameter, but the left parameter can be
953 empty. */
954 case DEMANGLE_COMPONENT_ARRAY_TYPE:
955 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
956 if (right == NULL)
957 return NULL;
958 break;
960 /* These are allowed to have no parameters--in some cases they
961 will be filled in later. */
962 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
963 case DEMANGLE_COMPONENT_RESTRICT:
964 case DEMANGLE_COMPONENT_VOLATILE:
965 case DEMANGLE_COMPONENT_CONST:
966 case DEMANGLE_COMPONENT_RESTRICT_THIS:
967 case DEMANGLE_COMPONENT_VOLATILE_THIS:
968 case DEMANGLE_COMPONENT_CONST_THIS:
969 case DEMANGLE_COMPONENT_REFERENCE_THIS:
970 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
971 case DEMANGLE_COMPONENT_ARGLIST:
972 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
973 break;
975 /* Other types should not be seen here. */
976 default:
977 return NULL;
980 p = d_make_empty (di);
981 if (p != NULL)
983 p->type = type;
984 p->u.s_binary.left = left;
985 p->u.s_binary.right = right;
987 return p;
990 /* Add a new demangle mangled name component. */
992 static struct demangle_component *
993 d_make_demangle_mangled_name (struct d_info *di, const char *s)
995 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
996 return d_make_name (di, s, strlen (s));
997 d_advance (di, 2);
998 return d_encoding (di, 0);
1001 /* Add a new name component. */
1003 static struct demangle_component *
1004 d_make_name (struct d_info *di, const char *s, int len)
1006 struct demangle_component *p;
1008 p = d_make_empty (di);
1009 if (! cplus_demangle_fill_name (p, s, len))
1010 return NULL;
1011 return p;
1014 /* Add a new builtin type component. */
1016 static struct demangle_component *
1017 d_make_builtin_type (struct d_info *di,
1018 const struct demangle_builtin_type_info *type)
1020 struct demangle_component *p;
1022 if (type == NULL)
1023 return NULL;
1024 p = d_make_empty (di);
1025 if (p != NULL)
1027 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1028 p->u.s_builtin.type = type;
1030 return p;
1033 /* Add a new operator component. */
1035 static struct demangle_component *
1036 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1038 struct demangle_component *p;
1040 p = d_make_empty (di);
1041 if (p != NULL)
1043 p->type = DEMANGLE_COMPONENT_OPERATOR;
1044 p->u.s_operator.op = op;
1046 return p;
1049 /* Add a new extended operator component. */
1051 static struct demangle_component *
1052 d_make_extended_operator (struct d_info *di, int args,
1053 struct demangle_component *name)
1055 struct demangle_component *p;
1057 p = d_make_empty (di);
1058 if (! cplus_demangle_fill_extended_operator (p, args, name))
1059 return NULL;
1060 return p;
1063 static struct demangle_component *
1064 d_make_default_arg (struct d_info *di, int num,
1065 struct demangle_component *sub)
1067 struct demangle_component *p = d_make_empty (di);
1068 if (p)
1070 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1071 p->u.s_unary_num.num = num;
1072 p->u.s_unary_num.sub = sub;
1074 return p;
1077 /* Add a new constructor component. */
1079 static struct demangle_component *
1080 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1081 struct demangle_component *name)
1083 struct demangle_component *p;
1085 p = d_make_empty (di);
1086 if (! cplus_demangle_fill_ctor (p, kind, name))
1087 return NULL;
1088 return p;
1091 /* Add a new destructor component. */
1093 static struct demangle_component *
1094 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1095 struct demangle_component *name)
1097 struct demangle_component *p;
1099 p = d_make_empty (di);
1100 if (! cplus_demangle_fill_dtor (p, kind, name))
1101 return NULL;
1102 return p;
1105 /* Add a new template parameter. */
1107 static struct demangle_component *
1108 d_make_template_param (struct d_info *di, long i)
1110 struct demangle_component *p;
1112 p = d_make_empty (di);
1113 if (p != NULL)
1115 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1116 p->u.s_number.number = i;
1118 return p;
1121 /* Add a new function parameter. */
1123 static struct demangle_component *
1124 d_make_function_param (struct d_info *di, long i)
1126 struct demangle_component *p;
1128 p = d_make_empty (di);
1129 if (p != NULL)
1131 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1132 p->u.s_number.number = i;
1134 return p;
1137 /* Add a new standard substitution component. */
1139 static struct demangle_component *
1140 d_make_sub (struct d_info *di, const char *name, int len)
1142 struct demangle_component *p;
1144 p = d_make_empty (di);
1145 if (p != NULL)
1147 p->type = DEMANGLE_COMPONENT_SUB_STD;
1148 p->u.s_string.string = name;
1149 p->u.s_string.len = len;
1151 return p;
1154 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1156 TOP_LEVEL is non-zero when called at the top level. */
1158 CP_STATIC_IF_GLIBCPP_V3
1159 struct demangle_component *
1160 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1162 struct demangle_component *p;
1164 if (! d_check_char (di, '_')
1165 /* Allow missing _ if not at toplevel to work around a
1166 bug in G++ abi-version=2 mangling; see the comment in
1167 write_template_arg. */
1168 && top_level)
1169 return NULL;
1170 if (! d_check_char (di, 'Z'))
1171 return NULL;
1172 p = d_encoding (di, top_level);
1174 /* If at top level and parsing parameters, check for a clone
1175 suffix. */
1176 if (top_level && (di->options & DMGL_PARAMS) != 0)
1177 while (d_peek_char (di) == '.'
1178 && (IS_LOWER (d_peek_next_char (di))
1179 || d_peek_next_char (di) == '_'
1180 || IS_DIGIT (d_peek_next_char (di))))
1181 p = d_clone_suffix (di, p);
1183 return p;
1186 /* Return whether a function should have a return type. The argument
1187 is the function name, which may be qualified in various ways. The
1188 rules are that template functions have return types with some
1189 exceptions, function types which are not part of a function name
1190 mangling have return types with some exceptions, and non-template
1191 function names do not have return types. The exceptions are that
1192 constructors, destructors, and conversion operators do not have
1193 return types. */
1195 static int
1196 has_return_type (struct demangle_component *dc)
1198 if (dc == NULL)
1199 return 0;
1200 switch (dc->type)
1202 default:
1203 return 0;
1204 case DEMANGLE_COMPONENT_TEMPLATE:
1205 return ! is_ctor_dtor_or_conversion (d_left (dc));
1206 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1207 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1208 case DEMANGLE_COMPONENT_CONST_THIS:
1209 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1210 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1211 return has_return_type (d_left (dc));
1215 /* Return whether a name is a constructor, a destructor, or a
1216 conversion operator. */
1218 static int
1219 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1221 if (dc == NULL)
1222 return 0;
1223 switch (dc->type)
1225 default:
1226 return 0;
1227 case DEMANGLE_COMPONENT_QUAL_NAME:
1228 case DEMANGLE_COMPONENT_LOCAL_NAME:
1229 return is_ctor_dtor_or_conversion (d_right (dc));
1230 case DEMANGLE_COMPONENT_CTOR:
1231 case DEMANGLE_COMPONENT_DTOR:
1232 case DEMANGLE_COMPONENT_CAST:
1233 return 1;
1237 /* <encoding> ::= <(function) name> <bare-function-type>
1238 ::= <(data) name>
1239 ::= <special-name>
1241 TOP_LEVEL is non-zero when called at the top level, in which case
1242 if DMGL_PARAMS is not set we do not demangle the function
1243 parameters. We only set this at the top level, because otherwise
1244 we would not correctly demangle names in local scopes. */
1246 static struct demangle_component *
1247 d_encoding (struct d_info *di, int top_level)
1249 char peek = d_peek_char (di);
1251 if (peek == 'G' || peek == 'T')
1252 return d_special_name (di);
1253 else
1255 struct demangle_component *dc;
1257 dc = d_name (di);
1259 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1261 /* Strip off any initial CV-qualifiers, as they really apply
1262 to the `this' parameter, and they were not output by the
1263 v2 demangler without DMGL_PARAMS. */
1264 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1265 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1266 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1267 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1268 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1269 dc = d_left (dc);
1271 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1272 there may be CV-qualifiers on its right argument which
1273 really apply here; this happens when parsing a class
1274 which is local to a function. */
1275 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1277 struct demangle_component *dcr;
1279 dcr = d_right (dc);
1280 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1281 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1282 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1283 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1284 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1285 dcr = d_left (dcr);
1286 dc->u.s_binary.right = dcr;
1289 return dc;
1292 peek = d_peek_char (di);
1293 if (dc == NULL || peek == '\0' || peek == 'E')
1294 return dc;
1295 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1296 d_bare_function_type (di, has_return_type (dc)));
1300 /* <tagged-name> ::= <name> B <source-name> */
1302 static struct demangle_component *
1303 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1305 char peek;
1306 while (peek = d_peek_char (di),
1307 peek == 'B')
1309 struct demangle_component *tag;
1310 d_advance (di, 1);
1311 tag = d_source_name (di);
1312 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1314 return dc;
1317 /* <name> ::= <nested-name>
1318 ::= <unscoped-name>
1319 ::= <unscoped-template-name> <template-args>
1320 ::= <local-name>
1322 <unscoped-name> ::= <unqualified-name>
1323 ::= St <unqualified-name>
1325 <unscoped-template-name> ::= <unscoped-name>
1326 ::= <substitution>
1329 static struct demangle_component *
1330 d_name (struct d_info *di)
1332 char peek = d_peek_char (di);
1333 struct demangle_component *dc;
1335 switch (peek)
1337 case 'N':
1338 return d_nested_name (di);
1340 case 'Z':
1341 return d_local_name (di);
1343 case 'U':
1344 return d_unqualified_name (di);
1346 case 'S':
1348 int subst;
1350 if (d_peek_next_char (di) != 't')
1352 dc = d_substitution (di, 0);
1353 subst = 1;
1355 else
1357 d_advance (di, 2);
1358 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1359 d_make_name (di, "std", 3),
1360 d_unqualified_name (di));
1361 di->expansion += 3;
1362 subst = 0;
1365 if (d_peek_char (di) != 'I')
1367 /* The grammar does not permit this case to occur if we
1368 called d_substitution() above (i.e., subst == 1). We
1369 don't bother to check. */
1371 else
1373 /* This is <template-args>, which means that we just saw
1374 <unscoped-template-name>, which is a substitution
1375 candidate if we didn't just get it from a
1376 substitution. */
1377 if (! subst)
1379 if (! d_add_substitution (di, dc))
1380 return NULL;
1382 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1383 d_template_args (di));
1386 return dc;
1389 case 'L':
1390 default:
1391 dc = d_unqualified_name (di);
1392 if (d_peek_char (di) == 'I')
1394 /* This is <template-args>, which means that we just saw
1395 <unscoped-template-name>, which is a substitution
1396 candidate. */
1397 if (! d_add_substitution (di, dc))
1398 return NULL;
1399 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1400 d_template_args (di));
1402 return dc;
1406 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1407 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1410 static struct demangle_component *
1411 d_nested_name (struct d_info *di)
1413 struct demangle_component *ret;
1414 struct demangle_component **pret;
1415 struct demangle_component *rqual;
1417 if (! d_check_char (di, 'N'))
1418 return NULL;
1420 pret = d_cv_qualifiers (di, &ret, 1);
1421 if (pret == NULL)
1422 return NULL;
1424 /* Parse the ref-qualifier now and then attach it
1425 once we have something to attach it to. */
1426 rqual = d_ref_qualifier (di, NULL);
1428 *pret = d_prefix (di);
1429 if (*pret == NULL)
1430 return NULL;
1432 if (rqual)
1434 d_left (rqual) = ret;
1435 ret = rqual;
1438 if (! d_check_char (di, 'E'))
1439 return NULL;
1441 return ret;
1444 /* <prefix> ::= <prefix> <unqualified-name>
1445 ::= <template-prefix> <template-args>
1446 ::= <template-param>
1447 ::= <decltype>
1449 ::= <substitution>
1451 <template-prefix> ::= <prefix> <(template) unqualified-name>
1452 ::= <template-param>
1453 ::= <substitution>
1456 static struct demangle_component *
1457 d_prefix (struct d_info *di)
1459 struct demangle_component *ret = NULL;
1461 while (1)
1463 char peek;
1464 enum demangle_component_type comb_type;
1465 struct demangle_component *dc;
1467 peek = d_peek_char (di);
1468 if (peek == '\0')
1469 return NULL;
1471 /* The older code accepts a <local-name> here, but I don't see
1472 that in the grammar. The older code does not accept a
1473 <template-param> here. */
1475 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1476 if (peek == 'D')
1478 char peek2 = d_peek_next_char (di);
1479 if (peek2 == 'T' || peek2 == 't')
1480 /* Decltype. */
1481 dc = cplus_demangle_type (di);
1482 else
1483 /* Destructor name. */
1484 dc = d_unqualified_name (di);
1486 else if (IS_DIGIT (peek)
1487 || IS_LOWER (peek)
1488 || peek == 'C'
1489 || peek == 'U'
1490 || peek == 'L')
1491 dc = d_unqualified_name (di);
1492 else if (peek == 'S')
1493 dc = d_substitution (di, 1);
1494 else if (peek == 'I')
1496 if (ret == NULL)
1497 return NULL;
1498 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1499 dc = d_template_args (di);
1501 else if (peek == 'T')
1502 dc = d_template_param (di);
1503 else if (peek == 'E')
1504 return ret;
1505 else if (peek == 'M')
1507 /* Initializer scope for a lambda. We don't need to represent
1508 this; the normal code will just treat the variable as a type
1509 scope, which gives appropriate output. */
1510 if (ret == NULL)
1511 return NULL;
1512 d_advance (di, 1);
1513 continue;
1515 else
1516 return NULL;
1518 if (ret == NULL)
1519 ret = dc;
1520 else
1521 ret = d_make_comp (di, comb_type, ret, dc);
1523 if (peek != 'S' && d_peek_char (di) != 'E')
1525 if (! d_add_substitution (di, ret))
1526 return NULL;
1531 /* <unqualified-name> ::= <operator-name>
1532 ::= <ctor-dtor-name>
1533 ::= <source-name>
1534 ::= <local-source-name>
1536 <local-source-name> ::= L <source-name> <discriminator>
1539 static struct demangle_component *
1540 d_unqualified_name (struct d_info *di)
1542 struct demangle_component *ret;
1543 char peek;
1545 peek = d_peek_char (di);
1546 if (IS_DIGIT (peek))
1547 ret = d_source_name (di);
1548 else if (IS_LOWER (peek))
1550 ret = d_operator_name (di);
1551 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1553 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1554 if (!strcmp (ret->u.s_operator.op->code, "li"))
1555 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1556 d_source_name (di));
1559 else if (peek == 'C' || peek == 'D')
1560 ret = d_ctor_dtor_name (di);
1561 else if (peek == 'L')
1563 d_advance (di, 1);
1565 ret = d_source_name (di);
1566 if (ret == NULL)
1567 return NULL;
1568 if (! d_discriminator (di))
1569 return NULL;
1571 else if (peek == 'U')
1573 switch (d_peek_next_char (di))
1575 case 'l':
1576 ret = d_lambda (di);
1577 break;
1578 case 't':
1579 ret = d_unnamed_type (di);
1580 break;
1581 default:
1582 return NULL;
1585 else
1586 return NULL;
1588 if (d_peek_char (di) == 'B')
1589 ret = d_abi_tags (di, ret);
1590 return ret;
1593 /* <source-name> ::= <(positive length) number> <identifier> */
1595 static struct demangle_component *
1596 d_source_name (struct d_info *di)
1598 long len;
1599 struct demangle_component *ret;
1601 len = d_number (di);
1602 if (len <= 0)
1603 return NULL;
1604 ret = d_identifier (di, len);
1605 di->last_name = ret;
1606 return ret;
1609 /* number ::= [n] <(non-negative decimal integer)> */
1611 static long
1612 d_number (struct d_info *di)
1614 int negative;
1615 char peek;
1616 long ret;
1618 negative = 0;
1619 peek = d_peek_char (di);
1620 if (peek == 'n')
1622 negative = 1;
1623 d_advance (di, 1);
1624 peek = d_peek_char (di);
1627 ret = 0;
1628 while (1)
1630 if (! IS_DIGIT (peek))
1632 if (negative)
1633 ret = - ret;
1634 return ret;
1636 ret = ret * 10 + peek - '0';
1637 d_advance (di, 1);
1638 peek = d_peek_char (di);
1642 /* Like d_number, but returns a demangle_component. */
1644 static struct demangle_component *
1645 d_number_component (struct d_info *di)
1647 struct demangle_component *ret = d_make_empty (di);
1648 if (ret)
1650 ret->type = DEMANGLE_COMPONENT_NUMBER;
1651 ret->u.s_number.number = d_number (di);
1653 return ret;
1656 /* identifier ::= <(unqualified source code identifier)> */
1658 static struct demangle_component *
1659 d_identifier (struct d_info *di, int len)
1661 const char *name;
1663 name = d_str (di);
1665 if (di->send - name < len)
1666 return NULL;
1668 d_advance (di, len);
1670 /* A Java mangled name may have a trailing '$' if it is a C++
1671 keyword. This '$' is not included in the length count. We just
1672 ignore the '$'. */
1673 if ((di->options & DMGL_JAVA) != 0
1674 && d_peek_char (di) == '$')
1675 d_advance (di, 1);
1677 /* Look for something which looks like a gcc encoding of an
1678 anonymous namespace, and replace it with a more user friendly
1679 name. */
1680 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1681 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1682 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1684 const char *s;
1686 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1687 if ((*s == '.' || *s == '_' || *s == '$')
1688 && s[1] == 'N')
1690 di->expansion -= len - sizeof "(anonymous namespace)";
1691 return d_make_name (di, "(anonymous namespace)",
1692 sizeof "(anonymous namespace)" - 1);
1696 return d_make_name (di, name, len);
1699 /* operator_name ::= many different two character encodings.
1700 ::= cv <type>
1701 ::= v <digit> <source-name>
1703 This list is sorted for binary search. */
1705 #define NL(s) s, (sizeof s) - 1
1707 CP_STATIC_IF_GLIBCPP_V3
1708 const struct demangle_operator_info cplus_demangle_operators[] =
1710 { "aN", NL ("&="), 2 },
1711 { "aS", NL ("="), 2 },
1712 { "aa", NL ("&&"), 2 },
1713 { "ad", NL ("&"), 1 },
1714 { "an", NL ("&"), 2 },
1715 { "at", NL ("alignof "), 1 },
1716 { "az", NL ("alignof "), 1 },
1717 { "cc", NL ("const_cast"), 2 },
1718 { "cl", NL ("()"), 2 },
1719 { "cm", NL (","), 2 },
1720 { "co", NL ("~"), 1 },
1721 { "dV", NL ("/="), 2 },
1722 { "da", NL ("delete[] "), 1 },
1723 { "dc", NL ("dynamic_cast"), 2 },
1724 { "de", NL ("*"), 1 },
1725 { "dl", NL ("delete "), 1 },
1726 { "ds", NL (".*"), 2 },
1727 { "dt", NL ("."), 2 },
1728 { "dv", NL ("/"), 2 },
1729 { "eO", NL ("^="), 2 },
1730 { "eo", NL ("^"), 2 },
1731 { "eq", NL ("=="), 2 },
1732 { "ge", NL (">="), 2 },
1733 { "gs", NL ("::"), 1 },
1734 { "gt", NL (">"), 2 },
1735 { "ix", NL ("[]"), 2 },
1736 { "lS", NL ("<<="), 2 },
1737 { "le", NL ("<="), 2 },
1738 { "li", NL ("operator\"\" "), 1 },
1739 { "ls", NL ("<<"), 2 },
1740 { "lt", NL ("<"), 2 },
1741 { "mI", NL ("-="), 2 },
1742 { "mL", NL ("*="), 2 },
1743 { "mi", NL ("-"), 2 },
1744 { "ml", NL ("*"), 2 },
1745 { "mm", NL ("--"), 1 },
1746 { "na", NL ("new[]"), 3 },
1747 { "ne", NL ("!="), 2 },
1748 { "ng", NL ("-"), 1 },
1749 { "nt", NL ("!"), 1 },
1750 { "nw", NL ("new"), 3 },
1751 { "oR", NL ("|="), 2 },
1752 { "oo", NL ("||"), 2 },
1753 { "or", NL ("|"), 2 },
1754 { "pL", NL ("+="), 2 },
1755 { "pl", NL ("+"), 2 },
1756 { "pm", NL ("->*"), 2 },
1757 { "pp", NL ("++"), 1 },
1758 { "ps", NL ("+"), 1 },
1759 { "pt", NL ("->"), 2 },
1760 { "qu", NL ("?"), 3 },
1761 { "rM", NL ("%="), 2 },
1762 { "rS", NL (">>="), 2 },
1763 { "rc", NL ("reinterpret_cast"), 2 },
1764 { "rm", NL ("%"), 2 },
1765 { "rs", NL (">>"), 2 },
1766 { "sc", NL ("static_cast"), 2 },
1767 { "st", NL ("sizeof "), 1 },
1768 { "sz", NL ("sizeof "), 1 },
1769 { "tr", NL ("throw"), 0 },
1770 { "tw", NL ("throw "), 1 },
1771 { NULL, NULL, 0, 0 }
1774 static struct demangle_component *
1775 d_operator_name (struct d_info *di)
1777 char c1;
1778 char c2;
1780 c1 = d_next_char (di);
1781 c2 = d_next_char (di);
1782 if (c1 == 'v' && IS_DIGIT (c2))
1783 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1784 else if (c1 == 'c' && c2 == 'v')
1786 struct demangle_component *type;
1787 int was_conversion = di->is_conversion;
1789 di->is_conversion = ! di->is_expression;
1790 type = cplus_demangle_type (di);
1791 di->is_conversion = was_conversion;
1792 return d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1794 else
1796 /* LOW is the inclusive lower bound. */
1797 int low = 0;
1798 /* HIGH is the exclusive upper bound. We subtract one to ignore
1799 the sentinel at the end of the array. */
1800 int high = ((sizeof (cplus_demangle_operators)
1801 / sizeof (cplus_demangle_operators[0]))
1802 - 1);
1804 while (1)
1806 int i;
1807 const struct demangle_operator_info *p;
1809 i = low + (high - low) / 2;
1810 p = cplus_demangle_operators + i;
1812 if (c1 == p->code[0] && c2 == p->code[1])
1813 return d_make_operator (di, p);
1815 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1816 high = i;
1817 else
1818 low = i + 1;
1819 if (low == high)
1820 return NULL;
1825 static struct demangle_component *
1826 d_make_character (struct d_info *di, int c)
1828 struct demangle_component *p;
1829 p = d_make_empty (di);
1830 if (p != NULL)
1832 p->type = DEMANGLE_COMPONENT_CHARACTER;
1833 p->u.s_character.character = c;
1835 return p;
1838 static struct demangle_component *
1839 d_java_resource (struct d_info *di)
1841 struct demangle_component *p = NULL;
1842 struct demangle_component *next = NULL;
1843 long len, i;
1844 char c;
1845 const char *str;
1847 len = d_number (di);
1848 if (len <= 1)
1849 return NULL;
1851 /* Eat the leading '_'. */
1852 if (d_next_char (di) != '_')
1853 return NULL;
1854 len--;
1856 str = d_str (di);
1857 i = 0;
1859 while (len > 0)
1861 c = str[i];
1862 if (!c)
1863 return NULL;
1865 /* Each chunk is either a '$' escape... */
1866 if (c == '$')
1868 i++;
1869 switch (str[i++])
1871 case 'S':
1872 c = '/';
1873 break;
1874 case '_':
1875 c = '.';
1876 break;
1877 case '$':
1878 c = '$';
1879 break;
1880 default:
1881 return NULL;
1883 next = d_make_character (di, c);
1884 d_advance (di, i);
1885 str = d_str (di);
1886 len -= i;
1887 i = 0;
1888 if (next == NULL)
1889 return NULL;
1891 /* ... or a sequence of characters. */
1892 else
1894 while (i < len && str[i] && str[i] != '$')
1895 i++;
1897 next = d_make_name (di, str, i);
1898 d_advance (di, i);
1899 str = d_str (di);
1900 len -= i;
1901 i = 0;
1902 if (next == NULL)
1903 return NULL;
1906 if (p == NULL)
1907 p = next;
1908 else
1910 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1911 if (p == NULL)
1912 return NULL;
1916 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1918 return p;
1921 /* <special-name> ::= TV <type>
1922 ::= TT <type>
1923 ::= TI <type>
1924 ::= TS <type>
1925 ::= GV <(object) name>
1926 ::= T <call-offset> <(base) encoding>
1927 ::= Tc <call-offset> <call-offset> <(base) encoding>
1928 Also g++ extensions:
1929 ::= TC <type> <(offset) number> _ <(base) type>
1930 ::= TF <type>
1931 ::= TJ <type>
1932 ::= GR <name>
1933 ::= GA <encoding>
1934 ::= Gr <resource name>
1935 ::= GTt <encoding>
1936 ::= GTn <encoding>
1939 static struct demangle_component *
1940 d_special_name (struct d_info *di)
1942 di->expansion += 20;
1943 if (d_check_char (di, 'T'))
1945 switch (d_next_char (di))
1947 case 'V':
1948 di->expansion -= 5;
1949 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1950 cplus_demangle_type (di), NULL);
1951 case 'T':
1952 di->expansion -= 10;
1953 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1954 cplus_demangle_type (di), NULL);
1955 case 'I':
1956 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1957 cplus_demangle_type (di), NULL);
1958 case 'S':
1959 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1960 cplus_demangle_type (di), NULL);
1962 case 'h':
1963 if (! d_call_offset (di, 'h'))
1964 return NULL;
1965 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1966 d_encoding (di, 0), NULL);
1968 case 'v':
1969 if (! d_call_offset (di, 'v'))
1970 return NULL;
1971 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1972 d_encoding (di, 0), NULL);
1974 case 'c':
1975 if (! d_call_offset (di, '\0'))
1976 return NULL;
1977 if (! d_call_offset (di, '\0'))
1978 return NULL;
1979 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1980 d_encoding (di, 0), NULL);
1982 case 'C':
1984 struct demangle_component *derived_type;
1985 long offset;
1986 struct demangle_component *base_type;
1988 derived_type = cplus_demangle_type (di);
1989 offset = d_number (di);
1990 if (offset < 0)
1991 return NULL;
1992 if (! d_check_char (di, '_'))
1993 return NULL;
1994 base_type = cplus_demangle_type (di);
1995 /* We don't display the offset. FIXME: We should display
1996 it in verbose mode. */
1997 di->expansion += 5;
1998 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1999 base_type, derived_type);
2002 case 'F':
2003 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2004 cplus_demangle_type (di), NULL);
2005 case 'J':
2006 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2007 cplus_demangle_type (di), NULL);
2009 case 'H':
2010 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2011 d_name (di), NULL);
2013 case 'W':
2014 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2015 d_name (di), NULL);
2017 default:
2018 return NULL;
2021 else if (d_check_char (di, 'G'))
2023 switch (d_next_char (di))
2025 case 'V':
2026 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2028 case 'R':
2030 struct demangle_component *name = d_name (di);
2031 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2032 d_number_component (di));
2035 case 'A':
2036 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2037 d_encoding (di, 0), NULL);
2039 case 'T':
2040 switch (d_next_char (di))
2042 case 'n':
2043 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2044 d_encoding (di, 0), NULL);
2045 default:
2046 /* ??? The proposal is that other letters (such as 'h') stand
2047 for different variants of transaction cloning, such as
2048 compiling directly for hardware transaction support. But
2049 they still should all be transactional clones of some sort
2050 so go ahead and call them that. */
2051 case 't':
2052 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2053 d_encoding (di, 0), NULL);
2056 case 'r':
2057 return d_java_resource (di);
2059 default:
2060 return NULL;
2063 else
2064 return NULL;
2067 /* <call-offset> ::= h <nv-offset> _
2068 ::= v <v-offset> _
2070 <nv-offset> ::= <(offset) number>
2072 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2074 The C parameter, if not '\0', is a character we just read which is
2075 the start of the <call-offset>.
2077 We don't display the offset information anywhere. FIXME: We should
2078 display it in verbose mode. */
2080 static int
2081 d_call_offset (struct d_info *di, int c)
2083 if (c == '\0')
2084 c = d_next_char (di);
2086 if (c == 'h')
2087 d_number (di);
2088 else if (c == 'v')
2090 d_number (di);
2091 if (! d_check_char (di, '_'))
2092 return 0;
2093 d_number (di);
2095 else
2096 return 0;
2098 if (! d_check_char (di, '_'))
2099 return 0;
2101 return 1;
2104 /* <ctor-dtor-name> ::= C1
2105 ::= C2
2106 ::= C3
2107 ::= D0
2108 ::= D1
2109 ::= D2
2112 static struct demangle_component *
2113 d_ctor_dtor_name (struct d_info *di)
2115 if (di->last_name != NULL)
2117 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2118 di->expansion += di->last_name->u.s_name.len;
2119 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2120 di->expansion += di->last_name->u.s_string.len;
2122 switch (d_peek_char (di))
2124 case 'C':
2126 enum gnu_v3_ctor_kinds kind;
2128 switch (d_peek_next_char (di))
2130 case '1':
2131 kind = gnu_v3_complete_object_ctor;
2132 break;
2133 case '2':
2134 kind = gnu_v3_base_object_ctor;
2135 break;
2136 case '3':
2137 kind = gnu_v3_complete_object_allocating_ctor;
2138 break;
2139 case '4':
2140 kind = gnu_v3_unified_ctor;
2141 break;
2142 case '5':
2143 kind = gnu_v3_object_ctor_group;
2144 break;
2145 default:
2146 return NULL;
2148 d_advance (di, 2);
2149 return d_make_ctor (di, kind, di->last_name);
2152 case 'D':
2154 enum gnu_v3_dtor_kinds kind;
2156 switch (d_peek_next_char (di))
2158 case '0':
2159 kind = gnu_v3_deleting_dtor;
2160 break;
2161 case '1':
2162 kind = gnu_v3_complete_object_dtor;
2163 break;
2164 case '2':
2165 kind = gnu_v3_base_object_dtor;
2166 break;
2167 /* digit '3' is not used */
2168 case '4':
2169 kind = gnu_v3_unified_dtor;
2170 break;
2171 case '5':
2172 kind = gnu_v3_object_dtor_group;
2173 break;
2174 default:
2175 return NULL;
2177 d_advance (di, 2);
2178 return d_make_dtor (di, kind, di->last_name);
2181 default:
2182 return NULL;
2186 /* <type> ::= <builtin-type>
2187 ::= <function-type>
2188 ::= <class-enum-type>
2189 ::= <array-type>
2190 ::= <pointer-to-member-type>
2191 ::= <template-param>
2192 ::= <template-template-param> <template-args>
2193 ::= <substitution>
2194 ::= <CV-qualifiers> <type>
2195 ::= P <type>
2196 ::= R <type>
2197 ::= O <type> (C++0x)
2198 ::= C <type>
2199 ::= G <type>
2200 ::= U <source-name> <type>
2202 <builtin-type> ::= various one letter codes
2203 ::= u <source-name>
2206 CP_STATIC_IF_GLIBCPP_V3
2207 const struct demangle_builtin_type_info
2208 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2210 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2211 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2212 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2213 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2214 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2215 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2216 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2217 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2218 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2219 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2220 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2221 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2222 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2223 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2224 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2225 D_PRINT_DEFAULT },
2226 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2227 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2228 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2229 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2230 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2231 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2232 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2233 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2234 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2235 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2236 D_PRINT_UNSIGNED_LONG_LONG },
2237 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2238 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2239 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2240 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2241 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2242 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2243 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2244 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2245 D_PRINT_DEFAULT },
2248 CP_STATIC_IF_GLIBCPP_V3
2249 struct demangle_component *
2250 cplus_demangle_type (struct d_info *di)
2252 char peek;
2253 struct demangle_component *ret;
2254 int can_subst;
2256 /* The ABI specifies that when CV-qualifiers are used, the base type
2257 is substitutable, and the fully qualified type is substitutable,
2258 but the base type with a strict subset of the CV-qualifiers is
2259 not substitutable. The natural recursive implementation of the
2260 CV-qualifiers would cause subsets to be substitutable, so instead
2261 we pull them all off now.
2263 FIXME: The ABI says that order-insensitive vendor qualifiers
2264 should be handled in the same way, but we have no way to tell
2265 which vendor qualifiers are order-insensitive and which are
2266 order-sensitive. So we just assume that they are all
2267 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2268 __vector, and it treats it as order-sensitive when mangling
2269 names. */
2271 peek = d_peek_char (di);
2272 if (peek == 'r' || peek == 'V' || peek == 'K')
2274 struct demangle_component **pret;
2276 pret = d_cv_qualifiers (di, &ret, 0);
2277 if (pret == NULL)
2278 return NULL;
2279 if (d_peek_char (di) == 'F')
2281 /* cv-qualifiers before a function type apply to 'this',
2282 so avoid adding the unqualified function type to
2283 the substitution list. */
2284 *pret = d_function_type (di);
2286 else
2287 *pret = cplus_demangle_type (di);
2288 if (!*pret)
2289 return NULL;
2290 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2291 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2293 /* Move the ref-qualifier outside the cv-qualifiers so that
2294 they are printed in the right order. */
2295 struct demangle_component *fn = d_left (*pret);
2296 d_left (*pret) = ret;
2297 ret = *pret;
2298 *pret = fn;
2300 if (! d_add_substitution (di, ret))
2301 return NULL;
2302 return ret;
2305 can_subst = 1;
2307 switch (peek)
2309 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2310 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2311 case 'o': case 's': case 't':
2312 case 'v': case 'w': case 'x': case 'y': case 'z':
2313 ret = d_make_builtin_type (di,
2314 &cplus_demangle_builtin_types[peek - 'a']);
2315 di->expansion += ret->u.s_builtin.type->len;
2316 can_subst = 0;
2317 d_advance (di, 1);
2318 break;
2320 case 'u':
2321 d_advance (di, 1);
2322 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2323 d_source_name (di), NULL);
2324 break;
2326 case 'F':
2327 ret = d_function_type (di);
2328 break;
2330 case '0': case '1': case '2': case '3': case '4':
2331 case '5': case '6': case '7': case '8': case '9':
2332 case 'N':
2333 case 'Z':
2334 ret = d_class_enum_type (di);
2335 break;
2337 case 'A':
2338 ret = d_array_type (di);
2339 break;
2341 case 'M':
2342 ret = d_pointer_to_member_type (di);
2343 break;
2345 case 'T':
2346 ret = d_template_param (di);
2347 if (d_peek_char (di) == 'I')
2349 /* This may be <template-template-param> <template-args>.
2350 If this is the type for a conversion operator, we can
2351 have a <template-template-param> here only by following
2352 a derivation like this:
2354 <nested-name>
2355 -> <template-prefix> <template-args>
2356 -> <prefix> <template-unqualified-name> <template-args>
2357 -> <unqualified-name> <template-unqualified-name> <template-args>
2358 -> <source-name> <template-unqualified-name> <template-args>
2359 -> <source-name> <operator-name> <template-args>
2360 -> <source-name> cv <type> <template-args>
2361 -> <source-name> cv <template-template-param> <template-args> <template-args>
2363 where the <template-args> is followed by another.
2364 Otherwise, we must have a derivation like this:
2366 <nested-name>
2367 -> <template-prefix> <template-args>
2368 -> <prefix> <template-unqualified-name> <template-args>
2369 -> <unqualified-name> <template-unqualified-name> <template-args>
2370 -> <source-name> <template-unqualified-name> <template-args>
2371 -> <source-name> <operator-name> <template-args>
2372 -> <source-name> cv <type> <template-args>
2373 -> <source-name> cv <template-param> <template-args>
2375 where we need to leave the <template-args> to be processed
2376 by d_prefix (following the <template-prefix>).
2378 The <template-template-param> part is a substitution
2379 candidate. */
2380 if (! di->is_conversion)
2382 if (! d_add_substitution (di, ret))
2383 return NULL;
2384 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2385 d_template_args (di));
2387 else
2389 struct demangle_component *args;
2390 struct d_info_checkpoint checkpoint;
2392 d_checkpoint (di, &checkpoint);
2393 args = d_template_args (di);
2394 if (d_peek_char (di) == 'I')
2396 if (! d_add_substitution (di, ret))
2397 return NULL;
2398 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2399 args);
2401 else
2402 d_backtrack (di, &checkpoint);
2405 break;
2407 case 'S':
2408 /* If this is a special substitution, then it is the start of
2409 <class-enum-type>. */
2411 char peek_next;
2413 peek_next = d_peek_next_char (di);
2414 if (IS_DIGIT (peek_next)
2415 || peek_next == '_'
2416 || IS_UPPER (peek_next))
2418 ret = d_substitution (di, 0);
2419 /* The substituted name may have been a template name and
2420 may be followed by tepmlate args. */
2421 if (d_peek_char (di) == 'I')
2422 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2423 d_template_args (di));
2424 else
2425 can_subst = 0;
2427 else
2429 ret = d_class_enum_type (di);
2430 /* If the substitution was a complete type, then it is not
2431 a new substitution candidate. However, if the
2432 substitution was followed by template arguments, then
2433 the whole thing is a substitution candidate. */
2434 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2435 can_subst = 0;
2438 break;
2440 case 'O':
2441 d_advance (di, 1);
2442 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2443 cplus_demangle_type (di), NULL);
2444 break;
2446 case 'P':
2447 d_advance (di, 1);
2448 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2449 cplus_demangle_type (di), NULL);
2450 break;
2452 case 'R':
2453 d_advance (di, 1);
2454 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2455 cplus_demangle_type (di), NULL);
2456 break;
2458 case 'C':
2459 d_advance (di, 1);
2460 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2461 cplus_demangle_type (di), NULL);
2462 break;
2464 case 'G':
2465 d_advance (di, 1);
2466 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2467 cplus_demangle_type (di), NULL);
2468 break;
2470 case 'U':
2471 d_advance (di, 1);
2472 ret = d_source_name (di);
2473 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2474 cplus_demangle_type (di), ret);
2475 break;
2477 case 'D':
2478 can_subst = 0;
2479 d_advance (di, 1);
2480 peek = d_next_char (di);
2481 switch (peek)
2483 case 'T':
2484 case 't':
2485 /* decltype (expression) */
2486 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2487 d_expression (di), NULL);
2488 if (ret && d_next_char (di) != 'E')
2489 ret = NULL;
2490 can_subst = 1;
2491 break;
2493 case 'p':
2494 /* Pack expansion. */
2495 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2496 cplus_demangle_type (di), NULL);
2497 can_subst = 1;
2498 break;
2500 case 'a':
2501 /* auto */
2502 ret = d_make_name (di, "auto", 4);
2503 break;
2505 case 'f':
2506 /* 32-bit decimal floating point */
2507 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2508 di->expansion += ret->u.s_builtin.type->len;
2509 break;
2510 case 'd':
2511 /* 64-bit DFP */
2512 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2513 di->expansion += ret->u.s_builtin.type->len;
2514 break;
2515 case 'e':
2516 /* 128-bit DFP */
2517 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2518 di->expansion += ret->u.s_builtin.type->len;
2519 break;
2520 case 'h':
2521 /* 16-bit half-precision FP */
2522 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2523 di->expansion += ret->u.s_builtin.type->len;
2524 break;
2525 case 's':
2526 /* char16_t */
2527 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2528 di->expansion += ret->u.s_builtin.type->len;
2529 break;
2530 case 'i':
2531 /* char32_t */
2532 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2533 di->expansion += ret->u.s_builtin.type->len;
2534 break;
2536 case 'F':
2537 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2538 ret = d_make_empty (di);
2539 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2540 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2541 /* For demangling we don't care about the bits. */
2542 d_number (di);
2543 ret->u.s_fixed.length = cplus_demangle_type (di);
2544 if (ret->u.s_fixed.length == NULL)
2545 return NULL;
2546 d_number (di);
2547 peek = d_next_char (di);
2548 ret->u.s_fixed.sat = (peek == 's');
2549 break;
2551 case 'v':
2552 ret = d_vector_type (di);
2553 can_subst = 1;
2554 break;
2556 case 'n':
2557 /* decltype(nullptr) */
2558 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2559 di->expansion += ret->u.s_builtin.type->len;
2560 break;
2562 default:
2563 return NULL;
2565 break;
2567 default:
2568 return NULL;
2571 if (can_subst)
2573 if (! d_add_substitution (di, ret))
2574 return NULL;
2577 return ret;
2580 /* <CV-qualifiers> ::= [r] [V] [K] */
2582 static struct demangle_component **
2583 d_cv_qualifiers (struct d_info *di,
2584 struct demangle_component **pret, int member_fn)
2586 struct demangle_component **pstart;
2587 char peek;
2589 pstart = pret;
2590 peek = d_peek_char (di);
2591 while (peek == 'r' || peek == 'V' || peek == 'K')
2593 enum demangle_component_type t;
2595 d_advance (di, 1);
2596 if (peek == 'r')
2598 t = (member_fn
2599 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2600 : DEMANGLE_COMPONENT_RESTRICT);
2601 di->expansion += sizeof "restrict";
2603 else if (peek == 'V')
2605 t = (member_fn
2606 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2607 : DEMANGLE_COMPONENT_VOLATILE);
2608 di->expansion += sizeof "volatile";
2610 else
2612 t = (member_fn
2613 ? DEMANGLE_COMPONENT_CONST_THIS
2614 : DEMANGLE_COMPONENT_CONST);
2615 di->expansion += sizeof "const";
2618 *pret = d_make_comp (di, t, NULL, NULL);
2619 if (*pret == NULL)
2620 return NULL;
2621 pret = &d_left (*pret);
2623 peek = d_peek_char (di);
2626 if (!member_fn && peek == 'F')
2628 while (pstart != pret)
2630 switch ((*pstart)->type)
2632 case DEMANGLE_COMPONENT_RESTRICT:
2633 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2634 break;
2635 case DEMANGLE_COMPONENT_VOLATILE:
2636 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2637 break;
2638 case DEMANGLE_COMPONENT_CONST:
2639 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2640 break;
2641 default:
2642 break;
2644 pstart = &d_left (*pstart);
2648 return pret;
2651 /* <ref-qualifier> ::= R
2652 ::= O */
2654 static struct demangle_component *
2655 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2657 struct demangle_component *ret = sub;
2658 char peek;
2660 peek = d_peek_char (di);
2661 if (peek == 'R' || peek == 'O')
2663 enum demangle_component_type t;
2664 if (peek == 'R')
2666 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2667 di->expansion += sizeof "&";
2669 else
2671 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2672 di->expansion += sizeof "&&";
2674 d_advance (di, 1);
2676 ret = d_make_comp (di, t, ret, NULL);
2679 return ret;
2682 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2684 static struct demangle_component *
2685 d_function_type (struct d_info *di)
2687 struct demangle_component *ret;
2689 if (! d_check_char (di, 'F'))
2690 return NULL;
2691 if (d_peek_char (di) == 'Y')
2693 /* Function has C linkage. We don't print this information.
2694 FIXME: We should print it in verbose mode. */
2695 d_advance (di, 1);
2697 ret = d_bare_function_type (di, 1);
2698 ret = d_ref_qualifier (di, ret);
2700 if (! d_check_char (di, 'E'))
2701 return NULL;
2702 return ret;
2705 /* <type>+ */
2707 static struct demangle_component *
2708 d_parmlist (struct d_info *di)
2710 struct demangle_component *tl;
2711 struct demangle_component **ptl;
2713 tl = NULL;
2714 ptl = &tl;
2715 while (1)
2717 struct demangle_component *type;
2719 char peek = d_peek_char (di);
2720 if (peek == '\0' || peek == 'E' || peek == '.')
2721 break;
2722 if ((peek == 'R' || peek == 'O')
2723 && d_peek_next_char (di) == 'E')
2724 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2725 break;
2726 type = cplus_demangle_type (di);
2727 if (type == NULL)
2728 return NULL;
2729 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2730 if (*ptl == NULL)
2731 return NULL;
2732 ptl = &d_right (*ptl);
2735 /* There should be at least one parameter type besides the optional
2736 return type. A function which takes no arguments will have a
2737 single parameter type void. */
2738 if (tl == NULL)
2739 return NULL;
2741 /* If we have a single parameter type void, omit it. */
2742 if (d_right (tl) == NULL
2743 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2744 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2746 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2747 d_left (tl) = NULL;
2750 return tl;
2753 /* <bare-function-type> ::= [J]<type>+ */
2755 static struct demangle_component *
2756 d_bare_function_type (struct d_info *di, int has_return_type)
2758 struct demangle_component *return_type;
2759 struct demangle_component *tl;
2760 char peek;
2762 /* Detect special qualifier indicating that the first argument
2763 is the return type. */
2764 peek = d_peek_char (di);
2765 if (peek == 'J')
2767 d_advance (di, 1);
2768 has_return_type = 1;
2771 if (has_return_type)
2773 return_type = cplus_demangle_type (di);
2774 if (return_type == NULL)
2775 return NULL;
2777 else
2778 return_type = NULL;
2780 tl = d_parmlist (di);
2781 if (tl == NULL)
2782 return NULL;
2784 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2785 return_type, tl);
2788 /* <class-enum-type> ::= <name> */
2790 static struct demangle_component *
2791 d_class_enum_type (struct d_info *di)
2793 return d_name (di);
2796 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2797 ::= A [<(dimension) expression>] _ <(element) type>
2800 static struct demangle_component *
2801 d_array_type (struct d_info *di)
2803 char peek;
2804 struct demangle_component *dim;
2806 if (! d_check_char (di, 'A'))
2807 return NULL;
2809 peek = d_peek_char (di);
2810 if (peek == '_')
2811 dim = NULL;
2812 else if (IS_DIGIT (peek))
2814 const char *s;
2816 s = d_str (di);
2819 d_advance (di, 1);
2820 peek = d_peek_char (di);
2822 while (IS_DIGIT (peek));
2823 dim = d_make_name (di, s, d_str (di) - s);
2824 if (dim == NULL)
2825 return NULL;
2827 else
2829 dim = d_expression (di);
2830 if (dim == NULL)
2831 return NULL;
2834 if (! d_check_char (di, '_'))
2835 return NULL;
2837 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2838 cplus_demangle_type (di));
2841 /* <vector-type> ::= Dv <number> _ <type>
2842 ::= Dv _ <expression> _ <type> */
2844 static struct demangle_component *
2845 d_vector_type (struct d_info *di)
2847 char peek;
2848 struct demangle_component *dim;
2850 peek = d_peek_char (di);
2851 if (peek == '_')
2853 d_advance (di, 1);
2854 dim = d_expression (di);
2856 else
2857 dim = d_number_component (di);
2859 if (dim == NULL)
2860 return NULL;
2862 if (! d_check_char (di, '_'))
2863 return NULL;
2865 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2866 cplus_demangle_type (di));
2869 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2871 static struct demangle_component *
2872 d_pointer_to_member_type (struct d_info *di)
2874 struct demangle_component *cl;
2875 struct demangle_component *mem;
2877 if (! d_check_char (di, 'M'))
2878 return NULL;
2880 cl = cplus_demangle_type (di);
2881 if (cl == NULL)
2882 return NULL;
2884 /* The ABI says, "The type of a non-static member function is considered
2885 to be different, for the purposes of substitution, from the type of a
2886 namespace-scope or static member function whose type appears
2887 similar. The types of two non-static member functions are considered
2888 to be different, for the purposes of substitution, if the functions
2889 are members of different classes. In other words, for the purposes of
2890 substitution, the class of which the function is a member is
2891 considered part of the type of function."
2893 For a pointer to member function, this call to cplus_demangle_type
2894 will end up adding a (possibly qualified) non-member function type to
2895 the substitution table, which is not correct; however, the member
2896 function type will never be used in a substitution, so putting the
2897 wrong type in the substitution table is harmless. */
2899 mem = cplus_demangle_type (di);
2900 if (mem == NULL)
2901 return NULL;
2903 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2906 /* <non-negative number> _ */
2908 static long
2909 d_compact_number (struct d_info *di)
2911 long num;
2912 if (d_peek_char (di) == '_')
2913 num = 0;
2914 else if (d_peek_char (di) == 'n')
2915 return -1;
2916 else
2917 num = d_number (di) + 1;
2919 if (! d_check_char (di, '_'))
2920 return -1;
2921 return num;
2924 /* <template-param> ::= T_
2925 ::= T <(parameter-2 non-negative) number> _
2928 static struct demangle_component *
2929 d_template_param (struct d_info *di)
2931 long param;
2933 if (! d_check_char (di, 'T'))
2934 return NULL;
2936 param = d_compact_number (di);
2937 if (param < 0)
2938 return NULL;
2940 ++di->did_subs;
2942 return d_make_template_param (di, param);
2945 /* <template-args> ::= I <template-arg>+ E */
2947 static struct demangle_component *
2948 d_template_args (struct d_info *di)
2950 struct demangle_component *hold_last_name;
2951 struct demangle_component *al;
2952 struct demangle_component **pal;
2954 /* Preserve the last name we saw--don't let the template arguments
2955 clobber it, as that would give us the wrong name for a subsequent
2956 constructor or destructor. */
2957 hold_last_name = di->last_name;
2959 if (d_peek_char (di) != 'I'
2960 && d_peek_char (di) != 'J')
2961 return NULL;
2962 d_advance (di, 1);
2964 if (d_peek_char (di) == 'E')
2966 /* An argument pack can be empty. */
2967 d_advance (di, 1);
2968 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2971 al = NULL;
2972 pal = &al;
2973 while (1)
2975 struct demangle_component *a;
2977 a = d_template_arg (di);
2978 if (a == NULL)
2979 return NULL;
2981 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2982 if (*pal == NULL)
2983 return NULL;
2984 pal = &d_right (*pal);
2986 if (d_peek_char (di) == 'E')
2988 d_advance (di, 1);
2989 break;
2993 di->last_name = hold_last_name;
2995 return al;
2998 /* <template-arg> ::= <type>
2999 ::= X <expression> E
3000 ::= <expr-primary>
3003 static struct demangle_component *
3004 d_template_arg (struct d_info *di)
3006 struct demangle_component *ret;
3008 switch (d_peek_char (di))
3010 case 'X':
3011 d_advance (di, 1);
3012 ret = d_expression (di);
3013 if (! d_check_char (di, 'E'))
3014 return NULL;
3015 return ret;
3017 case 'L':
3018 return d_expr_primary (di);
3020 case 'I':
3021 case 'J':
3022 /* An argument pack. */
3023 return d_template_args (di);
3025 default:
3026 return cplus_demangle_type (di);
3030 /* Parse a sequence of expressions until we hit the terminator
3031 character. */
3033 static struct demangle_component *
3034 d_exprlist (struct d_info *di, char terminator)
3036 struct demangle_component *list = NULL;
3037 struct demangle_component **p = &list;
3039 if (d_peek_char (di) == terminator)
3041 d_advance (di, 1);
3042 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3045 while (1)
3047 struct demangle_component *arg = d_expression (di);
3048 if (arg == NULL)
3049 return NULL;
3051 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3052 if (*p == NULL)
3053 return NULL;
3054 p = &d_right (*p);
3056 if (d_peek_char (di) == terminator)
3058 d_advance (di, 1);
3059 break;
3063 return list;
3066 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3067 dynamic_cast, static_cast or reinterpret_cast. */
3069 static int
3070 op_is_new_cast (struct demangle_component *op)
3072 const char *code = op->u.s_operator.op->code;
3073 return (code[1] == 'c'
3074 && (code[0] == 's' || code[0] == 'd'
3075 || code[0] == 'c' || code[0] == 'r'));
3078 /* <expression> ::= <(unary) operator-name> <expression>
3079 ::= <(binary) operator-name> <expression> <expression>
3080 ::= <(trinary) operator-name> <expression> <expression> <expression>
3081 ::= cl <expression>+ E
3082 ::= st <type>
3083 ::= <template-param>
3084 ::= sr <type> <unqualified-name>
3085 ::= sr <type> <unqualified-name> <template-args>
3086 ::= <expr-primary>
3089 static inline struct demangle_component *
3090 d_expression_1 (struct d_info *di)
3092 char peek;
3094 peek = d_peek_char (di);
3095 if (peek == 'L')
3096 return d_expr_primary (di);
3097 else if (peek == 'T')
3098 return d_template_param (di);
3099 else if (peek == 's' && d_peek_next_char (di) == 'r')
3101 struct demangle_component *type;
3102 struct demangle_component *name;
3104 d_advance (di, 2);
3105 type = cplus_demangle_type (di);
3106 name = d_unqualified_name (di);
3107 if (d_peek_char (di) != 'I')
3108 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3109 else
3110 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3111 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3112 d_template_args (di)));
3114 else if (peek == 's' && d_peek_next_char (di) == 'p')
3116 d_advance (di, 2);
3117 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3118 d_expression_1 (di), NULL);
3120 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3122 /* Function parameter used in a late-specified return type. */
3123 int index;
3124 d_advance (di, 2);
3125 if (d_peek_char (di) == 'T')
3127 /* 'this' parameter. */
3128 d_advance (di, 1);
3129 index = 0;
3131 else
3133 index = d_compact_number (di) + 1;
3134 if (index == 0)
3135 return NULL;
3137 return d_make_function_param (di, index);
3139 else if (IS_DIGIT (peek)
3140 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3142 /* We can get an unqualified name as an expression in the case of
3143 a dependent function call, i.e. decltype(f(t)). */
3144 struct demangle_component *name;
3146 if (peek == 'o')
3147 /* operator-function-id, i.e. operator+(t). */
3148 d_advance (di, 2);
3150 name = d_unqualified_name (di);
3151 if (name == NULL)
3152 return NULL;
3153 if (d_peek_char (di) == 'I')
3154 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3155 d_template_args (di));
3156 else
3157 return name;
3159 else if ((peek == 'i' || peek == 't')
3160 && d_peek_next_char (di) == 'l')
3162 /* Brace-enclosed initializer list, untyped or typed. */
3163 struct demangle_component *type = NULL;
3164 if (peek == 't')
3165 type = cplus_demangle_type (di);
3166 d_advance (di, 2);
3167 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3168 type, d_exprlist (di, 'E'));
3170 else
3172 struct demangle_component *op;
3173 const char *code = NULL;
3174 int args;
3176 op = d_operator_name (di);
3177 if (op == NULL)
3178 return NULL;
3180 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3182 code = op->u.s_operator.op->code;
3183 di->expansion += op->u.s_operator.op->len - 2;
3184 if (strcmp (code, "st") == 0)
3185 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3186 cplus_demangle_type (di));
3189 switch (op->type)
3191 default:
3192 return NULL;
3193 case DEMANGLE_COMPONENT_OPERATOR:
3194 args = op->u.s_operator.op->args;
3195 break;
3196 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3197 args = op->u.s_extended_operator.args;
3198 break;
3199 case DEMANGLE_COMPONENT_CAST:
3200 args = 1;
3201 break;
3204 switch (args)
3206 case 0:
3207 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3209 case 1:
3211 struct demangle_component *operand;
3212 int suffix = 0;
3214 if (code && (code[0] == 'p' || code[0] == 'm')
3215 && code[1] == code[0])
3216 /* pp_ and mm_ are the prefix variants. */
3217 suffix = !d_check_char (di, '_');
3219 if (op->type == DEMANGLE_COMPONENT_CAST
3220 && d_check_char (di, '_'))
3221 operand = d_exprlist (di, 'E');
3222 else
3223 operand = d_expression_1 (di);
3225 if (suffix)
3226 /* Indicate the suffix variant for d_print_comp. */
3227 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3228 d_make_comp (di,
3229 DEMANGLE_COMPONENT_BINARY_ARGS,
3230 operand, operand));
3231 else
3232 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3233 operand);
3235 case 2:
3237 struct demangle_component *left;
3238 struct demangle_component *right;
3240 if (op_is_new_cast (op))
3241 left = cplus_demangle_type (di);
3242 else
3243 left = d_expression_1 (di);
3244 if (!strcmp (code, "cl"))
3245 right = d_exprlist (di, 'E');
3246 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3248 right = d_unqualified_name (di);
3249 if (d_peek_char (di) == 'I')
3250 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3251 right, d_template_args (di));
3253 else
3254 right = d_expression_1 (di);
3256 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3257 d_make_comp (di,
3258 DEMANGLE_COMPONENT_BINARY_ARGS,
3259 left, right));
3261 case 3:
3263 struct demangle_component *first;
3264 struct demangle_component *second;
3265 struct demangle_component *third;
3267 if (!strcmp (code, "qu"))
3269 /* ?: expression. */
3270 first = d_expression_1 (di);
3271 second = d_expression_1 (di);
3272 third = d_expression_1 (di);
3274 else if (code[0] == 'n')
3276 /* new-expression. */
3277 if (code[1] != 'w' && code[1] != 'a')
3278 return NULL;
3279 first = d_exprlist (di, '_');
3280 second = cplus_demangle_type (di);
3281 if (d_peek_char (di) == 'E')
3283 d_advance (di, 1);
3284 third = NULL;
3286 else if (d_peek_char (di) == 'p'
3287 && d_peek_next_char (di) == 'i')
3289 /* Parenthesized initializer. */
3290 d_advance (di, 2);
3291 third = d_exprlist (di, 'E');
3293 else if (d_peek_char (di) == 'i'
3294 && d_peek_next_char (di) == 'l')
3295 /* initializer-list. */
3296 third = d_expression_1 (di);
3297 else
3298 return NULL;
3300 else
3301 return NULL;
3302 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3303 d_make_comp (di,
3304 DEMANGLE_COMPONENT_TRINARY_ARG1,
3305 first,
3306 d_make_comp (di,
3307 DEMANGLE_COMPONENT_TRINARY_ARG2,
3308 second, third)));
3310 default:
3311 return NULL;
3316 static struct demangle_component *
3317 d_expression (struct d_info *di)
3319 struct demangle_component *ret;
3320 int was_expression = di->is_expression;
3322 di->is_expression = 1;
3323 ret = d_expression_1 (di);
3324 di->is_expression = was_expression;
3325 return ret;
3328 /* <expr-primary> ::= L <type> <(value) number> E
3329 ::= L <type> <(value) float> E
3330 ::= L <mangled-name> E
3333 static struct demangle_component *
3334 d_expr_primary (struct d_info *di)
3336 struct demangle_component *ret;
3338 if (! d_check_char (di, 'L'))
3339 return NULL;
3340 if (d_peek_char (di) == '_'
3341 /* Workaround for G++ bug; see comment in write_template_arg. */
3342 || d_peek_char (di) == 'Z')
3343 ret = cplus_demangle_mangled_name (di, 0);
3344 else
3346 struct demangle_component *type;
3347 enum demangle_component_type t;
3348 const char *s;
3350 type = cplus_demangle_type (di);
3351 if (type == NULL)
3352 return NULL;
3354 /* If we have a type we know how to print, we aren't going to
3355 print the type name itself. */
3356 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3357 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3358 di->expansion -= type->u.s_builtin.type->len;
3360 /* Rather than try to interpret the literal value, we just
3361 collect it as a string. Note that it's possible to have a
3362 floating point literal here. The ABI specifies that the
3363 format of such literals is machine independent. That's fine,
3364 but what's not fine is that versions of g++ up to 3.2 with
3365 -fabi-version=1 used upper case letters in the hex constant,
3366 and dumped out gcc's internal representation. That makes it
3367 hard to tell where the constant ends, and hard to dump the
3368 constant in any readable form anyhow. We don't attempt to
3369 handle these cases. */
3371 t = DEMANGLE_COMPONENT_LITERAL;
3372 if (d_peek_char (di) == 'n')
3374 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3375 d_advance (di, 1);
3377 s = d_str (di);
3378 while (d_peek_char (di) != 'E')
3380 if (d_peek_char (di) == '\0')
3381 return NULL;
3382 d_advance (di, 1);
3384 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3386 if (! d_check_char (di, 'E'))
3387 return NULL;
3388 return ret;
3391 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3392 ::= Z <(function) encoding> E s [<discriminator>]
3393 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3396 static struct demangle_component *
3397 d_local_name (struct d_info *di)
3399 struct demangle_component *function;
3401 if (! d_check_char (di, 'Z'))
3402 return NULL;
3404 function = d_encoding (di, 0);
3406 if (! d_check_char (di, 'E'))
3407 return NULL;
3409 if (d_peek_char (di) == 's')
3411 d_advance (di, 1);
3412 if (! d_discriminator (di))
3413 return NULL;
3414 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3415 d_make_name (di, "string literal",
3416 sizeof "string literal" - 1));
3418 else
3420 struct demangle_component *name;
3421 int num = -1;
3423 if (d_peek_char (di) == 'd')
3425 /* Default argument scope: d <number> _. */
3426 d_advance (di, 1);
3427 num = d_compact_number (di);
3428 if (num < 0)
3429 return NULL;
3432 name = d_name (di);
3433 if (name)
3434 switch (name->type)
3436 /* Lambdas and unnamed types have internal discriminators. */
3437 case DEMANGLE_COMPONENT_LAMBDA:
3438 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3439 break;
3440 default:
3441 if (! d_discriminator (di))
3442 return NULL;
3444 if (num >= 0)
3445 name = d_make_default_arg (di, num, name);
3446 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3450 /* <discriminator> ::= _ <(non-negative) number>
3452 We demangle the discriminator, but we don't print it out. FIXME:
3453 We should print it out in verbose mode. */
3455 static int
3456 d_discriminator (struct d_info *di)
3458 long discrim;
3460 if (d_peek_char (di) != '_')
3461 return 1;
3462 d_advance (di, 1);
3463 discrim = d_number (di);
3464 if (discrim < 0)
3465 return 0;
3466 return 1;
3469 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3471 static struct demangle_component *
3472 d_lambda (struct d_info *di)
3474 struct demangle_component *tl;
3475 struct demangle_component *ret;
3476 int num;
3478 if (! d_check_char (di, 'U'))
3479 return NULL;
3480 if (! d_check_char (di, 'l'))
3481 return NULL;
3483 tl = d_parmlist (di);
3484 if (tl == NULL)
3485 return NULL;
3487 if (! d_check_char (di, 'E'))
3488 return NULL;
3490 num = d_compact_number (di);
3491 if (num < 0)
3492 return NULL;
3494 ret = d_make_empty (di);
3495 if (ret)
3497 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3498 ret->u.s_unary_num.sub = tl;
3499 ret->u.s_unary_num.num = num;
3502 if (! d_add_substitution (di, ret))
3503 return NULL;
3505 return ret;
3508 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3510 static struct demangle_component *
3511 d_unnamed_type (struct d_info *di)
3513 struct demangle_component *ret;
3514 long num;
3516 if (! d_check_char (di, 'U'))
3517 return NULL;
3518 if (! d_check_char (di, 't'))
3519 return NULL;
3521 num = d_compact_number (di);
3522 if (num < 0)
3523 return NULL;
3525 ret = d_make_empty (di);
3526 if (ret)
3528 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3529 ret->u.s_number.number = num;
3532 if (! d_add_substitution (di, ret))
3533 return NULL;
3535 return ret;
3538 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3541 static struct demangle_component *
3542 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3544 const char *suffix = d_str (di);
3545 const char *pend = suffix;
3546 struct demangle_component *n;
3548 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3550 pend += 2;
3551 while (IS_LOWER (*pend) || *pend == '_')
3552 ++pend;
3554 while (*pend == '.' && IS_DIGIT (pend[1]))
3556 pend += 2;
3557 while (IS_DIGIT (*pend))
3558 ++pend;
3560 d_advance (di, pend - suffix);
3561 n = d_make_name (di, suffix, pend - suffix);
3562 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3565 /* Add a new substitution. */
3567 static int
3568 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3570 if (dc == NULL)
3571 return 0;
3572 if (di->next_sub >= di->num_subs)
3573 return 0;
3574 di->subs[di->next_sub] = dc;
3575 ++di->next_sub;
3576 return 1;
3579 /* <substitution> ::= S <seq-id> _
3580 ::= S_
3581 ::= St
3582 ::= Sa
3583 ::= Sb
3584 ::= Ss
3585 ::= Si
3586 ::= So
3587 ::= Sd
3589 If PREFIX is non-zero, then this type is being used as a prefix in
3590 a qualified name. In this case, for the standard substitutions, we
3591 need to check whether we are being used as a prefix for a
3592 constructor or destructor, and return a full template name.
3593 Otherwise we will get something like std::iostream::~iostream()
3594 which does not correspond particularly well to any function which
3595 actually appears in the source.
3598 static const struct d_standard_sub_info standard_subs[] =
3600 { 't', NL ("std"),
3601 NL ("std"),
3602 NULL, 0 },
3603 { 'a', NL ("std::allocator"),
3604 NL ("std::allocator"),
3605 NL ("allocator") },
3606 { 'b', NL ("std::basic_string"),
3607 NL ("std::basic_string"),
3608 NL ("basic_string") },
3609 { 's', NL ("std::string"),
3610 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3611 NL ("basic_string") },
3612 { 'i', NL ("std::istream"),
3613 NL ("std::basic_istream<char, std::char_traits<char> >"),
3614 NL ("basic_istream") },
3615 { 'o', NL ("std::ostream"),
3616 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3617 NL ("basic_ostream") },
3618 { 'd', NL ("std::iostream"),
3619 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3620 NL ("basic_iostream") }
3623 static struct demangle_component *
3624 d_substitution (struct d_info *di, int prefix)
3626 char c;
3628 if (! d_check_char (di, 'S'))
3629 return NULL;
3631 c = d_next_char (di);
3632 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3634 unsigned int id;
3636 id = 0;
3637 if (c != '_')
3641 unsigned int new_id;
3643 if (IS_DIGIT (c))
3644 new_id = id * 36 + c - '0';
3645 else if (IS_UPPER (c))
3646 new_id = id * 36 + c - 'A' + 10;
3647 else
3648 return NULL;
3649 if (new_id < id)
3650 return NULL;
3651 id = new_id;
3652 c = d_next_char (di);
3654 while (c != '_');
3656 ++id;
3659 if (id >= (unsigned int) di->next_sub)
3660 return NULL;
3662 ++di->did_subs;
3664 return di->subs[id];
3666 else
3668 int verbose;
3669 const struct d_standard_sub_info *p;
3670 const struct d_standard_sub_info *pend;
3672 verbose = (di->options & DMGL_VERBOSE) != 0;
3673 if (! verbose && prefix)
3675 char peek;
3677 peek = d_peek_char (di);
3678 if (peek == 'C' || peek == 'D')
3679 verbose = 1;
3682 pend = (&standard_subs[0]
3683 + sizeof standard_subs / sizeof standard_subs[0]);
3684 for (p = &standard_subs[0]; p < pend; ++p)
3686 if (c == p->code)
3688 const char *s;
3689 int len;
3691 if (p->set_last_name != NULL)
3692 di->last_name = d_make_sub (di, p->set_last_name,
3693 p->set_last_name_len);
3694 if (verbose)
3696 s = p->full_expansion;
3697 len = p->full_len;
3699 else
3701 s = p->simple_expansion;
3702 len = p->simple_len;
3704 di->expansion += len;
3705 return d_make_sub (di, s, len);
3709 return NULL;
3713 static void
3714 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3716 checkpoint->n = di->n;
3717 checkpoint->next_comp = di->next_comp;
3718 checkpoint->next_sub = di->next_sub;
3719 checkpoint->did_subs = di->did_subs;
3720 checkpoint->expansion = di->expansion;
3723 static void
3724 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3726 di->n = checkpoint->n;
3727 di->next_comp = checkpoint->next_comp;
3728 di->next_sub = checkpoint->next_sub;
3729 di->did_subs = checkpoint->did_subs;
3730 di->expansion = checkpoint->expansion;
3733 /* Initialize a growable string. */
3735 static void
3736 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3738 dgs->buf = NULL;
3739 dgs->len = 0;
3740 dgs->alc = 0;
3741 dgs->allocation_failure = 0;
3743 if (estimate > 0)
3744 d_growable_string_resize (dgs, estimate);
3747 /* Grow a growable string to a given size. */
3749 static inline void
3750 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3752 size_t newalc;
3753 char *newbuf;
3755 if (dgs->allocation_failure)
3756 return;
3758 /* Start allocation at two bytes to avoid any possibility of confusion
3759 with the special value of 1 used as a return in *palc to indicate
3760 allocation failures. */
3761 newalc = dgs->alc > 0 ? dgs->alc : 2;
3762 while (newalc < need)
3763 newalc <<= 1;
3765 newbuf = (char *) realloc (dgs->buf, newalc);
3766 if (newbuf == NULL)
3768 free (dgs->buf);
3769 dgs->buf = NULL;
3770 dgs->len = 0;
3771 dgs->alc = 0;
3772 dgs->allocation_failure = 1;
3773 return;
3775 dgs->buf = newbuf;
3776 dgs->alc = newalc;
3779 /* Append a buffer to a growable string. */
3781 static inline void
3782 d_growable_string_append_buffer (struct d_growable_string *dgs,
3783 const char *s, size_t l)
3785 size_t need;
3787 need = dgs->len + l + 1;
3788 if (need > dgs->alc)
3789 d_growable_string_resize (dgs, need);
3791 if (dgs->allocation_failure)
3792 return;
3794 memcpy (dgs->buf + dgs->len, s, l);
3795 dgs->buf[dgs->len + l] = '\0';
3796 dgs->len += l;
3799 /* Bridge growable strings to the callback mechanism. */
3801 static void
3802 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3804 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3806 d_growable_string_append_buffer (dgs, s, l);
3809 /* Walk the tree, counting the number of templates encountered, and
3810 the number of times a scope might be saved. These counts will be
3811 used to allocate data structures for d_print_comp, so the logic
3812 here must mirror the logic d_print_comp will use. It is not
3813 important that the resulting numbers are exact, so long as they
3814 are larger than the actual numbers encountered. */
3816 static void
3817 d_count_templates_scopes (int *num_templates, int *num_scopes,
3818 const struct demangle_component *dc)
3820 if (dc == NULL)
3821 return;
3823 switch (dc->type)
3825 case DEMANGLE_COMPONENT_NAME:
3826 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3827 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3828 case DEMANGLE_COMPONENT_SUB_STD:
3829 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3830 case DEMANGLE_COMPONENT_OPERATOR:
3831 case DEMANGLE_COMPONENT_CHARACTER:
3832 case DEMANGLE_COMPONENT_NUMBER:
3833 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3834 break;
3836 case DEMANGLE_COMPONENT_TEMPLATE:
3837 (*num_templates)++;
3838 goto recurse_left_right;
3840 case DEMANGLE_COMPONENT_REFERENCE:
3841 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3842 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
3843 (*num_scopes)++;
3844 goto recurse_left_right;
3846 case DEMANGLE_COMPONENT_QUAL_NAME:
3847 case DEMANGLE_COMPONENT_LOCAL_NAME:
3848 case DEMANGLE_COMPONENT_TYPED_NAME:
3849 case DEMANGLE_COMPONENT_VTABLE:
3850 case DEMANGLE_COMPONENT_VTT:
3851 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3852 case DEMANGLE_COMPONENT_TYPEINFO:
3853 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3854 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3855 case DEMANGLE_COMPONENT_THUNK:
3856 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3857 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3858 case DEMANGLE_COMPONENT_JAVA_CLASS:
3859 case DEMANGLE_COMPONENT_GUARD:
3860 case DEMANGLE_COMPONENT_TLS_INIT:
3861 case DEMANGLE_COMPONENT_TLS_WRAPPER:
3862 case DEMANGLE_COMPONENT_REFTEMP:
3863 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3864 case DEMANGLE_COMPONENT_RESTRICT:
3865 case DEMANGLE_COMPONENT_VOLATILE:
3866 case DEMANGLE_COMPONENT_CONST:
3867 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3868 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3869 case DEMANGLE_COMPONENT_CONST_THIS:
3870 case DEMANGLE_COMPONENT_REFERENCE_THIS:
3871 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
3872 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3873 case DEMANGLE_COMPONENT_POINTER:
3874 case DEMANGLE_COMPONENT_COMPLEX:
3875 case DEMANGLE_COMPONENT_IMAGINARY:
3876 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3877 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3878 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3879 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3880 case DEMANGLE_COMPONENT_VECTOR_TYPE:
3881 case DEMANGLE_COMPONENT_ARGLIST:
3882 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3883 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
3884 case DEMANGLE_COMPONENT_CAST:
3885 case DEMANGLE_COMPONENT_NULLARY:
3886 case DEMANGLE_COMPONENT_UNARY:
3887 case DEMANGLE_COMPONENT_BINARY:
3888 case DEMANGLE_COMPONENT_BINARY_ARGS:
3889 case DEMANGLE_COMPONENT_TRINARY:
3890 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3891 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3892 case DEMANGLE_COMPONENT_LITERAL:
3893 case DEMANGLE_COMPONENT_LITERAL_NEG:
3894 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3895 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3896 case DEMANGLE_COMPONENT_DECLTYPE:
3897 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
3898 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
3899 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3900 case DEMANGLE_COMPONENT_TAGGED_NAME:
3901 case DEMANGLE_COMPONENT_CLONE:
3902 recurse_left_right:
3903 d_count_templates_scopes (num_templates, num_scopes,
3904 d_left (dc));
3905 d_count_templates_scopes (num_templates, num_scopes,
3906 d_right (dc));
3907 break;
3909 case DEMANGLE_COMPONENT_CTOR:
3910 d_count_templates_scopes (num_templates, num_scopes,
3911 dc->u.s_ctor.name);
3912 break;
3914 case DEMANGLE_COMPONENT_DTOR:
3915 d_count_templates_scopes (num_templates, num_scopes,
3916 dc->u.s_dtor.name);
3917 break;
3919 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3920 d_count_templates_scopes (num_templates, num_scopes,
3921 dc->u.s_extended_operator.name);
3922 break;
3924 case DEMANGLE_COMPONENT_FIXED_TYPE:
3925 d_count_templates_scopes (num_templates, num_scopes,
3926 dc->u.s_fixed.length);
3927 break;
3929 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
3930 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
3931 d_count_templates_scopes (num_templates, num_scopes,
3932 d_left (dc));
3933 break;
3935 case DEMANGLE_COMPONENT_LAMBDA:
3936 case DEMANGLE_COMPONENT_DEFAULT_ARG:
3937 d_count_templates_scopes (num_templates, num_scopes,
3938 dc->u.s_unary_num.sub);
3939 break;
3943 /* Initialize a print information structure. */
3945 static void
3946 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3947 void *opaque, const struct demangle_component *dc)
3949 dpi->len = 0;
3950 dpi->last_char = '\0';
3951 dpi->templates = NULL;
3952 dpi->modifiers = NULL;
3953 dpi->pack_index = 0;
3954 dpi->flush_count = 0;
3956 dpi->callback = callback;
3957 dpi->opaque = opaque;
3959 dpi->demangle_failure = 0;
3961 dpi->component_stack = NULL;
3963 dpi->saved_scopes = NULL;
3964 dpi->next_saved_scope = 0;
3965 dpi->num_saved_scopes = 0;
3967 dpi->copy_templates = NULL;
3968 dpi->next_copy_template = 0;
3969 dpi->num_copy_templates = 0;
3971 d_count_templates_scopes (&dpi->num_copy_templates,
3972 &dpi->num_saved_scopes, dc);
3973 dpi->num_copy_templates *= dpi->num_saved_scopes;
3975 dpi->current_template = NULL;
3978 /* Indicate that an error occurred during printing, and test for error. */
3980 static inline void
3981 d_print_error (struct d_print_info *dpi)
3983 dpi->demangle_failure = 1;
3986 static inline int
3987 d_print_saw_error (struct d_print_info *dpi)
3989 return dpi->demangle_failure != 0;
3992 /* Flush buffered characters to the callback. */
3994 static inline void
3995 d_print_flush (struct d_print_info *dpi)
3997 dpi->buf[dpi->len] = '\0';
3998 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3999 dpi->len = 0;
4000 dpi->flush_count++;
4003 /* Append characters and buffers for printing. */
4005 static inline void
4006 d_append_char (struct d_print_info *dpi, char c)
4008 if (dpi->len == sizeof (dpi->buf) - 1)
4009 d_print_flush (dpi);
4011 dpi->buf[dpi->len++] = c;
4012 dpi->last_char = c;
4015 static inline void
4016 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4018 size_t i;
4020 for (i = 0; i < l; i++)
4021 d_append_char (dpi, s[i]);
4024 static inline void
4025 d_append_string (struct d_print_info *dpi, const char *s)
4027 d_append_buffer (dpi, s, strlen (s));
4030 static inline void
4031 d_append_num (struct d_print_info *dpi, long l)
4033 char buf[25];
4034 sprintf (buf,"%ld", l);
4035 d_append_string (dpi, buf);
4038 static inline char
4039 d_last_char (struct d_print_info *dpi)
4041 return dpi->last_char;
4044 /* Turn components into a human readable string. OPTIONS is the
4045 options bits passed to the demangler. DC is the tree to print.
4046 CALLBACK is a function to call to flush demangled string segments
4047 as they fill the intermediate buffer, and OPAQUE is a generalized
4048 callback argument. On success, this returns 1. On failure,
4049 it returns 0, indicating a bad parse. It does not use heap
4050 memory to build an output string, so cannot encounter memory
4051 allocation failure. */
4053 CP_STATIC_IF_GLIBCPP_V3
4055 cplus_demangle_print_callback (int options,
4056 const struct demangle_component *dc,
4057 demangle_callbackref callback, void *opaque)
4059 struct d_print_info dpi;
4061 d_print_init (&dpi, callback, opaque, dc);
4064 #ifdef CP_DYNAMIC_ARRAYS
4065 __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
4066 __extension__ struct d_print_template temps[dpi.num_copy_templates];
4068 dpi.saved_scopes = scopes;
4069 dpi.copy_templates = temps;
4070 #else
4071 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4072 * sizeof (*dpi.saved_scopes));
4073 dpi.copy_templates = alloca (dpi.num_copy_templates
4074 * sizeof (*dpi.copy_templates));
4075 #endif
4077 d_print_comp (&dpi, options, dc);
4080 d_print_flush (&dpi);
4082 return ! d_print_saw_error (&dpi);
4085 /* Turn components into a human readable string. OPTIONS is the
4086 options bits passed to the demangler. DC is the tree to print.
4087 ESTIMATE is a guess at the length of the result. This returns a
4088 string allocated by malloc, or NULL on error. On success, this
4089 sets *PALC to the size of the allocated buffer. On failure, this
4090 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4091 failure. */
4093 CP_STATIC_IF_GLIBCPP_V3
4094 char *
4095 cplus_demangle_print (int options, const struct demangle_component *dc,
4096 int estimate, size_t *palc)
4098 struct d_growable_string dgs;
4100 d_growable_string_init (&dgs, estimate);
4102 if (! cplus_demangle_print_callback (options, dc,
4103 d_growable_string_callback_adapter,
4104 &dgs))
4106 free (dgs.buf);
4107 *palc = 0;
4108 return NULL;
4111 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4112 return dgs.buf;
4115 /* Returns the I'th element of the template arglist ARGS, or NULL on
4116 failure. */
4118 static struct demangle_component *
4119 d_index_template_argument (struct demangle_component *args, int i)
4121 struct demangle_component *a;
4123 for (a = args;
4124 a != NULL;
4125 a = d_right (a))
4127 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4128 return NULL;
4129 if (i <= 0)
4130 break;
4131 --i;
4133 if (i != 0 || a == NULL)
4134 return NULL;
4136 return d_left (a);
4139 /* Returns the template argument from the current context indicated by DC,
4140 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4142 static struct demangle_component *
4143 d_lookup_template_argument (struct d_print_info *dpi,
4144 const struct demangle_component *dc)
4146 if (dpi->templates == NULL)
4148 d_print_error (dpi);
4149 return NULL;
4152 return d_index_template_argument
4153 (d_right (dpi->templates->template_decl),
4154 dc->u.s_number.number);
4157 /* Returns a template argument pack used in DC (any will do), or NULL. */
4159 static struct demangle_component *
4160 d_find_pack (struct d_print_info *dpi,
4161 const struct demangle_component *dc)
4163 struct demangle_component *a;
4164 if (dc == NULL)
4165 return NULL;
4167 switch (dc->type)
4169 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4170 a = d_lookup_template_argument (dpi, dc);
4171 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4172 return a;
4173 return NULL;
4175 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4176 return NULL;
4178 case DEMANGLE_COMPONENT_LAMBDA:
4179 case DEMANGLE_COMPONENT_NAME:
4180 case DEMANGLE_COMPONENT_TAGGED_NAME:
4181 case DEMANGLE_COMPONENT_OPERATOR:
4182 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4183 case DEMANGLE_COMPONENT_SUB_STD:
4184 case DEMANGLE_COMPONENT_CHARACTER:
4185 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4186 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4187 return NULL;
4189 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4190 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4191 case DEMANGLE_COMPONENT_CTOR:
4192 return d_find_pack (dpi, dc->u.s_ctor.name);
4193 case DEMANGLE_COMPONENT_DTOR:
4194 return d_find_pack (dpi, dc->u.s_dtor.name);
4196 default:
4197 a = d_find_pack (dpi, d_left (dc));
4198 if (a)
4199 return a;
4200 return d_find_pack (dpi, d_right (dc));
4204 /* Returns the length of the template argument pack DC. */
4206 static int
4207 d_pack_length (const struct demangle_component *dc)
4209 int count = 0;
4210 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4211 && d_left (dc) != NULL)
4213 ++count;
4214 dc = d_right (dc);
4216 return count;
4219 /* DC is a component of a mangled expression. Print it, wrapped in parens
4220 if needed. */
4222 static void
4223 d_print_subexpr (struct d_print_info *dpi, int options,
4224 const struct demangle_component *dc)
4226 int simple = 0;
4227 if (dc->type == DEMANGLE_COMPONENT_NAME
4228 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4229 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4230 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4231 simple = 1;
4232 if (!simple)
4233 d_append_char (dpi, '(');
4234 d_print_comp (dpi, options, dc);
4235 if (!simple)
4236 d_append_char (dpi, ')');
4239 /* Save the current scope. */
4241 static void
4242 d_save_scope (struct d_print_info *dpi,
4243 const struct demangle_component *container)
4245 struct d_saved_scope *scope;
4246 struct d_print_template *src, **link;
4248 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4250 d_print_error (dpi);
4251 return;
4253 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4254 dpi->next_saved_scope++;
4256 scope->container = container;
4257 link = &scope->templates;
4259 for (src = dpi->templates; src != NULL; src = src->next)
4261 struct d_print_template *dst;
4263 if (dpi->next_copy_template >= dpi->num_copy_templates)
4265 d_print_error (dpi);
4266 return;
4268 dst = &dpi->copy_templates[dpi->next_copy_template];
4269 dpi->next_copy_template++;
4271 dst->template_decl = src->template_decl;
4272 *link = dst;
4273 link = &dst->next;
4276 *link = NULL;
4279 /* Attempt to locate a previously saved scope. Returns NULL if no
4280 corresponding saved scope was found. */
4282 static struct d_saved_scope *
4283 d_get_saved_scope (struct d_print_info *dpi,
4284 const struct demangle_component *container)
4286 int i;
4288 for (i = 0; i < dpi->next_saved_scope; i++)
4289 if (dpi->saved_scopes[i].container == container)
4290 return &dpi->saved_scopes[i];
4292 return NULL;
4295 /* Subroutine to handle components. */
4297 static void
4298 d_print_comp_inner (struct d_print_info *dpi, int options,
4299 const struct demangle_component *dc)
4301 /* Magic variable to let reference smashing skip over the next modifier
4302 without needing to modify *dc. */
4303 const struct demangle_component *mod_inner = NULL;
4305 /* Variable used to store the current templates while a previously
4306 captured scope is used. */
4307 struct d_print_template *saved_templates;
4309 /* Nonzero if templates have been stored in the above variable. */
4310 int need_template_restore = 0;
4312 if (dc == NULL)
4314 d_print_error (dpi);
4315 return;
4317 if (d_print_saw_error (dpi))
4318 return;
4320 switch (dc->type)
4322 case DEMANGLE_COMPONENT_NAME:
4323 if ((options & DMGL_JAVA) == 0)
4324 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4325 else
4326 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4327 return;
4329 case DEMANGLE_COMPONENT_TAGGED_NAME:
4330 d_print_comp (dpi, options, d_left (dc));
4331 d_append_string (dpi, "[abi:");
4332 d_print_comp (dpi, options, d_right (dc));
4333 d_append_char (dpi, ']');
4334 return;
4336 case DEMANGLE_COMPONENT_QUAL_NAME:
4337 case DEMANGLE_COMPONENT_LOCAL_NAME:
4338 d_print_comp (dpi, options, d_left (dc));
4339 if ((options & DMGL_JAVA) == 0)
4340 d_append_string (dpi, "::");
4341 else
4342 d_append_char (dpi, '.');
4344 struct demangle_component *local_name = d_right (dc);
4345 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4347 d_append_string (dpi, "{default arg#");
4348 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4349 d_append_string (dpi, "}::");
4350 local_name = local_name->u.s_unary_num.sub;
4352 d_print_comp (dpi, options, local_name);
4354 return;
4356 case DEMANGLE_COMPONENT_TYPED_NAME:
4358 struct d_print_mod *hold_modifiers;
4359 struct demangle_component *typed_name;
4360 struct d_print_mod adpm[4];
4361 unsigned int i;
4362 struct d_print_template dpt;
4364 /* Pass the name down to the type so that it can be printed in
4365 the right place for the type. We also have to pass down
4366 any CV-qualifiers, which apply to the this parameter. */
4367 hold_modifiers = dpi->modifiers;
4368 dpi->modifiers = 0;
4369 i = 0;
4370 typed_name = d_left (dc);
4371 while (typed_name != NULL)
4373 if (i >= sizeof adpm / sizeof adpm[0])
4375 d_print_error (dpi);
4376 return;
4379 adpm[i].next = dpi->modifiers;
4380 dpi->modifiers = &adpm[i];
4381 adpm[i].mod = typed_name;
4382 adpm[i].printed = 0;
4383 adpm[i].templates = dpi->templates;
4384 ++i;
4386 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4387 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4388 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4389 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4390 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4391 break;
4393 typed_name = d_left (typed_name);
4396 if (typed_name == NULL)
4398 d_print_error (dpi);
4399 return;
4402 /* If typed_name is a template, then it applies to the
4403 function type as well. */
4404 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4406 dpt.next = dpi->templates;
4407 dpi->templates = &dpt;
4408 dpt.template_decl = typed_name;
4411 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4412 there may be CV-qualifiers on its right argument which
4413 really apply here; this happens when parsing a class which
4414 is local to a function. */
4415 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4417 struct demangle_component *local_name;
4419 local_name = d_right (typed_name);
4420 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4421 local_name = local_name->u.s_unary_num.sub;
4422 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4423 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4424 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4425 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4426 || (local_name->type
4427 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4429 if (i >= sizeof adpm / sizeof adpm[0])
4431 d_print_error (dpi);
4432 return;
4435 adpm[i] = adpm[i - 1];
4436 adpm[i].next = &adpm[i - 1];
4437 dpi->modifiers = &adpm[i];
4439 adpm[i - 1].mod = local_name;
4440 adpm[i - 1].printed = 0;
4441 adpm[i - 1].templates = dpi->templates;
4442 ++i;
4444 local_name = d_left (local_name);
4448 d_print_comp (dpi, options, d_right (dc));
4450 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4451 dpi->templates = dpt.next;
4453 /* If the modifiers didn't get printed by the type, print them
4454 now. */
4455 while (i > 0)
4457 --i;
4458 if (! adpm[i].printed)
4460 d_append_char (dpi, ' ');
4461 d_print_mod (dpi, options, adpm[i].mod);
4465 dpi->modifiers = hold_modifiers;
4467 return;
4470 case DEMANGLE_COMPONENT_TEMPLATE:
4472 struct d_print_mod *hold_dpm;
4473 struct demangle_component *dcl;
4474 const struct demangle_component *hold_current;
4476 /* This template may need to be referenced by a cast operator
4477 contained in its subtree. */
4478 hold_current = dpi->current_template;
4479 dpi->current_template = dc;
4481 /* Don't push modifiers into a template definition. Doing so
4482 could give the wrong definition for a template argument.
4483 Instead, treat the template essentially as a name. */
4485 hold_dpm = dpi->modifiers;
4486 dpi->modifiers = NULL;
4488 dcl = d_left (dc);
4490 if ((options & DMGL_JAVA) != 0
4491 && dcl->type == DEMANGLE_COMPONENT_NAME
4492 && dcl->u.s_name.len == 6
4493 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4495 /* Special-case Java arrays, so that JArray<TYPE> appears
4496 instead as TYPE[]. */
4498 d_print_comp (dpi, options, d_right (dc));
4499 d_append_string (dpi, "[]");
4501 else
4503 d_print_comp (dpi, options, dcl);
4504 if (d_last_char (dpi) == '<')
4505 d_append_char (dpi, ' ');
4506 d_append_char (dpi, '<');
4507 d_print_comp (dpi, options, d_right (dc));
4508 /* Avoid generating two consecutive '>' characters, to avoid
4509 the C++ syntactic ambiguity. */
4510 if (d_last_char (dpi) == '>')
4511 d_append_char (dpi, ' ');
4512 d_append_char (dpi, '>');
4515 dpi->modifiers = hold_dpm;
4516 dpi->current_template = hold_current;
4518 return;
4521 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4523 struct d_print_template *hold_dpt;
4524 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4526 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4527 a = d_index_template_argument (a, dpi->pack_index);
4529 if (a == NULL)
4531 d_print_error (dpi);
4532 return;
4535 /* While processing this parameter, we need to pop the list of
4536 templates. This is because the template parameter may
4537 itself be a reference to a parameter of an outer
4538 template. */
4540 hold_dpt = dpi->templates;
4541 dpi->templates = hold_dpt->next;
4543 d_print_comp (dpi, options, a);
4545 dpi->templates = hold_dpt;
4547 return;
4550 case DEMANGLE_COMPONENT_CTOR:
4551 d_print_comp (dpi, options, dc->u.s_ctor.name);
4552 return;
4554 case DEMANGLE_COMPONENT_DTOR:
4555 d_append_char (dpi, '~');
4556 d_print_comp (dpi, options, dc->u.s_dtor.name);
4557 return;
4559 case DEMANGLE_COMPONENT_VTABLE:
4560 d_append_string (dpi, "vtable for ");
4561 d_print_comp (dpi, options, d_left (dc));
4562 return;
4564 case DEMANGLE_COMPONENT_VTT:
4565 d_append_string (dpi, "VTT for ");
4566 d_print_comp (dpi, options, d_left (dc));
4567 return;
4569 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4570 d_append_string (dpi, "construction vtable for ");
4571 d_print_comp (dpi, options, d_left (dc));
4572 d_append_string (dpi, "-in-");
4573 d_print_comp (dpi, options, d_right (dc));
4574 return;
4576 case DEMANGLE_COMPONENT_TYPEINFO:
4577 d_append_string (dpi, "typeinfo for ");
4578 d_print_comp (dpi, options, d_left (dc));
4579 return;
4581 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4582 d_append_string (dpi, "typeinfo name for ");
4583 d_print_comp (dpi, options, d_left (dc));
4584 return;
4586 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4587 d_append_string (dpi, "typeinfo fn for ");
4588 d_print_comp (dpi, options, d_left (dc));
4589 return;
4591 case DEMANGLE_COMPONENT_THUNK:
4592 d_append_string (dpi, "non-virtual thunk to ");
4593 d_print_comp (dpi, options, d_left (dc));
4594 return;
4596 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4597 d_append_string (dpi, "virtual thunk to ");
4598 d_print_comp (dpi, options, d_left (dc));
4599 return;
4601 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4602 d_append_string (dpi, "covariant return thunk to ");
4603 d_print_comp (dpi, options, d_left (dc));
4604 return;
4606 case DEMANGLE_COMPONENT_JAVA_CLASS:
4607 d_append_string (dpi, "java Class for ");
4608 d_print_comp (dpi, options, d_left (dc));
4609 return;
4611 case DEMANGLE_COMPONENT_GUARD:
4612 d_append_string (dpi, "guard variable for ");
4613 d_print_comp (dpi, options, d_left (dc));
4614 return;
4616 case DEMANGLE_COMPONENT_TLS_INIT:
4617 d_append_string (dpi, "TLS init function for ");
4618 d_print_comp (dpi, options, d_left (dc));
4619 return;
4621 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4622 d_append_string (dpi, "TLS wrapper function for ");
4623 d_print_comp (dpi, options, d_left (dc));
4624 return;
4626 case DEMANGLE_COMPONENT_REFTEMP:
4627 d_append_string (dpi, "reference temporary #");
4628 d_print_comp (dpi, options, d_right (dc));
4629 d_append_string (dpi, " for ");
4630 d_print_comp (dpi, options, d_left (dc));
4631 return;
4633 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4634 d_append_string (dpi, "hidden alias for ");
4635 d_print_comp (dpi, options, d_left (dc));
4636 return;
4638 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4639 d_append_string (dpi, "transaction clone for ");
4640 d_print_comp (dpi, options, d_left (dc));
4641 return;
4643 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4644 d_append_string (dpi, "non-transaction clone for ");
4645 d_print_comp (dpi, options, d_left (dc));
4646 return;
4648 case DEMANGLE_COMPONENT_SUB_STD:
4649 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4650 return;
4652 case DEMANGLE_COMPONENT_RESTRICT:
4653 case DEMANGLE_COMPONENT_VOLATILE:
4654 case DEMANGLE_COMPONENT_CONST:
4656 struct d_print_mod *pdpm;
4658 /* When printing arrays, it's possible to have cases where the
4659 same CV-qualifier gets pushed on the stack multiple times.
4660 We only need to print it once. */
4662 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4664 if (! pdpm->printed)
4666 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4667 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4668 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4669 break;
4670 if (pdpm->mod->type == dc->type)
4672 d_print_comp (dpi, options, d_left (dc));
4673 return;
4678 goto modifier;
4680 case DEMANGLE_COMPONENT_REFERENCE:
4681 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4683 /* Handle reference smashing: & + && = &. */
4684 const struct demangle_component *sub = d_left (dc);
4685 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4687 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4688 struct demangle_component *a;
4690 if (scope == NULL)
4692 /* This is the first time SUB has been traversed.
4693 We need to capture the current templates so
4694 they can be restored if SUB is reentered as a
4695 substitution. */
4696 d_save_scope (dpi, sub);
4697 if (d_print_saw_error (dpi))
4698 return;
4700 else
4702 const struct d_component_stack *dcse;
4703 int found_self_or_parent = 0;
4705 /* This traversal is reentering SUB as a substition.
4706 If we are not beneath SUB or DC in the tree then we
4707 need to restore SUB's template stack temporarily. */
4708 for (dcse = dpi->component_stack; dcse != NULL;
4709 dcse = dcse->parent)
4711 if (dcse->dc == sub
4712 || (dcse->dc == dc
4713 && dcse != dpi->component_stack))
4715 found_self_or_parent = 1;
4716 break;
4720 if (!found_self_or_parent)
4722 saved_templates = dpi->templates;
4723 dpi->templates = scope->templates;
4724 need_template_restore = 1;
4728 a = d_lookup_template_argument (dpi, sub);
4729 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4730 a = d_index_template_argument (a, dpi->pack_index);
4732 if (a == NULL)
4734 if (need_template_restore)
4735 dpi->templates = saved_templates;
4737 d_print_error (dpi);
4738 return;
4741 sub = a;
4744 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4745 || sub->type == dc->type)
4746 dc = sub;
4747 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4748 mod_inner = d_left (sub);
4750 /* Fall through. */
4752 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4753 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4754 case DEMANGLE_COMPONENT_CONST_THIS:
4755 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4756 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4757 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4758 case DEMANGLE_COMPONENT_POINTER:
4759 case DEMANGLE_COMPONENT_COMPLEX:
4760 case DEMANGLE_COMPONENT_IMAGINARY:
4761 modifier:
4763 /* We keep a list of modifiers on the stack. */
4764 struct d_print_mod dpm;
4766 dpm.next = dpi->modifiers;
4767 dpi->modifiers = &dpm;
4768 dpm.mod = dc;
4769 dpm.printed = 0;
4770 dpm.templates = dpi->templates;
4772 if (!mod_inner)
4773 mod_inner = d_left (dc);
4775 d_print_comp (dpi, options, mod_inner);
4777 /* If the modifier didn't get printed by the type, print it
4778 now. */
4779 if (! dpm.printed)
4780 d_print_mod (dpi, options, dc);
4782 dpi->modifiers = dpm.next;
4784 if (need_template_restore)
4785 dpi->templates = saved_templates;
4787 return;
4790 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4791 if ((options & DMGL_JAVA) == 0)
4792 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4793 dc->u.s_builtin.type->len);
4794 else
4795 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4796 dc->u.s_builtin.type->java_len);
4797 return;
4799 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4800 d_print_comp (dpi, options, d_left (dc));
4801 return;
4803 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4805 if ((options & DMGL_RET_POSTFIX) != 0)
4806 d_print_function_type (dpi,
4807 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4808 dc, dpi->modifiers);
4810 /* Print return type if present */
4811 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4812 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4813 d_left (dc));
4814 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4816 struct d_print_mod dpm;
4818 /* We must pass this type down as a modifier in order to
4819 print it in the right location. */
4820 dpm.next = dpi->modifiers;
4821 dpi->modifiers = &dpm;
4822 dpm.mod = dc;
4823 dpm.printed = 0;
4824 dpm.templates = dpi->templates;
4826 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4827 d_left (dc));
4829 dpi->modifiers = dpm.next;
4831 if (dpm.printed)
4832 return;
4834 /* In standard prefix notation, there is a space between the
4835 return type and the function signature. */
4836 if ((options & DMGL_RET_POSTFIX) == 0)
4837 d_append_char (dpi, ' ');
4840 if ((options & DMGL_RET_POSTFIX) == 0)
4841 d_print_function_type (dpi,
4842 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4843 dc, dpi->modifiers);
4845 return;
4848 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4850 struct d_print_mod *hold_modifiers;
4851 struct d_print_mod adpm[4];
4852 unsigned int i;
4853 struct d_print_mod *pdpm;
4855 /* We must pass this type down as a modifier in order to print
4856 multi-dimensional arrays correctly. If the array itself is
4857 CV-qualified, we act as though the element type were
4858 CV-qualified. We do this by copying the modifiers down
4859 rather than fiddling pointers, so that we don't wind up
4860 with a d_print_mod higher on the stack pointing into our
4861 stack frame after we return. */
4863 hold_modifiers = dpi->modifiers;
4865 adpm[0].next = hold_modifiers;
4866 dpi->modifiers = &adpm[0];
4867 adpm[0].mod = dc;
4868 adpm[0].printed = 0;
4869 adpm[0].templates = dpi->templates;
4871 i = 1;
4872 pdpm = hold_modifiers;
4873 while (pdpm != NULL
4874 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4875 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4876 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4878 if (! pdpm->printed)
4880 if (i >= sizeof adpm / sizeof adpm[0])
4882 d_print_error (dpi);
4883 return;
4886 adpm[i] = *pdpm;
4887 adpm[i].next = dpi->modifiers;
4888 dpi->modifiers = &adpm[i];
4889 pdpm->printed = 1;
4890 ++i;
4893 pdpm = pdpm->next;
4896 d_print_comp (dpi, options, d_right (dc));
4898 dpi->modifiers = hold_modifiers;
4900 if (adpm[0].printed)
4901 return;
4903 while (i > 1)
4905 --i;
4906 d_print_mod (dpi, options, adpm[i].mod);
4909 d_print_array_type (dpi, options, dc, dpi->modifiers);
4911 return;
4914 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4915 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4917 struct d_print_mod dpm;
4919 dpm.next = dpi->modifiers;
4920 dpi->modifiers = &dpm;
4921 dpm.mod = dc;
4922 dpm.printed = 0;
4923 dpm.templates = dpi->templates;
4925 d_print_comp (dpi, options, d_right (dc));
4927 /* If the modifier didn't get printed by the type, print it
4928 now. */
4929 if (! dpm.printed)
4930 d_print_mod (dpi, options, dc);
4932 dpi->modifiers = dpm.next;
4934 return;
4937 case DEMANGLE_COMPONENT_FIXED_TYPE:
4938 if (dc->u.s_fixed.sat)
4939 d_append_string (dpi, "_Sat ");
4940 /* Don't print "int _Accum". */
4941 if (dc->u.s_fixed.length->u.s_builtin.type
4942 != &cplus_demangle_builtin_types['i'-'a'])
4944 d_print_comp (dpi, options, dc->u.s_fixed.length);
4945 d_append_char (dpi, ' ');
4947 if (dc->u.s_fixed.accum)
4948 d_append_string (dpi, "_Accum");
4949 else
4950 d_append_string (dpi, "_Fract");
4951 return;
4953 case DEMANGLE_COMPONENT_ARGLIST:
4954 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4955 if (d_left (dc) != NULL)
4956 d_print_comp (dpi, options, d_left (dc));
4957 if (d_right (dc) != NULL)
4959 size_t len;
4960 unsigned long int flush_count;
4961 /* Make sure ", " isn't flushed by d_append_string, otherwise
4962 dpi->len -= 2 wouldn't work. */
4963 if (dpi->len >= sizeof (dpi->buf) - 2)
4964 d_print_flush (dpi);
4965 d_append_string (dpi, ", ");
4966 len = dpi->len;
4967 flush_count = dpi->flush_count;
4968 d_print_comp (dpi, options, d_right (dc));
4969 /* If that didn't print anything (which can happen with empty
4970 template argument packs), remove the comma and space. */
4971 if (dpi->flush_count == flush_count && dpi->len == len)
4972 dpi->len -= 2;
4974 return;
4976 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4978 struct demangle_component *type = d_left (dc);
4979 struct demangle_component *list = d_right (dc);
4981 if (type)
4982 d_print_comp (dpi, options, type);
4983 d_append_char (dpi, '{');
4984 d_print_comp (dpi, options, list);
4985 d_append_char (dpi, '}');
4987 return;
4989 case DEMANGLE_COMPONENT_OPERATOR:
4991 const struct demangle_operator_info *op = dc->u.s_operator.op;
4992 int len = op->len;
4994 d_append_string (dpi, "operator");
4995 /* Add a space before new/delete. */
4996 if (IS_LOWER (op->name[0]))
4997 d_append_char (dpi, ' ');
4998 /* Omit a trailing space. */
4999 if (op->name[len-1] == ' ')
5000 --len;
5001 d_append_buffer (dpi, op->name, len);
5002 return;
5005 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5006 d_append_string (dpi, "operator ");
5007 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5008 return;
5010 case DEMANGLE_COMPONENT_CAST:
5011 d_append_string (dpi, "operator ");
5012 d_print_cast (dpi, options, dc);
5013 return;
5015 case DEMANGLE_COMPONENT_NULLARY:
5016 d_print_expr_op (dpi, options, d_left (dc));
5017 return;
5019 case DEMANGLE_COMPONENT_UNARY:
5021 struct demangle_component *op = d_left (dc);
5022 struct demangle_component *operand = d_right (dc);
5023 const char *code = NULL;
5025 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5027 code = op->u.s_operator.op->code;
5028 if (!strcmp (code, "ad"))
5030 /* Don't print the argument list for the address of a
5031 function. */
5032 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5033 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5034 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5035 operand = d_left (operand);
5037 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5039 /* This indicates a suffix operator. */
5040 operand = d_left (operand);
5041 d_print_subexpr (dpi, options, operand);
5042 d_print_expr_op (dpi, options, op);
5043 return;
5047 if (op->type != DEMANGLE_COMPONENT_CAST)
5048 d_print_expr_op (dpi, options, op);
5049 else
5051 d_append_char (dpi, '(');
5052 d_print_cast (dpi, options, op);
5053 d_append_char (dpi, ')');
5055 if (code && !strcmp (code, "gs"))
5056 /* Avoid parens after '::'. */
5057 d_print_comp (dpi, options, operand);
5058 else if (code && !strcmp (code, "st"))
5059 /* Always print parens for sizeof (type). */
5061 d_append_char (dpi, '(');
5062 d_print_comp (dpi, options, operand);
5063 d_append_char (dpi, ')');
5065 else
5066 d_print_subexpr (dpi, options, operand);
5068 return;
5070 case DEMANGLE_COMPONENT_BINARY:
5071 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5073 d_print_error (dpi);
5074 return;
5077 if (op_is_new_cast (d_left (dc)))
5079 d_print_expr_op (dpi, options, d_left (dc));
5080 d_append_char (dpi, '<');
5081 d_print_comp (dpi, options, d_left (d_right (dc)));
5082 d_append_string (dpi, ">(");
5083 d_print_comp (dpi, options, d_right (d_right (dc)));
5084 d_append_char (dpi, ')');
5085 return;
5088 /* We wrap an expression which uses the greater-than operator in
5089 an extra layer of parens so that it does not get confused
5090 with the '>' which ends the template parameters. */
5091 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5092 && d_left (dc)->u.s_operator.op->len == 1
5093 && d_left (dc)->u.s_operator.op->name[0] == '>')
5094 d_append_char (dpi, '(');
5096 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5097 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5099 /* Function call used in an expression should not have printed types
5100 of the function arguments. Values of the function arguments still
5101 get printed below. */
5103 const struct demangle_component *func = d_left (d_right (dc));
5105 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5106 d_print_error (dpi);
5107 d_print_subexpr (dpi, options, d_left (func));
5109 else
5110 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5111 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5113 d_append_char (dpi, '[');
5114 d_print_comp (dpi, options, d_right (d_right (dc)));
5115 d_append_char (dpi, ']');
5117 else
5119 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5120 d_print_expr_op (dpi, options, d_left (dc));
5121 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5124 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5125 && d_left (dc)->u.s_operator.op->len == 1
5126 && d_left (dc)->u.s_operator.op->name[0] == '>')
5127 d_append_char (dpi, ')');
5129 return;
5131 case DEMANGLE_COMPONENT_BINARY_ARGS:
5132 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5133 d_print_error (dpi);
5134 return;
5136 case DEMANGLE_COMPONENT_TRINARY:
5137 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5138 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5140 d_print_error (dpi);
5141 return;
5144 struct demangle_component *op = d_left (dc);
5145 struct demangle_component *first = d_left (d_right (dc));
5146 struct demangle_component *second = d_left (d_right (d_right (dc)));
5147 struct demangle_component *third = d_right (d_right (d_right (dc)));
5149 if (!strcmp (op->u.s_operator.op->code, "qu"))
5151 d_print_subexpr (dpi, options, first);
5152 d_print_expr_op (dpi, options, op);
5153 d_print_subexpr (dpi, options, second);
5154 d_append_string (dpi, " : ");
5155 d_print_subexpr (dpi, options, third);
5157 else
5159 d_append_string (dpi, "new ");
5160 if (d_left (first) != NULL)
5162 d_print_subexpr (dpi, options, first);
5163 d_append_char (dpi, ' ');
5165 d_print_comp (dpi, options, second);
5166 if (third)
5167 d_print_subexpr (dpi, options, third);
5170 return;
5172 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5173 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5174 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5175 d_print_error (dpi);
5176 return;
5178 case DEMANGLE_COMPONENT_LITERAL:
5179 case DEMANGLE_COMPONENT_LITERAL_NEG:
5181 enum d_builtin_type_print tp;
5183 /* For some builtin types, produce simpler output. */
5184 tp = D_PRINT_DEFAULT;
5185 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5187 tp = d_left (dc)->u.s_builtin.type->print;
5188 switch (tp)
5190 case D_PRINT_INT:
5191 case D_PRINT_UNSIGNED:
5192 case D_PRINT_LONG:
5193 case D_PRINT_UNSIGNED_LONG:
5194 case D_PRINT_LONG_LONG:
5195 case D_PRINT_UNSIGNED_LONG_LONG:
5196 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5198 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5199 d_append_char (dpi, '-');
5200 d_print_comp (dpi, options, d_right (dc));
5201 switch (tp)
5203 default:
5204 break;
5205 case D_PRINT_UNSIGNED:
5206 d_append_char (dpi, 'u');
5207 break;
5208 case D_PRINT_LONG:
5209 d_append_char (dpi, 'l');
5210 break;
5211 case D_PRINT_UNSIGNED_LONG:
5212 d_append_string (dpi, "ul");
5213 break;
5214 case D_PRINT_LONG_LONG:
5215 d_append_string (dpi, "ll");
5216 break;
5217 case D_PRINT_UNSIGNED_LONG_LONG:
5218 d_append_string (dpi, "ull");
5219 break;
5221 return;
5223 break;
5225 case D_PRINT_BOOL:
5226 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5227 && d_right (dc)->u.s_name.len == 1
5228 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5230 switch (d_right (dc)->u.s_name.s[0])
5232 case '0':
5233 d_append_string (dpi, "false");
5234 return;
5235 case '1':
5236 d_append_string (dpi, "true");
5237 return;
5238 default:
5239 break;
5242 break;
5244 default:
5245 break;
5249 d_append_char (dpi, '(');
5250 d_print_comp (dpi, options, d_left (dc));
5251 d_append_char (dpi, ')');
5252 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5253 d_append_char (dpi, '-');
5254 if (tp == D_PRINT_FLOAT)
5255 d_append_char (dpi, '[');
5256 d_print_comp (dpi, options, d_right (dc));
5257 if (tp == D_PRINT_FLOAT)
5258 d_append_char (dpi, ']');
5260 return;
5262 case DEMANGLE_COMPONENT_NUMBER:
5263 d_append_num (dpi, dc->u.s_number.number);
5264 return;
5266 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5267 d_append_string (dpi, "java resource ");
5268 d_print_comp (dpi, options, d_left (dc));
5269 return;
5271 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5272 d_print_comp (dpi, options, d_left (dc));
5273 d_print_comp (dpi, options, d_right (dc));
5274 return;
5276 case DEMANGLE_COMPONENT_CHARACTER:
5277 d_append_char (dpi, dc->u.s_character.character);
5278 return;
5280 case DEMANGLE_COMPONENT_DECLTYPE:
5281 d_append_string (dpi, "decltype (");
5282 d_print_comp (dpi, options, d_left (dc));
5283 d_append_char (dpi, ')');
5284 return;
5286 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5288 int len;
5289 int i;
5290 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5291 if (a == NULL)
5293 /* d_find_pack won't find anything if the only packs involved
5294 in this expansion are function parameter packs; in that
5295 case, just print the pattern and "...". */
5296 d_print_subexpr (dpi, options, d_left (dc));
5297 d_append_string (dpi, "...");
5298 return;
5301 len = d_pack_length (a);
5302 dc = d_left (dc);
5303 for (i = 0; i < len; ++i)
5305 dpi->pack_index = i;
5306 d_print_comp (dpi, options, dc);
5307 if (i < len-1)
5308 d_append_string (dpi, ", ");
5311 return;
5313 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5315 long num = dc->u.s_number.number;
5316 if (num == 0)
5317 d_append_string (dpi, "this");
5318 else
5320 d_append_string (dpi, "{parm#");
5321 d_append_num (dpi, num);
5322 d_append_char (dpi, '}');
5325 return;
5327 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5328 d_append_string (dpi, "global constructors keyed to ");
5329 d_print_comp (dpi, options, dc->u.s_binary.left);
5330 return;
5332 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5333 d_append_string (dpi, "global destructors keyed to ");
5334 d_print_comp (dpi, options, dc->u.s_binary.left);
5335 return;
5337 case DEMANGLE_COMPONENT_LAMBDA:
5338 d_append_string (dpi, "{lambda(");
5339 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5340 d_append_string (dpi, ")#");
5341 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5342 d_append_char (dpi, '}');
5343 return;
5345 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5346 d_append_string (dpi, "{unnamed type#");
5347 d_append_num (dpi, dc->u.s_number.number + 1);
5348 d_append_char (dpi, '}');
5349 return;
5351 case DEMANGLE_COMPONENT_CLONE:
5352 d_print_comp (dpi, options, d_left (dc));
5353 d_append_string (dpi, " [clone ");
5354 d_print_comp (dpi, options, d_right (dc));
5355 d_append_char (dpi, ']');
5356 return;
5358 default:
5359 d_print_error (dpi);
5360 return;
5364 static void
5365 d_print_comp (struct d_print_info *dpi, int options,
5366 const struct demangle_component *dc)
5368 struct d_component_stack self;
5370 self.dc = dc;
5371 self.parent = dpi->component_stack;
5372 dpi->component_stack = &self;
5374 d_print_comp_inner (dpi, options, dc);
5376 dpi->component_stack = self.parent;
5379 /* Print a Java dentifier. For Java we try to handle encoded extended
5380 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5381 so we don't it for C++. Characters are encoded as
5382 __U<hex-char>+_. */
5384 static void
5385 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5387 const char *p;
5388 const char *end;
5390 end = name + len;
5391 for (p = name; p < end; ++p)
5393 if (end - p > 3
5394 && p[0] == '_'
5395 && p[1] == '_'
5396 && p[2] == 'U')
5398 unsigned long c;
5399 const char *q;
5401 c = 0;
5402 for (q = p + 3; q < end; ++q)
5404 int dig;
5406 if (IS_DIGIT (*q))
5407 dig = *q - '0';
5408 else if (*q >= 'A' && *q <= 'F')
5409 dig = *q - 'A' + 10;
5410 else if (*q >= 'a' && *q <= 'f')
5411 dig = *q - 'a' + 10;
5412 else
5413 break;
5415 c = c * 16 + dig;
5417 /* If the Unicode character is larger than 256, we don't try
5418 to deal with it here. FIXME. */
5419 if (q < end && *q == '_' && c < 256)
5421 d_append_char (dpi, c);
5422 p = q;
5423 continue;
5427 d_append_char (dpi, *p);
5431 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5432 qualifiers on this after printing a function. */
5434 static void
5435 d_print_mod_list (struct d_print_info *dpi, int options,
5436 struct d_print_mod *mods, int suffix)
5438 struct d_print_template *hold_dpt;
5440 if (mods == NULL || d_print_saw_error (dpi))
5441 return;
5443 if (mods->printed
5444 || (! suffix
5445 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5446 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5447 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5448 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5449 || (mods->mod->type
5450 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5452 d_print_mod_list (dpi, options, mods->next, suffix);
5453 return;
5456 mods->printed = 1;
5458 hold_dpt = dpi->templates;
5459 dpi->templates = mods->templates;
5461 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5463 d_print_function_type (dpi, options, mods->mod, mods->next);
5464 dpi->templates = hold_dpt;
5465 return;
5467 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5469 d_print_array_type (dpi, options, mods->mod, mods->next);
5470 dpi->templates = hold_dpt;
5471 return;
5473 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5475 struct d_print_mod *hold_modifiers;
5476 struct demangle_component *dc;
5478 /* When this is on the modifier stack, we have pulled any
5479 qualifiers off the right argument already. Otherwise, we
5480 print it as usual, but don't let the left argument see any
5481 modifiers. */
5483 hold_modifiers = dpi->modifiers;
5484 dpi->modifiers = NULL;
5485 d_print_comp (dpi, options, d_left (mods->mod));
5486 dpi->modifiers = hold_modifiers;
5488 if ((options & DMGL_JAVA) == 0)
5489 d_append_string (dpi, "::");
5490 else
5491 d_append_char (dpi, '.');
5493 dc = d_right (mods->mod);
5495 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5497 d_append_string (dpi, "{default arg#");
5498 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5499 d_append_string (dpi, "}::");
5500 dc = dc->u.s_unary_num.sub;
5503 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5504 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5505 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5506 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5507 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5508 dc = d_left (dc);
5510 d_print_comp (dpi, options, dc);
5512 dpi->templates = hold_dpt;
5513 return;
5516 d_print_mod (dpi, options, mods->mod);
5518 dpi->templates = hold_dpt;
5520 d_print_mod_list (dpi, options, mods->next, suffix);
5523 /* Print a modifier. */
5525 static void
5526 d_print_mod (struct d_print_info *dpi, int options,
5527 const struct demangle_component *mod)
5529 switch (mod->type)
5531 case DEMANGLE_COMPONENT_RESTRICT:
5532 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5533 d_append_string (dpi, " restrict");
5534 return;
5535 case DEMANGLE_COMPONENT_VOLATILE:
5536 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5537 d_append_string (dpi, " volatile");
5538 return;
5539 case DEMANGLE_COMPONENT_CONST:
5540 case DEMANGLE_COMPONENT_CONST_THIS:
5541 d_append_string (dpi, " const");
5542 return;
5543 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5544 d_append_char (dpi, ' ');
5545 d_print_comp (dpi, options, d_right (mod));
5546 return;
5547 case DEMANGLE_COMPONENT_POINTER:
5548 /* There is no pointer symbol in Java. */
5549 if ((options & DMGL_JAVA) == 0)
5550 d_append_char (dpi, '*');
5551 return;
5552 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5553 /* For the ref-qualifier, put a space before the &. */
5554 d_append_char (dpi, ' ');
5555 case DEMANGLE_COMPONENT_REFERENCE:
5556 d_append_char (dpi, '&');
5557 return;
5558 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5559 d_append_char (dpi, ' ');
5560 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5561 d_append_string (dpi, "&&");
5562 return;
5563 case DEMANGLE_COMPONENT_COMPLEX:
5564 d_append_string (dpi, "complex ");
5565 return;
5566 case DEMANGLE_COMPONENT_IMAGINARY:
5567 d_append_string (dpi, "imaginary ");
5568 return;
5569 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5570 if (d_last_char (dpi) != '(')
5571 d_append_char (dpi, ' ');
5572 d_print_comp (dpi, options, d_left (mod));
5573 d_append_string (dpi, "::*");
5574 return;
5575 case DEMANGLE_COMPONENT_TYPED_NAME:
5576 d_print_comp (dpi, options, d_left (mod));
5577 return;
5578 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5579 d_append_string (dpi, " __vector(");
5580 d_print_comp (dpi, options, d_left (mod));
5581 d_append_char (dpi, ')');
5582 return;
5584 default:
5585 /* Otherwise, we have something that won't go back on the
5586 modifier stack, so we can just print it. */
5587 d_print_comp (dpi, options, mod);
5588 return;
5592 /* Print a function type, except for the return type. */
5594 static void
5595 d_print_function_type (struct d_print_info *dpi, int options,
5596 const struct demangle_component *dc,
5597 struct d_print_mod *mods)
5599 int need_paren;
5600 int need_space;
5601 struct d_print_mod *p;
5602 struct d_print_mod *hold_modifiers;
5604 need_paren = 0;
5605 need_space = 0;
5606 for (p = mods; p != NULL; p = p->next)
5608 if (p->printed)
5609 break;
5611 switch (p->mod->type)
5613 case DEMANGLE_COMPONENT_POINTER:
5614 case DEMANGLE_COMPONENT_REFERENCE:
5615 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5616 need_paren = 1;
5617 break;
5618 case DEMANGLE_COMPONENT_RESTRICT:
5619 case DEMANGLE_COMPONENT_VOLATILE:
5620 case DEMANGLE_COMPONENT_CONST:
5621 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5622 case DEMANGLE_COMPONENT_COMPLEX:
5623 case DEMANGLE_COMPONENT_IMAGINARY:
5624 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5625 need_space = 1;
5626 need_paren = 1;
5627 break;
5628 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5629 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5630 case DEMANGLE_COMPONENT_CONST_THIS:
5631 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5632 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5633 break;
5634 default:
5635 break;
5637 if (need_paren)
5638 break;
5641 if (need_paren)
5643 if (! need_space)
5645 if (d_last_char (dpi) != '('
5646 && d_last_char (dpi) != '*')
5647 need_space = 1;
5649 if (need_space && d_last_char (dpi) != ' ')
5650 d_append_char (dpi, ' ');
5651 d_append_char (dpi, '(');
5654 hold_modifiers = dpi->modifiers;
5655 dpi->modifiers = NULL;
5657 d_print_mod_list (dpi, options, mods, 0);
5659 if (need_paren)
5660 d_append_char (dpi, ')');
5662 d_append_char (dpi, '(');
5664 if (d_right (dc) != NULL)
5665 d_print_comp (dpi, options, d_right (dc));
5667 d_append_char (dpi, ')');
5669 d_print_mod_list (dpi, options, mods, 1);
5671 dpi->modifiers = hold_modifiers;
5674 /* Print an array type, except for the element type. */
5676 static void
5677 d_print_array_type (struct d_print_info *dpi, int options,
5678 const struct demangle_component *dc,
5679 struct d_print_mod *mods)
5681 int need_space;
5683 need_space = 1;
5684 if (mods != NULL)
5686 int need_paren;
5687 struct d_print_mod *p;
5689 need_paren = 0;
5690 for (p = mods; p != NULL; p = p->next)
5692 if (! p->printed)
5694 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5696 need_space = 0;
5697 break;
5699 else
5701 need_paren = 1;
5702 need_space = 1;
5703 break;
5708 if (need_paren)
5709 d_append_string (dpi, " (");
5711 d_print_mod_list (dpi, options, mods, 0);
5713 if (need_paren)
5714 d_append_char (dpi, ')');
5717 if (need_space)
5718 d_append_char (dpi, ' ');
5720 d_append_char (dpi, '[');
5722 if (d_left (dc) != NULL)
5723 d_print_comp (dpi, options, d_left (dc));
5725 d_append_char (dpi, ']');
5728 /* Print an operator in an expression. */
5730 static void
5731 d_print_expr_op (struct d_print_info *dpi, int options,
5732 const struct demangle_component *dc)
5734 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5735 d_append_buffer (dpi, dc->u.s_operator.op->name,
5736 dc->u.s_operator.op->len);
5737 else
5738 d_print_comp (dpi, options, dc);
5741 /* Print a cast. */
5743 static void
5744 d_print_cast (struct d_print_info *dpi, int options,
5745 const struct demangle_component *dc)
5747 struct d_print_template dpt;
5749 /* For a cast operator, we need the template parameters from
5750 the enclosing template in scope for processing the type. */
5751 if (dpi->current_template != NULL)
5753 dpt.next = dpi->templates;
5754 dpi->templates = &dpt;
5755 dpt.template_decl = dpi->current_template;
5758 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5760 d_print_comp (dpi, options, d_left (dc));
5761 if (dpi->current_template != NULL)
5762 dpi->templates = dpt.next;
5764 else
5766 d_print_comp (dpi, options, d_left (d_left (dc)));
5768 /* For a templated cast operator, we need to remove the template
5769 parameters from scope after printing the operator name,
5770 so we need to handle the template printing here. */
5771 if (dpi->current_template != NULL)
5772 dpi->templates = dpt.next;
5774 if (d_last_char (dpi) == '<')
5775 d_append_char (dpi, ' ');
5776 d_append_char (dpi, '<');
5777 d_print_comp (dpi, options, d_right (d_left (dc)));
5778 /* Avoid generating two consecutive '>' characters, to avoid
5779 the C++ syntactic ambiguity. */
5780 if (d_last_char (dpi) == '>')
5781 d_append_char (dpi, ' ');
5782 d_append_char (dpi, '>');
5786 /* Initialize the information structure we use to pass around
5787 information. */
5789 CP_STATIC_IF_GLIBCPP_V3
5790 void
5791 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5792 struct d_info *di)
5794 di->s = mangled;
5795 di->send = mangled + len;
5796 di->options = options;
5798 di->n = mangled;
5800 /* We can not need more components than twice the number of chars in
5801 the mangled string. Most components correspond directly to
5802 chars, but the ARGLIST types are exceptions. */
5803 di->num_comps = 2 * len;
5804 di->next_comp = 0;
5806 /* Similarly, we can not need more substitutions than there are
5807 chars in the mangled string. */
5808 di->num_subs = len;
5809 di->next_sub = 0;
5810 di->did_subs = 0;
5812 di->last_name = NULL;
5814 di->expansion = 0;
5815 di->is_expression = 0;
5816 di->is_conversion = 0;
5819 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5820 mangled name, return strings in repeated callback giving the demangled
5821 name. OPTIONS is the usual libiberty demangler options. On success,
5822 this returns 1. On failure, returns 0. */
5824 static int
5825 d_demangle_callback (const char *mangled, int options,
5826 demangle_callbackref callback, void *opaque)
5828 enum
5830 DCT_TYPE,
5831 DCT_MANGLED,
5832 DCT_GLOBAL_CTORS,
5833 DCT_GLOBAL_DTORS
5835 type;
5836 struct d_info di;
5837 struct demangle_component *dc;
5838 int status;
5840 if (mangled[0] == '_' && mangled[1] == 'Z')
5841 type = DCT_MANGLED;
5842 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5843 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5844 && (mangled[9] == 'D' || mangled[9] == 'I')
5845 && mangled[10] == '_')
5846 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5847 else
5849 if ((options & DMGL_TYPES) == 0)
5850 return 0;
5851 type = DCT_TYPE;
5854 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5857 #ifdef CP_DYNAMIC_ARRAYS
5858 __extension__ struct demangle_component comps[di.num_comps];
5859 __extension__ struct demangle_component *subs[di.num_subs];
5861 di.comps = comps;
5862 di.subs = subs;
5863 #else
5864 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5865 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5866 #endif
5868 switch (type)
5870 case DCT_TYPE:
5871 dc = cplus_demangle_type (&di);
5872 break;
5873 case DCT_MANGLED:
5874 dc = cplus_demangle_mangled_name (&di, 1);
5875 break;
5876 case DCT_GLOBAL_CTORS:
5877 case DCT_GLOBAL_DTORS:
5878 d_advance (&di, 11);
5879 dc = d_make_comp (&di,
5880 (type == DCT_GLOBAL_CTORS
5881 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5882 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5883 d_make_demangle_mangled_name (&di, d_str (&di)),
5884 NULL);
5885 d_advance (&di, strlen (d_str (&di)));
5886 break;
5887 default:
5888 abort (); /* We have listed all the cases. */
5891 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5892 mangled string, then we didn't successfully demangle it. If
5893 DMGL_PARAMS is not set, we didn't look at the trailing
5894 parameters. */
5895 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5896 dc = NULL;
5898 #ifdef CP_DEMANGLE_DEBUG
5899 d_dump (dc, 0);
5900 #endif
5902 status = (dc != NULL)
5903 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5904 : 0;
5907 return status;
5910 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5911 name, return a buffer allocated with malloc holding the demangled
5912 name. OPTIONS is the usual libiberty demangler options. On
5913 success, this sets *PALC to the allocated size of the returned
5914 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5915 a memory allocation failure, and returns NULL. */
5917 static char *
5918 d_demangle (const char *mangled, int options, size_t *palc)
5920 struct d_growable_string dgs;
5921 int status;
5923 d_growable_string_init (&dgs, 0);
5925 status = d_demangle_callback (mangled, options,
5926 d_growable_string_callback_adapter, &dgs);
5927 if (status == 0)
5929 free (dgs.buf);
5930 *palc = 0;
5931 return NULL;
5934 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5935 return dgs.buf;
5938 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5940 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5942 /* ia64 ABI-mandated entry point in the C++ runtime library for
5943 performing demangling. MANGLED_NAME is a NUL-terminated character
5944 string containing the name to be demangled.
5946 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5947 *LENGTH bytes, into which the demangled name is stored. If
5948 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5949 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5950 is placed in a region of memory allocated with malloc.
5952 If LENGTH is non-NULL, the length of the buffer containing the
5953 demangled name, is placed in *LENGTH.
5955 The return value is a pointer to the start of the NUL-terminated
5956 demangled name, or NULL if the demangling fails. The caller is
5957 responsible for deallocating this memory using free.
5959 *STATUS is set to one of the following values:
5960 0: The demangling operation succeeded.
5961 -1: A memory allocation failure occurred.
5962 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5963 -3: One of the arguments is invalid.
5965 The demangling is performed using the C++ ABI mangling rules, with
5966 GNU extensions. */
5968 char *
5969 __cxa_demangle (const char *mangled_name, char *output_buffer,
5970 size_t *length, int *status)
5972 char *demangled;
5973 size_t alc;
5975 if (mangled_name == NULL)
5977 if (status != NULL)
5978 *status = -3;
5979 return NULL;
5982 if (output_buffer != NULL && length == NULL)
5984 if (status != NULL)
5985 *status = -3;
5986 return NULL;
5989 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5991 if (demangled == NULL)
5993 if (status != NULL)
5995 if (alc == 1)
5996 *status = -1;
5997 else
5998 *status = -2;
6000 return NULL;
6003 if (output_buffer == NULL)
6005 if (length != NULL)
6006 *length = alc;
6008 else
6010 if (strlen (demangled) < *length)
6012 strcpy (output_buffer, demangled);
6013 free (demangled);
6014 demangled = output_buffer;
6016 else
6018 free (output_buffer);
6019 *length = alc;
6023 if (status != NULL)
6024 *status = 0;
6026 return demangled;
6029 extern int __gcclibcxx_demangle_callback (const char *,
6030 void (*)
6031 (const char *, size_t, void *),
6032 void *);
6034 /* Alternative, allocationless entry point in the C++ runtime library
6035 for performing demangling. MANGLED_NAME is a NUL-terminated character
6036 string containing the name to be demangled.
6038 CALLBACK is a callback function, called with demangled string
6039 segments as demangling progresses; it is called at least once,
6040 but may be called more than once. OPAQUE is a generalized pointer
6041 used as a callback argument.
6043 The return code is one of the following values, equivalent to
6044 the STATUS values of __cxa_demangle() (excluding -1, since this
6045 function performs no memory allocations):
6046 0: The demangling operation succeeded.
6047 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6048 -3: One of the arguments is invalid.
6050 The demangling is performed using the C++ ABI mangling rules, with
6051 GNU extensions. */
6054 __gcclibcxx_demangle_callback (const char *mangled_name,
6055 void (*callback) (const char *, size_t, void *),
6056 void *opaque)
6058 int status;
6060 if (mangled_name == NULL || callback == NULL)
6061 return -3;
6063 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6064 callback, opaque);
6065 if (status == 0)
6066 return -2;
6068 return 0;
6071 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6073 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6074 mangled name, return a buffer allocated with malloc holding the
6075 demangled name. Otherwise, return NULL. */
6077 char *
6078 cplus_demangle_v3 (const char *mangled, int options)
6080 size_t alc;
6082 return d_demangle (mangled, options, &alc);
6086 cplus_demangle_v3_callback (const char *mangled, int options,
6087 demangle_callbackref callback, void *opaque)
6089 return d_demangle_callback (mangled, options, callback, opaque);
6092 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6093 conventions, but the output formatting is a little different.
6094 This instructs the C++ demangler not to emit pointer characters ("*"), to
6095 use Java's namespace separator symbol ("." instead of "::"), and to output
6096 JArray<TYPE> as TYPE[]. */
6098 char *
6099 java_demangle_v3 (const char *mangled)
6101 size_t alc;
6103 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6107 java_demangle_v3_callback (const char *mangled,
6108 demangle_callbackref callback, void *opaque)
6110 return d_demangle_callback (mangled,
6111 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6112 callback, opaque);
6115 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6117 #ifndef IN_GLIBCPP_V3
6119 /* Demangle a string in order to find out whether it is a constructor
6120 or destructor. Return non-zero on success. Set *CTOR_KIND and
6121 *DTOR_KIND appropriately. */
6123 static int
6124 is_ctor_or_dtor (const char *mangled,
6125 enum gnu_v3_ctor_kinds *ctor_kind,
6126 enum gnu_v3_dtor_kinds *dtor_kind)
6128 struct d_info di;
6129 struct demangle_component *dc;
6130 int ret;
6132 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6133 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6135 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6138 #ifdef CP_DYNAMIC_ARRAYS
6139 __extension__ struct demangle_component comps[di.num_comps];
6140 __extension__ struct demangle_component *subs[di.num_subs];
6142 di.comps = comps;
6143 di.subs = subs;
6144 #else
6145 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6146 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6147 #endif
6149 dc = cplus_demangle_mangled_name (&di, 1);
6151 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6152 to demangle the entire string. */
6154 ret = 0;
6155 while (dc != NULL)
6157 switch (dc->type)
6159 /* These cannot appear on a constructor or destructor. */
6160 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6161 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6162 case DEMANGLE_COMPONENT_CONST_THIS:
6163 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6164 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6165 default:
6166 dc = NULL;
6167 break;
6168 case DEMANGLE_COMPONENT_TYPED_NAME:
6169 case DEMANGLE_COMPONENT_TEMPLATE:
6170 dc = d_left (dc);
6171 break;
6172 case DEMANGLE_COMPONENT_QUAL_NAME:
6173 case DEMANGLE_COMPONENT_LOCAL_NAME:
6174 dc = d_right (dc);
6175 break;
6176 case DEMANGLE_COMPONENT_CTOR:
6177 *ctor_kind = dc->u.s_ctor.kind;
6178 ret = 1;
6179 dc = NULL;
6180 break;
6181 case DEMANGLE_COMPONENT_DTOR:
6182 *dtor_kind = dc->u.s_dtor.kind;
6183 ret = 1;
6184 dc = NULL;
6185 break;
6190 return ret;
6193 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6194 name. A non-zero return indicates the type of constructor. */
6196 enum gnu_v3_ctor_kinds
6197 is_gnu_v3_mangled_ctor (const char *name)
6199 enum gnu_v3_ctor_kinds ctor_kind;
6200 enum gnu_v3_dtor_kinds dtor_kind;
6202 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6203 return (enum gnu_v3_ctor_kinds) 0;
6204 return ctor_kind;
6208 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6209 name. A non-zero return indicates the type of destructor. */
6211 enum gnu_v3_dtor_kinds
6212 is_gnu_v3_mangled_dtor (const char *name)
6214 enum gnu_v3_ctor_kinds ctor_kind;
6215 enum gnu_v3_dtor_kinds dtor_kind;
6217 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6218 return (enum gnu_v3_dtor_kinds) 0;
6219 return dtor_kind;
6222 #endif /* IN_GLIBCPP_V3 */
6224 #ifdef STANDALONE_DEMANGLER
6226 #include "getopt.h"
6227 #include "dyn-string.h"
6229 static void print_usage (FILE* fp, int exit_value);
6231 #define IS_ALPHA(CHAR) \
6232 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6233 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6235 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6236 #define is_mangled_char(CHAR) \
6237 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6238 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6240 /* The name of this program, as invoked. */
6241 const char* program_name;
6243 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6245 static void
6246 print_usage (FILE* fp, int exit_value)
6248 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6249 fprintf (fp, "Options:\n");
6250 fprintf (fp, " -h,--help Display this message.\n");
6251 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6252 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6253 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6255 exit (exit_value);
6258 /* Option specification for getopt_long. */
6259 static const struct option long_options[] =
6261 { "help", no_argument, NULL, 'h' },
6262 { "no-params", no_argument, NULL, 'p' },
6263 { "verbose", no_argument, NULL, 'v' },
6264 { NULL, no_argument, NULL, 0 },
6267 /* Main entry for a demangling filter executable. It will demangle
6268 its command line arguments, if any. If none are provided, it will
6269 filter stdin to stdout, replacing any recognized mangled C++ names
6270 with their demangled equivalents. */
6273 main (int argc, char *argv[])
6275 int i;
6276 int opt_char;
6277 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6279 /* Use the program name of this program, as invoked. */
6280 program_name = argv[0];
6282 /* Parse options. */
6285 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6286 switch (opt_char)
6288 case '?': /* Unrecognized option. */
6289 print_usage (stderr, 1);
6290 break;
6292 case 'h':
6293 print_usage (stdout, 0);
6294 break;
6296 case 'p':
6297 options &= ~ DMGL_PARAMS;
6298 break;
6300 case 'v':
6301 options |= DMGL_VERBOSE;
6302 break;
6305 while (opt_char != -1);
6307 if (optind == argc)
6308 /* No command line arguments were provided. Filter stdin. */
6310 dyn_string_t mangled = dyn_string_new (3);
6311 char *s;
6313 /* Read all of input. */
6314 while (!feof (stdin))
6316 char c;
6318 /* Pile characters into mangled until we hit one that can't
6319 occur in a mangled name. */
6320 c = getchar ();
6321 while (!feof (stdin) && is_mangled_char (c))
6323 dyn_string_append_char (mangled, c);
6324 if (feof (stdin))
6325 break;
6326 c = getchar ();
6329 if (dyn_string_length (mangled) > 0)
6331 #ifdef IN_GLIBCPP_V3
6332 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6333 #else
6334 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6335 #endif
6337 if (s != NULL)
6339 fputs (s, stdout);
6340 free (s);
6342 else
6344 /* It might not have been a mangled name. Print the
6345 original text. */
6346 fputs (dyn_string_buf (mangled), stdout);
6349 dyn_string_clear (mangled);
6352 /* If we haven't hit EOF yet, we've read one character that
6353 can't occur in a mangled name, so print it out. */
6354 if (!feof (stdin))
6355 putchar (c);
6358 dyn_string_delete (mangled);
6360 else
6361 /* Demangle command line arguments. */
6363 /* Loop over command line arguments. */
6364 for (i = optind; i < argc; ++i)
6366 char *s;
6367 #ifdef IN_GLIBCPP_V3
6368 int status;
6369 #endif
6371 /* Attempt to demangle. */
6372 #ifdef IN_GLIBCPP_V3
6373 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6374 #else
6375 s = cplus_demangle_v3 (argv[i], options);
6376 #endif
6378 /* If it worked, print the demangled name. */
6379 if (s != NULL)
6381 printf ("%s\n", s);
6382 free (s);
6384 else
6386 #ifdef IN_GLIBCPP_V3
6387 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6388 #else
6389 fprintf (stderr, "Failed: %s\n", argv[i]);
6390 #endif
6395 return 0;
6398 #endif /* STANDALONE_DEMANGLER */