2013-11-05 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libiberty / cp-demangle.c
blob7be98048565930b12dd4e903c985e71077f6261d
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 enum { D_PRINT_BUFFER_LENGTH = 256 };
291 struct d_print_info
293 /* Fixed-length allocated buffer for demangled data, flushed to the
294 callback with a NUL termination once full. */
295 char buf[D_PRINT_BUFFER_LENGTH];
296 /* Current length of data in buffer. */
297 size_t len;
298 /* The last character printed, saved individually so that it survives
299 any buffer flush. */
300 char last_char;
301 /* Callback function to handle demangled buffer flush. */
302 demangle_callbackref callback;
303 /* Opaque callback argument. */
304 void *opaque;
305 /* The current list of templates, if any. */
306 struct d_print_template *templates;
307 /* The current list of modifiers (e.g., pointer, reference, etc.),
308 if any. */
309 struct d_print_mod *modifiers;
310 /* Set to 1 if we saw a demangling error. */
311 int demangle_failure;
312 /* The current index into any template argument packs we are using
313 for printing. */
314 int pack_index;
315 /* Number of d_print_flush calls so far. */
316 unsigned long int flush_count;
317 /* Array of saved scopes for evaluating substitutions. */
318 struct d_saved_scope *saved_scopes;
319 /* Number of saved scopes in the above array. */
320 int num_saved_scopes;
323 #ifdef CP_DEMANGLE_DEBUG
324 static void d_dump (struct demangle_component *, int);
325 #endif
327 static struct demangle_component *
328 d_make_empty (struct d_info *);
330 static struct demangle_component *
331 d_make_comp (struct d_info *, enum demangle_component_type,
332 struct demangle_component *,
333 struct demangle_component *);
335 static struct demangle_component *
336 d_make_name (struct d_info *, const char *, int);
338 static struct demangle_component *
339 d_make_demangle_mangled_name (struct d_info *, const char *);
341 static struct demangle_component *
342 d_make_builtin_type (struct d_info *,
343 const struct demangle_builtin_type_info *);
345 static struct demangle_component *
346 d_make_operator (struct d_info *,
347 const struct demangle_operator_info *);
349 static struct demangle_component *
350 d_make_extended_operator (struct d_info *, int,
351 struct demangle_component *);
353 static struct demangle_component *
354 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
355 struct demangle_component *);
357 static struct demangle_component *
358 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
359 struct demangle_component *);
361 static struct demangle_component *
362 d_make_template_param (struct d_info *, long);
364 static struct demangle_component *
365 d_make_sub (struct d_info *, const char *, int);
367 static int
368 has_return_type (struct demangle_component *);
370 static int
371 is_ctor_dtor_or_conversion (struct demangle_component *);
373 static struct demangle_component *d_encoding (struct d_info *, int);
375 static struct demangle_component *d_name (struct d_info *);
377 static struct demangle_component *d_nested_name (struct d_info *);
379 static struct demangle_component *d_prefix (struct d_info *);
381 static struct demangle_component *d_unqualified_name (struct d_info *);
383 static struct demangle_component *d_source_name (struct d_info *);
385 static long d_number (struct d_info *);
387 static struct demangle_component *d_identifier (struct d_info *, int);
389 static struct demangle_component *d_operator_name (struct d_info *);
391 static struct demangle_component *d_special_name (struct d_info *);
393 static int d_call_offset (struct d_info *, int);
395 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
397 static struct demangle_component **
398 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
400 static struct demangle_component *
401 d_ref_qualifier (struct d_info *, struct demangle_component *);
403 static struct demangle_component *
404 d_function_type (struct d_info *);
406 static struct demangle_component *
407 d_bare_function_type (struct d_info *, int);
409 static struct demangle_component *
410 d_class_enum_type (struct d_info *);
412 static struct demangle_component *d_array_type (struct d_info *);
414 static struct demangle_component *d_vector_type (struct d_info *);
416 static struct demangle_component *
417 d_pointer_to_member_type (struct d_info *);
419 static struct demangle_component *
420 d_template_param (struct d_info *);
422 static struct demangle_component *d_template_args (struct d_info *);
424 static struct demangle_component *
425 d_template_arg (struct d_info *);
427 static struct demangle_component *d_expression (struct d_info *);
429 static struct demangle_component *d_expr_primary (struct d_info *);
431 static struct demangle_component *d_local_name (struct d_info *);
433 static int d_discriminator (struct d_info *);
435 static struct demangle_component *d_lambda (struct d_info *);
437 static struct demangle_component *d_unnamed_type (struct d_info *);
439 static struct demangle_component *
440 d_clone_suffix (struct d_info *, struct demangle_component *);
442 static int
443 d_add_substitution (struct d_info *, struct demangle_component *);
445 static struct demangle_component *d_substitution (struct d_info *, int);
447 static void d_growable_string_init (struct d_growable_string *, size_t);
449 static inline void
450 d_growable_string_resize (struct d_growable_string *, size_t);
452 static inline void
453 d_growable_string_append_buffer (struct d_growable_string *,
454 const char *, size_t);
455 static void
456 d_growable_string_callback_adapter (const char *, size_t, void *);
458 static void
459 d_print_init (struct d_print_info *, demangle_callbackref, void *);
461 static inline void d_print_error (struct d_print_info *);
463 static inline int d_print_saw_error (struct d_print_info *);
465 static inline void d_print_flush (struct d_print_info *);
467 static inline void d_append_char (struct d_print_info *, char);
469 static inline void d_append_buffer (struct d_print_info *,
470 const char *, size_t);
472 static inline void d_append_string (struct d_print_info *, const char *);
474 static inline char d_last_char (struct d_print_info *);
476 static void
477 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
479 static void
480 d_print_java_identifier (struct d_print_info *, const char *, int);
482 static void
483 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
485 static void
486 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
488 static void
489 d_print_function_type (struct d_print_info *, int,
490 const struct demangle_component *,
491 struct d_print_mod *);
493 static void
494 d_print_array_type (struct d_print_info *, int,
495 const struct demangle_component *,
496 struct d_print_mod *);
498 static void
499 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
501 static void
502 d_print_cast (struct d_print_info *, int, const struct demangle_component *);
504 static int d_demangle_callback (const char *, int,
505 demangle_callbackref, void *);
506 static char *d_demangle (const char *, int, size_t *);
508 #ifdef CP_DEMANGLE_DEBUG
510 static void
511 d_dump (struct demangle_component *dc, int indent)
513 int i;
515 if (dc == NULL)
517 if (indent == 0)
518 printf ("failed demangling\n");
519 return;
522 for (i = 0; i < indent; ++i)
523 putchar (' ');
525 switch (dc->type)
527 case DEMANGLE_COMPONENT_NAME:
528 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
529 return;
530 case DEMANGLE_COMPONENT_TAGGED_NAME:
531 printf ("tagged name\n");
532 d_dump (dc->u.s_binary.left, indent + 2);
533 d_dump (dc->u.s_binary.right, indent + 2);
534 return;
535 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
536 printf ("template parameter %ld\n", dc->u.s_number.number);
537 return;
538 case DEMANGLE_COMPONENT_CTOR:
539 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
540 d_dump (dc->u.s_ctor.name, indent + 2);
541 return;
542 case DEMANGLE_COMPONENT_DTOR:
543 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
544 d_dump (dc->u.s_dtor.name, indent + 2);
545 return;
546 case DEMANGLE_COMPONENT_SUB_STD:
547 printf ("standard substitution %s\n", dc->u.s_string.string);
548 return;
549 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
550 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
551 return;
552 case DEMANGLE_COMPONENT_OPERATOR:
553 printf ("operator %s\n", dc->u.s_operator.op->name);
554 return;
555 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
556 printf ("extended operator with %d args\n",
557 dc->u.s_extended_operator.args);
558 d_dump (dc->u.s_extended_operator.name, indent + 2);
559 return;
561 case DEMANGLE_COMPONENT_QUAL_NAME:
562 printf ("qualified name\n");
563 break;
564 case DEMANGLE_COMPONENT_LOCAL_NAME:
565 printf ("local name\n");
566 break;
567 case DEMANGLE_COMPONENT_TYPED_NAME:
568 printf ("typed name\n");
569 break;
570 case DEMANGLE_COMPONENT_TEMPLATE:
571 printf ("template\n");
572 break;
573 case DEMANGLE_COMPONENT_VTABLE:
574 printf ("vtable\n");
575 break;
576 case DEMANGLE_COMPONENT_VTT:
577 printf ("VTT\n");
578 break;
579 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
580 printf ("construction vtable\n");
581 break;
582 case DEMANGLE_COMPONENT_TYPEINFO:
583 printf ("typeinfo\n");
584 break;
585 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
586 printf ("typeinfo name\n");
587 break;
588 case DEMANGLE_COMPONENT_TYPEINFO_FN:
589 printf ("typeinfo function\n");
590 break;
591 case DEMANGLE_COMPONENT_THUNK:
592 printf ("thunk\n");
593 break;
594 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
595 printf ("virtual thunk\n");
596 break;
597 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
598 printf ("covariant thunk\n");
599 break;
600 case DEMANGLE_COMPONENT_JAVA_CLASS:
601 printf ("java class\n");
602 break;
603 case DEMANGLE_COMPONENT_GUARD:
604 printf ("guard\n");
605 break;
606 case DEMANGLE_COMPONENT_REFTEMP:
607 printf ("reference temporary\n");
608 break;
609 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
610 printf ("hidden alias\n");
611 break;
612 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
613 printf ("transaction clone\n");
614 break;
615 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
616 printf ("non-transaction clone\n");
617 break;
618 case DEMANGLE_COMPONENT_RESTRICT:
619 printf ("restrict\n");
620 break;
621 case DEMANGLE_COMPONENT_VOLATILE:
622 printf ("volatile\n");
623 break;
624 case DEMANGLE_COMPONENT_CONST:
625 printf ("const\n");
626 break;
627 case DEMANGLE_COMPONENT_RESTRICT_THIS:
628 printf ("restrict this\n");
629 break;
630 case DEMANGLE_COMPONENT_VOLATILE_THIS:
631 printf ("volatile this\n");
632 break;
633 case DEMANGLE_COMPONENT_CONST_THIS:
634 printf ("const this\n");
635 break;
636 case DEMANGLE_COMPONENT_REFERENCE_THIS:
637 printf ("reference this\n");
638 break;
639 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
640 printf ("rvalue reference this\n");
641 break;
642 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
643 printf ("vendor type qualifier\n");
644 break;
645 case DEMANGLE_COMPONENT_POINTER:
646 printf ("pointer\n");
647 break;
648 case DEMANGLE_COMPONENT_REFERENCE:
649 printf ("reference\n");
650 break;
651 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
652 printf ("rvalue reference\n");
653 break;
654 case DEMANGLE_COMPONENT_COMPLEX:
655 printf ("complex\n");
656 break;
657 case DEMANGLE_COMPONENT_IMAGINARY:
658 printf ("imaginary\n");
659 break;
660 case DEMANGLE_COMPONENT_VENDOR_TYPE:
661 printf ("vendor type\n");
662 break;
663 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
664 printf ("function type\n");
665 break;
666 case DEMANGLE_COMPONENT_ARRAY_TYPE:
667 printf ("array type\n");
668 break;
669 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
670 printf ("pointer to member type\n");
671 break;
672 case DEMANGLE_COMPONENT_FIXED_TYPE:
673 printf ("fixed-point type\n");
674 break;
675 case DEMANGLE_COMPONENT_ARGLIST:
676 printf ("argument list\n");
677 break;
678 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
679 printf ("template argument list\n");
680 break;
681 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
682 printf ("initializer list\n");
683 break;
684 case DEMANGLE_COMPONENT_CAST:
685 printf ("cast\n");
686 break;
687 case DEMANGLE_COMPONENT_NULLARY:
688 printf ("nullary operator\n");
689 break;
690 case DEMANGLE_COMPONENT_UNARY:
691 printf ("unary operator\n");
692 break;
693 case DEMANGLE_COMPONENT_BINARY:
694 printf ("binary operator\n");
695 break;
696 case DEMANGLE_COMPONENT_BINARY_ARGS:
697 printf ("binary operator arguments\n");
698 break;
699 case DEMANGLE_COMPONENT_TRINARY:
700 printf ("trinary operator\n");
701 break;
702 case DEMANGLE_COMPONENT_TRINARY_ARG1:
703 printf ("trinary operator arguments 1\n");
704 break;
705 case DEMANGLE_COMPONENT_TRINARY_ARG2:
706 printf ("trinary operator arguments 1\n");
707 break;
708 case DEMANGLE_COMPONENT_LITERAL:
709 printf ("literal\n");
710 break;
711 case DEMANGLE_COMPONENT_LITERAL_NEG:
712 printf ("negative literal\n");
713 break;
714 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
715 printf ("java resource\n");
716 break;
717 case DEMANGLE_COMPONENT_COMPOUND_NAME:
718 printf ("compound name\n");
719 break;
720 case DEMANGLE_COMPONENT_CHARACTER:
721 printf ("character '%c'\n", dc->u.s_character.character);
722 return;
723 case DEMANGLE_COMPONENT_DECLTYPE:
724 printf ("decltype\n");
725 break;
726 case DEMANGLE_COMPONENT_PACK_EXPANSION:
727 printf ("pack expansion\n");
728 break;
729 case DEMANGLE_COMPONENT_TLS_INIT:
730 printf ("tls init function\n");
731 break;
732 case DEMANGLE_COMPONENT_TLS_WRAPPER:
733 printf ("tls wrapper function\n");
734 break;
735 case DEMANGLE_COMPONENT_DEFAULT_ARG:
736 printf ("default argument %d\n", dc->u.s_unary_num.num);
737 d_dump (dc->u.s_unary_num.sub, indent+2);
738 return;
739 case DEMANGLE_COMPONENT_LAMBDA:
740 printf ("lambda %d\n", dc->u.s_unary_num.num);
741 d_dump (dc->u.s_unary_num.sub, indent+2);
742 return;
745 d_dump (d_left (dc), indent + 2);
746 d_dump (d_right (dc), indent + 2);
749 #endif /* CP_DEMANGLE_DEBUG */
751 /* Fill in a DEMANGLE_COMPONENT_NAME. */
753 CP_STATIC_IF_GLIBCPP_V3
755 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
757 if (p == NULL || s == NULL || len == 0)
758 return 0;
759 p->type = DEMANGLE_COMPONENT_NAME;
760 p->u.s_name.s = s;
761 p->u.s_name.len = len;
762 return 1;
765 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
767 CP_STATIC_IF_GLIBCPP_V3
769 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
770 struct demangle_component *name)
772 if (p == NULL || args < 0 || name == NULL)
773 return 0;
774 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
775 p->u.s_extended_operator.args = args;
776 p->u.s_extended_operator.name = name;
777 return 1;
780 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
782 CP_STATIC_IF_GLIBCPP_V3
784 cplus_demangle_fill_ctor (struct demangle_component *p,
785 enum gnu_v3_ctor_kinds kind,
786 struct demangle_component *name)
788 if (p == NULL
789 || name == NULL
790 || (int) kind < gnu_v3_complete_object_ctor
791 || (int) kind > gnu_v3_object_ctor_group)
792 return 0;
793 p->type = DEMANGLE_COMPONENT_CTOR;
794 p->u.s_ctor.kind = kind;
795 p->u.s_ctor.name = name;
796 return 1;
799 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
801 CP_STATIC_IF_GLIBCPP_V3
803 cplus_demangle_fill_dtor (struct demangle_component *p,
804 enum gnu_v3_dtor_kinds kind,
805 struct demangle_component *name)
807 if (p == NULL
808 || name == NULL
809 || (int) kind < gnu_v3_deleting_dtor
810 || (int) kind > gnu_v3_object_dtor_group)
811 return 0;
812 p->type = DEMANGLE_COMPONENT_DTOR;
813 p->u.s_dtor.kind = kind;
814 p->u.s_dtor.name = name;
815 return 1;
818 /* Add a new component. */
820 static struct demangle_component *
821 d_make_empty (struct d_info *di)
823 struct demangle_component *p;
825 if (di->next_comp >= di->num_comps)
826 return NULL;
827 p = &di->comps[di->next_comp];
828 ++di->next_comp;
829 return p;
832 /* Add a new generic component. */
834 static struct demangle_component *
835 d_make_comp (struct d_info *di, enum demangle_component_type type,
836 struct demangle_component *left,
837 struct demangle_component *right)
839 struct demangle_component *p;
841 /* We check for errors here. A typical error would be a NULL return
842 from a subroutine. We catch those here, and return NULL
843 upward. */
844 switch (type)
846 /* These types require two parameters. */
847 case DEMANGLE_COMPONENT_QUAL_NAME:
848 case DEMANGLE_COMPONENT_LOCAL_NAME:
849 case DEMANGLE_COMPONENT_TYPED_NAME:
850 case DEMANGLE_COMPONENT_TAGGED_NAME:
851 case DEMANGLE_COMPONENT_TEMPLATE:
852 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
853 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
854 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
855 case DEMANGLE_COMPONENT_UNARY:
856 case DEMANGLE_COMPONENT_BINARY:
857 case DEMANGLE_COMPONENT_BINARY_ARGS:
858 case DEMANGLE_COMPONENT_TRINARY:
859 case DEMANGLE_COMPONENT_TRINARY_ARG1:
860 case DEMANGLE_COMPONENT_LITERAL:
861 case DEMANGLE_COMPONENT_LITERAL_NEG:
862 case DEMANGLE_COMPONENT_COMPOUND_NAME:
863 case DEMANGLE_COMPONENT_VECTOR_TYPE:
864 case DEMANGLE_COMPONENT_CLONE:
865 if (left == NULL || right == NULL)
866 return NULL;
867 break;
869 /* These types only require one parameter. */
870 case DEMANGLE_COMPONENT_VTABLE:
871 case DEMANGLE_COMPONENT_VTT:
872 case DEMANGLE_COMPONENT_TYPEINFO:
873 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
874 case DEMANGLE_COMPONENT_TYPEINFO_FN:
875 case DEMANGLE_COMPONENT_THUNK:
876 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
877 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
878 case DEMANGLE_COMPONENT_JAVA_CLASS:
879 case DEMANGLE_COMPONENT_GUARD:
880 case DEMANGLE_COMPONENT_TLS_INIT:
881 case DEMANGLE_COMPONENT_TLS_WRAPPER:
882 case DEMANGLE_COMPONENT_REFTEMP:
883 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
884 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
885 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
886 case DEMANGLE_COMPONENT_POINTER:
887 case DEMANGLE_COMPONENT_REFERENCE:
888 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
889 case DEMANGLE_COMPONENT_COMPLEX:
890 case DEMANGLE_COMPONENT_IMAGINARY:
891 case DEMANGLE_COMPONENT_VENDOR_TYPE:
892 case DEMANGLE_COMPONENT_CAST:
893 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
894 case DEMANGLE_COMPONENT_DECLTYPE:
895 case DEMANGLE_COMPONENT_PACK_EXPANSION:
896 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
897 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
898 case DEMANGLE_COMPONENT_NULLARY:
899 case DEMANGLE_COMPONENT_TRINARY_ARG2:
900 if (left == NULL)
901 return NULL;
902 break;
904 /* This needs a right parameter, but the left parameter can be
905 empty. */
906 case DEMANGLE_COMPONENT_ARRAY_TYPE:
907 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
908 if (right == NULL)
909 return NULL;
910 break;
912 /* These are allowed to have no parameters--in some cases they
913 will be filled in later. */
914 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
915 case DEMANGLE_COMPONENT_RESTRICT:
916 case DEMANGLE_COMPONENT_VOLATILE:
917 case DEMANGLE_COMPONENT_CONST:
918 case DEMANGLE_COMPONENT_RESTRICT_THIS:
919 case DEMANGLE_COMPONENT_VOLATILE_THIS:
920 case DEMANGLE_COMPONENT_CONST_THIS:
921 case DEMANGLE_COMPONENT_REFERENCE_THIS:
922 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
923 case DEMANGLE_COMPONENT_ARGLIST:
924 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
925 break;
927 /* Other types should not be seen here. */
928 default:
929 return NULL;
932 p = d_make_empty (di);
933 if (p != NULL)
935 p->type = type;
936 p->u.s_binary.left = left;
937 p->u.s_binary.right = right;
939 return p;
942 /* Add a new demangle mangled name component. */
944 static struct demangle_component *
945 d_make_demangle_mangled_name (struct d_info *di, const char *s)
947 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
948 return d_make_name (di, s, strlen (s));
949 d_advance (di, 2);
950 return d_encoding (di, 0);
953 /* Add a new name component. */
955 static struct demangle_component *
956 d_make_name (struct d_info *di, const char *s, int len)
958 struct demangle_component *p;
960 p = d_make_empty (di);
961 if (! cplus_demangle_fill_name (p, s, len))
962 return NULL;
963 return p;
966 /* Add a new builtin type component. */
968 static struct demangle_component *
969 d_make_builtin_type (struct d_info *di,
970 const struct demangle_builtin_type_info *type)
972 struct demangle_component *p;
974 if (type == NULL)
975 return NULL;
976 p = d_make_empty (di);
977 if (p != NULL)
979 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
980 p->u.s_builtin.type = type;
982 return p;
985 /* Add a new operator component. */
987 static struct demangle_component *
988 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
990 struct demangle_component *p;
992 p = d_make_empty (di);
993 if (p != NULL)
995 p->type = DEMANGLE_COMPONENT_OPERATOR;
996 p->u.s_operator.op = op;
998 return p;
1001 /* Add a new extended operator component. */
1003 static struct demangle_component *
1004 d_make_extended_operator (struct d_info *di, int args,
1005 struct demangle_component *name)
1007 struct demangle_component *p;
1009 p = d_make_empty (di);
1010 if (! cplus_demangle_fill_extended_operator (p, args, name))
1011 return NULL;
1012 return p;
1015 static struct demangle_component *
1016 d_make_default_arg (struct d_info *di, int num,
1017 struct demangle_component *sub)
1019 struct demangle_component *p = d_make_empty (di);
1020 if (p)
1022 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1023 p->u.s_unary_num.num = num;
1024 p->u.s_unary_num.sub = sub;
1026 return p;
1029 /* Add a new constructor component. */
1031 static struct demangle_component *
1032 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1033 struct demangle_component *name)
1035 struct demangle_component *p;
1037 p = d_make_empty (di);
1038 if (! cplus_demangle_fill_ctor (p, kind, name))
1039 return NULL;
1040 return p;
1043 /* Add a new destructor component. */
1045 static struct demangle_component *
1046 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1047 struct demangle_component *name)
1049 struct demangle_component *p;
1051 p = d_make_empty (di);
1052 if (! cplus_demangle_fill_dtor (p, kind, name))
1053 return NULL;
1054 return p;
1057 /* Add a new template parameter. */
1059 static struct demangle_component *
1060 d_make_template_param (struct d_info *di, long i)
1062 struct demangle_component *p;
1064 p = d_make_empty (di);
1065 if (p != NULL)
1067 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1068 p->u.s_number.number = i;
1070 return p;
1073 /* Add a new function parameter. */
1075 static struct demangle_component *
1076 d_make_function_param (struct d_info *di, long i)
1078 struct demangle_component *p;
1080 p = d_make_empty (di);
1081 if (p != NULL)
1083 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1084 p->u.s_number.number = i;
1086 return p;
1089 /* Add a new standard substitution component. */
1091 static struct demangle_component *
1092 d_make_sub (struct d_info *di, const char *name, int len)
1094 struct demangle_component *p;
1096 p = d_make_empty (di);
1097 if (p != NULL)
1099 p->type = DEMANGLE_COMPONENT_SUB_STD;
1100 p->u.s_string.string = name;
1101 p->u.s_string.len = len;
1103 return p;
1106 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1108 TOP_LEVEL is non-zero when called at the top level. */
1110 CP_STATIC_IF_GLIBCPP_V3
1111 struct demangle_component *
1112 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1114 struct demangle_component *p;
1116 if (! d_check_char (di, '_')
1117 /* Allow missing _ if not at toplevel to work around a
1118 bug in G++ abi-version=2 mangling; see the comment in
1119 write_template_arg. */
1120 && top_level)
1121 return NULL;
1122 if (! d_check_char (di, 'Z'))
1123 return NULL;
1124 p = d_encoding (di, top_level);
1126 /* If at top level and parsing parameters, check for a clone
1127 suffix. */
1128 if (top_level && (di->options & DMGL_PARAMS) != 0)
1129 while (d_peek_char (di) == '.'
1130 && (IS_LOWER (d_peek_next_char (di))
1131 || d_peek_next_char (di) == '_'
1132 || IS_DIGIT (d_peek_next_char (di))))
1133 p = d_clone_suffix (di, p);
1135 return p;
1138 /* Return whether a function should have a return type. The argument
1139 is the function name, which may be qualified in various ways. The
1140 rules are that template functions have return types with some
1141 exceptions, function types which are not part of a function name
1142 mangling have return types with some exceptions, and non-template
1143 function names do not have return types. The exceptions are that
1144 constructors, destructors, and conversion operators do not have
1145 return types. */
1147 static int
1148 has_return_type (struct demangle_component *dc)
1150 if (dc == NULL)
1151 return 0;
1152 switch (dc->type)
1154 default:
1155 return 0;
1156 case DEMANGLE_COMPONENT_TEMPLATE:
1157 return ! is_ctor_dtor_or_conversion (d_left (dc));
1158 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1159 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1160 case DEMANGLE_COMPONENT_CONST_THIS:
1161 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1162 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1163 return has_return_type (d_left (dc));
1167 /* Return whether a name is a constructor, a destructor, or a
1168 conversion operator. */
1170 static int
1171 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1173 if (dc == NULL)
1174 return 0;
1175 switch (dc->type)
1177 default:
1178 return 0;
1179 case DEMANGLE_COMPONENT_QUAL_NAME:
1180 case DEMANGLE_COMPONENT_LOCAL_NAME:
1181 return is_ctor_dtor_or_conversion (d_right (dc));
1182 case DEMANGLE_COMPONENT_CTOR:
1183 case DEMANGLE_COMPONENT_DTOR:
1184 case DEMANGLE_COMPONENT_CAST:
1185 return 1;
1189 /* <encoding> ::= <(function) name> <bare-function-type>
1190 ::= <(data) name>
1191 ::= <special-name>
1193 TOP_LEVEL is non-zero when called at the top level, in which case
1194 if DMGL_PARAMS is not set we do not demangle the function
1195 parameters. We only set this at the top level, because otherwise
1196 we would not correctly demangle names in local scopes. */
1198 static struct demangle_component *
1199 d_encoding (struct d_info *di, int top_level)
1201 char peek = d_peek_char (di);
1203 if (peek == 'G' || peek == 'T')
1204 return d_special_name (di);
1205 else
1207 struct demangle_component *dc;
1209 dc = d_name (di);
1211 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1213 /* Strip off any initial CV-qualifiers, as they really apply
1214 to the `this' parameter, and they were not output by the
1215 v2 demangler without DMGL_PARAMS. */
1216 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1217 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1218 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1219 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1220 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1221 dc = d_left (dc);
1223 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1224 there may be CV-qualifiers on its right argument which
1225 really apply here; this happens when parsing a class
1226 which is local to a function. */
1227 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1229 struct demangle_component *dcr;
1231 dcr = d_right (dc);
1232 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1233 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1234 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1235 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1236 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1237 dcr = d_left (dcr);
1238 dc->u.s_binary.right = dcr;
1241 return dc;
1244 peek = d_peek_char (di);
1245 if (dc == NULL || peek == '\0' || peek == 'E')
1246 return dc;
1247 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1248 d_bare_function_type (di, has_return_type (dc)));
1252 /* <tagged-name> ::= <name> B <source-name> */
1254 static struct demangle_component *
1255 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1257 char peek;
1258 while (peek = d_peek_char (di),
1259 peek == 'B')
1261 struct demangle_component *tag;
1262 d_advance (di, 1);
1263 tag = d_source_name (di);
1264 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1266 return dc;
1269 /* <name> ::= <nested-name>
1270 ::= <unscoped-name>
1271 ::= <unscoped-template-name> <template-args>
1272 ::= <local-name>
1274 <unscoped-name> ::= <unqualified-name>
1275 ::= St <unqualified-name>
1277 <unscoped-template-name> ::= <unscoped-name>
1278 ::= <substitution>
1281 static struct demangle_component *
1282 d_name (struct d_info *di)
1284 char peek = d_peek_char (di);
1285 struct demangle_component *dc;
1287 switch (peek)
1289 case 'N':
1290 return d_nested_name (di);
1292 case 'Z':
1293 return d_local_name (di);
1295 case 'U':
1296 return d_unqualified_name (di);
1298 case 'S':
1300 int subst;
1302 if (d_peek_next_char (di) != 't')
1304 dc = d_substitution (di, 0);
1305 subst = 1;
1307 else
1309 d_advance (di, 2);
1310 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1311 d_make_name (di, "std", 3),
1312 d_unqualified_name (di));
1313 di->expansion += 3;
1314 subst = 0;
1317 if (d_peek_char (di) != 'I')
1319 /* The grammar does not permit this case to occur if we
1320 called d_substitution() above (i.e., subst == 1). We
1321 don't bother to check. */
1323 else
1325 /* This is <template-args>, which means that we just saw
1326 <unscoped-template-name>, which is a substitution
1327 candidate if we didn't just get it from a
1328 substitution. */
1329 if (! subst)
1331 if (! d_add_substitution (di, dc))
1332 return NULL;
1334 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1335 d_template_args (di));
1338 return dc;
1341 case 'L':
1342 default:
1343 dc = d_unqualified_name (di);
1344 if (d_peek_char (di) == 'I')
1346 /* This is <template-args>, which means that we just saw
1347 <unscoped-template-name>, which is a substitution
1348 candidate. */
1349 if (! d_add_substitution (di, dc))
1350 return NULL;
1351 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1352 d_template_args (di));
1354 return dc;
1358 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1359 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1362 static struct demangle_component *
1363 d_nested_name (struct d_info *di)
1365 struct demangle_component *ret;
1366 struct demangle_component **pret;
1367 struct demangle_component *rqual;
1369 if (! d_check_char (di, 'N'))
1370 return NULL;
1372 pret = d_cv_qualifiers (di, &ret, 1);
1373 if (pret == NULL)
1374 return NULL;
1376 /* Parse the ref-qualifier now and then attach it
1377 once we have something to attach it to. */
1378 rqual = d_ref_qualifier (di, NULL);
1380 *pret = d_prefix (di);
1381 if (*pret == NULL)
1382 return NULL;
1384 if (rqual)
1386 d_left (rqual) = ret;
1387 ret = rqual;
1390 if (! d_check_char (di, 'E'))
1391 return NULL;
1393 return ret;
1396 /* <prefix> ::= <prefix> <unqualified-name>
1397 ::= <template-prefix> <template-args>
1398 ::= <template-param>
1399 ::= <decltype>
1401 ::= <substitution>
1403 <template-prefix> ::= <prefix> <(template) unqualified-name>
1404 ::= <template-param>
1405 ::= <substitution>
1408 static struct demangle_component *
1409 d_prefix (struct d_info *di)
1411 struct demangle_component *ret = NULL;
1413 while (1)
1415 char peek;
1416 enum demangle_component_type comb_type;
1417 struct demangle_component *dc;
1419 peek = d_peek_char (di);
1420 if (peek == '\0')
1421 return NULL;
1423 /* The older code accepts a <local-name> here, but I don't see
1424 that in the grammar. The older code does not accept a
1425 <template-param> here. */
1427 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1428 if (peek == 'D')
1430 char peek2 = d_peek_next_char (di);
1431 if (peek2 == 'T' || peek2 == 't')
1432 /* Decltype. */
1433 dc = cplus_demangle_type (di);
1434 else
1435 /* Destructor name. */
1436 dc = d_unqualified_name (di);
1438 else if (IS_DIGIT (peek)
1439 || IS_LOWER (peek)
1440 || peek == 'C'
1441 || peek == 'U'
1442 || peek == 'L')
1443 dc = d_unqualified_name (di);
1444 else if (peek == 'S')
1445 dc = d_substitution (di, 1);
1446 else if (peek == 'I')
1448 if (ret == NULL)
1449 return NULL;
1450 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1451 dc = d_template_args (di);
1453 else if (peek == 'T')
1454 dc = d_template_param (di);
1455 else if (peek == 'E')
1456 return ret;
1457 else if (peek == 'M')
1459 /* Initializer scope for a lambda. We don't need to represent
1460 this; the normal code will just treat the variable as a type
1461 scope, which gives appropriate output. */
1462 if (ret == NULL)
1463 return NULL;
1464 d_advance (di, 1);
1465 continue;
1467 else
1468 return NULL;
1470 if (ret == NULL)
1471 ret = dc;
1472 else
1473 ret = d_make_comp (di, comb_type, ret, dc);
1475 if (peek != 'S' && d_peek_char (di) != 'E')
1477 if (! d_add_substitution (di, ret))
1478 return NULL;
1483 /* <unqualified-name> ::= <operator-name>
1484 ::= <ctor-dtor-name>
1485 ::= <source-name>
1486 ::= <local-source-name>
1488 <local-source-name> ::= L <source-name> <discriminator>
1491 static struct demangle_component *
1492 d_unqualified_name (struct d_info *di)
1494 struct demangle_component *ret;
1495 char peek;
1497 peek = d_peek_char (di);
1498 if (IS_DIGIT (peek))
1499 ret = d_source_name (di);
1500 else if (IS_LOWER (peek))
1502 ret = d_operator_name (di);
1503 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1505 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1506 if (!strcmp (ret->u.s_operator.op->code, "li"))
1507 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1508 d_source_name (di));
1511 else if (peek == 'C' || peek == 'D')
1512 ret = d_ctor_dtor_name (di);
1513 else if (peek == 'L')
1515 d_advance (di, 1);
1517 ret = d_source_name (di);
1518 if (ret == NULL)
1519 return NULL;
1520 if (! d_discriminator (di))
1521 return NULL;
1523 else if (peek == 'U')
1525 switch (d_peek_next_char (di))
1527 case 'l':
1528 ret = d_lambda (di);
1529 break;
1530 case 't':
1531 ret = d_unnamed_type (di);
1532 break;
1533 default:
1534 return NULL;
1537 else
1538 return NULL;
1540 if (d_peek_char (di) == 'B')
1541 ret = d_abi_tags (di, ret);
1542 return ret;
1545 /* <source-name> ::= <(positive length) number> <identifier> */
1547 static struct demangle_component *
1548 d_source_name (struct d_info *di)
1550 long len;
1551 struct demangle_component *ret;
1553 len = d_number (di);
1554 if (len <= 0)
1555 return NULL;
1556 ret = d_identifier (di, len);
1557 di->last_name = ret;
1558 return ret;
1561 /* number ::= [n] <(non-negative decimal integer)> */
1563 static long
1564 d_number (struct d_info *di)
1566 int negative;
1567 char peek;
1568 long ret;
1570 negative = 0;
1571 peek = d_peek_char (di);
1572 if (peek == 'n')
1574 negative = 1;
1575 d_advance (di, 1);
1576 peek = d_peek_char (di);
1579 ret = 0;
1580 while (1)
1582 if (! IS_DIGIT (peek))
1584 if (negative)
1585 ret = - ret;
1586 return ret;
1588 ret = ret * 10 + peek - '0';
1589 d_advance (di, 1);
1590 peek = d_peek_char (di);
1594 /* Like d_number, but returns a demangle_component. */
1596 static struct demangle_component *
1597 d_number_component (struct d_info *di)
1599 struct demangle_component *ret = d_make_empty (di);
1600 if (ret)
1602 ret->type = DEMANGLE_COMPONENT_NUMBER;
1603 ret->u.s_number.number = d_number (di);
1605 return ret;
1608 /* identifier ::= <(unqualified source code identifier)> */
1610 static struct demangle_component *
1611 d_identifier (struct d_info *di, int len)
1613 const char *name;
1615 name = d_str (di);
1617 if (di->send - name < len)
1618 return NULL;
1620 d_advance (di, len);
1622 /* A Java mangled name may have a trailing '$' if it is a C++
1623 keyword. This '$' is not included in the length count. We just
1624 ignore the '$'. */
1625 if ((di->options & DMGL_JAVA) != 0
1626 && d_peek_char (di) == '$')
1627 d_advance (di, 1);
1629 /* Look for something which looks like a gcc encoding of an
1630 anonymous namespace, and replace it with a more user friendly
1631 name. */
1632 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1633 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1634 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1636 const char *s;
1638 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1639 if ((*s == '.' || *s == '_' || *s == '$')
1640 && s[1] == 'N')
1642 di->expansion -= len - sizeof "(anonymous namespace)";
1643 return d_make_name (di, "(anonymous namespace)",
1644 sizeof "(anonymous namespace)" - 1);
1648 return d_make_name (di, name, len);
1651 /* operator_name ::= many different two character encodings.
1652 ::= cv <type>
1653 ::= v <digit> <source-name>
1655 This list is sorted for binary search. */
1657 #define NL(s) s, (sizeof s) - 1
1659 CP_STATIC_IF_GLIBCPP_V3
1660 const struct demangle_operator_info cplus_demangle_operators[] =
1662 { "aN", NL ("&="), 2 },
1663 { "aS", NL ("="), 2 },
1664 { "aa", NL ("&&"), 2 },
1665 { "ad", NL ("&"), 1 },
1666 { "an", NL ("&"), 2 },
1667 { "at", NL ("alignof "), 1 },
1668 { "az", NL ("alignof "), 1 },
1669 { "cc", NL ("const_cast"), 2 },
1670 { "cl", NL ("()"), 2 },
1671 { "cm", NL (","), 2 },
1672 { "co", NL ("~"), 1 },
1673 { "dV", NL ("/="), 2 },
1674 { "da", NL ("delete[] "), 1 },
1675 { "dc", NL ("dynamic_cast"), 2 },
1676 { "de", NL ("*"), 1 },
1677 { "dl", NL ("delete "), 1 },
1678 { "ds", NL (".*"), 2 },
1679 { "dt", NL ("."), 2 },
1680 { "dv", NL ("/"), 2 },
1681 { "eO", NL ("^="), 2 },
1682 { "eo", NL ("^"), 2 },
1683 { "eq", NL ("=="), 2 },
1684 { "ge", NL (">="), 2 },
1685 { "gs", NL ("::"), 1 },
1686 { "gt", NL (">"), 2 },
1687 { "ix", NL ("[]"), 2 },
1688 { "lS", NL ("<<="), 2 },
1689 { "le", NL ("<="), 2 },
1690 { "li", NL ("operator\"\" "), 1 },
1691 { "ls", NL ("<<"), 2 },
1692 { "lt", NL ("<"), 2 },
1693 { "mI", NL ("-="), 2 },
1694 { "mL", NL ("*="), 2 },
1695 { "mi", NL ("-"), 2 },
1696 { "ml", NL ("*"), 2 },
1697 { "mm", NL ("--"), 1 },
1698 { "na", NL ("new[]"), 3 },
1699 { "ne", NL ("!="), 2 },
1700 { "ng", NL ("-"), 1 },
1701 { "nt", NL ("!"), 1 },
1702 { "nw", NL ("new"), 3 },
1703 { "oR", NL ("|="), 2 },
1704 { "oo", NL ("||"), 2 },
1705 { "or", NL ("|"), 2 },
1706 { "pL", NL ("+="), 2 },
1707 { "pl", NL ("+"), 2 },
1708 { "pm", NL ("->*"), 2 },
1709 { "pp", NL ("++"), 1 },
1710 { "ps", NL ("+"), 1 },
1711 { "pt", NL ("->"), 2 },
1712 { "qu", NL ("?"), 3 },
1713 { "rM", NL ("%="), 2 },
1714 { "rS", NL (">>="), 2 },
1715 { "rc", NL ("reinterpret_cast"), 2 },
1716 { "rm", NL ("%"), 2 },
1717 { "rs", NL (">>"), 2 },
1718 { "sc", NL ("static_cast"), 2 },
1719 { "st", NL ("sizeof "), 1 },
1720 { "sz", NL ("sizeof "), 1 },
1721 { "tr", NL ("throw"), 0 },
1722 { "tw", NL ("throw "), 1 },
1723 { NULL, NULL, 0, 0 }
1726 static struct demangle_component *
1727 d_operator_name (struct d_info *di)
1729 char c1;
1730 char c2;
1732 c1 = d_next_char (di);
1733 c2 = d_next_char (di);
1734 if (c1 == 'v' && IS_DIGIT (c2))
1735 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1736 else if (c1 == 'c' && c2 == 'v')
1737 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1738 cplus_demangle_type (di), NULL);
1739 else
1741 /* LOW is the inclusive lower bound. */
1742 int low = 0;
1743 /* HIGH is the exclusive upper bound. We subtract one to ignore
1744 the sentinel at the end of the array. */
1745 int high = ((sizeof (cplus_demangle_operators)
1746 / sizeof (cplus_demangle_operators[0]))
1747 - 1);
1749 while (1)
1751 int i;
1752 const struct demangle_operator_info *p;
1754 i = low + (high - low) / 2;
1755 p = cplus_demangle_operators + i;
1757 if (c1 == p->code[0] && c2 == p->code[1])
1758 return d_make_operator (di, p);
1760 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1761 high = i;
1762 else
1763 low = i + 1;
1764 if (low == high)
1765 return NULL;
1770 static struct demangle_component *
1771 d_make_character (struct d_info *di, int c)
1773 struct demangle_component *p;
1774 p = d_make_empty (di);
1775 if (p != NULL)
1777 p->type = DEMANGLE_COMPONENT_CHARACTER;
1778 p->u.s_character.character = c;
1780 return p;
1783 static struct demangle_component *
1784 d_java_resource (struct d_info *di)
1786 struct demangle_component *p = NULL;
1787 struct demangle_component *next = NULL;
1788 long len, i;
1789 char c;
1790 const char *str;
1792 len = d_number (di);
1793 if (len <= 1)
1794 return NULL;
1796 /* Eat the leading '_'. */
1797 if (d_next_char (di) != '_')
1798 return NULL;
1799 len--;
1801 str = d_str (di);
1802 i = 0;
1804 while (len > 0)
1806 c = str[i];
1807 if (!c)
1808 return NULL;
1810 /* Each chunk is either a '$' escape... */
1811 if (c == '$')
1813 i++;
1814 switch (str[i++])
1816 case 'S':
1817 c = '/';
1818 break;
1819 case '_':
1820 c = '.';
1821 break;
1822 case '$':
1823 c = '$';
1824 break;
1825 default:
1826 return NULL;
1828 next = d_make_character (di, c);
1829 d_advance (di, i);
1830 str = d_str (di);
1831 len -= i;
1832 i = 0;
1833 if (next == NULL)
1834 return NULL;
1836 /* ... or a sequence of characters. */
1837 else
1839 while (i < len && str[i] && str[i] != '$')
1840 i++;
1842 next = d_make_name (di, str, i);
1843 d_advance (di, i);
1844 str = d_str (di);
1845 len -= i;
1846 i = 0;
1847 if (next == NULL)
1848 return NULL;
1851 if (p == NULL)
1852 p = next;
1853 else
1855 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1856 if (p == NULL)
1857 return NULL;
1861 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1863 return p;
1866 /* <special-name> ::= TV <type>
1867 ::= TT <type>
1868 ::= TI <type>
1869 ::= TS <type>
1870 ::= GV <(object) name>
1871 ::= T <call-offset> <(base) encoding>
1872 ::= Tc <call-offset> <call-offset> <(base) encoding>
1873 Also g++ extensions:
1874 ::= TC <type> <(offset) number> _ <(base) type>
1875 ::= TF <type>
1876 ::= TJ <type>
1877 ::= GR <name>
1878 ::= GA <encoding>
1879 ::= Gr <resource name>
1880 ::= GTt <encoding>
1881 ::= GTn <encoding>
1884 static struct demangle_component *
1885 d_special_name (struct d_info *di)
1887 di->expansion += 20;
1888 if (d_check_char (di, 'T'))
1890 switch (d_next_char (di))
1892 case 'V':
1893 di->expansion -= 5;
1894 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1895 cplus_demangle_type (di), NULL);
1896 case 'T':
1897 di->expansion -= 10;
1898 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1899 cplus_demangle_type (di), NULL);
1900 case 'I':
1901 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1902 cplus_demangle_type (di), NULL);
1903 case 'S':
1904 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1905 cplus_demangle_type (di), NULL);
1907 case 'h':
1908 if (! d_call_offset (di, 'h'))
1909 return NULL;
1910 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1911 d_encoding (di, 0), NULL);
1913 case 'v':
1914 if (! d_call_offset (di, 'v'))
1915 return NULL;
1916 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1917 d_encoding (di, 0), NULL);
1919 case 'c':
1920 if (! d_call_offset (di, '\0'))
1921 return NULL;
1922 if (! d_call_offset (di, '\0'))
1923 return NULL;
1924 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1925 d_encoding (di, 0), NULL);
1927 case 'C':
1929 struct demangle_component *derived_type;
1930 long offset;
1931 struct demangle_component *base_type;
1933 derived_type = cplus_demangle_type (di);
1934 offset = d_number (di);
1935 if (offset < 0)
1936 return NULL;
1937 if (! d_check_char (di, '_'))
1938 return NULL;
1939 base_type = cplus_demangle_type (di);
1940 /* We don't display the offset. FIXME: We should display
1941 it in verbose mode. */
1942 di->expansion += 5;
1943 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1944 base_type, derived_type);
1947 case 'F':
1948 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1949 cplus_demangle_type (di), NULL);
1950 case 'J':
1951 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1952 cplus_demangle_type (di), NULL);
1954 case 'H':
1955 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
1956 d_name (di), NULL);
1958 case 'W':
1959 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
1960 d_name (di), NULL);
1962 default:
1963 return NULL;
1966 else if (d_check_char (di, 'G'))
1968 switch (d_next_char (di))
1970 case 'V':
1971 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1973 case 'R':
1975 struct demangle_component *name = d_name (di);
1976 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
1977 d_number_component (di));
1980 case 'A':
1981 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1982 d_encoding (di, 0), NULL);
1984 case 'T':
1985 switch (d_next_char (di))
1987 case 'n':
1988 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
1989 d_encoding (di, 0), NULL);
1990 default:
1991 /* ??? The proposal is that other letters (such as 'h') stand
1992 for different variants of transaction cloning, such as
1993 compiling directly for hardware transaction support. But
1994 they still should all be transactional clones of some sort
1995 so go ahead and call them that. */
1996 case 't':
1997 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
1998 d_encoding (di, 0), NULL);
2001 case 'r':
2002 return d_java_resource (di);
2004 default:
2005 return NULL;
2008 else
2009 return NULL;
2012 /* <call-offset> ::= h <nv-offset> _
2013 ::= v <v-offset> _
2015 <nv-offset> ::= <(offset) number>
2017 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2019 The C parameter, if not '\0', is a character we just read which is
2020 the start of the <call-offset>.
2022 We don't display the offset information anywhere. FIXME: We should
2023 display it in verbose mode. */
2025 static int
2026 d_call_offset (struct d_info *di, int c)
2028 if (c == '\0')
2029 c = d_next_char (di);
2031 if (c == 'h')
2032 d_number (di);
2033 else if (c == 'v')
2035 d_number (di);
2036 if (! d_check_char (di, '_'))
2037 return 0;
2038 d_number (di);
2040 else
2041 return 0;
2043 if (! d_check_char (di, '_'))
2044 return 0;
2046 return 1;
2049 /* <ctor-dtor-name> ::= C1
2050 ::= C2
2051 ::= C3
2052 ::= D0
2053 ::= D1
2054 ::= D2
2057 static struct demangle_component *
2058 d_ctor_dtor_name (struct d_info *di)
2060 if (di->last_name != NULL)
2062 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2063 di->expansion += di->last_name->u.s_name.len;
2064 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2065 di->expansion += di->last_name->u.s_string.len;
2067 switch (d_peek_char (di))
2069 case 'C':
2071 enum gnu_v3_ctor_kinds kind;
2073 switch (d_peek_next_char (di))
2075 case '1':
2076 kind = gnu_v3_complete_object_ctor;
2077 break;
2078 case '2':
2079 kind = gnu_v3_base_object_ctor;
2080 break;
2081 case '3':
2082 kind = gnu_v3_complete_object_allocating_ctor;
2083 break;
2084 case '5':
2085 kind = gnu_v3_object_ctor_group;
2086 break;
2087 default:
2088 return NULL;
2090 d_advance (di, 2);
2091 return d_make_ctor (di, kind, di->last_name);
2094 case 'D':
2096 enum gnu_v3_dtor_kinds kind;
2098 switch (d_peek_next_char (di))
2100 case '0':
2101 kind = gnu_v3_deleting_dtor;
2102 break;
2103 case '1':
2104 kind = gnu_v3_complete_object_dtor;
2105 break;
2106 case '2':
2107 kind = gnu_v3_base_object_dtor;
2108 break;
2109 case '5':
2110 kind = gnu_v3_object_dtor_group;
2111 break;
2112 default:
2113 return NULL;
2115 d_advance (di, 2);
2116 return d_make_dtor (di, kind, di->last_name);
2119 default:
2120 return NULL;
2124 /* <type> ::= <builtin-type>
2125 ::= <function-type>
2126 ::= <class-enum-type>
2127 ::= <array-type>
2128 ::= <pointer-to-member-type>
2129 ::= <template-param>
2130 ::= <template-template-param> <template-args>
2131 ::= <substitution>
2132 ::= <CV-qualifiers> <type>
2133 ::= P <type>
2134 ::= R <type>
2135 ::= O <type> (C++0x)
2136 ::= C <type>
2137 ::= G <type>
2138 ::= U <source-name> <type>
2140 <builtin-type> ::= various one letter codes
2141 ::= u <source-name>
2144 CP_STATIC_IF_GLIBCPP_V3
2145 const struct demangle_builtin_type_info
2146 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2148 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2149 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2150 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2151 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2152 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2153 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2154 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2155 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2156 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2157 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2158 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2159 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2160 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2161 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2162 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2163 D_PRINT_DEFAULT },
2164 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2165 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2166 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2167 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2168 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2169 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2170 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2171 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2172 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2173 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2174 D_PRINT_UNSIGNED_LONG_LONG },
2175 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2176 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2177 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2178 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2179 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2180 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2181 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2182 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2183 D_PRINT_DEFAULT },
2186 CP_STATIC_IF_GLIBCPP_V3
2187 struct demangle_component *
2188 cplus_demangle_type (struct d_info *di)
2190 char peek;
2191 struct demangle_component *ret;
2192 int can_subst;
2194 /* The ABI specifies that when CV-qualifiers are used, the base type
2195 is substitutable, and the fully qualified type is substitutable,
2196 but the base type with a strict subset of the CV-qualifiers is
2197 not substitutable. The natural recursive implementation of the
2198 CV-qualifiers would cause subsets to be substitutable, so instead
2199 we pull them all off now.
2201 FIXME: The ABI says that order-insensitive vendor qualifiers
2202 should be handled in the same way, but we have no way to tell
2203 which vendor qualifiers are order-insensitive and which are
2204 order-sensitive. So we just assume that they are all
2205 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2206 __vector, and it treats it as order-sensitive when mangling
2207 names. */
2209 peek = d_peek_char (di);
2210 if (peek == 'r' || peek == 'V' || peek == 'K')
2212 struct demangle_component **pret;
2214 pret = d_cv_qualifiers (di, &ret, 0);
2215 if (pret == NULL)
2216 return NULL;
2217 if (d_peek_char (di) == 'F')
2219 /* cv-qualifiers before a function type apply to 'this',
2220 so avoid adding the unqualified function type to
2221 the substitution list. */
2222 *pret = d_function_type (di);
2224 else
2225 *pret = cplus_demangle_type (di);
2226 if (!*pret)
2227 return NULL;
2228 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2229 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2231 /* Move the ref-qualifier outside the cv-qualifiers so that
2232 they are printed in the right order. */
2233 struct demangle_component *fn = d_left (*pret);
2234 d_left (*pret) = ret;
2235 ret = *pret;
2236 *pret = fn;
2238 if (! d_add_substitution (di, ret))
2239 return NULL;
2240 return ret;
2243 can_subst = 1;
2245 switch (peek)
2247 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2248 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2249 case 'o': case 's': case 't':
2250 case 'v': case 'w': case 'x': case 'y': case 'z':
2251 ret = d_make_builtin_type (di,
2252 &cplus_demangle_builtin_types[peek - 'a']);
2253 di->expansion += ret->u.s_builtin.type->len;
2254 can_subst = 0;
2255 d_advance (di, 1);
2256 break;
2258 case 'u':
2259 d_advance (di, 1);
2260 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2261 d_source_name (di), NULL);
2262 break;
2264 case 'F':
2265 ret = d_function_type (di);
2266 break;
2268 case '0': case '1': case '2': case '3': case '4':
2269 case '5': case '6': case '7': case '8': case '9':
2270 case 'N':
2271 case 'Z':
2272 ret = d_class_enum_type (di);
2273 break;
2275 case 'A':
2276 ret = d_array_type (di);
2277 break;
2279 case 'M':
2280 ret = d_pointer_to_member_type (di);
2281 break;
2283 case 'T':
2284 ret = d_template_param (di);
2285 if (d_peek_char (di) == 'I')
2287 /* This is <template-template-param> <template-args>. The
2288 <template-template-param> part is a substitution
2289 candidate. */
2290 if (! d_add_substitution (di, ret))
2291 return NULL;
2292 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2293 d_template_args (di));
2295 break;
2297 case 'S':
2298 /* If this is a special substitution, then it is the start of
2299 <class-enum-type>. */
2301 char peek_next;
2303 peek_next = d_peek_next_char (di);
2304 if (IS_DIGIT (peek_next)
2305 || peek_next == '_'
2306 || IS_UPPER (peek_next))
2308 ret = d_substitution (di, 0);
2309 /* The substituted name may have been a template name and
2310 may be followed by tepmlate args. */
2311 if (d_peek_char (di) == 'I')
2312 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2313 d_template_args (di));
2314 else
2315 can_subst = 0;
2317 else
2319 ret = d_class_enum_type (di);
2320 /* If the substitution was a complete type, then it is not
2321 a new substitution candidate. However, if the
2322 substitution was followed by template arguments, then
2323 the whole thing is a substitution candidate. */
2324 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2325 can_subst = 0;
2328 break;
2330 case 'O':
2331 d_advance (di, 1);
2332 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2333 cplus_demangle_type (di), NULL);
2334 break;
2336 case 'P':
2337 d_advance (di, 1);
2338 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2339 cplus_demangle_type (di), NULL);
2340 break;
2342 case 'R':
2343 d_advance (di, 1);
2344 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2345 cplus_demangle_type (di), NULL);
2346 break;
2348 case 'C':
2349 d_advance (di, 1);
2350 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2351 cplus_demangle_type (di), NULL);
2352 break;
2354 case 'G':
2355 d_advance (di, 1);
2356 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2357 cplus_demangle_type (di), NULL);
2358 break;
2360 case 'U':
2361 d_advance (di, 1);
2362 ret = d_source_name (di);
2363 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2364 cplus_demangle_type (di), ret);
2365 break;
2367 case 'D':
2368 can_subst = 0;
2369 d_advance (di, 1);
2370 peek = d_next_char (di);
2371 switch (peek)
2373 case 'T':
2374 case 't':
2375 /* decltype (expression) */
2376 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2377 d_expression (di), NULL);
2378 if (ret && d_next_char (di) != 'E')
2379 ret = NULL;
2380 can_subst = 1;
2381 break;
2383 case 'p':
2384 /* Pack expansion. */
2385 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2386 cplus_demangle_type (di), NULL);
2387 can_subst = 1;
2388 break;
2390 case 'a':
2391 /* auto */
2392 ret = d_make_name (di, "auto", 4);
2393 break;
2395 case 'f':
2396 /* 32-bit decimal floating point */
2397 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2398 di->expansion += ret->u.s_builtin.type->len;
2399 break;
2400 case 'd':
2401 /* 64-bit DFP */
2402 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2403 di->expansion += ret->u.s_builtin.type->len;
2404 break;
2405 case 'e':
2406 /* 128-bit DFP */
2407 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2408 di->expansion += ret->u.s_builtin.type->len;
2409 break;
2410 case 'h':
2411 /* 16-bit half-precision FP */
2412 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2413 di->expansion += ret->u.s_builtin.type->len;
2414 break;
2415 case 's':
2416 /* char16_t */
2417 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2418 di->expansion += ret->u.s_builtin.type->len;
2419 break;
2420 case 'i':
2421 /* char32_t */
2422 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2423 di->expansion += ret->u.s_builtin.type->len;
2424 break;
2426 case 'F':
2427 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2428 ret = d_make_empty (di);
2429 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2430 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2431 /* For demangling we don't care about the bits. */
2432 d_number (di);
2433 ret->u.s_fixed.length = cplus_demangle_type (di);
2434 if (ret->u.s_fixed.length == NULL)
2435 return NULL;
2436 d_number (di);
2437 peek = d_next_char (di);
2438 ret->u.s_fixed.sat = (peek == 's');
2439 break;
2441 case 'v':
2442 ret = d_vector_type (di);
2443 can_subst = 1;
2444 break;
2446 case 'n':
2447 /* decltype(nullptr) */
2448 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2449 di->expansion += ret->u.s_builtin.type->len;
2450 break;
2452 default:
2453 return NULL;
2455 break;
2457 default:
2458 return NULL;
2461 if (can_subst)
2463 if (! d_add_substitution (di, ret))
2464 return NULL;
2467 return ret;
2470 /* <CV-qualifiers> ::= [r] [V] [K] */
2472 static struct demangle_component **
2473 d_cv_qualifiers (struct d_info *di,
2474 struct demangle_component **pret, int member_fn)
2476 struct demangle_component **pstart;
2477 char peek;
2479 pstart = pret;
2480 peek = d_peek_char (di);
2481 while (peek == 'r' || peek == 'V' || peek == 'K')
2483 enum demangle_component_type t;
2485 d_advance (di, 1);
2486 if (peek == 'r')
2488 t = (member_fn
2489 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2490 : DEMANGLE_COMPONENT_RESTRICT);
2491 di->expansion += sizeof "restrict";
2493 else if (peek == 'V')
2495 t = (member_fn
2496 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2497 : DEMANGLE_COMPONENT_VOLATILE);
2498 di->expansion += sizeof "volatile";
2500 else
2502 t = (member_fn
2503 ? DEMANGLE_COMPONENT_CONST_THIS
2504 : DEMANGLE_COMPONENT_CONST);
2505 di->expansion += sizeof "const";
2508 *pret = d_make_comp (di, t, NULL, NULL);
2509 if (*pret == NULL)
2510 return NULL;
2511 pret = &d_left (*pret);
2513 peek = d_peek_char (di);
2516 if (!member_fn && peek == 'F')
2518 while (pstart != pret)
2520 switch ((*pstart)->type)
2522 case DEMANGLE_COMPONENT_RESTRICT:
2523 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2524 break;
2525 case DEMANGLE_COMPONENT_VOLATILE:
2526 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2527 break;
2528 case DEMANGLE_COMPONENT_CONST:
2529 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2530 break;
2531 default:
2532 break;
2534 pstart = &d_left (*pstart);
2538 return pret;
2541 /* <ref-qualifier> ::= R
2542 ::= O */
2544 static struct demangle_component *
2545 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2547 struct demangle_component *ret = sub;
2548 char peek;
2550 peek = d_peek_char (di);
2551 if (peek == 'R' || peek == 'O')
2553 enum demangle_component_type t;
2554 if (peek == 'R')
2556 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2557 di->expansion += sizeof "&";
2559 else
2561 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2562 di->expansion += sizeof "&&";
2564 d_advance (di, 1);
2566 ret = d_make_comp (di, t, ret, NULL);
2569 return ret;
2572 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2574 static struct demangle_component *
2575 d_function_type (struct d_info *di)
2577 struct demangle_component *ret;
2579 if (! d_check_char (di, 'F'))
2580 return NULL;
2581 if (d_peek_char (di) == 'Y')
2583 /* Function has C linkage. We don't print this information.
2584 FIXME: We should print it in verbose mode. */
2585 d_advance (di, 1);
2587 ret = d_bare_function_type (di, 1);
2588 ret = d_ref_qualifier (di, ret);
2590 if (! d_check_char (di, 'E'))
2591 return NULL;
2592 return ret;
2595 /* <type>+ */
2597 static struct demangle_component *
2598 d_parmlist (struct d_info *di)
2600 struct demangle_component *tl;
2601 struct demangle_component **ptl;
2603 tl = NULL;
2604 ptl = &tl;
2605 while (1)
2607 struct demangle_component *type;
2609 char peek = d_peek_char (di);
2610 if (peek == '\0' || peek == 'E' || peek == '.')
2611 break;
2612 if ((peek == 'R' || peek == 'O')
2613 && d_peek_next_char (di) == 'E')
2614 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2615 break;
2616 type = cplus_demangle_type (di);
2617 if (type == NULL)
2618 return NULL;
2619 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2620 if (*ptl == NULL)
2621 return NULL;
2622 ptl = &d_right (*ptl);
2625 /* There should be at least one parameter type besides the optional
2626 return type. A function which takes no arguments will have a
2627 single parameter type void. */
2628 if (tl == NULL)
2629 return NULL;
2631 /* If we have a single parameter type void, omit it. */
2632 if (d_right (tl) == NULL
2633 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2634 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2636 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2637 d_left (tl) = NULL;
2640 return tl;
2643 /* <bare-function-type> ::= [J]<type>+ */
2645 static struct demangle_component *
2646 d_bare_function_type (struct d_info *di, int has_return_type)
2648 struct demangle_component *return_type;
2649 struct demangle_component *tl;
2650 char peek;
2652 /* Detect special qualifier indicating that the first argument
2653 is the return type. */
2654 peek = d_peek_char (di);
2655 if (peek == 'J')
2657 d_advance (di, 1);
2658 has_return_type = 1;
2661 if (has_return_type)
2663 return_type = cplus_demangle_type (di);
2664 if (return_type == NULL)
2665 return NULL;
2667 else
2668 return_type = NULL;
2670 tl = d_parmlist (di);
2671 if (tl == NULL)
2672 return NULL;
2674 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2675 return_type, tl);
2678 /* <class-enum-type> ::= <name> */
2680 static struct demangle_component *
2681 d_class_enum_type (struct d_info *di)
2683 return d_name (di);
2686 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2687 ::= A [<(dimension) expression>] _ <(element) type>
2690 static struct demangle_component *
2691 d_array_type (struct d_info *di)
2693 char peek;
2694 struct demangle_component *dim;
2696 if (! d_check_char (di, 'A'))
2697 return NULL;
2699 peek = d_peek_char (di);
2700 if (peek == '_')
2701 dim = NULL;
2702 else if (IS_DIGIT (peek))
2704 const char *s;
2706 s = d_str (di);
2709 d_advance (di, 1);
2710 peek = d_peek_char (di);
2712 while (IS_DIGIT (peek));
2713 dim = d_make_name (di, s, d_str (di) - s);
2714 if (dim == NULL)
2715 return NULL;
2717 else
2719 dim = d_expression (di);
2720 if (dim == NULL)
2721 return NULL;
2724 if (! d_check_char (di, '_'))
2725 return NULL;
2727 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2728 cplus_demangle_type (di));
2731 /* <vector-type> ::= Dv <number> _ <type>
2732 ::= Dv _ <expression> _ <type> */
2734 static struct demangle_component *
2735 d_vector_type (struct d_info *di)
2737 char peek;
2738 struct demangle_component *dim;
2740 peek = d_peek_char (di);
2741 if (peek == '_')
2743 d_advance (di, 1);
2744 dim = d_expression (di);
2746 else
2747 dim = d_number_component (di);
2749 if (dim == NULL)
2750 return NULL;
2752 if (! d_check_char (di, '_'))
2753 return NULL;
2755 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2756 cplus_demangle_type (di));
2759 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2761 static struct demangle_component *
2762 d_pointer_to_member_type (struct d_info *di)
2764 struct demangle_component *cl;
2765 struct demangle_component *mem;
2767 if (! d_check_char (di, 'M'))
2768 return NULL;
2770 cl = cplus_demangle_type (di);
2771 if (cl == NULL)
2772 return NULL;
2774 /* The ABI says, "The type of a non-static member function is considered
2775 to be different, for the purposes of substitution, from the type of a
2776 namespace-scope or static member function whose type appears
2777 similar. The types of two non-static member functions are considered
2778 to be different, for the purposes of substitution, if the functions
2779 are members of different classes. In other words, for the purposes of
2780 substitution, the class of which the function is a member is
2781 considered part of the type of function."
2783 For a pointer to member function, this call to cplus_demangle_type
2784 will end up adding a (possibly qualified) non-member function type to
2785 the substitution table, which is not correct; however, the member
2786 function type will never be used in a substitution, so putting the
2787 wrong type in the substitution table is harmless. */
2789 mem = cplus_demangle_type (di);
2790 if (mem == NULL)
2791 return NULL;
2793 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2796 /* <non-negative number> _ */
2798 static long
2799 d_compact_number (struct d_info *di)
2801 long num;
2802 if (d_peek_char (di) == '_')
2803 num = 0;
2804 else if (d_peek_char (di) == 'n')
2805 return -1;
2806 else
2807 num = d_number (di) + 1;
2809 if (! d_check_char (di, '_'))
2810 return -1;
2811 return num;
2814 /* <template-param> ::= T_
2815 ::= T <(parameter-2 non-negative) number> _
2818 static struct demangle_component *
2819 d_template_param (struct d_info *di)
2821 long param;
2823 if (! d_check_char (di, 'T'))
2824 return NULL;
2826 param = d_compact_number (di);
2827 if (param < 0)
2828 return NULL;
2830 ++di->did_subs;
2832 return d_make_template_param (di, param);
2835 /* <template-args> ::= I <template-arg>+ E */
2837 static struct demangle_component *
2838 d_template_args (struct d_info *di)
2840 struct demangle_component *hold_last_name;
2841 struct demangle_component *al;
2842 struct demangle_component **pal;
2844 /* Preserve the last name we saw--don't let the template arguments
2845 clobber it, as that would give us the wrong name for a subsequent
2846 constructor or destructor. */
2847 hold_last_name = di->last_name;
2849 if (d_peek_char (di) != 'I'
2850 && d_peek_char (di) != 'J')
2851 return NULL;
2852 d_advance (di, 1);
2854 if (d_peek_char (di) == 'E')
2856 /* An argument pack can be empty. */
2857 d_advance (di, 1);
2858 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2861 al = NULL;
2862 pal = &al;
2863 while (1)
2865 struct demangle_component *a;
2867 a = d_template_arg (di);
2868 if (a == NULL)
2869 return NULL;
2871 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2872 if (*pal == NULL)
2873 return NULL;
2874 pal = &d_right (*pal);
2876 if (d_peek_char (di) == 'E')
2878 d_advance (di, 1);
2879 break;
2883 di->last_name = hold_last_name;
2885 return al;
2888 /* <template-arg> ::= <type>
2889 ::= X <expression> E
2890 ::= <expr-primary>
2893 static struct demangle_component *
2894 d_template_arg (struct d_info *di)
2896 struct demangle_component *ret;
2898 switch (d_peek_char (di))
2900 case 'X':
2901 d_advance (di, 1);
2902 ret = d_expression (di);
2903 if (! d_check_char (di, 'E'))
2904 return NULL;
2905 return ret;
2907 case 'L':
2908 return d_expr_primary (di);
2910 case 'I':
2911 case 'J':
2912 /* An argument pack. */
2913 return d_template_args (di);
2915 default:
2916 return cplus_demangle_type (di);
2920 /* Parse a sequence of expressions until we hit the terminator
2921 character. */
2923 static struct demangle_component *
2924 d_exprlist (struct d_info *di, char terminator)
2926 struct demangle_component *list = NULL;
2927 struct demangle_component **p = &list;
2929 if (d_peek_char (di) == terminator)
2931 d_advance (di, 1);
2932 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2935 while (1)
2937 struct demangle_component *arg = d_expression (di);
2938 if (arg == NULL)
2939 return NULL;
2941 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2942 if (*p == NULL)
2943 return NULL;
2944 p = &d_right (*p);
2946 if (d_peek_char (di) == terminator)
2948 d_advance (di, 1);
2949 break;
2953 return list;
2956 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2957 dynamic_cast, static_cast or reinterpret_cast. */
2959 static int
2960 op_is_new_cast (struct demangle_component *op)
2962 const char *code = op->u.s_operator.op->code;
2963 return (code[1] == 'c'
2964 && (code[0] == 's' || code[0] == 'd'
2965 || code[0] == 'c' || code[0] == 'r'));
2968 /* <expression> ::= <(unary) operator-name> <expression>
2969 ::= <(binary) operator-name> <expression> <expression>
2970 ::= <(trinary) operator-name> <expression> <expression> <expression>
2971 ::= cl <expression>+ E
2972 ::= st <type>
2973 ::= <template-param>
2974 ::= sr <type> <unqualified-name>
2975 ::= sr <type> <unqualified-name> <template-args>
2976 ::= <expr-primary>
2979 static struct demangle_component *
2980 d_expression (struct d_info *di)
2982 char peek;
2984 peek = d_peek_char (di);
2985 if (peek == 'L')
2986 return d_expr_primary (di);
2987 else if (peek == 'T')
2988 return d_template_param (di);
2989 else if (peek == 's' && d_peek_next_char (di) == 'r')
2991 struct demangle_component *type;
2992 struct demangle_component *name;
2994 d_advance (di, 2);
2995 type = cplus_demangle_type (di);
2996 name = d_unqualified_name (di);
2997 if (d_peek_char (di) != 'I')
2998 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2999 else
3000 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3001 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3002 d_template_args (di)));
3004 else if (peek == 's' && d_peek_next_char (di) == 'p')
3006 d_advance (di, 2);
3007 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3008 d_expression (di), NULL);
3010 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3012 /* Function parameter used in a late-specified return type. */
3013 int index;
3014 d_advance (di, 2);
3015 if (d_peek_char (di) == 'T')
3017 /* 'this' parameter. */
3018 d_advance (di, 1);
3019 index = 0;
3021 else
3023 index = d_compact_number (di) + 1;
3024 if (index == 0)
3025 return NULL;
3027 return d_make_function_param (di, index);
3029 else if (IS_DIGIT (peek)
3030 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3032 /* We can get an unqualified name as an expression in the case of
3033 a dependent function call, i.e. decltype(f(t)). */
3034 struct demangle_component *name;
3036 if (peek == 'o')
3037 /* operator-function-id, i.e. operator+(t). */
3038 d_advance (di, 2);
3040 name = d_unqualified_name (di);
3041 if (name == NULL)
3042 return NULL;
3043 if (d_peek_char (di) == 'I')
3044 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3045 d_template_args (di));
3046 else
3047 return name;
3049 else if ((peek == 'i' || peek == 't')
3050 && d_peek_next_char (di) == 'l')
3052 /* Brace-enclosed initializer list, untyped or typed. */
3053 struct demangle_component *type = NULL;
3054 if (peek == 't')
3055 type = cplus_demangle_type (di);
3056 d_advance (di, 2);
3057 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3058 type, d_exprlist (di, 'E'));
3060 else
3062 struct demangle_component *op;
3063 const char *code = NULL;
3064 int args;
3066 op = d_operator_name (di);
3067 if (op == NULL)
3068 return NULL;
3070 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3072 code = op->u.s_operator.op->code;
3073 di->expansion += op->u.s_operator.op->len - 2;
3074 if (strcmp (code, "st") == 0)
3075 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3076 cplus_demangle_type (di));
3079 switch (op->type)
3081 default:
3082 return NULL;
3083 case DEMANGLE_COMPONENT_OPERATOR:
3084 args = op->u.s_operator.op->args;
3085 break;
3086 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3087 args = op->u.s_extended_operator.args;
3088 break;
3089 case DEMANGLE_COMPONENT_CAST:
3090 args = 1;
3091 break;
3094 switch (args)
3096 case 0:
3097 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3099 case 1:
3101 struct demangle_component *operand;
3102 int suffix = 0;
3104 if (code && (code[0] == 'p' || code[0] == 'm')
3105 && code[1] == code[0])
3106 /* pp_ and mm_ are the prefix variants. */
3107 suffix = !d_check_char (di, '_');
3109 if (op->type == DEMANGLE_COMPONENT_CAST
3110 && d_check_char (di, '_'))
3111 operand = d_exprlist (di, 'E');
3112 else
3113 operand = d_expression (di);
3115 if (suffix)
3116 /* Indicate the suffix variant for d_print_comp. */
3117 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3118 d_make_comp (di,
3119 DEMANGLE_COMPONENT_BINARY_ARGS,
3120 operand, operand));
3121 else
3122 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3123 operand);
3125 case 2:
3127 struct demangle_component *left;
3128 struct demangle_component *right;
3130 if (op_is_new_cast (op))
3131 left = cplus_demangle_type (di);
3132 else
3133 left = d_expression (di);
3134 if (!strcmp (code, "cl"))
3135 right = d_exprlist (di, 'E');
3136 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3138 right = d_unqualified_name (di);
3139 if (d_peek_char (di) == 'I')
3140 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3141 right, d_template_args (di));
3143 else
3144 right = d_expression (di);
3146 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3147 d_make_comp (di,
3148 DEMANGLE_COMPONENT_BINARY_ARGS,
3149 left, right));
3151 case 3:
3153 struct demangle_component *first;
3154 struct demangle_component *second;
3155 struct demangle_component *third;
3157 if (!strcmp (code, "qu"))
3159 /* ?: expression. */
3160 first = d_expression (di);
3161 second = d_expression (di);
3162 third = d_expression (di);
3164 else if (code[0] == 'n')
3166 /* new-expression. */
3167 if (code[1] != 'w' && code[1] != 'a')
3168 return NULL;
3169 first = d_exprlist (di, '_');
3170 second = cplus_demangle_type (di);
3171 if (d_peek_char (di) == 'E')
3173 d_advance (di, 1);
3174 third = NULL;
3176 else if (d_peek_char (di) == 'p'
3177 && d_peek_next_char (di) == 'i')
3179 /* Parenthesized initializer. */
3180 d_advance (di, 2);
3181 third = d_exprlist (di, 'E');
3183 else if (d_peek_char (di) == 'i'
3184 && d_peek_next_char (di) == 'l')
3185 /* initializer-list. */
3186 third = d_expression (di);
3187 else
3188 return NULL;
3190 else
3191 return NULL;
3192 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3193 d_make_comp (di,
3194 DEMANGLE_COMPONENT_TRINARY_ARG1,
3195 first,
3196 d_make_comp (di,
3197 DEMANGLE_COMPONENT_TRINARY_ARG2,
3198 second, third)));
3200 default:
3201 return NULL;
3206 /* <expr-primary> ::= L <type> <(value) number> E
3207 ::= L <type> <(value) float> E
3208 ::= L <mangled-name> E
3211 static struct demangle_component *
3212 d_expr_primary (struct d_info *di)
3214 struct demangle_component *ret;
3216 if (! d_check_char (di, 'L'))
3217 return NULL;
3218 if (d_peek_char (di) == '_'
3219 /* Workaround for G++ bug; see comment in write_template_arg. */
3220 || d_peek_char (di) == 'Z')
3221 ret = cplus_demangle_mangled_name (di, 0);
3222 else
3224 struct demangle_component *type;
3225 enum demangle_component_type t;
3226 const char *s;
3228 type = cplus_demangle_type (di);
3229 if (type == NULL)
3230 return NULL;
3232 /* If we have a type we know how to print, we aren't going to
3233 print the type name itself. */
3234 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3235 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3236 di->expansion -= type->u.s_builtin.type->len;
3238 /* Rather than try to interpret the literal value, we just
3239 collect it as a string. Note that it's possible to have a
3240 floating point literal here. The ABI specifies that the
3241 format of such literals is machine independent. That's fine,
3242 but what's not fine is that versions of g++ up to 3.2 with
3243 -fabi-version=1 used upper case letters in the hex constant,
3244 and dumped out gcc's internal representation. That makes it
3245 hard to tell where the constant ends, and hard to dump the
3246 constant in any readable form anyhow. We don't attempt to
3247 handle these cases. */
3249 t = DEMANGLE_COMPONENT_LITERAL;
3250 if (d_peek_char (di) == 'n')
3252 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3253 d_advance (di, 1);
3255 s = d_str (di);
3256 while (d_peek_char (di) != 'E')
3258 if (d_peek_char (di) == '\0')
3259 return NULL;
3260 d_advance (di, 1);
3262 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3264 if (! d_check_char (di, 'E'))
3265 return NULL;
3266 return ret;
3269 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3270 ::= Z <(function) encoding> E s [<discriminator>]
3271 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3274 static struct demangle_component *
3275 d_local_name (struct d_info *di)
3277 struct demangle_component *function;
3279 if (! d_check_char (di, 'Z'))
3280 return NULL;
3282 function = d_encoding (di, 0);
3284 if (! d_check_char (di, 'E'))
3285 return NULL;
3287 if (d_peek_char (di) == 's')
3289 d_advance (di, 1);
3290 if (! d_discriminator (di))
3291 return NULL;
3292 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3293 d_make_name (di, "string literal",
3294 sizeof "string literal" - 1));
3296 else
3298 struct demangle_component *name;
3299 int num = -1;
3301 if (d_peek_char (di) == 'd')
3303 /* Default argument scope: d <number> _. */
3304 d_advance (di, 1);
3305 num = d_compact_number (di);
3306 if (num < 0)
3307 return NULL;
3310 name = d_name (di);
3311 if (name)
3312 switch (name->type)
3314 /* Lambdas and unnamed types have internal discriminators. */
3315 case DEMANGLE_COMPONENT_LAMBDA:
3316 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3317 break;
3318 default:
3319 if (! d_discriminator (di))
3320 return NULL;
3322 if (num >= 0)
3323 name = d_make_default_arg (di, num, name);
3324 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3328 /* <discriminator> ::= _ <(non-negative) number>
3330 We demangle the discriminator, but we don't print it out. FIXME:
3331 We should print it out in verbose mode. */
3333 static int
3334 d_discriminator (struct d_info *di)
3336 long discrim;
3338 if (d_peek_char (di) != '_')
3339 return 1;
3340 d_advance (di, 1);
3341 discrim = d_number (di);
3342 if (discrim < 0)
3343 return 0;
3344 return 1;
3347 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3349 static struct demangle_component *
3350 d_lambda (struct d_info *di)
3352 struct demangle_component *tl;
3353 struct demangle_component *ret;
3354 int num;
3356 if (! d_check_char (di, 'U'))
3357 return NULL;
3358 if (! d_check_char (di, 'l'))
3359 return NULL;
3361 tl = d_parmlist (di);
3362 if (tl == NULL)
3363 return NULL;
3365 if (! d_check_char (di, 'E'))
3366 return NULL;
3368 num = d_compact_number (di);
3369 if (num < 0)
3370 return NULL;
3372 ret = d_make_empty (di);
3373 if (ret)
3375 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3376 ret->u.s_unary_num.sub = tl;
3377 ret->u.s_unary_num.num = num;
3380 if (! d_add_substitution (di, ret))
3381 return NULL;
3383 return ret;
3386 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3388 static struct demangle_component *
3389 d_unnamed_type (struct d_info *di)
3391 struct demangle_component *ret;
3392 long num;
3394 if (! d_check_char (di, 'U'))
3395 return NULL;
3396 if (! d_check_char (di, 't'))
3397 return NULL;
3399 num = d_compact_number (di);
3400 if (num < 0)
3401 return NULL;
3403 ret = d_make_empty (di);
3404 if (ret)
3406 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3407 ret->u.s_number.number = num;
3410 if (! d_add_substitution (di, ret))
3411 return NULL;
3413 return ret;
3416 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3419 static struct demangle_component *
3420 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3422 const char *suffix = d_str (di);
3423 const char *pend = suffix;
3424 struct demangle_component *n;
3426 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3428 pend += 2;
3429 while (IS_LOWER (*pend) || *pend == '_')
3430 ++pend;
3432 while (*pend == '.' && IS_DIGIT (pend[1]))
3434 pend += 2;
3435 while (IS_DIGIT (*pend))
3436 ++pend;
3438 d_advance (di, pend - suffix);
3439 n = d_make_name (di, suffix, pend - suffix);
3440 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3443 /* Add a new substitution. */
3445 static int
3446 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3448 if (dc == NULL)
3449 return 0;
3450 if (di->next_sub >= di->num_subs)
3451 return 0;
3452 di->subs[di->next_sub] = dc;
3453 ++di->next_sub;
3454 return 1;
3457 /* <substitution> ::= S <seq-id> _
3458 ::= S_
3459 ::= St
3460 ::= Sa
3461 ::= Sb
3462 ::= Ss
3463 ::= Si
3464 ::= So
3465 ::= Sd
3467 If PREFIX is non-zero, then this type is being used as a prefix in
3468 a qualified name. In this case, for the standard substitutions, we
3469 need to check whether we are being used as a prefix for a
3470 constructor or destructor, and return a full template name.
3471 Otherwise we will get something like std::iostream::~iostream()
3472 which does not correspond particularly well to any function which
3473 actually appears in the source.
3476 static const struct d_standard_sub_info standard_subs[] =
3478 { 't', NL ("std"),
3479 NL ("std"),
3480 NULL, 0 },
3481 { 'a', NL ("std::allocator"),
3482 NL ("std::allocator"),
3483 NL ("allocator") },
3484 { 'b', NL ("std::basic_string"),
3485 NL ("std::basic_string"),
3486 NL ("basic_string") },
3487 { 's', NL ("std::string"),
3488 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3489 NL ("basic_string") },
3490 { 'i', NL ("std::istream"),
3491 NL ("std::basic_istream<char, std::char_traits<char> >"),
3492 NL ("basic_istream") },
3493 { 'o', NL ("std::ostream"),
3494 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3495 NL ("basic_ostream") },
3496 { 'd', NL ("std::iostream"),
3497 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3498 NL ("basic_iostream") }
3501 static struct demangle_component *
3502 d_substitution (struct d_info *di, int prefix)
3504 char c;
3506 if (! d_check_char (di, 'S'))
3507 return NULL;
3509 c = d_next_char (di);
3510 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3512 unsigned int id;
3514 id = 0;
3515 if (c != '_')
3519 unsigned int new_id;
3521 if (IS_DIGIT (c))
3522 new_id = id * 36 + c - '0';
3523 else if (IS_UPPER (c))
3524 new_id = id * 36 + c - 'A' + 10;
3525 else
3526 return NULL;
3527 if (new_id < id)
3528 return NULL;
3529 id = new_id;
3530 c = d_next_char (di);
3532 while (c != '_');
3534 ++id;
3537 if (id >= (unsigned int) di->next_sub)
3538 return NULL;
3540 ++di->did_subs;
3542 return di->subs[id];
3544 else
3546 int verbose;
3547 const struct d_standard_sub_info *p;
3548 const struct d_standard_sub_info *pend;
3550 verbose = (di->options & DMGL_VERBOSE) != 0;
3551 if (! verbose && prefix)
3553 char peek;
3555 peek = d_peek_char (di);
3556 if (peek == 'C' || peek == 'D')
3557 verbose = 1;
3560 pend = (&standard_subs[0]
3561 + sizeof standard_subs / sizeof standard_subs[0]);
3562 for (p = &standard_subs[0]; p < pend; ++p)
3564 if (c == p->code)
3566 const char *s;
3567 int len;
3569 if (p->set_last_name != NULL)
3570 di->last_name = d_make_sub (di, p->set_last_name,
3571 p->set_last_name_len);
3572 if (verbose)
3574 s = p->full_expansion;
3575 len = p->full_len;
3577 else
3579 s = p->simple_expansion;
3580 len = p->simple_len;
3582 di->expansion += len;
3583 return d_make_sub (di, s, len);
3587 return NULL;
3591 /* Initialize a growable string. */
3593 static void
3594 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3596 dgs->buf = NULL;
3597 dgs->len = 0;
3598 dgs->alc = 0;
3599 dgs->allocation_failure = 0;
3601 if (estimate > 0)
3602 d_growable_string_resize (dgs, estimate);
3605 /* Grow a growable string to a given size. */
3607 static inline void
3608 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3610 size_t newalc;
3611 char *newbuf;
3613 if (dgs->allocation_failure)
3614 return;
3616 /* Start allocation at two bytes to avoid any possibility of confusion
3617 with the special value of 1 used as a return in *palc to indicate
3618 allocation failures. */
3619 newalc = dgs->alc > 0 ? dgs->alc : 2;
3620 while (newalc < need)
3621 newalc <<= 1;
3623 newbuf = (char *) realloc (dgs->buf, newalc);
3624 if (newbuf == NULL)
3626 free (dgs->buf);
3627 dgs->buf = NULL;
3628 dgs->len = 0;
3629 dgs->alc = 0;
3630 dgs->allocation_failure = 1;
3631 return;
3633 dgs->buf = newbuf;
3634 dgs->alc = newalc;
3637 /* Append a buffer to a growable string. */
3639 static inline void
3640 d_growable_string_append_buffer (struct d_growable_string *dgs,
3641 const char *s, size_t l)
3643 size_t need;
3645 need = dgs->len + l + 1;
3646 if (need > dgs->alc)
3647 d_growable_string_resize (dgs, need);
3649 if (dgs->allocation_failure)
3650 return;
3652 memcpy (dgs->buf + dgs->len, s, l);
3653 dgs->buf[dgs->len + l] = '\0';
3654 dgs->len += l;
3657 /* Bridge growable strings to the callback mechanism. */
3659 static void
3660 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3662 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3664 d_growable_string_append_buffer (dgs, s, l);
3667 /* Initialize a print information structure. */
3669 static void
3670 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3671 void *opaque)
3673 dpi->len = 0;
3674 dpi->last_char = '\0';
3675 dpi->templates = NULL;
3676 dpi->modifiers = NULL;
3677 dpi->pack_index = 0;
3678 dpi->flush_count = 0;
3680 dpi->callback = callback;
3681 dpi->opaque = opaque;
3683 dpi->demangle_failure = 0;
3685 dpi->saved_scopes = NULL;
3686 dpi->num_saved_scopes = 0;
3689 /* Free a print information structure. */
3691 static void
3692 d_print_free (struct d_print_info *dpi)
3694 int i;
3696 for (i = 0; i < dpi->num_saved_scopes; i++)
3698 struct d_print_template *ts, *tn;
3700 for (ts = dpi->saved_scopes[i].templates; ts != NULL; ts = tn)
3702 tn = ts->next;
3703 free (ts);
3707 free (dpi->saved_scopes);
3710 /* Indicate that an error occurred during printing, and test for error. */
3712 static inline void
3713 d_print_error (struct d_print_info *dpi)
3715 dpi->demangle_failure = 1;
3718 static inline int
3719 d_print_saw_error (struct d_print_info *dpi)
3721 return dpi->demangle_failure != 0;
3724 /* Flush buffered characters to the callback. */
3726 static inline void
3727 d_print_flush (struct d_print_info *dpi)
3729 dpi->buf[dpi->len] = '\0';
3730 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3731 dpi->len = 0;
3732 dpi->flush_count++;
3735 /* Append characters and buffers for printing. */
3737 static inline void
3738 d_append_char (struct d_print_info *dpi, char c)
3740 if (dpi->len == sizeof (dpi->buf) - 1)
3741 d_print_flush (dpi);
3743 dpi->buf[dpi->len++] = c;
3744 dpi->last_char = c;
3747 static inline void
3748 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3750 size_t i;
3752 for (i = 0; i < l; i++)
3753 d_append_char (dpi, s[i]);
3756 static inline void
3757 d_append_string (struct d_print_info *dpi, const char *s)
3759 d_append_buffer (dpi, s, strlen (s));
3762 static inline void
3763 d_append_num (struct d_print_info *dpi, long l)
3765 char buf[25];
3766 sprintf (buf,"%ld", l);
3767 d_append_string (dpi, buf);
3770 static inline char
3771 d_last_char (struct d_print_info *dpi)
3773 return dpi->last_char;
3776 /* Turn components into a human readable string. OPTIONS is the
3777 options bits passed to the demangler. DC is the tree to print.
3778 CALLBACK is a function to call to flush demangled string segments
3779 as they fill the intermediate buffer, and OPAQUE is a generalized
3780 callback argument. On success, this returns 1. On failure,
3781 it returns 0, indicating a bad parse. It does not use heap
3782 memory to build an output string, so cannot encounter memory
3783 allocation failure. */
3785 CP_STATIC_IF_GLIBCPP_V3
3787 cplus_demangle_print_callback (int options,
3788 const struct demangle_component *dc,
3789 demangle_callbackref callback, void *opaque)
3791 struct d_print_info dpi;
3792 int success;
3794 d_print_init (&dpi, callback, opaque);
3796 d_print_comp (&dpi, options, dc);
3798 d_print_flush (&dpi);
3800 success = ! d_print_saw_error (&dpi);
3801 d_print_free (&dpi);
3802 return success;
3805 /* Turn components into a human readable string. OPTIONS is the
3806 options bits passed to the demangler. DC is the tree to print.
3807 ESTIMATE is a guess at the length of the result. This returns a
3808 string allocated by malloc, or NULL on error. On success, this
3809 sets *PALC to the size of the allocated buffer. On failure, this
3810 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3811 failure. */
3813 CP_STATIC_IF_GLIBCPP_V3
3814 char *
3815 cplus_demangle_print (int options, const struct demangle_component *dc,
3816 int estimate, size_t *palc)
3818 struct d_growable_string dgs;
3820 d_growable_string_init (&dgs, estimate);
3822 if (! cplus_demangle_print_callback (options, dc,
3823 d_growable_string_callback_adapter,
3824 &dgs))
3826 free (dgs.buf);
3827 *palc = 0;
3828 return NULL;
3831 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3832 return dgs.buf;
3835 /* Returns the I'th element of the template arglist ARGS, or NULL on
3836 failure. */
3838 static struct demangle_component *
3839 d_index_template_argument (struct demangle_component *args, int i)
3841 struct demangle_component *a;
3843 for (a = args;
3844 a != NULL;
3845 a = d_right (a))
3847 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3848 return NULL;
3849 if (i <= 0)
3850 break;
3851 --i;
3853 if (i != 0 || a == NULL)
3854 return NULL;
3856 return d_left (a);
3859 /* Returns the template argument from the current context indicated by DC,
3860 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3862 static struct demangle_component *
3863 d_lookup_template_argument (struct d_print_info *dpi,
3864 const struct demangle_component *dc)
3866 if (dpi->templates == NULL)
3868 d_print_error (dpi);
3869 return NULL;
3872 return d_index_template_argument
3873 (d_right (dpi->templates->template_decl),
3874 dc->u.s_number.number);
3877 /* Returns a template argument pack used in DC (any will do), or NULL. */
3879 static struct demangle_component *
3880 d_find_pack (struct d_print_info *dpi,
3881 const struct demangle_component *dc)
3883 struct demangle_component *a;
3884 if (dc == NULL)
3885 return NULL;
3887 switch (dc->type)
3889 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3890 a = d_lookup_template_argument (dpi, dc);
3891 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3892 return a;
3893 return NULL;
3895 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3896 return NULL;
3898 case DEMANGLE_COMPONENT_LAMBDA:
3899 case DEMANGLE_COMPONENT_NAME:
3900 case DEMANGLE_COMPONENT_TAGGED_NAME:
3901 case DEMANGLE_COMPONENT_OPERATOR:
3902 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3903 case DEMANGLE_COMPONENT_SUB_STD:
3904 case DEMANGLE_COMPONENT_CHARACTER:
3905 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3906 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3907 return NULL;
3909 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3910 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3911 case DEMANGLE_COMPONENT_CTOR:
3912 return d_find_pack (dpi, dc->u.s_ctor.name);
3913 case DEMANGLE_COMPONENT_DTOR:
3914 return d_find_pack (dpi, dc->u.s_dtor.name);
3916 default:
3917 a = d_find_pack (dpi, d_left (dc));
3918 if (a)
3919 return a;
3920 return d_find_pack (dpi, d_right (dc));
3924 /* Returns the length of the template argument pack DC. */
3926 static int
3927 d_pack_length (const struct demangle_component *dc)
3929 int count = 0;
3930 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3931 && d_left (dc) != NULL)
3933 ++count;
3934 dc = d_right (dc);
3936 return count;
3939 /* DC is a component of a mangled expression. Print it, wrapped in parens
3940 if needed. */
3942 static void
3943 d_print_subexpr (struct d_print_info *dpi, int options,
3944 const struct demangle_component *dc)
3946 int simple = 0;
3947 if (dc->type == DEMANGLE_COMPONENT_NAME
3948 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
3949 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
3950 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3951 simple = 1;
3952 if (!simple)
3953 d_append_char (dpi, '(');
3954 d_print_comp (dpi, options, dc);
3955 if (!simple)
3956 d_append_char (dpi, ')');
3959 /* Return a shallow copy of the current list of templates.
3960 On error d_print_error is called and a partial list may
3961 be returned. Whatever is returned must be freed. */
3963 static struct d_print_template *
3964 d_copy_templates (struct d_print_info *dpi)
3966 struct d_print_template *src, *result, **link = &result;
3968 for (src = dpi->templates; src != NULL; src = src->next)
3970 struct d_print_template *dst =
3971 malloc (sizeof (struct d_print_template));
3973 if (dst == NULL)
3975 d_print_error (dpi);
3976 break;
3979 dst->template_decl = src->template_decl;
3980 *link = dst;
3981 link = &dst->next;
3984 *link = NULL;
3986 return result;
3989 /* Subroutine to handle components. */
3991 static void
3992 d_print_comp (struct d_print_info *dpi, int options,
3993 const struct demangle_component *dc)
3995 /* Magic variable to let reference smashing skip over the next modifier
3996 without needing to modify *dc. */
3997 const struct demangle_component *mod_inner = NULL;
3999 /* Variable used to store the current templates while a previously
4000 captured scope is used. */
4001 struct d_print_template *saved_templates;
4003 /* Nonzero if templates have been stored in the above variable. */
4004 int need_template_restore = 0;
4006 if (dc == NULL)
4008 d_print_error (dpi);
4009 return;
4011 if (d_print_saw_error (dpi))
4012 return;
4014 switch (dc->type)
4016 case DEMANGLE_COMPONENT_NAME:
4017 if ((options & DMGL_JAVA) == 0)
4018 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4019 else
4020 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4021 return;
4023 case DEMANGLE_COMPONENT_TAGGED_NAME:
4024 d_print_comp (dpi, options, d_left (dc));
4025 d_append_string (dpi, "[abi:");
4026 d_print_comp (dpi, options, d_right (dc));
4027 d_append_char (dpi, ']');
4028 return;
4030 case DEMANGLE_COMPONENT_QUAL_NAME:
4031 case DEMANGLE_COMPONENT_LOCAL_NAME:
4032 d_print_comp (dpi, options, d_left (dc));
4033 if ((options & DMGL_JAVA) == 0)
4034 d_append_string (dpi, "::");
4035 else
4036 d_append_char (dpi, '.');
4038 struct demangle_component *local_name = d_right (dc);
4039 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4041 d_append_string (dpi, "{default arg#");
4042 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4043 d_append_string (dpi, "}::");
4044 local_name = local_name->u.s_unary_num.sub;
4046 d_print_comp (dpi, options, local_name);
4048 return;
4050 case DEMANGLE_COMPONENT_TYPED_NAME:
4052 struct d_print_mod *hold_modifiers;
4053 struct demangle_component *typed_name;
4054 struct d_print_mod adpm[4];
4055 unsigned int i;
4056 struct d_print_template dpt;
4058 /* Pass the name down to the type so that it can be printed in
4059 the right place for the type. We also have to pass down
4060 any CV-qualifiers, which apply to the this parameter. */
4061 hold_modifiers = dpi->modifiers;
4062 dpi->modifiers = 0;
4063 i = 0;
4064 typed_name = d_left (dc);
4065 while (typed_name != NULL)
4067 if (i >= sizeof adpm / sizeof adpm[0])
4069 d_print_error (dpi);
4070 return;
4073 adpm[i].next = dpi->modifiers;
4074 dpi->modifiers = &adpm[i];
4075 adpm[i].mod = typed_name;
4076 adpm[i].printed = 0;
4077 adpm[i].templates = dpi->templates;
4078 ++i;
4080 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4081 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4082 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4083 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4084 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4085 break;
4087 typed_name = d_left (typed_name);
4090 if (typed_name == NULL)
4092 d_print_error (dpi);
4093 return;
4096 /* If typed_name is a template, then it applies to the
4097 function type as well. */
4098 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4100 dpt.next = dpi->templates;
4101 dpi->templates = &dpt;
4102 dpt.template_decl = typed_name;
4105 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4106 there may be CV-qualifiers on its right argument which
4107 really apply here; this happens when parsing a class which
4108 is local to a function. */
4109 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4111 struct demangle_component *local_name;
4113 local_name = d_right (typed_name);
4114 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4115 local_name = local_name->u.s_unary_num.sub;
4116 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4117 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4118 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4119 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4120 || (local_name->type
4121 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4123 if (i >= sizeof adpm / sizeof adpm[0])
4125 d_print_error (dpi);
4126 return;
4129 adpm[i] = adpm[i - 1];
4130 adpm[i].next = &adpm[i - 1];
4131 dpi->modifiers = &adpm[i];
4133 adpm[i - 1].mod = local_name;
4134 adpm[i - 1].printed = 0;
4135 adpm[i - 1].templates = dpi->templates;
4136 ++i;
4138 local_name = d_left (local_name);
4142 d_print_comp (dpi, options, d_right (dc));
4144 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4145 dpi->templates = dpt.next;
4147 /* If the modifiers didn't get printed by the type, print them
4148 now. */
4149 while (i > 0)
4151 --i;
4152 if (! adpm[i].printed)
4154 d_append_char (dpi, ' ');
4155 d_print_mod (dpi, options, adpm[i].mod);
4159 dpi->modifiers = hold_modifiers;
4161 return;
4164 case DEMANGLE_COMPONENT_TEMPLATE:
4166 struct d_print_mod *hold_dpm;
4167 struct demangle_component *dcl;
4169 /* Don't push modifiers into a template definition. Doing so
4170 could give the wrong definition for a template argument.
4171 Instead, treat the template essentially as a name. */
4173 hold_dpm = dpi->modifiers;
4174 dpi->modifiers = NULL;
4176 dcl = d_left (dc);
4178 if ((options & DMGL_JAVA) != 0
4179 && dcl->type == DEMANGLE_COMPONENT_NAME
4180 && dcl->u.s_name.len == 6
4181 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4183 /* Special-case Java arrays, so that JArray<TYPE> appears
4184 instead as TYPE[]. */
4186 d_print_comp (dpi, options, d_right (dc));
4187 d_append_string (dpi, "[]");
4189 else
4191 d_print_comp (dpi, options, dcl);
4192 if (d_last_char (dpi) == '<')
4193 d_append_char (dpi, ' ');
4194 d_append_char (dpi, '<');
4195 d_print_comp (dpi, options, d_right (dc));
4196 /* Avoid generating two consecutive '>' characters, to avoid
4197 the C++ syntactic ambiguity. */
4198 if (d_last_char (dpi) == '>')
4199 d_append_char (dpi, ' ');
4200 d_append_char (dpi, '>');
4203 dpi->modifiers = hold_dpm;
4205 return;
4208 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4210 struct d_print_template *hold_dpt;
4211 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4213 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4214 a = d_index_template_argument (a, dpi->pack_index);
4216 if (a == NULL)
4218 d_print_error (dpi);
4219 return;
4222 /* While processing this parameter, we need to pop the list of
4223 templates. This is because the template parameter may
4224 itself be a reference to a parameter of an outer
4225 template. */
4227 hold_dpt = dpi->templates;
4228 dpi->templates = hold_dpt->next;
4230 d_print_comp (dpi, options, a);
4232 dpi->templates = hold_dpt;
4234 return;
4237 case DEMANGLE_COMPONENT_CTOR:
4238 d_print_comp (dpi, options, dc->u.s_ctor.name);
4239 return;
4241 case DEMANGLE_COMPONENT_DTOR:
4242 d_append_char (dpi, '~');
4243 d_print_comp (dpi, options, dc->u.s_dtor.name);
4244 return;
4246 case DEMANGLE_COMPONENT_VTABLE:
4247 d_append_string (dpi, "vtable for ");
4248 d_print_comp (dpi, options, d_left (dc));
4249 return;
4251 case DEMANGLE_COMPONENT_VTT:
4252 d_append_string (dpi, "VTT for ");
4253 d_print_comp (dpi, options, d_left (dc));
4254 return;
4256 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4257 d_append_string (dpi, "construction vtable for ");
4258 d_print_comp (dpi, options, d_left (dc));
4259 d_append_string (dpi, "-in-");
4260 d_print_comp (dpi, options, d_right (dc));
4261 return;
4263 case DEMANGLE_COMPONENT_TYPEINFO:
4264 d_append_string (dpi, "typeinfo for ");
4265 d_print_comp (dpi, options, d_left (dc));
4266 return;
4268 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4269 d_append_string (dpi, "typeinfo name for ");
4270 d_print_comp (dpi, options, d_left (dc));
4271 return;
4273 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4274 d_append_string (dpi, "typeinfo fn for ");
4275 d_print_comp (dpi, options, d_left (dc));
4276 return;
4278 case DEMANGLE_COMPONENT_THUNK:
4279 d_append_string (dpi, "non-virtual thunk to ");
4280 d_print_comp (dpi, options, d_left (dc));
4281 return;
4283 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4284 d_append_string (dpi, "virtual thunk to ");
4285 d_print_comp (dpi, options, d_left (dc));
4286 return;
4288 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4289 d_append_string (dpi, "covariant return thunk to ");
4290 d_print_comp (dpi, options, d_left (dc));
4291 return;
4293 case DEMANGLE_COMPONENT_JAVA_CLASS:
4294 d_append_string (dpi, "java Class for ");
4295 d_print_comp (dpi, options, d_left (dc));
4296 return;
4298 case DEMANGLE_COMPONENT_GUARD:
4299 d_append_string (dpi, "guard variable for ");
4300 d_print_comp (dpi, options, d_left (dc));
4301 return;
4303 case DEMANGLE_COMPONENT_TLS_INIT:
4304 d_append_string (dpi, "TLS init function for ");
4305 d_print_comp (dpi, options, d_left (dc));
4306 return;
4308 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4309 d_append_string (dpi, "TLS wrapper function for ");
4310 d_print_comp (dpi, options, d_left (dc));
4311 return;
4313 case DEMANGLE_COMPONENT_REFTEMP:
4314 d_append_string (dpi, "reference temporary #");
4315 d_print_comp (dpi, options, d_right (dc));
4316 d_append_string (dpi, " for ");
4317 d_print_comp (dpi, options, d_left (dc));
4318 return;
4320 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4321 d_append_string (dpi, "hidden alias for ");
4322 d_print_comp (dpi, options, d_left (dc));
4323 return;
4325 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4326 d_append_string (dpi, "transaction clone for ");
4327 d_print_comp (dpi, options, d_left (dc));
4328 return;
4330 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4331 d_append_string (dpi, "non-transaction clone for ");
4332 d_print_comp (dpi, options, d_left (dc));
4333 return;
4335 case DEMANGLE_COMPONENT_SUB_STD:
4336 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4337 return;
4339 case DEMANGLE_COMPONENT_RESTRICT:
4340 case DEMANGLE_COMPONENT_VOLATILE:
4341 case DEMANGLE_COMPONENT_CONST:
4343 struct d_print_mod *pdpm;
4345 /* When printing arrays, it's possible to have cases where the
4346 same CV-qualifier gets pushed on the stack multiple times.
4347 We only need to print it once. */
4349 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4351 if (! pdpm->printed)
4353 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4354 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4355 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4356 break;
4357 if (pdpm->mod->type == dc->type)
4359 d_print_comp (dpi, options, d_left (dc));
4360 return;
4365 goto modifier;
4367 case DEMANGLE_COMPONENT_REFERENCE:
4368 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4370 /* Handle reference smashing: & + && = &. */
4371 const struct demangle_component *sub = d_left (dc);
4372 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4374 struct demangle_component *a;
4375 struct d_saved_scope *scope = NULL, *scopes;
4376 int i;
4378 for (i = 0; i < dpi->num_saved_scopes; i++)
4379 if (dpi->saved_scopes[i].container == sub)
4380 scope = &dpi->saved_scopes[i];
4382 if (scope == NULL)
4384 /* This is the first time SUB has been traversed.
4385 We need to capture the current templates so
4386 they can be restored if SUB is reentered as a
4387 substitution. */
4388 ++dpi->num_saved_scopes;
4389 scopes = realloc (dpi->saved_scopes,
4390 sizeof (struct d_saved_scope)
4391 * dpi->num_saved_scopes);
4392 if (scopes == NULL)
4394 d_print_error (dpi);
4395 return;
4398 dpi->saved_scopes = scopes;
4399 scope = dpi->saved_scopes + (dpi->num_saved_scopes - 1);
4401 scope->container = sub;
4402 scope->templates = d_copy_templates (dpi);
4403 if (d_print_saw_error (dpi))
4404 return;
4406 else
4408 /* This traversal is reentering SUB as a substition.
4409 Restore the original templates temporarily. */
4410 saved_templates = dpi->templates;
4411 dpi->templates = scope->templates;
4412 need_template_restore = 1;
4415 a = d_lookup_template_argument (dpi, sub);
4416 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4417 a = d_index_template_argument (a, dpi->pack_index);
4419 if (a == NULL)
4421 if (need_template_restore)
4422 dpi->templates = saved_templates;
4424 d_print_error (dpi);
4425 return;
4428 sub = a;
4431 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4432 || sub->type == dc->type)
4433 dc = sub;
4434 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4435 mod_inner = d_left (sub);
4437 /* Fall through. */
4439 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4440 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4441 case DEMANGLE_COMPONENT_CONST_THIS:
4442 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4443 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4444 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4445 case DEMANGLE_COMPONENT_POINTER:
4446 case DEMANGLE_COMPONENT_COMPLEX:
4447 case DEMANGLE_COMPONENT_IMAGINARY:
4448 modifier:
4450 /* We keep a list of modifiers on the stack. */
4451 struct d_print_mod dpm;
4453 dpm.next = dpi->modifiers;
4454 dpi->modifiers = &dpm;
4455 dpm.mod = dc;
4456 dpm.printed = 0;
4457 dpm.templates = dpi->templates;
4459 if (!mod_inner)
4460 mod_inner = d_left (dc);
4462 d_print_comp (dpi, options, mod_inner);
4464 /* If the modifier didn't get printed by the type, print it
4465 now. */
4466 if (! dpm.printed)
4467 d_print_mod (dpi, options, dc);
4469 dpi->modifiers = dpm.next;
4471 if (need_template_restore)
4472 dpi->templates = saved_templates;
4474 return;
4477 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4478 if ((options & DMGL_JAVA) == 0)
4479 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4480 dc->u.s_builtin.type->len);
4481 else
4482 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4483 dc->u.s_builtin.type->java_len);
4484 return;
4486 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4487 d_print_comp (dpi, options, d_left (dc));
4488 return;
4490 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4492 if ((options & DMGL_RET_POSTFIX) != 0)
4493 d_print_function_type (dpi,
4494 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4495 dc, dpi->modifiers);
4497 /* Print return type if present */
4498 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4499 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4500 d_left (dc));
4501 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4503 struct d_print_mod dpm;
4505 /* We must pass this type down as a modifier in order to
4506 print it in the right location. */
4507 dpm.next = dpi->modifiers;
4508 dpi->modifiers = &dpm;
4509 dpm.mod = dc;
4510 dpm.printed = 0;
4511 dpm.templates = dpi->templates;
4513 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4514 d_left (dc));
4516 dpi->modifiers = dpm.next;
4518 if (dpm.printed)
4519 return;
4521 /* In standard prefix notation, there is a space between the
4522 return type and the function signature. */
4523 if ((options & DMGL_RET_POSTFIX) == 0)
4524 d_append_char (dpi, ' ');
4527 if ((options & DMGL_RET_POSTFIX) == 0)
4528 d_print_function_type (dpi,
4529 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4530 dc, dpi->modifiers);
4532 return;
4535 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4537 struct d_print_mod *hold_modifiers;
4538 struct d_print_mod adpm[4];
4539 unsigned int i;
4540 struct d_print_mod *pdpm;
4542 /* We must pass this type down as a modifier in order to print
4543 multi-dimensional arrays correctly. If the array itself is
4544 CV-qualified, we act as though the element type were
4545 CV-qualified. We do this by copying the modifiers down
4546 rather than fiddling pointers, so that we don't wind up
4547 with a d_print_mod higher on the stack pointing into our
4548 stack frame after we return. */
4550 hold_modifiers = dpi->modifiers;
4552 adpm[0].next = hold_modifiers;
4553 dpi->modifiers = &adpm[0];
4554 adpm[0].mod = dc;
4555 adpm[0].printed = 0;
4556 adpm[0].templates = dpi->templates;
4558 i = 1;
4559 pdpm = hold_modifiers;
4560 while (pdpm != NULL
4561 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4562 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4563 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4565 if (! pdpm->printed)
4567 if (i >= sizeof adpm / sizeof adpm[0])
4569 d_print_error (dpi);
4570 return;
4573 adpm[i] = *pdpm;
4574 adpm[i].next = dpi->modifiers;
4575 dpi->modifiers = &adpm[i];
4576 pdpm->printed = 1;
4577 ++i;
4580 pdpm = pdpm->next;
4583 d_print_comp (dpi, options, d_right (dc));
4585 dpi->modifiers = hold_modifiers;
4587 if (adpm[0].printed)
4588 return;
4590 while (i > 1)
4592 --i;
4593 d_print_mod (dpi, options, adpm[i].mod);
4596 d_print_array_type (dpi, options, dc, dpi->modifiers);
4598 return;
4601 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4602 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4604 struct d_print_mod dpm;
4606 dpm.next = dpi->modifiers;
4607 dpi->modifiers = &dpm;
4608 dpm.mod = dc;
4609 dpm.printed = 0;
4610 dpm.templates = dpi->templates;
4612 d_print_comp (dpi, options, d_right (dc));
4614 /* If the modifier didn't get printed by the type, print it
4615 now. */
4616 if (! dpm.printed)
4617 d_print_mod (dpi, options, dc);
4619 dpi->modifiers = dpm.next;
4621 return;
4624 case DEMANGLE_COMPONENT_FIXED_TYPE:
4625 if (dc->u.s_fixed.sat)
4626 d_append_string (dpi, "_Sat ");
4627 /* Don't print "int _Accum". */
4628 if (dc->u.s_fixed.length->u.s_builtin.type
4629 != &cplus_demangle_builtin_types['i'-'a'])
4631 d_print_comp (dpi, options, dc->u.s_fixed.length);
4632 d_append_char (dpi, ' ');
4634 if (dc->u.s_fixed.accum)
4635 d_append_string (dpi, "_Accum");
4636 else
4637 d_append_string (dpi, "_Fract");
4638 return;
4640 case DEMANGLE_COMPONENT_ARGLIST:
4641 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4642 if (d_left (dc) != NULL)
4643 d_print_comp (dpi, options, d_left (dc));
4644 if (d_right (dc) != NULL)
4646 size_t len;
4647 unsigned long int flush_count;
4648 /* Make sure ", " isn't flushed by d_append_string, otherwise
4649 dpi->len -= 2 wouldn't work. */
4650 if (dpi->len >= sizeof (dpi->buf) - 2)
4651 d_print_flush (dpi);
4652 d_append_string (dpi, ", ");
4653 len = dpi->len;
4654 flush_count = dpi->flush_count;
4655 d_print_comp (dpi, options, d_right (dc));
4656 /* If that didn't print anything (which can happen with empty
4657 template argument packs), remove the comma and space. */
4658 if (dpi->flush_count == flush_count && dpi->len == len)
4659 dpi->len -= 2;
4661 return;
4663 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4665 struct demangle_component *type = d_left (dc);
4666 struct demangle_component *list = d_right (dc);
4668 if (type)
4669 d_print_comp (dpi, options, type);
4670 d_append_char (dpi, '{');
4671 d_print_comp (dpi, options, list);
4672 d_append_char (dpi, '}');
4674 return;
4676 case DEMANGLE_COMPONENT_OPERATOR:
4678 const struct demangle_operator_info *op = dc->u.s_operator.op;
4679 int len = op->len;
4681 d_append_string (dpi, "operator");
4682 /* Add a space before new/delete. */
4683 if (IS_LOWER (op->name[0]))
4684 d_append_char (dpi, ' ');
4685 /* Omit a trailing space. */
4686 if (op->name[len-1] == ' ')
4687 --len;
4688 d_append_buffer (dpi, op->name, len);
4689 return;
4692 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4693 d_append_string (dpi, "operator ");
4694 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4695 return;
4697 case DEMANGLE_COMPONENT_CAST:
4698 d_append_string (dpi, "operator ");
4699 d_print_cast (dpi, options, dc);
4700 return;
4702 case DEMANGLE_COMPONENT_NULLARY:
4703 d_print_expr_op (dpi, options, d_left (dc));
4704 return;
4706 case DEMANGLE_COMPONENT_UNARY:
4708 struct demangle_component *op = d_left (dc);
4709 struct demangle_component *operand = d_right (dc);
4710 const char *code = NULL;
4712 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4714 code = op->u.s_operator.op->code;
4715 if (!strcmp (code, "ad"))
4717 /* Don't print the argument list for the address of a
4718 function. */
4719 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
4720 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
4721 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4722 operand = d_left (operand);
4724 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
4726 /* This indicates a suffix operator. */
4727 operand = d_left (operand);
4728 d_print_subexpr (dpi, options, operand);
4729 d_print_expr_op (dpi, options, op);
4730 return;
4734 if (op->type != DEMANGLE_COMPONENT_CAST)
4735 d_print_expr_op (dpi, options, op);
4736 else
4738 d_append_char (dpi, '(');
4739 d_print_cast (dpi, options, op);
4740 d_append_char (dpi, ')');
4742 if (code && !strcmp (code, "gs"))
4743 /* Avoid parens after '::'. */
4744 d_print_comp (dpi, options, operand);
4745 else if (code && !strcmp (code, "st"))
4746 /* Always print parens for sizeof (type). */
4748 d_append_char (dpi, '(');
4749 d_print_comp (dpi, options, operand);
4750 d_append_char (dpi, ')');
4752 else
4753 d_print_subexpr (dpi, options, operand);
4755 return;
4757 case DEMANGLE_COMPONENT_BINARY:
4758 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4760 d_print_error (dpi);
4761 return;
4764 if (op_is_new_cast (d_left (dc)))
4766 d_print_expr_op (dpi, options, d_left (dc));
4767 d_append_char (dpi, '<');
4768 d_print_comp (dpi, options, d_left (d_right (dc)));
4769 d_append_string (dpi, ">(");
4770 d_print_comp (dpi, options, d_right (d_right (dc)));
4771 d_append_char (dpi, ')');
4772 return;
4775 /* We wrap an expression which uses the greater-than operator in
4776 an extra layer of parens so that it does not get confused
4777 with the '>' which ends the template parameters. */
4778 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4779 && d_left (dc)->u.s_operator.op->len == 1
4780 && d_left (dc)->u.s_operator.op->name[0] == '>')
4781 d_append_char (dpi, '(');
4783 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
4784 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
4786 /* Function call used in an expression should not have printed types
4787 of the function arguments. Values of the function arguments still
4788 get printed below. */
4790 const struct demangle_component *func = d_left (d_right (dc));
4792 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
4793 d_print_error (dpi);
4794 d_print_subexpr (dpi, options, d_left (func));
4796 else
4797 d_print_subexpr (dpi, options, d_left (d_right (dc)));
4798 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4800 d_append_char (dpi, '[');
4801 d_print_comp (dpi, options, d_right (d_right (dc)));
4802 d_append_char (dpi, ']');
4804 else
4806 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4807 d_print_expr_op (dpi, options, d_left (dc));
4808 d_print_subexpr (dpi, options, d_right (d_right (dc)));
4811 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4812 && d_left (dc)->u.s_operator.op->len == 1
4813 && d_left (dc)->u.s_operator.op->name[0] == '>')
4814 d_append_char (dpi, ')');
4816 return;
4818 case DEMANGLE_COMPONENT_BINARY_ARGS:
4819 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4820 d_print_error (dpi);
4821 return;
4823 case DEMANGLE_COMPONENT_TRINARY:
4824 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4825 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4827 d_print_error (dpi);
4828 return;
4831 struct demangle_component *op = d_left (dc);
4832 struct demangle_component *first = d_left (d_right (dc));
4833 struct demangle_component *second = d_left (d_right (d_right (dc)));
4834 struct demangle_component *third = d_right (d_right (d_right (dc)));
4836 if (!strcmp (op->u.s_operator.op->code, "qu"))
4838 d_print_subexpr (dpi, options, first);
4839 d_print_expr_op (dpi, options, op);
4840 d_print_subexpr (dpi, options, second);
4841 d_append_string (dpi, " : ");
4842 d_print_subexpr (dpi, options, third);
4844 else
4846 d_append_string (dpi, "new ");
4847 if (d_left (first) != NULL)
4849 d_print_subexpr (dpi, options, first);
4850 d_append_char (dpi, ' ');
4852 d_print_comp (dpi, options, second);
4853 if (third)
4854 d_print_subexpr (dpi, options, third);
4857 return;
4859 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4860 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4861 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4862 d_print_error (dpi);
4863 return;
4865 case DEMANGLE_COMPONENT_LITERAL:
4866 case DEMANGLE_COMPONENT_LITERAL_NEG:
4868 enum d_builtin_type_print tp;
4870 /* For some builtin types, produce simpler output. */
4871 tp = D_PRINT_DEFAULT;
4872 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4874 tp = d_left (dc)->u.s_builtin.type->print;
4875 switch (tp)
4877 case D_PRINT_INT:
4878 case D_PRINT_UNSIGNED:
4879 case D_PRINT_LONG:
4880 case D_PRINT_UNSIGNED_LONG:
4881 case D_PRINT_LONG_LONG:
4882 case D_PRINT_UNSIGNED_LONG_LONG:
4883 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4885 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4886 d_append_char (dpi, '-');
4887 d_print_comp (dpi, options, d_right (dc));
4888 switch (tp)
4890 default:
4891 break;
4892 case D_PRINT_UNSIGNED:
4893 d_append_char (dpi, 'u');
4894 break;
4895 case D_PRINT_LONG:
4896 d_append_char (dpi, 'l');
4897 break;
4898 case D_PRINT_UNSIGNED_LONG:
4899 d_append_string (dpi, "ul");
4900 break;
4901 case D_PRINT_LONG_LONG:
4902 d_append_string (dpi, "ll");
4903 break;
4904 case D_PRINT_UNSIGNED_LONG_LONG:
4905 d_append_string (dpi, "ull");
4906 break;
4908 return;
4910 break;
4912 case D_PRINT_BOOL:
4913 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4914 && d_right (dc)->u.s_name.len == 1
4915 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4917 switch (d_right (dc)->u.s_name.s[0])
4919 case '0':
4920 d_append_string (dpi, "false");
4921 return;
4922 case '1':
4923 d_append_string (dpi, "true");
4924 return;
4925 default:
4926 break;
4929 break;
4931 default:
4932 break;
4936 d_append_char (dpi, '(');
4937 d_print_comp (dpi, options, d_left (dc));
4938 d_append_char (dpi, ')');
4939 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4940 d_append_char (dpi, '-');
4941 if (tp == D_PRINT_FLOAT)
4942 d_append_char (dpi, '[');
4943 d_print_comp (dpi, options, d_right (dc));
4944 if (tp == D_PRINT_FLOAT)
4945 d_append_char (dpi, ']');
4947 return;
4949 case DEMANGLE_COMPONENT_NUMBER:
4950 d_append_num (dpi, dc->u.s_number.number);
4951 return;
4953 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4954 d_append_string (dpi, "java resource ");
4955 d_print_comp (dpi, options, d_left (dc));
4956 return;
4958 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4959 d_print_comp (dpi, options, d_left (dc));
4960 d_print_comp (dpi, options, d_right (dc));
4961 return;
4963 case DEMANGLE_COMPONENT_CHARACTER:
4964 d_append_char (dpi, dc->u.s_character.character);
4965 return;
4967 case DEMANGLE_COMPONENT_DECLTYPE:
4968 d_append_string (dpi, "decltype (");
4969 d_print_comp (dpi, options, d_left (dc));
4970 d_append_char (dpi, ')');
4971 return;
4973 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4975 int len;
4976 int i;
4977 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4978 if (a == NULL)
4980 /* d_find_pack won't find anything if the only packs involved
4981 in this expansion are function parameter packs; in that
4982 case, just print the pattern and "...". */
4983 d_print_subexpr (dpi, options, d_left (dc));
4984 d_append_string (dpi, "...");
4985 return;
4988 len = d_pack_length (a);
4989 dc = d_left (dc);
4990 for (i = 0; i < len; ++i)
4992 dpi->pack_index = i;
4993 d_print_comp (dpi, options, dc);
4994 if (i < len-1)
4995 d_append_string (dpi, ", ");
4998 return;
5000 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5002 long num = dc->u.s_number.number;
5003 if (num == 0)
5004 d_append_string (dpi, "this");
5005 else
5007 d_append_string (dpi, "{parm#");
5008 d_append_num (dpi, num);
5009 d_append_char (dpi, '}');
5012 return;
5014 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5015 d_append_string (dpi, "global constructors keyed to ");
5016 d_print_comp (dpi, options, dc->u.s_binary.left);
5017 return;
5019 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5020 d_append_string (dpi, "global destructors keyed to ");
5021 d_print_comp (dpi, options, dc->u.s_binary.left);
5022 return;
5024 case DEMANGLE_COMPONENT_LAMBDA:
5025 d_append_string (dpi, "{lambda(");
5026 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5027 d_append_string (dpi, ")#");
5028 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5029 d_append_char (dpi, '}');
5030 return;
5032 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5033 d_append_string (dpi, "{unnamed type#");
5034 d_append_num (dpi, dc->u.s_number.number + 1);
5035 d_append_char (dpi, '}');
5036 return;
5038 case DEMANGLE_COMPONENT_CLONE:
5039 d_print_comp (dpi, options, d_left (dc));
5040 d_append_string (dpi, " [clone ");
5041 d_print_comp (dpi, options, d_right (dc));
5042 d_append_char (dpi, ']');
5043 return;
5045 default:
5046 d_print_error (dpi);
5047 return;
5051 /* Print a Java dentifier. For Java we try to handle encoded extended
5052 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5053 so we don't it for C++. Characters are encoded as
5054 __U<hex-char>+_. */
5056 static void
5057 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5059 const char *p;
5060 const char *end;
5062 end = name + len;
5063 for (p = name; p < end; ++p)
5065 if (end - p > 3
5066 && p[0] == '_'
5067 && p[1] == '_'
5068 && p[2] == 'U')
5070 unsigned long c;
5071 const char *q;
5073 c = 0;
5074 for (q = p + 3; q < end; ++q)
5076 int dig;
5078 if (IS_DIGIT (*q))
5079 dig = *q - '0';
5080 else if (*q >= 'A' && *q <= 'F')
5081 dig = *q - 'A' + 10;
5082 else if (*q >= 'a' && *q <= 'f')
5083 dig = *q - 'a' + 10;
5084 else
5085 break;
5087 c = c * 16 + dig;
5089 /* If the Unicode character is larger than 256, we don't try
5090 to deal with it here. FIXME. */
5091 if (q < end && *q == '_' && c < 256)
5093 d_append_char (dpi, c);
5094 p = q;
5095 continue;
5099 d_append_char (dpi, *p);
5103 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5104 qualifiers on this after printing a function. */
5106 static void
5107 d_print_mod_list (struct d_print_info *dpi, int options,
5108 struct d_print_mod *mods, int suffix)
5110 struct d_print_template *hold_dpt;
5112 if (mods == NULL || d_print_saw_error (dpi))
5113 return;
5115 if (mods->printed
5116 || (! suffix
5117 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5118 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5119 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5120 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5121 || (mods->mod->type
5122 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5124 d_print_mod_list (dpi, options, mods->next, suffix);
5125 return;
5128 mods->printed = 1;
5130 hold_dpt = dpi->templates;
5131 dpi->templates = mods->templates;
5133 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5135 d_print_function_type (dpi, options, mods->mod, mods->next);
5136 dpi->templates = hold_dpt;
5137 return;
5139 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5141 d_print_array_type (dpi, options, mods->mod, mods->next);
5142 dpi->templates = hold_dpt;
5143 return;
5145 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5147 struct d_print_mod *hold_modifiers;
5148 struct demangle_component *dc;
5150 /* When this is on the modifier stack, we have pulled any
5151 qualifiers off the right argument already. Otherwise, we
5152 print it as usual, but don't let the left argument see any
5153 modifiers. */
5155 hold_modifiers = dpi->modifiers;
5156 dpi->modifiers = NULL;
5157 d_print_comp (dpi, options, d_left (mods->mod));
5158 dpi->modifiers = hold_modifiers;
5160 if ((options & DMGL_JAVA) == 0)
5161 d_append_string (dpi, "::");
5162 else
5163 d_append_char (dpi, '.');
5165 dc = d_right (mods->mod);
5167 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5169 d_append_string (dpi, "{default arg#");
5170 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5171 d_append_string (dpi, "}::");
5172 dc = dc->u.s_unary_num.sub;
5175 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5176 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5177 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5178 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5179 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5180 dc = d_left (dc);
5182 d_print_comp (dpi, options, dc);
5184 dpi->templates = hold_dpt;
5185 return;
5188 d_print_mod (dpi, options, mods->mod);
5190 dpi->templates = hold_dpt;
5192 d_print_mod_list (dpi, options, mods->next, suffix);
5195 /* Print a modifier. */
5197 static void
5198 d_print_mod (struct d_print_info *dpi, int options,
5199 const struct demangle_component *mod)
5201 switch (mod->type)
5203 case DEMANGLE_COMPONENT_RESTRICT:
5204 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5205 d_append_string (dpi, " restrict");
5206 return;
5207 case DEMANGLE_COMPONENT_VOLATILE:
5208 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5209 d_append_string (dpi, " volatile");
5210 return;
5211 case DEMANGLE_COMPONENT_CONST:
5212 case DEMANGLE_COMPONENT_CONST_THIS:
5213 d_append_string (dpi, " const");
5214 return;
5215 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5216 d_append_char (dpi, ' ');
5217 d_print_comp (dpi, options, d_right (mod));
5218 return;
5219 case DEMANGLE_COMPONENT_POINTER:
5220 /* There is no pointer symbol in Java. */
5221 if ((options & DMGL_JAVA) == 0)
5222 d_append_char (dpi, '*');
5223 return;
5224 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5225 /* For the ref-qualifier, put a space before the &. */
5226 d_append_char (dpi, ' ');
5227 case DEMANGLE_COMPONENT_REFERENCE:
5228 d_append_char (dpi, '&');
5229 return;
5230 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5231 d_append_char (dpi, ' ');
5232 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5233 d_append_string (dpi, "&&");
5234 return;
5235 case DEMANGLE_COMPONENT_COMPLEX:
5236 d_append_string (dpi, "complex ");
5237 return;
5238 case DEMANGLE_COMPONENT_IMAGINARY:
5239 d_append_string (dpi, "imaginary ");
5240 return;
5241 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5242 if (d_last_char (dpi) != '(')
5243 d_append_char (dpi, ' ');
5244 d_print_comp (dpi, options, d_left (mod));
5245 d_append_string (dpi, "::*");
5246 return;
5247 case DEMANGLE_COMPONENT_TYPED_NAME:
5248 d_print_comp (dpi, options, d_left (mod));
5249 return;
5250 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5251 d_append_string (dpi, " __vector(");
5252 d_print_comp (dpi, options, d_left (mod));
5253 d_append_char (dpi, ')');
5254 return;
5256 default:
5257 /* Otherwise, we have something that won't go back on the
5258 modifier stack, so we can just print it. */
5259 d_print_comp (dpi, options, mod);
5260 return;
5264 /* Print a function type, except for the return type. */
5266 static void
5267 d_print_function_type (struct d_print_info *dpi, int options,
5268 const struct demangle_component *dc,
5269 struct d_print_mod *mods)
5271 int need_paren;
5272 int need_space;
5273 struct d_print_mod *p;
5274 struct d_print_mod *hold_modifiers;
5276 need_paren = 0;
5277 need_space = 0;
5278 for (p = mods; p != NULL; p = p->next)
5280 if (p->printed)
5281 break;
5283 switch (p->mod->type)
5285 case DEMANGLE_COMPONENT_POINTER:
5286 case DEMANGLE_COMPONENT_REFERENCE:
5287 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5288 need_paren = 1;
5289 break;
5290 case DEMANGLE_COMPONENT_RESTRICT:
5291 case DEMANGLE_COMPONENT_VOLATILE:
5292 case DEMANGLE_COMPONENT_CONST:
5293 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5294 case DEMANGLE_COMPONENT_COMPLEX:
5295 case DEMANGLE_COMPONENT_IMAGINARY:
5296 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5297 need_space = 1;
5298 need_paren = 1;
5299 break;
5300 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5301 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5302 case DEMANGLE_COMPONENT_CONST_THIS:
5303 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5304 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5305 break;
5306 default:
5307 break;
5309 if (need_paren)
5310 break;
5313 if (need_paren)
5315 if (! need_space)
5317 if (d_last_char (dpi) != '('
5318 && d_last_char (dpi) != '*')
5319 need_space = 1;
5321 if (need_space && d_last_char (dpi) != ' ')
5322 d_append_char (dpi, ' ');
5323 d_append_char (dpi, '(');
5326 hold_modifiers = dpi->modifiers;
5327 dpi->modifiers = NULL;
5329 d_print_mod_list (dpi, options, mods, 0);
5331 if (need_paren)
5332 d_append_char (dpi, ')');
5334 d_append_char (dpi, '(');
5336 if (d_right (dc) != NULL)
5337 d_print_comp (dpi, options, d_right (dc));
5339 d_append_char (dpi, ')');
5341 d_print_mod_list (dpi, options, mods, 1);
5343 dpi->modifiers = hold_modifiers;
5346 /* Print an array type, except for the element type. */
5348 static void
5349 d_print_array_type (struct d_print_info *dpi, int options,
5350 const struct demangle_component *dc,
5351 struct d_print_mod *mods)
5353 int need_space;
5355 need_space = 1;
5356 if (mods != NULL)
5358 int need_paren;
5359 struct d_print_mod *p;
5361 need_paren = 0;
5362 for (p = mods; p != NULL; p = p->next)
5364 if (! p->printed)
5366 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5368 need_space = 0;
5369 break;
5371 else
5373 need_paren = 1;
5374 need_space = 1;
5375 break;
5380 if (need_paren)
5381 d_append_string (dpi, " (");
5383 d_print_mod_list (dpi, options, mods, 0);
5385 if (need_paren)
5386 d_append_char (dpi, ')');
5389 if (need_space)
5390 d_append_char (dpi, ' ');
5392 d_append_char (dpi, '[');
5394 if (d_left (dc) != NULL)
5395 d_print_comp (dpi, options, d_left (dc));
5397 d_append_char (dpi, ']');
5400 /* Print an operator in an expression. */
5402 static void
5403 d_print_expr_op (struct d_print_info *dpi, int options,
5404 const struct demangle_component *dc)
5406 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5407 d_append_buffer (dpi, dc->u.s_operator.op->name,
5408 dc->u.s_operator.op->len);
5409 else
5410 d_print_comp (dpi, options, dc);
5413 /* Print a cast. */
5415 static void
5416 d_print_cast (struct d_print_info *dpi, int options,
5417 const struct demangle_component *dc)
5419 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5420 d_print_comp (dpi, options, d_left (dc));
5421 else
5423 struct d_print_mod *hold_dpm;
5424 struct d_print_template dpt;
5426 /* It appears that for a templated cast operator, we need to put
5427 the template parameters in scope for the operator name, but
5428 not for the parameters. The effect is that we need to handle
5429 the template printing here. */
5431 hold_dpm = dpi->modifiers;
5432 dpi->modifiers = NULL;
5434 dpt.next = dpi->templates;
5435 dpi->templates = &dpt;
5436 dpt.template_decl = d_left (dc);
5438 d_print_comp (dpi, options, d_left (d_left (dc)));
5440 dpi->templates = dpt.next;
5442 if (d_last_char (dpi) == '<')
5443 d_append_char (dpi, ' ');
5444 d_append_char (dpi, '<');
5445 d_print_comp (dpi, options, d_right (d_left (dc)));
5446 /* Avoid generating two consecutive '>' characters, to avoid
5447 the C++ syntactic ambiguity. */
5448 if (d_last_char (dpi) == '>')
5449 d_append_char (dpi, ' ');
5450 d_append_char (dpi, '>');
5452 dpi->modifiers = hold_dpm;
5456 /* Initialize the information structure we use to pass around
5457 information. */
5459 CP_STATIC_IF_GLIBCPP_V3
5460 void
5461 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5462 struct d_info *di)
5464 di->s = mangled;
5465 di->send = mangled + len;
5466 di->options = options;
5468 di->n = mangled;
5470 /* We can not need more components than twice the number of chars in
5471 the mangled string. Most components correspond directly to
5472 chars, but the ARGLIST types are exceptions. */
5473 di->num_comps = 2 * len;
5474 di->next_comp = 0;
5476 /* Similarly, we can not need more substitutions than there are
5477 chars in the mangled string. */
5478 di->num_subs = len;
5479 di->next_sub = 0;
5480 di->did_subs = 0;
5482 di->last_name = NULL;
5484 di->expansion = 0;
5487 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5488 mangled name, return strings in repeated callback giving the demangled
5489 name. OPTIONS is the usual libiberty demangler options. On success,
5490 this returns 1. On failure, returns 0. */
5492 static int
5493 d_demangle_callback (const char *mangled, int options,
5494 demangle_callbackref callback, void *opaque)
5496 enum
5498 DCT_TYPE,
5499 DCT_MANGLED,
5500 DCT_GLOBAL_CTORS,
5501 DCT_GLOBAL_DTORS
5503 type;
5504 struct d_info di;
5505 struct demangle_component *dc;
5506 int status;
5508 if (mangled[0] == '_' && mangled[1] == 'Z')
5509 type = DCT_MANGLED;
5510 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5511 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5512 && (mangled[9] == 'D' || mangled[9] == 'I')
5513 && mangled[10] == '_')
5514 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5515 else
5517 if ((options & DMGL_TYPES) == 0)
5518 return 0;
5519 type = DCT_TYPE;
5522 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5525 #ifdef CP_DYNAMIC_ARRAYS
5526 __extension__ struct demangle_component comps[di.num_comps];
5527 __extension__ struct demangle_component *subs[di.num_subs];
5529 di.comps = comps;
5530 di.subs = subs;
5531 #else
5532 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5533 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5534 #endif
5536 switch (type)
5538 case DCT_TYPE:
5539 dc = cplus_demangle_type (&di);
5540 break;
5541 case DCT_MANGLED:
5542 dc = cplus_demangle_mangled_name (&di, 1);
5543 break;
5544 case DCT_GLOBAL_CTORS:
5545 case DCT_GLOBAL_DTORS:
5546 d_advance (&di, 11);
5547 dc = d_make_comp (&di,
5548 (type == DCT_GLOBAL_CTORS
5549 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5550 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5551 d_make_demangle_mangled_name (&di, d_str (&di)),
5552 NULL);
5553 d_advance (&di, strlen (d_str (&di)));
5554 break;
5557 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5558 mangled string, then we didn't successfully demangle it. If
5559 DMGL_PARAMS is not set, we didn't look at the trailing
5560 parameters. */
5561 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5562 dc = NULL;
5564 #ifdef CP_DEMANGLE_DEBUG
5565 d_dump (dc, 0);
5566 #endif
5568 status = (dc != NULL)
5569 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5570 : 0;
5573 return status;
5576 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5577 name, return a buffer allocated with malloc holding the demangled
5578 name. OPTIONS is the usual libiberty demangler options. On
5579 success, this sets *PALC to the allocated size of the returned
5580 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5581 a memory allocation failure, and returns NULL. */
5583 static char *
5584 d_demangle (const char *mangled, int options, size_t *palc)
5586 struct d_growable_string dgs;
5587 int status;
5589 d_growable_string_init (&dgs, 0);
5591 status = d_demangle_callback (mangled, options,
5592 d_growable_string_callback_adapter, &dgs);
5593 if (status == 0)
5595 free (dgs.buf);
5596 *palc = 0;
5597 return NULL;
5600 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5601 return dgs.buf;
5604 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5606 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5608 /* ia64 ABI-mandated entry point in the C++ runtime library for
5609 performing demangling. MANGLED_NAME is a NUL-terminated character
5610 string containing the name to be demangled.
5612 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5613 *LENGTH bytes, into which the demangled name is stored. If
5614 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5615 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5616 is placed in a region of memory allocated with malloc.
5618 If LENGTH is non-NULL, the length of the buffer containing the
5619 demangled name, is placed in *LENGTH.
5621 The return value is a pointer to the start of the NUL-terminated
5622 demangled name, or NULL if the demangling fails. The caller is
5623 responsible for deallocating this memory using free.
5625 *STATUS is set to one of the following values:
5626 0: The demangling operation succeeded.
5627 -1: A memory allocation failure occurred.
5628 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5629 -3: One of the arguments is invalid.
5631 The demangling is performed using the C++ ABI mangling rules, with
5632 GNU extensions. */
5634 char *
5635 __cxa_demangle (const char *mangled_name, char *output_buffer,
5636 size_t *length, int *status)
5638 char *demangled;
5639 size_t alc;
5641 if (mangled_name == NULL)
5643 if (status != NULL)
5644 *status = -3;
5645 return NULL;
5648 if (output_buffer != NULL && length == NULL)
5650 if (status != NULL)
5651 *status = -3;
5652 return NULL;
5655 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5657 if (demangled == NULL)
5659 if (status != NULL)
5661 if (alc == 1)
5662 *status = -1;
5663 else
5664 *status = -2;
5666 return NULL;
5669 if (output_buffer == NULL)
5671 if (length != NULL)
5672 *length = alc;
5674 else
5676 if (strlen (demangled) < *length)
5678 strcpy (output_buffer, demangled);
5679 free (demangled);
5680 demangled = output_buffer;
5682 else
5684 free (output_buffer);
5685 *length = alc;
5689 if (status != NULL)
5690 *status = 0;
5692 return demangled;
5695 extern int __gcclibcxx_demangle_callback (const char *,
5696 void (*)
5697 (const char *, size_t, void *),
5698 void *);
5700 /* Alternative, allocationless entry point in the C++ runtime library
5701 for performing demangling. MANGLED_NAME is a NUL-terminated character
5702 string containing the name to be demangled.
5704 CALLBACK is a callback function, called with demangled string
5705 segments as demangling progresses; it is called at least once,
5706 but may be called more than once. OPAQUE is a generalized pointer
5707 used as a callback argument.
5709 The return code is one of the following values, equivalent to
5710 the STATUS values of __cxa_demangle() (excluding -1, since this
5711 function performs no memory allocations):
5712 0: The demangling operation succeeded.
5713 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5714 -3: One of the arguments is invalid.
5716 The demangling is performed using the C++ ABI mangling rules, with
5717 GNU extensions. */
5720 __gcclibcxx_demangle_callback (const char *mangled_name,
5721 void (*callback) (const char *, size_t, void *),
5722 void *opaque)
5724 int status;
5726 if (mangled_name == NULL || callback == NULL)
5727 return -3;
5729 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5730 callback, opaque);
5731 if (status == 0)
5732 return -2;
5734 return 0;
5737 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5739 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5740 mangled name, return a buffer allocated with malloc holding the
5741 demangled name. Otherwise, return NULL. */
5743 char *
5744 cplus_demangle_v3 (const char *mangled, int options)
5746 size_t alc;
5748 return d_demangle (mangled, options, &alc);
5752 cplus_demangle_v3_callback (const char *mangled, int options,
5753 demangle_callbackref callback, void *opaque)
5755 return d_demangle_callback (mangled, options, callback, opaque);
5758 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5759 conventions, but the output formatting is a little different.
5760 This instructs the C++ demangler not to emit pointer characters ("*"), to
5761 use Java's namespace separator symbol ("." instead of "::"), and to output
5762 JArray<TYPE> as TYPE[]. */
5764 char *
5765 java_demangle_v3 (const char *mangled)
5767 size_t alc;
5769 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5773 java_demangle_v3_callback (const char *mangled,
5774 demangle_callbackref callback, void *opaque)
5776 return d_demangle_callback (mangled,
5777 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5778 callback, opaque);
5781 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5783 #ifndef IN_GLIBCPP_V3
5785 /* Demangle a string in order to find out whether it is a constructor
5786 or destructor. Return non-zero on success. Set *CTOR_KIND and
5787 *DTOR_KIND appropriately. */
5789 static int
5790 is_ctor_or_dtor (const char *mangled,
5791 enum gnu_v3_ctor_kinds *ctor_kind,
5792 enum gnu_v3_dtor_kinds *dtor_kind)
5794 struct d_info di;
5795 struct demangle_component *dc;
5796 int ret;
5798 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5799 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5801 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5804 #ifdef CP_DYNAMIC_ARRAYS
5805 __extension__ struct demangle_component comps[di.num_comps];
5806 __extension__ struct demangle_component *subs[di.num_subs];
5808 di.comps = comps;
5809 di.subs = subs;
5810 #else
5811 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5812 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5813 #endif
5815 dc = cplus_demangle_mangled_name (&di, 1);
5817 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5818 to demangle the entire string. */
5820 ret = 0;
5821 while (dc != NULL)
5823 switch (dc->type)
5825 /* These cannot appear on a constructor or destructor. */
5826 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5827 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5828 case DEMANGLE_COMPONENT_CONST_THIS:
5829 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5830 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5831 default:
5832 dc = NULL;
5833 break;
5834 case DEMANGLE_COMPONENT_TYPED_NAME:
5835 case DEMANGLE_COMPONENT_TEMPLATE:
5836 dc = d_left (dc);
5837 break;
5838 case DEMANGLE_COMPONENT_QUAL_NAME:
5839 case DEMANGLE_COMPONENT_LOCAL_NAME:
5840 dc = d_right (dc);
5841 break;
5842 case DEMANGLE_COMPONENT_CTOR:
5843 *ctor_kind = dc->u.s_ctor.kind;
5844 ret = 1;
5845 dc = NULL;
5846 break;
5847 case DEMANGLE_COMPONENT_DTOR:
5848 *dtor_kind = dc->u.s_dtor.kind;
5849 ret = 1;
5850 dc = NULL;
5851 break;
5856 return ret;
5859 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5860 name. A non-zero return indicates the type of constructor. */
5862 enum gnu_v3_ctor_kinds
5863 is_gnu_v3_mangled_ctor (const char *name)
5865 enum gnu_v3_ctor_kinds ctor_kind;
5866 enum gnu_v3_dtor_kinds dtor_kind;
5868 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5869 return (enum gnu_v3_ctor_kinds) 0;
5870 return ctor_kind;
5874 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5875 name. A non-zero return indicates the type of destructor. */
5877 enum gnu_v3_dtor_kinds
5878 is_gnu_v3_mangled_dtor (const char *name)
5880 enum gnu_v3_ctor_kinds ctor_kind;
5881 enum gnu_v3_dtor_kinds dtor_kind;
5883 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5884 return (enum gnu_v3_dtor_kinds) 0;
5885 return dtor_kind;
5888 #endif /* IN_GLIBCPP_V3 */
5890 #ifdef STANDALONE_DEMANGLER
5892 #include "getopt.h"
5893 #include "dyn-string.h"
5895 static void print_usage (FILE* fp, int exit_value);
5897 #define IS_ALPHA(CHAR) \
5898 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5899 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5901 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5902 #define is_mangled_char(CHAR) \
5903 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5904 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5906 /* The name of this program, as invoked. */
5907 const char* program_name;
5909 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5911 static void
5912 print_usage (FILE* fp, int exit_value)
5914 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5915 fprintf (fp, "Options:\n");
5916 fprintf (fp, " -h,--help Display this message.\n");
5917 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5918 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5919 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5921 exit (exit_value);
5924 /* Option specification for getopt_long. */
5925 static const struct option long_options[] =
5927 { "help", no_argument, NULL, 'h' },
5928 { "no-params", no_argument, NULL, 'p' },
5929 { "verbose", no_argument, NULL, 'v' },
5930 { NULL, no_argument, NULL, 0 },
5933 /* Main entry for a demangling filter executable. It will demangle
5934 its command line arguments, if any. If none are provided, it will
5935 filter stdin to stdout, replacing any recognized mangled C++ names
5936 with their demangled equivalents. */
5939 main (int argc, char *argv[])
5941 int i;
5942 int opt_char;
5943 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5945 /* Use the program name of this program, as invoked. */
5946 program_name = argv[0];
5948 /* Parse options. */
5951 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5952 switch (opt_char)
5954 case '?': /* Unrecognized option. */
5955 print_usage (stderr, 1);
5956 break;
5958 case 'h':
5959 print_usage (stdout, 0);
5960 break;
5962 case 'p':
5963 options &= ~ DMGL_PARAMS;
5964 break;
5966 case 'v':
5967 options |= DMGL_VERBOSE;
5968 break;
5971 while (opt_char != -1);
5973 if (optind == argc)
5974 /* No command line arguments were provided. Filter stdin. */
5976 dyn_string_t mangled = dyn_string_new (3);
5977 char *s;
5979 /* Read all of input. */
5980 while (!feof (stdin))
5982 char c;
5984 /* Pile characters into mangled until we hit one that can't
5985 occur in a mangled name. */
5986 c = getchar ();
5987 while (!feof (stdin) && is_mangled_char (c))
5989 dyn_string_append_char (mangled, c);
5990 if (feof (stdin))
5991 break;
5992 c = getchar ();
5995 if (dyn_string_length (mangled) > 0)
5997 #ifdef IN_GLIBCPP_V3
5998 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5999 #else
6000 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6001 #endif
6003 if (s != NULL)
6005 fputs (s, stdout);
6006 free (s);
6008 else
6010 /* It might not have been a mangled name. Print the
6011 original text. */
6012 fputs (dyn_string_buf (mangled), stdout);
6015 dyn_string_clear (mangled);
6018 /* If we haven't hit EOF yet, we've read one character that
6019 can't occur in a mangled name, so print it out. */
6020 if (!feof (stdin))
6021 putchar (c);
6024 dyn_string_delete (mangled);
6026 else
6027 /* Demangle command line arguments. */
6029 /* Loop over command line arguments. */
6030 for (i = optind; i < argc; ++i)
6032 char *s;
6033 #ifdef IN_GLIBCPP_V3
6034 int status;
6035 #endif
6037 /* Attempt to demangle. */
6038 #ifdef IN_GLIBCPP_V3
6039 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6040 #else
6041 s = cplus_demangle_v3 (argv[i], options);
6042 #endif
6044 /* If it worked, print the demangled name. */
6045 if (s != NULL)
6047 printf ("%s\n", s);
6048 free (s);
6050 else
6052 #ifdef IN_GLIBCPP_V3
6053 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6054 #else
6055 fprintf (stderr, "Failed: %s\n", argv[i]);
6056 #endif
6061 return 0;
6064 #endif /* STANDALONE_DEMANGLER */