daily update
[binutils.git] / libiberty / cp-demangle.c
blobac1dfe56071a207dc0fdd29fcf6d25470ad0cb80
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
40 name.
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
48 Also, the interface to the component list is public, and defined in
49 demangle.h. The interface consists of these types, which are
50 defined in demangle.h:
51 enum demangle_component_type
52 struct demangle_component
53 and these functions defined in this file:
54 cplus_demangle_fill_name
55 cplus_demangle_fill_extended_operator
56 cplus_demangle_fill_ctor
57 cplus_demangle_fill_dtor
58 cplus_demangle_print
59 and other functions defined in the file cp-demint.c.
61 This file also defines some other functions and variables which are
62 only to be used by the file cp-demint.c.
64 Preprocessor macros you can define while compiling this file:
66 IN_LIBGCC2
67 If defined, this file defines the following function, q.v.:
68 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
69 int *status)
70 instead of cplus_demangle_v3() and java_demangle_v3().
72 IN_GLIBCPP_V3
73 If defined, this file defines only __cxa_demangle(), and no other
74 publically visible functions or variables.
76 STANDALONE_DEMANGLER
77 If defined, this file defines a main() function which demangles
78 any arguments, or, if none, demangles stdin.
80 CP_DEMANGLE_DEBUG
81 If defined, turns on debugging mode, which prints information on
82 stdout about the mangled string. This is not generally useful.
85 #ifdef HAVE_CONFIG_H
86 #include "config.h"
87 #endif
89 #include <stdio.h>
91 #ifdef HAVE_STDLIB_H
92 #include <stdlib.h>
93 #endif
94 #ifdef HAVE_STRING_H
95 #include <string.h>
96 #endif
98 #include "ansidecl.h"
99 #include "libiberty.h"
100 #include "demangle.h"
101 #include "cp-demangle.h"
103 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
104 also rename them via #define to avoid compiler errors when the
105 static definition conflicts with the extern declaration in a header
106 file. */
107 #ifdef IN_GLIBCPP_V3
109 #define CP_STATIC_IF_GLIBCPP_V3 static
111 #define cplus_demangle_fill_name d_fill_name
112 static int d_fill_name (struct demangle_component *, const char *, int);
114 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
115 static int
116 d_fill_extended_operator (struct demangle_component *, int,
117 struct demangle_component *);
119 #define cplus_demangle_fill_ctor d_fill_ctor
120 static int
121 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
122 struct demangle_component *);
124 #define cplus_demangle_fill_dtor d_fill_dtor
125 static int
126 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
127 struct demangle_component *);
129 #define cplus_demangle_mangled_name d_mangled_name
130 static struct demangle_component *d_mangled_name (struct d_info *, int);
132 #define cplus_demangle_type d_type
133 static struct demangle_component *d_type (struct d_info *);
135 #define cplus_demangle_print d_print
136 static char *d_print (int, const struct demangle_component *, int, size_t *);
138 #define cplus_demangle_init_info d_init_info
139 static void d_init_info (const char *, int, size_t, struct d_info *);
141 #else /* ! defined(IN_GLIBCPP_V3) */
142 #define CP_STATIC_IF_GLIBCPP_V3
143 #endif /* ! defined(IN_GLIBCPP_V3) */
145 /* See if the compiler supports dynamic arrays. */
147 #ifdef __GNUC__
148 #define CP_DYNAMIC_ARRAYS
149 #else
150 #ifdef __STDC__
151 #ifdef __STDC_VERSION__
152 #if __STDC_VERSION__ >= 199901L
153 #define CP_DYNAMIC_ARRAYS
154 #endif /* __STDC__VERSION >= 199901L */
155 #endif /* defined (__STDC_VERSION__) */
156 #endif /* defined (__STDC__) */
157 #endif /* ! defined (__GNUC__) */
159 /* We avoid pulling in the ctype tables, to prevent pulling in
160 additional unresolved symbols when this code is used in a library.
161 FIXME: Is this really a valid reason? This comes from the original
162 V3 demangler code.
164 As of this writing this file has the following undefined references
165 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
166 strcpy, strcat, strlen. */
168 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
169 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
170 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
172 /* The prefix prepended by GCC to an identifier represnting the
173 anonymous namespace. */
174 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
175 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
176 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
178 /* Information we keep for the standard substitutions. */
180 struct d_standard_sub_info
182 /* The code for this substitution. */
183 char code;
184 /* The simple string it expands to. */
185 const char *simple_expansion;
186 /* The length of the simple expansion. */
187 int simple_len;
188 /* The results of a full, verbose, expansion. This is used when
189 qualifying a constructor/destructor, or when in verbose mode. */
190 const char *full_expansion;
191 /* The length of the full expansion. */
192 int full_len;
193 /* What to set the last_name field of d_info to; NULL if we should
194 not set it. This is only relevant when qualifying a
195 constructor/destructor. */
196 const char *set_last_name;
197 /* The length of set_last_name. */
198 int set_last_name_len;
201 /* Accessors for subtrees of struct demangle_component. */
203 #define d_left(dc) ((dc)->u.s_binary.left)
204 #define d_right(dc) ((dc)->u.s_binary.right)
206 /* A list of templates. This is used while printing. */
208 struct d_print_template
210 /* Next template on the list. */
211 struct d_print_template *next;
212 /* This template. */
213 const struct demangle_component *template_decl;
216 /* A list of type modifiers. This is used while printing. */
218 struct d_print_mod
220 /* Next modifier on the list. These are in the reverse of the order
221 in which they appeared in the mangled string. */
222 struct d_print_mod *next;
223 /* The modifier. */
224 const struct demangle_component *mod;
225 /* Whether this modifier was printed. */
226 int printed;
227 /* The list of templates which applies to this modifier. */
228 struct d_print_template *templates;
231 /* We use this structure to hold information during printing. */
233 struct d_print_info
235 /* The options passed to the demangler. */
236 int options;
237 /* Buffer holding the result. */
238 char *buf;
239 /* Current length of data in buffer. */
240 size_t len;
241 /* Allocated size of buffer. */
242 size_t alc;
243 /* The current list of templates, if any. */
244 struct d_print_template *templates;
245 /* The current list of modifiers (e.g., pointer, reference, etc.),
246 if any. */
247 struct d_print_mod *modifiers;
248 /* Set to 1 if we had a memory allocation failure. */
249 int allocation_failure;
252 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
254 #define d_append_char(dpi, c) \
255 do \
257 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
258 (dpi)->buf[(dpi)->len++] = (c); \
259 else \
260 d_print_append_char ((dpi), (c)); \
262 while (0)
264 #define d_append_buffer(dpi, s, l) \
265 do \
267 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
269 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
270 (dpi)->len += l; \
272 else \
273 d_print_append_buffer ((dpi), (s), (l)); \
275 while (0)
277 #define d_append_string_constant(dpi, s) \
278 d_append_buffer (dpi, (s), sizeof (s) - 1)
280 #define d_last_char(dpi) \
281 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
283 #ifdef CP_DEMANGLE_DEBUG
284 static void d_dump (struct demangle_component *, int);
285 #endif
287 static struct demangle_component *
288 d_make_empty (struct d_info *);
290 static struct demangle_component *
291 d_make_comp (struct d_info *, enum demangle_component_type,
292 struct demangle_component *,
293 struct demangle_component *);
295 static struct demangle_component *
296 d_make_name (struct d_info *, const char *, int);
298 static struct demangle_component *
299 d_make_builtin_type (struct d_info *,
300 const struct demangle_builtin_type_info *);
302 static struct demangle_component *
303 d_make_operator (struct d_info *,
304 const struct demangle_operator_info *);
306 static struct demangle_component *
307 d_make_extended_operator (struct d_info *, int,
308 struct demangle_component *);
310 static struct demangle_component *
311 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
312 struct demangle_component *);
314 static struct demangle_component *
315 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
316 struct demangle_component *);
318 static struct demangle_component *
319 d_make_template_param (struct d_info *, long);
321 static struct demangle_component *
322 d_make_sub (struct d_info *, const char *, int);
324 static int
325 has_return_type (struct demangle_component *);
327 static int
328 is_ctor_dtor_or_conversion (struct demangle_component *);
330 static struct demangle_component *d_encoding (struct d_info *, int);
332 static struct demangle_component *d_name (struct d_info *);
334 static struct demangle_component *d_nested_name (struct d_info *);
336 static struct demangle_component *d_prefix (struct d_info *);
338 static struct demangle_component *d_unqualified_name (struct d_info *);
340 static struct demangle_component *d_source_name (struct d_info *);
342 static long d_number (struct d_info *);
344 static struct demangle_component *d_identifier (struct d_info *, int);
346 static struct demangle_component *d_operator_name (struct d_info *);
348 static struct demangle_component *d_special_name (struct d_info *);
350 static int d_call_offset (struct d_info *, int);
352 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
354 static struct demangle_component **
355 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
357 static struct demangle_component *
358 d_function_type (struct d_info *);
360 static struct demangle_component *
361 d_bare_function_type (struct d_info *, int);
363 static struct demangle_component *
364 d_class_enum_type (struct d_info *);
366 static struct demangle_component *d_array_type (struct d_info *);
368 static struct demangle_component *
369 d_pointer_to_member_type (struct d_info *);
371 static struct demangle_component *
372 d_template_param (struct d_info *);
374 static struct demangle_component *d_template_args (struct d_info *);
376 static struct demangle_component *
377 d_template_arg (struct d_info *);
379 static struct demangle_component *d_expression (struct d_info *);
381 static struct demangle_component *d_expr_primary (struct d_info *);
383 static struct demangle_component *d_local_name (struct d_info *);
385 static int d_discriminator (struct d_info *);
387 static int
388 d_add_substitution (struct d_info *, struct demangle_component *);
390 static struct demangle_component *d_substitution (struct d_info *, int);
392 static void d_print_resize (struct d_print_info *, size_t);
394 static void d_print_append_char (struct d_print_info *, int);
396 static void
397 d_print_append_buffer (struct d_print_info *, const char *, size_t);
399 static void d_print_error (struct d_print_info *);
401 static void
402 d_print_comp (struct d_print_info *, const struct demangle_component *);
404 static void
405 d_print_java_identifier (struct d_print_info *, const char *, int);
407 static void
408 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
410 static void
411 d_print_mod (struct d_print_info *, const struct demangle_component *);
413 static void
414 d_print_function_type (struct d_print_info *,
415 const struct demangle_component *,
416 struct d_print_mod *);
418 static void
419 d_print_array_type (struct d_print_info *,
420 const struct demangle_component *,
421 struct d_print_mod *);
423 static void
424 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
426 static void
427 d_print_cast (struct d_print_info *, const struct demangle_component *);
429 static char *d_demangle (const char *, int, size_t *);
431 #ifdef CP_DEMANGLE_DEBUG
433 static void
434 d_dump (struct demangle_component *dc, int indent)
436 int i;
438 if (dc == NULL)
439 return;
441 for (i = 0; i < indent; ++i)
442 putchar (' ');
444 switch (dc->type)
446 case DEMANGLE_COMPONENT_NAME:
447 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
448 return;
449 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
450 printf ("template parameter %ld\n", dc->u.s_number.number);
451 return;
452 case DEMANGLE_COMPONENT_CTOR:
453 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
454 d_dump (dc->u.s_ctor.name, indent + 2);
455 return;
456 case DEMANGLE_COMPONENT_DTOR:
457 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
458 d_dump (dc->u.s_dtor.name, indent + 2);
459 return;
460 case DEMANGLE_COMPONENT_SUB_STD:
461 printf ("standard substitution %s\n", dc->u.s_string.string);
462 return;
463 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
464 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
465 return;
466 case DEMANGLE_COMPONENT_OPERATOR:
467 printf ("operator %s\n", dc->u.s_operator.op->name);
468 return;
469 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
470 printf ("extended operator with %d args\n",
471 dc->u.s_extended_operator.args);
472 d_dump (dc->u.s_extended_operator.name, indent + 2);
473 return;
475 case DEMANGLE_COMPONENT_QUAL_NAME:
476 printf ("qualified name\n");
477 break;
478 case DEMANGLE_COMPONENT_LOCAL_NAME:
479 printf ("local name\n");
480 break;
481 case DEMANGLE_COMPONENT_TYPED_NAME:
482 printf ("typed name\n");
483 break;
484 case DEMANGLE_COMPONENT_TEMPLATE:
485 printf ("template\n");
486 break;
487 case DEMANGLE_COMPONENT_VTABLE:
488 printf ("vtable\n");
489 break;
490 case DEMANGLE_COMPONENT_VTT:
491 printf ("VTT\n");
492 break;
493 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
494 printf ("construction vtable\n");
495 break;
496 case DEMANGLE_COMPONENT_TYPEINFO:
497 printf ("typeinfo\n");
498 break;
499 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
500 printf ("typeinfo name\n");
501 break;
502 case DEMANGLE_COMPONENT_TYPEINFO_FN:
503 printf ("typeinfo function\n");
504 break;
505 case DEMANGLE_COMPONENT_THUNK:
506 printf ("thunk\n");
507 break;
508 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
509 printf ("virtual thunk\n");
510 break;
511 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
512 printf ("covariant thunk\n");
513 break;
514 case DEMANGLE_COMPONENT_JAVA_CLASS:
515 printf ("java class\n");
516 break;
517 case DEMANGLE_COMPONENT_GUARD:
518 printf ("guard\n");
519 break;
520 case DEMANGLE_COMPONENT_REFTEMP:
521 printf ("reference temporary\n");
522 break;
523 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
524 printf ("hidden alias\n");
525 break;
526 case DEMANGLE_COMPONENT_RESTRICT:
527 printf ("restrict\n");
528 break;
529 case DEMANGLE_COMPONENT_VOLATILE:
530 printf ("volatile\n");
531 break;
532 case DEMANGLE_COMPONENT_CONST:
533 printf ("const\n");
534 break;
535 case DEMANGLE_COMPONENT_RESTRICT_THIS:
536 printf ("restrict this\n");
537 break;
538 case DEMANGLE_COMPONENT_VOLATILE_THIS:
539 printf ("volatile this\n");
540 break;
541 case DEMANGLE_COMPONENT_CONST_THIS:
542 printf ("const this\n");
543 break;
544 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
545 printf ("vendor type qualifier\n");
546 break;
547 case DEMANGLE_COMPONENT_POINTER:
548 printf ("pointer\n");
549 break;
550 case DEMANGLE_COMPONENT_REFERENCE:
551 printf ("reference\n");
552 break;
553 case DEMANGLE_COMPONENT_COMPLEX:
554 printf ("complex\n");
555 break;
556 case DEMANGLE_COMPONENT_IMAGINARY:
557 printf ("imaginary\n");
558 break;
559 case DEMANGLE_COMPONENT_VENDOR_TYPE:
560 printf ("vendor type\n");
561 break;
562 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
563 printf ("function type\n");
564 break;
565 case DEMANGLE_COMPONENT_ARRAY_TYPE:
566 printf ("array type\n");
567 break;
568 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
569 printf ("pointer to member type\n");
570 break;
571 case DEMANGLE_COMPONENT_ARGLIST:
572 printf ("argument list\n");
573 break;
574 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
575 printf ("template argument list\n");
576 break;
577 case DEMANGLE_COMPONENT_CAST:
578 printf ("cast\n");
579 break;
580 case DEMANGLE_COMPONENT_UNARY:
581 printf ("unary operator\n");
582 break;
583 case DEMANGLE_COMPONENT_BINARY:
584 printf ("binary operator\n");
585 break;
586 case DEMANGLE_COMPONENT_BINARY_ARGS:
587 printf ("binary operator arguments\n");
588 break;
589 case DEMANGLE_COMPONENT_TRINARY:
590 printf ("trinary operator\n");
591 break;
592 case DEMANGLE_COMPONENT_TRINARY_ARG1:
593 printf ("trinary operator arguments 1\n");
594 break;
595 case DEMANGLE_COMPONENT_TRINARY_ARG2:
596 printf ("trinary operator arguments 1\n");
597 break;
598 case DEMANGLE_COMPONENT_LITERAL:
599 printf ("literal\n");
600 break;
601 case DEMANGLE_COMPONENT_LITERAL_NEG:
602 printf ("negative literal\n");
603 break;
606 d_dump (d_left (dc), indent + 2);
607 d_dump (d_right (dc), indent + 2);
610 #endif /* CP_DEMANGLE_DEBUG */
612 /* Fill in a DEMANGLE_COMPONENT_NAME. */
614 CP_STATIC_IF_GLIBCPP_V3
616 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
618 if (p == NULL || s == NULL || len == 0)
619 return 0;
620 p->type = DEMANGLE_COMPONENT_NAME;
621 p->u.s_name.s = s;
622 p->u.s_name.len = len;
623 return 1;
626 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
628 CP_STATIC_IF_GLIBCPP_V3
630 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
631 struct demangle_component *name)
633 if (p == NULL || args < 0 || name == NULL)
634 return 0;
635 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
636 p->u.s_extended_operator.args = args;
637 p->u.s_extended_operator.name = name;
638 return 1;
641 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
643 CP_STATIC_IF_GLIBCPP_V3
645 cplus_demangle_fill_ctor (struct demangle_component *p,
646 enum gnu_v3_ctor_kinds kind,
647 struct demangle_component *name)
649 if (p == NULL
650 || name == NULL
651 || (kind < gnu_v3_complete_object_ctor
652 && kind > gnu_v3_complete_object_allocating_ctor))
653 return 0;
654 p->type = DEMANGLE_COMPONENT_CTOR;
655 p->u.s_ctor.kind = kind;
656 p->u.s_ctor.name = name;
657 return 1;
660 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
662 CP_STATIC_IF_GLIBCPP_V3
664 cplus_demangle_fill_dtor (struct demangle_component *p,
665 enum gnu_v3_dtor_kinds kind,
666 struct demangle_component *name)
668 if (p == NULL
669 || name == NULL
670 || (kind < gnu_v3_deleting_dtor
671 && kind > gnu_v3_base_object_dtor))
672 return 0;
673 p->type = DEMANGLE_COMPONENT_DTOR;
674 p->u.s_dtor.kind = kind;
675 p->u.s_dtor.name = name;
676 return 1;
679 /* Add a new component. */
681 static struct demangle_component *
682 d_make_empty (struct d_info *di)
684 struct demangle_component *p;
686 if (di->next_comp >= di->num_comps)
687 return NULL;
688 p = &di->comps[di->next_comp];
689 ++di->next_comp;
690 return p;
693 /* Add a new generic component. */
695 static struct demangle_component *
696 d_make_comp (struct d_info *di, enum demangle_component_type type,
697 struct demangle_component *left,
698 struct demangle_component *right)
700 struct demangle_component *p;
702 /* We check for errors here. A typical error would be a NULL return
703 from a subroutine. We catch those here, and return NULL
704 upward. */
705 switch (type)
707 /* These types require two parameters. */
708 case DEMANGLE_COMPONENT_QUAL_NAME:
709 case DEMANGLE_COMPONENT_LOCAL_NAME:
710 case DEMANGLE_COMPONENT_TYPED_NAME:
711 case DEMANGLE_COMPONENT_TEMPLATE:
712 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
713 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
714 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
715 case DEMANGLE_COMPONENT_UNARY:
716 case DEMANGLE_COMPONENT_BINARY:
717 case DEMANGLE_COMPONENT_BINARY_ARGS:
718 case DEMANGLE_COMPONENT_TRINARY:
719 case DEMANGLE_COMPONENT_TRINARY_ARG1:
720 case DEMANGLE_COMPONENT_TRINARY_ARG2:
721 case DEMANGLE_COMPONENT_LITERAL:
722 case DEMANGLE_COMPONENT_LITERAL_NEG:
723 if (left == NULL || right == NULL)
724 return NULL;
725 break;
727 /* These types only require one parameter. */
728 case DEMANGLE_COMPONENT_VTABLE:
729 case DEMANGLE_COMPONENT_VTT:
730 case DEMANGLE_COMPONENT_TYPEINFO:
731 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
732 case DEMANGLE_COMPONENT_TYPEINFO_FN:
733 case DEMANGLE_COMPONENT_THUNK:
734 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
735 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
736 case DEMANGLE_COMPONENT_JAVA_CLASS:
737 case DEMANGLE_COMPONENT_GUARD:
738 case DEMANGLE_COMPONENT_REFTEMP:
739 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
740 case DEMANGLE_COMPONENT_POINTER:
741 case DEMANGLE_COMPONENT_REFERENCE:
742 case DEMANGLE_COMPONENT_COMPLEX:
743 case DEMANGLE_COMPONENT_IMAGINARY:
744 case DEMANGLE_COMPONENT_VENDOR_TYPE:
745 case DEMANGLE_COMPONENT_ARGLIST:
746 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
747 case DEMANGLE_COMPONENT_CAST:
748 if (left == NULL)
749 return NULL;
750 break;
752 /* This needs a right parameter, but the left parameter can be
753 empty. */
754 case DEMANGLE_COMPONENT_ARRAY_TYPE:
755 if (right == NULL)
756 return NULL;
757 break;
759 /* These are allowed to have no parameters--in some cases they
760 will be filled in later. */
761 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
762 case DEMANGLE_COMPONENT_RESTRICT:
763 case DEMANGLE_COMPONENT_VOLATILE:
764 case DEMANGLE_COMPONENT_CONST:
765 case DEMANGLE_COMPONENT_RESTRICT_THIS:
766 case DEMANGLE_COMPONENT_VOLATILE_THIS:
767 case DEMANGLE_COMPONENT_CONST_THIS:
768 break;
770 /* Other types should not be seen here. */
771 default:
772 return NULL;
775 p = d_make_empty (di);
776 if (p != NULL)
778 p->type = type;
779 p->u.s_binary.left = left;
780 p->u.s_binary.right = right;
782 return p;
785 /* Add a new name component. */
787 static struct demangle_component *
788 d_make_name (struct d_info *di, const char *s, int len)
790 struct demangle_component *p;
792 p = d_make_empty (di);
793 if (! cplus_demangle_fill_name (p, s, len))
794 return NULL;
795 return p;
798 /* Add a new builtin type component. */
800 static struct demangle_component *
801 d_make_builtin_type (struct d_info *di,
802 const struct demangle_builtin_type_info *type)
804 struct demangle_component *p;
806 if (type == NULL)
807 return NULL;
808 p = d_make_empty (di);
809 if (p != NULL)
811 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
812 p->u.s_builtin.type = type;
814 return p;
817 /* Add a new operator component. */
819 static struct demangle_component *
820 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
822 struct demangle_component *p;
824 p = d_make_empty (di);
825 if (p != NULL)
827 p->type = DEMANGLE_COMPONENT_OPERATOR;
828 p->u.s_operator.op = op;
830 return p;
833 /* Add a new extended operator component. */
835 static struct demangle_component *
836 d_make_extended_operator (struct d_info *di, int args,
837 struct demangle_component *name)
839 struct demangle_component *p;
841 p = d_make_empty (di);
842 if (! cplus_demangle_fill_extended_operator (p, args, name))
843 return NULL;
844 return p;
847 /* Add a new constructor component. */
849 static struct demangle_component *
850 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
851 struct demangle_component *name)
853 struct demangle_component *p;
855 p = d_make_empty (di);
856 if (! cplus_demangle_fill_ctor (p, kind, name))
857 return NULL;
858 return p;
861 /* Add a new destructor component. */
863 static struct demangle_component *
864 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
865 struct demangle_component *name)
867 struct demangle_component *p;
869 p = d_make_empty (di);
870 if (! cplus_demangle_fill_dtor (p, kind, name))
871 return NULL;
872 return p;
875 /* Add a new template parameter. */
877 static struct demangle_component *
878 d_make_template_param (struct d_info *di, long i)
880 struct demangle_component *p;
882 p = d_make_empty (di);
883 if (p != NULL)
885 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
886 p->u.s_number.number = i;
888 return p;
891 /* Add a new standard substitution component. */
893 static struct demangle_component *
894 d_make_sub (struct d_info *di, const char *name, int len)
896 struct demangle_component *p;
898 p = d_make_empty (di);
899 if (p != NULL)
901 p->type = DEMANGLE_COMPONENT_SUB_STD;
902 p->u.s_string.string = name;
903 p->u.s_string.len = len;
905 return p;
908 /* <mangled-name> ::= _Z <encoding>
910 TOP_LEVEL is non-zero when called at the top level. */
912 CP_STATIC_IF_GLIBCPP_V3
913 struct demangle_component *
914 cplus_demangle_mangled_name (struct d_info *di, int top_level)
916 if (! d_check_char (di, '_'))
917 return NULL;
918 if (! d_check_char (di, 'Z'))
919 return NULL;
920 return d_encoding (di, top_level);
923 /* Return whether a function should have a return type. The argument
924 is the function name, which may be qualified in various ways. The
925 rules are that template functions have return types with some
926 exceptions, function types which are not part of a function name
927 mangling have return types with some exceptions, and non-template
928 function names do not have return types. The exceptions are that
929 constructors, destructors, and conversion operators do not have
930 return types. */
932 static int
933 has_return_type (struct demangle_component *dc)
935 if (dc == NULL)
936 return 0;
937 switch (dc->type)
939 default:
940 return 0;
941 case DEMANGLE_COMPONENT_TEMPLATE:
942 return ! is_ctor_dtor_or_conversion (d_left (dc));
943 case DEMANGLE_COMPONENT_RESTRICT_THIS:
944 case DEMANGLE_COMPONENT_VOLATILE_THIS:
945 case DEMANGLE_COMPONENT_CONST_THIS:
946 return has_return_type (d_left (dc));
950 /* Return whether a name is a constructor, a destructor, or a
951 conversion operator. */
953 static int
954 is_ctor_dtor_or_conversion (struct demangle_component *dc)
956 if (dc == NULL)
957 return 0;
958 switch (dc->type)
960 default:
961 return 0;
962 case DEMANGLE_COMPONENT_QUAL_NAME:
963 case DEMANGLE_COMPONENT_LOCAL_NAME:
964 return is_ctor_dtor_or_conversion (d_right (dc));
965 case DEMANGLE_COMPONENT_CTOR:
966 case DEMANGLE_COMPONENT_DTOR:
967 case DEMANGLE_COMPONENT_CAST:
968 return 1;
972 /* <encoding> ::= <(function) name> <bare-function-type>
973 ::= <(data) name>
974 ::= <special-name>
976 TOP_LEVEL is non-zero when called at the top level, in which case
977 if DMGL_PARAMS is not set we do not demangle the function
978 parameters. We only set this at the top level, because otherwise
979 we would not correctly demangle names in local scopes. */
981 static struct demangle_component *
982 d_encoding (struct d_info *di, int top_level)
984 char peek = d_peek_char (di);
986 if (peek == 'G' || peek == 'T')
987 return d_special_name (di);
988 else
990 struct demangle_component *dc;
992 dc = d_name (di);
994 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
996 /* Strip off any initial CV-qualifiers, as they really apply
997 to the `this' parameter, and they were not output by the
998 v2 demangler without DMGL_PARAMS. */
999 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1000 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1001 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1002 dc = d_left (dc);
1004 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1005 there may be CV-qualifiers on its right argument which
1006 really apply here; this happens when parsing a class
1007 which is local to a function. */
1008 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1010 struct demangle_component *dcr;
1012 dcr = d_right (dc);
1013 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1014 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1015 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1016 dcr = d_left (dcr);
1017 dc->u.s_binary.right = dcr;
1020 return dc;
1023 peek = d_peek_char (di);
1024 if (peek == '\0' || peek == 'E')
1025 return dc;
1026 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1027 d_bare_function_type (di, has_return_type (dc)));
1031 /* <name> ::= <nested-name>
1032 ::= <unscoped-name>
1033 ::= <unscoped-template-name> <template-args>
1034 ::= <local-name>
1036 <unscoped-name> ::= <unqualified-name>
1037 ::= St <unqualified-name>
1039 <unscoped-template-name> ::= <unscoped-name>
1040 ::= <substitution>
1043 static struct demangle_component *
1044 d_name (struct d_info *di)
1046 char peek = d_peek_char (di);
1047 struct demangle_component *dc;
1049 switch (peek)
1051 case 'N':
1052 return d_nested_name (di);
1054 case 'Z':
1055 return d_local_name (di);
1057 case 'S':
1059 int subst;
1061 if (d_peek_next_char (di) != 't')
1063 dc = d_substitution (di, 0);
1064 subst = 1;
1066 else
1068 d_advance (di, 2);
1069 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1070 d_make_name (di, "std", 3),
1071 d_unqualified_name (di));
1072 di->expansion += 3;
1073 subst = 0;
1076 if (d_peek_char (di) != 'I')
1078 /* The grammar does not permit this case to occur if we
1079 called d_substitution() above (i.e., subst == 1). We
1080 don't bother to check. */
1082 else
1084 /* This is <template-args>, which means that we just saw
1085 <unscoped-template-name>, which is a substitution
1086 candidate if we didn't just get it from a
1087 substitution. */
1088 if (! subst)
1090 if (! d_add_substitution (di, dc))
1091 return NULL;
1093 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1094 d_template_args (di));
1097 return dc;
1100 default:
1101 dc = d_unqualified_name (di);
1102 if (d_peek_char (di) == 'I')
1104 /* This is <template-args>, which means that we just saw
1105 <unscoped-template-name>, which is a substitution
1106 candidate. */
1107 if (! d_add_substitution (di, dc))
1108 return NULL;
1109 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1110 d_template_args (di));
1112 return dc;
1116 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1117 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1120 static struct demangle_component *
1121 d_nested_name (struct d_info *di)
1123 struct demangle_component *ret;
1124 struct demangle_component **pret;
1126 if (! d_check_char (di, 'N'))
1127 return NULL;
1129 pret = d_cv_qualifiers (di, &ret, 1);
1130 if (pret == NULL)
1131 return NULL;
1133 *pret = d_prefix (di);
1134 if (*pret == NULL)
1135 return NULL;
1137 if (! d_check_char (di, 'E'))
1138 return NULL;
1140 return ret;
1143 /* <prefix> ::= <prefix> <unqualified-name>
1144 ::= <template-prefix> <template-args>
1145 ::= <template-param>
1147 ::= <substitution>
1149 <template-prefix> ::= <prefix> <(template) unqualified-name>
1150 ::= <template-param>
1151 ::= <substitution>
1154 static struct demangle_component *
1155 d_prefix (struct d_info *di)
1157 struct demangle_component *ret = NULL;
1159 while (1)
1161 char peek;
1162 enum demangle_component_type comb_type;
1163 struct demangle_component *dc;
1165 peek = d_peek_char (di);
1166 if (peek == '\0')
1167 return NULL;
1169 /* The older code accepts a <local-name> here, but I don't see
1170 that in the grammar. The older code does not accept a
1171 <template-param> here. */
1173 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1174 if (IS_DIGIT (peek)
1175 || IS_LOWER (peek)
1176 || peek == 'C'
1177 || peek == 'D')
1178 dc = d_unqualified_name (di);
1179 else if (peek == 'S')
1180 dc = d_substitution (di, 1);
1181 else if (peek == 'I')
1183 if (ret == NULL)
1184 return NULL;
1185 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1186 dc = d_template_args (di);
1188 else if (peek == 'T')
1189 dc = d_template_param (di);
1190 else if (peek == 'E')
1191 return ret;
1192 else
1193 return NULL;
1195 if (ret == NULL)
1196 ret = dc;
1197 else
1198 ret = d_make_comp (di, comb_type, ret, dc);
1200 if (peek != 'S' && d_peek_char (di) != 'E')
1202 if (! d_add_substitution (di, ret))
1203 return NULL;
1208 /* <unqualified-name> ::= <operator-name>
1209 ::= <ctor-dtor-name>
1210 ::= <source-name>
1213 static struct demangle_component *
1214 d_unqualified_name (struct d_info *di)
1216 char peek;
1218 peek = d_peek_char (di);
1219 if (IS_DIGIT (peek))
1220 return d_source_name (di);
1221 else if (IS_LOWER (peek))
1223 struct demangle_component *ret;
1225 ret = d_operator_name (di);
1226 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1227 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1228 return ret;
1230 else if (peek == 'C' || peek == 'D')
1231 return d_ctor_dtor_name (di);
1232 else
1233 return NULL;
1236 /* <source-name> ::= <(positive length) number> <identifier> */
1238 static struct demangle_component *
1239 d_source_name (struct d_info *di)
1241 long len;
1242 struct demangle_component *ret;
1244 len = d_number (di);
1245 if (len <= 0)
1246 return NULL;
1247 ret = d_identifier (di, len);
1248 di->last_name = ret;
1249 return ret;
1252 /* number ::= [n] <(non-negative decimal integer)> */
1254 static long
1255 d_number (struct d_info *di)
1257 int negative;
1258 char peek;
1259 long ret;
1261 negative = 0;
1262 peek = d_peek_char (di);
1263 if (peek == 'n')
1265 negative = 1;
1266 d_advance (di, 1);
1267 peek = d_peek_char (di);
1270 ret = 0;
1271 while (1)
1273 if (! IS_DIGIT (peek))
1275 if (negative)
1276 ret = - ret;
1277 return ret;
1279 ret = ret * 10 + peek - '0';
1280 d_advance (di, 1);
1281 peek = d_peek_char (di);
1285 /* identifier ::= <(unqualified source code identifier)> */
1287 static struct demangle_component *
1288 d_identifier (struct d_info *di, int len)
1290 const char *name;
1292 name = d_str (di);
1294 if (di->send - name < len)
1295 return NULL;
1297 d_advance (di, len);
1299 /* A Java mangled name may have a trailing '$' if it is a C++
1300 keyword. This '$' is not included in the length count. We just
1301 ignore the '$'. */
1302 if ((di->options & DMGL_JAVA) != 0
1303 && d_peek_char (di) == '$')
1304 d_advance (di, 1);
1306 /* Look for something which looks like a gcc encoding of an
1307 anonymous namespace, and replace it with a more user friendly
1308 name. */
1309 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1310 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1311 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1313 const char *s;
1315 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1316 if ((*s == '.' || *s == '_' || *s == '$')
1317 && s[1] == 'N')
1319 di->expansion -= len - sizeof "(anonymous namespace)";
1320 return d_make_name (di, "(anonymous namespace)",
1321 sizeof "(anonymous namespace)" - 1);
1325 return d_make_name (di, name, len);
1328 /* operator_name ::= many different two character encodings.
1329 ::= cv <type>
1330 ::= v <digit> <source-name>
1333 #define NL(s) s, (sizeof s) - 1
1335 CP_STATIC_IF_GLIBCPP_V3
1336 const struct demangle_operator_info cplus_demangle_operators[] =
1338 { "aN", NL ("&="), 2 },
1339 { "aS", NL ("="), 2 },
1340 { "aa", NL ("&&"), 2 },
1341 { "ad", NL ("&"), 1 },
1342 { "an", NL ("&"), 2 },
1343 { "cl", NL ("()"), 0 },
1344 { "cm", NL (","), 2 },
1345 { "co", NL ("~"), 1 },
1346 { "dV", NL ("/="), 2 },
1347 { "da", NL ("delete[]"), 1 },
1348 { "de", NL ("*"), 1 },
1349 { "dl", NL ("delete"), 1 },
1350 { "dv", NL ("/"), 2 },
1351 { "eO", NL ("^="), 2 },
1352 { "eo", NL ("^"), 2 },
1353 { "eq", NL ("=="), 2 },
1354 { "ge", NL (">="), 2 },
1355 { "gt", NL (">"), 2 },
1356 { "ix", NL ("[]"), 2 },
1357 { "lS", NL ("<<="), 2 },
1358 { "le", NL ("<="), 2 },
1359 { "ls", NL ("<<"), 2 },
1360 { "lt", NL ("<"), 2 },
1361 { "mI", NL ("-="), 2 },
1362 { "mL", NL ("*="), 2 },
1363 { "mi", NL ("-"), 2 },
1364 { "ml", NL ("*"), 2 },
1365 { "mm", NL ("--"), 1 },
1366 { "na", NL ("new[]"), 1 },
1367 { "ne", NL ("!="), 2 },
1368 { "ng", NL ("-"), 1 },
1369 { "nt", NL ("!"), 1 },
1370 { "nw", NL ("new"), 1 },
1371 { "oR", NL ("|="), 2 },
1372 { "oo", NL ("||"), 2 },
1373 { "or", NL ("|"), 2 },
1374 { "pL", NL ("+="), 2 },
1375 { "pl", NL ("+"), 2 },
1376 { "pm", NL ("->*"), 2 },
1377 { "pp", NL ("++"), 1 },
1378 { "ps", NL ("+"), 1 },
1379 { "pt", NL ("->"), 2 },
1380 { "qu", NL ("?"), 3 },
1381 { "rM", NL ("%="), 2 },
1382 { "rS", NL (">>="), 2 },
1383 { "rm", NL ("%"), 2 },
1384 { "rs", NL (">>"), 2 },
1385 { "st", NL ("sizeof "), 1 },
1386 { "sz", NL ("sizeof "), 1 },
1387 { NULL, NULL, 0, 0 }
1390 static struct demangle_component *
1391 d_operator_name (struct d_info *di)
1393 char c1;
1394 char c2;
1396 c1 = d_next_char (di);
1397 c2 = d_next_char (di);
1398 if (c1 == 'v' && IS_DIGIT (c2))
1399 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1400 else if (c1 == 'c' && c2 == 'v')
1401 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1402 cplus_demangle_type (di), NULL);
1403 else
1405 /* LOW is the inclusive lower bound. */
1406 int low = 0;
1407 /* HIGH is the exclusive upper bound. We subtract one to ignore
1408 the sentinel at the end of the array. */
1409 int high = ((sizeof (cplus_demangle_operators)
1410 / sizeof (cplus_demangle_operators[0]))
1411 - 1);
1413 while (1)
1415 int i;
1416 const struct demangle_operator_info *p;
1418 i = low + (high - low) / 2;
1419 p = cplus_demangle_operators + i;
1421 if (c1 == p->code[0] && c2 == p->code[1])
1422 return d_make_operator (di, p);
1424 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1425 high = i;
1426 else
1427 low = i + 1;
1428 if (low == high)
1429 return NULL;
1434 /* <special-name> ::= TV <type>
1435 ::= TT <type>
1436 ::= TI <type>
1437 ::= TS <type>
1438 ::= GV <(object) name>
1439 ::= T <call-offset> <(base) encoding>
1440 ::= Tc <call-offset> <call-offset> <(base) encoding>
1441 Also g++ extensions:
1442 ::= TC <type> <(offset) number> _ <(base) type>
1443 ::= TF <type>
1444 ::= TJ <type>
1445 ::= GR <name>
1446 ::= GA <encoding>
1449 static struct demangle_component *
1450 d_special_name (struct d_info *di)
1452 di->expansion += 20;
1453 if (d_check_char (di, 'T'))
1455 switch (d_next_char (di))
1457 case 'V':
1458 di->expansion -= 5;
1459 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1460 cplus_demangle_type (di), NULL);
1461 case 'T':
1462 di->expansion -= 10;
1463 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1464 cplus_demangle_type (di), NULL);
1465 case 'I':
1466 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1467 cplus_demangle_type (di), NULL);
1468 case 'S':
1469 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1470 cplus_demangle_type (di), NULL);
1472 case 'h':
1473 if (! d_call_offset (di, 'h'))
1474 return NULL;
1475 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1476 d_encoding (di, 0), NULL);
1478 case 'v':
1479 if (! d_call_offset (di, 'v'))
1480 return NULL;
1481 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1482 d_encoding (di, 0), NULL);
1484 case 'c':
1485 if (! d_call_offset (di, '\0'))
1486 return NULL;
1487 if (! d_call_offset (di, '\0'))
1488 return NULL;
1489 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1490 d_encoding (di, 0), NULL);
1492 case 'C':
1494 struct demangle_component *derived_type;
1495 long offset;
1496 struct demangle_component *base_type;
1498 derived_type = cplus_demangle_type (di);
1499 offset = d_number (di);
1500 if (offset < 0)
1501 return NULL;
1502 if (! d_check_char (di, '_'))
1503 return NULL;
1504 base_type = cplus_demangle_type (di);
1505 /* We don't display the offset. FIXME: We should display
1506 it in verbose mode. */
1507 di->expansion += 5;
1508 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1509 base_type, derived_type);
1512 case 'F':
1513 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1514 cplus_demangle_type (di), NULL);
1515 case 'J':
1516 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1517 cplus_demangle_type (di), NULL);
1519 default:
1520 return NULL;
1523 else if (d_check_char (di, 'G'))
1525 switch (d_next_char (di))
1527 case 'V':
1528 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1530 case 'R':
1531 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1532 NULL);
1534 case 'A':
1535 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1536 d_encoding (di, 0), NULL);
1538 default:
1539 return NULL;
1542 else
1543 return NULL;
1546 /* <call-offset> ::= h <nv-offset> _
1547 ::= v <v-offset> _
1549 <nv-offset> ::= <(offset) number>
1551 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1553 The C parameter, if not '\0', is a character we just read which is
1554 the start of the <call-offset>.
1556 We don't display the offset information anywhere. FIXME: We should
1557 display it in verbose mode. */
1559 static int
1560 d_call_offset (struct d_info *di, int c)
1562 if (c == '\0')
1563 c = d_next_char (di);
1565 if (c == 'h')
1566 d_number (di);
1567 else if (c == 'v')
1569 d_number (di);
1570 if (! d_check_char (di, '_'))
1571 return 0;
1572 d_number (di);
1574 else
1575 return 0;
1577 if (! d_check_char (di, '_'))
1578 return 0;
1580 return 1;
1583 /* <ctor-dtor-name> ::= C1
1584 ::= C2
1585 ::= C3
1586 ::= D0
1587 ::= D1
1588 ::= D2
1591 static struct demangle_component *
1592 d_ctor_dtor_name (struct d_info *di)
1594 if (di->last_name != NULL)
1596 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1597 di->expansion += di->last_name->u.s_name.len;
1598 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1599 di->expansion += di->last_name->u.s_string.len;
1601 switch (d_peek_char (di))
1603 case 'C':
1605 enum gnu_v3_ctor_kinds kind;
1607 switch (d_peek_next_char (di))
1609 case '1':
1610 kind = gnu_v3_complete_object_ctor;
1611 break;
1612 case '2':
1613 kind = gnu_v3_base_object_ctor;
1614 break;
1615 case '3':
1616 kind = gnu_v3_complete_object_allocating_ctor;
1617 break;
1618 default:
1619 return NULL;
1621 d_advance (di, 2);
1622 return d_make_ctor (di, kind, di->last_name);
1625 case 'D':
1627 enum gnu_v3_dtor_kinds kind;
1629 switch (d_peek_next_char (di))
1631 case '0':
1632 kind = gnu_v3_deleting_dtor;
1633 break;
1634 case '1':
1635 kind = gnu_v3_complete_object_dtor;
1636 break;
1637 case '2':
1638 kind = gnu_v3_base_object_dtor;
1639 break;
1640 default:
1641 return NULL;
1643 d_advance (di, 2);
1644 return d_make_dtor (di, kind, di->last_name);
1647 default:
1648 return NULL;
1652 /* <type> ::= <builtin-type>
1653 ::= <function-type>
1654 ::= <class-enum-type>
1655 ::= <array-type>
1656 ::= <pointer-to-member-type>
1657 ::= <template-param>
1658 ::= <template-template-param> <template-args>
1659 ::= <substitution>
1660 ::= <CV-qualifiers> <type>
1661 ::= P <type>
1662 ::= R <type>
1663 ::= C <type>
1664 ::= G <type>
1665 ::= U <source-name> <type>
1667 <builtin-type> ::= various one letter codes
1668 ::= u <source-name>
1671 CP_STATIC_IF_GLIBCPP_V3
1672 const struct demangle_builtin_type_info
1673 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1675 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1676 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1677 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1678 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1679 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1680 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1681 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1682 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1683 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1684 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1685 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1686 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1687 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1688 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1689 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1690 D_PRINT_DEFAULT },
1691 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1692 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1693 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1694 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1695 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1696 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1697 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1698 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1699 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1700 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1701 D_PRINT_UNSIGNED_LONG_LONG },
1702 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1705 CP_STATIC_IF_GLIBCPP_V3
1706 struct demangle_component *
1707 cplus_demangle_type (struct d_info *di)
1709 char peek;
1710 struct demangle_component *ret;
1711 int can_subst;
1713 /* The ABI specifies that when CV-qualifiers are used, the base type
1714 is substitutable, and the fully qualified type is substitutable,
1715 but the base type with a strict subset of the CV-qualifiers is
1716 not substitutable. The natural recursive implementation of the
1717 CV-qualifiers would cause subsets to be substitutable, so instead
1718 we pull them all off now.
1720 FIXME: The ABI says that order-insensitive vendor qualifiers
1721 should be handled in the same way, but we have no way to tell
1722 which vendor qualifiers are order-insensitive and which are
1723 order-sensitive. So we just assume that they are all
1724 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1725 __vector, and it treats it as order-sensitive when mangling
1726 names. */
1728 peek = d_peek_char (di);
1729 if (peek == 'r' || peek == 'V' || peek == 'K')
1731 struct demangle_component **pret;
1733 pret = d_cv_qualifiers (di, &ret, 0);
1734 if (pret == NULL)
1735 return NULL;
1736 *pret = cplus_demangle_type (di);
1737 if (! d_add_substitution (di, ret))
1738 return NULL;
1739 return ret;
1742 can_subst = 1;
1744 switch (peek)
1746 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1747 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1748 case 'o': case 's': case 't':
1749 case 'v': case 'w': case 'x': case 'y': case 'z':
1750 ret = d_make_builtin_type (di,
1751 &cplus_demangle_builtin_types[peek - 'a']);
1752 di->expansion += ret->u.s_builtin.type->len;
1753 can_subst = 0;
1754 d_advance (di, 1);
1755 break;
1757 case 'u':
1758 d_advance (di, 1);
1759 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1760 d_source_name (di), NULL);
1761 break;
1763 case 'F':
1764 ret = d_function_type (di);
1765 break;
1767 case '0': case '1': case '2': case '3': case '4':
1768 case '5': case '6': case '7': case '8': case '9':
1769 case 'N':
1770 case 'Z':
1771 ret = d_class_enum_type (di);
1772 break;
1774 case 'A':
1775 ret = d_array_type (di);
1776 break;
1778 case 'M':
1779 ret = d_pointer_to_member_type (di);
1780 break;
1782 case 'T':
1783 ret = d_template_param (di);
1784 if (d_peek_char (di) == 'I')
1786 /* This is <template-template-param> <template-args>. The
1787 <template-template-param> part is a substitution
1788 candidate. */
1789 if (! d_add_substitution (di, ret))
1790 return NULL;
1791 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1792 d_template_args (di));
1794 break;
1796 case 'S':
1797 /* If this is a special substitution, then it is the start of
1798 <class-enum-type>. */
1800 char peek_next;
1802 peek_next = d_peek_next_char (di);
1803 if (IS_DIGIT (peek_next)
1804 || peek_next == '_'
1805 || IS_UPPER (peek_next))
1807 ret = d_substitution (di, 0);
1808 /* The substituted name may have been a template name and
1809 may be followed by tepmlate args. */
1810 if (d_peek_char (di) == 'I')
1811 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1812 d_template_args (di));
1813 else
1814 can_subst = 0;
1816 else
1818 ret = d_class_enum_type (di);
1819 /* If the substitution was a complete type, then it is not
1820 a new substitution candidate. However, if the
1821 substitution was followed by template arguments, then
1822 the whole thing is a substitution candidate. */
1823 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1824 can_subst = 0;
1827 break;
1829 case 'P':
1830 d_advance (di, 1);
1831 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1832 cplus_demangle_type (di), NULL);
1833 break;
1835 case 'R':
1836 d_advance (di, 1);
1837 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1838 cplus_demangle_type (di), NULL);
1839 break;
1841 case 'C':
1842 d_advance (di, 1);
1843 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1844 cplus_demangle_type (di), NULL);
1845 break;
1847 case 'G':
1848 d_advance (di, 1);
1849 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1850 cplus_demangle_type (di), NULL);
1851 break;
1853 case 'U':
1854 d_advance (di, 1);
1855 ret = d_source_name (di);
1856 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1857 cplus_demangle_type (di), ret);
1858 break;
1860 default:
1861 return NULL;
1864 if (can_subst)
1866 if (! d_add_substitution (di, ret))
1867 return NULL;
1870 return ret;
1873 /* <CV-qualifiers> ::= [r] [V] [K] */
1875 static struct demangle_component **
1876 d_cv_qualifiers (struct d_info *di,
1877 struct demangle_component **pret, int member_fn)
1879 char peek;
1881 peek = d_peek_char (di);
1882 while (peek == 'r' || peek == 'V' || peek == 'K')
1884 enum demangle_component_type t;
1886 d_advance (di, 1);
1887 if (peek == 'r')
1889 t = (member_fn
1890 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1891 : DEMANGLE_COMPONENT_RESTRICT);
1892 di->expansion += sizeof "restrict";
1894 else if (peek == 'V')
1896 t = (member_fn
1897 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1898 : DEMANGLE_COMPONENT_VOLATILE);
1899 di->expansion += sizeof "volatile";
1901 else
1903 t = (member_fn
1904 ? DEMANGLE_COMPONENT_CONST_THIS
1905 : DEMANGLE_COMPONENT_CONST);
1906 di->expansion += sizeof "const";
1909 *pret = d_make_comp (di, t, NULL, NULL);
1910 if (*pret == NULL)
1911 return NULL;
1912 pret = &d_left (*pret);
1914 peek = d_peek_char (di);
1917 return pret;
1920 /* <function-type> ::= F [Y] <bare-function-type> E */
1922 static struct demangle_component *
1923 d_function_type (struct d_info *di)
1925 struct demangle_component *ret;
1927 if (! d_check_char (di, 'F'))
1928 return NULL;
1929 if (d_peek_char (di) == 'Y')
1931 /* Function has C linkage. We don't print this information.
1932 FIXME: We should print it in verbose mode. */
1933 d_advance (di, 1);
1935 ret = d_bare_function_type (di, 1);
1936 if (! d_check_char (di, 'E'))
1937 return NULL;
1938 return ret;
1941 /* <bare-function-type> ::= [J]<type>+ */
1943 static struct demangle_component *
1944 d_bare_function_type (struct d_info *di, int has_return_type)
1946 struct demangle_component *return_type;
1947 struct demangle_component *tl;
1948 struct demangle_component **ptl;
1949 char peek;
1951 /* Detect special qualifier indicating that the first argument
1952 is the return type. */
1953 peek = d_peek_char (di);
1954 if (peek == 'J')
1956 d_advance (di, 1);
1957 has_return_type = 1;
1960 return_type = NULL;
1961 tl = NULL;
1962 ptl = &tl;
1963 while (1)
1965 struct demangle_component *type;
1967 peek = d_peek_char (di);
1968 if (peek == '\0' || peek == 'E')
1969 break;
1970 type = cplus_demangle_type (di);
1971 if (type == NULL)
1972 return NULL;
1973 if (has_return_type)
1975 return_type = type;
1976 has_return_type = 0;
1978 else
1980 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
1981 if (*ptl == NULL)
1982 return NULL;
1983 ptl = &d_right (*ptl);
1987 /* There should be at least one parameter type besides the optional
1988 return type. A function which takes no arguments will have a
1989 single parameter type void. */
1990 if (tl == NULL)
1991 return NULL;
1993 /* If we have a single parameter type void, omit it. */
1994 if (d_right (tl) == NULL
1995 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
1996 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
1998 di->expansion -= d_left (tl)->u.s_builtin.type->len;
1999 tl = NULL;
2002 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2005 /* <class-enum-type> ::= <name> */
2007 static struct demangle_component *
2008 d_class_enum_type (struct d_info *di)
2010 return d_name (di);
2013 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2014 ::= A [<(dimension) expression>] _ <(element) type>
2017 static struct demangle_component *
2018 d_array_type (struct d_info *di)
2020 char peek;
2021 struct demangle_component *dim;
2023 if (! d_check_char (di, 'A'))
2024 return NULL;
2026 peek = d_peek_char (di);
2027 if (peek == '_')
2028 dim = NULL;
2029 else if (IS_DIGIT (peek))
2031 const char *s;
2033 s = d_str (di);
2036 d_advance (di, 1);
2037 peek = d_peek_char (di);
2039 while (IS_DIGIT (peek));
2040 dim = d_make_name (di, s, d_str (di) - s);
2041 if (dim == NULL)
2042 return NULL;
2044 else
2046 dim = d_expression (di);
2047 if (dim == NULL)
2048 return NULL;
2051 if (! d_check_char (di, '_'))
2052 return NULL;
2054 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2055 cplus_demangle_type (di));
2058 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2060 static struct demangle_component *
2061 d_pointer_to_member_type (struct d_info *di)
2063 struct demangle_component *cl;
2064 struct demangle_component *mem;
2065 struct demangle_component **pmem;
2067 if (! d_check_char (di, 'M'))
2068 return NULL;
2070 cl = cplus_demangle_type (di);
2072 /* The ABI specifies that any type can be a substitution source, and
2073 that M is followed by two types, and that when a CV-qualified
2074 type is seen both the base type and the CV-qualified types are
2075 substitution sources. The ABI also specifies that for a pointer
2076 to a CV-qualified member function, the qualifiers are attached to
2077 the second type. Given the grammar, a plain reading of the ABI
2078 suggests that both the CV-qualified member function and the
2079 non-qualified member function are substitution sources. However,
2080 g++ does not work that way. g++ treats only the CV-qualified
2081 member function as a substitution source. FIXME. So to work
2082 with g++, we need to pull off the CV-qualifiers here, in order to
2083 avoid calling add_substitution() in cplus_demangle_type(). But
2084 for a CV-qualified member which is not a function, g++ does
2085 follow the ABI, so we need to handle that case here by calling
2086 d_add_substitution ourselves. */
2088 pmem = d_cv_qualifiers (di, &mem, 1);
2089 if (pmem == NULL)
2090 return NULL;
2091 *pmem = cplus_demangle_type (di);
2093 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2095 if (! d_add_substitution (di, mem))
2096 return NULL;
2099 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2102 /* <template-param> ::= T_
2103 ::= T <(parameter-2 non-negative) number> _
2106 static struct demangle_component *
2107 d_template_param (struct d_info *di)
2109 long param;
2111 if (! d_check_char (di, 'T'))
2112 return NULL;
2114 if (d_peek_char (di) == '_')
2115 param = 0;
2116 else
2118 param = d_number (di);
2119 if (param < 0)
2120 return NULL;
2121 param += 1;
2124 if (! d_check_char (di, '_'))
2125 return NULL;
2127 ++di->did_subs;
2129 return d_make_template_param (di, param);
2132 /* <template-args> ::= I <template-arg>+ E */
2134 static struct demangle_component *
2135 d_template_args (struct d_info *di)
2137 struct demangle_component *hold_last_name;
2138 struct demangle_component *al;
2139 struct demangle_component **pal;
2141 /* Preserve the last name we saw--don't let the template arguments
2142 clobber it, as that would give us the wrong name for a subsequent
2143 constructor or destructor. */
2144 hold_last_name = di->last_name;
2146 if (! d_check_char (di, 'I'))
2147 return NULL;
2149 al = NULL;
2150 pal = &al;
2151 while (1)
2153 struct demangle_component *a;
2155 a = d_template_arg (di);
2156 if (a == NULL)
2157 return NULL;
2159 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2160 if (*pal == NULL)
2161 return NULL;
2162 pal = &d_right (*pal);
2164 if (d_peek_char (di) == 'E')
2166 d_advance (di, 1);
2167 break;
2171 di->last_name = hold_last_name;
2173 return al;
2176 /* <template-arg> ::= <type>
2177 ::= X <expression> E
2178 ::= <expr-primary>
2181 static struct demangle_component *
2182 d_template_arg (struct d_info *di)
2184 struct demangle_component *ret;
2186 switch (d_peek_char (di))
2188 case 'X':
2189 d_advance (di, 1);
2190 ret = d_expression (di);
2191 if (! d_check_char (di, 'E'))
2192 return NULL;
2193 return ret;
2195 case 'L':
2196 return d_expr_primary (di);
2198 default:
2199 return cplus_demangle_type (di);
2203 /* <expression> ::= <(unary) operator-name> <expression>
2204 ::= <(binary) operator-name> <expression> <expression>
2205 ::= <(trinary) operator-name> <expression> <expression> <expression>
2206 ::= st <type>
2207 ::= <template-param>
2208 ::= sr <type> <unqualified-name>
2209 ::= sr <type> <unqualified-name> <template-args>
2210 ::= <expr-primary>
2213 static struct demangle_component *
2214 d_expression (struct d_info *di)
2216 char peek;
2218 peek = d_peek_char (di);
2219 if (peek == 'L')
2220 return d_expr_primary (di);
2221 else if (peek == 'T')
2222 return d_template_param (di);
2223 else if (peek == 's' && d_peek_next_char (di) == 'r')
2225 struct demangle_component *type;
2226 struct demangle_component *name;
2228 d_advance (di, 2);
2229 type = cplus_demangle_type (di);
2230 name = d_unqualified_name (di);
2231 if (d_peek_char (di) != 'I')
2232 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2233 else
2234 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2235 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2236 d_template_args (di)));
2238 else
2240 struct demangle_component *op;
2241 int args;
2243 op = d_operator_name (di);
2244 if (op == NULL)
2245 return NULL;
2247 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2248 di->expansion += op->u.s_operator.op->len - 2;
2250 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2251 && strcmp (op->u.s_operator.op->code, "st") == 0)
2252 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2253 cplus_demangle_type (di));
2255 switch (op->type)
2257 default:
2258 return NULL;
2259 case DEMANGLE_COMPONENT_OPERATOR:
2260 args = op->u.s_operator.op->args;
2261 break;
2262 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2263 args = op->u.s_extended_operator.args;
2264 break;
2265 case DEMANGLE_COMPONENT_CAST:
2266 args = 1;
2267 break;
2270 switch (args)
2272 case 1:
2273 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2274 d_expression (di));
2275 case 2:
2277 struct demangle_component *left;
2279 left = d_expression (di);
2280 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2281 d_make_comp (di,
2282 DEMANGLE_COMPONENT_BINARY_ARGS,
2283 left,
2284 d_expression (di)));
2286 case 3:
2288 struct demangle_component *first;
2289 struct demangle_component *second;
2291 first = d_expression (di);
2292 second = d_expression (di);
2293 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2294 d_make_comp (di,
2295 DEMANGLE_COMPONENT_TRINARY_ARG1,
2296 first,
2297 d_make_comp (di,
2298 DEMANGLE_COMPONENT_TRINARY_ARG2,
2299 second,
2300 d_expression (di))));
2302 default:
2303 return NULL;
2308 /* <expr-primary> ::= L <type> <(value) number> E
2309 ::= L <type> <(value) float> E
2310 ::= L <mangled-name> E
2313 static struct demangle_component *
2314 d_expr_primary (struct d_info *di)
2316 struct demangle_component *ret;
2318 if (! d_check_char (di, 'L'))
2319 return NULL;
2320 if (d_peek_char (di) == '_')
2321 ret = cplus_demangle_mangled_name (di, 0);
2322 else
2324 struct demangle_component *type;
2325 enum demangle_component_type t;
2326 const char *s;
2328 type = cplus_demangle_type (di);
2329 if (type == NULL)
2330 return NULL;
2332 /* If we have a type we know how to print, we aren't going to
2333 print the type name itself. */
2334 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2335 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2336 di->expansion -= type->u.s_builtin.type->len;
2338 /* Rather than try to interpret the literal value, we just
2339 collect it as a string. Note that it's possible to have a
2340 floating point literal here. The ABI specifies that the
2341 format of such literals is machine independent. That's fine,
2342 but what's not fine is that versions of g++ up to 3.2 with
2343 -fabi-version=1 used upper case letters in the hex constant,
2344 and dumped out gcc's internal representation. That makes it
2345 hard to tell where the constant ends, and hard to dump the
2346 constant in any readable form anyhow. We don't attempt to
2347 handle these cases. */
2349 t = DEMANGLE_COMPONENT_LITERAL;
2350 if (d_peek_char (di) == 'n')
2352 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2353 d_advance (di, 1);
2355 s = d_str (di);
2356 while (d_peek_char (di) != 'E')
2358 if (d_peek_char (di) == '\0')
2359 return NULL;
2360 d_advance (di, 1);
2362 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2364 if (! d_check_char (di, 'E'))
2365 return NULL;
2366 return ret;
2369 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2370 ::= Z <(function) encoding> E s [<discriminator>]
2373 static struct demangle_component *
2374 d_local_name (struct d_info *di)
2376 struct demangle_component *function;
2378 if (! d_check_char (di, 'Z'))
2379 return NULL;
2381 function = d_encoding (di, 0);
2383 if (! d_check_char (di, 'E'))
2384 return NULL;
2386 if (d_peek_char (di) == 's')
2388 d_advance (di, 1);
2389 if (! d_discriminator (di))
2390 return NULL;
2391 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2392 d_make_name (di, "string literal",
2393 sizeof "string literal" - 1));
2395 else
2397 struct demangle_component *name;
2399 name = d_name (di);
2400 if (! d_discriminator (di))
2401 return NULL;
2402 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2406 /* <discriminator> ::= _ <(non-negative) number>
2408 We demangle the discriminator, but we don't print it out. FIXME:
2409 We should print it out in verbose mode. */
2411 static int
2412 d_discriminator (struct d_info *di)
2414 long discrim;
2416 if (d_peek_char (di) != '_')
2417 return 1;
2418 d_advance (di, 1);
2419 discrim = d_number (di);
2420 if (discrim < 0)
2421 return 0;
2422 return 1;
2425 /* Add a new substitution. */
2427 static int
2428 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2430 if (dc == NULL)
2431 return 0;
2432 if (di->next_sub >= di->num_subs)
2433 return 0;
2434 di->subs[di->next_sub] = dc;
2435 ++di->next_sub;
2436 return 1;
2439 /* <substitution> ::= S <seq-id> _
2440 ::= S_
2441 ::= St
2442 ::= Sa
2443 ::= Sb
2444 ::= Ss
2445 ::= Si
2446 ::= So
2447 ::= Sd
2449 If PREFIX is non-zero, then this type is being used as a prefix in
2450 a qualified name. In this case, for the standard substitutions, we
2451 need to check whether we are being used as a prefix for a
2452 constructor or destructor, and return a full template name.
2453 Otherwise we will get something like std::iostream::~iostream()
2454 which does not correspond particularly well to any function which
2455 actually appears in the source.
2458 static const struct d_standard_sub_info standard_subs[] =
2460 { 't', NL ("std"),
2461 NL ("std"),
2462 NULL, 0 },
2463 { 'a', NL ("std::allocator"),
2464 NL ("std::allocator"),
2465 NL ("allocator") },
2466 { 'b', NL ("std::basic_string"),
2467 NL ("std::basic_string"),
2468 NL ("basic_string") },
2469 { 's', NL ("std::string"),
2470 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2471 NL ("basic_string") },
2472 { 'i', NL ("std::istream"),
2473 NL ("std::basic_istream<char, std::char_traits<char> >"),
2474 NL ("basic_istream") },
2475 { 'o', NL ("std::ostream"),
2476 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2477 NL ("basic_ostream") },
2478 { 'd', NL ("std::iostream"),
2479 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2480 NL ("basic_iostream") }
2483 static struct demangle_component *
2484 d_substitution (struct d_info *di, int prefix)
2486 char c;
2488 if (! d_check_char (di, 'S'))
2489 return NULL;
2491 c = d_next_char (di);
2492 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2494 int id;
2496 id = 0;
2497 if (c != '_')
2501 if (IS_DIGIT (c))
2502 id = id * 36 + c - '0';
2503 else if (IS_UPPER (c))
2504 id = id * 36 + c - 'A' + 10;
2505 else
2506 return NULL;
2507 if (id < 0)
2508 return NULL;
2509 c = d_next_char (di);
2511 while (c != '_');
2513 ++id;
2516 if (id >= di->next_sub)
2517 return NULL;
2519 ++di->did_subs;
2521 return di->subs[id];
2523 else
2525 int verbose;
2526 const struct d_standard_sub_info *p;
2527 const struct d_standard_sub_info *pend;
2529 verbose = (di->options & DMGL_VERBOSE) != 0;
2530 if (! verbose && prefix)
2532 char peek;
2534 peek = d_peek_char (di);
2535 if (peek == 'C' || peek == 'D')
2536 verbose = 1;
2539 pend = (&standard_subs[0]
2540 + sizeof standard_subs / sizeof standard_subs[0]);
2541 for (p = &standard_subs[0]; p < pend; ++p)
2543 if (c == p->code)
2545 const char *s;
2546 int len;
2548 if (p->set_last_name != NULL)
2549 di->last_name = d_make_sub (di, p->set_last_name,
2550 p->set_last_name_len);
2551 if (verbose)
2553 s = p->full_expansion;
2554 len = p->full_len;
2556 else
2558 s = p->simple_expansion;
2559 len = p->simple_len;
2561 di->expansion += len;
2562 return d_make_sub (di, s, len);
2566 return NULL;
2570 /* Resize the print buffer. */
2572 static void
2573 d_print_resize (struct d_print_info *dpi, size_t add)
2575 size_t need;
2577 if (dpi->buf == NULL)
2578 return;
2579 need = dpi->len + add;
2580 while (need > dpi->alc)
2582 size_t newalc;
2583 char *newbuf;
2585 newalc = dpi->alc * 2;
2586 newbuf = (char *) realloc (dpi->buf, newalc);
2587 if (newbuf == NULL)
2589 free (dpi->buf);
2590 dpi->buf = NULL;
2591 dpi->allocation_failure = 1;
2592 return;
2594 dpi->buf = newbuf;
2595 dpi->alc = newalc;
2599 /* Append a character to the print buffer. */
2601 static void
2602 d_print_append_char (struct d_print_info *dpi, int c)
2604 if (dpi->buf != NULL)
2606 if (dpi->len >= dpi->alc)
2608 d_print_resize (dpi, 1);
2609 if (dpi->buf == NULL)
2610 return;
2613 dpi->buf[dpi->len] = c;
2614 ++dpi->len;
2618 /* Append a buffer to the print buffer. */
2620 static void
2621 d_print_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
2623 if (dpi->buf != NULL)
2625 if (dpi->len + l > dpi->alc)
2627 d_print_resize (dpi, l);
2628 if (dpi->buf == NULL)
2629 return;
2632 memcpy (dpi->buf + dpi->len, s, l);
2633 dpi->len += l;
2637 /* Indicate that an error occurred during printing. */
2639 static void
2640 d_print_error (struct d_print_info *dpi)
2642 free (dpi->buf);
2643 dpi->buf = NULL;
2646 /* Turn components into a human readable string. OPTIONS is the
2647 options bits passed to the demangler. DC is the tree to print.
2648 ESTIMATE is a guess at the length of the result. This returns a
2649 string allocated by malloc, or NULL on error. On success, this
2650 sets *PALC to the size of the allocated buffer. On failure, this
2651 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2652 failure. */
2654 CP_STATIC_IF_GLIBCPP_V3
2655 char *
2656 cplus_demangle_print (int options, const struct demangle_component *dc,
2657 int estimate, size_t *palc)
2659 struct d_print_info dpi;
2661 dpi.options = options;
2663 dpi.alc = estimate + 1;
2664 dpi.buf = (char *) malloc (dpi.alc);
2665 if (dpi.buf == NULL)
2667 *palc = 1;
2668 return NULL;
2671 dpi.len = 0;
2672 dpi.templates = NULL;
2673 dpi.modifiers = NULL;
2675 dpi.allocation_failure = 0;
2677 d_print_comp (&dpi, dc);
2679 d_append_char (&dpi, '\0');
2681 if (dpi.buf != NULL)
2682 *palc = dpi.alc;
2683 else
2684 *palc = dpi.allocation_failure;
2686 return dpi.buf;
2689 /* Subroutine to handle components. */
2691 static void
2692 d_print_comp (struct d_print_info *dpi,
2693 const struct demangle_component *dc)
2695 if (dc == NULL)
2697 d_print_error (dpi);
2698 return;
2700 if (d_print_saw_error (dpi))
2701 return;
2703 switch (dc->type)
2705 case DEMANGLE_COMPONENT_NAME:
2706 if ((dpi->options & DMGL_JAVA) == 0)
2707 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2708 else
2709 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2710 return;
2712 case DEMANGLE_COMPONENT_QUAL_NAME:
2713 case DEMANGLE_COMPONENT_LOCAL_NAME:
2714 d_print_comp (dpi, d_left (dc));
2715 if ((dpi->options & DMGL_JAVA) == 0)
2716 d_append_string_constant (dpi, "::");
2717 else
2718 d_append_char (dpi, '.');
2719 d_print_comp (dpi, d_right (dc));
2720 return;
2722 case DEMANGLE_COMPONENT_TYPED_NAME:
2724 struct d_print_mod *hold_modifiers;
2725 struct demangle_component *typed_name;
2726 struct d_print_mod adpm[4];
2727 unsigned int i;
2728 struct d_print_template dpt;
2730 /* Pass the name down to the type so that it can be printed in
2731 the right place for the type. We also have to pass down
2732 any CV-qualifiers, which apply to the this parameter. */
2733 hold_modifiers = dpi->modifiers;
2734 i = 0;
2735 typed_name = d_left (dc);
2736 while (typed_name != NULL)
2738 if (i >= sizeof adpm / sizeof adpm[0])
2740 d_print_error (dpi);
2741 return;
2744 adpm[i].next = dpi->modifiers;
2745 dpi->modifiers = &adpm[i];
2746 adpm[i].mod = typed_name;
2747 adpm[i].printed = 0;
2748 adpm[i].templates = dpi->templates;
2749 ++i;
2751 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2752 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2753 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2754 break;
2756 typed_name = d_left (typed_name);
2759 /* If typed_name is a template, then it applies to the
2760 function type as well. */
2761 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2763 dpt.next = dpi->templates;
2764 dpi->templates = &dpt;
2765 dpt.template_decl = typed_name;
2768 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2769 there may be CV-qualifiers on its right argument which
2770 really apply here; this happens when parsing a class which
2771 is local to a function. */
2772 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2774 struct demangle_component *local_name;
2776 local_name = d_right (typed_name);
2777 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2778 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2779 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2781 if (i >= sizeof adpm / sizeof adpm[0])
2783 d_print_error (dpi);
2784 return;
2787 adpm[i] = adpm[i - 1];
2788 adpm[i].next = &adpm[i - 1];
2789 dpi->modifiers = &adpm[i];
2791 adpm[i - 1].mod = local_name;
2792 adpm[i - 1].printed = 0;
2793 adpm[i - 1].templates = dpi->templates;
2794 ++i;
2796 local_name = d_left (local_name);
2800 d_print_comp (dpi, d_right (dc));
2802 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2803 dpi->templates = dpt.next;
2805 /* If the modifiers didn't get printed by the type, print them
2806 now. */
2807 while (i > 0)
2809 --i;
2810 if (! adpm[i].printed)
2812 d_append_char (dpi, ' ');
2813 d_print_mod (dpi, adpm[i].mod);
2817 dpi->modifiers = hold_modifiers;
2819 return;
2822 case DEMANGLE_COMPONENT_TEMPLATE:
2824 struct d_print_mod *hold_dpm;
2826 /* Don't push modifiers into a template definition. Doing so
2827 could give the wrong definition for a template argument.
2828 Instead, treat the template essentially as a name. */
2830 hold_dpm = dpi->modifiers;
2831 dpi->modifiers = NULL;
2833 d_print_comp (dpi, d_left (dc));
2834 if (d_last_char (dpi) == '<')
2835 d_append_char (dpi, ' ');
2836 d_append_char (dpi, '<');
2837 d_print_comp (dpi, d_right (dc));
2838 /* Avoid generating two consecutive '>' characters, to avoid
2839 the C++ syntactic ambiguity. */
2840 if (d_last_char (dpi) == '>')
2841 d_append_char (dpi, ' ');
2842 d_append_char (dpi, '>');
2844 dpi->modifiers = hold_dpm;
2846 return;
2849 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
2851 long i;
2852 struct demangle_component *a;
2853 struct d_print_template *hold_dpt;
2855 if (dpi->templates == NULL)
2857 d_print_error (dpi);
2858 return;
2860 i = dc->u.s_number.number;
2861 for (a = d_right (dpi->templates->template_decl);
2862 a != NULL;
2863 a = d_right (a))
2865 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
2867 d_print_error (dpi);
2868 return;
2870 if (i <= 0)
2871 break;
2872 --i;
2874 if (i != 0 || a == NULL)
2876 d_print_error (dpi);
2877 return;
2880 /* While processing this parameter, we need to pop the list of
2881 templates. This is because the template parameter may
2882 itself be a reference to a parameter of an outer
2883 template. */
2885 hold_dpt = dpi->templates;
2886 dpi->templates = hold_dpt->next;
2888 d_print_comp (dpi, d_left (a));
2890 dpi->templates = hold_dpt;
2892 return;
2895 case DEMANGLE_COMPONENT_CTOR:
2896 d_print_comp (dpi, dc->u.s_ctor.name);
2897 return;
2899 case DEMANGLE_COMPONENT_DTOR:
2900 d_append_char (dpi, '~');
2901 d_print_comp (dpi, dc->u.s_dtor.name);
2902 return;
2904 case DEMANGLE_COMPONENT_VTABLE:
2905 d_append_string_constant (dpi, "vtable for ");
2906 d_print_comp (dpi, d_left (dc));
2907 return;
2909 case DEMANGLE_COMPONENT_VTT:
2910 d_append_string_constant (dpi, "VTT for ");
2911 d_print_comp (dpi, d_left (dc));
2912 return;
2914 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
2915 d_append_string_constant (dpi, "construction vtable for ");
2916 d_print_comp (dpi, d_left (dc));
2917 d_append_string_constant (dpi, "-in-");
2918 d_print_comp (dpi, d_right (dc));
2919 return;
2921 case DEMANGLE_COMPONENT_TYPEINFO:
2922 d_append_string_constant (dpi, "typeinfo for ");
2923 d_print_comp (dpi, d_left (dc));
2924 return;
2926 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
2927 d_append_string_constant (dpi, "typeinfo name for ");
2928 d_print_comp (dpi, d_left (dc));
2929 return;
2931 case DEMANGLE_COMPONENT_TYPEINFO_FN:
2932 d_append_string_constant (dpi, "typeinfo fn for ");
2933 d_print_comp (dpi, d_left (dc));
2934 return;
2936 case DEMANGLE_COMPONENT_THUNK:
2937 d_append_string_constant (dpi, "non-virtual thunk to ");
2938 d_print_comp (dpi, d_left (dc));
2939 return;
2941 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
2942 d_append_string_constant (dpi, "virtual thunk to ");
2943 d_print_comp (dpi, d_left (dc));
2944 return;
2946 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
2947 d_append_string_constant (dpi, "covariant return thunk to ");
2948 d_print_comp (dpi, d_left (dc));
2949 return;
2951 case DEMANGLE_COMPONENT_JAVA_CLASS:
2952 d_append_string_constant (dpi, "java Class for ");
2953 d_print_comp (dpi, d_left (dc));
2954 return;
2956 case DEMANGLE_COMPONENT_GUARD:
2957 d_append_string_constant (dpi, "guard variable for ");
2958 d_print_comp (dpi, d_left (dc));
2959 return;
2961 case DEMANGLE_COMPONENT_REFTEMP:
2962 d_append_string_constant (dpi, "reference temporary for ");
2963 d_print_comp (dpi, d_left (dc));
2964 return;
2966 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
2967 d_append_string_constant (dpi, "hidden alias for ");
2968 d_print_comp (dpi, d_left (dc));
2969 return;
2971 case DEMANGLE_COMPONENT_SUB_STD:
2972 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
2973 return;
2975 case DEMANGLE_COMPONENT_RESTRICT:
2976 case DEMANGLE_COMPONENT_VOLATILE:
2977 case DEMANGLE_COMPONENT_CONST:
2979 struct d_print_mod *pdpm;
2981 /* When printing arrays, it's possible to have cases where the
2982 same CV-qualifier gets pushed on the stack multiple times.
2983 We only need to print it once. */
2985 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
2987 if (! pdpm->printed)
2989 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
2990 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
2991 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
2992 break;
2993 if (pdpm->mod->type == dc->type)
2995 d_print_comp (dpi, d_left (dc));
2996 return;
3001 /* Fall through. */
3002 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3003 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3004 case DEMANGLE_COMPONENT_CONST_THIS:
3005 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3006 case DEMANGLE_COMPONENT_POINTER:
3007 case DEMANGLE_COMPONENT_REFERENCE:
3008 case DEMANGLE_COMPONENT_COMPLEX:
3009 case DEMANGLE_COMPONENT_IMAGINARY:
3011 /* We keep a list of modifiers on the stack. */
3012 struct d_print_mod dpm;
3014 dpm.next = dpi->modifiers;
3015 dpi->modifiers = &dpm;
3016 dpm.mod = dc;
3017 dpm.printed = 0;
3018 dpm.templates = dpi->templates;
3020 d_print_comp (dpi, d_left (dc));
3022 /* If the modifier didn't get printed by the type, print it
3023 now. */
3024 if (! dpm.printed)
3025 d_print_mod (dpi, dc);
3027 dpi->modifiers = dpm.next;
3029 return;
3032 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3033 if ((dpi->options & DMGL_JAVA) == 0)
3034 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3035 dc->u.s_builtin.type->len);
3036 else
3037 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3038 dc->u.s_builtin.type->java_len);
3039 return;
3041 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3042 d_print_comp (dpi, d_left (dc));
3043 return;
3045 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3047 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3048 d_print_function_type (dpi, dc, dpi->modifiers);
3050 /* Print return type if present */
3051 if (d_left (dc) != NULL)
3053 struct d_print_mod dpm;
3055 /* We must pass this type down as a modifier in order to
3056 print it in the right location. */
3057 dpm.next = dpi->modifiers;
3058 dpi->modifiers = &dpm;
3059 dpm.mod = dc;
3060 dpm.printed = 0;
3061 dpm.templates = dpi->templates;
3063 d_print_comp (dpi, d_left (dc));
3065 dpi->modifiers = dpm.next;
3067 if (dpm.printed)
3068 return;
3070 /* In standard prefix notation, there is a space between the
3071 return type and the function signature. */
3072 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3073 d_append_char (dpi, ' ');
3076 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3077 d_print_function_type (dpi, dc, dpi->modifiers);
3079 return;
3082 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3084 struct d_print_mod *hold_modifiers;
3085 struct d_print_mod adpm[4];
3086 unsigned int i;
3087 struct d_print_mod *pdpm;
3089 /* We must pass this type down as a modifier in order to print
3090 multi-dimensional arrays correctly. If the array itself is
3091 CV-qualified, we act as though the element type were
3092 CV-qualified. We do this by copying the modifiers down
3093 rather than fiddling pointers, so that we don't wind up
3094 with a d_print_mod higher on the stack pointing into our
3095 stack frame after we return. */
3097 hold_modifiers = dpi->modifiers;
3099 adpm[0].next = hold_modifiers;
3100 dpi->modifiers = &adpm[0];
3101 adpm[0].mod = dc;
3102 adpm[0].printed = 0;
3103 adpm[0].templates = dpi->templates;
3105 i = 1;
3106 pdpm = hold_modifiers;
3107 while (pdpm != NULL
3108 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3109 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3110 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3112 if (! pdpm->printed)
3114 if (i >= sizeof adpm / sizeof adpm[0])
3116 d_print_error (dpi);
3117 return;
3120 adpm[i] = *pdpm;
3121 adpm[i].next = dpi->modifiers;
3122 dpi->modifiers = &adpm[i];
3123 pdpm->printed = 1;
3124 ++i;
3127 pdpm = pdpm->next;
3130 d_print_comp (dpi, d_right (dc));
3132 dpi->modifiers = hold_modifiers;
3134 if (adpm[0].printed)
3135 return;
3137 while (i > 1)
3139 --i;
3140 d_print_mod (dpi, adpm[i].mod);
3143 d_print_array_type (dpi, dc, dpi->modifiers);
3145 return;
3148 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3150 struct d_print_mod dpm;
3152 dpm.next = dpi->modifiers;
3153 dpi->modifiers = &dpm;
3154 dpm.mod = dc;
3155 dpm.printed = 0;
3156 dpm.templates = dpi->templates;
3158 d_print_comp (dpi, d_right (dc));
3160 /* If the modifier didn't get printed by the type, print it
3161 now. */
3162 if (! dpm.printed)
3164 d_append_char (dpi, ' ');
3165 d_print_comp (dpi, d_left (dc));
3166 d_append_string_constant (dpi, "::*");
3169 dpi->modifiers = dpm.next;
3171 return;
3174 case DEMANGLE_COMPONENT_ARGLIST:
3175 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3176 d_print_comp (dpi, d_left (dc));
3177 if (d_right (dc) != NULL)
3179 d_append_string_constant (dpi, ", ");
3180 d_print_comp (dpi, d_right (dc));
3182 return;
3184 case DEMANGLE_COMPONENT_OPERATOR:
3186 char c;
3188 d_append_string_constant (dpi, "operator");
3189 c = dc->u.s_operator.op->name[0];
3190 if (IS_LOWER (c))
3191 d_append_char (dpi, ' ');
3192 d_append_buffer (dpi, dc->u.s_operator.op->name,
3193 dc->u.s_operator.op->len);
3194 return;
3197 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3198 d_append_string_constant (dpi, "operator ");
3199 d_print_comp (dpi, dc->u.s_extended_operator.name);
3200 return;
3202 case DEMANGLE_COMPONENT_CAST:
3203 d_append_string_constant (dpi, "operator ");
3204 d_print_cast (dpi, dc);
3205 return;
3207 case DEMANGLE_COMPONENT_UNARY:
3208 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3209 d_print_expr_op (dpi, d_left (dc));
3210 else
3212 d_append_char (dpi, '(');
3213 d_print_cast (dpi, d_left (dc));
3214 d_append_char (dpi, ')');
3216 d_append_char (dpi, '(');
3217 d_print_comp (dpi, d_right (dc));
3218 d_append_char (dpi, ')');
3219 return;
3221 case DEMANGLE_COMPONENT_BINARY:
3222 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3224 d_print_error (dpi);
3225 return;
3228 /* We wrap an expression which uses the greater-than operator in
3229 an extra layer of parens so that it does not get confused
3230 with the '>' which ends the template parameters. */
3231 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3232 && d_left (dc)->u.s_operator.op->len == 1
3233 && d_left (dc)->u.s_operator.op->name[0] == '>')
3234 d_append_char (dpi, '(');
3236 d_append_char (dpi, '(');
3237 d_print_comp (dpi, d_left (d_right (dc)));
3238 d_append_string_constant (dpi, ") ");
3239 d_print_expr_op (dpi, d_left (dc));
3240 d_append_string_constant (dpi, " (");
3241 d_print_comp (dpi, d_right (d_right (dc)));
3242 d_append_char (dpi, ')');
3244 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3245 && d_left (dc)->u.s_operator.op->len == 1
3246 && d_left (dc)->u.s_operator.op->name[0] == '>')
3247 d_append_char (dpi, ')');
3249 return;
3251 case DEMANGLE_COMPONENT_BINARY_ARGS:
3252 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3253 d_print_error (dpi);
3254 return;
3256 case DEMANGLE_COMPONENT_TRINARY:
3257 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3258 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3260 d_print_error (dpi);
3261 return;
3263 d_append_char (dpi, '(');
3264 d_print_comp (dpi, d_left (d_right (dc)));
3265 d_append_string_constant (dpi, ") ");
3266 d_print_expr_op (dpi, d_left (dc));
3267 d_append_string_constant (dpi, " (");
3268 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3269 d_append_string_constant (dpi, ") : (");
3270 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3271 d_append_char (dpi, ')');
3272 return;
3274 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3275 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3276 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3277 d_print_error (dpi);
3278 return;
3280 case DEMANGLE_COMPONENT_LITERAL:
3281 case DEMANGLE_COMPONENT_LITERAL_NEG:
3283 enum d_builtin_type_print tp;
3285 /* For some builtin types, produce simpler output. */
3286 tp = D_PRINT_DEFAULT;
3287 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3289 tp = d_left (dc)->u.s_builtin.type->print;
3290 switch (tp)
3292 case D_PRINT_INT:
3293 case D_PRINT_UNSIGNED:
3294 case D_PRINT_LONG:
3295 case D_PRINT_UNSIGNED_LONG:
3296 case D_PRINT_LONG_LONG:
3297 case D_PRINT_UNSIGNED_LONG_LONG:
3298 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3300 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3301 d_append_char (dpi, '-');
3302 d_print_comp (dpi, d_right (dc));
3303 switch (tp)
3305 default:
3306 break;
3307 case D_PRINT_UNSIGNED:
3308 d_append_char (dpi, 'u');
3309 break;
3310 case D_PRINT_LONG:
3311 d_append_char (dpi, 'l');
3312 break;
3313 case D_PRINT_UNSIGNED_LONG:
3314 d_append_string_constant (dpi, "ul");
3315 break;
3316 case D_PRINT_LONG_LONG:
3317 d_append_string_constant (dpi, "ll");
3318 break;
3319 case D_PRINT_UNSIGNED_LONG_LONG:
3320 d_append_string_constant (dpi, "ull");
3321 break;
3323 return;
3325 break;
3327 case D_PRINT_BOOL:
3328 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3329 && d_right (dc)->u.s_name.len == 1
3330 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3332 switch (d_right (dc)->u.s_name.s[0])
3334 case '0':
3335 d_append_string_constant (dpi, "false");
3336 return;
3337 case '1':
3338 d_append_string_constant (dpi, "true");
3339 return;
3340 default:
3341 break;
3344 break;
3346 default:
3347 break;
3351 d_append_char (dpi, '(');
3352 d_print_comp (dpi, d_left (dc));
3353 d_append_char (dpi, ')');
3354 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3355 d_append_char (dpi, '-');
3356 if (tp == D_PRINT_FLOAT)
3357 d_append_char (dpi, '[');
3358 d_print_comp (dpi, d_right (dc));
3359 if (tp == D_PRINT_FLOAT)
3360 d_append_char (dpi, ']');
3362 return;
3364 default:
3365 d_print_error (dpi);
3366 return;
3370 /* Print a Java dentifier. For Java we try to handle encoded extended
3371 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3372 so we don't it for C++. Characters are encoded as
3373 __U<hex-char>+_. */
3375 static void
3376 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3378 const char *p;
3379 const char *end;
3381 end = name + len;
3382 for (p = name; p < end; ++p)
3384 if (end - p > 3
3385 && p[0] == '_'
3386 && p[1] == '_'
3387 && p[2] == 'U')
3389 unsigned long c;
3390 const char *q;
3392 c = 0;
3393 for (q = p + 3; q < end; ++q)
3395 int dig;
3397 if (IS_DIGIT (*q))
3398 dig = *q - '0';
3399 else if (*q >= 'A' && *q <= 'F')
3400 dig = *q - 'A' + 10;
3401 else if (*q >= 'a' && *q <= 'f')
3402 dig = *q - 'a' + 10;
3403 else
3404 break;
3406 c = c * 16 + dig;
3408 /* If the Unicode character is larger than 256, we don't try
3409 to deal with it here. FIXME. */
3410 if (q < end && *q == '_' && c < 256)
3412 d_append_char (dpi, c);
3413 p = q;
3414 continue;
3418 d_append_char (dpi, *p);
3422 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3423 qualifiers on this after printing a function. */
3425 static void
3426 d_print_mod_list (struct d_print_info *dpi,
3427 struct d_print_mod *mods, int suffix)
3429 struct d_print_template *hold_dpt;
3431 if (mods == NULL || d_print_saw_error (dpi))
3432 return;
3434 if (mods->printed
3435 || (! suffix
3436 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3437 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3438 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3440 d_print_mod_list (dpi, mods->next, suffix);
3441 return;
3444 mods->printed = 1;
3446 hold_dpt = dpi->templates;
3447 dpi->templates = mods->templates;
3449 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3451 d_print_function_type (dpi, mods->mod, mods->next);
3452 dpi->templates = hold_dpt;
3453 return;
3455 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3457 d_print_array_type (dpi, mods->mod, mods->next);
3458 dpi->templates = hold_dpt;
3459 return;
3461 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3463 struct d_print_mod *hold_modifiers;
3464 struct demangle_component *dc;
3466 /* When this is on the modifier stack, we have pulled any
3467 qualifiers off the right argument already. Otherwise, we
3468 print it as usual, but don't let the left argument see any
3469 modifiers. */
3471 hold_modifiers = dpi->modifiers;
3472 dpi->modifiers = NULL;
3473 d_print_comp (dpi, d_left (mods->mod));
3474 dpi->modifiers = hold_modifiers;
3476 if ((dpi->options & DMGL_JAVA) == 0)
3477 d_append_string_constant (dpi, "::");
3478 else
3479 d_append_char (dpi, '.');
3481 dc = d_right (mods->mod);
3482 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3483 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3484 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3485 dc = d_left (dc);
3487 d_print_comp (dpi, dc);
3489 dpi->templates = hold_dpt;
3490 return;
3493 d_print_mod (dpi, mods->mod);
3495 dpi->templates = hold_dpt;
3497 d_print_mod_list (dpi, mods->next, suffix);
3500 /* Print a modifier. */
3502 static void
3503 d_print_mod (struct d_print_info *dpi,
3504 const struct demangle_component *mod)
3506 switch (mod->type)
3508 case DEMANGLE_COMPONENT_RESTRICT:
3509 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3510 d_append_string_constant (dpi, " restrict");
3511 return;
3512 case DEMANGLE_COMPONENT_VOLATILE:
3513 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3514 d_append_string_constant (dpi, " volatile");
3515 return;
3516 case DEMANGLE_COMPONENT_CONST:
3517 case DEMANGLE_COMPONENT_CONST_THIS:
3518 d_append_string_constant (dpi, " const");
3519 return;
3520 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3521 d_append_char (dpi, ' ');
3522 d_print_comp (dpi, d_right (mod));
3523 return;
3524 case DEMANGLE_COMPONENT_POINTER:
3525 /* There is no pointer symbol in Java. */
3526 if ((dpi->options & DMGL_JAVA) == 0)
3527 d_append_char (dpi, '*');
3528 return;
3529 case DEMANGLE_COMPONENT_REFERENCE:
3530 d_append_char (dpi, '&');
3531 return;
3532 case DEMANGLE_COMPONENT_COMPLEX:
3533 d_append_string_constant (dpi, "complex ");
3534 return;
3535 case DEMANGLE_COMPONENT_IMAGINARY:
3536 d_append_string_constant (dpi, "imaginary ");
3537 return;
3538 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3539 if (d_last_char (dpi) != '(')
3540 d_append_char (dpi, ' ');
3541 d_print_comp (dpi, d_left (mod));
3542 d_append_string_constant (dpi, "::*");
3543 return;
3544 case DEMANGLE_COMPONENT_TYPED_NAME:
3545 d_print_comp (dpi, d_left (mod));
3546 return;
3547 default:
3548 /* Otherwise, we have something that won't go back on the
3549 modifier stack, so we can just print it. */
3550 d_print_comp (dpi, mod);
3551 return;
3555 /* Print a function type, except for the return type. */
3557 static void
3558 d_print_function_type (struct d_print_info *dpi,
3559 const struct demangle_component *dc,
3560 struct d_print_mod *mods)
3562 int need_paren;
3563 int saw_mod;
3564 int need_space;
3565 struct d_print_mod *p;
3566 struct d_print_mod *hold_modifiers;
3568 need_paren = 0;
3569 saw_mod = 0;
3570 need_space = 0;
3571 for (p = mods; p != NULL; p = p->next)
3573 if (p->printed)
3574 break;
3576 saw_mod = 1;
3577 switch (p->mod->type)
3579 case DEMANGLE_COMPONENT_POINTER:
3580 case DEMANGLE_COMPONENT_REFERENCE:
3581 need_paren = 1;
3582 break;
3583 case DEMANGLE_COMPONENT_RESTRICT:
3584 case DEMANGLE_COMPONENT_VOLATILE:
3585 case DEMANGLE_COMPONENT_CONST:
3586 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3587 case DEMANGLE_COMPONENT_COMPLEX:
3588 case DEMANGLE_COMPONENT_IMAGINARY:
3589 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3590 need_space = 1;
3591 need_paren = 1;
3592 break;
3593 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3594 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3595 case DEMANGLE_COMPONENT_CONST_THIS:
3596 break;
3597 default:
3598 break;
3600 if (need_paren)
3601 break;
3604 if (d_left (dc) != NULL && ! saw_mod)
3605 need_paren = 1;
3607 if (need_paren)
3609 if (! need_space)
3611 if (d_last_char (dpi) != '('
3612 && d_last_char (dpi) != '*')
3613 need_space = 1;
3615 if (need_space && d_last_char (dpi) != ' ')
3616 d_append_char (dpi, ' ');
3617 d_append_char (dpi, '(');
3620 hold_modifiers = dpi->modifiers;
3621 dpi->modifiers = NULL;
3623 d_print_mod_list (dpi, mods, 0);
3625 if (need_paren)
3626 d_append_char (dpi, ')');
3628 d_append_char (dpi, '(');
3630 if (d_right (dc) != NULL)
3631 d_print_comp (dpi, d_right (dc));
3633 d_append_char (dpi, ')');
3635 d_print_mod_list (dpi, mods, 1);
3637 dpi->modifiers = hold_modifiers;
3640 /* Print an array type, except for the element type. */
3642 static void
3643 d_print_array_type (struct d_print_info *dpi,
3644 const struct demangle_component *dc,
3645 struct d_print_mod *mods)
3647 int need_space;
3649 need_space = 1;
3650 if (mods != NULL)
3652 int need_paren;
3653 struct d_print_mod *p;
3655 need_paren = 0;
3656 for (p = mods; p != NULL; p = p->next)
3658 if (! p->printed)
3660 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3662 need_space = 0;
3663 break;
3665 else
3667 need_paren = 1;
3668 need_space = 1;
3669 break;
3674 if (need_paren)
3675 d_append_string_constant (dpi, " (");
3677 d_print_mod_list (dpi, mods, 0);
3679 if (need_paren)
3680 d_append_char (dpi, ')');
3683 if (need_space)
3684 d_append_char (dpi, ' ');
3686 d_append_char (dpi, '[');
3688 if (d_left (dc) != NULL)
3689 d_print_comp (dpi, d_left (dc));
3691 d_append_char (dpi, ']');
3694 /* Print an operator in an expression. */
3696 static void
3697 d_print_expr_op (struct d_print_info *dpi,
3698 const struct demangle_component *dc)
3700 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3701 d_append_buffer (dpi, dc->u.s_operator.op->name,
3702 dc->u.s_operator.op->len);
3703 else
3704 d_print_comp (dpi, dc);
3707 /* Print a cast. */
3709 static void
3710 d_print_cast (struct d_print_info *dpi,
3711 const struct demangle_component *dc)
3713 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3714 d_print_comp (dpi, d_left (dc));
3715 else
3717 struct d_print_mod *hold_dpm;
3718 struct d_print_template dpt;
3720 /* It appears that for a templated cast operator, we need to put
3721 the template parameters in scope for the operator name, but
3722 not for the parameters. The effect is that we need to handle
3723 the template printing here. */
3725 hold_dpm = dpi->modifiers;
3726 dpi->modifiers = NULL;
3728 dpt.next = dpi->templates;
3729 dpi->templates = &dpt;
3730 dpt.template_decl = d_left (dc);
3732 d_print_comp (dpi, d_left (d_left (dc)));
3734 dpi->templates = dpt.next;
3736 if (d_last_char (dpi) == '<')
3737 d_append_char (dpi, ' ');
3738 d_append_char (dpi, '<');
3739 d_print_comp (dpi, d_right (d_left (dc)));
3740 /* Avoid generating two consecutive '>' characters, to avoid
3741 the C++ syntactic ambiguity. */
3742 if (d_last_char (dpi) == '>')
3743 d_append_char (dpi, ' ');
3744 d_append_char (dpi, '>');
3746 dpi->modifiers = hold_dpm;
3750 /* Initialize the information structure we use to pass around
3751 information. */
3753 CP_STATIC_IF_GLIBCPP_V3
3754 void
3755 cplus_demangle_init_info (const char *mangled, int options, size_t len,
3756 struct d_info *di)
3758 di->s = mangled;
3759 di->send = mangled + len;
3760 di->options = options;
3762 di->n = mangled;
3764 /* We can not need more components than twice the number of chars in
3765 the mangled string. Most components correspond directly to
3766 chars, but the ARGLIST types are exceptions. */
3767 di->num_comps = 2 * len;
3768 di->next_comp = 0;
3770 /* Similarly, we can not need more substitutions than there are
3771 chars in the mangled string. */
3772 di->num_subs = len;
3773 di->next_sub = 0;
3774 di->did_subs = 0;
3776 di->last_name = NULL;
3778 di->expansion = 0;
3781 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3782 name, return a buffer allocated with malloc holding the demangled
3783 name. OPTIONS is the usual libiberty demangler options. On
3784 success, this sets *PALC to the allocated size of the returned
3785 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3786 a memory allocation failure. On failure, this returns NULL. */
3788 static char *
3789 d_demangle (const char* mangled, int options, size_t *palc)
3791 size_t len;
3792 int type;
3793 struct d_info di;
3794 struct demangle_component *dc;
3795 int estimate;
3796 char *ret;
3798 *palc = 0;
3800 len = strlen (mangled);
3802 if (mangled[0] == '_' && mangled[1] == 'Z')
3803 type = 0;
3804 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3805 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3806 && (mangled[9] == 'D' || mangled[9] == 'I')
3807 && mangled[10] == '_')
3809 char *r;
3811 r = (char *) malloc (40 + len - 11);
3812 if (r == NULL)
3813 *palc = 1;
3814 else
3816 if (mangled[9] == 'I')
3817 strcpy (r, "global constructors keyed to ");
3818 else
3819 strcpy (r, "global destructors keyed to ");
3820 strcat (r, mangled + 11);
3822 return r;
3824 else
3826 if ((options & DMGL_TYPES) == 0)
3827 return NULL;
3828 type = 1;
3831 cplus_demangle_init_info (mangled, options, len, &di);
3834 #ifdef CP_DYNAMIC_ARRAYS
3835 __extension__ struct demangle_component comps[di.num_comps];
3836 __extension__ struct demangle_component *subs[di.num_subs];
3838 di.comps = &comps[0];
3839 di.subs = &subs[0];
3840 #else
3841 di.comps = ((struct demangle_component *)
3842 malloc (di.num_comps * sizeof (struct demangle_component)));
3843 di.subs = ((struct demangle_component **)
3844 malloc (di.num_subs * sizeof (struct demangle_component *)));
3845 if (di.comps == NULL || di.subs == NULL)
3847 if (di.comps != NULL)
3848 free (di.comps);
3849 if (di.subs != NULL)
3850 free (di.subs);
3851 *palc = 1;
3852 return NULL;
3854 #endif
3856 if (! type)
3857 dc = cplus_demangle_mangled_name (&di, 1);
3858 else
3859 dc = cplus_demangle_type (&di);
3861 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3862 mangled string, then we didn't successfully demangle it. If
3863 DMGL_PARAMS is not set, we didn't look at the trailing
3864 parameters. */
3865 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3866 dc = NULL;
3868 #ifdef CP_DEMANGLE_DEBUG
3869 if (dc == NULL)
3870 printf ("failed demangling\n");
3871 else
3872 d_dump (dc, 0);
3873 #endif
3875 /* We try to guess the length of the demangled string, to minimize
3876 calls to realloc during demangling. */
3877 estimate = len + di.expansion + 10 * di.did_subs;
3878 estimate += estimate / 8;
3880 ret = NULL;
3881 if (dc != NULL)
3882 ret = cplus_demangle_print (options, dc, estimate, palc);
3884 #ifndef CP_DYNAMIC_ARRAYS
3885 free (di.comps);
3886 free (di.subs);
3887 #endif
3889 #ifdef CP_DEMANGLE_DEBUG
3890 if (ret != NULL)
3892 int rlen;
3894 rlen = strlen (ret);
3895 if (rlen > 2 * estimate)
3896 printf ("*** Length %d much greater than estimate %d\n",
3897 rlen, estimate);
3898 else if (rlen > estimate)
3899 printf ("*** Length %d greater than estimate %d\n",
3900 rlen, estimate);
3901 else if (rlen < estimate / 2)
3902 printf ("*** Length %d much less than estimate %d\n",
3903 rlen, estimate);
3905 #endif
3908 return ret;
3911 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3913 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
3915 /* ia64 ABI-mandated entry point in the C++ runtime library for
3916 performing demangling. MANGLED_NAME is a NUL-terminated character
3917 string containing the name to be demangled.
3919 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3920 *LENGTH bytes, into which the demangled name is stored. If
3921 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3922 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3923 is placed in a region of memory allocated with malloc.
3925 If LENGTH is non-NULL, the length of the buffer conaining the
3926 demangled name, is placed in *LENGTH.
3928 The return value is a pointer to the start of the NUL-terminated
3929 demangled name, or NULL if the demangling fails. The caller is
3930 responsible for deallocating this memory using free.
3932 *STATUS is set to one of the following values:
3933 0: The demangling operation succeeded.
3934 -1: A memory allocation failure occurred.
3935 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3936 -3: One of the arguments is invalid.
3938 The demangling is performed using the C++ ABI mangling rules, with
3939 GNU extensions. */
3941 char *
3942 __cxa_demangle (const char *mangled_name, char *output_buffer,
3943 size_t *length, int *status)
3945 char *demangled;
3946 size_t alc;
3948 if (mangled_name == NULL)
3950 if (status != NULL)
3951 *status = -3;
3952 return NULL;
3955 if (output_buffer != NULL && length == NULL)
3957 if (status != NULL)
3958 *status = -3;
3959 return NULL;
3962 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
3964 if (demangled == NULL)
3966 if (status != NULL)
3968 if (alc == 1)
3969 *status = -1;
3970 else
3971 *status = -2;
3973 return NULL;
3976 if (output_buffer == NULL)
3978 if (length != NULL)
3979 *length = alc;
3981 else
3983 if (strlen (demangled) < *length)
3985 strcpy (output_buffer, demangled);
3986 free (demangled);
3987 demangled = output_buffer;
3989 else
3991 free (output_buffer);
3992 *length = alc;
3996 if (status != NULL)
3997 *status = 0;
3999 return demangled;
4002 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4004 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4005 mangled name, return a buffer allocated with malloc holding the
4006 demangled name. Otherwise, return NULL. */
4008 char *
4009 cplus_demangle_v3 (const char* mangled, int options)
4011 size_t alc;
4013 return d_demangle (mangled, options, &alc);
4016 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4017 conventions, but the output formatting is a little different.
4018 This instructs the C++ demangler not to emit pointer characters ("*"), and
4019 to use Java's namespace separator symbol ("." instead of "::"). It then
4020 does an additional pass over the demangled output to replace instances
4021 of JArray<TYPE> with TYPE[]. */
4023 char *
4024 java_demangle_v3 (const char* mangled)
4026 size_t alc;
4027 char *demangled;
4028 int nesting;
4029 char *from;
4030 char *to;
4032 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4033 &alc);
4035 if (demangled == NULL)
4036 return NULL;
4038 nesting = 0;
4039 from = demangled;
4040 to = from;
4041 while (*from != '\0')
4043 if (strncmp (from, "JArray<", 7) == 0)
4045 from += 7;
4046 ++nesting;
4048 else if (nesting > 0 && *from == '>')
4050 while (to > demangled && to[-1] == ' ')
4051 --to;
4052 *to++ = '[';
4053 *to++ = ']';
4054 --nesting;
4055 ++from;
4057 else
4058 *to++ = *from++;
4061 *to = '\0';
4063 return demangled;
4066 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4068 #ifndef IN_GLIBCPP_V3
4070 /* Demangle a string in order to find out whether it is a constructor
4071 or destructor. Return non-zero on success. Set *CTOR_KIND and
4072 *DTOR_KIND appropriately. */
4074 static int
4075 is_ctor_or_dtor (const char *mangled,
4076 enum gnu_v3_ctor_kinds *ctor_kind,
4077 enum gnu_v3_dtor_kinds *dtor_kind)
4079 struct d_info di;
4080 struct demangle_component *dc;
4081 int ret;
4083 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4084 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4086 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4089 #ifdef CP_DYNAMIC_ARRAYS
4090 __extension__ struct demangle_component comps[di.num_comps];
4091 __extension__ struct demangle_component *subs[di.num_subs];
4093 di.comps = &comps[0];
4094 di.subs = &subs[0];
4095 #else
4096 di.comps = ((struct demangle_component *)
4097 malloc (di.num_comps * sizeof (struct demangle_component)));
4098 di.subs = ((struct demangle_component **)
4099 malloc (di.num_subs * sizeof (struct demangle_component *)));
4100 if (di.comps == NULL || di.subs == NULL)
4102 if (di.comps != NULL)
4103 free (di.comps);
4104 if (di.subs != NULL)
4105 free (di.subs);
4106 return 0;
4108 #endif
4110 dc = cplus_demangle_mangled_name (&di, 1);
4112 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4113 to demangle the entire string. */
4115 ret = 0;
4116 while (dc != NULL)
4118 switch (dc->type)
4120 default:
4121 dc = NULL;
4122 break;
4123 case DEMANGLE_COMPONENT_TYPED_NAME:
4124 case DEMANGLE_COMPONENT_TEMPLATE:
4125 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4126 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4127 case DEMANGLE_COMPONENT_CONST_THIS:
4128 dc = d_left (dc);
4129 break;
4130 case DEMANGLE_COMPONENT_QUAL_NAME:
4131 case DEMANGLE_COMPONENT_LOCAL_NAME:
4132 dc = d_right (dc);
4133 break;
4134 case DEMANGLE_COMPONENT_CTOR:
4135 *ctor_kind = dc->u.s_ctor.kind;
4136 ret = 1;
4137 dc = NULL;
4138 break;
4139 case DEMANGLE_COMPONENT_DTOR:
4140 *dtor_kind = dc->u.s_dtor.kind;
4141 ret = 1;
4142 dc = NULL;
4143 break;
4147 #ifndef CP_DYNAMIC_ARRAYS
4148 free (di.subs);
4149 free (di.comps);
4150 #endif
4153 return ret;
4156 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4157 name. A non-zero return indicates the type of constructor. */
4159 enum gnu_v3_ctor_kinds
4160 is_gnu_v3_mangled_ctor (const char *name)
4162 enum gnu_v3_ctor_kinds ctor_kind;
4163 enum gnu_v3_dtor_kinds dtor_kind;
4165 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4166 return (enum gnu_v3_ctor_kinds) 0;
4167 return ctor_kind;
4171 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4172 name. A non-zero return indicates the type of destructor. */
4174 enum gnu_v3_dtor_kinds
4175 is_gnu_v3_mangled_dtor (const char *name)
4177 enum gnu_v3_ctor_kinds ctor_kind;
4178 enum gnu_v3_dtor_kinds dtor_kind;
4180 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4181 return (enum gnu_v3_dtor_kinds) 0;
4182 return dtor_kind;
4185 #endif /* IN_GLIBCPP_V3 */
4187 #ifdef STANDALONE_DEMANGLER
4189 #include "getopt.h"
4190 #include "dyn-string.h"
4192 static void print_usage (FILE* fp, int exit_value);
4194 #define IS_ALPHA(CHAR) \
4195 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4196 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4198 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4199 #define is_mangled_char(CHAR) \
4200 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4201 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4203 /* The name of this program, as invoked. */
4204 const char* program_name;
4206 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4208 static void
4209 print_usage (FILE* fp, int exit_value)
4211 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4212 fprintf (fp, "Options:\n");
4213 fprintf (fp, " -h,--help Display this message.\n");
4214 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4215 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4216 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4218 exit (exit_value);
4221 /* Option specification for getopt_long. */
4222 static const struct option long_options[] =
4224 { "help", no_argument, NULL, 'h' },
4225 { "no-params", no_argument, NULL, 'p' },
4226 { "verbose", no_argument, NULL, 'v' },
4227 { NULL, no_argument, NULL, 0 },
4230 /* Main entry for a demangling filter executable. It will demangle
4231 its command line arguments, if any. If none are provided, it will
4232 filter stdin to stdout, replacing any recognized mangled C++ names
4233 with their demangled equivalents. */
4236 main (int argc, char *argv[])
4238 int i;
4239 int opt_char;
4240 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4242 /* Use the program name of this program, as invoked. */
4243 program_name = argv[0];
4245 /* Parse options. */
4248 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4249 switch (opt_char)
4251 case '?': /* Unrecognized option. */
4252 print_usage (stderr, 1);
4253 break;
4255 case 'h':
4256 print_usage (stdout, 0);
4257 break;
4259 case 'p':
4260 options &= ~ DMGL_PARAMS;
4261 break;
4263 case 'v':
4264 options |= DMGL_VERBOSE;
4265 break;
4268 while (opt_char != -1);
4270 if (optind == argc)
4271 /* No command line arguments were provided. Filter stdin. */
4273 dyn_string_t mangled = dyn_string_new (3);
4274 char *s;
4276 /* Read all of input. */
4277 while (!feof (stdin))
4279 char c;
4281 /* Pile characters into mangled until we hit one that can't
4282 occur in a mangled name. */
4283 c = getchar ();
4284 while (!feof (stdin) && is_mangled_char (c))
4286 dyn_string_append_char (mangled, c);
4287 if (feof (stdin))
4288 break;
4289 c = getchar ();
4292 if (dyn_string_length (mangled) > 0)
4294 #ifdef IN_GLIBCPP_V3
4295 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4296 #else
4297 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4298 #endif
4300 if (s != NULL)
4302 fputs (s, stdout);
4303 free (s);
4305 else
4307 /* It might not have been a mangled name. Print the
4308 original text. */
4309 fputs (dyn_string_buf (mangled), stdout);
4312 dyn_string_clear (mangled);
4315 /* If we haven't hit EOF yet, we've read one character that
4316 can't occur in a mangled name, so print it out. */
4317 if (!feof (stdin))
4318 putchar (c);
4321 dyn_string_delete (mangled);
4323 else
4324 /* Demangle command line arguments. */
4326 /* Loop over command line arguments. */
4327 for (i = optind; i < argc; ++i)
4329 char *s;
4330 #ifdef IN_GLIBCPP_V3
4331 int status;
4332 #endif
4334 /* Attempt to demangle. */
4335 #ifdef IN_GLIBCPP_V3
4336 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4337 #else
4338 s = cplus_demangle_v3 (argv[i], options);
4339 #endif
4341 /* If it worked, print the demangled name. */
4342 if (s != NULL)
4344 printf ("%s\n", s);
4345 free (s);
4347 else
4349 #ifdef IN_GLIBCPP_V3
4350 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4351 #else
4352 fprintf (stderr, "Failed: %s\n", argv[i]);
4353 #endif
4358 return 0;
4361 #endif /* STANDALONE_DEMANGLER */