2009-10-24 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libiberty / cp-demangle.c
blob43cf34a36cf2c9ff8cd3b4ebe2c6b826fa554238
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
20 executable.)
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
41 name.
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
58 demangle_callbackref
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
64 cplus_demangle_print
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
73 IN_LIBGCC2
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int *status)
77 int __gcclibcxx_demangle_callback (const char *,
78 void (*)
79 (const char *, size_t, void *),
80 void *)
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
84 IN_GLIBCPP_V3
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
89 STANDALONE_DEMANGLER
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
93 CP_DEMANGLE_DEBUG
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 #if defined (_AIX) && !defined (__GNUC__)
99 #pragma alloca
100 #endif
102 #ifdef HAVE_CONFIG_H
103 #include "config.h"
104 #endif
106 #include <stdio.h>
108 #ifdef HAVE_STDLIB_H
109 #include <stdlib.h>
110 #endif
111 #ifdef HAVE_STRING_H
112 #include <string.h>
113 #endif
115 #ifdef HAVE_ALLOCA_H
116 # include <alloca.h>
117 #else
118 # ifndef alloca
119 # ifdef __GNUC__
120 # define alloca __builtin_alloca
121 # else
122 extern char *alloca ();
123 # endif /* __GNUC__ */
124 # endif /* alloca */
125 #endif /* HAVE_ALLOCA_H */
127 #include "ansidecl.h"
128 #include "libiberty.h"
129 #include "demangle.h"
130 #include "cp-demangle.h"
132 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
133 also rename them via #define to avoid compiler errors when the
134 static definition conflicts with the extern declaration in a header
135 file. */
136 #ifdef IN_GLIBCPP_V3
138 #define CP_STATIC_IF_GLIBCPP_V3 static
140 #define cplus_demangle_fill_name d_fill_name
141 static int d_fill_name (struct demangle_component *, const char *, int);
143 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
144 static int
145 d_fill_extended_operator (struct demangle_component *, int,
146 struct demangle_component *);
148 #define cplus_demangle_fill_ctor d_fill_ctor
149 static int
150 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
151 struct demangle_component *);
153 #define cplus_demangle_fill_dtor d_fill_dtor
154 static int
155 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
156 struct demangle_component *);
158 #define cplus_demangle_mangled_name d_mangled_name
159 static struct demangle_component *d_mangled_name (struct d_info *, int);
161 #define cplus_demangle_type d_type
162 static struct demangle_component *d_type (struct d_info *);
164 #define cplus_demangle_print d_print
165 static char *d_print (int, const struct demangle_component *, int, size_t *);
167 #define cplus_demangle_print_callback d_print_callback
168 static int d_print_callback (int, const struct demangle_component *,
169 demangle_callbackref, void *);
171 #define cplus_demangle_init_info d_init_info
172 static void d_init_info (const char *, int, size_t, struct d_info *);
174 #else /* ! defined(IN_GLIBCPP_V3) */
175 #define CP_STATIC_IF_GLIBCPP_V3
176 #endif /* ! defined(IN_GLIBCPP_V3) */
178 /* See if the compiler supports dynamic arrays. */
180 #ifdef __GNUC__
181 #define CP_DYNAMIC_ARRAYS
182 #else
183 #ifdef __STDC__
184 #ifdef __STDC_VERSION__
185 #if __STDC_VERSION__ >= 199901L
186 #define CP_DYNAMIC_ARRAYS
187 #endif /* __STDC__VERSION >= 199901L */
188 #endif /* defined (__STDC_VERSION__) */
189 #endif /* defined (__STDC__) */
190 #endif /* ! defined (__GNUC__) */
192 /* We avoid pulling in the ctype tables, to prevent pulling in
193 additional unresolved symbols when this code is used in a library.
194 FIXME: Is this really a valid reason? This comes from the original
195 V3 demangler code.
197 As of this writing this file has the following undefined references
198 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
199 strcat, strlen. */
201 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
205 /* The prefix prepended by GCC to an identifier represnting the
206 anonymous namespace. */
207 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
211 /* Information we keep for the standard substitutions. */
213 struct d_standard_sub_info
215 /* The code for this substitution. */
216 char code;
217 /* The simple string it expands to. */
218 const char *simple_expansion;
219 /* The length of the simple expansion. */
220 int simple_len;
221 /* The results of a full, verbose, expansion. This is used when
222 qualifying a constructor/destructor, or when in verbose mode. */
223 const char *full_expansion;
224 /* The length of the full expansion. */
225 int full_len;
226 /* What to set the last_name field of d_info to; NULL if we should
227 not set it. This is only relevant when qualifying a
228 constructor/destructor. */
229 const char *set_last_name;
230 /* The length of set_last_name. */
231 int set_last_name_len;
234 /* Accessors for subtrees of struct demangle_component. */
236 #define d_left(dc) ((dc)->u.s_binary.left)
237 #define d_right(dc) ((dc)->u.s_binary.right)
239 /* A list of templates. This is used while printing. */
241 struct d_print_template
243 /* Next template on the list. */
244 struct d_print_template *next;
245 /* This template. */
246 const struct demangle_component *template_decl;
249 /* A list of type modifiers. This is used while printing. */
251 struct d_print_mod
253 /* Next modifier on the list. These are in the reverse of the order
254 in which they appeared in the mangled string. */
255 struct d_print_mod *next;
256 /* The modifier. */
257 const struct demangle_component *mod;
258 /* Whether this modifier was printed. */
259 int printed;
260 /* The list of templates which applies to this modifier. */
261 struct d_print_template *templates;
264 /* We use these structures to hold information during printing. */
266 struct d_growable_string
268 /* Buffer holding the result. */
269 char *buf;
270 /* Current length of data in buffer. */
271 size_t len;
272 /* Allocated size of buffer. */
273 size_t alc;
274 /* Set to 1 if we had a memory allocation failure. */
275 int allocation_failure;
278 enum { D_PRINT_BUFFER_LENGTH = 256 };
279 struct d_print_info
281 /* The options passed to the demangler. */
282 int options;
283 /* Fixed-length allocated buffer for demangled data, flushed to the
284 callback with a NUL termination once full. */
285 char buf[D_PRINT_BUFFER_LENGTH];
286 /* Current length of data in buffer. */
287 size_t len;
288 /* The last character printed, saved individually so that it survives
289 any buffer flush. */
290 char last_char;
291 /* Callback function to handle demangled buffer flush. */
292 demangle_callbackref callback;
293 /* Opaque callback argument. */
294 void *opaque;
295 /* The current list of templates, if any. */
296 struct d_print_template *templates;
297 /* The current list of modifiers (e.g., pointer, reference, etc.),
298 if any. */
299 struct d_print_mod *modifiers;
300 /* Set to 1 if we saw a demangling error. */
301 int demangle_failure;
302 /* The current index into any template argument packs we are using
303 for printing. */
304 int pack_index;
307 #ifdef CP_DEMANGLE_DEBUG
308 static void d_dump (struct demangle_component *, int);
309 #endif
311 static struct demangle_component *
312 d_make_empty (struct d_info *);
314 static struct demangle_component *
315 d_make_comp (struct d_info *, enum demangle_component_type,
316 struct demangle_component *,
317 struct demangle_component *);
319 static struct demangle_component *
320 d_make_name (struct d_info *, const char *, int);
322 static struct demangle_component *
323 d_make_builtin_type (struct d_info *,
324 const struct demangle_builtin_type_info *);
326 static struct demangle_component *
327 d_make_operator (struct d_info *,
328 const struct demangle_operator_info *);
330 static struct demangle_component *
331 d_make_extended_operator (struct d_info *, int,
332 struct demangle_component *);
334 static struct demangle_component *
335 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
336 struct demangle_component *);
338 static struct demangle_component *
339 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
340 struct demangle_component *);
342 static struct demangle_component *
343 d_make_template_param (struct d_info *, long);
345 static struct demangle_component *
346 d_make_sub (struct d_info *, const char *, int);
348 static int
349 has_return_type (struct demangle_component *);
351 static int
352 is_ctor_dtor_or_conversion (struct demangle_component *);
354 static struct demangle_component *d_encoding (struct d_info *, int);
356 static struct demangle_component *d_name (struct d_info *);
358 static struct demangle_component *d_nested_name (struct d_info *);
360 static struct demangle_component *d_prefix (struct d_info *);
362 static struct demangle_component *d_unqualified_name (struct d_info *);
364 static struct demangle_component *d_source_name (struct d_info *);
366 static long d_number (struct d_info *);
368 static struct demangle_component *d_identifier (struct d_info *, int);
370 static struct demangle_component *d_operator_name (struct d_info *);
372 static struct demangle_component *d_special_name (struct d_info *);
374 static int d_call_offset (struct d_info *, int);
376 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
378 static struct demangle_component **
379 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
381 static struct demangle_component *
382 d_function_type (struct d_info *);
384 static struct demangle_component *
385 d_bare_function_type (struct d_info *, int);
387 static struct demangle_component *
388 d_class_enum_type (struct d_info *);
390 static struct demangle_component *d_array_type (struct d_info *);
392 static struct demangle_component *
393 d_pointer_to_member_type (struct d_info *);
395 static struct demangle_component *
396 d_template_param (struct d_info *);
398 static struct demangle_component *d_template_args (struct d_info *);
400 static struct demangle_component *
401 d_template_arg (struct d_info *);
403 static struct demangle_component *d_expression (struct d_info *);
405 static struct demangle_component *d_expr_primary (struct d_info *);
407 static struct demangle_component *d_local_name (struct d_info *);
409 static int d_discriminator (struct d_info *);
411 static struct demangle_component *d_lambda (struct d_info *);
413 static struct demangle_component *d_unnamed_type (struct d_info *);
415 static int
416 d_add_substitution (struct d_info *, struct demangle_component *);
418 static struct demangle_component *d_substitution (struct d_info *, int);
420 static void d_growable_string_init (struct d_growable_string *, size_t);
422 static inline void
423 d_growable_string_resize (struct d_growable_string *, size_t);
425 static inline void
426 d_growable_string_append_buffer (struct d_growable_string *,
427 const char *, size_t);
428 static void
429 d_growable_string_callback_adapter (const char *, size_t, void *);
431 static void
432 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
434 static inline void d_print_error (struct d_print_info *);
436 static inline int d_print_saw_error (struct d_print_info *);
438 static inline void d_print_flush (struct d_print_info *);
440 static inline void d_append_char (struct d_print_info *, char);
442 static inline void d_append_buffer (struct d_print_info *,
443 const char *, size_t);
445 static inline void d_append_string (struct d_print_info *, const char *);
447 static inline char d_last_char (struct d_print_info *);
449 static void
450 d_print_comp (struct d_print_info *, const struct demangle_component *);
452 static void
453 d_print_java_identifier (struct d_print_info *, const char *, int);
455 static void
456 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
458 static void
459 d_print_mod (struct d_print_info *, const struct demangle_component *);
461 static void
462 d_print_function_type (struct d_print_info *,
463 const struct demangle_component *,
464 struct d_print_mod *);
466 static void
467 d_print_array_type (struct d_print_info *,
468 const struct demangle_component *,
469 struct d_print_mod *);
471 static void
472 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
474 static void
475 d_print_cast (struct d_print_info *, const struct demangle_component *);
477 static int d_demangle_callback (const char *, int,
478 demangle_callbackref, void *);
479 static char *d_demangle (const char *, int, size_t *);
481 #ifdef CP_DEMANGLE_DEBUG
483 static void
484 d_dump (struct demangle_component *dc, int indent)
486 int i;
488 if (dc == NULL)
490 if (indent == 0)
491 printf ("failed demangling\n");
492 return;
495 for (i = 0; i < indent; ++i)
496 putchar (' ');
498 switch (dc->type)
500 case DEMANGLE_COMPONENT_NAME:
501 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
502 return;
503 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
504 printf ("template parameter %ld\n", dc->u.s_number.number);
505 return;
506 case DEMANGLE_COMPONENT_CTOR:
507 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
508 d_dump (dc->u.s_ctor.name, indent + 2);
509 return;
510 case DEMANGLE_COMPONENT_DTOR:
511 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
512 d_dump (dc->u.s_dtor.name, indent + 2);
513 return;
514 case DEMANGLE_COMPONENT_SUB_STD:
515 printf ("standard substitution %s\n", dc->u.s_string.string);
516 return;
517 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
518 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
519 return;
520 case DEMANGLE_COMPONENT_OPERATOR:
521 printf ("operator %s\n", dc->u.s_operator.op->name);
522 return;
523 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
524 printf ("extended operator with %d args\n",
525 dc->u.s_extended_operator.args);
526 d_dump (dc->u.s_extended_operator.name, indent + 2);
527 return;
529 case DEMANGLE_COMPONENT_QUAL_NAME:
530 printf ("qualified name\n");
531 break;
532 case DEMANGLE_COMPONENT_LOCAL_NAME:
533 printf ("local name\n");
534 break;
535 case DEMANGLE_COMPONENT_TYPED_NAME:
536 printf ("typed name\n");
537 break;
538 case DEMANGLE_COMPONENT_TEMPLATE:
539 printf ("template\n");
540 break;
541 case DEMANGLE_COMPONENT_VTABLE:
542 printf ("vtable\n");
543 break;
544 case DEMANGLE_COMPONENT_VTT:
545 printf ("VTT\n");
546 break;
547 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
548 printf ("construction vtable\n");
549 break;
550 case DEMANGLE_COMPONENT_TYPEINFO:
551 printf ("typeinfo\n");
552 break;
553 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
554 printf ("typeinfo name\n");
555 break;
556 case DEMANGLE_COMPONENT_TYPEINFO_FN:
557 printf ("typeinfo function\n");
558 break;
559 case DEMANGLE_COMPONENT_THUNK:
560 printf ("thunk\n");
561 break;
562 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
563 printf ("virtual thunk\n");
564 break;
565 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
566 printf ("covariant thunk\n");
567 break;
568 case DEMANGLE_COMPONENT_JAVA_CLASS:
569 printf ("java class\n");
570 break;
571 case DEMANGLE_COMPONENT_GUARD:
572 printf ("guard\n");
573 break;
574 case DEMANGLE_COMPONENT_REFTEMP:
575 printf ("reference temporary\n");
576 break;
577 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
578 printf ("hidden alias\n");
579 break;
580 case DEMANGLE_COMPONENT_RESTRICT:
581 printf ("restrict\n");
582 break;
583 case DEMANGLE_COMPONENT_VOLATILE:
584 printf ("volatile\n");
585 break;
586 case DEMANGLE_COMPONENT_CONST:
587 printf ("const\n");
588 break;
589 case DEMANGLE_COMPONENT_RESTRICT_THIS:
590 printf ("restrict this\n");
591 break;
592 case DEMANGLE_COMPONENT_VOLATILE_THIS:
593 printf ("volatile this\n");
594 break;
595 case DEMANGLE_COMPONENT_CONST_THIS:
596 printf ("const this\n");
597 break;
598 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
599 printf ("vendor type qualifier\n");
600 break;
601 case DEMANGLE_COMPONENT_POINTER:
602 printf ("pointer\n");
603 break;
604 case DEMANGLE_COMPONENT_REFERENCE:
605 printf ("reference\n");
606 break;
607 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
608 printf ("rvalue reference\n");
609 break;
610 case DEMANGLE_COMPONENT_COMPLEX:
611 printf ("complex\n");
612 break;
613 case DEMANGLE_COMPONENT_IMAGINARY:
614 printf ("imaginary\n");
615 break;
616 case DEMANGLE_COMPONENT_VENDOR_TYPE:
617 printf ("vendor type\n");
618 break;
619 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
620 printf ("function type\n");
621 break;
622 case DEMANGLE_COMPONENT_ARRAY_TYPE:
623 printf ("array type\n");
624 break;
625 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
626 printf ("pointer to member type\n");
627 break;
628 case DEMANGLE_COMPONENT_FIXED_TYPE:
629 printf ("fixed-point type\n");
630 break;
631 case DEMANGLE_COMPONENT_ARGLIST:
632 printf ("argument list\n");
633 break;
634 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
635 printf ("template argument list\n");
636 break;
637 case DEMANGLE_COMPONENT_CAST:
638 printf ("cast\n");
639 break;
640 case DEMANGLE_COMPONENT_UNARY:
641 printf ("unary operator\n");
642 break;
643 case DEMANGLE_COMPONENT_BINARY:
644 printf ("binary operator\n");
645 break;
646 case DEMANGLE_COMPONENT_BINARY_ARGS:
647 printf ("binary operator arguments\n");
648 break;
649 case DEMANGLE_COMPONENT_TRINARY:
650 printf ("trinary operator\n");
651 break;
652 case DEMANGLE_COMPONENT_TRINARY_ARG1:
653 printf ("trinary operator arguments 1\n");
654 break;
655 case DEMANGLE_COMPONENT_TRINARY_ARG2:
656 printf ("trinary operator arguments 1\n");
657 break;
658 case DEMANGLE_COMPONENT_LITERAL:
659 printf ("literal\n");
660 break;
661 case DEMANGLE_COMPONENT_LITERAL_NEG:
662 printf ("negative literal\n");
663 break;
664 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
665 printf ("java resource\n");
666 break;
667 case DEMANGLE_COMPONENT_COMPOUND_NAME:
668 printf ("compound name\n");
669 break;
670 case DEMANGLE_COMPONENT_CHARACTER:
671 printf ("character '%c'\n", dc->u.s_character.character);
672 return;
673 case DEMANGLE_COMPONENT_DECLTYPE:
674 printf ("decltype\n");
675 break;
676 case DEMANGLE_COMPONENT_PACK_EXPANSION:
677 printf ("pack expansion\n");
678 break;
681 d_dump (d_left (dc), indent + 2);
682 d_dump (d_right (dc), indent + 2);
685 #endif /* CP_DEMANGLE_DEBUG */
687 /* Fill in a DEMANGLE_COMPONENT_NAME. */
689 CP_STATIC_IF_GLIBCPP_V3
691 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
693 if (p == NULL || s == NULL || len == 0)
694 return 0;
695 p->type = DEMANGLE_COMPONENT_NAME;
696 p->u.s_name.s = s;
697 p->u.s_name.len = len;
698 return 1;
701 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
703 CP_STATIC_IF_GLIBCPP_V3
705 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
706 struct demangle_component *name)
708 if (p == NULL || args < 0 || name == NULL)
709 return 0;
710 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
711 p->u.s_extended_operator.args = args;
712 p->u.s_extended_operator.name = name;
713 return 1;
716 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
718 CP_STATIC_IF_GLIBCPP_V3
720 cplus_demangle_fill_ctor (struct demangle_component *p,
721 enum gnu_v3_ctor_kinds kind,
722 struct demangle_component *name)
724 if (p == NULL
725 || name == NULL
726 || (int) kind < gnu_v3_complete_object_ctor
727 || (int) kind > gnu_v3_complete_object_allocating_ctor)
728 return 0;
729 p->type = DEMANGLE_COMPONENT_CTOR;
730 p->u.s_ctor.kind = kind;
731 p->u.s_ctor.name = name;
732 return 1;
735 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
737 CP_STATIC_IF_GLIBCPP_V3
739 cplus_demangle_fill_dtor (struct demangle_component *p,
740 enum gnu_v3_dtor_kinds kind,
741 struct demangle_component *name)
743 if (p == NULL
744 || name == NULL
745 || (int) kind < gnu_v3_deleting_dtor
746 || (int) kind > gnu_v3_base_object_dtor)
747 return 0;
748 p->type = DEMANGLE_COMPONENT_DTOR;
749 p->u.s_dtor.kind = kind;
750 p->u.s_dtor.name = name;
751 return 1;
754 /* Add a new component. */
756 static struct demangle_component *
757 d_make_empty (struct d_info *di)
759 struct demangle_component *p;
761 if (di->next_comp >= di->num_comps)
762 return NULL;
763 p = &di->comps[di->next_comp];
764 ++di->next_comp;
765 return p;
768 /* Add a new generic component. */
770 static struct demangle_component *
771 d_make_comp (struct d_info *di, enum demangle_component_type type,
772 struct demangle_component *left,
773 struct demangle_component *right)
775 struct demangle_component *p;
777 /* We check for errors here. A typical error would be a NULL return
778 from a subroutine. We catch those here, and return NULL
779 upward. */
780 switch (type)
782 /* These types require two parameters. */
783 case DEMANGLE_COMPONENT_QUAL_NAME:
784 case DEMANGLE_COMPONENT_LOCAL_NAME:
785 case DEMANGLE_COMPONENT_TYPED_NAME:
786 case DEMANGLE_COMPONENT_TEMPLATE:
787 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
788 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
789 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
790 case DEMANGLE_COMPONENT_UNARY:
791 case DEMANGLE_COMPONENT_BINARY:
792 case DEMANGLE_COMPONENT_BINARY_ARGS:
793 case DEMANGLE_COMPONENT_TRINARY:
794 case DEMANGLE_COMPONENT_TRINARY_ARG1:
795 case DEMANGLE_COMPONENT_TRINARY_ARG2:
796 case DEMANGLE_COMPONENT_LITERAL:
797 case DEMANGLE_COMPONENT_LITERAL_NEG:
798 case DEMANGLE_COMPONENT_COMPOUND_NAME:
799 if (left == NULL || right == NULL)
800 return NULL;
801 break;
803 /* These types only require one parameter. */
804 case DEMANGLE_COMPONENT_VTABLE:
805 case DEMANGLE_COMPONENT_VTT:
806 case DEMANGLE_COMPONENT_TYPEINFO:
807 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
808 case DEMANGLE_COMPONENT_TYPEINFO_FN:
809 case DEMANGLE_COMPONENT_THUNK:
810 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
811 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
812 case DEMANGLE_COMPONENT_JAVA_CLASS:
813 case DEMANGLE_COMPONENT_GUARD:
814 case DEMANGLE_COMPONENT_REFTEMP:
815 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
816 case DEMANGLE_COMPONENT_POINTER:
817 case DEMANGLE_COMPONENT_REFERENCE:
818 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
819 case DEMANGLE_COMPONENT_COMPLEX:
820 case DEMANGLE_COMPONENT_IMAGINARY:
821 case DEMANGLE_COMPONENT_VENDOR_TYPE:
822 case DEMANGLE_COMPONENT_CAST:
823 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
824 case DEMANGLE_COMPONENT_DECLTYPE:
825 case DEMANGLE_COMPONENT_PACK_EXPANSION:
826 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
827 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
828 if (left == NULL)
829 return NULL;
830 break;
832 /* This needs a right parameter, but the left parameter can be
833 empty. */
834 case DEMANGLE_COMPONENT_ARRAY_TYPE:
835 if (right == NULL)
836 return NULL;
837 break;
839 /* These are allowed to have no parameters--in some cases they
840 will be filled in later. */
841 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
842 case DEMANGLE_COMPONENT_RESTRICT:
843 case DEMANGLE_COMPONENT_VOLATILE:
844 case DEMANGLE_COMPONENT_CONST:
845 case DEMANGLE_COMPONENT_RESTRICT_THIS:
846 case DEMANGLE_COMPONENT_VOLATILE_THIS:
847 case DEMANGLE_COMPONENT_CONST_THIS:
848 case DEMANGLE_COMPONENT_ARGLIST:
849 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
850 break;
852 /* Other types should not be seen here. */
853 default:
854 return NULL;
857 p = d_make_empty (di);
858 if (p != NULL)
860 p->type = type;
861 p->u.s_binary.left = left;
862 p->u.s_binary.right = right;
864 return p;
867 /* Add a new name component. */
869 static struct demangle_component *
870 d_make_name (struct d_info *di, const char *s, int len)
872 struct demangle_component *p;
874 p = d_make_empty (di);
875 if (! cplus_demangle_fill_name (p, s, len))
876 return NULL;
877 return p;
880 /* Add a new builtin type component. */
882 static struct demangle_component *
883 d_make_builtin_type (struct d_info *di,
884 const struct demangle_builtin_type_info *type)
886 struct demangle_component *p;
888 if (type == NULL)
889 return NULL;
890 p = d_make_empty (di);
891 if (p != NULL)
893 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
894 p->u.s_builtin.type = type;
896 return p;
899 /* Add a new operator component. */
901 static struct demangle_component *
902 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
904 struct demangle_component *p;
906 p = d_make_empty (di);
907 if (p != NULL)
909 p->type = DEMANGLE_COMPONENT_OPERATOR;
910 p->u.s_operator.op = op;
912 return p;
915 /* Add a new extended operator component. */
917 static struct demangle_component *
918 d_make_extended_operator (struct d_info *di, int args,
919 struct demangle_component *name)
921 struct demangle_component *p;
923 p = d_make_empty (di);
924 if (! cplus_demangle_fill_extended_operator (p, args, name))
925 return NULL;
926 return p;
929 static struct demangle_component *
930 d_make_default_arg (struct d_info *di, int num,
931 struct demangle_component *sub)
933 struct demangle_component *p = d_make_empty (di);
934 if (p)
936 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
937 p->u.s_unary_num.num = num;
938 p->u.s_unary_num.sub = sub;
940 return p;
943 /* Add a new constructor component. */
945 static struct demangle_component *
946 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
947 struct demangle_component *name)
949 struct demangle_component *p;
951 p = d_make_empty (di);
952 if (! cplus_demangle_fill_ctor (p, kind, name))
953 return NULL;
954 return p;
957 /* Add a new destructor component. */
959 static struct demangle_component *
960 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
961 struct demangle_component *name)
963 struct demangle_component *p;
965 p = d_make_empty (di);
966 if (! cplus_demangle_fill_dtor (p, kind, name))
967 return NULL;
968 return p;
971 /* Add a new template parameter. */
973 static struct demangle_component *
974 d_make_template_param (struct d_info *di, long i)
976 struct demangle_component *p;
978 p = d_make_empty (di);
979 if (p != NULL)
981 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
982 p->u.s_number.number = i;
984 return p;
987 /* Add a new function parameter. */
989 static struct demangle_component *
990 d_make_function_param (struct d_info *di, long i)
992 struct demangle_component *p;
994 p = d_make_empty (di);
995 if (p != NULL)
997 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
998 p->u.s_number.number = i;
1000 return p;
1003 /* Add a new standard substitution component. */
1005 static struct demangle_component *
1006 d_make_sub (struct d_info *di, const char *name, int len)
1008 struct demangle_component *p;
1010 p = d_make_empty (di);
1011 if (p != NULL)
1013 p->type = DEMANGLE_COMPONENT_SUB_STD;
1014 p->u.s_string.string = name;
1015 p->u.s_string.len = len;
1017 return p;
1020 /* <mangled-name> ::= _Z <encoding>
1022 TOP_LEVEL is non-zero when called at the top level. */
1024 CP_STATIC_IF_GLIBCPP_V3
1025 struct demangle_component *
1026 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1028 if (! d_check_char (di, '_')
1029 /* Allow missing _ if not at toplevel to work around a
1030 bug in G++ abi-version=2 mangling; see the comment in
1031 write_template_arg. */
1032 && top_level)
1033 return NULL;
1034 if (! d_check_char (di, 'Z'))
1035 return NULL;
1036 return d_encoding (di, top_level);
1039 /* Return whether a function should have a return type. The argument
1040 is the function name, which may be qualified in various ways. The
1041 rules are that template functions have return types with some
1042 exceptions, function types which are not part of a function name
1043 mangling have return types with some exceptions, and non-template
1044 function names do not have return types. The exceptions are that
1045 constructors, destructors, and conversion operators do not have
1046 return types. */
1048 static int
1049 has_return_type (struct demangle_component *dc)
1051 if (dc == NULL)
1052 return 0;
1053 switch (dc->type)
1055 default:
1056 return 0;
1057 case DEMANGLE_COMPONENT_TEMPLATE:
1058 return ! is_ctor_dtor_or_conversion (d_left (dc));
1059 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1060 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1061 case DEMANGLE_COMPONENT_CONST_THIS:
1062 return has_return_type (d_left (dc));
1066 /* Return whether a name is a constructor, a destructor, or a
1067 conversion operator. */
1069 static int
1070 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1072 if (dc == NULL)
1073 return 0;
1074 switch (dc->type)
1076 default:
1077 return 0;
1078 case DEMANGLE_COMPONENT_QUAL_NAME:
1079 case DEMANGLE_COMPONENT_LOCAL_NAME:
1080 return is_ctor_dtor_or_conversion (d_right (dc));
1081 case DEMANGLE_COMPONENT_CTOR:
1082 case DEMANGLE_COMPONENT_DTOR:
1083 case DEMANGLE_COMPONENT_CAST:
1084 return 1;
1088 /* <encoding> ::= <(function) name> <bare-function-type>
1089 ::= <(data) name>
1090 ::= <special-name>
1092 TOP_LEVEL is non-zero when called at the top level, in which case
1093 if DMGL_PARAMS is not set we do not demangle the function
1094 parameters. We only set this at the top level, because otherwise
1095 we would not correctly demangle names in local scopes. */
1097 static struct demangle_component *
1098 d_encoding (struct d_info *di, int top_level)
1100 char peek = d_peek_char (di);
1102 if (peek == 'G' || peek == 'T')
1103 return d_special_name (di);
1104 else
1106 struct demangle_component *dc;
1108 dc = d_name (di);
1110 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1112 /* Strip off any initial CV-qualifiers, as they really apply
1113 to the `this' parameter, and they were not output by the
1114 v2 demangler without DMGL_PARAMS. */
1115 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1116 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1117 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1118 dc = d_left (dc);
1120 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1121 there may be CV-qualifiers on its right argument which
1122 really apply here; this happens when parsing a class
1123 which is local to a function. */
1124 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1126 struct demangle_component *dcr;
1128 dcr = d_right (dc);
1129 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1130 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1131 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1132 dcr = d_left (dcr);
1133 dc->u.s_binary.right = dcr;
1136 return dc;
1139 peek = d_peek_char (di);
1140 if (dc == NULL || peek == '\0' || peek == 'E')
1141 return dc;
1142 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1143 d_bare_function_type (di, has_return_type (dc)));
1147 /* <name> ::= <nested-name>
1148 ::= <unscoped-name>
1149 ::= <unscoped-template-name> <template-args>
1150 ::= <local-name>
1152 <unscoped-name> ::= <unqualified-name>
1153 ::= St <unqualified-name>
1155 <unscoped-template-name> ::= <unscoped-name>
1156 ::= <substitution>
1159 static struct demangle_component *
1160 d_name (struct d_info *di)
1162 char peek = d_peek_char (di);
1163 struct demangle_component *dc;
1165 switch (peek)
1167 case 'N':
1168 return d_nested_name (di);
1170 case 'Z':
1171 return d_local_name (di);
1173 case 'L':
1174 case 'U':
1175 return d_unqualified_name (di);
1177 case 'S':
1179 int subst;
1181 if (d_peek_next_char (di) != 't')
1183 dc = d_substitution (di, 0);
1184 subst = 1;
1186 else
1188 d_advance (di, 2);
1189 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1190 d_make_name (di, "std", 3),
1191 d_unqualified_name (di));
1192 di->expansion += 3;
1193 subst = 0;
1196 if (d_peek_char (di) != 'I')
1198 /* The grammar does not permit this case to occur if we
1199 called d_substitution() above (i.e., subst == 1). We
1200 don't bother to check. */
1202 else
1204 /* This is <template-args>, which means that we just saw
1205 <unscoped-template-name>, which is a substitution
1206 candidate if we didn't just get it from a
1207 substitution. */
1208 if (! subst)
1210 if (! d_add_substitution (di, dc))
1211 return NULL;
1213 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1214 d_template_args (di));
1217 return dc;
1220 default:
1221 dc = d_unqualified_name (di);
1222 if (d_peek_char (di) == 'I')
1224 /* This is <template-args>, which means that we just saw
1225 <unscoped-template-name>, which is a substitution
1226 candidate. */
1227 if (! d_add_substitution (di, dc))
1228 return NULL;
1229 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1230 d_template_args (di));
1232 return dc;
1236 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1237 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1240 static struct demangle_component *
1241 d_nested_name (struct d_info *di)
1243 struct demangle_component *ret;
1244 struct demangle_component **pret;
1246 if (! d_check_char (di, 'N'))
1247 return NULL;
1249 pret = d_cv_qualifiers (di, &ret, 1);
1250 if (pret == NULL)
1251 return NULL;
1253 *pret = d_prefix (di);
1254 if (*pret == NULL)
1255 return NULL;
1257 if (! d_check_char (di, 'E'))
1258 return NULL;
1260 return ret;
1263 /* <prefix> ::= <prefix> <unqualified-name>
1264 ::= <template-prefix> <template-args>
1265 ::= <template-param>
1267 ::= <substitution>
1269 <template-prefix> ::= <prefix> <(template) unqualified-name>
1270 ::= <template-param>
1271 ::= <substitution>
1274 static struct demangle_component *
1275 d_prefix (struct d_info *di)
1277 struct demangle_component *ret = NULL;
1279 while (1)
1281 char peek;
1282 enum demangle_component_type comb_type;
1283 struct demangle_component *dc;
1285 peek = d_peek_char (di);
1286 if (peek == '\0')
1287 return NULL;
1289 /* The older code accepts a <local-name> here, but I don't see
1290 that in the grammar. The older code does not accept a
1291 <template-param> here. */
1293 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1294 if (IS_DIGIT (peek)
1295 || IS_LOWER (peek)
1296 || peek == 'C'
1297 || peek == 'D'
1298 || peek == 'U'
1299 || peek == 'L')
1300 dc = d_unqualified_name (di);
1301 else if (peek == 'S')
1302 dc = d_substitution (di, 1);
1303 else if (peek == 'I')
1305 if (ret == NULL)
1306 return NULL;
1307 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1308 dc = d_template_args (di);
1310 else if (peek == 'T')
1311 dc = d_template_param (di);
1312 else if (peek == 'E')
1313 return ret;
1314 else if (peek == 'M')
1316 /* Initializer scope for a lambda. We don't need to represent
1317 this; the normal code will just treat the variable as a type
1318 scope, which gives appropriate output. */
1319 if (ret == NULL)
1320 return NULL;
1321 d_advance (di, 1);
1322 continue;
1324 else
1325 return NULL;
1327 if (ret == NULL)
1328 ret = dc;
1329 else
1330 ret = d_make_comp (di, comb_type, ret, dc);
1332 if (peek != 'S' && d_peek_char (di) != 'E')
1334 if (! d_add_substitution (di, ret))
1335 return NULL;
1340 /* <unqualified-name> ::= <operator-name>
1341 ::= <ctor-dtor-name>
1342 ::= <source-name>
1343 ::= <local-source-name>
1345 <local-source-name> ::= L <source-name> <discriminator>
1348 static struct demangle_component *
1349 d_unqualified_name (struct d_info *di)
1351 char peek;
1353 peek = d_peek_char (di);
1354 if (IS_DIGIT (peek))
1355 return d_source_name (di);
1356 else if (IS_LOWER (peek))
1358 struct demangle_component *ret;
1360 ret = d_operator_name (di);
1361 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1362 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1363 return ret;
1365 else if (peek == 'C' || peek == 'D')
1366 return d_ctor_dtor_name (di);
1367 else if (peek == 'L')
1369 struct demangle_component * ret;
1371 d_advance (di, 1);
1373 ret = d_source_name (di);
1374 if (ret == NULL)
1375 return NULL;
1376 if (! d_discriminator (di))
1377 return NULL;
1378 return ret;
1380 else if (peek == 'U')
1382 switch (d_peek_next_char (di))
1384 case 'l':
1385 return d_lambda (di);
1386 case 't':
1387 return d_unnamed_type (di);
1388 default:
1389 return NULL;
1392 else
1393 return NULL;
1396 /* <source-name> ::= <(positive length) number> <identifier> */
1398 static struct demangle_component *
1399 d_source_name (struct d_info *di)
1401 long len;
1402 struct demangle_component *ret;
1404 len = d_number (di);
1405 if (len <= 0)
1406 return NULL;
1407 ret = d_identifier (di, len);
1408 di->last_name = ret;
1409 return ret;
1412 /* number ::= [n] <(non-negative decimal integer)> */
1414 static long
1415 d_number (struct d_info *di)
1417 int negative;
1418 char peek;
1419 long ret;
1421 negative = 0;
1422 peek = d_peek_char (di);
1423 if (peek == 'n')
1425 negative = 1;
1426 d_advance (di, 1);
1427 peek = d_peek_char (di);
1430 ret = 0;
1431 while (1)
1433 if (! IS_DIGIT (peek))
1435 if (negative)
1436 ret = - ret;
1437 return ret;
1439 ret = ret * 10 + peek - '0';
1440 d_advance (di, 1);
1441 peek = d_peek_char (di);
1445 /* identifier ::= <(unqualified source code identifier)> */
1447 static struct demangle_component *
1448 d_identifier (struct d_info *di, int len)
1450 const char *name;
1452 name = d_str (di);
1454 if (di->send - name < len)
1455 return NULL;
1457 d_advance (di, len);
1459 /* A Java mangled name may have a trailing '$' if it is a C++
1460 keyword. This '$' is not included in the length count. We just
1461 ignore the '$'. */
1462 if ((di->options & DMGL_JAVA) != 0
1463 && d_peek_char (di) == '$')
1464 d_advance (di, 1);
1466 /* Look for something which looks like a gcc encoding of an
1467 anonymous namespace, and replace it with a more user friendly
1468 name. */
1469 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1470 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1471 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1473 const char *s;
1475 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1476 if ((*s == '.' || *s == '_' || *s == '$')
1477 && s[1] == 'N')
1479 di->expansion -= len - sizeof "(anonymous namespace)";
1480 return d_make_name (di, "(anonymous namespace)",
1481 sizeof "(anonymous namespace)" - 1);
1485 return d_make_name (di, name, len);
1488 /* operator_name ::= many different two character encodings.
1489 ::= cv <type>
1490 ::= v <digit> <source-name>
1493 #define NL(s) s, (sizeof s) - 1
1495 CP_STATIC_IF_GLIBCPP_V3
1496 const struct demangle_operator_info cplus_demangle_operators[] =
1498 { "aN", NL ("&="), 2 },
1499 { "aS", NL ("="), 2 },
1500 { "aa", NL ("&&"), 2 },
1501 { "ad", NL ("&"), 1 },
1502 { "an", NL ("&"), 2 },
1503 { "cl", NL ("()"), 2 },
1504 { "cm", NL (","), 2 },
1505 { "co", NL ("~"), 1 },
1506 { "dV", NL ("/="), 2 },
1507 { "da", NL ("delete[]"), 1 },
1508 { "de", NL ("*"), 1 },
1509 { "dl", NL ("delete"), 1 },
1510 { "dt", NL ("."), 2 },
1511 { "dv", NL ("/"), 2 },
1512 { "eO", NL ("^="), 2 },
1513 { "eo", NL ("^"), 2 },
1514 { "eq", NL ("=="), 2 },
1515 { "ge", NL (">="), 2 },
1516 { "gt", NL (">"), 2 },
1517 { "ix", NL ("[]"), 2 },
1518 { "lS", NL ("<<="), 2 },
1519 { "le", NL ("<="), 2 },
1520 { "ls", NL ("<<"), 2 },
1521 { "lt", NL ("<"), 2 },
1522 { "mI", NL ("-="), 2 },
1523 { "mL", NL ("*="), 2 },
1524 { "mi", NL ("-"), 2 },
1525 { "ml", NL ("*"), 2 },
1526 { "mm", NL ("--"), 1 },
1527 { "na", NL ("new[]"), 1 },
1528 { "ne", NL ("!="), 2 },
1529 { "ng", NL ("-"), 1 },
1530 { "nt", NL ("!"), 1 },
1531 { "nw", NL ("new"), 1 },
1532 { "oR", NL ("|="), 2 },
1533 { "oo", NL ("||"), 2 },
1534 { "or", NL ("|"), 2 },
1535 { "pL", NL ("+="), 2 },
1536 { "pl", NL ("+"), 2 },
1537 { "pm", NL ("->*"), 2 },
1538 { "pp", NL ("++"), 1 },
1539 { "ps", NL ("+"), 1 },
1540 { "pt", NL ("->"), 2 },
1541 { "qu", NL ("?"), 3 },
1542 { "rM", NL ("%="), 2 },
1543 { "rS", NL (">>="), 2 },
1544 { "rm", NL ("%"), 2 },
1545 { "rs", NL (">>"), 2 },
1546 { "st", NL ("sizeof "), 1 },
1547 { "sz", NL ("sizeof "), 1 },
1548 { "at", NL ("alignof "), 1 },
1549 { "az", NL ("alignof "), 1 },
1550 { NULL, NULL, 0, 0 }
1553 static struct demangle_component *
1554 d_operator_name (struct d_info *di)
1556 char c1;
1557 char c2;
1559 c1 = d_next_char (di);
1560 c2 = d_next_char (di);
1561 if (c1 == 'v' && IS_DIGIT (c2))
1562 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1563 else if (c1 == 'c' && c2 == 'v')
1564 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1565 cplus_demangle_type (di), NULL);
1566 else
1568 /* LOW is the inclusive lower bound. */
1569 int low = 0;
1570 /* HIGH is the exclusive upper bound. We subtract one to ignore
1571 the sentinel at the end of the array. */
1572 int high = ((sizeof (cplus_demangle_operators)
1573 / sizeof (cplus_demangle_operators[0]))
1574 - 1);
1576 while (1)
1578 int i;
1579 const struct demangle_operator_info *p;
1581 i = low + (high - low) / 2;
1582 p = cplus_demangle_operators + i;
1584 if (c1 == p->code[0] && c2 == p->code[1])
1585 return d_make_operator (di, p);
1587 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1588 high = i;
1589 else
1590 low = i + 1;
1591 if (low == high)
1592 return NULL;
1597 static struct demangle_component *
1598 d_make_character (struct d_info *di, int c)
1600 struct demangle_component *p;
1601 p = d_make_empty (di);
1602 if (p != NULL)
1604 p->type = DEMANGLE_COMPONENT_CHARACTER;
1605 p->u.s_character.character = c;
1607 return p;
1610 static struct demangle_component *
1611 d_java_resource (struct d_info *di)
1613 struct demangle_component *p = NULL;
1614 struct demangle_component *next = NULL;
1615 long len, i;
1616 char c;
1617 const char *str;
1619 len = d_number (di);
1620 if (len <= 1)
1621 return NULL;
1623 /* Eat the leading '_'. */
1624 if (d_next_char (di) != '_')
1625 return NULL;
1626 len--;
1628 str = d_str (di);
1629 i = 0;
1631 while (len > 0)
1633 c = str[i];
1634 if (!c)
1635 return NULL;
1637 /* Each chunk is either a '$' escape... */
1638 if (c == '$')
1640 i++;
1641 switch (str[i++])
1643 case 'S':
1644 c = '/';
1645 break;
1646 case '_':
1647 c = '.';
1648 break;
1649 case '$':
1650 c = '$';
1651 break;
1652 default:
1653 return NULL;
1655 next = d_make_character (di, c);
1656 d_advance (di, i);
1657 str = d_str (di);
1658 len -= i;
1659 i = 0;
1660 if (next == NULL)
1661 return NULL;
1663 /* ... or a sequence of characters. */
1664 else
1666 while (i < len && str[i] && str[i] != '$')
1667 i++;
1669 next = d_make_name (di, str, i);
1670 d_advance (di, i);
1671 str = d_str (di);
1672 len -= i;
1673 i = 0;
1674 if (next == NULL)
1675 return NULL;
1678 if (p == NULL)
1679 p = next;
1680 else
1682 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1683 if (p == NULL)
1684 return NULL;
1688 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1690 return p;
1693 /* <special-name> ::= TV <type>
1694 ::= TT <type>
1695 ::= TI <type>
1696 ::= TS <type>
1697 ::= GV <(object) name>
1698 ::= T <call-offset> <(base) encoding>
1699 ::= Tc <call-offset> <call-offset> <(base) encoding>
1700 Also g++ extensions:
1701 ::= TC <type> <(offset) number> _ <(base) type>
1702 ::= TF <type>
1703 ::= TJ <type>
1704 ::= GR <name>
1705 ::= GA <encoding>
1706 ::= Gr <resource name>
1709 static struct demangle_component *
1710 d_special_name (struct d_info *di)
1712 di->expansion += 20;
1713 if (d_check_char (di, 'T'))
1715 switch (d_next_char (di))
1717 case 'V':
1718 di->expansion -= 5;
1719 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1720 cplus_demangle_type (di), NULL);
1721 case 'T':
1722 di->expansion -= 10;
1723 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1724 cplus_demangle_type (di), NULL);
1725 case 'I':
1726 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1727 cplus_demangle_type (di), NULL);
1728 case 'S':
1729 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1730 cplus_demangle_type (di), NULL);
1732 case 'h':
1733 if (! d_call_offset (di, 'h'))
1734 return NULL;
1735 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1736 d_encoding (di, 0), NULL);
1738 case 'v':
1739 if (! d_call_offset (di, 'v'))
1740 return NULL;
1741 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1742 d_encoding (di, 0), NULL);
1744 case 'c':
1745 if (! d_call_offset (di, '\0'))
1746 return NULL;
1747 if (! d_call_offset (di, '\0'))
1748 return NULL;
1749 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1750 d_encoding (di, 0), NULL);
1752 case 'C':
1754 struct demangle_component *derived_type;
1755 long offset;
1756 struct demangle_component *base_type;
1758 derived_type = cplus_demangle_type (di);
1759 offset = d_number (di);
1760 if (offset < 0)
1761 return NULL;
1762 if (! d_check_char (di, '_'))
1763 return NULL;
1764 base_type = cplus_demangle_type (di);
1765 /* We don't display the offset. FIXME: We should display
1766 it in verbose mode. */
1767 di->expansion += 5;
1768 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1769 base_type, derived_type);
1772 case 'F':
1773 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1774 cplus_demangle_type (di), NULL);
1775 case 'J':
1776 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1777 cplus_demangle_type (di), NULL);
1779 default:
1780 return NULL;
1783 else if (d_check_char (di, 'G'))
1785 switch (d_next_char (di))
1787 case 'V':
1788 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1790 case 'R':
1791 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1792 NULL);
1794 case 'A':
1795 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1796 d_encoding (di, 0), NULL);
1798 case 'r':
1799 return d_java_resource (di);
1801 default:
1802 return NULL;
1805 else
1806 return NULL;
1809 /* <call-offset> ::= h <nv-offset> _
1810 ::= v <v-offset> _
1812 <nv-offset> ::= <(offset) number>
1814 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1816 The C parameter, if not '\0', is a character we just read which is
1817 the start of the <call-offset>.
1819 We don't display the offset information anywhere. FIXME: We should
1820 display it in verbose mode. */
1822 static int
1823 d_call_offset (struct d_info *di, int c)
1825 if (c == '\0')
1826 c = d_next_char (di);
1828 if (c == 'h')
1829 d_number (di);
1830 else if (c == 'v')
1832 d_number (di);
1833 if (! d_check_char (di, '_'))
1834 return 0;
1835 d_number (di);
1837 else
1838 return 0;
1840 if (! d_check_char (di, '_'))
1841 return 0;
1843 return 1;
1846 /* <ctor-dtor-name> ::= C1
1847 ::= C2
1848 ::= C3
1849 ::= D0
1850 ::= D1
1851 ::= D2
1854 static struct demangle_component *
1855 d_ctor_dtor_name (struct d_info *di)
1857 if (di->last_name != NULL)
1859 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1860 di->expansion += di->last_name->u.s_name.len;
1861 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1862 di->expansion += di->last_name->u.s_string.len;
1864 switch (d_peek_char (di))
1866 case 'C':
1868 enum gnu_v3_ctor_kinds kind;
1870 switch (d_peek_next_char (di))
1872 case '1':
1873 kind = gnu_v3_complete_object_ctor;
1874 break;
1875 case '2':
1876 kind = gnu_v3_base_object_ctor;
1877 break;
1878 case '3':
1879 kind = gnu_v3_complete_object_allocating_ctor;
1880 break;
1881 default:
1882 return NULL;
1884 d_advance (di, 2);
1885 return d_make_ctor (di, kind, di->last_name);
1888 case 'D':
1890 enum gnu_v3_dtor_kinds kind;
1892 switch (d_peek_next_char (di))
1894 case '0':
1895 kind = gnu_v3_deleting_dtor;
1896 break;
1897 case '1':
1898 kind = gnu_v3_complete_object_dtor;
1899 break;
1900 case '2':
1901 kind = gnu_v3_base_object_dtor;
1902 break;
1903 default:
1904 return NULL;
1906 d_advance (di, 2);
1907 return d_make_dtor (di, kind, di->last_name);
1910 default:
1911 return NULL;
1915 /* <type> ::= <builtin-type>
1916 ::= <function-type>
1917 ::= <class-enum-type>
1918 ::= <array-type>
1919 ::= <pointer-to-member-type>
1920 ::= <template-param>
1921 ::= <template-template-param> <template-args>
1922 ::= <substitution>
1923 ::= <CV-qualifiers> <type>
1924 ::= P <type>
1925 ::= R <type>
1926 ::= O <type> (C++0x)
1927 ::= C <type>
1928 ::= G <type>
1929 ::= U <source-name> <type>
1931 <builtin-type> ::= various one letter codes
1932 ::= u <source-name>
1935 CP_STATIC_IF_GLIBCPP_V3
1936 const struct demangle_builtin_type_info
1937 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1939 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1940 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1941 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1942 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1943 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1944 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1945 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1946 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1947 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1948 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1949 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1950 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1951 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1952 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1953 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1954 D_PRINT_DEFAULT },
1955 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1956 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1957 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1958 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1959 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1960 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1961 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1962 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1963 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1964 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1965 D_PRINT_UNSIGNED_LONG_LONG },
1966 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1967 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
1968 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
1969 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
1970 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
1971 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
1972 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
1975 CP_STATIC_IF_GLIBCPP_V3
1976 struct demangle_component *
1977 cplus_demangle_type (struct d_info *di)
1979 char peek;
1980 struct demangle_component *ret;
1981 int can_subst;
1983 /* The ABI specifies that when CV-qualifiers are used, the base type
1984 is substitutable, and the fully qualified type is substitutable,
1985 but the base type with a strict subset of the CV-qualifiers is
1986 not substitutable. The natural recursive implementation of the
1987 CV-qualifiers would cause subsets to be substitutable, so instead
1988 we pull them all off now.
1990 FIXME: The ABI says that order-insensitive vendor qualifiers
1991 should be handled in the same way, but we have no way to tell
1992 which vendor qualifiers are order-insensitive and which are
1993 order-sensitive. So we just assume that they are all
1994 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1995 __vector, and it treats it as order-sensitive when mangling
1996 names. */
1998 peek = d_peek_char (di);
1999 if (peek == 'r' || peek == 'V' || peek == 'K')
2001 struct demangle_component **pret;
2003 pret = d_cv_qualifiers (di, &ret, 0);
2004 if (pret == NULL)
2005 return NULL;
2006 *pret = cplus_demangle_type (di);
2007 if (! *pret || ! d_add_substitution (di, ret))
2008 return NULL;
2009 return ret;
2012 can_subst = 1;
2014 switch (peek)
2016 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2017 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2018 case 'o': case 's': case 't':
2019 case 'v': case 'w': case 'x': case 'y': case 'z':
2020 ret = d_make_builtin_type (di,
2021 &cplus_demangle_builtin_types[peek - 'a']);
2022 di->expansion += ret->u.s_builtin.type->len;
2023 can_subst = 0;
2024 d_advance (di, 1);
2025 break;
2027 case 'u':
2028 d_advance (di, 1);
2029 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2030 d_source_name (di), NULL);
2031 break;
2033 case 'F':
2034 ret = d_function_type (di);
2035 break;
2037 case '0': case '1': case '2': case '3': case '4':
2038 case '5': case '6': case '7': case '8': case '9':
2039 case 'N':
2040 case 'Z':
2041 ret = d_class_enum_type (di);
2042 break;
2044 case 'A':
2045 ret = d_array_type (di);
2046 break;
2048 case 'M':
2049 ret = d_pointer_to_member_type (di);
2050 break;
2052 case 'T':
2053 ret = d_template_param (di);
2054 if (d_peek_char (di) == 'I')
2056 /* This is <template-template-param> <template-args>. The
2057 <template-template-param> part is a substitution
2058 candidate. */
2059 if (! d_add_substitution (di, ret))
2060 return NULL;
2061 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2062 d_template_args (di));
2064 break;
2066 case 'S':
2067 /* If this is a special substitution, then it is the start of
2068 <class-enum-type>. */
2070 char peek_next;
2072 peek_next = d_peek_next_char (di);
2073 if (IS_DIGIT (peek_next)
2074 || peek_next == '_'
2075 || IS_UPPER (peek_next))
2077 ret = d_substitution (di, 0);
2078 /* The substituted name may have been a template name and
2079 may be followed by tepmlate args. */
2080 if (d_peek_char (di) == 'I')
2081 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2082 d_template_args (di));
2083 else
2084 can_subst = 0;
2086 else
2088 ret = d_class_enum_type (di);
2089 /* If the substitution was a complete type, then it is not
2090 a new substitution candidate. However, if the
2091 substitution was followed by template arguments, then
2092 the whole thing is a substitution candidate. */
2093 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2094 can_subst = 0;
2097 break;
2099 case 'O':
2100 d_advance (di, 1);
2101 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2102 cplus_demangle_type (di), NULL);
2103 break;
2105 case 'P':
2106 d_advance (di, 1);
2107 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2108 cplus_demangle_type (di), NULL);
2109 break;
2111 case 'R':
2112 d_advance (di, 1);
2113 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2114 cplus_demangle_type (di), NULL);
2115 break;
2117 case 'C':
2118 d_advance (di, 1);
2119 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2120 cplus_demangle_type (di), NULL);
2121 break;
2123 case 'G':
2124 d_advance (di, 1);
2125 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2126 cplus_demangle_type (di), NULL);
2127 break;
2129 case 'U':
2130 d_advance (di, 1);
2131 ret = d_source_name (di);
2132 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2133 cplus_demangle_type (di), ret);
2134 break;
2136 case 'D':
2137 can_subst = 0;
2138 d_advance (di, 1);
2139 peek = d_next_char (di);
2140 switch (peek)
2142 case 'T':
2143 case 't':
2144 /* decltype (expression) */
2145 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2146 d_expression (di), NULL);
2147 if (ret && d_next_char (di) != 'E')
2148 ret = NULL;
2149 break;
2151 case 'p':
2152 /* Pack expansion. */
2153 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2154 cplus_demangle_type (di), NULL);
2155 break;
2157 case 'f':
2158 /* 32-bit decimal floating point */
2159 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2160 di->expansion += ret->u.s_builtin.type->len;
2161 break;
2162 case 'd':
2163 /* 64-bit DFP */
2164 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2165 di->expansion += ret->u.s_builtin.type->len;
2166 break;
2167 case 'e':
2168 /* 128-bit DFP */
2169 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2170 di->expansion += ret->u.s_builtin.type->len;
2171 break;
2172 case 'h':
2173 /* 16-bit half-precision FP */
2174 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2175 di->expansion += ret->u.s_builtin.type->len;
2176 break;
2177 case 's':
2178 /* char16_t */
2179 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2180 di->expansion += ret->u.s_builtin.type->len;
2181 break;
2182 case 'i':
2183 /* char32_t */
2184 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2185 di->expansion += ret->u.s_builtin.type->len;
2186 break;
2188 case 'F':
2189 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2190 ret = d_make_empty (di);
2191 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2192 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2193 /* For demangling we don't care about the bits. */
2194 d_number (di);
2195 ret->u.s_fixed.length = cplus_demangle_type (di);
2196 d_number (di);
2197 peek = d_next_char (di);
2198 ret->u.s_fixed.sat = (peek == 's');
2199 break;
2201 default:
2202 return NULL;
2204 break;
2206 default:
2207 return NULL;
2210 if (can_subst)
2212 if (! d_add_substitution (di, ret))
2213 return NULL;
2216 return ret;
2219 /* <CV-qualifiers> ::= [r] [V] [K] */
2221 static struct demangle_component **
2222 d_cv_qualifiers (struct d_info *di,
2223 struct demangle_component **pret, int member_fn)
2225 char peek;
2227 peek = d_peek_char (di);
2228 while (peek == 'r' || peek == 'V' || peek == 'K')
2230 enum demangle_component_type t;
2232 d_advance (di, 1);
2233 if (peek == 'r')
2235 t = (member_fn
2236 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2237 : DEMANGLE_COMPONENT_RESTRICT);
2238 di->expansion += sizeof "restrict";
2240 else if (peek == 'V')
2242 t = (member_fn
2243 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2244 : DEMANGLE_COMPONENT_VOLATILE);
2245 di->expansion += sizeof "volatile";
2247 else
2249 t = (member_fn
2250 ? DEMANGLE_COMPONENT_CONST_THIS
2251 : DEMANGLE_COMPONENT_CONST);
2252 di->expansion += sizeof "const";
2255 *pret = d_make_comp (di, t, NULL, NULL);
2256 if (*pret == NULL)
2257 return NULL;
2258 pret = &d_left (*pret);
2260 peek = d_peek_char (di);
2263 return pret;
2266 /* <function-type> ::= F [Y] <bare-function-type> E */
2268 static struct demangle_component *
2269 d_function_type (struct d_info *di)
2271 struct demangle_component *ret;
2273 if (! d_check_char (di, 'F'))
2274 return NULL;
2275 if (d_peek_char (di) == 'Y')
2277 /* Function has C linkage. We don't print this information.
2278 FIXME: We should print it in verbose mode. */
2279 d_advance (di, 1);
2281 ret = d_bare_function_type (di, 1);
2282 if (! d_check_char (di, 'E'))
2283 return NULL;
2284 return ret;
2287 /* <type>+ */
2289 static struct demangle_component *
2290 d_parmlist (struct d_info *di)
2292 struct demangle_component *tl;
2293 struct demangle_component **ptl;
2295 tl = NULL;
2296 ptl = &tl;
2297 while (1)
2299 struct demangle_component *type;
2301 char peek = d_peek_char (di);
2302 if (peek == '\0' || peek == 'E')
2303 break;
2304 type = cplus_demangle_type (di);
2305 if (type == NULL)
2306 return NULL;
2307 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2308 if (*ptl == NULL)
2309 return NULL;
2310 ptl = &d_right (*ptl);
2313 /* There should be at least one parameter type besides the optional
2314 return type. A function which takes no arguments will have a
2315 single parameter type void. */
2316 if (tl == NULL)
2317 return NULL;
2319 /* If we have a single parameter type void, omit it. */
2320 if (d_right (tl) == NULL
2321 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2322 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2324 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2325 d_left (tl) = NULL;
2328 return tl;
2331 /* <bare-function-type> ::= [J]<type>+ */
2333 static struct demangle_component *
2334 d_bare_function_type (struct d_info *di, int has_return_type)
2336 struct demangle_component *return_type;
2337 struct demangle_component *tl;
2338 char peek;
2340 /* Detect special qualifier indicating that the first argument
2341 is the return type. */
2342 peek = d_peek_char (di);
2343 if (peek == 'J')
2345 d_advance (di, 1);
2346 has_return_type = 1;
2349 if (has_return_type)
2351 return_type = cplus_demangle_type (di);
2352 if (return_type == NULL)
2353 return NULL;
2355 else
2356 return_type = NULL;
2358 tl = d_parmlist (di);
2359 if (tl == NULL)
2360 return NULL;
2362 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2363 return_type, tl);
2366 /* <class-enum-type> ::= <name> */
2368 static struct demangle_component *
2369 d_class_enum_type (struct d_info *di)
2371 return d_name (di);
2374 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2375 ::= A [<(dimension) expression>] _ <(element) type>
2378 static struct demangle_component *
2379 d_array_type (struct d_info *di)
2381 char peek;
2382 struct demangle_component *dim;
2384 if (! d_check_char (di, 'A'))
2385 return NULL;
2387 peek = d_peek_char (di);
2388 if (peek == '_')
2389 dim = NULL;
2390 else if (IS_DIGIT (peek))
2392 const char *s;
2394 s = d_str (di);
2397 d_advance (di, 1);
2398 peek = d_peek_char (di);
2400 while (IS_DIGIT (peek));
2401 dim = d_make_name (di, s, d_str (di) - s);
2402 if (dim == NULL)
2403 return NULL;
2405 else
2407 dim = d_expression (di);
2408 if (dim == NULL)
2409 return NULL;
2412 if (! d_check_char (di, '_'))
2413 return NULL;
2415 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2416 cplus_demangle_type (di));
2419 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2421 static struct demangle_component *
2422 d_pointer_to_member_type (struct d_info *di)
2424 struct demangle_component *cl;
2425 struct demangle_component *mem;
2426 struct demangle_component **pmem;
2428 if (! d_check_char (di, 'M'))
2429 return NULL;
2431 cl = cplus_demangle_type (di);
2433 /* The ABI specifies that any type can be a substitution source, and
2434 that M is followed by two types, and that when a CV-qualified
2435 type is seen both the base type and the CV-qualified types are
2436 substitution sources. The ABI also specifies that for a pointer
2437 to a CV-qualified member function, the qualifiers are attached to
2438 the second type. Given the grammar, a plain reading of the ABI
2439 suggests that both the CV-qualified member function and the
2440 non-qualified member function are substitution sources. However,
2441 g++ does not work that way. g++ treats only the CV-qualified
2442 member function as a substitution source. FIXME. So to work
2443 with g++, we need to pull off the CV-qualifiers here, in order to
2444 avoid calling add_substitution() in cplus_demangle_type(). But
2445 for a CV-qualified member which is not a function, g++ does
2446 follow the ABI, so we need to handle that case here by calling
2447 d_add_substitution ourselves. */
2449 pmem = d_cv_qualifiers (di, &mem, 1);
2450 if (pmem == NULL)
2451 return NULL;
2452 *pmem = cplus_demangle_type (di);
2453 if (*pmem == NULL)
2454 return NULL;
2456 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2458 if (! d_add_substitution (di, mem))
2459 return NULL;
2462 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2465 /* <non-negative number> _ */
2467 static long
2468 d_compact_number (struct d_info *di)
2470 long num;
2471 if (d_peek_char (di) == '_')
2472 num = 0;
2473 else if (d_peek_char (di) == 'n')
2474 return -1;
2475 else
2476 num = d_number (di) + 1;
2478 if (! d_check_char (di, '_'))
2479 return -1;
2480 return num;
2483 /* <template-param> ::= T_
2484 ::= T <(parameter-2 non-negative) number> _
2487 static struct demangle_component *
2488 d_template_param (struct d_info *di)
2490 long param;
2492 if (! d_check_char (di, 'T'))
2493 return NULL;
2495 param = d_compact_number (di);
2496 if (param < 0)
2497 return NULL;
2499 ++di->did_subs;
2501 return d_make_template_param (di, param);
2504 /* <template-args> ::= I <template-arg>+ E */
2506 static struct demangle_component *
2507 d_template_args (struct d_info *di)
2509 struct demangle_component *hold_last_name;
2510 struct demangle_component *al;
2511 struct demangle_component **pal;
2513 /* Preserve the last name we saw--don't let the template arguments
2514 clobber it, as that would give us the wrong name for a subsequent
2515 constructor or destructor. */
2516 hold_last_name = di->last_name;
2518 if (! d_check_char (di, 'I'))
2519 return NULL;
2521 if (d_peek_char (di) == 'E')
2523 /* An argument pack can be empty. */
2524 d_advance (di, 1);
2525 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2528 al = NULL;
2529 pal = &al;
2530 while (1)
2532 struct demangle_component *a;
2534 a = d_template_arg (di);
2535 if (a == NULL)
2536 return NULL;
2538 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2539 if (*pal == NULL)
2540 return NULL;
2541 pal = &d_right (*pal);
2543 if (d_peek_char (di) == 'E')
2545 d_advance (di, 1);
2546 break;
2550 di->last_name = hold_last_name;
2552 return al;
2555 /* <template-arg> ::= <type>
2556 ::= X <expression> E
2557 ::= <expr-primary>
2560 static struct demangle_component *
2561 d_template_arg (struct d_info *di)
2563 struct demangle_component *ret;
2565 switch (d_peek_char (di))
2567 case 'X':
2568 d_advance (di, 1);
2569 ret = d_expression (di);
2570 if (! d_check_char (di, 'E'))
2571 return NULL;
2572 return ret;
2574 case 'L':
2575 return d_expr_primary (di);
2577 case 'I':
2578 /* An argument pack. */
2579 return d_template_args (di);
2581 default:
2582 return cplus_demangle_type (di);
2586 /* Subroutine of <expression> ::= cl <expression>+ E */
2588 static struct demangle_component *
2589 d_exprlist (struct d_info *di)
2591 struct demangle_component *list = NULL;
2592 struct demangle_component **p = &list;
2594 if (d_peek_char (di) == 'E')
2596 d_advance (di, 1);
2597 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2600 while (1)
2602 struct demangle_component *arg = d_expression (di);
2603 if (arg == NULL)
2604 return NULL;
2606 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2607 if (*p == NULL)
2608 return NULL;
2609 p = &d_right (*p);
2611 if (d_peek_char (di) == 'E')
2613 d_advance (di, 1);
2614 break;
2618 return list;
2621 /* <expression> ::= <(unary) operator-name> <expression>
2622 ::= <(binary) operator-name> <expression> <expression>
2623 ::= <(trinary) operator-name> <expression> <expression> <expression>
2624 ::= cl <expression>+ E
2625 ::= st <type>
2626 ::= <template-param>
2627 ::= sr <type> <unqualified-name>
2628 ::= sr <type> <unqualified-name> <template-args>
2629 ::= <expr-primary>
2632 static struct demangle_component *
2633 d_expression (struct d_info *di)
2635 char peek;
2637 peek = d_peek_char (di);
2638 if (peek == 'L')
2639 return d_expr_primary (di);
2640 else if (peek == 'T')
2641 return d_template_param (di);
2642 else if (peek == 's' && d_peek_next_char (di) == 'r')
2644 struct demangle_component *type;
2645 struct demangle_component *name;
2647 d_advance (di, 2);
2648 type = cplus_demangle_type (di);
2649 name = d_unqualified_name (di);
2650 if (d_peek_char (di) != 'I')
2651 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2652 else
2653 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2654 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2655 d_template_args (di)));
2657 else if (peek == 's' && d_peek_next_char (di) == 'p')
2659 d_advance (di, 2);
2660 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2661 d_expression (di), NULL);
2663 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2665 /* Function parameter used in a late-specified return type. */
2666 int index;
2667 d_advance (di, 2);
2668 index = d_compact_number (di);
2669 if (index < 0)
2670 return NULL;
2672 return d_make_function_param (di, index);
2674 else if (IS_DIGIT (peek))
2676 /* We can get an unqualified name as an expression in the case of
2677 a dependent member access, i.e. decltype(T().i). */
2678 struct demangle_component *name = d_unqualified_name (di);
2679 if (name == NULL)
2680 return NULL;
2681 if (d_peek_char (di) == 'I')
2682 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2683 d_template_args (di));
2684 else
2685 return name;
2687 else
2689 struct demangle_component *op;
2690 int args;
2692 op = d_operator_name (di);
2693 if (op == NULL)
2694 return NULL;
2696 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2697 di->expansion += op->u.s_operator.op->len - 2;
2699 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2700 && strcmp (op->u.s_operator.op->code, "st") == 0)
2701 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2702 cplus_demangle_type (di));
2704 switch (op->type)
2706 default:
2707 return NULL;
2708 case DEMANGLE_COMPONENT_OPERATOR:
2709 args = op->u.s_operator.op->args;
2710 break;
2711 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2712 args = op->u.s_extended_operator.args;
2713 break;
2714 case DEMANGLE_COMPONENT_CAST:
2715 args = 1;
2716 break;
2719 switch (args)
2721 case 1:
2723 struct demangle_component *operand;
2724 if (op->type == DEMANGLE_COMPONENT_CAST
2725 && d_check_char (di, '_'))
2726 operand = d_exprlist (di);
2727 else
2728 operand = d_expression (di);
2729 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2730 operand);
2732 case 2:
2734 struct demangle_component *left;
2735 struct demangle_component *right;
2737 left = d_expression (di);
2738 if (!strcmp (op->u.s_operator.op->code, "cl"))
2739 right = d_exprlist (di);
2740 else
2741 right = d_expression (di);
2743 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2744 d_make_comp (di,
2745 DEMANGLE_COMPONENT_BINARY_ARGS,
2746 left, right));
2748 case 3:
2750 struct demangle_component *first;
2751 struct demangle_component *second;
2753 first = d_expression (di);
2754 second = d_expression (di);
2755 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2756 d_make_comp (di,
2757 DEMANGLE_COMPONENT_TRINARY_ARG1,
2758 first,
2759 d_make_comp (di,
2760 DEMANGLE_COMPONENT_TRINARY_ARG2,
2761 second,
2762 d_expression (di))));
2764 default:
2765 return NULL;
2770 /* <expr-primary> ::= L <type> <(value) number> E
2771 ::= L <type> <(value) float> E
2772 ::= L <mangled-name> E
2775 static struct demangle_component *
2776 d_expr_primary (struct d_info *di)
2778 struct demangle_component *ret;
2780 if (! d_check_char (di, 'L'))
2781 return NULL;
2782 if (d_peek_char (di) == '_'
2783 /* Workaround for G++ bug; see comment in write_template_arg. */
2784 || d_peek_char (di) == 'Z')
2785 ret = cplus_demangle_mangled_name (di, 0);
2786 else
2788 struct demangle_component *type;
2789 enum demangle_component_type t;
2790 const char *s;
2792 type = cplus_demangle_type (di);
2793 if (type == NULL)
2794 return NULL;
2796 /* If we have a type we know how to print, we aren't going to
2797 print the type name itself. */
2798 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2799 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2800 di->expansion -= type->u.s_builtin.type->len;
2802 /* Rather than try to interpret the literal value, we just
2803 collect it as a string. Note that it's possible to have a
2804 floating point literal here. The ABI specifies that the
2805 format of such literals is machine independent. That's fine,
2806 but what's not fine is that versions of g++ up to 3.2 with
2807 -fabi-version=1 used upper case letters in the hex constant,
2808 and dumped out gcc's internal representation. That makes it
2809 hard to tell where the constant ends, and hard to dump the
2810 constant in any readable form anyhow. We don't attempt to
2811 handle these cases. */
2813 t = DEMANGLE_COMPONENT_LITERAL;
2814 if (d_peek_char (di) == 'n')
2816 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2817 d_advance (di, 1);
2819 s = d_str (di);
2820 while (d_peek_char (di) != 'E')
2822 if (d_peek_char (di) == '\0')
2823 return NULL;
2824 d_advance (di, 1);
2826 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2828 if (! d_check_char (di, 'E'))
2829 return NULL;
2830 return ret;
2833 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2834 ::= Z <(function) encoding> E s [<discriminator>]
2837 static struct demangle_component *
2838 d_local_name (struct d_info *di)
2840 struct demangle_component *function;
2842 if (! d_check_char (di, 'Z'))
2843 return NULL;
2845 function = d_encoding (di, 0);
2847 if (! d_check_char (di, 'E'))
2848 return NULL;
2850 if (d_peek_char (di) == 's')
2852 d_advance (di, 1);
2853 if (! d_discriminator (di))
2854 return NULL;
2855 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2856 d_make_name (di, "string literal",
2857 sizeof "string literal" - 1));
2859 else
2861 struct demangle_component *name;
2862 int num = -1;
2864 if (d_peek_char (di) == 'd')
2866 /* Default argument scope: d <number> _. */
2867 d_advance (di, 1);
2868 num = d_compact_number (di);
2869 if (num < 0)
2870 return NULL;
2873 name = d_name (di);
2874 if (name)
2875 switch (name->type)
2877 /* Lambdas and unnamed types have internal discriminators. */
2878 case DEMANGLE_COMPONENT_LAMBDA:
2879 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
2880 break;
2881 default:
2882 if (! d_discriminator (di))
2883 return NULL;
2885 if (num >= 0)
2886 name = d_make_default_arg (di, num, name);
2887 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2891 /* <discriminator> ::= _ <(non-negative) number>
2893 We demangle the discriminator, but we don't print it out. FIXME:
2894 We should print it out in verbose mode. */
2896 static int
2897 d_discriminator (struct d_info *di)
2899 long discrim;
2901 if (d_peek_char (di) != '_')
2902 return 1;
2903 d_advance (di, 1);
2904 discrim = d_number (di);
2905 if (discrim < 0)
2906 return 0;
2907 return 1;
2910 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
2912 static struct demangle_component *
2913 d_lambda (struct d_info *di)
2915 struct demangle_component *tl;
2916 struct demangle_component *ret;
2917 int num;
2919 if (! d_check_char (di, 'U'))
2920 return NULL;
2921 if (! d_check_char (di, 'l'))
2922 return NULL;
2924 tl = d_parmlist (di);
2925 if (tl == NULL)
2926 return NULL;
2928 if (! d_check_char (di, 'E'))
2929 return NULL;
2931 num = d_compact_number (di);
2932 if (num < 0)
2933 return NULL;
2935 ret = d_make_empty (di);
2936 if (ret)
2938 ret->type = DEMANGLE_COMPONENT_LAMBDA;
2939 ret->u.s_unary_num.sub = tl;
2940 ret->u.s_unary_num.num = num;
2943 if (! d_add_substitution (di, ret))
2944 return NULL;
2946 return ret;
2949 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
2951 static struct demangle_component *
2952 d_unnamed_type (struct d_info *di)
2954 struct demangle_component *ret;
2955 long num;
2957 if (! d_check_char (di, 'U'))
2958 return NULL;
2959 if (! d_check_char (di, 't'))
2960 return NULL;
2962 num = d_compact_number (di);
2963 if (num < 0)
2964 return NULL;
2966 ret = d_make_empty (di);
2967 if (ret)
2969 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
2970 ret->u.s_number.number = num;
2973 if (! d_add_substitution (di, ret))
2974 return NULL;
2976 return ret;
2979 /* Add a new substitution. */
2981 static int
2982 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2984 if (dc == NULL)
2985 return 0;
2986 if (di->next_sub >= di->num_subs)
2987 return 0;
2988 di->subs[di->next_sub] = dc;
2989 ++di->next_sub;
2990 return 1;
2993 /* <substitution> ::= S <seq-id> _
2994 ::= S_
2995 ::= St
2996 ::= Sa
2997 ::= Sb
2998 ::= Ss
2999 ::= Si
3000 ::= So
3001 ::= Sd
3003 If PREFIX is non-zero, then this type is being used as a prefix in
3004 a qualified name. In this case, for the standard substitutions, we
3005 need to check whether we are being used as a prefix for a
3006 constructor or destructor, and return a full template name.
3007 Otherwise we will get something like std::iostream::~iostream()
3008 which does not correspond particularly well to any function which
3009 actually appears in the source.
3012 static const struct d_standard_sub_info standard_subs[] =
3014 { 't', NL ("std"),
3015 NL ("std"),
3016 NULL, 0 },
3017 { 'a', NL ("std::allocator"),
3018 NL ("std::allocator"),
3019 NL ("allocator") },
3020 { 'b', NL ("std::basic_string"),
3021 NL ("std::basic_string"),
3022 NL ("basic_string") },
3023 { 's', NL ("std::string"),
3024 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3025 NL ("basic_string") },
3026 { 'i', NL ("std::istream"),
3027 NL ("std::basic_istream<char, std::char_traits<char> >"),
3028 NL ("basic_istream") },
3029 { 'o', NL ("std::ostream"),
3030 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3031 NL ("basic_ostream") },
3032 { 'd', NL ("std::iostream"),
3033 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3034 NL ("basic_iostream") }
3037 static struct demangle_component *
3038 d_substitution (struct d_info *di, int prefix)
3040 char c;
3042 if (! d_check_char (di, 'S'))
3043 return NULL;
3045 c = d_next_char (di);
3046 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3048 unsigned int id;
3050 id = 0;
3051 if (c != '_')
3055 unsigned int new_id;
3057 if (IS_DIGIT (c))
3058 new_id = id * 36 + c - '0';
3059 else if (IS_UPPER (c))
3060 new_id = id * 36 + c - 'A' + 10;
3061 else
3062 return NULL;
3063 if (new_id < id)
3064 return NULL;
3065 id = new_id;
3066 c = d_next_char (di);
3068 while (c != '_');
3070 ++id;
3073 if (id >= (unsigned int) di->next_sub)
3074 return NULL;
3076 ++di->did_subs;
3078 return di->subs[id];
3080 else
3082 int verbose;
3083 const struct d_standard_sub_info *p;
3084 const struct d_standard_sub_info *pend;
3086 verbose = (di->options & DMGL_VERBOSE) != 0;
3087 if (! verbose && prefix)
3089 char peek;
3091 peek = d_peek_char (di);
3092 if (peek == 'C' || peek == 'D')
3093 verbose = 1;
3096 pend = (&standard_subs[0]
3097 + sizeof standard_subs / sizeof standard_subs[0]);
3098 for (p = &standard_subs[0]; p < pend; ++p)
3100 if (c == p->code)
3102 const char *s;
3103 int len;
3105 if (p->set_last_name != NULL)
3106 di->last_name = d_make_sub (di, p->set_last_name,
3107 p->set_last_name_len);
3108 if (verbose)
3110 s = p->full_expansion;
3111 len = p->full_len;
3113 else
3115 s = p->simple_expansion;
3116 len = p->simple_len;
3118 di->expansion += len;
3119 return d_make_sub (di, s, len);
3123 return NULL;
3127 /* Initialize a growable string. */
3129 static void
3130 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3132 dgs->buf = NULL;
3133 dgs->len = 0;
3134 dgs->alc = 0;
3135 dgs->allocation_failure = 0;
3137 if (estimate > 0)
3138 d_growable_string_resize (dgs, estimate);
3141 /* Grow a growable string to a given size. */
3143 static inline void
3144 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3146 size_t newalc;
3147 char *newbuf;
3149 if (dgs->allocation_failure)
3150 return;
3152 /* Start allocation at two bytes to avoid any possibility of confusion
3153 with the special value of 1 used as a return in *palc to indicate
3154 allocation failures. */
3155 newalc = dgs->alc > 0 ? dgs->alc : 2;
3156 while (newalc < need)
3157 newalc <<= 1;
3159 newbuf = (char *) realloc (dgs->buf, newalc);
3160 if (newbuf == NULL)
3162 free (dgs->buf);
3163 dgs->buf = NULL;
3164 dgs->len = 0;
3165 dgs->alc = 0;
3166 dgs->allocation_failure = 1;
3167 return;
3169 dgs->buf = newbuf;
3170 dgs->alc = newalc;
3173 /* Append a buffer to a growable string. */
3175 static inline void
3176 d_growable_string_append_buffer (struct d_growable_string *dgs,
3177 const char *s, size_t l)
3179 size_t need;
3181 need = dgs->len + l + 1;
3182 if (need > dgs->alc)
3183 d_growable_string_resize (dgs, need);
3185 if (dgs->allocation_failure)
3186 return;
3188 memcpy (dgs->buf + dgs->len, s, l);
3189 dgs->buf[dgs->len + l] = '\0';
3190 dgs->len += l;
3193 /* Bridge growable strings to the callback mechanism. */
3195 static void
3196 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3198 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3200 d_growable_string_append_buffer (dgs, s, l);
3203 /* Initialize a print information structure. */
3205 static void
3206 d_print_init (struct d_print_info *dpi, int options,
3207 demangle_callbackref callback, void *opaque)
3209 dpi->options = options;
3210 dpi->len = 0;
3211 dpi->last_char = '\0';
3212 dpi->templates = NULL;
3213 dpi->modifiers = NULL;
3215 dpi->callback = callback;
3216 dpi->opaque = opaque;
3218 dpi->demangle_failure = 0;
3221 /* Indicate that an error occurred during printing, and test for error. */
3223 static inline void
3224 d_print_error (struct d_print_info *dpi)
3226 dpi->demangle_failure = 1;
3229 static inline int
3230 d_print_saw_error (struct d_print_info *dpi)
3232 return dpi->demangle_failure != 0;
3235 /* Flush buffered characters to the callback. */
3237 static inline void
3238 d_print_flush (struct d_print_info *dpi)
3240 dpi->buf[dpi->len] = '\0';
3241 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3242 dpi->len = 0;
3245 /* Append characters and buffers for printing. */
3247 static inline void
3248 d_append_char (struct d_print_info *dpi, char c)
3250 if (dpi->len == sizeof (dpi->buf) - 1)
3251 d_print_flush (dpi);
3253 dpi->buf[dpi->len++] = c;
3254 dpi->last_char = c;
3257 static inline void
3258 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3260 size_t i;
3262 for (i = 0; i < l; i++)
3263 d_append_char (dpi, s[i]);
3266 static inline void
3267 d_append_string (struct d_print_info *dpi, const char *s)
3269 d_append_buffer (dpi, s, strlen (s));
3272 static inline void
3273 d_append_num (struct d_print_info *dpi, long l)
3275 char buf[25];
3276 sprintf (buf,"%ld", l);
3277 d_append_string (dpi, buf);
3280 static inline char
3281 d_last_char (struct d_print_info *dpi)
3283 return dpi->last_char;
3286 /* Turn components into a human readable string. OPTIONS is the
3287 options bits passed to the demangler. DC is the tree to print.
3288 CALLBACK is a function to call to flush demangled string segments
3289 as they fill the intermediate buffer, and OPAQUE is a generalized
3290 callback argument. On success, this returns 1. On failure,
3291 it returns 0, indicating a bad parse. It does not use heap
3292 memory to build an output string, so cannot encounter memory
3293 allocation failure. */
3295 CP_STATIC_IF_GLIBCPP_V3
3297 cplus_demangle_print_callback (int options,
3298 const struct demangle_component *dc,
3299 demangle_callbackref callback, void *opaque)
3301 struct d_print_info dpi;
3303 d_print_init (&dpi, options, callback, opaque);
3305 d_print_comp (&dpi, dc);
3307 d_print_flush (&dpi);
3309 return ! d_print_saw_error (&dpi);
3312 /* Turn components into a human readable string. OPTIONS is the
3313 options bits passed to the demangler. DC is the tree to print.
3314 ESTIMATE is a guess at the length of the result. This returns a
3315 string allocated by malloc, or NULL on error. On success, this
3316 sets *PALC to the size of the allocated buffer. On failure, this
3317 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3318 failure. */
3320 CP_STATIC_IF_GLIBCPP_V3
3321 char *
3322 cplus_demangle_print (int options, const struct demangle_component *dc,
3323 int estimate, size_t *palc)
3325 struct d_growable_string dgs;
3327 d_growable_string_init (&dgs, estimate);
3329 if (! cplus_demangle_print_callback (options, dc,
3330 d_growable_string_callback_adapter,
3331 &dgs))
3333 free (dgs.buf);
3334 *palc = 0;
3335 return NULL;
3338 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3339 return dgs.buf;
3342 /* Returns the I'th element of the template arglist ARGS, or NULL on
3343 failure. */
3345 static struct demangle_component *
3346 d_index_template_argument (struct demangle_component *args, int i)
3348 struct demangle_component *a;
3350 for (a = args;
3351 a != NULL;
3352 a = d_right (a))
3354 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3355 return NULL;
3356 if (i <= 0)
3357 break;
3358 --i;
3360 if (i != 0 || a == NULL)
3361 return NULL;
3363 return d_left (a);
3366 /* Returns the template argument from the current context indicated by DC,
3367 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3369 static struct demangle_component *
3370 d_lookup_template_argument (struct d_print_info *dpi,
3371 const struct demangle_component *dc)
3373 if (dpi->templates == NULL)
3375 d_print_error (dpi);
3376 return NULL;
3379 return d_index_template_argument
3380 (d_right (dpi->templates->template_decl),
3381 dc->u.s_number.number);
3384 /* Returns a template argument pack used in DC (any will do), or NULL. */
3386 static struct demangle_component *
3387 d_find_pack (struct d_print_info *dpi,
3388 const struct demangle_component *dc)
3390 struct demangle_component *a;
3391 if (dc == NULL)
3392 return NULL;
3394 switch (dc->type)
3396 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3397 a = d_lookup_template_argument (dpi, dc);
3398 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3399 return a;
3400 return NULL;
3402 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3403 return NULL;
3405 case DEMANGLE_COMPONENT_NAME:
3406 case DEMANGLE_COMPONENT_OPERATOR:
3407 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3408 case DEMANGLE_COMPONENT_SUB_STD:
3409 case DEMANGLE_COMPONENT_CHARACTER:
3410 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3411 return NULL;
3413 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3414 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3415 case DEMANGLE_COMPONENT_CTOR:
3416 return d_find_pack (dpi, dc->u.s_ctor.name);
3417 case DEMANGLE_COMPONENT_DTOR:
3418 return d_find_pack (dpi, dc->u.s_dtor.name);
3420 default:
3421 a = d_find_pack (dpi, d_left (dc));
3422 if (a)
3423 return a;
3424 return d_find_pack (dpi, d_right (dc));
3428 /* Returns the length of the template argument pack DC. */
3430 static int
3431 d_pack_length (const struct demangle_component *dc)
3433 int count = 0;
3434 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3435 && d_left (dc) != NULL)
3437 ++count;
3438 dc = d_right (dc);
3440 return count;
3443 /* DC is a component of a mangled expression. Print it, wrapped in parens
3444 if needed. */
3446 static void
3447 d_print_subexpr (struct d_print_info *dpi,
3448 const struct demangle_component *dc)
3450 int simple = 0;
3451 if (dc->type == DEMANGLE_COMPONENT_NAME
3452 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3453 simple = 1;
3454 if (!simple)
3455 d_append_char (dpi, '(');
3456 d_print_comp (dpi, dc);
3457 if (!simple)
3458 d_append_char (dpi, ')');
3461 /* Subroutine to handle components. */
3463 static void
3464 d_print_comp (struct d_print_info *dpi,
3465 const struct demangle_component *dc)
3467 if (dc == NULL)
3469 d_print_error (dpi);
3470 return;
3472 if (d_print_saw_error (dpi))
3473 return;
3475 switch (dc->type)
3477 case DEMANGLE_COMPONENT_NAME:
3478 if ((dpi->options & DMGL_JAVA) == 0)
3479 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3480 else
3481 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3482 return;
3484 case DEMANGLE_COMPONENT_QUAL_NAME:
3485 case DEMANGLE_COMPONENT_LOCAL_NAME:
3486 d_print_comp (dpi, d_left (dc));
3487 if ((dpi->options & DMGL_JAVA) == 0)
3488 d_append_string (dpi, "::");
3489 else
3490 d_append_char (dpi, '.');
3491 d_print_comp (dpi, d_right (dc));
3492 return;
3494 case DEMANGLE_COMPONENT_TYPED_NAME:
3496 struct d_print_mod *hold_modifiers;
3497 struct demangle_component *typed_name;
3498 struct d_print_mod adpm[4];
3499 unsigned int i;
3500 struct d_print_template dpt;
3502 /* Pass the name down to the type so that it can be printed in
3503 the right place for the type. We also have to pass down
3504 any CV-qualifiers, which apply to the this parameter. */
3505 hold_modifiers = dpi->modifiers;
3506 dpi->modifiers = 0;
3507 i = 0;
3508 typed_name = d_left (dc);
3509 while (typed_name != NULL)
3511 if (i >= sizeof adpm / sizeof adpm[0])
3513 d_print_error (dpi);
3514 return;
3517 adpm[i].next = dpi->modifiers;
3518 dpi->modifiers = &adpm[i];
3519 adpm[i].mod = typed_name;
3520 adpm[i].printed = 0;
3521 adpm[i].templates = dpi->templates;
3522 ++i;
3524 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3525 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3526 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3527 break;
3529 typed_name = d_left (typed_name);
3532 if (typed_name == NULL)
3534 d_print_error (dpi);
3535 return;
3538 /* If typed_name is a template, then it applies to the
3539 function type as well. */
3540 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3542 dpt.next = dpi->templates;
3543 dpi->templates = &dpt;
3544 dpt.template_decl = typed_name;
3547 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3548 there may be CV-qualifiers on its right argument which
3549 really apply here; this happens when parsing a class which
3550 is local to a function. */
3551 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3553 struct demangle_component *local_name;
3555 local_name = d_right (typed_name);
3556 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3557 local_name = local_name->u.s_unary_num.sub;
3558 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3559 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3560 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3562 if (i >= sizeof adpm / sizeof adpm[0])
3564 d_print_error (dpi);
3565 return;
3568 adpm[i] = adpm[i - 1];
3569 adpm[i].next = &adpm[i - 1];
3570 dpi->modifiers = &adpm[i];
3572 adpm[i - 1].mod = local_name;
3573 adpm[i - 1].printed = 0;
3574 adpm[i - 1].templates = dpi->templates;
3575 ++i;
3577 local_name = d_left (local_name);
3581 d_print_comp (dpi, d_right (dc));
3583 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3584 dpi->templates = dpt.next;
3586 /* If the modifiers didn't get printed by the type, print them
3587 now. */
3588 while (i > 0)
3590 --i;
3591 if (! adpm[i].printed)
3593 d_append_char (dpi, ' ');
3594 d_print_mod (dpi, adpm[i].mod);
3598 dpi->modifiers = hold_modifiers;
3600 return;
3603 case DEMANGLE_COMPONENT_TEMPLATE:
3605 struct d_print_mod *hold_dpm;
3606 struct demangle_component *dcl;
3608 /* Don't push modifiers into a template definition. Doing so
3609 could give the wrong definition for a template argument.
3610 Instead, treat the template essentially as a name. */
3612 hold_dpm = dpi->modifiers;
3613 dpi->modifiers = NULL;
3615 dcl = d_left (dc);
3617 if ((dpi->options & DMGL_JAVA) != 0
3618 && dcl->type == DEMANGLE_COMPONENT_NAME
3619 && dcl->u.s_name.len == 6
3620 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3622 /* Special-case Java arrays, so that JArray<TYPE> appears
3623 instead as TYPE[]. */
3625 d_print_comp (dpi, d_right (dc));
3626 d_append_string (dpi, "[]");
3628 else
3630 d_print_comp (dpi, dcl);
3631 if (d_last_char (dpi) == '<')
3632 d_append_char (dpi, ' ');
3633 d_append_char (dpi, '<');
3634 d_print_comp (dpi, d_right (dc));
3635 /* Avoid generating two consecutive '>' characters, to avoid
3636 the C++ syntactic ambiguity. */
3637 if (d_last_char (dpi) == '>')
3638 d_append_char (dpi, ' ');
3639 d_append_char (dpi, '>');
3642 dpi->modifiers = hold_dpm;
3644 return;
3647 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3649 struct d_print_template *hold_dpt;
3650 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3652 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3653 a = d_index_template_argument (a, dpi->pack_index);
3655 if (a == NULL)
3657 d_print_error (dpi);
3658 return;
3661 /* While processing this parameter, we need to pop the list of
3662 templates. This is because the template parameter may
3663 itself be a reference to a parameter of an outer
3664 template. */
3666 hold_dpt = dpi->templates;
3667 dpi->templates = hold_dpt->next;
3669 d_print_comp (dpi, a);
3671 dpi->templates = hold_dpt;
3673 return;
3676 case DEMANGLE_COMPONENT_CTOR:
3677 d_print_comp (dpi, dc->u.s_ctor.name);
3678 return;
3680 case DEMANGLE_COMPONENT_DTOR:
3681 d_append_char (dpi, '~');
3682 d_print_comp (dpi, dc->u.s_dtor.name);
3683 return;
3685 case DEMANGLE_COMPONENT_VTABLE:
3686 d_append_string (dpi, "vtable for ");
3687 d_print_comp (dpi, d_left (dc));
3688 return;
3690 case DEMANGLE_COMPONENT_VTT:
3691 d_append_string (dpi, "VTT for ");
3692 d_print_comp (dpi, d_left (dc));
3693 return;
3695 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3696 d_append_string (dpi, "construction vtable for ");
3697 d_print_comp (dpi, d_left (dc));
3698 d_append_string (dpi, "-in-");
3699 d_print_comp (dpi, d_right (dc));
3700 return;
3702 case DEMANGLE_COMPONENT_TYPEINFO:
3703 d_append_string (dpi, "typeinfo for ");
3704 d_print_comp (dpi, d_left (dc));
3705 return;
3707 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3708 d_append_string (dpi, "typeinfo name for ");
3709 d_print_comp (dpi, d_left (dc));
3710 return;
3712 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3713 d_append_string (dpi, "typeinfo fn for ");
3714 d_print_comp (dpi, d_left (dc));
3715 return;
3717 case DEMANGLE_COMPONENT_THUNK:
3718 d_append_string (dpi, "non-virtual thunk to ");
3719 d_print_comp (dpi, d_left (dc));
3720 return;
3722 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3723 d_append_string (dpi, "virtual thunk to ");
3724 d_print_comp (dpi, d_left (dc));
3725 return;
3727 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3728 d_append_string (dpi, "covariant return thunk to ");
3729 d_print_comp (dpi, d_left (dc));
3730 return;
3732 case DEMANGLE_COMPONENT_JAVA_CLASS:
3733 d_append_string (dpi, "java Class for ");
3734 d_print_comp (dpi, d_left (dc));
3735 return;
3737 case DEMANGLE_COMPONENT_GUARD:
3738 d_append_string (dpi, "guard variable for ");
3739 d_print_comp (dpi, d_left (dc));
3740 return;
3742 case DEMANGLE_COMPONENT_REFTEMP:
3743 d_append_string (dpi, "reference temporary for ");
3744 d_print_comp (dpi, d_left (dc));
3745 return;
3747 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3748 d_append_string (dpi, "hidden alias for ");
3749 d_print_comp (dpi, d_left (dc));
3750 return;
3752 case DEMANGLE_COMPONENT_SUB_STD:
3753 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3754 return;
3756 case DEMANGLE_COMPONENT_RESTRICT:
3757 case DEMANGLE_COMPONENT_VOLATILE:
3758 case DEMANGLE_COMPONENT_CONST:
3760 struct d_print_mod *pdpm;
3762 /* When printing arrays, it's possible to have cases where the
3763 same CV-qualifier gets pushed on the stack multiple times.
3764 We only need to print it once. */
3766 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3768 if (! pdpm->printed)
3770 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3771 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3772 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3773 break;
3774 if (pdpm->mod->type == dc->type)
3776 d_print_comp (dpi, d_left (dc));
3777 return;
3782 /* Fall through. */
3783 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3784 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3785 case DEMANGLE_COMPONENT_CONST_THIS:
3786 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3787 case DEMANGLE_COMPONENT_POINTER:
3788 case DEMANGLE_COMPONENT_REFERENCE:
3789 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3790 case DEMANGLE_COMPONENT_COMPLEX:
3791 case DEMANGLE_COMPONENT_IMAGINARY:
3793 /* We keep a list of modifiers on the stack. */
3794 struct d_print_mod dpm;
3796 dpm.next = dpi->modifiers;
3797 dpi->modifiers = &dpm;
3798 dpm.mod = dc;
3799 dpm.printed = 0;
3800 dpm.templates = dpi->templates;
3802 d_print_comp (dpi, d_left (dc));
3804 /* If the modifier didn't get printed by the type, print it
3805 now. */
3806 if (! dpm.printed)
3807 d_print_mod (dpi, dc);
3809 dpi->modifiers = dpm.next;
3811 return;
3814 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3815 if ((dpi->options & DMGL_JAVA) == 0)
3816 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3817 dc->u.s_builtin.type->len);
3818 else
3819 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3820 dc->u.s_builtin.type->java_len);
3821 return;
3823 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3824 d_print_comp (dpi, d_left (dc));
3825 return;
3827 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3829 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3830 d_print_function_type (dpi, dc, dpi->modifiers);
3832 /* Print return type if present */
3833 if (d_left (dc) != NULL)
3835 struct d_print_mod dpm;
3837 /* We must pass this type down as a modifier in order to
3838 print it in the right location. */
3839 dpm.next = dpi->modifiers;
3840 dpi->modifiers = &dpm;
3841 dpm.mod = dc;
3842 dpm.printed = 0;
3843 dpm.templates = dpi->templates;
3845 d_print_comp (dpi, d_left (dc));
3847 dpi->modifiers = dpm.next;
3849 if (dpm.printed)
3850 return;
3852 /* In standard prefix notation, there is a space between the
3853 return type and the function signature. */
3854 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3855 d_append_char (dpi, ' ');
3858 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3859 d_print_function_type (dpi, dc, dpi->modifiers);
3861 return;
3864 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3866 struct d_print_mod *hold_modifiers;
3867 struct d_print_mod adpm[4];
3868 unsigned int i;
3869 struct d_print_mod *pdpm;
3871 /* We must pass this type down as a modifier in order to print
3872 multi-dimensional arrays correctly. If the array itself is
3873 CV-qualified, we act as though the element type were
3874 CV-qualified. We do this by copying the modifiers down
3875 rather than fiddling pointers, so that we don't wind up
3876 with a d_print_mod higher on the stack pointing into our
3877 stack frame after we return. */
3879 hold_modifiers = dpi->modifiers;
3881 adpm[0].next = hold_modifiers;
3882 dpi->modifiers = &adpm[0];
3883 adpm[0].mod = dc;
3884 adpm[0].printed = 0;
3885 adpm[0].templates = dpi->templates;
3887 i = 1;
3888 pdpm = hold_modifiers;
3889 while (pdpm != NULL
3890 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3891 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3892 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3894 if (! pdpm->printed)
3896 if (i >= sizeof adpm / sizeof adpm[0])
3898 d_print_error (dpi);
3899 return;
3902 adpm[i] = *pdpm;
3903 adpm[i].next = dpi->modifiers;
3904 dpi->modifiers = &adpm[i];
3905 pdpm->printed = 1;
3906 ++i;
3909 pdpm = pdpm->next;
3912 d_print_comp (dpi, d_right (dc));
3914 dpi->modifiers = hold_modifiers;
3916 if (adpm[0].printed)
3917 return;
3919 while (i > 1)
3921 --i;
3922 d_print_mod (dpi, adpm[i].mod);
3925 d_print_array_type (dpi, dc, dpi->modifiers);
3927 return;
3930 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3932 struct d_print_mod dpm;
3934 dpm.next = dpi->modifiers;
3935 dpi->modifiers = &dpm;
3936 dpm.mod = dc;
3937 dpm.printed = 0;
3938 dpm.templates = dpi->templates;
3940 d_print_comp (dpi, d_right (dc));
3942 /* If the modifier didn't get printed by the type, print it
3943 now. */
3944 if (! dpm.printed)
3946 d_append_char (dpi, ' ');
3947 d_print_comp (dpi, d_left (dc));
3948 d_append_string (dpi, "::*");
3951 dpi->modifiers = dpm.next;
3953 return;
3956 case DEMANGLE_COMPONENT_FIXED_TYPE:
3957 if (dc->u.s_fixed.sat)
3958 d_append_string (dpi, "_Sat ");
3959 /* Don't print "int _Accum". */
3960 if (dc->u.s_fixed.length->u.s_builtin.type
3961 != &cplus_demangle_builtin_types['i'-'a'])
3963 d_print_comp (dpi, dc->u.s_fixed.length);
3964 d_append_char (dpi, ' ');
3966 if (dc->u.s_fixed.accum)
3967 d_append_string (dpi, "_Accum");
3968 else
3969 d_append_string (dpi, "_Fract");
3970 return;
3972 case DEMANGLE_COMPONENT_ARGLIST:
3973 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3974 if (d_left (dc) != NULL)
3975 d_print_comp (dpi, d_left (dc));
3976 if (d_right (dc) != NULL)
3978 size_t len;
3979 d_append_string (dpi, ", ");
3980 len = dpi->len;
3981 d_print_comp (dpi, d_right (dc));
3982 /* If that didn't print anything (which can happen with empty
3983 template argument packs), remove the comma and space. */
3984 if (dpi->len == len)
3985 dpi->len -= 2;
3987 return;
3989 case DEMANGLE_COMPONENT_OPERATOR:
3991 char c;
3993 d_append_string (dpi, "operator");
3994 c = dc->u.s_operator.op->name[0];
3995 if (IS_LOWER (c))
3996 d_append_char (dpi, ' ');
3997 d_append_buffer (dpi, dc->u.s_operator.op->name,
3998 dc->u.s_operator.op->len);
3999 return;
4002 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4003 d_append_string (dpi, "operator ");
4004 d_print_comp (dpi, dc->u.s_extended_operator.name);
4005 return;
4007 case DEMANGLE_COMPONENT_CAST:
4008 d_append_string (dpi, "operator ");
4009 d_print_cast (dpi, dc);
4010 return;
4012 case DEMANGLE_COMPONENT_UNARY:
4013 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
4014 d_print_expr_op (dpi, d_left (dc));
4015 else
4017 d_append_char (dpi, '(');
4018 d_print_cast (dpi, d_left (dc));
4019 d_append_char (dpi, ')');
4021 d_print_subexpr (dpi, d_right (dc));
4022 return;
4024 case DEMANGLE_COMPONENT_BINARY:
4025 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4027 d_print_error (dpi);
4028 return;
4031 /* We wrap an expression which uses the greater-than operator in
4032 an extra layer of parens so that it does not get confused
4033 with the '>' which ends the template parameters. */
4034 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4035 && d_left (dc)->u.s_operator.op->len == 1
4036 && d_left (dc)->u.s_operator.op->name[0] == '>')
4037 d_append_char (dpi, '(');
4039 d_print_subexpr (dpi, d_left (d_right (dc)));
4040 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4041 d_print_expr_op (dpi, d_left (dc));
4042 d_print_subexpr (dpi, d_right (d_right (dc)));
4044 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4045 && d_left (dc)->u.s_operator.op->len == 1
4046 && d_left (dc)->u.s_operator.op->name[0] == '>')
4047 d_append_char (dpi, ')');
4049 return;
4051 case DEMANGLE_COMPONENT_BINARY_ARGS:
4052 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4053 d_print_error (dpi);
4054 return;
4056 case DEMANGLE_COMPONENT_TRINARY:
4057 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4058 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4060 d_print_error (dpi);
4061 return;
4063 d_print_subexpr (dpi, d_left (d_right (dc)));
4064 d_print_expr_op (dpi, d_left (dc));
4065 d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
4066 d_append_string (dpi, " : ");
4067 d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
4068 return;
4070 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4071 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4072 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4073 d_print_error (dpi);
4074 return;
4076 case DEMANGLE_COMPONENT_LITERAL:
4077 case DEMANGLE_COMPONENT_LITERAL_NEG:
4079 enum d_builtin_type_print tp;
4081 /* For some builtin types, produce simpler output. */
4082 tp = D_PRINT_DEFAULT;
4083 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4085 tp = d_left (dc)->u.s_builtin.type->print;
4086 switch (tp)
4088 case D_PRINT_INT:
4089 case D_PRINT_UNSIGNED:
4090 case D_PRINT_LONG:
4091 case D_PRINT_UNSIGNED_LONG:
4092 case D_PRINT_LONG_LONG:
4093 case D_PRINT_UNSIGNED_LONG_LONG:
4094 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4096 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4097 d_append_char (dpi, '-');
4098 d_print_comp (dpi, d_right (dc));
4099 switch (tp)
4101 default:
4102 break;
4103 case D_PRINT_UNSIGNED:
4104 d_append_char (dpi, 'u');
4105 break;
4106 case D_PRINT_LONG:
4107 d_append_char (dpi, 'l');
4108 break;
4109 case D_PRINT_UNSIGNED_LONG:
4110 d_append_string (dpi, "ul");
4111 break;
4112 case D_PRINT_LONG_LONG:
4113 d_append_string (dpi, "ll");
4114 break;
4115 case D_PRINT_UNSIGNED_LONG_LONG:
4116 d_append_string (dpi, "ull");
4117 break;
4119 return;
4121 break;
4123 case D_PRINT_BOOL:
4124 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4125 && d_right (dc)->u.s_name.len == 1
4126 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4128 switch (d_right (dc)->u.s_name.s[0])
4130 case '0':
4131 d_append_string (dpi, "false");
4132 return;
4133 case '1':
4134 d_append_string (dpi, "true");
4135 return;
4136 default:
4137 break;
4140 break;
4142 default:
4143 break;
4147 d_append_char (dpi, '(');
4148 d_print_comp (dpi, d_left (dc));
4149 d_append_char (dpi, ')');
4150 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4151 d_append_char (dpi, '-');
4152 if (tp == D_PRINT_FLOAT)
4153 d_append_char (dpi, '[');
4154 d_print_comp (dpi, d_right (dc));
4155 if (tp == D_PRINT_FLOAT)
4156 d_append_char (dpi, ']');
4158 return;
4160 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4161 d_append_string (dpi, "java resource ");
4162 d_print_comp (dpi, d_left (dc));
4163 return;
4165 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4166 d_print_comp (dpi, d_left (dc));
4167 d_print_comp (dpi, d_right (dc));
4168 return;
4170 case DEMANGLE_COMPONENT_CHARACTER:
4171 d_append_char (dpi, dc->u.s_character.character);
4172 return;
4174 case DEMANGLE_COMPONENT_DECLTYPE:
4175 d_append_string (dpi, "decltype (");
4176 d_print_comp (dpi, d_left (dc));
4177 d_append_char (dpi, ')');
4178 return;
4180 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4182 int len;
4183 int i;
4184 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4185 if (a == NULL)
4187 /* d_find_pack won't find anything if the only packs involved
4188 in this expansion are function parameter packs; in that
4189 case, just print the pattern and "...". */
4190 d_print_subexpr (dpi, d_left (dc));
4191 d_append_string (dpi, "...");
4192 return;
4195 len = d_pack_length (a);
4196 dc = d_left (dc);
4197 for (i = 0; i < len; ++i)
4199 dpi->pack_index = i;
4200 d_print_comp (dpi, dc);
4201 if (i < len-1)
4202 d_append_string (dpi, ", ");
4205 return;
4207 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4208 d_append_string (dpi, "{parm#");
4209 d_append_num (dpi, dc->u.s_number.number + 1);
4210 d_append_char (dpi, '}');
4211 return;
4213 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4214 d_append_string (dpi, "global constructors keyed to ");
4215 d_print_comp (dpi, dc->u.s_binary.left);
4216 return;
4218 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4219 d_append_string (dpi, "global destructors keyed to ");
4220 d_print_comp (dpi, dc->u.s_binary.left);
4221 return;
4223 case DEMANGLE_COMPONENT_LAMBDA:
4224 d_append_string (dpi, "{lambda(");
4225 d_print_comp (dpi, dc->u.s_unary_num.sub);
4226 d_append_string (dpi, ")#");
4227 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4228 d_append_char (dpi, '}');
4229 return;
4231 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4232 d_append_string (dpi, "{unnamed type#");
4233 d_append_num (dpi, dc->u.s_number.number + 1);
4234 d_append_char (dpi, '}');
4235 return;
4237 default:
4238 d_print_error (dpi);
4239 return;
4243 /* Print a Java dentifier. For Java we try to handle encoded extended
4244 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4245 so we don't it for C++. Characters are encoded as
4246 __U<hex-char>+_. */
4248 static void
4249 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4251 const char *p;
4252 const char *end;
4254 end = name + len;
4255 for (p = name; p < end; ++p)
4257 if (end - p > 3
4258 && p[0] == '_'
4259 && p[1] == '_'
4260 && p[2] == 'U')
4262 unsigned long c;
4263 const char *q;
4265 c = 0;
4266 for (q = p + 3; q < end; ++q)
4268 int dig;
4270 if (IS_DIGIT (*q))
4271 dig = *q - '0';
4272 else if (*q >= 'A' && *q <= 'F')
4273 dig = *q - 'A' + 10;
4274 else if (*q >= 'a' && *q <= 'f')
4275 dig = *q - 'a' + 10;
4276 else
4277 break;
4279 c = c * 16 + dig;
4281 /* If the Unicode character is larger than 256, we don't try
4282 to deal with it here. FIXME. */
4283 if (q < end && *q == '_' && c < 256)
4285 d_append_char (dpi, c);
4286 p = q;
4287 continue;
4291 d_append_char (dpi, *p);
4295 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4296 qualifiers on this after printing a function. */
4298 static void
4299 d_print_mod_list (struct d_print_info *dpi,
4300 struct d_print_mod *mods, int suffix)
4302 struct d_print_template *hold_dpt;
4304 if (mods == NULL || d_print_saw_error (dpi))
4305 return;
4307 if (mods->printed
4308 || (! suffix
4309 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4310 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4311 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4313 d_print_mod_list (dpi, mods->next, suffix);
4314 return;
4317 mods->printed = 1;
4319 hold_dpt = dpi->templates;
4320 dpi->templates = mods->templates;
4322 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4324 d_print_function_type (dpi, mods->mod, mods->next);
4325 dpi->templates = hold_dpt;
4326 return;
4328 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4330 d_print_array_type (dpi, mods->mod, mods->next);
4331 dpi->templates = hold_dpt;
4332 return;
4334 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4336 struct d_print_mod *hold_modifiers;
4337 struct demangle_component *dc;
4339 /* When this is on the modifier stack, we have pulled any
4340 qualifiers off the right argument already. Otherwise, we
4341 print it as usual, but don't let the left argument see any
4342 modifiers. */
4344 hold_modifiers = dpi->modifiers;
4345 dpi->modifiers = NULL;
4346 d_print_comp (dpi, d_left (mods->mod));
4347 dpi->modifiers = hold_modifiers;
4349 if ((dpi->options & DMGL_JAVA) == 0)
4350 d_append_string (dpi, "::");
4351 else
4352 d_append_char (dpi, '.');
4354 dc = d_right (mods->mod);
4356 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4358 d_append_string (dpi, "{default arg#");
4359 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4360 d_append_string (dpi, "}::");
4361 dc = dc->u.s_unary_num.sub;
4364 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4365 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4366 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4367 dc = d_left (dc);
4369 d_print_comp (dpi, dc);
4371 dpi->templates = hold_dpt;
4372 return;
4375 d_print_mod (dpi, mods->mod);
4377 dpi->templates = hold_dpt;
4379 d_print_mod_list (dpi, mods->next, suffix);
4382 /* Print a modifier. */
4384 static void
4385 d_print_mod (struct d_print_info *dpi,
4386 const struct demangle_component *mod)
4388 switch (mod->type)
4390 case DEMANGLE_COMPONENT_RESTRICT:
4391 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4392 d_append_string (dpi, " restrict");
4393 return;
4394 case DEMANGLE_COMPONENT_VOLATILE:
4395 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4396 d_append_string (dpi, " volatile");
4397 return;
4398 case DEMANGLE_COMPONENT_CONST:
4399 case DEMANGLE_COMPONENT_CONST_THIS:
4400 d_append_string (dpi, " const");
4401 return;
4402 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4403 d_append_char (dpi, ' ');
4404 d_print_comp (dpi, d_right (mod));
4405 return;
4406 case DEMANGLE_COMPONENT_POINTER:
4407 /* There is no pointer symbol in Java. */
4408 if ((dpi->options & DMGL_JAVA) == 0)
4409 d_append_char (dpi, '*');
4410 return;
4411 case DEMANGLE_COMPONENT_REFERENCE:
4412 d_append_char (dpi, '&');
4413 return;
4414 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4415 d_append_string (dpi, "&&");
4416 return;
4417 case DEMANGLE_COMPONENT_COMPLEX:
4418 d_append_string (dpi, "complex ");
4419 return;
4420 case DEMANGLE_COMPONENT_IMAGINARY:
4421 d_append_string (dpi, "imaginary ");
4422 return;
4423 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4424 if (d_last_char (dpi) != '(')
4425 d_append_char (dpi, ' ');
4426 d_print_comp (dpi, d_left (mod));
4427 d_append_string (dpi, "::*");
4428 return;
4429 case DEMANGLE_COMPONENT_TYPED_NAME:
4430 d_print_comp (dpi, d_left (mod));
4431 return;
4432 default:
4433 /* Otherwise, we have something that won't go back on the
4434 modifier stack, so we can just print it. */
4435 d_print_comp (dpi, mod);
4436 return;
4440 /* Print a function type, except for the return type. */
4442 static void
4443 d_print_function_type (struct d_print_info *dpi,
4444 const struct demangle_component *dc,
4445 struct d_print_mod *mods)
4447 int need_paren;
4448 int saw_mod;
4449 int need_space;
4450 struct d_print_mod *p;
4451 struct d_print_mod *hold_modifiers;
4453 need_paren = 0;
4454 saw_mod = 0;
4455 need_space = 0;
4456 for (p = mods; p != NULL; p = p->next)
4458 if (p->printed)
4459 break;
4461 saw_mod = 1;
4462 switch (p->mod->type)
4464 case DEMANGLE_COMPONENT_POINTER:
4465 case DEMANGLE_COMPONENT_REFERENCE:
4466 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4467 need_paren = 1;
4468 break;
4469 case DEMANGLE_COMPONENT_RESTRICT:
4470 case DEMANGLE_COMPONENT_VOLATILE:
4471 case DEMANGLE_COMPONENT_CONST:
4472 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4473 case DEMANGLE_COMPONENT_COMPLEX:
4474 case DEMANGLE_COMPONENT_IMAGINARY:
4475 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4476 need_space = 1;
4477 need_paren = 1;
4478 break;
4479 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4480 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4481 case DEMANGLE_COMPONENT_CONST_THIS:
4482 break;
4483 default:
4484 break;
4486 if (need_paren)
4487 break;
4490 if (d_left (dc) != NULL && ! saw_mod)
4491 need_paren = 1;
4493 if (need_paren)
4495 if (! need_space)
4497 if (d_last_char (dpi) != '('
4498 && d_last_char (dpi) != '*')
4499 need_space = 1;
4501 if (need_space && d_last_char (dpi) != ' ')
4502 d_append_char (dpi, ' ');
4503 d_append_char (dpi, '(');
4506 hold_modifiers = dpi->modifiers;
4507 dpi->modifiers = NULL;
4509 d_print_mod_list (dpi, mods, 0);
4511 if (need_paren)
4512 d_append_char (dpi, ')');
4514 d_append_char (dpi, '(');
4516 if (d_right (dc) != NULL)
4517 d_print_comp (dpi, d_right (dc));
4519 d_append_char (dpi, ')');
4521 d_print_mod_list (dpi, mods, 1);
4523 dpi->modifiers = hold_modifiers;
4526 /* Print an array type, except for the element type. */
4528 static void
4529 d_print_array_type (struct d_print_info *dpi,
4530 const struct demangle_component *dc,
4531 struct d_print_mod *mods)
4533 int need_space;
4535 need_space = 1;
4536 if (mods != NULL)
4538 int need_paren;
4539 struct d_print_mod *p;
4541 need_paren = 0;
4542 for (p = mods; p != NULL; p = p->next)
4544 if (! p->printed)
4546 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4548 need_space = 0;
4549 break;
4551 else
4553 need_paren = 1;
4554 need_space = 1;
4555 break;
4560 if (need_paren)
4561 d_append_string (dpi, " (");
4563 d_print_mod_list (dpi, mods, 0);
4565 if (need_paren)
4566 d_append_char (dpi, ')');
4569 if (need_space)
4570 d_append_char (dpi, ' ');
4572 d_append_char (dpi, '[');
4574 if (d_left (dc) != NULL)
4575 d_print_comp (dpi, d_left (dc));
4577 d_append_char (dpi, ']');
4580 /* Print an operator in an expression. */
4582 static void
4583 d_print_expr_op (struct d_print_info *dpi,
4584 const struct demangle_component *dc)
4586 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4587 d_append_buffer (dpi, dc->u.s_operator.op->name,
4588 dc->u.s_operator.op->len);
4589 else
4590 d_print_comp (dpi, dc);
4593 /* Print a cast. */
4595 static void
4596 d_print_cast (struct d_print_info *dpi,
4597 const struct demangle_component *dc)
4599 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4600 d_print_comp (dpi, d_left (dc));
4601 else
4603 struct d_print_mod *hold_dpm;
4604 struct d_print_template dpt;
4606 /* It appears that for a templated cast operator, we need to put
4607 the template parameters in scope for the operator name, but
4608 not for the parameters. The effect is that we need to handle
4609 the template printing here. */
4611 hold_dpm = dpi->modifiers;
4612 dpi->modifiers = NULL;
4614 dpt.next = dpi->templates;
4615 dpi->templates = &dpt;
4616 dpt.template_decl = d_left (dc);
4618 d_print_comp (dpi, d_left (d_left (dc)));
4620 dpi->templates = dpt.next;
4622 if (d_last_char (dpi) == '<')
4623 d_append_char (dpi, ' ');
4624 d_append_char (dpi, '<');
4625 d_print_comp (dpi, d_right (d_left (dc)));
4626 /* Avoid generating two consecutive '>' characters, to avoid
4627 the C++ syntactic ambiguity. */
4628 if (d_last_char (dpi) == '>')
4629 d_append_char (dpi, ' ');
4630 d_append_char (dpi, '>');
4632 dpi->modifiers = hold_dpm;
4636 /* Initialize the information structure we use to pass around
4637 information. */
4639 CP_STATIC_IF_GLIBCPP_V3
4640 void
4641 cplus_demangle_init_info (const char *mangled, int options, size_t len,
4642 struct d_info *di)
4644 di->s = mangled;
4645 di->send = mangled + len;
4646 di->options = options;
4648 di->n = mangled;
4650 /* We can not need more components than twice the number of chars in
4651 the mangled string. Most components correspond directly to
4652 chars, but the ARGLIST types are exceptions. */
4653 di->num_comps = 2 * len;
4654 di->next_comp = 0;
4656 /* Similarly, we can not need more substitutions than there are
4657 chars in the mangled string. */
4658 di->num_subs = len;
4659 di->next_sub = 0;
4660 di->did_subs = 0;
4662 di->last_name = NULL;
4664 di->expansion = 0;
4667 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4668 mangled name, return strings in repeated callback giving the demangled
4669 name. OPTIONS is the usual libiberty demangler options. On success,
4670 this returns 1. On failure, returns 0. */
4672 static int
4673 d_demangle_callback (const char *mangled, int options,
4674 demangle_callbackref callback, void *opaque)
4676 enum
4678 DCT_TYPE,
4679 DCT_MANGLED,
4680 DCT_GLOBAL_CTORS,
4681 DCT_GLOBAL_DTORS
4683 type;
4684 struct d_info di;
4685 struct demangle_component *dc;
4686 int status;
4688 if (mangled[0] == '_' && mangled[1] == 'Z')
4689 type = DCT_MANGLED;
4690 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4691 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4692 && (mangled[9] == 'D' || mangled[9] == 'I')
4693 && mangled[10] == '_')
4694 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
4695 else
4697 if ((options & DMGL_TYPES) == 0)
4698 return 0;
4699 type = DCT_TYPE;
4702 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4705 #ifdef CP_DYNAMIC_ARRAYS
4706 __extension__ struct demangle_component comps[di.num_comps];
4707 __extension__ struct demangle_component *subs[di.num_subs];
4709 di.comps = comps;
4710 di.subs = subs;
4711 #else
4712 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4713 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4714 #endif
4716 switch (type)
4718 case DCT_TYPE:
4719 dc = cplus_demangle_type (&di);
4720 break;
4721 case DCT_MANGLED:
4722 dc = cplus_demangle_mangled_name (&di, 1);
4723 break;
4724 case DCT_GLOBAL_CTORS:
4725 case DCT_GLOBAL_DTORS:
4726 d_advance (&di, 11);
4727 dc = d_make_comp (&di,
4728 (type == DCT_GLOBAL_CTORS
4729 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
4730 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
4731 d_make_name (&di, d_str (&di), strlen (d_str (&di))),
4732 NULL);
4733 d_advance (&di, strlen (d_str (&di)));
4734 break;
4737 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4738 mangled string, then we didn't successfully demangle it. If
4739 DMGL_PARAMS is not set, we didn't look at the trailing
4740 parameters. */
4741 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4742 dc = NULL;
4744 #ifdef CP_DEMANGLE_DEBUG
4745 d_dump (dc, 0);
4746 #endif
4748 status = (dc != NULL)
4749 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4750 : 0;
4753 return status;
4756 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4757 name, return a buffer allocated with malloc holding the demangled
4758 name. OPTIONS is the usual libiberty demangler options. On
4759 success, this sets *PALC to the allocated size of the returned
4760 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4761 a memory allocation failure, and returns NULL. */
4763 static char *
4764 d_demangle (const char *mangled, int options, size_t *palc)
4766 struct d_growable_string dgs;
4767 int status;
4769 d_growable_string_init (&dgs, 0);
4771 status = d_demangle_callback (mangled, options,
4772 d_growable_string_callback_adapter, &dgs);
4773 if (status == 0)
4775 free (dgs.buf);
4776 *palc = 0;
4777 return NULL;
4780 *palc = dgs.allocation_failure ? 1 : 0;
4781 return dgs.buf;
4784 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4786 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4788 /* ia64 ABI-mandated entry point in the C++ runtime library for
4789 performing demangling. MANGLED_NAME is a NUL-terminated character
4790 string containing the name to be demangled.
4792 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4793 *LENGTH bytes, into which the demangled name is stored. If
4794 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4795 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4796 is placed in a region of memory allocated with malloc.
4798 If LENGTH is non-NULL, the length of the buffer containing the
4799 demangled name, is placed in *LENGTH.
4801 The return value is a pointer to the start of the NUL-terminated
4802 demangled name, or NULL if the demangling fails. The caller is
4803 responsible for deallocating this memory using free.
4805 *STATUS is set to one of the following values:
4806 0: The demangling operation succeeded.
4807 -1: A memory allocation failure occurred.
4808 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4809 -3: One of the arguments is invalid.
4811 The demangling is performed using the C++ ABI mangling rules, with
4812 GNU extensions. */
4814 char *
4815 __cxa_demangle (const char *mangled_name, char *output_buffer,
4816 size_t *length, int *status)
4818 char *demangled;
4819 size_t alc;
4821 if (mangled_name == NULL)
4823 if (status != NULL)
4824 *status = -3;
4825 return NULL;
4828 if (output_buffer != NULL && length == NULL)
4830 if (status != NULL)
4831 *status = -3;
4832 return NULL;
4835 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4837 if (demangled == NULL)
4839 if (status != NULL)
4841 if (alc == 1)
4842 *status = -1;
4843 else
4844 *status = -2;
4846 return NULL;
4849 if (output_buffer == NULL)
4851 if (length != NULL)
4852 *length = alc;
4854 else
4856 if (strlen (demangled) < *length)
4858 strcpy (output_buffer, demangled);
4859 free (demangled);
4860 demangled = output_buffer;
4862 else
4864 free (output_buffer);
4865 *length = alc;
4869 if (status != NULL)
4870 *status = 0;
4872 return demangled;
4875 extern int __gcclibcxx_demangle_callback (const char *,
4876 void (*)
4877 (const char *, size_t, void *),
4878 void *);
4880 /* Alternative, allocationless entry point in the C++ runtime library
4881 for performing demangling. MANGLED_NAME is a NUL-terminated character
4882 string containing the name to be demangled.
4884 CALLBACK is a callback function, called with demangled string
4885 segments as demangling progresses; it is called at least once,
4886 but may be called more than once. OPAQUE is a generalized pointer
4887 used as a callback argument.
4889 The return code is one of the following values, equivalent to
4890 the STATUS values of __cxa_demangle() (excluding -1, since this
4891 function performs no memory allocations):
4892 0: The demangling operation succeeded.
4893 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4894 -3: One of the arguments is invalid.
4896 The demangling is performed using the C++ ABI mangling rules, with
4897 GNU extensions. */
4900 __gcclibcxx_demangle_callback (const char *mangled_name,
4901 void (*callback) (const char *, size_t, void *),
4902 void *opaque)
4904 int status;
4906 if (mangled_name == NULL || callback == NULL)
4907 return -3;
4909 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
4910 callback, opaque);
4911 if (status == 0)
4912 return -2;
4914 return 0;
4917 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4919 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4920 mangled name, return a buffer allocated with malloc holding the
4921 demangled name. Otherwise, return NULL. */
4923 char *
4924 cplus_demangle_v3 (const char *mangled, int options)
4926 size_t alc;
4928 return d_demangle (mangled, options, &alc);
4932 cplus_demangle_v3_callback (const char *mangled, int options,
4933 demangle_callbackref callback, void *opaque)
4935 return d_demangle_callback (mangled, options, callback, opaque);
4938 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4939 conventions, but the output formatting is a little different.
4940 This instructs the C++ demangler not to emit pointer characters ("*"), to
4941 use Java's namespace separator symbol ("." instead of "::"), and to output
4942 JArray<TYPE> as TYPE[]. */
4944 char *
4945 java_demangle_v3 (const char *mangled)
4947 size_t alc;
4949 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
4953 java_demangle_v3_callback (const char *mangled,
4954 demangle_callbackref callback, void *opaque)
4956 return d_demangle_callback (mangled,
4957 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4958 callback, opaque);
4961 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4963 #ifndef IN_GLIBCPP_V3
4965 /* Demangle a string in order to find out whether it is a constructor
4966 or destructor. Return non-zero on success. Set *CTOR_KIND and
4967 *DTOR_KIND appropriately. */
4969 static int
4970 is_ctor_or_dtor (const char *mangled,
4971 enum gnu_v3_ctor_kinds *ctor_kind,
4972 enum gnu_v3_dtor_kinds *dtor_kind)
4974 struct d_info di;
4975 struct demangle_component *dc;
4976 int ret;
4978 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4979 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4981 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4984 #ifdef CP_DYNAMIC_ARRAYS
4985 __extension__ struct demangle_component comps[di.num_comps];
4986 __extension__ struct demangle_component *subs[di.num_subs];
4988 di.comps = comps;
4989 di.subs = subs;
4990 #else
4991 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4992 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4993 #endif
4995 dc = cplus_demangle_mangled_name (&di, 1);
4997 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4998 to demangle the entire string. */
5000 ret = 0;
5001 while (dc != NULL)
5003 switch (dc->type)
5005 default:
5006 dc = NULL;
5007 break;
5008 case DEMANGLE_COMPONENT_TYPED_NAME:
5009 case DEMANGLE_COMPONENT_TEMPLATE:
5010 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5011 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5012 case DEMANGLE_COMPONENT_CONST_THIS:
5013 dc = d_left (dc);
5014 break;
5015 case DEMANGLE_COMPONENT_QUAL_NAME:
5016 case DEMANGLE_COMPONENT_LOCAL_NAME:
5017 dc = d_right (dc);
5018 break;
5019 case DEMANGLE_COMPONENT_CTOR:
5020 *ctor_kind = dc->u.s_ctor.kind;
5021 ret = 1;
5022 dc = NULL;
5023 break;
5024 case DEMANGLE_COMPONENT_DTOR:
5025 *dtor_kind = dc->u.s_dtor.kind;
5026 ret = 1;
5027 dc = NULL;
5028 break;
5033 return ret;
5036 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5037 name. A non-zero return indicates the type of constructor. */
5039 enum gnu_v3_ctor_kinds
5040 is_gnu_v3_mangled_ctor (const char *name)
5042 enum gnu_v3_ctor_kinds ctor_kind;
5043 enum gnu_v3_dtor_kinds dtor_kind;
5045 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5046 return (enum gnu_v3_ctor_kinds) 0;
5047 return ctor_kind;
5051 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5052 name. A non-zero return indicates the type of destructor. */
5054 enum gnu_v3_dtor_kinds
5055 is_gnu_v3_mangled_dtor (const char *name)
5057 enum gnu_v3_ctor_kinds ctor_kind;
5058 enum gnu_v3_dtor_kinds dtor_kind;
5060 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5061 return (enum gnu_v3_dtor_kinds) 0;
5062 return dtor_kind;
5065 #endif /* IN_GLIBCPP_V3 */
5067 #ifdef STANDALONE_DEMANGLER
5069 #include "getopt.h"
5070 #include "dyn-string.h"
5072 static void print_usage (FILE* fp, int exit_value);
5074 #define IS_ALPHA(CHAR) \
5075 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5076 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5078 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5079 #define is_mangled_char(CHAR) \
5080 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5081 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5083 /* The name of this program, as invoked. */
5084 const char* program_name;
5086 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5088 static void
5089 print_usage (FILE* fp, int exit_value)
5091 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5092 fprintf (fp, "Options:\n");
5093 fprintf (fp, " -h,--help Display this message.\n");
5094 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5095 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5096 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5098 exit (exit_value);
5101 /* Option specification for getopt_long. */
5102 static const struct option long_options[] =
5104 { "help", no_argument, NULL, 'h' },
5105 { "no-params", no_argument, NULL, 'p' },
5106 { "verbose", no_argument, NULL, 'v' },
5107 { NULL, no_argument, NULL, 0 },
5110 /* Main entry for a demangling filter executable. It will demangle
5111 its command line arguments, if any. If none are provided, it will
5112 filter stdin to stdout, replacing any recognized mangled C++ names
5113 with their demangled equivalents. */
5116 main (int argc, char *argv[])
5118 int i;
5119 int opt_char;
5120 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5122 /* Use the program name of this program, as invoked. */
5123 program_name = argv[0];
5125 /* Parse options. */
5128 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5129 switch (opt_char)
5131 case '?': /* Unrecognized option. */
5132 print_usage (stderr, 1);
5133 break;
5135 case 'h':
5136 print_usage (stdout, 0);
5137 break;
5139 case 'p':
5140 options &= ~ DMGL_PARAMS;
5141 break;
5143 case 'v':
5144 options |= DMGL_VERBOSE;
5145 break;
5148 while (opt_char != -1);
5150 if (optind == argc)
5151 /* No command line arguments were provided. Filter stdin. */
5153 dyn_string_t mangled = dyn_string_new (3);
5154 char *s;
5156 /* Read all of input. */
5157 while (!feof (stdin))
5159 char c;
5161 /* Pile characters into mangled until we hit one that can't
5162 occur in a mangled name. */
5163 c = getchar ();
5164 while (!feof (stdin) && is_mangled_char (c))
5166 dyn_string_append_char (mangled, c);
5167 if (feof (stdin))
5168 break;
5169 c = getchar ();
5172 if (dyn_string_length (mangled) > 0)
5174 #ifdef IN_GLIBCPP_V3
5175 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5176 #else
5177 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5178 #endif
5180 if (s != NULL)
5182 fputs (s, stdout);
5183 free (s);
5185 else
5187 /* It might not have been a mangled name. Print the
5188 original text. */
5189 fputs (dyn_string_buf (mangled), stdout);
5192 dyn_string_clear (mangled);
5195 /* If we haven't hit EOF yet, we've read one character that
5196 can't occur in a mangled name, so print it out. */
5197 if (!feof (stdin))
5198 putchar (c);
5201 dyn_string_delete (mangled);
5203 else
5204 /* Demangle command line arguments. */
5206 /* Loop over command line arguments. */
5207 for (i = optind; i < argc; ++i)
5209 char *s;
5210 #ifdef IN_GLIBCPP_V3
5211 int status;
5212 #endif
5214 /* Attempt to demangle. */
5215 #ifdef IN_GLIBCPP_V3
5216 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5217 #else
5218 s = cplus_demangle_v3 (argv[i], options);
5219 #endif
5221 /* If it worked, print the demangled name. */
5222 if (s != NULL)
5224 printf ("%s\n", s);
5225 free (s);
5227 else
5229 #ifdef IN_GLIBCPP_V3
5230 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5231 #else
5232 fprintf (stderr, "Failed: %s\n", argv[i]);
5233 #endif
5238 return 0;
5241 #endif /* STANDALONE_DEMANGLER */