2012-09-15 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / libiberty / cp-demangle.c
blob258aaa715504a293a677cc5d087b4929855a6bdf
1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 /* Fixed-length allocated buffer for demangled data, flushed to the
282 callback with a NUL termination once full. */
283 char buf[D_PRINT_BUFFER_LENGTH];
284 /* Current length of data in buffer. */
285 size_t len;
286 /* The last character printed, saved individually so that it survives
287 any buffer flush. */
288 char last_char;
289 /* Callback function to handle demangled buffer flush. */
290 demangle_callbackref callback;
291 /* Opaque callback argument. */
292 void *opaque;
293 /* The current list of templates, if any. */
294 struct d_print_template *templates;
295 /* The current list of modifiers (e.g., pointer, reference, etc.),
296 if any. */
297 struct d_print_mod *modifiers;
298 /* Set to 1 if we saw a demangling error. */
299 int demangle_failure;
300 /* The current index into any template argument packs we are using
301 for printing. */
302 int pack_index;
303 /* Number of d_print_flush calls so far. */
304 unsigned long int flush_count;
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_demangle_mangled_name (struct d_info *, const char *);
325 static struct demangle_component *
326 d_make_builtin_type (struct d_info *,
327 const struct demangle_builtin_type_info *);
329 static struct demangle_component *
330 d_make_operator (struct d_info *,
331 const struct demangle_operator_info *);
333 static struct demangle_component *
334 d_make_extended_operator (struct d_info *, int,
335 struct demangle_component *);
337 static struct demangle_component *
338 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
339 struct demangle_component *);
341 static struct demangle_component *
342 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
343 struct demangle_component *);
345 static struct demangle_component *
346 d_make_template_param (struct d_info *, long);
348 static struct demangle_component *
349 d_make_sub (struct d_info *, const char *, int);
351 static int
352 has_return_type (struct demangle_component *);
354 static int
355 is_ctor_dtor_or_conversion (struct demangle_component *);
357 static struct demangle_component *d_encoding (struct d_info *, int);
359 static struct demangle_component *d_name (struct d_info *);
361 static struct demangle_component *d_nested_name (struct d_info *);
363 static struct demangle_component *d_prefix (struct d_info *);
365 static struct demangle_component *d_unqualified_name (struct d_info *);
367 static struct demangle_component *d_source_name (struct d_info *);
369 static long d_number (struct d_info *);
371 static struct demangle_component *d_identifier (struct d_info *, int);
373 static struct demangle_component *d_operator_name (struct d_info *);
375 static struct demangle_component *d_special_name (struct d_info *);
377 static int d_call_offset (struct d_info *, int);
379 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
381 static struct demangle_component **
382 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
384 static struct demangle_component *
385 d_function_type (struct d_info *);
387 static struct demangle_component *
388 d_bare_function_type (struct d_info *, int);
390 static struct demangle_component *
391 d_class_enum_type (struct d_info *);
393 static struct demangle_component *d_array_type (struct d_info *);
395 static struct demangle_component *d_vector_type (struct d_info *);
397 static struct demangle_component *
398 d_pointer_to_member_type (struct d_info *);
400 static struct demangle_component *
401 d_template_param (struct d_info *);
403 static struct demangle_component *d_template_args (struct d_info *);
405 static struct demangle_component *
406 d_template_arg (struct d_info *);
408 static struct demangle_component *d_expression (struct d_info *);
410 static struct demangle_component *d_expr_primary (struct d_info *);
412 static struct demangle_component *d_local_name (struct d_info *);
414 static int d_discriminator (struct d_info *);
416 static struct demangle_component *d_lambda (struct d_info *);
418 static struct demangle_component *d_unnamed_type (struct d_info *);
420 static struct demangle_component *
421 d_clone_suffix (struct d_info *, struct demangle_component *);
423 static int
424 d_add_substitution (struct d_info *, struct demangle_component *);
426 static struct demangle_component *d_substitution (struct d_info *, int);
428 static void d_growable_string_init (struct d_growable_string *, size_t);
430 static inline void
431 d_growable_string_resize (struct d_growable_string *, size_t);
433 static inline void
434 d_growable_string_append_buffer (struct d_growable_string *,
435 const char *, size_t);
436 static void
437 d_growable_string_callback_adapter (const char *, size_t, void *);
439 static void
440 d_print_init (struct d_print_info *, demangle_callbackref, void *);
442 static inline void d_print_error (struct d_print_info *);
444 static inline int d_print_saw_error (struct d_print_info *);
446 static inline void d_print_flush (struct d_print_info *);
448 static inline void d_append_char (struct d_print_info *, char);
450 static inline void d_append_buffer (struct d_print_info *,
451 const char *, size_t);
453 static inline void d_append_string (struct d_print_info *, const char *);
455 static inline char d_last_char (struct d_print_info *);
457 static void
458 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
460 static void
461 d_print_java_identifier (struct d_print_info *, const char *, int);
463 static void
464 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
466 static void
467 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
469 static void
470 d_print_function_type (struct d_print_info *, int,
471 const struct demangle_component *,
472 struct d_print_mod *);
474 static void
475 d_print_array_type (struct d_print_info *, int,
476 const struct demangle_component *,
477 struct d_print_mod *);
479 static void
480 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
482 static void
483 d_print_cast (struct d_print_info *, int, const struct demangle_component *);
485 static int d_demangle_callback (const char *, int,
486 demangle_callbackref, void *);
487 static char *d_demangle (const char *, int, size_t *);
489 #ifdef CP_DEMANGLE_DEBUG
491 static void
492 d_dump (struct demangle_component *dc, int indent)
494 int i;
496 if (dc == NULL)
498 if (indent == 0)
499 printf ("failed demangling\n");
500 return;
503 for (i = 0; i < indent; ++i)
504 putchar (' ');
506 switch (dc->type)
508 case DEMANGLE_COMPONENT_NAME:
509 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
510 return;
511 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
512 printf ("template parameter %ld\n", dc->u.s_number.number);
513 return;
514 case DEMANGLE_COMPONENT_CTOR:
515 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
516 d_dump (dc->u.s_ctor.name, indent + 2);
517 return;
518 case DEMANGLE_COMPONENT_DTOR:
519 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
520 d_dump (dc->u.s_dtor.name, indent + 2);
521 return;
522 case DEMANGLE_COMPONENT_SUB_STD:
523 printf ("standard substitution %s\n", dc->u.s_string.string);
524 return;
525 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
526 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
527 return;
528 case DEMANGLE_COMPONENT_OPERATOR:
529 printf ("operator %s\n", dc->u.s_operator.op->name);
530 return;
531 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
532 printf ("extended operator with %d args\n",
533 dc->u.s_extended_operator.args);
534 d_dump (dc->u.s_extended_operator.name, indent + 2);
535 return;
537 case DEMANGLE_COMPONENT_QUAL_NAME:
538 printf ("qualified name\n");
539 break;
540 case DEMANGLE_COMPONENT_LOCAL_NAME:
541 printf ("local name\n");
542 break;
543 case DEMANGLE_COMPONENT_TYPED_NAME:
544 printf ("typed name\n");
545 break;
546 case DEMANGLE_COMPONENT_TEMPLATE:
547 printf ("template\n");
548 break;
549 case DEMANGLE_COMPONENT_VTABLE:
550 printf ("vtable\n");
551 break;
552 case DEMANGLE_COMPONENT_VTT:
553 printf ("VTT\n");
554 break;
555 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
556 printf ("construction vtable\n");
557 break;
558 case DEMANGLE_COMPONENT_TYPEINFO:
559 printf ("typeinfo\n");
560 break;
561 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
562 printf ("typeinfo name\n");
563 break;
564 case DEMANGLE_COMPONENT_TYPEINFO_FN:
565 printf ("typeinfo function\n");
566 break;
567 case DEMANGLE_COMPONENT_THUNK:
568 printf ("thunk\n");
569 break;
570 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
571 printf ("virtual thunk\n");
572 break;
573 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
574 printf ("covariant thunk\n");
575 break;
576 case DEMANGLE_COMPONENT_JAVA_CLASS:
577 printf ("java class\n");
578 break;
579 case DEMANGLE_COMPONENT_GUARD:
580 printf ("guard\n");
581 break;
582 case DEMANGLE_COMPONENT_REFTEMP:
583 printf ("reference temporary\n");
584 break;
585 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
586 printf ("hidden alias\n");
587 break;
588 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
589 printf ("transaction clone\n");
590 break;
591 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
592 printf ("non-transaction clone\n");
593 break;
594 case DEMANGLE_COMPONENT_RESTRICT:
595 printf ("restrict\n");
596 break;
597 case DEMANGLE_COMPONENT_VOLATILE:
598 printf ("volatile\n");
599 break;
600 case DEMANGLE_COMPONENT_CONST:
601 printf ("const\n");
602 break;
603 case DEMANGLE_COMPONENT_RESTRICT_THIS:
604 printf ("restrict this\n");
605 break;
606 case DEMANGLE_COMPONENT_VOLATILE_THIS:
607 printf ("volatile this\n");
608 break;
609 case DEMANGLE_COMPONENT_CONST_THIS:
610 printf ("const this\n");
611 break;
612 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
613 printf ("vendor type qualifier\n");
614 break;
615 case DEMANGLE_COMPONENT_POINTER:
616 printf ("pointer\n");
617 break;
618 case DEMANGLE_COMPONENT_REFERENCE:
619 printf ("reference\n");
620 break;
621 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
622 printf ("rvalue reference\n");
623 break;
624 case DEMANGLE_COMPONENT_COMPLEX:
625 printf ("complex\n");
626 break;
627 case DEMANGLE_COMPONENT_IMAGINARY:
628 printf ("imaginary\n");
629 break;
630 case DEMANGLE_COMPONENT_VENDOR_TYPE:
631 printf ("vendor type\n");
632 break;
633 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
634 printf ("function type\n");
635 break;
636 case DEMANGLE_COMPONENT_ARRAY_TYPE:
637 printf ("array type\n");
638 break;
639 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
640 printf ("pointer to member type\n");
641 break;
642 case DEMANGLE_COMPONENT_FIXED_TYPE:
643 printf ("fixed-point type\n");
644 break;
645 case DEMANGLE_COMPONENT_ARGLIST:
646 printf ("argument list\n");
647 break;
648 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
649 printf ("template argument list\n");
650 break;
651 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
652 printf ("initializer list\n");
653 break;
654 case DEMANGLE_COMPONENT_CAST:
655 printf ("cast\n");
656 break;
657 case DEMANGLE_COMPONENT_NULLARY:
658 printf ("nullary operator\n");
659 break;
660 case DEMANGLE_COMPONENT_UNARY:
661 printf ("unary operator\n");
662 break;
663 case DEMANGLE_COMPONENT_BINARY:
664 printf ("binary operator\n");
665 break;
666 case DEMANGLE_COMPONENT_BINARY_ARGS:
667 printf ("binary operator arguments\n");
668 break;
669 case DEMANGLE_COMPONENT_TRINARY:
670 printf ("trinary operator\n");
671 break;
672 case DEMANGLE_COMPONENT_TRINARY_ARG1:
673 printf ("trinary operator arguments 1\n");
674 break;
675 case DEMANGLE_COMPONENT_TRINARY_ARG2:
676 printf ("trinary operator arguments 1\n");
677 break;
678 case DEMANGLE_COMPONENT_LITERAL:
679 printf ("literal\n");
680 break;
681 case DEMANGLE_COMPONENT_LITERAL_NEG:
682 printf ("negative literal\n");
683 break;
684 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
685 printf ("java resource\n");
686 break;
687 case DEMANGLE_COMPONENT_COMPOUND_NAME:
688 printf ("compound name\n");
689 break;
690 case DEMANGLE_COMPONENT_CHARACTER:
691 printf ("character '%c'\n", dc->u.s_character.character);
692 return;
693 case DEMANGLE_COMPONENT_DECLTYPE:
694 printf ("decltype\n");
695 break;
696 case DEMANGLE_COMPONENT_PACK_EXPANSION:
697 printf ("pack expansion\n");
698 break;
701 d_dump (d_left (dc), indent + 2);
702 d_dump (d_right (dc), indent + 2);
705 #endif /* CP_DEMANGLE_DEBUG */
707 /* Fill in a DEMANGLE_COMPONENT_NAME. */
709 CP_STATIC_IF_GLIBCPP_V3
711 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
713 if (p == NULL || s == NULL || len == 0)
714 return 0;
715 p->type = DEMANGLE_COMPONENT_NAME;
716 p->u.s_name.s = s;
717 p->u.s_name.len = len;
718 return 1;
721 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
723 CP_STATIC_IF_GLIBCPP_V3
725 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
726 struct demangle_component *name)
728 if (p == NULL || args < 0 || name == NULL)
729 return 0;
730 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
731 p->u.s_extended_operator.args = args;
732 p->u.s_extended_operator.name = name;
733 return 1;
736 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
738 CP_STATIC_IF_GLIBCPP_V3
740 cplus_demangle_fill_ctor (struct demangle_component *p,
741 enum gnu_v3_ctor_kinds kind,
742 struct demangle_component *name)
744 if (p == NULL
745 || name == NULL
746 || (int) kind < gnu_v3_complete_object_ctor
747 || (int) kind > gnu_v3_object_ctor_group)
748 return 0;
749 p->type = DEMANGLE_COMPONENT_CTOR;
750 p->u.s_ctor.kind = kind;
751 p->u.s_ctor.name = name;
752 return 1;
755 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
757 CP_STATIC_IF_GLIBCPP_V3
759 cplus_demangle_fill_dtor (struct demangle_component *p,
760 enum gnu_v3_dtor_kinds kind,
761 struct demangle_component *name)
763 if (p == NULL
764 || name == NULL
765 || (int) kind < gnu_v3_deleting_dtor
766 || (int) kind > gnu_v3_object_dtor_group)
767 return 0;
768 p->type = DEMANGLE_COMPONENT_DTOR;
769 p->u.s_dtor.kind = kind;
770 p->u.s_dtor.name = name;
771 return 1;
774 /* Add a new component. */
776 static struct demangle_component *
777 d_make_empty (struct d_info *di)
779 struct demangle_component *p;
781 if (di->next_comp >= di->num_comps)
782 return NULL;
783 p = &di->comps[di->next_comp];
784 ++di->next_comp;
785 return p;
788 /* Add a new generic component. */
790 static struct demangle_component *
791 d_make_comp (struct d_info *di, enum demangle_component_type type,
792 struct demangle_component *left,
793 struct demangle_component *right)
795 struct demangle_component *p;
797 /* We check for errors here. A typical error would be a NULL return
798 from a subroutine. We catch those here, and return NULL
799 upward. */
800 switch (type)
802 /* These types require two parameters. */
803 case DEMANGLE_COMPONENT_QUAL_NAME:
804 case DEMANGLE_COMPONENT_LOCAL_NAME:
805 case DEMANGLE_COMPONENT_TYPED_NAME:
806 case DEMANGLE_COMPONENT_TEMPLATE:
807 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
808 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
809 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
810 case DEMANGLE_COMPONENT_UNARY:
811 case DEMANGLE_COMPONENT_BINARY:
812 case DEMANGLE_COMPONENT_BINARY_ARGS:
813 case DEMANGLE_COMPONENT_TRINARY:
814 case DEMANGLE_COMPONENT_TRINARY_ARG1:
815 case DEMANGLE_COMPONENT_LITERAL:
816 case DEMANGLE_COMPONENT_LITERAL_NEG:
817 case DEMANGLE_COMPONENT_COMPOUND_NAME:
818 case DEMANGLE_COMPONENT_VECTOR_TYPE:
819 case DEMANGLE_COMPONENT_CLONE:
820 if (left == NULL || right == NULL)
821 return NULL;
822 break;
824 /* These types only require one parameter. */
825 case DEMANGLE_COMPONENT_VTABLE:
826 case DEMANGLE_COMPONENT_VTT:
827 case DEMANGLE_COMPONENT_TYPEINFO:
828 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
829 case DEMANGLE_COMPONENT_TYPEINFO_FN:
830 case DEMANGLE_COMPONENT_THUNK:
831 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
832 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
833 case DEMANGLE_COMPONENT_JAVA_CLASS:
834 case DEMANGLE_COMPONENT_GUARD:
835 case DEMANGLE_COMPONENT_REFTEMP:
836 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
837 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
838 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
839 case DEMANGLE_COMPONENT_POINTER:
840 case DEMANGLE_COMPONENT_REFERENCE:
841 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
842 case DEMANGLE_COMPONENT_COMPLEX:
843 case DEMANGLE_COMPONENT_IMAGINARY:
844 case DEMANGLE_COMPONENT_VENDOR_TYPE:
845 case DEMANGLE_COMPONENT_CAST:
846 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
847 case DEMANGLE_COMPONENT_DECLTYPE:
848 case DEMANGLE_COMPONENT_PACK_EXPANSION:
849 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
850 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
851 case DEMANGLE_COMPONENT_NULLARY:
852 case DEMANGLE_COMPONENT_TRINARY_ARG2:
853 if (left == NULL)
854 return NULL;
855 break;
857 /* This needs a right parameter, but the left parameter can be
858 empty. */
859 case DEMANGLE_COMPONENT_ARRAY_TYPE:
860 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
861 if (right == NULL)
862 return NULL;
863 break;
865 /* These are allowed to have no parameters--in some cases they
866 will be filled in later. */
867 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
868 case DEMANGLE_COMPONENT_RESTRICT:
869 case DEMANGLE_COMPONENT_VOLATILE:
870 case DEMANGLE_COMPONENT_CONST:
871 case DEMANGLE_COMPONENT_RESTRICT_THIS:
872 case DEMANGLE_COMPONENT_VOLATILE_THIS:
873 case DEMANGLE_COMPONENT_CONST_THIS:
874 case DEMANGLE_COMPONENT_ARGLIST:
875 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
876 break;
878 /* Other types should not be seen here. */
879 default:
880 return NULL;
883 p = d_make_empty (di);
884 if (p != NULL)
886 p->type = type;
887 p->u.s_binary.left = left;
888 p->u.s_binary.right = right;
890 return p;
893 /* Add a new demangle mangled name component. */
895 static struct demangle_component *
896 d_make_demangle_mangled_name (struct d_info *di, const char *s)
898 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
899 return d_make_name (di, s, strlen (s));
900 d_advance (di, 2);
901 return d_encoding (di, 0);
904 /* Add a new name component. */
906 static struct demangle_component *
907 d_make_name (struct d_info *di, const char *s, int len)
909 struct demangle_component *p;
911 p = d_make_empty (di);
912 if (! cplus_demangle_fill_name (p, s, len))
913 return NULL;
914 return p;
917 /* Add a new builtin type component. */
919 static struct demangle_component *
920 d_make_builtin_type (struct d_info *di,
921 const struct demangle_builtin_type_info *type)
923 struct demangle_component *p;
925 if (type == NULL)
926 return NULL;
927 p = d_make_empty (di);
928 if (p != NULL)
930 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
931 p->u.s_builtin.type = type;
933 return p;
936 /* Add a new operator component. */
938 static struct demangle_component *
939 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
941 struct demangle_component *p;
943 p = d_make_empty (di);
944 if (p != NULL)
946 p->type = DEMANGLE_COMPONENT_OPERATOR;
947 p->u.s_operator.op = op;
949 return p;
952 /* Add a new extended operator component. */
954 static struct demangle_component *
955 d_make_extended_operator (struct d_info *di, int args,
956 struct demangle_component *name)
958 struct demangle_component *p;
960 p = d_make_empty (di);
961 if (! cplus_demangle_fill_extended_operator (p, args, name))
962 return NULL;
963 return p;
966 static struct demangle_component *
967 d_make_default_arg (struct d_info *di, int num,
968 struct demangle_component *sub)
970 struct demangle_component *p = d_make_empty (di);
971 if (p)
973 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
974 p->u.s_unary_num.num = num;
975 p->u.s_unary_num.sub = sub;
977 return p;
980 /* Add a new constructor component. */
982 static struct demangle_component *
983 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
984 struct demangle_component *name)
986 struct demangle_component *p;
988 p = d_make_empty (di);
989 if (! cplus_demangle_fill_ctor (p, kind, name))
990 return NULL;
991 return p;
994 /* Add a new destructor component. */
996 static struct demangle_component *
997 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
998 struct demangle_component *name)
1000 struct demangle_component *p;
1002 p = d_make_empty (di);
1003 if (! cplus_demangle_fill_dtor (p, kind, name))
1004 return NULL;
1005 return p;
1008 /* Add a new template parameter. */
1010 static struct demangle_component *
1011 d_make_template_param (struct d_info *di, long i)
1013 struct demangle_component *p;
1015 p = d_make_empty (di);
1016 if (p != NULL)
1018 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1019 p->u.s_number.number = i;
1021 return p;
1024 /* Add a new function parameter. */
1026 static struct demangle_component *
1027 d_make_function_param (struct d_info *di, long i)
1029 struct demangle_component *p;
1031 p = d_make_empty (di);
1032 if (p != NULL)
1034 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1035 p->u.s_number.number = i;
1037 return p;
1040 /* Add a new standard substitution component. */
1042 static struct demangle_component *
1043 d_make_sub (struct d_info *di, const char *name, int len)
1045 struct demangle_component *p;
1047 p = d_make_empty (di);
1048 if (p != NULL)
1050 p->type = DEMANGLE_COMPONENT_SUB_STD;
1051 p->u.s_string.string = name;
1052 p->u.s_string.len = len;
1054 return p;
1057 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1059 TOP_LEVEL is non-zero when called at the top level. */
1061 CP_STATIC_IF_GLIBCPP_V3
1062 struct demangle_component *
1063 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1065 struct demangle_component *p;
1067 if (! d_check_char (di, '_')
1068 /* Allow missing _ if not at toplevel to work around a
1069 bug in G++ abi-version=2 mangling; see the comment in
1070 write_template_arg. */
1071 && top_level)
1072 return NULL;
1073 if (! d_check_char (di, 'Z'))
1074 return NULL;
1075 p = d_encoding (di, top_level);
1077 /* If at top level and parsing parameters, check for a clone
1078 suffix. */
1079 if (top_level && (di->options & DMGL_PARAMS) != 0)
1080 while (d_peek_char (di) == '.'
1081 && (IS_LOWER (d_peek_next_char (di))
1082 || d_peek_next_char (di) == '_'
1083 || IS_DIGIT (d_peek_next_char (di))))
1084 p = d_clone_suffix (di, p);
1086 return p;
1089 /* Return whether a function should have a return type. The argument
1090 is the function name, which may be qualified in various ways. The
1091 rules are that template functions have return types with some
1092 exceptions, function types which are not part of a function name
1093 mangling have return types with some exceptions, and non-template
1094 function names do not have return types. The exceptions are that
1095 constructors, destructors, and conversion operators do not have
1096 return types. */
1098 static int
1099 has_return_type (struct demangle_component *dc)
1101 if (dc == NULL)
1102 return 0;
1103 switch (dc->type)
1105 default:
1106 return 0;
1107 case DEMANGLE_COMPONENT_TEMPLATE:
1108 return ! is_ctor_dtor_or_conversion (d_left (dc));
1109 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1110 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1111 case DEMANGLE_COMPONENT_CONST_THIS:
1112 return has_return_type (d_left (dc));
1116 /* Return whether a name is a constructor, a destructor, or a
1117 conversion operator. */
1119 static int
1120 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1122 if (dc == NULL)
1123 return 0;
1124 switch (dc->type)
1126 default:
1127 return 0;
1128 case DEMANGLE_COMPONENT_QUAL_NAME:
1129 case DEMANGLE_COMPONENT_LOCAL_NAME:
1130 return is_ctor_dtor_or_conversion (d_right (dc));
1131 case DEMANGLE_COMPONENT_CTOR:
1132 case DEMANGLE_COMPONENT_DTOR:
1133 case DEMANGLE_COMPONENT_CAST:
1134 return 1;
1138 /* <encoding> ::= <(function) name> <bare-function-type>
1139 ::= <(data) name>
1140 ::= <special-name>
1142 TOP_LEVEL is non-zero when called at the top level, in which case
1143 if DMGL_PARAMS is not set we do not demangle the function
1144 parameters. We only set this at the top level, because otherwise
1145 we would not correctly demangle names in local scopes. */
1147 static struct demangle_component *
1148 d_encoding (struct d_info *di, int top_level)
1150 char peek = d_peek_char (di);
1152 if (peek == 'G' || peek == 'T')
1153 return d_special_name (di);
1154 else
1156 struct demangle_component *dc;
1158 dc = d_name (di);
1160 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1162 /* Strip off any initial CV-qualifiers, as they really apply
1163 to the `this' parameter, and they were not output by the
1164 v2 demangler without DMGL_PARAMS. */
1165 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1166 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1167 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1168 dc = d_left (dc);
1170 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1171 there may be CV-qualifiers on its right argument which
1172 really apply here; this happens when parsing a class
1173 which is local to a function. */
1174 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1176 struct demangle_component *dcr;
1178 dcr = d_right (dc);
1179 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1180 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1181 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1182 dcr = d_left (dcr);
1183 dc->u.s_binary.right = dcr;
1186 return dc;
1189 peek = d_peek_char (di);
1190 if (dc == NULL || peek == '\0' || peek == 'E')
1191 return dc;
1192 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1193 d_bare_function_type (di, has_return_type (dc)));
1197 /* <name> ::= <nested-name>
1198 ::= <unscoped-name>
1199 ::= <unscoped-template-name> <template-args>
1200 ::= <local-name>
1202 <unscoped-name> ::= <unqualified-name>
1203 ::= St <unqualified-name>
1205 <unscoped-template-name> ::= <unscoped-name>
1206 ::= <substitution>
1209 static struct demangle_component *
1210 d_name (struct d_info *di)
1212 char peek = d_peek_char (di);
1213 struct demangle_component *dc;
1215 switch (peek)
1217 case 'N':
1218 return d_nested_name (di);
1220 case 'Z':
1221 return d_local_name (di);
1223 case 'L':
1224 case 'U':
1225 return d_unqualified_name (di);
1227 case 'S':
1229 int subst;
1231 if (d_peek_next_char (di) != 't')
1233 dc = d_substitution (di, 0);
1234 subst = 1;
1236 else
1238 d_advance (di, 2);
1239 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1240 d_make_name (di, "std", 3),
1241 d_unqualified_name (di));
1242 di->expansion += 3;
1243 subst = 0;
1246 if (d_peek_char (di) != 'I')
1248 /* The grammar does not permit this case to occur if we
1249 called d_substitution() above (i.e., subst == 1). We
1250 don't bother to check. */
1252 else
1254 /* This is <template-args>, which means that we just saw
1255 <unscoped-template-name>, which is a substitution
1256 candidate if we didn't just get it from a
1257 substitution. */
1258 if (! subst)
1260 if (! d_add_substitution (di, dc))
1261 return NULL;
1263 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1264 d_template_args (di));
1267 return dc;
1270 default:
1271 dc = d_unqualified_name (di);
1272 if (d_peek_char (di) == 'I')
1274 /* This is <template-args>, which means that we just saw
1275 <unscoped-template-name>, which is a substitution
1276 candidate. */
1277 if (! d_add_substitution (di, dc))
1278 return NULL;
1279 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1280 d_template_args (di));
1282 return dc;
1286 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1287 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1290 static struct demangle_component *
1291 d_nested_name (struct d_info *di)
1293 struct demangle_component *ret;
1294 struct demangle_component **pret;
1296 if (! d_check_char (di, 'N'))
1297 return NULL;
1299 pret = d_cv_qualifiers (di, &ret, 1);
1300 if (pret == NULL)
1301 return NULL;
1303 *pret = d_prefix (di);
1304 if (*pret == NULL)
1305 return NULL;
1307 if (! d_check_char (di, 'E'))
1308 return NULL;
1310 return ret;
1313 /* <prefix> ::= <prefix> <unqualified-name>
1314 ::= <template-prefix> <template-args>
1315 ::= <template-param>
1316 ::= <decltype>
1318 ::= <substitution>
1320 <template-prefix> ::= <prefix> <(template) unqualified-name>
1321 ::= <template-param>
1322 ::= <substitution>
1325 static struct demangle_component *
1326 d_prefix (struct d_info *di)
1328 struct demangle_component *ret = NULL;
1330 while (1)
1332 char peek;
1333 enum demangle_component_type comb_type;
1334 struct demangle_component *dc;
1336 peek = d_peek_char (di);
1337 if (peek == '\0')
1338 return NULL;
1340 /* The older code accepts a <local-name> here, but I don't see
1341 that in the grammar. The older code does not accept a
1342 <template-param> here. */
1344 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1345 if (peek == 'D')
1347 char peek2 = d_peek_next_char (di);
1348 if (peek2 == 'T' || peek2 == 't')
1349 /* Decltype. */
1350 dc = cplus_demangle_type (di);
1351 else
1352 /* Destructor name. */
1353 dc = d_unqualified_name (di);
1355 else if (IS_DIGIT (peek)
1356 || IS_LOWER (peek)
1357 || peek == 'C'
1358 || peek == 'U'
1359 || peek == 'L')
1360 dc = d_unqualified_name (di);
1361 else if (peek == 'S')
1362 dc = d_substitution (di, 1);
1363 else if (peek == 'I')
1365 if (ret == NULL)
1366 return NULL;
1367 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1368 dc = d_template_args (di);
1370 else if (peek == 'T')
1371 dc = d_template_param (di);
1372 else if (peek == 'E')
1373 return ret;
1374 else if (peek == 'M')
1376 /* Initializer scope for a lambda. We don't need to represent
1377 this; the normal code will just treat the variable as a type
1378 scope, which gives appropriate output. */
1379 if (ret == NULL)
1380 return NULL;
1381 d_advance (di, 1);
1382 continue;
1384 else
1385 return NULL;
1387 if (ret == NULL)
1388 ret = dc;
1389 else
1390 ret = d_make_comp (di, comb_type, ret, dc);
1392 if (peek != 'S' && d_peek_char (di) != 'E')
1394 if (! d_add_substitution (di, ret))
1395 return NULL;
1400 /* <unqualified-name> ::= <operator-name>
1401 ::= <ctor-dtor-name>
1402 ::= <source-name>
1403 ::= <local-source-name>
1405 <local-source-name> ::= L <source-name> <discriminator>
1408 static struct demangle_component *
1409 d_unqualified_name (struct d_info *di)
1411 char peek;
1413 peek = d_peek_char (di);
1414 if (IS_DIGIT (peek))
1415 return d_source_name (di);
1416 else if (IS_LOWER (peek))
1418 struct demangle_component *ret;
1420 ret = d_operator_name (di);
1421 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1423 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1424 if (!strcmp (ret->u.s_operator.op->code, "li"))
1425 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1426 d_source_name (di));
1428 return ret;
1430 else if (peek == 'C' || peek == 'D')
1431 return d_ctor_dtor_name (di);
1432 else if (peek == 'L')
1434 struct demangle_component * ret;
1436 d_advance (di, 1);
1438 ret = d_source_name (di);
1439 if (ret == NULL)
1440 return NULL;
1441 if (! d_discriminator (di))
1442 return NULL;
1443 return ret;
1445 else if (peek == 'U')
1447 switch (d_peek_next_char (di))
1449 case 'l':
1450 return d_lambda (di);
1451 case 't':
1452 return d_unnamed_type (di);
1453 default:
1454 return NULL;
1457 else
1458 return NULL;
1461 /* <source-name> ::= <(positive length) number> <identifier> */
1463 static struct demangle_component *
1464 d_source_name (struct d_info *di)
1466 long len;
1467 struct demangle_component *ret;
1469 len = d_number (di);
1470 if (len <= 0)
1471 return NULL;
1472 ret = d_identifier (di, len);
1473 di->last_name = ret;
1474 return ret;
1477 /* number ::= [n] <(non-negative decimal integer)> */
1479 static long
1480 d_number (struct d_info *di)
1482 int negative;
1483 char peek;
1484 long ret;
1486 negative = 0;
1487 peek = d_peek_char (di);
1488 if (peek == 'n')
1490 negative = 1;
1491 d_advance (di, 1);
1492 peek = d_peek_char (di);
1495 ret = 0;
1496 while (1)
1498 if (! IS_DIGIT (peek))
1500 if (negative)
1501 ret = - ret;
1502 return ret;
1504 ret = ret * 10 + peek - '0';
1505 d_advance (di, 1);
1506 peek = d_peek_char (di);
1510 /* Like d_number, but returns a demangle_component. */
1512 static struct demangle_component *
1513 d_number_component (struct d_info *di)
1515 struct demangle_component *ret = d_make_empty (di);
1516 if (ret)
1518 ret->type = DEMANGLE_COMPONENT_NUMBER;
1519 ret->u.s_number.number = d_number (di);
1521 return ret;
1524 /* identifier ::= <(unqualified source code identifier)> */
1526 static struct demangle_component *
1527 d_identifier (struct d_info *di, int len)
1529 const char *name;
1531 name = d_str (di);
1533 if (di->send - name < len)
1534 return NULL;
1536 d_advance (di, len);
1538 /* A Java mangled name may have a trailing '$' if it is a C++
1539 keyword. This '$' is not included in the length count. We just
1540 ignore the '$'. */
1541 if ((di->options & DMGL_JAVA) != 0
1542 && d_peek_char (di) == '$')
1543 d_advance (di, 1);
1545 /* Look for something which looks like a gcc encoding of an
1546 anonymous namespace, and replace it with a more user friendly
1547 name. */
1548 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1549 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1550 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1552 const char *s;
1554 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1555 if ((*s == '.' || *s == '_' || *s == '$')
1556 && s[1] == 'N')
1558 di->expansion -= len - sizeof "(anonymous namespace)";
1559 return d_make_name (di, "(anonymous namespace)",
1560 sizeof "(anonymous namespace)" - 1);
1564 return d_make_name (di, name, len);
1567 /* operator_name ::= many different two character encodings.
1568 ::= cv <type>
1569 ::= v <digit> <source-name>
1571 This list is sorted for binary search. */
1573 #define NL(s) s, (sizeof s) - 1
1575 CP_STATIC_IF_GLIBCPP_V3
1576 const struct demangle_operator_info cplus_demangle_operators[] =
1578 { "aN", NL ("&="), 2 },
1579 { "aS", NL ("="), 2 },
1580 { "aa", NL ("&&"), 2 },
1581 { "ad", NL ("&"), 1 },
1582 { "an", NL ("&"), 2 },
1583 { "at", NL ("alignof "), 1 },
1584 { "az", NL ("alignof "), 1 },
1585 { "cc", NL ("const_cast"), 2 },
1586 { "cl", NL ("()"), 2 },
1587 { "cm", NL (","), 2 },
1588 { "co", NL ("~"), 1 },
1589 { "dV", NL ("/="), 2 },
1590 { "da", NL ("delete[] "), 1 },
1591 { "dc", NL ("dynamic_cast"), 2 },
1592 { "de", NL ("*"), 1 },
1593 { "dl", NL ("delete "), 1 },
1594 { "ds", NL (".*"), 2 },
1595 { "dt", NL ("."), 2 },
1596 { "dv", NL ("/"), 2 },
1597 { "eO", NL ("^="), 2 },
1598 { "eo", NL ("^"), 2 },
1599 { "eq", NL ("=="), 2 },
1600 { "ge", NL (">="), 2 },
1601 { "gs", NL ("::"), 1 },
1602 { "gt", NL (">"), 2 },
1603 { "ix", NL ("[]"), 2 },
1604 { "lS", NL ("<<="), 2 },
1605 { "le", NL ("<="), 2 },
1606 { "li", NL ("operator\"\" "), 1 },
1607 { "ls", NL ("<<"), 2 },
1608 { "lt", NL ("<"), 2 },
1609 { "mI", NL ("-="), 2 },
1610 { "mL", NL ("*="), 2 },
1611 { "mi", NL ("-"), 2 },
1612 { "ml", NL ("*"), 2 },
1613 { "mm", NL ("--"), 1 },
1614 { "na", NL ("new[]"), 3 },
1615 { "ne", NL ("!="), 2 },
1616 { "ng", NL ("-"), 1 },
1617 { "nt", NL ("!"), 1 },
1618 { "nw", NL ("new"), 3 },
1619 { "oR", NL ("|="), 2 },
1620 { "oo", NL ("||"), 2 },
1621 { "or", NL ("|"), 2 },
1622 { "pL", NL ("+="), 2 },
1623 { "pl", NL ("+"), 2 },
1624 { "pm", NL ("->*"), 2 },
1625 { "pp", NL ("++"), 1 },
1626 { "ps", NL ("+"), 1 },
1627 { "pt", NL ("->"), 2 },
1628 { "qu", NL ("?"), 3 },
1629 { "rM", NL ("%="), 2 },
1630 { "rS", NL (">>="), 2 },
1631 { "rc", NL ("reinterpret_cast"), 2 },
1632 { "rm", NL ("%"), 2 },
1633 { "rs", NL (">>"), 2 },
1634 { "sc", NL ("static_cast"), 2 },
1635 { "st", NL ("sizeof "), 1 },
1636 { "sz", NL ("sizeof "), 1 },
1637 { "tr", NL ("throw"), 0 },
1638 { "tw", NL ("throw "), 1 },
1639 { NULL, NULL, 0, 0 }
1642 static struct demangle_component *
1643 d_operator_name (struct d_info *di)
1645 char c1;
1646 char c2;
1648 c1 = d_next_char (di);
1649 c2 = d_next_char (di);
1650 if (c1 == 'v' && IS_DIGIT (c2))
1651 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1652 else if (c1 == 'c' && c2 == 'v')
1653 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1654 cplus_demangle_type (di), NULL);
1655 else
1657 /* LOW is the inclusive lower bound. */
1658 int low = 0;
1659 /* HIGH is the exclusive upper bound. We subtract one to ignore
1660 the sentinel at the end of the array. */
1661 int high = ((sizeof (cplus_demangle_operators)
1662 / sizeof (cplus_demangle_operators[0]))
1663 - 1);
1665 while (1)
1667 int i;
1668 const struct demangle_operator_info *p;
1670 i = low + (high - low) / 2;
1671 p = cplus_demangle_operators + i;
1673 if (c1 == p->code[0] && c2 == p->code[1])
1674 return d_make_operator (di, p);
1676 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1677 high = i;
1678 else
1679 low = i + 1;
1680 if (low == high)
1681 return NULL;
1686 static struct demangle_component *
1687 d_make_character (struct d_info *di, int c)
1689 struct demangle_component *p;
1690 p = d_make_empty (di);
1691 if (p != NULL)
1693 p->type = DEMANGLE_COMPONENT_CHARACTER;
1694 p->u.s_character.character = c;
1696 return p;
1699 static struct demangle_component *
1700 d_java_resource (struct d_info *di)
1702 struct demangle_component *p = NULL;
1703 struct demangle_component *next = NULL;
1704 long len, i;
1705 char c;
1706 const char *str;
1708 len = d_number (di);
1709 if (len <= 1)
1710 return NULL;
1712 /* Eat the leading '_'. */
1713 if (d_next_char (di) != '_')
1714 return NULL;
1715 len--;
1717 str = d_str (di);
1718 i = 0;
1720 while (len > 0)
1722 c = str[i];
1723 if (!c)
1724 return NULL;
1726 /* Each chunk is either a '$' escape... */
1727 if (c == '$')
1729 i++;
1730 switch (str[i++])
1732 case 'S':
1733 c = '/';
1734 break;
1735 case '_':
1736 c = '.';
1737 break;
1738 case '$':
1739 c = '$';
1740 break;
1741 default:
1742 return NULL;
1744 next = d_make_character (di, c);
1745 d_advance (di, i);
1746 str = d_str (di);
1747 len -= i;
1748 i = 0;
1749 if (next == NULL)
1750 return NULL;
1752 /* ... or a sequence of characters. */
1753 else
1755 while (i < len && str[i] && str[i] != '$')
1756 i++;
1758 next = d_make_name (di, str, i);
1759 d_advance (di, i);
1760 str = d_str (di);
1761 len -= i;
1762 i = 0;
1763 if (next == NULL)
1764 return NULL;
1767 if (p == NULL)
1768 p = next;
1769 else
1771 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1772 if (p == NULL)
1773 return NULL;
1777 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1779 return p;
1782 /* <special-name> ::= TV <type>
1783 ::= TT <type>
1784 ::= TI <type>
1785 ::= TS <type>
1786 ::= GV <(object) name>
1787 ::= T <call-offset> <(base) encoding>
1788 ::= Tc <call-offset> <call-offset> <(base) encoding>
1789 Also g++ extensions:
1790 ::= TC <type> <(offset) number> _ <(base) type>
1791 ::= TF <type>
1792 ::= TJ <type>
1793 ::= GR <name>
1794 ::= GA <encoding>
1795 ::= Gr <resource name>
1796 ::= GTt <encoding>
1797 ::= GTn <encoding>
1800 static struct demangle_component *
1801 d_special_name (struct d_info *di)
1803 di->expansion += 20;
1804 if (d_check_char (di, 'T'))
1806 switch (d_next_char (di))
1808 case 'V':
1809 di->expansion -= 5;
1810 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1811 cplus_demangle_type (di), NULL);
1812 case 'T':
1813 di->expansion -= 10;
1814 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1815 cplus_demangle_type (di), NULL);
1816 case 'I':
1817 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1818 cplus_demangle_type (di), NULL);
1819 case 'S':
1820 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1821 cplus_demangle_type (di), NULL);
1823 case 'h':
1824 if (! d_call_offset (di, 'h'))
1825 return NULL;
1826 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1827 d_encoding (di, 0), NULL);
1829 case 'v':
1830 if (! d_call_offset (di, 'v'))
1831 return NULL;
1832 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1833 d_encoding (di, 0), NULL);
1835 case 'c':
1836 if (! d_call_offset (di, '\0'))
1837 return NULL;
1838 if (! d_call_offset (di, '\0'))
1839 return NULL;
1840 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1841 d_encoding (di, 0), NULL);
1843 case 'C':
1845 struct demangle_component *derived_type;
1846 long offset;
1847 struct demangle_component *base_type;
1849 derived_type = cplus_demangle_type (di);
1850 offset = d_number (di);
1851 if (offset < 0)
1852 return NULL;
1853 if (! d_check_char (di, '_'))
1854 return NULL;
1855 base_type = cplus_demangle_type (di);
1856 /* We don't display the offset. FIXME: We should display
1857 it in verbose mode. */
1858 di->expansion += 5;
1859 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1860 base_type, derived_type);
1863 case 'F':
1864 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1865 cplus_demangle_type (di), NULL);
1866 case 'J':
1867 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1868 cplus_demangle_type (di), NULL);
1870 default:
1871 return NULL;
1874 else if (d_check_char (di, 'G'))
1876 switch (d_next_char (di))
1878 case 'V':
1879 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1881 case 'R':
1883 struct demangle_component *name = d_name (di);
1884 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
1885 d_number_component (di));
1888 case 'A':
1889 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1890 d_encoding (di, 0), NULL);
1892 case 'T':
1893 switch (d_next_char (di))
1895 case 'n':
1896 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
1897 d_encoding (di, 0), NULL);
1898 default:
1899 /* ??? The proposal is that other letters (such as 'h') stand
1900 for different variants of transaction cloning, such as
1901 compiling directly for hardware transaction support. But
1902 they still should all be transactional clones of some sort
1903 so go ahead and call them that. */
1904 case 't':
1905 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
1906 d_encoding (di, 0), NULL);
1909 case 'r':
1910 return d_java_resource (di);
1912 default:
1913 return NULL;
1916 else
1917 return NULL;
1920 /* <call-offset> ::= h <nv-offset> _
1921 ::= v <v-offset> _
1923 <nv-offset> ::= <(offset) number>
1925 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1927 The C parameter, if not '\0', is a character we just read which is
1928 the start of the <call-offset>.
1930 We don't display the offset information anywhere. FIXME: We should
1931 display it in verbose mode. */
1933 static int
1934 d_call_offset (struct d_info *di, int c)
1936 if (c == '\0')
1937 c = d_next_char (di);
1939 if (c == 'h')
1940 d_number (di);
1941 else if (c == 'v')
1943 d_number (di);
1944 if (! d_check_char (di, '_'))
1945 return 0;
1946 d_number (di);
1948 else
1949 return 0;
1951 if (! d_check_char (di, '_'))
1952 return 0;
1954 return 1;
1957 /* <ctor-dtor-name> ::= C1
1958 ::= C2
1959 ::= C3
1960 ::= D0
1961 ::= D1
1962 ::= D2
1965 static struct demangle_component *
1966 d_ctor_dtor_name (struct d_info *di)
1968 if (di->last_name != NULL)
1970 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1971 di->expansion += di->last_name->u.s_name.len;
1972 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1973 di->expansion += di->last_name->u.s_string.len;
1975 switch (d_peek_char (di))
1977 case 'C':
1979 enum gnu_v3_ctor_kinds kind;
1981 switch (d_peek_next_char (di))
1983 case '1':
1984 kind = gnu_v3_complete_object_ctor;
1985 break;
1986 case '2':
1987 kind = gnu_v3_base_object_ctor;
1988 break;
1989 case '3':
1990 kind = gnu_v3_complete_object_allocating_ctor;
1991 break;
1992 case '5':
1993 kind = gnu_v3_object_ctor_group;
1994 break;
1995 default:
1996 return NULL;
1998 d_advance (di, 2);
1999 return d_make_ctor (di, kind, di->last_name);
2002 case 'D':
2004 enum gnu_v3_dtor_kinds kind;
2006 switch (d_peek_next_char (di))
2008 case '0':
2009 kind = gnu_v3_deleting_dtor;
2010 break;
2011 case '1':
2012 kind = gnu_v3_complete_object_dtor;
2013 break;
2014 case '2':
2015 kind = gnu_v3_base_object_dtor;
2016 break;
2017 case '5':
2018 kind = gnu_v3_object_dtor_group;
2019 break;
2020 default:
2021 return NULL;
2023 d_advance (di, 2);
2024 return d_make_dtor (di, kind, di->last_name);
2027 default:
2028 return NULL;
2032 /* <type> ::= <builtin-type>
2033 ::= <function-type>
2034 ::= <class-enum-type>
2035 ::= <array-type>
2036 ::= <pointer-to-member-type>
2037 ::= <template-param>
2038 ::= <template-template-param> <template-args>
2039 ::= <substitution>
2040 ::= <CV-qualifiers> <type>
2041 ::= P <type>
2042 ::= R <type>
2043 ::= O <type> (C++0x)
2044 ::= C <type>
2045 ::= G <type>
2046 ::= U <source-name> <type>
2048 <builtin-type> ::= various one letter codes
2049 ::= u <source-name>
2052 CP_STATIC_IF_GLIBCPP_V3
2053 const struct demangle_builtin_type_info
2054 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2056 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2057 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2058 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2059 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2060 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2061 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2062 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2063 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2064 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2065 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2066 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2067 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2068 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2069 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2070 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2071 D_PRINT_DEFAULT },
2072 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2073 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2074 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2075 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2076 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2077 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2078 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2079 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2080 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2081 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2082 D_PRINT_UNSIGNED_LONG_LONG },
2083 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2084 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2085 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2086 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2087 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2088 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2089 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2090 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2091 D_PRINT_DEFAULT },
2094 CP_STATIC_IF_GLIBCPP_V3
2095 struct demangle_component *
2096 cplus_demangle_type (struct d_info *di)
2098 char peek;
2099 struct demangle_component *ret;
2100 int can_subst;
2102 /* The ABI specifies that when CV-qualifiers are used, the base type
2103 is substitutable, and the fully qualified type is substitutable,
2104 but the base type with a strict subset of the CV-qualifiers is
2105 not substitutable. The natural recursive implementation of the
2106 CV-qualifiers would cause subsets to be substitutable, so instead
2107 we pull them all off now.
2109 FIXME: The ABI says that order-insensitive vendor qualifiers
2110 should be handled in the same way, but we have no way to tell
2111 which vendor qualifiers are order-insensitive and which are
2112 order-sensitive. So we just assume that they are all
2113 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2114 __vector, and it treats it as order-sensitive when mangling
2115 names. */
2117 peek = d_peek_char (di);
2118 if (peek == 'r' || peek == 'V' || peek == 'K')
2120 struct demangle_component **pret;
2122 pret = d_cv_qualifiers (di, &ret, 0);
2123 if (pret == NULL)
2124 return NULL;
2125 *pret = cplus_demangle_type (di);
2126 if (! *pret || ! d_add_substitution (di, ret))
2127 return NULL;
2128 return ret;
2131 can_subst = 1;
2133 switch (peek)
2135 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2136 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2137 case 'o': case 's': case 't':
2138 case 'v': case 'w': case 'x': case 'y': case 'z':
2139 ret = d_make_builtin_type (di,
2140 &cplus_demangle_builtin_types[peek - 'a']);
2141 di->expansion += ret->u.s_builtin.type->len;
2142 can_subst = 0;
2143 d_advance (di, 1);
2144 break;
2146 case 'u':
2147 d_advance (di, 1);
2148 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2149 d_source_name (di), NULL);
2150 break;
2152 case 'F':
2153 ret = d_function_type (di);
2154 break;
2156 case '0': case '1': case '2': case '3': case '4':
2157 case '5': case '6': case '7': case '8': case '9':
2158 case 'N':
2159 case 'Z':
2160 ret = d_class_enum_type (di);
2161 break;
2163 case 'A':
2164 ret = d_array_type (di);
2165 break;
2167 case 'M':
2168 ret = d_pointer_to_member_type (di);
2169 break;
2171 case 'T':
2172 ret = d_template_param (di);
2173 if (d_peek_char (di) == 'I')
2175 /* This is <template-template-param> <template-args>. The
2176 <template-template-param> part is a substitution
2177 candidate. */
2178 if (! d_add_substitution (di, ret))
2179 return NULL;
2180 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2181 d_template_args (di));
2183 break;
2185 case 'S':
2186 /* If this is a special substitution, then it is the start of
2187 <class-enum-type>. */
2189 char peek_next;
2191 peek_next = d_peek_next_char (di);
2192 if (IS_DIGIT (peek_next)
2193 || peek_next == '_'
2194 || IS_UPPER (peek_next))
2196 ret = d_substitution (di, 0);
2197 /* The substituted name may have been a template name and
2198 may be followed by tepmlate args. */
2199 if (d_peek_char (di) == 'I')
2200 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2201 d_template_args (di));
2202 else
2203 can_subst = 0;
2205 else
2207 ret = d_class_enum_type (di);
2208 /* If the substitution was a complete type, then it is not
2209 a new substitution candidate. However, if the
2210 substitution was followed by template arguments, then
2211 the whole thing is a substitution candidate. */
2212 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2213 can_subst = 0;
2216 break;
2218 case 'O':
2219 d_advance (di, 1);
2220 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2221 cplus_demangle_type (di), NULL);
2222 break;
2224 case 'P':
2225 d_advance (di, 1);
2226 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2227 cplus_demangle_type (di), NULL);
2228 break;
2230 case 'R':
2231 d_advance (di, 1);
2232 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2233 cplus_demangle_type (di), NULL);
2234 break;
2236 case 'C':
2237 d_advance (di, 1);
2238 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2239 cplus_demangle_type (di), NULL);
2240 break;
2242 case 'G':
2243 d_advance (di, 1);
2244 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2245 cplus_demangle_type (di), NULL);
2246 break;
2248 case 'U':
2249 d_advance (di, 1);
2250 ret = d_source_name (di);
2251 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2252 cplus_demangle_type (di), ret);
2253 break;
2255 case 'D':
2256 can_subst = 0;
2257 d_advance (di, 1);
2258 peek = d_next_char (di);
2259 switch (peek)
2261 case 'T':
2262 case 't':
2263 /* decltype (expression) */
2264 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2265 d_expression (di), NULL);
2266 if (ret && d_next_char (di) != 'E')
2267 ret = NULL;
2268 can_subst = 1;
2269 break;
2271 case 'p':
2272 /* Pack expansion. */
2273 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2274 cplus_demangle_type (di), NULL);
2275 can_subst = 1;
2276 break;
2278 case 'a':
2279 /* auto */
2280 ret = d_make_name (di, "auto", 4);
2281 break;
2283 case 'f':
2284 /* 32-bit decimal floating point */
2285 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2286 di->expansion += ret->u.s_builtin.type->len;
2287 break;
2288 case 'd':
2289 /* 64-bit DFP */
2290 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2291 di->expansion += ret->u.s_builtin.type->len;
2292 break;
2293 case 'e':
2294 /* 128-bit DFP */
2295 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2296 di->expansion += ret->u.s_builtin.type->len;
2297 break;
2298 case 'h':
2299 /* 16-bit half-precision FP */
2300 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2301 di->expansion += ret->u.s_builtin.type->len;
2302 break;
2303 case 's':
2304 /* char16_t */
2305 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2306 di->expansion += ret->u.s_builtin.type->len;
2307 break;
2308 case 'i':
2309 /* char32_t */
2310 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2311 di->expansion += ret->u.s_builtin.type->len;
2312 break;
2314 case 'F':
2315 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2316 ret = d_make_empty (di);
2317 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2318 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2319 /* For demangling we don't care about the bits. */
2320 d_number (di);
2321 ret->u.s_fixed.length = cplus_demangle_type (di);
2322 if (ret->u.s_fixed.length == NULL)
2323 return NULL;
2324 d_number (di);
2325 peek = d_next_char (di);
2326 ret->u.s_fixed.sat = (peek == 's');
2327 break;
2329 case 'v':
2330 ret = d_vector_type (di);
2331 can_subst = 1;
2332 break;
2334 case 'n':
2335 /* decltype(nullptr) */
2336 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2337 di->expansion += ret->u.s_builtin.type->len;
2338 break;
2340 default:
2341 return NULL;
2343 break;
2345 default:
2346 return NULL;
2349 if (can_subst)
2351 if (! d_add_substitution (di, ret))
2352 return NULL;
2355 return ret;
2358 /* <CV-qualifiers> ::= [r] [V] [K] */
2360 static struct demangle_component **
2361 d_cv_qualifiers (struct d_info *di,
2362 struct demangle_component **pret, int member_fn)
2364 struct demangle_component **pstart;
2365 char peek;
2367 pstart = pret;
2368 peek = d_peek_char (di);
2369 while (peek == 'r' || peek == 'V' || peek == 'K')
2371 enum demangle_component_type t;
2373 d_advance (di, 1);
2374 if (peek == 'r')
2376 t = (member_fn
2377 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2378 : DEMANGLE_COMPONENT_RESTRICT);
2379 di->expansion += sizeof "restrict";
2381 else if (peek == 'V')
2383 t = (member_fn
2384 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2385 : DEMANGLE_COMPONENT_VOLATILE);
2386 di->expansion += sizeof "volatile";
2388 else
2390 t = (member_fn
2391 ? DEMANGLE_COMPONENT_CONST_THIS
2392 : DEMANGLE_COMPONENT_CONST);
2393 di->expansion += sizeof "const";
2396 *pret = d_make_comp (di, t, NULL, NULL);
2397 if (*pret == NULL)
2398 return NULL;
2399 pret = &d_left (*pret);
2401 peek = d_peek_char (di);
2404 if (!member_fn && peek == 'F')
2406 while (pstart != pret)
2408 switch ((*pstart)->type)
2410 case DEMANGLE_COMPONENT_RESTRICT:
2411 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2412 break;
2413 case DEMANGLE_COMPONENT_VOLATILE:
2414 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2415 break;
2416 case DEMANGLE_COMPONENT_CONST:
2417 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2418 break;
2419 default:
2420 break;
2422 pstart = &d_left (*pstart);
2426 return pret;
2429 /* <function-type> ::= F [Y] <bare-function-type> E */
2431 static struct demangle_component *
2432 d_function_type (struct d_info *di)
2434 struct demangle_component *ret;
2436 if (! d_check_char (di, 'F'))
2437 return NULL;
2438 if (d_peek_char (di) == 'Y')
2440 /* Function has C linkage. We don't print this information.
2441 FIXME: We should print it in verbose mode. */
2442 d_advance (di, 1);
2444 ret = d_bare_function_type (di, 1);
2445 if (! d_check_char (di, 'E'))
2446 return NULL;
2447 return ret;
2450 /* <type>+ */
2452 static struct demangle_component *
2453 d_parmlist (struct d_info *di)
2455 struct demangle_component *tl;
2456 struct demangle_component **ptl;
2458 tl = NULL;
2459 ptl = &tl;
2460 while (1)
2462 struct demangle_component *type;
2464 char peek = d_peek_char (di);
2465 if (peek == '\0' || peek == 'E' || peek == '.')
2466 break;
2467 type = cplus_demangle_type (di);
2468 if (type == NULL)
2469 return NULL;
2470 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2471 if (*ptl == NULL)
2472 return NULL;
2473 ptl = &d_right (*ptl);
2476 /* There should be at least one parameter type besides the optional
2477 return type. A function which takes no arguments will have a
2478 single parameter type void. */
2479 if (tl == NULL)
2480 return NULL;
2482 /* If we have a single parameter type void, omit it. */
2483 if (d_right (tl) == NULL
2484 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2485 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2487 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2488 d_left (tl) = NULL;
2491 return tl;
2494 /* <bare-function-type> ::= [J]<type>+ */
2496 static struct demangle_component *
2497 d_bare_function_type (struct d_info *di, int has_return_type)
2499 struct demangle_component *return_type;
2500 struct demangle_component *tl;
2501 char peek;
2503 /* Detect special qualifier indicating that the first argument
2504 is the return type. */
2505 peek = d_peek_char (di);
2506 if (peek == 'J')
2508 d_advance (di, 1);
2509 has_return_type = 1;
2512 if (has_return_type)
2514 return_type = cplus_demangle_type (di);
2515 if (return_type == NULL)
2516 return NULL;
2518 else
2519 return_type = NULL;
2521 tl = d_parmlist (di);
2522 if (tl == NULL)
2523 return NULL;
2525 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2526 return_type, tl);
2529 /* <class-enum-type> ::= <name> */
2531 static struct demangle_component *
2532 d_class_enum_type (struct d_info *di)
2534 return d_name (di);
2537 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2538 ::= A [<(dimension) expression>] _ <(element) type>
2541 static struct demangle_component *
2542 d_array_type (struct d_info *di)
2544 char peek;
2545 struct demangle_component *dim;
2547 if (! d_check_char (di, 'A'))
2548 return NULL;
2550 peek = d_peek_char (di);
2551 if (peek == '_')
2552 dim = NULL;
2553 else if (IS_DIGIT (peek))
2555 const char *s;
2557 s = d_str (di);
2560 d_advance (di, 1);
2561 peek = d_peek_char (di);
2563 while (IS_DIGIT (peek));
2564 dim = d_make_name (di, s, d_str (di) - s);
2565 if (dim == NULL)
2566 return NULL;
2568 else
2570 dim = d_expression (di);
2571 if (dim == NULL)
2572 return NULL;
2575 if (! d_check_char (di, '_'))
2576 return NULL;
2578 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2579 cplus_demangle_type (di));
2582 /* <vector-type> ::= Dv <number> _ <type>
2583 ::= Dv _ <expression> _ <type> */
2585 static struct demangle_component *
2586 d_vector_type (struct d_info *di)
2588 char peek;
2589 struct demangle_component *dim;
2591 peek = d_peek_char (di);
2592 if (peek == '_')
2594 d_advance (di, 1);
2595 dim = d_expression (di);
2597 else
2598 dim = d_number_component (di);
2600 if (dim == NULL)
2601 return NULL;
2603 if (! d_check_char (di, '_'))
2604 return NULL;
2606 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2607 cplus_demangle_type (di));
2610 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2612 static struct demangle_component *
2613 d_pointer_to_member_type (struct d_info *di)
2615 struct demangle_component *cl;
2616 struct demangle_component *mem;
2617 struct demangle_component **pmem;
2619 if (! d_check_char (di, 'M'))
2620 return NULL;
2622 cl = cplus_demangle_type (di);
2624 /* The ABI specifies that any type can be a substitution source, and
2625 that M is followed by two types, and that when a CV-qualified
2626 type is seen both the base type and the CV-qualified types are
2627 substitution sources. The ABI also specifies that for a pointer
2628 to a CV-qualified member function, the qualifiers are attached to
2629 the second type. Given the grammar, a plain reading of the ABI
2630 suggests that both the CV-qualified member function and the
2631 non-qualified member function are substitution sources. However,
2632 g++ does not work that way. g++ treats only the CV-qualified
2633 member function as a substitution source. FIXME. So to work
2634 with g++, we need to pull off the CV-qualifiers here, in order to
2635 avoid calling add_substitution() in cplus_demangle_type(). But
2636 for a CV-qualified member which is not a function, g++ does
2637 follow the ABI, so we need to handle that case here by calling
2638 d_add_substitution ourselves. */
2640 pmem = d_cv_qualifiers (di, &mem, 1);
2641 if (pmem == NULL)
2642 return NULL;
2643 *pmem = cplus_demangle_type (di);
2644 if (*pmem == NULL)
2645 return NULL;
2647 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2649 if (! d_add_substitution (di, mem))
2650 return NULL;
2653 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2656 /* <non-negative number> _ */
2658 static long
2659 d_compact_number (struct d_info *di)
2661 long num;
2662 if (d_peek_char (di) == '_')
2663 num = 0;
2664 else if (d_peek_char (di) == 'n')
2665 return -1;
2666 else
2667 num = d_number (di) + 1;
2669 if (! d_check_char (di, '_'))
2670 return -1;
2671 return num;
2674 /* <template-param> ::= T_
2675 ::= T <(parameter-2 non-negative) number> _
2678 static struct demangle_component *
2679 d_template_param (struct d_info *di)
2681 long param;
2683 if (! d_check_char (di, 'T'))
2684 return NULL;
2686 param = d_compact_number (di);
2687 if (param < 0)
2688 return NULL;
2690 ++di->did_subs;
2692 return d_make_template_param (di, param);
2695 /* <template-args> ::= I <template-arg>+ E */
2697 static struct demangle_component *
2698 d_template_args (struct d_info *di)
2700 struct demangle_component *hold_last_name;
2701 struct demangle_component *al;
2702 struct demangle_component **pal;
2704 /* Preserve the last name we saw--don't let the template arguments
2705 clobber it, as that would give us the wrong name for a subsequent
2706 constructor or destructor. */
2707 hold_last_name = di->last_name;
2709 if (d_peek_char (di) != 'I'
2710 && d_peek_char (di) != 'J')
2711 return NULL;
2712 d_advance (di, 1);
2714 if (d_peek_char (di) == 'E')
2716 /* An argument pack can be empty. */
2717 d_advance (di, 1);
2718 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2721 al = NULL;
2722 pal = &al;
2723 while (1)
2725 struct demangle_component *a;
2727 a = d_template_arg (di);
2728 if (a == NULL)
2729 return NULL;
2731 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2732 if (*pal == NULL)
2733 return NULL;
2734 pal = &d_right (*pal);
2736 if (d_peek_char (di) == 'E')
2738 d_advance (di, 1);
2739 break;
2743 di->last_name = hold_last_name;
2745 return al;
2748 /* <template-arg> ::= <type>
2749 ::= X <expression> E
2750 ::= <expr-primary>
2753 static struct demangle_component *
2754 d_template_arg (struct d_info *di)
2756 struct demangle_component *ret;
2758 switch (d_peek_char (di))
2760 case 'X':
2761 d_advance (di, 1);
2762 ret = d_expression (di);
2763 if (! d_check_char (di, 'E'))
2764 return NULL;
2765 return ret;
2767 case 'L':
2768 return d_expr_primary (di);
2770 case 'I':
2771 case 'J':
2772 /* An argument pack. */
2773 return d_template_args (di);
2775 default:
2776 return cplus_demangle_type (di);
2780 /* Parse a sequence of expressions until we hit the terminator
2781 character. */
2783 static struct demangle_component *
2784 d_exprlist (struct d_info *di, char terminator)
2786 struct demangle_component *list = NULL;
2787 struct demangle_component **p = &list;
2789 if (d_peek_char (di) == terminator)
2791 d_advance (di, 1);
2792 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2795 while (1)
2797 struct demangle_component *arg = d_expression (di);
2798 if (arg == NULL)
2799 return NULL;
2801 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2802 if (*p == NULL)
2803 return NULL;
2804 p = &d_right (*p);
2806 if (d_peek_char (di) == terminator)
2808 d_advance (di, 1);
2809 break;
2813 return list;
2816 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2817 dynamic_cast, static_cast or reinterpret_cast. */
2819 static int
2820 op_is_new_cast (struct demangle_component *op)
2822 const char *code = op->u.s_operator.op->code;
2823 return (code[1] == 'c'
2824 && (code[0] == 's' || code[0] == 'd'
2825 || code[0] == 'c' || code[0] == 'r'));
2828 /* <expression> ::= <(unary) operator-name> <expression>
2829 ::= <(binary) operator-name> <expression> <expression>
2830 ::= <(trinary) operator-name> <expression> <expression> <expression>
2831 ::= cl <expression>+ E
2832 ::= st <type>
2833 ::= <template-param>
2834 ::= sr <type> <unqualified-name>
2835 ::= sr <type> <unqualified-name> <template-args>
2836 ::= <expr-primary>
2839 static struct demangle_component *
2840 d_expression (struct d_info *di)
2842 char peek;
2844 peek = d_peek_char (di);
2845 if (peek == 'L')
2846 return d_expr_primary (di);
2847 else if (peek == 'T')
2848 return d_template_param (di);
2849 else if (peek == 's' && d_peek_next_char (di) == 'r')
2851 struct demangle_component *type;
2852 struct demangle_component *name;
2854 d_advance (di, 2);
2855 type = cplus_demangle_type (di);
2856 name = d_unqualified_name (di);
2857 if (d_peek_char (di) != 'I')
2858 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2859 else
2860 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2861 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2862 d_template_args (di)));
2864 else if (peek == 's' && d_peek_next_char (di) == 'p')
2866 d_advance (di, 2);
2867 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2868 d_expression (di), NULL);
2870 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2872 /* Function parameter used in a late-specified return type. */
2873 int index;
2874 d_advance (di, 2);
2875 if (d_peek_char (di) == 'T')
2877 /* 'this' parameter. */
2878 d_advance (di, 1);
2879 index = 0;
2881 else
2883 index = d_compact_number (di) + 1;
2884 if (index == 0)
2885 return NULL;
2887 return d_make_function_param (di, index);
2889 else if (IS_DIGIT (peek)
2890 || (peek == 'o' && d_peek_next_char (di) == 'n'))
2892 /* We can get an unqualified name as an expression in the case of
2893 a dependent function call, i.e. decltype(f(t)). */
2894 struct demangle_component *name;
2896 if (peek == 'o')
2897 /* operator-function-id, i.e. operator+(t). */
2898 d_advance (di, 2);
2900 name = d_unqualified_name (di);
2901 if (name == NULL)
2902 return NULL;
2903 if (d_peek_char (di) == 'I')
2904 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2905 d_template_args (di));
2906 else
2907 return name;
2909 else if ((peek == 'i' || peek == 't')
2910 && d_peek_next_char (di) == 'l')
2912 /* Brace-enclosed initializer list, untyped or typed. */
2913 struct demangle_component *type = NULL;
2914 if (peek == 't')
2915 type = cplus_demangle_type (di);
2916 d_advance (di, 2);
2917 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
2918 type, d_exprlist (di, 'E'));
2920 else
2922 struct demangle_component *op;
2923 const char *code = NULL;
2924 int args;
2926 op = d_operator_name (di);
2927 if (op == NULL)
2928 return NULL;
2930 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2932 code = op->u.s_operator.op->code;
2933 di->expansion += op->u.s_operator.op->len - 2;
2934 if (strcmp (code, "st") == 0)
2935 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2936 cplus_demangle_type (di));
2939 switch (op->type)
2941 default:
2942 return NULL;
2943 case DEMANGLE_COMPONENT_OPERATOR:
2944 args = op->u.s_operator.op->args;
2945 break;
2946 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2947 args = op->u.s_extended_operator.args;
2948 break;
2949 case DEMANGLE_COMPONENT_CAST:
2950 args = 1;
2951 break;
2954 switch (args)
2956 case 0:
2957 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
2959 case 1:
2961 struct demangle_component *operand;
2962 int suffix = 0;
2964 if (code && (code[0] == 'p' || code[0] == 'm')
2965 && code[1] == code[0])
2966 /* pp_ and mm_ are the prefix variants. */
2967 suffix = !d_check_char (di, '_');
2969 if (op->type == DEMANGLE_COMPONENT_CAST
2970 && d_check_char (di, '_'))
2971 operand = d_exprlist (di, 'E');
2972 else
2973 operand = d_expression (di);
2975 if (suffix)
2976 /* Indicate the suffix variant for d_print_comp. */
2977 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2978 d_make_comp (di,
2979 DEMANGLE_COMPONENT_BINARY_ARGS,
2980 operand, operand));
2981 else
2982 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2983 operand);
2985 case 2:
2987 struct demangle_component *left;
2988 struct demangle_component *right;
2990 if (op_is_new_cast (op))
2991 left = cplus_demangle_type (di);
2992 else
2993 left = d_expression (di);
2994 if (!strcmp (code, "cl"))
2995 right = d_exprlist (di, 'E');
2996 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
2998 right = d_unqualified_name (di);
2999 if (d_peek_char (di) == 'I')
3000 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3001 right, d_template_args (di));
3003 else
3004 right = d_expression (di);
3006 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3007 d_make_comp (di,
3008 DEMANGLE_COMPONENT_BINARY_ARGS,
3009 left, right));
3011 case 3:
3013 struct demangle_component *first;
3014 struct demangle_component *second;
3015 struct demangle_component *third;
3017 if (!strcmp (code, "qu"))
3019 /* ?: expression. */
3020 first = d_expression (di);
3021 second = d_expression (di);
3022 third = d_expression (di);
3024 else if (code[0] == 'n')
3026 /* new-expression. */
3027 if (code[1] != 'w' && code[1] != 'a')
3028 return NULL;
3029 first = d_exprlist (di, '_');
3030 second = cplus_demangle_type (di);
3031 if (d_peek_char (di) == 'E')
3033 d_advance (di, 1);
3034 third = NULL;
3036 else if (d_peek_char (di) == 'p'
3037 && d_peek_next_char (di) == 'i')
3039 /* Parenthesized initializer. */
3040 d_advance (di, 2);
3041 third = d_exprlist (di, 'E');
3043 else if (d_peek_char (di) == 'i'
3044 && d_peek_next_char (di) == 'l')
3045 /* initializer-list. */
3046 third = d_expression (di);
3047 else
3048 return NULL;
3050 else
3051 return NULL;
3052 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3053 d_make_comp (di,
3054 DEMANGLE_COMPONENT_TRINARY_ARG1,
3055 first,
3056 d_make_comp (di,
3057 DEMANGLE_COMPONENT_TRINARY_ARG2,
3058 second, third)));
3060 default:
3061 return NULL;
3066 /* <expr-primary> ::= L <type> <(value) number> E
3067 ::= L <type> <(value) float> E
3068 ::= L <mangled-name> E
3071 static struct demangle_component *
3072 d_expr_primary (struct d_info *di)
3074 struct demangle_component *ret;
3076 if (! d_check_char (di, 'L'))
3077 return NULL;
3078 if (d_peek_char (di) == '_'
3079 /* Workaround for G++ bug; see comment in write_template_arg. */
3080 || d_peek_char (di) == 'Z')
3081 ret = cplus_demangle_mangled_name (di, 0);
3082 else
3084 struct demangle_component *type;
3085 enum demangle_component_type t;
3086 const char *s;
3088 type = cplus_demangle_type (di);
3089 if (type == NULL)
3090 return NULL;
3092 /* If we have a type we know how to print, we aren't going to
3093 print the type name itself. */
3094 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3095 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3096 di->expansion -= type->u.s_builtin.type->len;
3098 /* Rather than try to interpret the literal value, we just
3099 collect it as a string. Note that it's possible to have a
3100 floating point literal here. The ABI specifies that the
3101 format of such literals is machine independent. That's fine,
3102 but what's not fine is that versions of g++ up to 3.2 with
3103 -fabi-version=1 used upper case letters in the hex constant,
3104 and dumped out gcc's internal representation. That makes it
3105 hard to tell where the constant ends, and hard to dump the
3106 constant in any readable form anyhow. We don't attempt to
3107 handle these cases. */
3109 t = DEMANGLE_COMPONENT_LITERAL;
3110 if (d_peek_char (di) == 'n')
3112 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3113 d_advance (di, 1);
3115 s = d_str (di);
3116 while (d_peek_char (di) != 'E')
3118 if (d_peek_char (di) == '\0')
3119 return NULL;
3120 d_advance (di, 1);
3122 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3124 if (! d_check_char (di, 'E'))
3125 return NULL;
3126 return ret;
3129 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3130 ::= Z <(function) encoding> E s [<discriminator>]
3133 static struct demangle_component *
3134 d_local_name (struct d_info *di)
3136 struct demangle_component *function;
3138 if (! d_check_char (di, 'Z'))
3139 return NULL;
3141 function = d_encoding (di, 0);
3143 if (! d_check_char (di, 'E'))
3144 return NULL;
3146 if (d_peek_char (di) == 's')
3148 d_advance (di, 1);
3149 if (! d_discriminator (di))
3150 return NULL;
3151 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3152 d_make_name (di, "string literal",
3153 sizeof "string literal" - 1));
3155 else
3157 struct demangle_component *name;
3158 int num = -1;
3160 if (d_peek_char (di) == 'd')
3162 /* Default argument scope: d <number> _. */
3163 d_advance (di, 1);
3164 num = d_compact_number (di);
3165 if (num < 0)
3166 return NULL;
3169 name = d_name (di);
3170 if (name)
3171 switch (name->type)
3173 /* Lambdas and unnamed types have internal discriminators. */
3174 case DEMANGLE_COMPONENT_LAMBDA:
3175 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3176 break;
3177 default:
3178 if (! d_discriminator (di))
3179 return NULL;
3181 if (num >= 0)
3182 name = d_make_default_arg (di, num, name);
3183 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3187 /* <discriminator> ::= _ <(non-negative) number>
3189 We demangle the discriminator, but we don't print it out. FIXME:
3190 We should print it out in verbose mode. */
3192 static int
3193 d_discriminator (struct d_info *di)
3195 long discrim;
3197 if (d_peek_char (di) != '_')
3198 return 1;
3199 d_advance (di, 1);
3200 discrim = d_number (di);
3201 if (discrim < 0)
3202 return 0;
3203 return 1;
3206 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3208 static struct demangle_component *
3209 d_lambda (struct d_info *di)
3211 struct demangle_component *tl;
3212 struct demangle_component *ret;
3213 int num;
3215 if (! d_check_char (di, 'U'))
3216 return NULL;
3217 if (! d_check_char (di, 'l'))
3218 return NULL;
3220 tl = d_parmlist (di);
3221 if (tl == NULL)
3222 return NULL;
3224 if (! d_check_char (di, 'E'))
3225 return NULL;
3227 num = d_compact_number (di);
3228 if (num < 0)
3229 return NULL;
3231 ret = d_make_empty (di);
3232 if (ret)
3234 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3235 ret->u.s_unary_num.sub = tl;
3236 ret->u.s_unary_num.num = num;
3239 if (! d_add_substitution (di, ret))
3240 return NULL;
3242 return ret;
3245 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3247 static struct demangle_component *
3248 d_unnamed_type (struct d_info *di)
3250 struct demangle_component *ret;
3251 long num;
3253 if (! d_check_char (di, 'U'))
3254 return NULL;
3255 if (! d_check_char (di, 't'))
3256 return NULL;
3258 num = d_compact_number (di);
3259 if (num < 0)
3260 return NULL;
3262 ret = d_make_empty (di);
3263 if (ret)
3265 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3266 ret->u.s_number.number = num;
3269 if (! d_add_substitution (di, ret))
3270 return NULL;
3272 return ret;
3275 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3278 static struct demangle_component *
3279 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3281 const char *suffix = d_str (di);
3282 const char *pend = suffix;
3283 struct demangle_component *n;
3285 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3287 pend += 2;
3288 while (IS_LOWER (*pend) || *pend == '_')
3289 ++pend;
3291 while (*pend == '.' && IS_DIGIT (pend[1]))
3293 pend += 2;
3294 while (IS_DIGIT (*pend))
3295 ++pend;
3297 d_advance (di, pend - suffix);
3298 n = d_make_name (di, suffix, pend - suffix);
3299 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3302 /* Add a new substitution. */
3304 static int
3305 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3307 if (dc == NULL)
3308 return 0;
3309 if (di->next_sub >= di->num_subs)
3310 return 0;
3311 di->subs[di->next_sub] = dc;
3312 ++di->next_sub;
3313 return 1;
3316 /* <substitution> ::= S <seq-id> _
3317 ::= S_
3318 ::= St
3319 ::= Sa
3320 ::= Sb
3321 ::= Ss
3322 ::= Si
3323 ::= So
3324 ::= Sd
3326 If PREFIX is non-zero, then this type is being used as a prefix in
3327 a qualified name. In this case, for the standard substitutions, we
3328 need to check whether we are being used as a prefix for a
3329 constructor or destructor, and return a full template name.
3330 Otherwise we will get something like std::iostream::~iostream()
3331 which does not correspond particularly well to any function which
3332 actually appears in the source.
3335 static const struct d_standard_sub_info standard_subs[] =
3337 { 't', NL ("std"),
3338 NL ("std"),
3339 NULL, 0 },
3340 { 'a', NL ("std::allocator"),
3341 NL ("std::allocator"),
3342 NL ("allocator") },
3343 { 'b', NL ("std::basic_string"),
3344 NL ("std::basic_string"),
3345 NL ("basic_string") },
3346 { 's', NL ("std::string"),
3347 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3348 NL ("basic_string") },
3349 { 'i', NL ("std::istream"),
3350 NL ("std::basic_istream<char, std::char_traits<char> >"),
3351 NL ("basic_istream") },
3352 { 'o', NL ("std::ostream"),
3353 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3354 NL ("basic_ostream") },
3355 { 'd', NL ("std::iostream"),
3356 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3357 NL ("basic_iostream") }
3360 static struct demangle_component *
3361 d_substitution (struct d_info *di, int prefix)
3363 char c;
3365 if (! d_check_char (di, 'S'))
3366 return NULL;
3368 c = d_next_char (di);
3369 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3371 unsigned int id;
3373 id = 0;
3374 if (c != '_')
3378 unsigned int new_id;
3380 if (IS_DIGIT (c))
3381 new_id = id * 36 + c - '0';
3382 else if (IS_UPPER (c))
3383 new_id = id * 36 + c - 'A' + 10;
3384 else
3385 return NULL;
3386 if (new_id < id)
3387 return NULL;
3388 id = new_id;
3389 c = d_next_char (di);
3391 while (c != '_');
3393 ++id;
3396 if (id >= (unsigned int) di->next_sub)
3397 return NULL;
3399 ++di->did_subs;
3401 return di->subs[id];
3403 else
3405 int verbose;
3406 const struct d_standard_sub_info *p;
3407 const struct d_standard_sub_info *pend;
3409 verbose = (di->options & DMGL_VERBOSE) != 0;
3410 if (! verbose && prefix)
3412 char peek;
3414 peek = d_peek_char (di);
3415 if (peek == 'C' || peek == 'D')
3416 verbose = 1;
3419 pend = (&standard_subs[0]
3420 + sizeof standard_subs / sizeof standard_subs[0]);
3421 for (p = &standard_subs[0]; p < pend; ++p)
3423 if (c == p->code)
3425 const char *s;
3426 int len;
3428 if (p->set_last_name != NULL)
3429 di->last_name = d_make_sub (di, p->set_last_name,
3430 p->set_last_name_len);
3431 if (verbose)
3433 s = p->full_expansion;
3434 len = p->full_len;
3436 else
3438 s = p->simple_expansion;
3439 len = p->simple_len;
3441 di->expansion += len;
3442 return d_make_sub (di, s, len);
3446 return NULL;
3450 /* Initialize a growable string. */
3452 static void
3453 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3455 dgs->buf = NULL;
3456 dgs->len = 0;
3457 dgs->alc = 0;
3458 dgs->allocation_failure = 0;
3460 if (estimate > 0)
3461 d_growable_string_resize (dgs, estimate);
3464 /* Grow a growable string to a given size. */
3466 static inline void
3467 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3469 size_t newalc;
3470 char *newbuf;
3472 if (dgs->allocation_failure)
3473 return;
3475 /* Start allocation at two bytes to avoid any possibility of confusion
3476 with the special value of 1 used as a return in *palc to indicate
3477 allocation failures. */
3478 newalc = dgs->alc > 0 ? dgs->alc : 2;
3479 while (newalc < need)
3480 newalc <<= 1;
3482 newbuf = (char *) realloc (dgs->buf, newalc);
3483 if (newbuf == NULL)
3485 free (dgs->buf);
3486 dgs->buf = NULL;
3487 dgs->len = 0;
3488 dgs->alc = 0;
3489 dgs->allocation_failure = 1;
3490 return;
3492 dgs->buf = newbuf;
3493 dgs->alc = newalc;
3496 /* Append a buffer to a growable string. */
3498 static inline void
3499 d_growable_string_append_buffer (struct d_growable_string *dgs,
3500 const char *s, size_t l)
3502 size_t need;
3504 need = dgs->len + l + 1;
3505 if (need > dgs->alc)
3506 d_growable_string_resize (dgs, need);
3508 if (dgs->allocation_failure)
3509 return;
3511 memcpy (dgs->buf + dgs->len, s, l);
3512 dgs->buf[dgs->len + l] = '\0';
3513 dgs->len += l;
3516 /* Bridge growable strings to the callback mechanism. */
3518 static void
3519 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3521 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3523 d_growable_string_append_buffer (dgs, s, l);
3526 /* Initialize a print information structure. */
3528 static void
3529 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3530 void *opaque)
3532 dpi->len = 0;
3533 dpi->last_char = '\0';
3534 dpi->templates = NULL;
3535 dpi->modifiers = NULL;
3536 dpi->pack_index = 0;
3537 dpi->flush_count = 0;
3539 dpi->callback = callback;
3540 dpi->opaque = opaque;
3542 dpi->demangle_failure = 0;
3545 /* Indicate that an error occurred during printing, and test for error. */
3547 static inline void
3548 d_print_error (struct d_print_info *dpi)
3550 dpi->demangle_failure = 1;
3553 static inline int
3554 d_print_saw_error (struct d_print_info *dpi)
3556 return dpi->demangle_failure != 0;
3559 /* Flush buffered characters to the callback. */
3561 static inline void
3562 d_print_flush (struct d_print_info *dpi)
3564 dpi->buf[dpi->len] = '\0';
3565 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3566 dpi->len = 0;
3567 dpi->flush_count++;
3570 /* Append characters and buffers for printing. */
3572 static inline void
3573 d_append_char (struct d_print_info *dpi, char c)
3575 if (dpi->len == sizeof (dpi->buf) - 1)
3576 d_print_flush (dpi);
3578 dpi->buf[dpi->len++] = c;
3579 dpi->last_char = c;
3582 static inline void
3583 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3585 size_t i;
3587 for (i = 0; i < l; i++)
3588 d_append_char (dpi, s[i]);
3591 static inline void
3592 d_append_string (struct d_print_info *dpi, const char *s)
3594 d_append_buffer (dpi, s, strlen (s));
3597 static inline void
3598 d_append_num (struct d_print_info *dpi, long l)
3600 char buf[25];
3601 sprintf (buf,"%ld", l);
3602 d_append_string (dpi, buf);
3605 static inline char
3606 d_last_char (struct d_print_info *dpi)
3608 return dpi->last_char;
3611 /* Turn components into a human readable string. OPTIONS is the
3612 options bits passed to the demangler. DC is the tree to print.
3613 CALLBACK is a function to call to flush demangled string segments
3614 as they fill the intermediate buffer, and OPAQUE is a generalized
3615 callback argument. On success, this returns 1. On failure,
3616 it returns 0, indicating a bad parse. It does not use heap
3617 memory to build an output string, so cannot encounter memory
3618 allocation failure. */
3620 CP_STATIC_IF_GLIBCPP_V3
3622 cplus_demangle_print_callback (int options,
3623 const struct demangle_component *dc,
3624 demangle_callbackref callback, void *opaque)
3626 struct d_print_info dpi;
3628 d_print_init (&dpi, callback, opaque);
3630 d_print_comp (&dpi, options, dc);
3632 d_print_flush (&dpi);
3634 return ! d_print_saw_error (&dpi);
3637 /* Turn components into a human readable string. OPTIONS is the
3638 options bits passed to the demangler. DC is the tree to print.
3639 ESTIMATE is a guess at the length of the result. This returns a
3640 string allocated by malloc, or NULL on error. On success, this
3641 sets *PALC to the size of the allocated buffer. On failure, this
3642 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3643 failure. */
3645 CP_STATIC_IF_GLIBCPP_V3
3646 char *
3647 cplus_demangle_print (int options, const struct demangle_component *dc,
3648 int estimate, size_t *palc)
3650 struct d_growable_string dgs;
3652 d_growable_string_init (&dgs, estimate);
3654 if (! cplus_demangle_print_callback (options, dc,
3655 d_growable_string_callback_adapter,
3656 &dgs))
3658 free (dgs.buf);
3659 *palc = 0;
3660 return NULL;
3663 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3664 return dgs.buf;
3667 /* Returns the I'th element of the template arglist ARGS, or NULL on
3668 failure. */
3670 static struct demangle_component *
3671 d_index_template_argument (struct demangle_component *args, int i)
3673 struct demangle_component *a;
3675 for (a = args;
3676 a != NULL;
3677 a = d_right (a))
3679 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3680 return NULL;
3681 if (i <= 0)
3682 break;
3683 --i;
3685 if (i != 0 || a == NULL)
3686 return NULL;
3688 return d_left (a);
3691 /* Returns the template argument from the current context indicated by DC,
3692 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3694 static struct demangle_component *
3695 d_lookup_template_argument (struct d_print_info *dpi,
3696 const struct demangle_component *dc)
3698 if (dpi->templates == NULL)
3700 d_print_error (dpi);
3701 return NULL;
3704 return d_index_template_argument
3705 (d_right (dpi->templates->template_decl),
3706 dc->u.s_number.number);
3709 /* Returns a template argument pack used in DC (any will do), or NULL. */
3711 static struct demangle_component *
3712 d_find_pack (struct d_print_info *dpi,
3713 const struct demangle_component *dc)
3715 struct demangle_component *a;
3716 if (dc == NULL)
3717 return NULL;
3719 switch (dc->type)
3721 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3722 a = d_lookup_template_argument (dpi, dc);
3723 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3724 return a;
3725 return NULL;
3727 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3728 return NULL;
3730 case DEMANGLE_COMPONENT_LAMBDA:
3731 case DEMANGLE_COMPONENT_NAME:
3732 case DEMANGLE_COMPONENT_OPERATOR:
3733 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3734 case DEMANGLE_COMPONENT_SUB_STD:
3735 case DEMANGLE_COMPONENT_CHARACTER:
3736 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3737 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3738 return NULL;
3740 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3741 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3742 case DEMANGLE_COMPONENT_CTOR:
3743 return d_find_pack (dpi, dc->u.s_ctor.name);
3744 case DEMANGLE_COMPONENT_DTOR:
3745 return d_find_pack (dpi, dc->u.s_dtor.name);
3747 default:
3748 a = d_find_pack (dpi, d_left (dc));
3749 if (a)
3750 return a;
3751 return d_find_pack (dpi, d_right (dc));
3755 /* Returns the length of the template argument pack DC. */
3757 static int
3758 d_pack_length (const struct demangle_component *dc)
3760 int count = 0;
3761 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3762 && d_left (dc) != NULL)
3764 ++count;
3765 dc = d_right (dc);
3767 return count;
3770 /* DC is a component of a mangled expression. Print it, wrapped in parens
3771 if needed. */
3773 static void
3774 d_print_subexpr (struct d_print_info *dpi, int options,
3775 const struct demangle_component *dc)
3777 int simple = 0;
3778 if (dc->type == DEMANGLE_COMPONENT_NAME
3779 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
3780 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
3781 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3782 simple = 1;
3783 if (!simple)
3784 d_append_char (dpi, '(');
3785 d_print_comp (dpi, options, dc);
3786 if (!simple)
3787 d_append_char (dpi, ')');
3790 /* Subroutine to handle components. */
3792 static void
3793 d_print_comp (struct d_print_info *dpi, int options,
3794 const struct demangle_component *dc)
3796 /* Magic variable to let reference smashing skip over the next modifier
3797 without needing to modify *dc. */
3798 const struct demangle_component *mod_inner = NULL;
3800 if (dc == NULL)
3802 d_print_error (dpi);
3803 return;
3805 if (d_print_saw_error (dpi))
3806 return;
3808 switch (dc->type)
3810 case DEMANGLE_COMPONENT_NAME:
3811 if ((options & DMGL_JAVA) == 0)
3812 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3813 else
3814 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3815 return;
3817 case DEMANGLE_COMPONENT_QUAL_NAME:
3818 case DEMANGLE_COMPONENT_LOCAL_NAME:
3819 d_print_comp (dpi, options, d_left (dc));
3820 if ((options & DMGL_JAVA) == 0)
3821 d_append_string (dpi, "::");
3822 else
3823 d_append_char (dpi, '.');
3824 d_print_comp (dpi, options, d_right (dc));
3825 return;
3827 case DEMANGLE_COMPONENT_TYPED_NAME:
3829 struct d_print_mod *hold_modifiers;
3830 struct demangle_component *typed_name;
3831 struct d_print_mod adpm[4];
3832 unsigned int i;
3833 struct d_print_template dpt;
3835 /* Pass the name down to the type so that it can be printed in
3836 the right place for the type. We also have to pass down
3837 any CV-qualifiers, which apply to the this parameter. */
3838 hold_modifiers = dpi->modifiers;
3839 dpi->modifiers = 0;
3840 i = 0;
3841 typed_name = d_left (dc);
3842 while (typed_name != NULL)
3844 if (i >= sizeof adpm / sizeof adpm[0])
3846 d_print_error (dpi);
3847 return;
3850 adpm[i].next = dpi->modifiers;
3851 dpi->modifiers = &adpm[i];
3852 adpm[i].mod = typed_name;
3853 adpm[i].printed = 0;
3854 adpm[i].templates = dpi->templates;
3855 ++i;
3857 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3858 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3859 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3860 break;
3862 typed_name = d_left (typed_name);
3865 if (typed_name == NULL)
3867 d_print_error (dpi);
3868 return;
3871 /* If typed_name is a template, then it applies to the
3872 function type as well. */
3873 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3875 dpt.next = dpi->templates;
3876 dpi->templates = &dpt;
3877 dpt.template_decl = typed_name;
3880 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3881 there may be CV-qualifiers on its right argument which
3882 really apply here; this happens when parsing a class which
3883 is local to a function. */
3884 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3886 struct demangle_component *local_name;
3888 local_name = d_right (typed_name);
3889 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
3890 local_name = local_name->u.s_unary_num.sub;
3891 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3892 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3893 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3895 if (i >= sizeof adpm / sizeof adpm[0])
3897 d_print_error (dpi);
3898 return;
3901 adpm[i] = adpm[i - 1];
3902 adpm[i].next = &adpm[i - 1];
3903 dpi->modifiers = &adpm[i];
3905 adpm[i - 1].mod = local_name;
3906 adpm[i - 1].printed = 0;
3907 adpm[i - 1].templates = dpi->templates;
3908 ++i;
3910 local_name = d_left (local_name);
3914 d_print_comp (dpi, options, d_right (dc));
3916 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3917 dpi->templates = dpt.next;
3919 /* If the modifiers didn't get printed by the type, print them
3920 now. */
3921 while (i > 0)
3923 --i;
3924 if (! adpm[i].printed)
3926 d_append_char (dpi, ' ');
3927 d_print_mod (dpi, options, adpm[i].mod);
3931 dpi->modifiers = hold_modifiers;
3933 return;
3936 case DEMANGLE_COMPONENT_TEMPLATE:
3938 struct d_print_mod *hold_dpm;
3939 struct demangle_component *dcl;
3941 /* Don't push modifiers into a template definition. Doing so
3942 could give the wrong definition for a template argument.
3943 Instead, treat the template essentially as a name. */
3945 hold_dpm = dpi->modifiers;
3946 dpi->modifiers = NULL;
3948 dcl = d_left (dc);
3950 if ((options & DMGL_JAVA) != 0
3951 && dcl->type == DEMANGLE_COMPONENT_NAME
3952 && dcl->u.s_name.len == 6
3953 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3955 /* Special-case Java arrays, so that JArray<TYPE> appears
3956 instead as TYPE[]. */
3958 d_print_comp (dpi, options, d_right (dc));
3959 d_append_string (dpi, "[]");
3961 else
3963 d_print_comp (dpi, options, dcl);
3964 if (d_last_char (dpi) == '<')
3965 d_append_char (dpi, ' ');
3966 d_append_char (dpi, '<');
3967 d_print_comp (dpi, options, d_right (dc));
3968 /* Avoid generating two consecutive '>' characters, to avoid
3969 the C++ syntactic ambiguity. */
3970 if (d_last_char (dpi) == '>')
3971 d_append_char (dpi, ' ');
3972 d_append_char (dpi, '>');
3975 dpi->modifiers = hold_dpm;
3977 return;
3980 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3982 struct d_print_template *hold_dpt;
3983 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3985 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3986 a = d_index_template_argument (a, dpi->pack_index);
3988 if (a == NULL)
3990 d_print_error (dpi);
3991 return;
3994 /* While processing this parameter, we need to pop the list of
3995 templates. This is because the template parameter may
3996 itself be a reference to a parameter of an outer
3997 template. */
3999 hold_dpt = dpi->templates;
4000 dpi->templates = hold_dpt->next;
4002 d_print_comp (dpi, options, a);
4004 dpi->templates = hold_dpt;
4006 return;
4009 case DEMANGLE_COMPONENT_CTOR:
4010 d_print_comp (dpi, options, dc->u.s_ctor.name);
4011 return;
4013 case DEMANGLE_COMPONENT_DTOR:
4014 d_append_char (dpi, '~');
4015 d_print_comp (dpi, options, dc->u.s_dtor.name);
4016 return;
4018 case DEMANGLE_COMPONENT_VTABLE:
4019 d_append_string (dpi, "vtable for ");
4020 d_print_comp (dpi, options, d_left (dc));
4021 return;
4023 case DEMANGLE_COMPONENT_VTT:
4024 d_append_string (dpi, "VTT for ");
4025 d_print_comp (dpi, options, d_left (dc));
4026 return;
4028 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4029 d_append_string (dpi, "construction vtable for ");
4030 d_print_comp (dpi, options, d_left (dc));
4031 d_append_string (dpi, "-in-");
4032 d_print_comp (dpi, options, d_right (dc));
4033 return;
4035 case DEMANGLE_COMPONENT_TYPEINFO:
4036 d_append_string (dpi, "typeinfo for ");
4037 d_print_comp (dpi, options, d_left (dc));
4038 return;
4040 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4041 d_append_string (dpi, "typeinfo name for ");
4042 d_print_comp (dpi, options, d_left (dc));
4043 return;
4045 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4046 d_append_string (dpi, "typeinfo fn for ");
4047 d_print_comp (dpi, options, d_left (dc));
4048 return;
4050 case DEMANGLE_COMPONENT_THUNK:
4051 d_append_string (dpi, "non-virtual thunk to ");
4052 d_print_comp (dpi, options, d_left (dc));
4053 return;
4055 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4056 d_append_string (dpi, "virtual thunk to ");
4057 d_print_comp (dpi, options, d_left (dc));
4058 return;
4060 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4061 d_append_string (dpi, "covariant return thunk to ");
4062 d_print_comp (dpi, options, d_left (dc));
4063 return;
4065 case DEMANGLE_COMPONENT_JAVA_CLASS:
4066 d_append_string (dpi, "java Class for ");
4067 d_print_comp (dpi, options, d_left (dc));
4068 return;
4070 case DEMANGLE_COMPONENT_GUARD:
4071 d_append_string (dpi, "guard variable for ");
4072 d_print_comp (dpi, options, d_left (dc));
4073 return;
4075 case DEMANGLE_COMPONENT_REFTEMP:
4076 d_append_string (dpi, "reference temporary #");
4077 d_print_comp (dpi, options, d_right (dc));
4078 d_append_string (dpi, " for ");
4079 d_print_comp (dpi, options, d_left (dc));
4080 return;
4082 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4083 d_append_string (dpi, "hidden alias for ");
4084 d_print_comp (dpi, options, d_left (dc));
4085 return;
4087 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4088 d_append_string (dpi, "transaction clone for ");
4089 d_print_comp (dpi, options, d_left (dc));
4090 return;
4092 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4093 d_append_string (dpi, "non-transaction clone for ");
4094 d_print_comp (dpi, options, d_left (dc));
4095 return;
4097 case DEMANGLE_COMPONENT_SUB_STD:
4098 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4099 return;
4101 case DEMANGLE_COMPONENT_RESTRICT:
4102 case DEMANGLE_COMPONENT_VOLATILE:
4103 case DEMANGLE_COMPONENT_CONST:
4105 struct d_print_mod *pdpm;
4107 /* When printing arrays, it's possible to have cases where the
4108 same CV-qualifier gets pushed on the stack multiple times.
4109 We only need to print it once. */
4111 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4113 if (! pdpm->printed)
4115 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4116 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4117 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4118 break;
4119 if (pdpm->mod->type == dc->type)
4121 d_print_comp (dpi, options, d_left (dc));
4122 return;
4127 goto modifier;
4129 case DEMANGLE_COMPONENT_REFERENCE:
4130 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4132 /* Handle reference smashing: & + && = &. */
4133 const struct demangle_component *sub = d_left (dc);
4134 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4136 struct demangle_component *a = d_lookup_template_argument (dpi, sub);
4137 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4138 a = d_index_template_argument (a, dpi->pack_index);
4140 if (a == NULL)
4142 d_print_error (dpi);
4143 return;
4146 sub = a;
4149 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4150 || sub->type == dc->type)
4151 dc = sub;
4152 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4153 mod_inner = d_left (sub);
4155 /* Fall through. */
4157 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4158 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4159 case DEMANGLE_COMPONENT_CONST_THIS:
4160 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4161 case DEMANGLE_COMPONENT_POINTER:
4162 case DEMANGLE_COMPONENT_COMPLEX:
4163 case DEMANGLE_COMPONENT_IMAGINARY:
4164 modifier:
4166 /* We keep a list of modifiers on the stack. */
4167 struct d_print_mod dpm;
4169 dpm.next = dpi->modifiers;
4170 dpi->modifiers = &dpm;
4171 dpm.mod = dc;
4172 dpm.printed = 0;
4173 dpm.templates = dpi->templates;
4175 if (!mod_inner)
4176 mod_inner = d_left (dc);
4178 d_print_comp (dpi, options, mod_inner);
4180 /* If the modifier didn't get printed by the type, print it
4181 now. */
4182 if (! dpm.printed)
4183 d_print_mod (dpi, options, dc);
4185 dpi->modifiers = dpm.next;
4187 return;
4190 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4191 if ((options & DMGL_JAVA) == 0)
4192 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4193 dc->u.s_builtin.type->len);
4194 else
4195 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4196 dc->u.s_builtin.type->java_len);
4197 return;
4199 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4200 d_print_comp (dpi, options, d_left (dc));
4201 return;
4203 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4205 if ((options & DMGL_RET_POSTFIX) != 0)
4206 d_print_function_type (dpi,
4207 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4208 dc, dpi->modifiers);
4210 /* Print return type if present */
4211 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4212 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4213 d_left (dc));
4214 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4216 struct d_print_mod dpm;
4218 /* We must pass this type down as a modifier in order to
4219 print it in the right location. */
4220 dpm.next = dpi->modifiers;
4221 dpi->modifiers = &dpm;
4222 dpm.mod = dc;
4223 dpm.printed = 0;
4224 dpm.templates = dpi->templates;
4226 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4227 d_left (dc));
4229 dpi->modifiers = dpm.next;
4231 if (dpm.printed)
4232 return;
4234 /* In standard prefix notation, there is a space between the
4235 return type and the function signature. */
4236 if ((options & DMGL_RET_POSTFIX) == 0)
4237 d_append_char (dpi, ' ');
4240 if ((options & DMGL_RET_POSTFIX) == 0)
4241 d_print_function_type (dpi,
4242 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4243 dc, dpi->modifiers);
4245 return;
4248 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4250 struct d_print_mod *hold_modifiers;
4251 struct d_print_mod adpm[4];
4252 unsigned int i;
4253 struct d_print_mod *pdpm;
4255 /* We must pass this type down as a modifier in order to print
4256 multi-dimensional arrays correctly. If the array itself is
4257 CV-qualified, we act as though the element type were
4258 CV-qualified. We do this by copying the modifiers down
4259 rather than fiddling pointers, so that we don't wind up
4260 with a d_print_mod higher on the stack pointing into our
4261 stack frame after we return. */
4263 hold_modifiers = dpi->modifiers;
4265 adpm[0].next = hold_modifiers;
4266 dpi->modifiers = &adpm[0];
4267 adpm[0].mod = dc;
4268 adpm[0].printed = 0;
4269 adpm[0].templates = dpi->templates;
4271 i = 1;
4272 pdpm = hold_modifiers;
4273 while (pdpm != NULL
4274 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4275 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4276 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4278 if (! pdpm->printed)
4280 if (i >= sizeof adpm / sizeof adpm[0])
4282 d_print_error (dpi);
4283 return;
4286 adpm[i] = *pdpm;
4287 adpm[i].next = dpi->modifiers;
4288 dpi->modifiers = &adpm[i];
4289 pdpm->printed = 1;
4290 ++i;
4293 pdpm = pdpm->next;
4296 d_print_comp (dpi, options, d_right (dc));
4298 dpi->modifiers = hold_modifiers;
4300 if (adpm[0].printed)
4301 return;
4303 while (i > 1)
4305 --i;
4306 d_print_mod (dpi, options, adpm[i].mod);
4309 d_print_array_type (dpi, options, dc, dpi->modifiers);
4311 return;
4314 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4315 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4317 struct d_print_mod dpm;
4319 dpm.next = dpi->modifiers;
4320 dpi->modifiers = &dpm;
4321 dpm.mod = dc;
4322 dpm.printed = 0;
4323 dpm.templates = dpi->templates;
4325 d_print_comp (dpi, options, d_right (dc));
4327 /* If the modifier didn't get printed by the type, print it
4328 now. */
4329 if (! dpm.printed)
4330 d_print_mod (dpi, options, dc);
4332 dpi->modifiers = dpm.next;
4334 return;
4337 case DEMANGLE_COMPONENT_FIXED_TYPE:
4338 if (dc->u.s_fixed.sat)
4339 d_append_string (dpi, "_Sat ");
4340 /* Don't print "int _Accum". */
4341 if (dc->u.s_fixed.length->u.s_builtin.type
4342 != &cplus_demangle_builtin_types['i'-'a'])
4344 d_print_comp (dpi, options, dc->u.s_fixed.length);
4345 d_append_char (dpi, ' ');
4347 if (dc->u.s_fixed.accum)
4348 d_append_string (dpi, "_Accum");
4349 else
4350 d_append_string (dpi, "_Fract");
4351 return;
4353 case DEMANGLE_COMPONENT_ARGLIST:
4354 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4355 if (d_left (dc) != NULL)
4356 d_print_comp (dpi, options, d_left (dc));
4357 if (d_right (dc) != NULL)
4359 size_t len;
4360 unsigned long int flush_count;
4361 /* Make sure ", " isn't flushed by d_append_string, otherwise
4362 dpi->len -= 2 wouldn't work. */
4363 if (dpi->len >= sizeof (dpi->buf) - 2)
4364 d_print_flush (dpi);
4365 d_append_string (dpi, ", ");
4366 len = dpi->len;
4367 flush_count = dpi->flush_count;
4368 d_print_comp (dpi, options, d_right (dc));
4369 /* If that didn't print anything (which can happen with empty
4370 template argument packs), remove the comma and space. */
4371 if (dpi->flush_count == flush_count && dpi->len == len)
4372 dpi->len -= 2;
4374 return;
4376 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4378 struct demangle_component *type = d_left (dc);
4379 struct demangle_component *list = d_right (dc);
4381 if (type)
4382 d_print_comp (dpi, options, type);
4383 d_append_char (dpi, '{');
4384 d_print_comp (dpi, options, list);
4385 d_append_char (dpi, '}');
4387 return;
4389 case DEMANGLE_COMPONENT_OPERATOR:
4391 const struct demangle_operator_info *op = dc->u.s_operator.op;
4392 int len = op->len;
4394 d_append_string (dpi, "operator");
4395 /* Add a space before new/delete. */
4396 if (IS_LOWER (op->name[0]))
4397 d_append_char (dpi, ' ');
4398 /* Omit a trailing space. */
4399 if (op->name[len-1] == ' ')
4400 --len;
4401 d_append_buffer (dpi, op->name, len);
4402 return;
4405 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4406 d_append_string (dpi, "operator ");
4407 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4408 return;
4410 case DEMANGLE_COMPONENT_CAST:
4411 d_append_string (dpi, "operator ");
4412 d_print_cast (dpi, options, dc);
4413 return;
4415 case DEMANGLE_COMPONENT_NULLARY:
4416 d_print_expr_op (dpi, options, d_left (dc));
4417 return;
4419 case DEMANGLE_COMPONENT_UNARY:
4421 struct demangle_component *op = d_left (dc);
4422 struct demangle_component *operand = d_right (dc);
4423 const char *code = NULL;
4425 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4427 code = op->u.s_operator.op->code;
4428 if (!strcmp (code, "ad"))
4430 /* Don't print the argument list for the address of a
4431 function. */
4432 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
4433 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
4434 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4435 operand = d_left (operand);
4437 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
4439 /* This indicates a suffix operator. */
4440 operand = d_left (operand);
4441 d_print_subexpr (dpi, options, operand);
4442 d_print_expr_op (dpi, options, op);
4443 return;
4447 if (op->type != DEMANGLE_COMPONENT_CAST)
4448 d_print_expr_op (dpi, options, op);
4449 else
4451 d_append_char (dpi, '(');
4452 d_print_cast (dpi, options, op);
4453 d_append_char (dpi, ')');
4455 if (code && !strcmp (code, "gs"))
4456 /* Avoid parens after '::'. */
4457 d_print_comp (dpi, options, operand);
4458 else if (code && !strcmp (code, "st"))
4459 /* Always print parens for sizeof (type). */
4461 d_append_char (dpi, '(');
4462 d_print_comp (dpi, options, operand);
4463 d_append_char (dpi, ')');
4465 else
4466 d_print_subexpr (dpi, options, operand);
4468 return;
4470 case DEMANGLE_COMPONENT_BINARY:
4471 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4473 d_print_error (dpi);
4474 return;
4477 if (op_is_new_cast (d_left (dc)))
4479 d_print_expr_op (dpi, options, d_left (dc));
4480 d_append_char (dpi, '<');
4481 d_print_comp (dpi, options, d_left (d_right (dc)));
4482 d_append_string (dpi, ">(");
4483 d_print_comp (dpi, options, d_right (d_right (dc)));
4484 d_append_char (dpi, ')');
4485 return;
4488 /* We wrap an expression which uses the greater-than operator in
4489 an extra layer of parens so that it does not get confused
4490 with the '>' which ends the template parameters. */
4491 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4492 && d_left (dc)->u.s_operator.op->len == 1
4493 && d_left (dc)->u.s_operator.op->name[0] == '>')
4494 d_append_char (dpi, '(');
4496 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
4497 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
4499 /* Function call used in an expression should not have printed types
4500 of the function arguments. Values of the function arguments still
4501 get printed below. */
4503 const struct demangle_component *func = d_left (d_right (dc));
4505 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
4506 d_print_error (dpi);
4507 d_print_subexpr (dpi, options, d_left (func));
4509 else
4510 d_print_subexpr (dpi, options, d_left (d_right (dc)));
4511 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4513 d_append_char (dpi, '[');
4514 d_print_comp (dpi, options, d_right (d_right (dc)));
4515 d_append_char (dpi, ']');
4517 else
4519 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4520 d_print_expr_op (dpi, options, d_left (dc));
4521 d_print_subexpr (dpi, options, d_right (d_right (dc)));
4524 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4525 && d_left (dc)->u.s_operator.op->len == 1
4526 && d_left (dc)->u.s_operator.op->name[0] == '>')
4527 d_append_char (dpi, ')');
4529 return;
4531 case DEMANGLE_COMPONENT_BINARY_ARGS:
4532 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4533 d_print_error (dpi);
4534 return;
4536 case DEMANGLE_COMPONENT_TRINARY:
4537 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4538 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4540 d_print_error (dpi);
4541 return;
4544 struct demangle_component *op = d_left (dc);
4545 struct demangle_component *first = d_left (d_right (dc));
4546 struct demangle_component *second = d_left (d_right (d_right (dc)));
4547 struct demangle_component *third = d_right (d_right (d_right (dc)));
4549 if (!strcmp (op->u.s_operator.op->code, "qu"))
4551 d_print_subexpr (dpi, options, first);
4552 d_print_expr_op (dpi, options, op);
4553 d_print_subexpr (dpi, options, second);
4554 d_append_string (dpi, " : ");
4555 d_print_subexpr (dpi, options, third);
4557 else
4559 d_append_string (dpi, "new ");
4560 if (d_left (first) != NULL)
4562 d_print_subexpr (dpi, options, first);
4563 d_append_char (dpi, ' ');
4565 d_print_comp (dpi, options, second);
4566 if (third)
4567 d_print_subexpr (dpi, options, third);
4570 return;
4572 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4573 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4574 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4575 d_print_error (dpi);
4576 return;
4578 case DEMANGLE_COMPONENT_LITERAL:
4579 case DEMANGLE_COMPONENT_LITERAL_NEG:
4581 enum d_builtin_type_print tp;
4583 /* For some builtin types, produce simpler output. */
4584 tp = D_PRINT_DEFAULT;
4585 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4587 tp = d_left (dc)->u.s_builtin.type->print;
4588 switch (tp)
4590 case D_PRINT_INT:
4591 case D_PRINT_UNSIGNED:
4592 case D_PRINT_LONG:
4593 case D_PRINT_UNSIGNED_LONG:
4594 case D_PRINT_LONG_LONG:
4595 case D_PRINT_UNSIGNED_LONG_LONG:
4596 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4598 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4599 d_append_char (dpi, '-');
4600 d_print_comp (dpi, options, d_right (dc));
4601 switch (tp)
4603 default:
4604 break;
4605 case D_PRINT_UNSIGNED:
4606 d_append_char (dpi, 'u');
4607 break;
4608 case D_PRINT_LONG:
4609 d_append_char (dpi, 'l');
4610 break;
4611 case D_PRINT_UNSIGNED_LONG:
4612 d_append_string (dpi, "ul");
4613 break;
4614 case D_PRINT_LONG_LONG:
4615 d_append_string (dpi, "ll");
4616 break;
4617 case D_PRINT_UNSIGNED_LONG_LONG:
4618 d_append_string (dpi, "ull");
4619 break;
4621 return;
4623 break;
4625 case D_PRINT_BOOL:
4626 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4627 && d_right (dc)->u.s_name.len == 1
4628 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4630 switch (d_right (dc)->u.s_name.s[0])
4632 case '0':
4633 d_append_string (dpi, "false");
4634 return;
4635 case '1':
4636 d_append_string (dpi, "true");
4637 return;
4638 default:
4639 break;
4642 break;
4644 default:
4645 break;
4649 d_append_char (dpi, '(');
4650 d_print_comp (dpi, options, d_left (dc));
4651 d_append_char (dpi, ')');
4652 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4653 d_append_char (dpi, '-');
4654 if (tp == D_PRINT_FLOAT)
4655 d_append_char (dpi, '[');
4656 d_print_comp (dpi, options, d_right (dc));
4657 if (tp == D_PRINT_FLOAT)
4658 d_append_char (dpi, ']');
4660 return;
4662 case DEMANGLE_COMPONENT_NUMBER:
4663 d_append_num (dpi, dc->u.s_number.number);
4664 return;
4666 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4667 d_append_string (dpi, "java resource ");
4668 d_print_comp (dpi, options, d_left (dc));
4669 return;
4671 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4672 d_print_comp (dpi, options, d_left (dc));
4673 d_print_comp (dpi, options, d_right (dc));
4674 return;
4676 case DEMANGLE_COMPONENT_CHARACTER:
4677 d_append_char (dpi, dc->u.s_character.character);
4678 return;
4680 case DEMANGLE_COMPONENT_DECLTYPE:
4681 d_append_string (dpi, "decltype (");
4682 d_print_comp (dpi, options, d_left (dc));
4683 d_append_char (dpi, ')');
4684 return;
4686 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4688 int len;
4689 int i;
4690 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4691 if (a == NULL)
4693 /* d_find_pack won't find anything if the only packs involved
4694 in this expansion are function parameter packs; in that
4695 case, just print the pattern and "...". */
4696 d_print_subexpr (dpi, options, d_left (dc));
4697 d_append_string (dpi, "...");
4698 return;
4701 len = d_pack_length (a);
4702 dc = d_left (dc);
4703 for (i = 0; i < len; ++i)
4705 dpi->pack_index = i;
4706 d_print_comp (dpi, options, dc);
4707 if (i < len-1)
4708 d_append_string (dpi, ", ");
4711 return;
4713 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4715 long num = dc->u.s_number.number;
4716 if (num == 0)
4717 d_append_string (dpi, "this");
4718 else
4720 d_append_string (dpi, "{parm#");
4721 d_append_num (dpi, num);
4722 d_append_char (dpi, '}');
4725 return;
4727 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4728 d_append_string (dpi, "global constructors keyed to ");
4729 d_print_comp (dpi, options, dc->u.s_binary.left);
4730 return;
4732 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4733 d_append_string (dpi, "global destructors keyed to ");
4734 d_print_comp (dpi, options, dc->u.s_binary.left);
4735 return;
4737 case DEMANGLE_COMPONENT_LAMBDA:
4738 d_append_string (dpi, "{lambda(");
4739 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
4740 d_append_string (dpi, ")#");
4741 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4742 d_append_char (dpi, '}');
4743 return;
4745 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4746 d_append_string (dpi, "{unnamed type#");
4747 d_append_num (dpi, dc->u.s_number.number + 1);
4748 d_append_char (dpi, '}');
4749 return;
4751 case DEMANGLE_COMPONENT_CLONE:
4752 d_print_comp (dpi, options, d_left (dc));
4753 d_append_string (dpi, " [clone ");
4754 d_print_comp (dpi, options, d_right (dc));
4755 d_append_char (dpi, ']');
4756 return;
4758 default:
4759 d_print_error (dpi);
4760 return;
4764 /* Print a Java dentifier. For Java we try to handle encoded extended
4765 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4766 so we don't it for C++. Characters are encoded as
4767 __U<hex-char>+_. */
4769 static void
4770 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4772 const char *p;
4773 const char *end;
4775 end = name + len;
4776 for (p = name; p < end; ++p)
4778 if (end - p > 3
4779 && p[0] == '_'
4780 && p[1] == '_'
4781 && p[2] == 'U')
4783 unsigned long c;
4784 const char *q;
4786 c = 0;
4787 for (q = p + 3; q < end; ++q)
4789 int dig;
4791 if (IS_DIGIT (*q))
4792 dig = *q - '0';
4793 else if (*q >= 'A' && *q <= 'F')
4794 dig = *q - 'A' + 10;
4795 else if (*q >= 'a' && *q <= 'f')
4796 dig = *q - 'a' + 10;
4797 else
4798 break;
4800 c = c * 16 + dig;
4802 /* If the Unicode character is larger than 256, we don't try
4803 to deal with it here. FIXME. */
4804 if (q < end && *q == '_' && c < 256)
4806 d_append_char (dpi, c);
4807 p = q;
4808 continue;
4812 d_append_char (dpi, *p);
4816 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4817 qualifiers on this after printing a function. */
4819 static void
4820 d_print_mod_list (struct d_print_info *dpi, int options,
4821 struct d_print_mod *mods, int suffix)
4823 struct d_print_template *hold_dpt;
4825 if (mods == NULL || d_print_saw_error (dpi))
4826 return;
4828 if (mods->printed
4829 || (! suffix
4830 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4831 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4832 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4834 d_print_mod_list (dpi, options, mods->next, suffix);
4835 return;
4838 mods->printed = 1;
4840 hold_dpt = dpi->templates;
4841 dpi->templates = mods->templates;
4843 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4845 d_print_function_type (dpi, options, mods->mod, mods->next);
4846 dpi->templates = hold_dpt;
4847 return;
4849 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4851 d_print_array_type (dpi, options, mods->mod, mods->next);
4852 dpi->templates = hold_dpt;
4853 return;
4855 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4857 struct d_print_mod *hold_modifiers;
4858 struct demangle_component *dc;
4860 /* When this is on the modifier stack, we have pulled any
4861 qualifiers off the right argument already. Otherwise, we
4862 print it as usual, but don't let the left argument see any
4863 modifiers. */
4865 hold_modifiers = dpi->modifiers;
4866 dpi->modifiers = NULL;
4867 d_print_comp (dpi, options, d_left (mods->mod));
4868 dpi->modifiers = hold_modifiers;
4870 if ((options & DMGL_JAVA) == 0)
4871 d_append_string (dpi, "::");
4872 else
4873 d_append_char (dpi, '.');
4875 dc = d_right (mods->mod);
4877 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4879 d_append_string (dpi, "{default arg#");
4880 d_append_num (dpi, dc->u.s_unary_num.num + 1);
4881 d_append_string (dpi, "}::");
4882 dc = dc->u.s_unary_num.sub;
4885 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4886 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4887 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4888 dc = d_left (dc);
4890 d_print_comp (dpi, options, dc);
4892 dpi->templates = hold_dpt;
4893 return;
4896 d_print_mod (dpi, options, mods->mod);
4898 dpi->templates = hold_dpt;
4900 d_print_mod_list (dpi, options, mods->next, suffix);
4903 /* Print a modifier. */
4905 static void
4906 d_print_mod (struct d_print_info *dpi, int options,
4907 const struct demangle_component *mod)
4909 switch (mod->type)
4911 case DEMANGLE_COMPONENT_RESTRICT:
4912 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4913 d_append_string (dpi, " restrict");
4914 return;
4915 case DEMANGLE_COMPONENT_VOLATILE:
4916 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4917 d_append_string (dpi, " volatile");
4918 return;
4919 case DEMANGLE_COMPONENT_CONST:
4920 case DEMANGLE_COMPONENT_CONST_THIS:
4921 d_append_string (dpi, " const");
4922 return;
4923 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4924 d_append_char (dpi, ' ');
4925 d_print_comp (dpi, options, d_right (mod));
4926 return;
4927 case DEMANGLE_COMPONENT_POINTER:
4928 /* There is no pointer symbol in Java. */
4929 if ((options & DMGL_JAVA) == 0)
4930 d_append_char (dpi, '*');
4931 return;
4932 case DEMANGLE_COMPONENT_REFERENCE:
4933 d_append_char (dpi, '&');
4934 return;
4935 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4936 d_append_string (dpi, "&&");
4937 return;
4938 case DEMANGLE_COMPONENT_COMPLEX:
4939 d_append_string (dpi, "complex ");
4940 return;
4941 case DEMANGLE_COMPONENT_IMAGINARY:
4942 d_append_string (dpi, "imaginary ");
4943 return;
4944 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4945 if (d_last_char (dpi) != '(')
4946 d_append_char (dpi, ' ');
4947 d_print_comp (dpi, options, d_left (mod));
4948 d_append_string (dpi, "::*");
4949 return;
4950 case DEMANGLE_COMPONENT_TYPED_NAME:
4951 d_print_comp (dpi, options, d_left (mod));
4952 return;
4953 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4954 d_append_string (dpi, " __vector(");
4955 d_print_comp (dpi, options, d_left (mod));
4956 d_append_char (dpi, ')');
4957 return;
4959 default:
4960 /* Otherwise, we have something that won't go back on the
4961 modifier stack, so we can just print it. */
4962 d_print_comp (dpi, options, mod);
4963 return;
4967 /* Print a function type, except for the return type. */
4969 static void
4970 d_print_function_type (struct d_print_info *dpi, int options,
4971 const struct demangle_component *dc,
4972 struct d_print_mod *mods)
4974 int need_paren;
4975 int need_space;
4976 struct d_print_mod *p;
4977 struct d_print_mod *hold_modifiers;
4979 need_paren = 0;
4980 need_space = 0;
4981 for (p = mods; p != NULL; p = p->next)
4983 if (p->printed)
4984 break;
4986 switch (p->mod->type)
4988 case DEMANGLE_COMPONENT_POINTER:
4989 case DEMANGLE_COMPONENT_REFERENCE:
4990 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4991 need_paren = 1;
4992 break;
4993 case DEMANGLE_COMPONENT_RESTRICT:
4994 case DEMANGLE_COMPONENT_VOLATILE:
4995 case DEMANGLE_COMPONENT_CONST:
4996 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4997 case DEMANGLE_COMPONENT_COMPLEX:
4998 case DEMANGLE_COMPONENT_IMAGINARY:
4999 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5000 need_space = 1;
5001 need_paren = 1;
5002 break;
5003 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5004 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5005 case DEMANGLE_COMPONENT_CONST_THIS:
5006 break;
5007 default:
5008 break;
5010 if (need_paren)
5011 break;
5014 if (need_paren)
5016 if (! need_space)
5018 if (d_last_char (dpi) != '('
5019 && d_last_char (dpi) != '*')
5020 need_space = 1;
5022 if (need_space && d_last_char (dpi) != ' ')
5023 d_append_char (dpi, ' ');
5024 d_append_char (dpi, '(');
5027 hold_modifiers = dpi->modifiers;
5028 dpi->modifiers = NULL;
5030 d_print_mod_list (dpi, options, mods, 0);
5032 if (need_paren)
5033 d_append_char (dpi, ')');
5035 d_append_char (dpi, '(');
5037 if (d_right (dc) != NULL)
5038 d_print_comp (dpi, options, d_right (dc));
5040 d_append_char (dpi, ')');
5042 d_print_mod_list (dpi, options, mods, 1);
5044 dpi->modifiers = hold_modifiers;
5047 /* Print an array type, except for the element type. */
5049 static void
5050 d_print_array_type (struct d_print_info *dpi, int options,
5051 const struct demangle_component *dc,
5052 struct d_print_mod *mods)
5054 int need_space;
5056 need_space = 1;
5057 if (mods != NULL)
5059 int need_paren;
5060 struct d_print_mod *p;
5062 need_paren = 0;
5063 for (p = mods; p != NULL; p = p->next)
5065 if (! p->printed)
5067 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5069 need_space = 0;
5070 break;
5072 else
5074 need_paren = 1;
5075 need_space = 1;
5076 break;
5081 if (need_paren)
5082 d_append_string (dpi, " (");
5084 d_print_mod_list (dpi, options, mods, 0);
5086 if (need_paren)
5087 d_append_char (dpi, ')');
5090 if (need_space)
5091 d_append_char (dpi, ' ');
5093 d_append_char (dpi, '[');
5095 if (d_left (dc) != NULL)
5096 d_print_comp (dpi, options, d_left (dc));
5098 d_append_char (dpi, ']');
5101 /* Print an operator in an expression. */
5103 static void
5104 d_print_expr_op (struct d_print_info *dpi, int options,
5105 const struct demangle_component *dc)
5107 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5108 d_append_buffer (dpi, dc->u.s_operator.op->name,
5109 dc->u.s_operator.op->len);
5110 else
5111 d_print_comp (dpi, options, dc);
5114 /* Print a cast. */
5116 static void
5117 d_print_cast (struct d_print_info *dpi, int options,
5118 const struct demangle_component *dc)
5120 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5121 d_print_comp (dpi, options, d_left (dc));
5122 else
5124 struct d_print_mod *hold_dpm;
5125 struct d_print_template dpt;
5127 /* It appears that for a templated cast operator, we need to put
5128 the template parameters in scope for the operator name, but
5129 not for the parameters. The effect is that we need to handle
5130 the template printing here. */
5132 hold_dpm = dpi->modifiers;
5133 dpi->modifiers = NULL;
5135 dpt.next = dpi->templates;
5136 dpi->templates = &dpt;
5137 dpt.template_decl = d_left (dc);
5139 d_print_comp (dpi, options, d_left (d_left (dc)));
5141 dpi->templates = dpt.next;
5143 if (d_last_char (dpi) == '<')
5144 d_append_char (dpi, ' ');
5145 d_append_char (dpi, '<');
5146 d_print_comp (dpi, options, d_right (d_left (dc)));
5147 /* Avoid generating two consecutive '>' characters, to avoid
5148 the C++ syntactic ambiguity. */
5149 if (d_last_char (dpi) == '>')
5150 d_append_char (dpi, ' ');
5151 d_append_char (dpi, '>');
5153 dpi->modifiers = hold_dpm;
5157 /* Initialize the information structure we use to pass around
5158 information. */
5160 CP_STATIC_IF_GLIBCPP_V3
5161 void
5162 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5163 struct d_info *di)
5165 di->s = mangled;
5166 di->send = mangled + len;
5167 di->options = options;
5169 di->n = mangled;
5171 /* We can not need more components than twice the number of chars in
5172 the mangled string. Most components correspond directly to
5173 chars, but the ARGLIST types are exceptions. */
5174 di->num_comps = 2 * len;
5175 di->next_comp = 0;
5177 /* Similarly, we can not need more substitutions than there are
5178 chars in the mangled string. */
5179 di->num_subs = len;
5180 di->next_sub = 0;
5181 di->did_subs = 0;
5183 di->last_name = NULL;
5185 di->expansion = 0;
5188 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5189 mangled name, return strings in repeated callback giving the demangled
5190 name. OPTIONS is the usual libiberty demangler options. On success,
5191 this returns 1. On failure, returns 0. */
5193 static int
5194 d_demangle_callback (const char *mangled, int options,
5195 demangle_callbackref callback, void *opaque)
5197 enum
5199 DCT_TYPE,
5200 DCT_MANGLED,
5201 DCT_GLOBAL_CTORS,
5202 DCT_GLOBAL_DTORS
5204 type;
5205 struct d_info di;
5206 struct demangle_component *dc;
5207 int status;
5209 if (mangled[0] == '_' && mangled[1] == 'Z')
5210 type = DCT_MANGLED;
5211 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5212 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5213 && (mangled[9] == 'D' || mangled[9] == 'I')
5214 && mangled[10] == '_')
5215 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5216 else
5218 if ((options & DMGL_TYPES) == 0)
5219 return 0;
5220 type = DCT_TYPE;
5223 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5226 #ifdef CP_DYNAMIC_ARRAYS
5227 __extension__ struct demangle_component comps[di.num_comps];
5228 __extension__ struct demangle_component *subs[di.num_subs];
5230 di.comps = comps;
5231 di.subs = subs;
5232 #else
5233 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5234 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5235 #endif
5237 switch (type)
5239 case DCT_TYPE:
5240 dc = cplus_demangle_type (&di);
5241 break;
5242 case DCT_MANGLED:
5243 dc = cplus_demangle_mangled_name (&di, 1);
5244 break;
5245 case DCT_GLOBAL_CTORS:
5246 case DCT_GLOBAL_DTORS:
5247 d_advance (&di, 11);
5248 dc = d_make_comp (&di,
5249 (type == DCT_GLOBAL_CTORS
5250 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5251 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5252 d_make_demangle_mangled_name (&di, d_str (&di)),
5253 NULL);
5254 d_advance (&di, strlen (d_str (&di)));
5255 break;
5258 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5259 mangled string, then we didn't successfully demangle it. If
5260 DMGL_PARAMS is not set, we didn't look at the trailing
5261 parameters. */
5262 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5263 dc = NULL;
5265 #ifdef CP_DEMANGLE_DEBUG
5266 d_dump (dc, 0);
5267 #endif
5269 status = (dc != NULL)
5270 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5271 : 0;
5274 return status;
5277 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5278 name, return a buffer allocated with malloc holding the demangled
5279 name. OPTIONS is the usual libiberty demangler options. On
5280 success, this sets *PALC to the allocated size of the returned
5281 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5282 a memory allocation failure, and returns NULL. */
5284 static char *
5285 d_demangle (const char *mangled, int options, size_t *palc)
5287 struct d_growable_string dgs;
5288 int status;
5290 d_growable_string_init (&dgs, 0);
5292 status = d_demangle_callback (mangled, options,
5293 d_growable_string_callback_adapter, &dgs);
5294 if (status == 0)
5296 free (dgs.buf);
5297 *palc = 0;
5298 return NULL;
5301 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5302 return dgs.buf;
5305 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5307 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5309 /* ia64 ABI-mandated entry point in the C++ runtime library for
5310 performing demangling. MANGLED_NAME is a NUL-terminated character
5311 string containing the name to be demangled.
5313 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5314 *LENGTH bytes, into which the demangled name is stored. If
5315 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5316 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5317 is placed in a region of memory allocated with malloc.
5319 If LENGTH is non-NULL, the length of the buffer containing the
5320 demangled name, is placed in *LENGTH.
5322 The return value is a pointer to the start of the NUL-terminated
5323 demangled name, or NULL if the demangling fails. The caller is
5324 responsible for deallocating this memory using free.
5326 *STATUS is set to one of the following values:
5327 0: The demangling operation succeeded.
5328 -1: A memory allocation failure occurred.
5329 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5330 -3: One of the arguments is invalid.
5332 The demangling is performed using the C++ ABI mangling rules, with
5333 GNU extensions. */
5335 char *
5336 __cxa_demangle (const char *mangled_name, char *output_buffer,
5337 size_t *length, int *status)
5339 char *demangled;
5340 size_t alc;
5342 if (mangled_name == NULL)
5344 if (status != NULL)
5345 *status = -3;
5346 return NULL;
5349 if (output_buffer != NULL && length == NULL)
5351 if (status != NULL)
5352 *status = -3;
5353 return NULL;
5356 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5358 if (demangled == NULL)
5360 if (status != NULL)
5362 if (alc == 1)
5363 *status = -1;
5364 else
5365 *status = -2;
5367 return NULL;
5370 if (output_buffer == NULL)
5372 if (length != NULL)
5373 *length = alc;
5375 else
5377 if (strlen (demangled) < *length)
5379 strcpy (output_buffer, demangled);
5380 free (demangled);
5381 demangled = output_buffer;
5383 else
5385 free (output_buffer);
5386 *length = alc;
5390 if (status != NULL)
5391 *status = 0;
5393 return demangled;
5396 extern int __gcclibcxx_demangle_callback (const char *,
5397 void (*)
5398 (const char *, size_t, void *),
5399 void *);
5401 /* Alternative, allocationless entry point in the C++ runtime library
5402 for performing demangling. MANGLED_NAME is a NUL-terminated character
5403 string containing the name to be demangled.
5405 CALLBACK is a callback function, called with demangled string
5406 segments as demangling progresses; it is called at least once,
5407 but may be called more than once. OPAQUE is a generalized pointer
5408 used as a callback argument.
5410 The return code is one of the following values, equivalent to
5411 the STATUS values of __cxa_demangle() (excluding -1, since this
5412 function performs no memory allocations):
5413 0: The demangling operation succeeded.
5414 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5415 -3: One of the arguments is invalid.
5417 The demangling is performed using the C++ ABI mangling rules, with
5418 GNU extensions. */
5421 __gcclibcxx_demangle_callback (const char *mangled_name,
5422 void (*callback) (const char *, size_t, void *),
5423 void *opaque)
5425 int status;
5427 if (mangled_name == NULL || callback == NULL)
5428 return -3;
5430 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5431 callback, opaque);
5432 if (status == 0)
5433 return -2;
5435 return 0;
5438 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5440 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5441 mangled name, return a buffer allocated with malloc holding the
5442 demangled name. Otherwise, return NULL. */
5444 char *
5445 cplus_demangle_v3 (const char *mangled, int options)
5447 size_t alc;
5449 return d_demangle (mangled, options, &alc);
5453 cplus_demangle_v3_callback (const char *mangled, int options,
5454 demangle_callbackref callback, void *opaque)
5456 return d_demangle_callback (mangled, options, callback, opaque);
5459 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5460 conventions, but the output formatting is a little different.
5461 This instructs the C++ demangler not to emit pointer characters ("*"), to
5462 use Java's namespace separator symbol ("." instead of "::"), and to output
5463 JArray<TYPE> as TYPE[]. */
5465 char *
5466 java_demangle_v3 (const char *mangled)
5468 size_t alc;
5470 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5474 java_demangle_v3_callback (const char *mangled,
5475 demangle_callbackref callback, void *opaque)
5477 return d_demangle_callback (mangled,
5478 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5479 callback, opaque);
5482 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5484 #ifndef IN_GLIBCPP_V3
5486 /* Demangle a string in order to find out whether it is a constructor
5487 or destructor. Return non-zero on success. Set *CTOR_KIND and
5488 *DTOR_KIND appropriately. */
5490 static int
5491 is_ctor_or_dtor (const char *mangled,
5492 enum gnu_v3_ctor_kinds *ctor_kind,
5493 enum gnu_v3_dtor_kinds *dtor_kind)
5495 struct d_info di;
5496 struct demangle_component *dc;
5497 int ret;
5499 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5500 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5502 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5505 #ifdef CP_DYNAMIC_ARRAYS
5506 __extension__ struct demangle_component comps[di.num_comps];
5507 __extension__ struct demangle_component *subs[di.num_subs];
5509 di.comps = comps;
5510 di.subs = subs;
5511 #else
5512 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5513 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5514 #endif
5516 dc = cplus_demangle_mangled_name (&di, 1);
5518 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5519 to demangle the entire string. */
5521 ret = 0;
5522 while (dc != NULL)
5524 switch (dc->type)
5526 default:
5527 dc = NULL;
5528 break;
5529 case DEMANGLE_COMPONENT_TYPED_NAME:
5530 case DEMANGLE_COMPONENT_TEMPLATE:
5531 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5532 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5533 case DEMANGLE_COMPONENT_CONST_THIS:
5534 dc = d_left (dc);
5535 break;
5536 case DEMANGLE_COMPONENT_QUAL_NAME:
5537 case DEMANGLE_COMPONENT_LOCAL_NAME:
5538 dc = d_right (dc);
5539 break;
5540 case DEMANGLE_COMPONENT_CTOR:
5541 *ctor_kind = dc->u.s_ctor.kind;
5542 ret = 1;
5543 dc = NULL;
5544 break;
5545 case DEMANGLE_COMPONENT_DTOR:
5546 *dtor_kind = dc->u.s_dtor.kind;
5547 ret = 1;
5548 dc = NULL;
5549 break;
5554 return ret;
5557 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5558 name. A non-zero return indicates the type of constructor. */
5560 enum gnu_v3_ctor_kinds
5561 is_gnu_v3_mangled_ctor (const char *name)
5563 enum gnu_v3_ctor_kinds ctor_kind;
5564 enum gnu_v3_dtor_kinds dtor_kind;
5566 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5567 return (enum gnu_v3_ctor_kinds) 0;
5568 return ctor_kind;
5572 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5573 name. A non-zero return indicates the type of destructor. */
5575 enum gnu_v3_dtor_kinds
5576 is_gnu_v3_mangled_dtor (const char *name)
5578 enum gnu_v3_ctor_kinds ctor_kind;
5579 enum gnu_v3_dtor_kinds dtor_kind;
5581 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5582 return (enum gnu_v3_dtor_kinds) 0;
5583 return dtor_kind;
5586 #endif /* IN_GLIBCPP_V3 */
5588 #ifdef STANDALONE_DEMANGLER
5590 #include "getopt.h"
5591 #include "dyn-string.h"
5593 static void print_usage (FILE* fp, int exit_value);
5595 #define IS_ALPHA(CHAR) \
5596 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5597 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5599 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5600 #define is_mangled_char(CHAR) \
5601 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5602 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5604 /* The name of this program, as invoked. */
5605 const char* program_name;
5607 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5609 static void
5610 print_usage (FILE* fp, int exit_value)
5612 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5613 fprintf (fp, "Options:\n");
5614 fprintf (fp, " -h,--help Display this message.\n");
5615 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5616 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5617 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5619 exit (exit_value);
5622 /* Option specification for getopt_long. */
5623 static const struct option long_options[] =
5625 { "help", no_argument, NULL, 'h' },
5626 { "no-params", no_argument, NULL, 'p' },
5627 { "verbose", no_argument, NULL, 'v' },
5628 { NULL, no_argument, NULL, 0 },
5631 /* Main entry for a demangling filter executable. It will demangle
5632 its command line arguments, if any. If none are provided, it will
5633 filter stdin to stdout, replacing any recognized mangled C++ names
5634 with their demangled equivalents. */
5637 main (int argc, char *argv[])
5639 int i;
5640 int opt_char;
5641 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5643 /* Use the program name of this program, as invoked. */
5644 program_name = argv[0];
5646 /* Parse options. */
5649 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5650 switch (opt_char)
5652 case '?': /* Unrecognized option. */
5653 print_usage (stderr, 1);
5654 break;
5656 case 'h':
5657 print_usage (stdout, 0);
5658 break;
5660 case 'p':
5661 options &= ~ DMGL_PARAMS;
5662 break;
5664 case 'v':
5665 options |= DMGL_VERBOSE;
5666 break;
5669 while (opt_char != -1);
5671 if (optind == argc)
5672 /* No command line arguments were provided. Filter stdin. */
5674 dyn_string_t mangled = dyn_string_new (3);
5675 char *s;
5677 /* Read all of input. */
5678 while (!feof (stdin))
5680 char c;
5682 /* Pile characters into mangled until we hit one that can't
5683 occur in a mangled name. */
5684 c = getchar ();
5685 while (!feof (stdin) && is_mangled_char (c))
5687 dyn_string_append_char (mangled, c);
5688 if (feof (stdin))
5689 break;
5690 c = getchar ();
5693 if (dyn_string_length (mangled) > 0)
5695 #ifdef IN_GLIBCPP_V3
5696 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
5697 #else
5698 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
5699 #endif
5701 if (s != NULL)
5703 fputs (s, stdout);
5704 free (s);
5706 else
5708 /* It might not have been a mangled name. Print the
5709 original text. */
5710 fputs (dyn_string_buf (mangled), stdout);
5713 dyn_string_clear (mangled);
5716 /* If we haven't hit EOF yet, we've read one character that
5717 can't occur in a mangled name, so print it out. */
5718 if (!feof (stdin))
5719 putchar (c);
5722 dyn_string_delete (mangled);
5724 else
5725 /* Demangle command line arguments. */
5727 /* Loop over command line arguments. */
5728 for (i = optind; i < argc; ++i)
5730 char *s;
5731 #ifdef IN_GLIBCPP_V3
5732 int status;
5733 #endif
5735 /* Attempt to demangle. */
5736 #ifdef IN_GLIBCPP_V3
5737 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5738 #else
5739 s = cplus_demangle_v3 (argv[i], options);
5740 #endif
5742 /* If it worked, print the demangled name. */
5743 if (s != NULL)
5745 printf ("%s\n", s);
5746 free (s);
5748 else
5750 #ifdef IN_GLIBCPP_V3
5751 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5752 #else
5753 fprintf (stderr, "Failed: %s\n", argv[i]);
5754 #endif
5759 return 0;
5762 #endif /* STANDALONE_DEMANGLER */