Remove trailing whitespace from libstdc++ headers
[official-gcc.git] / libiberty / cp-demangle.c
blob09d646994665ec692c78822320e37bca7e9d378e
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
20 executable.)
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
41 name.
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
58 demangle_callbackref
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
64 cplus_demangle_print
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
73 IN_LIBGCC2
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int *status)
77 int __gcclibcxx_demangle_callback (const char *,
78 void (*)
79 (const char *, size_t, void *),
80 void *)
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
84 IN_GLIBCPP_V3
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
89 STANDALONE_DEMANGLER
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
93 CP_DEMANGLE_DEBUG
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
97 CHECK_DEMANGLER
98 If defined, additional sanity checks will be performed. It will
99 cause some slowdown, but will allow to catch out-of-bound access
100 errors earlier. This macro is intended for testing and debugging. */
102 #if defined (_AIX) && !defined (__GNUC__)
103 #pragma alloca
104 #endif
106 #ifdef HAVE_CONFIG_H
107 #include "config.h"
108 #endif
110 #include <stdio.h>
112 #ifdef HAVE_STDLIB_H
113 #include <stdlib.h>
114 #endif
115 #ifdef HAVE_STRING_H
116 #include <string.h>
117 #endif
119 #ifdef HAVE_ALLOCA_H
120 # include <alloca.h>
121 #else
122 # ifndef alloca
123 # ifdef __GNUC__
124 # define alloca __builtin_alloca
125 # else
126 extern char *alloca ();
127 # endif /* __GNUC__ */
128 # endif /* alloca */
129 #endif /* HAVE_ALLOCA_H */
131 #ifdef HAVE_LIMITS_H
132 #include <limits.h>
133 #endif
134 #ifndef INT_MAX
135 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
136 #endif
138 #include "ansidecl.h"
139 #include "libiberty.h"
140 #include "demangle.h"
141 #include "cp-demangle.h"
143 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
144 also rename them via #define to avoid compiler errors when the
145 static definition conflicts with the extern declaration in a header
146 file. */
147 #ifdef IN_GLIBCPP_V3
149 #define CP_STATIC_IF_GLIBCPP_V3 static
151 #define cplus_demangle_fill_name d_fill_name
152 static int d_fill_name (struct demangle_component *, const char *, int);
154 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
155 static int
156 d_fill_extended_operator (struct demangle_component *, int,
157 struct demangle_component *);
159 #define cplus_demangle_fill_ctor d_fill_ctor
160 static int
161 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
162 struct demangle_component *);
164 #define cplus_demangle_fill_dtor d_fill_dtor
165 static int
166 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
167 struct demangle_component *);
169 #define cplus_demangle_mangled_name d_mangled_name
170 static struct demangle_component *d_mangled_name (struct d_info *, int);
172 #define cplus_demangle_type d_type
173 static struct demangle_component *d_type (struct d_info *);
175 #define cplus_demangle_print d_print
176 static char *d_print (int, const struct demangle_component *, int, size_t *);
178 #define cplus_demangle_print_callback d_print_callback
179 static int d_print_callback (int, const struct demangle_component *,
180 demangle_callbackref, void *);
182 #define cplus_demangle_init_info d_init_info
183 static void d_init_info (const char *, int, size_t, struct d_info *);
185 #else /* ! defined(IN_GLIBCPP_V3) */
186 #define CP_STATIC_IF_GLIBCPP_V3
187 #endif /* ! defined(IN_GLIBCPP_V3) */
189 /* See if the compiler supports dynamic arrays. */
191 #ifdef __GNUC__
192 #define CP_DYNAMIC_ARRAYS
193 #else
194 #ifdef __STDC__
195 #ifdef __STDC_VERSION__
196 #if __STDC_VERSION__ >= 199901L
197 #define CP_DYNAMIC_ARRAYS
198 #endif /* __STDC__VERSION >= 199901L */
199 #endif /* defined (__STDC_VERSION__) */
200 #endif /* defined (__STDC__) */
201 #endif /* ! defined (__GNUC__) */
203 /* We avoid pulling in the ctype tables, to prevent pulling in
204 additional unresolved symbols when this code is used in a library.
205 FIXME: Is this really a valid reason? This comes from the original
206 V3 demangler code.
208 As of this writing this file has the following undefined references
209 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
210 strcat, strlen. */
212 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
213 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
214 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
216 /* The prefix prepended by GCC to an identifier represnting the
217 anonymous namespace. */
218 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
219 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
220 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
222 /* Information we keep for the standard substitutions. */
224 struct d_standard_sub_info
226 /* The code for this substitution. */
227 char code;
228 /* The simple string it expands to. */
229 const char *simple_expansion;
230 /* The length of the simple expansion. */
231 int simple_len;
232 /* The results of a full, verbose, expansion. This is used when
233 qualifying a constructor/destructor, or when in verbose mode. */
234 const char *full_expansion;
235 /* The length of the full expansion. */
236 int full_len;
237 /* What to set the last_name field of d_info to; NULL if we should
238 not set it. This is only relevant when qualifying a
239 constructor/destructor. */
240 const char *set_last_name;
241 /* The length of set_last_name. */
242 int set_last_name_len;
245 /* Accessors for subtrees of struct demangle_component. */
247 #define d_left(dc) ((dc)->u.s_binary.left)
248 #define d_right(dc) ((dc)->u.s_binary.right)
250 /* A list of templates. This is used while printing. */
252 struct d_print_template
254 /* Next template on the list. */
255 struct d_print_template *next;
256 /* This template. */
257 const struct demangle_component *template_decl;
260 /* A list of type modifiers. This is used while printing. */
262 struct d_print_mod
264 /* Next modifier on the list. These are in the reverse of the order
265 in which they appeared in the mangled string. */
266 struct d_print_mod *next;
267 /* The modifier. */
268 const struct demangle_component *mod;
269 /* Whether this modifier was printed. */
270 int printed;
271 /* The list of templates which applies to this modifier. */
272 struct d_print_template *templates;
275 /* We use these structures to hold information during printing. */
277 struct d_growable_string
279 /* Buffer holding the result. */
280 char *buf;
281 /* Current length of data in buffer. */
282 size_t len;
283 /* Allocated size of buffer. */
284 size_t alc;
285 /* Set to 1 if we had a memory allocation failure. */
286 int allocation_failure;
289 /* Stack of components, innermost first, used to avoid loops. */
291 struct d_component_stack
293 /* This component. */
294 const struct demangle_component *dc;
295 /* This component's parent. */
296 const struct d_component_stack *parent;
299 /* A demangle component and some scope captured when it was first
300 traversed. */
302 struct d_saved_scope
304 /* The component whose scope this is. */
305 const struct demangle_component *container;
306 /* The list of templates, if any, that was current when this
307 scope was captured. */
308 struct d_print_template *templates;
311 /* Checkpoint structure to allow backtracking. This holds copies
312 of the fields of struct d_info that need to be restored
313 if a trial parse needs to be backtracked over. */
315 struct d_info_checkpoint
317 const char *n;
318 int next_comp;
319 int next_sub;
320 int did_subs;
321 int expansion;
324 enum { D_PRINT_BUFFER_LENGTH = 256 };
325 struct d_print_info
327 /* Fixed-length allocated buffer for demangled data, flushed to the
328 callback with a NUL termination once full. */
329 char buf[D_PRINT_BUFFER_LENGTH];
330 /* Current length of data in buffer. */
331 size_t len;
332 /* The last character printed, saved individually so that it survives
333 any buffer flush. */
334 char last_char;
335 /* Callback function to handle demangled buffer flush. */
336 demangle_callbackref callback;
337 /* Opaque callback argument. */
338 void *opaque;
339 /* The current list of templates, if any. */
340 struct d_print_template *templates;
341 /* The current list of modifiers (e.g., pointer, reference, etc.),
342 if any. */
343 struct d_print_mod *modifiers;
344 /* Set to 1 if we saw a demangling error. */
345 int demangle_failure;
346 /* The current index into any template argument packs we are using
347 for printing. */
348 int pack_index;
349 /* Number of d_print_flush calls so far. */
350 unsigned long int flush_count;
351 /* Stack of components, innermost first, used to avoid loops. */
352 const struct d_component_stack *component_stack;
353 /* Array of saved scopes for evaluating substitutions. */
354 struct d_saved_scope *saved_scopes;
355 /* Index of the next unused saved scope in the above array. */
356 int next_saved_scope;
357 /* Number of saved scopes in the above array. */
358 int num_saved_scopes;
359 /* Array of templates for saving into scopes. */
360 struct d_print_template *copy_templates;
361 /* Index of the next unused copy template in the above array. */
362 int next_copy_template;
363 /* Number of copy templates in the above array. */
364 int num_copy_templates;
365 /* The nearest enclosing template, if any. */
366 const struct demangle_component *current_template;
369 #ifdef CP_DEMANGLE_DEBUG
370 static void d_dump (struct demangle_component *, int);
371 #endif
373 static struct demangle_component *
374 d_make_empty (struct d_info *);
376 static struct demangle_component *
377 d_make_comp (struct d_info *, enum demangle_component_type,
378 struct demangle_component *,
379 struct demangle_component *);
381 static struct demangle_component *
382 d_make_name (struct d_info *, const char *, int);
384 static struct demangle_component *
385 d_make_demangle_mangled_name (struct d_info *, const char *);
387 static struct demangle_component *
388 d_make_builtin_type (struct d_info *,
389 const struct demangle_builtin_type_info *);
391 static struct demangle_component *
392 d_make_operator (struct d_info *,
393 const struct demangle_operator_info *);
395 static struct demangle_component *
396 d_make_extended_operator (struct d_info *, int,
397 struct demangle_component *);
399 static struct demangle_component *
400 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
401 struct demangle_component *);
403 static struct demangle_component *
404 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
405 struct demangle_component *);
407 static struct demangle_component *
408 d_make_template_param (struct d_info *, int);
410 static struct demangle_component *
411 d_make_sub (struct d_info *, const char *, int);
413 static int
414 has_return_type (struct demangle_component *);
416 static int
417 is_ctor_dtor_or_conversion (struct demangle_component *);
419 static struct demangle_component *d_encoding (struct d_info *, int);
421 static struct demangle_component *d_name (struct d_info *);
423 static struct demangle_component *d_nested_name (struct d_info *);
425 static struct demangle_component *d_prefix (struct d_info *);
427 static struct demangle_component *d_unqualified_name (struct d_info *);
429 static struct demangle_component *d_source_name (struct d_info *);
431 static int d_number (struct d_info *);
433 static struct demangle_component *d_identifier (struct d_info *, int);
435 static struct demangle_component *d_operator_name (struct d_info *);
437 static struct demangle_component *d_special_name (struct d_info *);
439 static int d_call_offset (struct d_info *, int);
441 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
443 static struct demangle_component **
444 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
446 static struct demangle_component *
447 d_ref_qualifier (struct d_info *, struct demangle_component *);
449 static struct demangle_component *
450 d_function_type (struct d_info *);
452 static struct demangle_component *
453 d_bare_function_type (struct d_info *, int);
455 static struct demangle_component *
456 d_class_enum_type (struct d_info *);
458 static struct demangle_component *d_array_type (struct d_info *);
460 static struct demangle_component *d_vector_type (struct d_info *);
462 static struct demangle_component *
463 d_pointer_to_member_type (struct d_info *);
465 static struct demangle_component *
466 d_template_param (struct d_info *);
468 static struct demangle_component *d_template_args (struct d_info *);
470 static struct demangle_component *
471 d_template_arg (struct d_info *);
473 static struct demangle_component *d_expression (struct d_info *);
475 static struct demangle_component *d_expr_primary (struct d_info *);
477 static struct demangle_component *d_local_name (struct d_info *);
479 static int d_discriminator (struct d_info *);
481 static struct demangle_component *d_lambda (struct d_info *);
483 static struct demangle_component *d_unnamed_type (struct d_info *);
485 static struct demangle_component *
486 d_clone_suffix (struct d_info *, struct demangle_component *);
488 static int
489 d_add_substitution (struct d_info *, struct demangle_component *);
491 static struct demangle_component *d_substitution (struct d_info *, int);
493 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
495 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
497 static void d_growable_string_init (struct d_growable_string *, size_t);
499 static inline void
500 d_growable_string_resize (struct d_growable_string *, size_t);
502 static inline void
503 d_growable_string_append_buffer (struct d_growable_string *,
504 const char *, size_t);
505 static void
506 d_growable_string_callback_adapter (const char *, size_t, void *);
508 static void
509 d_print_init (struct d_print_info *, demangle_callbackref, void *,
510 const struct demangle_component *);
512 static inline void d_print_error (struct d_print_info *);
514 static inline int d_print_saw_error (struct d_print_info *);
516 static inline void d_print_flush (struct d_print_info *);
518 static inline void d_append_char (struct d_print_info *, char);
520 static inline void d_append_buffer (struct d_print_info *,
521 const char *, size_t);
523 static inline void d_append_string (struct d_print_info *, const char *);
525 static inline char d_last_char (struct d_print_info *);
527 static void
528 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
530 static void
531 d_print_java_identifier (struct d_print_info *, const char *, int);
533 static void
534 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
536 static void
537 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
539 static void
540 d_print_function_type (struct d_print_info *, int,
541 const struct demangle_component *,
542 struct d_print_mod *);
544 static void
545 d_print_array_type (struct d_print_info *, int,
546 const struct demangle_component *,
547 struct d_print_mod *);
549 static void
550 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
552 static void d_print_cast (struct d_print_info *, int,
553 const struct demangle_component *);
554 static void d_print_conversion (struct d_print_info *, int,
555 const struct demangle_component *);
557 static int d_demangle_callback (const char *, int,
558 demangle_callbackref, void *);
559 static char *d_demangle (const char *, int, size_t *);
561 #ifdef CP_DEMANGLE_DEBUG
563 static void
564 d_dump (struct demangle_component *dc, int indent)
566 int i;
568 if (dc == NULL)
570 if (indent == 0)
571 printf ("failed demangling\n");
572 return;
575 for (i = 0; i < indent; ++i)
576 putchar (' ');
578 switch (dc->type)
580 case DEMANGLE_COMPONENT_NAME:
581 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
582 return;
583 case DEMANGLE_COMPONENT_TAGGED_NAME:
584 printf ("tagged name\n");
585 d_dump (dc->u.s_binary.left, indent + 2);
586 d_dump (dc->u.s_binary.right, indent + 2);
587 return;
588 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
589 printf ("template parameter %ld\n", dc->u.s_number.number);
590 return;
591 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
592 printf ("function parameter %ld\n", dc->u.s_number.number);
593 return;
594 case DEMANGLE_COMPONENT_CTOR:
595 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
596 d_dump (dc->u.s_ctor.name, indent + 2);
597 return;
598 case DEMANGLE_COMPONENT_DTOR:
599 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
600 d_dump (dc->u.s_dtor.name, indent + 2);
601 return;
602 case DEMANGLE_COMPONENT_SUB_STD:
603 printf ("standard substitution %s\n", dc->u.s_string.string);
604 return;
605 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
606 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
607 return;
608 case DEMANGLE_COMPONENT_OPERATOR:
609 printf ("operator %s\n", dc->u.s_operator.op->name);
610 return;
611 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
612 printf ("extended operator with %d args\n",
613 dc->u.s_extended_operator.args);
614 d_dump (dc->u.s_extended_operator.name, indent + 2);
615 return;
617 case DEMANGLE_COMPONENT_QUAL_NAME:
618 printf ("qualified name\n");
619 break;
620 case DEMANGLE_COMPONENT_LOCAL_NAME:
621 printf ("local name\n");
622 break;
623 case DEMANGLE_COMPONENT_TYPED_NAME:
624 printf ("typed name\n");
625 break;
626 case DEMANGLE_COMPONENT_TEMPLATE:
627 printf ("template\n");
628 break;
629 case DEMANGLE_COMPONENT_VTABLE:
630 printf ("vtable\n");
631 break;
632 case DEMANGLE_COMPONENT_VTT:
633 printf ("VTT\n");
634 break;
635 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
636 printf ("construction vtable\n");
637 break;
638 case DEMANGLE_COMPONENT_TYPEINFO:
639 printf ("typeinfo\n");
640 break;
641 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
642 printf ("typeinfo name\n");
643 break;
644 case DEMANGLE_COMPONENT_TYPEINFO_FN:
645 printf ("typeinfo function\n");
646 break;
647 case DEMANGLE_COMPONENT_THUNK:
648 printf ("thunk\n");
649 break;
650 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
651 printf ("virtual thunk\n");
652 break;
653 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
654 printf ("covariant thunk\n");
655 break;
656 case DEMANGLE_COMPONENT_JAVA_CLASS:
657 printf ("java class\n");
658 break;
659 case DEMANGLE_COMPONENT_GUARD:
660 printf ("guard\n");
661 break;
662 case DEMANGLE_COMPONENT_REFTEMP:
663 printf ("reference temporary\n");
664 break;
665 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
666 printf ("hidden alias\n");
667 break;
668 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
669 printf ("transaction clone\n");
670 break;
671 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
672 printf ("non-transaction clone\n");
673 break;
674 case DEMANGLE_COMPONENT_RESTRICT:
675 printf ("restrict\n");
676 break;
677 case DEMANGLE_COMPONENT_VOLATILE:
678 printf ("volatile\n");
679 break;
680 case DEMANGLE_COMPONENT_CONST:
681 printf ("const\n");
682 break;
683 case DEMANGLE_COMPONENT_RESTRICT_THIS:
684 printf ("restrict this\n");
685 break;
686 case DEMANGLE_COMPONENT_VOLATILE_THIS:
687 printf ("volatile this\n");
688 break;
689 case DEMANGLE_COMPONENT_CONST_THIS:
690 printf ("const this\n");
691 break;
692 case DEMANGLE_COMPONENT_REFERENCE_THIS:
693 printf ("reference this\n");
694 break;
695 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
696 printf ("rvalue reference this\n");
697 break;
698 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
699 printf ("transaction_safe this\n");
700 break;
701 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
702 printf ("vendor type qualifier\n");
703 break;
704 case DEMANGLE_COMPONENT_POINTER:
705 printf ("pointer\n");
706 break;
707 case DEMANGLE_COMPONENT_REFERENCE:
708 printf ("reference\n");
709 break;
710 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
711 printf ("rvalue reference\n");
712 break;
713 case DEMANGLE_COMPONENT_COMPLEX:
714 printf ("complex\n");
715 break;
716 case DEMANGLE_COMPONENT_IMAGINARY:
717 printf ("imaginary\n");
718 break;
719 case DEMANGLE_COMPONENT_VENDOR_TYPE:
720 printf ("vendor type\n");
721 break;
722 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
723 printf ("function type\n");
724 break;
725 case DEMANGLE_COMPONENT_ARRAY_TYPE:
726 printf ("array type\n");
727 break;
728 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
729 printf ("pointer to member type\n");
730 break;
731 case DEMANGLE_COMPONENT_FIXED_TYPE:
732 printf ("fixed-point type, accum? %d, sat? %d\n",
733 dc->u.s_fixed.accum, dc->u.s_fixed.sat);
734 d_dump (dc->u.s_fixed.length, indent + 2);
735 break;
736 case DEMANGLE_COMPONENT_ARGLIST:
737 printf ("argument list\n");
738 break;
739 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
740 printf ("template argument list\n");
741 break;
742 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
743 printf ("initializer list\n");
744 break;
745 case DEMANGLE_COMPONENT_CAST:
746 printf ("cast\n");
747 break;
748 case DEMANGLE_COMPONENT_CONVERSION:
749 printf ("conversion operator\n");
750 break;
751 case DEMANGLE_COMPONENT_NULLARY:
752 printf ("nullary operator\n");
753 break;
754 case DEMANGLE_COMPONENT_UNARY:
755 printf ("unary operator\n");
756 break;
757 case DEMANGLE_COMPONENT_BINARY:
758 printf ("binary operator\n");
759 break;
760 case DEMANGLE_COMPONENT_BINARY_ARGS:
761 printf ("binary operator arguments\n");
762 break;
763 case DEMANGLE_COMPONENT_TRINARY:
764 printf ("trinary operator\n");
765 break;
766 case DEMANGLE_COMPONENT_TRINARY_ARG1:
767 printf ("trinary operator arguments 1\n");
768 break;
769 case DEMANGLE_COMPONENT_TRINARY_ARG2:
770 printf ("trinary operator arguments 1\n");
771 break;
772 case DEMANGLE_COMPONENT_LITERAL:
773 printf ("literal\n");
774 break;
775 case DEMANGLE_COMPONENT_LITERAL_NEG:
776 printf ("negative literal\n");
777 break;
778 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
779 printf ("java resource\n");
780 break;
781 case DEMANGLE_COMPONENT_COMPOUND_NAME:
782 printf ("compound name\n");
783 break;
784 case DEMANGLE_COMPONENT_CHARACTER:
785 printf ("character '%c'\n", dc->u.s_character.character);
786 return;
787 case DEMANGLE_COMPONENT_NUMBER:
788 printf ("number %ld\n", dc->u.s_number.number);
789 return;
790 case DEMANGLE_COMPONENT_DECLTYPE:
791 printf ("decltype\n");
792 break;
793 case DEMANGLE_COMPONENT_PACK_EXPANSION:
794 printf ("pack expansion\n");
795 break;
796 case DEMANGLE_COMPONENT_TLS_INIT:
797 printf ("tls init function\n");
798 break;
799 case DEMANGLE_COMPONENT_TLS_WRAPPER:
800 printf ("tls wrapper function\n");
801 break;
802 case DEMANGLE_COMPONENT_DEFAULT_ARG:
803 printf ("default argument %d\n", dc->u.s_unary_num.num);
804 d_dump (dc->u.s_unary_num.sub, indent+2);
805 return;
806 case DEMANGLE_COMPONENT_LAMBDA:
807 printf ("lambda %d\n", dc->u.s_unary_num.num);
808 d_dump (dc->u.s_unary_num.sub, indent+2);
809 return;
812 d_dump (d_left (dc), indent + 2);
813 d_dump (d_right (dc), indent + 2);
816 #endif /* CP_DEMANGLE_DEBUG */
818 /* Fill in a DEMANGLE_COMPONENT_NAME. */
820 CP_STATIC_IF_GLIBCPP_V3
822 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
824 if (p == NULL || s == NULL || len == 0)
825 return 0;
826 p->type = DEMANGLE_COMPONENT_NAME;
827 p->u.s_name.s = s;
828 p->u.s_name.len = len;
829 return 1;
832 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
834 CP_STATIC_IF_GLIBCPP_V3
836 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
837 struct demangle_component *name)
839 if (p == NULL || args < 0 || name == NULL)
840 return 0;
841 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
842 p->u.s_extended_operator.args = args;
843 p->u.s_extended_operator.name = name;
844 return 1;
847 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
849 CP_STATIC_IF_GLIBCPP_V3
851 cplus_demangle_fill_ctor (struct demangle_component *p,
852 enum gnu_v3_ctor_kinds kind,
853 struct demangle_component *name)
855 if (p == NULL
856 || name == NULL
857 || (int) kind < gnu_v3_complete_object_ctor
858 || (int) kind > gnu_v3_object_ctor_group)
859 return 0;
860 p->type = DEMANGLE_COMPONENT_CTOR;
861 p->u.s_ctor.kind = kind;
862 p->u.s_ctor.name = name;
863 return 1;
866 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
868 CP_STATIC_IF_GLIBCPP_V3
870 cplus_demangle_fill_dtor (struct demangle_component *p,
871 enum gnu_v3_dtor_kinds kind,
872 struct demangle_component *name)
874 if (p == NULL
875 || name == NULL
876 || (int) kind < gnu_v3_deleting_dtor
877 || (int) kind > gnu_v3_object_dtor_group)
878 return 0;
879 p->type = DEMANGLE_COMPONENT_DTOR;
880 p->u.s_dtor.kind = kind;
881 p->u.s_dtor.name = name;
882 return 1;
885 /* Add a new component. */
887 static struct demangle_component *
888 d_make_empty (struct d_info *di)
890 struct demangle_component *p;
892 if (di->next_comp >= di->num_comps)
893 return NULL;
894 p = &di->comps[di->next_comp];
895 ++di->next_comp;
896 return p;
899 /* Add a new generic component. */
901 static struct demangle_component *
902 d_make_comp (struct d_info *di, enum demangle_component_type type,
903 struct demangle_component *left,
904 struct demangle_component *right)
906 struct demangle_component *p;
908 /* We check for errors here. A typical error would be a NULL return
909 from a subroutine. We catch those here, and return NULL
910 upward. */
911 switch (type)
913 /* These types require two parameters. */
914 case DEMANGLE_COMPONENT_QUAL_NAME:
915 case DEMANGLE_COMPONENT_LOCAL_NAME:
916 case DEMANGLE_COMPONENT_TYPED_NAME:
917 case DEMANGLE_COMPONENT_TAGGED_NAME:
918 case DEMANGLE_COMPONENT_TEMPLATE:
919 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
920 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
921 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
922 case DEMANGLE_COMPONENT_UNARY:
923 case DEMANGLE_COMPONENT_BINARY:
924 case DEMANGLE_COMPONENT_BINARY_ARGS:
925 case DEMANGLE_COMPONENT_TRINARY:
926 case DEMANGLE_COMPONENT_TRINARY_ARG1:
927 case DEMANGLE_COMPONENT_LITERAL:
928 case DEMANGLE_COMPONENT_LITERAL_NEG:
929 case DEMANGLE_COMPONENT_COMPOUND_NAME:
930 case DEMANGLE_COMPONENT_VECTOR_TYPE:
931 case DEMANGLE_COMPONENT_CLONE:
932 if (left == NULL || right == NULL)
933 return NULL;
934 break;
936 /* These types only require one parameter. */
937 case DEMANGLE_COMPONENT_VTABLE:
938 case DEMANGLE_COMPONENT_VTT:
939 case DEMANGLE_COMPONENT_TYPEINFO:
940 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
941 case DEMANGLE_COMPONENT_TYPEINFO_FN:
942 case DEMANGLE_COMPONENT_THUNK:
943 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
944 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
945 case DEMANGLE_COMPONENT_JAVA_CLASS:
946 case DEMANGLE_COMPONENT_GUARD:
947 case DEMANGLE_COMPONENT_TLS_INIT:
948 case DEMANGLE_COMPONENT_TLS_WRAPPER:
949 case DEMANGLE_COMPONENT_REFTEMP:
950 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
951 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
952 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
953 case DEMANGLE_COMPONENT_POINTER:
954 case DEMANGLE_COMPONENT_REFERENCE:
955 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
956 case DEMANGLE_COMPONENT_COMPLEX:
957 case DEMANGLE_COMPONENT_IMAGINARY:
958 case DEMANGLE_COMPONENT_VENDOR_TYPE:
959 case DEMANGLE_COMPONENT_CAST:
960 case DEMANGLE_COMPONENT_CONVERSION:
961 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
962 case DEMANGLE_COMPONENT_DECLTYPE:
963 case DEMANGLE_COMPONENT_PACK_EXPANSION:
964 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
965 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
966 case DEMANGLE_COMPONENT_NULLARY:
967 case DEMANGLE_COMPONENT_TRINARY_ARG2:
968 if (left == NULL)
969 return NULL;
970 break;
972 /* This needs a right parameter, but the left parameter can be
973 empty. */
974 case DEMANGLE_COMPONENT_ARRAY_TYPE:
975 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
976 if (right == NULL)
977 return NULL;
978 break;
980 /* These are allowed to have no parameters--in some cases they
981 will be filled in later. */
982 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
983 case DEMANGLE_COMPONENT_RESTRICT:
984 case DEMANGLE_COMPONENT_VOLATILE:
985 case DEMANGLE_COMPONENT_CONST:
986 case DEMANGLE_COMPONENT_RESTRICT_THIS:
987 case DEMANGLE_COMPONENT_VOLATILE_THIS:
988 case DEMANGLE_COMPONENT_CONST_THIS:
989 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
990 case DEMANGLE_COMPONENT_REFERENCE_THIS:
991 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
992 case DEMANGLE_COMPONENT_ARGLIST:
993 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
994 break;
996 /* Other types should not be seen here. */
997 default:
998 return NULL;
1001 p = d_make_empty (di);
1002 if (p != NULL)
1004 p->type = type;
1005 p->u.s_binary.left = left;
1006 p->u.s_binary.right = right;
1008 return p;
1011 /* Add a new demangle mangled name component. */
1013 static struct demangle_component *
1014 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1016 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1017 return d_make_name (di, s, strlen (s));
1018 d_advance (di, 2);
1019 return d_encoding (di, 0);
1022 /* Add a new name component. */
1024 static struct demangle_component *
1025 d_make_name (struct d_info *di, const char *s, int len)
1027 struct demangle_component *p;
1029 p = d_make_empty (di);
1030 if (! cplus_demangle_fill_name (p, s, len))
1031 return NULL;
1032 return p;
1035 /* Add a new builtin type component. */
1037 static struct demangle_component *
1038 d_make_builtin_type (struct d_info *di,
1039 const struct demangle_builtin_type_info *type)
1041 struct demangle_component *p;
1043 if (type == NULL)
1044 return NULL;
1045 p = d_make_empty (di);
1046 if (p != NULL)
1048 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1049 p->u.s_builtin.type = type;
1051 return p;
1054 /* Add a new operator component. */
1056 static struct demangle_component *
1057 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1059 struct demangle_component *p;
1061 p = d_make_empty (di);
1062 if (p != NULL)
1064 p->type = DEMANGLE_COMPONENT_OPERATOR;
1065 p->u.s_operator.op = op;
1067 return p;
1070 /* Add a new extended operator component. */
1072 static struct demangle_component *
1073 d_make_extended_operator (struct d_info *di, int args,
1074 struct demangle_component *name)
1076 struct demangle_component *p;
1078 p = d_make_empty (di);
1079 if (! cplus_demangle_fill_extended_operator (p, args, name))
1080 return NULL;
1081 return p;
1084 static struct demangle_component *
1085 d_make_default_arg (struct d_info *di, int num,
1086 struct demangle_component *sub)
1088 struct demangle_component *p = d_make_empty (di);
1089 if (p)
1091 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1092 p->u.s_unary_num.num = num;
1093 p->u.s_unary_num.sub = sub;
1095 return p;
1098 /* Add a new constructor component. */
1100 static struct demangle_component *
1101 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1102 struct demangle_component *name)
1104 struct demangle_component *p;
1106 p = d_make_empty (di);
1107 if (! cplus_demangle_fill_ctor (p, kind, name))
1108 return NULL;
1109 return p;
1112 /* Add a new destructor component. */
1114 static struct demangle_component *
1115 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1116 struct demangle_component *name)
1118 struct demangle_component *p;
1120 p = d_make_empty (di);
1121 if (! cplus_demangle_fill_dtor (p, kind, name))
1122 return NULL;
1123 return p;
1126 /* Add a new template parameter. */
1128 static struct demangle_component *
1129 d_make_template_param (struct d_info *di, int i)
1131 struct demangle_component *p;
1133 p = d_make_empty (di);
1134 if (p != NULL)
1136 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1137 p->u.s_number.number = i;
1139 return p;
1142 /* Add a new function parameter. */
1144 static struct demangle_component *
1145 d_make_function_param (struct d_info *di, int i)
1147 struct demangle_component *p;
1149 p = d_make_empty (di);
1150 if (p != NULL)
1152 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1153 p->u.s_number.number = i;
1155 return p;
1158 /* Add a new standard substitution component. */
1160 static struct demangle_component *
1161 d_make_sub (struct d_info *di, const char *name, int len)
1163 struct demangle_component *p;
1165 p = d_make_empty (di);
1166 if (p != NULL)
1168 p->type = DEMANGLE_COMPONENT_SUB_STD;
1169 p->u.s_string.string = name;
1170 p->u.s_string.len = len;
1172 return p;
1175 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1177 TOP_LEVEL is non-zero when called at the top level. */
1179 CP_STATIC_IF_GLIBCPP_V3
1180 struct demangle_component *
1181 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1183 struct demangle_component *p;
1185 if (! d_check_char (di, '_')
1186 /* Allow missing _ if not at toplevel to work around a
1187 bug in G++ abi-version=2 mangling; see the comment in
1188 write_template_arg. */
1189 && top_level)
1190 return NULL;
1191 if (! d_check_char (di, 'Z'))
1192 return NULL;
1193 p = d_encoding (di, top_level);
1195 /* If at top level and parsing parameters, check for a clone
1196 suffix. */
1197 if (top_level && (di->options & DMGL_PARAMS) != 0)
1198 while (d_peek_char (di) == '.'
1199 && (IS_LOWER (d_peek_next_char (di))
1200 || d_peek_next_char (di) == '_'
1201 || IS_DIGIT (d_peek_next_char (di))))
1202 p = d_clone_suffix (di, p);
1204 return p;
1207 /* Return whether a function should have a return type. The argument
1208 is the function name, which may be qualified in various ways. The
1209 rules are that template functions have return types with some
1210 exceptions, function types which are not part of a function name
1211 mangling have return types with some exceptions, and non-template
1212 function names do not have return types. The exceptions are that
1213 constructors, destructors, and conversion operators do not have
1214 return types. */
1216 static int
1217 has_return_type (struct demangle_component *dc)
1219 if (dc == NULL)
1220 return 0;
1221 switch (dc->type)
1223 default:
1224 return 0;
1225 case DEMANGLE_COMPONENT_TEMPLATE:
1226 return ! is_ctor_dtor_or_conversion (d_left (dc));
1227 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1228 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1229 case DEMANGLE_COMPONENT_CONST_THIS:
1230 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1231 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1232 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
1233 return has_return_type (d_left (dc));
1237 /* Return whether a name is a constructor, a destructor, or a
1238 conversion operator. */
1240 static int
1241 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1243 if (dc == NULL)
1244 return 0;
1245 switch (dc->type)
1247 default:
1248 return 0;
1249 case DEMANGLE_COMPONENT_QUAL_NAME:
1250 case DEMANGLE_COMPONENT_LOCAL_NAME:
1251 return is_ctor_dtor_or_conversion (d_right (dc));
1252 case DEMANGLE_COMPONENT_CTOR:
1253 case DEMANGLE_COMPONENT_DTOR:
1254 case DEMANGLE_COMPONENT_CONVERSION:
1255 return 1;
1259 /* <encoding> ::= <(function) name> <bare-function-type>
1260 ::= <(data) name>
1261 ::= <special-name>
1263 TOP_LEVEL is non-zero when called at the top level, in which case
1264 if DMGL_PARAMS is not set we do not demangle the function
1265 parameters. We only set this at the top level, because otherwise
1266 we would not correctly demangle names in local scopes. */
1268 static struct demangle_component *
1269 d_encoding (struct d_info *di, int top_level)
1271 char peek = d_peek_char (di);
1273 if (peek == 'G' || peek == 'T')
1274 return d_special_name (di);
1275 else
1277 struct demangle_component *dc;
1279 dc = d_name (di);
1281 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1283 /* Strip off any initial CV-qualifiers, as they really apply
1284 to the `this' parameter, and they were not output by the
1285 v2 demangler without DMGL_PARAMS. */
1286 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1287 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1288 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1289 || dc->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
1290 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1291 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1292 dc = d_left (dc);
1294 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1295 there may be CV-qualifiers on its right argument which
1296 really apply here; this happens when parsing a class
1297 which is local to a function. */
1298 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1300 struct demangle_component *dcr;
1302 dcr = d_right (dc);
1303 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1304 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1305 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1306 || dcr->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
1307 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1308 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1309 dcr = d_left (dcr);
1310 dc->u.s_binary.right = dcr;
1313 return dc;
1316 peek = d_peek_char (di);
1317 if (dc == NULL || peek == '\0' || peek == 'E')
1318 return dc;
1319 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1320 d_bare_function_type (di, has_return_type (dc)));
1324 /* <tagged-name> ::= <name> B <source-name> */
1326 static struct demangle_component *
1327 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1329 struct demangle_component *hold_last_name;
1330 char peek;
1332 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1333 hold_last_name = di->last_name;
1335 while (peek = d_peek_char (di),
1336 peek == 'B')
1338 struct demangle_component *tag;
1339 d_advance (di, 1);
1340 tag = d_source_name (di);
1341 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1344 di->last_name = hold_last_name;
1346 return dc;
1349 /* <name> ::= <nested-name>
1350 ::= <unscoped-name>
1351 ::= <unscoped-template-name> <template-args>
1352 ::= <local-name>
1354 <unscoped-name> ::= <unqualified-name>
1355 ::= St <unqualified-name>
1357 <unscoped-template-name> ::= <unscoped-name>
1358 ::= <substitution>
1361 static struct demangle_component *
1362 d_name (struct d_info *di)
1364 char peek = d_peek_char (di);
1365 struct demangle_component *dc;
1367 switch (peek)
1369 case 'N':
1370 return d_nested_name (di);
1372 case 'Z':
1373 return d_local_name (di);
1375 case 'U':
1376 return d_unqualified_name (di);
1378 case 'S':
1380 int subst;
1382 if (d_peek_next_char (di) != 't')
1384 dc = d_substitution (di, 0);
1385 subst = 1;
1387 else
1389 d_advance (di, 2);
1390 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1391 d_make_name (di, "std", 3),
1392 d_unqualified_name (di));
1393 di->expansion += 3;
1394 subst = 0;
1397 if (d_peek_char (di) != 'I')
1399 /* The grammar does not permit this case to occur if we
1400 called d_substitution() above (i.e., subst == 1). We
1401 don't bother to check. */
1403 else
1405 /* This is <template-args>, which means that we just saw
1406 <unscoped-template-name>, which is a substitution
1407 candidate if we didn't just get it from a
1408 substitution. */
1409 if (! subst)
1411 if (! d_add_substitution (di, dc))
1412 return NULL;
1414 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1415 d_template_args (di));
1418 return dc;
1421 case 'L':
1422 default:
1423 dc = d_unqualified_name (di);
1424 if (d_peek_char (di) == 'I')
1426 /* This is <template-args>, which means that we just saw
1427 <unscoped-template-name>, which is a substitution
1428 candidate. */
1429 if (! d_add_substitution (di, dc))
1430 return NULL;
1431 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1432 d_template_args (di));
1434 return dc;
1438 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1439 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1442 static struct demangle_component *
1443 d_nested_name (struct d_info *di)
1445 struct demangle_component *ret;
1446 struct demangle_component **pret;
1447 struct demangle_component *rqual;
1449 if (! d_check_char (di, 'N'))
1450 return NULL;
1452 pret = d_cv_qualifiers (di, &ret, 1);
1453 if (pret == NULL)
1454 return NULL;
1456 /* Parse the ref-qualifier now and then attach it
1457 once we have something to attach it to. */
1458 rqual = d_ref_qualifier (di, NULL);
1460 *pret = d_prefix (di);
1461 if (*pret == NULL)
1462 return NULL;
1464 if (rqual)
1466 d_left (rqual) = ret;
1467 ret = rqual;
1470 if (! d_check_char (di, 'E'))
1471 return NULL;
1473 return ret;
1476 /* <prefix> ::= <prefix> <unqualified-name>
1477 ::= <template-prefix> <template-args>
1478 ::= <template-param>
1479 ::= <decltype>
1481 ::= <substitution>
1483 <template-prefix> ::= <prefix> <(template) unqualified-name>
1484 ::= <template-param>
1485 ::= <substitution>
1488 static struct demangle_component *
1489 d_prefix (struct d_info *di)
1491 struct demangle_component *ret = NULL;
1493 while (1)
1495 char peek;
1496 enum demangle_component_type comb_type;
1497 struct demangle_component *dc;
1499 peek = d_peek_char (di);
1500 if (peek == '\0')
1501 return NULL;
1503 /* The older code accepts a <local-name> here, but I don't see
1504 that in the grammar. The older code does not accept a
1505 <template-param> here. */
1507 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1508 if (peek == 'D')
1510 char peek2 = d_peek_next_char (di);
1511 if (peek2 == 'T' || peek2 == 't')
1512 /* Decltype. */
1513 dc = cplus_demangle_type (di);
1514 else
1515 /* Destructor name. */
1516 dc = d_unqualified_name (di);
1518 else if (IS_DIGIT (peek)
1519 || IS_LOWER (peek)
1520 || peek == 'C'
1521 || peek == 'U'
1522 || peek == 'L')
1523 dc = d_unqualified_name (di);
1524 else if (peek == 'S')
1525 dc = d_substitution (di, 1);
1526 else if (peek == 'I')
1528 if (ret == NULL)
1529 return NULL;
1530 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1531 dc = d_template_args (di);
1533 else if (peek == 'T')
1534 dc = d_template_param (di);
1535 else if (peek == 'E')
1536 return ret;
1537 else if (peek == 'M')
1539 /* Initializer scope for a lambda. We don't need to represent
1540 this; the normal code will just treat the variable as a type
1541 scope, which gives appropriate output. */
1542 if (ret == NULL)
1543 return NULL;
1544 d_advance (di, 1);
1545 continue;
1547 else
1548 return NULL;
1550 if (ret == NULL)
1551 ret = dc;
1552 else
1553 ret = d_make_comp (di, comb_type, ret, dc);
1555 if (peek != 'S' && d_peek_char (di) != 'E')
1557 if (! d_add_substitution (di, ret))
1558 return NULL;
1563 /* <unqualified-name> ::= <operator-name>
1564 ::= <ctor-dtor-name>
1565 ::= <source-name>
1566 ::= <local-source-name>
1568 <local-source-name> ::= L <source-name> <discriminator>
1571 static struct demangle_component *
1572 d_unqualified_name (struct d_info *di)
1574 struct demangle_component *ret;
1575 char peek;
1577 peek = d_peek_char (di);
1578 if (IS_DIGIT (peek))
1579 ret = d_source_name (di);
1580 else if (IS_LOWER (peek))
1582 ret = d_operator_name (di);
1583 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1585 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1586 if (!strcmp (ret->u.s_operator.op->code, "li"))
1587 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1588 d_source_name (di));
1591 else if (peek == 'C' || peek == 'D')
1592 ret = d_ctor_dtor_name (di);
1593 else if (peek == 'L')
1595 d_advance (di, 1);
1597 ret = d_source_name (di);
1598 if (ret == NULL)
1599 return NULL;
1600 if (! d_discriminator (di))
1601 return NULL;
1603 else if (peek == 'U')
1605 switch (d_peek_next_char (di))
1607 case 'l':
1608 ret = d_lambda (di);
1609 break;
1610 case 't':
1611 ret = d_unnamed_type (di);
1612 break;
1613 default:
1614 return NULL;
1617 else
1618 return NULL;
1620 if (d_peek_char (di) == 'B')
1621 ret = d_abi_tags (di, ret);
1622 return ret;
1625 /* <source-name> ::= <(positive length) number> <identifier> */
1627 static struct demangle_component *
1628 d_source_name (struct d_info *di)
1630 int len;
1631 struct demangle_component *ret;
1633 len = d_number (di);
1634 if (len <= 0)
1635 return NULL;
1636 ret = d_identifier (di, len);
1637 di->last_name = ret;
1638 return ret;
1641 /* number ::= [n] <(non-negative decimal integer)> */
1643 static int
1644 d_number (struct d_info *di)
1646 int negative;
1647 char peek;
1648 int ret;
1650 negative = 0;
1651 peek = d_peek_char (di);
1652 if (peek == 'n')
1654 negative = 1;
1655 d_advance (di, 1);
1656 peek = d_peek_char (di);
1659 ret = 0;
1660 while (1)
1662 if (! IS_DIGIT (peek))
1664 if (negative)
1665 ret = - ret;
1666 return ret;
1668 ret = ret * 10 + peek - '0';
1669 d_advance (di, 1);
1670 peek = d_peek_char (di);
1674 /* Like d_number, but returns a demangle_component. */
1676 static struct demangle_component *
1677 d_number_component (struct d_info *di)
1679 struct demangle_component *ret = d_make_empty (di);
1680 if (ret)
1682 ret->type = DEMANGLE_COMPONENT_NUMBER;
1683 ret->u.s_number.number = d_number (di);
1685 return ret;
1688 /* identifier ::= <(unqualified source code identifier)> */
1690 static struct demangle_component *
1691 d_identifier (struct d_info *di, int len)
1693 const char *name;
1695 name = d_str (di);
1697 if (di->send - name < len)
1698 return NULL;
1700 d_advance (di, len);
1702 /* A Java mangled name may have a trailing '$' if it is a C++
1703 keyword. This '$' is not included in the length count. We just
1704 ignore the '$'. */
1705 if ((di->options & DMGL_JAVA) != 0
1706 && d_peek_char (di) == '$')
1707 d_advance (di, 1);
1709 /* Look for something which looks like a gcc encoding of an
1710 anonymous namespace, and replace it with a more user friendly
1711 name. */
1712 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1713 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1714 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1716 const char *s;
1718 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1719 if ((*s == '.' || *s == '_' || *s == '$')
1720 && s[1] == 'N')
1722 di->expansion -= len - sizeof "(anonymous namespace)";
1723 return d_make_name (di, "(anonymous namespace)",
1724 sizeof "(anonymous namespace)" - 1);
1728 return d_make_name (di, name, len);
1731 /* operator_name ::= many different two character encodings.
1732 ::= cv <type>
1733 ::= v <digit> <source-name>
1735 This list is sorted for binary search. */
1737 #define NL(s) s, (sizeof s) - 1
1739 CP_STATIC_IF_GLIBCPP_V3
1740 const struct demangle_operator_info cplus_demangle_operators[] =
1742 { "aN", NL ("&="), 2 },
1743 { "aS", NL ("="), 2 },
1744 { "aa", NL ("&&"), 2 },
1745 { "ad", NL ("&"), 1 },
1746 { "an", NL ("&"), 2 },
1747 { "at", NL ("alignof "), 1 },
1748 { "az", NL ("alignof "), 1 },
1749 { "cc", NL ("const_cast"), 2 },
1750 { "cl", NL ("()"), 2 },
1751 { "cm", NL (","), 2 },
1752 { "co", NL ("~"), 1 },
1753 { "dV", NL ("/="), 2 },
1754 { "da", NL ("delete[] "), 1 },
1755 { "dc", NL ("dynamic_cast"), 2 },
1756 { "de", NL ("*"), 1 },
1757 { "dl", NL ("delete "), 1 },
1758 { "ds", NL (".*"), 2 },
1759 { "dt", NL ("."), 2 },
1760 { "dv", NL ("/"), 2 },
1761 { "eO", NL ("^="), 2 },
1762 { "eo", NL ("^"), 2 },
1763 { "eq", NL ("=="), 2 },
1764 { "ge", NL (">="), 2 },
1765 { "gs", NL ("::"), 1 },
1766 { "gt", NL (">"), 2 },
1767 { "ix", NL ("[]"), 2 },
1768 { "lS", NL ("<<="), 2 },
1769 { "le", NL ("<="), 2 },
1770 { "li", NL ("operator\"\" "), 1 },
1771 { "ls", NL ("<<"), 2 },
1772 { "lt", NL ("<"), 2 },
1773 { "mI", NL ("-="), 2 },
1774 { "mL", NL ("*="), 2 },
1775 { "mi", NL ("-"), 2 },
1776 { "ml", NL ("*"), 2 },
1777 { "mm", NL ("--"), 1 },
1778 { "na", NL ("new[]"), 3 },
1779 { "ne", NL ("!="), 2 },
1780 { "ng", NL ("-"), 1 },
1781 { "nt", NL ("!"), 1 },
1782 { "nw", NL ("new"), 3 },
1783 { "oR", NL ("|="), 2 },
1784 { "oo", NL ("||"), 2 },
1785 { "or", NL ("|"), 2 },
1786 { "pL", NL ("+="), 2 },
1787 { "pl", NL ("+"), 2 },
1788 { "pm", NL ("->*"), 2 },
1789 { "pp", NL ("++"), 1 },
1790 { "ps", NL ("+"), 1 },
1791 { "pt", NL ("->"), 2 },
1792 { "qu", NL ("?"), 3 },
1793 { "rM", NL ("%="), 2 },
1794 { "rS", NL (">>="), 2 },
1795 { "rc", NL ("reinterpret_cast"), 2 },
1796 { "rm", NL ("%"), 2 },
1797 { "rs", NL (">>"), 2 },
1798 { "sc", NL ("static_cast"), 2 },
1799 { "st", NL ("sizeof "), 1 },
1800 { "sz", NL ("sizeof "), 1 },
1801 { "tr", NL ("throw"), 0 },
1802 { "tw", NL ("throw "), 1 },
1803 { NULL, NULL, 0, 0 }
1806 static struct demangle_component *
1807 d_operator_name (struct d_info *di)
1809 char c1;
1810 char c2;
1812 c1 = d_next_char (di);
1813 c2 = d_next_char (di);
1814 if (c1 == 'v' && IS_DIGIT (c2))
1815 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1816 else if (c1 == 'c' && c2 == 'v')
1818 struct demangle_component *type;
1819 int was_conversion = di->is_conversion;
1820 struct demangle_component *res;
1822 di->is_conversion = ! di->is_expression;
1823 type = cplus_demangle_type (di);
1824 if (di->is_conversion)
1825 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
1826 else
1827 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1828 di->is_conversion = was_conversion;
1829 return res;
1831 else
1833 /* LOW is the inclusive lower bound. */
1834 int low = 0;
1835 /* HIGH is the exclusive upper bound. We subtract one to ignore
1836 the sentinel at the end of the array. */
1837 int high = ((sizeof (cplus_demangle_operators)
1838 / sizeof (cplus_demangle_operators[0]))
1839 - 1);
1841 while (1)
1843 int i;
1844 const struct demangle_operator_info *p;
1846 i = low + (high - low) / 2;
1847 p = cplus_demangle_operators + i;
1849 if (c1 == p->code[0] && c2 == p->code[1])
1850 return d_make_operator (di, p);
1852 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1853 high = i;
1854 else
1855 low = i + 1;
1856 if (low == high)
1857 return NULL;
1862 static struct demangle_component *
1863 d_make_character (struct d_info *di, int c)
1865 struct demangle_component *p;
1866 p = d_make_empty (di);
1867 if (p != NULL)
1869 p->type = DEMANGLE_COMPONENT_CHARACTER;
1870 p->u.s_character.character = c;
1872 return p;
1875 static struct demangle_component *
1876 d_java_resource (struct d_info *di)
1878 struct demangle_component *p = NULL;
1879 struct demangle_component *next = NULL;
1880 int len, i;
1881 char c;
1882 const char *str;
1884 len = d_number (di);
1885 if (len <= 1)
1886 return NULL;
1888 /* Eat the leading '_'. */
1889 if (d_next_char (di) != '_')
1890 return NULL;
1891 len--;
1893 str = d_str (di);
1894 i = 0;
1896 while (len > 0)
1898 c = str[i];
1899 if (!c)
1900 return NULL;
1902 /* Each chunk is either a '$' escape... */
1903 if (c == '$')
1905 i++;
1906 switch (str[i++])
1908 case 'S':
1909 c = '/';
1910 break;
1911 case '_':
1912 c = '.';
1913 break;
1914 case '$':
1915 c = '$';
1916 break;
1917 default:
1918 return NULL;
1920 next = d_make_character (di, c);
1921 d_advance (di, i);
1922 str = d_str (di);
1923 len -= i;
1924 i = 0;
1925 if (next == NULL)
1926 return NULL;
1928 /* ... or a sequence of characters. */
1929 else
1931 while (i < len && str[i] && str[i] != '$')
1932 i++;
1934 next = d_make_name (di, str, i);
1935 d_advance (di, i);
1936 str = d_str (di);
1937 len -= i;
1938 i = 0;
1939 if (next == NULL)
1940 return NULL;
1943 if (p == NULL)
1944 p = next;
1945 else
1947 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1948 if (p == NULL)
1949 return NULL;
1953 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1955 return p;
1958 /* <special-name> ::= TV <type>
1959 ::= TT <type>
1960 ::= TI <type>
1961 ::= TS <type>
1962 ::= GV <(object) name>
1963 ::= T <call-offset> <(base) encoding>
1964 ::= Tc <call-offset> <call-offset> <(base) encoding>
1965 Also g++ extensions:
1966 ::= TC <type> <(offset) number> _ <(base) type>
1967 ::= TF <type>
1968 ::= TJ <type>
1969 ::= GR <name>
1970 ::= GA <encoding>
1971 ::= Gr <resource name>
1972 ::= GTt <encoding>
1973 ::= GTn <encoding>
1976 static struct demangle_component *
1977 d_special_name (struct d_info *di)
1979 di->expansion += 20;
1980 if (d_check_char (di, 'T'))
1982 switch (d_next_char (di))
1984 case 'V':
1985 di->expansion -= 5;
1986 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1987 cplus_demangle_type (di), NULL);
1988 case 'T':
1989 di->expansion -= 10;
1990 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1991 cplus_demangle_type (di), NULL);
1992 case 'I':
1993 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1994 cplus_demangle_type (di), NULL);
1995 case 'S':
1996 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1997 cplus_demangle_type (di), NULL);
1999 case 'h':
2000 if (! d_call_offset (di, 'h'))
2001 return NULL;
2002 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2003 d_encoding (di, 0), NULL);
2005 case 'v':
2006 if (! d_call_offset (di, 'v'))
2007 return NULL;
2008 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2009 d_encoding (di, 0), NULL);
2011 case 'c':
2012 if (! d_call_offset (di, '\0'))
2013 return NULL;
2014 if (! d_call_offset (di, '\0'))
2015 return NULL;
2016 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2017 d_encoding (di, 0), NULL);
2019 case 'C':
2021 struct demangle_component *derived_type;
2022 int offset;
2023 struct demangle_component *base_type;
2025 derived_type = cplus_demangle_type (di);
2026 offset = d_number (di);
2027 if (offset < 0)
2028 return NULL;
2029 if (! d_check_char (di, '_'))
2030 return NULL;
2031 base_type = cplus_demangle_type (di);
2032 /* We don't display the offset. FIXME: We should display
2033 it in verbose mode. */
2034 di->expansion += 5;
2035 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2036 base_type, derived_type);
2039 case 'F':
2040 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2041 cplus_demangle_type (di), NULL);
2042 case 'J':
2043 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2044 cplus_demangle_type (di), NULL);
2046 case 'H':
2047 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2048 d_name (di), NULL);
2050 case 'W':
2051 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2052 d_name (di), NULL);
2054 default:
2055 return NULL;
2058 else if (d_check_char (di, 'G'))
2060 switch (d_next_char (di))
2062 case 'V':
2063 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2065 case 'R':
2067 struct demangle_component *name = d_name (di);
2068 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2069 d_number_component (di));
2072 case 'A':
2073 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2074 d_encoding (di, 0), NULL);
2076 case 'T':
2077 switch (d_next_char (di))
2079 case 'n':
2080 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2081 d_encoding (di, 0), NULL);
2082 default:
2083 /* ??? The proposal is that other letters (such as 'h') stand
2084 for different variants of transaction cloning, such as
2085 compiling directly for hardware transaction support. But
2086 they still should all be transactional clones of some sort
2087 so go ahead and call them that. */
2088 case 't':
2089 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2090 d_encoding (di, 0), NULL);
2093 case 'r':
2094 return d_java_resource (di);
2096 default:
2097 return NULL;
2100 else
2101 return NULL;
2104 /* <call-offset> ::= h <nv-offset> _
2105 ::= v <v-offset> _
2107 <nv-offset> ::= <(offset) number>
2109 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2111 The C parameter, if not '\0', is a character we just read which is
2112 the start of the <call-offset>.
2114 We don't display the offset information anywhere. FIXME: We should
2115 display it in verbose mode. */
2117 static int
2118 d_call_offset (struct d_info *di, int c)
2120 if (c == '\0')
2121 c = d_next_char (di);
2123 if (c == 'h')
2124 d_number (di);
2125 else if (c == 'v')
2127 d_number (di);
2128 if (! d_check_char (di, '_'))
2129 return 0;
2130 d_number (di);
2132 else
2133 return 0;
2135 if (! d_check_char (di, '_'))
2136 return 0;
2138 return 1;
2141 /* <ctor-dtor-name> ::= C1
2142 ::= C2
2143 ::= C3
2144 ::= D0
2145 ::= D1
2146 ::= D2
2149 static struct demangle_component *
2150 d_ctor_dtor_name (struct d_info *di)
2152 if (di->last_name != NULL)
2154 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2155 di->expansion += di->last_name->u.s_name.len;
2156 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2157 di->expansion += di->last_name->u.s_string.len;
2159 switch (d_peek_char (di))
2161 case 'C':
2163 enum gnu_v3_ctor_kinds kind;
2165 switch (d_peek_next_char (di))
2167 case '1':
2168 kind = gnu_v3_complete_object_ctor;
2169 break;
2170 case '2':
2171 kind = gnu_v3_base_object_ctor;
2172 break;
2173 case '3':
2174 kind = gnu_v3_complete_object_allocating_ctor;
2175 break;
2176 case '4':
2177 kind = gnu_v3_unified_ctor;
2178 break;
2179 case '5':
2180 kind = gnu_v3_object_ctor_group;
2181 break;
2182 default:
2183 return NULL;
2185 d_advance (di, 2);
2186 return d_make_ctor (di, kind, di->last_name);
2189 case 'D':
2191 enum gnu_v3_dtor_kinds kind;
2193 switch (d_peek_next_char (di))
2195 case '0':
2196 kind = gnu_v3_deleting_dtor;
2197 break;
2198 case '1':
2199 kind = gnu_v3_complete_object_dtor;
2200 break;
2201 case '2':
2202 kind = gnu_v3_base_object_dtor;
2203 break;
2204 /* digit '3' is not used */
2205 case '4':
2206 kind = gnu_v3_unified_dtor;
2207 break;
2208 case '5':
2209 kind = gnu_v3_object_dtor_group;
2210 break;
2211 default:
2212 return NULL;
2214 d_advance (di, 2);
2215 return d_make_dtor (di, kind, di->last_name);
2218 default:
2219 return NULL;
2223 /* <type> ::= <builtin-type>
2224 ::= <function-type>
2225 ::= <class-enum-type>
2226 ::= <array-type>
2227 ::= <pointer-to-member-type>
2228 ::= <template-param>
2229 ::= <template-template-param> <template-args>
2230 ::= <substitution>
2231 ::= <CV-qualifiers> <type>
2232 ::= P <type>
2233 ::= R <type>
2234 ::= O <type> (C++0x)
2235 ::= C <type>
2236 ::= G <type>
2237 ::= U <source-name> <type>
2239 <builtin-type> ::= various one letter codes
2240 ::= u <source-name>
2243 CP_STATIC_IF_GLIBCPP_V3
2244 const struct demangle_builtin_type_info
2245 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2247 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2248 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2249 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2250 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2251 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2252 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2253 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2254 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2255 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2256 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2257 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2258 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2259 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2260 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2261 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2262 D_PRINT_DEFAULT },
2263 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2264 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2265 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2266 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2267 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2268 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2269 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2270 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2271 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2272 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2273 D_PRINT_UNSIGNED_LONG_LONG },
2274 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2275 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2276 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2277 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2278 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2279 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2280 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2281 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2282 D_PRINT_DEFAULT },
2285 CP_STATIC_IF_GLIBCPP_V3
2286 struct demangle_component *
2287 cplus_demangle_type (struct d_info *di)
2289 char peek;
2290 struct demangle_component *ret;
2291 int can_subst;
2293 /* The ABI specifies that when CV-qualifiers are used, the base type
2294 is substitutable, and the fully qualified type is substitutable,
2295 but the base type with a strict subset of the CV-qualifiers is
2296 not substitutable. The natural recursive implementation of the
2297 CV-qualifiers would cause subsets to be substitutable, so instead
2298 we pull them all off now.
2300 FIXME: The ABI says that order-insensitive vendor qualifiers
2301 should be handled in the same way, but we have no way to tell
2302 which vendor qualifiers are order-insensitive and which are
2303 order-sensitive. So we just assume that they are all
2304 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2305 __vector, and it treats it as order-sensitive when mangling
2306 names. */
2308 peek = d_peek_char (di);
2309 if (peek == 'r' || peek == 'V' || peek == 'K'
2310 || (peek == 'D' && d_peek_next_char (di) == 'x'))
2312 struct demangle_component **pret;
2314 pret = d_cv_qualifiers (di, &ret, 0);
2315 if (pret == NULL)
2316 return NULL;
2317 if (d_peek_char (di) == 'F')
2319 /* cv-qualifiers before a function type apply to 'this',
2320 so avoid adding the unqualified function type to
2321 the substitution list. */
2322 *pret = d_function_type (di);
2324 else
2325 *pret = cplus_demangle_type (di);
2326 if (!*pret)
2327 return NULL;
2328 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2329 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2331 /* Move the ref-qualifier outside the cv-qualifiers so that
2332 they are printed in the right order. */
2333 struct demangle_component *fn = d_left (*pret);
2334 d_left (*pret) = ret;
2335 ret = *pret;
2336 *pret = fn;
2338 if (! d_add_substitution (di, ret))
2339 return NULL;
2340 return ret;
2343 can_subst = 1;
2345 switch (peek)
2347 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2348 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2349 case 'o': case 's': case 't':
2350 case 'v': case 'w': case 'x': case 'y': case 'z':
2351 ret = d_make_builtin_type (di,
2352 &cplus_demangle_builtin_types[peek - 'a']);
2353 di->expansion += ret->u.s_builtin.type->len;
2354 can_subst = 0;
2355 d_advance (di, 1);
2356 break;
2358 case 'u':
2359 d_advance (di, 1);
2360 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2361 d_source_name (di), NULL);
2362 break;
2364 case 'F':
2365 ret = d_function_type (di);
2366 break;
2368 case '0': case '1': case '2': case '3': case '4':
2369 case '5': case '6': case '7': case '8': case '9':
2370 case 'N':
2371 case 'Z':
2372 ret = d_class_enum_type (di);
2373 break;
2375 case 'A':
2376 ret = d_array_type (di);
2377 break;
2379 case 'M':
2380 ret = d_pointer_to_member_type (di);
2381 break;
2383 case 'T':
2384 ret = d_template_param (di);
2385 if (d_peek_char (di) == 'I')
2387 /* This may be <template-template-param> <template-args>.
2388 If this is the type for a conversion operator, we can
2389 have a <template-template-param> here only by following
2390 a derivation like this:
2392 <nested-name>
2393 -> <template-prefix> <template-args>
2394 -> <prefix> <template-unqualified-name> <template-args>
2395 -> <unqualified-name> <template-unqualified-name> <template-args>
2396 -> <source-name> <template-unqualified-name> <template-args>
2397 -> <source-name> <operator-name> <template-args>
2398 -> <source-name> cv <type> <template-args>
2399 -> <source-name> cv <template-template-param> <template-args> <template-args>
2401 where the <template-args> is followed by another.
2402 Otherwise, we must have a derivation like this:
2404 <nested-name>
2405 -> <template-prefix> <template-args>
2406 -> <prefix> <template-unqualified-name> <template-args>
2407 -> <unqualified-name> <template-unqualified-name> <template-args>
2408 -> <source-name> <template-unqualified-name> <template-args>
2409 -> <source-name> <operator-name> <template-args>
2410 -> <source-name> cv <type> <template-args>
2411 -> <source-name> cv <template-param> <template-args>
2413 where we need to leave the <template-args> to be processed
2414 by d_prefix (following the <template-prefix>).
2416 The <template-template-param> part is a substitution
2417 candidate. */
2418 if (! di->is_conversion)
2420 if (! d_add_substitution (di, ret))
2421 return NULL;
2422 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2423 d_template_args (di));
2425 else
2427 struct demangle_component *args;
2428 struct d_info_checkpoint checkpoint;
2430 d_checkpoint (di, &checkpoint);
2431 args = d_template_args (di);
2432 if (d_peek_char (di) == 'I')
2434 if (! d_add_substitution (di, ret))
2435 return NULL;
2436 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2437 args);
2439 else
2440 d_backtrack (di, &checkpoint);
2443 break;
2445 case 'S':
2446 /* If this is a special substitution, then it is the start of
2447 <class-enum-type>. */
2449 char peek_next;
2451 peek_next = d_peek_next_char (di);
2452 if (IS_DIGIT (peek_next)
2453 || peek_next == '_'
2454 || IS_UPPER (peek_next))
2456 ret = d_substitution (di, 0);
2457 /* The substituted name may have been a template name and
2458 may be followed by tepmlate args. */
2459 if (d_peek_char (di) == 'I')
2460 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2461 d_template_args (di));
2462 else
2463 can_subst = 0;
2465 else
2467 ret = d_class_enum_type (di);
2468 /* If the substitution was a complete type, then it is not
2469 a new substitution candidate. However, if the
2470 substitution was followed by template arguments, then
2471 the whole thing is a substitution candidate. */
2472 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2473 can_subst = 0;
2476 break;
2478 case 'O':
2479 d_advance (di, 1);
2480 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2481 cplus_demangle_type (di), NULL);
2482 break;
2484 case 'P':
2485 d_advance (di, 1);
2486 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2487 cplus_demangle_type (di), NULL);
2488 break;
2490 case 'R':
2491 d_advance (di, 1);
2492 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2493 cplus_demangle_type (di), NULL);
2494 break;
2496 case 'C':
2497 d_advance (di, 1);
2498 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2499 cplus_demangle_type (di), NULL);
2500 break;
2502 case 'G':
2503 d_advance (di, 1);
2504 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2505 cplus_demangle_type (di), NULL);
2506 break;
2508 case 'U':
2509 d_advance (di, 1);
2510 ret = d_source_name (di);
2511 if (d_peek_char (di) == 'I')
2512 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2513 d_template_args (di));
2514 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2515 cplus_demangle_type (di), ret);
2516 break;
2518 case 'D':
2519 can_subst = 0;
2520 d_advance (di, 1);
2521 peek = d_next_char (di);
2522 switch (peek)
2524 case 'T':
2525 case 't':
2526 /* decltype (expression) */
2527 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2528 d_expression (di), NULL);
2529 if (ret && d_next_char (di) != 'E')
2530 ret = NULL;
2531 can_subst = 1;
2532 break;
2534 case 'p':
2535 /* Pack expansion. */
2536 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2537 cplus_demangle_type (di), NULL);
2538 can_subst = 1;
2539 break;
2541 case 'a':
2542 /* auto */
2543 ret = d_make_name (di, "auto", 4);
2544 break;
2546 case 'f':
2547 /* 32-bit decimal floating point */
2548 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2549 di->expansion += ret->u.s_builtin.type->len;
2550 break;
2551 case 'd':
2552 /* 64-bit DFP */
2553 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2554 di->expansion += ret->u.s_builtin.type->len;
2555 break;
2556 case 'e':
2557 /* 128-bit DFP */
2558 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2559 di->expansion += ret->u.s_builtin.type->len;
2560 break;
2561 case 'h':
2562 /* 16-bit half-precision FP */
2563 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2564 di->expansion += ret->u.s_builtin.type->len;
2565 break;
2566 case 's':
2567 /* char16_t */
2568 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2569 di->expansion += ret->u.s_builtin.type->len;
2570 break;
2571 case 'i':
2572 /* char32_t */
2573 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2574 di->expansion += ret->u.s_builtin.type->len;
2575 break;
2577 case 'F':
2578 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2579 ret = d_make_empty (di);
2580 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2581 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2582 /* For demangling we don't care about the bits. */
2583 d_number (di);
2584 ret->u.s_fixed.length = cplus_demangle_type (di);
2585 if (ret->u.s_fixed.length == NULL)
2586 return NULL;
2587 d_number (di);
2588 peek = d_next_char (di);
2589 ret->u.s_fixed.sat = (peek == 's');
2590 break;
2592 case 'v':
2593 ret = d_vector_type (di);
2594 can_subst = 1;
2595 break;
2597 case 'n':
2598 /* decltype(nullptr) */
2599 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2600 di->expansion += ret->u.s_builtin.type->len;
2601 break;
2603 default:
2604 return NULL;
2606 break;
2608 default:
2609 return NULL;
2612 if (can_subst)
2614 if (! d_add_substitution (di, ret))
2615 return NULL;
2618 return ret;
2621 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2623 static struct demangle_component **
2624 d_cv_qualifiers (struct d_info *di,
2625 struct demangle_component **pret, int member_fn)
2627 struct demangle_component **pstart;
2628 char peek;
2630 pstart = pret;
2631 peek = d_peek_char (di);
2632 while (peek == 'r' || peek == 'V' || peek == 'K'
2633 || (peek == 'D' && d_peek_next_char (di) == 'x'))
2635 enum demangle_component_type t;
2637 d_advance (di, 1);
2638 if (peek == 'r')
2640 t = (member_fn
2641 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2642 : DEMANGLE_COMPONENT_RESTRICT);
2643 di->expansion += sizeof "restrict";
2645 else if (peek == 'V')
2647 t = (member_fn
2648 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2649 : DEMANGLE_COMPONENT_VOLATILE);
2650 di->expansion += sizeof "volatile";
2652 else if (peek == 'K')
2654 t = (member_fn
2655 ? DEMANGLE_COMPONENT_CONST_THIS
2656 : DEMANGLE_COMPONENT_CONST);
2657 di->expansion += sizeof "const";
2659 else
2661 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2662 di->expansion += sizeof "transaction_safe";
2663 d_advance (di, 1);
2666 *pret = d_make_comp (di, t, NULL, NULL);
2667 if (*pret == NULL)
2668 return NULL;
2669 pret = &d_left (*pret);
2671 peek = d_peek_char (di);
2674 if (!member_fn && peek == 'F')
2676 while (pstart != pret)
2678 switch ((*pstart)->type)
2680 case DEMANGLE_COMPONENT_RESTRICT:
2681 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2682 break;
2683 case DEMANGLE_COMPONENT_VOLATILE:
2684 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2685 break;
2686 case DEMANGLE_COMPONENT_CONST:
2687 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2688 break;
2689 default:
2690 break;
2692 pstart = &d_left (*pstart);
2696 return pret;
2699 /* <ref-qualifier> ::= R
2700 ::= O */
2702 static struct demangle_component *
2703 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2705 struct demangle_component *ret = sub;
2706 char peek;
2708 peek = d_peek_char (di);
2709 if (peek == 'R' || peek == 'O')
2711 enum demangle_component_type t;
2712 if (peek == 'R')
2714 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2715 di->expansion += sizeof "&";
2717 else
2719 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2720 di->expansion += sizeof "&&";
2722 d_advance (di, 1);
2724 ret = d_make_comp (di, t, ret, NULL);
2727 return ret;
2730 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2732 static struct demangle_component *
2733 d_function_type (struct d_info *di)
2735 struct demangle_component *ret;
2737 if (! d_check_char (di, 'F'))
2738 return NULL;
2739 if (d_peek_char (di) == 'Y')
2741 /* Function has C linkage. We don't print this information.
2742 FIXME: We should print it in verbose mode. */
2743 d_advance (di, 1);
2745 ret = d_bare_function_type (di, 1);
2746 ret = d_ref_qualifier (di, ret);
2748 if (! d_check_char (di, 'E'))
2749 return NULL;
2750 return ret;
2753 /* <type>+ */
2755 static struct demangle_component *
2756 d_parmlist (struct d_info *di)
2758 struct demangle_component *tl;
2759 struct demangle_component **ptl;
2761 tl = NULL;
2762 ptl = &tl;
2763 while (1)
2765 struct demangle_component *type;
2767 char peek = d_peek_char (di);
2768 if (peek == '\0' || peek == 'E' || peek == '.')
2769 break;
2770 if ((peek == 'R' || peek == 'O')
2771 && d_peek_next_char (di) == 'E')
2772 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2773 break;
2774 type = cplus_demangle_type (di);
2775 if (type == NULL)
2776 return NULL;
2777 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2778 if (*ptl == NULL)
2779 return NULL;
2780 ptl = &d_right (*ptl);
2783 /* There should be at least one parameter type besides the optional
2784 return type. A function which takes no arguments will have a
2785 single parameter type void. */
2786 if (tl == NULL)
2787 return NULL;
2789 /* If we have a single parameter type void, omit it. */
2790 if (d_right (tl) == NULL
2791 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2792 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2794 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2795 d_left (tl) = NULL;
2798 return tl;
2801 /* <bare-function-type> ::= [J]<type>+ */
2803 static struct demangle_component *
2804 d_bare_function_type (struct d_info *di, int has_return_type)
2806 struct demangle_component *return_type;
2807 struct demangle_component *tl;
2808 char peek;
2810 /* Detect special qualifier indicating that the first argument
2811 is the return type. */
2812 peek = d_peek_char (di);
2813 if (peek == 'J')
2815 d_advance (di, 1);
2816 has_return_type = 1;
2819 if (has_return_type)
2821 return_type = cplus_demangle_type (di);
2822 if (return_type == NULL)
2823 return NULL;
2825 else
2826 return_type = NULL;
2828 tl = d_parmlist (di);
2829 if (tl == NULL)
2830 return NULL;
2832 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2833 return_type, tl);
2836 /* <class-enum-type> ::= <name> */
2838 static struct demangle_component *
2839 d_class_enum_type (struct d_info *di)
2841 return d_name (di);
2844 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2845 ::= A [<(dimension) expression>] _ <(element) type>
2848 static struct demangle_component *
2849 d_array_type (struct d_info *di)
2851 char peek;
2852 struct demangle_component *dim;
2854 if (! d_check_char (di, 'A'))
2855 return NULL;
2857 peek = d_peek_char (di);
2858 if (peek == '_')
2859 dim = NULL;
2860 else if (IS_DIGIT (peek))
2862 const char *s;
2864 s = d_str (di);
2867 d_advance (di, 1);
2868 peek = d_peek_char (di);
2870 while (IS_DIGIT (peek));
2871 dim = d_make_name (di, s, d_str (di) - s);
2872 if (dim == NULL)
2873 return NULL;
2875 else
2877 dim = d_expression (di);
2878 if (dim == NULL)
2879 return NULL;
2882 if (! d_check_char (di, '_'))
2883 return NULL;
2885 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2886 cplus_demangle_type (di));
2889 /* <vector-type> ::= Dv <number> _ <type>
2890 ::= Dv _ <expression> _ <type> */
2892 static struct demangle_component *
2893 d_vector_type (struct d_info *di)
2895 char peek;
2896 struct demangle_component *dim;
2898 peek = d_peek_char (di);
2899 if (peek == '_')
2901 d_advance (di, 1);
2902 dim = d_expression (di);
2904 else
2905 dim = d_number_component (di);
2907 if (dim == NULL)
2908 return NULL;
2910 if (! d_check_char (di, '_'))
2911 return NULL;
2913 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2914 cplus_demangle_type (di));
2917 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2919 static struct demangle_component *
2920 d_pointer_to_member_type (struct d_info *di)
2922 struct demangle_component *cl;
2923 struct demangle_component *mem;
2925 if (! d_check_char (di, 'M'))
2926 return NULL;
2928 cl = cplus_demangle_type (di);
2929 if (cl == NULL)
2930 return NULL;
2932 /* The ABI says, "The type of a non-static member function is considered
2933 to be different, for the purposes of substitution, from the type of a
2934 namespace-scope or static member function whose type appears
2935 similar. The types of two non-static member functions are considered
2936 to be different, for the purposes of substitution, if the functions
2937 are members of different classes. In other words, for the purposes of
2938 substitution, the class of which the function is a member is
2939 considered part of the type of function."
2941 For a pointer to member function, this call to cplus_demangle_type
2942 will end up adding a (possibly qualified) non-member function type to
2943 the substitution table, which is not correct; however, the member
2944 function type will never be used in a substitution, so putting the
2945 wrong type in the substitution table is harmless. */
2947 mem = cplus_demangle_type (di);
2948 if (mem == NULL)
2949 return NULL;
2951 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2954 /* <non-negative number> _ */
2956 static int
2957 d_compact_number (struct d_info *di)
2959 int num;
2960 if (d_peek_char (di) == '_')
2961 num = 0;
2962 else if (d_peek_char (di) == 'n')
2963 return -1;
2964 else
2965 num = d_number (di) + 1;
2967 if (num < 0 || ! d_check_char (di, '_'))
2968 return -1;
2969 return num;
2972 /* <template-param> ::= T_
2973 ::= T <(parameter-2 non-negative) number> _
2976 static struct demangle_component *
2977 d_template_param (struct d_info *di)
2979 int param;
2981 if (! d_check_char (di, 'T'))
2982 return NULL;
2984 param = d_compact_number (di);
2985 if (param < 0)
2986 return NULL;
2988 ++di->did_subs;
2990 return d_make_template_param (di, param);
2993 /* <template-args> ::= I <template-arg>+ E */
2995 static struct demangle_component *
2996 d_template_args (struct d_info *di)
2998 struct demangle_component *hold_last_name;
2999 struct demangle_component *al;
3000 struct demangle_component **pal;
3002 /* Preserve the last name we saw--don't let the template arguments
3003 clobber it, as that would give us the wrong name for a subsequent
3004 constructor or destructor. */
3005 hold_last_name = di->last_name;
3007 if (d_peek_char (di) != 'I'
3008 && d_peek_char (di) != 'J')
3009 return NULL;
3010 d_advance (di, 1);
3012 if (d_peek_char (di) == 'E')
3014 /* An argument pack can be empty. */
3015 d_advance (di, 1);
3016 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3019 al = NULL;
3020 pal = &al;
3021 while (1)
3023 struct demangle_component *a;
3025 a = d_template_arg (di);
3026 if (a == NULL)
3027 return NULL;
3029 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3030 if (*pal == NULL)
3031 return NULL;
3032 pal = &d_right (*pal);
3034 if (d_peek_char (di) == 'E')
3036 d_advance (di, 1);
3037 break;
3041 di->last_name = hold_last_name;
3043 return al;
3046 /* <template-arg> ::= <type>
3047 ::= X <expression> E
3048 ::= <expr-primary>
3051 static struct demangle_component *
3052 d_template_arg (struct d_info *di)
3054 struct demangle_component *ret;
3056 switch (d_peek_char (di))
3058 case 'X':
3059 d_advance (di, 1);
3060 ret = d_expression (di);
3061 if (! d_check_char (di, 'E'))
3062 return NULL;
3063 return ret;
3065 case 'L':
3066 return d_expr_primary (di);
3068 case 'I':
3069 case 'J':
3070 /* An argument pack. */
3071 return d_template_args (di);
3073 default:
3074 return cplus_demangle_type (di);
3078 /* Parse a sequence of expressions until we hit the terminator
3079 character. */
3081 static struct demangle_component *
3082 d_exprlist (struct d_info *di, char terminator)
3084 struct demangle_component *list = NULL;
3085 struct demangle_component **p = &list;
3087 if (d_peek_char (di) == terminator)
3089 d_advance (di, 1);
3090 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3093 while (1)
3095 struct demangle_component *arg = d_expression (di);
3096 if (arg == NULL)
3097 return NULL;
3099 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3100 if (*p == NULL)
3101 return NULL;
3102 p = &d_right (*p);
3104 if (d_peek_char (di) == terminator)
3106 d_advance (di, 1);
3107 break;
3111 return list;
3114 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3115 dynamic_cast, static_cast or reinterpret_cast. */
3117 static int
3118 op_is_new_cast (struct demangle_component *op)
3120 const char *code = op->u.s_operator.op->code;
3121 return (code[1] == 'c'
3122 && (code[0] == 's' || code[0] == 'd'
3123 || code[0] == 'c' || code[0] == 'r'));
3126 /* <expression> ::= <(unary) operator-name> <expression>
3127 ::= <(binary) operator-name> <expression> <expression>
3128 ::= <(trinary) operator-name> <expression> <expression> <expression>
3129 ::= cl <expression>+ E
3130 ::= st <type>
3131 ::= <template-param>
3132 ::= sr <type> <unqualified-name>
3133 ::= sr <type> <unqualified-name> <template-args>
3134 ::= <expr-primary>
3137 static inline struct demangle_component *
3138 d_expression_1 (struct d_info *di)
3140 char peek;
3142 peek = d_peek_char (di);
3143 if (peek == 'L')
3144 return d_expr_primary (di);
3145 else if (peek == 'T')
3146 return d_template_param (di);
3147 else if (peek == 's' && d_peek_next_char (di) == 'r')
3149 struct demangle_component *type;
3150 struct demangle_component *name;
3152 d_advance (di, 2);
3153 type = cplus_demangle_type (di);
3154 name = d_unqualified_name (di);
3155 if (d_peek_char (di) != 'I')
3156 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3157 else
3158 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3159 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3160 d_template_args (di)));
3162 else if (peek == 's' && d_peek_next_char (di) == 'p')
3164 d_advance (di, 2);
3165 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3166 d_expression_1 (di), NULL);
3168 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3170 /* Function parameter used in a late-specified return type. */
3171 int index;
3172 d_advance (di, 2);
3173 if (d_peek_char (di) == 'T')
3175 /* 'this' parameter. */
3176 d_advance (di, 1);
3177 index = 0;
3179 else
3181 index = d_compact_number (di);
3182 if (index == INT_MAX || index == -1)
3183 return NULL;
3184 index++;
3186 return d_make_function_param (di, index);
3188 else if (IS_DIGIT (peek)
3189 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3191 /* We can get an unqualified name as an expression in the case of
3192 a dependent function call, i.e. decltype(f(t)). */
3193 struct demangle_component *name;
3195 if (peek == 'o')
3196 /* operator-function-id, i.e. operator+(t). */
3197 d_advance (di, 2);
3199 name = d_unqualified_name (di);
3200 if (name == NULL)
3201 return NULL;
3202 if (d_peek_char (di) == 'I')
3203 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3204 d_template_args (di));
3205 else
3206 return name;
3208 else if ((peek == 'i' || peek == 't')
3209 && d_peek_next_char (di) == 'l')
3211 /* Brace-enclosed initializer list, untyped or typed. */
3212 struct demangle_component *type = NULL;
3213 if (peek == 't')
3214 type = cplus_demangle_type (di);
3215 if (!d_peek_next_char (di))
3216 return NULL;
3217 d_advance (di, 2);
3218 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3219 type, d_exprlist (di, 'E'));
3221 else
3223 struct demangle_component *op;
3224 const char *code = NULL;
3225 int args;
3227 op = d_operator_name (di);
3228 if (op == NULL)
3229 return NULL;
3231 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3233 code = op->u.s_operator.op->code;
3234 di->expansion += op->u.s_operator.op->len - 2;
3235 if (strcmp (code, "st") == 0)
3236 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3237 cplus_demangle_type (di));
3240 switch (op->type)
3242 default:
3243 return NULL;
3244 case DEMANGLE_COMPONENT_OPERATOR:
3245 args = op->u.s_operator.op->args;
3246 break;
3247 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3248 args = op->u.s_extended_operator.args;
3249 break;
3250 case DEMANGLE_COMPONENT_CAST:
3251 args = 1;
3252 break;
3255 switch (args)
3257 case 0:
3258 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3260 case 1:
3262 struct demangle_component *operand;
3263 int suffix = 0;
3265 if (code && (code[0] == 'p' || code[0] == 'm')
3266 && code[1] == code[0])
3267 /* pp_ and mm_ are the prefix variants. */
3268 suffix = !d_check_char (di, '_');
3270 if (op->type == DEMANGLE_COMPONENT_CAST
3271 && d_check_char (di, '_'))
3272 operand = d_exprlist (di, 'E');
3273 else
3274 operand = d_expression_1 (di);
3276 if (suffix)
3277 /* Indicate the suffix variant for d_print_comp. */
3278 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3279 d_make_comp (di,
3280 DEMANGLE_COMPONENT_BINARY_ARGS,
3281 operand, operand));
3282 else
3283 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3284 operand);
3286 case 2:
3288 struct demangle_component *left;
3289 struct demangle_component *right;
3291 if (code == NULL)
3292 return NULL;
3293 if (op_is_new_cast (op))
3294 left = cplus_demangle_type (di);
3295 else
3296 left = d_expression_1 (di);
3297 if (!strcmp (code, "cl"))
3298 right = d_exprlist (di, 'E');
3299 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3301 right = d_unqualified_name (di);
3302 if (d_peek_char (di) == 'I')
3303 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3304 right, d_template_args (di));
3306 else
3307 right = d_expression_1 (di);
3309 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3310 d_make_comp (di,
3311 DEMANGLE_COMPONENT_BINARY_ARGS,
3312 left, right));
3314 case 3:
3316 struct demangle_component *first;
3317 struct demangle_component *second;
3318 struct demangle_component *third;
3320 if (code == NULL)
3321 return NULL;
3322 else if (!strcmp (code, "qu"))
3324 /* ?: expression. */
3325 first = d_expression_1 (di);
3326 second = d_expression_1 (di);
3327 third = d_expression_1 (di);
3329 else if (code[0] == 'n')
3331 /* new-expression. */
3332 if (code[1] != 'w' && code[1] != 'a')
3333 return NULL;
3334 first = d_exprlist (di, '_');
3335 second = cplus_demangle_type (di);
3336 if (d_peek_char (di) == 'E')
3338 d_advance (di, 1);
3339 third = NULL;
3341 else if (d_peek_char (di) == 'p'
3342 && d_peek_next_char (di) == 'i')
3344 /* Parenthesized initializer. */
3345 d_advance (di, 2);
3346 third = d_exprlist (di, 'E');
3348 else if (d_peek_char (di) == 'i'
3349 && d_peek_next_char (di) == 'l')
3350 /* initializer-list. */
3351 third = d_expression_1 (di);
3352 else
3353 return NULL;
3355 else
3356 return NULL;
3357 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3358 d_make_comp (di,
3359 DEMANGLE_COMPONENT_TRINARY_ARG1,
3360 first,
3361 d_make_comp (di,
3362 DEMANGLE_COMPONENT_TRINARY_ARG2,
3363 second, third)));
3365 default:
3366 return NULL;
3371 static struct demangle_component *
3372 d_expression (struct d_info *di)
3374 struct demangle_component *ret;
3375 int was_expression = di->is_expression;
3377 di->is_expression = 1;
3378 ret = d_expression_1 (di);
3379 di->is_expression = was_expression;
3380 return ret;
3383 /* <expr-primary> ::= L <type> <(value) number> E
3384 ::= L <type> <(value) float> E
3385 ::= L <mangled-name> E
3388 static struct demangle_component *
3389 d_expr_primary (struct d_info *di)
3391 struct demangle_component *ret;
3393 if (! d_check_char (di, 'L'))
3394 return NULL;
3395 if (d_peek_char (di) == '_'
3396 /* Workaround for G++ bug; see comment in write_template_arg. */
3397 || d_peek_char (di) == 'Z')
3398 ret = cplus_demangle_mangled_name (di, 0);
3399 else
3401 struct demangle_component *type;
3402 enum demangle_component_type t;
3403 const char *s;
3405 type = cplus_demangle_type (di);
3406 if (type == NULL)
3407 return NULL;
3409 /* If we have a type we know how to print, we aren't going to
3410 print the type name itself. */
3411 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3412 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3413 di->expansion -= type->u.s_builtin.type->len;
3415 /* Rather than try to interpret the literal value, we just
3416 collect it as a string. Note that it's possible to have a
3417 floating point literal here. The ABI specifies that the
3418 format of such literals is machine independent. That's fine,
3419 but what's not fine is that versions of g++ up to 3.2 with
3420 -fabi-version=1 used upper case letters in the hex constant,
3421 and dumped out gcc's internal representation. That makes it
3422 hard to tell where the constant ends, and hard to dump the
3423 constant in any readable form anyhow. We don't attempt to
3424 handle these cases. */
3426 t = DEMANGLE_COMPONENT_LITERAL;
3427 if (d_peek_char (di) == 'n')
3429 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3430 d_advance (di, 1);
3432 s = d_str (di);
3433 while (d_peek_char (di) != 'E')
3435 if (d_peek_char (di) == '\0')
3436 return NULL;
3437 d_advance (di, 1);
3439 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3441 if (! d_check_char (di, 'E'))
3442 return NULL;
3443 return ret;
3446 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3447 ::= Z <(function) encoding> E s [<discriminator>]
3448 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3451 static struct demangle_component *
3452 d_local_name (struct d_info *di)
3454 struct demangle_component *function;
3456 if (! d_check_char (di, 'Z'))
3457 return NULL;
3459 function = d_encoding (di, 0);
3461 if (! d_check_char (di, 'E'))
3462 return NULL;
3464 if (d_peek_char (di) == 's')
3466 d_advance (di, 1);
3467 if (! d_discriminator (di))
3468 return NULL;
3469 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3470 d_make_name (di, "string literal",
3471 sizeof "string literal" - 1));
3473 else
3475 struct demangle_component *name;
3476 int num = -1;
3478 if (d_peek_char (di) == 'd')
3480 /* Default argument scope: d <number> _. */
3481 d_advance (di, 1);
3482 num = d_compact_number (di);
3483 if (num < 0)
3484 return NULL;
3487 name = d_name (di);
3488 if (name)
3489 switch (name->type)
3491 /* Lambdas and unnamed types have internal discriminators. */
3492 case DEMANGLE_COMPONENT_LAMBDA:
3493 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3494 break;
3495 default:
3496 if (! d_discriminator (di))
3497 return NULL;
3499 if (num >= 0)
3500 name = d_make_default_arg (di, num, name);
3501 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3505 /* <discriminator> ::= _ <(non-negative) number>
3507 We demangle the discriminator, but we don't print it out. FIXME:
3508 We should print it out in verbose mode. */
3510 static int
3511 d_discriminator (struct d_info *di)
3513 int discrim;
3515 if (d_peek_char (di) != '_')
3516 return 1;
3517 d_advance (di, 1);
3518 discrim = d_number (di);
3519 if (discrim < 0)
3520 return 0;
3521 return 1;
3524 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3526 static struct demangle_component *
3527 d_lambda (struct d_info *di)
3529 struct demangle_component *tl;
3530 struct demangle_component *ret;
3531 int num;
3533 if (! d_check_char (di, 'U'))
3534 return NULL;
3535 if (! d_check_char (di, 'l'))
3536 return NULL;
3538 tl = d_parmlist (di);
3539 if (tl == NULL)
3540 return NULL;
3542 if (! d_check_char (di, 'E'))
3543 return NULL;
3545 num = d_compact_number (di);
3546 if (num < 0)
3547 return NULL;
3549 ret = d_make_empty (di);
3550 if (ret)
3552 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3553 ret->u.s_unary_num.sub = tl;
3554 ret->u.s_unary_num.num = num;
3557 if (! d_add_substitution (di, ret))
3558 return NULL;
3560 return ret;
3563 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3565 static struct demangle_component *
3566 d_unnamed_type (struct d_info *di)
3568 struct demangle_component *ret;
3569 int num;
3571 if (! d_check_char (di, 'U'))
3572 return NULL;
3573 if (! d_check_char (di, 't'))
3574 return NULL;
3576 num = d_compact_number (di);
3577 if (num < 0)
3578 return NULL;
3580 ret = d_make_empty (di);
3581 if (ret)
3583 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3584 ret->u.s_number.number = num;
3587 if (! d_add_substitution (di, ret))
3588 return NULL;
3590 return ret;
3593 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3596 static struct demangle_component *
3597 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3599 const char *suffix = d_str (di);
3600 const char *pend = suffix;
3601 struct demangle_component *n;
3603 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3605 pend += 2;
3606 while (IS_LOWER (*pend) || *pend == '_')
3607 ++pend;
3609 while (*pend == '.' && IS_DIGIT (pend[1]))
3611 pend += 2;
3612 while (IS_DIGIT (*pend))
3613 ++pend;
3615 d_advance (di, pend - suffix);
3616 n = d_make_name (di, suffix, pend - suffix);
3617 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3620 /* Add a new substitution. */
3622 static int
3623 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3625 if (dc == NULL)
3626 return 0;
3627 if (di->next_sub >= di->num_subs)
3628 return 0;
3629 di->subs[di->next_sub] = dc;
3630 ++di->next_sub;
3631 return 1;
3634 /* <substitution> ::= S <seq-id> _
3635 ::= S_
3636 ::= St
3637 ::= Sa
3638 ::= Sb
3639 ::= Ss
3640 ::= Si
3641 ::= So
3642 ::= Sd
3644 If PREFIX is non-zero, then this type is being used as a prefix in
3645 a qualified name. In this case, for the standard substitutions, we
3646 need to check whether we are being used as a prefix for a
3647 constructor or destructor, and return a full template name.
3648 Otherwise we will get something like std::iostream::~iostream()
3649 which does not correspond particularly well to any function which
3650 actually appears in the source.
3653 static const struct d_standard_sub_info standard_subs[] =
3655 { 't', NL ("std"),
3656 NL ("std"),
3657 NULL, 0 },
3658 { 'a', NL ("std::allocator"),
3659 NL ("std::allocator"),
3660 NL ("allocator") },
3661 { 'b', NL ("std::basic_string"),
3662 NL ("std::basic_string"),
3663 NL ("basic_string") },
3664 { 's', NL ("std::string"),
3665 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3666 NL ("basic_string") },
3667 { 'i', NL ("std::istream"),
3668 NL ("std::basic_istream<char, std::char_traits<char> >"),
3669 NL ("basic_istream") },
3670 { 'o', NL ("std::ostream"),
3671 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3672 NL ("basic_ostream") },
3673 { 'd', NL ("std::iostream"),
3674 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3675 NL ("basic_iostream") }
3678 static struct demangle_component *
3679 d_substitution (struct d_info *di, int prefix)
3681 char c;
3683 if (! d_check_char (di, 'S'))
3684 return NULL;
3686 c = d_next_char (di);
3687 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3689 unsigned int id;
3691 id = 0;
3692 if (c != '_')
3696 unsigned int new_id;
3698 if (IS_DIGIT (c))
3699 new_id = id * 36 + c - '0';
3700 else if (IS_UPPER (c))
3701 new_id = id * 36 + c - 'A' + 10;
3702 else
3703 return NULL;
3704 if (new_id < id)
3705 return NULL;
3706 id = new_id;
3707 c = d_next_char (di);
3709 while (c != '_');
3711 ++id;
3714 if (id >= (unsigned int) di->next_sub)
3715 return NULL;
3717 ++di->did_subs;
3719 return di->subs[id];
3721 else
3723 int verbose;
3724 const struct d_standard_sub_info *p;
3725 const struct d_standard_sub_info *pend;
3727 verbose = (di->options & DMGL_VERBOSE) != 0;
3728 if (! verbose && prefix)
3730 char peek;
3732 peek = d_peek_char (di);
3733 if (peek == 'C' || peek == 'D')
3734 verbose = 1;
3737 pend = (&standard_subs[0]
3738 + sizeof standard_subs / sizeof standard_subs[0]);
3739 for (p = &standard_subs[0]; p < pend; ++p)
3741 if (c == p->code)
3743 const char *s;
3744 int len;
3745 struct demangle_component *c;
3747 if (p->set_last_name != NULL)
3748 di->last_name = d_make_sub (di, p->set_last_name,
3749 p->set_last_name_len);
3750 if (verbose)
3752 s = p->full_expansion;
3753 len = p->full_len;
3755 else
3757 s = p->simple_expansion;
3758 len = p->simple_len;
3760 di->expansion += len;
3761 c = d_make_sub (di, s, len);
3762 if (d_peek_char (di) == 'B')
3764 /* If there are ABI tags on the abbreviation, it becomes
3765 a substitution candidate. */
3766 c = d_abi_tags (di, c);
3767 d_add_substitution (di, c);
3769 return c;
3773 return NULL;
3777 static void
3778 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3780 checkpoint->n = di->n;
3781 checkpoint->next_comp = di->next_comp;
3782 checkpoint->next_sub = di->next_sub;
3783 checkpoint->did_subs = di->did_subs;
3784 checkpoint->expansion = di->expansion;
3787 static void
3788 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3790 di->n = checkpoint->n;
3791 di->next_comp = checkpoint->next_comp;
3792 di->next_sub = checkpoint->next_sub;
3793 di->did_subs = checkpoint->did_subs;
3794 di->expansion = checkpoint->expansion;
3797 /* Initialize a growable string. */
3799 static void
3800 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3802 dgs->buf = NULL;
3803 dgs->len = 0;
3804 dgs->alc = 0;
3805 dgs->allocation_failure = 0;
3807 if (estimate > 0)
3808 d_growable_string_resize (dgs, estimate);
3811 /* Grow a growable string to a given size. */
3813 static inline void
3814 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3816 size_t newalc;
3817 char *newbuf;
3819 if (dgs->allocation_failure)
3820 return;
3822 /* Start allocation at two bytes to avoid any possibility of confusion
3823 with the special value of 1 used as a return in *palc to indicate
3824 allocation failures. */
3825 newalc = dgs->alc > 0 ? dgs->alc : 2;
3826 while (newalc < need)
3827 newalc <<= 1;
3829 newbuf = (char *) realloc (dgs->buf, newalc);
3830 if (newbuf == NULL)
3832 free (dgs->buf);
3833 dgs->buf = NULL;
3834 dgs->len = 0;
3835 dgs->alc = 0;
3836 dgs->allocation_failure = 1;
3837 return;
3839 dgs->buf = newbuf;
3840 dgs->alc = newalc;
3843 /* Append a buffer to a growable string. */
3845 static inline void
3846 d_growable_string_append_buffer (struct d_growable_string *dgs,
3847 const char *s, size_t l)
3849 size_t need;
3851 need = dgs->len + l + 1;
3852 if (need > dgs->alc)
3853 d_growable_string_resize (dgs, need);
3855 if (dgs->allocation_failure)
3856 return;
3858 memcpy (dgs->buf + dgs->len, s, l);
3859 dgs->buf[dgs->len + l] = '\0';
3860 dgs->len += l;
3863 /* Bridge growable strings to the callback mechanism. */
3865 static void
3866 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3868 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3870 d_growable_string_append_buffer (dgs, s, l);
3873 /* Walk the tree, counting the number of templates encountered, and
3874 the number of times a scope might be saved. These counts will be
3875 used to allocate data structures for d_print_comp, so the logic
3876 here must mirror the logic d_print_comp will use. It is not
3877 important that the resulting numbers are exact, so long as they
3878 are larger than the actual numbers encountered. */
3880 static void
3881 d_count_templates_scopes (int *num_templates, int *num_scopes,
3882 const struct demangle_component *dc)
3884 if (dc == NULL)
3885 return;
3887 switch (dc->type)
3889 case DEMANGLE_COMPONENT_NAME:
3890 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3891 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3892 case DEMANGLE_COMPONENT_SUB_STD:
3893 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3894 case DEMANGLE_COMPONENT_OPERATOR:
3895 case DEMANGLE_COMPONENT_CHARACTER:
3896 case DEMANGLE_COMPONENT_NUMBER:
3897 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3898 break;
3900 case DEMANGLE_COMPONENT_TEMPLATE:
3901 (*num_templates)++;
3902 goto recurse_left_right;
3904 case DEMANGLE_COMPONENT_REFERENCE:
3905 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3906 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
3907 (*num_scopes)++;
3908 goto recurse_left_right;
3910 case DEMANGLE_COMPONENT_QUAL_NAME:
3911 case DEMANGLE_COMPONENT_LOCAL_NAME:
3912 case DEMANGLE_COMPONENT_TYPED_NAME:
3913 case DEMANGLE_COMPONENT_VTABLE:
3914 case DEMANGLE_COMPONENT_VTT:
3915 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3916 case DEMANGLE_COMPONENT_TYPEINFO:
3917 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3918 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3919 case DEMANGLE_COMPONENT_THUNK:
3920 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3921 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3922 case DEMANGLE_COMPONENT_JAVA_CLASS:
3923 case DEMANGLE_COMPONENT_GUARD:
3924 case DEMANGLE_COMPONENT_TLS_INIT:
3925 case DEMANGLE_COMPONENT_TLS_WRAPPER:
3926 case DEMANGLE_COMPONENT_REFTEMP:
3927 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3928 case DEMANGLE_COMPONENT_RESTRICT:
3929 case DEMANGLE_COMPONENT_VOLATILE:
3930 case DEMANGLE_COMPONENT_CONST:
3931 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3932 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3933 case DEMANGLE_COMPONENT_CONST_THIS:
3934 case DEMANGLE_COMPONENT_REFERENCE_THIS:
3935 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
3936 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
3937 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3938 case DEMANGLE_COMPONENT_POINTER:
3939 case DEMANGLE_COMPONENT_COMPLEX:
3940 case DEMANGLE_COMPONENT_IMAGINARY:
3941 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3942 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3943 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3944 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3945 case DEMANGLE_COMPONENT_VECTOR_TYPE:
3946 case DEMANGLE_COMPONENT_ARGLIST:
3947 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3948 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
3949 case DEMANGLE_COMPONENT_CAST:
3950 case DEMANGLE_COMPONENT_CONVERSION:
3951 case DEMANGLE_COMPONENT_NULLARY:
3952 case DEMANGLE_COMPONENT_UNARY:
3953 case DEMANGLE_COMPONENT_BINARY:
3954 case DEMANGLE_COMPONENT_BINARY_ARGS:
3955 case DEMANGLE_COMPONENT_TRINARY:
3956 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3957 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3958 case DEMANGLE_COMPONENT_LITERAL:
3959 case DEMANGLE_COMPONENT_LITERAL_NEG:
3960 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3961 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3962 case DEMANGLE_COMPONENT_DECLTYPE:
3963 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
3964 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
3965 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3966 case DEMANGLE_COMPONENT_TAGGED_NAME:
3967 case DEMANGLE_COMPONENT_CLONE:
3968 recurse_left_right:
3969 d_count_templates_scopes (num_templates, num_scopes,
3970 d_left (dc));
3971 d_count_templates_scopes (num_templates, num_scopes,
3972 d_right (dc));
3973 break;
3975 case DEMANGLE_COMPONENT_CTOR:
3976 d_count_templates_scopes (num_templates, num_scopes,
3977 dc->u.s_ctor.name);
3978 break;
3980 case DEMANGLE_COMPONENT_DTOR:
3981 d_count_templates_scopes (num_templates, num_scopes,
3982 dc->u.s_dtor.name);
3983 break;
3985 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3986 d_count_templates_scopes (num_templates, num_scopes,
3987 dc->u.s_extended_operator.name);
3988 break;
3990 case DEMANGLE_COMPONENT_FIXED_TYPE:
3991 d_count_templates_scopes (num_templates, num_scopes,
3992 dc->u.s_fixed.length);
3993 break;
3995 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
3996 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
3997 d_count_templates_scopes (num_templates, num_scopes,
3998 d_left (dc));
3999 break;
4001 case DEMANGLE_COMPONENT_LAMBDA:
4002 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4003 d_count_templates_scopes (num_templates, num_scopes,
4004 dc->u.s_unary_num.sub);
4005 break;
4009 /* Initialize a print information structure. */
4011 static void
4012 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4013 void *opaque, const struct demangle_component *dc)
4015 dpi->len = 0;
4016 dpi->last_char = '\0';
4017 dpi->templates = NULL;
4018 dpi->modifiers = NULL;
4019 dpi->pack_index = 0;
4020 dpi->flush_count = 0;
4022 dpi->callback = callback;
4023 dpi->opaque = opaque;
4025 dpi->demangle_failure = 0;
4027 dpi->component_stack = NULL;
4029 dpi->saved_scopes = NULL;
4030 dpi->next_saved_scope = 0;
4031 dpi->num_saved_scopes = 0;
4033 dpi->copy_templates = NULL;
4034 dpi->next_copy_template = 0;
4035 dpi->num_copy_templates = 0;
4037 d_count_templates_scopes (&dpi->num_copy_templates,
4038 &dpi->num_saved_scopes, dc);
4039 dpi->num_copy_templates *= dpi->num_saved_scopes;
4041 dpi->current_template = NULL;
4044 /* Indicate that an error occurred during printing, and test for error. */
4046 static inline void
4047 d_print_error (struct d_print_info *dpi)
4049 dpi->demangle_failure = 1;
4052 static inline int
4053 d_print_saw_error (struct d_print_info *dpi)
4055 return dpi->demangle_failure != 0;
4058 /* Flush buffered characters to the callback. */
4060 static inline void
4061 d_print_flush (struct d_print_info *dpi)
4063 dpi->buf[dpi->len] = '\0';
4064 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4065 dpi->len = 0;
4066 dpi->flush_count++;
4069 /* Append characters and buffers for printing. */
4071 static inline void
4072 d_append_char (struct d_print_info *dpi, char c)
4074 if (dpi->len == sizeof (dpi->buf) - 1)
4075 d_print_flush (dpi);
4077 dpi->buf[dpi->len++] = c;
4078 dpi->last_char = c;
4081 static inline void
4082 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4084 size_t i;
4086 for (i = 0; i < l; i++)
4087 d_append_char (dpi, s[i]);
4090 static inline void
4091 d_append_string (struct d_print_info *dpi, const char *s)
4093 d_append_buffer (dpi, s, strlen (s));
4096 static inline void
4097 d_append_num (struct d_print_info *dpi, int l)
4099 char buf[25];
4100 sprintf (buf,"%d", l);
4101 d_append_string (dpi, buf);
4104 static inline char
4105 d_last_char (struct d_print_info *dpi)
4107 return dpi->last_char;
4110 /* Turn components into a human readable string. OPTIONS is the
4111 options bits passed to the demangler. DC is the tree to print.
4112 CALLBACK is a function to call to flush demangled string segments
4113 as they fill the intermediate buffer, and OPAQUE is a generalized
4114 callback argument. On success, this returns 1. On failure,
4115 it returns 0, indicating a bad parse. It does not use heap
4116 memory to build an output string, so cannot encounter memory
4117 allocation failure. */
4119 CP_STATIC_IF_GLIBCPP_V3
4121 cplus_demangle_print_callback (int options,
4122 const struct demangle_component *dc,
4123 demangle_callbackref callback, void *opaque)
4125 struct d_print_info dpi;
4127 d_print_init (&dpi, callback, opaque, dc);
4130 #ifdef CP_DYNAMIC_ARRAYS
4131 __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
4132 __extension__ struct d_print_template temps[dpi.num_copy_templates];
4134 dpi.saved_scopes = scopes;
4135 dpi.copy_templates = temps;
4136 #else
4137 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4138 * sizeof (*dpi.saved_scopes));
4139 dpi.copy_templates = alloca (dpi.num_copy_templates
4140 * sizeof (*dpi.copy_templates));
4141 #endif
4143 d_print_comp (&dpi, options, dc);
4146 d_print_flush (&dpi);
4148 return ! d_print_saw_error (&dpi);
4151 /* Turn components into a human readable string. OPTIONS is the
4152 options bits passed to the demangler. DC is the tree to print.
4153 ESTIMATE is a guess at the length of the result. This returns a
4154 string allocated by malloc, or NULL on error. On success, this
4155 sets *PALC to the size of the allocated buffer. On failure, this
4156 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4157 failure. */
4159 CP_STATIC_IF_GLIBCPP_V3
4160 char *
4161 cplus_demangle_print (int options, const struct demangle_component *dc,
4162 int estimate, size_t *palc)
4164 struct d_growable_string dgs;
4166 d_growable_string_init (&dgs, estimate);
4168 if (! cplus_demangle_print_callback (options, dc,
4169 d_growable_string_callback_adapter,
4170 &dgs))
4172 free (dgs.buf);
4173 *palc = 0;
4174 return NULL;
4177 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4178 return dgs.buf;
4181 /* Returns the I'th element of the template arglist ARGS, or NULL on
4182 failure. */
4184 static struct demangle_component *
4185 d_index_template_argument (struct demangle_component *args, int i)
4187 struct demangle_component *a;
4189 for (a = args;
4190 a != NULL;
4191 a = d_right (a))
4193 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4194 return NULL;
4195 if (i <= 0)
4196 break;
4197 --i;
4199 if (i != 0 || a == NULL)
4200 return NULL;
4202 return d_left (a);
4205 /* Returns the template argument from the current context indicated by DC,
4206 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4208 static struct demangle_component *
4209 d_lookup_template_argument (struct d_print_info *dpi,
4210 const struct demangle_component *dc)
4212 if (dpi->templates == NULL)
4214 d_print_error (dpi);
4215 return NULL;
4218 return d_index_template_argument
4219 (d_right (dpi->templates->template_decl),
4220 dc->u.s_number.number);
4223 /* Returns a template argument pack used in DC (any will do), or NULL. */
4225 static struct demangle_component *
4226 d_find_pack (struct d_print_info *dpi,
4227 const struct demangle_component *dc)
4229 struct demangle_component *a;
4230 if (dc == NULL)
4231 return NULL;
4233 switch (dc->type)
4235 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4236 a = d_lookup_template_argument (dpi, dc);
4237 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4238 return a;
4239 return NULL;
4241 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4242 return NULL;
4244 case DEMANGLE_COMPONENT_LAMBDA:
4245 case DEMANGLE_COMPONENT_NAME:
4246 case DEMANGLE_COMPONENT_TAGGED_NAME:
4247 case DEMANGLE_COMPONENT_OPERATOR:
4248 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4249 case DEMANGLE_COMPONENT_SUB_STD:
4250 case DEMANGLE_COMPONENT_CHARACTER:
4251 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4252 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4253 case DEMANGLE_COMPONENT_FIXED_TYPE:
4254 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4255 case DEMANGLE_COMPONENT_NUMBER:
4256 return NULL;
4258 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4259 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4260 case DEMANGLE_COMPONENT_CTOR:
4261 return d_find_pack (dpi, dc->u.s_ctor.name);
4262 case DEMANGLE_COMPONENT_DTOR:
4263 return d_find_pack (dpi, dc->u.s_dtor.name);
4265 default:
4266 a = d_find_pack (dpi, d_left (dc));
4267 if (a)
4268 return a;
4269 return d_find_pack (dpi, d_right (dc));
4273 /* Returns the length of the template argument pack DC. */
4275 static int
4276 d_pack_length (const struct demangle_component *dc)
4278 int count = 0;
4279 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4280 && d_left (dc) != NULL)
4282 ++count;
4283 dc = d_right (dc);
4285 return count;
4288 /* DC is a component of a mangled expression. Print it, wrapped in parens
4289 if needed. */
4291 static void
4292 d_print_subexpr (struct d_print_info *dpi, int options,
4293 const struct demangle_component *dc)
4295 int simple = 0;
4296 if (dc->type == DEMANGLE_COMPONENT_NAME
4297 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4298 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4299 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4300 simple = 1;
4301 if (!simple)
4302 d_append_char (dpi, '(');
4303 d_print_comp (dpi, options, dc);
4304 if (!simple)
4305 d_append_char (dpi, ')');
4308 /* Save the current scope. */
4310 static void
4311 d_save_scope (struct d_print_info *dpi,
4312 const struct demangle_component *container)
4314 struct d_saved_scope *scope;
4315 struct d_print_template *src, **link;
4317 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4319 d_print_error (dpi);
4320 return;
4322 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4323 dpi->next_saved_scope++;
4325 scope->container = container;
4326 link = &scope->templates;
4328 for (src = dpi->templates; src != NULL; src = src->next)
4330 struct d_print_template *dst;
4332 if (dpi->next_copy_template >= dpi->num_copy_templates)
4334 d_print_error (dpi);
4335 return;
4337 dst = &dpi->copy_templates[dpi->next_copy_template];
4338 dpi->next_copy_template++;
4340 dst->template_decl = src->template_decl;
4341 *link = dst;
4342 link = &dst->next;
4345 *link = NULL;
4348 /* Attempt to locate a previously saved scope. Returns NULL if no
4349 corresponding saved scope was found. */
4351 static struct d_saved_scope *
4352 d_get_saved_scope (struct d_print_info *dpi,
4353 const struct demangle_component *container)
4355 int i;
4357 for (i = 0; i < dpi->next_saved_scope; i++)
4358 if (dpi->saved_scopes[i].container == container)
4359 return &dpi->saved_scopes[i];
4361 return NULL;
4364 /* Subroutine to handle components. */
4366 static void
4367 d_print_comp_inner (struct d_print_info *dpi, int options,
4368 const struct demangle_component *dc)
4370 /* Magic variable to let reference smashing skip over the next modifier
4371 without needing to modify *dc. */
4372 const struct demangle_component *mod_inner = NULL;
4374 /* Variable used to store the current templates while a previously
4375 captured scope is used. */
4376 struct d_print_template *saved_templates;
4378 /* Nonzero if templates have been stored in the above variable. */
4379 int need_template_restore = 0;
4381 if (dc == NULL)
4383 d_print_error (dpi);
4384 return;
4386 if (d_print_saw_error (dpi))
4387 return;
4389 switch (dc->type)
4391 case DEMANGLE_COMPONENT_NAME:
4392 if ((options & DMGL_JAVA) == 0)
4393 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4394 else
4395 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4396 return;
4398 case DEMANGLE_COMPONENT_TAGGED_NAME:
4399 d_print_comp (dpi, options, d_left (dc));
4400 d_append_string (dpi, "[abi:");
4401 d_print_comp (dpi, options, d_right (dc));
4402 d_append_char (dpi, ']');
4403 return;
4405 case DEMANGLE_COMPONENT_QUAL_NAME:
4406 case DEMANGLE_COMPONENT_LOCAL_NAME:
4407 d_print_comp (dpi, options, d_left (dc));
4408 if ((options & DMGL_JAVA) == 0)
4409 d_append_string (dpi, "::");
4410 else
4411 d_append_char (dpi, '.');
4413 struct demangle_component *local_name = d_right (dc);
4414 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4416 d_append_string (dpi, "{default arg#");
4417 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4418 d_append_string (dpi, "}::");
4419 local_name = local_name->u.s_unary_num.sub;
4421 d_print_comp (dpi, options, local_name);
4423 return;
4425 case DEMANGLE_COMPONENT_TYPED_NAME:
4427 struct d_print_mod *hold_modifiers;
4428 struct demangle_component *typed_name;
4429 struct d_print_mod adpm[4];
4430 unsigned int i;
4431 struct d_print_template dpt;
4433 /* Pass the name down to the type so that it can be printed in
4434 the right place for the type. We also have to pass down
4435 any CV-qualifiers, which apply to the this parameter. */
4436 hold_modifiers = dpi->modifiers;
4437 dpi->modifiers = 0;
4438 i = 0;
4439 typed_name = d_left (dc);
4440 while (typed_name != NULL)
4442 if (i >= sizeof adpm / sizeof adpm[0])
4444 d_print_error (dpi);
4445 return;
4448 adpm[i].next = dpi->modifiers;
4449 dpi->modifiers = &adpm[i];
4450 adpm[i].mod = typed_name;
4451 adpm[i].printed = 0;
4452 adpm[i].templates = dpi->templates;
4453 ++i;
4455 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4456 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4457 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4458 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4459 && typed_name->type != DEMANGLE_COMPONENT_TRANSACTION_SAFE
4460 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4461 break;
4463 typed_name = d_left (typed_name);
4466 if (typed_name == NULL)
4468 d_print_error (dpi);
4469 return;
4472 /* If typed_name is a template, then it applies to the
4473 function type as well. */
4474 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4476 dpt.next = dpi->templates;
4477 dpi->templates = &dpt;
4478 dpt.template_decl = typed_name;
4481 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4482 there may be CV-qualifiers on its right argument which
4483 really apply here; this happens when parsing a class which
4484 is local to a function. */
4485 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4487 struct demangle_component *local_name;
4489 local_name = d_right (typed_name);
4490 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4491 local_name = local_name->u.s_unary_num.sub;
4492 if (local_name == NULL)
4494 d_print_error (dpi);
4495 return;
4497 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4498 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4499 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4500 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4501 || local_name->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
4502 || (local_name->type
4503 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4505 if (i >= sizeof adpm / sizeof adpm[0])
4507 d_print_error (dpi);
4508 return;
4511 adpm[i] = adpm[i - 1];
4512 adpm[i].next = &adpm[i - 1];
4513 dpi->modifiers = &adpm[i];
4515 adpm[i - 1].mod = local_name;
4516 adpm[i - 1].printed = 0;
4517 adpm[i - 1].templates = dpi->templates;
4518 ++i;
4520 local_name = d_left (local_name);
4524 d_print_comp (dpi, options, d_right (dc));
4526 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4527 dpi->templates = dpt.next;
4529 /* If the modifiers didn't get printed by the type, print them
4530 now. */
4531 while (i > 0)
4533 --i;
4534 if (! adpm[i].printed)
4536 d_append_char (dpi, ' ');
4537 d_print_mod (dpi, options, adpm[i].mod);
4541 dpi->modifiers = hold_modifiers;
4543 return;
4546 case DEMANGLE_COMPONENT_TEMPLATE:
4548 struct d_print_mod *hold_dpm;
4549 struct demangle_component *dcl;
4550 const struct demangle_component *hold_current;
4552 /* This template may need to be referenced by a cast operator
4553 contained in its subtree. */
4554 hold_current = dpi->current_template;
4555 dpi->current_template = dc;
4557 /* Don't push modifiers into a template definition. Doing so
4558 could give the wrong definition for a template argument.
4559 Instead, treat the template essentially as a name. */
4561 hold_dpm = dpi->modifiers;
4562 dpi->modifiers = NULL;
4564 dcl = d_left (dc);
4566 if ((options & DMGL_JAVA) != 0
4567 && dcl->type == DEMANGLE_COMPONENT_NAME
4568 && dcl->u.s_name.len == 6
4569 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4571 /* Special-case Java arrays, so that JArray<TYPE> appears
4572 instead as TYPE[]. */
4574 d_print_comp (dpi, options, d_right (dc));
4575 d_append_string (dpi, "[]");
4577 else
4579 d_print_comp (dpi, options, dcl);
4580 if (d_last_char (dpi) == '<')
4581 d_append_char (dpi, ' ');
4582 d_append_char (dpi, '<');
4583 d_print_comp (dpi, options, d_right (dc));
4584 /* Avoid generating two consecutive '>' characters, to avoid
4585 the C++ syntactic ambiguity. */
4586 if (d_last_char (dpi) == '>')
4587 d_append_char (dpi, ' ');
4588 d_append_char (dpi, '>');
4591 dpi->modifiers = hold_dpm;
4592 dpi->current_template = hold_current;
4594 return;
4597 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4599 struct d_print_template *hold_dpt;
4600 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4602 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4603 a = d_index_template_argument (a, dpi->pack_index);
4605 if (a == NULL)
4607 d_print_error (dpi);
4608 return;
4611 /* While processing this parameter, we need to pop the list of
4612 templates. This is because the template parameter may
4613 itself be a reference to a parameter of an outer
4614 template. */
4616 hold_dpt = dpi->templates;
4617 dpi->templates = hold_dpt->next;
4619 d_print_comp (dpi, options, a);
4621 dpi->templates = hold_dpt;
4623 return;
4626 case DEMANGLE_COMPONENT_CTOR:
4627 d_print_comp (dpi, options, dc->u.s_ctor.name);
4628 return;
4630 case DEMANGLE_COMPONENT_DTOR:
4631 d_append_char (dpi, '~');
4632 d_print_comp (dpi, options, dc->u.s_dtor.name);
4633 return;
4635 case DEMANGLE_COMPONENT_VTABLE:
4636 d_append_string (dpi, "vtable for ");
4637 d_print_comp (dpi, options, d_left (dc));
4638 return;
4640 case DEMANGLE_COMPONENT_VTT:
4641 d_append_string (dpi, "VTT for ");
4642 d_print_comp (dpi, options, d_left (dc));
4643 return;
4645 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4646 d_append_string (dpi, "construction vtable for ");
4647 d_print_comp (dpi, options, d_left (dc));
4648 d_append_string (dpi, "-in-");
4649 d_print_comp (dpi, options, d_right (dc));
4650 return;
4652 case DEMANGLE_COMPONENT_TYPEINFO:
4653 d_append_string (dpi, "typeinfo for ");
4654 d_print_comp (dpi, options, d_left (dc));
4655 return;
4657 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4658 d_append_string (dpi, "typeinfo name for ");
4659 d_print_comp (dpi, options, d_left (dc));
4660 return;
4662 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4663 d_append_string (dpi, "typeinfo fn for ");
4664 d_print_comp (dpi, options, d_left (dc));
4665 return;
4667 case DEMANGLE_COMPONENT_THUNK:
4668 d_append_string (dpi, "non-virtual thunk to ");
4669 d_print_comp (dpi, options, d_left (dc));
4670 return;
4672 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4673 d_append_string (dpi, "virtual thunk to ");
4674 d_print_comp (dpi, options, d_left (dc));
4675 return;
4677 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4678 d_append_string (dpi, "covariant return thunk to ");
4679 d_print_comp (dpi, options, d_left (dc));
4680 return;
4682 case DEMANGLE_COMPONENT_JAVA_CLASS:
4683 d_append_string (dpi, "java Class for ");
4684 d_print_comp (dpi, options, d_left (dc));
4685 return;
4687 case DEMANGLE_COMPONENT_GUARD:
4688 d_append_string (dpi, "guard variable for ");
4689 d_print_comp (dpi, options, d_left (dc));
4690 return;
4692 case DEMANGLE_COMPONENT_TLS_INIT:
4693 d_append_string (dpi, "TLS init function for ");
4694 d_print_comp (dpi, options, d_left (dc));
4695 return;
4697 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4698 d_append_string (dpi, "TLS wrapper function for ");
4699 d_print_comp (dpi, options, d_left (dc));
4700 return;
4702 case DEMANGLE_COMPONENT_REFTEMP:
4703 d_append_string (dpi, "reference temporary #");
4704 d_print_comp (dpi, options, d_right (dc));
4705 d_append_string (dpi, " for ");
4706 d_print_comp (dpi, options, d_left (dc));
4707 return;
4709 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4710 d_append_string (dpi, "hidden alias for ");
4711 d_print_comp (dpi, options, d_left (dc));
4712 return;
4714 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4715 d_append_string (dpi, "transaction clone for ");
4716 d_print_comp (dpi, options, d_left (dc));
4717 return;
4719 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4720 d_append_string (dpi, "non-transaction clone for ");
4721 d_print_comp (dpi, options, d_left (dc));
4722 return;
4724 case DEMANGLE_COMPONENT_SUB_STD:
4725 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4726 return;
4728 case DEMANGLE_COMPONENT_RESTRICT:
4729 case DEMANGLE_COMPONENT_VOLATILE:
4730 case DEMANGLE_COMPONENT_CONST:
4732 struct d_print_mod *pdpm;
4734 /* When printing arrays, it's possible to have cases where the
4735 same CV-qualifier gets pushed on the stack multiple times.
4736 We only need to print it once. */
4738 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4740 if (! pdpm->printed)
4742 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4743 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4744 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4745 break;
4746 if (pdpm->mod->type == dc->type)
4748 d_print_comp (dpi, options, d_left (dc));
4749 return;
4754 goto modifier;
4756 case DEMANGLE_COMPONENT_REFERENCE:
4757 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4759 /* Handle reference smashing: & + && = &. */
4760 const struct demangle_component *sub = d_left (dc);
4761 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4763 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4764 struct demangle_component *a;
4766 if (scope == NULL)
4768 /* This is the first time SUB has been traversed.
4769 We need to capture the current templates so
4770 they can be restored if SUB is reentered as a
4771 substitution. */
4772 d_save_scope (dpi, sub);
4773 if (d_print_saw_error (dpi))
4774 return;
4776 else
4778 const struct d_component_stack *dcse;
4779 int found_self_or_parent = 0;
4781 /* This traversal is reentering SUB as a substition.
4782 If we are not beneath SUB or DC in the tree then we
4783 need to restore SUB's template stack temporarily. */
4784 for (dcse = dpi->component_stack; dcse != NULL;
4785 dcse = dcse->parent)
4787 if (dcse->dc == sub
4788 || (dcse->dc == dc
4789 && dcse != dpi->component_stack))
4791 found_self_or_parent = 1;
4792 break;
4796 if (!found_self_or_parent)
4798 saved_templates = dpi->templates;
4799 dpi->templates = scope->templates;
4800 need_template_restore = 1;
4804 a = d_lookup_template_argument (dpi, sub);
4805 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4806 a = d_index_template_argument (a, dpi->pack_index);
4808 if (a == NULL)
4810 if (need_template_restore)
4811 dpi->templates = saved_templates;
4813 d_print_error (dpi);
4814 return;
4817 sub = a;
4820 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4821 || sub->type == dc->type)
4822 dc = sub;
4823 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4824 mod_inner = d_left (sub);
4826 /* Fall through. */
4828 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4829 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4830 case DEMANGLE_COMPONENT_CONST_THIS:
4831 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4832 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4833 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4834 case DEMANGLE_COMPONENT_POINTER:
4835 case DEMANGLE_COMPONENT_COMPLEX:
4836 case DEMANGLE_COMPONENT_IMAGINARY:
4837 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4838 modifier:
4840 /* We keep a list of modifiers on the stack. */
4841 struct d_print_mod dpm;
4843 dpm.next = dpi->modifiers;
4844 dpi->modifiers = &dpm;
4845 dpm.mod = dc;
4846 dpm.printed = 0;
4847 dpm.templates = dpi->templates;
4849 if (!mod_inner)
4850 mod_inner = d_left (dc);
4852 d_print_comp (dpi, options, mod_inner);
4854 /* If the modifier didn't get printed by the type, print it
4855 now. */
4856 if (! dpm.printed)
4857 d_print_mod (dpi, options, dc);
4859 dpi->modifiers = dpm.next;
4861 if (need_template_restore)
4862 dpi->templates = saved_templates;
4864 return;
4867 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4868 if ((options & DMGL_JAVA) == 0)
4869 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4870 dc->u.s_builtin.type->len);
4871 else
4872 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4873 dc->u.s_builtin.type->java_len);
4874 return;
4876 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4877 d_print_comp (dpi, options, d_left (dc));
4878 return;
4880 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4882 if ((options & DMGL_RET_POSTFIX) != 0)
4883 d_print_function_type (dpi,
4884 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4885 dc, dpi->modifiers);
4887 /* Print return type if present */
4888 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4889 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4890 d_left (dc));
4891 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4893 struct d_print_mod dpm;
4895 /* We must pass this type down as a modifier in order to
4896 print it in the right location. */
4897 dpm.next = dpi->modifiers;
4898 dpi->modifiers = &dpm;
4899 dpm.mod = dc;
4900 dpm.printed = 0;
4901 dpm.templates = dpi->templates;
4903 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4904 d_left (dc));
4906 dpi->modifiers = dpm.next;
4908 if (dpm.printed)
4909 return;
4911 /* In standard prefix notation, there is a space between the
4912 return type and the function signature. */
4913 if ((options & DMGL_RET_POSTFIX) == 0)
4914 d_append_char (dpi, ' ');
4917 if ((options & DMGL_RET_POSTFIX) == 0)
4918 d_print_function_type (dpi,
4919 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4920 dc, dpi->modifiers);
4922 return;
4925 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4927 struct d_print_mod *hold_modifiers;
4928 struct d_print_mod adpm[4];
4929 unsigned int i;
4930 struct d_print_mod *pdpm;
4932 /* We must pass this type down as a modifier in order to print
4933 multi-dimensional arrays correctly. If the array itself is
4934 CV-qualified, we act as though the element type were
4935 CV-qualified. We do this by copying the modifiers down
4936 rather than fiddling pointers, so that we don't wind up
4937 with a d_print_mod higher on the stack pointing into our
4938 stack frame after we return. */
4940 hold_modifiers = dpi->modifiers;
4942 adpm[0].next = hold_modifiers;
4943 dpi->modifiers = &adpm[0];
4944 adpm[0].mod = dc;
4945 adpm[0].printed = 0;
4946 adpm[0].templates = dpi->templates;
4948 i = 1;
4949 pdpm = hold_modifiers;
4950 while (pdpm != NULL
4951 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4952 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4953 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4955 if (! pdpm->printed)
4957 if (i >= sizeof adpm / sizeof adpm[0])
4959 d_print_error (dpi);
4960 return;
4963 adpm[i] = *pdpm;
4964 adpm[i].next = dpi->modifiers;
4965 dpi->modifiers = &adpm[i];
4966 pdpm->printed = 1;
4967 ++i;
4970 pdpm = pdpm->next;
4973 d_print_comp (dpi, options, d_right (dc));
4975 dpi->modifiers = hold_modifiers;
4977 if (adpm[0].printed)
4978 return;
4980 while (i > 1)
4982 --i;
4983 d_print_mod (dpi, options, adpm[i].mod);
4986 d_print_array_type (dpi, options, dc, dpi->modifiers);
4988 return;
4991 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4992 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4994 struct d_print_mod dpm;
4996 dpm.next = dpi->modifiers;
4997 dpi->modifiers = &dpm;
4998 dpm.mod = dc;
4999 dpm.printed = 0;
5000 dpm.templates = dpi->templates;
5002 d_print_comp (dpi, options, d_right (dc));
5004 /* If the modifier didn't get printed by the type, print it
5005 now. */
5006 if (! dpm.printed)
5007 d_print_mod (dpi, options, dc);
5009 dpi->modifiers = dpm.next;
5011 return;
5014 case DEMANGLE_COMPONENT_FIXED_TYPE:
5015 if (dc->u.s_fixed.sat)
5016 d_append_string (dpi, "_Sat ");
5017 /* Don't print "int _Accum". */
5018 if (dc->u.s_fixed.length->u.s_builtin.type
5019 != &cplus_demangle_builtin_types['i'-'a'])
5021 d_print_comp (dpi, options, dc->u.s_fixed.length);
5022 d_append_char (dpi, ' ');
5024 if (dc->u.s_fixed.accum)
5025 d_append_string (dpi, "_Accum");
5026 else
5027 d_append_string (dpi, "_Fract");
5028 return;
5030 case DEMANGLE_COMPONENT_ARGLIST:
5031 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5032 if (d_left (dc) != NULL)
5033 d_print_comp (dpi, options, d_left (dc));
5034 if (d_right (dc) != NULL)
5036 size_t len;
5037 unsigned long int flush_count;
5038 /* Make sure ", " isn't flushed by d_append_string, otherwise
5039 dpi->len -= 2 wouldn't work. */
5040 if (dpi->len >= sizeof (dpi->buf) - 2)
5041 d_print_flush (dpi);
5042 d_append_string (dpi, ", ");
5043 len = dpi->len;
5044 flush_count = dpi->flush_count;
5045 d_print_comp (dpi, options, d_right (dc));
5046 /* If that didn't print anything (which can happen with empty
5047 template argument packs), remove the comma and space. */
5048 if (dpi->flush_count == flush_count && dpi->len == len)
5049 dpi->len -= 2;
5051 return;
5053 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5055 struct demangle_component *type = d_left (dc);
5056 struct demangle_component *list = d_right (dc);
5058 if (type)
5059 d_print_comp (dpi, options, type);
5060 d_append_char (dpi, '{');
5061 d_print_comp (dpi, options, list);
5062 d_append_char (dpi, '}');
5064 return;
5066 case DEMANGLE_COMPONENT_OPERATOR:
5068 const struct demangle_operator_info *op = dc->u.s_operator.op;
5069 int len = op->len;
5071 d_append_string (dpi, "operator");
5072 /* Add a space before new/delete. */
5073 if (IS_LOWER (op->name[0]))
5074 d_append_char (dpi, ' ');
5075 /* Omit a trailing space. */
5076 if (op->name[len-1] == ' ')
5077 --len;
5078 d_append_buffer (dpi, op->name, len);
5079 return;
5082 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5083 d_append_string (dpi, "operator ");
5084 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5085 return;
5087 case DEMANGLE_COMPONENT_CONVERSION:
5088 d_append_string (dpi, "operator ");
5089 d_print_conversion (dpi, options, dc);
5090 return;
5092 case DEMANGLE_COMPONENT_NULLARY:
5093 d_print_expr_op (dpi, options, d_left (dc));
5094 return;
5096 case DEMANGLE_COMPONENT_UNARY:
5098 struct demangle_component *op = d_left (dc);
5099 struct demangle_component *operand = d_right (dc);
5100 const char *code = NULL;
5102 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5104 code = op->u.s_operator.op->code;
5105 if (!strcmp (code, "ad"))
5107 /* Don't print the argument list for the address of a
5108 function. */
5109 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5110 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5111 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5112 operand = d_left (operand);
5114 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5116 /* This indicates a suffix operator. */
5117 operand = d_left (operand);
5118 d_print_subexpr (dpi, options, operand);
5119 d_print_expr_op (dpi, options, op);
5120 return;
5124 if (op->type != DEMANGLE_COMPONENT_CAST)
5125 d_print_expr_op (dpi, options, op);
5126 else
5128 d_append_char (dpi, '(');
5129 d_print_cast (dpi, options, op);
5130 d_append_char (dpi, ')');
5132 if (code && !strcmp (code, "gs"))
5133 /* Avoid parens after '::'. */
5134 d_print_comp (dpi, options, operand);
5135 else if (code && !strcmp (code, "st"))
5136 /* Always print parens for sizeof (type). */
5138 d_append_char (dpi, '(');
5139 d_print_comp (dpi, options, operand);
5140 d_append_char (dpi, ')');
5142 else
5143 d_print_subexpr (dpi, options, operand);
5145 return;
5147 case DEMANGLE_COMPONENT_BINARY:
5148 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5150 d_print_error (dpi);
5151 return;
5154 if (op_is_new_cast (d_left (dc)))
5156 d_print_expr_op (dpi, options, d_left (dc));
5157 d_append_char (dpi, '<');
5158 d_print_comp (dpi, options, d_left (d_right (dc)));
5159 d_append_string (dpi, ">(");
5160 d_print_comp (dpi, options, d_right (d_right (dc)));
5161 d_append_char (dpi, ')');
5162 return;
5165 /* We wrap an expression which uses the greater-than operator in
5166 an extra layer of parens so that it does not get confused
5167 with the '>' which ends the template parameters. */
5168 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5169 && d_left (dc)->u.s_operator.op->len == 1
5170 && d_left (dc)->u.s_operator.op->name[0] == '>')
5171 d_append_char (dpi, '(');
5173 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5174 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5176 /* Function call used in an expression should not have printed types
5177 of the function arguments. Values of the function arguments still
5178 get printed below. */
5180 const struct demangle_component *func = d_left (d_right (dc));
5182 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5183 d_print_error (dpi);
5184 d_print_subexpr (dpi, options, d_left (func));
5186 else
5187 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5188 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5190 d_append_char (dpi, '[');
5191 d_print_comp (dpi, options, d_right (d_right (dc)));
5192 d_append_char (dpi, ']');
5194 else
5196 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5197 d_print_expr_op (dpi, options, d_left (dc));
5198 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5201 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5202 && d_left (dc)->u.s_operator.op->len == 1
5203 && d_left (dc)->u.s_operator.op->name[0] == '>')
5204 d_append_char (dpi, ')');
5206 return;
5208 case DEMANGLE_COMPONENT_BINARY_ARGS:
5209 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5210 d_print_error (dpi);
5211 return;
5213 case DEMANGLE_COMPONENT_TRINARY:
5214 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5215 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5217 d_print_error (dpi);
5218 return;
5221 struct demangle_component *op = d_left (dc);
5222 struct demangle_component *first = d_left (d_right (dc));
5223 struct demangle_component *second = d_left (d_right (d_right (dc)));
5224 struct demangle_component *third = d_right (d_right (d_right (dc)));
5226 if (!strcmp (op->u.s_operator.op->code, "qu"))
5228 d_print_subexpr (dpi, options, first);
5229 d_print_expr_op (dpi, options, op);
5230 d_print_subexpr (dpi, options, second);
5231 d_append_string (dpi, " : ");
5232 d_print_subexpr (dpi, options, third);
5234 else
5236 d_append_string (dpi, "new ");
5237 if (d_left (first) != NULL)
5239 d_print_subexpr (dpi, options, first);
5240 d_append_char (dpi, ' ');
5242 d_print_comp (dpi, options, second);
5243 if (third)
5244 d_print_subexpr (dpi, options, third);
5247 return;
5249 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5250 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5251 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5252 d_print_error (dpi);
5253 return;
5255 case DEMANGLE_COMPONENT_LITERAL:
5256 case DEMANGLE_COMPONENT_LITERAL_NEG:
5258 enum d_builtin_type_print tp;
5260 /* For some builtin types, produce simpler output. */
5261 tp = D_PRINT_DEFAULT;
5262 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5264 tp = d_left (dc)->u.s_builtin.type->print;
5265 switch (tp)
5267 case D_PRINT_INT:
5268 case D_PRINT_UNSIGNED:
5269 case D_PRINT_LONG:
5270 case D_PRINT_UNSIGNED_LONG:
5271 case D_PRINT_LONG_LONG:
5272 case D_PRINT_UNSIGNED_LONG_LONG:
5273 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5275 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5276 d_append_char (dpi, '-');
5277 d_print_comp (dpi, options, d_right (dc));
5278 switch (tp)
5280 default:
5281 break;
5282 case D_PRINT_UNSIGNED:
5283 d_append_char (dpi, 'u');
5284 break;
5285 case D_PRINT_LONG:
5286 d_append_char (dpi, 'l');
5287 break;
5288 case D_PRINT_UNSIGNED_LONG:
5289 d_append_string (dpi, "ul");
5290 break;
5291 case D_PRINT_LONG_LONG:
5292 d_append_string (dpi, "ll");
5293 break;
5294 case D_PRINT_UNSIGNED_LONG_LONG:
5295 d_append_string (dpi, "ull");
5296 break;
5298 return;
5300 break;
5302 case D_PRINT_BOOL:
5303 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5304 && d_right (dc)->u.s_name.len == 1
5305 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5307 switch (d_right (dc)->u.s_name.s[0])
5309 case '0':
5310 d_append_string (dpi, "false");
5311 return;
5312 case '1':
5313 d_append_string (dpi, "true");
5314 return;
5315 default:
5316 break;
5319 break;
5321 default:
5322 break;
5326 d_append_char (dpi, '(');
5327 d_print_comp (dpi, options, d_left (dc));
5328 d_append_char (dpi, ')');
5329 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5330 d_append_char (dpi, '-');
5331 if (tp == D_PRINT_FLOAT)
5332 d_append_char (dpi, '[');
5333 d_print_comp (dpi, options, d_right (dc));
5334 if (tp == D_PRINT_FLOAT)
5335 d_append_char (dpi, ']');
5337 return;
5339 case DEMANGLE_COMPONENT_NUMBER:
5340 d_append_num (dpi, dc->u.s_number.number);
5341 return;
5343 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5344 d_append_string (dpi, "java resource ");
5345 d_print_comp (dpi, options, d_left (dc));
5346 return;
5348 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5349 d_print_comp (dpi, options, d_left (dc));
5350 d_print_comp (dpi, options, d_right (dc));
5351 return;
5353 case DEMANGLE_COMPONENT_CHARACTER:
5354 d_append_char (dpi, dc->u.s_character.character);
5355 return;
5357 case DEMANGLE_COMPONENT_DECLTYPE:
5358 d_append_string (dpi, "decltype (");
5359 d_print_comp (dpi, options, d_left (dc));
5360 d_append_char (dpi, ')');
5361 return;
5363 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5365 int len;
5366 int i;
5367 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5368 if (a == NULL)
5370 /* d_find_pack won't find anything if the only packs involved
5371 in this expansion are function parameter packs; in that
5372 case, just print the pattern and "...". */
5373 d_print_subexpr (dpi, options, d_left (dc));
5374 d_append_string (dpi, "...");
5375 return;
5378 len = d_pack_length (a);
5379 dc = d_left (dc);
5380 for (i = 0; i < len; ++i)
5382 dpi->pack_index = i;
5383 d_print_comp (dpi, options, dc);
5384 if (i < len-1)
5385 d_append_string (dpi, ", ");
5388 return;
5390 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5392 long num = dc->u.s_number.number;
5393 if (num == 0)
5394 d_append_string (dpi, "this");
5395 else
5397 d_append_string (dpi, "{parm#");
5398 d_append_num (dpi, num);
5399 d_append_char (dpi, '}');
5402 return;
5404 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5405 d_append_string (dpi, "global constructors keyed to ");
5406 d_print_comp (dpi, options, dc->u.s_binary.left);
5407 return;
5409 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5410 d_append_string (dpi, "global destructors keyed to ");
5411 d_print_comp (dpi, options, dc->u.s_binary.left);
5412 return;
5414 case DEMANGLE_COMPONENT_LAMBDA:
5415 d_append_string (dpi, "{lambda(");
5416 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5417 d_append_string (dpi, ")#");
5418 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5419 d_append_char (dpi, '}');
5420 return;
5422 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5423 d_append_string (dpi, "{unnamed type#");
5424 d_append_num (dpi, dc->u.s_number.number + 1);
5425 d_append_char (dpi, '}');
5426 return;
5428 case DEMANGLE_COMPONENT_CLONE:
5429 d_print_comp (dpi, options, d_left (dc));
5430 d_append_string (dpi, " [clone ");
5431 d_print_comp (dpi, options, d_right (dc));
5432 d_append_char (dpi, ']');
5433 return;
5435 default:
5436 d_print_error (dpi);
5437 return;
5441 static void
5442 d_print_comp (struct d_print_info *dpi, int options,
5443 const struct demangle_component *dc)
5445 struct d_component_stack self;
5447 self.dc = dc;
5448 self.parent = dpi->component_stack;
5449 dpi->component_stack = &self;
5451 d_print_comp_inner (dpi, options, dc);
5453 dpi->component_stack = self.parent;
5456 /* Print a Java dentifier. For Java we try to handle encoded extended
5457 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5458 so we don't it for C++. Characters are encoded as
5459 __U<hex-char>+_. */
5461 static void
5462 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5464 const char *p;
5465 const char *end;
5467 end = name + len;
5468 for (p = name; p < end; ++p)
5470 if (end - p > 3
5471 && p[0] == '_'
5472 && p[1] == '_'
5473 && p[2] == 'U')
5475 unsigned long c;
5476 const char *q;
5478 c = 0;
5479 for (q = p + 3; q < end; ++q)
5481 int dig;
5483 if (IS_DIGIT (*q))
5484 dig = *q - '0';
5485 else if (*q >= 'A' && *q <= 'F')
5486 dig = *q - 'A' + 10;
5487 else if (*q >= 'a' && *q <= 'f')
5488 dig = *q - 'a' + 10;
5489 else
5490 break;
5492 c = c * 16 + dig;
5494 /* If the Unicode character is larger than 256, we don't try
5495 to deal with it here. FIXME. */
5496 if (q < end && *q == '_' && c < 256)
5498 d_append_char (dpi, c);
5499 p = q;
5500 continue;
5504 d_append_char (dpi, *p);
5508 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5509 qualifiers on this after printing a function. */
5511 static void
5512 d_print_mod_list (struct d_print_info *dpi, int options,
5513 struct d_print_mod *mods, int suffix)
5515 struct d_print_template *hold_dpt;
5517 if (mods == NULL || d_print_saw_error (dpi))
5518 return;
5520 if (mods->printed
5521 || (! suffix
5522 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5523 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5524 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5525 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5526 || mods->mod->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
5527 || (mods->mod->type
5528 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5530 d_print_mod_list (dpi, options, mods->next, suffix);
5531 return;
5534 mods->printed = 1;
5536 hold_dpt = dpi->templates;
5537 dpi->templates = mods->templates;
5539 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5541 d_print_function_type (dpi, options, mods->mod, mods->next);
5542 dpi->templates = hold_dpt;
5543 return;
5545 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5547 d_print_array_type (dpi, options, mods->mod, mods->next);
5548 dpi->templates = hold_dpt;
5549 return;
5551 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5553 struct d_print_mod *hold_modifiers;
5554 struct demangle_component *dc;
5556 /* When this is on the modifier stack, we have pulled any
5557 qualifiers off the right argument already. Otherwise, we
5558 print it as usual, but don't let the left argument see any
5559 modifiers. */
5561 hold_modifiers = dpi->modifiers;
5562 dpi->modifiers = NULL;
5563 d_print_comp (dpi, options, d_left (mods->mod));
5564 dpi->modifiers = hold_modifiers;
5566 if ((options & DMGL_JAVA) == 0)
5567 d_append_string (dpi, "::");
5568 else
5569 d_append_char (dpi, '.');
5571 dc = d_right (mods->mod);
5573 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5575 d_append_string (dpi, "{default arg#");
5576 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5577 d_append_string (dpi, "}::");
5578 dc = dc->u.s_unary_num.sub;
5581 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5582 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5583 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5584 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5585 || dc->type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
5586 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5587 dc = d_left (dc);
5589 d_print_comp (dpi, options, dc);
5591 dpi->templates = hold_dpt;
5592 return;
5595 d_print_mod (dpi, options, mods->mod);
5597 dpi->templates = hold_dpt;
5599 d_print_mod_list (dpi, options, mods->next, suffix);
5602 /* Print a modifier. */
5604 static void
5605 d_print_mod (struct d_print_info *dpi, int options,
5606 const struct demangle_component *mod)
5608 switch (mod->type)
5610 case DEMANGLE_COMPONENT_RESTRICT:
5611 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5612 d_append_string (dpi, " restrict");
5613 return;
5614 case DEMANGLE_COMPONENT_VOLATILE:
5615 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5616 d_append_string (dpi, " volatile");
5617 return;
5618 case DEMANGLE_COMPONENT_CONST:
5619 case DEMANGLE_COMPONENT_CONST_THIS:
5620 d_append_string (dpi, " const");
5621 return;
5622 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
5623 d_append_string (dpi, " transaction_safe");
5624 return;
5625 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5626 d_append_char (dpi, ' ');
5627 d_print_comp (dpi, options, d_right (mod));
5628 return;
5629 case DEMANGLE_COMPONENT_POINTER:
5630 /* There is no pointer symbol in Java. */
5631 if ((options & DMGL_JAVA) == 0)
5632 d_append_char (dpi, '*');
5633 return;
5634 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5635 /* For the ref-qualifier, put a space before the &. */
5636 d_append_char (dpi, ' ');
5637 case DEMANGLE_COMPONENT_REFERENCE:
5638 d_append_char (dpi, '&');
5639 return;
5640 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5641 d_append_char (dpi, ' ');
5642 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5643 d_append_string (dpi, "&&");
5644 return;
5645 case DEMANGLE_COMPONENT_COMPLEX:
5646 d_append_string (dpi, "complex ");
5647 return;
5648 case DEMANGLE_COMPONENT_IMAGINARY:
5649 d_append_string (dpi, "imaginary ");
5650 return;
5651 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5652 if (d_last_char (dpi) != '(')
5653 d_append_char (dpi, ' ');
5654 d_print_comp (dpi, options, d_left (mod));
5655 d_append_string (dpi, "::*");
5656 return;
5657 case DEMANGLE_COMPONENT_TYPED_NAME:
5658 d_print_comp (dpi, options, d_left (mod));
5659 return;
5660 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5661 d_append_string (dpi, " __vector(");
5662 d_print_comp (dpi, options, d_left (mod));
5663 d_append_char (dpi, ')');
5664 return;
5666 default:
5667 /* Otherwise, we have something that won't go back on the
5668 modifier stack, so we can just print it. */
5669 d_print_comp (dpi, options, mod);
5670 return;
5674 /* Print a function type, except for the return type. */
5676 static void
5677 d_print_function_type (struct d_print_info *dpi, int options,
5678 const struct demangle_component *dc,
5679 struct d_print_mod *mods)
5681 int need_paren;
5682 int need_space;
5683 struct d_print_mod *p;
5684 struct d_print_mod *hold_modifiers;
5686 need_paren = 0;
5687 need_space = 0;
5688 for (p = mods; p != NULL; p = p->next)
5690 if (p->printed)
5691 break;
5693 switch (p->mod->type)
5695 case DEMANGLE_COMPONENT_POINTER:
5696 case DEMANGLE_COMPONENT_REFERENCE:
5697 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5698 need_paren = 1;
5699 break;
5700 case DEMANGLE_COMPONENT_RESTRICT:
5701 case DEMANGLE_COMPONENT_VOLATILE:
5702 case DEMANGLE_COMPONENT_CONST:
5703 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5704 case DEMANGLE_COMPONENT_COMPLEX:
5705 case DEMANGLE_COMPONENT_IMAGINARY:
5706 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5707 need_space = 1;
5708 need_paren = 1;
5709 break;
5710 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5711 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5712 case DEMANGLE_COMPONENT_CONST_THIS:
5713 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5714 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5715 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
5716 break;
5717 default:
5718 break;
5720 if (need_paren)
5721 break;
5724 if (need_paren)
5726 if (! need_space)
5728 if (d_last_char (dpi) != '('
5729 && d_last_char (dpi) != '*')
5730 need_space = 1;
5732 if (need_space && d_last_char (dpi) != ' ')
5733 d_append_char (dpi, ' ');
5734 d_append_char (dpi, '(');
5737 hold_modifiers = dpi->modifiers;
5738 dpi->modifiers = NULL;
5740 d_print_mod_list (dpi, options, mods, 0);
5742 if (need_paren)
5743 d_append_char (dpi, ')');
5745 d_append_char (dpi, '(');
5747 if (d_right (dc) != NULL)
5748 d_print_comp (dpi, options, d_right (dc));
5750 d_append_char (dpi, ')');
5752 d_print_mod_list (dpi, options, mods, 1);
5754 dpi->modifiers = hold_modifiers;
5757 /* Print an array type, except for the element type. */
5759 static void
5760 d_print_array_type (struct d_print_info *dpi, int options,
5761 const struct demangle_component *dc,
5762 struct d_print_mod *mods)
5764 int need_space;
5766 need_space = 1;
5767 if (mods != NULL)
5769 int need_paren;
5770 struct d_print_mod *p;
5772 need_paren = 0;
5773 for (p = mods; p != NULL; p = p->next)
5775 if (! p->printed)
5777 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5779 need_space = 0;
5780 break;
5782 else
5784 need_paren = 1;
5785 need_space = 1;
5786 break;
5791 if (need_paren)
5792 d_append_string (dpi, " (");
5794 d_print_mod_list (dpi, options, mods, 0);
5796 if (need_paren)
5797 d_append_char (dpi, ')');
5800 if (need_space)
5801 d_append_char (dpi, ' ');
5803 d_append_char (dpi, '[');
5805 if (d_left (dc) != NULL)
5806 d_print_comp (dpi, options, d_left (dc));
5808 d_append_char (dpi, ']');
5811 /* Print an operator in an expression. */
5813 static void
5814 d_print_expr_op (struct d_print_info *dpi, int options,
5815 const struct demangle_component *dc)
5817 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5818 d_append_buffer (dpi, dc->u.s_operator.op->name,
5819 dc->u.s_operator.op->len);
5820 else
5821 d_print_comp (dpi, options, dc);
5824 /* Print a cast. */
5826 static void
5827 d_print_cast (struct d_print_info *dpi, int options,
5828 const struct demangle_component *dc)
5830 d_print_comp (dpi, options, d_left (dc));
5833 /* Print a conversion operator. */
5835 static void
5836 d_print_conversion (struct d_print_info *dpi, int options,
5837 const struct demangle_component *dc)
5839 struct d_print_template dpt;
5841 /* For a conversion operator, we need the template parameters from
5842 the enclosing template in scope for processing the type. */
5843 if (dpi->current_template != NULL)
5845 dpt.next = dpi->templates;
5846 dpi->templates = &dpt;
5847 dpt.template_decl = dpi->current_template;
5850 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5852 d_print_comp (dpi, options, d_left (dc));
5853 if (dpi->current_template != NULL)
5854 dpi->templates = dpt.next;
5856 else
5858 d_print_comp (dpi, options, d_left (d_left (dc)));
5860 /* For a templated cast operator, we need to remove the template
5861 parameters from scope after printing the operator name,
5862 so we need to handle the template printing here. */
5863 if (dpi->current_template != NULL)
5864 dpi->templates = dpt.next;
5866 if (d_last_char (dpi) == '<')
5867 d_append_char (dpi, ' ');
5868 d_append_char (dpi, '<');
5869 d_print_comp (dpi, options, d_right (d_left (dc)));
5870 /* Avoid generating two consecutive '>' characters, to avoid
5871 the C++ syntactic ambiguity. */
5872 if (d_last_char (dpi) == '>')
5873 d_append_char (dpi, ' ');
5874 d_append_char (dpi, '>');
5878 /* Initialize the information structure we use to pass around
5879 information. */
5881 CP_STATIC_IF_GLIBCPP_V3
5882 void
5883 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5884 struct d_info *di)
5886 di->s = mangled;
5887 di->send = mangled + len;
5888 di->options = options;
5890 di->n = mangled;
5892 /* We can not need more components than twice the number of chars in
5893 the mangled string. Most components correspond directly to
5894 chars, but the ARGLIST types are exceptions. */
5895 di->num_comps = 2 * len;
5896 di->next_comp = 0;
5898 /* Similarly, we can not need more substitutions than there are
5899 chars in the mangled string. */
5900 di->num_subs = len;
5901 di->next_sub = 0;
5902 di->did_subs = 0;
5904 di->last_name = NULL;
5906 di->expansion = 0;
5907 di->is_expression = 0;
5908 di->is_conversion = 0;
5911 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5912 mangled name, return strings in repeated callback giving the demangled
5913 name. OPTIONS is the usual libiberty demangler options. On success,
5914 this returns 1. On failure, returns 0. */
5916 static int
5917 d_demangle_callback (const char *mangled, int options,
5918 demangle_callbackref callback, void *opaque)
5920 enum
5922 DCT_TYPE,
5923 DCT_MANGLED,
5924 DCT_GLOBAL_CTORS,
5925 DCT_GLOBAL_DTORS
5927 type;
5928 struct d_info di;
5929 struct demangle_component *dc;
5930 int status;
5932 if (mangled[0] == '_' && mangled[1] == 'Z')
5933 type = DCT_MANGLED;
5934 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5935 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5936 && (mangled[9] == 'D' || mangled[9] == 'I')
5937 && mangled[10] == '_')
5938 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5939 else
5941 if ((options & DMGL_TYPES) == 0)
5942 return 0;
5943 type = DCT_TYPE;
5946 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5949 #ifdef CP_DYNAMIC_ARRAYS
5950 __extension__ struct demangle_component comps[di.num_comps];
5951 __extension__ struct demangle_component *subs[di.num_subs];
5953 di.comps = comps;
5954 di.subs = subs;
5955 #else
5956 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5957 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5958 #endif
5960 switch (type)
5962 case DCT_TYPE:
5963 dc = cplus_demangle_type (&di);
5964 break;
5965 case DCT_MANGLED:
5966 dc = cplus_demangle_mangled_name (&di, 1);
5967 break;
5968 case DCT_GLOBAL_CTORS:
5969 case DCT_GLOBAL_DTORS:
5970 d_advance (&di, 11);
5971 dc = d_make_comp (&di,
5972 (type == DCT_GLOBAL_CTORS
5973 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5974 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5975 d_make_demangle_mangled_name (&di, d_str (&di)),
5976 NULL);
5977 d_advance (&di, strlen (d_str (&di)));
5978 break;
5979 default:
5980 abort (); /* We have listed all the cases. */
5983 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5984 mangled string, then we didn't successfully demangle it. If
5985 DMGL_PARAMS is not set, we didn't look at the trailing
5986 parameters. */
5987 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5988 dc = NULL;
5990 #ifdef CP_DEMANGLE_DEBUG
5991 d_dump (dc, 0);
5992 #endif
5994 status = (dc != NULL)
5995 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5996 : 0;
5999 return status;
6002 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6003 name, return a buffer allocated with malloc holding the demangled
6004 name. OPTIONS is the usual libiberty demangler options. On
6005 success, this sets *PALC to the allocated size of the returned
6006 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6007 a memory allocation failure, and returns NULL. */
6009 static char *
6010 d_demangle (const char *mangled, int options, size_t *palc)
6012 struct d_growable_string dgs;
6013 int status;
6015 d_growable_string_init (&dgs, 0);
6017 status = d_demangle_callback (mangled, options,
6018 d_growable_string_callback_adapter, &dgs);
6019 if (status == 0)
6021 free (dgs.buf);
6022 *palc = 0;
6023 return NULL;
6026 *palc = dgs.allocation_failure ? 1 : dgs.alc;
6027 return dgs.buf;
6030 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6032 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6034 /* ia64 ABI-mandated entry point in the C++ runtime library for
6035 performing demangling. MANGLED_NAME is a NUL-terminated character
6036 string containing the name to be demangled.
6038 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6039 *LENGTH bytes, into which the demangled name is stored. If
6040 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6041 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6042 is placed in a region of memory allocated with malloc.
6044 If LENGTH is non-NULL, the length of the buffer containing the
6045 demangled name, is placed in *LENGTH.
6047 The return value is a pointer to the start of the NUL-terminated
6048 demangled name, or NULL if the demangling fails. The caller is
6049 responsible for deallocating this memory using free.
6051 *STATUS is set to one of the following values:
6052 0: The demangling operation succeeded.
6053 -1: A memory allocation failure occurred.
6054 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6055 -3: One of the arguments is invalid.
6057 The demangling is performed using the C++ ABI mangling rules, with
6058 GNU extensions. */
6060 char *
6061 __cxa_demangle (const char *mangled_name, char *output_buffer,
6062 size_t *length, int *status)
6064 char *demangled;
6065 size_t alc;
6067 if (mangled_name == NULL)
6069 if (status != NULL)
6070 *status = -3;
6071 return NULL;
6074 if (output_buffer != NULL && length == NULL)
6076 if (status != NULL)
6077 *status = -3;
6078 return NULL;
6081 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6083 if (demangled == NULL)
6085 if (status != NULL)
6087 if (alc == 1)
6088 *status = -1;
6089 else
6090 *status = -2;
6092 return NULL;
6095 if (output_buffer == NULL)
6097 if (length != NULL)
6098 *length = alc;
6100 else
6102 if (strlen (demangled) < *length)
6104 strcpy (output_buffer, demangled);
6105 free (demangled);
6106 demangled = output_buffer;
6108 else
6110 free (output_buffer);
6111 *length = alc;
6115 if (status != NULL)
6116 *status = 0;
6118 return demangled;
6121 extern int __gcclibcxx_demangle_callback (const char *,
6122 void (*)
6123 (const char *, size_t, void *),
6124 void *);
6126 /* Alternative, allocationless entry point in the C++ runtime library
6127 for performing demangling. MANGLED_NAME is a NUL-terminated character
6128 string containing the name to be demangled.
6130 CALLBACK is a callback function, called with demangled string
6131 segments as demangling progresses; it is called at least once,
6132 but may be called more than once. OPAQUE is a generalized pointer
6133 used as a callback argument.
6135 The return code is one of the following values, equivalent to
6136 the STATUS values of __cxa_demangle() (excluding -1, since this
6137 function performs no memory allocations):
6138 0: The demangling operation succeeded.
6139 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6140 -3: One of the arguments is invalid.
6142 The demangling is performed using the C++ ABI mangling rules, with
6143 GNU extensions. */
6146 __gcclibcxx_demangle_callback (const char *mangled_name,
6147 void (*callback) (const char *, size_t, void *),
6148 void *opaque)
6150 int status;
6152 if (mangled_name == NULL || callback == NULL)
6153 return -3;
6155 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6156 callback, opaque);
6157 if (status == 0)
6158 return -2;
6160 return 0;
6163 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6165 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6166 mangled name, return a buffer allocated with malloc holding the
6167 demangled name. Otherwise, return NULL. */
6169 char *
6170 cplus_demangle_v3 (const char *mangled, int options)
6172 size_t alc;
6174 return d_demangle (mangled, options, &alc);
6178 cplus_demangle_v3_callback (const char *mangled, int options,
6179 demangle_callbackref callback, void *opaque)
6181 return d_demangle_callback (mangled, options, callback, opaque);
6184 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6185 conventions, but the output formatting is a little different.
6186 This instructs the C++ demangler not to emit pointer characters ("*"), to
6187 use Java's namespace separator symbol ("." instead of "::"), and to output
6188 JArray<TYPE> as TYPE[]. */
6190 char *
6191 java_demangle_v3 (const char *mangled)
6193 size_t alc;
6195 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6199 java_demangle_v3_callback (const char *mangled,
6200 demangle_callbackref callback, void *opaque)
6202 return d_demangle_callback (mangled,
6203 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6204 callback, opaque);
6207 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6209 #ifndef IN_GLIBCPP_V3
6211 /* Demangle a string in order to find out whether it is a constructor
6212 or destructor. Return non-zero on success. Set *CTOR_KIND and
6213 *DTOR_KIND appropriately. */
6215 static int
6216 is_ctor_or_dtor (const char *mangled,
6217 enum gnu_v3_ctor_kinds *ctor_kind,
6218 enum gnu_v3_dtor_kinds *dtor_kind)
6220 struct d_info di;
6221 struct demangle_component *dc;
6222 int ret;
6224 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6225 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6227 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6230 #ifdef CP_DYNAMIC_ARRAYS
6231 __extension__ struct demangle_component comps[di.num_comps];
6232 __extension__ struct demangle_component *subs[di.num_subs];
6234 di.comps = comps;
6235 di.subs = subs;
6236 #else
6237 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6238 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6239 #endif
6241 dc = cplus_demangle_mangled_name (&di, 1);
6243 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6244 to demangle the entire string. */
6246 ret = 0;
6247 while (dc != NULL)
6249 switch (dc->type)
6251 /* These cannot appear on a constructor or destructor. */
6252 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6253 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6254 case DEMANGLE_COMPONENT_CONST_THIS:
6255 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6256 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6257 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
6258 default:
6259 dc = NULL;
6260 break;
6261 case DEMANGLE_COMPONENT_TYPED_NAME:
6262 case DEMANGLE_COMPONENT_TEMPLATE:
6263 dc = d_left (dc);
6264 break;
6265 case DEMANGLE_COMPONENT_QUAL_NAME:
6266 case DEMANGLE_COMPONENT_LOCAL_NAME:
6267 dc = d_right (dc);
6268 break;
6269 case DEMANGLE_COMPONENT_CTOR:
6270 *ctor_kind = dc->u.s_ctor.kind;
6271 ret = 1;
6272 dc = NULL;
6273 break;
6274 case DEMANGLE_COMPONENT_DTOR:
6275 *dtor_kind = dc->u.s_dtor.kind;
6276 ret = 1;
6277 dc = NULL;
6278 break;
6283 return ret;
6286 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6287 name. A non-zero return indicates the type of constructor. */
6289 enum gnu_v3_ctor_kinds
6290 is_gnu_v3_mangled_ctor (const char *name)
6292 enum gnu_v3_ctor_kinds ctor_kind;
6293 enum gnu_v3_dtor_kinds dtor_kind;
6295 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6296 return (enum gnu_v3_ctor_kinds) 0;
6297 return ctor_kind;
6301 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6302 name. A non-zero return indicates the type of destructor. */
6304 enum gnu_v3_dtor_kinds
6305 is_gnu_v3_mangled_dtor (const char *name)
6307 enum gnu_v3_ctor_kinds ctor_kind;
6308 enum gnu_v3_dtor_kinds dtor_kind;
6310 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6311 return (enum gnu_v3_dtor_kinds) 0;
6312 return dtor_kind;
6315 #endif /* IN_GLIBCPP_V3 */
6317 #ifdef STANDALONE_DEMANGLER
6319 #include "getopt.h"
6320 #include "dyn-string.h"
6322 static void print_usage (FILE* fp, int exit_value);
6324 #define IS_ALPHA(CHAR) \
6325 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6326 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6328 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6329 #define is_mangled_char(CHAR) \
6330 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6331 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6333 /* The name of this program, as invoked. */
6334 const char* program_name;
6336 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6338 static void
6339 print_usage (FILE* fp, int exit_value)
6341 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6342 fprintf (fp, "Options:\n");
6343 fprintf (fp, " -h,--help Display this message.\n");
6344 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6345 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6346 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6348 exit (exit_value);
6351 /* Option specification for getopt_long. */
6352 static const struct option long_options[] =
6354 { "help", no_argument, NULL, 'h' },
6355 { "no-params", no_argument, NULL, 'p' },
6356 { "verbose", no_argument, NULL, 'v' },
6357 { NULL, no_argument, NULL, 0 },
6360 /* Main entry for a demangling filter executable. It will demangle
6361 its command line arguments, if any. If none are provided, it will
6362 filter stdin to stdout, replacing any recognized mangled C++ names
6363 with their demangled equivalents. */
6366 main (int argc, char *argv[])
6368 int i;
6369 int opt_char;
6370 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6372 /* Use the program name of this program, as invoked. */
6373 program_name = argv[0];
6375 /* Parse options. */
6378 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6379 switch (opt_char)
6381 case '?': /* Unrecognized option. */
6382 print_usage (stderr, 1);
6383 break;
6385 case 'h':
6386 print_usage (stdout, 0);
6387 break;
6389 case 'p':
6390 options &= ~ DMGL_PARAMS;
6391 break;
6393 case 'v':
6394 options |= DMGL_VERBOSE;
6395 break;
6398 while (opt_char != -1);
6400 if (optind == argc)
6401 /* No command line arguments were provided. Filter stdin. */
6403 dyn_string_t mangled = dyn_string_new (3);
6404 char *s;
6406 /* Read all of input. */
6407 while (!feof (stdin))
6409 char c;
6411 /* Pile characters into mangled until we hit one that can't
6412 occur in a mangled name. */
6413 c = getchar ();
6414 while (!feof (stdin) && is_mangled_char (c))
6416 dyn_string_append_char (mangled, c);
6417 if (feof (stdin))
6418 break;
6419 c = getchar ();
6422 if (dyn_string_length (mangled) > 0)
6424 #ifdef IN_GLIBCPP_V3
6425 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6426 #else
6427 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6428 #endif
6430 if (s != NULL)
6432 fputs (s, stdout);
6433 free (s);
6435 else
6437 /* It might not have been a mangled name. Print the
6438 original text. */
6439 fputs (dyn_string_buf (mangled), stdout);
6442 dyn_string_clear (mangled);
6445 /* If we haven't hit EOF yet, we've read one character that
6446 can't occur in a mangled name, so print it out. */
6447 if (!feof (stdin))
6448 putchar (c);
6451 dyn_string_delete (mangled);
6453 else
6454 /* Demangle command line arguments. */
6456 /* Loop over command line arguments. */
6457 for (i = optind; i < argc; ++i)
6459 char *s;
6460 #ifdef IN_GLIBCPP_V3
6461 int status;
6462 #endif
6464 /* Attempt to demangle. */
6465 #ifdef IN_GLIBCPP_V3
6466 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6467 #else
6468 s = cplus_demangle_v3 (argv[i], options);
6469 #endif
6471 /* If it worked, print the demangled name. */
6472 if (s != NULL)
6474 printf ("%s\n", s);
6475 free (s);
6477 else
6479 #ifdef IN_GLIBCPP_V3
6480 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6481 #else
6482 fprintf (stderr, "Failed: %s\n", argv[i]);
6483 #endif
6488 return 0;
6491 #endif /* STANDALONE_DEMANGLER */