Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9-mobile / libiberty / cp-demangle.c
blobdaa292df72f536962b0b54594bce684c0618f2f3
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
20 executable.)
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
41 name.
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
58 demangle_callbackref
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
64 cplus_demangle_print
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
73 IN_LIBGCC2
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int *status)
77 int __gcclibcxx_demangle_callback (const char *,
78 void (*)
79 (const char *, size_t, void *),
80 void *)
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
84 IN_GLIBCPP_V3
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
89 STANDALONE_DEMANGLER
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
93 CP_DEMANGLE_DEBUG
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 #if defined (_AIX) && !defined (__GNUC__)
99 #pragma alloca
100 #endif
102 #ifdef HAVE_CONFIG_H
103 #include "config.h"
104 #endif
106 #include <stdio.h>
108 #ifdef HAVE_STDLIB_H
109 #include <stdlib.h>
110 #endif
111 #ifdef HAVE_STRING_H
112 #include <string.h>
113 #endif
115 #ifdef HAVE_ALLOCA_H
116 # include <alloca.h>
117 #else
118 # ifndef alloca
119 # ifdef __GNUC__
120 # define alloca __builtin_alloca
121 # else
122 extern char *alloca ();
123 # endif /* __GNUC__ */
124 # endif /* alloca */
125 #endif /* HAVE_ALLOCA_H */
127 #include "ansidecl.h"
128 #include "libiberty.h"
129 #include "demangle.h"
130 #include "cp-demangle.h"
132 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
133 also rename them via #define to avoid compiler errors when the
134 static definition conflicts with the extern declaration in a header
135 file. */
136 #ifdef IN_GLIBCPP_V3
138 #define CP_STATIC_IF_GLIBCPP_V3 static
140 #define cplus_demangle_fill_name d_fill_name
141 static int d_fill_name (struct demangle_component *, const char *, int);
143 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
144 static int
145 d_fill_extended_operator (struct demangle_component *, int,
146 struct demangle_component *);
148 #define cplus_demangle_fill_ctor d_fill_ctor
149 static int
150 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
151 struct demangle_component *);
153 #define cplus_demangle_fill_dtor d_fill_dtor
154 static int
155 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
156 struct demangle_component *);
158 #define cplus_demangle_mangled_name d_mangled_name
159 static struct demangle_component *d_mangled_name (struct d_info *, int);
161 #define cplus_demangle_type d_type
162 static struct demangle_component *d_type (struct d_info *);
164 #define cplus_demangle_print d_print
165 static char *d_print (int, const struct demangle_component *, int, size_t *);
167 #define cplus_demangle_print_callback d_print_callback
168 static int d_print_callback (int, const struct demangle_component *,
169 demangle_callbackref, void *);
171 #define cplus_demangle_init_info d_init_info
172 static void d_init_info (const char *, int, size_t, struct d_info *);
174 #else /* ! defined(IN_GLIBCPP_V3) */
175 #define CP_STATIC_IF_GLIBCPP_V3
176 #endif /* ! defined(IN_GLIBCPP_V3) */
178 /* See if the compiler supports dynamic arrays. */
180 #ifdef __GNUC__
181 #define CP_DYNAMIC_ARRAYS
182 #else
183 #ifdef __STDC__
184 #ifdef __STDC_VERSION__
185 #if __STDC_VERSION__ >= 199901L
186 #define CP_DYNAMIC_ARRAYS
187 #endif /* __STDC__VERSION >= 199901L */
188 #endif /* defined (__STDC_VERSION__) */
189 #endif /* defined (__STDC__) */
190 #endif /* ! defined (__GNUC__) */
192 /* We avoid pulling in the ctype tables, to prevent pulling in
193 additional unresolved symbols when this code is used in a library.
194 FIXME: Is this really a valid reason? This comes from the original
195 V3 demangler code.
197 As of this writing this file has the following undefined references
198 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
199 strcat, strlen. */
201 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
205 /* The prefix prepended by GCC to an identifier represnting the
206 anonymous namespace. */
207 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
211 /* Information we keep for the standard substitutions. */
213 struct d_standard_sub_info
215 /* The code for this substitution. */
216 char code;
217 /* The simple string it expands to. */
218 const char *simple_expansion;
219 /* The length of the simple expansion. */
220 int simple_len;
221 /* The results of a full, verbose, expansion. This is used when
222 qualifying a constructor/destructor, or when in verbose mode. */
223 const char *full_expansion;
224 /* The length of the full expansion. */
225 int full_len;
226 /* What to set the last_name field of d_info to; NULL if we should
227 not set it. This is only relevant when qualifying a
228 constructor/destructor. */
229 const char *set_last_name;
230 /* The length of set_last_name. */
231 int set_last_name_len;
234 /* Accessors for subtrees of struct demangle_component. */
236 #define d_left(dc) ((dc)->u.s_binary.left)
237 #define d_right(dc) ((dc)->u.s_binary.right)
239 /* A list of templates. This is used while printing. */
241 struct d_print_template
243 /* Next template on the list. */
244 struct d_print_template *next;
245 /* This template. */
246 const struct demangle_component *template_decl;
249 /* A list of type modifiers. This is used while printing. */
251 struct d_print_mod
253 /* Next modifier on the list. These are in the reverse of the order
254 in which they appeared in the mangled string. */
255 struct d_print_mod *next;
256 /* The modifier. */
257 const struct demangle_component *mod;
258 /* Whether this modifier was printed. */
259 int printed;
260 /* The list of templates which applies to this modifier. */
261 struct d_print_template *templates;
264 /* We use these structures to hold information during printing. */
266 struct d_growable_string
268 /* Buffer holding the result. */
269 char *buf;
270 /* Current length of data in buffer. */
271 size_t len;
272 /* Allocated size of buffer. */
273 size_t alc;
274 /* Set to 1 if we had a memory allocation failure. */
275 int allocation_failure;
278 /* A demangle component and some scope captured when it was first
279 traversed. */
281 struct d_saved_scope
283 /* The component whose scope this is. */
284 const struct demangle_component *container;
285 /* The list of templates, if any, that was current when this
286 scope was captured. */
287 struct d_print_template *templates;
290 /* Checkpoint structure to allow backtracking. This holds copies
291 of the fields of struct d_info that need to be restored
292 if a trial parse needs to be backtracked over. */
294 struct d_info_checkpoint
296 const char *n;
297 int next_comp;
298 int next_sub;
299 int did_subs;
300 int expansion;
303 enum { D_PRINT_BUFFER_LENGTH = 256 };
304 struct d_print_info
306 /* Fixed-length allocated buffer for demangled data, flushed to the
307 callback with a NUL termination once full. */
308 char buf[D_PRINT_BUFFER_LENGTH];
309 /* Current length of data in buffer. */
310 size_t len;
311 /* The last character printed, saved individually so that it survives
312 any buffer flush. */
313 char last_char;
314 /* Callback function to handle demangled buffer flush. */
315 demangle_callbackref callback;
316 /* Opaque callback argument. */
317 void *opaque;
318 /* The current list of templates, if any. */
319 struct d_print_template *templates;
320 /* The current list of modifiers (e.g., pointer, reference, etc.),
321 if any. */
322 struct d_print_mod *modifiers;
323 /* Set to 1 if we saw a demangling error. */
324 int demangle_failure;
325 /* The current index into any template argument packs we are using
326 for printing. */
327 int pack_index;
328 /* Number of d_print_flush calls so far. */
329 unsigned long int flush_count;
330 /* Array of saved scopes for evaluating substitutions. */
331 struct d_saved_scope *saved_scopes;
332 /* Index of the next unused saved scope in the above array. */
333 int next_saved_scope;
334 /* Number of saved scopes in the above array. */
335 int num_saved_scopes;
336 /* Array of templates for saving into scopes. */
337 struct d_print_template *copy_templates;
338 /* Index of the next unused copy template in the above array. */
339 int next_copy_template;
340 /* Number of copy templates in the above array. */
341 int num_copy_templates;
342 /* The nearest enclosing template, if any. */
343 const struct demangle_component *current_template;
346 #ifdef CP_DEMANGLE_DEBUG
347 static void d_dump (struct demangle_component *, int);
348 #endif
350 static struct demangle_component *
351 d_make_empty (struct d_info *);
353 static struct demangle_component *
354 d_make_comp (struct d_info *, enum demangle_component_type,
355 struct demangle_component *,
356 struct demangle_component *);
358 static struct demangle_component *
359 d_make_name (struct d_info *, const char *, int);
361 static struct demangle_component *
362 d_make_demangle_mangled_name (struct d_info *, const char *);
364 static struct demangle_component *
365 d_make_builtin_type (struct d_info *,
366 const struct demangle_builtin_type_info *);
368 static struct demangle_component *
369 d_make_operator (struct d_info *,
370 const struct demangle_operator_info *);
372 static struct demangle_component *
373 d_make_extended_operator (struct d_info *, int,
374 struct demangle_component *);
376 static struct demangle_component *
377 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
378 struct demangle_component *);
380 static struct demangle_component *
381 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
382 struct demangle_component *);
384 static struct demangle_component *
385 d_make_template_param (struct d_info *, long);
387 static struct demangle_component *
388 d_make_sub (struct d_info *, const char *, int);
390 static int
391 has_return_type (struct demangle_component *);
393 static int
394 is_ctor_dtor_or_conversion (struct demangle_component *);
396 static struct demangle_component *d_encoding (struct d_info *, int);
398 static struct demangle_component *d_name (struct d_info *);
400 static struct demangle_component *d_nested_name (struct d_info *);
402 static struct demangle_component *d_prefix (struct d_info *);
404 static struct demangle_component *d_unqualified_name (struct d_info *);
406 static struct demangle_component *d_source_name (struct d_info *);
408 static long d_number (struct d_info *);
410 static struct demangle_component *d_identifier (struct d_info *, int);
412 static struct demangle_component *d_operator_name (struct d_info *);
414 static struct demangle_component *d_special_name (struct d_info *);
416 static int d_call_offset (struct d_info *, int);
418 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
420 static struct demangle_component **
421 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
423 static struct demangle_component *
424 d_ref_qualifier (struct d_info *, struct demangle_component *);
426 static struct demangle_component *
427 d_function_type (struct d_info *);
429 static struct demangle_component *
430 d_bare_function_type (struct d_info *, int);
432 static struct demangle_component *
433 d_class_enum_type (struct d_info *);
435 static struct demangle_component *d_array_type (struct d_info *);
437 static struct demangle_component *d_vector_type (struct d_info *);
439 static struct demangle_component *
440 d_pointer_to_member_type (struct d_info *);
442 static struct demangle_component *
443 d_template_param (struct d_info *);
445 static struct demangle_component *d_template_args (struct d_info *);
447 static struct demangle_component *
448 d_template_arg (struct d_info *);
450 static struct demangle_component *d_expression (struct d_info *);
452 static struct demangle_component *d_expr_primary (struct d_info *);
454 static struct demangle_component *d_local_name (struct d_info *);
456 static int d_discriminator (struct d_info *);
458 static struct demangle_component *d_lambda (struct d_info *);
460 static struct demangle_component *d_unnamed_type (struct d_info *);
462 static struct demangle_component *
463 d_clone_suffix (struct d_info *, struct demangle_component *);
465 static int
466 d_add_substitution (struct d_info *, struct demangle_component *);
468 static struct demangle_component *d_substitution (struct d_info *, int);
470 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
472 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
474 static void d_growable_string_init (struct d_growable_string *, size_t);
476 static inline void
477 d_growable_string_resize (struct d_growable_string *, size_t);
479 static inline void
480 d_growable_string_append_buffer (struct d_growable_string *,
481 const char *, size_t);
482 static void
483 d_growable_string_callback_adapter (const char *, size_t, void *);
485 static void
486 d_print_init (struct d_print_info *, demangle_callbackref, void *,
487 const struct demangle_component *);
489 static inline void d_print_error (struct d_print_info *);
491 static inline int d_print_saw_error (struct d_print_info *);
493 static inline void d_print_flush (struct d_print_info *);
495 static inline void d_append_char (struct d_print_info *, char);
497 static inline void d_append_buffer (struct d_print_info *,
498 const char *, size_t);
500 static inline void d_append_string (struct d_print_info *, const char *);
502 static inline char d_last_char (struct d_print_info *);
504 static void
505 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
507 static void
508 d_print_java_identifier (struct d_print_info *, const char *, int);
510 static void
511 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
513 static void
514 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
516 static void
517 d_print_function_type (struct d_print_info *, int,
518 const struct demangle_component *,
519 struct d_print_mod *);
521 static void
522 d_print_array_type (struct d_print_info *, int,
523 const struct demangle_component *,
524 struct d_print_mod *);
526 static void
527 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
529 static void d_print_cast (struct d_print_info *, int,
530 const struct demangle_component *);
531 static void d_print_conversion (struct d_print_info *, int,
532 const struct demangle_component *);
534 static int d_demangle_callback (const char *, int,
535 demangle_callbackref, void *);
536 static char *d_demangle (const char *, int, size_t *);
538 #ifdef CP_DEMANGLE_DEBUG
540 static void
541 d_dump (struct demangle_component *dc, int indent)
543 int i;
545 if (dc == NULL)
547 if (indent == 0)
548 printf ("failed demangling\n");
549 return;
552 for (i = 0; i < indent; ++i)
553 putchar (' ');
555 switch (dc->type)
557 case DEMANGLE_COMPONENT_NAME:
558 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
559 return;
560 case DEMANGLE_COMPONENT_TAGGED_NAME:
561 printf ("tagged name\n");
562 d_dump (dc->u.s_binary.left, indent + 2);
563 d_dump (dc->u.s_binary.right, indent + 2);
564 return;
565 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
566 printf ("template parameter %ld\n", dc->u.s_number.number);
567 return;
568 case DEMANGLE_COMPONENT_CTOR:
569 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
570 d_dump (dc->u.s_ctor.name, indent + 2);
571 return;
572 case DEMANGLE_COMPONENT_DTOR:
573 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
574 d_dump (dc->u.s_dtor.name, indent + 2);
575 return;
576 case DEMANGLE_COMPONENT_SUB_STD:
577 printf ("standard substitution %s\n", dc->u.s_string.string);
578 return;
579 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
580 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
581 return;
582 case DEMANGLE_COMPONENT_OPERATOR:
583 printf ("operator %s\n", dc->u.s_operator.op->name);
584 return;
585 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
586 printf ("extended operator with %d args\n",
587 dc->u.s_extended_operator.args);
588 d_dump (dc->u.s_extended_operator.name, indent + 2);
589 return;
591 case DEMANGLE_COMPONENT_QUAL_NAME:
592 printf ("qualified name\n");
593 break;
594 case DEMANGLE_COMPONENT_LOCAL_NAME:
595 printf ("local name\n");
596 break;
597 case DEMANGLE_COMPONENT_TYPED_NAME:
598 printf ("typed name\n");
599 break;
600 case DEMANGLE_COMPONENT_TEMPLATE:
601 printf ("template\n");
602 break;
603 case DEMANGLE_COMPONENT_VTABLE:
604 printf ("vtable\n");
605 break;
606 case DEMANGLE_COMPONENT_VTT:
607 printf ("VTT\n");
608 break;
609 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
610 printf ("construction vtable\n");
611 break;
612 case DEMANGLE_COMPONENT_TYPEINFO:
613 printf ("typeinfo\n");
614 break;
615 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
616 printf ("typeinfo name\n");
617 break;
618 case DEMANGLE_COMPONENT_TYPEINFO_FN:
619 printf ("typeinfo function\n");
620 break;
621 case DEMANGLE_COMPONENT_THUNK:
622 printf ("thunk\n");
623 break;
624 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
625 printf ("virtual thunk\n");
626 break;
627 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
628 printf ("covariant thunk\n");
629 break;
630 case DEMANGLE_COMPONENT_JAVA_CLASS:
631 printf ("java class\n");
632 break;
633 case DEMANGLE_COMPONENT_GUARD:
634 printf ("guard\n");
635 break;
636 case DEMANGLE_COMPONENT_REFTEMP:
637 printf ("reference temporary\n");
638 break;
639 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
640 printf ("hidden alias\n");
641 break;
642 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
643 printf ("transaction clone\n");
644 break;
645 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
646 printf ("non-transaction clone\n");
647 break;
648 case DEMANGLE_COMPONENT_RESTRICT:
649 printf ("restrict\n");
650 break;
651 case DEMANGLE_COMPONENT_VOLATILE:
652 printf ("volatile\n");
653 break;
654 case DEMANGLE_COMPONENT_CONST:
655 printf ("const\n");
656 break;
657 case DEMANGLE_COMPONENT_RESTRICT_THIS:
658 printf ("restrict this\n");
659 break;
660 case DEMANGLE_COMPONENT_VOLATILE_THIS:
661 printf ("volatile this\n");
662 break;
663 case DEMANGLE_COMPONENT_CONST_THIS:
664 printf ("const this\n");
665 break;
666 case DEMANGLE_COMPONENT_REFERENCE_THIS:
667 printf ("reference this\n");
668 break;
669 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
670 printf ("rvalue reference this\n");
671 break;
672 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
673 printf ("vendor type qualifier\n");
674 break;
675 case DEMANGLE_COMPONENT_POINTER:
676 printf ("pointer\n");
677 break;
678 case DEMANGLE_COMPONENT_REFERENCE:
679 printf ("reference\n");
680 break;
681 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
682 printf ("rvalue reference\n");
683 break;
684 case DEMANGLE_COMPONENT_COMPLEX:
685 printf ("complex\n");
686 break;
687 case DEMANGLE_COMPONENT_IMAGINARY:
688 printf ("imaginary\n");
689 break;
690 case DEMANGLE_COMPONENT_VENDOR_TYPE:
691 printf ("vendor type\n");
692 break;
693 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
694 printf ("function type\n");
695 break;
696 case DEMANGLE_COMPONENT_ARRAY_TYPE:
697 printf ("array type\n");
698 break;
699 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
700 printf ("pointer to member type\n");
701 break;
702 case DEMANGLE_COMPONENT_FIXED_TYPE:
703 printf ("fixed-point type\n");
704 break;
705 case DEMANGLE_COMPONENT_ARGLIST:
706 printf ("argument list\n");
707 break;
708 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
709 printf ("template argument list\n");
710 break;
711 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
712 printf ("initializer list\n");
713 break;
714 case DEMANGLE_COMPONENT_CAST:
715 printf ("cast\n");
716 break;
717 case DEMANGLE_COMPONENT_CONVERSION:
718 printf ("conversion operator\n");
719 break;
720 case DEMANGLE_COMPONENT_NULLARY:
721 printf ("nullary operator\n");
722 break;
723 case DEMANGLE_COMPONENT_UNARY:
724 printf ("unary operator\n");
725 break;
726 case DEMANGLE_COMPONENT_BINARY:
727 printf ("binary operator\n");
728 break;
729 case DEMANGLE_COMPONENT_BINARY_ARGS:
730 printf ("binary operator arguments\n");
731 break;
732 case DEMANGLE_COMPONENT_TRINARY:
733 printf ("trinary operator\n");
734 break;
735 case DEMANGLE_COMPONENT_TRINARY_ARG1:
736 printf ("trinary operator arguments 1\n");
737 break;
738 case DEMANGLE_COMPONENT_TRINARY_ARG2:
739 printf ("trinary operator arguments 1\n");
740 break;
741 case DEMANGLE_COMPONENT_LITERAL:
742 printf ("literal\n");
743 break;
744 case DEMANGLE_COMPONENT_LITERAL_NEG:
745 printf ("negative literal\n");
746 break;
747 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
748 printf ("java resource\n");
749 break;
750 case DEMANGLE_COMPONENT_COMPOUND_NAME:
751 printf ("compound name\n");
752 break;
753 case DEMANGLE_COMPONENT_CHARACTER:
754 printf ("character '%c'\n", dc->u.s_character.character);
755 return;
756 case DEMANGLE_COMPONENT_DECLTYPE:
757 printf ("decltype\n");
758 break;
759 case DEMANGLE_COMPONENT_PACK_EXPANSION:
760 printf ("pack expansion\n");
761 break;
762 case DEMANGLE_COMPONENT_TLS_INIT:
763 printf ("tls init function\n");
764 break;
765 case DEMANGLE_COMPONENT_TLS_WRAPPER:
766 printf ("tls wrapper function\n");
767 break;
768 case DEMANGLE_COMPONENT_DEFAULT_ARG:
769 printf ("default argument %d\n", dc->u.s_unary_num.num);
770 d_dump (dc->u.s_unary_num.sub, indent+2);
771 return;
772 case DEMANGLE_COMPONENT_LAMBDA:
773 printf ("lambda %d\n", dc->u.s_unary_num.num);
774 d_dump (dc->u.s_unary_num.sub, indent+2);
775 return;
778 d_dump (d_left (dc), indent + 2);
779 d_dump (d_right (dc), indent + 2);
782 #endif /* CP_DEMANGLE_DEBUG */
784 /* Fill in a DEMANGLE_COMPONENT_NAME. */
786 CP_STATIC_IF_GLIBCPP_V3
788 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
790 if (p == NULL || s == NULL || len == 0)
791 return 0;
792 p->type = DEMANGLE_COMPONENT_NAME;
793 p->u.s_name.s = s;
794 p->u.s_name.len = len;
795 return 1;
798 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
800 CP_STATIC_IF_GLIBCPP_V3
802 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
803 struct demangle_component *name)
805 if (p == NULL || args < 0 || name == NULL)
806 return 0;
807 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
808 p->u.s_extended_operator.args = args;
809 p->u.s_extended_operator.name = name;
810 return 1;
813 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
815 CP_STATIC_IF_GLIBCPP_V3
817 cplus_demangle_fill_ctor (struct demangle_component *p,
818 enum gnu_v3_ctor_kinds kind,
819 struct demangle_component *name)
821 if (p == NULL
822 || name == NULL
823 || (int) kind < gnu_v3_complete_object_ctor
824 || (int) kind > gnu_v3_object_ctor_group)
825 return 0;
826 p->type = DEMANGLE_COMPONENT_CTOR;
827 p->u.s_ctor.kind = kind;
828 p->u.s_ctor.name = name;
829 return 1;
832 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
834 CP_STATIC_IF_GLIBCPP_V3
836 cplus_demangle_fill_dtor (struct demangle_component *p,
837 enum gnu_v3_dtor_kinds kind,
838 struct demangle_component *name)
840 if (p == NULL
841 || name == NULL
842 || (int) kind < gnu_v3_deleting_dtor
843 || (int) kind > gnu_v3_object_dtor_group)
844 return 0;
845 p->type = DEMANGLE_COMPONENT_DTOR;
846 p->u.s_dtor.kind = kind;
847 p->u.s_dtor.name = name;
848 return 1;
851 /* Add a new component. */
853 static struct demangle_component *
854 d_make_empty (struct d_info *di)
856 struct demangle_component *p;
858 if (di->next_comp >= di->num_comps)
859 return NULL;
860 p = &di->comps[di->next_comp];
861 ++di->next_comp;
862 return p;
865 /* Add a new generic component. */
867 static struct demangle_component *
868 d_make_comp (struct d_info *di, enum demangle_component_type type,
869 struct demangle_component *left,
870 struct demangle_component *right)
872 struct demangle_component *p;
874 /* We check for errors here. A typical error would be a NULL return
875 from a subroutine. We catch those here, and return NULL
876 upward. */
877 switch (type)
879 /* These types require two parameters. */
880 case DEMANGLE_COMPONENT_QUAL_NAME:
881 case DEMANGLE_COMPONENT_LOCAL_NAME:
882 case DEMANGLE_COMPONENT_TYPED_NAME:
883 case DEMANGLE_COMPONENT_TAGGED_NAME:
884 case DEMANGLE_COMPONENT_TEMPLATE:
885 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
886 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
887 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
888 case DEMANGLE_COMPONENT_UNARY:
889 case DEMANGLE_COMPONENT_BINARY:
890 case DEMANGLE_COMPONENT_BINARY_ARGS:
891 case DEMANGLE_COMPONENT_TRINARY:
892 case DEMANGLE_COMPONENT_TRINARY_ARG1:
893 case DEMANGLE_COMPONENT_LITERAL:
894 case DEMANGLE_COMPONENT_LITERAL_NEG:
895 case DEMANGLE_COMPONENT_COMPOUND_NAME:
896 case DEMANGLE_COMPONENT_VECTOR_TYPE:
897 case DEMANGLE_COMPONENT_CLONE:
898 if (left == NULL || right == NULL)
899 return NULL;
900 break;
902 /* These types only require one parameter. */
903 case DEMANGLE_COMPONENT_VTABLE:
904 case DEMANGLE_COMPONENT_VTT:
905 case DEMANGLE_COMPONENT_TYPEINFO:
906 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
907 case DEMANGLE_COMPONENT_TYPEINFO_FN:
908 case DEMANGLE_COMPONENT_THUNK:
909 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
910 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
911 case DEMANGLE_COMPONENT_JAVA_CLASS:
912 case DEMANGLE_COMPONENT_GUARD:
913 case DEMANGLE_COMPONENT_TLS_INIT:
914 case DEMANGLE_COMPONENT_TLS_WRAPPER:
915 case DEMANGLE_COMPONENT_REFTEMP:
916 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
917 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
918 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
919 case DEMANGLE_COMPONENT_POINTER:
920 case DEMANGLE_COMPONENT_REFERENCE:
921 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
922 case DEMANGLE_COMPONENT_COMPLEX:
923 case DEMANGLE_COMPONENT_IMAGINARY:
924 case DEMANGLE_COMPONENT_VENDOR_TYPE:
925 case DEMANGLE_COMPONENT_CAST:
926 case DEMANGLE_COMPONENT_CONVERSION:
927 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
928 case DEMANGLE_COMPONENT_DECLTYPE:
929 case DEMANGLE_COMPONENT_PACK_EXPANSION:
930 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
931 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
932 case DEMANGLE_COMPONENT_NULLARY:
933 case DEMANGLE_COMPONENT_TRINARY_ARG2:
934 if (left == NULL)
935 return NULL;
936 break;
938 /* This needs a right parameter, but the left parameter can be
939 empty. */
940 case DEMANGLE_COMPONENT_ARRAY_TYPE:
941 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
942 if (right == NULL)
943 return NULL;
944 break;
946 /* These are allowed to have no parameters--in some cases they
947 will be filled in later. */
948 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
949 case DEMANGLE_COMPONENT_RESTRICT:
950 case DEMANGLE_COMPONENT_VOLATILE:
951 case DEMANGLE_COMPONENT_CONST:
952 case DEMANGLE_COMPONENT_RESTRICT_THIS:
953 case DEMANGLE_COMPONENT_VOLATILE_THIS:
954 case DEMANGLE_COMPONENT_CONST_THIS:
955 case DEMANGLE_COMPONENT_REFERENCE_THIS:
956 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
957 case DEMANGLE_COMPONENT_ARGLIST:
958 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
959 break;
961 /* Other types should not be seen here. */
962 default:
963 return NULL;
966 p = d_make_empty (di);
967 if (p != NULL)
969 p->type = type;
970 p->u.s_binary.left = left;
971 p->u.s_binary.right = right;
973 return p;
976 /* Add a new demangle mangled name component. */
978 static struct demangle_component *
979 d_make_demangle_mangled_name (struct d_info *di, const char *s)
981 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
982 return d_make_name (di, s, strlen (s));
983 d_advance (di, 2);
984 return d_encoding (di, 0);
987 /* Add a new name component. */
989 static struct demangle_component *
990 d_make_name (struct d_info *di, const char *s, int len)
992 struct demangle_component *p;
994 p = d_make_empty (di);
995 if (! cplus_demangle_fill_name (p, s, len))
996 return NULL;
997 return p;
1000 /* Add a new builtin type component. */
1002 static struct demangle_component *
1003 d_make_builtin_type (struct d_info *di,
1004 const struct demangle_builtin_type_info *type)
1006 struct demangle_component *p;
1008 if (type == NULL)
1009 return NULL;
1010 p = d_make_empty (di);
1011 if (p != NULL)
1013 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1014 p->u.s_builtin.type = type;
1016 return p;
1019 /* Add a new operator component. */
1021 static struct demangle_component *
1022 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1024 struct demangle_component *p;
1026 p = d_make_empty (di);
1027 if (p != NULL)
1029 p->type = DEMANGLE_COMPONENT_OPERATOR;
1030 p->u.s_operator.op = op;
1032 return p;
1035 /* Add a new extended operator component. */
1037 static struct demangle_component *
1038 d_make_extended_operator (struct d_info *di, int args,
1039 struct demangle_component *name)
1041 struct demangle_component *p;
1043 p = d_make_empty (di);
1044 if (! cplus_demangle_fill_extended_operator (p, args, name))
1045 return NULL;
1046 return p;
1049 static struct demangle_component *
1050 d_make_default_arg (struct d_info *di, int num,
1051 struct demangle_component *sub)
1053 struct demangle_component *p = d_make_empty (di);
1054 if (p)
1056 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1057 p->u.s_unary_num.num = num;
1058 p->u.s_unary_num.sub = sub;
1060 return p;
1063 /* Add a new constructor component. */
1065 static struct demangle_component *
1066 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1067 struct demangle_component *name)
1069 struct demangle_component *p;
1071 p = d_make_empty (di);
1072 if (! cplus_demangle_fill_ctor (p, kind, name))
1073 return NULL;
1074 return p;
1077 /* Add a new destructor component. */
1079 static struct demangle_component *
1080 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1081 struct demangle_component *name)
1083 struct demangle_component *p;
1085 p = d_make_empty (di);
1086 if (! cplus_demangle_fill_dtor (p, kind, name))
1087 return NULL;
1088 return p;
1091 /* Add a new template parameter. */
1093 static struct demangle_component *
1094 d_make_template_param (struct d_info *di, long i)
1096 struct demangle_component *p;
1098 p = d_make_empty (di);
1099 if (p != NULL)
1101 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1102 p->u.s_number.number = i;
1104 return p;
1107 /* Add a new function parameter. */
1109 static struct demangle_component *
1110 d_make_function_param (struct d_info *di, long i)
1112 struct demangle_component *p;
1114 p = d_make_empty (di);
1115 if (p != NULL)
1117 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1118 p->u.s_number.number = i;
1120 return p;
1123 /* Add a new standard substitution component. */
1125 static struct demangle_component *
1126 d_make_sub (struct d_info *di, const char *name, int len)
1128 struct demangle_component *p;
1130 p = d_make_empty (di);
1131 if (p != NULL)
1133 p->type = DEMANGLE_COMPONENT_SUB_STD;
1134 p->u.s_string.string = name;
1135 p->u.s_string.len = len;
1137 return p;
1140 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1142 TOP_LEVEL is non-zero when called at the top level. */
1144 CP_STATIC_IF_GLIBCPP_V3
1145 struct demangle_component *
1146 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1148 struct demangle_component *p;
1150 if (! d_check_char (di, '_')
1151 /* Allow missing _ if not at toplevel to work around a
1152 bug in G++ abi-version=2 mangling; see the comment in
1153 write_template_arg. */
1154 && top_level)
1155 return NULL;
1156 if (! d_check_char (di, 'Z'))
1157 return NULL;
1158 p = d_encoding (di, top_level);
1160 /* If at top level and parsing parameters, check for a clone
1161 suffix. */
1162 if (top_level && (di->options & DMGL_PARAMS) != 0)
1163 while (d_peek_char (di) == '.'
1164 && (IS_LOWER (d_peek_next_char (di))
1165 || d_peek_next_char (di) == '_'
1166 || IS_DIGIT (d_peek_next_char (di))))
1167 p = d_clone_suffix (di, p);
1169 return p;
1172 /* Return whether a function should have a return type. The argument
1173 is the function name, which may be qualified in various ways. The
1174 rules are that template functions have return types with some
1175 exceptions, function types which are not part of a function name
1176 mangling have return types with some exceptions, and non-template
1177 function names do not have return types. The exceptions are that
1178 constructors, destructors, and conversion operators do not have
1179 return types. */
1181 static int
1182 has_return_type (struct demangle_component *dc)
1184 if (dc == NULL)
1185 return 0;
1186 switch (dc->type)
1188 default:
1189 return 0;
1190 case DEMANGLE_COMPONENT_TEMPLATE:
1191 return ! is_ctor_dtor_or_conversion (d_left (dc));
1192 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1193 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1194 case DEMANGLE_COMPONENT_CONST_THIS:
1195 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1196 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1197 return has_return_type (d_left (dc));
1201 /* Return whether a name is a constructor, a destructor, or a
1202 conversion operator. */
1204 static int
1205 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1207 if (dc == NULL)
1208 return 0;
1209 switch (dc->type)
1211 default:
1212 return 0;
1213 case DEMANGLE_COMPONENT_QUAL_NAME:
1214 case DEMANGLE_COMPONENT_LOCAL_NAME:
1215 return is_ctor_dtor_or_conversion (d_right (dc));
1216 case DEMANGLE_COMPONENT_CTOR:
1217 case DEMANGLE_COMPONENT_DTOR:
1218 case DEMANGLE_COMPONENT_CONVERSION:
1219 return 1;
1223 /* <encoding> ::= <(function) name> <bare-function-type>
1224 ::= <(data) name>
1225 ::= <special-name>
1227 TOP_LEVEL is non-zero when called at the top level, in which case
1228 if DMGL_PARAMS is not set we do not demangle the function
1229 parameters. We only set this at the top level, because otherwise
1230 we would not correctly demangle names in local scopes. */
1232 static struct demangle_component *
1233 d_encoding (struct d_info *di, int top_level)
1235 char peek = d_peek_char (di);
1237 if (peek == 'G' || peek == 'T')
1238 return d_special_name (di);
1239 else
1241 struct demangle_component *dc;
1243 dc = d_name (di);
1245 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1247 /* Strip off any initial CV-qualifiers, as they really apply
1248 to the `this' parameter, and they were not output by the
1249 v2 demangler without DMGL_PARAMS. */
1250 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1251 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1252 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1253 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1254 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1255 dc = d_left (dc);
1257 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1258 there may be CV-qualifiers on its right argument which
1259 really apply here; this happens when parsing a class
1260 which is local to a function. */
1261 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1263 struct demangle_component *dcr;
1265 dcr = d_right (dc);
1266 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1267 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1268 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1269 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1270 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1271 dcr = d_left (dcr);
1272 dc->u.s_binary.right = dcr;
1275 return dc;
1278 peek = d_peek_char (di);
1279 if (dc == NULL || peek == '\0' || peek == 'E')
1280 return dc;
1281 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1282 d_bare_function_type (di, has_return_type (dc)));
1286 /* <tagged-name> ::= <name> B <source-name> */
1288 static struct demangle_component *
1289 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1291 char peek;
1292 while (peek = d_peek_char (di),
1293 peek == 'B')
1295 struct demangle_component *tag;
1296 d_advance (di, 1);
1297 tag = d_source_name (di);
1298 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1300 return dc;
1303 /* <name> ::= <nested-name>
1304 ::= <unscoped-name>
1305 ::= <unscoped-template-name> <template-args>
1306 ::= <local-name>
1308 <unscoped-name> ::= <unqualified-name>
1309 ::= St <unqualified-name>
1311 <unscoped-template-name> ::= <unscoped-name>
1312 ::= <substitution>
1315 static struct demangle_component *
1316 d_name (struct d_info *di)
1318 char peek = d_peek_char (di);
1319 struct demangle_component *dc;
1321 switch (peek)
1323 case 'N':
1324 return d_nested_name (di);
1326 case 'Z':
1327 return d_local_name (di);
1329 case 'U':
1330 return d_unqualified_name (di);
1332 case 'S':
1334 int subst;
1336 if (d_peek_next_char (di) != 't')
1338 dc = d_substitution (di, 0);
1339 subst = 1;
1341 else
1343 d_advance (di, 2);
1344 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1345 d_make_name (di, "std", 3),
1346 d_unqualified_name (di));
1347 di->expansion += 3;
1348 subst = 0;
1351 if (d_peek_char (di) != 'I')
1353 /* The grammar does not permit this case to occur if we
1354 called d_substitution() above (i.e., subst == 1). We
1355 don't bother to check. */
1357 else
1359 /* This is <template-args>, which means that we just saw
1360 <unscoped-template-name>, which is a substitution
1361 candidate if we didn't just get it from a
1362 substitution. */
1363 if (! subst)
1365 if (! d_add_substitution (di, dc))
1366 return NULL;
1368 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1369 d_template_args (di));
1372 return dc;
1375 case 'L':
1376 default:
1377 dc = d_unqualified_name (di);
1378 if (d_peek_char (di) == 'I')
1380 /* This is <template-args>, which means that we just saw
1381 <unscoped-template-name>, which is a substitution
1382 candidate. */
1383 if (! d_add_substitution (di, dc))
1384 return NULL;
1385 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1386 d_template_args (di));
1388 return dc;
1392 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1393 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1396 static struct demangle_component *
1397 d_nested_name (struct d_info *di)
1399 struct demangle_component *ret;
1400 struct demangle_component **pret;
1401 struct demangle_component *rqual;
1403 if (! d_check_char (di, 'N'))
1404 return NULL;
1406 pret = d_cv_qualifiers (di, &ret, 1);
1407 if (pret == NULL)
1408 return NULL;
1410 /* Parse the ref-qualifier now and then attach it
1411 once we have something to attach it to. */
1412 rqual = d_ref_qualifier (di, NULL);
1414 *pret = d_prefix (di);
1415 if (*pret == NULL)
1416 return NULL;
1418 if (rqual)
1420 d_left (rqual) = ret;
1421 ret = rqual;
1424 if (! d_check_char (di, 'E'))
1425 return NULL;
1427 return ret;
1430 /* <prefix> ::= <prefix> <unqualified-name>
1431 ::= <template-prefix> <template-args>
1432 ::= <template-param>
1433 ::= <decltype>
1435 ::= <substitution>
1437 <template-prefix> ::= <prefix> <(template) unqualified-name>
1438 ::= <template-param>
1439 ::= <substitution>
1442 static struct demangle_component *
1443 d_prefix (struct d_info *di)
1445 struct demangle_component *ret = NULL;
1447 while (1)
1449 char peek;
1450 enum demangle_component_type comb_type;
1451 struct demangle_component *dc;
1453 peek = d_peek_char (di);
1454 if (peek == '\0')
1455 return NULL;
1457 /* The older code accepts a <local-name> here, but I don't see
1458 that in the grammar. The older code does not accept a
1459 <template-param> here. */
1461 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1462 if (peek == 'D')
1464 char peek2 = d_peek_next_char (di);
1465 if (peek2 == 'T' || peek2 == 't')
1466 /* Decltype. */
1467 dc = cplus_demangle_type (di);
1468 else
1469 /* Destructor name. */
1470 dc = d_unqualified_name (di);
1472 else if (IS_DIGIT (peek)
1473 || IS_LOWER (peek)
1474 || peek == 'C'
1475 || peek == 'U'
1476 || peek == 'L')
1477 dc = d_unqualified_name (di);
1478 else if (peek == 'S')
1479 dc = d_substitution (di, 1);
1480 else if (peek == 'I')
1482 if (ret == NULL)
1483 return NULL;
1484 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1485 dc = d_template_args (di);
1487 else if (peek == 'T')
1488 dc = d_template_param (di);
1489 else if (peek == 'E')
1490 return ret;
1491 else if (peek == 'M')
1493 /* Initializer scope for a lambda. We don't need to represent
1494 this; the normal code will just treat the variable as a type
1495 scope, which gives appropriate output. */
1496 if (ret == NULL)
1497 return NULL;
1498 d_advance (di, 1);
1499 continue;
1501 else
1502 return NULL;
1504 if (ret == NULL)
1505 ret = dc;
1506 else
1507 ret = d_make_comp (di, comb_type, ret, dc);
1509 if (peek != 'S' && d_peek_char (di) != 'E')
1511 if (! d_add_substitution (di, ret))
1512 return NULL;
1517 /* <unqualified-name> ::= <operator-name>
1518 ::= <ctor-dtor-name>
1519 ::= <source-name>
1520 ::= <local-source-name>
1522 <local-source-name> ::= L <source-name> <discriminator>
1525 static struct demangle_component *
1526 d_unqualified_name (struct d_info *di)
1528 struct demangle_component *ret;
1529 char peek;
1531 peek = d_peek_char (di);
1532 if (IS_DIGIT (peek))
1533 ret = d_source_name (di);
1534 else if (IS_LOWER (peek))
1536 ret = d_operator_name (di);
1537 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1539 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1540 if (!strcmp (ret->u.s_operator.op->code, "li"))
1541 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1542 d_source_name (di));
1545 else if (peek == 'C' || peek == 'D')
1546 ret = d_ctor_dtor_name (di);
1547 else if (peek == 'L')
1549 d_advance (di, 1);
1551 ret = d_source_name (di);
1552 if (ret == NULL)
1553 return NULL;
1554 if (! d_discriminator (di))
1555 return NULL;
1557 else if (peek == 'U')
1559 switch (d_peek_next_char (di))
1561 case 'l':
1562 ret = d_lambda (di);
1563 break;
1564 case 't':
1565 ret = d_unnamed_type (di);
1566 break;
1567 default:
1568 return NULL;
1571 else
1572 return NULL;
1574 if (d_peek_char (di) == 'B')
1575 ret = d_abi_tags (di, ret);
1576 return ret;
1579 /* <source-name> ::= <(positive length) number> <identifier> */
1581 static struct demangle_component *
1582 d_source_name (struct d_info *di)
1584 long len;
1585 struct demangle_component *ret;
1587 len = d_number (di);
1588 if (len <= 0)
1589 return NULL;
1590 ret = d_identifier (di, len);
1591 di->last_name = ret;
1592 return ret;
1595 /* number ::= [n] <(non-negative decimal integer)> */
1597 static long
1598 d_number (struct d_info *di)
1600 int negative;
1601 char peek;
1602 long ret;
1604 negative = 0;
1605 peek = d_peek_char (di);
1606 if (peek == 'n')
1608 negative = 1;
1609 d_advance (di, 1);
1610 peek = d_peek_char (di);
1613 ret = 0;
1614 while (1)
1616 if (! IS_DIGIT (peek))
1618 if (negative)
1619 ret = - ret;
1620 return ret;
1622 ret = ret * 10 + peek - '0';
1623 d_advance (di, 1);
1624 peek = d_peek_char (di);
1628 /* Like d_number, but returns a demangle_component. */
1630 static struct demangle_component *
1631 d_number_component (struct d_info *di)
1633 struct demangle_component *ret = d_make_empty (di);
1634 if (ret)
1636 ret->type = DEMANGLE_COMPONENT_NUMBER;
1637 ret->u.s_number.number = d_number (di);
1639 return ret;
1642 /* identifier ::= <(unqualified source code identifier)> */
1644 static struct demangle_component *
1645 d_identifier (struct d_info *di, int len)
1647 const char *name;
1649 name = d_str (di);
1651 if (di->send - name < len)
1652 return NULL;
1654 d_advance (di, len);
1656 /* A Java mangled name may have a trailing '$' if it is a C++
1657 keyword. This '$' is not included in the length count. We just
1658 ignore the '$'. */
1659 if ((di->options & DMGL_JAVA) != 0
1660 && d_peek_char (di) == '$')
1661 d_advance (di, 1);
1663 /* Look for something which looks like a gcc encoding of an
1664 anonymous namespace, and replace it with a more user friendly
1665 name. */
1666 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1667 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1668 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1670 const char *s;
1672 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1673 if ((*s == '.' || *s == '_' || *s == '$')
1674 && s[1] == 'N')
1676 di->expansion -= len - sizeof "(anonymous namespace)";
1677 return d_make_name (di, "(anonymous namespace)",
1678 sizeof "(anonymous namespace)" - 1);
1682 return d_make_name (di, name, len);
1685 /* operator_name ::= many different two character encodings.
1686 ::= cv <type>
1687 ::= v <digit> <source-name>
1689 This list is sorted for binary search. */
1691 #define NL(s) s, (sizeof s) - 1
1693 CP_STATIC_IF_GLIBCPP_V3
1694 const struct demangle_operator_info cplus_demangle_operators[] =
1696 { "aN", NL ("&="), 2 },
1697 { "aS", NL ("="), 2 },
1698 { "aa", NL ("&&"), 2 },
1699 { "ad", NL ("&"), 1 },
1700 { "an", NL ("&"), 2 },
1701 { "at", NL ("alignof "), 1 },
1702 { "az", NL ("alignof "), 1 },
1703 { "cc", NL ("const_cast"), 2 },
1704 { "cl", NL ("()"), 2 },
1705 { "cm", NL (","), 2 },
1706 { "co", NL ("~"), 1 },
1707 { "dV", NL ("/="), 2 },
1708 { "da", NL ("delete[] "), 1 },
1709 { "dc", NL ("dynamic_cast"), 2 },
1710 { "de", NL ("*"), 1 },
1711 { "dl", NL ("delete "), 1 },
1712 { "ds", NL (".*"), 2 },
1713 { "dt", NL ("."), 2 },
1714 { "dv", NL ("/"), 2 },
1715 { "eO", NL ("^="), 2 },
1716 { "eo", NL ("^"), 2 },
1717 { "eq", NL ("=="), 2 },
1718 { "ge", NL (">="), 2 },
1719 { "gs", NL ("::"), 1 },
1720 { "gt", NL (">"), 2 },
1721 { "ix", NL ("[]"), 2 },
1722 { "lS", NL ("<<="), 2 },
1723 { "le", NL ("<="), 2 },
1724 { "li", NL ("operator\"\" "), 1 },
1725 { "ls", NL ("<<"), 2 },
1726 { "lt", NL ("<"), 2 },
1727 { "mI", NL ("-="), 2 },
1728 { "mL", NL ("*="), 2 },
1729 { "mi", NL ("-"), 2 },
1730 { "ml", NL ("*"), 2 },
1731 { "mm", NL ("--"), 1 },
1732 { "na", NL ("new[]"), 3 },
1733 { "ne", NL ("!="), 2 },
1734 { "ng", NL ("-"), 1 },
1735 { "nt", NL ("!"), 1 },
1736 { "nw", NL ("new"), 3 },
1737 { "oR", NL ("|="), 2 },
1738 { "oo", NL ("||"), 2 },
1739 { "or", NL ("|"), 2 },
1740 { "pL", NL ("+="), 2 },
1741 { "pl", NL ("+"), 2 },
1742 { "pm", NL ("->*"), 2 },
1743 { "pp", NL ("++"), 1 },
1744 { "ps", NL ("+"), 1 },
1745 { "pt", NL ("->"), 2 },
1746 { "qu", NL ("?"), 3 },
1747 { "rM", NL ("%="), 2 },
1748 { "rS", NL (">>="), 2 },
1749 { "rc", NL ("reinterpret_cast"), 2 },
1750 { "rm", NL ("%"), 2 },
1751 { "rs", NL (">>"), 2 },
1752 { "sc", NL ("static_cast"), 2 },
1753 { "st", NL ("sizeof "), 1 },
1754 { "sz", NL ("sizeof "), 1 },
1755 { "tr", NL ("throw"), 0 },
1756 { "tw", NL ("throw "), 1 },
1757 { NULL, NULL, 0, 0 }
1760 static struct demangle_component *
1761 d_operator_name (struct d_info *di)
1763 char c1;
1764 char c2;
1766 c1 = d_next_char (di);
1767 c2 = d_next_char (di);
1768 if (c1 == 'v' && IS_DIGIT (c2))
1769 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1770 else if (c1 == 'c' && c2 == 'v')
1772 struct demangle_component *type;
1773 int was_conversion = di->is_conversion;
1774 struct demangle_component *res;
1776 di->is_conversion = ! di->is_expression;
1777 type = cplus_demangle_type (di);
1778 if (di->is_conversion)
1779 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
1780 else
1781 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1782 di->is_conversion = was_conversion;
1783 return res;
1785 else
1787 /* LOW is the inclusive lower bound. */
1788 int low = 0;
1789 /* HIGH is the exclusive upper bound. We subtract one to ignore
1790 the sentinel at the end of the array. */
1791 int high = ((sizeof (cplus_demangle_operators)
1792 / sizeof (cplus_demangle_operators[0]))
1793 - 1);
1795 while (1)
1797 int i;
1798 const struct demangle_operator_info *p;
1800 i = low + (high - low) / 2;
1801 p = cplus_demangle_operators + i;
1803 if (c1 == p->code[0] && c2 == p->code[1])
1804 return d_make_operator (di, p);
1806 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1807 high = i;
1808 else
1809 low = i + 1;
1810 if (low == high)
1811 return NULL;
1816 static struct demangle_component *
1817 d_make_character (struct d_info *di, int c)
1819 struct demangle_component *p;
1820 p = d_make_empty (di);
1821 if (p != NULL)
1823 p->type = DEMANGLE_COMPONENT_CHARACTER;
1824 p->u.s_character.character = c;
1826 return p;
1829 static struct demangle_component *
1830 d_java_resource (struct d_info *di)
1832 struct demangle_component *p = NULL;
1833 struct demangle_component *next = NULL;
1834 long len, i;
1835 char c;
1836 const char *str;
1838 len = d_number (di);
1839 if (len <= 1)
1840 return NULL;
1842 /* Eat the leading '_'. */
1843 if (d_next_char (di) != '_')
1844 return NULL;
1845 len--;
1847 str = d_str (di);
1848 i = 0;
1850 while (len > 0)
1852 c = str[i];
1853 if (!c)
1854 return NULL;
1856 /* Each chunk is either a '$' escape... */
1857 if (c == '$')
1859 i++;
1860 switch (str[i++])
1862 case 'S':
1863 c = '/';
1864 break;
1865 case '_':
1866 c = '.';
1867 break;
1868 case '$':
1869 c = '$';
1870 break;
1871 default:
1872 return NULL;
1874 next = d_make_character (di, c);
1875 d_advance (di, i);
1876 str = d_str (di);
1877 len -= i;
1878 i = 0;
1879 if (next == NULL)
1880 return NULL;
1882 /* ... or a sequence of characters. */
1883 else
1885 while (i < len && str[i] && str[i] != '$')
1886 i++;
1888 next = d_make_name (di, str, i);
1889 d_advance (di, i);
1890 str = d_str (di);
1891 len -= i;
1892 i = 0;
1893 if (next == NULL)
1894 return NULL;
1897 if (p == NULL)
1898 p = next;
1899 else
1901 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1902 if (p == NULL)
1903 return NULL;
1907 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1909 return p;
1912 /* <special-name> ::= TV <type>
1913 ::= TT <type>
1914 ::= TI <type>
1915 ::= TS <type>
1916 ::= GV <(object) name>
1917 ::= T <call-offset> <(base) encoding>
1918 ::= Tc <call-offset> <call-offset> <(base) encoding>
1919 Also g++ extensions:
1920 ::= TC <type> <(offset) number> _ <(base) type>
1921 ::= TF <type>
1922 ::= TJ <type>
1923 ::= GR <name>
1924 ::= GA <encoding>
1925 ::= Gr <resource name>
1926 ::= GTt <encoding>
1927 ::= GTn <encoding>
1930 static struct demangle_component *
1931 d_special_name (struct d_info *di)
1933 di->expansion += 20;
1934 if (d_check_char (di, 'T'))
1936 switch (d_next_char (di))
1938 case 'V':
1939 di->expansion -= 5;
1940 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1941 cplus_demangle_type (di), NULL);
1942 case 'T':
1943 di->expansion -= 10;
1944 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1945 cplus_demangle_type (di), NULL);
1946 case 'I':
1947 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1948 cplus_demangle_type (di), NULL);
1949 case 'S':
1950 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1951 cplus_demangle_type (di), NULL);
1953 case 'h':
1954 if (! d_call_offset (di, 'h'))
1955 return NULL;
1956 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1957 d_encoding (di, 0), NULL);
1959 case 'v':
1960 if (! d_call_offset (di, 'v'))
1961 return NULL;
1962 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1963 d_encoding (di, 0), NULL);
1965 case 'c':
1966 if (! d_call_offset (di, '\0'))
1967 return NULL;
1968 if (! d_call_offset (di, '\0'))
1969 return NULL;
1970 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1971 d_encoding (di, 0), NULL);
1973 case 'C':
1975 struct demangle_component *derived_type;
1976 long offset;
1977 struct demangle_component *base_type;
1979 derived_type = cplus_demangle_type (di);
1980 offset = d_number (di);
1981 if (offset < 0)
1982 return NULL;
1983 if (! d_check_char (di, '_'))
1984 return NULL;
1985 base_type = cplus_demangle_type (di);
1986 /* We don't display the offset. FIXME: We should display
1987 it in verbose mode. */
1988 di->expansion += 5;
1989 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1990 base_type, derived_type);
1993 case 'F':
1994 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1995 cplus_demangle_type (di), NULL);
1996 case 'J':
1997 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1998 cplus_demangle_type (di), NULL);
2000 case 'H':
2001 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2002 d_name (di), NULL);
2004 case 'W':
2005 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2006 d_name (di), NULL);
2008 default:
2009 return NULL;
2012 else if (d_check_char (di, 'G'))
2014 switch (d_next_char (di))
2016 case 'V':
2017 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2019 case 'R':
2021 struct demangle_component *name = d_name (di);
2022 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2023 d_number_component (di));
2026 case 'A':
2027 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2028 d_encoding (di, 0), NULL);
2030 case 'T':
2031 switch (d_next_char (di))
2033 case 'n':
2034 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2035 d_encoding (di, 0), NULL);
2036 default:
2037 /* ??? The proposal is that other letters (such as 'h') stand
2038 for different variants of transaction cloning, such as
2039 compiling directly for hardware transaction support. But
2040 they still should all be transactional clones of some sort
2041 so go ahead and call them that. */
2042 case 't':
2043 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2044 d_encoding (di, 0), NULL);
2047 case 'r':
2048 return d_java_resource (di);
2050 default:
2051 return NULL;
2054 else
2055 return NULL;
2058 /* <call-offset> ::= h <nv-offset> _
2059 ::= v <v-offset> _
2061 <nv-offset> ::= <(offset) number>
2063 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2065 The C parameter, if not '\0', is a character we just read which is
2066 the start of the <call-offset>.
2068 We don't display the offset information anywhere. FIXME: We should
2069 display it in verbose mode. */
2071 static int
2072 d_call_offset (struct d_info *di, int c)
2074 if (c == '\0')
2075 c = d_next_char (di);
2077 if (c == 'h')
2078 d_number (di);
2079 else if (c == 'v')
2081 d_number (di);
2082 if (! d_check_char (di, '_'))
2083 return 0;
2084 d_number (di);
2086 else
2087 return 0;
2089 if (! d_check_char (di, '_'))
2090 return 0;
2092 return 1;
2095 /* <ctor-dtor-name> ::= C1
2096 ::= C2
2097 ::= C3
2098 ::= D0
2099 ::= D1
2100 ::= D2
2103 static struct demangle_component *
2104 d_ctor_dtor_name (struct d_info *di)
2106 if (di->last_name != NULL)
2108 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2109 di->expansion += di->last_name->u.s_name.len;
2110 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2111 di->expansion += di->last_name->u.s_string.len;
2113 switch (d_peek_char (di))
2115 case 'C':
2117 enum gnu_v3_ctor_kinds kind;
2119 switch (d_peek_next_char (di))
2121 case '1':
2122 kind = gnu_v3_complete_object_ctor;
2123 break;
2124 case '2':
2125 kind = gnu_v3_base_object_ctor;
2126 break;
2127 case '3':
2128 kind = gnu_v3_complete_object_allocating_ctor;
2129 break;
2130 case '4':
2131 kind = gnu_v3_unified_ctor;
2132 break;
2133 case '5':
2134 kind = gnu_v3_object_ctor_group;
2135 break;
2136 default:
2137 return NULL;
2139 d_advance (di, 2);
2140 return d_make_ctor (di, kind, di->last_name);
2143 case 'D':
2145 enum gnu_v3_dtor_kinds kind;
2147 switch (d_peek_next_char (di))
2149 case '0':
2150 kind = gnu_v3_deleting_dtor;
2151 break;
2152 case '1':
2153 kind = gnu_v3_complete_object_dtor;
2154 break;
2155 case '2':
2156 kind = gnu_v3_base_object_dtor;
2157 break;
2158 /* digit '3' is not used */
2159 case '4':
2160 kind = gnu_v3_unified_dtor;
2161 break;
2162 case '5':
2163 kind = gnu_v3_object_dtor_group;
2164 break;
2165 default:
2166 return NULL;
2168 d_advance (di, 2);
2169 return d_make_dtor (di, kind, di->last_name);
2172 default:
2173 return NULL;
2177 /* <type> ::= <builtin-type>
2178 ::= <function-type>
2179 ::= <class-enum-type>
2180 ::= <array-type>
2181 ::= <pointer-to-member-type>
2182 ::= <template-param>
2183 ::= <template-template-param> <template-args>
2184 ::= <substitution>
2185 ::= <CV-qualifiers> <type>
2186 ::= P <type>
2187 ::= R <type>
2188 ::= O <type> (C++0x)
2189 ::= C <type>
2190 ::= G <type>
2191 ::= U <source-name> <type>
2193 <builtin-type> ::= various one letter codes
2194 ::= u <source-name>
2197 CP_STATIC_IF_GLIBCPP_V3
2198 const struct demangle_builtin_type_info
2199 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2201 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2202 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2203 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2204 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2205 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2206 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2207 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2208 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2209 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2210 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2211 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2212 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2213 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2214 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2215 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2216 D_PRINT_DEFAULT },
2217 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2218 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2219 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2220 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2221 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2222 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2223 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2224 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2225 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2226 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2227 D_PRINT_UNSIGNED_LONG_LONG },
2228 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2229 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2230 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2231 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2232 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2233 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2234 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2235 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2236 D_PRINT_DEFAULT },
2239 CP_STATIC_IF_GLIBCPP_V3
2240 struct demangle_component *
2241 cplus_demangle_type (struct d_info *di)
2243 char peek;
2244 struct demangle_component *ret;
2245 int can_subst;
2247 /* The ABI specifies that when CV-qualifiers are used, the base type
2248 is substitutable, and the fully qualified type is substitutable,
2249 but the base type with a strict subset of the CV-qualifiers is
2250 not substitutable. The natural recursive implementation of the
2251 CV-qualifiers would cause subsets to be substitutable, so instead
2252 we pull them all off now.
2254 FIXME: The ABI says that order-insensitive vendor qualifiers
2255 should be handled in the same way, but we have no way to tell
2256 which vendor qualifiers are order-insensitive and which are
2257 order-sensitive. So we just assume that they are all
2258 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2259 __vector, and it treats it as order-sensitive when mangling
2260 names. */
2262 peek = d_peek_char (di);
2263 if (peek == 'r' || peek == 'V' || peek == 'K')
2265 struct demangle_component **pret;
2267 pret = d_cv_qualifiers (di, &ret, 0);
2268 if (pret == NULL)
2269 return NULL;
2270 if (d_peek_char (di) == 'F')
2272 /* cv-qualifiers before a function type apply to 'this',
2273 so avoid adding the unqualified function type to
2274 the substitution list. */
2275 *pret = d_function_type (di);
2277 else
2278 *pret = cplus_demangle_type (di);
2279 if (!*pret)
2280 return NULL;
2281 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2282 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2284 /* Move the ref-qualifier outside the cv-qualifiers so that
2285 they are printed in the right order. */
2286 struct demangle_component *fn = d_left (*pret);
2287 d_left (*pret) = ret;
2288 ret = *pret;
2289 *pret = fn;
2291 if (! d_add_substitution (di, ret))
2292 return NULL;
2293 return ret;
2296 can_subst = 1;
2298 switch (peek)
2300 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2301 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2302 case 'o': case 's': case 't':
2303 case 'v': case 'w': case 'x': case 'y': case 'z':
2304 ret = d_make_builtin_type (di,
2305 &cplus_demangle_builtin_types[peek - 'a']);
2306 di->expansion += ret->u.s_builtin.type->len;
2307 can_subst = 0;
2308 d_advance (di, 1);
2309 break;
2311 case 'u':
2312 d_advance (di, 1);
2313 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2314 d_source_name (di), NULL);
2315 break;
2317 case 'F':
2318 ret = d_function_type (di);
2319 break;
2321 case '0': case '1': case '2': case '3': case '4':
2322 case '5': case '6': case '7': case '8': case '9':
2323 case 'N':
2324 case 'Z':
2325 ret = d_class_enum_type (di);
2326 break;
2328 case 'A':
2329 ret = d_array_type (di);
2330 break;
2332 case 'M':
2333 ret = d_pointer_to_member_type (di);
2334 break;
2336 case 'T':
2337 ret = d_template_param (di);
2338 if (d_peek_char (di) == 'I')
2340 /* This may be <template-template-param> <template-args>.
2341 If this is the type for a conversion operator, we can
2342 have a <template-template-param> here only by following
2343 a derivation like this:
2345 <nested-name>
2346 -> <template-prefix> <template-args>
2347 -> <prefix> <template-unqualified-name> <template-args>
2348 -> <unqualified-name> <template-unqualified-name> <template-args>
2349 -> <source-name> <template-unqualified-name> <template-args>
2350 -> <source-name> <operator-name> <template-args>
2351 -> <source-name> cv <type> <template-args>
2352 -> <source-name> cv <template-template-param> <template-args> <template-args>
2354 where the <template-args> is followed by another.
2355 Otherwise, we must have a derivation like this:
2357 <nested-name>
2358 -> <template-prefix> <template-args>
2359 -> <prefix> <template-unqualified-name> <template-args>
2360 -> <unqualified-name> <template-unqualified-name> <template-args>
2361 -> <source-name> <template-unqualified-name> <template-args>
2362 -> <source-name> <operator-name> <template-args>
2363 -> <source-name> cv <type> <template-args>
2364 -> <source-name> cv <template-param> <template-args>
2366 where we need to leave the <template-args> to be processed
2367 by d_prefix (following the <template-prefix>).
2369 The <template-template-param> part is a substitution
2370 candidate. */
2371 if (! di->is_conversion)
2373 if (! d_add_substitution (di, ret))
2374 return NULL;
2375 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2376 d_template_args (di));
2378 else
2380 struct demangle_component *args;
2381 struct d_info_checkpoint checkpoint;
2383 d_checkpoint (di, &checkpoint);
2384 args = d_template_args (di);
2385 if (d_peek_char (di) == 'I')
2387 if (! d_add_substitution (di, ret))
2388 return NULL;
2389 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2390 args);
2392 else
2393 d_backtrack (di, &checkpoint);
2396 break;
2398 case 'S':
2399 /* If this is a special substitution, then it is the start of
2400 <class-enum-type>. */
2402 char peek_next;
2404 peek_next = d_peek_next_char (di);
2405 if (IS_DIGIT (peek_next)
2406 || peek_next == '_'
2407 || IS_UPPER (peek_next))
2409 ret = d_substitution (di, 0);
2410 /* The substituted name may have been a template name and
2411 may be followed by tepmlate args. */
2412 if (d_peek_char (di) == 'I')
2413 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2414 d_template_args (di));
2415 else
2416 can_subst = 0;
2418 else
2420 ret = d_class_enum_type (di);
2421 /* If the substitution was a complete type, then it is not
2422 a new substitution candidate. However, if the
2423 substitution was followed by template arguments, then
2424 the whole thing is a substitution candidate. */
2425 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2426 can_subst = 0;
2429 break;
2431 case 'O':
2432 d_advance (di, 1);
2433 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2434 cplus_demangle_type (di), NULL);
2435 break;
2437 case 'P':
2438 d_advance (di, 1);
2439 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2440 cplus_demangle_type (di), NULL);
2441 break;
2443 case 'R':
2444 d_advance (di, 1);
2445 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2446 cplus_demangle_type (di), NULL);
2447 break;
2449 case 'C':
2450 d_advance (di, 1);
2451 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2452 cplus_demangle_type (di), NULL);
2453 break;
2455 case 'G':
2456 d_advance (di, 1);
2457 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2458 cplus_demangle_type (di), NULL);
2459 break;
2461 case 'U':
2462 d_advance (di, 1);
2463 ret = d_source_name (di);
2464 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2465 cplus_demangle_type (di), ret);
2466 break;
2468 case 'D':
2469 can_subst = 0;
2470 d_advance (di, 1);
2471 peek = d_next_char (di);
2472 switch (peek)
2474 case 'T':
2475 case 't':
2476 /* decltype (expression) */
2477 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2478 d_expression (di), NULL);
2479 if (ret && d_next_char (di) != 'E')
2480 ret = NULL;
2481 can_subst = 1;
2482 break;
2484 case 'p':
2485 /* Pack expansion. */
2486 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2487 cplus_demangle_type (di), NULL);
2488 can_subst = 1;
2489 break;
2491 case 'a':
2492 /* auto */
2493 ret = d_make_name (di, "auto", 4);
2494 break;
2496 case 'f':
2497 /* 32-bit decimal floating point */
2498 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2499 di->expansion += ret->u.s_builtin.type->len;
2500 break;
2501 case 'd':
2502 /* 64-bit DFP */
2503 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2504 di->expansion += ret->u.s_builtin.type->len;
2505 break;
2506 case 'e':
2507 /* 128-bit DFP */
2508 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2509 di->expansion += ret->u.s_builtin.type->len;
2510 break;
2511 case 'h':
2512 /* 16-bit half-precision FP */
2513 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2514 di->expansion += ret->u.s_builtin.type->len;
2515 break;
2516 case 's':
2517 /* char16_t */
2518 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2519 di->expansion += ret->u.s_builtin.type->len;
2520 break;
2521 case 'i':
2522 /* char32_t */
2523 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2524 di->expansion += ret->u.s_builtin.type->len;
2525 break;
2527 case 'F':
2528 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2529 ret = d_make_empty (di);
2530 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2531 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2532 /* For demangling we don't care about the bits. */
2533 d_number (di);
2534 ret->u.s_fixed.length = cplus_demangle_type (di);
2535 if (ret->u.s_fixed.length == NULL)
2536 return NULL;
2537 d_number (di);
2538 peek = d_next_char (di);
2539 ret->u.s_fixed.sat = (peek == 's');
2540 break;
2542 case 'v':
2543 ret = d_vector_type (di);
2544 can_subst = 1;
2545 break;
2547 case 'n':
2548 /* decltype(nullptr) */
2549 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2550 di->expansion += ret->u.s_builtin.type->len;
2551 break;
2553 default:
2554 return NULL;
2556 break;
2558 default:
2559 return NULL;
2562 if (can_subst)
2564 if (! d_add_substitution (di, ret))
2565 return NULL;
2568 return ret;
2571 /* <CV-qualifiers> ::= [r] [V] [K] */
2573 static struct demangle_component **
2574 d_cv_qualifiers (struct d_info *di,
2575 struct demangle_component **pret, int member_fn)
2577 struct demangle_component **pstart;
2578 char peek;
2580 pstart = pret;
2581 peek = d_peek_char (di);
2582 while (peek == 'r' || peek == 'V' || peek == 'K')
2584 enum demangle_component_type t;
2586 d_advance (di, 1);
2587 if (peek == 'r')
2589 t = (member_fn
2590 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2591 : DEMANGLE_COMPONENT_RESTRICT);
2592 di->expansion += sizeof "restrict";
2594 else if (peek == 'V')
2596 t = (member_fn
2597 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2598 : DEMANGLE_COMPONENT_VOLATILE);
2599 di->expansion += sizeof "volatile";
2601 else
2603 t = (member_fn
2604 ? DEMANGLE_COMPONENT_CONST_THIS
2605 : DEMANGLE_COMPONENT_CONST);
2606 di->expansion += sizeof "const";
2609 *pret = d_make_comp (di, t, NULL, NULL);
2610 if (*pret == NULL)
2611 return NULL;
2612 pret = &d_left (*pret);
2614 peek = d_peek_char (di);
2617 if (!member_fn && peek == 'F')
2619 while (pstart != pret)
2621 switch ((*pstart)->type)
2623 case DEMANGLE_COMPONENT_RESTRICT:
2624 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2625 break;
2626 case DEMANGLE_COMPONENT_VOLATILE:
2627 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2628 break;
2629 case DEMANGLE_COMPONENT_CONST:
2630 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2631 break;
2632 default:
2633 break;
2635 pstart = &d_left (*pstart);
2639 return pret;
2642 /* <ref-qualifier> ::= R
2643 ::= O */
2645 static struct demangle_component *
2646 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2648 struct demangle_component *ret = sub;
2649 char peek;
2651 peek = d_peek_char (di);
2652 if (peek == 'R' || peek == 'O')
2654 enum demangle_component_type t;
2655 if (peek == 'R')
2657 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2658 di->expansion += sizeof "&";
2660 else
2662 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2663 di->expansion += sizeof "&&";
2665 d_advance (di, 1);
2667 ret = d_make_comp (di, t, ret, NULL);
2670 return ret;
2673 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2675 static struct demangle_component *
2676 d_function_type (struct d_info *di)
2678 struct demangle_component *ret;
2680 if (! d_check_char (di, 'F'))
2681 return NULL;
2682 if (d_peek_char (di) == 'Y')
2684 /* Function has C linkage. We don't print this information.
2685 FIXME: We should print it in verbose mode. */
2686 d_advance (di, 1);
2688 ret = d_bare_function_type (di, 1);
2689 ret = d_ref_qualifier (di, ret);
2691 if (! d_check_char (di, 'E'))
2692 return NULL;
2693 return ret;
2696 /* <type>+ */
2698 static struct demangle_component *
2699 d_parmlist (struct d_info *di)
2701 struct demangle_component *tl;
2702 struct demangle_component **ptl;
2704 tl = NULL;
2705 ptl = &tl;
2706 while (1)
2708 struct demangle_component *type;
2710 char peek = d_peek_char (di);
2711 if (peek == '\0' || peek == 'E' || peek == '.')
2712 break;
2713 if ((peek == 'R' || peek == 'O')
2714 && d_peek_next_char (di) == 'E')
2715 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2716 break;
2717 type = cplus_demangle_type (di);
2718 if (type == NULL)
2719 return NULL;
2720 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2721 if (*ptl == NULL)
2722 return NULL;
2723 ptl = &d_right (*ptl);
2726 /* There should be at least one parameter type besides the optional
2727 return type. A function which takes no arguments will have a
2728 single parameter type void. */
2729 if (tl == NULL)
2730 return NULL;
2732 /* If we have a single parameter type void, omit it. */
2733 if (d_right (tl) == NULL
2734 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2735 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2737 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2738 d_left (tl) = NULL;
2741 return tl;
2744 /* <bare-function-type> ::= [J]<type>+ */
2746 static struct demangle_component *
2747 d_bare_function_type (struct d_info *di, int has_return_type)
2749 struct demangle_component *return_type;
2750 struct demangle_component *tl;
2751 char peek;
2753 /* Detect special qualifier indicating that the first argument
2754 is the return type. */
2755 peek = d_peek_char (di);
2756 if (peek == 'J')
2758 d_advance (di, 1);
2759 has_return_type = 1;
2762 if (has_return_type)
2764 return_type = cplus_demangle_type (di);
2765 if (return_type == NULL)
2766 return NULL;
2768 else
2769 return_type = NULL;
2771 tl = d_parmlist (di);
2772 if (tl == NULL)
2773 return NULL;
2775 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2776 return_type, tl);
2779 /* <class-enum-type> ::= <name> */
2781 static struct demangle_component *
2782 d_class_enum_type (struct d_info *di)
2784 return d_name (di);
2787 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2788 ::= A [<(dimension) expression>] _ <(element) type>
2791 static struct demangle_component *
2792 d_array_type (struct d_info *di)
2794 char peek;
2795 struct demangle_component *dim;
2797 if (! d_check_char (di, 'A'))
2798 return NULL;
2800 peek = d_peek_char (di);
2801 if (peek == '_')
2802 dim = NULL;
2803 else if (IS_DIGIT (peek))
2805 const char *s;
2807 s = d_str (di);
2810 d_advance (di, 1);
2811 peek = d_peek_char (di);
2813 while (IS_DIGIT (peek));
2814 dim = d_make_name (di, s, d_str (di) - s);
2815 if (dim == NULL)
2816 return NULL;
2818 else
2820 dim = d_expression (di);
2821 if (dim == NULL)
2822 return NULL;
2825 if (! d_check_char (di, '_'))
2826 return NULL;
2828 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2829 cplus_demangle_type (di));
2832 /* <vector-type> ::= Dv <number> _ <type>
2833 ::= Dv _ <expression> _ <type> */
2835 static struct demangle_component *
2836 d_vector_type (struct d_info *di)
2838 char peek;
2839 struct demangle_component *dim;
2841 peek = d_peek_char (di);
2842 if (peek == '_')
2844 d_advance (di, 1);
2845 dim = d_expression (di);
2847 else
2848 dim = d_number_component (di);
2850 if (dim == NULL)
2851 return NULL;
2853 if (! d_check_char (di, '_'))
2854 return NULL;
2856 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2857 cplus_demangle_type (di));
2860 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2862 static struct demangle_component *
2863 d_pointer_to_member_type (struct d_info *di)
2865 struct demangle_component *cl;
2866 struct demangle_component *mem;
2868 if (! d_check_char (di, 'M'))
2869 return NULL;
2871 cl = cplus_demangle_type (di);
2872 if (cl == NULL)
2873 return NULL;
2875 /* The ABI says, "The type of a non-static member function is considered
2876 to be different, for the purposes of substitution, from the type of a
2877 namespace-scope or static member function whose type appears
2878 similar. The types of two non-static member functions are considered
2879 to be different, for the purposes of substitution, if the functions
2880 are members of different classes. In other words, for the purposes of
2881 substitution, the class of which the function is a member is
2882 considered part of the type of function."
2884 For a pointer to member function, this call to cplus_demangle_type
2885 will end up adding a (possibly qualified) non-member function type to
2886 the substitution table, which is not correct; however, the member
2887 function type will never be used in a substitution, so putting the
2888 wrong type in the substitution table is harmless. */
2890 mem = cplus_demangle_type (di);
2891 if (mem == NULL)
2892 return NULL;
2894 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2897 /* <non-negative number> _ */
2899 static long
2900 d_compact_number (struct d_info *di)
2902 long num;
2903 if (d_peek_char (di) == '_')
2904 num = 0;
2905 else if (d_peek_char (di) == 'n')
2906 return -1;
2907 else
2908 num = d_number (di) + 1;
2910 if (! d_check_char (di, '_'))
2911 return -1;
2912 return num;
2915 /* <template-param> ::= T_
2916 ::= T <(parameter-2 non-negative) number> _
2919 static struct demangle_component *
2920 d_template_param (struct d_info *di)
2922 long param;
2924 if (! d_check_char (di, 'T'))
2925 return NULL;
2927 param = d_compact_number (di);
2928 if (param < 0)
2929 return NULL;
2931 ++di->did_subs;
2933 return d_make_template_param (di, param);
2936 /* <template-args> ::= I <template-arg>+ E */
2938 static struct demangle_component *
2939 d_template_args (struct d_info *di)
2941 struct demangle_component *hold_last_name;
2942 struct demangle_component *al;
2943 struct demangle_component **pal;
2945 /* Preserve the last name we saw--don't let the template arguments
2946 clobber it, as that would give us the wrong name for a subsequent
2947 constructor or destructor. */
2948 hold_last_name = di->last_name;
2950 if (d_peek_char (di) != 'I'
2951 && d_peek_char (di) != 'J')
2952 return NULL;
2953 d_advance (di, 1);
2955 if (d_peek_char (di) == 'E')
2957 /* An argument pack can be empty. */
2958 d_advance (di, 1);
2959 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2962 al = NULL;
2963 pal = &al;
2964 while (1)
2966 struct demangle_component *a;
2968 a = d_template_arg (di);
2969 if (a == NULL)
2970 return NULL;
2972 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2973 if (*pal == NULL)
2974 return NULL;
2975 pal = &d_right (*pal);
2977 if (d_peek_char (di) == 'E')
2979 d_advance (di, 1);
2980 break;
2984 di->last_name = hold_last_name;
2986 return al;
2989 /* <template-arg> ::= <type>
2990 ::= X <expression> E
2991 ::= <expr-primary>
2994 static struct demangle_component *
2995 d_template_arg (struct d_info *di)
2997 struct demangle_component *ret;
2999 switch (d_peek_char (di))
3001 case 'X':
3002 d_advance (di, 1);
3003 ret = d_expression (di);
3004 if (! d_check_char (di, 'E'))
3005 return NULL;
3006 return ret;
3008 case 'L':
3009 return d_expr_primary (di);
3011 case 'I':
3012 case 'J':
3013 /* An argument pack. */
3014 return d_template_args (di);
3016 default:
3017 return cplus_demangle_type (di);
3021 /* Parse a sequence of expressions until we hit the terminator
3022 character. */
3024 static struct demangle_component *
3025 d_exprlist (struct d_info *di, char terminator)
3027 struct demangle_component *list = NULL;
3028 struct demangle_component **p = &list;
3030 if (d_peek_char (di) == terminator)
3032 d_advance (di, 1);
3033 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3036 while (1)
3038 struct demangle_component *arg = d_expression (di);
3039 if (arg == NULL)
3040 return NULL;
3042 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3043 if (*p == NULL)
3044 return NULL;
3045 p = &d_right (*p);
3047 if (d_peek_char (di) == terminator)
3049 d_advance (di, 1);
3050 break;
3054 return list;
3057 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3058 dynamic_cast, static_cast or reinterpret_cast. */
3060 static int
3061 op_is_new_cast (struct demangle_component *op)
3063 const char *code = op->u.s_operator.op->code;
3064 return (code[1] == 'c'
3065 && (code[0] == 's' || code[0] == 'd'
3066 || code[0] == 'c' || code[0] == 'r'));
3069 /* <expression> ::= <(unary) operator-name> <expression>
3070 ::= <(binary) operator-name> <expression> <expression>
3071 ::= <(trinary) operator-name> <expression> <expression> <expression>
3072 ::= cl <expression>+ E
3073 ::= st <type>
3074 ::= <template-param>
3075 ::= sr <type> <unqualified-name>
3076 ::= sr <type> <unqualified-name> <template-args>
3077 ::= <expr-primary>
3080 static inline struct demangle_component *
3081 d_expression_1 (struct d_info *di)
3083 char peek;
3085 peek = d_peek_char (di);
3086 if (peek == 'L')
3087 return d_expr_primary (di);
3088 else if (peek == 'T')
3089 return d_template_param (di);
3090 else if (peek == 's' && d_peek_next_char (di) == 'r')
3092 struct demangle_component *type;
3093 struct demangle_component *name;
3095 d_advance (di, 2);
3096 type = cplus_demangle_type (di);
3097 name = d_unqualified_name (di);
3098 if (d_peek_char (di) != 'I')
3099 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3100 else
3101 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3102 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3103 d_template_args (di)));
3105 else if (peek == 's' && d_peek_next_char (di) == 'p')
3107 d_advance (di, 2);
3108 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3109 d_expression_1 (di), NULL);
3111 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3113 /* Function parameter used in a late-specified return type. */
3114 int index;
3115 d_advance (di, 2);
3116 if (d_peek_char (di) == 'T')
3118 /* 'this' parameter. */
3119 d_advance (di, 1);
3120 index = 0;
3122 else
3124 index = d_compact_number (di) + 1;
3125 if (index == 0)
3126 return NULL;
3128 return d_make_function_param (di, index);
3130 else if (IS_DIGIT (peek)
3131 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3133 /* We can get an unqualified name as an expression in the case of
3134 a dependent function call, i.e. decltype(f(t)). */
3135 struct demangle_component *name;
3137 if (peek == 'o')
3138 /* operator-function-id, i.e. operator+(t). */
3139 d_advance (di, 2);
3141 name = d_unqualified_name (di);
3142 if (name == NULL)
3143 return NULL;
3144 if (d_peek_char (di) == 'I')
3145 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3146 d_template_args (di));
3147 else
3148 return name;
3150 else if ((peek == 'i' || peek == 't')
3151 && d_peek_next_char (di) == 'l')
3153 /* Brace-enclosed initializer list, untyped or typed. */
3154 struct demangle_component *type = NULL;
3155 if (peek == 't')
3156 type = cplus_demangle_type (di);
3157 d_advance (di, 2);
3158 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3159 type, d_exprlist (di, 'E'));
3161 else
3163 struct demangle_component *op;
3164 const char *code = NULL;
3165 int args;
3167 op = d_operator_name (di);
3168 if (op == NULL)
3169 return NULL;
3171 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3173 code = op->u.s_operator.op->code;
3174 di->expansion += op->u.s_operator.op->len - 2;
3175 if (strcmp (code, "st") == 0)
3176 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3177 cplus_demangle_type (di));
3180 switch (op->type)
3182 default:
3183 return NULL;
3184 case DEMANGLE_COMPONENT_OPERATOR:
3185 args = op->u.s_operator.op->args;
3186 break;
3187 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3188 args = op->u.s_extended_operator.args;
3189 break;
3190 case DEMANGLE_COMPONENT_CAST:
3191 args = 1;
3192 break;
3195 switch (args)
3197 case 0:
3198 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3200 case 1:
3202 struct demangle_component *operand;
3203 int suffix = 0;
3205 if (code && (code[0] == 'p' || code[0] == 'm')
3206 && code[1] == code[0])
3207 /* pp_ and mm_ are the prefix variants. */
3208 suffix = !d_check_char (di, '_');
3210 if (op->type == DEMANGLE_COMPONENT_CAST
3211 && d_check_char (di, '_'))
3212 operand = d_exprlist (di, 'E');
3213 else
3214 operand = d_expression_1 (di);
3216 if (suffix)
3217 /* Indicate the suffix variant for d_print_comp. */
3218 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3219 d_make_comp (di,
3220 DEMANGLE_COMPONENT_BINARY_ARGS,
3221 operand, operand));
3222 else
3223 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3224 operand);
3226 case 2:
3228 struct demangle_component *left;
3229 struct demangle_component *right;
3231 if (op_is_new_cast (op))
3232 left = cplus_demangle_type (di);
3233 else
3234 left = d_expression_1 (di);
3235 if (!strcmp (code, "cl"))
3236 right = d_exprlist (di, 'E');
3237 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3239 right = d_unqualified_name (di);
3240 if (d_peek_char (di) == 'I')
3241 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3242 right, d_template_args (di));
3244 else
3245 right = d_expression_1 (di);
3247 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3248 d_make_comp (di,
3249 DEMANGLE_COMPONENT_BINARY_ARGS,
3250 left, right));
3252 case 3:
3254 struct demangle_component *first;
3255 struct demangle_component *second;
3256 struct demangle_component *third;
3258 if (!strcmp (code, "qu"))
3260 /* ?: expression. */
3261 first = d_expression_1 (di);
3262 second = d_expression_1 (di);
3263 third = d_expression_1 (di);
3265 else if (code[0] == 'n')
3267 /* new-expression. */
3268 if (code[1] != 'w' && code[1] != 'a')
3269 return NULL;
3270 first = d_exprlist (di, '_');
3271 second = cplus_demangle_type (di);
3272 if (d_peek_char (di) == 'E')
3274 d_advance (di, 1);
3275 third = NULL;
3277 else if (d_peek_char (di) == 'p'
3278 && d_peek_next_char (di) == 'i')
3280 /* Parenthesized initializer. */
3281 d_advance (di, 2);
3282 third = d_exprlist (di, 'E');
3284 else if (d_peek_char (di) == 'i'
3285 && d_peek_next_char (di) == 'l')
3286 /* initializer-list. */
3287 third = d_expression_1 (di);
3288 else
3289 return NULL;
3291 else
3292 return NULL;
3293 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3294 d_make_comp (di,
3295 DEMANGLE_COMPONENT_TRINARY_ARG1,
3296 first,
3297 d_make_comp (di,
3298 DEMANGLE_COMPONENT_TRINARY_ARG2,
3299 second, third)));
3301 default:
3302 return NULL;
3307 static struct demangle_component *
3308 d_expression (struct d_info *di)
3310 struct demangle_component *ret;
3311 int was_expression = di->is_expression;
3313 di->is_expression = 1;
3314 ret = d_expression_1 (di);
3315 di->is_expression = was_expression;
3316 return ret;
3319 /* <expr-primary> ::= L <type> <(value) number> E
3320 ::= L <type> <(value) float> E
3321 ::= L <mangled-name> E
3324 static struct demangle_component *
3325 d_expr_primary (struct d_info *di)
3327 struct demangle_component *ret;
3329 if (! d_check_char (di, 'L'))
3330 return NULL;
3331 if (d_peek_char (di) == '_'
3332 /* Workaround for G++ bug; see comment in write_template_arg. */
3333 || d_peek_char (di) == 'Z')
3334 ret = cplus_demangle_mangled_name (di, 0);
3335 else
3337 struct demangle_component *type;
3338 enum demangle_component_type t;
3339 const char *s;
3341 type = cplus_demangle_type (di);
3342 if (type == NULL)
3343 return NULL;
3345 /* If we have a type we know how to print, we aren't going to
3346 print the type name itself. */
3347 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3348 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3349 di->expansion -= type->u.s_builtin.type->len;
3351 /* Rather than try to interpret the literal value, we just
3352 collect it as a string. Note that it's possible to have a
3353 floating point literal here. The ABI specifies that the
3354 format of such literals is machine independent. That's fine,
3355 but what's not fine is that versions of g++ up to 3.2 with
3356 -fabi-version=1 used upper case letters in the hex constant,
3357 and dumped out gcc's internal representation. That makes it
3358 hard to tell where the constant ends, and hard to dump the
3359 constant in any readable form anyhow. We don't attempt to
3360 handle these cases. */
3362 t = DEMANGLE_COMPONENT_LITERAL;
3363 if (d_peek_char (di) == 'n')
3365 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3366 d_advance (di, 1);
3368 s = d_str (di);
3369 while (d_peek_char (di) != 'E')
3371 if (d_peek_char (di) == '\0')
3372 return NULL;
3373 d_advance (di, 1);
3375 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3377 if (! d_check_char (di, 'E'))
3378 return NULL;
3379 return ret;
3382 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3383 ::= Z <(function) encoding> E s [<discriminator>]
3384 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3387 static struct demangle_component *
3388 d_local_name (struct d_info *di)
3390 struct demangle_component *function;
3392 if (! d_check_char (di, 'Z'))
3393 return NULL;
3395 function = d_encoding (di, 0);
3397 if (! d_check_char (di, 'E'))
3398 return NULL;
3400 if (d_peek_char (di) == 's')
3402 d_advance (di, 1);
3403 if (! d_discriminator (di))
3404 return NULL;
3405 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3406 d_make_name (di, "string literal",
3407 sizeof "string literal" - 1));
3409 else
3411 struct demangle_component *name;
3412 int num = -1;
3414 if (d_peek_char (di) == 'd')
3416 /* Default argument scope: d <number> _. */
3417 d_advance (di, 1);
3418 num = d_compact_number (di);
3419 if (num < 0)
3420 return NULL;
3423 name = d_name (di);
3424 if (name)
3425 switch (name->type)
3427 /* Lambdas and unnamed types have internal discriminators. */
3428 case DEMANGLE_COMPONENT_LAMBDA:
3429 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3430 break;
3431 default:
3432 if (! d_discriminator (di))
3433 return NULL;
3435 if (num >= 0)
3436 name = d_make_default_arg (di, num, name);
3437 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3441 /* <discriminator> ::= _ <(non-negative) number>
3443 We demangle the discriminator, but we don't print it out. FIXME:
3444 We should print it out in verbose mode. */
3446 static int
3447 d_discriminator (struct d_info *di)
3449 long discrim;
3451 if (d_peek_char (di) != '_')
3452 return 1;
3453 d_advance (di, 1);
3454 discrim = d_number (di);
3455 if (discrim < 0)
3456 return 0;
3457 return 1;
3460 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3462 static struct demangle_component *
3463 d_lambda (struct d_info *di)
3465 struct demangle_component *tl;
3466 struct demangle_component *ret;
3467 int num;
3469 if (! d_check_char (di, 'U'))
3470 return NULL;
3471 if (! d_check_char (di, 'l'))
3472 return NULL;
3474 tl = d_parmlist (di);
3475 if (tl == NULL)
3476 return NULL;
3478 if (! d_check_char (di, 'E'))
3479 return NULL;
3481 num = d_compact_number (di);
3482 if (num < 0)
3483 return NULL;
3485 ret = d_make_empty (di);
3486 if (ret)
3488 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3489 ret->u.s_unary_num.sub = tl;
3490 ret->u.s_unary_num.num = num;
3493 if (! d_add_substitution (di, ret))
3494 return NULL;
3496 return ret;
3499 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3501 static struct demangle_component *
3502 d_unnamed_type (struct d_info *di)
3504 struct demangle_component *ret;
3505 long num;
3507 if (! d_check_char (di, 'U'))
3508 return NULL;
3509 if (! d_check_char (di, 't'))
3510 return NULL;
3512 num = d_compact_number (di);
3513 if (num < 0)
3514 return NULL;
3516 ret = d_make_empty (di);
3517 if (ret)
3519 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3520 ret->u.s_number.number = num;
3523 if (! d_add_substitution (di, ret))
3524 return NULL;
3526 return ret;
3529 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3532 static struct demangle_component *
3533 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3535 const char *suffix = d_str (di);
3536 const char *pend = suffix;
3537 struct demangle_component *n;
3539 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3541 pend += 2;
3542 while (IS_LOWER (*pend) || *pend == '_')
3543 ++pend;
3545 while (*pend == '.' && IS_DIGIT (pend[1]))
3547 pend += 2;
3548 while (IS_DIGIT (*pend))
3549 ++pend;
3551 d_advance (di, pend - suffix);
3552 n = d_make_name (di, suffix, pend - suffix);
3553 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3556 /* Add a new substitution. */
3558 static int
3559 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3561 if (dc == NULL)
3562 return 0;
3563 if (di->next_sub >= di->num_subs)
3564 return 0;
3565 di->subs[di->next_sub] = dc;
3566 ++di->next_sub;
3567 return 1;
3570 /* <substitution> ::= S <seq-id> _
3571 ::= S_
3572 ::= St
3573 ::= Sa
3574 ::= Sb
3575 ::= Ss
3576 ::= Si
3577 ::= So
3578 ::= Sd
3580 If PREFIX is non-zero, then this type is being used as a prefix in
3581 a qualified name. In this case, for the standard substitutions, we
3582 need to check whether we are being used as a prefix for a
3583 constructor or destructor, and return a full template name.
3584 Otherwise we will get something like std::iostream::~iostream()
3585 which does not correspond particularly well to any function which
3586 actually appears in the source.
3589 static const struct d_standard_sub_info standard_subs[] =
3591 { 't', NL ("std"),
3592 NL ("std"),
3593 NULL, 0 },
3594 { 'a', NL ("std::allocator"),
3595 NL ("std::allocator"),
3596 NL ("allocator") },
3597 { 'b', NL ("std::basic_string"),
3598 NL ("std::basic_string"),
3599 NL ("basic_string") },
3600 { 's', NL ("std::string"),
3601 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3602 NL ("basic_string") },
3603 { 'i', NL ("std::istream"),
3604 NL ("std::basic_istream<char, std::char_traits<char> >"),
3605 NL ("basic_istream") },
3606 { 'o', NL ("std::ostream"),
3607 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3608 NL ("basic_ostream") },
3609 { 'd', NL ("std::iostream"),
3610 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3611 NL ("basic_iostream") }
3614 static struct demangle_component *
3615 d_substitution (struct d_info *di, int prefix)
3617 char c;
3619 if (! d_check_char (di, 'S'))
3620 return NULL;
3622 c = d_next_char (di);
3623 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3625 unsigned int id;
3627 id = 0;
3628 if (c != '_')
3632 unsigned int new_id;
3634 if (IS_DIGIT (c))
3635 new_id = id * 36 + c - '0';
3636 else if (IS_UPPER (c))
3637 new_id = id * 36 + c - 'A' + 10;
3638 else
3639 return NULL;
3640 if (new_id < id)
3641 return NULL;
3642 id = new_id;
3643 c = d_next_char (di);
3645 while (c != '_');
3647 ++id;
3650 if (id >= (unsigned int) di->next_sub)
3651 return NULL;
3653 ++di->did_subs;
3655 return di->subs[id];
3657 else
3659 int verbose;
3660 const struct d_standard_sub_info *p;
3661 const struct d_standard_sub_info *pend;
3663 verbose = (di->options & DMGL_VERBOSE) != 0;
3664 if (! verbose && prefix)
3666 char peek;
3668 peek = d_peek_char (di);
3669 if (peek == 'C' || peek == 'D')
3670 verbose = 1;
3673 pend = (&standard_subs[0]
3674 + sizeof standard_subs / sizeof standard_subs[0]);
3675 for (p = &standard_subs[0]; p < pend; ++p)
3677 if (c == p->code)
3679 const char *s;
3680 int len;
3682 if (p->set_last_name != NULL)
3683 di->last_name = d_make_sub (di, p->set_last_name,
3684 p->set_last_name_len);
3685 if (verbose)
3687 s = p->full_expansion;
3688 len = p->full_len;
3690 else
3692 s = p->simple_expansion;
3693 len = p->simple_len;
3695 di->expansion += len;
3696 return d_make_sub (di, s, len);
3700 return NULL;
3704 static void
3705 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3707 checkpoint->n = di->n;
3708 checkpoint->next_comp = di->next_comp;
3709 checkpoint->next_sub = di->next_sub;
3710 checkpoint->did_subs = di->did_subs;
3711 checkpoint->expansion = di->expansion;
3714 static void
3715 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3717 di->n = checkpoint->n;
3718 di->next_comp = checkpoint->next_comp;
3719 di->next_sub = checkpoint->next_sub;
3720 di->did_subs = checkpoint->did_subs;
3721 di->expansion = checkpoint->expansion;
3724 /* Initialize a growable string. */
3726 static void
3727 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3729 dgs->buf = NULL;
3730 dgs->len = 0;
3731 dgs->alc = 0;
3732 dgs->allocation_failure = 0;
3734 if (estimate > 0)
3735 d_growable_string_resize (dgs, estimate);
3738 /* Grow a growable string to a given size. */
3740 static inline void
3741 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3743 size_t newalc;
3744 char *newbuf;
3746 if (dgs->allocation_failure)
3747 return;
3749 /* Start allocation at two bytes to avoid any possibility of confusion
3750 with the special value of 1 used as a return in *palc to indicate
3751 allocation failures. */
3752 newalc = dgs->alc > 0 ? dgs->alc : 2;
3753 while (newalc < need)
3754 newalc <<= 1;
3756 newbuf = (char *) realloc (dgs->buf, newalc);
3757 if (newbuf == NULL)
3759 free (dgs->buf);
3760 dgs->buf = NULL;
3761 dgs->len = 0;
3762 dgs->alc = 0;
3763 dgs->allocation_failure = 1;
3764 return;
3766 dgs->buf = newbuf;
3767 dgs->alc = newalc;
3770 /* Append a buffer to a growable string. */
3772 static inline void
3773 d_growable_string_append_buffer (struct d_growable_string *dgs,
3774 const char *s, size_t l)
3776 size_t need;
3778 need = dgs->len + l + 1;
3779 if (need > dgs->alc)
3780 d_growable_string_resize (dgs, need);
3782 if (dgs->allocation_failure)
3783 return;
3785 memcpy (dgs->buf + dgs->len, s, l);
3786 dgs->buf[dgs->len + l] = '\0';
3787 dgs->len += l;
3790 /* Bridge growable strings to the callback mechanism. */
3792 static void
3793 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3795 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3797 d_growable_string_append_buffer (dgs, s, l);
3800 /* Walk the tree, counting the number of templates encountered, and
3801 the number of times a scope might be saved. These counts will be
3802 used to allocate data structures for d_print_comp, so the logic
3803 here must mirror the logic d_print_comp will use. It is not
3804 important that the resulting numbers are exact, so long as they
3805 are larger than the actual numbers encountered. */
3807 static void
3808 d_count_templates_scopes (int *num_templates, int *num_scopes,
3809 const struct demangle_component *dc)
3811 if (dc == NULL)
3812 return;
3814 switch (dc->type)
3816 case DEMANGLE_COMPONENT_NAME:
3817 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3818 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3819 case DEMANGLE_COMPONENT_SUB_STD:
3820 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3821 case DEMANGLE_COMPONENT_OPERATOR:
3822 case DEMANGLE_COMPONENT_CHARACTER:
3823 case DEMANGLE_COMPONENT_NUMBER:
3824 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3825 break;
3827 case DEMANGLE_COMPONENT_TEMPLATE:
3828 (*num_templates)++;
3829 goto recurse_left_right;
3831 case DEMANGLE_COMPONENT_REFERENCE:
3832 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3833 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
3834 (*num_scopes)++;
3835 goto recurse_left_right;
3837 case DEMANGLE_COMPONENT_QUAL_NAME:
3838 case DEMANGLE_COMPONENT_LOCAL_NAME:
3839 case DEMANGLE_COMPONENT_TYPED_NAME:
3840 case DEMANGLE_COMPONENT_VTABLE:
3841 case DEMANGLE_COMPONENT_VTT:
3842 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3843 case DEMANGLE_COMPONENT_TYPEINFO:
3844 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3845 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3846 case DEMANGLE_COMPONENT_THUNK:
3847 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3848 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3849 case DEMANGLE_COMPONENT_JAVA_CLASS:
3850 case DEMANGLE_COMPONENT_GUARD:
3851 case DEMANGLE_COMPONENT_TLS_INIT:
3852 case DEMANGLE_COMPONENT_TLS_WRAPPER:
3853 case DEMANGLE_COMPONENT_REFTEMP:
3854 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3855 case DEMANGLE_COMPONENT_RESTRICT:
3856 case DEMANGLE_COMPONENT_VOLATILE:
3857 case DEMANGLE_COMPONENT_CONST:
3858 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3859 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3860 case DEMANGLE_COMPONENT_CONST_THIS:
3861 case DEMANGLE_COMPONENT_REFERENCE_THIS:
3862 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
3863 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3864 case DEMANGLE_COMPONENT_POINTER:
3865 case DEMANGLE_COMPONENT_COMPLEX:
3866 case DEMANGLE_COMPONENT_IMAGINARY:
3867 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3868 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3869 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3870 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3871 case DEMANGLE_COMPONENT_FIXED_TYPE:
3872 case DEMANGLE_COMPONENT_VECTOR_TYPE:
3873 case DEMANGLE_COMPONENT_ARGLIST:
3874 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3875 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
3876 case DEMANGLE_COMPONENT_CAST:
3877 case DEMANGLE_COMPONENT_CONVERSION:
3878 case DEMANGLE_COMPONENT_NULLARY:
3879 case DEMANGLE_COMPONENT_UNARY:
3880 case DEMANGLE_COMPONENT_BINARY:
3881 case DEMANGLE_COMPONENT_BINARY_ARGS:
3882 case DEMANGLE_COMPONENT_TRINARY:
3883 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3884 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3885 case DEMANGLE_COMPONENT_LITERAL:
3886 case DEMANGLE_COMPONENT_LITERAL_NEG:
3887 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3888 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3889 case DEMANGLE_COMPONENT_DECLTYPE:
3890 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
3891 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
3892 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3893 case DEMANGLE_COMPONENT_TAGGED_NAME:
3894 case DEMANGLE_COMPONENT_CLONE:
3895 recurse_left_right:
3896 d_count_templates_scopes (num_templates, num_scopes,
3897 d_left (dc));
3898 d_count_templates_scopes (num_templates, num_scopes,
3899 d_right (dc));
3900 break;
3902 case DEMANGLE_COMPONENT_CTOR:
3903 d_count_templates_scopes (num_templates, num_scopes,
3904 dc->u.s_ctor.name);
3905 break;
3907 case DEMANGLE_COMPONENT_DTOR:
3908 d_count_templates_scopes (num_templates, num_scopes,
3909 dc->u.s_dtor.name);
3910 break;
3912 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3913 d_count_templates_scopes (num_templates, num_scopes,
3914 dc->u.s_extended_operator.name);
3915 break;
3917 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
3918 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
3919 d_count_templates_scopes (num_templates, num_scopes,
3920 d_left (dc));
3921 break;
3923 case DEMANGLE_COMPONENT_LAMBDA:
3924 case DEMANGLE_COMPONENT_DEFAULT_ARG:
3925 d_count_templates_scopes (num_templates, num_scopes,
3926 dc->u.s_unary_num.sub);
3927 break;
3931 /* Initialize a print information structure. */
3933 static void
3934 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3935 void *opaque, const struct demangle_component *dc)
3937 dpi->len = 0;
3938 dpi->last_char = '\0';
3939 dpi->templates = NULL;
3940 dpi->modifiers = NULL;
3941 dpi->pack_index = 0;
3942 dpi->flush_count = 0;
3944 dpi->callback = callback;
3945 dpi->opaque = opaque;
3947 dpi->demangle_failure = 0;
3949 dpi->saved_scopes = NULL;
3950 dpi->next_saved_scope = 0;
3951 dpi->num_saved_scopes = 0;
3953 dpi->copy_templates = NULL;
3954 dpi->next_copy_template = 0;
3955 dpi->num_copy_templates = 0;
3957 d_count_templates_scopes (&dpi->num_copy_templates,
3958 &dpi->num_saved_scopes, dc);
3959 dpi->num_copy_templates *= dpi->num_saved_scopes;
3961 dpi->current_template = NULL;
3964 /* Indicate that an error occurred during printing, and test for error. */
3966 static inline void
3967 d_print_error (struct d_print_info *dpi)
3969 dpi->demangle_failure = 1;
3972 static inline int
3973 d_print_saw_error (struct d_print_info *dpi)
3975 return dpi->demangle_failure != 0;
3978 /* Flush buffered characters to the callback. */
3980 static inline void
3981 d_print_flush (struct d_print_info *dpi)
3983 dpi->buf[dpi->len] = '\0';
3984 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3985 dpi->len = 0;
3986 dpi->flush_count++;
3989 /* Append characters and buffers for printing. */
3991 static inline void
3992 d_append_char (struct d_print_info *dpi, char c)
3994 if (dpi->len == sizeof (dpi->buf) - 1)
3995 d_print_flush (dpi);
3997 dpi->buf[dpi->len++] = c;
3998 dpi->last_char = c;
4001 static inline void
4002 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4004 size_t i;
4006 for (i = 0; i < l; i++)
4007 d_append_char (dpi, s[i]);
4010 static inline void
4011 d_append_string (struct d_print_info *dpi, const char *s)
4013 d_append_buffer (dpi, s, strlen (s));
4016 static inline void
4017 d_append_num (struct d_print_info *dpi, long l)
4019 char buf[25];
4020 sprintf (buf,"%ld", l);
4021 d_append_string (dpi, buf);
4024 static inline char
4025 d_last_char (struct d_print_info *dpi)
4027 return dpi->last_char;
4030 /* Turn components into a human readable string. OPTIONS is the
4031 options bits passed to the demangler. DC is the tree to print.
4032 CALLBACK is a function to call to flush demangled string segments
4033 as they fill the intermediate buffer, and OPAQUE is a generalized
4034 callback argument. On success, this returns 1. On failure,
4035 it returns 0, indicating a bad parse. It does not use heap
4036 memory to build an output string, so cannot encounter memory
4037 allocation failure. */
4039 CP_STATIC_IF_GLIBCPP_V3
4041 cplus_demangle_print_callback (int options,
4042 const struct demangle_component *dc,
4043 demangle_callbackref callback, void *opaque)
4045 struct d_print_info dpi;
4047 d_print_init (&dpi, callback, opaque, dc);
4050 #ifdef CP_DYNAMIC_ARRAYS
4051 __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
4052 __extension__ struct d_print_template temps[dpi.num_copy_templates];
4054 dpi.saved_scopes = scopes;
4055 dpi.copy_templates = temps;
4056 #else
4057 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4058 * sizeof (*dpi.saved_scopes));
4059 dpi.copy_templates = alloca (dpi.num_copy_templates
4060 * sizeof (*dpi.copy_templates));
4061 #endif
4063 d_print_comp (&dpi, options, dc);
4066 d_print_flush (&dpi);
4068 return ! d_print_saw_error (&dpi);
4071 /* Turn components into a human readable string. OPTIONS is the
4072 options bits passed to the demangler. DC is the tree to print.
4073 ESTIMATE is a guess at the length of the result. This returns a
4074 string allocated by malloc, or NULL on error. On success, this
4075 sets *PALC to the size of the allocated buffer. On failure, this
4076 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4077 failure. */
4079 CP_STATIC_IF_GLIBCPP_V3
4080 char *
4081 cplus_demangle_print (int options, const struct demangle_component *dc,
4082 int estimate, size_t *palc)
4084 struct d_growable_string dgs;
4086 d_growable_string_init (&dgs, estimate);
4088 if (! cplus_demangle_print_callback (options, dc,
4089 d_growable_string_callback_adapter,
4090 &dgs))
4092 free (dgs.buf);
4093 *palc = 0;
4094 return NULL;
4097 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4098 return dgs.buf;
4101 /* Returns the I'th element of the template arglist ARGS, or NULL on
4102 failure. */
4104 static struct demangle_component *
4105 d_index_template_argument (struct demangle_component *args, int i)
4107 struct demangle_component *a;
4109 for (a = args;
4110 a != NULL;
4111 a = d_right (a))
4113 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4114 return NULL;
4115 if (i <= 0)
4116 break;
4117 --i;
4119 if (i != 0 || a == NULL)
4120 return NULL;
4122 return d_left (a);
4125 /* Returns the template argument from the current context indicated by DC,
4126 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4128 static struct demangle_component *
4129 d_lookup_template_argument (struct d_print_info *dpi,
4130 const struct demangle_component *dc)
4132 if (dpi->templates == NULL)
4134 d_print_error (dpi);
4135 return NULL;
4138 return d_index_template_argument
4139 (d_right (dpi->templates->template_decl),
4140 dc->u.s_number.number);
4143 /* Returns a template argument pack used in DC (any will do), or NULL. */
4145 static struct demangle_component *
4146 d_find_pack (struct d_print_info *dpi,
4147 const struct demangle_component *dc)
4149 struct demangle_component *a;
4150 if (dc == NULL)
4151 return NULL;
4153 switch (dc->type)
4155 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4156 a = d_lookup_template_argument (dpi, dc);
4157 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4158 return a;
4159 return NULL;
4161 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4162 return NULL;
4164 case DEMANGLE_COMPONENT_LAMBDA:
4165 case DEMANGLE_COMPONENT_NAME:
4166 case DEMANGLE_COMPONENT_TAGGED_NAME:
4167 case DEMANGLE_COMPONENT_OPERATOR:
4168 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4169 case DEMANGLE_COMPONENT_SUB_STD:
4170 case DEMANGLE_COMPONENT_CHARACTER:
4171 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4172 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4173 return NULL;
4175 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4176 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4177 case DEMANGLE_COMPONENT_CTOR:
4178 return d_find_pack (dpi, dc->u.s_ctor.name);
4179 case DEMANGLE_COMPONENT_DTOR:
4180 return d_find_pack (dpi, dc->u.s_dtor.name);
4182 default:
4183 a = d_find_pack (dpi, d_left (dc));
4184 if (a)
4185 return a;
4186 return d_find_pack (dpi, d_right (dc));
4190 /* Returns the length of the template argument pack DC. */
4192 static int
4193 d_pack_length (const struct demangle_component *dc)
4195 int count = 0;
4196 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4197 && d_left (dc) != NULL)
4199 ++count;
4200 dc = d_right (dc);
4202 return count;
4205 /* DC is a component of a mangled expression. Print it, wrapped in parens
4206 if needed. */
4208 static void
4209 d_print_subexpr (struct d_print_info *dpi, int options,
4210 const struct demangle_component *dc)
4212 int simple = 0;
4213 if (dc->type == DEMANGLE_COMPONENT_NAME
4214 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4215 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4216 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4217 simple = 1;
4218 if (!simple)
4219 d_append_char (dpi, '(');
4220 d_print_comp (dpi, options, dc);
4221 if (!simple)
4222 d_append_char (dpi, ')');
4225 /* Save the current scope. */
4227 static void
4228 d_save_scope (struct d_print_info *dpi,
4229 const struct demangle_component *container)
4231 struct d_saved_scope *scope;
4232 struct d_print_template *src, **link;
4234 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4236 d_print_error (dpi);
4237 return;
4239 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4240 dpi->next_saved_scope++;
4242 scope->container = container;
4243 link = &scope->templates;
4245 for (src = dpi->templates; src != NULL; src = src->next)
4247 struct d_print_template *dst;
4249 if (dpi->next_copy_template >= dpi->num_copy_templates)
4251 d_print_error (dpi);
4252 return;
4254 dst = &dpi->copy_templates[dpi->next_copy_template];
4255 dpi->next_copy_template++;
4257 dst->template_decl = src->template_decl;
4258 *link = dst;
4259 link = &dst->next;
4262 *link = NULL;
4265 /* Attempt to locate a previously saved scope. Returns NULL if no
4266 corresponding saved scope was found. */
4268 static struct d_saved_scope *
4269 d_get_saved_scope (struct d_print_info *dpi,
4270 const struct demangle_component *container)
4272 int i;
4274 for (i = 0; i < dpi->next_saved_scope; i++)
4275 if (dpi->saved_scopes[i].container == container)
4276 return &dpi->saved_scopes[i];
4278 return NULL;
4281 /* Subroutine to handle components. */
4283 static void
4284 d_print_comp (struct d_print_info *dpi, int options,
4285 const struct demangle_component *dc)
4287 /* Magic variable to let reference smashing skip over the next modifier
4288 without needing to modify *dc. */
4289 const struct demangle_component *mod_inner = NULL;
4291 /* Variable used to store the current templates while a previously
4292 captured scope is used. */
4293 struct d_print_template *saved_templates;
4295 /* Nonzero if templates have been stored in the above variable. */
4296 int need_template_restore = 0;
4298 if (dc == NULL)
4300 d_print_error (dpi);
4301 return;
4303 if (d_print_saw_error (dpi))
4304 return;
4306 switch (dc->type)
4308 case DEMANGLE_COMPONENT_NAME:
4309 if ((options & DMGL_JAVA) == 0)
4310 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4311 else
4312 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4313 return;
4315 case DEMANGLE_COMPONENT_TAGGED_NAME:
4316 d_print_comp (dpi, options, d_left (dc));
4317 d_append_string (dpi, "[abi:");
4318 d_print_comp (dpi, options, d_right (dc));
4319 d_append_char (dpi, ']');
4320 return;
4322 case DEMANGLE_COMPONENT_QUAL_NAME:
4323 case DEMANGLE_COMPONENT_LOCAL_NAME:
4324 d_print_comp (dpi, options, d_left (dc));
4325 if ((options & DMGL_JAVA) == 0)
4326 d_append_string (dpi, "::");
4327 else
4328 d_append_char (dpi, '.');
4330 struct demangle_component *local_name = d_right (dc);
4331 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4333 d_append_string (dpi, "{default arg#");
4334 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4335 d_append_string (dpi, "}::");
4336 local_name = local_name->u.s_unary_num.sub;
4338 d_print_comp (dpi, options, local_name);
4340 return;
4342 case DEMANGLE_COMPONENT_TYPED_NAME:
4344 struct d_print_mod *hold_modifiers;
4345 struct demangle_component *typed_name;
4346 struct d_print_mod adpm[4];
4347 unsigned int i;
4348 struct d_print_template dpt;
4350 /* Pass the name down to the type so that it can be printed in
4351 the right place for the type. We also have to pass down
4352 any CV-qualifiers, which apply to the this parameter. */
4353 hold_modifiers = dpi->modifiers;
4354 dpi->modifiers = 0;
4355 i = 0;
4356 typed_name = d_left (dc);
4357 while (typed_name != NULL)
4359 if (i >= sizeof adpm / sizeof adpm[0])
4361 d_print_error (dpi);
4362 return;
4365 adpm[i].next = dpi->modifiers;
4366 dpi->modifiers = &adpm[i];
4367 adpm[i].mod = typed_name;
4368 adpm[i].printed = 0;
4369 adpm[i].templates = dpi->templates;
4370 ++i;
4372 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4373 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4374 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4375 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4376 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4377 break;
4379 typed_name = d_left (typed_name);
4382 if (typed_name == NULL)
4384 d_print_error (dpi);
4385 return;
4388 /* If typed_name is a template, then it applies to the
4389 function type as well. */
4390 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4392 dpt.next = dpi->templates;
4393 dpi->templates = &dpt;
4394 dpt.template_decl = typed_name;
4397 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4398 there may be CV-qualifiers on its right argument which
4399 really apply here; this happens when parsing a class which
4400 is local to a function. */
4401 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4403 struct demangle_component *local_name;
4405 local_name = d_right (typed_name);
4406 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4407 local_name = local_name->u.s_unary_num.sub;
4408 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4409 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4410 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4411 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4412 || (local_name->type
4413 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4415 if (i >= sizeof adpm / sizeof adpm[0])
4417 d_print_error (dpi);
4418 return;
4421 adpm[i] = adpm[i - 1];
4422 adpm[i].next = &adpm[i - 1];
4423 dpi->modifiers = &adpm[i];
4425 adpm[i - 1].mod = local_name;
4426 adpm[i - 1].printed = 0;
4427 adpm[i - 1].templates = dpi->templates;
4428 ++i;
4430 local_name = d_left (local_name);
4434 d_print_comp (dpi, options, d_right (dc));
4436 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4437 dpi->templates = dpt.next;
4439 /* If the modifiers didn't get printed by the type, print them
4440 now. */
4441 while (i > 0)
4443 --i;
4444 if (! adpm[i].printed)
4446 d_append_char (dpi, ' ');
4447 d_print_mod (dpi, options, adpm[i].mod);
4451 dpi->modifiers = hold_modifiers;
4453 return;
4456 case DEMANGLE_COMPONENT_TEMPLATE:
4458 struct d_print_mod *hold_dpm;
4459 struct demangle_component *dcl;
4460 const struct demangle_component *hold_current;
4462 /* This template may need to be referenced by a cast operator
4463 contained in its subtree. */
4464 hold_current = dpi->current_template;
4465 dpi->current_template = dc;
4467 /* Don't push modifiers into a template definition. Doing so
4468 could give the wrong definition for a template argument.
4469 Instead, treat the template essentially as a name. */
4471 hold_dpm = dpi->modifiers;
4472 dpi->modifiers = NULL;
4474 dcl = d_left (dc);
4476 if ((options & DMGL_JAVA) != 0
4477 && dcl->type == DEMANGLE_COMPONENT_NAME
4478 && dcl->u.s_name.len == 6
4479 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4481 /* Special-case Java arrays, so that JArray<TYPE> appears
4482 instead as TYPE[]. */
4484 d_print_comp (dpi, options, d_right (dc));
4485 d_append_string (dpi, "[]");
4487 else
4489 d_print_comp (dpi, options, dcl);
4490 if (d_last_char (dpi) == '<')
4491 d_append_char (dpi, ' ');
4492 d_append_char (dpi, '<');
4493 d_print_comp (dpi, options, d_right (dc));
4494 /* Avoid generating two consecutive '>' characters, to avoid
4495 the C++ syntactic ambiguity. */
4496 if (d_last_char (dpi) == '>')
4497 d_append_char (dpi, ' ');
4498 d_append_char (dpi, '>');
4501 dpi->modifiers = hold_dpm;
4502 dpi->current_template = hold_current;
4504 return;
4507 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4509 struct d_print_template *hold_dpt;
4510 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4512 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4513 a = d_index_template_argument (a, dpi->pack_index);
4515 if (a == NULL)
4517 d_print_error (dpi);
4518 return;
4521 /* While processing this parameter, we need to pop the list of
4522 templates. This is because the template parameter may
4523 itself be a reference to a parameter of an outer
4524 template. */
4526 hold_dpt = dpi->templates;
4527 dpi->templates = hold_dpt->next;
4529 d_print_comp (dpi, options, a);
4531 dpi->templates = hold_dpt;
4533 return;
4536 case DEMANGLE_COMPONENT_CTOR:
4537 d_print_comp (dpi, options, dc->u.s_ctor.name);
4538 return;
4540 case DEMANGLE_COMPONENT_DTOR:
4541 d_append_char (dpi, '~');
4542 d_print_comp (dpi, options, dc->u.s_dtor.name);
4543 return;
4545 case DEMANGLE_COMPONENT_VTABLE:
4546 d_append_string (dpi, "vtable for ");
4547 d_print_comp (dpi, options, d_left (dc));
4548 return;
4550 case DEMANGLE_COMPONENT_VTT:
4551 d_append_string (dpi, "VTT for ");
4552 d_print_comp (dpi, options, d_left (dc));
4553 return;
4555 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4556 d_append_string (dpi, "construction vtable for ");
4557 d_print_comp (dpi, options, d_left (dc));
4558 d_append_string (dpi, "-in-");
4559 d_print_comp (dpi, options, d_right (dc));
4560 return;
4562 case DEMANGLE_COMPONENT_TYPEINFO:
4563 d_append_string (dpi, "typeinfo for ");
4564 d_print_comp (dpi, options, d_left (dc));
4565 return;
4567 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4568 d_append_string (dpi, "typeinfo name for ");
4569 d_print_comp (dpi, options, d_left (dc));
4570 return;
4572 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4573 d_append_string (dpi, "typeinfo fn for ");
4574 d_print_comp (dpi, options, d_left (dc));
4575 return;
4577 case DEMANGLE_COMPONENT_THUNK:
4578 d_append_string (dpi, "non-virtual thunk to ");
4579 d_print_comp (dpi, options, d_left (dc));
4580 return;
4582 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4583 d_append_string (dpi, "virtual thunk to ");
4584 d_print_comp (dpi, options, d_left (dc));
4585 return;
4587 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4588 d_append_string (dpi, "covariant return thunk to ");
4589 d_print_comp (dpi, options, d_left (dc));
4590 return;
4592 case DEMANGLE_COMPONENT_JAVA_CLASS:
4593 d_append_string (dpi, "java Class for ");
4594 d_print_comp (dpi, options, d_left (dc));
4595 return;
4597 case DEMANGLE_COMPONENT_GUARD:
4598 d_append_string (dpi, "guard variable for ");
4599 d_print_comp (dpi, options, d_left (dc));
4600 return;
4602 case DEMANGLE_COMPONENT_TLS_INIT:
4603 d_append_string (dpi, "TLS init function for ");
4604 d_print_comp (dpi, options, d_left (dc));
4605 return;
4607 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4608 d_append_string (dpi, "TLS wrapper function for ");
4609 d_print_comp (dpi, options, d_left (dc));
4610 return;
4612 case DEMANGLE_COMPONENT_REFTEMP:
4613 d_append_string (dpi, "reference temporary #");
4614 d_print_comp (dpi, options, d_right (dc));
4615 d_append_string (dpi, " for ");
4616 d_print_comp (dpi, options, d_left (dc));
4617 return;
4619 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4620 d_append_string (dpi, "hidden alias for ");
4621 d_print_comp (dpi, options, d_left (dc));
4622 return;
4624 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4625 d_append_string (dpi, "transaction clone for ");
4626 d_print_comp (dpi, options, d_left (dc));
4627 return;
4629 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4630 d_append_string (dpi, "non-transaction clone for ");
4631 d_print_comp (dpi, options, d_left (dc));
4632 return;
4634 case DEMANGLE_COMPONENT_SUB_STD:
4635 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4636 return;
4638 case DEMANGLE_COMPONENT_RESTRICT:
4639 case DEMANGLE_COMPONENT_VOLATILE:
4640 case DEMANGLE_COMPONENT_CONST:
4642 struct d_print_mod *pdpm;
4644 /* When printing arrays, it's possible to have cases where the
4645 same CV-qualifier gets pushed on the stack multiple times.
4646 We only need to print it once. */
4648 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4650 if (! pdpm->printed)
4652 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4653 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4654 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4655 break;
4656 if (pdpm->mod->type == dc->type)
4658 d_print_comp (dpi, options, d_left (dc));
4659 return;
4664 goto modifier;
4666 case DEMANGLE_COMPONENT_REFERENCE:
4667 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4669 /* Handle reference smashing: & + && = &. */
4670 const struct demangle_component *sub = d_left (dc);
4671 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4673 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4674 struct demangle_component *a;
4676 if (scope == NULL)
4678 /* This is the first time SUB has been traversed.
4679 We need to capture the current templates so
4680 they can be restored if SUB is reentered as a
4681 substitution. */
4682 d_save_scope (dpi, sub);
4683 if (d_print_saw_error (dpi))
4684 return;
4686 else
4688 /* This traversal is reentering SUB as a substition.
4689 Restore the original templates temporarily. */
4690 saved_templates = dpi->templates;
4691 dpi->templates = scope->templates;
4692 need_template_restore = 1;
4695 a = d_lookup_template_argument (dpi, sub);
4696 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4697 a = d_index_template_argument (a, dpi->pack_index);
4699 if (a == NULL)
4701 if (need_template_restore)
4702 dpi->templates = saved_templates;
4704 d_print_error (dpi);
4705 return;
4708 sub = a;
4711 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4712 || sub->type == dc->type)
4713 dc = sub;
4714 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4715 mod_inner = d_left (sub);
4717 /* Fall through. */
4719 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4720 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4721 case DEMANGLE_COMPONENT_CONST_THIS:
4722 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4723 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4724 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4725 case DEMANGLE_COMPONENT_POINTER:
4726 case DEMANGLE_COMPONENT_COMPLEX:
4727 case DEMANGLE_COMPONENT_IMAGINARY:
4728 modifier:
4730 /* We keep a list of modifiers on the stack. */
4731 struct d_print_mod dpm;
4733 dpm.next = dpi->modifiers;
4734 dpi->modifiers = &dpm;
4735 dpm.mod = dc;
4736 dpm.printed = 0;
4737 dpm.templates = dpi->templates;
4739 if (!mod_inner)
4740 mod_inner = d_left (dc);
4742 d_print_comp (dpi, options, mod_inner);
4744 /* If the modifier didn't get printed by the type, print it
4745 now. */
4746 if (! dpm.printed)
4747 d_print_mod (dpi, options, dc);
4749 dpi->modifiers = dpm.next;
4751 if (need_template_restore)
4752 dpi->templates = saved_templates;
4754 return;
4757 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4758 if ((options & DMGL_JAVA) == 0)
4759 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4760 dc->u.s_builtin.type->len);
4761 else
4762 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4763 dc->u.s_builtin.type->java_len);
4764 return;
4766 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4767 d_print_comp (dpi, options, d_left (dc));
4768 return;
4770 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4772 if ((options & DMGL_RET_POSTFIX) != 0)
4773 d_print_function_type (dpi,
4774 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4775 dc, dpi->modifiers);
4777 /* Print return type if present */
4778 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4779 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4780 d_left (dc));
4781 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4783 struct d_print_mod dpm;
4785 /* We must pass this type down as a modifier in order to
4786 print it in the right location. */
4787 dpm.next = dpi->modifiers;
4788 dpi->modifiers = &dpm;
4789 dpm.mod = dc;
4790 dpm.printed = 0;
4791 dpm.templates = dpi->templates;
4793 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4794 d_left (dc));
4796 dpi->modifiers = dpm.next;
4798 if (dpm.printed)
4799 return;
4801 /* In standard prefix notation, there is a space between the
4802 return type and the function signature. */
4803 if ((options & DMGL_RET_POSTFIX) == 0)
4804 d_append_char (dpi, ' ');
4807 if ((options & DMGL_RET_POSTFIX) == 0)
4808 d_print_function_type (dpi,
4809 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4810 dc, dpi->modifiers);
4812 return;
4815 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4817 struct d_print_mod *hold_modifiers;
4818 struct d_print_mod adpm[4];
4819 unsigned int i;
4820 struct d_print_mod *pdpm;
4822 /* We must pass this type down as a modifier in order to print
4823 multi-dimensional arrays correctly. If the array itself is
4824 CV-qualified, we act as though the element type were
4825 CV-qualified. We do this by copying the modifiers down
4826 rather than fiddling pointers, so that we don't wind up
4827 with a d_print_mod higher on the stack pointing into our
4828 stack frame after we return. */
4830 hold_modifiers = dpi->modifiers;
4832 adpm[0].next = hold_modifiers;
4833 dpi->modifiers = &adpm[0];
4834 adpm[0].mod = dc;
4835 adpm[0].printed = 0;
4836 adpm[0].templates = dpi->templates;
4838 i = 1;
4839 pdpm = hold_modifiers;
4840 while (pdpm != NULL
4841 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4842 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4843 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4845 if (! pdpm->printed)
4847 if (i >= sizeof adpm / sizeof adpm[0])
4849 d_print_error (dpi);
4850 return;
4853 adpm[i] = *pdpm;
4854 adpm[i].next = dpi->modifiers;
4855 dpi->modifiers = &adpm[i];
4856 pdpm->printed = 1;
4857 ++i;
4860 pdpm = pdpm->next;
4863 d_print_comp (dpi, options, d_right (dc));
4865 dpi->modifiers = hold_modifiers;
4867 if (adpm[0].printed)
4868 return;
4870 while (i > 1)
4872 --i;
4873 d_print_mod (dpi, options, adpm[i].mod);
4876 d_print_array_type (dpi, options, dc, dpi->modifiers);
4878 return;
4881 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4882 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4884 struct d_print_mod dpm;
4886 dpm.next = dpi->modifiers;
4887 dpi->modifiers = &dpm;
4888 dpm.mod = dc;
4889 dpm.printed = 0;
4890 dpm.templates = dpi->templates;
4892 d_print_comp (dpi, options, d_right (dc));
4894 /* If the modifier didn't get printed by the type, print it
4895 now. */
4896 if (! dpm.printed)
4897 d_print_mod (dpi, options, dc);
4899 dpi->modifiers = dpm.next;
4901 return;
4904 case DEMANGLE_COMPONENT_FIXED_TYPE:
4905 if (dc->u.s_fixed.sat)
4906 d_append_string (dpi, "_Sat ");
4907 /* Don't print "int _Accum". */
4908 if (dc->u.s_fixed.length->u.s_builtin.type
4909 != &cplus_demangle_builtin_types['i'-'a'])
4911 d_print_comp (dpi, options, dc->u.s_fixed.length);
4912 d_append_char (dpi, ' ');
4914 if (dc->u.s_fixed.accum)
4915 d_append_string (dpi, "_Accum");
4916 else
4917 d_append_string (dpi, "_Fract");
4918 return;
4920 case DEMANGLE_COMPONENT_ARGLIST:
4921 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4922 if (d_left (dc) != NULL)
4923 d_print_comp (dpi, options, d_left (dc));
4924 if (d_right (dc) != NULL)
4926 size_t len;
4927 unsigned long int flush_count;
4928 /* Make sure ", " isn't flushed by d_append_string, otherwise
4929 dpi->len -= 2 wouldn't work. */
4930 if (dpi->len >= sizeof (dpi->buf) - 2)
4931 d_print_flush (dpi);
4932 d_append_string (dpi, ", ");
4933 len = dpi->len;
4934 flush_count = dpi->flush_count;
4935 d_print_comp (dpi, options, d_right (dc));
4936 /* If that didn't print anything (which can happen with empty
4937 template argument packs), remove the comma and space. */
4938 if (dpi->flush_count == flush_count && dpi->len == len)
4939 dpi->len -= 2;
4941 return;
4943 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4945 struct demangle_component *type = d_left (dc);
4946 struct demangle_component *list = d_right (dc);
4948 if (type)
4949 d_print_comp (dpi, options, type);
4950 d_append_char (dpi, '{');
4951 d_print_comp (dpi, options, list);
4952 d_append_char (dpi, '}');
4954 return;
4956 case DEMANGLE_COMPONENT_OPERATOR:
4958 const struct demangle_operator_info *op = dc->u.s_operator.op;
4959 int len = op->len;
4961 d_append_string (dpi, "operator");
4962 /* Add a space before new/delete. */
4963 if (IS_LOWER (op->name[0]))
4964 d_append_char (dpi, ' ');
4965 /* Omit a trailing space. */
4966 if (op->name[len-1] == ' ')
4967 --len;
4968 d_append_buffer (dpi, op->name, len);
4969 return;
4972 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4973 d_append_string (dpi, "operator ");
4974 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4975 return;
4977 case DEMANGLE_COMPONENT_CONVERSION:
4978 d_append_string (dpi, "operator ");
4979 d_print_conversion (dpi, options, dc);
4980 return;
4982 case DEMANGLE_COMPONENT_NULLARY:
4983 d_print_expr_op (dpi, options, d_left (dc));
4984 return;
4986 case DEMANGLE_COMPONENT_UNARY:
4988 struct demangle_component *op = d_left (dc);
4989 struct demangle_component *operand = d_right (dc);
4990 const char *code = NULL;
4992 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4994 code = op->u.s_operator.op->code;
4995 if (!strcmp (code, "ad"))
4997 /* Don't print the argument list for the address of a
4998 function. */
4999 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5000 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5001 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5002 operand = d_left (operand);
5004 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5006 /* This indicates a suffix operator. */
5007 operand = d_left (operand);
5008 d_print_subexpr (dpi, options, operand);
5009 d_print_expr_op (dpi, options, op);
5010 return;
5014 if (op->type != DEMANGLE_COMPONENT_CAST)
5015 d_print_expr_op (dpi, options, op);
5016 else
5018 d_append_char (dpi, '(');
5019 d_print_cast (dpi, options, op);
5020 d_append_char (dpi, ')');
5022 if (code && !strcmp (code, "gs"))
5023 /* Avoid parens after '::'. */
5024 d_print_comp (dpi, options, operand);
5025 else if (code && !strcmp (code, "st"))
5026 /* Always print parens for sizeof (type). */
5028 d_append_char (dpi, '(');
5029 d_print_comp (dpi, options, operand);
5030 d_append_char (dpi, ')');
5032 else
5033 d_print_subexpr (dpi, options, operand);
5035 return;
5037 case DEMANGLE_COMPONENT_BINARY:
5038 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5040 d_print_error (dpi);
5041 return;
5044 if (op_is_new_cast (d_left (dc)))
5046 d_print_expr_op (dpi, options, d_left (dc));
5047 d_append_char (dpi, '<');
5048 d_print_comp (dpi, options, d_left (d_right (dc)));
5049 d_append_string (dpi, ">(");
5050 d_print_comp (dpi, options, d_right (d_right (dc)));
5051 d_append_char (dpi, ')');
5052 return;
5055 /* We wrap an expression which uses the greater-than operator in
5056 an extra layer of parens so that it does not get confused
5057 with the '>' which ends the template parameters. */
5058 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5059 && d_left (dc)->u.s_operator.op->len == 1
5060 && d_left (dc)->u.s_operator.op->name[0] == '>')
5061 d_append_char (dpi, '(');
5063 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5064 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5066 /* Function call used in an expression should not have printed types
5067 of the function arguments. Values of the function arguments still
5068 get printed below. */
5070 const struct demangle_component *func = d_left (d_right (dc));
5072 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5073 d_print_error (dpi);
5074 d_print_subexpr (dpi, options, d_left (func));
5076 else
5077 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5078 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5080 d_append_char (dpi, '[');
5081 d_print_comp (dpi, options, d_right (d_right (dc)));
5082 d_append_char (dpi, ']');
5084 else
5086 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5087 d_print_expr_op (dpi, options, d_left (dc));
5088 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5091 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5092 && d_left (dc)->u.s_operator.op->len == 1
5093 && d_left (dc)->u.s_operator.op->name[0] == '>')
5094 d_append_char (dpi, ')');
5096 return;
5098 case DEMANGLE_COMPONENT_BINARY_ARGS:
5099 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5100 d_print_error (dpi);
5101 return;
5103 case DEMANGLE_COMPONENT_TRINARY:
5104 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5105 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5107 d_print_error (dpi);
5108 return;
5111 struct demangle_component *op = d_left (dc);
5112 struct demangle_component *first = d_left (d_right (dc));
5113 struct demangle_component *second = d_left (d_right (d_right (dc)));
5114 struct demangle_component *third = d_right (d_right (d_right (dc)));
5116 if (!strcmp (op->u.s_operator.op->code, "qu"))
5118 d_print_subexpr (dpi, options, first);
5119 d_print_expr_op (dpi, options, op);
5120 d_print_subexpr (dpi, options, second);
5121 d_append_string (dpi, " : ");
5122 d_print_subexpr (dpi, options, third);
5124 else
5126 d_append_string (dpi, "new ");
5127 if (d_left (first) != NULL)
5129 d_print_subexpr (dpi, options, first);
5130 d_append_char (dpi, ' ');
5132 d_print_comp (dpi, options, second);
5133 if (third)
5134 d_print_subexpr (dpi, options, third);
5137 return;
5139 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5140 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5141 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5142 d_print_error (dpi);
5143 return;
5145 case DEMANGLE_COMPONENT_LITERAL:
5146 case DEMANGLE_COMPONENT_LITERAL_NEG:
5148 enum d_builtin_type_print tp;
5150 /* For some builtin types, produce simpler output. */
5151 tp = D_PRINT_DEFAULT;
5152 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5154 tp = d_left (dc)->u.s_builtin.type->print;
5155 switch (tp)
5157 case D_PRINT_INT:
5158 case D_PRINT_UNSIGNED:
5159 case D_PRINT_LONG:
5160 case D_PRINT_UNSIGNED_LONG:
5161 case D_PRINT_LONG_LONG:
5162 case D_PRINT_UNSIGNED_LONG_LONG:
5163 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5165 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5166 d_append_char (dpi, '-');
5167 d_print_comp (dpi, options, d_right (dc));
5168 switch (tp)
5170 default:
5171 break;
5172 case D_PRINT_UNSIGNED:
5173 d_append_char (dpi, 'u');
5174 break;
5175 case D_PRINT_LONG:
5176 d_append_char (dpi, 'l');
5177 break;
5178 case D_PRINT_UNSIGNED_LONG:
5179 d_append_string (dpi, "ul");
5180 break;
5181 case D_PRINT_LONG_LONG:
5182 d_append_string (dpi, "ll");
5183 break;
5184 case D_PRINT_UNSIGNED_LONG_LONG:
5185 d_append_string (dpi, "ull");
5186 break;
5188 return;
5190 break;
5192 case D_PRINT_BOOL:
5193 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5194 && d_right (dc)->u.s_name.len == 1
5195 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5197 switch (d_right (dc)->u.s_name.s[0])
5199 case '0':
5200 d_append_string (dpi, "false");
5201 return;
5202 case '1':
5203 d_append_string (dpi, "true");
5204 return;
5205 default:
5206 break;
5209 break;
5211 default:
5212 break;
5216 d_append_char (dpi, '(');
5217 d_print_comp (dpi, options, d_left (dc));
5218 d_append_char (dpi, ')');
5219 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5220 d_append_char (dpi, '-');
5221 if (tp == D_PRINT_FLOAT)
5222 d_append_char (dpi, '[');
5223 d_print_comp (dpi, options, d_right (dc));
5224 if (tp == D_PRINT_FLOAT)
5225 d_append_char (dpi, ']');
5227 return;
5229 case DEMANGLE_COMPONENT_NUMBER:
5230 d_append_num (dpi, dc->u.s_number.number);
5231 return;
5233 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5234 d_append_string (dpi, "java resource ");
5235 d_print_comp (dpi, options, d_left (dc));
5236 return;
5238 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5239 d_print_comp (dpi, options, d_left (dc));
5240 d_print_comp (dpi, options, d_right (dc));
5241 return;
5243 case DEMANGLE_COMPONENT_CHARACTER:
5244 d_append_char (dpi, dc->u.s_character.character);
5245 return;
5247 case DEMANGLE_COMPONENT_DECLTYPE:
5248 d_append_string (dpi, "decltype (");
5249 d_print_comp (dpi, options, d_left (dc));
5250 d_append_char (dpi, ')');
5251 return;
5253 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5255 int len;
5256 int i;
5257 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5258 if (a == NULL)
5260 /* d_find_pack won't find anything if the only packs involved
5261 in this expansion are function parameter packs; in that
5262 case, just print the pattern and "...". */
5263 d_print_subexpr (dpi, options, d_left (dc));
5264 d_append_string (dpi, "...");
5265 return;
5268 len = d_pack_length (a);
5269 dc = d_left (dc);
5270 for (i = 0; i < len; ++i)
5272 dpi->pack_index = i;
5273 d_print_comp (dpi, options, dc);
5274 if (i < len-1)
5275 d_append_string (dpi, ", ");
5278 return;
5280 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5282 long num = dc->u.s_number.number;
5283 if (num == 0)
5284 d_append_string (dpi, "this");
5285 else
5287 d_append_string (dpi, "{parm#");
5288 d_append_num (dpi, num);
5289 d_append_char (dpi, '}');
5292 return;
5294 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5295 d_append_string (dpi, "global constructors keyed to ");
5296 d_print_comp (dpi, options, dc->u.s_binary.left);
5297 return;
5299 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5300 d_append_string (dpi, "global destructors keyed to ");
5301 d_print_comp (dpi, options, dc->u.s_binary.left);
5302 return;
5304 case DEMANGLE_COMPONENT_LAMBDA:
5305 d_append_string (dpi, "{lambda(");
5306 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5307 d_append_string (dpi, ")#");
5308 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5309 d_append_char (dpi, '}');
5310 return;
5312 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5313 d_append_string (dpi, "{unnamed type#");
5314 d_append_num (dpi, dc->u.s_number.number + 1);
5315 d_append_char (dpi, '}');
5316 return;
5318 case DEMANGLE_COMPONENT_CLONE:
5319 d_print_comp (dpi, options, d_left (dc));
5320 d_append_string (dpi, " [clone ");
5321 d_print_comp (dpi, options, d_right (dc));
5322 d_append_char (dpi, ']');
5323 return;
5325 default:
5326 d_print_error (dpi);
5327 return;
5331 /* Print a Java dentifier. For Java we try to handle encoded extended
5332 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5333 so we don't it for C++. Characters are encoded as
5334 __U<hex-char>+_. */
5336 static void
5337 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5339 const char *p;
5340 const char *end;
5342 end = name + len;
5343 for (p = name; p < end; ++p)
5345 if (end - p > 3
5346 && p[0] == '_'
5347 && p[1] == '_'
5348 && p[2] == 'U')
5350 unsigned long c;
5351 const char *q;
5353 c = 0;
5354 for (q = p + 3; q < end; ++q)
5356 int dig;
5358 if (IS_DIGIT (*q))
5359 dig = *q - '0';
5360 else if (*q >= 'A' && *q <= 'F')
5361 dig = *q - 'A' + 10;
5362 else if (*q >= 'a' && *q <= 'f')
5363 dig = *q - 'a' + 10;
5364 else
5365 break;
5367 c = c * 16 + dig;
5369 /* If the Unicode character is larger than 256, we don't try
5370 to deal with it here. FIXME. */
5371 if (q < end && *q == '_' && c < 256)
5373 d_append_char (dpi, c);
5374 p = q;
5375 continue;
5379 d_append_char (dpi, *p);
5383 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5384 qualifiers on this after printing a function. */
5386 static void
5387 d_print_mod_list (struct d_print_info *dpi, int options,
5388 struct d_print_mod *mods, int suffix)
5390 struct d_print_template *hold_dpt;
5392 if (mods == NULL || d_print_saw_error (dpi))
5393 return;
5395 if (mods->printed
5396 || (! suffix
5397 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5398 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5399 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5400 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5401 || (mods->mod->type
5402 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5404 d_print_mod_list (dpi, options, mods->next, suffix);
5405 return;
5408 mods->printed = 1;
5410 hold_dpt = dpi->templates;
5411 dpi->templates = mods->templates;
5413 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5415 d_print_function_type (dpi, options, mods->mod, mods->next);
5416 dpi->templates = hold_dpt;
5417 return;
5419 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5421 d_print_array_type (dpi, options, mods->mod, mods->next);
5422 dpi->templates = hold_dpt;
5423 return;
5425 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5427 struct d_print_mod *hold_modifiers;
5428 struct demangle_component *dc;
5430 /* When this is on the modifier stack, we have pulled any
5431 qualifiers off the right argument already. Otherwise, we
5432 print it as usual, but don't let the left argument see any
5433 modifiers. */
5435 hold_modifiers = dpi->modifiers;
5436 dpi->modifiers = NULL;
5437 d_print_comp (dpi, options, d_left (mods->mod));
5438 dpi->modifiers = hold_modifiers;
5440 if ((options & DMGL_JAVA) == 0)
5441 d_append_string (dpi, "::");
5442 else
5443 d_append_char (dpi, '.');
5445 dc = d_right (mods->mod);
5447 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5449 d_append_string (dpi, "{default arg#");
5450 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5451 d_append_string (dpi, "}::");
5452 dc = dc->u.s_unary_num.sub;
5455 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5456 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5457 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5458 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5459 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5460 dc = d_left (dc);
5462 d_print_comp (dpi, options, dc);
5464 dpi->templates = hold_dpt;
5465 return;
5468 d_print_mod (dpi, options, mods->mod);
5470 dpi->templates = hold_dpt;
5472 d_print_mod_list (dpi, options, mods->next, suffix);
5475 /* Print a modifier. */
5477 static void
5478 d_print_mod (struct d_print_info *dpi, int options,
5479 const struct demangle_component *mod)
5481 switch (mod->type)
5483 case DEMANGLE_COMPONENT_RESTRICT:
5484 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5485 d_append_string (dpi, " restrict");
5486 return;
5487 case DEMANGLE_COMPONENT_VOLATILE:
5488 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5489 d_append_string (dpi, " volatile");
5490 return;
5491 case DEMANGLE_COMPONENT_CONST:
5492 case DEMANGLE_COMPONENT_CONST_THIS:
5493 d_append_string (dpi, " const");
5494 return;
5495 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5496 d_append_char (dpi, ' ');
5497 d_print_comp (dpi, options, d_right (mod));
5498 return;
5499 case DEMANGLE_COMPONENT_POINTER:
5500 /* There is no pointer symbol in Java. */
5501 if ((options & DMGL_JAVA) == 0)
5502 d_append_char (dpi, '*');
5503 return;
5504 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5505 /* For the ref-qualifier, put a space before the &. */
5506 d_append_char (dpi, ' ');
5507 case DEMANGLE_COMPONENT_REFERENCE:
5508 d_append_char (dpi, '&');
5509 return;
5510 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5511 d_append_char (dpi, ' ');
5512 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5513 d_append_string (dpi, "&&");
5514 return;
5515 case DEMANGLE_COMPONENT_COMPLEX:
5516 d_append_string (dpi, "complex ");
5517 return;
5518 case DEMANGLE_COMPONENT_IMAGINARY:
5519 d_append_string (dpi, "imaginary ");
5520 return;
5521 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5522 if (d_last_char (dpi) != '(')
5523 d_append_char (dpi, ' ');
5524 d_print_comp (dpi, options, d_left (mod));
5525 d_append_string (dpi, "::*");
5526 return;
5527 case DEMANGLE_COMPONENT_TYPED_NAME:
5528 d_print_comp (dpi, options, d_left (mod));
5529 return;
5530 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5531 d_append_string (dpi, " __vector(");
5532 d_print_comp (dpi, options, d_left (mod));
5533 d_append_char (dpi, ')');
5534 return;
5536 default:
5537 /* Otherwise, we have something that won't go back on the
5538 modifier stack, so we can just print it. */
5539 d_print_comp (dpi, options, mod);
5540 return;
5544 /* Print a function type, except for the return type. */
5546 static void
5547 d_print_function_type (struct d_print_info *dpi, int options,
5548 const struct demangle_component *dc,
5549 struct d_print_mod *mods)
5551 int need_paren;
5552 int need_space;
5553 struct d_print_mod *p;
5554 struct d_print_mod *hold_modifiers;
5556 need_paren = 0;
5557 need_space = 0;
5558 for (p = mods; p != NULL; p = p->next)
5560 if (p->printed)
5561 break;
5563 switch (p->mod->type)
5565 case DEMANGLE_COMPONENT_POINTER:
5566 case DEMANGLE_COMPONENT_REFERENCE:
5567 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5568 need_paren = 1;
5569 break;
5570 case DEMANGLE_COMPONENT_RESTRICT:
5571 case DEMANGLE_COMPONENT_VOLATILE:
5572 case DEMANGLE_COMPONENT_CONST:
5573 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5574 case DEMANGLE_COMPONENT_COMPLEX:
5575 case DEMANGLE_COMPONENT_IMAGINARY:
5576 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5577 need_space = 1;
5578 need_paren = 1;
5579 break;
5580 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5581 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5582 case DEMANGLE_COMPONENT_CONST_THIS:
5583 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5584 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5585 break;
5586 default:
5587 break;
5589 if (need_paren)
5590 break;
5593 if (need_paren)
5595 if (! need_space)
5597 if (d_last_char (dpi) != '('
5598 && d_last_char (dpi) != '*')
5599 need_space = 1;
5601 if (need_space && d_last_char (dpi) != ' ')
5602 d_append_char (dpi, ' ');
5603 d_append_char (dpi, '(');
5606 hold_modifiers = dpi->modifiers;
5607 dpi->modifiers = NULL;
5609 d_print_mod_list (dpi, options, mods, 0);
5611 if (need_paren)
5612 d_append_char (dpi, ')');
5614 d_append_char (dpi, '(');
5616 if (d_right (dc) != NULL)
5617 d_print_comp (dpi, options, d_right (dc));
5619 d_append_char (dpi, ')');
5621 d_print_mod_list (dpi, options, mods, 1);
5623 dpi->modifiers = hold_modifiers;
5626 /* Print an array type, except for the element type. */
5628 static void
5629 d_print_array_type (struct d_print_info *dpi, int options,
5630 const struct demangle_component *dc,
5631 struct d_print_mod *mods)
5633 int need_space;
5635 need_space = 1;
5636 if (mods != NULL)
5638 int need_paren;
5639 struct d_print_mod *p;
5641 need_paren = 0;
5642 for (p = mods; p != NULL; p = p->next)
5644 if (! p->printed)
5646 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5648 need_space = 0;
5649 break;
5651 else
5653 need_paren = 1;
5654 need_space = 1;
5655 break;
5660 if (need_paren)
5661 d_append_string (dpi, " (");
5663 d_print_mod_list (dpi, options, mods, 0);
5665 if (need_paren)
5666 d_append_char (dpi, ')');
5669 if (need_space)
5670 d_append_char (dpi, ' ');
5672 d_append_char (dpi, '[');
5674 if (d_left (dc) != NULL)
5675 d_print_comp (dpi, options, d_left (dc));
5677 d_append_char (dpi, ']');
5680 /* Print an operator in an expression. */
5682 static void
5683 d_print_expr_op (struct d_print_info *dpi, int options,
5684 const struct demangle_component *dc)
5686 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5687 d_append_buffer (dpi, dc->u.s_operator.op->name,
5688 dc->u.s_operator.op->len);
5689 else
5690 d_print_comp (dpi, options, dc);
5693 /* Print a cast. */
5695 static void
5696 d_print_cast (struct d_print_info *dpi, int options,
5697 const struct demangle_component *dc)
5699 d_print_comp (dpi, options, d_left (dc));
5702 /* Print a conversion operator. */
5704 static void
5705 d_print_conversion (struct d_print_info *dpi, int options,
5706 const struct demangle_component *dc)
5708 struct d_print_template dpt;
5710 /* For a conversion operator, we need the template parameters from
5711 the enclosing template in scope for processing the type. */
5712 if (dpi->current_template != NULL)
5714 dpt.next = dpi->templates;
5715 dpi->templates = &dpt;
5716 dpt.template_decl = dpi->current_template;
5719 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5721 d_print_comp (dpi, options, d_left (dc));
5722 if (dpi->current_template != NULL)
5723 dpi->templates = dpt.next;
5725 else
5727 d_print_comp (dpi, options, d_left (d_left (dc)));
5729 /* For a templated cast operator, we need to remove the template
5730 parameters from scope after printing the operator name,
5731 so we need to handle the template printing here. */
5732 if (dpi->current_template != NULL)
5733 dpi->templates = dpt.next;
5735 if (d_last_char (dpi) == '<')
5736 d_append_char (dpi, ' ');
5737 d_append_char (dpi, '<');
5738 d_print_comp (dpi, options, d_right (d_left (dc)));
5739 /* Avoid generating two consecutive '>' characters, to avoid
5740 the C++ syntactic ambiguity. */
5741 if (d_last_char (dpi) == '>')
5742 d_append_char (dpi, ' ');
5743 d_append_char (dpi, '>');
5747 /* Initialize the information structure we use to pass around
5748 information. */
5750 CP_STATIC_IF_GLIBCPP_V3
5751 void
5752 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5753 struct d_info *di)
5755 di->s = mangled;
5756 di->send = mangled + len;
5757 di->options = options;
5759 di->n = mangled;
5761 /* We can not need more components than twice the number of chars in
5762 the mangled string. Most components correspond directly to
5763 chars, but the ARGLIST types are exceptions. */
5764 di->num_comps = 2 * len;
5765 di->next_comp = 0;
5767 /* Similarly, we can not need more substitutions than there are
5768 chars in the mangled string. */
5769 di->num_subs = len;
5770 di->next_sub = 0;
5771 di->did_subs = 0;
5773 di->last_name = NULL;
5775 di->expansion = 0;
5776 di->is_expression = 0;
5777 di->is_conversion = 0;
5780 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5781 mangled name, return strings in repeated callback giving the demangled
5782 name. OPTIONS is the usual libiberty demangler options. On success,
5783 this returns 1. On failure, returns 0. */
5785 static int
5786 d_demangle_callback (const char *mangled, int options,
5787 demangle_callbackref callback, void *opaque)
5789 enum
5791 DCT_TYPE,
5792 DCT_MANGLED,
5793 DCT_GLOBAL_CTORS,
5794 DCT_GLOBAL_DTORS
5796 type;
5797 struct d_info di;
5798 struct demangle_component *dc;
5799 int status;
5801 if (mangled[0] == '_' && mangled[1] == 'Z')
5802 type = DCT_MANGLED;
5803 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5804 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5805 && (mangled[9] == 'D' || mangled[9] == 'I')
5806 && mangled[10] == '_')
5807 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5808 else
5810 if ((options & DMGL_TYPES) == 0)
5811 return 0;
5812 type = DCT_TYPE;
5815 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5818 #ifdef CP_DYNAMIC_ARRAYS
5819 __extension__ struct demangle_component comps[di.num_comps];
5820 __extension__ struct demangle_component *subs[di.num_subs];
5822 di.comps = comps;
5823 di.subs = subs;
5824 #else
5825 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5826 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5827 #endif
5829 switch (type)
5831 case DCT_TYPE:
5832 dc = cplus_demangle_type (&di);
5833 break;
5834 case DCT_MANGLED:
5835 dc = cplus_demangle_mangled_name (&di, 1);
5836 break;
5837 case DCT_GLOBAL_CTORS:
5838 case DCT_GLOBAL_DTORS:
5839 d_advance (&di, 11);
5840 dc = d_make_comp (&di,
5841 (type == DCT_GLOBAL_CTORS
5842 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5843 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5844 d_make_demangle_mangled_name (&di, d_str (&di)),
5845 NULL);
5846 d_advance (&di, strlen (d_str (&di)));
5847 break;
5848 default:
5849 abort (); /* We have listed all the cases. */
5852 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5853 mangled string, then we didn't successfully demangle it. If
5854 DMGL_PARAMS is not set, we didn't look at the trailing
5855 parameters. */
5856 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5857 dc = NULL;
5859 #ifdef CP_DEMANGLE_DEBUG
5860 d_dump (dc, 0);
5861 #endif
5863 status = (dc != NULL)
5864 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5865 : 0;
5868 return status;
5871 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5872 name, return a buffer allocated with malloc holding the demangled
5873 name. OPTIONS is the usual libiberty demangler options. On
5874 success, this sets *PALC to the allocated size of the returned
5875 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5876 a memory allocation failure, and returns NULL. */
5878 static char *
5879 d_demangle (const char *mangled, int options, size_t *palc)
5881 struct d_growable_string dgs;
5882 int status;
5884 d_growable_string_init (&dgs, 0);
5886 status = d_demangle_callback (mangled, options,
5887 d_growable_string_callback_adapter, &dgs);
5888 if (status == 0)
5890 free (dgs.buf);
5891 *palc = 0;
5892 return NULL;
5895 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5896 return dgs.buf;
5899 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5901 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5903 /* ia64 ABI-mandated entry point in the C++ runtime library for
5904 performing demangling. MANGLED_NAME is a NUL-terminated character
5905 string containing the name to be demangled.
5907 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5908 *LENGTH bytes, into which the demangled name is stored. If
5909 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5910 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5911 is placed in a region of memory allocated with malloc.
5913 If LENGTH is non-NULL, the length of the buffer containing the
5914 demangled name, is placed in *LENGTH.
5916 The return value is a pointer to the start of the NUL-terminated
5917 demangled name, or NULL if the demangling fails. The caller is
5918 responsible for deallocating this memory using free.
5920 *STATUS is set to one of the following values:
5921 0: The demangling operation succeeded.
5922 -1: A memory allocation failure occurred.
5923 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5924 -3: One of the arguments is invalid.
5926 The demangling is performed using the C++ ABI mangling rules, with
5927 GNU extensions. */
5929 char *
5930 __cxa_demangle (const char *mangled_name, char *output_buffer,
5931 size_t *length, int *status)
5933 char *demangled;
5934 size_t alc;
5936 if (mangled_name == NULL)
5938 if (status != NULL)
5939 *status = -3;
5940 return NULL;
5943 if (output_buffer != NULL && length == NULL)
5945 if (status != NULL)
5946 *status = -3;
5947 return NULL;
5950 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5952 if (demangled == NULL)
5954 if (status != NULL)
5956 if (alc == 1)
5957 *status = -1;
5958 else
5959 *status = -2;
5961 return NULL;
5964 if (output_buffer == NULL)
5966 if (length != NULL)
5967 *length = alc;
5969 else
5971 if (strlen (demangled) < *length)
5973 strcpy (output_buffer, demangled);
5974 free (demangled);
5975 demangled = output_buffer;
5977 else
5979 free (output_buffer);
5980 *length = alc;
5984 if (status != NULL)
5985 *status = 0;
5987 return demangled;
5990 extern int __gcclibcxx_demangle_callback (const char *,
5991 void (*)
5992 (const char *, size_t, void *),
5993 void *);
5995 /* Alternative, allocationless entry point in the C++ runtime library
5996 for performing demangling. MANGLED_NAME is a NUL-terminated character
5997 string containing the name to be demangled.
5999 CALLBACK is a callback function, called with demangled string
6000 segments as demangling progresses; it is called at least once,
6001 but may be called more than once. OPAQUE is a generalized pointer
6002 used as a callback argument.
6004 The return code is one of the following values, equivalent to
6005 the STATUS values of __cxa_demangle() (excluding -1, since this
6006 function performs no memory allocations):
6007 0: The demangling operation succeeded.
6008 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6009 -3: One of the arguments is invalid.
6011 The demangling is performed using the C++ ABI mangling rules, with
6012 GNU extensions. */
6015 __gcclibcxx_demangle_callback (const char *mangled_name,
6016 void (*callback) (const char *, size_t, void *),
6017 void *opaque)
6019 int status;
6021 if (mangled_name == NULL || callback == NULL)
6022 return -3;
6024 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6025 callback, opaque);
6026 if (status == 0)
6027 return -2;
6029 return 0;
6032 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6034 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6035 mangled name, return a buffer allocated with malloc holding the
6036 demangled name. Otherwise, return NULL. */
6038 char *
6039 cplus_demangle_v3 (const char *mangled, int options)
6041 size_t alc;
6043 return d_demangle (mangled, options, &alc);
6047 cplus_demangle_v3_callback (const char *mangled, int options,
6048 demangle_callbackref callback, void *opaque)
6050 return d_demangle_callback (mangled, options, callback, opaque);
6053 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6054 conventions, but the output formatting is a little different.
6055 This instructs the C++ demangler not to emit pointer characters ("*"), to
6056 use Java's namespace separator symbol ("." instead of "::"), and to output
6057 JArray<TYPE> as TYPE[]. */
6059 char *
6060 java_demangle_v3 (const char *mangled)
6062 size_t alc;
6064 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6068 java_demangle_v3_callback (const char *mangled,
6069 demangle_callbackref callback, void *opaque)
6071 return d_demangle_callback (mangled,
6072 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6073 callback, opaque);
6076 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6078 #ifndef IN_GLIBCPP_V3
6080 /* Demangle a string in order to find out whether it is a constructor
6081 or destructor. Return non-zero on success. Set *CTOR_KIND and
6082 *DTOR_KIND appropriately. */
6084 static int
6085 is_ctor_or_dtor (const char *mangled,
6086 enum gnu_v3_ctor_kinds *ctor_kind,
6087 enum gnu_v3_dtor_kinds *dtor_kind)
6089 struct d_info di;
6090 struct demangle_component *dc;
6091 int ret;
6093 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6094 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6096 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6099 #ifdef CP_DYNAMIC_ARRAYS
6100 __extension__ struct demangle_component comps[di.num_comps];
6101 __extension__ struct demangle_component *subs[di.num_subs];
6103 di.comps = comps;
6104 di.subs = subs;
6105 #else
6106 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6107 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6108 #endif
6110 dc = cplus_demangle_mangled_name (&di, 1);
6112 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6113 to demangle the entire string. */
6115 ret = 0;
6116 while (dc != NULL)
6118 switch (dc->type)
6120 /* These cannot appear on a constructor or destructor. */
6121 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6122 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6123 case DEMANGLE_COMPONENT_CONST_THIS:
6124 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6125 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6126 default:
6127 dc = NULL;
6128 break;
6129 case DEMANGLE_COMPONENT_TYPED_NAME:
6130 case DEMANGLE_COMPONENT_TEMPLATE:
6131 dc = d_left (dc);
6132 break;
6133 case DEMANGLE_COMPONENT_QUAL_NAME:
6134 case DEMANGLE_COMPONENT_LOCAL_NAME:
6135 dc = d_right (dc);
6136 break;
6137 case DEMANGLE_COMPONENT_CTOR:
6138 *ctor_kind = dc->u.s_ctor.kind;
6139 ret = 1;
6140 dc = NULL;
6141 break;
6142 case DEMANGLE_COMPONENT_DTOR:
6143 *dtor_kind = dc->u.s_dtor.kind;
6144 ret = 1;
6145 dc = NULL;
6146 break;
6151 return ret;
6154 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6155 name. A non-zero return indicates the type of constructor. */
6157 enum gnu_v3_ctor_kinds
6158 is_gnu_v3_mangled_ctor (const char *name)
6160 enum gnu_v3_ctor_kinds ctor_kind;
6161 enum gnu_v3_dtor_kinds dtor_kind;
6163 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6164 return (enum gnu_v3_ctor_kinds) 0;
6165 return ctor_kind;
6169 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6170 name. A non-zero return indicates the type of destructor. */
6172 enum gnu_v3_dtor_kinds
6173 is_gnu_v3_mangled_dtor (const char *name)
6175 enum gnu_v3_ctor_kinds ctor_kind;
6176 enum gnu_v3_dtor_kinds dtor_kind;
6178 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6179 return (enum gnu_v3_dtor_kinds) 0;
6180 return dtor_kind;
6183 #endif /* IN_GLIBCPP_V3 */
6185 #ifdef STANDALONE_DEMANGLER
6187 #include "getopt.h"
6188 #include "dyn-string.h"
6190 static void print_usage (FILE* fp, int exit_value);
6192 #define IS_ALPHA(CHAR) \
6193 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6194 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6196 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6197 #define is_mangled_char(CHAR) \
6198 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6199 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6201 /* The name of this program, as invoked. */
6202 const char* program_name;
6204 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6206 static void
6207 print_usage (FILE* fp, int exit_value)
6209 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6210 fprintf (fp, "Options:\n");
6211 fprintf (fp, " -h,--help Display this message.\n");
6212 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6213 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6214 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6216 exit (exit_value);
6219 /* Option specification for getopt_long. */
6220 static const struct option long_options[] =
6222 { "help", no_argument, NULL, 'h' },
6223 { "no-params", no_argument, NULL, 'p' },
6224 { "verbose", no_argument, NULL, 'v' },
6225 { NULL, no_argument, NULL, 0 },
6228 /* Main entry for a demangling filter executable. It will demangle
6229 its command line arguments, if any. If none are provided, it will
6230 filter stdin to stdout, replacing any recognized mangled C++ names
6231 with their demangled equivalents. */
6234 main (int argc, char *argv[])
6236 int i;
6237 int opt_char;
6238 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6240 /* Use the program name of this program, as invoked. */
6241 program_name = argv[0];
6243 /* Parse options. */
6246 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6247 switch (opt_char)
6249 case '?': /* Unrecognized option. */
6250 print_usage (stderr, 1);
6251 break;
6253 case 'h':
6254 print_usage (stdout, 0);
6255 break;
6257 case 'p':
6258 options &= ~ DMGL_PARAMS;
6259 break;
6261 case 'v':
6262 options |= DMGL_VERBOSE;
6263 break;
6266 while (opt_char != -1);
6268 if (optind == argc)
6269 /* No command line arguments were provided. Filter stdin. */
6271 dyn_string_t mangled = dyn_string_new (3);
6272 char *s;
6274 /* Read all of input. */
6275 while (!feof (stdin))
6277 char c;
6279 /* Pile characters into mangled until we hit one that can't
6280 occur in a mangled name. */
6281 c = getchar ();
6282 while (!feof (stdin) && is_mangled_char (c))
6284 dyn_string_append_char (mangled, c);
6285 if (feof (stdin))
6286 break;
6287 c = getchar ();
6290 if (dyn_string_length (mangled) > 0)
6292 #ifdef IN_GLIBCPP_V3
6293 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6294 #else
6295 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6296 #endif
6298 if (s != NULL)
6300 fputs (s, stdout);
6301 free (s);
6303 else
6305 /* It might not have been a mangled name. Print the
6306 original text. */
6307 fputs (dyn_string_buf (mangled), stdout);
6310 dyn_string_clear (mangled);
6313 /* If we haven't hit EOF yet, we've read one character that
6314 can't occur in a mangled name, so print it out. */
6315 if (!feof (stdin))
6316 putchar (c);
6319 dyn_string_delete (mangled);
6321 else
6322 /* Demangle command line arguments. */
6324 /* Loop over command line arguments. */
6325 for (i = optind; i < argc; ++i)
6327 char *s;
6328 #ifdef IN_GLIBCPP_V3
6329 int status;
6330 #endif
6332 /* Attempt to demangle. */
6333 #ifdef IN_GLIBCPP_V3
6334 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6335 #else
6336 s = cplus_demangle_v3 (argv[i], options);
6337 #endif
6339 /* If it worked, print the demangled name. */
6340 if (s != NULL)
6342 printf ("%s\n", s);
6343 free (s);
6345 else
6347 #ifdef IN_GLIBCPP_V3
6348 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6349 #else
6350 fprintf (stderr, "Failed: %s\n", argv[i]);
6351 #endif
6356 return 0;
6359 #endif /* STANDALONE_DEMANGLER */