natClassLoader.cc (_Jv_RegisterInitiatingLoader): Check loading constraints.
[official-gcc.git] / libiberty / cp-demangle.c
blobedcfedca7a59f338a4a44afd3d298410a7f1ee85
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
40 name.
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 int cplus_demangle_v3_callback(const char *mangled, int options,
46 demangle_callbackref callback)
47 int java_demangle_v3_callback(const char *mangled,
48 demangle_callbackref callback)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
57 demangle_callbackref
58 and these functions defined in this file:
59 cplus_demangle_fill_name
60 cplus_demangle_fill_extended_operator
61 cplus_demangle_fill_ctor
62 cplus_demangle_fill_dtor
63 cplus_demangle_print
64 cplus_demangle_print_callback
65 and other functions defined in the file cp-demint.c.
67 This file also defines some other functions and variables which are
68 only to be used by the file cp-demint.c.
70 Preprocessor macros you can define while compiling this file:
72 IN_LIBGCC2
73 If defined, this file defines the following functions, q.v.:
74 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
75 int *status)
76 int __gcclibcxx_demangle_callback (const char *,
77 void (*)
78 (const char *, size_t, void *),
79 void *)
80 instead of cplus_demangle_v3[_callback]() and
81 java_demangle_v3[_callback]().
83 IN_GLIBCPP_V3
84 If defined, this file defines only __cxa_demangle() and
85 __gcclibcxx_demangle_callback(), and no other publically visible
86 functions or variables.
88 STANDALONE_DEMANGLER
89 If defined, this file defines a main() function which demangles
90 any arguments, or, if none, demangles stdin.
92 CP_DEMANGLE_DEBUG
93 If defined, turns on debugging mode, which prints information on
94 stdout about the mangled string. This is not generally useful.
97 #if defined (_AIX) && !defined (__GNUC__)
98 #pragma alloca
99 #endif
101 #ifdef HAVE_CONFIG_H
102 #include "config.h"
103 #endif
105 #include <stdio.h>
107 #ifdef HAVE_STDLIB_H
108 #include <stdlib.h>
109 #endif
110 #ifdef HAVE_STRING_H
111 #include <string.h>
112 #endif
114 #ifdef HAVE_ALLOCA_H
115 # include <alloca.h>
116 #else
117 # ifndef alloca
118 # ifdef __GNUC__
119 # define alloca __builtin_alloca
120 # else
121 extern char *alloca ();
122 # endif /* __GNUC__ */
123 # endif /* alloca */
124 #endif /* HAVE_ALLOCA_H */
126 #include "ansidecl.h"
127 #include "libiberty.h"
128 #include "demangle.h"
129 #include "cp-demangle.h"
131 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
132 also rename them via #define to avoid compiler errors when the
133 static definition conflicts with the extern declaration in a header
134 file. */
135 #ifdef IN_GLIBCPP_V3
137 #define CP_STATIC_IF_GLIBCPP_V3 static
139 #define cplus_demangle_fill_name d_fill_name
140 static int d_fill_name (struct demangle_component *, const char *, int);
142 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
143 static int
144 d_fill_extended_operator (struct demangle_component *, int,
145 struct demangle_component *);
147 #define cplus_demangle_fill_ctor d_fill_ctor
148 static int
149 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
150 struct demangle_component *);
152 #define cplus_demangle_fill_dtor d_fill_dtor
153 static int
154 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
155 struct demangle_component *);
157 #define cplus_demangle_mangled_name d_mangled_name
158 static struct demangle_component *d_mangled_name (struct d_info *, int);
160 #define cplus_demangle_type d_type
161 static struct demangle_component *d_type (struct d_info *);
163 #define cplus_demangle_print d_print
164 static char *d_print (int, const struct demangle_component *, int, size_t *);
166 #define cplus_demangle_print_callback d_print_callback
167 static int d_print_callback (int, const struct demangle_component *,
168 demangle_callbackref, void *);
170 #define cplus_demangle_init_info d_init_info
171 static void d_init_info (const char *, int, size_t, struct d_info *);
173 #else /* ! defined(IN_GLIBCPP_V3) */
174 #define CP_STATIC_IF_GLIBCPP_V3
175 #endif /* ! defined(IN_GLIBCPP_V3) */
177 /* See if the compiler supports dynamic arrays. */
179 #ifdef __GNUC__
180 #define CP_DYNAMIC_ARRAYS
181 #else
182 #ifdef __STDC__
183 #ifdef __STDC_VERSION__
184 #if __STDC_VERSION__ >= 199901L
185 #define CP_DYNAMIC_ARRAYS
186 #endif /* __STDC__VERSION >= 199901L */
187 #endif /* defined (__STDC_VERSION__) */
188 #endif /* defined (__STDC__) */
189 #endif /* ! defined (__GNUC__) */
191 /* We avoid pulling in the ctype tables, to prevent pulling in
192 additional unresolved symbols when this code is used in a library.
193 FIXME: Is this really a valid reason? This comes from the original
194 V3 demangler code.
196 As of this writing this file has the following undefined references
197 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
198 strcat, strlen. */
200 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
201 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
202 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
204 /* The prefix prepended by GCC to an identifier represnting the
205 anonymous namespace. */
206 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
207 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
208 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
210 /* Information we keep for the standard substitutions. */
212 struct d_standard_sub_info
214 /* The code for this substitution. */
215 char code;
216 /* The simple string it expands to. */
217 const char *simple_expansion;
218 /* The length of the simple expansion. */
219 int simple_len;
220 /* The results of a full, verbose, expansion. This is used when
221 qualifying a constructor/destructor, or when in verbose mode. */
222 const char *full_expansion;
223 /* The length of the full expansion. */
224 int full_len;
225 /* What to set the last_name field of d_info to; NULL if we should
226 not set it. This is only relevant when qualifying a
227 constructor/destructor. */
228 const char *set_last_name;
229 /* The length of set_last_name. */
230 int set_last_name_len;
233 /* Accessors for subtrees of struct demangle_component. */
235 #define d_left(dc) ((dc)->u.s_binary.left)
236 #define d_right(dc) ((dc)->u.s_binary.right)
238 /* A list of templates. This is used while printing. */
240 struct d_print_template
242 /* Next template on the list. */
243 struct d_print_template *next;
244 /* This template. */
245 const struct demangle_component *template_decl;
248 /* A list of type modifiers. This is used while printing. */
250 struct d_print_mod
252 /* Next modifier on the list. These are in the reverse of the order
253 in which they appeared in the mangled string. */
254 struct d_print_mod *next;
255 /* The modifier. */
256 const struct demangle_component *mod;
257 /* Whether this modifier was printed. */
258 int printed;
259 /* The list of templates which applies to this modifier. */
260 struct d_print_template *templates;
263 /* We use these structures to hold information during printing. */
265 struct d_growable_string
267 /* Buffer holding the result. */
268 char *buf;
269 /* Current length of data in buffer. */
270 size_t len;
271 /* Allocated size of buffer. */
272 size_t alc;
273 /* Set to 1 if we had a memory allocation failure. */
274 int allocation_failure;
277 enum { D_PRINT_BUFFER_LENGTH = 256 };
278 struct d_print_info
280 /* The options passed to the demangler. */
281 int options;
282 /* Fixed-length allocated buffer for demangled data, flushed to the
283 callback with a NUL termination once full. */
284 char buf[D_PRINT_BUFFER_LENGTH];
285 /* Current length of data in buffer. */
286 size_t len;
287 /* The last character printed, saved individually so that it survives
288 any buffer flush. */
289 char last_char;
290 /* Callback function to handle demangled buffer flush. */
291 demangle_callbackref callback;
292 /* Opaque callback argument. */
293 void *opaque;
294 /* The current list of templates, if any. */
295 struct d_print_template *templates;
296 /* The current list of modifiers (e.g., pointer, reference, etc.),
297 if any. */
298 struct d_print_mod *modifiers;
299 /* Set to 1 if we saw a demangling error. */
300 int demangle_failure;
303 #ifdef CP_DEMANGLE_DEBUG
304 static void d_dump (struct demangle_component *, int);
305 #endif
307 static struct demangle_component *
308 d_make_empty (struct d_info *);
310 static struct demangle_component *
311 d_make_comp (struct d_info *, enum demangle_component_type,
312 struct demangle_component *,
313 struct demangle_component *);
315 static struct demangle_component *
316 d_make_name (struct d_info *, const char *, int);
318 static struct demangle_component *
319 d_make_builtin_type (struct d_info *,
320 const struct demangle_builtin_type_info *);
322 static struct demangle_component *
323 d_make_operator (struct d_info *,
324 const struct demangle_operator_info *);
326 static struct demangle_component *
327 d_make_extended_operator (struct d_info *, int,
328 struct demangle_component *);
330 static struct demangle_component *
331 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
332 struct demangle_component *);
334 static struct demangle_component *
335 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
336 struct demangle_component *);
338 static struct demangle_component *
339 d_make_template_param (struct d_info *, long);
341 static struct demangle_component *
342 d_make_sub (struct d_info *, const char *, int);
344 static int
345 has_return_type (struct demangle_component *);
347 static int
348 is_ctor_dtor_or_conversion (struct demangle_component *);
350 static struct demangle_component *d_encoding (struct d_info *, int);
352 static struct demangle_component *d_name (struct d_info *);
354 static struct demangle_component *d_nested_name (struct d_info *);
356 static struct demangle_component *d_prefix (struct d_info *);
358 static struct demangle_component *d_unqualified_name (struct d_info *);
360 static struct demangle_component *d_source_name (struct d_info *);
362 static long d_number (struct d_info *);
364 static struct demangle_component *d_identifier (struct d_info *, int);
366 static struct demangle_component *d_operator_name (struct d_info *);
368 static struct demangle_component *d_special_name (struct d_info *);
370 static int d_call_offset (struct d_info *, int);
372 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
374 static struct demangle_component **
375 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
377 static struct demangle_component *
378 d_function_type (struct d_info *);
380 static struct demangle_component *
381 d_bare_function_type (struct d_info *, int);
383 static struct demangle_component *
384 d_class_enum_type (struct d_info *);
386 static struct demangle_component *d_array_type (struct d_info *);
388 static struct demangle_component *
389 d_pointer_to_member_type (struct d_info *);
391 static struct demangle_component *
392 d_template_param (struct d_info *);
394 static struct demangle_component *d_template_args (struct d_info *);
396 static struct demangle_component *
397 d_template_arg (struct d_info *);
399 static struct demangle_component *d_expression (struct d_info *);
401 static struct demangle_component *d_expr_primary (struct d_info *);
403 static struct demangle_component *d_local_name (struct d_info *);
405 static int d_discriminator (struct d_info *);
407 static int
408 d_add_substitution (struct d_info *, struct demangle_component *);
410 static struct demangle_component *d_substitution (struct d_info *, int);
412 static void d_growable_string_init (struct d_growable_string *, size_t);
414 static inline void
415 d_growable_string_resize (struct d_growable_string *, size_t);
417 static inline void
418 d_growable_string_append_buffer (struct d_growable_string *,
419 const char *, size_t);
420 static void
421 d_growable_string_callback_adapter (const char *, size_t, void *);
423 static void
424 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
426 static inline void d_print_error (struct d_print_info *);
428 static inline int d_print_saw_error (struct d_print_info *);
430 static inline void d_print_flush (struct d_print_info *);
432 static inline void d_append_char (struct d_print_info *, char);
434 static inline void d_append_buffer (struct d_print_info *,
435 const char *, size_t);
437 static inline void d_append_string (struct d_print_info *, const char *);
439 static inline char d_last_char (struct d_print_info *);
441 static void
442 d_print_comp (struct d_print_info *, const struct demangle_component *);
444 static void
445 d_print_java_identifier (struct d_print_info *, const char *, int);
447 static void
448 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
450 static void
451 d_print_mod (struct d_print_info *, const struct demangle_component *);
453 static void
454 d_print_function_type (struct d_print_info *,
455 const struct demangle_component *,
456 struct d_print_mod *);
458 static void
459 d_print_array_type (struct d_print_info *,
460 const struct demangle_component *,
461 struct d_print_mod *);
463 static void
464 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
466 static void
467 d_print_cast (struct d_print_info *, const struct demangle_component *);
469 static int d_demangle_callback (const char *, int,
470 demangle_callbackref, void *);
471 static char *d_demangle (const char *, int, size_t *);
473 #ifdef CP_DEMANGLE_DEBUG
475 static void
476 d_dump (struct demangle_component *dc, int indent)
478 int i;
480 if (dc == NULL)
482 if (indent == 0)
483 printf ("failed demangling\n");
484 return;
487 for (i = 0; i < indent; ++i)
488 putchar (' ');
490 switch (dc->type)
492 case DEMANGLE_COMPONENT_NAME:
493 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
494 return;
495 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
496 printf ("template parameter %ld\n", dc->u.s_number.number);
497 return;
498 case DEMANGLE_COMPONENT_CTOR:
499 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
500 d_dump (dc->u.s_ctor.name, indent + 2);
501 return;
502 case DEMANGLE_COMPONENT_DTOR:
503 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
504 d_dump (dc->u.s_dtor.name, indent + 2);
505 return;
506 case DEMANGLE_COMPONENT_SUB_STD:
507 printf ("standard substitution %s\n", dc->u.s_string.string);
508 return;
509 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
510 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
511 return;
512 case DEMANGLE_COMPONENT_OPERATOR:
513 printf ("operator %s\n", dc->u.s_operator.op->name);
514 return;
515 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
516 printf ("extended operator with %d args\n",
517 dc->u.s_extended_operator.args);
518 d_dump (dc->u.s_extended_operator.name, indent + 2);
519 return;
521 case DEMANGLE_COMPONENT_QUAL_NAME:
522 printf ("qualified name\n");
523 break;
524 case DEMANGLE_COMPONENT_LOCAL_NAME:
525 printf ("local name\n");
526 break;
527 case DEMANGLE_COMPONENT_TYPED_NAME:
528 printf ("typed name\n");
529 break;
530 case DEMANGLE_COMPONENT_TEMPLATE:
531 printf ("template\n");
532 break;
533 case DEMANGLE_COMPONENT_VTABLE:
534 printf ("vtable\n");
535 break;
536 case DEMANGLE_COMPONENT_VTT:
537 printf ("VTT\n");
538 break;
539 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
540 printf ("construction vtable\n");
541 break;
542 case DEMANGLE_COMPONENT_TYPEINFO:
543 printf ("typeinfo\n");
544 break;
545 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
546 printf ("typeinfo name\n");
547 break;
548 case DEMANGLE_COMPONENT_TYPEINFO_FN:
549 printf ("typeinfo function\n");
550 break;
551 case DEMANGLE_COMPONENT_THUNK:
552 printf ("thunk\n");
553 break;
554 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
555 printf ("virtual thunk\n");
556 break;
557 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
558 printf ("covariant thunk\n");
559 break;
560 case DEMANGLE_COMPONENT_JAVA_CLASS:
561 printf ("java class\n");
562 break;
563 case DEMANGLE_COMPONENT_GUARD:
564 printf ("guard\n");
565 break;
566 case DEMANGLE_COMPONENT_REFTEMP:
567 printf ("reference temporary\n");
568 break;
569 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
570 printf ("hidden alias\n");
571 break;
572 case DEMANGLE_COMPONENT_RESTRICT:
573 printf ("restrict\n");
574 break;
575 case DEMANGLE_COMPONENT_VOLATILE:
576 printf ("volatile\n");
577 break;
578 case DEMANGLE_COMPONENT_CONST:
579 printf ("const\n");
580 break;
581 case DEMANGLE_COMPONENT_RESTRICT_THIS:
582 printf ("restrict this\n");
583 break;
584 case DEMANGLE_COMPONENT_VOLATILE_THIS:
585 printf ("volatile this\n");
586 break;
587 case DEMANGLE_COMPONENT_CONST_THIS:
588 printf ("const this\n");
589 break;
590 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
591 printf ("vendor type qualifier\n");
592 break;
593 case DEMANGLE_COMPONENT_POINTER:
594 printf ("pointer\n");
595 break;
596 case DEMANGLE_COMPONENT_REFERENCE:
597 printf ("reference\n");
598 break;
599 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
600 printf ("rvalue reference\n");
601 break;
602 case DEMANGLE_COMPONENT_COMPLEX:
603 printf ("complex\n");
604 break;
605 case DEMANGLE_COMPONENT_IMAGINARY:
606 printf ("imaginary\n");
607 break;
608 case DEMANGLE_COMPONENT_VENDOR_TYPE:
609 printf ("vendor type\n");
610 break;
611 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
612 printf ("function type\n");
613 break;
614 case DEMANGLE_COMPONENT_ARRAY_TYPE:
615 printf ("array type\n");
616 break;
617 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
618 printf ("pointer to member type\n");
619 break;
620 case DEMANGLE_COMPONENT_ARGLIST:
621 printf ("argument list\n");
622 break;
623 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
624 printf ("template argument list\n");
625 break;
626 case DEMANGLE_COMPONENT_CAST:
627 printf ("cast\n");
628 break;
629 case DEMANGLE_COMPONENT_UNARY:
630 printf ("unary operator\n");
631 break;
632 case DEMANGLE_COMPONENT_BINARY:
633 printf ("binary operator\n");
634 break;
635 case DEMANGLE_COMPONENT_BINARY_ARGS:
636 printf ("binary operator arguments\n");
637 break;
638 case DEMANGLE_COMPONENT_TRINARY:
639 printf ("trinary operator\n");
640 break;
641 case DEMANGLE_COMPONENT_TRINARY_ARG1:
642 printf ("trinary operator arguments 1\n");
643 break;
644 case DEMANGLE_COMPONENT_TRINARY_ARG2:
645 printf ("trinary operator arguments 1\n");
646 break;
647 case DEMANGLE_COMPONENT_LITERAL:
648 printf ("literal\n");
649 break;
650 case DEMANGLE_COMPONENT_LITERAL_NEG:
651 printf ("negative literal\n");
652 break;
653 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
654 printf ("java resource\n");
655 break;
656 case DEMANGLE_COMPONENT_COMPOUND_NAME:
657 printf ("compound name\n");
658 break;
659 case DEMANGLE_COMPONENT_CHARACTER:
660 printf ("character '%c'\n", dc->u.s_character.character);
661 return;
664 d_dump (d_left (dc), indent + 2);
665 d_dump (d_right (dc), indent + 2);
668 #endif /* CP_DEMANGLE_DEBUG */
670 /* Fill in a DEMANGLE_COMPONENT_NAME. */
672 CP_STATIC_IF_GLIBCPP_V3
674 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
676 if (p == NULL || s == NULL || len == 0)
677 return 0;
678 p->type = DEMANGLE_COMPONENT_NAME;
679 p->u.s_name.s = s;
680 p->u.s_name.len = len;
681 return 1;
684 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
686 CP_STATIC_IF_GLIBCPP_V3
688 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
689 struct demangle_component *name)
691 if (p == NULL || args < 0 || name == NULL)
692 return 0;
693 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
694 p->u.s_extended_operator.args = args;
695 p->u.s_extended_operator.name = name;
696 return 1;
699 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
701 CP_STATIC_IF_GLIBCPP_V3
703 cplus_demangle_fill_ctor (struct demangle_component *p,
704 enum gnu_v3_ctor_kinds kind,
705 struct demangle_component *name)
707 if (p == NULL
708 || name == NULL
709 || (kind < gnu_v3_complete_object_ctor
710 && kind > gnu_v3_complete_object_allocating_ctor))
711 return 0;
712 p->type = DEMANGLE_COMPONENT_CTOR;
713 p->u.s_ctor.kind = kind;
714 p->u.s_ctor.name = name;
715 return 1;
718 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
720 CP_STATIC_IF_GLIBCPP_V3
722 cplus_demangle_fill_dtor (struct demangle_component *p,
723 enum gnu_v3_dtor_kinds kind,
724 struct demangle_component *name)
726 if (p == NULL
727 || name == NULL
728 || (kind < gnu_v3_deleting_dtor
729 && kind > gnu_v3_base_object_dtor))
730 return 0;
731 p->type = DEMANGLE_COMPONENT_DTOR;
732 p->u.s_dtor.kind = kind;
733 p->u.s_dtor.name = name;
734 return 1;
737 /* Add a new component. */
739 static struct demangle_component *
740 d_make_empty (struct d_info *di)
742 struct demangle_component *p;
744 if (di->next_comp >= di->num_comps)
745 return NULL;
746 p = &di->comps[di->next_comp];
747 ++di->next_comp;
748 return p;
751 /* Add a new generic component. */
753 static struct demangle_component *
754 d_make_comp (struct d_info *di, enum demangle_component_type type,
755 struct demangle_component *left,
756 struct demangle_component *right)
758 struct demangle_component *p;
760 /* We check for errors here. A typical error would be a NULL return
761 from a subroutine. We catch those here, and return NULL
762 upward. */
763 switch (type)
765 /* These types require two parameters. */
766 case DEMANGLE_COMPONENT_QUAL_NAME:
767 case DEMANGLE_COMPONENT_LOCAL_NAME:
768 case DEMANGLE_COMPONENT_TYPED_NAME:
769 case DEMANGLE_COMPONENT_TEMPLATE:
770 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
771 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
772 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
773 case DEMANGLE_COMPONENT_UNARY:
774 case DEMANGLE_COMPONENT_BINARY:
775 case DEMANGLE_COMPONENT_BINARY_ARGS:
776 case DEMANGLE_COMPONENT_TRINARY:
777 case DEMANGLE_COMPONENT_TRINARY_ARG1:
778 case DEMANGLE_COMPONENT_TRINARY_ARG2:
779 case DEMANGLE_COMPONENT_LITERAL:
780 case DEMANGLE_COMPONENT_LITERAL_NEG:
781 case DEMANGLE_COMPONENT_COMPOUND_NAME:
782 if (left == NULL || right == NULL)
783 return NULL;
784 break;
786 /* These types only require one parameter. */
787 case DEMANGLE_COMPONENT_VTABLE:
788 case DEMANGLE_COMPONENT_VTT:
789 case DEMANGLE_COMPONENT_TYPEINFO:
790 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
791 case DEMANGLE_COMPONENT_TYPEINFO_FN:
792 case DEMANGLE_COMPONENT_THUNK:
793 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
794 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
795 case DEMANGLE_COMPONENT_JAVA_CLASS:
796 case DEMANGLE_COMPONENT_GUARD:
797 case DEMANGLE_COMPONENT_REFTEMP:
798 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
799 case DEMANGLE_COMPONENT_POINTER:
800 case DEMANGLE_COMPONENT_REFERENCE:
801 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
802 case DEMANGLE_COMPONENT_COMPLEX:
803 case DEMANGLE_COMPONENT_IMAGINARY:
804 case DEMANGLE_COMPONENT_VENDOR_TYPE:
805 case DEMANGLE_COMPONENT_ARGLIST:
806 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
807 case DEMANGLE_COMPONENT_CAST:
808 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
809 if (left == NULL)
810 return NULL;
811 break;
813 /* This needs a right parameter, but the left parameter can be
814 empty. */
815 case DEMANGLE_COMPONENT_ARRAY_TYPE:
816 if (right == NULL)
817 return NULL;
818 break;
820 /* These are allowed to have no parameters--in some cases they
821 will be filled in later. */
822 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
823 case DEMANGLE_COMPONENT_RESTRICT:
824 case DEMANGLE_COMPONENT_VOLATILE:
825 case DEMANGLE_COMPONENT_CONST:
826 case DEMANGLE_COMPONENT_RESTRICT_THIS:
827 case DEMANGLE_COMPONENT_VOLATILE_THIS:
828 case DEMANGLE_COMPONENT_CONST_THIS:
829 break;
831 /* Other types should not be seen here. */
832 default:
833 return NULL;
836 p = d_make_empty (di);
837 if (p != NULL)
839 p->type = type;
840 p->u.s_binary.left = left;
841 p->u.s_binary.right = right;
843 return p;
846 /* Add a new name component. */
848 static struct demangle_component *
849 d_make_name (struct d_info *di, const char *s, int len)
851 struct demangle_component *p;
853 p = d_make_empty (di);
854 if (! cplus_demangle_fill_name (p, s, len))
855 return NULL;
856 return p;
859 /* Add a new builtin type component. */
861 static struct demangle_component *
862 d_make_builtin_type (struct d_info *di,
863 const struct demangle_builtin_type_info *type)
865 struct demangle_component *p;
867 if (type == NULL)
868 return NULL;
869 p = d_make_empty (di);
870 if (p != NULL)
872 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
873 p->u.s_builtin.type = type;
875 return p;
878 /* Add a new operator component. */
880 static struct demangle_component *
881 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
883 struct demangle_component *p;
885 p = d_make_empty (di);
886 if (p != NULL)
888 p->type = DEMANGLE_COMPONENT_OPERATOR;
889 p->u.s_operator.op = op;
891 return p;
894 /* Add a new extended operator component. */
896 static struct demangle_component *
897 d_make_extended_operator (struct d_info *di, int args,
898 struct demangle_component *name)
900 struct demangle_component *p;
902 p = d_make_empty (di);
903 if (! cplus_demangle_fill_extended_operator (p, args, name))
904 return NULL;
905 return p;
908 /* Add a new constructor component. */
910 static struct demangle_component *
911 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
912 struct demangle_component *name)
914 struct demangle_component *p;
916 p = d_make_empty (di);
917 if (! cplus_demangle_fill_ctor (p, kind, name))
918 return NULL;
919 return p;
922 /* Add a new destructor component. */
924 static struct demangle_component *
925 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
926 struct demangle_component *name)
928 struct demangle_component *p;
930 p = d_make_empty (di);
931 if (! cplus_demangle_fill_dtor (p, kind, name))
932 return NULL;
933 return p;
936 /* Add a new template parameter. */
938 static struct demangle_component *
939 d_make_template_param (struct d_info *di, long i)
941 struct demangle_component *p;
943 p = d_make_empty (di);
944 if (p != NULL)
946 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
947 p->u.s_number.number = i;
949 return p;
952 /* Add a new standard substitution component. */
954 static struct demangle_component *
955 d_make_sub (struct d_info *di, const char *name, int len)
957 struct demangle_component *p;
959 p = d_make_empty (di);
960 if (p != NULL)
962 p->type = DEMANGLE_COMPONENT_SUB_STD;
963 p->u.s_string.string = name;
964 p->u.s_string.len = len;
966 return p;
969 /* <mangled-name> ::= _Z <encoding>
971 TOP_LEVEL is non-zero when called at the top level. */
973 CP_STATIC_IF_GLIBCPP_V3
974 struct demangle_component *
975 cplus_demangle_mangled_name (struct d_info *di, int top_level)
977 if (! d_check_char (di, '_'))
978 return NULL;
979 if (! d_check_char (di, 'Z'))
980 return NULL;
981 return d_encoding (di, top_level);
984 /* Return whether a function should have a return type. The argument
985 is the function name, which may be qualified in various ways. The
986 rules are that template functions have return types with some
987 exceptions, function types which are not part of a function name
988 mangling have return types with some exceptions, and non-template
989 function names do not have return types. The exceptions are that
990 constructors, destructors, and conversion operators do not have
991 return types. */
993 static int
994 has_return_type (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 (struct demangle_component *dc)
1017 if (dc == NULL)
1018 return 0;
1019 switch (dc->type)
1021 default:
1022 return 0;
1023 case DEMANGLE_COMPONENT_QUAL_NAME:
1024 case DEMANGLE_COMPONENT_LOCAL_NAME:
1025 return is_ctor_dtor_or_conversion (d_right (dc));
1026 case DEMANGLE_COMPONENT_CTOR:
1027 case DEMANGLE_COMPONENT_DTOR:
1028 case DEMANGLE_COMPONENT_CAST:
1029 return 1;
1033 /* <encoding> ::= <(function) name> <bare-function-type>
1034 ::= <(data) name>
1035 ::= <special-name>
1037 TOP_LEVEL is non-zero when called at the top level, in which case
1038 if DMGL_PARAMS is not set we do not demangle the function
1039 parameters. We only set this at the top level, because otherwise
1040 we would not correctly demangle names in local scopes. */
1042 static struct demangle_component *
1043 d_encoding (struct d_info *di, int top_level)
1045 char peek = d_peek_char (di);
1047 if (peek == 'G' || peek == 'T')
1048 return d_special_name (di);
1049 else
1051 struct demangle_component *dc;
1053 dc = d_name (di);
1055 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1057 /* Strip off any initial CV-qualifiers, as they really apply
1058 to the `this' parameter, and they were not output by the
1059 v2 demangler without DMGL_PARAMS. */
1060 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1061 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1062 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1063 dc = d_left (dc);
1065 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1066 there may be CV-qualifiers on its right argument which
1067 really apply here; this happens when parsing a class
1068 which is local to a function. */
1069 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1071 struct demangle_component *dcr;
1073 dcr = d_right (dc);
1074 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1075 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1076 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1077 dcr = d_left (dcr);
1078 dc->u.s_binary.right = dcr;
1081 return dc;
1084 peek = d_peek_char (di);
1085 if (dc == NULL || peek == '\0' || peek == 'E')
1086 return dc;
1087 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1088 d_bare_function_type (di, has_return_type (dc)));
1092 /* <name> ::= <nested-name>
1093 ::= <unscoped-name>
1094 ::= <unscoped-template-name> <template-args>
1095 ::= <local-name>
1097 <unscoped-name> ::= <unqualified-name>
1098 ::= St <unqualified-name>
1100 <unscoped-template-name> ::= <unscoped-name>
1101 ::= <substitution>
1104 static struct demangle_component *
1105 d_name (struct d_info *di)
1107 char peek = d_peek_char (di);
1108 struct demangle_component *dc;
1110 switch (peek)
1112 case 'N':
1113 return d_nested_name (di);
1115 case 'Z':
1116 return d_local_name (di);
1118 case 'L':
1119 return d_unqualified_name (di);
1121 case 'S':
1123 int subst;
1125 if (d_peek_next_char (di) != 't')
1127 dc = d_substitution (di, 0);
1128 subst = 1;
1130 else
1132 d_advance (di, 2);
1133 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1134 d_make_name (di, "std", 3),
1135 d_unqualified_name (di));
1136 di->expansion += 3;
1137 subst = 0;
1140 if (d_peek_char (di) != 'I')
1142 /* The grammar does not permit this case to occur if we
1143 called d_substitution() above (i.e., subst == 1). We
1144 don't bother to check. */
1146 else
1148 /* This is <template-args>, which means that we just saw
1149 <unscoped-template-name>, which is a substitution
1150 candidate if we didn't just get it from a
1151 substitution. */
1152 if (! subst)
1154 if (! d_add_substitution (di, dc))
1155 return NULL;
1157 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1158 d_template_args (di));
1161 return dc;
1164 default:
1165 dc = d_unqualified_name (di);
1166 if (d_peek_char (di) == 'I')
1168 /* This is <template-args>, which means that we just saw
1169 <unscoped-template-name>, which is a substitution
1170 candidate. */
1171 if (! d_add_substitution (di, dc))
1172 return NULL;
1173 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1174 d_template_args (di));
1176 return dc;
1180 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1181 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1184 static struct demangle_component *
1185 d_nested_name (struct d_info *di)
1187 struct demangle_component *ret;
1188 struct demangle_component **pret;
1190 if (! d_check_char (di, 'N'))
1191 return NULL;
1193 pret = d_cv_qualifiers (di, &ret, 1);
1194 if (pret == NULL)
1195 return NULL;
1197 *pret = d_prefix (di);
1198 if (*pret == NULL)
1199 return NULL;
1201 if (! d_check_char (di, 'E'))
1202 return NULL;
1204 return ret;
1207 /* <prefix> ::= <prefix> <unqualified-name>
1208 ::= <template-prefix> <template-args>
1209 ::= <template-param>
1211 ::= <substitution>
1213 <template-prefix> ::= <prefix> <(template) unqualified-name>
1214 ::= <template-param>
1215 ::= <substitution>
1218 static struct demangle_component *
1219 d_prefix (struct d_info *di)
1221 struct demangle_component *ret = NULL;
1223 while (1)
1225 char peek;
1226 enum demangle_component_type comb_type;
1227 struct demangle_component *dc;
1229 peek = d_peek_char (di);
1230 if (peek == '\0')
1231 return NULL;
1233 /* The older code accepts a <local-name> here, but I don't see
1234 that in the grammar. The older code does not accept a
1235 <template-param> here. */
1237 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1238 if (IS_DIGIT (peek)
1239 || IS_LOWER (peek)
1240 || peek == 'C'
1241 || peek == 'D'
1242 || peek == 'L')
1243 dc = d_unqualified_name (di);
1244 else if (peek == 'S')
1245 dc = d_substitution (di, 1);
1246 else if (peek == 'I')
1248 if (ret == NULL)
1249 return NULL;
1250 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1251 dc = d_template_args (di);
1253 else if (peek == 'T')
1254 dc = d_template_param (di);
1255 else if (peek == 'E')
1256 return ret;
1257 else
1258 return NULL;
1260 if (ret == NULL)
1261 ret = dc;
1262 else
1263 ret = d_make_comp (di, comb_type, ret, dc);
1265 if (peek != 'S' && d_peek_char (di) != 'E')
1267 if (! d_add_substitution (di, ret))
1268 return NULL;
1273 /* <unqualified-name> ::= <operator-name>
1274 ::= <ctor-dtor-name>
1275 ::= <source-name>
1276 ::= <local-source-name>
1278 <local-source-name> ::= L <source-name> <discriminator>
1281 static struct demangle_component *
1282 d_unqualified_name (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 if (peek == 'L')
1302 struct demangle_component * ret;
1304 d_advance (di, 1);
1306 ret = d_source_name (di);
1307 if (ret == NULL)
1308 return NULL;
1309 if (! d_discriminator (di))
1310 return NULL;
1311 return ret;
1313 else
1314 return NULL;
1317 /* <source-name> ::= <(positive length) number> <identifier> */
1319 static struct demangle_component *
1320 d_source_name (struct d_info *di)
1322 long len;
1323 struct demangle_component *ret;
1325 len = d_number (di);
1326 if (len <= 0)
1327 return NULL;
1328 ret = d_identifier (di, len);
1329 di->last_name = ret;
1330 return ret;
1333 /* number ::= [n] <(non-negative decimal integer)> */
1335 static long
1336 d_number (struct d_info *di)
1338 int negative;
1339 char peek;
1340 long ret;
1342 negative = 0;
1343 peek = d_peek_char (di);
1344 if (peek == 'n')
1346 negative = 1;
1347 d_advance (di, 1);
1348 peek = d_peek_char (di);
1351 ret = 0;
1352 while (1)
1354 if (! IS_DIGIT (peek))
1356 if (negative)
1357 ret = - ret;
1358 return ret;
1360 ret = ret * 10 + peek - '0';
1361 d_advance (di, 1);
1362 peek = d_peek_char (di);
1366 /* identifier ::= <(unqualified source code identifier)> */
1368 static struct demangle_component *
1369 d_identifier (struct d_info *di, int len)
1371 const char *name;
1373 name = d_str (di);
1375 if (di->send - name < len)
1376 return NULL;
1378 d_advance (di, len);
1380 /* A Java mangled name may have a trailing '$' if it is a C++
1381 keyword. This '$' is not included in the length count. We just
1382 ignore the '$'. */
1383 if ((di->options & DMGL_JAVA) != 0
1384 && d_peek_char (di) == '$')
1385 d_advance (di, 1);
1387 /* Look for something which looks like a gcc encoding of an
1388 anonymous namespace, and replace it with a more user friendly
1389 name. */
1390 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1391 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1392 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1394 const char *s;
1396 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1397 if ((*s == '.' || *s == '_' || *s == '$')
1398 && s[1] == 'N')
1400 di->expansion -= len - sizeof "(anonymous namespace)";
1401 return d_make_name (di, "(anonymous namespace)",
1402 sizeof "(anonymous namespace)" - 1);
1406 return d_make_name (di, name, len);
1409 /* operator_name ::= many different two character encodings.
1410 ::= cv <type>
1411 ::= v <digit> <source-name>
1414 #define NL(s) s, (sizeof s) - 1
1416 CP_STATIC_IF_GLIBCPP_V3
1417 const struct demangle_operator_info cplus_demangle_operators[] =
1419 { "aN", NL ("&="), 2 },
1420 { "aS", NL ("="), 2 },
1421 { "aa", NL ("&&"), 2 },
1422 { "ad", NL ("&"), 1 },
1423 { "an", NL ("&"), 2 },
1424 { "cl", NL ("()"), 0 },
1425 { "cm", NL (","), 2 },
1426 { "co", NL ("~"), 1 },
1427 { "dV", NL ("/="), 2 },
1428 { "da", NL ("delete[]"), 1 },
1429 { "de", NL ("*"), 1 },
1430 { "dl", NL ("delete"), 1 },
1431 { "dv", NL ("/"), 2 },
1432 { "eO", NL ("^="), 2 },
1433 { "eo", NL ("^"), 2 },
1434 { "eq", NL ("=="), 2 },
1435 { "ge", NL (">="), 2 },
1436 { "gt", NL (">"), 2 },
1437 { "ix", NL ("[]"), 2 },
1438 { "lS", NL ("<<="), 2 },
1439 { "le", NL ("<="), 2 },
1440 { "ls", NL ("<<"), 2 },
1441 { "lt", NL ("<"), 2 },
1442 { "mI", NL ("-="), 2 },
1443 { "mL", NL ("*="), 2 },
1444 { "mi", NL ("-"), 2 },
1445 { "ml", NL ("*"), 2 },
1446 { "mm", NL ("--"), 1 },
1447 { "na", NL ("new[]"), 1 },
1448 { "ne", NL ("!="), 2 },
1449 { "ng", NL ("-"), 1 },
1450 { "nt", NL ("!"), 1 },
1451 { "nw", NL ("new"), 1 },
1452 { "oR", NL ("|="), 2 },
1453 { "oo", NL ("||"), 2 },
1454 { "or", NL ("|"), 2 },
1455 { "pL", NL ("+="), 2 },
1456 { "pl", NL ("+"), 2 },
1457 { "pm", NL ("->*"), 2 },
1458 { "pp", NL ("++"), 1 },
1459 { "ps", NL ("+"), 1 },
1460 { "pt", NL ("->"), 2 },
1461 { "qu", NL ("?"), 3 },
1462 { "rM", NL ("%="), 2 },
1463 { "rS", NL (">>="), 2 },
1464 { "rm", NL ("%"), 2 },
1465 { "rs", NL (">>"), 2 },
1466 { "st", NL ("sizeof "), 1 },
1467 { "sz", NL ("sizeof "), 1 },
1468 { NULL, NULL, 0, 0 }
1471 static struct demangle_component *
1472 d_operator_name (struct d_info *di)
1474 char c1;
1475 char c2;
1477 c1 = d_next_char (di);
1478 c2 = d_next_char (di);
1479 if (c1 == 'v' && IS_DIGIT (c2))
1480 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1481 else if (c1 == 'c' && c2 == 'v')
1482 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1483 cplus_demangle_type (di), NULL);
1484 else
1486 /* LOW is the inclusive lower bound. */
1487 int low = 0;
1488 /* HIGH is the exclusive upper bound. We subtract one to ignore
1489 the sentinel at the end of the array. */
1490 int high = ((sizeof (cplus_demangle_operators)
1491 / sizeof (cplus_demangle_operators[0]))
1492 - 1);
1494 while (1)
1496 int i;
1497 const struct demangle_operator_info *p;
1499 i = low + (high - low) / 2;
1500 p = cplus_demangle_operators + i;
1502 if (c1 == p->code[0] && c2 == p->code[1])
1503 return d_make_operator (di, p);
1505 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1506 high = i;
1507 else
1508 low = i + 1;
1509 if (low == high)
1510 return NULL;
1515 static struct demangle_component *
1516 d_make_character (struct d_info *di, int c)
1518 struct demangle_component *p;
1519 p = d_make_empty (di);
1520 if (p != NULL)
1522 p->type = DEMANGLE_COMPONENT_CHARACTER;
1523 p->u.s_character.character = c;
1525 return p;
1528 static struct demangle_component *
1529 d_java_resource (struct d_info *di)
1531 struct demangle_component *p = NULL;
1532 struct demangle_component *next = NULL;
1533 long len, i;
1534 char c;
1535 const char *str;
1537 len = d_number (di);
1538 if (len <= 1)
1539 return NULL;
1541 /* Eat the leading '_'. */
1542 if (d_next_char (di) != '_')
1543 return NULL;
1544 len--;
1546 str = d_str (di);
1547 i = 0;
1549 while (len > 0)
1551 c = str[i];
1552 if (!c)
1553 return NULL;
1555 /* Each chunk is either a '$' escape... */
1556 if (c == '$')
1558 i++;
1559 switch (str[i++])
1561 case 'S':
1562 c = '/';
1563 break;
1564 case '_':
1565 c = '.';
1566 break;
1567 case '$':
1568 c = '$';
1569 break;
1570 default:
1571 return NULL;
1573 next = d_make_character (di, c);
1574 d_advance (di, i);
1575 str = d_str (di);
1576 len -= i;
1577 i = 0;
1578 if (next == NULL)
1579 return NULL;
1581 /* ... or a sequence of characters. */
1582 else
1584 while (i < len && str[i] && str[i] != '$')
1585 i++;
1587 next = d_make_name (di, str, i);
1588 d_advance (di, i);
1589 str = d_str (di);
1590 len -= i;
1591 i = 0;
1592 if (next == NULL)
1593 return NULL;
1596 if (p == NULL)
1597 p = next;
1598 else
1600 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1601 if (p == NULL)
1602 return NULL;
1606 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1608 return p;
1611 /* <special-name> ::= TV <type>
1612 ::= TT <type>
1613 ::= TI <type>
1614 ::= TS <type>
1615 ::= GV <(object) name>
1616 ::= T <call-offset> <(base) encoding>
1617 ::= Tc <call-offset> <call-offset> <(base) encoding>
1618 Also g++ extensions:
1619 ::= TC <type> <(offset) number> _ <(base) type>
1620 ::= TF <type>
1621 ::= TJ <type>
1622 ::= GR <name>
1623 ::= GA <encoding>
1624 ::= Gr <resource name>
1627 static struct demangle_component *
1628 d_special_name (struct d_info *di)
1630 di->expansion += 20;
1631 if (d_check_char (di, 'T'))
1633 switch (d_next_char (di))
1635 case 'V':
1636 di->expansion -= 5;
1637 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1638 cplus_demangle_type (di), NULL);
1639 case 'T':
1640 di->expansion -= 10;
1641 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1642 cplus_demangle_type (di), NULL);
1643 case 'I':
1644 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1645 cplus_demangle_type (di), NULL);
1646 case 'S':
1647 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1648 cplus_demangle_type (di), NULL);
1650 case 'h':
1651 if (! d_call_offset (di, 'h'))
1652 return NULL;
1653 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1654 d_encoding (di, 0), NULL);
1656 case 'v':
1657 if (! d_call_offset (di, 'v'))
1658 return NULL;
1659 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1660 d_encoding (di, 0), NULL);
1662 case 'c':
1663 if (! d_call_offset (di, '\0'))
1664 return NULL;
1665 if (! d_call_offset (di, '\0'))
1666 return NULL;
1667 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1668 d_encoding (di, 0), NULL);
1670 case 'C':
1672 struct demangle_component *derived_type;
1673 long offset;
1674 struct demangle_component *base_type;
1676 derived_type = cplus_demangle_type (di);
1677 offset = d_number (di);
1678 if (offset < 0)
1679 return NULL;
1680 if (! d_check_char (di, '_'))
1681 return NULL;
1682 base_type = cplus_demangle_type (di);
1683 /* We don't display the offset. FIXME: We should display
1684 it in verbose mode. */
1685 di->expansion += 5;
1686 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1687 base_type, derived_type);
1690 case 'F':
1691 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1692 cplus_demangle_type (di), NULL);
1693 case 'J':
1694 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1695 cplus_demangle_type (di), NULL);
1697 default:
1698 return NULL;
1701 else if (d_check_char (di, 'G'))
1703 switch (d_next_char (di))
1705 case 'V':
1706 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1708 case 'R':
1709 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1710 NULL);
1712 case 'A':
1713 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1714 d_encoding (di, 0), NULL);
1716 case 'r':
1717 return d_java_resource (di);
1719 default:
1720 return NULL;
1723 else
1724 return NULL;
1727 /* <call-offset> ::= h <nv-offset> _
1728 ::= v <v-offset> _
1730 <nv-offset> ::= <(offset) number>
1732 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1734 The C parameter, if not '\0', is a character we just read which is
1735 the start of the <call-offset>.
1737 We don't display the offset information anywhere. FIXME: We should
1738 display it in verbose mode. */
1740 static int
1741 d_call_offset (struct d_info *di, int c)
1743 if (c == '\0')
1744 c = d_next_char (di);
1746 if (c == 'h')
1747 d_number (di);
1748 else if (c == 'v')
1750 d_number (di);
1751 if (! d_check_char (di, '_'))
1752 return 0;
1753 d_number (di);
1755 else
1756 return 0;
1758 if (! d_check_char (di, '_'))
1759 return 0;
1761 return 1;
1764 /* <ctor-dtor-name> ::= C1
1765 ::= C2
1766 ::= C3
1767 ::= D0
1768 ::= D1
1769 ::= D2
1772 static struct demangle_component *
1773 d_ctor_dtor_name (struct d_info *di)
1775 if (di->last_name != NULL)
1777 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1778 di->expansion += di->last_name->u.s_name.len;
1779 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1780 di->expansion += di->last_name->u.s_string.len;
1782 switch (d_peek_char (di))
1784 case 'C':
1786 enum gnu_v3_ctor_kinds kind;
1788 switch (d_peek_next_char (di))
1790 case '1':
1791 kind = gnu_v3_complete_object_ctor;
1792 break;
1793 case '2':
1794 kind = gnu_v3_base_object_ctor;
1795 break;
1796 case '3':
1797 kind = gnu_v3_complete_object_allocating_ctor;
1798 break;
1799 default:
1800 return NULL;
1802 d_advance (di, 2);
1803 return d_make_ctor (di, kind, di->last_name);
1806 case 'D':
1808 enum gnu_v3_dtor_kinds kind;
1810 switch (d_peek_next_char (di))
1812 case '0':
1813 kind = gnu_v3_deleting_dtor;
1814 break;
1815 case '1':
1816 kind = gnu_v3_complete_object_dtor;
1817 break;
1818 case '2':
1819 kind = gnu_v3_base_object_dtor;
1820 break;
1821 default:
1822 return NULL;
1824 d_advance (di, 2);
1825 return d_make_dtor (di, kind, di->last_name);
1828 default:
1829 return NULL;
1833 /* <type> ::= <builtin-type>
1834 ::= <function-type>
1835 ::= <class-enum-type>
1836 ::= <array-type>
1837 ::= <pointer-to-member-type>
1838 ::= <template-param>
1839 ::= <template-template-param> <template-args>
1840 ::= <substitution>
1841 ::= <CV-qualifiers> <type>
1842 ::= P <type>
1843 ::= R <type>
1844 ::= O <type> (C++0x)
1845 ::= C <type>
1846 ::= G <type>
1847 ::= U <source-name> <type>
1849 <builtin-type> ::= various one letter codes
1850 ::= u <source-name>
1853 CP_STATIC_IF_GLIBCPP_V3
1854 const struct demangle_builtin_type_info
1855 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1857 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1858 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1859 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1860 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1861 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1862 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1863 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1864 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1865 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1866 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1867 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1868 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1869 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1870 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1871 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1872 D_PRINT_DEFAULT },
1873 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1874 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1875 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1876 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1877 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1878 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1879 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1880 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1881 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1882 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1883 D_PRINT_UNSIGNED_LONG_LONG },
1884 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1887 CP_STATIC_IF_GLIBCPP_V3
1888 struct demangle_component *
1889 cplus_demangle_type (struct d_info *di)
1891 char peek;
1892 struct demangle_component *ret;
1893 int can_subst;
1895 /* The ABI specifies that when CV-qualifiers are used, the base type
1896 is substitutable, and the fully qualified type is substitutable,
1897 but the base type with a strict subset of the CV-qualifiers is
1898 not substitutable. The natural recursive implementation of the
1899 CV-qualifiers would cause subsets to be substitutable, so instead
1900 we pull them all off now.
1902 FIXME: The ABI says that order-insensitive vendor qualifiers
1903 should be handled in the same way, but we have no way to tell
1904 which vendor qualifiers are order-insensitive and which are
1905 order-sensitive. So we just assume that they are all
1906 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1907 __vector, and it treats it as order-sensitive when mangling
1908 names. */
1910 peek = d_peek_char (di);
1911 if (peek == 'r' || peek == 'V' || peek == 'K')
1913 struct demangle_component **pret;
1915 pret = d_cv_qualifiers (di, &ret, 0);
1916 if (pret == NULL)
1917 return NULL;
1918 *pret = cplus_demangle_type (di);
1919 if (! *pret || ! d_add_substitution (di, ret))
1920 return NULL;
1921 return ret;
1924 can_subst = 1;
1926 switch (peek)
1928 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1929 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1930 case 'o': case 's': case 't':
1931 case 'v': case 'w': case 'x': case 'y': case 'z':
1932 ret = d_make_builtin_type (di,
1933 &cplus_demangle_builtin_types[peek - 'a']);
1934 di->expansion += ret->u.s_builtin.type->len;
1935 can_subst = 0;
1936 d_advance (di, 1);
1937 break;
1939 case 'u':
1940 d_advance (di, 1);
1941 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1942 d_source_name (di), NULL);
1943 break;
1945 case 'F':
1946 ret = d_function_type (di);
1947 break;
1949 case '0': case '1': case '2': case '3': case '4':
1950 case '5': case '6': case '7': case '8': case '9':
1951 case 'N':
1952 case 'Z':
1953 ret = d_class_enum_type (di);
1954 break;
1956 case 'A':
1957 ret = d_array_type (di);
1958 break;
1960 case 'M':
1961 ret = d_pointer_to_member_type (di);
1962 break;
1964 case 'T':
1965 ret = d_template_param (di);
1966 if (d_peek_char (di) == 'I')
1968 /* This is <template-template-param> <template-args>. The
1969 <template-template-param> part is a substitution
1970 candidate. */
1971 if (! d_add_substitution (di, ret))
1972 return NULL;
1973 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1974 d_template_args (di));
1976 break;
1978 case 'S':
1979 /* If this is a special substitution, then it is the start of
1980 <class-enum-type>. */
1982 char peek_next;
1984 peek_next = d_peek_next_char (di);
1985 if (IS_DIGIT (peek_next)
1986 || peek_next == '_'
1987 || IS_UPPER (peek_next))
1989 ret = d_substitution (di, 0);
1990 /* The substituted name may have been a template name and
1991 may be followed by tepmlate args. */
1992 if (d_peek_char (di) == 'I')
1993 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1994 d_template_args (di));
1995 else
1996 can_subst = 0;
1998 else
2000 ret = d_class_enum_type (di);
2001 /* If the substitution was a complete type, then it is not
2002 a new substitution candidate. However, if the
2003 substitution was followed by template arguments, then
2004 the whole thing is a substitution candidate. */
2005 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2006 can_subst = 0;
2009 break;
2011 case 'O':
2012 d_advance (di, 1);
2013 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2014 cplus_demangle_type (di), NULL);
2015 break;
2017 case 'P':
2018 d_advance (di, 1);
2019 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2020 cplus_demangle_type (di), NULL);
2021 break;
2023 case 'R':
2024 d_advance (di, 1);
2025 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2026 cplus_demangle_type (di), NULL);
2027 break;
2029 case 'C':
2030 d_advance (di, 1);
2031 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2032 cplus_demangle_type (di), NULL);
2033 break;
2035 case 'G':
2036 d_advance (di, 1);
2037 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2038 cplus_demangle_type (di), NULL);
2039 break;
2041 case 'U':
2042 d_advance (di, 1);
2043 ret = d_source_name (di);
2044 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2045 cplus_demangle_type (di), ret);
2046 break;
2048 default:
2049 return NULL;
2052 if (can_subst)
2054 if (! d_add_substitution (di, ret))
2055 return NULL;
2058 return ret;
2061 /* <CV-qualifiers> ::= [r] [V] [K] */
2063 static struct demangle_component **
2064 d_cv_qualifiers (struct d_info *di,
2065 struct demangle_component **pret, int member_fn)
2067 char peek;
2069 peek = d_peek_char (di);
2070 while (peek == 'r' || peek == 'V' || peek == 'K')
2072 enum demangle_component_type t;
2074 d_advance (di, 1);
2075 if (peek == 'r')
2077 t = (member_fn
2078 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2079 : DEMANGLE_COMPONENT_RESTRICT);
2080 di->expansion += sizeof "restrict";
2082 else if (peek == 'V')
2084 t = (member_fn
2085 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2086 : DEMANGLE_COMPONENT_VOLATILE);
2087 di->expansion += sizeof "volatile";
2089 else
2091 t = (member_fn
2092 ? DEMANGLE_COMPONENT_CONST_THIS
2093 : DEMANGLE_COMPONENT_CONST);
2094 di->expansion += sizeof "const";
2097 *pret = d_make_comp (di, t, NULL, NULL);
2098 if (*pret == NULL)
2099 return NULL;
2100 pret = &d_left (*pret);
2102 peek = d_peek_char (di);
2105 return pret;
2108 /* <function-type> ::= F [Y] <bare-function-type> E */
2110 static struct demangle_component *
2111 d_function_type (struct d_info *di)
2113 struct demangle_component *ret;
2115 if (! d_check_char (di, 'F'))
2116 return NULL;
2117 if (d_peek_char (di) == 'Y')
2119 /* Function has C linkage. We don't print this information.
2120 FIXME: We should print it in verbose mode. */
2121 d_advance (di, 1);
2123 ret = d_bare_function_type (di, 1);
2124 if (! d_check_char (di, 'E'))
2125 return NULL;
2126 return ret;
2129 /* <bare-function-type> ::= [J]<type>+ */
2131 static struct demangle_component *
2132 d_bare_function_type (struct d_info *di, int has_return_type)
2134 struct demangle_component *return_type;
2135 struct demangle_component *tl;
2136 struct demangle_component **ptl;
2137 char peek;
2139 /* Detect special qualifier indicating that the first argument
2140 is the return type. */
2141 peek = d_peek_char (di);
2142 if (peek == 'J')
2144 d_advance (di, 1);
2145 has_return_type = 1;
2148 return_type = NULL;
2149 tl = NULL;
2150 ptl = &tl;
2151 while (1)
2153 struct demangle_component *type;
2155 peek = d_peek_char (di);
2156 if (peek == '\0' || peek == 'E')
2157 break;
2158 type = cplus_demangle_type (di);
2159 if (type == NULL)
2160 return NULL;
2161 if (has_return_type)
2163 return_type = type;
2164 has_return_type = 0;
2166 else
2168 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2169 if (*ptl == NULL)
2170 return NULL;
2171 ptl = &d_right (*ptl);
2175 /* There should be at least one parameter type besides the optional
2176 return type. A function which takes no arguments will have a
2177 single parameter type void. */
2178 if (tl == NULL)
2179 return NULL;
2181 /* If we have a single parameter type void, omit it. */
2182 if (d_right (tl) == NULL
2183 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2184 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2186 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2187 tl = NULL;
2190 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2193 /* <class-enum-type> ::= <name> */
2195 static struct demangle_component *
2196 d_class_enum_type (struct d_info *di)
2198 return d_name (di);
2201 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2202 ::= A [<(dimension) expression>] _ <(element) type>
2205 static struct demangle_component *
2206 d_array_type (struct d_info *di)
2208 char peek;
2209 struct demangle_component *dim;
2211 if (! d_check_char (di, 'A'))
2212 return NULL;
2214 peek = d_peek_char (di);
2215 if (peek == '_')
2216 dim = NULL;
2217 else if (IS_DIGIT (peek))
2219 const char *s;
2221 s = d_str (di);
2224 d_advance (di, 1);
2225 peek = d_peek_char (di);
2227 while (IS_DIGIT (peek));
2228 dim = d_make_name (di, s, d_str (di) - s);
2229 if (dim == NULL)
2230 return NULL;
2232 else
2234 dim = d_expression (di);
2235 if (dim == NULL)
2236 return NULL;
2239 if (! d_check_char (di, '_'))
2240 return NULL;
2242 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2243 cplus_demangle_type (di));
2246 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2248 static struct demangle_component *
2249 d_pointer_to_member_type (struct d_info *di)
2251 struct demangle_component *cl;
2252 struct demangle_component *mem;
2253 struct demangle_component **pmem;
2255 if (! d_check_char (di, 'M'))
2256 return NULL;
2258 cl = cplus_demangle_type (di);
2260 /* The ABI specifies that any type can be a substitution source, and
2261 that M is followed by two types, and that when a CV-qualified
2262 type is seen both the base type and the CV-qualified types are
2263 substitution sources. The ABI also specifies that for a pointer
2264 to a CV-qualified member function, the qualifiers are attached to
2265 the second type. Given the grammar, a plain reading of the ABI
2266 suggests that both the CV-qualified member function and the
2267 non-qualified member function are substitution sources. However,
2268 g++ does not work that way. g++ treats only the CV-qualified
2269 member function as a substitution source. FIXME. So to work
2270 with g++, we need to pull off the CV-qualifiers here, in order to
2271 avoid calling add_substitution() in cplus_demangle_type(). But
2272 for a CV-qualified member which is not a function, g++ does
2273 follow the ABI, so we need to handle that case here by calling
2274 d_add_substitution ourselves. */
2276 pmem = d_cv_qualifiers (di, &mem, 1);
2277 if (pmem == NULL)
2278 return NULL;
2279 *pmem = cplus_demangle_type (di);
2280 if (*pmem == NULL)
2281 return NULL;
2283 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2285 if (! d_add_substitution (di, mem))
2286 return NULL;
2289 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2292 /* <template-param> ::= T_
2293 ::= T <(parameter-2 non-negative) number> _
2296 static struct demangle_component *
2297 d_template_param (struct d_info *di)
2299 long param;
2301 if (! d_check_char (di, 'T'))
2302 return NULL;
2304 if (d_peek_char (di) == '_')
2305 param = 0;
2306 else
2308 param = d_number (di);
2309 if (param < 0)
2310 return NULL;
2311 param += 1;
2314 if (! d_check_char (di, '_'))
2315 return NULL;
2317 ++di->did_subs;
2319 return d_make_template_param (di, param);
2322 /* <template-args> ::= I <template-arg>+ E */
2324 static struct demangle_component *
2325 d_template_args (struct d_info *di)
2327 struct demangle_component *hold_last_name;
2328 struct demangle_component *al;
2329 struct demangle_component **pal;
2331 /* Preserve the last name we saw--don't let the template arguments
2332 clobber it, as that would give us the wrong name for a subsequent
2333 constructor or destructor. */
2334 hold_last_name = di->last_name;
2336 if (! d_check_char (di, 'I'))
2337 return NULL;
2339 al = NULL;
2340 pal = &al;
2341 while (1)
2343 struct demangle_component *a;
2345 a = d_template_arg (di);
2346 if (a == NULL)
2347 return NULL;
2349 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2350 if (*pal == NULL)
2351 return NULL;
2352 pal = &d_right (*pal);
2354 if (d_peek_char (di) == 'E')
2356 d_advance (di, 1);
2357 break;
2361 di->last_name = hold_last_name;
2363 return al;
2366 /* <template-arg> ::= <type>
2367 ::= X <expression> E
2368 ::= <expr-primary>
2371 static struct demangle_component *
2372 d_template_arg (struct d_info *di)
2374 struct demangle_component *ret;
2376 switch (d_peek_char (di))
2378 case 'X':
2379 d_advance (di, 1);
2380 ret = d_expression (di);
2381 if (! d_check_char (di, 'E'))
2382 return NULL;
2383 return ret;
2385 case 'L':
2386 return d_expr_primary (di);
2388 default:
2389 return cplus_demangle_type (di);
2393 /* <expression> ::= <(unary) operator-name> <expression>
2394 ::= <(binary) operator-name> <expression> <expression>
2395 ::= <(trinary) operator-name> <expression> <expression> <expression>
2396 ::= st <type>
2397 ::= <template-param>
2398 ::= sr <type> <unqualified-name>
2399 ::= sr <type> <unqualified-name> <template-args>
2400 ::= <expr-primary>
2403 static struct demangle_component *
2404 d_expression (struct d_info *di)
2406 char peek;
2408 peek = d_peek_char (di);
2409 if (peek == 'L')
2410 return d_expr_primary (di);
2411 else if (peek == 'T')
2412 return d_template_param (di);
2413 else if (peek == 's' && d_peek_next_char (di) == 'r')
2415 struct demangle_component *type;
2416 struct demangle_component *name;
2418 d_advance (di, 2);
2419 type = cplus_demangle_type (di);
2420 name = d_unqualified_name (di);
2421 if (d_peek_char (di) != 'I')
2422 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2423 else
2424 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2425 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2426 d_template_args (di)));
2428 else
2430 struct demangle_component *op;
2431 int args;
2433 op = d_operator_name (di);
2434 if (op == NULL)
2435 return NULL;
2437 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2438 di->expansion += op->u.s_operator.op->len - 2;
2440 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2441 && strcmp (op->u.s_operator.op->code, "st") == 0)
2442 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2443 cplus_demangle_type (di));
2445 switch (op->type)
2447 default:
2448 return NULL;
2449 case DEMANGLE_COMPONENT_OPERATOR:
2450 args = op->u.s_operator.op->args;
2451 break;
2452 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2453 args = op->u.s_extended_operator.args;
2454 break;
2455 case DEMANGLE_COMPONENT_CAST:
2456 args = 1;
2457 break;
2460 switch (args)
2462 case 1:
2463 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2464 d_expression (di));
2465 case 2:
2467 struct demangle_component *left;
2469 left = d_expression (di);
2470 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2471 d_make_comp (di,
2472 DEMANGLE_COMPONENT_BINARY_ARGS,
2473 left,
2474 d_expression (di)));
2476 case 3:
2478 struct demangle_component *first;
2479 struct demangle_component *second;
2481 first = d_expression (di);
2482 second = d_expression (di);
2483 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2484 d_make_comp (di,
2485 DEMANGLE_COMPONENT_TRINARY_ARG1,
2486 first,
2487 d_make_comp (di,
2488 DEMANGLE_COMPONENT_TRINARY_ARG2,
2489 second,
2490 d_expression (di))));
2492 default:
2493 return NULL;
2498 /* <expr-primary> ::= L <type> <(value) number> E
2499 ::= L <type> <(value) float> E
2500 ::= L <mangled-name> E
2503 static struct demangle_component *
2504 d_expr_primary (struct d_info *di)
2506 struct demangle_component *ret;
2508 if (! d_check_char (di, 'L'))
2509 return NULL;
2510 if (d_peek_char (di) == '_')
2511 ret = cplus_demangle_mangled_name (di, 0);
2512 else
2514 struct demangle_component *type;
2515 enum demangle_component_type t;
2516 const char *s;
2518 type = cplus_demangle_type (di);
2519 if (type == NULL)
2520 return NULL;
2522 /* If we have a type we know how to print, we aren't going to
2523 print the type name itself. */
2524 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2525 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2526 di->expansion -= type->u.s_builtin.type->len;
2528 /* Rather than try to interpret the literal value, we just
2529 collect it as a string. Note that it's possible to have a
2530 floating point literal here. The ABI specifies that the
2531 format of such literals is machine independent. That's fine,
2532 but what's not fine is that versions of g++ up to 3.2 with
2533 -fabi-version=1 used upper case letters in the hex constant,
2534 and dumped out gcc's internal representation. That makes it
2535 hard to tell where the constant ends, and hard to dump the
2536 constant in any readable form anyhow. We don't attempt to
2537 handle these cases. */
2539 t = DEMANGLE_COMPONENT_LITERAL;
2540 if (d_peek_char (di) == 'n')
2542 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2543 d_advance (di, 1);
2545 s = d_str (di);
2546 while (d_peek_char (di) != 'E')
2548 if (d_peek_char (di) == '\0')
2549 return NULL;
2550 d_advance (di, 1);
2552 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2554 if (! d_check_char (di, 'E'))
2555 return NULL;
2556 return ret;
2559 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2560 ::= Z <(function) encoding> E s [<discriminator>]
2563 static struct demangle_component *
2564 d_local_name (struct d_info *di)
2566 struct demangle_component *function;
2568 if (! d_check_char (di, 'Z'))
2569 return NULL;
2571 function = d_encoding (di, 0);
2573 if (! d_check_char (di, 'E'))
2574 return NULL;
2576 if (d_peek_char (di) == 's')
2578 d_advance (di, 1);
2579 if (! d_discriminator (di))
2580 return NULL;
2581 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2582 d_make_name (di, "string literal",
2583 sizeof "string literal" - 1));
2585 else
2587 struct demangle_component *name;
2589 name = d_name (di);
2590 if (! d_discriminator (di))
2591 return NULL;
2592 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2596 /* <discriminator> ::= _ <(non-negative) number>
2598 We demangle the discriminator, but we don't print it out. FIXME:
2599 We should print it out in verbose mode. */
2601 static int
2602 d_discriminator (struct d_info *di)
2604 long discrim;
2606 if (d_peek_char (di) != '_')
2607 return 1;
2608 d_advance (di, 1);
2609 discrim = d_number (di);
2610 if (discrim < 0)
2611 return 0;
2612 return 1;
2615 /* Add a new substitution. */
2617 static int
2618 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2620 if (dc == NULL)
2621 return 0;
2622 if (di->next_sub >= di->num_subs)
2623 return 0;
2624 di->subs[di->next_sub] = dc;
2625 ++di->next_sub;
2626 return 1;
2629 /* <substitution> ::= S <seq-id> _
2630 ::= S_
2631 ::= St
2632 ::= Sa
2633 ::= Sb
2634 ::= Ss
2635 ::= Si
2636 ::= So
2637 ::= Sd
2639 If PREFIX is non-zero, then this type is being used as a prefix in
2640 a qualified name. In this case, for the standard substitutions, we
2641 need to check whether we are being used as a prefix for a
2642 constructor or destructor, and return a full template name.
2643 Otherwise we will get something like std::iostream::~iostream()
2644 which does not correspond particularly well to any function which
2645 actually appears in the source.
2648 static const struct d_standard_sub_info standard_subs[] =
2650 { 't', NL ("std"),
2651 NL ("std"),
2652 NULL, 0 },
2653 { 'a', NL ("std::allocator"),
2654 NL ("std::allocator"),
2655 NL ("allocator") },
2656 { 'b', NL ("std::basic_string"),
2657 NL ("std::basic_string"),
2658 NL ("basic_string") },
2659 { 's', NL ("std::string"),
2660 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2661 NL ("basic_string") },
2662 { 'i', NL ("std::istream"),
2663 NL ("std::basic_istream<char, std::char_traits<char> >"),
2664 NL ("basic_istream") },
2665 { 'o', NL ("std::ostream"),
2666 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2667 NL ("basic_ostream") },
2668 { 'd', NL ("std::iostream"),
2669 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2670 NL ("basic_iostream") }
2673 static struct demangle_component *
2674 d_substitution (struct d_info *di, int prefix)
2676 char c;
2678 if (! d_check_char (di, 'S'))
2679 return NULL;
2681 c = d_next_char (di);
2682 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2684 int id;
2686 id = 0;
2687 if (c != '_')
2691 if (IS_DIGIT (c))
2692 id = id * 36 + c - '0';
2693 else if (IS_UPPER (c))
2694 id = id * 36 + c - 'A' + 10;
2695 else
2696 return NULL;
2697 if (id < 0)
2698 return NULL;
2699 c = d_next_char (di);
2701 while (c != '_');
2703 ++id;
2706 if (id >= di->next_sub)
2707 return NULL;
2709 ++di->did_subs;
2711 return di->subs[id];
2713 else
2715 int verbose;
2716 const struct d_standard_sub_info *p;
2717 const struct d_standard_sub_info *pend;
2719 verbose = (di->options & DMGL_VERBOSE) != 0;
2720 if (! verbose && prefix)
2722 char peek;
2724 peek = d_peek_char (di);
2725 if (peek == 'C' || peek == 'D')
2726 verbose = 1;
2729 pend = (&standard_subs[0]
2730 + sizeof standard_subs / sizeof standard_subs[0]);
2731 for (p = &standard_subs[0]; p < pend; ++p)
2733 if (c == p->code)
2735 const char *s;
2736 int len;
2738 if (p->set_last_name != NULL)
2739 di->last_name = d_make_sub (di, p->set_last_name,
2740 p->set_last_name_len);
2741 if (verbose)
2743 s = p->full_expansion;
2744 len = p->full_len;
2746 else
2748 s = p->simple_expansion;
2749 len = p->simple_len;
2751 di->expansion += len;
2752 return d_make_sub (di, s, len);
2756 return NULL;
2760 /* Initialize a growable string. */
2762 static void
2763 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
2765 dgs->buf = NULL;
2766 dgs->len = 0;
2767 dgs->alc = 0;
2768 dgs->allocation_failure = 0;
2770 if (estimate > 0)
2771 d_growable_string_resize (dgs, estimate);
2774 /* Grow a growable string to a given size. */
2776 static inline void
2777 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
2779 size_t newalc;
2780 char *newbuf;
2782 if (dgs->allocation_failure)
2783 return;
2785 /* Start allocation at two bytes to avoid any possibility of confusion
2786 with the special value of 1 used as a return in *palc to indicate
2787 allocation failures. */
2788 newalc = dgs->alc > 0 ? dgs->alc : 2;
2789 while (newalc < need)
2790 newalc <<= 1;
2792 newbuf = (char *) realloc (dgs->buf, newalc);
2793 if (newbuf == NULL)
2795 free (dgs->buf);
2796 dgs->buf = NULL;
2797 dgs->len = 0;
2798 dgs->alc = 0;
2799 dgs->allocation_failure = 1;
2800 return;
2802 dgs->buf = newbuf;
2803 dgs->alc = newalc;
2806 /* Append a buffer to a growable string. */
2808 static inline void
2809 d_growable_string_append_buffer (struct d_growable_string *dgs,
2810 const char *s, size_t l)
2812 size_t need;
2814 need = dgs->len + l + 1;
2815 if (need > dgs->alc)
2816 d_growable_string_resize (dgs, need);
2818 if (dgs->allocation_failure)
2819 return;
2821 memcpy (dgs->buf + dgs->len, s, l);
2822 dgs->buf[dgs->len + l] = '\0';
2823 dgs->len += l;
2826 /* Bridge growable strings to the callback mechanism. */
2828 static void
2829 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
2831 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
2833 d_growable_string_append_buffer (dgs, s, l);
2836 /* Initialize a print information structure. */
2838 static void
2839 d_print_init (struct d_print_info *dpi, int options,
2840 demangle_callbackref callback, void *opaque)
2842 dpi->options = options;
2843 dpi->len = 0;
2844 dpi->last_char = '\0';
2845 dpi->templates = NULL;
2846 dpi->modifiers = NULL;
2848 dpi->callback = callback;
2849 dpi->opaque = opaque;
2851 dpi->demangle_failure = 0;
2854 /* Indicate that an error occurred during printing, and test for error. */
2856 static inline void
2857 d_print_error (struct d_print_info *dpi)
2859 dpi->demangle_failure = 1;
2862 static inline int
2863 d_print_saw_error (struct d_print_info *dpi)
2865 return dpi->demangle_failure != 0;
2868 /* Flush buffered characters to the callback. */
2870 static inline void
2871 d_print_flush (struct d_print_info *dpi)
2873 dpi->buf[dpi->len] = '\0';
2874 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
2875 dpi->len = 0;
2878 /* Append characters and buffers for printing. */
2880 static inline void
2881 d_append_char (struct d_print_info *dpi, char c)
2883 if (dpi->len == sizeof (dpi->buf) - 1)
2884 d_print_flush (dpi);
2886 dpi->buf[dpi->len++] = c;
2887 dpi->last_char = c;
2890 static inline void
2891 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
2893 size_t i;
2895 for (i = 0; i < l; i++)
2896 d_append_char (dpi, s[i]);
2899 static inline void
2900 d_append_string (struct d_print_info *dpi, const char *s)
2902 d_append_buffer (dpi, s, strlen (s));
2905 static inline char
2906 d_last_char (struct d_print_info *dpi)
2908 return dpi->last_char;
2911 /* Turn components into a human readable string. OPTIONS is the
2912 options bits passed to the demangler. DC is the tree to print.
2913 CALLBACK is a function to call to flush demangled string segments
2914 as they fill the intermediate buffer, and OPAQUE is a generalized
2915 callback argument. On success, this returns 1. On failure,
2916 it returns 0, indicating a bad parse. It does not use heap
2917 memory to build an output string, so cannot encounter memory
2918 allocation failure. */
2920 CP_STATIC_IF_GLIBCPP_V3
2922 cplus_demangle_print_callback (int options,
2923 const struct demangle_component *dc,
2924 demangle_callbackref callback, void *opaque)
2926 struct d_print_info dpi;
2928 d_print_init (&dpi, options, callback, opaque);
2930 d_print_comp (&dpi, dc);
2932 d_print_flush (&dpi);
2934 return ! d_print_saw_error (&dpi);
2937 /* Turn components into a human readable string. OPTIONS is the
2938 options bits passed to the demangler. DC is the tree to print.
2939 ESTIMATE is a guess at the length of the result. This returns a
2940 string allocated by malloc, or NULL on error. On success, this
2941 sets *PALC to the size of the allocated buffer. On failure, this
2942 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2943 failure. */
2945 CP_STATIC_IF_GLIBCPP_V3
2946 char *
2947 cplus_demangle_print (int options, const struct demangle_component *dc,
2948 int estimate, size_t *palc)
2950 struct d_growable_string dgs;
2952 d_growable_string_init (&dgs, estimate);
2954 if (! cplus_demangle_print_callback (options, dc,
2955 d_growable_string_callback_adapter,
2956 &dgs))
2958 free (dgs.buf);
2959 *palc = 0;
2960 return NULL;
2963 *palc = dgs.allocation_failure ? 1 : dgs.alc;
2964 return dgs.buf;
2967 /* Subroutine to handle components. */
2969 static void
2970 d_print_comp (struct d_print_info *dpi,
2971 const struct demangle_component *dc)
2973 if (dc == NULL)
2975 d_print_error (dpi);
2976 return;
2978 if (d_print_saw_error (dpi))
2979 return;
2981 switch (dc->type)
2983 case DEMANGLE_COMPONENT_NAME:
2984 if ((dpi->options & DMGL_JAVA) == 0)
2985 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2986 else
2987 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2988 return;
2990 case DEMANGLE_COMPONENT_QUAL_NAME:
2991 case DEMANGLE_COMPONENT_LOCAL_NAME:
2992 d_print_comp (dpi, d_left (dc));
2993 if ((dpi->options & DMGL_JAVA) == 0)
2994 d_append_string (dpi, "::");
2995 else
2996 d_append_char (dpi, '.');
2997 d_print_comp (dpi, d_right (dc));
2998 return;
3000 case DEMANGLE_COMPONENT_TYPED_NAME:
3002 struct d_print_mod *hold_modifiers;
3003 struct demangle_component *typed_name;
3004 struct d_print_mod adpm[4];
3005 unsigned int i;
3006 struct d_print_template dpt;
3008 /* Pass the name down to the type so that it can be printed in
3009 the right place for the type. We also have to pass down
3010 any CV-qualifiers, which apply to the this parameter. */
3011 hold_modifiers = dpi->modifiers;
3012 i = 0;
3013 typed_name = d_left (dc);
3014 while (typed_name != NULL)
3016 if (i >= sizeof adpm / sizeof adpm[0])
3018 d_print_error (dpi);
3019 return;
3022 adpm[i].next = dpi->modifiers;
3023 dpi->modifiers = &adpm[i];
3024 adpm[i].mod = typed_name;
3025 adpm[i].printed = 0;
3026 adpm[i].templates = dpi->templates;
3027 ++i;
3029 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3030 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3031 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3032 break;
3034 typed_name = d_left (typed_name);
3037 if (typed_name == NULL)
3039 d_print_error (dpi);
3040 return;
3043 /* If typed_name is a template, then it applies to the
3044 function type as well. */
3045 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3047 dpt.next = dpi->templates;
3048 dpi->templates = &dpt;
3049 dpt.template_decl = typed_name;
3052 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3053 there may be CV-qualifiers on its right argument which
3054 really apply here; this happens when parsing a class which
3055 is local to a function. */
3056 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3058 struct demangle_component *local_name;
3060 local_name = d_right (typed_name);
3061 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3062 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3063 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3065 if (i >= sizeof adpm / sizeof adpm[0])
3067 d_print_error (dpi);
3068 return;
3071 adpm[i] = adpm[i - 1];
3072 adpm[i].next = &adpm[i - 1];
3073 dpi->modifiers = &adpm[i];
3075 adpm[i - 1].mod = local_name;
3076 adpm[i - 1].printed = 0;
3077 adpm[i - 1].templates = dpi->templates;
3078 ++i;
3080 local_name = d_left (local_name);
3084 d_print_comp (dpi, d_right (dc));
3086 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3087 dpi->templates = dpt.next;
3089 /* If the modifiers didn't get printed by the type, print them
3090 now. */
3091 while (i > 0)
3093 --i;
3094 if (! adpm[i].printed)
3096 d_append_char (dpi, ' ');
3097 d_print_mod (dpi, adpm[i].mod);
3101 dpi->modifiers = hold_modifiers;
3103 return;
3106 case DEMANGLE_COMPONENT_TEMPLATE:
3108 struct d_print_mod *hold_dpm;
3109 struct demangle_component *dcl;
3111 /* Don't push modifiers into a template definition. Doing so
3112 could give the wrong definition for a template argument.
3113 Instead, treat the template essentially as a name. */
3115 hold_dpm = dpi->modifiers;
3116 dpi->modifiers = NULL;
3118 dcl = d_left (dc);
3120 if ((dpi->options & DMGL_JAVA) != 0
3121 && dcl->type == DEMANGLE_COMPONENT_NAME
3122 && dcl->u.s_name.len == 6
3123 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3125 /* Special-case Java arrays, so that JArray<TYPE> appears
3126 instead as TYPE[]. */
3128 d_print_comp (dpi, d_right (dc));
3129 d_append_string (dpi, "[]");
3131 else
3133 d_print_comp (dpi, dcl);
3134 if (d_last_char (dpi) == '<')
3135 d_append_char (dpi, ' ');
3136 d_append_char (dpi, '<');
3137 d_print_comp (dpi, d_right (dc));
3138 /* Avoid generating two consecutive '>' characters, to avoid
3139 the C++ syntactic ambiguity. */
3140 if (d_last_char (dpi) == '>')
3141 d_append_char (dpi, ' ');
3142 d_append_char (dpi, '>');
3145 dpi->modifiers = hold_dpm;
3147 return;
3150 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3152 long i;
3153 struct demangle_component *a;
3154 struct d_print_template *hold_dpt;
3156 if (dpi->templates == NULL)
3158 d_print_error (dpi);
3159 return;
3161 i = dc->u.s_number.number;
3162 for (a = d_right (dpi->templates->template_decl);
3163 a != NULL;
3164 a = d_right (a))
3166 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3168 d_print_error (dpi);
3169 return;
3171 if (i <= 0)
3172 break;
3173 --i;
3175 if (i != 0 || a == NULL)
3177 d_print_error (dpi);
3178 return;
3181 /* While processing this parameter, we need to pop the list of
3182 templates. This is because the template parameter may
3183 itself be a reference to a parameter of an outer
3184 template. */
3186 hold_dpt = dpi->templates;
3187 dpi->templates = hold_dpt->next;
3189 d_print_comp (dpi, d_left (a));
3191 dpi->templates = hold_dpt;
3193 return;
3196 case DEMANGLE_COMPONENT_CTOR:
3197 d_print_comp (dpi, dc->u.s_ctor.name);
3198 return;
3200 case DEMANGLE_COMPONENT_DTOR:
3201 d_append_char (dpi, '~');
3202 d_print_comp (dpi, dc->u.s_dtor.name);
3203 return;
3205 case DEMANGLE_COMPONENT_VTABLE:
3206 d_append_string (dpi, "vtable for ");
3207 d_print_comp (dpi, d_left (dc));
3208 return;
3210 case DEMANGLE_COMPONENT_VTT:
3211 d_append_string (dpi, "VTT for ");
3212 d_print_comp (dpi, d_left (dc));
3213 return;
3215 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3216 d_append_string (dpi, "construction vtable for ");
3217 d_print_comp (dpi, d_left (dc));
3218 d_append_string (dpi, "-in-");
3219 d_print_comp (dpi, d_right (dc));
3220 return;
3222 case DEMANGLE_COMPONENT_TYPEINFO:
3223 d_append_string (dpi, "typeinfo for ");
3224 d_print_comp (dpi, d_left (dc));
3225 return;
3227 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3228 d_append_string (dpi, "typeinfo name for ");
3229 d_print_comp (dpi, d_left (dc));
3230 return;
3232 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3233 d_append_string (dpi, "typeinfo fn for ");
3234 d_print_comp (dpi, d_left (dc));
3235 return;
3237 case DEMANGLE_COMPONENT_THUNK:
3238 d_append_string (dpi, "non-virtual thunk to ");
3239 d_print_comp (dpi, d_left (dc));
3240 return;
3242 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3243 d_append_string (dpi, "virtual thunk to ");
3244 d_print_comp (dpi, d_left (dc));
3245 return;
3247 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3248 d_append_string (dpi, "covariant return thunk to ");
3249 d_print_comp (dpi, d_left (dc));
3250 return;
3252 case DEMANGLE_COMPONENT_JAVA_CLASS:
3253 d_append_string (dpi, "java Class for ");
3254 d_print_comp (dpi, d_left (dc));
3255 return;
3257 case DEMANGLE_COMPONENT_GUARD:
3258 d_append_string (dpi, "guard variable for ");
3259 d_print_comp (dpi, d_left (dc));
3260 return;
3262 case DEMANGLE_COMPONENT_REFTEMP:
3263 d_append_string (dpi, "reference temporary for ");
3264 d_print_comp (dpi, d_left (dc));
3265 return;
3267 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3268 d_append_string (dpi, "hidden alias for ");
3269 d_print_comp (dpi, d_left (dc));
3270 return;
3272 case DEMANGLE_COMPONENT_SUB_STD:
3273 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3274 return;
3276 case DEMANGLE_COMPONENT_RESTRICT:
3277 case DEMANGLE_COMPONENT_VOLATILE:
3278 case DEMANGLE_COMPONENT_CONST:
3280 struct d_print_mod *pdpm;
3282 /* When printing arrays, it's possible to have cases where the
3283 same CV-qualifier gets pushed on the stack multiple times.
3284 We only need to print it once. */
3286 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3288 if (! pdpm->printed)
3290 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3291 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3292 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3293 break;
3294 if (pdpm->mod->type == dc->type)
3296 d_print_comp (dpi, d_left (dc));
3297 return;
3302 /* Fall through. */
3303 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3304 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3305 case DEMANGLE_COMPONENT_CONST_THIS:
3306 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3307 case DEMANGLE_COMPONENT_POINTER:
3308 case DEMANGLE_COMPONENT_REFERENCE:
3309 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3310 case DEMANGLE_COMPONENT_COMPLEX:
3311 case DEMANGLE_COMPONENT_IMAGINARY:
3313 /* We keep a list of modifiers on the stack. */
3314 struct d_print_mod dpm;
3316 dpm.next = dpi->modifiers;
3317 dpi->modifiers = &dpm;
3318 dpm.mod = dc;
3319 dpm.printed = 0;
3320 dpm.templates = dpi->templates;
3322 d_print_comp (dpi, d_left (dc));
3324 /* If the modifier didn't get printed by the type, print it
3325 now. */
3326 if (! dpm.printed)
3327 d_print_mod (dpi, dc);
3329 dpi->modifiers = dpm.next;
3331 return;
3334 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3335 if ((dpi->options & DMGL_JAVA) == 0)
3336 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3337 dc->u.s_builtin.type->len);
3338 else
3339 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3340 dc->u.s_builtin.type->java_len);
3341 return;
3343 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3344 d_print_comp (dpi, d_left (dc));
3345 return;
3347 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3349 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3350 d_print_function_type (dpi, dc, dpi->modifiers);
3352 /* Print return type if present */
3353 if (d_left (dc) != NULL)
3355 struct d_print_mod dpm;
3357 /* We must pass this type down as a modifier in order to
3358 print it in the right location. */
3359 dpm.next = dpi->modifiers;
3360 dpi->modifiers = &dpm;
3361 dpm.mod = dc;
3362 dpm.printed = 0;
3363 dpm.templates = dpi->templates;
3365 d_print_comp (dpi, d_left (dc));
3367 dpi->modifiers = dpm.next;
3369 if (dpm.printed)
3370 return;
3372 /* In standard prefix notation, there is a space between the
3373 return type and the function signature. */
3374 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3375 d_append_char (dpi, ' ');
3378 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3379 d_print_function_type (dpi, dc, dpi->modifiers);
3381 return;
3384 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3386 struct d_print_mod *hold_modifiers;
3387 struct d_print_mod adpm[4];
3388 unsigned int i;
3389 struct d_print_mod *pdpm;
3391 /* We must pass this type down as a modifier in order to print
3392 multi-dimensional arrays correctly. If the array itself is
3393 CV-qualified, we act as though the element type were
3394 CV-qualified. We do this by copying the modifiers down
3395 rather than fiddling pointers, so that we don't wind up
3396 with a d_print_mod higher on the stack pointing into our
3397 stack frame after we return. */
3399 hold_modifiers = dpi->modifiers;
3401 adpm[0].next = hold_modifiers;
3402 dpi->modifiers = &adpm[0];
3403 adpm[0].mod = dc;
3404 adpm[0].printed = 0;
3405 adpm[0].templates = dpi->templates;
3407 i = 1;
3408 pdpm = hold_modifiers;
3409 while (pdpm != NULL
3410 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3411 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3412 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3414 if (! pdpm->printed)
3416 if (i >= sizeof adpm / sizeof adpm[0])
3418 d_print_error (dpi);
3419 return;
3422 adpm[i] = *pdpm;
3423 adpm[i].next = dpi->modifiers;
3424 dpi->modifiers = &adpm[i];
3425 pdpm->printed = 1;
3426 ++i;
3429 pdpm = pdpm->next;
3432 d_print_comp (dpi, d_right (dc));
3434 dpi->modifiers = hold_modifiers;
3436 if (adpm[0].printed)
3437 return;
3439 while (i > 1)
3441 --i;
3442 d_print_mod (dpi, adpm[i].mod);
3445 d_print_array_type (dpi, dc, dpi->modifiers);
3447 return;
3450 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3452 struct d_print_mod dpm;
3454 dpm.next = dpi->modifiers;
3455 dpi->modifiers = &dpm;
3456 dpm.mod = dc;
3457 dpm.printed = 0;
3458 dpm.templates = dpi->templates;
3460 d_print_comp (dpi, d_right (dc));
3462 /* If the modifier didn't get printed by the type, print it
3463 now. */
3464 if (! dpm.printed)
3466 d_append_char (dpi, ' ');
3467 d_print_comp (dpi, d_left (dc));
3468 d_append_string (dpi, "::*");
3471 dpi->modifiers = dpm.next;
3473 return;
3476 case DEMANGLE_COMPONENT_ARGLIST:
3477 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3478 d_print_comp (dpi, d_left (dc));
3479 if (d_right (dc) != NULL)
3481 d_append_string (dpi, ", ");
3482 d_print_comp (dpi, d_right (dc));
3484 return;
3486 case DEMANGLE_COMPONENT_OPERATOR:
3488 char c;
3490 d_append_string (dpi, "operator");
3491 c = dc->u.s_operator.op->name[0];
3492 if (IS_LOWER (c))
3493 d_append_char (dpi, ' ');
3494 d_append_buffer (dpi, dc->u.s_operator.op->name,
3495 dc->u.s_operator.op->len);
3496 return;
3499 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3500 d_append_string (dpi, "operator ");
3501 d_print_comp (dpi, dc->u.s_extended_operator.name);
3502 return;
3504 case DEMANGLE_COMPONENT_CAST:
3505 d_append_string (dpi, "operator ");
3506 d_print_cast (dpi, dc);
3507 return;
3509 case DEMANGLE_COMPONENT_UNARY:
3510 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3511 d_print_expr_op (dpi, d_left (dc));
3512 else
3514 d_append_char (dpi, '(');
3515 d_print_cast (dpi, d_left (dc));
3516 d_append_char (dpi, ')');
3518 d_append_char (dpi, '(');
3519 d_print_comp (dpi, d_right (dc));
3520 d_append_char (dpi, ')');
3521 return;
3523 case DEMANGLE_COMPONENT_BINARY:
3524 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3526 d_print_error (dpi);
3527 return;
3530 /* We wrap an expression which uses the greater-than operator in
3531 an extra layer of parens so that it does not get confused
3532 with the '>' which ends the template parameters. */
3533 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3534 && d_left (dc)->u.s_operator.op->len == 1
3535 && d_left (dc)->u.s_operator.op->name[0] == '>')
3536 d_append_char (dpi, '(');
3538 d_append_char (dpi, '(');
3539 d_print_comp (dpi, d_left (d_right (dc)));
3540 d_append_string (dpi, ") ");
3541 d_print_expr_op (dpi, d_left (dc));
3542 d_append_string (dpi, " (");
3543 d_print_comp (dpi, d_right (d_right (dc)));
3544 d_append_char (dpi, ')');
3546 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3547 && d_left (dc)->u.s_operator.op->len == 1
3548 && d_left (dc)->u.s_operator.op->name[0] == '>')
3549 d_append_char (dpi, ')');
3551 return;
3553 case DEMANGLE_COMPONENT_BINARY_ARGS:
3554 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3555 d_print_error (dpi);
3556 return;
3558 case DEMANGLE_COMPONENT_TRINARY:
3559 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3560 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3562 d_print_error (dpi);
3563 return;
3565 d_append_char (dpi, '(');
3566 d_print_comp (dpi, d_left (d_right (dc)));
3567 d_append_string (dpi, ") ");
3568 d_print_expr_op (dpi, d_left (dc));
3569 d_append_string (dpi, " (");
3570 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3571 d_append_string (dpi, ") : (");
3572 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3573 d_append_char (dpi, ')');
3574 return;
3576 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3577 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3578 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3579 d_print_error (dpi);
3580 return;
3582 case DEMANGLE_COMPONENT_LITERAL:
3583 case DEMANGLE_COMPONENT_LITERAL_NEG:
3585 enum d_builtin_type_print tp;
3587 /* For some builtin types, produce simpler output. */
3588 tp = D_PRINT_DEFAULT;
3589 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3591 tp = d_left (dc)->u.s_builtin.type->print;
3592 switch (tp)
3594 case D_PRINT_INT:
3595 case D_PRINT_UNSIGNED:
3596 case D_PRINT_LONG:
3597 case D_PRINT_UNSIGNED_LONG:
3598 case D_PRINT_LONG_LONG:
3599 case D_PRINT_UNSIGNED_LONG_LONG:
3600 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3602 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3603 d_append_char (dpi, '-');
3604 d_print_comp (dpi, d_right (dc));
3605 switch (tp)
3607 default:
3608 break;
3609 case D_PRINT_UNSIGNED:
3610 d_append_char (dpi, 'u');
3611 break;
3612 case D_PRINT_LONG:
3613 d_append_char (dpi, 'l');
3614 break;
3615 case D_PRINT_UNSIGNED_LONG:
3616 d_append_string (dpi, "ul");
3617 break;
3618 case D_PRINT_LONG_LONG:
3619 d_append_string (dpi, "ll");
3620 break;
3621 case D_PRINT_UNSIGNED_LONG_LONG:
3622 d_append_string (dpi, "ull");
3623 break;
3625 return;
3627 break;
3629 case D_PRINT_BOOL:
3630 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3631 && d_right (dc)->u.s_name.len == 1
3632 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3634 switch (d_right (dc)->u.s_name.s[0])
3636 case '0':
3637 d_append_string (dpi, "false");
3638 return;
3639 case '1':
3640 d_append_string (dpi, "true");
3641 return;
3642 default:
3643 break;
3646 break;
3648 default:
3649 break;
3653 d_append_char (dpi, '(');
3654 d_print_comp (dpi, d_left (dc));
3655 d_append_char (dpi, ')');
3656 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3657 d_append_char (dpi, '-');
3658 if (tp == D_PRINT_FLOAT)
3659 d_append_char (dpi, '[');
3660 d_print_comp (dpi, d_right (dc));
3661 if (tp == D_PRINT_FLOAT)
3662 d_append_char (dpi, ']');
3664 return;
3666 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3667 d_append_string (dpi, "java resource ");
3668 d_print_comp (dpi, d_left (dc));
3669 return;
3671 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3672 d_print_comp (dpi, d_left (dc));
3673 d_print_comp (dpi, d_right (dc));
3674 return;
3676 case DEMANGLE_COMPONENT_CHARACTER:
3677 d_append_char (dpi, dc->u.s_character.character);
3678 return;
3680 default:
3681 d_print_error (dpi);
3682 return;
3686 /* Print a Java dentifier. For Java we try to handle encoded extended
3687 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3688 so we don't it for C++. Characters are encoded as
3689 __U<hex-char>+_. */
3691 static void
3692 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3694 const char *p;
3695 const char *end;
3697 end = name + len;
3698 for (p = name; p < end; ++p)
3700 if (end - p > 3
3701 && p[0] == '_'
3702 && p[1] == '_'
3703 && p[2] == 'U')
3705 unsigned long c;
3706 const char *q;
3708 c = 0;
3709 for (q = p + 3; q < end; ++q)
3711 int dig;
3713 if (IS_DIGIT (*q))
3714 dig = *q - '0';
3715 else if (*q >= 'A' && *q <= 'F')
3716 dig = *q - 'A' + 10;
3717 else if (*q >= 'a' && *q <= 'f')
3718 dig = *q - 'a' + 10;
3719 else
3720 break;
3722 c = c * 16 + dig;
3724 /* If the Unicode character is larger than 256, we don't try
3725 to deal with it here. FIXME. */
3726 if (q < end && *q == '_' && c < 256)
3728 d_append_char (dpi, c);
3729 p = q;
3730 continue;
3734 d_append_char (dpi, *p);
3738 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3739 qualifiers on this after printing a function. */
3741 static void
3742 d_print_mod_list (struct d_print_info *dpi,
3743 struct d_print_mod *mods, int suffix)
3745 struct d_print_template *hold_dpt;
3747 if (mods == NULL || d_print_saw_error (dpi))
3748 return;
3750 if (mods->printed
3751 || (! suffix
3752 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3753 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3754 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3756 d_print_mod_list (dpi, mods->next, suffix);
3757 return;
3760 mods->printed = 1;
3762 hold_dpt = dpi->templates;
3763 dpi->templates = mods->templates;
3765 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3767 d_print_function_type (dpi, mods->mod, mods->next);
3768 dpi->templates = hold_dpt;
3769 return;
3771 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3773 d_print_array_type (dpi, mods->mod, mods->next);
3774 dpi->templates = hold_dpt;
3775 return;
3777 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3779 struct d_print_mod *hold_modifiers;
3780 struct demangle_component *dc;
3782 /* When this is on the modifier stack, we have pulled any
3783 qualifiers off the right argument already. Otherwise, we
3784 print it as usual, but don't let the left argument see any
3785 modifiers. */
3787 hold_modifiers = dpi->modifiers;
3788 dpi->modifiers = NULL;
3789 d_print_comp (dpi, d_left (mods->mod));
3790 dpi->modifiers = hold_modifiers;
3792 if ((dpi->options & DMGL_JAVA) == 0)
3793 d_append_string (dpi, "::");
3794 else
3795 d_append_char (dpi, '.');
3797 dc = d_right (mods->mod);
3798 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3799 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3800 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3801 dc = d_left (dc);
3803 d_print_comp (dpi, dc);
3805 dpi->templates = hold_dpt;
3806 return;
3809 d_print_mod (dpi, mods->mod);
3811 dpi->templates = hold_dpt;
3813 d_print_mod_list (dpi, mods->next, suffix);
3816 /* Print a modifier. */
3818 static void
3819 d_print_mod (struct d_print_info *dpi,
3820 const struct demangle_component *mod)
3822 switch (mod->type)
3824 case DEMANGLE_COMPONENT_RESTRICT:
3825 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3826 d_append_string (dpi, " restrict");
3827 return;
3828 case DEMANGLE_COMPONENT_VOLATILE:
3829 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3830 d_append_string (dpi, " volatile");
3831 return;
3832 case DEMANGLE_COMPONENT_CONST:
3833 case DEMANGLE_COMPONENT_CONST_THIS:
3834 d_append_string (dpi, " const");
3835 return;
3836 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3837 d_append_char (dpi, ' ');
3838 d_print_comp (dpi, d_right (mod));
3839 return;
3840 case DEMANGLE_COMPONENT_POINTER:
3841 /* There is no pointer symbol in Java. */
3842 if ((dpi->options & DMGL_JAVA) == 0)
3843 d_append_char (dpi, '*');
3844 return;
3845 case DEMANGLE_COMPONENT_REFERENCE:
3846 d_append_char (dpi, '&');
3847 return;
3848 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3849 d_append_string (dpi, "&&");
3850 return;
3851 case DEMANGLE_COMPONENT_COMPLEX:
3852 d_append_string (dpi, "complex ");
3853 return;
3854 case DEMANGLE_COMPONENT_IMAGINARY:
3855 d_append_string (dpi, "imaginary ");
3856 return;
3857 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3858 if (d_last_char (dpi) != '(')
3859 d_append_char (dpi, ' ');
3860 d_print_comp (dpi, d_left (mod));
3861 d_append_string (dpi, "::*");
3862 return;
3863 case DEMANGLE_COMPONENT_TYPED_NAME:
3864 d_print_comp (dpi, d_left (mod));
3865 return;
3866 default:
3867 /* Otherwise, we have something that won't go back on the
3868 modifier stack, so we can just print it. */
3869 d_print_comp (dpi, mod);
3870 return;
3874 /* Print a function type, except for the return type. */
3876 static void
3877 d_print_function_type (struct d_print_info *dpi,
3878 const struct demangle_component *dc,
3879 struct d_print_mod *mods)
3881 int need_paren;
3882 int saw_mod;
3883 int need_space;
3884 struct d_print_mod *p;
3885 struct d_print_mod *hold_modifiers;
3887 need_paren = 0;
3888 saw_mod = 0;
3889 need_space = 0;
3890 for (p = mods; p != NULL; p = p->next)
3892 if (p->printed)
3893 break;
3895 saw_mod = 1;
3896 switch (p->mod->type)
3898 case DEMANGLE_COMPONENT_POINTER:
3899 case DEMANGLE_COMPONENT_REFERENCE:
3900 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3901 need_paren = 1;
3902 break;
3903 case DEMANGLE_COMPONENT_RESTRICT:
3904 case DEMANGLE_COMPONENT_VOLATILE:
3905 case DEMANGLE_COMPONENT_CONST:
3906 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3907 case DEMANGLE_COMPONENT_COMPLEX:
3908 case DEMANGLE_COMPONENT_IMAGINARY:
3909 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3910 need_space = 1;
3911 need_paren = 1;
3912 break;
3913 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3914 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3915 case DEMANGLE_COMPONENT_CONST_THIS:
3916 break;
3917 default:
3918 break;
3920 if (need_paren)
3921 break;
3924 if (d_left (dc) != NULL && ! saw_mod)
3925 need_paren = 1;
3927 if (need_paren)
3929 if (! need_space)
3931 if (d_last_char (dpi) != '('
3932 && d_last_char (dpi) != '*')
3933 need_space = 1;
3935 if (need_space && d_last_char (dpi) != ' ')
3936 d_append_char (dpi, ' ');
3937 d_append_char (dpi, '(');
3940 hold_modifiers = dpi->modifiers;
3941 dpi->modifiers = NULL;
3943 d_print_mod_list (dpi, mods, 0);
3945 if (need_paren)
3946 d_append_char (dpi, ')');
3948 d_append_char (dpi, '(');
3950 if (d_right (dc) != NULL)
3951 d_print_comp (dpi, d_right (dc));
3953 d_append_char (dpi, ')');
3955 d_print_mod_list (dpi, mods, 1);
3957 dpi->modifiers = hold_modifiers;
3960 /* Print an array type, except for the element type. */
3962 static void
3963 d_print_array_type (struct d_print_info *dpi,
3964 const struct demangle_component *dc,
3965 struct d_print_mod *mods)
3967 int need_space;
3969 need_space = 1;
3970 if (mods != NULL)
3972 int need_paren;
3973 struct d_print_mod *p;
3975 need_paren = 0;
3976 for (p = mods; p != NULL; p = p->next)
3978 if (! p->printed)
3980 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3982 need_space = 0;
3983 break;
3985 else
3987 need_paren = 1;
3988 need_space = 1;
3989 break;
3994 if (need_paren)
3995 d_append_string (dpi, " (");
3997 d_print_mod_list (dpi, mods, 0);
3999 if (need_paren)
4000 d_append_char (dpi, ')');
4003 if (need_space)
4004 d_append_char (dpi, ' ');
4006 d_append_char (dpi, '[');
4008 if (d_left (dc) != NULL)
4009 d_print_comp (dpi, d_left (dc));
4011 d_append_char (dpi, ']');
4014 /* Print an operator in an expression. */
4016 static void
4017 d_print_expr_op (struct d_print_info *dpi,
4018 const struct demangle_component *dc)
4020 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4021 d_append_buffer (dpi, dc->u.s_operator.op->name,
4022 dc->u.s_operator.op->len);
4023 else
4024 d_print_comp (dpi, dc);
4027 /* Print a cast. */
4029 static void
4030 d_print_cast (struct d_print_info *dpi,
4031 const struct demangle_component *dc)
4033 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4034 d_print_comp (dpi, d_left (dc));
4035 else
4037 struct d_print_mod *hold_dpm;
4038 struct d_print_template dpt;
4040 /* It appears that for a templated cast operator, we need to put
4041 the template parameters in scope for the operator name, but
4042 not for the parameters. The effect is that we need to handle
4043 the template printing here. */
4045 hold_dpm = dpi->modifiers;
4046 dpi->modifiers = NULL;
4048 dpt.next = dpi->templates;
4049 dpi->templates = &dpt;
4050 dpt.template_decl = d_left (dc);
4052 d_print_comp (dpi, d_left (d_left (dc)));
4054 dpi->templates = dpt.next;
4056 if (d_last_char (dpi) == '<')
4057 d_append_char (dpi, ' ');
4058 d_append_char (dpi, '<');
4059 d_print_comp (dpi, d_right (d_left (dc)));
4060 /* Avoid generating two consecutive '>' characters, to avoid
4061 the C++ syntactic ambiguity. */
4062 if (d_last_char (dpi) == '>')
4063 d_append_char (dpi, ' ');
4064 d_append_char (dpi, '>');
4066 dpi->modifiers = hold_dpm;
4070 /* Initialize the information structure we use to pass around
4071 information. */
4073 CP_STATIC_IF_GLIBCPP_V3
4074 void
4075 cplus_demangle_init_info (const char *mangled, int options, size_t len,
4076 struct d_info *di)
4078 di->s = mangled;
4079 di->send = mangled + len;
4080 di->options = options;
4082 di->n = mangled;
4084 /* We can not need more components than twice the number of chars in
4085 the mangled string. Most components correspond directly to
4086 chars, but the ARGLIST types are exceptions. */
4087 di->num_comps = 2 * len;
4088 di->next_comp = 0;
4090 /* Similarly, we can not need more substitutions than there are
4091 chars in the mangled string. */
4092 di->num_subs = len;
4093 di->next_sub = 0;
4094 di->did_subs = 0;
4096 di->last_name = NULL;
4098 di->expansion = 0;
4101 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4102 mangled name, return strings in repeated callback giving the demangled
4103 name. OPTIONS is the usual libiberty demangler options. On success,
4104 this returns 1. On failure, returns 0. */
4106 static int
4107 d_demangle_callback (const char *mangled, int options,
4108 demangle_callbackref callback, void *opaque)
4110 int type;
4111 struct d_info di;
4112 struct demangle_component *dc;
4113 int status;
4115 if (mangled[0] == '_' && mangled[1] == 'Z')
4116 type = 0;
4117 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4118 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4119 && (mangled[9] == 'D' || mangled[9] == 'I')
4120 && mangled[10] == '_')
4122 const char *intro;
4124 intro = (mangled[9] == 'I')
4125 ? "global constructors keyed to "
4126 : "global destructors keyed to ";
4128 callback (intro, strlen (intro), opaque);
4129 callback (mangled + 11, strlen (mangled + 11), opaque);
4130 return 1;
4132 else
4134 if ((options & DMGL_TYPES) == 0)
4135 return 0;
4136 type = 1;
4139 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4142 #ifdef CP_DYNAMIC_ARRAYS
4143 __extension__ struct demangle_component comps[di.num_comps];
4144 __extension__ struct demangle_component *subs[di.num_subs];
4146 di.comps = comps;
4147 di.subs = subs;
4148 #else
4149 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4150 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4151 #endif
4153 if (type)
4154 dc = cplus_demangle_type (&di);
4155 else
4156 dc = cplus_demangle_mangled_name (&di, 1);
4158 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4159 mangled string, then we didn't successfully demangle it. If
4160 DMGL_PARAMS is not set, we didn't look at the trailing
4161 parameters. */
4162 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4163 dc = NULL;
4165 #ifdef CP_DEMANGLE_DEBUG
4166 d_dump (dc, 0);
4167 #endif
4169 status = (dc != NULL)
4170 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4171 : 0;
4174 return status;
4177 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4178 name, return a buffer allocated with malloc holding the demangled
4179 name. OPTIONS is the usual libiberty demangler options. On
4180 success, this sets *PALC to the allocated size of the returned
4181 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4182 a memory allocation failure, and returns NULL. */
4184 static char *
4185 d_demangle (const char *mangled, int options, size_t *palc)
4187 struct d_growable_string dgs;
4188 int status;
4190 d_growable_string_init (&dgs, 0);
4192 status = d_demangle_callback (mangled, options,
4193 d_growable_string_callback_adapter, &dgs);
4194 if (status == 0)
4196 free (dgs.buf);
4197 *palc = 0;
4198 return NULL;
4201 *palc = dgs.allocation_failure ? 1 : 0;
4202 return dgs.buf;
4205 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4207 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4209 /* ia64 ABI-mandated entry point in the C++ runtime library for
4210 performing demangling. MANGLED_NAME is a NUL-terminated character
4211 string containing the name to be demangled.
4213 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4214 *LENGTH bytes, into which the demangled name is stored. If
4215 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4216 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4217 is placed in a region of memory allocated with malloc.
4219 If LENGTH is non-NULL, the length of the buffer containing the
4220 demangled name, is placed in *LENGTH.
4222 The return value is a pointer to the start of the NUL-terminated
4223 demangled name, or NULL if the demangling fails. The caller is
4224 responsible for deallocating this memory using free.
4226 *STATUS is set to one of the following values:
4227 0: The demangling operation succeeded.
4228 -1: A memory allocation failure occurred.
4229 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4230 -3: One of the arguments is invalid.
4232 The demangling is performed using the C++ ABI mangling rules, with
4233 GNU extensions. */
4235 char *
4236 __cxa_demangle (const char *mangled_name, char *output_buffer,
4237 size_t *length, int *status)
4239 char *demangled;
4240 size_t alc;
4242 if (mangled_name == NULL)
4244 if (status != NULL)
4245 *status = -3;
4246 return NULL;
4249 if (output_buffer != NULL && length == NULL)
4251 if (status != NULL)
4252 *status = -3;
4253 return NULL;
4256 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4258 if (demangled == NULL)
4260 if (status != NULL)
4262 if (alc == 1)
4263 *status = -1;
4264 else
4265 *status = -2;
4267 return NULL;
4270 if (output_buffer == NULL)
4272 if (length != NULL)
4273 *length = alc;
4275 else
4277 if (strlen (demangled) < *length)
4279 strcpy (output_buffer, demangled);
4280 free (demangled);
4281 demangled = output_buffer;
4283 else
4285 free (output_buffer);
4286 *length = alc;
4290 if (status != NULL)
4291 *status = 0;
4293 return demangled;
4296 extern int __gcclibcxx_demangle_callback (const char *,
4297 void (*)
4298 (const char *, size_t, void *),
4299 void *);
4301 /* Alternative, allocationless entry point in the C++ runtime library
4302 for performing demangling. MANGLED_NAME is a NUL-terminated character
4303 string containing the name to be demangled.
4305 CALLBACK is a callback function, called with demangled string
4306 segments as demangling progresses; it is called at least once,
4307 but may be called more than once. OPAQUE is a generalized pointer
4308 used as a callback argument.
4310 The return code is one of the following values, equivalent to
4311 the STATUS values of __cxa_demangle() (excluding -1, since this
4312 function performs no memory allocations):
4313 0: The demangling operation succeeded.
4314 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4315 -3: One of the arguments is invalid.
4317 The demangling is performed using the C++ ABI mangling rules, with
4318 GNU extensions. */
4321 __gcclibcxx_demangle_callback (const char *mangled_name,
4322 void (*callback) (const char *, size_t, void *),
4323 void *opaque)
4325 int status;
4327 if (mangled_name == NULL || callback == NULL)
4328 return -3;
4330 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
4331 callback, opaque);
4332 if (status == 0)
4333 return -2;
4335 return 0;
4338 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4340 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4341 mangled name, return a buffer allocated with malloc holding the
4342 demangled name. Otherwise, return NULL. */
4344 char *
4345 cplus_demangle_v3 (const char *mangled, int options)
4347 size_t alc;
4349 return d_demangle (mangled, options, &alc);
4353 cplus_demangle_v3_callback (const char *mangled, int options,
4354 demangle_callbackref callback, void *opaque)
4356 return d_demangle_callback (mangled, options, callback, opaque);
4359 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4360 conventions, but the output formatting is a little different.
4361 This instructs the C++ demangler not to emit pointer characters ("*"), to
4362 use Java's namespace separator symbol ("." instead of "::"), and to output
4363 JArray<TYPE> as TYPE[]. */
4365 char *
4366 java_demangle_v3 (const char *mangled)
4368 size_t alc;
4370 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
4374 java_demangle_v3_callback (const char *mangled,
4375 demangle_callbackref callback, void *opaque)
4377 return d_demangle_callback (mangled,
4378 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4379 callback, opaque);
4382 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4384 #ifndef IN_GLIBCPP_V3
4386 /* Demangle a string in order to find out whether it is a constructor
4387 or destructor. Return non-zero on success. Set *CTOR_KIND and
4388 *DTOR_KIND appropriately. */
4390 static int
4391 is_ctor_or_dtor (const char *mangled,
4392 enum gnu_v3_ctor_kinds *ctor_kind,
4393 enum gnu_v3_dtor_kinds *dtor_kind)
4395 struct d_info di;
4396 struct demangle_component *dc;
4397 int ret;
4399 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4400 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4402 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4405 #ifdef CP_DYNAMIC_ARRAYS
4406 __extension__ struct demangle_component comps[di.num_comps];
4407 __extension__ struct demangle_component *subs[di.num_subs];
4409 di.comps = comps;
4410 di.subs = subs;
4411 #else
4412 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4413 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4414 #endif
4416 dc = cplus_demangle_mangled_name (&di, 1);
4418 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4419 to demangle the entire string. */
4421 ret = 0;
4422 while (dc != NULL)
4424 switch (dc->type)
4426 default:
4427 dc = NULL;
4428 break;
4429 case DEMANGLE_COMPONENT_TYPED_NAME:
4430 case DEMANGLE_COMPONENT_TEMPLATE:
4431 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4432 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4433 case DEMANGLE_COMPONENT_CONST_THIS:
4434 dc = d_left (dc);
4435 break;
4436 case DEMANGLE_COMPONENT_QUAL_NAME:
4437 case DEMANGLE_COMPONENT_LOCAL_NAME:
4438 dc = d_right (dc);
4439 break;
4440 case DEMANGLE_COMPONENT_CTOR:
4441 *ctor_kind = dc->u.s_ctor.kind;
4442 ret = 1;
4443 dc = NULL;
4444 break;
4445 case DEMANGLE_COMPONENT_DTOR:
4446 *dtor_kind = dc->u.s_dtor.kind;
4447 ret = 1;
4448 dc = NULL;
4449 break;
4454 return ret;
4457 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4458 name. A non-zero return indicates the type of constructor. */
4460 enum gnu_v3_ctor_kinds
4461 is_gnu_v3_mangled_ctor (const char *name)
4463 enum gnu_v3_ctor_kinds ctor_kind;
4464 enum gnu_v3_dtor_kinds dtor_kind;
4466 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4467 return (enum gnu_v3_ctor_kinds) 0;
4468 return ctor_kind;
4472 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4473 name. A non-zero return indicates the type of destructor. */
4475 enum gnu_v3_dtor_kinds
4476 is_gnu_v3_mangled_dtor (const char *name)
4478 enum gnu_v3_ctor_kinds ctor_kind;
4479 enum gnu_v3_dtor_kinds dtor_kind;
4481 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4482 return (enum gnu_v3_dtor_kinds) 0;
4483 return dtor_kind;
4486 #endif /* IN_GLIBCPP_V3 */
4488 #ifdef STANDALONE_DEMANGLER
4490 #include "getopt.h"
4491 #include "dyn-string.h"
4493 static void print_usage (FILE* fp, int exit_value);
4495 #define IS_ALPHA(CHAR) \
4496 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4497 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4499 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4500 #define is_mangled_char(CHAR) \
4501 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4502 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4504 /* The name of this program, as invoked. */
4505 const char* program_name;
4507 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4509 static void
4510 print_usage (FILE* fp, int exit_value)
4512 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4513 fprintf (fp, "Options:\n");
4514 fprintf (fp, " -h,--help Display this message.\n");
4515 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4516 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4517 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4519 exit (exit_value);
4522 /* Option specification for getopt_long. */
4523 static const struct option long_options[] =
4525 { "help", no_argument, NULL, 'h' },
4526 { "no-params", no_argument, NULL, 'p' },
4527 { "verbose", no_argument, NULL, 'v' },
4528 { NULL, no_argument, NULL, 0 },
4531 /* Main entry for a demangling filter executable. It will demangle
4532 its command line arguments, if any. If none are provided, it will
4533 filter stdin to stdout, replacing any recognized mangled C++ names
4534 with their demangled equivalents. */
4537 main (int argc, char *argv[])
4539 int i;
4540 int opt_char;
4541 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4543 /* Use the program name of this program, as invoked. */
4544 program_name = argv[0];
4546 /* Parse options. */
4549 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4550 switch (opt_char)
4552 case '?': /* Unrecognized option. */
4553 print_usage (stderr, 1);
4554 break;
4556 case 'h':
4557 print_usage (stdout, 0);
4558 break;
4560 case 'p':
4561 options &= ~ DMGL_PARAMS;
4562 break;
4564 case 'v':
4565 options |= DMGL_VERBOSE;
4566 break;
4569 while (opt_char != -1);
4571 if (optind == argc)
4572 /* No command line arguments were provided. Filter stdin. */
4574 dyn_string_t mangled = dyn_string_new (3);
4575 char *s;
4577 /* Read all of input. */
4578 while (!feof (stdin))
4580 char c;
4582 /* Pile characters into mangled until we hit one that can't
4583 occur in a mangled name. */
4584 c = getchar ();
4585 while (!feof (stdin) && is_mangled_char (c))
4587 dyn_string_append_char (mangled, c);
4588 if (feof (stdin))
4589 break;
4590 c = getchar ();
4593 if (dyn_string_length (mangled) > 0)
4595 #ifdef IN_GLIBCPP_V3
4596 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4597 #else
4598 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4599 #endif
4601 if (s != NULL)
4603 fputs (s, stdout);
4604 free (s);
4606 else
4608 /* It might not have been a mangled name. Print the
4609 original text. */
4610 fputs (dyn_string_buf (mangled), stdout);
4613 dyn_string_clear (mangled);
4616 /* If we haven't hit EOF yet, we've read one character that
4617 can't occur in a mangled name, so print it out. */
4618 if (!feof (stdin))
4619 putchar (c);
4622 dyn_string_delete (mangled);
4624 else
4625 /* Demangle command line arguments. */
4627 /* Loop over command line arguments. */
4628 for (i = optind; i < argc; ++i)
4630 char *s;
4631 #ifdef IN_GLIBCPP_V3
4632 int status;
4633 #endif
4635 /* Attempt to demangle. */
4636 #ifdef IN_GLIBCPP_V3
4637 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4638 #else
4639 s = cplus_demangle_v3 (argv[i], options);
4640 #endif
4642 /* If it worked, print the demangled name. */
4643 if (s != NULL)
4645 printf ("%s\n", s);
4646 free (s);
4648 else
4650 #ifdef IN_GLIBCPP_V3
4651 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4652 #else
4653 fprintf (stderr, "Failed: %s\n", argv[i]);
4654 #endif
4659 return 0;
4662 #endif /* STANDALONE_DEMANGLER */