PR target/35485
[official-gcc.git] / libiberty / cp-demangle.c
blob3fa5f1f21d3b2e30e2a6df7e3f01df7e69451bd6
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_ARGLIST:
625 printf ("argument list\n");
626 break;
627 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
628 printf ("template argument list\n");
629 break;
630 case DEMANGLE_COMPONENT_CAST:
631 printf ("cast\n");
632 break;
633 case DEMANGLE_COMPONENT_UNARY:
634 printf ("unary operator\n");
635 break;
636 case DEMANGLE_COMPONENT_BINARY:
637 printf ("binary operator\n");
638 break;
639 case DEMANGLE_COMPONENT_BINARY_ARGS:
640 printf ("binary operator arguments\n");
641 break;
642 case DEMANGLE_COMPONENT_TRINARY:
643 printf ("trinary operator\n");
644 break;
645 case DEMANGLE_COMPONENT_TRINARY_ARG1:
646 printf ("trinary operator arguments 1\n");
647 break;
648 case DEMANGLE_COMPONENT_TRINARY_ARG2:
649 printf ("trinary operator arguments 1\n");
650 break;
651 case DEMANGLE_COMPONENT_LITERAL:
652 printf ("literal\n");
653 break;
654 case DEMANGLE_COMPONENT_LITERAL_NEG:
655 printf ("negative literal\n");
656 break;
657 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
658 printf ("java resource\n");
659 break;
660 case DEMANGLE_COMPONENT_COMPOUND_NAME:
661 printf ("compound name\n");
662 break;
663 case DEMANGLE_COMPONENT_CHARACTER:
664 printf ("character '%c'\n", dc->u.s_character.character);
665 return;
666 case DEMANGLE_COMPONENT_DECLTYPE:
667 printf ("decltype\n");
668 break;
669 case DEMANGLE_COMPONENT_PACK_EXPANSION:
670 printf ("pack expansion\n");
671 break;
674 d_dump (d_left (dc), indent + 2);
675 d_dump (d_right (dc), indent + 2);
678 #endif /* CP_DEMANGLE_DEBUG */
680 /* Fill in a DEMANGLE_COMPONENT_NAME. */
682 CP_STATIC_IF_GLIBCPP_V3
684 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
686 if (p == NULL || s == NULL || len == 0)
687 return 0;
688 p->type = DEMANGLE_COMPONENT_NAME;
689 p->u.s_name.s = s;
690 p->u.s_name.len = len;
691 return 1;
694 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
696 CP_STATIC_IF_GLIBCPP_V3
698 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
699 struct demangle_component *name)
701 if (p == NULL || args < 0 || name == NULL)
702 return 0;
703 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
704 p->u.s_extended_operator.args = args;
705 p->u.s_extended_operator.name = name;
706 return 1;
709 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
711 CP_STATIC_IF_GLIBCPP_V3
713 cplus_demangle_fill_ctor (struct demangle_component *p,
714 enum gnu_v3_ctor_kinds kind,
715 struct demangle_component *name)
717 if (p == NULL
718 || name == NULL
719 || (kind < gnu_v3_complete_object_ctor
720 && kind > gnu_v3_complete_object_allocating_ctor))
721 return 0;
722 p->type = DEMANGLE_COMPONENT_CTOR;
723 p->u.s_ctor.kind = kind;
724 p->u.s_ctor.name = name;
725 return 1;
728 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
730 CP_STATIC_IF_GLIBCPP_V3
732 cplus_demangle_fill_dtor (struct demangle_component *p,
733 enum gnu_v3_dtor_kinds kind,
734 struct demangle_component *name)
736 if (p == NULL
737 || name == NULL
738 || (kind < gnu_v3_deleting_dtor
739 && kind > gnu_v3_base_object_dtor))
740 return 0;
741 p->type = DEMANGLE_COMPONENT_DTOR;
742 p->u.s_dtor.kind = kind;
743 p->u.s_dtor.name = name;
744 return 1;
747 /* Add a new component. */
749 static struct demangle_component *
750 d_make_empty (struct d_info *di)
752 struct demangle_component *p;
754 if (di->next_comp >= di->num_comps)
755 return NULL;
756 p = &di->comps[di->next_comp];
757 ++di->next_comp;
758 return p;
761 /* Add a new generic component. */
763 static struct demangle_component *
764 d_make_comp (struct d_info *di, enum demangle_component_type type,
765 struct demangle_component *left,
766 struct demangle_component *right)
768 struct demangle_component *p;
770 /* We check for errors here. A typical error would be a NULL return
771 from a subroutine. We catch those here, and return NULL
772 upward. */
773 switch (type)
775 /* These types require two parameters. */
776 case DEMANGLE_COMPONENT_QUAL_NAME:
777 case DEMANGLE_COMPONENT_LOCAL_NAME:
778 case DEMANGLE_COMPONENT_TYPED_NAME:
779 case DEMANGLE_COMPONENT_TEMPLATE:
780 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
781 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
782 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
783 case DEMANGLE_COMPONENT_UNARY:
784 case DEMANGLE_COMPONENT_BINARY:
785 case DEMANGLE_COMPONENT_BINARY_ARGS:
786 case DEMANGLE_COMPONENT_TRINARY:
787 case DEMANGLE_COMPONENT_TRINARY_ARG1:
788 case DEMANGLE_COMPONENT_TRINARY_ARG2:
789 case DEMANGLE_COMPONENT_LITERAL:
790 case DEMANGLE_COMPONENT_LITERAL_NEG:
791 case DEMANGLE_COMPONENT_COMPOUND_NAME:
792 if (left == NULL || right == NULL)
793 return NULL;
794 break;
796 /* These types only require one parameter. */
797 case DEMANGLE_COMPONENT_VTABLE:
798 case DEMANGLE_COMPONENT_VTT:
799 case DEMANGLE_COMPONENT_TYPEINFO:
800 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
801 case DEMANGLE_COMPONENT_TYPEINFO_FN:
802 case DEMANGLE_COMPONENT_THUNK:
803 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
804 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
805 case DEMANGLE_COMPONENT_JAVA_CLASS:
806 case DEMANGLE_COMPONENT_GUARD:
807 case DEMANGLE_COMPONENT_REFTEMP:
808 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
809 case DEMANGLE_COMPONENT_POINTER:
810 case DEMANGLE_COMPONENT_REFERENCE:
811 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
812 case DEMANGLE_COMPONENT_COMPLEX:
813 case DEMANGLE_COMPONENT_IMAGINARY:
814 case DEMANGLE_COMPONENT_VENDOR_TYPE:
815 case DEMANGLE_COMPONENT_CAST:
816 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
817 case DEMANGLE_COMPONENT_DECLTYPE:
818 case DEMANGLE_COMPONENT_PACK_EXPANSION:
819 if (left == NULL)
820 return NULL;
821 break;
823 /* This needs a right parameter, but the left parameter can be
824 empty. */
825 case DEMANGLE_COMPONENT_ARRAY_TYPE:
826 if (right == NULL)
827 return NULL;
828 break;
830 /* These are allowed to have no parameters--in some cases they
831 will be filled in later. */
832 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
833 case DEMANGLE_COMPONENT_RESTRICT:
834 case DEMANGLE_COMPONENT_VOLATILE:
835 case DEMANGLE_COMPONENT_CONST:
836 case DEMANGLE_COMPONENT_RESTRICT_THIS:
837 case DEMANGLE_COMPONENT_VOLATILE_THIS:
838 case DEMANGLE_COMPONENT_CONST_THIS:
839 case DEMANGLE_COMPONENT_ARGLIST:
840 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
841 break;
843 /* Other types should not be seen here. */
844 default:
845 return NULL;
848 p = d_make_empty (di);
849 if (p != NULL)
851 p->type = type;
852 p->u.s_binary.left = left;
853 p->u.s_binary.right = right;
855 return p;
858 /* Add a new name component. */
860 static struct demangle_component *
861 d_make_name (struct d_info *di, const char *s, int len)
863 struct demangle_component *p;
865 p = d_make_empty (di);
866 if (! cplus_demangle_fill_name (p, s, len))
867 return NULL;
868 return p;
871 /* Add a new builtin type component. */
873 static struct demangle_component *
874 d_make_builtin_type (struct d_info *di,
875 const struct demangle_builtin_type_info *type)
877 struct demangle_component *p;
879 if (type == NULL)
880 return NULL;
881 p = d_make_empty (di);
882 if (p != NULL)
884 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
885 p->u.s_builtin.type = type;
887 return p;
890 /* Add a new operator component. */
892 static struct demangle_component *
893 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
895 struct demangle_component *p;
897 p = d_make_empty (di);
898 if (p != NULL)
900 p->type = DEMANGLE_COMPONENT_OPERATOR;
901 p->u.s_operator.op = op;
903 return p;
906 /* Add a new extended operator component. */
908 static struct demangle_component *
909 d_make_extended_operator (struct d_info *di, int args,
910 struct demangle_component *name)
912 struct demangle_component *p;
914 p = d_make_empty (di);
915 if (! cplus_demangle_fill_extended_operator (p, args, name))
916 return NULL;
917 return p;
920 /* Add a new constructor component. */
922 static struct demangle_component *
923 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
924 struct demangle_component *name)
926 struct demangle_component *p;
928 p = d_make_empty (di);
929 if (! cplus_demangle_fill_ctor (p, kind, name))
930 return NULL;
931 return p;
934 /* Add a new destructor component. */
936 static struct demangle_component *
937 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
938 struct demangle_component *name)
940 struct demangle_component *p;
942 p = d_make_empty (di);
943 if (! cplus_demangle_fill_dtor (p, kind, name))
944 return NULL;
945 return p;
948 /* Add a new template parameter. */
950 static struct demangle_component *
951 d_make_template_param (struct d_info *di, long i)
953 struct demangle_component *p;
955 p = d_make_empty (di);
956 if (p != NULL)
958 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
959 p->u.s_number.number = i;
961 return p;
964 /* Add a new standard substitution component. */
966 static struct demangle_component *
967 d_make_sub (struct d_info *di, const char *name, int len)
969 struct demangle_component *p;
971 p = d_make_empty (di);
972 if (p != NULL)
974 p->type = DEMANGLE_COMPONENT_SUB_STD;
975 p->u.s_string.string = name;
976 p->u.s_string.len = len;
978 return p;
981 /* <mangled-name> ::= _Z <encoding>
983 TOP_LEVEL is non-zero when called at the top level. */
985 CP_STATIC_IF_GLIBCPP_V3
986 struct demangle_component *
987 cplus_demangle_mangled_name (struct d_info *di, int top_level)
989 if (! d_check_char (di, '_'))
990 return NULL;
991 if (! d_check_char (di, 'Z'))
992 return NULL;
993 return d_encoding (di, top_level);
996 /* Return whether a function should have a return type. The argument
997 is the function name, which may be qualified in various ways. The
998 rules are that template functions have return types with some
999 exceptions, function types which are not part of a function name
1000 mangling have return types with some exceptions, and non-template
1001 function names do not have return types. The exceptions are that
1002 constructors, destructors, and conversion operators do not have
1003 return types. */
1005 static int
1006 has_return_type (struct demangle_component *dc)
1008 if (dc == NULL)
1009 return 0;
1010 switch (dc->type)
1012 default:
1013 return 0;
1014 case DEMANGLE_COMPONENT_TEMPLATE:
1015 return ! is_ctor_dtor_or_conversion (d_left (dc));
1016 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1017 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1018 case DEMANGLE_COMPONENT_CONST_THIS:
1019 return has_return_type (d_left (dc));
1023 /* Return whether a name is a constructor, a destructor, or a
1024 conversion operator. */
1026 static int
1027 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1029 if (dc == NULL)
1030 return 0;
1031 switch (dc->type)
1033 default:
1034 return 0;
1035 case DEMANGLE_COMPONENT_QUAL_NAME:
1036 case DEMANGLE_COMPONENT_LOCAL_NAME:
1037 return is_ctor_dtor_or_conversion (d_right (dc));
1038 case DEMANGLE_COMPONENT_CTOR:
1039 case DEMANGLE_COMPONENT_DTOR:
1040 case DEMANGLE_COMPONENT_CAST:
1041 return 1;
1045 /* <encoding> ::= <(function) name> <bare-function-type>
1046 ::= <(data) name>
1047 ::= <special-name>
1049 TOP_LEVEL is non-zero when called at the top level, in which case
1050 if DMGL_PARAMS is not set we do not demangle the function
1051 parameters. We only set this at the top level, because otherwise
1052 we would not correctly demangle names in local scopes. */
1054 static struct demangle_component *
1055 d_encoding (struct d_info *di, int top_level)
1057 char peek = d_peek_char (di);
1059 if (peek == 'G' || peek == 'T')
1060 return d_special_name (di);
1061 else
1063 struct demangle_component *dc;
1065 dc = d_name (di);
1067 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1069 /* Strip off any initial CV-qualifiers, as they really apply
1070 to the `this' parameter, and they were not output by the
1071 v2 demangler without DMGL_PARAMS. */
1072 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1073 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1074 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1075 dc = d_left (dc);
1077 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1078 there may be CV-qualifiers on its right argument which
1079 really apply here; this happens when parsing a class
1080 which is local to a function. */
1081 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1083 struct demangle_component *dcr;
1085 dcr = d_right (dc);
1086 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1087 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1088 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1089 dcr = d_left (dcr);
1090 dc->u.s_binary.right = dcr;
1093 return dc;
1096 peek = d_peek_char (di);
1097 if (dc == NULL || peek == '\0' || peek == 'E')
1098 return dc;
1099 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1100 d_bare_function_type (di, has_return_type (dc)));
1104 /* <name> ::= <nested-name>
1105 ::= <unscoped-name>
1106 ::= <unscoped-template-name> <template-args>
1107 ::= <local-name>
1109 <unscoped-name> ::= <unqualified-name>
1110 ::= St <unqualified-name>
1112 <unscoped-template-name> ::= <unscoped-name>
1113 ::= <substitution>
1116 static struct demangle_component *
1117 d_name (struct d_info *di)
1119 char peek = d_peek_char (di);
1120 struct demangle_component *dc;
1122 switch (peek)
1124 case 'N':
1125 return d_nested_name (di);
1127 case 'Z':
1128 return d_local_name (di);
1130 case 'L':
1131 return d_unqualified_name (di);
1133 case 'S':
1135 int subst;
1137 if (d_peek_next_char (di) != 't')
1139 dc = d_substitution (di, 0);
1140 subst = 1;
1142 else
1144 d_advance (di, 2);
1145 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1146 d_make_name (di, "std", 3),
1147 d_unqualified_name (di));
1148 di->expansion += 3;
1149 subst = 0;
1152 if (d_peek_char (di) != 'I')
1154 /* The grammar does not permit this case to occur if we
1155 called d_substitution() above (i.e., subst == 1). We
1156 don't bother to check. */
1158 else
1160 /* This is <template-args>, which means that we just saw
1161 <unscoped-template-name>, which is a substitution
1162 candidate if we didn't just get it from a
1163 substitution. */
1164 if (! subst)
1166 if (! d_add_substitution (di, dc))
1167 return NULL;
1169 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1170 d_template_args (di));
1173 return dc;
1176 default:
1177 dc = d_unqualified_name (di);
1178 if (d_peek_char (di) == 'I')
1180 /* This is <template-args>, which means that we just saw
1181 <unscoped-template-name>, which is a substitution
1182 candidate. */
1183 if (! d_add_substitution (di, dc))
1184 return NULL;
1185 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1186 d_template_args (di));
1188 return dc;
1192 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1193 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1196 static struct demangle_component *
1197 d_nested_name (struct d_info *di)
1199 struct demangle_component *ret;
1200 struct demangle_component **pret;
1202 if (! d_check_char (di, 'N'))
1203 return NULL;
1205 pret = d_cv_qualifiers (di, &ret, 1);
1206 if (pret == NULL)
1207 return NULL;
1209 *pret = d_prefix (di);
1210 if (*pret == NULL)
1211 return NULL;
1213 if (! d_check_char (di, 'E'))
1214 return NULL;
1216 return ret;
1219 /* <prefix> ::= <prefix> <unqualified-name>
1220 ::= <template-prefix> <template-args>
1221 ::= <template-param>
1223 ::= <substitution>
1225 <template-prefix> ::= <prefix> <(template) unqualified-name>
1226 ::= <template-param>
1227 ::= <substitution>
1230 static struct demangle_component *
1231 d_prefix (struct d_info *di)
1233 struct demangle_component *ret = NULL;
1235 while (1)
1237 char peek;
1238 enum demangle_component_type comb_type;
1239 struct demangle_component *dc;
1241 peek = d_peek_char (di);
1242 if (peek == '\0')
1243 return NULL;
1245 /* The older code accepts a <local-name> here, but I don't see
1246 that in the grammar. The older code does not accept a
1247 <template-param> here. */
1249 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1250 if (IS_DIGIT (peek)
1251 || IS_LOWER (peek)
1252 || peek == 'C'
1253 || peek == 'D'
1254 || peek == 'L')
1255 dc = d_unqualified_name (di);
1256 else if (peek == 'S')
1257 dc = d_substitution (di, 1);
1258 else if (peek == 'I')
1260 if (ret == NULL)
1261 return NULL;
1262 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1263 dc = d_template_args (di);
1265 else if (peek == 'T')
1266 dc = d_template_param (di);
1267 else if (peek == 'E')
1268 return ret;
1269 else
1270 return NULL;
1272 if (ret == NULL)
1273 ret = dc;
1274 else
1275 ret = d_make_comp (di, comb_type, ret, dc);
1277 if (peek != 'S' && d_peek_char (di) != 'E')
1279 if (! d_add_substitution (di, ret))
1280 return NULL;
1285 /* <unqualified-name> ::= <operator-name>
1286 ::= <ctor-dtor-name>
1287 ::= <source-name>
1288 ::= <local-source-name>
1290 <local-source-name> ::= L <source-name> <discriminator>
1293 static struct demangle_component *
1294 d_unqualified_name (struct d_info *di)
1296 char peek;
1298 peek = d_peek_char (di);
1299 if (IS_DIGIT (peek))
1300 return d_source_name (di);
1301 else if (IS_LOWER (peek))
1303 struct demangle_component *ret;
1305 ret = d_operator_name (di);
1306 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1307 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1308 return ret;
1310 else if (peek == 'C' || peek == 'D')
1311 return d_ctor_dtor_name (di);
1312 else if (peek == 'L')
1314 struct demangle_component * ret;
1316 d_advance (di, 1);
1318 ret = d_source_name (di);
1319 if (ret == NULL)
1320 return NULL;
1321 if (! d_discriminator (di))
1322 return NULL;
1323 return ret;
1325 else
1326 return NULL;
1329 /* <source-name> ::= <(positive length) number> <identifier> */
1331 static struct demangle_component *
1332 d_source_name (struct d_info *di)
1334 long len;
1335 struct demangle_component *ret;
1337 len = d_number (di);
1338 if (len <= 0)
1339 return NULL;
1340 ret = d_identifier (di, len);
1341 di->last_name = ret;
1342 return ret;
1345 /* number ::= [n] <(non-negative decimal integer)> */
1347 static long
1348 d_number (struct d_info *di)
1350 int negative;
1351 char peek;
1352 long ret;
1354 negative = 0;
1355 peek = d_peek_char (di);
1356 if (peek == 'n')
1358 negative = 1;
1359 d_advance (di, 1);
1360 peek = d_peek_char (di);
1363 ret = 0;
1364 while (1)
1366 if (! IS_DIGIT (peek))
1368 if (negative)
1369 ret = - ret;
1370 return ret;
1372 ret = ret * 10 + peek - '0';
1373 d_advance (di, 1);
1374 peek = d_peek_char (di);
1378 /* identifier ::= <(unqualified source code identifier)> */
1380 static struct demangle_component *
1381 d_identifier (struct d_info *di, int len)
1383 const char *name;
1385 name = d_str (di);
1387 if (di->send - name < len)
1388 return NULL;
1390 d_advance (di, len);
1392 /* A Java mangled name may have a trailing '$' if it is a C++
1393 keyword. This '$' is not included in the length count. We just
1394 ignore the '$'. */
1395 if ((di->options & DMGL_JAVA) != 0
1396 && d_peek_char (di) == '$')
1397 d_advance (di, 1);
1399 /* Look for something which looks like a gcc encoding of an
1400 anonymous namespace, and replace it with a more user friendly
1401 name. */
1402 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1403 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1404 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1406 const char *s;
1408 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1409 if ((*s == '.' || *s == '_' || *s == '$')
1410 && s[1] == 'N')
1412 di->expansion -= len - sizeof "(anonymous namespace)";
1413 return d_make_name (di, "(anonymous namespace)",
1414 sizeof "(anonymous namespace)" - 1);
1418 return d_make_name (di, name, len);
1421 /* operator_name ::= many different two character encodings.
1422 ::= cv <type>
1423 ::= v <digit> <source-name>
1426 #define NL(s) s, (sizeof s) - 1
1428 CP_STATIC_IF_GLIBCPP_V3
1429 const struct demangle_operator_info cplus_demangle_operators[] =
1431 { "aN", NL ("&="), 2 },
1432 { "aS", NL ("="), 2 },
1433 { "aa", NL ("&&"), 2 },
1434 { "ad", NL ("&"), 1 },
1435 { "an", NL ("&"), 2 },
1436 { "cl", NL ("()"), 2 },
1437 { "cm", NL (","), 2 },
1438 { "co", NL ("~"), 1 },
1439 { "dV", NL ("/="), 2 },
1440 { "da", NL ("delete[]"), 1 },
1441 { "de", NL ("*"), 1 },
1442 { "dl", NL ("delete"), 1 },
1443 { "dt", NL ("."), 2 },
1444 { "dv", NL ("/"), 2 },
1445 { "eO", NL ("^="), 2 },
1446 { "eo", NL ("^"), 2 },
1447 { "eq", NL ("=="), 2 },
1448 { "ge", NL (">="), 2 },
1449 { "gt", NL (">"), 2 },
1450 { "ix", NL ("[]"), 2 },
1451 { "lS", NL ("<<="), 2 },
1452 { "le", NL ("<="), 2 },
1453 { "ls", NL ("<<"), 2 },
1454 { "lt", NL ("<"), 2 },
1455 { "mI", NL ("-="), 2 },
1456 { "mL", NL ("*="), 2 },
1457 { "mi", NL ("-"), 2 },
1458 { "ml", NL ("*"), 2 },
1459 { "mm", NL ("--"), 1 },
1460 { "na", NL ("new[]"), 1 },
1461 { "ne", NL ("!="), 2 },
1462 { "ng", NL ("-"), 1 },
1463 { "nt", NL ("!"), 1 },
1464 { "nw", NL ("new"), 1 },
1465 { "oR", NL ("|="), 2 },
1466 { "oo", NL ("||"), 2 },
1467 { "or", NL ("|"), 2 },
1468 { "pL", NL ("+="), 2 },
1469 { "pl", NL ("+"), 2 },
1470 { "pm", NL ("->*"), 2 },
1471 { "pp", NL ("++"), 1 },
1472 { "ps", NL ("+"), 1 },
1473 { "pt", NL ("->"), 2 },
1474 { "qu", NL ("?"), 3 },
1475 { "rM", NL ("%="), 2 },
1476 { "rS", NL (">>="), 2 },
1477 { "rm", NL ("%"), 2 },
1478 { "rs", NL (">>"), 2 },
1479 { "st", NL ("sizeof "), 1 },
1480 { "sz", NL ("sizeof "), 1 },
1481 { NULL, NULL, 0, 0 }
1484 static struct demangle_component *
1485 d_operator_name (struct d_info *di)
1487 char c1;
1488 char c2;
1490 c1 = d_next_char (di);
1491 c2 = d_next_char (di);
1492 if (c1 == 'v' && IS_DIGIT (c2))
1493 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1494 else if (c1 == 'c' && c2 == 'v')
1495 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1496 cplus_demangle_type (di), NULL);
1497 else
1499 /* LOW is the inclusive lower bound. */
1500 int low = 0;
1501 /* HIGH is the exclusive upper bound. We subtract one to ignore
1502 the sentinel at the end of the array. */
1503 int high = ((sizeof (cplus_demangle_operators)
1504 / sizeof (cplus_demangle_operators[0]))
1505 - 1);
1507 while (1)
1509 int i;
1510 const struct demangle_operator_info *p;
1512 i = low + (high - low) / 2;
1513 p = cplus_demangle_operators + i;
1515 if (c1 == p->code[0] && c2 == p->code[1])
1516 return d_make_operator (di, p);
1518 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1519 high = i;
1520 else
1521 low = i + 1;
1522 if (low == high)
1523 return NULL;
1528 static struct demangle_component *
1529 d_make_character (struct d_info *di, int c)
1531 struct demangle_component *p;
1532 p = d_make_empty (di);
1533 if (p != NULL)
1535 p->type = DEMANGLE_COMPONENT_CHARACTER;
1536 p->u.s_character.character = c;
1538 return p;
1541 static struct demangle_component *
1542 d_java_resource (struct d_info *di)
1544 struct demangle_component *p = NULL;
1545 struct demangle_component *next = NULL;
1546 long len, i;
1547 char c;
1548 const char *str;
1550 len = d_number (di);
1551 if (len <= 1)
1552 return NULL;
1554 /* Eat the leading '_'. */
1555 if (d_next_char (di) != '_')
1556 return NULL;
1557 len--;
1559 str = d_str (di);
1560 i = 0;
1562 while (len > 0)
1564 c = str[i];
1565 if (!c)
1566 return NULL;
1568 /* Each chunk is either a '$' escape... */
1569 if (c == '$')
1571 i++;
1572 switch (str[i++])
1574 case 'S':
1575 c = '/';
1576 break;
1577 case '_':
1578 c = '.';
1579 break;
1580 case '$':
1581 c = '$';
1582 break;
1583 default:
1584 return NULL;
1586 next = d_make_character (di, c);
1587 d_advance (di, i);
1588 str = d_str (di);
1589 len -= i;
1590 i = 0;
1591 if (next == NULL)
1592 return NULL;
1594 /* ... or a sequence of characters. */
1595 else
1597 while (i < len && str[i] && str[i] != '$')
1598 i++;
1600 next = d_make_name (di, str, i);
1601 d_advance (di, i);
1602 str = d_str (di);
1603 len -= i;
1604 i = 0;
1605 if (next == NULL)
1606 return NULL;
1609 if (p == NULL)
1610 p = next;
1611 else
1613 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1614 if (p == NULL)
1615 return NULL;
1619 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1621 return p;
1624 /* <special-name> ::= TV <type>
1625 ::= TT <type>
1626 ::= TI <type>
1627 ::= TS <type>
1628 ::= GV <(object) name>
1629 ::= T <call-offset> <(base) encoding>
1630 ::= Tc <call-offset> <call-offset> <(base) encoding>
1631 Also g++ extensions:
1632 ::= TC <type> <(offset) number> _ <(base) type>
1633 ::= TF <type>
1634 ::= TJ <type>
1635 ::= GR <name>
1636 ::= GA <encoding>
1637 ::= Gr <resource name>
1640 static struct demangle_component *
1641 d_special_name (struct d_info *di)
1643 di->expansion += 20;
1644 if (d_check_char (di, 'T'))
1646 switch (d_next_char (di))
1648 case 'V':
1649 di->expansion -= 5;
1650 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1651 cplus_demangle_type (di), NULL);
1652 case 'T':
1653 di->expansion -= 10;
1654 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1655 cplus_demangle_type (di), NULL);
1656 case 'I':
1657 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1658 cplus_demangle_type (di), NULL);
1659 case 'S':
1660 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1661 cplus_demangle_type (di), NULL);
1663 case 'h':
1664 if (! d_call_offset (di, 'h'))
1665 return NULL;
1666 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1667 d_encoding (di, 0), NULL);
1669 case 'v':
1670 if (! d_call_offset (di, 'v'))
1671 return NULL;
1672 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1673 d_encoding (di, 0), NULL);
1675 case 'c':
1676 if (! d_call_offset (di, '\0'))
1677 return NULL;
1678 if (! d_call_offset (di, '\0'))
1679 return NULL;
1680 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1681 d_encoding (di, 0), NULL);
1683 case 'C':
1685 struct demangle_component *derived_type;
1686 long offset;
1687 struct demangle_component *base_type;
1689 derived_type = cplus_demangle_type (di);
1690 offset = d_number (di);
1691 if (offset < 0)
1692 return NULL;
1693 if (! d_check_char (di, '_'))
1694 return NULL;
1695 base_type = cplus_demangle_type (di);
1696 /* We don't display the offset. FIXME: We should display
1697 it in verbose mode. */
1698 di->expansion += 5;
1699 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1700 base_type, derived_type);
1703 case 'F':
1704 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1705 cplus_demangle_type (di), NULL);
1706 case 'J':
1707 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1708 cplus_demangle_type (di), NULL);
1710 default:
1711 return NULL;
1714 else if (d_check_char (di, 'G'))
1716 switch (d_next_char (di))
1718 case 'V':
1719 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1721 case 'R':
1722 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1723 NULL);
1725 case 'A':
1726 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1727 d_encoding (di, 0), NULL);
1729 case 'r':
1730 return d_java_resource (di);
1732 default:
1733 return NULL;
1736 else
1737 return NULL;
1740 /* <call-offset> ::= h <nv-offset> _
1741 ::= v <v-offset> _
1743 <nv-offset> ::= <(offset) number>
1745 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1747 The C parameter, if not '\0', is a character we just read which is
1748 the start of the <call-offset>.
1750 We don't display the offset information anywhere. FIXME: We should
1751 display it in verbose mode. */
1753 static int
1754 d_call_offset (struct d_info *di, int c)
1756 if (c == '\0')
1757 c = d_next_char (di);
1759 if (c == 'h')
1760 d_number (di);
1761 else if (c == 'v')
1763 d_number (di);
1764 if (! d_check_char (di, '_'))
1765 return 0;
1766 d_number (di);
1768 else
1769 return 0;
1771 if (! d_check_char (di, '_'))
1772 return 0;
1774 return 1;
1777 /* <ctor-dtor-name> ::= C1
1778 ::= C2
1779 ::= C3
1780 ::= D0
1781 ::= D1
1782 ::= D2
1785 static struct demangle_component *
1786 d_ctor_dtor_name (struct d_info *di)
1788 if (di->last_name != NULL)
1790 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1791 di->expansion += di->last_name->u.s_name.len;
1792 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1793 di->expansion += di->last_name->u.s_string.len;
1795 switch (d_peek_char (di))
1797 case 'C':
1799 enum gnu_v3_ctor_kinds kind;
1801 switch (d_peek_next_char (di))
1803 case '1':
1804 kind = gnu_v3_complete_object_ctor;
1805 break;
1806 case '2':
1807 kind = gnu_v3_base_object_ctor;
1808 break;
1809 case '3':
1810 kind = gnu_v3_complete_object_allocating_ctor;
1811 break;
1812 default:
1813 return NULL;
1815 d_advance (di, 2);
1816 return d_make_ctor (di, kind, di->last_name);
1819 case 'D':
1821 enum gnu_v3_dtor_kinds kind;
1823 switch (d_peek_next_char (di))
1825 case '0':
1826 kind = gnu_v3_deleting_dtor;
1827 break;
1828 case '1':
1829 kind = gnu_v3_complete_object_dtor;
1830 break;
1831 case '2':
1832 kind = gnu_v3_base_object_dtor;
1833 break;
1834 default:
1835 return NULL;
1837 d_advance (di, 2);
1838 return d_make_dtor (di, kind, di->last_name);
1841 default:
1842 return NULL;
1846 /* <type> ::= <builtin-type>
1847 ::= <function-type>
1848 ::= <class-enum-type>
1849 ::= <array-type>
1850 ::= <pointer-to-member-type>
1851 ::= <template-param>
1852 ::= <template-template-param> <template-args>
1853 ::= <substitution>
1854 ::= <CV-qualifiers> <type>
1855 ::= P <type>
1856 ::= R <type>
1857 ::= O <type> (C++0x)
1858 ::= C <type>
1859 ::= G <type>
1860 ::= U <source-name> <type>
1862 <builtin-type> ::= various one letter codes
1863 ::= u <source-name>
1866 CP_STATIC_IF_GLIBCPP_V3
1867 const struct demangle_builtin_type_info
1868 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1870 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1871 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1872 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1873 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1874 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1875 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1876 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1877 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1878 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1879 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1880 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1881 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1882 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1883 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1884 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1885 D_PRINT_DEFAULT },
1886 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1887 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1888 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1889 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1890 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1891 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1892 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1893 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1894 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1895 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1896 D_PRINT_UNSIGNED_LONG_LONG },
1897 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1898 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
1899 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
1900 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
1901 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
1902 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
1903 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
1906 CP_STATIC_IF_GLIBCPP_V3
1907 struct demangle_component *
1908 cplus_demangle_type (struct d_info *di)
1910 char peek;
1911 struct demangle_component *ret;
1912 int can_subst;
1914 /* The ABI specifies that when CV-qualifiers are used, the base type
1915 is substitutable, and the fully qualified type is substitutable,
1916 but the base type with a strict subset of the CV-qualifiers is
1917 not substitutable. The natural recursive implementation of the
1918 CV-qualifiers would cause subsets to be substitutable, so instead
1919 we pull them all off now.
1921 FIXME: The ABI says that order-insensitive vendor qualifiers
1922 should be handled in the same way, but we have no way to tell
1923 which vendor qualifiers are order-insensitive and which are
1924 order-sensitive. So we just assume that they are all
1925 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1926 __vector, and it treats it as order-sensitive when mangling
1927 names. */
1929 peek = d_peek_char (di);
1930 if (peek == 'r' || peek == 'V' || peek == 'K')
1932 struct demangle_component **pret;
1934 pret = d_cv_qualifiers (di, &ret, 0);
1935 if (pret == NULL)
1936 return NULL;
1937 *pret = cplus_demangle_type (di);
1938 if (! *pret || ! d_add_substitution (di, ret))
1939 return NULL;
1940 return ret;
1943 can_subst = 1;
1945 switch (peek)
1947 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1948 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1949 case 'o': case 's': case 't':
1950 case 'v': case 'w': case 'x': case 'y': case 'z':
1951 ret = d_make_builtin_type (di,
1952 &cplus_demangle_builtin_types[peek - 'a']);
1953 di->expansion += ret->u.s_builtin.type->len;
1954 can_subst = 0;
1955 d_advance (di, 1);
1956 break;
1958 case 'u':
1959 d_advance (di, 1);
1960 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1961 d_source_name (di), NULL);
1962 break;
1964 case 'F':
1965 ret = d_function_type (di);
1966 break;
1968 case '0': case '1': case '2': case '3': case '4':
1969 case '5': case '6': case '7': case '8': case '9':
1970 case 'N':
1971 case 'Z':
1972 ret = d_class_enum_type (di);
1973 break;
1975 case 'A':
1976 ret = d_array_type (di);
1977 break;
1979 case 'M':
1980 ret = d_pointer_to_member_type (di);
1981 break;
1983 case 'T':
1984 ret = d_template_param (di);
1985 if (d_peek_char (di) == 'I')
1987 /* This is <template-template-param> <template-args>. The
1988 <template-template-param> part is a substitution
1989 candidate. */
1990 if (! d_add_substitution (di, ret))
1991 return NULL;
1992 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1993 d_template_args (di));
1995 break;
1997 case 'S':
1998 /* If this is a special substitution, then it is the start of
1999 <class-enum-type>. */
2001 char peek_next;
2003 peek_next = d_peek_next_char (di);
2004 if (IS_DIGIT (peek_next)
2005 || peek_next == '_'
2006 || IS_UPPER (peek_next))
2008 ret = d_substitution (di, 0);
2009 /* The substituted name may have been a template name and
2010 may be followed by tepmlate args. */
2011 if (d_peek_char (di) == 'I')
2012 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2013 d_template_args (di));
2014 else
2015 can_subst = 0;
2017 else
2019 ret = d_class_enum_type (di);
2020 /* If the substitution was a complete type, then it is not
2021 a new substitution candidate. However, if the
2022 substitution was followed by template arguments, then
2023 the whole thing is a substitution candidate. */
2024 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2025 can_subst = 0;
2028 break;
2030 case 'O':
2031 d_advance (di, 1);
2032 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2033 cplus_demangle_type (di), NULL);
2034 break;
2036 case 'P':
2037 d_advance (di, 1);
2038 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2039 cplus_demangle_type (di), NULL);
2040 break;
2042 case 'R':
2043 d_advance (di, 1);
2044 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2045 cplus_demangle_type (di), NULL);
2046 break;
2048 case 'C':
2049 d_advance (di, 1);
2050 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2051 cplus_demangle_type (di), NULL);
2052 break;
2054 case 'G':
2055 d_advance (di, 1);
2056 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2057 cplus_demangle_type (di), NULL);
2058 break;
2060 case 'U':
2061 d_advance (di, 1);
2062 ret = d_source_name (di);
2063 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2064 cplus_demangle_type (di), ret);
2065 break;
2067 case 'D':
2068 can_subst = 0;
2069 d_advance (di, 1);
2070 peek = d_next_char (di);
2071 switch (peek)
2073 case 'T':
2074 case 't':
2075 /* decltype (expression) */
2076 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2077 d_expression (di), NULL);
2078 if (ret && d_next_char (di) != 'E')
2079 ret = NULL;
2080 break;
2082 case 'p':
2083 /* Pack expansion. */
2084 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2085 cplus_demangle_type (di), NULL);
2086 break;
2088 case 'f':
2089 /* 32-bit decimal floating point */
2090 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2091 di->expansion += ret->u.s_builtin.type->len;
2092 break;
2093 case 'd':
2094 /* 64-bit DFP */
2095 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2096 di->expansion += ret->u.s_builtin.type->len;
2097 break;
2098 case 'e':
2099 /* 128-bit DFP */
2100 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2101 di->expansion += ret->u.s_builtin.type->len;
2102 break;
2103 case 'h':
2104 /* 16-bit half-precision FP */
2105 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2106 di->expansion += ret->u.s_builtin.type->len;
2107 break;
2108 case 's':
2109 /* char16_t */
2110 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2111 di->expansion += ret->u.s_builtin.type->len;
2112 break;
2113 case 'i':
2114 /* char32_t */
2115 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2116 di->expansion += ret->u.s_builtin.type->len;
2117 break;
2119 break;
2121 default:
2122 return NULL;
2125 if (can_subst)
2127 if (! d_add_substitution (di, ret))
2128 return NULL;
2131 return ret;
2134 /* <CV-qualifiers> ::= [r] [V] [K] */
2136 static struct demangle_component **
2137 d_cv_qualifiers (struct d_info *di,
2138 struct demangle_component **pret, int member_fn)
2140 char peek;
2142 peek = d_peek_char (di);
2143 while (peek == 'r' || peek == 'V' || peek == 'K')
2145 enum demangle_component_type t;
2147 d_advance (di, 1);
2148 if (peek == 'r')
2150 t = (member_fn
2151 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2152 : DEMANGLE_COMPONENT_RESTRICT);
2153 di->expansion += sizeof "restrict";
2155 else if (peek == 'V')
2157 t = (member_fn
2158 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2159 : DEMANGLE_COMPONENT_VOLATILE);
2160 di->expansion += sizeof "volatile";
2162 else
2164 t = (member_fn
2165 ? DEMANGLE_COMPONENT_CONST_THIS
2166 : DEMANGLE_COMPONENT_CONST);
2167 di->expansion += sizeof "const";
2170 *pret = d_make_comp (di, t, NULL, NULL);
2171 if (*pret == NULL)
2172 return NULL;
2173 pret = &d_left (*pret);
2175 peek = d_peek_char (di);
2178 return pret;
2181 /* <function-type> ::= F [Y] <bare-function-type> E */
2183 static struct demangle_component *
2184 d_function_type (struct d_info *di)
2186 struct demangle_component *ret;
2188 if (! d_check_char (di, 'F'))
2189 return NULL;
2190 if (d_peek_char (di) == 'Y')
2192 /* Function has C linkage. We don't print this information.
2193 FIXME: We should print it in verbose mode. */
2194 d_advance (di, 1);
2196 ret = d_bare_function_type (di, 1);
2197 if (! d_check_char (di, 'E'))
2198 return NULL;
2199 return ret;
2202 /* <bare-function-type> ::= [J]<type>+ */
2204 static struct demangle_component *
2205 d_bare_function_type (struct d_info *di, int has_return_type)
2207 struct demangle_component *return_type;
2208 struct demangle_component *tl;
2209 struct demangle_component **ptl;
2210 char peek;
2212 /* Detect special qualifier indicating that the first argument
2213 is the return type. */
2214 peek = d_peek_char (di);
2215 if (peek == 'J')
2217 d_advance (di, 1);
2218 has_return_type = 1;
2221 return_type = NULL;
2222 tl = NULL;
2223 ptl = &tl;
2224 while (1)
2226 struct demangle_component *type;
2228 peek = d_peek_char (di);
2229 if (peek == '\0' || peek == 'E')
2230 break;
2231 type = cplus_demangle_type (di);
2232 if (type == NULL)
2233 return NULL;
2234 if (has_return_type)
2236 return_type = type;
2237 has_return_type = 0;
2239 else
2241 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2242 if (*ptl == NULL)
2243 return NULL;
2244 ptl = &d_right (*ptl);
2248 /* There should be at least one parameter type besides the optional
2249 return type. A function which takes no arguments will have a
2250 single parameter type void. */
2251 if (tl == NULL)
2252 return NULL;
2254 /* If we have a single parameter type void, omit it. */
2255 if (d_right (tl) == NULL
2256 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2257 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2259 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2260 tl = NULL;
2263 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2266 /* <class-enum-type> ::= <name> */
2268 static struct demangle_component *
2269 d_class_enum_type (struct d_info *di)
2271 return d_name (di);
2274 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2275 ::= A [<(dimension) expression>] _ <(element) type>
2278 static struct demangle_component *
2279 d_array_type (struct d_info *di)
2281 char peek;
2282 struct demangle_component *dim;
2284 if (! d_check_char (di, 'A'))
2285 return NULL;
2287 peek = d_peek_char (di);
2288 if (peek == '_')
2289 dim = NULL;
2290 else if (IS_DIGIT (peek))
2292 const char *s;
2294 s = d_str (di);
2297 d_advance (di, 1);
2298 peek = d_peek_char (di);
2300 while (IS_DIGIT (peek));
2301 dim = d_make_name (di, s, d_str (di) - s);
2302 if (dim == NULL)
2303 return NULL;
2305 else
2307 dim = d_expression (di);
2308 if (dim == NULL)
2309 return NULL;
2312 if (! d_check_char (di, '_'))
2313 return NULL;
2315 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2316 cplus_demangle_type (di));
2319 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2321 static struct demangle_component *
2322 d_pointer_to_member_type (struct d_info *di)
2324 struct demangle_component *cl;
2325 struct demangle_component *mem;
2326 struct demangle_component **pmem;
2328 if (! d_check_char (di, 'M'))
2329 return NULL;
2331 cl = cplus_demangle_type (di);
2333 /* The ABI specifies that any type can be a substitution source, and
2334 that M is followed by two types, and that when a CV-qualified
2335 type is seen both the base type and the CV-qualified types are
2336 substitution sources. The ABI also specifies that for a pointer
2337 to a CV-qualified member function, the qualifiers are attached to
2338 the second type. Given the grammar, a plain reading of the ABI
2339 suggests that both the CV-qualified member function and the
2340 non-qualified member function are substitution sources. However,
2341 g++ does not work that way. g++ treats only the CV-qualified
2342 member function as a substitution source. FIXME. So to work
2343 with g++, we need to pull off the CV-qualifiers here, in order to
2344 avoid calling add_substitution() in cplus_demangle_type(). But
2345 for a CV-qualified member which is not a function, g++ does
2346 follow the ABI, so we need to handle that case here by calling
2347 d_add_substitution ourselves. */
2349 pmem = d_cv_qualifiers (di, &mem, 1);
2350 if (pmem == NULL)
2351 return NULL;
2352 *pmem = cplus_demangle_type (di);
2353 if (*pmem == NULL)
2354 return NULL;
2356 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2358 if (! d_add_substitution (di, mem))
2359 return NULL;
2362 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2365 /* <template-param> ::= T_
2366 ::= T <(parameter-2 non-negative) number> _
2369 static struct demangle_component *
2370 d_template_param (struct d_info *di)
2372 long param;
2374 if (! d_check_char (di, 'T'))
2375 return NULL;
2377 if (d_peek_char (di) == '_')
2378 param = 0;
2379 else
2381 param = d_number (di);
2382 if (param < 0)
2383 return NULL;
2384 param += 1;
2387 if (! d_check_char (di, '_'))
2388 return NULL;
2390 ++di->did_subs;
2392 return d_make_template_param (di, param);
2395 /* <template-args> ::= I <template-arg>+ E */
2397 static struct demangle_component *
2398 d_template_args (struct d_info *di)
2400 struct demangle_component *hold_last_name;
2401 struct demangle_component *al;
2402 struct demangle_component **pal;
2404 /* Preserve the last name we saw--don't let the template arguments
2405 clobber it, as that would give us the wrong name for a subsequent
2406 constructor or destructor. */
2407 hold_last_name = di->last_name;
2409 if (! d_check_char (di, 'I'))
2410 return NULL;
2412 if (d_peek_char (di) == 'E')
2414 /* An argument pack can be empty. */
2415 d_advance (di, 1);
2416 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2419 al = NULL;
2420 pal = &al;
2421 while (1)
2423 struct demangle_component *a;
2425 a = d_template_arg (di);
2426 if (a == NULL)
2427 return NULL;
2429 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2430 if (*pal == NULL)
2431 return NULL;
2432 pal = &d_right (*pal);
2434 if (d_peek_char (di) == 'E')
2436 d_advance (di, 1);
2437 break;
2441 di->last_name = hold_last_name;
2443 return al;
2446 /* <template-arg> ::= <type>
2447 ::= X <expression> E
2448 ::= <expr-primary>
2451 static struct demangle_component *
2452 d_template_arg (struct d_info *di)
2454 struct demangle_component *ret;
2456 switch (d_peek_char (di))
2458 case 'X':
2459 d_advance (di, 1);
2460 ret = d_expression (di);
2461 if (! d_check_char (di, 'E'))
2462 return NULL;
2463 return ret;
2465 case 'L':
2466 return d_expr_primary (di);
2468 case 'I':
2469 /* An argument pack. */
2470 return d_template_args (di);
2472 default:
2473 return cplus_demangle_type (di);
2477 /* Subroutine of <expression> ::= cl <expression>+ E */
2479 static struct demangle_component *
2480 d_exprlist (struct d_info *di)
2482 struct demangle_component *list = NULL;
2483 struct demangle_component **p = &list;
2485 if (d_peek_char (di) == 'E')
2487 d_advance (di, 1);
2488 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2491 while (1)
2493 struct demangle_component *arg = d_expression (di);
2494 if (arg == NULL)
2495 return NULL;
2497 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2498 if (*p == NULL)
2499 return NULL;
2500 p = &d_right (*p);
2502 if (d_peek_char (di) == 'E')
2504 d_advance (di, 1);
2505 break;
2509 return list;
2512 /* <expression> ::= <(unary) operator-name> <expression>
2513 ::= <(binary) operator-name> <expression> <expression>
2514 ::= <(trinary) operator-name> <expression> <expression> <expression>
2515 ::= cl <expression>+ E
2516 ::= st <type>
2517 ::= <template-param>
2518 ::= sr <type> <unqualified-name>
2519 ::= sr <type> <unqualified-name> <template-args>
2520 ::= <expr-primary>
2523 static struct demangle_component *
2524 d_expression (struct d_info *di)
2526 char peek;
2528 peek = d_peek_char (di);
2529 if (peek == 'L')
2530 return d_expr_primary (di);
2531 else if (peek == 'T')
2532 return d_template_param (di);
2533 else if (peek == 's' && d_peek_next_char (di) == 'r')
2535 struct demangle_component *type;
2536 struct demangle_component *name;
2538 d_advance (di, 2);
2539 type = cplus_demangle_type (di);
2540 name = d_unqualified_name (di);
2541 if (d_peek_char (di) != 'I')
2542 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2543 else
2544 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2545 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2546 d_template_args (di)));
2548 else if (peek == 's' && d_peek_next_char (di) == 'T')
2550 /* Just demangle a parameter placeholder as its type. */
2551 d_advance (di, 2);
2552 return cplus_demangle_type (di);
2554 else if (IS_DIGIT (peek))
2556 /* We can get an unqualified name as an expression in the case of
2557 a dependent member access, i.e. decltype(T().i). */
2558 struct demangle_component *name = d_unqualified_name (di);
2559 if (name == NULL)
2560 return NULL;
2561 if (d_peek_char (di) == 'I')
2562 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2563 d_template_args (di));
2564 else
2565 return name;
2567 else
2569 struct demangle_component *op;
2570 int args;
2572 op = d_operator_name (di);
2573 if (op == NULL)
2574 return NULL;
2576 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2577 di->expansion += op->u.s_operator.op->len - 2;
2579 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2580 && strcmp (op->u.s_operator.op->code, "st") == 0)
2581 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2582 cplus_demangle_type (di));
2584 switch (op->type)
2586 default:
2587 return NULL;
2588 case DEMANGLE_COMPONENT_OPERATOR:
2589 args = op->u.s_operator.op->args;
2590 break;
2591 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2592 args = op->u.s_extended_operator.args;
2593 break;
2594 case DEMANGLE_COMPONENT_CAST:
2595 if (d_peek_char (di) == 'v')
2596 /* T() encoded as an operand of void. */
2597 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2598 cplus_demangle_type (di));
2599 else
2600 args = 1;
2601 break;
2604 switch (args)
2606 case 1:
2607 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2608 d_expression (di));
2609 case 2:
2611 struct demangle_component *left;
2612 struct demangle_component *right;
2614 left = d_expression (di);
2615 if (!strcmp (op->u.s_operator.op->code, "cl"))
2616 right = d_exprlist (di);
2617 else
2618 right = d_expression (di);
2620 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2621 d_make_comp (di,
2622 DEMANGLE_COMPONENT_BINARY_ARGS,
2623 left, right));
2625 case 3:
2627 struct demangle_component *first;
2628 struct demangle_component *second;
2630 first = d_expression (di);
2631 second = d_expression (di);
2632 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2633 d_make_comp (di,
2634 DEMANGLE_COMPONENT_TRINARY_ARG1,
2635 first,
2636 d_make_comp (di,
2637 DEMANGLE_COMPONENT_TRINARY_ARG2,
2638 second,
2639 d_expression (di))));
2641 default:
2642 return NULL;
2647 /* <expr-primary> ::= L <type> <(value) number> E
2648 ::= L <type> <(value) float> E
2649 ::= L <mangled-name> E
2652 static struct demangle_component *
2653 d_expr_primary (struct d_info *di)
2655 struct demangle_component *ret;
2657 if (! d_check_char (di, 'L'))
2658 return NULL;
2659 if (d_peek_char (di) == '_')
2660 ret = cplus_demangle_mangled_name (di, 0);
2661 else
2663 struct demangle_component *type;
2664 enum demangle_component_type t;
2665 const char *s;
2667 type = cplus_demangle_type (di);
2668 if (type == NULL)
2669 return NULL;
2671 /* If we have a type we know how to print, we aren't going to
2672 print the type name itself. */
2673 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2674 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2675 di->expansion -= type->u.s_builtin.type->len;
2677 /* Rather than try to interpret the literal value, we just
2678 collect it as a string. Note that it's possible to have a
2679 floating point literal here. The ABI specifies that the
2680 format of such literals is machine independent. That's fine,
2681 but what's not fine is that versions of g++ up to 3.2 with
2682 -fabi-version=1 used upper case letters in the hex constant,
2683 and dumped out gcc's internal representation. That makes it
2684 hard to tell where the constant ends, and hard to dump the
2685 constant in any readable form anyhow. We don't attempt to
2686 handle these cases. */
2688 t = DEMANGLE_COMPONENT_LITERAL;
2689 if (d_peek_char (di) == 'n')
2691 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2692 d_advance (di, 1);
2694 s = d_str (di);
2695 while (d_peek_char (di) != 'E')
2697 if (d_peek_char (di) == '\0')
2698 return NULL;
2699 d_advance (di, 1);
2701 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2703 if (! d_check_char (di, 'E'))
2704 return NULL;
2705 return ret;
2708 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2709 ::= Z <(function) encoding> E s [<discriminator>]
2712 static struct demangle_component *
2713 d_local_name (struct d_info *di)
2715 struct demangle_component *function;
2717 if (! d_check_char (di, 'Z'))
2718 return NULL;
2720 function = d_encoding (di, 0);
2722 if (! d_check_char (di, 'E'))
2723 return NULL;
2725 if (d_peek_char (di) == 's')
2727 d_advance (di, 1);
2728 if (! d_discriminator (di))
2729 return NULL;
2730 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2731 d_make_name (di, "string literal",
2732 sizeof "string literal" - 1));
2734 else
2736 struct demangle_component *name;
2738 name = d_name (di);
2739 if (! d_discriminator (di))
2740 return NULL;
2741 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2745 /* <discriminator> ::= _ <(non-negative) number>
2747 We demangle the discriminator, but we don't print it out. FIXME:
2748 We should print it out in verbose mode. */
2750 static int
2751 d_discriminator (struct d_info *di)
2753 long discrim;
2755 if (d_peek_char (di) != '_')
2756 return 1;
2757 d_advance (di, 1);
2758 discrim = d_number (di);
2759 if (discrim < 0)
2760 return 0;
2761 return 1;
2764 /* Add a new substitution. */
2766 static int
2767 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2769 if (dc == NULL)
2770 return 0;
2771 if (di->next_sub >= di->num_subs)
2772 return 0;
2773 di->subs[di->next_sub] = dc;
2774 ++di->next_sub;
2775 return 1;
2778 /* <substitution> ::= S <seq-id> _
2779 ::= S_
2780 ::= St
2781 ::= Sa
2782 ::= Sb
2783 ::= Ss
2784 ::= Si
2785 ::= So
2786 ::= Sd
2788 If PREFIX is non-zero, then this type is being used as a prefix in
2789 a qualified name. In this case, for the standard substitutions, we
2790 need to check whether we are being used as a prefix for a
2791 constructor or destructor, and return a full template name.
2792 Otherwise we will get something like std::iostream::~iostream()
2793 which does not correspond particularly well to any function which
2794 actually appears in the source.
2797 static const struct d_standard_sub_info standard_subs[] =
2799 { 't', NL ("std"),
2800 NL ("std"),
2801 NULL, 0 },
2802 { 'a', NL ("std::allocator"),
2803 NL ("std::allocator"),
2804 NL ("allocator") },
2805 { 'b', NL ("std::basic_string"),
2806 NL ("std::basic_string"),
2807 NL ("basic_string") },
2808 { 's', NL ("std::string"),
2809 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2810 NL ("basic_string") },
2811 { 'i', NL ("std::istream"),
2812 NL ("std::basic_istream<char, std::char_traits<char> >"),
2813 NL ("basic_istream") },
2814 { 'o', NL ("std::ostream"),
2815 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2816 NL ("basic_ostream") },
2817 { 'd', NL ("std::iostream"),
2818 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2819 NL ("basic_iostream") }
2822 static struct demangle_component *
2823 d_substitution (struct d_info *di, int prefix)
2825 char c;
2827 if (! d_check_char (di, 'S'))
2828 return NULL;
2830 c = d_next_char (di);
2831 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2833 unsigned int id;
2835 id = 0;
2836 if (c != '_')
2840 unsigned int new_id;
2842 if (IS_DIGIT (c))
2843 new_id = id * 36 + c - '0';
2844 else if (IS_UPPER (c))
2845 new_id = id * 36 + c - 'A' + 10;
2846 else
2847 return NULL;
2848 if (new_id < id)
2849 return NULL;
2850 id = new_id;
2851 c = d_next_char (di);
2853 while (c != '_');
2855 ++id;
2858 if (id >= (unsigned int) di->next_sub)
2859 return NULL;
2861 ++di->did_subs;
2863 return di->subs[id];
2865 else
2867 int verbose;
2868 const struct d_standard_sub_info *p;
2869 const struct d_standard_sub_info *pend;
2871 verbose = (di->options & DMGL_VERBOSE) != 0;
2872 if (! verbose && prefix)
2874 char peek;
2876 peek = d_peek_char (di);
2877 if (peek == 'C' || peek == 'D')
2878 verbose = 1;
2881 pend = (&standard_subs[0]
2882 + sizeof standard_subs / sizeof standard_subs[0]);
2883 for (p = &standard_subs[0]; p < pend; ++p)
2885 if (c == p->code)
2887 const char *s;
2888 int len;
2890 if (p->set_last_name != NULL)
2891 di->last_name = d_make_sub (di, p->set_last_name,
2892 p->set_last_name_len);
2893 if (verbose)
2895 s = p->full_expansion;
2896 len = p->full_len;
2898 else
2900 s = p->simple_expansion;
2901 len = p->simple_len;
2903 di->expansion += len;
2904 return d_make_sub (di, s, len);
2908 return NULL;
2912 /* Initialize a growable string. */
2914 static void
2915 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
2917 dgs->buf = NULL;
2918 dgs->len = 0;
2919 dgs->alc = 0;
2920 dgs->allocation_failure = 0;
2922 if (estimate > 0)
2923 d_growable_string_resize (dgs, estimate);
2926 /* Grow a growable string to a given size. */
2928 static inline void
2929 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
2931 size_t newalc;
2932 char *newbuf;
2934 if (dgs->allocation_failure)
2935 return;
2937 /* Start allocation at two bytes to avoid any possibility of confusion
2938 with the special value of 1 used as a return in *palc to indicate
2939 allocation failures. */
2940 newalc = dgs->alc > 0 ? dgs->alc : 2;
2941 while (newalc < need)
2942 newalc <<= 1;
2944 newbuf = (char *) realloc (dgs->buf, newalc);
2945 if (newbuf == NULL)
2947 free (dgs->buf);
2948 dgs->buf = NULL;
2949 dgs->len = 0;
2950 dgs->alc = 0;
2951 dgs->allocation_failure = 1;
2952 return;
2954 dgs->buf = newbuf;
2955 dgs->alc = newalc;
2958 /* Append a buffer to a growable string. */
2960 static inline void
2961 d_growable_string_append_buffer (struct d_growable_string *dgs,
2962 const char *s, size_t l)
2964 size_t need;
2966 need = dgs->len + l + 1;
2967 if (need > dgs->alc)
2968 d_growable_string_resize (dgs, need);
2970 if (dgs->allocation_failure)
2971 return;
2973 memcpy (dgs->buf + dgs->len, s, l);
2974 dgs->buf[dgs->len + l] = '\0';
2975 dgs->len += l;
2978 /* Bridge growable strings to the callback mechanism. */
2980 static void
2981 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
2983 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
2985 d_growable_string_append_buffer (dgs, s, l);
2988 /* Initialize a print information structure. */
2990 static void
2991 d_print_init (struct d_print_info *dpi, int options,
2992 demangle_callbackref callback, void *opaque)
2994 dpi->options = options;
2995 dpi->len = 0;
2996 dpi->last_char = '\0';
2997 dpi->templates = NULL;
2998 dpi->modifiers = NULL;
3000 dpi->callback = callback;
3001 dpi->opaque = opaque;
3003 dpi->demangle_failure = 0;
3006 /* Indicate that an error occurred during printing, and test for error. */
3008 static inline void
3009 d_print_error (struct d_print_info *dpi)
3011 dpi->demangle_failure = 1;
3014 static inline int
3015 d_print_saw_error (struct d_print_info *dpi)
3017 return dpi->demangle_failure != 0;
3020 /* Flush buffered characters to the callback. */
3022 static inline void
3023 d_print_flush (struct d_print_info *dpi)
3025 dpi->buf[dpi->len] = '\0';
3026 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3027 dpi->len = 0;
3030 /* Append characters and buffers for printing. */
3032 static inline void
3033 d_append_char (struct d_print_info *dpi, char c)
3035 if (dpi->len == sizeof (dpi->buf) - 1)
3036 d_print_flush (dpi);
3038 dpi->buf[dpi->len++] = c;
3039 dpi->last_char = c;
3042 static inline void
3043 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3045 size_t i;
3047 for (i = 0; i < l; i++)
3048 d_append_char (dpi, s[i]);
3051 static inline void
3052 d_append_string (struct d_print_info *dpi, const char *s)
3054 d_append_buffer (dpi, s, strlen (s));
3057 static inline char
3058 d_last_char (struct d_print_info *dpi)
3060 return dpi->last_char;
3063 /* Turn components into a human readable string. OPTIONS is the
3064 options bits passed to the demangler. DC is the tree to print.
3065 CALLBACK is a function to call to flush demangled string segments
3066 as they fill the intermediate buffer, and OPAQUE is a generalized
3067 callback argument. On success, this returns 1. On failure,
3068 it returns 0, indicating a bad parse. It does not use heap
3069 memory to build an output string, so cannot encounter memory
3070 allocation failure. */
3072 CP_STATIC_IF_GLIBCPP_V3
3074 cplus_demangle_print_callback (int options,
3075 const struct demangle_component *dc,
3076 demangle_callbackref callback, void *opaque)
3078 struct d_print_info dpi;
3080 d_print_init (&dpi, options, callback, opaque);
3082 d_print_comp (&dpi, dc);
3084 d_print_flush (&dpi);
3086 return ! d_print_saw_error (&dpi);
3089 /* Turn components into a human readable string. OPTIONS is the
3090 options bits passed to the demangler. DC is the tree to print.
3091 ESTIMATE is a guess at the length of the result. This returns a
3092 string allocated by malloc, or NULL on error. On success, this
3093 sets *PALC to the size of the allocated buffer. On failure, this
3094 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3095 failure. */
3097 CP_STATIC_IF_GLIBCPP_V3
3098 char *
3099 cplus_demangle_print (int options, const struct demangle_component *dc,
3100 int estimate, size_t *palc)
3102 struct d_growable_string dgs;
3104 d_growable_string_init (&dgs, estimate);
3106 if (! cplus_demangle_print_callback (options, dc,
3107 d_growable_string_callback_adapter,
3108 &dgs))
3110 free (dgs.buf);
3111 *palc = 0;
3112 return NULL;
3115 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3116 return dgs.buf;
3119 /* Returns the I'th element of the template arglist ARGS, or NULL on
3120 failure. */
3122 static struct demangle_component *
3123 d_index_template_argument (struct demangle_component *args, int i)
3125 struct demangle_component *a;
3127 for (a = args;
3128 a != NULL;
3129 a = d_right (a))
3131 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3132 return NULL;
3133 if (i <= 0)
3134 break;
3135 --i;
3137 if (i != 0 || a == NULL)
3138 return NULL;
3140 return d_left (a);
3143 /* Returns the template argument from the current context indicated by DC,
3144 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3146 static struct demangle_component *
3147 d_lookup_template_argument (struct d_print_info *dpi,
3148 const struct demangle_component *dc)
3150 if (dpi->templates == NULL)
3152 d_print_error (dpi);
3153 return NULL;
3156 return d_index_template_argument
3157 (d_right (dpi->templates->template_decl),
3158 dc->u.s_number.number);
3161 /* Returns a template argument pack used in DC (any will do), or NULL. */
3163 static struct demangle_component *
3164 d_find_pack (struct d_print_info *dpi,
3165 const struct demangle_component *dc)
3167 struct demangle_component *a;
3168 if (dc == NULL)
3169 return NULL;
3171 switch (dc->type)
3173 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3174 a = d_lookup_template_argument (dpi, dc);
3175 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3176 return a;
3177 return NULL;
3179 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3180 return NULL;
3182 case DEMANGLE_COMPONENT_NAME:
3183 case DEMANGLE_COMPONENT_OPERATOR:
3184 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3185 case DEMANGLE_COMPONENT_SUB_STD:
3186 case DEMANGLE_COMPONENT_CHARACTER:
3187 return NULL;
3189 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3190 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3191 case DEMANGLE_COMPONENT_CTOR:
3192 return d_find_pack (dpi, dc->u.s_ctor.name);
3193 case DEMANGLE_COMPONENT_DTOR:
3194 return d_find_pack (dpi, dc->u.s_dtor.name);
3196 default:
3197 a = d_find_pack (dpi, d_left (dc));
3198 if (a)
3199 return a;
3200 return d_find_pack (dpi, d_right (dc));
3204 /* Returns the length of the template argument pack DC. */
3206 static int
3207 d_pack_length (const struct demangle_component *dc)
3209 int count = 0;
3210 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3211 && d_left (dc) != NULL)
3213 ++count;
3214 dc = d_right (dc);
3216 return count;
3219 /* DC is a component of a mangled expression. Print it, wrapped in parens
3220 if needed. */
3222 static void
3223 d_print_subexpr (struct d_print_info *dpi,
3224 const struct demangle_component *dc)
3226 int simple = 0;
3227 if (dc->type == DEMANGLE_COMPONENT_NAME)
3228 simple = 1;
3229 if (!simple)
3230 d_append_char (dpi, '(');
3231 d_print_comp (dpi, dc);
3232 if (!simple)
3233 d_append_char (dpi, ')');
3236 /* Subroutine to handle components. */
3238 static void
3239 d_print_comp (struct d_print_info *dpi,
3240 const struct demangle_component *dc)
3242 if (dc == NULL)
3244 d_print_error (dpi);
3245 return;
3247 if (d_print_saw_error (dpi))
3248 return;
3250 switch (dc->type)
3252 case DEMANGLE_COMPONENT_NAME:
3253 if ((dpi->options & DMGL_JAVA) == 0)
3254 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3255 else
3256 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3257 return;
3259 case DEMANGLE_COMPONENT_QUAL_NAME:
3260 case DEMANGLE_COMPONENT_LOCAL_NAME:
3261 d_print_comp (dpi, d_left (dc));
3262 if ((dpi->options & DMGL_JAVA) == 0)
3263 d_append_string (dpi, "::");
3264 else
3265 d_append_char (dpi, '.');
3266 d_print_comp (dpi, d_right (dc));
3267 return;
3269 case DEMANGLE_COMPONENT_TYPED_NAME:
3271 struct d_print_mod *hold_modifiers;
3272 struct demangle_component *typed_name;
3273 struct d_print_mod adpm[4];
3274 unsigned int i;
3275 struct d_print_template dpt;
3277 /* Pass the name down to the type so that it can be printed in
3278 the right place for the type. We also have to pass down
3279 any CV-qualifiers, which apply to the this parameter. */
3280 hold_modifiers = dpi->modifiers;
3281 i = 0;
3282 typed_name = d_left (dc);
3283 while (typed_name != NULL)
3285 if (i >= sizeof adpm / sizeof adpm[0])
3287 d_print_error (dpi);
3288 return;
3291 adpm[i].next = dpi->modifiers;
3292 dpi->modifiers = &adpm[i];
3293 adpm[i].mod = typed_name;
3294 adpm[i].printed = 0;
3295 adpm[i].templates = dpi->templates;
3296 ++i;
3298 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3299 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3300 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3301 break;
3303 typed_name = d_left (typed_name);
3306 if (typed_name == NULL)
3308 d_print_error (dpi);
3309 return;
3312 /* If typed_name is a template, then it applies to the
3313 function type as well. */
3314 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3316 dpt.next = dpi->templates;
3317 dpi->templates = &dpt;
3318 dpt.template_decl = typed_name;
3321 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3322 there may be CV-qualifiers on its right argument which
3323 really apply here; this happens when parsing a class which
3324 is local to a function. */
3325 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3327 struct demangle_component *local_name;
3329 local_name = d_right (typed_name);
3330 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3331 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3332 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3334 if (i >= sizeof adpm / sizeof adpm[0])
3336 d_print_error (dpi);
3337 return;
3340 adpm[i] = adpm[i - 1];
3341 adpm[i].next = &adpm[i - 1];
3342 dpi->modifiers = &adpm[i];
3344 adpm[i - 1].mod = local_name;
3345 adpm[i - 1].printed = 0;
3346 adpm[i - 1].templates = dpi->templates;
3347 ++i;
3349 local_name = d_left (local_name);
3353 d_print_comp (dpi, d_right (dc));
3355 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3356 dpi->templates = dpt.next;
3358 /* If the modifiers didn't get printed by the type, print them
3359 now. */
3360 while (i > 0)
3362 --i;
3363 if (! adpm[i].printed)
3365 d_append_char (dpi, ' ');
3366 d_print_mod (dpi, adpm[i].mod);
3370 dpi->modifiers = hold_modifiers;
3372 return;
3375 case DEMANGLE_COMPONENT_TEMPLATE:
3377 struct d_print_mod *hold_dpm;
3378 struct demangle_component *dcl;
3380 /* Don't push modifiers into a template definition. Doing so
3381 could give the wrong definition for a template argument.
3382 Instead, treat the template essentially as a name. */
3384 hold_dpm = dpi->modifiers;
3385 dpi->modifiers = NULL;
3387 dcl = d_left (dc);
3389 if ((dpi->options & DMGL_JAVA) != 0
3390 && dcl->type == DEMANGLE_COMPONENT_NAME
3391 && dcl->u.s_name.len == 6
3392 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3394 /* Special-case Java arrays, so that JArray<TYPE> appears
3395 instead as TYPE[]. */
3397 d_print_comp (dpi, d_right (dc));
3398 d_append_string (dpi, "[]");
3400 else
3402 d_print_comp (dpi, dcl);
3403 if (d_last_char (dpi) == '<')
3404 d_append_char (dpi, ' ');
3405 d_append_char (dpi, '<');
3406 d_print_comp (dpi, d_right (dc));
3407 /* Avoid generating two consecutive '>' characters, to avoid
3408 the C++ syntactic ambiguity. */
3409 if (d_last_char (dpi) == '>')
3410 d_append_char (dpi, ' ');
3411 d_append_char (dpi, '>');
3414 dpi->modifiers = hold_dpm;
3416 return;
3419 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3421 struct d_print_template *hold_dpt;
3422 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3424 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3425 a = d_index_template_argument (a, dpi->pack_index);
3427 if (a == NULL)
3429 d_print_error (dpi);
3430 return;
3433 /* While processing this parameter, we need to pop the list of
3434 templates. This is because the template parameter may
3435 itself be a reference to a parameter of an outer
3436 template. */
3438 hold_dpt = dpi->templates;
3439 dpi->templates = hold_dpt->next;
3441 d_print_comp (dpi, a);
3443 dpi->templates = hold_dpt;
3445 return;
3448 case DEMANGLE_COMPONENT_CTOR:
3449 d_print_comp (dpi, dc->u.s_ctor.name);
3450 return;
3452 case DEMANGLE_COMPONENT_DTOR:
3453 d_append_char (dpi, '~');
3454 d_print_comp (dpi, dc->u.s_dtor.name);
3455 return;
3457 case DEMANGLE_COMPONENT_VTABLE:
3458 d_append_string (dpi, "vtable for ");
3459 d_print_comp (dpi, d_left (dc));
3460 return;
3462 case DEMANGLE_COMPONENT_VTT:
3463 d_append_string (dpi, "VTT for ");
3464 d_print_comp (dpi, d_left (dc));
3465 return;
3467 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3468 d_append_string (dpi, "construction vtable for ");
3469 d_print_comp (dpi, d_left (dc));
3470 d_append_string (dpi, "-in-");
3471 d_print_comp (dpi, d_right (dc));
3472 return;
3474 case DEMANGLE_COMPONENT_TYPEINFO:
3475 d_append_string (dpi, "typeinfo for ");
3476 d_print_comp (dpi, d_left (dc));
3477 return;
3479 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3480 d_append_string (dpi, "typeinfo name for ");
3481 d_print_comp (dpi, d_left (dc));
3482 return;
3484 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3485 d_append_string (dpi, "typeinfo fn for ");
3486 d_print_comp (dpi, d_left (dc));
3487 return;
3489 case DEMANGLE_COMPONENT_THUNK:
3490 d_append_string (dpi, "non-virtual thunk to ");
3491 d_print_comp (dpi, d_left (dc));
3492 return;
3494 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3495 d_append_string (dpi, "virtual thunk to ");
3496 d_print_comp (dpi, d_left (dc));
3497 return;
3499 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3500 d_append_string (dpi, "covariant return thunk to ");
3501 d_print_comp (dpi, d_left (dc));
3502 return;
3504 case DEMANGLE_COMPONENT_JAVA_CLASS:
3505 d_append_string (dpi, "java Class for ");
3506 d_print_comp (dpi, d_left (dc));
3507 return;
3509 case DEMANGLE_COMPONENT_GUARD:
3510 d_append_string (dpi, "guard variable for ");
3511 d_print_comp (dpi, d_left (dc));
3512 return;
3514 case DEMANGLE_COMPONENT_REFTEMP:
3515 d_append_string (dpi, "reference temporary for ");
3516 d_print_comp (dpi, d_left (dc));
3517 return;
3519 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3520 d_append_string (dpi, "hidden alias for ");
3521 d_print_comp (dpi, d_left (dc));
3522 return;
3524 case DEMANGLE_COMPONENT_SUB_STD:
3525 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3526 return;
3528 case DEMANGLE_COMPONENT_RESTRICT:
3529 case DEMANGLE_COMPONENT_VOLATILE:
3530 case DEMANGLE_COMPONENT_CONST:
3532 struct d_print_mod *pdpm;
3534 /* When printing arrays, it's possible to have cases where the
3535 same CV-qualifier gets pushed on the stack multiple times.
3536 We only need to print it once. */
3538 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3540 if (! pdpm->printed)
3542 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3543 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3544 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3545 break;
3546 if (pdpm->mod->type == dc->type)
3548 d_print_comp (dpi, d_left (dc));
3549 return;
3554 /* Fall through. */
3555 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3556 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3557 case DEMANGLE_COMPONENT_CONST_THIS:
3558 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3559 case DEMANGLE_COMPONENT_POINTER:
3560 case DEMANGLE_COMPONENT_REFERENCE:
3561 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3562 case DEMANGLE_COMPONENT_COMPLEX:
3563 case DEMANGLE_COMPONENT_IMAGINARY:
3565 /* We keep a list of modifiers on the stack. */
3566 struct d_print_mod dpm;
3568 dpm.next = dpi->modifiers;
3569 dpi->modifiers = &dpm;
3570 dpm.mod = dc;
3571 dpm.printed = 0;
3572 dpm.templates = dpi->templates;
3574 d_print_comp (dpi, d_left (dc));
3576 /* If the modifier didn't get printed by the type, print it
3577 now. */
3578 if (! dpm.printed)
3579 d_print_mod (dpi, dc);
3581 dpi->modifiers = dpm.next;
3583 return;
3586 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3587 if ((dpi->options & DMGL_JAVA) == 0)
3588 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3589 dc->u.s_builtin.type->len);
3590 else
3591 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3592 dc->u.s_builtin.type->java_len);
3593 return;
3595 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3596 d_print_comp (dpi, d_left (dc));
3597 return;
3599 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3601 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3602 d_print_function_type (dpi, dc, dpi->modifiers);
3604 /* Print return type if present */
3605 if (d_left (dc) != NULL)
3607 struct d_print_mod dpm;
3609 /* We must pass this type down as a modifier in order to
3610 print it in the right location. */
3611 dpm.next = dpi->modifiers;
3612 dpi->modifiers = &dpm;
3613 dpm.mod = dc;
3614 dpm.printed = 0;
3615 dpm.templates = dpi->templates;
3617 d_print_comp (dpi, d_left (dc));
3619 dpi->modifiers = dpm.next;
3621 if (dpm.printed)
3622 return;
3624 /* In standard prefix notation, there is a space between the
3625 return type and the function signature. */
3626 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3627 d_append_char (dpi, ' ');
3630 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3631 d_print_function_type (dpi, dc, dpi->modifiers);
3633 return;
3636 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3638 struct d_print_mod *hold_modifiers;
3639 struct d_print_mod adpm[4];
3640 unsigned int i;
3641 struct d_print_mod *pdpm;
3643 /* We must pass this type down as a modifier in order to print
3644 multi-dimensional arrays correctly. If the array itself is
3645 CV-qualified, we act as though the element type were
3646 CV-qualified. We do this by copying the modifiers down
3647 rather than fiddling pointers, so that we don't wind up
3648 with a d_print_mod higher on the stack pointing into our
3649 stack frame after we return. */
3651 hold_modifiers = dpi->modifiers;
3653 adpm[0].next = hold_modifiers;
3654 dpi->modifiers = &adpm[0];
3655 adpm[0].mod = dc;
3656 adpm[0].printed = 0;
3657 adpm[0].templates = dpi->templates;
3659 i = 1;
3660 pdpm = hold_modifiers;
3661 while (pdpm != NULL
3662 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3663 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3664 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3666 if (! pdpm->printed)
3668 if (i >= sizeof adpm / sizeof adpm[0])
3670 d_print_error (dpi);
3671 return;
3674 adpm[i] = *pdpm;
3675 adpm[i].next = dpi->modifiers;
3676 dpi->modifiers = &adpm[i];
3677 pdpm->printed = 1;
3678 ++i;
3681 pdpm = pdpm->next;
3684 d_print_comp (dpi, d_right (dc));
3686 dpi->modifiers = hold_modifiers;
3688 if (adpm[0].printed)
3689 return;
3691 while (i > 1)
3693 --i;
3694 d_print_mod (dpi, adpm[i].mod);
3697 d_print_array_type (dpi, dc, dpi->modifiers);
3699 return;
3702 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3704 struct d_print_mod dpm;
3706 dpm.next = dpi->modifiers;
3707 dpi->modifiers = &dpm;
3708 dpm.mod = dc;
3709 dpm.printed = 0;
3710 dpm.templates = dpi->templates;
3712 d_print_comp (dpi, d_right (dc));
3714 /* If the modifier didn't get printed by the type, print it
3715 now. */
3716 if (! dpm.printed)
3718 d_append_char (dpi, ' ');
3719 d_print_comp (dpi, d_left (dc));
3720 d_append_string (dpi, "::*");
3723 dpi->modifiers = dpm.next;
3725 return;
3728 case DEMANGLE_COMPONENT_ARGLIST:
3729 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3730 if (d_left (dc) != NULL)
3731 d_print_comp (dpi, d_left (dc));
3732 if (d_right (dc) != NULL)
3734 d_append_string (dpi, ", ");
3735 d_print_comp (dpi, d_right (dc));
3737 return;
3739 case DEMANGLE_COMPONENT_OPERATOR:
3741 char c;
3743 d_append_string (dpi, "operator");
3744 c = dc->u.s_operator.op->name[0];
3745 if (IS_LOWER (c))
3746 d_append_char (dpi, ' ');
3747 d_append_buffer (dpi, dc->u.s_operator.op->name,
3748 dc->u.s_operator.op->len);
3749 return;
3752 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3753 d_append_string (dpi, "operator ");
3754 d_print_comp (dpi, dc->u.s_extended_operator.name);
3755 return;
3757 case DEMANGLE_COMPONENT_CAST:
3758 d_append_string (dpi, "operator ");
3759 d_print_cast (dpi, dc);
3760 return;
3762 case DEMANGLE_COMPONENT_UNARY:
3763 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3764 d_print_expr_op (dpi, d_left (dc));
3765 else
3767 d_append_char (dpi, '(');
3768 d_print_cast (dpi, d_left (dc));
3769 d_append_char (dpi, ')');
3771 if (d_left (dc)->type == DEMANGLE_COMPONENT_CAST
3772 && d_right (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3773 /* type() -- FIXME what about type(multiple,args) */
3774 d_append_string (dpi, "()");
3775 else
3776 d_print_subexpr (dpi, d_right (dc));
3777 return;
3779 case DEMANGLE_COMPONENT_BINARY:
3780 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3782 d_print_error (dpi);
3783 return;
3786 /* We wrap an expression which uses the greater-than operator in
3787 an extra layer of parens so that it does not get confused
3788 with the '>' which ends the template parameters. */
3789 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3790 && d_left (dc)->u.s_operator.op->len == 1
3791 && d_left (dc)->u.s_operator.op->name[0] == '>')
3792 d_append_char (dpi, '(');
3794 d_print_subexpr (dpi, d_left (d_right (dc)));
3795 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
3796 d_print_expr_op (dpi, d_left (dc));
3797 d_print_subexpr (dpi, d_right (d_right (dc)));
3799 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3800 && d_left (dc)->u.s_operator.op->len == 1
3801 && d_left (dc)->u.s_operator.op->name[0] == '>')
3802 d_append_char (dpi, ')');
3804 return;
3806 case DEMANGLE_COMPONENT_BINARY_ARGS:
3807 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3808 d_print_error (dpi);
3809 return;
3811 case DEMANGLE_COMPONENT_TRINARY:
3812 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3813 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3815 d_print_error (dpi);
3816 return;
3818 d_print_subexpr (dpi, d_left (d_right (dc)));
3819 d_print_expr_op (dpi, d_left (dc));
3820 d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
3821 d_append_string (dpi, " : ");
3822 d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
3823 return;
3825 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3826 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3827 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3828 d_print_error (dpi);
3829 return;
3831 case DEMANGLE_COMPONENT_LITERAL:
3832 case DEMANGLE_COMPONENT_LITERAL_NEG:
3834 enum d_builtin_type_print tp;
3836 /* For some builtin types, produce simpler output. */
3837 tp = D_PRINT_DEFAULT;
3838 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3840 tp = d_left (dc)->u.s_builtin.type->print;
3841 switch (tp)
3843 case D_PRINT_INT:
3844 case D_PRINT_UNSIGNED:
3845 case D_PRINT_LONG:
3846 case D_PRINT_UNSIGNED_LONG:
3847 case D_PRINT_LONG_LONG:
3848 case D_PRINT_UNSIGNED_LONG_LONG:
3849 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3851 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3852 d_append_char (dpi, '-');
3853 d_print_comp (dpi, d_right (dc));
3854 switch (tp)
3856 default:
3857 break;
3858 case D_PRINT_UNSIGNED:
3859 d_append_char (dpi, 'u');
3860 break;
3861 case D_PRINT_LONG:
3862 d_append_char (dpi, 'l');
3863 break;
3864 case D_PRINT_UNSIGNED_LONG:
3865 d_append_string (dpi, "ul");
3866 break;
3867 case D_PRINT_LONG_LONG:
3868 d_append_string (dpi, "ll");
3869 break;
3870 case D_PRINT_UNSIGNED_LONG_LONG:
3871 d_append_string (dpi, "ull");
3872 break;
3874 return;
3876 break;
3878 case D_PRINT_BOOL:
3879 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3880 && d_right (dc)->u.s_name.len == 1
3881 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3883 switch (d_right (dc)->u.s_name.s[0])
3885 case '0':
3886 d_append_string (dpi, "false");
3887 return;
3888 case '1':
3889 d_append_string (dpi, "true");
3890 return;
3891 default:
3892 break;
3895 break;
3897 default:
3898 break;
3902 d_append_char (dpi, '(');
3903 d_print_comp (dpi, d_left (dc));
3904 d_append_char (dpi, ')');
3905 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3906 d_append_char (dpi, '-');
3907 if (tp == D_PRINT_FLOAT)
3908 d_append_char (dpi, '[');
3909 d_print_comp (dpi, d_right (dc));
3910 if (tp == D_PRINT_FLOAT)
3911 d_append_char (dpi, ']');
3913 return;
3915 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3916 d_append_string (dpi, "java resource ");
3917 d_print_comp (dpi, d_left (dc));
3918 return;
3920 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3921 d_print_comp (dpi, d_left (dc));
3922 d_print_comp (dpi, d_right (dc));
3923 return;
3925 case DEMANGLE_COMPONENT_CHARACTER:
3926 d_append_char (dpi, dc->u.s_character.character);
3927 return;
3929 case DEMANGLE_COMPONENT_DECLTYPE:
3930 d_append_string (dpi, "decltype (");
3931 d_print_comp (dpi, d_left (dc));
3932 d_append_char (dpi, ')');
3933 return;
3935 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3937 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
3938 int len = d_pack_length (a);
3939 int i;
3941 dc = d_left (dc);
3942 for (i = 0; i < len; ++i)
3944 dpi->pack_index = i;
3945 d_print_comp (dpi, dc);
3946 if (i < len-1)
3947 d_append_string (dpi, ", ");
3950 return;
3952 default:
3953 d_print_error (dpi);
3954 return;
3958 /* Print a Java dentifier. For Java we try to handle encoded extended
3959 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3960 so we don't it for C++. Characters are encoded as
3961 __U<hex-char>+_. */
3963 static void
3964 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3966 const char *p;
3967 const char *end;
3969 end = name + len;
3970 for (p = name; p < end; ++p)
3972 if (end - p > 3
3973 && p[0] == '_'
3974 && p[1] == '_'
3975 && p[2] == 'U')
3977 unsigned long c;
3978 const char *q;
3980 c = 0;
3981 for (q = p + 3; q < end; ++q)
3983 int dig;
3985 if (IS_DIGIT (*q))
3986 dig = *q - '0';
3987 else if (*q >= 'A' && *q <= 'F')
3988 dig = *q - 'A' + 10;
3989 else if (*q >= 'a' && *q <= 'f')
3990 dig = *q - 'a' + 10;
3991 else
3992 break;
3994 c = c * 16 + dig;
3996 /* If the Unicode character is larger than 256, we don't try
3997 to deal with it here. FIXME. */
3998 if (q < end && *q == '_' && c < 256)
4000 d_append_char (dpi, c);
4001 p = q;
4002 continue;
4006 d_append_char (dpi, *p);
4010 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4011 qualifiers on this after printing a function. */
4013 static void
4014 d_print_mod_list (struct d_print_info *dpi,
4015 struct d_print_mod *mods, int suffix)
4017 struct d_print_template *hold_dpt;
4019 if (mods == NULL || d_print_saw_error (dpi))
4020 return;
4022 if (mods->printed
4023 || (! suffix
4024 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4025 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4026 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4028 d_print_mod_list (dpi, mods->next, suffix);
4029 return;
4032 mods->printed = 1;
4034 hold_dpt = dpi->templates;
4035 dpi->templates = mods->templates;
4037 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4039 d_print_function_type (dpi, mods->mod, mods->next);
4040 dpi->templates = hold_dpt;
4041 return;
4043 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4045 d_print_array_type (dpi, mods->mod, mods->next);
4046 dpi->templates = hold_dpt;
4047 return;
4049 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4051 struct d_print_mod *hold_modifiers;
4052 struct demangle_component *dc;
4054 /* When this is on the modifier stack, we have pulled any
4055 qualifiers off the right argument already. Otherwise, we
4056 print it as usual, but don't let the left argument see any
4057 modifiers. */
4059 hold_modifiers = dpi->modifiers;
4060 dpi->modifiers = NULL;
4061 d_print_comp (dpi, d_left (mods->mod));
4062 dpi->modifiers = hold_modifiers;
4064 if ((dpi->options & DMGL_JAVA) == 0)
4065 d_append_string (dpi, "::");
4066 else
4067 d_append_char (dpi, '.');
4069 dc = d_right (mods->mod);
4070 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4071 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4072 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4073 dc = d_left (dc);
4075 d_print_comp (dpi, dc);
4077 dpi->templates = hold_dpt;
4078 return;
4081 d_print_mod (dpi, mods->mod);
4083 dpi->templates = hold_dpt;
4085 d_print_mod_list (dpi, mods->next, suffix);
4088 /* Print a modifier. */
4090 static void
4091 d_print_mod (struct d_print_info *dpi,
4092 const struct demangle_component *mod)
4094 switch (mod->type)
4096 case DEMANGLE_COMPONENT_RESTRICT:
4097 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4098 d_append_string (dpi, " restrict");
4099 return;
4100 case DEMANGLE_COMPONENT_VOLATILE:
4101 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4102 d_append_string (dpi, " volatile");
4103 return;
4104 case DEMANGLE_COMPONENT_CONST:
4105 case DEMANGLE_COMPONENT_CONST_THIS:
4106 d_append_string (dpi, " const");
4107 return;
4108 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4109 d_append_char (dpi, ' ');
4110 d_print_comp (dpi, d_right (mod));
4111 return;
4112 case DEMANGLE_COMPONENT_POINTER:
4113 /* There is no pointer symbol in Java. */
4114 if ((dpi->options & DMGL_JAVA) == 0)
4115 d_append_char (dpi, '*');
4116 return;
4117 case DEMANGLE_COMPONENT_REFERENCE:
4118 d_append_char (dpi, '&');
4119 return;
4120 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4121 d_append_string (dpi, "&&");
4122 return;
4123 case DEMANGLE_COMPONENT_COMPLEX:
4124 d_append_string (dpi, "complex ");
4125 return;
4126 case DEMANGLE_COMPONENT_IMAGINARY:
4127 d_append_string (dpi, "imaginary ");
4128 return;
4129 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4130 if (d_last_char (dpi) != '(')
4131 d_append_char (dpi, ' ');
4132 d_print_comp (dpi, d_left (mod));
4133 d_append_string (dpi, "::*");
4134 return;
4135 case DEMANGLE_COMPONENT_TYPED_NAME:
4136 d_print_comp (dpi, d_left (mod));
4137 return;
4138 default:
4139 /* Otherwise, we have something that won't go back on the
4140 modifier stack, so we can just print it. */
4141 d_print_comp (dpi, mod);
4142 return;
4146 /* Print a function type, except for the return type. */
4148 static void
4149 d_print_function_type (struct d_print_info *dpi,
4150 const struct demangle_component *dc,
4151 struct d_print_mod *mods)
4153 int need_paren;
4154 int saw_mod;
4155 int need_space;
4156 struct d_print_mod *p;
4157 struct d_print_mod *hold_modifiers;
4159 need_paren = 0;
4160 saw_mod = 0;
4161 need_space = 0;
4162 for (p = mods; p != NULL; p = p->next)
4164 if (p->printed)
4165 break;
4167 saw_mod = 1;
4168 switch (p->mod->type)
4170 case DEMANGLE_COMPONENT_POINTER:
4171 case DEMANGLE_COMPONENT_REFERENCE:
4172 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4173 need_paren = 1;
4174 break;
4175 case DEMANGLE_COMPONENT_RESTRICT:
4176 case DEMANGLE_COMPONENT_VOLATILE:
4177 case DEMANGLE_COMPONENT_CONST:
4178 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4179 case DEMANGLE_COMPONENT_COMPLEX:
4180 case DEMANGLE_COMPONENT_IMAGINARY:
4181 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4182 need_space = 1;
4183 need_paren = 1;
4184 break;
4185 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4186 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4187 case DEMANGLE_COMPONENT_CONST_THIS:
4188 break;
4189 default:
4190 break;
4192 if (need_paren)
4193 break;
4196 if (d_left (dc) != NULL && ! saw_mod)
4197 need_paren = 1;
4199 if (need_paren)
4201 if (! need_space)
4203 if (d_last_char (dpi) != '('
4204 && d_last_char (dpi) != '*')
4205 need_space = 1;
4207 if (need_space && d_last_char (dpi) != ' ')
4208 d_append_char (dpi, ' ');
4209 d_append_char (dpi, '(');
4212 hold_modifiers = dpi->modifiers;
4213 dpi->modifiers = NULL;
4215 d_print_mod_list (dpi, mods, 0);
4217 if (need_paren)
4218 d_append_char (dpi, ')');
4220 d_append_char (dpi, '(');
4222 if (d_right (dc) != NULL)
4223 d_print_comp (dpi, d_right (dc));
4225 d_append_char (dpi, ')');
4227 d_print_mod_list (dpi, mods, 1);
4229 dpi->modifiers = hold_modifiers;
4232 /* Print an array type, except for the element type. */
4234 static void
4235 d_print_array_type (struct d_print_info *dpi,
4236 const struct demangle_component *dc,
4237 struct d_print_mod *mods)
4239 int need_space;
4241 need_space = 1;
4242 if (mods != NULL)
4244 int need_paren;
4245 struct d_print_mod *p;
4247 need_paren = 0;
4248 for (p = mods; p != NULL; p = p->next)
4250 if (! p->printed)
4252 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4254 need_space = 0;
4255 break;
4257 else
4259 need_paren = 1;
4260 need_space = 1;
4261 break;
4266 if (need_paren)
4267 d_append_string (dpi, " (");
4269 d_print_mod_list (dpi, mods, 0);
4271 if (need_paren)
4272 d_append_char (dpi, ')');
4275 if (need_space)
4276 d_append_char (dpi, ' ');
4278 d_append_char (dpi, '[');
4280 if (d_left (dc) != NULL)
4281 d_print_comp (dpi, d_left (dc));
4283 d_append_char (dpi, ']');
4286 /* Print an operator in an expression. */
4288 static void
4289 d_print_expr_op (struct d_print_info *dpi,
4290 const struct demangle_component *dc)
4292 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4293 d_append_buffer (dpi, dc->u.s_operator.op->name,
4294 dc->u.s_operator.op->len);
4295 else
4296 d_print_comp (dpi, dc);
4299 /* Print a cast. */
4301 static void
4302 d_print_cast (struct d_print_info *dpi,
4303 const struct demangle_component *dc)
4305 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4306 d_print_comp (dpi, d_left (dc));
4307 else
4309 struct d_print_mod *hold_dpm;
4310 struct d_print_template dpt;
4312 /* It appears that for a templated cast operator, we need to put
4313 the template parameters in scope for the operator name, but
4314 not for the parameters. The effect is that we need to handle
4315 the template printing here. */
4317 hold_dpm = dpi->modifiers;
4318 dpi->modifiers = NULL;
4320 dpt.next = dpi->templates;
4321 dpi->templates = &dpt;
4322 dpt.template_decl = d_left (dc);
4324 d_print_comp (dpi, d_left (d_left (dc)));
4326 dpi->templates = dpt.next;
4328 if (d_last_char (dpi) == '<')
4329 d_append_char (dpi, ' ');
4330 d_append_char (dpi, '<');
4331 d_print_comp (dpi, d_right (d_left (dc)));
4332 /* Avoid generating two consecutive '>' characters, to avoid
4333 the C++ syntactic ambiguity. */
4334 if (d_last_char (dpi) == '>')
4335 d_append_char (dpi, ' ');
4336 d_append_char (dpi, '>');
4338 dpi->modifiers = hold_dpm;
4342 /* Initialize the information structure we use to pass around
4343 information. */
4345 CP_STATIC_IF_GLIBCPP_V3
4346 void
4347 cplus_demangle_init_info (const char *mangled, int options, size_t len,
4348 struct d_info *di)
4350 di->s = mangled;
4351 di->send = mangled + len;
4352 di->options = options;
4354 di->n = mangled;
4356 /* We can not need more components than twice the number of chars in
4357 the mangled string. Most components correspond directly to
4358 chars, but the ARGLIST types are exceptions. */
4359 di->num_comps = 2 * len;
4360 di->next_comp = 0;
4362 /* Similarly, we can not need more substitutions than there are
4363 chars in the mangled string. */
4364 di->num_subs = len;
4365 di->next_sub = 0;
4366 di->did_subs = 0;
4368 di->last_name = NULL;
4370 di->expansion = 0;
4373 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4374 mangled name, return strings in repeated callback giving the demangled
4375 name. OPTIONS is the usual libiberty demangler options. On success,
4376 this returns 1. On failure, returns 0. */
4378 static int
4379 d_demangle_callback (const char *mangled, int options,
4380 demangle_callbackref callback, void *opaque)
4382 int type;
4383 struct d_info di;
4384 struct demangle_component *dc;
4385 int status;
4387 if (mangled[0] == '_' && mangled[1] == 'Z')
4388 type = 0;
4389 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4390 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4391 && (mangled[9] == 'D' || mangled[9] == 'I')
4392 && mangled[10] == '_')
4394 const char *intro;
4396 intro = (mangled[9] == 'I')
4397 ? "global constructors keyed to "
4398 : "global destructors keyed to ";
4400 callback (intro, strlen (intro), opaque);
4401 callback (mangled + 11, strlen (mangled + 11), opaque);
4402 return 1;
4404 else
4406 if ((options & DMGL_TYPES) == 0)
4407 return 0;
4408 type = 1;
4411 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4414 #ifdef CP_DYNAMIC_ARRAYS
4415 __extension__ struct demangle_component comps[di.num_comps];
4416 __extension__ struct demangle_component *subs[di.num_subs];
4418 di.comps = comps;
4419 di.subs = subs;
4420 #else
4421 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4422 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4423 #endif
4425 if (type)
4426 dc = cplus_demangle_type (&di);
4427 else
4428 dc = cplus_demangle_mangled_name (&di, 1);
4430 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4431 mangled string, then we didn't successfully demangle it. If
4432 DMGL_PARAMS is not set, we didn't look at the trailing
4433 parameters. */
4434 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4435 dc = NULL;
4437 #ifdef CP_DEMANGLE_DEBUG
4438 d_dump (dc, 0);
4439 #endif
4441 status = (dc != NULL)
4442 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4443 : 0;
4446 return status;
4449 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4450 name, return a buffer allocated with malloc holding the demangled
4451 name. OPTIONS is the usual libiberty demangler options. On
4452 success, this sets *PALC to the allocated size of the returned
4453 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4454 a memory allocation failure, and returns NULL. */
4456 static char *
4457 d_demangle (const char *mangled, int options, size_t *palc)
4459 struct d_growable_string dgs;
4460 int status;
4462 d_growable_string_init (&dgs, 0);
4464 status = d_demangle_callback (mangled, options,
4465 d_growable_string_callback_adapter, &dgs);
4466 if (status == 0)
4468 free (dgs.buf);
4469 *palc = 0;
4470 return NULL;
4473 *palc = dgs.allocation_failure ? 1 : 0;
4474 return dgs.buf;
4477 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4479 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4481 /* ia64 ABI-mandated entry point in the C++ runtime library for
4482 performing demangling. MANGLED_NAME is a NUL-terminated character
4483 string containing the name to be demangled.
4485 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4486 *LENGTH bytes, into which the demangled name is stored. If
4487 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4488 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4489 is placed in a region of memory allocated with malloc.
4491 If LENGTH is non-NULL, the length of the buffer containing the
4492 demangled name, is placed in *LENGTH.
4494 The return value is a pointer to the start of the NUL-terminated
4495 demangled name, or NULL if the demangling fails. The caller is
4496 responsible for deallocating this memory using free.
4498 *STATUS is set to one of the following values:
4499 0: The demangling operation succeeded.
4500 -1: A memory allocation failure occurred.
4501 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4502 -3: One of the arguments is invalid.
4504 The demangling is performed using the C++ ABI mangling rules, with
4505 GNU extensions. */
4507 char *
4508 __cxa_demangle (const char *mangled_name, char *output_buffer,
4509 size_t *length, int *status)
4511 char *demangled;
4512 size_t alc;
4514 if (mangled_name == NULL)
4516 if (status != NULL)
4517 *status = -3;
4518 return NULL;
4521 if (output_buffer != NULL && length == NULL)
4523 if (status != NULL)
4524 *status = -3;
4525 return NULL;
4528 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4530 if (demangled == NULL)
4532 if (status != NULL)
4534 if (alc == 1)
4535 *status = -1;
4536 else
4537 *status = -2;
4539 return NULL;
4542 if (output_buffer == NULL)
4544 if (length != NULL)
4545 *length = alc;
4547 else
4549 if (strlen (demangled) < *length)
4551 strcpy (output_buffer, demangled);
4552 free (demangled);
4553 demangled = output_buffer;
4555 else
4557 free (output_buffer);
4558 *length = alc;
4562 if (status != NULL)
4563 *status = 0;
4565 return demangled;
4568 extern int __gcclibcxx_demangle_callback (const char *,
4569 void (*)
4570 (const char *, size_t, void *),
4571 void *);
4573 /* Alternative, allocationless entry point in the C++ runtime library
4574 for performing demangling. MANGLED_NAME is a NUL-terminated character
4575 string containing the name to be demangled.
4577 CALLBACK is a callback function, called with demangled string
4578 segments as demangling progresses; it is called at least once,
4579 but may be called more than once. OPAQUE is a generalized pointer
4580 used as a callback argument.
4582 The return code is one of the following values, equivalent to
4583 the STATUS values of __cxa_demangle() (excluding -1, since this
4584 function performs no memory allocations):
4585 0: The demangling operation succeeded.
4586 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4587 -3: One of the arguments is invalid.
4589 The demangling is performed using the C++ ABI mangling rules, with
4590 GNU extensions. */
4593 __gcclibcxx_demangle_callback (const char *mangled_name,
4594 void (*callback) (const char *, size_t, void *),
4595 void *opaque)
4597 int status;
4599 if (mangled_name == NULL || callback == NULL)
4600 return -3;
4602 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
4603 callback, opaque);
4604 if (status == 0)
4605 return -2;
4607 return 0;
4610 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4612 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4613 mangled name, return a buffer allocated with malloc holding the
4614 demangled name. Otherwise, return NULL. */
4616 char *
4617 cplus_demangle_v3 (const char *mangled, int options)
4619 size_t alc;
4621 return d_demangle (mangled, options, &alc);
4625 cplus_demangle_v3_callback (const char *mangled, int options,
4626 demangle_callbackref callback, void *opaque)
4628 return d_demangle_callback (mangled, options, callback, opaque);
4631 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4632 conventions, but the output formatting is a little different.
4633 This instructs the C++ demangler not to emit pointer characters ("*"), to
4634 use Java's namespace separator symbol ("." instead of "::"), and to output
4635 JArray<TYPE> as TYPE[]. */
4637 char *
4638 java_demangle_v3 (const char *mangled)
4640 size_t alc;
4642 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
4646 java_demangle_v3_callback (const char *mangled,
4647 demangle_callbackref callback, void *opaque)
4649 return d_demangle_callback (mangled,
4650 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4651 callback, opaque);
4654 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4656 #ifndef IN_GLIBCPP_V3
4658 /* Demangle a string in order to find out whether it is a constructor
4659 or destructor. Return non-zero on success. Set *CTOR_KIND and
4660 *DTOR_KIND appropriately. */
4662 static int
4663 is_ctor_or_dtor (const char *mangled,
4664 enum gnu_v3_ctor_kinds *ctor_kind,
4665 enum gnu_v3_dtor_kinds *dtor_kind)
4667 struct d_info di;
4668 struct demangle_component *dc;
4669 int ret;
4671 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4672 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4674 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4677 #ifdef CP_DYNAMIC_ARRAYS
4678 __extension__ struct demangle_component comps[di.num_comps];
4679 __extension__ struct demangle_component *subs[di.num_subs];
4681 di.comps = comps;
4682 di.subs = subs;
4683 #else
4684 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4685 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4686 #endif
4688 dc = cplus_demangle_mangled_name (&di, 1);
4690 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4691 to demangle the entire string. */
4693 ret = 0;
4694 while (dc != NULL)
4696 switch (dc->type)
4698 default:
4699 dc = NULL;
4700 break;
4701 case DEMANGLE_COMPONENT_TYPED_NAME:
4702 case DEMANGLE_COMPONENT_TEMPLATE:
4703 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4704 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4705 case DEMANGLE_COMPONENT_CONST_THIS:
4706 dc = d_left (dc);
4707 break;
4708 case DEMANGLE_COMPONENT_QUAL_NAME:
4709 case DEMANGLE_COMPONENT_LOCAL_NAME:
4710 dc = d_right (dc);
4711 break;
4712 case DEMANGLE_COMPONENT_CTOR:
4713 *ctor_kind = dc->u.s_ctor.kind;
4714 ret = 1;
4715 dc = NULL;
4716 break;
4717 case DEMANGLE_COMPONENT_DTOR:
4718 *dtor_kind = dc->u.s_dtor.kind;
4719 ret = 1;
4720 dc = NULL;
4721 break;
4726 return ret;
4729 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4730 name. A non-zero return indicates the type of constructor. */
4732 enum gnu_v3_ctor_kinds
4733 is_gnu_v3_mangled_ctor (const char *name)
4735 enum gnu_v3_ctor_kinds ctor_kind;
4736 enum gnu_v3_dtor_kinds dtor_kind;
4738 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4739 return (enum gnu_v3_ctor_kinds) 0;
4740 return ctor_kind;
4744 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4745 name. A non-zero return indicates the type of destructor. */
4747 enum gnu_v3_dtor_kinds
4748 is_gnu_v3_mangled_dtor (const char *name)
4750 enum gnu_v3_ctor_kinds ctor_kind;
4751 enum gnu_v3_dtor_kinds dtor_kind;
4753 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4754 return (enum gnu_v3_dtor_kinds) 0;
4755 return dtor_kind;
4758 #endif /* IN_GLIBCPP_V3 */
4760 #ifdef STANDALONE_DEMANGLER
4762 #include "getopt.h"
4763 #include "dyn-string.h"
4765 static void print_usage (FILE* fp, int exit_value);
4767 #define IS_ALPHA(CHAR) \
4768 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4769 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4771 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4772 #define is_mangled_char(CHAR) \
4773 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4774 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4776 /* The name of this program, as invoked. */
4777 const char* program_name;
4779 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4781 static void
4782 print_usage (FILE* fp, int exit_value)
4784 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4785 fprintf (fp, "Options:\n");
4786 fprintf (fp, " -h,--help Display this message.\n");
4787 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4788 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4789 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4791 exit (exit_value);
4794 /* Option specification for getopt_long. */
4795 static const struct option long_options[] =
4797 { "help", no_argument, NULL, 'h' },
4798 { "no-params", no_argument, NULL, 'p' },
4799 { "verbose", no_argument, NULL, 'v' },
4800 { NULL, no_argument, NULL, 0 },
4803 /* Main entry for a demangling filter executable. It will demangle
4804 its command line arguments, if any. If none are provided, it will
4805 filter stdin to stdout, replacing any recognized mangled C++ names
4806 with their demangled equivalents. */
4809 main (int argc, char *argv[])
4811 int i;
4812 int opt_char;
4813 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4815 /* Use the program name of this program, as invoked. */
4816 program_name = argv[0];
4818 /* Parse options. */
4821 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4822 switch (opt_char)
4824 case '?': /* Unrecognized option. */
4825 print_usage (stderr, 1);
4826 break;
4828 case 'h':
4829 print_usage (stdout, 0);
4830 break;
4832 case 'p':
4833 options &= ~ DMGL_PARAMS;
4834 break;
4836 case 'v':
4837 options |= DMGL_VERBOSE;
4838 break;
4841 while (opt_char != -1);
4843 if (optind == argc)
4844 /* No command line arguments were provided. Filter stdin. */
4846 dyn_string_t mangled = dyn_string_new (3);
4847 char *s;
4849 /* Read all of input. */
4850 while (!feof (stdin))
4852 char c;
4854 /* Pile characters into mangled until we hit one that can't
4855 occur in a mangled name. */
4856 c = getchar ();
4857 while (!feof (stdin) && is_mangled_char (c))
4859 dyn_string_append_char (mangled, c);
4860 if (feof (stdin))
4861 break;
4862 c = getchar ();
4865 if (dyn_string_length (mangled) > 0)
4867 #ifdef IN_GLIBCPP_V3
4868 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4869 #else
4870 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4871 #endif
4873 if (s != NULL)
4875 fputs (s, stdout);
4876 free (s);
4878 else
4880 /* It might not have been a mangled name. Print the
4881 original text. */
4882 fputs (dyn_string_buf (mangled), stdout);
4885 dyn_string_clear (mangled);
4888 /* If we haven't hit EOF yet, we've read one character that
4889 can't occur in a mangled name, so print it out. */
4890 if (!feof (stdin))
4891 putchar (c);
4894 dyn_string_delete (mangled);
4896 else
4897 /* Demangle command line arguments. */
4899 /* Loop over command line arguments. */
4900 for (i = optind; i < argc; ++i)
4902 char *s;
4903 #ifdef IN_GLIBCPP_V3
4904 int status;
4905 #endif
4907 /* Attempt to demangle. */
4908 #ifdef IN_GLIBCPP_V3
4909 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4910 #else
4911 s = cplus_demangle_v3 (argv[i], options);
4912 #endif
4914 /* If it worked, print the demangled name. */
4915 if (s != NULL)
4917 printf ("%s\n", s);
4918 free (s);
4920 else
4922 #ifdef IN_GLIBCPP_V3
4923 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4924 #else
4925 fprintf (stderr, "Failed: %s\n", argv[i]);
4926 #endif
4931 return 0;
4934 #endif /* STANDALONE_DEMANGLER */