2005-04-17 David S. Miller <davem@davemloft.net>
[official-gcc.git] / libiberty / cp-demangle.c
blobe5b396c7ef2007154d0a1a76263997b7bcaeb562
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
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_RESTRICT:
524 printf ("restrict\n");
525 break;
526 case DEMANGLE_COMPONENT_VOLATILE:
527 printf ("volatile\n");
528 break;
529 case DEMANGLE_COMPONENT_CONST:
530 printf ("const\n");
531 break;
532 case DEMANGLE_COMPONENT_RESTRICT_THIS:
533 printf ("restrict this\n");
534 break;
535 case DEMANGLE_COMPONENT_VOLATILE_THIS:
536 printf ("volatile this\n");
537 break;
538 case DEMANGLE_COMPONENT_CONST_THIS:
539 printf ("const this\n");
540 break;
541 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
542 printf ("vendor type qualifier\n");
543 break;
544 case DEMANGLE_COMPONENT_POINTER:
545 printf ("pointer\n");
546 break;
547 case DEMANGLE_COMPONENT_REFERENCE:
548 printf ("reference\n");
549 break;
550 case DEMANGLE_COMPONENT_COMPLEX:
551 printf ("complex\n");
552 break;
553 case DEMANGLE_COMPONENT_IMAGINARY:
554 printf ("imaginary\n");
555 break;
556 case DEMANGLE_COMPONENT_VENDOR_TYPE:
557 printf ("vendor type\n");
558 break;
559 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
560 printf ("function type\n");
561 break;
562 case DEMANGLE_COMPONENT_ARRAY_TYPE:
563 printf ("array type\n");
564 break;
565 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
566 printf ("pointer to member type\n");
567 break;
568 case DEMANGLE_COMPONENT_ARGLIST:
569 printf ("argument list\n");
570 break;
571 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
572 printf ("template argument list\n");
573 break;
574 case DEMANGLE_COMPONENT_CAST:
575 printf ("cast\n");
576 break;
577 case DEMANGLE_COMPONENT_UNARY:
578 printf ("unary operator\n");
579 break;
580 case DEMANGLE_COMPONENT_BINARY:
581 printf ("binary operator\n");
582 break;
583 case DEMANGLE_COMPONENT_BINARY_ARGS:
584 printf ("binary operator arguments\n");
585 break;
586 case DEMANGLE_COMPONENT_TRINARY:
587 printf ("trinary operator\n");
588 break;
589 case DEMANGLE_COMPONENT_TRINARY_ARG1:
590 printf ("trinary operator arguments 1\n");
591 break;
592 case DEMANGLE_COMPONENT_TRINARY_ARG2:
593 printf ("trinary operator arguments 1\n");
594 break;
595 case DEMANGLE_COMPONENT_LITERAL:
596 printf ("literal\n");
597 break;
598 case DEMANGLE_COMPONENT_LITERAL_NEG:
599 printf ("negative literal\n");
600 break;
603 d_dump (d_left (dc), indent + 2);
604 d_dump (d_right (dc), indent + 2);
607 #endif /* CP_DEMANGLE_DEBUG */
609 /* Fill in a DEMANGLE_COMPONENT_NAME. */
611 CP_STATIC_IF_GLIBCPP_V3
613 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
615 if (p == NULL || s == NULL || len == 0)
616 return 0;
617 p->type = DEMANGLE_COMPONENT_NAME;
618 p->u.s_name.s = s;
619 p->u.s_name.len = len;
620 return 1;
623 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
625 CP_STATIC_IF_GLIBCPP_V3
627 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
628 struct demangle_component *name)
630 if (p == NULL || args < 0 || name == NULL)
631 return 0;
632 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
633 p->u.s_extended_operator.args = args;
634 p->u.s_extended_operator.name = name;
635 return 1;
638 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
640 CP_STATIC_IF_GLIBCPP_V3
642 cplus_demangle_fill_ctor (struct demangle_component *p,
643 enum gnu_v3_ctor_kinds kind,
644 struct demangle_component *name)
646 if (p == NULL
647 || name == NULL
648 || (kind < gnu_v3_complete_object_ctor
649 && kind > gnu_v3_complete_object_allocating_ctor))
650 return 0;
651 p->type = DEMANGLE_COMPONENT_CTOR;
652 p->u.s_ctor.kind = kind;
653 p->u.s_ctor.name = name;
654 return 1;
657 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
659 CP_STATIC_IF_GLIBCPP_V3
661 cplus_demangle_fill_dtor (struct demangle_component *p,
662 enum gnu_v3_dtor_kinds kind,
663 struct demangle_component *name)
665 if (p == NULL
666 || name == NULL
667 || (kind < gnu_v3_deleting_dtor
668 && kind > gnu_v3_base_object_dtor))
669 return 0;
670 p->type = DEMANGLE_COMPONENT_DTOR;
671 p->u.s_dtor.kind = kind;
672 p->u.s_dtor.name = name;
673 return 1;
676 /* Add a new component. */
678 static struct demangle_component *
679 d_make_empty (struct d_info *di)
681 struct demangle_component *p;
683 if (di->next_comp >= di->num_comps)
684 return NULL;
685 p = &di->comps[di->next_comp];
686 ++di->next_comp;
687 return p;
690 /* Add a new generic component. */
692 static struct demangle_component *
693 d_make_comp (struct d_info *di, enum demangle_component_type type,
694 struct demangle_component *left,
695 struct demangle_component *right)
697 struct demangle_component *p;
699 /* We check for errors here. A typical error would be a NULL return
700 from a subroutine. We catch those here, and return NULL
701 upward. */
702 switch (type)
704 /* These types require two parameters. */
705 case DEMANGLE_COMPONENT_QUAL_NAME:
706 case DEMANGLE_COMPONENT_LOCAL_NAME:
707 case DEMANGLE_COMPONENT_TYPED_NAME:
708 case DEMANGLE_COMPONENT_TEMPLATE:
709 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
710 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
711 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
712 case DEMANGLE_COMPONENT_UNARY:
713 case DEMANGLE_COMPONENT_BINARY:
714 case DEMANGLE_COMPONENT_BINARY_ARGS:
715 case DEMANGLE_COMPONENT_TRINARY:
716 case DEMANGLE_COMPONENT_TRINARY_ARG1:
717 case DEMANGLE_COMPONENT_TRINARY_ARG2:
718 case DEMANGLE_COMPONENT_LITERAL:
719 case DEMANGLE_COMPONENT_LITERAL_NEG:
720 if (left == NULL || right == NULL)
721 return NULL;
722 break;
724 /* These types only require one parameter. */
725 case DEMANGLE_COMPONENT_VTABLE:
726 case DEMANGLE_COMPONENT_VTT:
727 case DEMANGLE_COMPONENT_TYPEINFO:
728 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
729 case DEMANGLE_COMPONENT_TYPEINFO_FN:
730 case DEMANGLE_COMPONENT_THUNK:
731 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
732 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
733 case DEMANGLE_COMPONENT_JAVA_CLASS:
734 case DEMANGLE_COMPONENT_GUARD:
735 case DEMANGLE_COMPONENT_REFTEMP:
736 case DEMANGLE_COMPONENT_POINTER:
737 case DEMANGLE_COMPONENT_REFERENCE:
738 case DEMANGLE_COMPONENT_COMPLEX:
739 case DEMANGLE_COMPONENT_IMAGINARY:
740 case DEMANGLE_COMPONENT_VENDOR_TYPE:
741 case DEMANGLE_COMPONENT_ARGLIST:
742 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
743 case DEMANGLE_COMPONENT_CAST:
744 if (left == NULL)
745 return NULL;
746 break;
748 /* This needs a right parameter, but the left parameter can be
749 empty. */
750 case DEMANGLE_COMPONENT_ARRAY_TYPE:
751 if (right == NULL)
752 return NULL;
753 break;
755 /* These are allowed to have no parameters--in some cases they
756 will be filled in later. */
757 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
758 case DEMANGLE_COMPONENT_RESTRICT:
759 case DEMANGLE_COMPONENT_VOLATILE:
760 case DEMANGLE_COMPONENT_CONST:
761 case DEMANGLE_COMPONENT_RESTRICT_THIS:
762 case DEMANGLE_COMPONENT_VOLATILE_THIS:
763 case DEMANGLE_COMPONENT_CONST_THIS:
764 break;
766 /* Other types should not be seen here. */
767 default:
768 return NULL;
771 p = d_make_empty (di);
772 if (p != NULL)
774 p->type = type;
775 p->u.s_binary.left = left;
776 p->u.s_binary.right = right;
778 return p;
781 /* Add a new name component. */
783 static struct demangle_component *
784 d_make_name (struct d_info *di, const char *s, int len)
786 struct demangle_component *p;
788 p = d_make_empty (di);
789 if (! cplus_demangle_fill_name (p, s, len))
790 return NULL;
791 return p;
794 /* Add a new builtin type component. */
796 static struct demangle_component *
797 d_make_builtin_type (struct d_info *di,
798 const struct demangle_builtin_type_info *type)
800 struct demangle_component *p;
802 if (type == NULL)
803 return NULL;
804 p = d_make_empty (di);
805 if (p != NULL)
807 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
808 p->u.s_builtin.type = type;
810 return p;
813 /* Add a new operator component. */
815 static struct demangle_component *
816 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
818 struct demangle_component *p;
820 p = d_make_empty (di);
821 if (p != NULL)
823 p->type = DEMANGLE_COMPONENT_OPERATOR;
824 p->u.s_operator.op = op;
826 return p;
829 /* Add a new extended operator component. */
831 static struct demangle_component *
832 d_make_extended_operator (struct d_info *di, int args,
833 struct demangle_component *name)
835 struct demangle_component *p;
837 p = d_make_empty (di);
838 if (! cplus_demangle_fill_extended_operator (p, args, name))
839 return NULL;
840 return p;
843 /* Add a new constructor component. */
845 static struct demangle_component *
846 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
847 struct demangle_component *name)
849 struct demangle_component *p;
851 p = d_make_empty (di);
852 if (! cplus_demangle_fill_ctor (p, kind, name))
853 return NULL;
854 return p;
857 /* Add a new destructor component. */
859 static struct demangle_component *
860 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
861 struct demangle_component *name)
863 struct demangle_component *p;
865 p = d_make_empty (di);
866 if (! cplus_demangle_fill_dtor (p, kind, name))
867 return NULL;
868 return p;
871 /* Add a new template parameter. */
873 static struct demangle_component *
874 d_make_template_param (struct d_info *di, long i)
876 struct demangle_component *p;
878 p = d_make_empty (di);
879 if (p != NULL)
881 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
882 p->u.s_number.number = i;
884 return p;
887 /* Add a new standard substitution component. */
889 static struct demangle_component *
890 d_make_sub (struct d_info *di, const char *name, int len)
892 struct demangle_component *p;
894 p = d_make_empty (di);
895 if (p != NULL)
897 p->type = DEMANGLE_COMPONENT_SUB_STD;
898 p->u.s_string.string = name;
899 p->u.s_string.len = len;
901 return p;
904 /* <mangled-name> ::= _Z <encoding>
906 TOP_LEVEL is non-zero when called at the top level. */
908 CP_STATIC_IF_GLIBCPP_V3
909 struct demangle_component *
910 cplus_demangle_mangled_name (struct d_info *di, int top_level)
912 if (d_next_char (di) != '_')
913 return NULL;
914 if (d_next_char (di) != 'Z')
915 return NULL;
916 return d_encoding (di, top_level);
919 /* Return whether a function should have a return type. The argument
920 is the function name, which may be qualified in various ways. The
921 rules are that template functions have return types with some
922 exceptions, function types which are not part of a function name
923 mangling have return types with some exceptions, and non-template
924 function names do not have return types. The exceptions are that
925 constructors, destructors, and conversion operators do not have
926 return types. */
928 static int
929 has_return_type (struct demangle_component *dc)
931 if (dc == NULL)
932 return 0;
933 switch (dc->type)
935 default:
936 return 0;
937 case DEMANGLE_COMPONENT_TEMPLATE:
938 return ! is_ctor_dtor_or_conversion (d_left (dc));
939 case DEMANGLE_COMPONENT_RESTRICT_THIS:
940 case DEMANGLE_COMPONENT_VOLATILE_THIS:
941 case DEMANGLE_COMPONENT_CONST_THIS:
942 return has_return_type (d_left (dc));
946 /* Return whether a name is a constructor, a destructor, or a
947 conversion operator. */
949 static int
950 is_ctor_dtor_or_conversion (struct demangle_component *dc)
952 if (dc == NULL)
953 return 0;
954 switch (dc->type)
956 default:
957 return 0;
958 case DEMANGLE_COMPONENT_QUAL_NAME:
959 case DEMANGLE_COMPONENT_LOCAL_NAME:
960 return is_ctor_dtor_or_conversion (d_right (dc));
961 case DEMANGLE_COMPONENT_CTOR:
962 case DEMANGLE_COMPONENT_DTOR:
963 case DEMANGLE_COMPONENT_CAST:
964 return 1;
968 /* <encoding> ::= <(function) name> <bare-function-type>
969 ::= <(data) name>
970 ::= <special-name>
972 TOP_LEVEL is non-zero when called at the top level, in which case
973 if DMGL_PARAMS is not set we do not demangle the function
974 parameters. We only set this at the top level, because otherwise
975 we would not correctly demangle names in local scopes. */
977 static struct demangle_component *
978 d_encoding (struct d_info *di, int top_level)
980 char peek = d_peek_char (di);
982 if (peek == 'G' || peek == 'T')
983 return d_special_name (di);
984 else
986 struct demangle_component *dc;
988 dc = d_name (di);
990 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
992 /* Strip off any initial CV-qualifiers, as they really apply
993 to the `this' parameter, and they were not output by the
994 v2 demangler without DMGL_PARAMS. */
995 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
996 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
997 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
998 dc = d_left (dc);
1000 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1001 there may be CV-qualifiers on its right argument which
1002 really apply here; this happens when parsing a class
1003 which is local to a function. */
1004 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1006 struct demangle_component *dcr;
1008 dcr = d_right (dc);
1009 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1010 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1011 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1012 dcr = d_left (dcr);
1013 dc->u.s_binary.right = dcr;
1016 return dc;
1019 peek = d_peek_char (di);
1020 if (peek == '\0' || peek == 'E')
1021 return dc;
1022 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1023 d_bare_function_type (di, has_return_type (dc)));
1027 /* <name> ::= <nested-name>
1028 ::= <unscoped-name>
1029 ::= <unscoped-template-name> <template-args>
1030 ::= <local-name>
1032 <unscoped-name> ::= <unqualified-name>
1033 ::= St <unqualified-name>
1035 <unscoped-template-name> ::= <unscoped-name>
1036 ::= <substitution>
1039 static struct demangle_component *
1040 d_name (struct d_info *di)
1042 char peek = d_peek_char (di);
1043 struct demangle_component *dc;
1045 switch (peek)
1047 case 'N':
1048 return d_nested_name (di);
1050 case 'Z':
1051 return d_local_name (di);
1053 case 'S':
1055 int subst;
1057 if (d_peek_next_char (di) != 't')
1059 dc = d_substitution (di, 0);
1060 subst = 1;
1062 else
1064 d_advance (di, 2);
1065 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1066 d_make_name (di, "std", 3),
1067 d_unqualified_name (di));
1068 di->expansion += 3;
1069 subst = 0;
1072 if (d_peek_char (di) != 'I')
1074 /* The grammar does not permit this case to occur if we
1075 called d_substitution() above (i.e., subst == 1). We
1076 don't bother to check. */
1078 else
1080 /* This is <template-args>, which means that we just saw
1081 <unscoped-template-name>, which is a substitution
1082 candidate if we didn't just get it from a
1083 substitution. */
1084 if (! subst)
1086 if (! d_add_substitution (di, dc))
1087 return NULL;
1089 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1090 d_template_args (di));
1093 return dc;
1096 default:
1097 dc = d_unqualified_name (di);
1098 if (d_peek_char (di) == 'I')
1100 /* This is <template-args>, which means that we just saw
1101 <unscoped-template-name>, which is a substitution
1102 candidate. */
1103 if (! d_add_substitution (di, dc))
1104 return NULL;
1105 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1106 d_template_args (di));
1108 return dc;
1112 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1113 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1116 static struct demangle_component *
1117 d_nested_name (struct d_info *di)
1119 struct demangle_component *ret;
1120 struct demangle_component **pret;
1122 if (d_next_char (di) != 'N')
1123 return NULL;
1125 pret = d_cv_qualifiers (di, &ret, 1);
1126 if (pret == NULL)
1127 return NULL;
1129 *pret = d_prefix (di);
1130 if (*pret == NULL)
1131 return NULL;
1133 if (d_next_char (di) != 'E')
1134 return NULL;
1136 return ret;
1139 /* <prefix> ::= <prefix> <unqualified-name>
1140 ::= <template-prefix> <template-args>
1141 ::= <template-param>
1143 ::= <substitution>
1145 <template-prefix> ::= <prefix> <(template) unqualified-name>
1146 ::= <template-param>
1147 ::= <substitution>
1150 static struct demangle_component *
1151 d_prefix (struct d_info *di)
1153 struct demangle_component *ret = NULL;
1155 while (1)
1157 char peek;
1158 enum demangle_component_type comb_type;
1159 struct demangle_component *dc;
1161 peek = d_peek_char (di);
1162 if (peek == '\0')
1163 return NULL;
1165 /* The older code accepts a <local-name> here, but I don't see
1166 that in the grammar. The older code does not accept a
1167 <template-param> here. */
1169 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1170 if (IS_DIGIT (peek)
1171 || IS_LOWER (peek)
1172 || peek == 'C'
1173 || peek == 'D')
1174 dc = d_unqualified_name (di);
1175 else if (peek == 'S')
1176 dc = d_substitution (di, 1);
1177 else if (peek == 'I')
1179 if (ret == NULL)
1180 return NULL;
1181 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1182 dc = d_template_args (di);
1184 else if (peek == 'T')
1185 dc = d_template_param (di);
1186 else if (peek == 'E')
1187 return ret;
1188 else
1189 return NULL;
1191 if (ret == NULL)
1192 ret = dc;
1193 else
1194 ret = d_make_comp (di, comb_type, ret, dc);
1196 if (peek != 'S' && d_peek_char (di) != 'E')
1198 if (! d_add_substitution (di, ret))
1199 return NULL;
1204 /* <unqualified-name> ::= <operator-name>
1205 ::= <ctor-dtor-name>
1206 ::= <source-name>
1209 static struct demangle_component *
1210 d_unqualified_name (struct d_info *di)
1212 char peek;
1214 peek = d_peek_char (di);
1215 if (IS_DIGIT (peek))
1216 return d_source_name (di);
1217 else if (IS_LOWER (peek))
1219 struct demangle_component *ret;
1221 ret = d_operator_name (di);
1222 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1223 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1224 return ret;
1226 else if (peek == 'C' || peek == 'D')
1227 return d_ctor_dtor_name (di);
1228 else
1229 return NULL;
1232 /* <source-name> ::= <(positive length) number> <identifier> */
1234 static struct demangle_component *
1235 d_source_name (struct d_info *di)
1237 long len;
1238 struct demangle_component *ret;
1240 len = d_number (di);
1241 if (len <= 0)
1242 return NULL;
1243 ret = d_identifier (di, len);
1244 di->last_name = ret;
1245 return ret;
1248 /* number ::= [n] <(non-negative decimal integer)> */
1250 static long
1251 d_number (struct d_info *di)
1253 int negative;
1254 char peek;
1255 long ret;
1257 negative = 0;
1258 peek = d_peek_char (di);
1259 if (peek == 'n')
1261 negative = 1;
1262 d_advance (di, 1);
1263 peek = d_peek_char (di);
1266 ret = 0;
1267 while (1)
1269 if (! IS_DIGIT (peek))
1271 if (negative)
1272 ret = - ret;
1273 return ret;
1275 ret = ret * 10 + peek - '0';
1276 d_advance (di, 1);
1277 peek = d_peek_char (di);
1281 /* identifier ::= <(unqualified source code identifier)> */
1283 static struct demangle_component *
1284 d_identifier (struct d_info *di, int len)
1286 const char *name;
1288 name = d_str (di);
1290 if (di->send - name < len)
1291 return NULL;
1293 d_advance (di, len);
1295 /* A Java mangled name may have a trailing '$' if it is a C++
1296 keyword. This '$' is not included in the length count. We just
1297 ignore the '$'. */
1298 if ((di->options & DMGL_JAVA) != 0
1299 && d_peek_char (di) == '$')
1300 d_advance (di, 1);
1302 /* Look for something which looks like a gcc encoding of an
1303 anonymous namespace, and replace it with a more user friendly
1304 name. */
1305 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1306 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1307 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1309 const char *s;
1311 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1312 if ((*s == '.' || *s == '_' || *s == '$')
1313 && s[1] == 'N')
1315 di->expansion -= len - sizeof "(anonymous namespace)";
1316 return d_make_name (di, "(anonymous namespace)",
1317 sizeof "(anonymous namespace)" - 1);
1321 return d_make_name (di, name, len);
1324 /* operator_name ::= many different two character encodings.
1325 ::= cv <type>
1326 ::= v <digit> <source-name>
1329 #define NL(s) s, (sizeof s) - 1
1331 CP_STATIC_IF_GLIBCPP_V3
1332 const struct demangle_operator_info cplus_demangle_operators[] =
1334 { "aN", NL ("&="), 2 },
1335 { "aS", NL ("="), 2 },
1336 { "aa", NL ("&&"), 2 },
1337 { "ad", NL ("&"), 1 },
1338 { "an", NL ("&"), 2 },
1339 { "cl", NL ("()"), 0 },
1340 { "cm", NL (","), 2 },
1341 { "co", NL ("~"), 1 },
1342 { "dV", NL ("/="), 2 },
1343 { "da", NL ("delete[]"), 1 },
1344 { "de", NL ("*"), 1 },
1345 { "dl", NL ("delete"), 1 },
1346 { "dv", NL ("/"), 2 },
1347 { "eO", NL ("^="), 2 },
1348 { "eo", NL ("^"), 2 },
1349 { "eq", NL ("=="), 2 },
1350 { "ge", NL (">="), 2 },
1351 { "gt", NL (">"), 2 },
1352 { "ix", NL ("[]"), 2 },
1353 { "lS", NL ("<<="), 2 },
1354 { "le", NL ("<="), 2 },
1355 { "ls", NL ("<<"), 2 },
1356 { "lt", NL ("<"), 2 },
1357 { "mI", NL ("-="), 2 },
1358 { "mL", NL ("*="), 2 },
1359 { "mi", NL ("-"), 2 },
1360 { "ml", NL ("*"), 2 },
1361 { "mm", NL ("--"), 1 },
1362 { "na", NL ("new[]"), 1 },
1363 { "ne", NL ("!="), 2 },
1364 { "ng", NL ("-"), 1 },
1365 { "nt", NL ("!"), 1 },
1366 { "nw", NL ("new"), 1 },
1367 { "oR", NL ("|="), 2 },
1368 { "oo", NL ("||"), 2 },
1369 { "or", NL ("|"), 2 },
1370 { "pL", NL ("+="), 2 },
1371 { "pl", NL ("+"), 2 },
1372 { "pm", NL ("->*"), 2 },
1373 { "pp", NL ("++"), 1 },
1374 { "ps", NL ("+"), 1 },
1375 { "pt", NL ("->"), 2 },
1376 { "qu", NL ("?"), 3 },
1377 { "rM", NL ("%="), 2 },
1378 { "rS", NL (">>="), 2 },
1379 { "rm", NL ("%"), 2 },
1380 { "rs", NL (">>"), 2 },
1381 { "st", NL ("sizeof "), 1 },
1382 { "sz", NL ("sizeof "), 1 },
1383 { NULL, NULL, 0, 0 }
1386 static struct demangle_component *
1387 d_operator_name (struct d_info *di)
1389 char c1;
1390 char c2;
1392 c1 = d_next_char (di);
1393 c2 = d_next_char (di);
1394 if (c1 == 'v' && IS_DIGIT (c2))
1395 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1396 else if (c1 == 'c' && c2 == 'v')
1397 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1398 cplus_demangle_type (di), NULL);
1399 else
1401 /* LOW is the inclusive lower bound. */
1402 int low = 0;
1403 /* HIGH is the exclusive upper bound. We subtract one to ignore
1404 the sentinel at the end of the array. */
1405 int high = ((sizeof (cplus_demangle_operators)
1406 / sizeof (cplus_demangle_operators[0]))
1407 - 1);
1409 while (1)
1411 int i;
1412 const struct demangle_operator_info *p;
1414 i = low + (high - low) / 2;
1415 p = cplus_demangle_operators + i;
1417 if (c1 == p->code[0] && c2 == p->code[1])
1418 return d_make_operator (di, p);
1420 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1421 high = i;
1422 else
1423 low = i + 1;
1424 if (low == high)
1425 return NULL;
1430 /* <special-name> ::= TV <type>
1431 ::= TT <type>
1432 ::= TI <type>
1433 ::= TS <type>
1434 ::= GV <(object) name>
1435 ::= T <call-offset> <(base) encoding>
1436 ::= Tc <call-offset> <call-offset> <(base) encoding>
1437 Also g++ extensions:
1438 ::= TC <type> <(offset) number> _ <(base) type>
1439 ::= TF <type>
1440 ::= TJ <type>
1441 ::= GR <name>
1444 static struct demangle_component *
1445 d_special_name (struct d_info *di)
1447 char c;
1449 di->expansion += 20;
1450 c = d_next_char (di);
1451 if (c == 'T')
1453 switch (d_next_char (di))
1455 case 'V':
1456 di->expansion -= 5;
1457 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1458 cplus_demangle_type (di), NULL);
1459 case 'T':
1460 di->expansion -= 10;
1461 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1462 cplus_demangle_type (di), NULL);
1463 case 'I':
1464 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1465 cplus_demangle_type (di), NULL);
1466 case 'S':
1467 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1468 cplus_demangle_type (di), NULL);
1470 case 'h':
1471 if (! d_call_offset (di, 'h'))
1472 return NULL;
1473 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1474 d_encoding (di, 0), NULL);
1476 case 'v':
1477 if (! d_call_offset (di, 'v'))
1478 return NULL;
1479 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1480 d_encoding (di, 0), NULL);
1482 case 'c':
1483 if (! d_call_offset (di, '\0'))
1484 return NULL;
1485 if (! d_call_offset (di, '\0'))
1486 return NULL;
1487 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1488 d_encoding (di, 0), NULL);
1490 case 'C':
1492 struct demangle_component *derived_type;
1493 long offset;
1494 struct demangle_component *base_type;
1496 derived_type = cplus_demangle_type (di);
1497 offset = d_number (di);
1498 if (offset < 0)
1499 return NULL;
1500 if (d_next_char (di) != '_')
1501 return NULL;
1502 base_type = cplus_demangle_type (di);
1503 /* We don't display the offset. FIXME: We should display
1504 it in verbose mode. */
1505 di->expansion += 5;
1506 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1507 base_type, derived_type);
1510 case 'F':
1511 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1512 cplus_demangle_type (di), NULL);
1513 case 'J':
1514 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1515 cplus_demangle_type (di), NULL);
1517 default:
1518 return NULL;
1521 else if (c == 'G')
1523 switch (d_next_char (di))
1525 case 'V':
1526 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1528 case 'R':
1529 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1530 NULL);
1532 default:
1533 return NULL;
1536 else
1537 return NULL;
1540 /* <call-offset> ::= h <nv-offset> _
1541 ::= v <v-offset> _
1543 <nv-offset> ::= <(offset) number>
1545 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1547 The C parameter, if not '\0', is a character we just read which is
1548 the start of the <call-offset>.
1550 We don't display the offset information anywhere. FIXME: We should
1551 display it in verbose mode. */
1553 static int
1554 d_call_offset (struct d_info *di, int c)
1556 if (c == '\0')
1557 c = d_next_char (di);
1559 if (c == 'h')
1560 d_number (di);
1561 else if (c == 'v')
1563 d_number (di);
1564 if (d_next_char (di) != '_')
1565 return 0;
1566 d_number (di);
1568 else
1569 return 0;
1571 if (d_next_char (di) != '_')
1572 return 0;
1574 return 1;
1577 /* <ctor-dtor-name> ::= C1
1578 ::= C2
1579 ::= C3
1580 ::= D0
1581 ::= D1
1582 ::= D2
1585 static struct demangle_component *
1586 d_ctor_dtor_name (struct d_info *di)
1588 if (di->last_name != NULL)
1590 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1591 di->expansion += di->last_name->u.s_name.len;
1592 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1593 di->expansion += di->last_name->u.s_string.len;
1595 switch (d_next_char (di))
1597 case 'C':
1599 enum gnu_v3_ctor_kinds kind;
1601 switch (d_next_char (di))
1603 case '1':
1604 kind = gnu_v3_complete_object_ctor;
1605 break;
1606 case '2':
1607 kind = gnu_v3_base_object_ctor;
1608 break;
1609 case '3':
1610 kind = gnu_v3_complete_object_allocating_ctor;
1611 break;
1612 default:
1613 return NULL;
1615 return d_make_ctor (di, kind, di->last_name);
1618 case 'D':
1620 enum gnu_v3_dtor_kinds kind;
1622 switch (d_next_char (di))
1624 case '0':
1625 kind = gnu_v3_deleting_dtor;
1626 break;
1627 case '1':
1628 kind = gnu_v3_complete_object_dtor;
1629 break;
1630 case '2':
1631 kind = gnu_v3_base_object_dtor;
1632 break;
1633 default:
1634 return NULL;
1636 return d_make_dtor (di, kind, di->last_name);
1639 default:
1640 return NULL;
1644 /* <type> ::= <builtin-type>
1645 ::= <function-type>
1646 ::= <class-enum-type>
1647 ::= <array-type>
1648 ::= <pointer-to-member-type>
1649 ::= <template-param>
1650 ::= <template-template-param> <template-args>
1651 ::= <substitution>
1652 ::= <CV-qualifiers> <type>
1653 ::= P <type>
1654 ::= R <type>
1655 ::= C <type>
1656 ::= G <type>
1657 ::= U <source-name> <type>
1659 <builtin-type> ::= various one letter codes
1660 ::= u <source-name>
1663 CP_STATIC_IF_GLIBCPP_V3
1664 const struct demangle_builtin_type_info
1665 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1667 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1668 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1669 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1670 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1671 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1672 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1673 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1674 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1675 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1676 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1677 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1678 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1679 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1680 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1681 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1682 D_PRINT_DEFAULT },
1683 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1684 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1685 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1686 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1687 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1688 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1689 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1690 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1691 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1692 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1693 D_PRINT_UNSIGNED_LONG_LONG },
1694 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1697 CP_STATIC_IF_GLIBCPP_V3
1698 struct demangle_component *
1699 cplus_demangle_type (struct d_info *di)
1701 char peek;
1702 struct demangle_component *ret;
1703 int can_subst;
1705 /* The ABI specifies that when CV-qualifiers are used, the base type
1706 is substitutable, and the fully qualified type is substitutable,
1707 but the base type with a strict subset of the CV-qualifiers is
1708 not substitutable. The natural recursive implementation of the
1709 CV-qualifiers would cause subsets to be substitutable, so instead
1710 we pull them all off now.
1712 FIXME: The ABI says that order-insensitive vendor qualifiers
1713 should be handled in the same way, but we have no way to tell
1714 which vendor qualifiers are order-insensitive and which are
1715 order-sensitive. So we just assume that they are all
1716 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1717 __vector, and it treats it as order-sensitive when mangling
1718 names. */
1720 peek = d_peek_char (di);
1721 if (peek == 'r' || peek == 'V' || peek == 'K')
1723 struct demangle_component **pret;
1725 pret = d_cv_qualifiers (di, &ret, 0);
1726 if (pret == NULL)
1727 return NULL;
1728 *pret = cplus_demangle_type (di);
1729 if (! d_add_substitution (di, ret))
1730 return NULL;
1731 return ret;
1734 can_subst = 1;
1736 switch (peek)
1738 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1739 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1740 case 'o': case 's': case 't':
1741 case 'v': case 'w': case 'x': case 'y': case 'z':
1742 ret = d_make_builtin_type (di,
1743 &cplus_demangle_builtin_types[peek - 'a']);
1744 di->expansion += ret->u.s_builtin.type->len;
1745 can_subst = 0;
1746 d_advance (di, 1);
1747 break;
1749 case 'u':
1750 d_advance (di, 1);
1751 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1752 d_source_name (di), NULL);
1753 break;
1755 case 'F':
1756 ret = d_function_type (di);
1757 break;
1759 case '0': case '1': case '2': case '3': case '4':
1760 case '5': case '6': case '7': case '8': case '9':
1761 case 'N':
1762 case 'Z':
1763 ret = d_class_enum_type (di);
1764 break;
1766 case 'A':
1767 ret = d_array_type (di);
1768 break;
1770 case 'M':
1771 ret = d_pointer_to_member_type (di);
1772 break;
1774 case 'T':
1775 ret = d_template_param (di);
1776 if (d_peek_char (di) == 'I')
1778 /* This is <template-template-param> <template-args>. The
1779 <template-template-param> part is a substitution
1780 candidate. */
1781 if (! d_add_substitution (di, ret))
1782 return NULL;
1783 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1784 d_template_args (di));
1786 break;
1788 case 'S':
1789 /* If this is a special substitution, then it is the start of
1790 <class-enum-type>. */
1792 char peek_next;
1794 peek_next = d_peek_next_char (di);
1795 if (IS_DIGIT (peek_next)
1796 || peek_next == '_'
1797 || IS_UPPER (peek_next))
1799 ret = d_substitution (di, 0);
1800 /* The substituted name may have been a template name and
1801 may be followed by tepmlate args. */
1802 if (d_peek_char (di) == 'I')
1803 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1804 d_template_args (di));
1805 else
1806 can_subst = 0;
1808 else
1810 ret = d_class_enum_type (di);
1811 /* If the substitution was a complete type, then it is not
1812 a new substitution candidate. However, if the
1813 substitution was followed by template arguments, then
1814 the whole thing is a substitution candidate. */
1815 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1816 can_subst = 0;
1819 break;
1821 case 'P':
1822 d_advance (di, 1);
1823 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1824 cplus_demangle_type (di), NULL);
1825 break;
1827 case 'R':
1828 d_advance (di, 1);
1829 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1830 cplus_demangle_type (di), NULL);
1831 break;
1833 case 'C':
1834 d_advance (di, 1);
1835 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1836 cplus_demangle_type (di), NULL);
1837 break;
1839 case 'G':
1840 d_advance (di, 1);
1841 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1842 cplus_demangle_type (di), NULL);
1843 break;
1845 case 'U':
1846 d_advance (di, 1);
1847 ret = d_source_name (di);
1848 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1849 cplus_demangle_type (di), ret);
1850 break;
1852 default:
1853 return NULL;
1856 if (can_subst)
1858 if (! d_add_substitution (di, ret))
1859 return NULL;
1862 return ret;
1865 /* <CV-qualifiers> ::= [r] [V] [K] */
1867 static struct demangle_component **
1868 d_cv_qualifiers (struct d_info *di,
1869 struct demangle_component **pret, int member_fn)
1871 char peek;
1873 peek = d_peek_char (di);
1874 while (peek == 'r' || peek == 'V' || peek == 'K')
1876 enum demangle_component_type t;
1878 d_advance (di, 1);
1879 if (peek == 'r')
1881 t = (member_fn
1882 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1883 : DEMANGLE_COMPONENT_RESTRICT);
1884 di->expansion += sizeof "restrict";
1886 else if (peek == 'V')
1888 t = (member_fn
1889 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1890 : DEMANGLE_COMPONENT_VOLATILE);
1891 di->expansion += sizeof "volatile";
1893 else
1895 t = (member_fn
1896 ? DEMANGLE_COMPONENT_CONST_THIS
1897 : DEMANGLE_COMPONENT_CONST);
1898 di->expansion += sizeof "const";
1901 *pret = d_make_comp (di, t, NULL, NULL);
1902 if (*pret == NULL)
1903 return NULL;
1904 pret = &d_left (*pret);
1906 peek = d_peek_char (di);
1909 return pret;
1912 /* <function-type> ::= F [Y] <bare-function-type> E */
1914 static struct demangle_component *
1915 d_function_type (struct d_info *di)
1917 struct demangle_component *ret;
1919 if (d_next_char (di) != 'F')
1920 return NULL;
1921 if (d_peek_char (di) == 'Y')
1923 /* Function has C linkage. We don't print this information.
1924 FIXME: We should print it in verbose mode. */
1925 d_advance (di, 1);
1927 ret = d_bare_function_type (di, 1);
1928 if (d_next_char (di) != 'E')
1929 return NULL;
1930 return ret;
1933 /* <bare-function-type> ::= <type>+ */
1935 static struct demangle_component *
1936 d_bare_function_type (struct d_info *di, int has_return_type)
1938 struct demangle_component *return_type;
1939 struct demangle_component *tl;
1940 struct demangle_component **ptl;
1942 return_type = NULL;
1943 tl = NULL;
1944 ptl = &tl;
1945 while (1)
1947 char peek;
1948 struct demangle_component *type;
1950 peek = d_peek_char (di);
1951 if (peek == '\0' || peek == 'E')
1952 break;
1953 type = cplus_demangle_type (di);
1954 if (type == NULL)
1955 return NULL;
1956 if (has_return_type)
1958 return_type = type;
1959 has_return_type = 0;
1961 else
1963 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
1964 if (*ptl == NULL)
1965 return NULL;
1966 ptl = &d_right (*ptl);
1970 /* There should be at least one parameter type besides the optional
1971 return type. A function which takes no arguments will have a
1972 single parameter type void. */
1973 if (tl == NULL)
1974 return NULL;
1976 /* If we have a single parameter type void, omit it. */
1977 if (d_right (tl) == NULL
1978 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
1979 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
1981 di->expansion -= d_left (tl)->u.s_builtin.type->len;
1982 tl = NULL;
1985 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
1988 /* <class-enum-type> ::= <name> */
1990 static struct demangle_component *
1991 d_class_enum_type (struct d_info *di)
1993 return d_name (di);
1996 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1997 ::= A [<(dimension) expression>] _ <(element) type>
2000 static struct demangle_component *
2001 d_array_type (struct d_info *di)
2003 char peek;
2004 struct demangle_component *dim;
2006 if (d_next_char (di) != 'A')
2007 return NULL;
2009 peek = d_peek_char (di);
2010 if (peek == '_')
2011 dim = NULL;
2012 else if (IS_DIGIT (peek))
2014 const char *s;
2016 s = d_str (di);
2019 d_advance (di, 1);
2020 peek = d_peek_char (di);
2022 while (IS_DIGIT (peek));
2023 dim = d_make_name (di, s, d_str (di) - s);
2024 if (dim == NULL)
2025 return NULL;
2027 else
2029 dim = d_expression (di);
2030 if (dim == NULL)
2031 return NULL;
2034 if (d_next_char (di) != '_')
2035 return NULL;
2037 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2038 cplus_demangle_type (di));
2041 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2043 static struct demangle_component *
2044 d_pointer_to_member_type (struct d_info *di)
2046 struct demangle_component *cl;
2047 struct demangle_component *mem;
2048 struct demangle_component **pmem;
2050 if (d_next_char (di) != 'M')
2051 return NULL;
2053 cl = cplus_demangle_type (di);
2055 /* The ABI specifies that any type can be a substitution source, and
2056 that M is followed by two types, and that when a CV-qualified
2057 type is seen both the base type and the CV-qualified types are
2058 substitution sources. The ABI also specifies that for a pointer
2059 to a CV-qualified member function, the qualifiers are attached to
2060 the second type. Given the grammar, a plain reading of the ABI
2061 suggests that both the CV-qualified member function and the
2062 non-qualified member function are substitution sources. However,
2063 g++ does not work that way. g++ treats only the CV-qualified
2064 member function as a substitution source. FIXME. So to work
2065 with g++, we need to pull off the CV-qualifiers here, in order to
2066 avoid calling add_substitution() in cplus_demangle_type(). */
2068 pmem = d_cv_qualifiers (di, &mem, 1);
2069 if (pmem == NULL)
2070 return NULL;
2071 *pmem = cplus_demangle_type (di);
2073 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2076 /* <template-param> ::= T_
2077 ::= T <(parameter-2 non-negative) number> _
2080 static struct demangle_component *
2081 d_template_param (struct d_info *di)
2083 long param;
2085 if (d_next_char (di) != 'T')
2086 return NULL;
2088 if (d_peek_char (di) == '_')
2089 param = 0;
2090 else
2092 param = d_number (di);
2093 if (param < 0)
2094 return NULL;
2095 param += 1;
2098 if (d_next_char (di) != '_')
2099 return NULL;
2101 ++di->did_subs;
2103 return d_make_template_param (di, param);
2106 /* <template-args> ::= I <template-arg>+ E */
2108 static struct demangle_component *
2109 d_template_args (struct d_info *di)
2111 struct demangle_component *hold_last_name;
2112 struct demangle_component *al;
2113 struct demangle_component **pal;
2115 /* Preserve the last name we saw--don't let the template arguments
2116 clobber it, as that would give us the wrong name for a subsequent
2117 constructor or destructor. */
2118 hold_last_name = di->last_name;
2120 if (d_next_char (di) != 'I')
2121 return NULL;
2123 al = NULL;
2124 pal = &al;
2125 while (1)
2127 struct demangle_component *a;
2129 a = d_template_arg (di);
2130 if (a == NULL)
2131 return NULL;
2133 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2134 if (*pal == NULL)
2135 return NULL;
2136 pal = &d_right (*pal);
2138 if (d_peek_char (di) == 'E')
2140 d_advance (di, 1);
2141 break;
2145 di->last_name = hold_last_name;
2147 return al;
2150 /* <template-arg> ::= <type>
2151 ::= X <expression> E
2152 ::= <expr-primary>
2155 static struct demangle_component *
2156 d_template_arg (struct d_info *di)
2158 struct demangle_component *ret;
2160 switch (d_peek_char (di))
2162 case 'X':
2163 d_advance (di, 1);
2164 ret = d_expression (di);
2165 if (d_next_char (di) != 'E')
2166 return NULL;
2167 return ret;
2169 case 'L':
2170 return d_expr_primary (di);
2172 default:
2173 return cplus_demangle_type (di);
2177 /* <expression> ::= <(unary) operator-name> <expression>
2178 ::= <(binary) operator-name> <expression> <expression>
2179 ::= <(trinary) operator-name> <expression> <expression> <expression>
2180 ::= st <type>
2181 ::= <template-param>
2182 ::= sr <type> <unqualified-name>
2183 ::= sr <type> <unqualified-name> <template-args>
2184 ::= <expr-primary>
2187 static struct demangle_component *
2188 d_expression (struct d_info *di)
2190 char peek;
2192 peek = d_peek_char (di);
2193 if (peek == 'L')
2194 return d_expr_primary (di);
2195 else if (peek == 'T')
2196 return d_template_param (di);
2197 else if (peek == 's' && d_peek_next_char (di) == 'r')
2199 struct demangle_component *type;
2200 struct demangle_component *name;
2202 d_advance (di, 2);
2203 type = cplus_demangle_type (di);
2204 name = d_unqualified_name (di);
2205 if (d_peek_char (di) != 'I')
2206 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2207 else
2208 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2209 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2210 d_template_args (di)));
2212 else
2214 struct demangle_component *op;
2215 int args;
2217 op = d_operator_name (di);
2218 if (op == NULL)
2219 return NULL;
2221 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2222 di->expansion += op->u.s_operator.op->len - 2;
2224 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2225 && strcmp (op->u.s_operator.op->code, "st") == 0)
2226 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2227 cplus_demangle_type (di));
2229 switch (op->type)
2231 default:
2232 return NULL;
2233 case DEMANGLE_COMPONENT_OPERATOR:
2234 args = op->u.s_operator.op->args;
2235 break;
2236 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2237 args = op->u.s_extended_operator.args;
2238 break;
2239 case DEMANGLE_COMPONENT_CAST:
2240 args = 1;
2241 break;
2244 switch (args)
2246 case 1:
2247 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2248 d_expression (di));
2249 case 2:
2251 struct demangle_component *left;
2253 left = d_expression (di);
2254 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2255 d_make_comp (di,
2256 DEMANGLE_COMPONENT_BINARY_ARGS,
2257 left,
2258 d_expression (di)));
2260 case 3:
2262 struct demangle_component *first;
2263 struct demangle_component *second;
2265 first = d_expression (di);
2266 second = d_expression (di);
2267 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2268 d_make_comp (di,
2269 DEMANGLE_COMPONENT_TRINARY_ARG1,
2270 first,
2271 d_make_comp (di,
2272 DEMANGLE_COMPONENT_TRINARY_ARG2,
2273 second,
2274 d_expression (di))));
2276 default:
2277 return NULL;
2282 /* <expr-primary> ::= L <type> <(value) number> E
2283 ::= L <type> <(value) float> E
2284 ::= L <mangled-name> E
2287 static struct demangle_component *
2288 d_expr_primary (struct d_info *di)
2290 struct demangle_component *ret;
2292 if (d_next_char (di) != 'L')
2293 return NULL;
2294 if (d_peek_char (di) == '_')
2295 ret = cplus_demangle_mangled_name (di, 0);
2296 else
2298 struct demangle_component *type;
2299 enum demangle_component_type t;
2300 const char *s;
2302 type = cplus_demangle_type (di);
2303 if (type == NULL)
2304 return NULL;
2306 /* If we have a type we know how to print, we aren't going to
2307 print the type name itself. */
2308 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2309 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2310 di->expansion -= type->u.s_builtin.type->len;
2312 /* Rather than try to interpret the literal value, we just
2313 collect it as a string. Note that it's possible to have a
2314 floating point literal here. The ABI specifies that the
2315 format of such literals is machine independent. That's fine,
2316 but what's not fine is that versions of g++ up to 3.2 with
2317 -fabi-version=1 used upper case letters in the hex constant,
2318 and dumped out gcc's internal representation. That makes it
2319 hard to tell where the constant ends, and hard to dump the
2320 constant in any readable form anyhow. We don't attempt to
2321 handle these cases. */
2323 t = DEMANGLE_COMPONENT_LITERAL;
2324 if (d_peek_char (di) == 'n')
2326 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2327 d_advance (di, 1);
2329 s = d_str (di);
2330 while (d_peek_char (di) != 'E')
2331 d_advance (di, 1);
2332 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2334 if (d_next_char (di) != 'E')
2335 return NULL;
2336 return ret;
2339 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2340 ::= Z <(function) encoding> E s [<discriminator>]
2343 static struct demangle_component *
2344 d_local_name (struct d_info *di)
2346 struct demangle_component *function;
2348 if (d_next_char (di) != 'Z')
2349 return NULL;
2351 function = d_encoding (di, 0);
2353 if (d_next_char (di) != 'E')
2354 return NULL;
2356 if (d_peek_char (di) == 's')
2358 d_advance (di, 1);
2359 if (! d_discriminator (di))
2360 return NULL;
2361 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2362 d_make_name (di, "string literal",
2363 sizeof "string literal" - 1));
2365 else
2367 struct demangle_component *name;
2369 name = d_name (di);
2370 if (! d_discriminator (di))
2371 return NULL;
2372 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2376 /* <discriminator> ::= _ <(non-negative) number>
2378 We demangle the discriminator, but we don't print it out. FIXME:
2379 We should print it out in verbose mode. */
2381 static int
2382 d_discriminator (struct d_info *di)
2384 long discrim;
2386 if (d_peek_char (di) != '_')
2387 return 1;
2388 d_advance (di, 1);
2389 discrim = d_number (di);
2390 if (discrim < 0)
2391 return 0;
2392 return 1;
2395 /* Add a new substitution. */
2397 static int
2398 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2400 if (dc == NULL)
2401 return 0;
2402 if (di->next_sub >= di->num_subs)
2403 return 0;
2404 di->subs[di->next_sub] = dc;
2405 ++di->next_sub;
2406 return 1;
2409 /* <substitution> ::= S <seq-id> _
2410 ::= S_
2411 ::= St
2412 ::= Sa
2413 ::= Sb
2414 ::= Ss
2415 ::= Si
2416 ::= So
2417 ::= Sd
2419 If PREFIX is non-zero, then this type is being used as a prefix in
2420 a qualified name. In this case, for the standard substitutions, we
2421 need to check whether we are being used as a prefix for a
2422 constructor or destructor, and return a full template name.
2423 Otherwise we will get something like std::iostream::~iostream()
2424 which does not correspond particularly well to any function which
2425 actually appears in the source.
2428 static const struct d_standard_sub_info standard_subs[] =
2430 { 't', NL ("std"),
2431 NL ("std"),
2432 NULL, 0 },
2433 { 'a', NL ("std::allocator"),
2434 NL ("std::allocator"),
2435 NL ("allocator") },
2436 { 'b', NL ("std::basic_string"),
2437 NL ("std::basic_string"),
2438 NL ("basic_string") },
2439 { 's', NL ("std::string"),
2440 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2441 NL ("basic_string") },
2442 { 'i', NL ("std::istream"),
2443 NL ("std::basic_istream<char, std::char_traits<char> >"),
2444 NL ("basic_istream") },
2445 { 'o', NL ("std::ostream"),
2446 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2447 NL ("basic_ostream") },
2448 { 'd', NL ("std::iostream"),
2449 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2450 NL ("basic_iostream") }
2453 static struct demangle_component *
2454 d_substitution (struct d_info *di, int prefix)
2456 char c;
2458 if (d_next_char (di) != 'S')
2459 return NULL;
2461 c = d_next_char (di);
2462 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2464 int id;
2466 id = 0;
2467 if (c != '_')
2471 if (IS_DIGIT (c))
2472 id = id * 36 + c - '0';
2473 else if (IS_UPPER (c))
2474 id = id * 36 + c - 'A' + 10;
2475 else
2476 return NULL;
2477 c = d_next_char (di);
2479 while (c != '_');
2481 ++id;
2484 if (id >= di->next_sub)
2485 return NULL;
2487 ++di->did_subs;
2489 return di->subs[id];
2491 else
2493 int verbose;
2494 const struct d_standard_sub_info *p;
2495 const struct d_standard_sub_info *pend;
2497 verbose = (di->options & DMGL_VERBOSE) != 0;
2498 if (! verbose && prefix)
2500 char peek;
2502 peek = d_peek_char (di);
2503 if (peek == 'C' || peek == 'D')
2504 verbose = 1;
2507 pend = (&standard_subs[0]
2508 + sizeof standard_subs / sizeof standard_subs[0]);
2509 for (p = &standard_subs[0]; p < pend; ++p)
2511 if (c == p->code)
2513 const char *s;
2514 int len;
2516 if (p->set_last_name != NULL)
2517 di->last_name = d_make_sub (di, p->set_last_name,
2518 p->set_last_name_len);
2519 if (verbose)
2521 s = p->full_expansion;
2522 len = p->full_len;
2524 else
2526 s = p->simple_expansion;
2527 len = p->simple_len;
2529 di->expansion += len;
2530 return d_make_sub (di, s, len);
2534 return NULL;
2538 /* Resize the print buffer. */
2540 static void
2541 d_print_resize (struct d_print_info *dpi, size_t add)
2543 size_t need;
2545 if (dpi->buf == NULL)
2546 return;
2547 need = dpi->len + add;
2548 while (need > dpi->alc)
2550 size_t newalc;
2551 char *newbuf;
2553 newalc = dpi->alc * 2;
2554 newbuf = realloc (dpi->buf, newalc);
2555 if (newbuf == NULL)
2557 free (dpi->buf);
2558 dpi->buf = NULL;
2559 dpi->allocation_failure = 1;
2560 return;
2562 dpi->buf = newbuf;
2563 dpi->alc = newalc;
2567 /* Append a character to the print buffer. */
2569 static void
2570 d_print_append_char (struct d_print_info *dpi, int c)
2572 if (dpi->buf != NULL)
2574 if (dpi->len >= dpi->alc)
2576 d_print_resize (dpi, 1);
2577 if (dpi->buf == NULL)
2578 return;
2581 dpi->buf[dpi->len] = c;
2582 ++dpi->len;
2586 /* Append a buffer to the print buffer. */
2588 static void
2589 d_print_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
2591 if (dpi->buf != NULL)
2593 if (dpi->len + l > dpi->alc)
2595 d_print_resize (dpi, l);
2596 if (dpi->buf == NULL)
2597 return;
2600 memcpy (dpi->buf + dpi->len, s, l);
2601 dpi->len += l;
2605 /* Indicate that an error occurred during printing. */
2607 static void
2608 d_print_error (struct d_print_info *dpi)
2610 free (dpi->buf);
2611 dpi->buf = NULL;
2614 /* Turn components into a human readable string. OPTIONS is the
2615 options bits passed to the demangler. DC is the tree to print.
2616 ESTIMATE is a guess at the length of the result. This returns a
2617 string allocated by malloc, or NULL on error. On success, this
2618 sets *PALC to the size of the allocated buffer. On failure, this
2619 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2620 failure. */
2622 CP_STATIC_IF_GLIBCPP_V3
2623 char *
2624 cplus_demangle_print (int options, const struct demangle_component *dc,
2625 int estimate, size_t *palc)
2627 struct d_print_info dpi;
2629 dpi.options = options;
2631 dpi.alc = estimate + 1;
2632 dpi.buf = malloc (dpi.alc);
2633 if (dpi.buf == NULL)
2635 *palc = 1;
2636 return NULL;
2639 dpi.len = 0;
2640 dpi.templates = NULL;
2641 dpi.modifiers = NULL;
2643 dpi.allocation_failure = 0;
2645 d_print_comp (&dpi, dc);
2647 d_append_char (&dpi, '\0');
2649 if (dpi.buf != NULL)
2650 *palc = dpi.alc;
2651 else
2652 *palc = dpi.allocation_failure;
2654 return dpi.buf;
2657 /* Subroutine to handle components. */
2659 static void
2660 d_print_comp (struct d_print_info *dpi,
2661 const struct demangle_component *dc)
2663 if (dc == NULL)
2665 d_print_error (dpi);
2666 return;
2668 if (d_print_saw_error (dpi))
2669 return;
2671 switch (dc->type)
2673 case DEMANGLE_COMPONENT_NAME:
2674 if ((dpi->options & DMGL_JAVA) == 0)
2675 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2676 else
2677 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2678 return;
2680 case DEMANGLE_COMPONENT_QUAL_NAME:
2681 case DEMANGLE_COMPONENT_LOCAL_NAME:
2682 d_print_comp (dpi, d_left (dc));
2683 if ((dpi->options & DMGL_JAVA) == 0)
2684 d_append_string_constant (dpi, "::");
2685 else
2686 d_append_char (dpi, '.');
2687 d_print_comp (dpi, d_right (dc));
2688 return;
2690 case DEMANGLE_COMPONENT_TYPED_NAME:
2692 struct d_print_mod *hold_modifiers;
2693 struct demangle_component *typed_name;
2694 struct d_print_mod adpm[4];
2695 unsigned int i;
2696 struct d_print_template dpt;
2698 /* Pass the name down to the type so that it can be printed in
2699 the right place for the type. We also have to pass down
2700 any CV-qualifiers, which apply to the this parameter. */
2701 hold_modifiers = dpi->modifiers;
2702 i = 0;
2703 typed_name = d_left (dc);
2704 while (typed_name != NULL)
2706 if (i >= sizeof adpm / sizeof adpm[0])
2708 d_print_error (dpi);
2709 return;
2712 adpm[i].next = dpi->modifiers;
2713 dpi->modifiers = &adpm[i];
2714 adpm[i].mod = typed_name;
2715 adpm[i].printed = 0;
2716 adpm[i].templates = dpi->templates;
2717 ++i;
2719 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2720 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2721 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2722 break;
2724 typed_name = d_left (typed_name);
2727 /* If typed_name is a template, then it applies to the
2728 function type as well. */
2729 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2731 dpt.next = dpi->templates;
2732 dpi->templates = &dpt;
2733 dpt.template = typed_name;
2736 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2737 there may be CV-qualifiers on its right argument which
2738 really apply here; this happens when parsing a class which
2739 is local to a function. */
2740 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2742 struct demangle_component *local_name;
2744 local_name = d_right (typed_name);
2745 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2746 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2747 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2749 if (i >= sizeof adpm / sizeof adpm[0])
2751 d_print_error (dpi);
2752 return;
2755 adpm[i] = adpm[i - 1];
2756 adpm[i].next = &adpm[i - 1];
2757 dpi->modifiers = &adpm[i];
2759 adpm[i - 1].mod = local_name;
2760 adpm[i - 1].printed = 0;
2761 adpm[i - 1].templates = dpi->templates;
2762 ++i;
2764 local_name = d_left (local_name);
2768 d_print_comp (dpi, d_right (dc));
2770 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2771 dpi->templates = dpt.next;
2773 /* If the modifiers didn't get printed by the type, print them
2774 now. */
2775 while (i > 0)
2777 --i;
2778 if (! adpm[i].printed)
2780 d_append_char (dpi, ' ');
2781 d_print_mod (dpi, adpm[i].mod);
2785 dpi->modifiers = hold_modifiers;
2787 return;
2790 case DEMANGLE_COMPONENT_TEMPLATE:
2792 struct d_print_mod *hold_dpm;
2794 /* Don't push modifiers into a template definition. Doing so
2795 could give the wrong definition for a template argument.
2796 Instead, treat the template essentially as a name. */
2798 hold_dpm = dpi->modifiers;
2799 dpi->modifiers = NULL;
2801 d_print_comp (dpi, d_left (dc));
2802 if (d_last_char (dpi) == '<')
2803 d_append_char (dpi, ' ');
2804 d_append_char (dpi, '<');
2805 d_print_comp (dpi, d_right (dc));
2806 /* Avoid generating two consecutive '>' characters, to avoid
2807 the C++ syntactic ambiguity. */
2808 if (d_last_char (dpi) == '>')
2809 d_append_char (dpi, ' ');
2810 d_append_char (dpi, '>');
2812 dpi->modifiers = hold_dpm;
2814 return;
2817 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
2819 long i;
2820 struct demangle_component *a;
2821 struct d_print_template *hold_dpt;
2823 if (dpi->templates == NULL)
2825 d_print_error (dpi);
2826 return;
2828 i = dc->u.s_number.number;
2829 for (a = d_right (dpi->templates->template);
2830 a != NULL;
2831 a = d_right (a))
2833 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
2835 d_print_error (dpi);
2836 return;
2838 if (i <= 0)
2839 break;
2840 --i;
2842 if (i != 0 || a == NULL)
2844 d_print_error (dpi);
2845 return;
2848 /* While processing this parameter, we need to pop the list of
2849 templates. This is because the template parameter may
2850 itself be a reference to a parameter of an outer
2851 template. */
2853 hold_dpt = dpi->templates;
2854 dpi->templates = hold_dpt->next;
2856 d_print_comp (dpi, d_left (a));
2858 dpi->templates = hold_dpt;
2860 return;
2863 case DEMANGLE_COMPONENT_CTOR:
2864 d_print_comp (dpi, dc->u.s_ctor.name);
2865 return;
2867 case DEMANGLE_COMPONENT_DTOR:
2868 d_append_char (dpi, '~');
2869 d_print_comp (dpi, dc->u.s_dtor.name);
2870 return;
2872 case DEMANGLE_COMPONENT_VTABLE:
2873 d_append_string_constant (dpi, "vtable for ");
2874 d_print_comp (dpi, d_left (dc));
2875 return;
2877 case DEMANGLE_COMPONENT_VTT:
2878 d_append_string_constant (dpi, "VTT for ");
2879 d_print_comp (dpi, d_left (dc));
2880 return;
2882 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
2883 d_append_string_constant (dpi, "construction vtable for ");
2884 d_print_comp (dpi, d_left (dc));
2885 d_append_string_constant (dpi, "-in-");
2886 d_print_comp (dpi, d_right (dc));
2887 return;
2889 case DEMANGLE_COMPONENT_TYPEINFO:
2890 d_append_string_constant (dpi, "typeinfo for ");
2891 d_print_comp (dpi, d_left (dc));
2892 return;
2894 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
2895 d_append_string_constant (dpi, "typeinfo name for ");
2896 d_print_comp (dpi, d_left (dc));
2897 return;
2899 case DEMANGLE_COMPONENT_TYPEINFO_FN:
2900 d_append_string_constant (dpi, "typeinfo fn for ");
2901 d_print_comp (dpi, d_left (dc));
2902 return;
2904 case DEMANGLE_COMPONENT_THUNK:
2905 d_append_string_constant (dpi, "non-virtual thunk to ");
2906 d_print_comp (dpi, d_left (dc));
2907 return;
2909 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
2910 d_append_string_constant (dpi, "virtual thunk to ");
2911 d_print_comp (dpi, d_left (dc));
2912 return;
2914 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
2915 d_append_string_constant (dpi, "covariant return thunk to ");
2916 d_print_comp (dpi, d_left (dc));
2917 return;
2919 case DEMANGLE_COMPONENT_JAVA_CLASS:
2920 d_append_string_constant (dpi, "java Class for ");
2921 d_print_comp (dpi, d_left (dc));
2922 return;
2924 case DEMANGLE_COMPONENT_GUARD:
2925 d_append_string_constant (dpi, "guard variable for ");
2926 d_print_comp (dpi, d_left (dc));
2927 return;
2929 case DEMANGLE_COMPONENT_REFTEMP:
2930 d_append_string_constant (dpi, "reference temporary for ");
2931 d_print_comp (dpi, d_left (dc));
2932 return;
2934 case DEMANGLE_COMPONENT_SUB_STD:
2935 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
2936 return;
2938 case DEMANGLE_COMPONENT_RESTRICT:
2939 case DEMANGLE_COMPONENT_VOLATILE:
2940 case DEMANGLE_COMPONENT_CONST:
2942 struct d_print_mod *pdpm;
2944 /* When printing arrays, it's possible to have cases where the
2945 same CV-qualifier gets pushed on the stack multiple times.
2946 We only need to print it once. */
2948 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
2950 if (! pdpm->printed)
2952 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
2953 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
2954 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
2955 break;
2956 if (pdpm->mod->type == dc->type)
2958 d_print_comp (dpi, d_left (dc));
2959 return;
2964 /* Fall through. */
2965 case DEMANGLE_COMPONENT_RESTRICT_THIS:
2966 case DEMANGLE_COMPONENT_VOLATILE_THIS:
2967 case DEMANGLE_COMPONENT_CONST_THIS:
2968 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
2969 case DEMANGLE_COMPONENT_POINTER:
2970 case DEMANGLE_COMPONENT_REFERENCE:
2971 case DEMANGLE_COMPONENT_COMPLEX:
2972 case DEMANGLE_COMPONENT_IMAGINARY:
2974 /* We keep a list of modifiers on the stack. */
2975 struct d_print_mod dpm;
2977 dpm.next = dpi->modifiers;
2978 dpi->modifiers = &dpm;
2979 dpm.mod = dc;
2980 dpm.printed = 0;
2981 dpm.templates = dpi->templates;
2983 d_print_comp (dpi, d_left (dc));
2985 /* If the modifier didn't get printed by the type, print it
2986 now. */
2987 if (! dpm.printed)
2988 d_print_mod (dpi, dc);
2990 dpi->modifiers = dpm.next;
2992 return;
2995 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
2996 if ((dpi->options & DMGL_JAVA) == 0)
2997 d_append_buffer (dpi, dc->u.s_builtin.type->name,
2998 dc->u.s_builtin.type->len);
2999 else
3000 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3001 dc->u.s_builtin.type->java_len);
3002 return;
3004 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3005 d_print_comp (dpi, d_left (dc));
3006 return;
3008 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3010 if (d_left (dc) != NULL)
3012 struct d_print_mod dpm;
3014 /* We must pass this type down as a modifier in order to
3015 print it in the right location. */
3017 dpm.next = dpi->modifiers;
3018 dpi->modifiers = &dpm;
3019 dpm.mod = dc;
3020 dpm.printed = 0;
3021 dpm.templates = dpi->templates;
3023 d_print_comp (dpi, d_left (dc));
3025 dpi->modifiers = dpm.next;
3027 if (dpm.printed)
3028 return;
3030 d_append_char (dpi, ' ');
3033 d_print_function_type (dpi, dc, dpi->modifiers);
3035 return;
3038 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3040 struct d_print_mod *hold_modifiers;
3041 struct d_print_mod adpm[4];
3042 unsigned int i;
3043 struct d_print_mod *pdpm;
3045 /* We must pass this type down as a modifier in order to print
3046 multi-dimensional arrays correctly. If the array itself is
3047 CV-qualified, we act as though the element type were
3048 CV-qualified. We do this by copying the modifiers down
3049 rather than fiddling pointers, so that we don't wind up
3050 with a d_print_mod higher on the stack pointing into our
3051 stack frame after we return. */
3053 hold_modifiers = dpi->modifiers;
3055 adpm[0].next = hold_modifiers;
3056 dpi->modifiers = &adpm[0];
3057 adpm[0].mod = dc;
3058 adpm[0].printed = 0;
3059 adpm[0].templates = dpi->templates;
3061 i = 1;
3062 pdpm = hold_modifiers;
3063 while (pdpm != NULL
3064 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3065 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3066 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3068 if (! pdpm->printed)
3070 if (i >= sizeof adpm / sizeof adpm[0])
3072 d_print_error (dpi);
3073 return;
3076 adpm[i] = *pdpm;
3077 adpm[i].next = dpi->modifiers;
3078 dpi->modifiers = &adpm[i];
3079 pdpm->printed = 1;
3080 ++i;
3083 pdpm = pdpm->next;
3086 d_print_comp (dpi, d_right (dc));
3088 dpi->modifiers = hold_modifiers;
3090 if (adpm[0].printed)
3091 return;
3093 while (i > 1)
3095 --i;
3096 d_print_mod (dpi, adpm[i].mod);
3099 d_print_array_type (dpi, dc, dpi->modifiers);
3101 return;
3104 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3106 struct d_print_mod dpm;
3108 dpm.next = dpi->modifiers;
3109 dpi->modifiers = &dpm;
3110 dpm.mod = dc;
3111 dpm.printed = 0;
3112 dpm.templates = dpi->templates;
3114 d_print_comp (dpi, d_right (dc));
3116 /* If the modifier didn't get printed by the type, print it
3117 now. */
3118 if (! dpm.printed)
3120 d_append_char (dpi, ' ');
3121 d_print_comp (dpi, d_left (dc));
3122 d_append_string_constant (dpi, "::*");
3125 dpi->modifiers = dpm.next;
3127 return;
3130 case DEMANGLE_COMPONENT_ARGLIST:
3131 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3132 d_print_comp (dpi, d_left (dc));
3133 if (d_right (dc) != NULL)
3135 d_append_string_constant (dpi, ", ");
3136 d_print_comp (dpi, d_right (dc));
3138 return;
3140 case DEMANGLE_COMPONENT_OPERATOR:
3142 char c;
3144 d_append_string_constant (dpi, "operator");
3145 c = dc->u.s_operator.op->name[0];
3146 if (IS_LOWER (c))
3147 d_append_char (dpi, ' ');
3148 d_append_buffer (dpi, dc->u.s_operator.op->name,
3149 dc->u.s_operator.op->len);
3150 return;
3153 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3154 d_append_string_constant (dpi, "operator ");
3155 d_print_comp (dpi, dc->u.s_extended_operator.name);
3156 return;
3158 case DEMANGLE_COMPONENT_CAST:
3159 d_append_string_constant (dpi, "operator ");
3160 d_print_cast (dpi, dc);
3161 return;
3163 case DEMANGLE_COMPONENT_UNARY:
3164 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3165 d_print_expr_op (dpi, d_left (dc));
3166 else
3168 d_append_char (dpi, '(');
3169 d_print_cast (dpi, d_left (dc));
3170 d_append_char (dpi, ')');
3172 d_append_char (dpi, '(');
3173 d_print_comp (dpi, d_right (dc));
3174 d_append_char (dpi, ')');
3175 return;
3177 case DEMANGLE_COMPONENT_BINARY:
3178 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3180 d_print_error (dpi);
3181 return;
3184 /* We wrap an expression which uses the greater-than operator in
3185 an extra layer of parens so that it does not get confused
3186 with the '>' which ends the template parameters. */
3187 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3188 && d_left (dc)->u.s_operator.op->len == 1
3189 && d_left (dc)->u.s_operator.op->name[0] == '>')
3190 d_append_char (dpi, '(');
3192 d_append_char (dpi, '(');
3193 d_print_comp (dpi, d_left (d_right (dc)));
3194 d_append_string_constant (dpi, ") ");
3195 d_print_expr_op (dpi, d_left (dc));
3196 d_append_string_constant (dpi, " (");
3197 d_print_comp (dpi, d_right (d_right (dc)));
3198 d_append_char (dpi, ')');
3200 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3201 && d_left (dc)->u.s_operator.op->len == 1
3202 && d_left (dc)->u.s_operator.op->name[0] == '>')
3203 d_append_char (dpi, ')');
3205 return;
3207 case DEMANGLE_COMPONENT_BINARY_ARGS:
3208 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3209 d_print_error (dpi);
3210 return;
3212 case DEMANGLE_COMPONENT_TRINARY:
3213 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3214 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3216 d_print_error (dpi);
3217 return;
3219 d_append_char (dpi, '(');
3220 d_print_comp (dpi, d_left (d_right (dc)));
3221 d_append_string_constant (dpi, ") ");
3222 d_print_expr_op (dpi, d_left (dc));
3223 d_append_string_constant (dpi, " (");
3224 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3225 d_append_string_constant (dpi, ") : (");
3226 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3227 d_append_char (dpi, ')');
3228 return;
3230 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3231 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3232 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3233 d_print_error (dpi);
3234 return;
3236 case DEMANGLE_COMPONENT_LITERAL:
3237 case DEMANGLE_COMPONENT_LITERAL_NEG:
3239 enum d_builtin_type_print tp;
3241 /* For some builtin types, produce simpler output. */
3242 tp = D_PRINT_DEFAULT;
3243 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3245 tp = d_left (dc)->u.s_builtin.type->print;
3246 switch (tp)
3248 case D_PRINT_INT:
3249 case D_PRINT_UNSIGNED:
3250 case D_PRINT_LONG:
3251 case D_PRINT_UNSIGNED_LONG:
3252 case D_PRINT_LONG_LONG:
3253 case D_PRINT_UNSIGNED_LONG_LONG:
3254 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3256 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3257 d_append_char (dpi, '-');
3258 d_print_comp (dpi, d_right (dc));
3259 switch (tp)
3261 default:
3262 break;
3263 case D_PRINT_UNSIGNED:
3264 d_append_char (dpi, 'u');
3265 break;
3266 case D_PRINT_LONG:
3267 d_append_char (dpi, 'l');
3268 break;
3269 case D_PRINT_UNSIGNED_LONG:
3270 d_append_string_constant (dpi, "ul");
3271 break;
3272 case D_PRINT_LONG_LONG:
3273 d_append_string_constant (dpi, "ll");
3274 break;
3275 case D_PRINT_UNSIGNED_LONG_LONG:
3276 d_append_string_constant (dpi, "ull");
3277 break;
3279 return;
3281 break;
3283 case D_PRINT_BOOL:
3284 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3285 && d_right (dc)->u.s_name.len == 1
3286 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3288 switch (d_right (dc)->u.s_name.s[0])
3290 case '0':
3291 d_append_string_constant (dpi, "false");
3292 return;
3293 case '1':
3294 d_append_string_constant (dpi, "true");
3295 return;
3296 default:
3297 break;
3300 break;
3302 default:
3303 break;
3307 d_append_char (dpi, '(');
3308 d_print_comp (dpi, d_left (dc));
3309 d_append_char (dpi, ')');
3310 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3311 d_append_char (dpi, '-');
3312 if (tp == D_PRINT_FLOAT)
3313 d_append_char (dpi, '[');
3314 d_print_comp (dpi, d_right (dc));
3315 if (tp == D_PRINT_FLOAT)
3316 d_append_char (dpi, ']');
3318 return;
3320 default:
3321 d_print_error (dpi);
3322 return;
3326 /* Print a Java dentifier. For Java we try to handle encoded extended
3327 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3328 so we don't it for C++. Characters are encoded as
3329 __U<hex-char>+_. */
3331 static void
3332 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3334 const char *p;
3335 const char *end;
3337 end = name + len;
3338 for (p = name; p < end; ++p)
3340 if (end - p > 3
3341 && p[0] == '_'
3342 && p[1] == '_'
3343 && p[2] == 'U')
3345 unsigned long c;
3346 const char *q;
3348 c = 0;
3349 for (q = p + 3; q < end; ++q)
3351 int dig;
3353 if (IS_DIGIT (*q))
3354 dig = *q - '0';
3355 else if (*q >= 'A' && *q <= 'F')
3356 dig = *q - 'A' + 10;
3357 else if (*q >= 'a' && *q <= 'f')
3358 dig = *q - 'a' + 10;
3359 else
3360 break;
3362 c = c * 16 + dig;
3364 /* If the Unicode character is larger than 256, we don't try
3365 to deal with it here. FIXME. */
3366 if (q < end && *q == '_' && c < 256)
3368 d_append_char (dpi, c);
3369 p = q;
3370 continue;
3374 d_append_char (dpi, *p);
3378 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3379 qualifiers on this after printing a function. */
3381 static void
3382 d_print_mod_list (struct d_print_info *dpi,
3383 struct d_print_mod *mods, int suffix)
3385 struct d_print_template *hold_dpt;
3387 if (mods == NULL || d_print_saw_error (dpi))
3388 return;
3390 if (mods->printed
3391 || (! suffix
3392 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3393 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3394 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3396 d_print_mod_list (dpi, mods->next, suffix);
3397 return;
3400 mods->printed = 1;
3402 hold_dpt = dpi->templates;
3403 dpi->templates = mods->templates;
3405 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3407 d_print_function_type (dpi, mods->mod, mods->next);
3408 dpi->templates = hold_dpt;
3409 return;
3411 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3413 d_print_array_type (dpi, mods->mod, mods->next);
3414 dpi->templates = hold_dpt;
3415 return;
3417 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3419 struct d_print_mod *hold_modifiers;
3420 struct demangle_component *dc;
3422 /* When this is on the modifier stack, we have pulled any
3423 qualifiers off the right argument already. Otherwise, we
3424 print it as usual, but don't let the left argument see any
3425 modifiers. */
3427 hold_modifiers = dpi->modifiers;
3428 dpi->modifiers = NULL;
3429 d_print_comp (dpi, d_left (mods->mod));
3430 dpi->modifiers = hold_modifiers;
3432 if ((dpi->options & DMGL_JAVA) == 0)
3433 d_append_string_constant (dpi, "::");
3434 else
3435 d_append_char (dpi, '.');
3437 dc = d_right (mods->mod);
3438 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3439 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3440 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3441 dc = d_left (dc);
3443 d_print_comp (dpi, dc);
3445 dpi->templates = hold_dpt;
3446 return;
3449 d_print_mod (dpi, mods->mod);
3451 dpi->templates = hold_dpt;
3453 d_print_mod_list (dpi, mods->next, suffix);
3456 /* Print a modifier. */
3458 static void
3459 d_print_mod (struct d_print_info *dpi,
3460 const struct demangle_component *mod)
3462 switch (mod->type)
3464 case DEMANGLE_COMPONENT_RESTRICT:
3465 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3466 d_append_string_constant (dpi, " restrict");
3467 return;
3468 case DEMANGLE_COMPONENT_VOLATILE:
3469 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3470 d_append_string_constant (dpi, " volatile");
3471 return;
3472 case DEMANGLE_COMPONENT_CONST:
3473 case DEMANGLE_COMPONENT_CONST_THIS:
3474 d_append_string_constant (dpi, " const");
3475 return;
3476 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3477 d_append_char (dpi, ' ');
3478 d_print_comp (dpi, d_right (mod));
3479 return;
3480 case DEMANGLE_COMPONENT_POINTER:
3481 /* There is no pointer symbol in Java. */
3482 if ((dpi->options & DMGL_JAVA) == 0)
3483 d_append_char (dpi, '*');
3484 return;
3485 case DEMANGLE_COMPONENT_REFERENCE:
3486 d_append_char (dpi, '&');
3487 return;
3488 case DEMANGLE_COMPONENT_COMPLEX:
3489 d_append_string_constant (dpi, "complex ");
3490 return;
3491 case DEMANGLE_COMPONENT_IMAGINARY:
3492 d_append_string_constant (dpi, "imaginary ");
3493 return;
3494 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3495 if (d_last_char (dpi) != '(')
3496 d_append_char (dpi, ' ');
3497 d_print_comp (dpi, d_left (mod));
3498 d_append_string_constant (dpi, "::*");
3499 return;
3500 case DEMANGLE_COMPONENT_TYPED_NAME:
3501 d_print_comp (dpi, d_left (mod));
3502 return;
3503 default:
3504 /* Otherwise, we have something that won't go back on the
3505 modifier stack, so we can just print it. */
3506 d_print_comp (dpi, mod);
3507 return;
3511 /* Print a function type, except for the return type. */
3513 static void
3514 d_print_function_type (struct d_print_info *dpi,
3515 const struct demangle_component *dc,
3516 struct d_print_mod *mods)
3518 int need_paren;
3519 int saw_mod;
3520 int need_space;
3521 struct d_print_mod *p;
3522 struct d_print_mod *hold_modifiers;
3524 need_paren = 0;
3525 saw_mod = 0;
3526 need_space = 0;
3527 for (p = mods; p != NULL; p = p->next)
3529 if (p->printed)
3530 break;
3532 saw_mod = 1;
3533 switch (p->mod->type)
3535 case DEMANGLE_COMPONENT_POINTER:
3536 case DEMANGLE_COMPONENT_REFERENCE:
3537 need_paren = 1;
3538 break;
3539 case DEMANGLE_COMPONENT_RESTRICT:
3540 case DEMANGLE_COMPONENT_VOLATILE:
3541 case DEMANGLE_COMPONENT_CONST:
3542 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3543 case DEMANGLE_COMPONENT_COMPLEX:
3544 case DEMANGLE_COMPONENT_IMAGINARY:
3545 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3546 need_space = 1;
3547 need_paren = 1;
3548 break;
3549 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3550 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3551 case DEMANGLE_COMPONENT_CONST_THIS:
3552 break;
3553 default:
3554 break;
3556 if (need_paren)
3557 break;
3560 if (d_left (dc) != NULL && ! saw_mod)
3561 need_paren = 1;
3563 if (need_paren)
3565 if (! need_space)
3567 if (d_last_char (dpi) != '('
3568 && d_last_char (dpi) != '*')
3569 need_space = 1;
3571 if (need_space && d_last_char (dpi) != ' ')
3572 d_append_char (dpi, ' ');
3573 d_append_char (dpi, '(');
3576 hold_modifiers = dpi->modifiers;
3577 dpi->modifiers = NULL;
3579 d_print_mod_list (dpi, mods, 0);
3581 if (need_paren)
3582 d_append_char (dpi, ')');
3584 d_append_char (dpi, '(');
3586 if (d_right (dc) != NULL)
3587 d_print_comp (dpi, d_right (dc));
3589 d_append_char (dpi, ')');
3591 d_print_mod_list (dpi, mods, 1);
3593 dpi->modifiers = hold_modifiers;
3596 /* Print an array type, except for the element type. */
3598 static void
3599 d_print_array_type (struct d_print_info *dpi,
3600 const struct demangle_component *dc,
3601 struct d_print_mod *mods)
3603 int need_space;
3605 need_space = 1;
3606 if (mods != NULL)
3608 int need_paren;
3609 struct d_print_mod *p;
3611 need_paren = 0;
3612 for (p = mods; p != NULL; p = p->next)
3614 if (! p->printed)
3616 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3618 need_space = 0;
3619 break;
3621 else
3623 need_paren = 1;
3624 need_space = 1;
3625 break;
3630 if (need_paren)
3631 d_append_string_constant (dpi, " (");
3633 d_print_mod_list (dpi, mods, 0);
3635 if (need_paren)
3636 d_append_char (dpi, ')');
3639 if (need_space)
3640 d_append_char (dpi, ' ');
3642 d_append_char (dpi, '[');
3644 if (d_left (dc) != NULL)
3645 d_print_comp (dpi, d_left (dc));
3647 d_append_char (dpi, ']');
3650 /* Print an operator in an expression. */
3652 static void
3653 d_print_expr_op (struct d_print_info *dpi,
3654 const struct demangle_component *dc)
3656 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3657 d_append_buffer (dpi, dc->u.s_operator.op->name,
3658 dc->u.s_operator.op->len);
3659 else
3660 d_print_comp (dpi, dc);
3663 /* Print a cast. */
3665 static void
3666 d_print_cast (struct d_print_info *dpi,
3667 const struct demangle_component *dc)
3669 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3670 d_print_comp (dpi, d_left (dc));
3671 else
3673 struct d_print_mod *hold_dpm;
3674 struct d_print_template dpt;
3676 /* It appears that for a templated cast operator, we need to put
3677 the template parameters in scope for the operator name, but
3678 not for the parameters. The effect is that we need to handle
3679 the template printing here. */
3681 hold_dpm = dpi->modifiers;
3682 dpi->modifiers = NULL;
3684 dpt.next = dpi->templates;
3685 dpi->templates = &dpt;
3686 dpt.template = d_left (dc);
3688 d_print_comp (dpi, d_left (d_left (dc)));
3690 dpi->templates = dpt.next;
3692 if (d_last_char (dpi) == '<')
3693 d_append_char (dpi, ' ');
3694 d_append_char (dpi, '<');
3695 d_print_comp (dpi, d_right (d_left (dc)));
3696 /* Avoid generating two consecutive '>' characters, to avoid
3697 the C++ syntactic ambiguity. */
3698 if (d_last_char (dpi) == '>')
3699 d_append_char (dpi, ' ');
3700 d_append_char (dpi, '>');
3702 dpi->modifiers = hold_dpm;
3706 /* Initialize the information structure we use to pass around
3707 information. */
3709 CP_STATIC_IF_GLIBCPP_V3
3710 void
3711 cplus_demangle_init_info (const char *mangled, int options, size_t len,
3712 struct d_info *di)
3714 di->s = mangled;
3715 di->send = mangled + len;
3716 di->options = options;
3718 di->n = mangled;
3720 /* We can not need more components than twice the number of chars in
3721 the mangled string. Most components correspond directly to
3722 chars, but the ARGLIST types are exceptions. */
3723 di->num_comps = 2 * len;
3724 di->next_comp = 0;
3726 /* Similarly, we can not need more substitutions than there are
3727 chars in the mangled string. */
3728 di->num_subs = len;
3729 di->next_sub = 0;
3730 di->did_subs = 0;
3732 di->last_name = NULL;
3734 di->expansion = 0;
3737 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3738 name, return a buffer allocated with malloc holding the demangled
3739 name. OPTIONS is the usual libiberty demangler options. On
3740 success, this sets *PALC to the allocated size of the returned
3741 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3742 a memory allocation failure. On failure, this returns NULL. */
3744 static char *
3745 d_demangle (const char* mangled, int options, size_t *palc)
3747 size_t len;
3748 int type;
3749 struct d_info di;
3750 struct demangle_component *dc;
3751 int estimate;
3752 char *ret;
3754 *palc = 0;
3756 len = strlen (mangled);
3758 if (mangled[0] == '_' && mangled[1] == 'Z')
3759 type = 0;
3760 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3761 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3762 && (mangled[9] == 'D' || mangled[9] == 'I')
3763 && mangled[10] == '_')
3765 char *r;
3767 r = malloc (40 + len - 11);
3768 if (r == NULL)
3769 *palc = 1;
3770 else
3772 if (mangled[9] == 'I')
3773 strcpy (r, "global constructors keyed to ");
3774 else
3775 strcpy (r, "global destructors keyed to ");
3776 strcat (r, mangled + 11);
3778 return r;
3780 else
3782 if ((options & DMGL_TYPES) == 0)
3783 return NULL;
3784 type = 1;
3787 cplus_demangle_init_info (mangled, options, len, &di);
3790 #ifdef CP_DYNAMIC_ARRAYS
3791 __extension__ struct demangle_component comps[di.num_comps];
3792 __extension__ struct demangle_component *subs[di.num_subs];
3794 di.comps = &comps[0];
3795 di.subs = &subs[0];
3796 #else
3797 di.comps = ((struct demangle_component *)
3798 malloc (di.num_comps * sizeof (struct demangle_component)));
3799 di.subs = ((struct demangle_component **)
3800 malloc (di.num_subs * sizeof (struct demangle_component *)));
3801 if (di.comps == NULL || di.subs == NULL)
3803 if (di.comps != NULL)
3804 free (di.comps);
3805 if (di.subs != NULL)
3806 free (di.subs);
3807 *palc = 1;
3808 return NULL;
3810 #endif
3812 if (! type)
3813 dc = cplus_demangle_mangled_name (&di, 1);
3814 else
3815 dc = cplus_demangle_type (&di);
3817 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3818 mangled string, then we didn't successfully demangle it. If
3819 DMGL_PARAMS is not set, we didn't look at the trailing
3820 parameters. */
3821 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3822 dc = NULL;
3824 #ifdef CP_DEMANGLE_DEBUG
3825 if (dc == NULL)
3826 printf ("failed demangling\n");
3827 else
3828 d_dump (dc, 0);
3829 #endif
3831 /* We try to guess the length of the demangled string, to minimize
3832 calls to realloc during demangling. */
3833 estimate = len + di.expansion + 10 * di.did_subs;
3834 estimate += estimate / 8;
3836 ret = NULL;
3837 if (dc != NULL)
3838 ret = cplus_demangle_print (options, dc, estimate, palc);
3840 #ifndef CP_DYNAMIC_ARRAYS
3841 free (di.comps);
3842 free (di.subs);
3843 #endif
3845 #ifdef CP_DEMANGLE_DEBUG
3846 if (ret != NULL)
3848 int rlen;
3850 rlen = strlen (ret);
3851 if (rlen > 2 * estimate)
3852 printf ("*** Length %d much greater than estimate %d\n",
3853 rlen, estimate);
3854 else if (rlen > estimate)
3855 printf ("*** Length %d greater than estimate %d\n",
3856 rlen, estimate);
3857 else if (rlen < estimate / 2)
3858 printf ("*** Length %d much less than estimate %d\n",
3859 rlen, estimate);
3861 #endif
3864 return ret;
3867 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3869 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
3871 /* ia64 ABI-mandated entry point in the C++ runtime library for
3872 performing demangling. MANGLED_NAME is a NUL-terminated character
3873 string containing the name to be demangled.
3875 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3876 *LENGTH bytes, into which the demangled name is stored. If
3877 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3878 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3879 is placed in a region of memory allocated with malloc.
3881 If LENGTH is non-NULL, the length of the buffer conaining the
3882 demangled name, is placed in *LENGTH.
3884 The return value is a pointer to the start of the NUL-terminated
3885 demangled name, or NULL if the demangling fails. The caller is
3886 responsible for deallocating this memory using free.
3888 *STATUS is set to one of the following values:
3889 0: The demangling operation succeeded.
3890 -1: A memory allocation failure occurred.
3891 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3892 -3: One of the arguments is invalid.
3894 The demangling is performed using the C++ ABI mangling rules, with
3895 GNU extensions. */
3897 char *
3898 __cxa_demangle (const char *mangled_name, char *output_buffer,
3899 size_t *length, int *status)
3901 char *demangled;
3902 size_t alc;
3904 if (mangled_name == NULL)
3906 if (status != NULL)
3907 *status = -3;
3908 return NULL;
3911 if (output_buffer != NULL && length == NULL)
3913 if (status != NULL)
3914 *status = -3;
3915 return NULL;
3918 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
3920 if (demangled == NULL)
3922 if (status != NULL)
3924 if (alc == 1)
3925 *status = -1;
3926 else
3927 *status = -2;
3929 return NULL;
3932 if (output_buffer == NULL)
3934 if (length != NULL)
3935 *length = alc;
3937 else
3939 if (strlen (demangled) < *length)
3941 strcpy (output_buffer, demangled);
3942 free (demangled);
3943 demangled = output_buffer;
3945 else
3947 free (output_buffer);
3948 *length = alc;
3952 if (status != NULL)
3953 *status = 0;
3955 return demangled;
3958 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3960 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3961 mangled name, return a buffer allocated with malloc holding the
3962 demangled name. Otherwise, return NULL. */
3964 char *
3965 cplus_demangle_v3 (const char* mangled, int options)
3967 size_t alc;
3969 return d_demangle (mangled, options, &alc);
3972 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3973 conventions, but the output formatting is a little different.
3974 This instructs the C++ demangler not to emit pointer characters ("*"), and
3975 to use Java's namespace separator symbol ("." instead of "::"). It then
3976 does an additional pass over the demangled output to replace instances
3977 of JArray<TYPE> with TYPE[]. */
3979 char *
3980 java_demangle_v3 (const char* mangled)
3982 size_t alc;
3983 char *demangled;
3984 int nesting;
3985 char *from;
3986 char *to;
3988 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
3990 if (demangled == NULL)
3991 return NULL;
3993 nesting = 0;
3994 from = demangled;
3995 to = from;
3996 while (*from != '\0')
3998 if (strncmp (from, "JArray<", 7) == 0)
4000 from += 7;
4001 ++nesting;
4003 else if (nesting > 0 && *from == '>')
4005 while (to > demangled && to[-1] == ' ')
4006 --to;
4007 *to++ = '[';
4008 *to++ = ']';
4009 --nesting;
4010 ++from;
4012 else
4013 *to++ = *from++;
4016 *to = '\0';
4018 return demangled;
4021 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4023 #ifndef IN_GLIBCPP_V3
4025 /* Demangle a string in order to find out whether it is a constructor
4026 or destructor. Return non-zero on success. Set *CTOR_KIND and
4027 *DTOR_KIND appropriately. */
4029 static int
4030 is_ctor_or_dtor (const char *mangled,
4031 enum gnu_v3_ctor_kinds *ctor_kind,
4032 enum gnu_v3_dtor_kinds *dtor_kind)
4034 struct d_info di;
4035 struct demangle_component *dc;
4036 int ret;
4038 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4039 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4041 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4044 #ifdef CP_DYNAMIC_ARRAYS
4045 __extension__ struct demangle_component comps[di.num_comps];
4046 __extension__ struct demangle_component *subs[di.num_subs];
4048 di.comps = &comps[0];
4049 di.subs = &subs[0];
4050 #else
4051 di.comps = ((struct demangle_component *)
4052 malloc (di.num_comps * sizeof (struct demangle_component)));
4053 di.subs = ((struct demangle_component **)
4054 malloc (di.num_subs * sizeof (struct demangle_component *)));
4055 if (di.comps == NULL || di.subs == NULL)
4057 if (di.comps != NULL)
4058 free (di.comps);
4059 if (di.subs != NULL)
4060 free (di.subs);
4061 return 0;
4063 #endif
4065 dc = cplus_demangle_mangled_name (&di, 1);
4067 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4068 to demangle the entire string. */
4070 ret = 0;
4071 while (dc != NULL)
4073 switch (dc->type)
4075 default:
4076 dc = NULL;
4077 break;
4078 case DEMANGLE_COMPONENT_TYPED_NAME:
4079 case DEMANGLE_COMPONENT_TEMPLATE:
4080 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4081 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4082 case DEMANGLE_COMPONENT_CONST_THIS:
4083 dc = d_left (dc);
4084 break;
4085 case DEMANGLE_COMPONENT_QUAL_NAME:
4086 case DEMANGLE_COMPONENT_LOCAL_NAME:
4087 dc = d_right (dc);
4088 break;
4089 case DEMANGLE_COMPONENT_CTOR:
4090 *ctor_kind = dc->u.s_ctor.kind;
4091 ret = 1;
4092 dc = NULL;
4093 break;
4094 case DEMANGLE_COMPONENT_DTOR:
4095 *dtor_kind = dc->u.s_dtor.kind;
4096 ret = 1;
4097 dc = NULL;
4098 break;
4102 #ifndef CP_DYNAMIC_ARRAYS
4103 free (di.subs);
4104 free (di.comps);
4105 #endif
4108 return ret;
4111 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4112 name. A non-zero return indicates the type of constructor. */
4114 enum gnu_v3_ctor_kinds
4115 is_gnu_v3_mangled_ctor (const char *name)
4117 enum gnu_v3_ctor_kinds ctor_kind;
4118 enum gnu_v3_dtor_kinds dtor_kind;
4120 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4121 return (enum gnu_v3_ctor_kinds) 0;
4122 return ctor_kind;
4126 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4127 name. A non-zero return indicates the type of destructor. */
4129 enum gnu_v3_dtor_kinds
4130 is_gnu_v3_mangled_dtor (const char *name)
4132 enum gnu_v3_ctor_kinds ctor_kind;
4133 enum gnu_v3_dtor_kinds dtor_kind;
4135 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4136 return (enum gnu_v3_dtor_kinds) 0;
4137 return dtor_kind;
4140 #endif /* IN_GLIBCPP_V3 */
4142 #ifdef STANDALONE_DEMANGLER
4144 #include "getopt.h"
4145 #include "dyn-string.h"
4147 static void print_usage (FILE* fp, int exit_value);
4149 #define IS_ALPHA(CHAR) \
4150 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4151 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4153 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4154 #define is_mangled_char(CHAR) \
4155 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4156 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4158 /* The name of this program, as invoked. */
4159 const char* program_name;
4161 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4163 static void
4164 print_usage (FILE* fp, int exit_value)
4166 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4167 fprintf (fp, "Options:\n");
4168 fprintf (fp, " -h,--help Display this message.\n");
4169 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4170 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4171 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4173 exit (exit_value);
4176 /* Option specification for getopt_long. */
4177 static const struct option long_options[] =
4179 { "help", no_argument, NULL, 'h' },
4180 { "no-params", no_argument, NULL, 'p' },
4181 { "verbose", no_argument, NULL, 'v' },
4182 { NULL, no_argument, NULL, 0 },
4185 /* Main entry for a demangling filter executable. It will demangle
4186 its command line arguments, if any. If none are provided, it will
4187 filter stdin to stdout, replacing any recognized mangled C++ names
4188 with their demangled equivalents. */
4191 main (int argc, char *argv[])
4193 int i;
4194 int opt_char;
4195 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4197 /* Use the program name of this program, as invoked. */
4198 program_name = argv[0];
4200 /* Parse options. */
4203 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4204 switch (opt_char)
4206 case '?': /* Unrecognized option. */
4207 print_usage (stderr, 1);
4208 break;
4210 case 'h':
4211 print_usage (stdout, 0);
4212 break;
4214 case 'p':
4215 options &= ~ DMGL_PARAMS;
4216 break;
4218 case 'v':
4219 options |= DMGL_VERBOSE;
4220 break;
4223 while (opt_char != -1);
4225 if (optind == argc)
4226 /* No command line arguments were provided. Filter stdin. */
4228 dyn_string_t mangled = dyn_string_new (3);
4229 char *s;
4231 /* Read all of input. */
4232 while (!feof (stdin))
4234 char c;
4236 /* Pile characters into mangled until we hit one that can't
4237 occur in a mangled name. */
4238 c = getchar ();
4239 while (!feof (stdin) && is_mangled_char (c))
4241 dyn_string_append_char (mangled, c);
4242 if (feof (stdin))
4243 break;
4244 c = getchar ();
4247 if (dyn_string_length (mangled) > 0)
4249 #ifdef IN_GLIBCPP_V3
4250 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4251 #else
4252 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4253 #endif
4255 if (s != NULL)
4257 fputs (s, stdout);
4258 free (s);
4260 else
4262 /* It might not have been a mangled name. Print the
4263 original text. */
4264 fputs (dyn_string_buf (mangled), stdout);
4267 dyn_string_clear (mangled);
4270 /* If we haven't hit EOF yet, we've read one character that
4271 can't occur in a mangled name, so print it out. */
4272 if (!feof (stdin))
4273 putchar (c);
4276 dyn_string_delete (mangled);
4278 else
4279 /* Demangle command line arguments. */
4281 /* Loop over command line arguments. */
4282 for (i = optind; i < argc; ++i)
4284 char *s;
4285 #ifdef IN_GLIBCPP_V3
4286 int status;
4287 #endif
4289 /* Attempt to demangle. */
4290 #ifdef IN_GLIBCPP_V3
4291 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4292 #else
4293 s = cplus_demangle_v3 (argv[i], options);
4294 #endif
4296 /* If it worked, print the demangled name. */
4297 if (s != NULL)
4299 printf ("%s\n", s);
4300 free (s);
4302 else
4304 #ifdef IN_GLIBCPP_V3
4305 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4306 #else
4307 fprintf (stderr, "Failed: %s\n", argv[i]);
4308 #endif
4313 return 0;
4316 #endif /* STANDALONE_DEMANGLER */