Rename switch to enable/disable -Werror to --enable-werror/--disable-werror
[binutils.git] / libiberty / cp-demangle.c
blobc4436d7d7d592fcf290c3c87eb3e07e0bd344b47
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004 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
113 d_fill_name PARAMS ((struct demangle_component *, const char *, int));
115 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
116 static int
117 d_fill_extended_operator PARAMS ((struct demangle_component *, int,
118 struct demangle_component *));
120 #define cplus_demangle_fill_ctor d_fill_ctor
121 static int
122 d_fill_ctor PARAMS ((struct demangle_component *, enum gnu_v3_ctor_kinds,
123 struct demangle_component *));
125 #define cplus_demangle_fill_dtor d_fill_dtor
126 static int
127 d_fill_dtor PARAMS ((struct demangle_component *, enum gnu_v3_dtor_kinds,
128 struct demangle_component *));
130 #define cplus_demangle_mangled_name d_mangled_name
131 static struct demangle_component *
132 d_mangled_name PARAMS ((struct d_info *, int));
134 #define cplus_demangle_type d_type
135 static struct demangle_component *
136 d_type PARAMS ((struct d_info *));
138 #define cplus_demangle_print d_print
139 static char *
140 d_print PARAMS ((int, const struct demangle_component *, int, size_t *));
142 #define cplus_demangle_init_info d_init_info
143 static void
144 d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
146 #else /* ! defined(IN_GLIBCPP_V3) */
147 #define CP_STATIC_IF_GLIBCPP_V3
148 #endif /* ! defined(IN_GLIBCPP_V3) */
150 /* See if the compiler supports dynamic arrays. */
152 #ifdef __GNUC__
153 #define CP_DYNAMIC_ARRAYS
154 #else
155 #ifdef __STDC__
156 #ifdef __STDC_VERSION__
157 #if __STDC_VERSION__ >= 199901L
158 #define CP_DYNAMIC_ARRAYS
159 #endif /* __STDC__VERSION >= 199901L */
160 #endif /* defined (__STDC_VERSION__) */
161 #endif /* defined (__STDC__) */
162 #endif /* ! defined (__GNUC__) */
164 /* We avoid pulling in the ctype tables, to prevent pulling in
165 additional unresolved symbols when this code is used in a library.
166 FIXME: Is this really a valid reason? This comes from the original
167 V3 demangler code.
169 As of this writing this file has the following undefined references
170 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
171 strcpy, strcat, strlen. */
173 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
174 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
175 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
177 /* The prefix prepended by GCC to an identifier represnting the
178 anonymous namespace. */
179 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
180 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
181 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
183 /* Information we keep for the standard substitutions. */
185 struct d_standard_sub_info
187 /* The code for this substitution. */
188 char code;
189 /* The simple string it expands to. */
190 const char *simple_expansion;
191 /* The length of the simple expansion. */
192 int simple_len;
193 /* The results of a full, verbose, expansion. This is used when
194 qualifying a constructor/destructor, or when in verbose mode. */
195 const char *full_expansion;
196 /* The length of the full expansion. */
197 int full_len;
198 /* What to set the last_name field of d_info to; NULL if we should
199 not set it. This is only relevant when qualifying a
200 constructor/destructor. */
201 const char *set_last_name;
202 /* The length of set_last_name. */
203 int set_last_name_len;
206 /* Accessors for subtrees of struct demangle_component. */
208 #define d_left(dc) ((dc)->u.s_binary.left)
209 #define d_right(dc) ((dc)->u.s_binary.right)
211 /* A list of templates. This is used while printing. */
213 struct d_print_template
215 /* Next template on the list. */
216 struct d_print_template *next;
217 /* This template. */
218 const struct demangle_component *template;
221 /* A list of type modifiers. This is used while printing. */
223 struct d_print_mod
225 /* Next modifier on the list. These are in the reverse of the order
226 in which they appeared in the mangled string. */
227 struct d_print_mod *next;
228 /* The modifier. */
229 const struct demangle_component *mod;
230 /* Whether this modifier was printed. */
231 int printed;
232 /* The list of templates which applies to this modifier. */
233 struct d_print_template *templates;
236 /* We use this structure to hold information during printing. */
238 struct d_print_info
240 /* The options passed to the demangler. */
241 int options;
242 /* Buffer holding the result. */
243 char *buf;
244 /* Current length of data in buffer. */
245 size_t len;
246 /* Allocated size of buffer. */
247 size_t alc;
248 /* The current list of templates, if any. */
249 struct d_print_template *templates;
250 /* The current list of modifiers (e.g., pointer, reference, etc.),
251 if any. */
252 struct d_print_mod *modifiers;
253 /* Set to 1 if we had a memory allocation failure. */
254 int allocation_failure;
257 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
259 #define d_append_char(dpi, c) \
260 do \
262 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
263 (dpi)->buf[(dpi)->len++] = (c); \
264 else \
265 d_print_append_char ((dpi), (c)); \
267 while (0)
269 #define d_append_buffer(dpi, s, l) \
270 do \
272 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
274 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
275 (dpi)->len += l; \
277 else \
278 d_print_append_buffer ((dpi), (s), (l)); \
280 while (0)
282 #define d_append_string_constant(dpi, s) \
283 d_append_buffer (dpi, (s), sizeof (s) - 1)
285 #define d_last_char(dpi) \
286 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
288 #ifdef CP_DEMANGLE_DEBUG
289 static void
290 d_dump PARAMS ((struct demangle_component *, int));
291 #endif
293 static struct demangle_component *
294 d_make_empty PARAMS ((struct d_info *));
296 static struct demangle_component *
297 d_make_comp PARAMS ((struct d_info *, enum demangle_component_type,
298 struct demangle_component *,
299 struct demangle_component *));
301 static struct demangle_component *
302 d_make_name PARAMS ((struct d_info *, const char *, int));
304 static struct demangle_component *
305 d_make_builtin_type PARAMS ((struct d_info *,
306 const struct demangle_builtin_type_info *));
308 static struct demangle_component *
309 d_make_operator PARAMS ((struct d_info *,
310 const struct demangle_operator_info *));
312 static struct demangle_component *
313 d_make_extended_operator PARAMS ((struct d_info *, int,
314 struct demangle_component *));
316 static struct demangle_component *
317 d_make_ctor PARAMS ((struct d_info *, enum gnu_v3_ctor_kinds,
318 struct demangle_component *));
320 static struct demangle_component *
321 d_make_dtor PARAMS ((struct d_info *, enum gnu_v3_dtor_kinds,
322 struct demangle_component *));
324 static struct demangle_component *
325 d_make_template_param PARAMS ((struct d_info *, long));
327 static struct demangle_component *
328 d_make_sub PARAMS ((struct d_info *, const char *, int));
330 static int
331 has_return_type PARAMS ((struct demangle_component *));
333 static int
334 is_ctor_dtor_or_conversion PARAMS ((struct demangle_component *));
336 static struct demangle_component *
337 d_encoding PARAMS ((struct d_info *, int));
339 static struct demangle_component *
340 d_name PARAMS ((struct d_info *));
342 static struct demangle_component *
343 d_nested_name PARAMS ((struct d_info *));
345 static struct demangle_component *
346 d_prefix PARAMS ((struct d_info *));
348 static struct demangle_component *
349 d_unqualified_name PARAMS ((struct d_info *));
351 static struct demangle_component *
352 d_source_name PARAMS ((struct d_info *));
354 static long
355 d_number PARAMS ((struct d_info *));
357 static struct demangle_component *
358 d_identifier PARAMS ((struct d_info *, int));
360 static struct demangle_component *
361 d_operator_name PARAMS ((struct d_info *));
363 static struct demangle_component *
364 d_special_name PARAMS ((struct d_info *));
366 static int
367 d_call_offset PARAMS ((struct d_info *, int));
369 static struct demangle_component *
370 d_ctor_dtor_name PARAMS ((struct d_info *));
372 static struct demangle_component **
373 d_cv_qualifiers PARAMS ((struct d_info *, struct demangle_component **, int));
375 static struct demangle_component *
376 d_function_type PARAMS ((struct d_info *));
378 static struct demangle_component *
379 d_bare_function_type PARAMS ((struct d_info *, int));
381 static struct demangle_component *
382 d_class_enum_type PARAMS ((struct d_info *));
384 static struct demangle_component *
385 d_array_type PARAMS ((struct d_info *));
387 static struct demangle_component *
388 d_pointer_to_member_type PARAMS ((struct d_info *));
390 static struct demangle_component *
391 d_template_param PARAMS ((struct d_info *));
393 static struct demangle_component *
394 d_template_args PARAMS ((struct d_info *));
396 static struct demangle_component *
397 d_template_arg PARAMS ((struct d_info *));
399 static struct demangle_component *
400 d_expression PARAMS ((struct d_info *));
402 static struct demangle_component *
403 d_expr_primary PARAMS ((struct d_info *));
405 static struct demangle_component *
406 d_local_name PARAMS ((struct d_info *));
408 static int
409 d_discriminator PARAMS ((struct d_info *));
411 static int
412 d_add_substitution PARAMS ((struct d_info *, struct demangle_component *));
414 static struct demangle_component *
415 d_substitution PARAMS ((struct d_info *, int));
417 static void
418 d_print_resize PARAMS ((struct d_print_info *, size_t));
420 static void
421 d_print_append_char PARAMS ((struct d_print_info *, int));
423 static void
424 d_print_append_buffer PARAMS ((struct d_print_info *, const char *, size_t));
426 static void
427 d_print_error PARAMS ((struct d_print_info *));
429 static void
430 d_print_comp PARAMS ((struct d_print_info *,
431 const struct demangle_component *));
433 static void
434 d_print_java_identifier PARAMS ((struct d_print_info *, const char *, int));
436 static void
437 d_print_mod_list PARAMS ((struct d_print_info *, struct d_print_mod *, int));
439 static void
440 d_print_mod PARAMS ((struct d_print_info *,
441 const struct demangle_component *));
443 static void
444 d_print_function_type PARAMS ((struct d_print_info *,
445 const struct demangle_component *,
446 struct d_print_mod *));
448 static void
449 d_print_array_type PARAMS ((struct d_print_info *,
450 const struct demangle_component *,
451 struct d_print_mod *));
453 static void
454 d_print_expr_op PARAMS ((struct d_print_info *,
455 const struct demangle_component *));
457 static void
458 d_print_cast PARAMS ((struct d_print_info *,
459 const struct demangle_component *));
461 static char *
462 d_demangle PARAMS ((const char *, int, size_t *));
464 #ifdef CP_DEMANGLE_DEBUG
466 static void
467 d_dump (dc, indent)
468 struct demangle_component *dc;
469 int indent;
471 int i;
473 if (dc == NULL)
474 return;
476 for (i = 0; i < indent; ++i)
477 putchar (' ');
479 switch (dc->type)
481 case DEMANGLE_COMPONENT_NAME:
482 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
483 return;
484 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
485 printf ("template parameter %ld\n", dc->u.s_number.number);
486 return;
487 case DEMANGLE_COMPONENT_CTOR:
488 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
489 d_dump (dc->u.s_ctor.name, indent + 2);
490 return;
491 case DEMANGLE_COMPONENT_DTOR:
492 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
493 d_dump (dc->u.s_dtor.name, indent + 2);
494 return;
495 case DEMANGLE_COMPONENT_SUB_STD:
496 printf ("standard substitution %s\n", dc->u.s_string.string);
497 return;
498 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
499 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
500 return;
501 case DEMANGLE_COMPONENT_OPERATOR:
502 printf ("operator %s\n", dc->u.s_operator.op->name);
503 return;
504 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
505 printf ("extended operator with %d args\n",
506 dc->u.s_extended_operator.args);
507 d_dump (dc->u.s_extended_operator.name, indent + 2);
508 return;
510 case DEMANGLE_COMPONENT_QUAL_NAME:
511 printf ("qualified name\n");
512 break;
513 case DEMANGLE_COMPONENT_LOCAL_NAME:
514 printf ("local name\n");
515 break;
516 case DEMANGLE_COMPONENT_TYPED_NAME:
517 printf ("typed name\n");
518 break;
519 case DEMANGLE_COMPONENT_TEMPLATE:
520 printf ("template\n");
521 break;
522 case DEMANGLE_COMPONENT_VTABLE:
523 printf ("vtable\n");
524 break;
525 case DEMANGLE_COMPONENT_VTT:
526 printf ("VTT\n");
527 break;
528 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
529 printf ("construction vtable\n");
530 break;
531 case DEMANGLE_COMPONENT_TYPEINFO:
532 printf ("typeinfo\n");
533 break;
534 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
535 printf ("typeinfo name\n");
536 break;
537 case DEMANGLE_COMPONENT_TYPEINFO_FN:
538 printf ("typeinfo function\n");
539 break;
540 case DEMANGLE_COMPONENT_THUNK:
541 printf ("thunk\n");
542 break;
543 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
544 printf ("virtual thunk\n");
545 break;
546 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
547 printf ("covariant thunk\n");
548 break;
549 case DEMANGLE_COMPONENT_JAVA_CLASS:
550 printf ("java class\n");
551 break;
552 case DEMANGLE_COMPONENT_GUARD:
553 printf ("guard\n");
554 break;
555 case DEMANGLE_COMPONENT_REFTEMP:
556 printf ("reference temporary\n");
557 break;
558 case DEMANGLE_COMPONENT_RESTRICT:
559 printf ("restrict\n");
560 break;
561 case DEMANGLE_COMPONENT_VOLATILE:
562 printf ("volatile\n");
563 break;
564 case DEMANGLE_COMPONENT_CONST:
565 printf ("const\n");
566 break;
567 case DEMANGLE_COMPONENT_RESTRICT_THIS:
568 printf ("restrict this\n");
569 break;
570 case DEMANGLE_COMPONENT_VOLATILE_THIS:
571 printf ("volatile this\n");
572 break;
573 case DEMANGLE_COMPONENT_CONST_THIS:
574 printf ("const this\n");
575 break;
576 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
577 printf ("vendor type qualifier\n");
578 break;
579 case DEMANGLE_COMPONENT_POINTER:
580 printf ("pointer\n");
581 break;
582 case DEMANGLE_COMPONENT_REFERENCE:
583 printf ("reference\n");
584 break;
585 case DEMANGLE_COMPONENT_COMPLEX:
586 printf ("complex\n");
587 break;
588 case DEMANGLE_COMPONENT_IMAGINARY:
589 printf ("imaginary\n");
590 break;
591 case DEMANGLE_COMPONENT_VENDOR_TYPE:
592 printf ("vendor type\n");
593 break;
594 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
595 printf ("function type\n");
596 break;
597 case DEMANGLE_COMPONENT_ARRAY_TYPE:
598 printf ("array type\n");
599 break;
600 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
601 printf ("pointer to member type\n");
602 break;
603 case DEMANGLE_COMPONENT_ARGLIST:
604 printf ("argument list\n");
605 break;
606 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
607 printf ("template argument list\n");
608 break;
609 case DEMANGLE_COMPONENT_CAST:
610 printf ("cast\n");
611 break;
612 case DEMANGLE_COMPONENT_UNARY:
613 printf ("unary operator\n");
614 break;
615 case DEMANGLE_COMPONENT_BINARY:
616 printf ("binary operator\n");
617 break;
618 case DEMANGLE_COMPONENT_BINARY_ARGS:
619 printf ("binary operator arguments\n");
620 break;
621 case DEMANGLE_COMPONENT_TRINARY:
622 printf ("trinary operator\n");
623 break;
624 case DEMANGLE_COMPONENT_TRINARY_ARG1:
625 printf ("trinary operator arguments 1\n");
626 break;
627 case DEMANGLE_COMPONENT_TRINARY_ARG2:
628 printf ("trinary operator arguments 1\n");
629 break;
630 case DEMANGLE_COMPONENT_LITERAL:
631 printf ("literal\n");
632 break;
633 case DEMANGLE_COMPONENT_LITERAL_NEG:
634 printf ("negative literal\n");
635 break;
638 d_dump (d_left (dc), indent + 2);
639 d_dump (d_right (dc), indent + 2);
642 #endif /* CP_DEMANGLE_DEBUG */
644 /* Fill in a DEMANGLE_COMPONENT_NAME. */
646 CP_STATIC_IF_GLIBCPP_V3
648 cplus_demangle_fill_name (p, s, len)
649 struct demangle_component *p;
650 const char *s;
651 int len;
653 if (p == NULL || s == NULL || len == 0)
654 return 0;
655 p->type = DEMANGLE_COMPONENT_NAME;
656 p->u.s_name.s = s;
657 p->u.s_name.len = len;
658 return 1;
661 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
663 CP_STATIC_IF_GLIBCPP_V3
665 cplus_demangle_fill_extended_operator (p, args, name)
666 struct demangle_component *p;
667 int args;
668 struct demangle_component *name;
670 if (p == NULL || args < 0 || name == NULL)
671 return 0;
672 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
673 p->u.s_extended_operator.args = args;
674 p->u.s_extended_operator.name = name;
675 return 1;
678 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
680 CP_STATIC_IF_GLIBCPP_V3
682 cplus_demangle_fill_ctor (p, kind, name)
683 struct demangle_component *p;
684 enum gnu_v3_ctor_kinds kind;
685 struct demangle_component *name;
687 if (p == NULL
688 || name == NULL
689 || (kind < gnu_v3_complete_object_ctor
690 && kind > gnu_v3_complete_object_allocating_ctor))
691 return 0;
692 p->type = DEMANGLE_COMPONENT_CTOR;
693 p->u.s_ctor.kind = kind;
694 p->u.s_ctor.name = name;
695 return 1;
698 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
700 CP_STATIC_IF_GLIBCPP_V3
702 cplus_demangle_fill_dtor (p, kind, name)
703 struct demangle_component *p;
704 enum gnu_v3_dtor_kinds kind;
705 struct demangle_component *name;
707 if (p == NULL
708 || name == NULL
709 || (kind < gnu_v3_deleting_dtor
710 && kind > gnu_v3_base_object_dtor))
711 return 0;
712 p->type = DEMANGLE_COMPONENT_DTOR;
713 p->u.s_dtor.kind = kind;
714 p->u.s_dtor.name = name;
715 return 1;
718 /* Add a new component. */
720 static struct demangle_component *
721 d_make_empty (di)
722 struct d_info *di;
724 struct demangle_component *p;
726 if (di->next_comp >= di->num_comps)
727 return NULL;
728 p = &di->comps[di->next_comp];
729 ++di->next_comp;
730 return p;
733 /* Add a new generic component. */
735 static struct demangle_component *
736 d_make_comp (di, type, left, right)
737 struct d_info *di;
738 enum demangle_component_type type;
739 struct demangle_component *left;
740 struct demangle_component *right;
742 struct demangle_component *p;
744 /* We check for errors here. A typical error would be a NULL return
745 from a subroutine. We catch those here, and return NULL
746 upward. */
747 switch (type)
749 /* These types require two parameters. */
750 case DEMANGLE_COMPONENT_QUAL_NAME:
751 case DEMANGLE_COMPONENT_LOCAL_NAME:
752 case DEMANGLE_COMPONENT_TYPED_NAME:
753 case DEMANGLE_COMPONENT_TEMPLATE:
754 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
755 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
756 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
757 case DEMANGLE_COMPONENT_UNARY:
758 case DEMANGLE_COMPONENT_BINARY:
759 case DEMANGLE_COMPONENT_BINARY_ARGS:
760 case DEMANGLE_COMPONENT_TRINARY:
761 case DEMANGLE_COMPONENT_TRINARY_ARG1:
762 case DEMANGLE_COMPONENT_TRINARY_ARG2:
763 case DEMANGLE_COMPONENT_LITERAL:
764 case DEMANGLE_COMPONENT_LITERAL_NEG:
765 if (left == NULL || right == NULL)
766 return NULL;
767 break;
769 /* These types only require one parameter. */
770 case DEMANGLE_COMPONENT_VTABLE:
771 case DEMANGLE_COMPONENT_VTT:
772 case DEMANGLE_COMPONENT_TYPEINFO:
773 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
774 case DEMANGLE_COMPONENT_TYPEINFO_FN:
775 case DEMANGLE_COMPONENT_THUNK:
776 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
777 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
778 case DEMANGLE_COMPONENT_JAVA_CLASS:
779 case DEMANGLE_COMPONENT_GUARD:
780 case DEMANGLE_COMPONENT_REFTEMP:
781 case DEMANGLE_COMPONENT_POINTER:
782 case DEMANGLE_COMPONENT_REFERENCE:
783 case DEMANGLE_COMPONENT_COMPLEX:
784 case DEMANGLE_COMPONENT_IMAGINARY:
785 case DEMANGLE_COMPONENT_VENDOR_TYPE:
786 case DEMANGLE_COMPONENT_ARGLIST:
787 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
788 case DEMANGLE_COMPONENT_CAST:
789 if (left == NULL)
790 return NULL;
791 break;
793 /* This needs a right parameter, but the left parameter can be
794 empty. */
795 case DEMANGLE_COMPONENT_ARRAY_TYPE:
796 if (right == NULL)
797 return NULL;
798 break;
800 /* These are allowed to have no parameters--in some cases they
801 will be filled in later. */
802 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
803 case DEMANGLE_COMPONENT_RESTRICT:
804 case DEMANGLE_COMPONENT_VOLATILE:
805 case DEMANGLE_COMPONENT_CONST:
806 case DEMANGLE_COMPONENT_RESTRICT_THIS:
807 case DEMANGLE_COMPONENT_VOLATILE_THIS:
808 case DEMANGLE_COMPONENT_CONST_THIS:
809 break;
811 /* Other types should not be seen here. */
812 default:
813 return NULL;
816 p = d_make_empty (di);
817 if (p != NULL)
819 p->type = type;
820 p->u.s_binary.left = left;
821 p->u.s_binary.right = right;
823 return p;
826 /* Add a new name component. */
828 static struct demangle_component *
829 d_make_name (di, s, len)
830 struct d_info *di;
831 const char *s;
832 int len;
834 struct demangle_component *p;
836 p = d_make_empty (di);
837 if (! cplus_demangle_fill_name (p, s, len))
838 return NULL;
839 return p;
842 /* Add a new builtin type component. */
844 static struct demangle_component *
845 d_make_builtin_type (di, type)
846 struct d_info *di;
847 const struct demangle_builtin_type_info *type;
849 struct demangle_component *p;
851 if (type == NULL)
852 return NULL;
853 p = d_make_empty (di);
854 if (p != NULL)
856 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
857 p->u.s_builtin.type = type;
859 return p;
862 /* Add a new operator component. */
864 static struct demangle_component *
865 d_make_operator (di, op)
866 struct d_info *di;
867 const struct demangle_operator_info *op;
869 struct demangle_component *p;
871 p = d_make_empty (di);
872 if (p != NULL)
874 p->type = DEMANGLE_COMPONENT_OPERATOR;
875 p->u.s_operator.op = op;
877 return p;
880 /* Add a new extended operator component. */
882 static struct demangle_component *
883 d_make_extended_operator (di, args, name)
884 struct d_info *di;
885 int args;
886 struct demangle_component *name;
888 struct demangle_component *p;
890 p = d_make_empty (di);
891 if (! cplus_demangle_fill_extended_operator (p, args, name))
892 return NULL;
893 return p;
896 /* Add a new constructor component. */
898 static struct demangle_component *
899 d_make_ctor (di, kind, name)
900 struct d_info *di;
901 enum gnu_v3_ctor_kinds kind;
902 struct demangle_component *name;
904 struct demangle_component *p;
906 p = d_make_empty (di);
907 if (! cplus_demangle_fill_ctor (p, kind, name))
908 return NULL;
909 return p;
912 /* Add a new destructor component. */
914 static struct demangle_component *
915 d_make_dtor (di, kind, name)
916 struct d_info *di;
917 enum gnu_v3_dtor_kinds kind;
918 struct demangle_component *name;
920 struct demangle_component *p;
922 p = d_make_empty (di);
923 if (! cplus_demangle_fill_dtor (p, kind, name))
924 return NULL;
925 return p;
928 /* Add a new template parameter. */
930 static struct demangle_component *
931 d_make_template_param (di, i)
932 struct d_info *di;
933 long i;
935 struct demangle_component *p;
937 p = d_make_empty (di);
938 if (p != NULL)
940 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
941 p->u.s_number.number = i;
943 return p;
946 /* Add a new standard substitution component. */
948 static struct demangle_component *
949 d_make_sub (di, name, len)
950 struct d_info *di;
951 const char *name;
952 int len;
954 struct demangle_component *p;
956 p = d_make_empty (di);
957 if (p != NULL)
959 p->type = DEMANGLE_COMPONENT_SUB_STD;
960 p->u.s_string.string = name;
961 p->u.s_string.len = len;
963 return p;
966 /* <mangled-name> ::= _Z <encoding>
968 TOP_LEVEL is non-zero when called at the top level. */
970 CP_STATIC_IF_GLIBCPP_V3
971 struct demangle_component *
972 cplus_demangle_mangled_name (di, top_level)
973 struct d_info *di;
974 int top_level;
976 if (d_next_char (di) != '_')
977 return NULL;
978 if (d_next_char (di) != 'Z')
979 return NULL;
980 return d_encoding (di, top_level);
983 /* Return whether a function should have a return type. The argument
984 is the function name, which may be qualified in various ways. The
985 rules are that template functions have return types with some
986 exceptions, function types which are not part of a function name
987 mangling have return types with some exceptions, and non-template
988 function names do not have return types. The exceptions are that
989 constructors, destructors, and conversion operators do not have
990 return types. */
992 static int
993 has_return_type (dc)
994 struct demangle_component *dc;
996 if (dc == NULL)
997 return 0;
998 switch (dc->type)
1000 default:
1001 return 0;
1002 case DEMANGLE_COMPONENT_TEMPLATE:
1003 return ! is_ctor_dtor_or_conversion (d_left (dc));
1004 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1005 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1006 case DEMANGLE_COMPONENT_CONST_THIS:
1007 return has_return_type (d_left (dc));
1011 /* Return whether a name is a constructor, a destructor, or a
1012 conversion operator. */
1014 static int
1015 is_ctor_dtor_or_conversion (dc)
1016 struct demangle_component *dc;
1018 if (dc == NULL)
1019 return 0;
1020 switch (dc->type)
1022 default:
1023 return 0;
1024 case DEMANGLE_COMPONENT_QUAL_NAME:
1025 case DEMANGLE_COMPONENT_LOCAL_NAME:
1026 return is_ctor_dtor_or_conversion (d_right (dc));
1027 case DEMANGLE_COMPONENT_CTOR:
1028 case DEMANGLE_COMPONENT_DTOR:
1029 case DEMANGLE_COMPONENT_CAST:
1030 return 1;
1034 /* <encoding> ::= <(function) name> <bare-function-type>
1035 ::= <(data) name>
1036 ::= <special-name>
1038 TOP_LEVEL is non-zero when called at the top level, in which case
1039 if DMGL_PARAMS is not set we do not demangle the function
1040 parameters. We only set this at the top level, because otherwise
1041 we would not correctly demangle names in local scopes. */
1043 static struct demangle_component *
1044 d_encoding (di, top_level)
1045 struct d_info *di;
1046 int top_level;
1048 char peek = d_peek_char (di);
1050 if (peek == 'G' || peek == 'T')
1051 return d_special_name (di);
1052 else
1054 struct demangle_component *dc;
1056 dc = d_name (di);
1058 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1060 /* Strip off any initial CV-qualifiers, as they really apply
1061 to the `this' parameter, and they were not output by the
1062 v2 demangler without DMGL_PARAMS. */
1063 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1064 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1065 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1066 dc = d_left (dc);
1068 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1069 there may be CV-qualifiers on its right argument which
1070 really apply here; this happens when parsing a class
1071 which is local to a function. */
1072 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1074 struct demangle_component *dcr;
1076 dcr = d_right (dc);
1077 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1078 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1079 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1080 dcr = d_left (dcr);
1081 dc->u.s_binary.right = dcr;
1084 return dc;
1087 peek = d_peek_char (di);
1088 if (peek == '\0' || peek == 'E')
1089 return dc;
1090 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1091 d_bare_function_type (di, has_return_type (dc)));
1095 /* <name> ::= <nested-name>
1096 ::= <unscoped-name>
1097 ::= <unscoped-template-name> <template-args>
1098 ::= <local-name>
1100 <unscoped-name> ::= <unqualified-name>
1101 ::= St <unqualified-name>
1103 <unscoped-template-name> ::= <unscoped-name>
1104 ::= <substitution>
1107 static struct demangle_component *
1108 d_name (di)
1109 struct d_info *di;
1111 char peek = d_peek_char (di);
1112 struct demangle_component *dc;
1114 switch (peek)
1116 case 'N':
1117 return d_nested_name (di);
1119 case 'Z':
1120 return d_local_name (di);
1122 case 'S':
1124 int subst;
1126 if (d_peek_next_char (di) != 't')
1128 dc = d_substitution (di, 0);
1129 subst = 1;
1131 else
1133 d_advance (di, 2);
1134 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1135 d_make_name (di, "std", 3),
1136 d_unqualified_name (di));
1137 di->expansion += 3;
1138 subst = 0;
1141 if (d_peek_char (di) != 'I')
1143 /* The grammar does not permit this case to occur if we
1144 called d_substitution() above (i.e., subst == 1). We
1145 don't bother to check. */
1147 else
1149 /* This is <template-args>, which means that we just saw
1150 <unscoped-template-name>, which is a substitution
1151 candidate if we didn't just get it from a
1152 substitution. */
1153 if (! subst)
1155 if (! d_add_substitution (di, dc))
1156 return NULL;
1158 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1159 d_template_args (di));
1162 return dc;
1165 default:
1166 dc = d_unqualified_name (di);
1167 if (d_peek_char (di) == 'I')
1169 /* This is <template-args>, which means that we just saw
1170 <unscoped-template-name>, which is a substitution
1171 candidate. */
1172 if (! d_add_substitution (di, dc))
1173 return NULL;
1174 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1175 d_template_args (di));
1177 return dc;
1181 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1182 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1185 static struct demangle_component *
1186 d_nested_name (di)
1187 struct d_info *di;
1189 struct demangle_component *ret;
1190 struct demangle_component **pret;
1192 if (d_next_char (di) != 'N')
1193 return NULL;
1195 pret = d_cv_qualifiers (di, &ret, 1);
1196 if (pret == NULL)
1197 return NULL;
1199 *pret = d_prefix (di);
1200 if (*pret == NULL)
1201 return NULL;
1203 if (d_next_char (di) != 'E')
1204 return NULL;
1206 return ret;
1209 /* <prefix> ::= <prefix> <unqualified-name>
1210 ::= <template-prefix> <template-args>
1211 ::= <template-param>
1213 ::= <substitution>
1215 <template-prefix> ::= <prefix> <(template) unqualified-name>
1216 ::= <template-param>
1217 ::= <substitution>
1220 static struct demangle_component *
1221 d_prefix (di)
1222 struct d_info *di;
1224 struct demangle_component *ret = NULL;
1226 while (1)
1228 char peek;
1229 enum demangle_component_type comb_type;
1230 struct demangle_component *dc;
1232 peek = d_peek_char (di);
1233 if (peek == '\0')
1234 return NULL;
1236 /* The older code accepts a <local-name> here, but I don't see
1237 that in the grammar. The older code does not accept a
1238 <template-param> here. */
1240 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1241 if (IS_DIGIT (peek)
1242 || IS_LOWER (peek)
1243 || peek == 'C'
1244 || peek == 'D')
1245 dc = d_unqualified_name (di);
1246 else if (peek == 'S')
1247 dc = d_substitution (di, 1);
1248 else if (peek == 'I')
1250 if (ret == NULL)
1251 return NULL;
1252 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1253 dc = d_template_args (di);
1255 else if (peek == 'T')
1256 dc = d_template_param (di);
1257 else if (peek == 'E')
1258 return ret;
1259 else
1260 return NULL;
1262 if (ret == NULL)
1263 ret = dc;
1264 else
1265 ret = d_make_comp (di, comb_type, ret, dc);
1267 if (peek != 'S' && d_peek_char (di) != 'E')
1269 if (! d_add_substitution (di, ret))
1270 return NULL;
1275 /* <unqualified-name> ::= <operator-name>
1276 ::= <ctor-dtor-name>
1277 ::= <source-name>
1280 static struct demangle_component *
1281 d_unqualified_name (di)
1282 struct d_info *di;
1284 char peek;
1286 peek = d_peek_char (di);
1287 if (IS_DIGIT (peek))
1288 return d_source_name (di);
1289 else if (IS_LOWER (peek))
1291 struct demangle_component *ret;
1293 ret = d_operator_name (di);
1294 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1295 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1296 return ret;
1298 else if (peek == 'C' || peek == 'D')
1299 return d_ctor_dtor_name (di);
1300 else
1301 return NULL;
1304 /* <source-name> ::= <(positive length) number> <identifier> */
1306 static struct demangle_component *
1307 d_source_name (di)
1308 struct d_info *di;
1310 long len;
1311 struct demangle_component *ret;
1313 len = d_number (di);
1314 if (len <= 0)
1315 return NULL;
1316 ret = d_identifier (di, len);
1317 di->last_name = ret;
1318 return ret;
1321 /* number ::= [n] <(non-negative decimal integer)> */
1323 static long
1324 d_number (di)
1325 struct d_info *di;
1327 int negative;
1328 char peek;
1329 long ret;
1331 negative = 0;
1332 peek = d_peek_char (di);
1333 if (peek == 'n')
1335 negative = 1;
1336 d_advance (di, 1);
1337 peek = d_peek_char (di);
1340 ret = 0;
1341 while (1)
1343 if (! IS_DIGIT (peek))
1345 if (negative)
1346 ret = - ret;
1347 return ret;
1349 ret = ret * 10 + peek - '0';
1350 d_advance (di, 1);
1351 peek = d_peek_char (di);
1355 /* identifier ::= <(unqualified source code identifier)> */
1357 static struct demangle_component *
1358 d_identifier (di, len)
1359 struct d_info *di;
1360 int len;
1362 const char *name;
1364 name = d_str (di);
1366 if (di->send - name < len)
1367 return NULL;
1369 d_advance (di, len);
1371 /* A Java mangled name may have a trailing '$' if it is a C++
1372 keyword. This '$' is not included in the length count. We just
1373 ignore the '$'. */
1374 if ((di->options & DMGL_JAVA) != 0
1375 && d_peek_char (di) == '$')
1376 d_advance (di, 1);
1378 /* Look for something which looks like a gcc encoding of an
1379 anonymous namespace, and replace it with a more user friendly
1380 name. */
1381 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1382 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1383 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1385 const char *s;
1387 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1388 if ((*s == '.' || *s == '_' || *s == '$')
1389 && s[1] == 'N')
1391 di->expansion -= len - sizeof "(anonymous namespace)";
1392 return d_make_name (di, "(anonymous namespace)",
1393 sizeof "(anonymous namespace)" - 1);
1397 return d_make_name (di, name, len);
1400 /* operator_name ::= many different two character encodings.
1401 ::= cv <type>
1402 ::= v <digit> <source-name>
1405 #define NL(s) s, (sizeof s) - 1
1407 CP_STATIC_IF_GLIBCPP_V3
1408 const struct demangle_operator_info cplus_demangle_operators[] =
1410 { "aN", NL ("&="), 2 },
1411 { "aS", NL ("="), 2 },
1412 { "aa", NL ("&&"), 2 },
1413 { "ad", NL ("&"), 1 },
1414 { "an", NL ("&"), 2 },
1415 { "cl", NL ("()"), 0 },
1416 { "cm", NL (","), 2 },
1417 { "co", NL ("~"), 1 },
1418 { "dV", NL ("/="), 2 },
1419 { "da", NL ("delete[]"), 1 },
1420 { "de", NL ("*"), 1 },
1421 { "dl", NL ("delete"), 1 },
1422 { "dv", NL ("/"), 2 },
1423 { "eO", NL ("^="), 2 },
1424 { "eo", NL ("^"), 2 },
1425 { "eq", NL ("=="), 2 },
1426 { "ge", NL (">="), 2 },
1427 { "gt", NL (">"), 2 },
1428 { "ix", NL ("[]"), 2 },
1429 { "lS", NL ("<<="), 2 },
1430 { "le", NL ("<="), 2 },
1431 { "ls", NL ("<<"), 2 },
1432 { "lt", NL ("<"), 2 },
1433 { "mI", NL ("-="), 2 },
1434 { "mL", NL ("*="), 2 },
1435 { "mi", NL ("-"), 2 },
1436 { "ml", NL ("*"), 2 },
1437 { "mm", NL ("--"), 1 },
1438 { "na", NL ("new[]"), 1 },
1439 { "ne", NL ("!="), 2 },
1440 { "ng", NL ("-"), 1 },
1441 { "nt", NL ("!"), 1 },
1442 { "nw", NL ("new"), 1 },
1443 { "oR", NL ("|="), 2 },
1444 { "oo", NL ("||"), 2 },
1445 { "or", NL ("|"), 2 },
1446 { "pL", NL ("+="), 2 },
1447 { "pl", NL ("+"), 2 },
1448 { "pm", NL ("->*"), 2 },
1449 { "pp", NL ("++"), 1 },
1450 { "ps", NL ("+"), 1 },
1451 { "pt", NL ("->"), 2 },
1452 { "qu", NL ("?"), 3 },
1453 { "rM", NL ("%="), 2 },
1454 { "rS", NL (">>="), 2 },
1455 { "rm", NL ("%"), 2 },
1456 { "rs", NL (">>"), 2 },
1457 { "st", NL ("sizeof "), 1 },
1458 { "sz", NL ("sizeof "), 1 },
1459 { NULL, NULL, 0, 0 }
1462 static struct demangle_component *
1463 d_operator_name (di)
1464 struct d_info *di;
1466 char c1;
1467 char c2;
1469 c1 = d_next_char (di);
1470 c2 = d_next_char (di);
1471 if (c1 == 'v' && IS_DIGIT (c2))
1472 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1473 else if (c1 == 'c' && c2 == 'v')
1474 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1475 cplus_demangle_type (di), NULL);
1476 else
1478 /* LOW is the inclusive lower bound. */
1479 int low = 0;
1480 /* HIGH is the exclusive upper bound. We subtract one to ignore
1481 the sentinel at the end of the array. */
1482 int high = ((sizeof (cplus_demangle_operators)
1483 / sizeof (cplus_demangle_operators[0]))
1484 - 1);
1486 while (1)
1488 int i;
1489 const struct demangle_operator_info *p;
1491 i = low + (high - low) / 2;
1492 p = cplus_demangle_operators + i;
1494 if (c1 == p->code[0] && c2 == p->code[1])
1495 return d_make_operator (di, p);
1497 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1498 high = i;
1499 else
1500 low = i + 1;
1501 if (low == high)
1502 return NULL;
1507 /* <special-name> ::= TV <type>
1508 ::= TT <type>
1509 ::= TI <type>
1510 ::= TS <type>
1511 ::= GV <(object) name>
1512 ::= T <call-offset> <(base) encoding>
1513 ::= Tc <call-offset> <call-offset> <(base) encoding>
1514 Also g++ extensions:
1515 ::= TC <type> <(offset) number> _ <(base) type>
1516 ::= TF <type>
1517 ::= TJ <type>
1518 ::= GR <name>
1521 static struct demangle_component *
1522 d_special_name (di)
1523 struct d_info *di;
1525 char c;
1527 di->expansion += 20;
1528 c = d_next_char (di);
1529 if (c == 'T')
1531 switch (d_next_char (di))
1533 case 'V':
1534 di->expansion -= 5;
1535 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1536 cplus_demangle_type (di), NULL);
1537 case 'T':
1538 di->expansion -= 10;
1539 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1540 cplus_demangle_type (di), NULL);
1541 case 'I':
1542 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1543 cplus_demangle_type (di), NULL);
1544 case 'S':
1545 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1546 cplus_demangle_type (di), NULL);
1548 case 'h':
1549 if (! d_call_offset (di, 'h'))
1550 return NULL;
1551 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1552 d_encoding (di, 0), NULL);
1554 case 'v':
1555 if (! d_call_offset (di, 'v'))
1556 return NULL;
1557 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1558 d_encoding (di, 0), NULL);
1560 case 'c':
1561 if (! d_call_offset (di, '\0'))
1562 return NULL;
1563 if (! d_call_offset (di, '\0'))
1564 return NULL;
1565 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1566 d_encoding (di, 0), NULL);
1568 case 'C':
1570 struct demangle_component *derived_type;
1571 long offset;
1572 struct demangle_component *base_type;
1574 derived_type = cplus_demangle_type (di);
1575 offset = d_number (di);
1576 if (offset < 0)
1577 return NULL;
1578 if (d_next_char (di) != '_')
1579 return NULL;
1580 base_type = cplus_demangle_type (di);
1581 /* We don't display the offset. FIXME: We should display
1582 it in verbose mode. */
1583 di->expansion += 5;
1584 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1585 base_type, derived_type);
1588 case 'F':
1589 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1590 cplus_demangle_type (di), NULL);
1591 case 'J':
1592 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1593 cplus_demangle_type (di), NULL);
1595 default:
1596 return NULL;
1599 else if (c == 'G')
1601 switch (d_next_char (di))
1603 case 'V':
1604 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1606 case 'R':
1607 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1608 NULL);
1610 default:
1611 return NULL;
1614 else
1615 return NULL;
1618 /* <call-offset> ::= h <nv-offset> _
1619 ::= v <v-offset> _
1621 <nv-offset> ::= <(offset) number>
1623 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1625 The C parameter, if not '\0', is a character we just read which is
1626 the start of the <call-offset>.
1628 We don't display the offset information anywhere. FIXME: We should
1629 display it in verbose mode. */
1631 static int
1632 d_call_offset (di, c)
1633 struct d_info *di;
1634 int c;
1636 if (c == '\0')
1637 c = d_next_char (di);
1639 if (c == 'h')
1640 d_number (di);
1641 else if (c == 'v')
1643 d_number (di);
1644 if (d_next_char (di) != '_')
1645 return 0;
1646 d_number (di);
1648 else
1649 return 0;
1651 if (d_next_char (di) != '_')
1652 return 0;
1654 return 1;
1657 /* <ctor-dtor-name> ::= C1
1658 ::= C2
1659 ::= C3
1660 ::= D0
1661 ::= D1
1662 ::= D2
1665 static struct demangle_component *
1666 d_ctor_dtor_name (di)
1667 struct d_info *di;
1669 if (di->last_name != NULL)
1671 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1672 di->expansion += di->last_name->u.s_name.len;
1673 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1674 di->expansion += di->last_name->u.s_string.len;
1676 switch (d_next_char (di))
1678 case 'C':
1680 enum gnu_v3_ctor_kinds kind;
1682 switch (d_next_char (di))
1684 case '1':
1685 kind = gnu_v3_complete_object_ctor;
1686 break;
1687 case '2':
1688 kind = gnu_v3_base_object_ctor;
1689 break;
1690 case '3':
1691 kind = gnu_v3_complete_object_allocating_ctor;
1692 break;
1693 default:
1694 return NULL;
1696 return d_make_ctor (di, kind, di->last_name);
1699 case 'D':
1701 enum gnu_v3_dtor_kinds kind;
1703 switch (d_next_char (di))
1705 case '0':
1706 kind = gnu_v3_deleting_dtor;
1707 break;
1708 case '1':
1709 kind = gnu_v3_complete_object_dtor;
1710 break;
1711 case '2':
1712 kind = gnu_v3_base_object_dtor;
1713 break;
1714 default:
1715 return NULL;
1717 return d_make_dtor (di, kind, di->last_name);
1720 default:
1721 return NULL;
1725 /* <type> ::= <builtin-type>
1726 ::= <function-type>
1727 ::= <class-enum-type>
1728 ::= <array-type>
1729 ::= <pointer-to-member-type>
1730 ::= <template-param>
1731 ::= <template-template-param> <template-args>
1732 ::= <substitution>
1733 ::= <CV-qualifiers> <type>
1734 ::= P <type>
1735 ::= R <type>
1736 ::= C <type>
1737 ::= G <type>
1738 ::= U <source-name> <type>
1740 <builtin-type> ::= various one letter codes
1741 ::= u <source-name>
1744 CP_STATIC_IF_GLIBCPP_V3
1745 const struct demangle_builtin_type_info
1746 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1748 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1749 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1750 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1751 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1752 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1753 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1754 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1755 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1756 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1757 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1758 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1759 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1760 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1761 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1762 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1763 D_PRINT_DEFAULT },
1764 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1765 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1766 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1767 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1768 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1769 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1770 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1771 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1772 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1773 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1774 D_PRINT_UNSIGNED_LONG_LONG },
1775 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1778 CP_STATIC_IF_GLIBCPP_V3
1779 struct demangle_component *
1780 cplus_demangle_type (di)
1781 struct d_info *di;
1783 char peek;
1784 struct demangle_component *ret;
1785 int can_subst;
1787 /* The ABI specifies that when CV-qualifiers are used, the base type
1788 is substitutable, and the fully qualified type is substitutable,
1789 but the base type with a strict subset of the CV-qualifiers is
1790 not substitutable. The natural recursive implementation of the
1791 CV-qualifiers would cause subsets to be substitutable, so instead
1792 we pull them all off now.
1794 FIXME: The ABI says that order-insensitive vendor qualifiers
1795 should be handled in the same way, but we have no way to tell
1796 which vendor qualifiers are order-insensitive and which are
1797 order-sensitive. So we just assume that they are all
1798 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1799 __vector, and it treats it as order-sensitive when mangling
1800 names. */
1802 peek = d_peek_char (di);
1803 if (peek == 'r' || peek == 'V' || peek == 'K')
1805 struct demangle_component **pret;
1807 pret = d_cv_qualifiers (di, &ret, 0);
1808 if (pret == NULL)
1809 return NULL;
1810 *pret = cplus_demangle_type (di);
1811 if (! d_add_substitution (di, ret))
1812 return NULL;
1813 return ret;
1816 can_subst = 1;
1818 switch (peek)
1820 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1821 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1822 case 'o': case 's': case 't':
1823 case 'v': case 'w': case 'x': case 'y': case 'z':
1824 ret = d_make_builtin_type (di,
1825 &cplus_demangle_builtin_types[peek - 'a']);
1826 di->expansion += ret->u.s_builtin.type->len;
1827 can_subst = 0;
1828 d_advance (di, 1);
1829 break;
1831 case 'u':
1832 d_advance (di, 1);
1833 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1834 d_source_name (di), NULL);
1835 break;
1837 case 'F':
1838 ret = d_function_type (di);
1839 break;
1841 case '0': case '1': case '2': case '3': case '4':
1842 case '5': case '6': case '7': case '8': case '9':
1843 case 'N':
1844 case 'Z':
1845 ret = d_class_enum_type (di);
1846 break;
1848 case 'A':
1849 ret = d_array_type (di);
1850 break;
1852 case 'M':
1853 ret = d_pointer_to_member_type (di);
1854 break;
1856 case 'T':
1857 ret = d_template_param (di);
1858 if (d_peek_char (di) == 'I')
1860 /* This is <template-template-param> <template-args>. The
1861 <template-template-param> part is a substitution
1862 candidate. */
1863 if (! d_add_substitution (di, ret))
1864 return NULL;
1865 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1866 d_template_args (di));
1868 break;
1870 case 'S':
1871 /* If this is a special substitution, then it is the start of
1872 <class-enum-type>. */
1874 char peek_next;
1876 peek_next = d_peek_next_char (di);
1877 if (IS_DIGIT (peek_next)
1878 || peek_next == '_'
1879 || IS_UPPER (peek_next))
1881 ret = d_substitution (di, 0);
1882 /* The substituted name may have been a template name and
1883 may be followed by tepmlate args. */
1884 if (d_peek_char (di) == 'I')
1885 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1886 d_template_args (di));
1887 else
1888 can_subst = 0;
1890 else
1892 ret = d_class_enum_type (di);
1893 /* If the substitution was a complete type, then it is not
1894 a new substitution candidate. However, if the
1895 substitution was followed by template arguments, then
1896 the whole thing is a substitution candidate. */
1897 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1898 can_subst = 0;
1901 break;
1903 case 'P':
1904 d_advance (di, 1);
1905 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1906 cplus_demangle_type (di), NULL);
1907 break;
1909 case 'R':
1910 d_advance (di, 1);
1911 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1912 cplus_demangle_type (di), NULL);
1913 break;
1915 case 'C':
1916 d_advance (di, 1);
1917 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1918 cplus_demangle_type (di), NULL);
1919 break;
1921 case 'G':
1922 d_advance (di, 1);
1923 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1924 cplus_demangle_type (di), NULL);
1925 break;
1927 case 'U':
1928 d_advance (di, 1);
1929 ret = d_source_name (di);
1930 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1931 cplus_demangle_type (di), ret);
1932 break;
1934 default:
1935 return NULL;
1938 if (can_subst)
1940 if (! d_add_substitution (di, ret))
1941 return NULL;
1944 return ret;
1947 /* <CV-qualifiers> ::= [r] [V] [K] */
1949 static struct demangle_component **
1950 d_cv_qualifiers (di, pret, member_fn)
1951 struct d_info *di;
1952 struct demangle_component **pret;
1953 int member_fn;
1955 char peek;
1957 peek = d_peek_char (di);
1958 while (peek == 'r' || peek == 'V' || peek == 'K')
1960 enum demangle_component_type t;
1962 d_advance (di, 1);
1963 if (peek == 'r')
1965 t = (member_fn
1966 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1967 : DEMANGLE_COMPONENT_RESTRICT);
1968 di->expansion += sizeof "restrict";
1970 else if (peek == 'V')
1972 t = (member_fn
1973 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1974 : DEMANGLE_COMPONENT_VOLATILE);
1975 di->expansion += sizeof "volatile";
1977 else
1979 t = (member_fn
1980 ? DEMANGLE_COMPONENT_CONST_THIS
1981 : DEMANGLE_COMPONENT_CONST);
1982 di->expansion += sizeof "const";
1985 *pret = d_make_comp (di, t, NULL, NULL);
1986 if (*pret == NULL)
1987 return NULL;
1988 pret = &d_left (*pret);
1990 peek = d_peek_char (di);
1993 return pret;
1996 /* <function-type> ::= F [Y] <bare-function-type> E */
1998 static struct demangle_component *
1999 d_function_type (di)
2000 struct d_info *di;
2002 struct demangle_component *ret;
2004 if (d_next_char (di) != 'F')
2005 return NULL;
2006 if (d_peek_char (di) == 'Y')
2008 /* Function has C linkage. We don't print this information.
2009 FIXME: We should print it in verbose mode. */
2010 d_advance (di, 1);
2012 ret = d_bare_function_type (di, 1);
2013 if (d_next_char (di) != 'E')
2014 return NULL;
2015 return ret;
2018 /* <bare-function-type> ::= <type>+ */
2020 static struct demangle_component *
2021 d_bare_function_type (di, has_return_type)
2022 struct d_info *di;
2023 int has_return_type;
2025 struct demangle_component *return_type;
2026 struct demangle_component *tl;
2027 struct demangle_component **ptl;
2029 return_type = NULL;
2030 tl = NULL;
2031 ptl = &tl;
2032 while (1)
2034 char peek;
2035 struct demangle_component *type;
2037 peek = d_peek_char (di);
2038 if (peek == '\0' || peek == 'E')
2039 break;
2040 type = cplus_demangle_type (di);
2041 if (type == NULL)
2042 return NULL;
2043 if (has_return_type)
2045 return_type = type;
2046 has_return_type = 0;
2048 else
2050 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2051 if (*ptl == NULL)
2052 return NULL;
2053 ptl = &d_right (*ptl);
2057 /* There should be at least one parameter type besides the optional
2058 return type. A function which takes no arguments will have a
2059 single parameter type void. */
2060 if (tl == NULL)
2061 return NULL;
2063 /* If we have a single parameter type void, omit it. */
2064 if (d_right (tl) == NULL
2065 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2066 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2068 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2069 tl = NULL;
2072 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2075 /* <class-enum-type> ::= <name> */
2077 static struct demangle_component *
2078 d_class_enum_type (di)
2079 struct d_info *di;
2081 return d_name (di);
2084 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2085 ::= A [<(dimension) expression>] _ <(element) type>
2088 static struct demangle_component *
2089 d_array_type (di)
2090 struct d_info *di;
2092 char peek;
2093 struct demangle_component *dim;
2095 if (d_next_char (di) != 'A')
2096 return NULL;
2098 peek = d_peek_char (di);
2099 if (peek == '_')
2100 dim = NULL;
2101 else if (IS_DIGIT (peek))
2103 const char *s;
2105 s = d_str (di);
2108 d_advance (di, 1);
2109 peek = d_peek_char (di);
2111 while (IS_DIGIT (peek));
2112 dim = d_make_name (di, s, d_str (di) - s);
2113 if (dim == NULL)
2114 return NULL;
2116 else
2118 dim = d_expression (di);
2119 if (dim == NULL)
2120 return NULL;
2123 if (d_next_char (di) != '_')
2124 return NULL;
2126 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2127 cplus_demangle_type (di));
2130 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2132 static struct demangle_component *
2133 d_pointer_to_member_type (di)
2134 struct d_info *di;
2136 struct demangle_component *cl;
2137 struct demangle_component *mem;
2138 struct demangle_component **pmem;
2140 if (d_next_char (di) != 'M')
2141 return NULL;
2143 cl = cplus_demangle_type (di);
2145 /* The ABI specifies that any type can be a substitution source, and
2146 that M is followed by two types, and that when a CV-qualified
2147 type is seen both the base type and the CV-qualified types are
2148 substitution sources. The ABI also specifies that for a pointer
2149 to a CV-qualified member function, the qualifiers are attached to
2150 the second type. Given the grammar, a plain reading of the ABI
2151 suggests that both the CV-qualified member function and the
2152 non-qualified member function are substitution sources. However,
2153 g++ does not work that way. g++ treats only the CV-qualified
2154 member function as a substitution source. FIXME. So to work
2155 with g++, we need to pull off the CV-qualifiers here, in order to
2156 avoid calling add_substitution() in cplus_demangle_type(). */
2158 pmem = d_cv_qualifiers (di, &mem, 1);
2159 if (pmem == NULL)
2160 return NULL;
2161 *pmem = cplus_demangle_type (di);
2163 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2166 /* <template-param> ::= T_
2167 ::= T <(parameter-2 non-negative) number> _
2170 static struct demangle_component *
2171 d_template_param (di)
2172 struct d_info *di;
2174 long param;
2176 if (d_next_char (di) != 'T')
2177 return NULL;
2179 if (d_peek_char (di) == '_')
2180 param = 0;
2181 else
2183 param = d_number (di);
2184 if (param < 0)
2185 return NULL;
2186 param += 1;
2189 if (d_next_char (di) != '_')
2190 return NULL;
2192 ++di->did_subs;
2194 return d_make_template_param (di, param);
2197 /* <template-args> ::= I <template-arg>+ E */
2199 static struct demangle_component *
2200 d_template_args (di)
2201 struct d_info *di;
2203 struct demangle_component *hold_last_name;
2204 struct demangle_component *al;
2205 struct demangle_component **pal;
2207 /* Preserve the last name we saw--don't let the template arguments
2208 clobber it, as that would give us the wrong name for a subsequent
2209 constructor or destructor. */
2210 hold_last_name = di->last_name;
2212 if (d_next_char (di) != 'I')
2213 return NULL;
2215 al = NULL;
2216 pal = &al;
2217 while (1)
2219 struct demangle_component *a;
2221 a = d_template_arg (di);
2222 if (a == NULL)
2223 return NULL;
2225 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2226 if (*pal == NULL)
2227 return NULL;
2228 pal = &d_right (*pal);
2230 if (d_peek_char (di) == 'E')
2232 d_advance (di, 1);
2233 break;
2237 di->last_name = hold_last_name;
2239 return al;
2242 /* <template-arg> ::= <type>
2243 ::= X <expression> E
2244 ::= <expr-primary>
2247 static struct demangle_component *
2248 d_template_arg (di)
2249 struct d_info *di;
2251 struct demangle_component *ret;
2253 switch (d_peek_char (di))
2255 case 'X':
2256 d_advance (di, 1);
2257 ret = d_expression (di);
2258 if (d_next_char (di) != 'E')
2259 return NULL;
2260 return ret;
2262 case 'L':
2263 return d_expr_primary (di);
2265 default:
2266 return cplus_demangle_type (di);
2270 /* <expression> ::= <(unary) operator-name> <expression>
2271 ::= <(binary) operator-name> <expression> <expression>
2272 ::= <(trinary) operator-name> <expression> <expression> <expression>
2273 ::= st <type>
2274 ::= <template-param>
2275 ::= sr <type> <unqualified-name>
2276 ::= sr <type> <unqualified-name> <template-args>
2277 ::= <expr-primary>
2280 static struct demangle_component *
2281 d_expression (di)
2282 struct d_info *di;
2284 char peek;
2286 peek = d_peek_char (di);
2287 if (peek == 'L')
2288 return d_expr_primary (di);
2289 else if (peek == 'T')
2290 return d_template_param (di);
2291 else if (peek == 's' && d_peek_next_char (di) == 'r')
2293 struct demangle_component *type;
2294 struct demangle_component *name;
2296 d_advance (di, 2);
2297 type = cplus_demangle_type (di);
2298 name = d_unqualified_name (di);
2299 if (d_peek_char (di) != 'I')
2300 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2301 else
2302 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2303 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2304 d_template_args (di)));
2306 else
2308 struct demangle_component *op;
2309 int args;
2311 op = d_operator_name (di);
2312 if (op == NULL)
2313 return NULL;
2315 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2316 di->expansion += op->u.s_operator.op->len - 2;
2318 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2319 && strcmp (op->u.s_operator.op->code, "st") == 0)
2320 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2321 cplus_demangle_type (di));
2323 switch (op->type)
2325 default:
2326 return NULL;
2327 case DEMANGLE_COMPONENT_OPERATOR:
2328 args = op->u.s_operator.op->args;
2329 break;
2330 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2331 args = op->u.s_extended_operator.args;
2332 break;
2333 case DEMANGLE_COMPONENT_CAST:
2334 args = 1;
2335 break;
2338 switch (args)
2340 case 1:
2341 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2342 d_expression (di));
2343 case 2:
2345 struct demangle_component *left;
2347 left = d_expression (di);
2348 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2349 d_make_comp (di,
2350 DEMANGLE_COMPONENT_BINARY_ARGS,
2351 left,
2352 d_expression (di)));
2354 case 3:
2356 struct demangle_component *first;
2357 struct demangle_component *second;
2359 first = d_expression (di);
2360 second = d_expression (di);
2361 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2362 d_make_comp (di,
2363 DEMANGLE_COMPONENT_TRINARY_ARG1,
2364 first,
2365 d_make_comp (di,
2366 DEMANGLE_COMPONENT_TRINARY_ARG2,
2367 second,
2368 d_expression (di))));
2370 default:
2371 return NULL;
2376 /* <expr-primary> ::= L <type> <(value) number> E
2377 ::= L <type> <(value) float> E
2378 ::= L <mangled-name> E
2381 static struct demangle_component *
2382 d_expr_primary (di)
2383 struct d_info *di;
2385 struct demangle_component *ret;
2387 if (d_next_char (di) != 'L')
2388 return NULL;
2389 if (d_peek_char (di) == '_')
2390 ret = cplus_demangle_mangled_name (di, 0);
2391 else
2393 struct demangle_component *type;
2394 enum demangle_component_type t;
2395 const char *s;
2397 type = cplus_demangle_type (di);
2398 if (type == NULL)
2399 return NULL;
2401 /* If we have a type we know how to print, we aren't going to
2402 print the type name itself. */
2403 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2404 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2405 di->expansion -= type->u.s_builtin.type->len;
2407 /* Rather than try to interpret the literal value, we just
2408 collect it as a string. Note that it's possible to have a
2409 floating point literal here. The ABI specifies that the
2410 format of such literals is machine independent. That's fine,
2411 but what's not fine is that versions of g++ up to 3.2 with
2412 -fabi-version=1 used upper case letters in the hex constant,
2413 and dumped out gcc's internal representation. That makes it
2414 hard to tell where the constant ends, and hard to dump the
2415 constant in any readable form anyhow. We don't attempt to
2416 handle these cases. */
2418 t = DEMANGLE_COMPONENT_LITERAL;
2419 if (d_peek_char (di) == 'n')
2421 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2422 d_advance (di, 1);
2424 s = d_str (di);
2425 while (d_peek_char (di) != 'E')
2426 d_advance (di, 1);
2427 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2429 if (d_next_char (di) != 'E')
2430 return NULL;
2431 return ret;
2434 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2435 ::= Z <(function) encoding> E s [<discriminator>]
2438 static struct demangle_component *
2439 d_local_name (di)
2440 struct d_info *di;
2442 struct demangle_component *function;
2444 if (d_next_char (di) != 'Z')
2445 return NULL;
2447 function = d_encoding (di, 0);
2449 if (d_next_char (di) != 'E')
2450 return NULL;
2452 if (d_peek_char (di) == 's')
2454 d_advance (di, 1);
2455 if (! d_discriminator (di))
2456 return NULL;
2457 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2458 d_make_name (di, "string literal",
2459 sizeof "string literal" - 1));
2461 else
2463 struct demangle_component *name;
2465 name = d_name (di);
2466 if (! d_discriminator (di))
2467 return NULL;
2468 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2472 /* <discriminator> ::= _ <(non-negative) number>
2474 We demangle the discriminator, but we don't print it out. FIXME:
2475 We should print it out in verbose mode. */
2477 static int
2478 d_discriminator (di)
2479 struct d_info *di;
2481 long discrim;
2483 if (d_peek_char (di) != '_')
2484 return 1;
2485 d_advance (di, 1);
2486 discrim = d_number (di);
2487 if (discrim < 0)
2488 return 0;
2489 return 1;
2492 /* Add a new substitution. */
2494 static int
2495 d_add_substitution (di, dc)
2496 struct d_info *di;
2497 struct demangle_component *dc;
2499 if (dc == NULL)
2500 return 0;
2501 if (di->next_sub >= di->num_subs)
2502 return 0;
2503 di->subs[di->next_sub] = dc;
2504 ++di->next_sub;
2505 return 1;
2508 /* <substitution> ::= S <seq-id> _
2509 ::= S_
2510 ::= St
2511 ::= Sa
2512 ::= Sb
2513 ::= Ss
2514 ::= Si
2515 ::= So
2516 ::= Sd
2518 If PREFIX is non-zero, then this type is being used as a prefix in
2519 a qualified name. In this case, for the standard substitutions, we
2520 need to check whether we are being used as a prefix for a
2521 constructor or destructor, and return a full template name.
2522 Otherwise we will get something like std::iostream::~iostream()
2523 which does not correspond particularly well to any function which
2524 actually appears in the source.
2527 static const struct d_standard_sub_info standard_subs[] =
2529 { 't', NL ("std"),
2530 NL ("std"),
2531 NULL, 0 },
2532 { 'a', NL ("std::allocator"),
2533 NL ("std::allocator"),
2534 NL ("allocator") },
2535 { 'b', NL ("std::basic_string"),
2536 NL ("std::basic_string"),
2537 NL ("basic_string") },
2538 { 's', NL ("std::string"),
2539 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2540 NL ("basic_string") },
2541 { 'i', NL ("std::istream"),
2542 NL ("std::basic_istream<char, std::char_traits<char> >"),
2543 NL ("basic_istream") },
2544 { 'o', NL ("std::ostream"),
2545 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2546 NL ("basic_ostream") },
2547 { 'd', NL ("std::iostream"),
2548 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2549 NL ("basic_iostream") }
2552 static struct demangle_component *
2553 d_substitution (di, prefix)
2554 struct d_info *di;
2555 int prefix;
2557 char c;
2559 if (d_next_char (di) != 'S')
2560 return NULL;
2562 c = d_next_char (di);
2563 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2565 int id;
2567 id = 0;
2568 if (c != '_')
2572 if (IS_DIGIT (c))
2573 id = id * 36 + c - '0';
2574 else if (IS_UPPER (c))
2575 id = id * 36 + c - 'A' + 10;
2576 else
2577 return NULL;
2578 c = d_next_char (di);
2580 while (c != '_');
2582 ++id;
2585 if (id >= di->next_sub)
2586 return NULL;
2588 ++di->did_subs;
2590 return di->subs[id];
2592 else
2594 int verbose;
2595 const struct d_standard_sub_info *p;
2596 const struct d_standard_sub_info *pend;
2598 verbose = (di->options & DMGL_VERBOSE) != 0;
2599 if (! verbose && prefix)
2601 char peek;
2603 peek = d_peek_char (di);
2604 if (peek == 'C' || peek == 'D')
2605 verbose = 1;
2608 pend = (&standard_subs[0]
2609 + sizeof standard_subs / sizeof standard_subs[0]);
2610 for (p = &standard_subs[0]; p < pend; ++p)
2612 if (c == p->code)
2614 const char *s;
2615 int len;
2617 if (p->set_last_name != NULL)
2618 di->last_name = d_make_sub (di, p->set_last_name,
2619 p->set_last_name_len);
2620 if (verbose)
2622 s = p->full_expansion;
2623 len = p->full_len;
2625 else
2627 s = p->simple_expansion;
2628 len = p->simple_len;
2630 di->expansion += len;
2631 return d_make_sub (di, s, len);
2635 return NULL;
2639 /* Resize the print buffer. */
2641 static void
2642 d_print_resize (dpi, add)
2643 struct d_print_info *dpi;
2644 size_t add;
2646 size_t need;
2648 if (dpi->buf == NULL)
2649 return;
2650 need = dpi->len + add;
2651 while (need > dpi->alc)
2653 size_t newalc;
2654 char *newbuf;
2656 newalc = dpi->alc * 2;
2657 newbuf = realloc (dpi->buf, newalc);
2658 if (newbuf == NULL)
2660 free (dpi->buf);
2661 dpi->buf = NULL;
2662 dpi->allocation_failure = 1;
2663 return;
2665 dpi->buf = newbuf;
2666 dpi->alc = newalc;
2670 /* Append a character to the print buffer. */
2672 static void
2673 d_print_append_char (dpi, c)
2674 struct d_print_info *dpi;
2675 int c;
2677 if (dpi->buf != NULL)
2679 if (dpi->len >= dpi->alc)
2681 d_print_resize (dpi, 1);
2682 if (dpi->buf == NULL)
2683 return;
2686 dpi->buf[dpi->len] = c;
2687 ++dpi->len;
2691 /* Append a buffer to the print buffer. */
2693 static void
2694 d_print_append_buffer (dpi, s, l)
2695 struct d_print_info *dpi;
2696 const char *s;
2697 size_t l;
2699 if (dpi->buf != NULL)
2701 if (dpi->len + l > dpi->alc)
2703 d_print_resize (dpi, l);
2704 if (dpi->buf == NULL)
2705 return;
2708 memcpy (dpi->buf + dpi->len, s, l);
2709 dpi->len += l;
2713 /* Indicate that an error occurred during printing. */
2715 static void
2716 d_print_error (dpi)
2717 struct d_print_info *dpi;
2719 free (dpi->buf);
2720 dpi->buf = NULL;
2723 /* Turn components into a human readable string. OPTIONS is the
2724 options bits passed to the demangler. DC is the tree to print.
2725 ESTIMATE is a guess at the length of the result. This returns a
2726 string allocated by malloc, or NULL on error. On success, this
2727 sets *PALC to the size of the allocated buffer. On failure, this
2728 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2729 failure. */
2731 CP_STATIC_IF_GLIBCPP_V3
2732 char *
2733 cplus_demangle_print (options, dc, estimate, palc)
2734 int options;
2735 const struct demangle_component *dc;
2736 int estimate;
2737 size_t *palc;
2739 struct d_print_info dpi;
2741 dpi.options = options;
2743 dpi.alc = estimate + 1;
2744 dpi.buf = malloc (dpi.alc);
2745 if (dpi.buf == NULL)
2747 *palc = 1;
2748 return NULL;
2751 dpi.len = 0;
2752 dpi.templates = NULL;
2753 dpi.modifiers = NULL;
2755 dpi.allocation_failure = 0;
2757 d_print_comp (&dpi, dc);
2759 d_append_char (&dpi, '\0');
2761 if (dpi.buf != NULL)
2762 *palc = dpi.alc;
2763 else
2764 *palc = dpi.allocation_failure;
2766 return dpi.buf;
2769 /* Subroutine to handle components. */
2771 static void
2772 d_print_comp (dpi, dc)
2773 struct d_print_info *dpi;
2774 const struct demangle_component *dc;
2776 if (dc == NULL)
2778 d_print_error (dpi);
2779 return;
2781 if (d_print_saw_error (dpi))
2782 return;
2784 switch (dc->type)
2786 case DEMANGLE_COMPONENT_NAME:
2787 if ((dpi->options & DMGL_JAVA) == 0)
2788 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2789 else
2790 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2791 return;
2793 case DEMANGLE_COMPONENT_QUAL_NAME:
2794 case DEMANGLE_COMPONENT_LOCAL_NAME:
2795 d_print_comp (dpi, d_left (dc));
2796 if ((dpi->options & DMGL_JAVA) == 0)
2797 d_append_string_constant (dpi, "::");
2798 else
2799 d_append_char (dpi, '.');
2800 d_print_comp (dpi, d_right (dc));
2801 return;
2803 case DEMANGLE_COMPONENT_TYPED_NAME:
2805 struct d_print_mod *hold_modifiers;
2806 struct demangle_component *typed_name;
2807 struct d_print_mod adpm[4];
2808 unsigned int i;
2809 struct d_print_template dpt;
2811 /* Pass the name down to the type so that it can be printed in
2812 the right place for the type. We also have to pass down
2813 any CV-qualifiers, which apply to the this parameter. */
2814 hold_modifiers = dpi->modifiers;
2815 i = 0;
2816 typed_name = d_left (dc);
2817 while (typed_name != NULL)
2819 if (i >= sizeof adpm / sizeof adpm[0])
2821 d_print_error (dpi);
2822 return;
2825 adpm[i].next = dpi->modifiers;
2826 dpi->modifiers = &adpm[i];
2827 adpm[i].mod = typed_name;
2828 adpm[i].printed = 0;
2829 adpm[i].templates = dpi->templates;
2830 ++i;
2832 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2833 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2834 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2835 break;
2837 typed_name = d_left (typed_name);
2840 /* If typed_name is a template, then it applies to the
2841 function type as well. */
2842 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2844 dpt.next = dpi->templates;
2845 dpi->templates = &dpt;
2846 dpt.template = typed_name;
2849 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2850 there may be CV-qualifiers on its right argument which
2851 really apply here; this happens when parsing a class which
2852 is local to a function. */
2853 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2855 struct demangle_component *local_name;
2857 local_name = d_right (typed_name);
2858 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2859 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2860 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2862 if (i >= sizeof adpm / sizeof adpm[0])
2864 d_print_error (dpi);
2865 return;
2868 adpm[i] = adpm[i - 1];
2869 adpm[i].next = &adpm[i - 1];
2870 dpi->modifiers = &adpm[i];
2872 adpm[i - 1].mod = local_name;
2873 adpm[i - 1].printed = 0;
2874 adpm[i - 1].templates = dpi->templates;
2875 ++i;
2877 local_name = d_left (local_name);
2881 d_print_comp (dpi, d_right (dc));
2883 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2884 dpi->templates = dpt.next;
2886 /* If the modifiers didn't get printed by the type, print them
2887 now. */
2888 while (i > 0)
2890 --i;
2891 if (! adpm[i].printed)
2893 d_append_char (dpi, ' ');
2894 d_print_mod (dpi, adpm[i].mod);
2898 dpi->modifiers = hold_modifiers;
2900 return;
2903 case DEMANGLE_COMPONENT_TEMPLATE:
2905 struct d_print_mod *hold_dpm;
2907 /* Don't push modifiers into a template definition. Doing so
2908 could give the wrong definition for a template argument.
2909 Instead, treat the template essentially as a name. */
2911 hold_dpm = dpi->modifiers;
2912 dpi->modifiers = NULL;
2914 d_print_comp (dpi, d_left (dc));
2915 if (d_last_char (dpi) == '<')
2916 d_append_char (dpi, ' ');
2917 d_append_char (dpi, '<');
2918 d_print_comp (dpi, d_right (dc));
2919 /* Avoid generating two consecutive '>' characters, to avoid
2920 the C++ syntactic ambiguity. */
2921 if (d_last_char (dpi) == '>')
2922 d_append_char (dpi, ' ');
2923 d_append_char (dpi, '>');
2925 dpi->modifiers = hold_dpm;
2927 return;
2930 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
2932 long i;
2933 struct demangle_component *a;
2934 struct d_print_template *hold_dpt;
2936 if (dpi->templates == NULL)
2938 d_print_error (dpi);
2939 return;
2941 i = dc->u.s_number.number;
2942 for (a = d_right (dpi->templates->template);
2943 a != NULL;
2944 a = d_right (a))
2946 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
2948 d_print_error (dpi);
2949 return;
2951 if (i <= 0)
2952 break;
2953 --i;
2955 if (i != 0 || a == NULL)
2957 d_print_error (dpi);
2958 return;
2961 /* While processing this parameter, we need to pop the list of
2962 templates. This is because the template parameter may
2963 itself be a reference to a parameter of an outer
2964 template. */
2966 hold_dpt = dpi->templates;
2967 dpi->templates = hold_dpt->next;
2969 d_print_comp (dpi, d_left (a));
2971 dpi->templates = hold_dpt;
2973 return;
2976 case DEMANGLE_COMPONENT_CTOR:
2977 d_print_comp (dpi, dc->u.s_ctor.name);
2978 return;
2980 case DEMANGLE_COMPONENT_DTOR:
2981 d_append_char (dpi, '~');
2982 d_print_comp (dpi, dc->u.s_dtor.name);
2983 return;
2985 case DEMANGLE_COMPONENT_VTABLE:
2986 d_append_string_constant (dpi, "vtable for ");
2987 d_print_comp (dpi, d_left (dc));
2988 return;
2990 case DEMANGLE_COMPONENT_VTT:
2991 d_append_string_constant (dpi, "VTT for ");
2992 d_print_comp (dpi, d_left (dc));
2993 return;
2995 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
2996 d_append_string_constant (dpi, "construction vtable for ");
2997 d_print_comp (dpi, d_left (dc));
2998 d_append_string_constant (dpi, "-in-");
2999 d_print_comp (dpi, d_right (dc));
3000 return;
3002 case DEMANGLE_COMPONENT_TYPEINFO:
3003 d_append_string_constant (dpi, "typeinfo for ");
3004 d_print_comp (dpi, d_left (dc));
3005 return;
3007 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3008 d_append_string_constant (dpi, "typeinfo name for ");
3009 d_print_comp (dpi, d_left (dc));
3010 return;
3012 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3013 d_append_string_constant (dpi, "typeinfo fn for ");
3014 d_print_comp (dpi, d_left (dc));
3015 return;
3017 case DEMANGLE_COMPONENT_THUNK:
3018 d_append_string_constant (dpi, "non-virtual thunk to ");
3019 d_print_comp (dpi, d_left (dc));
3020 return;
3022 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3023 d_append_string_constant (dpi, "virtual thunk to ");
3024 d_print_comp (dpi, d_left (dc));
3025 return;
3027 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3028 d_append_string_constant (dpi, "covariant return thunk to ");
3029 d_print_comp (dpi, d_left (dc));
3030 return;
3032 case DEMANGLE_COMPONENT_JAVA_CLASS:
3033 d_append_string_constant (dpi, "java Class for ");
3034 d_print_comp (dpi, d_left (dc));
3035 return;
3037 case DEMANGLE_COMPONENT_GUARD:
3038 d_append_string_constant (dpi, "guard variable for ");
3039 d_print_comp (dpi, d_left (dc));
3040 return;
3042 case DEMANGLE_COMPONENT_REFTEMP:
3043 d_append_string_constant (dpi, "reference temporary for ");
3044 d_print_comp (dpi, d_left (dc));
3045 return;
3047 case DEMANGLE_COMPONENT_SUB_STD:
3048 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3049 return;
3051 case DEMANGLE_COMPONENT_RESTRICT:
3052 case DEMANGLE_COMPONENT_VOLATILE:
3053 case DEMANGLE_COMPONENT_CONST:
3055 struct d_print_mod *pdpm;
3057 /* When printing arrays, it's possible to have cases where the
3058 same CV-qualifier gets pushed on the stack multiple times.
3059 We only need to print it once. */
3061 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3063 if (! pdpm->printed)
3065 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3066 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3067 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3068 break;
3069 if (pdpm->mod->type == dc->type)
3071 d_print_comp (dpi, d_left (dc));
3072 return;
3077 /* Fall through. */
3078 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3079 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3080 case DEMANGLE_COMPONENT_CONST_THIS:
3081 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3082 case DEMANGLE_COMPONENT_POINTER:
3083 case DEMANGLE_COMPONENT_REFERENCE:
3084 case DEMANGLE_COMPONENT_COMPLEX:
3085 case DEMANGLE_COMPONENT_IMAGINARY:
3087 /* We keep a list of modifiers on the stack. */
3088 struct d_print_mod dpm;
3090 dpm.next = dpi->modifiers;
3091 dpi->modifiers = &dpm;
3092 dpm.mod = dc;
3093 dpm.printed = 0;
3094 dpm.templates = dpi->templates;
3096 d_print_comp (dpi, d_left (dc));
3098 /* If the modifier didn't get printed by the type, print it
3099 now. */
3100 if (! dpm.printed)
3101 d_print_mod (dpi, dc);
3103 dpi->modifiers = dpm.next;
3105 return;
3108 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3109 if ((dpi->options & DMGL_JAVA) == 0)
3110 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3111 dc->u.s_builtin.type->len);
3112 else
3113 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3114 dc->u.s_builtin.type->java_len);
3115 return;
3117 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3118 d_print_comp (dpi, d_left (dc));
3119 return;
3121 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3123 if (d_left (dc) != NULL)
3125 struct d_print_mod dpm;
3127 /* We must pass this type down as a modifier in order to
3128 print it in the right location. */
3130 dpm.next = dpi->modifiers;
3131 dpi->modifiers = &dpm;
3132 dpm.mod = dc;
3133 dpm.printed = 0;
3134 dpm.templates = dpi->templates;
3136 d_print_comp (dpi, d_left (dc));
3138 dpi->modifiers = dpm.next;
3140 if (dpm.printed)
3141 return;
3143 d_append_char (dpi, ' ');
3146 d_print_function_type (dpi, dc, dpi->modifiers);
3148 return;
3151 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3153 struct d_print_mod *hold_modifiers;
3154 struct d_print_mod adpm[4];
3155 unsigned int i;
3156 struct d_print_mod *pdpm;
3158 /* We must pass this type down as a modifier in order to print
3159 multi-dimensional arrays correctly. If the array itself is
3160 CV-qualified, we act as though the element type were
3161 CV-qualified. We do this by copying the modifiers down
3162 rather than fiddling pointers, so that we don't wind up
3163 with a d_print_mod higher on the stack pointing into our
3164 stack frame after we return. */
3166 hold_modifiers = dpi->modifiers;
3168 adpm[0].next = hold_modifiers;
3169 dpi->modifiers = &adpm[0];
3170 adpm[0].mod = dc;
3171 adpm[0].printed = 0;
3172 adpm[0].templates = dpi->templates;
3174 i = 1;
3175 pdpm = hold_modifiers;
3176 while (pdpm != NULL
3177 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3178 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3179 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3181 if (! pdpm->printed)
3183 if (i >= sizeof adpm / sizeof adpm[0])
3185 d_print_error (dpi);
3186 return;
3189 adpm[i] = *pdpm;
3190 adpm[i].next = dpi->modifiers;
3191 dpi->modifiers = &adpm[i];
3192 pdpm->printed = 1;
3193 ++i;
3196 pdpm = pdpm->next;
3199 d_print_comp (dpi, d_right (dc));
3201 dpi->modifiers = hold_modifiers;
3203 if (adpm[0].printed)
3204 return;
3206 while (i > 1)
3208 --i;
3209 d_print_mod (dpi, adpm[i].mod);
3212 d_print_array_type (dpi, dc, dpi->modifiers);
3214 return;
3217 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3219 struct d_print_mod dpm;
3221 dpm.next = dpi->modifiers;
3222 dpi->modifiers = &dpm;
3223 dpm.mod = dc;
3224 dpm.printed = 0;
3225 dpm.templates = dpi->templates;
3227 d_print_comp (dpi, d_right (dc));
3229 /* If the modifier didn't get printed by the type, print it
3230 now. */
3231 if (! dpm.printed)
3233 d_append_char (dpi, ' ');
3234 d_print_comp (dpi, d_left (dc));
3235 d_append_string_constant (dpi, "::*");
3238 dpi->modifiers = dpm.next;
3240 return;
3243 case DEMANGLE_COMPONENT_ARGLIST:
3244 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3245 d_print_comp (dpi, d_left (dc));
3246 if (d_right (dc) != NULL)
3248 d_append_string_constant (dpi, ", ");
3249 d_print_comp (dpi, d_right (dc));
3251 return;
3253 case DEMANGLE_COMPONENT_OPERATOR:
3255 char c;
3257 d_append_string_constant (dpi, "operator");
3258 c = dc->u.s_operator.op->name[0];
3259 if (IS_LOWER (c))
3260 d_append_char (dpi, ' ');
3261 d_append_buffer (dpi, dc->u.s_operator.op->name,
3262 dc->u.s_operator.op->len);
3263 return;
3266 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3267 d_append_string_constant (dpi, "operator ");
3268 d_print_comp (dpi, dc->u.s_extended_operator.name);
3269 return;
3271 case DEMANGLE_COMPONENT_CAST:
3272 d_append_string_constant (dpi, "operator ");
3273 d_print_cast (dpi, dc);
3274 return;
3276 case DEMANGLE_COMPONENT_UNARY:
3277 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3278 d_print_expr_op (dpi, d_left (dc));
3279 else
3281 d_append_char (dpi, '(');
3282 d_print_cast (dpi, d_left (dc));
3283 d_append_char (dpi, ')');
3285 d_append_char (dpi, '(');
3286 d_print_comp (dpi, d_right (dc));
3287 d_append_char (dpi, ')');
3288 return;
3290 case DEMANGLE_COMPONENT_BINARY:
3291 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3293 d_print_error (dpi);
3294 return;
3297 /* We wrap an expression which uses the greater-than operator in
3298 an extra layer of parens so that it does not get confused
3299 with the '>' which ends the template parameters. */
3300 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3301 && d_left (dc)->u.s_operator.op->len == 1
3302 && d_left (dc)->u.s_operator.op->name[0] == '>')
3303 d_append_char (dpi, '(');
3305 d_append_char (dpi, '(');
3306 d_print_comp (dpi, d_left (d_right (dc)));
3307 d_append_string_constant (dpi, ") ");
3308 d_print_expr_op (dpi, d_left (dc));
3309 d_append_string_constant (dpi, " (");
3310 d_print_comp (dpi, d_right (d_right (dc)));
3311 d_append_char (dpi, ')');
3313 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3314 && d_left (dc)->u.s_operator.op->len == 1
3315 && d_left (dc)->u.s_operator.op->name[0] == '>')
3316 d_append_char (dpi, ')');
3318 return;
3320 case DEMANGLE_COMPONENT_BINARY_ARGS:
3321 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3322 d_print_error (dpi);
3323 return;
3325 case DEMANGLE_COMPONENT_TRINARY:
3326 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3327 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3329 d_print_error (dpi);
3330 return;
3332 d_append_char (dpi, '(');
3333 d_print_comp (dpi, d_left (d_right (dc)));
3334 d_append_string_constant (dpi, ") ");
3335 d_print_expr_op (dpi, d_left (dc));
3336 d_append_string_constant (dpi, " (");
3337 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3338 d_append_string_constant (dpi, ") : (");
3339 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3340 d_append_char (dpi, ')');
3341 return;
3343 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3344 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3345 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3346 d_print_error (dpi);
3347 return;
3349 case DEMANGLE_COMPONENT_LITERAL:
3350 case DEMANGLE_COMPONENT_LITERAL_NEG:
3352 enum d_builtin_type_print tp;
3354 /* For some builtin types, produce simpler output. */
3355 tp = D_PRINT_DEFAULT;
3356 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3358 tp = d_left (dc)->u.s_builtin.type->print;
3359 switch (tp)
3361 case D_PRINT_INT:
3362 case D_PRINT_UNSIGNED:
3363 case D_PRINT_LONG:
3364 case D_PRINT_UNSIGNED_LONG:
3365 case D_PRINT_LONG_LONG:
3366 case D_PRINT_UNSIGNED_LONG_LONG:
3367 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3369 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3370 d_append_char (dpi, '-');
3371 d_print_comp (dpi, d_right (dc));
3372 switch (tp)
3374 default:
3375 break;
3376 case D_PRINT_UNSIGNED:
3377 d_append_char (dpi, 'u');
3378 break;
3379 case D_PRINT_LONG:
3380 d_append_char (dpi, 'l');
3381 break;
3382 case D_PRINT_UNSIGNED_LONG:
3383 d_append_string_constant (dpi, "ul");
3384 break;
3385 case D_PRINT_LONG_LONG:
3386 d_append_string_constant (dpi, "ll");
3387 break;
3388 case D_PRINT_UNSIGNED_LONG_LONG:
3389 d_append_string_constant (dpi, "ull");
3390 break;
3392 return;
3394 break;
3396 case D_PRINT_BOOL:
3397 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3398 && d_right (dc)->u.s_name.len == 1
3399 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3401 switch (d_right (dc)->u.s_name.s[0])
3403 case '0':
3404 d_append_string_constant (dpi, "false");
3405 return;
3406 case '1':
3407 d_append_string_constant (dpi, "true");
3408 return;
3409 default:
3410 break;
3413 break;
3415 default:
3416 break;
3420 d_append_char (dpi, '(');
3421 d_print_comp (dpi, d_left (dc));
3422 d_append_char (dpi, ')');
3423 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3424 d_append_char (dpi, '-');
3425 if (tp == D_PRINT_FLOAT)
3426 d_append_char (dpi, '[');
3427 d_print_comp (dpi, d_right (dc));
3428 if (tp == D_PRINT_FLOAT)
3429 d_append_char (dpi, ']');
3431 return;
3433 default:
3434 d_print_error (dpi);
3435 return;
3439 /* Print a Java dentifier. For Java we try to handle encoded extended
3440 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3441 so we don't it for C++. Characters are encoded as
3442 __U<hex-char>+_. */
3444 static void
3445 d_print_java_identifier (dpi, name, len)
3446 struct d_print_info *dpi;
3447 const char *name;
3448 int len;
3450 const char *p;
3451 const char *end;
3453 end = name + len;
3454 for (p = name; p < end; ++p)
3456 if (end - p > 3
3457 && p[0] == '_'
3458 && p[1] == '_'
3459 && p[2] == 'U')
3461 unsigned long c;
3462 const char *q;
3464 c = 0;
3465 for (q = p + 3; q < end; ++q)
3467 int dig;
3469 if (IS_DIGIT (*q))
3470 dig = *q - '0';
3471 else if (*q >= 'A' && *q <= 'F')
3472 dig = *q - 'A' + 10;
3473 else if (*q >= 'a' && *q <= 'f')
3474 dig = *q - 'a' + 10;
3475 else
3476 break;
3478 c = c * 16 + dig;
3480 /* If the Unicode character is larger than 256, we don't try
3481 to deal with it here. FIXME. */
3482 if (q < end && *q == '_' && c < 256)
3484 d_append_char (dpi, c);
3485 p = q;
3486 continue;
3490 d_append_char (dpi, *p);
3494 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3495 qualifiers on this after printing a function. */
3497 static void
3498 d_print_mod_list (dpi, mods, suffix)
3499 struct d_print_info *dpi;
3500 struct d_print_mod *mods;
3501 int suffix;
3503 struct d_print_template *hold_dpt;
3505 if (mods == NULL || d_print_saw_error (dpi))
3506 return;
3508 if (mods->printed
3509 || (! suffix
3510 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3511 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3512 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3514 d_print_mod_list (dpi, mods->next, suffix);
3515 return;
3518 mods->printed = 1;
3520 hold_dpt = dpi->templates;
3521 dpi->templates = mods->templates;
3523 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3525 d_print_function_type (dpi, mods->mod, mods->next);
3526 dpi->templates = hold_dpt;
3527 return;
3529 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3531 d_print_array_type (dpi, mods->mod, mods->next);
3532 dpi->templates = hold_dpt;
3533 return;
3535 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3537 struct d_print_mod *hold_modifiers;
3538 struct demangle_component *dc;
3540 /* When this is on the modifier stack, we have pulled any
3541 qualifiers off the right argument already. Otherwise, we
3542 print it as usual, but don't let the left argument see any
3543 modifiers. */
3545 hold_modifiers = dpi->modifiers;
3546 dpi->modifiers = NULL;
3547 d_print_comp (dpi, d_left (mods->mod));
3548 dpi->modifiers = hold_modifiers;
3550 if ((dpi->options & DMGL_JAVA) == 0)
3551 d_append_string_constant (dpi, "::");
3552 else
3553 d_append_char (dpi, '.');
3555 dc = d_right (mods->mod);
3556 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3557 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3558 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3559 dc = d_left (dc);
3561 d_print_comp (dpi, dc);
3563 dpi->templates = hold_dpt;
3564 return;
3567 d_print_mod (dpi, mods->mod);
3569 dpi->templates = hold_dpt;
3571 d_print_mod_list (dpi, mods->next, suffix);
3574 /* Print a modifier. */
3576 static void
3577 d_print_mod (dpi, mod)
3578 struct d_print_info *dpi;
3579 const struct demangle_component *mod;
3581 switch (mod->type)
3583 case DEMANGLE_COMPONENT_RESTRICT:
3584 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3585 d_append_string_constant (dpi, " restrict");
3586 return;
3587 case DEMANGLE_COMPONENT_VOLATILE:
3588 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3589 d_append_string_constant (dpi, " volatile");
3590 return;
3591 case DEMANGLE_COMPONENT_CONST:
3592 case DEMANGLE_COMPONENT_CONST_THIS:
3593 d_append_string_constant (dpi, " const");
3594 return;
3595 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3596 d_append_char (dpi, ' ');
3597 d_print_comp (dpi, d_right (mod));
3598 return;
3599 case DEMANGLE_COMPONENT_POINTER:
3600 /* There is no pointer symbol in Java. */
3601 if ((dpi->options & DMGL_JAVA) == 0)
3602 d_append_char (dpi, '*');
3603 return;
3604 case DEMANGLE_COMPONENT_REFERENCE:
3605 d_append_char (dpi, '&');
3606 return;
3607 case DEMANGLE_COMPONENT_COMPLEX:
3608 d_append_string_constant (dpi, "complex ");
3609 return;
3610 case DEMANGLE_COMPONENT_IMAGINARY:
3611 d_append_string_constant (dpi, "imaginary ");
3612 return;
3613 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3614 if (d_last_char (dpi) != '(')
3615 d_append_char (dpi, ' ');
3616 d_print_comp (dpi, d_left (mod));
3617 d_append_string_constant (dpi, "::*");
3618 return;
3619 case DEMANGLE_COMPONENT_TYPED_NAME:
3620 d_print_comp (dpi, d_left (mod));
3621 return;
3622 default:
3623 /* Otherwise, we have something that won't go back on the
3624 modifier stack, so we can just print it. */
3625 d_print_comp (dpi, mod);
3626 return;
3630 /* Print a function type, except for the return type. */
3632 static void
3633 d_print_function_type (dpi, dc, mods)
3634 struct d_print_info *dpi;
3635 const struct demangle_component *dc;
3636 struct d_print_mod *mods;
3638 int need_paren;
3639 int saw_mod;
3640 int need_space;
3641 struct d_print_mod *p;
3642 struct d_print_mod *hold_modifiers;
3644 need_paren = 0;
3645 saw_mod = 0;
3646 need_space = 0;
3647 for (p = mods; p != NULL; p = p->next)
3649 if (p->printed)
3650 break;
3652 saw_mod = 1;
3653 switch (p->mod->type)
3655 case DEMANGLE_COMPONENT_POINTER:
3656 case DEMANGLE_COMPONENT_REFERENCE:
3657 need_paren = 1;
3658 break;
3659 case DEMANGLE_COMPONENT_RESTRICT:
3660 case DEMANGLE_COMPONENT_VOLATILE:
3661 case DEMANGLE_COMPONENT_CONST:
3662 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3663 case DEMANGLE_COMPONENT_COMPLEX:
3664 case DEMANGLE_COMPONENT_IMAGINARY:
3665 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3666 need_space = 1;
3667 need_paren = 1;
3668 break;
3669 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3670 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3671 case DEMANGLE_COMPONENT_CONST_THIS:
3672 break;
3673 default:
3674 break;
3676 if (need_paren)
3677 break;
3680 if (d_left (dc) != NULL && ! saw_mod)
3681 need_paren = 1;
3683 if (need_paren)
3685 if (! need_space)
3687 if (d_last_char (dpi) != '('
3688 && d_last_char (dpi) != '*')
3689 need_space = 1;
3691 if (need_space && d_last_char (dpi) != ' ')
3692 d_append_char (dpi, ' ');
3693 d_append_char (dpi, '(');
3696 hold_modifiers = dpi->modifiers;
3697 dpi->modifiers = NULL;
3699 d_print_mod_list (dpi, mods, 0);
3701 if (need_paren)
3702 d_append_char (dpi, ')');
3704 d_append_char (dpi, '(');
3706 if (d_right (dc) != NULL)
3707 d_print_comp (dpi, d_right (dc));
3709 d_append_char (dpi, ')');
3711 d_print_mod_list (dpi, mods, 1);
3713 dpi->modifiers = hold_modifiers;
3716 /* Print an array type, except for the element type. */
3718 static void
3719 d_print_array_type (dpi, dc, mods)
3720 struct d_print_info *dpi;
3721 const struct demangle_component *dc;
3722 struct d_print_mod *mods;
3724 int need_space;
3726 need_space = 1;
3727 if (mods != NULL)
3729 int need_paren;
3730 struct d_print_mod *p;
3732 need_paren = 0;
3733 for (p = mods; p != NULL; p = p->next)
3735 if (! p->printed)
3737 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3739 need_space = 0;
3740 break;
3742 else
3744 need_paren = 1;
3745 need_space = 1;
3746 break;
3751 if (need_paren)
3752 d_append_string_constant (dpi, " (");
3754 d_print_mod_list (dpi, mods, 0);
3756 if (need_paren)
3757 d_append_char (dpi, ')');
3760 if (need_space)
3761 d_append_char (dpi, ' ');
3763 d_append_char (dpi, '[');
3765 if (d_left (dc) != NULL)
3766 d_print_comp (dpi, d_left (dc));
3768 d_append_char (dpi, ']');
3771 /* Print an operator in an expression. */
3773 static void
3774 d_print_expr_op (dpi, dc)
3775 struct d_print_info *dpi;
3776 const struct demangle_component *dc;
3778 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3779 d_append_buffer (dpi, dc->u.s_operator.op->name,
3780 dc->u.s_operator.op->len);
3781 else
3782 d_print_comp (dpi, dc);
3785 /* Print a cast. */
3787 static void
3788 d_print_cast (dpi, dc)
3789 struct d_print_info *dpi;
3790 const struct demangle_component *dc;
3792 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3793 d_print_comp (dpi, d_left (dc));
3794 else
3796 struct d_print_mod *hold_dpm;
3797 struct d_print_template dpt;
3799 /* It appears that for a templated cast operator, we need to put
3800 the template parameters in scope for the operator name, but
3801 not for the parameters. The effect is that we need to handle
3802 the template printing here. */
3804 hold_dpm = dpi->modifiers;
3805 dpi->modifiers = NULL;
3807 dpt.next = dpi->templates;
3808 dpi->templates = &dpt;
3809 dpt.template = d_left (dc);
3811 d_print_comp (dpi, d_left (d_left (dc)));
3813 dpi->templates = dpt.next;
3815 if (d_last_char (dpi) == '<')
3816 d_append_char (dpi, ' ');
3817 d_append_char (dpi, '<');
3818 d_print_comp (dpi, d_right (d_left (dc)));
3819 /* Avoid generating two consecutive '>' characters, to avoid
3820 the C++ syntactic ambiguity. */
3821 if (d_last_char (dpi) == '>')
3822 d_append_char (dpi, ' ');
3823 d_append_char (dpi, '>');
3825 dpi->modifiers = hold_dpm;
3829 /* Initialize the information structure we use to pass around
3830 information. */
3832 CP_STATIC_IF_GLIBCPP_V3
3833 void
3834 cplus_demangle_init_info (mangled, options, len, di)
3835 const char *mangled;
3836 int options;
3837 size_t len;
3838 struct d_info *di;
3840 di->s = mangled;
3841 di->send = mangled + len;
3842 di->options = options;
3844 di->n = mangled;
3846 /* We can not need more components than twice the number of chars in
3847 the mangled string. Most components correspond directly to
3848 chars, but the ARGLIST types are exceptions. */
3849 di->num_comps = 2 * len;
3850 di->next_comp = 0;
3852 /* Similarly, we can not need more substitutions than there are
3853 chars in the mangled string. */
3854 di->num_subs = len;
3855 di->next_sub = 0;
3856 di->did_subs = 0;
3858 di->last_name = NULL;
3860 di->expansion = 0;
3863 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3864 name, return a buffer allocated with malloc holding the demangled
3865 name. OPTIONS is the usual libiberty demangler options. On
3866 success, this sets *PALC to the allocated size of the returned
3867 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3868 a memory allocation failure. On failure, this returns NULL. */
3870 static char *
3871 d_demangle (mangled, options, palc)
3872 const char* mangled;
3873 int options;
3874 size_t *palc;
3876 size_t len;
3877 int type;
3878 struct d_info di;
3879 struct demangle_component *dc;
3880 int estimate;
3881 char *ret;
3883 *palc = 0;
3885 len = strlen (mangled);
3887 if (mangled[0] == '_' && mangled[1] == 'Z')
3888 type = 0;
3889 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3890 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3891 && (mangled[9] == 'D' || mangled[9] == 'I')
3892 && mangled[10] == '_')
3894 char *r;
3896 r = malloc (40 + len - 11);
3897 if (r == NULL)
3898 *palc = 1;
3899 else
3901 if (mangled[9] == 'I')
3902 strcpy (r, "global constructors keyed to ");
3903 else
3904 strcpy (r, "global destructors keyed to ");
3905 strcat (r, mangled + 11);
3907 return r;
3909 else
3911 if ((options & DMGL_TYPES) == 0)
3912 return NULL;
3913 type = 1;
3916 cplus_demangle_init_info (mangled, options, len, &di);
3919 #ifdef CP_DYNAMIC_ARRAYS
3920 __extension__ struct demangle_component comps[di.num_comps];
3921 __extension__ struct demangle_component *subs[di.num_subs];
3923 di.comps = &comps[0];
3924 di.subs = &subs[0];
3925 #else
3926 di.comps = ((struct demangle_component *)
3927 malloc (di.num_comps * sizeof (struct demangle_component)));
3928 di.subs = ((struct demangle_component **)
3929 malloc (di.num_subs * sizeof (struct demangle_component *)));
3930 if (di.comps == NULL || di.subs == NULL)
3932 if (di.comps != NULL)
3933 free (di.comps);
3934 if (di.subs != NULL)
3935 free (di.subs);
3936 *palc = 1;
3937 return NULL;
3939 #endif
3941 if (! type)
3942 dc = cplus_demangle_mangled_name (&di, 1);
3943 else
3944 dc = cplus_demangle_type (&di);
3946 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3947 mangled string, then we didn't successfully demangle it. If
3948 DMGL_PARAMS is not set, we didn't look at the trailing
3949 parameters. */
3950 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3951 dc = NULL;
3953 #ifdef CP_DEMANGLE_DEBUG
3954 if (dc == NULL)
3955 printf ("failed demangling\n");
3956 else
3957 d_dump (dc, 0);
3958 #endif
3960 /* We try to guess the length of the demangled string, to minimize
3961 calls to realloc during demangling. */
3962 estimate = len + di.expansion + 10 * di.did_subs;
3963 estimate += estimate / 8;
3965 ret = NULL;
3966 if (dc != NULL)
3967 ret = cplus_demangle_print (options, dc, estimate, palc);
3969 #ifndef CP_DYNAMIC_ARRAYS
3970 free (di.comps);
3971 free (di.subs);
3972 #endif
3974 #ifdef CP_DEMANGLE_DEBUG
3975 if (ret != NULL)
3977 int rlen;
3979 rlen = strlen (ret);
3980 if (rlen > 2 * estimate)
3981 printf ("*** Length %d much greater than estimate %d\n",
3982 rlen, estimate);
3983 else if (rlen > estimate)
3984 printf ("*** Length %d greater than estimate %d\n",
3985 rlen, estimate);
3986 else if (rlen < estimate / 2)
3987 printf ("*** Length %d much less than estimate %d\n",
3988 rlen, estimate);
3990 #endif
3993 return ret;
3996 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3998 extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
4000 /* ia64 ABI-mandated entry point in the C++ runtime library for
4001 performing demangling. MANGLED_NAME is a NUL-terminated character
4002 string containing the name to be demangled.
4004 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4005 *LENGTH bytes, into which the demangled name is stored. If
4006 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4007 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4008 is placed in a region of memory allocated with malloc.
4010 If LENGTH is non-NULL, the length of the buffer conaining the
4011 demangled name, is placed in *LENGTH.
4013 The return value is a pointer to the start of the NUL-terminated
4014 demangled name, or NULL if the demangling fails. The caller is
4015 responsible for deallocating this memory using free.
4017 *STATUS is set to one of the following values:
4018 0: The demangling operation succeeded.
4019 -1: A memory allocation failure occurred.
4020 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4021 -3: One of the arguments is invalid.
4023 The demangling is performed using the C++ ABI mangling rules, with
4024 GNU extensions. */
4026 char *
4027 __cxa_demangle (mangled_name, output_buffer, length, status)
4028 const char *mangled_name;
4029 char *output_buffer;
4030 size_t *length;
4031 int *status;
4033 char *demangled;
4034 size_t alc;
4036 if (mangled_name == NULL)
4038 if (status != NULL)
4039 *status = -3;
4040 return NULL;
4043 if (output_buffer != NULL && length == NULL)
4045 if (status != NULL)
4046 *status = -3;
4047 return NULL;
4050 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4052 if (demangled == NULL)
4054 if (status != NULL)
4056 if (alc == 1)
4057 *status = -1;
4058 else
4059 *status = -2;
4061 return NULL;
4064 if (output_buffer == NULL)
4066 if (length != NULL)
4067 *length = alc;
4069 else
4071 if (strlen (demangled) < *length)
4073 strcpy (output_buffer, demangled);
4074 free (demangled);
4075 demangled = output_buffer;
4077 else
4079 free (output_buffer);
4080 *length = alc;
4084 if (status != NULL)
4085 *status = 0;
4087 return demangled;
4090 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4092 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4093 mangled name, return a buffer allocated with malloc holding the
4094 demangled name. Otherwise, return NULL. */
4096 char *
4097 cplus_demangle_v3 (mangled, options)
4098 const char* mangled;
4099 int options;
4101 size_t alc;
4103 return d_demangle (mangled, options, &alc);
4106 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4107 conventions, but the output formatting is a little different.
4108 This instructs the C++ demangler not to emit pointer characters ("*"), and
4109 to use Java's namespace separator symbol ("." instead of "::"). It then
4110 does an additional pass over the demangled output to replace instances
4111 of JArray<TYPE> with TYPE[]. */
4113 char *
4114 java_demangle_v3 (mangled)
4115 const char* mangled;
4117 size_t alc;
4118 char *demangled;
4119 int nesting;
4120 char *from;
4121 char *to;
4123 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
4125 if (demangled == NULL)
4126 return NULL;
4128 nesting = 0;
4129 from = demangled;
4130 to = from;
4131 while (*from != '\0')
4133 if (strncmp (from, "JArray<", 7) == 0)
4135 from += 7;
4136 ++nesting;
4138 else if (nesting > 0 && *from == '>')
4140 while (to > demangled && to[-1] == ' ')
4141 --to;
4142 *to++ = '[';
4143 *to++ = ']';
4144 --nesting;
4145 ++from;
4147 else
4148 *to++ = *from++;
4151 *to = '\0';
4153 return demangled;
4156 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4158 #ifndef IN_GLIBCPP_V3
4160 /* Demangle a string in order to find out whether it is a constructor
4161 or destructor. Return non-zero on success. Set *CTOR_KIND and
4162 *DTOR_KIND appropriately. */
4164 static int
4165 is_ctor_or_dtor (mangled, ctor_kind, dtor_kind)
4166 const char *mangled;
4167 enum gnu_v3_ctor_kinds *ctor_kind;
4168 enum gnu_v3_dtor_kinds *dtor_kind;
4170 struct d_info di;
4171 struct demangle_component *dc;
4172 int ret;
4174 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4175 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4177 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4180 #ifdef CP_DYNAMIC_ARRAYS
4181 __extension__ struct demangle_component comps[di.num_comps];
4182 __extension__ struct demangle_component *subs[di.num_subs];
4184 di.comps = &comps[0];
4185 di.subs = &subs[0];
4186 #else
4187 di.comps = ((struct demangle_component *)
4188 malloc (di.num_comps * sizeof (struct demangle_component)));
4189 di.subs = ((struct demangle_component **)
4190 malloc (di.num_subs * sizeof (struct demangle_component *)));
4191 if (di.comps == NULL || di.subs == NULL)
4193 if (di.comps != NULL)
4194 free (di.comps);
4195 if (di.subs != NULL)
4196 free (di.subs);
4197 return 0;
4199 #endif
4201 dc = cplus_demangle_mangled_name (&di, 1);
4203 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4204 to demangle the entire string. */
4206 ret = 0;
4207 while (dc != NULL)
4209 switch (dc->type)
4211 default:
4212 dc = NULL;
4213 break;
4214 case DEMANGLE_COMPONENT_TYPED_NAME:
4215 case DEMANGLE_COMPONENT_TEMPLATE:
4216 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4217 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4218 case DEMANGLE_COMPONENT_CONST_THIS:
4219 dc = d_left (dc);
4220 break;
4221 case DEMANGLE_COMPONENT_QUAL_NAME:
4222 case DEMANGLE_COMPONENT_LOCAL_NAME:
4223 dc = d_right (dc);
4224 break;
4225 case DEMANGLE_COMPONENT_CTOR:
4226 *ctor_kind = dc->u.s_ctor.kind;
4227 ret = 1;
4228 dc = NULL;
4229 break;
4230 case DEMANGLE_COMPONENT_DTOR:
4231 *dtor_kind = dc->u.s_dtor.kind;
4232 ret = 1;
4233 dc = NULL;
4234 break;
4238 #ifndef CP_DYNAMIC_ARRAYS
4239 free (di.subs);
4240 free (di.comps);
4241 #endif
4244 return ret;
4247 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4248 name. A non-zero return indicates the type of constructor. */
4250 enum gnu_v3_ctor_kinds
4251 is_gnu_v3_mangled_ctor (name)
4252 const char *name;
4254 enum gnu_v3_ctor_kinds ctor_kind;
4255 enum gnu_v3_dtor_kinds dtor_kind;
4257 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4258 return (enum gnu_v3_ctor_kinds) 0;
4259 return ctor_kind;
4263 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4264 name. A non-zero return indicates the type of destructor. */
4266 enum gnu_v3_dtor_kinds
4267 is_gnu_v3_mangled_dtor (name)
4268 const char *name;
4270 enum gnu_v3_ctor_kinds ctor_kind;
4271 enum gnu_v3_dtor_kinds dtor_kind;
4273 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4274 return (enum gnu_v3_dtor_kinds) 0;
4275 return dtor_kind;
4278 #endif /* IN_GLIBCPP_V3 */
4280 #ifdef STANDALONE_DEMANGLER
4282 #include "getopt.h"
4283 #include "dyn-string.h"
4285 static void print_usage PARAMS ((FILE* fp, int exit_value));
4287 #define IS_ALPHA(CHAR) \
4288 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4289 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4291 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4292 #define is_mangled_char(CHAR) \
4293 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4294 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4296 /* The name of this program, as invoked. */
4297 const char* program_name;
4299 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4301 static void
4302 print_usage (fp, exit_value)
4303 FILE* fp;
4304 int exit_value;
4306 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4307 fprintf (fp, "Options:\n");
4308 fprintf (fp, " -h,--help Display this message.\n");
4309 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4310 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4311 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4313 exit (exit_value);
4316 /* Option specification for getopt_long. */
4317 static const struct option long_options[] =
4319 { "help", no_argument, NULL, 'h' },
4320 { "no-params", no_argument, NULL, 'p' },
4321 { "verbose", no_argument, NULL, 'v' },
4322 { NULL, no_argument, NULL, 0 },
4325 /* Main entry for a demangling filter executable. It will demangle
4326 its command line arguments, if any. If none are provided, it will
4327 filter stdin to stdout, replacing any recognized mangled C++ names
4328 with their demangled equivalents. */
4331 main (argc, argv)
4332 int argc;
4333 char *argv[];
4335 int i;
4336 int opt_char;
4337 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4339 /* Use the program name of this program, as invoked. */
4340 program_name = argv[0];
4342 /* Parse options. */
4345 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4346 switch (opt_char)
4348 case '?': /* Unrecognized option. */
4349 print_usage (stderr, 1);
4350 break;
4352 case 'h':
4353 print_usage (stdout, 0);
4354 break;
4356 case 'p':
4357 options &= ~ DMGL_PARAMS;
4358 break;
4360 case 'v':
4361 options |= DMGL_VERBOSE;
4362 break;
4365 while (opt_char != -1);
4367 if (optind == argc)
4368 /* No command line arguments were provided. Filter stdin. */
4370 dyn_string_t mangled = dyn_string_new (3);
4371 char *s;
4373 /* Read all of input. */
4374 while (!feof (stdin))
4376 char c;
4378 /* Pile characters into mangled until we hit one that can't
4379 occur in a mangled name. */
4380 c = getchar ();
4381 while (!feof (stdin) && is_mangled_char (c))
4383 dyn_string_append_char (mangled, c);
4384 if (feof (stdin))
4385 break;
4386 c = getchar ();
4389 if (dyn_string_length (mangled) > 0)
4391 #ifdef IN_GLIBCPP_V3
4392 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4393 #else
4394 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4395 #endif
4397 if (s != NULL)
4399 fputs (s, stdout);
4400 free (s);
4402 else
4404 /* It might not have been a mangled name. Print the
4405 original text. */
4406 fputs (dyn_string_buf (mangled), stdout);
4409 dyn_string_clear (mangled);
4412 /* If we haven't hit EOF yet, we've read one character that
4413 can't occur in a mangled name, so print it out. */
4414 if (!feof (stdin))
4415 putchar (c);
4418 dyn_string_delete (mangled);
4420 else
4421 /* Demangle command line arguments. */
4423 /* Loop over command line arguments. */
4424 for (i = optind; i < argc; ++i)
4426 char *s;
4427 #ifdef IN_GLIBCPP_V3
4428 int status;
4429 #endif
4431 /* Attempt to demangle. */
4432 #ifdef IN_GLIBCPP_V3
4433 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4434 #else
4435 s = cplus_demangle_v3 (argv[i], options);
4436 #endif
4438 /* If it worked, print the demangled name. */
4439 if (s != NULL)
4441 printf ("%s\n", s);
4442 free (s);
4444 else
4446 #ifdef IN_GLIBCPP_V3
4447 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4448 #else
4449 fprintf (stderr, "Failed: %s\n", argv[i]);
4450 #endif
4455 return 0;
4458 #endif /* STANDALONE_DEMANGLER */