Update contributors list for update of alpha-vms bfd.
[binutils.git] / libiberty / cp-demangle.c
blob0b4e05c6e5c044b1e655aaafc62ad47ed928cdf7
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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 enum { D_PRINT_BUFFER_LENGTH = 256 };
279 struct d_print_info
281 /* The options passed to the demangler. */
282 int options;
283 /* Fixed-length allocated buffer for demangled data, flushed to the
284 callback with a NUL termination once full. */
285 char buf[D_PRINT_BUFFER_LENGTH];
286 /* Current length of data in buffer. */
287 size_t len;
288 /* The last character printed, saved individually so that it survives
289 any buffer flush. */
290 char last_char;
291 /* Callback function to handle demangled buffer flush. */
292 demangle_callbackref callback;
293 /* Opaque callback argument. */
294 void *opaque;
295 /* The current list of templates, if any. */
296 struct d_print_template *templates;
297 /* The current list of modifiers (e.g., pointer, reference, etc.),
298 if any. */
299 struct d_print_mod *modifiers;
300 /* Set to 1 if we saw a demangling error. */
301 int demangle_failure;
302 /* The current index into any template argument packs we are using
303 for printing. */
304 int pack_index;
307 #ifdef CP_DEMANGLE_DEBUG
308 static void d_dump (struct demangle_component *, int);
309 #endif
311 static struct demangle_component *
312 d_make_empty (struct d_info *);
314 static struct demangle_component *
315 d_make_comp (struct d_info *, enum demangle_component_type,
316 struct demangle_component *,
317 struct demangle_component *);
319 static struct demangle_component *
320 d_make_name (struct d_info *, const char *, int);
322 static struct demangle_component *
323 d_make_builtin_type (struct d_info *,
324 const struct demangle_builtin_type_info *);
326 static struct demangle_component *
327 d_make_operator (struct d_info *,
328 const struct demangle_operator_info *);
330 static struct demangle_component *
331 d_make_extended_operator (struct d_info *, int,
332 struct demangle_component *);
334 static struct demangle_component *
335 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
336 struct demangle_component *);
338 static struct demangle_component *
339 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
340 struct demangle_component *);
342 static struct demangle_component *
343 d_make_template_param (struct d_info *, long);
345 static struct demangle_component *
346 d_make_sub (struct d_info *, const char *, int);
348 static int
349 has_return_type (struct demangle_component *);
351 static int
352 is_ctor_dtor_or_conversion (struct demangle_component *);
354 static struct demangle_component *d_encoding (struct d_info *, int);
356 static struct demangle_component *d_name (struct d_info *);
358 static struct demangle_component *d_nested_name (struct d_info *);
360 static struct demangle_component *d_prefix (struct d_info *);
362 static struct demangle_component *d_unqualified_name (struct d_info *);
364 static struct demangle_component *d_source_name (struct d_info *);
366 static long d_number (struct d_info *);
368 static struct demangle_component *d_identifier (struct d_info *, int);
370 static struct demangle_component *d_operator_name (struct d_info *);
372 static struct demangle_component *d_special_name (struct d_info *);
374 static int d_call_offset (struct d_info *, int);
376 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
378 static struct demangle_component **
379 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
381 static struct demangle_component *
382 d_function_type (struct d_info *);
384 static struct demangle_component *
385 d_bare_function_type (struct d_info *, int);
387 static struct demangle_component *
388 d_class_enum_type (struct d_info *);
390 static struct demangle_component *d_array_type (struct d_info *);
392 static struct demangle_component *
393 d_pointer_to_member_type (struct d_info *);
395 static struct demangle_component *
396 d_template_param (struct d_info *);
398 static struct demangle_component *d_template_args (struct d_info *);
400 static struct demangle_component *
401 d_template_arg (struct d_info *);
403 static struct demangle_component *d_expression (struct d_info *);
405 static struct demangle_component *d_expr_primary (struct d_info *);
407 static struct demangle_component *d_local_name (struct d_info *);
409 static int d_discriminator (struct d_info *);
411 static int
412 d_add_substitution (struct d_info *, struct demangle_component *);
414 static struct demangle_component *d_substitution (struct d_info *, int);
416 static void d_growable_string_init (struct d_growable_string *, size_t);
418 static inline void
419 d_growable_string_resize (struct d_growable_string *, size_t);
421 static inline void
422 d_growable_string_append_buffer (struct d_growable_string *,
423 const char *, size_t);
424 static void
425 d_growable_string_callback_adapter (const char *, size_t, void *);
427 static void
428 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
430 static inline void d_print_error (struct d_print_info *);
432 static inline int d_print_saw_error (struct d_print_info *);
434 static inline void d_print_flush (struct d_print_info *);
436 static inline void d_append_char (struct d_print_info *, char);
438 static inline void d_append_buffer (struct d_print_info *,
439 const char *, size_t);
441 static inline void d_append_string (struct d_print_info *, const char *);
443 static inline char d_last_char (struct d_print_info *);
445 static void
446 d_print_comp (struct d_print_info *, const struct demangle_component *);
448 static void
449 d_print_java_identifier (struct d_print_info *, const char *, int);
451 static void
452 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
454 static void
455 d_print_mod (struct d_print_info *, const struct demangle_component *);
457 static void
458 d_print_function_type (struct d_print_info *,
459 const struct demangle_component *,
460 struct d_print_mod *);
462 static void
463 d_print_array_type (struct d_print_info *,
464 const struct demangle_component *,
465 struct d_print_mod *);
467 static void
468 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
470 static void
471 d_print_cast (struct d_print_info *, const struct demangle_component *);
473 static int d_demangle_callback (const char *, int,
474 demangle_callbackref, void *);
475 static char *d_demangle (const char *, int, size_t *);
477 #ifdef CP_DEMANGLE_DEBUG
479 static void
480 d_dump (struct demangle_component *dc, int indent)
482 int i;
484 if (dc == NULL)
486 if (indent == 0)
487 printf ("failed demangling\n");
488 return;
491 for (i = 0; i < indent; ++i)
492 putchar (' ');
494 switch (dc->type)
496 case DEMANGLE_COMPONENT_NAME:
497 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
498 return;
499 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
500 printf ("template parameter %ld\n", dc->u.s_number.number);
501 return;
502 case DEMANGLE_COMPONENT_CTOR:
503 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
504 d_dump (dc->u.s_ctor.name, indent + 2);
505 return;
506 case DEMANGLE_COMPONENT_DTOR:
507 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
508 d_dump (dc->u.s_dtor.name, indent + 2);
509 return;
510 case DEMANGLE_COMPONENT_SUB_STD:
511 printf ("standard substitution %s\n", dc->u.s_string.string);
512 return;
513 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
514 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
515 return;
516 case DEMANGLE_COMPONENT_OPERATOR:
517 printf ("operator %s\n", dc->u.s_operator.op->name);
518 return;
519 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
520 printf ("extended operator with %d args\n",
521 dc->u.s_extended_operator.args);
522 d_dump (dc->u.s_extended_operator.name, indent + 2);
523 return;
525 case DEMANGLE_COMPONENT_QUAL_NAME:
526 printf ("qualified name\n");
527 break;
528 case DEMANGLE_COMPONENT_LOCAL_NAME:
529 printf ("local name\n");
530 break;
531 case DEMANGLE_COMPONENT_TYPED_NAME:
532 printf ("typed name\n");
533 break;
534 case DEMANGLE_COMPONENT_TEMPLATE:
535 printf ("template\n");
536 break;
537 case DEMANGLE_COMPONENT_VTABLE:
538 printf ("vtable\n");
539 break;
540 case DEMANGLE_COMPONENT_VTT:
541 printf ("VTT\n");
542 break;
543 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
544 printf ("construction vtable\n");
545 break;
546 case DEMANGLE_COMPONENT_TYPEINFO:
547 printf ("typeinfo\n");
548 break;
549 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
550 printf ("typeinfo name\n");
551 break;
552 case DEMANGLE_COMPONENT_TYPEINFO_FN:
553 printf ("typeinfo function\n");
554 break;
555 case DEMANGLE_COMPONENT_THUNK:
556 printf ("thunk\n");
557 break;
558 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
559 printf ("virtual thunk\n");
560 break;
561 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
562 printf ("covariant thunk\n");
563 break;
564 case DEMANGLE_COMPONENT_JAVA_CLASS:
565 printf ("java class\n");
566 break;
567 case DEMANGLE_COMPONENT_GUARD:
568 printf ("guard\n");
569 break;
570 case DEMANGLE_COMPONENT_REFTEMP:
571 printf ("reference temporary\n");
572 break;
573 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
574 printf ("hidden alias\n");
575 break;
576 case DEMANGLE_COMPONENT_RESTRICT:
577 printf ("restrict\n");
578 break;
579 case DEMANGLE_COMPONENT_VOLATILE:
580 printf ("volatile\n");
581 break;
582 case DEMANGLE_COMPONENT_CONST:
583 printf ("const\n");
584 break;
585 case DEMANGLE_COMPONENT_RESTRICT_THIS:
586 printf ("restrict this\n");
587 break;
588 case DEMANGLE_COMPONENT_VOLATILE_THIS:
589 printf ("volatile this\n");
590 break;
591 case DEMANGLE_COMPONENT_CONST_THIS:
592 printf ("const this\n");
593 break;
594 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
595 printf ("vendor type qualifier\n");
596 break;
597 case DEMANGLE_COMPONENT_POINTER:
598 printf ("pointer\n");
599 break;
600 case DEMANGLE_COMPONENT_REFERENCE:
601 printf ("reference\n");
602 break;
603 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
604 printf ("rvalue reference\n");
605 break;
606 case DEMANGLE_COMPONENT_COMPLEX:
607 printf ("complex\n");
608 break;
609 case DEMANGLE_COMPONENT_IMAGINARY:
610 printf ("imaginary\n");
611 break;
612 case DEMANGLE_COMPONENT_VENDOR_TYPE:
613 printf ("vendor type\n");
614 break;
615 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
616 printf ("function type\n");
617 break;
618 case DEMANGLE_COMPONENT_ARRAY_TYPE:
619 printf ("array type\n");
620 break;
621 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
622 printf ("pointer to member type\n");
623 break;
624 case DEMANGLE_COMPONENT_FIXED_TYPE:
625 printf ("fixed-point type\n");
626 break;
627 case DEMANGLE_COMPONENT_ARGLIST:
628 printf ("argument list\n");
629 break;
630 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
631 printf ("template argument list\n");
632 break;
633 case DEMANGLE_COMPONENT_CAST:
634 printf ("cast\n");
635 break;
636 case DEMANGLE_COMPONENT_UNARY:
637 printf ("unary operator\n");
638 break;
639 case DEMANGLE_COMPONENT_BINARY:
640 printf ("binary operator\n");
641 break;
642 case DEMANGLE_COMPONENT_BINARY_ARGS:
643 printf ("binary operator arguments\n");
644 break;
645 case DEMANGLE_COMPONENT_TRINARY:
646 printf ("trinary operator\n");
647 break;
648 case DEMANGLE_COMPONENT_TRINARY_ARG1:
649 printf ("trinary operator arguments 1\n");
650 break;
651 case DEMANGLE_COMPONENT_TRINARY_ARG2:
652 printf ("trinary operator arguments 1\n");
653 break;
654 case DEMANGLE_COMPONENT_LITERAL:
655 printf ("literal\n");
656 break;
657 case DEMANGLE_COMPONENT_LITERAL_NEG:
658 printf ("negative literal\n");
659 break;
660 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
661 printf ("java resource\n");
662 break;
663 case DEMANGLE_COMPONENT_COMPOUND_NAME:
664 printf ("compound name\n");
665 break;
666 case DEMANGLE_COMPONENT_CHARACTER:
667 printf ("character '%c'\n", dc->u.s_character.character);
668 return;
669 case DEMANGLE_COMPONENT_DECLTYPE:
670 printf ("decltype\n");
671 break;
672 case DEMANGLE_COMPONENT_PACK_EXPANSION:
673 printf ("pack expansion\n");
674 break;
677 d_dump (d_left (dc), indent + 2);
678 d_dump (d_right (dc), indent + 2);
681 #endif /* CP_DEMANGLE_DEBUG */
683 /* Fill in a DEMANGLE_COMPONENT_NAME. */
685 CP_STATIC_IF_GLIBCPP_V3
687 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
689 if (p == NULL || s == NULL || len == 0)
690 return 0;
691 p->type = DEMANGLE_COMPONENT_NAME;
692 p->u.s_name.s = s;
693 p->u.s_name.len = len;
694 return 1;
697 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
699 CP_STATIC_IF_GLIBCPP_V3
701 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
702 struct demangle_component *name)
704 if (p == NULL || args < 0 || name == NULL)
705 return 0;
706 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
707 p->u.s_extended_operator.args = args;
708 p->u.s_extended_operator.name = name;
709 return 1;
712 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
714 CP_STATIC_IF_GLIBCPP_V3
716 cplus_demangle_fill_ctor (struct demangle_component *p,
717 enum gnu_v3_ctor_kinds kind,
718 struct demangle_component *name)
720 if (p == NULL
721 || name == NULL
722 || (kind < gnu_v3_complete_object_ctor
723 && kind > gnu_v3_complete_object_allocating_ctor))
724 return 0;
725 p->type = DEMANGLE_COMPONENT_CTOR;
726 p->u.s_ctor.kind = kind;
727 p->u.s_ctor.name = name;
728 return 1;
731 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
733 CP_STATIC_IF_GLIBCPP_V3
735 cplus_demangle_fill_dtor (struct demangle_component *p,
736 enum gnu_v3_dtor_kinds kind,
737 struct demangle_component *name)
739 if (p == NULL
740 || name == NULL
741 || (kind < gnu_v3_deleting_dtor
742 && kind > gnu_v3_base_object_dtor))
743 return 0;
744 p->type = DEMANGLE_COMPONENT_DTOR;
745 p->u.s_dtor.kind = kind;
746 p->u.s_dtor.name = name;
747 return 1;
750 /* Add a new component. */
752 static struct demangle_component *
753 d_make_empty (struct d_info *di)
755 struct demangle_component *p;
757 if (di->next_comp >= di->num_comps)
758 return NULL;
759 p = &di->comps[di->next_comp];
760 ++di->next_comp;
761 return p;
764 /* Add a new generic component. */
766 static struct demangle_component *
767 d_make_comp (struct d_info *di, enum demangle_component_type type,
768 struct demangle_component *left,
769 struct demangle_component *right)
771 struct demangle_component *p;
773 /* We check for errors here. A typical error would be a NULL return
774 from a subroutine. We catch those here, and return NULL
775 upward. */
776 switch (type)
778 /* These types require two parameters. */
779 case DEMANGLE_COMPONENT_QUAL_NAME:
780 case DEMANGLE_COMPONENT_LOCAL_NAME:
781 case DEMANGLE_COMPONENT_TYPED_NAME:
782 case DEMANGLE_COMPONENT_TEMPLATE:
783 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
784 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
785 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
786 case DEMANGLE_COMPONENT_UNARY:
787 case DEMANGLE_COMPONENT_BINARY:
788 case DEMANGLE_COMPONENT_BINARY_ARGS:
789 case DEMANGLE_COMPONENT_TRINARY:
790 case DEMANGLE_COMPONENT_TRINARY_ARG1:
791 case DEMANGLE_COMPONENT_TRINARY_ARG2:
792 case DEMANGLE_COMPONENT_LITERAL:
793 case DEMANGLE_COMPONENT_LITERAL_NEG:
794 case DEMANGLE_COMPONENT_COMPOUND_NAME:
795 if (left == NULL || right == NULL)
796 return NULL;
797 break;
799 /* These types only require one parameter. */
800 case DEMANGLE_COMPONENT_VTABLE:
801 case DEMANGLE_COMPONENT_VTT:
802 case DEMANGLE_COMPONENT_TYPEINFO:
803 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
804 case DEMANGLE_COMPONENT_TYPEINFO_FN:
805 case DEMANGLE_COMPONENT_THUNK:
806 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
807 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
808 case DEMANGLE_COMPONENT_JAVA_CLASS:
809 case DEMANGLE_COMPONENT_GUARD:
810 case DEMANGLE_COMPONENT_REFTEMP:
811 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
812 case DEMANGLE_COMPONENT_POINTER:
813 case DEMANGLE_COMPONENT_REFERENCE:
814 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
815 case DEMANGLE_COMPONENT_COMPLEX:
816 case DEMANGLE_COMPONENT_IMAGINARY:
817 case DEMANGLE_COMPONENT_VENDOR_TYPE:
818 case DEMANGLE_COMPONENT_CAST:
819 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
820 case DEMANGLE_COMPONENT_DECLTYPE:
821 case DEMANGLE_COMPONENT_PACK_EXPANSION:
822 if (left == NULL)
823 return NULL;
824 break;
826 /* This needs a right parameter, but the left parameter can be
827 empty. */
828 case DEMANGLE_COMPONENT_ARRAY_TYPE:
829 if (right == NULL)
830 return NULL;
831 break;
833 /* These are allowed to have no parameters--in some cases they
834 will be filled in later. */
835 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
836 case DEMANGLE_COMPONENT_RESTRICT:
837 case DEMANGLE_COMPONENT_VOLATILE:
838 case DEMANGLE_COMPONENT_CONST:
839 case DEMANGLE_COMPONENT_RESTRICT_THIS:
840 case DEMANGLE_COMPONENT_VOLATILE_THIS:
841 case DEMANGLE_COMPONENT_CONST_THIS:
842 case DEMANGLE_COMPONENT_ARGLIST:
843 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
844 break;
846 /* Other types should not be seen here. */
847 default:
848 return NULL;
851 p = d_make_empty (di);
852 if (p != NULL)
854 p->type = type;
855 p->u.s_binary.left = left;
856 p->u.s_binary.right = right;
858 return p;
861 /* Add a new name component. */
863 static struct demangle_component *
864 d_make_name (struct d_info *di, const char *s, int len)
866 struct demangle_component *p;
868 p = d_make_empty (di);
869 if (! cplus_demangle_fill_name (p, s, len))
870 return NULL;
871 return p;
874 /* Add a new builtin type component. */
876 static struct demangle_component *
877 d_make_builtin_type (struct d_info *di,
878 const struct demangle_builtin_type_info *type)
880 struct demangle_component *p;
882 if (type == NULL)
883 return NULL;
884 p = d_make_empty (di);
885 if (p != NULL)
887 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
888 p->u.s_builtin.type = type;
890 return p;
893 /* Add a new operator component. */
895 static struct demangle_component *
896 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
898 struct demangle_component *p;
900 p = d_make_empty (di);
901 if (p != NULL)
903 p->type = DEMANGLE_COMPONENT_OPERATOR;
904 p->u.s_operator.op = op;
906 return p;
909 /* Add a new extended operator component. */
911 static struct demangle_component *
912 d_make_extended_operator (struct d_info *di, int args,
913 struct demangle_component *name)
915 struct demangle_component *p;
917 p = d_make_empty (di);
918 if (! cplus_demangle_fill_extended_operator (p, args, name))
919 return NULL;
920 return p;
923 /* Add a new constructor component. */
925 static struct demangle_component *
926 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
927 struct demangle_component *name)
929 struct demangle_component *p;
931 p = d_make_empty (di);
932 if (! cplus_demangle_fill_ctor (p, kind, name))
933 return NULL;
934 return p;
937 /* Add a new destructor component. */
939 static struct demangle_component *
940 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
941 struct demangle_component *name)
943 struct demangle_component *p;
945 p = d_make_empty (di);
946 if (! cplus_demangle_fill_dtor (p, kind, name))
947 return NULL;
948 return p;
951 /* Add a new template parameter. */
953 static struct demangle_component *
954 d_make_template_param (struct d_info *di, long i)
956 struct demangle_component *p;
958 p = d_make_empty (di);
959 if (p != NULL)
961 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
962 p->u.s_number.number = i;
964 return p;
967 /* Add a new standard substitution component. */
969 static struct demangle_component *
970 d_make_sub (struct d_info *di, const char *name, int len)
972 struct demangle_component *p;
974 p = d_make_empty (di);
975 if (p != NULL)
977 p->type = DEMANGLE_COMPONENT_SUB_STD;
978 p->u.s_string.string = name;
979 p->u.s_string.len = len;
981 return p;
984 /* <mangled-name> ::= _Z <encoding>
986 TOP_LEVEL is non-zero when called at the top level. */
988 CP_STATIC_IF_GLIBCPP_V3
989 struct demangle_component *
990 cplus_demangle_mangled_name (struct d_info *di, int top_level)
992 if (! d_check_char (di, '_'))
993 return NULL;
994 if (! d_check_char (di, 'Z'))
995 return NULL;
996 return d_encoding (di, top_level);
999 /* Return whether a function should have a return type. The argument
1000 is the function name, which may be qualified in various ways. The
1001 rules are that template functions have return types with some
1002 exceptions, function types which are not part of a function name
1003 mangling have return types with some exceptions, and non-template
1004 function names do not have return types. The exceptions are that
1005 constructors, destructors, and conversion operators do not have
1006 return types. */
1008 static int
1009 has_return_type (struct demangle_component *dc)
1011 if (dc == NULL)
1012 return 0;
1013 switch (dc->type)
1015 default:
1016 return 0;
1017 case DEMANGLE_COMPONENT_TEMPLATE:
1018 return ! is_ctor_dtor_or_conversion (d_left (dc));
1019 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1020 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1021 case DEMANGLE_COMPONENT_CONST_THIS:
1022 return has_return_type (d_left (dc));
1026 /* Return whether a name is a constructor, a destructor, or a
1027 conversion operator. */
1029 static int
1030 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1032 if (dc == NULL)
1033 return 0;
1034 switch (dc->type)
1036 default:
1037 return 0;
1038 case DEMANGLE_COMPONENT_QUAL_NAME:
1039 case DEMANGLE_COMPONENT_LOCAL_NAME:
1040 return is_ctor_dtor_or_conversion (d_right (dc));
1041 case DEMANGLE_COMPONENT_CTOR:
1042 case DEMANGLE_COMPONENT_DTOR:
1043 case DEMANGLE_COMPONENT_CAST:
1044 return 1;
1048 /* <encoding> ::= <(function) name> <bare-function-type>
1049 ::= <(data) name>
1050 ::= <special-name>
1052 TOP_LEVEL is non-zero when called at the top level, in which case
1053 if DMGL_PARAMS is not set we do not demangle the function
1054 parameters. We only set this at the top level, because otherwise
1055 we would not correctly demangle names in local scopes. */
1057 static struct demangle_component *
1058 d_encoding (struct d_info *di, int top_level)
1060 char peek = d_peek_char (di);
1062 if (peek == 'G' || peek == 'T')
1063 return d_special_name (di);
1064 else
1066 struct demangle_component *dc;
1068 dc = d_name (di);
1070 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1072 /* Strip off any initial CV-qualifiers, as they really apply
1073 to the `this' parameter, and they were not output by the
1074 v2 demangler without DMGL_PARAMS. */
1075 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1076 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1077 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1078 dc = d_left (dc);
1080 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1081 there may be CV-qualifiers on its right argument which
1082 really apply here; this happens when parsing a class
1083 which is local to a function. */
1084 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1086 struct demangle_component *dcr;
1088 dcr = d_right (dc);
1089 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1090 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1091 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1092 dcr = d_left (dcr);
1093 dc->u.s_binary.right = dcr;
1096 return dc;
1099 peek = d_peek_char (di);
1100 if (dc == NULL || peek == '\0' || peek == 'E')
1101 return dc;
1102 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1103 d_bare_function_type (di, has_return_type (dc)));
1107 /* <name> ::= <nested-name>
1108 ::= <unscoped-name>
1109 ::= <unscoped-template-name> <template-args>
1110 ::= <local-name>
1112 <unscoped-name> ::= <unqualified-name>
1113 ::= St <unqualified-name>
1115 <unscoped-template-name> ::= <unscoped-name>
1116 ::= <substitution>
1119 static struct demangle_component *
1120 d_name (struct d_info *di)
1122 char peek = d_peek_char (di);
1123 struct demangle_component *dc;
1125 switch (peek)
1127 case 'N':
1128 return d_nested_name (di);
1130 case 'Z':
1131 return d_local_name (di);
1133 case 'L':
1134 return d_unqualified_name (di);
1136 case 'S':
1138 int subst;
1140 if (d_peek_next_char (di) != 't')
1142 dc = d_substitution (di, 0);
1143 subst = 1;
1145 else
1147 d_advance (di, 2);
1148 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1149 d_make_name (di, "std", 3),
1150 d_unqualified_name (di));
1151 di->expansion += 3;
1152 subst = 0;
1155 if (d_peek_char (di) != 'I')
1157 /* The grammar does not permit this case to occur if we
1158 called d_substitution() above (i.e., subst == 1). We
1159 don't bother to check. */
1161 else
1163 /* This is <template-args>, which means that we just saw
1164 <unscoped-template-name>, which is a substitution
1165 candidate if we didn't just get it from a
1166 substitution. */
1167 if (! subst)
1169 if (! d_add_substitution (di, dc))
1170 return NULL;
1172 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1173 d_template_args (di));
1176 return dc;
1179 default:
1180 dc = d_unqualified_name (di);
1181 if (d_peek_char (di) == 'I')
1183 /* This is <template-args>, which means that we just saw
1184 <unscoped-template-name>, which is a substitution
1185 candidate. */
1186 if (! d_add_substitution (di, dc))
1187 return NULL;
1188 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1189 d_template_args (di));
1191 return dc;
1195 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1196 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1199 static struct demangle_component *
1200 d_nested_name (struct d_info *di)
1202 struct demangle_component *ret;
1203 struct demangle_component **pret;
1205 if (! d_check_char (di, 'N'))
1206 return NULL;
1208 pret = d_cv_qualifiers (di, &ret, 1);
1209 if (pret == NULL)
1210 return NULL;
1212 *pret = d_prefix (di);
1213 if (*pret == NULL)
1214 return NULL;
1216 if (! d_check_char (di, 'E'))
1217 return NULL;
1219 return ret;
1222 /* <prefix> ::= <prefix> <unqualified-name>
1223 ::= <template-prefix> <template-args>
1224 ::= <template-param>
1226 ::= <substitution>
1228 <template-prefix> ::= <prefix> <(template) unqualified-name>
1229 ::= <template-param>
1230 ::= <substitution>
1233 static struct demangle_component *
1234 d_prefix (struct d_info *di)
1236 struct demangle_component *ret = NULL;
1238 while (1)
1240 char peek;
1241 enum demangle_component_type comb_type;
1242 struct demangle_component *dc;
1244 peek = d_peek_char (di);
1245 if (peek == '\0')
1246 return NULL;
1248 /* The older code accepts a <local-name> here, but I don't see
1249 that in the grammar. The older code does not accept a
1250 <template-param> here. */
1252 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1253 if (IS_DIGIT (peek)
1254 || IS_LOWER (peek)
1255 || peek == 'C'
1256 || peek == 'D'
1257 || peek == 'L')
1258 dc = d_unqualified_name (di);
1259 else if (peek == 'S')
1260 dc = d_substitution (di, 1);
1261 else if (peek == 'I')
1263 if (ret == NULL)
1264 return NULL;
1265 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1266 dc = d_template_args (di);
1268 else if (peek == 'T')
1269 dc = d_template_param (di);
1270 else if (peek == 'E')
1271 return ret;
1272 else
1273 return NULL;
1275 if (ret == NULL)
1276 ret = dc;
1277 else
1278 ret = d_make_comp (di, comb_type, ret, dc);
1280 if (peek != 'S' && d_peek_char (di) != 'E')
1282 if (! d_add_substitution (di, ret))
1283 return NULL;
1288 /* <unqualified-name> ::= <operator-name>
1289 ::= <ctor-dtor-name>
1290 ::= <source-name>
1291 ::= <local-source-name>
1293 <local-source-name> ::= L <source-name> <discriminator>
1296 static struct demangle_component *
1297 d_unqualified_name (struct d_info *di)
1299 char peek;
1301 peek = d_peek_char (di);
1302 if (IS_DIGIT (peek))
1303 return d_source_name (di);
1304 else if (IS_LOWER (peek))
1306 struct demangle_component *ret;
1308 ret = d_operator_name (di);
1309 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1310 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1311 return ret;
1313 else if (peek == 'C' || peek == 'D')
1314 return d_ctor_dtor_name (di);
1315 else if (peek == 'L')
1317 struct demangle_component * ret;
1319 d_advance (di, 1);
1321 ret = d_source_name (di);
1322 if (ret == NULL)
1323 return NULL;
1324 if (! d_discriminator (di))
1325 return NULL;
1326 return ret;
1328 else
1329 return NULL;
1332 /* <source-name> ::= <(positive length) number> <identifier> */
1334 static struct demangle_component *
1335 d_source_name (struct d_info *di)
1337 long len;
1338 struct demangle_component *ret;
1340 len = d_number (di);
1341 if (len <= 0)
1342 return NULL;
1343 ret = d_identifier (di, len);
1344 di->last_name = ret;
1345 return ret;
1348 /* number ::= [n] <(non-negative decimal integer)> */
1350 static long
1351 d_number (struct d_info *di)
1353 int negative;
1354 char peek;
1355 long ret;
1357 negative = 0;
1358 peek = d_peek_char (di);
1359 if (peek == 'n')
1361 negative = 1;
1362 d_advance (di, 1);
1363 peek = d_peek_char (di);
1366 ret = 0;
1367 while (1)
1369 if (! IS_DIGIT (peek))
1371 if (negative)
1372 ret = - ret;
1373 return ret;
1375 ret = ret * 10 + peek - '0';
1376 d_advance (di, 1);
1377 peek = d_peek_char (di);
1381 /* identifier ::= <(unqualified source code identifier)> */
1383 static struct demangle_component *
1384 d_identifier (struct d_info *di, int len)
1386 const char *name;
1388 name = d_str (di);
1390 if (di->send - name < len)
1391 return NULL;
1393 d_advance (di, len);
1395 /* A Java mangled name may have a trailing '$' if it is a C++
1396 keyword. This '$' is not included in the length count. We just
1397 ignore the '$'. */
1398 if ((di->options & DMGL_JAVA) != 0
1399 && d_peek_char (di) == '$')
1400 d_advance (di, 1);
1402 /* Look for something which looks like a gcc encoding of an
1403 anonymous namespace, and replace it with a more user friendly
1404 name. */
1405 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1406 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1407 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1409 const char *s;
1411 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1412 if ((*s == '.' || *s == '_' || *s == '$')
1413 && s[1] == 'N')
1415 di->expansion -= len - sizeof "(anonymous namespace)";
1416 return d_make_name (di, "(anonymous namespace)",
1417 sizeof "(anonymous namespace)" - 1);
1421 return d_make_name (di, name, len);
1424 /* operator_name ::= many different two character encodings.
1425 ::= cv <type>
1426 ::= v <digit> <source-name>
1429 #define NL(s) s, (sizeof s) - 1
1431 CP_STATIC_IF_GLIBCPP_V3
1432 const struct demangle_operator_info cplus_demangle_operators[] =
1434 { "aN", NL ("&="), 2 },
1435 { "aS", NL ("="), 2 },
1436 { "aa", NL ("&&"), 2 },
1437 { "ad", NL ("&"), 1 },
1438 { "an", NL ("&"), 2 },
1439 { "cl", NL ("()"), 2 },
1440 { "cm", NL (","), 2 },
1441 { "co", NL ("~"), 1 },
1442 { "dV", NL ("/="), 2 },
1443 { "da", NL ("delete[]"), 1 },
1444 { "de", NL ("*"), 1 },
1445 { "dl", NL ("delete"), 1 },
1446 { "dt", NL ("."), 2 },
1447 { "dv", NL ("/"), 2 },
1448 { "eO", NL ("^="), 2 },
1449 { "eo", NL ("^"), 2 },
1450 { "eq", NL ("=="), 2 },
1451 { "ge", NL (">="), 2 },
1452 { "gt", NL (">"), 2 },
1453 { "ix", NL ("[]"), 2 },
1454 { "lS", NL ("<<="), 2 },
1455 { "le", NL ("<="), 2 },
1456 { "ls", NL ("<<"), 2 },
1457 { "lt", NL ("<"), 2 },
1458 { "mI", NL ("-="), 2 },
1459 { "mL", NL ("*="), 2 },
1460 { "mi", NL ("-"), 2 },
1461 { "ml", NL ("*"), 2 },
1462 { "mm", NL ("--"), 1 },
1463 { "na", NL ("new[]"), 1 },
1464 { "ne", NL ("!="), 2 },
1465 { "ng", NL ("-"), 1 },
1466 { "nt", NL ("!"), 1 },
1467 { "nw", NL ("new"), 1 },
1468 { "oR", NL ("|="), 2 },
1469 { "oo", NL ("||"), 2 },
1470 { "or", NL ("|"), 2 },
1471 { "pL", NL ("+="), 2 },
1472 { "pl", NL ("+"), 2 },
1473 { "pm", NL ("->*"), 2 },
1474 { "pp", NL ("++"), 1 },
1475 { "ps", NL ("+"), 1 },
1476 { "pt", NL ("->"), 2 },
1477 { "qu", NL ("?"), 3 },
1478 { "rM", NL ("%="), 2 },
1479 { "rS", NL (">>="), 2 },
1480 { "rm", NL ("%"), 2 },
1481 { "rs", NL (">>"), 2 },
1482 { "st", NL ("sizeof "), 1 },
1483 { "sz", NL ("sizeof "), 1 },
1484 { NULL, NULL, 0, 0 }
1487 static struct demangle_component *
1488 d_operator_name (struct d_info *di)
1490 char c1;
1491 char c2;
1493 c1 = d_next_char (di);
1494 c2 = d_next_char (di);
1495 if (c1 == 'v' && IS_DIGIT (c2))
1496 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1497 else if (c1 == 'c' && c2 == 'v')
1498 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1499 cplus_demangle_type (di), NULL);
1500 else
1502 /* LOW is the inclusive lower bound. */
1503 int low = 0;
1504 /* HIGH is the exclusive upper bound. We subtract one to ignore
1505 the sentinel at the end of the array. */
1506 int high = ((sizeof (cplus_demangle_operators)
1507 / sizeof (cplus_demangle_operators[0]))
1508 - 1);
1510 while (1)
1512 int i;
1513 const struct demangle_operator_info *p;
1515 i = low + (high - low) / 2;
1516 p = cplus_demangle_operators + i;
1518 if (c1 == p->code[0] && c2 == p->code[1])
1519 return d_make_operator (di, p);
1521 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1522 high = i;
1523 else
1524 low = i + 1;
1525 if (low == high)
1526 return NULL;
1531 static struct demangle_component *
1532 d_make_character (struct d_info *di, int c)
1534 struct demangle_component *p;
1535 p = d_make_empty (di);
1536 if (p != NULL)
1538 p->type = DEMANGLE_COMPONENT_CHARACTER;
1539 p->u.s_character.character = c;
1541 return p;
1544 static struct demangle_component *
1545 d_java_resource (struct d_info *di)
1547 struct demangle_component *p = NULL;
1548 struct demangle_component *next = NULL;
1549 long len, i;
1550 char c;
1551 const char *str;
1553 len = d_number (di);
1554 if (len <= 1)
1555 return NULL;
1557 /* Eat the leading '_'. */
1558 if (d_next_char (di) != '_')
1559 return NULL;
1560 len--;
1562 str = d_str (di);
1563 i = 0;
1565 while (len > 0)
1567 c = str[i];
1568 if (!c)
1569 return NULL;
1571 /* Each chunk is either a '$' escape... */
1572 if (c == '$')
1574 i++;
1575 switch (str[i++])
1577 case 'S':
1578 c = '/';
1579 break;
1580 case '_':
1581 c = '.';
1582 break;
1583 case '$':
1584 c = '$';
1585 break;
1586 default:
1587 return NULL;
1589 next = d_make_character (di, c);
1590 d_advance (di, i);
1591 str = d_str (di);
1592 len -= i;
1593 i = 0;
1594 if (next == NULL)
1595 return NULL;
1597 /* ... or a sequence of characters. */
1598 else
1600 while (i < len && str[i] && str[i] != '$')
1601 i++;
1603 next = d_make_name (di, str, i);
1604 d_advance (di, i);
1605 str = d_str (di);
1606 len -= i;
1607 i = 0;
1608 if (next == NULL)
1609 return NULL;
1612 if (p == NULL)
1613 p = next;
1614 else
1616 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1617 if (p == NULL)
1618 return NULL;
1622 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1624 return p;
1627 /* <special-name> ::= TV <type>
1628 ::= TT <type>
1629 ::= TI <type>
1630 ::= TS <type>
1631 ::= GV <(object) name>
1632 ::= T <call-offset> <(base) encoding>
1633 ::= Tc <call-offset> <call-offset> <(base) encoding>
1634 Also g++ extensions:
1635 ::= TC <type> <(offset) number> _ <(base) type>
1636 ::= TF <type>
1637 ::= TJ <type>
1638 ::= GR <name>
1639 ::= GA <encoding>
1640 ::= Gr <resource name>
1643 static struct demangle_component *
1644 d_special_name (struct d_info *di)
1646 di->expansion += 20;
1647 if (d_check_char (di, 'T'))
1649 switch (d_next_char (di))
1651 case 'V':
1652 di->expansion -= 5;
1653 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1654 cplus_demangle_type (di), NULL);
1655 case 'T':
1656 di->expansion -= 10;
1657 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1658 cplus_demangle_type (di), NULL);
1659 case 'I':
1660 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1661 cplus_demangle_type (di), NULL);
1662 case 'S':
1663 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1664 cplus_demangle_type (di), NULL);
1666 case 'h':
1667 if (! d_call_offset (di, 'h'))
1668 return NULL;
1669 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1670 d_encoding (di, 0), NULL);
1672 case 'v':
1673 if (! d_call_offset (di, 'v'))
1674 return NULL;
1675 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1676 d_encoding (di, 0), NULL);
1678 case 'c':
1679 if (! d_call_offset (di, '\0'))
1680 return NULL;
1681 if (! d_call_offset (di, '\0'))
1682 return NULL;
1683 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1684 d_encoding (di, 0), NULL);
1686 case 'C':
1688 struct demangle_component *derived_type;
1689 long offset;
1690 struct demangle_component *base_type;
1692 derived_type = cplus_demangle_type (di);
1693 offset = d_number (di);
1694 if (offset < 0)
1695 return NULL;
1696 if (! d_check_char (di, '_'))
1697 return NULL;
1698 base_type = cplus_demangle_type (di);
1699 /* We don't display the offset. FIXME: We should display
1700 it in verbose mode. */
1701 di->expansion += 5;
1702 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1703 base_type, derived_type);
1706 case 'F':
1707 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1708 cplus_demangle_type (di), NULL);
1709 case 'J':
1710 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1711 cplus_demangle_type (di), NULL);
1713 default:
1714 return NULL;
1717 else if (d_check_char (di, 'G'))
1719 switch (d_next_char (di))
1721 case 'V':
1722 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1724 case 'R':
1725 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1726 NULL);
1728 case 'A':
1729 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1730 d_encoding (di, 0), NULL);
1732 case 'r':
1733 return d_java_resource (di);
1735 default:
1736 return NULL;
1739 else
1740 return NULL;
1743 /* <call-offset> ::= h <nv-offset> _
1744 ::= v <v-offset> _
1746 <nv-offset> ::= <(offset) number>
1748 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1750 The C parameter, if not '\0', is a character we just read which is
1751 the start of the <call-offset>.
1753 We don't display the offset information anywhere. FIXME: We should
1754 display it in verbose mode. */
1756 static int
1757 d_call_offset (struct d_info *di, int c)
1759 if (c == '\0')
1760 c = d_next_char (di);
1762 if (c == 'h')
1763 d_number (di);
1764 else if (c == 'v')
1766 d_number (di);
1767 if (! d_check_char (di, '_'))
1768 return 0;
1769 d_number (di);
1771 else
1772 return 0;
1774 if (! d_check_char (di, '_'))
1775 return 0;
1777 return 1;
1780 /* <ctor-dtor-name> ::= C1
1781 ::= C2
1782 ::= C3
1783 ::= D0
1784 ::= D1
1785 ::= D2
1788 static struct demangle_component *
1789 d_ctor_dtor_name (struct d_info *di)
1791 if (di->last_name != NULL)
1793 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1794 di->expansion += di->last_name->u.s_name.len;
1795 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1796 di->expansion += di->last_name->u.s_string.len;
1798 switch (d_peek_char (di))
1800 case 'C':
1802 enum gnu_v3_ctor_kinds kind;
1804 switch (d_peek_next_char (di))
1806 case '1':
1807 kind = gnu_v3_complete_object_ctor;
1808 break;
1809 case '2':
1810 kind = gnu_v3_base_object_ctor;
1811 break;
1812 case '3':
1813 kind = gnu_v3_complete_object_allocating_ctor;
1814 break;
1815 default:
1816 return NULL;
1818 d_advance (di, 2);
1819 return d_make_ctor (di, kind, di->last_name);
1822 case 'D':
1824 enum gnu_v3_dtor_kinds kind;
1826 switch (d_peek_next_char (di))
1828 case '0':
1829 kind = gnu_v3_deleting_dtor;
1830 break;
1831 case '1':
1832 kind = gnu_v3_complete_object_dtor;
1833 break;
1834 case '2':
1835 kind = gnu_v3_base_object_dtor;
1836 break;
1837 default:
1838 return NULL;
1840 d_advance (di, 2);
1841 return d_make_dtor (di, kind, di->last_name);
1844 default:
1845 return NULL;
1849 /* <type> ::= <builtin-type>
1850 ::= <function-type>
1851 ::= <class-enum-type>
1852 ::= <array-type>
1853 ::= <pointer-to-member-type>
1854 ::= <template-param>
1855 ::= <template-template-param> <template-args>
1856 ::= <substitution>
1857 ::= <CV-qualifiers> <type>
1858 ::= P <type>
1859 ::= R <type>
1860 ::= O <type> (C++0x)
1861 ::= C <type>
1862 ::= G <type>
1863 ::= U <source-name> <type>
1865 <builtin-type> ::= various one letter codes
1866 ::= u <source-name>
1869 CP_STATIC_IF_GLIBCPP_V3
1870 const struct demangle_builtin_type_info
1871 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1873 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1874 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1875 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1876 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1877 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1878 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1879 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1880 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1881 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1882 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1883 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1884 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1885 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1886 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1887 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1888 D_PRINT_DEFAULT },
1889 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1890 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1891 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1892 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1893 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1894 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1895 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1896 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1897 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1898 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1899 D_PRINT_UNSIGNED_LONG_LONG },
1900 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1901 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
1902 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
1903 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
1904 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
1905 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
1906 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
1909 CP_STATIC_IF_GLIBCPP_V3
1910 struct demangle_component *
1911 cplus_demangle_type (struct d_info *di)
1913 char peek;
1914 struct demangle_component *ret;
1915 int can_subst;
1917 /* The ABI specifies that when CV-qualifiers are used, the base type
1918 is substitutable, and the fully qualified type is substitutable,
1919 but the base type with a strict subset of the CV-qualifiers is
1920 not substitutable. The natural recursive implementation of the
1921 CV-qualifiers would cause subsets to be substitutable, so instead
1922 we pull them all off now.
1924 FIXME: The ABI says that order-insensitive vendor qualifiers
1925 should be handled in the same way, but we have no way to tell
1926 which vendor qualifiers are order-insensitive and which are
1927 order-sensitive. So we just assume that they are all
1928 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1929 __vector, and it treats it as order-sensitive when mangling
1930 names. */
1932 peek = d_peek_char (di);
1933 if (peek == 'r' || peek == 'V' || peek == 'K')
1935 struct demangle_component **pret;
1937 pret = d_cv_qualifiers (di, &ret, 0);
1938 if (pret == NULL)
1939 return NULL;
1940 *pret = cplus_demangle_type (di);
1941 if (! *pret || ! d_add_substitution (di, ret))
1942 return NULL;
1943 return ret;
1946 can_subst = 1;
1948 switch (peek)
1950 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1951 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1952 case 'o': case 's': case 't':
1953 case 'v': case 'w': case 'x': case 'y': case 'z':
1954 ret = d_make_builtin_type (di,
1955 &cplus_demangle_builtin_types[peek - 'a']);
1956 di->expansion += ret->u.s_builtin.type->len;
1957 can_subst = 0;
1958 d_advance (di, 1);
1959 break;
1961 case 'u':
1962 d_advance (di, 1);
1963 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1964 d_source_name (di), NULL);
1965 break;
1967 case 'F':
1968 ret = d_function_type (di);
1969 break;
1971 case '0': case '1': case '2': case '3': case '4':
1972 case '5': case '6': case '7': case '8': case '9':
1973 case 'N':
1974 case 'Z':
1975 ret = d_class_enum_type (di);
1976 break;
1978 case 'A':
1979 ret = d_array_type (di);
1980 break;
1982 case 'M':
1983 ret = d_pointer_to_member_type (di);
1984 break;
1986 case 'T':
1987 ret = d_template_param (di);
1988 if (d_peek_char (di) == 'I')
1990 /* This is <template-template-param> <template-args>. The
1991 <template-template-param> part is a substitution
1992 candidate. */
1993 if (! d_add_substitution (di, ret))
1994 return NULL;
1995 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1996 d_template_args (di));
1998 break;
2000 case 'S':
2001 /* If this is a special substitution, then it is the start of
2002 <class-enum-type>. */
2004 char peek_next;
2006 peek_next = d_peek_next_char (di);
2007 if (IS_DIGIT (peek_next)
2008 || peek_next == '_'
2009 || IS_UPPER (peek_next))
2011 ret = d_substitution (di, 0);
2012 /* The substituted name may have been a template name and
2013 may be followed by tepmlate args. */
2014 if (d_peek_char (di) == 'I')
2015 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2016 d_template_args (di));
2017 else
2018 can_subst = 0;
2020 else
2022 ret = d_class_enum_type (di);
2023 /* If the substitution was a complete type, then it is not
2024 a new substitution candidate. However, if the
2025 substitution was followed by template arguments, then
2026 the whole thing is a substitution candidate. */
2027 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2028 can_subst = 0;
2031 break;
2033 case 'O':
2034 d_advance (di, 1);
2035 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2036 cplus_demangle_type (di), NULL);
2037 break;
2039 case 'P':
2040 d_advance (di, 1);
2041 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2042 cplus_demangle_type (di), NULL);
2043 break;
2045 case 'R':
2046 d_advance (di, 1);
2047 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2048 cplus_demangle_type (di), NULL);
2049 break;
2051 case 'C':
2052 d_advance (di, 1);
2053 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2054 cplus_demangle_type (di), NULL);
2055 break;
2057 case 'G':
2058 d_advance (di, 1);
2059 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2060 cplus_demangle_type (di), NULL);
2061 break;
2063 case 'U':
2064 d_advance (di, 1);
2065 ret = d_source_name (di);
2066 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2067 cplus_demangle_type (di), ret);
2068 break;
2070 case 'D':
2071 can_subst = 0;
2072 d_advance (di, 1);
2073 peek = d_next_char (di);
2074 switch (peek)
2076 case 'T':
2077 case 't':
2078 /* decltype (expression) */
2079 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2080 d_expression (di), NULL);
2081 if (ret && d_next_char (di) != 'E')
2082 ret = NULL;
2083 break;
2085 case 'p':
2086 /* Pack expansion. */
2087 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2088 cplus_demangle_type (di), NULL);
2089 break;
2091 case 'f':
2092 /* 32-bit decimal floating point */
2093 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2094 di->expansion += ret->u.s_builtin.type->len;
2095 break;
2096 case 'd':
2097 /* 64-bit DFP */
2098 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2099 di->expansion += ret->u.s_builtin.type->len;
2100 break;
2101 case 'e':
2102 /* 128-bit DFP */
2103 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2104 di->expansion += ret->u.s_builtin.type->len;
2105 break;
2106 case 'h':
2107 /* 16-bit half-precision FP */
2108 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2109 di->expansion += ret->u.s_builtin.type->len;
2110 break;
2111 case 's':
2112 /* char16_t */
2113 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2114 di->expansion += ret->u.s_builtin.type->len;
2115 break;
2116 case 'i':
2117 /* char32_t */
2118 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2119 di->expansion += ret->u.s_builtin.type->len;
2120 break;
2122 case 'F':
2123 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2124 ret = d_make_empty (di);
2125 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2126 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2127 /* For demangling we don't care about the bits. */
2128 d_number (di);
2129 ret->u.s_fixed.length = cplus_demangle_type (di);
2130 d_number (di);
2131 peek = d_next_char (di);
2132 ret->u.s_fixed.sat = (peek == 's');
2133 break;
2135 default:
2136 return NULL;
2138 break;
2140 default:
2141 return NULL;
2144 if (can_subst)
2146 if (! d_add_substitution (di, ret))
2147 return NULL;
2150 return ret;
2153 /* <CV-qualifiers> ::= [r] [V] [K] */
2155 static struct demangle_component **
2156 d_cv_qualifiers (struct d_info *di,
2157 struct demangle_component **pret, int member_fn)
2159 char peek;
2161 peek = d_peek_char (di);
2162 while (peek == 'r' || peek == 'V' || peek == 'K')
2164 enum demangle_component_type t;
2166 d_advance (di, 1);
2167 if (peek == 'r')
2169 t = (member_fn
2170 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2171 : DEMANGLE_COMPONENT_RESTRICT);
2172 di->expansion += sizeof "restrict";
2174 else if (peek == 'V')
2176 t = (member_fn
2177 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2178 : DEMANGLE_COMPONENT_VOLATILE);
2179 di->expansion += sizeof "volatile";
2181 else
2183 t = (member_fn
2184 ? DEMANGLE_COMPONENT_CONST_THIS
2185 : DEMANGLE_COMPONENT_CONST);
2186 di->expansion += sizeof "const";
2189 *pret = d_make_comp (di, t, NULL, NULL);
2190 if (*pret == NULL)
2191 return NULL;
2192 pret = &d_left (*pret);
2194 peek = d_peek_char (di);
2197 return pret;
2200 /* <function-type> ::= F [Y] <bare-function-type> E */
2202 static struct demangle_component *
2203 d_function_type (struct d_info *di)
2205 struct demangle_component *ret;
2207 if (! d_check_char (di, 'F'))
2208 return NULL;
2209 if (d_peek_char (di) == 'Y')
2211 /* Function has C linkage. We don't print this information.
2212 FIXME: We should print it in verbose mode. */
2213 d_advance (di, 1);
2215 ret = d_bare_function_type (di, 1);
2216 if (! d_check_char (di, 'E'))
2217 return NULL;
2218 return ret;
2221 /* <bare-function-type> ::= [J]<type>+ */
2223 static struct demangle_component *
2224 d_bare_function_type (struct d_info *di, int has_return_type)
2226 struct demangle_component *return_type;
2227 struct demangle_component *tl;
2228 struct demangle_component **ptl;
2229 char peek;
2231 /* Detect special qualifier indicating that the first argument
2232 is the return type. */
2233 peek = d_peek_char (di);
2234 if (peek == 'J')
2236 d_advance (di, 1);
2237 has_return_type = 1;
2240 return_type = NULL;
2241 tl = NULL;
2242 ptl = &tl;
2243 while (1)
2245 struct demangle_component *type;
2247 peek = d_peek_char (di);
2248 if (peek == '\0' || peek == 'E')
2249 break;
2250 type = cplus_demangle_type (di);
2251 if (type == NULL)
2252 return NULL;
2253 if (has_return_type)
2255 return_type = type;
2256 has_return_type = 0;
2258 else
2260 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2261 if (*ptl == NULL)
2262 return NULL;
2263 ptl = &d_right (*ptl);
2267 /* There should be at least one parameter type besides the optional
2268 return type. A function which takes no arguments will have a
2269 single parameter type void. */
2270 if (tl == NULL)
2271 return NULL;
2273 /* If we have a single parameter type void, omit it. */
2274 if (d_right (tl) == NULL
2275 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2276 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2278 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2279 tl = NULL;
2282 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2285 /* <class-enum-type> ::= <name> */
2287 static struct demangle_component *
2288 d_class_enum_type (struct d_info *di)
2290 return d_name (di);
2293 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2294 ::= A [<(dimension) expression>] _ <(element) type>
2297 static struct demangle_component *
2298 d_array_type (struct d_info *di)
2300 char peek;
2301 struct demangle_component *dim;
2303 if (! d_check_char (di, 'A'))
2304 return NULL;
2306 peek = d_peek_char (di);
2307 if (peek == '_')
2308 dim = NULL;
2309 else if (IS_DIGIT (peek))
2311 const char *s;
2313 s = d_str (di);
2316 d_advance (di, 1);
2317 peek = d_peek_char (di);
2319 while (IS_DIGIT (peek));
2320 dim = d_make_name (di, s, d_str (di) - s);
2321 if (dim == NULL)
2322 return NULL;
2324 else
2326 dim = d_expression (di);
2327 if (dim == NULL)
2328 return NULL;
2331 if (! d_check_char (di, '_'))
2332 return NULL;
2334 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2335 cplus_demangle_type (di));
2338 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2340 static struct demangle_component *
2341 d_pointer_to_member_type (struct d_info *di)
2343 struct demangle_component *cl;
2344 struct demangle_component *mem;
2345 struct demangle_component **pmem;
2347 if (! d_check_char (di, 'M'))
2348 return NULL;
2350 cl = cplus_demangle_type (di);
2352 /* The ABI specifies that any type can be a substitution source, and
2353 that M is followed by two types, and that when a CV-qualified
2354 type is seen both the base type and the CV-qualified types are
2355 substitution sources. The ABI also specifies that for a pointer
2356 to a CV-qualified member function, the qualifiers are attached to
2357 the second type. Given the grammar, a plain reading of the ABI
2358 suggests that both the CV-qualified member function and the
2359 non-qualified member function are substitution sources. However,
2360 g++ does not work that way. g++ treats only the CV-qualified
2361 member function as a substitution source. FIXME. So to work
2362 with g++, we need to pull off the CV-qualifiers here, in order to
2363 avoid calling add_substitution() in cplus_demangle_type(). But
2364 for a CV-qualified member which is not a function, g++ does
2365 follow the ABI, so we need to handle that case here by calling
2366 d_add_substitution ourselves. */
2368 pmem = d_cv_qualifiers (di, &mem, 1);
2369 if (pmem == NULL)
2370 return NULL;
2371 *pmem = cplus_demangle_type (di);
2372 if (*pmem == NULL)
2373 return NULL;
2375 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2377 if (! d_add_substitution (di, mem))
2378 return NULL;
2381 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2384 /* <template-param> ::= T_
2385 ::= T <(parameter-2 non-negative) number> _
2388 static struct demangle_component *
2389 d_template_param (struct d_info *di)
2391 long param;
2393 if (! d_check_char (di, 'T'))
2394 return NULL;
2396 if (d_peek_char (di) == '_')
2397 param = 0;
2398 else
2400 param = d_number (di);
2401 if (param < 0)
2402 return NULL;
2403 param += 1;
2406 if (! d_check_char (di, '_'))
2407 return NULL;
2409 ++di->did_subs;
2411 return d_make_template_param (di, param);
2414 /* <template-args> ::= I <template-arg>+ E */
2416 static struct demangle_component *
2417 d_template_args (struct d_info *di)
2419 struct demangle_component *hold_last_name;
2420 struct demangle_component *al;
2421 struct demangle_component **pal;
2423 /* Preserve the last name we saw--don't let the template arguments
2424 clobber it, as that would give us the wrong name for a subsequent
2425 constructor or destructor. */
2426 hold_last_name = di->last_name;
2428 if (! d_check_char (di, 'I'))
2429 return NULL;
2431 if (d_peek_char (di) == 'E')
2433 /* An argument pack can be empty. */
2434 d_advance (di, 1);
2435 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2438 al = NULL;
2439 pal = &al;
2440 while (1)
2442 struct demangle_component *a;
2444 a = d_template_arg (di);
2445 if (a == NULL)
2446 return NULL;
2448 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2449 if (*pal == NULL)
2450 return NULL;
2451 pal = &d_right (*pal);
2453 if (d_peek_char (di) == 'E')
2455 d_advance (di, 1);
2456 break;
2460 di->last_name = hold_last_name;
2462 return al;
2465 /* <template-arg> ::= <type>
2466 ::= X <expression> E
2467 ::= <expr-primary>
2470 static struct demangle_component *
2471 d_template_arg (struct d_info *di)
2473 struct demangle_component *ret;
2475 switch (d_peek_char (di))
2477 case 'X':
2478 d_advance (di, 1);
2479 ret = d_expression (di);
2480 if (! d_check_char (di, 'E'))
2481 return NULL;
2482 return ret;
2484 case 'L':
2485 return d_expr_primary (di);
2487 case 'I':
2488 /* An argument pack. */
2489 return d_template_args (di);
2491 default:
2492 return cplus_demangle_type (di);
2496 /* Subroutine of <expression> ::= cl <expression>+ E */
2498 static struct demangle_component *
2499 d_exprlist (struct d_info *di)
2501 struct demangle_component *list = NULL;
2502 struct demangle_component **p = &list;
2504 if (d_peek_char (di) == 'E')
2506 d_advance (di, 1);
2507 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2510 while (1)
2512 struct demangle_component *arg = d_expression (di);
2513 if (arg == NULL)
2514 return NULL;
2516 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2517 if (*p == NULL)
2518 return NULL;
2519 p = &d_right (*p);
2521 if (d_peek_char (di) == 'E')
2523 d_advance (di, 1);
2524 break;
2528 return list;
2531 /* <expression> ::= <(unary) operator-name> <expression>
2532 ::= <(binary) operator-name> <expression> <expression>
2533 ::= <(trinary) operator-name> <expression> <expression> <expression>
2534 ::= cl <expression>+ E
2535 ::= st <type>
2536 ::= <template-param>
2537 ::= sr <type> <unqualified-name>
2538 ::= sr <type> <unqualified-name> <template-args>
2539 ::= <expr-primary>
2542 static struct demangle_component *
2543 d_expression (struct d_info *di)
2545 char peek;
2547 peek = d_peek_char (di);
2548 if (peek == 'L')
2549 return d_expr_primary (di);
2550 else if (peek == 'T')
2551 return d_template_param (di);
2552 else if (peek == 's' && d_peek_next_char (di) == 'r')
2554 struct demangle_component *type;
2555 struct demangle_component *name;
2557 d_advance (di, 2);
2558 type = cplus_demangle_type (di);
2559 name = d_unqualified_name (di);
2560 if (d_peek_char (di) != 'I')
2561 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2562 else
2563 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2564 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2565 d_template_args (di)));
2567 else if (peek == 's'
2568 && (d_peek_next_char (di) == 'T' || d_peek_next_char (di) == 'R'))
2570 /* Just demangle a parameter placeholder as its type. */
2571 d_advance (di, 2);
2572 return cplus_demangle_type (di);
2574 else if (IS_DIGIT (peek))
2576 /* We can get an unqualified name as an expression in the case of
2577 a dependent member access, i.e. decltype(T().i). */
2578 struct demangle_component *name = d_unqualified_name (di);
2579 if (name == NULL)
2580 return NULL;
2581 if (d_peek_char (di) == 'I')
2582 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2583 d_template_args (di));
2584 else
2585 return name;
2587 else
2589 struct demangle_component *op;
2590 int args;
2592 op = d_operator_name (di);
2593 if (op == NULL)
2594 return NULL;
2596 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2597 di->expansion += op->u.s_operator.op->len - 2;
2599 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2600 && strcmp (op->u.s_operator.op->code, "st") == 0)
2601 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2602 cplus_demangle_type (di));
2604 switch (op->type)
2606 default:
2607 return NULL;
2608 case DEMANGLE_COMPONENT_OPERATOR:
2609 args = op->u.s_operator.op->args;
2610 break;
2611 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2612 args = op->u.s_extended_operator.args;
2613 break;
2614 case DEMANGLE_COMPONENT_CAST:
2615 args = 1;
2616 break;
2619 switch (args)
2621 case 1:
2622 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2623 d_expression (di));
2624 case 2:
2626 struct demangle_component *left;
2627 struct demangle_component *right;
2629 left = d_expression (di);
2630 if (!strcmp (op->u.s_operator.op->code, "cl"))
2631 right = d_exprlist (di);
2632 else
2633 right = d_expression (di);
2635 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2636 d_make_comp (di,
2637 DEMANGLE_COMPONENT_BINARY_ARGS,
2638 left, right));
2640 case 3:
2642 struct demangle_component *first;
2643 struct demangle_component *second;
2645 first = d_expression (di);
2646 second = d_expression (di);
2647 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2648 d_make_comp (di,
2649 DEMANGLE_COMPONENT_TRINARY_ARG1,
2650 first,
2651 d_make_comp (di,
2652 DEMANGLE_COMPONENT_TRINARY_ARG2,
2653 second,
2654 d_expression (di))));
2656 default:
2657 return NULL;
2662 /* <expr-primary> ::= L <type> <(value) number> E
2663 ::= L <type> <(value) float> E
2664 ::= L <mangled-name> E
2667 static struct demangle_component *
2668 d_expr_primary (struct d_info *di)
2670 struct demangle_component *ret;
2672 if (! d_check_char (di, 'L'))
2673 return NULL;
2674 if (d_peek_char (di) == '_')
2675 ret = cplus_demangle_mangled_name (di, 0);
2676 else
2678 struct demangle_component *type;
2679 enum demangle_component_type t;
2680 const char *s;
2682 type = cplus_demangle_type (di);
2683 if (type == NULL)
2684 return NULL;
2686 /* If we have a type we know how to print, we aren't going to
2687 print the type name itself. */
2688 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2689 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2690 di->expansion -= type->u.s_builtin.type->len;
2692 /* Rather than try to interpret the literal value, we just
2693 collect it as a string. Note that it's possible to have a
2694 floating point literal here. The ABI specifies that the
2695 format of such literals is machine independent. That's fine,
2696 but what's not fine is that versions of g++ up to 3.2 with
2697 -fabi-version=1 used upper case letters in the hex constant,
2698 and dumped out gcc's internal representation. That makes it
2699 hard to tell where the constant ends, and hard to dump the
2700 constant in any readable form anyhow. We don't attempt to
2701 handle these cases. */
2703 t = DEMANGLE_COMPONENT_LITERAL;
2704 if (d_peek_char (di) == 'n')
2706 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2707 d_advance (di, 1);
2709 s = d_str (di);
2710 while (d_peek_char (di) != 'E')
2712 if (d_peek_char (di) == '\0')
2713 return NULL;
2714 d_advance (di, 1);
2716 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2718 if (! d_check_char (di, 'E'))
2719 return NULL;
2720 return ret;
2723 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2724 ::= Z <(function) encoding> E s [<discriminator>]
2727 static struct demangle_component *
2728 d_local_name (struct d_info *di)
2730 struct demangle_component *function;
2732 if (! d_check_char (di, 'Z'))
2733 return NULL;
2735 function = d_encoding (di, 0);
2737 if (! d_check_char (di, 'E'))
2738 return NULL;
2740 if (d_peek_char (di) == 's')
2742 d_advance (di, 1);
2743 if (! d_discriminator (di))
2744 return NULL;
2745 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2746 d_make_name (di, "string literal",
2747 sizeof "string literal" - 1));
2749 else
2751 struct demangle_component *name;
2753 name = d_name (di);
2754 if (! d_discriminator (di))
2755 return NULL;
2756 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2760 /* <discriminator> ::= _ <(non-negative) number>
2762 We demangle the discriminator, but we don't print it out. FIXME:
2763 We should print it out in verbose mode. */
2765 static int
2766 d_discriminator (struct d_info *di)
2768 long discrim;
2770 if (d_peek_char (di) != '_')
2771 return 1;
2772 d_advance (di, 1);
2773 discrim = d_number (di);
2774 if (discrim < 0)
2775 return 0;
2776 return 1;
2779 /* Add a new substitution. */
2781 static int
2782 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2784 if (dc == NULL)
2785 return 0;
2786 if (di->next_sub >= di->num_subs)
2787 return 0;
2788 di->subs[di->next_sub] = dc;
2789 ++di->next_sub;
2790 return 1;
2793 /* <substitution> ::= S <seq-id> _
2794 ::= S_
2795 ::= St
2796 ::= Sa
2797 ::= Sb
2798 ::= Ss
2799 ::= Si
2800 ::= So
2801 ::= Sd
2803 If PREFIX is non-zero, then this type is being used as a prefix in
2804 a qualified name. In this case, for the standard substitutions, we
2805 need to check whether we are being used as a prefix for a
2806 constructor or destructor, and return a full template name.
2807 Otherwise we will get something like std::iostream::~iostream()
2808 which does not correspond particularly well to any function which
2809 actually appears in the source.
2812 static const struct d_standard_sub_info standard_subs[] =
2814 { 't', NL ("std"),
2815 NL ("std"),
2816 NULL, 0 },
2817 { 'a', NL ("std::allocator"),
2818 NL ("std::allocator"),
2819 NL ("allocator") },
2820 { 'b', NL ("std::basic_string"),
2821 NL ("std::basic_string"),
2822 NL ("basic_string") },
2823 { 's', NL ("std::string"),
2824 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2825 NL ("basic_string") },
2826 { 'i', NL ("std::istream"),
2827 NL ("std::basic_istream<char, std::char_traits<char> >"),
2828 NL ("basic_istream") },
2829 { 'o', NL ("std::ostream"),
2830 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2831 NL ("basic_ostream") },
2832 { 'd', NL ("std::iostream"),
2833 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2834 NL ("basic_iostream") }
2837 static struct demangle_component *
2838 d_substitution (struct d_info *di, int prefix)
2840 char c;
2842 if (! d_check_char (di, 'S'))
2843 return NULL;
2845 c = d_next_char (di);
2846 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2848 unsigned int id;
2850 id = 0;
2851 if (c != '_')
2855 unsigned int new_id;
2857 if (IS_DIGIT (c))
2858 new_id = id * 36 + c - '0';
2859 else if (IS_UPPER (c))
2860 new_id = id * 36 + c - 'A' + 10;
2861 else
2862 return NULL;
2863 if (new_id < id)
2864 return NULL;
2865 id = new_id;
2866 c = d_next_char (di);
2868 while (c != '_');
2870 ++id;
2873 if (id >= (unsigned int) di->next_sub)
2874 return NULL;
2876 ++di->did_subs;
2878 return di->subs[id];
2880 else
2882 int verbose;
2883 const struct d_standard_sub_info *p;
2884 const struct d_standard_sub_info *pend;
2886 verbose = (di->options & DMGL_VERBOSE) != 0;
2887 if (! verbose && prefix)
2889 char peek;
2891 peek = d_peek_char (di);
2892 if (peek == 'C' || peek == 'D')
2893 verbose = 1;
2896 pend = (&standard_subs[0]
2897 + sizeof standard_subs / sizeof standard_subs[0]);
2898 for (p = &standard_subs[0]; p < pend; ++p)
2900 if (c == p->code)
2902 const char *s;
2903 int len;
2905 if (p->set_last_name != NULL)
2906 di->last_name = d_make_sub (di, p->set_last_name,
2907 p->set_last_name_len);
2908 if (verbose)
2910 s = p->full_expansion;
2911 len = p->full_len;
2913 else
2915 s = p->simple_expansion;
2916 len = p->simple_len;
2918 di->expansion += len;
2919 return d_make_sub (di, s, len);
2923 return NULL;
2927 /* Initialize a growable string. */
2929 static void
2930 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
2932 dgs->buf = NULL;
2933 dgs->len = 0;
2934 dgs->alc = 0;
2935 dgs->allocation_failure = 0;
2937 if (estimate > 0)
2938 d_growable_string_resize (dgs, estimate);
2941 /* Grow a growable string to a given size. */
2943 static inline void
2944 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
2946 size_t newalc;
2947 char *newbuf;
2949 if (dgs->allocation_failure)
2950 return;
2952 /* Start allocation at two bytes to avoid any possibility of confusion
2953 with the special value of 1 used as a return in *palc to indicate
2954 allocation failures. */
2955 newalc = dgs->alc > 0 ? dgs->alc : 2;
2956 while (newalc < need)
2957 newalc <<= 1;
2959 newbuf = (char *) realloc (dgs->buf, newalc);
2960 if (newbuf == NULL)
2962 free (dgs->buf);
2963 dgs->buf = NULL;
2964 dgs->len = 0;
2965 dgs->alc = 0;
2966 dgs->allocation_failure = 1;
2967 return;
2969 dgs->buf = newbuf;
2970 dgs->alc = newalc;
2973 /* Append a buffer to a growable string. */
2975 static inline void
2976 d_growable_string_append_buffer (struct d_growable_string *dgs,
2977 const char *s, size_t l)
2979 size_t need;
2981 need = dgs->len + l + 1;
2982 if (need > dgs->alc)
2983 d_growable_string_resize (dgs, need);
2985 if (dgs->allocation_failure)
2986 return;
2988 memcpy (dgs->buf + dgs->len, s, l);
2989 dgs->buf[dgs->len + l] = '\0';
2990 dgs->len += l;
2993 /* Bridge growable strings to the callback mechanism. */
2995 static void
2996 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
2998 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3000 d_growable_string_append_buffer (dgs, s, l);
3003 /* Initialize a print information structure. */
3005 static void
3006 d_print_init (struct d_print_info *dpi, int options,
3007 demangle_callbackref callback, void *opaque)
3009 dpi->options = options;
3010 dpi->len = 0;
3011 dpi->last_char = '\0';
3012 dpi->templates = NULL;
3013 dpi->modifiers = NULL;
3015 dpi->callback = callback;
3016 dpi->opaque = opaque;
3018 dpi->demangle_failure = 0;
3021 /* Indicate that an error occurred during printing, and test for error. */
3023 static inline void
3024 d_print_error (struct d_print_info *dpi)
3026 dpi->demangle_failure = 1;
3029 static inline int
3030 d_print_saw_error (struct d_print_info *dpi)
3032 return dpi->demangle_failure != 0;
3035 /* Flush buffered characters to the callback. */
3037 static inline void
3038 d_print_flush (struct d_print_info *dpi)
3040 dpi->buf[dpi->len] = '\0';
3041 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3042 dpi->len = 0;
3045 /* Append characters and buffers for printing. */
3047 static inline void
3048 d_append_char (struct d_print_info *dpi, char c)
3050 if (dpi->len == sizeof (dpi->buf) - 1)
3051 d_print_flush (dpi);
3053 dpi->buf[dpi->len++] = c;
3054 dpi->last_char = c;
3057 static inline void
3058 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3060 size_t i;
3062 for (i = 0; i < l; i++)
3063 d_append_char (dpi, s[i]);
3066 static inline void
3067 d_append_string (struct d_print_info *dpi, const char *s)
3069 d_append_buffer (dpi, s, strlen (s));
3072 static inline char
3073 d_last_char (struct d_print_info *dpi)
3075 return dpi->last_char;
3078 /* Turn components into a human readable string. OPTIONS is the
3079 options bits passed to the demangler. DC is the tree to print.
3080 CALLBACK is a function to call to flush demangled string segments
3081 as they fill the intermediate buffer, and OPAQUE is a generalized
3082 callback argument. On success, this returns 1. On failure,
3083 it returns 0, indicating a bad parse. It does not use heap
3084 memory to build an output string, so cannot encounter memory
3085 allocation failure. */
3087 CP_STATIC_IF_GLIBCPP_V3
3089 cplus_demangle_print_callback (int options,
3090 const struct demangle_component *dc,
3091 demangle_callbackref callback, void *opaque)
3093 struct d_print_info dpi;
3095 d_print_init (&dpi, options, callback, opaque);
3097 d_print_comp (&dpi, dc);
3099 d_print_flush (&dpi);
3101 return ! d_print_saw_error (&dpi);
3104 /* Turn components into a human readable string. OPTIONS is the
3105 options bits passed to the demangler. DC is the tree to print.
3106 ESTIMATE is a guess at the length of the result. This returns a
3107 string allocated by malloc, or NULL on error. On success, this
3108 sets *PALC to the size of the allocated buffer. On failure, this
3109 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3110 failure. */
3112 CP_STATIC_IF_GLIBCPP_V3
3113 char *
3114 cplus_demangle_print (int options, const struct demangle_component *dc,
3115 int estimate, size_t *palc)
3117 struct d_growable_string dgs;
3119 d_growable_string_init (&dgs, estimate);
3121 if (! cplus_demangle_print_callback (options, dc,
3122 d_growable_string_callback_adapter,
3123 &dgs))
3125 free (dgs.buf);
3126 *palc = 0;
3127 return NULL;
3130 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3131 return dgs.buf;
3134 /* Returns the I'th element of the template arglist ARGS, or NULL on
3135 failure. */
3137 static struct demangle_component *
3138 d_index_template_argument (struct demangle_component *args, int i)
3140 struct demangle_component *a;
3142 for (a = args;
3143 a != NULL;
3144 a = d_right (a))
3146 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3147 return NULL;
3148 if (i <= 0)
3149 break;
3150 --i;
3152 if (i != 0 || a == NULL)
3153 return NULL;
3155 return d_left (a);
3158 /* Returns the template argument from the current context indicated by DC,
3159 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3161 static struct demangle_component *
3162 d_lookup_template_argument (struct d_print_info *dpi,
3163 const struct demangle_component *dc)
3165 if (dpi->templates == NULL)
3167 d_print_error (dpi);
3168 return NULL;
3171 return d_index_template_argument
3172 (d_right (dpi->templates->template_decl),
3173 dc->u.s_number.number);
3176 /* Returns a template argument pack used in DC (any will do), or NULL. */
3178 static struct demangle_component *
3179 d_find_pack (struct d_print_info *dpi,
3180 const struct demangle_component *dc)
3182 struct demangle_component *a;
3183 if (dc == NULL)
3184 return NULL;
3186 switch (dc->type)
3188 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3189 a = d_lookup_template_argument (dpi, dc);
3190 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3191 return a;
3192 return NULL;
3194 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3195 return NULL;
3197 case DEMANGLE_COMPONENT_NAME:
3198 case DEMANGLE_COMPONENT_OPERATOR:
3199 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3200 case DEMANGLE_COMPONENT_SUB_STD:
3201 case DEMANGLE_COMPONENT_CHARACTER:
3202 return NULL;
3204 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3205 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3206 case DEMANGLE_COMPONENT_CTOR:
3207 return d_find_pack (dpi, dc->u.s_ctor.name);
3208 case DEMANGLE_COMPONENT_DTOR:
3209 return d_find_pack (dpi, dc->u.s_dtor.name);
3211 default:
3212 a = d_find_pack (dpi, d_left (dc));
3213 if (a)
3214 return a;
3215 return d_find_pack (dpi, d_right (dc));
3219 /* Returns the length of the template argument pack DC. */
3221 static int
3222 d_pack_length (const struct demangle_component *dc)
3224 int count = 0;
3225 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3226 && d_left (dc) != NULL)
3228 ++count;
3229 dc = d_right (dc);
3231 return count;
3234 /* DC is a component of a mangled expression. Print it, wrapped in parens
3235 if needed. */
3237 static void
3238 d_print_subexpr (struct d_print_info *dpi,
3239 const struct demangle_component *dc)
3241 int simple = 0;
3242 if (dc->type == DEMANGLE_COMPONENT_NAME)
3243 simple = 1;
3244 if (!simple)
3245 d_append_char (dpi, '(');
3246 d_print_comp (dpi, dc);
3247 if (!simple)
3248 d_append_char (dpi, ')');
3251 /* Subroutine to handle components. */
3253 static void
3254 d_print_comp (struct d_print_info *dpi,
3255 const struct demangle_component *dc)
3257 if (dc == NULL)
3259 d_print_error (dpi);
3260 return;
3262 if (d_print_saw_error (dpi))
3263 return;
3265 switch (dc->type)
3267 case DEMANGLE_COMPONENT_NAME:
3268 if ((dpi->options & DMGL_JAVA) == 0)
3269 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3270 else
3271 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3272 return;
3274 case DEMANGLE_COMPONENT_QUAL_NAME:
3275 case DEMANGLE_COMPONENT_LOCAL_NAME:
3276 d_print_comp (dpi, d_left (dc));
3277 if ((dpi->options & DMGL_JAVA) == 0)
3278 d_append_string (dpi, "::");
3279 else
3280 d_append_char (dpi, '.');
3281 d_print_comp (dpi, d_right (dc));
3282 return;
3284 case DEMANGLE_COMPONENT_TYPED_NAME:
3286 struct d_print_mod *hold_modifiers;
3287 struct demangle_component *typed_name;
3288 struct d_print_mod adpm[4];
3289 unsigned int i;
3290 struct d_print_template dpt;
3292 /* Pass the name down to the type so that it can be printed in
3293 the right place for the type. We also have to pass down
3294 any CV-qualifiers, which apply to the this parameter. */
3295 hold_modifiers = dpi->modifiers;
3296 i = 0;
3297 typed_name = d_left (dc);
3298 while (typed_name != NULL)
3300 if (i >= sizeof adpm / sizeof adpm[0])
3302 d_print_error (dpi);
3303 return;
3306 adpm[i].next = dpi->modifiers;
3307 dpi->modifiers = &adpm[i];
3308 adpm[i].mod = typed_name;
3309 adpm[i].printed = 0;
3310 adpm[i].templates = dpi->templates;
3311 ++i;
3313 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3314 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3315 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3316 break;
3318 typed_name = d_left (typed_name);
3321 if (typed_name == NULL)
3323 d_print_error (dpi);
3324 return;
3327 /* If typed_name is a template, then it applies to the
3328 function type as well. */
3329 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3331 dpt.next = dpi->templates;
3332 dpi->templates = &dpt;
3333 dpt.template_decl = typed_name;
3336 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3337 there may be CV-qualifiers on its right argument which
3338 really apply here; this happens when parsing a class which
3339 is local to a function. */
3340 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3342 struct demangle_component *local_name;
3344 local_name = d_right (typed_name);
3345 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3346 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3347 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3349 if (i >= sizeof adpm / sizeof adpm[0])
3351 d_print_error (dpi);
3352 return;
3355 adpm[i] = adpm[i - 1];
3356 adpm[i].next = &adpm[i - 1];
3357 dpi->modifiers = &adpm[i];
3359 adpm[i - 1].mod = local_name;
3360 adpm[i - 1].printed = 0;
3361 adpm[i - 1].templates = dpi->templates;
3362 ++i;
3364 local_name = d_left (local_name);
3368 d_print_comp (dpi, d_right (dc));
3370 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3371 dpi->templates = dpt.next;
3373 /* If the modifiers didn't get printed by the type, print them
3374 now. */
3375 while (i > 0)
3377 --i;
3378 if (! adpm[i].printed)
3380 d_append_char (dpi, ' ');
3381 d_print_mod (dpi, adpm[i].mod);
3385 dpi->modifiers = hold_modifiers;
3387 return;
3390 case DEMANGLE_COMPONENT_TEMPLATE:
3392 struct d_print_mod *hold_dpm;
3393 struct demangle_component *dcl;
3395 /* Don't push modifiers into a template definition. Doing so
3396 could give the wrong definition for a template argument.
3397 Instead, treat the template essentially as a name. */
3399 hold_dpm = dpi->modifiers;
3400 dpi->modifiers = NULL;
3402 dcl = d_left (dc);
3404 if ((dpi->options & DMGL_JAVA) != 0
3405 && dcl->type == DEMANGLE_COMPONENT_NAME
3406 && dcl->u.s_name.len == 6
3407 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3409 /* Special-case Java arrays, so that JArray<TYPE> appears
3410 instead as TYPE[]. */
3412 d_print_comp (dpi, d_right (dc));
3413 d_append_string (dpi, "[]");
3415 else
3417 d_print_comp (dpi, dcl);
3418 if (d_last_char (dpi) == '<')
3419 d_append_char (dpi, ' ');
3420 d_append_char (dpi, '<');
3421 d_print_comp (dpi, d_right (dc));
3422 /* Avoid generating two consecutive '>' characters, to avoid
3423 the C++ syntactic ambiguity. */
3424 if (d_last_char (dpi) == '>')
3425 d_append_char (dpi, ' ');
3426 d_append_char (dpi, '>');
3429 dpi->modifiers = hold_dpm;
3431 return;
3434 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3436 struct d_print_template *hold_dpt;
3437 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3439 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3440 a = d_index_template_argument (a, dpi->pack_index);
3442 if (a == NULL)
3444 d_print_error (dpi);
3445 return;
3448 /* While processing this parameter, we need to pop the list of
3449 templates. This is because the template parameter may
3450 itself be a reference to a parameter of an outer
3451 template. */
3453 hold_dpt = dpi->templates;
3454 dpi->templates = hold_dpt->next;
3456 d_print_comp (dpi, a);
3458 dpi->templates = hold_dpt;
3460 return;
3463 case DEMANGLE_COMPONENT_CTOR:
3464 d_print_comp (dpi, dc->u.s_ctor.name);
3465 return;
3467 case DEMANGLE_COMPONENT_DTOR:
3468 d_append_char (dpi, '~');
3469 d_print_comp (dpi, dc->u.s_dtor.name);
3470 return;
3472 case DEMANGLE_COMPONENT_VTABLE:
3473 d_append_string (dpi, "vtable for ");
3474 d_print_comp (dpi, d_left (dc));
3475 return;
3477 case DEMANGLE_COMPONENT_VTT:
3478 d_append_string (dpi, "VTT for ");
3479 d_print_comp (dpi, d_left (dc));
3480 return;
3482 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3483 d_append_string (dpi, "construction vtable for ");
3484 d_print_comp (dpi, d_left (dc));
3485 d_append_string (dpi, "-in-");
3486 d_print_comp (dpi, d_right (dc));
3487 return;
3489 case DEMANGLE_COMPONENT_TYPEINFO:
3490 d_append_string (dpi, "typeinfo for ");
3491 d_print_comp (dpi, d_left (dc));
3492 return;
3494 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3495 d_append_string (dpi, "typeinfo name for ");
3496 d_print_comp (dpi, d_left (dc));
3497 return;
3499 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3500 d_append_string (dpi, "typeinfo fn for ");
3501 d_print_comp (dpi, d_left (dc));
3502 return;
3504 case DEMANGLE_COMPONENT_THUNK:
3505 d_append_string (dpi, "non-virtual thunk to ");
3506 d_print_comp (dpi, d_left (dc));
3507 return;
3509 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3510 d_append_string (dpi, "virtual thunk to ");
3511 d_print_comp (dpi, d_left (dc));
3512 return;
3514 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3515 d_append_string (dpi, "covariant return thunk to ");
3516 d_print_comp (dpi, d_left (dc));
3517 return;
3519 case DEMANGLE_COMPONENT_JAVA_CLASS:
3520 d_append_string (dpi, "java Class for ");
3521 d_print_comp (dpi, d_left (dc));
3522 return;
3524 case DEMANGLE_COMPONENT_GUARD:
3525 d_append_string (dpi, "guard variable for ");
3526 d_print_comp (dpi, d_left (dc));
3527 return;
3529 case DEMANGLE_COMPONENT_REFTEMP:
3530 d_append_string (dpi, "reference temporary for ");
3531 d_print_comp (dpi, d_left (dc));
3532 return;
3534 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3535 d_append_string (dpi, "hidden alias for ");
3536 d_print_comp (dpi, d_left (dc));
3537 return;
3539 case DEMANGLE_COMPONENT_SUB_STD:
3540 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3541 return;
3543 case DEMANGLE_COMPONENT_RESTRICT:
3544 case DEMANGLE_COMPONENT_VOLATILE:
3545 case DEMANGLE_COMPONENT_CONST:
3547 struct d_print_mod *pdpm;
3549 /* When printing arrays, it's possible to have cases where the
3550 same CV-qualifier gets pushed on the stack multiple times.
3551 We only need to print it once. */
3553 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3555 if (! pdpm->printed)
3557 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3558 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3559 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3560 break;
3561 if (pdpm->mod->type == dc->type)
3563 d_print_comp (dpi, d_left (dc));
3564 return;
3569 /* Fall through. */
3570 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3571 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3572 case DEMANGLE_COMPONENT_CONST_THIS:
3573 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3574 case DEMANGLE_COMPONENT_POINTER:
3575 case DEMANGLE_COMPONENT_REFERENCE:
3576 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3577 case DEMANGLE_COMPONENT_COMPLEX:
3578 case DEMANGLE_COMPONENT_IMAGINARY:
3580 /* We keep a list of modifiers on the stack. */
3581 struct d_print_mod dpm;
3583 dpm.next = dpi->modifiers;
3584 dpi->modifiers = &dpm;
3585 dpm.mod = dc;
3586 dpm.printed = 0;
3587 dpm.templates = dpi->templates;
3589 d_print_comp (dpi, d_left (dc));
3591 /* If the modifier didn't get printed by the type, print it
3592 now. */
3593 if (! dpm.printed)
3594 d_print_mod (dpi, dc);
3596 dpi->modifiers = dpm.next;
3598 return;
3601 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3602 if ((dpi->options & DMGL_JAVA) == 0)
3603 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3604 dc->u.s_builtin.type->len);
3605 else
3606 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3607 dc->u.s_builtin.type->java_len);
3608 return;
3610 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3611 d_print_comp (dpi, d_left (dc));
3612 return;
3614 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3616 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3617 d_print_function_type (dpi, dc, dpi->modifiers);
3619 /* Print return type if present */
3620 if (d_left (dc) != NULL)
3622 struct d_print_mod dpm;
3624 /* We must pass this type down as a modifier in order to
3625 print it in the right location. */
3626 dpm.next = dpi->modifiers;
3627 dpi->modifiers = &dpm;
3628 dpm.mod = dc;
3629 dpm.printed = 0;
3630 dpm.templates = dpi->templates;
3632 d_print_comp (dpi, d_left (dc));
3634 dpi->modifiers = dpm.next;
3636 if (dpm.printed)
3637 return;
3639 /* In standard prefix notation, there is a space between the
3640 return type and the function signature. */
3641 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3642 d_append_char (dpi, ' ');
3645 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3646 d_print_function_type (dpi, dc, dpi->modifiers);
3648 return;
3651 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3653 struct d_print_mod *hold_modifiers;
3654 struct d_print_mod adpm[4];
3655 unsigned int i;
3656 struct d_print_mod *pdpm;
3658 /* We must pass this type down as a modifier in order to print
3659 multi-dimensional arrays correctly. If the array itself is
3660 CV-qualified, we act as though the element type were
3661 CV-qualified. We do this by copying the modifiers down
3662 rather than fiddling pointers, so that we don't wind up
3663 with a d_print_mod higher on the stack pointing into our
3664 stack frame after we return. */
3666 hold_modifiers = dpi->modifiers;
3668 adpm[0].next = hold_modifiers;
3669 dpi->modifiers = &adpm[0];
3670 adpm[0].mod = dc;
3671 adpm[0].printed = 0;
3672 adpm[0].templates = dpi->templates;
3674 i = 1;
3675 pdpm = hold_modifiers;
3676 while (pdpm != NULL
3677 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3678 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3679 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3681 if (! pdpm->printed)
3683 if (i >= sizeof adpm / sizeof adpm[0])
3685 d_print_error (dpi);
3686 return;
3689 adpm[i] = *pdpm;
3690 adpm[i].next = dpi->modifiers;
3691 dpi->modifiers = &adpm[i];
3692 pdpm->printed = 1;
3693 ++i;
3696 pdpm = pdpm->next;
3699 d_print_comp (dpi, d_right (dc));
3701 dpi->modifiers = hold_modifiers;
3703 if (adpm[0].printed)
3704 return;
3706 while (i > 1)
3708 --i;
3709 d_print_mod (dpi, adpm[i].mod);
3712 d_print_array_type (dpi, dc, dpi->modifiers);
3714 return;
3717 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3719 struct d_print_mod dpm;
3721 dpm.next = dpi->modifiers;
3722 dpi->modifiers = &dpm;
3723 dpm.mod = dc;
3724 dpm.printed = 0;
3725 dpm.templates = dpi->templates;
3727 d_print_comp (dpi, d_right (dc));
3729 /* If the modifier didn't get printed by the type, print it
3730 now. */
3731 if (! dpm.printed)
3733 d_append_char (dpi, ' ');
3734 d_print_comp (dpi, d_left (dc));
3735 d_append_string (dpi, "::*");
3738 dpi->modifiers = dpm.next;
3740 return;
3743 case DEMANGLE_COMPONENT_FIXED_TYPE:
3744 if (dc->u.s_fixed.sat)
3745 d_append_string (dpi, "_Sat ");
3746 /* Don't print "int _Accum". */
3747 if (dc->u.s_fixed.length->u.s_builtin.type
3748 != &cplus_demangle_builtin_types['i'-'a'])
3750 d_print_comp (dpi, dc->u.s_fixed.length);
3751 d_append_char (dpi, ' ');
3753 if (dc->u.s_fixed.accum)
3754 d_append_string (dpi, "_Accum");
3755 else
3756 d_append_string (dpi, "_Fract");
3757 return;
3759 case DEMANGLE_COMPONENT_ARGLIST:
3760 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3761 if (d_left (dc) != NULL)
3762 d_print_comp (dpi, d_left (dc));
3763 if (d_right (dc) != NULL)
3765 size_t len;
3766 d_append_string (dpi, ", ");
3767 len = dpi->len;
3768 d_print_comp (dpi, d_right (dc));
3769 /* If that didn't print anything (which can happen with empty
3770 template argument packs), remove the comma and space. */
3771 if (dpi->len == len)
3772 dpi->len -= 2;
3774 return;
3776 case DEMANGLE_COMPONENT_OPERATOR:
3778 char c;
3780 d_append_string (dpi, "operator");
3781 c = dc->u.s_operator.op->name[0];
3782 if (IS_LOWER (c))
3783 d_append_char (dpi, ' ');
3784 d_append_buffer (dpi, dc->u.s_operator.op->name,
3785 dc->u.s_operator.op->len);
3786 return;
3789 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3790 d_append_string (dpi, "operator ");
3791 d_print_comp (dpi, dc->u.s_extended_operator.name);
3792 return;
3794 case DEMANGLE_COMPONENT_CAST:
3795 d_append_string (dpi, "operator ");
3796 d_print_cast (dpi, dc);
3797 return;
3799 case DEMANGLE_COMPONENT_UNARY:
3800 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3801 d_print_expr_op (dpi, d_left (dc));
3802 else
3804 d_append_char (dpi, '(');
3805 d_print_cast (dpi, d_left (dc));
3806 d_append_char (dpi, ')');
3808 d_print_subexpr (dpi, d_right (dc));
3809 return;
3811 case DEMANGLE_COMPONENT_BINARY:
3812 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3814 d_print_error (dpi);
3815 return;
3818 /* We wrap an expression which uses the greater-than operator in
3819 an extra layer of parens so that it does not get confused
3820 with the '>' which ends the template parameters. */
3821 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3822 && d_left (dc)->u.s_operator.op->len == 1
3823 && d_left (dc)->u.s_operator.op->name[0] == '>')
3824 d_append_char (dpi, '(');
3826 d_print_subexpr (dpi, d_left (d_right (dc)));
3827 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
3828 d_print_expr_op (dpi, d_left (dc));
3829 d_print_subexpr (dpi, d_right (d_right (dc)));
3831 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3832 && d_left (dc)->u.s_operator.op->len == 1
3833 && d_left (dc)->u.s_operator.op->name[0] == '>')
3834 d_append_char (dpi, ')');
3836 return;
3838 case DEMANGLE_COMPONENT_BINARY_ARGS:
3839 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3840 d_print_error (dpi);
3841 return;
3843 case DEMANGLE_COMPONENT_TRINARY:
3844 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3845 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3847 d_print_error (dpi);
3848 return;
3850 d_print_subexpr (dpi, d_left (d_right (dc)));
3851 d_print_expr_op (dpi, d_left (dc));
3852 d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
3853 d_append_string (dpi, " : ");
3854 d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
3855 return;
3857 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3858 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3859 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3860 d_print_error (dpi);
3861 return;
3863 case DEMANGLE_COMPONENT_LITERAL:
3864 case DEMANGLE_COMPONENT_LITERAL_NEG:
3866 enum d_builtin_type_print tp;
3868 /* For some builtin types, produce simpler output. */
3869 tp = D_PRINT_DEFAULT;
3870 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3872 tp = d_left (dc)->u.s_builtin.type->print;
3873 switch (tp)
3875 case D_PRINT_INT:
3876 case D_PRINT_UNSIGNED:
3877 case D_PRINT_LONG:
3878 case D_PRINT_UNSIGNED_LONG:
3879 case D_PRINT_LONG_LONG:
3880 case D_PRINT_UNSIGNED_LONG_LONG:
3881 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3883 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3884 d_append_char (dpi, '-');
3885 d_print_comp (dpi, d_right (dc));
3886 switch (tp)
3888 default:
3889 break;
3890 case D_PRINT_UNSIGNED:
3891 d_append_char (dpi, 'u');
3892 break;
3893 case D_PRINT_LONG:
3894 d_append_char (dpi, 'l');
3895 break;
3896 case D_PRINT_UNSIGNED_LONG:
3897 d_append_string (dpi, "ul");
3898 break;
3899 case D_PRINT_LONG_LONG:
3900 d_append_string (dpi, "ll");
3901 break;
3902 case D_PRINT_UNSIGNED_LONG_LONG:
3903 d_append_string (dpi, "ull");
3904 break;
3906 return;
3908 break;
3910 case D_PRINT_BOOL:
3911 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3912 && d_right (dc)->u.s_name.len == 1
3913 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3915 switch (d_right (dc)->u.s_name.s[0])
3917 case '0':
3918 d_append_string (dpi, "false");
3919 return;
3920 case '1':
3921 d_append_string (dpi, "true");
3922 return;
3923 default:
3924 break;
3927 break;
3929 default:
3930 break;
3934 d_append_char (dpi, '(');
3935 d_print_comp (dpi, d_left (dc));
3936 d_append_char (dpi, ')');
3937 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3938 d_append_char (dpi, '-');
3939 if (tp == D_PRINT_FLOAT)
3940 d_append_char (dpi, '[');
3941 d_print_comp (dpi, d_right (dc));
3942 if (tp == D_PRINT_FLOAT)
3943 d_append_char (dpi, ']');
3945 return;
3947 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3948 d_append_string (dpi, "java resource ");
3949 d_print_comp (dpi, d_left (dc));
3950 return;
3952 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3953 d_print_comp (dpi, d_left (dc));
3954 d_print_comp (dpi, d_right (dc));
3955 return;
3957 case DEMANGLE_COMPONENT_CHARACTER:
3958 d_append_char (dpi, dc->u.s_character.character);
3959 return;
3961 case DEMANGLE_COMPONENT_DECLTYPE:
3962 d_append_string (dpi, "decltype (");
3963 d_print_comp (dpi, d_left (dc));
3964 d_append_char (dpi, ')');
3965 return;
3967 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3969 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
3970 int len = d_pack_length (a);
3971 int i;
3973 dc = d_left (dc);
3974 for (i = 0; i < len; ++i)
3976 dpi->pack_index = i;
3977 d_print_comp (dpi, dc);
3978 if (i < len-1)
3979 d_append_string (dpi, ", ");
3982 return;
3984 default:
3985 d_print_error (dpi);
3986 return;
3990 /* Print a Java dentifier. For Java we try to handle encoded extended
3991 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3992 so we don't it for C++. Characters are encoded as
3993 __U<hex-char>+_. */
3995 static void
3996 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3998 const char *p;
3999 const char *end;
4001 end = name + len;
4002 for (p = name; p < end; ++p)
4004 if (end - p > 3
4005 && p[0] == '_'
4006 && p[1] == '_'
4007 && p[2] == 'U')
4009 unsigned long c;
4010 const char *q;
4012 c = 0;
4013 for (q = p + 3; q < end; ++q)
4015 int dig;
4017 if (IS_DIGIT (*q))
4018 dig = *q - '0';
4019 else if (*q >= 'A' && *q <= 'F')
4020 dig = *q - 'A' + 10;
4021 else if (*q >= 'a' && *q <= 'f')
4022 dig = *q - 'a' + 10;
4023 else
4024 break;
4026 c = c * 16 + dig;
4028 /* If the Unicode character is larger than 256, we don't try
4029 to deal with it here. FIXME. */
4030 if (q < end && *q == '_' && c < 256)
4032 d_append_char (dpi, c);
4033 p = q;
4034 continue;
4038 d_append_char (dpi, *p);
4042 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4043 qualifiers on this after printing a function. */
4045 static void
4046 d_print_mod_list (struct d_print_info *dpi,
4047 struct d_print_mod *mods, int suffix)
4049 struct d_print_template *hold_dpt;
4051 if (mods == NULL || d_print_saw_error (dpi))
4052 return;
4054 if (mods->printed
4055 || (! suffix
4056 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4057 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4058 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4060 d_print_mod_list (dpi, mods->next, suffix);
4061 return;
4064 mods->printed = 1;
4066 hold_dpt = dpi->templates;
4067 dpi->templates = mods->templates;
4069 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4071 d_print_function_type (dpi, mods->mod, mods->next);
4072 dpi->templates = hold_dpt;
4073 return;
4075 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4077 d_print_array_type (dpi, mods->mod, mods->next);
4078 dpi->templates = hold_dpt;
4079 return;
4081 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4083 struct d_print_mod *hold_modifiers;
4084 struct demangle_component *dc;
4086 /* When this is on the modifier stack, we have pulled any
4087 qualifiers off the right argument already. Otherwise, we
4088 print it as usual, but don't let the left argument see any
4089 modifiers. */
4091 hold_modifiers = dpi->modifiers;
4092 dpi->modifiers = NULL;
4093 d_print_comp (dpi, d_left (mods->mod));
4094 dpi->modifiers = hold_modifiers;
4096 if ((dpi->options & DMGL_JAVA) == 0)
4097 d_append_string (dpi, "::");
4098 else
4099 d_append_char (dpi, '.');
4101 dc = d_right (mods->mod);
4102 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4103 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4104 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4105 dc = d_left (dc);
4107 d_print_comp (dpi, dc);
4109 dpi->templates = hold_dpt;
4110 return;
4113 d_print_mod (dpi, mods->mod);
4115 dpi->templates = hold_dpt;
4117 d_print_mod_list (dpi, mods->next, suffix);
4120 /* Print a modifier. */
4122 static void
4123 d_print_mod (struct d_print_info *dpi,
4124 const struct demangle_component *mod)
4126 switch (mod->type)
4128 case DEMANGLE_COMPONENT_RESTRICT:
4129 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4130 d_append_string (dpi, " restrict");
4131 return;
4132 case DEMANGLE_COMPONENT_VOLATILE:
4133 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4134 d_append_string (dpi, " volatile");
4135 return;
4136 case DEMANGLE_COMPONENT_CONST:
4137 case DEMANGLE_COMPONENT_CONST_THIS:
4138 d_append_string (dpi, " const");
4139 return;
4140 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4141 d_append_char (dpi, ' ');
4142 d_print_comp (dpi, d_right (mod));
4143 return;
4144 case DEMANGLE_COMPONENT_POINTER:
4145 /* There is no pointer symbol in Java. */
4146 if ((dpi->options & DMGL_JAVA) == 0)
4147 d_append_char (dpi, '*');
4148 return;
4149 case DEMANGLE_COMPONENT_REFERENCE:
4150 d_append_char (dpi, '&');
4151 return;
4152 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4153 d_append_string (dpi, "&&");
4154 return;
4155 case DEMANGLE_COMPONENT_COMPLEX:
4156 d_append_string (dpi, "complex ");
4157 return;
4158 case DEMANGLE_COMPONENT_IMAGINARY:
4159 d_append_string (dpi, "imaginary ");
4160 return;
4161 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4162 if (d_last_char (dpi) != '(')
4163 d_append_char (dpi, ' ');
4164 d_print_comp (dpi, d_left (mod));
4165 d_append_string (dpi, "::*");
4166 return;
4167 case DEMANGLE_COMPONENT_TYPED_NAME:
4168 d_print_comp (dpi, d_left (mod));
4169 return;
4170 default:
4171 /* Otherwise, we have something that won't go back on the
4172 modifier stack, so we can just print it. */
4173 d_print_comp (dpi, mod);
4174 return;
4178 /* Print a function type, except for the return type. */
4180 static void
4181 d_print_function_type (struct d_print_info *dpi,
4182 const struct demangle_component *dc,
4183 struct d_print_mod *mods)
4185 int need_paren;
4186 int saw_mod;
4187 int need_space;
4188 struct d_print_mod *p;
4189 struct d_print_mod *hold_modifiers;
4191 need_paren = 0;
4192 saw_mod = 0;
4193 need_space = 0;
4194 for (p = mods; p != NULL; p = p->next)
4196 if (p->printed)
4197 break;
4199 saw_mod = 1;
4200 switch (p->mod->type)
4202 case DEMANGLE_COMPONENT_POINTER:
4203 case DEMANGLE_COMPONENT_REFERENCE:
4204 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4205 need_paren = 1;
4206 break;
4207 case DEMANGLE_COMPONENT_RESTRICT:
4208 case DEMANGLE_COMPONENT_VOLATILE:
4209 case DEMANGLE_COMPONENT_CONST:
4210 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4211 case DEMANGLE_COMPONENT_COMPLEX:
4212 case DEMANGLE_COMPONENT_IMAGINARY:
4213 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4214 need_space = 1;
4215 need_paren = 1;
4216 break;
4217 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4218 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4219 case DEMANGLE_COMPONENT_CONST_THIS:
4220 break;
4221 default:
4222 break;
4224 if (need_paren)
4225 break;
4228 if (d_left (dc) != NULL && ! saw_mod)
4229 need_paren = 1;
4231 if (need_paren)
4233 if (! need_space)
4235 if (d_last_char (dpi) != '('
4236 && d_last_char (dpi) != '*')
4237 need_space = 1;
4239 if (need_space && d_last_char (dpi) != ' ')
4240 d_append_char (dpi, ' ');
4241 d_append_char (dpi, '(');
4244 hold_modifiers = dpi->modifiers;
4245 dpi->modifiers = NULL;
4247 d_print_mod_list (dpi, mods, 0);
4249 if (need_paren)
4250 d_append_char (dpi, ')');
4252 d_append_char (dpi, '(');
4254 if (d_right (dc) != NULL)
4255 d_print_comp (dpi, d_right (dc));
4257 d_append_char (dpi, ')');
4259 d_print_mod_list (dpi, mods, 1);
4261 dpi->modifiers = hold_modifiers;
4264 /* Print an array type, except for the element type. */
4266 static void
4267 d_print_array_type (struct d_print_info *dpi,
4268 const struct demangle_component *dc,
4269 struct d_print_mod *mods)
4271 int need_space;
4273 need_space = 1;
4274 if (mods != NULL)
4276 int need_paren;
4277 struct d_print_mod *p;
4279 need_paren = 0;
4280 for (p = mods; p != NULL; p = p->next)
4282 if (! p->printed)
4284 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4286 need_space = 0;
4287 break;
4289 else
4291 need_paren = 1;
4292 need_space = 1;
4293 break;
4298 if (need_paren)
4299 d_append_string (dpi, " (");
4301 d_print_mod_list (dpi, mods, 0);
4303 if (need_paren)
4304 d_append_char (dpi, ')');
4307 if (need_space)
4308 d_append_char (dpi, ' ');
4310 d_append_char (dpi, '[');
4312 if (d_left (dc) != NULL)
4313 d_print_comp (dpi, d_left (dc));
4315 d_append_char (dpi, ']');
4318 /* Print an operator in an expression. */
4320 static void
4321 d_print_expr_op (struct d_print_info *dpi,
4322 const struct demangle_component *dc)
4324 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4325 d_append_buffer (dpi, dc->u.s_operator.op->name,
4326 dc->u.s_operator.op->len);
4327 else
4328 d_print_comp (dpi, dc);
4331 /* Print a cast. */
4333 static void
4334 d_print_cast (struct d_print_info *dpi,
4335 const struct demangle_component *dc)
4337 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4338 d_print_comp (dpi, d_left (dc));
4339 else
4341 struct d_print_mod *hold_dpm;
4342 struct d_print_template dpt;
4344 /* It appears that for a templated cast operator, we need to put
4345 the template parameters in scope for the operator name, but
4346 not for the parameters. The effect is that we need to handle
4347 the template printing here. */
4349 hold_dpm = dpi->modifiers;
4350 dpi->modifiers = NULL;
4352 dpt.next = dpi->templates;
4353 dpi->templates = &dpt;
4354 dpt.template_decl = d_left (dc);
4356 d_print_comp (dpi, d_left (d_left (dc)));
4358 dpi->templates = dpt.next;
4360 if (d_last_char (dpi) == '<')
4361 d_append_char (dpi, ' ');
4362 d_append_char (dpi, '<');
4363 d_print_comp (dpi, d_right (d_left (dc)));
4364 /* Avoid generating two consecutive '>' characters, to avoid
4365 the C++ syntactic ambiguity. */
4366 if (d_last_char (dpi) == '>')
4367 d_append_char (dpi, ' ');
4368 d_append_char (dpi, '>');
4370 dpi->modifiers = hold_dpm;
4374 /* Initialize the information structure we use to pass around
4375 information. */
4377 CP_STATIC_IF_GLIBCPP_V3
4378 void
4379 cplus_demangle_init_info (const char *mangled, int options, size_t len,
4380 struct d_info *di)
4382 di->s = mangled;
4383 di->send = mangled + len;
4384 di->options = options;
4386 di->n = mangled;
4388 /* We can not need more components than twice the number of chars in
4389 the mangled string. Most components correspond directly to
4390 chars, but the ARGLIST types are exceptions. */
4391 di->num_comps = 2 * len;
4392 di->next_comp = 0;
4394 /* Similarly, we can not need more substitutions than there are
4395 chars in the mangled string. */
4396 di->num_subs = len;
4397 di->next_sub = 0;
4398 di->did_subs = 0;
4400 di->last_name = NULL;
4402 di->expansion = 0;
4405 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4406 mangled name, return strings in repeated callback giving the demangled
4407 name. OPTIONS is the usual libiberty demangler options. On success,
4408 this returns 1. On failure, returns 0. */
4410 static int
4411 d_demangle_callback (const char *mangled, int options,
4412 demangle_callbackref callback, void *opaque)
4414 int type;
4415 struct d_info di;
4416 struct demangle_component *dc;
4417 int status;
4419 if (mangled[0] == '_' && mangled[1] == 'Z')
4420 type = 0;
4421 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4422 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4423 && (mangled[9] == 'D' || mangled[9] == 'I')
4424 && mangled[10] == '_')
4426 const char *intro;
4428 intro = (mangled[9] == 'I')
4429 ? "global constructors keyed to "
4430 : "global destructors keyed to ";
4432 callback (intro, strlen (intro), opaque);
4433 callback (mangled + 11, strlen (mangled + 11), opaque);
4434 return 1;
4436 else
4438 if ((options & DMGL_TYPES) == 0)
4439 return 0;
4440 type = 1;
4443 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4446 #ifdef CP_DYNAMIC_ARRAYS
4447 __extension__ struct demangle_component comps[di.num_comps];
4448 __extension__ struct demangle_component *subs[di.num_subs];
4450 di.comps = comps;
4451 di.subs = subs;
4452 #else
4453 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4454 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4455 #endif
4457 if (type)
4458 dc = cplus_demangle_type (&di);
4459 else
4460 dc = cplus_demangle_mangled_name (&di, 1);
4462 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4463 mangled string, then we didn't successfully demangle it. If
4464 DMGL_PARAMS is not set, we didn't look at the trailing
4465 parameters. */
4466 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4467 dc = NULL;
4469 #ifdef CP_DEMANGLE_DEBUG
4470 d_dump (dc, 0);
4471 #endif
4473 status = (dc != NULL)
4474 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4475 : 0;
4478 return status;
4481 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4482 name, return a buffer allocated with malloc holding the demangled
4483 name. OPTIONS is the usual libiberty demangler options. On
4484 success, this sets *PALC to the allocated size of the returned
4485 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4486 a memory allocation failure, and returns NULL. */
4488 static char *
4489 d_demangle (const char *mangled, int options, size_t *palc)
4491 struct d_growable_string dgs;
4492 int status;
4494 d_growable_string_init (&dgs, 0);
4496 status = d_demangle_callback (mangled, options,
4497 d_growable_string_callback_adapter, &dgs);
4498 if (status == 0)
4500 free (dgs.buf);
4501 *palc = 0;
4502 return NULL;
4505 *palc = dgs.allocation_failure ? 1 : 0;
4506 return dgs.buf;
4509 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4511 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4513 /* ia64 ABI-mandated entry point in the C++ runtime library for
4514 performing demangling. MANGLED_NAME is a NUL-terminated character
4515 string containing the name to be demangled.
4517 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4518 *LENGTH bytes, into which the demangled name is stored. If
4519 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4520 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4521 is placed in a region of memory allocated with malloc.
4523 If LENGTH is non-NULL, the length of the buffer containing the
4524 demangled name, is placed in *LENGTH.
4526 The return value is a pointer to the start of the NUL-terminated
4527 demangled name, or NULL if the demangling fails. The caller is
4528 responsible for deallocating this memory using free.
4530 *STATUS is set to one of the following values:
4531 0: The demangling operation succeeded.
4532 -1: A memory allocation failure occurred.
4533 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4534 -3: One of the arguments is invalid.
4536 The demangling is performed using the C++ ABI mangling rules, with
4537 GNU extensions. */
4539 char *
4540 __cxa_demangle (const char *mangled_name, char *output_buffer,
4541 size_t *length, int *status)
4543 char *demangled;
4544 size_t alc;
4546 if (mangled_name == NULL)
4548 if (status != NULL)
4549 *status = -3;
4550 return NULL;
4553 if (output_buffer != NULL && length == NULL)
4555 if (status != NULL)
4556 *status = -3;
4557 return NULL;
4560 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4562 if (demangled == NULL)
4564 if (status != NULL)
4566 if (alc == 1)
4567 *status = -1;
4568 else
4569 *status = -2;
4571 return NULL;
4574 if (output_buffer == NULL)
4576 if (length != NULL)
4577 *length = alc;
4579 else
4581 if (strlen (demangled) < *length)
4583 strcpy (output_buffer, demangled);
4584 free (demangled);
4585 demangled = output_buffer;
4587 else
4589 free (output_buffer);
4590 *length = alc;
4594 if (status != NULL)
4595 *status = 0;
4597 return demangled;
4600 extern int __gcclibcxx_demangle_callback (const char *,
4601 void (*)
4602 (const char *, size_t, void *),
4603 void *);
4605 /* Alternative, allocationless entry point in the C++ runtime library
4606 for performing demangling. MANGLED_NAME is a NUL-terminated character
4607 string containing the name to be demangled.
4609 CALLBACK is a callback function, called with demangled string
4610 segments as demangling progresses; it is called at least once,
4611 but may be called more than once. OPAQUE is a generalized pointer
4612 used as a callback argument.
4614 The return code is one of the following values, equivalent to
4615 the STATUS values of __cxa_demangle() (excluding -1, since this
4616 function performs no memory allocations):
4617 0: The demangling operation succeeded.
4618 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4619 -3: One of the arguments is invalid.
4621 The demangling is performed using the C++ ABI mangling rules, with
4622 GNU extensions. */
4625 __gcclibcxx_demangle_callback (const char *mangled_name,
4626 void (*callback) (const char *, size_t, void *),
4627 void *opaque)
4629 int status;
4631 if (mangled_name == NULL || callback == NULL)
4632 return -3;
4634 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
4635 callback, opaque);
4636 if (status == 0)
4637 return -2;
4639 return 0;
4642 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4644 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4645 mangled name, return a buffer allocated with malloc holding the
4646 demangled name. Otherwise, return NULL. */
4648 char *
4649 cplus_demangle_v3 (const char *mangled, int options)
4651 size_t alc;
4653 return d_demangle (mangled, options, &alc);
4657 cplus_demangle_v3_callback (const char *mangled, int options,
4658 demangle_callbackref callback, void *opaque)
4660 return d_demangle_callback (mangled, options, callback, opaque);
4663 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4664 conventions, but the output formatting is a little different.
4665 This instructs the C++ demangler not to emit pointer characters ("*"), to
4666 use Java's namespace separator symbol ("." instead of "::"), and to output
4667 JArray<TYPE> as TYPE[]. */
4669 char *
4670 java_demangle_v3 (const char *mangled)
4672 size_t alc;
4674 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
4678 java_demangle_v3_callback (const char *mangled,
4679 demangle_callbackref callback, void *opaque)
4681 return d_demangle_callback (mangled,
4682 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4683 callback, opaque);
4686 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4688 #ifndef IN_GLIBCPP_V3
4690 /* Demangle a string in order to find out whether it is a constructor
4691 or destructor. Return non-zero on success. Set *CTOR_KIND and
4692 *DTOR_KIND appropriately. */
4694 static int
4695 is_ctor_or_dtor (const char *mangled,
4696 enum gnu_v3_ctor_kinds *ctor_kind,
4697 enum gnu_v3_dtor_kinds *dtor_kind)
4699 struct d_info di;
4700 struct demangle_component *dc;
4701 int ret;
4703 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4704 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4706 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4709 #ifdef CP_DYNAMIC_ARRAYS
4710 __extension__ struct demangle_component comps[di.num_comps];
4711 __extension__ struct demangle_component *subs[di.num_subs];
4713 di.comps = comps;
4714 di.subs = subs;
4715 #else
4716 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4717 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4718 #endif
4720 dc = cplus_demangle_mangled_name (&di, 1);
4722 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4723 to demangle the entire string. */
4725 ret = 0;
4726 while (dc != NULL)
4728 switch (dc->type)
4730 default:
4731 dc = NULL;
4732 break;
4733 case DEMANGLE_COMPONENT_TYPED_NAME:
4734 case DEMANGLE_COMPONENT_TEMPLATE:
4735 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4736 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4737 case DEMANGLE_COMPONENT_CONST_THIS:
4738 dc = d_left (dc);
4739 break;
4740 case DEMANGLE_COMPONENT_QUAL_NAME:
4741 case DEMANGLE_COMPONENT_LOCAL_NAME:
4742 dc = d_right (dc);
4743 break;
4744 case DEMANGLE_COMPONENT_CTOR:
4745 *ctor_kind = dc->u.s_ctor.kind;
4746 ret = 1;
4747 dc = NULL;
4748 break;
4749 case DEMANGLE_COMPONENT_DTOR:
4750 *dtor_kind = dc->u.s_dtor.kind;
4751 ret = 1;
4752 dc = NULL;
4753 break;
4758 return ret;
4761 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4762 name. A non-zero return indicates the type of constructor. */
4764 enum gnu_v3_ctor_kinds
4765 is_gnu_v3_mangled_ctor (const char *name)
4767 enum gnu_v3_ctor_kinds ctor_kind;
4768 enum gnu_v3_dtor_kinds dtor_kind;
4770 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4771 return (enum gnu_v3_ctor_kinds) 0;
4772 return ctor_kind;
4776 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4777 name. A non-zero return indicates the type of destructor. */
4779 enum gnu_v3_dtor_kinds
4780 is_gnu_v3_mangled_dtor (const char *name)
4782 enum gnu_v3_ctor_kinds ctor_kind;
4783 enum gnu_v3_dtor_kinds dtor_kind;
4785 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4786 return (enum gnu_v3_dtor_kinds) 0;
4787 return dtor_kind;
4790 #endif /* IN_GLIBCPP_V3 */
4792 #ifdef STANDALONE_DEMANGLER
4794 #include "getopt.h"
4795 #include "dyn-string.h"
4797 static void print_usage (FILE* fp, int exit_value);
4799 #define IS_ALPHA(CHAR) \
4800 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4801 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4803 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4804 #define is_mangled_char(CHAR) \
4805 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4806 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4808 /* The name of this program, as invoked. */
4809 const char* program_name;
4811 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4813 static void
4814 print_usage (FILE* fp, int exit_value)
4816 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4817 fprintf (fp, "Options:\n");
4818 fprintf (fp, " -h,--help Display this message.\n");
4819 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4820 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4821 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4823 exit (exit_value);
4826 /* Option specification for getopt_long. */
4827 static const struct option long_options[] =
4829 { "help", no_argument, NULL, 'h' },
4830 { "no-params", no_argument, NULL, 'p' },
4831 { "verbose", no_argument, NULL, 'v' },
4832 { NULL, no_argument, NULL, 0 },
4835 /* Main entry for a demangling filter executable. It will demangle
4836 its command line arguments, if any. If none are provided, it will
4837 filter stdin to stdout, replacing any recognized mangled C++ names
4838 with their demangled equivalents. */
4841 main (int argc, char *argv[])
4843 int i;
4844 int opt_char;
4845 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4847 /* Use the program name of this program, as invoked. */
4848 program_name = argv[0];
4850 /* Parse options. */
4853 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4854 switch (opt_char)
4856 case '?': /* Unrecognized option. */
4857 print_usage (stderr, 1);
4858 break;
4860 case 'h':
4861 print_usage (stdout, 0);
4862 break;
4864 case 'p':
4865 options &= ~ DMGL_PARAMS;
4866 break;
4868 case 'v':
4869 options |= DMGL_VERBOSE;
4870 break;
4873 while (opt_char != -1);
4875 if (optind == argc)
4876 /* No command line arguments were provided. Filter stdin. */
4878 dyn_string_t mangled = dyn_string_new (3);
4879 char *s;
4881 /* Read all of input. */
4882 while (!feof (stdin))
4884 char c;
4886 /* Pile characters into mangled until we hit one that can't
4887 occur in a mangled name. */
4888 c = getchar ();
4889 while (!feof (stdin) && is_mangled_char (c))
4891 dyn_string_append_char (mangled, c);
4892 if (feof (stdin))
4893 break;
4894 c = getchar ();
4897 if (dyn_string_length (mangled) > 0)
4899 #ifdef IN_GLIBCPP_V3
4900 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4901 #else
4902 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4903 #endif
4905 if (s != NULL)
4907 fputs (s, stdout);
4908 free (s);
4910 else
4912 /* It might not have been a mangled name. Print the
4913 original text. */
4914 fputs (dyn_string_buf (mangled), stdout);
4917 dyn_string_clear (mangled);
4920 /* If we haven't hit EOF yet, we've read one character that
4921 can't occur in a mangled name, so print it out. */
4922 if (!feof (stdin))
4923 putchar (c);
4926 dyn_string_delete (mangled);
4928 else
4929 /* Demangle command line arguments. */
4931 /* Loop over command line arguments. */
4932 for (i = optind; i < argc; ++i)
4934 char *s;
4935 #ifdef IN_GLIBCPP_V3
4936 int status;
4937 #endif
4939 /* Attempt to demangle. */
4940 #ifdef IN_GLIBCPP_V3
4941 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4942 #else
4943 s = cplus_demangle_v3 (argv[i], options);
4944 #endif
4946 /* If it worked, print the demangled name. */
4947 if (s != NULL)
4949 printf ("%s\n", s);
4950 free (s);
4952 else
4954 #ifdef IN_GLIBCPP_V3
4955 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4956 #else
4957 fprintf (stderr, "Failed: %s\n", argv[i]);
4958 #endif
4963 return 0;
4966 #endif /* STANDALONE_DEMANGLER */